diff --git a/TriviaBot.sln b/TriviaBot.sln new file mode 100644 index 0000000..93f546c --- /dev/null +++ b/TriviaBot.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TriviaBot", "TriviaBot\TriviaBot.vcxproj", "{E400396F-0C05-413E-B83E-1A4F41D86442}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E400396F-0C05-413E-B83E-1A4F41D86442}.Debug|x64.ActiveCfg = Debug|x64 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Debug|x64.Build.0 = Debug|x64 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Debug|x86.ActiveCfg = Debug|Win32 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Debug|x86.Build.0 = Debug|Win32 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Release|x64.ActiveCfg = Release|x64 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Release|x64.Build.0 = Release|x64 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Release|x86.ActiveCfg = Release|Win32 + {E400396F-0C05-413E-B83E-1A4F41D86442}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TriviaBot/CSV/LICENSE b/TriviaBot/CSV/LICENSE new file mode 100644 index 0000000..da603a9 --- /dev/null +++ b/TriviaBot/CSV/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2015, ben-strasser +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of fast-cpp-csv-parser nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/TriviaBot/CSV/LoadDB.cpp b/TriviaBot/CSV/LoadDB.cpp new file mode 100644 index 0000000..950c5a4 --- /dev/null +++ b/TriviaBot/CSV/LoadDB.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include "csv.h" + +/** +/ Takes the questions stored in the CSV downloaded from https://www.reddit.com/r/trivia/comments/3wzpvt/free_database_of_50000_trivia_questions/ +/ Questions are stored in a weird format, which makes it a lot harder. +/ Hideous code, but only needs to be run one time. +/ +**/ + +static int callback(void *x, int argc, char **argv, char **azColName) { + int i; + for (i = 0; i, io::double_quote_escape<',', '"'>> in("trivia.csv"); + in.read_header(io::ignore_extra_column, "Category", "Question", "Answer", "Category1", "Question1", "Answer1", "Category2", "Question2", "Answer2"); + std::string c0, q0, a0, c1, q1, a1, c2, q2, a2; + int row = 0; + sqlite3_stmt *insertThreeQuestions; + std::string sql; + while (in.read_row(c0, q0, a0, c1, q1, a1, c2, q2, a2)) { + sql = "INSERT INTO Questions (Category, Question, Answer) VALUES (?1, ?2, ?3), (?4, ?5, ?6), (?7, ?8, ?9);"; + sqlite3_prepare_v2(db, sql.c_str(), -1, &insertThreeQuestions, NULL); + sqlite3_bind_text(insertThreeQuestions, 1, c0.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 2, q0.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 3, a0.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 4, c1.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 5, q1.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 6, a1.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 7, c2.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 8, q2.c_str(), -1, ((sqlite3_destructor_type)-1)); + sqlite3_bind_text(insertThreeQuestions, 9, a2.c_str(), -1, ((sqlite3_destructor_type)-1)); + + int result = sqlite3_step(insertThreeQuestions); + std::cout << result << " "; + + } + std::cout << std::endl; + + sqlite3_close(db); + + std::getchar(); + return 0; +} \ No newline at end of file diff --git a/TriviaBot/CSV/csv.h b/TriviaBot/CSV/csv.h new file mode 100644 index 0000000..4d59618 --- /dev/null +++ b/TriviaBot/CSV/csv.h @@ -0,0 +1,1258 @@ +// Copyright: (2012-2015) Ben Strasser +// License: BSD-3 +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +//2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +//3. Neither the name of the copyright holder nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#ifndef CSV_H +#define CSV_H + +#include +#include +#include +#include +#include +#include +#include +#ifndef CSV_IO_NO_THREAD +#include +#include +#include +#endif +#include +#include +#include +#include + +namespace io{ + //////////////////////////////////////////////////////////////////////////// + // LineReader // + //////////////////////////////////////////////////////////////////////////// + + namespace error{ + struct base : std::exception{ + virtual void format_error_message()const = 0; + + const char*what()const throw(){ + format_error_message(); + return error_message_buffer; + } + + mutable char error_message_buffer[256]; + }; + + const int max_file_name_length = 255; + + struct with_file_name{ + with_file_name(){ + std::memset(file_name, 0, max_file_name_length+1); + } + + void set_file_name(const char*file_name){ + std::strncpy(this->file_name, file_name, max_file_name_length); + this->file_name[max_file_name_length] = '\0'; + } + + char file_name[max_file_name_length+1]; + }; + + struct with_file_line{ + with_file_line(){ + file_line = -1; + } + + void set_file_line(int file_line){ + this->file_line = file_line; + } + + int file_line; + }; + + struct with_errno{ + with_errno(){ + errno_value = 0; + } + + void set_errno(int errno_value){ + this->errno_value = errno_value; + } + + int errno_value; + }; + + struct can_not_open_file : + base, + with_file_name, + with_errno{ + void format_error_message()const{ + if(errno_value != 0) + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Can not open file \"%s\" because \"%s\"." + , file_name, std::strerror(errno_value)); + else + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Can not open file \"%s\"." + , file_name); + } + }; + + struct line_length_limit_exceeded : + base, + with_file_name, + with_file_line{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Line number %d in file \"%s\" exceeds the maximum length of 2^24-1." + , file_line, file_name); + } + }; + } + + class ByteSourceBase{ + public: + virtual int read(char*buffer, int size)=0; + virtual ~ByteSourceBase(){} + }; + + namespace detail{ + + class OwningStdIOByteSourceBase : public ByteSourceBase{ + public: + explicit OwningStdIOByteSourceBase(FILE*file):file(file){ + // Tell the std library that we want to do the buffering ourself. + std::setvbuf(file, 0, _IONBF, 0); + } + + int read(char*buffer, int size){ + return std::fread(buffer, 1, size, file); + } + + ~OwningStdIOByteSourceBase(){ + std::fclose(file); + } + + private: + FILE*file; + }; + + class NonOwningIStreamByteSource : public ByteSourceBase{ + public: + explicit NonOwningIStreamByteSource(std::istream&in):in(in){} + + int read(char*buffer, int size){ + in.read(buffer, size); + return in.gcount(); + } + + ~NonOwningIStreamByteSource(){} + + private: + std::istream∈ + }; + + class NonOwningStringByteSource : public ByteSourceBase{ + public: + NonOwningStringByteSource(const char*str, long long size):str(str), remaining_byte_count(size){} + + int read(char*buffer, int desired_byte_count){ + int to_copy_byte_count = desired_byte_count; + if(remaining_byte_count < to_copy_byte_count) + to_copy_byte_count = remaining_byte_count; + std::memcpy(buffer, str, to_copy_byte_count); + remaining_byte_count -= to_copy_byte_count; + str += to_copy_byte_count; + return to_copy_byte_count; + } + + ~NonOwningStringByteSource(){} + + private: + const char*str; + long long remaining_byte_count; + }; + + #ifndef CSV_IO_NO_THREAD + class AsynchronousReader{ + public: + void init(std::unique_ptrarg_byte_source){ + std::unique_lockguard(lock); + byte_source = std::move(arg_byte_source); + desired_byte_count = -1; + termination_requested = false; + worker = std::thread( + [&]{ + std::unique_lockguard(lock); + try{ + for(;;){ + read_requested_condition.wait( + guard, + [&]{ + return desired_byte_count != -1 || termination_requested; + } + ); + if(termination_requested) + return; + + read_byte_count = byte_source->read(buffer, desired_byte_count); + desired_byte_count = -1; + if(read_byte_count == 0) + break; + read_finished_condition.notify_one(); + } + }catch(...){ + read_error = std::current_exception(); + } + read_finished_condition.notify_one(); + } + ); + } + + bool is_valid()const{ + return byte_source != 0; + } + + void start_read(char*arg_buffer, int arg_desired_byte_count){ + std::unique_lockguard(lock); + buffer = arg_buffer; + desired_byte_count = arg_desired_byte_count; + read_byte_count = -1; + read_requested_condition.notify_one(); + } + + int finish_read(){ + std::unique_lockguard(lock); + read_finished_condition.wait( + guard, + [&]{ + return read_byte_count != -1 || read_error; + } + ); + if(read_error) + std::rethrow_exception(read_error); + else + return read_byte_count; + } + + ~AsynchronousReader(){ + if(byte_source != 0){ + { + std::unique_lockguard(lock); + termination_requested = true; + } + read_requested_condition.notify_one(); + worker.join(); + } + } + + private: + std::unique_ptrbyte_source; + + std::thread worker; + + bool termination_requested; + std::exception_ptr read_error; + char*buffer; + int desired_byte_count; + int read_byte_count; + + std::mutex lock; + std::condition_variable read_finished_condition; + std::condition_variable read_requested_condition; + }; + #endif + + class SynchronousReader{ + public: + void init(std::unique_ptrarg_byte_source){ + byte_source = std::move(arg_byte_source); + } + + bool is_valid()const{ + return byte_source != 0; + } + + void start_read(char*arg_buffer, int arg_desired_byte_count){ + buffer = arg_buffer; + desired_byte_count = arg_desired_byte_count; + } + + int finish_read(){ + return byte_source->read(buffer, desired_byte_count); + } + private: + std::unique_ptrbyte_source; + char*buffer; + int desired_byte_count; + }; + } + + class LineReader{ + private: + static const int block_len = 1<<24; + #ifdef CSV_IO_NO_THREAD + detail::SynchronousReader reader; + #else + detail::AsynchronousReader reader; + #endif + char*buffer; + int data_begin; + int data_end; + + char file_name[error::max_file_name_length+1]; + unsigned file_line; + + static std::unique_ptr open_file(const char*file_name){ + // We open the file in binary mode as it makes no difference under *nix + // and under Windows we handle \r\n newlines ourself. + FILE*file = std::fopen(file_name, "rb"); + if(file == 0){ + int x = errno; // store errno as soon as possible, doing it after constructor call can fail. + error::can_not_open_file err; + err.set_errno(x); + err.set_file_name(file_name); + throw err; + } + return std::unique_ptr(new detail::OwningStdIOByteSourceBase(file)); + } + + void init(std::unique_ptrbyte_source){ + file_line = 0; + + buffer = new char[3*block_len]; + try{ + data_begin = 0; + data_end = byte_source->read(buffer, 2*block_len); + + // Ignore UTF-8 BOM + if(data_end >= 3 && buffer[0] == '\xEF' && buffer[1] == '\xBB' && buffer[2] == '\xBF') + data_begin = 3; + + if(data_end == 2*block_len){ + reader.init(std::move(byte_source)); + reader.start_read(buffer + 2*block_len, block_len); + } + }catch(...){ + delete[]buffer; + throw; + } + } + + public: + LineReader() = delete; + LineReader(const LineReader&) = delete; + LineReader&operator=(const LineReader&) = delete; + + explicit LineReader(const char*file_name){ + set_file_name(file_name); + init(open_file(file_name)); + } + + explicit LineReader(const std::string&file_name){ + set_file_name(file_name.c_str()); + init(open_file(file_name.c_str())); + } + + LineReader(const char*file_name, std::unique_ptrbyte_source){ + set_file_name(file_name); + init(std::move(byte_source)); + } + + LineReader(const std::string&file_name, std::unique_ptrbyte_source){ + set_file_name(file_name.c_str()); + init(std::move(byte_source)); + } + + LineReader(const char*file_name, const char*data_begin, const char*data_end){ + set_file_name(file_name); + init(std::unique_ptr(new detail::NonOwningStringByteSource(data_begin, data_end-data_begin))); + } + + LineReader(const std::string&file_name, const char*data_begin, const char*data_end){ + set_file_name(file_name.c_str()); + init(std::unique_ptr(new detail::NonOwningStringByteSource(data_begin, data_end-data_begin))); + } + + LineReader(const char*file_name, FILE*file){ + set_file_name(file_name); + init(std::unique_ptr(new detail::OwningStdIOByteSourceBase(file))); + } + + LineReader(const std::string&file_name, FILE*file){ + set_file_name(file_name.c_str()); + init(std::unique_ptr(new detail::OwningStdIOByteSourceBase(file))); + } + + LineReader(const char*file_name, std::istream&in){ + set_file_name(file_name); + init(std::unique_ptr(new detail::NonOwningIStreamByteSource(in))); + } + + LineReader(const std::string&file_name, std::istream&in){ + set_file_name(file_name.c_str()); + init(std::unique_ptr(new detail::NonOwningIStreamByteSource(in))); + } + + void set_file_name(const std::string&file_name){ + set_file_name(file_name.c_str()); + } + + void set_file_name(const char*file_name){ + strncpy(this->file_name, file_name, error::max_file_name_length); + this->file_name[error::max_file_name_length] = '\0'; + } + + const char*get_truncated_file_name()const{ + return file_name; + } + + void set_file_line(unsigned file_line){ + this->file_line = file_line; + } + + unsigned get_file_line()const{ + return file_line; + } + + char*next_line(){ + if(data_begin == data_end) + return 0; + + ++file_line; + + assert(data_begin < data_end); + assert(data_end <= block_len*2); + + if(data_begin >= block_len){ + std::memcpy(buffer, buffer+block_len, block_len); + data_begin -= block_len; + data_end -= block_len; + if(reader.is_valid()) + { + data_end += reader.finish_read(); + std::memcpy(buffer+block_len, buffer+2*block_len, block_len); + reader.start_read(buffer + 2*block_len, block_len); + } + } + + int line_end = data_begin; + while(buffer[line_end] != '\n' && line_end != data_end){ + ++line_end; + } + + if(line_end - data_begin + 1 > block_len){ + error::line_length_limit_exceeded err; + err.set_file_name(file_name); + err.set_file_line(file_line); + throw err; + } + + if(buffer[line_end] == '\n'){ + buffer[line_end] = '\0'; + }else{ + // some files are missing the newline at the end of the + // last line + ++data_end; + buffer[line_end] = '\0'; + } + + // handle windows \r\n-line breaks + if(line_end != data_begin && buffer[line_end-1] == '\r') + buffer[line_end-1] = '\0'; + + char*ret = buffer + data_begin; + data_begin = line_end+1; + return ret; + } + + ~LineReader(){ + delete[] buffer; + } + }; + + + //////////////////////////////////////////////////////////////////////////// + // CSV // + //////////////////////////////////////////////////////////////////////////// + + namespace error{ + const int max_column_name_length = 63; + struct with_column_name{ + with_column_name(){ + std::memset(column_name, 0, max_column_name_length+1); + } + + void set_column_name(const char*column_name){ + std::strncpy(this->column_name, column_name, max_column_name_length); + this->column_name[max_column_name_length] = '\0'; + } + + char column_name[max_column_name_length+1]; + }; + + + const int max_column_content_length = 63; + + struct with_column_content{ + with_column_content(){ + std::memset(column_content, 0, max_column_content_length+1); + } + + void set_column_content(const char*column_content){ + std::strncpy(this->column_content, column_content, max_column_content_length); + this->column_content[max_column_content_length] = '\0'; + } + + char column_content[max_column_content_length+1]; + }; + + + struct extra_column_in_header : + base, + with_file_name, + with_column_name{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Extra column \"%s\" in header of file \"%s\"." + , column_name, file_name); + } + }; + + struct missing_column_in_header : + base, + with_file_name, + with_column_name{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Missing column \"%s\" in header of file \"%s\"." + , column_name, file_name); + } + }; + + struct duplicated_column_in_header : + base, + with_file_name, + with_column_name{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Duplicated column \"%s\" in header of file \"%s\"." + , column_name, file_name); + } + }; + + struct header_missing : + base, + with_file_name{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Header missing in file \"%s\"." + , file_name); + } + }; + + struct too_few_columns : + base, + with_file_name, + with_file_line{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Too few columns in line %d in file \"%s\"." + , file_line, file_name); + } + }; + + struct too_many_columns : + base, + with_file_name, + with_file_line{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Too many columns in line %d in file \"%s\"." + , file_line, file_name); + } + }; + + struct escaped_string_not_closed : + base, + with_file_name, + with_file_line{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "Escaped string was not closed in line %d in file \"%s\"." + , file_line, file_name); + } + }; + + struct integer_must_be_positive : + base, + with_file_name, + with_file_line, + with_column_name, + with_column_content{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "The integer \"%s\" must be positive or 0 in column \"%s\" in file \"%s\" in line \"%d\"." + , column_content, column_name, file_name, file_line); + } + }; + + struct no_digit : + base, + with_file_name, + with_file_line, + with_column_name, + with_column_content{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "The integer \"%s\" contains an invalid digit in column \"%s\" in file \"%s\" in line \"%d\"." + , column_content, column_name, file_name, file_line); + } + }; + + struct integer_overflow : + base, + with_file_name, + with_file_line, + with_column_name, + with_column_content{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "The integer \"%s\" overflows in column \"%s\" in file \"%s\" in line \"%d\"." + , column_content, column_name, file_name, file_line); + } + }; + + struct integer_underflow : + base, + with_file_name, + with_file_line, + with_column_name, + with_column_content{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "The integer \"%s\" underflows in column \"%s\" in file \"%s\" in line \"%d\"." + , column_content, column_name, file_name, file_line); + } + }; + + struct invalid_single_character : + base, + with_file_name, + with_file_line, + with_column_name, + with_column_content{ + void format_error_message()const{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "The content \"%s\" of column \"%s\" in file \"%s\" in line \"%d\" is not a single character." + , column_content, column_name, file_name, file_line); + } + }; + } + + typedef unsigned ignore_column; + static const ignore_column ignore_no_column = 0; + static const ignore_column ignore_extra_column = 1; + static const ignore_column ignore_missing_column = 2; + + template + struct trim_chars{ + private: + constexpr static bool is_trim_char(char){ + return false; + } + + template + constexpr static bool is_trim_char(char c, char trim_char, OtherTrimChars...other_trim_chars){ + return c == trim_char || is_trim_char(c, other_trim_chars...); + } + + public: + static void trim(char*&str_begin, char*&str_end){ + while(is_trim_char(*str_begin, trim_char_list...) && str_begin != str_end) + ++str_begin; + while(is_trim_char(*(str_end-1), trim_char_list...) && str_begin != str_end) + --str_end; + *str_end = '\0'; + } + }; + + + struct no_comment{ + static bool is_comment(const char*){ + return false; + } + }; + + template + struct single_line_comment{ + private: + constexpr static bool is_comment_start_char(char){ + return false; + } + + template + constexpr static bool is_comment_start_char(char c, char comment_start_char, OtherCommentStartChars...other_comment_start_chars){ + return c == comment_start_char || is_comment_start_char(c, other_comment_start_chars...); + } + + public: + + static bool is_comment(const char*line){ + return is_comment_start_char(*line, comment_start_char_list...); + } + }; + + struct empty_line_comment{ + static bool is_comment(const char*line){ + if(*line == '\0') + return true; + while(*line == ' ' || *line == '\t'){ + ++line; + if(*line == 0) + return true; + } + return false; + } + }; + + template + struct single_and_empty_line_comment{ + static bool is_comment(const char*line){ + return single_line_comment::is_comment(line) || empty_line_comment::is_comment(line); + } + }; + + template + struct no_quote_escape{ + static const char*find_next_column_end(const char*col_begin){ + while(*col_begin != sep && *col_begin != '\0') + ++col_begin; + return col_begin; + } + + static void unescape(char*&, char*&){ + + } + }; + + template + struct double_quote_escape{ + static const char*find_next_column_end(const char*col_begin){ + while(*col_begin != sep && *col_begin != '\0') + if(*col_begin != quote) + ++col_begin; + else{ + do{ + ++col_begin; + while(*col_begin != quote){ + if(*col_begin == '\0') + throw error::escaped_string_not_closed(); + ++col_begin; + } + ++col_begin; + }while(*col_begin == quote); + } + return col_begin; + } + + static void unescape(char*&col_begin, char*&col_end){ + if(col_end - col_begin >= 2){ + if(*col_begin == quote && *(col_end-1) == quote){ + ++col_begin; + --col_end; + char*out = col_begin; + for(char*in = col_begin; in!=col_end; ++in){ + if(*in == quote && *(in+1) == quote){ + ++in; + } + *out = *in; + ++out; + } + col_end = out; + *col_end = '\0'; + } + } + + } + }; + + struct throw_on_overflow{ + template + static void on_overflow(T&){ + throw error::integer_overflow(); + } + + template + static void on_underflow(T&){ + throw error::integer_underflow(); + } + }; + + struct ignore_overflow{ + template + static void on_overflow(T&){} + + template + static void on_underflow(T&){} + }; + + struct set_to_max_on_overflow{ + template + static void on_overflow(T&x){ + x = std::numeric_limits::max(); + } + + template + static void on_underflow(T&x){ + x = std::numeric_limits::min(); + } + }; + + + namespace detail{ + template + void chop_next_column( + char*&line, char*&col_begin, char*&col_end + ){ + assert(line != nullptr); + + col_begin = line; + // the col_begin + (... - col_begin) removes the constness + col_end = col_begin + (quote_policy::find_next_column_end(col_begin) - col_begin); + + if(*col_end == '\0'){ + line = nullptr; + }else{ + *col_end = '\0'; + line = col_end + 1; + } + } + + template + void parse_line( + char*line, + char**sorted_col, + const std::vector&col_order + ){ + for(std::size_t i=0; i(line, col_begin, col_end); + + if(col_order[i] != -1){ + trim_policy::trim(col_begin, col_end); + quote_policy::unescape(col_begin, col_end); + + sorted_col[col_order[i]] = col_begin; + } + } + if(line != nullptr) + throw ::io::error::too_many_columns(); + } + + template + void parse_header_line( + char*line, + std::vector&col_order, + const std::string*col_name, + ignore_column ignore_policy + ){ + col_order.clear(); + + bool found[column_count]; + std::fill(found, found + column_count, false); + while(line){ + char*col_begin,*col_end; + chop_next_column(line, col_begin, col_end); + + trim_policy::trim(col_begin, col_end); + quote_policy::unescape(col_begin, col_end); + + for(unsigned i=0; i + void parse(char*col, char &x){ + if(!*col) + throw error::invalid_single_character(); + x = *col; + ++col; + if(*col) + throw error::invalid_single_character(); + } + + template + void parse(char*col, std::string&x){ + x = col; + } + + template + void parse(char*col, const char*&x){ + x = col; + } + + template + void parse(char*col, char*&x){ + x = col; + } + + template + void parse_unsigned_integer(const char*col, T&x){ + x = 0; + while(*col != '\0'){ + if('0' <= *col && *col <= '9'){ + T y = *col - '0'; + if(x > (std::numeric_limits::max()-y)/10){ + overflow_policy::on_overflow(x); + return; + } + x = 10*x+y; + }else + throw error::no_digit(); + ++col; + } + } + + templatevoid parse(char*col, unsigned char &x) + {parse_unsigned_integer(col, x);} + templatevoid parse(char*col, unsigned short &x) + {parse_unsigned_integer(col, x);} + templatevoid parse(char*col, unsigned int &x) + {parse_unsigned_integer(col, x);} + templatevoid parse(char*col, unsigned long &x) + {parse_unsigned_integer(col, x);} + templatevoid parse(char*col, unsigned long long &x) + {parse_unsigned_integer(col, x);} + + template + void parse_signed_integer(const char*col, T&x){ + if(*col == '-'){ + ++col; + + x = 0; + while(*col != '\0'){ + if('0' <= *col && *col <= '9'){ + T y = *col - '0'; + if(x < (std::numeric_limits::min()+y)/10){ + overflow_policy::on_underflow(x); + return; + } + x = 10*x-y; + }else + throw error::no_digit(); + ++col; + } + return; + }else if(*col == '+') + ++col; + parse_unsigned_integer(col, x); + } + + templatevoid parse(char*col, signed char &x) + {parse_signed_integer(col, x);} + templatevoid parse(char*col, signed short &x) + {parse_signed_integer(col, x);} + templatevoid parse(char*col, signed int &x) + {parse_signed_integer(col, x);} + templatevoid parse(char*col, signed long &x) + {parse_signed_integer(col, x);} + templatevoid parse(char*col, signed long long &x) + {parse_signed_integer(col, x);} + + template + void parse_float(const char*col, T&x){ + bool is_neg = false; + if(*col == '-'){ + is_neg = true; + ++col; + }else if(*col == '+') + ++col; + + x = 0; + while('0' <= *col && *col <= '9'){ + int y = *col - '0'; + x *= 10; + x += y; + ++col; + } + + if(*col == '.'|| *col == ','){ + ++col; + T pos = 1; + while('0' <= *col && *col <= '9'){ + pos /= 10; + int y = *col - '0'; + ++col; + x += y*pos; + } + } + + if(*col == 'e' || *col == 'E'){ + ++col; + int e; + + parse_signed_integer(col, e); + + if(e != 0){ + T base; + if(e < 0){ + base = 0.1; + e = -e; + }else{ + base = 10; + } + + while(e != 1){ + if((e & 1) == 0){ + base = base*base; + e >>= 1; + }else{ + x *= base; + --e; + } + } + x *= base; + } + }else{ + if(*col != '\0') + throw error::no_digit(); + } + + if(is_neg) + x = -x; + } + + template void parse(char*col, float&x) { parse_float(col, x); } + template void parse(char*col, double&x) { parse_float(col, x); } + template void parse(char*col, long double&x) { parse_float(col, x); } + + template + void parse(char*col, T&x){ + // GCC evalutes "false" when reading the template and + // "sizeof(T)!=sizeof(T)" only when instantiating it. This is why + // this strange construct is used. + static_assert(sizeof(T)!=sizeof(T), + "Can not parse this type. Only buildin integrals, floats, char, char*, const char* and std::string are supported"); + } + + } + + template, + class quote_policy = no_quote_escape<','>, + class overflow_policy = throw_on_overflow, + class comment_policy = no_comment + > + class CSVReader{ + private: + LineReader in; + + char*(row[column_count]); + std::string column_names[column_count]; + + std::vectorcol_order; + + template + void set_column_names(std::string s, ColNames...cols){ + column_names[column_count-sizeof...(ColNames)-1] = std::move(s); + set_column_names(std::forward(cols)...); + } + + void set_column_names(){} + + + public: + CSVReader() = delete; + CSVReader(const CSVReader&) = delete; + CSVReader&operator=(const CSVReader&); + + template + explicit CSVReader(Args&&...args):in(std::forward(args)...){ + std::fill(row, row+column_count, nullptr); + col_order.resize(column_count); + for(unsigned i=0; i + void read_header(ignore_column ignore_policy, ColNames...cols){ + static_assert(sizeof...(ColNames)>=column_count, "not enough column names specified"); + static_assert(sizeof...(ColNames)<=column_count, "too many column names specified"); + try{ + set_column_names(std::forward(cols)...); + + char*line; + do{ + line = in.next_line(); + if(!line) + throw error::header_missing(); + }while(comment_policy::is_comment(line)); + + detail::parse_header_line + + (line, col_order, column_names, ignore_policy); + }catch(error::with_file_name&err){ + err.set_file_name(in.get_truncated_file_name()); + throw; + } + } + + template + void set_header(ColNames...cols){ + static_assert(sizeof...(ColNames)>=column_count, + "not enough column names specified"); + static_assert(sizeof...(ColNames)<=column_count, + "too many column names specified"); + set_column_names(std::forward(cols)...); + std::fill(row, row+column_count, nullptr); + col_order.resize(column_count); + for(unsigned i=0; i + void parse_helper(std::size_t r, T&t, ColType&...cols){ + if(row[r]){ + try{ + try{ + ::io::detail::parse(row[r], t); + }catch(error::with_column_content&err){ + err.set_column_content(row[r]); + throw; + } + }catch(error::with_column_name&err){ + err.set_column_name(column_names[r].c_str()); + throw; + } + } + parse_helper(r+1, cols...); + } + + + public: + template + bool read_row(ColType& ...cols){ + static_assert(sizeof...(ColType)>=column_count, + "not enough columns specified"); + static_assert(sizeof...(ColType)<=column_count, + "too many columns specified"); + try{ + try{ + + char*line; + do{ + line = in.next_line(); + if(!line) + return false; + }while(comment_policy::is_comment(line)); + + detail::parse_line + (line, row, col_order); + + parse_helper(0, cols...); + }catch(error::with_file_name&err){ + err.set_file_name(in.get_truncated_file_name()); + throw; + } + }catch(error::with_file_line&err){ + err.set_file_line(in.get_file_line()); + throw; + } + + return true; + } + }; +} +#endif + diff --git a/TriviaBot/CSV/trivia.csv b/TriviaBot/CSV/trivia.csv new file mode 100644 index 0000000..9cdad44 --- /dev/null +++ b/TriviaBot/CSV/trivia.csv @@ -0,0 +1,15236 @@ +Category,Question,Answer,Category1,Question1,Answer1,Category2,Question2,Answer2 +General,Juliet 'i melt with you' was which band's signature hit released on their 'after the snow' album in 1982,Modern english,General,The Norse god of light & peace,Balder,Science & Nature,What is the more scientific name for quicksilver?,Mercury +Geography,"""Honolulu"" means _____________",Sheltered harbor,General,What was the first day of the Roman month called,The calends,General,What did Alexander Graham Bell muffle to keep it from interrupting his work,His telephone the telephone telephone +General,How many gallons of fuel does a jumbo jet use during take off?,Four thousand,General,What is the unit of currency in Laos?,Kip,Entertainment,What is a violoncello usually called?,Cello +Sports & Leisure,In the game of cricket which bird name means scoring no runs ,Duck ,General,5:45 pm In military time is how many hours,1745,General,What sequence is this the start of: 1 4 11 20 31 44 61 100,Octal squares +Music,With which singer did Westlife record a version of Against all Odds in 2000?,Mariah Carey,General,In what sport are bacon hamburgers chips prunes spuds terms,Mountain Biking injury / damage,General,"According to the Talmud, who was the first wife of Adam",Lilith + History & Holidays,"In 1951 Howard Hawks Produced The Thing. Who Directed The 1982 remake, Starring Kurt Russell ",John Carpenter ,Entertainment,Who was Barney Rubble's best friend?,Fred Flintstone,Science & Nature,Which Is The Most Sensitive Of The Senses ,Smell  +General,The Paris stock exchange,Bourse,General,Alfred Schneider became famous as who,Lenny Bruce, History & Holidays,What Is The Original Name For Halloween ,Samhain (pron) sow-en  + History & Holidays,What amendment to the u.s. constitution ended prohibition ,The twenty first ,General,Burn to stop the flow of blood,Cauterize,General,What brand name did Galvin Manufacturing give to its line of radios for motorists,Motorola +General,What game of chance was originally called 'Beano',Bingo,Entertainment,"Who did a version of 'One Bourbon, One Scotch, One Beer' on his 1977 debut album?",George Thorogood,General,What does the acronym NIMBY mean,Not in my back yard + History & Holidays,Where Did Napoleon Bonaparte Die In Exile? ,St Helena ,Geography,The United States would fit into the continent of ____________ three and a half times.,Africa,Science & Nature,What do the auricularis muscles move?,Ears +Food & Drink,The eggs of this sturgeon are the preferred form of caviar.,Beluga,General,What links Ada - Lisp - Algol,Program Languages,General,"What kind of dog was ""rin tin tin""",German shepherd +General,What is the motto of the three muskateers,All for one & one for all, Geography,The Ionian and Cyclades are island groups of which country?,Greece,General,"What links Sword, Square, Floral and Barn",Types of Dance +General,What is the flower that stands for: a heart ignorant of love,White rosebud,People & Places,What Was William Bonney Better Known As ,Billy The Kid ,General,"Of which fruit is ""pearmain"" a variety",Apple +General,What nationality was the sculpture Brancusi,Romanian,Geography,What Is The Most Westerly European Capital City ,"Lisbon In Portugal / Reykjavic In Iceland, It's Complex But I Think Iceland Has It You Decide ",General,What tragedy occurred two years to the day after the federal raid on the branch Davidian complex in Waco,Oklahoma city bombing +Toys & Games,"Charles Darrow based his game upon the 1904 ""The Landlord's Game"".",Monopoly,General,Melita in the Bible where Paul was shipwrecked is where today,Malta,Science & Nature," There are about 5,000 species of __________ known. Only about half of them build reefs.",Coral +General,"Who sang ""I Want My MTV"" on the Dire Straits song ""Money For Nothing""?",Sting,General,Remove forcibly or exile to another country,Deport,General,For which film did James Cagney win an oscar,Yankee doodle dandy + History & Holidays,"What country did poinsettias originally come from? (Mexico,Us, Spain ,Brazil) ",Mexico ,General,The seventeen year locust is also know as a _____,Cicada,Entertainment,"Which brand of guitar is played by Jimmy Page, Slash, and Brian May?",Gibson Les Paul +Music,Who Starred As Cole Porter In The 1945 Film Night & Day,Cary Grant,General,The Chinese year cycle starts with 1st to ask Buddha which,Rat,General,"Which group sang the song ""All The Small Things""?",Blink 182 +General,"Before Hitting The Big Time As A Singer & Tv Personality What Was ""Perry Como's "" Profession",A Barber / Hair Dresser,General,Countess Roza Marie Lubienski Is More Commonly Known As Who,Rula Lenska,General,Gymnophobia is the fear of __________.,Naked bodies +Music,What was finally a top ten hit for Ce Ce Peniston on its 1992 Re-Release,Finally,General,"Name the variety of brassica , whose thickened stem, resembling a turnip, is eaten as a vegetable",Kohlrabi,General,"American rocket engineer, born in Worcester, Massachusetts, & educated at Worcester Polytechnic Institute & Clark University",Goddard +General,"Seventeen thousand how many camels carried the 117,000 volumes of abdul kassem ismael's library",400,General,Which is the smallest independent country,Vatican city,General,What is the fear of choking or being smothered known as,Pnigophobia +General,Under Michigan State law who are officially classed mechanics,Dentists,Geography,What is the capital of Croatia? ,Zagreb ,General,What is a group of this animal called: Ant,Colony army +General,"Medieval monks invented this stopper, necessary for champagne",Cork,Entertainment,"This 1974 film started a run of nostalgia culminating in the TV series ""Happy Days"".",American Graffiti,General,Who went to sea with silver buckles at his knees,Bobby shaftoe +General,In the body luteinizing hormone is produced by what gland,Pituitary,Religion & Mythology,To where do Muslims make pilgrimage?,Mecca,General,"The celtic queen, Boudicca (Boadicea), allegedly took poison to prevent torture by the Romans after they defeated her rebel forces in battle. Where is she thought to be buried",Kings cross station +Food & Drink,What colour is chablis? ,White ,General,"Who wrote the book ""Darker Than Amber""",John macdonald,General,What sport can you play at Indonesia's Senayan Statium,Badminton +Sports & Leisure,Football: The Denver ____?,Broncos,General,At The Oscars Ceremony In March 2002 Which Film Won The Award For Best Picture,A Beautiful Mind,General,Betsy ross is the only real person to ever have been the head of a ______,Pez dispenser +General,What are the stars in traditional Cornish Star Gazey pie,Pilchards eyes, History & Holidays,Who rode naked through the streets of Coventry,Lady godiva,Science & Nature,What Is The Most Common Blood Group ,O  +General,What is raku,Japanese pottery,Sports & Leisure,Who won the Bahrain Grand Prix at weekend? ,Fernando Alonso ,General,Maclaine how many letters (maximum) can a thoroughbred horse can have in its name,Fourteen +General,Proverbially it was a bold man who first ate what,An Oyster,General,Which Flemish painter produced Adoration of the Kings,Brueghel,General,How many stars are on the paramount film studio logo,22 +Food & Drink,Where is the oldest distillery in the world? ,"The Old Bushmills, Cty Antrim ",General,In what Dickens novel does Alfred Jingle appear,The Pickwick Papers,General,Where would you find Puck Miranda and Ariel,Circling Uranus +General,"What countries days include Fire day, Water day and wood day",Japan Tue Wed Thu,General,What is the Capital of: Cayman Islands,George town,General,Which instrument did George Harrison of the Beatles play?,Lead guitar +General,In golf what would you put in your shag bag,Practice Balls,General,When do diurnal animals sleep,Night time,General,What type of animal is a jennet,Small Spanish horse +General,"Constance Markiewicz, Became The Worlds First Ever What In 1918?",First British Female MP,General,What is a gondola?,Water taxi,General,"What is the nickname for Paterson, New Jersey",Silk city +Sports & Leisure,Which Modern Gymnastics Event Made Its First Appearance At The 2000 Sydney Olympics ,Trampolining ,Music,Whose real name is Annie Mae Bullock,Tina Turner,Geography,"The city of _________ used to be known as Puerto Rico (which means ""rich port"" in Spanish), while the island of Puerto Rico was originally named San Juan.",San juan +General,Burning potassium has what colour flame,Purple,General,Oneirophobia is the fear of,Dreams,General,In 1982 which group declared they were 'hungry like the wolf',Duran duran +General,"Who directed the monochrome (sepia) sequences at the beginning and end of ""the wizard of oz"" (1939)",King vidor,General,"The Lost Island Of ""Naboombu"" Features In Which Disney Movie",Bedknobs & Broomsticks,General,Dactylography is the study and practice of what,Finger printing + History & Holidays,Who Became premier of Cuba in 1959. ,Fidel Castro ,General,Who won the woman's 400 m hurdles in the 1992 olympics,Sally gunnell,General,Who sang 'any way you want me',Elvis presley + History & Holidays,According to the song what did my true love give to me on the fifth day of Christmas ,Five Gold Rings ,General,What fruits are sorted for ripeness by bouncing them,Cranberries,Technology & Video Games,"Nolan Bushnell, creator of the first arcade game, also founded the video arcade chain now known as what? ",Chuck E. Cheese's +General,What is the cap on the fire hydrant called,Bonnet,General,What does a myrmecologist study,Ants,Music,In Which Lake Did Otis Redding's Plane Crash In 1967,Lake Monoma + Geography,What is the capital of Philippines ?,Manila,General,Kuala lumpur is the capital of ______,Malaysia,General,What was named after amerigo vespucci,America +Science & Nature,Who Originated The Uncertainty Principle ,Heisenberg ,General,Allium cepa - one of the lilicaea - world most used food item,Onion,General,Who was beaten at the battle of Actium by Octavian,Mark antony +General,What dodgers and cubs first-baseman dropped baseball to take up actin,Chuck,General,In what movie did Sinatra sing My Kind of Town,Robin and the 7 Hoods,Music,"What Group Consisted Of Marki King, Boon Gould, Mike Lindup, Phil Gould",Level 42 +General,Which usical instrument was invented by Henry Schluter?,Vibraphone,General,In 1984 Challenger astronauts complete 1st in space satellite,Repair, History & Holidays,Who Provided The Voice Of Charlie In The TV Show 'Charlies Angels'' ,John Forsythe  +General,How long is a silver anniversary,25 years,General,"There were three Kings of England in 1066. Harold and William, of course, and who else",Edward the confessor,Music,What Was The Official Single Of Euro 96,We're In This Together (Simply Red) +Music,Johnny Marr Teamed Up With Which Former New Order Member To Form The Band Electronic,Bernard Sumner,General,What is a group of trout,Hover,General,What is athens' old quarter,Plaka +General,Test of metal or ore for ingredients and quality,Assay,General,What is the Capital of: Romania,Bucharest,General,Which musical features the song 'Happy Talk',"South pacific," +General,What is a woolly bear,Caterpillar,General,The word rodent comes from the Latin rodere meaning what,To Gnaw,General,Whats traditionally the first event in a rodeo,Bareback riding +General,What is the name of the telescope that was placed in orbit in the eighties?,Hubble,General,"On 'the lucy show', who played vivian bagley",Vivian vance,Science & Nature,Which Large Feline Of Asia Is Sometimes Called An Ounce ,Snow Leopard  +General,"Where was The Beast, the first mobile robot, developed",Johns hopkins,General,What country had Europe's fastest economic growth rate in 1988,Spain,General,You're eating maggots Michael.,Lost Boys +General,The province of Alberta in Canada has been completely free of rats since what year,1905,General,George Hoy-Booth became famous as who,George Formby,General,What Was Founded In The UK In 1694 Because Of Englands War With France,The Bank Of England +General,Arlanda is the airport which serves which European city,Stockholm,General,"From which Marx Brothers film comes the line 'Either he's dead, or my watch has stopped",A day at the races,Religion & Mythology,Who is the Norse god of poetry ?,Bragi +Music,What Group Did Nick Cave Sing With Before Forming The Bad Seeds,The Birthday Party,General,Caterpillar comes from the old French what's it literally mean,Hairy Cat,Science & Nature,How long does it take for Earth to travel around the Sun?,1 Year +Science & Nature,Sound Navigation Ranging is better known as _________.,Sonar,General,What was the first film directed by robert redford,Ordinary people,General,Who made his name with Jimmy James and the Blue Flames,Jimmy Hendrix +General,What is a group of geese called,Gaggle,General,What song gave a 9 year old US a UK No 1 lots weeks 1972/73,Long haired lover from Liverpool,General,Who was the first choice to play Beverley Hills Cop,Sylvester Stallone +Music,"Which Music Festival Was Attended By Over 400,000 People In 1969",Woodstock,General,Allium Sativum is better known as what,Garlic,General,Of who was atahualpa king,Incas +General,The last Nascar driver to serve jail time for running moonshine was who,Buddy arrington,General,Who was kidnapped in Robert Louis Stevenson's Kidnapped,David Balfour,General,Who did pat sajak play on the soapie 'days of our lives',Kevin hathaway +General,"Which Capital City Is Home To ""Carrasco"" International Airport",Montevideo (Uruguay),General,California Valley and a Chinese cabbage what word means both,Napa,Music,Saul Hudson Is The Real Name Of Which Guitarist,Slash +General,The worlds what museum is at 19 Green belt North York Ontario,Contraceptive,General,What is 'applejack',Fermented cider,Music,How many clubs a day did the girl who came in through the bathroom window work at?,Fifteen +General,What is hippopotomonstrosesquippedaliophobia the fear of?,Long words,General,What short term is given to the leader of the Red Arrows aerobatic display team?,Red 1,Food & Drink,If you were eating aloo in an Indian restaurant what would you be eating? ,Potato  +Toys & Games,"In the game of chess, which piece has the most freedom to move?",Queen,Geography,What are drumlins and eskers formed by,Glacier, History & Holidays,Which Of Courtney Cox's Co Stars From The Scream Films Is Now Her Husband ,David Arquette  +Science & Nature,What Is The Function Of The Thyroid Gland ,To Regulate Growth & The Metabolism ,General,Alain Boublil and Claude-Michel Schonberg wrote which hit musical,Les miserables,General,Tethys is a moon of which planet in the solar system?,Saturn +Music,"Who Claimed That ""Mama Gave Birth To The Soul Children""",Queen Latifah,Art & Literature,How Many Holes Are There On A Traditional Artists Pallette? ,One ,General,What type of adornment is a Postiche,Small Hairpiece +Food & Drink,Singapore banned which popular western foodstuff because of litter implicatons? ,Chewing Guim ,General,TAP is the national airline of which country,Portugal,Mathematics,An angle greater than 90 degrees is said to be _________.,Obtuse +General,Metal framework on a vehicle to protect it in the event of a collision with a large animal,Bull bar,General,How many 'Air Force One'(s) are there?,Two,General,In Delaware by law a newlywed must do what if wife asks,Take her Shopping +General,What is a Bellwether,Leader of flock of sheep,General,Non dairy creamer is _________,Flammable,General,Which playwright wrote Lady Windermere's Fan,Oscar wilde +General,As what is 'South West Township' known in south africa,Soweto,Art & Literature,Who wrote the Belgariad ?,Leigh and David Eddings, History & Holidays,Name The Fabulous Winged Horse Of Greek Mythology? ,Pegasus  +General,What is the most important mineral for strong bones & teeth,Calcium,General,Who did the original thugs worship,The Goddess Kali,Entertainment,What was the first cartoon to feature sound,Steamboat willy + History & Holidays,What asian leader was known as the little brown saint ,Mahatma gandhi ,Music,Which Artists Recorded The Album The Original Soundtrack,10cc, History & Holidays,Which Baltic seaport was the German rocket centre during WWII?,Peenemunde +General,Of who did the u.s postal service print 500 million stamps in 1993,Elvis,General,What word may be used to refer to a group of peacocks,Muster,General,Which 1956 film about the pocket battleship Graf Spec starred John Gregson,Battle of the river plate +General,What is 'bountiful mother' in latin,Alma mater, History & Holidays,What does ALF stand for? ,Alien Life Form ,General,What red fruit is most often eaten in green salads,Tomato +General,What was the name of john lennon and cynthia lennon's son,Sean, History & Holidays,After Much Legal Wrangling Who Became The Controller Of 'The News Of The World'' In 1969 ,Rupert Murdoch ,General,"Who Was Ian Flemming's Top Choice To Play James Bond Before ""Sean Connery"" Eventually Got The Part",Roger Moore +General,Lake Vanern is the largest lake in which country,Sweden,General,In which film did Lee Marvin throw boiling coffee in Gloria Graham's face,The big heat,General,Who sold the most albums on a single day,Elvis 20 million day after death +General,"What is the missing word in english with the letter combination 'uu' vacuum, muumuu, continuum, residuum, duumvir",Duumvirate,General,How long is an elephant pregnant,2 years,Geography,This imaginary line approximately follows the 180 degree meridian through the Pacific Ocean.,International date line + History & Holidays,Who was the ruler of the USSR from 1917-22? ,Vladimir Lenin ,General,What is measured with an ombrometer,Rainfall,General,What does a Puissance event test in showjumping,High Jump wall +General,Isolophobia is the fear of,"Solitude, being alone", History & Holidays,The song White Christmas was first performed in which 1942 film? ,Holiday Inn ,Art & Literature,"Who wrote ""Ender's Game"" ?",Orson Scott Card +Science & Nature,What Did Jesse Reno Invent Using Traditional Conveyor Belt Principles To Pull Folding Steps Up A Slope ,The Escalator ,General,Who wrote the play 'Rosencrantz and Guildenstein' are dead,Tom stoppard,General,What are you doing if you use the egg position,Skiing +General,"Name the wife of King Ahab, who Jehu ordered to be thrown to her death from a window",Jezebel,General,Where did Mathias Rust land his Cessna in 1987,Red Square Moscow,Science & Nature,Who Designed The Mini And The Morris Minor ,Sir Alexander Issigonis  +General,If not Shakespeare what links Juliet Cleopatra Mark Antony,Commit Suicide,General,Baseball: the san diego _______,Padres,General,A Limousine was originally what (From Limousine in France),French shepherds protective cloak +General,Who was the first person to prove that planets move in elliptical orbits,Kepler,Sports & Leisure,Basketball: The Los Angeles _________.,Lakers,Art & Literature,"What did Jeannie C. Riley describe as ""a little Peyton Place""",Harper valley +General,Who was the first black mayor of Chicago?,Harold Washington,General,How many litres of water vapour can be lost in a large tree in an hour by transpiration,Three hundred, Geography,What is the capital of Iraq ?,Baghdad +Sports & Leisure,Who Won The US Open Men's *Tennis * Title in 1990 At The Age Of Just 19? ,Pete Sampras ,General,The study of the manner in which organisms carry on their life processes is ________,Physiology,Sports & Leisure,Who did Lennox Lewis beat to win his first world heavyweight title? ,"Nobody, Riddick Bowe relinquished his title " +General,Which architect designed London's tallest building The Shard?,Renzo Piano,General,What does 360 degrees make,Circle,General,Pteromerhanophobia is the fear of,Flying +General,"Which meteorological phenomena means ""a curl of hair"" in Latin",Cirrus - clouds,General,Name ship sunk by the submarine Conqueror Falklands War,General Belgrano,General,Whats the secret identity of Don Vito Corleone,The godfather +General,Originally a Toss Pot did a lot of what,Drinking - Alcohol,General,Traditionally there are 100 pleats in what item,A Chefs hat,General,Equinophobia is a fear of ______,Horses +General,What country is Men Without Hats originally from?,Canada,Entertainment,From what platform does the 'Chattanooga Choo Choo' leave Pennsylvania station?,29,General,What is the capitol of Fiji,Suva +Music,What group was Ringo with before the Beatles?,Rory Storm and the Hurricanes,Music,Who introduced Paul McCartney to John Lennon?,Ivan Vaughn,General,What canadian island has a capital named charlottetown,Prince edward island +General,"What Did Bruce Willis, Harvey Keitel & Winston Churchill All have to overcome?",Stutters / Stammers,General,A fisherman in the Arral sea had his boat destroyed by what,A Cow - USA air force dumped it,Science & Nature,What is the name for 0.1 Newtons ?,Dyne +General,What 3 ingredients make a sidecar cocktail,Brandy Cointreau Lemon juice,General,Pak man was called Paka in Japan what does paka mean,Eating,Geography,Which Scottish Loch Is The Deepest ,Loch Ness  +General,Anna Maria Louisa Italiano changed her name to what,Anne Bancroft,General,Eunectes Murinus largest of its kind in South America - what,Annoconda,General,What canadian professional snooker player is nicknamed the grinder,Cliff +General,An Aficionado originally followed what sport,Bullfighting,Science & Nature,What is the atomic number of sulphur?,16,Music,Which 60's Star Was Born In Johannesburg And Christened Michael Lubowitz,Manfred Mann +People & Places,Which is the largest of the Canary Islands? ,Tenerife ,General,What species of fish is caught most,Anchovetta - Anchovy,Science & Nature,What Treatment Involves The Precise Use Of Needles ,Acupuncture  + History & Holidays,The land that would eventually be known as oklahoma was part of the what in 1803 ,Louisiana purchase. ,General,What were volitos first demonstrated in Soho London in 1823,Roller Skates,General,The traditional Sweedish drink,Aquavit +General,Which U.S. boxer was known as 'The Manessa Mauler',Jack dempsey,General,In Hitchcock's film The Trouble with Harry - what was the trouble,He was dead,Music,Who Wrote The Messiah,Handel +Music,"Who Had Minor Hits With ""Dogs Of Lust"", ""Slow Emotion Replay"" And ""Dis -Infected"" EP",The The,General,"Music What is the narrow, inland sea, separating the Arabian peninsula, western Asia, from northeastern Africa",Red sea,Sports & Leisure,What Is An Oxer ,A Type Of Jump In Show Jumping +General,What does a Stupprator prefer sexually,Virgins,General,"In Gulliver's Travels, from what did the scientists seek to get sunshine",Cucumbers,General,Kenophobia is a fear of ______,Empty spaces +General,An elephant has 400000 what in its trunk,Muscles,General,William Blake Winston Churchill John Lennon what links,Ordained Druids, History & Holidays,"Then name Santa Claus is derived from which language, is it Dutch, French or Italian ",Dutch  +General,The Zagros mountain range is in which country,Iran,Science & Nature, An extinct species of __________ had a head the size of a Shetland pony's and reached a height of more than ten feet.,Kangaroo,General,Which Very Famous Book Which I'm Sure Many Of You Have Read Was Written & Illustrated By “ Eric Carle ” In 1969 ?,The Hungry Hungry Caterpillar + Geography,What is the highest mountain in the Appalachian range?,Mt. Mitchell,Sports & Leisure,Alex Hurricane Higgins & Jimmy The Whirlwind White But Whats Ronnie O Sullivans Nickname ,Ronnie The Rocket O Sullivan , History & Holidays,Which ship did Charles Darwin captain?,HMS Beagle +General,In South Korea traffic police must report what,Tourists bribes taken,General,Needlework of hooked yarn producing lacy patterned fabric,Crochet,Music,"Who Asked ""What Have You Done For Me Lately"" In 1986",Janet Jackson +General,What does SALT stand for,Strategic arms limitation treaty,Science & Nature,What is a male goose called,Gander,General,Who wrote Candide,Voltaire +General,Which chemical element has the atomic number 4?,Berylium,People & Places,Who Spent A Year In Prison For Sleeping With An Underage Girl ,Graham Rix ,General,Fill in the blank: the ___ Piper,Pied +General,Who had a hit no 5 in US 1971 with Bridge over Troubled Water,Aretha Franklin,General,What is a Fata Morgana,Type of Mirage,Sports & Leisure,Which football team has played at Wembley more times than anyone else? ,Leyton Orient  + History & Holidays,"William the First was crowned on Christmas day, but in what year ",1066 , History & Holidays,Clement C. Moore wrote a very famous Christmas poem What was it called? ,Twas The Night Before Christmas ,Science & Nature,Which Stock Control System Was Introduced In American Supermarkets in 1974 ,Bar Codes  +General,What country covers an entire continent,Australia,General,Who is on a u.s. bill,Benjamin franklin,General,What is the Capital of: Guam,Hagatna agana +Music,In which film did Donna Summer make her debut as an aspiring disco singer?,Thank God It's Friday,General,Which was the first 'james bond' novel,Casino royale,Music,In Which Year Did Sting Release Fields Of Gold,1993 +General,Who was responsible for driving the english out of scotland in 1297,William,General,What is studied in the science of somatology,The Body,General,"Harrison What do the San Joaquin kit fox, Hawaiian hawk and Ocelot have in common",Endangered species +Music,Which duo released albums called Introspective and Very?,Pet Shop Boys,General,What is the last book of the Bible,Revelations,General,What is a group of this animal called: Monkey,Troop +General,What is the front of a saddle called?,Pommel, History & Holidays,In Which Scary Movie Will You Find The Character Martin Brody ,Jaws ,General,What name is given to the group of animals that eat meat,Carnivores +General,Fried Chicken Strawberry Shortcake trad Xmas eve meal where,Japan,General,"Which European country has regions named Limburg, Drente and Brabant",Netherlands,Music,According The Who keeps a ten bob note up his nose?,Mean Mr. Mustard +General,U.S. Captials - Michigan,Lansing,General,What does an animal have if it is a fish,Gills, History & Holidays,John Callcott Horsley designed what first commercial Christmas item in 1843? ,Christmas card  +General,Which epic film was based on a novel by Lew Wallace,Ben hur,Food & Drink,What Eastern Fruit Comes In A Shell ,The Lychee ,Entertainment,Pancho was whose faithful sidekick?,Cisco Kid's +General,What are the young of a rabbit called,Kittens,General,In which film based on a James Jones novel does Deborah Kerr embrace on the beach with Burt Lancaster,From here to eternity,General,"Which company slogan was ""We're No 2 We try harder""",Avis rent a car +Music,"Paul wrote ""Martha My Dear"". Who did he know who was named Martha?",His Dog,General,Officers how many british officers were forced by indian troops into the black hole of calcutta,146,General,Who rejected the 1964 Nobel prize for literature,Jean Paul Sarte +Music,Who filled in for Ringo on the Beatles' 1964 world tour?,Jimmy Nicol,General,STDs are the most costly health problem in the USA what's 2nd,Dog Bites,General,What is the capital of texas,Austin +General,What planet did Alf come from?,Melmac,Music,Which Country Are Bachman Turner Overdrive From,Canada,General,Which country is the largest in Africa,Sudan +General,Who wrote dionne warwick's 'walk on by' and 'say a little prayer',Burt,General,What is the tenth letter of the Greek alphabet,Kappa,General,Who wrote the book The Puppet Masters later a film,Robert Heinlein +General,What is the technical difference between rowing and sculling,Rowing means using a single oar,General,What links Hong Kong Singapore Rio and Utopia,Road Films,General,What is a group of travelling wolves,Route +General,"Outside poland, where is the largest polish population",Chicago,Geography,In which continent would you find the amur river ,Asia ,General,Where would you find or what are guntlines,Rope - groves +General,In The Film Saving Private Ryan What Was Private Ryan's First Name?,James,Music,Length of the longest Beatles track,"Revolution 9,'' from the White Album",Art & Literature,Who wrote the threepenny opera?,Bertolt Brecht +General,"What song won the grammy for ""song of the year"" in 1977",You light up my,General,What part of a horse would you look at to determine its age,Teeth,General,What is the Capital of: Denmark,Copenhagen +Music,Which Band From Manchester Incluides The Former Entertainments Manager From The Hacienda (Nightclub),M People,General,Which Well Known Gangster Lives At “ 36 Cherry Blossom Close ”,Ali G,General,What organization was given the only Nobel Peace Price awarded during World War I,International Red Cross +General,How did George II die,Fell off toilet,General,What is the fear of home surroundings or house known as,Oikophobia,General,What is the flower that stands for: reverie,Flowering gern +General,"Electric, Perse and smalt shades of which colour",Blue,General,Sorrowful song or poem,Elegy,Art & Literature,"Who wrote the novels 'About A Boy'', 'How To Be Good'' and 'High Fidelity''? ",Nick Hornby  +General,Every day in the US people steal $20000 from where,Vending machines,People & Places,Who wrote the book 'The Guns Of Navarone'' ,Alistair McClaine ,Geography,In what country is the jutland peninsula ,Denmark  +General,"Who wrote ""The Picture of Dorian Grey""",Oscar wilde,General,"What country controls access to the North Sea from the rivers Schelde, Meuse and Rhine",Netherlands,General,Who is always the victim the game of Cluedo,Dr. black +General,Indiana jones: who was known to get lost in his own museum,Marcus brody,General,"What was the name of the first ""portable"" computer?",Osbourne,Music,Who Wrote Pink Cadillac For Natalie Cole,Bruce Springsteen +General,"In Which Childrens TV Show Will You Find The Characters Of ""Florrie & Baby Pom""",The Fimbles,Food & Drink,What is the most widely eaten meat in the world? ,Pork ,General,What is the SI unit of pressure or stress,Pascal +General,Mr Chips said goodbye from Brookfield school - What subject,Latin,Science & Nature,The molten material from a volcano is ________?,Lava,General,"Who released 'time, love and tenderness' in 1981",Michael bolton +General,Where is the worlds largest mine,Carletonville South Africa,General,What WasMichael Barymores Occupation Before He Found Fame As A TV Star,Hair Dresser,General,The Walibri tribe central Australia greet each other how,Shaking Penises +General,Florence Nightingale tended the soldiers in which war,Crimean,General,Phineas Barnum opened his circus in what year,1871,General,In Hill Street Blues which character used to bite people,Animal - Mick +General,What is the name of the cord joining a mother & her unborn child,Umbilical cord,General,What is the next-to-next-to-last event,Antepenultimate,General,Who in US was given Hitler's Supreme Order of German Eagle,Henry Ford +General,What Is Joseph Cyril Bamfords Claim To Fame?,The JCB Earth Mover,General,"Which railway tunnel, 20 kilometres long, joining Isselle in Italy with Brig in Switzerland, is cut under Mount Leone in the Alps",Simplon tunnel,Music,Who Sang Gonna Fly Now (Theme From Rocky) In 1977,Bill Conti + Language,"The name for this semi-precious stone comes from the Latin for ""sea water""",Aquamarine,General,What is the flower that stands for: consolation,Red poppy,General,Where Was Britains First Escalator Installed,Harrods +Music,What Name Appears In The Title Of Both A Beatles Hit And A Novel By ThomasHardy?,Jude,Sports & Leisure,Can you name 2 British golfers to win the US Masters in the 1990's? ,Nick Faldo & Ian Woosnam ,General,Why did the USA govt open Lincolns coffin in 1887 and 1901,Check body still there +General,"An atom is comprized of these 3 subatomic particles: electron, neutron, & _____",Proton, History & Holidays,Who succeeded Churchill when he resigned in 1955,Sir anthony eden,Sports & Leisure,Which Two Sports Comprise A Biathlon? (PFE) ,Cross Country Skiing & Rifle Shooting  +Music,Which Group Comprises Of Colin Angus And Mr C,The Shamen,General,Henri Charrier is better known by what nickname,Papillion – The Butterfly,General,What is the most northerly town in Europe,Hammerfest +General,What are the colours of the Olympics rings (in order).?,Blue Yellow Black Green Red,General,For whom was buckingham palace built,John sheffield,General,Idaho Falls Idaho its illegal for over 88 year olds to do what,Ride a motorcycle +General,Who Was The First Woman To Appear Naked On The Front Cover Of The Radio Times,Helen Mirren,Food & Drink,"Booze Name: 1 oz. gin, 1/2 oz. dry vermouth, 1/2 oz. sweet vermouth, 1/2 oz. orange juice",Bronx cocktail,General,There are more the 38000 types of what,Mushrooms +General,What was known as the spice island,Zanzibar,General,Leslie Sebastian Charles is better known as who,Billy Ocean,Music,Silver Convention was a studio-created group. In which country was the studio situated?,Germany +General,In which county is Charnwood Forest,Leicestershire,General,What is the fear of the night known as,Noctiphobia,General,The telephone county code 39 would connect you with _____,Italy +General,What is the name of the Tokyo Stock Market Index,Nikkei,General,"In the monty python parody 'search for the holy grail', what was arthur's trusty servant's name",Patsy,General,"In Greek mythology, who was the first woman on Earth, created by Hephaestus at the request of Zeus",Pandora +General,"In The Pilgrim's Progress, which city of Christian's final destination",Celestial city,General,"The original name for this Pontiac car was the Banshee, for its mythicality.",Firebird,General,The star Aldebran is part of which constellation?,Taurus +Science & Nature,What Do You Get If A Male Donkey Breeds With A Horse? ,A Mule ,General,What would an Irishman do with his shone,Cut Peat,Geography,What is the capital of Bosnia and Hercegovina,Sarajevo +Sports & Leisure,How Many Suits Are There In A Game Of (Mah Jong) ,3 Suits ,General,In Which Country Is The Chernobyl Nuclear Power Plant That Had A Melt Down Causing Widespread Radioactive Fallout,"Ukraine, 1986",Music,Name The Kajagoogoo's Debut Single,Too Shy +Food & Drink,Who released the following album 'Strange brew ' ,Cream ,Art & Literature,Which Of Jane Austens Novels Was Publsihed Posthumously ,Persuasion ,General,In what book did we meet the Eoli and the Morlocks,The Time Machine – HG Wells +General,What is the derivative of sin x,Cos x,Food & Drink,What is the name for the liquid butter used in Indian cooking? ,Ghee , Language,Which is the only english word that contains all the vowels in alphabetical order?,Facetious +General,What name is given to the fraction part of a logarithm number?,Mantissa,General,What U.S. state includes the telephone area code 701,North dakota,General,Who played the role of Miss Hannigan in the film Annie,Carol Burnett +General,Ralph Freeman designed which Australian landmark,Sydney harbour bridge,General,"Which singer/songwriter won a Grammy in 1988 with 'Don't Worry, Be Happy'",Bobby mcferrin,General,What color was Coca Cola originally,Green +Science & Nature,"In Wonderland, Who Took A Watch Out Of His Waistcoat Pocket Only To Discover That He Was Late ",The White Rabbit ,General,Who is the only American president elected unopposed,George Washington,General, Who or what could win a Golden Clio award ,Advertising Film +General,What animals teeth were used as knife blades by the Indians,Beaver,General,Al Alcorn wrote which famous computer program / game,Pong,General,Who was the Celebrity Captain of the Chicago Blackhawks in 1991-92,Jim + History & Holidays,In The Film 'Halloween' For How Many Years Was The Killer Locked Up In An Asylum Before He Escaped ,15 Years ,General,In what country is the Eucumbene Dam,Australia New S Wales, Geography,What is the basic unit of currency for Haiti ?,Gourde +General,Norman Bean became famous as which author,Edgar Rice Burroughs,General,What is the Roman numeral mcdxlii in decimal,1442,Geography,What is the capital of Afghanistan,Kabul +General,In which tv series does fred savage play kevin arnold,The wonder years,Food & Drink,"Which drink made of hot milk curdled with ale, wine, etc. and often flavoured with spices, was formerly used as a cold remedy? ",Posset ,General,What is a group of grouse,Pack +Science & Nature,The valuable blue form of corundum is called:,Sapphire,General,Any type of machine that obtains mechanical energy directly from the expenditure of the chemical energy of fuel burned in a combustion chamber that is an integral part of the engine,Internal combustion,Food & Drink,Who released the following 'edible' album 'Chocolate starfish and the hot dog flavoured water' ,Limp Bizkit  +Science & Nature," It takes approximately 69,000 venom extractions from the __________",To fill a 1_pint container. coral snake,General,"Who On TV Drove A Car With The Licence Plate ""CNH 320""",The Dukes Of Hazzard,General,A long cylindrical pillow,Bolster +General,What are people encouraged to kiss under,Mistletoe,General,Haifa is a major seaport in which country,Israel,General,What is the British equivalent of the game known in America as 'Tick-Tack-Toe',Noughts and crosses +General,In Islington in London it’s a £50 fine for sleeping where,The Public Library,General,How many elevators stopped at the bridge on Star Trek,One,General,Who was the cisco kid's faithful sidekick,Pancho +General,If a dish is served pomontier what does it contain,Potatoes,General,"In the film 'star wars', what species is chewbacca",Wookie,Science & Nature,Which moon is the largest satellite in our solar system?,Ganymede +Science & Nature,What Describes A Person With No Skin Pigment ,An Albino ,Science & Nature," Fish travel in schools, whales travel in pods or __________",Gams,General,In America what colour is the Green Card,White +Music,"Which Of These People Is The Shortest ""Jarvis Cocker, Micheal Stipe Or Nicky Wire""",Michael Stipe,Sports & Leisure,With which sport is Chris Evert Lloyd identified?,Tennis,General,Who died three days before groucho marx,Elvis presley +General,Creedence clearwater revival sings 'have you ever ______',Seen the rain,General,What is the name given to a pregnant goldfish?,Twit,General,"What ballerina is known by her peers as ""the body""",Cynthia gregory +General,"In the tv series 'leave it to beaver', what was wally's best friend's name",Eddie haskell,Geography,Which 2 Countries Occupy The Iberian Peninsular ,Spain & Portugal ,General,"Which poem begins ""Oh to be in England now that April's here""",Home thoughts from abroad +Sports & Leisure,"In Boxing Who Suffered Defeats To Tim Witherspoon, Mike Tyson & Lennox Lewis ",Frank Bruno ,Religion & Mythology,"His wife was Penelope and his son, Telemachus. He was exiled from his home on Ithaca for angering the gods?",Odysseus,Geography,Which U.S. city is known as Beantown,Boston + History & Holidays,"In 'Bridget Jones Diary'', Bridget Jones makes a New Years Resolution, to sort out her life, meet a man and keep a diary, but who played Bridget Jones ",Renée Zellweger ,General,Where did the Ghost of Christmas Present take Scrooge first,His clerk's,General,Black and white seabird with small wings,Auk +General,What fashion designers symbol is a swan,Gloria Vanderbilt,Science & Nature,What fish is the fastest?,Sailfish,General,What is the real name of the murderer known either as 'The .44 Calibre Killer' or 'The Son of Sam',David berkowitz +General,In which English town or city would you find The Christmas Steps,Bristol,Music,Which Pioneer Black Singer/Songwriter Dedicated An Oscar To Nelson Mandella,Stevie Wonder,General,What is latke,A potato pancake +Geography,On which river is Rome located,Tiber,General,What is the process of splitting atoms called,Fission,General,Surgical pincers,Forceps +General,96% of American children can recognise - who,Ronald MacDonald,General,What sort of wood was Noah's Ark made from,Gopher wood,General,Which Country Won The Eurovision Song Contest In 2008,Poland +Science & Nature, The female American Oyster lays an average of 500 million eggs per year. Usually only one oyster out of the bunch reaches __________,Maturity,General,What area gets its name from the Greek word for bear,Arctic because great bear over it,General,The via appia went from brindisi to ______,Rome +General,What are the grinding teeth called,Molars,General,Which UK artist painted The Rakes Progress in the 1700s,William Hogarth,Science & Nature,The Lockheed F-117 Nighthawk Was The First Of Which Type Of Aircraft ,Stealth  +Science & Nature,A one-humped camel is called a _________.,Dromedary,General,Who wrote the novel Invisible Man in 1952,Ralph Waldo Emerson,General,Who composed the music for the musical Showboat,Jerome kern +General,In 2001 one active UK warship named after an Englishman who,Sir Winston Churchill,General,Harmatophobia is the fear of what,Sexual Incompetence,Toys & Games,"This is the length, in feet, of a regulation Snooker table",12 +General,What country has the most bookshops per head population,New Zealand,General,What Was The Bon Jovi's First UK Top Ten Hit,You Give Love A Bad Name,General,Damascus is the capital of ______,Syria +Geography,Which U.S. state has the least rainfall,Nevada,General,From what coast do dalmatian originate,Dalmatian coast,General,Who was the Greek equivalent of the Roman god Cupid,Eros +Music,"Who Sang The Smokin Dance Hit ""Feel It"" Including The Featured Artist",Tamperer Feat Maya (Pepper),General,"In transport terms, what is an ACV",Air cushioned vehicle hovercraft, Geography,What is the basic unit of currency for Romania ?,Leu +General, Legal Terms: A supplement to a will.,Codicil,General,One in four Americans has done what,Appeared on TV,General,In which month do russians celebrate the october revolution,May +General,Edward Whymper was the first to do what,Climb Matterhorn,General,Which Greek hero finally tamed Pegasus,Bellerophon,Geography,Which Countries Collectively Are Known As Benelux ,"Belgium, Netherlands, Luuxemborg " +General,"A 1957 title recorded by the Crickets, was a line taken from the classic John Ford Western's 'The Searchers'. What was the title",That'll Be the Day,General,Who founded the Ballets Russe in 1909,Serge diaghilev,General,What name is given to the study of living things in their environment,Ecology +General,Opperman what is the capital of iowa,Des moines,General,This city was the headquarters of the British East India Company until 1708,Bombay,Music,What Was Simply Reds First Ever Top Ten Hit,Holding Back The Years +General,"Saanen, Hongtong & Toggenburg Are All Breeds Of Which Animal",Goats,General,Which ocean can yoU.S.wim in from the Seychelles,Indian,General,Of what continent is cyprus a part,Asia +General,Which 9-fingered pop pianist starred in the film Its all Happening,Russ,Religion & Mythology,"What has no reflection, no shadow, and can't stand the smell of garlic",Vampires,General,Which is the largest of the Society Islands,Tahiti +Sports & Leisure,A Silver Fern Is The Emblem Of Which Nations Rugby Union Team? ,New Zealand ,Geography,Over 75% of the Earth's surface is covered by some form of _____.,Water,General,Which great city replaced its hereditary kings with chief magistrates in 683 bc,Athens +General,What is the Latin word for liquid produced by the Ficus Elastica,Latex,General,Which garment traditionally contains eight sections of material,Kimono,Music,"Who Had ""Self Control"" In 1984",Laura Brannigan +General,What is the fear of parents-in-law known as,Soceraphobia,Geography,Which element makes up 2.6% of the Earth's crust,Magnesium,Science & Nature,Acetic Acid Is More Commonly Known As What? ,Vinegar  +General,Which is the only work by Dukas most people have ever heard of,The sorcerer's apprentice, Geography,"If its 4:00pm in Seattle, Washington what time is it in Portland, Oregon?",4:00pm,Science & Nature,A coral island consisting of a ring of rock enclosing a central lagoon is a(n) ________.,Atoll +Geography,What queen were the virgin islands named for in 1627 ,Elizabeth i ,General,What company introduced the first commercial minicomputer 65,DEC,General,Which of the four Horsemen of Apocalypse is known as Christ,War +General,What did henry shrapnel invent,Exploding shell,General,The French call it Gulf de Gascoine what's its English name,Bay of Biscay,Sports & Leisure,What Is Tthe Mark Behind Which Dart Player Must Stand Called ,The Oche  +General,What's the collective noun for a group of gulls,Colony,General,"Which Award WinningMovie Ended With The Line ""Hey Can I Try On Your Yellow Dress""",Tootise, Geography,Des Moines is the capital of ______?,Iowa +General,The American Society for Prevention of Cruelty to Animals (aspca) was formed in what year,1866,General,Who commanded Bill Jukes Cecco Noodler Skylights Starkey,Captain Hook,General,Where did Clark Kent usually change into his Superman Costume in the Daily Planet building,Store Room +General,In Pac Man eating what was worth 5000 points,Banana,General,What happened to Catherine Eddowes on 30th September 1888,Met Jack the Ripper,General,A large S.American vulture,Condor + History & Holidays,"""What Did My True Love Give To Me On The """"Nineth"""" Day Of Christmas"" ",9 Ladies Dancing ,General,In which television series do the characters Doctor Carter and Doctor Benton appear,E r,General,What is the most common metallic ore in the Earths crust,Aluminium +General,What is the Capital of: Virgin Islands,Charlotte amalie,Music,"Whose Middle Names Are ""Peter George St John De Baptiste De La Salle""",Brian Eno (No Wonder He Changed It),Geography,In which city is the Royal Mile? ,Edinburgh  +General,What does Do Loop do for a living,Computer programmer,Food & Drink,What Is An Egg Plant ,An Aubergine ,General,Who is Anne Catherick in the title of an 1859 novel?,The Woman in White +People & Places,What Did You Associate With Rod Hull ? ,Emu ,Sports & Leisure,In Which Sport Might You Come Across An Albertross ,Golf ,Science & Nature,What is another name for iron oxide ?,Rust +General,"Which famous Englishwoman was born in Florence on 12th May 1820,where her well-to-do parents were temporarily resident",Florence nightingale,General,"Which TV program ""returned control"" to you at its conclusion",Outer limits,Sports & Leisure,Basketball: The Seattle ________.,Supersonics +General,What is the opposite of nocturnal,Diurnal,General,"What race's runners refer to the noisy section along wellesley college as the ""screech tunnel""",The boston marathon, History & Holidays,For what country did Columbus make his historic voyage?,Spain +General,Which Saint is said to have carried his own head for six miles after his death?,St Denis,General,Who was the first British monarch to live in Buckingham Palace,Queen victoria,Science & Nature,On Which Modern Continent Did Tyrannosaurus Live ,North America  +General,Five out of six of the Village people have got what,Moustaches,Art & Literature,"Captain Hook, Tiger Lily, and Tinker Bell are characters in what story?",Peter Pan, History & Holidays,In which 1970's films does Dustin Hoffman play the character 'David Sumner' ? ,Straw Dogs  +General,In Watership Down rabbit language what is a hrududu,A motor car,General,What did pocahontas do to entertain the colonists,Cartwheels,General,Of what mineral is limestone composed of,Calcite +General,Who is Aladdin's father,Mustapha the tailor,General,Which French philosopher and political thinker wrote The Social Contract in 1762,Rousseau,General,What two sports use mallets,Croquet - Polo +General,Which brother sister won Wimbledon mixed doubles in 1980,John Tracy Austin,Food & Drink,What food has a name that means twice cooked? ,Biscuit ,People & Places,Which Movies Star Was Famous For The Phrase 'You Dirty Rat'' ,James Cagney  +General,"What can be a font, architectural style or novel type",Gothic,General,Who painted Les Parapluies in about 1883,Pierre auguste renoir,General,What is the most common Spanish surname,Garcia +Geography,In what country is the waterloo battlefield ,Belgium ,Technology & Video Games,Who is the first gym leader you fight in 'Pokémon' for the Game Boy? ,Brock,General,An 18th century law in Britain banned the poor from having what,Gin - they were too pissed +General,"Which Classic Tv Character Was Played By ""Sorrell Brooke"" Between (1979-1985)",Boss Hog,Music,Which Girl Band Had A Hit With The Song “Really Saying Something”?,Bananarama,General,What 80s show is Frasier a spinoff of?,Cheers! + History & Holidays,In Which Modern Day Country Would You Find The Region Known As Transylvania ,Romania ,General,Cassiterite is the ore what is extracted,Tin,General,Seawood's Folly is better known as what today,Alaska +Music,Who Were The Original Members Of Suede,"Brett Anderson, Bernard Butler, Mat Osman, Simon Gilbert, And Justine Frischmann",General,Where in Australia were British satellites launched in early 70s,Woomera,General,What is the art of tracing designs and making impressions of them,Lithography +General,Who Was Time Magazines Man Of The Year In 1938,Adolf Hitler,General,Stadler and Waldorf were critics who appeared in which TV show,The muppets,General,From where are walloons,Belgium +General,March 21st to April 20th is what Star sign,Aries, Geography,In which country is the Great Victoria Desert?,Australia, History & Holidays,"""What is the origin of The X In """"Xmas""""? "" ",X is the first letter of the word christ in latin  +Geography,Where is the city of Brotherly Love,Philadelphia,General,What is the offspring of a female horse and a male donkey,Mule,Art & Literature,"From which Shakespeare play is this line taken: ""To be or not to be?""",Hamlet +General,1300 to 1500 it was illegal for Englishmen to have 3 what a day,Meals,General,What is tripsophillia,Arousal from massage,General,What color is mary poppin's umbrella,Black + History & Holidays,"In 1959, Berry Gordy Jr. started a small record company called ",Motown ,General,Old lady from pasadena,The little old lady from pasadena a little old lady,General,Saint Andrew is the Patron Saint of Scotland and where else,Russia +General,What Film Won The Oscar For Best Visual Effects Back In 1984,Indiana Jones (Temple Of Doom),Music,"Which Musical Contains The Song, “Oh What A Beautiful Morning”",Oklahoma,General,"After recording 'The Nitty Gritty', who released the rhyming 'Name Game'",Shirley Ellis +General,Racing driver James Hunts nickname was Hunt the what,Shunt,General,Hippopotomonstrosesquippedaliophobia is a fear of ____,Long words,Sports & Leisure,Hockey: The Edmonton __________.,Oilers +General,Where is the kitty hawk,Smithsonian, Geography,"What is the capital of the overseas department and administrative region of France, Réunion?",Saint-Denis,General,And what is the first,The Whirlpool +People & Places,Which British comedian became well known for his broken microphone routine? ,Norman Collier , History & Holidays,Which houses fought the War of the Roses?,Lancaster and York,General,Device for viewing a revolving or oscillating object by making the object appear to be at rest,Stroboscope +General,Calvin and Hobbes artist.,Bill watterson,Sports & Leisure,Approximately how many dimples are in a golf ball?,Between 300 and 500,General,What is the third most popular meal ordered in sit down restaurants in the U.S.,Spaghetti +General,In the US what job has an average IQ of 104,Policeman,General,The study of inland water is known as,Hydrology,Toys & Games,How many folds does a Monopoly board have?,One +Entertainment,"Name the actor who played the leading role in ""The Good, the Bad, and the Ugly"".",Clint Eastwood,General,What country launched its first space rocket January 1961,Italy,Sports & Leisure,In which sport is the Cy Young Trophy awarded?,Baseball +Geography,The tallest building in the Southern Hemisphere is in which city,Melbourne,Music,"Who Had A Hit With ""Kokomo""",The Beach Boys,General,What Type Of Animal Is A Suffolk Punch,A Horse +General,"In ballet, an elongated line; in particular, the horizontal line of an arasbesque with one arm stretched front and the other back.",Allongé, Geography,What is the name of the large natural landmark in northern Australia also known as Uluru?,Ayers Rock,Food & Drink,What Exactly Is Saffron ,Crocus Stamens  +Geography,What is the capital of France,Paris,General,On which racecourse is the Kentucky Derby run,Churchill downs,General,What was the only film starring Mae West and WC Fields,My little chickadee +General,Who wrote the music to West Side Story,Leonard bernstein,General,What did jim henson create,Muppets,Food & Drink,Gin and Collins mix make which type of Cocktail ,Tom Collins  +General,What is the currency of Poland,Zloty,General,What interested a nomologist,Scientific Laws,Music,Who Released An Album Called “White Ladder”?,David Grey +Geography,What Was Sri Lanka Previously Known As ,Ceylon ,Entertainment,He was the voice of draco the dragon in the movie Dragonheart.,Sean Connery,Music,Which Band Were At The Number One Spot In 1998 With “Deeper Underground”?,Jamiroquai +General,Who won an Oscar for the soundtrack to Chariots of Fire,Vangelis,General,What is cerumen?,Earwax,General,"Who is ""the iron lady""",Margaret thatcher +General,"In Greek mythology, where were lotuses eaten",Island of jerba,General,In Tokyo there is a restaurant restricted to who,Dogs,Technology & Video Games,What fuzzy animal does Croc rescue? ,The Gobbos +General,Assen in the Netherlands is most famously associated with which sport?,Motorcycling,General,"Who recorded ""Join Together"" in 1972",The who,General,What is Top Kanal in Poland,Commercial TV station +Music,Which Musical Was Based On H G Wells Novel Kipp's,Half A Sixpence,General,Steptoe and Son' was the model for which amercian sitcom,All in the Family,General,What countries nation anthem is Land of Two Rivers,Iraq + History & Holidays,Which great American inventor died in October 1931? ,Thomas Edison ,General,What was the first movie Disney released through a subsidary company that carried an R rating?,Down and Out In Beverly Hills,Science & Nature,Water containing carbon dioxide under pressure is called ________.,Soda water +General,Who was reportedly the strongest man on earth,Samson,General,Australia was originally created to serve as a British ______ colony.,Penal,General,Who was Chairman of British Coal during the miners strike of the 1980s,Ian mcgregor +General,Which two fruits are an anagram of each other,Lemon and melon, History & Holidays,Who ruled England at the time of Shakespeare?,Elizabeth I,General,"What was the name of the novel by Elmore Leonard on which the film, ""Jackie Brown"" was based",Rum punch +General,In what country would you be rich in Leu,Rumanian currency,General,"Who said ""The great masses of the people will more easily fall victims to a big lie than to a small one.""?",Adolf Hitler,General,On what day of the year does grouse shooting end,December 10th +General,Where did Queen Victoria and Prince Albert get married?,St. James' Palace,General,The star constellation Ara has what English name,The Alter,General,Collective nouns - what's a group of photographers called,A Click +General,What was the first magazine to publish a hologram on it's cover,National Geographic,General,What everyday item was named after St.Pantaleon,Pants,General,The Reknas company - Calcutta worlds biggest exporter what,Human (skeletons) +General,"Popular social dance during the eighteenth century; done in rows or circles, it may have derived from English country dancing.",Contredanse,General,Which poet gave his name to a cape south of Brisbane,Byron,General,In which country is the port of Chittagong,Bangladesh +Toys & Games,Whose 3_dimensional cube became a 70s & 80s craze,Rubiks cube,General,What Is An Alligators Baby Called,A Hatchling,General,Baseball: the atlanta ______,Braves +Geography,In which continent would you find the mackenzie river ,North america ,General,What did Robert Ballard discover in 1985,The Titanic,General,What is the worlds largest food company,Nestle + History & Holidays,Which Olympics did the US boycott? ,1980 ,General,"1n the 1960s, what was the name of the Wolseley version of the Austin Mini",Hornet, Geography,Which element makes up 8.13% of the Earth's crust ?,Aluminium +General,I will please is the Latin translation of what medical treatment,Placebo,General,In computing who are floppy and mootilda,Gateways cows,General,What is the shape of the pasta 'tortlloni' based on,Venus's navel +General,What is 'mother's ruin'?,Gin,General,Soylent Green the band took name from film and book by who,Harry Harrison,General,Which religious order was founded by Saint Bruno,Carthusian +General,The U.S. has never lost a war in which _____ were used,Mules,Music,What Remains David Bowies Best Selling Original Album,Ziggy Stardust,Science & Nature," If frightened or threatened, a mother rabbit may abandon, ignore, or __________",Eat her young +General,Which actor was the first to play Robin Hood on television,Richard greene,General,Like what can a fully ripened cranberry be dribbled?,Basketball, History & Holidays,Who Was Henry VIII's Last Wife? ,Catherine Parr  +General,Name Disney's first film to win an Academy Award,Flowers and Trees,General,In what Hitchcock film did the heroine find shrunken head in bed,Under Capricorn,Sports & Leisure,Hockey: The Chicago _________.,Blackhawks +General,Issur Danielovitch became famous a who,Kirk Douglas,General,If you saw Fringilla Coelebs what bird have you noticed,The Chaffinch,General,Red Connors played by Edgar Buchanan which cowboys partner,Hopalong Cassidy +Entertainment,Who took dictation from Perry Mason?,Della Street,General,What is a group of leopards,Leap,General,"What is the full term for the abbreviation ""zoo"" ?",Zoological garden +Entertainment,Where are Rocket J. Squirel and Bullwinkle Moose from,Frostbite falls,General,Which countries name translates as land of the free,Thailand,General,John Lowe Oct 1984 got £102000 first to do what on TV,Nine dart 501 game +General,What living organism can be 30 times the size of a blue whale,A giant sequoia giant sequoia,General,"Although not named in the new testament, tradition names the two thieves crucified at the same time as Jesus as (alphabetically)",Dismas & gestas,General,From what is the liqueur kirsch made,Cherries +Science & Nature,Graphite dust is formed when what is cut with a laser?,Diamond,General,Who was known as The man in Black,Johnny Cash,General,Which moon was discovered on 22 June 1978,Charon +General,What is the flower that stands for: lasting beauty,Stock,Science & Nature,What is the modern name for Plumbum?,Lead,General,What was the Roman name for Odysseus,Ulysse +General,What is the Capital of: Seychelles,Victoria,General,"Who said ""All the world's art ain't worth a good potato pie""",L S Lowrie,Geography,"What Nationality Was Christian Schonbein, Who Discovered Ozone In 1840 ",German  +Entertainment,Charles Boyer inspired a cartoon skunk. Who?,Pepe le Pew,Technology & Video Games,What was the Earth's first artificial satellite ?,Sputnik 1, Geography,"If you flew due West from Portugal, what is the first continent you would reach?",North America +General,Who is Prime Minister of Australia,John howard,General,Lockiophobia is the fear of,Childbirth,General,This word describes the nazi annihilation of Jews,Holocaust +General,After the Bible what book did Americans rate as their favourite,Sears Roebuck Catalogue,General,What Was Madonna's First UK No.1 Single?,Into The Groove,General,Name the producer of Starsky & Hutch and Beverly Hills 91210,Aaron Sperling +General,A dime is equal to how many cents,Ten,General,In the US what was free before 1863,Mail service,Science & Nature,In Which US National Park Is The Old Faithful Geyser? ,Yellowstone National Park  +Music,Which Guitar Manufacturer Was Responsible For The Stratocaster,Fender,General,Which jethro tull lp cover showed a man leading two horses,Heavy horses,Music,"Who Suggested ""Lets Get Rocked""",Def Leppard +Music,Which Duo Wrote The Song “You'll Never Walk Alone”?,Rogers & Hammerstein,General,What is the eleventh month of the year,November,General,Novercaphobia is the fear of,Step mother +General,What is the capital of New Jersey,Trenton,General,What is the name of an Italian dessert made from egg yolks and Marsala wine,Zabaglione,Geography,In US city houses the headquarters of Coca Cola? ,Atlanta  +General,What is john robertson osbourne's stage name,Ozzy osbourne,Science & Nature,What is the main component of Brass and Bronze?,Copper,General,Where could you spend your Tala - Capital Apia,Samoa +General,"Who was the most notable wife of King Priam of Troy during the Trojan War, having no less than 19 children to him?",Hecuba,General,What was the occupation of most american presidents prior to politics,Lawyer,Music,"In The Beatles Track ""Penny Lane"", what did the Fireman keep in his pocket?",A portrait of the Queen +People & Places,Whose son got lost in the desert during the Paris Daker rally ? ,Mrs Thathchers Son Mark ,Art & Literature,This girl hid from the Nazis in Amsterdam.,Anne frank,General,What is Sean Connery's real first name,Thomas +General,Who plays 'Cat' in Red Dwarf,Danny john-jules,General,Which TV Show Used The Song Known As The Wizard Composed By Paul Hardcastle As It's Theme In The 1980's,Top Of The Pops,General,What links AP AUP CP Reuters,News Agencies +General,What was the leading cause of death in the late 19th century,Tuberculosis, History & Holidays,This is said to be history's greatest military evacuation.,Dunkirk,Food & Drink,What Is Ghee ,Clarified Butter Used In Asian Cooking  +Science & Nature, The most carnivorous of all bears is the __________. Its diet consists almost entirely of seals and fish.,Polar bear,Music,"Which Group Took The Title Of Their 1967 Debut Album ""Piper At The Gates Of Dawn"" From A Chapter In Kenneth Grahams ""The Wind In The Willows""",The Pink Floyd,General,Elwood Edwards voice is heard by AOL users - where,Welcome and You Got Mail +Music,Which Year Did Bryan Adams Refer To In 1985,1969 (Summer Of 69),General,What colour do you get when you mix blue and yellow together?,Green,General,Whose biography is entitled 'the dark side of genius',Alfred hitchcock +General,What is an algonquin,Moose,Music,What Method Of Singing Does The Singer Alternate Between Natural Voice & Falsetto,Yodelling,General,What was mohammed ali's original name,Cassius clay +General,A temple in Sri-Lanka is dedicated to what,Buddha's tooth,Science & Nature,What is the chief constituent of air?,Nitrogen,Music,Name The Band Formed By Ace Frehley After He Left Kiss,Freshleys Comet +Science & Nature,What Is The Study Of Low Temperatures Called ,Cryogenics ,Geography,"___________ was called the ""Gateway to the West"" in the 1800s because it served as a starting place for wagon train departures.",St. louis,General,"European architecture and music,ornate and extravagant",Baroque + History & Holidays,What English city was known to the Romans as Venta Bulgarum?,Winchester,General,What 1969 sex spoof had a different author for every chapter,Naked came the, History & Holidays,April 1889 Saw The Birth Of Which Future Dictator? ,Adolf Hitler  +Science & Nature,What is the symbol for iron in chemistry?,Fe,General,What was the first Carry On film,Carry on Sergeant,General,Who would use a chimere and a rochet,Bishop - parts of dress + Geography,What is the capital of North Carolina?,Raleigh,Science & Nature,Which meteor shower occurs on the 4th May ?,Eta Aquarids,Entertainment,"Which character in ""Forrest Gump"" loved shrimp?",Bubba +Entertainment,What job was Sting before he was a rock star?,Teacher,General,Tete-beche is a familiar term in which hobby,Stamp Collecting,General,Whose memoirs are entitled Long Walk to Freedom,Nelson mandela +General,What is compressed snow also called,Neve,General,Suzy was a star of a 60s TV show what character did she play,Flipper the dolphin,General,Celibate Egyptian priests were forbidden to eat what aphrodisiac,Onions +General,Adolph Hitler had a phobia - what,Claustrophobia,General,John Dick Ann Timmy who is missing from this famous five,Julian,General,"What do the Italian, French and Irish flag have in common",Vertical stripes +General,"In the Bible, who is the Book of Proverbs attributed to",Solomon,General,Which historical character is often referred to as 'The Mad Monk',Rasputin,Entertainment,Who was Dick Dastardly's pet?,Muttley +General,Who kept searching for his long lost salt shaker,Jimmy buffet, History & Holidays,"Who penned the words, 'Hubble, bubble toil and trouble, fire burn and cauldron bubble' ",William Shakespeare ,General,Who had a hit with First Cut is the Deepest in 1977,Rod Stuart +General,Which two teams automatically qualified for the france '98 soccer world cup,France and brazil,General,"Final and decisive action of the Napoleonic Wars, that effectively ended French domination of Europe",Battle of waterloo,General,Which art gallery is the home of Rodin's ' The Kiss' and Picasso's The Three Dancers',Tate modern +General,Where did Thomas Magnum grow up as a kid?,San Diego,Art & Literature,How Is Mrs William Heelis Better Known ,Beatrix Potter ,General,What does a gallophoic englishman fear,France +General, The feeling of having experienced something before is known as _______.,Deja vu,Sports & Leisure,Where were the 1948 Olympics held ?,"London, England",General,What country consumes the most meat per capita 124 lb,Argentina + History & Holidays,What was the leading cause of death in the late 19th century?,Tuberculosis,General,"The Name Of The Capital Of Which Country Means ""Good Air""",Buenos Aries,Geography,The most densely populated state in the United States is _______________,New jersey +General,Who was the first novelist to present a typed manuscript to his publisher,Mark twain,General,Large rich eloborate cake,Gateau,General,Kinshasa is the capital of ______,Zaire +Science & Nature," Despite man's fear and hatred of the wolf, it has not ever been proved that a non_rabid wolf ever attacked a __________",Human,General,From whom did J.F. Kennedy accept his dog Pushinska,Nikita kruschev,General,Credit card on which magnetically encoded information is stored to be read by an electronic device,Swipe card +Science & Nature,What does LPG stands for?,Liquid Petroleum Gas, History & Holidays,Which country traditionally provides Britain with a Christmas tree for Trafalgar Square in London? ,Norway ,General,"If the groundhog sees his shadow on Feb. 2, there will be how many more weeks of bad weather",Six +General,What is the French phrase that means already seen,Déjà vu,General,Wild Australian dog,Dingo,General,Which Is The Only Creature Where The Male Becomes Pregnant?,Sea Horse +General,What school does Harry Potter attend,Hogwarts,General,In Hindu mythology Agni is the god of what,Fire,General,What does sideshow bob of 'the simpsons' and jean valjean of 'les miserables' have in common,Criminal number +General,"Which company, during the 1984 Super Bowl, aired what is considered one of the best commercials in TV history?",Apple,Science & Nature,What Proportion Of People Are Left Handed ,About 5% ,Music,The Police Had Us Wrapped Around Their What,Finger +General,What is unusual about The lake of Monteith in Scotland,Only one the rest are Lochs,General,What does IRS stand for,Internal revenue service,Sports & Leisure,"Green Room, Crystal Cathedral & Walking The Dog are all terms from which sport? ",Surfing  +Food & Drink,Who In The World Of Music Has The Real Name Of Derek Dick ,Fish ,General,Colleen McCullough wrote which best selling book,The Thorn Birds,General,Which companies name translates as abundant fields,Toyota +Music,"""Radio Gaga"" Was A 1984 Hit For Whom",Queen,General,"Who was the first person to reach the South Pole, in 1911",Roald amundsen, History & Holidays,Which Well Known Actor Died On Christmas Day 1977 ,Charlie Chaplin  +General,Musical groups: england dan and _____,John ford coley,Art & Literature,Which Colour Followed Picasso's Blue Period ,Pink ,General,Which country's national flag has a tree depicted on it,Lebanon +General,"The world's smallest mammal is the _______ ___ of Thailand, weighing less than a penny",Bumblebee bat,Science & Nature,Which Lotus Car Came On The Market In 1963 And Featured In The Avengers ,The Elan ,General,Who might wear a wimple,A Nun +General,The treatment of disease by chemical substances which are toxic to the causative micro-organisms is called ____________.,Chemotherapy,General,What does the Easter Triduum refer to ,The last three days before Easter Sunday ,General,Which film is the first of the spaghetti westerns,A fistful of Dollars +Music,Name The Hit Making 3 Tenors,"Jose Carreras, Placido Domingo, Luciano Pavarotti",Music,Which Black American Band Featured On The British Band Aid Single,Kool And The Gang,General,"Which two British cities, along with ancient Rome, were built on seven hills?",Edinburgh and Sheffield +General,What animal is responsible for most deaths in the USA annually,Dogs,General,Who discovered the four largest moons of jupiter,Galileo,General,What U.S. president was the target of two assassination attempts in 17 days,Gerald ford +Geography,Where would you be if you landed smack in the middle of plock ,Poland ,General,What is the fear of the pope known as,Papaphobia,General,What year was Aunt Jemima pancake flour invented,1889 +General,What is the fear of heredity known as,Patroiophobia,General,What is the capital of the Northern Territorry of Australia?,Darwin,General,Who died three days after elvis presley,Groucho marx +General,License Plates: What job does SRREAL have,Artist,General,What is the study of sound,Acoustics,General,What is the young of this animal called: Rooster,Cockerel +Science & Nature,A Phon is a unit of what? ,Loudness ,General,Mageirocophobia is the fear of,Cooking,Science & Nature,Phobos and Deimos are the moons of which planet?,Mars +General,Who was the law for a shire,Reeve,General,Florence was severely flooded in what year,1966,General,In 1823 the British army soldiers were first issued with what,Trousers +General,What lies east of mauritius,Australia,General,What Is The Most Common Species Of Bird In The World,Red Billed Quela (Finch),General,What Shakespearean play features the line 'a plague on both your houses',Romeo & juliet +General,"Cushat, Rock and Stock all types of which creature",Doves, History & Holidays,Who Released The 70's Album Entitled Selling England by the Pound ,Genesis ,Entertainment,The key of A major has ___ sharps.,Three + Geography,What is the capital of Liberia ?,Monrovia,Food & Drink,"Boston butt, jowl, and picnic ham are parts of a ______.",Pig,General,Who recorded the song 'the humpty dance',Digital underground +General,In which war was the charge of the Light Brigade,Crimean,General,What colour tranquillisers work best,Yellow,General,How did John Belushi die?,Drug and Alcohol overdose +General,Sir Eyre Massey Shaw hold what Olympic record from 1900,Oldest gold yachting he was 70,General,What tough guy actor has a real first name of Walter,Bruce Willis,General,Queen Mary II died at age 32 what killed her,Smallpox + History & Holidays,Where In Egypt Is The Tomb Of Tutankhamen ,"Valley Of The Kings, Luxor ",General,"What's the international radio code word for the letter ""A""",Alpha,General,In which TV program did Sergeant Bosco appear,A Team +General,Your suffering from circadian dysrhythmia what have you got,Jet Lag,General,Which Bond films theme tune was sung by Duran Duran,A view to a kill,General,What type of celestial body is andromeda,Galaxy +General,The word for soda in Japanese when translated means what,Poisoned Water,General,Pienaar what gender is a snail,Hermaphrodite,Entertainment,From where was Ricky in 'I Love Lucy'?,Cuba +General,Who did patrick macnee portray in the spy drama the avengers,Jonathan steed,General,Venustraphobia is the fear of ______,Beautiful women,General,If in law someone is convicted of A.B.H. for what does the 'A' stand,Actual +Music,"In Which Year Did ""Wooden Heart"" & ""Surrender"" Reach No.1 For Elvis",1961,Science & Nature,What Does FORTRAN Stand For ,Formula Translation ,Science & Nature,Sound travels fastest through which state of matter?,Solid + History & Holidays,*What were the 'character' names of the 3 lead women of Charlie's Angels? (PFE)* ,"Sabrina, Kelly, Jill ",General,In what Australian state would you find Katherine,Northern territory,General,"In what Bible book is ""The love of money is the root of all evil""",Timothy 6:10 +General,About which game has most books been written,Chess,Music,"Which 1970's Disco Pop Band Ended Their Chart Career In 1981 With ""We Kill The World, Don't Kill The World)""",Boney M,General,Who was given a honorary Oscar in 1985 after 50 years acting,James Stewart +Music,For Whom Did David Byrne Produce An Album Called Mesopotamia In 1981,B52's,General,How to you turn a woman into a cuckquean,Shag her husband female cuckold,General,What countries name translates as lion mountains,Sierra Leone +Sports & Leisure,Which snooker player ended Stephen Hendry's five-year unbeaten run at The Crucible? ,Ken Doherty ,General,The Egyptian god Horus had the head of what creature,Falcon,Science & Nature, A male pig is a boar. A female pig is a sow. A baby pig is a __________,Piglet +General,"This delightfully soft topping, care of your local bovine",Cheese,General,Johnny Rotten describe sex as five minutes of what,Squelching,General,Sport control amateur level Federation International de Quilleurs,Ten Pin Bowling +General,15% of American males are what - so are bulls,Colour Blind, Geography,Name the sea between Korea and China.,The Yellow Sea,General,What is the fear of cold known as,Psychrophobia +Sports & Leisure,In Which Athletics Event Are Competitors Known As Either 'Spinners Or Shifters'' ,Shot Putt ,General,"Caciocavallo, herkimer, mysost, & trappist are all varieties of what",Cheese,General,What year did the lights go out,1965 +Entertainment,Who hosted the 1997 Grammy Awards?,Ellen Degeneres,Music,"Leslie Gore's Single ""Judy's Turn To Cry "" Was The Sequel To Which Top Ten Hit In 1963",It's My Party, History & Holidays,Sir Henry Morgan was a famous 17th century___? ,Buccaneer  +Art & Literature,Which Shakespearean Character Has The Most Lines ,Hamlet ,General,Who did Sidney say would play her in Sream 2?,Tori Spelling,General,What country introduced the secret ballot for government 1856,Australia - Victoria 27 =- 8 - 1856 +General,Which American author wrote Moby Dick,Herman melville,General,What European country is threatened by over 100 active volcanoes,Iceland,General,Who was Time Magazines first man of the year (1927),Charles Lindbergh +General,An external agent that alters foetal development is called a,Teratogen,Music,Who Replaced Guitarist Mick Taylor In The Stones Line Up,Ronnie Wood, History & Holidays,What is the name of the Russian Czar's daughter who might-or might not-have survived the Russian revolution?,Anastasia +General,"Who said 'ask not what your country can do for you, but what you can do for your country'",John f kennedy,General,What does the pancreas produce,Insulin,General,What is a group of finches,Charm +General,What is a boats speed measured in,Knots,Music,Chris Stein Was A Member Of Which Band,Blondie,General,Jerry Yang and David Filo created what,Yahoo +General,On a chemical what does a skull and cross-bone mean ?,Toxic,General,What is celebrated on 14 July in France,Bastille day,General,What is the young of this animal called: Owl,Owlet +General,Who patrols gotham city,Batman and robin,General,"Who was, ""first in war, first in peace & first in the hearts of his countrymen""",George washington,General,What's the worlds longest rail journey made no train change,Moscow Peking +Entertainment,"Who portrayed Han Solo in ""Star Wars""?",Harrison Ford,General,In Washington its illegal to buy what on Sunday,A Mattress,General,Over where does the uvula dangle,Tongue +Sports & Leisure,Where were the 1988 Olympics held ?,"Seoul, South Korea",General,Hindu religion Batavia is an incarnation of Vishnu in what form,A Fish,General,"What surname do Bobby, John and Teddy have in common",Kennedy +General,What do like charges do,Repel,Science & Nature,What Is Unusual About The Pitcher Plant? ,Its Carnivorous ,Sports & Leisure,How Many Consecutive Wimbledon's Men's Singles Titles Did Bjorn Borg Win? ,5  +General,What is the fear of microbes known as,Microbiophobia,General,In which London thoroughfare is the famous Hamley's Toy Shop,Regent street,General,What does anti pasta literally mean,Before the meal +General,The Atlanta Hawks basketball team have retired 23 which used to belong to _____,Lou hudson,Music,"What Year Was Madonna's ""Like A Virgin"" Released",1984,General,"In Arthurian legend, who was the Lily Maid of Astolat",Elaine + History & Holidays,In What Year Did The Vietnam War Begin ,1954 ,General,Which role was played by Tim McInerny on TV's 'Blackadder IV',Captain darling,Entertainment,"When not a Birdman, what does Ray Randall do for a living?",Police officer +General,What is the capital of the dominican republic,Santo domingo,Music,Belinda Kurczesky Is The Real Name Of Which Singer,Belinda Carlisle,General,In Greek mythology who was the first woman,Pandora +General,Psychologists says men who do what during sex are insecure,Keep socks on,Science & Nature," A pig is a hog _ but a hog is not a pig. ""Hog"" is a generic name for all swine. Per hog_raising terminology, a pig is a baby hog less than __________",10 weeks old,Sports & Leisure,"Which football team was nicknamed the ""Orange Crush""?",Denver Broncos +General,Who had a hit in 1986 with 'Don't Leave Me This Way',The communards,General,Where was the worlds first water clock invented,Peking,General,In Sesame street name the detective who often said egad,Sherlock Hemlock +General,Whose symphony number seven is called the Leningrad,Shostakovich,General,In a French restaurant what's plate de jure translate as,Dish of the Day,General,What does a priest distribute with an aspergillum,Holy water +Music,"Who Recorded The Albums ""Cuts Both Ways"", & ""Into The Light""",Gloria Estefan,General,Bondi Grape Blueberry Lime Tangerine Strawberry colours what,Apple Imac,General,"Only One Planet In Our Solar System Spins Clockwise, Which One?",Venus +General,"What does ""el pueblo de nuestra senora la reina de los angeles del rio porciuncula"" (los angeles' original name!) Translate into",The village of our lady the queen of the angels of the porciuncula river,Technology & Video Games,What do you need to collect 100 of to get an extra life in Super Mario Bros.? ,Coins,Music,"Who Are We ""Tracey Thorn & Benn Watt""",Everything But The Girl +Entertainment,What was John Wayne's real name?,Marion Morrison, History & Holidays,Who Wrote A Famous Diary Between 1660 & 1669? ,Samuel Pepys ,General,Actually caused by layers of hot air refracting sunlight,Mirage +General,The Mariners Compass or Pyxis is what,Constellation,General,What did D H Laurence do with his horse Aaron when it died,Had the skin made a Duffel Bag,General,"What was South Crofty in Cornwall, closed in 1998",Tin mine +Food & Drink,What do you get when you add fresh fruit to red wine ,Sangria ,General,Who was the Greek goddess of retribution,Nemesis,General,Which English Football Club Are Known As The Quakers,Darlington +General,What Country is consistently The Worlds Biggest Beer Consumer,Czech Republic,General,What is the lightest known substance,Hydrogen, History & Holidays,"Greased Lightnin was taken from the film Grease, but who had chart success with it? ",John Travolta  +General,Hathor was the Egyptian goddess of what,The Sky,Music,"According To The Beatles Who helped them to get married in ""The Ballad of John and Yoko""?",Peter Brown,General,What was first man made object to exceed sound barrier,Tip of a whip +Religion & Mythology,Who is the greek equivalent of the roman god Mars ?,Ares,General,RC Cola were the first company to do what,Sell cola in cans,General,Which Michael Jackson song could have referred to a tennis star,Billie jean +General,Pat Reid wrote which book - filmed and TV often,The Colditz Story, History & Holidays,Who Directed The 1968 Classic Horror Movie 'Rosemary's Baby'' ,Roman Polanski ,General,In which country is the port of Stravangar,Norway +General,Which chemical element is named after die Latin word for 'red',Rubidium,General,What is the world's largest weekly newspaper for stamp collectors,Linn's stamp news,Science & Nature,Who Was The Chief Of Aeronautical Research & Development At The British Aircraft Corporation 1945 To 1971 Having Previously Designed The Wellington Bomber Of World War 2 ,Barnes Wallis  +Music,"""Dancing With Tears In My Eyes"" Was A 1984 Hit For Which Group?",Ultravox,Sports & Leisure,Which Sport Was Featured In The Movie 'When We Were Kings ,Boxing ,General,Who was the first president whose mother was eligible to vote for him,Franklin delano roosevelt +Music,Who Took A Eurovision Song Contest Entry To No.2 In 1969 And What Was It,Lulu / Boom Bang A Bang,General,L is the roman numeral for what number,Fifty,General,An african-american dance in which couples strut and compete with high kicks and fast steps.,Cakewalk +General,In Eureka Nevada its illegal for moustached men to do what,Kiss Women,General,"Who recorded the album ""the smoker you drink, the player you get""",Joe walsh,Science & Nature," It is estimated that a single toad may catch and eat as many as 10,000 insects in the course of a __________",Summer +Sports & Leisure,Which Country Hosted The Olympic Games In 2004? ,Greece (Athens) ,General,Flexible disc for storage of computer data,Floppy disk,Science & Nature," When eating, __________ Often gorge themselves to the point that they can't fly. the bird will quickly regurgitate its meal to become light enough to escape if flight from an attacker is necessary.",Vultures +General,What animal is the mascot of the US Naval Academy,Goat,General,Name the first war submarine invented by David Bushnell it sank,Turtle,General,In Delaware it is illegal to pawn what,Wooden Leg + History & Holidays,Who Wtote The Book 'How The Grinch Stole Christmas' ,Dr Seuss ,General,What is the alternative name of the foodstuff called sea asparagus,Samphire,General,From Here to Eternity gets its name from a poem by who,Tennyson +General,Sabotage is French - What did the saboteurs use,Shoes - sabot means shoe,Music,"Chicago Found It ""Hard To Say I Love You"" Or ""Hard To Say im Sorry""",Hard To Say Im Sorry,General,The milk of what creature will not curdle,Camel +General,What is the flower that stands for: sweet and secret love,Honey flower,Music,Wet Wet Wet Come From Which Scottish Town,"Clydebank, Near Glasgow",General,"What linked Armenia, Georgia, Latvia and Moldavia",USSR +General,On which U. S. river is the Grand Coulee Dam,Columbia,General,Who was Spandau Prison's last inmate,Rudolf hess,Music,In Which Us City Was Motown Records Founded,Detroit +General,In music what is meant by pianissimo,Very softly,General,Who is called the 'texas tornado' by her fans,Tanya tucker,Science & Nature,Units of frequency?,Hertz +General,The Largest Muslim Population In The World Is Found In Which Country,Indonesia,General,Which Australian city stands on the Port Jackson Inlet,Sydney,General,In Bradshaws you would find information about what,Railways +General,In Russia by law the homeless must be where after 10pm,At Home,General,The statue of Eros in Piccadilly Circus commemorates which Victorian reformer,Earl of shaftsbury anthony ashley cooper,General,Women do it 4 times to a mans once - what,Shoplift +General,"""Mr. Mojo Risin"" is an anagram for___",Jim Morrison.,Art & Literature,"What did Jeannie C. Riley describe as ""a little Peyton Place""?",Harper Valley,General,Mungo Scott travelled the River Niger in what year,1795 + Geography,What is the only country in the world whose name starts with 'O'?,Oman,General,What did Snoop Dogg change his name to in 2012?,Snoop Lion,General,Who won the Oscar for Best Director for the film The French Connection in 1971,William friedkin +General,What did Aristotle claim as the most delicate of table meats,Camel,General,Which cities public transport lost property office is the busiest,Tokyo,General,What is the flower that stands for: sharpness,Barberry +General,First feature film US TV Heart of New York what was subject,Washing machine inventors,Science & Nature,What is the technical name for an animal's pouch?,Marsupium,General,What two '80's dance movies did Cynthia Rhodes have a major role in?,Dirty Dancing and Flashdance +Science & Nature,What word is used for a male ass? (Other than that the word used for that #*§^*&%@! ex-boyfriend.),Jack,General,Name the national rugby team of Argentina,Pumas,General,Who fiddled while rome burned,Nero +General,Golden books in how many films did grace kelly appear,Eleven,General,John Garfield and Lana Turner starred in the film 'The Postman Always Rings Twice' what year was it released,1945,General,Which entire novel is set in June 16th 1904,Ulysses +General,Which day is the first day of Holy Week in the Christian calendar,Palm sunday,General,"What Spanish islands are Gomera, Hierro & Lanzarote a part of",Canary islands canary,General,"Who Said During Their One & Only Oscar Acceptance Speech ""I Think They Gave It To Me Because Im The Oldest""",Jessica Tandy +General,The transverse entrance hall of a church.,Narthex, Geography,What mountain range separates Europe from Asia?,Ural,General,Amahl and the Night Visitors was the first opera written for what,Television +General,How did Jamie Sommers become bionic?,From a parachute accident.,General,The green variety of beryl is called ________,Emerald,General,"Which credit card company has the slogan ""don't leave home without it""",American express +Science & Nature,When does the human uterus expand 500 times its normal size?,During pregnancy,General,Paludism is an old name for which disease,Malaria,General,What is a Tragopan,A Himalayan Pheasant +Entertainment,Who sang 'That's Alright Mama'?,Elvis Presley,Music,Which Pennsylvania DJ Helped Launch Rock By Hosting Live Shows At The New York Paramount In The 1950's,Alan Freed,General,"Duvali, Dushira and Holi are religious days in which religion",Hindu + History & Holidays,Who had a hit with the smooth and soulful You To Me Are Everything ? ,The Real Thing ,General,To what Scandinavian country would you have to travel to watch the football team Malmo play at home,Sweden,General,We know who wrote Little Women but who wrote Little Men,Lousia May Alcott +General,What is the largest item on any menu in the world,Roast camel, History & Holidays,What did Julius Caesar cross to signal a revolt against the senate?,Rubikon, History & Holidays,What Model Of Children's Bicycle Had A Gear Shift On The Frame ,The Chopper  +General,Which famous battle took place on July 1st to 3rd 1863,Gettysburg,General,What translates and executes your program,An interpreter, Geography,What is the basic unit of currency for Gambia ?,Dalasi +General,-isms: The social and philosophic creedo of Irish playwright George Bernard Shaw is called:,Shavianism,Technology & Video Games,What was the first home console system released by Sega? ,Sega Master System,General,"Country in East Asia, the world's third largest country by area (after Russia and Canada) and the largest by population",China +General,Rustic or awkward person,Bumpkin, History & Holidays,Which new country was formed in 1971 at the end of the Pakistan / India conflict? ,Bangladesh ,Geography,In which city is the Wailing Wall,Jerusalem +General,In which trade are composing frames and sticks used,Printing,Music,"Who Had Hits With Parodies Of Sh Boom , Rock Island Line, & Heartbreak Hotel",Stan Freeberg,General,What was the most commonly occurring name on the internet,Bill Clinton +General,What is an erythrocyte,Red blood cell, History & Holidays,"Germany's WW I allies were Austria-Hungary, Bulgaria, and ________.",Turkey,General,"Which sportsman, who died in 1999, was nicknamed the Yankee Clipper",Joe di maggio +General,What do the letters 'SAM' mean in SAM missiles?,Surface To Air, History & Holidays,What is the Roman numeral for fifty,L,General,What did a clue originally mean,Ball of Thread +General,"Which artist, born in Russia in 1887, painted Self Portrait With Seven Fingers, Birthday and Bouquet With Flying Lovers",Marc chagall,General,"A carbohydrate consists of carbon, hydrogen, and which other element",Oxygen,General,In the Simpsons name Ned Flanders wife,Maude +General,What is the leaf of a fern called,Frond,General,The city of winnipeg manitoba's by-pass is known as _____,Duff's ditch,Music,Who was the first girl group to top the UK singles chart?,The Supremes +General,What was founded in the UK 1694 because of war with France,Bank of England,General,Who said Old age isn’t so bad - considering the alternative,Maurice Chevalier,Food & Drink,Which Chef Created The 'Peach Melba' In Honour Of An Australian Opera Singer ,Auguste Escoffier  +General,What large red flower is the floral emblem of New South Wales,Waratah,General,What measure is used for the purity or fineness of gold,Carat, History & Holidays,"*What 70s blockbuster included the music, 'Cantina Band' by Mecco?* ",Star Wars  +General,Where does the word COP come from,Constable on Patrol,General,What very rude nickname was given to suede shoes with rubber soles in the 1950s,Brothel creepers,General,A foot-long ruler is __ inches long.,12 +Science & Nature,What Do The Initials E.E.G Stand For ,Electroencephalograph ,General,What Is The Most Successful Comedy Movie Franchise Of All Time,Police Academy,General,What sport uses barrier stalls,Horse racing +General,What 1970's film came from a pink floyd tune,The wall,General,A belief that government and law should be abolished,Anarchism,General,Malden Serkiovitch famous as which actor,Karl Malden +General,Who wrote 'weird harold and fat albert',Bill cosby,General,Mariner Chinese: What do you call a Chinese sailing ship,Junk,Entertainment,What is Elton John's real name?,Reginald Dwight +General,Somebody who rings bells,Campanologist,Art & Literature,This magazine chronicled the Man of Bronze and the Fabulous Five.,Doc savage,General,The University of Houston once elected what rock star as homecoming queen,Alice cooper +Science & Nature,"What does the ""lithosphere"" refer to?",The earth's crust,General,The Chinese pictogram for trouble also means what,Two women under same roof,Music,Who Was The First To Sing On The 1984 Band Aid Single,Paul Young +General,Which is the most Northerly African country through which the Greenwich meridian passes,Algeria,General,What was the first country to approve aid drug azt,Britain,General,Who was the first woman in space,Valentina tereshkova +General,Karl Landsteiner won a Nobel prize in 1930 for which medical discovery,Blood groups,General,What are a swallowtail and a burgee,Flags,General,H2S04 is the chemical formula for which compound,Sulphuric acid +General,What Crime Has A Person Committed If They Have Been Convicted Of Regicide?,They Have Killed The King,General,What is a group of this animal called: Swine,Sounder drift,General,Old times mid eastern women swallowed what as contraceptive,Foam from camels mouth +General,What type of acid is used in car batteries,Sulphuric,General,"What sport was involved in the movie ""kansas city bomber""",Roller derby,Science & Nature,What name is given to a chemical reaction which takes in heat ?,Endothermic +General,Who was the last King of Troy?,Priam,General,What is an ethnologue,Catalogue of languages,General,"Roald Dahl's children's story Charlie and the Chocolate Factory was made in to a film, what was the title",Willie wonka and the chocolate factory +General,The study of the composition of substances and the changes that they undergo is _________.,Chemistry,General,Household items such as television sets and audio equipment are know as,Brown goods,General,What gun does James Bond traditionally use,A Walther PPK +Music,"When The Motels Took The ""L"" Out Of Lover It Was What",Over,General,"What links Buddy Holly, Lyndon Johnston, Janice Joplin",State of Texas,General,Which Bruce made the cover of Time in 1975,Jaws - Bruce was the shark + History & Holidays,The Gold Coast gained its independence from Great Britain in 1957 and renamed itself as what ,Ghana ,General,The belief that no God exists,Atheism,General,Which well-known author wrote the James Bond novel Colonel Sun under the pseudonym Robert Markham,Kingsley amis +General,How Many Hearts Does The Common Earthworm Have?,10,Sports & Leisure,What sport would you helicopter to the Bugaboos for,Skiing,General,What was the city symbol of Pompeii ,A Winged Penis +Sports & Leisure,In which Gymnastics discipline would a man do the scissors? ,Pommel Horse ,General,What was the first James Bond film,Dr No,General,What do the scottish call hockey,Shinny + History & Holidays,Which Football Team Became The First To Win The European Cup Winners Cup In 1963 ,Tottenham (Spurs) ,General,What does GDP stand for ?,Gross Domestic Product,General,Who fired the first shots in the 1970 film MASH,The Football game Timekeeper +General,What brand of cheese celebrated its 60th birthday in 1988,Velveeta,General,Who wrote Travels with a Donkey on his honeymoon,Robert Louis Stevenson,General,What song do elvis presley and ub40 have in common,Can't help falling in +Toys & Games,How many tiles does a player play to score a bingo in Scrabble,7,General,Storm Warning the only film in which this Actress killed who,Doris Day,Entertainment,What was Citizen Kane's first name?,Charles +General,Pathophobia is the fear of,Disease,General,What is the fear of friday the 13th known as,Paraskavedekatriaphobia,General,What is the correct name for a dandelion seed ball,A Clock +Entertainment,What song by Don McLean talks about the day Buddy Holly died?,American Pie,General,"A 1976 hit single for Cockney Rebel was titled ""make me _____""",Smile,General,Lusitania was the Roman name of what modern country,Portugal +General,Which Novelist Sometimes Writes Under The Pseudonym Richard Bachman?,Stephen King,General,What term is applied to rocks changed by heat and pressure,Metamorphic,Music,Who is the Youngest Beatle?,George Harrison +General,What was the name of Punky Brewster's dog?,Brandon,General,"Either of the contibuters to the 4-note Hindol ""Taril ha juj Girlja Shankur""",Marathe,General,"Countries of the world:western Asia, the capital is Tehran",Iran +General,"In Greek mythology, what did the cyclops forge for zeus",Thunderbolts,General,What U.S. state is free of houseflies,Alaska,General,A sea with many islands,Archipelago +General,Collective nouns - A gang of what,Elks,Sports & Leisure,Who Was The First Asian Snooker Player To Be Ranked In The World's Top 10? ,James Wattana ,General, An artist supports his canvas on a(n) _________.,Easel +General,In what sport would you perform an Adolf,Trampolining forward 3.5 twists,General,"During which war did the battles of The Modder River, Majuba and Magersfontein take place",The boer war,Music,"Name The Band ""Eight Wonder's"" Lead Singer A) Tiffany, B) Patsy Kensit, C) Debbie Gibson",Patsy Kensit +Science & Nature,Name Tin Tin's Canine Companion ,Snowy ,Food & Drink,"Natural vanilla flavoring comes from this plant, What Is It ",Orchid ,General,What planet's orbit does Pluto's intersect,Neptune +General,Where does the famous 'running of the bulls' take place,Pamplona,Science & Nature,Light rays consist of small packets of energy called _____,Photons,General,Winston Churchill had a dog - what type,Miniature Poodle +General,What 80's Pro-Wrestler was turned into a G.I. Joe character?,Sgt. Slaughter,General,"What sport has 'screwballs', 'bunt' and 'flies'",Baseball,General,On which national flag is there an eagle and a snake,Mexico +Geography,"The Amazon river pushes so much water into the _____________ that, more than a hundred miles at sea, off the mouth of the river, one can dip fresh water out of the ocean and drink it.",Atlantic,Geography,"There are 42 other year_round research stations on Antarctica. All told, about __________ people live on Antarctica in summer, 1,000 in winter.","4,000",General,Baseball: the baltimore ______,Orioles +General,Who starred as 'ouboet' in the first tv series of 'orkney snork nie',Frank,General,Pierre Augustine Caron de Baumarchais play what Opera based on it,Barber of Seville,Art & Literature,"The book ""Wamyouruijoshou"" was the first to use what word ?",Kite +General,What does a Belly Man do for a living,A Piano Tuner,General,Who was the first United States Secretary of State,Thomas jefferson,General,What is Canada's oldest city founded in 1608,Quebec +General,Middle Ages having what creatures in the house was good luck,Ants,General,What is the name given to the salted roe of a sturgeon,Caviar,General,Which ovine expression is used for a disreputable member of a family or group,Black sheep +Food & Drink,Which type of restaurant popularized the use of conveyor belts? ,Sushi Restaurants ,General,What New England state is the fitting site for the town of Teaticket,Massachusetts,Music,Who was the first Beatle to have a solo No 1 hit?,George Harrison +General,Who were the co-leaders of rock pile,Edmunds and lowe edmunds and lowe,General,Levi Stubbs Renaldo Benson Abdul Fakir Laurence Payton Who,The Four Tops,General,What is a Kerry Blue,Dog type of Terrier +General,Knismolagnia is sexual arousal from what,Tickling,People & Places,Which Pop Stars Called Themselves sharon & Phyllis ,Elton John & Rod Stewart ,General,What is the flower that stands for: comforting,Scarlet geranium +General,"In which film is the line, 'I could have been a contender'",On the waterfront,Music,Which Successful Glam Career Was Eneded In A 1977 Car Crash,Marc Bolan,General,George Armstrong Custer was court martialed in 1867 for what,Hit a fellow officer +General,What word beginning with M is the unit measurement for the brightness of stars,Magnitude,Science & Nature,What do the initials nasa of the us government agency for space flight represent ,National aeronautics and space administration ,General,Capital cities: Denmark,Copenhagen +General,Michelle and Julia are songs by which group,The beatles,General,In sport what would a person do with a quoit,Throw it,General,"Acronym for quasi-stellar radio source, any of the blue, starlike objects that are strong radio emitters and the spectra of what exhibit a strong red shift",Quasar +Art & Literature,Who killed Macbeth?,Macduff,General,Who is maureen o'sullivan's daughter,Mia farrow, History & Holidays,What did 'My True Love'' give to me on the eight day of Christmas ,Eight Maids-a-milking  +General,What is the fear of nudity known as,Nudophobia,General,What was the first music CD burned in America,Springsteen's Born in the USA,General,What's the highest navigable lake in the world,Lake titicaca titicaca +General,Which African country had its capital transferred to Abuja,Nigeria,Music,"Which Film Featured The R Kelly Song ""I Believe I Can Fly""",Space Jam,Music,"Who designed the ""banana cover"" of the Velvet Underground's debut album?",Andy Warhol +General,Does 'verbatim' mean 'in the same words' or 'opposite',In the same words,General,Which snake kills the most humans,King cobra,General,"Which dictator preferred 50,000 rifles to 50,000 votes",Benito Mussolini + History & Holidays,How do you write 69 in Roman numerals?,LXIX, History & Holidays,As what was winchester known by the Romans?,Venta Bulgarum,General,Which US TV show was the top rated in the 1988 89 season,Roseanne +General,Beside the long winding river is the translation of which US state,Connecticut,General,What Type Of Fruit Is Known As A Warwickshire Drooper,A Plum,General,Which writer rode Devon Loch in the 1956 Grand National,Dick Francis +General,"What actor was stung in ""the sting""",Robert shaw,General,Scotia What would you call the act of making a mark on a body by burning,Branding,General,What do Rastafarians generally refer to God as,Jah + History & Holidays,In which country was the Rosetta Stone found,Egypt,General,Which city is the location for the 1998 Commonwealth Games,Kula lumpur,General,What colour do you get when you mix blue and red together?,Purple +Science & Nature,What is an emasculated stallion called?,Gelding,Music,Under What Name Did Vince Clark And Alison Moyet Record?,Yazoo,General,From which sport does the phrase' to win hands down' come,Horse racing +General,In which Dickens novel would you find the characters Jonh Jarndyce and Mrs Jellyby,Bleak house,Music,"Number of music artists who have covered ''Yesterday""","2,961",General,What cat is unable to draw its claws into its paws,Cheetah +Science & Nature,Infantile Paralysis is commonly known as ________.,Polio,General,Who wrote The Hunchback of Notre Dame,Victor Hugo, History & Holidays,The _____ universe was replaced by the Copernican universe.,Ptolemic +Science & Nature,What is another name for the prairie wolf?,Coyote,General,The bones of a _____ weigh less than it's feathers,A pigeon,General,Where could you spend your Gourde,Haiti +General,"On Which Island Which Is Also A Country Will You Find ""Adams Peak""",Sri Lanka,General,What year did the Bolsheviks overthrow the Russian government,1917,Entertainment,"What was the original name of ""Little Rascals""?",Our Gang +Music,What band did James Brown tour and record with in the 1950's?,The Famous Flames,Food & Drink,Sorbitol dulcitol and xylitol are forms of what?) ,Sugars (artificial sweeteners) ,General,56% of men cannot tell you the colour of what in their house,Vacuum Cleaner +Sports & Leisure,Who Was The First Professional Footballer To Be Knighted ,Sir Stanley Matthews ,General,To what did touching the ark of the covenant lead,Death,General,What space shuttle did Discovery replace,Challenger +General,Which is the only Shakespeare play with the name of an English town in the title,The merry wives of windsor,Sports & Leisure,Baseball: The Toronto _________.,Bluejays, History & Holidays,King Richard the ________?,Lionhearted +General,In Britain what are Sing Tao and Weng Wei Po,Chinese Newspapers,Food & Drink,From what country does Sangria originate? ,Spain ,General,Rapa Nui is better known as what,Easter Island +General,Colonel Tom Parker Elvis's manager had what earlier act,Dancing Chickens – on hot plate,Science & Nature,What Is Herpes Zoster More Commonly Known As ,Shingles ,General,Which US states constitution was the first to prohibit slavery,Vermont +General,What writer lived at hilltop near Hawkshead now museum to her,Beartrix Potter,General,What is the fear of insanity known as,Maniaphobia,General,Who was W.C. Field's co-star in the film 'My Little Chickadee',Mae west + History & Holidays,What Was The Contribution Of Actress Mercedes McCambridge To Linda Blairs Performance In The Excorcist ,Provided The Devils Voice ,General,What is a curragh,Boat,General,"Who would use a Jigger, Buzz, Flagging iron, Round shaver, Adze",Cooper – making barrels +General,What color is the tip of a Canada lynx's tail,Black,Sports & Leisure,The person who carries the golfer's clubs is called a(n) ________.,Caddie,General,Notional environment in which electronic communication occurs,Cyberspace +General,La Celestina was the first one in Spain the first what,Theatre play,General,What do the letters in SAM missiles refer to,Surface_to_air missile,General,"Which Rock Band Took Their Name From The Latin For ""Beyond These Things""",Procol Harem +General,What was the first mickey mouse cartoon,Plane crazy,General,"What did Portuguese explorers christen O Rio Mar, ""The River Sea"", in the 16th century",The amazon river amazon river,General,What would a German do with a Gravenstein,Eat it - a yellow apple +General,"What modern word comes from the Arab ""hashishi"" ?",Assassin,General,Two things grew on the little nut tree. A silver nutmeg & what else,Golden pear,General,"A tall, tapering, four-sided stone shaft with a pyramidal top.",Obelisk +General,Hoffman who wrote about a british agent named george smiley,John le carr,Entertainment,What album holds the world record for copies sold?,Thriller,General,"Which singer was portrayed by Kevin Spacey in the film ""Beyond the Sea""?",Bobby Darin +General,What is the main food of the oyster catcher,Mussels,General,Caligari is the capital of what island,Sardinia, History & Holidays,How many people were killed in the battle of Lexington?,Eight +General,Bob Fitzsimmons world boxing champion had what middle name,Prometheus,General,Who was 'the elephant man',John merrick,General,Melissophobia is the fear of,Bees +General,Oil seed rape belongs to which plant family,Mustard,Entertainment,What character did Tex Avery first create upon arriving at MGM?,Screwball Squirrel,General,Marlon Brando starred in 'Last Tango in Paris' which actress was he tangoing with,Maria schneider +General,What first occurred at California Disneyland in March 1981,Murder,General,Of what modern mammal is the Mastodon an early relative,Elephant,Geography,The land area of the country of __________ is slightly smaller than Alabama.,Greece +General,"Area of commerce that encompasses farming or trapping certain furbearing animals, processing their skins for sale to manufacturers of fur garments, & marketing finished garments to retail outlets",Fur industry,General,A philomath has a love of what,Learning,Science & Nature,Where Might You Find The Metatarsals ,In The Foot  +Sports & Leisure,What Colour Caps Do The Australian Cricket Team Wear ,Green ,Science & Nature,"If a robin's egg is put in vinegar for thirty days, what colour does it become?",Yellow,General,On what are the worlds smallest paintings painted,Pin Heads +General,What is the latin word for 'a junction of three roads',Trivia,General,In which Cathedral is Jane Austen buried,Winchester,Music,Which Buddy Holly song was covered by The Rolling Stones,Not Fade Away +General,49% of Americans go out to dinner on what day,Their Birthday,General,Who was Margret Thatcher?,Prime Minister of Great Britan,Music,"Hello, Hey Now & Wonderwall Are Tracks Of Which Best Selling Album",Whats The Story Morning Glory +Sports & Leisure,Who Rode Shergar To Victory In The Epsom Derby? ,Walter Swinburn ,General,What country consumes the most fish per capita,Japan,Entertainment,"He was the villain in ""Star Wars"".",Darth Vader +General,Whose normal heartbeat is 242 per minute,Mr Spock in Star Trek,General,Instead of what does the royal family use rose petals,Confetti,Art & Literature,"Who wrote the epic poems, the Iliad and the Odyssey?",Homer +Art & Literature,What Was The Name Of Charles Dickens Last Novel Unfinished At His Death ,The Mystery Of Edwin Drood ,General,What is the largest volcano on mars,Olympus mons,Sports & Leisure,Football: The Denver _________.,Broncos +General,What are The Chiuhauhan Nubian and Alaskan,Deserts,General,Which red-haird slide guitar player won a grammy in 1989 for her album 'nick of time',Bonnie raitt,General,A sadhu is a holy man in which country,India +General,What's the seventh sign of the Zodiac,Libra, History & Holidays,Who Was The First Christian Emperor Of Rome ,Constantine The Great ,Music,What video can Angelo of Fishbone be seen in with the Mighty Mighty Bosstones?,Simmer down +General,New Zealand's Rugby team is know as the __________________.,All Blacks,General,Who was the last incan king of peru,Atahualpa,Entertainment,What is Cape Town's major choir called?,Philharmonic choir +Sports & Leisure,At which sport was Fred Perry crowned world champion in 1929? ,Table Tennis ,General,The Red Rose City has what more common name in Jordan,Petra,General,Who assassinated john lennon,Mark david chapman +Art & Literature,"Who is associated with the address 221B Baker Street, London ?",Sherlock Holmes,General,In what Australian state would you find Inverell,New south wales nsw,Music,Which Song By The Palindromic Pop Group Abba Had a Palindromic Title?,SOS +Sports & Leisure,In Which Scandanavian Country Is Gambling Illegal ,Sweden ,General,Destructive insect related to the grasshopper,Locust, History & Holidays,She was the greatest trick shot artist of all time.,Annie Oakley +General,Which country is the largest producer of cheese,America,General,Who was ronald reagan's first wife,Jane wyman,General,"Firefly, Longhorn, Harlequin and Tortoise types of what",Beetles +Geography,Which state is the Evergreen State,Washington,General,What is the name for a branch of a river?,Tributary, Geography,What is capital of Lithuania?,Vilnius +Science & Nature,What name is given to animals which eat both plants and meat ?,Omnivore,General,"If you were to fly due West from New York, what would be the next country you would fly over",Japan,General,What was the name of the bartender on The Love Boat?,Isaac Washington +General,What is the most redesigned appliance in the world,Telephone Handset,Music,"Name the Kiss member whose solo album featured Cher, Bob Seger, Donna Summer, Helen Reddy and Janis Ian?",Gene Simmons,General,What did boxer Cassius Clay change his name to,Muhammed ali +Music,"Where was ""Can't Buy Me Love"" recorded?","Paris, France",General,An integer that is greater than 1 & is divisible only by itself & 1 is known as a(n) _______,Prime number,General,What Was Invented By The American Company Texas Instruments In 1958?,The Silicone Chip +General,"What's the sky king's home, near the town of grover, called?",Flying crown ranch, History & Holidays,Who was the last British Viceroy of India? ,Lord Louis Mountbatten ,General,"How much memory did the early pc, the sinclair zx80 have",One kilobyte +Science & Nature,Which Continent Is The Natural Habitat Of The Ostrich ,Africa ,General,In a MORI pole what does MORI stand for,Market opinion research international,General,Coprastastaphobia is the fear of what,Constipation +General,"What craft toy involved cutting plastic figures, coloring them in, and then baking them in the oven?",Shrinky Dinks,Science & Nature, Adult polar bears usually eat just the skin and blubber of a seal. They leave the meat for cubs and scavengers. One seal will sustain an adult bear for __________,11 days,General,Since 1980 Willeston School Near Nantshich Cheshire Has Staged The World Championships At Which Sport,Worm Charming +General,Deepest from what language is the word mummy derived,Persian,General,U.S. captials Arkansas,Little rock,General,Earth's outer layer of surface soil or crust is called the _____________.,Lithosphere +General,When was buckingham palace built,1703,General,72% of what country is covered by forest,Finland,General,Where is the world's largest computer manufacturing plant,Japan +General,Which is the only body cell with no nucleus,Red blood cell,General,Who wrote the book 'Jaws',Peter benchley, History & Holidays,What is the title of the 1992 Belgian film about a film crew following and documenting the exploits of a serial killer? ,Man bites dog  +General,"Who Was The First And Original Host Of ITV's ""World Of Sport""",Eamon Andrews,General,What is Snoopy's mothers name,Missy,Sports & Leisure,During A Hockey Bully Off How Many Times Must Sticks Touch ,3 Times  +General,An onomastician studies what,Names,General,What does an optician make,Spectacles,Entertainment,What was the original name of Paul McCartney's fictional church cleaner 'Eleanor Rigby'?,Miss Daisy Hawkins +General,Hey! what was the name of this song released by 'the romantics' in february 1980,What i like about you,Sports & Leisure,"What vehicles are involved in the ""Tour de France""",Bicycles, Geography,What is the capital of Democratic Republic of the Congo?,Kinshasa +Music,Which Band Performed The Theme To The Secret Diary Of Adrian Mole Aged 13 & 3 Quarters,Ian Dury & The Blockheads,General,What did archers at the ancient Olympics use as targets,Tethered Doves, History & Holidays,Where were the Hanging Gardens?,Babylon +General,Penis comes from the Latin meaning what,Tail,General,Name the first chocolate bar created by Forest Mars in 1923,Milky Way,General,Who was the first woman scientist to be awarded a Nobel prize,Madame curie +General,What is the colour of mourning in Turkey,Violet,General,What is a group of wolves,Pack,General,When was the English and Scottish parliament united,1707 +General,What special talent did Molly Ringwald have in The Breakfast Club?,She could apply lipstick with her breasts,General,Where Will You Find The Spirit Of Ecstasy?,On A Rolls Royce,Music,What Is The Common Term For A Slow Dance In Triple Time,A Waltz +General,Amatripsis is what sexual practice,Female masturbation rubbing labia,General,Collective nouns - A Congregation of what,Plovers,General,"Which photographer, in 1976, told us that we'd take great snaps if we used an Olympus",David bailey +General,Name the Shakespeare character son of the witch Sycorax,Caliban in The Tempest,General,What does Intel stand for,Integrated Electronics,General,Japan's equivalent to the dollar is the ______?,Yen +General,What is a calm ocean region near the equator called?,Doldrums,General,Who was goyathlay,Geronimo,General,What kind of creature is a 'dunnock',Bird +Music,Susanne & Joanne Catherall Sang With Which Band,The Human League,General,What countries flag red circle on green background,Singapore,General,Quidnunc means an eager gossip but in Latin literally means,What Now +Geography,"Created By German Scientists In 1876, What Was The First Artificial Favouring ",Vanilla Essence ,General,Whose debut album was Definitely Maybe,Oasis,Music,"Who Guessed ""That's Why They Call It The Blues""",Elton John + History & Holidays,What's the oldest college in the u.s. ,Harvard ,Technology & Video Games,What member of the original StarFox team betrays the group to join StarWolf? ,Pigma Dengar,General,On television what was Flipper,Dolphin + History & Holidays,Where in the UK would you go to celebrate 'Uphelia'' shortly after Christmas ,Shetland Isles ,General,Who is the Roman equivalent of the Greek god Dionysus,Bacchus,General,Excluding cars what the most commonly used 4 wheel devices,Supermarket Trolleys +General,What is the young of this animal called: Antelope,Calf, Geography,What is the basic unit of currency for Italy ?,Lira,General,What colours are on a pollution free beach flag,Blue - gold stars +General,The via appia went from rome to ______,Brindisi,General,Where were the first European coffee houses opened,Vienna,Technology & Video Games,"Who pilots the Gundam ""Epyon"" in the game 'Gundam Wing: Endless Duel'? ",Miliardo Peacecraft +General,Who qualified for pole position in the 1984 brazilian grand prix,Elio de,General,What is the most popular street name in the US?,Park Street,General,What was the hang out of the 90210 gang?,The peach pit +General,The distance around the outside of a circle is its ____________.,Circumference,General,Diana ross sings 'everytime you touch me i become a hero'. what is the song title,When you tell me that you love me,General,Whose motto is 'Be prepared'?,Boy Scouts + History & Holidays,"""What Is The Name Of The Toy That Arnold Schwarzenegger Desperately Tries To Get For His Son For Christmas In the Film """"Jingle All The Way""""?"" ",Turbo Man ,Science & Nature,What Is The Name For 1 Followed By 100 Noughts ,A Googol ,General,"In Far East Asian Cuisine What Is ""Hashi"" More Commonly Known As",Chopsticks +General,If you landed at Lindberg airport where are you,San Diego,General,What is the state bird of Nevada,Mountain bluebird,General,What game show had Dr Joyce Brothers as a panelist from 1978 to 1980,Gong + Geography,Which Central American country extends furthest north?,Belize,General,What is the original literal meaning of the word bride,To cook (ancient tutonic),General,Mojo is a Filipino what,Alcoholic fruit punch +General,What replaced English as the official language of Kenya in 1974,Swahili,General,What is the correct name for a two handed timber saw,Whipsaw,Science & Nature,What Bird Uses A Stone To Smash Open Its Prey? ,The Thrush  + History & Holidays,On what date did America become an independant nation?,"July 4th, 1776",Music,What Kind Of Turks Did Rod Stewart Sing About,Young Turks,General,Who landed on Timor Island after being cast adrift,Captain Bligh +General,What is the flower that stands for: steadfast piety,Wild geranium,General,Condition of persistent mental and emotional stress occuring after injury or severe shock,Post-traumatic stress disorder,Music,What Was Tom Jones First Ever UK No.1 Single?,It's Not Unusual +General,"On One Day at a Time,what were the two daughter's names?",Barbara and Julie,General,Japanese rice wine,Sake,General,What is the only state with an official state ship and hero,Connecticut +Science & Nature,What Animal Can Change Its Colour? ,A Chameleon ,General,St Sithney is the Patron Saint of what,Mad Dogs,General,In mythology who rode an eight legged horse called Sleipner,Odin +General,In the nursery rhyme what is Fridays child,Loving and Giving,General,What was made illegal in England in 1439,Kissing,Science & Nature, The average life expectancy of a leopard in captivity is __________,12 years +General,What key is to the right of T on a keyboard?,Y,General,In which country would you find the holiday destination of Sousse,Tunisia,General,What is the Capital of: Kazakhstan,Astana +General,What is the main food of walruses ,Clams, History & Holidays,What Was The Most Common Present Girls Above The Age Of 16 Have Asked For in 2008 ,A Boob Job ,Religion & Mythology,"The Greek goddess of fertility, also known as a protectress of witches.",Hecate +General,In A 2007 Poll What Music Video Was Voted The Best Video Of All Time,Peter Gabriel / Sledgehammer,General,More people are killed by donkeys every year than are killed in ______,Plane,General,Which ballet position is named after the French for bent,Plie +Art & Literature,What is the fourth book in the Harry Potter series ,The Goblet Of Fire ,General,What is the most popular beverage in north america,Milk,General,What is the medical term for cancer of the blood,Leukemia +Art & Literature,For What Is Dame Margott Fonteyn Famous ,Ballet Dancing ,General,In Greek mythology who was the father of Iphigenia & Electra,Agamemnon,General,Where were the 1960 summer Olympics held?,"Rome, Italy" +General,In which Chinese city is the Terracotta Army,Xian,General,What mountains are located on the border of Tennessee & North Carolina,Smoky mountains,Art & Literature,Who wrote 'The Birds'?,Daphne du Maurier +Entertainment,Who played George Costanza on 'Seinfeld'?,Jason Alexander,General,Which of Santa's reindeer comes first alphabetically,Comet,General,Songwriter Bernie Taupin wrote for which well known musician,Elton john +People & Places,Which FootbaIl Bad Boy Was Accused Of Violent Assault Against Tv Presenter Ulrika Jonsson ,Stan Collymore ,General,Geniophobia is the fear of what,Chins,General,O'Shey Jackson became better known as who,Ice Cube +General,Capitol city - Cathedrale Notra-Dame - statue Marron Inconnu,Port au Prince Haiti,Music,Who wrote the Opera 'The Magic Flute'?,Mozart,Music,"In What Year Did MTV First Hit The Airwaves A) 1979, B) 1980, C) 1981, D) 1982",1981 + History & Holidays,Alphabetically Which Is The Last Of Santa's Reindeers? ,Vixen ,General,What UK football team nick The Glaziers play at Selhurst Park,Crystal Palace,General,What are a Jalpa Jarama Shamal and Merak,Italian Sports Cars +General,The American Triple Crown - Belmont st Kentucky Derby and?,Preakness Stakes,General,"When using a telephone, you must wait for a ____ tone before starting your call.",Dial,General,What are the Roman numerals for 505,DV + Geography,What country is directly north of the continental United States?,Canada,General,Who laughed when the cow jumped over the moon,Little dog,General,For what olympic team did andre agassi's father box,Iranian olympic team +General,Happy Days was a spin off from what US TV show,Love American Style,General,"In the film Trading Places, who played the part of the prostitute",Jamie lee curtis,General,"The measurement by which parts of a building are related to one another, for example, the diameter of a column. ",Module +General,Who was called The Man of Destiny,Napoleon Bonaparte,General,Britain's first space rocket was launched in 1964. What was it called,Blue streak,General,"What are the two highest consecutive numbers that can be multiplied together to give a number less than 1,000",31 & 32 +General,Which thick custard-like food is made when fresh milk is artificially curdled by bacteria,Yoghurt,General,In which state is Michael Jackson's Never Land Ranch?,California,General,Stanley Kubricks Full Metal Jacket was filmed in what Location,London's Dockland +General,What is the only bird that can fly backwards,Hummingbird,General,With whom is Ludwig Ritter Von Kochel best associated,Mozart - catalogued K numbers,General,In what game might you collect a pung of East Winds,Mahjongg +General,What is a group of toads,Knot,Music,"Who Gave Us A ""Beatnik Fly"" And A ""Rocking Goose""",Johnny & The Hurricanes,Art & Literature,"Who Wrote The Novel Jaws, That Was Later Turned Into A Blockbuster Movie By Steven Spielburg ",Peter Benchley  +General,The Blue Ridge Mountains lie in Virginia and which other state,Georgia,Food & Drink,What Is the Main Ingredient Of The Liquor Mead ,Honey ,General,What is the fear of hell known as,Stygiophobia +Sports & Leisure,What Do The Initials TT Stand For In Regard With The Isle Of Man Motorcyle Race? ,Tourist Trophy ,General,What is the state bird of Wisconsin,The Robin,Entertainment,To gradually decrease in volume.,Decrescendo +General,"Who said ""In my free time I do differential and integral calculus""",Karl Marx,General,What is john wayne's real name,Marion morrison,General,In which country is the Lazio region,Italy +General,Ouranophobia is a fear of ______,Heaven,General,What flower produces pink and white flowers in alkaline soil,Hydrangea,General,"Scientific study of old age, emphasizing the social and behavioral aspects of aging",Gerontology +General,As what is sulphur also known,Brimstone,General,What's the term for a configuration of stars,Constellation,General,What was the first food consumed on the moon in Apollo 11,Turkey +General,Who was the male lead character in Gershwins musical that featured the song Summertime,Porgy,General,"What Broadway show went through 2,488 pounds of yak hair between 1982 & 1995",Cats,Sports & Leisure,Baseball: The Texas ______?,Rangers +General,What does barley become when prepared for brewing,Malt,General,Old Joe was the name of what on where,Camel on Camel Cigarettes,Art & Literature,What is the second best-selling book of all time ?,Quotations from the Works of Chairman Mao Tse-Tung +General,What minty confection is a boys name - in reverse,Trebor mints - Robert,General,To which country do the Galapagos Islands belong,Ecuador,General,Which two metals are alloyed to make pewter,Tin and Lead +General,What is a cpu,Central processing unit,Music,Which Pioneer Punk Band Signed To EMI's United Artists Label On The Day Elvis Presley Died,The Buzzcocks,General,Dr Ludwig L Zamenhof invented what 1887 Poland,Esperanto +Sports & Leisure,Tony Drago is a snooker professional from which country? ,Malta ,General,What is the most common surname among the Hmong people of Laos,Vang,General,What song's words were changed and then published in 1935 as 'happy birthday to you',Good morning to you +General,The use of astronomical phenomena to predict earthly & human events is called,Astrology,General,33% of American women lie about what,Their Weight,General,The Lombard League was a mediavel coalition of cities in which European country,Italy +General,Name the female British climber while killed trying to climb K2 in 1995,Alison hargreaves,General,Who worked for dr zorba,Ben casey,General,Who wore laceless shoes and was said to regard undone shoelaces as unlucky,Anthony perkins +General,The amount of what substance in granite determines its color,Feldspar, History & Holidays,Which French king was known as the Sun King ?,Louis XIV,Food & Drink,How would you say 'house wine' in 'German' ,Hauswein  +Music,"Who Had A Hit In 1993 With ""She Don't Let Nobody""",Chaka Demus & Pliers,General,What organization did Benjamin Chavis take over as head in 1993,Naacp,General,Who sent her taped conversations with Monika Lewinsky to Kenneth Starr,Linda tripp +General,Who released the No.1 hit single 'Barbie Girl' in October 1997,Aqua,General,"The Anglo Nubian, Toggenburg and Murcian are breeds of which animal",Goat,Food & Drink,Which famous French chef invented the Peach Melba in 1893? ,Auguste Escoffier  +Music,With Which Band Is Raul Malo The Lead Singer,The Mavericks,General,What are the rhea and cassowary both types of,Flightless birds,General,In which country would you find Lake Disappointment,Australia +General,12 is the atomic number of which metal,Magnesium,General,What is the fear of lakes known as,Limnophobia,General,Oneirology is a study and interpretation of what,Dreams +General,What does a zoologist study,Animals,Entertainment,Who played the president of the U.S in 'Air Force One'?,Harrison Ford,General,What food was sent to England in WW2 as part of lease lend,Spam +General,What does a linguist study,Languages,General,Which is the only living bird with two toes on each foot,Ostrich,General,Where are chinese gooseberries from,New zealand +General,"What rock makes the cheapest, softest form of sandpaper",Flint,General,The Louvre Museum - Palace - but what was it first,Fort,Art & Literature,Which Character Is The Most Famous Creation Of James Matthew Barrie? ,Peter Pan  +General,"In Greek mythology, for who did the cyclops forge thunderbolts",Zeus,General,Peter pan rescued what indian princess from the claws of captain hook,Princess tiger lily tiger lily,Sports & Leisure,Who Has One The Most Mens Singles Tennis Titles ,Pete Sampras  +General,Hans Lippershey made the worlds first practical what,Microscope,General,"Basking, nurse and whale are all types of which animal",Shark,General,What language is most widely spoken in Iran,Persian +Music,Who Is Known As Lady Soul,Aretha Franklin,Science & Nature,What is the formal name for when a substance breaks down on heating ?,Thermal Decomposition,General,What is the most widely used seasoning,Salt +General,Which department uses 65% of all paper bought by US gov,Defence department,Sports & Leisure,Which Avid Everton Fan Won His Only Snooker World Title In 1991? ,John Parrott ,Science & Nature,"What bird is an excellent swimmer, but can't fly",Penguin +General,What is the flower that stands for: betrayal,Judas tere,General,What nationality was the last person executed by guillotine,Tunisian,General,National capitals: Honduras,Tegucigalpa +Sports & Leisure,What Events Are Involved In A Biathlon ,Cross Country Skiing & Shooting ,General,Which eighties musician got sued by a music related company for using their name as part of his pseudonym?,Thomas Dolby,General,The title of which Moody Blues album is a mnemonic to remember the lines of the treble stave of a sheet of music,Every good boy deserves favour +General,What is the flower that stands for: avarice,Scarlet auricula,General,Gail Borden invented what food item,Condensed Milk, Geography,What is the capital of Senegal ?,Dakar +Geography,____________ possesses more proven oil reserves than any country outside the Middle East.,Venezuela,General,Woolworth's - the 5 /10 cent store started in which us state 1979, Pennsylvania,General,Eonism is what sexual practice,Cross Dressing +Music,"Who Won An Oscar For His Portrayal Of Broadway Legend George M Cohan In ""Yankee Doodle Dandy""",James Cagney,Science & Nature,To which order of mammals does the beaver belong? ,Rodents ,General,What are the initials for deoxyribonucleic acid,Dna +General,Name the only major Greek God whose Roman counterpart has the same name,Apollo,General,In what language did St Paul write his epistles,Greek, History & Holidays,Who was the last to sit on the peacock throne ,Shah mohammed reza pahlavi  +Music,Ebony & Ivory Was A Hit For Which Famous Duo,Paul McCartney & Stevie Wonder,General,License Plates: What does POVALT sell for a living,Sports equipment,General,Brontophobia is the fear of _______.,Thunder +General,Where can you buy a cup of coffee with cruzeiros,Brazil,General,"Which Actor Played The Role Of Rocker Jerry Lee Lewis In The 1989 Biopic Of His Life Entitled ""Kissing Cousins""",Dennis Quaid,General,Hari-Kari is vulgar name Seppuku - what's it literally mean,Belly Splitting +General,Alfred Jingle appears in which Dickens novel,The Pickwick Papers,General,Who had poachers castrated,Richard the lionheart,General,If You Suffered From Megamalagaphobia What Would You Be Very Concerned About Loosing,An Erection + Geography,What is the largest island in the Indian Ocean?,Madagascar,General,What are the first three words of the bible,In the beginning,General,What 1995 movie was initially banned in malasyia because pigs are offensive to muslims,Babe +General,A pound of armadillo meat contains how many calories,780 calories,General,What was the world's principal Christian city before it fell to the Ottoman Turks in 1453,Constantinople,General,Who is the greek counterpart of hercules,Heracles +Science & Nature, A wolf's odor detecting ability is __________ times greater than man's.,100,Geography,What island is known as the Spice Island,Zanzibar,General,"Which famous film actor, who died of lung cancer in 1957, used his real name but dropped his middle name of de Forest",Humphrey bogart +General,On what sea is the crimea,Black sea,General,By who was gerald ford almost assassinated,Squeaky fromme,Music,Who Got Booed Off The Stage At The Bob Dylan 30th Anniversary Concert In Madison Square Garden In 1992,Sinead O Connor +General,Whose boat Bluebird was recently raised from Coniston water,Donald Campbell, History & Holidays,Where was Nelson mandela in prison?,Robben Island,General,What date is the 'Ides' of March?,March 15th +General,Lee Where were the Toltecs from,Mexico,General,Josip Broz became famous as who,Marshal Tito,General,What famous racehorse is the grandfather of sea biscuit,Man of war +Music,What note is written in the space above the bottom line of the treble clef?,F,General,Which fungal plant disease particularly affects brassicas,Club root,General,In which national park is the mauna loa volcano located,Hawaii volcanoes +General,What myth is the rhinocerous thought to have inspired,Unicorn,Music,Name A Flock Of Seagulls Top Ten US Hit,I Ran (So Far Away),General,Grande and Chico are versions of what Spanish activity,Flamenco dancing +Music,What Was The First Top 10 Hit For The Temptations Reaching No.3 In 1969,I'm Gonna Make You Love Me,General,Hell hath no fury like ______,A woman scorned,General,What Patsy Cline song put LeAnn Rimes in the limelight,Blue +Music,"From Which Country Did The Dance, The Quadrille, Originate",France,Entertainment,"What was the name of Luke's strange little advisor in ""The Empire Strikes Back""?",Yoda,General,Where is the cordon bleU.S.chool of cooking,Paris +Geography,"If its 4:00pm in Seattle Washington, what time is it in Portland Oregon",4,General,What is the official language of Liberia,English,General,Apis Mnevis Onuphis 3 names for the sacred what of Egypt,Bull +General,Lockjaw is another name for which disease,Tetanus,General,Who got his 100-meter dash gold medal stripped away due to to steroid use in the 1988 Olympics,Ben Johnson,Music,"Who Had A Uk No.1 In January 1975 With The Track ""January""",Pilot +Science & Nature,What two colors is a magpie ,Black and white ,General,Who wrote 'cruel shoes',Steve martin,Music,"Who Had A One Hit Wonder With ""Uptown Top Ranking""",Althia And Donna +General,What is Moscow's famed opera house,The bolshoi bolshoi,Technology & Video Games,Who invented Tetris? ,Alexey Pazhitnov,General,Adjustable fabric roof of a car,Drophead +General,The pound cake got its name from the pound of ______ it contained,Butter,General,"Who composed the musical ""Annie Get Your Gun""",Irving berlin,General,"In the tv series 'seinfeld', who does michael richards play",Kramer +General,The town of Beersheba is in which country,Israel,Sports & Leisure,What Nationality Is Tennis Player Michael Chang? ,American , History & Holidays,Who were the first people to be elected into the Aviation Hall Of Fame?,The Wright Brothers + Geography,What country borders Libya on the East?,Egypt,Science & Nature,What Colour Is The Beak Of A Mature Mute Swan ,Orange ,General,Jim Bakus supplied the voice of which cartoon character,Mr Magoo +General,In what Australian state would you find Port Pirie,South australia,General,Who did Rocky Marciano defeat to first take the title in 1952,Walcott,General,______ is the brand name of Morphine once marketed by Bayer,Heroin +General,What sporting event first took place in 1903,Tour de france,Music,"Who Had A Hit With ""A Baby Named Sue"" In 1969",Johnny Cash,General,What note is placed on the centre line of a treble clef,B +General,By US government figures people have tried 28000 ways of what,Losing Weight,General,Eidology is the search for what,Existence of Ghosts,General,How long is a swimming race that covers sixteen lengths of an olympic pool,800 metres +Music,"Who Is The Lead Singer Of The Band ""The Police""",Sting,General,What did the shire's reeve become when the concept was brought to the u.s,Sheriff,General,Band to keep up stockings,Garter +General,Who holds the nhl record for the most goals scored during a regular season,Wayne gretzky,General,What cartoon character was born April 1st 1980,Bart Simpson,General,What's the highest mountain in Africa?,Kilimanjaro +General,What type of number has no factors other than 1 and itself,Prime,General,What Australian Prime Minister drowned near Melbourne,Harold Holt 17 12/1/1967,Food & Drink,"What are bigoli, farfalle, rigati and pansotti? ",Pasta Shapes  +Geography,In which state is stone mountain ,Georgia ,General,Who managed to win Atlanta by dropping three golden apples,Hippomenes,Food & Drink,What spirit is added to brandy to make a sidecar?,Cointreau or Triple Sec +General,Agrippa poisoned her husband/uncle who was he,Claudius,Science & Nature," With few exceptions, birds do not sing while on the ground. They sing during flight or while sitting on an object off the __________",Ground,General,Which actor wrote the book The Outlaw Trail,Robert Redford +Music,What Was The Beat's Second Chart Success Achieving No.9 In Feb 1980,"Hands Off, She's Mine",Entertainment,Actor: _______ Borgnine.,Ernest,General,Margaret Hookham changed her name famed as who,Dame Margot Fonteyn +General,In what country could you spend a tugrik,Mongolia,General,An average American eats 28 what in their lifetime,Pigs,General,Which opera/musical is set in Catfish Row,Porgy & bess +Geography,"What island group contains jersey, guernsey, sark and herm ",The channel islands ,Music,Which band had a 1994 number one with “Cotton Eye Joe”,Rednex,General,What was the first doctor film made in 1954,Doctor in the House +General,"Why, in 1969, did the French flag appear on some British postage stamps",To commemorate concorde's maiden flight,General,St patrick the patron saint ______,Ireland,General,"What major does david bowie's ""space oddity"" refer to",Major tom +General,How was Australias most infamous bush ranger,Ned kelly,General,"Whose films include 'giant','written on the wind' and 'a farewell to arms'",Rock hudson,General,What was the name of Murphy Brown's news program?,FYI +Science & Nature," When young, the hoatzin, a crested, olive_colored South American bird, has claws on its __________",Wings,General,What is opened somewhere every 4 seconds,Can of Spam,General,What actor heartthrob was born Michael Shalboub in 1932,Omar sharif +General,A spice from which root is used to give food a yellow colour,Turmeric,General,Fire bellied Spadefoot and Midwife are all types of what,Toads,General,Who first appeared in All Star Comics in 1941,Wonder Woman +General,Legal Terms: A supplement to a will.,Codicil,General,What does the 'catchfly' plant feed on,Moths,General,"From where does the expression ""thin as a rail"" originate",Bird +General,Who is the only musician in history to bring out two albums to reach number one in one year?,DMX,General,What brought Frosty the Snowman to life,An old silk hat,Science & Nature,What is a young goose called,Gosling +General,What country in distance is furthest from New Zealand,Spain,General,Names for numbers: 1 followed by 42 zeroes =,Tredecillion,General,In which city was Bob Hope born,London (Eltham) +General,"In Greek mythology, which monster had the head and breasts of a woman, the body of a lion and the wings of a bird",Sphinx,General,"Who (boxer) appeared in 1962 in the film ""Requiem for Heavyweight""",Muhammad ali,General,Anubis was the egyptian god of the ______,Dead +Entertainment,"What was the name of the restaurant the TV series ""Happy Days""?",Arnolds,General,Russian country cottage,Dacha,General,Mount Mitchell is the highest peak in which mountain range,Appalachians +General,What was the pre-war German name for the Baltic city which is now Russian and called Kaliningrad,Konigsberg,General,What does the body release that dilates small blood vessels and so causes a person to blush,Peptides,General,Where are a whales nipples,On its back +General,What is sometimes known as Goober Grease,Peanut Butter,Geography,The only river that flows both north and south of the equator is the _____________. It crosses the equator twice.,Congo,Music,Which heavy-metal guitarist provided the soundtrack to the film Death Wish II?,Jimmy Paige +General,On which river is Warsaw to be found,Vistula wisla,General,What is the former name of Botswana,Bechuanaland,Sports & Leisure,Which Black American Athlete Burst Hitlers Aryan Party Bubble In 1936 By Winning 4 Gold's ,Jesse Owens  +General,What animal always gives birth to same sex twins,Armadillo,General,Practice of driving recklessly in a stolen car,Hotting,General,"Agency of the United States government, generally responsible for administering federal policy for Native Americans and Inuits (Eskimos)",Bureau of indian affairs + History & Holidays,Dr. Christian Barnard performed the world's first ever what in South Africa in 1967. ,Heart Transplant ,Music,What Where The Christian Names Of The Everly Brothers?,Don & Phil,General,What animal stands for the longest period,African Elephant over 50 years +General,"Who sang the Song ""Gangsta's Paradise""?",Coolio,General,Where is bond street,London,Toys & Games,"In which sport or game are the terms: 'pin', 'fork', and 'skewer' used",Chess +Geography,"Can you give me the capital of the following three countries: Syria, Morocco and Libya? ","Damascus, Rabat, Tripoli ",General,What sport was called Harpastum by the ancient Greeks,Football,General,What is Papins Digester - Invented Denis Papin 1679,Pressure Cooker +Music,Pete Burns From Dead Or Alive Was Spun Around Like A What,Record,Geography,Which country uses a Lek as a unit of currency? ,Albania ,General,Which country has the most emigrants,Mexico +General,The temperature at what dew forms is called the,Dew point,General,For what song did country and western singer marty robbins win a grammy,El,General,What was the first complete symphony to be recorded,Beethoven's fifth +General,How many children did Adam and Eve have together,Three,General,How many spokes does an umbrella have,Eight, History & Holidays,Who Released The 70's Album Entitled Broken English ,Marianne Faithfull  +General,In the USA domestic violence peaks on what day of the year,Superbowl Sunday, Geography,What state borders Alabama to the north?,Tennessee,General,Who sailed to the new world in 'the mayflower',Pilgrims +Sports & Leisure,How many balls are on a pool table at the start of a game? ,16 ,Science & Nature,How Many Teeth Does A Healthy Adult Human Have ,32 ,General,Loose thick usually cotton trousers worn for sports or leisurewear,Sweatpants +General,What is the eighth month of the year,August,General,In what tv series did henry winkler play arthur fonzarelli,Happy days,General,A magnum of champagne is how many litres,1.5 +Science & Nature,Which two metals make up brass? ,Copper and Zinc ,Music,Where Was Lola A Showgirl,Coca Cabana,General,"What was the name of Wagner's last Opera, first performed in 1882",Parsifal +Science & Nature,What Is Another Name For The Wildebeest? ,A Gnu ,General,The Savannah was the worlds first commercial what,Atomic powered ship,General,A position on the tip of the toes.,Point +Science & Nature,This protein makes the blood red in color.,Hemoglobin,General,Who was the female Prime Minister of England throughout the eighties?,Marget Thatcher,General,What is it against the law to kill in Pacific Grove California,Butterflies – local ordinance 352 +General,"Who stars in the movie ""Tomb Raider""?",Angelina Jolie,General,What is the troposphere lower than,Stratosphere, Geography,What is the capital of Monaco ?,Monaco +General,Who wrote Fahrenheit 451,Ray bradbury,General,A sheep duck and rooster were worlds the first what,Passengers in hot air balloon,General,Before Becoming An Actor What Was Bob Hoskins Profession When He Worked In The Circus,A Fire Eater +General,A flat round soft creamy French cheese,Brie,General,Al Capone's business card identified him as a what,Furniture dealer,General,"On international automobile license plates, what country is represented by the letter E",Spain +Food & Drink,Who released the following 'edible' album 'Pretzel logic' ,Steely Dan ,General,The name of which language means off the coast in Arabic,Swahili, History & Holidays,Who met in Yalta in 1945 (in alphabetical order)?,Churchill Roosevelt Stalin +General,What is the most southerly County of Ireland,Cork,Entertainment,Who was the Hulk's first friend?,Rick Jones,General,Odin owned Geri and Freki what were they,Wolves +General,A chronic disease of the liver,Cirrhosis,Music,Whose Autobiography Was Entitled Moonwalk,Michael Jackson,General,Olfactophobia is the fear of,Smells +General,"In Shakespeare's 'Merchant of Venice', with whom does Portia fall in love",Bassanio,General,Who (not Peter Sellers) played Inspector Clouseau in 1968,Alan Arkin,Food & Drink,"Which drink, which has been illegal but is being imported once again, is made from the leaves of the wormwood plant? ",Absinthe  +Music,"With Just ""Paul McCartney"" On The Label As Artist Name His First No.1 In 1984",Pipes Of Peace, History & Holidays,"Which Christmas song contains the line 'Oh, the weather outside is frightful, but the fire is so delightful'' ","Let it Snow, Let it Snow, Let it Snow ",General,Whose quartet provided the musical accompaniment to the Goon Show,Ray ellington +General,"The Graf Zeppelin completed a 19,500 mile trip in what year",1929,General,What is a group of plovers,Congregation,General,What is the boiling point of water on the fahrenheit scale,212 +General,The state tree of Arizona is really a legume - name it,Palo Alto,General,Which is the worlds busiest metro system,Moscow,General,In 'the shining' what was the child's imaginary friend's name (the one who told him things that were going to happen),Tony +General,The locals call it Misi what do we call this country,Egypt,General,Who was the only unidentified person awarded the Victoria Cross,US unknown soldier,General,Who played the 'universal soldier',Jean claude van damme +Sports & Leisure,Which Football Team Play Their Home Games At Stamford Bridge ,Chelsea ,General,What is the name given to the young of 'dragonflies' and 'damselflies',Nymphs,General,What animal is found on the flag of Sri Lanka,Lion + History & Holidays,Godzilla first made his mark so to say in 1954. What do the Japanese call Godzilla ? ,Gojira ,Music,"Who Sang Vocal On Sub Sub's Hit ""Aint No Love, Aint No Use""",Melanie Williams,General,What were H47 and L12 that collided causing deaths in 1920s,Submarines +General,Which Singer Of A Very Famous 90's One Hit Wonder Has The Real Name “ Robert Van Winkle ”,Vanilla Ice,General,Between what ages is a brandy or port described as VSOP,20 to 25 years,General,"What is the more popular name for the flower, Calendula",Marigold +General,The FIS governs what sport,International Ski Federation,General,How many children did noah have,Three,General,The first steam engine arrived in america in this year,1753 +General,"""Fruit smack flavored syrup"" was the original name of",Kool aid,General,"What legendary us magazine publisher was born in tengchow, china",Henry luce,General,How many books are in the old testament of the holy Bible,39 +General,Texans consume 40% of farm grown what in the USA,Catfish,General,In Latin mala means bad it's also a favourite Roman food what,Apple,General,Five u.s states border which ocean,Pacific ocean +General,Vivaldi's concertos Opus 8 Numbers 1-4 better known as what,The four Seasons, History & Holidays,What are male witches called ,Warlocks ,General,In which city would you find The Blue Mosque,Istanbul +General,How Did Louis Washkansky Of South Africa Make History In 1967,1st Heart Transplant Recipient,General,"According to Enrico Caruso, ""It is a good fruit. You eat, you drink, and you wash your face"". What fruit was he talking about",Watermelon,General,"Relating to food, what are 'loquats'",Fruit +General,"When someone is clumsy or awkward, especially with their hands, they are often said to be ""all ___."" These",Thumbs,Sports & Leisure,"Which NFL team's defensive unit was nicknamed ""The Purple People Eaters""",The minnesota vikings,General,Who won the Oscar for Best Director for the film The Deer Hunter in 1978,Michael cimino +Science & Nature," From crocodile farms, Australia exports about 5,000 crocodile skins a year. Most go to Paris, where a crocodile purse can sell for more than __________",10000,General,Which U.S. biologist has published collections of essays entitled Bully for Brontosaurus and Dinosaur in a Haystack,Stephen jay gould,General,George Bush removed what from the White House menus,Broccoli +Science & Nature,"What links flamsteed halley, bradley and bliss ",Astronomy ,General,"Nuclear membrane, cytoplasm, & nucleus are parts of a ______",Cell,General,"What animal, other than humans, can get leprosy",Armadillos +General,To which part of the body does the adjective 'renal' refer,Kidney,Music,Where Was Lionel Richie Dancing According To His 1986 Hit?,On The Ceiling,General,When was julius caesar murdered,Ides of march +General,Which annual sporting event between 2 teams started in 1829,The University Boat Race,General,"Name the Italian President of the Christian Democrats and five times Prime Minister, who was kidnapped and murdered by the Red Brigade guerrillas in 1978",Aldo moro,General,"What country did the king in ""The King and I"" rule",Siam +General,Archbishop Makarios of Cyprus was exiled in 1956 to where,The Seychelles,General,Whose autobiography is called Where's the Rest of Me,Ronald reagan,General,When was the first toothbrush with bristles invented,1498 +General,-isms: Indifference to pleasure of pain; Greek philosophical system following the teachings of Zeno?,Stoicism,General,The Riksdag is the parliament of which country,Sweden,Technology & Video Games,What does FTP stand for?,File Transfer Protocol +General,"Which Danish philosopher, wrote The Concept of Dread and later had his views used as the basis for existentialism",Soren kierkegaard,General,What's unusual about Ernest Vincent Wrights 50000 word novel,No letter E, History & Holidays,Who played the title role in The Abominable Dr Phibes ,Vincent Price  +General,The Kung San people live in what area of Africa,Kalahari Desert in Botswana, History & Holidays,Amityville House On The Hill' was sung by who in 1986 ,Lovebug Starski ,General,What name is given to those days which have equal hours of daylight and darkness ?,Equinox +General,What is the flower that stands for: delay,Eupatorium,Music,"Who Produced The Album ""A Christmas Gift To You"" Featuring Seasonal Favourites Sung By His Labels Star Acts",Phil Spector,General,Which strait separates Tierra del Fuego from mainland South America,Magellan's strait +General,"Who composed the opera ""A Village Romeo and Juliet""",Frederick delius,Religion & Mythology,In Norse mythology who was Odin's blood-brother?,Loki,General,Besides Play Away Brian Cant Was The Narrator Of What Classic Tv Programme,Camberwick Green Or Trumpton + History & Holidays,"What Connects Kenny, Everett, Annie Lennox, And Sir Isaac Newton ",Born On Xmas Day ,General,A wading bird with long upturned bill,Avocet,General,"The Merry Man and his Maid', is the alternate title for which Gilbert and Sullivan operetta",Yeoman of the guard +Science & Nature,How many tentacles does a squid have,Ten,Mathematics,A line drawn from an angle of a triangle to the mid_point of the opposite side is a(n) _______.,Median,Sports & Leisure,Which football team are nicknamed the Swans ,Swansea  +General,William Herschel astronomer was a musician what instrument,Organ,Music,Four Letter Word Was A Hit 1988 But Who Sang It,Kim Wilde,Art & Literature,What publication was subtitled The What's New Magazine?,Popular Science +General,What got its name from a bridgeport pie company's name and light-weight pie tins,Frisbees,General,What did the Oshkosh steamer win,First automobile race,General,"What Beatles album spent the longest time atop the charts, at 15 weeks",Sgt. +General,Which character is the narrator of Melville's Moby Dick,Ishmael,General,"By what name, is the entertainment business, is Victoria Adams better known",Posh spice,General,"The first mass production began in 1808, what was the product",Wooden pulley blocks + Geography,Into which bay does the Golden Gate Strait lead?,San Francisco Bay,General,A triangle with two equal sides is called __________,Isosceles,Sports & Leisure,What Is Awarded To A British Champion In Boxing ,A Lonsdale Belt  +People & Places,Who Was The First man To Set Foot On The Moon ? ,Neil Armstrong ,General,What name was given to the period during the French Revolution when 1400 of Robespierre's opponents were executed,Reign of terror,Music, Which group had a number 1 in 1966 with 'Keep on Running'?,The Spencer Davis Group +General,"What lake is the world's largest, having an area of almost 394,000sq km",Caspian sea,General,In the Dr Dolittle stories what type of creature was Gub-Gub,Pig,General,Who was the female lead in The Shootist,Lauren Bacall +General,What was Kevin Bacon's first big hit,Footloose,General,What Would You Be Suffering From If You Had The Medical Condition Kyphosis?,Hunchback,Music,"Which American Songwriter Co Wrote Abba's ""Ring Ring""",Neil Sedaka +General,For who was the play 'peter pan' exclusively written,Children,General,Why did Ghengis Khans soldier ride female horses,To drink their milk,General,Who wrote m°a°s°h,Richard hooker +Food & Drink,"Italian dish consiting of olives, anchovies, salami, celery, and appetizers.",Antipasto, History & Holidays,Which 1976 film starring John Travolta is about a persecuted schoolgirl with psychokinetic powers? ,Carrie ,General,Which detective was played by Robert Stack on TV and by Kevin Costner in a film,Eliott ness +General,Bond: What is Goldfinger's first name,Auric,General,The lowest country in the world is?,The Netherlands, History & Holidays,"Often Played At Halloween, Which Song Was A Hit Gor Bobby Boris Picket & The Crypt Kickers ",Monster Mash  +General,Fulton John Short - Later John Fulton first US to do what,Qualify as Matador,General,Who was the author of 'Of Mice and Men',John Steinbeck,Sports & Leisure,In Which Year Did Synchronised Swimming Make It's Debut At The Olympics ,1984  +Science & Nature,The Sun is how much more dense than water?,1.41 times,General,"Agricultural and industrial region in the state of New South Wales, Australia, about 160 km (about 100 mi) north of Sydney.",Hunter valley,General,What chess piece is usually valued as 5 points,Rook +General,"Who sang the song ""Cry"" which was the first video on Mtv to use morphing technology through the entire video?",Godley and Creme,Music,Who Produced USA For In Aid Of Famine Relief In 1985,Quincy Jones,Science & Nature,The force perpendicular to the surface of an object which counters the gravitational force?,Normal Force +General,How many medals did the USSR win at the 1984 Olympics,None,Toys & Games,What bowling term means three straight strikes?,Turkey,General,Who was the 1990 wimbledon women's singles runner up,Zena garrison + History & Holidays,The second line of which carol is '… When they are both full grown'' ,The Holly & The Ivy ,Music,"Who Had A Minor Hit With ""Lonely Man Theme"" In 1960",Cliff Adams,General,Member of a fraternity for mutual help with secret rituals,Freemason +Entertainment,Who played Charlie in Charlies Angels?,John Forsythe,General,"Which pop star appeared in many of the 'gidget' films, and hit no 3 with the calliope sound of 'goodbye cruel world' in the early 60's",James darren,General,Politicophobia is the fear of,Politicians +General,To which family does the coffee plant belong,Madder, Geography,In which state is Mount McKinley?,Alaska,Food & Drink,How is steak cooked if cooked blue? ,Very Rare  +General,Who designed the first Iron ship the Great Britain in 1845,I. Kingdom Brunel,Geography,Which Bridge That Opened In 1988 Connected Japans 4 Main Islands By High Speed Rail For The First Time ,The Seto Ohashi Bridge ,Food & Drink,Cocktails: Vodka and Kahlua make a ___________.,Black russian +General,"Who won an oscar for her role in ""gone with the wind""",Vivien leigh,General,Of what do earthworms have five,Hearts,General,"Link Aurora Texas, Spitsbergen Norway and Ubatuba Brazil",UFO crash sites +General,Who played Lofty in Eastenders,Tom watt,General,"Modern ballroom dance, of Argentinian origin",Tango,General,What is the nickname for Kentucky,Bluegrass state +Music,How Are Craig And Charlie Reid Better Known?,The Proclaimers,General,"Who wrote the fantasies ""Popcorn"" and ""This Other Eden""",Ben elton,General,Who kissed the girls and made them cry,Georgie porgie +Music,Bandleader James Last Started Out Playing Which Instrument,Double Bass,Technology & Video Games,What alternate dimension does Link find himself in in Majora's Mask? ,Termina,General,Who is first of the 7 dwarfs alphabetically,Bashful +People & Places,What is the singer Meat Loafs Real Name ,Marvin Lee Aday ,General,What unusual flavour did the Jell-O company try in 1942,Cola,General,What fraction of an iceberg shows above water,One ninth +General,Life Love Cows French translation of which film,City Slickers,Music,"Which Group Had A Hit With ""Mmmm Mmmm Mmmm Mmmm""",The Crash Test Dummies,General,U.S. captials Maryland,Annapolis +General,What does breaking the sound barrier cause,A Sonic Boom,General,What is the fear of poverty known as,Peniaphobia,General,What is the main ingredient of sauce Lyonnaise,Onions +Religion & Mythology,"What was the town that ancient Greeks believed to be the centre of the world, and was the home of a famous oracle?",Delphi,Music,Who was the lead singer of Big Brother and the holding Company?,Janis Joplin,Science & Nature,What name is given to the point where a river starts ?,Source +General,Which small republic makes up Yugoslavia with Serbia,Montenegro,Sports & Leisure,In what sport is the Heisman trophy awarded?,American football,Science & Nature,What fruit is considered to be the most nutritious ,Avocado  +Science & Nature,What Is The Largest Key On A Standard Computer Keyboard ,The Space Bar ,General,Pitches with what is charcoal and saltpetre mixed to make gunpowder,Sulphur,General,"In horse racing, what is the maximum age of a filly",Four years +General,What Event Occurs At Walthamstow Stadium?,Greyhound Racing,General,Which word is used to describe a fin on a fish's abdomen?,Ventral,General,What u.s state has no telephones in 12% of its households,Mississippi +Music,Which Letter Of The Alphabet Do The Sound Holes On Violins Resemble,The Letter 'S',Science & Nature, The average giraffe's __________ is two or three times that of a healthy man.,Blood pressure,General,What did the ancient Greeks call the fear of woods and forests,Panic from god Pan +General,In Norway what is a brisling,A Sprat,Geography,Which Is The Largest State In America ,Alaska ,General,Stranger in what prehistoric kingdom does 'alley oop' live,Moo +Music,"In the world of music, what job did Simon Fuller get sacked from in 1997?",Manager of the Spice Girls,Science & Nature,"In 1851, The Largest Building The World Had Ever Seen Was Erected In London What Was It? ",Crystal Palace ,General,What US state has the Worlds Champion Chili Cookoff every year?,Texas +General,What does a hyperphiliac suffer from,Great desire for sex,General,What was James Deans middle name,Byron,Music,Which Rock Superstar Once Appeared In Front Of An Audience Of 53 People,Bob Dylan +General,In what modified vegetable did Cinderalla travel to the ball in?,Pumpkin,General,What was another city that the usfl Boston Breakers eventually played in,New orleans,General,"Copernicus what is new, last or gibbous",Moon +General,"Which Classic TV Show Had A Theme une Entitled ""Quiz Wizard""",Blockbusters,General,What is the Capital of: Korea North,P'yongyang,General,Mastigothymia is sexual arousal from what,Flagellation whipping +General,Which is the longest river - not river system - in the U.S.A.,The missouri,General,Which painter spent last 4 years of his life on the run for murder,Carravagio for murder in Rome,General,Nadsat was a made up language in what book and film,A Clockwork Orange +Food & Drink,Food That Is Permitted To Be Eaten Under Jewish Dietary Laws Is Known As What ,Kosher ,Sports & Leisure,Where were the 1912 Olympics held ?,"Stockholm, Sweden",Sports & Leisure,Who Won The BBC Sports Personality Of The Year Award In 1971 ,Princess Anne  +General,In 1861 Dows Ginger Ale was the first to be what,Sold in a Bottle,Geography,"_________ is the largest country in Africa. It has a population greater than 28,100,000.",Sudan,Music,What Did Sting Suggest You Do If You Love Somebody,Set Them Free +General,"Which US state holds the record for most snowfall in a day, recorded February 7, 1916?",Alaska,Geography,What US state is completely surrounded by the Pacific Ocean,Hawaii,General,Who painted 'the night watch',Rembrandt +General,Russia has one but the US has at least six - what,Places called Moscow,General,Whic country developed the first jet fighter?,Germany,Art & Literature,From which Shakespeare play is this line taken: To be or not to,Hamlet +General,What U.S. state includes the telephone area code 505,New mexico,Science & Nature,Which Breed Of Cat Has Blue Eyes? ,Siamese ,Geography,"If the ___________ River were stretched across the United States, it would run just about from New York to Los Angeles.",Nile +General,In Scotland what was the tawse,Teachers Strap or belt,General, What is a figure with eight equal sides called,Octagon,General,"Even though they broke up 25 years ago, which group still sells more records each year than the rolling stones",Beatles +Music,Which French Superstars Originated My Way,Claude Francois,General,What is Muckle Flugga,Rock and Lighthouse on Uist,General,Who was the FBI's 1st 'most wanted' criminal,John dillinger +General,What number is at 12 o'clock on a dartboard,20,Art & Literature,Name The Famous Greek Writer Of Fables ,Aesop ,General,What word do Alaskan sled drivers shout to move their teams,Hike not Mush + Geography,Which city is known as Motown?,Detroit,General,"Which Football Club Started Out Life As ""Dial Square FC""",Arsenal,Sports & Leisure,Baseball: The Philadelphia ______?,Phillies +General,What is a Chorizo,Spicy Sausage,Sports & Leisure,How Many Players Are There In A Baseball Team? ,Nine ,Sports & Leisure,What Sport Has 4 Letters And Begins With A 'T''? ,Golf!!!!  +General,Who recorded the album Beggars Banquet,Rolling stones,General,What in medical terms does the abbreviation A & E represent,Accident & emergency,Music,Which Singer Did The Family Stone Back,Sly +General,"John Gore, Edward Saunders pioneered what org in Australia",Salvation Army,General,Which planet in our solar system orbits closest to the sun,Mercury, Geography,What state was the home to Mayberry?,North Carolina +General,Catch 22 had what original name - publisher changed it,Catch 18,General,Natalie Horler Is The Lead Singer Of Which Very Pupular Modern Day Band,Cascada,General,In which month of 1940 did the Battle of Britain begin,June + Geography,What is the basic unit of currency for Uzbekistan ?,Sum,Entertainment,How many semitones are there in an octave?,12,Entertainment,Who was the first James Bond?,Sean Connery +Science & Nature,"Which Fish, Previously Thought To Be Extinct Was Rediscovered In 1939? ",The Coelacanth ,General,"Who was ""bonnie prince charles""",Charles edward stuart,General,What 1949 book was a bestseller 35 years later,1984 +General,"In Germany, where would you be if you were in a ""Krankenhaus""",Hospital,Art & Literature,In 2007 Who Topped the Best Sellers Non Fiction List With Born To Be Riled? ,Jeremy Clarkson ,General,What is the first sign of the Zodiac,Aries +General,What was created in Canada in 1923 for first time criminals,A Spanking Machine,General,Which group was formed in 1972 by Don Henley and Glen Fry,The eagles,General,Where On The Human Body Would You Find The Filtrum,The Ridge above The Top Lip +General,In which Mediterranean Sea are the Cyclades islands,The aegean,Science & Nature,What polish astronomer demonstrated in 1512 that the sun is the center of the solar system ,Nicolas copernicus ,Science & Nature," Since white tigers have pigmented stripes and blue eyes, they are not __________",Albinos +General,What colour toothbrush do most people have,Blue,General,In what Australian state would you find Wagga Wagga,New south wales nsw,General,Jill Oppenheimer changed her name to become famous as who,Jill St John – Tiffany Case in Bond film +General,Which fictional park is the home of Yogi Bear,Jellystone,Music,"Who Kept The Discos Humming In 1977 With Their UK Debut ""Boogie Nights""",Heatwave,Music,Who Was Starry Eyed In 1960,Michael Holiday +General,The first American advertisement for tobacco was published in what year,1789,General,"Major in greek mythology, who was visited by zeus in the form of a swan, and became the mother of helen and pollux",Leda,General,Which comic strip hero shares his name with a town in Turkey,Batman +General,Papaphobia is the fear of,The pope,Music,Arthur Christopher Benson Wrote The Words To Which Patriotic Song,Land Of Hope And Glory,General,If you graduate with a degree in music what colour tassel wear,Pink +General,How many US states border an ocean?,23,General,What is a redwood tree,Giant sequoia,General,"What was the master's nickname for caine in ""kung fu""",Grasshopper +General,The Amazon river dolphins are what colour,Pink,General,"The song ""One O'clock Jump"" is particularly associated with whose big band",Count basie,General,What huge firm did Thomas Watson found in 1924,International business machines +General,What is the Islamic equal to the red cross,Red Crescent,General,What sport has Crumb Gatherers Followers Rovers and Wings,Aussie Rules Football,Science & Nature, The average capacity of a pelican's __________ is 12 quarts.,Pouch +General,What is a group of this animal called: Sparrow,Host,General,What new domestic device was launched by Hoover in 1963,Steam Iron,Sports & Leisure,Which City Is Set To Host The 2008 Summer Olympics? ,Beijing  +General,William Tell was the best crossbow archer and what else,Boatman,Sports & Leisure,"Which Is Older, The New York Or The London Marathon ",New York ,General,What two biblical cities did god destroy with fire and brimstone,Sodom and +Sports & Leisure,Loftus Road is the home of which football club? ,Queens Park Rangers ,General,New Jersey has a museum with 5400 exhibits of what,Spoons,General,"According to Benjamin Disraeli, what is the third, & worst kind of lie",Statistics +General,Palmolive promised to help women keep what,Schoolgirl complexion,Entertainment,Composer of the Brandenburg Concerti: J.S. ____,Bach,General,Alpine plant with white bracts,Edelweiss +Sports & Leisure,The Solheim Cup Is The Womans Equivalent Of Whhich Famous Sporting Cup ? ,The Ryder Cup ,General,"What was the profession of J.B.Dunlop, inventor of the pneumatic tyre",Vet,General,Which singer died an unhappy man when his father shot him in 1984,Marvin gaye +Music,What did The Band The Clash Rock,The Casbah,Geography,The United States is made up of __ states.,50,General,What is the Capital of: Zimbabwe,Harare +General,How many ring are there on the olympic flag,Five, Geography,Which island country lies to the East of Mauritius?,Australia,General,What was Jimmy Hoffas middle name,Riddle +General,How long is the le mans endurance motor race,24 four hours,General,Which film star used to be a circus acrobat,Burt Lancaster,General,Bamako is the capital of ______,Mali +General,On which fictional street does 'Freddy Kruger' live out a nightmare,Elm street,General,What American building has double - needed toilets -segregated,Pentagon,General,The song 'Why Do Fools Fall in Love was a hit in which year,1956 +Food & Drink,Which contains more caffeine - coffee beans or tea leaves? ,Tea Leaves ,Mathematics,What is x to the power of zero equal to?,One,Entertainment,"Name the band - songs include ""Monday Monday, California Dreamin'""?",The Mamas and the Papas +General,What does a depilatory remove,Hair,General,Scurvy is a lack of which vitamin,Vitamin c,General,"Who is the mother of Huey, Duey and Louie",Dumbella Duck +General,Leaflike part of a fern or palm,Frond,Sports & Leisure,Who has made more test match appearances for England than any other cricketer? ,Graham Gooch ,General,"Who or what was ""strong to the finish""",Popeye - cos he eats his spinach +Music,"Who Recorded The Early 1990's Indie Classic ""Sheffield Sex City""",Pulp,General,"In the original story, of what were cinderella's slippers actually made",Fur,General,In UK whose private house has its own court and 11 prison cells,Lord Mayor London Mansion House +General,On which Caribbean island are the Blue Mountains,Jamaica,Music,According To The Lyrics Of The B52's Song Love Shack What Was Their Chrysler As Big As,A Whale, History & Holidays,"Which Directors Films Include, Shivers , Videodrome & The Fly ",David Cronenberg  +General,Name Alice's pet cat,Dinah,General,"Which author wrote the four best-selling crime novels known as ""The LA Quartet""",James ellroy,General,Which railway station features at the end of the Hitchcok film 'The Lady Vanishes'?,Victoria Station +Food & Drink,"What drink is a mixture of brandy, egg yolks & vanilla? ",Advocaat ,General,"American cooking expert, author, & television personality",Julia child,General,Who gave the UN the land in NY to build their HQ,John D Rockerfeller +General,Who was the first woman to fly the Atlantic alone ?,Amelia Earhart,General,What does the reference book Crockfords list,Church of England clergy,General,A comet for the Greek Kometes literally means what,Long Haired +General,John Augustus Larson invented what in 1921,Lie detector,General,What is a group of this animal called: Buffalo,Herd,General,Dublin is the capital of _____,Ireland +Sports & Leisure,"How is the chess term ""shah mat"" better known ?",Checkmate, History & Holidays,"Which of these women NEVER married Frank Sinatra? A= Mia Farrow, B= Ava Gardner, C= Lana Turner ",C = Lana Turner ,General,With what country was catherine the great associated,Russia +Science & Nature,What body function is improved if you sleep on your right side?,Digestion,General,What was the subject of the first book printed in England,Chess,General,"As of December 30, 1997, Disney held eight of the top ten spots on the All Time Movie Video Sales Chart. The Lion King (1), Aladdin (2), Cinderella (3), Beauty and The Beast (4), Snow White and the Seven Dwarfs (5), Toy Story (7), 101 Dalmatians (8), and Pocahontas (10). The two non_Disney flicks to make the list _ Forrest Gump (6), and __________ ",Jurassic park +General,"What Mojave Desert city has a name meaning ""the meadows"" in spanish",Las vegas,General,The Salk vaccine is used against what disease,Polio,People & Places,Who Was The First Woman To Fly Solo Accross The Atlantic ? ,Amelia Earheart  +Food & Drink,How Are The TV Chefs 'Simon King & David Myers'' More Commonly Known ,The Hairy Bikers ,General,The boss is also known as ___________,Bruce springsteen,General,What is the main ingredient in an omelet,Egg +General,What nutrient is required by the body in order to build up muscles,Protein,General,In what Australian state would you find Wollongong,New south wales nsw,General,How many basic positions of the feet are there in ballet,Five +General,What is a Texas Ruby Red,Grapefruit,General,In Hartford Connecticut it is illegal to educate what,Your dog,General,"Who raised money for cancer research with there ""journey for lives"" run across Canada",Steve fonyo + History & Holidays,What notorious event occurred on the 24th October 1929? ,The Wall Street Crash or (Black Thursday) ,General,Which highwayman rode the horse Black Bess,Dick Turpin,General,What was Abelard's punishment for his romance with Heloise,He was castrated +General,"What cereal carries the slogan, ""a bowl a day keeps the bullies away""",Apple,Sports & Leisure,Who was disqualified after failing a drugs test at the 1988 Olympic Games In Seoul ,Ben Johnson ,General,What is a cathedra,Bishops throne or chair +General,Where are you if you land at Norman Manley airport,Kingston Jamaica,General,J G Galle discovered it in 1846 - discovered what,The planet Neptune,General,Who began his career as one of the Tennessee Two,Johnny Cash +General,Durbarry is cream of a vegetable soup - what one,Cauliflower,Sports & Leisure,Rhona Martin Was Captain Of Which British Olympic Gold Medal Winning Team In 2002? ,Curling ,General,How is the 15th March also known ?,Ides of March +General,"Which word follows Juliet, Kilo and Lima",Mike, History & Holidays,"In the 17th century, if you were given Christmas Clap off someone, what have they given you? ",A Kiss Under The Misletoe ,Religion & Mythology,What are the first three words of The Bible?,In the beginning +General,What is a gondola,Water taxi,General,"Which Explorers Last Words Were ""I Have Not Told Half"" Of What I Saw",Marco Polo,General,Which Russian composer wrote the opera 'Mozart and Salieri' in 1897,Rimsky-korsakov +General,Which science fiction writer developed a series of ethics for robots known as the Laws of Robotics,Isaac asimov,General,Which actor's autobiography is entitled Dear Me,Peter ustinov,General,Who is known as The father of Poetry,Homer +General,What year was Neptune discovered,1846, Geography,What is the capital of Morocco ?,Rabat,General,"Whose campaign warned, ""a vote for & erson is a vote for Reagan""",Jimmy carter +General,In what Hitchcock film did Shirley MacLaine debut in 1956,The trouble with Harry,Science & Nature,Which Scottish engineer was the first to use horsepower? ,James Watt ,General,Rodney Dangerfield left what career to return to show business,Paint salesman + Geography,Name the capital of Brazil.,Brasilia,General,In Costa Rica and El Salvador you spend what,Colons,General,Which Album Cover Features A Car With The Registration Plate LMW 281F?,Abbey Road +General,What who caught the grinch in the act of stealing the christmas tree,Cindy-lou who cindy lou who,General,With what are toads often confused,Frogs,General,What does a geophage enjoy,Eating earth + History & Holidays,"Whose epitaph reads, free at last, free at last, thank god almighty, i'm free at last ",Martin luther king jr. ,General,Two out of 3 adults in the USA suffer from what,Piles,General,In Shakespeare what is Richard III title before he becomes king,Duke of Gloucester +General,"In 'star trek', what is data's rank",Lieutenant commander,General,Israel occupied the golan heights. whose territory was it?,Syria,Science & Nature,Which Gentle Creature Did Walt Disney Create In 1942 ,"Bambi, The Fawn " +Music,"""He's In Town"" Was A Hit In 1964 For Which British Group",Rockin Berries, Geography,What Scandinavian capital begins and ends with the same letter?,Oslo, Geography,What is the capital of Micronesia ?,Palikir +General,Which word describing chaos first appeared in Milton's Paradise Lost,Pandemonium,Music,Who Took Porcelain Into The UK Top 10 In 2000,Moby,Food & Drink,How Did Spaghetti Alla Puttanesca Get Its Name ,Neopolitan For Prostitute (Hot & Spicy)  +General,Which soul singer was Sittin on the Dock of the Bay,Otis Redding,Science & Nature,Name the smallest breed of dog.,Chihuahua,General,"House of renee what song won the grammy for ""song of the year"" in 1977",You light up my life +General,Who patented the safety razor in 1895,King gillette,General,What type of drink is arabica,Coffee,Music,Who Played The Part Of Bongo Herbert In A Classic 1959 Movie,Cliff Richard +Sports & Leisure,Which Team Has Won The County Cricket Championships The Most Often? ,Yorkshire ,General,A woven fabric with raised pattern,Brocade, History & Holidays,Which Egyptian president ordered the seizure of the Suez Canal in 1956? ,President Nassar  +Art & Literature,"A realistic style of painting in which everyday life forms the subject matter, as distinguished from religious or historical painting. ",Genre painting,Geography,What u.s. state has the largest indian population ,Oklahoma ,General,What is measured in Scroville Units,Heat of Chillies +General,What was the first country to recognise the US as independent,Morocco,Music,Who was the first female DJ on Radio 1?,Anne Nightingale,Science & Nature," Mice, whales, elephants, giraffes, and humans all have seven neck __________",Vertebra +General,Who invented the electrical bass,Leo fender,Food & Drink,What is a segment of garlic called? ,Clove ,General,On which major river are The Owen Falls dam,Nile +General,Who was 'too sexy for his shirt',Right said fred,People & Places,Which British Prime Minister was also the longest serving MP at almost 62 years? ,Winston Churchill ,General,What does a Caligynephobe fear,Beautiful Women +General,What are a group of magpies called,A tittering,General,Myosotis is the Latin name for what blue flower,Forget me Not,General,What's an angle subtended by the diameter of an object called,Angular + History & Holidays,In What Year Was Band Aids 'Do They Know Its Christmas' The UK Christmas No.1 Single ,1984 ,Music,Which Female Star Reached The UK Number 1 Spot 29 Years After Having Her First Hit,Lulu,General,Who is the ghost that walks,Phantom +General,Who invented the Polaroid camera,Edwin land,General,What is 1 days and 2 hours in hours,26,General,In what sport is The Lugano Trophy awarded every 2 years,Men's National Team Walking +General,Ethel who is the spokesperson for the exercise tapes 'tae bo',Billy blanks,General,Where is the wailing wall,Jerusalem,General,An instrument on a car to measure the distance travelled is called a(n) _________,Odometer + Geography,What is the basic unit of currency for Solomon Islands ?,Dollar,Music,CC Deville Was A Member Of Which Band,Poison,General,The Slave of Duty is alternate title what G&S operetta,Pirates of Penzance +General,Tree with a thin peeling bark the twigs also used for flogging,Birch, History & Holidays,In which 1970's films does Dustin Hoffman play the character 'Louis Dega' ? ,Papillon ,General,Which TV detective kept his gun in a biscuit jar,Jim Rockford +Food & Drink,"Creme de Cacao, cream, and brandy make up which type of cocktail ",Brandy Alexander ,People & Places,Which 'S' is the most populated city in the world? ,Shanghai ,General,What city's baseball stadium is known as the coliseum,Oakland +Science & Nature,What animal is depicted on the logo of Toys R Us? ,Giraffe ,General,What is a group of this animal called: Donkey,Herd pace,Science & Nature,"Peat, lignite and bituminous are types of _________.",Coal +General,How many bits was the intel 4004 chip,4 bits,General,Disc of light shown round the head of a sacred person,Halo,General,"Parallel of latitude at 2327' south of the equator, delineating the southernmost point at what the sun can appear directly overhead at noon",Tropic of capricorn +General,Who married Thelma Catherine Patricia Ryan,Richard nixon,Geography,Which is the only city in Cornwall? ,Truro , History & Holidays,With Which Horror Film Would You Associate The Character Of Leatherface ,Texas Chainsaw Massacre  +General,In WW2 the Germans launched operation Bernhard - what,Counterfeit British notes,General,Megalophobia is the fear of,Large things,General,"In ""The Golden Child,"" what object does the Child animate to amuse his captor?",A Coke can +General,What takes place in Happy Valley Hong Kong,Horse racing,General,Which Female Won The Oscar For 'Best Actress' The Year Lady Diana Died,Helen Hunt, History & Holidays,Who Was The 2nd Wife Of Henry Viii ,Anne Boleyn  +General,"What is the name of Mr. Rochester's house in ""Jane Eyre""",Thornfield hall,General,Which actress was the Connecticut state golf champ at age 16,Katherine Hepburn,Science & Nature,What Is The Main Use Of Silver ,Photographic Film  +General,"What is a characteristic of a ""nimbus"" cloud",Rain,General,How did Bunito Mussolini ward off the evil eye,Touch his testicles,General,Kimberlite contains what precious item,Diamonds +Food & Drink,What Dessert Consists Of Ice Cream Covered In Meringue And Baked In The Oven ,Baked Alaska ,General,"In ballroom dance, a characteristic figure that remains constant.",Basic movement,General,When was nelson mandela released from prison,1990 +General,Pluto Greek god of the underworld - what was Plutus god of,Wealth,General,This ugly creature has patches of red on his rear end,Mandrill,General,Why did Ancient Egyptians shave their eyebrows,Mourning death cat +General,Which Salford brewer gave his name to the S.I. unit of energy,Joule,General,The average human body contains enough ____ to kill all fleas on an average dog,Sulfur,Food & Drink,"What is the minimum age for a three star brandy - is it three, five or ten years? ",5 Years  +Music,Which Lucky Man Spent Seven Weeks In The charts With Olivia And Sixteen With Sarah,Cliff Richard (Olivia Newton John) & (Sarah Brightman),Science & Nature, The cat was the symbol of liberty in ancient __________,Rome,Sports & Leisure,Which Football Team Has Won The European Cup The Most Times ,Real Madrid (6 Times)  +Geography,Which Is The Smallest State In The Usa ,Rhode Island ,General,Vientiane is the capital of ______,Laos,Music,Which group had a number one UK chart hit with the same song in 1975 and 1999?,Queen (Bohemian Rhapsody) +People & Places,Who Sued For Libel After Being Described As Boring ,William Roache (Ken Barlow) ,General,What is the fear of standing or walking known as,Stasibasiphobia, History & Holidays,"Name The Event Involving The Anarchist, Peter The Painter? ",The Siege Of Sydney Street  +General,What did Trevor Baylis invent,Clockwork Radio,Science & Nature,What Is The More Common Name For Sodium Chloride ,Salt ,General,Where was miss piggy's birthplace,Hog springs +General,"To send transatlantic telegraph messages, 3,000 miles of cable were laid under the Atlantic in what year",1886,General,What is the seventh month of the year,July,General,When did man first set foot on the moon,1969 +General,A novel by Keith Waterhouse,Billy liar,Science & Nature," A __________ never actually sees the food as it eats, since its eyes are on top of its head and its mouth and nostrils are on the bottom.",Stingray,General,The football huddle originated at what university,Gallaudet +General,What is a female cat called?,Queen,Religion & Mythology,According to popular belief brides walk to the what in the church? (not the aisle),The nave of the church,General,What sausage gets it's name from the Italian for Onion,Chipolata +General,Which nfl's defensive unit was named 'the purple people eaters',Minnesota,General,What was the name of Dagwood Bumstead and Blondies dog,Daisy, History & Holidays,How Many Pilgrims Were Aboard The Mayflower ,120  +General,How many feet are there in a nautical mile,6080, Geography,In what country is The Blue Lagoon?,Iceland,General,What is the preferred reading material for the 66% of americans who admit to reading in the bathroom,Reader's digest +General,What is the full moon nearest the autumnal equinox,Harvest moon,General,What does an accordion have that a concertina doesn't,Keyboard,Music,What Rolling Stones album originally had a 3-D cover in 1967?,Their Satanic Majesties Request +General,One of this groups greatest albums was 'slippery when wet' released in 1986,Bon jovi,General,Lemniscate is the correct name for what symbol,Infinity,General,The term Sesquibicentennial represents how many years ?,250 +General,To which gentleman's club did Phineas Fogg belong,The Reform,Science & Nature,What is the yellow variety of quartz?,Citrine,Mathematics,The square root of 1 is?,1 +General,Approximately how many inches are in a meter,39,General,In cooking where does 'angelica' come from,Plant root,Science & Nature,What protein makes blood red?,Haemoglobin +General,"Suspension of tiny particles of one substance, called the dispersed phase, in another phase, called the dispersion medium",Colloid,General,What is a Tallith,Fringed prayer shawl Jewish,General,Who invented the first chocolate candy,Conrad j van houten +General,What is the flower that stands for: sensitiveness,Mimosa,General,In 1906 the John Gable Entertainer was the first what,Juke Box,Science & Nature, More __________ are raised in California than in any other state in the United States.,Turkeys +General,Sandra Wes of Texas died in 1977 was buried in what,Her blue Ferrari,General,Who won the 1966 f1 championship,Jack brabham,General,In RKO movies what does RKO stand for,Radio Keith Orphium +Science & Nature,What italian astronomer wrote the starry messenger ,Galileo ,General,What author wrote about Adrian Mole,Sue Townsend,General,Who Penned The Auto Biography “ Under No Illusion ” ?,Paul Daniels +General,I'll Never Fall In Love Again' came from which musical,Promises promises,General,Cyesolagnia is a fetish about what,Pregnant women,General,Ranidaphobia is the fear of what?,Frogs +General,Handel's Largo comes from which opera,Xerxes,General,Where could you find a rundle,Ladder - it’s a rung,General,What Is Carol Hersee's Claim To Fame,Test Card Girl +General,"Who Sang Backing Vocals On The Dire Straits Hit ""Money For Nothing""",Sting,General,What are lop cheong,Chinese Sausages,General,Which famous dance of the 1920's took its name from a city In the Southern United States,The charleston +General,What major law was violated in the movie Smokey and the Bandit?,Smuggling beer,General,What does a sphygmomanometer measure,Blood pressure,General,Which film won the best story Oscar in 1958,The Defiant Ones +General,What is the name of the reverend who appears in the cartoon series 'The Simpsons'?,Timothy Lovejoy,General,What band sold the most number of records in the 1970's,Led Zepplin,General,How many blades does a kayak paddle have,Two +General,Actor Arnold Schwarzenegger bought the first Hummer manufactured for civilian use when,1992,General,"Who, in Greek mythology, was condemned to eternally roll a stone uphill, where it continually rolled back down again",Sisyphus,General,In Dads Army What Was The Occupation Of Private Frazier,An Undertaker +Music,Which Trio Released The Album Crazy Sexy Cool,TLC,General,What was the #8 movie (1988) to the end of 1988 with 109 million dollars gross,Indiana jones and the temple of doom, History & Holidays,The four Sundays before Christmas are known by what name ,Advent (Sundays)  +General,What was first used as a medical treatment in 2700 bc by Chinese emperor Shen Nung,Acupuncture,General,Which apollo space mission put the first men on the moon,Apollo 11,General,"Who said ""Pennies do not come from heaven, they have to be earned here on Earth""?",Margaret Thatcher +General,Alfred Wallace coined which phrase - Wrongly given to Darwin,Survival of the Fittest,Geography,Before Brasilia What Was The Capital Of Brazil ,Rio De Janeiro ,General,What sunken ship did U.S. and French scientists discover in 1985,Titanic +General,"Transom, poop and keel are all parts of a what",Boat,Music,On Which Beatles Song Did George Harrison First Play A Sitar,Norwegian (This Bird Has Flown),General,American Paul Theroux wrote novels and about what else,Travel +General,"In The United States Of America How Is ""Feb 2nd"" More Commonly Known",Groundhog Day,General,"Name the lead singer and principal songwriter of the American group ""The Doors""",Jim morrison,General,What is cher's maiden name,Sarkassian +General,What is a two-humped dromedary,Camel,General,Which substance may be added in the preparation of jams to make them set,Pectin, Geography,What is the basic unit of currency for Madagascar ?,Franc +General,The Best or Nothing is the motto of what company,Mercedes-Bentz,General,Who turned down the Bogart role in Casablanca,Ronald Reagan,General,Name one person to be awarded the Victoria Cross twice,Noel chev asse arthur martin leake charles hazitt upham +General,What was the first magazine to publish a hologram on its cover,National Geographic,General,The longest one-syllable word in the English language is?,Screeched,General,The Yellow Kid by Richard Felton Outcault in 1896 first what,Comic Strip – New York World +General,The comma bacillus causes what disease,Cholera,General,"In an average lifetime, the average american is counted in 7 ___",Censuses,General,Why a camel haired brushes (made from squirrel) called camel,Invented by Mr Camel +General,How many spectral colors are in a rainbow,27,General,Eureka - is the state motto of what state,California,General,What do you call a large linear molecule that is formed from many simple molecules,Polymer +General,50% of the US annual rainfall falls in what month,April,Music,Which Group Originally Recorded The Original Of “Light My Fire”?,The Doors,General,A town is not a city until it has a _________,Cathedral +General,What were the dolls in the novel 'valley of the dolls',Pills,Geography,In what country is the source of the Blue Nile,Ethiopia,General,Mr Chips said goodbye - from which fictional school,Brookfield + History & Holidays,Who was UK Labour cabinet minister and social reformer Frank Pakenham better known as? ,Lord Longford (7th Earl of Longford) ,Music,Who Went To No.1 In The Album Charts In 2008 With Their Album Accelerate,REM,General,In Japan a trainee geisha must leave what uncovered,Upper lip unpainted +General,Who were the first people to measure the year,Babylonians,Food & Drink,Umbles can be made into a pie and gave rise to the expression 'to eat (h)umble pie'. What are umbles? ,Edible entrails ,Music,"""Good Feeling"", ""Naked"" And ""Weird"" Preceeded A Number Six Hit For Which Group",Reef +General,What type of metal is used in the filament of an electric light bulb,Tungsten,Food & Drink,Which English king not only had to deal with the bloody Vikings but with a barrage of burnt bannocks? ,King Alfred ,General,Tina Turner Sang The Theme Tune To The James Bond Movie “ Golden Eye” But Who Actually Wrote It ?,U2 +General,Michael O Brien Became Britains First Ever What On April 20th 1974 (Many Have Followed In His Footsteps),Streaker,General,Which Countries Currency Is Called The Rufiya?,The Maldives,General,"According to Noel Coward, who 'go out in the mid-day sun'",Mad dogs and englishmen +General,Smith most common English surname what's the Japanese,Suzuki,General,"What name is given to the sedimentary rock that has changed after being subjected to heat and pressure, for example, Marble",Metamorphic, History & Holidays,In which year was the Rosetta stone written ?,196 BC +General,Temperature when condensation occurs is called the,Dew point,Food & Drink,What is Laver Bread made from? ,Seaweed ,General,An aromatic herb often used with tomatoes,Basil +General,"Botvinnik, Petrosian and Tal have all been World Champions of what",Chess,General,Sindonology is a debate concerning the origins of what,Turin Shroud,Geography,What is the capital of Cape Verde,Praia + Geography,Libreville is the capital of ______?,Gabon,Music,Rusty Egan & Steve Strange Worked As DJ's At A Legendary Club What Was It Called,Blitz,General,If you suffered from scripturience what are you compelled to do,Write things down +General,Term for animals in the class including the scorpions and spiders,Arachnid,General,What do x and y chromosomes combine in making,Males men,General,What does an insect do when it moults,Sheds its skin + History & Holidays,Who was known as 'the peanut president'?,Jimmy Carter,General,In scrabble what two letters are worth ten points,QZ,Sports & Leisure,"Who was known as the ""Sultan of Swat""?",Babe Ruth +Geography,In which South American country is the Atacama desert? ,Chile , History & Holidays,Where was the 1962 world's fair held ,Seattle , History & Holidays,Which Aerosmith song was re-made by Run D.M.C.? ,Walk this way  +General,"Where in the body would you find the malleus, incus and stapes",Inner ear,Music,Land Of Hope & Glory Comes From Whose Pomp & Circumstance Marches,Sir Edward Elgar,General,"Agency of the Executive Office of the President of the United States, created in 1947, together with the National Security Council.",Central intelligence agency +General,What is the next-to-last event,Penultimate,General,What famous occurrence happened at Ujiji in 1871,Stanley found livingstone, History & Holidays,"In Halloween, Michael Meyers wore a Halloween mask of what famous character ",Captain Kirk mask  +Sports & Leisure,What is the last event in a decathlon ? ,1500 Metres ,General,Who sings 'you've got a friend',Carole king,General,"What book did a Mississippi man try to sue in 1994, claiming it was based on ""oppressive hearsay""",The bible bible +General,What do pigments give to your hair & skin,Colour,General,Dried fish eaten as a relish with curry,Bombay duck,General,"In mIRC, what colour does control-K + 4 give?",Red +Sports & Leisure,Which English club was formed in 1887 as Ardwick FC? ,Manchester United ,General,Who does Adrian Mole lust after,Pandora,General,Beethoven's sixth symphony is known as what,The Pastoral + Geography,What is the basic unit of currency for Kiribati ?,Dollar,General,"Minnelli who wrote ""moby dick""",Herman melville, History & Holidays,Who designed Regent's Park in London in 1811? ,John Nash  +General,Which film won the best makeup Oscar in 1992,Bram Stokers Dracula,General,The popular ski resort of Aspen is in which U.S. state,Colorado,General,For $150 you can become a licensed what in Texas,Dead animal hunter +General,"In an average lifetime, the average american sees 345 movies in ___",Theaters,Science & Nature,Slate is formed by the metamorphosis of _________.,Shale,Science & Nature,What type of frog is the smallest frog?,Gold frog +Sports & Leisure,The German Athlete Jurgen Hingsen was always runner-up to which British Athlete in major competitions? ,Daley Thompson ,General,What is an extra lane on an uphill stretch of motorway provided for slow-moving vehicles called,Crawler lane,General,Which of the four Horsemen of the Apocalypse rode a red horse,Slaughter +General,What is the fear of things to the left or left-handed known as,Sinistrophobia,Geography,"Australia's highest mountain is named for Thaddeus ________________, the Polish general who fought in the American Revolution.",Kosciusko,General,Which sea does the river Rhone flow into,Mediterranean +General,What are Munroes,Mountains in Scotland,Science & Nature,How many beams of light are used to record a holograph?,Two,General,Who was the founder of the basotho nation,Moshoeshoe +General,Marbella is a resort on which Spainsh coast,Costa del sol,Music,According To The Beatles How many holes does it take to fill the Albert Hall?,4000,General,Be prepared is the boy scout motto what's the girl guides motto,Be prepared +General,Who controlled the government of china before mao tse tung,Chiang kai-chek,General,In The TV Show Dangermouse What Was Dangermouse's Sidekick Penfolds First Name,Ernest,General,What warning often appeared over dangerous stunts on that's incredible,Do +Geography,In What Year Did Captain Cook Reach New Zealand ,1769 ,General,"What city are you in if yoU.S.troll in the piazza san marco & visit torcello, burano & murano",Venice,General,"Early in his career, which rolling stones guitarist played with rod stewart and 'the faces'",Ron wood +Food & Drink,What 'R'' Is The Name Given To A Picked Herring ,Rolemop ,General,Who is the only man to have been both chief justice and president of the US?,William Taft,General,Who were originally The Sons of Daniel Boone,The Boy Scouts of America +General,Angelo Scicilano better know as who,Charles Atlas,Music,Who recorded 'Shadows of the Night' in 1982?,Pat Benatar,General,James L Herlihy wrote what novel - Oscar winning film,Midnight Cowboy +General,What fish is known as poor mans lobster,Monk Fish,Music,Cotton Eye Joe Was A Hit In 1994 Who Sang It,Rednex,General,In which city was Alexander Graham Bell born in 1847,Edinburgh +Geography,In what city are the famous tivoli gardens ,Copenhagen ,General,"According to Mattel, her manufacturer, what is Barbie's last name",Roberts,Food & Drink,Which dessert was created in honour of a famous ballet dancer? ,Pavlova  +General,"Which Wimbledon Tennis Champion Had A Role In The John Wayne Film ""The Horse Soldiers""",Althea Gibson,General,To whom was Marie Antoinette married,Louis xvi,General,Produced By Film Maker Arthur Melbourne Cooper In 1897 What Was The Product In Britains First Film Advertisement,Birds Custard Powder +General,How far does the cruise liner 'queen elizabeth ii' move for each gallon of diesel it burns,Six inches,Food & Drink,In The Year 2001 Which Popular Food Dish Was Declared As Britain's Favorite ,Chicken Tikka Masala ,Music,What Was The Only Top 10 Single For Thin Lizzy In The 80's,Killer On The Loose +General,Which saint founded a monastery at Iona in the sixth century,Saint columba, Geography,What is the capital of Slovakia ?,Bratislava,General,Which poet wrote 'Howl'?,Allen Ginsberg +Music,"Who Recorded The Album ""Who Made Who""",ACDC,General,What ocean fills nearly a complete hemisphere of the earth's surface,Pacific pacific ocean,Sports & Leisure,Which football team plays at Pride Park ? ,Derby county  +General,Hukusai and Hiroshige were famous Japanese what,Artists,General,Who was the second Soviet cosmonaut to successfully orbit the Earth,Titov,General,How Is The Fictitious Law Enforcement Character “ Archibald Barclay Willoby ” Better Known,PC 49 +General,What is the fear of being tickled by feathers known as,Pteronophobia,Sports & Leisure,"Which Sport Includes Events Called The Halfpipe, The Slopestyle And The Bordercross ",Snowboardring ,General,In Yorkshire in 1872 what took 3 days to pass by,Swarm of Ladybirds +Food & Drink,What is the most common pub name in Great Britain? ,The Red Lion ,General,What is the largest museum,Louvre,Entertainment,Girlfriend of Lex Luther II,Supergirl + History & Holidays,What toy was released in 1957 creating an instant craze among children? ,The Hula Hoop ,Sports & Leisure,At Which Ground Do Scotland Play Their Home International Rugby Union Matches ,Murrayfield ,General,What group sang about a Suicide Blonde,INXS +General,What is the Capital of: Samoa,Apia,General,If you have cherophobia what are you afraid of,Joy - happiness,General,"In the James Bond film Dr No, what was the name of the character played by Ursula Andress",Honey ryder + Geography,Tirana is the capital of ______?,Albania,General,A device used to change the voltage of alternating currents is a __________.,Transformer,Art & Literature,"Which Gallery Has Exhibitions In London & St Ives, Cornwall ",The Tate Gallery  +General,The angel of independence in Mexico City was built by who,Salvador rivas mercado,Science & Nature,The branch of medicine dealing with curing by operative procedures is ________.,Surgery,General,"What country has the largest school, with an enrollment of about 25,000?",Philippines +Geography,What Is The 2nd Official Language Spoken In Germany ,Turkish ,General,What Ancient Invention Was Commercialized And Sold By The “Wham-O” Corporation in 1957 Selling Over 20 Million Of Them In Just 6 Months,Hula Hoop,General,A type of musical instrument turned by hand,Hurdy-gurdy +General,"In the 'james bond' books, to who is miss moneypenny secretary",M, History & Holidays,Who Was The Last British Monarch Born Abroad? ,King George II ,General,Who was al gore's freshman roommate at harvard,Tommy lee jones +Science & Nature," The weasel and the ermine are the same animal. This mammal's coat changes with the season _ in its white winter coat, it is known as an ermine, in its brown coat, it is a __________",Weasel,General,"""retro"" is latin for what",Backward, Geography,What is the circle of the earth at 0 degrees latitude called?,Equator +General,Which fictional land was used to describe John F Kennedy's term as president,Camelot,General,Name the last province to become part of canada,Newfoundland,Toys & Games,You inserted sticks into round blocks to build abstractions with these.,Tinker toys +General,Where are the deepest mines,South africa,Geography,In which country is sapporo ,Japan ,General,Heterophobia is a fear of ______,Opposite sex +General,Which is the only u.s state capital without a mcdonald's,"Montpelier, vermont",General,What sexual practice does a mazophallate enjoy,Tit Wank,General,A person who deliberately sets fire to property,Arsonist +General,What is the name of the Danish parliament,The folketing,Art & Literature,Who wrote the vampire series that featured Lestat as the main character?,Anne Rice,General,What is the minimum IQ score for the genius category?,140 +Music,"About Whose Sister Is John Lennons Song ""Dear Prudence""",Mia Farrow's,Science & Nature,What Is The Largest Species Of Penguin ,Emperer ,General,What is the architectural term for ornamental stonework representing a scroll of paper,Cartouche +General,What is the main ingredient in Borsch,Beetroot,General,The Boys from Syracuse is based on what Shakespeare play,The comedy of errors,General,From what country does the dish skordalia come,Greece +General,What does jefferson davis' headstone say,"At rest, an american soldier and",General,What are officers of 46 u.s federal agencies authorised to carry,Firearms,Entertainment,Who married Shania Twain?,"Robert ""Mutt"" Lange" +General,Who was the first woman to sail single-handed around the world,Naomi james,Music,"Who Had A No.1 Hit In 1984 With ""The Reflex""",Duran Duran,General,Which character first appeared in the book Call for the Dead,George Smiley by John LeCarre +Science & Nature,Our galaxy is commonly known as the ________.,Milky Way,General,What was the Mark Twain in WW2,A Bombsight,General,What English poet always carried a supply of poison in case he had the urge to die,Shelley +General,What is the capitol of Chechnya,Grozny,Music,What color was Lucy's sky?,Marmalade,Music,"Who Had A Hit In 1993 With ""Stairway To Heaven""",Rolf Harris +General,Teenage riot' and 'silver rocket' are two cuts off which alternative rock group's 1988 'daydream nation' album,Sonic Youth,General,BCG is a vaccination for which disease,Tuberculosis,General,What car was used in 'back to the future',De lorean +General,Rabbit 32 - Cat 62 - Ferret 42 - Squirrel 44 - Hedgehog 40 what,Gestation days,Entertainment,What TV series from 1970-1974 starred Susan Dey?,Partridge Family,Art & Literature,Which Artists Name Literally Means (Little Barrel) ,Botticelli  +General,"Who In The World Of Music And Born 1984 Has The Real Name ""Victoria Christina Hesketh""",Little Boots,General,What nationality was the composer Delius,British,General,Whats a sudden & violent burning called,Deflagration +Music,"Which Stones Hit Was Reputedly Based On Martha Reeves & The Vandellas ""Dancing In The Street""",I Can't Get No Satisfaction,General,"In 1981, film was introduced to replace glass in making ______",Photographic,General,In the 1944 film National Velvet name Elizabeth Taylor's horse,Pie + History & Holidays,In What Year Was The Chinese Republic Established ,1911 ,General,What was the name of Hitlers mountain retreat,Berchtesgaden,General,What is the most popular meal ordered in sit down restaurants in the us,Fried chicken +General,What country celebrates its National Day on 17th May?,Norway,Science & Nature," The male __________ will mate for life, and if the female dies, he remains single for the rest of his life. However, if the male dies, the female will hook up with a new mate.",Fox,General,What is the capital of costa rica,San jose +Geography,Which City Is The Worlds Most Densely Populated ,Macau ,Music,Morten Harket Was The Lead Singer With Which 80's Band,Aha,Music,What Was The Beatles Second Movie,Help +General,Who announced his retirement from football after nine seasons in the NFL on the set of The Dirty Dozen in London,Jim Brown,General,What is the fear of being contaminated with germs known as,Misophobia,General,What surname links the singers Jim and Van,Morrison +General,"Which film director's films include ""Raging Bull"" and ""Goodfellas""",Martin scorsese,Entertainment,"Who sings ""Imitation Of Life""?",R.E.M.,General,Progress through Pain was whose motto,Arnold Swartzenager +General,What is the old english word for 'sneeze',Fneasan,General,What is the Capital of: Benin,Porto-novo,General,Liquid food of oatmeal boiled in milk or water,Gruel +Music,Name Dan Hartman's first hit?,Instant Replay,Music,In The World Of Music How Are “SAW” More Commonly Known,"Stock, Aitken, Waterman",General,What is the Capital of: Brunei,Bandar seri begawan +General,What is the Capital of: Cameroon,Yaounde, History & Holidays,"If you were born on Christmas Day, what would your star sign be? ",Capricorn ,Music,"Terence Trent D'Arby Had A hit With ""Dance Little Baby"" Or ""Dance Little Sister""",Dance Little Sister +General,"In the traditional song, what did the children dance around ""on a cold and frosty morning""",The mulberry bush,General,What are salopettes,Snow proof Dungaree trousers,General,"Klu Klux Clam, Uncle Ant Disgruntled goat characters from?",Itchy & Scratchy in The Simpsons +General,"What is made up of the duodenum, jejenum and ileum",Small intestine, Geography,Austin is the capital of ______?,Texas,General,"When the Titanic left Southampton in April 1912, what was its first port of call",Cherbourg +General,Which detective dies in The Final Problem,Sherlock Holmes,General,In the acronym SHAPE for what does the letter S stand,Supreme,General,What dead african leader was originally known as Ras Taffari,Haile selassie +Food & Drink,"This drink is made from espresso coffee, steamed milk and chocolate.",Mocha,General,"Until 1796, which state in the U.S. was known as Franklin",Tennessee,General,What was the first model Rolls Royce called?,Silver ghost +General,What was the screen name of the lead character in The Untouchables,Elliot,General,"Wolves, Curlews, Bulls and Ravens the first groups of what",Boy Scouts,Music,Who Had A Live Album From 1962 Released In 1977 Against Their Wishes,The Beatles +Food & Drink,Which Type Of Prepared Bread Is Named After An Opera Singer? ,Melba Toast ,General,Names - Baker Cook obvious what did Cordwainer do,Shoemaker,Science & Nature," A male kangaroo is called a boomer, and a female is called a __________",Flyer +General,Bobby locke was the first south african golfer to win which open,British,General,To which country do the Coral Sea Islands belong,Australia,Geography,What is the capital of Japan,Tokyo + History & Holidays,Who Was Declared Dead On New Years Day 1953 At The Age Of 29? ,Hank Williams ,General,The name of which fabric comes from the Persian word for spun,Taffeta,Sports & Leisure,What Colour Is The Right Hand Corner Square On A Chess Board ,White  +Geography,Which country would come first in an alphabetical list of countries,Afghanistan,General,What Became The Uk's First Registered Trademark In 1875,Bass Red Triangle,General,What is mixed with tin to produce bronze,Copper +Science & Nature,Which Aviation Company Produces The DC Series Of Aircraft ,McDonnel Douglas ,General,"In June 19&3, SvetLana Savatskaya was the first wornan to do what",Walk in space,Geography,In which state is mount mckinley ,Alaska  +General,What does 'aandw' of root beer fame mean,Allen and wright,General,Of what is '9' the billionth digit,Pi,General,William Le Baron Jenny is credited with inventing what in the US,Skyscraper +General,In the Northern hemisphere its Jan 1st South Aug 1st what is,Racehorse official birthdays,General,Ambigu was an early form of what card game,Poker,Science & Nature,What is brine ?,Salt water +General,Which 1954 film won eight Oscars,On the Waterfront,General,What is the sum of 67 + 67 + 67 + 67 + 67,365,Art & Literature,"Who wrote ""The Rime of the Ancient Mariner?""",Samuel Taylor Coleridge + History & Holidays,In which country do they pound rice into a glutinous form using a huge hammer? ,Japan ,General,In which cop show did Petrie and Isbecki appear,Cagney and Lacey,General,What fluid ran through the Greek Gods instead of blood,Ichor +General,What capital city translates as Capital City in the native tongue,Seoul - South Korea, Geography,What is the name of the mountain chain separating most of Spain from France?,Pyrenees,General,On a UK ordinance survey map what is shown by a red flag,Motor Racing Circuit +General,The Gorbals is a district of which city,Glasgow,Art & Literature,The Term (Impressionism) Was First Used Abusively About Which Artist ,Claude Monet , Language,"What three letter word means the same as ""to ingest""?",Eat +General,"Sweet or fermented juice of apples, used as a beverage and for making vinegar",Cider,General,How many colours are there in the rainbow,Seven,General,Ninkasi was the ancient Egyptian goddess of what,Beer +General,Who wrote mIRC?,Khaled Mardam-Bey,General,96% of all what are purchased by women,Candles, Geography,What is the capital of Madagascar?,Antananarivo +General,"Who recorded ""Barbara Ann"" in 1961",Regents, Geography,Near what river is the Temple of Karnak?,Nile, Geography,"What is South America's highest peak in the Andes, Argentina?",Aconcagua +General,Which Singer Become A Father For The 7 th Time Aged 55 In the Year 2007,Stevie Wonder,General,"Which Star Of A Popular TV Sitcom Released A Fitness Video Called ""Lets Dance"" In 1996",Richard Wilson,General,Who had a hit in 1974 with 'when will i see you again',Three degrees +Music,Name Two Frank Ifields No.1's,"I Remember You, Lovesick Blues, Wayward Wind, Confessin",General,"What describes ""noon good""",Good afternoon,Religion & Mythology,Who is the greek equivalent of the roman god Vulcan,Hephaistos +General,Which English actor won his first Oscar for a role in 'Arthur' at the age of 77,Sir john gielgud,General,What actress played the female cop on hill street blues,Betty thomas,Music,With What Song Did Brotherhood Of Man Win The Eurovision Song Contest In 1976?,Save Your Kisses For Me +General,Which real person took name meaning Man of Steel,Joseph Stalin,General,Which city has the longest metro system,London,Music,"How Many Songs Did Elvis Presley Record With The Word ""Blue"" In The Title",19 +Science & Nature,What is the common name for the sternum?,Breastbone,General,1994 what's the most registered dog by American Kennel Club,Labrador retriever,General,What Were Introduced For The First Time In 1961 By New Scotland Yard?,Photofits / Identikits +General,What has accumulated in the muscles in someone suffering from emphysema,Air,General,In cookery what does ricotta literally mean,Twice cooked,General,"What links the names Botvinik, Tal, Karpov, Fischer",Chess World Champs +General,The average American does it 1811 times in their life - what,Eat at MacDonald's,Geography,In Which Country Is Guallatiri (The Worlds Largest Volcano) ,Chile ,General,Which is the most Northerly African country,Tunisia +Sports & Leisure,What piece of sporting equipment has a maximum length of 42 inches and a maximum thickness of 1 & 1/2 inches? ,A Baseball Bat ,General,"In which country is the seaport of Trabzon, or Trebizond",Turkey,Music,Who replaced 'Bernie Leadon' of 'The Eagles' in 1975?,Joe Walsh +Geography,"Which Mountain Range Lies In The North Of Spain , West Of The Pyrenees ",The Cantabrian Mountains ,Geography,What is the capital of Iran,Tehran,General,What's still legal in Paraguay if the participants are blood doners,Duelling +Art & Literature,On what book was 'Three Days Of The Condor' based?,Six Days Of The Condor,General,What was first opened in 1922 by national department stores in saint louis,Shopping mall,Music,Who Wrote The Theme tune For The bond Movie Live And Let Die,Paul Mc-Cartney +General,If you went on the road to Mandalay what country are you in,Miramar or Burma,General,What is the literal translation of 'Passat' as in the Volkswagen car model?,Trade Wind,General,What do the plants laburnum and broom have in common,Yellow flowers +General,Letterman who did david letterman pay four cartons of marlboro to be on his show,Miss,General,Which author created Svengali,Georges du maurier,General,According to J K Rowling what are muggles,Ordinary people no magic +Science & Nature,What has superseded Newtonian mechanics of the atomic scale ?,Quantum mechanics,General,What's the Australian slang name female man trainee sheep farm,Jillaroo,Technology & Video Games,"In Adventure, which castles require the bridge to explore fully? ",Black and White Castles +General,What is the correct name for a baby mink,Kit or Kitten,Art & Literature,What Dr Seuss character steals Christmas?,Grinch,Music,Which British King Reputedly Wrote Greensleeves,Henry Viii + History & Holidays,Of which Cambodian party was Pol Pot the leader?,Khmer Rouge,General,What type of animal was selected to test the first electric toothbrush,The dog dog,General,Who was beheaded by Henry VIII after writing Utopia,Thomas More +General,What does UFC stand for?,Ultimate fighting championships,Science & Nature,What would a Conchologist be intrested in?,Shells,General,Of which fruit is 'Red Gauntlet' a variety,Strawberry +General,The Campbell-Stokes recorder measures what with a glass ball,Sunshine,General,Which characters were invented in 1957 in Belgium,The Smurfs,General,First used in Salt Lake 1980s what was a jarvik,Artificial heart +Science & Nature,What Is An Otoscope Used To Look At ,The Ear ,General,Just before their planned comeback tour michael hutchence of which band was found dead of an apparent suicide,Inxs,General,What toy was first launched as The Magic Screen,Etch - A - Sketch +General,What lies between Stockholm and Riga,Baltic sea,General,Nicky Chinn Mike Chapman wrote Suzi Quatro No 1 UK song,Can the Can 1973,General,"Which jazz pianist composed the jungle music ""Black and Tan Fantasy"" with Bubber Miley, and recorded it with his band in 1927",Duke ellington +Food & Drink,What can you do after eating a garlic doused Indian meal in order to prevent your breath smelling of garlic the day after? ,Drink a Lassie (yoghurt drink) to coat your throat ,General,Corie Ten Boom was the first licensed female what in Holland,Watchmaker,General,"What Arab capital has a name that means ""God's gift""",Baghdad +General,"Small, active, carnivorous freshwater fish, found in the islands of the southern caribbean sea & in northern south america",Guppy,General,What is the first name of the son of Patsy Kensit and Liam Gallagher,Lennon,General,International car registration letters what country is ZA,South Africa +General,What was given to children to rid them of threadworm,Salt enema,General,What is the most common mammal in the UK,Brown Rat,General,Who was born at Daisy Hill Puppy Farm,Snoopy – Peanuts +General,What New Zealand native was the first man to climb Mt. Everest?,Sir Edmund Hillary,General,"Joan Collins was how old when she posed semi-nude for ""Playboy"" in 1983?",50,General,"A banana is not a fruit, but a ______",Herb +General,What are great waves resulting from earthquakes,Tsunami waves,Geography,Which American State Has The Highest Population ,California ,General,Where's the 19th hole on a golf course,Clubhouse +Science & Nature,What is an emasculated stallion called,Gelding,General,"Which Very Popular Cartoon Character Actually Has The First Name ""Mimi""",Road Runner, History & Holidays,Chrissie Hynde was in which early eighties group? ,The Pretenders  +Sports & Leisure,In Which Athletics Even Might You Use A Fosbury Flop ,High Jump ,General,Who was the only person to ever play in the Super Bowl and World Series?,Deion Sanders,General,Who wrote Madame Bovary,Gustave flaubert +Geography,Which Is The Largest Ocean ,The Pacific Ocean ,General,A waterproof over shoe,Galosh,General,Where On The Human Body Would You Find The Septum,The Gap Between The Nostrils +General,"Best known for 'dancin' in the streets', martha and the vandella's also hit the charts with the song about which man",Jimmy mack,Entertainment,Secret Identities: Bruce Wayne,Batman,General,In The Books Mr Silly Was Light Brown & Had Yellow Shoes What Colour Was His Hat,Orange +General,The Sejm are the legislative body in which country,Poland,Music,Which Suffolk town was the birthplace of Benjamin Britten?,Lowestoft,General,Painting in water colour on fresh plaster,Fresco +General,What is liquor distilled from the fermented mash of cereal grains and containing about 40 to 50 percent ethyl alcohol by volume,Whiskey,General,Vectis' Is The Roman Name For What Part Of The British Isles,Isle Of Wight,General,The study of animal and plant tissues?,Histology +General,Who won an academy award for best supporting actress in the film 'the english patient',Juliet binoche, Geography,What is the capital of Mexico ?,Mexico City,General,A device used to change the voltage of alternating currents is a ______,Transformer +General,Robert Record in the mid 16th century invented what sign,Equals =,General,Thinker who sang the 1963 hit 'it's my party',Lesley gore,General,"In Great Expectations, what was the occupation of Joe Gargery",Blacksmith +General,What did the Ayatollah Khomeni ban in 1979,Music on radio,People & Places,Who was the first snooker player to be a subject of this is your life on the 14 th January 1978? ,Ray Reardon ,General,The religious text Mishna comes from which religion,Judaism +Sports & Leisure,In Which Blood Sport Are Banderillas Used ,Bull Fighting ,General,As what is East Pakistan now known,Bangladesh,General,What Bond thing did Roger Moore not do in 7 films,Drink Martinis - and smoke +General,What was the first stop of the mayflower when it reached the new world,Cape,General,The pleistocene epoch of geologic time is more commonly known as,Last ice,Music,Which Two Impresarios Mixed The 1997 Ministry Of Sound Compilation Album,Pete Tong / Boy George +Entertainment,Who sang 'Beat It'?,Michael Jackson,General,What is the most frequently accessed article World Encyclopaedia,Snake Information,Entertainment,"Who recorded the 1957 hit ""Tammy""?",Debby Reynolds +General,A person who starts fires maliciously is a(n) _________.,Arsonist,Music,Richey Edwards Who Is Missing Presumed Dead Played In Which Band,Marseilles,General,In what country was Greenpeace founded in 1971,Canada +Science & Nature,"Which is a small flightless bird, also New Zealand's national symbol?",Kiwi,General,What is a Tsunami,Tidal wave,General,In which city is the horse race the Palio run,Sienna +Geography,Florida is not the southernmost state in the United States. ___________ is farther south.,Hawaii,Entertainment,He played Superman in the 1978 movie version.,Christopher Reeve,General,Who directed the film Lawrence of Arabia,David lean +General,The koala eats the leaves from this tree,Eucalyptus,General,What crime did Theresa Vaughn commit 62 times in 5 years,Bigamy - Tried 1922,Geography,Which Theory Is Used To Explain Continental Drift ,Plate Tectonics  +General,"In the cartoon Jem and the Holograms,what was the name of Jems computer.",Cinergy,General,Who invented the ball point pen?,Laszlo and Georg Biro, Geography,Nicosia is the capital of ______?,Cyprus +Art & Literature,"From the Hebrew word for 'prophet'. A group of French painters active in the 1890s who worked in a subjective, sometimes mystical style, stressing flat areas of color and pattern. ",Nabis,General,Hemeroticism is what,Sexual fantasy, History & Holidays,According to the lyrics of the song Mr Who? Was asked to turn on his magic beam ,Mr Sandman  +General,Douglas Engelbart invented what - we all use it,Mouse,General,Which sport is played by L.A. Lakers,Basketball,Geography,"The first letter of every continent's name is the same as the last: AmericA, AntarcticA, EuropE, AsiA, ______________, AfricA.",Australia +Sports & Leisure,Which Famous Athletics World Record Stood Between 1968 & 1991 ,Bob Beamans Long Jump ,General,In Riverside Cal its illegal to kiss unless both wipe lips with what,Rose Water,General,"At Disneyland Paris, the park's famous __________ is known as Le Château de la Belle au Bois Dormant. ",Sleeping beauty castle +Science & Nature, Baby mink are born blind and remain sightless for a __________,Month,General,What do 12% of mississippi households lack,Telephones, History & Holidays,"Who was Ulysses' son, who grew to manhood in his absence?",Telemachus +General,What Muslims practise shirling as a religious act,Dervishes,General,What type of clothing article is a Belcher,Neckerchief,Music,Which Brothers Reached No.5 In 1961 With Warpaint,The Brook Brothers +Entertainment,Who invented the synthesiser ?,Bob Moog,Music,Which 1994 Album Bore The Name And Naked Picture Of The Artist,Seal,Science & Nature,What is measured by a Geiger counter,Radioactivity + History & Holidays,Which Revolutionary Icon Was Killed On 9th October 1967 ,Che Guevara ,General,What was invented by Henry D Perky 1893 Denver Colorado,Shredded Wheat,General,According to the traditional rhyme what's the fate of Wed. child,Full of Woe +Geography,Constructed In 1333 Which Is The Oldest Wooden Bridge In Europe ,The Kapellbrucke In Lucerne Switzerland ,General,Sheena Easton sang the title song for which 'James Bond' film,For your eyes only,Music,Which Us State Is Mentioned In The Lyrics To The Tony Christie Song Amarillo,Texas +General,How do you tell a male chromosome from a female chromosome,Pull down their genes,Music,Who designed Madonna's conical basque for her 1990 Blonde Ambition tour?,Jean Paul Gautier,Science & Nature,Sodium barcarbonate is better known as ________.,Baking soda +General,What year was Robert the bruce crowned King of Scotland,1306,General,Who sang 'islands in the stream' with kenny rogers,Dolly parton,General,Name every actress who played an 'angel' on Charlie's Angels,"Farrah Fawcett,Kate Jackson,Jaclyn Smith,Cheryl Ladd,Shelley hack,Tanya Roberts" +General,Washington police officers get a half hour class in how to what,Sit Down,General,For the city 'summer of 69' was a hit for which canuck in 1984,Bryan adams,People & Places,Who Was The First Player To Be Sent Off In An FA Cup Final ,Moran  +General,"Who recorded the album ""the cry of love"" in 1970",Jimi hendrix,General,Beasley how much does park place cost in monopoly,Four hundred fifty dollars,Science & Nature,Walrus tusks are made of ________.,Ivory +General,What is the most common name in italy?,Mario Rossi,General,American Football - where do the Lions play at home,Detroit,General,What is the capital of the state of Oklahoma,Oklahoma city +General,What year was the intel pentium processor introduced,1993,General,Nixon what hans christian andersen fairy tale character is immortalized in a famous statue in copenhagen's harbor,The little mermaid,General,Gesture of celebration in which two people slap each others palms with their arms outstretched over their heads,High-five +Music,How Is Rock Star Vincent Furnier Better Known?,Alice Cooper,General,In what country did tulips originate,Persia,General,Who was john the baptist's mother,Elizabeth +General,Diverse group of either single-celled or multicellular organisms that obtain food by direct absorption of nutrients,Fungi,Food & Drink,What alcoholic drink is distilled from pears? ,Perry ,General,99% of American households have at least one what,TV +General,Which animal uses white ear spots as identification marks,Tiger,Science & Nature, The Hirudo leech lays its babies within a cocoon; the Amazon leech carries its babies on its __________ _ sometimes as many as 300.,Stomach,Science & Nature,"Knife, Clown, and Pencil are types of __________.",Fish +Science & Nature,Francis Crick was the most famous of the discoverers of what? ,DNA ,General,What film won the best visual effects Oscar in 1985,Cocoon,Music,"Which Song Urged You To ""Push Pineapple , Shake The Tree""",Agadoo +General,Elvis Presley said big what on a woman turned him off,Feet,General,"According to the Buddhist tradition, deceased people who have followed the eightfold path will be freed from the cycle of reincarnation & gain what state of total peace",Nirvana,General,When did Henry Ford build his first car,1896 + History & Holidays,Which Actor Of The Silent Era Was Known As 'The Man Of A Thousand Faces' ,Lon Chaney ,General,What instrument did Glen Miller play,Trombone,General,Who gave millions of dollars to britain in 1930,Edward s harkness +General,What is the Capital of: Turkmenistan,Ashgabat,General,What US film was based on Thomas Dixons The Clansman,Birth of a Nation,General,In the comic strip Garfield what is the teddy bear’s name,Pooky +General,George Fox founded which religious group,Quakers, Geography,"What U.S. state is known as The Land of 10,000 Lakes?",Minnesota,Geography,On what island is the Blue Grotto,Capri +General,What is Wales' national dish called?,Cawl,Music,"""Word Up"" Was A 1986 Hit For Which Band",Cameo,General,"The fireballs had some success with the record 'torquay', but when they teamed up with jimmy gilmer, which record stayed at no. 1 for an entire year",Sugar shack +General,Where were Panama hats first made,Peru,General,Disney: The original name of Goofy was ___________ ,Dippy dawg,General,What is the Capital of: MacaU.S.AR,Macau +General,Sweet opaque jelly of flavoured cornflour and milk,Blancmange,Music,Which Beatle was against hiring Allan Klein as business manager in 1969?,Paul, Geography,What is the capital of Kenya ?,Nairobi +Science & Nature,Where Was The First Successful Steam Engine Installed ,Dudley East Midlands 1712 ,General,What food dishes name translates as pepper water,Mulligatawny – from India, History & Holidays,"Who said ""Veni, vidi, vici"" (I came, I saw, I conquered)",Julius caesar +Music,Which British Female Singer Recorded An Album In Memphis With Jerry Wexler Producing,Dusty Springfield,General,What is the currency of guatemala,Quetzal, Geography,What is the basic unit of currency for Gabon ?,Franc +Science & Nature,In what organ of the body is insulin produced?,Pancreas,General,What type of sword was excalibur,Long sword,General,Which shipping area covers the North coast of Ireland,Malin +Tech & Video Games,"Who said ""All life begins and ends with Nu___at least this is my belief for now___""? ",Nu,Music,"Which song and artists sang this during the 1970’s “ A year has passed since I wrote my note, but I should have known this right from the start, only hope can keep me together, love can mend your life, but love can break your heart”?",Police / Message In A Bottle,Sports & Leisure,Which sport takes place in a circle 4.55m in diameter? ,Sumo Wrestling  + History & Holidays,What Was The Name Of The Son Of Cleopatra & Julius Caesar ,Caesarion ,General,Who sang arthur's theme (best that you can do),Christopher cross,General,What is the birthstone for January,Garnet +General,The practice of women taking more than one husband is called?,Polyandry,General,"What is the name of the heroine in the cartoon ""Beauty and the Beast""",Belle,General,Collective nouns - what are a group of apes called,Shrewdness +General,Where did doughnuts originate,Holland,Music,With Which Rock Band Did Slash Play Guitar?,Guns N Roses,Sports & Leisure,With what sport is Chris Boardman associated?,Cycling +Science & Nature, The __________ has green bones.,Garfish, History & Holidays,"Also known as the 'Isle of Apples', Christ and Joseph of Aramathea travelled here in ancient times.",Avalon, History & Holidays,"""In the movie """"It's A Wonderful Life"""" how do you know that a new angel has received his wings? (""""a cock crows, there is a lightening flash, a bell rings, a trumpet sounds"""") "" ",A Bell Rings  +General,The Tilia is the Latin name for what type of tree,Lime,General,What is the highest peak in Australia,Mount kosciusko,General,What is the stage name of George Alan O'Dowd,Boy george +General,What is the main flavour of aioli,Garlic,Music,Name Black Sabbath's debut hit?,Paranoid,General,In which town or city is the national Museum for Photography,Bradford +General,"Bruce Willis, Arnold Schwarzenegger and Sylvester Stallone own which London restaurant",Planet hollywood,General,"Before Becoming A Famous Poet ""Pam Ayres"" Used To Be Part Of Which Famous Organisation?",Mi5,Food & Drink,From what animal do we get venison ,Deer  + Geography,Budapest is the capital of ______?,Hungary,Music,On Which Record Label Did James Have A String Of Hits In The 90's,Fontana,General,What was Didus Ineptus better known as,The Dodo +Entertainment,What was Don Rickles' nickname?,Mr. Warmth,Entertainment,"Who appeared in 'St. Elmo's Fire', 'The Scarlett Letter' and 'Striptease'?",Demi Moore,General,Any of the drugs used to reduce nervous tension or induce sleep,Sedative +General,A normal human body has 46 what,Chromosomes,General,What do humans get from the Cassava,Tapioca,General,Who was Time magazine man of the year 1952,Queen Elizabeth II +General,Which English Monarch Had The Shortest Reign,Edward 5th,General,El cid was the name of what college's mascot goat,Annapolis naval academy,General,In 1867 Lucian B Smith invented what restraint,Barbed Wire +General,"Nelis, Seckel, Forelle and Bosc all varieties of what",Pears,Geography,Where Is The Mathematical Bridge ,Oxford ,General,Who won six consecutive Wimbledon titles in the 1980s,Martina Navratilova +Geography,"Located in the Indian Ocean just north of the equator, __________ had a highly developed civilization as early as the fifth century B.C.",Sri lanka,General,What animal is in the hassen in hassenpfeffer,Hare or Rabbit,General,In which game would you use the royal fork,Chess knight threaten king queen +Science & Nature,Dogs bark. What do donkeys do,Bray,General,What was the longest running primetime TV drama 20 seasons,Gunsmoke,Food & Drink,Natural vanilla flavoring comes from this plant.,Orchid +General,George Harrison lost a plagiarism suit for which of his songs,My sweet lord,General,Which Sci-Fi Sitcom star like to eat cats?,Alf,Music,"His First Chart Entry, Which Record Did Tom Jones Take To No.1 In 1965",It's Not Unusual +General,Lack of what makes albinos,Pigment,Sports & Leisure,Which footballer was England`s first black player? ,Viv Anderson , History & Holidays,Which Long Running Sci Fi Program Was First Aired In November 1963 ,Dr Who  +Science & Nature,Circuits can be wired in series or in _________.,Parallel,Entertainment,What type of plant does Broom Hilda sell,Venus flytrap,Science & Nature,Of what is 98% of the weight of water made?,Oxygen +General,Who did zola budd trip in the 1984 los angeles olympics,Mary decker,General,"Apart from Gottfried Leibniz, which famous scientist developed the many techniques of calculus in mathematics",Isaac newton,General,"Who was the ferrymen of the dead in Greek Mythology, famously immortalised in the name of a moon of the former planet Pluto?",Charon +General,If you landed at Shannon airport where are you,Limerick,General,In what constellation would you find the 'Horesehead' nebula,Orion,Music,What female rock star did Mike Wallace of '60 Minutes' interview in 1969?,Janis Joplin +General,Which of tea leaves or coffee beans have more caffeine,Tea leaves,General,What is the capital of south vietnam,Saigon, History & Holidays,For Which Crime Was 'Thomas McMahon'' Jailed For Life In October 1979 ,Mountbatten's Murder  +General,What have over 80% of boxers suffered,Brain damage,Science & Nature,What Is The Common Name For The Plant Chlorophytum? ,Spider Plant ,General,"In American universities, what is a second-year student called",Sophomore + History & Holidays,Which TV Character Owns A Talking Cat Called Salem ,Sabrina Teenage Witch ,General,The word vinegar come from French meaning what,Sour Wine,General,What is the fear of rooms known as,Koinoniphobia +Music,What Kind Of triangle Did New Order Have,Bizarre Love Triangle,Science & Nature,In dyeing what is the name given to the substance used for fixing the color ,Catalyst ,General,Which Saint is associated with an eagle in religious art,Saint John +General,What is the longest continuous footpath in the world,Appalachian trail,General,__isms: Exalting one's country above all others.,Nationalism,Science & Nature,"Which Birds Were Kept On The Capitol And, Allegedly, Saved Rome From Attack By The Gauls ",The Geese  +General,Who was the Greek and Roman god of the wind?,Aeolus,General,Charles S Stratton became famous as who,General Tom Thumb with Barnum,General,In sport what stands four feet by six feet,An Ice Hockey Net +General,Who was the first person to break the sound barrier,Chuck yager, History & Holidays,What company made the first color arcade game? ,Atari ,General,What three numbers divide into nine evenly,"1,3,9" +General,BB King gave his guitar what nickname,Lucile,Music,What Is Doris Day's Real Name,Doris Von Kappelhoff,Entertainment,Who directed the film 'The Birds' from Daphne du Maurier?,Alfred Hitchcock +General,What is the french word for 'mistake',Faux pas,General,Lil'folkes was the original name of what comic strip,Peanuts,General,What is the largest moon in our solar system,Ganymede of Jupiter +Geography,This re_opened in 1975 after being closed for 8 years.,Suez canal,Music,Where Does Singer Chris De Burgh Come From,Argentina,General,What 1968-69 tv series did joan blondell star in,Here come the brides +Science & Nature,How Many Faces Has The Solid A Dodecahedron ,12 ,General,What word in English has the most definitions,Set,General,What Country Was The First To Have A Woman Prime Minster?,Sri Lanka +General,For what is ethylene glycol used in automobiles,Anti-freeze,General,What is strange about the Golden Queen holly,It is male Golden King is female,General,What countries leader does not have an official residence,Cuba + History & Holidays,Which member of the Beatles got his first drum kit on Christmas Day 1957 ,Ringo Starr ,General,If you suffer from Tinea Pedis what have you got,Athletes foot,Music,Who were the first Scottish group to have 3 No.1 hits?,Wet Wet Wet +Music,Name Led Zeppelins Drummer Who Died Of An Alcohol Related Incident In 1980,John Bonham,General,What do the French call la manche,The english channel,General,Which country music legend was known as 'the drifting cowboy',Hank williams +General,What is the flower that stands for: promptness,Ten-week stock, History & Holidays,"Which Country Hosted The Summer Olympic Games In 1952 A= Germany, B=Finland, C=France ",B= Finland ,General,"Count Almaviva, a character in Rossini's The Barber of Seville , also appears in which opera by Mozart",The marriage of figaro + History & Holidays,Which U.S. President was the first to have a Christmas tree in the White House? ,Franklin Pierce (1853-1857) ,Sports & Leisure,How Many Players Are In A Basketball Team? ,Five ,General,What kind of tradesman uses a 'plunger',A plumber +Entertainment,"Besides the Stones, which group had the longest touring career until the founder's death in 1995?",The Grateful Dead,Geography,In which state is appomattax ,Virginia , History & Holidays,"In Holland, Santa traditionally doesn't actually deliver the presents who does, is it, Rudolph, Santa's servant Black Peter, A goat named Ukko or Thirteen Elves ",His servant Black Peter  +General,Winston Churchill married in 1908. What was his wife's Christian name,Clementine,General,Who won best actor oscar for gandhi,Ben kingsley,Science & Nature,The largest single organ of the human body is the ________.,Skin +General,What is the town locale in the tv serial search for tomorrow,Henderson,Science & Nature,"The study of the size, composition and distribution of the human population.",Demography,Music,What Instrumental Piece Did Bruce Johnson Take Us Down In 1977,Pipeline +Tech & Video Games,"In the 'Extreme Battle' mini-game in 'Resident Evil 2', what characters are selectable? ","Claire, Leon, Ada, and Chris",Geography,In which ocean or sea are the Seychelles,Indian,General,What is taught at the Californian Academy of Tauromaquia,Bullfighting +General,How many days are there in a leap year,366,General,What were the names of noah's children,Shem ham and japheth,General,What are Trawsfynnydd (Trous-fun-uth) and Wylfa (Wilva) in Wales,Nuclear power stations +General,How many segments for the number 8 on a digital clock,Seven,General,Where would you find your shank,Sole of foot, History & Holidays,Who Placed His Cloak On A Puddle For Elizabeth I? ,Sir Walter Raleigh  +Music,What Was The First No.1 For The Hollies,I'm Alive,Music,"Who Had A Hit With ""I Can't Go For That (No Can Do)""",Hall & Oats,General,What country's people were taxed for using salt in the 17th century,France + History & Holidays,Name The 8 Reindeer From 'The Night Before Christmas' Poem ,"Blitzen, Comet, Cupid, Dancer, Dasher, Donner, Prancer, Vixen ",Science & Nature, There are 690 known species of __________,Bats,General,Which sea area lies between Plymouth and Wight,Portland +General,Which Famous Pop Star Made An On Screen Appearance Alongside Mr.T In The A Team,Boy George,Geography,What Nationality Was The First Person To Sail Round Northern Canada Through The Bering Strait ,Norwegian Roald Amundsen ,General,"A story by Hans Anderson ""The..",Ugly duckling +General,In golf what name is given to the No 3 wood,Spoon,General,Who Was The Last Female Winner Of Wimbledon In The 20th Century (1999),Lindsey Davenport,General,Name woman set up free birth control clinic in Holloway in 1920,Marie Stopes +General,London and which UK city are joined by the Grand Union canal,Birmingham,General,What is a Havana Brown,A Small Rabbit,General,What represent the body and blood of Christ in the service of Holy Communion,Bread and wine +General,Why did Mehmet Ali Agca become famous in 1981,Shooting pope jean paul ll,Entertainment,Who was the Hulk's first friend,Rick jones,General,Which emperor made his horse a senator,Caligula +General,Marduk was the creator of the world to what ancient people,Babylonians,General,In Arizona you can have no more than two what in a house,Dildos - any more and it’s a brothel,General,What is the Soviet Unions highest military honor,Order of lenin +General,Casey Kasem was the original voice of what cartoon character,Shaggy on Scooby Doo,General,Where was Ice Cream invented,China,Art & Literature,Who Wrote (Brave New World) ,Aldous Huxley  +Tech & Video Games,Which body part is found in Bodley Mansion in 'Castlevania II: Simon's Quest'? ,An eye,Religion & Mythology,"In Greek mythology, what was Minos the king of?",Crete,Music,In Which Popular Song Did The Singers Girlfriend Have “Hair Of Gold & Lips Like Cherries”,Green Green Grass Of Home +General,What is the US equivalent of the UK Anglican church,Episcopalian,General,Whose backing group was called 'The Blockheads',Ian drury,General,Treatment of disease by various approaches,Therapy +General,What is a mexican avocado dip,Guacamole,General,Which animal pronks,Springbok,General,Who was the first Englishman to die in an aircrash,Charles Rolls +General,Who created james bond,Ian fleming,General,"Who wrote ""The Analects""",Confucius,General,Between who was the shortest war in history,Zanzibar and england +General,Who sang 'foolish games',Jewel, History & Holidays,What year was NASA founded? ,1958 ,Music,Which Family Group Recorded The Album “In Blue” In 2000?,The Corrs +General,What egyptian object is also known as 'the key to the nile',Ankh,Music,Which Leo Sayer Song Won A Grammy For Best Rhythm & Blues Song In 1977,You Make Me Feel like Dancing,General,If someone from Australia is Australian what is someone from New Zealand,New zealand +Science & Nature,What is the meaning of the name of the constellation Sagitta ?,Arrow,General,Starting highest write the Roman Numerals in descending order,MDCLXVI or 1966,General,What is the state capital of Texas,Austin +General,"Who wrote ""The Outcast of the Islands""",Joseph conrad,General,"In pottery, what is crackle",Tiny cracks in the glaze,Science & Nature,This ancient attempt to transmute base metals into gold was called ________.,Alchemy +General,Where is the novel 'Anne of Green Gables' set?,Avonlea,General,Military operations above the surface of the earth,Air warfare,General,In 1987 the Jockey Club disqualified a horse that had eaten what,Mars Bar +General,What colours are on the belgian flag,"Yellow, black and red",General,Where could you spend a Dram - Capital Yerevan,Armenia,Music,Which American group enjoyed a worldwide hit in 1976 with the song “If You Leave Me Now”,Chicago +Geography,What is the capital of Suriname,Paramaribo,General,"264,000 bottles of what were recalled after 7 people died",Tylenol, History & Holidays,"What did Marie Curie die of on 4th July, 1934?",Radiation poisoning +Science & Nature, It would require an average of 18 __________ to weigh in at 1 ounce.,Hummingbirds,General,A fetus acquires fingerprints at what age,3 months,General,Black and Blue play Red and Yellow at what game,Croquet + History & Holidays,Who assassinated John Lennon?,Mark David Chapman,General,What show featured Nell Carter as a larger than life housekeeper?,Gimme a Break,General,The name of the little people in Frank Baum's the Wonderful Wizard of Oz,Munchkins +Music,Who Had A 90's Hit With The Song Mona,Craig McLachlan,General,Who wrote the play All My Sons in 1947,Arthur miller,General,"What is the least dense substance in the world, at 0.08988 g/cc",Hydrogen gas +General,What is a group of this animal called: Bear,Sleuth sloth,Science & Nature, Almost half the pigs in the world are kept by farmers in __________,China,General,What is the Capital of: Guernsey,Saint peter port +Tech & Video Games,"In the game Joust, what animal was your mount? ",An Ostritch,Food & Drink,"What name is given to the Japanese dish in which fish, shellfish, or vegetables are fried in batter? ",Tempura ,General,What was eddie murphy's character name in 'beverley hills cop',Axel foley +General,Animals that once existed but don't exist now are said to be _______.,Extinct,General,"How does Alice kill Freddy Krueger in ""Nightmare on Elm Street 4""?",With a mirror,Science & Nature,"Which Came Of Better In A Fight, Tyrannosaurus Rex, Iguanadon ",Neither They Missed Eachother By About 42 Million Years  +General,What are Waist Overalls,Jeans - Levi Struss,General,"What famous singer played the title role in ""the great caruso""?",Mario lanza,General,Jean-Christopher Denner invented what musical instrument,Clarinet +General,What's the term for a bet before cards are dealt,Ante, History & Holidays,How many spirits appeared to Ebenezer Scrooge in Charles Dickens' A Christmas Carol? ,"4, Jacob MarleyPast Present Future ",General,"Complete this quote: ""I feel the need___ the need for___""",Speed +General,Joe Louis became World Champion in which year,1937, Geography,In which city is Red Square?,Moscow, History & Holidays,Where Did The Mayflower Set Sail From In 1620 ,Southampton  +General,What dr seuss character steals christmas,Grinch,General,"What does ""the absent minded professor"" teach",Chemistry,Science & Nature,What Type Of Fuel Is Usually Used In Jet Aircraft Engines ,Paraffin  +Sports & Leisure,Which Sport Do The Miami Dolphins Play? ,American Football ,General,Name one of the three branches of the Methodist Church that merged in 1932 to form the current Church.,Wesleyan primitive united,General,"Who said, ich bin ein Berliner",John f kennedy +Music,"Whose 1999 Debut Solo Album Was Entitled ""Schizophonic""",Geri Halliwell, History & Holidays,"Originating from a story about an Irish blacksmith, you ran into the Devil in a pub, what name is given to a pumpkin if it has a face carved into it and is illuminated by a candle ",Jack O' Lantern ,General,What does COD stand for,Cash on delivery +General,What is the name of Moses and Aaron's sister,Miriam,Geography,Which US state capital is known as the (mile high city) because of its elevation above sea level? ,Denver ,General,In which country was the Caesar Salad invented,Mexico +General,What Are You Frightened Of If you are Alektorophobic?,Chickens,General,In Call of the Wild by Jack London what was the dogs name,Buck,General,Nichole Dunsdon was the last what in October 1992,Miss Canada +General,What Italian habit did Thomas Coyrat introduce to England 1608,Eating with forks,General,People eaters what tools are used to crewel,Needles,General,What is the monetary unit of Ecuador,Sucre +General,Crashes what was the first ship to reach the titanic after it sank,Carpathia,Art & Literature,Which Famous Book Contains The Line 'Mr & Mrs Dursley of number 4 Privet Drive were proud to say that they were perfectly normal' ,Harry Potter And The Philosophers Stone ,Music,Which Girl Did John Fred And The Playboy Band Try To Hide Behind Spectacles,Judy In Disguise With Glasses +General,What is the official state food of Texas,Chilli,General,In 1935 Charlton C McGee invented what in the USA,Parking Meter,General,Hard heavy black tropical wood,Ebony +General,Khrushchev what was the sequel to 'going my way',The bells of st mary's,General,What colour were ETs eyes,Blue,Science & Nature,Who Invented The Telephone ,Alexander Graham Bell  +General,Where in the USA is Yale university,Connecticut,General,Who betrayed jesus to the romans,Judas iscariot,General,Where is the largest volcano in our solar system,Mars +General,"What are described by the terms rep, challis & foulard",Neckties,General,Where is the fictional television station bdrx located,Bedrock,General,What's the only mammal that can't jump,Elephant +General,What actor played seven roles in no way to treat a lady,Rod steiger,General,What Football Team Plays It's Home Games At The Ibrox Stadium?,Glasgow Rangers,General,In what book does Humpty Dumpty first appear,Through the looking Glass +Science & Nature,"What nationality was Robert Bunsen, of Bunsen Burner Fame? ",German , History & Holidays,"Which actor, an archdeacon in the TV series all gas & gaiters died in January 1979? ",Robertson Hare ,Geography,What Was Iran formerly Known As ,Persia  +General,The Rimac river runs through what city,Lima Peru,Music,Which Pair Had The Best Selling Album In Both 1970 And 1971,Simon & Garfunkel,People & Places,Who Was The Director General Of The BBC In 1995 ,Lord Reith  +General,What is the slowest moving land mammal,Sloth,General,What is the fear of neglecting duty known as,Paralipophobia,General,"To determine the percentage of alcohol in a bottle of liquor, by how much is proof divided",Two +General,On average what weight nine pounds,Cremated Ashes,Geography,The roadrunner is the official bird of _____________,New mexico,General,What sport's been the subject of the most American movies,Boxing +General,What is used to lift fingerprints from difficult surfaces,Super glue,General,"First preached by arius in the 4th centruy, what term is used for the belief that jesus is not the equal of god",Arianism,General,"Disney's __________ was the first roller coaster to run on steel tubes, which made the ride smoother while allowing Disney to build longer_lasting coasters faster and cheaper. ",Matterhorn +Science & Nature,Which Country Containes The Worlds Biggest Oil Refinery ,Venezuela ,General,What is the motto of the SAS?,Who Dares Wins,General,The pica pica is what common bird,Magpie +General,Tanzania Was Formely A Colony Of Which Country,Germany,General,Basketball: the Denver ______,Nuggets, History & Holidays,Who Was The Very First Presenter Of The UK National Lottery Draw ,Noel Edmonds  +Music,What Was The Beatles First Hit Single,Love Me Do,General,The Two Ronnies first appeared together in which TV series,The frost report,General,Who invented the wristwatch,Louis cartier +General,1990s slang for very short cutoff jeans.,Daisy dukes,General,The thickness of silk is measured in what,Denier,General,What is the fear of computers known as,Logizomechanophobia +Art & Literature,Who wrote Great Expectations?,Charles Dickens,Music,In Which Year Was John Lennon Shot & Killed In New York City,1980,General,Which Pop Star Became An Honorary Member Of Exeter City Football Club In 2002,Michael Jackson + History & Holidays,How Long Was The Hundred Years War? ,116 Years ,Music,"Which group's debut album, Always & Forever , shadowed their name?",Eternal,Sports & Leisure,Which country was judo developed in?,Japan +General,What is the only country with one train station,Singapore,General,Who became' Chief Executive' of Pakistan in October 1999,General pervaiz musharraf,Food & Drink,"Toasted Bread, Covered With A Mixture Of Cheese, Pale Ale, And Mustard Is Often Referred To As what ",Welsh Rarebit  +General,Name the twins on the Thundercats.,Wily Kit and Wily Kat,Toys & Games,What number is at 12 o'clock on a dartboard?,20,General,In what club are all the members liars,Ananias club +General,In which river did the Pied Piper drown the rats of Hamelin,Weser,Geography,What Canadian city is at the west end of Lake Ontario,Hamilton,General,Who was the oldest heavyweight boxing champion,George forman +General,Which actor was dubbed the muscles from Brussels,Jean Claude Van Dam,Music,"How In The World Of Music Is ""Dino Crocetti"" More Commonly Known",Dean Martin,General,How many episodes were there in the original star trek series,75 +General,What product was originally manufactured by communications giant Nokia?,Paper,General,In Kansas its illegal to eat cherry pie with what,Ice Cream,General,Who invented the egg mcmuffin,Ed peterson +General,What is the line frequency of the american power supply,60hz,General, An anemometer measures _________.,Wind velocity,General,Ictheologists study what,Fish +General,The Charles Bridge and St Vitus' Cathedral are in which European city,Prague,General,"In the 1960s, which company made a version of the Austin Mini, called the Elf",Riley,General,Which country invented French fried potatoes,Belgium +Science & Nature,What is a group of larks called?,Exaltation,General,Which athlete withdrew from the Sydney Olympics claiming she had been confronted by a stranger in her hotel room,Marie-jose perec,General,What was Louis 14th born with two of - that amazed everyone,Teeth +General,"Carl Djerassi, Pioneered, Invented and Patented Which Everyday Product?",The Contraceptive Pill,General,What do you call an exaggerated fear of sleep,Hypnophobia,Sports & Leisure,In Which Sport Do Competitors Soop The Ice And Throw Stones At Houses ,Curling  +Geography,Which Palace was the principal home of the Monarchy prior to 1837 when Buckingham Palace became the Monarchs address ,St James Palace ,General,Phonophobia is the fear of,Noises voices,General,Name Ronald Reagan's first wife.,Wyman +General,In 1995 the average US public school had 75 what,Computers installed,General,What city exists on every continent,Rome,General,Americans consume how many tons of aspirin per day,Forty two 42 +General,Who wrote about Willie Wonka and the Chocolate Factory,Roald Dahl,General,Where did John Lenon marry Yoko Ono (place name),"Gibraltar - Flew from Paris - back", History & Holidays,"""Which Christmas poem does this line belong to? """"With peace on earth, good-will to men!"""" (""""Christmas Bells"""", """"The 3 Kings"""", """"A Christmas Carol"""", """"Nativity"""" "" ",Christmas Bells  +Music,Irene T Escalera Is The Real Name Of Which Singer,Irene Cara,General,What is a baby rooster,Cockerel,General,What country celebrates its National Day on 2nd June?,Italy +General,Tennis what links Coke Rolex Slazenger Robinson's Barley water,Only adverts Wimbledon Centre,General,On a piano the left pedal is the soft what's the right called,Sustaining,Geography,"The planner of the city of __________________ was French architect Pierre L'Enfant. In 1791, it was known as Federal City.",Washington d.c +General,What's the main ingredient in glass,Sand,General,"What name was given to a small gaiter, worn over the instep",Spat,General,Wayne rogers plays this character in house calls,Charlie michaels +General,Who directed the film 'ordinary people',Robert redford,General,"According to Roy Orbison, he's 'Saving nickels and dimes and looking forward to happier times on ______'",Blue Bayou,Sports & Leisure,Which Type Of Wood Are Cricket Bats Usually Made From ,Willow  +General,What type of creature was an Archelon,A Turtle,General,Which group had their first U.K. number one tilt ill 1974 with Down Down,Status quo,Music,Which Musical Did Meat Loaf Appear In As An American Hero In 1969,Hair (As Grant) +General,From whom did the u.s buy the virgin islands,Denmark,General,What did the ancient Greeks use instead of soap,Olive Oil,General,What is the fear of noises or voices known as,Phonophobia +People & Places,Whose Daughter Married Thin Lizzys Phil Lynott ,Leslie Crowther ,Music,Who Was Driving Home For Christmas In 1988,Chris Rea,Geography,Name the longest river in Nigeria,Niger +General,"According to Elton John, what were we hoppin' and boppin' to",Crocodile Rock,Science & Nature,What 2 planets do not have moons?,Mercury and Venus,General,"This bar first appeared in oats'n honey, cinammon, and coconut flavors.",Granola +Science & Nature,Who Opened The First Cinema In Paris In 1895 ,The Lumiere Brothers ,General,Which 2015 animated film is about the workings and emotions of a child's mind after she is forced to move to San Francisco from the mid-west?,Inside Out,General,Who created british master spy george smiley,John le carre +General,Albert De Salvo was better known as who,The Boston Strangler,General,Who coined the word 'bourgeoise',Karl marx,General,On which day of the week is the Moslem Sabbath,Friday +General,In which film did Kevin Costner make his directorial debut,Dances with wolves,Music,Knocking On Heavens Door Was A Hit In 1992 Who Sang It,Guns & Roses,Geography,What Is Rhodesia Now Called ,Zimbabwe  +Music,"The Cover Version ""Sea Of Love"" Was A Hit For The Honey Drippers Or The honey Eaters",The Honey Drippers,General,By what other name is the island of Lindisfarne known,Holy island,General,"In 1909, who was the first man to reach the North Pole",Commander robert peary +General,"What, on an Italian menu, is 'zuppa inglese'",Trifle,Sports & Leisure,Which Wood Are Cricket Bats Usually Made From? ,Willow ,General,What mercy doctor called for death row inmates to donate their organs in 1993,Jack kevorkian +General,Who or what killed Al Capone,Syphilis,General,When was crystal palace destroyed,1936,General,Which Italian born U.S. physicist won the 1938 Nobel Prize for Physics for his work on radioactive elements,Enrico fermi +General,In Korea which animal is the symbol of long life,The deer,General,"Who once famously said ""Paint me warts and all""?",Oliver Cromwell,Sports & Leisure,Who fell behind Roger Maris in 1961 for the homerun record?,Mickey Mantle +Music,What was Brian's brother's name?,Clive,General,What is the fear of dust known as,Koniophobia,Science & Nature,What Disease Is Caused By The Inhalation Of Coal Dust ,Pneumoconiosis  +General,In Greek mythology Cronos and Rhea were the parents of who,Zeus,General,Sienna law forbids women of what name from prostitution,Maria,General,IBM is Big Blue Coca Cola Big Red who is Big Black,United Parcel Service UPS +General,What sort of drink is barbancourt,Rum,General,What state did sam mccloud come from,New mexico,Music,What Was The Only Top 10 hit For Rita Coolidge,We're All Alone +General,In England what can you not hang out of your window,A Bed,General,What in US are Ambassador Ben Franklin George Washington,Suspension Bridges,General,Meteorophobia is the fear of,Meteors +General,"What surgical operation was done 115,000 times in us in 1986",Silicone breast,General,"On the fahrenheit scale, how many degrees are there between freezing point and boiling point",180,General,Wilhelm Steinitz was the world's first what in 1886,Chess Champion +General,Who was the only pope born in England,Adrian iv,General,Through which volcano did Jules Verne's explorers leave the centre of the Earth,Stromboli,General,Cultured pearls were first grown in which country,Japan +General,What is the name of Doctor Claws pet in Inspector Gadget,Madcat,General,"In what sport are Triffus, Miller and Rudolf moves",Trampolining,Food & Drink,Indian deep fried triangular pasty ,Samosas  +Sports & Leisure,From Which Country Do The Soccer Team Anderlecht Hail ,Belgium ,Music,What Type Of Instruments Do Associate With Stradivarious,Violins,General,In Which English County Was The 1970's book & Movie Set,Hampshire +General,What is South Carolinas official state dance,The Shag,General,Small hole for passing cord through,Eyelet,General,Plantalgia is pain where,Soles of feet +General,Mount Teide is the highest mountain in which country,Spain it's on Tenerife,General,"Any important face of a building, usually the principal front with the main entrance.",Facade,Music,Which composer was baptised with the forenames Johannes Chrysostamus Wolfgangus Theophilyus?,Mozart +Music,"Which Product Was Advertised Using The Clash's ""Should I Stay Or Should I Go"" Making The Record A UK Number Hit In 1991",Levi's,General,Which novel by H G Wells describes the invasion of Earth by martians,The war of the worlds,General,To which instrument does an orchestra normally tune,Oboe +Geography,What is the capital of Slovakia,Bratislava,General,What Is The Only Creature To Contain An Odd Number Of Whiskers,The Catfish,Art & Literature,"From which Shakespeare play is this line taken: ""Double, double ___ """,Macbeth +Geography,What Is The Most Westerly Capital City In Mainland Europe ,Lisbon (Portugal) ,General,Cape Verde is a former colony of which country,Portugal,General,In Queensland Australia pubs must still have what,Hitching rail for horses +Music,Which Country Singer Guested With The KLF On Their Single Justified & Ancient,Tammy Wynette,General,What in the 19th century were 'Cape Triangles'?,Postage Stamps,Geography,"On March 27, 1964, North America's strongest recorded earthquake, with a moment magnitude of 9.2, rocked central ______________",Alaska +General,Who invented the kinetoscope,Thomas edison,General,"What city in southern jordan did john burgon speak of when he described it as 'a rose-red city, half as old as time'",Petra,General,In 1990 there were 15000 accidents involving what,Vacuum cleaner +General,Kr is the chemical symbol for which element,Krypton,General,What are rain boots called,Galoshes, Geography,What body of water borders Saudi Arabia to the east?,Persian Gulf +General,The name Hilary comes from Latin meaning what,Cheerfulness,General,In 1897 who were the first baseball team introduce a ladies day,Washington Senators,General,Onomatophobia is the fear of,Hearing a certain word + Geography,What is the capital of Gambia ?,Banjul,Music,"Who Had A Hit In 1993 With ""I Have Nothing""",Whitney Houston,Sports & Leisure,On A Netball Players Bib What Do The Letters G.A. Stand For? ,Goal Attack  + Geography,Into what sea does the Elbe River flow?,North Sea,General,The Man Who Would be King was released in what year,1975,General,In Miami it is illegal for men to be seen in public wearing what,Any strapless gown +General,What city is Sugarloaf Mountain located in,Rio de janeiro,General,The malleus and the incus are two of the three auditory ossicles. Which is the third,Stapes,General,"In an average lifetime, the average american eats 84,775 _____",Crackers +General,What book was made into the first feature length British cartoon,Animal Farm in 1954,General,Ncaa: what team lost the men's basketball championship in 1985,Georgetown,General,Who was the first actress to endorse a product commercially,Lilly Langtree +General,Who is the famous giantkiller?,Jack,Food & Drink,Marsala is a type of this.,Sweet wine,General,What make of car is a 'Thunderbird'?,Ford +General,Copper gets its name from which Mediterranean country,Cyprus where it was first found,General,In the 1920s Dr Ida Rolf developed Rolfing as what alt therapy,Deep Massage,General,With which country did Britain break off diplomatic relations in April 1984,Libya +General,What does a Geophage do,Eats earth, History & Holidays,Which group had a UK No.1 in 1981 with The Song 'Ghost Town' ,The Specials ,General,Which International Singing Star Failed The Audition For Opportunity Knocks?,Engelbert Humperdink +Music,Name The One Colour Group And Their Best Selling One Colour Album From 1998,Simply Red / Blue, History & Holidays,Originally native to Mexico what is traditionally known as the Christmas flower ,The Poinsettia ,Geography,Of Which Country Is Nicosia The Capital ,Cyprus  +General,Planet 24 Is A Production Company Founded By Which Singer,Bob Geldof,Art & Literature,Which Character Invariably Misused English Words ,Mrs Malaprop ,General,How many sides does a snowflake have,Six +General,Candlestick maker,Butcher baker candlestick maker,Food & Drink,What name is given to the deep south stew or soup thickened with Okra? ,Gumbo ,General,The dybbuk is a creature in which peoples mythology,Jewish migrating soul enters body +General,Approximately how deep are the deepest mines? (in km),Four,General,The Napstar logo shows headphones on what,A cat,General,"At 30 miles long, in which North American city would you find Figueroa Street",Los angeles +General,In which country did Turkeys originate,USA,General,According to the ad At Benneton the smallest garment is a what,Condom,General,What dirty building did heracles clean by diverting a river,Augean stables +General,Which of King Arthur's knights survived his last battle,Sir Bedavere,General,What is the art of writing decoratively called,Calligraphy,General,The average person spends 12 years of their life doing what,Watching TV +General,Rex Stout created what corpulent orchid loving private eye,Nero Wolfe,General,Which organization was awarded the Nobel Peace Prize during WWll,The red cross,General,What is he the chemical symbol for,Helium +General,What is the fear of getting wrinkles known as,Rhytiphobia,Entertainment,Who played the murder victim in the original version of 'Psycho'?,Janet Leigh,Sports & Leisure,Who Won Nine Formula One Grand Prix Races In 2000 ,Michael Schumacher  +General,"Which American devised a noise reduction system, now in general use, for tape-recorders etc",Ray dolby,General,Which town stands at the mouth of the Great Ouse,Kings lynn,General,What is the fear of symmetry known as,Symmetrophobia +People & Places,Whom Did Neil Kinnock Succeed As Leader Of The Labour Party? ,Michael Foot ,General,What purple flower is the emblem of Scotland,The thistle thistle, History & Holidays,In what year did Lord Lucan leap into obscurity ,1974  +General,Who found the long lost explorer david livingston,Henry stanley,General,Peladophobia is the fear of,Bald people,General,Who were David Soul and Paul Michael Glaser better known as,Starsky and hutch +General,U.S. captials Louisiana,Baton rouge,General,If not a bird magician or a spitfire engine What is a Merlin,Artificial vagina,Geography,Which country hosted the 1982 World Cup of soccer,Spain +Science & Nature, The average __________ weighs 14 pounds.,Fox, Geography,What is the basic unit of currency for United Kingdom ?,Pound,General,Which vegetable is referred to as 'eggplant' in the U.S.A.,Aubergine +General,In Star Trek the Ferengi 10th rule of Acquisition what is eternal,Greed,General,Who opened for the Monkees on their 1968 American Tour,Jimi hendrix,Music,Name the thrash metal kings headed by guitarist Dave Mustaine?,Megadeath +General,What is the fear of the figure 8 known as,Octophobia,Sports & Leisure,In Which Sport Might You Play For The Sheffield Shield ,Australian Cricket ,General,How many feet are there in one fathom,Six +General,Scandinavian aquavit is flavoured with what,Cumin or Caraway,General,In which city is the Eiffel Tower,Paris,General,Real names Susan Alexandria Stage name from Great Gatsby,Sigourney Weaver + History & Holidays,"""What Was The Name Of The Character That Henry Travers Plays In The Movie """"It's A Wonderful Life"""""" ",Clarence Oddbody ,Religion & Mythology,What is a person who has made a pilgimage to Mecca?,Hajji,Music,Which Star Did Barbara Bach Marry In 1981,Ringo Starr +Food & Drink,"In 1999, which American retail giant announced that it was buying the Asda food retail chain? ",Wal-Mart ,Music,"""Livin La Vida Loca"" Was A 1999 No.1 For Which Singer",Ricky Martin,General,Elizabeth I had anthrophobia what was she afraid of,Roses +General,South Africa's Robben Island takes its name from which creatures,Seals,General,What did William Young invent in 1800,Different shoes left right,General,What year was the last woman hung in England,1955 +General, What does an ornithologist study,Birds,General,Which Country Has The Worlds Oldest Flag?,Wales,General,"A letter might end with SWAK, which is an acronym for",Sealed with a kiss +General,"In Knight Rider,what does K.I.T.T.'s name stand for?",Knight Industries Two Thousand,General,"Which gland, situated at the base of the skull regulates growth and metabolism",Pituitary gland,General,"Chronic transmissible disease, due to bacilius leprae, is better known as _____",Leprosy +Music,Which 3 ReMixed Sister Sledge Tracks Were Released In 1993,"We Are Family, Lost In Music, Thinking Of You",Music,"According To The Beatles Who has ""hair of floating sky""?",Julia,General,Nobody puts baby in the corner.,Dirty Dancing +General,In Which Country Was The Singer Mika Born,Lebanon,General,What is the national flower of Japan,Chrysanthemum,General,In Lawrence Kansas its illegal to carry what in your hat,Bees +Entertainment,Who killed Kenny?,They,General,What is the national religion of Scotland,Presbyterianism,General,Who is William H. Bonney,Billy the kid +General,Pinigerophobia is the fear of,Smothering,General,What is another name for the card game 'blackjack',21,Technology & Video Games,What game takes place on the fantastic island of Koholint? ,Link's Awakening +General,When are new states admitted to the u.s,"Noon, july 4th",Geography,What is the capital of Ethiopia,Addis ababa,General,What do you do to see phosphenes,Shut eyes flashing lights seen +General,In ancient China what meat was reserved for the Emperor,Pork,Music,"In Which Year did The Beatles Release ""Get Back"" Was It 1967, 1968,1969",1969,General,What is 'chowder' a type of,Thick soup +General,What line on a map connects places of equal rainfall,Isohyat,General,Who won the Wimbledon in 1972/73,Billie jean king,General,What speed did Marty have to reach in order to activate the flux capacitor?,88 miles an hour +General,What does elly may call her pets in the beverly hillbillies,Critters,General,What is pure china clay,Kaolin,Entertainment,For which film did Art Carney win best actor Oscar in 1974?,Harry and Tonto +General,What is the Capital of: Guadeloupe,Basse-terre, Geography,What is the capital of Gabon?,Libreville,General,"Name beginning with ""A"" meaning ""Little Father""",Attila +Science & Nature,How many pockets has a snooker table ,Six ,General,Which British airport has the identification code EMA,East midlands, Geography,What is the capital of Burkina Faso ?,Ouagadougou +Entertainment,"In 1987, who released her second album 'Solitude Standing'?",Suzanne Vega,General,"Who was known as the ""prince of light""",Thomas edison,General,What is jamaica's nickname,Regaa boyz +General,Who did Imran Khan marry in 1995,Jemima goldsmith,General,What is known as the Lost Continent ?,Atlantis,General,In which country is the volcano Cotopaxi,Ecuador + Geography,What country borders Egypt to the South?,Sudan,General,In Star Trek who would go to Sha Ka Ree,Vulcans it’s heaven,Music,Which Band Members Wanted To Be A 3 Minute Hero,The Selector +Science & Nature,What venomous serpent is known as the gentlemen among snakes ,The rattlesnake ,Sports & Leisure,Trent Bridge Cricket Ground Is In Which English City? ,Nottingham ,General,Bowl of red is the Aztec translation of which food item,Chilli +Music,The Brotherhood Of Man Recorded The Best Selling Single Of 1975 Can You Name It,Save All Your Kisses For Me, History & Holidays,Who was the first ghost appear to scrooge in the dickens classic a Christmas carol? ,Marleys Ghost ,General,What cocktail is made from brandy and white creme de menthe,Stinger +General,What percent of the world's water is potable (drinkable),1%,General,Why did Handel compose The Messiah,For Cash,Sports & Leisure,Hockey: The Los Angeles ________.,Kings +Music,Which Fraudulent Duo Told The Girl It Was True But Must Have Missed Her When They Were Found Out,Milli Vanilli,General,What is a group of sharks,Shiver,Music,"Which Duo Asked ""Are You Sure"" In 1961",The Allisons +General,Bristlemouths are the worlds most common what,Fish,General,How did Scrooge McDuck earn his first dime,Shining shoes,Music,Who Is The Lead Singer With The British Band The Verve,Richard Ashcroft +General,A story of ones own life,Autobiography,General,Which animals fur is used to make a musquash coat,Musk rat,General,Who first appeared in the Star Trek episode Space Seed,Khan +General,Hannibal had only one what,Eye after Rome attack,General,"Of what country did Napoleon make his brother, Jerome, king",Westphalia,General,"The small intestine is made up of the duodenum, the ileum and the ______",Jejenum +General,What nationality was the architect Le Corbusier,Swiss,General,From which element is pitchblende derived,Uranium,General,What holiday is celebrated on july 14th in French Polynesia,Bastille day +General,What nationality is Prince Phillip,Greek,Sports & Leisure,Who was the first NHL player to score 50 goals in one season,Maurice richard,General,What sort of stone floats on water,Pumice +General,In 1973 Pioneer 11 launched. First spacecraft to flyby,Saturn,Geography,Which Country In The World Is The Second Most Populous ,India ,Science & Nature," Gray __________ migrate 12,000 miles each year, farther than any other mammal.",Whales +General,What does NATO stand for ?,North Atlantic Treaty Organization,Sports & Leisure,In the world of Tennis how did Gunther Parche gain notoriety in 1993? ,Stabbed Monica Seles ,Music,"A Product Of The 80's Who Had A Hit In 1994 With ""Tell Me When""",The Human League +Art & Literature,"A termed coined by British art critic Roger Fry to refer to a group of nine-teenth century painters, who were dissatisfied with the limitations of impressionism. It has since been used to refer to various reactions against impressionism, such as fauvism and expressionism.",Postimpressionism,General,On what show would you find Gary Gnu?,The Great Space Coaster,Sports & Leisure,Which Sports Star Was Known as The Preston Plumber? ,Tom Finney  +General,Nashville is the capital of ______,Tennessee,People & Places,"What is the nickname of Jamew Gardner, friend of footballer Paul Gascoigne? ",Five Bellies ,General,Felix Hoffman discovered the worlds first synthetic drug 1897?,Aspirin +General,In the song where was the 'House of the Rising Sun' located,New orleans,General,200 years ago all white people knew what were deadly poison,Tomatoes,General,What country consumes the most coke per capita,Iceland +General,Who said Politics is the art of the possible 11 Aug 1867,Otto Von Bismarck,General,Tropical shrub used for making hair dye,Henna,General,What company produces Olympia beer,Miller in USA +Music,Which Single Brought Together The Eurythmics & Aretha Franklin In 1985,Sisters Are Doin It For Themselves,General,"Repetition of the initial letter (generally a consonant) or first sound of several words, marking the stressed syllables in a line of poetry or prose",Alliteration,General,Glassy mineral the red kind is used as a gem,Garnet +General,What do French speakers call the German town that Germans call Aachen,Aix-la-chapelle,General,What is the flower that stands for: bashfulness,Peony,Sports & Leisure,Who Was The First Footballer To Receive A Knighthood? ,Sir Stanley Matthews  + History & Holidays,Who Played The Title Role In The 1960 Hammer Production The Curse Of The Werewolf ,Oliver Reed ,General,Who had three breasts,Anne boleyn,General,Which popular cartoon strip has never included an adult,Peanuts +General,What is the name of the island that separates the two waterfalls at Niagara,Goat island,General,Pogonophobia is the fear of,Beards,General,Who invented the most common projection for world maps?,Gerardus Mercator +General,What's the opposite of the orient,The occident,General,With what branch of medicine is mesmer associated,Hypnotism,Sports & Leisure,What Nationality Was The Formula 1 Driver Ayrton Senna Who Was Tragically Killed In 1994 ,Brazilian  +Music,"This Group Had Their greatest Hit Between ""Texas Cowboys"" And Rollercoaster Name It",Swamp Thing / The Grid,General,Show How many tines are in a standard dinner fork,Four,General,In which Irish county is the Blarney Stone,Cork +General,What does WD stand for in WD 40 Water,Displacer,General,Port (left) was called what before Admiralty named it port in 1844,Larboard,Food & Drink,Who released the following album 'Bitches brew' ,Miles Davis  +Religion & Mythology,Poseidon was the Greek god of the ______?,Sea,General,Which 1977 film won seven Oscars but none for acting,Star Wars,General,In France who are nicknamed the Kepis blancs,Foreign Legion + History & Holidays,Who Is The Youngest Member Of The Rolling Stones ,Keith Richards ,General,What's a more proper name for artificial or false teeth,Dentures, Geography,Which is the largest lake in South America?,Lake Maracaibo +General,"During which conflict were the battles of Blore Heath, Wakefield, Northampton and Mortimer's Cross",Wars of the roses,General,"Which 2nd Division Football Club Play Their Home Games At ""Dean Court""",Bournemouth,Science & Nature,"What Is The Name Of The Wild Pig Of Of Central & South America That Has 3 Species Called Collared, White Lipped & Chaco ",Peccary  +General,What type of bird is a 'Khaki Campbell',Duck,General,Name the character played by Frank Sinatra in films such as Lady in Cement,Tony rome,General,Who patented the coin operated telephone patented,William gray +General,Dish served A la Crecy is garnished with what,Carrots,Geography,"What country covers more than 194,000 square miles of the iberain peninsula ",Spain , Geography,Where is the world's biggest prison camp?,Siberia + History & Holidays,What region is the world's largest exporter of Christmas trees? ,Nova Scotia ,Entertainment,Which was the first 'Indiana Jones' film?,Raiders Of The Lost Ark,General,"Literature: In HG Wells ""The Time Machine,"" two races of the future are the child-like Eloi, and the subterranean monsters called the ___. ?",Morlocks +Music,Who Sang Backing Vocals On U2's Pride,Chrissie Hynde,General,In The Simpson's what was the name of the Barbie type doll,Malibu Stacey,General,"Who Was The First Ever Sports Person To Be Interviewed On ""This Is Your Life""",Sir Stanley Matthews +General,What do you call the underground systems in both Paris & Newcastle,Metro,General,What is the skullcap worn by the men of Nigeria called,Fez,Religion & Mythology,He was the first King of the Hebrews.,Saul +General,What caused fjords,Glaciers,General,Merging the words 'melt' and 'weld' created which word,Meld,General,French word meaning growth is applied to top quality wines,Cru + History & Holidays,"Formed in 1955, with which island was the organisation known by the acronym EOKA associated? ",Cyprus ,General,O'Henry created which western character in a short story,The Cisco Kid,General,"What has no reflection, no shadow, & can't stand the smell of garlic",Vampire + Geography,What is the capital of Kansas?,Topeka,Science & Nature,Which Metal Can Be Beaten Into The Thinnest Film ,Gold ,General,What does a meteorologist study,The weather +General,What animal has the most taste buds over 27000,Catfish,General,What body part gets bigger as the day progresses,Feet 5 to 10%,Music,What Was The Name Of The Music Venue Where The Beatles Were Discovered,The Cavern Club +Sports & Leisure,Alan Shearer was the first player to score 200 Premiership goals. Who was the second? ,Andy Cole ,General,Flux which english physicist worked on thermodynamics and has a unit of energy named for him,James prescott joule,General,Gothenburg is the chief seaport of which country,Sweden +General,"Squid, octopus and cuttlefish are all types of what",Cephalopods,General,Which is the highest capital city in Europe,Madrid,Sports & Leisure,"Which Dart's commentator is known for his hyperbole such as (Alexander The Great was 33 when he conquered the known world, Bristow is 27)? ",Sid Waddell  +General,Baseballer Joe Schlabotnik's greatest fan,Charlie brown,General,What Is The Name Of The Murder Victim In The Board Game Cluedo,Dr Black,General,What kind of cowboy was John Voight in 1969,Midnight cowboy +General,Who was the owner of the soda fountain on sesame street,Mr hooper,Geography,On Whose Real Life Exploits Is James Clavells Novel Shogun Based ,Will Adams An Elizabethan Adventurer ,General,Which is the largest (in area) of the Australian States and Territories,Western australia +General,What Searchers hit was written by Sony Bono,Needles and Pins,Entertainment,Name Donald Duck's girlfriend,Daisy,General,Which two countries form what used to be called patagonia,Chile and +General,Who played 'johnny mnemonic',Keanu reeves,General,What parts of the rhubarb plant are poisonous,Leaves,General,In Astrology Aquarians are ruled by what planet,Uranus +General,"What is the dish of chopped avocado with onions, tomatoes, chilli and seasoning called",Guacamole,Music,"""Girlfriend"" Was A Hit In 1988 For Whom",Pebbles,General,The port of Oran is the second city of which North African country,Algeria +General,What were the names of the 3 mascots at the Sydney Olympic Games?,"Ollie, Millie & Syd", History & Holidays,In Which City Was John F Kennedy Assassinated? ,Dallas ,General,Jack What is the world's deepest lake,Lake Baikal +General,"In Greek mythology, to where did zeus abduct europa",Crete,Geography,Which river contains the most fresh water,Amazon,Science & Nature," The spiny cheek, starsnout poacher, and monkeyface prickleback are all names of __________",Fish +General,Edward Teach became famous as who,Blackbeard the Pirate,Sports & Leisure,How Long In Miles Is A Marathon ,26 , History & Holidays,In Which Year Was Mr Blobby A Christmas Number One In The Uk ,1993  +Music,Which Label Did Blondie Record Under For Their String Of Late 70's & Early 80's Hits,Chrysalis,General,"States who wrote ""three lives""",Gertrude stein,General,Dario Fo won the Nobel Prize for Literature in which year,1997 + History & Holidays,Which Famous Artist Was Shot And Wounded By Valeria Solanis In 1968 ,Andy Warhol ,Religion & Mythology,What is the last word in the New Testament?,Amen,People & Places,What Is The Most Common British Surname ,Smith  +General,"Who won an Oscar for his role in the film 'The Fugitive""",Tommy lee jones,People & Places,Which Former Presenter Of Grandstand Became A Cocaine Addict ,Frank Bough ,General,Which company made the Cross Your Heart bra,Playtex +Science & Nature, The giant __________ is the largest creature without a backbone. It weighs up to 2.5 tons and grows up to 55 feet long. Each eye is a foot or more in diameter.,Squid,General,Who's loretta lynn's singing sister,Crystal gayle,General,Who had a hit with November Rain,Guns n Roses +General,Whose daughter became the wealthiest three year old in 1988?,Christina onassis,General,Malabo is the capital of ______,Equatorial guinea,Art & Literature,"A style, c. 1520-1600, that arose in reaction to the harmony and proportion of the High Renaissance. It featured elongated, contorted poses, crowded canvases, and harsh lighting and coloring.",Mannerism +General,In what Australian state would you find Parkes,New south wales nsw,Music,To Which All Saint Did Robbie Williams Become Engaged In 1998,Nicole Appleton, Geography,In which country would you find Angkor Wat?,Cambodia +General,What was the name of the first 'talking movie',The jazz singer,General,Which species of fir is named after the plant collector. who sent its seeds back to Britain in 1827,Douglas fir,Toys & Games,"Word derived from ""shah mat"", from the arabic for ""the king is dead""",Checkmate +Entertainment,Singer Paula ______?,Abdul, Geography,The Auckland Islands belong to which country?,New Zealand,General,Who sang 'jet airliner',Steve miller +General,This branch of physics deals with the general laws governing the motion of material objects,Mechanics,General,Presley elvis presley appeared on how many stamps in 1993,500 million,General,"A horizontal projection, such as a balcony or beam, supported at one end only.",Cantilever +General,"""Its all Greek to me"" comes from what Shakespeare play",Julius Caesar,General,What country joins Central America to South America,Panama,General,Which traditional japanese sport takes place in a circular ring,Sumo +General,Jefferson city is the capital of ______,Missouri,Music,With Which Band Is Shirley manson The Lead Singer With,Garbage,Music,"Which band finished 7th with the song `Mary Ann` for the UK in the 1979 Eurovision song contest, before later going on to have a massive party hit in the UK several years later?",Black Lace +General,What did Tommy Kirk shoot because of hydrophobia,Old yeller,General,What is a xerophobic afraid of,Deserts,Music,"Who Had A Hit With ""We Didn't Start The Fire""",Billy Joel +Geography,"Miami, Florida, is the most southerly major city in the continental United States, sitting about two degrees north of the ____________________",Tropic of cancer,Food & Drink,What is the name of the apple based liqueur from Normandy? ,Calvados ,Food & Drink,What is the smell given off by wine called? ,The Boquet  + Geography,Which mainland Latin American country is in neither South America nor Central America?,Mexico,Music,Who Recorded Telstar In 1962,The Tornados,General,Scarlet O'Hara had what original first name,Patsy +Art & Literature,"Who Co-Wrote (Yeoman Of The Guard), (Lolanthe And The Mikado) ",Gilbert & Sullivan ,General,The seven-branched candlestick called the Menorah is the official state emblem and appears on the president's flag of which country,Israel,General,What is the Capital of: Belarus,Minsk +Sports & Leisure,How Many Lanes Does An Olympic Standard Swimming Pool Have ,Eight ,General,What love song featured elton john and kiki dee,Don't go breaking my heart,General,"What is the name for a legal document, used in court, in which a person swears that certain facts are true",Affadavit +General,In 2000 what word was written on Sydney bridge in fireworks,Eternity,General,Which planet is fourth closest to the Sun,Mars,General,The Russians used what to cure piles,Potato suppositories +General,Which city is built on 118 islands,Venice,General,What is a dried plum called?,Prune,Music,"Which Artist Has Spent The Most Time In The UK Charts Clocking Up Over 1,100 Weeks",Elvis Presley +General,Who painted 'The 3rd of May' (1808),Francisco de goya,General,What is the only New England state without a seacoast,Vermont, History & Holidays,Name the 2 of the 3 flags placed by Hillary in the summit of Everest in 1953? ,"British, Nepal and the UN " +General,"Venus, Aphrodite and Hathor are all goddesses of what",Love,General,"Who said ""Sometimes too much drink is barely enough""",Mark Twain,General,Whose song did the Beatles sing on first TV appearance 1962,Roy Orbison Dream Baby +General,What is the name of Fred Flintstones paperboy,Arnold,General,What year did sir richard whittington die,1423,General,To which dog was a statue erected in Edinburgh,Greyfriers Bobby +Music,Which Group Spawned A Teenage Army Of Tartan Scarf Waving Fans,The Bay City Rollers,General,What was the name of Matt Dillon's band in Singles?,Citizen Dick,Science & Nature,Which is the worlds tallest grass ,Bamboo  +General,What is a tightrope walker,Funambulist,General,What was unusual - beauty contest judge Percy Moorby 1985,He was Blind,General,How long is an eon,One billion years +General,BONANZA: What clothing did Little Joe usually wear,Green jacket and grey,General,Starts with F ends with K if you cant get one you use your hand,Fork,General,What simple kitchen utensil is used to get lumps out of flour,Sieve + History & Holidays,Which French Ruler Was Finally Defeated In 1815 ,Napoleon ,General,What does the mighty Thor throw at his enemies,Hammer,Entertainment,"In a 1976 release, who wanted to 'fly like an eagle'?",Steve Miller Band + History & Holidays,Who Released The 70's Album Entitled Trans-Europe Express ,Kraftwerk ,General,What ancient middle east kingdom spread to it's greatest extent under king ashurnasirpal in 663 bc,Assyria,General,The white marks intersecting each five yard line are called ________,Hashmarks +General,What games name literally means To Grope Frantically,Scrabble,General,In what sport is the danger flag yellow,Motor racing,General,Who rode a horse called Phantom,Zorro +Music,"Faithless, Absolute & Hypnotize Were 80's Hits For Which Group",Scritti Politti,General,What is the flower that stands for: deception,White cherry,General,Barbra Streisand starred in the sequel to Funny Girl what was it called,Funny lady +Art & Literature,"From which Shakespeare play is this line taken: Goodnight, goodnight! parting is such sweet sorrow, That I should say goodnight till it be morrow.",Romeo and juliet,General,In WW2 what was the German codename for invasion of Russia,Barberossa,General,What Was The First & Obviously The Oldest State In The USA,Delaware +General,Who was the minstrel that found Richard I imprisoned,Blondel (De Nesle),General,"Which group sang the Song ""Last Resort""?",Papa Roach,General,Which film star is the real life husband of Goldie Hawn,Kurt russell +Food & Drink,What is the minimum age for scotch whisky before it can be sold in the uk? ,3 Years ,Science & Nature, A young pigeon that has not yet flown is a __________,Squab, History & Holidays,Who sang the song 'Cry' which was the first video on Mtv to use morphing technology through the entire video? ,Godley and Creme  +Art & Literature,"Which Author Wrote The Sound & The Fury, The Wild Palms, And As I Lay Dying ",William Faulkner ,Entertainment,Secret Identities: Jimmy Olson,Elastic lad,General,Who was world champion in boxing from 1952-1962,Archie moore +Science & Nature,Name The American Who Patented A Burglar Proof Lock Described As Magic & Infallible ,Linus Yale ,Music,"His Recording Of ""I'll Never Get Out Of This World Alive"" Charted Just A Few Days Before His Death In January 1953 Name The Country Star",Hank Williams,General,What does a blue flag white cross mean in motor racing,Give way or be disqualified +General,What country celebrates its National Day on 14th July?,France,General,Optophobia is the fear of,Opening one's eyes,General,Who married actress nancy davis,Ronald reagan +General,Which singer is known as the 'Empress of the Blues'?,Bessie Smith,General,"What's a hockey team's ""blue line corps""",Defensemen,Science & Nature, About 24 newborn opossums can fit in a teaspoon. They are about .07 ounce at __________,Birth +General,"In ballet, a lowering of the body by bending the knee.",Fondu, History & Holidays,Sputnik 2 Was Launched Into Space In 1957 What Was The Name Of The Dog That Was On Board ,Laika ,General,Who was known in Germany as Der Bingle,Bing Crosby +General,What is the 'bole' of a tree,Trunk,Geography,In Which Country Is Mount Etna ,Sicily ,Science & Nature,What does a sphygmomanometer measure?,Blood pressure +Music,Who introduced Paul to John?,Ivan Vaughn,General,All members of'which religion bear the surname Singh,Sikhism,General,"In the first part of 'hard to kill', what did steven seagal use to kill the mobster",Credit card +Food & Drink,Which Ancient Central American Civilisation Were Drinking Chocolate Milk As Long Ago As 600 BC ,Mayans , Geography,What is the world's widest river?,Amazon,General,Pulque is a beer based on what,Cactus +General,What is William Hague's middle name,Jefferson, History & Holidays,What time would you watch 'Late Night With David Letterman' on NBC? ,12:30-1:30 ,General,Neopharmaphobia is the fear of,New drugs +General,The angel shark has what other name,The monkfish,General,Hearts bells leaves and acorns card suits which country,Germany,Music,Who Was Shakin All Over At The Start Of The 60's,Johnny Kidd And The Pirates +General,What is the only dog to have a barb on each individual hair follicle,Dalmatian, History & Holidays,"""What is the connection between """"Comet"""", """"Cupid"""" and """"Vixen""""?"" ",All Names Of Santa's Reindeers ,Science & Nature,Approximately how many pounds of salt is in every gallon of seawater?,One quarter +General,St Appolonia Patron Saint of what,Toothache,General,What is the common name of Eucalyptus microtheca,Coolabah tree (waltzing mathilda),General,"In 'the wizard of oz', what was dorothy's dog's name",Toto +General,40% of MacDonald's profits come from selling what,Happy Meals,General,What major city is close to the middle of the Iberian Peninsula,Madrid,General,"Cocktails: creme de cacao, cream, and brandy make a(n) __________",Brandy +General,Who sailed to the Antarctic in the ship Discovery,Scott amundsen,General,Bill Medly was part of what group,The Righteous Brothers,General,Which city is served by ringway airport,Manchester +Sports & Leisure,"In pro football a ""sudden death"" period lasts how many minutes long?",Fifteen,General,If an Australian called you a Gumsucker what would he mean,You were from Victoria state,General,What does the dominican republic have on its flag,Bible +Geography,What is the capital of Poland,Warsaw,Sports & Leisure,What Is The Highest Possible Out Shot In A Game Of Darts ,170 (t20 t20 Bull) ,General,What is the full name of E. Coli,Escherichia coli +Sports & Leisure,What Are Goldie & Isis ,Reserve Boats For The Boat Race,Science & Nature,A one_humped camel is called a _________.,Dromedary,General,Frankfurt stands on what river,Main +General,Who composed the music for the opera The Tales of Hoffman,Jacques Offenbach,Music,What is meant by the musical instruction largo?,"Slow, dignified in style",General,In France in what kind of meat does a Charcuterie specialise,Pork +General,In which Welsh town is the Royal Mint,Llantrisant, History & Holidays,What is the name of the German Officer who lived and died in Madrid and rescued Mussolini?,Otto Skorzeny,Food & Drink,Where is Scotland's largest malt whisky distillery? ,Tomatin  +Music,"What Part Did Tina Turner Play In Ken Russell's Movie ""Tommy""",The Acid Queen,General,What is the Hellenic republic,Greece, History & Holidays,Who invented the cotton gin?,Eli Whitney +Science & Nature,What is the meaning of the name of the constellation Draco ?,Dragon, History & Holidays,"Which character in a Disney cartoon, wanted to make a coat out of 101 Dalmatians ",Cruella Devil ,General,What is the roughly circular hollow feature on the top of a volcano called,Caldera +General,"Wile E. Coyote, Supra Genius, gets all his traps to try to catch the Road Runner from what company",Acme,Art & Literature,"From which Shakespeare play is this line taken: ""Goodnight, goodnight! Parting is such sweet sorrow, That I should say goodnight till it be morrow.""",Romeo and Juliet,General,Which golfer has won the British Open most times since 1945,Tom watson (5) +General,In Montana it is a felony for a wife to open her husbands what,Mail,General,In Blythe Ca. you cant wear cowboy boots unless you own what,Minimum 5 head of cattle,Sports & Leisure,What Major Sporting Milestone Occurred On 6 th May 1954 ,First Sub 4 Minute Mile  +General,Vaccinophobia is the fear of ______,Vaccination,General,What remained at the bottom of pandora's box,Hope,General,Name the first space probe to land on the moon 13 Sept 1959,Luna 2 +General,Who was the first American to win a Nobel prize,Theodore Roosevelt,General,Second city: Salt Lake City (state),West valley city,General,In which Middle Eastern country is the site of the Hanging Gardens of Babylon understood to have been,Iraq +General,What happens every 45 seconds in the USA,House Fire, History & Holidays,"The state motto for oklahoma is labor omnia vincit, which means what ",Labor conquers all things ,General,In Kentucky people wearing what on streets get police protection,Bathing Suits +General,What is the Capital of: Comoros,Moroni,General,What is the second day of the week,Monday,General,What kind of hat took its name from a George Du Maurier novel,Trilby +Science & Nature,Which soft metal is used in the production of soap? ,Sodium ,General,John lennon and yoko ono marry in gibralter on march 20th and honeymoon in,Paris,General,Who Is The Greek God Of Love ,Eros  +General,From the Earth to the sun is one AU what does AU stand for,Astronomical Unit,General,"What was the name of the heroic boy in ""The Never Ending Story?",Atreyu,General,How many floors are in the empire state building,102 +Music,In 1972 Mary Whitehouse Campaigned Furiously To Get Which Song Performed By An American Solo Artist Banned As She Deemed It Offensive,My Ding A Ling,Science & Nature,"In Which Country Is Cherepovets, Site Of One Of The Worlds Largest Blast Furnaces? ","In Russia , At The Cherepovets Works ",General,"A welt, a vamp, a tongue and a quarter can all be found on which object",A shoe +Entertainment,"The eldest sister in the TV Series Charmed, is played by who?",Shannon Doherty,General,Structure for hanging criminals,Gallows,General,What kind of bird is a bourbon,Turkey +People & Places,Who Was Humphrey Bogarts 4th Wife ? ,Lauren Bacall ,General,Who wrote the classic thriller 'The Birds',Alfred Hitchcock,General,Sable is the heraldic name for which colour,Black +Sports & Leisure,Between 1950 And 2000 Name Three Tennis Players Born in Germany That Won The Men's Singles Title At Wimbledon? (PFE) ,"Boris Becker , John McEnroe & Michael Stich ", History & Holidays,Oklahoma's recorded History began in 1541 when what spanish explorer ventured through the area on his quest for the lost city of gold ,Coronado ,General,U.S. captials - virginia,Richmond +General,"Cool, Nice and Oz are all types of what?",Computer Languages,General,Lancelot where is 'the breadbasket of russia',Ukraine,People & Places,In 1995 Which Comedien Dissapeared For Several Days After Receiving Adverse Reviews ,Stephen Fry  +General,"What have woodpecker scalps, porpoise teeth and giraffe tails all been used as",Money,General,"Who said ""Today Europe tomorrow the world""?",Adolf Hitler,General,Which spirit was once known as kill devil,Rum +General,Who was the first person to make a million pounds out of playing golf,Arnold Palmer,General,Love what does encephalitus affect,Brain,General,Which Common Expression Is Said To Derive From The Name Of 2 Inns In The Village Of Stony Stratford,A Cock & Bull Story +Music,Which male vocal range is pitched between tenor and bass?,Baritone,General,Collective nouns - A congress or flange of what,Baboons,General,What famously happened on 6th August 1945,The atom bomb was dropped on hiroshima +General,More than 100 women make a living from impersonating who,Marilyn Monroe, Geography,What is the basic unit of currency for Ethiopia ?,Birr,General,Vincent Price Made His Last Big Screen Appearance In Which Movie,Edward Scissorhands +General,What fleshy muscular organ is joined to the hyoid bone,Tongue,General,"Excellency Which company, during the 1984 Super Bowl, aired what is considered one of the best commercials in TV history",Apple, History & Holidays,"What was the instrument of execution during the ""Reign of Terror""?",Guillotine +General,"Where Exactly Were The Band ""The Bee Gees"" Actually Born",The Isle Of Man, Geography,Where is Tongeren?,Belgium,General,What is a group of bats,Colony +Entertainment,Who is Snoopy's arch enemy?,The Red Baron,Science & Nature,Which British City Street Had The First Gas Lamps ,Pall Mall London In 1807 ,Music,Which Popstars Rejects Topped The Charts In 2002,Liberty X +General,In Milan citizens can be fined $100 if they don’t always do what,Smile - Not Hospital and Funerals,General,"In boxing, what is a blow to the back of the head",Rabbit punch,Music,Who Was The First Beatle To Have A Solo Number One Hit In The Uk,George Harrison (My Sweet Lord) +General,In the UK the Clerk of the Closet is the Queens official what,Chaplin,General,What is the fourth month of the year,April,General,Gary Mabbut Was The Last Person To Do What In An FA Cup Final In 1987,Score At Both Ends +General,"Either of two saturated hydrocarbons, or alkanes, with the chemical formula C4H10",Butane,General,What substance was used to build the Kinga Chapel in Poland?,Salt,General,"What gift was given on the 10th day, according to ""The 12 Days of Christmas",Ten leaping lords ten lords a leaping +General,What is the drummer's name in 'the muppet show',Animal,Music,Survivir Sang About The Eye Of The What,Tiger, History & Holidays,Which eighties fashion accessory consisted of a saftey pin and small beads? ,Friendship pins  +General,"There are 2 annual publications devoted to the peerage. One is Burkes Peerage, what is the other",Debretts,People & Places,Who In Music Is Known As The Boss ,Bruce Springsteen ,Sports & Leisure,Who Became The BBC Sports Personality Of The Year In 2006? ,Zara Phillips  +General,Which Character On Television Has The Surname “ Sagdiyev ” ?,Borat,General,Who wrote Dr Zhivago,Boris Pasternak,Music,"Who Had A Smash Hit With The Song ""Don't Worry Be Happy""",Bobby McFerrin +General,Portion of the central nervous system contained within the skull,Brain,General,What sort of music is intended for a room rather than a large auditorium,Chamber,General,Reagan and Nancy Davis What 19th century novelist spent his last days as an inspector at New York's Customs House,Herman Melville +General,Where is the guggenheim museum,New york city,Art & Literature,"Stephen King's: ""The Dead ________"".",Zone,General,In the 18th century offensive what were whipped,Books +General,"In physics, process of reduction of matter into a denser form, as in the liquefaction of vapor or steam.",Condensation,General,-ism: The belief in the existence of a god or gods.,Theism,General,Over 400 films have been made based on the plays of which famous writer,Shakespeare +General,Who Is The Only Carry On Star To Have Won As Oscar,Jim Dale,General,What is the sum of 75 + 23 + 84 - 101 + 18,Ninety nine,General,Fill in the blank: dont ____ be happy,Worry +General,What shape is the set of all points in a plane equidistant from a certain point,Circle,Music,Number of Beatles-related websites listed on Dave Haber's website,52,Food & Drink,"In the Arab world, to eat or share what with someone is an expression for hospitality ? ",Salt  +General,What percentage of the earths weight is taken up by the oceans,Less than 1,Science & Nature,Name one male fish that gives birth?,Sea horse or pipe fish,Music,Let Me Be You're Fantasy Was A Massive Dance Hit For Whom,Baby D +Entertainment,Who directed the classic thriller 'The Birds'?,Alfred Hitchcock, History & Holidays,In which city was President Kennedy killed,Dallas,General,"Upon his death in 1931, all non essential lights in the U.S. were turned off for one minute in his honor",Thomas edison +General,What is another name for the star fruit,Carambula,General,The Mantu and Heath tests check for what infectious disease,Tuberculosis,General,Where is mount washington,New hampshire +Toys & Games,How many pieces are found in a chess set ?,32,General,Strip where is 'the strip' that was designated an official scenic byway,Las vegas,General,Spain Portugal and Algeria are three top produces of what,Cork + Language,"Other than Germany, whose official language is German?",Austria,General,What number appeared on the side of the car 'Herbie' in the series of films?,53,General,Which bird is the symbol of the Royal Society Protection Birds,Avocet +General,"Which Actress Turned Singer Released Their 4th Album In 2009 Entitled ""Come To Life""",Natalie Imbruglia,General,"Which recording by 10 cc contains the lines 9 keep your picture on the wall, it hides a nasty stain that's lying there.""",I'm not in love,General,What is a rhinocerous horn made of,Hair +General,Which king of England met the king of France on the field of the cloth of gold,Henry viii,General,Which planet is closest to the Sun,Mercury,General,A condition causing breathing difficulties,Asthma +General,"Richard Drew produced an all purpose sticky tape, under what name was it sold in Europe",Sellotape,Music,Name The 1976 One Hitv P.M Wonder Of Thar Starland Vocal Band,Afternoon Delight, History & Holidays,"The most famous indian relocation to oklahoma was by the cherokee indians, what was the route called ",Trail of tears  +General,Which band recorded the live album 'Strangers in the Night',Ufo,People & Places,Which Former Husband And Wife Provide A Link Between The Films Death Becomes Her And Disclosure (PFE)? ,Bruce Willis And Demi Moore ,General,Who is the Patron Saint of cooks,St Laurence +People & Places,What Surrey town is famed for its salts?,Epsom,General,U.S. Captials - South Dakota,Pierre,General,The back of what item is called a gore,A Sock +Geography,The U.S. state that contains the most square miles of inland water is ______________,Alaska,General,Schubert's fourth symphony is nicknamed the what,Tragic,General,Thousand on what river is liverpool,Mersey +General,"What was Higgins first name in ""Magnum PI""",Jonathan,General,Who argued it out in 'the kitchen debate',Richard nixon and nikita,General,What kind of fruit were prunes before they shrivelled,Plums +General,Who directed Serpico,Sidney Lumet,General,This world class model recently crashed in a helicopter,Christie brinkley,General,What is made by the crush tear curl process,Tea + History & Holidays,How Many Gold Rings In Total Did My True Love Give To Me (Think About It) ,40 (5x8=40) ,General,Surveyed 70% of US females said they preferred this to sex what,Chocolate,General,"Common name for a large sea turtle, named for the color of its fat, although the animal is brownish overall",Green turtle +Science & Nature,What Function Does The Labyrinth In Your Ear Have ,It Maintains Your Balance ,Science & Nature, The female condor lays a single egg once every __________,2 years,General,What's the only female animal that has antlers,Caribou +Geography,Which country has Budapest as its capital,Hungary,General,"Who was the ""great pyramid"" built for",Cheops,General,"What 80's game show featured the ""Whammy""?",Press Your Luck +Sports & Leisure,What Is The Most Popular Finishing Double In Professional Darts? ,Double 16 ,Music,With Which Instrument Is Earl Scruggs Associated,The Banjo,General,Which dog breed gets its name from German meaning to splash,The Poodle + History & Holidays,Who was the first person to break the sound barrier?,Chuck Yeager,Technology & Video Games,What colour is the 'Black box' on commercial planes?,Orange,General,"Who was the German National Socialist official, notorious as the head of the Nazi police forces",Himmler +General,What is the common name for many species of burrowing mollusks,Clam,General,Who is woody woodpecker's girlfriend,Winnie woodpecker,Music,"What Instrument Was ""Sackbut"" The Old Name For",The Trombone +Music,In The Film Risky Business Tom Cruise Danced Around His Living Room In His Underpants To What Song,Old Time Rock & Roll,General,What is the flower that stands for: delicate beauty,Hibiscus,General,In what film did Paul Robeson sing Old Man River,Showboat +General,What was the first country to have a public monorail system,Japan,General,"What was Spode the first to make in 1797, by adding bone ash to hard paste porcelain",Bone china, History & Holidays,"The invention of what in 1867, made Alfred Nobel famous? ",Dynamite  +General,The lumbar spine consists of how many vertebrae,Five,General,What was the name of John Steinbeck's dog Charley,Standard Poodle,Music,"The Monkees ""I'm A Believer"" Was A Hit In What Year",1967 +General,Which country and western singer is known as the 'okie from muskogee',Merle,General,In America its noise is B flat in England its G what is,Buzz of Electric Razor,Science & Nature,Which Bone In The Body Is Broken Most Frequenly ,The Collar Bone  +General,"Panther Cap', 'Stinkhorn' and 'Penny Bun' are types of which plants",Fungi,General,And what country won it,Sweden,General,What do Americans call 'candy floss',Cotton candy +General,How many freckles did howdy doody have,Forty eight,General,The Hubble telescope is named after this astronomer?,Edwin Hubble,Sports & Leisure,Irishman Stephen Roche Won Which Sporting Event In 1987 ,The Tour De France  +Music,Which 1970's Group Featured The Longmuir Brothers?,Bay City Rollers,General,What is one of the flavors that make up neopolitan ice cream,Strawberry,General,Review This show suspends all belief it will never work what ?,Original Star Trek +General,Which US state has a buffalo or bison on its flag,Wyoming,General,Who wrote the song 'do they know it's christmas' with midge ure,Bob geldof,General,What is the family name of the Dukes of Somerset,Seymour +General,What does bbl mean,Be back later,General,What was the name of the Soviet Security Service immediately after the October Revolution,Cheka,General,In which country was Pilsner beer originally brewed,Czech republic +General,In which state is Cornell University,New york,Food & Drink,"Even though it tastes nothing like grapes, a __________ is often eaten for breakfast.",Grapefruit,General,What exploded in 1720,The South Sea Bubble +General,Which author wrote the two World War 2 books 'Fighter' and 'Bomber',Len deighton, History & Holidays,What two mountain ranges did hannibal and his elephants march through in 218 b.c. ,The pyrenees and alps ,Science & Nature,This animal's shell is used to make attractive jewelry.,Abalone +General,What did president lincoln proclaim a national holiday in 1863,Thanksgiving, Geography,What is the most mountainous country in Europe?,Switzerland,Music,"What day of the week is mentioned in ""I Am The Walrus""?",Tuesday +General,In June of what year was the treaty ending WWl signed,1919,General,Who always had 56 curls in her hair,Shirley temple,General,Declan macmanus is better known to us as,Elvis costello +General,What west coast nfl team has the motto 'commitment to excellence',Oakland, Geography,What is the basic unit of currency for Mongolia ?,Tugrik,General,Which tropic passes through Taiwan,Tropic of cancer +Music,Which DJ & Recording Artist Is Known As The House Godfather,Frankie Knuckes,General,Which Former World Champion Was Nicknamed The Black Cloud,Larry Holmes,General,Collective nouns a rhumba of what,Rattlesnakes +General,What is the study of soil,Paedology, Language,What is 'blackpool' in Irish?,Dubh linn,General,Who played trixie delight in the film 'paper moon',Madeline kahn +General,Which TV series was narrated by Walter Winchell,The Untouchables,General,Why could you find a hoist and a fly,On a flag,General,Time magazine lists the second most powerful person in Washington in 1993,Hillary rodham clinton +General,What do Ethiopians spend,Birrs,General, A receptacle for holy water is a(n) ________.,Font,General,Who wrote the book Coral Island,R. m. ballantyne +Music,Which 2 Boys Spent 12 Weeks In The Charts Together With A Re-Mixed Surfari's Track,Beach Boys & Fat Boys (Wipeout),General,What us state includes the telephone area code 610,Pennsylvania,Geography,Which U.S. state borders a Canadian territory,Alaska +General,56% of men have had sex where,At Work,General,What's another word for stewardess,Air hostess,Sports & Leisure,What Is The Dart Player Eric Bristows Nickname ,The Crafty Cockney  +People & Places,How Did Dmaon Hill's Father Graham Die ? ,In A Plane Crash ,Science & Nature,Who developed the vaccine for smallpox ?,Edward Jenner,General,The study of shells,Conchology +General,A 'roux' is produced when making which food,Sauce,General,Baseball: the new york ______,Mets,General,What name is given to a statistician employed by an insurance company to calculate risks,An actuary +General,In which musical work would you hear the song As Long As He Needs Me,Oliver,General,"Name the small fried cakes made from chick peas, eaten in the Middle East",Falafel,Science & Nature,What Does Omnivorous Mean? ,Feeds On Any Food  +General,What is the nickname for Texas,Lone star state,Music,"Was Llouyd Cole's Band Called ""The Rattlesnakes"" Or The Commotions",The Commotions,Entertainment,What film featured a cat named Mr. Bigglesworth?,Austin Powers +General,What was ALF's girlfriend from Melmac's name?,Rhonda,General,Which European city is called Mailand by the Germans,Milan,General,Who wrote 'the hobbit',J.r.r tolkien +Geography,In Which Country Will You Find The Holiday Resort Bodrum ,Turkey ,General,What character Tamed the Shrew in the Shakespeare play,Petrucchio,General,Which car company produced the first front wheel drive 1934,Citroen +Music,"Liza Minelli Recorded An Album With The ""Pet Shop Boys"" Or ""David Bowie""",The Pet Shop Boys,General,What is a group of this animal called: Lion,Pride,General,Disney's __________ was featured on cereal boxes for the Post cereal Toasties corn flakes back in 1935. ,Mickey mouse +Geography,What Is The Principle Language Of Brazil ,Portuguese ,General,The Teletubbies have two favourite foods. Name one.,Tubby toast tubby custard, Geography,What is the capital of Nepal?,Kathmandu +General,For which ad campaign was the line 'i can't believe i ate the whole thing' used,Alka seltzer,General,Which number could not be represented in the Roman numbering system,Zero,General,Which of the Beatles group played piano on Don't Pass Me By,Ringo starr + History & Holidays,"When the choctaw indians were moved to oklahoma,they brought their crack police force with them. what were they called ",Lighthorsemen ,General,As what is Hungary also known,Magyar,General,Who painted Marriage d la Mode in 1743,William hogarth +General,Who was Tommy Lee Jones' freshman roommate at Harvard?,Al Gore,Geography,Which Scandinavian Capital Begins And Ends With The Same Letter? ,Oslo ,General,In 1964 Jett Bock composed the music for what hit show,Fiddler on the Roof +Food & Drink,"Bourbon, sugar and mint make up which type of Cocktail ",Mint Julep ,General,What is the equivalent RAF rank to Sub-lieutenant RN and Lieutenant in the army,Flying officer,General,"""Mr Boddy"" is the murder victim in what board game",Clue +General,How many individual bets make up a Yankee,Eleven,General,Which country's national flag consists only of a green field,Libya,General,Menophobia is the fear of,Menstruation +General,What ocean separates North America from Europe,Atlantic,General,Bond: What was the first James Bond film,Dr. No,Entertainment,"This magic word was in the movie, ""Mary Poppins"".",Supercalifragilisticexpialidocious + History & Holidays,When was the Greek alphabet first used?,800 BC,General,Which associate of Ricky Gervase played the character known as 'The Idiot Abroad' in a series of comedy travel documentaries?,Karl Pilkington,General,"Whose story did the books 'born free', 'living free' and 'forever free' tell",The lioness elsa +General,"In medical matters, what does the letter C stand for in C.A.T. scanner",Computerised,General,Cross what day of the week did solomon grundy die,Saturday,General,In what US state is the town of Maggie's Nipples,Wyoming +General,What name is given to a doctor who specialises in skin disorders,Dermatologist, History & Holidays,"These fighters always began a bout by saying, ""Hail Emperor, those about to die salute you.""?",Gladiators,General,For who did the song '867-5309/jenny' spawn a lawsuit,Tommy tutone +General,What is the commonest name for a pub in Britain,The Red Lion,Science & Nature,What Are Gonads ,The Male Or Female Sex Organs ,People & Places,In Whose Car Did Mary Jo Kopechne Die At Chappaquidick ,Edward Kennedy  +General,"What's the radical feminist word for ""history""",Herstory,General,What country had RCH on its cars?,Chile,Music,Number of Beatles fanzines currently published worldwide,44 +General,Who was the first woman to win 4 consecutive US tennis open,Chris Evert Lloyd,General,Who is the ventriloquist who created Lamb Chop,Shari Lewis,General,"What percentage of your weight is water 30, 50 or 70 percent",70 +General,What is a bangtail,Perforated tag,General,Who played 'uncle tim' in 'my favourite martian',Ray walston,General,What is a calm ocean region near the equator,Doldrums + History & Holidays,Released in 1978 what were the names of the two gangs which John Travolta and Olivia Newton John belonged to in the movie `Grease'? ,The T-Birds and The Pink Ladies ,Geography,What is the capital of Czech Republic,Prague,Geography,What is the capital of Malta,Valletta +General,How many chromosomes do each body cell contain,Forty six,Entertainment,Who directed 'The Shining'?,Stanley Kubrick, Geography,What are drumlins and eskers formed by?,Glaciers +Geography,What is the capital of Romania,Bucharest, History & Holidays,What Did Frenchman Andre Turcat Do On 2nd March 1969 ,Flew Concordes Maiden Voyage ,General,A woman described as a Magdalene in the 17th Century was a repentant what,Prostitute +General,In which California city does Poltergeist take place?,Cuesta Verde,General,Ascorbic acid & sodium ascorbate are the most common forms of which vitamin,Vitamin c,Art & Literature,"In one of Donald Horne's novels, which was 'the lucky country'?",Australia +General,"Where did over 300 defiant Indians head off to, after Sitting Bull was killed",Wounded knee,Sports & Leisure,Who Was The First Person To Beat Muhammed Ali in a Professional Fight? ,Joe Frazier ,General,What was Michael Jackson advertising when he was nearly killed?,Pepsi +Sports & Leisure,A Statue Of Billy Bremner Stands Outside The Ground Of Which Football Ground? ,Leeds United ,General,In Idaho you cant give a citizen something more than 50lb - what,Gift of candy,General,Who married to Jenny Von Westphalen in 1843,Karl marx +Science & Nature,What's the term for the hybrid offspring if a male lion and a female tiger ,Liger ,General,"He released the parody ""oh you ate one too"" in 1988 which included the song ""Cabo Wabo""",Van Halen,Entertainment,Who was the first singer in Genesis?,Peter Gabriel +Music,Which 1985 Song Was Co Written By Michael Jackson And Lionel Richie,We Are The World,Geography,"________________, of the southern Baja Peninsula, was favored by early pirates because of its safe harbors.",Los cabos,General,Sportsman are most likely to get diseased anuses / rectum,Water Skiers +General,"Which classic film of 1948 was advertised as, 'Greed, Gold and Gunplay on a Mexican Mountain of Malice'",Treasure of the sierra madre,Art & Literature,Who Wrote About Tarzan ,Edgar Rice Burroughs ,General,Which Roman ampitheatre was built by Emperor Vespasian in around 70ad,Colosseum + History & Holidays,How Does Good King Wencelesas Like To Eat His Pizza ,Deep Pan Crisp And Even ,General,What is a group of swallows,Flight,General,What type of food is coulibac,Russian Fish Pie + History & Holidays,Who Released The 70's Album Entitled John Barleycorn Must Die ,Traffic ,General,What is a mexican afternoon nap,Siesta,General,What's the most popular name for a female pet cat,Samantha +General,New jersey has a spoon featuring over _________spoons from every state and almost every country,5400,General,What is a hen of the woods,A mushroom,General,"The study of populations of animals & plants, a population being a group of interbreeding organisms in a specific region; for example, the members of a fish species in a lake",Population biology +Science & Nature, The __________ whale is the mammal with the heaviest brain _ about six times heavier than a human's.,Sperm,Sports & Leisure,How old was Boris Becker when he won his first ever Wimbledon singles Championship? ,17 ,General,Flies and humans can both get which condition,Athletes Foot +Music,James Todd Smith Is The Real Name Of Which Singer,LL Cool J,Science & Nature,What is the luminous intensity of light measured in ?,Candela,General,"The arrangement of individual characters of a particular typeface into words, sentences, paragraphs, & so on, for the purpose of printing & publishing (printing; type)",Typesetting +General,What are the first names of the popular early 80's duo hall and oates,Darryl, History & Holidays,Who appeared on the back of a US banknote in 1875?,Pocahontas,General,In which fictional land did 'a girl and her dog travel to the Emerald City',Oz +General,What were the wicks in the Vestal Virgins lamps made from,Asbestos,Geography,Which European capital City is nicknamed (the city of a hundred spires) and has tourist attractions including St Vitus Cathedral and Charles Bridge? ,Prague ,Music,In Paul Hardcastle's Hit Song What Was The Average Age Of The Combat Soldier During The Vietnam War,19 +General,The Beatles Were The 1st British Group To Have A Us No.1 Single Who Was The 2nd,The Animals,General,The Jefferson's was a spinoff of what show?,All in the Family,General,What did Moses do for a living before he was called by God,Shepard +General,You can ski on the piste but what other sport uses the term,Fencing where the fight happens,General,What mixture is used to calm crying babies,Gripe water,Music,Chris Stein Worked With Which Sex Siren To Produce A String Of 80's Hits,Deborah Harry +General,What's the main feature of a Chong Sang skirt,Split up side,Music,The Kane Gang Split To Become Who,Hue & Cry,General,What nationality was Aladdin,Chinese +General,What is the fear of cosmic phenomenon known as,Kosmikophobia,General,"Where would you find bow, bay and traytime parts of what",Male deer's antlers,General,What does a kayser measure,Waves +General,What is the literal meaning of the title Viceroy,In place of the King,General,Vootery is the practice of what,Deceit or Lying,Entertainment,Where did George of the Jungle live,Imgwee gwee valley +General,What French cheese is ripened in caves,Roquefort ewes milk,General,Who largely took over ontario for a month in the 19th century,Irish rebels,Music,"What Was Advertised In 1998 With A Photo Of One Of John & Yoko's Bed Ins & Slogans ""Think Different""",Apple Computers +General,Which planet was discovered in 1846,Neptune,General,Which food did Victorians deride as little bags of mystery,Sausages,General,Who first appeared in the cartoon strip Thimble Theatre,Olive Oyl +General,What is the most frequently seen comet?,Encke,Sports & Leisure,Hockey: The Toronto _______.,Maple Leafs,Geography,What is the capital of Macedonia,Skopje +General,Name award shaped like a teapot with a skull and crossbones,Agatha - for crime mystery writers,Music,"Who Has Written Songs With Mike Love , Tony Asher & Van Dyke Parks",Brian Wilson,Geography,"What U.S. state is known as The Land of 10,000 Lakes",Minnesota +Mathematics,The angles inside a square total _______ degrees.,360,Geography,From which London Station Do Trains To The Channel Tunnel Leave From ,Waterloo ,General,Who invented the carpet sweeper in 1876,Melville bissell +General,What was the first gramophone record made from,Tinfoil,Entertainment,"Who played the 'Wicked Witch of the West' in ""The Wizard of Oz""",Margaret Hamilton,General,China which is the most remote island in the southern atlantic ocean,Bouvet island +Music,"Who Am I ""frederick Bulsara (1964-1991)",Freddie Mercury, History & Holidays,Who Was The Last British Monarch To Lead His Forces Into Battle? ,George II ,General,What did thousands ogle for the first time when it served as a pace car for a race in Alabama in 1964,The mustang mustang +General,"-isms: Public ownership of the basic means of production, distribution, and exchange.",Socialism,General,What gas is produced in plants in the process of photosynthesis,Oxygen, History & Holidays,On the 1st of July of what year was the British colony of Hong Kong returned to China? ,1997  +General,Where was slave trading abolished in 1807,British empire,General,Which James Bond Movie Was The Last To Use A Title Actually Written By Ian Flemming,The Living Daylights,General,Where did nikki lauda get his disfiguring burns,Nurburgring +General,It is estimated that at any give time 1% world’s population is what,Drunk,General,"Who sang the song ""Run To You""?",Bryan Adams,General,Prova from provolone means what,Ball shaped +Music,"Which Swedish Female Vovalist Used ""Open Sesame"" To Enter The Charts",Leila K,General,What is the Capital of: Montserrat,Plymouth,General,"What animals name is Aboriginal for ""no drink""",Koala +Music,Which Song Was Used To Launch MTV In America,Video Killed The Radio Star,General,In fable who pulled the thorn from the lions paw - not eaten,Androcles,People & Places,What Part Of Betty Grable Was Insured For Over A Million Dollars ,Her Legs  +General,In what Indian city is the Taj Mahal located,Agra,Food & Drink,What type of meat is used in the preparation of Osso Bucco? ,Veal ,General,What do the initials IOU stand for on an IOU,I Owe Unto + Geography,What is the basic unit of currency for Morocco ?,Dirham,General,Which Star Died During The Filming Of The Movie “Game Of Death” In 1973?,Bruce Lee,Music,Brett Michaels Was The Lead Singer Of Which Band,Poison +General,What was the first Bond film not to be titled from a Bond book,Licence to Kill,Science & Nature,What Was Built At Obrusk In The USSR ,The Worlds First Nuclear Power Station ,Art & Literature,Who Painted The Mona Lisa ,Leonardo Da Vinci  +General,What is the measure of monitor screen quality,Resolution,General,What is the magazine of the Salvation Army called,The Warcry,General,Who is known in England as the tatty detective,Columbo +General,On what common object would you find a sleeve and a tray,A Matchbox,Entertainment,Who drew the comic 'The Maxx'?,Sam Keith,General,Name the primeval supercontinent which split into Gondwanaland and Laurasia between 250 and 300 million years ago,Pangaea +General,"Parr, Smolt and Grilse different names same thing what",Salmon - life stages,General,"Who was marshall james butler ""wild bill"" hickock's sidekick",Jingles,General,What is Damson Cheese,Thick Damson Jam +General,"Where was ""Conozca Beatles"" released",Mexico,General,What is the culinary term meaning to coat or sprinkle with flour or sugar,Dredging,General,"Who Was The Youngest Person Ever To Appear On ""This Is Your Life"" Aged Just 19",Twiggy +General,William Perks became more famous as who,Bill Wyman,General,Volcanic lake in Romania:,Lake st ana,General,By what other name is the 180 degree meridian known as,International date line +General,"Which city, capital of the Assyrian Empire, was destroyed by fire in 612 B.C.",Nineveh,General,What medical procedure is said to work by manipulating the body’s electrical energy flow?,Acupuncture,General,Who composed a Paris and a Prague Symphony,Mozart +General,Under what structure was the first nuclear reactor built in Chicago,Football stadium,General,25 years after first playing James Bond Sean Connery won an Oscar for his part in which film,The untouchables,Music,Which 3 Bands Has Johnny Marr Been Associated With (PFE),"The Smiths, The The, Electronic" +People & Places,Which Celebrity Launched A Perfume Range Entitled 'M' Just In Time For Christmas 2007 ,Mariah Carey ,General,Xavier Roberts was the name associated with which eighties toy?,Cabbage Patch Kids,Sports & Leisure,At Which Course Is The British Grand Prix Held? ,Silverstone  +Sports & Leisure,In Athletics Track Races What Does The Ringing Of A Bell Signify ,The Last Lap ,General,With which art form would you associate the name Karsh of Ottawa,Photography,General,Who wrote Lolita,Vladimir nabokov +General,Bob van Winkle changed his name to what,Vanilla Ice,General,"Countries of the world: landlocked country in central South America, the capital is Asuncion",Paraguay,General,A moist fertile spot in a desert is called a(n),Oasis +Music,In The World Of Music Monica & Gabriela Irimia Are Otherwise Known As Who?,The Cheeky Girls,General,What was abolished by the english parliament in 1647,Christmas,General,Advertising film which is informative and purportedly objective,Infomercial +General,Osteoporosis primarily affects,Bones,General,In The Tv Show Auf Wiedersehen Pet What Was The First Name Of The character Oz,Leonard,General,What is MMM minus MD,MD 3000-1500=1500 +Music,"When playing the bagpipes, what name is given to the “melody” pipe, played with one or two hands.",The CHANTER,General,What is the stage name of film actress Betty Joan Perske born 1924,Lauren bacall,General,"In medicine, of what is oncology the study'",Tumours +General,What city is signified by the 'd' stamped on some american coins,Denver,General,"In The Godfather, who played the role of Mo Green",Alex rocco,General,Not Including The Word OK Or Any Brand Names What Word Is Pronounced The Same In Over 36 Countries Worldwide,Taxi +General,What is the most common blood type in humans,Type o,General,What is the main job of a striker in soccer,Score goals,Art & Literature,Who Wrote The Novel Lady Chatterly's Lover ,DH Lawrence  +General,Red white blue yellow green what's missing from Rubik's cube,Orange,General,The Bermuda Bowl is world championship in which game,Bridge, Geography,What is the capital of Pakistan ?,Islamabad +General,Which American crowned a great 1998 by winning at Wentworth,Mark o'neara,Sports & Leisure,"Other than skiing, which sport takes place on a piste?",Fencing,General,What was Supergirl's secret identity,Linda Lee Danvers +General,Bulls Blood wine comes from which country ,Hungary,General,Who wrote the surreal novel Tarantula,Bob Dylan,General,What planet is often referred to as Earth's sister planet,Venus + History & Holidays,Which singer sang 'I Believe In Father Christmas'' ,I Believe In Father Christmas ,General,U.S. Captials - Alaska,Juneau,General,In the Bible which book follows John,Acts of the Apostles +General,What should be done with a used worn out flag,Burned,General,To whom is the Wizard of Oz dedicated,The Young in Heart,Music,In what year did the 'Pretenders' release their first LP?,1980 +General,What was the real name of the hospital on St. Elsewhere?,St. Eligius,General,Francis Scott Key Born On August 1st 1779 Is Credited With Writing Which Song,The Star Spangled Banner / American National Anthem,General,Member of male non enclosed religious order,Friar +General,Johannes Ostermeir invented which photographic aid,Flashbulbs,General,"""It only takes a _____ to get a fire going""",Spark,General,Which Major League baseball team has its stadium in South Bronx,New york yankees +General,An assisted reproductive technology (art) in what one or more eggs are fertilized outside a female's body,Invitro fertilization,General,Which novel features Room 101,1984,Entertainment,"Who played the lead in the movie ""Mission Impossible""?",Tom Cruise +General,In what Australian state would you find whyalla,South australia,Food & Drink,For Which beer is Rutland famous? ,Ruddles ,General,"What sexually ambigious prisonmate was often dubbed ""Mrs. Hitler""",Rudolf hess +General,Name two self cleaning organs,Eye Vagina,General,Who Is The Only Person Outside Formula One To Have Been Awarded The BBC Sports Personality Twice,Henry Cooper,General,By what name is the skin condition called 'naevus' better known,Strawberry mark +General,Who owned the yacht Lady Ghislane,Robert Maxwell,General,What is the most chosen name for US schools sports teams,Eagles or Tigers,Entertainment,"Darth Vader was the villan in the movie, ""____ Wars"".",Star + History & Holidays,Ghost Town' was the only number single released by which British band ,Specials ,General,What is a 'palas',Queen of spades,General,What is the collective noun for a group of tigers,An ambush +General,What us state includes the telephone area code 608,Wisconsin, Geography,Rabat is the capital of which country?,Morocco,General,In which century was the poet Robert Frost born,19th +Geography,Which is the largest lake in the British Isles? ,Lough Neagh in Northern Ireland , History & Holidays,Who is recognised as the father of geometry?,Euclid,Science & Nature,This branch of medicine deals with old age and its diseases.,Geriatrics +Sports & Leisure,Who was the first NHL player to score 50 goals in one season?,"Maurice Richard <-- pronounced ""Reeee-shard""",General,What collective name is given to the four largest moons of Jupiter?,Galilean Moons,Music,Who underwent treatment for rabies following an incident in which he bit the head off a live bat?,Ozzy Osbourne +General,"In Which Us State Was Rapper And Hip Hop Artist ""Eminem"" Born",Missouri,Food & Drink,From Which Fruit Is Grenadine Obtained? ,Pomegranate ,General,What was the last black and white film to win Oscar best film,The Apartment 1960 Jack Lemon +General,Aviatophobia is a fear of ______,Flying,General,What song is about a sheep stealing suicide,Waltzing Matilda,General,What are the names of donald duck's nephews,Huey dewey and louey +Science & Nature, A quarter horse gets its name from its speed in running the __________,Quarter_mile,Technology & Video Games,What was the Jaguar Car called before 1945?,SS,General,Which creature appears on the Samoan flag,Eagle +General,"What was the setting for walk, don't run",Tokyo olympics, History & Holidays,The Monument in the City Of London is a monument to which event? ,The Great Fire Of London ,General,What is an igloo built out of,Snow +Music,What is Ray Charles' real last name?,Robinson,General,What city in the USA has the fewest % of native born residents,Huston - Texas,General,"Of what are throat, foxing and platform parts",Shoe +General,Which is the largest known butterfly,Queen alexandra's birdwing,General,What are young bats called,Pups,General,Which Australian author wrote Illywhacker and Oscar and Lucinda,Peter carey +General,Terrence Nezman became more famous as who,Stanley Kubrick,General,What is usually sprinkled on top of cappuccino,Chocolate,General,Who voices the cowboy doll 'Woody' in the film 'Toy Story',Tom hanks +General,Where was the first Pony Express set up,Outer Mongolia, History & Holidays,"Traditionally are the 12 Days of Christmas, before or after Christmas ",After (26 Dec - 06 Jan) ,General,What is the young of this animal called: Grouse,Cheeper +General,Denzil Washington's first film as director was what,Finding Fish,General,What is Panphopia a fear of?,Everything,General,Fragrant plant with edible leaf-stalks and seeds,Fennel +General,"Which South African Country (NOT South Africa) Banned The Book ""Black Beauty"" After Claiming It's Title Was Racist",Namibia,General,What is the common name for the marine animals asteroidea,Starfish,Music,"Who originally recorded the Beatles' cover song, ""Chains""?",The Cookies +General,Who wrote the novel' The Turn of the Screw',Henry james,General,By law Las Cruces New Mexico can't carry what in mainstreet,Lunchbox,General,The star constellation Lepus has what English name,The Hare +General,Which Tv Legend Plays The Captain In The New Series Of Fort Boyard ?,Tom Baker,Toys & Games,What game is Bobby Fischer associated with?,Chess,General,What sport features snatches and clean jerks,Weightlifting +General,"In 'coronation street', who is ken and denise's son",Daniel,General,Where will children as young as 15 be jailed for cheating on their finals,Bangladesh,Food & Drink,From which flower does the expensive spice of saffron come from ? ,Crocus  +Music,Whiose first solo hit was Spread A Little Happiness in 1982?,Sting,General,To what great civilization/empire do we owe our custom of 60 minutes in an hour,Babylonian empire,Music,A picture of Burt Bacharaach appeared on the cover of which best selling 90's album?,Definitely/Maybe +General,Approximately how many times a minute does lightning strike the earth,Six,Music,Which R&B Bad Boy Changed His Name To Abdul Rahman In 1998,Mark Morrison,General,Where in your body is your patella,Knee ( it's the kneecap ) +General,In The Classic 80's Video Prince Charming By Adam And The Ants Who Played The Role Of The Fairy God Mother,Diana Dors,General,The Pinotage grape is a native of which country,South africa,General,King George III is remembered in which children's nursery rhyme,The Grand old Duke of York +General,What is a pharaoh,King of egypt,Music,Who Had A Monkey Gone To Heaven In 1989,Pixies, History & Holidays,On what was Pennsylvania incorrectly spelled?,Liberty Bell + History & Holidays,Which British alternative band takes its name from a character in the film To Kill a Mockingbird ? ,The Boo Radleys ,General,Where could you hear the Cuckoo Song,Laurel Hardy film theme song,Geography,"Apart from water, what runs through the mouth of the River Amazon and Lake Victoria? ",The Equator  +General,Point Maley is the coast guard cutter in what Disney movie,Boatniks,General,"Messidor, Thermidor and Fructidor were what in 1789 (JJA)",French revolutionary calendar months,General,Which Song By Michael Jackson Spent The Most Weeks At No.1 In The UK Singles Chart,Earth Song +General,"Who recorded the album ""nebraska"" in 1982",Bruce springsteen,General,Which kitchen appliance was first designed in 1927 by the American Charles Strite,Automatic toaster,Science & Nature,What word is used for a male ass (Other than that the word used for that,Jack +General,"Other than martha washington, which two women have been represented on u.s currency",Pocahontas and susan b anthony,Music,"Who Had A Hit With ""If I Could Turn Back Time""",Cher,Art & Literature,Which English Author Created Horatio Hornblower ,C.S. Forester  +General,What creature has a penis 20 times the length of its body,Barnacle,Science & Nature, A snake is capable of eating an animal four times larger than the width of its own __________,Head,General,What tome did Nazis give newlyweds & parents of large families,Mein kampf + Geography,What is the basic unit of currency for Switzerland ?,Franc,General,CANAM is a major competition in what 'sport',Cheerleading,General,Where is Huracan stadium,Buenos aires +Science & Nature," A group of bees can be called either a hive, a swarm, or a __________",Grist,Food & Drink,What Are 'Ladies Fingers' Better Known As ,Okra Or Bhindi , History & Holidays,Which Royal House Ruled England Between 1603 & 1714? ,The Stuarts  +Music,"How Are ""Florence Ballard & Mary Wilson"" More Commonly Known",The Supremes,General,Britain's call it sellotape - What's the brand name in Australia,Durex,Science & Nature,What Is Also Known As The Freshwater Lobster? ,The Crayfish  +General,Name the Australian film about the pianist Halstadt,Shine,General,In 1998 the space probe Luna Perfecta found what on the moon,Ice,General,What company first guaranteed satisfaction or your money back,Montgomery Ward 1874 +General,"In 1925 Grace Scurr, A Secretary Invented & Named Which Product Now Synonomous With The 1980's?",The Filofax,General,The royal yacht Britannia is now moored in the port area of which city,Edinburgh,General,On what date Greek Russian orthodox churches celebrate Xmas,January 6th +General,In Which Sport Might You Perform “ A Double Axel ” & “ A Lutz ” ?,Ice Skating,General,What is the meaning of the word 'Dodecanese',Twelve islands,General,We know what a bronco is but what does it mean in Spanish,Rough +General,Which Latin American author wrote 'The War of the End of the World',Mario vargas llosa,Art & Literature,Paint applied very thickly. It often projects from the picture surface. ,Impasto,General,Who was the first losing candidate in a U.S. presidential election,Thomas jefferson +General,Who was king of the Franks from 768 to 814 a.d,Charlemagne,General,Who wrote the scripts for Hill street Blues,Steven Bochco,General,Marie Gresholtz is better known as who,Madam Tussaud +Sports & Leisure,Who Ended Up Winning The Formula One Drivers Championship In 2007 ,Kimi Raikkonen ,Science & Nature,Which Hi Tech Gadget Which Can Also Be A Bit Of A Pest Was Invented By Douglas Englebart In 1964 ,Computer Mouse ,General,In which creatures does formic acid occur,Ants +General,Which German city was the birthplace of the poet Heinrich Heine,Dusseldorf,Food & Drink,According to Ken Dodd what are mined in Knotty Ash? ,Jam Butties ,General,"Name the fat, rich detective with a passion for beer, food and orchids",Nero +General,What's the capital of the Comoros,Moroni,General,What does Ally Sheedy use to decorate her picture in the Breakfast Club?,Dandruf,General,"What was the nickname of Charles Heidsick, the 19th Century French wine producer",Champagne charlie +Music,Before Changing It To Bob Dylan What Was This Legends Name,Robert Zimmerman,Entertainment,"Sung by Robert Palmer, '______ to love'?",Addicted,General,When light waves pass from one medium into another they change direction. This is called ________,Refraction +General,Details of what can be found in The Blue Book,US Aristocracy,Music,Johnny Halliday Was A Massive Star Over Three Decades In Which Country,France,General,Volvo's chairman resigned in 1993 in protest of a merger with this automaker.,Renault +General,What is the heaviest snake,Anaconda,General,Which was the first country to give women the right to vote,New zealand,General,Which Band Was Originally Known As Feedback?,U2 +Geography,"Excluding Alaska, the American state with the lowest population density is?",Wyoming,General,This is the name of a fairy in A Midsummer Nights Dream,Moth,General,What poet wrote how do i love thee,Elizabeth barrett browning +General,Who painted The Blue Boy,Thomas gainsborough,General,"In an ancient Roman house, a central room open to the sky, usually having a pool for the collection of rainwater. In churches, a front courtyard. ",Atrium,General,"Who wrote 'the agony and the ecstasy""",Irving stone +Music,Name Either Of Chopin's Christian Names,Frederic Froncois,Science & Nature,What Was The Name Of The First Orbiting Space Station ,Salytut ,Music,"Members Of The Buggles, King Crimson & Yes Went On To Form Which Band",Asia +Music,"Who Had A Hit With ""Sweet Dreams Are Made Of This""",The Eurythmics,General,You would find a mummy in one of these stone coffins,Sarcophagus,General,Which animals make a sound called nuzzing,Camels +General,"Which Safety Device First Constructed & Used In 1982, Is Known By Its Creators As “Hybrid 3”",Crash Test Dummy,General,In Kansas the law prohibits shooting rabbits from where,A Motorboat,General,First ad on Radio Luxemburg 1930s for Bible Beans - which are?,Laxatives +General,The Intelligent whale was the nickname of an early what,Submarine,General,What is the largest planet in the solar system,Jupiter,General,In what series of stories did Inspector Lestrade appear,Sherlock Holmes +Tech & Video Games,What video game starts out with Koma Pigs stealing your treasured bracelet? ,Tomba!,Entertainment,Who played the Agent james Bond in the 1966 film 'Casino Royale'?,David Niven,General,On which river is the city of Florence situated.,Arno + History & Holidays,This French peasant girl led the army to victories.,Joan of Arc,General,Automysophobia is a fear of ______,Being dirty,Geography,What is the Capital of Barbados Called? ,Bridgetown  +General,What distant planet circles the sun every 84 years,Uranus,General,"What Type Of Foodstuff Was Invented In The Early 1900's By ""Charles Jung""",The Fortune Cookie, History & Holidays,In what type of building did Plato and Aristotle teach ?,Gymnasium +General,According to the ancient Chinese what cures headaches,Swinging your arms,General,Hans Steininger had the world longest what - that killed him,Beard - Tripped over it down stairs,General,What does a philluminist collect,Match box labels +General,What is the longest insect,Walking stick,General,When was the cash register invented,1879,Geography,What is the capital of Bahrain,Manama +Food & Drink,In which dishes is sticky rice used? ,Sushi ,General,What animal in The Jungle book is also a type of uncut velvet,Bagheera,Geography,In which state are the finger lakes ,New york  +General,Who sang the 1975 remake of the Flamingos' I Only Have Eyes For You,Art,General,Hydra Gyrum was the Latin name for which element,Mercury - Hg,Entertainment,Vincent Vega appeared in which movie?,Pulp Fiction +General,Which year did Jemima Goldsmith marry Imram Kahn,1995,General,In 1999 Dallas Texas passed a law banning what from city,Roosters noise pollute,General,By law in Guam who are not allowed to marry,Virgins - They pay men to pop em +General,What was the name of Roses monkey in Friends,Marcel,General,Who received seven perfect scores in gymnastics at the 76 montreal olympics,Nadia comaneci,General,How do you catch a Phart,With a net it’s a fish +Science & Nature,Who invented the smallpox vaccine?,Edward Jenner,General,What do you take dramamine for,Motion sickness, Geography,Where is the bridge of San Luis Rey?,Peru +General,Fear of haircuts is called,Tonsurephobia,General,What is the nickname of the Derbyshire One-Day Cricket team?,Falcons,General,"If yoU.S.aw the word 'sag' on an Indian menu, which vegetable would it signify",Spinach +General,Which star of films such as 'The Philadelphia Story' died in 1997,James stewart,General,The Audi car company created by August Horch – means what Latin,Bear,General,It’s a flock of sheep what's a group of owls called,Parliament +General,Which English King had the most legitimate children (18),Edward I,General,What vegetable did Gregor Mendel use in his studies,Peas,General,What American state is the Badger state,Wisconsin +General,Photophobia is fear of ______,Light,General,Ireland and New Zealand are the only countries that lack what,Native Snakes,Geography,Into what sea does the Mackenzie River flow,Beaufort +General,Which country made the worlds first feature film in 1906,Australia Story of Kelly gang,General,Who introduced the first designer collection for men,Pierre cardin,General,What was the title of William Golding's book about boys marooned on an island in the Pacific,Lord of the flies +Science & Nature,From 1979 until 2000 the most distant planet from the earth was _______.,Neptune, History & Holidays,Which country was previously known as the Dutch East Indies? ,Indonesia ,General,Danes Overst Senap Spanish Marques de Marina what in UK,Colonel Mustard Cluedo +General,What's the world's largest Gulf?,Gulf of mexico,General,What geological era preceeded the cenozoic,Mesozoic,People & Places,By what name is Arthur Leech better known as?,Cary Grant +General,"What do the pig, horse and rat have in common",Chinese astrology,General,What do the initials U.F.O stand for,Unidentified flying object,General,Who did Fess Parker play in on TV 1964,Daniel Boone +Entertainment,Where does Yogi Bear Live?,Jellystone Park,General,Branch of medicine dealing with the care of old people,Geriatrics,General,Who left three pt boat tie clasps on top of the yukon's mount kennedy in 1965,Robert kennedy +General,What was unusual about Tyrell's car in the 1976 Spanish G Prix,Six Wheels,General,What's the longest tendon in the body,Achilles tendon,Music,Who Told Us That It's Only Make Believe In 1978,Child +General,"Mr & Mrs Dursley Of No. 4 Privet Drive,Were Proud To Say That They Were Perfectly Normal Is the opening passage to which famous book?",Harry Potter & The Philosphers Stone,General,In Tv & Film The Character Of “ Selena Kyle ” Is More Commonly Known As Whom?,Catwoman,Music,"Which singer was Michael Jacksons massive hit ""Ben"" Originally intended for",Donny Osmond +General,Who was murdered in Bohemia in 929,Good King Wenceslas,General,What would you do with a soft cock,Wear it - it’s a wig,General,What river winds through Hell's Canyon,Snake +General,Nosocomephobia is the fear of,Hospitals, History & Holidays,21 October 1805 Is The Anniversary Of Which Famous Battle ,Trafalgar ,General,How many sides does a heptagon have,Seven +General,Where did the British Brown Bess musket get its name,Thin line of uniform brown rust,General,Who denied jesus three times,Peter,General,What was the name of the Akla-Seltzer boy?,Speedy +General,Which actress said 'Sometimes I'm so sweet even l can't stand it',Julie andrews,Sports & Leisure,Which Swimming Stroke Was Introduced Into Competition In 1952 ,Butterfly ,General,In 1919 in the USA you could be jailed for doing what in Public,Sneezing +General,What sort of animal is a fennec,Desert Fox,Sports & Leisure,What Is The Weight Of A Cricket Ball In Ounces ,5.5 - 5.75 Ounces ,General,Which Test match team plays home Cricket matches at the Kensington Oval?,West Indies +General,In what country did bongo drums originate,Cuba,Science & Nature," While some sharks lay eggs, blue sharks give birth to live __________, as do about two_thirds of all sharks, estimated at nearly 350 species.",Pups,Music,What Was The Beatles 8 th Album Released On 1 st June 1967?,Sgt Peppers Lonely Hearts Club Band +General,Phil Collins won an Oscar for Best Original Song in 2000. In which film,Tarzan,Music,Who Claimed To Have Friday On My Mind,The Easybeats,General,Which ovine expression is used to describe a wishful amorous glance,Sheep's eyes +General,India became independent in what year,1947,General,There are 16 bottles of Champagne in a what,Balthazar,General,"Who was known as ""the yankee clipper""",Joe dimaggio +Music,What Is Paul McCartneys Middle Name,Paul His First Name Is James,General,In WW2 what came between Sword and Gold,Juno - D Day Beaches,General,"In Greek mythology, who turned arachne into a spider",Athena +Sports & Leisure,Which English Footballer Was Sent Off In The 1998 World Cup ,David Beckham , Geography,In what country is the highest point in South America?,Argentina,General,A male goose,Gander +General,"In the tv series 'leave it to beaver', what was the mother's name",June, History & Holidays,He led 900 followers in a mass suicide in 1979.,Jim Jones,Music,What Was The First Uk Hit For Gladys Knight And The Pips,Take Me To Your Arms And Love Me +General,What Everyday Object Was Invented By Don Wetzel In 1968,The Cash Machine,Music,Which Singer Was Born In Canada And Has The Real Name Eileen Edwards?,Shania Twain,General,Whats the largest man made structure visable from space,Great wall of china +General,What album got arrowsmith a gold lp in 1975,Get your wings,Science & Nature,"This dry, warm wind flows eastward down the slopes of the Rocky Mountains.",Chinook,General,Bardolatry is an excessive admiration for which writer,William shakespeare +General,What order of insects contains the most species,Beetles,General,Australians call someone from where a cockroach,New South Wales,General,Michael Dumble-Smith became famous as who,Michael Crawford +General,What is the slogan for 'a sprinkle a day helps keep odour away',Shower to, Geography,Where is Beacon Street?,Boston,Entertainment,Who is the lead singer of limp bizkit?,Fred Durst +Geography,How Many Times Was Tower Bridge Raised In 1990 ,460 Times ,Science & Nature, The skin of baby __________ is so transparent that one can actually see the milk flowing into them as they nurse.,Mice,General,"At andrew Jackson's funeral in 1845, his pet parrot had to be removed. Why",Because it was swearing +General,In Greek mythology who was the Goddess of Chastity,Artemis – sister Apollo,General,What year did George Washington become U.S. president,1789,General,What's the difference between sleeping gorillas and men,Gorillas don’t snore + History & Holidays,Christmas Island Is A Territory Belonging To Which Country ,Australia ,General,The Adi Granth is the Holy Book of which religion,Sikh,General,What is the covering on the tip of a shoelace,Iglet +Science & Nature,What Is A Pencils Lead made From ,Graphite ,General,Which state has the only royal palace in the United States?,Hawaii,General,Selachophobia Is The Morbid Fear Of Which Highly Dangerous Creatures?,Sharks +General,What common sign derived from the Medici family crest,Pawnbrokers balls,General,What term is given to the seasonal movement of animals especially birds and fish,Migration,General,Only 30% of women do what,Swallow +Toys & Games,Where on your body would you wear flippers,Feet,General,Reuben James was the first what 31 Oct 1941,US warship sunk by submarine in WW2,General,Which Indian tribe is mainly associated with the state of Florida,Seminole +General,What U.S. state was once an independent republic,Texas,General,What is exalting one's country above all others,Nationalism,General,What was the most significant battle fought on Belgian soil in 1815,The battle of waterloo battle of waterloo +Food & Drink,Where was Budweiser first brewed?,St. Louis,Science & Nature,"Which Mammel And Scavenger Has Brown, Spotted And Striped Varieties? ",The Hyena , History & Holidays,"What show did the catch phrase, 'Yeah, That's The Ticket' originate on? ",Saturday Night Live  +General,In Massachusetts its illegal to have what in the bathroom,A lightswitch,General,What is the term given to mammals which have hands that can grip,Primates,General,In ancient India what was cut off adulterers,Noses - and they tried to hide it +General,In what game do countries play for the Venice cup,Contract Bridge,General,Choroti women are expected to do what during sex,Spit in their partners face,General,On what river is rome built,Tiber +General,What is an onychophagist,A nail biter, History & Holidays,Which Famous Person Of Film And TV Wrote The Autobiography Tall Dark And Gruesome ,Christopher Lee ,General,"Set of written symbols, each representing a given sound or sounds, which can be variously combined to form all the words of a language",Alphabet +General,What novel centres on the romances of Ursula and Gudrun Brangwen,Women in love,General,On average what one action makes a man live 13 years longer,Castration,General,Athens 1896 Paris 1900 St Louis 1904 London 1908 what next,Stockholm 1912 – Olympic venues +General,"What's the common name for the disease that doctors call ""rubella""",German measles,General,"Name given to that part of North America first seen in or about 986 by Bjarni Herjlfsson, who was driven there by a storm during a voyage from Iceland to Greenland",Vinland, History & Holidays,Who was Henry VIII's second wife?,Anne Boleyn +Geography,"For centuries, Spain's _________________ has been and still is one of the world's largest.",Fishing fleet,General,In 1974 Last Americans evacuated from,Saigon,General,The Arabs call it Al-Maghrib what do we call it,Morocco +General,In Israel what unexpected item is certified Kosher,Postage Stamps,General,Who was the 1990 wimbledon women's singles runner-up,Zina garrison,General,"In World War II, what was ""Operation Torch""",Allied invasion of north africa +General,In Albany New York what's it illegal to do in the streets,Play Golf,Music,Although Often Close What Was The Only No.1 Madness Ever Achieved,House Of Fun,Science & Nature,Cirrus and Cumulus are types of what? ,Cloud  +General,What was the real name of the gangster known as pretty boy,Charles floyd,General,Which English Kings armour has the biggest codpiece,Henry 8,General,Who bought manattan island for the equivalent of 24 dollars,Peter minuit +General,"In 'les miserables', jean valjean's criminal number is the same as what character in 'the simpsons'",Sideshow bob,General,German film of the 1920s starred Max Schreck as a vampire,Nosferatu,Geography,Name the sea north of Alaska.,Beaufort +General,Name the American pilot shot down over Russia in 1960,Francis Garry Powers,General,"Who said ""The internet is a good way to get on the net""",Republican candidate Bob Dole,General,A dime has how many ridges around the edge,118 +Music,Both Roxette & Sheena Easton Had A Hit With A Song Called What,You Got The Look,Art & Literature,"A soft, subdued color; a drawing stick made of ground pigments, chalk, and gum water. ",Pastel,General,Baseball: the florida ______,Marlins +General,"Germany's allies in WW II were Japan, Italy, Hungary, Bulgaria, Finland, Libya, and _________",Romania,General,What are a Boomer and a Blue Flier,Adult Male – Female Kangaroo, Geography,What is the basic unit of currency for Equatorial Guinea ?,Franc +General,"The prefix tetra, used in such words as tetrach, tetrapod, & tetrameter, means what",Four,General,In Elko Nevada sex without what is illegal,Condoms,General,What said I'm never through with a girl till I've had her three ways,John F Kennedy +Food & Drink,What European Country Is The consumer Of The Most Ice Cream ,Sweden , Language,"Many Meanings: Fuel, vapor, flattulence, helium. What is it",Gas,General,What would you expect in a Japanese No Pan Kissa restaurant,Mirror floor knickerless women +General,Britain's oldest existing Trade Union was founded in 1747 what trade,Brushmakers and General Workers,General,What scientist is credited with the discovery that there are galaxies beyond our milky way,Edwin hubble,General,Willkie what is the principal tributary of the wabash river,White river +General,What does sputnik mean,Fellow traveller,General,What were the names of the four main characters of the Facts of Life?,Jo Blair Natalie Tootie,General,What actor played the Wizard of Oz,Frank morgan +General,What is the sum of all the angles in a square? (in degrees),360,General,Who married Marie Louise of Austria because he wanted an heir,Napoleon,General,"If you ordered 'pamplemousse' in a French restaurant, what would you be served",Grapefruit +General,One who tells fortunes by the stars is a(n) __________.,Astrologer,Art & Literature,Which Antipodean Opera Singer Sung At Prince Charles Wedding To Lady Diana Spencer ,Kiri Te Kanawa ,General,Doraphobia is a fear of ______,Fur +General,"What's ""birth control for roaches""",Black flag,General,What was the first u.s consumer product sold in the soviet union,Pepsi cola,General,What animal has red patches on its rear,Mandrill +Music,Which White Blues Singer Had Attended The University Of Texas In The Early 60's And Been Voted The Ugliest Man On Campus,Janis Joplin,General,Which Group Was Banned From The Official Washington 4th July Celebrations In 1983 Because Secretary For The Interior Said They Would Attract An Undesirable Element,The Beach Boys,Music,Patricia Andrzejewski Is The Real Name Of Which Singer,Pat Benatar +Sports & Leisure,What was football player Dick Lane's nickname,Night train,General,"Pepsin, lactase and amylase are types of what",Enzymes,Science & Nature,What Is Represented By The Blue And White Quartered Circle Of The BMW Logo ,A Spinning Propellor  +General,What was the first name of the cartoon character Mr Magoo,Quincy,General,The White House has 13092 of them - what,Knives forks and spoons,General,What year saw the launch of Sputnik I,1957 +Food & Drink,Sweetbread is derived from this organ.,Pancreas,Music,"Which Female Singer Used To Be A Presenter On ""Razzamatazz""",Lisa Stansfield,Entertainment,Who appeared solo at the Woodstock festival after leaving 'The Lovin' Spoonful'?,John Sebastian +General,Grover Cleveland is the only United States president to have been married where?,White House,Science & Nature,What Causes Hypoglycemia ,Low Blood Sugar ,General,"Who painted ""Resurrection: Cookham""",Stanley spencer +General,Tall revolving wheel at fairgrounds,Ferris,General,Second city: Cheyenne (state),Casper, History & Holidays,"Nicknamed the Godfather Of Soul, who died on Christmas Day 2006 ",James Brown  +General,"Which game is played at 12 a-side for women, but at 10 a-side for men",Lacrosse,General,Who produced an electric bicycle called a Zike in 1992,Sir clive sinclair,Science & Nature,Which Company Produced The Worlds First Commercial Jet Air Liner ,De Havilland (The Comet)  +General,Name actor called The voice of Canada - had 1964 hit Ringo,Lorne Green,General,The minnow is the smallest member of what fish family,Carp,General,What was RJ Mitchell's contribution to WW2,Designed the Spitfire +General,"Havilland what does dean martin's california license plate say, on his stutz blackhawk",Drunky,General,In which theater was abe lincoln shot,Ford's theater, History & Holidays,"In the 15th century, what was the war between the houses of Lancaster and York?",War of the Roses +General,What is the fear of tapeworms known as,Taeniophobia,Entertainment,"This electronic instrument's creator was surnamed Moog, and his models are worth a fortune! Other brands include Roland, Korg, and Casio.",Synthesizer,General,In Romeo and Juliet what day is Juliet's Birthday,31st June +General,What common pests does New Zealand not have,Squirrels,General,In which country was the Auschwitz death camp located,Poland, Geography,Bridgetown is the capital of ______?,Barbados +General,Who won the best actress Oscar 1959 Room at the Top,Simone Signoret,General,What is the fear of long waits known as,Macrophobia,General,What fruit can be Red Black or White,Currants +General,A Scotsman tosses his caber - what does caber literally mean,Pole,Music,Which British group were formed by former members of the Housemartins?,The Beautiful South,General,Who sang about Angel in a Centrefold,J Geils Band +General,Who was the classical composer of the tune which was used for the 'Alfred Hitchcock Presents' TV show?,Gounod,Toys & Games,This game of chance was originally called 'Beano'.,Bingo,Food & Drink,The Name Of Which Dish Literally Translated Means 'Outside The Work' ,Hors d'oeuvre  + History & Holidays,Who released an album called 'Ghost in the Machine' ,The Police ,General,What Is The Last Name Of The Character Compo In The Tv Sit-Com Last Of The Summer Wine?,Simmonite,General,"Who wrote the novel that the play ""Les Miserables"" is based on?",Victor Hugo +General,In cookery what happens to food served farci,It's stuffed,General,What year was the first world series game played,1903,General,Who spun straw into gold,Rumplestiltskin +General,What musical play - find a character called Magnolia Hawks,Showboat,General,What is the french for hospital,Hotel-dieu,Music,"In The NME ReadersPoll, Who Was Voted The Best UK Male Singer For 7 Consecutive Years In The 1960's",Cliff Richard +General,Which dormant volcano is the highest peak in Japan,Fujiyama,General,In which German city was the composer Pachelbel born,Nuremberg,General,In Switzerland what device is illegal on Sundays,Lawnmower +Music,What Did Tom Petty's Band Break,Hearts,General,Which modern Irish writer's novels include Dublin Four and Evening Class,Maeve binchy,Science & Nature,What Do You Call A 'Medicine' That Has No Physical Effect ,A Placebo  +General,What is the currency of Turkey,Lira,General,What is the largest lizard,Komodo dragon,General,In The Hobbit what colour is Bilbo's door,Green +Sports & Leisure,How Many People In A Tug Of War Team ,8 People ,Science & Nature,"Which Small Rodent , Highly Valued For It's Fine , Silky Fur, Lives Almost Exclusively High In The Andes ",Chinchilla ,General,Who was Secretary General of the UN from 1946 to 1952,Trigvye lie +General,"Often, a vampire's pallor.",Pale,General,"In common: labrador duck, passenger pigeon, dodo, heath hen, carolina",Extinct birds,Science & Nature,What is the tibia more commonly known as?,Shin bone + History & Holidays,"In 1953 Science fiction author L.Ron Hubbard founded which cult religion, one of who's prominent modern day followers is John Travolta ",Scientology ,General,What bird is sacred in Peru,Condor,Music,Who Was The Lead Singer Of Haircut 100,Nick Hayward +General,"Who wrote ""Wuthering Heights""",Emily bronte,General,What is the capital of Sicily,Palermo,General,The Gloucester E 28/39 first flew in 1941 - what was unusual,Whittle Jet Engine +Sports & Leisure,Football: The San Diego ______?,Chargers,General,Fishy short story also known as Creation Took Eight Days,Goldfish bowl,General,What is the flower that stands for: charming,Cluster of musk roses +General,Aesculus is the Latin name of what type of tree,Horse Chestnut,Entertainment,Who was the director of 'Terminator' and 'Titanic'?,James Cameron,General,Dave Rowntree Is The Drummer With Which Band?,Blur +General,What nationality was the explorer Vitus Bering,Danish,General,Who is Britains best known former kindergarten teacher,Diana,Music,Deniece Williams Wanted To Hear It for Who,The Boy +General,"Who recorded the 1996 alburn, ""Older""",George michael,General,"Countries of the world:north eastern Africa, the capital is Djibouti",French somaliland, Geography,What is the basic unit of currency for Barbados ?,Dollar +Sports & Leisure,What Distance Is Covered In One Circuit Of A Modern Outdoor Running Track? ( In Meters) ,400 Metres ,General,The Hindu trinity are Shiva Vishnu and who,Brahma,Music,Who Wrote Most Of The Pretenders Hits,Chrissie Hynde +General,How many books are there in a trilogy,Three,General,Where would you find you columella - or what is it,Space between nostrils,General,The study of religion is ________.,Theology + Geography,What is the capital of Luxembourg?,Luxembourg,General,In Oklahoma by law baseball teams cannot do what,Hit ball over fence out of ground,General,In Oklahoma it is illegal to bite some else's what,Hamburger +General,This organ is a small pouch that stores bile,Gall bladder, Geography,Which U.S. city is known as the Biggest Little City in the World?,Reno,General,"Which group sang the song ""Rollin""?",Limp Bizkit +Geography,In which country is Cusco,Peru, History & Holidays,How old was John F. Kennedy when he became president?,Forty three,General,How many axles does an 18 wheeler truck have?,5 +General,What animal is incapable of making a sound that will echo?,Duck,General,In Massachusetts by law bars cannot offer what,Happy hours,General,In the international code of signals what does Oscar signify,Man overboard +General,Who wrote a series of novels about the Ballentines of Africa,Wilber Smith,General,In Poland if you asked for a piwo what would you get,A Beer,General,What Jewish festival takes place near Easter? ,Passover  +Music,What Was A Big Hit For Cameo In 1986,Word Up,Entertainment,"What was Sir Alec Guinness's role in ""Star Wars""?",Obi-Wan Kenobi,General,Prince Adam was the secret identity of which Super hero?,He-Man +Toys & Games,"Which game usually begins with, ""Is it animal, vegetable, or mineral""",20 questions,General,What was Joseph Pujol - La Petomanes stage act,He farted – imitating music etc,General,Dark edible fruit of the bramble,Blackberry +General,"The highest waterfall in the world, Angel Falls in Venezuela, has a total drop of how many feet",3121,General,What's the capital of Sweden,Stockholm,General,"Who wrote the novel ""Finnegan's Wake""",James joyce +General,"Got to do with it Who created the famed butler ""Jeeves""",Pg wodehouse,General,Who played dr. frankenfurter in the pop-culture film 'rocky horror picture show,Tim curry,General,"A tagged bird of this species flew the greatest known distance for a bird. (22,500 km)",Arctic tern + History & Holidays,*The war in Vietnam ended with the fall of Saigon in what year?* ,1975 ,Music,What Did Gladys Knight And The Pips Catch In 1976,Midnight Train To Georgia,General,Who wrote April is the cruellest month in poem The Wasteland,T S Elliot +General,Name the Teletubbies?,"Laa Laa, Tinky Winky, Dipsy and Po", Geography,What country borders Sudan to the North?,Egypt,General,Execute by strangulation with a thin wire,Garrotte +General,"In the movie ""Stand By Me"", what did Gordy, Chris, Vern and Teddy set out to find?",A Body,General,In Greek legend who turned men into swine,Circe,Science & Nature,What is a young hare called? ,Leveret  +General,Pantophobia is the fear of,Fears,Technology & Video Games,What is your character's name in the 'Legend of Zelda' series? ,Link,General,"What Everyday Item Was Invented By ""Whitcomb L Judson"" In 1893",The Zip +Music,"Who Sang ""I can Drive 55"" From The Film ""Back To The Future 2""",Sammy Hagar,General,What element was named after the Greek word for green,Chlorine, History & Holidays,"At the time of Julius Caesar, who was the ruler of Egypt ?",Cleopatra +Religion & Mythology,How many times did Peter deny Jesus?,Three,General,"Who said ""men are creatures with two legs and 8 hands""",Jayne Mansfield,General,Which concert hall is now the home of the Halle orchestra,Bridgewater hall +General,Ariel is a satellite of which planet in the solar system,Uranus,General,Bill haley sings 'see you later ______ ',Alligator,Music,Which Group Started A Bumper Crop Of Singles In 1986 With System Addict,Five Star +Music,"Name The Band: Robert Plant, Jimmy Page, John Paul Jones, John Bonham",Led Zeppelin,Music,"Rock & Roll Revival Band ""Sha Na Na"" Appeared As Johnny Casino & The Gamblers In Which Film",Grease,Science & Nature,"The Average Woman Has 7 Pints Of Blood, How Much Has The Average Man ",12 Pints  +Geography,In What Year Was The First Propeller Driven Atlantic Crossing ,1845 ,People & Places,Who Was The First Director General Of The BBC ,Marmaduke Hussey ,General,The Russian composer Alexander Borodin had what other job,Chemistry Professor St Petersburg +General,What did the Manhattan project set out to develop in 1941,Atomic bomb,Entertainment,What is Dennis the Menace's last name,Mitchell,General,What is the SI unit of power equal to 1.341 horsepower,Kilowatt +Music,"A Decade Later He Found A New Dawn But Who Had A No.5 Hit ""Bless You"" In 1961",Tony Orlando,General,"In Greek mythology, who was condemned to bearing the world on his shoulders for trying to storm the heavens",Atlas,General,Carnegie Melon University only one offers a degree in what,Bagpiping +General,What is the most common surname in sweden,Johanssen,General,What would a car have if its specification included A.B.S.,Anti-lock braking system,General,Who has the most personalised licence plates,Illinois +General,Which American Vice President was the only one to serve two full terms as President?,Thomas Jefferson,Religion & Mythology,How many animals of each kind did Moses take onto the ark?,None (It Was Noah Not Moses),General,Who shot andy warhol,Valeri solanis +General,Rap group 2 Live Crew tells it their way and gets banned in,Florida,General,Person who throws gloom over social enjoyment,Party-pooper,General,"A row of windows in the upper part of a wall, especially in a church, to admit light below. ",Clerestory +General,What outfit merged with Time Warner in 1996 to form the world's largest entertainment company,Turner broadcasting systems,General,Citrus Grandis is the Latin name of which fruit,Grapefruit,Music,What Song Did Abba Write For The Eurovision Song Contest In 1973,Ring Ring +Music,Which Playing Card Was A Big Hit With Motorhead,The Ace Of Spades,Music,"What's The Connection Between Chrissie Hynde, & Robert Palmer",UB 40,General,In what language was The Communist Manifesto written,German +General,What are dolly parton's working hours,9 to 5, History & Holidays,Jadis the White Witch is the villain in which children's book ,The Lion the Witch and the Wardrobe ,General,Who was the first wife of zeus,Metis +Geography,___________ is the only country in the Middle East that does not have a desert.,Lebanon,General,"In Greek mythology, who did jocasta marry",Oedipus,General,Apart from man what is New Zealand's only native mammals,Bats + Geography,What is the basic unit of currency for Cameroon ?,Franc,General,Programming language named after 17th cent French mathematician,Pascal - Blaise Pascal, History & Holidays,What is the the state capital of oklahoma ,Oklahoma city  +General,Name the first cartoon character made into a parade balloon,Felix the Cat,Music,Who Directed A Chorus Line,Richard Attenborough,Food & Drink,"Which alcoholic drink would you need to make the cocktail Tom Collins? Vodka, Whiskey or Gin? ",Gin  +Science & Nature, A __________ can swallow a rabbit whole and may eat as many as 150 mice in a 6_month period.,Python,General,What Utah city became the 37th in the U.S. to reach one million in population,Salt lake city,General,In the Dr Dolittle stories what type of creature was Dab-Dab,Duck +General,What committee eventually developed a standard for the 'c' programming language,Ansi,General,What was Angela's son's name in Who's the Boss?,Jonathon,General,Which Film Won The Oscar For Best Film The Year Prince Charles Married Lady Diana Spencer,Chariots Of Fire +General,Frodo Baggins was the first to enter Britain using what,Pet Passport he was a dog,General,"Who recorded the album ""London Calling"" in 1975",Clash,Music,"Which Album Was The Hugely Successful ""Insomnia"" Taken From",Faithless / Reverence +General,What is the capital of the state of Minnesota,St. paul,General,"What is the ""rathaus"" in frankfurt",City hall,Music,Paul Young Was Once A Member Of Which Band Was It The Q Tips Or The Q Jumpers,The Q Tips +General,"A grave, processional court dance popular in the sixteenth and seventeenth centuries.",Pavane,Geography,Name a country which has the same name as a bird. ,Turkey ,Geography,What is the capital of Chad,N'djamena +General,In Shakespeare's play who was the wife of Othello,Desdemona,Science & Nature,What Was The Name Of The First Ship To Sail Around The World ,The Victoria Magellans Ship ,General,Which character did not appear in the cartoon Star Trek,Chekov +Music,Which Andrew Lloyd Webber musical is set in the Troubles Of Belfast in the early 1970's?,The Beautiful Game,General,"Refusal of a group to trade or associate with another group, an individual, an organization, or a nation",Boycott, History & Holidays,In which battle was George A. Custer defeated?,Battle of Little Bighorn + History & Holidays,*What movie earned Steven Spielberg his first hit in 1975?* ,Close Encounters ,Food & Drink,To Which Family Do The Cabbage And Cauliflower Belong ,Brassicas ,Food & Drink,"During the reign of Charles the II, what 'black enemy of sleep and copulation' was called a magnet for political dissent by the King and was condemned as a den of dalliance by Puritans? ",Coffee houses  +General,Who married prince albert of saxe-coburg-gotha,Queen victoria,General,What arabian peninsula nations recently merged under communist leadership?,Yemen,General,Who conquered the Matterhorn in 1865?,Edward Whymper +Tech & Video Games,How many buttons (excluding the control pad) did the original NES controller have? ,4,General,What is the only continent without reptiles or snakes,Antarctica,Music,"Who Had A Hit With ""You Give Love A Bad Name""",Bon Jovi +Music,Which Bands Career Was Boosted By An MTV Appearance Without Their Make Up,Kiss,General,Where is the blarney stone,Blarney castle,General,At what farm does Aunt Ada Doom go on about nasty woodshed,Cold Comfort Farm +Geography,___________ has official state neckwear _ the bolo tie.,Arizona,General,Who is the roman god amor's mother,Venus,Sports & Leisure,Which Snooker Player Is Nicknamed The Rocket? ,Ronnie O'Sullivan  +General,What country has a regiment of bicycle mounted soldiers,Switzerland,General,What is the flower that stands for: disappointment,Carolina syringa,General,Who was the male star in the 1967 film Bonnie & Clyde,Warren beatty +Music,In which Beatles song does the singer feel 2 foot small?,You've Got To Hide Your Love Away,General,What city is closest to Copacabana beach,Rio de Janeiro,Religion & Mythology,Apollo was the Greek god of ______?,Prophecy and archery +Sports & Leisure,Where were the 1896 Olympics held ?,"Athens, Greece",General,Which Country Has The Highest Populatiuon Of Broadband Internet Users In The World,Iceland,General,Fredrick Sanger discovered which medical life saver,Insulin +Religion & Mythology,Which Titan had snakes for hair?,Medusa,General,What Was The Name Of The Album Released By Michael Jackson In 2001,Invincible,General,What group of animals would be in a clowder,Cats +Science & Nature,"Excluding the sun, what star is closest to the earth?",Proxima Centauri (aka Alpha Centauri),General,What is the Capital of: Hungary,Budapest,General,Collective nouns - A parcel of what,Hinds +General,"What's the international radio code word for the letter ""V""",Victor,General,What metal has the chemical symbol al,Aluminium,Music,Which Record Took Madonna To No.1 At The Start Of The Decade,Vogue +General,What is an organism called that lives on or in a host animal,Parasite,General,Who directed the 1971 film Macbeth,Roman polanski,General,What is the sum of 47b + 96b,143b +Art & Literature,What was H.G Wells' first novel?,The Time Machine,General,What is litmus derived from,Lichens,General,What was punky brewster's best friends name?,Cherry +Toys & Games,"These are the two highest valued letters in ""Scrabble"". ""Q"" and _____.",Z,General,Smiths Bon-Bons changed their name to what after 1840,Christmas Crackers,General,Joseph Gayette invented it in 1857 to prevent piles - what,Toilet Paper +General,Harvard University was originally called what,Cambridge Harvard gave 400 books,General,What is 'honcho' in english,Squad leader,General,"June 1988 who's on covers Time, Life, People, and Sports Illustrated",Mike Tyson +Religion & Mythology,"What does the ""touch of Midas"" turn everything into ?",Gold,General,U.S. Captials - Iowa,Des Moines,General,"From Which Musical Does The Song ""You'll Never Walk Alone"" Come From",Carousel +Music,Marvin Lee Aday Is The Real Name Of Which Well Known Singer,Meatloaf,General,What was the name of the cab company in Taxi,Sunshine cabs,People & Places,Albert Einstein Was Born On The 14 th March 1879 In Germany He Was Educated In Austria And Then Eventually Moved To Switzerland But In Which Country Did He Die ,America  +General,"Who created Gomez, Mortia and Uncle Fester as a cartoon",Charles Adams,General,What medal shows 3 naked men hands on each others shoulders,Nobel Peace Prize,Technology & Video Games,What are names of the two brothers in the Double Dragon games? ,Billy and Jimmy +General,Hitchcock appears in a newspaper in Lifeboat who wrote book,John Steinbeck,General,What is the Ishihara test designed to detect,Colour blindness,General,What is the highest point in China,Mount everest mt everest mt. everest +General,"In what substance are eggs rich, which causes silver to tarnish",Sulphur,General,What are scutes,Snakes belly scales,Sports & Leisure,What article of clothing was banned from Ascot in 1971? ,Hot Pants  +General,What is the capital of italy,Rome,General,Who named the st lawrence river,Jacques cartier,General,What was the former name for the African country of Malawi,Nyasaland +Food & Drink,Which Food Stuff Has A Name Which Translates Into English As 'On A Skewer'? ,Kebab ,General,"The song ""Some Day My Prince Will Come"" was introduced in the 1937 Disney movie ________________________ ",Snow white and the Seven Dwarfs,General,Rose O'Neil created the Kewpie doll where is it signed,Sole of Foot + History & Holidays,"Where did Churchill, Roosevelt and Stalin meet in 1945?",Yalta, History & Holidays,"What was the destination of the ship 'Mary Celeste' on it's final voyage November 1872, where it should never arrive ?",Genoa,General,In the animal kingdom what creatures are in the order Chiroptera,Bats +General,Where would you find A Wall The white line and Bars,Horses foot, History & Holidays,Where did the most famous encirclement of the Nazi troops during WWII take place?,Stalingrad,General,What is the Capital of: Armenia,Yerevan +Science & Nature,What Is Botany The Study Of ,Plants ,General,What is the approximate temperature on the planet Pluto,Minus 230 degrees celsius,General,List the 1st & last names of all 4 Sweathogs on Welcome Back Kotter?,"Vinnie Barbarino,Freddie Washington,Arnold Horshack,and Juan Epstein" +General,Every photograph of an american atomic bomb detonation was taken by who,Harold edgerton,General,Who wrote the opera 'i pagliacci',Ruggiero leoncavallo,General,"What does the latin word ""mensa"" mean",Table +General,Which religion's holiest shrine's are in the Ise Shima National Park near Osaka,Shinto,General,"On an analogue clock, what number faces 4",10,Sports & Leisure,What was football player Dick Lane's nickname?,Night Train +General,"Which sport uses ""stones"" and a ""house""",Curling,Science & Nature," Depending on the geographic region, about 30 to 60 percent of all animals brought in to animal shelters in the United States are __________",Euthanized,Music,What Was Britney Spears Debut Single Which Shot To No.1,Baby One More Time +General,Mincing Lane in London is traditionally home of what trade,Tea, Geography,What country does the island of Mykonos belong to?,Greece,General,What is a heavenly body moving under the attraction of the sun and consisting of a nucleus and a tail,Comet +General,The de beers group of companies controls more than 80% of the world's supply of ______,Rough diamonds,Food & Drink,What type of food is Dorset Blue Vinney? ,Cheese ,General,Where is the worlds largest bullfighting ring,Mexico City +General,"Who was cremated on the banks of the Ganges river on January 31, 1948",Mahatma gandhi,Entertainment,Who was John Wayne's musical co-star in true grit?,Glen Campbell,General,If you were drinking Red Stripe lager what country are you in,Jamaica +General,Bill Gates dropped out of which of educational institution,Harvard,General,In Japan what is an obi,A wide Sash worn like a belt,Science & Nature,What is the biological term for the voice box?,Larynx +General,What colour are shelled pistachio nuts,Green,General,What colour is the cross on the Swedish national flag,Yellow,General,Name of the German WWII military code,Enigma +General,When was the quadruplex telegraph invented,1864,Geography,The cities of Cairo in Egypt and Fez in Morroco are generally accepted to have the oldest of what type of institution in the world? ,University ,General,Twinkies originally contained what flavoured cream,Banana – Changed ww2 - no bananas +General,Who would use a brannock or what for,Measure foot shoes,General,Which Greek astronomer wrote the Almagest,Ptolomy,General,Madrid missouri who lived at 1313 mockingbird lane,Munsters +General,What is the name of a plant that turns to keep facing the sun,Heliotrope,General,In Islam what is the fourth piller of wisdom - there's 5 in total,Fasting in Ramadan,General,Which vegetable is used if a dish is described as 'a la Bretonne',Haricot beans +General,Which acid is found in unripe apples and other fruit,Malic acid, Geography,How many countries border the black sea?,"Six - Turkey, Georgia, Russia, Ukraine, Romania and Bulgaria",General,What did the chinese not refer to themselves as,Silk people +Music,"""Love Affair"" Had Three Top 10 Hits In 1968 Name 2 Of Them","Everlasting Love, Rainbow Valley, A Day Without Love",General,Harry Alan Robert Stewart made redundant Britain 1960s Job,Official Hangman,General,Mythophobia is the fear of,Myths stories false statements +General,In Kansas its illegal to eat what on Sunday,Snakes,General,What are the Twin Cities,Minneapolis and St Paul,General,International dialling codes what country is 86,China +General,What sport was banned in England in 1849,Cockfighting,General,"Which English king married Berengaria of Navarre, who never set foot on English soil",Richard the first,General,What is the English statute of 1689 guaranteeing the rights & liberty of the individual subject,Bill of rights +General,Who is the author of Jude the Obscure,Thomas hardy,General,"Who sang the theme for the James Bond film, ""You Only Live Twice""",Nancy,Food & Drink,Which cocktail would you find in a toolbox? ,Screwdriver  +General,"Which Cartoon Character Has An Arch Enemy Called ""Hugo A Go Go""",Batfink,General,Who invented the 'bunsen burner',Robert bunsen,General,Who did gawain accuse of sleeping with guinevere,Sir lancelot +General,Which award has the words for valour on it,Victoria Cross,General,Plastic wrapping material in sheets containing numerous air filled bladders,Bubble wrap,General,What english writer said 'give me a decent bottle of poison and i'll construct the perfect crime'|agatha christie,Agatha christie +General,Lucille Le Sueur became famous as who,Joan Crawford,General,Agricultural science concerning methods of soil management & crop production,Agronomy,General,Until computers replaced it who would use a Bloggoscope,OS Map Makers from Aerial photos +General,What is the most popular last name in France,Martin,General,What is the Capital of: Eritrea,Asmara,General,What planet in the solar system is the largest,Jupiter + History & Holidays,"""How Many gifts Would you receive if you received all the gifts in the song """"The 12 Days Of Christmas"""") Is It 196, 296, 364, 398 "" ",364 ,General,An angle greater than 180 degrees & less than 360 degrees is a(n) ________ angle.,Reflex,General,What is a group of gorillas,Band +General,Which country had four kings called malcolm,Scotland,Music,"""A Little Time"" Was A Hit For ""The Beautiful South"" Or ""Some Kind Of Beautiful""",The Beautiful South,General,What does lacrimal fluid lubricate,Eyes +General,What was the maid's name in the tv series 'the brady bunch',Alice,General,"Erie, suez, panama etc",Canals,People & Places,In Which Couuntry Was The Playwright Tom Stoppard Born ,Czechoslovakia  + Geography,Where is the Machu Picchu?,Peru,General,Five tons are mined annually - five tons of what,Diamonds,General,"Who was named Chairman of the U.S. Federal Reserve Board by Ronald Reagan in 1987, a post he still (February '99) holds",Alan greenspan +General,"Directed by Milos Foreman, which film won the 1975 Oscar for Best Picture",One flew over the cuckoo's nest,General,What is the Capital of: Finland,Helsinki,General,What year did Theodore Roosevelt die,1919 +General,Which composer was nickname the Red Priest,Vivaldi,General,What Illness Is Caused By The Epstein Barr Virus,Glandular Fever, History & Holidays,Which 60's singing duo divorced after 11 years of marriage? ,Sonny and Cher  +Geography,Addis Ababa Is The Capital Of Which African Country ,Ethiopia ,General,Who was the first male host of 'entertainment tonight',Tom hallick, Geography,What is the capital of Denmark ?,Copenhagen +Science & Nature,What's the largest artery in the human body ,The aorta ,General,What countries brides get the most diamond engagement rings,Canada,Religion & Mythology,In what month is Christmas observed?,December +General,Who sang the title theme of the James Bond film A View to a Kill,Duran duran,General,Martin Luther King was the youngest recipient of what,Nobel peace prize,General,An artist supports his canvas on a(n) _________,Easel +General,Which beatle took up racing cars,George harrison,General,What is the name of the mountain range in North Africa,Atlas mountains,General,"Animals that once existed and exist no more, are called ______",Extinct +General,What song earned lionel richie his first grammy,Truly,General,Which 19th Century poet was known as 'The Bard of Rydal Mount',William wordsworth,Music,"Who Wrote ""Great Balls Of Fire"" , ""All Shook Up"" & ""Dont Be Cruel""",Otis Blackwell +General,What is a smew,A type of wild duck,Sports & Leisure,In which sport is the Canadian Wayne Gretzky an all time great? ,Ice Hockey ,General,Nairobi is the capital of ______,Kenya + History & Holidays,After who was America named?,Amerigo Vespucci,General,What do oak trees grow from?,Acorns,General,And which one comes second,Mayonnaise +General,"What is the Capital of: Congo ,Republic of the",Brazzaville, History & Holidays,Who Knocked Out Floyd Patterson To Win The World Heavyweight Boxing Title In 1962 ,Sonny Liston ,Music,What Was The Relation Of Karen Carpenter To Richard,Sister +Science & Nature,Who Invented the ball point Pen ,Laslo Biro ,General,Which characters are described as being three apples high,The Smurfs,Entertainment,"In the TV series 'The Fall Guy', who played Colt Seavers?",Lee Majors +General,With what is rainfall measured?,Ombrometer,General,Galena is the principal Lead ore which other element is combined with the metal to form Galena,Sulphur,Music,"Name The John Cougar Mellencamp Album That Featured The Hit Single ""Jack & Diane""",American Fool + Geography,Which is the most remote island in the southern atlantic ocean?,Bouvet Island,Food & Drink,What name is given to an illegal highly alcoholic Irish whiskey ,Poteen (pocheen) ,Music,What was always Elvis Presley’s closing number in his Las Vegas stage shows?,Can’t Help Falling in Love +General,"At which American University were four students shot dead , while protesting against the Vietnam War",Kent,Music,Who Had A Hit In 1987 With The Song Angel Eyes,Wet Wet Wet,General,How Did Henry Tate Owner Of The Tate Gallery In London Make His Fortune,Sugar (Tate & Lyle) +General,"Hermes, Symphony and Anik are all what",Com satellites,General,Who played the part of Cruella de Vil in the 1996 film '101 Dalmatians',Glenn close,General,"The Super Bowl, and never re-used the commercial?",Apple +General,"Directed by Sydney Pollack, which film won the 1985 Oscar for Best Picture",Out of africa,General,What is 'the helvetic confederation' in latin,Confederatio helvetica,General,The world heritage site Monticello was designed and lived in by whom?,Thomas Jefferson +Science & Nature,This small animal is trained to hunt rats and rabbits.,Ferret,General,What was originally called the Chinese gooseberry?,Kiwi,General,Who was the famous TV painter from the 80's?,Bob Ross +General,Stage who made her show business debut under the name of 'baby frances',Judy,General,"Israel Baline, born in Temum, Russia, on 1lth May 1898, became famous under what name",Irving berlin,General,Can you see Lenny Bruce E A Poe Karl Marx H G Wells etc,Sergeant Peppers +Music,Unknown Until the 60's Who Started Their Career With A Wartime Mining Disaster,"The Bee Gees, New York Mining Disaster",General,"The style of this school, founded in Germany by Walter Gropius in 1919, emphasizing simplicity, functionalism and craftsmanship. ",Bauhaus,General,Which cartoon character was originally called egghead,Elmer J Fudd +General,What is the chemical name for the mineral known as 'fool's gold',Iron sulphide,General,What counties national drink is called aizag pronounced I shag,Mongolian mares milk,General,What make was the first car with air conditioning,Packard +General,"A ""pigskin"" is another name for a(n) ________",Football,General,Scotopic people can do what,See in the dark,General,Which Former England Manager Was Born In Burnley Lancashire,Ron Greenwood +General,"Which French Atlantic port was the target of a raid on March 28th 1942, code-named 'Operation Chariot'",St nazaire,General,Tomika and Uyeshiba are the two main forms of what,Aikido,General,What was the second bridge built across the Thames?,Westminster Bridge +General,Which is the largest bird of prey native to England,Buzzard,General,What was Woody Allen's first film as writer/actor,What's New Pussycat,Science & Nature," The most venomous of all snakes, known as the Inland Taipan, has enough venom in one bite to kill more than 200,000 __________",Mice +People & Places,Which Politician Punched A Protestor Who Threw An Egg At Him During A Visit To Wales In 1991 ,John Prescott ,General,Who wrote the 1955 play Long Day's Journey into Night,Eugene o'neill, Geography,Which country would come first in an alphabetical list of countries?,Afghanistan +General,Superstition says the feathers of which bird shouldn't be used as house decorations,Peacock,General,In Minnesota it is illegal to wear what in bed,Nothing ie be naked,General,What element is represented by the chemical symbol pb,Lead +General,What was invented by Dr Albert Southwick in 1881,Electric chair,Geography,In which country is Loch Ness,Scotland,General,With whom was the first covenant of God,Adam & eve +General,The film Cleopatra was banned in Egypt in 1963 why,Liz Taylor Jewish convert,Music,In The Lou Bega Song “Mambo No.5” What Is The Name Of The 1st Girl Mentioned In The Song,"Angela, Pamela, Sandra, Rita",General,Daisy cooper was a housekeeper in what western,Laramie +General,Name the boat on which you can see Niagara Falls,Maid of the Mist,General,In Mesquite Texas its illegal for children to have what,Unusual Haircuts,General,Who is the headmaster of Hogwarts school in the Harry Potter books,Professor dumbledore +General,Who ordered the persecution of the christians in which peter and paul died,Nero, Geography,Name the capital city of Massachusetts.,Boston,General,Capital L is the Roman numeral for which number,Fifty +General,William the boys name means what,Resolute Protector,General,Where are the katydid bug's ears,Hind legs,General,Name the little elephant in books by Jean de Brunhoff,Babar +General,In 1878 Thomas Alva Edison patents the,Phonograph,Entertainment,"She played the lead role in ""Coal Miner's Daughter"".",Sissy Spacek,General,What was the name of the old man in The Old Man and the Sea,Santiago +Science & Nature,This science deals with the motion of projectiles.,Ballistics,General,What restaurant chain had served forty five thousand million hamburgers by 1983,Mcdonalds,General,Who was given an honorary Oscar in 1985 after 50 years acting,James Stuart +Science & Nature,The North Star is also known as _______.,Polaris,General,Slivovitz is a brandy made from what,Plums,Music,Who played guitar on the Michael Jackson song Give In To Me?,Slash (Guns and Roses) +General,What are wrapped in rashers of bacon to make the dish Angels on Horseback,Oysters,General,"Whose ads tout ""the return of fz on cd""",Frank zappa,General,"Woman's are faster than men's, they usually have more - what",Heartbeats +General,Of what was vadim viktorovich bakatin the last chairman,K.g.b,General,"What weed derives its name from the French for ""lion's tooth""",Dandelion,General,"Who Did Surgeon ""Dr Neil Murray"" Marry In A 20 Minute Private Ceremony At His Home In Pershire Scotland On Boxing Day In The Year 2001",J.K Rowling +Science & Nature," In 1880, there were approximately 2 billion passenger pigeons in the United States. By 1914, the species was __________",Extinct,Art & Literature,Who wrote 'The Great Gatsby'?,F. Scott Fitzgerald,General,What does the acronym WYSIWYG stand for,What yoU.S.ee is what you get +General,What does a philologist study,Languages,General,What is a group of this animal called: Coyote,Band,General,Who wrote the poem Kubla Khan,Samuel Taylor Coleridge + Geography,In which continent would you find the Amur river ?,Asia,General,Outside which london building were traffic lights first installed,Houses of,General,What is a 'gilt',Young female pig +General,POETS: Who wrote Kabla Kahn,Samuel taylor coleridge,General,What is the capital of cameroon,Yaounde,General,Name the triangular cotton headscarf or Russian grandmother,Babushka +General,What is the largest natural harbour in south africa,Saldanha bay,General,Who wrote the play 'Under Milk Wood',Dylan thomas, History & Holidays,The following is a line from which 1970's film My name is___. 'Names is for tombstones baby' ? ,Live and Let Die  +General,Who wrote Lord Of The Rings,J.R.R. tolkien,General,What is the brightest asteroid,Vesta,General,To what does the original term' cutty sark ' refer,Short shift chemise +General,What was the last film of director Stanley Kubrick?,Eyes Wide Shut,Sports & Leisure,This team won their first World Series in 1969.,New york mets,Music,Which Beatle Had His Tonsils Removed In The Latter Part Of 1964,Ringo +General,What is the flower that stands for: do me justice,Sweet chestnut tree,Sports & Leisure,Why Was Linford Christie Disqualified From the 1996 Atlanta Olypics ,2 False Starts ,General,Who in the Bible received thirty pieces of silver,Judas +General,What animal provide 50% of all the protein eaten in Peru,Guinea Pigs,General,In 1688 Edinburgh Became The First City To Have What?,Pavements,General,What is the deepest mine in the world?,Western Deep Levels Mine +General,Which Beatle wrote the score for the 1966 film The Family Way,Paul mccartney, Geography,Where is Eurodisney?,"Paris, France",People & Places,Who Was Sent Off In Both Opening Premiership Games Of The 2000 Season ,Patrick Vieira  +General,Who was the first king of israel,Saul,General,The word volar refers to what part/s of the body,Palms of hands and soles of feet,General,Bat Masterson was a sports writer for what paper,Cape town +Science & Nature,What lives in a formicary?,Ants,General,How many solutions are there to a quartic equation,Four,General,"Mark Twain referred to the what as the ""stomach Steinway.""?",Accordion +Science & Nature,What muscle is joined by the lingual nerve to the brain?,Tongue,General,In August 1945 atomic bombs were dropped on which two cities,Hiroshima and nagasaki,Sports & Leisure,The Pumas Are The Rugby Team Of Which Country? ,Argentina  + History & Holidays,What's the claim to fame of abraham zapruder ,He filmed john f. kennedy's assassination ,Music,"What Equally Successful Track Was On The Flip side Of ""Walk Right Back"" In 1961",Ebony Eyes,General,What is usually given to 'trick or treaters',Candy +Entertainment,"In the film 'American Hot Wax', who did Jay Leno play?",Mookie,General,Who was the leader of the Transformers?,Optimus Prime,General,Frosties Tony the Tiger had a Son Tony Jr and a daughter name,Antoinette +General,In 1999 20% of all US tourists came from which country,Japan,General,What did Sandy Fowler invent for drinkers that had he patented it would have made him millions,Tea bag,Entertainment,What was the last Beatles album to be released before they broke up in 1970?,Let It Be + History & Holidays,In which hospital would you find Sir Lancelot Spratt? ,St Swithens , History & Holidays,"The first charity Christmas card was produced by UNICEF in 1949. The picture chosen was painted by a: A Chimp, A 7 Year Old Girl, A Pop Star, A Buddhist Monk ",A 7 Year Old Girl , History & Holidays,What did Eli Whitney invent?,Cotton gin +Geography,What is the capital of Barbados,Bridgetown,General," The study of the earth's physical divisions into mountains, seas, etc. is ________.",Geography,Science & Nature,"Nitrogen, a poisonous gas, makes up 78% of the ___ that we breathe.",Air +Art & Literature,This statue was found on the Greek island of Melos in 1820.,Venus de milo,General,What was the ancient Egyptian cure for haemorrhoids,Beer - lots of beer,General,"Who's Autobiography Is Entitled "" And There's More""?",Jimmy Cricket +General,What is a group of this animal called: Rook,Building clamour,Entertainment,What was painted on Peter Fonda's helmet motorcycle helmet in 'Easy Rider'?,Stars and stripes,Science & Nature,In which country was the match invented,France +General,What kind of tree is Alexander the Greats entire army said to have sheltered under,Banyan,General,Who won best supporting actor for his role in ryan's daughter,John mills,Entertainment,Who played Kevin Hathaway on the soapie 'Days Of Our Lives'?,Pat Sajak +General,Who has the most u.s banknotes,Russia,General,"In Which Us State Does The Fst Food Chain ""Subway"" Originate",Connecticut,General,In Which US State Is John F Kennedy Buried,Virginia +General,Large extinct flightless bird,Dodo,General,Ile de France was the former name of which country?,Mauritius, Geography,What is the capital of Taiwan ?,Taipei +General,Who was the first (and last) catholic president,Kennedy,General,What is the capital of the state of Connecticut,Hartford,General,What is a trainee jockey under 21 years of age called,Apprentice +General,The Bay of Naples and the Gulf of Salerno are inlets of which sea,Tyrrhenian,General,"When driving from Innsbruck in Austria to Bolzano in Italy, you cross the 'Bridge of Europe' as you climb which Alpine pass",Brenner pass,Art & Literature,"Tilly Trotter, Hannah Massey And Maggie Rowan Are All Characters Created By Which Novelist? ",Catherine Cookson  +General,The duodenum and the jejunum are two of the three sections of the small intestine. Which is the third,Ileum,General,Thomas the boys name means what,Twin,General,What is a group of this animal called: Frog,Army colony +General,Rodrigo Diaz de Vivar famous under what nickname,El Cid,General,What animal does the adjective 'macropine' refer to,Kangaroo,General,What nationality was Hans Christian andersen,Danish +Toys & Games,The King in chess can move a maximum of this many squares.,Two,Entertainment,"Which formber Beatle released the hit single ""My Sweet Lord""?",George Harrison,Geography,"If its 4:00pm in seattle washington, what time is it in portland oregon ",4.00pm  + Entertainment,What is the longest running musical in Broadway history?,Cats,General,Who was Englands first Norman king,William the conqueror,Science & Nature,What is the most reliable geyser in the world?,Old Faithful + History & Holidays,"Who In 1963 , Became The First Film Star To Earn A Million Dollars For A Single Film ",Elizabeth Taylor In Cleopatra ,General,In what Australian state would you find Canowndra,New south wales nsw,Sports & Leisure,How many sides does a home_plate have,Five +Geography,The only national airline that has never had a crash nor a forced landing.,Qantas,General,Dermaptera are what species of insects,Earwigs,General,In which year was the first FA Cup final held at Wembley?,1923 +General,What constellation is represented by scales,Libra,Sports & Leisure,How many players make up a field hockey team,Eleven,General,Victor Barna was a famous name in which sport?,Table Tennis +Music,"The Song ""Crazy"" Is Synonymous With Pasty Cline But Who Wrote It",Willie Nelson,Music,What Was The Name Of The Horse In America's 1971 Hit,(Nothing (Horse With No Name),General,"What small Arctic rodents are said to, but don't, commit suicide in mass plunges into the sea",Lemmings +General,What U.S. state includes the telephone area code 501,Arkansas, History & Holidays,What was Europe's first super-high-speed passenger train powered by?,Electricity,General,Which search engine gets its name from Latin for wolf spider,Lycos +General,Who does fred savage play on 'the wonder years',Kevin arnold,Sports & Leisure,For which International side did Shane Warne play Cricket? ,Australia ,General,"In which river is the group of about 1,500 islands known as ""Thousand Islands""",St lawrence river +Sports & Leisure,How Many Jumps Are There In The Grand National ,30 Jumps ,Sports & Leisure,Which footballer was called (Crazy Horse)? ,Emlyn Hughes ,General,Churches in Malta have two what,Clocks right and wrong confuse devil +General,A technique by which patients monitor their own bodily functions in an attempt to alter those functions,Bio feedback,General,What is a goat sucker,A Bird,General,What group of people meet at Kingdom Halls,Jehovah's Witnesses +Food & Drink,Which vegetable is the principal ingredient of rosti? ,Potato ,Music,"Who Recorded ""Wee All Stand Together"" With The Frog Chorus",Paul McCartney,General,What is a 'fody' a type of,Bird +General,When was the smoke detector invented,1969,General,What is the correct name for a male red deer,Hart,General,What company created a computer dubbed Jackintosh,Atari +General,"What herb seed can be either black or white, & yields an oil that resists turning rancid",Sesame,General,What organization was given the only Nobel peace price awarded during WW I,International red cross,General,What was the secret identity of captain america,Steve rogers +General,Who was the first athlete to have an animated cartoon series,Mohamed Ali,General,What is the israeli knesset,Parliament,General,Which dinosaurs name translated as speedy predator,Velociraptor +Science & Nature, The cells which make up the antlers of a __________,Are the fastest growing animal cells in nature. moose,General,Collective nouns - A sute of what,Bloodhounds,General,What manufacturer of pens & throw away razors also make sailboards,Bic + History & Holidays,"Talking of Holland, traditionally Santa doesn't actually deliver the presents who does, is it, Rudolph, Santa's servant Black Peter, A goat named Ukko or Thirteen Elves ",His servant Black Peter ,General,Who was the 'serpent of the nile',Cleopatra,General,Michael Jackson Sadly Died In 2009 But Can You Tell Me His Middle Name,Joseph +General,"During which war did the expression ""They shall not pass"" originate",World war 1,General,Who was the last briton to win the men's singles at wimbledon,Fred perry,General,"Which Female Artist Released Their 6th Album In 1985 Entitled ""The Whole Story""",Kate Bush +General,In 1983 Challenger crew perform a spacewalk - first by U.S. in,9 years,Geography,What is the capital of Portugal,Lisbon,General,"What mode of transport was invented in 1959 by the Canadian, Armand Bombardier",Snowmobile +General,What was John Fitzgerald Kennedy's campaign song in 1960,High Hopes,Geography,In which country is Angel Falls,Venezuela,General,"What product built Hershey, Pennsylvania?",Chocolate +General,"Because the emu and the kangaroo cannot walk backwards, they are on the australian ______",Coat of arms,Geography,Lake Ladoga Is The Largest Lake In Which Country ,Russia ,General,What is the fastest swimming ocean fish over 60 mph,Sailfish - Marlin +General,What is the second-highest mountain in Africa?,Mt Kenya,General,"In 'star wars, four people played darth vader david prowse was his body, james earl jones did the voice, sebastian shaw was his face and a fourth person did the ______",Breathing,General,"Which major food company grew out of the Sanitas Food Company of Battle Creek, Michigan",Kelloggs +Science & Nature,Why Do Fossils Of Long Necked Dinosaurs Appear To Show The Head Pulled Back Over The Body ,Shrinkage Of Neck Muscles After Death ,General,Which of India's states is thought to have the largest Sikh population,Punjab, History & Holidays,Who burned Atlanta in 1864?,General Sherman +General,The Dutch Royal family are Orange where is Orange,Village in France,General,Which countries government spends most in social security %,Uruguay,Music,"""This Old House"" & ""Mambo Italiano"" Were UK No.1 For Which Songstress",Rosemary Clooney +General,Chronos in Greek Saturnus in Roman Gods of what,The Harvest,General,What late night show replaced Tom Synder's show?,David Letterman,General,Who in 1994 knocked out Michael Moorer to become the oldest man ever to win a version of the World Heavyweight Boxing title,George foreman +General,Who Starred In The Very Last Program To Be Broadcast By The BBC Just Before The Start Of The 2 nd World War,Mickey Mouse, History & Holidays,"Which Margaret Mitchell novel, later a successful film, won the 1937 Pulitzer Prize? ",Gone With The Wind ,General,Who drew the Peanuts series of cartoons,Charles schulz +General,Where on your body are the most sweat glands,Feet,General,In Troy measurement a pennyweight contains 24 what,Grains,General,Which chief minister of Charles I of England was nicknamed Black Tom Tyrant?,Thomas Wentworth +General,The most common hat in the world is made out of what,Bamboo, History & Holidays,In Which Country Will You Find Lapland? ,Norway ,General,"What pop group recorded the title track for ""a view to a kill"" in 1985",A-ha +Entertainment,"Which comic strip was banned from ""Stars and Stripes""?",Beetle Bailey,People & Places,Which 2 People Are Now Best Associated With The Phrase 'You're Fired' (PFE)? ,Alan Sugar & Donald Trump ,Geography,What is the capital of Antigua and Barbuda,Saint john's +General,Bilrubin is produced by what part of the body,Liver,General,Who wrote Don't count your chickens before they are hatched,Aesop - Milkmaid and her Pail,General,Cashmere comes from a(n) _____,Goat +General,Who was the Chief Designer of the Hawker Hurricane fighter aircraft,Sidney camm,Food & Drink," Mustard, ketchup and onions on a hotdog are all ___________.",Condiments,General,Collective nouns - a knot of what,Toads +General,What is the currency of Egypt,The Pound,Music,Which Record Re-Entered The Charts For The 3rd Time For Donny Osmond In 1973,Puppy Love,General,Who was the only Russian born prime minister of Israel,Golda meir +General,What sort or creature is a Boto,Dolphin,General,Which places name means many islands,Polynesia,General,What number stays the same when yoU.S.quare it or cube it,One +General,"Who wrote ""The Old Man and the Sea""",Ernest hemingway,General,What is the logo of the reference work 'Encyclopedia Britannica'?,Thistle,General,What is a line drawn from an angle of a triangle to the midpoint of the opposite side,Median +Food & Drink,In Which Country Was Gin Invented ,Holland ,General,Teiichi igarashi climbed what mountain at the age of 99,Mount fuji,General,In London 1915 what became illegal subject to £100 fine,Buying a round of drinks +General,"In law, special court exercising jurisdiction over all maritime issues",Admiralty,General,What is the symbol for tungsten,W,Sports & Leisure,What trophy is awarded to the winner of the NHL play_offs,Stanley cup +General,What does Honolulu mean in Hawaiian,Sheltered Harbour,Geography,What country does the island of Mykonos belong to,Greece, History & Holidays,In What Century Were The War Of The Roses Fought ,15th Century  +General,What is the former name of Guinea-Bissau,Portuguese guinea, History & Holidays,"The ""Bay of Pigs"" fiasco took place in this country.",Cuba,General,"What links Da Vinci, Picasso, Charlie Chaplain, Ben Franklin",Left Handed +General,What kind of apple is on the beatles' apple label,Granny smith,General,The sea gods had a three pronged spear called a(n) ________,Trident,General,What Was Said To Be Hitlers Favourite Film,King Kong +Music,Who Was Standing In the Road,Blackfoot Sue, History & Holidays,What is the name of the lead singer for the Smiths? ,Morrisey ,General,What is a group of this animal called: Turtledove,Pitying dule +Music,"""Need You Tonight"" Was A Hit For Which Aussie Rockers",INXS,General,"What did the americans buy of Russia for $7,200,000 in 1867?",Alaska,General,Martina Navratilova won most doubles with which partner,Pam Shriver +General,What is the third movement of a symphony called,Minuet,General,"The mola mola, or ocean sunfish, lays up to how many eggs at one time",5 billion,General,What tv network features programming just for children,Nickelodeon +General,Laika was the first ever dog to do what,Go into space,General,74 year old Margaret Weldon FL 2 hole in one 2 days - unusual,She was totally blind aided hubby,General,As what is the tree with the botanical name 'Betula' better known,Birch +General,Who Was The First Musical Act To Have A No.1 Hit With The Stock Aitken & Waterman Hit Factory,Dead Or Alive,General,Walter and john huston became the first father-and-son team to win oscars for which film,Treasure of sierra madre,General,What common item was a sign of wealth in 19th century England,An Umbrella +Religion & Mythology,What's heaven to fallen Norse warriors,Valhalla,General,We know what Mardi Gras is but what's its literal translation,Fat Tuesday,General,Which Hollywood star has made the cover of Life most times,Elizabeth Taylor (11) +Music,What Was Rod Stewarts 1975 Nautical UK No.1 Single,Sailing,Music,"Whose Debut Solo Album In 1968 Was Titled ""Wonderwall""",George Harrison,Music,Where Is Elvis Buried,Graceland + History & Holidays,What was the initial capital of USSR?,Leningrad,General,A long tunic worn by men in the Near East,Caftan,General,"On the average, there are how many peas in a pod?",Eight +General,More than 40% of USA women were once what,Girl Scouts,Geography,Which Is The Oldest Surviving Thames Crossing In London ,Richmond Bridge ,General,In Aussie slang what are Bum Nuts,Eggs + History & Holidays,Which country was the first to allow women to vote in 1893? ,New Zealand ,Art & Literature,Name The 3 Bronte Sisters ,"Charlotte, Emily & Anne ",General,Dropped out of the sky onto the actors' heads,Green Slime +General,What fictitious murderer first appeared in String of Pearls 1840s,Sweeny Todd,Sports & Leisure,Which Sport Uses The Lightest Ball? ,Table Tennis ,General,Name the painting medium which involves the use of egg yolks,Tempera +General,What wife of a U.S. President was accused of being a spy,Mary todd lincoln,General,"Which Game Was First Patented Under The Name ""Spharistrike""",Lawn Tennis,General,Actress Jody Foster was a student at what college,Yale +Science & Nature,What Does Mass Multipled By Velocity Give You ,Momentum ,General,In what film did whoopi goldberg make her screen debut,Color purple,General,In Alabama its illegal to have more than 3 what in your house,Animals +Science & Nature,What Is The Collective Term For Ducks? ,A Paddling , History & Holidays,Who drafted most of the American Declaration of Independence?,Thomas Jefferson,General,Mapother IV is the real surname of what film star,Tom Cruise +General,"Who said about his songs ""some are 10 minutes long some are 6""",Bob Dylan,Art & Literature,What are arranged in the Japanese art of Ikebana?,Flowers,General,What was the very first James Bond movie that was shown in the 80's?What was the last?,For Your Eyes Only and Licence To Kill +General,What state has the bluebonnet as it's flower,Texas,General,What missionary station was built by albert schweitzer,Lambarene,General,Noctiphobia is the fear of,The night +General,"What Do Sir Isaac Newton, Kenny Everett & Annie Lennox All Have In Common",All Born On Christmas Day,General,Who played Eth in the radio show The Glums,June whitfield,General,"What are lust, pride, anger, envy, sloth, averice and gluttony",Seven deadly sins +General,What is Ronald Reagan's middle name,Wilson,Science & Nature,What Does The XP In The Operating System 'Windows XP' Actually Stand For? ,Experience ,Music,Who Played Eliza Doolittle In The Original Stage Production Of My Fair Lady,Julie Andrew +General,James hunt was disqualified after winning which grand prix,1976 british,Science & Nature,What is a male swan called,Cob,General,What is tuberculosis,Consumption +General,What was Ghandi's profession,Lawyer,General,Which part of the human eye may be removed and kept in an eye bank,Cornea,General,52% of Americans would rather spend a week in jail than What,Be President +General,What's the opposite of 'synonym',Antonym,General,What can be measured in angstroms,Wavelengths,General,"""Carmine"" is a shade of which color?",Red +General,"In World War 2, where was the defensive line known as The Gin Drinkers Line",Hong kong,General,What is the capital of the Spanish region of Navarre,Pamplona,General,Who is the roman counterpart of hera,Juno +Music,Which Film Director Did Madonna Marry In Scotland In 2000,Guy Richie,General,What song did michael jackson sing about a rodent,Ben,Food & Drink,A tayberry is a cross between which two fruits?,Blackberry and raspberry +General,What is the state flower of Wisconsin,Wood violet,General,Which playwright wrote Blithe Spirit,Noel coward,Science & Nature, __________ are the only animals born with horns. Both males and females are born with bony knobs on the forehead.,Giraffes +General,Which was the first Pinball game that used flippers ?,Humpty Dumpty,General,What is the Capital of: Cuba,Havana,General,The island of Yap has the worlds largest what,Coins up to 12 feet across +General,An animal described as ecostate lacks which physical feature,Ribs,Music,Which 80's Classic Became UK's Best Selling 12 Inch Single Of All Time,Blue Monday,General,What was the name of the world's first multi-coloured postage stamp?,Basel Dove +General,Which detective story writer created the character Tommy Beresford,Agatha christie,General,"In 1657 london, what was advertised as a cure for scurvy, gout and other ills",Coffee,General,What is a group of this animal called: Woodpecker,Descent +General,Who was the first prime minister of Israel,David ben gurion,Music,Name The Lead Vocalist With Wizzard In The Early 70's,Roy Wood,General,Who began his career as The Worlds Worst Juggler,Fred Astair +General,Who rode Rocinante,Don Quixote, History & Holidays,"Jesus was allegedly born on Christmas Day, what Starsign would he have been ",Capricorn ,General,What is the fear of rivers or running water known as,Potamophobia +Music,Who Knew Major Tom Was A Junkie,David Bowie,General,Which French author and philosopher was the cousin of Nobel Prize-winner Albert Schweitzer,Jean paul sartre,Music,"Where Was The Setting For ""On The Town""",New York +General,"Who played Samantha on ""Bewitched""",Elizabeth Montgomery,General,Who flies planes directly into tropical storms and hurricanes,53rd weather,General,Vivaldi the composer had what other profession,Priest +Music,What Are Tracy Thorn & Ben Watt Collectively Known As,Everything But The Girl,Geography,What is the capital of Swaziland,Mbabane,General,The Bahunia a five petal wild orchid is the symbol of where,Hong Kong +General,What seaport's name is spanish for 'white house',Casablanca,General,What is the name given to the science of improving the population by controlled breeding,Eugenics,Music,"From Which film did the Bee Gees ""Stayin Alive"" originally stem?",Saturday Night Fever +General,"What is 9 metres high, 7 metres wide and 2,500 kilometres long",Great wall of,Technology & Video Games,"In the game ""Chrono Trigger,"" what is Lucca's mother's name? ",Lara,Music,What Was The Name Of The Leisure Complex Founded In Nashville By Country star Conway Twitty,Twitty City +Science & Nature,If The Sum Of The Distances Between 2 Points Is Constant What Shape Do You Get ,Circle / Ellipse ,General,In North Dakota if you are in a covered wagon you can do what,Shoot mounted Indians,General,What British author was offered £250000 to write book of ET,Jeffrey Archer +General,Name the only war to end on the same day that the U.S. draft ended,Vietnam war,General,What is a noggin,A small cup,General,Where in the world can you find Friday before Thursday,In a Dictionary +Art & Literature,French impressionist Claude _____,Monet,General,Which islands capital is Flying Fish Cove,Christmas Island,General,Which US place name translates Indian as place of drunkenness,Manhattan +General,Vegetable with green or purple leaves forming a round head,Cabbage,General,"In ballet, a position of the body at an oblique angle and partly hidden.",Effacé,General,21% Americans don’t do it every day 5% never do it - what,Make their bed +General,What is the square root of the sum of 55+65+24,12,General,What is the shin bone,Tibia,Geography,Near Tewkesbury in Gloucestershire the river Avon flows into which other river ,River Severn  +Entertainment,"In the 'Nightmare On Elm Street' films, who played Freddy Krueger?",Robert Englund,Music,Which Oxford University Professor Of Poetry Had His Lyrics For Les Miserables Rejected,James Fenton,Science & Nature,What Is The Alloy Pewter Made From ,Lead & Tin  +General,What London landmark has an 11 foot long hand?,Big Ben,Music,Who Did The Stilettoes Go On To Become,Blondie,General,Collective nouns - A cloud of what,Gnats +General,Where on a horse is the pastern,Above the hoof,Geography,Which City Is The Capitaal Of Malaysia ,Kuala Lumpur ,General,Lagomorphs refer to which animal,Rabbits +General,"Famous Lyrics: ""There's a lady who knows all that glitters is gold and she's _____ _ _____ __ _____""",Buying a stairway to heaven,General,Who led the Soviets when they invaded Hungary in 1956,Nikita Khruschcev,General,Bureau of Alcohol Tobacco and Firearms bans what word in adds,Refreshing +General,What word did non-English speakers say sounded prettiest,Diarrhoea,General,What organisation opposes ASH,FOREST,General,Which element is extracted from the ore Scheelite,Tungsten +General,Leporine refers to what kind of animal,Rabbit,General,Who spent 18 months in 1940s as no 3188 home wayward boys,Steve McQueen,Food & Drink,Of which vegetable are Globe and Jerusalem varieties ,Artichoke  +Science & Nature,The Big Dipper is part of what constellation?,Ursa Major,General,On what common item would you find a keeper,Belt its loop that holds end,General,What country hosted the 1982 world cup of soccer,Spain +General,Sir Emest Swinton was a British colonel credited with the invention of which weapon,Tank,General,What is the young of this animal called: Seal,Pup,General,What is a group of boars,Singular +General,"In 'Star Trek', who was the captain of the 'Enterprise C'?",Rachel Garret,Science & Nature," A __________ gives nearly 200,000 glasses of milk in her lifetime.",Cow,Science & Nature," Since housecats are clean and their coats are dry and glossy, their fur easily becomes charged with __________",Electricity +General,Kainolophobia is the fear of,Novelty,General,Largest single user of almonds in North America.,Hershey,General,What war involving england began in 1899 and ended in 1902,Boer war + Geography,Which element makes up 3.63% of the Earth's crust ?,Calcium,General,What relation was Queen Victoria to George III,Granddaughter,Food & Drink,What is the name of the liquid butter made from cow or buffalo milk which is used in Eastern Countries? ,Ghee  +General,What is a Winston Churchill,Cigar,General,Ancient Aztecs of Mexico used a rabbit scale to measure what,Drunkenness few to 400,General,Way who lived on bonnie meadow way in new rochelle,Rob and laura petrie +General,What mineral salt is an important constituent of bones and teeth,Calcium,Music,Which One Of The Bands Hamburg Friends Designed The Sleeve Of The Revolver Album,Klaus Voormann,Music,According To Her Hit Single What Was Mary McGregors Dilemma,She Was Torn Between Two Lovers +General,"Which drink is known as ""the uncola""",7-up,General,How many best director Oscars did Alfred Hitchcock win,None,General,Who is the greek messenger god,Hermes +General,Where is tobruk,Libya,General,In what Australian state would you find Tamworth,New south wales nsw,Science & Nature,Swedish Botanist Anders Dahl Gave His Name To Which Flower ,The Dahlia  +General,What is the Capital of: Puerto Rico,San juan,General,"Which Hugely Successful 90's Movie Was Originally Entitled ""$3000""",Pretty Woman,General,"In the Bible, who was Abraham's first son",Ishmael + History & Holidays,*What was the name of the scandal that forced President Nixon to resign?* ,Watergate ,Food & Drink,"In French cookery, What is the earthenware dish in Which pat? is served? ",Terrine ,General,Christian Dior launched his 'New Look' in which year,1947 +General,What is liquid clay used in pottery,Slip, Geography,What is the world's deepest lake ?,Lake Baikal,General,What outdoor sport does 'love' mean a score of zero in,Tennis + History & Holidays,Which TV show portrayed the lives of performing arts high school students ,Fame ,General,Ignoring USA whose motto is E Pluribus Unum,Benfica Football Club,General,"Other than skiing, which sport takes place on a piste",Fencing +General,What blood type has been found in less than a dozen people since it was first discovered,Type ah,Science & Nature,What Was The Family Name Of The French Brothers Who Were Pioneer Developers Of The Hot Air Balloon And Who Conducted The First Untehered Flights ,Montgolfier ,Entertainment,Who was born on Krypton?,Superman +Geography,What city has a newpaper called the plain dealer ,Cleveland ,General,Ernest Hemmingway said what would protect against allergies,Having lots of sex,General,If you landed at Schipol airport where are you,Amsterdam +General,What first appeared at the 1928 Winter Olympics,Five Olympic Rings,General,How much did a mcdonald's hamburger cost in 1963,Fifteen cents 15 cents,General,John Huxham in 1750 invented which word,Influenza +General,Who appeared on the first cover of TV guide 3 April 1953,Desi Arnaz Jr,Sports & Leisure,"What is a Japanese sumo wrestling tournament called? *a Bisho, a Basho or a Bonko * ",A Basho ,General,Who first appeared on TV December 17th 1989,The Simpsons – Episode 1 in USA +General,Cord what colour is the umbilical cord,Blue,General,What is a military governer in japanese,Shogun,General,Which leader was defeated at the battle of Salamis,Alexander +General,Which two early 19th century German brothers wrote a collection of fairy tales,Grimm brothers, Geography,What is the capital of Vatican City ?,Vatican City,General,How do male moths find female moths in the dark,By smell smell +General,Taal is an alternative name for what language,Afrikaans, Geography,What city is on Lake Erie at the mouth of the Cuyahoga River?,Cleveland,General,The pedal bicycle was invented in which year,1839 +General,Which queen had menstrual cramps eased with marijuana,Queen victoria,General,"What avenue runs along the east side of Central Park, New York",Fifth avenue,General,Jorn Utzon of Denmark designed what landmark,Sydney Opera House +General,What is the Capital of: China,Beijing, History & Holidays,"Two groups have had three consecutive Christmas No 1's, the first was the Beatles, in 1963-64-65, name the second ",The Spice Girls (1996-97-98) ,Food & Drink,Which Rap Act Includes 'MC Taboo' And 'Will I Am' In Their Line Up? ,Black Eyed Peas  +General,What animal has the highest blood pressure,Giraffe,General,Where in your body is the labyrinth,Ear,Science & Nature, The __________ bird can fly at a speed of 260 miles per hour.,Frigate +General,According to Billboard what was the top single of the 60s,Hey Jude,General,"Which American president renamed ""Shangri-la"" as ""Camp David"" after his grandson",Eisenhower,General,Groundhog day is this day in february.,2nd +General,Worlds first paperback book written in 1867 by Goethe what title,Faust I,General,"Oranges and lemons, the bells of ______",St clements,General,What is the only patented uniform / costume in the USA,Playboy Bunny Girls +General,The Horned Planet is better known as what,Venus,General,What is the Capital of: Libya,Tripoli,General,What Was Arsenal Tube Station Called Before It Was Renamed To Arsenal,Gillespie Road +General,What do diners in a restaurant use to take away their leftovers,Doggy bag,General,What is a group of stars,Cluster,Science & Nature,This animal is kept as a house pet to kill cobras.,Mongoose +General,What religions sacred writings are divided into the Tripitaka,Buddhism, Language,From what language is the term 'finito'?,Italian,Food & Drink,What spirit is used in fortifying red wine to create port? ,Port  +General,Where is Bonnie Prince Charlie buried,Rome,General,Who founded mormonism,Joseph smith,General,"This ain't no party, this ain't no disco ..' were lyrics from which group's 1979 release 'Life During Wartime'",Talking Heads + History & Holidays,In which war was Bunker Hill a major battle? ,The American War Of Independence ,General,How was William Pitt the Elder's son known ?,William Pitt the Younger,Science & Nature,What is the atomic number of Bromine?,Thirty five + History & Holidays,In which European country did the use of Christmas trees originate ,Germany (5th Century) ,General,"Who was george burns' wife, whom he had a popular tv show with",Gracy allen,General,Who wrote the choral work the Messiah,George frederick handel +Tech & Video Games,Who created the music for N2O: Nitrous Oxide? ,The Crystal Method,Music,"What Song Features The Lyric ""So Tell Me What You Want What You Really Really Want""",Wannabe, Geography,Which country altered it's timezone in order to be the first to see in the year 2000 ?,Tonga +Music,What Was Westlifes First UK Number One Hit?,Swear It All Over Again,Music,Whose Albums Included Plastic Letters & Parallel Lines,Blondie,General,What is the traditional gift for a sixth wedding anniversary,Sugar +General,"Springsteen What country's explorers discovered the site of Pensacola, Florida",Spain,General,Indoors 6 players a side outdoors numbers vary what sport,Volleyball,General,The closest living relative of this African mammal is the Giraffe?,Okapi +General,By the time a child finishes elementary school they will have witnessed how many murders on television,"8,000",General,What was formerly called the Christian Revival Association and the East London Christian Mission,Salvation army,General,What subject is covered in the mag 'bondage',James bond +General,What place is known as 'the land nowhere near',Cape three points,General,What name is given to the parliament of Norway?,Storthing,General,What does 'rio de janeiro' mean in portuguese,January river +General,What is the latin phrase meaning 'for the particular end or purpose at hand',Ad hoc,General,The USA declared war on which country in 1898,Spain, History & Holidays,What popular US TV sitcom With 3 Words made its first appearance in 1951 & ran until 1957 ,I Love Lucy  +General,What is the name given to a group of geese,Gaggle,General,Who directed Spartacus and Lolita,Stanley Kubrick,General,Which county lies between the north sea and greater london,Essex +General,What relation to you is your uncle's father,Grandfather,General,Garlic and Chives belong to which plant family,Lilly,General,Sgt Jack Mills Was The Designated Driver In Which Infamous Journey In History,The Great Train Robbery +Food & Drink,Which fruit is used to make Calvados? ,Apples ,Science & Nature,Which is the largest nerve in the Human Body? ,Sciatic ,General,Who played the murder victim in the original version of 'psycho',Janet leigh +General,Women do it twice as often as men - what,Blink, Geography,Which tropic passes through Australia?,Tropic of Capricorn,Music,Whose First Chart Album Was Called Concerto For Group And Orchestra,Deep Purple +General,In the Bible Judah was in which province,Palestine,General,What is detective Hercules Poirot's brothers name,Achille,General,By law what unpopular thing must prostitutes now do in Holland,Pay Income Tax +General,Macrophobia is the fear of,Long waits,Science & Nature,What Is A Rhinoceros's Horn Made Out Of? ,Hair ,Entertainment,"This band's highly original video for ""Whip it,"" characterized by red flower pot hats was criticized for being both sado-masochistic and racist?",Devo +General,"Who said ""Its so long since sex I forget who gets tied up""",Joan Rivers,General,"Where In The Uk , Is Charles Dickens Buried",Westminster Abbey,General,What is a toboggan course,Cresta run +General,What was Anne's surname in Anne of Green Gables,Shirley,General,What was the last film where Sergio Leone directed Eastwood,The Good the Bad and the Ugly,General,What other name is used for the snow leopard,Ounce +Sports & Leisure,Which Premiership Club Play Their Home Games At The JJB Stadium? ,Wigan Athletic ,General,In fable who sold a cow for five beans,Jack ( and grew a beanstalk ),Music,“Roll Up And That’s An Invitation” What Were The Beatles Asking You To Roll Up For,Magical Mystery Tour +General,What was Spencer Tracy's last film,Guess whose coming to dinner,Music,In Which City Did The Beatles Perform Their Final Concert,San Francisco,General,"In Russia, what type of food is a blini or blintze",Pancake +General,In what Australian state would you find Canberra,Act,General,Science of control systems and communications,Cybernetics, History & Holidays,Who was President of the USA from 1953 till 1961? ,Dwight D. Eisenhower  +General,What product is consumed most in California,Bottled Water,General,What is a galeophobic afraid of,Sharks,General,Bees live in a hive what do seals live in,A Rookery +Art & Literature,In Which Story Does The Schoolboy (Piggy) Star ,Lord Of The Flies ,Science & Nature,A loss of memory is known as __________.,Amnesia,General,"Who wrote this line of poetry ""I wandered lonely as a cloud""",William +General,What did robert bunsen invent,Bunsen burner,General,Where did the eentsy-weentsy spider climb,Up the water spout,Geography,Which Country Is The Worlds Leading Exporter Of Salmon ,Norway  +General,Who was Olive Oyls boyfriend - before Popeye,Ham Gravy,General,The name of which element comes from the Greek for 'stranger',Xenon,General,Paris and What other capital had the worlds first telephone link,Brussels +General,In which North American city would you find the 1815 feet high C.N. Tower,Toronto,General,Who was the first UK solo artist to have a US number 1 hit,Acker Bilk Stranger on the Shore,Food & Drink,What Food Do You Use Up More Calories Eating Than You Gain Through Consumption ,Celery  +General,Charles Jung invented what in America,Fortune Cookies, History & Holidays,The date of which Christian festival was fixed in 325AD by the Council of Nicaea?,Easter,General,What is the fear of syphilis known as,Syphilophobia + History & Holidays,Who Was Ptolemy Dionysius Related To As Both Brother And Husband ,Cleopatra ,General,Which vertebrate holds the record for the longest recorded life span,Tortoise, Geography,Name the largest lake in Australia.,Eyre +General,In Georgia what can you not keep in your bathtub,Donkey,Food & Drink,After Whom Was The Desert 'Pavlova' Named ,The Russian Ballet Dancer Anna Pavlova ,General,Who is the only author to have a book in every dewey decimal category,Isaac asimov +General,What was the chief Roman silver coin,Denarius,Music,In 1979 who sang about Walking on the Moon?,The Police,General,Who is President of Germany,Roman herzog +Science & Nature,Are Worker Ants Male Or Female? ,Female ,Geography,Which Is The Highest Mountain In The Alps ,Mont Blanc , Geography,Jefferson City is the capital of ______?,Missouri +General,Who was the first man in space?,Yuri Gagarin,General,What metal are hot water pipes most often made of,Copper,General,What is a group of hornets,Nest +General,What is Virga,Rain the don’t reach ground,General,Which English speaking country consumes most table wine per,Australia, History & Holidays,Who in 1988 became the first elected female prime minister in an Islamic country ?,Benazir Bhutto +General,"In telephony, what do the initials ISDN stand for",Integrated services digital network,General,Name the pet alligator in Miami Vice,Elvis,General,Which drug company manufacture Viagra,Pfizer +General,The Dirty Harry franchise ran to five films what was the title of the final 1988 film,The dead pool,General,Cephalalgia refers to a(n)___.,Headache,General,Who forced 146 captured british officers into the black hole of calcutta,Indian troops +Sports & Leisure,Which footballer was sentenced to 3 months at Ford Open Prison in 1984 for drink driving and assaulting a police officer? ,George Best ,General,"From Which Country Does The Game ""Canasta"" Originate",Uruguay,General,"What was the name given to the short bobbed hairstyle, popular amongst upper class women of the 1920s",Eton crop +General,What is the flower that stands for: addresses rejected,Ice plant,Science & Nature,Encephalitis affects the ________.,Brain,Sports & Leisure,In Which City Were The 2000 Olympic Games Held? ,Sydney  +General,Basketball: the Milwaukee _______,Bucks,General,Albert Harry Jack and Samuel Eichelbaum known as who,Warner Brothers,General,In what city was Audry Hepburn born,Brussels +General,A Stand or Flamboyance is the collective noun for which type of bird?,Flamingo,General,Which country produces Tokay,Hungary,General,Which girls name means farseeing,Prudence +General,The telephone was invented in which year,1876, History & Holidays,Which actress played the same part in all 3 Scream movies ,Neve Campbell ,General,What was discovered at Qumran,Dead Sea Scrolls +General,Type of spongiform encaphalopathy affecting human beings and leading to dementia,Creutzfeldt-jakob disease,General,Who played Webster,Emanuelle lewis,General,"In India, and also the British forces, what job does a Dhobi Wallah do",Laundry +General,Unit of measure for energy,Joule,General,"Whose comment on the first moon landing was ""This is the greatest week in the history of the world since the creation""",Richard Nixon,Music,Who played guitar for 'The James Gang'?,Joe Walsh +General,Who was the second king of israel,David,General,A line drawn from an angle of a triangle to the mid-point of the opposite side is a(n) _______,Median,General,What U.S. state includes the telephone area code 513,Ohio +Music,Which Musical Was Built Around The Songs Of The Pop Group Abba,Mamma Mia,Entertainment,What other well known singer shares the same birthday as Elvis Presley (Jan 8)?,David Bowie,General,Thunder music is the literal translation of which musical term,Philharmonic +General,Which saint died about 601 AD,David,Science & Nature,What Is An Ovipositor ,It Is An Appendage Through Which A Female Insect Lays Her Eggs ,General,Another name for a slaughter house,Abattoir +General,What geographic term describes a hill with sharply sloping sides & a flat top,Butte, History & Holidays,What kind of teeth did George Washington have?,Wooden,General,What is the literal translation of terrapin,Eatable (Algonquin word) +Geography,What is the capital of Philippines,Manila,General,What is the musical form of Pygmalion,My fair lady,Music,Which Band Was Formed In 1994 By Fred Durst?,Limp Biskit +Music,Who Had A Number One Hit With “Fill Me In” In 2000?,Craig David,Sports & Leisure,"Was The Oxford And Cambridge Boat Race First Contested In The 18th, 19th Or 20th Century ",19th ,History & Holidays,How many Christmas trees are produced by Nova Scotia anually,1 +General,What class of ship was the Caine,Minesweeper,General,What greenish cylindrical fruit is the Cucumis sativus,Cucumber,General,Saying: don't switch horses in___.,Midstream +Food & Drink,Creme de Menthe and brandy make which cocktail? ,A Stinger ,General,Who played Popeye in the 1980 film of the same name,Robin williams,General,"Garnet, amethyst and zircon are all types of what",Minerals +General,Name Dennis the Menace dog Hank Ketchum comics 1950s,Ruff,General,Mount Logan is the highest mountain in which country,Canada,General,"If bats are nocturnal and horses diurnal, than coyotes and others animals that roam at the twilight hours and dawn are called:",Crepuscular +General,The 9 banded armadillo and humans have what in common,Both catch Leprosy,General,Whose biography is over 8.5 million words long,Winston Churchill,General,The symbols used on a map are explained by the ______.,Legend +General,Reeves who produced 'sgt pepper's lonely hearts club band',George martin,General,The Yucatan peninsula is mainly in which country,Mexico, History & Holidays,"What country was ruled by Pol Pot, leader of the Khmer Rouge party?",Cambodia (Kampuchea) +Geography,What Type Of Shop Predominates On The Ponte Vecchio In Florence ,Jewellers Have Had The Monopoly Since 1593 ,General,When was the first suburban shopping mall opened,1922,People & Places,According To Marilyn Monroe What Was The Only Thing She Wore In Bed? ,Chanel No.5  +General,What US state is the magnolia state,Mississippi,General,Smith Johnson Williams Brown Jones next US common surnames,Miller,General,In Algeria what is rai,A form of music +General,Wiley E Coyote chases roadrunner what does the E stand for,Ethelbert,General,Which is the largest artery in the human body,Aorta,General,In Indian cookery what dish literally translates as Red Juice,Rogan Josh +General,Thousand after what are the b52 bombers named,Fifties hairdo,General,In what sport would you find a Bagel,Tennis - Set won 6-0,Sports & Leisure,In Which Sport Is The Lonsdale Belt Awarded ,Boxing  + History & Holidays,Who was the first female American astronaut ?,Sally Ride,Geography,Which Is The Worlds 2nd Highest Mountain ,K2 ,General,How many episodes of Fawlty Towers were made?,12 + Geography,What is the capital of Algeria ?,Algiers,Science & Nature,What is the symbol for Iron?,Fe,General,Which part of the body is most sensitive to radiation,The Blood +Entertainment,Who collaborated with John Lennon on 'Whatever Gets You Through The Night'?,Elton John,Religion & Mythology,What dod Pandora Release when she opened the box?,Misery and evil,General,"What is the fear of lues, syphillis known as",Luiphobia +General,Dance with a Stranger was the film of who's life story,Ruth Ellis,General,Theophobia is a fear of ______,Religions, History & Holidays,What was King Arthur's mother's name?,Igraine +Art & Literature,"A band of painted or sculpted decoration, often at the top of a wall. ",Frieze,Music,"Which Japanese Artist Released The !973 Album ""Approximately Infinite Universe""",Yoko Ono,Music,Which US Pop Sensation Made Her Movie Debut In The Movie Crosroads,Britney Spears +General,Who's first girlfriend was named Thelma Pickles?,John Lennon,General,"Name Any Year During The ""Reign"" Of Genghis Khan",1206-1227,Science & Nature,Does The Male Or Female Horsefly Feed On Blood ,The Female  +General,What keeps growing until you are 35 then starts to shrink,Your Skeleton,General,"In international car registrations, which country has the letters ZA",South africa,General,Near Ayres Rock is a lake named after which composer,Mozart +General,Who was the first African American to play in a NBA game,Earl Lloyd,General,What is the large central inner tower of a castle called,Keep,Entertainment,What was the name of Ross' pet monkey on 'Friends'?,Marcel +General,Which building material gets its name from Arabic for the brick,Adobe,General,"Who sang ""cold, cold heart""",Hank williams,General,What relative of King Faisal of Saudia Arabia assassinated him,Nephew +General,What Was The First TV Show To Be Filmed In Colour,Stingray,Music,Ace Of Base Come From Which Country,Sweden,Music,For which singer were The Beatles a backing group for in 1961 before hitting the big time,Tony Sheridan +Entertainment,Where did Clark Kent attend college?,Metropolis University,General,In Ackworth Georgia all citizens must own what by law,A Rake,General,What was the fight between Argentina and Great Britan over?,Faulkland Islands +General,"Which Comedian Has The Real Name Of ""Robert Davis""",Jasper Carrott,General,What isnt present in a fillet,Bones,General,Who was the female star of the film Klute,Jane fonda +General,What is the medical term for an eyeball shaped like a rugby ball,Astigmatism,General,What African republic's name was inspired by its thriving elephant tusk trade?,The Ivory Coast,General,Arthur Sarsfield Ward the creator of Fu Man Chu is which author,Sax Rohmer +Science & Nature,How Long Can A Stag Beetle Spend As A Larva ,Up To Three Years ,Sports & Leisure,Which Colour Ball Is Not Placed Back On The Table If It Is Illegally Potted ,Red ,Religion & Mythology,Who is the Norse god of lightning?,Odin +General,Rebecca Rolfe Is More Commonly Known As Who,Pocahontas,General,What is the state capitol of New Jersey,Trenton,General,"Whose members get ""Promoted to Glory"" on their death",Salvation Army +Entertainment,What is the address of The Munsters?,1313 Mockingbird Lane, History & Holidays,What is the English title of the carol written in 1818 by Austrian priest Josef Mohr originally called Stille Nacht? ,Silent Night ,General,Crown - Ring - Shank - Stock - Fluke parts of what,Anchor + Geography,In what mountain range is Kicking Horse Pass?,Rocky,General,What is the name for a person hired to carry a golfer's clubs for him,Caddie,Geography,What is the capital of Fiji,Suva +General,"In an average lifetime, the average american charges on _____",Credit cards,General,Spaceman Spiff was a charcter on which eighties cartoon strip?,Calvin and Hobbes,Art & Literature,"A European movement of the late eighteenth to mid-nineteenth century. In reaction to neoclassicism, it focused on emotion over reason, and on spontaneous expression. ",Romanticism +General,"Which cow disease was first identified in Britian in 1986 and by 1996 had claimed 158,000 cattle",Bovine spongiform encephalopathy,General,Name the Rolling Stones second album,Rolling Stones No 2, Geography,What country's capital is Caracas?,Venezuela +General,"In the film 'home alone', who played the baddies",Joe pesci and daniel stern, Geography,What is the capital of Namibia ?,Windhoek,General,What was E.T.'s favorite candy?,Recee's Pieces +Science & Nature,What is the highest active volcano in the world?,Cotopaxi,Music,Which Working Hours Did Dolly Parton Sing About,9 to 5,General,What is the wrought iron tower in paris,Eiffel tower +General,On what does the firefly depend to find mates,Sight,General,Where are plaques of humans wearing nothing,Pioneer spacecraft,General,The Chinese ideograph with two women under one roof means what,Trouble +General,"Whose suicide made robert mitchum sigh, ""she seemed like a lost child""",Marilyn monroe,People & Places,Which actress was the first wife of Ronald Regan? ,Jane Wyman ,Sports & Leisure,In 1998 Who Became The Youngest Footballer To Score A Hat Trick In The English Premiership ,Michael Owen  +General,Who was captain of 'the mayflower',Miles standish,Science & Nature,What is Borborygmus?,Stomach Noises,General,"Football"" the oakland ______",Raiders +General,On which motorway are the Michael Wood and Gordano service areas,M5,Entertainment,The Who's rock musical stars Elton John. It's called ________.,Tommy,General,"In 'alice in wonderland', who never stopped sobbing",Mock turtle +General,What is the fear of gods or religion known as,Theophobia,Music,What Was The One Hit Wonder For Little Anthony And The Imperials In 1976,Better Loose Your Head,Music,"Who Has Been A Founder Member Of Yazoo, Depeche Mode And Erasure?",Vince Clarke +General,Who was the female star of the film 'Seperate Tables',Deborah kerr, History & Holidays,Who denounced Stalin?,Nikita,General,In Rosemead California its illegal to eat what with a fork in public,Ice Cream +General,England its illegal for a lady to do what on a public conveyance,Eat Chocolates,General,"What completes the term ""double-_____"" which forms the structure of DNA",Helix,Mathematics,What is the name given to the number equal to 10 raised to the power of 100?,"A ""googol""" +General,What boys name means Rich Guard,Edward or Edmund,General,What is the ratio of the speed of an object to the speed of sound in the surrounding medium,Mach speed,General,What religion has mosques as its places of worship,Islam +General,Wwhat is the name of Mulder and Scully's supervisor on the X-files?,Walter Skinner,General,For what is the chemical symbol h2o2,Hydrogen peroxide,General,What type of chemical are adrenaline and oestrogen,Hormones +General,Estelle Parsons best supporting actress Oscar what 1967 film,Bonnie and Clyde,Music,Which Band Did David Coverdale Form After Leaving Deep Purple,Whitesnake,General,What is meat slaughtered according to Muslim law,Halal +General,"In egyptian mythology, who was isis the wife of",Osiris,General,What is th name of the famous hyrogen gas filled airship that crashed in 1936?,Hindenburg,Food & Drink,"Which food connects Blanche Ames, Lord Lambourne and Willis Williams? ",They're all Varieties of Apple  +General,"Who recorded the album ""Wings Over America"" in 1976",Paul McCartney,General,In which book of the bible are the Ten Commandments first listed,Exodus,General,In which group would you find Jarvis Cocker,Pulp +General,Worker ants may live up to how many years,Seven,Music,Name The Singer And The Song Of Brian Hyland's Hit Reaching No.1 In 1989,Jason Donovan / Sealed With A Kiss,General,Mary Robinson became president of Ireland in which year,1990 + History & Holidays,When And Over What Was The First British Referendum Held? ,"1975, Membership Of The EEC ",Sports & Leisure,What type of testing was first carried out at the Olympics in 1968? ,Sex Testing ,General,What is a group of this animal called: Buck,Brace clash +General,What is the Australian sea wasp,Jellyfish,General,In Disney's Jungle book name four vultures,John Paul George Ringo,General,"Which trophy is inscribed ""The Gentlemen's Single-Handed Championships of the World""",Wimbledon men's singles cup +Music,"What Connects Jeff Healey, Ray Charles, Stevie Wonder",All Are Blind,General,What term is derived from the practise of clans of long ago wanting to get rid of their unwanted people without killing them so burned their houses down instead,Getting fired,General,What is a male cat called?,Tom +General,Which country blew up a greenpeace ship in new zealand,France,Music,With Which Group Do You Associate David Ruffin & Eddie Kendricks,The Temptaions,Art & Literature,What Did Winston Encounter In Room 101 ,Rats  + Geography,What is the principal river of Ireland?,Shannon,General,Mardi gras is French for ___________,Fat Tuesday,General,What was the name of the changeling on Deep Space Nine,Odo +General,What Russian revolutionary founded Pravda,Leon Trotsky,General,What country's major seaport is alexandria,Egypt,People & Places,Who was elected as the Conservative MP for Henley in 2001? ,Boris Johnson  +General,What name is given to the current of warm water that flows across the Pacific to Peru,El nino,Sports & Leisure,Who Won The Young Persons BBC Sports Personality Of The Year Award In 2006? ,Theo Walcott ,General,Which hats became popular with children in 1956,Davy Crocket +Sports & Leisure,What Is Lepidoptery Better Known As ,Butterfly Collecting ,Science & Nature,What animal lives in a warren?,Rabbit,General,And in 1985 who was on the last one produced,Rambo +General,What's the plural of 'larva',Larvae,General,Who is the Patron Saint of sailors,Saint Elmo,General,An anti-tussed is used to treat what,Coughs +General,Which russian scientist used dogs to study conditioned reflexes,Ivan pavlov,General,What is the name of the metal discs set in a tambourines rim,Jingles,Sports & Leisure,What Annual Sporting Event Takes Place Between Putney And Mortlake? ,The University Boat Race  +Food & Drink,After Who Was The Sandwich Named ,The Earl Of Sandwich ,General,Who draws the Far Side series of cartoons,Gary larson,Food & Drink,In which English county is Brie made? ,Somerset  +General,Who was the first Christian missionary?,Paul,General,When was the cascade tuning radio receiver invented,1913,General,What part was played by Patrick Newell in the series The Avengers,Mother +Sports & Leisure,Who won a boxing gold medal at Sydney in 2000 in the super heavyweight division? ,Audley Harrison ,Art & Literature,Who created 'The Saint'?,Leslie Charteris,General,In the wild what animal pollinates banana plants,Bats +General,Where is the site of the U.S. bullion depository,Fort knox,General,Which leader lives in the Potola,Dalai Lama, History & Holidays,"If you were born on Halloween, what star sign would you be ",Scorpio  +General,Which famous athlete was was a Tory M.P. from 1959-66 and 1969-74; latterly holding ministerial office,Chris chataway,General,An animal described as ecaudate lacks which physical feature,Tail,General,Term literally jointed foot applies to insects spiders and crabs,Arthropods +General,How many locks are there on the Suez Canal,None,General,Who is the Roman Goddess of invention and wisdom,Minerva,General,"Tylenol, darvon, & aspirin are examples of _____",Analgesics +General,"Who is the Norse god of fertility, sun, & rain",Frey,General,If you suffer from protanopia you cannot see what,Colour red,General,Name Woody Allen's black and white romantic study of his home town,Manhattan +General,One of the band steps is nicknamed H what's it stand for,Hyperactive,General,South africa is the biggest producer and exporter of ______?,Mohair,General,"After five years as ""Suzanne Sugarbaker"", Delta Burke left what hit show",Designing Women +General,A person with refined taste in food and drink,Epicure,General,Who wrote the Star Spangled Banner,Francis Scott-key,General,Who is the leader of the inkatha freedom party,Mangosuthu buthelezi +Music,"Who Organised The Legendary 1972 Bickershaw Festival Starring The Grateful Dead, Captain Beefheart, & Donovan",Jeremy Beadle,General,"""Victor Willis, Felipe Rose, Randy Jones, David Hodo, Glenn Hughes, & Alex Briley"" Are All Members Of Which Chart Topping Group",The Village People,General,What's the word for the luminous mist that surrounds a saint,Nimbus +Food & Drink,"In Italy, if you were served pesce martello, what would you be about to eat? ",Shark ,General,Which Greek island is also a variety of lettuce,Cos,General,What is also known as liberty cabbage,Sauerkraut +Tech & Video Games,Who is the famed musical director for Square's Final Fantasy series? ,Nobuo Uematsu,Music,Anna Mae Bullock Is The Real Name Of Which Singer,Tina Turner,General,Which Nobel Prize winner wrote 'The Old Man and the Sea,Ernest hemingway +General,"Equestrian, Yachting and what Olympic sport are sexes equal",Shooting,Music,"Which Swingers Were Doing ""The Hippy Hippy Shake"" In 1963",The Swinging Blue Jeans,General,What does susan b. anthony's middle initial stand for,Brownell +General,What is the current no 1 aphrodisiac (reputedly),Asparagus,General,"In rugby, what is the equivalent of a hockey face-off",Scrum, History & Holidays,Name the incident in which tea was dumped into the harbour.,Boston Tea Party +General,Who is Goldie Hawns actor partner,Kurt russell,General,What was the ceaseless medieval search for a method of turning base metals to gold called,Alchemy,Music,"Brian Jones Died In A Swimming Pool At Cotchford Farm, Sussex But Which Author Once Owned The Farm",A A Milne (Winne The Pooh) + History & Holidays,What planet was 'ALF' from? ,Melmac ,General,Ad Lib is short for the Latin Ad libitum what's it literally mean,At Pleasure,General,Which actor refused the leading role in Laurence of Arabia,Marlon Brando +Sports & Leisure,What Country Was The First To Win The World Cup & What Country Was The First To Host It (PFE) ,Uruguay & Uruguay ,Science & Nature," The Ozark blind salamander begins life with eyes and plumelike gills. As the animal matures, its eyelids fuse together and the gills __________",Disappear,General,What is the fear of birds known as,Ornithophobia +General,What is the address Donald Duck lives at,"1313 webfoot walk, duckburg, calisota",Science & Nature, A shrimp has __________ pairs of legs.,5,General,Where is the worlds largest gold depository,Federal reserve bank Manhattan +General,What is the unit of currency in Hungary,Forint,General,What did the US government call predawn vertical insertion,Invasion of Granada,General,What are you supposed to give/get for 40 years of marriage,Ruby +General,One of Ferdinand Magellen vessels completed the first circumnavigation of the world in which year,1522, History & Holidays,Which military battle took place in 1815?,Waterloo,General,The mersey river runs through _____,Liverpool +General,How long is Lent is western churches? ,40 Days ,General,What metallic element is essential to the red blood cells,Iron,General,What is the Capital of: Suriname,Paramaribo + History & Holidays,According to the Bible who is Jesus's father ,God ,General,Champagne bottles - 6 bottles of champagne are in a what,Rheoboam,Food & Drink,Forks Were Originally Known As What ,Splitspoons  +Food & Drink,What Is A Ramekin ,A Small Casserole Dish ,General,Whose nickname was 'babe',Mildred ella didrikson,General,Name the first mailman in Philadelphia,Benjamin Franklin +General,Pilgrims visit Mecca where is Mecca,Saudi Arabia,General,Who is the biggest producer and exporter of mohair,South africa,General,Ailurophobia is the fear of?,Cats +General,March April and May are the only months that have what,Anagrams Charm Ripal Yam,General,Which U.S. film actress was married to Mickey Rooney and Frank Sinatra,Ava gardner,Music,Who had a hit song in 2007 called “I don't feel like dancing”,SCISSOR SISTERS +General,"Puritan preacher who said ""Wine is from God, the drunkard is from the Devil""",Increase mather,General,What are you doing if you pandiculate,Yawn,General,What is measured in ohms,Electrical resistance +General,Which Canadian island is the setting for 'Anne of Green Gables,Prince edward island,General,What's the capital of paraguay,Asuncion,General,M31 is the nearest galaxy to us - what is its other name,Andromeda +General,"What ship sank in ""a night to remember""",Titanic,General,Franki valli and the _______,Four seasons,General,Scottish clan were traitors and caused the Glencoe massacre,Campbells +General,By the time a child finishes elementary school they will have witnessed how many acts of violence on television,100000,General,Who was the first UK royal interviewed on television,Prince Philip,Geography,Into which sea does the Nile flow?* * ,The Mediterranean  +General,Stewart Goddard changed his name to become what pop hit,Adam Ant,General,Which character in TV's Red Dwarf is a hologram,Rimmer,General,How many pounds are there in a stone,Fourteen +General,Which English Prime Minister was known as 'the Great Commoner'?,William Pitt the Elder,Music,Which Song About Contraception Was Blazoned Across Countless White T-Shirts,Relax,Religion & Mythology,These wounds of christ mysteriously appear on believers who sometimes weep blood as well.,Stigmata +General,Who is the lead singer of the group doors,Jim morrison,General,The Fort George Point in Belize City was formed by the silt runoff of what hurricane,Hattie,General,Who would use a Pig'in String,Calf Roper Rodeo – Tie its feet +General,Which Novel Was The First To Be Written On A Typewriter,Adventures Of Tom Sawyer,General,Name the first British film studio set up in the 1930s,Pinewood Studios,General,The Suez Canal was nationalized in what year,1950 +General,In Guelph Ontario a by-law makes what illegal in the city,Peeing A no Pee zone,General,What nationality was the entertainer Victor Borges,Danish,General,What is the Badger state,Wisconsin +General,In which European city is Charles university,Prague,General,What are the clouds of magellan,Galaxies,General,What toy company is the world's1 maker of female apparel,Mattel +General,What is the young of this animal called: Pigeon,Squab squeaker,General,What is the fear of leprosy known as,Leprophobia,General,Which is the only country to represent a letter in the phonetic alphabet,India +Toys & Games,The easiest to defend continent in Risk,Australia,General,"The Movie Star ""Mary Cathleen Collins"" Is Better Known As Who",Bo Derek,Sports & Leisure,Who Beat Mike Tyson In 1990 To Become Undisputed Heavy Weight Boxing Champion Of The World ,James Buster Douglas  +General,In dry measure 8 quarts are a what,Peck,General,U.S. captials Montana,Helena,General,A receptacle for holy water is a(n) ________,Font +General,Gerald Thomas directed what series of films,Carry on Films,General,What are alabaster marbles,Alleys,General,Lexico was invented in 1932 what did it change its name to,Scrabble +General,What links Sivan Av Tevat and Adar,Jewish months,General,In Ashville North Carolina its illegal to do what on the streets,Sneeze,Music,What is the stage name of the rap star born Curtis Jackson,50 Cent +General,Winnie the Pooh lived where,Hundred Acre Wood,General,"In Greek mythology, the nymph Callisto was turned into which creature",Bear,General,Julian Dick George Anne and who make the famous five,Timmy the Dog +General,What do men do half as much as women,Blink,General,What is the fear of halloween known as,Samhainophobia,Sports & Leisure,Baseball: The Milwaukee ______?,Brewers +General,"In 'startrek', who did leonard nimoy play",Dr spock,General,Who is the roman goddess of harmonious relations,Concordia, History & Holidays,Which was the last of the 5 civilized tribe to arrive in oklahoma ,Seminole  +General,Which song did the easybeats record that everyone sang at the end of a work week,Friday on my mind,General,What was the name of Frank Zappas first band,The Ramblers,General,What are the sandals called that are worn in ceremonial japanese tradition?,Tabi +General,Who Originally Said Of Gerald Ford That 'He Was So Dumb That He Couldn't Fart And Chew Gum At The Same Time',Lyndon B Johnson, History & Holidays,Which does Carrie's Mother Warn Her Will Happen If She Attends The Prom ,They are all going laugh at you ,General,What is the Capital of: Turkey,Ankara +General,If an Aussie called you a Bananabender what would he mean,You were from Queensland state,General,What is the name of the police inspector that is bent on getting Valjean in Les Miserables,Javert,Geography,What Is The Largest Lake In England ,Windermere  +General,"Born May 7, 1901, He Starred In This Movie: Dallas - 1950",Gary cooper,General,What chemical substance can halophytic bacteria tolerate,Salt,General,"Which Irish pop band consists of' Paul Hewson, Larry Mullen, David Evans and Adam Clayton",U2 +Science & Nature,Which metal has the chemical element W? ,Tungsten ,General,Vigesimal is which base numbering system,Base twenty,General,Who uses the slogan 'aol',American online +General,What colour is Llamas milk,Yellow,General,Insulin is commonly used to treat which condition,Diabetes,People & Places,"About Whom Did President Eisenhower Say 'I Just Will Not, I Refuse To Get Into The Gutter With That Guy' ",Senator Joseph McCarthy  +Entertainment,For which ad campaign was the line 'I can't believe I ate the whole thing' used?,Alka Seltzer,General,Of which country was Salvador Allende president,Chile,General,What is tina turner's real name,Annie mae bullock +General,Who is the persian god of light,Mithra,General,A fingernail or toenail takes about how many months to grow from base to tip,Six,General,Who designed the first feasible automobile with an internal combustion engine,Karl f benz +General,What is the national sport of Finland,Motor Rallying,General,How many teeth constitute a set of adult teeth,Thirty two,Music,"Which 1970's song and singer “It's late September and I really should be back at school, I know I keep you amused but I feel I'm being used”?",Rod Stewart / Maggie May +General,Who was the black assistant of mandrake the magician,Lothar,General,Who won the eurovision song contest twice,Johnny logan, History & Holidays,Who wrote How the Grinch Stole Christmas? ,Dr Seuss  +General,Which doctor loved Lara Antipova,Dr zhivago,General,What do ungulate animals alone have,Hooves,General,Where would you find a bema narthex and apse,In a Basilica +Science & Nature, A crocodile can't stick out its __________,Tongue,Music,Who had a hit with Lazy Sunday in 1968?,The Small Faces,General,"Which Famous Movie Features A Car With The Licence Plate ""ECTO 1""",Ghostbusters +General,If yoU.S.uffer from ailurophobia what are you afraid of,Cats,General,The Indiana Pacers basketball team retired 35 which used to belong to _____,Roger brown, History & Holidays,In which film is the bad guy a one handed criminal named Han who deals in drugs and white slavery ? ,Enter the Dragon  +General,"Who Said ""Who controls the past controls the future. Who controls the present controls the past""?",George Orwell,General,"In the Bible, who led 10,000 soldiers into battle against the Midianites",Gideon,General,What is the name of the man who gave his name to the World Cup Trophy,David rimet +Music,Name Paul McCartneys Back Up Group On His We All Stand Together Single,The Frog Chorus,General,Brian Lara broke the record for the highest first class cricket innings by scoring 501 not out against which county in 1994,Durham,General,"Which company produced the World War Two aeroplane, the 'Lightning'",Lockheed +General,What popular drink was introduced at the St Louis World's Fair in 1904,Ice,General,"Who is the mayan earth and moon goddess, and patroness of pregnant women",Ixchel,General,Jack Nicholson and Jessica Lange starred in the remake of the film 'The Postman Always Rings Twice' in what year,1981 +General,Which writer coined the word Cyberspace in 1984,William Gibson – Neuromancer,General,What is the most stolen item in US drugstores,Batteries cosmetics,General,"What was the real name of the actress, Gloria Swanson",Gloria may josephine svensson +General,Who won the 1988 Superbowl,Washington Redskins,General,In standard cine film how many frames are shown each second,24,General,Mary Cathleen Collins changed her name to what,Bo Derek +General,What is the Capital of: Aruba,Oranjestad,General,What kids tv show celebrated its 20th season in 1988,Sesame street,General,What is the flower that stands for: ambition,Mountain laurel + History & Holidays,The Christmas Tree Displayed In Londons Trafalgar Square Is Traditionally Donated By Which Country ,Norway ,General,Bonnie Booth (38) used what to remove a corn from her foot,410 shotgun – after razor hurt,General,What's the royal dog of china,Pekingese +General,"Countries of the world:central Europe, the capital is Prague",Czech republic,Music,"Which Female Vocal Group Started A String Of Hits With Stay In ""1993""",Eternal,General,"Which metal, with the Atomic Number 3, is the lightest known solid element",Lithium +General,What dog has the best eyesight,Greyhound,General,Where in the human body is the round window,Ear, History & Holidays,On which Christmas day did English and German troops play football ,1914  +General,What was Madam Curie's husbands name,Pierre,Food & Drink,"White Russian Cocktails Are Made From Milk, Vodka And Which Liquer ",Kahlua ,General,Film The Dead Heat Merry go Round 60s what stars first 1 line,Harrison Ford +General,"What was first played at Ballarrat gold fields, Australia in 1853",Australian rules football, History & Holidays,Who played the first screen incarnation of Dr. Hannibal Lecter ,Brian Cox ,Music,"In Which Year Did Micheal Jackson Top The Charts With The Song ""Black Or White""",1991 +General,"Spanish: how do yoU.S.ay ""fifty""",Cincuenta,General,The story about a king who prematurely divides his kingdom between his daughters is portrayed in which film,King lear,General,Atlanta burned in Gone With the Wind was what old film set,King Kong it needed clearing +General,"The first programmable machine was made in 1808, what was it",Loom,Music,Who Composed The Score For Gentleman Prefer Blondes And Funny Girl,"Jule Styne, Last Of Broadway Giants",General,What tv series starred six female impersonators during its 17 year run,Lassie +Geography,"How Did Henry Stanley Carry His Boat, The Lady Alice Overland ",He Divided It Into 8 Sections ,General,In musical notation which note is half a minim,Crotchet,General,What East Indian herb of the family Pedaliaceae linked Ali Baba,Sesame +General,In The World Of Music How Is Raymond Burns More Commonly Known,Captain Sensible,General,Superstition says that this birds feathers should never be inside a house,Peacock,General,Motor racing across country or on unmade roads,Autocross +General,Where did aristotle believe the seat of intelligence was,Heart,Sports & Leisure,In Which Game Might You Tug On Your Ear ,Charades ,General,What is the Capital of: Saudi Arabia,Riyadh +General,What is bovine spongiform encephalopathy,Mad cow disease,General,Spanakopia is a Greek pie filled with what,Sautéed Spinach,General,What is Agoraphobia the fear of?,Open Spaces + History & Holidays,Britain's first stretch of motorway was opened in 1958 to bypass which northern town (now a city) ,Preston ,General,What is the world's longest river,Nile,People & Places,In Which African Country Is The Seaport Of Banana ,"The Democratic Republic Of Congo , Formally Zaire " +General,What did members of the nazi ss have tattooed in their armpits,Blood type,Music,Who Was Married To Nirvana's Late Lead Singer Kurt Cobain,Courtney Love,General,What gives its name to a protein found in the human blood,The rhesus monkey +Science & Nature,Heroin is the brand name of morphine once marketed by which pharmaceutical company?,Bayer,General,The PH scale measures acid or alkali what's PH stand for,Potential of Hydrogen,Sports & Leisure,What swimming stroke is named after an insect?,Butterfly +General,In what city 1985 was the worlds first computer museum opened,Boston,General,Named After A Bird What Is The Specific Tertm In Golf For The Unusual Score Of Four Under Par?,A Vulture,General,How high is a baketball hoop?,10 Feet +General,Whose likeness is displayed on the purple heart medal,George washington,General,A Retifist has a fetish about what,Shoes,General,In which country would you find the Negev desert,Israel +Sports & Leisure,Which Film Starring John Candy Told The Story Of The Jamaican Bobsleigh Team ,Cool Runnings , History & Holidays,Where Did Harold McMillan Make His Famous Winds Of Change Speech In 1969 ,"Cape Town, South Africa ",Science & Nature,What is the only day named after a planet?,Saturday +General,What is a gricer interested in?,Trains,General,What does an odometer measure,Speed,Sports & Leisure,There Are 2 Major League Baseball Teams In New York City Cab You Name Them PFE ,Yankees & The Mets  +Entertainment,Which films are about the Corleone family?,The Godfather films,General,Who wrote the opera 'norma',Vincenzo bellini, History & Holidays,"What type of men's jacket featured it's name on the outer breast pocket,and epaulets on the shoulders? ",Member's Only  +General,Floating wreckage at sea,Flotsam,General,Who created the cartoon character 'Bugs Bunny',Chuck jones,General,What is the only word in the english language that begins and ends with 'und',Underground +Science & Nature, The biggest frog is the appropriately named __________ frog (Conraua goliath) of Cameroon. They reach nearly 30 cm (a foot) and weigh as much as 3.3 kilograms.,Goliath,People & Places,Which Famous Child Actor of the 80's is presently giving evidence at the trial of Michael Jackson ,Corey Feldman ,General,What is the nickname for South Dakota,Coyote state +General,What roman emperor does the King of Diamonds represent,Julius caesar,General,What the most north eastern state of the contiguous U.S.,Maine,General,What Was The First Ever Consumer Product To Contain Nylon?,The Toothhbrush +Food & Drink,The Name Of Which Chinese Dish Means 'Odds & Ends' ,Chop Suey ,General,In what Shakespeare play does the character Caliban appear,The Tempest,General,What is the fear of voids known as,Kenophobia +General,"Link the sports Cricket, Rackets, Croquet and Motorboat racing",Only appeared once in Olympics,General,On the same subject who eventually married Lord Peter Wimsey,Harriot Vane, History & Holidays,In which state was `The Blair Witch Project' set ,Maryland  +General,"Who wrote 'Bliss was it in that dawn to be alive, but to be young was very heaven'",William wordsworth, History & Holidays,Which fellow singer was Roberta Flack paying tribute to in her song 'Killing Me Softly ,Don McLean ,General,The Pentagon accidentally ordered 82 year supply of which food,Freeze dried Tuna salad mix +General,What does wyatt earp's headstone say,..that nothing's so sacred as honor and,Music,"Who Recorded The Albums ""All Over The Place"", ""Distant Light"", ""Everything""",The Bangles,General,Who is the Greek god of shepherds & flocks,Pan +General,Who played bonnie to warren beatty's clyde,Faye dunaway,General,How many musicians are there in a nonet,Nine,General,Who was Cleopatra's first husband,Ptolemy Dionysus – her brother +Music,"Which Band Recorded The Album ""Hysteria""",Def Leppard,General,What capitol city means Bay of Smoke in the local language,Reykjavik,Music,Number of solo albums by individual Beatles,71 +Music,On the 80's album Purple Rain who backed Prince?,The Revolution,General,This space station killed a cow on re entry into earth's atmosphere,Skylab,Music,In Which UK City is Abbey Road?,London +General,"Which classic Hollywood western is based on the film ""The Seven Samurai",The magnificent seven,General,Men must toss what at least 3 times during Olneys Great Race,A Pancake,General,In what sport is a 'chukka',Polo +General,What two-person group recorded the song 'love will keep us together',Captain,General,What kind of rain was New York cop Michael Douglas running through in Japan in 1989,Black rain,General,What is the flower that stands for: beauty and prosperity,Red-leaved rose +General,Who was the leader of the wolves in Kipling's Jungle Book,Akala,General,What is the fear of wasps known as,Spheksophobia,General,What caused the computer in Electric Dreams to become alive?,Spilled Champagne +Music,Who Had A Hit In 1981 With The Song Daddy's Home,Cliff Richard,General,Where does a tortoise have an upturned shell at its neck so it can eat cactus brances,Galapagos islands,General,Who founded the Missionaries of Charity in 1948,Mother theresa +Sports & Leisure,Monza & Silverstone Are Venue's For Which Sport? ,Formula One Racing ,General,Famous 20th century novel is set mainly on fictional Pianosa,Catch 22,General,What is the young of this animal called: Rat,Kitten +General,"What novel contains the line: ""who promoted major major""",Catch 22,General,An arched brick or stone ceiling or roof.,Vault,Music,"At This Moment Was A Hit For ""Bob & The Boot Leggers"" Or Was It ""Billy & The Beaters""",Billy And The Beaters +General,What is the real identity of aquaman,Arthur curry,General,"Who played edgar linton in william wyler's ""wuthering heights""",David niven,General,What term was used from 1914 onwards to describe music emanating from New Orleans,Jazz +Science & Nature,What Is The Square Root Of 144 ,12 ,General,Whats the last word of the bible,Amen,General,Which 20th century leader introduced the custom of carrying a flaming torch from Athens to the site of each Olympic games,Adolf hitler +General,At which village in West Sussex was a Roman palace discovered in 1960,Fishbourne, Geography,What is the capital of Bosnia and Hercegovina ?,Sarajevo,Music,"In The 10 Years Between 1972 & 1981 John Peel Was Voted Best DJ In The NME Readers Poll Nine Times, Which Radio 1 DJ Was The Only Man To Win The Award",Noel Edmonds + History & Holidays,For what is the eighteenth and nineteenth century sailor Sir Francis Beaufort best remembered? ,His System Of Wind Force Indicators ,General,Who played arthur fonzarelli in 'happy days',Henry winkler,General,"In The World Of Music How Is ""Evangelos Odysseus Papathanassiou"" More Commonly Known",Vangelis +General,In which Australian state or territory is the Kimberley Plateau and Eighty Mile Beach,Western australia,General,"If a bear lives in the northern hemisphere, the opening of the cave in which he hibernates is always on which slope",North,General,What do british tabloids often refer to as buck house,Buckingham palace +General,Which nation calls its parliament 'The Knesset'?,Israel,General,From which plant does the drug belladonna come from,Deadly nightshade,General,Mickey mouse has some nephews what were there names,Mortie and ferdie +General,90% of all thoroughbreds are descended from what horse,Eclipse,General,Who is the mesopotamian god of vegetation,Dagon,General,Poem or song narrating popular story,Ballad +General,Alan Stuart Konigsberg famous as who,Woody Allen,Sports & Leisure,Who Did Mike Tyson Defeat to gain his first HeavyWeight Title ,Trevor BerBick ,General,If You Suffered From “ Pediophobia ” What Type Of Object Do You Really Dislike?,Dolls +Sports & Leisure,How many points does a player score for a goal in hurling? ,3 Points ,Geography,"Reykjavik, _______________ is likely the cleanest capital city in the world.",Iceland,General,What color is produced by the complete absorption of light rays,Black +Entertainment,"Tinky winky, Dipsy LaLa and Po are know as what?",The Teletubbies,General,67% of the worlds population have never done what,Make a phone call,General,"Who said Hanover, Indiana was the ""dufus capital of the world""",Larry bird +General,What kind of food is Cullan Skink,Fish,General,What museum is home to half the paintings now attributed to Leondardo,The louvre louvre,General,What is the back boundry line in tennis called,Baseline +Geography,In Which Ocean Is The Gulf Stream ,The Atlantic ,General,Where can you buy a copy of Penguin News,Falkland Islands,Music,Which Groups Name Has The Initials RFTC,Rocket From The Crypt +General,"In religious institutions, a courtyard with covered walks. ",Cloister,Music,What Is The Term Which Defines Unaccompanied Singing,A Cappella,Music,What Was A No.1 Hit In The 70's For Tina Charles,I Love to Love (My Baby Just Loves To Dance) +Music,"Which 1990's Star Appeared As A Child On The Sleeve To The Grateful Deads 1969 ""Aoxmoxoa"" Album",Courtney Love,General,Walt Disney's family dog was named _________. She was a poodle. ,Lady,General,Which cheese is traditionally put on pizza,Mozzarella +General,"Concertino=short concerto, concertina=simple type of this instrument",Accordian,Science & Nature," When a __________ is first born, it is male, and it gradually evolves to female as it matures.",Shrimp prawn, History & Holidays,In which country do people who want to travel take a suitcase and carry it around the house on New Year's Eve? ,Venezuela  +General,In 18th century England what was known as Old Tom,Gin,Food & Drink,The Aperitif Made From White Aligote Wine And Cassis Is Known As What ,Kir ,Music,Which Teen Boy Band Did Johnny Wright Manage Before The Backstreet Boys,New Kids On The Block +General,Nudophobia is the fear of,Nudity,General,According to doctors people with what pets fall asleep easiest,Fish,General,What Boston Celtic's player think that Woody's hometown is full of idiots,Larry bird +General,What was the nationality of Franz Liszt,Hungarian,General,Most lipsticks contain what unexpected item,Fish Scales,General,Kopophobia is the fear of,Fatigue +General,What does bette davis' headstone say,She did it the hard way,General,"Who played victoria barkley on the tv show, ""the big valley""?",Barbara stanwyck, Geography,In which country is Tobruk?,Libya +General,What capital city is found near the headwaters of the Bosna River,Sarajevo, History & Holidays,How many astronauts manned each project mercury flight ,One ,Science & Nature,What Fundamental Part Of Computer Technology Was Pantented In The Us In 1961 ,Silicon Chip  + Geography,What is the basic unit of currency for Qatar ?,Riyal,General,Brothers A terrapin is a type of _________.,Turtle,General,Mary Pickford Charlie Chaplin and who founded United Artists,D W Griffith +General,Which Pop Duo Before Hitting The Big Time Were Formally Kno wn As Executive,Wham,Science & Nature, Most varieties of __________ can go an entire year without eating a single morsel of food.,Snake,General,What is the name for meat killed in the prescribed Muslim manner,Halal +General,Who founded the academy in athens,Plato,Food & Drink,In which US state did chilli con carne originate? ,Texas ,Sports & Leisure,In Tennis What Have You Achieved When You Score A Point Directly From A Serve? ,An Ace  +General,"The oldest tree in Britain is a yew in Scotland, how old is it","1,500 years",General,Glen Morris was the last Olympic gold medallist to do what 1938,Act Tarzan in Tarzans Revenge 1938,General,What stage of a meal would you eat charlotte,Dessert +Geography,Grasshopper Glacier in ___________ was named for the grasshoppers that can still be seen frozen in the ice.,Montana, History & Holidays,"Who were Balthazar, Melchior and Caspar? ",The Three Wise Men ,Science & Nature,"What Type Of Insects Are Hawkers, Clubtails, Biddies, Emeralds, Darts & Skimmers ",Dragon Flies  +Music,"Was ""Glory Of Love"" A Hit For Peter Cetera Or Kenny Loggins",Peter Cetera,General,Which French book illustrator of the middle 19th Century became widely known for his illustrations of such books as Dante's Inferno and Don Quixote,Gustav dore,General,Which spice is used in a whisky sling,Nutmeg +General,Angus Drummie Zeb Gaye is a member of which group,Aswad,Music,"When Did The Prodigy Release ""Fire""",1992,General,Name the port at the mouth of the River Seine,Le havre +General,Which island lies to the west of australia,Mauritius,General,What kind of creatures are the canary islands named for,Dogs,General,"What group sang ""Come On Eileen""?",Dexy's Midnight Runners +General,As who is the Frankish ruler Charles the Great better known,Charlemagne,General,What statuette is awarded annually for the best television commercial?,Clio,General,Magnifera Indica is the Latin name of what fruit,Mango +General,"In the 1998 film ""Titanic"", who played the part of Captain Smith",Bernard hill,General,What famous mountain is often photographed by film of the same name,Fuji,General,"Who wrote the classic war novel ""All Quiet on the Western Front""",Erich maria remarque + History & Holidays,Who imported the first Go set into Britain ?,Marco Polo,Food & Drink,In which month does beaujolais nouveau arrive? ,November ,General,Albany is the capital of _____,New York +General,"Only 2 Songs By The Beatles Spent Over 7 Weeks At No.1 ""From Me To You"" Was One Name The Other","Hello, Goodbye",Sports & Leisure,Who Moved Up To The No.1 Ranking For Snookers 1998-1999 Season ,John Higgins ,General,"What outlaws last words were supposed to be "" such is life """,Ned Kelly +General,According to historians what is the oldest device still used,Toothpicks,General,Which Famous Sporting Event Was Founded By Chris Brasher In 1981,The London Marathon,General,The former ancient region of Numidia lies mostly in which modern day country?,Algeria +General,Which US president twice served as an executioner,Grover Cleveland – duty as sheriff,General,What is made of fermented grape juice,Wine,General,"Which Country & Western singer is quoted as saying ""It takes an awful lot of money to look this cheap.""",Dolly parton + History & Holidays,Near what falls did Jimmy Angel crash his plane in 1937?,Angel Falls,General,What is histology,Study of tissues,Geography,In which state is the Kennedy Space Center,Florida +General,What colour bill does a greylag goose have?,Orange,General,For which company did Elvis Costello program computers,Elizabeth Arden,Sports & Leisure,Who was the first to win the grand slam of tennis?,Don Budge +Geography,In Which English City Will You Find The Angel Of The North ,Newcastle ,General,Who got Judas job as the twelfth apostle,Matthias,General,What fruit does not ripen after picking,Pineapple +General,Who once entered a Charlie Chaplin contest in Monte Carlo & placed third,Charlie chaplin,General,What is the sacred river of Hinduism?,Ganges,General, Ichthyology is the study of ________.,Fish +General,What is the longest word that can be typed using only the left hand,Stewardesses,General,The term Quincentennial represents how many years ?,500,General,In the Texas version 12 Days Xmas what is given on the 4th day,Four Prickly Pears + History & Holidays,What name was given to the Allied invasion of North Africa in 1942? ,Operation Torch ,Sports & Leisure,How many minutes is a major penalty in hockey,Five,Geography,"He visited Australia and New Zealand, then surveyed the Pacific Coast of North America.",Vancouver +General,What gland washes the eyes,Tear gland,Science & Nature,What Type Of Plague Was The Black Death ,Bubonic ,General,What film did Art Carney win the 1974 best actor Oscar for,Harry and Tonto +General,Caries refers to decay in what,Teeth,General,What did ed peterson invent,Egg mcmuffin,General,According to Playboy what is their Playmates greatest turn on,Music +General,Methacrylate resin is used to make what,Prosthetic eyes,General,Who built camelot,King arthur,General,"In 1986, what was the maximum fuel capacity (in litres) imposed in Formula 1 racing?",195 +Sports & Leisure,Which Famous TV show was introduced by the theme tune The Black and White Rag? ,Pot Black ,General,In the Christmas song your true love gave you give eight what,Maids a Milking,General,What is in apple pips,Cyanide +General,What is the capital of the U.S. state of Delaware,Dover,General,Where would you find your Zygomatic Arch,Cheekbone,General,Who was the Italian captain who skied a penalty in the 1994 world cup final's penalty shootout,Franco baresi +General,"In Greek mythology, who did melanion defeat in a footrace",Atlanta,General,What kind of creature is a 'Meadow Brown',Butterfly,General,Mans scarf worn inside an open necked shirt,Cravat +General,Who collaborated with john lennon on 'whatever gets you through the night',Elton john,General,Georges Claude invented what in 1911,Neon lights,Science & Nature,What is another term for a black leopard?,Panther +Music,What Is Micks Middle Name,Philip,General,There are 325 days in a christian year; the rest are called _____?,Lent, Geography,Which is the only land-locked country in South East Asia?,Laos +Music,"How Are ""Alan, Wayne Merrill, Jay & Donny"" More Commonly Known",The Osmonds,General,In which country was the yo-yo originally a weapon,Philippines,General,What is unique about the pistol star,Brightest in sky +General,"How many top ten digit hits came from Lionel Richie's LP ""Can't Slow Down""",Five,General,By Indonesian law what is the penalty for masturbation,Decapitation Which head? :o),General,Marathon runners have on average 14 a week - what,Alcoholic drinks +Food & Drink,"What was the first name of Senor Cardini, the Mexican restaurateur who created a classic salad in 1924? ",Caesar ,Science & Nature,Hepatitis affects the __________.,Liver,General,Dark skinned nomadic European,Gypsy +General,By what title was Mohandas K Gandhi known,Mahatma, Geography,Which element makes up 2.5% of the Earth's crust ?,Potassium,General,This large bean shaped lymph gland can expand & contract as needed,The spleen +General,International registration letters what country is ZR,Zaire,Art & Literature,"Whose Works The Ballad Of Reading Gaol & De Profundis, Were Written From His Experiences In Prison ",Oscar Wilde ,General,What are the 2 languages spoken in Malta,English and maltese + History & Holidays,What was the name of the character on the 1st Garbage Pail Kids Pack? ,Blasted Billy or Adam Bomb ,General,What two European states enjoy observer status at the United Nations,Switzerland & vatican city vatican city & switzerland,General,What caused the first Rednecks to be Redneck,Vitamin D Deficiency - poor diet +General,Who was king of Egypt from 1375 to 1358 b.c,Amenhotep iv,Geography,What Is The Capital Of New Zealand ,Wellington ,Music,"With Which Instrument Would You Associate Lionel Hampton, Red Norvo & Gary Burton",Vibraphone +General,Where is the statue 'le petit pissoir',Brussels,General,What causes an Iatrogenic illness,Doctors or treatment,General,What is an animal stuffer,Taxidermist +General,What people were the first to use the rounded arch,Romans,General,From Which Language Does The World Commando's Originate,Afrikaans,General,Where is the worlds largest gay festival held annually,Sydney gay Mardi gras Australia +General,Diane Leather was the first woman to do what,Sub 5 minute mile,Food & Drink,What is the other name for a ground nut or a peanut? ,Monkey Nut ,General,What mythical beast is a cross between a lion and an eagle,Griffin + History & Holidays,Which 1976 Film Is About A Persecuted Schoolgirl With Psychokinetic Powers ,Carrie ,Music,Dominion & Lucretia My Reflection Are Tracks From Whom,Sisters Of Mercy,Entertainment,Secret Identities: Wally West,The flash +General,What Monty Python movie was banned in Scotland,Life of brian,General,Ranidaphobia is the fear of,Frogs,General,"Which group had a British top five hit in the 1960s with Monday, Monday",Mamas and the papas +General,What is the top holiday in the US for candy / sweet sales,Halloween,General,What is a group of this animal called: Fly,Swarm,Science & Nature,What is the most reactive element?,Fluorine + History & Holidays,Which witch did Ozzy Osborne sing about in 1980 ,Aleister Crowley ,General,Who was the first Marvel Comics superhero,Human Torch,Science & Nature,What small region at end of medulla oblongata serves as 'bridge' to brain?,Pons +General,Who wrote the psalms,David,General,Brass is an alloy of copper and what,Zinc,General,Encephalitis affects the ________,Brain +General,William Moulton Marston Lie Detector and what comic character,Wonderwoman,Food & Drink,Fred The Flour Grader Was The Trademark Of Which Company? ,Home Pride ,General,White ribbed red cabbage is named from Italian for Chicory,Radicchio +General,"Who wrote the novel ""Slaughterhouse Five""",Kurt vonnegut jr,Science & Nature,Other Than Mercury What Other Planet Has No Moons ,Venus ,Music,"Who Had A Hit In 1990 & 1994 With ""Tell Me There Is A Heaven""",Chris Rea +Science & Nature,Which Rodent Has Given It's Name To A Mean Spirited Or Bad Tempered Woman ,Shrew ,General,In Which Film Was The Bill Haley Classic “ Rock Around The Clock ” First Heard ?,Blackboard Jungle,Music,Name Two Of The Three Members Of The Police,"Sting, Andy Summers, Stewart Copland" +General,Where is the dirtiest skin on your body,The face,General,What is the Capital of: Slovakia,Bratislava,General,In mythology Romulus Remus suckled by a shewolf fed by what,Woodpecker +Entertainment,"What famous animal character called ""Skull Island"" home?",King Kong,General,Which Icon Of The 1960's Used To Own A Nightclub Called Slack Alices,George Best,General,What organ will most often suffer permanent damage if you have amoebic dysentery,The liver liver +General,What creature in nature is most sensitive to heat,Rattlesnake organs between eyes,General,Jane Peters became famous as who - ( Clark Gables wife ),Carol Lombard,General,Which european peak was first conquered in 1865 by english mountaineer edward whymper,The matterhorn +General,American Hamilton Smith invented what in 1858,A washing machine,General,Capital cities: Tonga,Nuku'alofa,General,What is an ophthalmologist,Eye doctor +General,"What's the ancient greek word for ""great city""",Megalopolis,General,In Britain in 1746 what type of clothing was made illegal,Highland Dress – kilt plaid etc,General,"On which planet, other than earth, did a manmade object first land",Mars +General,What can readers learn about in Equus magazine,Horses,General,What is a marsupium,Pouch,Music,Who Had Hits With “Kiss From A Rose” & “Crazy”,Seal +General,What is a group of finches called,Trimming,Sports & Leisure,How many pockets on a standard snooker table?,6,General,Who was the original killer in Friday the 13th?,Jason's mother +General,Britain's live cable TV used to show what game topless,Darts - filmed in Australia,Music,"Which Lancastrian Town Features In The Beatles ""A Day In The Life""",Blackburn, History & Holidays,The following is a line from which 1970's film 'you're gonna need a bigger boat' ? ,Jaws  +General,"Countries of the world: north eastern coast of central America, the capital is Belmopan",Belize,General,"In music, the art of extemporization or creating all or part of a composition at the moment of performance",Improvisation,General,Rodgers who discovered the grand canyon,Francisco coronado + Geography,What country has a birth rate of 0?,The Vatican City,Science & Nature,What Is Acetylsalicycil Acid Better Known As ,Aspirin ,General,If you were given a French Gobelin what would you have had,A Tapestry +General,Which Mediterranean countries orchestra is bigger than its army,Monaco,General,What Hollywood star was the inspiration for Bugs Bunny,Clark Gable,Sports & Leisure,"Peter Simple, Jack Horner & Ben Nevis have all won which famous sporting event? ",The Grand National  +Tech & Video Games,What is the development team at Nintendo headed by Shigeru Miyamotocalled? ,EAD,Toys & Games,How many balls are used in a game of snooker in addition to the cue ball,21,General,Which pop group were named after the inventor of the seed drill,Jethro Tull +Sports & Leisure,Which City Was The First Ever To Host The Olympics Twice ,Paris ,General,What is the chemical symbol for plutonium,Pu,General,Whose autobiography is Parcel Arrived Safely: Tied With String,Micheal crawford +General,What Was The Vey First London Underground Tube Station To Be Built,Baker Street,Science & Nature,"A ""sirocco"" refers to a type of __________.",Wind,General,Seasoned smoked sausage,Frankfurter +General,What eats 14 feet of earthworms every day,Baby robins,Religion & Mythology,"In Greek mythology, who was the only mortal gorgon?",Medusa,General,Who is the smallest member of the European union,Luxembourg +General,Who won an Academy Award in 1968 as Director of Midnight Cowboy,John schlesinger,General,A woman with dark hair,Brunette,General,What planet is nearest the sun,Mercury +General,What first name has been used by most presidents,James,General,When did shires have reeves,Feudal times,General,Captain Hanson Gregory Crockett created what void in 1847,Hole in Doughnuts +General,What is the Capital of: Portugal,Lisbon,Music,Berry Gordy Was The Founder Of Which Incredibly Famous Music Label,Motown,General,Who did James Bond marry - character - (both names),Theresa Draco +General,In which constellation are the Seven Sisters,Taurus,Music,Which American Band Took Their Name From A Lesbian Sex Position?,Scissor Sisters,General,Which cartoon character lives in Sweetwater,Popeye's home port +General,"In the film The Great Escape , what were 'Tom', 'Dick' and 'Harry'",Three escape tunnels,Food & Drink,"Once very popular in Europe, which leafy weed was later called pig weed because it was said to be only suitable for pigs and Frenchmen ? ","Portulaca, Purslane or Pusley ",General,Until 1819 technically you could be hung for what in Britain,Cutting down a tree + History & Holidays,"After Henry VIII, Who Was The Next Member Of British Royalty To Get Divorced? ",Princess Margaret ,Technology & Video Games,"Name all of the main characters from each Final Fantasy game, in order (Final Fantasy 2 and 4-10 only). ","Frionel,Cecil,Butz,Terra,Cloud,Squall,Zidane,and Tidus",General,In Heraldry what is a canton,A Corner +General,What is the small irregular white cloud that zips around neptune approximately every 16 hours,Scooter,General,What was Black Beauties original name,Darkie,Science & Nature,What is a group of geese called?,Gaggle +General,What well known drug comes from the yellow cinchona plant,Quinine,General,Where is the centennial safe,Washington dc, History & Holidays,Which Fictional Radio Character Worked For Radio Norwich ,Alan Partidge  +Entertainment,How many flats are in the key of B flat major?,Two,General,Who was the featured vocalist of the miracles,Smokey robinson,General,"Cod French philosopher, scientist, and mathematician, sometimes called the father of modern philosophy",Descartes +Science & Nature,What phenomenon is caused by the gravitational attraction of the moon,Tides,General,In Minnesota woman can get 30 days for impersonating who,Santa Claus,General,"In 1513, Ponce de Leon discovered what is now the state of",Florida +Music,"Name The Label That Links Joy Division, Durutti Column, OMD, A Certain Ratio, New Order & Happy Mondays",Factory,General,"Countries of the world:western Asia, the capital is Damascus",Syria, History & Holidays,"""What town is the setting for the perennial Christmas classic 'It's a Wonderful Life'? (""""Bailey Park, Beford Falls, Sleepy Hollow, Pottersville"""") "" ",Bedford Falls  +Food & Drink, What colour is creme de menthe?,Green,General,What was the title of Jung Chang's account of growing up in China,Wild swans,Music,Which 1986 No.1 Hit For A Group Was A Cover Of A 1970 No.1 Hit For A Solo Artist,Spirit In The Sky +Music,Which Paul Simon Album Won A Grammy For Album Of The Year In 1987,Graceland,General,Who painted the Water Lilly Pond in 1899 (both names),Claude Monet,General,What talk show host did fred foy announce for,Dick cavett +General,What is a heart attack,Myocardial infarct,General,In which country is the port of Frey Bentos,Uruguay,General,A complex alcohol constituent of all animal fats and oils,Cholesterol +Music,What Was Des O Connor's Only No.1 Single,I Pretend,Science & Nature,"Butterflies & Moths Are In The Insect Group Lepidoptera, Which Group Contains Ants Bees & Wasps ",Hymenoptera ,General,Which actress sang the theme tune to the British sitcom 'A Fine Romance' ,Judi Dench  +Religion & Mythology,Who is the greek equivalent of the roman god Minerva ?,Athena,General,What is a group of hawks,Cast,General,Polyphemus was the leader of which group of mythical giants,Cyclops +Entertainment,"When danger appeared, Quick Draw McGraw became which super hero?",El KaBong,Geography,Lombard Street is London's equivalent of New York's ______________,Wall street,General,The Sydney Olympic torch showed a boomerang and what else,The Opera house +General,What are the devils bones,Dice,Music,Pickin The Blues By Grinderswitch Was Used By Which Radio 1 Disc Jockey As His Theme Tune In The 1970's,John Peel, Geography,Which country administers Martinique?,France +General,Which international alliance was set up in Vienna in 1960 to control the production and pricing of a specific commodity,O p e c,General,In the Sikh religion what is kesh,Uncut hair or beard,General,"The analysis of blood splatter patterns, insect specimens taken from a crime victim, and ballistic information are all part of a larger crime-solving science known as:",Forensics +Science & Nature,What is -459.7 F also know as?,Absolute Zero,General,"Who played John Candy's obnoxious brother-in-law in ""The Great Outdoors""",Dan Akyroyd,General,"Toothpaste Was Famously The First Advert On ITV, What Was The Second ?",Drinking Chocolate +Food & Drink,What Are Scallions ,Spring Onions Or Shallots ,Sports & Leisure,In The Game Of Darts How Much Would You Have Scored If You Achieved A 'Tic Tac Toe'' ,Darts (180) ,General,Scopophobia is the fear of,Being stared at +Science & Nature,The energy which a body possesses by virtue of its motion is called ________.,Kinetic,General,Des moines is the capital of ______,Iowa,General,"William Powell and which actress played the sleuthing couple in the ""Thin Man' series of films 1934 and 1947",Myrna loy +Music,"Name The Year: ""Wuthering Heights"", Grease (Movie), Keith Moon Dies Of An Overdose",1978,General,What is a group of flamingoes,Stand,Science & Nature,What Do You Call A Group Of Kangeroos? ,A Troop  +Science & Nature, The blubber of a male __________ is considered superior to that of the sperm whale for lubricating machinery.,Elephant seal,Sports & Leisure,The Japanese martial art of fencing is called ________.,Kendo, History & Holidays,What battle of the war of 1812 wa fought after the treaty of ghent was signed ,The battle of new orleans  +General,Which Sanskrit phrase means love story,Karma Sutra,General,How many cards are there in a tarot pack?,78,Science & Nature,Your ____ holds your head to your shoulders.,Neck +Music,Reaching No.13 In The Charts What Did Secret Affair Advocate 1979 As,Time For Action,General,What is Erse,Irish Gaelic language,General,What is conway twitty's real name,Harold lloyd jenkins +General,German kids wear what round neck for good luck on New Year,Pretzels,General,Which writer created the detective Lord Peter Wimsey,Dorethy L Sayers,General,"Which opera, composed by Saint-Saens, and first performed in 1877, is set in Palestine",Samson and delilah +General,"In which film, starring James Cagney, with Pat O'Brien as Father Connolly did he play a character called Rocky Sullivan",Angels with dirty faces,General,In which town does the famous 'running of the bulls' take place?,Pamplona,Geography,Which Country Has The Most Donkeys In The World ,China  +General,What ailment kills the most fruit flies,Constipation,General,Apart from a brand name what is a Reebok,An Antelope,General,What unit of length is equal to ten raised to the negative ten meter,Angstrom +Food & Drink,Who released the following 'edible' album 'Burnt weeny sandwich' ,Frank Zappa , History & Holidays,Which Small Car Did Austin And Morris Launch In 1959? ,Mini , History & Holidays,Who Was The 3rd Wife Of Henry Viii ,Jane Seymour  +General,Who Has An In-House Magazine Called Aerial?,The BBC, Language,What does 'shogun' mean in English?,Military governor,General,Sheet or ring of rubber to seal joint between metal surfaces,Gasket +General,"Who said ""Losing my virginity was a career move""",Madonna,General,"Which historian wrote ""The Lays of Ancient Rome""",Macaulay,General,Who was the author of the anthropological work 'Coming of Age in Samoa',Margaret Mead +General,There are more than 1500 varieties of what food,Rice,General,On Quaker Oats what word is written on the scroll on the box,Pure,General,"93 percent of U.S. homes have at least one, & the Thomas Nelson Company sells 8 million of them a year. What are they",Bibles +General,When introduced they were pockets for men only - what were,Handbags,General,Tasseomancy is fortune telling using what,Tea Leaves,General,What is the name of the river on the Isle of Wight which virtually cuts it in half?,Medina + History & Holidays,What band got their name from the sixties movie Barbarella? ,Duran Duran ,Music,"""She"" Was A Hit For Who In The Mid 70's",Charles Aznavour, History & Holidays,Which Footballer Was Sent home From The 1978 Football World Cup For Taking Illegal Substances ,Willie Johnston  +General,If You were drinking Rolling Rock lager what US state are you in,Pennsylvania,Geography,What is the official language of Egypt,Arabic,General,What chief justice headed the commission that declared 'lee harvey oswald___acted alone'¿,Earl warren +Science & Nature,What does the 'c' in the equation e=mc^2 stand for?,Speed of light,General,The Stutz_Bearcat is this type of car.,Sports,Geography,Which Country Boasts The Most Goats In The World ,India  +Science & Nature, Goldfish have four color recepectors in their __________ compared to our three _ the mantis shrimp has ten color receptors.,Eyes,General,Which Film Studio Got Its Name From The First Name Of The Parents Of It's 2 Founders,"MIRAMAX, (Mirianda & Maxwell)",General,Who wrote the opera 'the masked ball',Guiseppe verdi +Sports & Leisure,In Bowling What Is The Term For Knocking Down All Ten Pins With 2 Consecutive Balls ,A Spare ,Science & Nature,What is a group of peacocks called,Muster,Science & Nature, The white elephant is the sacred animal of __________,Thailand +General,Which TV show portrayed the lives of performing arts high school students,Fame, Geography,What is the capital of France ?,Paris, Geography,What is the basic unit of currency for Russia ?,Rouble + Geography,What is the basic unit of currency for Nauru ?,Dollar,General,Who coined the theory that the earth revolves around the sun,Nicolaus,General,Theodore Roosevelt received the 1906 Nobel Peace prize for his work to end which conflict?,Ruso-Japanese War +General,Name the legendary Hollywood cowboy who was born as Leonard Slye in 1912,Roy rogers,General,In Dallas Texas it is illegal to possess a realistic what,Dildo,Music,Who Was The Oldest Member Of The Beatles,Ringo Starr +General,How many stars are there on Brazil's flag?,23,General,To what do the tendons attach the muscles,Bones or cartilage,General,Who was the first newspaper owner to give staff a paid holiday,Joseph Pulitzer +General,General sherman burned this city in 1864,Atlanta,General,Demetria Gene Guynes became famous as who,Demi Moore,General,"American mathematician & founder of cybernetics, the study of control & communication in machines, animals, & organizations",Wiener +General,Dick Brams And Advertising Consultant From Missouri Is Responsible For Marketing Which Famous Edible Item,The Happy Meal,General,What peace treaty ended WWI,Treaty of versailles,General,Who were Curier Ellis and Acton Bell,Bronte sisters – pen names +Music,"Which US Songwriting Team Produced The Hits ""Searchin, Yakety Yak, Charlie Brown, & Poison Ivy"" For The Coasters",Jerry Leiber & Mike Stoller,General,Who created 'the saint',Leslie charteris,General,Rich Duncan Robert Whittle Thomas Suchanek champs sport,Hang Gliding +General,Who wrote the poem Anthem for Doomed Youth,Wilfred owen,General,Collective nouns - what's a group of donkeys,A Herd,General,What is the worlds longest insect,Borneo stick insect +General,Devon is the only county in Great Britain to have two ______,Coasts,General,Which author introduced the phrase 'the Beat Generation' in his novel entitled On The Road,Jack kerouac,Geography,What is the capital of Bulgaria,Sofia +Sports & Leisure,"Who Scored AS Record 11,174 Cricket Test Runs For Australia ",Alan Border ,General,Who is olive oyl's boyfriend,Popeye,General,Which heraldic term means flying,Volant +General,Who entertained the colonists by doing cartwheels in the nude,Pocahontas,General,In the rhyme who married The Owl and the Pussycat,The Turkey,General,If you saw a hummock off your port bow what are you looking at,Ice broken from berg +General,Which Beatles song did The Overlanders take to number one,Michelle,General,Who played Dwight D. Eisenhower in the mini series IKE,Robert duvall,Music,"Only 1 Artist Had 2 Records Named By ""Melody Maker"" As Album Of The Year During The 1970's Who Was He",David Bowie +People & Places,Which Actress Was Sued For Not Acting In Boxing Helena ,Kim Bassinger ,General,"Which Politician Did Neil Kinnock Describe As ""A Dither, A Dodger, A Ducker And A Weaver""",John Major,General,Who is the voice of 'phil' in the film 'hercules',Danny devito +General,What is phonetic alphabet word for U,Uniform,General,In what Australian state would you find Orange,New south wales nsw,General,Who said A computer will never need more than 640k of memory in 1982?,Bill Gates +General,Well known phrase Mad as a Hatter - but what made them mad,Mercury Poisoning,General,BSE was identified in Britain in which year,1986,General,Which world-famous concert pianist became President of Poland in 1940,Paderewski +General,"Which John Wayne Western was described by a critic as, 'Grand Hotel on Wheels'",Stagecoach,Food & Drink,What does it mean about the taste if a wine is described as 'brut'? ,Very Dry ,General,"In the book '1984', who is watching",Big brother +General,The great warrior _______________ died in bed while having sex,Ghenghis khan,General,"The current line-up of which POP group includes John McVie, Christine McVie, Stevie Nicks and Lindsay Buckingham",Fleetwood mac,Sports & Leisure,Which Team Joined Formula 1 Racing In 1991? ,Jordan  +General,In which film is danny devito the voice of 'phil',Hercules,General,What is the metal part of a lamp surrounding the bulb and supporting the shade called?,Harp,General,In The film Reservoir Dogs what song was discussed at the start,Like a Virgin – Madonna +General,Who conquered the matterhorn in 1865,Edward whymper,General,"What is the name of the central tower of a castle, the innermost and strongest part",Keep,General,Ishtar is the babylonian goddesss of ______,Love and fertility +General,Who founded the salvation army,William booth, Geography,What is the largest natural lake found in Africa?,Lake Victoria,General,What is the name of Superman's Supercat,Streaky + History & Holidays,At which battle did Nelson famously (turn a blind eye) to orders to disengage? ,The Battle of Copenhagen ,General,Woollen covering for head and neck,Balaclava helmet,Music,Under What Name Did Freddie Mercury Release His Early Singles Under,Larry Lurex + History & Holidays,"In The Ukraine what does it mean if find a spiders web In your house Christmas morning is it, good luck , bad luck, winter will be unusually cold ",Good Luck ,General,Gnu is the three letter term for which animal,Wildebeest,General,Kenny g is the best-selling ______,Saxophonist +Entertainment,An alien creature in a funny hat has opposed both Bugs Bunny and Daffy Duck. Where is he from?,Mars,Entertainment,Who is Scooby Doo's son,Scrappy doo,Mathematics,How many corners are there in a cube?,Eight +General,What number is on the opposite side of the 'five' on dice,Two,General,What is the national dish of the Faeroe Islands,Puffin stuffed with Rhubarb,Music,How Many Semi Quavers Are There In A Crotchet,4 +Entertainment,Forrest ____ liked shrimp.,Gump,General,"Who said ""Come up and see me sometime""?",Mae West,General,What is an 'octothorpe',Pound or number symbol +General,"Largest island of the continental United States, southeastern New York",Long,General,On which other animals does the pill work,Gorillas,General,What is the term for a fine mist or fog which is dispersed in and carried by a gas,Aerosol +Sports & Leisure,Who Was The First Boxer To Defeat Frank Bruno In A Professional Fight? ,James Bonecrusher Smith ,Music,"Who Had Chart Success With ""Set You Free"" In The Mid 90's",N Trance,General,Rod Taylor starred in a 1960 version of which HG Wells story,The Time Machine + Geography,What is the basic unit of currency for Sudan ?,Dinar,General,"What is the meaning of the title of Adolf Hitler's book ""Mein Kampf'",My struggle,General,What sport features a railroad split,Bowling +General,"Branch of mathematics concerned with the study of such concepts as the rate of change of one variable quantity with respect to another, the slope of a curve at a prescribed point, and the computation of the maximum and minimum values of functions",Calculus,General,In Which Sport Will You Find The Corridoor Of Uncertainty,Cricket,Sports & Leisure,Which Football Clubs Motto 'Superbia In Proelio'' Translates Into English As Pride In Battle ,Manchester City  +Sports & Leisure,How many points win a game in badminton? ,15 ,Music,Which musician took Vivaldi's The Four Seasons into the charts?,Nigel Kennedy, History & Holidays,"In 1902 this volcano erupted, killing 30,000.",Pelee +General,Who won the world soccer championship in 1958,Brazil,General,Underground cemetry,Catacomb,General,"In 1965 this group had formed as the versatiles, but changed their name at the request of johnny rivers who had just signed them to his soul city label",Fifth dimension +Music,From Which Film Did Duran Duran Take Their Name,Barbarella,General,The Straits of Malacca separate Malaysia from which country,Indonesia,Geography,Which capital city stands on the Potomac River? ,Washington D.C.  +General,What was invented by Garnet Carter of Chattanooga in 1926,Miniature Golf,General,Where was the colossus of rhodes,Rhodes harbour,General,"Who Was The Fist Person Ever To Be Handed The ""This Is You're Life"" Red Book By Eamon Andrews",Ray Reardon +Art & Literature,Who Wrote The Famous Book 'A Brief History Of Time'' In 1988? ,Stephen Hawking ,People & Places,Which aviator was Time magazine's first Man Of The Year in 1927? ,Charles Lindenbergh , History & Holidays,In what year was the first Christmas card produced? ,1846  +General,Which is the most abundant element in the universe,Hydrogen,General,In America they are called Mutual Funds how are they known here,Unit trusts,General,"Which character from Dickens' ""Great Expectations"" had been jilted on her wedding day",Miss havisham +General,What is the world's largest sea,Mediterranean,Geography,Name the capital city of massachusetts. ,Boston ,General,How many lanes are there in an olympic swimming pool,Eight +General,What is Alice Cooper's real name?,Vincent Furnier,General,Who gave a piano concert in Moscow in April 1986 having left Russia 61 years earlier,Vladimir horowitz,General,What is the surname of u.s president herbert c ______,Hoover +General,What award did washington create in 1782 as a decoration to recognize merit in enlisted men and non-commissioned officers,Purple heart,General,What is the fear of the color black known as,Melanophobia,General,"In which Charles Dickens' novel do the characters, Mrs Pardiggle and the Jarndyce family appear",Bleak house +General,Who was the 15th president of the U.S.,James buchanan,General,French novelist - nearly 100 books all La Comedie Humaine,Honor'e de Balzac,General,"In the original version of 'romeo and juliet', who played juliet",Olivia +Music,"Name The Geordie ""Hard Man"" And His No.1 From July 1992",Jimmy Nail / Aint No Doubt,General,Where are the Hausa and Ibo tribes?,Nigeria,General,"The well-known aria ""La Donna e Mobile"" features in which Verdi opera about a court jester",Rigoletto +General,In Mexico it is illegal for the police to sell what,Their Guns, History & Holidays,Which shop in Regent St is the largest toy store in the world ,Hamley's ,General,This russian scientist used dogs to study conditioned reflexes,Ivan pavlov +General,"Libya is the only country in the world with a solid, single-colored flag __ what color is it",Green,General,Who invented popsicles?,Frank Epperson,General,What kind of juice goes in a salty dog,Grapefruit +Science & Nature,"In 1447 Johannes Gutenberg Invented The Printing Press, What Was The First Book He Produced ",A Latin Edition Of The Bible ,General,What did Paceard and Balmat conquer in 1786,Mont Blanc,General,The liqueur crème de cassis is made from what,Blackcurrants +Sports & Leisure,Who Became The Oldest Boxer To Retain The Heavyweight Title At The Age Of 45 ,George Foreman ,Music,Who Was The First Western Band To Play A Concert Venue In China,Wham,General,"How would one say the month ""April"" in french",Avril +Entertainment,This was the first cartoon talking picture.,Steamboat Willie,Music,"Which 1961 Movie Directed By Brian Forbes & Starring Hayley Mills & Alan Bates, Provided The Inspiration For Andrew Lloyd Webbers Last Musical Of 20th Century",Whistle Down The Wind,General,"What word did Dan Quayle devote an entire chapter to, in his book of memoirs",Potato +General,The IAAF finally recognised women in which sport in 1995,Pole Vault,Science & Nature,"What Can Be African, Chinstrap, King Or Emperor? ",Penguins , History & Holidays,Which actress starred in the movie The Grudge? ,Sarah Michelle Gellar  +General,Bohea is a type of what,Tea,General,Collective nouns - A glint of what,Goldfish,General,What was Acadia,Nova Scotia (French Name) +General,"If a dish is served A la Chantilly, what would be its main ingredient",Whipped cream,General,What young animal is an offspring of a nanny & a billy,Kid,General,The Emperor Augustus banned his men wearing silk - why,It was Effeminate +General,"Who wrote the original story ""The Lost World""?",Sir Arthur Conan Doyle,General,What divides the American north from the south,Mason dixon line, History & Holidays,What colour was added to the French flag during the French revolution?,White +General,What continent is submerged,Atlantis,General,John de Lancie played which character in the Star Trek series,Q,General,Sacrofricosis is what sort of sexual behaviour,Pocket holes public masturbation +General,What relation was William the Conqueror to Stephen (1096-1154),Grandfather,Toys & Games,Tic-Tac-Toe is based on which game ?,Nine men morris,General,"If a robin's egg is put in vinegar for thirty days, what colour does it become",Yellow +General,"Who sent the brief message ""i came, i saw, i conquered""",Julius caesar,General,Where is the most sensitive cluster of nerves,Base of the spine,General,"What musical instrument has gourd, shoulder, nut and leaves",A Sitar +General,What is the fear of one thing known as,Monophobia,General,What river provided water to feed the Hanging Gardens of Babylon?,The Euphrates,General,Steven the 1st founded what country in 1000 ad,Hungary +Music,Which Rock Star is Nicknamed Slowhand?,Eric Clapton,Sports & Leisure,What Sport is Played On The Back Of a Ten Pound Note ,Cricket ,General,As what is Minnesota also known,Gopher state +General,Eileen Collins was the first _______ on a space shuttle mission.,Female captain,General,What was the first shoe brand name to go into Oxford dictionary,Dr Martins,General,"In which sporting activity are the manoeuvres Fliffus, Miller, Adolph and Barani executed",Trampolining +General,What ingredient must French ice cream contain by law,Eggs,General,What is an 'armsaye' in clothing,Armhole,General,Which of the seven wonders of the ancient world was alive,The hanging gardens of babylon +General,Which country was the first to legalise abortion,Iceland,General,What is the fear of virgins or young girls known as,Parthenophobia,General,What do nine pennies weigh,1 ounce +General,Caer-Lud was the former name of what capitol city,London,General,How many red stripes are there on the national flag of the U.S.A.,Seven,General,The name for which body organ translates as all flesh,Pancreas +General,In Massachusetts it's illegal to wear what without a licence,Goatee,Mathematics,What is the minimum number of integer degrees in a reflex angle?,181,Music,Name The Thrash Metal Kings Headed By Guitarist Dave Mustaine,Megadeath +Food & Drink,With what is champagne mixed to produce `Buck's Fizz'? ,Orange Juice ,General,For what was black the most common colour in the depression,Automobiles, History & Holidays,In what year was 'Dirty Dancing' released? ,1987  +General,Who comes on stage before conductor and tunes orchestra,Concert Master / Mistress 1st violin,General,Marley Who still receives an estimated 25 pieces of junk mail per year at Walden Pond,Thoreau,General,What's a person who does not eat meat called,Vegetarian + Geography,What is the capital of Tunisia ?,Tunis,General,Action Comics 720 after 58 years who returned engage ring,Lois Lane to Superman,Geography,Where is Westminster Abbey located,London +General,A person at his wit's end is said to be losing his what,Marbles,General,What's a funambulist,Tightrope walker,General,Who was the first director of Britain's National Theatre,Laurence olivier +General,In Wyoming in June it is illegal to take a picture of what,A Rabbit,General,What does a chronometer measure?,Time,Science & Nature,"The four Galilean moons of Jupiter are: Callisto, Io, Ganymede, and _________",Europa +Music,Who Sang The Theme Tune To The Bond Movie The Spy Who Loved Me?,Carly Simon, History & Holidays,Which Household Cleaning Product What Launched At The Savoy Hotel In 1960 And By The 1970's Was Being Used In 6 Out Of 10 UK Households ,Fairy Liquid ,Entertainment,"In ""Gone With the Wind"", Scarlett regains her wealth by investing in what type of business?",Sawmill +General,In AFTs top 100 movies only 2 sequels - Godfather and what,French Connection II, History & Holidays,Who Released The 70's Album Entitled Innervisions ,Stevie Wonder ,Music,"What Is The Method Of Playing A Note By Shortening It's Time Value, So That It Is Detached From Subsequent Notes",Staccato +Music,"""Brass In Pocket"" Was A Hit For Which Band",The Pretenders,Food & Drink,Which cake is traditionally eaten on 5th november to commemorate guy fawkes? ,Parkin ,Religion & Mythology,Who killed a dragon in ancient mythology?,St. George + History & Holidays,Who Invented The Christmas Cracker ,Thomas Smith ,General,Oakley what are large snow bumps known as in skiing terms,Moguls,General,What is a group of this animal called: Stork,Mustering +General,"Who, according to a song, damaged her foot on a piece of wood and fell into a raging torrent?",Clementine,Geography,Which Is The 2nd Largest Country In The World ,Canada ,General,Who coined the word sociology,Auguste comte +General,Who coined the term 'proletariat',Karl marx,General,"Name the female comedy star who once had a show on Fox, that had a pop hit in the early eighties.",Tracy Ullman,General,Like what can a fully ripened cranberry be dribbled,Basketball +General,Who played Andy Capp on television in 1988,James bolam,Sports & Leisure,Which Goal Keeper Was Beaten By Maradona's 'Hand Of God'' Goal? ,Peter Shilton ,Entertainment,What city's police force did Charlie Chan work with?,Honolulu +General,Who was the founder of the 'epicurean' philosophy,Epicurus,General,And what's the top annoyance among wives,Untidiness,General,Name only boxer to win a world title who never had a manager,Jake La Motta +General,In Shakespeare's Merchant of Venice name Shylocks wife,Leah,Music,What Were The First Names Of The 3 Original Members Of Bananarama,"Siobhan, Sara, Keren",General,Tanjong Panger Container Terminal where worlds largest con port,Singapore +General,Collective nouns - A group of beavers is what,Colony,General,What is the average lifespan of a tastebud,Ten days,General,The highest mountain in North America,Mount mckinley +General,"Even if up to 80% of this is removed from a human, what is it that will continue to function & grow back to its original size",Liver,General,Which Shakespeare play was originally entitled What You Will,Twelfth Night,General,A 'gam' is a collection of which creatures,Whales +General,What was the name of Joan Fontaine's actress sister with whom she had a notoriously bad relationship,Olivia de havilland,Art & Literature,The rendering of light and shade in painting; the subtle graduations and marked variations of light and shade for dramatic effect. ,Chiaroscuro,Art & Literature,"The technique of producing printed designs through various methods of incising on wood or metal blocks, which are then inked and printed. ",Etching +General,"What's the most valuable crop in burma, laos and thailand",Poppy,General,"If one angle in an isosceles triangle is 100 degrees, what is each of the other two",Forty degrees,Science & Nature,What name is used to describe permanently_frozen subsoil,Permafrost +General,"Which Football Club Play Their Home Games At ""The Home Depot Centre""",La Galaxy,General,The Andaman Islands are in which bay,The Bay of Bengal,General,What name is given to a breed of cat which results from cross-breeding between Siamese and Burmese cats?,Tonkinese Cats +Sports & Leisure,Which Woman Won The Wimbledon Singles Title For The Last Time In 1975? ,Billy Jean King ,General,What is the season before Christmas called,Advent,General,What does the angora cat enjoy,Swimming +General,Which country was the first to make seat belts compulsory,Czechoslovakia,General,What is the earth's outer layer of surface soil or crust,Lithosphere,Music,What Was Barry Whites Only no.1 Single In The 1970's,"You're The First , The Last My Everything" +General,Name The First Foreign Company To Open A Factory In The USA,Volkswagen,General,"Who in the 20th century is credited with building the first successful helicopter, which flew in 1939",Igor sikorsky,Geography,Of Which Country Is Rangoon The Capital ,Burma  +Religion & Mythology,What's heaven to fallen Norse warriors?,Valhalla,General,A horses height is measured from the ground to what part,Withers - base of neck crest line,General,What does uranus circle every 84 years,Sun +General,Who was the first non human to win an Oscar,Mickey mouse,Science & Nature,What type of animal lives in a formicary,Ants,General,What is Rapec,Type of snuff +General,What sport did andre agassi's dad compete in,Boxing,General,What's the maximum number of players in a game of Chinese chequers,Six,General,What do you call hair like or feathery clouds,Cirrus +General,Pax is the roman goddess of ______,Peace,Science & Nature," The world's largest mammal, the blue whale, weighs 50 tons at birth. Fully grown, it weighs as much as __________ tons",150,General,Male voice above normal range,Falsetto +General,What Number Is Written On The Back Of David Beckham's Shirt When He Played For Real Madrid ?,23,General,What marx brother had real name milton,Gummo, Geography,What is the capital of Maine?,Augusta +Science & Nature, The average elephant produces 50 pounds of __________ each day.,Dung shit,General,Army Greatcoat Horn of Plenty Diamond Kimono types of what,Napkin Folds,General,Which actor portrayed head of Tommy Lee Jones department in the film Men In Black,Rip torn +General,What animal does the adjective 'pardine' refer to,Leopard,Religion & Mythology,Who is the Norse god of poetry,Bragi,General,In the Old Testament whose name means Gods with us,Emanuel +Art & Literature,"In one of Donald Horne's novels, as what was Australia dubbed?",The lucky country,General,Name the first storm 19 June 1978 called after a man,Bud,General,What is the highest lake in the world,Lake titicaca +Music,"Which Solo Artist Sang Backing Vocals On The Dire Straits Hit ""Money For Nothing""",Sting,General,Rhoda was a spinoff from what other popular tv show,Mary tyler moore show,General,The average person does what 16 times a day,Swear +General,What bird makes an excellent watchdog,Goose,General,What do the EPPY awards honour,Electronic editions of newspapers,General,What was the license number on the Ghostbusters' car?,ECTO-1 + History & Holidays,What was the first product to have a barcode?,Wrigley's gum,General,U.S. captials Wyoming,Cheyenne,General,"Who said ""power is the ultimate aphrodisiac""",Henry kissinger +General,"Four thirds multiplied by pi multiplied by the radius cubed, gives you the volume of what geometric object",Sphere, Geography,What is the basic unit of currency for Angola ?,Kwanza,General,Who released the album Invincible,Michael Jackson +General,Sikorsky what jerry van dyke sitcom is widely hailed as the worst in tv history,My,General,What basketballer was dubbed the NBA's Identified Flying Object,Julius,General,Who first suggested the idea of Daylight Saving Time in an essay he wrote in 1784?,Benjamin Franklin +General,"What Nationality Was The The Musician & Composer ""James Last""",German,General,What is the study of heredity,Genetics, Geography,What river separates the city of Florence?,Arno +General,Who was the Phoenician Goddess of love,Astarte,Science & Nature,What Is The Coloured Part Of The Eye Better Known As ,The Iris ,General,"Football Team, chicago ______",Bears +Sports & Leisure,How Many Tournaments make up a Grand Slam In Golf ,4 ,General,In what sport are ten pieces of wood separated by a chain,Cricket,General,Roman God of love,Cupid +Science & Nature,Ascorbic acid is commonly reffered to as Vitamin - ?,C, Geography,What is the capital of Malaysia ?,Kuala Lumpur,General,In The Lion the Witch and the Wardrobe whets the Lions name,Assan +General,Whose brother wrote seven songs for the movie staying alive,Sylvester,General,Fanny Crosby wrote over 8000 of these - what,Hymns,General,What brand of footwear was invented by Adolf Dassler,Adidas +General,Doraphobia is the fear of _________,Fur,General,"What product is known for sponsoring the ""rotten sneaker contest",Odor eater,General,"A style that flourished in the seventeenth and early eighteenth centuries, characterized by exuberant decoration, curvaceous forms, and a grand scale generating a sense of movement; later developments within the movement show more restraint. ",Baroque +General,The Mason-Dixon line ran between Maryland and which other American State,Pennsylvan1a,General,What are religious ascetics who practise scourging or mutual whipping for bodily discipline or penance,Flagellants,General,The A1 is the longest trunk road in the UK between what 2 cities,London - Edinburgh +General,A beast of prey sometimes called a glutton - what is it,Wolverine, History & Holidays,She was the first First Lady to be received privately by the Pope.,Jackie kennedy,General,What was the first boxed cereal,Shredded wheat +General,In which African country would you find the towns of Kumasi and Cape,Ghana,General,"What's the international radio code word for the letter ""L""",Lima,General,What country calls themselves Magyar?,Hungary +General,The word Utopia from Greek means what,Nowhere,Music,"Who Had A Hit With The Song ""Celebration""",Kool & The Gang,General,What is the art of composing dances,Choreography +General,"In ballet, a step done off the ground.",En l'air,General,James l succeeded to the throne of England in which year,1603,General,Who are the only brothers to win the pga tournament?,Lionel & jay hebert +Entertainment,"When danger appeared, Quick Draw McGraw became which super hero",El kabong,General,What is the latin phrase meaning 'in the original arrangement',In situ,General,What US city hosted the final of the 1994 football world cup,Pasadena +Sports & Leisure,Which Cricketer Captained England In 23 Test Matches Between 1986 And 1988? ,Mike Gatting ,General,How many wives was Mormon leader Brigham Young said to have,27,General,What is the main ingredient of most shampoos,Water +Geography,Which city is known as Motown,Detroit,General,What is the only female animal that has antlers,Caribou,General,Who uses a signalling system known as tick tack,Bookmakers +General,Where do 'open-collar workers' work,Home,General,What famous person was born in Ahmaddnagar India,Spike Milligan,General,Necrophobia is a fear of ______,Dead things +General,Who had a 30 year love affair with Lillian Hellman,Dashiel hammett,General,"What's the international radio code word for the letter ""Q""",Quebec,General,The Chinese scattered firecrackers around the house - why,Fire Alarms +General,Assassin Magazine and Sofa come from which language,Arabic,General,Which film won the best special effects Oscar in 1961,The Guns of Naverone,General,"Where were 'foul lane', 'stinking lane' and 'bladder street'",London +Music,What Is The Best Selling Classical Album Of All Time,Essential Pavarotti,Music,"Who came up with the title for ""A Hard Day's Night""?",Ringo, History & Holidays,In Which Movie Did Jim Carey Play Lloyd Christmas ,Dumb & Dumber  +General,Short legged breed of Welsh dog,Corgi,General,What is the fear of death known as,Necrophobia,Music,Which Artist Went Straight Into The No.1 Slot In July 2007 With The Album “One Chance”?,Paul Potts +General,In the Rocky films what was the name of Rocky's wife,Adrian,General,In Saudi Arabia by law women may not become what,A Doctor,General,The rate of change of velocity is known as _________,Acceleration +General,When did richard burton finish his last film,1984,General,Ncaa: in what year was the heisman memorial trophy first awarded,1935,Geography,Bridgetown is the capital of Where ,Barbados  +General,What do you get by mixing gin and vermouth,Martini,General,"Besides being candidates for president in the 1996 U.S. elections, what do Bill Clinton, Ross Perot & Bob Dole have in common",All left handed,General,What is the Decalogue normally called?,Ten Commandments +General,What is a leech a type of,Worm,General,Good rhine wines are bottled in what colour bottles,Brown,General,Sport is played on a variable ground 50x100yd min 100x130 max,Association Football (soccer) +Science & Nature,What Vehicular Safety Device Was First Pantented By Ej Claghorn ,Seat Belt , Geography,Malabo is the capital of ______?,Equatorial Guinea,General,Eglantine Jebb founded which charitable organisation,Save the children fund +Music,"What Song Features The Lyric ""Giant Steps Are What You Take""",Walking On The Moon,General,Which great battle took place from July 1st to November 18th 1916,The Battle,General,What animal's shell is used to make jewellery,Abalone +General,What do the STP Corporation's initials stand for?,Scientifically Tested Products,General,How many Jews were saved on Schindler's List,1100,Sports & Leisure,Ignoring the Words 'Football'' And 'Club'' Which Is The Only Team In The 2006/2007 Premiership League Whose Name Contains All The Letters From The Phrase 'Red Card''? ,Manchester United  +General,If you had canitis what have you got,Grey hair,Sports & Leisure,What is the white ball called in bowls? ,The Jack ,General,Circuits can be wired in series or ______,Parallel +Music,John Elliot Was A Member Of Which Band,Def Leppard,General,Which sea route connects the North Atlantic with the Beaufort Sea and the Pacific Ocean,The northwest passage,Music,"Who Performed The Theme Song to The James Bond Film ""The Living Daylights""",Aha +Geography,Which Bridge In London Was Officially Named William Pitt Bridge In 1769 ,Blackfriars Bridge London ,Art & Literature,A group of American painters who united out of opposition to academic standards in the early twentieth century. ,The eight,General,Which animals kill 275 million creature in UK annually,Domestic cats +Science & Nature,With what body part is otology involved?,Ear,General,This animal is found at the beginning of an encyclopedia,Aardvark,General,"What three categories are rocks divided, according to their formation",Igneous sedimentary metamorphic +Science & Nature,For What Illness Did Louis Pasteur Develop A Cure? ,Rabies ,General,What is Nelson Mandela's middle name,Rolihlahla,General,What county has the largest army in the world,China +General,Anne Boleyn lost her head over this guy,Henry viii,General,Mancha who wrote 'don quixote',Cervantes,General,"What links - Sarte, Neitzsche, Russell and Decartes",Philosophers +General,Tchaikovsky died of which disease,Cholera,General,"In California 22 "" is the minimum legal length of saleable what",Halibut, Geography,What is the current name for south-west Africa?,Namibia +General,"Disgusting question - longest verified American one 12' 2"" what",Turd - over 2hrs 12 minutes,Science & Nature,What is the number of blue razor blades a given beam can puncture?,Gillette,General,What is a pyrotechnic display,Fireworks +General,Zoologist and writer Gerald Durrell spent part of his childhood on which Greek island,Corfu,Science & Nature,What is a skin specialist called?,Dermatologist,General,Who was the third king of israel,Solomon +General,What is the Latin word for poison,Virus,General,What is the fear of small things known as,Microphobia,General,Cecil B De Mille the film director had what middle name,Blount +Music,"What Connects The Lightning Seeds, Cilla black , Space",Liverpool,General,What do callipygian people have,Prettily shaped buttocks,General,Which city did Crocket and Tubbs spend most of their time in?,Miami Vice +General,What did the S stand for in Harry S Truman,Nothing,General,Mildrid Harris Lita Grey Paulette Goddard Oona O Neil - Link,Charlie Chaplains wives, History & Holidays,Who Released The 70's Album Entitled Deja Vu ,CSNY  +General,Cleopatra sometimes wore a fake what,Beard – for Official Duties,General,Bentlet Drummle appears in which Dickens novel,Great Expectations,General,Which feature of the night sky is also known as a 'shooting star',Meteor +Sports & Leisure,In Which Athletic Event Was Colin Jackson A Dominant Figure ,110m Hurdles ,General,And who was his original voice,Clarence Nash,General,What motto ends merrie melodies cartoons,That's all folks +General,What is the English title of Voyna i Mir,War and Peace,General,Benazir Bhutto regained power in 1993 after being ousted how many years before,Three,General,What us state includes the telephone area code 612,Minnesota +General,In which U.S. state was the world's first 'silicon valley',California,Science & Nature,Which chemical element is represented by the symbol Pa?,Protactinium,General,What English philosopher was one of the founders of utilitarianism,John bentham +Music,"Which Famous Pop Group Did Brothers Brian, Carl & Dennis Form In 1961 Joined By Cousin Mike & Friend Al",The Beach Boys,General,Capital cities: Ethiopia,Addis ababa,General,What u.s. vice-president said 'some newspapers dispose of their garbage by printing it',Spiro agnew +Science & Nature,He discovered the process of vaccination for prevention of smallpox.,Edward Jenner,General,In some areas of Paris what is provided for dogs,Private flush toilets,General,What was classical composer Mussorgsky's first name,Modest +Music,Which Group Features Tommy Lee As Drummer,Motley Crue,General,11 am In military time is how many hours,1100,General,Embankment built to prevent flooding,Dyke +People & Places,Which Famous Tv Presenters Are Former MP's ,"Brian Walden, Robert Kilroy Silk ",Music,"Who had his first UK top 10 single in ten years in 2004 with Irish Blood, English Heart?",Morrissey,General,Kitty the most common cats name in US what's the second,Smokey + History & Holidays,In what year was Band-Aid's Do They Know It's Christmas the UK Christmas chart-topping record? ,1984 ,General,Bartolomeo Diaz called it the Cape of Storms. What is it called now,The cape of good hope,General,Numerophobia is a fear of ______,Numbers +General,What was the name of Juliet's cousin killed by Benvolio in R+J,Tynbalt,Music,"He Was Killed When A Car Driven By His Girlfriend ""Gloria Jones"" Crashed Into A Tree On Barnes Common Name Him",Marc Bolan,General,Of what is dendrology the study,Trees + Geography,Which is the Earth's second smallest continent ?,Europe,Science & Nature,What Term Is Given To The Annual Marking Of Swans On The Thames? ,Swan Upping ,Science & Nature,What is the heaviest of the naturally occuring Noble gases?,Radon +Geography,In which state are the Finger Lakes,New york,General,Which actress's real name was Jane Peters,Carole lombard,Music,What Was The Name Of Robson & Jeromes 1996 Chart Topping Album,Take Two +General,What is the fear of clouds known as,Nephophobia,General,"Who would ""never forget a face but in your case i'll make an exception""",Groucho marx,Food & Drink,Where Exactly Were Cornflakes Invented ,Battle Creek Sanitarium  + History & Holidays,"He was the captain of the ""Mayflower"".",Standish,General,What country had three presidents - in the same day,Mexico,Geography,What is the capital of Trinidad and Tobago,Port_of_spain +General,What does brb mean,Be right back,General,What is a group of this animal called: Lark,Ascension exaultation,Sports & Leisure,"In Which Sport Would You Encounter A Bedpost, A Six Pack, A Blow & A Cherry ",Tenpin Bowling  +General,Ethernet is a registered trademark of what company,Xerox,Science & Nature,What Part Of The Brain Is Affected In A Lobotomy ,The Frontal Lobe ,General,What drug can be found in tonic water?,Quinine +Music,Which Band did Mick Talbot & Paul Weller Form In 1983,The Style Council,General,In which city is the oldest Zoo in the world still in use,London,General,"What is the last name of the family ""lost in space""",Robinson +Art & Literature,"Three main types of Greek columns are Doric, Ionic, and __________.",Corinthian,Music,Who Used His Wife's Compensation Money From A Car Crash To Take Up Music Full Time,Howard Jones,General,What is the state bird of Texas,Mockingbird +General,"Spear, apple and pepper are types of which plant",Mint,General,How many of the 150 psalms are attributed to moses,One - psalm 90,General,"Which Brand Common In A Variety Of Products Often Found In A Kitchen Was Invented By ""Eugene Sullivan & William Taylor""",Pyrex +Music,"Which Musician Was Once A Member Of Depeche Mode, Yazoo & Erasure",Vince Clark,General,What is an ecdysiast,Stripper,General,What is a small fertile area in a desert called,Oasis +General,A Spanish dance in ¾ time or 3/8 time with castanets.,Cachucha ,General,It's illegal in the USA for any citizens to have contact with who,Extraterrestrials or their vehicles,General,Where in the United States would you find the country's only royal palace,Hawaii +General,What was Bugs Bunnies original name,Happy Rabbit,General,"Gaur, Gayal, Banteng and Kouprey are types of what",Wild Cattle,Art & Literature,"In sculpture, the building up of form using a soft medium such as clay or wax, as distinguished from carving. In painting and drawing, using color and lighting variations to produce a three-dimensional effect. ",Modeling +General,80% of restaurant diners don’t do what,Eat dessert,General,9% of divorced people say they still do what,Have sex together,Technology & Video Games,"What number is the Pokémon ""Scyther"" in the Pokémon GameBoy games? ",123 +General,Ajax was the trade mark of the worlds first what,Flush lavatory,General,What is a group of this animal called: Peep,Litter,Science & Nature,What does the Binet test measure?,Intelligence +Music,What Was The First Top Twenty Hit In The Uk For Stevie Wonder,Uptight (Everythings Alright),General,This city is the capital of Australian Capital Territorry?,Canberra,General,What is a sleepwalker,Somnambulist +General,First division of the Paleozoic Era,Cambrian Period,General,Away we Go was the original 1942 title what musical show/film,Oaklahoma,General,What is a group of this animal called: Chicken,Brood peep +Sports & Leisure,Which Liverpudlian Won The WBC Light Heavyweight Title In 1974 ,John Conteh ,General,Terra Psittacorum was an early name for which country,Australia,General,Name the most famous English artist who painted mostly horses,George Stubbs +General,What car company made the first glass fibre racing car,Lotus,General,"What name is given to the traditional, charcoal heated, Russian tea urn",Samovar,Music,Aretha Franklin Was The First woman Inducted Into What,The Rock & Roll Hall Of Fame +Science & Nature,For What Substance Is K The Chemical Notation ,Potassium ,General,What is the proffesion of Jorge Amado,Author,People & Places,Who Made A Controversioal Speach Concerning 'Rivers Of Blood' ,Enoch Powell  +General,What is ix,9,General,Who received the Nobel Physics prize jointly with the Curies in 1913 for their work on radioactivity,Antoine becouerel,General,What musical term means playing with each note detached,Staccato +General,What did The Chief nickname Starsky's car on Stasky and Hutch??,The Striped Tomato,General,Which John Updike novel features three divorcees on the make who are seduced by the devil,The witches of eastwick,General,A wheel turns or spins on an _____,Axle +Tech & Video Games,Which was the first manned aircraft to exceed the speed of sound ?,Bell X-1,General,What is the common name of the plant Calluna,Heather erica ling,General,Mark Mishon ate 61 of these in 7 minutes at The Obelix Creperie.,Pancakes +General,"Anatomically speaking, what is the axilla better known as",Armpit,General,Collective nouns - what is a group of swans,Herd or Bevy,General,These animals were once used to bleed the sick,Leeches +Music,Who Released An Album In 1982 Called Simply,Status Quo,Music,How Many Tears Did The Goombay Dance Band Have,Seven, Geography,What is the basic unit of currency for Malaysia ?,Ringgit +General,What printing system utilizes tiny dots,Dot matrix,General,What's the currency in algeria,Dinar,General,An archangel cat has what colour coat,Blue +General,The Average American does what 22 times a day,Opens Fridge,General,What's the capital of Liberia,Monrovia,General,What was the name of the operatic diva who gave her name to a peach dessert,Dame nellie melba +General,Who made his first appearance as Sherlock Holmes in the 1939 film The Hound of the Baskervilles,Basil rathbone,General,How much playing time is there in a football game,60 minutes,Geography,"_____________ has the highest per capita income in the world, except for the oil sheikdoms.",Bermuda +General,What does the ureter carry,Urine,Geography,What is the capital of Alaska,Juneau,General,Who wrote Of Human Bondage and The Moon and Sixpence,Somerset Maugham +Music,Who Was Responsible For The Assassination Of John Lennon,Mark David Chapman,General,Which Hollywood legend won an Oscar for her starring role in Jezebel,Bette davis,Entertainment,Juliette Binoche won an academy award for best supporting role in which film?,English Patient +General,Collective nouns - a leash of what,Greyhounds,General,What kind of poisoning is known as plumbism,Lead,General,What's belgium's main port,Antwerp +General,"Who said ""To many of our imports are from abroad""",George Bush,General,Who won the Superbowl in 1987,New York Giants,Science & Nature,What features of the african elephant are larger that those of an indian elephant ,The ears  +General,If you suffered from pyrexia what have you got,Fever,General,A man has first at 18 then every day spent 106 days by 60 what,Shaving,General,In 1956 Irme Nagy lead a revolt in which country,Hungary against Russia - Stalin +General,Which Lombardy town is famed for its cheese,Gorgonzola, History & Holidays,"What did presidents Madison, Monroe, Polk, and Garfield have in common?","The first name ""James""",General,Chicago Transit Authority is now known as which group?,Chicago +General,Name Lois Lanes stewardess sister,Lucy Lane,General,What word can go before Work Yard Laying Bat to make others,Brick,General,Which Fijian golfer won the 1998 U.S. Masters,Vijay singh +Geography,What is the capital of Saint Vincent,Kingstown,General,What is the usual term for Scotoma?,Blind Spot,Geography,In which continent would you find the amazon river ,South america  +General,On an average day in USA 40 people are hurt doing what,Trampolining,General,In Hartford Connecticut its illegal to do what to wife on Sunday,Kiss her,Sports & Leisure,What Are The Only Two Fences Not Jumped Twice In The Aintree Grand National? (PFE) ,The Water Jump & The Chair  +General,"Eskimo ice cream is neither icy, nor ______",Creamy,General,What is a catalogue of languages,Ethnologue,General,Who invented the ice cream soda,Robert green +General,EAU international car registration plate which country,Uganda,General,What does the Australian slang word Hooroo mean,Good bye, Geography,What is the basic unit of currency for Maldives ?,Rufiyaa +Music,What was George's first composition on the A Side of a Beatles single?,Something,General,"Sharing his name with a former manager of Arsenal FC, who was the inventor of the orrery, a device used to represent the Solar System?",George Graham,Geography,The sun sets in the ____,West +General,What does a pulsar emit,Radio waves,Music,What Was Frank Zappa's Backing Band,The Mothers Of Invention,General,In which country is the largest gold refinery?,South Africa +Music,"Can You Name ""Tears For Fears"" Debut Album",The Hurting,General,What is God called in the Muslim faith,Allah,Science & Nature,What is measured by a Geiger counter?,Radioactivity +Music,In which area of London are the Abbey Road studios located?,St. John's Wood,Entertainment,In 'The Shining' what was the child's imaginary friend's name (the one who told him things that were going to happen)?,Tony,Entertainment,Who runs Andy Capp's favorite pub?,Jack and Jill +General,"What russian city was formerly known as st petersburg, then petrograd, and until 1918 was the capital of the country",Leningrad,General,Shamanism is the religion of which people,Inuit or Eskimos,Food & Drink,Who released the following 'edible' album Pies 'Descalzos' ,Shakira  +Science & Nature,What Would You Suffer From With Hypotension ,Low Blood Pressure ,General,The Bahamas lie off which U.S. state,Florida,General,Where is Dam Square,Amsterdam +General,At what address did the TV family 'The Addams Family' live?,001 Cemetery Lane,General,What is the final event in the modern pentathlon,Horse riding,General,"Who is known as the ""Father of Modern Economics"" ?",Adam Smith + History & Holidays,In What Century was St Nicholas first Mentioned ,4th ,General,Name European Cathedral known as the coronation cathedral,Rheims, Geography,What is the capital of Papua New Guinea?,Port Moresby +Geography,What U.S. city is known as Insurance City,Hartford,General,What profession receives the Pritzker prize,Architects,General,Ncaa: who was the mvp in the men's basketball championship game in 1976,Kent benson +Art & Literature,What Are The March Sisters Names In Louisa M Alcotts (Little Woman) ,"Jo, Meg Beth & Amy ",General,Casanova ate fifty each morning to increase potency 50 what,Oysters,Music,"Who Saw Nothing Wrong In A Little ""Bump And Grind"" In The 90's",R Kelly +Mathematics,How many nickles are there in $2.25,45,Entertainment,"On 'The Lucy Show', who played Vivian Bagley?",Vivian Vance,Science & Nature,What constellation is represented by scales?,Libra +General,Confederate General William Smith carried what into battle,A Blue Parasol,General,Leaves forming the protective case of a flower bud,Calyx,General,"From what ""black metal"" did blacksmith's take their name",Iron +General,An average of 708 what in the USA each year,Tornados,Geography,In which country would you find the yucatan peninsula ,Mexico ,General,What is the main ingredient of the Indian dish 'dhal',Pulses + History & Holidays,Who played the Spirit of Christmas in the 1988 TV film 'Blackadder's Christmas Carol'' ,Robbie Coltrane ,Music,Alex Gifford & Will White Were Joined By Shirley Bassey On Their Single History Repeating By What Name Are They Known Collectively,Propellerheads,General,John Tenniel drew the illustrations for what famous book,Alice in Wonderland +General,Which planet has a year lasting approx. 88 earth days,Mercury,General,What is chorizo,Spanish salami,General,"A passageway of a Christian Church or a Roman basilica running paralell to the nave, separated from it by an arcade or colonnade. ",Aisle +Entertainment,What film was the last featuring Mel Blanc's voice?,Jetsons,General,What country drinks the most beer,Germany,General,"This instrument is used for measuring the distance between two points, on a curved surface.",Calliper +General,Pneumatiphobia is the fear of,Spirits,General,What was the first zeppelin made of,Aluminum,Art & Literature,Which member of the Monty Python Team wrote children's books about 'Erik The Viking''? ,Terry Jones  +General,Raquel Welch was once a what,Weathergirl,General,What breakfast dish was ordered by a writer as a hangover cure,Eggs benedict,General,"What singer who had his one and only hit song in 1991 play a high school jock named Ricky in the 1987 movie ""Can't Buy Me Love""?",Gerardo Mejia +General,Cab is a shortened version of what word,Cabriolet,General,"Who composed ""Night on the Bare Mountain"" in 1867",Moussorgsky,Sports & Leisure,What Is The Maximum Number Of Horses Allowed To Run In The Grand National? ,40  +General,What title is reserved for the leader of the entire Ku Klux Klan,Grand wizard, History & Holidays,Which band member was Boy George allegedly seeing in Culture Club during the 80's? (Just name instrument he plays) ,The Drummer ,Entertainment,What was the first film directed by Robert Redford?,Ordinary People +Music,"From 1994 What Was ""All For Ones"" Biggest Hit",I Swear,General,"In the movie ""Rain Man"" (1988), she stared with Tom Cruise and Dustin Hoffman",Valeria golino,General,"With Which 80's Band Is Was ""Martin Fry"" The Lead Singer",ABC +General,"What is the full name of the creator of ""Jeeves & Wooster""",Pelham grenville wodehouse,Sports & Leisure,How many games must you win to win a normal set in tennis?,Six,General,What is the world's tallest grass,Bamboo +Geography,What is the capital of Mauritania,Nouakchott,General,Who was the male lead in the 1965 film the war lord,Charlton heston,General,Psyscrophillia is sexual arousal by what,Cold +Food & Drink,"What soft drink is advertised with the slogan, What's the worst that could happen? ",Dr Pepper ,General,Who discovered florida,Ponce de leon,General,To what family of vegetables does the popular Zucchini or Courgette belong,Gourd or squash +General,In France if you were served le miel what would you eat,Honey,General,41% of American women believe what nationality best husbands,Canadian,General,"What makes Argon, Neon and Helium unique in chemistry",No compounds +Science & Nature,This term means 'cone_bearing trees'.,Conifers,Music,Ruth Ann Boyle was the lead singer of which band who had a UK Number One single in 1997 with “You're Not Alone”?,Olive,General,"What show was ""Whatcha talkin' 'bout Willis?"" a standard catch phrase?",Different Strokes +General,Which was the second japanese city bombed in 1945,Nagasaki,Music,Name The Lead Singer With The Bangles?,Susanna Hoffs,Food & Drink,"What is the common name for goobers, pindars, groundnuts, or grass nuts? ",Peanuts  +General,The Korat plateau in Thailand gives its name to what kind of animal,Cat,General,Pluviophobia is the fear of,Rain,General,"Dogs bark and cows moo, but what does a Khaki Campbell do",Quack +General,How many legs does a lobster have,Ten,General,"What, according to Hilaire Belloc's cautionary verse, was Matilda's problem",She told such dreadful lies,Sports & Leisure,Which Boxer Was Nicknamed 'The Clones Cyclone''? ,Barry McGuigan  +Sports & Leisure,How Many People Are In Each Of The Crews For The Oxford & Cambridge Boat Race? ,9 = 8 Rowers & 1 Cox , Geography,What is the capital of Bolivia ?,La Paz,General,Mary Cathering Collins became famous as who,Bo Derek + History & Holidays,Cleopatra's slaves often died because she tested this on them,Poison,Geography,What Countries Flag Has Only One Colour (Green) ,Libya ,General,What us general was known as old blood and guts,George s patton jr +Geography,Which Was The First Suspension Bridge In London ,Hammersmith Bridge ,General,Who invented the radio?,Guglielmo Marconi, History & Holidays,In the Christmas song what was 'Roasting on an Open Fire' ,Chestnuts  +General,Who is Olive Oyls brother,Castor Oyl,General,What was a sheriff during feudal rule in england,Reeve,General,What is another term for the instrument sometimes called a 'mouth organ',Harmonica + History & Holidays,Which country did Idi Amin invade in 1978? ,Tanzania ,Science & Nature,What element has the periodic table name Na ?,Sodium,General,Which major river flows through gloucester,Severn +Music,Which Was The First Single Released By The Beatles On The Apple Label,Hey Jude,General,What company makes Pampers disposable diapers?,Proctor & Gamble,General,What was Marie Osmonds only solo hit,Paper Roses +General,Honi soit qui mal y pence is the motto of what organisation,Order of the Garter,General,What kind of pain is a migraine,Headache,General,"American applied mathematician & electrical engineer, noted for his development of the theory of communication now known as information theory",Shannon +General,Denis Gabor of Hungary 1971 Nobel prize for what invention,Holograms,General,"Who said ""necessity is the mother of invention""",Ovid,General,From what words is dublin derived,Dubh linn +General,What is the only insect that can turn its head,The praying mantis,General,In Welsh Cwrw pronounced koo roo is what,Beer,General,What breakfast cereal was invented at battle creek sanitarium,Cornflakes +General,The 'love apple' is more commonly known as what,Tomato,General,Where were the first winter Olympics held in 1924,Charmonix France,Technology & Video Games,"Which RPG series features a line of inventors of the name ""Shaia?"" ",Lufia +Science & Nature,Gastritis affects the __________.,Stomach,Art & Literature,Which Celebration Of The Arts In Held In Wales ,The Eisteddfod ,General,What is the flowering shrub Syringa usually called,Lilac +General,What country saw the origin of lawn tennis,England, History & Holidays,"Which toy, with a name meaning 'play well'' was launched by Danish toymakers Ole and Godtfred Kristiansen in 1958 ",Lego ,General,In what new york city club did many famous vocalists get their start,Continental baths +General,"Who started on the san francisco scene with 'oh well', but are probably best known for their album 'rumors'",Fleetwood mac,General,Whose picture was on the first adhesive nickel postage stamp in the US?,Benjamin Franklin,General,What capital city began as the village of Edo,Tokyo +Sports & Leisure,How Many Players Are In A Polo Team? ,4 ,Science & Nature," The famous cow used as the corporate symbol on all Elmer's products is actually named __________, and she is the spouse of Elmer, the steer who the company is named after.",Elsie,General,Which German Company Developed The First (ABS) Breaking System?,Bosch +General,What was the name of He-Man's magician sidekick?,Orko,General,Who wrote Ode to a Nightingale,Keats,General,What are jackrabbits,Hares +Music,Where Did The Beatles Appear On The 30th January 1969,"Roof Of The Apple Offices ""Saville Row, London""",General,"In ballet, a position of the arms above the head.",En haut,Music,"Which Girl Group Had Hits With ""Give It Up, Turn It Loose, Whatta Man , & Don't Let Go Love""",En Vogue +General,What bird has the most feathers per square inch,Penguin,Sports & Leisure,Who wore a cabbage leaf under his cap?,Babe Ruth,General,What Does It Say Under The Lion On The MGM Symbol,Art For Arts Sake +Entertainment,"What film starred Helen Hunt, Cary Elwes and Bill Paxton?",Twister,Science & Nature,What Is The Correct Name For The Adam's Apple ,The Larynx ,General,What play is set in Venice and Cyprus,Othello - Shakespeare +General,Autophobia is a fear of ______,Being alone,General,Which department is the ITU in a hospital,Intensive therapy unit,General,What is a Rhodesian Ridgeback,Dog +Science & Nature,Who Was The First Animal In Space ,Laika The Dog ,General,Towards which direction does the Tower of Pisa lean?,South,Music,Name The Instrument Played By Larry Adler,Harmonica +Geography,Name the second largest country in Africa.,Algeria,General,Insectophobia is a fear of ______,Insects,Science & Nature,Which planet is covered in thick clouds of carbon dioxide and sulphuric acid ?,Venus +General,What is the Capital of: Korea South,Seoul,Art & Literature,In Which Shakespearean Tragedy Does Laertes Appear ,Hamlet ,General,"""Antananarivo"" Is The Capital City Of Which Country",Madagascar +General,What is the fear of one's own fears known as,Phobophobia,General,"Of which piece of music is ""The Great Gate at Kiev"" a part",Pictures at an exhibition,Science & Nature," A mated pair of __________ can produce up to 15,000 babies in one year.",Rats +General,What is the name of the capital of Quebec (Canada),Quebec city,Music,In Which American City Did The Beatles Play Their Last Concert In 1966 Vowing Never To Tour Again,San Francisco,General,The British Raj in India lasted 90 years what's it literally mean,Rule +General,Neophobia is the fear of,Anything new,Music,Who was the first American-born principal conductor of the New York Philharmonic Orchestra?,Leonard Bernstein,General,In which town or city in the north-west is the Rylands Library,Manchester +Art & Literature,In which book did four ghosts visit Scrooge?,A Christmas Carol,Music,Which Musical Featured A Giant Faberge Egg Containing A Hologram Of Laurence Olivier's Head,Daves Clark's Time In 1986,General,What did the name 'battenberg' become,Mountbatten +General,What artist cut off his right ear,Vincent van gogh,General,Which is the longest river in the western hemisphere,Amazon,Entertainment,What was the first cartoon character called?,Oswald the Rabbit +General,Which character from beavis and Butthead has their own show?,Daria,General,What would an anemometer measure,Wind Speed,Entertainment,What composer was working on his 10th symphony at the time of his death?,Ludwig van Beethoven + Geography,Which country has Budapest as its capital?,Hungary,Science & Nature,By What Abbreviated Term Is The Group Of Eight Industrialized Nations Known ,G8 (G8 Sumit) ,General,Which baltic seaport was the german rocket centre during wwii,Peenemunde +General,Haiphong is the third largest city in which south-east Asian country,Vietnam,Sports & Leisure,Who Did Forbes Magazine Name As The Highest Earning Sportsman Of 2006? ,Michael Schumacher ,Science & Nature,What is the meaning of the name of the constellation Delphinus ?,Dolphin + History & Holidays,What Are The first Names Of The Everly Bros ,Don & Phil ,General,Which computerized diagnostic technique is Godfrey Hounsfield credited with inventing,Cat scan,Music,As Of 2009 Who Is The Only Non British Band To Perform On A James Bond Theme,Aha +General,Pluto (the Planet) was almost called what name,Zeus,Music,Who Did Spice Girl Mel B Marry In 1998,Jimmy Gulzar,General,Buenos Aires is the capital of ______,Argentina +General,Name (in the US) Denis the Menaces cat,Hot Dog,General,In which Royal residence did both George V and George VI die,Sandringham,General,Who started the second punic war,Carthage + History & Holidays,Who sang the theme song to the 'Breakfast Club'? ,Simple Minds ,Food & Drink,What is special about Porcini mushrooms ,They Are Dried ,General,What ws Balki Bartokamus' occupation when he lived in Mypos?,Sheep Herder +General,Name the companion of the cartoon character Secret Squirrel,Morocco Mole,General,Most people wear a watch on their ____ wrist.,Left,General,In 1887 who solved his first case,Sherlock Holmes +Science & Nature, Camel milk is the only milk that doesn't curdle when __________,Boiled,General,King Faisal was assassinated in 1975. Of which country was he king,Saudi arabia,General,"Which television western series always ended with the words 'Head 'em up, move 'em out'",Rawhide +Sports & Leisure,What Does The C Stand For On A Netball Team? ,Centre ,General,Another name for a villain or scoundrel,Blackguard,General,In what form of government do paid officials exercise controlling influence,Bureaucracy + History & Holidays,Where were Anne Boleyn and Catherine Howard both executed? ,The Tower Of London ,Geography,What is the capital of Finland,Helsinki,General,Who wrote David Copperfield,Charles dickens +General,Of which metal is sperrylite the ore,Platinum, History & Holidays,Who wrote The Strange Case of Dr Jeckyll and Mr Hyde ,Robert Louis Stevenson ,General,The constellation Norma has what English name,Level +General,Which leader sits on the 'Lion Throne'?,Dalai Lama,Sports & Leisure,In which year did Eddie the Eagle Edwards represent England ? ,1988 ,General,In Fargo North Dakota you can be jailed for dancing with what,Your Hat on +Entertainment,"In the TV series 'The Fall Guy', who did Lee Majors play?",Colt Seavers,General,Who built the 'cherokee' and 'commanche' aircraft,Piper,General,What title did Zola give to the letter he wrote to the newspaper L'Aurore in 1898,J'accuse +Music,In which year was the Summer Of Love?,1967,General,A cow gives nearly how many glasses of milk in her lifetime,"200,000",General,The Alaska highway terminal is in,Edmonton +General,What canal parts redesigned by Leonardo da Vinci in 1497 are still in use today,Locks,General,The first president to ride in an auto; he didn't care for it much.,Theodore roosevelt,Art & Literature,What Did A E Housemans Initials Stand For ,Alfred Edward  +Food & Drink,What do Americans call what the British call an iced-lolly? ,Popsicle ,Science & Nature," Due to a retinal adaptation that reflects light back to the retina, the night vision of tigers is six times better than that of __________",Humans,General,What country which has the same name as a bird,Turkey +General,Who wrote the opera Madame Butterfly,Puccini,Music,What Instrument Was Played By King Curtis,Saxophone,General,As mad as a _______,Wet hen +Geography,"With about 865 people per square mile, the island of _____________ is one of Europe's most densely populated regions.",Madeira,Music,What were Madonna's book and 1992 album called?,Sex,Science & Nature,Which Animal Has The Longest Gestation Period? ,The African Elephant  +General,On what common object could you find a gate and a claw,Camera,General,What does an oologist study,Bird's eggs,General,Princeton University is in which state,New jersey +Science & Nature," Very unusual for carnivores, hyena clans are dominated by __________",Females,Sports & Leisure,In The 1990's Which Player Played Both For And Against Chelsea In F.A Cup Finals? ,Mark Hughes ,General,"Who was kidnaped on the night of March 1, 1932",Charles Lindbergh +General,What precious metal does the U.S. store in fort knox,Gold,General,In the US civil war what pet did Robert E Lee have,A Hen,Geography,In which country is the calabria region ,Italy  +General,What is the penalty for drunk driving in Sumatra,Loss of a hand,General,Who played nick nack & came rolling home,This old man,General,What kind of machine is built to be 'booted',Computer +General,What are the Christian names of the novelist P D James,Phyllis dorothy,General,What is the name of the Paris stock exchange,Bourse,General,What is the slogan on New Hampshire license plates,Live free or die +General,Who directed the 1916 film 'intolerance',D.w griffith,General,What is the scottish equivalent of the name john,Ian,General,Who was the 31st president of the U.S.,Herbert c hoover +General,Joseph Lister - first operation antiseptic - 1867 on who,His sister,General,Who said he wouldn't pay a nickel for another patterson-liston fight,Floyd,Art & Literature,What Colour Is The Art and Literature Wedge In (Trivial Pursuit)? ,Brown  +General,In 1449 Thomas Brightfield built London's first what,Lavatory, History & Holidays,Who Released The 70's Album Entitled Physical Graffiti ,Led Zeppelin ,General,Type of Pakistani curry cooked and served in a shallow dish,Balti +General,Where does the dollar sign come from,U on S bottom U dropped out $,Sports & Leisure,In Which Olympic Games Did Steve Redgrave Win His First Gold Medal? ,Los Angeles In 1984 ,General,Which RSPB reserve is located on the River Ouse near to the junction of the three rivers at Trent Falls?,Blacktoft Sands + History & Holidays,Near which Belarus City did the biggest ever tank battle take place during WWII?,Kursk,Science & Nature,What is the name of the instrument used for measuring humidity in the air ,Hygrometer ,General,In Hamlet who is Ophelia's father,Polonius +General,Which Tv Detective Kept A Pet Alligator Named “ Elvis ”,Sonny Crocket,General,How much did seward pay russia for alaska,7.2 million dollars,Music,Which Member Of The Police Starred In The Film Quadrophenia,Sting +General,Gus Wickie Pinto Colvig William Pennell Jackson Beck - Link,Bluto in Popeye,Music,"Which Famous Singer / Drummer Played Drums On The Band Aid Single ""Do They Know It's Christmas""",Phil Collins,General,"In the U.S. comedy series Sergeant Bilko, what is Bilko's first name",Ernie +General,Who was the world's longest reigning monarch,Louis xiv of france,Science & Nature, An __________ egg can make eleven_and_a_half omelets.,Ostrich,General,"What do revelers drink at a ""pivince"" tavern in Prague",Beer +General,President Andrew Jackson's funeral 1845 who removed swearing,His Pet Parrot,General,If you were suffering from Preblysis what have you got,Premature Ejaculation,General,In what sport does herringboning take place,Skiing +General,Who made a 1990's cover version of The Monkees 'I'm a Believer',Vic reeves,Music,"Who Had A Top 10 Album In 1981 With ""Mondo Bongo""",The Boomtown Rats,General,Collective nouns an erst of what creatures,Bees +General,Slang word meaning aggressively blatent or provocative,In-your-face, History & Holidays,During Which King's Reign Did The Great Fire Of London Occur? ,Charles II ,General,Florizel & Perdita are characters in which Shakespeare play,The winter's tale +General,With which group is Damon Allbarn the lead singer,Blur,General,The only #1 best selling novel in the us to be published anonymously,Inner,General,"Which creatures communicate by touch, smell and dance",Bees + History & Holidays,"Is kissing under the Mistletoe, a Roman, Druid, or Celtic custom ",Druid ,General,What is the more familiar name to the British of the Swiss lake Vierwaldstattersee,Lake lucerne,General,A group of partridges is called a,Covey +General,Chang 1st Wang 2nd what third most common Chinese name,Li,General,Which famous radio and television family appeared in the show 'Take It From Here',The glums,Music,Which Record Company Did Blondie Spend Their Sucessful Career With,Chrysalis +General,Pitney which is the largest planet in our solar system,Jupiter,Music,"Buddy Holly Died In A Plane Crash In 1959, Which Other Two Singers Died With Him",Ritchie Valens & The Big Bopper,Music,What's The Name Of The British Pop Sensation Comprising Of A Number Of Oap's That Released Their Debut In June 2007,The Zimmers +Food & Drink,What Type Of Foodstuff Is Traditionally Eaten On Shrove Tuesday ,Pancakes ,People & Places,Which Aussie Wicket Keeper Bet Against His Own Team At 500-1 And Won The Bet ,Rodney Marsh ,General,In Christian tradition what day is known as the festival of lights,Candlemas 2 Feb +General,Which light wood is commonly used for making aeromodels?,Balsa,General,Marley whose likeness is depicted on the purple heart,George washington,General,Which U.S. state is sometimes known as 'Seward's Folly',Alaska +General,White creame de menthe and brandy make what cocktail,Stinger,General,"In the movie Porkey's, why did they call Meat Tuperello 'Meat'?",Because of the size of his penis,General,What's one round of a polo match called,Chukkah +General,A catalogue of words and synonyms.,Thesaurus,General,The Plains of Abraham overlook which city,Quebec,Science & Nature,What is the technical name for 'falling stars'?,Meteors +General,Who was Americas first billionaire,Rockefeller,Geography,In What State Is The Grand Canyon Found ,Colorado ,General,"In The Simpsons, what is the first name of Mr Burns assistant Smithers",Waylon +General,More water flows over _____ _____every year than over any other falls on earth,Niagara falls,General,What was mozart's first name,Wolfgang, Geography,What is the basic unit of currency for Sweden ?,Krona + History & Holidays,"""Hot cockles"""" was popular at Christmas in medieval times What was it? """"A Drinking Song"""" """"A Medievil Game"""" """"A Dish Of Seafood"""" """"A Hot Spice Drink"""" "" ",A Tudor Game ,General,What short-lived sitcom featured Snow White and Prince Charming living as husband & wife?,The Charmings,General,Which comedienne's alternative persona is 'Mrs. Merton',Caroline aherne +General,Which is the largest of the Canary Islands,Tenerife,General,What sport did burt reynolds play at florida state university,Football,Entertainment,"Popeye's chief adversary has two names, Bluto and ______?",Brutus +General,Mary Read and Anne Boney had what job in common,Pirates,General,Who played tv's superman in the original series,George reeves,General,What is the small grey and brown titmouse,Bird +Science & Nature,What is a group of peacocks called?,Muster,General,"The Smithsonian Institute houses this 44 & 1/2 carat, cursed blue diamond",Hope diamond,Music,Which Roy Orbison Track Entered The Top 10 In The 90's,I Drove All Night +General,"In 'romeo and juliet', why couldn't the nurse tell juliet the news of her meeting with romeo immediately",She was out of breath,General,Jimmy Doyle died during a title fight in 1947 who was opponent,Sugar Ray Robinson,General,Which society cared - plague victims when physicians left 1665,Apothecaries +General,What is the Capital of: Vanuatu,Port-vila,General,What is the smallest state of Australia,Tasmania,General,Which country did the French know as Terra Napoleon,Australia +General,"In The World Of Science What Was Discovered By ""Clyde Tombaugh"" In The Year 1930",Pluto,General,"In 'star wars', sebastian shaw was darth vader's ______",Face,Music,According To The Lyrics Of The Eagles Classic Hotel California When Can You Check Out?,Anytime You Like +General,Poliosophobia is the fear of,Contracting poliomyelitis,Music,What Was The First Single To Sell 2 Million Copies In Great Britain,Mull Of Kintyre / Wings,General,What charles manson follower attacked a fellow inmate with a hammer,Lynette +General,U.S. captials Florida,Tallahassee,General,Leatherjackets are the larvae of which insect,Cranefly,General,Who was known as guardian of the safety of the world,Captain video +General,How was the country of Belize known prior to 1971?,British Honduras,General,What is the subject of the reference book Janes,Militaria – Ships Planes Guns etc,General,"The Portuguese capital, Lisbon, stands on which river",Tagus +General,In which country was the first soccer World Cup held,Uruguay,Science & Nature,Which Cross Eyed Lion In A 1965 Film Spawned The Dakari Television Series ,Clarence ,General,"Which Royal Property Did Prince Albert Pay 31,000 Pounds For In 1846?",Balmorral +Science & Nature," We are sure that whales and dolphins had land_living ancestors, but we don't know what they were like and we don't know how they __________",Evolved,General,Which was the last country to host both the Summer and the Winter Olympic Games in the same year,Germany,Music,Whe Are Were Typically Tropical Going To,Barbados +General,What do x & y chromozomes combine in making,Males,General,"In area, which is the largest Australian state",Western australia, History & Holidays,Which nation did Moshoeshoe found?,Basotho +General,By what other name is Lac Leman known,Lake geneva,General,What is the flower that stands for: warmth of sentiment,Spearmint,General,Music: This Vienese piano manufacturer includes 3 extra bass keys. It is the favorite piano of such performers as Tori Amos.,Bosendorfer +General,Charles Duff wrote the macabre Handbook of what,Hanging,General,A large oven in which pottery is fired is a ____,Kiln,General,What was the Acta Diurna introduced by Julius Caesar 59 BC,Daily News (paper) posted in forum +General,What breed of dog bites the most humans,German Shepherd – Alsatian,Entertainment,An arrangement for five performers is called a:,Quintet,General,Who Was The Very First Artist Managed By Louis Walsh To Have A Number One In the UK ?,Johnny Logan +General,What Type Of Creature Is Located On Top Of The Calcutta Cup,Elephant,Art & Literature,"Name the author of his famous and only novel 'Doctor Zhivago', which presents a panoramic view of Russian society at the time of the 1917 Revolution.",Boris Pasternak,General,Julius Caesar Hamlet Macbeth Richard III links not Obvious,All plays contain ghosts +General,Where were fortune cookies invented,United states,General,Who is the greek equivalent of vulcan,Hephaestus,General,Who was Chancellor of the Exchequer during the 1949 devaluation of the Pound,Sir stafford cripps +Science & Nature,Which substance has the chemical formula HCl?,Hydrochloric acid,General,What is another name for a sleepwalker?,Somnambulist,Music,"Whose Albums Include ""Kind Of Blue, In A Silent Way, Bitches Bru & Tutu""",Miles Davis +General,What actress wrote the autobiography Call Me Anna,Patty duke,General,"Starring Nigel Hawthorne, which 1994 film was publicised with ""His Majesty was all-knowing. But he wasn't quite all there.""",The madness of king george,General,What is the flower that stands for: sleep,White poppy +General,In which river was jesus baptised,Jordan,General,What was the name of the topless stage dancer with ample charms who often accompanied the rock band Hawkwind on stage?,Stacia,General,You have a foursome and a shag what have you done,Dance they are types of dance +Science & Nature,"How Many Legs, Including Pincers Does A Crab Have? ",Ten ,People & Places,Which Former Radio One DJ Used To Broadcast 'Our Tune' ,Simon Bates ,General,Muppet vampire enjoyed doing this.,Counting +General,"In Terry Pratchett's Discworld series, Death rides a pale horse. What is its name",Binkie,General,In Vermont it is illegal to do what under water,Whistle,General,Symbolic addmission to Christianity and name giving,Baptism +General,What Is Michael Jacksons Middle Name,Joseph,Geography,Name the largest island in the world.,Greenland,General,Mstislav Rostropovich was a maestro on what instrument,Cello +General,"In art, a form of watercolor that uses opaque pigments rather than the usual transparent watercolor pigments",Gouache, History & Holidays,"""What Did My True Love Give To Me On The """"First"""" Day Of Christmas"" ",A Partirdge In A Pear Tree ,Art & Literature,The Hardy Boys and ______?,Nancy Drew +General,Dish of food covered with alcohol and set alight,Flambe, History & Holidays,In which country do people eat noodles while listening to a bell start ringing and strike 108 times? ,Japan ,General,What is the Capital of: New Caledonia,Noumea +Geography,What is the capital of the United Arab Emirates,Abu dhabi, Geography,What is the capital of Eritrea ?,Asmara,General,When did prohibition end in the USA,1933 +General,Collective Nouns - a Convocation of what,Eagles, Geography,Where are the great Walls of Babylon located in the modern day world ?,Iraq,General,Dire straits sings that 'we're fools to make war on our ______',Brothers in +Entertainment,Tess Trueheart married which plainclothes detective,Dick tracy,Music,Who Spent Midnight In Moscow,Kenny Ball And His Jazzmen,Science & Nature,Which Flightless Bird From Mauritus Is Now Extinct ,The Dodo  +General,In Kansas City its illegal for children to buy what,Cap Guns – but shotguns are OK,General,What is the main ingredient of faggots,Liver,Sports & Leisure,For Which Formula One Team Did Both Nigel Mansell & Damon Hill Drive ,Williams  +General,What color are a zebra's stripes during the first six months of life,Brown, History & Holidays,What is the fear of Halloween called is it A Samhainophobia B Hallowphobia or is it C Druiphobia ,Samhainophobia ,General,What organisation was founded Canada by Mrs Hoodless 1897,The Women's Institute +General,If you had variola what disease have you got,Smallpox,General,"Who sang 'popsicle', 'hangin' tough', please don't go girl' and 'step by step'",New kids on the block,General,What is the birthstone for May?,Emerald + History & Holidays,What Was A Sopwith Camel? ,A Single Seater Armed Biplane Used In WWI ,General,What Is Measured Using A “ Spirometer ”,Lung Capacity,General,In Which London Building Is The Annual Lord Mayor's Banquet Held?,The Guild Hall +Science & Nature,Which is the largest aquatic bird?,Albatross,Sports & Leisure,In A Decathlon What Is The First Event ,100 Metres ,People & Places,What Do You Call A Person From Manchester ,Mancunion  +General,At the outbreak of WWI what country's airforce consisted of only 50 men?,United States,General,"On a ship, what is the plimsoll line",Loading line,General,What do trees get 90% of their nutrients from,Air +General,"What actress said ""It's better to be nude than unemployed""",Angie Dickinson,General,"What hobby uses the term ""cast on""",Knitting,Science & Nature,What do you offer someone as a peace gesture?,An Olive Branch +Science & Nature, __________ can climb trees faster than they can run on the ground.,Squirrels, History & Holidays,Which former British prime minister returned to power in 1951? ,Winston Churchill ,Music,The Housemartins Had A Caravan Of What,Love +General,Kalium. is the Latin name for which element,Potassium,Toys & Games,To what do opposite faces of a dice always add up?,Seven,General,"In the play, ""Death of a Salesman"", what was the name of the salesman",Willy loman +General,What do male butterflies like to lick,Stones - to get nutrients,General,The Stirling prize is awarded annually for which field of design,Architecture, History & Holidays,Who discovered the vaccination against smallpox in 1796? ,Edward Jenner  +General,Women do it weekly to sleep better men every two weeks what,Change sheets,General,The octal number system is based on the number _____,Eight,Music,The Sugarcubes Are From Which Country,Iceland +General,What was the print left on the car in the boat in the movie Titanic?,A handprint,General,"What's a ""group"" of toads called",Knot, History & Holidays,Four Japanese carriers were destroyed in this battle.,Midway +Food & Drink,The Process Where Food Browns During Cooking Is Known As The What ,Maillard Reaction , History & Holidays,Which 20th century American icon died on Halloween After A Night Out At The Viper Rooms ,River Phoenix ,General,What french painter was the subject of somerset maugham's 'the moon and sixpence',Gauguin +General,The Ten Commandments what was number four,Keep the Sabbath holy,General,In which house did Charles Dickens live from 1857 to his death in 1870,Gad's hill,General,"What do itchy people call the ""rhus radicans"" they were sorry they came into contact with",Poison ivy +General,In which country was Rudyard Kipling born,India,General,Cyprieunia is sex with who or what,A Prostitute,General,What system is based on the number 60,Sexagesimal +General,In which Commonwealth country is Cobra beer brewed,India,Toys & Games,"In which game or sport can a person be ""skunked""?",Cribbage,General,Which is the largest landlocked country,Mongolia +General,"Which river passes through germany, austria, slovakia, hungary, croatia, yugoslavia, romania, blugaria and ukraine before arriving at the black sea",Danube,General,With what sport is the Zimbabwean Heath Streak associated,Cricket,Entertainment,What did Hannibal Lecter like to eat with liver?,Fava Beans +General,"A ballet in which the women wear white tutus, such as the second and fourth acts of Swan Lake.",Ballet blanc,General,Name Canada's oldest incorporated city,Saint John,Science & Nature,"Corolla, filament and stigma are parts of a(n) _________.",Flower +General,The Coromandel Coast is an area which can be found in which two countries?,India or New Zealand,General,What does a heliologist study,The sun,General,Which was the first South American country to gain independence from Spain,Paraguay +General,An Ochlophilliac gets sexually aroused from what,Being in crowds,Music,"In 1922, which composer scored Mussorgsky's piano piece Pictures at an Exhibition for full orchestra?",Ravel,General,The covering on the tip of a shoelace is a(n) _____.,Aglet +Music,In The Peter Kay Video To Is This The Way To Amarillo Who Are The First 2 People To Join Peter Kay On His Journey In This Charity Video,Ken & Deirdre (Barlow),General,In the bible who authorises the Crucifixion,Pontius pilate,General,What is the oldest ship commissioned in the Royal Navy,HMS Victory from 5/7/1775 +Music,"""Safety Dance"" Was A Hit For Which Band",Men Without Hats,General,Lusaca Is The Capital Of Which Country,Zambia,General,Virginia woolf what two rivers join forces on the outskirts of st louis,Missouri and +General,Phoenix is the capital of ______,Arizona,Music,"Who Performed A 1982 No.1 Duet, Without Actually Laying Eyes On Each Other?",McCartney & Wonder,General,Pidge appeared in which Disney film,Lady and Tramp pet name Lady +General,Who wrote the humorous books on One Upmanship,Steven Potter,General,Who composed the cantata Carmina Burana,Carl orff, Geography,Is Belfast in Northern or Southern Ireland?,Northern +General,What links the Isle of Portland to the mainland coast of Dorset,Chesil bank,General,A nervous kangaroo licks its where,Forearms,General,What are the longest cells in the human body,Neurones +General,Who designed the bouncing bomb in World War II,Barnes wallis,General,How many days and nights did the Lord flood the earth while Noah and his family were safely aboard the ark,Forty 40 fourty,Science & Nature, The duckbill platypus of Australia can store up to 600 worms in its large cheek __________,Pouches +Entertainment,Who founded 'Live Aid' and 'Band Aid'?,Bob Geldof & Midge Ure,General,What is the occupation of Tom Hanks' character in Bachelor Party?,School Bus Driver, Geography,Name the strait joining the Atlantic Ocean and the Mediterranean Sea.,Gibraltar +Music,"Lou Rawls, Sam & Dave , Wilson Pickett, Otis Redding Who Is Next In Line And Why",James Brown,Entertainment,Who sang 'You Can Call Me Al'?,Paul Simon,General,Brilliant Bumper Bubbles Bigheart Boofuls Baby Bonny are who,Jelly Babies +General,What links George Patton Jayne Mansfield Margaret Mitchell,Died in car crashes,General,What does the computer acronym EFTPOS stand for,Electronic fund transfer at point of sale,General,Mastigophobia is the fear of what,Flogging +Science & Nature,What Is The Worlds Longest Manmade Waterway ,The Grand Canal In China ,General,Which city has the most homeless cats per square mile,Rome,General,Who killed jesse james,Robert ford + Geography,What is the capital of Moldova?,Kishinev,General,"What is the name of the President of Peru, very much in the news during the siege of the Japanese Embassy in Lima",Fujimori,General,How many dice are used in Backgammon,Five +General, The art of tracing designs and taking impressions of them is _______.,Lithography,General,Who were the first people to be elected into the aviation hall of fame,Wright brothers,Art & Literature,"In HG Wells ""The Time Machine,"" two races of the future are the child-like Eloi, and the underground monsters called the ____?",Morlocks +General,By Law in Massachusetts what can you not do to a pigeon,Scare it,General,Haven What was the name of Flash Gordon's girlfriend,Dale arden,Food & Drink,What Spirit Is Used To Fortify Red Wine Thereby Creating Port ,Brandy  + History & Holidays,Which bank holiday was first celebrated in Britain in 1978? ,1st May ,General,Miss Ellen Church was the worlds first what in 1930,Air Stewardess,General,What nation did 23 African countries sever relations with in 1974,Israel +General,N2O is more commonly known as what,Laughing gas,General,"Alicante, Money-maker and Ailsa Craig varieties of what",Tomatoes,General,The era that followed the Mesozoic,Cenozoic +General,On a standard rainbow what colour is on the inside of the curve,Violet,General,What is the flower that stands for: splendid beauty,Amarylis,General,Who ruled judea and had john the baptist beheaded at the request of salomen,Herod antipas +General,In what film does Steve McQueen get to race a Mustang round the streets of San Francisco,Bullitt,General,Who was the blind Norse god of who shot balder,Hoeder,General,Who was Cuisine Minceur designed for,Slimmers (Fine cooking) +General,What is the flower that stands for: charity,Turnip,General,"Where were the fighting shipes 'arizona', 'oklahoma' and 'utah' sunk",Pearl,General,What is San Francisco's equivalent to Sydney's 'City To Surf' race?,Bay to Breakers footrace + History & Holidays,"The Colosseum received its name not for its size, but for a colossal statue of who that stood close by?",Nero,General,"What's parts include barbican, oilette and donjon",A Castle,Music,Who Was Tossing & Turning In 1965,Ivy League +Science & Nature,What Is The Name Of Stephen Hawkins Famous Cosmology Book Published In 1998 ,A Brief History Of Time ,General,Phonophobia is a fear of ______,Voices,General,What is the capital of the state of West Virginia,Charleston +General,Mauritania is in which continent,Africa,General,The Owen Ford Dam one of worlds largest is in which country,Uganda,General,What is the more common title of the sovereign of the state of Vatican City,Pope +General,From pasadena Whose soldiers were the first ever to win the Nobel Peace Prize,United,General,Brasco is Australian slang for what,Toilet,General,How was Alexander the Greats body preserved,In large jar of honey +General,Under what name is thomas lanier williams better known,Tennessee williams,General,What was the first game show on Mtv?,Remote Control,General,Which musical instrument did Dizzy Gillespie play,Trumpet +General,What is a water taxi,Gondola,General,A turn while jumping straight up in the air.,Tour en l'air,General,Ben Veeren played what character in Arthur Hailey's roots,Chicken George +Geography,Acadia was the original name of which canadian province ,Nova scotia ,General,What was Hugh Hefner's jet plane called,Big Bunny,General,In which country did the Gallipoli Campaign take place,Turkey +Science & Nature,"The ""canebrake"", ""timber"" and ""pygmy"" are types of what?",Rattlesnake,General,Randy Newman said short people don’t have what,Reason to Live,General,How is something cooked if done en papillote,In Paper +General,Who wrote the Prisoner of Zenda?,Anthony Hope, History & Holidays,Which shipbuilding company constructed the Titanic? ,Harland & Wolff ,General,What animal could be Siberian or Caspian,Tiger +General,Every ship in the Royal Navy have customised what,Zippo Lighters,General,What species of mammal can come in fairy or giant size,Banded Armadillo,General,Who was sports illustrateds sportsman of the year in 1954,Roger bannister +General,"Measured from base to summit, what is the highest mountain",Mauna kea,Food & Drink,This is a common nickname for McDonalds(singular),Micky d,General,What does the girls name Zoe mean,Life +General,Kuwait is the capital of ______,Kuwait,Sports & Leisure,In What Sport Would You (Catch A Crab) ,Rowing ,General,What is the official language of Ethiopia,Amharic +General,Arched or domed recess at the end of a church,Apse,General,What did J Edgar Hoover bar people from walking on,His Shadow,General,Who had an acting career in the u.k and got his first break in music when he was chosedn to be a replacement drummer in genesis in 1970,Phil collins +General,What European city is nicknamed Auld Reekie,Edinburgh,General,Which song did robin luke write about his 5 year old sister,Susie darlin',General,Graham McPherson changed his name to what,Suggs from Madness +Geography,If You Drove In A Straight Line From Moscow To Madrid How Many Countries Would You Drive In All Together ,"Eight (Russia, Belarus, Poland, Czech Republic, Germany, Switzerland, France & Spain ",General,"In 'startrek', who played captain james t kirk",William shatner, History & Holidays,What was the name of Elizabeth Taylor's character in 'Cat on a Hot Tin Roof'? ,Margaret Pollitt 'Maggie the Cat''  +General,A person who works with iron,Blacksmith, Geography,"Mayfair, London is a district of little streets near ______?",Hyde Park,General,Linonophobia is the fear of,String +Food & Drink,What Is The Principle Ingredient Of The Indian Dish Biryani ,Rice ,General,The Egyptian godess Bastet was depicted with the head of which creature,Cat, History & Holidays,What Was The Name Of Dustin Hoffman's Character In The 1967 Film The Graduate ,Ben Braddock  +General,What European country has no head of state,Switzerland,Music,"In Which Year Did The Beach Boys Release ""Good Vibrations"" & ""Sloop John B""",1966,Music,Scary Spice Wed In 1998 But Where Was The Wedding Held,Little Marlow In Buckinghamshire +Geography,What Is The 3rd Most Populous Country On Earth ,The Usa ,General,Gillan Music: Who re-recorded 'Secret Agent Man' in 1979,Devo,General,Elsinore Castle is the setting for which of Shakespeare's plays,Hamlet +General,"Of the 266 popes, how many died violently",Thirty three,General,In The Arabian Nights what was Ali Babas job,Woodcutter,General,Gynecomania is what compulsion,Female sex partners +General,What is the correct name for a virgin (uncalfed) cow,Heifer,General,Who wrote Les Miserable,Victor Hugo,General,Graham bell His ship was the H.M.S. Beagle,Charles darwin +General,What is the flower that stands for: concealed love,Motherwort,Technology & Video Games,What was Fortran designed for ?,Formula Translation,General,In 1939 in the US what was the first patented plant,New Dawn Rose +General,Guru Nanak founded which religion,Sikhism,Science & Nature,What Element Is Used In The Process Of Galvanisation ,Zinc ,General,What is on a 5000 acre landfill at the head of Jamaica Bay near New York City?,John F. Kennedy Airport + History & Holidays,What was the capital of East Germany?,East Berlin,General,On which racecourse is the Lincoln Handicap run,Doncaster,Entertainment,Who was 'hooked on a feeling'?,Blue Suede +Art & Literature,What Is The Name Of The Russian National Ballet ,The Kirov Ballet ,Music,Footballer Jamie Redknapp Is Married To Which Pop Star,Louise,Music,"What Was Singing Duo ""Chan N Dave's"" First UK Top Ten Hit",Rabbit +Music,"Which Regard To The Po Boyband ""JLS"" What Do The Initials JLS Stand For",Jack The Lad Swing, History & Holidays,"In 1976 which computer company was set up by Steven Jobs and Stephen Wozniak, marketing the first open system machine in 1977? ",Apple ,General,What year was The Bible printed using moveable type,1455 +General,In which month is the Earth nearest the Sun ?,January,General,What would you expect to find in a binnacle,Ships compass,General,What ball contains 216 stitches,Official baseball +Music,Rod Stewart's first album was called after whiuch alley?,Gasoline Alley,General,"Common name for a family of mostly woody flowering plants, and for one of its important genera",Tea,Entertainment,"Who is the elder statesman of 'british blues', and fronted 'The Bluesbreakers'?",John Mayall + History & Holidays,"In the fairy tale 'Cinderella'', which slipper did she lose, the left or the right ",Left ,General,What did Archie Bunker call his son-in-law?,Meathead,General,What is viewed by a stroboscope,Rapidly moving objects + History & Holidays,Who Released The 70's Album Entitled Gasoline Alley ,Rod Stewart ,General,The book 'One Hundred Years of Solitude' won the Nobel Prize for Literature in what year,1982,General,"What type of men's jacket featured it's name on the outer breast pocket,and epaulets on the shoulders?",Member's Only +General,What is the name of the pig that Jim Davis draws,Orson,Sports & Leisure,What sport was first introduced into the Olympics in 2000? ,Trampoline ,Science & Nature,What is the proper name for falling stars?,Meteors + History & Holidays,Which Card Appeared In The UK For The Very First Time In 1963 ,American Express ,General,In Greek what (bad for your diet item) translates into solid bile,Cholesterol,General,She was the first woman to fly the atlantic solo,Amelia earhart +General,Who became Prime Minister following the assassination of Spencer Percival in 1812,Robert banks jenkinson,General,What is the science based upon the assumption that planetary influences affect human affairs,Astrology, History & Holidays,"I Love The Dead', 'Call It Evil' and 'Prince Of Darkness' are all singles released by whom ",Alice Cooper  +General,If you landed at MCO airfield where are you,Orlando Florida,General,Company what is the abbreviation for lake minnetonka,Lake tonka,General,Name the superspy man from Z.O.W.I.E. played James Coburn,Our Man Flint +General,Jeff Lynne - Roy Wood - Bev Bevan - what pop group,Electric Light Orchestra,General,What is the young of this animal called: Frog,Tadpole,General,Sergey Bubka broke world record over 30 times in which event,Pole Vault +General,International dialling codes - what country has 61 as code,Australia,General,Badderlocks is a form of___.,Seaweed,General,Collective nouns - a Trip of what,Goats +General,The film The Madness of King George III Dropped III - why,So Americans don’t think it’s a sequel,Sports & Leisure,How Many Hurdles Are Jumped Over In A 110-Metre Men's Hurdles Race? ,10 ,General,What's the largest & most powerful of the American cats,Jaguar +General,What is a roker,A foot long ruler,General,"Moving anti-clockwise on a dartboard, what is the number next to '4'",Eighteen,General,Where is the lowest point in south america,Argentina +General,"In October 1999, who replaced Frank Dobson as Secretary of State for Health",Alan milburn,General,Scrambled Eggs was the working title of which Beatles song,Yesterday,Art & Literature,Group of American artists from 1908 to 1918. Their work featured scenes of urban realism.,Ash Can School +General,What is the top New Years Resolution,Lose weight, History & Holidays,`Witchy Woman' was a hit for which American group in the 1970s ,Eagles ,Art & Literature,With What Art Movement Was (Salvador Dali) Associated ,Surrealism  +General,Illnesses in which the immune system reacts to normal components of the body as if they were foreign substances and produces antibodies against them,Autoimmune Diseases,Food & Drink,What Is The Meaning Of The Term Julienne ,Cut Into Thin Strips ,Entertainment,What is Cher's maiden name?,Sarkassian +General,"""Louis, 1 think this is the beginning of a beautiful friendship"" are the last words of which film",Casablanca,General,The first trees to move into a newly cleared forest area are called,Pioneer,General,What is the collective name for a group of hummingbirds,Charm +General,What is the flower that stands for: cleanliness,Hyssop,General,What's the light sensitive layer on photographic film called,Emulsion,General,"Shakespeare - Antony, Romeo, Othello - what in common",Suicide +Music,Which Shakespeare play shares it's title with a 1981 Dire Straits hit,Romeo & Juliet,General,What flavour is added to LAMBIC beers to make them into KRIEK,Cherry,Entertainment,A __________ helps to set and maintain your tempo while playing.,Metronome +General,How long can a bedbug live without food,One Year,General,Which French word is used to describe a restaurant wine specialist?,Sommelier,General,Of which Spanish province is Santander the capital city,Cantabria + Geography,Which is the only musical bird that can fly backwards?,Hummingbird,General,Which sport featured in the 1968 film 'Fat City'?,Boxing,Geography,Which City Is The Capital Of New Zealand ,Wellington  +People & Places,Which Liberal Leader Was Aquitted Of Attempted Murder ,Jeremy Thorpe ,General,What make of car is an 'Espace'?,Renault,Sports & Leisure,What nationality was formula one driver Ayrton Senna? ,Brazilian  +General,Bell's palsy results in numbness in which area,Face,General,Fluid produced in the lacrimal glands above the outside corner of each eye,Tears,Geography,Where is the Parthenon located,Athens +General,Which phrase means computer simulation that seems life like,Virtual reality,General,Who invented the ballpoint pen,Georgo and laszlo biro,General,Who replaced the assassinated Giacomo Matteotti in 1924,Benito +General,Slugs have four of them - what,Noses,General,In old English what is a Bellibone - From French Belle Bonne,Fair Maiden,Music,"Which Soft Cell Single Was Dismissed In A Melody Maker Review As ""Some Of The Most Appallingly Limp Music It's Ever Been My Misfortune To Hear""",Soft Cell / Tainted Love +General,In Japan what is Yomiyuri Shimbun,Newspaper – worlds best seller,General,"In 1543, who published a theory that planets revolve around the sun",Copernicus,General,What is grimace of the Mcdonald's characters,Tastebud +General,Fill in the blank: when you ____ upon a star,Wish,General,There are three parks in the USA dedicated to what,Butterflies,General,If you Absterse something what do you do,Clean it +General,The first US copyrighted film showed what in 1894,A man sneezing,General,"In which American state are the towns of Anaconda, Moscow and also the Salmon river",Idaho,General,Zero on a roulette wheel is what colour,Green +Science & Nature,What is the name of the layer between the Earth's crust and the Earth's core ?,Mantle,General,Which everyday labour-saving device was patented by Cecil Booth in 1901,Vacuum cleaner,Science & Nature,Which Is The Fastest Growing Plant? ,Bamboo (15 Inches A Day)  +Food & Drink,"Booze Name: Vodka, consomme, lemon, tabasco sauce, salt, pepper, celery salt.",Bullshot,General,Middle ages it was believed birds picked mates what Saints day,14th February – Saint Valentine,General,The ELO system is used to rate players of which board game,Chess +General,Who plays Auntie Wainwright in Last Of The Summer Wine,Jean alexander,General,Which class of racing yacht has the same name as a Wagner opera,Flying dutchman,General, A sun_dried grape is known as a(n) ___________.,Raisin +General,In Greek mythology Penthesilea was the queen of which people,Amazons,General,What drink is also known as Adam's Ale,Water,General,Name Def Leopards one armed drummer,Rick Allen +General,What was the annihilation of the jews in wwii called,Holocaust,General,What shape is a goat's pupil,Rectangular,General,Which planet has a moon called Europa,Jupiter +General,Baseball: the st louis ______,Cardinals,General,St Brigit of Ireland could do what amazing trick for visitors,Bathwater into Beer,General,Name the first king to ever make an official visit to communist China?,Juan Carlos +General,"Translate ""january river"" into portuguese",Rio de janeiro,General,Where were the worlds first paved streets,Rome 170 bc,General,Turkish city and port on the Bosporus,Istanbul +Music,Name The 4 Members Of The Travelling Wilburys PFE,"George Harrison, Roy Orbison, Tom Petty, Bob Dylan",General,What type of creature was 'Mang' in Kipling's 'Jungle Book'?,Bat,General,What Scottish city does a Glaswegian call home,Glasgow +General,What is biltong,Dried meat,General,What's the gaelic name for Dublin,Baile atha cliath,Art & Literature,Which Science Fiction Story Centres Around Alien Children In A Village ,The Midwitch Cuckoos  +General,Any of a group of composite organisms made up of a fungus & an alga living in symbiotic association (symbiosis),Lichen,Sports & Leisure,Which Moroccan Athlete Became The first Man To Run 5000 m In Under 13 Minutes ,Said Aouita ,General,Paramount Pictures logo has 22 what,Stars +General,What's the only city today split in two by a wall,Nicosia Cyprus,General,Who recorded left overture in 1976,Kansas,Music,Which duo wrote the musicals Brigadoon and My Fair Lady?,Alan Lerner and Fredric Loewe +General,What battle does the French Legion Celebrate Annually,Camerone - A Defeat,General,Louis the XVI France only two (recorded) what in his lifetime,Baths,People & Places,"In Which State Is Fort Knox, The Location Of The USA's Gold Reserves To Be Found? ",Kentucky  +General,Reuben Tice died trying to invent a machine to do what,Dewrinkle prunes, Geography,In what US state is Panama City?,Florida,General,What actress played mrs margaret williams in the danny thomas show?,Jean hagen +General,What kind of animal is Beatrix Potters Mrs Tiggy Winkle,Hedgehog,Religion & Mythology,Who was Hercules' father?,Zeus,General,George W Trendle and Fran Striker created what Western hero,The Lone Ranger +Tech & Video Games,The Sega Genesis game about two lost aliens looking for their spaceship was called what? ,ToeJam and Earl,Science & Nature,What Can Babies Do That Adults Cannot ,Breathe And Swallow At The Same Time ,General,What is the flower that stands for: ardent love,Balsam +General,Who sailed in a ship called the Argo,Jason,General,Sir Christopher Wren Who Designed St Pauls Cathedral Was A Professor In Which Scientific Field,Astronomy,General,What is mixed with kahlua to make a 'black russian',Vodka + Geography,To what country does the Gaza Strip belong?,Egypt,Music,How Are Messrs Moor & Prater Better Known,Sam & Dave,General,"What famous airborne event occurred May 21, 1927",Lindbergh flight +General,Ben Franklin invented it - Britain tried it in 1916 - What,Daylight saving Time,Science & Nature,What Is V = IR Better Known As ,Ohm's Law ,Science & Nature,"What are Petrol, Naphta, Kerosine, Diesel and Oil ?",Hydrocarbons +General,What was the first credit card,Diners Club,Sports & Leisure,"Which Sporting Milestone Occurred At Iffley Road, Oxford In May 1954? ",Roger Bannister Ran The First 4 Minute Mile ,General,In DC comics Linda Lee Danvers is whose alter ego,Supergirl +General,What country would a Bulgarian with a good sense of direction walk through to reach Armenia by foot,Turkey,General,What was the Troggs most famous hit,Wild Thing,General,Who were believed to celebrate Walpurgis Night on the eve of May Day,Witches +Entertainment,Who is Scooby Doo's nephew??,Scrappy Doo,General,Who is on a u.s. $100 bill?,Benjamin franklin,General,Who pulled the thorn from the lion's paw?,Androcles +Geography,Name the largest city in canada ,Toronto ,General,How many 'tarsal' bones do we have in each foot,Seven,General,The term red herring comes from what activity,Foxhunting +General,Who was the youngest world heavyweight boxing champion,Floyd patterson,Music,Of Which Group Was Suggs A Member,Madness,General,What creatures sex act lasts exactly two seconds,Mosquito +General,The only member of the band zz top without a beard has what last name?,Beard,Music,"Which Singer Recorded The Album ""No Angel"" Which First Charted In 2000",Dido,General,April fool's day came from ________ when the Gregorian calendar was adopted.,France +General,Which Platinum Selling Recording Artist Survived An Assassination Attempt In 1976,Bob Marley,Entertainment,"Who played the lead in the movie ""Erin Brokovich""?",Julia Roberts, History & Holidays,Who directed the 1968 film Rosemarys Baby ,Roman Polanski  + History & Holidays,"Which classic 1946 Christmas film features the line 'Daddy, every-time a bell rings, an angel gets its wings'' ",It's a Wonderful Life ,General,"Who sang 'forever and ever, amen'",Randy travis,General,For what sport is the Camanachd cup contested,Shinty +General,What is a group of this animal called: Owls,Parliament,Art & Literature,What shakespearean play refers to the date of epiphany?,Twelfth Night,Music,With Which European City Is Ultravox Linked Songwise,Vienna +General,What is the name of the Dukes of Hazzards car?,General Lee,General,What does aka stand for,Also known as,General,Someone who dibbles is drinking like what,A Duck +General,The volume of which solid is given by the formula 4/3(pi)r^3?,Sphere,General,What do the Germans now celebrate on October 3rd,Unity day,General,What is the flower that stands for: come down,Jacob's ladder +General,"Who said ""A Single death is a tragedy a million a statistic""",Joseph Stalin,General,French German Italian 3 official languages Switzerland what 4th,Romansch,General,"What company is dubbed ""BUD"" on a stock exchange ticker tape",Anheuser busch anheuser busch +General,What name is given to twins who are joined together by some part of their anatomy,Siamese,General,What lifeboat gets launched from the coast of cornwall,Padstow lifeboat,General,Duran Duran were the 'wild boys' of which year,1984 +Music,"The Lee Dorsey Song ""Working In A Coal Mine"" Was Later Covered By Which Quirky Band",Devo,General,A pigs penis is shaped like what,Corkscrew,General,We know who Darth Vader is but what's Vader mean in Dutch,Father + History & Holidays,"What is Myrrh? Is it a herb, a gum resin or an essence distilled from orchids ",Gum resin , History & Holidays,Who is identified with the word 'eureka'?,Archimedes,General,There must be 15 banked turns on what sporting course,Championship Bobsleigh +Food & Drink,Laetrile is associated with the pit of which fruit ,Apricot ,General,In MASH what was the character Radars full name,Walter O'Reilly,General,Halcyon is a poetic name used for what bird,Kingfisher +General,Thomas Harris created what character,Hannibal Lector,General,A puggle is a baby what,Echidna,General,"Only 10,000 people visited Disney World, Florida during the first year. With time, however, the attendance numbers rose to more than _________ people an hour.",10000 +General,What was known as Arabian Wine,Coffee,General,Who had a best selling single in 1972 with 'Amazing Grace,Royal scots dragoon guards,General,What are falling stars,Meteors +General,What is the capital of north carolina,Raleigh,General,By what is the hudson river spanned,George washington bridge,General,An unfolding of the leg in the air.,Développé +General,"Who claimed that in the garden of eden god spoke swedish, adam spoke danish, and the serpent spoke french",Swedish philologist,General,What's the christian penitential season from end of november to christmas,Advent,General,The sweetener saccharin is made from what,Coal Tar +General,"In which 1945 film starring Lauren Bacall, did Humphrey Bogart play a Martinique bar-owner who was opposing the Nazis",To have and have not,General,Queen Alexandria's is the worlds largest what,Butterfly 1 foot wing, History & Holidays,"What were the B-52's named after? (Hint, it's not a plane) ",Beehive  +General,"Field of engineering & applied physics dealing with the design & application of devices, usually electronic circuits, the operation of what depends on the flow of electrons for the generation, transmission, reception, & storage of information",Electronics,General,This is the fear of enclosed spaces,Claustrophobia, Geography,Lansing is the capital of ______?,Michigan +General,Tracey and Hepburn first film in 1942 was what,Woman of the Year,Music,Which Band Were Saving All Their Kisses In 1976,The Brotherhood Of Man,General,What is the term for precipitation that has been polluted by sulfur dioxide & nitrogen oxides,Acid rain +Science & Nature,How Many Hearts Does An Octopus Have? ,Three Hearts ,General,On which island are the Troodos mountains,Cyprus,General,Which Actor Voices Homer Simpsons Brother “Herb” In The Simpsons ?,Danny De Vito +General,Property of a fluid that tends to prevent it from flowing when subjected to an applied force,Viscosity,General,Humphry Bogart played Rick in Casablanca - Rick Who,Fleming,General,Indiana smoking banned in the legislature building except when,Building is being used +General,The nest of an eagle or bird of prey is an,Eyrie,General,Basketball: the new york _________,Knickerbockers,General,"Which word comes from the Roman ""where three roads meet"" as a place where messages were left",Trivia +General,Filo pastry stuffed with chopped nuts and honey,Baklava,General,Name surveyors symbol that looks like a broad arrow with a bar,Benchmark,General,There are five stars on the flag of which country,Peoples republic of China +Art & Literature,"In 'Romeo and Juliet', about what was Mercutio's long monologue?",Queen Mab,General,Which American State Drinks The Most Alcohol Per Person,Nevada,Science & Nature,"Cocci, Spirilla, and Streptococci are types of ________.",Bacteria +General,What is the flower that stands for: call me not beautiful,Unique rose,Entertainment,"What does the Italian term ""poco a poco"" mean?",Little by little,General,Capital cities: Jamaica,Kingston +General,In what country is the town of Liege,Belgium,General,Sailors lacking vitamin c would contract which disease,Scurvy,Music,"In Which Film Did Doris Day Sing The Oscar Winning Song ""Secret Love""",Calamity Jane +General,In which film was the best supporting actor Oscar won in 1975,The Sunshine Boys George Burns,Geography,Bridgetown is the capital of ________.,Barbados,General,What kind of biscuits were named after Australian & New Zealand soldiers,Anzac biscuits +General,34% of Californian Male students 10% of Female lied to get what,Sexual Partner,People & Places,Which saint was known as the apostles of Northumbria? ,St Aidan ,General,Where are the 'wallops',Hampshire +Science & Nature,A scientist who studies reptiles and amphibians is known as a:,Herpetologist,Geography,What is the capital of Sierra Leone,Freetown,General,Who betrayed king arthur by sleeping with his wife guinevere,Sir lancelot +General,International Phonetice Alphabet: N,November,Science & Nature, The penculine __________ of Africa builds its home in such a sturdy manner that Masai tribesman use their nests for purses and carrying cases.,Titmouse,Science & Nature,Who Invented Tthe Aqua Lung ,Jacques Cousteau & (Emil Gagnan)  +General,"Two of the permanent residents of Fawlty Towers were old ladies. One is Miss Tibbs, what is the name of the other",Miss gatsby,General,What is the Capital of: Croatia,Zagreb,Music,What Couldn't REO Speedwagon Fight,This Feeling +General,If silver is stamped with a leopard in which city was it assayed,London,General,What is the flower that stands for: relieve my anxiety,Christmas rose,General,Which Descriptive Term Is Applied To “Force 11” On The Beaufort Scale,Violent Storm +General,Who invented Coca Cola,Dr john pemberton,General,Who discovered oxygen,Joseph priestley,General,His ship was the H.M.S. Beagle,Charles darwin +General,"Henry the Eighth was the father of two English Queens, Mary I and Elizabeth I. Which other King was the father did the same",James the second,General,What is the fear of philosophy known as,Philosophobia,General,In Florida its illegal for a housewife to do what more 3 times daily,Break a dish +General,What model of automobile is known for its water-tight characteristics,Volkswagen beetle,General,What countries dialects has varieties called Twi and Fanti,Ghana,Music,"Who Claimed He Was ""An Innocent Man""",Billy Joel +General, Bibliophobia is a fear of __________.,Books,Music,Written By Billy Strayhorn What Was The Signature Tune Of The Duke Ellington Band,"Take The ""A"" Train",General,What is a peanut if it is not a pea or a nut,Legume +General,"In the movie snow white, what instrument did sneezy play",Accordian,General,"In Greek mythology, what is the alternate name for pollux",Polydeuces,Art & Literature,Eliza Doolittle Appears In Which Play ,Pygmalion/My Fair Lady  +Science & Nature,As what is the North Star also known?,Polaris,General,Which plant of the daisy family provides a medicine used for the treatment of bruises,Arnica,General,What is the national flower of Spain?,Carnation (Red) +General,Who was the dictator of Uganda from 1971 to 1979,Idi amin,Science & Nature," An adult lion's roar can be heard up to five miles away, and warns off intruders or reunites scattered members of the __________",Pride,General,How many bonus points in Scrabble if all seven tiles played at once,Fifty 50 +General,The ionian and cyclades are island groups of which country,Greece,General,What type of level is used for measuring the angle of a slope,Abney,General,In Gustav Holsts Planets suite which planet represents old age,Saturn +Music,The Scottish Band The Soup Dragons Got Their Name From Which Childrens TV Show,The Clangers,General,What is the proper name for a moving stairway,Escalator,General,What animal can hop as fast as 40 mph,Kangaroo +General,What is the hobby of a 'twitcher',Bird watching,General,In the song Waltzing Matilda - What is a Jumbuck,Sheep,General,What U.S. state is the golden state,California +General,Jack Haley played the Tin Man what was the Tin Mans name,Hickory Twicker,General,Which Puccini opera featured Nessun Dorma,Turendot,General,"What part of a face, according to research, do infants like most to look at",Eyes +General,"In dentistry, what are ""angle irons"" and 'rolex'",Braces,General,Canada is an Indian word meaning what,Big Village,General,Who had the UK Christmas NO 1 single in 1995 with Earth Song,Michael jackson +General,"To ""testify"" was based on men in the Roman court swearing to a statement made by swearing on what",Their testicles,General,60% of women say they have eleven a day - 11 what,Emails,Music,"In The Beatles Song ""I Am The Walrus"", who were they kicking?",Edgar Allen Poe +General,What legendary character is rooted in U.S. pioneer John Chapman,Johnny appleseed,General,What's missing from ale that’s included in beer,Hops,General,What holiday does Japan celebrate on December 23?,Birthday of the Emperor + History & Holidays,"""Which former star of 'Crossroads'' Played """"Meg Richardson"""" And was born on 25th December,1923"" ",Noel Gordon ,General,"Which American state was the first, in 1780, to abolish slavery",Pennsylvania,Geography,Which state has Cape Hatteras,North carolina +General,Which Famous Artists Designed The Chupa Chups Logo In 1969,Salvador Dahli,General,What animal appears on an australian 5 cent coin?,Echidna,General,Operation Market Garden WW2 involved the invasion of where,Arnham +Science & Nature,How Many Ribs Do You Have ,24 Ribs ,General,What police resource was first used in the Jack the Ripper case,Bloodhounds,General,Who was the pilot in the first fatal air crash,Orville Wright +Sports & Leisure,What is the only English football team without a vowel in the first five letters of it`s name? ,Crystal Palace ,General,This frontiersman & politician was killed at the Alamo,Davey crockett,General,Italy's equivalant to the dollar is the ______?,Lira +General,What do ladybugs do in the winter,Hibernate,Science & Nature,What Is The Name For A Baby Hare? ,A Leveret ,General,"What means of transport was invented in Marin County, California, in the 1970s",Mountain bike +General,What 3 flags does the Union Jack comprise of?,England Scotland Ireland,Art & Literature,Who Wrote The Novel 'Silence Of The Lambs''? ,Thomas Harris ,General,Which toilet fittings name comes from baggage laden pony,Bidet +General,What is the official sport of Maryland,Jousting,General,Lucille LeSueur was the real name of which famous actress?,Joan Crawford,General,What other name is Mellor’s famously known by,Lady Chatterlys Lover +Science & Nature," The __________ has only two toes, unlike most birds, which have three or four.",Ostrich, History & Holidays,What Is Bob Dylan's Real Name ,Robert Zimmerman ,General,What shape were the sailors plates in Nelsons navy,Square Thus Square Meal +General,What kind of book did dr seuss pen five of ten best-sellers in u.s history by 1994,Children's books,General,What is alfred e neuman's motto,"Whta, me worry",Music,"Complete the Lyrics To The Following “I am the eggman, they are the Eggmen, I am… (2 Words)",The Walrus +Science & Nature,What is the name for a group of stars,Constellation,General,Who made the first solo transatlantic flight in 1927,Charles lindbergh,Geography,Which famous country park surrounds the village of Edwinstowe in Nottinghamsire? ,Sherwood Forest  +General,What does MTA stand for among Frisbee freaks,Maximum time aloft,Music,The Pop Duo The Carpenters Had A Lot Of Hits In The 70's But What Are Their Actual First Names PFE?,Karen & Richard,General,Who is the only South African driver to have won the World Formula One Championship?,Jody Scheckter +General,The word 'batrachian' describes which animals,Frogs & toads,General,"On Night Court,Harry had a ""statue"" of what animal in his office?",Armidillo,General,Who was the first man to win the 1500m swimming back to back at 2 olympics,Kieren perkins +General,Who sang the title song to 'goldfinger',Shirley bassey,Science & Nature,Name the most venomous spider.,Black widow,Technology & Video Games,"In Dead or Alive 2, Hayabusa informs you that ""If your soul is imperfect,"" what? ",Living will be difficult +General,"In popular culture, what is a 'Tamagotchi'",Virtual pet,General,Where did the word 'biscuit' originate,France,Sports & Leisure,What Is The Pitch Called In American Football ,The Grid Iron  +General,What asian country is bordered by the soviet union and china,Mongolia,General,Film title 'the last days of ______',Pompeii,General,"Where did Ronald and Cindy go on there last date together in the movie ""Can't Buy Me Love""?",Airplane junkyard +General,Who is known as big mama in ladies pro golf,Joanne carner,General,Medina is located in what country,Saudi arabia,General,"The word ""cumulus"" refers to a type of ___________.",Cloud +General,Only female humans and what have hymens,Horses, History & Holidays,Who Assassinated Robert Kennedy? ,Sirhan Bishara Sirhan ,General,Who was the first person inducted into the u.s. swimming hall of fame,Johnny +General,In which country was the Pahlavi family a ruling dynasty,Iran,Food & Drink,What Country Does Bacardi Originate ,Cuba ,General,"These are words in different languages that have the same original source, eg: ""water"" (english) and ""wasser"" (german)?",Cognates +General,What do tendons join to bones,Muscles,General,What is a coho a type of,Fish,Science & Nature,Water freezes at _ degrees Celcius.,0 +Music,Which Chicago Club Gave House Music It's Name,The Warehouse Club,General,Merrylegs was a performing circus dog in which Dickens novel,Hard Times,General,Whats the largest library in the world,The library of congress +General,1938 marked the introduction of this Volkswagen car.,Beetle,Music,"Whose 1996 Debut Album Was Entitled ""First Band On The Moon""",The Cardigans,General,This is the sandy area nearest the ocean.,Beach + Geography,What is the basic unit of currency for Brunei ?,Dollar,Food & Drink,What is France's oldest type of brandy? ,Armagnac ,General,Mr Spocks blood was green - but what group,T - Negative +General,Which vegetable is used if a dish is described as 'a la Crecy',Carrots,General,"Of indian affairs Branch of mathematics concerned with the study of such concepts as the rate of change of one variable quantity with respect to another, the slope of a curve at a prescribed point, and the computation of the maximum and minimum values of functions",Calculus,Entertainment,Actor: __________ Nimoy.,Leonard +General,What kind of hat is depicted on lee trevino's golf cap,A sombrero,General,What are young rats called,Pups,General,A non_cancerous tumor is said to be _______.,Benign +Sports & Leisure,Who Made The First 147 Break At The Snooker World Championships ,Cliff Thorburn ,Science & Nature,Which Shiny Insect Feeds On Such Materials As Wallpaper Paste And Bookbindings ,The Silverfish ,General,Miso a traditional Japanese cooking ingredient is what,Soybean Paste +Science & Nature, The mouse is the most common mammal in the __________,United states,General,Two out of 3 visits to an American doctor are for what problems,Stress Related,General,What country excludes women from the graveside rituals,China +General,What does a 'postman' normally receive in kids' party games,Kisses,General,According to the saying where does charity begin,At home,General,What colours are on the Belgian flag?,"Yellow, black and red" +Music,John's first published book was called:,In His Own Write,People & Places,Whom did Mervyn King replace as Governer of the Bank of England in 2003? ,Eddie George ,General,Which Australian city is named after William IV's Queen,Adelaide +General,"In the tv series 'the dukes of hazard', what was painted on the top of their car",Confederate flag,General,Fescue is a generic type of what,Grass,Geography,Native American Indians are of which human subrace? (e.g. Caucasian),Mongoloid +Science & Nature, A South African __________ can grow to be 35 inches (90 cm) in length _ longer than your arm.,Bullfrog,General,In which country is the city of Mandalay,Burma,Music,On Which Shakespeare Play Was Kiss Me Kate Based,The Taming Of The Shrew +General,What is the capital of Montenegro?,Podgorica,General,Rathbone what was tarzan's true identity,Lord greystoke,General,Collective nouns - which creatures are a clamour or building,Rooks in a rookery +Science & Nature,"What are these: Ceres, Juno, Iris, and Flora",Asteroid,General,Murphy's Oil soap is most often used to clean what,Elephants,General,Although He Never Set Foot Here Which Elvis Presley Movie Is Actually Set In England,Double Trouble +General,What is divided into 114 surahs,The Koran - Surah = chapter,General,What does roulette literally mean,Little Wheel,Entertainment,Which large tuned orchestral drum is also known as a kettledrum?,Tympani +General,What country has the biggest population,China,General,"Which book was the sequel to ""Little Women""",Good wives,General,"Japheth, Shem and Ham were sons of which biblical man",Noah +General,Port Said lies on which waterway,The suez canal,Music,What Was Unusual About The American Top 100 Singles Charts In June 1983,It Contained More Records By Foreigners Than Americans For The First Time,Entertainment,"In late 1957, Buddy Holly's solo release 'Peggy Sue' challenged which song recorded with The Crickets?",Oh Boy +General,"Optiphone, Lustreer and Mirascope early names for which item",Television,General,According to Samuel Johnson what is the drink for heroes,Brandy,General,The raised reflective dots in the middle of highways are called what,Botz dots +General,The Mbuti tribe in Africa are the worlds what,Shortest race,Entertainment,What actress's real name was Frances Gumm?,Judy Garland,General,What colour would one associate with the Spanish volunteer force on the eastern front in the WWII,Blue +General,What does a botanist study,Plants,General,What is the 30th longest river in the world ?,The Thames,General,In which state of the U.S.A. would you find Mount Rushmore,South dakota +Sports & Leisure,Which crew sank in the 1978 boat race? ,Cambridge ,Geography,The Arctic ocean is the smallest and _____________,Shallowest, History & Holidays,"""What Did My True Love Give To Me On The """"Eighth"""" Day Of Christmas"" ",8 Maids A Milking  +General,1979 at Clifton suspension bridge Britain's first what happened,Bungee Jump,General,Zimbabwe won its first ever Olympic gold in 1980 in what event,Women's Hockey,Music,Route 66 Composer Bobby Troup Was Married To Which Sultry Singer,Julie London +General,What in Arthurian legend was the Siege Perilous,Empty Chair for grail finder,General,In nautical terms what five letter word means duty at the helm,Trick,General,On which scale are there 180 degrees between freezing point and boiling point,Fahrenheit scale +Food & Drink,How would you say 'house wine' in 'French' ,Vin (de la) maison ,General,"Which character in Bond films has been played by Donald Pleasance, Telly Savalas, and Charles Gray",Blofeld, History & Holidays,Henry The 8 th Had A Bit Of Bad Rep When It Comes To His Wives But He Only Executed 2 (PFE) ,"Anne Boleyn, Katheryn Howard " +General,"The cocktail ""Margarita"" contains cointreau, lime and which spirit",Tequila,General,What does f.b.i. stand for,Federal bureau of investigation,General,Give a year in the life of the painter Frans Hals.,1580-1666 +General,"Which Very Successful Pop Duo Consist Of ""Roland Orzabal & Curt Smith""",Tears For Fears,General,What was the name of george of the jungle's ape friend,Ape,Science & Nature, There is just one known species of __________ in the world _ it is in the order of Struthioniformes.,Ostrich +Entertainment,"What hardcore rock group sings, 'Blind' and 'Clown'?",Korn,General,What compound might you expect to find in diet soft drinks,Aspartame,General,Which Female Minister Of Transport Introduced The Breathalyzer?,Barbara Castle +General,What are the only canines whose hair has a hook (or barb) on each individual follicle,Dalmatians,General,Idi Amin of Uganda excelled at what sport,Rugby, Geography,Which country is the smallest population ?,Vatican City +General,U.S. Captials - Missouri,Jefferson City,General,The wingspan of a ______ ___ jet is longer than the Wright Brothers' first flight,Boeing 747,Food & Drink,What is a lift for food in a restaurant known as? ,A dumb waiter  +General,"Which TV Personality Had A Cameo Role In The Movie ""Bedknobs & Broomsticks""",Bruce Forsythe,General,What's the name of the large prominent vein in the side of your neck,Jugular vein jugular,General,Who was Abraham Lincoln's first choice to lead the Union Army,Robert e lee robert e. lee +General,Who did Nathuram Godsay Murder in 1948?,Mahatma Gandhi,Music,Which sixties pop group were the first to have 3 No.1 UK hits with their first 3 singles?,Gerry And The Pacemakers,General,What is used in the game of Bettle Drive?,Dice +Music,What Numeric Link Did Bros Have In Common With Tears For Fears,Each Group Had 2 Members (Twins),General,Michigan State University is located in what city,East lansing, Geography,Which river produces the most sediment?,Yellow River +General,What Was Walt Disney's Middle Name,Elias,General,Shellac dissolved in alcohol makes what type of varnish,French Polish,People & Places,Who is known as 'The Big Yin'? ,Billy Connolly  +General,What does a philatelist collect,Stamps,Sports & Leisure,At Which Event Has Steve Backley Won Olympic Bronze & Silver Medals ,Javelin ,Science & Nature,What Is Spains Largest Port ,Barcelona  +General,The name of this animal translates as ghost what is it,The Lemur,General,The most northerly point of mainland Africa is in which country,Tunisia,General,What name is given to the tidal wave which travels up to 800km upstream on the Amazon with waves up to 4 metres high?,Pororoca +General,"What was the name of the ""fake"" evolutionary missing link found in Sussex, England ?",Piltdown Man,Entertainment,What expression did Clark Kent's newspaper boss like to use?,Great Caesar's ghost!,General,U.S. captials Colorado,Denver +General,What countries national anthem is The Patriotic Song,Russia,General,What sport is often called 'ping pong',Table tennis,General,Which countries men use the most deodorant,Japan +General,Who wrote the 'noddy' books,Enid blyton,General,Who played Judge Roy Bean on film in 1972,Paul newman,General,In Huston its illegal to sell what on Sunday,Limburger Cheese +General,What's the Malayan sun bear's main claim to fame,Smallest bear,Science & Nature,How Many Vertebrae Does The Average Human Possess ,33 Vertibrae ,General,Which is the last of the year's four quarter days,Christmas day +Science & Nature,As Their Names Would Suggest Where Were The Mamenchisaurus & The Tuojiangosaurus Found ,China ,General,What was 1993s biggest selling single,Ill do anything for love,General,What can withstand hundreds of bee stings,Honey badger +Sports & Leisure,Baseball: The St. Louis ______?,Cardinals,Science & Nature,How many tons of gem diamonds are mined every year ,Two ,Music,Who Was the First African American Group To Reach No.1 In the Singles Charts,The Platters +General,What name is given to a year with 366 days,Leap, History & Holidays,"In 'Sixteen Candles', what did the geek need to get to prove he had sex (which he got from Molly Ringwald)? ",Underpants ,General,"Who was the author of ""The Moor's Last Sigh""",Salman rushdie +General,India has the largest Hindu population what country has second,Nepal,General,Which world-wide organisation was founded in 1865 as the Christian Mission,Salvation army,General,What kind of craft was the mast atop the Empire State Building intended to moor,Dirigibles dirigible +General,Parasitophobia is the fear of,Parasites,General,Who won the first Oscar for a musical in 1943,James Cagney,Music,"Who Sang The Hit ""Take My Breath Away"" From the Movie Top Gun",Berlin +General,Which city's airport is the home base for Cathay Pacific Airlines,Hong kong,General,Name the nursery rhyme mother whos last name is that of a bird?,Goose,Geography,Frankfort is the capital of which state ,Kentucky  +General,What is the only n.y.c borough that is not on an island,Bronx,General,Which US sportsman is mentioned in The Old Man and the Sea,Joe De Maggio,General,How many feet tall is London's monument to the Great Fire known simply as 'The Monument'?,202 Feet +General,What does Israel call its parliament?,Knesset,Music,Who wrote a book titled The Adventures of Lord Iffy Boatrace?,Bruce Dickinson,Music,Who Was The Lead Singer Of The Group TA-PAU,Carol Decker + History & Holidays,How Many Colonies Signed The American Declaration Of Independence? ,13 Colonies ,General,Hazel was the maid for what family,Baxters, History & Holidays,Who Released The 70's Album Entitled Tea for the Tillerman ,Cat Stevens  +General,Yvon Petra 1946 was the last Wimbledon champion to do what,Wear Trousers,Music,Father Abraham Was The Guiding Light For Which Hit Making Figures,The Smurfs,General,In Which European Countrv Will You Find The Headquarters Of Greenpeace,Amsterdam +Toys & Games,"In pool, what color is the eight ball?",Black,General,Archaeopteryx was the first what,Bird,General,What is the Maths equivalent of the Nobel prize ?,Fields Medal +General,In Palding Ohio police officers can legally do what to dogs,Bite them to quiet,General,What two countries were known as 'The Yellow Peril' in then 1890's?,China and Japan,General,Every day 2700 Americans find out what,They have gonorrhoea +General,What is another name for a tomato,Love apple,General,"Who released 'she's so unusual' in 1984, and won a grammy for best new artist",Cyndi lauper,General,What job Michael Cane Uma Thurman Sidney Poitier common,Dish washers +General,"The French Physician ""Laennec"" Was Responsible For Inventing Which Medical Aid",Stethoscope,Sports & Leisure,What are large snow bumps known as in skiing terms?,Moguls,General,"What Ray Charles song pines: ""I'll just live my life in dreams of yesterday""",I can't stop loving you +General,What name is given to the control column of an aircraft,Joystick,General,Small pieces of bread or crackers with a savoury topping are called what,Canapes,General,The architect Edwin Lutyens is best known for his plans for which capital city,New delhi +General,"In ballet, the position of the torso from the waist up.",Épaulement,General,What do English speakers call the city that ltalians call Torino,Turin,General,What 1942 movie did the song white christmas come from,Holiday inn +Food & Drink,What Is Albumen The Correct Term For ,Egg White ,General,The agave cactus is the source of which liquor?,Tequila,Science & Nature,What muscles move the ears?,Auricularis +General,His campaign slogan was vote for AuH20 whats his name,Barry Goldwater,General,What was the real name of Boris Karloff,William henry pratt,General,What play has line - Shall there be no more cakes and ale,Twelfth Night +General,Give the name of Elizabeth Barrett Browning's dog,Flush, Geography,What is known as the graveyard of the Atlantic ?,Sable Island,Art & Literature,What was the name of Mother Goose's son,Jack +General,What does the Campbell-Stokes recorder measure,Duration of sunshine,General,In Paris what has been defined as a deadly weapon,Ashtray,General,What Gilbert & Sullivan operetta was subtitled Bunthorns Bride,Patience +General,"How Are The Musical Duo Of ""Martha Wash & Izora Armstead"" More Commonly Known",The Weathergirls,Sports & Leisure,Who Did Muhammed Ali Fight In 'The Rumble In The Jungle''? ,George Foreman ,General,Who was born Anne Frances Robbins?,Nancy Davis Reagan +General,"""Poppy Field"" and ""Water Lilies"" were painted by which artist",Claude monet, Geography,What is the basic unit of currency for Bolivia ?,Boliviano,General,In which Chinese city is the tomb of Sun Yat Sen,Nanking +General,What does israel call its parliament,Knesset,General,What zodiacal sign is represented by fish,Pisces,General,The average automatic dishwasher uses how many litres of water per wash cycle,68 +General,Tests on drugs and poisons determine the LD-50 dose. What does LD stand for,Lethal dose,Food & Drink,Which tuber is sometimes called the 'Black Diamond' ? ,Truffles ,General,What is the technical term for the bones in the fingers and toes,Phalanges +Music,Who Is The Youngest Member Of Boyzone,Ronan Keating,Entertainment,"Who played the lead in the movie ""The Matrix""?",Keanu Reeves,General,What did Mege-Mouries invent in 1870 winning a Napoleon prize,Margarine +General,What Country-Pop artist was once a cheerleader alongside classmate Brenda Lee at Maplewood High School in Nashville,Rita coolidge,General,Roman orator Marcus Tillius nicknamed what for wart on nose,Cicero,General," A flat, round hat sometimes worn by soldiers is a _________.",Beret +General,"Whose last words were - ""That was the best soda I ever tasted""",Lou Costello,General,Who launched a Cultural Revolution in April 1966,Mao tse-tung,Music,"""When I Need You"" Was A No.1 Hit For Which Singer",Leo Sayer +Food & Drink,What did Martin Stone invent in 1888 that millions of suckers use every day? ,The Drinking Straw ,Music,Who covered the Diana Ross & The Supremes song “You Can't Hurry Love” in 1983,Phil Collins,General,How many times have the olympic games been cancelled due to war,Three +General,The name of what product - German water Greek olive Oil,Vaseline Wasser Elaion, History & Holidays,Who sang Happy Birthday to John F. Kennedy for his 45th,Marilyn monroe,Geography,Hanoi Is The Capital Of Which Country ,Cuba  +General,Which instrument did Paganini play,Violin,General,Old Dominion was a nickname of which US state,Virginia,General,What Shakespeare King was killed at Pontefract Castle,King Richard III +General,Which film actor is known as 'The muscles from Brussels',Jean claude van damme,General,In 1900 Persian soldiers were paid with what,Donkeys,General,Which country lies between Tunisia and Egypt,Libya +General,At the Festival of the Cleaver Spartans nailed what to the wall,Sausages for older men to gnaw,Food & Drink,The Marine Snail Is Better Known As What ,Whelk ,General,A group of deer is called a,Herd +General,The word bungalow comes from which language,Hindi,General,In which country was cricketer Ted Dexter born?,Italy,General,A husband and wife won gold medals 1952 Olympics who,Emile Dana Zatopek marathon javelin +Science & Nature,"Which Variety Of Pine Tree, Alive Today, Was A Sapling During The Life Of Christ/ ",The Bristlecone Pine ,General,Who is the Patron Saint of Gout,Saint Andrew,General,What 1970's tv series gave larry hagman his start,I dream of genie +Sports & Leisure,"In which sport is the term, ""Hang ten"" used?",Surfing,Science & Nature," Ergonomic waterbeds are the latest must_have on the bovine circuit. The beds, listed at $175 each, are said to enhance cattle health by reducing __________",Joint damage,Sports & Leisure,In Which Game Might You Castle ,Chess  +General,What does the computer acronym IKBS stand for,Intelligent knowledge based system,General,What is the largest desert in the world,Sahara,Science & Nature," A __________ weighing 120 pounds exerts a force of about 1,540 pounds between its jaws. A human being's jaws exert a force of only 40 to 80 pounds.",Crocodile +General,Isms: Exalting one's country above all others,Nationalism,General,Thomas Jefferson's home has a hidden what? illegal in his time,Billiard Table in Virginia,Geography,What is the capital of Azerbaijan,Baku + History & Holidays,What was Alexander The Great's wife's name?,Roxana,General,Which country contains every type of climate in the world,New Zealand,General,"What links Humphry Davie, Michael Faraday, Madam Curie",Poisoned by chemicals work + Language,What ONE word fits? ____stream; ____hill; _____pour.,Down,General,What US President said I Promise instead of I swear inaugurate,Richard M Nixon,General,Which Group Used To Be Called The Primettes?,The Supremes +General,Strine is the vernacular language spoken in which country,Australia,General,How many laps make up the Indianapolis 500,200, History & Holidays,"What was the nickname of Charlene Tilton, who played Lucy Ewing in Dallas? ",Poison Dwarf  +General,At which weight was boxer Freddie Mills World Champion,Light heavyweight,General,"The Greek Words for Beautiful, Appearance And I Behold Converge to give us which word",Kaleidoscope, History & Holidays,What was the pen name of author Charles Lutwidge Dodgson ,Lewis Carrol  +General,What was the only horror film nominated best film Oscar,The Exorcist in 1973,Entertainment,What did TVs IMF stand for?,Impossible Mission Forces,Geography,What river is known as china's sorrow because of its flooding ,The yellow river  +Food & Drink,Rioja Wine Comes From Which Country ,Spain ,General,Which Vatican building was built for Pope Sixtus lV,The sistine chapel,General,What is the name of the Buckinghamshire estate where the German Enigma codes were cracked during World War 2,Bletchley park +Music,Instrumental Band B Bumble & The Stingers Had A No.1 Hit In 1960 With A Single Based On A Tchaikovsky Tune What Was The Song,Nut Rocker,General,Name the first actor to get best actor Oscar for repeating a role,Paul Newman – Colour of Money,General,Which male Christian name derives from the Gaelic word for 'handsome',Kenneth +General,What is the Capital of: Belgium,Brussels,General,In UK tennis where is the Stella Artois tournament held,Queens,General,"What is the general name for a protein molecule which acts as a natural catalyst in the bodies of all bacteria, plants and animals",Enzyme +General,What actor began his career doing Doctor Pepper commercials,Bruce Willis,General,What nationality was Saint Paul of Tarsus,Turkish,Science & Nature,"Cows Come In Herds, What Do Geese Come In? ",Gaggles or Skeins  +General,"Which female singer collaborated with Eminem with the song ""Stan""?",Dido,General,What is the Capital of: Ecuador,Quito,General,What is the Capital of: Central African Republic,Bangui +Food & Drink,What would you buy from a Bodega? ,Wine ,Art & Literature,How many lines are in a sonnet,14, Geography,The island of Hispaniola consists of the Dominican Republic and this country.,Haiti +General,Venation is used to describe what item,Leaves,General,Where is the Lubianka prison located?,Moscow,General,Collective nouns - A Down or Husk of what animals,Hares +Sports & Leisure,Who Scored England's Last Minute Winning Goal Against Belgium In The 1990 Football World Cup? ,David Platt ,Music,"Which British Blues Rock Trio released The Albums ""Undead"" & ""Stonedhenge""",Ten Years After,Geography,In which city is Wembley Stadium,London +General,In which USA state is Frankfort the capital,Kentucky,General,In what country are the Painted Lakes,Indonesia,Entertainment,Where does George Jetson work?,Spacely Sprockets +General,What US State flag has a UK Union Flag on it,Hawaii,General,In what Italian town can you find the Piazza del Erbe,Verona,Geography,Where Is Cape Horn ,Bottom Of South America  +General,What does amd stand for,Advanced micro devices,General,What part of the body ages the fastest,The Hands,General,"Creek what position has been held by 266 men, 33 of whom have died violently",Pope +Science & Nature," Because baby pigs grow so quickly, a succession of 48 little pigs were used in the title role during the filming of the 1995 movie hit __________",Babe,General,"A solemn court dance usually in duple time, popular in the fifteenth and sixteenth centuries.",Basse danse,General,What measures blood pressure,Sphygmomanometer +General,What is the name of the disease which is considered the human form of 'Mad Cow's Disease'?,Creutzfeldt -Jakobs Disease,Science & Nature,The sulphate of which metal is used to render the alimentary canal opaque to X-rays (symbol Ba)?,Barium, Geography,What is the basic unit of currency for Botswana ?,Pula +General,Which novel by Louis de Bernieres is set in Cephalonia,Captain corelli's mandolin,Toys & Games,What is another name for the card game 'Blackjack'?,21,Sports & Leisure,Who Was Disqualified After Winning The Men's 100m At The 1988 Olympic Games ,Ben Johnson  +General,For how much did an American urologist buy Napoleon's penis? (US Dollars),"$3,800 ",General,What aid to archaeologists from 197 bc was found in Egypt 1799,Rosetta Stone,General,What is a Digamy?,A Second Legal Marriage +General,John Ruskin - Art Critic - Marriage collapsed when wife had what,Pubic Hair,General,What was the name of James Bonds housekeeper,May,General,"Who was the last British general who surrendered near Jamestown, Virginia",Cornwallis +General,Which gas has the characteristic smell of rotten eggs,Hydrogen sulphide,Music,In 2005 who sang a duet with ex westlife member Brian Mcfaddden?,Delta Goodrum,Music,What Was The Two-Word Title Of The England World Cup Team's 1970's Official Song?,Back Home +General,Arturo Toscanini played what instrument before conducting,The Cello, History & Holidays,Who Was The British Prime Minister Between 1964 & 1969 ,Harold Wilson ,General,What traditional Maori insult was seen in the film Braveheart,Mooning +General,Marion Barry - Mayor of Washington arrested for what,Possession of Crack,General,Who played deanna troi in 'star trek the next generation',Marina sirtis,General,Where would you find the Sea of Tranquillity,On the moon +General,"What common item has a coil, point, sheath and two shafts",Safety Pin,General,What famous battle was fought at Pancenoit,Waterloo - (four miles away),General,Narrow trench made by a plough,Furrow +General,Who was the oldest man ever to play Test cricket when he represented England against West Indies in 1930 aged 52,Wilfred rhodes,General,When did prohibition in the U.S. come to an end,1933, History & Holidays,Who banned Christmas in England between 1647 and 1660? ,Oliver Cromwell  +General,What season should you head to the pond to look for tadpoles,Spring,Science & Nature,What is the heaviest naturally occuring element?,Uranium,Food & Drink,How or when can one eat angel hair or bridegrooms? ,When eating pasta  +Entertainment,What city do Batman and Robin patrol?,Gotham City,Entertainment,Who is Donald Duck's uncle?,Scrooge,General,Who sang Come on Eileen,Dexies Midnight Runners + Geography,What is the basic unit of currency for Afghanistan ?,Afghani,Science & Nature,Do Butterflies Usually Rest With Their Wings Together Or Open ,Together ,General,Where is the largest volcano,Ecuador +Science & Nature,What is the small irregular white cloud that zips around Neptune approximately every 16 hours called?,Scooter,Music,"""Dancing On The Ceiling"" Was A Hit For You",Lionel Richie,General,What is new york city's 'street of forgotten men',Bowery +People & Places,Which European City Is Famous For Its Latin Quarter ,Paris ,Science & Nature,Doctors often have this instrument around their neck.,Stethoscope,Music,"Who In The World Of Music Has The Real Name ""Marshal Mathers""",Eminem +General,What flag flies over gibraltar,Union jack,Music,"Whose 'Variations On a Theme by Paganini"" is a standard part of the piano repertoire?",Rachmaninoff, Geography,San Francisco Bay is located near what city?,San Francisco +General,Which famous author also writes under the name pen name of Richard Bachman?,Steven King,Music,And What War Was His 1969 Hit 2 Little Boys About,The American Civil War,General,"Thor Heyerdahl vessels were Kon Tiki, Ra and what",Tigris +Sports & Leisure,Who Won The First University Boat Race Between Oxford And Cambridge? ,Oxford ,General,Janet Jackson had a starring role in which TV series,Fame,General,Process by what an organism becomes better adapted to exist in an environment different from the one to what it was indigenous,Acclimatization +General,"Which French artist painted 'The Bridge at Argenteuil', and 'The Magpie'",Claude monet, History & Holidays,In which country did this practice of 'Trick Or Treating' originate ,"The United Kingdom, surprisingly ",General,Who was the first Plantagenet monarch of England 1154 to 89,Henry II +General,Which Old Testament King was succeeded by his son Rehoboam,Solomon,General,How many points does a backgammon board have,24,General,What does a craftsman called a bodger make,Chair parts +General,What sequence is this the start of: 2 4 6 8 10 12 14,Even numbers,General,Who starred in the film Sixth Sense,Bruce Willis,Science & Nature,Which Metal is Also Known As Quicksilver? ,Mercury  +General,What form modern sculpture invented Calder named Dechamp,The Mobile,General,Who was acquired from the Daisy Hill Puppy Farm,Snoopy,General,If you were watching cricket at The Outer in which city are you,Melbourne Australia + History & Holidays,"In what country did ""Sepoy Mutiny"" occur?",India,General,"What name was given to the 17 1h century chair with shafts, borne by two servants",Sedan chair,General,Humans lose 27 what a day,Moulted Pubic Hairs +General,In Baltimore it is illegal to scrub or wash what,A Sink,General,In Tremonton Utah illegal for a woman not man what in ambulance,Have sex,General,What is the most popular participation sport in Britain,Darts +General,Lilongwe is the capital of ______,Malawi,General,What is the name of Oprah Winfrey's production company,Harpo,General,A bilateral chips pact between the U.S. and this country expires July/1996,Japan +General,What is the service ceiling of an f-14a tomcat,68900 feet,General,Which Italian composer's funeral in 1924 reputedly 'brought Italy to a standstill,Puccini,General,In what country are the ports Oran and Bone,Algeria +Music,What Was Ray Stevens 1974 Hit Based On The Phenomenon That Was Occuring At Sports Events Of The Time,The Streak,Music,What Was The Best Selling Album Of The 1980's In The UK,Dire Straits / Brothers In Arms,General,What was sharkskin once used as,Sandpaper +General,In 1895 the world's first disposable item made - what was it,Razor Blade – King Camp Gillette,General,48 Extra's Died That Appeared In What Oscar Making Movie Within A Year Of It's Release Name The Film,Babe,General,In Which English County Are The Clee Hills,Shropshire +Geography,What Is The Scirocco ,A Wind ,Art & Literature,"A composition made of cut and pasted pieces of materials, sometimes with images added by the artist. ",Collage,Toys & Games,"Originally an exercise ring, Indonesia banned it as ""might stimulate passion"".",Hula hoop +General,Which famous artist lost an ear,Vincent van goch,General,What is a group of crows,Horde,General,Which country is the world's largest producer and exporter of Cobalt?,Zaire +General,What mountain range is in New York state,Adirondack mountains,General,Which game is played on an oval with 18 player per team,Australian football,General,Where is selfridges,"Oxford street, london" +General,"In Greek mythology, who was paris' wife before he was smitten by helen",Oenone,General,What is the fear of writing in public known as,Scriptophobia, Geography,What does the George Washington Bridge span?,Hudson River +Music,"Who Wrote And Performed ""Informer""",Snow, History & Holidays,In What Year Did World War I End ,1918 ,General,How did King Arthur acquire the round table,A Wedding gift +Food & Drink,"Wine vinegar, egg white and what other ingredient goes into making hollandaise sauce? ",Butter ,Music,Who Had A Hit With Millenium,Robbie Williams,General,Halcyon is the poetic name for which bird,Kingfisher +Sports & Leisure,In the world of sport what is 62 feet and 10 inches long and 42 inches wide? ,Ten Pin Bowling Alley ,General,What parts of people's bodies contain their femurs,Legs,General,Which dog was originally bred to hunt badgers,Dachshund +General,"The actor who played captain sisko in 'star trek deep space nine', played ____ the 1970's series 'spencer for hire'",Hawk,General,What does a dolorimeter measure,Pain,General,What U.S. state includes the telephone area code 510,California +Science & Nature,How Many Stomachs Does A Cow Have? ,Four ,General,In what Australian state would you find Narabri,New south wales nsw,General,Who is the only American author to win both the Pulitzer & Nobel prizes,John steinbeck +General,Louis Washkansky was the first to do what in 1967,Get a heart transplant lived 18 days,General,What does ludo mean (literally),I Play,Music,"What was the on flip side of the 45 of ""She Loves You""?",I'll Get You +General,What city has the fictional zipcode 90210,Beverly hills,Sports & Leisure,NICE TEAM THUNDERS Is An Anagram Of Which Football Club? ,Manchester United ,General,Who was the mistress of D.H. Lawrence's Wragley Hall,Lady chatterley +General,What is the wife of a duke called,Duchess,General,What sports name translates as Little Game of War,Lacrosse,General,What nationality is Gerhard Berger,Austrian +General,Where is charlottetown,Prince edward island,Music,What Form Of Transport Featured On A 1992 Manics Single,Motorcycle (Motorcycle Emptiness),General,Fill in the blank: ____ & shine,Rise +General,A depilatory is a substance used for removing ____.,Hair,General,The Nitty Gritty Dirt Band first USA group to do what 1977,Tour USSR,General,What is the flower that stands for: changeable disposition,Rye grass +General,"From 1979 to 2000, which will be the most distant planet earth",Neptune,Sports & Leisure,At which sporting venue are the Grace Gates? ,Lord's ,General,Bovine is cow like but what does hircine refer to,Goat +General,Who was the 35th president of the U.S.,John f kennedy,General,What is the currency of Slovenia?,Tolar,General,Saint Augustine first argued for what,Missionary position only in sex +General,Collective nouns - A Business of what,Flies and Ferrets,General,Widespread occurrence of a disease,Epidemic,General,A shy retiring person is known as a shrinking what,Violet +General,"What is the term for a wolf's home""",Den,General,Marie Tussaud was born in what country,Switzerland,General,What Radio Program Did Roy Plomley Dream Up,Desert Island Discs + History & Holidays,Which Apollo mission carried Neil Armstrong and his cohorts to the moon? ,Apollo 11 ,Science & Nature, A giant Pacific __________ can fit its entire body through an opening no bigger than the size of its beak.,Octopus,Toys & Games,Term for any number between one & 18 in Roulette,Manque +General,What did captain matthew webb swim first,English channel,Science & Nature, A marine catfish can taste with any part of its body. The female marine catfish hatches her eggs in her __________,Mouth,General,Tosk is a dialect of which country,Albania +General,The Spinet is another name for what old musical instrument,The Virginal,General,What film star role was played by over 48 different animals,Babe the Pig,Sports & Leisure,"In which sport is the term ""love"" used?",Tennis +General,"Flattened, pear-shaped, hollow organ in the pelvis of the human female & most other mammals",Uterus,General,What does a pluviomoter measure,Rainfall,Geography,The islands of _____________ have no rivers or lakes. The inhabitants must use rain for water.,Bermuda +Geography,What is London's largest park? ,Hyde Park ,General,What sport do you rack your balls in,Billiards pool, Geography,What is the basic unit of currency for Bangladesh ?,Taka +Geography,In what city is the Leaning Tower,Pisa,Music,"Who Recorded The Albums ""Tango In The Night"" & ""The Mask""",Fleetwood Mac,General,"On TV How Is The Modern Day Prankster ""Kavan Novak"" Better Known",The Phonejacker +General,The State Duma is the lower house of assembly in the parliament of which country,Russia,General,What direction does the Tower of London face across the Thames,South west,General,What is the most extensively grown and eaten food,Wheat +General,What kind of animal is a lurcher,Dog,General,In which North Italian city is a world renowned opera season held annually in the Roman arena during July and August,Verona,General,If you had hemicrania what would you be suffering from,Migraine +General,Shoot the moon is a term used in which card game,Hearts - win all,General,Hydrophobia is a fear of ______,Water,Music,Joan Jett Was Once A Member Of The All Girl Group The Go's-Go's Or The Runaways,The Runaways +Entertainment,What did Dorothy's house land on in 'The Wizard Of Oz'?,The Wicked Witch of the West, History & Holidays,What is the popular name for little baked sausages wrapped in rashers of streaky bacon? ,Pigs in blankets ,General,Sardines are the young of which fish,Pilchard +General,The England World Cup team released a song called Back Home in which year,1970, History & Holidays,Which James Bond Movie Is Set During The Christmas Period ,On Her Majestys Secret Service ,General,What is a group of this animal called: Cobra,Quiver +General,What does lager literally mean in German,Storage,General,What is the most venomous snake,King cobra,Geography,What Is The Official Language Of Israel ,Hebrew  +Entertainment,Who is the lead singer of the Rolling Stones?,Mick Jagger,General,What New World mammal did Columbus say resembled mermaids,The manatee,Music,Who Sang About A Strange Little Girl In 1980,Sad Cafe +General,Sinistrophobia is the fear of,Things to the left left-handed,Geography,"What country was the setting for ""Casablanca""",Morocco,General,Gathering in of a seasons crops,Harvest +General,What is another term for the sternum,Breastbone,General,Sumo cant be Olympic because it bans women - why,Menstrual cycle offends Gods,General,What is the young of this animal called: Cow,Calf +General,At the battle of Actium who beat Mark Anthony and Cleopatra,Octavian - Emperor Augustus,Science & Nature,Name the second-largest planet in the solar system.,Saturn,General,Which country did the allies invade in 'Operation Avalanche',Italy +General,Who Was The First Female To Be Inducted Into The Rock N Roll Hall Of Fame,Aretha Franklin,Geography,What is the capital of Syria,Damascus,General,Who played juliet in zeffirelli's romeo and juliet,Olivia hussey +General,"What tree can be English, American or Eurasian",Elm,Science & Nature,What Is A Marmoset? ,A Monkey ,General,"What's the international radio code word for the letter ""K""",Kilo +General,Edith Smith Became The First Ever Female What In 1914,Police Woman,Sports & Leisure,Sergei Bubka From The Ukraine Is Often Regarded As The Greatest Ever Athlete In Which Field Event ,Pole Vault ,General,Who was the tallest of Robin Hood's men?,Little John +General,In Old English what is a frieosan,A Sneeze,General,Pluviophobia is a fear of ______,Rain,General,What creature gets its name for the Spanish for slowly,Remora +General,Hit parade 1992 - What movie has Meg Ryan switching identities with an elderly man,Prelude to a kiss,General,The lowest part of an entablature resting on the capital of a column. ,Architrave,Entertainment,"Name the band - songs include ""Mystify, Listen Like Thieves, Original Sin""?",INXS +General,What is the most frequent cause of business errors,Illegible handwriting,Sports & Leisure,On What Would You See A Person Perform A Cocked Hat Double ,Snooker Table ,General,The Maori are native to what country,New zealand +General,Thomas Cook the travel agent was born in what year,1808,General,Portland Rosebuds were the first US team to do what,Compete Stanley cup, Geography,Where is Cape Hatteras?,North Carolina +Music,How Many Members Are There Of The Band “The Pussy Cat Dolls”,7,Music,What Was The First UK No.1 In Which The Entire Title Actually Posed A Question,How Much Is That Doggy In The Window,General,Name the Oscar-winning actor who played Ron Jenkins in TVs Coronation Street?,Ben Kingsley +General,Sakhmet was the egyptian god of ______,War and divine vengeance,Music,"The Rolling Stones Haven't Topped The UK Charts Since 1969 , What Was The Name Of Their Last Charttopper",Honky Tonk Woman,General,What Did Harry Beck Design In The Late 1930's?,The London Underground Map +Music,Now That We've Caressed And Kissed So Warm And Tender I Cant Wait,Johnny Bristol / Hang On In There Baby,Sports & Leisure,Which football country plays in red and white checked shirts? ,Croatia ,Music,"Which Hit Features The Line ""Hop In My Chrysler It's As Big As A Whale And It's About To Set Sale ""?",Loveshack (B52's) + History & Holidays,Who fixed the date of the Christian festival 'Easter'?,Council of Nicaea,General,In Oregon it is illegal to wear what on clothing in public,The Number 69, History & Holidays,What very Famous Link To Christmas Does Irving Berlin Have ,He Wrote White Christmas  +General,One of the five special senses (sense organs) by what odors are perceived,Smell,Sports & Leisure,What 3 Letter Word It The Name Given To A Replayed Point In Tennis ,Let ,General,The river Po flows into which sea,Adriatic +General,What does atp stand for,Adenine triphosphate,Science & Nature,Apart From Potassium Nitrate What Else Is In Gunpowder ,Sulphur & Charcoal ,General,Who calls its parliament 'the knesset',Israel +General,In 1840 London Sweet Maker Tom Smith Came Up An Invention Still Hugely Popular Today What Was It?,The Christmas Cracker,Entertainment,What is the Pink panter in the Pink Pather film?,A Diamond,General,In Roman mythology Faunus was the god of what,Prophecy +General,If you have Acute hasopharyngitis what's wrong,You got a cold,General,What is the name given to jealousy shown towards a new baby by an older brother or sister?,Sibling Rivalry,General,Construction hard hats were first used on what project in 1933,Hoover Dam +Geography,"The _____________ is the world's smallest ocean. It is mostly covered by solid ice, ice floes, and icebergs.",Arctic ocean,Science & Nature, The leech has 32 brains _ 31 more than a __________,Human,General,Which brand of sportswear takes its nape from a South African gazelle,Reebok +General,In what sport does a player win when they get 15 points,Badminton,General,"After his vision, who was Saul known as",Paul,General,U.S. Captials - Utah,Salt Lake City +General,Whose second album was the chart-topping 'no need to argue',Cranberries,Sports & Leisure,Fighting With Gloves Became Standard In 1867 After The Formulation Of Which Set Of Rules ,The Queensbury Rules ,General,Where did venetian blinds originate?,Japan +General,What was the only dummy awarded an academy award,Charlie mccarthy,Food & Drink,From what is American Bourbon distilled? ,Corn mash and malted barley ,Music,Which Pop Singer Married Jamie Redknap In 1998,Louise Nerding +General,"The popular character of __________ was not created by Carolyn Keene, but was actually created by a man named Edward Stratemeyer",Nancy drew,General,Kier Auro is good morning in what language,Maori,Music,Average length of a Beatles concert in their first American tour,28 Mins +General,What was Winston Churchill's codename during WW2,Agent,General,"Dasher, prancer and vixen what is god called in the muslim faith",Allah,General,What is the name given to a fox's tail,Brush +Music,Who Is Placebo's Outrageous Lead Singer,Brian Molko,General,What's nm to a sailor,Nautical mile,Science & Nature,How Much Does One Million Equal In Binary ,64  +Science & Nature," Just like people, mother __________ often develop lifelong relationships with their offspring.",Chimpanzees,General,In 1901 Dr Dausand demonstrated what that never caught on,Silent Cinema – for the blind,General,Chinchillas bathe in what seemingly paradoxic substance?,Dust +General,"Marble sized, vanilla flavored, chewy caramels covered with milk choclate.",Milk duds, History & Holidays,"Which Family Live At 1313 Mockingbird Lane, Mockingbird Heights ",The Munsters ,General,"At darts, what is a score of 26 called",Bed & breakfast +General,On what number is the decimal system based,Ten,Science & Nature,What is the ocean of air around the earth called,Atmosphere,Science & Nature, The American __________ weighs approximately one pound when fully grown.,Crow +General,Name marc bolan's first single,Teenage dream,General,Who was Oberon's wife?,Titania,Art & Literature,"Works of a culturally homogenous people without formal training, generally according to regional traditions and involving crafts.",Folk art +General,What Was The Unlikely Former Job Of EX US President “ Gerald Ford ”,Male Model,General,What sport was deemed to violate civil rights banned New York,Dwarf Throwing – From Aus 16 feet,General,"What cocktail is made from whiskey, hot coffee and whipped cream",Irish +General,What city is famed for its rive gauche,Paris,General,What is a group of jays,Party,General,Which city in Rajasthan has riding breeches named after it,Jodhpur +General,What famous trials were held in 1692,Salem witch trials,General,In Michigan illegal woman do what without husband permission,Cut her hair,General,"Characters such as those in chinese in which a word is represented by a picture, are called:",Ideograms +Music,Who led the Crazy World?,Arthur Brown,General,The main square in Venice is named after this saint,Mark,Science & Nature,What Is The Main Constituent In The Manufacture Of Glass ,Sand  +General,The vernal equinox is the beginning of ________,Spring,General,What's the oldest fraternity in america,Phi beta kappa,General,"Belgium, the Netherlands and Luxembourg came together in 1948 to form an economic union, known as what",Benelux +General,What did a Cordwainer make,Shoes,General,What is the southernmost country,Chile,Science & Nature, More people are killed in Africa by crocodiles than by __________,Lions +General,"In the anglo-saxon poem, who killed grendel",Beowolf, Geography,What is the basic unit of currency for Chad ?,Franc,Science & Nature,What Type Of Engine Was Invented By German Felix Wankel In The Mid 1950's ,Rotary Internal Combustion Engine  +General,What is the billionth digit of 'pi',Nine,General,What is the most common name in the world,Mohammed.,General,Who sang the title theme of the James Bond film The Living Daylights,Aha +General,Vivaldi composed The Four Seasons - what's his first name,Antonio, Geography,Where is the Holy Kaaba?,Mecca,General,"What, in knitting, do 'cable', 'purl' & 'popcorn' refer to",Stitches +General,The average human body contains enough fat to make how many bars of soap,7,General,Who drives a car licensed 6YZ643,Fred Flintstone,General,Who was the last chairman of the k.g.b,Vadim viktorovich bakatin +General,In a survey 4% of US employees never do what at work,Laugh,General,Which famous whore said - God is love but get it in writing,Gypsy Rose Lee,General,Which King of England died in 1910,Edward vii +General,For Red October What African republic's name was inspired by its thriving elephant tusk trade,The Ivory Coast,Music,With Which Band Did Norman Cook Have A Number One In 1990,Beats International,General,"What colourless, odourless light gas is used to lift airships",Helium +Geography,In which country is the Dalai Lama's palace,Tibet,General,In Japan what would you find in a Heya,Sumo wrestlers,General,"The most common phobia, arachnephobia, is a phobia of?",Spiders +General,What are the young of seals called,Pups,General,What is another term for a police informer,Nark, History & Holidays,The Wars of the Roses (1455-85) were fought between which two houses of England? ,York and Lancaster  +General,Who composed The Carnival of the Animals,Saint saens,General,Something Happened 74 Picture This 88 Closing Time 94 author,Joseph Heller,General,What was the British nickname given to the Lorenz Cipher machine used by the Germans in World War II?,Tunny +General,What independent states name has 10 letters only one vowel,Kyrgystan,General,What is the second largest continent,Africa,General,Hey Big Spender comes from what musical,Sweet Charity +Geography,What is the capital of Egypt,Cairo, Geography,What is the capital of Israel ?,Jerusalem,General,What is a group of cormorants,Solitude + Geography,What is the capital of Bhutan ?,Thimphu,General,Scriptophobia is a fear of ______,Writing in public,General,"What Was The Name Of The Brothel In The 1982 Dolly Parton / Burt Reynolds Movie ""The Best Little Whorehouse In Texas""",The Chicken Ranch +Science & Nature,What word is used for a male deer?,Buck,General,How many muscles are in a human ?,639,General,"If you had chronic regional ileitis, what eponymous disease would you have",Crohn's +General,Name Eddie Murphy's skit about vocabulary on Saturday Night Live.,Mr. Robinson's Neighborhood,General,What song did Marilyn Munroe sing in the film Bus Stop,That Old Black Magic,Music,Amount that's generated on Beatles bootlegs yearly,$2.7 million +General,The Venice Cup is for women only playing what,Bridge,General,What Canadian province has been virtually rat free since 1905,Alberta,General,Who played eddie in the pop-culture film 'rocky horro picture show,Meatloaf +General,"Countries of the world:landlocked country in southern Africa, the capital is Harare",Zimbabwe,General,At the beginning of a game of draughts how many pieces does each player have,12,Science & Nature,Which of the six noble gases comes first alphabetically? ,Argon  +General,The Nuffield Radio Astronomy Laboratories are more usually known by what name,Jodrell bank,Science & Nature,What is the atomic number for thalium?,81,General,What started in 1935 when a doctor and a stockbroker met,Alcoholics Anonymous +General,Canada is the world leader in the production of,Newsprint,General,In the Dr Dolittle stories what type of creature was Too-Too,Owl,General,Kingston the capital of ______,Jamaica +Music,"""Rollin"" And ""Once Upon A Star"" Were No.1 Albums For Which 70's Sensation",The Bay City Rollers,General,What is Mo Mowlam's first name,Marjorie,General,What is a siesta,Mexican afternoon nap +General,What ancient measure was the distance from the elbow to the tip of the middle finger,Cubit,General,What was the name of the funky van Scooby Doo and friends rode in?,The Mystery Machine,General,How many American voyages did Christopher Columbus make,Four +Tech & Video Games,Who is the main character from Nintendo's 'Earthbound'? ,Ness,General,Where would you find a gemshorn,On an Organ,General,Which American state is nicknamed The Diamond State,Delaware +General,Capital of Azerbaijan,Baku,General,What is the name given to stoats when they have their winter coats,Ermine,General,Eight bells on board a ship means what,End of a watch +General,"What do the dodo, moa and great auk have in common",They are extinct,Geography,What volcano showers ash on Sicily,Etna,Music,"Who Was ""Standing In The Shadows Of Love"" In 1964",Four Tops +Music,What was Britain's first winning entry in The Eurovision Song Contest (name The Song),Sandie Shaw / Puppet On A String,General,"Mineral & the most abundant ore of iron, composed of ferric oxide, fe2o3",Hematite,General,Nathan Bedford Forest a confederate general was the first what,Grand Wizard KKK +Science & Nature,Which Large Animals Did Hannibal Bring With Him Across The Alps ,Elephants ,General,Which cartoon character was originally pink and called Orsen,Tweety Pie,General,What children's book did Forrest Gump keep in his suitcase,Curious George +General,Which car company was founded by Sir William Lyons in 1922,Jaguar,Music,Do You Remember Who Was Flying High To His Music In The Mid 70's,John Miles,Sports & Leisure,In which sport would you compete for the Waterloo Cup? ,Crown Green Bowls  + History & Holidays,"In the 1946 film, It's a Wonderful Life, what's the name of George Bailey's guardian angel? ",Clarence (Oddbody) ,Geography,Which County Is Basingstoke In? ,Hampshire ,Art & Literature,"Refers to art that uses emphasis and distortion to communicate emotion. More specifically, it refers to early twentieth-century northern European art, especially in Germany c. 1905-23. ",Expressionism +General,For what are allen and wright famous,Root beer, History & Holidays,What Disaster Hit London In 1666? ,The Great Fire ,General,Which competition was organised by Mecca Ltd. to coincide with the 1951 Festival of Britain,Miss world +General,What did American Harland D. Sanders give to the world in 1939,Kentucky fried chicken,General,Where is the hockey hall of fame,Toronto,General,"What does the abbreviation ""i.o.u."" Really stand for",I owe unto +General,"In Greek mythology, who hired daedalus to construct the labyrinth",Minos,General,Biblical city was the code for RAF bombings of Hamburg WW2,Gomorrah,General,What seven letter word do all Americans pronounce wrongly,Wrongly + Geography,What is the basic unit of currency for Panama ?,Balboa,General,What is the only english word that ends in the letters 'mt',Dreamt,Food & Drink,In The Dish 'Devils On Horseback' Of What Are The Devils Made ,Prunes  +Sports & Leisure,"Which football team was nicknamed the ""Orange Crush""",The denver broncos,General,Who is President of the European Commission,Jacques santer,Music,Which Famous Group Was Denny Laine In Before He Joined Wings,The Moody Blue +General,Who is known for the 'theory of evolution',Charles darwin,Art & Literature,Who Drew Drawings Of Absurd Mechanical Contrivances ,William Heath Robinson ,General,A Gandy Dancer has what job,Railroad repair gang +General,Name the first president of the american red cross,Clara barton,General,Who works from home,Open-collar workers,General,What nba team chose Dominique Wilkins as their first round draft pick,Utah jazz +General,Orienteering began in which country,Sweden,General,As what was tomato ketchup once sold,Medicine,General,When was the incandescent lamp invented,1879 + Geography,Where is the world's largest desert?,North Africa,Science & Nature,Who Invented Nylon ,An American Chemist Named William H Carothers , Geography,In which state is Hoover Dam?,Arizona +Music,What Vehicle Drove Natalie Cole Into The Top Ten In 1988,Pink Cadilac,General,Who taught alexander the great,Aristotle,General,"Who sang the 1963 hit ""it's my party""",Lesley gore + History & Holidays,"Who Was A Tory, then Became A Labour MP And Finally Lead The British Facist Party? ",Oswald Mosley ,Science & Nature,"When traces of a calcium compound are held in a bunsen flame, the colour of the flame changes to ___?",Red,Mathematics,How many different letters are used in the roman numeral system?,Seven +Music,Who recorded 'In the Air Tonight' in 1981?,Phil Collins,Entertainment,What was the original name Charles Schultz had for Peanuts,Li'l folk s,General,What is Linus last name in the Peanuts cartoons,Van Pelt +General,Generally cornflowers are what colour,Blue,General,What is the substance obtained from acacia trees that is used in medicine,Gum arabic,General,What is roasted in South Africa and eaten like popcorn,Termites +Music,Who Joined Queen in their 1981 hit Under Pressure,David Bowie,Music,"Which British Pop Band Had A Hit Album In 1967 With ""If Music Be The Food Of Love..Prepare For Indigestion""","Dave Dee, Dozy, Beaky, Mick & Tich",Food & Drink,From What Country Does Advocaat Originate ,Holland  +General,Buddy Rich's real first name was what,Bernard,General,What is a group of this animal called: Peacock,Muster ostentation,General,Who was the Greek goddess of love & beauty,Aphrodite + History & Holidays,"I Am A Suburb Of California, I Have The Nickname (Surf City) and part of my name sounds the same as the surname as 2 Famous Hollywood Celebrities, Where Am I ",Santa Cruz ,General,What kind of bug emerges in the 1975 movie the bug?,Beetle,General,"The most prominent of the 12 disciples of Jesus Christ, a leader and missionary in the early church, and traditionally the first bishop of Rome",Peter +Music,Who Married Actress Julia Roberts In 1993,Lyle Lovett,Entertainment,"What is the name of the rabbit in the film, ""Bambi""?",Thumper,General,Who was dropped by 20th cent Fox for being too ugly,Marilyn Munroe by Darryl Zanuck +General,As what is Krung Thep is more commonly known,Bangkok,General,What heisman trophy winner returned his first nfl kickoff for a touchdown?,Tim brown,General,In ancient Greece aristocratic women were deflowered with what,Stone Penis +General,Who was shaka's successor,Dingaan,General,The word stymie originated in what sport,Golf ball blocking you,General,Hundred and ninety two khartoum is the capital of ______,Sudan +General,"After the end of the Vietnam War, to what was Saigon's name changed",H0 chi min city,Music,"Who Had A Hit With ""Higher Love""",Steve Winwood,General,"Cytosine, Adenine, Thymine And Guanine Are The Basic Requirements For Which Compound",DNA +People & Places,What Is Princess Diana's Madien Name ? ,Spencer ,General,What creature acts as a carrier of the diseae bilharzia,Freshwater snail,Music,"Who Recorded The Aptly Named Album ""Have Twangy Guitar, Will Travel""",Duane Eddy +General,"Who, on his deathbed, said that he did not wish Queen Victoria to visit him because she would only give him a message for Albert",Disraeli,Science & Nature,Which Nationality Was The Man Who Built The First Bicycle Propelled By Pedals ,Scottish Kirkpatrick Macmillan 1840 ,General,What does 'int' stand for in the C programming language?,Integer +General,You can do a degree in brewing at Heriot-Watt University where,Edinburgh,Art & Literature,Douglas Adams is famous for writing what ?,The hitchhiker's guide to the galaxy,General,Who was Hitler's propaganda minister,Goebbels +Entertainment,Who is Gordon Sumner better known as?,Sting,General,What colour is vermilion a shade of,Red, History & Holidays,"The two crooks in the 1990 film 'Home Alone'', one was Daniel Stern, who was the other ",Joe Pesci  + History & Holidays,In Which Year Was The Berlin Wall Constructed ,1961 ,General,Who was Adam & Eve's third child,Seth,Science & Nature,"Of Stalactites And Stalagmites, Which Grow Upwards? ",Stalagmites  +General,"What is the creature that is half eagle, half lion",Griffin,General,In which ruins was the first known written advertisement found?,Thebes,General,Baklava is a form of___.,Dessert +General,"What is the nickname for Reading, Pennsylvania.",Pretzel city,General,Chlorine is derived from the Greek word meaning what,Green,General,What mountain range includes the siwaliks & karakorams,Himalaya +Entertainment,Who played Clyde to Faye Dunaway's Bonnie?,Warren Beatty,General,"Who wrote the book 'The Lion, The Witch and The Wardrobe'",C.s. lewis,General,Bargasse is what type of vegetable matter,Sugar Cane Pulp +Food & Drink,What Is The Italian Name For Squid In A Restaurant? ,Calamari ,General,The Hula Hoop was illegal in what country,Finland,General,Which 19th century Australian ruffians were noted for broad-rimmed hats and bell bottom trousers?,Larrikins +Entertainment,"Name the band - songs include ""Light My Fire, Love Her Madly""?",The Doors,Music,"He Flew His Own Single Engine Aircraft But Crashed Into Monterey Bay In October 1997. What Was The Stage Name Of The Man Born ""Henry John Deutschendorf Jr""",John Denver,Music,What Is The Name Given To A Piano That Plays Mechanically,A Pianola +People & Places,In Which Country Was Paper Magnate Robert Maxwell Born ? ,Czechoslovakia ,General,What disney character's picture did the N Y Times wrongly call goofy,Pluto,General,Richard Penniman became famous as who,Little Richard +Science & Nature,How many colored squares has a rubiks cube ,54 ,General,What is the more common term for enuresis,Bed wetting,Science & Nature,Sodium bicarbonate is better known as _________.,Baking soda +General,Who was nicknamed Impeesa (no sleep wolf) by Matabele tribe,Baden Powell,Geography,What is the capital of Tanzania,Dar es salaam,General,Name the organisation for which Jim Bergerac worked,Bureau des etrangers +Music,"What Did Ron Pigpen McKernan, Keith Godcheaux & Brent Myland All Have In Common",All Keyboardists Who Died While With The Grateful Dead,General,What does michelle remeber the formula for in Romy and Michelle's High School Renuion?,Post-it glue,General,Only two north American Indian tribes with 3 letters Wea and what,Fox + History & Holidays,The Character Of Mike Myers Features Heavily In Which Series Of Horror Movies ,Halloween ,General,"What did AOL claim had been found, on April Fool's Day in 1996 ?",Life on Jupiter,General,In which sphere of industry or commerce is the name of Arthur Maiden famous,Advertising +General,Florence Nightingale took what cos she was around young men,Bromide,General,"According to superstition, what do you do when yoU.S.tub the toes on your right foot",Make a wish,Science & Nature,What is the fastest growing species of grass ?,Bamboo +General,Mount Athos is famous for its many monasteries of which religion,Greek orthodox,General,"What links Alex, Ben, Chrissie and Quint",Eaten by Jaws,General,What makes a solution saline,Salt +General,A line that touches a circle at only one point is called a ______. ?,Tangent,General,What Musical Act Had The Last UK No.1 Of The Millenium,Westlife,General,Susannah Yolanda Fletcher became famous as who,Susannah York +Science & Nature,Which Acid Is Present In Your Stomach ,Hydrochloric Acid ,General,Laurence Tureaud became more famous as who,Mr T,General,How many westerns were directed by a woman,One +General,Who drinks the most tea per capita,Ireland,General,What is the largest wild animal in the UK,The Scottish red deer,General,"Who was the first British Prime Minister, although he did not use the title",Sir robert walpole +General,What is Pennsylvania's main agricultural export,Mushrooms,Food & Drink,Whose 'how to cheat' cookbook was heavily criticized when it was launched in 2008 ,Delia Smith ,General,What name is given to the drink which is composed of equal parts of beer and stout,Black and tan +General,When was the first credit card issued,1900,General,Carom is a form of what sport / game,Billiards,General,On What Type Of Object Did The Queen Sign Her Autograph On A Tour O Kuala Lumpur In 1998,A Football +General,"Who was the son of Saturn and Ops, and the husband of Juno",Jupiter,Music,What Was The Name Of George Michael & Andrew Ridgeley's First Band,Executive,General,What bird can see the colour blue,An Owl +General,What Is The Full Address Of The Simpsons In The Cartoon Series (Number Worth Jackpot),742 Evergreen Terrace,General,"What golfing accessory was patented by George Grant on december 12th, 1899",Tee,Science & Nature,What is the biological name for the shin bone?,Tibia +Geography,In which state is Mount Vernon,Virginia,General,If you were taking a class in pistology what are you studying,Faith,General,Who played the Uncle on Family Ties?,Tom Hanks +General,"Excluding Rudolph, how many reindeer pull Santa's sleigh",Eight,General,Which country began the tradition of exchanging Xmas gifts,Italy,General,What animals name translates as water horse,Hippopotamus + History & Holidays,Which Food Of The Gods Was Said To Give Humans Immortality? ,Ambrosia ,General,"Belgian Joseph Merlin invented this form of ""transportation"" in 1760",Roller skate,Geography,What American state is the 'Hoosier State'?,Indiana +General,In what stage show does Frank N Furter appear,The Rocky Horror Picture Show,General,Which cookery dish is named after a Napoleonic battle,Chicken marengo,General,What Italian city is considered the fashion capital,Milan +Geography,This country is home to the world's oldest continuous local democracy.,Iceland,Science & Nature, The grizzly bear is capable of running as fast as the average __________,Horse,Science & Nature,Osteomyelitis affects the _________.,Bones +General,In which large bay would you find the Belcher Islands,Hudson bay,Art & Literature,At which railway station does Harry Potter catch the Hogwart's Express at platform 9 and 3 quarters? ,King's Cross ,General,What brass instrument is thought to be the most difficult to play,The French Horn +General,What is the fear of definite plans known as,Teleophobia,General,This island was ulysses' home,Ithaca,General,William Manson And Thomas Woods Became Partners In Which Famous London Business In The 19th Century,Christies Auction House +General,83% of all Americans purchase what product,Peanut Butter,General,What is the largest country in Africa,Sudan,General,When was the electromagnet invented,1828 +General,"Who was the last president of the U.S., as of 1998, to die in office",John kennedy,Art & Literature,Which Shakespeare play opens with the 3 Witches? ,Macbeth ,General,On what is an espadrille worn?,Foot +General,Ncaa: who were the finalists in the men's basketball championship in 1948,Kentucky & baylor,General,Maschalophilous people get sexually aroused by what,Armpits,General,Aprosexia is the abnormal inability to do what,Concentrate +General,What country is the home of the Ashanti people,Ghana,General,Which animal lives at the highest altitude,Yak,General,According to the 2000 census in the UK what is now a religion,Jedi Knight +General,A light clear red colour,Cerise,Music,Which Soul Artist Was The Surprise Hit Of The Otherwise Flowery 1967 Monterey Pop Festival,Otis Redding,Entertainment,Photographer for Daily Planet,Jimmy olsen +General,What is the young of this animal called: Elephant seal,Weaner,General,"Of whom was it said by Gerald Ford, 'He doesn't dye his hair - he's just prematurely orange'",Ronald reagan,General,The dinosaur apatosaurus used to be called what,Brontosaurus +General,What is the best score in blackjack,21,General,"Who sang the 80's smash hit 'future's so bright, i gotta wear shades'",Timbuk,Toys & Games,Talking toy that helped you do well on Spelling tests.,Speak n spell +General,What does a manometer measure,Gas pressure,Music,Name The 4 Members Of The Band The Travelling Wilburys (PFE),"George Harrison, Roy Orbison, Tom Petty, Bob Dylan",Food & Drink,"Vodka or gin, ____ juice and sugar make a gimlet.",Lime +General,What is a moon in its first quarter,Two-bit moon,Music,"""Higher Love"" Was A Hit For Which Uk Artist",Steve Winwood,General,The process in which a solid changes directly to a gas is,Sublimation +Geography,What is the capital of Colorado,Denver,General,"Widespread disease caused by the infestation of the human body by flukes fluke commonly called blood flukes, of the genus schistosoma",Schistosomiasis,General,What is the real name of Jem from Jem and the Holograms?,Jerrica Benton +General,What's the USA's largest legal cash crop,Corn,General,Name the world's first nuclear powered merchant ship,Savannah,General,Whose dog was Gala Poochie,Rootie kazootie + Geography,Djibouti is the capital of ______?,Djibouti,General,What Austrian city was named for it's mining & trading of salt,Salzburg,General,Storehouse or place where vehicles are kept,Depot +Sports & Leisure,In What Context Did Thierry Replace Ian Who Had Previously Replaced Cliff? ,Leading All Time Goal Scorer For Arsenal ,Geography,Havana is the capital of which country ,Cuba , Geography,What is the basic unit of currency for Burundi ?,Franc +Music,"Which Group Starred ""Magical Mystery Tour""",The Beatles,General,What sort of fish is used to make an Arbroath Smokie,Smoked haddock,General,Name for a large seawater aquarium for keeping sea animals,Oceanarium +General,Whose secretary was Loelia Ponsonby,James Bond,General,"According to the Bible, what is the root of all evil",Money,General,Why was Eric Moussambani famous at the Sydney Olympic games,The slow but heroic swimmer from equatorial guinea +Science & Nature," To survive, most birds must eat at least half their own weight in food __________",Each day, History & Holidays,What 'BH' Do You Get When You Cross & Sheep With A Sweet ,Bah Humbug ,Religion & Mythology,Dionysus was the greek god of ______?,Wine +Science & Nature,Coal is predominantly made up of this element.,Carbon,Science & Nature,What Are You Deficient In If You Suffer From Scurvy ,Vitamin C ,General,Who wrote the play Amadeus,Peter Shaffer + History & Holidays,Which eighties cartoon ended with the phrase: 'And knowing is half the battle?' ,G.I.Joe ,General,In fashion correspondent and bar are types of what item,Shoe,General,A JPEG is a picture file format - what does JPEG stand for,Joint Photographic Experts Group +General,The golden lion is awarded at which film festival,Venice,General,Philemaphobia is the fear of,Kissing, History & Holidays,What was the name of the pandemic which killed over 1% of the world's population in 1918? ,Spanish Flu  +Music,Son Of My Father Is Credited As Being The First Song Ever To Feature Which Instrument,A Synthesizer,General,What are you if yoU.S.uffer from baker's leg,Knock kneed,General,In West Virginia its illegal to cook what - because of smell,Cabbage - Can go to Prison for it +General,What three letter word means 'the front of a ship',Bow,General,This Sioux indian toured with Buffalo Bill's wild west show,Sitting bull,Music,What Job Did Neil Tennant Have Before Forming The Pet Shop Boys,Music Journalist +General,Roger Bresnahan introduced what to baseball in 1907,Shin Guards,General,Name the spinoff show from Who's the Boss?,Living Dolls,General,"The olympic motto 'citius, altius, fortius' means what","Faster, higher, stronger" +Music,Which TV Talent Show Did Mary Hopkins Win,Opportunity Knocks,Music,Stairway To Heaven Was The Title For A Book About Which Band,Led Zeppelin,General,What's the offspring of a lioness & a tiger called,Liger +Sports & Leisure,What is James Naismith best known for?,Inventing Basketball, Geography,What do Americans traditionally eat on thanksgiving day?,Turkey,General,Who at Buckingham Palace wears bearskins?,Guards +General,Where were the first gambling casinos,Egypt,General,U.S. Captials - Alabama,Montgomery,Music,Which Album Cover Did Andy Warhol Design For The Rolling Stones,Sticky Fingers +General,Who was the catcher on the peanuts gang's baseball team,Schroeder, History & Holidays,"Which Christmas carol includes the lyrics '___To save us all from Satan's power, when we were gone astray..'? ",God Rest Ye Merry Gentlemen ,General,"Sandra Bullock, Kris Christophensen, Bruce Willis - Job Links",Not Acting – Bartender +General,In 1879 James Ritty invented what,Cash Register,General,Seeds of SE Asian aromatic plant used as a spice,Cardamon,General,What's capital of The Peoples Democratic Republic of Yemen,Aden +Music,Name The First Chart Entry for Guns N Roses Ranking Higher On Its Re-Release In 1988,Welcome To The Jungle,General,The cancer drug 'Taxol' is derived from which tree?,Yew Tree,General,What is the literal translation of pot-pouri,Putrid Pot +General,Which dress designer was shot dead in the summer of 1997,Gianni versace,General,What is the nickname for South Carolina,Palmetto state,General,What does 'the monument' in london commemorate,Great fire of london +General,No nfl team which plays its home games in a domed stadium has ever won a ______,Super bowl,General,Boreas Eurus Notus Zephyrus were what,Classic Winds NESW,General,What U.S. president wrote 37 books,Theodore roosevelt +Music,Which Police Track Got To No.2 On Its Re-Entry In 1979,Can't Stand Losing You,Entertainment,What did Peppermint Patty always call Charlie Brown?,Chuck,General,Weight-loss guru ___ brings his fitness ideas to the little screen,Richard + History & Holidays,When Are Werewolves Expected To Appear ,When There Is A Full Moon ,General,Which type of full moon follows a harvest moon,Hunters Moon,General,"What Part Of The Human Body Has The Medical Term ""Cillium""",The Eyelashes +General,Which sports trophy was named after Fredrick Arthur Stanley Cup,Fred is Lord Stanley,General,Who founded the London Metropolitan Police,Sir robert peel,General,Which company pioneered the first trans-Pacific passenger flights,Pan american +General,Which country's national flag has a bird and a snake depicted in combat,Mexico,General,Eve of All Saints Day,Halloween,General,From new york to where was the first commercial Boeing 747 flight,London +General,Aromatic gum resin burnt as incense,Frankincense,General,Chief monetary unit of germany,Deutschmark,General,"What name is given to the central single wedge-shaped block at the top of an arch, which is essentially a central voussoir",Keystone +General,What is the translation of the Greek name Vanessa,Butterfly,General,What's the tallest mountain in Canada,Logan,General,Which gas discovered in 1898 has a name meaning new,Neon +Food & Drink,"In which order do you drink lemon, salt & tequila? ","Salt, Tequila, Lemon ",General,Name the pet peacock on The Walton's,Rover,Art & Literature,Which 2 Brothers Wrote Fairy Sttories In Germany In The 19th Century ,The Brothers Grimm  +General,Which traditional English sweet dish consists of apples with Victoria Sponge on top?,Eve's Pudding,General,Jumping Badger was the original name of which Indian leader,Chief Sitting Bull,General,How did camerawoman Lee Lyon die while working,Charged by Elephant +General,What is the occupation of Mary Poppins,Nanny,General,Who was the first character to speak in Star Wars,C3PO,Sports & Leisure,In The World Of Motor Sports Which Sporting World Champion Was Born in Blackburn On July 1 st 1966? ,Carl Fogarty  +General,What is the name of the dish of eggs baked with spinach,Eggs florentine,General,What is the Capital of: Panama,Panama,General,Who stole the English Crown Jewels was pardoned Charles II,Colonel Blood +General,Heinrich the lion founded what city,Munich,Music,Who Played The Part Of Che Guevara In The Original London Production Of Evita,David Essex, History & Holidays,Who ran unsuccesfully against Regan in 1984? ,Walter Mondale  +General,What was the title of Kevin's byline story in St. Elmos Fire?,The Meaning Of Life,General,Who was the tallest of robin hood's men,Little john,General,Brian Gamlin of Bury is credited with what sporting invention,Numbers on Dartboard +General,Where do you find the medulla oblongata,Brain,Toys & Games,"Which game usually begins with, ""Is it animal, vegetable, or mineral?""",Twenty questions,General,The Lord of the Rings_,J.r.r. tolkien +General,What was Fonzies favourite magazine,Hot Rod,Science & Nature,The lack of this element in the diet is a cause of goitre.,Iodine,General,What voltage are most car batteries,12 +Science & Nature," The anaconda, one of the world's largest snakes, gives birth to its young instead of __________",Laying eggs,General,In 1990 When Stephen Hendry Became The Youngest Person ever To Be Crowned The World Snooker Champion Who Did He Beat In The Final,Jimmy White,General,Aromatic bitter bark of S.American tree,Angostura +General,“Right From The Start” & “Talking Point” Are Autobiographies By Whom ?,Gareth Gates,Science & Nature,What Are The 3 Divisions Of An Insects Body ,Head Thorax & Abdomen ,General,"Only Hawaii, Utah and Tennessee dont have some form of what",Legal gambling +Music,Bonnie Tyler Had A Total Eclipse Of The What,Heart,General,If a male ass is a Jackass what is a female called,Jenny, History & Holidays,Who was the ruler of the Principality of Monaco who married actress Grace Kelly in 1956 ,Prince Ranier  +General,Who invented wax paper,Thomas Edison,General,What is an antitussive medicine taken for?,Prevent Coughing,General,With what did writers replace the pouncepot,Blotting paper +Music,"Who Let His Trumpet Do The Talking With ""This Guys In Love With You""",Herb Albert,Art & Literature,Who wrote 'The Hobbit'?,J.R.R. Tolkien,General,Complete this proverb:'Listeners never hear any..',Good of themselves +General,What colour is malachite,Green,Music,N 2006 Who Became The First British Solo Artist For Eight Years To Top The US Charts?,James Blunt,Art & Literature,Where Would You Find Poets Corner ,Westminster Abbey  +Geography,Into what body of water does the danube river flow ,Black sea ,General,Instrument for measuring wind force,Anemometer,General,What do people do when they 'tie the knot',Get married +General,The okapi belongs to what family of animals,Giraffe,Science & Nature," At birth, baby __________ are only about an inch long _ no bigger than a large waterbug or a queen bee.",Kangaroos,General,What is a south african coin containing 1 troy ounce of gold,Krugerrand +Science & Nature,Which snake kills the most humans?,King cobra,General,What fruit is usually used to make Marmalade,Orange,General,What part of the human body grows to 20 times its size at birth?,Nose +General,What Is The Largest Living Land Carnivore,Grizzly Bear,General,Who played Domino in Never say Never Again,Kim Bassinger,Music,Who Launched The Rocket Record Label In 1972,Elton John +General,The Queen has what music with her breakfast,Bagpipes - Started by Victoria,General,What was the first desert rodent to spin its wheels into popular petdom,Hamster,General,How long were Jerry Seinfeld and his pals sentenced in the series finale?,One year +General,What is the Capital of: Oman,Muscat,General,Creatures of class including spiders and scorpions,Arachnid,General,Who sold Louisiana to the USA in 1803,Napoleon +General,Morbi in Gujarat is where most of the worlds what are made,Wall Clocks,General,What was the name of the bar on Dukes of Hazzard?,Boars Nest,General,The top basketball player of the decade,Michael jordan +General,National capitals: Costa Rica,San jose,Geography,What is the capital of Djibouti,Djibouti,General,Which Indonesian fruit is known for its unpleasant smell,Durian +General,Who wrote the best selling novel Polo,Jilly cooper,General,What is named after Dr Ernest Grafenberg,The G spot,Art & Literature,"""The Diary of Anne Frank"" was first published in English under what title?",The Diary of a Young Girl +General,What is the second largest bird in the world,Emu,Music,Who Was The First Beatle To Have A Solo Number One Hit,George Harrison,General,What kind of poem its keats's 'to a Nightingale',Ode +General,A average male will have 2000 what during his lifetime,Masturbated Ejaculations,Science & Nature,What is the common name for the tympanic membrane?,Eardrum,Music,In Which Year Did The Bay City Rollers Have 2 Singles In The Number One Slot,1975 +Music,Jim Morisson Was Found Dead In What,The Bath,General,Gladys Mary Smith became famous under what name,Mary Pickford,Art & Literature,A painting or drawing executed in a single color. ,Monochrome +General,What part of a frog do you rub to hypnotise it,Its belly,General,What item does Dogbert use to find empty chairs,Dowsing rod,General,The baptist What New World mammal did Columbus say resembled mermaids,The manatee +Entertainment,Hanna_Barbera rose to fame by creating what duo for MGM,Tom and jerry,Technology & Video Games,What was invented over 3000 years ago that is now considered the first 'computer'?,Abacus,General,In what kind of buildings are hops dried,Oast house +General,In which American state is Cincinnati,Ohio,General,Which Russian composer died on the same day as Josef Stalin?,Sergei Prokofiev,General,What are you doing if you performed Applejack Chaos Rev up,Line Dancing +General,Who Was Britain's First Million Pound Footballer,Trevor Fraancis,General,How long were Noah and his family confined in the ark,One year, History & Holidays,How Old Was James Dean When He Died ,24  + History & Holidays,What Was The Name Of The Character Played By Anne Bancroft In The 1967 Film 'The Graduate'' ,Mrs Robinson ,General,Apparatus mixing air with petrol vapour in an internal combustion engine,Carburettor,General,What explorer wrote the history of the word in prison in 1600s,Sir Walter Raleigh +Geography,What Is The Worlds Largest Desert Called ,The Sahara ,General,What's the primary function of two-thirds of a shark's brain,Detecting,General,What did Popeye eat for strength before spinach,Garlic +Music,"In 1981 Which British Band Had A Hit Album Entitled ""From The Tearooms Of Mars To The Hellholes Of Uranus"" And A Hit Single Called Einstein A Go Go",Landscape,Art & Literature,"This is the choice and arrangement of words and phrases in a literary work. It is the vocabulary that the author, poet or playwright uses to create style and effect in a piece of writing?",Diction,General,Who played the president of the u.s in 'air force one',Harrison ford +Science & Nature," Elephants, lions, and camels roamed __________ 12,000 years ago.",Alaska,General,"George Michael was one half of Wham!, name the other half.",Andrew Ridgley,General,What is the fear of drugs known as,Pharmacophobia +General,"Which herb whose leaves and blue flowers are both edible, is used in drinks such as 'Pimms'",Borage,General,Most effective spot to pierce a Vampire with a wooden stake.,Heart,General,What was the original name of the singer Tina Turner,Annie mae bullock +General,"What are Manhattan, Apollo, Hedwig, Cartman, Guinness",Linux versions,General,What is consumption otherwise known as,Tuberculosis,Toys & Games,Little blue cartoon figurines.,Smurfs +Sports & Leisure,Who Won The Embassy World Snooker Championships In 1998 ,John Higgins ,General,What is samian ware,Fine pottery,General,Of what does a human being lose an average of 40 to 100 per day,Hairs +General,What bird is associated with lundy island,Puffin,General,What islands name is Australian slang for a football shirt,Guernsey,General,What is 'Zulu' time?,Greenwich Mean Time +General,Who ruled rome when christ was born,Caesar augustus,Food & Drink,What name is given to a clear soup with thin strips of vegetables? ,Julienne ,General,What is a group of this animal called: Pig,Litter + History & Holidays,"Who played George III in Beau Brummell? (Robert Morely, Alec Guiness, Cardew Robinson) ",Robert Morely , History & Holidays,"Which hero, a Christmas panto favourite, marries Bedr-El-Budar? ",Aladdin ,General,"Quiz, bandlore, coblentz, disk all different names for what item",Yoyo - from Tagalog +General,"Who is on a U.S. $5,000 bill",James madison,General,In what was the strength of early lasers measured,Gillettes,General,What Is Measured By The Gay Lussac Scale,Alcohol +General,What is the national drink of Yugoslavia,Slivovitz,General,What animals are likely to die first from global warming,Polar Bears,Music,"Kimberly, Nadine, Sarah, Cheryl Who Is Missing?",Nicola Roberts (Girls Aloud) +General,What product uses the most silver,Camera Film,General,Small pieces of fried or toasted bread usually served with soup,Croutons,General,What is the capital of argentina,Buenos aires +General,Who was the egyptian god of the underworld and vegetation,Osiris,General,"What rifle accessory originated in bayonne, france, in 1641",Bayonet,Music,"What Group Consists Of Bill Berry, Peter Buck, Mike Mills, Michael Stipe",REM + Geography,Where is Gorky Park?,Moscow,General,Who was the shortest british monarch,Charles i,General,1961 who was first actress to win Oscar for a non English film,Sophia Loren +General,Sir Jack Cohen founded what,Tescos - supermarkets,General,In Biker Slang what are Giblets,Chrome add ons,General,What word is derived from the Arabic mawsim meaning season,Monsoon +General,What is the national flower of Australia,The Wattle Blossom, History & Holidays,In which country did the Boxer Rebellion take place,China,General,Where was Mark Twain born,Florida - Missouri +General,What does the name Mesopotamia mean,Between two Rivers,General,Where is mount etna,Sicily,General,What does an icthyophage do,Eats Fish +Science & Nature,What Diameter Floppy Disks Were Introduced By Ibm In 1970 ,8 Inch ,Science & Nature,Which Company Formed By Larry Paige & Sergei Brin Was Incorporated On 7 th Sep 1998 ,Google ,Science & Nature," It is the female __________ who does more than 90 percent of the hunting, while the male is afraid to risk his life, or simply prefers to rest.",Lion +General,Who was the first victim of the electric chair,William kemmler,Geography,Which Ancient Egyptian Burial Monuments Built During The Old & Middle Kingdom Periods Have Associations With Royal Solar & Stellar Cults ,Pyramids ,General,Which is the only work by Leoncavallo most people have ever heard of,I pagliacci + History & Holidays,Which 1992 Film Starred Goldie Hawn & Meryl Streep As 2 Woman Going To Extraordinary Lengths To Preserve Their Looks ,Death Becomes Her ,General,Which author wrote the 'Stone Diaries',Carol shields,General,What queen married 2 of her brothers,Cleopatra +Music,"Who recorded Paul's song ""Goodbye""?",Mary Hopkin,General,What is the fear of performing known as,Topophobia,General,Nancy Cartwright and Yeardley Smith provide the voices for which brother and sister on television,Bart & lisa simpson +General,On an ordinance survey map which symbol shows a battlefield,Crossed swords,General,What is a cob and a pen,Swan,General,Which writer invented the word drab,C S Lewis +General,What vitamin deficiency causes rickets,Vitamin d,General,What were the two dogs names on Magnum PI?,Apollo and Zeus,General,What continent is home to half the worlds people,Asia +General,What is Mr. Roger's first name,Fred, History & Holidays,The following is a line from which 1970's film 'Is it safe' ? ,Marathon Man ,General,A Fennec Is The Smallest Breed Of Which Mammal?,Fox + Geography,This Moslem republic in asia was formerly part of India.,Pakistan,General,What is the most popular Mexican beer in the USA,Corona,Entertainment,"Band: "" _________ And the Bad Seeds""",Nick Cave + Geography,What is the capital of Lesotho ?,Maseru,Science & Nature,To make a car go backwards you have to put it in what gear,Reverse,General,Battle of the Somme With what acid do nettles cause irritation,Formic acid + History & Holidays,"In 1978 which famous comedy returned for a second series, four years after the first had been shown ",Fawlty Towers ,General,Who is the idol of the german organization of non-commercial supporters of donaldism,Donald duck,Science & Nature,"The four stages in the life-cycle of an insect are: egg, adult, pupa, and ________.",Larva +General,Wesley Snipes and who starred in the film Money Train 1995,Woody Harrelson,Music,"Who had 1960's hits with 'Detroit City', I'm Coming Home' & 'Help Yourself'”?",Tom Jones,Food & Drink,What is 'water of life' in 'Scottish Gaelic' ,"Uisge beatha , usquebaugh (whisky) " +Science & Nature,For What Is Lepidoptera The Scientific Name? ,Butterflies & Moths ,Entertainment,What detective duo was featured in Mystery at Devil's Paw?,Hardy,General,In which city was Michael Hutchence found dead,Sydney +Music,Who Legally Adopted A Symbol To Replace His Name In 1993,Prince,Food & Drink,Which spirit is fermented and distilled from sugar cane? ,Rum ,General,Who wrote the Uncle Remus tales,Joel chandler harris +General,"In The TV Show Sesame Street What Was The First Name Of The Character ""Mr Snuffleupagus""",Aloysius,General,What is a male camel called,Bull,General,In Pueblo Colorado its illegal for what to grow in city limits,A Dandelion +General,Fife the only member of the band zz top without a beard has what last name,Beard,General,A capital D is the Roman numeral for which number,Five hundred, Geography,"Which river passes through Germany, Austria, Slovakia, Hungary, Croatia, Yugoslavia, Romania, Bulgaria and Ukraine before arriving at the Black Sea?",Danube +General,What is the fear of vehicles known as,Ochophobia,General,Countries name means Place where one struggles with God,Israel,Music,"T-Boz, Left Eye & Chilli Make Up Which Group",TLC +General,What is the name of Dennis the Menace's dog,Gnasher,Music,Who Received Damages From The Sun After Allegations About Gatecrashing A Party,George Michael,General,At epiquarian.com you would find information about what,Food or Restaurants +General,Who were UPS original customers,Department stores,General,"What film made 58 times - cartoon, porrno, operatic, ballet",Cinderella, History & Holidays,What colour was Diana Spencer's engagement photograph suit?,Blue +General,What is all hallow's eve,Halloween,General,"What type of tree has a specimen known to be 4,764 years old",Bristlecone pine,General,What feminist wrote Sexual Politics & Flying,Kate millett + Geography,"On the London Underground, which station has a different name on two of its platforms?",Bank and Monument,General,"Who won the best Actress Oscar for ""Blue Sky""",Jessica lange,General,What is the Capital of: Russia,Moscow +Music,Who Sang The First Song Ever To Be Performed On Top Of The Pops,Dusty Springfield, History & Holidays,"Which German Grand Prix circuit was used for a Formula 1 race for the last time in 1976, when Niki Lauda was almost killed? ",N?rburgring ,General,An underground layer of water filled rock is called an,Aquifer +General,Down which street is the st patrick's day parade,Fifth avenue,Geography,In which country would you find the Yucatan Peninsula,Mexico, History & Holidays,In which two continents might you find vampire bats ,North & South America  +General,"Which single word connects a Beethoven composition, a Glenn Miller melody and some Terry's chocolates",Moonlight,General,What U.S. state includes the telephone area code 703,Virginia,Food & Drink,What type of pasta resembles a bow-tie in shape ,Farfalle  +Entertainment,Who's first release was 'Talking Heads 77'?,Psycho Killer,General,Who did the USA buy the Virgin islands from,Denmark, History & Holidays,Who Was Known As Coeur De Lion? ,Richard I (The Lionheart)  +General,If you suffered from Chirospasm what have you got,Writers Cramp,General,What U.S. state includes the telephone area code 504,Louisiana,General,Sleeping sickness is carried by which insect,Tsetse fly +General,Red headed men are more likely than others to do what,Go Bald,General,For how long is the note sustained at the end of the beatles' song 'a day in the life',Forty seconds, History & Holidays,`Expletive Deleted' came into fashion as a result of the publication of the transcript of what? ,The Watergate Tapes  +General,Which comedy duo first performed together in 1926,Laurel and hardy,Food & Drink,Which drink was created when Indian army officers added quinine to soda water to help fight malaria? ,Tonic Water ,General,"In Magnum PI,what was the name of the club?",The King Kamehameha +General,Why did sailors wear tattoos,Prevent Catching Pox,General,"What number on the Richter scale, does an earthquake have to reach to be considered major",Seven,General,What is the Capital of: Lesotho,Maseru +General,Whose last words were supposed to have been 'Let not poor Nelly starve',Charles ii,General,What made two monkeys famous in 1959,Space trip,Music,"In Which Country Wwas The Beatles Track ""Can't Buy Me Love"" recorded?",France +General,What British Motor vehicle displayed at 1948 Amsterdam show,Land Rover,General,Who did the walrus and the carpenter ask to walk with them,The Oysters,Music,Who Made Their Bill Topping Debut On Sunday Night At The London Palladium In 1963,The Beatles +General,Determination of the depth of a body of water,Sounding,Geography,In what mountain range is kicking horse pass ,Rockies ,General,What was the first country in 1824 to legalise Trade Unions,Britain +General,Which tennis star wore denim shorts during matches,Andre agassi,Entertainment,Film Title: Fahrenheit ________. (a number),451,General,"What meat is most often cut into ""noisettes""",Lamb +Entertainment,What is Kermit D Frog's girlfriend's name?,Miss Piggy,General,Who wrote the 'Odyssey',Homer,General,Who rejected the Olivia Newton John role in Grease,Marie Osmond +Music,Who Sang About Patches In 1970,Clarence Carter,General,Ochlophobia is the fear of,Crowds mobs,General,"The first successful British/American space satellite was launched in April 1962, named after a character in Shakespeare's The Tempest",Ariel +General,What is the name for a painting depicting objects such as fruit and flowers,Still life,People & Places,What is Nigel Benn Known Has In The Ring ? ,The Dark Destroyer , History & Holidays,What Was Charlie Rich Looking For In The 1970's ,The Most Beautiful Girl In The World  +General,Purl Plain Fisherman's Cable types of what,Knitting stitches, History & Holidays,Who released the Pet Shop Boys song `I'm Not Scared' in 1988 ,Eighth Wonder ,General,What is a conundrum,Riddle +General,"Game show: before she became the head-turning letter turner on wheel of fortune in 1982, vanna white appeared as a contestant on what show",The Price is Right,General,Laurence Skikne changed his name to what and found fame,Laurence Harvey,General,Which company launched the first clone of an IBM pc in 1982,Compac +Entertainment,After who was Deana Carter named?,Dean Martin,General,A receptacle for holy water in a church is a ___.?,Font,General,Mould board Disc and Rotary are types of what,Plough + Geography,What is the basic unit of currency for Sri Lanka ?,Rupee,Music,The Assembly One Hit Wonder Was A Collaboration By Which 2 Successful Artists,Vince Clarke & Feargal Sharkey,General,What would you do with a Yashmak,Wear it - it's an Arab veil +General,How many numbers are on the spinner in 'the game of life',Ten,General,What element does the chemical symbol ca stand for,Calcium,General,What was the claymation Domino's Pizza thing to avoid?,The Noid +Music,Who Sang The Theme To The Bond Movie For Your Eyes Only,Sheena Easton,General,"The name of which musical instrument means ""sound of wood""",Xylophone,General,Mitchell what is a group of birds on the ground,Flock +People & Places,Jane Caine Has A Claim To Fame As The First Person To Do What ,Speaking Clock Voice ,Music,Which Other 80's Recording Artist Did Whitney Houston Marry,Bobby Brown,General,What is a young beaver called,Kit or sometime a Kitten +Music,What Was The Biggest Selling Album Of The 80's,Michael Jacksons (Thriller),General,Beethoven's ninth symphony is nicknamed what,The Choral,General,Which Shakespeare play ends in marriage of Benedict Beatrice,Much ado About Nothing +Science & Nature," A newborn gray whale calf is an average 16 feet long. For reasons unknown, all gray whale calves are born in the warm, shallow lagoons of Baja, __________",California,Music,Which Future Member Of Duran Duran Appeared In A Persil Advert As A Child,Simon Le Bon,General,Bombardier Billy Wells was seen on many Rank films - why,Hit Gong +Sports & Leisure,How many players are there on a water polo team,Seven,General,"What single word connects the Spanish Armada, and the two TV programmes, Danger Man and The Worker",Drake,General,Who is Shadow Chancellor of the Exchequer,Francis maude +General,According to Homer Simpson what is a feline,An Elephant,General,Who is the current monarch of the Netherlands,Beatrix, Geography,What is the oldest town in Belgium?,Tongeren +Geography,What is the capital of Senegal,Dakar,General,Where is the world's oldest belltower AD 1069,St Benedict's Church Rome,General,Semiology is the study of what,Signals +Music,From What Do The Doobie Bros Take Their Name,A Marijuana Joint Known As Doobie,General,In which sport would you find the Sag Wagon,Cycling - it picks up dropouts,General,Who hosts the monza grand prix,Italy +General,Which English brewery has the oldest patent on beer,Bass Ale,General,Stanwyck what phenomenon is caused by the gravitational attraction of the moon,Tides, History & Holidays,What Is The Name Of The Bad Guy In The Movie Halloween ,Michael Myers  +People & Places,Who was The Second Man To Set Foot On The Moon ,Buzz Aldrin ,General,Stockholm is the capital of ______,Sweden,Art & Literature,Who painted the ceiling of the Sistine Chapel ?,Michelangelo +General,Which of Dicken's novels is set during the Gordon Riots,Barnaby rudge,General,What couldn't jack sprat's wife eat,Lean, Geography,On what peninsula are Spain and Portugal located?,The Iberian peninsula +Geography,Between Which 2 Countries Would You Find Lake Olirid ,Albania & Macedonia ,General,Which car company took over Mitsubishi in March 2000,Daimler chrysler,General,Where is the Star Fleet Academy located,San Francisco +General,What is schizophrenia,Hallucinations & delusions,General,"Who played the leading role in 'the good, the bad and the ugly'",Clint,General,What do cockroaches do every fifteen minutes,Fart +General,The discovery of gold in which region of Yukon provoked a rush,Klondike,General,Strontium 90 was the original name of which band,The Police, History & Holidays,What Became America's 50th State On August 21st 1959 ,Hawaii  +General,What is the largest volcano in the solar system,Olympus mons,General,What is the first name of the French painter Monet,Claude,General,Name the sea west of alaska,Bering sea +General,Which 1998 film stars Robert Redford as a hero blessed with a gift for healing,The horse whisperer,General,Which sea area is immediately south of Ireland,Fastnet,General,Zaire diverted roads to avoid disturbing communities of what,Elves +General,The word Atom comes from the Greek meaning what,Indestructible,General,In Atlanta Georgia what is it illegal to do to a giraffe,Tie it to tel pole,General,Chicago comes from a native Indian word that means what,Place that smells bad +General,Moon Soo King has represented which country at badminton,South korea,General,Stoppered glass container for wine or spirits,Decanter, Geography,What is the smallest of the Central American countries?,El Salvador +General,Illinois second largest city and a TV detective share what name,Rockford, Geography,On what mountain are four presidents' faces carved?,Rushmore,Food & Drink,What Tradionally Can You Eat When There Is An R In The Month ,Oysters  +General,"Which garden pest, which can cause great damage to lawns, is the grub of the crane fly",Leatherjacket,General,The Caspian Java Bali what became extinct in 19th Century,Tigers,General,"What one word makes sense when it precedes top, drive & rock",Hard +General,Where is the machu picchu,Peru,General,Which sport requires stones to be 'thrown' at houses,Curling,General,Who owned Rin Tin Tin in the series,Rusty +General,In what city in Georgia is it illegal to tie a giraffe to a telephone pole or street lamp?,Atlanta,Science & Nature," Alligators and __________ have something in common, at least auditorily. They can hear notes only up to 4,000 vibrations a second.",Old people,General,Which of its products did Heinz try to squeeze out in November 1999,Salad cream +General,Playing card - Raymond Shaw trance - Manchurian Candidate,Queen Diamonds,General,What gas did Joseph Priestley discover in 1774,Oxygen,General,If you landed at Mirabel airport where are you,Montreal +General,King richard the ________,Lionheart,General,Name the ancient region that corresponds roughly to modern day Tuscany,Etruria,Music,Who Plays Guitar For Genesis,Mike Rutherford +Music,When Did Sid Vicious Join The Sex Pistols,1977,General,What eats cactus branches in the galapagos islands,Tortoise,Food & Drink,What Is Sodium Chloride Better Known As ,Salt  +General,What the W. of George W. Bush stand for?,Walker,General,Who was defeated at the battle of little bighorn,George a custer,General,Middle ages Monks denied meat on fast days ate what,Rabbit Foetuses – Said were eggs +General,Parascopisim is what sexual behaviour,Voyeurism through bedroom windows,General,What language was spoken by the ancient Romans,Latin,Food & Drink,From which fish is caviar obtained ,Sturgeon  + Language,What is 'military governor' in Japanese?,Shogun,General,Oysters can do what - according to water temperature,Change sex, History & Holidays,"In America, what became the 49th state to enter the union in 1959? ",Alaska  +General,"What links The Friend, The Tablet and The Universe",Religious publications,Music,Which Trumpet Playing Jazz Singer Was Known As Satchmo,Louis Armstrong,General,Katmandu is the capital of ______,Nepal +Music,Who Sung The Theme Tune to The Bond Movie “For You're Eyes Only”?,Sheena Easten,General,What nationality was Heitor Villa-Lobos,Brazilian,General,"What was the first personal computer: Kenbak, Scelbi, or Apple1",Kenbak +General,Two angles that total 180 degrees are called _______,Supplementary,General,What is the Capital of: Austria,Vienna,Science & Nature,What Do Alligators Lay Their Eggs In? ,Sand  + History & Holidays,In the US what was known as Armistice Day until 1954? ,Veterans Day ,Music,"What Connects Supergrass, Hurricane 1, Radiohead",Oxford,General,About which family are the 'Godfather' films,Corleone +General,What is the flower that stands for: cure,Balm of gilead,General,What does the acronym 'scuba' mean,Self-contained underwater breathing,Music,Which Non Human Had The Biggets Hit Of The Year 2000 In The Uk,Bob The Builder +Religion & Mythology,Who was the Greek god of fire?,Hephaestus,General,What was Terry's surname in the television series Minder.,Mccann, Geography,Warsaw is the capital of what country?,Poland +General,Which food item gets its name from the French for melted,Fondue, Geography,What is the basic unit of currency for Slovenia ?,Tolar,Music,"Who Was Singing About ""Macarthur Park"" In 1978",Donna Summer +General,What is the Capital of: Dominica,Roseau,General,Curtis Sliwa founded what in 1979,Guardian Angels,General,Which exponent of solidarity was awarded the 1983 Nobel Peace Prize,Lech walesa +General,What is the literal translation of aardvark,Earth pig,General,Great Brother is the Chinese translation of which drug,Viagra,General,In 1969 who formed tangerine records,Ray Charles +General,What sport is sometimes called rugger,Rugby union,General,Venus has how many moons,0, History & Holidays,What date is st. patrick's day ,March 17th  + History & Holidays,"Arnold Schwarzenegger is a father trying to get the must have toy, Turbo Man, in which 1996 film ",Jingle All The Way ,General,What is 'the soul of wit',Brevity,Science & Nature, A __________ fish can swim 100 miles in a single day.,Tuna +Sports & Leisure,How is Edson Arantes Do Nascimento better known? ,Pele ,General,Bette Midler won an Oscar nomination for playing a rock singer in the style of Janis Joplin in which 1979 film,The rose,Science & Nature,How many teats does a cow have?,Four +General,Name the first web browser publicly available,NCSA Mosaic,General,"Which Famous Vehicle Of The Movies Has The License Registration Mark ""DXJ 432""",Greased Lightning,General,In the Bible in what city did Jesus perform his first miracle,Cana - John 2:1.11 Water into wine +Sports & Leisure,Hockey: The Montreal ________.,Canadians,General,Christopher Cockerill invented what in 1955?,Hovercraft, History & Holidays,What date is St Stephen's Day? ,26th December  +General,What is a group of whales,Pod,Sports & Leisure,"In Bingo , For What Is The Call 2 Fat Ladies ",88 ,General,What colour is the artist's pigment celandine?,Yellow +General,What did Alfred Hitchcock Fear?,Eggs,General,What is the chemical formula for Ozone,3,General,The French call it Pas de Calais what do the English call it,Straits of Dover +General,What organ of the body is particularly affected by hepatitis,Liver,General,What did a scuttlebutt hold,Drinking water,General,Apart from Star Trek Kirk Scott Spock Sulu (actors) what prog,The Twilight Zone +General,What links fire escapes windshield wipers bullet proof vests,Invented by women,Art & Literature,Edgar Allen Poe wrote a famous poem about this animal.,Raven,General,USA has most airports which country has second most,Australia + History & Holidays,"Who said: when i look at my children, i say lillian you should have stayed a virgin ",Lillian carter ,General,What was the name of the home of the Care Bear Cousins?,Forest of Feelings,General,What is the currency of Iran,Rial + History & Holidays,What did the Romans call Scotland? ,Caledonia ,General,What is the most profitable section in supermarkets,1 Meat - 2 Fresh veg – 3 Pet Food,General,Alfred Butta invented what in 1941 - marketed 1948,Scrabble + Geography,Nashville is the capital of ______?,Tennessee,General,For how long was the Eiffel Tower the tallest building in the world,Forty years,Toys & Games,"In which game or sport can a person be ""skunked""",Cribbage +General,Kappelkoff is the real surname of which actress,Doris day,General,"In which country is the town originally called Angostura, a place which gave its name to a herbal bitter drink",Venezuela,General,Clyde Tonbaugh discovered what planet in 1930,Pluto +Sports & Leisure,What is the international governing board of football (soccer)?,FIFA,General,Where was the first known lighthouse,Alexandria,General,Who was the first U.S. president to be photographed at his inauguration,Abraham lincoln +General,What does Amoco stand for,American oil company,General,If you have Iantronudia what turns you on,Flashing a physician, Geography,What is the basic unit of currency for Moldova ?,Leu +General,"Which group sang the song ""Original Prankster""?",Offspring,General,Which sport has a name which literally means 'gentle way',Judo,General,Which actor has been portrayed most on screen by other actors,Charlie Chaplain +General,Building started on Westminster Abbey in which year,1050,General,Which is the only Australian state capital that is not named after a person,Perth,Science & Nature,What is the world's longest snake?,Python +General,Addis Ababa is the capital of which country,Ethiopia,General,What's shredded to make sauerkraut,Cabbage,Art & Literature,Who wrote 'A Tale Of Two Cities'?,Charles Dickens +Science & Nature,If You Were Travelling At Mach II How Fast Would You Be Moving ,Twice The Speed Of Sound ,General,"In the dr seuss books, which elephant hatched an egg",Horton,General,What is traditioinal Japanese wrestling called,Sumo +Music,Which Song Was George Harrison Accused Of Plagiarising For His Hit My Sweet Lord,He's So Fine,Sports & Leisure,Who Was Tennis Player Andre Agassi Married To Between 1997 And 1999 ,Brooke Shields ,General,Pate de foie gras is made from the liver of which bird,Goose + History & Holidays,"In which country do families dress a stuffed male doll with old clothes from each member of the family and then burn it, symbolizing forgetting all the bad things of the old year? ",Colombia ,General,What's name translates from Chinese as white vegetable,Bok choy,Music,Kelly Marie Had A Short Solo Career In The Early 80's That Included Which No.1 Hit,Feels Like Im In Love +General,How to Handle a Woman came from which stage musical,Camelot,General,What name is given to a mixture of champagne and orange juice,Buck's fizz,General,The Roman roadbuilders lacked which elementary tool,Wheelbarrow +General,"To determine the percentage of alcohol in a bottle of liquor, what is divided by two",Proof,General,Large voracious tropical sea fish,Barracuda,General,Where would u look at a Snellen Chart,Opticians +General,Air Lingus is the national airline of which country,Republic of Ireland or Eire,General,Who administers martinique,France,Music,Which Singer Is Known As The Wicked,Wilson Picket +General,Who Is Reg Dwight Otherwise Known As,Elton John,Music,"Which Canadian Born Singer Was Born ""Roberta Joan Anderson""",Joni Mitchell,Toys & Games,This is the oldest board game still played (5000 yr old boards were found),Backgammon +General,Josephine Hull best supporting actress Oscar which 1950 film,Harvey,Music,Which female singer's debut album was No Angels in 1999?,Dido,General,What did model manufacturers Airfix first make,Plastic Combs + Geography,What is the basic unit of currency for Nepal ?,Rupee,General,What is a cremnophobe afraid of,Falling down stairs,General,In ancient Rome what was the tabularium?,Public Records Office +Music,What Was The Christmas Number One In 1995,Michael Jackson / Earth Song,General,"Of what does the typical man have 13,000",Whiskers,Sports & Leisure,How many players make up a field hockey team?,Eleven +General,What type of food is Fontina?,Cheese,General,In Yiddish what is your Pupik,Belly Button,Music,Who Wrote The Wedding March,Mendelssohn +General,Which branch of science is concerned with the study of matter and energy,Physics,General,Katy Cropper Was The First Woman To Win Which TV Competition?,1 Man & His Dog,General,Richard Hannay hero of the 39 steps is which nationality,Canadian +Music,Elvis Presleys Twin Brother Died At Birth What Was His Name,Jesse Garon, History & Holidays,What Was The Given To The Period From 1919 To 1933 When Alcohol Was Banned In America ,The Prohibition ,General,"Bob, Wally, Alice and Asok can be found in which strip cartoon",Dilbert +General,Who originally designed the Washington Monument,Robert mills,General,To which family of fishes does the Tuna belong,Mackerel,General,"Actress Amy Irving divorced her producer husband in 1989, who was he",Steven spielberg +Geography,Halifax is the capital of which Canadian province,Nova scotia,General,What did Britain swap Havana for with Spain in 1763,Florida,General,The CN Tower is in which Canadian city,Toronto +General,Which Famous Duo Are Members Of The Pop Group Wild Stallions,Bill & Ted,Geography,"The only three countries in the world whose names begin with ""Z"" are Zambia, Ziare and __________, all in Africa.",Zimbabwe,General,Which Tv Soap Star Played The Role Of “Emma Jackson” In Home & Away,Danni Minogue +General,Who sings 'sweet home alabama',Lynyrd skynyrd,Science & Nature,The force that brings moving bodies to a halt is _________.,Friction, History & Holidays,Who was Margaret Thatcher? ,Prime Minister of Great Britan  +General,Who played Sarah Conner in 1984s Terminator,Linda Hamilton,Music,Which Group Recorded The 1991 Album “Stars”?,Simply Red,General,Which European country is divided into areas called Cantons,Switzerland +General,What is the largest volcano in our solar system & what planet is it on,Olympus mons on mars,General,What is the literal meaning of Kangaroo,I don’t understand,Art & Literature,Who painted 'Irises'?,Vincent Van Gogh +General,What term is given to the scientific study of all aspects of the life of fungi,Mycology,General,"What type of shoes did Run-D.M.C. sing about, which were what most rappers in the early eighties were into?",Addidas,General,Who wrote the children's story Badjelly the Witch,Spike milligan +Entertainment,What is Tina Turner's real name?,Anne Mae Bullock,General,Who is Dumbella,Donald Ducks sister,General,Dakar is the capital of ______,Senegal +General,"What does ""alopecia"" refer to",Hair loss,General,What's unusual about portrait Duke of Monmouth in Nat Gallery,Its not him,General,Which Song Was Abba's Second UK Number One Hit,Mamma Mia +General,The Koh-i-Nor is a famous diamond - what does the name mean,Mountain of Light,People & Places,Who Did Churchill Refer To As A 'Half Naked Fakir' ,Mahatma Gandhi ,General,Ipsisism is what common sexual practice,Masturbation +General,What is the meaning of Ghandi,Grocer,General,What is another name for 'okra',Ladies finger's,General,"Of which island do ireland, britain, iceland and norway dispute ownership",Rockall +Science & Nature,Which moon is the second largest satellite in our solar system?,Titan,General,Married men in France use more what than their wives,Cosmetics,General,"Who lives at 10 Downing St, London",British prime minister +General,In Morse Code Which Letter Of The Alphabet Is Represented By The Sequence “Dash Dash Dot Dot”,Z,Science & Nature,What is the smallest bone in the human body?,Stirrup bone, History & Holidays,"Considered to be the last full blooded Tasmanian aborigine, (died 1876 - aged 73), her name is_____?",Truganini +General,Collective nouns - a smuck of what,Jellyfish,General,Hokusai and Hiroshige were famous Japanese what,Artists,General,Name the Motown star shot and killed by his father in 1984,Marvin Gaye +General,"What phrase meaning 'replacement or backup,' comes from the middle ages when an archer always carried an extra string in case the one on his bow broke",Second string,Entertainment,Who sang 'Islands In The Stream' with Kenny Rogers?,Dolly Parton,General,Melanophobia is the fear of,The color black +General,Which domesticated pet animal is never mentioned in the Bible,Cats,General,In the original trivial pursuit brown was what category,Geography,General,Grey and black #1 what latin word means 'little shaded area',Umbrella +General,Knickerbockers used to be the residents of where,New York,Food & Drink,In which century were bananas first sold in London? ,17th ,General,The longest section American slang dictionary what subject,Vomit +General,Which key word was removed from the Olympic charter in 1971,Amateur,General,The first American satellite was launched in which year,1958,General,According to law what must all London Taxis always carry,Bale of hay for horse +General,What slim volume began a record 161 week stint atop the hardcover fiction best sellar list in 1993,The bridges of madison county,General,When is turkey traditionally eaten in America?,Thanksgiving,Art & Literature,"Which Shakesperian play features the line ""Now is the winter of our discontent""?",Richard III + Geography,What is the capital of Moldova ?,Chisinau,General,In Portacello Idaho concealed weapons are illegal unless what,They are openly displayed,General,If you ordered Tori Udon in a Japanese restaurant you get what,Thick Noodles broth with Chicken +General,Who was the famous grandfather of Charles Darwin?,Josiah Wedgewood,General,With what type of painting was John Singer Sargent principally associated,Portraits, History & Holidays,How many years was Nelson Mandela in prison?,27 +General,What is the fear of fire known as,Pyrophobia,General,Paraskavedekatriaphobia is a fear of ______,Friday thirteenth,Music,Eddie Vedder is lead singer with which band?,Pearl Jam +General,In 1925 two men first drove round Australia in what make of car,Citroen 2 seater,General,The U.S. is made up of __ states,Fifty 50,General,"Who said ""This game is about beating the crap out of everyone""",Bears Quarterback Jim McMahon +General,Who was the English man of religion founded Society of Friends,George Fox,General,What flavours the liquor chambord,Raspberries,General,The large Hollywood sign in LA was originally?,Hollywoodland +Music,Which pop group invited you to take them “Dancing Naked In The Rain”,Blue Pearl,General,What group did earl carroll join in 1960,Coasters,General,Who owns the island of bermuda,Britain + History & Holidays,In which European city did composer Richard Wagner die in 1883? ,Venice ,General,Branch of biology concerned with the study of plants (kingdom plantae; plant),Botany,Toys & Games,How many dots are there on a pair of dice,42 +General,Sleeping sickness is carried by which insect?,Tsetse fly,General,"In the 1960s, Alan Reed and Jean Vander Pyle were the voices of which television husband and wife",Fred & wilma flintstone,Science & Nature,What Metal Is The Best Conductor Of Electricity ,Silver  +Music,Slim Whitman Stayed At No.1 In The Uk Charts For 11 Weeks With Which Ballad,Rosie Marie,General,This Canadian art-metal trio sings sci-fi themes a lot.,Rush,General,What do the broken chains at the bottom of the statue of liberty symbolise,Overthrow of tyranny +General,A wild ox,Bison,General,What religious leaders name means Sign of God,Ayatollah,General,What saint offerred jesus a handkerchief,St veronica +General,"In the film 'day of the jackal', who did edward fox play",Jackal,Music,"Who Had A Hit In 1983 With ""Hey Little Girl""",Ice Gouse, History & Holidays,Who played the monster in the 1931 film Frankenstein ,Boris Karloff  +General,Which Alan Parker film dealt with racial murders in America,Mississippi burning,General,What type of creature is the Common Darter?,Dragonfly,General,"Born In Canada In 1950 Who In The World Of Music Has The Real Name ""August Darnell""",Kid Creole (Coconuts) +General,In which forest does the River Danube rise,Black forest, History & Holidays,"In 1957, who left the music business to become a preacher. ",Little Richard ,General,On which river does Melbourne stand,Yarra +General,What is acute hasopharyngitis,A cold,General,"In ancient Greece, where were the original Olympics held?",Olympia,General,What are snacks eaten with drinks before a meal,Canapes +General,What is Dagwood's dog's name,Daisy,Science & Nature,Which Dutch Company Began Marketing Compact Disc Players In 1982 ,Philips ,General,What city is known as little havana,Miami +General,Where is area 51 generally said to be,Groom lake,General,Who owns: sprite soda,Coca-cola,General,Kakiemon is what Japanese product,Porcelain +General,Who directed the dark crystal (2 people jh fo),Jim henson frank oz,General,A Pullicologist is an expert in what,Fleas,General,In what film did the character Regan McNeil appear,The Exorcist +General,"What is the disease frequent in asia, africa and america, which is believed to result from eating polished rice",Beri beri, Geography,What is the world's second highest mountain ?,K2,General,Amomaxia is having sex where,Parked Car +General,What was Billboards hit single of the 1970s,You light up my life, History & Holidays,In 1816 which US state was admitted to the Union as the 20th state? ,Mississippi , History & Holidays,In the Christmas carol when did 'Good King Wenceslas' look out? ,Feast Of Stephen  +General,In a period of 400 years how many times does the 1st of January fall on a Sunday ?,58,General,How many red stripes are there on the American flag?,7,General,On a prescription what does QOD stand for,Take every other day +General,Haggard as what is merle haggard also known as,Okie from muskogee,General,"Who wrote ""The Naked Ape""",Desmond morris,General,When was the addressograph invented,1892 +General,What is the more common name for serigraphy,Silk screen printing,General,Most fossils are found in this type of rock,Sedimentary, History & Holidays,Which 1922 German Vampire Film Starred The Genuinely Scary Looking Max Schreck In The Title Role ,Nosferatu  +General,In Burma illegal possession of what item can mean prison,A Modem,General,What is the name for the kind of writing used by early Egyptians,Hieroglyphics,General,What type of aircraft is the p39 airacobra built by bell,Fighter +Geography,In what country is Thunder Bay,Canada,General,What does the electrical term 'dc' stand for,Direct current,Music,"Was Phil Collins ""Another Day In Paradise"" Released In 1983 Or 1989",1989 +General,What does an anemometer measure,Wind velocity,Food & Drink,Who was the roman god of wine? ,Bacchus ,People & Places,By what name is Norma Jean Baker better known as?,Marilyn Monroe +General,What is the longest river in Australia,Murray-Darling,Science & Nature,Which Insect Can Infect Humans With Malaria? ,The Mosquito ,General,Judy garland made her show business debut under what name,Baby frances + Geography,Which is the Earth's smallest continent ?,Australia,General,Where is a horses poll,Between its ears,General,In I love Lucy what was Lucy Ricardo's maiden name,McGillicuddy +General,In what Agatha Christi book does Poriot Die,Curtain,Music,"For How Many Weeks Was ""Everything I Do, I Do For You"" No.1 In The UK",16 Weeks,General,Phnom-penh is the capital of ______,Cambodia +General,In what are monocotyledon and dicotyledon terms,Botany,General,"Also called Bettas, the males of what fish species are bred in Thailand for the purpose of competitive combat, with people gambling on the matches.",Siamese Fighting Fish,General,What was the name of the once Morrissey-fronted band in the 80's?,The Smiths +General,A loofah is a type of what,Plant,General,What Country Has The Lowest Teen Pregnancy Rate In The Western World,Holland,General,The convex surface of a road,Camber +General,A duffer is Australian slang for what,Cattle Thief,General,What actor is the spokesman for the National Rifle Association,Charlton Heston, History & Holidays,Which religious group first celebrated what we've come to know as Halloween ,The Druids  +General,Richard Arkwright invented the Spinning Jenny what job had he,Barber,General,"Who wrote the line""I'll make him an offer he can't refuse""",Mario puzo,General,Where is the biggest calibre cannon?,Kremlin +General,"What are Limerick, Round Bend, Aberdeen and Octopus",Fishing Hooks,General,What was Clint Eastwood's first film as a director,Play Misty for Me,General,David Robert Hayward-Jones became famous as who,David Bowie +General,Into what ocean does the zambezi river empty,Indian ocean,Music,"From the 1980's which artists and song “So strike a pose on a Cadillac, If you want to find all the cops, They're hanging out in the donut shop, They sing and dance”?",The Bangles / Walk Like An Egyptian,Food & Drink,Where Might You Find Lactose ,In Milk  +Sports & Leisure,How Many Tournaments Constitute A Golf Grand Slam? ,4 ,General,In Japan what is a Kissaten,Coffee Shop,Science & Nature,Why Was The Psittacosaurus So Called ,It Had A Short Head With A Parrot Like Beak  +General,Who was the last astronaut to fly alone in a spacecraft,Ron evans,General,Where would you buy a Steinlager beer,New Zealand,General,Feline cats - Bovine Cows - Aquiline what,Eagle +Music,Why Does Gene Kelly Suddenly Stop In The Singing In The Rain Routine,He Notices The Policeman,Geography,"Of the twenty_five highest mountains on Earth, nineteen are in the ______________",Himalayas,Science & Nature,The smallest portion of a substance capable of existing independently and retaining its original properties is a(n) ________.,Molecule +General,"Who said ""sex appeal 50% what you got 50% they think you got""",Sophia Loren,General,What hobby was developed by the Palmer Paint company of Detriot,Painting by numbers,General,Who was the last Ferrari driver before Michael Schumaker to win the Formula 1 World Championship,Jody schechter +General,What is the square root of 65536,256,General,Quilp (A Dwarf) is a character in which Dickens novel,The Old Curiosity Shop,General,What are Australian 'Lamingtons'?,Chocolate Cakes +General,Which major U.S. city is in Dade County,Miami, Geography,What is the basic unit of currency for Colombia ?,Peso,Science & Nature, The largest known egg ever laid by a creature was that of the extinct __________ of Madagascar. The egg was 9.5 inches long. It had a volume of 2.35 gallons.,Aepyornis +General,Who was King of Mycenae and Commander of the Greek forces in the Trojan War,Agamemnon,Sports & Leisure,How Many Cards Are You Dealt In A Game Of Gin Rummy ,10 Cards ,Science & Nature,This astronomer had a metal nose,Tycho Brahe +General,"In Battle Star Galactica,What was the name of the human leader of the Cylons?",Baltar,General,Where would you see a pilcrow,New paragraph symbol,General,What does s.o.s stand for,Save our souls +Science & Nature,What Was The Name Of Alexander The Greats Famous Horse ,BucePhalus ,General,"In Greek mythology, how many heads did hydra have",Nine,General,In Boise Idaho where by law are you not allowed to fish,From a Giraffes back +General,A skulk is a group of which animals,Foxes,General,Two under par on a hole of golf is called a(n) ________,Eagle,General,Germany was split into two zones by which agreement,Yalta agreement +Sports & Leisure,What Race Takes Place On An Annual Basis Between Putney And Mortlake? ,Oxford & Cambridge Boat Race ,General,Before tennis what drew spectators to Wimbledon,Croquet,General,Cockney rhyming slang what is elephants (trunk),Drunk +General,A long thin French type of bread,Baguette,Music,"What Was The Name Of The Supergroup That Had A Hit With ""We Are The World""",USA For Africa,General,Reddish-brown colour alluding to hair,Auburn +General,Which Italian dramatist wrote the play 'Accidental Death of an Anarchist',Dario fo,General,Who was the mouse in the Krazy Kat comics,Ignatz,General,What saint came to be known as santa claus,Saint nicholas +Religion & Mythology,What were the 'Golden Apples' in Greek myth?,Apricots,General,What is the flower of January,Carnation,General,What is the winter counterpart of estivation,Hibernation +General,"What was Tom Clancy's blockbuster first novel, published in 1984?",The Hunt for Red October, Geography,In which country would you find the spectacular rock formation known as The Three Sisters?,Australia,Geography,"The Philippines is an archipelago of 7,107 islands in the ________________",Pacific ocean +General,What is the sixth day of the week,Friday, History & Holidays,"Who was the first English monarch to have a Christmas tree? (Elizabeth I, King George II, King James VI, Queen Victoria ",Queen Victoria ,Sports & Leisure,What Is The Maximum Number Of Clubs Allowed In A Golf Bag? ,14  +General,What is the fear of dependence on others known as,Soteriophobia,General,What comedienne's baby appeared on the first cover of tv guide,Lucille ball,General,Who built the hurricane aircraft?,Hawker + History & Holidays,What do historians call the journey made by Mao to the Northwest of China after Chiang Kai-Shek had driven his forces out of the South and East?,The Long March,General,Office what is the name of the film in which steven segal's character dies,Executive decision,General,Who was the first American in outer space,Alan shepard + Geography,Which city has the largest rodeo in the world?,Calgary,General,What is Britain's largest carnivorous animal,Badger,General,Who was the architect of the new Coventry Cathedral,Basil spence +General,Who had a hit in 1983 with 'True',Spandau ballet,General,Alls Well That Ends Well the original title of which classic novel,War and Peace,Food & Drink,What Is Bombay Duck ,A Dried Fish  +General,What is the name of Marty's band that trys out for the dance in Back To The Future,The Pinheads,General,Which French town is famous for Porcelain and less famous as H.Q. of the International Bureau of Weights and Measures,Sevres,Entertainment,This was the first 3-D film.,Bwana Devil +Science & Nature,What Is The Study Of Insects Called ,Entomology ,General,What do you call marine echinoderms having 5 arms extending from a central disc,Shekel,Sports & Leisure,"In sport, what do the initials PGA stand for? ",Professional Golfer's Association  +Entertainment,"His films include: Spartacus, The Vikings, and Ulysses.",Kirk Douglas,General,What are the official languages of malta,Maltese and english,General,Who tripped Mary Decker in the 1984 Los Angeles Olympics?,Zola Budd +General,Where is The Popliteal Fossa,Back of Knee,General,The world record speed for _________ is 143.08 mph,Water skiing,Science & Nature,Which Antibiotic Was The First To Be Discovered ,Penicillin  +General,"Fishes which, as adults, lie on one side of the body with both eyes on the opposite, upward-facing side of the head",Flatfish,Food & Drink,What Are The Best Selling Sweets In The UK ,Rowntrees Fruit Pastels , Geography,The Indus River flows through which country?,Pakistan +General,Which country is also the world's largest archipelago,Indonesia,General,Percy Shaw invented what in 1934,Cats eyes,Music,Who Sang Lead On The Hits Jimmy Mack & Nowhere To Run,Martha Reeves +General,What was the first film Paul Newman directed,Rachel Rachel,General,A dealer in dress accessories and sewing goods,Haberdasher,People & Places,Which Outspoken Ex Mp Shares Her Birthday with Margaret Thatcher ,Edwina Currie  +General,The composer Berlioz married Harriet Smithson in which year,1833,General,Earl D Biggers created which oriental detective (both names),Charlie Chan,General,Which horse won the 1998 Aintree Grand National,Earth summit +Science & Nature,Which Has The Larger Ears: The African Or The Indian Elephant? ,The African ,Music,What Was Gloria Gaynor's Anthemic No.1 Disco Hit,I Will Survive,General,"Who recorded ""16 candles"" in 1959",Crest +General,Who created the animated characters Wallace and Grommet,Nick Parks,General,Who played the named character in the following films: Darby's Rangers; Mister Buddwing; and Marlowe,James garner,Science & Nature,What Is The SI Unit Of Resistance ,Ohm  +General,Who wrote the Science Fiction novel The Left Hand of Darkness,Ursula LeGuin,Science & Nature,Which meteor shower occurs on the 10th October ?,Draconids, History & Holidays,Which 60's hit started with the line 'The Taxman's Taken All My Dough''? ,Kinks/Sunny Afternoon  +General,Area in which aircraft are forbidden to fly,No-fly zone,General,Figaro in what opera did cherubino serve count almaviva,Marriage of figaro,General,What weight is the lightest in Amateur Boxing,Light Flyweight + History & Holidays,"What Opened In 1955 in Anaheim, near Los Angeles, California. ",Disneyland ,General,A vestiphobe is afraid of what,Wearing Clothes,General,Satan is Lucifer but what does Lucifer mean,The Light Bearer +General,What country used the ringgit as currency,Malaysia, History & Holidays,"Theodor Geisel published an easy reading book for kids, featuring a cat. How is he more commonly known ",Dr Seuss ,General,"Who, or what is a trogon",Tropical bird +General,Which is the fourth planet from the Sun,Mars,Science & Nature,What Disease Results In The Death Of Body Tissue ,Gangrene ,General,What country's capital is caracas,Venezuela + History & Holidays,What was the name of the witch in the tv show emu's world ,Grotbags ,General,"On the show Kight Rider,what was the name of K.I.T.T.s evil double?",K.A.R.R.,General,"Smew, garganey and shoveler are all types of what bird",Duck +General,The Jaguar missile is used as a deterrant against what type of military vehicle or weapon?,Tank,General,"What's a ""mae west""",Life preserver,General,"The Police Academy film franchise ran to seven films, what was the title of the final 1994 film",Police academy: mission to moscow +General,TVs Ben Casey started with Man Woman Birth Death and what,Infinity,General,Which flag flies over the canary islands,Spanish,General,Who did orson welles play in the film 'the third man',Harry lime +Music,"Who had A Top 20 Hit With ""Over The Hills And Far Away"" In The Mid 80's",Gary Moore,General,The first merchandise item to feature Mickey Mouse was a child's school tablet in _________ ,1929,General,What is the official language in the Republic of Yemen,Arabic +General,What is nucleomitiphobia the fear of,Nuclear bombs, History & Holidays,"Who Shot Lee Harvey Oswald, The Assassin Of John F Kennedy? ",Jack Ruby ,General,What's the capital of Comoros,Moroni +Science & Nature,What is the name for the theoretical end_product of the gravitational collapse of a massive star,Black hole,General,What is the Capital of: Holy See (Vatican City),Vatican city,Science & Nature,In Which Decade Did The Post Office Introduce The Telex Service ,1930's (1932)  + History & Holidays,What started in 1849 when gold was discovered at sutter's mill ,The california gold rush ,General,"What does 'vtec', honda's trademarked acronym mean",Variable valve timing and,General,What is the main ingredient in glass,Sand +General,The annual Hackademy awards are given for what,Smoking in films,General,The first manned balloon flight was in which year,1783,Science & Nature, The great horned owl can turn its head __________,270 degrees +General,Who won the formula 1 championship after death at Monza,Jochen Rindt - 1970,General,In What Country Did The Rather Prestigious Sport Of “ Polo ” Originate?,Iran,Entertainment,Bill Justis was a studio musician when he recorded this 'sloppy' instrumental in october 1957?,Raunchy +Science & Nature,This African animal kills the most people.,Crocodile,General,Which group were derided as The poor mans Rolling Stones,Aerosmith,General,On what british sitcom was 'all in the family' based,Steptoe and son +Music,"Who has ""hair of floating sky""?",Julia,General,"What are elementary particles originating in the sun & other stars, that continuously rain down on the earth",Cosmic rays,Geography,What state is the Golden State,California +Sports & Leisure,In Which Sport Is The Espirito Santo Trophy Contested? ,Golf ,Entertainment,Who play Captian Jean-Luc Picard in Star Trek the Next Generation?,Patrick Stewart,General,The Roman Apian Way went from Rome to where,Brindisi - Brindisium + History & Holidays,The horse Khartoum plays a 'deciding' role in which classic 70's film ? ,The Godfather ,General,The world's longest tunnel connects New York and ______?,Delaware,General,"Which women injured riding, eloped with a poet, dog called Flash",Elizabeth Barret Browning +General,In Michigan it is illegal to put what on your bosses desk,A Skunk,General,Leonhard Euler was a famous name in which field?,Mathematics,General,"Which Very Successful Person In The World Of Music Married ""Renate Blauel"" In 1984",Elton John +General,Which Famous Painting Was Stolen From An Oslo Museum On The 12th Dec 1994,The Scream,Entertainment,Who wanted 'a lover with a slow hand'?,The Pointer Sisters,General,A line that touches a circle at two points is called a _____,Chord +Science & Nature,What is it that turns blue litmus paper red?,Acid,Music,With Which Famous Disco Producer Did Phil Oakley Collaborate On The Soundtrack For The Film Electric Dreams,Giorgio Moroder,General,Who sang 'another one bites the dust',Queen +General,Who is the greatest,Me,Music,Who Sang The Theme Tune To The 1989 James Bond Movie “Licence To Kill”,Gladys Knight,General,What country did Christopher Columbus insist Cuba was a part of,China +General,What U.S. City is known as The River capital of the world,Akron,General,What is the Capital of: Anguilla,The valley,People & Places,Who is Stevland Morris Hardaway better known as? ,Stevie Wonder  +General,"Which German was the first man to win the Nobel prize for Physics, doing so in 1901 for a major discovery made in 1895",Wilhelm roentgen,General,Where is the dewey classification system used,Library,Geography,Which Bridge Connects Nepal To Tibet ,The Friendship Bridge  +General,"How In The World Of Music Is ""Richard Melville Hall"" More Commonly Known",Moby,General,What is the capital of the state of Wisconsin,Madison,Food & Drink,What Is The Term Used To Describe Pasta That Is Cooked Correctly ,Al Dente  +Science & Nature,Cetology is the study of ________.,Whales,General,Ichthyophobia is a fear of ______,Fish,General,Paso what did welch's grape juice become a favorite substitute for when the 18th amendment passed,Wine +General,What phenomenon do cereologists study,Crop circles,General,Where are the Canarie Islands situated,Atlantic Ocean,General,Who led the 'kon-Tiki expedition in 1947,Thor heyerdahl + History & Holidays,Who built the Taj Mahal?,Shah Jahan,General,Bentham of what was john bentham one of the founders,Utilitarianism,Entertainment,Ian Gillain is the singer for this legendary band,Deep Purple +General,What is the name of the starfish who is considered the best friend of Spongebob Squarepants?,Patrick,General,Old superstition - a sneezing cat means what,It will rain,General,Who was born marion morrison,John wayne +Science & Nature,"Which chemical element was foremerly known as the latin ""Kalium"", hence bears the symbol ""K""?",Potassium,Art & Literature,Which Author Wrote The Book Black Beauty? ,Anna Sewell ,General,What desert wine is a normal ingredient of zabaglione,Marsala +General,Where did the mafia originate,Sicily,Geography,Which city in the Americas has the largest population? ,Buenos Aires ,Food & Drink,What is the name of the syrup drained from raw sugar ,Molasses  +General,"What is the fear of radiation, x-rays known as",Radiophobia,Food & Drink,Which brewery brews a beer called Old Peculiar? ,Theakstons ,Food & Drink,Which Scottish Dish Is Boiled In The Stomach Lining Of A Sheep ,Haggis  +General,What Australian town used to be called Stuart until 1925,Alice Springs,General,What was invented 1903 - patented 1906 G C Beilder,Photocopier,General,What does a philluminist collect?,Match box labels +Music,"In Village People, there was an American Indian a cowboy, a consrtuction worker, a biker and who else?",A Policeman,Science & Nature,Other Than The Neutron & Proton What Else Makes Up An Atom ,Electrons ,Geography,Mount victoria is the highest peak of this island country. ,Fiji  +General,What seed takes five years to yield consumable fruit,Coffee beans,General,Ronald Ross campaigned for the destruction of what,Mosquitoes - stop malaria, Language,What does S.O.S. stand for?,Save Our Souls + History & Holidays,Who became president of South Africa in 1989?,F.W. de Klerk,General,Which country produces wine in the Casablanca valley,Chile,Music,Which Singer Born In 1981 Has The Same Surname As The Edible Shoots Of The Asparagus Tree,Britney Spears +General,What two Julies won best actress Oscars for 1964 and 1965,Andrews and,General,In Singapore you can be publicly caned for failing to do what,Flush Toilet after use, History & Holidays,He was the American inventor of the Cotton Gin.,Whitney +General,Bambi was the first Disney film without what,Human characters,General,For which cartoon character was beethoven a favourite composer,Shroeder,General,On what does an artist support his canvas,Easel +General,"The Carrick Roads, a well known haven for wildlife is situated in which English county?",Cornwall,General,"The ""Chariots of Fire' Olympics took place in which year",1924,Geography,Into what body of water does the yukon river flow ,Bering sea  +General,What is the inflammation of the stomach & intestines called,Gastroenteritis,Music,Name the 40-piece orchestra led by Barry White?,Love Unlimited Orchestra,General,Where is the busiest highway in the USA - It’s a bridge,New York's George Washington +General,What Mathematical Symbol Has The Proper Name Of A Lemniscate,The Infinity Sign,General,"What collegiate sport includes 3 match periods, one shorter than the other",Wrestling,General,What is the word used to describe species that are not extinct,Extant +General,What is the most commonly used descriptive word on menus,Mom,General,What is a turtle,Terrapin,Music,"What Connects Al Hibbler, The Righteous Bros, Robson And Jerome",Unchained Melody +General,"Football Team, dallas ______",Cowboys,General,For what does the second letter 'A' stand in the initials BAFTA,British academy of film and television arts,General,Americans Call Them Garbanzo Beans- What Do We Call Them?,Chick Peas + History & Holidays,He was stabbed by Gaius Cassius Longinus.,Julius Caesar,General,Who was the only Apostle to die a natural death,Saint John, Geography,Where are the pyramids located?,Egypt +Music,"Which Union Did ""Duran Duran"" Sing About",The Union Of The Snake,General,What is the capital of new york,Albany,Science & Nature,How Many Lines Do Televisions Have On Their Screen In The UK ,625  + History & Holidays,Which American Military Academt was established in 1802 on the Hudson river ?,West Point,Music,From Which Country Do The Dance Band Cascada Originate,Germany,General,This French peasant girl led the army to victories,Joan of arc +People & Places,Which Footballer Drop Kicked A Fan At Crystal Palace ,Eric Cantona ,Science & Nature," A __________ weighs about 1,400 pounds and eats about 55 pounds of food per day.",Cow,General,Hockey the los angeles ________',Kings +General,Which couple were exiled from The Philippines in 1986,Ferdinand and imelda Marcos,General,What in business terms is the IMF,International Monetary Fund,General,"On ""Who's the Boss?"",what was Samantha's liscense plate on her first car?",SAM'S CAR +General,What passenger train once ran between London & Edinburgh,Flying scotsman,General,And which country comes second,Israel,General,What word could Ernie Bilko not say without stuttering,Million + Geography,How many stars are on the flag of New Zealand?,Four,General,What is a greyish cat with dark stripes called,Tabby,General,"Bowline, Figure Eight & Square are all types of what?",Knots +General,A research scientist is sometimes called this,Boffin,General,"What did the ancient greeks call any group of numbers more than 10,000",Myriad,General,"To err is human, to forgive___.",Divine +General,A tittiliomaniac has a compulsion to do what,Scratch,General,What is a lower than average tide normally occuring at the first and third quarters of the moon called?,Neap tide,Music,"More Of A 50's Legend, Who Crept Into The Early 60's With ""Dream Talk"" & ""Train Of Love""",Alma Cogan +Geography,"The world's longest railway is in _______. The Central Railway climbs to 15,694 feet in the Galera tunnel, 108 miles from Lima. Tourists take it to get to the ruins of Machu Picchu.",Peru,Science & Nature," Weighing approximately 13 pounds at birth, a baby caribou will double its weight in just __________",10 days,General,What's nucleomitiphobia the fear of,Nuclear bombs +Geography,What is the capital of Nauru,Yaren,General,In Arthur C Clarks Childhoods end the aliens look like what,Devils,General,What do you call the machine that cleans the ice in skating rinks,Zamboni +General,What was advertised with the slogan 'Gives a meal man appeal',Oxo,Science & Nature," The shoebill __________, native to Africa, is often compared to a statue. The bird will stand perfectly still for long periods waiting for fish to come to surface in the water.",Stork,General,Which Simpsons character has starred in such movies as 'The Erotic Adventures Of Hercules' & 'Dial M For Murderousness'?,Troy McClure +General,"R kelly sings 'if i can see it then i can do it, if i just believe it, there's nothing to it' what's the song title",I believe i can fly,General,Which general has been described as 'The Black Eisenhower',Colin powell,General,"In 1892, who raised the marriageable age for girls to 12 years old",Italy +General,What New York City's restaurant is alluded to in many stories by Damon Runyon,Lindy's,General,Country singer Hank Wangford has what profession,Gynaecologist,General,Which animal family does the mandrill belong to,Baboon +Science & Nature," Expressing recognition rather than love, Utah __________ exchange ""kisses."" By the touching of incisor teeth, they quickly confirm the identity of group members.",Prairie dogs,General,What animal produces its own sun tan lotion,Hippopotamus,Toys & Games,From what were balloons originally made?,Animal bladders +General,What is a group of this animal called: Wolf,Pack route,Sports & Leisure,Why was Mike Tyson fined $3 million by the boxing association in 1997? ,He Bit Off Evander Hollyfields Ear,Science & Nature," Ancient Egyptians believed that ""Bast"" was the mother of all cats on Earth. They also believed that cats were __________",Sacred animals +General,"Originally made in Nimes, France, this fabric was called serge denimes",Denim,Art & Literature,Who Is The Most Famous Character Ever To Be Created By Helen Fielding ,Bridget Jones ,Science & Nature,What is a corrosive substance with a pH value less than 7 called?,Acid +Geography,The nation of _______________ covers approximately the same land area as the state of Wisconsin. Yet it ranks eighth in population among all the world's countries.,Bangladesh,General,Australians call someone from where a croweater,South Australia,Science & Nature,What is the hardest bone in the human body? ,Jaw Bone  +Art & Literature,Who Wrote The Books (The Firm) And (The Pelican Brief) Both Of Which Were Made Into Films? ,John Grisham ,Sports & Leisure,In what sport is the term 'terminal speed' used?,Drag Racing,Entertainment,Secret Identities: Jay Garrick,The flash +General,"Who directed the film ""bridge over the river kwai""",David lean,General,What does RAMDAC stand for?,Random access memory digital to analogue converter,General,What does the term 'gps' mean,Global positioning system +Geography,"_________ sits on the southern coast of France, near the border with Italy, and covers 0.73 square miles (approximately 1/2 the size of New York's Central Park).",Monaco,General,What is a dogrib,A Boat,General,What country awards the Nobel peace prize,Norway +General,What world war i hero received 50 medals,Alvin york,General,"In cooking, which meat is used in the stew a Navarin",Lamb or mutton,General,In the 18th century what job did a fart-catcher do,A footman – walk behind master +General,Spring bulb with trumpet shaped yellow flowers,Daffodil,General,Cerumen is the technical name for what body part,Earwax, Geography,Kigali is the capital of ______?,Rwanda + History & Holidays,Who Released The 70's Album Entitled Kimono my House ,Sparks ,General,Which famous museum opened in london in april 1928,Madame tussaud,Science & Nature,What name is given to animals which have pouches ?,Marsupials +Science & Nature,What Type Of Insect Is A Glow Worm? ,A Beetle ,General,What original story begins Aladdin was a little Chinese boy,1001 Arabian Nights,General,In Utah in 1870s what could you get from a slot machine,Divorce - Papers cost 2.5 +General,Who wrote Northanger Abbey,Jayne Austin,General,Count de Grisly was the first to perform what trick in 1799,Saw woman in half,Sports & Leisure,Which Boxer Used To Enter The Ring To Tina Turner's ' Simply The Best''? ,Chris Eubank  +General,What was the first film to use stereophonic sound,Disney's Fantasia,People & Places,Where Is Britain's National Horseracing Museum ,Newmarket ,General,Who was the first person to swim the english channel,Captain matthew webb +Religion & Mythology,"In Egyptian mythology, who is the god of the underworld?",Osiris,General,What is the Capital of: Pakistan,Islamabad,General,Dunkin donuts' yeast-based donuts must be set to rise for how long,Forty + History & Holidays,Which is the name of the killer in A Nightmare on Elm Street ,Freddy Krueger ,General,What is the correct form of address for a foreign ambassador,His/Her,General,"Tea Who was on hand for the ""Return of the Native""",Thomas hardy +General,What is the favourite sport of the kennedy clan,Football,General,Which countries name come from the wood its first major export,Brazil,General,Danny Zuko was a main character in what film,Grease +General,Which president was responsible for the 'louisiana purchase',Thomas, History & Holidays,"Who is famous for historically riding naked on horseback through Coventry, England ?",Lady Godiva,Religion & Mythology,"Which city is sacred to Jews, Christians, and Muslims?",Jerusalem + Geography,What is the capital of Oman ?,Muscat,General,You can get 5 years in Kentucky for sending a friend what,Bottle beer or booze,General,What was Jimmy Stuarts middle name,Crane +General,Name the Italian-born American inventor whose form of hydrotherapy has become a popular facility in home or hotel,Candido jacuzzi,Science & Nature,What plant is opium derived from,Poppy,Food & Drink,Where is most of the vitamin C in fruits ,Skin  +Music,"Although The Claim Of Millions Who Is Undoubtedly ""Born In The Usa""",Bruce Springsteen,Music,Who was the only non-Beatle ever credited on a Beatles record?,Billy Preston,General,In golf the no 10 iron is usually called what,Wedge + Geography,What is the capital of Uzbekistan ?,Tashkent,General,Josef Vissarionovich Dzhugashvili became famous as who,Joseph Stalin,General,What is a group of penguins,Colony +General,What US state has the most murders,California,General,The most common colour on national flags is?,Red,General,What's gerald ford's middle name,Rudolph +General,Who was 'the postman',Kevin costner,General,"Where did churchill, roosevelt and stalin meet in 1945",Yalta,General,Who discovered victoria falls,David livingston +Geography,What is the capital of Niger,Niamey,Entertainment,"What is the destination of the plane at the end of the film ""Casablanca""?",Lisbon,Food & Drink,What is the French name of the pate made from goose or duck liver? ,Pate de Foie Gras  +Music,"Which British Rock Band Launched A Record Label Named ""Bludgeon Riffola"" In The 1980's",Def Leppard,General,What's unusual about the ink used to print money,It's magnetic,General,What is produced when a magnetic chip is put under a magnetic field,Magnetic bubble +General,Which city is the capital of the Italian region of Tuscany,Florence,General,US tennis open held at Flushing Meadows used to be where,Forest Hills,Science & Nature," The whale has the slowest metabolism of all animals. Despite its great size, it lives on one of the smallest of all creatures: the microscopic __________, found throughout the sea.",Plankton +General,Which nation has the longest predicted life expectancy for both men and women,Liechtenstein,General,Name of Shakespeare's simple constable in Measure for Measure,Elbow,Music,In 1998 A World Albums Chart Was Introduced For The First Time Which Soundtrack Was The First No.1,Titanic +General,"Who sang the song ""The Way I Am""?",Eminem,General,Who was the first Archbishop of Canterbury,St. augustine,General,In the Harry Potter books what is Aragog,Chief Spider +General,If you have a Barr test what was tested,Your Sex - in athletics,General,In which disney film is the song 'so this is love',Cinderella,General,What is the largest of the great apes,Gorilla +General,"Spectacles for entertainment, usually with allegorical or mythological themes, performed by the aristocracy in the sixteenth and seventeenth centuries, combining music, recitatives and mime.",Court ballet,Music,"How many pictures of each Beatle are on the cover of ""A Hard Day's Night""?",Five,Geography,What is the capital of Maine,Augusta +General,"Capital city of Quebec, Canada",Quebec,General,Which Famous Novel Begins With The Line “ It Was A Bright Cold Day In April And The Clocks Were Striking Thirteen ”,George Orwell 1984,General,Writer who created Hannah Massay Maggie Rowan Tillie Trotter,Catherine Cookson +Food & Drink,What Is The Most Expensive Spice ,Saffron ,General,What was Louise Joy Brown the first of,Test tube baby,General,In 1901 which brand of car was seen for the first time,Mercedes +General,Pop a 12 ounce can of soda pop contains the equivalent of how many teaspoons of sugar,Nine,General,What is the magazine of the Jehovah's Witnesses called,The Watchtower,General,Who Wrote A 2003 Children's Story Called “The English Roses”,Madonna +Music,High on the Happy Side was a No1 album for which band in 1992?,Wet Wet Wet,General,"What kind of plants are chervil, borage & thyme",Herbs,General,What is the fear of taking tests known as,Testophobia +General,What does the Latin phrase Ex Mores mean,According to Custom,General,What was the name of Sancho Panza's donkey,Dapple, Geography,Name the desert located in south-east California.,Mojave +General,What is a female calf,Heifer,Music,"""Till There Was You"" was originally from which Broadway musical?",The Music Man,General,Badlands is a feature of which American state,South dakota +General,There are over 32000 known species of what in the world,Spiders,General,Which film followed the career of athletes Eric Henry Liddell and Harold Abrahams,Chariots of fire,General,To nearest 1000 in 1800 how many wild turkeys were in Turkey,None it’s a native US bird +General,What does it say on the bottom of New Jersey license plates,Garden state,General,A 3 1/2' floppy disk measures ___ & 1/2 inches across.,Three,General,What underwater explosive missiles are shot from a submarine,Torpedoes + Geography,What is the capital of Belarus ?,Minsk,General,"In Egyptian mythology, what sort of creature was Apis",Bull,General,What type of vehicle was Charles Rolls (Rolls_Royce) in when he died,Aeroplane +General,Mao Muka Neko Pisica are what Chinese Gypsy Japan Ruman ia,Cats 4 languages,General,"Loafers, espadrilles and brogues are all types of what",Footwear,General,Which city is served by Fornebu airport,Oslo + History & Holidays,"Introduced in 46 BC by Julius Caesar, how many months was the Julian calendar divided into? ",12 ,General,Until at least 1980 the country of bhutan had no what,Telephones,General,"Where is the ""winter carnival"" held",Quebec +General,By Law - Nebraska Barbers can't do what between 7 am 7 pm,Eat Onions,General,Which countries name comes from the Arawak word for central,Cuba,General,The Wheel Spins Ethel Lina White basis for what Hitchcock film,The Lady Vanishes +General,Sociophobia is the fear of,Society,Entertainment,"R. Kelly sings: 'If I can see it then I can do it, if I just believe it, there's nothing to it'. What's the song title?",I Believe I Can Fly,General,"On Television In 1972 ""Nancy Wilkinson"" Became The First Person Ever To Do What",Win Mastermind +General,Which US vehicle company has a bulldog as its symbol,Mack trucks,Art & Literature,Who Composed The Ballet (The Nutcracker) ,Tchaikovsky ,General,The IHF govern what sport,International Handball Federation +Geography,"The longest main street in America, 33 miles in length, can be found in Island Park, __________",Idaho,People & Places,Which Norwegian explorer's ships included Fram and Maud? ,Roald Amundsen ,Food & Drink,"What drink comprises Rum, Coconut Milk and Pineapple? ",Pina Collada  + Geography,What 'I' was once Mesopotamia?,Iraq,General,From Which Country Did The Game Of Chess Originate?,India,General,Which group of people produces the Watchtower magazine?,Jehovah's Witness +General,"Around 3000 bc, what writing system originated in sumer",Cuneiform,Science & Nature,By What Process Does A Plant Convert Sunlight Into Energy? ,Photosynthesis ,Geography,What is the capital of Belgium,Brussels +General,Where is sclerotinite found,Coal,General,Lightweight tropical American wood used for making models,Balsa,General,Jeff Bezoz Is The Founder Of Which Giant Internet Based Corporation,Amazon +General,What is a group of this animal called: Oxen,Yoke drove team herd,General,What series was voted the best fiction of the 20th century,Lord of the Rings,General,Alphabetically what is the first element in the periodic table,Actinium + History & Holidays,Who Released The 70's Album Entitled Next ,The Sensational Alex Harvey Band ,General,Which creature do Eskimos (or Inuit) call a nanook,Polar bear,General,Leonato is the main character in what Shakespeare play,Much ado about Nothing +General,What Device was Invented By Rune Elmqvist & Åke Senning in 1958 ?,The Pacemaker,General,What is the fear of otters known as,Lutraphobia,Toys & Games,Atari competitor that featured better graphics.,Intellevision +Entertainment,What comic strip character is Beetle Bailey's sister?,Lois (of Hi and Lois),General,Who composed 'Peer Gynt'?,Edward Grieg,General,How much 'monopoly' money do you collect for finishing second in a beauty contest,Ten dollars +General,Honshu is the largest island of which country,Japan,General,Placophobia is the fear of,Tombstones,General,"What is the French term for ""d day""",J +General,What did Marlon Brando and George C Scott refuse,Oscars,General,"Price is right music: who recorded ""sos""",Abba,Science & Nature,What Causes The Inflammation Resulting From Touching A Stinging Nettle? ,Formic Acid  + History & Holidays,Which actress played the role of Samantha stevens in the tv show bewitched and for a bonus point who played her in the 1990 movie version ,Elizabeth Montgomery & Nicole Kidman ,Food & Drink,What is a `Blenheim Orange'? ,Eating Apple ,General,"In Greek mythology, who was oedipus' mother",Jocasta +General,Whose autobiography is entitled 'Part My Soul',Winnie mandela,Food & Drink,What ingredient is used to flavour Amaretto liqueurs and biscuits? ,Almond ,General,In Baldwin Park California where is it illegal to ride your bike,In a Swimming Pool +Science & Nature,"There are three types of rocks: metamorphic, sedimentary, and _________.",Igneous,Music,"Who Had A Hit In 1967 With ""When You're Young And In Love""",The Marvelettes,Art & Literature,"A technique of engraving, using a sharp-pointed needle, that produces a furrowed edge resulting in a print with soft, velvety lines. ",Drypoint +Geography,This section of Manhattan is noted for its Negro and Latin American residents.,Harlem, History & Holidays,Spain ceded Florida to Britain in exchange for this territory.,Cuba,General,What name did Kresge's end up with,K mart k mart kmart +General,In what game does the new york institute for the investigation of rolling spheroids specialize,Marbles,General,"Who created the characters ""The Toff"" and ""The Baron""",John creasey,General,What is the flower that stands for: return of happiness,Lily of the valley +General,"Written by Lennon and McCartney, what, in 1963 was the first rolling Stones' single to enter the top twenty",I wanna be your man,General,"What links Duke Wellington, Earl Derby, Marquis Salisbury",UK Prime Ministers,Music,Who Sung About Russians In 1985 And An Englishman In 1988,Sting +Geography,Which Bridge Connects Europe With Asia ,The Galata Bridge Over The Bosphorus In Istanbul ,General,What star's memphis home was bruce springsteen refused entry to in 1976,Elvis presley,Food & Drink,What Are The Ingredients Of A Pina Colada ,"Rum, Pineapple Juice, Coconut Milk " +General,What animals evidence is admissible in US courts,A Bloodhound,General,Westminster Abbey is dedicated to who,Saint Peter,General,How did sonny bono die,Skiing accident +General,"Who said - ""The bigger they come the harder they fall"" 1899",Bob Fitzsimmons,Food & Drink,Which fruit goes into the liqueur Kirsch? ,Cherry ,General,What is the oldest registered trade mark still used in USA,Red Devil Undewoods devilled ham +Music,Amount Beatles received to play The Ed Sullivan Show,"$3,500 ",General,Who composed the opera The Flying Dutchman in 1843,Richard wagner,General,Which painter did Hans van Meegeren most fake,Vermeer +General,"What is the Capital of: Micronesia, Federated States of",Palikir,General,What is the currency of Austria,Schilling,General,"State in the east central U.S., bordering the Ohio River",Kentucky +Geography,What is the capital of Turkey,Ankara,General,In the Flintstones Dino was Fred's pet who was Barnie's,Hoppy,Science & Nature,"What Do Squids, Snails And Oysters Have In Common? ",They Are All Molluscs  +General,Shu was an Egyptian God of what,The Air,General,Who was the third president of Singpore,Devan nair, History & Holidays,Which war did the signing of an armistice on 27th July 1953 end? ,Korea  +Music,"Which member of Guns 'n' Roses was born in Stoke, England?",Slash,General,What is the nickname for Tennessee,Volunteer state,General,"What river snakes through Germany, Austria, Slovakia, Hungary, Serbia, Romania, & Bulgaria",The danube danube danube river the danube river +General,"In the Star Trek: Deep Space Nine series, name the Ferengi owner of the bar on Deep Space Nine",Quark,General,What animal appears on the label of Levi 500 jeans,Horse,General,All PCs have a BIOS what does bios stand for,Basic Input Output System +General,Reno The assassination of what country's Archduke led to World War I,Austria,Geography,What is the capital of Rwanda,Kigali,General,What is the most reliable geyser in the world,Old faithful + History & Holidays,What Was The Name Given To Textile Workers Who Opposed Modernisation During The Nineteenth Century? ,Luddites ,General,Shrub with flowers attractive to butterflies,Buddleia,General,Eddie Slovak only American to do what 31/01/1945,Executed desertion WW2 +Music,What was John Lennon's real middle name?,Winston,Sports & Leisure,"What sport do the following terms belong to - ""Tight End & Wide Receiver""?",American Football (Gridiron),General,Who released a chart-busting album in 1976 which featured 'the lido shuffle',Boz scaggs +General,In any given 6 month period 40% of Americans are what,Affected by mental Illness,General,What is a wether,A castrated ram,General,Give another name for the Gnu,Wldebeest +General,Rudolph Valentinos movie premiere was in which year,1921,General,In 1931 what was the first live televised sporting event in the UK,The Derby, History & Holidays,Who Was The 1st wife of henry Viii ,Catherine Of Aragon  + History & Holidays,"Who was made Lord Mayor of London in 1397, 1398, 1406 and 1419? ",Richard (Dick) Whittington ,General,What is used in a field to ward off unwelcome birds,Scarecrow,General,Popular Canadian term for the Liberal Party.,Grits +General,Who were the second pair of astronauts to set foot on the moon?,Conrad and Bean,Geography,Which state is the Garden State,New jersey,Geography,This country is divided at the 38th parallel.,Korea +General,What is podobromhidrosis,Smelly feet,Science & Nature," Thinking that its parents were a camel and a leopard, the Europeans once called the animal a ""camelopard."" Today, it is called the __________",Giraffe,Sports & Leisure,What sport do the Harlem Globetrotters play?,Basketball +General,"Who recorded ""Mama Don't Lie"" in 1963",Jan bradley,Art & Literature,Who Wrote The Play (Hay Fever) ,Noel Coward ,General,These were invented - 51 years later us president got one - what,Telephone on his desk +General,Which TV series linked the real-life spouses of film stars William Holden and Natalie Wood ,Hart to Hart (Stephanie Powers and Robert Wagner) ,General,What is the international cry for help?,Mayday, Geography,In what state is Silicon Valley?,California +General,The Dogs of War took its title from which other work,Shakespeare's Julius Caesar, History & Holidays,What did British Honduras change its name to in 1973? ,Belize ,General,Official taxis in New York are what colour,Yellow +General,Which actor wore an old trenchcoat in one scene in all his films,David Niven,General,Who did charlie becker play in 'the wizard of oz',The mayor of the munchkins, History & Holidays,"How Was Mistletoe Known By The Vikings Who Discovered It, Was It Snow Berries, Love Berries, White Tear Drops Or Dung on A Stick? ",Dung On A Stick  +General,In Scandinavian mythology what is the day of final doom called,Ragnerok,General,Who was voted most popular film performer in the USA in 1926,Rin Tin-Tin,General,What kind of animal is Jormangard in Norse mythology,Serpent +Music,Who Played Lead Guitar And Sang Backing Vocals On Champagne Supernova,Paul Weller,General,Which artist painted The Fighting Temeraire,Joseph Mallard William Turner,Geography,"In Which Century Was The Canal Du Midi Opened, Connecting The Atlantic To The Mediterranean Sea ",Seventeenth (1681)  +General,In what city was Mozart born,Saltzberg,Geography,Approximately What Percentaage Of The Earths Surface IS Covered In Water ,71% ,General,Napka currency Ismara official capital which African country,Eritrea +General,Montpelier is the capital of which American state,Vermont, History & Holidays,What was Operation Sea Lion in WWII?,The Invasion of Britian,Music,"Which 3 Artists Performed On The 1987 Hit ""What Have I Done To Deserve This""","Neil Tennant, Chris Lowe, Dusty Springfield" +General,With what type of reference book is Joseph Whitaker associated,Almanack,General,In France Pate De Grives a la Provencal is made from what,Thrushes,General,Kenneth Weekes nick Ban Ban born Boston only US do what,Play Test match cricket West Indies +General,What was the name of the multi-colored cube you had to re-organize?,Rubik Cube,Science & Nature,What is the term for the path followed a by a small body around a massive body in space?,Orbit, History & Holidays,"In The Original 1984 Band Aid Song, 'Do they Know It's Christmas'', Who Sang The First Line ",Paul Young  +General,On Average a West German goes 7 days without doing what,Washing his underwear,Sports & Leisure,In Which Country Were The Summer Olympic Games Held In 2008 ,Beijing / China ,Music,Which Country Did The Bachelors Come From,Ireland +General,Whats the best known artificial international language,Esperanto,General,Who wrote Moll Flanders,Daniel defoe,Music,The Radio 1 Dick Jockey Michael Pasternak Used What Regal Pseudonym,Emperor Rosko +General,What South American country takes its name from the latin for silvery,Argentina,Science & Nature,What gives leaves their colour ?,Chlorophyll,General,"Generals Gowon, Abasanjo and Abacha have all been leaders of which African State",Nigeria +General,"What did the ""P"" in Roscoe P. Coltrane (from Dukes of Hazzard) stand for?",Purvis,General,What is the fear of poetry known as,Metrophobia,General,What is another name for Mount Godwin Austen,K2 +Music,Jenny & Lyn Beggren Are Singers With Which 90's Group,Ace Of Base,General,Who sang 'good morning to you,Mildred and patty hill,General,25% of women regularly do what,Shave off pubic hair +General,Which Arthur Miller play uses the witch trials of Salem to comment on the so called McCarthy witchhunts,The crucible,General,"What remote region of Russia borders China, Kazakhstan & Mongolia",Siberia, Geography,What is the capital of Estonia ?,Tallinn +Music,Which was the first group beginning with the letter V to have a No.1 hit in the UK?,The Village People,General,Astrophobia is a fear of ______,Stars,General,John Benyon Harris became famous as what S F writer,John Wyndham + History & Holidays,"Bashful, Doc, Dopey, Grumpy Are 4 Of The Seven Dwarfs Name The Other 3 (PFE) ","Happy, Sleepy, Sneezy ",Music,Who Performed At Live Aid In A Wheelchair Making His First Live Appearance Since Being Partially Paralysed In A Car Crash,Teddy Pendergrass,General,Which film won the best song Oscar in 1971,Shaft - The theme from Shaft +General,On which circuit is the Portuguese grand prix held,Estoril, History & Holidays,What Does Dracula Actually Mean ,Son Of The Devil Or Son Of A Dragon ,General,What is desserts backwards,Stressed +General,"The Pentagon, in Arlington, Virginia, has twice as many __________ as is necessary",Bathrooms,General,What was the name of Arnold's fish on Different Strokes?,Abraham, Geography,What is the basic unit of currency for Marshall Islands ?,Dollar +General,What animal rests in a form,Hare,General,The Primes and The Distants merged to form what group,The Temptations,General,The house of Aviz was a royal dynasty associated with which country?,Portugal +General,What Everyday Device Was First Patented In 1885 By E.J Claghorn?,The Seat Belt,Music,In 1993 Who Had A Hit With The Song “ This Is It ”?,Danni Minogue,Entertainment,Who shot Bruce Wayne's parents?,Chill +General,Thomas Caneery writer Schlinders Ark comes from what country,Australia,Science & Nature,Earth's outer layer of surface soil or crust is called the _______.,Lithosphere,Science & Nature," A plaice, a large European flounder, can lie on a checkerboard and reproduce on its upper surface the same pattern of squares, for __________",Camouflage +General,In what literary work would you find the yahoos,Gulliver's Travels,Food & Drink,What Are Cos & Density Both Types Of ,Lettuce ,General,Where was the worlds first televised baseball game,Tokyo +General,Who painted the Rockerby Venus,Velazquez,General,Randolph Crane became famous as which cowboy actor,Randolph Scott,General,Which film won the best sound effects Oscar in 1987,Robocop +General,What is the sequel to the film 'every which way but loose',Every which way,Entertainment,Whose theme song was Back In The Saddle Again?,Gene Autry's,General,What is the fear of long words known as,Sesquipedalophobia +General,Name the X man who shoots laser beams from his eyes,Cyclops,Science & Nature,What is the meaning of the name of the constellation Triangulum ?,Triangle,General,The locals call it Druk Yul - Land of the Dragon what country,Bhutan +General,"Who wrote ""valley of the dolls""",Jacqueline susann,General,Of which cambodian party was pol pot the leader,Khmer rouge,Music,"Gary Webb Had A Top Ten Hit On Both Sides Of The Atlantic With ""Cars"" How Is He Better Known",Gary Numan +General,Illustrator Sydney Paget created the trademarks of who,Sherlock Holmes Deerstalker,General,In which Spencer Tracy film was a teacher accused of teaching the theory of evolution,Inherit the wind,Music,Which Song Was A Hit For Both Elvis Presley & UB40,Can't Help Falling In Love With You +General,357 UK roads are specially marked to protect what,Toads during mating season,Geography,To what country do the Faeroe Islands belong,Denmark,General,What is the literal translation of the Latin word 'video'?,I See +Entertainment,What is Blondie's maiden name,Oop,General,What is the literal meaning of the word 'cenotaph',Empty tomb,General,What aria from Madam Butterfly is a Michelle Pfeiffer 1996 film,One fine Day +General,What is Kimogayo in Japan,National Anthem,General,Phonetically spelled out what does Esso mean in Japan,Stalled Car,General,Pentlandite is the main ore providing which metal,Nickel +Science & Nature," Of the 250_plus known species of shark in the world, only about 18 are known to be __________",Dangerous to man,General,December 73 Switzerland has 6.6 million people 81 were what,Unemployed,Toys & Games,How many dots are there on a pair of dice?,Forty two +General,What's the triangular Indian pasty containing spiced meat,Samosa,General,Verdi's opera Aida is set in what country,Egypt,General,What happened French President Fronsois Faure on dying 1899,Whore contracted so had to cut off penis +Music,Shorty Long Had A Short Solo Career With Which Tamia Motown Classic,Here Comes The Judge,Geography,What Is The Second Smallest Sovereign State In The World ,Monaco ,General,Solzhenitsyn What were Christmas tree icicles originally made from,Lead +Science & Nature,"After a bolus has been digested in the stomach, it is called ______ as it moves into the small intestine.",Chyme,General,Robert Fulton Was Ther Very First Person To Sucessfullyy test Which Form Of Transport,The Submarine,Food & Drink,Which TV soap features a short chocolate animation at the beginning of each episode? ,Coronation St.  +General,"There are more statues of ________, Lewis & Clarks female indian guide, in the U.S. than any other person",Sacajewa,General,Which Television Show Featured The First Ever Interracial Kiss To Be Broadcast On TV?,Star Trek,General,Where was entertainer john candy born,Toronto +Science & Nature,Which world champion was beaten by a machine called Deep Blue in 1997? ,Gary Kasparov (Chess) ,General,This Alcatraz inmate was provided with identity 325,Alvin karpis,General,Who Took Over As Presenter Of News At Ten After Trevor McDonald Retired In 2005,Mark Austin +Sports & Leisure,In Which Country Is The Monza Motor Racing Circuit? ,Italy ,General,The firefly depends on sight to find ______,Mate,Music,Which Trumpter Born In 1961 Enjoys Equally Successful Jazz & Classical Careers,Wynton Marsalis +General,In Wacky Races who drove the Turbo Terrific,Peter Perfect,General,Halifax is the capital of ______,Nova scotia,Science & Nature,Dogs bark. What do donkeys do?,Bray +Science & Nature,Which Is The Most Intelligent Breed Of Dog? ,The Collie ,General,Easy question: type the alphabet,Abcdefghijklmnopqrstuvwxyz,Food & Drink,Which Italian phrase is used to describe pasta cooked only until it offers a slight resistance when bitten? ,Al dente  +General,What is the name of Dr. Seuss's egg hatching elephant,Horton,General,What is a group of birds flying,Flight,General,"In mythology, who tamed the winged horse Pegasus",Bellerophon +General,In the Bible who was the father of Abraham,Terah,General,What is the name of the whale that swallowed Pinocchio,Monstro,General,What did gail borden give to the world in 1853,Condensed milk +General,How would you feel if you were forswanked,It means very tired,Geography,What is the capital of Lithuania,Vilnius,People & Places,Whaat Is Paddy Ashdown First Name ,Jeremy  +General,The national bird of India is the?,Peacock,General,What creature has seven penises assorted shapes sizes,Cockroach,Geography,The nation of ______________ has an AK_47 assault rifle on its flag.,Mozambique +General,"Who recorded the album ""nine tonite""",Bob segar,General,Brian Eno created which sound,Windows 95 start-up,Geography,What is the capital of Ecuador,Quito +General,Rodin's The Thinker is really a portrait of what Italian poet,Dante,General,What did Archie Bunker on 'All In The Family' call his son-in-law Mike?,Meathead,Food & Drink,What Type Of Food Is Pepperoni? ,Spiced Sausage  +General,"In November 1981, Diana Ross had a number one hit with the duet 'Endless Love'. Who was her co-singer",Lionel ritchie,General,What is the nearest galaxy to the Solar System?,Andromeda,General,Who wrote 'No man is an island___never send to know for whom the bell tolls; it tolls for thee',Donne +General,Absolutely pure ____ is so soft that it can be molded with the hands,Gold,Music,"""Would I Lie To You"" & ""Here Comes The Rain Again"" Were Hit Songs For Which Famous Band",The Eurythmics,General,What does a Grabatologist collect,Ties + History & Holidays,This Sioux Indian toured with Buffalo Bill's Wild West Show.,Sitting bull,Art & Literature,What was Picasso's first name? ,Pablo ,General,Delage guy delage claimed to be the first person to swim across which ocean,Atlantic ocean +Music,Cantonese Boy Was Between 2 Top Ten Hits In Which Early 80's Bands career,Japan,Science & Nature,What type of creatures are slugs and snails? ,Gastropods ,General,Ennio Morricone wrote the music for what film series,Good bad ugly etc Spaghetti westerns +Science & Nature,What Are Oak Apples? ,Wasp Eggs ,General,Which is the world's warmest sea,Red sea,General,Who wrote 'the dragons of eden',Carl sagan +General,Who was the original choice to play the terminator,O J Simpson,General,What did Emily Davidson do,Suicide under kings horse 1913,General,Where do ants live,Fornicary +Art & Literature,"Which US author penned the novels ""Of Mice and Men"" and ""East Of Eden""?",John Steinbeck,General,Complete the title of the Umberto Eco book 'The Name of the_____,Rose,General,What is the longest muscle in the human body,Sartorius +General,If You Rolled A 4 On Your First Throw In a Game Of Monopoly What Square Would You Land On,Income Tax,General,"Which sport uses the terms knuckleball, cycle, and bunt",Baseball,General,In Vancouver a city law says all cars must carry what,An Anchor – Emergency brake +General,"Dodgers what famous singer played the title role in ""the great caruso""",Mario lanza,General,What city boasts a world of coca cola pavilion featuring futuristic soda fountains,Atlanta,General,What animal has a prehensile penis,Dolphin +General,"Other than meat, what is also known as a leg of mutton",A kind of sleeve,General,The science of life,Biology,General,Whose first novel was Where Angels Fear to Tread,E m forster + History & Holidays,Who Wrote The Book Alice In Wonderland ,Lewis Carrol ,General,Which country invented Venetian Blinds,Japan,General,36-inch tall Charles Sherwood was better known as __________,General tom thumb + Geography,What is a peanut if it is not a pea or a nut?,Legume, History & Holidays,What time does the train arrive in Hadleyville ? Clue. 'A mans gotta do what a mans gotta do' ,High Noon ,General,In Christian myth man was created from dust what in Islam,Clots of Blood +General,What does a taxidermist do,Stuff animals,General,What did the big bang create,Universe,General,According to a 1997 survey what nation are the best kissers,Italian +Science & Nature,The second space shuttle was named __________.,Challenger,General,What is a government in which power is restricted to a few,Oligarchy,General,What was the Titanic's last port of call,Queenstown Cobh (1922) Cork +General,Where do the English monarchs live?,Buckingham Palace, History & Holidays,What pre-tv radio show turned film caused people to commit suicide when it was first aired?,War Of The Worlds,General,Who was born on krypton,Superman +Geography,What is the capital of Solomon Islands,Honiara,General,At her beheading Marie Antoinette wore what colour shoes,Purple,General,Any object worn as a charm may be called a(n) ______.,Amulet +Science & Nature,What metal do you get from Hematite?,Iron,General,Alamein how many Ringling brothers were there,Five,General,Where did skylab crash land,Australia +Food & Drink,In which country would you expect to be served a yoghurt-based starter called 'tzatziki'? ,Greece ,General,What name is used by Private Eye when referring to the Queen,Brenda,General,What cult film made an instant star out of a rotund rocker named Meat Loaf,The Rocky Horror Picture Show +General,Which natural phenomenon can be measured on the Mercalli scale,Earthquakes,Sports & Leisure,What Horrific Sporting Event Took Place On Saturday The 28 th June 1997 ,Evander Holyfields Ear ,Science & Nature,What is the ocean of air around the earth called?,Atmosphere +General,By law every Swiss citizen must have access to what,Personal bomb shelter,General,"Who was the lead singer and principal songwriter with the American pop group ""Bread""",David gates,General,"Which group featured in the film ""Four Weddings and a Funeral""",Wet wet wet +General,Who was the first woman to win an Oscar best actress 1928,Janet Gaynor, History & Holidays,"If one takes inflation into account, which 1960's film was the most expensive film ever made ? ",Cleopatra 'On your knees!!!' ,General,What film won the best makeup Oscar in 1988,Beetlejuice +General,Scotomaphobia is the fear of,Blindness in visual field,General,Which element derives its name from the Greek for 'bringer of light',Phosphorus,General,From which fish is caviar obtained,Sturgeon +General,What was Thin Lizzies first hit in 1973,Whiskey in the Jar,Geography,Name the largest city in Canada,Toronto,General,"Who sang the song ""Desert Rose""?",Sting +General,The effect produced when sound is reflected back is known as a(n) ____.,Echo,General,Who sat on the bench for the Simpson murder trial,Judge ito,General,In Tarka the Otter what was Old Nog,A Heron + History & Holidays,What was the name of the paperboy whose 1978 murder sparked a massive manhunt by British police? ,Carl Bridgewater , History & Holidays,Legendary magician Harry Houdini died in Detroit on Halloween in 1926 but what caused his death was it A Trick went wrong B Heart Attack or was it C Ruptured Appendix ,C Ruptured Appendix , Geography,What is the basic unit of currency for Netherlands ?,Guilder +General,What is the most popular type of holiday greeting card mailed in the U.S.,Christmas,General,In Greek Myth who solved the riddle of the Sphinx,Oedipus,Music,Frank Sinatra Topped The UK Singles Charts Just 3 Times With 3 Songs PFE,"3 Coins In A Fountain, Strangers In The Night, Something Stupid" + History & Holidays,"In 1975, what re-opened after an 8 year closure?",Suez Canal,Science & Nature,Which two initials are used to describe Chronic Fatigue Syndrome? ,ME ,General,What type of animal is a caribou,Deer +General,"When honey is swallowed, it enters the blood stream within a period of how many minutes",20 minutes,General,What links tulip balloon and flute,Types of glasses,General,In what European city is the Tower of Belem?,Lisbon + History & Holidays,In which country do some people eat long noodles to to wish for a long life? ,Japan , Geography,What is the basic unit of currency for China ?,Yuan,Music,Name The Son Of John Lennon & Yoko Ono,Sean Lennon +Music,Michael McNeil & Charles Burchill Are Members Of Which Band,Simple Minds,General,In Islamic law after having sex with a lamb mortal sin to do what,Eat its flesh,General,Monopathophobia is the fear of,Definite disease +General,In Alaska it's legal to shoot bears but illegal to do what,Wake up for photo,General,In which French city did the German Army surrender on the 7th May 1945,Rheims,General,What is a red blood cell,Erythrocyte +Tech & Video Games,In which Mega Man game did Mega Man first gain the ability to charge up his shots? ,Mega Man 4,Music,What Question Did Lenny Kravitz Ask To Take Him To No.4,Are You Gonna Go My Way,General,What is a Flemish Giant,Rabbit +General,"Brief commemorative inscription on a tomb; also, a short piece of poetry or prose lauding a deceased person",Epitaph,General,Where is John Frost bridge shown on A Bridge too Far,Arnham - it was renamed after him,General,What is the former name of Zaire,Belgian congo +General,"A _______ is the only standard international unit of measure still defined by a physical object, a bar composed of two elements",Kilogram,General,What is the red substance eastern woodland nativeAmericans used in their war paint called,Vermillion,General,What sort of body fat produces a dimpled effect on the skin,Cellulite +Sports & Leisure,Who won the 2001 FA Cup?,Liverpool,Science & Nature, February is the mating month for __________,Gray whales,General,"In ballet, the position of the arms.",Port de bras +General,Who painted Starry Night,Vincent van gogh,Music,"Which Detroit Producer Has Used Pseudonyms Such As 69 Psyche, Paperclip People & Innerzone Orchestra",Carl Craig,General,In card games what name is given to the most important suit,Trumps +Science & Nature,What Herb Do Cats Love? ,Catnip/ Cat Mint ,General,Lachanophobia is the fear of what,Vegetables,Sports & Leisure,"Straight, Eight Ball And Nine Ball Are Varieties Of Which Game ",Pool  +Music,"Who Had Hits With ""Kiss Me Deadly"" & ""Close My Eyes Forever""",Lita Ford,General,"Taking its name from the adjacent country, which channel lies between mainland Africa and Madagascar",Mozambique channel,General,A person who believes that the existence of God is not provable,Agnostic +General,Which Daniel Defoe character was born in Newgate Prison,Moll flanders,General,The Roman philosopher Seneca was tutor to which emperor,Nero,Science & Nature," Lemon sharks grow a new set of teeth every two weeks. They grow more than 24,000 new __________ every year.",Teeth + History & Holidays,What Is The Name Of The Summer Camp In The Friday The 13th Movies ,Camp Crystal Lake ,General,In Hindu Castes Brahmins were priests what were Varsyas,Farmers,General,Roy Rogers girlfriend Dale Evans rode what named horse,Buttermilk +General,Silvio Gazzangia Is Responsible For The Design And Creation Of What,The World Cup,General,Which mammal has the fewest teeth,Armadillo - none,General,What is found in one third of American homes,Scrabble +Food & Drink,What is the chief food for half the people in the world? ,Rice ,General,From which part of a tree does turmeric come,Root,Science & Nature,"The insect class ""hymenoptera"" includes ants and these colonial honey-makers.",Bees +General,Who is schroeder's favourite composer,Beethoven,General,What is an amulet,Lucky charm,Music,Which Band Did Siobhan Fahey Form After Leaving Bananarama?,Shakespeare Sister +General,"For what was the last person hanged in the american colonies september 22, 1692",Witchcraft,Music,Who is David Robert Jones better known as?,David Bowie, History & Holidays,Who presided over the trial of Jesus?,Pontius Pilate +General,"Who was the original singer of Los Lobos' 1987 hit ""La Bamba""?",Ritchie Valens,General,If you had Psygophillia what would arouse you,Contact with buttocks,General,Breakfast at Tiffanies - famous film - who wrote the book,Truman Capote +General,"Who's Autobiography Is Called ""True""?",Martin Kemp,General,In Bonanza Hoss Cartwright was afraid of what,The Dark, Geography,What Canadian city is at the west end of Lake Ontario?,Hamilton +General,What is the cardinal number for a set of 10 elephants,Ten,General,"Who was told they ""better think"" in the Blues Brothers?",Guitar Murphy, History & Holidays,Name the eighties sitcom in which Bob Ueker was upstaged by an obese butler regularly. ,Mr. Belvedere  +General,64% of American teenagers have what in their bedrooms,Television,Food & Drink,What was the weekly butter ration during the second world war? ,Four ounces ,Food & Drink,Which drink was once advertised with the slogan: 'I'm only here for the beer'? ,Double Diamond  +Entertainment,Who released a chart-busting album in 1976 which featured 'The Lido Shuffle'?,Boz Scaggs,Entertainment,"Who portrayed Moses in ""The Ten Commandments""?",Charlton Heston, History & Holidays,What Was Baron Manfred Von Richthofen Also Known As? ,The Red Baron  +Science & Nature, The average life expectancy of a rhinoceros in captivity is __________,15 years,Science & Nature,What Colour Is Sonic the Hedgehog? ,Blue ,General,The average Britain in their lifetime eats 5400 what,Bags Crisps - chips +Entertainment,What was Lucy's maiden name on 'I Love Lucy'?,McGillicuddy,Toys & Games,"In Monopoly, What is the cost to buy New York Avenue",200,General,"Which Singer/Songwriter Founded The Record Label ""Respond"" Which Lasted (1981-1986)",Paul Weller +General,In what country did the Sabines live,Italy,Music,With Which Band Was David Johansen Lead Singer,The New York Dolls,General,"In the 1938 film 'Bringing Up Baby', what was Baby",Leopard +General,What is the fear of words known as,Logophobia,Music,"In Which Year Did Rod Stewart Deliver ""Baby Jane"", 1983, 1984, 1986",1983,Entertainment,"Name the band - songs include ""Add It Up, Blister In The Sun, Kiss Off""?",Violet Femmes +General,What is a group of goats,Tribe,General,What is the 15' by 18' cell that 146 captured british officers were forced into by indian troops in the 19th century,Black hole of calcutta,General,What job did the pianist Paderewski hold in the newly created state of Poland,Prime minister + History & Holidays,In which city were the Hanging Gardens,Babylon, History & Holidays,What Was Deciphed As A Result Of The Discovery Of The Rosetta Stone? ,Hieroglyphics ,General,What is the fear of puppets pyrexiophobia known as,Pupaphobia +General,What is it when five or fewer water molecules bond tightly together in a ring,Cluster,Geography,What is the capital of South Korea,Seoul,Entertainment,What was the name of the pinball machine in the film 'Tommy'?,Wizard +General,Who was with patricia hearst the night she was kidnaped,Steven weed,General,What is a young hare called,Leveret,Food & Drink,What is the name of the irish whiskey illicitly made from barley? ,Poteen  +General,"In Greek mythology, who was the hunter who was torn apart by artemis' dogs",Actaeon,General,Bridgetown the capital of ______,Barbados,General,The Mau Mau were terrorists in which country late 50s early 60s,Kenya +General,What is new brunswicks highest point at 820 m,Mount carleton, History & Holidays,What United States president was in office during the civil war?,Abraham Lincoln,General,How many degrees are all the angles in a square,360 +General,Who sang 'that's alright mama',Elvis presley,General,In which film did henry fonda play a fallen priest,The fugitive,General,In Texarkana it’s illegal to ride a horse at night without what,Tail Lights +Music,Which Sean Maguire Track Re-Entered The Charts In 1994,Take this Time, History & Holidays,Who Was England's Manager In The 1970 Football World Cup Finals ,Sir Alf Ramsey , History & Holidays,"If an Italian gave you Smeg for a present, what would it be? ",A Kitchen Appliance  +General,Which is the largest of the Canadian Provinces and Territories,Northwest territories,General,Who starred in the film version of 'to kill a mockingbird',Gregory peck, History & Holidays,Who did Squeaky Fromme try to assassinate?,Gerald Ford +Music,Name The Owner Of Sun Records,Sam Phillips, History & Holidays,What happened at 2:56 on the 21st July 1969 ?,First Manned moon landing,General,In what part of the body are rabies injections given,Abdomen +General,Why could William Tell not have shot the apple with a crossbow,Crossbow not known 13th century,General,What does c.o.d. mean in the business world,Cash on delivery,General,Preparing to invade Japan in WW2 the US ordered 400000 what,Purple Hearts +General,Who was the losing Republican candidate in the 1964 U.S. Presidential Election,Barry goldwater,General,What is the opposite of a utopia,Dystopia, History & Holidays,Complete this quote: 'I feel the need___ the need for___' ,Speed  +General,Where is Montevideo,Uraguay,General,What is the weight at the end of a pendulum,Bob,General,In which American state is Pittsburgh,Pennsylvania +General,"What cocktail is made from triple sec, tequila and lemon or lime juice",Margarita,Music,Name Iron Maidens Famous Mascot (Depicted On The Cover Of Sanctuary) Standing Over Margaret Thatchers Decapitated Body,Eddie,General,Silvester in Germany is what day in USA / England,New Years Eve +Music,In Which 80's Band Was Craig Logan A Member,Bros,General,Which frontiersman died at the alamo,Davey crockett,General,Who was killed defending Quebec in 1759,Louis montcalm + History & Holidays,Which geographical location was the first word spoken on the moon? ,Houston ,General,To where do muslims make pilgrimage,Mecca,General,Independence who played george costanza on 'seinfeld',Jason alexander +General,What does a Erotophobe fear,Sexual Love,General,"Induction', 'compression', 'ignition' and' exhaust' are the main elements of what",4 stroke engine cycle,General,What Is The Name Of The Clock Tower That Houses Big Ben?,St Stephens Tower +General,At Roman feasts which birds tongues were delicacies,Flamingos,Geography,"The southernmost point in the 48 American states (excluding Alaska, Hawaii)?",Key west,General,What does ecg stand for,Electrocardiogram +General,The winter olympics were first held in which country,France,General,What is the headquarter of the British Metropolitan Police Force ?,Scotland Yard,General,Which Game is Played 15 a side and scores 3 or 1 points,Gaelic Football +General,"Who famously said ""if in doubt, tell the truth.""",Mark twain,General,What advertised phrase Born 1820 still going strong,Johnnie Walker,General,Name the first film to have its sequel released in the same year,King Kong - Son of Kong +Music,"""Mean Man"", ""The Real Me"" And ""Forever Free"" Are All 80's Tracks From Which US Band",W.A.S.P,Science & Nature,Which Organ's Action Is Replaced By Artifical Dialysis ,The Kidney ,General,From the milk of which animal is real Mozzarella cheese made,Buffalo +Sports & Leisure,Where Is The Irish Grand National Run ,Fairyhouse ,Entertainment,"What film starred Rosie O'Donnell, Rita Wilson and Meg Ryan?",Sleepless in Seattle,General,What U.S. state includes the telephone area code 516,New york +Music,"In The Song ""Oh What A Beautiful Morning"" From Oklohoma How High Does The Corn Grow",As High As An Elephants Eye,General,What is the fear of children known as,Pedophobia,General,Which rock is the result of limestone undergoing a metamorphic change due to heat and pressure in the earth,Marble +General,Which name has been most frequently chosen by Roman Catholic Popes,John,Entertainment,"Richard Strauss' majestic overture ""Also Sprach Zarathustra"" was the theme music for which Stanley Kubrick film?",2001 : A Space Odyessy,General,What is an enlargement of the thyroid,Goitre +General,What colour on black produces the most visible combination?,Yellow,General,What is a scimitar,A sword,General,In The 1920's What Was Designed By Cedric Gibbons & Sculpted By George Stanley?,The Oscar Statue +General,"What's the international radio code word for the letter ""H""",Hotel,Science & Nature, It takes a __________ approximately seven years to grow to be one pound,Lobster,General,What vegetable was considered a cure for sex problems in old Egypt,Radish +General,In mythology who married the beautiful maid Galatea,King Pygmalion Aphrodite life,Music,"Which Group Went Up The Charts In 1964 With ""Don't Bring Me Down""",The Pretty Things,General,Who were the first Australian group to sell a million records,The Seekers Ill never find another you +Art & Literature,Who wrote the shortest ever letter ?,Victor Hugo,General,What is a group of this animal called: Squirrel,Dray,General,The kinkajou belongs to what family of animals,Raccoon +General,What is the term for a full grown female horse,Mare,Music,Which Rolling Stones album featured a 3-D cover?,Their Satanic Majesties Request,General,What are young eels called,Elvers + History & Holidays,"""Macaulay Caulkin's folks left him """"Home Alone"""" in the original 1990 movie Where did they go? "" ",Paris ,Food & Drink,The drink Sake (sah'ki) comes from this country.,Japan,General,In Maryland it is illegal to take what to the movies,A Lion +General,What was nancy davis reagan's birth name,Anne frances robbins,Geography,What Are The 3 Components Called That Form The Earth ,"Core, Mantle, Crust ",General,What does lan stand for,Local area network +General,In cooking six drops equal one what,Dash,General,Who was the Supreme Allied Commander at the end of World War One,Marshal ferdinand foch, Geography,What is the basic unit of currency for Hong Kong ?,Dollar +General,What is the largest gland in the human body,Liver,Geography,Which European Country Is The Worlds Largest Producer Of Cork ,Portugal ,Science & Nature, Racehorses have been known to wear out __________ in one race.,New shoes +General,Which British Monarch Came To The Thrown On Christmas Day 1066 (Alternative),William The Conqueror,General,Who Was The First Woman To Be Shot By The FBI?,Bonnie Parker (Bonnie And Clyde ),Food & Drink,"What ""secco"" means on a bottle of Italian wine",Dry +General,Grace Metalious wrote which famous novel (and TV show),Peyton Place,General,Which Country Currently Holds The Record (2010) For The Hottest Temperature Ever Recorded,Libya,General,Which is the most populated continent in the world,Asia +General,When was the first telephone used,"March 10, 1876",General,Captain What two word term is considered the lowest possible temperature,Absolute,General,A riddle or a hard question,Conundrum +General,Into what ocean does the Zambezi river flow,Indian,Sports & Leisure,In which sport did Conrad Bartelski compete for Britain? ,Downhill Skiing ,General,"Which airline has its home base in Atlanta, Georgia",Delta +General,What countries official name is Bharat,India - in Hindi,General,What Booker Prize Winning Novelist Wrote The Slogan “That'll Do Nicely” For American Express?,Salman Rushdie,General,What is the Capital of: Barbados,Bridgetown +General,What represents the zodiac sign Scorpio,Scorpion,General,What's a block & tackle used for,Lifting weights,General,This small gland attached to the brain exerts a control over growth,Pituitary +General,Jonquil is a shade of what colour,Yellow,Geography,"Located 137 miles north of Rome, _____________ is the oldest and one of the smallest republics in the world.",San marino,General,What is Canada's highest waterfall,Della falls +General,Off which South American port was the Graf Spee scuttled in 1939,Montevideo,General,Instrument measuring atmospheric pressure,Barometer,Science & Nature,What Is The Smallest Cell ,The Male Sperm  +General,Which Renaissance City was severely flooded in November 1966,Florence, Geography,What is the capital of Ireland ?,Dublin,General,Which mythological King chained grapes rose water fell,Tantalus +General,In Arthurian legend who is the son of Sir Lancelot,Galahad,General,What is the 3rd book of the Bible,Leviticus,General,Franz Liszt was the farther in law of what composer,Richard Wagner +General,Cereal used as food and in spirits,Barley,General,Which country grew the first Orange,China,Music,"Who Were Rick Buckler, Bruce Foxton & Paul Weller",The Jam +General,Polish sprinter Ewa Klobukowska first do what at Kiev in 1967,Fail sex test was disqualified,General,Which current british rock/pop star was at one time a teacher,Sting,General,One death per day is caused by what,Wrong prescription / dose handwriting +Science & Nature,What Is The common Name For Acetic Acid ,Vinegar ,General,What is a group of larks,Exaltation,General,Who owns Dartmoor prison,Prince Charles +General,An average American does it 2.2 times a week – what,Visit a Supermarket,General,What element do all organic compounds contain?,Carbon,General,The numbers on what if added come to 666,Roulette wheel +People & Places,Which Politician was Nick Named Bambi ,Tony Blaire ,General,Who plays its home games at byrd stadium,University of maryland's football,Science & Nature,In which constellation would you look to find the center of The Milky Way?,Sagittarius + Geography,Which large city is on the Southeastern coast of Australia?,Sydney,General,Where is the biggest calibre cannon,Kremlin,Food & Drink,What Type Of Foodstuff Was Invited By Charles Jung ,Fortune Cookies  +General,The body of an insect is divided into how many sections,Three,Toys & Games,"Moving anti-clockwise on a dartboard, what is the number next to '4'?",Eighteen,General,Who lives in buckingham palace,English monarchs +General,"What Is The More Common Name For The Bird Traditionally Called A ""Gillhowter"" In Norfolk",Barn Owl,Entertainment,This term means to play smoothly?,Legato,Music,"Which Classic 70's Hit Features The Line ""And If I Start A Commotion I Run The Risk Of Losing You And That's Worse""?",Ever Fallen In Love +General,Kneeling cushion in church,Hassock, History & Holidays,Name any of James Dean's three films (Point For Each). ,"_Rebel without a cause, East of Eden_ _and __Giant__._ ",General,In South Dakota it's illegal to fall down and sleep where,Cheese Factory +General,Sergai Kalenikov holds the world record in what,Pig Kissing,General,What's the capital of michigan,Lansing,General,What's the decimal equivalent of the binary number 101,Five +General,"Her headline hit in 1984 was ""Girls Just Want To Have Fun""",Cyndi lauper,General,Where would you find an Orcadian,Orkney Islands,General,What is the most popular name for a boat,Obsession +Music,What was the second successive No1 for Aqua?,Doctor Jones, History & Holidays,How many astronauts manned each Apollo flight,3,General,"Where, in New Mexico, was the first atomic bomb detonated in July 1945",Alamogordo +General,"Who was Vice President to Jimmy Carter, and the Democratic nomination for the presidency in 1984",Walter mondale,General,What type of singing does Pavarotti use,Baritone, Geography,What city is the Kremlin located in?,Moscow +General,"What U.S. states were named for the Sioux word ""friend"" or ""ally""",Dakota,General,Name the Editor in Chief New York Herald sent Stanley to Africa,Gordon Bennett,General,What's the most frequently ingested mood altering drug,Caffeine + History & Holidays,What was the name of the space shuttle that exploded? ,Challenger ,General,"What kind of glass, common in baking dishes, can resist very high temperatures",Pyrex,General,Into which estuary do the trent and ouse flow,Humber +Music,Whose Trumpet Was Recognizable By The Way It Was Bent Out Of Shape,Dizzy Gillespie,General,What is interpol,International criminal police,General,Who was Queen of England for only nine days,Lady Jane Grey +General,What natural disasters are ranked in severity by the Saffir Simpson scale,Hurricanes,General,3 countries on 2 continents Russia Turkey (Asia Europe) and ?,Egypt - Africa and Asia,Science & Nature, Some species of rain forest birds migrate every summer from South America to Canada to __________,Breed +General,"A social dance popular in the nineteenth century. It was a square dance in five sections, each in a different time.",Quadrille,General,Which Italian inventor first transmitted signals across the English Channel,Marconi,General,Iolanda Balas - Romania - won 150 consecutive events - what,High Jump 1956 - 67 +General,What is another name for Nitre,Saltpetre,General,"In ballet, an open position of the feet.",Ouvert,General,What is a group of this animal called: Baboons,Troop +General,Whose backing group was The Coconuts,King Creole,Music,"Who Recorded The Album ""Music For The Masses""",Depeche Mode,Music,What Is The Highest Normal Male Voice Known As,Tenor +Science & Nature,What Is The Shaddock A Variety Of? ,Grapefruit ,General,What unit of measurement is used to rate the loudness of sound,Decibel, History & Holidays,What eighties TV show starred Tom Hanks in women's clothing? ,Bosom Buddies  +General,What is the European city of Culture for 1998,Stockholm,General,Where are a crickets ears located,Front legs,General,Hobophobia is a fear of ______,Beggars + History & Holidays,"It's the Great Pumpkin, Charlie Brown' is a critically-acclaimed and very popular animated halloween television special based on a comic strip by which American cartoonist ",Charles M. Schultz ,Science & Nature,What is the Scientific name for the eardrum?,Tympanic membrane,General,Which country always leads the opening olympic procession,Greece + History & Holidays,What do Care Bears do when fighting enemies? ,Stare , History & Holidays,Name the original comic strip Bill The Cat appeared in. ,Bloom County ,General,In the field of psychiatry this term means self_love.,Narcissism +Geography,In which continent would you find the mississippi river ,North america ,General,Leslie Lynch King became famous as who,Gerald Ford,General,Which is the largest theme resort hotel?,Lost City +General,"This weapon lends its name to a type of woman's shoe with a slender, tapered high-heel?",Stiletto,Science & Nature,What is the name of the process used by green plants for obtaining food ?,Photosynthesis,General,Artillery NCO below the rank of sergeant,Bombadier +General,A similar earlier event is known as a,Precedent,Food & Drink,Which well known fruit juice drink brand is the Maori word for 'Good Health''? ,Kia Ora ,General,Who starred in the film 'the man with two brains',Steve martin +Food & Drink,A Cappuccino Coffee Is Named After What ,Monks ,General,Shaddock is another name for what,Grapefruit,General,Tippi hedren is best known for her lead role in which film,Birds +General,Who was the first US president to be sworn in by a woman,Lyndon B Johnson – when JFK killed,General,Stuffed vine or cabbage leaves are called what,Dolmades,Art & Literature,Who Was The First Person To Be Buried At Poets Corner ,Geoffrey Chaucer  +Geography,What is the capital of Liechtenstein,Vaduz,General,What is the Capital of: France,Paris,General,What was used before the baton was invented to conduct,A Violin Bow +General,Where in the world would you find Cumbum,India,General,What is more addictive than all illicit drugs except crack and heroin combined,Cigarettes,General,Who would use a punty in their job,Glass blowers rod +General,"Which American political party which held a brief existence throughout the 1850s and 1860s, resisted immigration from Europe and supported slavery?",Knownothings,Music,Who were the only band to play at Both Woodstock and Live Aid,The Who,Toys & Games,A.k.a a pair of aces & a pair of eights,Dead mans hand +General,"Who On TV During The 1990's Had The Real Name Of ""Michael Van Wijk""",Wolf (Gladiators),Science & Nature,Acetylsalicylic acid is more commonly known as _________.,Aspirin,General,"To the nearest minute, how long does it take sunlight to reach earth",Eight +General,In Iowa 1978 Judge dismissed drink driving charge - why,Too Drunk to sample,General,The sachem is a chief of what confederation,Algonquian confederation,Music,What Is The Best Selling Film Soundtrack Of All Time In The UK,The Sound Of Music +General,Who played the part of Mildred Roper on TV,Yootha joyce,Toys & Games,What score is not possible for a cribbage hand?,Nineteen,General,"What, in Japan, is the Yakusa",Organised crime syndicates +General,"This word means ""split personality"".",Schizophrenia,General,Whose 31st and 38th Symphonies are the Paris and the Prague,Mozart,General,Who was the alter ego of 'the incredible hulk',Dr david banner + History & Holidays,Which Very Famous Horror Story Was Written By Gaston Leroux ,The Phantom Of the Opera ,General,Shakespeare's lovers Romeo and Juliet lived in which Italian city?,Verona,General,What is sally ride's scientific calling,Physics +General,Who makes maps,Cartographer,General,What is the name for a narrow necked wine or spirit container of more than two gallon capacity,Demijohn,Science & Nature,This animal is the symbol of the U.S. Democratic Party.,Donkey +General,Lake Titicaca lies in which two countries,Bolivia and peru,Art & Literature,Which Welsh Poet Died Of Alcohol Poisoning The Year He Publsihed His Collected Poems ,Dylan Thomas , Geography,What is the capital of Egypt ?,Cairo +General,What was the top single record in 1957 by debbie reynolds,Tammy,General,In which town or city was the novelist Sir Waiter Scott born,Edinburgh,General,Name the first Bond film not based on an Ian Fleming book,Goldeneye + Geography,What is the basic unit of currency for Guinea ?,Franc,General,Who was the first American to receive the Nobel Literature prize,Sinclair Lewis, History & Holidays,Who Was The First X Factor Winner To Have A Christmas Number One ,Shayne Ward  +General,What is the capital of indiana,Indianapolis,General,Who wrote the book of proverbs,Solomon,Music,What Was The Title Of Tina Turners 1991 Best Selling Album,Simply The Best +General,What was the theme song of the film The Grapes of Wrath,Red River Valley,General,Sciophobia is the fear of,Shadows,Music,"A Hit For Elvis Costello Inj 1999 Who Wrote And Had A Uk Number One With ""She"" In 1974",Charles Aznavour +General,What is the capital of the state of California,Sacramento,General,New york has the longest subway system in ______,North america,General,Truffles & mushrooms are edible _______,Fungi +General,What hat was first made in and named after a Moroccan city,Fez,General,Which czech wrote the peasant comedy opera 'the bartered bride',Bedrich,General,Who has the highest per capital consumption of cheese,France +Science & Nature,What animal has bony plates and rolls up into a ball if it is frightened?,Armadillo,General,Whit countries parliament is called The Storting,Norway,General,Who was the first woman to win an Academy Award,Janet Gaynor 1929 +General,How old is a horse when it changes from a filly to a mare,Four,Music,"Which Group Released An Album In 2007 Entitled ""Tangled Up”",Girls Aloud,General,MandM candy was first produced in 1940 for what specific group,Us +General,What is the name for a sexual disorder in which a person obtains gratification by receiving physical pain or abuse,Masochism,Entertainment,How many members are in the 'fairfield four'?,Five,General,According to Earth Medicine what's the birth totem for march,The Falcon +General,Stake put by poker players before receiving cards,Ante,General,"What is the first prime number after 1,000,000","1,000,003",General,What two word term was used to describe a cheap unsanitary restaurant,Greasy spoon +General,Carlos Menim was elected president of what country in 1989,Argentina,General,What name is given to the art of dwarfing trees,Bonsai,General,What is the fictitious name of a plaintiff,John doe +Art & Literature,His many Romantic odes include 'Ode to Melancholy' and 'Ode to a Graecian Urn',John Keats,Sports & Leisure,What Is The Highest Possible Break In A Game Of Snooker ,155 Opponent Fouls (Snookered On All Reds) Pot A Colour To Represent A Red Then A Black + 147 , Geography,Sydney is on the east coast of ______?,Australia +General,Which actor/writer born 1939 has the first names John Marwood,John Cleese,General,"Who in children's literature was told ""to begin at the beginning and end at the end""",Alice,General,"Vietnam, laos, cambodia, thailand and malaysia, were influenced by who in early times",China and india +Science & Nature,What Is The Second Most Common Element On Earth ,Silicone ,Science & Nature," The sea lion is susceptible to sunburn, and if put on board a ship, will get as seasick as a __________",Man,General,"Steve miller released a song about this, which he hoped wouldn't carry him too far away",Jet airliner +General,Name the racehorse decapitated in the Godfather,Khartoum,General,What colour M&M is most prevalent in each packet,Brown - 30%,General,Samuel Johnson's dictionary was published in what year,1755 +General,Math: The answer to a division problem is called the ___.?,Quotient,Art & Literature,The Famous Lithograph (The Scream) Was Created By Which Artist ,Edvard Munch ,General,Which medical tool was developed by Sanctorius in 1612,Thermometer +Music,"Who Performed The Main Title Song To The Film ""The Girl Can't Help It""",Little Richard, Geography,What is the basic unit of currency for United States ?,Dollar,Food & Drink, Little round chocolate candies are known as _&m's.,M +General,What illustrated sex guide did alex comfort write,The joy of sex,General,What country has the most doughnut shops per capita,Canada,General,Who is the leader of Iraq,Saddam hussein +General,What's the 1st name of Wayne Gretzky's father,Walter, History & Holidays,The Beatles Were One Of The Biggest Phenomenon's Of The 1960's But Who Was Their Manager ,Brian Epstein ,General,The average American drinks approximately how many sodas per yer,600 +Music,"""Ever So Lonely"" Was A Hit In 1982 For Whom",Monsoon,General,Khalkha Is The Largest Spoken Language Of Which Country,Mongolia,General,In what song did john lennon sleep in a bath,Norwegian wood +Sports & Leisure,Which tennis player was famous for the 'You cannot be serious'' outburst ,John McEnroe ,General,Transylvanian people believed sleeping with what was dangerous,Mouth open,General,Who first settled maryland,Lord baltimore +Music,Name the band formed by Ace Frehley after he left Kiss?,Frehley's Comet,General,Which bird is known as the Sea Parrot,Puffin,General,What building links Stacy Keach and Oscar Wilde,Reading Jail +Geography,What Is The Most Southernly Point Of Mainland Great Britain ,Lizard Point ,Science & Nature,"This plant has leaves with delicate trigger hairs, allowing it to sense and trap insects.",Venus flytrap,General,"What is the name of Sarah's brother that she is trying to save in ""The Labyrinth""?",Toby +Mathematics,The first antiderivative of acceleration is:,Velocity,General,What name is given to the lace scarf worn on the head by Spanish women,Mantilla,General,"In the tv series 'the fall guy', who played colt seavers",Lee majors +General,"Football Team, new orleans ______",Saints,General,What is the Capital of: Antigua and Barbuda,Saint john's,General,Who was Pope during World War II,Pius xii +General,Who created the fictional detective Nero Wolfe,Rex stout,General,Who created 'maudie frickett',Jonathan winters,General,"Who was known as ""The Father of the A Bomb""",Robert oppenheimer +General,What characters first appeared in Entertaining Young Gussie,Jeeves and Wooster,General,In which city is the worlds oldest museum - Ashmolian 1679,Oxford,General,"According to the Holy Bible, where did Cain go after killing Abel",Land of nod + Geography,Name the largest river forming part of the U.S. - Mexican border.,Rio Grande, Geography,What is the world's largest lake?,Caspian Sea, Geography,What is the capital of Sudan ?,Khartoum +General,"Which 3 actresses are the new ""Charlie's Angels""","Lucy liu, drew barrymore & cameron diaz",General,What was Disney's Donald Duck originally called,Donald Drake,General,Which Clint Eastwood film has the most killings (65),Where Eagles Dare +General,If you landed at Capodichino airport where are you,Naples,General,Mastigophobia is the fear of,Punishment,General,Which cities name means End of the elephants trunk,Khartoum +Music,"In Which Year Were The Following All UK Chart Hits: ""Come As You Are"" By Nirvana, ""The Crying Game"" By Boy George And Rhythm Is A Dancer"" By Snap?",1992,General,"What is the name of the Freelings' dog in ""Poltergeist""?",Ebuzz,General,What does the name Ghengis Khan mean,Very Mighty Ruler +General,Which 18th Century poet was known as 'The Bard of Ayrshire',Robert burns,Science & Nature,From Which Type Of Tree Do We Get Hardwood? ,Deciduous ,General,What is a rower who competes in an individual event,Sculler +Geography,Name the desrt located in south-east california. ,Mojave ,General,What is the technical name for the voice box,Layrnx,General,How long is an Olympic swimming pool,50 metres +General,What did Gentleman Jim do for a living,Fight,General,How was the Sword of Damocles suspended?,From a single hair,General,"In the scrolling final credits of Disney's ________, the sorcerer's name is listed as ""Yensid"" (Disney spelt backwards). ",Fantasia +General,What are swedish buns,Danishes,General,What would you do with a blue willie,Raise it - it’s a flag,General,Paper Lace' told Billy not to be a hero in which year,1974 +General,"There are 318,979,564,000 possible ways of playing just the first four moves on each side in a game of",Chess,Religion & Mythology,Which group of people elect the pope ?,Cardinals,General,Which US actor woke up when a elephant crapped on his head,William Shatner +General,"Although his career was snuffed out in the same plane crash that killed Buddy Holly, which east L.A kid had a memorable top ten hit about his girlfriend Donna",Richie Valens,General,Tegucigalpa is the capital of ______,Honduras,General,"Who recorded ""Back in Black"" in 1980",Ac/dc +General,What is a 'Blenheim Orange.',Apple,General,What is the end of the song 'it's beginning to look a lot like ______,Christmas,General,Harvey Stevens Was The First Person To Play Which Infamous Character On Film,Damien Omen +Science & Nature,Which chemical has the atomic number one? ,Hydrogen ,Music,What Was The First Aned Biggest Hit For Texas,I Don't Want A Lover,General,Which city is the title of Mozart's 36th Symphony,Prague +General,If a doctor says you have singultus what have you got,Hiccups,General,In 1880 Smiths Patent Germ Bread changed its name to what,Hovis,General,Which flower is named after the beautiful youth killed by Apollo?,Hyacinth +General,What activity burns up 140 calories per hour,Standing,Geography,Into what sea does the mackenzie river flow ,Beaufort ,General,Jeopardy whose father boxed for the iranian olympic team,Andre agassi + Language,What is the meaning of the Mercedes Benz motto 'Das beste oder nichts'?,The best or nothing,General,Caribbean plant yeilding a substance used in cosmetics,Aloe vera,General,"In 'star wars', who was c3p0's sidekick",R2d2 +General,What is a fifty year anniversary,Diamond anniversary,General,Which novel by Jane Austen features the Woodhouse family,Emma,General,What do the letters in SAM missiles refer to?,Surface-to-Air Missile +General,New Miserable Experience' was which group's first album in 1993,Gin,General,What is the function of a sheepshank knot,To shorten a length of rope,General,"Which European capital city's name translates into English as ""Merchants Haven""",Copenhagen +General,What is the capital of montana,Helena,General,"What game usually starts with 'is it animal, vegetable or mineral'",20,Entertainment,Who always tried to kill Krazy Kat,Captain marvel +General,Which Husband And Wife Pop Duo Formed In 1982 Consist Of Ben Watt & Tracy Thorn,Everything But The Girl,General,What US president spent between 11 and 15 hours asleep daily,Calvin Coolidge,Science & Nature,What is calcium oxide commonly called?,Lime +General,What Breakthrough in fashion and Sometimes Elsewhere. Was invented in 1955 by George De Mestral,Velcro,General,What is the national bird of America,Bald headed eagle,General,Which animal eats wood twice as fast when listening to heavy metal music,Termite +General,In Russia the national product is called Soldatsky what is it,Bread,Sports & Leisure,Who Was The First British Athlete To Hold The World Javelin Record? ,Fatima Whitbread ,Geography,For Which Newspaper Did Henry Stanley Work? ,The New York Herald  +Tech & Video Games,What does the 'x' mean when referring to the speed of a CD-rom (eg. 32x)?,Times (faster than standard speed),General,Half the worlds population has seen at least one what,James Bond Film,General,Who was born Doris Kappelhoff,Doris day +General,What is the regulation height for a pin in tenpin bowling,Fifteen inches,General,Who portrayed phileas fogg in around the world in 80 days,David niven,General,What boys name would be signalled in Morse by six dashes,Tom +Science & Nature,"The atomic weights in the periodic table are stated in proportion to the weight of what element, with atomic number 6?",Carbon,General,What's the name for a pen for pigs,Pigsty,Science & Nature,Which particles are emitted by cathode ray tubes?,Electrons +Art & Literature,Who Created Lord Peter Wimsey ,Dorothy L Sayers ,Geography,Into what body of water does the Danube River flow,Black sea,General,What type of animal was drooper on banana splits,Lion +General,What is the common two word name for Yuca Brevifolia,Joshua Tree,Entertainment,What film is generally considered the worst film ever made?,Attack of the Killer Tomatoes,General,"Which 'brothers' had a hit with, 'The Sun ain't gonna shine anymore'",Walker brothers +Religion & Mythology,How many children did Noah have?,Three,General,What's sporting field is 78 feet long,Tennis court,General,What is the only bone in the body unattached to any other bone,Hyoid in throat +Toys & Games,Monopoly penalty space between Baltic Ave. and Reading RR (2 wds),Income tax,Music,What Was The Best Selling Pop Song Of 2007,Leona Lewis/ Bleeding Love,General,What girls name is also a pass made by a bullfighters cape,Veronica + Geography,What is the capital of Wales?,Cardiff,General,Who sent Stanley to Africa to look for Livingstone,New York Herald,General,Who sang the original version of Blue Suede Shoes,Carl Perkins +Geography,"Four states have active volcanoes: Washington, _______________, Alaska, and Hawaii, whose Mauna Loa is the world's largest active volcano.",California,General,What is the only sign in the zodiac which doesn't represent a living thing?,Libra,General,Americans spend approximately how much each year on beer,$25 billion +General,What does a ruminant do,Chew the cud,General,Nostology is the study of what,Senility,General,Onomatophobia is a fear of _____,Names +General,What american tv show centred on the cleaver family,Leave it to beaver, History & Holidays,Who Released The 70's Album Entitled Destroyer ,KISS ,General,Benjamin Briggs captained what mystery ship,Marie Celeste +General,Which gas is used in a refrigerator,Freon, History & Holidays,The Halloween Tree' is a 1972 fantasy novel by which American author ,Ray Bradbury ,General,"In Which US State Was ""Britney Spears"" Born",Mississippi +People & Places,Which James Bond Actor Once Worked As A Coffin Polisher ,Sean Connery ,General,What does vhs stand for,Video home system,General,What's it called when time is reckoned by the position of the sun,Apparent +General,What city has the most underground stations,New york,General,What Wimbledon single champ had a part in a John Wayne film,Anthea Gibson Lukey Horse Soldiers,General,"What kind of cameras take instant, self developing photographs",Polaroid +General,What is the name of the largest moon of Jupiter,Ganymede,General,Whats the term for a mass of diffused gas & ice particles in solar orbit,Comet,Sports & Leisure,"In golf, what bird's name means two under par ",Eagle  +General,Where are the colours blue and yellow used at funerals for warding off evil spirits,Singapore, History & Holidays,Name the TV show which featured an average housewife teamed up with a secret agent. ,Scarecrow and Ms. King ,General,What city is at the head of the Nile River Delta,Cairo +General,What book is the connection between E. Nesbitt and railways,The railway children,Science & Nature,What Are Aberdeen Angus? ,Cattle ,General,A summer house giving a view,Gazebo +General,When was the first test tube baby born,1978,General,What is the fear of the ocean known as,Thalassophobia,General,Who was the Chief Designer of the AVRO Lancaster bomber aircraft,Ray chadwick +General,Which English word is a combination of the first 2 letters of the Greek alphabet,Alphabet,General,In which Australian state is Tittybong,Victoria,General,Who was the norse goddess of lust and fertility,Freya +General,"Primrose, Montgomery, Petunia, Zinnia, And Victoria Were 5 Of The 6 Children That Featured In Which TV Series",The Darling Buds Of May,General,"Which French author wrote 'The Outsider""",Albert camus,General,33% of what are fake in the USA,Blondes + History & Holidays,Who Released The 70's Album Entitled Here Come the Warm Jets ,Eno ,General,What do Indianapolis 500 winners traditionally drink in the winners circle,Milk,General,In which country did draughts (checkers) originate,Egypt +Music,"The Police sung about it in 1981, Queen sung about it in 1986 and Take That sung about it in 1992 what word did they all sing about?",Magic,General,In medieval Spain which city was noted for its quality leather,Cordova,General,What is the decrease in size of a tissue or organ due to degeneration,Atrophy +General,Who designed the largest church in the world,Michelangelo,General,Only 14% of americans say they've done this with the opposite sex. what is it,Skinny dipping,General,What technique did Patrick Steptoe and Robert Edwards pioneer,In vitro fertilization +General,This animal is armed with bony plates & rolls up into a ball if frightened,Armadillo,Sports & Leisure,What Are The Five Colours Of An Archery Target ,"Gold, Red, Blue, Black, White ",General,Which Scots-born engineer devised the loading mechanism of the rifle which was the standard weapon for British forces in both World Wars,James lee +General,Who sang 'you can call me al',Paul simon,General,Who live in Frostbite falls Minnesota,Rocky and Bullwinkle,General,As Of 2008 Which Country Now Has The Highest Suicide Rate In The World,Lithuania +Tech & Video Games,"In the Sonic the Hedgehog series, what serves as your energy? ",Rings,Food & Drink,The Italian Word Vermicelli literally means ,Little Woms ,General,What is the flower that stands for: enduring affection,Gorse +General,Only approx one third worlds population uses what regularly,Fork,Music,Which Stevie Wonder Hit Featured In The Movie “The Woman In Red”,I Just Called To Say I Love You, History & Holidays,Thanksgiving has traditionally been the start of the Christmas season in the U.S. Which U.S. President moved Thanksgiving up a week to extend the holiday shopping season? ,Frankiln Delano Roosevelt  +Food & Drink,Cocktails: Cognac (brandy) and white creme de menthe make a(n) ____________·.,Stinger,Entertainment,What is a cello's full name?,Violoncello,General,Stamps are the most collected thing in the USA what is second,Dolls +General,What is the highest commissioned rank in the Royal Air Force,Marshal of the raf,General,Who is the only Greek god in greek mythology with a mortal mother & an immortal father,Dionysus,Science & Nature,"The greyhound, along with this smaller relative, is used in the sport of coursing.",Whippet +General,What liquid is metered in most households,Water,General,Who killed kenny,They,General,The chinese were using what metal to make things as early as 300 ad,Aluminium +General,What was the first colony to legalise witchcraft,Pennsylvania,General,"A massive ensemble of hundreds of millions of stars, all gravitationally interacting, and orbiting about a common center",Galaxy,General,A babirusa is a type of___.,Pig +Science & Nature,What Was Special About The Iron Bridge At Coal Brookdale ,It Was The Worlds First Major Iron Construction ,General,Which classic dish contains strips of steak cooked in a wine sauce with sour cream,Stroganoff,General,Robodoc is the first robot to help in which common replacemant operation,Hip +General,Name 3rd cent BC Greek mathematician wrote The Elements,Euclid,General,What is the tenth month of the year,October,General,Which Italian cruise ship was attacked in 1985,Achille lauro +General,"Fandible, lateral line and dorsal fin and a part of which animal",Fish,General,Which month was named after caesar augustus,August,Science & Nature," The __________, often called ""possum,"" dates back over 45 million years.",Opossum +General,What is the distance of the Olympic Biathlon,20 kilometres,Science & Nature,Does Uranus have an aurora,Yes,General,"What Was The Title Of ""The Beatles"" Last Uk No.1 Single Before They Split",The Ballad Of John & Yoko +General,Who is Homer Simpson's brother,Herb Powell,General,Who's first person to win Wimbledon singles on a tie breaker,Evonne Cawley,General,One Flew Out Of The Cuckoos Nest Was The Final Episode In Which Long Running TV Show,The Golden Girls +People & Places,Dame Shirley Porter Is Heiress To Which Super Market Chain ,Tesco ,General,Which is the only bird that can see the colour blue,Owl,General,Who was the male star in the film True Grit,John wayne +General,James Leblanche Stewart became famous under what name,Stewart Granger,Entertainment,"What song did Aretha Franklin sing in ""The Blues Brothers""?",Think,Art & Literature,How many lines are in a sonnet?,Fourteen +General,On the banks of which river is the taj mahal,River jumna,Science & Nature,In What Units Would You Measure Frequency ,Hertz , History & Holidays,As what was Istanbul previously known?,Constantinople +Music,"Who Wrote Aretha Franklins ""Respect""",Otis Redding,General,Which writer created Sergeant Cuff,Wilkie collins,General,"How many books of the Bible, generally known as the letters of Paul, are between Romans & Philemon (inclusive)",Thirteen +General,What is the most varied species on the planet,Domesticated dog,General,"In ancient Roman architecture, a large oblong building, generally with double columns and a semicircular apse at one end. In Christian architecture, a church with a nave, apse, and aisles. ",Basilica,General,Who captained the sloop The Witch of Endor,Horatio Hornblower +Geography,_______________________ has parallel drops of only 158 and 167 feet.,Niagara falls,General,Who portrayed sherlock holmes in 14 films between 1939 and 1946?,Basil rathbone,General,What is the gardening term for a weed,Ruderal +General,What is the infinity sign called,Lemniscate,Music,What Was Voted Best Single Of 1992 By Radio 1 Listeners,Could It Be Magic,Science & Nature,Which substance takes its name from the Greek for `not flammable'? ,Asbestos  +General,What is the name of Madonna's Chiuhauha,Chiquita,General,"Who wrote ""Death of a Salesman"" in 1949",Arthur miller,General,What is the line in Labrynith that Sarah can never remember?,You have no power over me +General,Sea or salted water,Brine,General,Who was the Goddess of the rainbow,Iris,Music,Cyndi Lauper Was Born In Which Part Of New York,Queens +Geography,In which city is the coliseum located ,Rome ,Science & Nature,There Are 4 Classes Of Myriapods2 Of Which Are Pauropoda & Symphyla. What Are The 2 Better Known Classes Called ,Centipedes And Millipedes ,General,In Greek mythology a Hamadryads spirit guarded what,Trees +General,What is the largest type of warship,Aircraft carrier, History & Holidays,In which war did Florence Nightingale earn her reputation ?,Crimean War,General,What family were the last ruling house of Italy,Savoy +General,Which country is the biggest producer and exporter of mohair?,South Africa,General,"In ice hockey, what name is given to a period of play in which one team has a player temporarily suspended from the game",Power play,General,Name one type of insect belonging to the order Hymenoptera,Ant bee sawfly wasp hornet ichneumon +Music,In a standard orchestra line-up which musicians sit immediately to the conductor's left?,First Violin, History & Holidays,Halloween originated from which Pagan festival ,Samhain ,General,In the Bible what in Hebrew means Place of a Skull,Golgotha +General,"Which band comprises sisters Andrea, Sharon, and Caroline with their brother Jim",The corrs, Geography,What island is known as the Spice Island?,Zanzibar,General,What is a group of this animal called: Fish,School shoal run haul catch +General,Lenin what country is the world's biggest coffee exporter,Brazil,General,What children's character lived at Scatterbrook farm,Worzel Gummage,General,"A social dance in ¾ time, which after originating in Spain, developed in Argentina, where it was influenced by black dance style and rhytm. ",Tango +Music,What is Ozzy Osbourne's real first name,John,General,In which city is De Montfort University,Leicester,Music,Who Did The Harlem Shuffle In 1969,Bob & Earl +General,In which film did Cliff Richard sing Living Doll in 1959,Serious Charge,General,Creed Prayer Alms Fasting Pilgrimage five pillars what religion,Islam,General,Victoria was not Queen Victoria's first name - what was,Alexandrina +General,"Which 1965 Ballad Was Covered By An Amazing 1,186 Different Performers Within 10 Years Of It's Release?",Yesterday (The Beatles),General,What U.S. state song is named for a river within the state,Swanee river,General,Ron howard's directed his first film in 1977. what was it called,Grand theft +General,When did Mark and Engels write the Communist Manifesto,1848,General,Who is the roman goddess of justice,Justitia,Science & Nature," The domestic cat is the only species able to hold its tail vertically while walking. Wild cats hold their tail horizontally, or tucked between their legs while __________",Walking + History & Holidays,In which country did the Boxer Rebellion take place?,China,People & Places,Who Has The Car Number Plate 'COM1C ,Jimmy Tarbuck ,General,Dinotopia's illustrator.,James gurney +General,Whiskey made from maize and rye,Bourbon,General,Which english house had the red rose as its symbol,House of lancaster,General,In 1900 Americans did it for 9hour 20 min now its 7hour 20 what,Sleep +Geography,What is the highest mountain in Canada,Mt. logan,General,What Was The Final Single To Be Released By The Pop Duo Wham,Edge Of Heaven,General,"What would you do with ""ackee"" in jamaica",Eat it +General,What thin net of silk or Rayon named after French place it made,Tulle,General,Long necked long legged wading bird,Heron,General,Name most performed opera at London opera House since 1833,La Boheme + Geography,What is the basic unit of currency for Australia ?,Dollar,General,Which British Railway station has the greatest number of platforms,Waterloo,Music,In The World Of Music How Are “Hodges & Peacock” More Commonly Known,Chas And Dave +General,"Who was married to Francis II, Lord Darnley and The Earl of Bosworth","Mary, queen of scots",General,Hayley Turner is a well-known name in which sport?,Horse Racing (Jockey),Food & Drink,What famous product did cerebos manufacture? ,Cooking salt  +General,Where is Prince of Wales Island,Canada,Sports & Leisure,A Yokouna is a grand champion in which sport? ,Sumo ,General,Who received a Nobel Prize for Physiology in 1904 for work on conditioned reflexes,Ivan pavlov +Food & Drink,In Scotland Which Soup Is Traditionally Served On Burns Night ,Cock-A-Leekie ,Science & Nature,The North Star is also known as ________.,Polaris,General,"Whose last words were ""It's unbelievable""",Mata Hari +General,Who is popeye's girlfriend,Olive oyl,General,What is the Capital of: Georgia,T'bilisi,Music,"Who Had A One Hit Wonder With The Song ""Loving You""",Minnie Ripperton +General,Which woman has the most monuments erected in her honour?,Virgin Mary,Geography,Into what body of water does the Yukon River flow,Bering sea,General,"Who were Kate Jackson, Jacklyn Smith and Farrah Fawcett Majors",Charlie's angels +General,Who plays pacey witter on 'dawson's creek',Joshua jackson,General,Patty Hearst was kidnapped (later joined) which organisation,Symbionese Liberation Army,General,Who wrote 'Crime & Punishment' & 'The Brothers Kamarazov',Fyodor dostoevsky +Science & Nature, A King Cobra is the biggest of all poisonous snakes and can grow to over 13 feet long. A bite from a King Cobra can kill an elephant in __________,4 hours,General,The Central Perk Café appears in which TV series,Friends,Music,Who Had A Hit In 1977 With Roadrunner,Jonathan Richman & The Modern Lovers +Music,Who Underwent Treatment For Rabies Following An Incident In Which He Bit Of The Head Of A Live Bat,Ozzy Osbourne,General,Hygrophobia is the fear of,"Liquids, dampness moisture",General,What number lies between 1 & 5 on a dart board,20 +General,Topeka is the capital of ______,Kansas,General,What Was The First Country To Legalize Abortion?,Iceland,General,"What us state was named for the choctaw indian word meaning ""red man""",Oklahoma +General,Genuphobia is the fear of what,Knees,General,YKK Yoshida Kogyo Kabushibibaisha worlds largest makers what,Zips,General,In which city does Rocky live in the film of the same name,Philadelphia +General,"According to the Old Testament, he planted the first vineyard",Noah,General,"American pioneer, who was killed while defending the Alamo?",James Bowie,General,What phrase is associated with the happy face,Have a nice day +General,What year was Theodore Roosevelt born,1858,General,"In the movie ""Sixteen Candles,"" what was Sam's grandparents exchange students name?",Long Duck Dong,Sports & Leisure,Who Lit The Olympic Torch At The 2000 Sydney Olympics ,Cathy Freeman  +General,What is the only digit that has the same number of letters as its value,Four,Music,"Mel & Kim Had A Hit With The Song ""Respect Yourself"" Or Respectable",Respectable,General,In Massachusetts its illegal to put what in clam chowder,Tomatoes +Music,"Who recorded The Paul McCartney Track ""Goodbye""?",Mary Hopkin,General,What's the more common name for prepatellar bursitis,Housemaids Knee, History & Holidays,The Deaths Of 3 Pop Icons Was Known As 'The Day The Music Died'' Name The Three (PFE) ,"Buddy Holly, Richie Valens, Big Bopper " +General,What basic skill is lacking in most Royal Navy entrants,They cannot swim,General,The Island of Sheep was the last novel of what Buchan Hero,Richard Hannay,General,What country's currency is the bolivar,Venezuela + Geography,"What unit of currency will buy you dinner in Iraq, Jordan, Tunisia, and Yugoslavia?",Dinar,General,"In October of 1962, Johnny Carson succeeds him on the Tonight Show",Jack parr,Sports & Leisure,Who did 'Tennis World' name rookie of the year in 1974?,Martina Navratilova +General,"President Roosevelt had a landslide victory in 1932, who did he defeat",Herbert hoover,General,Where was volleyball invented,France,General,Fired unglazed pottery,Bisque +General,Britains say 'tarmac'; Americans say ______.,Runway,General,What did ancient Egyptians rub on their dicks to enlarge them,Crocodile shit,General,Where do Cuckoo clocks come from,Black Forest Germany +General,On what do honeybees have a type of hair,Eyes,General,On what date did america become an independant nation,July 4th 1776,General,Of which metal is azurite the ore,Copper +Geography,In what country is Mandalay,Burma,Science & Nature,What do scientists predict will happen when the universe ends?,The Big Crunch,Music,John Lennons first published book was called:,In His Own Write +Entertainment,What were Wilma Flintstone and Betty Rubble's maiden names?,Slaghoople and Mcbricker,General,"In 1943, the whole population of Naples was dusted with DDT. What organism did this kill",Lice,General,Which band leader is associated with Take the A Train,Duke ellington +General,Don Quixote was the man of La Mancha what's it in English,The Stain,General,Who is Country Music Hall of Fames only 2 time inductee,Roy rogers,General,Who was the leader of the Polish trade union Solidarity,Lech Walesa +General,Woodpecker Scalps - Porpoise Teeth - Giraffe Tails what links,All been used as money,General,In Holland what would you do with bare buttocks in the grass,Eat them mix of string navy beans,Music,Which Band Sang The Title Track To The Movie weird Science,Oingo Boingo +General,What letter on a weather map indicates a high pressure system,H, History & Holidays,"He was assassinated on Nov. 22, 1963.",John fitzgerald kennedy,Sports & Leisure,Football: The New Orleans __________.,Saints +General,Jim Thorpe won Olympic pentathlon 1912 who was fifth,General George S Patton,General,With what were queen victoria's menstrual cramps eased,Marijuana,General,Political system in which total power is vested in a single individual or a group of rulers.,Absolutism +General,In Spain what are Paradors,State owned tourist hotels,Entertainment,"Name the band - songs include ""Doctor Doctor, Hold Me Now, Don't Mess With Dr Dream""?",Thompson Twins,General,A name for energy fuelled in ways that do not harm the enviroment,Alternative +General,Pupaphobia is the fear of,Puppets pyrexiophobia, History & Holidays,What American feminist went bust as a silver dollar?,Susan B. Anthony,Science & Nature, Macaws are the largest and most colorful species of the __________,Parrot family +General,In Which US State Was The TV Show The Dukes Of Hazzard Set,Gerogie,General,"What Event Took Place On ""Gown Avenue"" In London On The 26th April 1999",The Murder Of Jill Dando,General,Where would you find Saracens Bluestones and Trilithons,Stonehenge +General,Which turn-of-the-century French dramatist is famous for his farces,Georges feydeau,Sports & Leisure,Which Former Tennis Legend Was Charged For A 10 Million Pounds Tax Evasion In 2002 ,Boris Becker ,General,Who was the roman goddess of grain,Ceres +General,"What Athenian comic poet wrote ""Lysistrata""",Aristophanes,Music,Name 2 Of The 3 Members Of Fat Les,"Keith Allen, Damien Hirst, Alex James",Sports & Leisure,"He was the NBA, MVP in 1976, 77, and 80.",Kareem Abdul-Jabbar +General,Which Island Is The Dodo Originally From,Mauritus,General,Where is tongeren,Belgium,Sports & Leisure,Which 1990 Film Was Set Against The NASCAR Racing Circuit ,Days Of Thunder  +General,What film featured dennis weaver vs a scuzzy gasoline tanker truck,The duel,General,Which was the most successful Grand National horse,Red Rum,General,Doctor Albert Hoffman Is The Inventor Of What,L.S.D +General,In Globe Arizona it's illegal to play cards in the street with who,American Indian, History & Holidays,In 1960 Penguin Books Announced That They Would Be Publishing Which Controversial Novel ,Lady Chatterlys Lover ,Food & Drink,What Is The Name Given To The Flat Indian Bread Resembling A Pancake? ,Chapati  +General,In traditional anniversaries what is given for the thirtieth,Pearl,Food & Drink,Which country would you associate with the dish Couscous? ,Tunisia ,General,Who starred as the Six Million Dollar Man,Lee Majors +Geography,In what city is the Smithsonian Institute,Washington,General,U.S. Captials - West Virginia,Charleston,Geography,What Is The Largest US State By Area ,Alaska  +General,Where in the body are the adenoids,Nose,General,When is Halley's comet next due to appear,2062,General,What sport involves bats & flies,Baseball +Sports & Leisure,In Which Sport Would You Expect To Find A Short Stop ,Baseball ,Science & Nature,Which Curved Billed Bird Is The Largest European Wader? ,Curlew , Language,From which language does the term 'Mayday' come?,French +General,In Halstead Kansas doing what is illegal at the airport,Walking and loud burping,General,What is the capital of burkino faso,Ouagadougou,Science & Nature, A baby __________ is about six feet tall at birth.,Giraffe + Geography,What is the capital of Brazil?,Brasilia,General,Which organ is inflamed in the condition known as Cholecystitis?,Gall Bladder,General,In The Fim Madasgascar Who Voiced The Character Of Maty The Zebra,Chris Rock +General,Which capital city stands on the north shore of the river Plate estuary,Montevideo,General,What is measured with a hygrometer,Humidity,General,The 42 string guitar is correctly called what,Pikasso guitar +General,This African animal kills the most people,Crocodile,General,What is the capital city of Azerbaijan,Baku,General,What is a pismire,An Ant + History & Holidays,What was the D-Day invasion password?,Mickey Mouse,General,Who live at 742 Evergreen Terrace,The Simpson's,General,The Bosman ruling was a legal decision relating to which sport,Football +General,Who sculpted the four lions in Trafalgar Square,Sir Edwin Landseer,Music,Which Singing Diva Flopped At The Box Office With Her Debut Movie “Glitter”,Mariah Carey,General,What are bactrians and dromedaries,Camels (one hump or two) +Music,"Name Three Of The Songs Featured On The 1986 Release ""It's It's The Sweet Mix""","Blockbuster, Fox On The Run, Teenage Rampage, Hell Raiser, Ballroom Blitz",General,In Quitman Georgia its illegal for a chicken to do what,Cross the Road,General,The Houston Astrodome was opened in what year,1965 +General,Who discovered x-rays,Wilhelm roentgen,General,"Churchill, Iroquois, Owen and Smiths are all what",Waterfalls,General,"What's the hebrew word for ""proper""",Kosher +General,In which sport would they use the term straight handle,Curling - stone sent no curve,General,Which Country Publishes More Books Per Head Than Any Other,Iceland,General,Germans call a WW1 sea fight Battle of Skagerrak what in UK,Battle of Jutland + History & Holidays,Moondance' was a hit for which male singer ,Van Morrison ,General,King James the IV practiced what (and charged) on subjects,Dentistry,General,"In music, instrumental introduction to an opera or other musical or nonmusical dramatic work",Overture +General,How many gigawatts of electricity did Doc Brown need to generate to power the delorion in back to the future?,1.21 gigawatts!!,General,What does an vexillologist study,Flags,General,For which series of films did Talbot Rothwell write most of the scripts,The carry on films +General,Which chemical compound in the body is linked to hardening of the arteries,Cholesterol,Science & Nature,What is the main component of air?,Nitrogen,Entertainment,Who is Snoopy's arch enemy,The Red Baron +General,What does a hippophobic fear,Horses,General,"On what date did ""The Wall"" fall?",1989,General,Black Lady - the Queen of Spades is worth 13 - is based on this game..,Hearts +General,What were Club Nouveu originally known as?,Timex Social Club,General,What did Sir Humphry Davy say was his best discovery,His student Michael Faraday,Music,Of Which Group Is Jet Harris A Long Standing Member,The Shadows +General,People with hypertrichosis have lots of what,Hair,General,Who created the fictional detective Cordelia Gray,P d james,General,Which materials name comes from the French for rag,Chiffon +General,Collective nouns - A bloat of what,Hippotomi,General,A small shop selling fashionable clothes,Boutique,General,What is a natterjack,Toad +General,What is 'au contraire' in english,On the contrary,General,What Was The Name Of The Cab Firm Owned By Ali & Mehmet In Eastenders,Oz Cabs (Osman),General,"Dish consisting of flavoured mince, chilli and beans",Chilli con carne +General,What state is only part of the united states by treaty,Texas,General,"In what sport does one have luffing, gybing and bearing away",Yachting,Sports & Leisure,What Type Of Race Got It's Name From A Short Dash Between 2 Churches ,Steeple Chase  +Geography,If James Bond was to dial 007 he would be using the dialing code for which country? ,Russia ,Entertainment,Who sang 'All Right Now'?,Free,General,Who was the court portrait painter of Henry the Eighth,Hans Holbein +General,What is the art of creating decorative shapes by trimming trees and shrubs?,Topiary,General,What is the capital of utah,Salt lake city,General,Which boxer broke Muhammed Ali's jaw,Ken norton +General,What is special about the hooded pitohui bird (New Guinea),It’s highly poisonous,Music,Who Composed The Pink Panther Theme,Henry Mancini, Geography,Montevideo is the capital of ______?,Uruguay +General,Who played the part of the Headmistress in the film 'Blue Murder at St.Trinians,Alistair sim,General,In 1797 3 pence could buy you a good (second hand) what,Wife,General,"Which Italian Composer Wrote The Operas Rigoletto, La Traviata & Falstaff ?",Verdi (Giuseppe Verdi) +General,"What is the first name of webster, the man who published a dictionary still used today",Noah,General,In Edward Lear's poem who married the owl and the Pussy-cat,Turkey,General,In what state is silicon valley,California +General,Mickey Rooney made a series of films based on what family,Hardy family,General,Who was the 30th president of the U.S.,Calvin coolidge,General,In which European capitol city is a cannon fired at 1.00 pm daily,Edinburgh +General,"What 19th century war between russia and england, turkey, britain and france, was named for a peninsula in the black sea",Crimean war,General,Back of horse's leg where hair grows above hoof,Fetlock,Entertainment,Where did George Harrison discover gold?,Witwatersrand +General,Who was America named after ?,Amerigo Vespucci,Science & Nature,In what was the strength of early lasers measured?,Gillettes,General,Lack of which chemical compound in the body is linked with Parkinsons disease,Dopamine +General,What city is Canada's steel capital,Hamilton,General,"Countries of the world:north eastern Europe, the capital is Tallinn",Estonia, History & Holidays,In 1840 London Sweet Maker Tom Smith Came Up An Invention Still Hugely Popular Today What Was It? ,The Christmas Cracker  +General,Pediculophobia is the fear of,Lice,General,"Which term for an important person, stems from the large headgear once worn by the rich?",Bigwig,General,What book featured Topsy who growed,Uncle Toms Cabin –H B Stowe + Geography,What is the second largest state in the USA?,Texas,General,Which ancient Greek astronomer is credited with having compiled the first known star catalogue?,Hipparchus, History & Holidays,"Born on Christmas Day 1954, who was the female half of the 80's band 'The Eurhythmics'' ",Annie Lennox  +General,Why were Mothers called mama or mommy in many languages,From mammary or tits,General,In what city does Matlock take place?,"Atlanta,Georgia",General,"In the Christian calendar, what is the alternative name for the Feast of Pentecost",Whitsun +General,Hibernia was the Roman name for which country,Ireland,General,Year of the first manned space flight,1961,General,Which Company Is An Offshoot Of Disney Makes Movies For Adults?,TouchStone Pictures +General,Who does the voiceover at the end of Michael Jackson's Thriller,Vincent Price,Entertainment,Country singer Vince ____?,Gill,Music,What did Pat Benatar sing before she went into Rock music?,Opera + History & Holidays,What famous artist could write with both his left and right hand at the same time?,Leonardo da Vinci,General,Plant based compound often used as a drug,Alkaloid,General,Collective nouns - A sleuth of what,Bears +Music,"Who Had A Multi Million Best Seller With ""How Much Is That Doggy In The Window""",Patti Page,General,The town of Banana in Queensland is named after what,A huge bullock, History & Holidays,What came to an end in the United States in December 1933? ,Prohibition  +Music,"What Connects The Average White Band, The Stranglers, Dionne Warwick",Walk On By,General,Chogori is better know by what boring name,K2,Art & Literature,Which Tennesee Williams play is about a Sicilian-American woman?,The Rose Tattoo +Science & Nature,"Technically, What Type Of Fruit Is A Pineapple? ",A Berry , History & Holidays,The Christmas carol 'Away In A Manger'' … what is a manger ,A Feeding Trough ,Geography,In which state is the Painted Desert,Arizona +General,Choo Kiko Wapi - what have I asked in Swahili,Where is the toilet,Technology & Video Games,What arcade game did Shigeru Miyamoto design Donkey Kong as a replacement for? ,Radarscope,General,Where are the headquarters for the c.i.a,"Langley, virginia" +General,What is a group of snipe,Wisp,Sports & Leisure,What Is The Name Of The Golf Stroke That Is 2 Under Parr For The Hole ,An Eagle ,General,Whose version of A View to a Kill reached 1 in USA 2 in UK,Duran Duran +General,In Lebanon it is legal to have sex with who / what,Female animals – males death,Music,"The Stones Took Their Name From A Blues Giant, Can You Name Him",Muddy Waters,General,The Epstein-Barr virus causes what illness,Glandular fever +General,What is another name for the egg plant,Aubergine,General,Who was Lady Emma Hamilton's famous lover,Lord nelson,General,"What's the name of the counting system in which four is written ""100""",Binary +Religion & Mythology,Who is the greek equivalent of the roman god Vulcan ?,Hephaistos,General,"Women burn fat more slowly than men, by a rate of about how many calories a day",50,People & Places,Who Committed Suicide In An Attempt To save Scott's Expedition ,Captain Oates  +Food & Drink,What does the phrase al dente mean? ,Pasta cooked firm ,People & Places,Which Former British Ambassador To The US Is Now BBC's Chief Financial Editor ,Peter Jay ,Geography,What is the capital of Libya,Tripoli +General,In 1917 Lucy Slowe was the first US what,Black tennis champion,General,"Who called overcrowded, poorly ventilated hospitals ""gateways to death""",Florence nightingale,General,What is the centre of an archery target called,Bullseye +General,"Of what did robert the bruce, king of scotland, die in 1329",Leprosy,General,The Gluckauf was the worlds first what,Oil Tanker,General,"Who said ""computers are useless they only give you answers""",Pablo Picasso +General,What does Mit Hefe on a German beer bottle mean,With Yeast,General,"Percent what was the final destination of the first u.s. paddle wheel steamboat, what departed from pittsburgh",New orleans, History & Holidays,"Which of the following is a 1983 New Order song; `Creepy', `Spooky' or `Scary' ",Spooky  +Music,"Who Achieved Chart Success With ""Everybodys Free To Feel Good"" In 1991",Rozalla,General,Iceland and Greenland are separated by which stretch of water,Denmark strait, Geography,Dakar is the capital of ______?,Senegal +General,In what movie did Whoopee Goldberg make her debut,The Colour Purple,General,Mary Robinson became president of which country in 1990,Ireland,General,What profession makes regular use of vibrators,Potters - remove air from clay +Geography,"The smallest island with country status is __________ in Polynesia, at just 1.75 square miles (4.53 sq km).",Pitcairn,General,Name the U.S.S.R. leader with a birthmark on his forehead?,Gorbachev,General,What Is The Capital City Of Nepal,Katmandoo +General,Which car manufacturer produces the models Sorento and Picanto?,Kia,Music,Who Sang A Song About Rasputin In 1978,Boney M,General,If a doctor gives you a clyster what have you just got,Injected into rectum by tube +General,"What was the unit of work in the c.g.s. system, which was replaced by the Joule in the S.I. system",The erg, History & Holidays,What was the real name of Eric Morecambes partner ,Ernest Wiseman ,Music,Which Famous Singer Got Their Name Because Of A Black & Yellow Jumper That They Often Wore,Sting +General,What year was the Live Aid concert,1985,Food & Drink,What is meant by deglazing? ,Adding alchohol or stock to pan juices ,General,In which sport is the Lugano trophy awarded,Road Walking +General,Another name for phencyclidine hydrochloride,Angel dust,General,"Name the minister who was hanged at Salem, Massachusetts for witchcraft",George burroughs,General,Small deer with white spotted reddish brown summer coat,Fallow +General,What is unusual about the 1965 horror film Incubus,Only one in Esperanto,General,What soprano simply titled her autobiography beverly,Beverly sills,General,According to the poem who dug the grave for cock robin,The Owl +Geography,Hell's Canyon on the Snake River is deeper than the _______________,Grand canyon,General,The kings of four different countries are said to be buried on which Scottish island?,Iona,Music,What group taped the first 3-D rock video in 1983?,Aerosmith +General,What is the name of Data's cat in Star Trek Next Generation,Spot,General,"Is someone suffered from halitosis, what would the problem be",Bad breath,General,The murder of Gonzago was performed in what Shakespeare play,Hamlet +General,Poon Lim holds the record of 133 days doing what,Surviving on a raft,General,What token was added to Monopoly in 1999,Sack of Money,Sports & Leisure,In Which Sport Would The Game Start With A Tip Off? ,Basketball  +General,What does a dipsomaniac crave,Alcohol,General,In 1963 what finally ended in Alaska,Mail by dog sled,General,In East Anglia England what put in house walls to ward off evil,Mummified Cats +General,Deacon is church official from Greek what's it literally mean,Servant,People & Places,Who Invented The Potato Crisp In 1853 ,George Crumb ,General,What is the name of the dark fine grained rock of which the Giant's Causeway is formed,Basalt +General,They are only found in Lake Nicaragua - what are,Fresh water sharks,General,Who or what are Taikonauts,Chinese astronauts, History & Holidays,Who Was the Queen Of The Iceni Tribe Of England Who Led An Uprising Against The Romans? ,Boudica  +General,What is a group of this animal called: Trout,Hover,Sports & Leisure,In 2006 At Which Club Did Roy Replace Niall? ( Roy Keane Replaced Niall Quinn) ,Sunderland ,Sports & Leisure,In Which City Not Country Were The 2000 Summer Olympics Held? ,Sydney  +Food & Drink,What Does Vodka Actually Mean ,Little Water ,General,Goats have rectangular ______,Pupils,General,What is the only bird with a fully formed penis,Swan +General,How Is Saint Sylvester's Day Better Known?,News Years Eve,Geography,In which city is the bridge of sighs ,Venice ,Technology & Video Games,What is Celine Jules's (Star Ocean: The Second Story) favorite food? ,Baby rabbit risotto +Sports & Leisure,WA is what position in Netball? ,Wing Attack ,Music,Mango Records Is A Division Of Which Famous Record Label,Island,General,Who did ron howard play in 'happy days',Richie +General,Bragi was the Norse God of what,Poetry,General,Regular oval shape,Ellipse,General,"Who according to Hilaire Belloc, had a chief defect of ""chewing little bits of string""",Henry king +General,Who is mickey mouse's girlfriend,Minnie mouse,General,The study of the manner in which organisms carry on their life processes is _________.,Physiology,General,"Which Football Club Are Known As ""The Bluebirds""",Cardiff City +General,Jim Rhodes is the alter ego of which Marvel comic book hero,Iron Man,General,In New Zealand what is a Punga,A Fern,Science & Nature,Where in the body is the tiniest human muscle?,Ear +General,"2 Gentleman Verona, 12th Night, Merchant Venice name links",Characters called Antonio,General,"This city on the Nile is where you will find the 'valley of the kings' & the temple of karnak, also the site of a tourist massacre in 1997","Luxor, egypt",Science & Nature,Which was the longest dinosaur ?,Diplodocus +General,Whose big break was playing Vinnie Barbarino on Welcome Back Kotter?,John Travolta,Science & Nature, Arabian horses have one less vertebra in their backbones than other __________,Horses,Sports & Leisure,In what sport would you find a 'Sukahara'?,Gymnastics +Science & Nature, Male __________ have antlers 7 feet across. The antlers often weigh 60 pounds.,Moose,General,In The Beverly Hillbillies what was the name of Ellie's Chimp,Cousin Bessie,General,"In Which Country Is The Ruvironza River, Considered To Be The Most Southerly Source Of The Nile",Burundi +General,What joins muscles to bones,Tendons,General,"Who commanded the ""Caine""",Captain queeg,General,A flocculent thing resembles what,Wool +General,"Small buns made from choux pastry, filled with cream and covered in chocolate are called what",Profiteroles,General,By how much do the world's termites outweigh human beings,Ten to one,General,Lack of vitamin d causes which disease,Rickets + History & Holidays,"If you licked Mr T's booty, it would taste like___ ",Sexual Chocolate ,General,Name the lion in the Lion the Witch and the Wardrobe,Aslan,General,Sean Connery has what real first name,Thomas +General,Who was born anne frances robbins,Nancy davis reagan,General,What type of creature is a Whirligig,Water beetle, Geography,What is the basic unit of currency for Ireland ?,Pound +General,This defense is also know as compulsion by threat.,Duress,Music,Give The Christian Names Of Abba's 2 Female Singers,Agnetha & Anni-Frid,General,"What it the ninth sign of the zodiac, symbolized by an archer",Sagittarius +Entertainment,"What was the name of Ashley Wilkes' plantation in ""Gone With the Wind""?",Twelve Oaks,Sports & Leisure,In 1995 Which Formula One Driver Signed For McLaren But Couldn't Fit In The Car? ,Nigel Mansell ,General,"Common liquor in Alien Urine Sample, Napoleon, Blue Hawaiian",Curacao +General,Who Had a father Called Jor El And a Mother Called Lara?,Superman,General,What impressionist painted different views Rouen cathedral,Claude Monet, History & Holidays,Which US President Was Forced To Resign In 1974 ,Richard Nixon  +General,"In which classical music suite, would one find ""The Market Place at Limoges"", and ""The Great Gate of Kiev""",Pictures at an exhibition,General,What is the Capital of: Greenland,Nuuk,Music,What Are The Everly Brothers Christian Names,Don & Phil + History & Holidays,During which conflict is The Caine Mutiny' set? ,World War 2 ,General,Susan Abigail Tomalin became famous as who,Susan Saradon,General,Walter Koenig played which part in the Star Trek series,Ensign Chekov +General,Who beat Sonny Liston to become world champion for the first time,Cassius clay,Science & Nature,Where Are A Grasshoppers Ears? ,On Its Legs ,General,Who is the main character in 'touched by an angel',Monica +Geography,Who Paid For Christopher Columbus 's First Voyage Across The Atlantic ,Ferdinand & Isabella Of Spain ,General,Thurl Ravenscroft is the voice of who,Tony the Tiger,General,What did The Musician Union ban on TV in 1966,Artists miming to records +General,Who wrote the 'The Dam Busters March' for the 1954 film of that name,Eric coates,General,Whose abduction of Helen of Troy brought about the Trojan War,Paris,Science & Nature,Which machine was invented by the German Wilhelm Rontgen in 1895? ,X-Rays  +General,There are 4.5 gallons of ale in what container,Pin,Art & Literature,Who Wrote (Far From The Madding Crowd) ,Thomas Hardy ,Technology & Video Games,"In Sonic the Hedgehog, the character Knuckles is what species? ",Echidna +General,What is the fear of wealth known as,Plutophobia,General,Barbie's nautical-sounding sister,Skipper,General,What declaration warned against interference in the America's,Monroe doctrine +General,Which Marilyn Monroe film was adapted for a stage play starring Daryl Hannah in London in 2000,Seven year itch,General,Treifa foods are forbidden to which religious group,Jews - opposite of Kosher,General,Generation X Toys: You coloured these and then watched them contract in the oven,Shrinky dinks +General,The song 'Raindrops keep falling on my head' was introduced in which film,Butch cassidy and the sundance kid,General,An 'ortanique' is a cross between which two fruits,Orange and tangerine,General,What event does Maundy Thursday commemorate? ,The Last Supper  +General,The childrens story 'The Rose and The Ring' was written by which 19th century novelist,William thackeray,General,Who did the painting on the cover of The Bands first album,Bob Dylan,General,Time ____ when you're having fun,Flies +General,What creature make the loudest noise - 188 decibels,Blue Whale,General,What is the Capital of: Falkland Islands (Islas Malvinas),Stanley,General,In Little Rock Arkansas men/women can get 30 days jail what,Public Flirting +General,What was pirate Captain Flint's ship called,Walrus,General,Which nursery rhyme was the first gramophone recording,Mary had a little,Science & Nature,Botany and Zoology combined make up the science of _______.,Biology +Entertainment,"Who says, ""Th_th_th_that's all folks!""",Porky pig,General,What system was introduced in 1967 to deal with the loud hiss on cassettes,Dolby,General,Ill Never Forget Whatshisname 1968 film first said fuck – who,Marianne Faithful +General,"In the Bible, who is the Book of Psalms attributed to",David,Food & Drink,What Grain Is Whiskey Made From ,Rye Or Barley ,General,What does Ally Sheedy say she likes to drink in the Breakfast Club?,Vodka +General,"Erotic solo dance of North Africa, the Middle East, and Turkish-influenced Balkan areas",Belly Dance,General,Over 28 million Americans are what,Hearing Impaired, Geography,What is the basic unit of currency for Portugal ?,Escudo +General,What is the capital of Nauru?,Yaren,General,The mandylion is another name for which contentious object,The Turin Shroud,General,Who sang 'we've only just begun',Carpenters +General,What is used to measure horizontal and vertical angles in surveying,A theodolite,General,Who married the poet Robert Browning in secret in 1846,Elizabeth barrett,General,What is the name of the stick used in hurling,Hurley + History & Holidays,Where did the Birkenhead sink?,Danger Point,General,"What Spanish surrealist modestly titled his 1965 book ""Diary of a Genius""",Salvador dali,General,What was the first soap to feature crib death in its story line,All my +General,A sliding step in which one foot 'chases' and displaces the other.,Chassé,General,Plant of which stalks are used as a vegetable,Celery,General,"In Magnum PI,Rick was not Rick's real name. What was his real name?",Orville Wright +Science & Nature,What is the heart rate of the blue whale? (in beats per minute),Nine,Art & Literature,By What Name Is The Great Italian Sculptor & Artist Buonarroti Better Known ,Michelangelo , History & Holidays,Who Wrote the Novel 'The Witching Hour' ,Anne Rice  +General,Baseball is not sport - state of mind - cant learn it - what author,John Steinbeck,General,What brand of footwear is endorsed by dr j,Converse,General,A Grabatologist collects ____,Ties +Music,"Before she joined Jefferson Airplane, for which group did Grace Slick sing?",The Great Society,General,Who wrote the song Johnny be Good,Chuck Berry,General,The first auto accident on record occurred in this city.,New york +Music,What was John Lennons father's name?,"Alfred ""Freddie"" Lennon",General,Which Brit set a record when becoming the first man to walk the entire length of the Amazon in 2008?,Ed Stafford,General,What's been the ruin of many a poor boy in New Orleans,House of the rising +General,Where was nixon's western white house,San clemente,General,What is a group of this animal called: Curlew,Herd,General,What links Paul and Ringo in the Beatles,Left Handed +General,Which was the first book to tell the story of the lioness elsa,Born free,General,"Plaza hotel who was the ""castaway cowboy""",James garner,General,What is the young of this animal called: Goose,Gosling +General,Who's directorial debut was with Reservoir Dogs,Quentin Tarantino, Geography,What is the capital of New York state?,Albany,General,Who wrote Wuthering Heights,Emily bronte +General,Whose first album was 'jagged little pill',Alanis morissette,General,Popular in North America what kind of sport is 'birling',Log rolling,General,"Who are santa's reindeer, in alphabetical order","Blitzen, comet, dancer," + History & Holidays,"What was the third country to get the ""bomb""",Britain,General,What North African country contains the largest area of the Sahara,Algeria,General,Ombrophobia is the fear of,Rain +General,To the Apache Indians what were God Dogs,Horses,General,What popular drink was introduced at the St Louis worlds fair in 1904,Ice tea,Science & Nature,What dog is named after a Mexican state?,Chihuahua +General,"Book or table containing a calendar, together with astronomical and navigational data and, often, religious holidays, historical notes, proverbs, and astrological and agricultural forecasts",Almanac,Music,"Which Female Vocalist Teamed Up With Larry Adler On The 1994 Single ""The Man I Love""",Kate Bush,General,US civil war what disease incorrectly treated by ink injections,Gonorrhoea +General,What country invented high octane gasoline in 1930,Russia,General,Klaus Voormann designed the cover for which Beatles album,Revolver,Music,"Who Had A Hit With The Song ""Like A Prayer""",Madonna +General,Sunglasses were invented in China to do what,Hide a Judges eyes,General,Where can you see Ada Byron,Woman on Microsoft watermark,General,What was the first ocean liner to have a swimming pool,Titanic +Geography,Which British Land Owner Owns The Largest Acerage ,The Forestry Commision ,General,What is the only capital letter in the Roman alphabet with exactly one endpoint,P,Music,How many holes does it take to fill the Albert Hall?,4000 +General,1878 Wanamaker's of Philadelphia first US store to install what,Electric Lights,General,What is ikebana,Flower arranging,General,Where is the setting for 'the bridge on the river kwai',Burma +General,Artificial barrier or obstacle on a motor racing course,Chicane, History & Holidays,Which Of Santas Reindeers Shares Its Name With A High Street Shop ,Comet ,General,"In National Lampoon's Vacation, what did the Griswalds call their ugly green station wagon?",The Family Truckster +General,Plant with edible fruits in red green and yellow,Capsicum,General,Biko was involved in what protest movement?,Apartheid,Geography,What is the capital of Pakistan,Islamabad +General,What was Mae West sent to the workhouse for in 1926,Writing starring in play called sex,General,Where in Europe is the Barbary ape found in the wild,Gibraltar,Food & Drink,Who had a big hit with `Sweet Like Chocolate' in 1999? ,Shanks & Bigfoot  +Music,Sloop John B & God Only Knows Appeared On Which Milestone Album,Pet Sounds, History & Holidays,What Were The Six Farm Hands Arrested For Forming A Trade Union Better Known As? ,The Tolpuddle Martyrs ,General,"Baby I see this world has made yoU.S.ad, some people can be bad ___"" What is the Dire Straits song title",Why Worry +Food & Drink,How Did Eggs Benedict Get It's Name ,As A Cure For A Certain Mr Benedict's Hangover ,General,What was the first Beatles song licensed for use in a Nike add,Revolution,Food & Drink,What are dried plums called ,Prunes  +General,Dorothy Cavis-Brown made news at Wimbledon - why,Lineswoman - slept in chair,General,What is a group of ducks,Brace,General,On what show did Dano get to book the bad guy?,Hawaii 5-0 + Geography,What is the capital of The Netherlands ?,Amsterdam,General,What happens every 30 seconds in America,Teenage pregnancy, Geography,In which country is Cusco?,Peru +General,The first Russian satellite was launched in which year,1957,General,"What is the UK's largest publicly funded building, constructed in the 1900's",The british library,Music,What is the world's best selling musical instrument?,The Harmonica +Music,Who Boogied On Down To The Funky Nassau,Beginning Of The End,Science & Nature,Which is the largest African bird of prey?,Lammergeyer,General,In Pennsylvania legally a man needs written permit from wife do what , Purchase Alcohol +Music,Which Robert Palmer Song Featured In The Movie Cocktail,Addicted To Love,General,What is one of the more expensive automobiles in the world,Mercedes,General,Who played the part of Malcolm X in the film of the same name in 1992,Denzelwashington +General,What two brothers were nominated for president by the Republicans in 1884,William and john sherman william and john sherman william john sherman,General,"Eight hundred dollars if locked in a completely sealed room, of what will you die before yoU.S.uffocate",Carbon monoxide poisoning,Toys & Games,This ancient Chinese game is played with 156 small rectangular tiles.,Mah jongg +General,Self-praise is no ___.?,Recommendation,General,"Which group sang the song ""Picture Of You""?",Boyzone,General,What dangles over the tongue from the palate,Uvula +General, Toxiphobia is a fear of _________.,Poison,General,"Largest island of the continental United States, southeastern New York?",Long island,General,What mates with a drake,A duck +General,Who was the composer of the classical piece known as 'Zadok The Priest'?,Handel,Art & Literature,Who wrote 'Psycho'?,Robert Bloch,General,Plant with aromatic seeds used in Indian food,Cumin +General,Charcarodon Carcharias is the Latin name for what creature,Great White Shark,General,What star was once a vacuum cleaner salesman,Rock Hudson,Music,"With whom was Janet Jackson performing a duet, when she 'popped out' at the 2004 Superbowl?",Justin Timberlake +General,What's the predominant church in greece,Greek orthodox church greek orthodox,General,Name the former topless model who inherited $300 million from her late husband J. Howard Marshall II,Anna nicole smith,Music,Which Solo Artist Released An Album Entitled “Music” In 2000,Madonna +General,What did George Washington soak his wooden teeth in for taste,Port,General,Edwin Drake sank the first of them in 1859 - what were they,Oil Wells,Entertainment,To play music smoothly?,Legato +General,Which famous ship and whiskeys name means short underskirt,Cutty Sark,General,"On ""It's Your Move,"" what is the name of the rock band that Mark and his friend Eliot pretended to be?",The Dregs Of Humanity,General,The violet belongs to which genus of flowers,Viola +General,What are camel haired brushes made of,Squirrels tails,General,What is another name for a spiny anteater,Echidna,Science & Nature,What Is Affected If A Patient Suffer From Gingivitis ,The Gums  +General,What is the traditional curse of Adam that affects most of us,Working for a living,General,What would you put on your escutcheon - if you had one,Coat of Arms,General,What does 'Alma Mater' mean,Bountiful mother +General,Although He Never Set Foot Here What Elvis Presley Movie Is Actually Set In England,Double Trouble,Art & Literature,Which American Artist Is Known For A Portarit Of His Mother ,James Whistler ,General,What war did Joan of Arc's inspirational leadership help end,The hundred years war + History & Holidays,Which Of Hitler's Deputies Flew To Scotland In 1941? ,Rudolf Hess ,General,Schwarzkopf on what game in 1994 did norman schwarzkopf post the highest score,Celebrity,Science & Nature,What Is A Blockage Or Clot In A Blood Vessel Known As ,An Embolism  + Geography,What seaport's name is spanish for 'white house'?,Casablanca,General,Where would you find your lunula,Fingernail - white part,General,A wind with a speed of 74 miles or more is designated as a what,Hurricane +General,In The World Of Music How Is 'Harry Lillis' More Commonly Known,Bing Crosby,General,What was the name of Ali Babas female slave,Morgiana,General,A melcryptovestimentaphiliac compulsively steals what,Knickers + History & Holidays,What two mountain ranges did hannibal and his elephants march through in 218 b.c ,The pyrenees and alps ,General,Victoria is the capital of the ______,Seychelles, Geography,What sea is between Italy and Yugoslavia?,Adriatic +Science & Nature," If they are well treated, camels in captivity can live to the age of __________",50,Music,Was Rio Duran Durans First Or Second Album,Second,General,Khons was the Egyptian god of what,Moon + History & Holidays,"Who asked, Ever Fallen In Love? ",The Buzzcocks ,General,What is french national anthem,Marseillaise,Entertainment,How many storm troopers were seen in Star Trek?,None (It Was Star Wars) +General,What was the name of the union jack that was used to capture blackbeard off ocracoke island in 1718,Ranger,General,What is the Capital of: Mozambique,Maputo,General,Which Human Body Part Contains A Quarter Of Your Bones,Ear +General,"Which Film Opens With The Line ""No Man's Life Can Be Encompassed In One Telling""",Ghandi,General,The Roman province Maxima Ceasariensis was in what country,England,Entertainment,Where are Rocket J. Squirel and Bullwinkle Moose from?,Frostbite Falls +General,How long was jonah in the whale's stomach,3 days,General,Nathan Burnbaum became famous under what name,George Burns,General,Which Scottish engineer gave his name to the S.I. unit of power,Watt +General,"What is the nickname for Rome, Italy",Eternal city,General,In 1951 Kiki Haakonson Became The First Ever Miss World Winner But What Country Did She Come From,Sweden,General,With what is the organisation UKAEA concerned,Atomic energy +General,Who wrote the semi-autobiographical novel 'Of Human Bondage,Somerset maughan,General,What is the term used by the military when they lose a nuclear weapon,Broken arrow,Science & Nature," The first medical use of leeches dates back to approximately 2,500 years ago. The leech's saliva contains a property that acts as an anticoagulant for __________",Human blood +General,Name the condition of the mind that leads to a need to steal things,Kleptomania,Science & Nature,What Metal Is Liquid At Room Temperature ,Mercury ,General,"The human body contains enough of what to make 2,200 match heads",Phosphorous +General,What is non-rhyming poetry called?,Blank verse,General,"Which island was defended by Faith, Hope, and Charity during WW2",Malta,General,Holden Caulfield - Catcher in the Rye - where JD Sal get name,Movie marquee W Holden J Caulfield +General,What is the flower that stands for: compassion,Allspice,General,"Who did Christina Onassis grudgingly give $25 million to, in 1977",Jacqueline onassis jackie onassis,Geography,Before The Euro What Was The Currency Of Sweden ,Krona  +People & Places,Who Is Stevland Morris Hardaway Better Known As ,Stevie Wonder ,General,What is a group of this animal called: Locust,Plague,Music,Which Instrument Did Mark King Of Level 42 Play,Bass +General,What is a group of flies,Business,General,Which Car Manufacturer Was The First To Mass Produce Cars With Power Steering,Chrysler,General,"Habib Ibn Ali Bourguiba became president of which country in 1957, was re-elected three times before being made Life President in 1975",Tunisia +General,What is the flower that stands for: slander,Stinging nettle,General,Menkes Kinky Hair Syndrome caused by deficiency of what,Copper,People & Places,Why Did Michael Fagan Hit The Headlines In 1982 ,Broke Into Queens Bedroom  +General,What wood is plywood mostly made from,Birch,General,"Which city hosted the world fair under the theme ""man and his world""",Montreal,General,What colour thread is used for filigree,Silver or gold +General,What was the first country to leave the United Nations,Indonesia,General,Which City Claims Table Mountain As It's Backdrop,Capetown (South Africa), History & Holidays,Which Political Party Rose To Power In China In 1949 ,The Communists  +General,The filbert is an alternative name for which nut,Hazelnut,Food & Drink," Often eaten for breakfast, the egg comes from what barnyard animal",Chicken,General,Ranch what famous building did sir john vanbrugh design,Blenheim palace +General,In what Australian state would you find Armidale,New south wales nsw,Entertainment,What is Hawkeye's full name in M.A.S.H.?,Benjamin Franklin Pierce,General,What finger is the most sensitive,Forefinger +General,What is the official drink of the state of Ohio,Tomato Juice,General,What form of horse racing boasts the Little Brown Jug,Harness racing,General,Patsy Mclenny became famous as who,Morgan Fairchild +General,Which actress and singer played 'Breathless Mahoney' in the film Dick Tracy,Madonna,Sports & Leisure,In which sport is the Davis Cup awarded?,Tennis,General,What is the general term for all chemical substances produced by the endocrine glands,Hormones +General,The hundred year war actually lasted how many years,116 years,General,What is the technical term for long-sightedness,Hypermetropia,General,Which French philosopher created analytical geometry,Rene Decartes +General,"Who Was The Original Choice To Play The Role Of ""The Terminator"" Before Arnold Schwarzenegger Got The Part",O.J Simpson,General,In 1942 Japanese occupied,Manila,General,Astronomer Josephe-Jerome de Lalande eat what on bread butter,Spiders +General,Name the dance that means new voice in Portuguese,Bossa Nova,Music,"Who Were Mark Farner, Mel Schacher & Don Brewer",Grand Funk Railroad,General,60% of English women cannot orgasm without what,Vibrator +Sports & Leisure,At The Start Of The 2002/03 Season Who was the Premierships Highest Goal Scorer ,Alan Shearer ,General,"Who recorded the album ""Ssssh"" in 1969",Ten years after,General,"Which Countrys International Registration Mark Bears The Letters ""DZ""",Algeria +General,By which name is Mendelssohn's Third Symphony known,Scottish,Sports & Leisure,W A is what position in Netball? ,Wing Attack ,General,Who composed the overture Hebrides (Fingals cave) two names,Felix Mendelssohn +General,The opera Aida was commissioned in 1869 to mark what event,Opening Suez canal,General,Who is the helmsman of the boat when you are sailing into Hades,Charon,General,"Who was the nba's most valuable player in 1976, 1977 and 1980",Kareem +Food & Drink,In which countries would you find the following city 'Whiskeytown'? ,USA ,General,This cereal's mascot is Tony the Tiger,Frosted flakes,Music,"Who Had A Hit With The Song ""Do That to Me One More Time""",The Captain & Tenille +Art & Literature,Under What Pen Name Did Hector Hugh Munro Write ,Saki ,General,Who's advertisement ends 'The ultimate active sports vehicle, bmw,General,What is added to the ice & water mixture in domestic ice cream makers in order to lower the temperature,Salt +General,Why was the dinosaur Triceratops so called,Three horns,General,What is the fear of music known as,Melophobia,Geography,In what country is the lowest point in South America,Argentina +Music,From What Country Do The Dance Band Cascada Come From,Germany,Religion & Mythology,In what animal form did Zeus seduce Europa?,Bull,General,Name the Greek equivalent of the Roman god Juno,Hera +General,What do you add to vegetables to make the dish salmagundi,Duck or Chicken,General,Which sauce has the same name as a state in Mexico,Tabasco,General,Which Greek mountain is consecrated to the muses,Helicon +General,"French philosopher, scientist, and mathematician, sometimes called the father of modern philosophy",Descartes,Entertainment,How many symphonies did Beethoven complete?,Eight,General,Doraphobia is the fear of _________.,Fur +General,"In 'la traviata', what does violetta sing",Sempre libera, History & Holidays,"Alphabetically, Which Is The Last Of Santa's Reindeers? ",Vixen ,General,What is the only creature born with horns,Giraffe +General,Who's voice was rex the robot for disneyland's star tours,Pee wee herman,General,"What tv dog had successive masters named jeff, timmy and corey",Lassie,Entertainment,Secret Identities: Clark Kent,Superman +General,More than 14 million are sold daily in 150 countries - what,Bic pens,General,A white mtallic element,Barium,Geography,The Thatcher Ferry Bridge crosses what canal,The panama canal +General,Alex and his 'droogs' are thugs in what Anthony Burgess novel,A Clockwork Orange,General,What is the capital of brazil,Brazilia,General,At which racecourse would you find a jump called Valentine's Brook ,Aintree  +General,What is the American equivalent of the Irish Poteen,Moonshine,General,Iron block on which metals are worked,Anvil,General,"What's the international radio code word for the letter ""I""",India +Science & Nature,What Is The UK's Most Poisonous Plant? ,Deadly Nightshade ,General,A planes black box is usually named after what King,Midas,General,Who wrote Vanity Fair,William thackeray +Science & Nature,What Type Of Animal Is A Borzoi? ,A Dog The Russian Wolfhound ,Entertainment,What is Peter Parker's secret identity?,Spiderman,General,What is a group of porcupines,Prickle +General,What links Scorpion Seawolf and Thresher,Lost US nuclear reactors at sea,Music,What Did Brenda Lee Want To Jump Over In 1961,The Broomstick,General,Who is known as a collector of trivia,Spermologer +General,When did Alaska become the 49th U.S. state,1959,Science & Nature,What is a male swine called?,Boar,Science & Nature, The giant African __________ grows to a foot long and reaches weights greater than a pound.,Snail +Geography,What is the capital of Gabon,Libreville, History & Holidays,"Which type of British fighter plane shot down 1,294 enemy aircraft in World War One? ",The Sopwith Camel ,General,Who ruled the seas in Greek mythology,Poseidon +General,"Who released ""carrie anne"" in 1967",Hollies,General,In 1643 Evangalisa Torichelli invented the first what,Barometer, History & Holidays,Who Released The 70's Album Entitled The Stranger ,Billy Joel  +Music,Name The Arranger / Orchestra Leader On Frank Sinatra's Songs For Swinging Lovers Album,Nelson Riddle,General,In St Louis Missouri its illegal for a fireman to rescue who,Undressed women full dress only,General,Effect that occurs when two or more waves overlap or intersect,Interference +General,What animals eye is bigger than its brain,Ostrich,Music,"What Clothing Are The 2 Figures Wearing On The 1978 Black Sabbath Album Cover ""Never Say Die""",Some Type Of Flight Suits,General,Who won the Nobel Peace Prize in 1972,Nobody +General,"Which 1996 Disney Movie Featured The ""Eternal "" Hit ""Someday""",The Hunchback Of Notre Damme,General,The animal responsible for the most human deaths world wide is the ________,Mosquito,General,What group did tommy james leave in 1970,Shondelles +Science & Nature," The star_nosed mole, with 22 pink __________ on its snout, is said to have the most delicate sense of touch in the animal world.",Tentacles,General,What is alice cooper's real name,Vincent furnier,General,Which chart song was based upon psalm 137,Rivers of babylon +General,As Of 2008 Only One President So Far Was Born On The 4th Of July Can You Named Them,Calvin Coolidge,General,What is liquid clay used in pottery called?,Slip,Food & Drink,Eggwash is generally used for what purpose? ,Glazing breads and pastries  +General,A Saudi Arabian woman can get a divorce if her husband doesnt give her what,Coffee,General,What Did Businessman Peter De Savray Buy For 6.7 Million Pounds In 1987,Lands End,General,What creature is the symbol of Bacardi,Bat +General,What links Bill Clinton Fidel Castro Alb Einstein Jimmy Hendrix,Left Handed,General,Marnie Nixon what Deborah Kerr Natilie Wood Audrey Hepburn,Dubbed in their singing voices,General,Name one of the three men who sat in the tub,Candlestick maker butcher baker +General,Almonds are members of what family,Peach,General,When are finger prints formed,3rd week in the womb,General,"In 1982, who was accused of killing both her parents with an axe",Lizzie borden + Geography,In which continent would you find the Lena river ?,Asia,General, The study of plants is _________.,Botany,General,What does a hypodermic literally mean,Under skin + History & Holidays,In Which Year Did A Soviet Cosmonaut Make The First Spacewalk ,1965 ,Geography,LAR is the international vehicle registration of which country? ,Libya (Libyan Arab Republic) ,Science & Nature,Which Wood Are Divining Rods Usually Made From? ,Hazel  +General,10% of men claim to do this regularly – what,Shave their pubic hair,General,"In 1997, which sportsman described his ethnic background as Cablinasian",Tiger woods,General,Who played laura petrie in 'the dick van dyke show',Mary tyler-moore +Music,"What's The Connection Between Nenah Cherry, Malcolm Mclaren & Bob Marley",Buffalo's,Music,"Tracy Chapman Or Tanita Tikaram Had A Hit With ""Twist In My Sobriety""",Tanita Tikaram,General,In Lebanon Virginia its illegal to do what to your wife,Kick her out of bed +Music,By What Name Is George O'Dowd Better Known,Boy George,General,What did pocahontas wear while entertaining the colonists,Nothing,General,What is mickey mouse's dog's name,Pluto +Music,"What's The Connection Between Len Barry, Paul Hardcastle & Manfred Mann","(Numbers) 123, 19, 54321",Food & Drink,"Which dish contains offal and oatmeal, and is traditionally boiled in a bag made from the stomach of an animal? ",Haggis ,General,Carmenta is the roman goddess of ______,Childbirth +General,It is illegal to use what to plough cotton fields in North Carolina,Elephants,General,If you planted a bandarilla what are you doing,Bullfighting,General,What french phrase means 'well informed',Au courant + History & Holidays,According To The Song How Many Drummers Drumming Were There ,12 ,General,What is the fear of spirits known as,Pneumatiphobia,General,What South American country has both a Pacific & Atlantic coastline,Colombia +General,The 1st buffalo ever born in captivity was born at Chicago's Lincoln Park Zoo in what year,1884,General,"Eric Clapton shared a woman with this superb guitarist and songwriter. Later, it inspired his to write Layla.",George Harrison,General,What Shakespeare character ends saying The rest is Silence,Hamlet + History & Holidays,George Washington Carver advocated planting peanuts and sweet potatoes to replace what?,Cotton and tobacco,Music,What Was Otis Redding Sitting On In 1967,Dock Of A Bay,General,Which country has won the most tug of war world championships,England + History & Holidays,Who shot President James Garfield?,Charles Guiteau,General,What was invented 1963 150 billion made since,Ring Pulls on cans not cans,General,Where is saint paul's cathedral,London +General,In Australian slang what is a ten ounce sandwich,Liquid Lunch - Can of Beer,Science & Nature,Which Dam Harnesses The Colorado River ,The Hoover Dam ,General,What is the study of tissues,Histology +General,"What is the correct chemical name for iron pyrites, sometimes known as 'Fool's gold'",Iron sulphide,Music,Who Was Front Man For Leon Young String Chorlae And The Paramount Jazz Band,Mr Acker Bilk,General,What did 'd.m.z' stand for in the vietnam war,Demilitarized zone +Science & Nature,What Communication Aid Was Invented In 1905 By American Undertaker Almon Strowger ,The Dial Telephone , Geography,What river is the Temple of Karnak near?,Nile,Science & Nature,Where do ants live?,Formicary +General,"Finely ground meal of grains of wheat, obtained by milling",Flour, Geography,Where is Dogger Bank?,The North Sea,General,Tellus was the Greek god of what,Earth +Food & Drink,What Is The Main Ingredient In Advocaat ,Egg Yolk ,General,What is the Capital of: United Kingdom,London, Geography,In which state is the Natchez Trail?,Mississippi +Tech & Video Games,"Who is the boss of the SNES game, 'Ranma 1/2: SUPER Hard Battle'? ",Herb,General,What returned in 1985 that is pictured on the Bayeux tapestry,Halley's comet,General,Of which British politician was it said 'He delivers all his statements as though auditioning for the speaking clock',John major +General,What poem is recited as the finale to Disney's christmas festivities,A visit from st nicholas,Geography,"The largest lake in Australia is ________, measuring 3,420 square miles (8,885 sq. km).",Eyre,General,Who fixed the date of the christian festival 'easter',Council of nicaea +Science & Nature, Birds do not sing because they are happy. It is a __________,Territorial behavior,Science & Nature,What Constant Related The Diameter Of A Circle To It's Circumference ,Pi ,General,"What are popcorn, moss and seed",Knitting Stitches +Geography,Which Country Left The Commonwealth In 1972 ,Pakistan ,General,What African country and its currency have the same name,Zaire,General,Which country imports the most champagne,Great Britain +General,In the Jewish religion where are shellfish and pork kosher,Chinese Restaurants,General,As pretty as a ______,Picture,Geography,What Does Dc Stand For In Whashington DC ,District Of Columbia  +General,"Cattle are bovine, sheep are ______",Ovine,General,"""But touch my tears with your lips, touch my world with your fingertips"" what is the queen song title",Who Wants to Live Forever,General,What is the sum of 2y + 32y + 56y,90y +General,"Who earned the nickname ""top gun"" in the Persian Gulf War",Colin Powell,General,By what other name is the double album The Beatles known,The White album,Sports & Leisure,From which club did Manchester United sign Roy Keane? ,Nottingham Forest  +General,Which heroine comes from Amphipolis,Xena warrior princess,General,What is the capital of Illinois,Springfield,Music,"What was the working title of ""With A Little Help From My Friends""?",Bad Finger Boogie +General,Which musical term denotes that a piece is to be played 'sweetly',Dolce, History & Holidays,What was the predecessor of the United Nations ?,League of Nations,Geography,What river is the Temple of Karnak near,The Nile +General,"Before wwii, the new york phone book had 22 listings for what surname, and none after wwii",Hitler,Geography,In which state is the mayo clinic ,Minnesota ,General,In which musical work is there a jester called Jack Point,Yeomen of the guard +General,"Which film ends with the line, 'What we have here is a failure to communicate'",Cool hand luke,General,In US on what Day are most collect calls reverse charges made,Fathers Day,Sports & Leisure,In Squash Each Game Is Played Until A Player Scores How Many Points ,15  +General,"Whose first major album was ""ziggy stardust and the spiders from mars""",David,General,Which course will stage the 2014 Ryder Cup torunament?,"Centenary Course, Gleneagles",General,Who wrote Oedipus Rex,Sophocles +General,What did Brahms compose for the University of Breslau after they gave him an honorary PhD,Academic festival overture,General,What is a young fish called,Fry,General,What machine is used to record the electrical activity in the brain,Electroencephalogram +General,Hundred when does the uterus expand 500 times its normal size,During pregnancy,General,Who were the first pop stars to appear in Madam Tussaud's,The Beatles,Sports & Leisure,Who Became World Heavyweight Boxing Champion By Beating Ken Norton After Muhammad Ali Retired In 1979 ,Larry Holmes  +Toys & Games,If you combined k & p to make cables what would your hobby be,Knitting,General,Crack gets it name because it ________ when yoU.S.moke it,Crackles,General,How long was nelson mandela in prison,27 years + History & Holidays,Who said 'public service is my motto'?,Al Capone,General,What is the greek word for the biblical book of revelation,Apocalypse,General,What is a violincello,Cello +General,In the US its 911 in the UK 999 what in Australia,0,General,"In ancient greece, what was a myriad","Groups of numbers over 10,000",People & Places,How Did James Dean Die ,In A Car Crash  +General,Who played the male starring role in 'The Graduate',Dustin hoffman,General,In 1955 Israel acquires 4 of the 7 dead sea,Scrolls,General,What pets did lyndon johnson have,Beagles +General,Super glue is used to lift fingerprints from what surfaces,Difficult,General,Collective nouns - A Float of what,Crocodiles,General,"Yoi, Yame, Seremade and Hantai terms in what sport",Karate +General,"Who wrote the thriller ""The Bourne Identity""",Robert ludlum,Music,What Is The Most Popular Karaoke Tune Worldwide,"""You've Lost That Lovin Feeling""",General,What is the saltiest sea in the world,Dead sea +General,Hemmingway said there's only 3 sports Bullfighting Car Racing ?,Mountaineering,Sports & Leisure,Football: The Chicago ______?,Bears,General,What happened July 15 1815 on HMS Bellerophon,Napoleon surrendered + History & Holidays,Which poisonous concoction was Socrates given to drink to carry out his death sentence?,Hemlock, History & Holidays,How did the tradition of serving turkey at Christmas originate? ,Medievil Europe serving Large Fouls Was A sign Of Wealth ,Science & Nature,How many hours does an antelope sleep at night?,One +General,"Spanish: how do yoU.S.ay ""four""",Cuatro,Food & Drink,"If you saw broiled food on an American menu, how would it be cooked? ",Grilled ,General,In which country would you drink a local beer called Keo?,Cyprus +Food & Drink,What is the most widely used seasoning ,Salt ,General,What is the name of the capital of Saskatchewan (canada),Regina,Music,"Who Had 2 Number One LP's Called ""Rollin"" & ""Once Upon A Star""",The Bay City Rollers +General,Who produced and directed the Death Wish series of films,Michael Winner,General,Which famous million dollar building cost more than a million dollars?,Sydney Opera House,General,"Which artist painted ""The Potato Eaters""",Vincent van gogh +General,US 1900 census people with 2 or less what were lower mid class,Servants,General,Who Holds The The Record For The Highest Number Of Olympic Gold Medals In A Single Summer Olympics,Mark Spitz,Food & Drink,What are the ingredients of the cocktail Cuba Libre? ,Rum And Coke  + History & Holidays,What was the first transatlantic radio message sent ?,S,Religion & Mythology,What two biblical cities did God destroy with fire and brimstone?,Sodom and Gomorrah,General,Which chemical element is named after the Greek word for 'green',Chlorine +General,What spy novelist was Moscow correspondent for Reuters & The Times of London,Ian fleming,General,"He was elected President of France, in 1981.",Francios Mitterrand,General,"The word 'whisky' comes from Gaelic, what does it mean",Water of life +General,John larroquette was the narrator of which gruesome film,Texas chainsaw,General,What disease is spread in minute water droplets,Legionnaires Disease,Sports & Leisure,What trophy is awarded to the winner of the NHL playoffs?,Stanley Cup +General,"The French call it ""La Train Sifflera Trois Fois"" what film is it",High Noon,General,Which New Zealand novelist won the Booker Prize with her novel The Bone People,Keri hulme,General,In computing what is the smallest movement of a mouse called,Mickey +Science & Nature,Who Invented the worlds first electronic pocket calculator in 1972 ,Sir Clive Sinclair ,General,Ignoring obvious what links Jupiter Neptune Uranus and Saturn,All have Rings,General,"On Little House on the Prarie,what was the original name for the school teacher?",Miss Beatle +General,In 1500 BC Egyptian women had to be what to be beautiful,Bald,General,On the Beaufort scale 8 represents what,A Gale,General,Where are birds kept in captivity,Aviary +General,Marie Osmond has only had one UK hit single as a solo artist name it,Paper roses,General,Meningitophobia is the fear of,Brain disease,General,What grows faster in the morning than at any other time of the day,Your hair +Sports & Leisure,In 2006 Who Was Voted Overseas Sports Personality Of The Year? ,Roger Federer ,Geography,In what country is taipei ,Taiwan ,General,Where is the island of korcula,Croatia +General,Who said 'ronald reagan doesn't dye his hair; he bleaches his face',Johnny,Music,Where is Abbey Road?,London,Music,Who Picked Up An academy Award In 1971 For Best Film Song,Isaac Hayes (Shaft Theme ) +General,Which river is dammed by the hoover dam,Colorado river,General,What is uruguay's chief port,Montevideo,People & Places,What Is MP Dennis Skinners Nickname ? ,The Beast Of Bolsover  +General,What does YoYo mean in English,Come-Come,Food & Drink,German dish of chopped cabbage? ,Sauerkraut ,Music,"Tiny In Stature But Huge In Both Ego And Talent Who Had The No.1 Album ""LoveSexy""",Prince As He Was Then Known +General,What was a Nuremberg egg,Pocket watch / clock,General,What is the second letter in the Greek alphabet,Beta,General,Where is the Greyhound Racing Hall of Fame located,Abilene Kansas + History & Holidays,Grease The Musical Opened On Broadway In 1973 How Many Performances Were There ,"3,388 ",General,Which country did France beat to take the 1998 world cup?,Brazil,General,Picturesque cave,Grotto +General,Anthony McMillan became famous as who,Robbie Coltrane,General,What has the chemical formula H2O2,Hydrogen Peroxide,General,What is a group of this animal called: Quail,Covey bevy +General,In 1964 who was the first non royal to appear on a UK stamp,William Shakespeare,General,"In bowling, what is it if you knock down all the pins with one ball",Strike,General,Device for driving air into fire,Bellows +General,"Which Movie Star Published An Autobiography In 2002 Entitled ""Lucky Man""",Michael J Fox,General,Talc is a hydrated silicate of which metal,Magnesium, History & Holidays,"What Apollo 13 astronaut contacted Mission Control with the words, (Houston, we've had a problem)? ",Jack Swigert  +General,Who is the Patron Saint of housewives,Martha,General,What alternative name is given to the Barn Owl because of its harsh cry,Screech owl,General,Thanatology is the study of what,Death +Science & Nature, The whistling swan has more than __________,"Feathers on its body. 25,000",General,Silent night the Christmas carol was first played on what,Guitar,General,"Which period was first, jurassic or carboniferous",Carboniferous +Science & Nature,Commonly Regarded As A Bit Of A Nuisance Amongst Computer Users ' It Writes Once But Reads Many Times '' What Is It? ,A Worm (Mass Mailing) ,People & Places,Washington is part of which English city? ,Sunderland ,General,In Texas by law criminals must give their victims what,24 hours advance written notice +General,What does a la carte mean in a restaurant,According to the menu,General,A hoop worn under skirts is called a what,Farthingale,General,"Philosophy: Epicurus, who believed that pleasure is the highest good, gave us which term synonymous with hedonistic?",Epicurean + History & Holidays,Which president was responsible for the Louisiana Purchase,Jefferson,General,What is the capital of missouri,Jefferson city,General,What are the 'irons' in horse racing,Stirrups +General,Out of what is paper money made,Linen,Geography,The geographic center of __________ is located in the city of Thunder Bay.,Canada,General,Alfred Gerald Caplin is better known as this (first+last name),Al capp +General,Who was made the first Holy Roman Emperor,Charlemagne,General,Who directed the films 'Carrie' and 'The Untouchables',Brian de palma,General,What is the capital of Chechnya,Grozny +Entertainment,In what did someone squish her hands to make the sound of e.t walking?,Jelly,Music,"Who Released An LP Entitled ""George Best""",The Wedding Present, Food & Drink,What is the main ingredient in the soup Borscht?,Beetroot +Art & Literature,Water-soluble paint made from pigments and a plastic binder…? ,Acrylic,General,Who sang the title song in the film Grease,Frankie Valli,General,This superb composer has composed scores to over 400 films?,Ennio Morricone +General,Which nation invented sauerkraut,Chinese,General,"In baseball, who won their first world series in 1969",New york mets,General,"Countries of the world: western coast of South American, major cities include Arequipa & Trujillo",Peru +General,What is the chalice used by Jesus Christ at the Last Supper called?,Holy Grail,General,What was the secret identity of don diego de la vega,Zorro,Music,What Is The Central Area Of A Hurricane Called,The Eye +General,A nilometer measures the rise and fall of what,Rivers (originally Nile),Sports & Leisure,How many hurdles in a 400m hurdle race?,10,General,Which species of bird gave Darwin his theory of evolution,Finch +General,What is the worlds oldest monotheistic religion,Judaism,General,France named it Chapeau Melon et Bottes de Cuir what UK,The Avengers,General,What colour is diamond dust,Black +General,Who was captain of the 1995 south african rugby world cup team,Francois,General,Name the theory which proposed that a person's mental development could be measured by a skull examination,Phrenology,Music,Which Record Label Were The Mindbenders Signed To,Fontana +General,In England what ocean current allows gardeners on the west coast to grow exotic plants,Gulf stream,General,What links Ygrana - Valentino and Cerrutti,Paris fashion houses,General, Doraphobia is the fear of _________.,Fur +General,What is the capital of Alberta,Edmonton,General,Town Australia is named after the wife Sir C H Todd postmaster,Alice Springs,General,Before 1687 clocks never had what,Minute hands +Music,"Who Wrote A Book Entitled ""The Adventures Of Lord Iffy Boatrace""",Bruce Dickinson, History & Holidays,Which movie prompted the style of wearing cutoff sweatshirts over the shoulder? ,Flashdance ,Sports & Leisure,What's the stick in golf? ,The Flag  +Geography,Which City Is The Capital Of Iceland ,Reykjavik ,General,Where is mount mckinley,Alaska,General,What was the final episode of MASH called,Goodbye Farewell and Amen +Entertainment,Who played Steve Erkel in 'Family Matters'?,Jaleel White,General,"After Greenland, which is the second largest island",New guinea,Geography,Which City Is The Capital Of Zimbabwe ,Harare  +General,Name Indian chief who rode in Roosevelt's inaugural procession,Geronimo,Entertainment,Before being married to Pamela Anderson what other famous actress was Tommy Lee married to?,Heather Locklear,General,What is the unit of currency in Jordan,Dinar +General,Which is the only book written by margaret mitchell,Gone with the wind, Geography,What is the basic unit of currency for Rwanda ?,Franc,General,What does a carpologist study,Fruit and seeds +Music,"What Connects ""Windmills Of Your Mind"", ""Evergreen"", ""Take My Breath Away""",Oscar Winning Songs,Sports & Leisure,How Wide Is The Beam In Womans Gymnastics ,4 Inches ,General,Britain Ireland and what country joined the EEC simultaneously,Denmark +General,Which U.S. writer wrote The Naked and the Dead,Norman mailer,General,What process allows plants receiving sunlight to make their own food,Photosynthesis,General,How did the Emperor Claudius die,Choked on a Feather + History & Holidays,"Humphrey Bogart, who was born on Christmas Day 1899, said the line 'Here's looking at you kid.'' In which film? ",Casablanca ,Science & Nature, A shrimp has more than a hundred pairs of chromosomes in each cell nucleus. Man has only __________,23,General,What central american country extends furthest north,Belize +Science & Nature,What name is given to the excrement produced by bacteria that feed on yeast cells and then defecate ,Alcohol ,General,R D Blackmore wrote which classic novel,Lorna Doone,General,In Greek mythology who was the mother of Uranus,Gaia +General,Dame where is the notre dame cathedral,Paris,General,"Religious phenomenon in which a message is sent by God (or by a god) to human beings through an intermediary, or prophet",Prophecy,General,What is the only creature that can turn its stomach inside out,Starfish +General,"Schwarzenegger what us state was named after french words for ""green"" and ""mountain""",Vermont,Music,Boleskin House On The Shores Of Loch Ness Was Bought By Jimmy Page In The 1970's Who Was Its Most Famous Former Occupant,Aleister Crowley, History & Holidays,"""Which diarist noted on 25th December 1662, """"(Christmas Day). Had a pleasant walk to White Hall, where I intended to have received the communion with the family, but I have come too late___"""""" ",Samuel Pepys (pronounced 'peeps')  +Science & Nature,What comprises than 54% of humpback whale's milk?,Fat,General,"What Shakespeare play is ""The Green Eyed Monster"" mentioned",Othello,General,What is the collective name for a group of foxes,Skulk +General,In Norse mythology what was Audulma - wet nurse of giants,Cow,Technology & Video Games,"In the original All-C Saga game, who kills Mandy and Matthew? ",Simetra,Music,"Which Future Member Of 10cc Appeared As A Pupil In The Film ""To Sir With Love""",Eric Stewart +General,Who's aliases John Willard Eric Gault George Ramom Sneyd,James Earl Ray – killed M L King,Music,For Which Film Was Director Bob Fosse Awarded An Oscar In 1972,Cabaret,General,What were the myrmidons who were created by zeus,Ant people +General,After WW I what was Hitler promoted to in rank,Corporal,General,Only humans and what have hymens,Horses,General,Of which country does the kalahari desert cover 84%,Botswana +General,"What is a 'yesterday, today & tomorrow'",Shrub,Entertainment,Who is the autor of the song 'Blue Suede Shoes'?,Carl Perkins,General,Orbis non Sufficit - World is not enough - whose family motto,James Bond +General,"What 1902 children's book continues to sell over 50,000 copies per year",The tale of peter rabbit,Geography,What country is directly north of Israel,Lebanon,General,Where is the kennedy space centre,"Cape canaveral, florida" +General,Which funnyman appears in a series of TV adverts for the insurance company Aviva?,Paul Whitehouse,General,Who sang the Song 'Electric Youth'?,Debbie Gibson,Art & Literature,"Artwork, usually paintings, characterized by a simplified style, nonscientific perspective, and bold colors. The artists are generally not professionally trained. ",Naive art +General,Who was the last to stab Caesar in Shakespeare's Julius Caesar,Brutus,General,Where is eurodisney,"Paris, france",General,Prowse What foreign country's phone book is alphabetized by first name,Iceland +General,"What god were the Thugees worshipping in ""Indiana Jones and the Temple of Doom""?",Kali, History & Holidays,Which Shakespeare Play Begins With 3 Witches Upon The Heath ,Macbeth ,General,What is missing from the 'venus de milo',Arms +Music,Who Wrote The Musical The Rocky Horror Picture Show?,Richard O'Brian,General,In the Terminator film who was the boy who would be the leader,John Conner – mother Sarah,Music,Stupid Cupid Reached No.1 For Which Female Vocalist,Connie Francis + History & Holidays,Who built Camelot?,King Arthur,General,"What is the meaning of the name Irene, which comes from the Greek words eirenikos or eirene",Aiming at peace,Food & Drink,"Which drink is named after the Duke of Beaufort's seat and is a long, refreshing drink of claret with soda water and sugar? ",Badminton Cup  +General,What ship meaning new land carried Scott to the Antarctic 1910,Terra Nova,Music,Which Album Was The Follow Up To Thriller For Michael Jackson?,Bad,General,"Who had her first entry into the British charts in December 1985 with ""Saving All my Love for You""",Whitney houston + Geography,In which continent would you find the Amazon river ?,South America,General,Framboise' is a liqueur flavoured with what,Raspberries,General,Nostradamus was famous for making what,Predictions +Geography,What is the capitla of Croatia? ,Zagreb ,General,Thaslophobia is the fear of what,Sitting Idle – doing nothing, History & Holidays,In which city was President Kennedy killed?,Dallas +General,In which country was Robert Maxwell born,Czechoslovakia,General,What is the fear of dirt known as,Rhypophobia,General,Kriss Kristofferson and Barbra Streisand starred in the re-make of which film,A star is born +General,In which Welsh county is Beddgelert,Gwynedd,Art & Literature,"In painting, a thin layer of translucent color.",Wash,General,Who was the norse goddess of love and fertility,Freya +General,What is the Capital of: Solomon Islands,Honiara,Music,What Was Marvin Gayes Only UK No.1,I Heard It Through The Grapevine,General,What is the largest single known gold object in the world,Tutankhamens Coffin +Geography,What Was So Important About The Oresund Bridge Between Malmo & Copenhagen Which Opened In July 2000 ,Because It Was The First Permanent Link Between Europe & The Swedish Peninsula ,General,Who was hercules' stepmother,Hera,General,What is a quahog,Type of clam +General,Which country was named after the sea people known as Peleset or Philistines,Palestine,General,Narcotics comes from the Greek - what it literally mean,Electric eels - put on foreheads,General,Which is the only arab country without a desert,Lebanon +General,Who played 'Kookie' in the T.V. series Seventy Seven Sunset Strip,Ed byrnes,General,In Texas its illegal to put graffiti on your neighbours what,Cow,General,Ray Bolger played who in The Wizard of Oz,Scarecrow +General,What are scaup,Wild ducks,General,"What is the name of a formal, written accusation of crime against a person, presented by a grand jury to a court, and upon which the accused person is subsequently tried",Indictment,General,What material is touched or knocked on for good luck,Wood +General,April is the cruellest month - which poet wrote that line,T S Elliot it refers to income tax,General,How much current does a south american eel put out,One amp,General,In which city is the Kentucky Derby run,Louisville +General,In the German episode of Fawlty Towers Sybil was in hospital for which operation,Ingrown toenail,General,Solfatara in Southern Italy is a what,Volcano,General,Dentophobia is a fear of ______,Dentists +General,Who invented the gatling gun,Richard gatling,Geography,Which capital city has the highest population (as at 2006)? ,Tokyo (c. 34 million) ,General,In Goldfinger name the actress painted gold,Shirley Eaton +General,A psychological disorder in which the patient refuses to eat.,Anorexia nervosa,General,John Gutzon Borglum Born March 25 1871 Is Responsible For The Design & Creation Of What,Mount Rushmore,General,In Finland who rides a goat named Ukko,Santa Clause +General,"What was the movie that starred the little furry creatures from ""Return of the Jedi""?",Escape from Endor,General,What is a camel's hair brush made of,Squirrel fur,General,In The World Of Music How Is “Graham Mcpherson” Better Known ?,Suggs From Madness +Entertainment,What is the name of the film in which Steven Segal's character dies?,Executive Decision,General,To drive out an evil spirit,Exorcize,General,A bowling pin need only tilt how far in order to fall down (it's a decimal),7.5 degrees +General,What is a Gopak?,A Russian dance,General,Where was pizza first invented ,Milan,General,"Which gentleman thief, created by E W Hornung was played by Anthony Valentine on TV ",Raffles  +General,Who played the television detective Frank Cannon,William conrad,Sports & Leisure,In Which Sport Is The Curtis Cup Contested ,Golf ,General,What is the study of prehistoric plants & animals called,Paleontology +Art & Literature,In Swifts Gullivers Travels What Is Gullivers Profession ,Surgeon ,General,Who invented the fountain pen in 1884,Lewis waterman, Geography,Bridgeport is the largest city in which state?,Connecticut +General,What are Common Darter and Southern Hawker types of?,Dragonflies,Science & Nature,"This spikey succulent, native of Africa, is often an additive in creams and lotions.",Aloe vera,General,El Dago was the name of whose first private plane,Frank Sinatra +General,The wheel was invented in about what year BC,3500,General,"Who, in 1976, were the 3 original actresses in ""Charlie's Angels""","Jaclyn smith, kate jackson & farah fawcett",General,What storied European mountain is known in Italy as Monte Cervino,The matterhorn +General,The marine iguana is native to which island group,Galapagos, Geography,In what country is the lowest point in South America?,Argentina,General,What's the largest USA metro area without an NHL franchise,Dallas +General,What house was the biggest in america until the Cival war?,The White House,General,In Frank Herbert's Dune what are the Makers,Sandworms - Shai- haulud,General,What was the only film about Vietnam made during the war,The Green Berets +General,Which English king's coronation was postponed because he was suffering from appendicitis,Edward vii,General,What is the term for the period of change in form of an organism from the larval to the adult stage,Metamorphosis,People & Places,Which Controversial Manchester Comedian Died In June 2007 Aged 76 ,Bernard Manning  +General,Which team won the recently concluded UEFA cup?,Liverpool,General,"In Greek myth, who gave birth to the winged horse, Pegasus, from her blood after she had been slain",Medusa,General,Who is credited with inventing the world's first mechanical calculating machine,Blaise pascal +Music,Which Successful Solo Artist Is The Regular Host Of The Country Music Awards,Vince Gill,General,"Proposed in 1944, a 'spat' was a unit of distance equal to 1012 metres (ten to the power twelve metres), for use in what science",Astronomy,General,"Where in the world would you find the Provinces of Santa Cruz, Rio Negro, Chubut and Neuquen",Patagonia argentina +General,In Denmark there is a 20 Kroner fine for not reporting what,Your own or anyone else's death,General,The closest relatives to anteaters are what,Sloths & armadillos,General,"In Shakespeare's The Merchant of Venice, what is the name of the merchant",Antonio +General,Apart From Smoking A pipe What Was Sherlock Holmes Favourite Vice,Injecting Cocaine,General,What was ALF's real name?,Gordon Shumway,General,A favourite spot for vampires to bite (that's one hell of a hickey!),Neck +General,Which sport is played at Roland Garros,Tennis in Paris on clay courts,General,"""Beverly Hills Cop"", when Axel Foley enters the hotel, he uses an alias. Who does he say he works for, and who does he say he's going to interview?","Rolling Stone,Michael Jackson",General,Which is the most populated territory in australia,New south wales +General,Lucy Hobbs Taylor 1867 first woman in the US to do what,Become a registered dentist, History & Holidays,This assassin of Julius Caesar was his friend.,Brutus,General,In what Australian state would you find Taree,New south wales nsw + Geography,What is the basic unit of currency for Jordan ?,Dinar,General,What do elephants do on average 2 hours a day,Sleep,General,Who is the father of the Russian alphabet,Saint Cyril +People & Places,Which singer wrote the autobiography 'Take It Like A Man''? ,Boy George ,General,What was advertised as anytime anyplace anywhere,Martini,General,G Roddenbery Star Trek 2 radical ideas Spock's Ears and what,Woman second in Command +General,"US, West Germany and Japan not participate in the 1980 Olympic games in Moscow, as a protest because of Soviet activity in which country",Afganistan,General,The first country to host the summer & the winter olympics in the same year,France,General,"Which movie is the line ""Snakes, I hate snakes"" from?",Raiders Of The Lost Ark +General,Sociophobia is a fear of ______,Society,General,What word did Harry Truman coin for his daily walk,Constitutional,General,What is the proper name for marsh gas,Methane +General,What segment of television receieves the ACE awards,Cable, Language,What was the language of ancient India?,Sanskrit,General,Which Chemical Element Derives It's Name From The Greek Word for Heavy,Barium +General,What American playwright titled his autobiography Timebends,Arthur miller,General,"Football Team, cincinnati _________",Bengals,General,The foodstuff 'carambola' is known by what alternative name,Starfruit +General,In France what kind of nuts are noisette,Hazelnuts,General,"Which film starrs jonathan taylor thomas, devon sawa, scott bairstow",Wild,General,"Shirley Bassey recorded a hit song taken from The Sound of Music, what was it",Climb every mountain +General,"The second most common phobia, anthropophobia, is a fear of?",People,Science & Nature,What Is The Substance From Which Nails And Hair Are Made ,Keratin ,Toys & Games,"In this card game, teams are designated North_South and East_West.",Bridge +General,In what sport do teams compete for the Swaythling Cup,Men's table tennis,General,Hitler annexed the Sudetenland in the 1930s. Of which country was it a part,Czechoslovakia,General,In South Dakota its illegal to show movies that picture what,Police getting beaten +Sports & Leisure,How many points are awarded for a drop goal in rugby league? ,One ,General,Ripken What planet is farthest from the sun in the Milky Way,Pluto,Geography,Name the largest cathedral in the world.,St. peter's +Tech & Video Games,What does LED stand for?,Light Emitting Diode,General,"In the 1969 filin ""Anne of the Thousand Days"" starring Genevieve Bujold and Richard Burton, who was the Anne of the title",Anne boleyn,General,"What Do Cleopatra, Margaret Thatcher And Mia Farrow Have In Common?",All Have Twins +General,Who played commander riker in 'star trek',Jonathan frakes,General,What word appears over 46000 times in the Bible,And,General,"The application of chemical principles & techniques to geologic studies, to understand how chemical elements are distributed in the crust, mantle, & core of the earth",Geochemistry +General,The Red Cross was initiated in what year,1862,General,What feature of a triangle makes it scalene,Different side lengths,General,When was the newspaper 'pravda' first published,1912 + History & Holidays,What was the nationallity of Rasputin ?,Russian, History & Holidays,Which Classic Scary Movie Features The Line 'They're Here' ,Poltergeist , Geography,What is the capital of Thailand?,Bangkok +General,What is the shadow-casting pointer on a sundial called,Gnomon,Music,Which Chaka Khan Smash Was Written By Prince And Featured Stevie Wonder,I Feel For You,General,Russophobia is the fear of,Russians +General,Alain Boubil - Claude-Michael Schonberg music what hit show,Miss Saigon,General,"Who wrote the poem ""The Owl and the Pussycat""",Edward lear,Art & Literature,What famous character did Edgar Rice Burroughs create?,Tarzan +General,How many constellations are used in modern astronomy?,88,General,"What kind of money, more than real money, is printed in a year throughout the world",Monopoly, History & Holidays,What Happened If You Looked A Gorgon In The Eye? ,You Turned To Stone  +General,What country borders sudan to the north,Egypt,General,Commandaria is a desert wine made for over 800 years - where,Cyprus,General,What do spelunkers explore,Caves +General,How many rings on the Olympic flag,Five,General,What character did Michael J Fox play in the film Back to the Future,Marty mcfly,General,Samuel Morse the inventor was originally what till he was 46,Portrait Painter +Music,"In 1963 The Cougars Had A Hit Instrumental ""Saturday Night At The Duckpond"" But Were Banned By The BBC For ""Defacing A classical Melody"" What Ballet Did The Melody Come From",Swan Lake / Tchaikovsky, History & Holidays,Name James Dean's three films. ,"A Rebel without a cause, East of Eden, Giant ",General,Cedrick Errol Was The Name Given To Which Epnymous Child Hero Of The 19th Century,Little Lord Fauntleroy +General,After sex what does the female marine bristleworm do,Bites off eats penis,Music,Which 1979 Earth Wind And Fire Track Featured The Emotions,Boogie Wonderland,General,Cockney Rhyming slang what is Mutt (mutt and jeff),Deaf +General,What is the flower that stands for: sensuality,Spanish jasmine,Science & Nature,What Would You Find In The Pods Of The Pisum Stativum ,Peas ,General,In which country did the study of geometry originate,Egypt +General,In Greek mythology who ferries the dead across the river styx,Charon,General,What is the flattest continent,Australia,General,"Which group sang the song ""sleeping child""?",Michael Learns To Rock +Music,"Bluebird, Tishbite & Violaine Were All Minor Hits For Whom",The Cocteau Twins,General,"On the cartoon show 'the jetsons', how old is judy",Fifteen,Science & Nature, The stegosaurus had a brain that weighed only 2 ounces and was no bigger than a __________,Walnut +General,"Who was assassinated on november 22, 1963 in dallas",President john f kennedy,General,Which fruit is nicknamed the 'Date Plum' or 'Sharon Fruit'?,Persimmon,Food & Drink,What Does The Term 'A La Carte' Actually Mean? ,From The Menu  +General,"Which Opera Does ""Pavarotti's"" ""Nesun Dorma"" Actually Come From",Turandot,General,Who was the first black Wimbledon singles champion,Althea gibson,Music,"Who Were ""Hungry Like The Wolf"" In 1982",Duran Duran +General,Name the computer developed fromTuring's bombes at Bletchley Park to decode the German Enigma codes during World War 2.,Colossus,General,In Bewitched name Samantha's identical cousin,Serena,General,"What percentage of the earth's surface is land, approximately",Thirty percent +General,What are silver coins made from,Copper Nickel,General,What plant was named after the Greek goddess of the rainbow,Iris,Music,"What Position In The UK Charts Did ACDC's Hightway To Hell Reach In 1979 1, 4 Or 56",56 +General,Who was the first astronaut to orbit earth 3 times,John glenn,General,What is the most sensitive finger,Forefinger,General,"Which Singers Debut Solo Album Was Entitled ""Schizophonic""",Geri Halliwell +General,What city has the worlds biggest taxi fleet,Mexico - over 60000,General,Salem is the capital of which U.S. state,Oregon,General,"Dirk, poniard, and stiletto are all types of what",Daggers + History & Holidays,"In 1979 who was revealed to be the fourth Russian spy in the Burgess, MacLean and Philby affair? ",Anthony Blunt ,General,Sydney is on the east coast of ______,Australia,General,AMSTRAD companies name comes from what i.e. what mean,Alan Michael Sugar Trading + History & Holidays,What is the more common name for the plant Viscum Album? ,Mistletoe ,Sports & Leisure,In Which Sport Would You Be Awarded The Lonsdale Belt ,Boxing ,General,Which Famous Building Was Designed By Jorn Utzon?,Sydney Opera House +Science & Nature,What Apparently Inappropriate Name Is Used For A Gathering & A Breeding Place For Seals ,Rookery ,Geography,How many stars are on the flag of new zealand ,Four ,General,Who began her career making death masks from the severed heads of those executed by the guillotine after the French Revolution,Madame tussaud +General,Name Harry Potters non magical cousin,Dudley Dursley,General,Frankfort is the capital of ______,Kentucky,Sports & Leisure,Where was footballer John Barnes born? ,Jamaica  +Geography,What is the capital of Cuba,Havana,General,Boaz appears in which book of the Bible,Ruth,Science & Nature,What Is The Larva Of A Fly Commonly Called? ,A Maggot  + Geography,What is the basic unit of currency for Lithuania ?,Litas,General,What is another name for a black leopard,Panther,Sports & Leisure,Snooker player Peter Ebdon has a significant physical disadvantage. What is it? ,He is colour blind  +General,23 29 31 first 3 impossible numbers in what pub game,Darts - cant score 1 dart,General,"Which herb is similar in appearance to parsley, but its leaves have a slight flavour of aniseed",Chervil,Music,Which Stones Album Title Parodied That Of A Beatles Release,Let It Bleed +General,Who won the 1995 rugby world cup,South africa,Geography,What sea is between Italy and Yugoslavia,Adriatic,Geography,What and when is the biggest national celebration every year in Australia? ,Australia Day  +General,What name is given to the large Russian utensil for making tea?,Samovar, History & Holidays,What did Henry Shrapnel invent?,The exploding shell,General,"Who, in 1948, became the first Prime Minister of Israel",David ben-gurion +General,Who was Bette Midlers piano player before going solo,Barry Manilow,Art & Literature,"The fallacy of personifying inanimate objects, often in bad taste?",Pathetic fallacy,General,What tree provided the laurel of classical wreaths,Bay +General,In Which US State Is Yale University?,Connecticut,General,Oporto in Portugal stands on what river,The Duoro,General,What is the fear of frogs known as,Ranidaphobia +General,"Which substance used as a bleach, can also be used as the oxidizing agent in rocket fuel",Hydrogen peroxide,Geography,The largest policeman's beat (territory) in Europe is in which country? ,Scotland ,General,Sting Vogue Moschino Adidas Police Wires Ice types of what,Spectacles - Glasses +General,The Pogues took their name from Pogue Mahone - what mean,Kiss my arse,Food & Drink,Little round chocolate candies are known as _&m's.,M,Entertainment,Who starred in the 1952 film 'Niagara'?,Marilyn Monroe +General,What was jean-claude van damme's original stage name,Cujo,Entertainment,Who was always trying to get rent from Andy Capp,Percy,General,How did sir walter raleigh die,Execution +Geography,What country boasts the southernmost point in continental europe ,Spain ,General,What pacific island is Noumea the capital of,New caledonia,Sports & Leisure,Which Sport Was Inroduced Into The Olympic Games In 1988 ,Tennis  +General,In what country were Trabant cars made,East Germany,Food & Drink,Where is most of the vitamin C in fruits?,Skin,General,What is the state bird of Washington,Willow goldfinch +General,Name the smallest breed of dog,Chihuahua,Science & Nature," Wandering __________ spread their wings, clack bills, and shake heads in a ritual dance. Bonds between courting birds may last the whole of a 50_year lifetime.",Albatrosses,General,Isaiah Sellers did it before Samuel Clements - did what,Used Mark Twain +General,Corinthian Ionic and what are the orders of Greek architecture,Doric,Music,Which Cover Of An Elvis Presley Song Gave The Pet Shop Boys A No.1 Hit In 1987,Always On My Mind,General,"Which musical features the song ""Getting to Know You""",The king and i +General,How many kilograms make up a metric tonne?,1000,General,What Was The Name Of Michael Jacksons Pet Chimp,Bubbles,Music,In the Elvis Presley song Jailhouse Rock which instrument did Spider Murphy play?,Saxophone +General,A statement made by placing your hand on a bible,Oath,Science & Nature," The kakapo is a nocturnal burrowing parrot of __________ that has a green body with brown and yellow markings. Its name is from Maori and means ""night parrot.""",New zealand,General,What was the constituency of Rik Mayall's character alan B'stard in the TV sitcom 'The New Statesman'?,Haltemprice + Geography,Accra is the capital of ______?,Ghana,Music,"Who Had A 80's Hit With The Song ""Lipstick, Powder & Paint""",Shakin Stevens,Music,"Who Sang With Glen Campbell On His No.3 Hit ""All I Have To Do Is Dream""",Bobbie Gentry +General,Room or space immediately under the roof of a house,Attic,Entertainment,Who is the fastest mouse in all of Mexico?,Speedy Gonzalez,General,Who was known as the Father of Science Fiction,Jules Verne +General,Who wrote The Stars Look Down and Hatter's Castle,A j cronin,Entertainment,An adventurous penguin named Tennessee Tuxedo had a sidekick named _______,Chumley,General,Both sexes get them but men more often - get what,Hiccups +General,What artist composed the classical work Tannhauser,Wagner,General,Northern species of duck with soft feathers,Eider, History & Holidays,Who Released The 70's Album Entitled Tonights the Night ,Neil Young  +General,Epistemophobia is the fear of what,Knowledge,General,What do eccrine glands produce,Sweat,General,"In the early 20th century, rattlesnake venom was used to treat which illness",Epilepsy +General,Barrel size - what wine barrel contains 126 gallons,Pipe,General,"In Greek mythology, who personified dread",Diemos,Geography,Name the U.S. state with the smallest population.,Alaska +General,What is the most rural state in the u.s,North dakota, History & Holidays,Who was the first chancellor of West Germany after WW II,Adenauer,General,Which Country Changed From Driving On The Left To Driving On The Right In 1967?,Sweden +Music,After Initial Sucess With Two Tone Which Group Changed Their Label To Go Feet In 1980,The Beat,General,16th century where the most fashionable place to wear a ribbon,Pomaded grown female pubic hair,Science & Nature,What 2 words term is given to a simulated 3d computer environment ,Virtaul Reality  +General,Syncope is the medical name for what condition,Fainting,General,The male of what species explodes on mating - then dies,Honeybee, History & Holidays,Who was the father of Elizabeth I?,Henry VIII +Science & Nature,What Is The Name Of The Instrument Used For Listening To The Heart And Lungs ,A Stethoscope ,General,Material burned to give fragrant fumes,Incense,Geography,Which element makes up 8.13% of the Earth's crust,Aluminium +General,In which film did Woody Allen direct Sylvester Stallone,Bananas - 1971 – Stallone a punk,General,In the game 'banjo-kazooie' what is the witch's name,Gruntilda,General,How was USA president James Buchanan different from all rest,Batchelor maybe gay +General,"Which popular English dessert consists of strawberries, whipped cream and lemon meringue?",Eton Mess,General,What was created in France in 1885 by Frederic Auguste Bartholdi With The Intention Of Enlightening The World,The Statue Of Liberty,Science & Nature," The raccoon derives its name from the Indian word meaning ""he who scratches with his __________",Hands +General,The day after Christmas day,Boxing day,Science & Nature,What Is The Term Used For A Drug Designed To Reduce Body Fluid ,Diuretic ,General,"In 'the great escape', how many finally made it to freedom",Three +General,Roy Chapman became baseball's first fatality in which year,1920,General,What weapon is tattooed on Glen Campell's arm,Dagger,Science & Nature,"What device used to be described in Greek as ""watcher of the small"" ?",Microscope +General,Electronic lift control what is the second biggest organ in the human body,Liver,General,What is the highest point on Exmoor?,Dunkery Beacon,General,Who were the 1991 nba champions,Chicago bulls +General,Aotearoa is Maori name for New Zealand - what's it mean,Long Daylight,General, An instrument on a car to measure the distance travelled is called a(n) _________.,Odometer,General,Who was The First Male Solo Act To Achieve A US Number One On The Billboard Hot 100,Acker Bilk +General,"In Knight Rider,what's the real last name of Michael Knight?",Long,Toys & Games,What does a twitcher look for,Birds,General,Attend a party uninvited,Gatecrash +General,What does a brandophile collect,Cigar bands,General,Prokofievs opera War and Peace has what first to its credit 1973,First one done Sydney Opera House, History & Holidays,"Roman statues were made with detachable whats, so that one what could be removed and replaced by another?",Heads +General,Who sang the theme song in 9 to 5,Dolly Parton,General,"Small area on a computer screen in which the user is prompted to provide information, select commans etc",Dialogue box,General,What is the Capital of: Chile,Santiago + History & Holidays,Yorkshireman William Strickland is believed to have brought the first what to Britain from North America in 1526? ,Turkey ,General,Name first animated film to be nominated for best picture Oscar,Disney Beauty and the Beast,Sports & Leisure,How many human players are in a polo team? ,Four  +General,What is the world's second largest religion,Islam,General,What has a 21 inch tongue,Giraffe,General,What is the name of the queen's Scottish residence,Balmoral castle +General,In what field did Frances Mary Buss and Dorothea Beale become well known in the 19th Century,Girls higher education,Music,What Was The First Spice Girls Single Not To Top The Charts,Stop, History & Holidays,According to The Everly Bros What Time Did Suzie Finally Wake Up ,4 O Clock  +Science & Nature, __________ has more homeless cats per square mile than any other city in the world.,Rome,General,Whom did Idi Amin replace as leader of Uganda in 1971?,Milton Obote,General,"Who told a senate committee: ""billy carter is not a buffoon, boob or wack",Billy carter +General,Sanskrit is an old language - what does the word mean,Put together – Perfected,General,Stag Party was the original name of what,Playboy Magazine,General,To what category of numbers does 'pi'belong,Transcendental +General,Who spoke for the first time in a Bugs Bunny cartoon in 1951,Roadrunner,General,What are the 2 main divisions of chemistry,Organic and inorganic,General,"The characters, Farmer Boulderwood and Sergeant Troy appear in which Thomas Hardy novel",Far from the madding crowd +Science & Nature,At what angle above the horizon must the sun be to create a rainbow? (in degrees),Forty,General,In Columbus Ohio it is illegal to sell what on Sunday,Cornflakes,Toys & Games,Plastic vehicle equipped with spin_out brake.,Big wheel +Food & Drink,What is the name of the two-coloured oblong cake covered in almond paste? ,Battenburg ,General,In which city are the Headquarters of OPEC located,Vienna,General,Baseball: the Baltimore ________,Orioles +Sports & Leisure,Which player won her last tennis Grand Slam Tournament in Paris in 1999? ,Steffi Graff ,General,What President appears on the US $100000 bill,Woodrow Wilson,General,What shape are ice crystals,Hexagonal +General,Dragon Stout is brewed in what city / country,Kingston Jamaica,General,A person employed to drive a car,Chauffeur,Music,"Who played drums with Black Sabbath, Whitesnake and Rainbow, and had a hit in his own right with “Dance with the Devil”.",COZY POWELL +Geography,Which element makes up 5% of the Earth's crust,Iron,General,What movie starred Lee Marvin as twins Kid Shelleen and Tim Strawn,Cat,General,Book It was a bright cold day in April and clocks were striking 13,1984 George Orwell +People & Places,What were the surnames of Bonnie & Clyde ,Clyde Barrow & Bonnie Parker ,Music,What Is Morrisey's Christian Name,Stephen,Entertainment,"Was Shirley Temple 21, 25 or 29 when she made her last film?",21 +General,The plant life in the oceans make up about what percent of all the greenery on the earth,85,General,Shop selling exotic cooked meats and cheeses,Delicatessen, Geography,What is the basic unit of currency for Iran ?,Rial +Music,What Musical Act Spent The Most Weeks At No.1 Throughout The Year 2006,"Gnarls Barclay, Crazy",General,Who is the Patron Saint of grocers,Saint Michael, Geography,What is the basic unit of currency for Thailand ?,Baht +General,What is philography,Autograph collecting,General,Postage stamps were first introduced in to Britain in what year,1840,General,Name the English chemist who first isolated sodium,Sir Humphry Davie +General,Who designed the difference engine finally built in 1991,Charles Babbage's computer 1796,General,Between October and March what is illegal in Indiana,Taking Baths,Food & Drink,After Whom Is Caesar Salad Named ,"Caesar Cardini Who Would Assemble It For His Guests In Tijuana, Mexico In The 1920's " +General,Which London MP is more famous as an actress,Glenda jackson,Music,"Who Sang About ""The Greatest Love Of All"" In 1986",Whitney Houston,General,In which film did matthew modine play a schizophrenic war veteran,Birdy +General,Where on the human body is the ulnar loop,Finger,General,What's the correct name for a young un-bred male fur seal,Batchelor,Art & Literature,From whom did Bilbo obtain The Ring,Gollum +General,"In Greek mythology, what city did cadmus found",Troy,General,Goa used to be a colony of which nation,Portugal,General,Where was the 1995 rugby world cup held,South africa +General,Which plant has the scientific name Convallaria,Lily of the valley, Geography,What is the basic unit of currency for Kazakhstan ?,Tenge,General,Lutitia is what the Romans called where,Paris +Art & Literature,"A method of watercolor painting, but prepared with a more gluey base, producing a less transparent effect. ",Gouache,General,How many ribs does a human have,24,General,Obesophobia is a fear of ______,Gaining weight +General,Who invented the vacuum flask,James dewar,General,What was the most bombed place in WW2,Malta,General,On which river does Londonderry stand?,Foyle + History & Holidays,Who Is The Only British Prime Minister To Have Been Assassinated? ,"Spencer Perceval, 1812 ",General,During which month is the shortest day in the Northern hemisphere?,December,General,How long is the longest tunnel? (in kms),169 +Music,Paul Weller Ocean Colour Scene & Dodgy All Contributed To A Tribute Album Of Which Band,The Small Faces,General,Michael Henchard better known as what eponymous literary hero,The Mayor of Castorbridge T Hardy,General,In Which Long Running Tv Show Are You Likely To Find “Susie Dent”?,Countdown +General,"Capone Music Artists: Who did ""Cheap Sunglasses"" in 1979",ZZ Top,General,What Was The Biggest Selling Album In The UK In The 1970's & Also The Title Of One Of Their Biggest Hits,Bridge Over Troubled Water,General,What was the sequel to The Rocky Horror Picture Show,Shock Treatment +General,What is the literal translation of the word brandy,Burnt wine,General,Does a person's heart rate increase or decrease during exercise,Increase,Geography,____________ averages the greatest number of shark attacks annually _ an average of 13.,Florida +General,Information from a reliable source is said to come from where,Horse's mouth,General,"Who was the ""keymaster"" in Ghostbusters?",Rick Moranis,General,C'est manifique mais ce n'est pas la guerre 1854 about what,Charge Light Brigade +General,Which country has the currency 'yen'?,Japan,General,Who were the two disappointed candidates when Richard Nixon was elected as U.S. President in 1968,Hubert humphrey george wallace,Music,Which Single When It Peaked At No.2 In The Uk Charts in 1967 Ended a Run Of Eleven Consecutive Number Ones,Penny Lane/Strawberry Fields Forver +General,Which city is the capital of Tuscany,Florence,General,Which famous scientist's first published work was a monograph on barnacles,Charles Darwin,General,What fruit does the 'stackspur golden delicious' tree produce,Apples +General,Legal Terms: A crime more serious than a misdemeanor.,Felony,General,What color do you get in mirc using control code number 4,4red red,Science & Nature,What Do The Initials SLK mean In Relation To Mercedes Cars ,Spory Light Compact  +Science & Nature,What is the horn of a rhinoceros made of,Hair,General,What was the name of Judge Smales grandson in the movie 'Caddyshack'?,SpauldingIn,General,Who designed clothes under the emporio label,Georgio Armani +General,What is Falaka,Turkish feet beating,General,A castrated horse,Gelding,Toys & Games,What is the name of the board that Baduk is player on ?,Goban + History & Holidays,Which Spanish explorer first travelled to Jamaica?,Christopher Columbus,Geography,"""Utah"" is from the Navajo word meaning _________",Upper,General,"Who wrote the classic spy novel ""The Thirty-nine Steps""",John buchan +General,Who enacted a law requiring cigarette manufacturers to put health warnings on their packages,Lyndon b johnson,General,"London link Prince Edward's, Prince of Wales, Her Majesty's",Theatres,General,What animal has the same name as a high church official?,Cardinal +Food & Drink,What is the name of a small barrel of beer of nine gallons capacity? ,A firkin ,Music,You're Moving Out Today Was A One Hit Wonder For Which US Female Vocalist,Carole Bayer Sager,General,"Which Fictional Movie Character Lived On An Island Called ""Seahaven""",Truman (The Truman Show) +General,Clothes designer Alexander McQueen works for which fashion house,Givenchy,Entertainment,What was Citizen Kane's dying word?,Rosebud,Entertainment,Who is Declan Patrick McManus better known as?,Elvis Costello +General,Which tennesee williams play is about a sicilian-american woman,Rose tattoo,General,More than one half of New Brunswick is surrounded by,Water,General,Paralipophobia is the fear of,Neglecting duty +General,In films who could win the Golden Boot award,Actors in cowboy films,General,"All pinball machines are made near this city, the pinball capital of the world",Chicago,General,The island of Mauritius lies in which ocean,Indian +Geography,What Are Switzerlands Three Recognised Languages ,"French, German, Italian ",General,From which prison in South Africa was Nelson Mandela released,Victor verster,Music,Which House Did Lovebug Starski Sing About In 1986,Amityville (The House On The Hill) +Art & Literature,Who created Sherlock Holmes ?,Sir Arthur Conan Doyle,General,Who is the Patron Saint of Germany and sodomy,Saint Boniface,General,License plates: what does sixpak run,Liquor store + History & Holidays,What was the name of the society heiress who stole 8 million pounds worth of pictures in 1974 and was sentenced to 8 years in prison ,Dr Bridget Rose Dugdale ,General,Defeated in a footrace in greek mythology what did atlanta want to do until she was defeated in a footrace,Remain unmarried,General,What british prime minister claimed 'hitler has missed the bus',Neville +Sports & Leisure,What was Mohammed Ali's original name?,Cassius Clay,General,What is a sunbeam that shines down through the clouds called,Crespucular rays,General,"Near which German town would you find Fredrick the Great's retreat, Shloss Sanssouci",Potsdam +General,Bennie Hills Ernie fastest milkman in the west - name his horse,Trigger,Sports & Leisure,Hockey: The Pittsburgh __________.,Penguins,General,Which 90s Grand National Winner was trained by Martin Pipe,Minnehoma +General,What tennis player made it to finals US open 8 times in 80s,Ivan Lendl,General,What flavours cavatappi al nero pasta,Squid Ink,General,Janette Rankin in 1917 first to do what,Woman elected to Congress +General,What country was formerly known as siam,Thailand,General,Thumb Lock Mongolian Release Mediterranean Draw what sport,Archery,General,What is 'confederatio helvetica' in english,Helvetic confederation +General,What is the name of the big muscle used in breathing that seperates the chest from the abdomen,Diaphragm,General,Who painted Diana and The Bathers,Renoir,General,"Who lamented ""all my ex's live in texas""",George strait +General,What was the royal residence before buckingham palace,St james court,Entertainment,Secret Identities: Maggie Sawyer,Maggie sawyer,People & Places,Which Country Owns Easter Island ,Chile  +Music,In 1970 Sessionman Tony Burrows Famously Appeared On Top Of The Pops Singing With 3 Different Groups Can You Name Any Of Them PFE,"Brotherhood Of Man, Edison Lighthouse, White Plains",Food & Drink,Cocktails: Gin and Collins mix make a(n) __________.,Tom Collins,General,The Japanese believed Earthquakes caused by underground what,Giant Spider +General,The wind what is the best score in blackjack,21,General,The Queen of Spades and Jack of Diamonds combine to form the key combination in which card game,Bezique pinochle,General,Based on a book by William Styron which film stars Meryl Streep and Kevin Kline,Sophie's choice +General,Who was king arthur's court wizard,Merlin,General,This is the medical name for shoulder blade?,Scapula,Music,Who Let The Dogs Out In 2000,The Baha Men + History & Holidays,He ordered the persecution of Christians in which Peter and Paul died.,Nero,General,What is an island formed by a volcanic eruption,Atoll,Sports & Leisure,Which is the only position in soccer allowed to handle the ball?,Goalkeeper +General,What is the Capital of: Tanzania,Dar es salaam,Music,"Who Did Soft Cell Team Up With For ""Somethings Gotten Hold Of My Heart""",Gene Pitney,Music,"Who Had A Hit With ""Gilly Gilly Ossenfeffer Katzenallen Bogen"" By The Sea",Max Bygraves +General,What's the most common name in nursery rhymes,Jack,General,At which university did the poet Philip Larkin work as a librarian,Hull, History & Holidays,Israel occupied the Golan Heights. Whose territory was it?,Syria +Music,What Was The Name Of The Debut Single For The 90's German Band Snap!,The Power,General,This radical Agrarian communist organization controlled Cambodia in the 1970s until the Vietnamese removed them from power.,Khmer rouge,Entertainment,Where did Mighty Mouse get his superpowers?,Supermarket +Geography,"Among the fifty_two _____________ churches Sir Christopher Wren created from 1670 to 1711, the greatest was St. Paul's Cathedral.",London,General,What is a type of west indian popular music,Reggae,General,What U.S. state has the smallest population,Alaska +Music,"To Who Was U2's Song ""Pride In The Name Of Love"" Dedicated",Martin Luther King,General,What are ratite birds the only ones not to do,Fly,General,Who wrote the comic opera Robinson Crusoe,Offenbach +General,What is the flower that stands for: pure and ardent love,Double red pink,General,"""The Beach Boys"" First UK No.1 Hit Was ""Good Vibrations"" But Who Actually Wrote The Song",Neil Diamond,General,What former resistance fighter was Israel's first Likud Party prime minister,Menachem begin +General,"In the opera 'La Traviata', what does La Traviata mean",The fallen woman,General,Who was Bram Stoker's most infamous character,Dracula,General,What is the fear of telephones known as,Telephonophobia + Geography,What is the capital of Wisconsin?,Madison,General,"In 1962, who lamented that 'only love can break a heart'",Gene pitney,Sports & Leisure,Which is the only country to have played in every World Cup since it started in 1930? ,Brazil  +General,What did Esso become,Exxon,Music,In The Beatles Song “Penny Lane” What Does The Fireman Have In His Pocket,A Portrait Of The Queen,General,Give either of the food items eaten by the Owl and the Pussycat.Mince,Quince +General,What were the two birds that noah sent out from the ark?,Raven & dove,Music,In Which 2 Consecutive Years Did Amen Corner Achieve Four Top Ten Hits Including A No.1,1968 & 1969,General,What is the unit of measurement which is equal to the mean distance from the Earth to the Sun,Astronomical unit +General,What novel was alexandra ripley hired to pen a sequel to,Gone with the wind,General,"In the tv sitcom 'married with children', what is the dog's name",Buck,General,In New York its illegal to teach your parrot to do what,Squawk +General,How many blackbirds were baked in a pie,4 & 20,General,What is Orchesis - either professional or amateur,Art of Dancing,Music,"Born Roberta Streeta, Whose First Single Was The Strange ""Ode To Billy Joe"" About A Young Mans Suicide",Robbie Gentry +General,When is the longest day in the northern hemisphere,June,Music,"The American Band Chicago Have Numbered All Their Albums From ""Chicago 2"" In 1970 Onwards What Number Had They Reached By 1986",Chicago 18,Science & Nature," __________, like other equids, have three gaits: the walk, the trot, and the gallop.",Zebras +General,The British consume twice as much per capita as the US - what,Baked Beans,Food & Drink,How big is a Jeroboam compared to a normal size bottle? ,4 Times ,General,Who was found dead in Hollywood's Landmark Hotel 4 Oct 1970,Janis Joplin +General,What does a Yellow Flag Red stripes mean in motor racing,Slippery Track,Sports & Leisure,"When Boris Becker Was The First Unseeded Player To Win Wimbledon, Who Did He Beat In The Final? ",Kevin Curran ,General,Whose attendance compulsory at priests banquets in Egypt,Mummies – dead reminded short life +General,In what country is the worlds biggest national park,Canada,Geography,What country are the Islands of Quemoy and Matsu part of,Taiwan,General,Canberra is the capital of ______,Australia +General,What is the Capital of: Kyrgyzstan,Bishkek,General,Philosophobia is the fear of,Philosophy,General,Tall Dark And Gruesome Is The Auto Biography Of Which Famous Person,Christopher Lee +General,What do you call a weasel whose coat turns white during the winter,An ermine ermine,Art & Literature,How many plays is Shakespeare generally credited with today?,Thirty seven,General,What is the fear of definite disease known as,Monopathophobia +Food & Drink,For what was licourice used in ancient Egypt? ,Medicine ,Music,Phil Oakley Was The Lead Singer With Which Band,The Human League,General,Phyllis Pearsall Is Responsible For The Design And Creation Of What Book First Published In 1936,The London A-Z +General,What is the Capital of: Chad,N'djamena,General,"Cross country, Riding, Swimming, Fencing what's missing",Pistol Shoot Modern Pentathlon,General,A dog was arrested in Seville in 1983 for what crime,Handbag Snatching +General,"Ulna, radius, & clavicle are types of __________",Bones,General,The Pirate Khair-ed-Din had what Italian name meaning redbeard,Barberossa,General,What sort of lenses float on eye fluid,Contact lenses +General,"Who, in 1957, was the first-black player to win a singles title at Wimbledon",Althea gibson,Music,In Which Song Released First in 1966 & Again In 1991 Will You Hear The Name Of Americas First Woman Astronaut Uttered Time & Time Again (66 Wilson Picket / 91 The Commitments),Mustang Sally,General,US actor William Gillette invented which characters phrase,Elementary my dear Watson + History & Holidays,What 'C' Was A 1992 Horror Movie And A 2006 Hit For Christina Aguilera ,Candyman , History & Holidays,Which president was responsible for the 'Louisiana Purchase'?,Thomas Jefferson,General,What was the top single record in 1950 by the Weavers,Goodnight irene +Music,Which Neil Diamond Huit Was Inspired By The Movie E.T,Heartlight,General,Name the computer which beat World Chess Champion Garry Kasparov in 1997,Deep blue,General,Who was with Macbeth when he met the witches,Banquo +General,The visible spectrum of light ranges from red to ________,Violet,General,What does am/fm stand for,Amplitude modulation/frequency modulation,General,Which man in the bible is simply described as 'living forever'?,Enoch +Science & Nature,In Which Period Did The Diplodocus Live ,Jurassic ,Science & Nature, The only country in the world that has a Bill of Rights for Cows is __________,India,General,Which is the largest joint in the human body,Knee +General,Who invented the Carpet Sweeper?,Melvin Bissel,General,In which film did paul newman and robert redford hold hands and jump into a river,Butch cassidy and the sundance kid,General,Who married the Owl and The Pussycat,The turkey +General,Which meridian does the international date line approximately follow through the pacific ocean,180 degree meridian,General,What is 3 days and 6 hours in minutes,4680,General,Cher Ami saved the Lost Battalion in 1918 what was it,Pigeon +General,"The word ""angel"" is derived from the Greek term angelos, from the Hebrew experssion mal'akh, usually translated as what",Messenger,General,Which treaty between Great Britain and China ended British rule in Hong Kong?,Treaty of Nanking,General,Gypsum is a hydrated sulphate of which metal,Calcium +Sports & Leisure,Name The Famous Sportsman Who Lit The Torch At The Opening Of The 1996 Olympic Games? ,Muhammad Ali ,General,What ocean lies to the north of Alaska,Arctic ocean arctic,Music,"Who Had A Top 5 Hit In 1971 With ""Me And You And A Dog Named Boo""",Lobo +General,What was russian america after 1867,Alaska,Geography,"In ________________, the Presidential highway links the towns of Gore and Clinton.",New zealand,Science & Nature,What Did Alfred Nobel Invent Before Initiating His Nobel Peace Prize Award Scheme ,Dynamite  +Mathematics,An angle greater than 180 degrees and less than 360 degrees is a(n) ________ angle.,Reflex,Science & Nature, Baby rattlesnakes are born without __________,Rattles,Music,What Was Paul Young's 1983 Million Selling LP Called,No Parlez +General,Who was meant to play Annie Okley but was replaced in 1950,Judie Garland Annie get Your Gun,General,"Pussycat sings 'now the country song forever lost its soul, when the guitar player turned to rock n roll ______' what's the song title",Mississippi,General,What is another name for a pigskin,Football +Music,"Which Belgian Artist Is Remembered For ""Ca Plane Pour Moi""",Plastic Bertrand,General,What aid to cooking was first manufactured by Mark Gregoire in 1954,Non stick pans,General,What is the flower that stands for: power,Cress + History & Holidays,How Many Points Does A Snowflake Have ,Six ,General,How does a tortoise drink water,Thru its nose,Science & Nature,What makes plants green ,Chlorophyll  +Music,"Who Recorded ""Schools Out"", ""Muscle Of Love"", ""Billion Dollar Babe""",Alice Cooper,General,Exodus and which Bible book list the ten commandments,Deuteronomy,General,What is the fear of satan known as,Satanophobia +Music,"""It's Too Late Now"" Entered The Charts Twice In 1963 For Which Group",Swinging Blue Jeans,General,"Handlebar, toothbrush and walrus are all types of what",Moustache,General,Who was the Queen of Sparta,Gorgo +General,Paper Porter Dresser Mud Dauber types of what,Wasp,General,Where would you find A Pope Empress Hermit and Juggler,They are Tarot Cards,General,What was the last sequel to win best picture award,Silence of the Lambs to Manhunter +General,Who is the lead singer of the group yes,John anderson,General,In the Fantastic Four what is Mr Fantastic name,Reed Richards,General,"""The curfew tolls the knell of parting day."" which poems start",Elegy written in a country churchyard +General,Billion what can release approximately one billion grains of pollen,Ragweed plant,Science & Nature,What large herbivore sleeps only one hour a night,Antelope, History & Holidays,Which song was a UK chart hit for `The Goodies' in 1975? ,The Funky Gibbon  +General,A male cat is called a what,Tom,General,Myctophobia is the fear of,Darkness,General,Until 1862 there was a tax in England for those who used what,Soap +General,Greek mathematician cylinder enclosed sphere carved on grave,Archimedes,Science & Nature,The fourth planet from the sun is _______.,Mars,General,"What Is The Only Breed Of Insect That Has Both A ""King"" And A ""Queen""",Termites +Sports & Leisure,"Who, in 2002, became the first woman in 11 years to win the BBC Sport Personality Of The Year? ",Paula Radcliffe ,Mathematics,What is 65% of 60?,39,General,What is the official language of Papua New Guinea?,English +Music,Jello Biafra Was The Lead Singer For Which American Punk Band,The Dead Kennedys,General,Nessiteras Rhombopteryx Latinised name of what - as a Hoax,Loch Ness Monster,Science & Nature,How many teats does a cow have,Four +General,What kind of a head does a Criosphinx possess,Ram,Food & Drink,From Which Country Does The Beer 'Red Stripe'' Originate ,Jamaica ,General,"Where is the land of 10,000 lakes",Minnesota +General,In Bewitched Aunt Clara had a collection of what,Doorknobs,Science & Nature,How fast (mph) can a kangaroo hop?,Forty,General,Finally a good old body sound where is Farta,Nigeria +Music,"Which group had a top 10-album chart success with 'What's the story, morning glory'?",Oasis,Sports & Leisure,How Many Dice Do You Have In A Backgammon Set ,5 Dice ,Science & Nature, A bison can jump __________,6 feet +Sports & Leisure,If All The Race Courses In Britain Were Listed Alphabetically Which Would Come First ? ,Aintree ,General,The fleur de lis is a representation of which flower,Iris,General,Who is the Patron Saint of boy scouts,Saint George +Music,"""Rainy Days & Mondays"" Was A Hit For Which 70's Group?",The Carpenters,General,"Who was ulysses' son, who grew to manhood in his absence",Telemachus,General,She died at 28 but her book on household management famous,Mrs Beeton +General,"The National day of Ethiopia, 21st March, commemorates a victory at Adowa in 1896 over which country",Italy,General,What is written at the bottom of a Oiuja board,Good bye,General,What is the fear of moths known as,Mottephobia +Geography,Who Employed Henry Hudson To Search For The North West Passage ,The Dutch East India Company ,General,What's the better known name of the card game twenty one,Blackjack,General,Where is the houston space centre,Texas + History & Holidays,Which Was The First Town To Be Liberated By The Allies On D-Day? ,Caen ,General,Where could you find The round window and The oval window,In the human ear,General,What does rabbi literally mean,My Master +Science & Nature,Which Part Of the Body Receives Oxygen Without A Blood Supply ,The Cornea ,General,What poke hand comprises three of a kind and a pair,Full house,Music,"Released In May 1999, What Was Geri Halliwell's First Solo Single",Look At Me +General,What is a group of gulls,Colony,General,Which architect designed the Guggenheim Museum in New York,Frank lloyd wright,General,On what might yoU.S.erve Bearnaise sauce,Grilled meat +General,Poisonous substance especially a pesticide,Biocide,General,Which Oscar-winning film of the 1980s was directed by Hugh Hudson,Chariots of fire,General,What do the 4 of top 10 children's authors have in common,British - Blyton Dahl Potter etc +Music,How Old Was John Lennon When He Was Murdered,40,Music,Get Down On It Was A Smash Disco Hit For Which Us Group,Kool And The Gang, History & Holidays,In What Year Did India Gain Independence From British Colonial Rule ,1947  +General,Who was vice president US when A bomb dropped on Hiroshima,No One - was not one,Art & Literature,Who Wrote The Noddy Stories ,Enid Blyton ,Art & Literature,In 1526 Hans Holbein Became The Officiaal Portrait Painter Of Which English King ,Henry VIII  +General,In Woodstock NY it's illegal to walk what without a leash,A Bear,Food & Drink,What does the brut indicate on a bottle of champagne? ,Very Dry ,General,What is the wbc,World boxing council +Music,Who was best man at John Lennon and Cynthia Powells wedding?,Brian,General,What is the fear of anything new known as,Neophobia, Geography,What is the basic unit of currency for Belgium ?,Franc +General,In Which US City Did Techno Music Originate,Detroit,Science & Nature,"Archaeopteryx Differed From It's Modern Counterparts In That It Had Teeth, Clawed Fingers & A Bony Tail Core. Otherwise It Bore The Characteristics Of Which Group Of Creatures ",Birds ,Entertainment,Broke Batman,Bane +General,"Who said ""Religion___ is the opium of the masses""?",Karl Marx,General,In minerology what does the Mohs scale measure,Hardness,General,Which is the largest of the Seychelles,Mahe +General,Which three word catchphrase is most universally recognised,Bond James Bond,General,"A tall, tapering, pointed roof on a tower, as in the top of a steeple.",Spire,Science & Nature,What Is The Antirrhinum Better Known As? ,The Snapdragon  +Music,Will Smith's Rapper Alter Ego Was Who,The Fresh Prince,Science & Nature,What does hepatitis affect?,Liver,General,If you were a chiropodist which part of the body would you treat,Feet +General,What is the square root of 64,Eight,General,In which country is the chief range of Drakensberg Mountains,South africa,Music,"In The World Of Music Who Am I ""Harry Roger Webb""",Cliff Richard +General,Slang:Person with very short hair or very little,Slaphead,General,Olfactophobia is a fear of ______,Smells,Music,Who Had A Surprise UK Hit In The 90’s With A Cover Of The Classic  Led Zeppelin Song “Stairway To Heaven”,Rolf Harris +General,What is banned by public schools in San Diego,Hypnotism,Music,Which Song Penned By The Gibb Brothers Provided Barbara Streisand With Her Only UK Number One Single In 1980?,Woman In Love,General,How many times did peter deny jesus,Three +General,What sport was obligatory for Kennedy males during Hyannis Port weekends,Touch football,General,What is the Capital of: Estonia,Tallinn,General,"What is the other official language of Sri Lanka, along with Tamil",Sinhala +General,What links Helicon Hutchinson Macmillan and Penguin,Book Publishers,Music,Who Worked In Mime And Appeared In Jubilee And Quadrophenia Before Her 1981 Chart Entry,Toyah,Music,Over Which 2 Years Did The Bay City Rollers Achieve Most Of Their Chart Sucess,1974 & 1975 +General,Plutophobia is the fear of,Wealth,General,What do the words nick and tuck refer to,Cosmetic surgery,General,What candy bar was first introduced by dropping them from airplanes over 40 american cities,Butterfingers +General,Which saxophonist joined David A Stewart in the charts on 'Lily Was Here',Candy dulfer,General,What U.S. city was the capital from 1789 to 1790,New York City,General,Which country has the worlds first greyhound racing track,USA +General,What is the name of a hairstyle in which the head is shaved except for a central strip,Mohican,General,Airman T E Shaw in WW2 was better known as who,T E Laurence of Arabia,General,Six ounces of orange juice contains the minimum daily requirement for which vitamin,Vitamin c +General,In Bristol England an old law says dogs can do what,Watch sex in your bed,Science & Nature, The part of the foot of a horse between the fetlock and the hoof is the __________,Pastern,General,Which TV comedy series featured a regular animated section known as the World Staring Championships?,Big Train +General,This first king of Israel reputedly had 700 wives?,Soloman,General,"Which Actress Won The Title Of ""Miss Orange County Beauty Queen"" In 1976",Michelle Pfeiffer,General,"What was the name of the Swedish Prime Minister, assassinated in Stockholm in 1986 as he was walking home from the cinema with his wife",Olaf palme +Geography,What building built in 1897 contains 327 miles of bookshelves ,The library of congress , History & Holidays,What 'CE' Does Adam Call His Wife On December 24th ,Christmas Eve ,General,"What are Strength, Chariot and Hermit",Tarot Cards +General,"Pony, Shot and Jigger are all units to measure what?",Spirits, Geography,Where is the Admirality Arch?,London,General,What is a group of ponies,String +Geography,"______________, in the eastern West Indies, is one of the world's most densely populated countries.",Barbados,General,Who in 1994 knocked out Michael Moore to become the oldest man ever to win a version of the World Heavyweight Boxing title,George foreman,Science & Nature,What is the term for a tree which sheds its foliage at the end of the growing season?,Deciduous +General,"Which country's national flag consists of a white crescent and star, offset left of centre, on a red field",Turkey, History & Holidays,Which country has the oldest national flag ?,Denmark,Sports & Leisure,In which country is the famous Maracana stadium?,Brazil +Food & Drink,Which drink do you associate with Holy Island in Northumberland? ,Lindisfarne Mead , History & Holidays,Who committed the first daytime robbery?,Frank and Jesse James,General,"Where do the souls of unbaptised babies go after death, according to Catholocism?",Limbo +General,Which Popular Tv Presenter & Broadcaster Sadly Died In 1988 At Leeds Hospital Suffering From Hepatitus,Russell Harty,General,"In terms of tons of cargo handled, which is europe's busiest port",Rotterdam,General,Interpol was founded in 1923 in what city,Vienna +General,What is Kensington Gore,Actors fake blood,General,What is the fear of lawsuits known as,Liticaphobia,General,Army unit usually of 300-1000 men,Battalion +General,"In Greek legend, Zeus wooed Europa, daughter of King Agenor, in the form of which animal",Bull,Science & Nature,What Is The Collective Name For Rhinoceroses? ,A Crash , History & Holidays,East Berlin was the capital of ______?,East Germany +General,Rhabdophobia fear of what,Being Beaten,General,Which army rank is equivalent to the naval rank of Lieutenant-Commander,Major,General,Thomas Young the Physicist and Egyptologist spoke how many language when he was 14 ?,12 +General,La Who played the title role In the film The Madness of King George,Nigel hawthorne,People & Places,Which Tory Author Won substantial Damages From The Daily Star ,Jeffrey Archer ,General,During the 20th century who was the only England bowler to take a hat trick in an Ashes test,Darren gough +General,Who invented the assembly line,Henry ford,General,In which Dickens novel does Little Nell appear,The Old Curiosity Shop,Science & Nature,Which Flightless Bird Is The Emblem Of New Zealand? ,The Kiwi  +General,"Who portrayed clare quilty in the film, ""lolita""",Peter sellers,Science & Nature,The medical name for the voice box is the _________.,Larynx,General,Urchin is an old English name for which British native mammal,Hedgehog +General,What is a sardine,Young herring,General,"What links Sheffield, Edinburgh, Rome",Built on 7 Hills,Entertainment,Younger version of Aquaman,Aqualad + History & Holidays,"Who was known as ""the wizard of Menlo Park"" ?",Thomas Alva Edison,General,An unkindness is a group of what birds,Ravens, Geography,What is the basic unit of currency for Papua New Guinea ?,Kina +General,What was the name of the last film where george burns played god,"Oh god, you",General,Where is the sear's tower,Chicago,General,Dark green fruit with creamy flesh,Avocado pear +General,In 1928 Simon Bolivar was president 3 countries Bolivia and ?,Columbia Peru,General,Wrigley's promoted what new flavor chewing gum in 1915 by mailing 4 sample sticks to each of the 1.5 million names listed in us telephone books,Spearment,General,In The World Of Music How Is Ronald William Wycherley More Commonly Known,Billy Fury +General,In Which Us City Is The Head Quarters Of The Coca Cola Company,Atlanta / Georgia,General,In which country is Penina golf course,Portugal,General,What is the fear of automobiles known as,Motorphobia +General,Which U.S. government branch includes the President and Vice President,Executive,General,Which officer commands a platoon,Lieutenant,General,"Who was elected president of france, in 1981",Francois mitterand +Food & Drink,Laetrile is associated with the pit of which fruit?,Apricot,General,The Acropolis - what does the word literally mean,Highest point (of a city), History & Holidays,Who outlawed gladiator sports in Rome?,Caesar +Sports & Leisure,In BaseBall where do the braves come from ,Atalanta ,General,Bird Music: What was the original name of the group 'Chicago',Chicago Transit,General,What is the flower that stands for: confidence,Lilac polyanthus +General,What is the largest bird of prey in the United Kingdom?,White-Tailed Eagle (Sea Eagle),General,What did Fletch's initials (I. M.) stand for?,Irwin Maurice,Music,Who Singer Performed With Marvin Gaye On The 1966 Hit Song “It Takes Two”,Kim Weston +General,Musca Domestica can cause disease in man - what is it,Common Housefly,Entertainment,"Who is the male lead in the ""Naked Gun"" movies?",Leslie Nielsen,Science & Nature, There is approximately one __________ for every human being in the world.,Chicken +General,"Who said 'what, me worry'",Alfred e neuman,General,Approximately how many times sweeter is saccharin than sucrose,Five hundred,General,FT (London) Dow Jones (USA) what is Japans Share Index called,Nikkei +General,In heraldry what is a vertical line dividing a shield called,Pale,General,If you landed at Cannon airport where are you,Reno,General,What nursery rhyme character sang for his supper,Little tommy tucker +General,Eczema affects which part of the body,Skin,General,What is the fear of smothering known as,Pinigerophobia,General,What is the largest gem-quality diamond discovered,Cullinan diamond +General,What is the Capital of: Uruguay,Montevideo,General,Tiramisu is a coffee desert but what does it literally mean,Pick me up,General,"Which Artist Once Had The Aler Ego ""The Thin White Duke""",David Bowie +General,Who composed The Dream of Gerontius,Edward Elgar,General,What is the term for the study of how the inherited characteristics of a human population can be improved by genetics,Engenics,General,"Who recorded ""my love is like a tire iron"" in 1981",Ted nugent + Geography,Which country hosted the 1982 World Cup of soccer?,Spain,Music,"What Connects David Cassidy, The Monkees, KISS",Own Tv Programme,Science & Nature,This is the largest of the deer family.,Moose +General,Mendelssohn's Wedding March comes from which work,A Midsummer Nights Dream,General,What is the fear of sitting known as,Thaasophobia, Geography,What is the basic unit of currency for Taiwan ?,Dollar +General,"In Animal House, what was Bluto's grade point average?",0,General,Whats the Muslim name of God,Allah,Geography,Of Where Are The Maoris The Indigenous Population ,New Zealand  +General,What were the spice islands formerly known as,Zanzibar,General,Who wrote the novel Ben Hur,Lew Wallace,General,As what was Taiwan formerly known,Formosa +General,This was originally published in the U.S. as _Murder in the Calais Coach,Murder on the orient express,General,Robert Fitzroy captained which famous ship?,The Beagle,General,What is considered the most successful poem of all time,If - Rudyard Kipling +General,What's the connection between Benson & Growing Pains?,"Missy Gold and Tracey Gold,",General,With what kind of pen were allied bomb crews issued,Biro pens,General,Who spoke the first recorded message,Thomas edison +General,What does an anthrophage practice,Cannibalism,General,"In pottery, what is biscuit ware",Pottery fired but not glazed,General,What dutch master painted 64 self portraits,Rembrandt +General,What colour was the hundred billionth crayola crayon,Periwinkle blue,Science & Nature,Which colour is at the top of a rainbow? ,Red ,General,What causes gout,Uric acid +Music,In Which Type Of Car Was Marc Bolan A Passenger When He Was Tragically Killed,A Mini,General,How many litters can have a mouse in a year,Up to ten,Food & Drink,"A great companion to Stilton Cheese, which drink comes in Ruby, Tawny and Vintage varieties? ",Port  + Geography,Into what bay does the Ganges River flow?,Bay Of Bengal,Science & Nature,"The Lightweight Portable Drill Was First Marketed In 1917 By 2 Americans With The First Names Duncan & Alonso, What Were Their Surnames ",Black & Decker , History & Holidays,She won the 1979 Nobel Peace Prize for her work among the poor.,Mother Teresa +General,What disease is carried by the tsetse fly,Sleeping sickness, Geography,This is the longest mountain chain in the world.,Andes,General,What is the fear of surgical operations,Tomophobia +General,Hotel California' by the Eagles was single of the year and 'Rumours' by Fleetwood Mac was the album of the year. Which year was it,1977, History & Holidays,"Who was ""The Mad Monk""?",Rasputin,General,"In 1976 Audiences Were Frightened To Death When ""The Omen"" First Hit The Big Screen But Who Directed It",Richard Donner +General,Which south east Asian city was formerly called Krung Threp,Bangkok,General,What basketball star played a genie in 'kazaam',Shaquille o'neal,General,Kind of skin inflammation,Eczema +General,What did the lady of the lake give king arthur,Excalibur,Geography,What Did Wellington Replace As The Capital Of New Zealand? ,Auckland ,General,What ship was sunk in Auckland harbour in 1987,Rainbow Warrior (Greenpeace) +Geography,The Volta is the largest river in which country,Ghana,General,"Of which bird did Wordsworth write ""Shall I call thee bird, or but a wandering voice""",The cuckoo, Geography,What is the basic unit of currency for Kenya ?,Shilling +General,Who would write a decratal or rescript,The Pope,General,What was the first name of the baby girl who fell down the well?,Jessica, History & Holidays,Who was the lead singer of the Cure? ,Robert Smith  +General,Leslie Hornby became more famous as who,Twiggy,General,Anethum tastes a little like aniseed - what herb is it,Dill,Music,In Which Year Did bobby Brown Have More Weeks In The UK Chart Than Any Other Artists,1989 +Sports & Leisure,In Snooker How Much Is a (Red Black Red Green Red) Worth ,13 ,General,"Which Prime Minister Was Once Editor Of The ""Church Times""",Edward Heath,General,"Which country covers the largest area, Iran or Iraq",Iran +General,Melba sauce is made from what fruit,Raspberries,General,Super Model Jodie Kidd Once Represented England At The Olympics In Which Event,Polo,General,Every year 30000 US people are seriously injured by what,Exercise equipment +Geography,"There is a U.S. state capital that was named after a famous German. Bismarck, North Dakota, was named after ______________________",Otto von bismarck,Sports & Leisure,In Which Sport Is The Lance Todd Memorial Trophy Contested ,Rugby League ,Language,An adjective meaning 'pertaining to the sun.',Solar +General,Comedian that is the disc jockey on the soundtrack to Resevoir Dogs,Steven wright,General,A Cruciverbalist is interested in what,Crossword Puzzles,General,Who is the Greek equivalent of the Roman Jupiter,Zeus - chief God +General,Ten degrees Celsius is the equivalent of how many degrees Fahrenheit,Fifty degrees,General,Rudolf the red nosed reindeer had a girlfriend - name her,Clarissa,General,If you suffered from tantartism what would you be doing,Dancing Mania +Music,Who Was Charles Edward Anderson Berry Better Known As,Chuck Berry,General,What does Magna Carta literally mean,Great Charter,General,Which writer created Detective-Inspector Bucket,Charles dickens +General,"Hook and eye', 'strap', 'tee', 'butt' and 'blind' are all types of what",Hinges,General,In Chaucer's England a mussel was slang for what,Vulva,General,In which European city is the Montjuic stadium and the Parc Gruell,Barcelona +General,What is the name of Mr.Krane's dog on Frasier?,Eddie.,Music,Frank Sinatra Had 2 No.1 In The 60's Name Them,Strangers In The Knight / Something Stupid,Religion & Mythology,Who is the greek equivalent of the roman god Diana ?,Artemis +General,What is added to soap to make it clear,Alcohol,General,Milton lost which sense,Sight,General,Sarah Josepha Hall wrote what,Mary had a little lamb +General,The FIC govern what sport,Canoeing,General,On what street in new rochelle did rob and laura petrie live,Bonnie meadow,General,Who painted the famous picture of Marat assassinated in his bath,David +General,Who is the president of Russia,Vladimir putin,General,"In which Shakespeare play would you find the lines: 'This royal throne of kings, this sceptred isle, this earth of 'majesty, this other Eden'",King richard ii,Music,"Members Of The Band ""Split Enz"" Went On To Form Which Famous Band",Crowded House +Music,What Was The Name Of Elvis Presleys Home,Graceland,General,"Who did the voices of bugs bunny, sylvester and tweety pie",Mel blanc,General,"Disney World in Florida was opened to the public in 1971. The amusement park was the largest in the world, set within ________ acres. It required a $400_million investment, and did not do well during the first year it was opened. ",28000 +General,Hack - Hog Line - House are terms in what sport,Curling, Geography,Banjul is the capital of ______?,Gambia,General,"Common name for the family comprising a peculiar group of spiny, fleshy plants native to America",Cactus + Geography,Which city is home to the 4th largest pyramid in the world?,Las Vegas,Geography,What is the capital of Croatia,Zagreb,Music,What nationality are pop group Roxette?,Swedish +General,"In the tv series 'the brady bunch', what was cindy's toy doll's name",Kitty,General,The capital of the African country Liberia is named after which American President,James Monroe,General,What was the heaviest known dinosaur,Brachiosaurus +Geography,In what country is thunder bay ,Canada ,General,In which county is the town of Market Harborough,Leicestershire,General,Cyndi Lauper sing this song 1994,Hey now(girls just want to have fun) +General,What is the glass capital of the world,Toledo,General,What are the 3 big colleges of the Ivy League? (name them alphabetically),"Harvard, Princeton, Yale",General,How many squares are on a Shogi (Japanese chess) board?,81 +Entertainment,Who wrote the opera 'The Giant'?,Sergei Prokofiev,Science & Nature," What do bats' wings, elephants' ears, flamingos' legs, rabbits' ears, goats' horns, and human skin all have in common? They radiate heat to providing __________",Cooling,General,The average chocolate bar has eight what in it,Insects legs +General,"What major British disaster occurred in Beauvais, France, in 1930",Crash of the r101 airship,General,How high could the Klopek's furnace go in The 'burbs?,5000 degrees,General,"This means ""the art of twisting together strands of material to form objects"". (_________)",Basketry +Music,"Whose First Single Was ""Release Me""",Engelbert Humperdinck,Toys & Games,"In Monopoly, What is the cost to buy Oriental Avenue",100,General,This instrument has black and white keys?,Piano +Science & Nature,What Is A Portuguese Man-O-War? ,Jelly Fish ,General,According to Christian religion what happened at Epiphany,Wise men visited,Music,Martin Kemp From Spandau Ballet Married A Backing Singer From Which Other Famous 80's Band,Wham (Shirlie) +General,If you landed at Arlanda airport where would you be,Stockholm Sweden,General,"This city can be abbreivated to 3% of its size ""El Pueblo de Nuestra Senora la Reina de los Angeles de Porciuncula""?",LA,General,Who was the first U.S. postmaster general,Benjamin franklin +Music,"With Which Group Did Marc Almond Record The 1985 Hit ""I Feel Love""?",Bronski Beat,General,Which pop artist died in New York in 1987?,Andy Warhol,Music,"Who Had A Hit In 1993 With The Song ""Mr Vain""",Culture Beat +General,Carlos This planet has an atmosphere that is 98 percent Helium,Mercury,General,Sanford what presidential candidate from indiana lost to roosevelt in 1940,Wendell,General,In a state of readiness is the literal meaning of which job title,Waiter +Entertainment,Who wrote the oprea 'La Traviata'?,Guiseppe Verdi,General,What was the first toy advertised on TV in USA,Mr Potato Head,Music,"Who Starred In & Performed The Title Track From The Film ""Xanadu""",Olivia Newton John +General,The term Sesquincentennial represents how many years ?,150,General,What is the flower that stands for: difficulties that i surmount,Mistletoe,General,What is a Knout,Russian flogging whip + History & Holidays,Which Film First Featured A Character Called Pinhead ,Hellraiser ,General,In Which Former Soviet Republic Do 100 Luma Make A Dram?,Armenia,General,Name either of the churches that merged in 1972 to form the United Reformed Church,Presbyterian congregational +General,"In 1987 Bernardo Bertolucci became the first western director to be allowed to film in Beijing, what film was he making",The last emperor,Music,Which Country Did Celine Dion Represent In 1988 Eurovision Song Contest,Switzerland,Entertainment,On what LP Cover can we read the words 'Welcome Rolling Stones'?,Sergeant Pepper´s Lonely Hearts Club Band +General,"What are Grenadier, Idared and Ellison's Orange types of",Apples, History & Holidays,"Who was assassinated on December 8, 1980 in New York City?",John Lennon,General,What's the general name for germ fighting drugs such as penicillin & streptomycin,Antibiotics +General,What links Augsburger - La Stampa - El Pais and Duma,Newspapers Germany Italy Spain Bulgaria,General,The ______ tea party,Boston,General,What is chewing gum made from?,Chickle +General,"In the TV Western series ""Bonanza"", what was the name of the ranch",Ponderosa, Geography,Singapore is the capital of ______?,Singapore,General,What are the two christian names of HE Bates,Herbert ernest + Language,"""Entre nous"" means __________.",Between ourselves,Science & Nature,Which of Neptune's moons is the biggest?,Triton,Music,"Who Had Hits In The 1980's With Singles Like ""Iron Fist"", ""Killed By Death"", ""Beer Drinkers & Hell Raisers"", ""Deaf Forever""",Motorhead +General,Freyr was the Norse god of what,Fertility,General,With which group does Damon Albarn sing,Blur,General,"In what television series did miss usa, 1974 star",Wonder woman +General,The only Italian masterpiece you can drive to work,Maserati, History & Holidays,For which European country did Mozambique declare independence in 1975? ,Portugal ,General,Who was not offered the lead role in a fistful of dollars due to his high fee,Henry fonda +General,A Group of Lion is called a,Pride,General,"In 1964 Douglas MacArthur U.S. general (Pacific theater-WW II), dies at",84,General,In what sport is a stimpmeter used,Golf - measure greens pace +Science & Nature,Which element has the chemical symbol PT ,Platinum , History & Holidays,On the face of which mountain does the climax of North by Northwest take place? ,Mount Rushmore ,General, The study of human behaviour is ________.,Psychology + Geography,What is the basic unit of currency for Azerbaijan ?,Manat,Sports & Leisure,"Canadian Ben Johnson Famously Lost His Olympic Title,World Record & Gold Medal By Failing A Drugs Test At The Summer Olympics But Where & When Did This Take Place ' Where (Country) & When (Year) '' ","1988, Seoul / Korea ", History & Holidays,What did Anne Boleyn have three of?,Breasts +General,"Name the stretch of water which lies between New Brunswick, Maine and Nova Scotia",Bay of fundy,General,What unrecognised phenomenon was discovered when Comet aircraft started to fall out of the sky,Metal fatigue,General,Who wrote the opera 'samson and delilah',Camille saint-saens +General,Which position is traditionally played in Rugby Union by the player wearing a number 11 shirt?,Left Wing,General,What is the world's fastest passenger aircraft?,Concorde,General,What's the average annual temperature worldwide (Fahrenheit),58 +General,Goose Flats changed its name to what US city,Tombstone Arizona,General,Which sign used in punctuation denotes interrogation,Question mark,Science & Nature, A __________ will lay bigger and stronger eggs if you change the lighting in such a way as to make them think a day is 28 hours long.,Chicken +Geography,In Which Country Is Europe's Highest Mountain Mt Elbrus ,Russia ,General,Who built the lambarene missionary station,Albert schweitzer,General,"A fun, new winter sport",Snowboarding +General,What geological period followed the Jurassic,Cretaceous,General,Who was Sitting Bull's right hand man,Crazy horse,General,Where would you find vox humana and vox angelica together,On an organ + History & Holidays,Who directed the movie 'An American in Paris? ,Vincente Minnelli ,General,Phronemophobia is the fear of,Thinking,Science & Nature,Dense sea_water swamps along coasts of hot countries are called ________.,Mangrove +Music,"Who Begged For ""Just One More Night"" In 1978",Yellow Dog,General,Who was the first athlete to clear eight feet in the high jump,Javier sotomayor,General,What Asian city boasts the world's biggest bowling alley,Tokyo +General,In Star Trek Deep Space Nine which character had a simbiant,Jadzea Dax,General,"Balein, Boops, Fin, Grampus and Pothead are types of what",Whale,Geography,Madrid and lisbon are both located near this river. ,Tagus  +Art & Literature,Dr. Seuss wrote this book: The Cat in the______.,Hat,General,Who had a number one hit in 1966 with Keep on Running,Spencer davis group,General,Membrenaphone musical instruments commonly called what,Drums +General,The brutal treatment of Billy Hayes was the inspiration what film,Midnight Express,General,Unusual words - What's the only word 4 double letters in a row,Bookkeeppers,General,Who wrote one of the earliest English Operas entitled 'Dido and Aeneas'?,Henry Purcell +General,What instrument is used for measuring the distance between two points on a curved surface,Caliper,Music,"The Band King Sang ""Love & Pride"" Or ""Love & Rockets""",Love & Pride,General,The name Calvin has what unfortunate Latin meaning,Bald +General,What is the worlds biggest profession,Teaching,Science & Nature," At birth, the white whale is __________",Black,General,What is the part of the sole between the heel and the ball of the foot,Shank +General,"What is the nickname for Indianapolis, Indiana",Railroad city,General,How many pole positions did ayrton senna score,65,General,The grand prix d'Endurance is run on which circuit,Le Mans 24 hour race +General,Meridians converge at the ______,Poles,General,6300 was the biggest cast in a commercial for what company,British Airways,General,Who has the biggest percentage of female heads of household,Botswana + Geography,What is the capital of Honduras ?,Tegucigalpa,General,Hagen the _____ _______ supplies the liver with oxygen,Hepatic artery,General,"What was ""the pink panther"" in ""the pink panther""",Diamond + Geography,In which continent would you find the Mississippi river ?,North America,General,Who would wear a Hachimaki - or headband,Kamikaze pilots,General,What was the gift from the gods in the movie The Gods Must Be Crazy?,A coke bottle +Science & Nature,Which substance has the chemical symbol 03? ,Ozone , Geography,What is the capital of Belize ?,Belmopan,Music,According To The Beatles What was Brian's brother's name?,Clive +General,"Impurities, particularly of which compound, according to modern chemists, gives amethyst its violet or blue colouration",Iron oxide,General,Who wrote The Poseidon Adventure,Paul Gallico,General,What Time Is The Clock On The Front Page Of The Times Newspaper Showing,4.30pm +General,The rover the last is red the rest are white in what sport,Croquet hoops,Music,"Which Band Consisted Of The Following Members “Wyclef Jean, Lauryn Hill, Pras MKichel",The Fugees,General,"Which Musical Act Finally Knocked The Brian Adams Song ""Everything I Do I Do For You"" Off The UK No.1 Spot In 1991",U2 / The Fly +General,Who was British Prime Minister at the outbreak of WWI,Herbert asquith,General,In which cult novel do gang members known as 'droogs' appear,A Clockwork Orange,Music,What Did Joy Division Change Their Name To,New Order +General,And what is officially the richest,District of Columbia,General,What is the name of the least dense element,Hydrogen,Music,Who had an album called Incense and Peppermints?,Strawberry Alarm Clock +General,In which Woody Allen film was Allen's character visited by the ghost of Humphrey Bogart,Play it again sam,General,In what sport is the Palma Match contested,Shooting,Music,"According To Those In The Medical Profession When A Man Ejaculates He Fills On Average A Table Spoon Of Deposit, What You May Not Know Is That 2 Pop Groups Actually Got Their Names Based On This Scientific Fact (PFE)","10cc," +General,Police Academy got its theme song from which other film,Patton,General,What was the first company formed to manufacture motor cars,Daimler,General,When was the first major road race from Paris to Bordeaux,1895 +Art & Literature,Which Sculptor Produced A Lobster Telephone ,Salvador Dali ,General,What is the common name for a coin operated record player,Juke box,Geography,What city did the mormons establish as their headquarters in 1847 ,Salt lake city  + Geography,In which state is Mount Vernon?,Virginia,Music,Maya Angelou Made Herself Unavailable For Rehearsals After Working On Which Miusical,King - The Musical (1990),General,What does the DIN number mean on photographic film,Speed of film +General,What was Japan's most famous WWll aeroplane,Zero,General,"Which Football Club Reached The Fa Cup Final In 1885, 1984, & 1986",Everton,General,Compounds of which molecule outnumber tenfold all other compounds of molecules together,Carbon +General,Which Irish playwrights middle names are Fingall O'Flahertie Wills,Oscar wilde,Food & Drink,If a dish is served `au gratin' what does it have on it? ,Cheese , History & Holidays,Dia de los Muertos (The Day of the Dead) is celebrated in which Latin American country ,Mexico  +General,Manya Sklodowska became famous under what name,Madam Curie,General,Which nationality drinks the most coffee per person per day,Sweden,Music,What Kind Of Of Arrow Did ABC Want You Th Shoot Through Their heart In The 1980's,Poison +General,TABSO is the national airline of which country,Bulgaria,General,"Whose last word were ""I have not told half of what I saw""",Marco Polo,Music,On Of The Most Prolific Composers Of All Time His Surname Means Stream In His Native German Language Who Is He,Johann Sebastian Bach +Geography,"Two thousand years ago, the ancient Roman city of __________ was a thriving commercial port of 20,000 people.",Pompeii,General,What Shakespeare play is set in Massina and has Claudio in it,Much ado about Nothing,Sports & Leisure,Which team won the UEFA cup this year?,Liverpool +General,Inflammation of the gums,Gingivitis,General,By what name is the 3rd battle of Ypres known,Passchendaele,General,What was Reverend Jims real last name on TAXI?,Caldwell +General,Who did a double album after leaving the beatles as an effort to raise money for the famine in bangladesh,George harrison,General,In Britain what is The Andrew,The Royal Navy,General,The assassination of what country's Archduke led to World War I?,Austria +Music,Which Artist Married Carrie Fisher In August 1983,Paul Simon, Geography,Which country has the longest land border?,China,Geography,Which is the Earth's second largest continent,Africa +General,What US president was born in Corsica,William Harding,General,What does 'n.b.a' mean,National basketball association,Technology & Video Games,"In 'Pac-Man' for the Atari 2600, how many points were each pellet worth? ",1 +General,What language speakers were shot Russia and Germany 1930s,Esperanto,Religion & Mythology,"In Greek mythology, who drove the sun across the sky in his chariot?",Helios,General,The worlds first chocolate candy was produced in what year,1828 +Science & Nature,Which vital organ does the adjective renal refer to? ,Kidney ,General,The 2 months added when the Roman calendar was expanded from 10 to 12 months,January & february,General,Bowie what is pro football quarterback sonny jurgenson's christian name,Christian +General,Which of the 5 senses is less sharp after you eat too much,Hearing,General,Where would you Wedel,Ski slope,General,What is a group of this animal called: Starling,Murmuration +General,"Which author wrote the ""Redwall"" series of novels",Brian jacques,Sports & Leisure,Who was the 1990 Wimbledon women's singles runner-up?,Zina Garrison,General,"In 'la traviata', who sings 'sempre libera'",Violetta +General,There are 150 what in The Bible,Psalms,General,In 1931 Al Capone was sentenced to how many years in prison,Eleven, History & Holidays,Which Hitchcock movie With One Word In It's Title was released in 1958? ,Vertigo  + History & Holidays,Eras are divided into units called ________.,Periods,General,What insect gives its name and body to a food colouring,Cochineal,Science & Nature, Goats generally need their __________ trimmed once a month.,Hoofs +General,American folklore Abner Doubleday invented what at West Point,Baseball,Sports & Leisure,Who won the Spanish grand prix at weekend? ,Kimi Raikkonen ,General,The Somers Islands has what more familiar name,Bermuda + Language,What is the Spanish word for 'festival'?,Fiesta,General,"Who is paul mccartney singing about in ""here today""",John lennon,General,"Ambrosia, darkling and death watch are all kinds of what",Beetles + Language,"The name Australia is derived from the Latin word ""australis"" which means _______.",Southern, Geography,What is the basic unit of currency for Egypt ?,Pound,Music,Which Was The First Sex Pistols Single To Feature A Song Writing Credit For Sid Vicious,Holidays In The Sun +Music,"What Group Consisted Of Jo,Tina,Hannah,Rachel,Paul,Bradley,Jon",S Club 7,General,Which actress left 'The Queen Vic' in 1999 to begin a solo singing career,Martine mccutcheon,General,Price one kilo went from $63 to $260 in 1976 when sale illegal,Ivory +General,What is a SR N4,Hovercraft, Geography,Which city is known as the Windy City?,Chicago,General,Bissau is the capital of ______,Guinea-bissau +General,Britain's Frankie Wainman was world champion 1979 what sport,Stock Car Racing,General,In Old England what would you do with your titties,Its an old word for Sisters,General,What is the most common street name in the USA,Park Street +General,What US state named in 1664 in honour of Sir George Cateret,New Jersey Cateret defended Jersey,Art & Literature,"Which Famous Book Begins With The Line 'When shall we three meet again, in thunder, lightening, or in rain' ",Macbeth ,General,"In which organ is your ""hypothalmus"" located",Brain +General,Who Sang The Theme Tune To The James Bond Movie Octopussy?,Rita Coolige,Science & Nature,Every human has one of these on their tummies.,Navel,Music,"Which Female Singer Asked The Question ""What's Love Got To Do With It""",Tina Turner +Geography,What name is shared by mountain ranges in Scotland & Australia? ,Grampians ,Music,What was the thirteenth album released by Chicago?,Chicago XIII,General,This song singed by Boney M,Brown girl in the ring +Geography,Canada is the second_largest country in the world after __________. Nearly 90 percent of the Canada's population is concentrated within 161 km of the United States/Canada border.,Russia,General,The 'frigorifique' was the names of the world's first what,Refrigerator ship, Geography,What is the basic unit of currency for Cape Verde ?,Escudo +General,What country do most stolen US cars end up in?,Mexico,Music,What Is EP Short,Extended Play,People & Places,Where Was Mike Tyson Born ,Brooklyn  +Entertainment,Band: Elvis Costello and the ___________ ?,Attractions,General,What country is home to Sabena airlines,Belgium,General,What Nationality Was The Explorer Christopher Columbus,Italian +General,"As Nick Park drove to the 1996 Oscar ceremony on a Wallace and Gromit-style red motorcycle and sidecar, why was he cautioned by the police",Not wearing a crash helmet,General,Which Famous Athlete Became Master Of Pembroke College Oxford In 1985,Roger Bannister,Sports & Leisure,How Many Players Are There In An Ice Hockey Team ,11 (Check This) n  +General,In Natchez Missouri it is illegal to provide beer to what,An Elephant,Food & Drink, What is the main ingredient of a traditional fondue?,Cheese,General,In 1996 which Celine Dion album Grammy album of year,Falling into You +General,Which country produces most of the worlds gold,South africa,General,Which Group Released The Album Entitled “Chemistry” In Late 2005 ?,Girls Aloud,General,"Who co-wrote ""The Communist Manifesto""",Marx engels marx and engels +General,"All Hebrew orignating names that end with the letters ""el"" have something to do with what",God,Science & Nature,As what is minus forty celcius the same?,Minus forty fahrenheit,Music,Which Soul Singer Had A string Of Hits With The Commodores Before Launching His Solo Career,Lionel Richie +General,What concentration camp is the name of Anne Frank's burial site,Bergen belsen, History & Holidays,What Was The Name Given To The Coding Machine Used By The Germans In World War II? ,Enigma ,General,What was the only team to win two world series in the 1980s?,Los angeles dodgers +General,"Which Company Now Owned By Jaqueline Gold Was The Brain Child Of ""Cabourn Waterfield""",Ann Somers,General,What large sea is between Europe & Africa,Mediterranean,General,What did dan aykroyd and john belushi quit 'saturday night live' to become,Blues brothers +General,Peter Benchley Published His First Novel In 1969 It Went On To Become A Hugely Popular Film What Was It Called?,Jaws,General,Who became U.S. president following the assassination of William McKinley in 1901,Theodore roosevelt,General,The Sureto are the secret service of which country,France +General,Kelsey grammar sings and plays the theme song for which tv show,Frasier,General,What is the sum of 9685z + 235z - 1800z + 2z,8122z,General,What is rock star Sting's real name?,Gordon Summer +People & Places,Which Famous Artist Painted The Most Paintings ,Piccaso ,General,What Classic Toy Was Invented By Richard & Betty James In 1945?,Slinky,General,What was the name of jim henson's muppet hound on the jimmy dean show,Rowlf +General,For which film did Sophia Loren become the first to win a Best Actor/Best Actress Oscar in a foreign language film,Two women,Geography,"The highest mountain on Earth, _____________, reaches only about halfway through the lowest layer of the troposphere.",Mount Everest,General,What's the world's most common compound,Water +General,Busey what disney movie stars merlin the magician and wart the boy king,Sword in,General,-isms: This 20th century pihlosophical movement with thinkers and writers such as Sartre and Camus is responsible for the Theatre of the Absurd,Existentialism,Music,"The Spice Girls Had 3 Successive UK No.1’s In 1996, 1997, 1998 Name The 3 Songs (PFE)","2 Become 1, Too Much, Goodbye" + History & Holidays,What did Temujin change his name to ?,Genghis Khan, Geography,What is the basic unit of currency for Norway ?,Krone,General,What's the common term for precipitation infused by sulfuric & nitric acids,Acid rain +General,Which One Word Phrase Literally Means “ The Place Of All Demons ”?,Pandemonium,General,Which English King met Francis I of France on the 'Field of the Cloth of Gold',Henry viii,Music,In What Year Did Robbie Williams Leave Take That,1995 +Music,"1971's ""Move On Up"" Was The Biggest UK Hit For Which Soul Superstar",Curtis Mayfield,General,"Studios who recorded ""true love ways"" in 1965",Peter and gordon peter and gordon,General,What is the only river that flows both north and south of equator,The Congo +General,What liqueur means cupid in Italian and love in Latin,Amaretto,General,"What is the throwing event making up part of the ancient greek pentathlon, in which a circular object had to be thrown",Discus,Music,Who Was Born Barry Allen Pinkus In June 1946,Barry Manilow +General,How many children did president william henry harrison have,Ten,Music,"Which Country Music Star Sang As Guest Vocalist On The KLF Track ""Justfied And Ancient""",Tammy Wynette,General,The French newspaper La Monde translates as what,The World +General,What is the national drink of Poland,Mead,General,Who would perform the Maha Mantra,Hari Krishnas,General,Where was marco polo born,Island of korcula +Music,Name The Russian Composer Who Wrote Peter & The Wolf,Sergei Prokofiev,General,"Common name for many rodents belonging to the same family as the woodchuck (marmot), chipmunk, and prairie dog",Squirrel,General,What is the southern most city in the U.S.,"Na'alehu, hawaii" +General,Patroiophobia is the fear of,Heredity,General,What term describes a male swan?,Cob,General,"Who is the director father of Madonna's son, Rocco",Guy ritchie +General,It is against the law to do what to the Mayor of Paris,Stare at Him,General,After Athens which is the largest Greek speaking city in the world,Melbourne,General,Joyce Frankenberg born 1951 changed her name to what,Jane Seymour + Geography,Which country has a flag of a red circle on a white background ?,Japan,General,What is considered to be the worlds fastest team game,Ice Hockey,General,What are Portland Vases made from,Glass +Food & Drink,"This spiny fruit with a pungent odor and rich yellow flesh is considered ""The King of Fruits"" by many southeast asians.",Durian,General,A light canvas shoe with a plaited sole,Espadrille,Entertainment,"Who starred with John Travolta in the movie ""Broken Arrow""?",Christian Slater +General,The tune Rhapsody in Blue was first performed in which year,1924,General,Who did Spain fight in the 1808-1814 Peninsular War,Portugal,General,"What can be dipole, loop or helical",Aerials +General,Which South American country does not border the Pacific,Belize,Religion & Mythology,What was the name of the mythical hero-king who slew Grendal?,Beowulf,General,In the film 'the jerk' whose character was 'born a poor black child',Steve +General,What is the atomic mass of molybdenum,95.94,General,How did Karmuala Searlel - an early Tarzan die,Mauled by Elephant,General,"Known as 'the Butcher of Lyons', name the World War Two Gestapo chief who was captured in South America and tried in Lyons in 1987",Klaus barbie +Geography,What is the capital of Western Samoa,Apia,General,"During the u.s civil war, how many blacks served in the union army",Two,General,Name the first person to hit a golf ball over 400 yards with 6 iron,Alan Shepherd on the moon +Entertainment,"What country singer/songwriter (and sometimes actor) is known as ""the country outlaw""?",Willie Nelson,General,This imaginary line approximately follows the 180 degree meridian through the pacific ocean,International date line,General,Who did larry hagman portray in the tv series 'dallas',J.r ewing +Science & Nature,For How Long Does A Female Turkey Incubate Her Eggs ,Aprox 28 Days ,General,What musical instrument does John McEnroe play,Guitar,General,What was invented in the Humpty Dumpty store Oklahoma,Supermarket Trolley +General,A Maryland t-shirt slogan that parodied 'virginia is for lovers' read what,Maryland is for crabs,General,"What type of dog can be Manchester, Skye or Greenland",Terrier,General,What is a group of this animal called: Kitten,Kindle litter +General,What do the aperture & shutter let into a camera,Light,General,Beat It' by Michael Jackson was the single of the year What year was it,1983,General,Who was the only President of the Confederate States of America,Jefferson davies +General,What is the capital city of the Kingdom of Tonga,Nuku'alofa,General,Angel falls Venezuela Highest but where second Highest,Yosemite USA,General,Who does the Mona Lisa depict ?,Madonna Lisa Gherardini +General,With which hand do soldiers salute?,Right,General,Small beetle destructive to the potato,Colorado,General,A Study in Scarlet was the first novel to feature which literary character,Sherlock holmes +General,Which Italian writer wrote The Periodic Table,Primo levi,General,Which area of water lies between China and Korea,Yellow sea, History & Holidays,A lot of men end up in the hospital casualty department at Christmas time because they've decorated their nether regions with what? ,Sprigs Of Holly  +General,In Only Fools and Horses what is Rodney's middle name,Charlton,General,"In the nursery rhyme, what medication was applied to Jack's head after his tumble down the hill",Vinegar and brown paper,General,By what process is rock worn down by the weather,Erosion + Geography,What is the basic unit of currency for Tanzania ?,Shilling, History & Holidays,"""Which of the following places is NOT a real U.S. city or town? """"Noel,Missouri"""" - """"Santa Claus,Georgia"""", """"St Nicholas Florida"""", """"Snowflake,Texas"""" "" ","Snowflake, Texas ",General,Where is the tomb of the Venerable Bede,Durham +General,"Birmingham is flanked by the M6 on the north and M5 on the west, which motorway flanks south & east",M42,Religion & Mythology,"Which city is sacred to Jews, Christians, and Muslims",Jerusalem,General,What New Zealand native was the first man to climb Mt. Everest,Sir Edmund +Music,Who Had A No.1 In 1998 With C'est La Vie,Bewitched,Entertainment,"He directed ""The Godfather"".",Francis Ford Coppola,General,"Of what are Syrah, Merlot and Pinot Noir favourites",Grapes +Music,"Posdnous, Trugoy the Dove & PA Pacemaster Mase Had Their First Hit In 1989 How Are They Collectively Known",De La Soul, Geography,Ulan Bator is the capital of ______?,Mongolia,General,What night club did ricky work at on 'i love lucy',The tropicana +General,Maraschino' is a liqueur flavoured with what,Cherries,Music,"Who Was Famous For Singing About ""The Biggest Aspidastra In The World""",Dame Gracie Fields,General,"What are hunting, dress, old and Price Charles Edward types of",Tartans +General,What First Appeared In Britain On The 12th January 1948,A Supermarket,General,How To Save A Life Was The Debut Album Of Which Band,The Fray, Geography,What's the former name of Istanbul?,Constantinople +General,What Is The Only Creature Male Creature To Carry & Hatch Eggs,A Seahorse,General,What is the largest wholly Indonesian island,Sumatra,General,Rocket USA is going to produce a wind up doll what figure 2002,Homer Simpson +General,Who has won the soccer world cup as a player and a coach,Franz Beckenbauer,Science & Nature,What is the medical term for cancer of the blood?,Leukemia,General,"Who said - ""One more drink and Ill be under the host""",Dorethy Parker +General,What is the Capital of: Swaziland,Mbabane,General,Abbot - Costello routine who's on first name the pitcher,Tomorrow,People & Places,By what name is Maurice Micklewhite better known as?,Michael Caine +Science & Nature,Sodium Hydroxide is more commonly known as _________.,Lye,General,What is Mongolia often called to distinguish it from an autonomous region in China,Outer mongolia,General,What makes brown bread healthier than white bread,Wholemeal +General,Indiana jones: how many shankara stones did indy deliver,Only one one,Music,Who was the recording engineer on Sgt. Pepper's?,Geoff Emerick,Science & Nature," The now_extinct ancestor of the horse, __________, had a short neck, a pug muzzle, and stood no higher than a medium_sized dog.",Eohippus + Geography,What is the capital of Guam?,Agana,Entertainment,Who is Steveland Morris better known as?,Stevie Wonder,General,Which Famous Cartoon Character Was Created by Mary Tourtel,Rupert The Bear +Science & Nature,How Is The Binary Numer '010' Expressed As A Decimal ,2 ,General,Collective nouns - a Barren of what,Mules,Geography,"_______________ use wooden ""eyeglasses"" with narrow slits for eyepieces to protect their eyes from glare reflected by ice and snow.",Eskimos +General,What animal would you find in a form,Hare,Music,Name The Jackson Five's Fourth consecutive Hit Reaching No.4 In The UK Charts In 1970,I'll Be There,General,Who is nick and nora charles' dog,Asta +General,LOT is the national airline of what country,Poland,General,Which English bowler achieved his test career best when he took 7 wickets for 46 runs against South Africa in December 1999,Andrew caddick,Science & Nature, There once were more sea lions on Earth than __________,People +General,"25th anniversary is silver, what's the 6th",Iron,General,He released the parody 'oh you ate one too' in 1988 which included the song 'cabo wabo',Van halen,General,What is the flower that stands for: adoration,Dwarf sunflower +Sports & Leisure,Name The Youngest Ever World Heavyweight Boxing Champion ,Mike Tyson ,General,Disease caused by deficiency of vitamin d,Rickets, History & Holidays,"Germany's allies in WW II were Japan, Italy, Hungary, Bulgaria, Finland, Libya, and _________.",Romania +General,In the human body where is your occiput,Back of head,General,What was computer pioneer niklaus wirth's nickname at stanford,Bucky,Music,"Who directed the Beatles film ""Let It Be""?",Michael Lindsay-Hogg +General,The Great Gazoo was an alien in which cartoon series,The Flintstones,General,Picardy is in the north east of which country,France,Sports & Leisure,What Surname Has Been Shared By Three Formula One Champions Of The 20 th Century? ,"Hill (Damon, Graham & Phil) " +General,In the body what may be endocrine or exocrine,Glands,General,Ailurophobia is the fear of what,Cats,Food & Drink,Which type of rice should be used to make the italian dish risotto? ,Arborio (or vialone)  +General,What popular brand named of sugar coated breakfast cereal contained so much iron when introduced in 1977 it could be picked up with a magnet,Kellogg's frosted rice cereal,General,Who played lestat in 'interview with the vampire',Tom cruise,General,"In Imperial measurement, how many pounds are there in one hundredweight",112 +General,What is the more common name for grape sugar,Glucose, History & Holidays,What was the name of the B-29 used at Hiroshima to drop the bomb?,Enola Gay, History & Holidays,How old was Stanley Matthews when he retired in 1965? ,50  + History & Holidays,Which actor from The Harry Potter Films Also starred in The Movie Dracula ,Gary Oldman ,General,Mount Dashan is in which African country,Ethiopia,Science & Nature, A __________ can open its mouth wide enough to accommodate a 4_foot_tall child.,Hippopotamus +General,What unusual ingredient is often included in a Gibson coctail?,Pickled Onion,General,What was the name of Minnie Caldwell's cat in Coronation Street,Bobby,General,"Blind, Comb, Fine Line and Harrow are types of what",Stamp Perforation +General,"Mineral, a cryptocrystalline variety of quartz of various shades of white, gray, yellow, brown, green, & blue",Chalcedony,Music,"Who Is Tommy Tutone Trying To Call When He Dials ""867 5309""",Jenny,General,What well known bard (poems & songs) was born on 25th January 1759?,Robert Burns +General,What musical was named after a u.s city,Oklahoma,General,In which country would you find Karachi,Pakistan,Food & Drink,Adding this to a mere martini makes it a Gibson.,Onion +Music,Who Tore Up A Picture Of The Pope Live On US Television In 1992,Sinead O Connor,General,What is Kanga's son's name in the Winnie The Pooh stories,Baby roo,General,What 1982 horror film starred JoBeth Williams as a woman whose youngest child is carried off into a TV set,Poltergeist +General,"Which man has made the highest individual innings at Lord's, an innings of 333",Graham gooch,General,What does the Latin phrase Deo Volente mean,God willing,General,Ford What U.S. state includes the telephone area code 507,Minnesota +Music,"Whose Albums Include ""Copperhead Road, Guitar Town & A Train A Comin""?",Steve Earl,General,What Is The Michelin Man's Nickname,Bibendum,Sports & Leisure,Which television comedian once rowed in the Boat Race? ,Hugh Laurie  +General,A method of construction in which vertical beams are used to support a horizontal beam.,Post and lintel,General,Hansen's disease has another name what is it,Leprosy,General,What's ulster,Northern ireland +General,Which Disney film features the ballad 'Can You Feel The Love Tonight,The lion king,General,Which partnership was responsible for the musical Paint Your Wagon,Lerner & loewe,Food & Drink,What was discovered by the McDougall Brothers in 1864 after an experiment with phosphatic yeast? ,Self-Raising Flour  +General,Psychologists say men who prefer small breasts what mentally,Depressed, Geography,In what country is Taipei?,Taiwan,General,"What Very Famous Link To Christmas Does Irving Berlin, Have?",He Wrote The Song White Christmas +General,"Name the French salad containing tuna, anchovies, french beans and hard boiled eggs",Salade nicoise,Science & Nature,"How many days will it be before a clock, losing 30 minutes a day shows the right time again ",24,General,What happens when reverse peristalsis takes place,You puke +General,In Cheyenne Wyoming what is illegal on Wednesday,Taking a Shower,Music,Which opera overture is used as a background to Post-Grand Prix champagne celebrations?,Bizet's Carmen,Science & Nature,What Is Moh's Scale A Measure Of ,Hardness  +General,In what business are 'angle irons' and 'rolex',Dentistry,General,"Cat stevens sings 'i can't keep it in, i gotta ______'",Let it out,Mathematics,What is the square root of -1 ?,I + History & Holidays,What was the only state george mcgovern carried in the 1972 presidential election ,Massachusetts ,General,U.S. captials Pennsylvania,Harrisburg,General,Who owns: kool aid,Philip morris +General,Mace is the outer covering of which common spice,Nutmeg,General,Jayne Austin is famous but who reigned Britain when she wrote,George III,General,In ancient Egypt men and women did what opposite to today,Peeing - men sat women stood +General,What actor played George Cooper in My Favorite Husband,Barry nelson,General,Whats the surname of the mediaeval author of Canterbury Tales,Chaucer,Sports & Leisure,"Which contender has won most gold's in any one Olympics? Was it Mark Spitz(Swimming), Nadia Comaneci(Gymnastics) or Raymond Ewry (Track & Field) ",Mark Spitz (Swimming)  +General,In what industry did John Davidson Rockefeller get rich,Oil,General,A racing sledge steered and braked mechanically,Bobsleigh,Geography,Which three countries border Luxembourg? ,"France, Belgium and Germany " +General,"What actress declared ""I'll get naked at the drop of a hat""",Sharon Stone,General,St Fiacre is the Patron Saint of what,Piles,General,What French relish sounds like a machine gun firing,Ratatouille +General,What was the name of the beatles' corporation before it was renamed 'apple',Beatles and company,General,Who played by Luther Adler Roy Goldman Peter Sellers etc,Adolf Hitler,Science & Nature,How Many Legs Per segment Does A Millipede Have? ,4 Legs  +General,What is the non obvious link Superman 1 and the Godfather,Mario Puzo write both stories,Art & Literature,Who Wrote Mein Kampf (My Struggle) ,Adolf Hitler ,General,Oneirogmophobia is the fear of,Wet dreams +Food & Drink,What Would You Be Drinking If You Were Given Earl Grey ,Tea ,General,Which individual had the highest income of anyone in the USA in the 1920s,Al capone,Music,How Many Red Balloons Did Nena Have,99 +Music,What Was XTC's Senses Working,Overtime,General,Wiesmuller what was canada's first national park,Banff national park,General,What is Camilla Parker Bowles' nickname,Bulldog +General,"Who won the Oscar for Best Director for the 1987 film ""The Last Emperor""",Bernardo bertolucci,General,Whats the worlds largest church,"St peters, vatican city",General,Which disease was known as the Kings Evil,Scrofula +General,What is hypertext markup language,Html,General,Where is salzburg,Austria,General,In which country does the Serengeti lie,Tanzania +Sports & Leisure,"What in 1926, did Gertrude Ederle become the first woman to do? ",Swim Across The English Channel ,General,What is a male guinea pig called,Boar,Music,"Who Recorded The Albums ""Love Hurts"" & ""Heart Of Stone""",Cher +General,Which British King Was On The Throne When America Gained Independence,King George 3rd,Geography,Guayaquil is the largest city in what country ,Ecuador ,Food & Drink,What is the fruit flavour of a traditional crepe suzette? ,Orange  +General,Fredrick Bulsara was the lead singer of what pop group,Queen - Freddie Mercury,General,What is the board game where it pays to know your trivia,Trivial pursuit,Sports & Leisure,"Which NFL team's defensive unit was nicknamed ""The Purple People Eaters""?",Minnesota Vikings + Geography,What is the capital of Greece ?,Athens,General,What famous soap opera duo reigned on General Hospital in the eighties?,Luke and Laura,Music,From Which Heavy Metal Band Was Dave Mustaine Fired In 1982,Metallica +Sports & Leisure,In Which Pastime Might You Score A Hit ,Battleships ,General,Where was the original Crystal Palace built,Hyde Park in London,General,"Before Canberra, which city was the capital of Australia",Melbourne +General,In Montreal you cannot park a car blocking what,Your own driveway,General,How did Mussolini die,Execution,Music,"Who Recorded The Album ""Love Over Gold""",Dire Straits +General,Which film had song Springtime for Hitler,The Producers,Art & Literature,In which book is Scheherazade a story teller ?,Arabian Nights,Food & Drink,From Which Plant Does Rum Come From ,Sugar Cane  +General,What Georgia park features carvings on the world's largest piece of exposed granite,Stone mountain,General,October 1939 what UK battleship sunk torpedo loss 800 lives,Royal Oak,General,____________ dubbed the voice of the Beast in the 1991 Disney version of Beauty and the Beast. ,Robby benson +General,Who wrote the opera 'the giant',Sergei prokofiev,General,What is South Korea's national dish called?,Kimchee,General,In Florida it's illegal to molest what,A Trash Can +General,What hit lp did rockpile release in 1980,Seconds of pleasure,General,"What Does The Term ""Kung Fu"" Mean",Leisure Time,General,A cyclonic (rotating) tropical storm with winds at the center in excess of 74 miles per hour is called,Hurricane +General,What was the (West) German capital before the 'Wiedervereinigung',Bonn,Science & Nature,A terrapin is a type of _________.,Turtle,Music,Which hymn traditionally closes The Last Night of the Proms?,Jerusalem +General,What would you buy in a Manitee length,Pearls - a 24 inch choker, Geography,What is the capital of Ethiopia ?,Addis Ababa,General,What's the respiratory organ of most fish,Gill +General,In Iowa state laws prohibits charging admission to see what,One armed piano players,General,Dammen in Dutch is what game,Draughts or Checkers, Geography,Which country owns Corfu?,Greece +General,Radical russian marxists are known as,Bolsheviks,General,Who is the only solo performer to win Euro song twice,Johnny Logan 1980 – 1987,Art & Literature,"Who wrote ""Ten Little Indians?""",Agatha Christie +General,What 20th-century American president was so obsessed with secrecy that he often wrote 'burn this' on personal letters,Lyndon Johnson,General,"Who was the king of England from 1509-1547, and founder of the Church of England",Henry viii,General,What was the name of the aunt in the western How the West Was Won,Molly +General,What is the capital of Hawaii,Honolulu,Food & Drink,What term is used in cooking to describe food that has been cooked so that it is still firm to the bite? ,Al Dente ,Science & Nature,"Carditis, affects the ________.",Heart +Science & Nature,Where Was The Blast Furnace First Introduced ,France Around 1400 , History & Holidays,This European war was named after a peninsula between the Black sea and the sea of Azov?,Crimean War,General,Who was Alexander the Great's father,Phillip ii +General,Estimated there are 4 100 million billion molecules cubic inch what,Air,General,-isms: A painful stiffness of the muscles and joints,Rheumatism,General,An average person uses the bathroom how many times per day,Six +Art & Literature,In Which Century Did Artists First Start Painting On Canvas ,15th Century ,General,Peter George wrote Two Hours to Doom filmed as what,Dr Strangelove,Science & Nature,"This is known as ""The Royal Disease"".",Haemophilia +General,What is the capital of Tasmania,Hobart,General,Name the profession most often late for doctors appointments,Doctors,Music,Who Was The Star Behind Rocket Records,Elton John +General,What airlines identification code is VS,Virgin Atlantic,General,"What is known to Pentagon procurement people as a ""portable handheld communications inscriber""",A pen pen,General,What is the chief monetary unit of Croatia,Kuna +Science & Nature,In Which 1986 film Did Michael Hutchence Star As A Truly Terrible Punk Singer ,Dogs In Space ,Science & Nature,In which organ is Bile produced ?,Liver, Geography,In which country would you find Dunkirk ?,France +General,What type of fruit is a pineapple,Berry,General,Who live in monasteries,Monks,Sports & Leisure,What Nationality Is Former Snooker World Champion Cliff Thorburn ,Canadian  +General,"Name the South African author of 'Cry, the Beloved Country'",Alan paton,General,What's the other name for the statue of Egyptian god Harmachis,The Sphinx, Geography,What is the capital of San Marino ?,San Marino +General,"Who, in 1874, painted the picture called La Loge",Auguste renoir,General,Thirty three degrees fahrenheit who sang 'islands in the stream' with dolly parton,Kenny rogers,General,What tennis players name meant Tall trees by still water,Evonne Goolagong +Geography,Which Bridge Was The First Major Suspension Bridge In The World ,The Menai Strait Bridge In Wales ,General,Who invented a screw which lifts water to a higher level,Archimedes,General,Why is Louise Brown - born 1978 famous,First test tube baby +Science & Nature," In 1874, the first animal purchased for the Lincoln Park Zoo in Chicago was a __________, bought for $10.",Bear cub,General,Managua is the capital of ______,Nicaragua,General,Who did george bush lose a 1970 texas senate election to,Lloyd bentsen +General,"In the bible the book of Psalms is attributed to King David, to who is the book of Ecclesiastes attributed",Solomon,Music,"Which Band Started Out In Basildon Under The Name ""The Composition Of Sound""",Depeche Mode,Music,Where was the first US live Beatles Concert?,Washington D.C. +General,What Was The Name Of Sherlock Holme's More Intelligent Brother?,Mycroft,General,"A Backgammon board is marked out in sawtooth ""points"" in two colours. How many of these points are there",24,General,In which city is the highest steeple?,Ulm +Food & Drink, Laetrile is associated with the pit of which fruit,Apricot,General,In the soapie 'one life to live' who was vicky's alter ego,Nicky,General,Which country was the first to issue parking tickets,France +General,"Who lost 41 of a crew of 98 to scurvy in 1868, on his first voyage to the south pacific",Captain cook,General,What would you do with a drupe,Eat it - it’s a fruit,General,A Primagravida is what,First Pregnancy +General,Which actor/director was responsible for the rebuilding of the Globe Theatre,Sam wanamaker,General,What is the royal disease,Haemophilia,Food & Drink,What Is The Principle Ingredient Of Mayonaise ,Eggs  +General,On any given day half of Americans are on what,A special Diet,General,Gold or silver in lump valued by weight,Bullion,General,What Type Of Foodstuff Takes It's Name From The Spanish For Girdle,Fajita +Music,"Fairground Attraction Had A Hit With The Song ""Perfect"" Or ""People""",Perfect,General,A group of pigs is called a,Litter,General,60% of women experience what,Menstrual Cramps +General,What is the Capital of: Switzerland,Bern,General,"Lake Eyre, Australia's lowest point , is in which state",South australia,General,Which poet wrote 'Ode to the West Wind',Shelley +General,What is the name of a quarter of Jerusalem that can be translated as 'hundred gates'?,Mea Shearim,General,River Providence is the capital of what state,Rhode island,General,What is the name of the desert in California,Mojave +General,In Cheyenne Wyoming its illegal to do what on a Wednesday,Take a shower,General,Slate is formed by the metamorphosis of what,Shale,General,"""Slow Ride"" was Foghats biggest hit from this album released in 1975?",Fool for the city + History & Holidays,Which Country Was Divided Into North And South In 1954 after communists took control of government. ,Vietnam ,Geography,"Each year ___________ has approximately 5,000 earthquakes, including 1,000 that measure above 3.5 on the Richter scale.",Alaska,General,"The Vitra Design Museum in Weil am Rhein, Germany, was the first major work of which architect?",Frank Gehry +General,The stuff that dreams are made off - last words in what film,The Maltese Falcon, History & Holidays,"What is the common name for muscivora forficata, the state bird of oklahoma ",Scissor-tailed flycatcher , History & Holidays,What is the name of David Hasslehoff's Talking car in the show 'Knight Rider'? ,KITT  +General,"Who presents the radio programme ""In the Psychiatrist's Chair""",Anthony clare,General,How thick is an ice hockey puck,One inch,General,Paddie's Wigwam nickname of the RC cathedral what UK city,Liverpool +General,Where is the Kennedy Space Centre?,"Cape Canaveral, Florida",General,Why was Fred Lorz disqualified 1904 Olympic marathon,Hitched a lift passing car,General,The Vince Lombardi Trophy is awarded in which sport?,American Football + Geography,What is the basic unit of currency for Dominica ?,Dollar,General,Which character was invented in a comic for Montgomery Ward,Rudolf the red nosed reindeer,Sports & Leisure,Which Is The Only English Football League Club Beginning Wi The Letter Q ,Queens Park Rangers  +Science & Nature, The Adélie __________ bears the name of French explorer Dumont d'Urville's beloved wife.,Penguin,Food & Drink,Rum is made from this plant.,Sugar cane,General,"What was replaced on the tail planes of British Airways planes, by a design meant to represent its international status",Union jack +General,Who would spin a Gob on their nose end,Glassblower,Geography,What is the largest country in the world? ,Russia (c.17 million square kilometres) ,Science & Nature,Who Was The First Director Of The Atomic Lab At Los Alamos New Mexico ,J Robert Oppenheimer  +Toys & Games,"In snooker, how many points are accumulated in a perfect break?",147,General,"A jump in which the legs open in second position in the air, resembling a scissors.",Ciseaux,General,56% of Americans believe there is what in heaven,Baseball +General,Who gave Yves Saint Laurent his start in fashion,Christian Dior,General,What was Colombo's dog called,Fang - Basset Hound,Sports & Leisure,Who hosted the 1999 cricket World Cup?,England +General,Where is mount vesuvius,Italy,General,Who born US Edu UK Expelled West Point Died Baltimore 40,Edgar Alen Poe,General,In 1656 Christian Huygens invented what type of timekeeper,Pendulum clock +General,Tudor England mans apron shows job white cook what checked,Barber,General,What's the number ten to the power of 100,A googol, History & Holidays,"Which period was first, jurassic or carboniferous?",Carboniferous +General,What is the longest strait in the world,Malacca,General,In what area of France is champagne made,Reims,General,Edinburgh Castle stands on Arthur's Seat what was Arthur's seat,Volcano +General,How many stories did enid blyton publish in 1959,Fifty nine,General,"What sitcom was ""The Facts of Life"" a spinoff of?",Different Strokes,General,"In The World Of Music How Is ""Terry Nelhams"" More Commonly Known",Adam Faith +General,Which animals name comes from Arabic he who walks swiftly,Giraffe,Science & Nature,What Is The Common Name For The Crane Fly ,Daddy Long Legs ,General,What Does A Graphologist Study,Hand Writing +General,What is the official newspaper of capitol hill,Roll call,Entertainment,"In the 1996 version of ""Romeo and Juliet"", who played Juliet?",Claire Danes,General,What is a dzo,Cow Yak cross +General,"This Is “ Tracie Andrews ” Who Hit All The Tabloid Healines On The 3rd Dec 1996, But Why?",Road Rage She Murdered Her Boyfriend Lee Harvey,General,Where could you find the Lutine Bell,Lloyds of London,General,What is the Capital of: Peru,Lima + Geography,Which bridge spans the Hudson River?,George Washington Bridge,General,A government in which power is restricted to a few is a(n) __________.,Oligarchy,General,Name Hopalong Cassidy's horse,Topper +Music,Which group was no.1 in the charts in ’95 with “back for good”?,Take That,General,"What coutnry did 300,000 Chinese troops invade in February, 17979",Vietnam,General,"If a dish is served 'au gratin', what does it have on it",Chesse +Sports & Leisure,"Which Grand Prix Racing Team Are Based In Woking, Surrey ",McClaren ,General,Who was the first sports announcer to address Muhammed Ali by his Muslim name,Howard Cosell,General,Charles Bronson got acting job because he did what on demand,Belch or Burp + History & Holidays,In 70's CB Radio Slang What Is A 'Smokie'' ,A Policeman ,General,"Alphabetically speaking, which is the last of the 26 Irish counties. Most people say Wexford, but they're wrong",Wicklow,General,Is belfast in northern or southern ireland,Northern +General,"Which Band Started Out In Basildon Essex Under The Name ""The Composition Of Sound""",Depeche Mode,Sports & Leisure,Who Was The First Athlete To Exceed a Distance Of Eight Metres In The Long Jump In 1935? ,Jesse Owens ,Religion & Mythology,Who is the greek equivalent of the roman god Cupid,Eros +General,What name is given to the legendary black dog with huge teeth and claws which is prevalent in the North of England mythology?,Barghest,General,"What does the sign BYO, seen outside some Australian eating places, mean",Bring your own booze, Geography,What is the capital of Turkey?,Ankara +General,What movie starred michael caine and angie dickenson,Dressed to kill,Food & Drink,What Is Sauerkraut ,Pickled Cabbage ,General,What was Beethoven's only opera,Fidelio +Food & Drink,"The mild stimulant ""theobromine"" is found in this common lip_smacker.",Chocolate,General,How many letters are in the Cambodian Alphabet,74,General,What childhood disease did only 312 americans have in 1993,Measles +Food & Drink,In which country is the wine making area of Stellenbosch? ,South Africa ,General,Which singer is a former school teacher,Sheryl crow,Science & Nature,"What Colour Is The Mineral, Lapis Lazuli ",Blue  +General,Released in 1908 what was the first ever horror film,Dr Jeckyll and Mr Hyde,General,What is the Capital of: Brazil,Brasilia,General,Hugh Lofting created which famous character,Doctor Dolittle +General,In what book did the grinch steal christmas,The grinch who stole christmas,General,"Who wrote, 'Goodbye to all That'",Robert graves,General, The covering on the tip of a shoelace is a(n)_________.,Aglet +Music,KC and the Sunshine Band stemmed from which US state?,Florida,General, Name the porceilan chair you sit on at least once a day.,Toilet,General,In Massachusetts it is illegal to deliver what on Sundays,Diapers - Nappies +General,What is elton john's real name,Reginald dwight,General,Relating to food what is another name for 'tamarillo',Tree tomato,People & Places,Which Argentine Prop Was Sent Off For Knocking Out An English Second Row With A Punch ,Carlos Mendes  +Religion & Mythology,Egyptian Ibis-headed god?,Thoth,General,What units are used to measure the size of pearls,Grains,General,Roschfort was a baddie in what book and (many) film versions,The Three Musketeers +General,In what country is K2 the world's second-highest mountain,Pakistan,General,Stygiophobia is the fear of,Hell,General,What is Terry Wogans real first name,Michael +General,What is an aasvogel,A vulture,General,"What kind of cancer did Napoleon fear, because it ran in his family",Stomach,General,"Who had a hit with ""Son of my Father"" in 1972",Chicory Tip +General,Who was the first U.S. President to use a word processor?,Jimmy Carter,General,What state is considered the vampire capitol of America,Rhode island,Sports & Leisure,What is the score of a forfeited softball game?,Jul-00 +General,Which Group Was The Motown Labels Biggest Selling Act Of The 1990's,Boys To Men,General,Edgar Allen Poe wrote a famous poem about his animal,Raven,Science & Nature,What Is The More Common Name Of The Poisonous Plant Atropa Belladonna ,Deadly Nightshade  +General,Where is the 'whispering gallery',St paul's cathedral,General,Who failed an audition for Fame because was not pretty enough,Tom Cruise,General,What is the name of the coloured part of the eye,Iris +General,The average person does it 17 times a day - what,Laugh,General,Who wrote the 'Waverley Novels',Walter scott,General,Relating to food what meat is the dish 'saltimbocca' made with,Veal +General,Who is melanie griffith's mother,Tippi hedren,General,The zeedonk is a cross between which two animals,Zebra & donkey,Music,Which Artist Had The Most Number 1 Hits On The US Billboard Charts,Michael Jackson +Entertainment,This movie starring Marlon Brando won the best picture award in 1972.,The Godfather,General,What is stuffed with a sheep's carcass stuffed with chickens stuffed with fish stuffed with eggs,Roast camel,General,"Otter when a player hits a double in darts, what can he do",Start scoring + History & Holidays,Two 747's collided here in 1977.,Canary islands,General,It is illegal to sell what in Lehigh New England,Doughnut Holes,General,Who was the king of sweden from 1907 to 1950,King gustav v +General,What U.S. state includes the telephone area code 515,Iowa,General,What is the name of dr dolittle's parrot,Polynesia,General,What was the first official currency of the u.s,Dollar +General,"Who said ""The no-mind not-thinks no-thoughts about no-things"" ?",Buddha,Food & Drink,Harrissa is what in cooking? ,A spicy paste ,Music,"Who Had A Hit Single In 1982 With ""Chalk Dust"" - The Umpire Strikes Back",The Brat +General,Cousin What U.S. state includes the telephone area code 602,Arizona,General,Who wrote the best-selling novel 'The Family'?,Martina Cole,General,What sport uses a ball called a slitter,Hurling +General,What is the name of the river that runs on the border of california and arizona,Colorado,General,"Which Shakespeare character described himself as having ""Loved not wisely but too well""",Othello,Music,Who Wrote Lionel Richie's Massive Uk No.1 “Hello” ?,Lionel Ritchie +Entertainment,_____ in the name of love?,Stop,General,What did Foucault demonstrate with his pendulum,The rotation of the earth,Food & Drink,Who is the Roman god of wine? ,Bacchhus  +General,What is the scientific name for the gorilla,"""Gorilla gorilla- gorilla""",General,"Whose epitaph reads ""Workers of all lands unite""",Karl marx,General,Juglans Regia is the real name of what type of nut tree,Walnut +General,What gem was Cleopatra's signet,The Amethyst,General,What is the fear of rabies known as,Kynophobia,General,Who invented the first practical steam engine,Thomas newcomen +General,What is the longest typed word that alternates hands,Skepticisms,General,Which two countries signed up to the common market in 1973 alongside the U.K?,Eire and Denmark,Science & Nature,Of what is genetics the study?,Heredity +General,Who coupled with the devil and gave birth to Andrew John,Rosemary,General,Who was the world's oldest man,Bir narayan chaudhari,General,Where do boy scout leaders get their names from,The Jungle Book – Rudyard Kipling +General,In which country is the highest European waterfall,"Ormeli, norway",General,Who does a Filicide kill,Son or Daughter,General,What 1980 Vietnam flick was re-released in 1987,Apocalypse now +General,Grammy Awards: What album by Michael Jackson won the grammy in 1983,Thriller,General,What English king married his brother's widow,Henry viii,General,"What causes ""baker's itch""",Yeast +General,In which country is the Nokia company based,Finland,General,Who wrote The Picture of Dorian Grey in 1891,Oscar Wilde,General,Soap what's the capital of illinois,Springfield +General,What is e.t famous for saying,E.t phone home,General,What come in varieties Norway Oriental Sitka White Siberian,Spruce Trees,General,Which country was the first to give women the vote,New zealand +General,What country had an airline called Rottnest,Australia,General,What is the young of this animal called: Turkey,Poult,Music,"Who Stormed To The Top Of The Charts With ""You To Me Are Everything"" In 1976",The Real Thing +General,African American Garrett A Morgan invented what,The Traffic Signal,General,This underground cartoonist created Fritz the cat & Mr. Natural.,Robert crumb,General,What was the basis of the giant u.s prune industry,Agen plum +General,"The heraldic term ""gules"", meaning red, comes from the french word ""gueules,"" meaning a ______",Throat,General,"What are Kayobi, Doyobi, and Kinyobi",Japanese days of the week,General,Which countries translated name means Land of the Eagle,Albania +General,Bowser' and 'Jocko' have been two prominent members of what very successful rock & roll nostalgia act?,Sha Na Na,General,In Washington state it is specifically illegal to have sex with who,Virgins,General,Who was the first rock star arrested on stage,Jim Morrison of the Doors +General,Where was Enrico Caruso born,"Naples, italy",General,What animal has bony plates and rolls up into a ball if its frightened,Armadillo,General,Halophobia fear of what,Speaking +General,Who would use a technique called pleaching,Gardener twine branches to hedge,Entertainment,Who did the music for the 1970's film 'Saturday Night Fever'?,Bee Gees,Geography,In what country is Taipei,Taiwan +Science & Nature, A camel can shut its nostrils during a __________,Desert sandstorm,General,What does v.s.o.p. stand for on a bottle of brandy,Very superior old pale,General,Who was the first fallen angel,Lucifer +General,What is the fear of progress known as,Prosophobia,General,Hipopota Agravis or guasano is what in Mexican Tequila,Not Worm – Butterfly Caterpillar,General,An oxlike antelope,Gnu +General,"What state contains the most cacti, or cactuses",Arizona,General,There are 45 miles of what in the skin of a human being,Nerves,General, Name the pain_inflicting person you go to to get your teeth fixed,Dentist +General,What colour is the wax covering Gouda cheese,Yellow,General,In proportion which animal has the largest eye,Cat,General,Rickets is caused by a lack of which vitamin,Vitamin d +General,"Wheel according to bonnie tyler, what hits you when it's too late",Heartache,Music,What Question Was Asked In The Title Of A Hit By Both The Eurythmics And Charles & Eddy?,Would I Lie To You,General,What was napoleon's surname,Bonaparte +General,"In 'star wars', who was darth vader's face",Sebastian shaw,General,"Which play opens with: ""when shall we three meet again""",Macbeth,Science & Nature,What Do The Lymph Glands Produce ,White Blood Cells  +General,What is the easternmost city in Germany,Berlin,General,How many thousand million tonnes of carbon dioxide are formed each year by the combustion of fossil fuels,Sixteen,General,Head which spanish explorer named the amazon river,Orellana +General,Who is the only baseball player to have been killed in a major league game,Raymond chapman,General,Teheran is the capital of ______,Iran,General,What is the date on Smirnoff Vodka bottles,1818 +General,Which car's name translates from latin as 'I Roll'?,Volvo,General,The USA president lives in the White House - Who Blue House,President South Korea,General,What is the worlds most widely eaten fish,Herring +General,Where's the famed Arch of Hadrian,Athens,General,Which fruit contains the most protein,Avocado,Sports & Leisure,Football: The Pittsburgh ______?,Steelers +General,"Who From The World Of Sport Penned The Autobiography Entitled ""Facing The Music""",Torville & Dean,Art & Literature,Meaning 'fresh' in italian. The technique of painting on moist lime plaster with colors ground in water. ,Fresco,General,Name Irelands oldest licensed whiskey distillery,Bushmills +Music,Fred Scnheider Was A Member Of Which Famous Band,The B 52's,General,What country celebrates its National Day on 26th October?,Austria,Music,"Who Had A Christmas Hit In 1979 With ""Day Trip To Bangor""",Fiddlers Dram +General,What is a dialogue,A conversation,General,A hot spring which shoots steam into the air is a _______,Geyser,General,25% of sexually active people have tried what,Anal Sex +General,Where in the body is the axilla,Armpit,General,"Liquor whose name is derived from uisge beath, Gaelic for aqua vitae",Whisky,General,Whats the nickname for Indiana,Hoosier state +Music,"What Is The Connection Between Jean Luc Ponty, Stephane Grappelli, & Jerry Goodman",Violin,General,The Brownies ( junior Girl Guides) used to be named what,The Rosebuds,General,This bird can also be found on a chess board,Rook +General,What star of the Breakfast Club was also part of the orginal cast of the facts of life?,Molly Ringwald,Sports & Leisure,What is the name of the Manchester City's old home ground? ,Maine Road ,General,The spot on the earth's surface directly above an earthquake's focus is called the ______,Epicenter +Sports & Leisure,Packy East Had 3 Boxing Bouts All Of Which He Lost When Asked About Them He Said & I Quote ' I Was On The Canvas More Times Than Rembrandt '' How is he better known ,Bob Hope ,General,Who won two Nobel prizes in different fields,Marie Curie Physics 1903 Chemistry 1911,General,What does the girls name Donna mean,Lady - From Latin +General,What is the official language of Cuba,Spanish,General,What car manufacturer also makes airplane engines,Rolls royce,General,The study of human pre-history is ___________.,Archaeology +General,Motorphobia is the fear of,Automobiles,General,What city used to be known as Bytown,Ottawa, History & Holidays,"In which Christmas song would you find the lines, 'He's making a list and he's checking it twice. He's going to know who's naughty and nice'' ",Santa Claus Is Coming To Town''  +General,"__________ Ford is listed as one of 50 people barred from entering Tibet _ apparently, Disney clashed with Chinese officials over the film Kundun (1997). Ford's wife Melissa Mathison wrote the screenplay. ",Harrison,General,If You Studied Horology What Do You Study,Clocks / Time,Sports & Leisure,In a game of netball how many players on a team are allowed To score? ,Two  +Entertainment,"When not fighting crime, what did Underdog do for a living?",Shoeshine boy,General,What cities underground has the most stations,New York,General,What's missing from a woman suffering from amazia,Breasts +Food & Drink,"Often drunk, this liquid is normally harvested from female cows ",Milk ,General,Richard Attenbourough what character in The Great Escape,Bartlett,General,"What type of particle alongside Helium, is released during the process of Nuclear Fusion in the sun?",Photon +General,Which country grows the most fruit,China,General,Who was once billed as The Atomic Powered Singer,Elvis Presley,Science & Nature,What Does The Acronym ROM Stand For With Regard To Computer Technology? ,Read Only Memory  +Science & Nature,What Vegetables Are Part Of The Family Allium? ,"Onions, Garlic, Leek, Shallot And Chive ",Sports & Leisure,"In the Entire English Football League, Which Football Team Has The Shortest Name? ",Bury ,General,In Alberta what are you not allowed to Paint (its illegal),Wooden Log +General,220 yards equals one,Furlong,General,An ecostate animal lacks what,Ribs,Music,Which California festival brought Jimi Hendrix fame in the US?,Monterey +General,Which Mediterranean island was divided into two in 1974,Cyprus,Sports & Leisure,Which Athlete Won Olmpic Gold In Both The 800m & 1500m At The 1990 Games In Athens In 2004 ,Kelly Holmes ,General,Young man paid by older woman to be escort or lover,Gigolo +General,"To what does the obscure song ""Turning Japanese"" refer?",Masturbation,People & Places,In which county is Uttoxeter? ,Staffordshire ,General,Of what substance are teeth made,Enamel +General,How many years did Hitler predict the Third Reich would reign,1000 years,General,"In Which US State Will You Find ""Lincoln"" International Airport",Nebraska,General,"What does the ancient Greek word ""electron"" mean ?",Amber + Geography,In which state are Gettysburg and the Liberty Bell?,Pennsylvania,General,Who was the first man to run a sub four minute mile,Roger Bannister,General,Citius Altius Fortius is the motto of what organisation,Olympic +General,A collision between a bird and an aircraft,Bird-strike,General,King Zog ruled which country,Albania,Music,"The Lead Singer Of Spandau Ballet Was ""Tony Hadley"" Or ""Martin Kemp""",Tony Hadley + History & Holidays,"""What Did My True Love Give To Me On The """"Tenth"""" Day Of Christmas"" ",10 Lords A Leaping ,General,Which herb is used to make the garnish 'gremolata',Parsley,General,At what age does a filly become a mare,Five +Science & Nature," __________ feel safest when they are crowded together, hundreds in a group.",Flamingoes,General,Who painted the Sistine Chapel,Michelangelo,General,The Chinese apple is another name for what fruit,Pomegranate +General,Which company developed 'instant' coffee in the 1930s,Nestle, History & Holidays,"What was significant about the words (Watson, please come here, I want you) when spoken in 1876? ",First Complete Sentance Spoken On The Phone ,General,What Is The Collective Term For A Group Of Pigs,Drove +People & Places,Who Bought The Ritz Hotel In Paris In 1979 ,Mohammed Al Fayed ,General,"""Janie's Got a Gun"" was on their 1989 album ""Pump""",Aerosmith, History & Holidays,Which English King issued the Magna Carta in 1215?,King John +General,What former british colony has a famed jade market on canton road,Hong kong,General,Cavatina what the theme music to which film,The Deer Hunter,General,Who played the title role in Tom Jones,Albert finney +General,Who was the male star of the film Fatal Attraction,Michael douglas,General,Which animal has legs but cant walk,Hummingbird,General,What is the modern name of the Roman town of Glevum,Gloucester +General,What is the average temperature (f) at the South Pole,Minus fifty six 56,General,What's nero wolfe's favorite drink during office hours,Beer,General,Who was the first Plantaggenet King of England,Henry i +Food & Drink,"In the year 2000, what was the most popular sweet brand in the UK - Rowntree Fruit Pastilles, Starburst or Polo Mints? ",Polo Mints , Geography,"Which country uses the ""yen"" for currency?",Japan,Religion & Mythology,What is the 1st book of the Hindu scripture?,Rig Veda +General,"Which element, discovered by Pierre and Marie Curie, did she name after the country of her birth",Polonium,General,Which two nations built the concorde?,Britain and France,General,What does the lack of iodine in the diet cause,Goitre +General,What is the day called when the earth is closest to the sun,Perigee,General,What is the name of the world's most expensive brand of coffee which is made from Civet poo?,Kopi Luwak,General,Avocados have the highest calories of any fruit at ___ calories per hundred grams,167 +General,What relation was Louis XV of France to Louis XIV,Great grandson,General,What animal is mentioned most in the Bible,Sheep,General,What colour is a robin's egg,Blue +Science & Nature,What two planets dont have moons?,Mercury and Venus,General,What was Maxwell Smarts cover,Greetings Card salesman,General,Who uses roosevelt's phrase 'good to the last drop',Maxwell house coffee +Science & Nature," A baby gray __________ drinks enough milk to fill more than 2,000 bottles a day.",Whale,General,The earth's atmosphere and the space beyond is known as _________.,Aerospace,General,This U.S. state touches 4 of 5 great lakes,Michigan +General,How many inches tall are the bearskins worn by the guards at Buckingham Palace?,20,General,Agnes Gonxha Bojaxhiu born Skopje 1910 other name,Mother Theresa,General,What plant has flowers but no leaves,Cactus +General,What does a polyandric women have more than one of,Husband,General,"With over 41% of the population, which country has the highest ratio of cellular mobile phones",Finland,General,Blood can be artificially cleansed via what process,Dialysis +General,Beethoven's 9th was his interpretation of what work by Schiller,Ode to Joy,General,Los Pedernales is a Spanish translation what TV show,The Flintstones,General,A kind of small domestic fowl,Bantam + Language,What does the abbreviation N/A mean?,Not applicable,General,What sport has you hike out while close hauled,Sailing,General,What country holds the Olympic polo championship,Argentina last contested 1932 +General,"The study of natural phenomena: motion, forces, light, sound, etc. is called ______.",Physics,Music,What Was The Only No.1 Single For The Monkees,I'm A Believer,Music,The Beatles Had 3 No.1's In 1963 Name 2 Of Them,"From Me To You, I Want To Hold Your Hand, She Loves You" +General,Who played clyde to faye dunaway's bonnie,Warren beatty,General,The are six sides on a standard one - a standard what,Pencil,Sports & Leisure,Who Holds The Record For The Most Grand Prix Wins ,Alain Prost  +Sports & Leisure,What is soccer star Pele's real name?,Edson Arantes do Nascimento,General,How often did babe ruth change the cabbage leaf which he wore under his cap,Every two innings every 2 innings two innings 2 innings,General,"Who played Little Joe on ""Bonanza""",Michael Landon +General,What is the sfa associated with,Football,General,"In warfare and law-enforcement, what is the more common name for lachrymators",Tear gas,General,What is usually served at bedouin feasts,Roast camel +General,"Of love Heavier-than-air craft that derives its lift not from fixed wings like those of conventional airplanes, but from a power-driven rotor or rotors, revolving on a vertical axis above the fuselage",Helicopter,Food & Drink,What Is The Main Ingredient Of Paella ,Rice ,Science & Nature," At birth, a __________ is smaller than a mouse and weighs about four ounces.",Panda +General,Which U.S. soul singer was known as 'Lady Day',Billie holiday,General,Who refused to leave the table when gambling & invented a new meal,Earl of sandwich,Music,Which Group Featured Carl Douglas In A Re-Mix Of Kung Fu Fighting,Bus Stop +Science & Nature,What is the meaning of the name of the constellation Cygnus ?,Swan,General,Who invented the lie detector,John augustus larson,General,"What 1,300 foot column of basalt do wyoming indians want to keep people from climbing",Devil's tower + History & Holidays,Who Was The First President Of The United States? ,George Washington ,General,The British call it Shrove Tuesday (pancake day) what in France,Mardi Gras,General,Technophobia is a fear of ______,Technology +General,Institution for young offenders having a tough quasi-military regime,Boot camp,General,Whose cusine would offer you Leberkas,German it’s liver,General,Mlb: who was the last American league player to win the triple crown?,Mickey mantle + History & Holidays,What caused the gremlins in the movie Gremlin to become evil? ,Eating after midnight ,Music,Who Had A Hit With Maneater In The 80's,Hall & Oates,Geography,Under what river does the Holland Tunnel run,Hudson +General,If an alloy is an amalgam what metal must it contain,Mercury, History & Holidays,What Major Event Occured In Russia In 1917 ,The Russian Revolution ,General,It is illegal to take picture of who in Zambia,Pygmies +Sports & Leisure,"What sport do the following terms belong to - ""Sweeper & Advantage Rule""?",Soccer,Religion & Mythology,"Who was the Greek God of prophecy & archery, music & healing, light & truth, agriculture and cattle?",Apollo, Geography,What is the basic unit of currency for Saint Kitts and Nevis ?,Dollar +Religion & Mythology,How many sayings did Jesus say from the cross?,Seven,General,Titus Oates was the instigator of which historical plot,Popish,General,"In 'startrek', who played dr spock",Leonard nimoy +General,"In the opera 'la traviata', what was violetta's occupation",Courtesan, Geography,Luxembourg is the capital of ______?,Luxembourg,Music,Who sang vocals for 'Big Brother and the Holding Company'?,Janis Joplin +Sports & Leisure,In What Sport Might You Need To Perform An Eskimo Roll ,Canoeing & Kayaking ,Music,"Who Had A Hit In 1984 With The Song ""It's A Miracle""",Culture Club,General,"During World War II, which London theatre boasted, ""We never closed""",The windmill +Science & Nature,What name is used to describe permanently-frozen subsoil?,Permafrost,Science & Nature,"Which type of bat can be common, white-winged or hairy-legged? ",Vampire ,Science & Nature,"As Stipulated By The International Code Of Zoological Nomenclature, Dinasaur Names Are In 2 Parts , The First Part Being The Genus. What Is Denoted By The Second Part ",Species  +General,In 1666 Jesuit Bark was used as a prevention against what,Malaria,General,What is measured with a Snodgrass grathodynamometer,Strength Shark Bites,General,When does a full moon always rise,Sunset +General,"Who is the hero of Rider Haggard's adventure, King Solomon's Mines",Alan quartermain,General,The Soviet Sukhoi-34 fighter was the worlds first with what,A Toilet in it,General,What was patented in 1954 - the best thing?,Sliced bread +General,What country is the world's deepest mine located,South Africa,Sports & Leisure,In Which City Does The Cycle Race The Tour De France Finish? ,Paris ,General,Name the Monkeys only film made in 1969,Head +General,Stanley Kubrick Started It In The Early 1970's & Steven Spielberg Finished It In 2001 Name The Movie,A.I,General,"By What Name Is The Notorious ""Richard John Bingham"" More Commonly Known",Lord Lucan,Sports & Leisure,The Gallagher Brothers From The Band Oasis Support Which Football Team? ,Manchester United  + Geography,What is the capital of Croatia ?,Zagreb, History & Holidays,On Which Date Does Halloween Fall ,October 31st ,Music,Which Songwriters Formed Philadelphia International Records,Gamble & Huff +General,For which game would you win the Plimpton Cup,Backgammon,General,A paratrichosic person has extra what,Hair in unusual places,Art & Literature,Which Character Featured In Jules Verne's (Around The World In 80 Days) ,Phileas Fogg  +General,Where are bangtails found,Mailer envelopes,General,What is the name of the weak attractive bonds which exist between molecules,Van der waals forces,General,What was the last European nation to accept the potato,France +Science & Nature,What Is A Particle Of Light Called ,A Photon ,General,A weavers knot is known by seamen as a common what,Sheet Bend,Toys & Games,"If you ""peg out"" what game are you playing?",Cribbage +General,"What, with arabic, is the official language of mauritania",French,General,What was barbara streisand's first film,Funny girl,People & Places,Name The Worlds Richest Woman ? ,Queen Elizabeth II  +General,Zoisite is a semi precious stone - National stone which country,Norway,General,What is contained in a dish described as Lyonnaise?,Onions,General,What country is known to its inhabitants as Suomen Tasavalta,Finland +Science & Nature,What bird has the biggest wingspan?,Albatross,Science & Nature,What Form Of Radiation Has The Shortest Wavelength ,Gamma Rays ,General,What bird is the symbol of Penguin books (children's section),Puffin +General,Large dog of a breed of wolfhound,Alsatian,General,Churchill what kind of bug emerges in the 1975 movie the bug,Beetle,General,The first known what happened in Wisconsin 1878,Organised motor race +Science & Nature, The __________ is the first bird mentioned in the Bible. It was sent out by Noah to see if the waters had abated.,Raven,General,Why was McDonalds fillet of fish invented,Meatless Lent,Geography,The distance from Honolulu to New York is greater than the distance from Honolulu to ___________,Japan +General,In the Canterbury Tales why were the pilgrims travelling,To visit Thomas a' Becketts Tomb,General,Most blue eyed cats are what,Deaf,General,Illinois State law its illegal to speak what language,English – only American is legal +Sports & Leisure,Who Was PFA Young Player Of The Year In 1995 & 1996? ,Robbie Fowler ,General,How many bags of mail were lost by the Pony Express,One,General,An average person does it six times a day - what,Goes to bathroom +General,Who was the first british royal to make people magazine's 'worst dressed list' five times,Sarah ferguson,General,A numismatist collects coins and what else,Medals,General,What U.S. state includes the telephone area code 605,South dakota +General,Meat from animal killed according to Muslim law,Halal, History & Holidays,"In _Rocky,_ what does Apollo Creed's trainer warn him about Rocky Balboa? ",He's Left Handed ,General,Name William Shakespeare son,Hamnet +General,What can be Vulgar Common Simple Improper or proper,Fractions,General,In Singapore you can be fined 10% of income for not doing what,Flushing public toilet,Geography,What is the capital of Uruguay,Montevideo +General,Where are the Guiana Highlands,Northern south america,General,Who is the roman counterpart of hermes,Mercury,General,Which car company features a badge called the lion of Belfort?,Peugeot +General,It was Greek to me is a line from which Shakespearean play,Julius caesar, History & Holidays,To What Was Byzantium Renamed In 330 AD ,Constantinople ,General,Where were Chinese Checkers invented,England +Science & Nature,Where Was The Potters Wheel First Used ,In Mesopotamia c.3000 Bc ,General,"German physicist and Nobel laureate, who was the originator of the quantum theory",Planck,General,What word describes the practice of growing and tending of a forest,Silviculture +Music,"Who Had A Hit With ""I Love A Rainy Night""",Eddie Rabbitt,General,"One of the earliest centers of urban civilization, in the area of modern Iraq and eastern Syria between the Tigris and Euphrates rivers",Mesopotamia,General,What name is given to the horizontal bar found in a window?,Transom +General,Who would be scored on the Apgar scale,Newborn Babies,General,Which African bird is famous for its ability to walk up to 20 miles per day and also for its legendary ability to kill snakes by stamping on them with its giant feet?,Secretary Bird,General,What kind of creature is a mudskipper,Fish +General,Who wrote the 'Aeneid',Virgil, History & Holidays,What colour is Santa Claus' belt? ,Black ,General,Where is the TV space alien ALF from,Melmac +General,What did air cadet frank whittle invent in 1928,Jet engine,General,Which planet was the 'Planet of the Apes',Earth,General,"What,mainly,is added to iron to make steel resistant to corrosion",Chromium +General,"Which famous singer lived at 20 Forthlin Road,Liverpool",Paul mccartney,General,This more efficient distillate of coal was one of the main fuels of the industrial revolution:,Coke,General,"The Band ""Oingo Boingo"" Sang The Theme Tune That Is Also The The Title To Which Cult 1985 Movie",Weird Science +General,"Bill Watterson, cartoonist for Calvin & Hobbes, is the first cartoonist to use what word in his cartoon",Booger,General,"What sports celebrity had the nickname of ""Truck""",Leonard robinson,Geography,Of Which Country Is (Freetown) The Capital ,Sierra Leone  + History & Holidays,What was first worn on the 10th October 1886 ?,Tuxedo,Music,Travelling Without Moving Was A Huge Selling Album For Which 1990's Dance Act,Jamiroquai,General,What is a cockerel,Baby rooster +General,"The chemical phenylethylamine, which your brain produces when you fall in love, is found in what food",Chocolate,General,What was the nickname of the Scottish novelist and poet James Hogg?,Ettrick Shepherd,General,Who once kept live mice in a desk drawer so they'd be available when he wanted to sketch one,Walt disney +General,Who was called The Scourge of God,Attila the Hun,Music,What Popstar With A 185 Inch Waistline & Standing 6”1 Topped The Charts In 1993,Mr Blobby,General,The Golden Bear is awarded at which film festival,Berlin +Music,Who Was The Lead Singer & Producer For Cameo,Larry Blackmon,General,What is the largest state in the USA,Alaska,General,What is the 'd' in dwight d eisenhower's name,David +Art & Literature,Name the author of 'The Catcher in the Rye',J.D. Salinger,Music,Which Row Was Sung About In Porgy And Bess,Catfish Row In Charlston,Food & Drink," Often drank, this liquid is normally harvested from female cows.",Milk +General,This Indian group ruled in early Peru,Incas,Music,Name Archie Bells Backing Group,The Drells,General,What is the atomic number for californium,Ninety eight +General,There are 16 ______ in a cup,Tablespoons,General,In the man from UNCLE who were their enemies,THRUSH,General,What is the expulsion of evil spirits from persons or places through ritual methods,Exorcism +General,What is one of the items that the wood of the sycamore tree is used for,Boxes,General,"What film began ""Most of what follows is true""",Butch Cassidy and the Sundance Kid, History & Holidays,In 1692 The Witchcraft Trials took place in America. Where ,"Salem, Massachusetts " + Geography,On which coast of Australia is Sydney?,East,Sports & Leisure,Which American was Olympic long jump champion in 1996? ,Carl Lewis ,General,In which state is Yale University,Connecticut +General,What sport/game is bobby fischer associated with,Chess,General,Who invented analytic geometry,Descartes,Science & Nature,What Does A Butterfly Use To Taste? ,Its Feet  +General,Eskimo culture encourages male visitors to do what,Sleep with hosts wife,General,Robert Whithead invented what weapon in 1866,Torpedo,General,Who designed the famous cover of 'Sgt Peppers Lonely Heart Club Band',Peter blake +General,Honeydew' is a variety of what,Melon,Sports & Leisure,Which American Horse Race Is Run At Churchill Downs? ,The Kentucky Derby ,General,In the US 20% of all lightning strike deaths occur where,Golf Course +General,"What Cartoon Character Was Created, Inspired by The Frank Sinatra Song Strangers In The Night",Scooby Doo,General,"Who, in the 18th Century, was known as 'sea green incorruptible'",Robespierre,Geography,On which River does the City of Budapest stand ,The Danube  +General,Irish Proverb - If you want to be criticized do what,Marry,General,What is the name of the process whereby plants lose water into the atmosphere,Transpiration,General,A Badger gets its name from badge meaning what,White mark on face +General,Maputo is the capital of ______,Mozambique, History & Holidays,What Saint's Day is celebrated on 26th December ,St. Stephen ,Sports & Leisure,With which sport is Chirs Evert-Lloyd associated ?,Tennis +Food & Drink,What Type Of Fruit Is A Russet ,An Apple ,General,What is evonne cawley's maiden name,Evonne goolagong,General,What subject did critic Mary Whitehouse teach,Art +General,Marengo was Napoleons horse but he rode who at Waterloo,Disiree white Arabian,General,"Who sang for 'bad company' and 'the firm', then went out on his own",Paul,General,What was the most common automobile colour in the depression,Black +General,What were J.B. Priestley's christian names,John boynton,General,Which Country Has The Biggest Proportion Of It's Population In The Armed Forces,North Korea,General,"Where was Robinson Crusoe's home, according to the book",York +Science & Nature," The hippopotamus gives birth __________ and nurses its young in the river as well, although the young hippos must come up periodically for air.",Underwater,People & Places,Which English comedian's real name is Bob Davies? ,Jasper Carrott , History & Holidays,What Song Was Christmas Number One In The UK In Both 1975 And 1991? ,Bohemian Rhapsody By Queen  +General,Which orchestral instrument can play the highest note,The Violin,Music,"Who Had Mid 80's Hits With ""Grimly Fiendish"" And ""The Shadow Of Love""",The Damned,General,"In the pioneer spacecraft, what are the humans on the plaques wearing",Nothing +General,Which kellogg's cereal was advertised by tusk tusk the elephant,Coco,General,Lauris Nobilis is the Latin name of what common herb,Bay,Entertainment,"Whose films include 'Giant', 'Written On The Wind' and 'A Farewell To Arms'?",Rock Hudson +General,In 'Dirty Dancing' what was Baby's real name?,Frances,General,What was the name of the giant panda in the Moscow Zoo in 1977,An-an,Sports & Leisure,Which 150:1 outsider won the Embassy World Snooker Championship in 1986? ,Joe Johnson  +General,What is the alternative name for the Galaxy,Milky way,General,When does a Bride walk up the Aisle,Never Aisles at side only,General,What's the largest island in the Atlantic,Greenland +General,Type of frothy milky coffee,Cappuccino,General,Who said 'et tu brute',Julius caesar,Music,What Was The Best Selling Abba Single In The Uk,Dancing Queen +Music,"""Get Off My Cloud"" Was A Hit For Which Group In 1965",The Rolling Stones,General,Which conqueror started with a small tribe at the age of 13 and had conquered empires from the Black Sea to the Pacific by the time he died in 1227,Genghiz khan,General,Venice stands on what river,The Arno +Entertainment,In what city does Fat Albert live,Philadelphia,General,What was the shoulder patch US Army 45th in WW2,Swastika,General,"Who is the sumerian goddess of love, fertility and war",Inanna +Music,"Who Were ""Big In Japan""",Alphaville,General,What are Ingrid Marie and Blushing Golden,Varieties of Apple,General,Who was the first Dutch player to become European footballer of the year,Johan cruyff +Food & Drink,McDonald's 'Iced Tea' is actually this brand,Nestea,General,"What are Bullace, Kirke's Blue, and Opal varieties of",Plum,General,What do you call a substance containing only one kind of atom,An element +General,Where was the first public library opened in 1747,Warsaw Poland,Geography,What country is directly north of the continental United States,Canada,General,Which two bodies of water separate Britain from mainland Europe,The english channel & the north sea english channel & north sea north sea & english channel +General,9 p.m. In military time is how many hours,2100, Language,What is the only English word formed by the first three letters of the alphabet,Cab,General,In Celebrity Big Brother 2004 The Non Celebrity Chantelle Houghton Entered The House And Was Asked To Assume The Identity Of A Member Of A Fictional Girl Band What Was It Called?,Kandyfoss (With A K) +General,"Which author wrote ""The Duncton Chronicles""",William horwood, History & Holidays,In What Year Was Abraham Lincoln Assasinated ,1865 ,General,Who wrote the novels on which the films 'Carrie' and 'The Shining' were based,Stephen king +General,Which former member of the group 'Cream' received the OBE in 1995,Eric clapton,Art & Literature,The study of building design is ____________.,Architecture,General,In Greek mythology who placed callisto in the heavens as the constellation of ursa major,Zeus +General,What is the 'pound' or 'number' symbol on the telephone,Octothorpe,General, Which science studies weather,Meteorology,Science & Nature,To What Was The Process Of Vulcanisation Applied ,Rubber  +General,What 1839 innovation changed the face of mail delivery,The envelope envelope,General,Which Mark Twain classic was remade by Disney in 1993,Adventures of huckleberry finn, History & Holidays,"What 19th century war between Russia and England, Turkey, Britain and France, was named after a peninsula in the Black Sea?",Crimean War +General,In Utmost Good Faith is the motto of which organisation,Lloyds of London,Entertainment,"Who is the only singer to have no.1 hits in the 50's, 60's, 70's, 80's and 90's?",Cliff Richard,General,What were the two cities in 'a tale of two cities',London and paris +General,Harold Edgerton has taken all the worlds photos of what,US nuclear bomb explosions,General,What Shakespeare play Course true love never did run smooth,Midsummer Nights Dream,General,"What was described on TV as, 'Bread wi' nowt taken out'?",Allinsons Bread +Geography,"____________ means ""land of the free.""",Thailand,General,"In Which American City Was ""Martin Luther King"" Assasinated",Memphis,General,The Spink standard catalogue lists information about what,Coins +General,13th century Paris brothels were the first to have what,Red Lights,General,Moored in halifax harbour is a replica of this famous schooner,Bluenose ii,General,Where is the taj mahal,India +General,Name Merlin's owl in Disney's Sword in the Stone,Archimedes,General,Who composed the musical piece Carmina Burana,Carl orff,General,In Arkansas a man can only do what legally once a month,Beat his wife +General,"Compact, opaque gemstone ranging in color from dark green to almost white",Jade,Geography,What is the capital of Zaire,Kinshasa,General,"Who wrote 'little lamb, who made thee'",William blake +Science & Nature, Most __________ lived to be more than a hundred years old.,Dinosaurs,People & Places,Who Was The 16th Century Seer Famous For His Predictions ,Nostradamus ,General,What is the largest city in ecuador,Guayaquil +Science & Nature,"At the equator, what is the brightest star in the night sky?",Sirius,General,Who was the first woman to win an acting Emmy in a sf series,Lindsay Wagner – Bionic Woman,General,One anger Two mirth Three wedding Four birth what are they,Crows +Sports & Leisure,In what sport is the Heisman trophy awarded,American football,Music,Gene Pitney Was How Far From Which Town In 1963,24 Hours From Tulsa,General,What is the highest U.S. mountain,Mt. mckinley +General,The martial art tai quon do translates literally as what,Kick Art Way,General,According to survey what European country has the vainest men,Britain,General,"""Who"" is in every episode of the tv series 'Seinfeld'",Superman +Food & Drink,The Berries Of The Juniper Tree Are Used To Flavour Which Alcoholic Drink? ,Gin ,Music,"Who Had A 90's Debut Hit With The Song ""House Of Love""",East 17,Art & Literature,Who did author Leslie Charteris create?,The Saint +Art & Literature,The surrealist painter Salvador Dali was a native of which country?,Spain, Geography,"What country was the setting for ""Doctor Zhivago""?",Russia,General,In which season are coyote pups usually born,Spring +General,"Who appeared in the films Pulp Fiction, Primary Colors and Staying Alive",John travolta,Science & Nature, A mole can dig a tunnel __________ feet long in one night.,300,General,Where is the BernabaU.S.tadium,"Madrid, spain" +General,What is the largest lake in the u.s,Superior,General,What is the flower that stands for: rupture of a contract,Broken straw,General,Who wrote Gentlemen Prefer Blonds,Anita Loos +General,Viscum Album provides an excuse for stealing what,A Kiss (its Mistletoe),Sports & Leisure,The first cricket one-day international was held between england and ______?,Australia,General,In literature who was the wife of Othello,Desdemona +General,What was the principal wood used by Thomas Chippendale during the 18th century,Mahogany,General,"In the Breakfast Club, Bender tells a joke without a punchline. What was the naked blonde carrying under her arms?",A two foot salami and a poodle,General,The Vistula flows into which sea,Baltic +General,What's miami's most famous suburb,Miami beach,General,The bishop's throne in a cathedral is called a___,Cathedra,General,What type of creature was Salar - that Tarka would like to eat,Salmon +General,"What is stolen by tom, tom the piper's son",Pig, History & Holidays,What was the first movie Disney released through a subsidary company that carried an R rating? ,Down and Out In Beverly Hills ,Science & Nature," There are seven distinctive types of combs on __________: rose, strawberry, single, cushion, buttercup, pea, and V_shaped.",Chickens +General,Which European city has a cathedral located inside an old mosque,Cordoba, History & Holidays,*What arcade game became a hit in 1973* ,Pong ,People & Places,"Whaat Do Lord Lucan, Amelia Earheart , & Buster Crabbe Have In Common ",They All Disappeared Without A Trace  +Music,The Bee Gees Were Born In Manchester But Spent Their Teenage Years Where,Australia,General,"In physics and engineering, the property of a body that causes it to return to its original position or motion as a result of the action of the so-called restoring forces, or torques, once the body has been disturbed from a condition of equilibrium.",Stability,General,After who was 'decibel' named,Alexander graham bell +Geography,What is the capital of Micronesia,Palikir,General,What is the worlds most popular first name,Mohammed,General,From which Shakespeare play does the line 'All the world's a stage' come,As you like it +General,What pre-tv radio show turned film caused people to commit suicide when it was first aired,War of the worlds,General,"In music, what does prestissimo mean",Extremely fast,General,In what jurisdiction was elroy p lobo sheriff,Orly county georgia +General,Which Country Is The Largest Importer Of Arms In Europe,Greece,General,Whats the ball on the top of a flagpole called?,Truck,Art & Literature,Which Author Wrote To Books The Shining And Pet Sematary ,Stephen King  +Sports & Leisure,Who is the last British player to win the women's singles title at Wimbledon? ,Virginia Wade ,General,What is the only known animal to have cube-shaped droppings?,Wombat,General,What does the symbol 'sm' represent,Samarium +Sports & Leisure,Who was Martina Navratilova's usual ladies doubles partner in the 1980's? ,Pam Shriver ,General,What is the name of Dilbert's company's competitor,Nirvana co,General,"""Little Boy"" & ""Fat Man"" were the first",Atomic bombs +General,"Once married to Ted Hughes, which American poet committed suicide in 1963",Sylvia plath,General,Which company had slogan You don’t win silver you lose gold,Nike - 1996 Olympics,General,Where are the glasshouse mountains,Queensland Australia +General,"What U.S. state name is sioux for ""south wind people""",Kansas,General,Which Irish writer appeared on the Irish £10 note,James Joyce,General,What nursery rhyme character slept in the mountains for 20years,Rip van winkle +General,Spacephobia is a fear of ______,Outer space,Science & Nature,How many large holes are in your head?,Seven,General,Which period followed Picasso's Blue period,Rose + History & Holidays,This Queen of France was beheaded in 1793.,Marie antoinette,General,Who created the Discworld series of novels,Terry pratchett,Science & Nature,What is a Salamander ?,Amphibian +Music,"Which 2 Musicians Co Wrote & Produced ""Do They Know Its Christmas""",Midge Ure & Bob Geldof,General,What spirit is mixed with ginger beer in a Moscow mule,Vodka,General,Sebhorric Dermatitus Is More Commonly Known Aas What,Dandruff +General,In some religions mistletoe represents God's what,Testicles - balls to you,Sports & Leisure,Which Player Scored The Most Goals In International Football ,Pele ,General,Alcoholics get the DTs what does it stand for,Delirium Tremens +General,What comes in types Rock Ball Greentree Indian Reticulated,Pythons,Sports & Leisure,"Which Ex-Eastenders Actor, Who Is A Massive Arsenal Fan Was the Ghost Writer For David Beckham When He Wrote His Autobiography (Half A Point For Character Name)? ",Tom Watt ( Lofty) , History & Holidays,In which country was the Rosetta Stone found?,Egypt +Food & Drink,Which Nation Eats The Most Chocolate Per Capita (Per Head) ,Switzerland ,General,Which US state is known as the Nutmeg State,Connecticut,Music,"Which Band Sang ""If You Leave"" On The Pretty In Pink Soundtrack",OMD +General,Vasco da Gama rounded the Cape of Good Hope in what year,1498,General,"Which dire straits song tells of 'the monster mash,and most of the taxis,and the whores are only taking calls for cash'",Your latest trick,Music,What Did Adolf Sax Invent,The Saxophone +General,Who 'came whiffling through the tulgey wood',Jabberwock,General,Aphids can give birth how long after being born themselves,10 days,Music,"Which word appears in the title of a Police hit in 1981, a Queen hit in 1986 and a Take That hit in 1992?",Magic +General,La Paz is the capital of what country,Bolivia,General,What is the largest native carnivore in England,Badger,General,"What metal makes up most of earth's centre copper, gold or iron",Iron + History & Holidays,"""In which famous Christmas Song is a snowman pretended to be """"Parsons Brown""""?"" ",Winter Wonderland ,General,Titan is a moon of which planet,Saturn,General,Who was the wife of Moses,Zipporah +General,"What is the dire straits song title for 'here comes johnny singing oldies, goldies ___'",Walk of life, History & Holidays,Which British Comedian Was At No.1 In The UK With The Song 'Tears'' ,Ken Dodd ,General,There are over 1000 recognised slang words for what,Vagina +General,From which country did the original vandals come,Germany,Music,Which Guitarist Used A Sixpence To Play His Instrument?,Brian May,General,Name both the Greek and Roman God of Prophecy and Plagues,Apollo +General,What new york city avenue divides the east side from the west side,Fifth,Entertainment,Who was Chief Marshall of the Mickey Mouse Club?,Walt Disney,Food & Drink,What is bouillon? ,A Type Of Soup  +General,Chiuhauha dogs were originally bred for what,Tasty meat,General,Who plays the part of Rachel in TV's 'Friends',Jennifer aniston,General,Who was the first non head of state to appear on a stamp,Benjamin Franklin +General,When Harrison Ford was The Fugitive who was the lawman,Tommy Lee Jones,General,What is the real name of Thomas Dolby,Thomas robertson,General,Over 2500 people are killed annually from using products intended for ______,Right-handed people +General,What shields the earth from the solar wind,Earth's magnetic field earths,General, Eleutherophobia is a fear of ___________.,Freedom,General,Which Film Won An Oscar For “Best Film” In 1997?,Titanic +General,"Proper term for ""Eskimo.""",Inuit,General,What is a quadriga,Roman 4 horse chariot,General,One thousandth of a second is a_________,Millisecond +General,Who was the defeated Labour Prime Minister in the Israeli General Election of May 1996,Shimon peres, History & Holidays,Who Wrote The 1951 Novel The Catcher in the Rye? ,J.D Salinger ,General,Who was defeated at Mantinea in the Peloponnesian war,Alcibiades +General,What is the state fruit of Louisiana,Strawberry,Music,Which Group Currently Holds The Record Of 134 Weeks In The The Charts In One Year,Oasis / 1996,General,What did eric morley found,Miss world competition +General,In Sanskrit it means House of Snow - what does,Himalayas,General,Emelio Marco Palma was the first to do what in 1978,Born in Antarctica,General,Who was the last emperor of Russia,Nicholas ii +General,A vicious one of these is a series of reactions that compound an initial problem,Circle,General,What african country is home to Air Ivoire,Ivory coast,General,Where was the setting for 'shogun',Japan +Science & Nature,Which Is The Most Infectous Disease ,Measles ,General,"A dressing made with oil, wine, vinegar and seasoning is called what",Vinaigrette,General,What do the initials Bt mean after a surname,Baronet +General,Who wrote the epic poem Samson Agonites,John Milton,General,In Alabama it is illegal to be what while driving,Blindfolded,Music,Who Has Released An Album In 2008 Called “The Music Of Spheres”?,Mike Oldfield +General,What is the slogan on license plates manufactured by prisoners in the state prison in Concord,Live Free or Die,General,It is illegal to pawn what in Las Vegas,Your Dentures,General,Former baseball star chuck connors hits a bull's-eye with adult-western,Rifleman +General,The locals call it Firenze what do we call it,Florence Italy,General,Luke wrote two Bible books Luke and what,Acts,Science & Nature,What is the meaning of the name of the constellation Lacerta ?,Lizard +General,In Old English what kind of person often had a 'shite',Gossip - phrase Chit Chat from it,General,"In Greek mythology, who did minos hire to construct the labyrinth",Daedalus,Science & Nature," The __________ eagle, swooping at better than 100 miles per hour, can brake to a halt in 20 feet.",African +General,Pan is the greek god of ______,Shepherds and flocks,Sports & Leisure,"In which sport is the term ""wishbone"" used?",Football,General,Edgar allen poe wrote a famous poem about which animal,Raven +Science & Nature,"What fruit is ""Citrus grandis""",Grapefruit,General,About one-tenth of the earth's surface is permanently covered with ___,Ice,Music,Where did the Beatles stay on their first trip to Hamburg?,A Movie Theatre +General,What is the most common breeding bird in the US,Red Winged Blackbird,General,What is a pugilist?,Boxer,General,Jacque Cousteau's ship Calypso used to be what before he got it,Minesweeper +General,Where is poet's corner,Westminster abbey,General,What is the mathematical diagram in which sets are represented by overlapping circles,Venn,General,1 in 20 children born in US today will do what,Serve time in prison + History & Holidays,Who Succceeded Hitler In 1945? ,Admiral Donitz ,Sports & Leisure,In which city is the Hockey Hall of Fame located,Toronto,General,What was the composer Dvorak's christian name,Antonin +General,What '27 baseball team had a crew of heavy hitters called murderer's row,New,General,Whose first novel was 'Saturday Night and Sunday Morning',Alan sillitoe,Geography,What is the capital of Grenada,Saint george's +Science & Nature,The rate of change of velocity is known as _________.,Acceleration,Sports & Leisure,"Ian Rush said (If I don't drink my milk, I'll only be good enough to play for) which football team? ",Accrington Stanley ,General,Which English king did Robert the Bruce defeat at Bannockburn,Edward ii +Sports & Leisure,Which football team's home ground is Ibrox Stadium? ,Rangers Football Club ,Toys & Games,"In which sport are terms ""spare"" and ""gutter"" used",Bowling,General,Noah's Ark had two of everything including what feature,Windows +Science & Nature,Some animals spend the winter in a sleep_like state known as _________.,Hibernation,General,What were early diaphragms IUD - Dutch cap made from,Orange skin - Half an orange,General,Why does a cynophobe fear,Dogs +General,What is the only flag permitted to be flown over the US flag,United Nations Flag,Music,Which Artist Spent The Most Weeks In The Charts In The 1950's,Elvis Presley,General,Pungent crystalline substance used in medicine and mothballs,Camphor +General,Off what country lies the island of Zanzibar,Tanzania,Entertainment,What famous classical composer continued to compose great music after becoming deaf?,Ludwig van Beethoven,Science & Nature,What name is given to a young frog ?,Tadpole + History & Holidays,"Ex tv detective, turned crooner. Who sang Silver Lady? ",David Soul ,General,In 1776 the first union went on strike in the US what job,Journeyman Printers,General,In 1955 Production announced-first pilot plant to produce man-made,Diamonds +General,Beverly hills has what fictional zipcode,90210,General,In which Olympics did Mark Spitz win seven gold medals,1972, History & Holidays,"Which actor best known for his horror films, did the narration on Micheal Jackson's hit 'Thriller' ",Vincent Price  +General,In Ren and Stimpy what sort of dog is Ren,Chiuhauha,People & Places,Which Zoologist Wrote The Book Man Watching ,Demond Morris ,Sports & Leisure,Which sport allows substitutions without stoppage in play?,Hockey +General,"Who provided the voice for the independent Eilonwy in Disney's often criticized 1985 movie ""The Black Cauldron""?",Susan Sheridan,General,Who is the central figure in Peter C Newmans 'The Establishment Man',Conrad black,General,Collective nouns - A shiver of what,Sharks +General,"What material features in the construction of a ""corduroy road""",Logs,Music,Who Was Known As The Father Of The Waltz,Johann Strauss Snr,General,Which side of a book are the even numbered pages usually on,Left +General,This queen of France was beheaded in 1793,Marie antoinette,Music,How Many Crotchets Do You Get In A Minim,2,General,Micky Dolenz Of The Monkees Briefly Provided The Voice Of Which Famous Cartoon Character,Scooby Doo +General,What is a group of this animal called: Viper,Nest,General,What is the fear of the color white known as,Leukophobia,General,Who wrote the 1958 play Five Finger Exercise,Peter shaffer +General,In the 1954 film A Star is Born starring Judy Garland who played the leading man,James mason,General,What are the only three nations with Jewish populations over one million,"Israel, russia, u.s. israel, russia & us",Food & Drink, Where is the best brandy bottled,Cognac +General,What is a somnambulist,Sleepwalker,General,What is the area of water between alaska and russia,Bering strait,Music,Who won best international female artist at this years Brit awards?,Gwen Stafani +General,Close encounters of the first kind,Sighting unexplained craft sighting ufo's,General,What is the square root of 9801,Ninety nine,General,In Wisconsin by law you must carry fire insurance on what,A Jet Ski +Science & Nature,"On Borneo and Sumatra, the literal translation of this ape's name means ""man of the forest.""",Orang-utan,People & Places,Who Founded the Salvation Army ? ,Wiliam Booth ,General,Who rode a horse called Aethenoth,Lady Godiva +General,What film won the 1943 Oscar as best film,Casablanca,General,Small flat emblem worn as a sign of office,Badge,General,First comic book character to return from death by demand is?,Joker in Batman +Music,Who Immortalised The 1969 Woodstock Festival In Song,Joni Mitchell,Music,Who Were The Beatles Accused Of Snubbing In Manilla In 1966,Imelda Marcos,Technology & Video Games,What item's sound effect was removed from Smash Brothers when it was ported from Japan to the United States ,The Beam Sword +Food & Drink,Caviar Is A Delicacy That Comes From Which Fish ,Sturgeon ,General,In Which US State Was Michael Jackson Born,Indiana,General,"Christine Child won six British titles in the 1970s, in which sport",Judo +General,Barkley name the only animal whose main source of food is the porcupine,Fisher,General,In Iowa pouring what down a pub drain with cop there is illegal,Water it becomes an illegal alcohol,General,What country used the deadly nerve gas SARIN against Kurdish minority factions in the 1990s,Iraq +General,Name the first African American doll produced by Mattel,Francie,General,Tyrannophobia is the fear of _____,Tyrants,General,What was the first product to have a barcode,Wrigley's gum +General,"Which Latin Word Is Used To Express The Meaning ""Word For Word""",Verbatim,General,What does a drosomoter measure,Dew,General,What bird lays the largest clutch of eggs,The Grey Partridge – up to 16 +Geography,"Though part of the British Isles, the _______________ is administered according to its own laws by the Court of Tynwald. The island is not bound by British law unless it chooses to be.",Isle of man,General,Which 50's Actress was born Vera Jayne Palmer,Jane mansfield,General,In what city does a certain church forbid burping or sneezing,"Omaha," +Sports & Leisure,At Which Course Is The Derby Run Each Year? ,Epsom ,General,Name the Honolulu detective whose favourite foe was 'Wo Fat',Steve mcgarrett,General,The Word Emmet Is An Archaic Term For Which Creature,An Ant +General,What is the fictitious name of a defendent,Richard roe,Technology & Video Games,What was the first game created by Rare? ,Slalom,General,What did Teddy Roosevelt ban from the White House,Christmas trees + History & Holidays,Which Of Santa's Reindeer Shares It's Name With A High Street Store ,Comet , Language,Mardi Gras is French for ___________.,Fat tuesday,Music,Who introduced a Disco Duck?,Rick Dees and His Band of Idiots +Tech & Video Games,On what non-Nintendo console can you find Zelda games? ,Philips CD-I,Science & Nature," The Nile crocodile averages about 45 years in the wild, and may live up to 80 years in __________",Captivity,General,"Whose wife was roxana, his horse bacephalus",Alexander the great +Music,Which Song Recorded By Whitney Houston In 1993 Was Originally A Hit For Chaka Khan In 1978?,I'm Every Woman,General,What country consumes the most calories per capita,Ireland,General,Levophobia is the fear of,Things to the left side of the body +Tech & Video Games,What is IRC an acronym for?,Internet relay chat,Music,Who Was The First Member Of Take That To Have A Solo No 1 Hit?,Gary Barlow,General,"To what instrument family do ""french horns"" belong",Brass +General,What is the honeymoon capital of the world,Niagara falls,General,Who is the male lead in the film 'volcano',Tommy lee jones,General,What used to be called (in Europe) Arabian wine,Coffee +Science & Nature,In which county is the UK's Met Office? ,Devon ,General,In which country was Graham Greene's novel 'A Burnt Out Case' set,Belgian congo,General,What is a group of buffalo,Gang +General,Where would you see a stoop or what creature is doing it,A falcons diving,General,What is the Capital of: Grenada,Saint george's,General,Which arabic country has the biggest proportion of Christians?,Lebanon +General,After whom is the month of January named,Janus,General,What did Moldavia & Walachia unite to become,Romania,General,Who is the only real person to ever have been the head on a Pez dispenser?,Betsy Ross + Geography,This is the only borough of New York City that is not on an island.,The Bronx,General,What was KFCs Colonel Sanders first name,Harland,General,What is 'au courant' in english,Well informed +General,What are hiragana and katakana,Japanese alphabets,General,Who wrote the lyrics for Oscar winning song Whole New World,Tim Rice,Food & Drink,What Bird Is Traditionally Eaten For Thanksgiving ,Turkey  +General,Pavarotti popularized Nessun dorma but what does it mean,None shall sleep,General,What did Tantalus serve to the Gods that caused his punishment,His dismembered son Pelops,General,Long tubular Australian Aboriginal musical instrument,Didgeridoo +General,Which companies logo is based on the legend of cats nine lives,Ever Ready,General,"What was the tv name of the one-armed man who pursued ""the fugitive""",Fred,Religion & Mythology,Who is the greek equivalent of the roman god Mars,Ares +General,What do insects do through their spiracles,Breathe, Geography,Which country has the most emigrants?,Mexico,Sports & Leisure,Which league club did Ali McCoist first play for? ,St Johnstone  +Music,Which Members Of The Bee Gees Were Twins,Maurice & Robin,General,What is 'guacamole',Mexican avocado dip,General,Which of the halogens is liquid at room temperature,Bromine +General,What natural product is petrol refined from,Oil,Food & Drink,What is taramasalata made from? ,Smoked cod's roe ,General,Who were the first club to be knocked out of the FA Cup on penalties?,Scunthorpe United +General,What is a chemically castrated cock called,Capon,General,Which British Prime Minister was born in Canada,Andrew bonar law,General,The science of preparing and dispensing drugs is ________.,Pharmacy +General,"What's the international radio code word for the letter ""U""",Uniform,Science & Nature," The striped __________ can fire its musk stream accurately for up to 12 feet, and even farther with a cooperative downwind.",Skunk,General,"Material world Where does the Dicken's story ""A CHRISTMAS CAROL"" take place",England +General,What is the effect of the earth's rotation on the wind called,Coriolis,General,Who won four consecutive Belgian grand prix victories beginning in 1962,Jim clark,General,"Acute, highly contagious viral disease, often fatal, that appears to have been completely eradicated",Smallpox +General,Where in London was the Great Exhibition of 1851 held,Hyde park,General,What is linda mccartney's maiden name,Eastman,General,"Callisto, lo and Europa are moons of which planet",Jupiter +Science & Nature,Where are there over 58 million dogs?,USA,General,Admiral Horatio Nelson suffered from what common condition,Seasickness,Entertainment,What is Batman's butler Alfred's last name.,Pennyworth +General,"In the parable of the Good Samaritan, to which city was the Samaritan travelling",Jericho,Religion & Mythology,"In Greek mythology, who was the beautiful young man Echo fell in love with?",Narcissus,General,Who is Ivanhoe's wife,Rowena +Music,"The Single ""Up Where We Belong"" Is Taken From Which Film",An Officer And A Gentleman,Music,What Was The Name Of The Club At Richmond's Station Hotel Where The Stones Really Came To Prominence,The Crawdaddy,Music,Which Actress Was The Inspiration For Elton Johns Song Candle In The Wind,Marilyn Monroe +General,The Monument In The City Of London Is A Monument To Which Event,The Great Fire Of London,Music,In What Year Did Madonna Marry Sean Penn,1985,Science & Nature,Which meteor shower occurs on the 21st October ?,Orionids +General,Which children's character was created by Mary Tourtel,Rupert the Bear,Music,Which Scot Became A Modern Girl In 1980,Sheena Easton,General,N'Djamena is the capital of which African country,Chad +General,In which country is milk the most popular beverage?,USA,General,"Julias Caesar & Cleopatra Had A Child Together The Name Of Which Can Also Be Encountered In The Medical Profession, What Is It?",Caesarion,General,Which car company makes the Xsara,Citroen +General,It is illegal to cross the Iowa state boundaries wearing what,Duck on your head,Food & Drink,"Which familiar carbonated soft drink contains quinine, a fact which influenced the name it was given? ",Tonic Water ,General,Who played romeo in the 1996 release of romeo and juliet,Leonardo di caprio +General,The worlds first what opened in Brighton England in 1897,Petrol (gas) station,General,"Who used 8,000 different words in his poem, ""Paradise Lost""",John milton,General,In UK its 10 USA its 8 Continental Europe its 38 what is,Women's dress size +Mathematics,What is the maximum number of integer degrees in a reflex angle?,359,Science & Nature,This fingerlike projection is attached to the large intestine.,Appendix,General,Who played hopalong cassidy,William boyd +General,"What is striped on a tiger, besides it's fur",It's skin,Sports & Leisure,Which Scottish Football Club Is Named After An Irish Monk? ,St Mirren ,General,"A step that rocks from one foot to the other, usually in ¾ time.",Balancé +General,What sank German submarine U120 in WW2,Broken toilet,Music,"Who Fell In Love With ""My Little China Girl""",David Bowie,General,What dog is named after a mexican state,Chihuahua +General,US Pres mom said Looking at my children wish I'd stayed virgin,Jimmy Carter,General,What is a group of bison,Herd,Music,"What day of the week is mentioned in The Beatles Song ""I Am The Walrus""?",Tuesday +General,What nationality was the artist Whistler,American,General,"In alphabet radio code, what word is used for 't'",Tango,General,"Who recorded ""blueberry hill"" in 1956",Fats domino +General,In 1961 French army revolts in,Algeria,General,Two short words are combined to give the name of which small stand with several shelves or layers for displaying ornaments,Whatnot,General,What is the most famous song to be re-recorded by the same artist,Candle in +General,Fiochetti is what shaped pasta,Bows,Music,"Which Team Later Married , Wrote ""Ain't No Mountain High Enough"" , ""Reach Out And Touch"" & ""You're All I Need To Get By""",Nickolas Ashford & Valerie Simpson,General,Hockey the vancouver _______,Canucks +General,The electric light first available product what's second,Electric Oven,General,"What are Blenheim, Lord Derby and Peasgood?",Apples,General,"Nutagak, perksertok and pokaktok are Eskimo words for what ?",Snow +General,What do rabbits love,Licorice,Geography,Twenty_three states in the U.S. border an ____________,Ocean,Music,Little Richard Recorded Two Top 20 Hits In The 60's Name One Of Them,He Got What He Wanted / Bama Lama Bama Loo +General,The Germans name for their country,Deutschland,General,"In showjumping, how many points are incurred for knocking down a fence",Four,General,Which American Crooner sang the theme to the TV show `The Love Boat' ,Jack Jones  +General,"He said 'i have nothing to offer but blood, tears, toil and sweat'",Winston, History & Holidays,Who Is The Father Of Queen Elizabeth 2nd ,George VI ,General,The first car with a non_U.S. nameplate to be classified as U.S. domestic.,Mazda mx_6 +General,In which novel by George Eliot is Eppie Cass adopted by a miser whose gold has been stolen by her father,Silas marner,Science & Nature,"Two 1.5 volt batteries, when connected in series, produces _ volts.",3,General,From whom did rocky first win the boxing championship,Apollo creed +General,Where in Huddersfield was the Rugby League formed in 1895,The george hotel,Food & Drink,What Is Scrumpy ,Cider ,General,"In Greek mythology, who was medea's husband",Jason +General,How was William Huskinson killed in 1830 - first ever,Run over by Railway Train,General,Ferrite is a form of which metal,Iron,General,Which group wished it could be Christmas every day,Wizard +General,"Weapon consisting of a long, sharp edged or pointed blade fixed in a hilt (a handle that usually has a protective guard at the place where the handle joins the blade)",Sword,Sports & Leisure,Who was the last Briton to win the men's singles at Wimbledon?,Fred Perry,General,"Which British actor, famous for roles in horror films, was the cousin of author lan Fleming",Christopher lee + History & Holidays,"In What Year Was Robert Kennedy Assassinated? (2 Points Spot On, 1 Point Year Either Way) ",1968 ,General,Which poet wrote Ode to a Skylark,Shelley,General,What exactly are chitterlings,Fried animals birds small intestines +General,What was banned in Horneytown North Carolina,Massage Parlours,General,1727 Helen Morris put in asylum for putting what in a newspaper,Lonely Hearts Advert,Science & Nature,What are animals called if they are abl to live on land or in water ,Amphibians  + History & Holidays,Which group had a one hit wonder with 'Book of Love''? ,The Monotones ,General,In West Virginia its illegal to snooze where,On a train,General,Name Hercule Poirot's valet,George +General,What is the word for hallucinations and delusions,Schizophrenia, Geography,What is the fifth largest country in the world?,Brazil,General,The Gatun Lake and the Gaillard Cut are found on which waterway?,Panama Canal +Music,Which British Sax Player Fronted The Band Paraphernalia,Barbara Thompson,Sports & Leisure,Which Team Were The England Cricket Team Playing When Michael Atherton Was Involved In The 'Dirt In The Pocket Scandal''? ,South Africa ,Entertainment,What is the stage name of Greta Gustafson?,Greta Garbo +General,"Which herb is used to make ""Pesto Sauce""",Basil, History & Holidays,What war followed the shot heard round the world ,The war of american independence,General,In what country was the longbow invented,Wales + Geography,New Delhi is the capital of ______?,India,General,"In the tv series 'the brady bunch', what was mike brady's occupation",Architect, History & Holidays,Where is the infamous Lubjanka prison?,Moscow +General,Which fruit is used in the drink cassis,Blackcurrant,General,When Boris Becker Won His First Wimbledon Title Aged Just 17 Who Did He Beat In The Final,Kevin Curran,Music,Name The Instrument Connected With Guy Barker,Trumpet +General,"In Greek mythology, who wanted to remain unmarried until she was defeated in a footrace",Atlanta,General,Britain's most dangerous job used to kill one person every 3 days,Trawlerman,General,"Which Group Of Animals Are Collectively Known As A ""Rafter""",Turkeys +General,The Romans called it Numidia what do we call it today,Algeria, History & Holidays,How many states did richard nixon carry in 1972 ,Forty nine ,General,Name the cocktail which consists of Scotch and Drambuie,Rusty nail +General,What is the name of the dog from the Grinch who stole christmas,Max,Food & Drink,What Is 'SPAM' Short For ,Spiced Ham ,General,Alcohol comes from the Arabic word Al Kohl meaning what,The Essence +General,"Samuel Sewall, John Hathome and William, Stoughton were the presiding judges at which series of 17th Century trials",Salem witch trials,General,The Jordanian city Amman was once called what,Philadelphia,General,"What can be types called chordate, needle and cruciform",Tree Leaves +Music,The Daughter Of Which Tv Game Show Host Had A Hit As A Member Of Toto Coelo With I Eat Cannibals,Bob Holness,Art & Literature,"Who is a successful recording artist, talented landscape artist, and author of children's books?",Ricky Van Shelton,General,"Sang by robert palmer, '______ to love'",Addicted +General,An arenaceous plant grown in what type of soil,Sandy,General,"Terrestrial gastropod mollusk, related to the snail, but with the shell represented by an internal horny plate overlying the respiratory cavity",Slug,General,UK what sized by Grains Peas Singles Doubles Trebles Cobbles,Coal +General,Which is the windy city,Chicago,General,Which is the longest river in the british isles,Shannon,Food & Drink,What Does Demi Sec Mean On A Champagne Bottle ,Sweet  +General,Term for the role the atmosphere plays in insulating and warming the earth's surface,Greenhouse effect,General,Who played the part of Claude Greengrass in Heartbeat,Bill maynard,Music,When Radio 2 Conducted A Poll In 199 To Discover The Best Songs Of The Century Which Was The Only Bob Dylan Song To Make The Top 50,Blowing In The Wind +General,30% of women have done it but only 10% do it regularly - what,Multiple Orgasms,General,Who recorded the Album In Through the Out Door,Led Zeppelin,General,From where does the uvula dangle,Palate +General,What does qb vii refer to in leon uris's title,Queen's bench no 7,General,Who did cassius stab,Julius caesar,Science & Nature,How many moons does Mercury have?,None +General,Evidence of what alternative treatment found in 5300 mummy,Acupuncture,General,What is the Capital of: India,New delhi,General,What nationality was Alfred Hitchcock,British +Science & Nature,"In the early 20th century, rattlesnake venom was used to treat which illness?",Epilepsy,General,Rhypophobia is the fear of,Dirt,General,In which country was Auschwitz,Poland +General,What nhl hockey player was sports illustrated's 'sportsman of the year' for 1970,Bobby orr,General,Mcbricker of what did aristotle say all things were made up,"Air, earth, fire, water",General,"Somali, Balinese and Abyssinian breeds of what",Cat +Art & Literature,What is the name of the main character in Homer's Odyssey?,Odysseus,General,A person in his eighties is called a(n) __________.,Octogenarian,Geography,What is the capital of Marshall Islands,Dalap_uliga_darrit +Music,"Which 1982 Chart Topping Singer Had Earlier Appeared On TV As Alex Haley's Mother In ""Roots The Next Generation"" And As A Jim Jones Cult Member In ""The Guyana Tragedy""",Irene Cara,Geography,This is the residence of English monarchs.,Buckingham palace,Science & Nature,This complex substance makes up all living things.,Protoplasm +General,What does 'cassata' ice cream contain,Fruit and nuts,Science & Nature,What term appies to space devoid of matter ,Vacuum ,General,The St. Valentine's day massacre took place in this city,Chicago +General,Who won the women's heptathlon at Seoul in 1988,Jackie Joyner-Kersey,Music,Which Album Cover Had To Be Reshot Because It Featured Unauthorised Shots Of Various Personalities,Some Girls,General,What does 'entre nous' mean,Between ourselves +General,What's a truffle,Edible fungus,Technology & Video Games,What is Ozzie's hyper sister's name in the original 'All-C Saga' game? ,Treli,General,Who was the actress that played Ferris Bueller's sister?,Jennifer Grey +General,What is the Capital of: Cook Islands,Avarua,General,"From which musical did the gongs ""Spring, spring, spring"" and ""The Lonesome Polecat Lament"" come",Seven brides for seven brothers,General,What computer game featured a disco leftover looking for love?,Leisure Suit Larry +General,Christopher Cockerel invented what,Hovercraft,Sports & Leisure,"In ten_pin bowling, how many points does a perfect game consist of",300,General,In which country is the cheese Tome produced,Denmark +General,Klysmophillia is arousal from what,Enemas,Music,With Which Record Did The Lighthouse Family Enter The Charts At No.6 In 1998,Lost In Space,General,Name the late eighties band that named the sides of their first album Hardware and Software and also used samples from Star Trek movies in their songs.,Information Society +Music,In Which Year Was John Lennon Murdered,1980,Music,"Who Had A Hit In 1983 With ""The Chinese Way""",Level 42,General,What Sort Of Creature Is A Greylag,Goose +General,How many celebrities featured on the panel in each episode of the game show 'Blankety Blank'?,6,Science & Nature,What Is An Anechoic Chamber ,An Acoustic Echo Free Room ,Religion & Mythology,Who is the Norse god of justice,Forseti +General,Who is the Greek Goddess of witchcraft and black magic,Hecate,General,A u-shaped bend in a river is called a(n)___.,Oxbow,General,"What is the ""southern lights"" called",Aurora australis +General,What Was The First Product Available In The UK Under Hire Purchase,A Sewing Machine,General,What was the inexpensive designer watch of choice amongst teenagers during the eighties?,Swatch, History & Holidays,Who Released The 70's Album Entitled All Things Must Pass ,George Harrison  +General,Musophobia is the fear of,Mice,General,Turf Stone and Hedge are all types of what,Mazes,General,The refraction of light by ice crystals causes a what to form round the Sun,Halo +Sports & Leisure,Which Country Won The World Cup First Germany Or Brazil ,Germany ,Music,"Who Recorded The Album ""Shaken & Stirred""",Robert Plant,General,Hirohito ascended to the Japanese throne in which year,1926 +General,Leukophobia is the fear of,The color white,General,In New York by law the death penalty is required for what act,Jumping off a building, History & Holidays,What Was A Ballista? ,"An Ancient Siege Machine, A Giant Catapult Or Crossbow? " +General,Which American poet was also a surgeon,Oliver Wendell Holmes, History & Holidays,What Happened To The Apollo I Spacecraft On 27 January 1967 ,Caught Fire On Launch Pad Killing All 3 Crew ,General,Vikings what was the nickname given to the minnesota vikings' defensive unit,Purple +Science & Nature,What fish is the fastest,Sailfish,Music,What Was the Best Selling Single In The UK In The 20th Century By A Duo?,You're The One That I Want,General,What london landmark has an 11 foot long hand,Big ben + History & Holidays,Holy Roman Emperor Charles VI created which principality in 1719? ,Lichtenstein ,General,Funchal is the principal city of which Portuguese province,Madeira,General,Which group's Best of Album is entitled Like You Do,Lightning seeds +General,What animals make up the Suidae family,Pigs,Music,"What Was Snaps Follow Up Single To ""Rhythm Is A Dancer""",Exterminate,General,"If bats are nocturnal and horses diurnal, then coyotes and others animals that roam at dawn and the twilight hours are called?",Crepuscular +Music,What Stevie Wonder song was recorded by 'Beck Bogart and Appice'?,Superstition,General,John Quincy Adams was the only US president to do what,Marry a non American woman, History & Holidays,"In 1972 which flash new motor was advertised with the slogan, ` The car you always promised yourself'? ",Ford Capri  +Science & Nature,What was created with the big bang?,Universe,General,An animal stuffer is a(n) ___________.,Taxidermist,Music,Who keeps a ten bob note up his nose?,Mean Mr. Mustard +General,What was the Rolling Stones first no 1 hit,Its all over now,Music,Johnny Mathis Was Once A World Class Athlete In Which Event,High Jump,General,Of which academy is a goat the mascot,U.s naval academy +General,Alberto Tomba is a name associated with which sport,Skiing,General,Whose name did god change to israel,Jacob,General,Who was the last British king born outside the UK,George II - Hanover +General,What colour was Tweety Bird originally,Pink,General,The weight at the end of a pendulum is a(n) ______.,Bob,General,What is the Capital of: Sri Lanka,Colombo +Sports & Leisure,In April 2003 Who Became The Oldest Man To Be Rated No1 In The World Tennis Association Rankings At The Age Of 33? ,Andre Agassi ,General,What is the flower that stands for: precaution,Golden rod,Science & Nature,Which Is The Only Chemical Element With A Three Letter Name? ,Tin  +Geography,"The muskellunge, a fierce fighting fish that can weigh in at around 70 pounds, is the official state fish of ___________________",Wisconsin,General,What is an australian bandit also known as,Bushranger,Entertainment,Who starred in 'Conan The Barbarian'?,Arnold Schwarzenegger +General,Who is the babylonian goddess of love and fertility,Ishtar,General,Rabbits like _______,Licorice, History & Holidays,"Charlie Chaplin Died On Christmas Day In 1977, How Old Was He When He Died ",88  +General,Belgrade lies on The Danube and which other river,Sava,Art & Literature,Of Which Famous London Landmark Was Sir Alfred Gilbert The Sculptor ,Eros ,Music,"John Parr Sang About What ""Fire"" From The Film Of The Same Name",St Elmo's Fire +General,Who was the king of Judah (800-783 bc),Amaziah,General,Who succeeded Hitler in 1945,Admiral donitz,General,D D Palmer was the worlds first what,Chiropractor – Osteopath +General,Collective nouns - a group of swans are called what,A Bevy,Food & Drink, From which fruit is the liqueur Kirsh made,Cherry,General,What is the ball on top of a flagpole called,Truck +Toys & Games,In poker five cards of the same suit is called a(n) ________.,Flush,General,Venus Observa is the technical term for what,Missionary position,General,How is abba calling for help,Sos +General,How many bones are there in the human body,206,Food & Drink,"Unlike other oranges, what does a navel orange not have ",Seeds ,General,"What was the original title of the movie ""A Hard Days Night""",Beatlemania +General,What dog in ancient China was restricted to the aristocracy,Pekinese,General,What kind of skiing held its first world championship in 1979,Grass skiing,General,Who wrote the autobiographical book My Family and Other Animals in 1956,Gerald durrell +People & Places,Who won a place in the Guinness Book of Records for writing 26 books in 1983? ,Barbara Cartland ,General,"In Which Book Of The Bible Will You Learn Of The Number Of The Beast ""666""",The Book Of Revelations,General,Name the treaty of 1929 which recognised Papal authority within the Vatican City,Lateran treaty +Science & Nature,"What has the chemical formula H2, SO4? ",Sulphuric Acid ,General,Crispies driving: what country is identified by the letter c,Cuba,General,Who was the oldest man in the bible?,Methuselah +General,What is the name of the Israeli national anthem,Hatikvah,General,What is the fear of illness known as,Nosemaphobia,General,Logizomechanophobia is a fear of ______,Computers +General,In the middle of the land is the literal translation of where,Mediterranean sea,Entertainment,"His films include: Giant, Written on the Wind, and A Farewell to Arms.",Rock Hudson,General,What is the science pertaining to the earth's interior heat,Geothermics +Music,"Who Recorded the Album ""Alright Now"" - ""Change"" & ""Heartache""",Pepsi & Shirlie,Sports & Leisure,"At what age can a player join the Seniors Golf Tour 45, 50 or 55? ",50 ,General,Flagg canadian: wild hairy monster of indian lore,Sasquatch +General,Which famous piece of artwork depcits the Battle of Hastings ?,Bayeux Tapestry,General,Silverwood Michigan its illegal to kill what using your hands,A Bear to impress a girl,General,Whats the worlds largest coral reef,Great barrier reef +General,Who was the world's first woman Prime Minister,Sirimavo bandaranaike,People & Places,Who Or What Is The Actress Uma Thurman Named After ,A Hindu Goddess ,General,Who played bass guitar in Suzi Quarto's group,Suzi Quatro +General,In what opera did count almaviva have a page named cherubino,Marriage of,General,"Two South American countries have no coastline, Bolivia is one what is the other",Paraguay, History & Holidays,Who did Henry VIII marry when he was 18?,Catharine of Aragon +General,"Who did arthur h bremer try to assassinate on may 13, 1972",George wallace,General,Who rode a horse called Bucephalus,Alexander the Great,Music,What Type Of Concerto Did The Toys Sing In 1965,A Lovers Concerto +Geography,"A distillery was originally on the site of America's first mint, the ____________ mint, which opened in 1792.",Philadelphia,General,The code name for the allied invasion of Italy in WW II was operation _______,Avalanche,General,Complete the name of the 1970s group Sutherland Brothers and ___.,Quiver +General,"In ""St. Elmo's Fire,"" What city does Billy go to at the end???",New York,General,What book did forrest gump keep in his suitcase,Curious george,General,"What is the English title of the German opera, 'Die lustigen Weiber von Windsor'",The merry wives of windsor +General,What is the principal religion in Romania,Orthodox,Music,"""Damon Albarn, Alex James, Graham Coxon & Dave Rowntree"" Were All members Of Which Band",Blur,Geography,In which country is the dalai lama's palace ,Tibet  + Geography,What is the capital of Colorado?,Denver,General,Name the Greek national airline,Olympic Airways,General,What zone lies between the tropics of Capricorn & Cancer,Tropical zone +General,From where to london was the first commercial boeing 747 flight,New york,General,After English what's the most widely used language on the net,German,General,Who wrote the music for Local Hero,Mark knopfler + History & Holidays,What was the last chinese dynasty?,Manchu,General,What is saltimbocca,An italian dish of ham & veal,General,The filaments for the first electric lamp were made from what,Bamboo +General,A light aircraft without an engine,Glider,Music,"Who Sang The Song ""Danger Zone"" From The Movie Top Gun",Kenny Logins,General,Who was the author of the series of novels referred to as the Raj Quartet,Paul scott + Language,The Scots call it 'shinty' - what do Canadians and Americans call it?,Hockey,General,Water found below the surface of the land,Groundwater,General,Seattle Rome Edinburgh Sheffield what links them,Built on seven hills +General,What is the binary equivalent of decimal 10,1010,General,"In which American state are Toledo, Cincinnati and Dayton",Ohio,General,In 1846 American inventor Elias Howe patented what type of machine,Sewing machine +Music,Who Was Bound For Delaware In 1960,Perry Como,Music,Who Changed His Name From Maurice Cole To Become One Of The UK's Famous DJ's,Kenny Everett,General,What was the name of the Royal Navy's first nuclear submarine,H m s dreadnought +General,What is the flower that stands for: domestic industry,Flax,General,In literature who taught at the Marcia Blain school for Girls,Miss Jean Brodie,General,What culture introduced hats and crackers at Xmas season,Ancient Rome +General,What snake builds a nest,King Cobra,General,The first double_decker bus was introduced in this city.,London,General,What were dachshunds bred to hunt,Badgers +General,What type of food is Lollo rosso?,Lettuce,Technology & Video Games,"Who is the developer at Nintendo responsible for classics like Donkey Kong, Super Mario Bros., and The Legend of Zelda? ",Shigeru Miyamoto,Music,"Who Had A UK No.1 Album With ""Number Of The Beast"" In 1982",Iron Maiden +General,In which of Charles Dickens' novels would you find the character Dora Spenlow,David copperfield,Music,Who Did The Wonder Stuff Serve As The Backing Group For In The 90's,Vic Reeves,General,"What is the term for the study of friction, lubrication and wear",Tribology +General,Who was Hitler's foreign secretary (full name),Joachim von ribbentrop,General,My Name is Mud' was on pork soda released in 1993 by which group,Primus,Geography,The northernmost U.S. state capital is ________________,"Juneau, alaska" +General,What sport exercises all the muscles at once,Swimming,General,What is the fear of paper known as,Papyrophobia,General,The Raven called 'Grip' appears in which novel by Charles Dickens?,Barnaby Rudge +General,Trimontaine was the original name of where,Boston Massachusetts,Science & Nature, The average adult __________ weighs 21 pounds.,Raccoon, Geography,Name the three baltic countries?,"Estonia, Latvia, Lithuania" +General,Nomatophobia is the fear of,Names,General,Name the only country with a national dog,Holland,General,The word Mongol means what in Mongolian,Brave +General,The Detours changed to The High Numbers then what name,The Who,General,Harry Longabaugh A Very Famous Person In History & The Subject Of Many Movies How Is He More Commonly Known,The Sundance Kid,General,The u.s minted a 1787 copper coin with what tongue-in-cheek motto,Mind your +General,In the proverb Heaven protects children sailors and who,Drunken men,General,What are the Boyoma and Tugela,Waterfalls,Geography,"There are four mountain ranges in New York State: Adirondack, Catskill, Shawangunk, and __________",Taconic +General,"What Las Vegas hotel burned in November, 1980, with the loss of 84 lives",Mgm grand,General,"Proverbially, what is rubbed into the wound to make things worse",Salt,Music,Which Song Is The Theme From Midnight Cowboy,Everybody's Talking +Science & Nature,What is another name for the coyote?,Prairie wolf, Geography,What is the capital of Romania ?,Bucharest,General,"Of which country does the group of about 100 islands, known variously as the Spice Islands, the Moluccas, Maluku and PulaU.S.eribu, form part",Indonesia +General,What is the point in the moon's orbit which is farthest from the earth,Apogee,General,Who was the first rock band to perform at NY Opera house,The Who, Geography,What is the capital of Pennsylvania?,Harrisburg +Sports & Leisure,"Which Sport Featured In The Olympics Will You Find The Terms 'Bump, Set, Spike & Pancake'' ",Volleyball ,Music,From which 1951 musical is the song I'm On My Way?,Paint Your Wagon,Science & Nature,In What Conditions Do Thermophilous Plants Thrive ,Warm Or Sunny  +General,Who's band was The Quarrymen,John Lenon,General,In what famous poem does killing an albatross cause disaster,Rime of the,General,In which country is Mount Aspiring National Park,New Zealand (South Island) +General,On which annual day do most heart attacks occur,New years day,People & Places,By what name is Marion Morrison better known as?,John Wayne,General,What era were the first traces of land life believed to appeared,Paleozoic +General,Brooks what is the new name of the mound metalcraft company,Tonka metalcraft,Science & Nature," An __________ can go through 2,000 to 3,000 teeth in a lifetime.",Alligator,Science & Nature,Which Substance In The Skin Filters Out Harmful Rays From The Sun ,Melanin  +Art & Literature,"""Our Town"" is a play by whom?",Thornton Wilder, History & Holidays,What did the license plate on the Delorean in Back To The Future spell out? ,OUTATIME ,General,What is the tallest and thickest kind of grass,Bamboo +General,In the UK which school choir had a No 1 with a song grandma,Saint Winifred's,Science & Nature, The electric organs in an electric eel make up four_fifths of its __________,Body,General,Andrew Patterson wrote which definitive Australian song,Waltzing Matilda +Music,"What was the original working title of the Beatles movie ""Help""?",Eight Arms To Hold You,Sports & Leisure,In what sport do you find 'coursing'?,Greyhound racing, History & Holidays,"Which fictional character created by Steve Coogan, is a presenter for Radio Norwich ",Alan Partridge  +Art & Literature,Which Poet Wrote No Verse During His Time As Poet Laureate ,William Wordsworth ,General,What Roman Emperor was killed by an overdose of laxative,Nero - by an aunt,Science & Nature,What Is Sodium Carbonate Better Known As ,Washing Soda  +Entertainment,Who is Tippi Hedren's daughter?,Melanie Griffith,Science & Nature," When cows graze in their natural head_down position, their saliva production increases by __________",17%,General,What is your job if you have the title Imam,Muslim leader +Food & Drink,Who released the following 'edible' album 'The spaghetti incident' ,Guns & Roses ,Science & Nature,Who spoke the first recorded message?,Thomas Edison,Music,"Johnny Hates Jazz Sang ""Shattered Dreams"" Or Was It ""Dreaming Of You""",Shattered Dreams + Geography,What place is known as 'the land nowhere near'?,Cape Three Points,General,What is the worlds fastest moving insect,Tropical Cockroach,Music,From which Aldous Huxley book did the Doors take the inspiration for their name?,The Doors Of Perception +General,The ancient Egyptians worshiped a sky Goddess name her,Nut,Entertainment,What was Kevin Bacon's first big hit?,Footloose,General,What's the name of the dragon in the Ivor the Engine stories,Idris +Music,"Name The Band: Tony Mortimer, Brian Harvey, John Hendy, Terry Coldwell",East 17,General,Eisenhower trophy is given annually to what best amateur team,Golf,General,What got named by novelist Gilbert Frankau at a party in 1926,Zip he said Zip its open Zip its shut +General,"Which word, taken from the French, translates literally as 'rotten pot'",Potpourri,General,What is the zodiacal symbol for capricorn,Goat,Food & Drink,From What Is Sake Made ,Rice  +General,Where would you find your pollers,Hands its your thumbs, Geography,What is the basic unit of currency for Eritrea ?,Nakfa,General,What did Gabriel Fahrenheit invent,Thermometer +General,Puccini's Turendot is set in which country,China,General,What was the title of Agatha Christie's first Hercule Poirot novel,The mysterious affair at styles,General,Pidgin English started because of trade between UK and where,China +General,What is a sternocleidomastoid,A muscle,General,What is a group of cattle,Herd,General,An Ortaline is a cross between what two items,Tangerine - Orange +General,The flowers of the curry plant are what colour,Yellow,Sports & Leisure,How Many Times Do Entrants Hurdle The Water Jump In The Steeplechase ,7 Times ,Music,"Which pop group of the 90’s were made up of John Hendry, Tony Mortimer, Brian Harvey and Terry Caldwell",East 17 +General,"Which English theologian, who became Dean of Westminster, was the first person to write a full account of a fossil dinosaur?",William Buckland,General,The San Andreas is what type of geological fault,Conservative,General,What vitamin is sometimes called the 'sunshine vitamin'>,Vitamin d +General,"Which Argentinian golfer, aged 44,won the British Open in 1967",Roberto de vicenzo,General,Rubens is considered to be the supreme master of which style of painting,Baroque,General,In which film did Roger Moore first appear as James Bond,Live and let die +Sports & Leisure,Which Olympic Sport Prohibits The Wearing Of A Beard ,Boxing ,General,"All gondolas in venice, italy must be painted what color, unless they belong to a high official",Black,General,Chi is the Chinese year of what,Cock +Music,Which Band Produced the Mega Selling Album “Rumours”,Fleetwood Mac,General,Which children's author wrote the autobiographies 'Boy' and 'Solo'?,Roald Dahl,Music,Reaching No.2 In 1970 Which Record Set T-Rex On The Road To 70's Super Stardom,Ride A White Swan +Music,Ian Brown Fronts Which Band,The Stone Roses,Music,"The Film ""Mermaids"" Featured A Song Called ""It's In His Kiss"" (The Shoop Shoop Song) By Which Singer",Cher,Geography,Which Country Is The Largest ,Russia  +Food & Drink,"Often eaten for breakfast, bacon is actually the flesh of what barnyard animal?",Pig,Sports & Leisure,What do the letters ERA mean in baseball,Earned run average,General,Who said 'Marriage is a wonderful invention but so is the bicycle repair kit,Laurence olivier +General,"In Growing Pains,What was boners dad's name?",Sylvester Stabone,General,Which economist wrote 'The Affluent Society',J k galbraith,Entertainment,What is the term used for 'slowly' in music?,Lento +General,Who plays centerfield for the Seattle Mariners?,Mike Cameron,Geography,"____________ is derived from the Indian word Bhotanta, meaning ""the edge of Tibet."" It is located in Asia near the southern fringes of the eastern Himalayas.",Bhutan,General,"On which town did Mrs Gaskell base ""Cranford""",Knutsford +General,"Most men do this each morning, using a razor.",Shave, History & Holidays,Sugar and ground almonds are the ingredients for which part of a Christmas cake ,Marzipan ,General,What is or was the capitol of Hong Kong,Victoria +General,Name the classic dish of mussels cooked in white wine with garlic and onion,Moules mariniere,Music,Brian Wilson Was The Mainstay Of Which 60's Legends,The Beach Boys,Science & Nature,What Is The Oxygen Carrying Component Of Blood ,Haemoglobin  +General,"Three_letter clothing outlet, or a space or void.",Gap,Music,"With which European city is Ultravox linked,songwise?",Vienna,General,What country does Paul Hogan come from,Australia +General,Which war was ended by the Treaty of Westphalia,30 years war,General,How many animals are used to designate the years of the Chinese calendar,12,Sports & Leisure,In which sport is the Cy Young Trophy awarded,Baseball +General,"What, made from the dried stamens of cultivated crocus flowers, is the most expensive cooking spice",Saffron,General,What is the flower that stands for: rustic beauty,French honeysuckle,General,Which chemical element is named from the Greek for violent,Iodine +General,Virginia McMath became famous as which actress,Ginger Rodgers,Sports & Leisure,When Commonwealth Gold Medalist Judy Simpson Was On Gladiators What Was Her Characters Name ,Night Shade ,Science & Nature,Whats the chemical symbol for Helium ?,He +General,Benjamin Kubelsky 1894 fame as what comedian,Jack Benny,General,Spirit obtained from petroleum and used as a cleaning agent,Benzine,General,In 1953 what was first successfully transmitted in the USA,Colour Television +General,Of which country is voodoo the national religious folk cult,Haiti,General,1970 who announced he was entering a clinic for a sex change,John Lennon (April fool joke),General,Perry Como the singer once worked as what,A Barber +Music,"Which Punk Band Had A Minor Hit In 1982 With A Cover Of Ralph Mctell's ""The Streets Of London""",The Anti Nowhere League, History & Holidays,In Which Month Of Which Year Was The Six Day War Between The Arabs & The Israelis ,Jun-67 ,Science & Nature," A single __________, with its razor_sharp teeth, is still dangerous enough when out of water to rip off the flesh, or a finger or toe, from an unwary fisherman.",Piranha + Language,What is the official language of Austria?,German, History & Holidays,Ringing a bell on Halloween is said to do what ,Scare Evil spirits ,General,What is max in 'the grinch who stole christmas,Dog + History & Holidays,What Was The Coronation Year Of Queen Elizabeth 2nd ,1952 ,General,Who played the male lead in the 1965 film entitled the war lord?,Charlton heston,General,Whats the smallest breed of dog,Chihuahua +Science & Nature,What is the only animal that cant stick out its tongue? ,Crocodile ,Religion & Mythology,Name the holiest day in the Jewish calendar.,Yom kippur,General,What do you call the cap on a fire hydrant,A Bonnet +General,Who played the lead female in meet me in las vegas,Cyd charisse,General,"Mount Usborne, at 2,312 feet, is the highest peak in which British dependency",Falkland islands,Science & Nature,From What Material Do Wasps Build Their Nests ,Wood Fibre  +General,Who's the leading rebounder in NBA playoff history,Bill russell,Science & Nature,In the 1980's the Australian Government instigated a vasectomy programme for which wild animal? ,Koala Bear ,General,The coast line around this lake in North Dakota is longer than the California coastline along the Pacific Ocean.,Lake Sakakawea +General,What fruit will keep floating to the top and sinking to the bottom of a glass of champagne,Raisin, History & Holidays,"Traditional in Germany at Christmas, what sort of food is stollen? ",Cake ,General,Which book by James Joyce takes palce on a single Dublin day in June 1904,Ulysses +General,The Gettysburg Address was written on what,Used envelopes,General,What is the range of an aim-7 sparrow,28 miles,Science & Nature,What Is The Male Part Of A Flower Called? ,The Stamen  +General,"What is the name of the rabbit in the film, ""Bambi""",Thumper,General,What dance is most associated with Buenos Aires,The tango,General,"Where was the first gold strike in California, setting off the 1849 gold rush",Sutter's mill +Science & Nature,Which British Flower Is Known As The Lent Lily? ,The Daffodil ,Music,"Which Lyricist Worked With Richard Rogers On Such Songs As Blue Moon , Where Or When, & My Funny Valentine",Lorenz Hart,General,Which biscuit is named after a French royal family,Bourbons +General,What is a kookaburra,Bird,General,What is the worlds fifth largest religion,Sikhism,General,Who played louis in 'interview with the vampire',Brad pitt +Music,"Who Wrote The Words & Music For ""Fifty Ways To Leave Your Lover""",Paul Simon, History & Holidays,Name The German Battleship Sunk In A Norwegian Fjord In 1944? ,The Tirpitz ,Entertainment,What was George of the Jungle always running in to,Tree +General,Steve McQueen played Hiltz Great Escape what's first name,Virgil,General,When was the rechargable storage battery invented,1859,General,Phoenix is the capital city of which U.S. state,Arizona +General,What Roman Emperor was the first to convert to Christianity,Constantine - the great,General,Charles Stratton became famous as what circus act,Tom Thumb,Entertainment,Who wrote Tubular Bells?,Mike Oldfield +General,Saturn is the roman god of ______,Agriculture,General,Which book caused a minor controversy in 1997 when a survey of Waterstone's customers voted it the best book of the twentieth century,Lord of the rings,General,Kind of Afro American dance music originally from Southern Louisiana,Zydeco +General,As what is the exclamation point known to mathematicians,Factoria,General,"Which river forms part of the southern boundary of the state of Indiana, separating it from Kentucky",Ohio, History & Holidays,Who Released The 70's Album Entitled Bitches Brew ,Miles Davis  +Science & Nature,From What Is The Heart Drug Digitalis Obtained ,Foxgloves ,Sports & Leisure,In this team sport each player gets a chance to play every position.,Volleyball,General,Which Soviet leader backed down over the Cuban missile crisis in 1962,Kruschchev +General,A carbonade is a dish that must contain what,Beer,General,25% of Americans believe which fictional character is real,Sherlock Holmes,Science & Nature,What Are The Two Female Sex Hormones ,Oestrogen & Progesterone  +General,"What is the meaning of the Latin phrase ""cum grano salis""",With a pinch of salt,General,"What pro athlete did Fortune claim added $10 billion to the U.S. economy, in 1998",Michael jordan, History & Holidays,What group landed in America in 1620,The pilgrim s +General,What is a group of this animal called: Horse,Team pair harras,Geography,How Long Did It Take To Complete The First Circumnavigation Of The Earth By Air ,175 Days ,General,What was the capitol of England before London also a US gun,Winchester +Art & Literature,"Which Thriller Writers Works Include (The Dark Eyes Of London, Four Just Men & Sanders Of The River) ",Edgar Wallace ,General,Which two colours are on a semaphore flag,Red Yellow,General,Which sweet-sounding fungus kills trees,Honey fungus +General,USA favourite computer passwords are love and sex what UK,Fred,General,What is the flower that stands for: cordiality,Peppermint,General,In The African Queen what was the name of the steam launch,The African Queen +Sports & Leisure,What is the heaviest class of weight_lifting,Super heavyweight,Science & Nature,What Is The Method Of Growing Plants Without Soil? ,Hydroponics ,Science & Nature,Which Well Known Video Game Character Features In The Games 'The Tides Of Time'' & 'Defender Of The Future'' ,Ecco The Dolphin  +General,The study of human pre history is ___________,Archaeology,Science & Nature,Which Large Rodents Fur Is Known As Nutria ,Coypu ,General,Whose favourite poodle was called Rufus,Winston Churchill +General,Gemellus is a fancy name for what,Testicles,General,"In a Gilbert and Sullivan opera, who was John Wellington Wells",Sorcerer, Geography,In which state is the Mayo Clinic?,Minnesota +General,"In a church, the area where the transept and the nave intersect, usually emphasized by a dome or a tower. ",Crossing,Entertainment,"Who co-starred with Julie Andrews in ""Mary Poppins""?",Dick Van Dyke,Geography,What is the current name for south_west Africa,Namibia +General,Promise of future benefits e.g. pensions or share options for those who stay with a company,Golden handcuffs,General,Ophidiophobia is the fear of what,Snakes,General,What was the Russian city of St. Petersburg called from 1924 to 1991,Leningrad +Art & Literature,What controversial book did Germaine Greer write?,The Female Eunuch,Science & Nature,Which Is The Largest Species Of Beetle ,The Goliath Beetle ,General,In 1885 Canada sold what to US for $150000,Niagara Falls +General,What is the only planet that is less dense than water,Saturn,General,What city was once called York,Toronto - by British governor 1793,General,Troy was the last Derby winner for which jockey,Willie carson + Geography,What is the capital of Somalia ?,Mogadishu,General,In Which City Were The Most Murders Commited In 2003,Chicago,Geography,The strongest recorded earthquake (8.9) occurred in which country in 1933,Japan +General,What transporter room aboard the enterprise is chief o'brien's favorite,Three,Art & Literature,What Is The Name Of Colin Dexter's Fictional Detective ,Inspector Morse ,General,Which year - Nasser and De Gaulle died and Allende was elected President of Chile,1970 +General,Where would you find a Walloon,South Belgium – Native Flemish,General,What is the most slippery substance known to man,Tufoil,Music,There No.5 Hit Was Also Their Name So Who Was Surrounded By Cardboard In 1987,Living In A Box +General,"What is the nickname for San Antonio, Texas",Alamo city,General,What seductive WW I spy had a daughter named Banda who was also a spy,Mata hari,General,What is on the banks of the river jumna,Taj mahal +People & Places,What Letter Do Most British Names Begin With ,B ,Music,"What Was The Top Selling Album , David Bowie Single, & Box Office Hit In 1986",Absolute Beginners,General,With what instrument is the jazz musician Dexter Gordon associated, tenor saxophone +Music,"Which Peter Was Offered A Record Contract Live On Air After His Version Of ""Don't Be Cruel""",Peter Andre,General,"Who quoted ""Don't count your chickens before they hatch'",Aesop,General,Collective nouns - a sneak of what animals,Weasels +General,In the kama-sutra the art of which game is recommened for women to study ?,Chess,General,"In the rhyme about magpies,'one for sorrow, two for joy', what are 6 magpies for",Gold,Music,"In 2004 The Family Of The Late Johnny Cash Tried To Stop One Of His Songs Being Featured In An Advertisement For Hemorrhoid Cream , Name The Song",The Ring Of Fire +General,In the poem who dug cock robins grave,Owl with his trowel,General,Who was Al Gore's freshman roommate at Harvard?,Tommy Lee Jones,General,Spell the name of the largest city in new mexico,Albuquerque +General,To which plant family (strictly genus) do jonquils and daffodils belong,Narcissus,General,"In Greek mythology, what was attributed to athena",Owl,People & Places,Who Was The Last English Player To Be Sent Off In The World Cup (2006) ,Wayne Rooney  + Geography,What canal connects Lake Ontario and Lake Erie?,Welland,General,Who pushed the Paperwork Reduction Act through Congress,Jimmy carter,General,What does an alopecia sufferer lack,Hair +Science & Nature,What is the process of converting glucose to energy in cells called ?,Respiration,Music,Which opera star was born Helen Porter Mitchell in Melbourne in 1861?,Dame Nellie Melba,General,How did ed mcmahon of the tonite show start his career,Circus clown +General,"Who is the subject of the book ""Longitude"" by Dava Sobel",John harrison,General,Who sat on her tuffet?,Little Miss Muffet,Religion & Mythology,Who is the greek equivalent of the roman god Pluto,Hades +Science & Nature,Who Built The First Tunnel Under The River Thames ,Sir Marc Isambard Kingdon Brunel, History & Holidays,Who plays the lead role of the accident prone Clark Griswold in the 1989 'National Lampoon's Christmas Vacation'' ,Chevy Chase ,Geography,Who renamed the South Sea as the Pacific Ocean in 1520? ,Ferdinand Magellan  +Science & Nature,"In the electomagnetic spectrum, what comes between X-rays and Light?",Ultraviolet light,General,What did you do 10 million times last year,Breathe,General,Who carried on charles babbage's work when he died,His son +Science & Nature,This small gland attached to the brain exerts a control over growth.,Pituitary,General,Who was the first Austrian act to top the U.K singles chart?,Falco,General,The Gulf of Panama leads into which ocean,Pacific +General,What is a group of turtle doves,Pitying,General,Who is the Patron Saint of France,St Denis,General,What is the common name for a five wood in golf,Baffy +Food & Drink,What Fruit Is Used In Making 'Creme de Cassis' ,Blackcurrants ,Religion & Mythology,What is the holy cup of Christ called?,Holy Grail,General,She won the 1979 Nobel peace prize for her work among the poor,Mother teresa +Science & Nature, The tailorbird of Africa makes its nest by sewing together two broad leaves. It uses fiber as the thread and its bill as the __________,Needle,General, A word like 'NASA' formed from the initials of other words is a(n) _________.,Acronym,General,What name did the Standard Oil Co. of California adopt,Esso +General,What is the sacrament of anointing for dying persons,Extreme unction,General,Kolduns popular in Russia as analysts in America what are they,Witches,General,Which playwrite was nicknamed doc,Neil Simon +General,What is a sorcerer who deals in black magic,Necromancer,Science & Nature,What Kind Of Animal Is A Marmoset? ,A Monkey ,General,Lyssophobia is the fear of,Rabies of becoming mad +General,An area of London got its name from a hunting call what,Soho,General,In the Simpsons name the Police Chief,Chester Wiggum,General,In WW2 what was operation dynamo,Evacuation of Dunkirk + History & Holidays,"Which 60's Movie features The Line wise men and grocers, they weigh everything ",Zorba the Greek ,General,What country is Ulan Bator the capital of,Mongolia,General,Who was the Beatles original Bass Player,Stuart Sutcliffe +General,"What Type Of Vegetable Is Used In A ""Dubarry"" Soup",Cauliflower,General,What roars in the 'roaring forties',Wind,General,Fill in the blank: blind as a ___,Bat +General,Americans consume 2 billion lbs of what each year,Chocolate,Sports & Leisure,What's the nickname of the University of Georgia football team,Bulldogs,General,Which country has won the most Nobel Prizes for Peace,U.S.A. +General,Afrikaans developed from which language,Dutch,General,Which TV series is set in the village of Adensfield,Heartbeat,General,Herpetophobia is a fear of ______,Reptiles +General,What city has the longest metro system,London,General,Phyllophobia is the fear of what,Leaves,Music,"With Strings Prominent Which duo Released The Classic ""Reet Petite"" In 1993",Pinky & Perky +General,What was Helen Keller's first word,Water,Toys & Games,What is the best possible score in blackjack?,21,General,What Do You Call A Group Of Snakes,A Nest +Music,"Who Wanted To Take Things ""One Day At A Time"" In 1979",Lena Martell,Geography,Which Bridge Had Van Gogh Just Finished Painting Before He Cut Of His Ear ,The Lanlois Bridge In Arles ,General,What country celebrates its National Day on 29th October?,Turkey +General,Who ate watercress to dissolve gravel and stones in the bladder,North,General,Yoon-Mi Kim Olympic gold aged 13 years 83 days what sport,Short track speedskating,General,What was the name of the pet monkey aboard the ship Venture,Ignatz +General,Nonpariel Mission Caramel Neplus Peerless types of what,Almonds, History & Holidays,Who shot J.R. Euing? ,Kristin Sheppard ,General,Vodka - Triple Sec and lime juice make what cocktail,Kamikaze +General,Fruit puree thin enough to pour,Coulis,Music,Who Was Velvet Fog,Mel Torme,General,"Back in the day at Walt Disney studios, Walt's brother Ray (yes, Ray) reportedly peddled what to employees on the lot",Insurance +General,What marvel Comics superhero carries a star spangled shield,Captain america,General,Danish variety caraway flavoured liquor driest and most famous,Aquavit,General,Who read casey at the bat for her tv debut on the ed sullivan show?,Lauren bacall +General,What was a Spiney Pear,Pineapple,General,Of which country was Admiral Miklos Horthy the political leader during World War 2,Hungary,Geography,What's the only arab country without a desert ,Lebanon  +General,What European countries flag is square,Switzerland,General,What is the study of the flight path of projectiles under the influence of gravity known as,Ballistics,General,Where do the natives speak tagalog,Philippines +General,Names from jobs - Baker Cook obvious what did a Mercer do,Textile dealer,General,What drink has a totally tropical taste?,Lilt,General,"Which austere Christian sect, founded in 1650, rejects cultural rites and an ordained ministry",Quakers +General,Who was the leader of the notorious Gambino Mafia family,John Gotti,General,"In Norse mythology, what is the name of Odin's six-legged horse",Sleipn1r,Entertainment,"Who was a member of 'Crosby, Stills and Nash' and 'The Hollies'?",Graham Nash +General,A Cow Moos - A Cock Crows - What does an Ape do,Jibber,General,Whose sister Pamela appeared in the 1988 movie Sleepaway Camp 2,Bruce,General,The Russian sled drawn by 3 horses abreast,Troika +General,Whose best-selling album is An Innocent Man which reached number two in the charts in 1983,Billy joel,General,A standard what contains eight holes,Horseshoe, History & Holidays,What was Russian America called after 1867?,Alaska +General,In mythology who are sometimes called the dioscuri,Castor Pollux Zeus twin sons,Food & Drink,What Nationality Is The Lager Giant Grolsch? ,Dutch ,General,What Organization Is Also Known As “La Cosa Nostra”,The Mafia + Geography,Which U.S. state receives the most rainfall?,Hawaii,General,What award can baseball pitchers earn,Cy young award,General,Who composed 'romeo and juliet',Sergei prokofiev +General,Which sci-fi writer adapted his own book for the movie Pet Sematary,Stephen king,General,What does a konimeter measure,Dust,General,"Persian, Siameses and Abyssinian are types fo what",Cats +General,"Discoverer of nine ancient cities including Troy, Heinrich Schliemann made spectacular excavations of which city during 1874 to 1876",Mycenae,General,In what country is Tiahuanaco,Bolivia,General,Tropical mollusc with bright shell used as money in some parts of asia,Cowrie +General,Which is the largest of the Asteroids,Ceres,General,St Appolonia is the patron saint of what,Toothache,Music,What was the first George Harrison composition recorded by the Beatles?,Don't Bother Me +General,"In the cartoon show My Pet Monster,what were the only things that could send Beastor,Monster's enemy,back to the monster world?",A pair of orange handcuffs,General,What was the name of Felix the Cats girlfriend,Phyllis,General,"In the female version of this Simon play, the leads are Olive and Florence",The odd couple +General,What is a group of this animal called: Pony,String,General,The Carmelite monks have what more common name,Whitefriers,General,Which popular singer of the 60's &70's has the real name Clive Powell,Georgie fame +General,Who sang '25 or 6 to 4',Chicago, History & Holidays,She was the first woman premier of Israel.,Golda Meir,General,Name all five New Kids On The Block.,Danny Donny Jordan Joe Jon +Music,"Which 90's Song Opens With The Line ""She Came From Greece She Had A Thirst For Knowledge""",Pulp / Common People,General,Which early rock singer was nicknamed killer,Jerry Lee Lewis,General,"Which Swiss artist, born in 1879, painted Landscape With Yellow Birds, The Twittering Machine and Fish Magic",Paul klee +General,What wondrous creation was built by Sostratus of Cnidos,Pharos of Alexandria,Geography,Guayaquil is the largest city in what country,Ecuador,General,What is the fear of tombstones known as,Placophobia +General,In the Bible what did David give Saul as a dowry for Michal,200 Foreskins from Philistines,Science & Nature,The instrument used in geometry to measure angles is a(n) _______.,Protractor,General,What is your zodiac sign if you are born on july 15,Cancer +Sports & Leisure,In Which Game Might You Peg Out ,Cribbage ,General,Who is the Patron Saint of Spain,St James,General,The Red Arrows display team use which type of aircraft for their performances?,BAE T1 Hawk + Geography,What is the capital of Slovenia ?,Ljubljana,General,"What mechanical cop was described as ""the thinking man's terminator""",Robocop,General,"The Character Of ""Catwoman"" Is Based On Which Star Of The Silver Screen",Rita Hayworth +Science & Nature,What Name Is Given To The Diagram In Which Sets Are Represented By Circles ,Venn Diagram , Geography,Which element makes up 2.83% of the Earth's crust ?,Sodium,General,To which family of birds does the 'Goldeneye' belong,Duck +Music,By Which Name Is Paul Gadd Better Known,Gary Glitter,General,Department what was the worst team in major league baseball in 1991,Cleveland indians,General,What is Mexico's largest seaside resort,Cancun +Geography,Each tour through Natural Bridge Caverns in ____________ covers 3/4 mile. An average tour guide will walk almost 560 miles in one year.,Texas,General,What stat is stone mountain located in,Georgia,General,Anything that occupies space & has mass is generally known as ______,Matter +General,What is the second biggest country in South America after Brazil,Argentina,General,Which U.S. President was awarded the 1919 Nobel Peace,Woodrow wilson,Geography,"What's the most southerly city; Toronto, Seattle, Budapest or Bordeaux? ",Toronto  +General,Vodka and orange makes up what cocktail,Screwdriver,General,Which iron warship is docked close to HMS Victory at Portsmouth,Hms warrior,General,What band got their name from the sixties movie Barbarella?,Duran Duran +General,What facial feature contains approximately 550 hairs,Eyebrow,Music,Which Two Legendary Groups Were Having Fun Fun Fun Wogether In 1986,Status Quo & The Beach Boys,General,What is the name of the elephant headed god in India,Ganish +General,Tarlike mixture of hydrocarbons derived from petroleum,Bitumen,General,"What nationality was Boutros Boutros Ghali, once the Secretary General of the United Nations",Egyptian,General,In Monty Pythons Flying Circus Dinsdale was a giant what,Hedgehog +General,The original Peeping Tom had what job,Tailor,General,Who compete in the Maccabiah Games,Jewish world athletes,General,What is an 'earth pig',Aardvark +Religion & Mythology,"In Egyptian mythology, who is known as the god of the desert?",Ash,Science & Nature,Which type of fish are the Sea Wasp and Cubozoa? ,Jellyfish ,General,Who stopped Bjorn Borg's wimbledon winning streak,John mcenroe +General,What word comes from the Latin phrase to crowd together,Constipation Con Sta Pay Shun,General,What is the fear of opening one's eyes known as,Optophobia,General,The 1991 national championship in women's volleyball was won by,Ucla +Geography,"This country's flag has a large ""R"" on it.",Rwanda, Geography,Halifax is the capital of which Canadian province?,Nova Scotia,General,Which city was Axel Foley a cop in?,Detroit +Science & Nature,Saturday is named after which planet?,Saturn,General, A depilatory is a substance used for removing _______.,Hair,Science & Nature,Which Branch of Science is the study of sound? ,Acoustics  +General,What is Israel's domestic intelligence agency called?,Shin Bet,General,"To the nearest 0.1 km, how many kilometers in a mile?",1.6,General,What toxin is found in apple seeds,Cysnogenic glycoside +General,From whom did Peter Mandelson borrow the money for his house,Geoffrey robinson,Art & Literature,The effect of the harmony of color and values in a work. ,Tone,General,What animal comes in types spotted striped and brown,Hyena +General,What does the 'o' used as a prefix in irish surnames mean,Descendent of,Science & Nature,What is a group of donkeys called?,Herd,General,In what year was the first World Snooker Championship held at the Crucible Theatre in Sheffield?,1977 +General,Who could win a PATSY,Picture Animal Top Star of Year,General,The hard piece at the end of a shoelace or piece of clothing is called an_____,Aglet,Sports & Leisure,Harry Redknapp was manager of which club before West Ham? ,Bournemouth  +General,Operation Thunderbolt was the nickname given to which raid,Israeli raid on Entebbe,General,In which industry did Alfred Nobel make his fortune,Dynamite,Food & Drink,"Often drunk, this liquid is normally harvested from female cows.",Milk +General,What is the official language of new caledonia,French,General,What composer was the there of the 1947 film Song of Love,Johannes Brahms,General,The first cartoon character on the Beano was Eggo what was he,Ostrich +General,Which former Soviet Republic in Central Asia has Tashkent as its capital,Uzbekistan, History & Holidays,Where Did King John Sign The Magna Carta? ,Ruunymede ,General,What's the name of Snoopy's secretary,Woodstock +General,Wiccaphobia is the fear of ______,Witches and witchcraft,Sports & Leisure,Wembley Stadium Hosted The FA Cup Final On Saturday But How Many Years Did It Take To Build The New Stadium? ,7 Years ,General,Schmuck in German literally means what,Jewellery +Food & Drink,What gives pasta its green colour? ,Spinach ,General,Who was the lead singer in Herman's Hermits,Peter Noone,General,Who solves the crime in Death on the Nile,Hercule poirot +General,What is sex on the internet called,Cybersex,General,"Gold medal who asked the musical question ""didn't i blow your mind this time""",Delfonics,General,Who was the Head of State of Vichy France,Petain + History & Holidays,Who was the only survivor of Custer's last stand?,His horse,Music,"Who Co-Wrote ""Stop The World I Want To Get Off"" With Anthony Newley In 1960",Lionel Bart,General,In The Chinese Zodiac What Sign Comes Between The Rat & The Tiger ?,Ox +General,In ancient Rome by law prostitutes had to do what,Dye blond or wear blond wig,General,Which U.S. President pardoned Robert E. Lee posthumously of all crimes of treason,Gerald Ford,General,The second largest of the earths four oceans & the most heavily traveled,Atlantic ocean +General,In Curse of the Pink Panther who plays Clouseau after surgery,Roger Moore,General,What sport in Belgium people compete in the Fleche Walloons,Cycling,Science & Nature,What Is Rubella ,German Measles  +General,What type of alcoholic drink is Manzanilla,Sherry,General,"Who was the sponsor of ""Wild Kingdom""",Mutual of omaha,General,"What city's homeowner hoped to discourage tourists with a sign that read: ""Mork doesn't live here, so go away""",Boulder's boulders boulder +Science & Nature,"Like fingerprints, what other print is individual?",Tongueprints,Sports & Leisure,In american football where is the Orange Bowl?,Miami,Food & Drink,What vegetable is known a zucchini in the USA? ,Courgette  +General,What woman won 6 gold medals in the Olympic Summer games,Kristin Otto,General,Who was the first woman to lead a British trade union,Brenda Dean,General,In traditional pantomime who is the sweetheart of Harlequin,Columbine + History & Holidays,Who Released The 70's Album Entitled Superfly ,Curtis Mayfield ,General,Whats the Italian game thats similar to lawn bowling,Bocci,General,Papyrophobia is the fear of,Paper +General,"Who wrote 'born free', 'living free' and 'forever free'",Joy adamson,General,San Francisco by law unleashed what can't walk down market street,Elephants,General,What gets its name from the Aztec meaning bitter water,Chocolate - xocatl +Science & Nature,What is the scientific name for brimstone?,Sulphur,Art & Literature,"The ____ generation included such authors as Jack Kerouac, William S. Burroughs and Allen Ginsburg.",Beat,Science & Nature," The __________ does not chew its food, but swallows it whole. It carries several pounds of small stones in its stomach to aid in grinding up and digesting its nourishment.",Crocodile +General,What was Gary Gnu's catch phrase?,No Gnus is Good Gnus,General,The Comstock load is a silver deposit in what US state,Nevada,General,"What event was the interview of the Natural Born Killer, Mickey, to be held during?",The Superbowl +General,"Which breed of dog was introduced to Britain from Seistan province of Persia by John Barff, who exhibited it at the Kennel Club in 1907",Afghan hound,Science & Nature,What is the chemical symbol for copper?,Cu,General,Missouri has an unusual inalienable right - what,Drunkenness +General,Philippe Pages became famous as who,Richard Clayderman,General,Ben Hurs rival in the great chariot race,Messala,General,"What transparent material is produced by heating lime, sand & soda",Glass +General,Who is the adopted son of Vito Corleone,Tom Hagan,General,Edward Hunter USA Journalist invented what term Korean war,Brainwashing,General,Roy Thines played David Vincent in which TV series,The Invaders +General,What Fashion Accessory Did Mary Quant Design,The Mini Skirt,Music,What kind of pillow did Jefferson Airplane promote?,Surrealistic,General,"The great gothic cathedral of Milan was started in 1386, and wasn't completed until what year",1805 +General,What is the study of poisons called,Toxicology,General,Which movie prompted the style of wearing cutoff sweatshirts over the shoulder?,Flashdance,General,Ancient name for France,Gaul +Music,"Who Had A No.1 Huit In 1967 With ""Let The Heartaches Begin""",Long John Baldry,General,"Which Company In Britain Were Responsible For Pioneering ""The Video Disc""",Decca,General,What drink was named after Queen Mary l of England,Bloody mary +General,"Where did you see Fancy, Spook and Choo-Choo",In Top Cat,Sports & Leisure,What Is The Fastest Swimming Stroke ,The Crawl ,Science & Nature,Who Invented The Hovercraft ,Sir Christopher Cockeral  +General,Judeophobia is the fear of,Jews,General,What sweet fruit grows on certain kinds of palm tress,Dates,General,In the strip cartoon Dilbert name Dilbert's girlfriend,Liz +General,What did north american indians eat to dissolve gravel and stones in the bladder,Watercress,General,Who said 'The way of the warrior is the resolute acceptance of death' ?,Miyamoto Musashi,Art & Literature,How Is Jack Dawkins Otherwise Known As In A Dickens Novel? ,Artful Dodger  +General,In 1911 the archaeologist Hiram Bingham discovered what lost city,Machu Picchu,General,What is 'vichyssoise',Chilled soup,Food & Drink,"To make Drambuie, you add some honey to what type of whiskey?",Scotch +General,Who is the greek counterpart of juno,Hera,General,Who invented the television,John Logie Baird,General,What Is The Closest Living Creature To The T-Rex,A Chicken +General,Who sang the theme to The Spy who Loved Me,Carly Simon,General,Who tell of the mythical Bunyips that eat people,Australian Aboriginal,General,Who was the first woman Prime Minister of New Zealand?,Helen Clark +General,What does the Australian word 'duuny' mean,Toilet,General,Whose only line in his first play was Tennis Anyone,Humphry Bogart,Music,Anita Dels And Kid Ray Slijngaard Aro Known Collectively As,2 Unlimited +General,To which of its games did Hasbro give a red card in January 2000,Subbuteo,General,Time of the Season (1969) was done by what group,Zombies,General,What city has been air-rated the cleanest in the U.S. for a city its size,Amarillo +General,Which is the oldest walled city in the world,Jericho,Sports & Leisure,Which sportsman is mentioned in the Simon & Garfunkel song Mrs Robinson? ,Joe Dimaggio ,General,Collective nouns - A Chair of what (tradesmen),Glass Blowers +Music,"Writer For Michael Jackson, George Benson & Michael Mcdonald Among Others With Which Band Did Rod Temperton First Find Success",Heatwave,General,What would you be watching if you saw a round or waggledance,Honey Bees,Art & Literature,"In 'A Christmas Carol', what was the name of the miser?",Ebenezer Scrooge +General,"In ancient Rome, it was considered a sin to eat the flesh of what bird?",Woodpecker,General,What is the correct form of address for a foreign ambassador?,His/Her Excellency, Geography,Which of the U.S. states borders only one other state?,Maine + History & Holidays,Who starred in the 1957 film showboat as June? ,Ava Gardner ,Geography,What continent is Cyprus considered to be part of,Asia,General,In German cusine what are kartofflen,Potato dumplings +General,What was the first daily comic strip in the USA,Mutt and Jeff,General,Upon which river did Babylon stand,Euphrates,General,In Which English City Was The First Public Library Opened In 1608 (Under Some Dispute),Norwich +General,Former YES drummer Bill Bruford played with what group in 1972,King crimson,General,Who was the US equivalent of Alf Garnet,Archie Bunker,General,In which country is Innsbruck,Austria +General,Telly Savalas played which TV detective,Kojak,Geography,What is the capital of Indonesia,Jakarta,General,Which European city is served by Turnhouse Airport,Edinburgh +General,"In the theme song from 'the flintstones', what is the line after 'let's ride with the family down the street'",Through the courtesy of fred's two feet,General,"Immunity of the communications media including newspapers, books, magazines, radio, and television from government control or censorship.",Freedom of the press,General,Name the term for the length of a hawks legs from thigh to foot,In Falconry its an Arm +General,Where might you spend a Won,North or South Korea,Geography,Where are the North Yolla Bolly mountains? ,"USA, California ",Music,T'Pau Sang About China In Your What?,Hand +General,43% of Americans regularly do what,Attend church,General,What is the naval equivalent of an army major,Lieutenant commander,General,What is the Capital of: Malta,Valletta +General,Got a way what sport did andre agassi's dad compete in,Boxing,Science & Nature,What does the Rankine scale measure?,Temperature,General,What is the longest english word that can be typed using only the right hand,Lollipop +General,Approximately how many spoons are there in the 'New Jersey Spoon Museum',Five,General,"In Greek mythology, who was jocasta's son",Oedipus,Sports & Leisure,In 1998 Who Became The Youngest Footballer To Score A Hat Trick In The English Premiership? ,Michael Owen  +General,What word refers to very harsh laws such as those devised by a 7th century BC Athenian legislator,Draconian draconic,Food & Drink,What Is Used To Give Earl Grey It's Distinctive Flavour ,Burgamot Oil ,General,Which type of wood is used to smoke meats,Hickory +General,Stagecoach and Fort Apache starred which actor,John wayne,General,Which plant has the alternative name Lonicera,Honeysuckle,General,Who wrote the title song for Live and let Die,Paul McCartney +General,Marcus Garvey founded what,Rastafarians,General,What does sro stand for,Standing room only,General,Which Duo Where Formally Members Of The 80's Group The Tourists,The Eurythmics +General,"What form of verse is ""paradise lost"" written in",Blank verse,General,In ancient Egypt what food was reserved for the Pharaohs,Mushrooms,General,A Spanish-American farm worker is called a what,Peon +General,A Russian space programs name meant East what was it,Vostok,General,The Romans called it Eboracum name this English city,York,General,"Araucaria or Chile Pine has a more common name, what is it",Monkey puzzle tree +Entertainment,In which opera does Leporello entertain a vengeful jilted lover?,Don Giovanni,General,What colour did Ida McKinley ban from the White House,Yellow,General,Who played the nurse on the rookies,Kate jackson +General,What colour was moby dick,White,General, The study of insects is __________.,Entomology,Sports & Leisure,Which swimming stroke is named after an insect,Butterfly + History & Holidays,"Plus or minus 100 pounds (lb), how much did the world's largest recorded pumpkin weigh ","1,524 lb ",General,I don't like my friends.I don't like your friends either.,Heathers,General,John Henry Deutchendorf famous as who (both names),John Denver +Religion & Mythology,What religion was founded by Lao-tzu ?,Taoism,Food & Drink,Isabella Mayson is the famous author of a Cookery Book. How is she better known? ,Mrs Beeton , History & Holidays,"Which eighties album, that sold 20 million plus copies, featured Vincent Price ",Thriller  +General,What did Barbie first get in 1962,Her car made by Irwin for Mattel,General,In Star Trek what is the name of Spock's father,Sarek,General,Where is spain the southernmost,Continental europe +General,What European Nation Was The First To Drink Tea,The Dutch / Holland,General,What does a barometer measure,Atmospheric pressure,Science & Nature,Onto Whose Back Should The Gingerbread Man Not Have Jumped ,The Sly Fox  +Music,"Where Can you see Lenny Bruce, Edgar Allen Poe, Karl Marx, and HG Wells all in the same place at the same time",Sergeant Peppers,General,Austin is the capital of ______,Texas,General,Captain W E Johns invented which hero,Biggles +Science & Nature,Where Is The World's Rarest Plant? ,The UK. Encephalartos Woodii At Kew Gardens ,General,Who Wrote The Autobiography Entitled Dont Laugh At Me,Norman Wisdom,General,"In the monty python parody 'search for the holy grail', what did patsy say when they reached camelot",It's only a model +Music,Name The Carly Simon US No.1 For Which Mick Supplied A Backup Vocal,You're So Vain,General,"In the Bible, who was credited with killing tens of thousands of the Philistines, when his king was only credited with killing thousands of the Philistines",David,General,What coffee concoction is named after the Capuchin monks,Cappucino +Food & Drink,What is the main ingredient of Tapanade? ,Olives ,General,Who succeeded tommy douglas as leader of the new democratic party,David,General,What US state was the last to ratify abolition slavery 1990s,Mississippi +General,1836 Mr Gray a gasfitter 10 years penal servitude stealing what,One Rabbit,General,Who wrote 'Stardust',Hoagy carmichael,General,Oslo is the capital of ______,Norway +General,"""I'm a soldier of fortune, I'm a dog of war ___"" What's the Dire Straits song title",Ride Across the River,General,"What caused a separation of Baja, California and the rest of Mexico?",The San Andreas Fault,Sports & Leisure,What is it called when a football team loses possession of the ball due to a misplay?,Turnover +General,Jean Montgolfier in 1157 built the worlds first what,Paper factory,General,What science ficton author wrote about The Cities in Flight series,James Blish,General,What does a compass needle point to?,North +General,What's the most popular form of bridge,Contract bridge,General,Which classical poet said Amor vincet omnia Love Conquers all,Virgil,Sports & Leisure,Which Horse Won The Grand National In 2007? ,Silver Birch  +General,Elvis Stojko was an ice skating word champion - what country,Canada,General,"Who said ""Public service is my motto""",Al Capone,General,Which law did sir isaac newton discover when he was only twenty three years old,Law of universal gravitation +General,What is the old name for solid sodium hydroxide,Caustic soda,General,What 3 colours are featured on the Australian Aboriginal flag?,Black Red Yellow,General,A pommel is part of a what,Saddle +General,The ore bauxite is the chief commercial source of which element,Aluminium,General,What name is given to a cross fruit of tangerines and grapefruits?,Ugli Fruit,General,The Arcocarpus altilis was involved in a mutiny - what is it,Breadfruit + History & Holidays,What age preceded the Iron Age?,The Bronze Age,General,Market research says what colour makes people spend more,Light Purple,General,Which magazine uses the winged horse Pegasus as it's logo,Readers Digest +Science & Nature," Kittens are born both blind and deaf, but the vibration of their mother's purring is a physical signal that the kittens can feel _ it acts like a __________, signaling them to nurse.",Homing device,General,If you were doing vaccimulgence what doing,Milking a cow,General,Name group originally called the golliwogs had hit Proud Mary,Credence Clearwater Revival +General,What was the name of the German Republic of 1918-1933 overthrown by Hitler,Weimar republic, Geography,What is the basic unit of currency for Slovakia ?,Koruna,General,USA courts spend half their time with cases involving what,Cars +General,What officer of king pharoah bought joseph as a slave,Potiphar,General,What is the fear of pain known as,Odynephobia,General,Where is the best brandy bottled,"Cognac, france" +General,What is the oldest soft drink in the USA,Doctor Pepper,Music,Ghost Town Was A Number One Hit For Which Group,The Specials,Entertainment,"Name the band - songs include ""Black Night, Smoke On The Water""?",Deep Purple +General,What is the most ordered seafood item in a restaurant,Shrimp,General,Who wrote The Witches of Eastwick,John Updike,General,Who did Mork call to each week on Ork,Orsen +Music,"In Music How Are The Trio Of “Ferguson, Pinkney And Holliday” Better Known",The 3 Degrees,Entertainment,Who is the voice of Darth Vadar?,James Earl Jones,Music,"Which Funkster Had A Hit With ""Living In America""",James Brown +General,Magnus Huss (a Swede) coined which word,Alcoholism,General,What is the name of the oath which doctor's must take,Hippocratic,Sports & Leisure,The Stadium Of Light Is The Home Stadium Of Which English Football Team ,Sunderland  + History & Holidays,What was Gary Gnu's catch phrase? ,No Gnus is Good Gnus ,General,What is the smallest state in the u.s,Rhode island,General,Mlb: who was the last American league player to win the triple crown,Mickey +General,What mountain has the figures of three mounted confederate heroes of the American Civil War?,Stone Mountain, History & Holidays,How is the stockmarket collapse of the 24th October 1929 better known ?,Black Thursday,General,In the US what's the most common reason for a visit to ER,Stomach Cramps +General,Linda Hunt won an Oscar Year of Living Dangerously what 1st,First Oscar playing opposite sex,General,Which fruit has its seeds on the outside,Strawberry,General,What was the first disney film to feature stereophonic sound,Fantasia + History & Holidays,Who Played Jack Nicholson's Wife In The Shining ,Shelley Duvall ,Art & Literature,Which American Author Wrote The Novel (Roots) ,Alex Haley ,General,What pollinates malacophilous plants,Snails + History & Holidays,This U.S. president was fatally shot in 1881.,James Garfield, History & Holidays,Name the cartoon that featured characters who had messages on their clothes to express their feelings. ,Shirt Tales ,Sports & Leisure,Which Rugby League Club Has Won The Challenge Cup More Times Than Any Other ,Wigan (15)  +Entertainment,Name Li'l Abner's favorite Indian drink.,Kickapoo joy juice,General,What us state includes the telephone area code 609,New jersey,General,"What is the name given to a quadrilateral with one, and only one, pair of sides parallel to each other?",Trapezium +General,What bird has two toes,Ostrich,Entertainment,In what year did both Peanuts and Beetle Bailey first appear,1950,General,What links Kelly Highway Block and Wales,Clint Eastwood character names +Music,Which All-Male Group Spent More Weeks On The UK Charts In The 1980s Than Any Other Group?,Madness,General,Where is the cleanest air,Tasmania, History & Holidays,"Oklahoma's four include the ouachitas, arbuckles, wichitas, and the kiamichis. ",Mountain ranges  +General,Gregory Peck played Lt Joe Clements in what 1950s film,Pork Chop Hill,Science & Nature,What is the clear homogeneous liquid portion of a nuclear protoplasm?,Karyolymph,General,On the Beaufort scale what is defined as force 11,A Storm +Music,Kim Smith Is The Real Name Of Wjich Singer,Kim Wilde,General,Which Organization Takes It's Name From The Latin For Table,Mensa,General,What cocktail is made from gin and collins,Tom collins +Music,Which phrase with a French flavour was the titleof Paul Young's debut album?,No Parlez,Music,"""Wouldn't It Be Good"" Was A UK Hit For Which Solo Male Artist",Nik Kershaw,General,Which Apostle didn't believe in the resurrection until he had seen the Saviour's wounds,Thomas +Geography,Which French Explorer Of West Africa Convinced Many African Leaders To Cede Power To France ,Pierre Ne Brazza ,General,What's the largest commercial passenger plane,The jumbojet 747,Sports & Leisure,Who Did John McEnroe Play 3 Times In The Wimbledon Mens Final ,Bjorn Borg  +General,What was jean harris found guilty of,Second degree murder 2nd degree murder,Science & Nature," Some sloths, opossum, and armadillos spend up to 80 percent of their lives sleeping or __________",Dozing,Geography,What is the nearest town to the Lightwater Valley Theme Park? ,Ripon  +General,What is Britain's largest wild mammel,Grey seal,General,What is the flower that stands for: anxious and trembling,Red columbine,General,What actress had made a million dollars by the age of 10,Shirley temple +General,Which country consumes the most chicken per capita,Saudi Arabia,General,Madrid there are more what in one street than all of Finland,Bars,General,Who was the American President from 1923 to 1929,Calvin coolidge +General,How did the crew of Red Dwarf get brought back to life?,By Nanobots,General,What is the only domestic animal not mentioned in the 'bible',Cat,General,Who invented the first completely automated robotic milking machine,Denmark +General,What is a group of herring,Army, History & Holidays,What Type Of Ghosts Are Known To Play Pranks On Humans ,Poltergeists ,General,What flower is the symbol of secrecy,Rose +General,Elvis Presley Adolf Hitler Errol Flynn all had what kinky habit,Peeping Toms,Geography,What famous geyser erupts regularly at the Yellowstone National Park,Old faithful,General,Who fought professionally in roman arenas,Gladiator +General,What titan had snakes for hair,Medusa,General,"The most famous church in Great Britain, enshrining many of the traditions of the British people",Westminster Abbey,General,Oneirophobia is a fear of ______,Dreams + History & Holidays,Who Had An 80's Hit With The Song 'Is There Something I Should Know' ,Duran Duran ,General,Gobo was the male cousin of which Disney character,Bambi,General,"Which U.S. cop series had the catchphrase ""who loves ya baby'",Kojak +General,What Canadian city has the most bars per capita,Halifax Nova Scotia, History & Holidays,Which eighties sitcom featured Tom Hanks in drag on a regular basis? ,Bosom Buddies ,Music,In Which Year Did Albatross Top The Charts For Fleetwood Mac,1968 +General,Which Alabama city was the first capital of the Confederacy during the Civil War,Montgomery,General,Where would you find a coiffe or muselet,Bottle wire cork hold, History & Holidays,"What war lasted from June 5 to June 11, 1967?",Six day war +General,Which elephant cant be domesticated the african or indian,African,People & Places,Who Was The Youngest US President To Die In Office ,John F Kennedy ,General,A long wined sea bird related to the petrel,Albatross +Food & Drink,Sapporo is brewed in this country.,Japan,General,Which Song By Holly Valance Was A UK Chart Topper In 2002 ,Kiss Kiss ,General,"In 1797 Wilhelm Beer, first to map",Mars +General,Mottephobia is the fear of what?,Moths,General,What is the Capital of: Gibraltar,Gibraltar,Science & Nature,Growing plants in liquids rather than soil is known as ________.,Hydroponics +General,"Because metal was scarce, the oscars given out during WW II were made out of what",Wood,General,Who was president when charles lindbergh flew into history,Calvin coolidge,General,Who did gawain accuse lancelot of sleeping with,Guinevere +General,A flag flown upside-down is a signal of a(n) _________,Emergency,General,Who were Thors two sons (the brave & the powerful),Modi & magni,Music,According To The Song By Katie Melua Where Are There 4 Million Bicycles,Beijjing +Music,Who is Pat Andrejewski better known as?,Pat Benatar,General,Who palyed Mork from Ork's son?,Johnathen Winters,General,What is the Capital of: Sudan,Khartoum +General,Egyptian embalmers replaced the bodies eyes with what,Onions,General,"In astronomy, the apparent great-circle annual path of the sun in the celestial sphere, as seen from the earth",Ecliptic,General,Which golfer was nicknamed Supermex,Lee Travino +General,What chicken part is the snack of choice for Chinese movie goers,Feet,General,Bananas are actually what,Herbs,General,In the UK The Elder Brethren of Trinity House manage what,All Lighthouses +General,What type of scientific equipment was named after the german Bunsen,Burner,General,We know what a moussaka is but what does it literally mean,Moistened, History & Holidays,What is a witches broom called ,A Besom  +General,With what sport is gabriela sabatini associated,Tennis, History & Holidays,Mussolini invaded this country in 1935.,Ethiopia,General,The USA call her the Maid of Honour what do the British call her,The Chief Bridesmaid +General,Which Country Consumes More Coca-Cola Per Head Than Any Other?,Iceland, History & Holidays,Who wrote the children's book 'How The Grinch Stole Christmas'' ,Dr Seuss ,Science & Nature,What Is The Rowan Tree Also Known As? ,The Mountain Ash  +General,Which breed of dog was developed from the Bullenbaiser,Boxer,General,Who wrote about a british agent named george smiley?,John le carr,Science & Nature,The name for the group of stars which form a hunter with a club and shield is ________.,Orion + History & Holidays,What Star Sign Would You Be If Your Birthday Fell On Christmas Day ,Capricorn ,General,What 1951 film featured ronald reagan raising a chimp,Bedtime for bonzo,General,A cows stomach has how many compartments,Four +General,The Stasi were an intelligence organisation in what country,East Germany,General,Who played selena in the film 'selena',Jennifer lopez,General,Who wrote the play Androcles and the Lion,George Bernard Shaw +General,Who Was The Very First Actor To Win 2 Consecutive Best Actor Oscars,Spencer Tracy,General,What is a group of oxen,Writing,General,Of who was eva braun the mistress,Adolf hitler + History & Holidays,What was Alaska called before 1867?,Russian America,General,What major league baseball team was forced to endure a 20-day road trip in 1996,The Atlanta Braves, Language,"Many Meanings: Fuel, vapor, flattulence, helium. What is it?",Gas +General,What French word literally means little skip,Cabriolet,Science & Nature,"The leaves of the tomato plant are poisonous, they contain ________",Strychnine,General,David Letterman made his movie debut in what film,Cabin Boy +General,What type of creature is an Orb Weaver,Spider,General,What cemetery is Karl Marx buried in,Highgate,General,Who shared the 1993 Nobel Peace Prize with Nelson Mandela,F w deklerk +General,Who wrote Private Lives - 1930 - Blyth Spirit 1941 (both names),Noel Coward,General,5 African Mediterranean countries share what language,Arabic,General,Name the man who developed the first practical pneumatic tyre.,John dunlop +General,All commercially bred turkeys are what,Artificially Inseminated – males oversized, History & Holidays,Which stormy couple had a hit with Nutbush City Limits? ,Ike & Tina Turner ,Geography,Which British Island is served by Ronaldsway Airport? ,Isle Of Man  +Art & Literature,Who Was The Merchant In Shakespear's (The Merchant Of Venice) ,Antonio ,General,"Which Castle, home of the Dukes of Rutland, is near Grantham in Lincolnshire",Belvoir castle,Music,Which Dire Straits Album Features The Song “Walk Of Life” And “Money For Nothing”?,Brothers In Arms +General,Three fourths of household _____ is used to flush the toilet & take baths & showers,Water,General,What masked hero first appeared 1919 The Curse of Capistrano,Zorro,Science & Nature,What Is The Worlds Largest Mammal ,Blue Whale  +General,Who performed the world's worst circumcision,Lorena bobbit,Music,What Was The Temptations Only Top 10 Hit From The 60's,Get Ready,General,What is a group of partridges,Covey +General,Large amphibious broad tailed rodent,Beaver,General,An average human drinks about how many gallons of water in a lifetime,"16,000",General,Who would wear a Zucchetto,Catholic Clergy – hat like Biretta +General,Topo in Italian Fare in Turkish what in English,Mouse,General,What was the name of the first U.S. communications satellite to amplify radio & tv signals,Telstar,General,In Norway 1980 man fined for being drunk in charge of what,Mobile vacuum cleaner +General,A story by Edgar Allan Poe___Fall of the..,House of usher,General,"Who in fiction rode a horse called ""Shadowfax""",The wizard gandalf,General,"What links Doric, Ionic, Tuscan, Corinthian and Composite",Classical Architecture + History & Holidays,In cockney rhyming slang what are 'Mince Pies'' ,Your Eyes ,Sports & Leisure,"What sport do the following terms belong to - ""Pist & Telemark""?",Skiing,Music,What Is The First Name Of Pauls Fashion Designer Daughter,Stella +General,"Who said ""I have no problems with drugs - only policemen""",Keith Richard,General,"In Terry Pratchett's Discworld series, what was the name of Death's apprentice",Mort,General,What was Super Mario's original name,Jumperman +General,The human body has over 600 what to account for 40% of the body's weight,Muscles,General,"Bean setting, Leap frog, Laudnum bunches types of what dance",Morris dances,Technology & Video Games,"In Chrono Trigger, who is the ""Master of War?"" ",Spekkio +Music,"Who Had A 1994 Hit With ""Them Girls, Them Girls""",Zig & Zag,General,What is the name of the classic dessert of pancakes served in an orange sauce with alcohol,Crepe suzette,General,Who directed the film 'the duel',Steven spielberg +General,Diane Belmont became famous as who,Lucille Ball,General,Which was the first British group to have 3 consecutive U.K. No. 1 hits that went straight to No. 1 upon release,Gerry and the pacemakers,General,Joel Chandler Harris wrote which series of stories,Uncle Remus +General,Which was the first U.S. city to stage the summer Olympics,St louis,Art & Literature,"A painting movement that flourished in France in the 1880s and 1980s in which subject matter was suggested rather than directly presented. It featured decorative, stylized, and evocative images. ",Symbolism,General,George De Mestral Is Credited With The Invention Of Which Non Electrical Product In 1948,Velcro + History & Holidays,Where do bathers traditionally go for a swim in Hyde Park on Christmas Day ,The Serpentine ,General,Who made the first successful balloon flight across the Atlantic,"Ben abruzzo, maxie erson & larry newman",General,What is the product for 'get that 'just brushed freshness' with it',Dentyne +General,What animal has the same name as a high church official,Cardinal,General,45% of Americans use what each day,Mouthwash,Music,Japanese Boy Was A One Off UK Hit For Which Singer In 1981,Aneka +General,Acmegenesis is a fancy name for what,Orgasm,Music,Which Company used the Queen Single “I Want To Break Free” In A Commercial,Shell,General,What is the name given to the side opposite the right angle of a right-angled triangle,Hypoteneuse +General,What well known Russian author was also a doctor,Anton Chekov,General,Which is the only bird that drops it's upper eyelid to blink,Owl,Sports & Leisure,Which Bridge Is Past First In The University Boat Race ,Hammersmith  +Science & Nature,What is a group of larks called,Exaltation,Mathematics,An angle greater than 90 degrees and less than 180 degrees is said to be _________.,Obtuse, History & Holidays,Which Actress played Grotbags the witch in the TV show Emu's World ,Carol Lee Scott  +Science & Nature,What word is used for a female sheep?,Ewe,General,Mount Stuart lies at the centre of which country,Australia,General,How many people did andrew cunanan kill before killing gianni versace,Four +General,In Bonanza what was Hoss Cartwright's characters first name,Eric,General,On Monday Morning July 16 th 1945 The World Was Changed Forever When The Worlds First Atomic Bomb Was Tested In An Isolated Area Of Where?,New Mexico,General,What is the spanish equivalent of the english 'april fool's day',Boob day +Music,How Many Girls Were There In Steps,"3 , Lisa, Claire, Faye",General,"In which constellation is the first magnitude, giant star Aldebaran",Taurus,General,"After five years as ""suzanne sugarbaker"", delta burke leaves the hit show?",Designing women +Music,"According To The Title Of Their 1979 Hit, Where Did The Leyton Buzzards Spend Saturday Night",Beneath The Plastic Palm Trees,General,What is an Oklahoma / Arizona / Harlem credit card,Petrol Siphon hose,General,"What is the longest thing an ""abseiler"" carries with him",Rope +General,When was the date of the Christian festival Easter fixed by the Council of Nicaea?,325 AD,General,What was the name of the dog in Peter Pan,Nana,General,What's the worlds largest selling designer clothing range,Ralph Lauren +General,What is the correct name for the honey bear or potto,Kinkajou,General,What is the naval equivalent of an army Major?,Lieutenant Commander,Geography,What Is The Capital Of Chile ,Santiago  +General,Yogi Bear's sidekick was?,Boo boo,General,Who is the roman counterpart of hephaestus,Vulcan,Sports & Leisure,"According to rules of both sports, the top of which net is highest from the ground, Tennis or Table Tennis ",Tennis  +Geography,Into what sea does the elbe river flow ,North ,General,By Olympic rules what must have 14 feathers,Badminton Bird,General,What country celebrates its National Day on 21st July?,Belgium +General,"Lee Which U.S. state is known as the ""Volunteer State""",Tennessee,General,What mammal lives longest,Man,General,What does the medical abbreviation PM stand for,Post mortem +General,By law what can you not do in Minnesota with your washing line,Put male female washing together,Food & Drink,Which part of the English breakfast was known derisively in Victorian times as Little bags of mystery? ,Sausages ,General, A pugilist is a _________.,Boxer +General,What comedy team's films included 'hollywood or bust' and 'living it up',Dean martin and jerry lewis,General,Alcohol is added to soap to make it,Clear,General,What was the name of the horse before Silver in lone ranger,Dusty +General,Which european country will lose its independence if there is no heir to the throne,Monaco,General,In Blue Earth Minnesota illegal under 12s do what without parent,Talk on Telephone,General,"What is the japanese word for ""squad leader""",Honcho +General,Mark David Chapman was famous for what in 1980?,Shooting John Lennon,General,The Koh I Nor Is The Worlds Largest What,Diamond,General,What's the name of the Bar Restaurant in the TV show Quincy,Danny's + History & Holidays,Which `Rocky Horror Picture Show' actor played Mocata in the 1967 film 'The Devil Rides Out' ,Charles Gray ,General,"What links Escalator, Kerosene, YoYo, Zipper and Thermos",Names into language,Food & Drink,What sort of cheese would you sprinkle on spaghetti? ,Parmesan  +General,Which camera company produces the popular 'Trip',Olympus,Geography,Which European capital city stands on the Manzanares River? ,Madrid ,General,What was Mussolini forced to do when the allies landed in Sicily,Resign +General,Harvey Lee Yeary II became famous under what name,Lee Majors,General,A hot spring,Geyser,General,Who invented the bolometer,Samuel p langley +Geography,"Of the ten strongest earthquakes ever recorded in the world, three have occurred in ____________",Alaska,Music,"Who Recorded The Albums ""Woodface"" And ""Together Alone""",Crowded House,Food & Drink,Where Did Worcester Sauce Originate From ,India  +General,What is wynonna judd's real name,Christina clair ciminella,General,Where could you spend a guarani,Paraguay,General,"Which islands off Northumberland are renowned as a Guillemot, Cormorant and Puffin breeding ground but also host over 180 species?",Farne Islands +General,Somniphobia is a fear of ______,Sleep,General,Tha battle of Salamanca occurred in which war?,Peninsular War,General,Game bird with feathered feet,Grouse +General,What is the default extension given to paintbrush files,It's .bmp,General,The Panmunjon talks ended which war,Korean,General,Of which country was louis philippe the last king,France +General,A Quidnunc is a what - from the Latin Quidnunc what now,Gossip - used to be all politicians,People & Places,Who became the new Poet Laureate in 1999? ,Andrew Motion ,General,In what film did Elvis play a Red Indian,Stay away Joe +General,Which girls name comes from German meaning battle,Hilda,General,Which metal is combined with lead to make pewter,Tin,General,In which religion are the holy writings called the Adi Granth,Sikh +General,A large box for valuables,Coffer,General,What was designed and built in Iowa 1930s by George Nissan,Trampoline,General,"Relating to cookery what are 'lokshen', used in a type of Jewish soup",Noodles +General,Why do Christians fast during the 40 days of Lent leading up to Easter ,To replicate the 40 days Jesus spent in the wilderness ,General,In what film did madeline kahn play 'trixie delight,Paper moon,General,If you had a Colles' fracture which part of the body would be affected,Wrist +General,What kind of pants were first worn during the California gold rush,Denim jeans,Science & Nature,North American Indians ate watercress to dissolve gravel and stones in the ______?,Bladder, History & Holidays,Which nation was led by Genghis Khan?,Mongolia +General,Who founded 'live aid' and 'band aid',Bob geldof,General,What's the nfl penalty for an invalid fair catch signal,Five yards,General,Who Was The First Artist To Record With The Virgin Music Label,Mike Oldfield +Science & Nature,From which plant is the poison ricin obtained? ,The Castor Oil Plant ,General,In the human body what is replaced every three months,Eyelashes,General,"Where in the Ancient World, were the Pillars of Hercules",Straits of gibraltar +General,How was the universe said to have been created,Big bang,General,What 1958 eddie cochran song became his biggest us hit and a rock classic,Summertime blues,Toys & Games,Which British game is known as checkers in the USA,Draughts +Science & Nature,What is the meaning of the name of the constellation Lyra ?,Lyre,Science & Nature,What is the largest lizard?,Komodo Dragon,Science & Nature,An unusual feature of the wombats pouch is that it ______.?,Faces backwards +General,With what song did Status Quo opened the Band Aid concert,Rocking all over the World,General,"Barcelona 1992 Olympics: This countries medal tally was: 44 Gold, 37 Silver, 29 Bronze, 110 in Total",Unified team 1992,General,What does dermatitis affect,Skin +General,Who ate chicken little,Foxy loxy,General,What is the name of the company that was the first shirt sponsor of Liverpool football club,Hitachi,General,"Since 1600, how many species and subspecies of birds have become extinct",One +General,Garnet is the birthstone of January - what does it symbolise,Truth - consistency,General,Which record label signed the Rolling Stones in 1991,Virgin,Science & Nature,"Forked, Sheet, and Ball are types of __________.",Lightning +General,In which country are the Philips company based,Holland,General,"The ______________ has 14,174 teeth (105 rows, 135 teeths in each)",Garden snail,General,"What links Calabria, Liguria, Puglia and Veneto",Regions of Italy +Science & Nature,"Maxillary palps, abdomen, and metathorax are parts of a(n) ________.",Insect,General,What ex-girl friend of prince andrew appeared naked on screen,Koo stark,Sports & Leisure,What do runners pass to each other in a relay race,Baton +Music,"Who Asked ""Am I The Same Girl"" In 1992",Swing Out Sister,General,"In the film 'Titanic', who played Jack",Leonardo Dicaprio,People & Places,What Is The Title Of Tony Adams Biography ,Addicted  +General,Name the Hindu god with the head of an elephant.,Ganesh ganesha ganapati ganesa,General,If you had Naphephillia what turns you on,Being Touched,General,What did Woody ask Kelly's father when they first met,He asked if they could date +Science & Nature,What is normal body temperature for an adult human,97.8 F or 36.5C,Science & Nature,What Did A Professor Of Vocal Physiology At Boston University Use His Deaf Students To Help Him Invent In 1876 ,The Telephone ,General,In which sport would you compete for the Nino Bibia cup,Bobsleighing + History & Holidays,In 1954 Roger Bannister achieved the worlds 1st Sub 4 min mile in what English County did this take place ,Oxforshire ,General,"Who recorded ""tragedy"" in 1960",Thomas wayne,General,What Gilbert & Sullivan operetta subtitled The Peer and the Peri,Iolanthe +General,What tv series was based on the series of books by laura ingalls wilder,Little house on the prairie,General,In Providence Rhode Island its illegal to buy what on a Sunday,Toothbrush,Entertainment,As who is Terry Bollea known?,Hulk Hogan +General,In Maine it is illegal to bite your own what,Landlord,General,What is the minimum number of secret service agents to accompany the u.s. president at all times,Six,General,Involuntary audible spasm of respiratory organ,Hiccup +General,"Which German chemist, along with Fritz Strassmann, is credited with the discovery of Nuclear Fission?",Otto Hahn,General,After the US what country imports the most scotch,France,General,What is the technical term for blood poisoning,Toxaemia +General,How many men have walked on the moon,12,General,Los Angeles alone has more than the whole of France - what,Judges,General,Who directed Full Metal Jacket,Stanley Kubrick + History & Holidays,From what were Jack-o-Lanterns first made in U.K. ,Turnips ,Art & Literature,The Royal Opera House in London is home to which Branch of the arts other than Opera? ,Ballet ,Music,"""R.O.C.K In The USA"" Was A Hit For Which American Rocker",John Cougar Mellencamp +General,What was the top film of 1990,Home Alone,General,In Zion city Illinois its illegal to do what,Make ugly faces at anyone,General,What is the only metal that is liquid at room temperature,Mercury +General,Which cartoon character's vital statistics were 19-19-19 -in inches,Olive oyl,General,Who was Anastasia & Drizella's stepsister,Cinderella,General,The 'Seven Ages of Man' speech appears in which of Shakespeare's plays?,As You Like It +General,In WW1 what warning device was on the top of Eiffel Tower,Parrots,General,The roads on the island of Guam are made with what,Coral,General,How many contestants participated in the first modern olympiad,484 +General,What is installed in the world's deepest mine in carletonville,Refrigeration,General,The Lent Lilly has a more common name - what,Daffodil,General,Who did valeri solanis shoot,Andy warhol +General,In the Bible who climbed Mount Nebo,Moses to see promised land,General,Which company is the largest buyer of sugar in the world,Coca-cola,General,Roy Scherer jr became famous as who,Rock Hudson +General,"The average human body contains enough iron to make how many 3"" nails",1,Geography,What is the capital of Nigeria,Abuja,General,In 1984 Bloomingdale's started selling 100000 year old what,Glacial ice from Greenland +General,"Spanish: How do yoU.S.ay ""sixteen""",Diez y seis,General,"Whose epitaph says ""On the whole, I'd rather be in Philadelphia""",Wc fields,General,What was the original name of paul mccartney's fictional church cleaner 'eleanor rigby',Miss daisy hawkins +Toys & Games,Which exercises are designed to increase O2 consumption & speed circulation,Aerobics,General,In WW2 Germans used a Schlusselmaschine E what do we call it,Enigma coding machine,Science & Nature,Who Invented The British Jet Engine ,Frank Whittle  +General,Where is your zygomatic bone,Cheek,General,What was A.A. Milne's first name,Alan,Geography,___________________ includes the islands of New Britain and New Ireland.,Papua new guinea +General,Run away to marry secretly,Elope,General,Where was formed the disco band Boney M in 1976,Germany,Science & Nature,Does Uranus have an aurora?,Yes + Geography,What is the capital of Florida?,Tallahassee,Geography,Which mainland Latin American country is in neither South America nor Central America,Mexico,Music,What Was The Name Of ABC's First Album,The Lexicon Of Love + Geography,Name the capital city of Utah.,Salt Lake City,General,Murcia is a region in which European country,Spain,Geography,Which islands were named after Prince Philip of Spain,The philippine s +General,"Who fought at the battles of Bastia, Calvi and Toulon",Horatio Nelson,General,What was Paul McCartney's first solo album called,McCartney,Music,"Who Reached No.1 With The Song ""You're The First The Last My Everything""",Barry White +General,"What is often referred to as ""the oldest profession""?",Prostitution,Music,Bon Scott Was The Lead Singer For Which Famous Rock Band?,AC/DC,General,Attribution of human form or qualities to that which is not human,Anthropomorphism personification +Music,What Is The Name Given To A Sailors Work Song,A Sea Shanty,General,Place of shelter for ships,Harbour,General,In 17th century if you got Xmas clap what have you been given,A Kiss under Mistletoe +Science & Nature,What is the SI Unit for pressure? ,Pascal ,General,Which order of monks are famous for their silence,Trappist,Food & Drink,In which country do red onions originate? ,Italy  +General,Who won Best Supporting Actress for her role in Melvin and Howard,Mary,General,"A sugar with the formula c12h22011, belonging to the group of carbohydrates known as disaccharides (sugar)",Sucrose,General,"If a dish is served A la Bretonne, with what would it be garnished",Haricot beans +General,Kigali is the capital of ______,Rwanda,General,A Weaner is a baby what,Elephant Seal,General,What is the speed of a body expressed as a ratio with the speed of sound,Mach number +General,What kind of birds would you find in a gaggle,Geese,Science & Nature,What herbivore sleeps one hour a night?,Antelope,Art & Literature,Who created 'Maudie Frickett'?,Jonathan Winters + Geography,What is the basic unit of currency for Burkina Faso ?,Franc,Sports & Leisure,What is the name of the oldest National Hockey League team in the US?,The Boston Bruins,General,Which TV characters blood pressure was minus 3,Herman Munster +General,The French call it La Mort aux Trousses what Hitchcock film is it,North by Northwest,General,"What sort of athletes do sit spins, axel jumps & flying camels",Figure skaters,Science & Nature, __________ have no ability to taste sweet things.,Cats +General,What common 4 legged animals never walk or trot,Rabbits,Geography,St Catherine's Point is at the most Southerly tip of which island in the British Isles? ,Isle Of Wight ,Art & Literature,"Meaning 'rebirth' in french. Refers to Europe c. 1400-1600. The style began in Italy and stressed the forms of classical antiquity, a realistic representation of space based on scientific perspective, and secular subjects.",Renaissance +General,Where is David Livingstone Buried (Two Places / countries),Westminster Abby / Tanzania,General,"Agni, face covered in butter, is the Hindu god of what",Fire,General,What is the fear of loud noises known as,Ligyrophobia +General,What does the prefix 'pseudo' mean,Pretend,Art & Literature,This Romantic poet and husband to Mary Shelley drowned in a boating accident.,Percy Bysshe Shelley,Food & Drink,"Booze Name: 2 oz. gin, juice of 1/2 lemon, 1 tsp sugar, pour over ice cubes.",Tom collins +General,"Ezekiel, Jeremiah, Eli and Isaiah were all what in The Bible",Prophets,General,What is the fear of mind known as,Psychophobia,General,"In his profession who's entitled to wear the ""traje de luces""",Bullfighter +General,The Venetian island of Murano is particularly associated with the manufacture of which product,Glass,General,"Way Religious phenomenon in which a message is sent by God (or by a god) to human beings through an intermediary, or prophet",Prophecy,General,What's the nickname of the Iowa state football team,Cyclones + History & Holidays,"In 1902 this volcano erupted, killing 30,000",Pelee,General,What is the fear of being last known as,Telosphobia,General,What was tarzan's true identity?,Lord greystoke +People & Places,Who Would You Associate With Lou Costello ,Bud Abbott , History & Holidays,Which city was Axel Foley a cop in? ,Detroit ,People & Places,Which English County Has The Longest Coastline? ,Cornwall  +General,Holland hosted the Olympics in which year,1928,General,Who acquired 4 of the 7 dead sea scrolls in 1955,Israel,Music,"What Was 81 A Good Year For According To ""Elvis Costello""",Roses +General,What company created the gif image file format,CompuServe,Science & Nature,Which Insect Has A Pair Of Prominent Pincers At The Tip Of It's Abdomen ,The Earwig ,General,Film - who played carrie,Sissy spacek +General,What is the binot simon scale used to measure,Intelligence,General,This place in Germany is also the name of a (popular) cake,Black forest,General,What are you caught in if a haboob blows up,Sandstorm +General,50 years ago Texas giving advice on what was prison sentence,Birth Control,General,1 pm In military time is how many hours,1300,General,When was the first transatlantic solo flight,1927 +General,The Murryfield Racers play which sport,Ice Hockey,General,"Who was kidnapped on the night of march 1, 1932",Lindbergh,General,In Which State Is The Mormon Religion Based?,Utah +Science & Nature,In Which City Is The Oldest Metro System In The World ,London ,Science & Nature,Which Is The Largest Of The Migratory Animals? ,The Whale ,Food & Drink,There are only two perennial vegetables. Rhubarb & What? ,Asparagus  +General,"Who sang the song ""Californication""?",Red Hot Chilli Peppers,General,With who did benito mussolini dally,Clara petacci,General,Who was the last member of the bonaparte family,Jerome napoleon bonaparte +General,"In common: detroit, phibes, demento, faustus?",Doctors,General,What English explorer discovered & named Virginia,Sir walter raleigh,General,What Nationality Was The Formula One Driver Jaques Villeneuve,Canadian +Religion & Mythology,Which Roman God was the equivalent of the Greek God Dionysus?,Bacchus,Sports & Leisure,Who Was Beaten By Muhamed Ali In The Bout Entitled The Thriller In Manilla ,Joe Frazier ,General,How many mascots did the Sydney Olympics have?,3 +Music,Where Were typically Tropical Going To In 1975,Barbados,General,What is brady's profession,Singer,General,In what organ is the islands of langerhans found,Pancreas +General,Who was castrated in the time of richard the lionheart,Poachers,General,"In the 1969 Apollo 11 landing, who stayed in the command module",Michael collins, History & Holidays,In which city did the Duke of Windsor die in 1972? ,Paris  +General,Who Was Sportswoman Of The Year In 1971,Princess Anne,General,Who was the victim of the first murder seen live on tv,Lee harvey oswald,General,Who preceded Richard Nixon as President of the U.S.A.,Lyndon b johnson +General,What gemstone has a name literally meaning not intoxicated,Amethyst,Music,Who Wrote The Autobiography A Cellarful Of Noise,Brian Epstein, History & Holidays,What colour is Santa Claus's belt? ,Black  +General,Which record company rejected the Beatles as being past it,Decca,General,In Mens Tennis Players From Which Country Have Won Wimbledon More Times Than Any Other,England,General,Solomon built his temple on a hill name it,Zion +General,Patterson what do you call a person that stuffs dead animals,Taxidermist,General,U.S. captials - hawaii,Honolulu,General,What is the alternative name of Beethoven's sonata number 14 in C sharp minor,Moonlight +General,In what country was the worlds first wildlife sanctuary set up,Sri Lanka 3rd cent BC,Science & Nature,A salt enema was given to children to rid them of ______?,Threadworm,General,Monophobia is the fear of,Solitude being alone +General,Shinguards were introduced into football in which year,1839,General,What is the world largest seed,Coco-de-mare palm – double coconut,General,"What theme park was dubbed by some wags as ""Euro dismal""",Disneyland paris +General,What name is given to sweet chestnuts preserved in syrup,Marrons glace,General,Over what place in india is it forbidden to fly an airplane?,Taj Mahal,General,In which modem day country did the Battle of Austerlitz take place in 1805,Czech republic +Sports & Leisure,Which Boys Name Is Also The Name Of The Object Ball In Bowls? ,Jack ,Music,Reaching No 49 In 1971 Which Record Climbed To No 3 For America On It's Re-Entry In 1972,Horse With No Name,General,What is a person who makes barrels called?,Cooper +Science & Nature," The shrew is known to eat up to its own weight about every three hours. Deprived of nutrition for a day, it may __________",Starve to death,General,What is a group of swans in flight,Wedge,Science & Nature,What Element Can Be Added To Petrol To Eliminate To Eliminate Knock ,Lead  +General,Who used to do naked cartwheels to amuse the English settlers,Pocahontas,General,"What group of minerals are diamond, coal and graphite a part",Carbon,Sports & Leisure,Which Former Heavy Weight Boxing Champion Has An Identical Twin Brother Called George? ,Henry Cooper  +General,Name the first British show to air on US autumn prime time,The Avengers,General,What is a group of wild dogs,Pack,General,What 1942 naval engagement saw the sinking of four japanese aircraft carriers,Battle at midway midway +Science & Nature," A __________ keeps purring, no matter if it is inhaling or exhaling, a baffling accomplishment.",Cat,General,Salvador Allende former president of Chile was assassinated in what year,1973, History & Holidays,In 'Home Alone 2'' which city was Kevin lost at Christmas ,New York  +General,What is the Capital of: Czech Republic,Prague,General,"In 'dawson's creek', who does katie holmes play",Joey potter,Sports & Leisure,Who Won the Men's & Woman's Wimbledon Singles Title In 2007? (PFE) ,Roger Federer & Venus Williams  +General,Which West Bengal town is the centre of production of the tea called 'The Champagne of Teas' because of its grape aroma,Darjeeling,General,An isoneph on a map joins places of equal what,Average Cloud Cover,General,Who did John F Kennedy succeed as U.S. president,Dwight d eisenhower +General,Who was the second man to step onto the Moon in 1969,Buzz aldrin,General,"In Greek mythology, who was the Muse of Tragedy",Melpomene,Music,"""Crazy Train"" & ""Mr Crowley"" Were Songs From Whose ""Blizzard Of Ozz""",Ozzy Osbourne +Geography,In what country is the source of the blue nile ,Ethiopia ,General,Which human rights organisation founded 1961 got Nobel 1977,Amnesty International,Entertainment,To which elemetary school did TV's 'Brady Bunch' go?,Dixie Canyon Elementary +General,Pax was the Roman god of peace who's the Greek equivalent,Irene,Science & Nature,With which island is the puffin associated?,Lundy Island,General,What was the name of the first film where george burns played god,"Oh, god" +General,"In 1968, who invited you to Dance To The Music",Sly and the Family Stones,General,"At 9,970,610 km2, Canada is the world's second-largest ______",Country,General,How did Lawrence of Arabia meet his death in 1935,Motor cycle accident +General,Which Mediterranean island is named after a metal,Cyprus,General,"Thor is the god of thunder, loki is the god of _____",Mischief,General,Who originally patented the safety pin,Walter hunt +General,"In addition to writing novels, Jonathan Swift also wrote social and philosophical commentary. In one satirical piece, ""A Modest Proposal,"" what did he suggest should be made out of the skin of children?",Gloves,General,"In medical matters, what does the letter B stand for in B.C.G.",Bacillus,General,What is the young of this animal called: Hen,Pullet +General,"What deranged movie murderer has been dubbed a ""jaws on land""",Jason, History & Holidays,"Nanny, Sandra Rivett was the victim of which infamous murderer? ",Lord Lucan ,General,"What is the relatively constant, but dynamic internal environment necessary for life",Homeostasis +General,Pieces of vegetable coated in seasoned flour and deep fried,Pakora,General,What is a lacuna,A Space,Music,According To the Lyrics Of One Of his Songs Where Was Bruce Springsteen Dancing,In The Dark +Science & Nature,He designed the first feasible automobile with an internal combustion engine.,Karl Freidrich Benz,Geography,What Scale Is Used To Meausre The Intensity Of An Earthquake ,The Richter Scale ,General,What was Lestat's mother's name,Gabrielle +General,Quercus is the Latin name of what Tree,Oak,General,"In the 1984 movie ""Splash"", the pretty blonde mermaid chooses which street name to be her own?",Madison,General,Who recorded A Walk in the Black Forest in 1965,Horst jankowski +General,"In 'Back To The Future', where did Doc Brown get the plutonium to power the time-travelling DeLorean?",Lybian terrorists,General,"The Pearl River flows south from which major Chinese city, entering the South China Sea between Hong Kong and Macao",Canton,General,"Who was the editor of the magazine ""Babies"" when her husband was elected president in 1932",Eleanor roosevelt +General,Where could you spend a Sol,Peru,General,What is the metal part of a lamp surrounding the bulb & supporting the shade,Harp, Geography,This re-opened in 1975 after being closed for 8 years.,Suez Canal +Music,"Mark King Was The Lead Singer With Which UK Pop Outfit ""Level 42"" Or ""Big Country""",Level 42,General,"In Greek mythology, atlanta wanted to remain unmarried until ______",She was,General,In which part of the world is the EC dollar a unit of currency,Caribbean +Science & Nature," An adult walrus typically eats about 3,000 __________ per day.",Clams,Geography,_____________ is the largest Spanish_speaking country and the second_largest Roman Catholic nation in the world.,Mexico,General,Verminophobia is the fear of ______,Germs + Geography,Name a country which has the same name as a bird.,Turkey,General,What phase of moon precedes a full moon,Gibbous,General,Goyanthlay (one who yawns) famed under what Mexican name,Geronimo +General,"When Jonathan Edwards smashed the world triple-jump in 1995, by how much did he increase it",0.31 metres,Food & Drink,A green gelatinous substance known as calipee is used to make which favorite amongst gastronomes ? ,Turtle soup. Calipee is found beneath the lower shell of the Green Turtle. ,General,"The Rose Bowl, America's oldest college football contest, is held annually in which city",Pasadena +Music,Who Sang In A Pop Duo Alongside Mel Appleby,Kim Appleby,General,What shape is the head of an Allen key,Hexagonal,General,Where would you find a Mott Bailey and Keep,A Castle +General,In which city is the worlds longest skating rink - rideau canal,Ottawa Canada,General,Whic group won the inaugural 'Mercury' music Prize in 1992?,Primal Scream,General,What kind of fish is a 'porbeagle',Shark +Science & Nature," A mother __________ often gives birth while standing, so the newborn's first experience outside the womb is a 1.8_meter (6_foot) drop. Ouch!",Giraffe,Geography,Name the continent that consists of a single country.,Australia, History & Holidays,How Is St Sylvester's Day Otherwise Known? ,New Years Eve  +General,Table Tennis competitions only two coloured balls allowed what,White and Yellow, Geography,What is the capital of Virginia?,Richmond,General,What is a 'crossbuck',An x +General,Who wrote Last Tango in Brooklyn his third novel,Kirk Douglas,General,What does an average person use approximately six times per day,Bathroom,Food & Drink,What Is Calamari ,Squid  +General,Vincent Furnier is better known as who,Alice Cooper,General,What shape is Anelli pasta,Rings,General,What type of food is a Munster plum,Potato +General,To which tree family does the osier belong,Willow,General,Name Elvis Presley's father,Vernon Presley,General,What is the Capital of: Honduras,Tegucigalpa + History & Holidays,"""What brought Frosty the Snowman to life? (""""Pixie Dust, A Kiss,Magic Snow,An Old Silk Hat"""" "" ",An Old Silk Hat ,General,Capital cities: Mongolia,Ulaanbaatar,Entertainment,Who wrote the song 'Do They Know It's Christmas' with Bob Geldof?,Midge Ure +General,Artistic movement shared name with French for hobby horse,Dada thus Dadaism,General,What form of racial segregation was introduced in south africa in 1948,Apartheid,Sports & Leisure,In The Schoolboys Card Game Of The Same Name What Cancels Out A Black Jack ,A Red Jack  +Music,What Did Hot Chocolate Say It Sterted With In The 1970's,A Kiss,General,What animal can live several weeks without its head,Cockroach,Food & Drink,With What Food Might You Associate Arbroath ,Smokies (Kippers)  +General,Who dropped out of Harvard in 1975,Bill Gates,General,Aurore Dupin b 1804 changed her name what 19th cent author,George Sand,General,What is smocking,Needlework +General,"Art, science, & industry of managing the growth of plants & animals for human use",Agriculture, Geography,The Hebrides are part of this country.,Scotland,General,Who directed the musical 'My Fair Lady',George cukor +Sports & Leisure,Which country was represented by the athlete Don Quarrie? ,Jamaica ,Science & Nature,On what side should you sleep to improve digestion?,Right,Science & Nature," Many seabirds that swallow fishes too large for immediate digestion go about with the esophagus filled. Apparently without discomfort, the tail of the fish sticks out of the __________",Bird's mouth +General,"What actress said ""I acted vulgar - Madonna is vulgar""",Marlene Dietrich,General,"Who said: ""nice guys finish last""",Leo durocher,Entertainment,"Name the European hit, now an animated series about underwater people.",The Snorks +General,What Was The Name Of The Dog In Fraggle Rock,Sprocket,General,What was Margaret Thatcher's nickname?,Iron Lady,General,"In Soylent Green, what was the last resort used for riot control?",Scoups +General,Who breathes through spiracles,Insects,General,What was the name of the alligator on Miami Vice,Elvis,Geography,Which London Bridge Had A Gate Over It On Which The Heads Of Criminals Were Displayed On Spikes As A Deterrent ,London Bridge  +General,"Which eponymous, or title female, cartoon character was created by Max Fleischer",Betty boop,Entertainment,"On 'Dragnet', who played officer Bill Gannon?",Harry Morgan,General,The pop duo '2 Unlimted' are from which country?,Netherlands +General,What are the colours of the five olympic rings?,"Red, Blue, Green, Yellow, Black", History & Holidays,She overcame her handicaps to become a lecturer and a scholar.,Helen Keller,General,Who directed the film Born on the Fourth of July,Oliver stone + Geography,What city is associated with Alcatraz?,San Francisco,General,Who played garp's mother in the world according to garp,Glenn close,General,What was karl marx's term for owners of capitalist production,Bourgeoise +General,Who owned the dog called Peritus,Alexander the Great,General, The wide wall built along the banks of rivers to stop flooding is a(n) ________.,Levee,General,What is the official name of michael knight's car,Knight Industries 2000 +General,What is the fear of dolls known as,Pediophobia,General,What is the Capital of: Thailand,Bangkok, History & Holidays,In Which City Did The Indian Mutiny Start? ,Meerut  +General,Which is the highest mountain outside Asia,Aconcagua,Sports & Leisure,Which Boxer Sacked His Manager Frank Maloney In 2001? ,Lennox Lewis ,Geography,What london museum features a chamber of horrors ,Madame tussaud's  +General,Hormone used in treating inflammation and allergy,Cortisone,General,In North Carolina it is illegal for what to race down the street,Rabbits,General,What part of Betty Grable was insured for over a million dollars?,Her Legs +General,What is the worlds most polluted major city,Mexico City,General,"Who plays many voices, such as dr nick, and moe on 'the simpsons'",Hank,Sports & Leisure,What Colour Caps Are Worn By The Australian National Cricket Team? ,Green  +General,What was the capital of east germany,East berlin, History & Holidays,His wife was Roxana. His horse was Bacephalus. He was ________.,Alexander the great,General,John Huston scored a hit with his first film - what?,Maltese falcon + History & Holidays,Who invented crop insurance?,Benjamin Franklin,General,Who succeeded U Thant as Secretary General of the United Nations,Kurt waldheim,General,What presidents birthday is a national holiday,George washington +General,A pudding of stewed fruit under bread,Charlotte, History & Holidays,In 1921 Who Founded The First Birth Control Clinic In London ,Marie Stoppes ,General,"Who was Evander Holyfield's trainer talking about when he said, in 1991, ""Some say he's as fit as a fiddle, but he looks more like a cello to me"".",George foreman +General,With what day does a month start if it has a friday 13th,Sunday,General,Which freezes faster - hot or cold water,Hot,General,What country is accessed with the international telephone calling code 60,Malaysia +Music,Name 3 Of The Four Members Of The Classic REM Lineup,"Bill Berry, Peter Buck, Mick Mills, Michael Stipe",General,Where did Lord Byron die,Greece,General,The largest tree in the world grows in the U.S.; which tree is it,Redwood tree +General,Proverb: the early bird __________,Catches the worm,General,Which new engine regulation replaced the 2.5 litre rule at the start of the 1961 season,1.5 litre,Entertainment,What is the name of Jaleel White's character in the tv series 'Family ties'?,Steve Urkel + Language,"What three letter word means the same as ""to ingest""",Eat,General,The ice cream soda was invented in what year,1874,Music,"Who Duetted With Elton John On 1976's ""Don't Go Breaking My Heart""?",Kiki Dee +General,"In Korean, what does seoul mean",The capital,General,What was Butch Cassidy's original profession,Butcher hence Butch,General,The female side of a family tree is known as the distaff side. What is the male side called,Spear +Music,Which Declaration Brought The Beastie Boys Into The Charts In 1987,You Gotta Fight For Your Right To Party,General,"What U.S. academy is located in Kings Point, New York",Us merchant,General,What's the echidna's favorite food,Ants +General,The Underworld in Greek mythology,Hades,General,Bee Gees Movies: Neve Campbell and Courtney Cox are terrorized by a murderous horror movie fan in what movie,Scream, Geography,In what island group is Corregidor?,Philippines +General,Who was the first winner of the British version of the TV reality show 'Big Brother'?,Craig Phillips,General,"What in history were the Beaver, Dartmouth and Eleanor",Ships Boston Tea Party,Music,What Were The Names Of The 2 Gangs In West Side Story,Jets & Sharks +Sports & Leisure,"Who was football manager at Southampton, Rangers and Liverpool in the 1980's & 1990's? ",Graeme Souness ,General,We know what a fez is but what does fez mean in Turkish,Hat,General,What does a potometer measure,Water intake +Sports & Leisure,"Other than Alain Prost, which driver won three Formula One World Championships in the 80's? ",Nelson Piquet ,General,Annually 2500 left handed people die doing what,Using right handed products,General,"Fandible, lateral line, & dorsal fin are parts of a(n) ________",Fish +Music,What Was Dire straits 1982 Album Called,Love Over Gold,General,"What links Elvis Presley, Bruce Willis, Richard Gere",Married in Las Vegas,Science & Nature,Which bird has the best sense of smell? ,Kiwi  +General,What kind of animal was the now extinct Dodo,Bird,General,In the bible - Leviticus - what was lapidation,Death by stoning,General,Blue red green yellow four Olympic rings colour what's missing,Black +General,The character Lieutenant Pinkerton appears in what work,Madam Butterfly,General,What planet is considered the earth's twin in size & mass,Venus,General,"In ballet, a slow turn of the body on the whole foot.",Promenade +General,What term is applied to animals or plants that are not nocturnal,Diurnal,General,In 1971 which USA space probe was first to orbit another planet,Mariner 9,General,Sabastian Melmoth died in Paris 1900 better known as who,Oscar Wilde +General,Who said 'public service is my motto',Al capone, Geography,Where is the wailing wall?,Jerusalem,General,What is the 'cresta run',Toboggan course +General,Which group of Australian origin had a top twenty hit in 1965 with The Carnival is Over,The seekers,General,In medicine what do the letters HRT stand for,Hormone replacement therapy,General,What was the name of Barbie's first horse,Dancer +Science & Nature,What Colour Is The Australian Swan? ,Black ,Music,"Who Was The Lead Singer Of The Group ""Visage""",Steve Strange,Food & Drink,A black Russian cocktail is made with one part coffee liqueur and two parts of which spirit? ,Vodka  +General,The largest internal organ of the human body is,Liver,General,Peccatophobia is the fear of,Sinning,General,"Disease of animals, especially birds, monkeys, & humans, caused by infection by protozoans of the genus plasmodium & characterized by chills & intermittent fever",Malaria +General,What is measured in fathoms,Depth of water,General,"Robin, Rugby and Simple appear in which Shakespeare play",The Merry Wives of Windsor,General,What is the capital of the U.S. state of Oregon,Salem +General,What muscles provide about 200 pounds of force,Jaw muscles,General,What is the compulsive desire to give gifts,Doromania,General,To what do opposite faces of a dice always add up,Seven + Geography,What is the basic unit of currency for Belarus ?,Rubel,General,To whom are the Jews Gentiles,The Mormons,Toys & Games,What toy was originally made from the bladder of an animal?,Balloon +General,80% of Americans say they believe in what,Miracles,General,What is the Capital of: Guinea-Bissau,Bissau,General,What activity is featured in the magazine Winkers World,Tiddlywinks +General,What gift is associated with the 40th Wedding Anniversary?,Rubies,General,What is a baby whale called,Calf,General,In traditional wedding anniversaries what is given on the 14th,Ivory + History & Holidays,From what country did the U.S. buy the Virgin Islands?,Denmark,General,Proctophobia is the fear of,Rectum,General,What is the wife of a baron called,Baroness + Geography,What is the basic unit of currency for Uruguay ?,Peso,General,What calculating aid was invented by William Oughtred in 1662,Slide Rule,Science & Nature,Female Honeybees That Receive Royal Jelly Throughout Their Larval Stage Develop Into What ,Queens  +General,The Fagus is the Latin name of what type of tree,Beech,Science & Nature,What plant is opium derived from?,Poppy, Geography,In which city is the Canale Grande?,Venice +General,What does a heliologist study?,The sun,General,The museum of modern art in New York City hung Matisse's 'Le Bateau' upside down for how long before an art student noticed the error: How many days,Forty seven 47,General,Ei-Hajj Malik Ei-Shabazz better known as who,Malcolm X +General,What colour is the the Northern Line on the London underground?,Black,Geography,Khartoum is the capital of which country,Sudan, Geography,The City of Fireze is better known is english as what?,Florence +General,In Texas its illegal to shoot a buffalo from where,Hotel second story, Geography,What country is known as the Hellenic Republic?,Greece,Food & Drink,What is the name of the Scandinavian alcoholic drink made from Potatoes? ,Aquavit  +General,Isms: a painful stiffness of the muscles and joints,Rheumatism, History & Holidays,Which token vegetable is often included in the ingredients of a Christmas pudding? ,Carrot ,Science & Nature,"What Was The Colossus, Developed In Buckinghamshire In 1943 ",An Electronic Computer To Help Break Codes During World War 2  +Geography,___________________ has the largest rural population in the United States.,Pennsylvania,General,"A dance for two, usually a woman and a man. In its traditional form, it begins with an entreé and adagio, followed by solo variations for each dancer, and a coda.",Pas de deux,General,Type of moist aerated Italian bread,Ciabatta +General,"What links Catalonia, Andalusia, Cantabria, Galicia",Regions of Spain,Music,"What Was The Title Of The 1956 Musical Remake Of ""The Philadelphia Story""",High Society,Science & Nature,What Planet In Our Solar System Is Closest To Earth ,Venus  +General,What is the name of Jonny Quest's Dog,Bandit,Music,What Sort Of Girl Took Jamiriquai To No.6 In 1996,Cosmic Girl,General,Which state became the 14th state of the u.s,Vermont +Sports & Leisure,Which Country Has Had The Most Wins At The Angling World Championships ,France ,General,What constellation is represented by a goat,Capricorn,General,What company made the first color arcade game?,Atari +General,Betsy Ross is the only real person to ever have been the head of a ______?,Pez dispenser,General,What would be kept in a humidor,Cigars,General,Who directed 'the godfather',Francis ford coppola + History & Holidays,Who was the first dog in space ?,Laika,General,A person with a strong desire to steal is a(n) ________.,Kleptomaniac,General,"In golf, we call '3 under par' an 'albatross'. What do Americans call it",Double eagle +General,Nipper is the RCA dog in the US what's he known as in the UK,HMV dog,General,What veteran of tv commercials died at the advanced age of 17,Morris the cat,Science & Nature,What is the meaning of the name of the constellation Leo Minor ?,Lesser Lion +General,What is the significance of the moth found in the Harvard Mark I computer,"First computer ""bug""",Geography,Where is the Admirality Arch,London,Science & Nature,A __________ can go without water longer than a camel can.,Giraffe or Kangaroo Rat +General,A large French country house,Chateau,General,Where did Spam get its name,Spiced Ham,General,Give one of Michael Portillo's other Christian names.denzil,Xavier +Art & Literature,A Russian abstract movement originated by Malevich c. 1913. It was characterized by flat geometric shapes on plain backgrounds and emphasized the spiritual qualities of pure form. ,Suprematism,General,Who was winnie the pooh's neighbor,Piglet,General,Which planet is named after the sky God in Greek mythology,Uranus +General,All living things contain what,Water,General,In what movie did Richard Drefus make his one line debut,The Graduate,People & Places,Whose Real Name Is Eric Clap ? ,Eric Clapton  +General, What is the common name for the Aurora Borealis,Northern lights,Music,Shortly After Discovering His Brother Was A TV Detective Who Died In FEb 1997,Brian Connolly,General,"In Roman mythology, who was the father of Romulus and Remus",Mars +Sports & Leisure,What do runners pass to each other in a relay race?,Baton,General,With which instrument is the musician Dennis Brain associated,French horn,General,Who recorded 'hejira' in 1976,Joni mitchell +General,Which planet did John Couch Adams and Urbain Leverrier work out the existence and position of before it could actually be seen,Neptune,Geography,What is the capital of Sweden,Stockholm,Food & Drink,"After 1066 the French introduced such joys as cidre to England with 'staggering' success. If pears replaced apples in the process, what was it called ? (Clue, if needed, a famous lawyer.) ",Perry  +General,What was the worlds first patented synthetic food in 1869,Margarine,Food & Drink,What do you get when you add fresh fruit to red wine?,Sangria,General,Rogers what film features the song 'born free',Born free +General,In what Australian state would you find Fremantle,Western australia,General,What does the columella separate,Nostrils,Music,Who Was Was Ther Lead Singer With Cockney Rebel,Steve Harley +People & Places,Formerly known as East Pakistan what is this country now called ,Bangladesh ,General,A Greek type mandolin,Bouzouki,General,In what Australian state would you find Maitland,New south wales nsw + History & Holidays,Live Aid was to benefit which starving country? ,Ethopia ,General,"Game show: before she became the head-turning letter turner on wheel of fortune in 1982, vanna white appeared as a contestant on what show?",The price is right,Science & Nature,Which Disease Is The Most Widespread ,Tooth Decay  +General,Jr how many tunes blared from the 1948 wurlitzer model 1100 jukebox,24,General,What soul great appears in the flick Ski Party,James brown,General,As what is the Bowery known,Street of forgotten men +Toys & Games,"This chess term means ""in passing""",En passant,People & Places,"Whose Last Words Were Reputedly, 'ET TU BRUTE' ? ",Julius Caesar ,General,Complete the title of this Johny Cash song 'A Boy Named___.,Sue +Music,What Song Was Christmas Number One In The UK In Both 1975 And 1991?,Bohemian Rhapsody By Queen,General,What new york city avenue divides the east side from the west side?,Fifth avenue,General,Which british colony was returned to the Chinese in 1997,Hong kong +Entertainment,Which of Paul Simon's musical characters was told to hop on the bus?,Gus,Music,"Emerson Lake And Palmer Had A No.2 Hit With ""Fanfare For The Common Man "" Who Wrote It",Aaron Copland,General,Cereology Is The Study Of What,Crop Circles +General,What martial arts name means gentle way,Judo,General,What is the Japanese Shinkasen,High speed Train,General,What is the Roman Numerals for 4000,MMMM +Music,Which Group Sat Down In The Top Ten Twice,James,Music,Who Was The First Person To Play Evita On Stage,Elaine Paige,General,What sequence is this the start of: 1 2 4 8 16 32 64,Power of 2 +General,Arnold Cream was a famous (early) boxer - who,Jersey Jo Walcott,General,What is the lowest rank of the british nobility,Baron,General,What Is Unusual About a Polydactyl Person,More Than 10 Fingers Or Toes +Sports & Leisure,Who was voted BBC's Golden Sports Personality for the last 50 Years in 2003? ,Steve Redgrave ,General,Which English porcelain factory used an anchor as its mark,Chelsea,General,What city is at the mouth of the Menam river,Bangkok + History & Holidays,What Shakespearean king was actually king of Scotland for 17 years?,Macbeth, History & Holidays,In Which Century Did Lady Godiva Live? ,11th Century ,General,What is the common name for an integrated circuit,A Chip +General,J D Sallenger wrote Catcher in the Rye what's the J D stand for,Jerome David,General,What is a hoblet,A Vasectomised ferret,General,What is the most air polluted city in the united states,Los angeles +General,Which tough guy actor was once a drop hammer operator,Robert Mitchum,General,"Queen Berengaria never came to England, although she was married to the King. Which King",Richard the first, Geography,The Thatcher Ferry Bridge crosses what canal?,Panama Canal +General,What is the flower that stands for: death preferrable to loss of innocence,White rose,General,When is Saint George's day celebrated,April 23rd,General,What do chimpanzees do when nervous,Masturbate +General,"What part of an eatery do some restaurant owners call ""the cancer ward""",The smoking section smoking section,General,Fear of one's mother in law,Pentheraphobia,General,What was discovered in 1922 by Howard Carter,Tutankamen tomb +Sports & Leisure,In Australian Rules Football How Many Players May A Team Have On The Field At One Time ,18 ,General,"According to an old proverb, what is 'the soul of wit'",Brevity,General,Whets the difference between fog and mist,Seeing Distance under 1000yd +General,"In boy meets world,what is the crazy older brother's name?",Eric,General,"Often eaten for breakfast, the egg comes from what barnyard animal",Chicken,General,Dionea Muscipula is the Latin name for which flesh eater,Venus Fly Trap +General,What element is lacking in a diet causes goitre,Iodine, Geography,What is the basic unit of currency for Paraguay ?,Guarani,General,Encephalopathy what is the richest country,Switzerland + Geography,What is the capital of Mauritius ?,Port Louis,Music,"What Contribution Was Made By Tony Gilbert, Sidney Sax, Kenneth Essex & Francisco Gabarro To The Recording Of The Help! Album",String Quartet On Yesterday,Science & Nature," The blue whale weighs as much as thirty elephants, and is as long as three __________",Greyhound buses +General,Charles Henry Stuard Gmelin was the first UK what 6 Apr 1896,Olympic competitor 4th 3rd heat 100 m,General,Approximately how many species of butterfly are there,"100 thousand 100,000",General,Who was the first to speak to Jesus after he had risen from the dead ,Mary Magdalene  +General,"In motor racing, what is yellow",Danger flag,General,Who advocated planting peanuts and sweet potatoes to replace cotton and tobacco,George washington carver,General,What is the Capital of: Costa Rica,San jose +Food & Drink,"The French call it Sabayon, what do the Italians call it? ",Zabaglione ,General,"What family do these fruits belong to - Kumquat, Pommelo, Ugli Fruit?",Citrus,Art & Literature,Which Literary Character Had A Dog Called Bulls Eye? ,Bill Sykes (Oliver!)  +General,Nylon was invented in 1934 what product first used it,Toothbrush,Sports & Leisure,In The World Of Snooker Disappeared In 1991 & Was Brought Back In 2005 ,Pot Black ,Science & Nature,Which Part Of A Computer Is Responsible For Carrying Out Instructions ,CPU / Processor  +Science & Nature,Which Significant Beverage Did Dr John Pemberton Of Atlanta Georgia Invent In 1886 ,Coca Cola ,Sports & Leisure,Where were the 1908 Olympics held ?,"London, England",General,What does a cooper make,Barrels +Sports & Leisure,In Professional World Cup Rugby How Many Minutes Does The Game Actually Last ,80 Mins ,General,Who preceded Bobby Robson as England Football Manager,Ron greenwood, History & Holidays,Who invented the ballpoint pen?,Georgo and Laszlo Biro +General,"Which Country Did The 1998 Eurovision Song Contest Winner ""Dana International"" Represent",Israel,General,"In Ferris Buellers Day Off, who is Cameron going to marry?",The first girl he lays,General,In Peter and the Wolf what instrument represents the cat,The Clarinet +General,"What is the common name for a ""canis lupus""",Timber wolf,General,Boned steak cut off sirloin,Entrecote,Music,Name Wayne Fontana's Backing Group,The Mindbenders + History & Holidays,How Did UN Secretary General Dag Hammarskjoeld Die In September 1961 ,In An Air Crash ,General,In what country is the language Fanti spoken,Ghana,General,In Bristol Rhode Island its illegal to smoke during what event,Public Hanging in town square +General,Engine that employs gas flow as the working medium by what heat energy is transformed into mechanical energy,Gas turbine,General,What science deals with the origin and structure of the universe,Cosmology,Food & Drink,"What is a `kiwano', a spiky orange fruit, a fish dish, or a cocktail? ",A spiky orange fruit.  +General,Approximately what percentage of the earth do the oceans cover,71%,People & Places,Which Actors Real Name Is Morris Micklewhite ,Michael Caine ,People & Places,Thomas Hamilton A Former Scout Master Shocked The Nation On 13 th March 1996 But Why ,Dunblane Massacre  +General,In which European country is the world's deepest known cave,France,General,What is the fastest fish in the world,Sailfish,General,Who sailed in a ship called Queen Ann's Revenge,Blackbeard +General,The Composer Mozart Wrote The Music To Which Popular Tune?,Twinkle Twinkle Little Star,General,J Worthington Foulfeather was the name of what Disney character,The Fox in Pinocchio,General,Whose ghost haunted scrooge with clanking chains and wierd sounds,Jacob +General,A is Alpha is the international alphabet but A used to be what,Able,General,In what American State is the city of Nevada?,Missouri,General,What's the soft tissue inside bones called,Marrow +General,What is the Capital of: Sierra Leone,Freetown,General,A tropical house lizzard,Gecko,General,Crab is the only named one in any Shakespeare play - what,Dog - Two Gentlemen of Verona + Geography,What is the basic unit of currency for India ?,Rupee,Entertainment,Who wanted 'a new drug'?,Huey Lewis and The News,Sports & Leisure,At what age did Will Carling first become England skipper? ,22  +General,The Zoastrian religion began in what country,Persia or Iran,Science & Nature,What is a young goose called?,Gosling,General,In Which City Was The Singer Lonnie Donegan Born?,Glasgow + Geography,What was the former name of Thailand?,Siam,General,In which game is banjo and tooty,Banjo-kazooie,Sports & Leisure,In What Game Might You Get A Cannon ,Billiards Pool Or Snooker  +Science & Nature,Which video game series is sometimes referred to as GTA? ,Grand Theft Auto ,General,What is the fear of teeth or dental surgery known as,Odontophobia,General,"If a bear lives in the southern hemisphere, the opening of the cave in which he hibernates is always on which slope",South +General,Who played Beau Geste in the 1939 film,Gary Cooper,General,"The word ""cumulus"" refers to a type of ___________",Cloud,Entertainment,"What was the name of the motel in the film ""Psycho""?",Bates Motel +General,What is the second largest state in the u.s,Texas,Music,The Lover Speak's Only Chart Entry Was Later Covered By Annie Lennox . What Was It Called,No More I Love You's, Geography,On what river is Blackpool?,River Fylde +General,What is the scent of an artificial hare at greyhound tracks,Anise,General,Photographic inventor suicided 1932 said my work is done why wait,George Eastman Kodak Brownie,General,"What U.S. president was known as ""the rail splitter""",Abraham lincoln +General,Potamophobia is the fear of,Rivers running water,General,What's the capital of Syria,Damascus,General,Who's first play was The Room,Harold Pinter +General,What is a group of this animal called: Cur,Cowardice,General,Spectrophobia is the fear of,Specters ghosts,Music,With whom did Westlife team up with on the 2000 single ‘Against all Odds’?,Mariah Carey +General,Who invented the hamburger,Louis lassen,General,What makes up about 85% of all the greenery on earth,Ocean plants,General,What Object Was Bought By The Yasuda Fire And Insurance Company Of Tokyo For A Record 24.75 Million Pounds In 1987,Van Gogh's Sunflowers +Science & Nature,What device is used to measure weather pressure ?,Barometer,General,"What three words mean the same as 5,880,000,000,000 miles",One light year, Geography,If you climbed the Euromast what country would you be in?,Rotterdam +General,What was invented by James Dewer in 1872,Vacuum or thermos flask,General,Rene Laennac invented which aid for doctors in 1810,Stethoscope,General,What name is given to a substance in which resistance decreaces as temperature increases,Semiconductor +General,Almost half the bones in your body are in what two body parts,Hands & feet,General,What is a resident of liverpool?,Liverpudlian,Music,Name Depeche Modes First Album,Speak & Spell + History & Holidays,In which English town was the Co-Operative Society formed in 1844? ,Rochdale ,General,What is the registry number of the enterprise in the original star trek,Ncc,General,Who recorded the tune 'dream weaver',Gary wright +General,In February 1990 160 million bottles of what were withdrawn,Perrier – contaminated benzine,General,Guiyaquil is the largest city in which country,Ecuador,General,Who sang about 'the bugle boy of company b',Andrews sisters +General,How many eyes are there in a deck of 52 cards,Forty two,Art & Literature,In what opera would you find Lt. Pinkerton?,Madame butterfly,General,Manticore was a mythical beast head of a man body of what,A Lion +Music,"Their Debut Album was Called ""Lexicon In Love"" Who Was The Band",ABC,Geography,What is the capital of Papua New Guinea,Port moresby,Science & Nature," __________ eels are not really eels but a kind of fish. Although they look like eels, their internal organs are arranged differently.",Electric +General,A spool or reel for thread,Bobbin,General,Anonymous letters of hostility towards the recipient,Hate mail,Sports & Leisure,What Is Denise Lewis Main Athletic Event ,Heptathlon  +Sports & Leisure,In Which Sport Would Win The Federation Cup ,Womens Tennis ,Music,"Who Wanted To Know ""Have You Ever Been In Love""",Leo Sayer,General,Gabriel Fallopius is credited with inventing what,Condoms +General,What is a 'kiwano' a type of,Fruit,General,"What FBI agent tracked Charles ""Pretty Boy"" Floyd to Ohio, where Floyd died",Melvin purvis,General,Which nazi leader had his 6 children poisoned prior to his own death,Goebbels +Music,Tammi Terrel Died From A Brain Tumour 3 Years After Collapsing In The Arms Of Another Motown Singer Who Was He,Marvin Gaye,General,Whets the correct name for golf club called Texas Wedge,Putter,Food & Drink,"Generally speaking, on a restaurant menu, what might be presented in the style referred to as Macedoine? ",Vegetables or Fruit  +General,"Which book has the statement 'all pigs are equal, but some pigs are more equal than others'",1984,Science & Nature,What is the only other animal besides humans to have unique prints?,Koala Bears,Music,Alex Turner Is The Lead Singer With Which Band?,Arctic Monkey's +General,For whom did colonel tom parker act as manager,Elvis presley, History & Holidays,What was the nationality of Marco Polo ?,Italian,General,What is the worlds largest inland sea,Caspian sea +Toys & Games,How many squares are there on a chessboard,64,General,What is a Shofar,Carved Rams Horn Jewish faith,General,What French word means liquor is half frozen,Frappe +General,What's the adhesion of molecules to the surfaces of solids called,Absorbtion,General,"Who was the head villian on ""The Smurfs"" and what was his cat's name?",Gargamel and Azreal,General,"Due to precipitation, for a few weeks k2 is bigger than",Mt everest +Geography,Why Is The Golden Gate Bridge Painted In International Orange ,The Colour Helps Visibility In The Frequent Early Morning Fog ,General,What is the flower that stands for: artifice,Acanthus,Music,Who Had A Number One Single With A Song With A Song About A Rat,Michael Jacksons (Ben) +General,The wallendas were noted as,High wire performers,General,Hartford is the capital of ______,Connecticut,General,Athropod with worm like body and many legs,Centipede +General,Which relative of John Travolta's made a cameo appearance in Saturday Night Fever?,His mother,General,The Sydney Olympics had 3 mascots. What type of animals were they?,Kookaburra Echidna Platypus,Food & Drink,These beans are the most often used in the production of bean sprouts.,Mung beans +General,What march did Felix Mendelssohn compose,Wedding march,General,What island do most of the North Atlantic icebergs come from,Greenland,Music,Amount that the Fab Three were recently offered to play 10 concerts,$100 million +General,"Who was the first person to reach the North Pole, in 1909",Robert peary,Food & Drink,What is made of fermented grape juice?,Wine,General,Ouagodougou is the capitol of what country,Bakina Faso +General,A belemnoid is what sort of shape in zoological terms,Dart shaped,General,Who was the last prisoner in the Tower of London,Rudolf Hess,General,What term describes a pain in the chest usually caused by lack of oxygen,Angina +General,In What UK Town Did The First Branch Of The Body Shop Open,Brighton,Science & Nature,What is the meaning of the name of the constellation Camelopardalis ?,Giraffe,General,Hodophobia is the fear of,Road travel +General,What is the most common plastic surgery done on US men,Breast Reduction,General,N is the civil aircraft marking for which country,USA,General,How many lines does a sonnet have,Fourteen +General,Churchill what are phalanges,Finger bones,General,Where might you spend a Ceti - Capital Accra,Ghana,Food & Drink,What are Pershore eggs and Marjorie's seedlings? ,Plums  +General,Vaselina and Brillantino were alternate names which film,Grease, Geography,What is the basic unit of currency for Samoa ?,Tala,General,What is the flower that stands for: rendezvous,Chickweed +General,From what did julius caesar and napoleon suffer,Epilepsy,Religion & Mythology,Who is the greek equivalent of the roman god Jupiter,Zeus,Music,"What Is The Surname Of The Brothers Who Wrote, Among Others, Chim Chim Cheree, Chitty Chitty Bang Bang & The Bare Necessities",Sherman +General,Plate tectonics theory contends there are how many major plates,Seven,General,In 1800 Free black commission of Philadelphia petitioned Congress to abolish,Slavery,Sports & Leisure,Which American Football Team Are Known As The Falcons ,Atlanta  +Music,"Who Had A 1983 Hit With ""Si Si Je Suis Un Rock Star""",Bill Wyman,Science & Nature,What Is The Technical Term For Memory Loss ,Amnesia ,General,Who staged a major musical comeback with his 1988 album Cloud 9,George +General,In the Bible who came from Gath,Goliath,General,Which is the only country that has every type of climate,New zealand,Art & Literature,Which Author Penned The Disc- World Series Of Sci-Fi Novels? ,Terry Pratchett  +General,What toe is the foot reflexology pressure point for the head,Big toe,General,Vanilla is part of which plant family,Orchids,General,Who released 'tell him' in november 1962,The exciters + History & Holidays,What type of animal is a caribou? ,A Reindeer ,General,What is a resident of moscow,Muscovite,General,There is enough iron in a human being to make ______,One small nail +General,In Greek mythology the place of ideal happiness,Elysium,General,What does 2thwrk do for a living,Dentist,General,What 4 Words Are Written On The Tombstone Of Mel Blanc,That’s All Folks +General,Gwizador in Poland is who in English,Santa Claus,General,What popular bird derives its name from Abo for Good to Eat,Budgerigar,Geography,In which state is the kennedy space center ,Florida  +General,"Who sang the song ""I'll Be Missing You""?",Puff Daddy,General,Indian fig tree with self-rooting branches,Banyan,General,"Teresa Quotations: ""Do one thing at a time, and do that one thing as if your life depended on it.""",Eugene Grace +General,The lowest elevation in the usa is,Death valley, History & Holidays,"This war began on June 25, 1950.",Korean,Music,"Who Sang The Song ""The Lion Sleeps Tonight"" In The 80's",Tight Fit +Geography,"The city name ________________ is derived from an Algonquin word meaning ""traders.""",Ottawa,General,Which Lakeland poet was born in 1770,William wordsworth,General,Which actress starred in the film Love Story,Ali mcgraw +General,Alma Mater means what,Bountiful mother,Entertainment,What is Hulk Hogan's real name?,Terry Bollea,Geography,What is the world's highest city,"Lhasa, tibet" +Entertainment,Who said 'you'd be surprised how much it costs to look this cheap'?,Dolly Parton,General,A dolphin can remember a specific ______ better than a human,Tone,General,Marge Simpson has the same maiden name as a former First Lady. Name the former First Lady,Jackie Kennedy +General,"What shouts 'tip me over,pour me out' in a children's song",Little teapot,General,"Contagious disease of warm-blooded animals, including humans, caused by the bacterium Bacillus anthracis",Anthrax,General,As what was Anne Bonney notorious in the 18th century,Pirate +General,"Which character did Berlioz, Gounod and Liszt all compose music about",Faust,Science & Nature,What Electrical Device Can Change The Magnitude Of Voltage Or Current ,A Transformer ,General,Riotous Assembly' was the first novel by which author,Tom sharpe +General,What is the study of weather technically called?,Meteorology,General,What measures wind velocity,Anemometer,General,What colour is caffeine,White powder +General,Who was & romedas mother,Cassiopeia,General,Which Northumberland castle is claimed to be the second largest inhabited castle in the world,Alnwick,General,"""Gotta have 'em"" is this cereal's slogan",Corn pops +Music,Who Was The Lead Vocalist With Haircut 100,Nick Hayward,General,President Woodrow Wilson May 6th 1919 first to do what,Take out Air accident Insurance,General,The royal 'House of Savoy' ruled which country from 1860 to the end of Word War II?,Italy +Sports & Leisure,In What Context Did Thierry Replace Ian Who Had Previously Replaced Cliff ,Leading Top Scorer for Arsenal ,General,Garuda is the national airline of which far-eastern country,Indonesia,Art & Literature,"A print made by carving on a wood block, which is then inked and printed.",Woodcut +Food & Drink,How many standard bottles of wine are in a Methuselah ,Eight ,General,Small rectangular instrument played by blowing and sucking air through it,Harmonica,Entertainment,Who began his career with 'The Yardbirds' and established himself as one of the best rock guitarists of his generation?,Eric Clapton +General,Country singer vince ____,Gill,General,Cheval-vapeur in France is equal to what in English,Horse power,General,Which country makes panama hats,Ecuador + Geography,What country is Phnom Penh the capital of?,Cambodia,General,What year did Yuri Gagarin become the first human in space,1961,Music,"Who Had A 1984 Hit With The Song ""The Longest Time""",Billy Joel +Sports & Leisure,What Is The First Event In Three Day Eventing ,Dressage ,General,"A gigantic mass of snow and ice, mixed with stones and earth, which falls from the mountains into the valleys is called a ______",Avalanche,General,Radiophobia is the fear of,"Radiation, x-rays" +General,What is the Jack of Hearts holding up in a deck of cards,Leaf,General,What is the name of the ridge seperating two glacial valleys,Arete,General,In Slovenian if you heard Na Mesta Pozor Zdaj what sport,Athletics - Marks set Go +General,Who played the title role in the 'mad max' series of films,Mel gibson,General,Who developed the first jet fighter,Germany,General,What is the Capital of: Saint Pierre and Miquelon,Saint-pierre +Music,Who Was The Reggae Legend Who Died From Cancer In Miami Florida On 11th May 1981,Bob Marley,General,In the Bible the good Samaritan was travelling to where,Jericho,Sports & Leisure,What are large snow bumps known as in skiing terms,Moguls +General,With which instrument is jazz musician Charlie Mingus principally associated,Bass, Geography,What is the capital of Sierra Leone ?,Freetown,General,The dunnock is another name for which common bird,Hedge Sparrow +General,Mary Donaldson was the first woman to hold which post,Lord mayor of london,General,Chrometrophobia is the fear of what,Money,General,"Which East Anglian county shares its name with a city in Virginia, USA",Norfolk +General,What boy scout merit badge is earned most often,First Aid,General,"When It Comes To Flowers Especially Roses, What Colour Is The Rose The Silver Jubilee ?",Pink,General,In 1958 who had a pop music hit with 'willie and the hand jive',Johnny otis +People & Places,What Radio Program Did Roy Plomley Dream Up ,Desert Island Discs ,General,How did Marvin Gaye die,Shot by his father,General,Which author wrote screenplay Bonds You Only Live Twice,Roald Dahl +General,"The creature Hirudo medicinalis was used extensively by doctors, what is it",Leech,General,Who sang 'beauty and the beast',Celine dion,General,In Auburn Washington men can get five years for doing what,Deflowering Virgins +Music,Pink Floyd Featured A Chorus Of Pupils From A North London School For Which Of Their Hits,Another Brick In The Wall,Science & Nature,What is the chemical symbol for radium?,Ra,General,By law in Boston what is banned from the back seat of a car,Gorillas +Science & Nature,Which Model Ferrari Has A Name Which Means Redhead ,Tetarossa ,Science & Nature,What Theory Did Darwin Propose ,Theory Of Evolution ,General,What does a herpetologist study,Reptiles and amphibians +General,"Which Brand Name (Often Seen In The High Street) Get's It's Name From the Finnish For ""Wild Grass""",Timotei,General,What are the three colours on a roulette wheel,"Black, green & red",General,What Was The Name Of The Founder Of The Mcdonalds Chain,Ray Croc +General,Which disease is now known as Hansen's disease,Leprosy,Geography,"What notable geographical feature is shared by Oxford, Reading, Windsor and London? ",The River Thames ,General,What was the trademark of mobile gasoline,Flying red horse +General,"Brilliant red or black mineral, with diamondlike luster, composed of titanium oxide, tio2",Rutile,General,"An ""omniscient"" person has unlimited __________.",Knowledge, History & Holidays,Which quirky brunette was one of the first MTv VJ's? ,Martha Quinn  +General,What Sport Very Popular In The USA Was Created In 1973 After A Discussion By A Group Of Locals In A Pub In Wisborough Green Near Horsham In West Sussex,Lawn Mower Racing,General,Word for slight of hand comes from the French for nimble finger,Prestidigitation,General,In what Australian state would you find Salisbury,South australia +General,"What do Julius Caeser, Ghandi and Trotsky have in common",All assassinated,General,Which metal is the main constituent of Gunmetal?,Copper,Science & Nature,What name is given to a female swan ,Pen  +General,What is the US slang term for formal male evening dress,Monkey Suit,General,What Greek runner of 5 bc is said to have run 241 km in 48 hours to summon help for Athens,Pheidippedes,General,"Born Yaron Cohen, to the dismay of a number of her fellow countrymen, she won which international competition in May 1998",Eurovision song contest +General,Who is gaylord the buzzard's buddy,Broomhilda, History & Holidays,Which nursery rhyme was the first gramophone recording?,Mary Had A Little Lamb,General,What do you call a creature that is going to change into a frog,Tadpole +General,Who played the alien in Predator jumping and climbing scenes,Jean-Claude Van Damme,Music,What is Paul's McCartneys middle name?,Paul,General,"What is 9 metres high, 7 metres wide & 2,500 kilometres long",Great wall of china +General,Victoria is the only Australian state without what,Letter S in name,Science & Nature,Which Colours Are Most Commonly Confused In Colour Blindness ,Red And Green ,General,An algophile loves what,Pain +Entertainment,"Name the band - songs include ""Heart of Glass, The Tide is High""?",Blondie,General,"In the movie ""Rainman"", what was the only airline that Raymond said had never crashed?",Quantas, History & Holidays,"Who Coined The Phrase 'Turn On Tune In, Drop Out'' ",Timothy Leary  +General,Ivan Maugher won six world titles at what sport,Speedway,General,What is the commonest name used in London streets,Victoria,General,In the 1920s what was a Chicago Overcoat,A coffin +Music,"In Which Movie Did Madonna Play The Character ""Gloria Tatlock""",Shanghai Surprise,General,Where would you have found Binky Inky Pinky and Clyde,Pac Man Ghosts,General,What bean is used in the production of chocolate,Cocoa +General,What Bird Lays The Smallest Eggs,Hummingbird,Music,Which Suitable Named Cars Single Re-Entered The Charts In 1985 Reaching No.4,Drive,General,What is the minimum number of degrees in an obtuse angle,91 degrees +General,What was the first film musical based on a Shakespeare play,The boys from Syracuse C of Error,Art & Literature,Painting in which natural scenery is the subject. ,Landscape,Science & Nature,What does a camel store in its hump,Fat +General,What was Dr. Zhivago's first name,Yuri,General,Small polecat used in catching rabbits and rats,Ferret,General,What U.S. state boasts the most gasoline stations,California +General,The Hawaiian alphabet has how many letters,12,General,My Word were the final words of which famous TV character,James Tiberius Kirk,General,Which Country Was The First To Introduce A Driving Test,France +Music,Raymond O Sullivan Changed His Name To Become Who,Gilbert O Sullivan,Science & Nature,Which freezes faster - hot or cold water?,Hot,General,What does 'HTTP' stand for,Hypertext Transfer Protocol +General,Barrel size - what wine barrel size contains 10 gallons,Anker,General,After whom is the month of July named,Julius caesar,General,Who invented crop insurance,Benjamin franklin +General,In 1961 which Henry Mancini record won Grammy record of year,Moon River,Science & Nature,What Is The Name Of The Russian Born US Pioneer In Aircraft Design Who Is Best Known For His Successful Development Of The Helicopter ,Igor Sikorsky ,Science & Nature,What is the meaning of the name of the constellation Cassiopeia ?,Cassiopeia +Music,Name The Album for Kula Shaker Titled After A Single Letter From The Alphabet,K,General,A piece of soft leather from sheep or goats,Chamois,General,Who wrote the song of songs,Solomon +General,"Who recorded the album ""business as usual"" in 1983",Men at work,General,Who were lucy and ricky's next door neighbours and best friends,Fred and, History & Holidays,"In 1959, Tibet was invaded by which country?",China +General,How many days did the first successful trans-atlantic balloon flight take,6 days,General,What product did the first commercial in the USA advertise,Bulova Watches,General,In Germany what can you not wear during a strike,A Mask +General,In 1974 Somalia created its first ever what,Written language,General,Starliters what's the only video game that became a television show,Pac-man,General,No US president has ever been what,An only child + History & Holidays,Who Released The 70's Album Entitled Madman Across the Water ,Elton John ,General,What was the name of the film star Gene Autry's horse,Champion,General,In 1951 the shortest ever lasted 2.5 minutes - shortest what,Boat race Oxford sank +General,The teeth used for biting or cutting are known as _______,Incisors,General,What's Margaret Houlihan's nickname,Hot lips,General,For how much did peter minuit buy manhattan island,24 dollars +General,What can't roosters do if they can't fully extend their necks,Crow,General,What hereditary defect of vision is sex-linked affecting more men than women,Colour blindness,General,"Which Fruit Is A Cross Between A Tangerine, A Grapefruit And An Orange?",An Ugli Fruit +Tech & Video Games,What is the reason behind the layout of the Qwerty keyboard ?,To slow down typing rates,General,What light operas name literally means Honourable Gate,The Mikado,Geography,"From 1835 - 1843 14,000 Dutch Boers Moved Inland To Set Up New Colonies, What Was This Journey Called ",The Great Trek  +General,Which tyre company withdrew from formula 1 at the end of 1986,Pirelli,General,Ray WilkingsWas The First English Player To Be Sent Of In A World Cup Final But Which Country Were England Playing At The Time,Morocco, Geography,What is the basic unit of currency for Singapore ?,Dollar +General,What Is The First Name Of Inspector Morse In The Tv Series,Endeavour, History & Holidays,"Which 60s sex symbol was, in film at least, attacked by anti-bodies ? ",Rachel Welch ,General,What would you do if someone gave you a Twank,Drink it - it's tea +General,"According to all four Gospels the precursor of Jesus Christ, born in Judea, the son of the priest Zacharias and Elisabeth, cousin of Mary, the mother of Jesus?",John the baptist,General,Who had the motto Non Sans Droit - not without right,William Shakespeare,General,How many calories are there in a glass of water,0 +Entertainment,Who played Dr. Frankenfurter in the pop-culture film 'The Rocky Horror Picture Show?,Tim Curry,General,Who shot J.R. Euing?,Kristin Sheppard,General,How long does it take a fully loaded supertanker to stop from travelling at normal speed,20 minutes +General,The first spaghetti western starring Clint Eastwood was made in what year,1964,General,In which 1974 disaster movie did fireman Steve McQueen need a long ladder,The towering inferno,Sports & Leisure,Hockey: The Vancouver ________.,Canucks +General,"What animal may be stag, rhinoceros or dung",Beetle,General,Where is Kloster beer brewed,Thailand,General,Andy Fletcher Dave Gahan Martin Gore Alan Wilder what group,Depeche Mode +General,What links Steve McQueen Ian Botham Spike Milligan,All called Terence, Geography,Which U.S. city is known as Beantown?,Boston,General,What was used at Wimbledon for the first time in 1971,Tie Break System +General,What was the name of the show that featured Sniglets?,Not Necessarily The News,General,What is cuneiform,Writing system,General,Who wrote the childrens classic 'The Borrowers',Mary norton +General,Baseball: The San Diego _______?,Padres,General,Whose daughter became the wealthiest three year old in 1988,Christina,General,What county first used pepper,China +General,Where on a woman is her J spot,Nape of the neck,General,Which Mediterranean Country Has The “Livre” As It's Basic Unit Of Currency?,Lebanon,General,In what game/sport is the McRobinson shield played for,Croquet + History & Holidays,Who discovered the Grand Canyon?,Francisco Coronado, History & Holidays,"In what country did ""Sepoy Mutiny"" occur",India,General,Name the columnist: 'At Wit's End',Erma Bombeck +General,What is another name for the sport of logrolling,Birling,Music,"Who Had A No.1 With ""She Wears Red Feathers"" In 1952",Guy Mitchell,General,The Kremlin is located in this city?,Moscow +General,Inches who at buckingham palace wears bearskins,Guards,Food & Drink,Which contribution to western tea culture was introduced in New York restaurants in 1908 ,Tea Bags ,Science & Nature,What Is The Most Common UK Bird? ,The Blue Tit  +General,In what modern country is mount Ararat,Turkey,General,Bearbrass founded by John Batman the original name of where,Melbourne,General,For which 1949 film did Anton Karas write and perform the music,The third man +General,What kind of birds are most commonly raced,Pigeons,General,Which dancer found guilty of espionage was shot dead during WWll,Mata hari,Entertainment,Who played Louis in 'Interview With The Vampire'?,Brad Pitt +General,Johan Schober the first president of what organisation in 1923,Interpol,General,Which Australian state capital was named in honour of a British naturalist,Darwin,General,A castrated male reindeer is known as what,Bull +General,How many spots are on a dice,21,Food & Drink,What Is The Origin Of The Word Gammon ,From The French Word For Ham Jambon ,Music,In The 1960's Who Acquired The Nickname Of “The White Queen Of Soul”?,Dusty Springfield +Geography,Name the sea between Asia Minor and Greece.,Aegean,Science & Nature,What Is The Liquid Portion Of Blood Known As ,Plasma ,General,Horse brasses - on dreyhorses - originally what purpose,Charms - ward off evil +General,Florida is often referred to as 'The Sunshine State' what place in Australia also has the same nickname,Queensland,General,Name the NYC club where Blondie and the Ramones got their start?,CBGBs,General,Who directed the 1976 film All The President's Men,Alan j pakula +General,What is the voice box,Larynx,Music,Who sang the theme tune for the James Bond film “Die Another Day”?,Madonna,Art & Literature,"On a represented form, a point of most intense light. ",Highlight +General,What is a nidologist interested in,Birds nests,Toys & Games,"In which sport or game is the term ""rook"" used",Chess,General,What is the proper name for a whale's penis,Dork +General,Which Real Island Famed In Fiction Is Some 25 Miles South Of Elba,Monte Cristo,General,Who played the mother in lost in space,June lockhart,General,Saigon is the capital of ______,South vietnam +General,"What film gave the following hype ""brando sings!""?",Guys & dolls,Music,What Was Frank Sinatra's Middle Name,Albert, History & Holidays,What mistake did Coca-Cola make in 1985? ,New Coke  +General,Inspecting Galvaynes Groove tells you what,Age of horse – it’s on its teeth,General,"In Greek mythology, who ruled over the island of samos",Polycrates,General,Who recorded the song 'Space Oddity',David bowie +General,"ON Tv In 1987 ""Marion Chanter"" Became The First And Only Female To Do What",Win The Krypton Factor,General,What sinatra hit did he dooby dooby do in,Strangers in the night,General,What dam is said the be the largest hydroelectric station in the world,Itaipu +General,How many stitches are on a regulation baseball,108,General,4000 patents for a variation of what issued since first 1838,Mousetrap,Food & Drink,Which TV Chef Shares His Surname With A Popular Holiday Island Populated By Uk Tourists? ,Gary Rhodes  +General,The port of Piraeus serves which European capital,Athens,General,The first Apple Mac hard disk was how big,5 Megabytes,General,Saint Homobonus is the Patron Saint of who,Business People +General,At the end of TVs MASH what character stayed in Korea,Corp Maxwell Klinger,Music,Which Female Singer Had Two No.1 Hits In 1998 At The Age Of Fifteen,Billie,General,Wind speed is measured by a(n)___.,Anemometer +General,What is the name for 100th of a second,A Jiffy,General,What Show did the Simpsons first appear?,The Tracy Ullman Show,General,What animal comes in both spotted and striped varieties,Skunk +General,What was the first US TV series broadcast in the USSR,Daktari,General,What is the capital of Kosovo,Pristina,General,"In fashion, what do the letters 'LBD' mean",Little black dress +General,What appear when the sun activates your melanocytes,Freckles,Science & Nature,Of What Is A Jenny The Female? ,Donkeys ,General,"Where was the movie ""running brave"" filmed",Edmonton +Science & Nature, Ducks will lay eggs only in the __________,Early morning,General,What is the most critical thing keeping bananas fresh transport,Temperature not below 13 C 55F,General,What is a female swan called?,Pen +Geography,The highest temperature ever recorded on Earth was in which country?,Libya,General,What was the name of William Tells son (the apple head boy),Walter,General,Synthetic hormone used to build muscle,Anabolic steroid +General,Sophophobia is the fear of,Learning,General,Who Owns The Worlds Most Valuble Stamp Collection,Queen Elizabeth II,General,Leaflike part of plant growing before flower,Bract +Science & Nature," When thirsty, a camel can swig down 25 gallons of water in less than __________",Three minutes,General,"Which film, directed by Jonathan Demme, won the 1991 Academy Award for Best Picture",Silence of the lambs,General,Why did Disney recall his first celluloid Donald Duck toys,They exploded into flames + History & Holidays,Which artificial fiber was invented in 1938 ?,Nylon,General,Name Leonard Nimoy's autobiography,I am not Spock,Music,"Which Pianist Had Hit Albums In The 1980's With Records Like ""The Music Of Love"", ""The Classic Touch"", & ""The Love Songs Of Andrew Lloyd Webber""",Richard Clayderman +General,What was St. Petersburg called immediately before its name was changed to Leningrad,Petrograd,General,What sea animal looks like a pin cushion,Sea urchin,Entertainment,In the 60s Mokees Song Here comes Tommorow who does Davey Jones say he Loves?,Sandra and Mary +General,Which character did David Jason play in the T.V. series Porridge,Blanco,General,Joni mitchells hit from ladies of the canyon album says what came and took away her old man,Big yellow taxi,General,"What, specifically, won’t Meatloaf do for love",Screw Around +General,"""Vissi D'Arte"" is a famous aria from a Puccini opera in which the eponymous heroine is an opera singer. Name the opera",Tosca,General,George Washington carried a portable what,Sundial,General,What was unusual about Joe Davis the World Snooker Champ,Blind in one eye +General,Around which French town is the champagne industry located,Epernay,General," Rats, mice, beavers, and squirrels are all ________.",Rodent,General,What vegetable has a name that sounds like a letter of the alphabet,Pea +Food & Drink,What type of sweets was American President Ronald Reagan known for having on his desk? ,Jelly-beans ,General,"Who's Autobiography Is Called "" Is It Me""?",Terry Wogan,Music,Glad All Over Was A Hit For Which Five Piece Band In 1963,The Dave Clark Five +Entertainment,What is the drummer's name in 'The Muppet Show'?,Animal,General,"In the nursery rhyme, who ran away when the boys came out to play",Georgie,General,If you committed Vaticide who would you have killed,Prophet +General,"In 'Star Trek', what is Data's rank?",Lieutenant Commander,General,Which great artist and sculptor designed the fortifications when the Florentine Republic was besieged by the Medicis in 1530,Michelangelo,General,What bird is associated with the Tower of London ?,Raven +General,Which new york city building was finished in 1931,Empire state building,General,What do you mix with equal amounts of coffee to make 'cafe au lait',Milk,General,Who wrote 'the starry messenger',Galileo +General,"A painting of which famous Old Testament ruling, completed in 1495, is one of the earliest works by the artist Giorgione",Judgment of solomon,General,Turkish soccer team wich won UEFA Cup in 2000,Galatasaray istanbul,General,After King Henry VIII Died Who Became The Next King Of England,Edward The VI +General,Who broke Bearings bank and inspired the film Rogue Trader,Nick Leason,General,"What are pink, pram, snow, koff, buss, bark and dory types of",Boats or other water craft,General,Erving Inky and Dinky were the nephews of what comic book cat,Felix the cat +General,Who discovered the River Zambezi,David livingstone,General,Charles Conrad took a cassette to the moon on Apollo 11 who,Jerry Lee Lewis,General,The guillotine was invented for chopping off what,Hands +General,Artist Marc Chegal died in 1985 in what country was he born,Russia,General,Which camera company produces the popular 'Sureshot',Canon,General,What was a reeve in a shire,Law +Music,Which Musical Concerned A Strike At The Sleep Tite Factory In Iowa,The Pajama Game (1954),Geography,Name the second largest lake in North America.,Huron,Art & Literature,What Colour Is Art & Literature In The Standard Edition Of 'Trivial Pursuit''? ,Brown  +General,Galahad what woman poet only left her home state of massachusetts once,Emily,General,Hypengyophobia is the fear of,Responsibility,Music,Which Christmas song contains the line “sleigh bells ring are you listening”,Winter Wonderland +General,"Where did Stalin, Churchill, Attlee and Truman meet in 1945 to determine the future of Germany after their unconditional surrender",Potsdam, History & Holidays,What missionary station was built by Albert Schweitzer?,Lambarene,General,"The ""brat"" was a model of which car",Subaru +Art & Literature,How is Samuel Clemens better known ?,Mark Twain,General,What is the name given to young deer,Fawns,General,This Toy is based on a Filipino Hunters Weapon what is it?,Yo Yo +General,On who's show did carol burnett rise to prominence,Gary moore,Music,Name The EP Which Reached No.1 In Fenruary 1980 From The Specials,AKA Live (EP),General,Fill in the blank: chomping at the ____,Bit +General,There are more telephones than people in what city,Washington USA,General,Which authors personal publishing venture is Philtrum Press,Steven King,General,Who was nicknamed The Brocton Bomber,Rocky Marciano +General,What colour are French letter boxes,Yellow,General,What instrument was named after Laurens Hammond,Organ,Entertainment,Which superhero loves peace enough to kill for it,Peacemaker +General,Na is the chemical symbol for which element,Sodium,Music,Which group were all out of love in 1980,Air Supply,General,The art of beautiful handwriting,Calligraphy +General,In film making what is a martini shot,Last of day before pub,Science & Nature,The thylacine (considered extinct) is more commonly know as the?,Tasmanian Tiger,General,The Statue of Liberty arrived in New York City in 1885 aboard what French ship,Isere +General,Caesar Salad originated in which country,Mexico,General,Who was the leader of the khmer rouge,Pol pot,General,Which nation did moshoeshoe found,Basotho +Geography,Khartoum is the capital of which country ,Sudan ,General,What is a group of this animal called: Porpoise,Pod,Geography,More than two_thirds of Earth's land surface lies north of the _____________,Equator + History & Holidays,Christmas Crackers is cockney rhyming slang for which part of the male anatomy? ,Testicles ,General,What is the highest peak in fiji,Mount victoria,General,Which Actress Married Dennis Quaid On Valentines Day In 1991 ,Meg Ryan  +General,What is the fear of the color purple known as,Porphyrophobia,General,Name John Huston's last film,The Dead,General,Archaeologists found 145 what in King Tuts tomb,Loincloths +Science & Nature, The average cod deposits between 4 and 6 million eggs at a single __________,Spawning,General,"Which TV Series Began It's Life With The Working Title ""Woodentop""",The Bill,General,The Pips Of Which Fruit Contain A Minute measure Of Cyandide?,Apples +Geography,What Is Officially The Largest Sea By Area ,South China Sea , History & Holidays,The following is a line from which 1970's film 'you talkin to me' ? ,Taxi Driver ,General,"What common dog breed takes its name from the fact that they were originally bred to hunt a game bird called ""woodcocks""",Cocker spaniels +General,With what is sulphur and saltpetre mixed to make gunpowder,Charcoal,General,"""Harry Wayne Casey"" A Record Store Worker In Miami Florida Formed Which Group In 1973",KC And The Sunshine Band,General,Who was the first golfer to get hole in one on British TV,Tony Jacklin +Music,How Old Was Donny Osmond When He Topped The UK Chart With The Song “Puppy Love”?,Fourteen,Entertainment,What TV network features programming just for children?,Nickelodeon,Music,Who Is The Lead Singer If John Entwistle Is The Bass Player,Roger Daltrey +Music,What Beatles song did Dr. John remake?,Yesterday,Science & Nature,Which isotope of carbon is used for dating (give number) ?,14,Science & Nature, The average adult __________ pig weighs two pounds.,Guinea +General,What was innovative about Co-op winter warmer ale,Labelled in Braille, History & Holidays,How was the 1839-42 Anglo-Chinese war better known ?,The Opium War,Science & Nature," In Milwaukee during the 1900s, 12,500 horses in the city left an estimated 133 tons of __________ and urine on the streets per year.",Manure +Sports & Leisure,With which sport is Pele associated ?,Soccer,General,In which country would you find the site of the World War One Battle of Tannenberg,Poland, History & Holidays,"Who was assassinated on November 22, 1963 in Dallas, Texas?",President John F. Kennedy +Art & Literature,In What Work Does The HAL 9000 Appear ,"2001, A Space Odyssey ",Geography,In which continent would you find the ob' river ,Asia ,General,In Victoria Australia by law only electricians may do what,Change a lightbulb +General,"Which BBC TV comedy featured spoof news and current affairs, and introduced Alan Partridge",The day today,General,Ringo Star narrates which children's TV series,Thomas the tank engine,General,"What British royal was dubbed ""fish face"" by his wife",Prince Charles +General,What is a sound called when it bounces back to the person who made it,Echo,General,Which star appears brightest in the northern constellation Lyra -it is also the fourth brightest in the night sky,Vega, Geography,What is the capital of Mozambique ?,Maputo +General,What was a symbol of welcome in the 1700's to 1800's and can often be seen on doorknockers today,Pineapple,General,The Original Hovis Advert (1974) Was Voted The Most Popular Advert Of All Time In 2010 But Who Actually Directed It,Ridley Scott,General,Of which country was Achmed Sukarno president from 1945 - 1962,Indonesia +General,What is the average speed of a running pig,7.5 mph,General,"On German pottery, which factory's mark was from 1724 a pair of crossed swords",Meissen,People & Places,Who was the first ever president of the united states ,John Hanson (1715 - 1783)  +General,"In the film 'the day of the jackal', who played the jackal",Edward fox,General,What was Hoyt Wilhelm's best pitch,Knuckleball,General,What character on TV and film must have sex every 7 years,Mr Spock +General,What is a group of locusts,Plague,General,The US IRS manual gives the plan for collecting taxes after what,Nuclear War,General,A point to which rays of light converge is called a(n) ________,Focus +Music,Name the legendary fourteen hour show that was held at London's Alexandra Palace in 1967?,The 14-Hour Technicolour Dream,General,In what Australian state would you find Stirling,Western australia,Music,"Which Country Artist Starred Alongside John Wayne In The Film ""True Grit""",Glen Campbell +General,What ingredient causes the shine in expensive eye shadow,Fish Scales,General,What is a group of this animal called: Plover,Wing congregation,General,How many miles are there in a league,Three +General,Where would you see sprites blur jets and elves,Thunderstorm electrical discharge,General,What did drinkers first see on Jan 24 1935,Beer Can,General,What would be happening if you suffered from canitis,Greying Hair +General,In English its worth 10 points but in Polish only one what is,Letter Z in scrabble, History & Holidays,In which century was the first pantomime in Britain ,18th (1717) ,General,"Brickbat, Pecorino, Mycella and Tilsiter all types of what",Cheese +General,RCA released the first LP in 1959 without artists name - who,Elvis Presley,General,What was winston churchill's wife's name,Clementine,Science & Nature,What branch of science studies the motion of air and the forces acting on objects in air,Aerodynamics + Geography,What is the basic unit of currency for Hungary ?,Forint,Science & Nature," Each day, 100 or more whales are killed by __________",Fishermen,General,AA Gill a writer for the Sunday Times comments in which field?,Food/Cooking Critic +General,What links the Cassowary Kakapo and the Kagu,Flightless Birds,General,When was the chair developed,2181 bc,General,Where is Landino spoken,Spain by Spanish Hebrew mix +General,Back Blanket and Button Hole types of what,Stitches,Music,What Was The Name Of Spike Jones Comedy Band,The City Slickers,General,"Where would you find a Bonnet, Course, Dabbler and Driver",Ship they are sails +General,Which food was rationed after WW2 ended but not during it,Bread, History & Holidays,In what language was the (Communist Manifesto) by Karl Marx written? ,German ,General,Language derived from Dutch and used in South Africa,Afrikaans +Food & Drink,Which countrys does one associate with the following foods or drinks 'Dum Aloo' ,India ,General,Which king's only legitimate heir was killed in the White Ship disaster?,Henry I,General,"Which Female Singer Named Her First Daughter ""Chastity"" After The Film In Which She Made Her Acting Debut",Chastity +General,Who was the first Chancellor of the German Federal Republic,Konrad adenauer,General,"Whose funeral train traveled from Washington, D.C. to Springfield, Illinois",Abraham lincoln's,General,What is the Capital of: Djibouti,Djibouti +Entertainment,"In the TV series 'Seinfeld', who does Michael Richards play?",Kramer,General,Ontology is the study of what,Being,General,Salvatore A Lombino used Ewan Hunter what famous pen name,Ed McBain +General,In what country is the northernmost point of Africa,Tunisia,Music,What Do The Police Tell Roxanne Not To Put On,The Red Light,General,Which modern day country was formerly known as Dutch East Indies?,Indonesia +General,Who was the greek god of war,Aeries,General,Name first monochrome film converted electronically to colour,Yankee Doodle Dandy,General,"Who was the female lead in ""the deep""",Jacqueline bisset +General,What is a 'somnambulist'?,Sleepwalker,General,Who is the voice of darth vadar,James earl jones,General,What country eats most turkey per capita annually,Israel +General,Field of physics that describes & correlates the physical properties of macroscopic systems of matter & energy,Thermodynamics, History & Holidays,The collective noun for a group of witches is a ____ of witches. ,Covern ,General,What kind of dancer was Mister Bojangles,A Tap Dancer +General,What is the english word for 'fiesta',Festival,General,"Active volcano in the Philippines, in the central part of Luzon?",Mount pinatubo,General,What game was patented under the name Sphairistrike,Lawn Tennis +General,What percentage of Earth's circumference does the Great Wall span,Ten,General,Name the first manned spacecraft to be launched into orbit for the second time.,Columbia,General,In Michigan it is illegal to chain what to a fire hydrant,An Alligator +General,A kamikaze shooter contains Vodka Triple sec and what,Lime juice,General,The song I know him so well comes from what stage musical,Chess,General,What Is The Only American State Where The Legal Age Of Consent Is Just 14,Hawaii + History & Holidays,What food was almost non-existent in Ireland in the 1840's?,Potatoes,General,Where was the Grand National raced three times during World War I?,Gatwick,General,"At funerals in ancient China, when the lid of the coffin was closed, mourners took a few steps backward incase their WHAT got caught in the box?",Shadow +General,Noel Coward gave what director his start on In Which we Serve,David Lean,General,From what country does the Elkhound originate,Norway, Geography,What is the capital of Malta ?,Valletta +People & Places,Who Was William Bonney Better Known As ,Billy The Kid ,General,Europa is a satellite of which planet in the solar system,Jupiter,General,Where was Holmes pal Dr Watson wounded during the war,Shoulder +General,As what was Sir Matthew Baillie Begbie known as in the late 1800's,Hanging judge,Music,Whose debut solo album was called Faith?,George Michael,General,What is the state capital of South Dakota,Pierre + History & Holidays,Who starred in Funny Girl and Lawrence of Arabia? ,Omar Sharif ,General,What is the largest bone in the human body?,Femur,Food & Drink,What is the main constituent of Guacamole? ,Avocado  +General,What is heaven called in the Pilgrims Progress,Celestial city,General,What food item are most people allergic to,Cows Milk,Science & Nature, An elephant may consume 500 pounds of hay and 60 gallons of water in a __________,Single day +General,"In the Bible, Goliath was the champion of which people or tribe",Philistines,General,"Who did the barons Fitzurse, De Tracy, De Morville and Le Breton conspire to murder",Thomas becket,Religion & Mythology,In Greek mythology who did Athena turn into a spider?,Arachne +General,Who was the first thoroughbred horse to win one million dollars,Citation, History & Holidays,What Were (Mulberries)? ,Floating Harbours Used On D-Day,General,What is the most common sexually transmitted disease in USA,Herpes +General,"In 1938, Pearl S. Buck became the first American woman to be awarded what international honour",Nobel literature prize,General,Which Cornish town lends its name to the second largest city on the island of Tasmania?,Launceston,General,What kind of sword did Thundar the Barbarian have?,A Sun Sword +Food & Drink,"Which soft drink was invented by the Nicholls family of Wythenshawe, near Manchester, during the 1900's and is sold in both still and sparkling forms? ",Vimto ,General,A type of windsurfing board that is less stable but faster than the standard one,Funboard,General,The human body contains enough phosphorous to make ______,"2,200 match heads" + Geography,What is the basic unit of currency for Austria ?,Schilling,General,Which US comapny makes the most profit per second??,Ford,General,"Which comedian, born in 1893, was 'The man who found the Lost Chord'",Jimmy durante +General,What is the Capital of: Liechtenstein,Vaduz,General,In 1986 Which Film Was Nominated For A Staggering 11 Oscars And Never Won A Damn Thing,The Colour Purple,Music,"Which Famous Album Included The Tracks ""Maxwell's Silver Hammer"" & Carry The Weight",Abbey Road +General,Which singer married Renate Blauel on Valentine's Day 1984? The same year that Watford reached the FA cup final ,Elton John ,General,Where is fujiyama,Japan,General,Who was the mother of King John,Eleanor of aquitaine +General,Which is the largest of the Greek islands,"Crete,",General,Fill in the blank: ____ maids all in a row,Pretty,General,"In the galapagos islands, what has an upturned shell at its neck",Tortoise +Entertainment,What is a Hurdy-Gurdy?,A Fiddle, History & Holidays,Who Released The 70's Album Entitled Transformer ,Lou Reed ,General,Any of the non-metallic elements which forma salt when combined with a metal,Halogen +General,What is the name of the main European broadcasting system,Pal,Music,What Was Film Star Lee Marvins UK Chart Success,Wand'rin Star,General,What was invented over 3000 years ago that is now considered the first 'computer',Abacus +Art & Literature,Who Was Samuel Clemens Better Known As ,Mark Twain ,General,In the Beverly Hillbillies what did Jethro get Jed for his birthday,Electric Pencil sharpener,General,What international rugby team perform The Hakka before match,All Blacks + History & Holidays,"If you kiss someone under the mistletoe, what should you then remove ",One of the berries (for good luck) ,General,A legendary or moral tale,Fable,General,What is a Hindi in Turkey,A Turkey +Music,Which Of Carol Kings Songs Gave James Taylor A Big Hit In 1971,You've Got A Friend,General,What was the first name of Mr. Valentine who had a 1954 number 1 hit with `Finger of Suspicion' ,Dickie ,General,A condition characterized by self-absorption and withdrawal,Autism +General,Who did Michael Caine play in the Ipcress File (both names) ,Harry Palmer, History & Holidays,"""The words """"Myrrh is mine, its bitter perfume breathes of life, of gathering gloom___"""" come from which Christmas carol?"" ",We Three Kings of Orient Are ,General,Speedy Gonzales was fastest mouse in Mexico who slowest,Slowpoke Rodriquez + Geography,What is the capital of Wyoming?,Cheyenne,General,Munich hosted the Olympic games in which year,1972,General,What is a malamute,Eskimo dog +Science & Nature,If You Studied Histology What Would You Be Involved In ,Cells ,General,Who owned a cat called Apollinaris,Mark Twain,General,Sudden overthrow of government,Coup d'etat +General,Which of the brightest stars is furthest north,Capella,General,Who created the TV series - The man from UNCLE,Ian Fleming,Science & Nature,What colour identifies an ordinary diesel pump at a service station? ,Black  +General,Six ounces of what contains the minimum daily requirement for vitamin c,Orange juice,General,On what scale are there 180 degrees between freezing point & boiling point,Fahrenheit scale,General,What does a Bedouin use an agal to secure,Head cloth +General,"What links Samuel Delaney, Fredrick Pohl, Harlan Ellison",Science Fiction,General,"Seven Dwarfs Hammer, anvil, and stirrup are parts of the ______.",Ear, History & Holidays,Which mother and daughter both appeared in John Carpenters The Fog ,Jamie Lee Curtis / Janet leigh  +General,What's the largest alluvial flood plain in the U.S.,Mississippi delta the mississippi delta,General,What's the capital of Vermont,Montpelier,Geography,He invented the most common projection for world maps. ,Mercator  +General,Moses 10 plagues on Egypt - what was the fourth,Flies,General,Who owns The Oval cricket ground,Prince Charles,Science & Nature," An __________, despite its ponderous appearance, can reach speeds up to 25 miles per hours on an open stretch.",Elephant +General,In what sport would you find a coffin,Cross country riding it’s a fence,General,Which novel opens and closes with the letters of Robert Walton,Frankenstein,General,What's the capital of kentucky,Frankfort +Science & Nature," Because its eyeball is fixed, the __________ must move its huge body to shift its line of sight.",Whale,Music,What time did the Everly Brothers' Wake Up Little Susie,Four a.m.,General,What was mussogorsky's first name,Modest +General,The quokka is a member of which animal family,Wallaby,General,Which Film Won An Oscar For Best Makeup In 1992,Bram Stokers Dracula,General,Who saught to create The Great Society,Lyndon johnson +General,"______________________________ tree house in Disneyland has 300,000 fake leaves on it which are changed twice a year to reflect the seasons. ",The swiss family robinson, History & Holidays,"What black muslim leader was assassinated on feb. 21, 1965 ",Malcolm x , History & Holidays,Peter Jackson Is Responsible For Directing Which Famous Trilogy ,Lord Of The Rings  +General,Of the seven colours of the rainbow which is the middle one,Green,General,What Was The First Man Made Object To Move Faster Than Sound,A Whip,General,99% of India's Truck Drivers can't do what,Read Road Signs +General,Who was anatasia and drizella's stepsister,Cinderella,General,If you were performing a fillip what are you doing,Snapping Fingers,General,The word bank comes from the Italian banco - literal meaning what,Bench – where moneylender sat +General,What type of solution is made when a base dissolves in water,Alkaline,Entertainment,"Who played Scarlette O'Hara in ""Gone With the Wind""?",Vivien Leigh,Geography,The two Canadian provinces that are landlocked are __________ and Saskatchewan.,Alberta +General,Melvin R Bissell invented what in 1876 in the USA,Carpet Sweeper,Science & Nature,Which Road Runs From Alaska To Chile ,The Pan American Highway ,General,Which film begins Friday December 11th 2.43pm,Psycho +General,What does Karaoke literally mean,Empty Orchestra,Music,At Which Summer Event In 2000 Did Christina Aguilara Make Her Life UK Debut,London Party In The Park,General,What state has the most workers employed by the travel & tourism industry,California +Sports & Leisure,At Which Sport Was Hungarian Victor Barna World Singles Champion 5 Times ,Table Tennis ,General,What company pioneered floppy discs,IBM,General,What shape is Fusilli pasta,Corkscrews +General,What is the atomic number for thalium,81, History & Holidays,"Name the remake of a 1960's film with the aid of the following actors. The first actor was in the 60's original, the second actor played the same role in the remake David Niven and Daniel Craig ",Casino Royale ,General,Baghdad is the capital of ______,Iraq +General,"On Different Strokes,who got kidnapped?",Sam,General,Which us state has the fewest gas stations,Alaska,Music,"Who Was ""Walkin In The Rain"" With The One I Love",Love Unlimited +Music,What Do The Lyrics In The 1st Line Of The Elvis Presley Song “Blue Suede Shoes” Add Up To,6,General,Hovercrafts can be described as A.C.V.s. For what do the letters A.C.V. stand,Air cushion vehicle,Entertainment,"What kind of creature was Chewbacca in ""Star Wars""?",Wookiee +General,Who designed the steam engines Flying Scotsman and Mallard,Sir nigel gresley,Music,With Which Group Do You Mostly Associate Jimmy Paige & Robert Plant,Led Zeppelin,General,What is the flower that stands for: pretension,Spiked willow herb +General,What large animal has a less than two inch erect penis,Gorilla,General,Who composed the Illiad,Homer,General,The character Marion Crane died in what film,Psycho in the shower +Science & Nature,What Is Diazepam Better Known As ,Valium ,General,What is the smallest time interval,A picosecond picosecond,General,Who died in 1821 from arsenic poisoning from the wallpaper,Napoleon Bonaparte +Entertainment,"Name the band - songs include ""Forgiven Not Forgotten, Runaway""?",The Coors, History & Holidays,Queen Cleopatra proclaimed herself to be which Egyptian goddess?,Isis,General,The Necromancer in The Hobbit became who in later works,Sauron +Entertainment,Group of heroes led by Dick Grayson,New titans,Science & Nature,What is Cytology the study of?,The Structure of Cells,General,What Individual Has Won The Most Oscars Ever?,Walt Disney +General,Indiana jones: name the second challenge,Word of god,General,What hill lies to the north-west of the Acropolis in Athens,Areopagus,General,Which people are known in their own language as Saami,Lapps +General,Which city is served by Schwekat airport,Vienna,General,President Roosevelt was the first president to do what,Fly 1943 secret trip Casablanca,Music,What Hit Song Did Aerosmith Team Up With Run DMC For,Walk This Way +General,Romanian comunist president shot in december 1989,Nicolae ceausescu, Geography,In what country is Banff National Park?,Canada,General,What colour is the flesh of the Charentais melon,Orange +General,Famous book divided into three parts Mosques Caves Temples,Passage to India,General,BCG vaccine is used against which infectious disease,Tuberculosis,Music,"Name Four Of The Five Original Members Of ""Take That""","Gary Barlow, Howard Donald, Jason Orange, Mark Owen, Robbie Williams" +General,Which English composer wrote a Sea symphony,Ralph Vaughan Williams,General,What are animals that live in tree called,Arboreal,General,"Who portrayed eliza doolittle in broadway's original ""my fair lady""",Julie +General,Who Is The Only Boxer To Win More Heavyweight Title Fights That Muhammed Ali,Joe Louis,General,"What was the name of the owner of the talking horse, Mr. Ed on TV",Wilbur post,General,Which Romantic Movie Had The Tagline 'Can two friends sleep together and still love each other in the morning' ,When Harry Met Sally  +Food & Drink,Which Brand Of Pizza Was Also The Title Of A 1990 Movie ,Goodfellas ,General,Who strangled two snakes that attacked him & his brother in their cradle,Hercules,General,Which Modern Day Female Pop Star Released The Album “Alright Still ”,Lily Allen +General,Alopecia meaning baldness comes from Greek word for what,Fox - Mange = Bald,General,"A herb or drug described as 'diaphoretic', causes what condition",Perspiration,Music,"What Nationality Was Sylvia Who Had A 1974 Hit With ""Y Viva Espania""",Swedish +General,BA British Airways AA American Airways what is AI,Air India,General,"Which cathedral city, sixty miles from Paris, has two spires",Chartres,General,"In 'the wizard of oz', where did dorothy live",Kansas +Entertainment,"Which actor said, ""Love means never having to say you're sorry""?",Ryan O'Neil,General,Who was banned from writing USA Constitution - secret Jokes,Benjamin Franklin,General,What is a chief of the algonquian confederation,Sachem + Geography,What is the basic unit of currency for Kuwait ?,Dinar,General,I'm Gonna Be'(500 miles) was a hit for who,The proclaimers,Science & Nature, __________ for most snakes is accomplished with one lung only. The left lung is either greatly reduced in size or missing completely.,Breathing +General,Rome is the capital of ______,Italy,General,What are the only two london boroughs that start with the letter 'e'?,Ealing and Enfield,Music,Which Celebrity Named Their First Child Brooklyn,David & Victoria Beckham +General,What is another name for a spiny anteate,Echidna, History & Holidays,"""In the Christmas song 'Have Yourself A Merry Little Christmas' what """"will be out of sight"""" ? "" ",Troubles ,Art & Literature,Who Designed The Album Sleeve For The Rolling Stones LP (Sticky Fingers) ,Andy Warhol  +General,Which dog was originally bred by a tax collector in Germany for protection on his rounds,Doberman,General, The study of sound is ________.,Acoustics,General,What did the license plate on the Delorean in Back To The Future spell out?,OUTATIME +General,Where was the worlds first supermarket built (country),France,General,"Who, in 1902, made the first million selling record",Enrico caruso,General,4% of women never do what according to survey,Wear Underwear +General,What is the general designation for the period in English history from 1640 to 1660,English Revolution,Music,"Which Keyboard Player Had An ""Arkestra"" & Claimed To Have Come From Another Planet",Sun Ra,Music,"In The Beatles Track ""Lucy In The Sky With Diamonds"", what were the colors of the Celophane Flowers?",Yellow and Green +General,The cecum is the pouch at the beginning of the..,Large intestine,Science & Nature,What Is The World's Largest Mammel? ,The Blue Whale ,General,What is a male swine called (no ex boyfriends names___),Boar +General,In Greek mythology who was the son of Hypnos God of sleep,Morphious - God of dreams,General,Taurophobia is a fear of ______,Bulls, Language,"Which word means ""profound boredom"" in both french and english?",Ennui +General,Admiral Horatio Nelson lost his arm at which battle,Tenerife,General,Starting the engine of a car by bypassing the ignition switch,Hot-wire,General,"Whose last words were reportedly, 'I shall hear in heaven!'",Beethoven +Food & Drink,What Is The Alcoholic Ingredient In A Bloody Mary ,Vodka ,General,"Which Long Running Tv Show Screened It's Final Episode In 1983 And Was Entitled ""Goodbye, Farewell & Amen""",MASH,Entertainment,"She played Lois Lane in the 1978 film version of ""Superman"".",Margot Kidder +Science & Nature," Cattle branding was practiced 4,000 years ago. Old tomb paintings show __________ branding their fat, spotted cattle.",Egyptians,General,What is a group of hawks spiralling in flight,Boil,Science & Nature,Which Country Is The Worlds Main Supplier Of Teak ,Burma (Myanmar)  +General,What was thailand formerly known as,Siam,General,What is the second largest ocean,Atlantic ocean,General,Where Are You Most Likely To Find A Tittle,"The Dot Above The Letter ""i""" +General,What is the branch of theology which concerns itself with the grounds and defense of the christian faith,Apologetics,General,Who was captured and kept in a cage by Stromboli,Pinocchio,Science & Nature,Who invented dynamite?,Alfred Nobel +General,What is the state capital of New York,Albany,General,What is the young of this animal called: Salmon,Parr smolt grilse,General,What was the Soviet Vostok 3 space flight the first to do,Send back TV pictures +General,Sharp pointed projection on a plant,Thorn,General,What is the name of the stretch of water that separates Iceland and Greenland,Denmark strait,Food & Drink,What berries give gin its flavour ,Juniper Berries  +General,The Marino sheep originated in what country,Spain,Science & Nature," The pronghorn __________ is the fastest mammal to be found in North America, and second only to the cheetah as the fastest mammal on the planet.",Antelope, History & Holidays,Who discovered the tomb of Tutankhamen?,Howard Carter +General,"If you were waiting in Yonkers, in which U.S. state would you be",New york,General,What sporting trophy is named after the US sec of war 1920s,Davis Cup – Dwight Filley Davis,General,"In The Back To The Future Movie Trilogy Doc Brown Had A Dog Called Einstein In 1985, But What Was The Name Of His Dog In 1995",Copernicus +Music,Better Known Down On The FarmWho Took The Starring Roll In Grease In 1997,Ian Kelsey,General,Which car company makes the 'Avensis',Toyota,General,Who composed the opera buffo The Golden Cockerel in 1909,Rimsky-korsakov + History & Holidays,With Which War Is Florence Nightingale Associated ,Crimean ,General,Whose last words were 'thus with a kiss i die',Romeo,General,What book translates as My Struggle,Mein Kampf +General,Fax is short for which word,Facsimile, History & Holidays,February 6th 1958 saw a plane carrying Manchester United home from a match in Belgrade crash during take-off at which city's airport ,Munich ,General,"The film ""The Innocents"" was based on which Henry James story",The turn of the screw +Music,Will Smith Is Better Known As Who,The Fresh Prince,Geography,In which country is Madras,India,General,Modern Olympics - only Greece and which country in all,Australia +General,In which American state is Baltimore,Maryland,General,Who wrote man and superman,George bernard shaw, History & Holidays,"In a 60s film, who was the bravest of them all ? ",The Man Who Shot Liberty Valance (Gene Pitney)  +General,The first person to swim the English Channel did so in what year,1875,General,Philip Glass wrote an opera about which famous person,Albert Einstein,Geography,In Which Country Is The Bridge Over The River Kwai ,Burma (Myanmar)  +General,What is the Capital of: Fiji,Suva,Science & Nature,What non-working stingless bee mates with the queen ,A drone ,Entertainment,In which movie did Bruce Willis play the role of Corben Dallas?,The Fifth Element +General,What is a group of elks called,Gang,General,A beating movement of the legs.,Battement,General,Canada is seperated on an imaginary line along the ______,49th parallel +Music,The Presenters Of The Classic Saturday Morning Show Swap Shop Released A Single But What Was The Name Of The Band,Brown Sauce,General,Where would you find a Terret,Dogs Collar – ring lead fits on,General,What was David Shepherd's position before he became Bishop of Liverpool,Bishop of woolwich +General,What fruit family do almonds belong?,Peach,General,"Which crosby, stills, and nash's debut album included a song about a girl and the colour of her eyes",Sweet judy blue eyes,General,Horseradish sauce originated in which country,Japan +General,This vegetable is a variety of broccoli,Calabrese,General,As Of 2008 Who Is The Only Prime Minister To Die At No.10 Downing Street,Henry Campbell Bannerman (Lib),General,International aircraft registration letters what country is PP or PT,Brazil +Music,"Who Sang About ""The King Of Rock & Roll"" In 1988",Prefab Sprout,General,"Of what are Raucous, Spadefoot or Bounties Dwarf types",Toads,Science & Nature,This ugly creature has patches of red on his rear_end.,Mandrill +General,In mythology the fountain Aganippe was famous to whom,Muses,General,What's the main boulevard of paris,Champs elysee,Music,How Are Ed Symonds And Tom Rowlands Better Known,The Chemical Brothers +General,In 1954 Girls Names Were First Applied To And Used To Identify What,Hurricanes,General,Where was atahualpa king,Peru,General,"Who said ""I've no problem with drugs - only policemen""",Keith Richard +General,"In What Famous Event In World History Did ""Thomas Farynor"" Play A Fundmental Role",The Great Fire Of London (Baker),General,Which types of wood are most often used for firewood in the home,Hardwood,General,"Who wrote the book ""Outwitting The Gestapo""",Lucie aubrac +General,A herb or drug described as 'haemostatic' performs which effect,Stops bleeding, History & Holidays,What was the inexpensive designer watch of choice amongst teenagers during the eighties? ,Swatch ,Music,Which British Dance Act Was Master Minded By Jazzy B & Nellee Hooper,Soul II Soul +General,To coat metal with a protective layer by electrolysis,Anodize,General,What is the largest inhabited castle in the world?,Windsor Castle,Science & Nature,This animal is the symbol of the U.S. Republican Party.,Elephant +General,"What group drove ""drive"" into the top five",Cars,General,What is the fear of being contagious known as,Tapinophobia,General,In which city is the worlds busiest MacDonald's,Moscow +General,A nudist is Spain fined £65 - £60 for being nude and £5 for what,Having no ID papers,General,Who Penned The Autobiography “Wrinkles And All” ?,Kathy Staff,Music,"Who Found Chart Success With ""Rock My Heart""",Haddaway +General,Which American state is called 'The Silver State',Nevada,General,The highest man-made temperature was ___. million degrees Celsius?,70,Music,After Singing The Praises Of Muhammad Ali Who Then Found Himself In Zaire,Johnny Wakelin +General,What is a 'california long white',Potato,General,"What group did ""louie louie""",Kingsmen,General,An American in Maine got a divorce cos wife fed him only what,Pea Soup +General,Which gestalt entity produced the cult TV show Red Dwarf ?,"(Grant( &| and|,)? Naylor|Naylor( &|and|,) Grant)", History & Holidays,What is the nickname of oklahoma ,Sooner state ,Music,Which Charity Single Was A UK Number One Hit For “Various Artists” In 1997?,Perfect Day +General,What is the nickname for Alaska,Land of the midnight sun,General,And who was commissioned to rebuild them,Sir Christopher Wren,General,What is the state capital of South Australia,Adelaide +Entertainment,"This was the sequel to ""The Empire Strikes Back"".",Return of the Jedi,General,In Peter and the Wolf what instrument represents the duck,The Oboe,General,This instrument measures the velocity of the wind.,Anemometer +General,1980 Pac Man was released arcade version by which company,Midway,General,"Dog Breeds: This small, sausage shaped dog was bred to hunt small underground mammals in their dens.",Dachsund,General,What is the fear of crosses or the crucifix known as,Staurophobia +General,BaseBall: The Texas _______,Rangers,General,Which australian duo took 13 nominations and 10 wins at the aria awards,Savage garden,General,A cappella is unaccompanied singing but what it literally mean,In the style of the chapel +People & Places,Where is the comemoration statue of fictional character Sherlock holems?,"Baker Street, london", History & Holidays,Name The Slave Who Lead An Unsuccessful Revolt By The Gladiators Against Rome? ,Spartacus ,People & Places,Who Was Born And Also Died During Halley's Comet Passing ? ,MArk Twain  +General,Jack Palance won best supporting actor Oscar in what 1991 film,City Slickers, Geography,What is the capital of Italy?,Rome,Geography,On what island is Honolulu,Oahu +General,Which canadian province has the largest population,Ontario,General,Charles Atlas promised to make you a new man - what system,Dynamic Tension,General,Harold J Smith a Canadian changed name famed as sidekick,Jay Silverheels Tonto +General,Apparatus or specially constructed chamber for maintaining living organisms in an environment that encourages growth,Incubator,General,Name Frank Sinatra's Yacht,My Way Again,General,What was jack nicklaus' nickname,Golden bear +Art & Literature,What Is Tthe Ballet Term For Spinning On One Foot ,A Piroutte ,Sports & Leisure,Other Than The Epee & Foil What Other Weapon Is Used In Fencing ,The Sabre ,General,What Japanese name means fried food often at the table,Tempura +Music,Who recorded 'Cuts Like a Knife' in 1983?,Bryan Adams,General,What do homodonts have that hetrodonts don’t,Same shaped teeth,General,Farina is Italian for what,Flour +General,Who was the only English King crowned on the battlefield,Henry VII,General,Who sang 'ben',Michael jackson,Food & Drink,In Which City Was Chop Suey Invented? ,San Francisco  +General," The science of providing men, equipment and supplies for military operations is called ___________.",Logistics,General,Which American city is served by Dulles Airport,Washington d c,General,What is the highest French civil decoration awarded,Legion de Honour +Science & Nature,Which Bird Has The Largest Wingspan? ,The Albatross ,General,In Greek legend what was Pygmalion's kingdom,Cyprus, History & Holidays,Whats the resting place of those buried at sea ,Davey jones locker  + Geography,What American state has a Thames river?,Connecticut,General,In computing there are 8 bits to a byte what are 4 bits called,Nibble,Entertainment,Secret Identities: Arthur Curry,Aquaman +Art & Literature,Which Character Created By Dodie Smith Drove A Black & White Car & Wore A Black & White Fur Coat? ,Cruella De Vil ,General,What colour is grover,Blue,General,Calgary University offers a two day course in what,Igloo Building +General,Mageiricophobia is a fear of what,Having to cook,General,Golden and Argus are varieties of what bird,Pheasant,Food & Drink,Italian egg-shaped tomatoes are named after which fruit? ,Plum  +Science & Nature,How is german measles also known ?,Rubella,General,Nekal was the first type of what product (Germany 1917),Detergent,General,Where is the Cape of Good Hope,"Cape town, south africa" +General,"In which musical was the song ""I remember it well""",Gigi,General,Where would you find a porcelator,Sink - it’s the top drainhole,General,In which TV series did Michael Douglas first make his mark,The streets of san francisco +General,How many engines does the saturn rocket boast,Eight,Science & Nature,Which Cartoon Family Have A Pet Dinosaur Called Dino ,The Flintstones ,General,What type of bomb was invented by Sir Barnes Wallis,Bouncing bomb +General,Dr David Hessian 2nd Catherine Cookson what type his books,Gardening,General,Who got best actor award for the character Charlie Allnut,Bogart - The African Queen 1951,General,74% of American women say what is biggest dating turn off,Swearing – Foul Language +General,The Demologos was the first steam powered what,Warship,General,What kind of fruit is a kumquat,Small Orange,General,"What author penned such books as ""Mila 18"", ""Trinity"" & ""Exodus""",Leon uris +General,Since 1991 Crufts London dog show has been held where,Birmingham NEC,General,What is a harness racer's vehicle called,Sulky,Music,"Which Which Instrument Would You Associate Tommy Dorsey, Glen Miller & Trummy Young",Trombone +General,What is the chemical formula for water,H2o,General,As what is the glue on israeli postage stamps certified,Kosher,Art & Literature,"Name the Shakespeare play from this ultra short plot summary: Urged on by his wife, a man murders his king in order to take his place.",Macbeth +General,What name is given to the crater at the top of a volcano caused by its collapse,Caldera,General,Who was the runner up in the 1979 Le Mans 24 hour race,Paul Newman,General,Who is Dick Tracy's sweetheart,Tess trueheart +General,What airport has the code MME,Marseilles,General,"In Greek mythology, alcemene was the child of ______",Heracles,Geography,What Is The World's Most Northerly Capital City? ,Reykjavik ( Iceland )  + Geography,What is the capital of Turkey ?,Ankara,Art & Literature,"Which Writer , Archaeologist & Soldier Joined The RAF After The First World War & Changed His Name To Shaw In 1927 ",T E Lawrence ,General,What is the sacred animal of Thailand,White Elephant +General,What does an otologist study,Ears,General,"What movie had the line ""We're on a mission from God""",The Blues Brothers,Food & Drink,The Turkish dish of small pieces of meat and vegetables cooked on a skewer? ,Shish Kebab  +General,"A health profession concerned with the prevention, diagnosis, & treatment of disorders of the teeth & adjacent tissues of the head, neck, & mouth",Dentistry,General,What animal - faster horse - longer no h20 camel - see behind,Giraffe,General,The balboa is the unit of currancy in which country,Panama + Geography,What is the capital of Malawi ?,Lilongwe,General,What is the world's largest rodent,Capybara,General,What would you do with a nan prick in Thailand,Eat it - It’s a hot sauce +Music,"Who Had A Hit With ""Jacobs Ladder""",Huey Lewis & The News,General,When was the worlds first human heart transplant performed,1967,General,What are the height and width of a horse measured in ?,Hands +General,As what is California also known,Golden state,General,In what Australian state would you find Devonport,Tasmania,Music,"Who Had An Early 90's Hit With ""Things That Make You Go Hmmm""",C & C Music Factory +General,A young dogs a pup what's the correct name for a young skunk,A kit or kitten,General,What Country Has Won The Worlds Strongest Man Competition The Most Times,Iceland,General,What flavour is kirshwasser liqueur,Cherry +Music,On Which Label Did Oasis Rise To Fame,Creation,General,Rich blue-veined Italian cheese,Gorgonzola,General,Who wrote 'the rose tattoo',Tennessee williams +Music,Who replaced Ronnie James Dio in Black Sabbath?,Ian Gillan,General,For what would an Edgar be awarded or won,Mystery Writing,General,As who is Vincent Furnier known,Alice Cooper +General,Who composed the opera Turendot,Giacomo Puccini,General,Who was the first golfer to officially earn over $1 million a year,Curtis Strange,General,Which film won the oscar for best picture in 1987,The last emperor +General,Who was dingaan's predecessor,Shaka,General,If a dish is served Florentine what will it contain,Spinach,Science & Nature,Who Invented The Steam Engine ,James Watt  + History & Holidays,For which label did Elvis first record? ,Sun Records ,Music,"Who Had Minor Hits With ""Wasteland"" & ""Tower Of Strength""",The Mission,General,What was the name of Isaac Newton's dog - caused fire in lab,Diamond +General,In Omaha Parents can be arrested if child does what in church,Burps,Science & Nature,Dugongs & Manatees Belong To Which Family Of Animals ,Sirenia Or Sea Cows ,General,Israel's equivalent to the dollar is the ______?,Shekel +General,What do zoologists call the leader of a wolf pack,The alpha male,Geography,What is the capital of United Kingdom,London,General,Who won the Eurovision song contest with Jack in a Box,Clodagh Rogers +Music,"Who Co Wrote The Band Aid Single ""Do They Know It's Chrismas""",Bob Geldof & Midge Ure,General,Where is the oldest brewery in the u.s,"Pottsville, pennsylvania", History & Holidays,Which Michael Jackson song Was Christmas Number One In 1995 ,Earth Song  + History & Holidays,Which Horror Film Star Was Portrayed In An Oscar Winning Performance By Martin Landau In Tim Burtons 1994 Film About Cult Filmaker Ed- Wood ,Bella Logosi ,General,What 1988 movie reunited william hurt and kathleen turner,Accidental tourist,General,There are one what for every 6 people in Canada,River + History & Holidays,"""When are the '12 Days of Christmas'? """"Dec 14th - Dec 25th) - (Dec 21-Jan 1) - (Dec 26 - Jan 6) - (Christmas Eve-Jan 4th) "" ",(Dec 26 - Jan 6) , History & Holidays,How many countries joined the United Nations at it's start ?,51,General,"What Sudanese city's name means ""elephant trunk""",Khartoum +General,Which star of films such as 'Ryan's Daughter' died in 1997,Robert mitchum,Religion & Mythology,Who founded the People's Temple Commune,Jim jones,General,What was the former name of the Chrysler Corporation,Maxwell Motors +General,Who was the female star of Basic Instinct,Sharon Stone,General,Sadat what athlete released the photo book rare air in 1993,Michael jordan,Geography,What Asian city was once called Edo,Tokyo +General,Which U. S. State provided the title of a Bee Gees hit single,Massachusetts,General,What is the present use of the tobacco factory which features in the opera Carmen,University of seville,General,What are meteors,Falling stars +General,1500 paces was what Roman measurement,League,Science & Nature,Name the largest of the dinosaurs.,Brachiosaurus,Music,All Of Connie Francis's Hit Singles Appeared On One Label Which One,MGM + Geography,Which sea is located between Australia and New Zealand?,Tasman,General,Ab Ovo Latin for the very beginning but what's it literally mean,From the egg,General,This cereal features a honey bee as its mascot (very specific),Honey nut cheerios +General,"The city of Sheffield stands on the River Sheaf; and on which other, major, river",Dublin,General,What is the family name of the Dukes of Bedford,Russell,General,"From Which US State Did Frank Sinatra, Bruce Springsteen And Thomas Edison All Come From",New Jersey +General,In the Bible David played the Kinnor what is a Kinnor,Lyre,Sports & Leisure,What Is The Green Fabric That Covers A Snooker Table Called ,Baize ,General,What is a group of this animal called: Roebuck,Bevy + Geography,Acadia was the original name of which Canadian province?,Nova Scotia,Sports & Leisure,Where were the 1972 Olympics held ?,"Munich, West Germany",Sports & Leisure,Which Former Sports Person Became MP For Falmouth & Cambourne In 1992 ,Sebastian Coe  +General,How much did greta garbo insure her legs for,One million dollars,General,Grilled on a Ploughshare literal meaning what Japanese dish,Sukiyaki,General,Ping Pong and Pang are characters in which opera,Turendot - by Puccini + History & Holidays,What eighties TV show starred Bruce Willis in a detective agency? ,Moonlighting ,Sports & Leisure,How many players are there on a soccer team,Eleven,General,In which building do they elect a new Pope,Sistine Chapel +Sports & Leisure,Football: The Seattle _______.,Seahawks,General,Which drink should be served in a glass called a copita,Sherry,General,What was the title of Madonna's Very first greatest hits album,The Immaculate Collection +General,Odontophobia is the fear of,Teeth dental surgery,General,What English word meaning disaster comes from Italian for flask,Fiasco,General,On which motorway are the Trowell and Woodhall service areas,M1 +General,Which Sport Was Banned In England In 1849,Cock Fighting,General,What is extracted from the ore cinnabar,Mercury,General,What two countries sandwich the Dead Sea,Israel & jordan +People & Places,Who Batted With An Aluminium Bat ,Dennis Lillee ,Sports & Leisure,How Many Players Are There In A Baseball Team ,9 Players ,General,Who wrote 'the birds',Alfred hitchcock +General,On what island is pearl harbor,Oahu,General,What is the correct name for a Hawaiian Goose,Nene,General,From which American state do the Bighorn Mountains arch northwest into southern Montana,Wyoming +General,What kind of surface is tennis' French open tournament played on,Clay,Art & Literature,"Which Famous Book Begins With The Line 'Marley was dead, to begin with. There was no doubt about that' ",A Christmas Carol ,General,Who would use a caret,Printer it’s an insertion mark + Geography,Into which estuary do the Trent and Ouse flow?,Humber,General,Alfred Packer in the USA was convicted of what strange crime,Cannibalism,General,The Greek word meaning The writings of prostitutes now what,Pornography +Sports & Leisure,Who has played in the most consecutive baseball games?,Cal Ripken Jr,General,What do navel oranges lack,Seeds,General,What nationality was first person in space besides US or Russian,Czech – Vladimir Remek Soyuz 28 +General,Selaphobia is the fear of,Light flashes,General,When was the date of the christian festival easter fixed by the council of nicaea,325 ad,Geography,Which City Is The Capital Of Taiwan ,Taipei  +General,Wedding rings are normally worn on what finger of your hand,Ring,General,"In The World Of Music How Are ""Rob Pilatus & Fabrice Morvan"" More Commonly Known",Milli Vanilli,Music,"Which song contains the line “One Dream One Soul, One Prize, One Goal?",A Kind Of Magic (Queen) +General,In Norse mythology women were made from a tree - which tree,Elm,General,Ichthyology is the study of ________.,Fish,Food & Drink,"What colour is pistachio nut, often used in ice cream and confectionery? ",Light Green  +General,75% industrial accidents happen to people who ain't done what,Eaten Breakfast,Music,"What Connects Bob Dylan, Absolutely Fabulous, Julie Driscoll",This Wheels On Fire,General,On average what contains 5 - 7 calories,Single ejaculation +General,Which Suffolk town can boast no fewer than three 15th century perpendicular gothic churches?,Sudbury,General,What is the Capital of: Jamaica,Kingston,General,Which country invented the concentration camp,Britain - Boer war +General,"What word come from the Latin phrase ""to be ashamed of""",Pudenda,General,Butterfly Chisel Lead Pipe Mallet Occult Willow all types of what,Bone Fracture,General,"To the nearest minute, how long does it take for light to travel from the sun to earth",Eight +General,What was steven spielberg's first film,The duel,Science & Nature, Pigeons and hummingbirds have tiny magnetic particles in their heads that respond to the Earth's magnetic fields and are used for __________,Navigation,General,What is the Capital of: French Polynesia,Papeete +General,Which drink does Melanie Sykes advertise on TV,Boddingtons,Science & Nature,"What does ""Ursa Major"" mean in everyday English",Big bear,General,Who created jergens lotion,Andrew jergens +Music,Who recorded the Immaculate Collection?,Madonna,Music,In Which Year Was Culture Clubs Karma Chameleon A Hit,1983,Science & Nature,"To The Nearest 250 Grams, What Is The Weight Of An Average Liver ",1.75kg (4lbs)  +General,Name Alfred Hitchcock's first sound film as director,Blackmail, Geography,What city boasts the Copacabana Beach and Ipanema?,Rio de Janeiro,General,"What vitamin is most commonly added to milk, becasue it is not found in milk naturally",Vitamin d +General,Which Hitchcock film starred Margaret Lockwood and Michael Redgrave,The lady vanishes,General,Elizabeth Bennett is the central character in what novel,Pride and Prejudice,General,"Which British Singers Real Name Is ""Gaynor Hopkins""",Bonnie Tyler +General,What did friar roger bacon invent,Magnifying glass,General,Born Aug 24 to Sept 23 what star sign,Virgo,General,If you had Cynophillia what type of sex turns you on,Sex with dogs +Food & Drink,Complete the classic advertising slogan 'Good Old Bertie Bassett_____'? ,He's Britain's Greatest Asset ,General,From which modern country did the Franks come,Germany,General,"In the film 'jurassic park', in which comical place did someone hide when the t-rex escaped",Toilet +General,Which country saw the Mau Mau uprising,Kenya,Sports & Leisure,What game features the largest ball,Earthball, Geography,What is the capital of Barbados?,Bridgetown +General,Parasitophobia is a fear of ______,Parasites,Music,Bjorn Ulvaeus & Benny Andersson From Pop Supergroup Abba Collaborated With Tim,Chess,General,Chu is the Chinese year of what animal,Boar + Geography,What is the highest peak in Fiji?,Mount Victoria,Music,Name One Of Shirley Basseys Two No.1 Hits,As I love You / Reach For The Stars / Climb Every Mountain,Music,Who Was Ringo Stars first wife?,Maureen Cox +General,Rheoboam is a bottle size but also the last king of where,Israel,General,What does an insect do through its spiracles,Breath,General,Iraq's premier infantry corps,Republican guard +General,What did Captain Matthew Webb swim first?,English Channel,General,Mont Dore is the highest point in which French mountain range,Massif central,General,Jeri Ryan plays what character in the Star Trek series,Seven of nine in Voyager +General, Animals that once existed but don't exist now are said to be ______.,Extinct,General,In medicine what is boric acid used for,Antiseptic, History & Holidays,Which American President Was Assassinated In A Theatre? ,Abraham Lincoln  + History & Holidays,Which Axis Powers In World War II Fought With The Allies In World War I? ,Japan & Italy ,General,The Pacific Ocean accounts for roughly what volume of the world's oceans,Half,Geography,What Was (Ho Chi Minh) City Previously Known As ,Saigon  +General,What golden yellow gem sounds like a fruit related to lemons,Citrine,General,Who wanted 'a new drug',Huey lewis,Science & Nature,Which Shrub Was Introduced To England In 1598 Can Be Used In Cooking & Is Also A Component Of Eau De Cologne ,Rosemary  +Food & Drink,Where Might You Drink The Wine Retsina ,Greece ,Music,"Who had a hit single in 1989 with the song ""We Didn't Start The Fire""?",Billy Joel,General,Moldings and ornamentation projecting from the surface of a wall.,Relief + History & Holidays,"According to some legends, what holiday decorating practice is attributed to spiders? ",Tinsel ,General,What is a jockey's uniform called,Silks,Food & Drink,Saffron Comes From The Stamen Of Which Flower ,Crocus  +Sports & Leisure,Who is Edson Arantes do Nascimento better known as?,Pele,General,Who wrote the musical Porgy and Bess,George gershwin,General,What is the Capital of: Togo,Lome +General,Which Country Used More Condoms Last Year Than Any Other? (2005),Japan, Geography,What is the capital of Thailand ?,Bangkok,General,"Plant with pink, red or white flowers with backward turned leaves",Cyclamen +General,Who steals the Pink Panther in the original film,The Phantom,General,By law every fifth song on Canadian Radio must be what,By a Canadian,Entertainment,Who sings 'Sweet Home Alabama'?,Lynyrd Skynyrd +General,"What is the only river that flows both north and south of the equator, crossing it twice",Congo,General,In Winnie the Pooh what's the name over the door Pooh's house,Mr Sanders,Mathematics,"He is known as ""The Father of Geometry"".",Euclid +Entertainment,The Hard Rock Cafe is named after a song by what band?,The Doors,Music,"""Sense"", ""Pure"", ""Joy"", And ""Happy"" Are All Tracks Taken From Which Lightning Seeds Album",Pure,Food & Drink,"What On A Bottle Of Wine, Is The Punt? ",The Dent On The Base  +General,Tomatillo is a feature in what countries cusine,Mexico,General,What people founded cheese making in England,Romans,Science & Nature,What Is The Largest Plant In The World? ,The Giant Redwood Or California Sequoia  +General,Walrus tusks are made of ________,Ivory, History & Holidays,Which 1972 musical won eight Oscars ? 'divine decadence darling' ,Cabaret ,General,"The small intestine is made up of the duodenum, the jejenum and the ______",Ileum +Music,Which Famous Rapper Was Born Stanley Kirk Burrel In 1962,MC Hammer,General,What was the name of the older brother on Happy Days?,Chuck Cunningham,Entertainment,Benny and Cecil were at odds with whom,John +General,The Titanic has a sister ship - name it,The Olympic,Toys & Games,The object of this card game is to meld sets of 7 or more cards.,Canasta,General,Fast food franchise: Taco ____,Bell +General,The Inquisition forced this person to recant his belief in the Coppernican Theory. Who was he,Galileo,General,3-D that can be seen without glasses is also known as this,Stereoscopic,General,"The four throwing events at the olympics are shotput, discus, javelin & ______",Hammer throw +General,"How In The World Of Music First Discovered On A 2007 Reality TV Show Is ""Vivien Smallwood"" Better Known",The Rapping Granny,General,"In ballet, leaning forward.",Penché,General,Scriptophobia is the fear of,Writing in public +General,Which Hollywood heart throbs real name was Roy Scherer,Rock hudson,General,What ocean is found along the east border of asia,Pacific ocean,General,In which Irish county can you kiss the Blaney stone,Cork +General,How long is a giraffe's tongue,21 inches,Geography,Which element makes up 46.6% of the Earth's crust,Oxygen,General,In Fort Madison Iowa Firemen must do what before attending fire,Practice for 15 minutes +General,Which Car Manufacturer's Name Means I Hear?,Audi, History & Holidays,Whose radio production of War Of The World's caused panic in America in 1938? ,Orson Welles ,General,Where was the worlds first oil well drilled,Pennsylvania +General,Wayne Brazel shot and killed what Western figure in 20th cent,Pat Garret,General,Ursus Artus Horribilus - the Latin name of what creature,Grizzly Bear,General,On What Day Is Easter Celebrated ,Sunday  +Geography,Which borough is northeast of and adjacent to Manhattan? ,The Bronx ,General,The initials pc on a medicine means it should be taken when,After Meals, History & Holidays,In Which Country Is The Auschwitz Concentration Camp Located ,Poland  +Geography,Name the world's most photographed and most climbed mountain.,Fuji,Art & Literature,Who wrote the famous series of Discworld books?,Terry Pratchett,General,In 1998 there were 508 hospital causing injuries what sport,Snooker Pool +General,What is Jane Fonda's middle name,Seymour,General,Who is known as Tuhkimo in Finland,Cinderella,Music,"Who Had A Hit In 1988 With ""Monkey""",George Michael +General,What is the capital of albania,Tirana,General,"Who wrote a series of novels about C.I.A. analyst, Jack Ryan",Tom clancy,General,What fashion did General Ambrose Burnside start in Civil War,Sideburns +General,"In ballet, a closed position of the feet.",Fermé,General,As Of 2010 Which Footballer Holds The Record For The Fastest Hat Trick Scored In The Premiership,Robbie Fowler,Music,Who Worked As An Arranger For Glen Miller & Later Wrote The Scores For The Pink Panther & Breakfast At Tiffany's,Henry Mancini +Science & Nature, __________ communicate in sound waves below the frequency that humans can hear.,Elephants,General,"What's a ""cat-o'-nine-tails""",Whip,General,What is the spanish word for 'festival',Fiesta + History & Holidays,Which 60s film was the true story of Helen Keller ,The Miracle Worker ,Music,Aged 15 Who Had Success In 1964 With Her Version Of An Isley Brothers Track,Lulu,General,What country does China have its longest land border with,Mongolia +General,What have Jan Zajic and Quang Duc got in common,Self Immolation,General,What is the state capital of Nevada,Carson city, Geography,What is the capital of Mali?,Bamako +Sports & Leisure,What Is The Technique Called In Which You Right A Capsized Canoe ,Eskimo Roll , Geography,What is the capital of Guinea-Bissau?,Bissau,General,"Who, in 1990, was the first professional boxer to beat Mike Tyson",James (buster) douglas +Music,"Whose Songs Were Recorded By A Variety Of Artists On The Album ""Red Hot And Blue""",Cole Porter,General,What was the name of the plantation in Gone with the Wind,Tara,Music,Bjork Was Originally A Member Of The Sugarcubes Or Was It The Sugababes,The Sugacubes +General,What links horses rabbits and rats,Cannot Vomit,General,In Natoma Kansas illegal to throw knives at people wearing what,Stripped Shirts,General,Who was president of france's first fifth republic,Charles de gaulle +General,Which Italian city was severely damaged by an earthquake in 1908,Messina,General,What nationality was prince albert,German,General,Name the first car model with transverse engine front wheel drive,Austin Mini +General,What book of the Bible does not mention the name of God,Esther,General,What is the staple food of the Maori people of New Zealand,Sweet Potato,Entertainment,The rolling stones first recorded song was?,Come On +General,The Volga is Europe longest river what is the second longest,Danube,General,In Minnesota its illegal for a woman dressed as what on street,Santa Clause,Geography,"""Oceania"" is a name for the thousands of islands in the central and southern _______________. It is sometimes referred to as the South Seas.",Pacific ocean +General,What is a kissing gourami,Tropical fish,General,The ancient Egyptian word for cat was mau - what does it mean,To see,General,Which European city has the tomb of the three wise men,Cologne +General,Who wrote The female of the species more deadly than the male,Rudyard Kipling,General,Clinton Oklahoma see two having sex in car what's illegal you do,If you Masturbate,General,What type of insect performs a waggle dance,Hive bee +General,Limnology is the study of what,Marshes from Greek,General,What's david bowie son's name,Zowie bowie,General,Name Pink Floyds only single,Oranges and Lemons +Music,Who Is Martin Fry Lead Singer With,ABC,General,What Russian city's name translates to 'ruler of the east',Vladivostok, Geography,What is the basic unit of currency for Bahrain ?,Dinar +General,If the vestal virgins were caught having sex what punishment,Burried Alive,General,"Africa's four great rivers Nile, Congo Zambezi and what",Niger,General,"What was the name of the t.v. show with the theme song that began: ""Believe it or not,I'm walking on air___""?",Greatest American Hero + History & Holidays,"Who was, ""First in war, first in peace and first in the hearts of his countrymen""?",George Washington,General,What type of line on a weather map joins places with equal rainfall?,Isohyet,General,What does a soccer player have to do when the referee shows him a red card,Leave the game +General,In which European city are the headquarters of OPEC (Organisation of Petroleum Exporting Countries),Vienna,General,What is a hot spring that shoots steam into the air,Geyser, History & Holidays,Which Christmas carol contains the lines 'O Tidings of Comfort and Joy ,God Rest Ye Merry Gentlemen  +Art & Literature,Who was the author of 'Dracula'?,Bram Stoker, Geography,What is the capital of Iowa?,Des Moines,Entertainment,Who plays the lead role in The Usual Suspects?,Kevin Spacey +General,Measure for Measure deals with what contemporary theme,Angelo harasses Isabella sexually,General,What was the only remake to win the best picture Oscar,Ben Hur,General,What serious umderwater ailment was named after a Victorian notion of chic posture,The bends bends +Music,Which Teen Idol First Found Success In The Partidge Family,David Cassidy, History & Holidays,Through the streets of what town did Lady Godiva ride naked?,Coventry,General,Where was the canning process for fish first developed,Sardinia +General,UK group who had a hit with 'House of the Rising Sun',Animals,General,Near what river is the temple of karnak,Nile,General,"Name the American President assassinated in 1901 in Buffalo, New York State.",William mckinley +General,"What's the international radio code word for the letter ""M""",Mike,Food & Drink,What is the main ingredient of Sauerkraut? ,Cabbage ,General,Who wrote 'the grinch who stole christmas',Dr seuss +General,If you have Chlorosis what colour does the skin go,Green,General,In which EEC country is abortion still illegal,Ireland,General,"In the TV show ""Scarecrow and Mrs King"", what is Mrs King's first name",Amanda +Sports & Leisure,Dwight York & Jimmy Floyd Hasselbaink Were Two Foreign Players Who Were First To Score 100 Goals In the English Premiership Who Was The Third? ,Thierry Henry ,General,Insectophobia is the fear of,Insects,General,What was banned in US movie theatres in the 1920s,Popcorn - too noisy +General,"What film gave the following hype ""brando sings!""",Guys and dolls,General,In what US City is most blond hair dye sold,Dallas Texas,General,What was the first million dollar seller paperback,I the Jury – Mickey Spillane +General,Over 90% of the worlds total population of what gone since 1970,Rhinoceros,General,In LA by law you can't hunt what at night in streetlight,Moths,General,"What is the title of Peter Shaffer's play about Mozart, also made into a film",Amadeus +Music,Who Is The Author Of High Fidelity A Tale Set In An 80's Record Store,Nick Hornby,General,Horse statue - mounted man - on two legs - how man die,Killed in Battle,Science & Nature,"From what animal is ""ambergis"" obtained?",Sperm whale +General,What eponymous Dickens character born with a caul over head,David Copperfield,General,What is sclerotinite,Fungal remains,General,What automaker bought Rolls Royce in 1998,Volkswagen +General,What is the official residence of the Lord Mayor of London?,Mansion House,General,"Who was nominated for a Best Actress Oscar for the film Mary, Queen of Scots",Vanessa redgrave,Geography,What is the capital of Wisconsin,Madison + History & Holidays,What wonder stood 32m high in rhodes harbour?,Colossus of Rhodes,General,What kind of vegetable is 'calabrese',Broccoli,General,Which bank did the jailed Nick Leeson work for and ruin,Barings +General,What is the Capital of: South Africa,Pretoria,Music,Who Was Shouting For Lager Lager Lager,Underworld,General,A method of resolving questions of conscience by applying moral principles or laws to concrete cases?,Casuistry +General,Jan 21 1976 What linked Bahrain and Rio de Janeiro,1st Concord passenger destinations,General,Who sings the song Clint Eastwood?,Gorillaz,General,What film featured a cat named mr. bigglesworth,Austin powers +General,What was awarded to a football player who scored 3 goals in one match,Hat,General,In Key West Florida what are barred from racing in city limits,Turtles,Music,Which Hymn Traditionally Closes The Last Night Of The Proms Every Year?,Jerusalem +General,"Caracul, Dorset, Urial, Mufflon and Jacobs are types of what",Sheep,General,What is the purple ink used to stamp meat made from?,Dark Grape Skins,General,Which large area of scrubland plains lies just to the north of the Pampas in South America?,Chaco +General,Which English cathedral is famous for its whispering gallery,St paul's,General,What profession was lillian hellman,Authoress,Music,What Nationality Was The Singer Roy Orbison,USA / Texas +General,Fill in the blank: ___ on a limb,Out,General,What member of the weasel family is over 1 meter or 3 feet long,Badger, History & Holidays,Which City Did The Allies Liberate On August 25th 1944 ,Paris  + History & Holidays,What is the name of the summer camp in the Friday 13th movies ,Camp Crystal Lake ,General,What is the standard unit of measure for weighing gems,Carat,General,Gaddafi came to power in which year,1969 +General,What was the name of the hit by The Archies,Sugar sugar,General,What actress's legs were insured for one million dollars,Greta garbo,General,"Coolidge Colorless, corrosive liquid that has the chemical formula HNO3",Nitric Acid +General,Which country's borders was established in 1919 by the 'Treaty of St.Germain',Austria,General,What product can be found in four out of five American homes,WD40,General,Maryland No1 Montana No 1 Minnesota No 3 types of what,Pigs American Breeds +General,"Mary Somerville said ""It wont last, a flash in the pan"" what",Television,General,Who is the only cricketer in Wisden 2000's Five cricketers of the Century not to have a knighthood,Shane warne,General,The natural process of rock disintegration is better known as _______,Erosion +Geography,On which River does the City of Dublin stand ,The Liffey ,General,Which is the only planet that rotates clockwise,Venus,General,In which country could you spend a taka,Bangladesh + History & Holidays,Alanis Morrisette appeared on what 80's cable children's show? ,You Can't Do that On Television ,Music,"Which Group Did ""Sounds"" Magazine Describe As ""Provincial Clods Trailing In The Wake Of The More Sophisticated Spandau Ballet""",Duran Duran,General,Kain(ol)ophobia is a fear of ______,Novelty +General,What is the alternative name of the foodstuff called scallions,Spring onions,Music,Steven Tyler Is Lead Vocalist With Which Rock Band,Aerosmith,General,"Which wild flower is also known as Fireweed, because it is the first to grow back after a fire",Rose bay willow herb +People & Places,Complete This partnership Hinge and ? ,Bracket , History & Holidays,Which Popular Christmas Movie Featured A Must Have Toy Called 'Turbo Man'' ,Jingle All The Way ,General,Who won Oscars for Jaws ET Star Wars Schindlers List,John Williams (music) +General,"Other than 'she loves you', in which other beatles number one hit can you hear the words 'she loves you, yeah, yeah, yeah",All you need is love,Music,Who Was The First Black Country Singer To Be Successful In Nashville,Charley Pride,General,Ornamental band worn on wrist or arm,Bracelet +General,Who is the subject of Anouilh's play L'Alouette ( The Lark ),Joan of arc, Geography,What is the basic unit of currency for Canada ?,Dollar,General,What two mountain ranges does the Tour de France race through,Alps & pyrenees +General,Who said 'hitch your wagon to a star',Ralph waldo emerson,General,What is the fear of certain fabrics known as,Textophobia,Science & Nature," By age 6 months, the voracious __________ will have increased its 3_pound birth weight by 7,000 percent.",Pig +Music,Who Established Paisley Park As His Own Record Label,Prince,Entertainment,Casper the Friendly Ghost frolicked with which witch,Wendy,General,What was king george vi's first name,Albert +General,Actor ______ Borgnine,Ernest,Entertainment,Rolling Stones first hit was written by what group?,The Beatles,Geography,What is the capital of Saint Kitts and Nevis,Basseterre +General,What dinosaurs name translates as three horned,Triceratops,Geography,In which country is angel falls ,Venezuela ,General,Who performed the first heart transplant in South Africa,Christian Barnard +General,What does a dowser do,Find water,General,On which river is the Kariba Dam situated,Zambesi,General,What is LCD an abbreviation of ?,Liquid Crystal Display +General,From which album did the original version of Elton John's Candle in the Wind come,Goodbye yellow brick road,General,With which instrument is Larry Adler associated,Harmonica,General,The Eggplant is part of what family of plants,Thistle +General,What confection named for a French field marshal chef made it,Praline,General,What is the common name for the star Sirius,Dog Star,Sports & Leisure,In Which Year Did Eddie the Eagle Edwards Leap To Stardom At the Winter Olympics? ,1988  +Music,Which Hot Dance Classic Did The Trammps Bring Us,Disco Inferno,General,Uncultured or primative person,Barbarian, History & Holidays,In the 1970s films directed by Robert Fuest (Pronounced Feast) how did Dr. Anton Phibes become disfigured ,Car Crash  +General,Name the carnivorous mammal related to the hyena,Aardwolf,Religion & Mythology,"His wife was Penelope and his son, Telemachus. He was exiled from his home on Ithaca for angering the gods.",Odysseus,Sports & Leisure,What is the period of play in a game of polo called ,A Chukka  +General,In Yugoslavia if you asked for Pljeskavice what do you get,Hamburger,General,Beethoven gave up what while writing his ninth symphony,Bathing,General,From what BBC TV series comes the phrase 'And now for something completely different,Monty python's flying circus +General,What item would you see on the flag of Malta,George Cross,General,Can a short-sighted person see objects more clearly when they are close up or far away,Close up,General,What are Wa Cha Wej Vagh Hut Soch and chorgh,Numbers - In Klingon +General,"Which actress was in the TV movies: The Oddyssey,and Merlin?",Isabella Rosselini,Music,According To The Lyrics Of The Song “House Of The Rising Sun” What Was The Occupation Of His Mother,Tailor,General,What oval shaped organ near the stomach purify's blood,Spleen +General,"Claylike mineral, chief source of aluminium",Bauxite,Science & Nature,What is a the technical name for a heart attack?,Myocardial infarct,General,Which trio was originally known as the 'primettes',Supremes +General,International Airline Registrations OO is what country,Belgium, History & Holidays,In pantomime what is Alladin's surname ,Twankey ,General,"In 1891, film was introduced to replace ______ in making photographic negatives",Glass +People & Places,What is the county town of Cornwall? ,Truro ,General,"In-the film world, whose real name is Melvin Kaminsky",Mel brooks,General,"The iron content of which vegetable was, for years, overstated due to the incorrect placement of a decimal point in U.S. Government's food tables",Spinach +General,In Scottish Gaelic what is a clarsach,A Harp,General,The earths atmosphere & the space beyond is known as _______,Aerospace,General,What took place on London's serpentine first time 16 June 1930,Mixed Bathing +Music,Which had a hit with the song “Obviously” in 2004?,McFly, History & Holidays,Which British Crime Author And Dame Died In January 1976 Aged 85 ,Agatha Christie ,Entertainment,What show/game has characters such as Bulbasaur and Pikachu?,Pokemon +Entertainment,"In the TV series 'Seinfeld', who plays Kramer?",Michael Richards,General,Leona Lewis Achieved A UK No.1 Hit With Her Debut Single In 2006 Who Was The Last Female To Achieve This Feat,Kym Wilde,General,What country produces the most tobacco in the world,China +General,How many days are there in a fortnight,Fourteen,General,The last descendant of what musical family died on christmas day 1845,Bach,General,A stellate object is shaped like what,Star shaped +General,What type of instrument is a celeste,Keyboard,General,What country lies north of france and south of holland,Belgium,Geography,In which country is cusco ,Peru  +Geography,Name 2 Of The 3 South American Countries Through Which The Equator Passes ,"Brazil, Equador, Columbia ",General,"In ancient egypt, the brain was extracted through the nasal passages during what process",Mummification,People & Places,Who Was Known As The Face That Launched A Thousand Ships 'Helen Of Troy' ,Helen Of Troy  +General,Crosby Stills and Nash debut album included this captivating song about a girl and the colour of her eyes,Sweet judy blue eyes,Science & Nature, Adult electric eels 5ft to 7ft long produce enough electricity __ 600 volts __ to stun a __________,Horse,Entertainment,What is the mother's name in Family Circus?,Thelma +General,Who starred in the 1916 film 'intolerance',Seena owen,General,Division what's popeye's official age,R thirty four 34,General,What was the first country to use postcards,Austria +Science & Nature," Cats have more than one hundred vocal sounds, while dogs only have about __________",10,General,What was the name of the second Pope,St linus,Science & Nature,What Was Don Quixote's Faithful Horse Called ,Rozinante  + History & Holidays,"Soleil Moon Frye was the real name of which young eighties sitcom star, whose character name was also the title of the show? ",Punky Brewster ,Sports & Leisure,"In a maximum snooker break of 147, how many points are scored on the black ball alone ",112 ,General,In what film would you find The Orgasmitron,Woody Alan's Sleeper +General,What is the medical term for ear wax,Cerumen,General,What's Cheops's profession,Egyptologist,General,What is the yellow of an egg,Yolk +General,Which Historical Figure Sadly Passed Away On The 18th April 1955,Albert Einstein,General,How did Dr Watson's first wife die,Diphtheria,General,Cod what three colors are the arrow poison frogs of south america,"Orange, red" +Music,"All Were Released In Which Year: ""Anarchy In The Uk"", ""Fernando"", ""A Star Is Born""",1976,Religion & Mythology,"In Greek mythology, who was the son of Peleus and Thetis?",Achilles,General,In Which Country Is Bild The Top Selling Newspaper,Germany +General,What 18th century German soldier told very tall tales of himself,Baron Munchhausen,General,Rhytiphobia is the fear of,Getting wrinkles,General,Who was the first US to have indoor plumbing installed,Henry Wadsworth Longfellow +General,What country is home to the bank of alexandria,Egypt,General,What was the name of the host of Double Dare?,Mark Summers,General,Roland Orzabel and Curt Smith were better known as what,Tears for fears +Food & Drink,What ingredient is added to wine to make port? ,Brandy ,General,What does Shrove Tuesday have to do with Easter? ,Shrove Tuesday is the last day to indulge before Lent. ,General,What is the Capital of: Pitcairn Islands,Adamstown +General,Americans eat approximately 100 acres of what each day?,Pizza,General,In which country is Tobruk,Libya,General,"Umber, sienna and sepia are all shades of which colour",Brown +General,Traditionally what should be given on an 11th anniversary,Steel,General,The penny-farthing was an early form of what transportation,Bicycle,General,What is the fear of crowds or mobs known as,Ochlophobia +People & Places,Who Is Julie Andrews Married To ? ,Blake Edwards ,General,Which building features the largest clock face in Great Britain?,Royal Liver Building (Liverpool),General,What is the young of this animal called: Fish,Fry +Geography,What Nationality Was The Chemist Alfred Nobel ,Swedish ,General,Meeting place for public discussion,Forum,Art & Literature,"A technique in abstract painting developed in the 1950s. It focuses on the lyrical effects of large areas of color, often poured or stained onto the canvas.",Color field painting +General,"What is the deleterious gas in the air, which is exhaled by humans called",Carbon dioxide,Geography,What is the capital of United States,Washington,General,Who are Britain's oldest publisher dating from 1469,Oxford University Press +General,What is the second book of the old testament,Exodus,General,What word is given to the line which forms the boundary between the day and night hemispheres of the moon,Terminator,General,Which part of the body will expand three times when excited,Iris +General,What does per capita literally mean,Per Head,General,Which Tennessee Williams play features a character called 'Big Daddy',Cat on a hot tin roof,General,Papa Doc' was president of which Caribbean republic until his death in 1967,Haiti +General,How many pockets are there on a true billiard table,None,General,Which country won the first Cricket World Cup,West indies,General,Orthophobia is the fear of,Property +General,To which saint is the cathedral in Prague dedicated,Saint vitus,Music,Which song is credited to Harrison-Lennon?,Cry For A Shadow,General,Where would you find the original Mr Plod,Noddy books + History & Holidays,Which famous mathematician and astronomer was born on Christmas day in 1642 ,Sir Isaac Newton ,General,What Was The Name Released By Michael Jackson In 2001,Invincible,General,Paronite and caronite are the ores what is extracted,Titanium +General,Which is Shakespeare's shortest play,The Comedy of Errors,General,Where is the Blarney Stone?,"Blarney Castle, Ireland",General,Where is the blue grotto,"Capri, italy" +General,What instrument is used to measure the strength of a magnetic field,Magnetometer,General,Montgomery and rommel directed the forces in which battle,Battle of el,General,"To 25 Years Either Way In What Year Was ""Robert The Bruce"" Born",1274 +General,When is the longest day in the southern hemisphere,December,General,"Which actor, best known for his portrayal of a hero of the Napoleonic wars, secretly married Abigail Cruttenden in November 1997",Sean bean,General,Which cricket player holds the world record for the highest individual score in first-class cricket,Brian lara +Sports & Leisure,What Does A Black Flag Signify In Motor Racing ,Driver Disqualified ,General,How - two thieves convicted 1984 executed in Sudan Aug 1990,Crucified,Science & Nature,In Which Country Is The Daewoo Company Based ,Korea  +Food & Drink,"In Which Country Did The Cheese Emmanthal Originate Was It France, Switzerland, Denmark Or Belgium? ",Switzerland ,Music,Which Disco Legends Might Have Asked Several Detectives To Find A Missing Angel,Tavares,General,Which US state drinks the least beer per capita,Utah +General,Who rode a horse called Diomed,Duke of Wellington,General,What would you be doing if you were suffering from somniloquy,Sleep Talking,General,Luiphobia is the fear of,"Lues, syphillis" +General,Thick light bark of S.European oak,Cork, Geography,What is the basic unit of currency for Saudi Arabia ?,Riyal,General,What is polenta,Cornmeal and water +General,Which literary character takes his name from Latin for nobody,Captain Nemo,General,What English word comes from Latin for Sheath for a Sword,Vagina,Music,"Which 1991 CD Sleeve features Quotes From Blake, Freud, And Father X",Enigma / MCMXC AD +General,"Which film director created ""Olympiad"" - a celebration of the 1936 Olympics and Nazi ideology",Leni riefenstahil,Music,"What Picture Features The Classic Album / ""Money For Nothing""",A Faceless Guitar Player Wearing A Blue Jacket Hair & Shirtsleeves,General,"Island Infectious virus disease of the central nervous system, sometimes resulting in paralysis",Poliomyelitis +General,Fran Philps of Canada was the first woman to do what,Reach the North Pole,General,"What cocktail does bourbon, sugar and mint make",Mint julep,General,"Link Apollo, Ghengis Khan and Abraham Lincoln on TV",All met James T Kirk +General,Which part of the body suffers from opthalmia,Eyes,General,What is the largest (in population) state/territory in Australia,New south wales,General,"Warm current of the North Atlantic ocean, flowing in a generally northeastern direction from the straits of Florida to the grand banks, east & south of Newfoundland",Gulf stream +General,"Tarsus, metatarsus, and phalanges are parts of a ______",Foot,General,What silvery liquid metal is used in thermometers,Mercury,Science & Nature,What foul smelling compound is commonly known as rotten egg gas?,Hydrogen sulphide +Food & Drink,Chablis comes from which French wine region? ,Burgundy , Geography,What is the basic unit of currency for Luxembourg ?,Franc,Religion & Mythology,What is the birthstone for January?,Garnet +Science & Nature,What Common Affliction Makes The Lens Of The Eye Go Opaque ,Cataracts ,General,Which country has the highest per capita divorce rate,1 Latvia - 2 Russia – 3 Belarus,General,What does cb stand for,Citizen's band +General,"Dogs bark, donkeys ______",Bray,Music,Who Choreographed Cats,Gillian Lynne,General,"When Spencer Tracy won his first Academy award in 1937, his Oscar statuette mistakenly bore the name of which detective",Dick tracy +Food & Drink,What Is The Annual Yield Of A Single Coffee Plant ,1kg ,General,In 1856 John C Freeman was the first what,Republican candidate US president,General,Who had a dog called Boatswain,Lord Byron +General,What tribe walked the 'trail of tears',Cherokee,Music,"Upon Which Shakespearean Play Is The Musical ""Kiss Me Kate"" Based",The Taming Of The Shrew,General,What is the young of this animal called: Fowl,Chick chicken +Religion & Mythology,He was the second King of Israel.,David,General,What was Buzz Aldrin's mother's maiden name,Moon,General,In Bewitched name the witch doctor who treats Samantha,Doctor Bombay +General,When Dino was Fred's pet what was Barney's called,Hoppy a Hoparoo,Music,Who Won The 1983 Record Of The Year Grammy With Rosanna,Toto,General,What is the collective name for a group of frogs,Army +Music,"In ""Don't You Want Me Baby"" By The Human League Where Did The Waitress Work",In A Cocktail Bar,General,Which Team Won The First Scottish FA Cup Cup Final,Queen's Park (NOT) QPR,General,Mythological beasts name comes from the Greek chimney man,Salamander +General,Who created the 'purple heart' decoration in 1782,George washington,General,Combine a Van & a Car & you get this word.,Caravan,Music,Which Singer Began His Career In The Moonglows With Motown Boss Berry Gordy's Brother In Law Harvey Fuqua?,Marvin Gaye +General,What does ring a ring a roses refer to,The Black Death,General,Which artist painted the work popularly known as 'Bubbles',Millais,General,The underside of a horse's hoof is called a what,Frog +General,Person who undertakes commercial venture,Entrepreneur,Science & Nature,At Which Point Do The Temperature Scales Fahrenheit & Celcius Coincide ,-40 ,General,Who wrote the best-seling novel 'The Family'?,Martina Cole +General,"Who earned the moniker ""Lady Lindy""",Amelia earhart,General,Who directed the film The African Queen,John Huston,General,What is the stratosphere higher than,Troposphere +Entertainment,Who sang with 'The Dakotas'?,Billy J. Kramer,General,What actor played the lead in the remake of breathless,Richard gere,General,"Whose autobiography is entitled ""Life is a Roller Coaster""",Ronan keating +General,What is the Capital of: Philippines,Manila,General,The members of a ballet company who do not perform solo.,Corps de ballet,Music,Name the group led by violinist David La Flamme?,It's A Beautiful Day +General,The Aetherius Society believes who is alive and living on Venus,Jesus Christ,General,How many cycles per second in one megahertz,1 million,General,In MASH name Radars pet mouse,Daisy +General,In Memphis Tennessee beggars must have what before begging,A $10 begging licence 1996 law,General,U.S. Captials - Minnesota,St. Paul St Paul,General,What is the deepest land gorge,Grand canyon +General,French artist Aquabouse paints cows in what material,Cow shit,General,"Who is Oscar, Zoroaster, Phadrig, Isaac, Norman,Henkle, Emmanual, Ambrose Diggs",Wizard of Oz,General,Which psychologist invented the terms introvert and extrovert,Carl Jung +General,Where could you legally flash your dong - then spend it,Vietnam currency,Science & Nature, Flamingoes live remarkably long lives _ up to __________,80 years,General,Which plant gets its name from the Persian for turban,Tulip +General,Which actor played Mozart in the 1984 film Amadeus,Tom hulse,General,Who gets the 'picayune intelligence',Frostbite falls,Music,What girls name gave Derek & the Dominoes a 1972 single?,Layla +General,Turnov Rusty and Bobo appear in what stage musical,Starlight Express,General,Name Popeye's hungry friend,J Wellington Wimpy,Music,Which 1999 Pop Hit Translates To English As “The Crazy Life”,Livin La Vida Loca +General,Dan Emmett a northerner wrote which song,Dixie,Music,"Who Had A Hit In 1991 With The Song ""Jump to The Beat""",Danni Minogue,General,"What is defined as 'Dry winged fruits, as of ash or maple, often hanging with others in bunches'",Keys +General,Who was not an original 'charlie's angel',Cheryl ladd,General,"In Greek mythology, leda was visited by zeus in the the form of a swan, and become the mother of ______",Helen and pollux,Music,Who re-recorded 'Secret Agent Man' in 1979?,Devo +General,What is the flower that stands for: suspceptibility,Passion-flower,General,In Wisconsin its against state law to serve apple pie without what,Cheese,General,Caneletto is famous for landscapes of Venice and where,London +General,Roman men had to swear on what to testify,Holding their testicles,General,Who founded the Fascist party in Italy in 1919,Benito mussolini, History & Holidays,Which Government Post Was Held By John Profumo At The Time Of The Affair Involving Christine Keeler ,Secretary Of State For War  +General,Cathy Rigby was the first woman to do what,Pose Nude sports illustrated,General,Quality Street Toffee And Chocolate Assortment Is Named After A Play From Which Famous Author,J.M Barrie,General,"Who led the first band of outlaws to rob a U.S. train, in Adair, Iowa in 1873",Jesse james +General,What is the Capital of: Bermuda,Hamilton, History & Holidays,Which 60's Movie features The Line you wouldn't be able to do those awful things to me if I weren't still in this chair ,What ever happened to Baby Jane ? ,General,Burke and Hare supplied which doctor with cadavers,Dr Knox +General,From the bull what colour is fourth on an archery target,Black,Music,"Maurice White Was A Session Drummer Playing On Chess Hits Such As ""Rescue Me"" By Fontella Bass. Which Soul/Funk Band Did He Go On To Form In The 1970's",Earth Wind And Fire,Food & Drink,What breakfast cereal was invented at Battle Creek Sanitarium?,Cornflakes +Science & Nature, The bat is the only mammal that can __________,Fly,General,U.S. Captials - Califorina,Sacramento, History & Holidays,"Who Had An 80's Hit With The Song 'Politics of Dancing', ",Re-Flex  +General,A shark is the only fish that can do it - do what,Blink with both eyes same time,Entertainment,"What city is also known as Music City, U.S.A.?",Nashville,Science & Nature,How many hearts do earthworms have?,Five +General,"Mozart wrote Ah, Vous Dirai-Je, Maman what's its English title",Twinkle – Twinkle little star,General,"Roosevelt Quotations: ""Do you realize the responsibility I carry I'm the only person standing between Nixon and the White House.""",John F. Kennedy,General,From what modern country does damask come from,Syria +General,What object is said to bring bad luck if it is broken,Mirror,General,What province of Canada has French as its major language,Quebec,General,Which Saints day is 1st March,David +General,Who was Agrippa's son,Nero,General,Connors who did bobby fischer beat to win the world chess championship,Boris spassky, Language,What is the English word for 'fiesta'?,Festival +General,What was the highest grossing american silent movie,Birth of a nation,General,Who captured the first confederate flag in the US civil war,George Armstrong Custer,Sports & Leisure,In Which Sport Is The Americas Cup Awarded? ,Yacht Racing  +General,Collective nouns - a streak of which creatures,Tigers,General,Name vegetable banned different times causing leprosy rickets,Potato, History & Holidays,Which is the name of the baddie in the Harry Potter series ,Lord Voldemort  +General,"For the holy grail in the monty python parody 'search for the holy grail', what did arthur's servant use to make the sound of horses hooves",Empty coconuts,Food & Drink,What is 'water of life' in 'French' ,Eau de vie ,General,98% of Americans feel better about themselves after doing what,Flushing Toilets +General,"Which British statesman, Minister of Labour in the National Government 1940-45, became Foreign Secretary in 1945",Ernest bevin,General,The African Queen Bogart Hepburn but who should it have been,David Niven Bette Davis,General,Which Mediterranean island once housed Napoleon,Elba +General,To which group of artists does Vincent Van Gough belong,Post Impressionists,Science & Nature,Which Record Breaking Video Game Character Made It's Debut May 10 th 1980 ,Pac Man , History & Holidays,Who invented the gatling gun?,Richard Gatling +General,Where did the bay of pigs take place,Cuba,General,In what country is the Pageant of the Golden Tree celebrated,Belgium,General,What did the repair technicians of the first 'modern' computers wear while working,Shorts & roller skates + Geography,What is the basic unit of currency for Cambodia ?,Riel,General,What u.s president did robert montgomery coach for tv,Dwight eisenhower,Music,Who had hit a in the 1990's  with 'Cotton Eye Joe'?,Rednex +Geography,What is the capital of Canada,Ottawa,General,Where in the body are the Haversian canals,Inside bones,General,"Arc, radius, & sector are parts of a(n) _________",Circle +General,"The Mask In The Movie ""Scream"" Was Inspired By The Painting ""The Scream"" But Who Painted It",Edvard Munch,General,Which football team plays home games at Gay Meadow,Shrewsbury,Music,In 1983 To Whom Did The NME Give The First Annual Bryan Ferry 3 - Fitting Suit & Ungainly Dancer Award,Martin Fry +General,"Which motorway is, being developed as a ring motorway round Manchester",M60,General,What job is most likely to make the practitioner an alcoholic,Barbers,General,What airline used to be called Dobrolet,Aeroflot +General,"In ancient Assyria and Babylonia, a tower in the shape of a stepped pyramide. It formed the base of a temple.",Ziggurat,General,What does a lachrymose person do - a lot,Cry,General,"How Is Singer, Actor, Writer & Producer “ Tracy Marrow ” Better Known?",Ice T +Music,What was Richard Wagner composing intermittently between 1848 and 1874?,The Ring Cycle,Food & Drink,What is the head waiter at a French restaurant called? ,Maitre d' or maitre d' hotel ,Sports & Leisure,Before Alex Ferguson Who Was Manager Of Manchester United ,Ron Atkinson (Red Ron)  +Music,Who Are The 2 Members Of Wilson Phillips,Carnie & Wendy Wilson,General,Walt Disney Died Right Before The Completion Of Which Disney Movie?,The Jungle Book,General,What country was the world's oldest man from?,Nepal +General,"Who sang the song ""We Didn't Start The Fire""?",Billy Joel,Music,Which Olivia Newton John 81' album could describe some exercise?,Physical,General,Who was the only sister on petticoat junction to marry?,Betty jo +General,What type of creature is a Fritillary,Butterfly,General,In which sport is the Lance B Todd memorial award won,Man of the Match Rugby League,General,"Other than humans, which is the only animal that can get sunburned",Pig +General,Who was the first British monarch to visit America,George VI in 1939,General,"Which character in ""The Avengers"" drove a Lotus Elan",Emma pefl,General,What instrument is used to measure atmospheric pressure,Barometer + History & Holidays,What 'SG' Do The Salt And Pepper Say To Eachother At Christmas Time ,Seasons Greetings ,General,After Oxford & Cambridge What Is The 3 rd Largest University In Britain ?,Durham,General,As what was Surinam formerly known,Dutch guiana +General,"Many thought this song stood for lsd, but john lennon insisted it was about a girl at his son's school. what is the song title",Lucy in the sky with,General,What is the astrological sign for death?,Pluto,General,What sea lies to the west of Japan,The sea of japan +General,In London when did the Globe Theatre catch fire,1613,Science & Nature,To Which Country Is The Carrot Native? ,Afghanistan ,General,A professional boxer is limited to 36 feet - feet of what,Bandages on hands +General,What pet did Florence Nightingale carry with her,An Owl (in her pocket),General,What is the name of the instrument that is used to test the level of charge in a car battery,Hydrometer,General,"Which writer quoted, 'Men seldom make passes at girls who wear glasses'",Dorothy parker +Music,What Was The Mistake On Elvis Presleys Birth Certificate,Middle Name AAron Was Mispelt,Food & Drink,In Which Alcoholic Drink Might You Find A Worm ,Mescal ,General,Hylophobia is the fear of,Forests +General,What is a flat-bottomed conical laboratory flask with a narrow neck,Erlenmeyer flask,General,What's the capital of jordan,Amman,General,Which island gets its name from the Portuguese for bearded,Barbados +General,In 1778 name the first country to send an ambassador to US,France,General,A short womens jacket without fastenings,Bolero,Music,What is Paul's real birth name?,James Paul McCartney +General,Mohs scale hardest substance is diamond - what's the softest,Talc,General,Bill justis was a studio musician when he recorded this 'sloppy' instrumental in october 1957,Raunchy,General,What is the monetary unit of Malaysia?,Ringgit +Music,Which Best Selling Band Named Themselves After A Price Buster Song,Madness,General,Who became the first President of the French Fifth Republic in 1958,Charles de gaulle,Science & Nature,What is the term for the path followed by a body in space,Orbit +General,Drinking vessel with foot and stem,Goblet,Geography,What is the capital of Jamaica,Kingston,General,What is the name of the ruling house of Monaco,Grimaldi +General,Which company produced the syndicated mouse factory series,Walt disney,Sports & Leisure,Who Was Sportswoman Of The Year In 1971 ,Princess Anne ,Science & Nature,What does the body release that dilates small blood vessels and so causes a person to blush?,Peptides +General,"What term has gradually replaced ""jungle"" because it ""has a nice ring to it,"" according to William Safire",Rain forest,General,What is a dish of fried potato often eaten for breakfast,Hash browns,General,A Curofact is a sexual fetish about what,Legs + History & Holidays,Who wrote Auld Lange Syne? ,Robert Burns ,General,What is a slang name for diamonds,Ice,General,In Las Vegas which gambling thing generates the most profit,Slot Machines +Science & Nature,Term for an emasculated male pig,Barrow,General,What is the name given to male sheep?,Ram,Food & Drink,What Is The Main Ingredient In A Black Pudding ,Pigs Blood  +General,What is the main food of most bats,Insects, History & Holidays,"Deriving its name from the Latin word meaning knowledge and a Greek word meaning branch of learning which church was founded in 1954 in California, moving it's headquarters to Sussex in 1959? ",Church of Scientology ,Science & Nature," The American opossum, a marsupial, bears its young just 12 to 13 days after conception. The Asiatic elephant takes 608 days to give birth, or just over __________",20 months +General,What appears in the middle of the Rwandan flag,Capital R,General,Which river seperates Buenos Aires from Montivideo,River plate,General,"What has u.s. patent no 174,465",Telephone +Entertainment,This film starring Richard Beymer and Natalie Wood won the best picture Oscar for 1961.,West Side Story, History & Holidays,Which Wall Did The Romans Build To Keep Out Marauding Scots? ,Hadrians Wall , Geography,"Linz, Austria is a leading port on which river?",Danube +Music,What Is The Real Name Of Rick Nelson,Erick Nelson,General,What Was The First US State To Abolish The Death Penalty,Michigan,Sports & Leisure,Baseball: The Atlanta ______?,Braves +Music,The Chipmonks Were The Bran Child Of David Seville But What Was Sevilles Real Name,Ross Bagdasarian,General,VH is the international aircraft registration for which country,Australia,Science & Nature,What Is The Main Diet Of The Pangolin ,Ants & Termites  +General,What countries international car registration letters are DZ,Algeria,General,The Dance of the Sugar Plum Fairy comes from which piece by Tchaikovsky,Nutcracker suite,Science & Nature, The ancient nautilus is considered the most intelligent of the invertebrates; it is said to have been as intelligent as a __________,Young cat +General,"In medical descriptions, what is the meaning of the term ""chronic""","Lingering, lasting",Food & Drink,From what country does Advocaat originate? ,Holland ,General,Who was murdered by the Manson family in 1969,Sharon tate +General,Which item first appeared in Superior Hotel Montana in 1908,Gideon Bible,General,What motto do brownies pledge,Lend a hand,General,Who are Mickey Mouse's nephews,Mortie and Ferdie +General,On which river does the city of Oporto stand,Douro,General,What kind of feathers does an emu have,Double-plumed,General,What is a bouvier des flanders,Dog +Sports & Leisure,"How Many Times Was Stephen Hendry Crowned World Champion In The 1990's Was It (6,7,8,9) ",Seven ,General,"Which country's name means ""equator""",Ecuador,Sports & Leisure,"Which Of These Sports Does Not Allow You To Play Left Handed (Polo, Squash, Fencing, Tennis) ",Polo  +General,McLean Stevenson played which character in MASH,Colonel Blake,General,The term Sesquitercentennial represents how many years ?,350,General,Calvados or Apple Brandy and Dubonnet make what cocktail,Bentley +General,Old fashioned word for a prostitute,Harlot, History & Holidays,In What Year Did Great Britain Gain Control Of Hong Hong ,1842 ,General,The Glis Glis was fattened and eaten by the Romans what is it,Edible Dormouse +General,Who was the Norse god of peace & prosperity,Frey,Art & Literature,What Is The Nationality Of Picasso ,Spanish ,Geography,What Was So Special About Brooklyn Bridge ,It Was The Worlds First Steel Suspension Bridge  +General,Which comic character is both a princess and a prince,Wonder Woman – Diana Prince,General,In what country do they answer the phone by saying I'm listening,Russia,General,"Forked, sheet and ball are types of ______",Lightning +General,"Film title ' ______, a space odyssey'",2001,General,Which Long Running TV Show (1976-1981) Took Place At The Vaudeville Theatre,The Muppet Show,General,Where would you find Fiordland National Park,New zealand +General,Colonel Jacob Schick invented what in 1928 in USA,Electric Razor,General,Who became emperor of France in 1804,Napoleon,General,"Sand, Soda and what are the main ingredients of glass",Limestone +General,Name the swimmer who became a Hollywood star in the 1940's and 50s in films such as Bathing beauty and Neptune's Daughter,Esther williams,General,Who is the most filmed comic strip character,Zorro,General,Frank Gorshin played what role in a 60s series films,The Riddler +General,What is the opposite of allopathy,Homeopathy,Science & Nature," In the air, puffins are powerful flyers, beating their __________",Wings,General,"In England, what is the Speaker of the House not allowed to do?",Speak +General,Who owns the Audi car company,Ford motor company,General,Which companies first product was an audio oscillator,Hewlett Packard,General,Agatha Christie disappeared for 10 days in which year,1926 +General,"What does the word ""amen"" mean",Let it be,People & Places,Which Sportsmans Name Was Hurricane ,Alex Higgins ,General,In which Jane Austen novel does Fanny Price appear,Mansfield park +Geography,In which state is hoover dam ,Arizona ,General,Which author created Dick Tracy,Chester Gould,General,Which Actress Played Queen Amidala's Double In The Sci Fi Movie Star Wars The Phantom Menace,Keira Knightley +General,Who wrote the children's classic Ann of Green Gables,L M Montgomery, Geography,What is the capital of Uruguay ?,Montevideo,General,What name is Samuel Langhorne Clemens known by,Mark twain +General,Gertrude Ederlie (USA) was the first woman to do what,Swim English Channel,General,Russian word means dissolute - nickname Gregory Efimovitch,Rasputin,Science & Nature,What is the device in a common household microwave called ,Magnetron  +General,Whats the name given a star that has collapsed into no dimensions,Black hole,General,A small informal restaurant,Bistro,Music,Which Musical Includes the ’1978 hit “ Summer Nights”?,Grease +Entertainment,What is the name of the Family Circus's dog,Barf,General,"What Is The Name Of The Boy In The Christmas Animation ""The Snowman""",James,General,Which U. S. State did Ray Charles have on his mind on a 1960 single,Georgia +Music,Singer Chrisssie Hynde Married Which Member Of Simple Minds,Jim Kerr,General,"On Saved By The Bell,what was the name of the beach club that the gang worked at?",MalibU.S.ands Beach Club,General,In heraldry gules are what colour,Red +General,The flower convallaria is better known as what,Lily of the Valley,General,A bergschrund is a crevasse at the head of a(n),Glacier,General,The Nobel Peace Prize Is Awarded Every Year In Which European City,Oslo +General,Norse mythology who was killed with mistletoe by blind Hodur,Balder - most loved god,General,What is the fear of german or german things known as,Teutophobia,Technology & Video Games,What type of printer did Seiko develop for the 1964 Tokyo Olympics?,Dot matrix +Food & Drink,Which vegetable has the most calories? ,Avovado ,General,What is the flower that stands for: slighted love,Yellow chrysanthemum, History & Holidays,Which entertainer famous for the one liner 'My Little Chickadee'' died on Christmas Day 1946 ,W.C. Fields  + Geography,Through which ocean does the International Date Line approximately follow the 180 degree meridian?,Pacific Ocean,General,What was Yosser's catchword,Gizzajob, Geography,Which element makes up 46.6% of the Earth's crust ?,Oxygen +General,What is the more common name for 'self contained underwater breathing apparatus,Scuba,General,What is the Capital of: Nigeria,Abuja,General,Musical instrument of bells or metal bars played with hammers,Glockenspiel +General,Who starred in cartoon where the Tasmanian Devil 1st appeared,Bugs Bunny – Devil may Hare,General,Which french dramatist's works include Phedre and Andromaque,Jean racine,General,What did the 1st u.s federal legislation in 1909 prohibit,Narcotics +Art & Literature,"This man was Time magazine's 1938 ""Man of the Year""",Adolf hitler,General,Baseball: the toronto ______,Bluejays,General,Where could you find a 1925 humpmobile car,Back of a US $10 bill +General,What is the Capital of: Cyprus,Nicosia, Geography,What is the basic unit of currency for Saint Vincent and the Grenadines ?,Dollar,Geography,Which U.S. city is known as the Biggest Little City in the World,Reno +General,How fast does the tip of a standard rotary mower travel? (in km/h),200,People & Places,What is the only country which is crossed by both the Equator and the Tropic Of Cancer? ,Brazil ,General,What game are the Pilgrim Fathers known to have played on the Mayflower,Darts +General,Who is the Roman Goddess of Hunting,Diana,General,Who was known as the Little Brown Saint,Ghandi,General,What is the Capital of: Netherlands Antilles,Willemstad +General,"On Full House,what was Jesse's REAL first name?",Hermes,Geography,In what country is mandalay ,Burma ,General,What is the Capital of: Faroe Islands,Torshavn +General,What is a nibong a type of,Palm tree,General,"One of the twelve members of the administrative council of the Mormon church, is called a(n) ______.?",Apostle,Music,"What recording artist made ""Call Me"" into one of the top hits of 1980",Blondie +General,Who won an Oscar as Best Supporting actor in the 1993 film 'The Fugitive',Tommy lee jones,General,In what Elvis film does he play a hillbilly garage hand,Loving You, History & Holidays,Who in March 1958 became known by the number 53310761? ,Elvis Presley  +General,Who is frank cujo now known as,Jean-claude van damme,Science & Nature,What word is used for a female fox?,Vixen,General,Where is the 'Day of the Dead' celebrated,Mexico +General,What is the fruit of the hawthorn called,Haw,General,Septime Is a Position In Which Sport?,Fencing,General,February 21st 1878 the first what was published in New Haven,Telephone Directory +General,"What can be Safety, Tableaux or Swag",Curtains in theatre,General,What did andrew jergens create,Jergens lotion,Science & Nature, __________ are powerful jumpers. A 20_inch adult can leap 20 feet in a single bound.,Jackrabbits +General,What did eli whitney invent,Cotton gin,Sports & Leisure,Which Italian football club did Paul Gascoigne turn out for? ,Lazio ,General,Which childrens classic did Johann David Wyss write,The swiss family robinson +General,1899 what first was installed Palace Royal hotel San Francisco,Jukebox, History & Holidays,Which Bond film does the character Dr Christmas Jones feature ,The World Is not Enough ,Entertainment,Who always tried to kill Krazy Kat?,Captain Marvel +General,Martin what 1980's tv series starred bruce willis,Moonlighting,General,What's the telephone area code for Chicago?,312,Music,"From the 1980's “ Let's hope you never leave old friend, Like all good things on you we depend”?",Queen / Radio GaGa +General,"In mediaval history, who was the lover of Heloise",Abelard,General,"Where are ""coquis"" fluently from",Puerto Rico, Geography,What is the capital of Liechtenstein?,Vaduz +General,"Who is only artist that toured with Elvis, Beatles and Eagles",Roy Orbison,General,The term Sesquiquadricentennial represents how many years ?,450,General,In 1969 John Lennon & Yoko Ono Staged Their Honeymoon Protest By Staying In Their Hotel Beds For A Week. But In Which European Capital City Did This Take Place,Amsterdam +General,"H.R. Haldeman and Ron Ziegler, who helped plan the __________ burglary for President Nixon, both worked at Disneyland when they were younger. ",Watergate,General,What is the high flying swing in a circus called,Trapeze,General,The film Chinatown was released in what year,1974 +General,Official scrutiny of accounts,Audit,General,Name the Indian triangular pastries stuffed with meat or vegetables,Samosas, History & Holidays,"Rudolph the Red-Nosed Reindeer was a character created in a story written in: A=1939, B=1949, C=1954,D=1959 ",A=1939  +General,What is the only Christian country in Asia,The Philippines,General,In medieval times what was a mangonel used for,It was a siege catapult,General,"In Greek mythology, who did Ganymede replace as Cup-Bearer to the Gods",Hebe +General,Collective nouns - Team Plump Flush Safe Smeath of what,Ducks, History & Holidays,Which Actress Starred As Columnist 'Jane Lucas'' In The TV Sitcom Agony ,Maureen Lipman ,General,What star only began singing when she broke her leg,Doris Day was a dancer +General,"Rhapsody, Aromel, Tamella Cambridge favourite types of what",Strawberry Varieties, History & Holidays,Which witch is credited in the Bible with raising the spirit of Samuel at the request of King Saul ,Witch of Endor ,General,Which item of clothing do Eskimos (or Inuit) call mukluks,Sealskin boots +General,During which conflict did the battles of Alma and Inkermann take place,The crimean war,General,What is the young of this animal called: Rhino,Calf,General,What item used in offices was voted the product of the century,The Paperclip +General,What is Bart Simpson's middle name,JoJo,Music,"Who Was A Member Of The Byrds & The Flying Burrito Brothers, & Recorded The Solo Albums ""Gp & Grevious Angel""",Gram Parsons,General,As What Are Craig & Charlie Reid Better Known as?,The Proclaimers +General,Building for housing aircraft,Hanger,General,What elements name comes from the Greek for light bearing,Phosphorous,Science & Nature,What is the term for the group of plants that catch and digest insects?,Carnivorous +General,Where did the card game 'bridge' originate,Turkey,Music,"Who Had A Hit With The Song ""The Tide Is High""",Blondie,Music,In 1983 Rita Coolidge Performed The Song “On An All Time High” But In What Bond Movie Did It Feature?,Octopussy +General,The official 'Battle of Britain Memorial Flight' museum is based at which RAF station?,RAF Coningsby,General,Whose first 2 books are entitled The Lost Continent and Neither Here Nor There,Bill bryson,General,In which country do they play houlani - type of hockey,Turkey +General,Architectural style developed in the Eastern Empire,Byzantine,Sports & Leisure,Who beat Betty Stove to win the women's singles title at Wimbledon in 1977 ,Virginia Wade ,General,Frozen dew or vapour,Frost +Geography,Bridgeport is the largest city in which state ,Connecticut ,General,Thurle Sandstorm first world champion in 1923 at what sport,Ten Pin Bowling,General,What is the hole in a pencil sharpener called,Chuck +Science & Nature,What Type Of Mammel Is The Tasmanian Devil? ,A Marsupial ,General,Englund what was the first ironclad warship launched,Hms warrior,Music,What Did Odetta Tell Harry To Fix,The Hole In His Bucket +General,Where is the dalai lama's palace,Tibet,General,"The ""l.l."" In l.l. Bean stands for what",Leon leonwood,General,"Which horticultural fungicide, consisting of equal parts of copper sulphate and lime, bears the same name as a French port",Bordeaux mixture +General,In law what is a co-parcener,Joint Heir,General,An exultation is a group of what animals,Larks,General,In publishing what is the verso,Left page - Recto right page +General,Frigophobia is a fear of ______,Cold,General,"In a game of horseshoes, how many feet apart must the stakes be",Forty feet,General,What is the flower that stands for: a beauty,Orchis +General,Thomas Minton at Stoke on Trent created what in 1789,The Willow Pattern,Science & Nature,"In Edward Lear's Poem, Which Bird Sang To The Pussycat ",The Owl ,Music,"Who Recorded The Album ""Around The World In A Day""",Prince +General,What was first used at the 1904 St Louis Olympic games,Gold medals silver was first before, History & Holidays,What is the most popular Halloween decoration ,Jack-o-Lantern ,General,To what family does the wigeon belong,Duck +General,Why did eggs become significant at Easter? ,They represent fertility & new beginnings at spring time ,General,Who is the Patron Saint of learning,St Ambrose, Geography,Which is the Earth's second largest continent ?,Africa +General,80% of vibrator using English women don’t do what with it,Insert it,General,The Vietnamese call it The Brother the Chinese The Friend what,Bamboo,Science & Nature,On which planet are the craters Brahms and Liszt?,Mercury +General,The film 'The Wizard of Oz' was released in which year,1939,General,Cheap ornaments and trinkets,Bric-a-brac, Geography,In which continent would you find the Mekong river ?,Asia +General,Who was born in Limbini Nepal,Buddha,Music,In 2007 What Song Was Recently Voted The Most Annoying Song Of The Decade?,James Blunt / You're Beautiful,Music,"Who Had A Hit In 1989 With ""I Drove All Night""",Cyndi Lauper +Music,Name The First Top Ten Hit For Genesis Reaching No 7 In 1978,Follow You Follow Me,Food & Drink,"A Linzer torte, named after an Austrian town has a ground nut pastry base spread with Jam. What type of topping does it have? ",Criss Cross pastry strips ,General,What's the American term for a flat,Apartment +Music,"With Which Rock Band Does ""Slash"" Play Guitar",Guns N Roses,Geography,What is the capital of Sudan,Khartoum,General,Where was the greatest snowfall ever recorded in a single storm,Mount shasta +General,John Hetherington in London introduced what in 1797,Top Hats,General,"Which country has been called ""the gift of the nile""",Egypt,Music,"Which 2 Singers Married In 1969 , Divorced In 1975, & Recorded One Last Album Together In 1995",George Jones & Tammy Winette +General,In their lifetime the average human grows 8 feet of what,Nose Hair,General,A large fortified residential building,Castle,General,"Film - Who played ""Annie Hall""",Diane keaton +Music,Who Is Generally Credited With Freeing The Role Of The Jazz Guitar To That Of A Solo Instrument,Charlie Christian,General,What was Peter Blake a pop art designers most famous work,Beatles Sergeant Peppers cover,General,What state has a former pro wrestler as governor,Minnesota +General,"Who recorded the album ""Freak Out""",Frank zappa,General,Who would wear motley,Jester clothing,General,Which actor appeared in drag in two Marlene Dietrich films,John Wayne +General,Who was codenamed Napoleon by the secret service,Frank Sinatra,Food & Drink,What is common name for the fruit Citrus Grandis? ,Grapefruit ,General,What is a Cattalo,Buffalo and cow cross + Geography,What is the capital of Singapore?,Singapore,General,What does a pilot drop to slow an airplane,Flaps,Entertainment,Who invented the electrical bass?,Leo Fender +Music,Name The European City Tjhat Was A Hit For Ultravox In The 80's,Vienna,General,In which G&S operetta is eating a sausage roll a secret sign,The Grand Duke,Food & Drink,Which ingredient was added to ale in Bavaria around 900 AD and worked the Wonder that gave us beer ,Hops  +General,During which month is the longest day in the Northern hemisphere?,June,General,What city was the leader of the delian league,Athens,General,Who figured out how to win friends and influence people,Dale carnegie +People & Places,For what are Allen and Wright most famous?,Root beer,Geography,Where Was Henry Stanley When He Uttered The Phrase (Dr Livingstone I Presume) ,Ujiji On The Shores Of Lake Tanganyika ,General,Where would you be if you landed at Santa Cruz airport,Bombay + History & Holidays,"Who took the title ""Lord Protector of the Commonwelth of England, Scotland, and Ireland"" ?",Oliver Cromwell,General,In Animal Farm what kind of creature was Bluebell,A Dog,General,Female bathing caps were invented to prevent what,Clogged up Drains +General,What U.S. state includes the telephone area code 601,Mississippi,Food & Drink,What Is Feta A Type Of ,Cheese ,General,"What struck honshu island, japan in 1934 killing 4,000 people",Typhoon +General,The remains of prehistoric organisms that have been preserved in rocks are called ________,Fossils,General,In which sport would you hear the term cleek,Golf it’s a wood,General,A young unmarried woman,Damsel +General,Who was john reid,Lone ranger,General,What is the scientific name for earwax?,Cerumen,General,What type of fish is Scomber Scombrus,Mackerel +General,What is a person who maliciously starts fires,Arsonist,General,What is another name for the Feast of the Annunciation,Lady day,General,What colour is the ferrari emblem,Yellow +General,Who painted the Laughing Cavalier?,Franz Hals,General,Who sang about Sylvia's Mother,Dr Hook,General,Who predicted in the 1500s that a man named Franco would provoke a civil war in Spain,Nostradamus +General,In the abbreviation VDU what does the V stand for,Visual,Entertainment,Who played 'The Scorpion King' in the recent movie 'The Mummy Returns'?,Dwight Johnson,General,The Chinese only do it every 10 years – what,Celebrate Birthdays +General,What name is given to a boxer who leads with his right hand,Southpaw,General,What sport was observed by Captain James Cook in 1771,Surfing,Music,"How Big Was The First Ever Chart In 1952 , Top 10, Top 12, Top 20, Top 100",Top 12 +General,What job has a pudentacurist,Shapes pussy hair,Science & Nature,Where Was The First Alam Clock Produced ,In Germany In 1360 ,General,After The Red Baron was shot down who took over his squadron,Herman Goering +General,Many female children are named 'friday' after which patron saint of oxford,St frideswide, Geography,What is the largest lake in Europe?,Lake Lagoda,General,"Generation X Toys: Once scarce, pudgy dolls that came with their own birth certificates",Cabbage patch kids +Music,Ride On Time Spent 6 Weeks At No.1 In The UK In 1989 For Whom,Black Box,General, Legal Terms: The people chosen to render a verdict in a court.,Jury,Music,Who Has Both The Top Two Best Selling Heavy Metal Albums Of All Time In The UK,Meatloaf (Bat Out Of Hell and Bat Out Of Hell 2) + History & Holidays,Which 1950s films took place in Hawaii 1941 ,From Here to Eternity ,General,Which is the largest island in the Caribbean,Cuba,General,What do the letter RCMP stand for,Royal canadian mounted police +Music,"Name The Group That Had A Hit With ""Inside"" The Musical Backdrop To A Levi's Ad",Stiltskin,General,In the 1976 Olympics who were the Yellow Bananas,Officials (cos of uniform colour),General,Where was the battle of Hastings fought,Senlac hill +Science & Nature,"If you're in the northern hemisphere, Polaris, the North Star, can be found by looking which direction",North,General,What disease do the French call la rage,Rabies,General,What is a whale's penis called,Dork +General,"Who recorded the album ""Troubadour"" in 1976",Jj cale,Science & Nature,In Engineering What Do The Initials CAD stand for ,Computer Aided Design ,Food & Drink,What name is given to rough cider distilled from withered apples? ,Scrumpy  +General,From which team did marlboro switch its backing to mclaren in the 1974 season,Brm,Art & Literature,Who wrote 'Valley Of The Dolls'?,Jacqueline Susann,General,Ancient China Treason Robbery Adultery what punishment,Castration +General,What common item were once called moth patches,Freckles, History & Holidays,What automotive flop was named for the only child of henry ford ,The edsel , History & Holidays,Who Was Shot Dead In Dallas On 24th November 1963 ,Lee Harvey Oswald  +General,In what did ray walston play 'uncle tim',My favourite martian,Music,"How Many Times Was George Michaels Video ""I Want Your Sex"" Re-Edited Before MTV Would Show It",3 Times,General,Who performed the original version of the song entitled 'Song to the Siren' which has since been covered by numerous well known artists?,Tim Buckley +General,What TV series had a signature tune called Liberty Bell,Monty Pythons Flying Circus,General,The Creators Of Which Cartoon Characters Were Threatened With Legal Action By Oasis In 1996 If They Covered Any Of Their Songs?,The Smurfs,General,What language has the most words,English +General,What is the most famous 500 mile car race in the U.S.,Indianapolis 500,General,In what sport would you find a Tell Tale,Squash - Tin strip ball can't hit, History & Holidays,"Bing Crosby famously sang 'I'm dreaming of a white Christmas'', what is the next line. ",Just like the ones I used to know  +Food & Drink,What Is A Boullabaisse ,A French Fish Soup Or Stew ,General,What did the word bald originally mean,Clean or White,General,What is a receptacle for holy water,Font +Art & Literature,What is the opposite of an utopia?,Dystopia,General,Who recorded blue morning blue day in 1978,Foreigner,Entertainment,What is the frog's name in 'The Muppet Show'?,Kermit D Frog +Music,Terry Hall Of Fun Boy Three Later Formed A New Band Was It Called The Colourbox Or The Colourfield,The Colourfield,General,What Is The Worlds Most Tornado Prone Country,Uk because of it's large coastline in ratio to it's surface area,General,Montevideo is the capital of ______,Uruguay +General,What act do the French call The English Perversion,Whipping Flagellation,General,In Denver Colorado it is illegal to lend what to your neighbour,Vacuum Cleaner,General,What is the study of mankind called,Anthropology +General,Lauds Prime Tierce Sext Nones what comes next,Vespers,Sports & Leisure,Who was the only woman to win an athletics gold medal for Britain in the 1992 Barcelona Olympics? ,Sally Gunnell ,General,"In Greek mythology, phoebe was the goddess of the ______",Moon +General,What U.S. state grants the most fishing licenses,California,General,EL is the international aircraft letters of which country,Liberia,General,In a survey 32% of wives would change their husbands what,Weight +General,What was the language of ancient india,Sanskrit,General,What author said 'accidents will occur in the best regulated families',Charles dickens,General,Jerome napoleon bonaparte died in 1945 of injuries sustained from tripping over his ______,Dog's leash +General,Humbert Humbert' is a character in which book,Lolita,General,Sufferers from lambdacism cannot do what,Pronounce letter R,Mathematics,What is the minimum number of integer degrees in an acute angle?,One +Entertainment,"Formerly with Spencer Davis, he went on to form Traffic with Dave Mason. He is?",Steve Winwood,General,Your ____ holds your head to your shoulders,Neck,General,"Who was the ""gatekeeper"" in Ghostbusters?",Sigorney Weaver +General,Who was the author of the novel 'The Midwich Cuckoos'?,John Wyndham,General,"Where, on cattle, is the dewlap",Under the throat,General,"In England what can be private, public or approved",Schools +Sports & Leisure,Who is known as (King Kenny)? ,Kenny Dalglish ,General,Stepping directly onto the point of a foot.,Piqué,General,What is measured by an interferometer,Wavelength of light +General,What bone connects your shoulder blade & elbow joint,The humerus humerus,General,Until 1955 in England you needed a licence to take what on road,Lawn Mower,General,What is the symbol for iron,Fe +General,The German New Year's carnival,Fasching,General,"Like a lady in 'the simpsons', sideshow bob's criminal number is the same as what character in 'les miserables'",Jean valjean,General,What is the Latin name for the North Star,Polaris +General,Who was the male star of the 1967 film Barefoot in the Park,Robert redford,General,Whose first book was called Child Whispers,Enid Blyton,General,Which Country Has Montivideo As It's Capital,Uruguay +General,Over which islands did britain and argentina fight in 1982,Falkland islands,Science & Nature,Which Carnivorous Plants Natural Habitat Consists Of A Small Coastal Area Between North & South Carolina ,Venus Flytrap ,General,As sick as a ______,Dog +General,What was the first Pink Floyd album,Piper at the gates of dawn,General,"Anthropoid (Ape) of equatorial Africa that, physically and genetically, is the animal most closely related to humans?",Chimpanzee, History & Holidays,In December 1967 53 Year Old Louis Washkansky Became The First Person To Undergo What ,A Heart Transplant  +Sports & Leisure,As who is Cassius Clay now known?,Mohammed Ali,General,"Who, in Greek mythology, was chained to rock with an eagle picking at his liver",Prometheus,General,"Where Will You Come Across The Substances Of Sodium Thiopental, Pancuronium Bromide, Potassium Chloride",Lethal Injection +Toys & Games,Where did the card game 'bridge' originate?,Turkey,Food & Drink,What Does The Name Of The Veal Dish 'Saltimbocca' Mean? ,Literally 'Jump In The Mouth' In Italian ,Food & Drink,The flop 1960's diet drink Minivitine was a spinoff of this drink mix.,Ovaltine + History & Holidays,The festival of Halloween was first celebrated by which ancient tribe ,The Celts , History & Holidays,With three words complete this movie trailer catch line for the film Alien. 'In space no one can_____' ,Hear you scream ,General,"What submarine vanished on May 21, 1968",The scorpion +General,The city with the most riders in it's subway system is what,Moscow,General,"Who said ""Bigamy is one husband too many like Monogamy""",Erica Jong - Fear of Flying 1973,General,Who was the first driver to wear a helmet in the indy 500,Eddie Rickenbacker +General,"James Hepburn, the fourth Earl of Bothwell, died in 1578. Who did he marry in 1567",Mary Queen of Scots,Sports & Leisure,What are the two basic aids in orienteering?,Map and compass,General,What actor was born Krishna Bhanji,Ben Kingsley +General,What is the Hobbit's favourite food,Mushrooms,General,What is the white part of an egg called,Albumen,Geography,How Many Countries are There In South America ,Thirteen  +General,"Which eighties album, that sold 20 million plus copies, featured Vincent Price",Thriller,Technology & Video Games,secret of Evermore' was entirely produced in which country? ,U.S.A.,General,What would you do with your koko in Japan,Play it – Musical instrument +General,In the Winnie the Pooh stories what is Kanga’s baby called,Roo,General,What is the Capital of: Nepal,Kathmandu,General,New Orleans USA - Southampton GB same nick football team,Saints +General,What is the most popular South American aphrodisiac,Piranha head soup,General,"What country is headed by King Fahd Ibn Abdul Iziz, one of 44 sons sired by a 22 wife dad",Saudi arabia,General,From which fruit is the liqueur obtained,Cherry +General,What did clio represent in the nine muses,History,General,USA has most roads what country has second most,India,General,How many Nightmare On Elm Street movies were made in the 80's?,5 +General,What is the name of the leading female star in an opera,Prima donna,General,In which London park do deer roam free,Richmond, History & Holidays,What duo lost their Grammy for Best New Artist from the eighties? ,Milli Vanilli  +General,What was George Hepplewhite's profession,Furniture maker,General,Beagles were a hunting dog bred to hunt what,Hares,Art & Literature,"Design style prevalent during the 1920s and 1930s, characterized by a sleek use of straight lines and slender forms.",Art deco +General,What is a collection of penguins known as,Rookery,Music,What Do PJ Harveys Initials Stand For,Polly Jean,General,In which sport would you hear the term bedposts,Ten Pin Bowling a 7 – 10 split + History & Holidays,In the Christmas song 'White Christmas' what did children listen for? ,Sleigh Bells ,General,What is the flower that stands for: chaste love,Acacia,General,"What's the international radio code word for the letter ""Y""",Yankee +General,What is the more common name of Sildenafil Citrate,Viagra,General,Which Country Has The Currency The Tugrik,Mongolia,General,Who was the world's first public computer information service,Compuserve +General,One person every 6 seconds dies from what,Contaminated water diseases,General,Daffodils' belong to which genus of bulb,Narcissus,General,"A curved triangle at the corners of a square or polygonal room, used at the opening of a dome.",Pendentive +General,Girls name can mean big fruit basket or a meeting whaling captains,Molly,General,What did the first issue of Playboy in 1953 not have,Date - unsure if it would continue,Music,Which Re-Issued Abba Track Got To Number 16 In 1992,Dancing Queen +Science & Nature,A bone specialist is a(n) ________.,Osteopath,General,Which is considered the most powerful piece on the chess board,Queen,General,What US city is named after vice president of the mid 1840s,Dallas +General, Legal Terms: A crime more serious than a misdemeanor.,Felony,General,Which is the earliest US military award for service beyond duty,Purple Heart,General,Who ordered John the Baptists execution,King Herod +General,Who was the longest reigning Prime Minister of Britain in the 20th Century?,Margaret Thatcher,General,Which canadian province was formerly called acadia,Nova scotia, History & Holidays,Who Released The 70's Album Entitled New Boots and Panties ,Ian Dury  +General,Who was the male star of the 1998 'blockbuster' film Titanic,Leonardo di caprio,General,Who hosted the 1999 cricket world cup,England,General,Who was The First Actor To Refuse An Oscar?,George C Scott +General,Which actor played the marine expert Matt Hooper in the film Jaws,Richard dreyfuss,Geography,In which city is the Bridge of Sighs,Venice,General,Who Is The Singer Michael Barret Otherwise Known?,Shakin Stevens +General,Which former British TV celebrity designed the logo for children's TV programme 'Blue Peter'?,Tony Hart, History & Holidays,"Born on Christmas Day 1887, this American tycoon, created one of the largest hotel chains in the world (Surname) ",Conrad Hilton ,General,A Robert Heinlein book won 1960 Hugo award name it,Starship Troopers +General,"Martin British rock-music group that rivaled the popularity of the group's early contemporaries, The Beatles",The rolling stones,General,How many sides does a baseball homeplate have,Five,General,Which islands wildlife is 90% unique,Madagascar +General,Which American state is nicknamed 'The Empire State of the South' or 'Peach State',Georgia,General,Which county has the most MEPs 99,Germany,General,What was ludwig von beethoven once arrested for,Vagrancy +General,Galena is a major ore of which metal,Lead,General,Which planet has a satellite called Cordelia,Uranus,General,What was banned in China in 1911 as a sign of feudalism,The Pigtail +Food & Drink,Roast turkey: does white/dark meat have most calories? ,Dark ,Music,Which Group Released The Album Definitely Maybe,Oasis, History & Holidays,What is the name of the Russian Czar's daughter who might_or might not_have survived the Russian revolution,Anastasia +General,The canary islands in the pacific are named after what animal,Dog dogs,Science & Nature,Which tree only produces acorns after it is fifty years old?,Oak, Geography,What is the basic unit of currency for Liberia ?,Dollar +Sports & Leisure,Susan Brown Was the first woman to Take Part In which race ,The Boat Race ,General,What was the name of the ghostly st bernard in topper,Neil, History & Holidays,What late night news show became popular in the eighties after the Iranian Hostage takeover? ,Nightline  +General,Fly that bites cattle,Gadfly, Geography,What is the capital of Grenada ?,Saint George's,Music,In Which Musical Was There A Dance Routine Set At A Barn Raising,7 Brides For 7 Brothers +General,Name the ship lost off Zuyder Zee in 1799 from which a famous item was salvaged in 1858,Lutine,General,Which Bands Name When Translated To English Literally Means Fast Fashion,Depeche Mode,Sports & Leisure,Why Was Number Six Valverde Famous In The Year Of 2006? ,Won The Grand National  +General,The skin of which animal is used to make Morocco Leather,Goat,Music,"""Debora"" And ""One Inch Rock"" Were The First Chart Entries For Which Soon To Be Legends",T-Rex,Music,"Who Recorded The Albums ""Lets Dance"", ""Tonight"", And Black And White Noise",David Bowie +General,Which French actor director takes the role of 'Monsieur Hulot' in films such as Mon Oncle and Traffic,Jacques tati,General,What film star was born in Sakhalin Siberia,Yul Bryner,General,What was the name of the I.B.M. computer which played Chess against Gary Kasparov,Deep blue +General,What is the world's largest desert,Sahara desert,General,What ocean current moderates the weather in north-western europe,Gulf stream,General,Which was Dickens' first novel,Pickwick papers +Geography,Where Is The Glenveagh National Park ,"County Donegal, Ireland ",General,What is the fruit of a rosebush called,The hip,General,What is the stage name of film actress Caryn Johnson born 1949,Whoopi goldberg +General,A ship due to leave port flies a 'Blue Peter'. What does the flag look like,Blue rectangle with a white rectangular centre,General,What canadian city was carling beer first brewed in,Toronto,General,Who was the first U.S. President to visit China,Richard m nixon + History & Holidays,Which actress caused a sensation by appearing naked in the opening scene of the 1957 film _And God Created Woman _directed by her then husband Roger Vadim ,Brigitte Bardot ,General,Mother the car what's the name of garfield's teddy bear,Pooky,General,Traditional 7 Seas N S Atlantic N S Pacific Arctic Antarctic ?,Indian +General,What is the name for the number 1 followed by 100 zeros,Google,General,What is the Capital of: Botswana,Gaborone,General,"Which sport is featured in the book and film ""This Sporting Life""",Rugby league +General,According to psychologists the happiest people watch what TV,Soap Operas, History & Holidays,Who is generally given credit for the term 'Rock and Roll? ,Alan Freed ,General,What German city is best known for its Oktoberfest,Munich + Geography,The sun sets in the ____?,West,Entertainment,Name the late actor who played Obi-Wan Kenobi in Star Wars?,Alec Guiness,General,What is a group of chicks,Brood +General,What Foodwise Is A Munster Plum,A Potato,Sports & Leisure,"What sport has sprint, tandem and team pursuit events?",Cycling,Art & Literature,Who Said When A Man Is Tired Of London He Is Tired Of Life ,Dr Samuel Johnson  +General,Luxembourg is the capital of ______,Luxembourg,General,"What's the parent company of the ""jack-in-the-box"" hamburger chain",Ralston,General,What is the third part of the 'Lord Of The Rings' trilogy,Return of the king +Music,Name One Of The Two Classic Tracks On A Barry Manilow Single Released In 1978,Copacobana & Somewhere In The Night, History & Holidays,What Happened To Alaska In 1867? ,It Was Sold By Russia To The United States ,General,Who was known as 'The last of the Red Hot Mommas',Sophie tucker +Science & Nature,What Does A Chiropodist Treat ,The Feet ,General,Arthur Flegenheimer died Oct 1935 was better known as who,Dutch Schultz,General,What male human feature was taxed in Elizabethan times,Beards +General,Who made his debut in a 1955 Warner Brothers cartoon,Speedy Gonzales,General,What does yellow gold contains 10% of,Copper,Music,Who Was Originally Known As Harry Webb,Sir Cliff Richard +General,Spanish dictator Franco nominated whom as his successor,Prince juan carlos,General,What colour would the sky be if viewed from mars,Pink,Sports & Leisure,In which country was netball invented? ,America  +General,Oriental market,Bazaar,General,What did Edwin Land invent in the 1940's,Polaroid camera,General,What are scallops,Shellfish +General,"If the groundhog sees his shadow on Feb. 2, there will be how many more weeks of bad weather?",Six,General,In what film did john wayne get stranded in labrador,Islands in the sky,General,What is a Boodie,A Marsupial related kangaroo rat +Food & Drink,Salted and glazed biscuit shaped like a knot ,Pretzel ,General,Which country rejected membership of the E.E.C. in 1972,Norway,General,Process of heat treatment by what glass & certain metals & alloys are rendered less brittle & more resistant to fracture,Annealing +General,What's ibm's motto,Think,Science & Nature,What Is The Common Name For A Baby Kangeroo? ,Joey ,Music,What event Occurred At Finsbury Park On The 8th And 9th Of August 1992,Madstock / Madness Reunion Concert +General,In which ocean is mauritius,Indian ocean,General,The term Semicentennial represents how many years ?,50,General,What is the only european country where monkeys live free,Gibraltar +General,What food item in French literally means twice cooked,Biscuit,General,"Who recorded the album ""diver down"" in 1980",Van halen,General,What are Puli Sloghi and Kuvaszok,Exotic dog breeds +General,Heliophobia is a fear of ______,Sun,People & Places,Which Labour MP Attempted To Fake His Own Disappearance ,John Stonehouse ,Music,Which Famous 1960's Band Was Jane Ashers Brother In,Peter & Gordon +General,Do the pupils in a person's eyes get larger or smaller in bright light,Smaller,General,What is the more common name for the Buddleia,Butterfly Bush,General,What chilean president was killed in a 1973 coup d'etat,Salvador allende +General,"How is ""Rodrigo Diaz de Vivar"" better known",El cid,General,What kind of insect is a 'whirligig',Beetle,General,The potato arrived in Spain in which year,1565 +General,Which country is indicated by the car identification letters RA,Argentina,General,What musical term means 'slowely and stately',Largo,Science & Nature,"To what group of elements do cerium, praesiodymium and promethium belong?",Rare earth metals +Science & Nature,Why Did Henry Ford Say People Can Have A Model T Ford In Any Colour Just So Long As It Was Black ,Japan Black Enamel Was The Only Paint That Would Dry Quick Enough to Keep Up With The Assembly Line , History & Holidays,Who is considered the father of medicine?,Hippocrates, History & Holidays,Which singing family was David Cassidy a member of ,The Partridge Family  +General,Lansing is the capital of ______,Michigan,General,If you were given some marlite what would you do with it,Dig into soil it’s a clay lime mulch,Science & Nature,Who invented the telescope?,Galileo Galilei +Music,What Did Slipstream Tell Us They Were Doing In 1992,We Are Raving - The Anthem,General,What is the birth flower for January,Carnation,Science & Nature,Which Is The Only Bird That Casn Fly Backwards? ,The Hummingbird  +General,In the US what links Fort McHenry with the moon,Flag flies 24/7 president decree, Geography,What is the basic unit of currency for Dominican Republic ?,Peso,Technology & Video Games,"The Internet Relay Chat Program, which normally connects to port 6667, is more commonly known as ___.",IRC +General,Which author wrote about the fictional Napoleonic war hero Sharpe,Bernard cornwell,General,Evidence of the first recorded brothel was found in which city,Athens Greece, Entertainment,What is know as 'The Greatest Show On Earth'?,Barnum & Bailey Circus +Mathematics & Geometry,A triangle with three equal sides is called _______.,Equilateral,General,"In a uniform gravitational field, the center of gravity is also a______",Centre of mass,General,What does bmx stand for,Bicycle motocross +General,Freya was the norse goddess of ______,Love and fertility,General,What is someone who collects banknotes called?,Notaphile,General,What is the perforated tag with advertisements that are put in mailer envelopes,Bangtail +General,Second city: St. Croix (U.S. Virgin Islands),St. thomas, History & Holidays,"Played subsequently by Sarah Michelle Geller on tv, which character was played in a movie by Kirsty Swanson ",Buffy theVampire Slayer ,General,"The Olympic motto 'citius, altius, fortius' means what?","Faster, higher, stronger" +General,Who invented popcorn,American Indians,Music,"Who Had Hits With ""Don't Stop Believing"", ""Faithfully"" & ""Open Arms""",Journey,General,Who played harvey weskit on mr peepers,Tony randall +General,"What song does Rodney Dangerfield sing in ""Back To School""",Twist and,People & Places,Who Was French President From 1969 Until 1974 ,Georges Pompidou ,General,The Finnish know her as Tuna what do we call her,Cinderella +Entertainment,In the Gene Pitney how many hours was it from Tulsa?,24,General,What was the name of the Rat who was seen as a master of the Teenage Mutant Turtles and an arch enemy of Shredder?,Spilnter,General,What was the forerunner of croquet,Pall mall +General,Which course was the first on the European mainland to host the Ryder Cup?,Valderama,General,"On Little House on the Prairie,what was Laura's horse's name?",Bunny,General,What is a group of this animal called: Jellyfish,Smack +General,What was the first cartoon character called,Oswald the rabbit,General,The word Angel derives from the Greek meaning what,Messenger,Sports & Leisure,"How Many Times Did Nick Faldo Win The Masters Golf Tournament? Is It (3,5,7) ","3 (1989, 90 , 96) " +People & Places,Who Was Assassinated By Two Sikh Bodyguards ,Indiri Gandhi ,General,"Are periwinkles animal, vegetable or mineral",Vegetable,General,Nut - Neuth - Nuit alterative names Egyptian goddess of what,Sky + History & Holidays,Name the loner rebel reindeer with the red shiny nose.,Rudolph,General,What movie were the passengers on the plane in Executive Desicion watching?,Born to be Wild,General,What country singer's duets with loretta lynn ended when he died at age 59,Conway twitty +Sports & Leisure,Football: The Baltimore ________.,Colts,Music,Which 1969 Hit Was The Biggest Foreign Language Single Of The Decade In Britain,Je T'Aime / Jane Birkin & Serge Gainsborough,General,"In which musical was the song ""The Girl that I Marry""",Annie get your gun +General,Time during which a machine esp. computer is out of action or unavailable for use,Down time, History & Holidays,Who was King Arthur's foster-father?,Ector,General,The rivers Lahn and Mosel are tributaries of what river,The Rhine +General,What Georgia town did the first Dukes of Hazzard episodes take place?,"Covington,Georgia",Religion & Mythology,Who was the Greek goddess of spring?,Persephone,General,A poster depicting Lord Kitchener pointing outwards stated what message,Your country needs you +General,In which play does dame pliant appear,Alchemist,General,What album by george michael won the grammy in 1988,Faith,General,Where was Harry Houdini born,Budapest Hungary +General,Which peoples name translates as eaters of raw flesh,Eskimo,Music,Who Sang With The Attractions,Elvis Costello,General,"Although it doesnt sound like a dog, ____dust is ornamental wood chips often placed in flowerbeds",Bark +Science & Nature,This is the symbol for tin.,Sn,General,In what sport is the arena 8 metres square,Karate,General,Which biologist has written the books The Selfish Gene and Climbing Mount Improbable,Richard dawkins +General,What was betty grable's nickname,The legs,Religion & Mythology,Where was Methodism founded ?,Oxford University,General,Who was Stan Laurels partner,Oliver Hardy +General,What name is given to any muscle with three heads,Triceps,General,What river did John baptize Christ in,Jordan,Religion & Mythology,"In Greek mythology, where did Perseus kill his grandfather?",Larrisan games +General,Who was the 32nd president of the U.S.,Franklin d roosevelt,Science & Nature, A cat has 32 muscles in __________,Each ear,General,Which battle is sometimes called the Battle of Three Emperors,Austerlitz +General,Who invented popsicles,Frank epperson,General,"What are brick, fontina, port salut, quargel types of",Cheese, Language,"The word rodent comes from the italian 'rodere', which means?",Gnaw +General,Which nation has an AK-47 assault rifle on its flag?,Mozambique,General,What did george harrison discover on the witwatersrand,Gold,General,"What is the Christian (Latin) name for the place which the Hebrews called Golgotha, (In Aramaic, 'The Place of the Skulls)",Calvary +Geography,"The _____________ got its name from the occasionally extensive blooms of algae that, upon dying, turn the sea's normally intense blue_green waters to red.",Red sea,Sports & Leisure,What was the first London club that David Seaman played for? ,Queens park rangers ,General,What name is given to music that constantly syncopates a straightforward tune,Ragtime +General,What is the name of the cartilage flap at the trachea which prevents food going down the wrong way,Epiglottis,General,Toxiphobia is a fear of _________.,Poison,General,What does an ombrometer measure,Rainfall +Entertainment,What is tattooed on Glen Campbell's arm?,Dagger, History & Holidays,Which country was split into two zones by the Yalta agreement?,Germany,General,"On which badge are Minerva, Clipper ship, Grizzly bear, Eureka",California Highway Patrol - Chips +Sports & Leisure,Where were the 1960 Olympics held ?,"Rome, Italy",General,Clemintina Campbell famous as who,Cleo Lane,Music,The Drum Break From Funky Drummer Is One Of The Most Used Samples Of All Time Who Recorded The Original Track,James Brown + Geography,What is the highest mountain in the world?,Mount Everest,Sports & Leisure,On which cricket ground was the first test match in England played? ,The Oval ,General,"Which Hollywood Actor Not Really Known For His Action Roles Baffled Movie Producers In 1987 When He Turned Down The Role In What Went On To Become The ""Die Hard"" Series Of Movies",Richard Gere +General,Who was born Mark Feld,Marc Bolan,General,Where would you find a planchette,Oiuja board indicator,General,Which actress played The Sculptress on TV?,Pauline Quirke + History & Holidays,In The Inspirational 1946 Film 'Its A Wonderful Life' What Is The Name Of George Baileys Guardian Angel ,Clarence Oddbody ,General,Where could you spend a Rufiyya - Capital Male,Maldives,Entertainment,What group's biggest-ever hit was Be My Baby?,The Ronettes +General,East berlin was the capital of ______,East germany,Science & Nature,What Advance In Sound Recording Was Made By British Engineer Alan Blumlein In 1933 ,Stereophonic Sound ,General,"Better known by her maiden name, who was the 1930s aviation pioneer Mrs. Mollison",Amy johnson +General,The second tallest mountain on earth,K2,General,What number jersey was synonymous with Ice Hockey legend Wayne Gtrezky?,99,Science & Nature,What first appeared beside the roads outside the houses of parliament in 1868 ,Traffic lights  +General,What vegetable was Emperor Nero's favourite,The Leek,General,Which country set up the world’s first chemistry lab in 1650,Netherlands,General,In USA by law only 2 paid services limited to one sex - what,Sperm Doner Wet Nurse +Art & Literature,"In painting, the degree of lightness or darkness in a color.",Values,General,"On whose life is the film 'The Music Lovers', based",Tchaikovsky,General,Who was the star of the post appocolyptic sci-fi film A boy and his Dog?,Don Johnson +General,What is the middle name of author Arthur C. Clarke,Charles,General,Peggy is a diminutive for which girls name,Margaret,General,The toronto maple leafs used to be originally called what,The toronto arenas +Tech & Video Games,What is Mario's profession? ,Plumber,Music,Tom Jones What Prince Song In 1989,Kiss,General,Domenikos Theotocopoulos born Crete - died Spain - who,El Greco +General,There are 20 days in the week in whose calendar,Aztec,General,What is the flower that stands for: pleasures of memory,White periwinkle,Sports & Leisure,What Is The Down Wind Sail On A Yacht Called ,The Spinnaker  +General,Philosopher Jeremy Bentham has a very unusual pet - what,Tea Pot,General,The first what was installed in Antarctica in 1997,ATM cash point machine,Music,"Who Recorded Ther Album ""Diva""",Annie Lennox +Sports & Leisure,Which county cricket team play their home games at Grace Road? ,Leicestershire ,General,"Who wrote ""Damn Yankees"" in 1955",George abbott,General,What word comes from Arabic means reunion of broken parts,Algebra +Music,"Who Sang About ""Skipping The Light Fandango"" & Turning Cartwheels",Procol Harem,General,Who discovered the tomb of Tutenkhamen,Howard carter,General,"Apart from Police, what other word is prominent on Welsh Policemen's uniforms?",Heddlu (Welsh for Police) +General,Keats Who was the first astronaut to return to space,Gus grissom, History & Holidays,Britain and Argentina fought over these islands in 1982.,Falklands Islands,General,"In Beatrix Potter's Tale of Tom Kitten, what was the name of his mother",Tabitha twitchett +Art & Literature,"Who is the author of ""Harry Potter"" ?",Joan Rowling,Religion & Mythology,The Lord's Prayer appears in the Bible how many times (written numerically)?,Two,General,Which was the first magazine to publish a hologram on its cover,National +General,Arachnoid refers to what kind of insect,Spider,Music,Who Had the best selling Album Entitled Diva,Annie Lennox,Music,What Was The Full Name Of The Artist Formely Known As Prince,Prince Rogers Nelson +Food & Drink,Who had a number 1 record in 1963 with Sweets For My Sweet? ,The Searchers , History & Holidays,In 1715 Louis XIV Of France Was Succeeded By Louuis XV What Relation Was He ,Great Grandson ,General,"In Greek mythology, which king made a statue of a woman which Aphrodite brought to life",Pygmalion +General,Name the pain-inflicting person you go to to get your teeth fixed.,Dentist,Food & Drink,"Which South-East Asian fruit's smell reminds people, to put it mildly, of various stages of decay ? ",Durian ,General,Portrait of a Man is the real title of which artistic work,The Laughing Cavalier Franz Halls +Entertainment,Fat Albert and friends was created by ______ ?,Bill Cosby,General,What is the central part of a backgammon board called,The Bar,General,In 1945 Marines land on,Iwo jima +General,"""Night of he Hunter' was the only film directed by which actor",Charles laughton,Music,Which 1995 Album Cover Features A Wedding Photo With One Of The Band Members Shown In Black & White,Pulp / a Different Class,General,What sort of sexual practice is Lectamia,Caressing in bed no coitus +General,"What Cheers actor was in ""The Empire Strikes Back?""",John Ratzenberg,General,In Ancient Mesopotamia people worshiped what,Pigeons,General,Apparition or double of living person,Doppelganger +General,"In the Ian Fleming novel ""Goldfinger"", what was Goldfinger's first name",Auric,General,What was Professor Moriarties first name,James, History & Holidays,What is the first ghost seen by Ebenezer Scrooge in 'A Christmas Carol'' ,His ex-partner Jacob Marley  +General,On the PH scale what does PH stand for,Potential Hydrogen,General,Somniphobia is the fear of,Sleep,General,What's the world's largest cat,Tiger +General,"Which Company Advertised With The Slogan ""Someday All Watches Will Be Made This Way""",Seiko,General,"Who recorded ""love is all around"" in 1968",Troggs,General,Rice what was lestat's mother's name,Gabrielle +General,Who played The Fugitive,David Jason,General,"What job links Paul Clifford, Claude Duval, Capt. Macheath",Highwayman,General,What is the storage polymer of plants,Starch +General,What was in Catherine's crucifix in the movie Cruel Intentions?,Cocaine,General,"Which film director coined the term ""Paparazzi"" to describe intrusive photographers",Fellini,General,Grammy Awards: What single by Tina Turner won the grammy in 1984,What's love +General,To who did the lady of the lake give excalibur,Sir lancelot,General,How many buttons does a double breasted suit have?,Six,General,Condition in a circuit in which the combined impedances of the capacity and induction to alternating currents cancel each other out or reinforce each other,Resonance +General,Which film was grace kelly making when she met prince ranier,To catch a,General,Only one miracle is mentioned in all four gospels what is it,Feeding of 5000,Food & Drink,What does iron deficiency cause?,Anaemia +People & Places,"Who Said 'I Hope Harry Secombe Goes Before Me, I Don't Want Him Singing At My Funeral'? ",Spike Milligan ,General,The orchestra usually tunes up to what instrument,Oboe,General,What Was U2's Very First UK No.1,Desire +General,In which 1989 film did Kevin Costner play Ray Kinsella?,Field Of Dreams,General,Allergic Rhinitis has what more common term,Hay Fever,General,"If Brazil had won the 1998 tournament, how many times would they have won the soccer World Cup?",Five +General,Which mountains are between the Caspian Sea and the Black Sea,Caucasus,General,"What film links Cher, Michelle Pfeiffer and Susan Sarandon",The witches of eastwick,Art & Literature,Who Painted The Blue Boy ,Gainsborough  +General,Bantu-speaking people of southern Africa,Zulu,General,Ii what is the round fruit of the sycamore tree called,A buttonball,General,Gjetost is the national cheese of what country,Norway + Geography,In which city is the famous Bond Street?,London,General,A castrated bull,Bullock,Music,"""George O Dowd, Roy Hay, Mikey Craig, Jon Moss"" Were All Members Of Which 80's Band",Culture Club +General,Where are Bay of Heats and Bay of Dew Sinus Aestuum - Roris,Near side of Moon,Geography,Which Group Of Islands Are Known As The Friendly Islands ,The Tonga Islands ,Entertainment,What was The Beatles' biggest hit single?,Hey Jude +General,Square of cloth used to wipe nose,Handkerchief,General,To which Berkshire destination did C.N.D. march from London each Easter beginning in the late 1950s,Aldermaston,General,"Who said 'Everything must either be or not be, whether in the present or in the future' ?",Aristotle +Geography,Which Famous City Bridge Is Known Locally As The Coathanger ,Sydney Harbour Bridge ,General,What is a Characin,Small Fish,General,On which continent are the Iguaca waterfalls,South america +General,A Stag with 12 point antlers is known as a what,Royal, History & Holidays,Who Was Nick-Named (The Desert Fox)? ,Field Marshal Rommel , History & Holidays,Who Commanded The Confederate Armies During The American Civil War? ,General Robert E.Lee  +Science & Nature,Which Bone Is The Patella ,The Knee Cap ,Music,"Who Recorded The Album ""Eat Em And Smile""",David Lee Roth,Music,Who Said Go West In 1993,The Pet Shop Boys +General,You can have a troop of actors and what group of animals,Monkeys,Food & Drink,What delicatessen dish is highly seasoned beef prepared from a shoulder cut called? ,Pastrami ,General,What is the literal Greek translation of Sarcophagus,Flesh Eater +Science & Nature, The largest jellyfish in the world has a bell that can reach 8 feet across and tentacles that extend over half the length of a __________,Football field,General,What is the oldest known cultivated vegetable,The Pea,General,24-karat gold has to have a small amount of _____ in it to keep it from being too soft,Copper +General,Vinnie Jones is associated with which sport,Football,General,What beautiful hotel commands a matchless view of quebec city,Chateau,General,"The Slave of Duty', is the alternate title for which Gilbert and Sullivan operetta",Pirates of penzance +Music,Name The UK Label Associated With A Dog Named Nipper,HMV,General,What is the Capital of: Uzbekistan,Tashkent toshkent,General,What is a group of this animal called: Heron,Hedge +Music,Who Preceded Phil Collins As The Lead Singer Of Genesis?,Peter Gabriel,General,What type of large vehicle is named after a Hindu God,Juggernaut,Geography,Which is the Earth's fifth largest continent,Antarctica +General,Creator of Perry Mason,Erle stanley gardner,General,Which jockey rode a Derby winner called Pinza,Gordon richards,General,Formal or informal agreement among business firms designed to reduce or suppress competition in a particular market.,Cartel +General,What's the word for the front of a dogs chest and joint of beef,Brisket,General,Who appeared as the infant Moses 1956 film 10 commandments,Frazer Heston – Charlton's son,General,Which two male fish give birth,Sea horse and pipe fish +General,Which novelist created Crown Prosecutor Helen West,Frances fyfield,General,What is a group of this animal called: Walrus,Pod,Music,"Which Group Had A Hit LP With ""Babylon By Bus""",Bob Marley & The Wailers +General,Who directed the 1989 film The War of the Roses,Danny devito,General,What job does an Oikologiost do,Housekeeper,General,Collective nouns - A Husk of what,Jackrabbits +General,"Who did a version of 'one bourbon, one scotch, one beer' on his 1977 debut album",George thorogood,General,Pair of briefs consisting of small panels connected with strings,Tanga,General,Whose headstone reads 'she did it the hard way',Bette davis +Sports & Leisure,Name the hockey trophy awarded to the player demonstrating the best sportsmanship.,The lady byng trophy,General,What's the name of the holy book of Islan,The koran,General,Name Santa Clauses (St Nicholas) French brother,Bells Nichols +General,Who wanted 'a lover with a slow hand',Pointer sisters,General,King Henry VIII trained as what ,A Priest,General,Game show: which of the 60 minutes men hosted several game shows in the early part of his career,Mike wallace +Food & Drink,Who is the American artist who uses Campbell's Soup cans in his pop art? ,Andy Warhol ,Music,Which Musical Opens At Uncle Jocko's Kiddie Show In Seattle,Gypsy, History & Holidays,Who invented the exploding shell?,Henry Shrapnel +Sports & Leisure,Which English Football Team Has The Longest One Word Name? ,Middlesbrough ,General,What was originally called olive oil water,Vaseline,General,"Who recorded ""king of the road"" in 1965",Roger miller +Music,Which US superstar has had over 70 chart albums in his career?,Frank Sinatra,General,What is the British equivalent of the US Navy rank Rear Admiral (lower half) ?,Commodore,General,What is the top speed in mph that cheetahs have been clocked at,71 +General,Wings of Desire a foreign film remade as what with Nicolas Cage,City of Angels,General,What nation on average takes most time to eat meals,French,General,Who was the American President from 1869 to 1877,Ulysses s grant +Science & Nature,What is the meaning of the name of the constellation Ursa Major ?,Great Bear,General,Frederick Gowland Hopkins won a Nobel prize in 1929 for which medical discovery,Vitamins,General,"If you drive on a parkway, you park on a _______",Drive +Science & Nature,The spiral galaxy nearest ours is the ________.,Andromeda,General,Which country left the Commonwealth in 1987 and has not rejoined,Fiji, Geography,What is the capital of Norway ?,Oslo +Entertainment,Who sings and plays the theme song for the TV show 'Frasier'?,Kelsey Grammer,General,Bacteriophobia is a fear of ______,Bacteria,General,Workshop for casting metal,Foundry +General,What is the name of the evil spirit in Polterguise?,Kane,General,What was the most prescribed drug in the u.k in 1985,Valium,General,In Wyoming it is illegal to wear what in a theatre,A Hat if others can’t see over it +General,Fourth letter of the Greek alphabet,Delta,General,What is the toothpick capital of the world,Maine,General,U.S. capitals Alaska,Juneau +General,"Justin Hayward-Young, great grandson of English painter and postcard artist Walter Hayward-Young, is the lead singer of which band?",The Vaccines,Entertainment,Who sang 'We've only just begun'?,Carpenters,General,Someone who is androphobic has a fear of what?,Men +General,What is the Capital of: Northern Mariana Islands,Saipan,Science & Nature," The once popular dog name __________ is from Latin and means ""fidelity.""",Fido,General,What country is nearest to the North Pole,Greenland +General,Of what are quemoy and matsu part,Taiwan,Sports & Leisure,Who Were Man Utd Playing When Eric Cantona Performed His Famous Kung Fu Kick On A Fan ,Crystal Palace ,Music,"Who Had A Hit In 1988 With ""Don't Go""",Hot House Flowers +General,What popular party drink gets its name from Sanskrit meaning 5,Punch - originally 5 ingredients,General,What do New Zealanders claim Jack Lovelock did in 1935,Run a four minute mile,General,What was Christopher Deans job before Ice Skating,Policeman +Food & Drink,From Which Country Does The Drink Tequila Originate? ,Mexico ,General,"In which tv series are joey potter, pacey witter, dawson leary and jennifer lindley",Dawson's creek,General,Which Sidney Pollack film won the Oscar for best film in 1985,Out of africa +Science & Nature, The underwater mating song of the __________ is so loud that sometimes it can be heard by humans on the shore.,Toadfish,General,What Was Hung Upside Down In The Piazzale Loreto In Milan In 1945,Mussolini's Body,General,Which English writer was named after a Staffordshire lake,Rudyard kipling +General,What's Princess Aurora's better-known name,Sleeping beauty,General,"Which plant has gills, a veil and scales",Mushroom,General,"Xanthic, Fallow and Aureate shades of which colour",Yellow +Geography,In which country is the machu picchu ,Peru ,General,A pair of small drums played with the fingers,Bongo, History & Holidays,Which river did George Washington cross on Christmas night in 1776 in the American Revolutionary War? ,Delaware  +Music,Give The Title Of The 1973 Mott The Hoople Single Featuring A Tennessee City,All The Way From Memphis,General,Faith Hope Charity Fortitude Justice Prudence what's missing,Temperance,General,Which is the most famous engine to run on the Merioneth line,Thomas the tank +Entertainment,Who played Matt Helm in the movies?,Dean Martin,General,In the Hindu pantheon Hanuman is the King of which creatures,Monkeys, Geography,Name the highest mountain in Africa.,Mt. Kilimanjaro + History & Holidays,Who shot Abraham Lincoln?,John Wilkes Booth,General,Heliotaxis means the response of an organism to what,Sunlight,Science & Nature,How many planets are there in our solar system?,Nine +General,The number unemployed consists of all those people in a country who are willing and able to work but are unable to ___ ? ,Find jobs,General,What famous artist could write with both his left and right hand at the same time,Leonardo da vinci,General,Ralph Wonderone became better known under what name,Minnesota Fats +General,What liqueur is prepared from cumin and caraway seeds,Kummel,Science & Nature,Who Took The First Practical Photograph ,Louis Daguerre In 1826 ,General,Musophobia is a fear of what,Mice +General,What is the actual vat in Romania,19%, History & Holidays,How Many Children Did Queen Victoria Have ,Nine ,Science & Nature,The molten material from a volcano is ________,Lava +General,In Star Trek Generation who was the chief of security killed off,Tasha Yar,General,"In the song American Pie, who did the word 'jester' refer to?",Bob Dylan,General,The bering strait lies between alaska and ______,Russia + Geography,What is the basic unit of currency for Vatican City ?,Lira,General,What does Abraham Lincoln never do in any photographs,Smile, History & Holidays,"Name the remake of a 1960's film with the aid of the following actors. The first actor was in the 60's original, the second actor played the same role in the remake James Mason and Jeremy Irons ",Lolita  +General,A plant allied to the thistle with a partly edible flower,Artichoke,General,Lack of Vitamin D results in ___. ?,Rickets,General,The first modern Olympiad was held where,Athens +General,What is the Capital of: Tunisia,Tunis,General,"Which Cecil B. De Mille classic carried the publicity tag, 'The mightiest dramatic spectacle of all the ages'",The ten commandments, History & Holidays,What Was The 9th Century Tax Levied To Fight The Vikings? ,Dane Geld  +General,Firm Music: What was the first album Roger Waters released after leaving Pink Floyd,The Pros and Cons of Hitch Hiking,General,One quarter of people who lose sense of small also lose what,Desire for sex,General,"Who had top ten hits in the 1980s with Run to the Hills, Can I Play With Madness and The Evil That Men Do",Iron maiden +General,In which game are there hashmarks on each five-yard line,Football,General,Ball point pen ink is made from dye and what,Castor Oil,General,What game has only 7 possible opening moves,Draughts - Checkers +General,"The puma, cougar, lynx & other wildcats are all",Catamounts,Music,What Kind Of People Did REM Take To No.6 In 1991,Shiny Happy People, Geography,What is the capital of Mongolia ?,Ulaanbaatar +General,In WW2 what was unique about the US 222 Infantry battalion,All Japanese or Hawaiian immigrants,General,The study of insects is __________.,Entomology,Music,"Name The Year John Lennon Was Shot, Crying By Don Mclean Was Released & So Was The Movie Blues Bros",1980 +General,"What sport does ""FISA"" govern",Auto racing,General,Who discovered radium,The Curies,Food & Drink,In which country did the word 'biscuit' originate?,France +General,Where did Jim Morrison die,Bath - in Paris hotel,General,Genoa overlooks which sea,Ligurian,Science & Nature,What Is The Largest Carnivore Native To The UK? ,The Badger  +General,Who was john merrick,Elephant man,General,English festival word from French literally Farewell to Flesh,Carnival,Music,"""There But For Fortune"" Was A 60's Classic For Which Female Soloist",Joan Baez +General,"When a U.S. army bomber crashed into the New York's Empire State Building, how many people did it kill",Fourteen,General,A small atmospheric vortex that comes from surface heating is known as what,Dust devil,Food & Drink,What Was The First Nationally Distributed Beer In The USA ,Budweiser  +Religion & Mythology,Which tree do Druids regard sacred ?,Oak,General,"In 1936 King Edward VIII abdicated from the British throne to marry an American divorcee, who was she",Mrs. Wallis Simpson,General,In Macbeth what witch speaks first,The first witch +General, What does a heliologist study,The sun,General,Islands what is ice cube's real name,O'shea jackson,Science & Nature,Which Steam Locomotive Won The Prize For Providing The First Regular Passenger Service ,The Rocket  +General,Nephologists study what,Clouds - Meteorology,General,Who said - Remember time is money,Benjamin Franklin,Science & Nature,Who discovered X-rays?,Wilhelm Roentgen +General,What year did Chernobyl explode,1986,General,"In the film 'pulp fiction', what was the name of uma thurman's pilot character",Fox force five,General,"""Albuquerque"" International Airport Is In Which US State",New Mexico +General,Which breed of dog gets its name from the French for earth,Terrier,General,Osiris was the egyptian god of ______,Underworld and vegetation,General,What would you expect to see at Santa Pod,Drag Racing +Science & Nature,In Computing of what is the term 'bit'' an abbreviation? ,Binary Digit ,General,Pinatubo The application of science to law,Forensic science,General,What forms when a diamond is cut with a laser,Graphite dust +General,"Which Victorian explorer and translator was best known for his translations of the ""Kama Sutra"" and ""Arabian Nights""",Richard burton,Sports & Leisure,How Many Draughts Are On The Board At The Start Of A Game ,24 (12 White 12 Black) ,General,Whose business was ran from 2222 South Wabash,Al Capone +General,Who Wrote The 1972 Mott The Hoople Hit All The Young Dudes?,David Bowie,Music,What group is Phil Lesh a member of?,The Grateful Dead,General,What is the flower that stands for: deceitful chams,Thorn-apple + History & Holidays,"In 1785, Blanchard and Jeffries became the first to cross the English channel using which method of transport? ",Balloon , Geography,Which city has the highest population ?,Mexico City, Geography,What is the largest country in Central America?,Nicaragua +General,Fidelity Bravery Integrity is which organisations motto,FBI,General,Bowling for lizards was whose favourite TV program,Fred Flintstone,General,"Life what major does david bowie's ""space oddity"" refer to",Major tom +General,How many pairs of ribs are there in a male skeleton,12,General,Where did forrest gump keep the book 'curious george',In his suitcase,General,When did the Fuller Brush Company begin using catalogs,1986 +Music,Who Had A Number One Hit In 1962 With The Song Telstar?,The Tornados,Sports & Leisure,This football team was formerly known as the Frankford Yellow Jackets?,Philadelphia Eagles,General,The name Malissa means what,Bee +Music,"Who Had A Surprise Hit With ""Atmosphere"" In 1984",Russ Abbot,General,Two most commonly sold items in stores are sodas and what,Breakfast Cereals,General,"Shakespeare character says ""Blow winds and crack your cheeks""",King Lear +General,In Kiplings How the Leopard got its Spots name the Leopard,Best Beloved, History & Holidays,"In Harry Potter And The Goblet of Fire, What ingredient was needed to resurrect the dark lord ",Blood of an enemy ,General,Where is terre haute,Indiana +General,"Which novel, when broadcast in America, was believed by many people to be a real news report, and it caused widespread panic",The war of the worlds,Music,Which Motown Singer/Songwriter Was Once Described By Bob Dylan As America's Greatest Living Poet,Smokey Robinson,General,On what is an 'octothorpe' found,Telephone +Science & Nature,When does a full moon rise?,Sunset,General,Where was Bobby Kennedy shot,California,General,"Which band had members Robert palmer, Andy and John Taylor, and Tony Thompson?",The Power Station +General,"Which book is subtitled ""the Mistakes of a Night""",She stoops to conquer,General,Dick Tracy the comic strip started life as what name,Plainclothes Tracy,General,ET drank which brand of beer,Coors +General,Name the legless fighter pilot of ww2,Douglas Bader,General,What classic novel sold only 50 copies authors lifetime,Moby Dick,General,What is the Capital of: Moldova,Chisinau +General,What U.S. senator gives out the golden fleece awards,William proxmire,Food & Drink,Which vegetable is a Welsh emblem? ,Leek ,General,St Stevens Tower is usually misnamed what,Big Ben +Geography,Which U.S. state receives the most rainfall,Hawaii,General,What does an ornithologist study?,Birds,Science & Nature,What is another name for consumption?,Tuberculosis + Geography,What is the capital of Guatemala?,Guatemala,General,How fast did the bus in the movie Speed need to go in order not to blow up?,Below 50mph,General,"Who is famously buried in the churchyard at Bamburgh, Northumberland",Grace darling +General,As what is a giraffe also known,Camelopard,General,"""As free as the wind blows, as free as the grass grows'. What is the song title",Born Free,General,When I Was Lying There In The VA Hospital With A Big Hole Blown Through The Middle Of My Life Are The Opening Lines To Which Movie,Avatar +General,Steven Georgi is now Yussef Islam what other name had he,Cat Stevens, History & Holidays,Which Annual Event First Hit Our Screens On The 5 th February 1988 ,Red Nose Day ,General,"What Olympic event's winner is considered to be the ""world's greatest athlete""",The decathlon's the decathlons decathlon's decathlons decathlon +General,After his death what bit Walter Raleigh did his wife carry around,His Head,General,What is the largest lake in australia,Eyre,General,What Was The Former Name Of The Sellafield Nuclear Power Plant?,Windscale +General,How tall was the world's shortest man,670 mm,Science & Nature, More species of __________ live in a single tributary of the Amazon River than in all the rivers in North America combined.,Fish, History & Holidays,The Character Jack Skellington Appears In Which 1993 Tim Burton Film? ,The Nightmare Before Christmas  +General,Goodfellow's Lumholtz's and Bennett's type of what animal,Tree Kangaroo,General,According to its name what major Italian city is the new city,Naples - short for Neopolis,General,Kellogg's Corn Flakes were invented to do what,Reduce Masturbation +General,Which is the world's second largest monolith,Ayers rock,General,Which unfinished castle is found on the island of Anglesey?,Beaumaris,General,"In 1898 , the Bayer company began marketing what they claimed was a non addictive opiate what was it",Heroin +General,In food labelling what does GM mean,Genetically Modified,General,What is the tartan skirt worn by Scottish men called,Kilt,General,Which magazine is most often stolen from US libraries,Sports Illustrated +Geography,Acadia was the original name of which Canadian province,Nova scotia,General,What 1968-71 tv series did ken berry star in,Mayberry rfd,Food & Drink,What Is The Earliest Known Cereal To Be Cultivated ,Barley  +General,I see your schwartz is almost as big as mine.,Spaceballs,Science & Nature,What Type Of Glass Darkens When Exposed To Light ,Photochromic ,General,"For women, what is the least favourite part of the male body",Feet + Geography,"The ""Old City"" of this holy location is divided into four quarters — a Christian quarter, a Muslim Quarter, a Jewish Quarter, and an Armenian Quarter.",Jerusalem,General,In which war did Ulysses Grant & Robert Lee fight on the same side,Mexican,Entertainment,"Mentor of Titan had two children in the Marvel comics, Thanos and ____",Ero +General,Where did abraham lincoln gave his historic speech,"Gettysburg, pennsylvania",General,What animals did hannibal lead over the alps for the first time,Elephants,General,"Who said, 'Whenever meditating over a disease, I never think of finding a remedy for it, but instead a means of preventing it",Louis pasteur +General,What is the flower that stands for: evanescent pleasure,Poppy,General,"In ""10,000 Leagues Under The Sea"", what ship did Captain Nemo travel in",Nautilus,General,What is unusual about the crab eating seal,It don't eat crabs +General,"Countries of the world: east-central Europe, the capital is Kiev",Ukraine,General,Who is the Patron Saint of hunters,St Hubert,Geography,On what island is Pearl Harbour,Oahu +General,Ncaa: what team won the men's basketball championship game in 1976,Indiana,General,A Lady Paramount judges at what sport,Archery,General,"Chekhov Quotations: ""Doctors can bury their mistakes, Architects can only advise their clients to plant vines.""",Frank Lloyd Wright +General,What was the name of Hannibal's father,Hamilcar barca,General,Who always ended his show in a white e-type jaguar,Simon dee, History & Holidays,"U.S. President, Chester Alan ________.",Arthur +General,What trio were originally called The Rattlesnakes,The Bee Gees,General,Where would you find a howdah,Back of Elephant (basket),General,In Which Asian Country Is Divorce Illegal ?,The Phillipines +General,"Who is known as the ""Father of History"" ?",Herodotus,General,"While creating the movie ______________(1995), the animation team at Pixar Animation Studios perfected the movement of the toy soldiers by gluing some sneakers to a sheet of wood and trying to walk around with them on. ",Toy story,Food & Drink,Which German spirit is flavoured with caraway seeds and distilled from potatoes? ,Schnapps  +Sports & Leisure,In which decade was cricket's first World Cup Final played? ,1970's ,General,Which u.s state gets the most rainfall,Hawaii,Entertainment,Which Australian duo took 13 nominations and 10 wins at the ARIA awards?,Savage Garden +General,"The cardinal is the state bird of 5, 7 or 9 u.s states",Seven,General,Who many gallons of fuel does a jumbo jet use during take off,Four thousand,General,In Tejo a S American game players throw stones at buried what,Gunpowder - try to explode it + History & Holidays,Who led 900 followers in a mass suicide in 1979?,Jim Jones,General,Contralto and Soprano are female voices what comes between,Mezzo - soprano,General,Who was the last living person on a US postal stamp,Nobody it's illegal +General,What religious movement was founded by william booth,Salvation army,General,The French call it creame anglaise what do we call it,Custard,Music,"A Huge Hit For Whitney Houston, Who Wrote The Song ""I Will Always Love You""",Dolly Parton +General,What is more effective than caffeine for waking up in the morning,Apples,Entertainment,Tippi Hedren is best known for her lead role in which film?,The Birds,General,Who might make use of a maulstick?,Artist +Music,Which Song Was A Hit For Both Cliff Richard And Elvis Presley,The Twelth Of Never,General,"What is a moist, fertile spot inside a desert called",Oasis,Food & Drink,If wine is made from grapes what is Tequila made from ,Cactus  +Sports & Leisure,"In Which Sport Would You Encounter a Bed Post, A Six Pack, A Blow And A Cherry ",Ten Pin Bowling ,General,What is the fear of being severely punished or beaten by a rod known as,Rhabdophobia,Science & Nature,Who was the first man to return to space ,Virgil grissom  +General,What is a young Irish girl called,Colleen,General,Who was Prince Charles' mistress while he was married,Camilla parker bowles,General,Why was Clark Kent rejected military service during WW2,Failed eye test +General,"The world's rarest coffee, Kopi Luwak, comes from which country",Indonesia,Science & Nature,What Is The Value Of Sine 30 Degrees ,0.5 ,General,Temperature at which a liquid congeals into the solid state at a given pressure,Freezing point + History & Holidays,Which (Age) Occured Between The Stone And The Iron Ages? ,Bronze Age ,General,Which 'Eastenders' character was found guilty of the murder of Saskia,Matthew rose,General,"Who wrote the novel ""Brighton Rock""",Graham greene +General,"Which actor and muscle man, a former 'Mr. Universe', gained fame and fortune in Italy, playing mythical heroes such as Hercules",Steve reeves,General,Dybowski's Formosa and Japanese are types of what,Sika or Saki Deer,General,What island group is named after a type of crocodile,Cayman +General,2% of Americans admit to doing what,Affair with Postman,General,"In Rome, what was the Cloaca Maxima",Main sewer,General,What was the first nation to resign from the united nations,Indonesia +General,What mountain overlooks Rio de Janeiro harbour,Sugar Loaf,General,3.26 light years equals one___.,Parsec,General,Who shot lee harvey oswald,Jack ruby +General,An old sweet scented rose,Damask,General,Who searched for the holy grail,Knights of the round table,General,Who invented punched cards used in early computing 1880s,Herman Hollerith +General,What was rod serling's last television series,Night gallery,General,"In 'David Copperfield', which of his so-called ""Aunts"" has the Christian name Clara",Aunt peggotty,General,What was banned in Indonesia for stimulating passion,Hula Hoops +General,What is a Tam Tam,Orchestral Gong,General,"The song Love is All Around, performed by Wet Wet Wet, is featured in which Richard Curtis film",Four weddings and a funeral,Sports & Leisure,In a nine-ball pool set what colour is the number 2 ball? ,Blue  +General,What colour was the maltese falcon,Black,Science & Nature,"In organic chemistry nomenclature, the prefix ""meth"" means how many atoms of carbon?",One,General,Who was married to Leofric Earl of Mercia in the 11th century,Lady godiva +General,Joseph Hobson Jagger broke it in 1886 broke what,Bank at Monte Carlo,General,"The administration of which U.S. President was known as ""The thousand days of Camelot""",Kennedy,General,Who is Anne Mae Bullock better known as?,Tina Turner +General,A short legged hunting dog,Basset,General,"In The World Of Entertainment How Is “Erik Muhlheim” Or ""Eric Claudin"" Better Known",The Phantom Of The Opera,General,What did Elisha Otis invent in 1852,Elevator +General,The Albert Canal links Liege with which city,Antwerp,General,What is Barbara Streisands middle name,Joan,General,"Which outdoor game is won by ""pegging out""",Croquet +Science & Nature,What Name Is Given To A Plant That Grows From Seed & flowers And Dies Within A Year ,An Annual ,General,By what other name do we know table tennis,Ping pong,General,Who succeeded tommy douglas as leader of the new democratic party?,David lewis +People & Places,Who is Joley Richardson's famous mother? ,Vanessa Redgrave , History & Holidays,"Traditionally children in Italy and Spain don't get their presents on Christmas day. On what day do they get them, is it Christmas Eve, Boxing Day, January 1st or January 5th ",Januray 5th ,General,Epistemology is the study of what,Knowledge + History & Holidays,Who signed the 'thanksgiving proclamation'?,Abraham Lincoln,General,The larva of the click beetle is called what,Wireworm,General,What was the name of the first synthetic plastic made in 1908,Bakelite +General,Which Scandinavian alcoholic spirit is made from potatoes,Aquavit,General,Locomotive 4472 is better known by what name,Flying Scotsman,General,Which decorative art means in Arabic stripped cloth,Macramé +General,Small graceful antelope,Gazelle,General,What US state was once an independent republic?,Texas,General,18% of animal owners do what with their pets,Share beds +Music,The Rise Of Cuban Music In The Late 90's Was Represented By Which Album,Buena Vista Social Club,General,Which part of the body is most affected by the disease diptheria,Throat,General,"In 1950, which mountain was the first of over 26,000 feet to be climbed to the summit",Anapurna +General,In what sort of landscape would you find an erg,Desert,General,"In the final scene of the film ""White Heat"", James Cagney stands on a roof and shouts, ""Made it, Ma!"" Which four words follow","""top of the world!""",General,What is paedology,Study of soil + History & Holidays,What is the name of the telescope that was placed in orbit in the eighties? ,Hubble ,General,Who designed the mini skirt,Mary Quant,General,The opal is the birthstone for which month of the year?,October +General,Is South America or Australia closer to the Antarctic,South america,General,"What star, most popular of 1925 was born in a trench in France",Rin Tin-Tin during WW1,General,What state boasts the highest peak in the bighorn mountains,Wyoming +Science & Nature,As what is a moose also known?,Algonquin,Music,Where Did George & Ira Gershwin Experience A Foggy Day,London Town,General,Of what is mycology the study,Fungi +Art & Literature,Who painted the Mona Lisa,Leonardo da vinci,General,Who was the last amateur to win US tennis open 1968,Arthur Ashe,General,What is a six sided polygon called,Hexagon +General,What city stands on the river Torens,Adelaide - Australia,Music,"On the song ""Dear Prudence"", who's Prudence?",Mia Farrow's sister,General,The triangular area between the two sides of two adjacent arches.,Spandrel +Music,What was the Beatles' first single in 1962?,Love Me Do,General,What did Mother Hubbard look for in her cupboard,A bone,General,Name the first full length color cartoon talking picture.,Snow White and the Seven Dwarfs +Music,"Billy Joels ""Uptown Girl"" Video Featured Which Super Model",Christie Brinkley,Art & Literature,This Romantic poet and wife of Mary Shelley drowned in a boating accident?,Percy Bysshe Shelley, History & Holidays,He advocated the planting peanuts and sweet potatoes to replace cotton and tobacco.,Carver +Music,What Was The Name Of Phil Collins First ever Solo Album,Face Value,General,In December 2004 Which Piece Of Artwork Was Voted Most Influential Of The 20th Century,Marcel Duchamps / Fountain,General,Carrie all what is the capital of ohio,Columbus +General,Which Wild Animal Has The Proper Name Of Acinonyx Jubatus,Cheetah,General,What did He-man say when he lifted his sword and gained his strength?,By the power of Grayskull I am He-Man,General,What breed of dog advertises hush puppies,A basset hound +General,What is the first book of the Bible,Genesis,Science & Nature,Every Few Years Lemmings Do Something Rather Stupid___.What? ,Throw Themselves Off Cliffs ,General,In which cartoon series did the chartacter 'Snagglepuss' first appear,Yogi bear +General,"The Allman Bros Song Entitled ""Jessica"" Is The Theme Tune To Which Long Running Tv Show",Top Gear,Music,"Name The Band That Had Hits With ""Freedom Of Choice"" ""Beatiful World"" & ""Working In A Coalmine""",Devo,General,In what Australian state would you find Gladstone,Queensland +General,What name is given to the hypothetical super-continent which consisted of all the present continents before they split up,Pangaea,General,What country's cavalry used dried milk as long ago as the 13th century,Mongolia,Music,Who Is Janet Jacksons Famous Older Sister,LaToya +Food & Drink,"What name is given to the unripe, ground or whole berries of Piper nigrum? ",Black Pepper ,General,John Le Carr invented what common term used in espionage,Mole,Music,"""Hello Nasty"" In 1998 Was The Beastie Boys 5th Album Name 2 Of The Previous 4","Licensed To Ill, Pauls Boutique, Check Your Head, Ill Communication" +General,Musical terms - what does De Capo mean on a score,From the beginning,General,"What is the song ""Pass the Dutchie"" about?",A Cooking Pot,Entertainment,Who was Miss Hungary in 1936?,Zsa-Zsa Gabor + History & Holidays,Which former dictator was executed by firing squad on Christmas Day 1999 ,"Nicolae Ceausescu, (Romania) ",General,Little used name for either corner of the eye?,Canthus,General,Who was the 16th president of the united states,Abraham lincoln +Art & Literature,"From which Shakespeare play is this line taken: Double, double",Macbeth,Food & Drink,In what US state are 75% of world's pineapples grown? ,Hawaii ,Art & Literature,Which Author Described World War One As The War To End All Wars? ,HG Wells  +General,"The ""canebrake"", ""timber"" & ""pygmy"" are types of what",Rattlesnakes,Music,What did Joy Division change their name to?,New Order,General,Able to be decomposed by bacteria or other living organisms,Biodegradable +General,"What is the nickname for Mobile, Alabama",Gulf city,Music,"During the 1980's, which song and who sang “These mist covered mountains, Are a home now for me, But my home is the lowlands, And always will be, Some day you'll return to, Your valleys and your farm”?",Dire Straits / Brothers In Arms,General,Where was Freddie Mercury born,Zanzibar +General,What is a resident of Manchester called?,Mancunian,General,What animals cannot swim,Gorillas,General,The fibrous form of several minerals and hydrous silicates of magnesium,Asbestos +General,Lee Marvin won his only Best Actor Oscar for the dual role of Kid Sheleen and Tim Strawn in which film,Cat ballou,General,In the USA the government says its a crime to give false what,Weather Reports,Entertainment,This movie directed by Woody Allen won the best picture Oscar in 1978.,Annie Hall +Geography,Which Woman Lived With Natives In West Africa & Became The First European To Visit Parts Of Gabon In 1894 ,Mary Kingsley , History & Holidays,"In 1979, Jeremy Thorpe was found not guilty of plotting to murder whom? ",Norman Scott ,General,What word is used for the branches of willow used to make baskets,Osier +General,Who directed the Halloween series of films,John Carpenter, Geography,Where is George Washington buried?,"Mt. Vernon, Virginia",General,Which artist painted the picture Guernica,Pablo picasso +General,What is the middle name of author H.G. Wells,George,General,What common word comes from the French for purse or wallet,Budget,General,What's the highest position attained in the loyal order of moose lodge,Supreme governor +General,How many 'southpark' episodes have there been that kenny didn't get killed,One,Geography,Where did the most powerful explosion ever witnessed on Earth occur?,Krakatoa,Sports & Leisure,Which Country Won The 2007 Woman's World Cup Final ,Germany (Beat Brazil 2.0)  +General,Which British director founded the Theatre Workshop in Manchester in 1945,Joan littlewood,Food & Drink,"Which fruit has varieties called Concord, Niagra and Muscat? ",Grapes ,General,What was Vivaldi's profession apart from composing,Priest +Music,Who Was On Paul Evans Telephone Answering Machine In 1978,Joanie,General,Which Danish word means 'play well',Lego,General,What is Madame Tussaud's,Wax museum +General,Percent in which county are all ten of england's highest peaks,Cumbria,General,Where is the coldest desert in the world?,Antarctica,General,Who was known as 'the iron lady',Margaret thatcher +Sports & Leisure,Which British Driver Was Formula One World Champion In 1996 ,Damon Hill ,General,"Which word is related to these three rat, blue, cottage",Cheese,Music,Who Replaced David Lee Roth As Lead Singer Of Van Halen,Sammy Hagar +General,The locals call it Shqiperia what do we call this country,Albania,Science & Nature,"What is the name given to elementary particles originating in the sun and other stars, that continuously rain down on the earth?",Cosmic rays, History & Holidays,When Did The Storming Of The Bastille Take Place? ,"July 14th, 1789 " +Sports & Leisure,Which Female Tennis Star Was Handed A Two Year Ban After Testing Positive For Cocaine At Wimbledon In 2007? ,Martina Hingis ,General,What is a Roastchaffer,A Beetle,General,The are 336 on a standard one 336 what on what,Dimples on a Golf Ball +General,Who did anthony eden succeed as british prime minister in 1955,Winston, Geography,Which capital is known as the Glass Capital of the World?,Toledo,General,What is the Capital of: United Arab Emirates,Abu dhabi +General,Gaye on which show were we introduced to ernestine the operator,Laugh in,General,Whose girlfriend had a pet snake called Enid,Adolf Hitler,General,What australian food was discovered by john macadam,Macadamia nuts +Food & Drink,"Popular in the Netherlands, what type of food is a frikandel? ",A Sausage ,General,When did disneyland open,1955,Music,What Instrument Did Count Basie Play,Piano +General,Spermophobia is the fear of,Germs,General,What part of you is an orthodontist most interested in,Teeth, History & Holidays,"In the 17th Century, how did Matthew Hopkins become known and feared ",The Witchfinder General. He hung 60 witches  + Geography,What is the name for the deepest part of the ocean ?,Abyss,General,Men without chest hair are more likely to get what disease,Cirrhosis of the liver,Science & Nature,Where Would You Find Your Uvula ,The Back Of Your Mouth  +General,How much were betty grable's legs insured for,One million dollars,General,"In Greek mythology, who was oenone's husband",Paris,General,How did multi millionaire Russell Sage save money,Not wear underwear +General,What was the full name of the cat,Thomas hewitt edward cat,Food & Drink,In Indian cuisine what is ghee? ,Clarified butter ,General,Nosophobia is the fear of,Disease +People & Places,Who Is Kenneth Brannagh Married To ,Emma Thompson ,Science & Nature,"Who was the first person to notice ""canals"" on Mars ?",Schiaparelli,General,After how many points do players change service in table tennis,Five +General,"What links Bass, Messina, Hormuz and Torres",Straits of water,General,Women compete between USA and UK in Wightman Cup - Sport,Tennis Lancaster,Entertainment,Which superhero loves peace enough to kill for it?,Peacemaker +General,What is a person with a strong desire to steal,Kleptomaniac,People & Places,What instrument does Vanessa Mae play ,Violin , Geography,What is the largest city in Switzerland?,Zurich +Music,"What Was Barbara Disksons Only Top 10 Hit, Reaching No 9 In 1976",Answer Me,General,Who is the Greek equivalent of the Roman goddess Diana,Artemis,Music,Which Beach Was Far Away In Time,Echo Beach +General,What is the offspring of a male horse and a female donkey,Hinny,General,According to Elvis Presley who / what was Little Elvis,His Dick or Penis,General,What's the ancient language of India,Sanskrit +General,Where would you find your Glabella,Space between your eyes,Science & Nature,How many degrees are there in a circle ,360 ,General,"What sport penalises for spearing, slashing, boarding and butt-ending",Ice +General,Who sang the theme song in the Bond film For Your Eyes Only,Shena Easton,General,If you had pogonophobia what would you be afraid of,Beards,General,How many senators comprise the U.S. Senate,100 +Music,What Was The Name Of Elvis Presley's Manager?,Colonel Tom Parker,Art & Literature,"A movement in European painting in the seventeenth and early eighteenth centuries, characterized by violent movement, strong emotion, and dramatic lighting and coloring.",Baroque,Science & Nature,To The Nearest 250 Grams What Is The Average Weight Of An Adult Brain ,1.5kg (3lbs)  +General,Which planet in the solar system was discovered in 1846,Neptune,General,Yorick in Shakespeare's Hamlet had what job (when alive),Jester,Science & Nature," __________ bats do not suck blood. They bite, then lick up the flow.",Vampire +General,Who starred in the Alfred Hitchcock film 'To Catch a Thief,Cary grant,General,What is measured with a sphygnomanometer,Blood pressure,Science & Nature,What is the symbol for silver?,Ag +General,In France what is Framboise,Raspberry,General,Baseball: the boston ______,Red sox,Food & Drink, Where was Budweiser first brewed,St. louis +Science & Nature,For which illness did Lois Pasteur develop a cure ,Rabies ,General,What is the flower that stands for: admiration,Amethyst,General,Transylvania is in which present day country,Romania +Music,The Song Mr ________ Man Was A Hit For The Birds,Tambourine,General,"Who wrote the book ""A Clockwork Orange""",Anthony burgess,Music,"Ub40's ""Red Red Wine"" Was From Which Album",Labour Of Love +General,Wild Bill Hickok was shot in the back by a stranger during a poker game. The hand he was holding aces & eights is known to poker players as what,Dead man's hand,General,In Portugal if you bought sem chumbo what is it,Unleaded Petrol,General,What is the capital of wyoming,Cheyenne +General,What does Sweden call itself on its stamps,Sverige,People & Places,By What Name Was William Bonney Better Known As? ,Billy The Kid ,General,What Team Sport Was Invented By William George Morgan Of Holyoke Massachusetts In 1895,Volleyball +Science & Nature,Some animals spend the winter in a sleep-like state known as _________.,Hibernation,General,Who is Wendy Darling's friend,Peter pan,General,What is the fear of heat known as,Thermophobia +General,When a a positive & negative plate are placed together in an electrical circuit which stores a charge in the form of an electric field it is called a _________,Capacitor,General,What is the more popular name for the Londonderry Air,Danny Boy,General,Which President weighed 352 pounds?,Taft +General,"Cocktails: whiskey, angostura bitters, and sugar make an",Old fashion,Entertainment,What is the sequel to the film 'Every Which Way But Loose'?,Every Which Way You Can,General,What egyptian object is also known as 'the key to the Nile'?,Ankh +Sports & Leisure,Which Jump Event Did Carl Lewis Specialize In As Well As The Sprint? ,Long Jump ,General,"On Friends,what did Phoebe promise to give Chandler if her never smoked again?",7000, History & Holidays,Who invented the record player ?,Thomas Alva Edison +Science & Nature,"What does ""Ursa Major"" mean in everyday English?",Great bear,General,What is a group of curs,Cowardice,General,Which one of the A Team was a Pilot?,H.M. Murdoch +General,"Who said ""Too much of a good thing is wonderful""",Mae West,General,What is the flower that stands for: pure and lovely,Red rosebud,General,T H White wrote the book for which Disney animated feature,Sword in the Stone +General,"The legendary creature ""sasquatch"" was also known as_________",Bigfoot,General,Babies are born without what,Knee Caps - form at 2 - 6 years,Food & Drink,What is beeswing? ,Film that forms on the side of a bottle of port  +General,What is the young of this animal called: Fox,Cub pup,General,"On a ship, what is the line that indicates the maximum load that may be transported?",Plimsoll Line,General,What is the atomic mass of sulphur,32.06 +Science & Nature,What was the first recorded message?,Mary had a little lamb,Geography,"The ____________ is actually a desert environment, averaging about the same amount of monthly rainfall as the Sahara Desert.",South pole,General,"In the book Goodbye Mister Chips, what subject did Mr. Chipping teach",Latin +General,Who is robert van winkle,Vanilla ice,Music,According To The Beatles How many clubs a day did the girl who came in through the bathroom window work at?,Fifteen,Music,Which Radio One DJ Used To Feature A Daily Slot Known As Our Tune?,Simon Bates +People & Places,"Which film star's statue stands in Leicester Square, London? ",Charlie Chaplin ,General,According to the nursery rhyme which bush do we go round on a cold and frosty morning,Mulberry,General,What is the literal meaning of Graffiti,Scratched Drawings +General,What product was originally called Baby Gays,Q Tips,General,In the Jewish religion what's banned during The three weeks,Marriage or Haircut,Music,What Was The First No.1 For The Searchers,Sweets For My Sweet +General,The word negligee is French and suggests wearer does what,Avoids refrains from housework,Science & Nature,What is also known as the 'bishop's stone'?,Amethyst,Religion & Mythology,"In Greek mythology, who ruled over the island of Samos?",Polycrates +General,What saint's day is march 17,St. patrick's,Food & Drink,What shape is macaroni? ,Tubular ,Sports & Leisure,California Dolls Is An 80's Movie Comedy About Which Sport ,Wrestling  +General,US civil war Confederate Kingston hospital Georgia specialised,Soldiers with Venereal Disease,General,"What name is given to the crab that lives in an empty gastropod shell, moving to another shell when it outgrows its current home",Hermit crab,Entertainment,What was the relationship between Superman and Supergirl?,Cousin +General,US school buses are Chrome Yellow but they used to be what,Omaha Orange,General,What is the former name of Istanbul,Constantinople,General,What is a langouste,Crawfish +General,What type of food is a sacatorta,Chocolate cake or gateau,General,By what Alias does Ferris Bueller get into Chez Luis?,Abe Frohman,Music,"In Which Country Was Duran's Duran's ""Hungry Like The Wolf"" Video Shot",Sri Lanka +General,Doctors often have this instrument around their neck,Stethoscope,General,What armys motto is blood and fire,Salvation army,General,Germanophobia is a fear of ______,Anything german + Geography,Where is Tabasco?,Mexico, History & Holidays,The USA's official National Christmas Tree is in which National Park? ,"King's Canyon National Park, California ",General,What is the Capital of: Saint Vincent and the Grenadines,Kingstown +Entertainment,In which London recording studios did The Beatles record the majority of their work?,Abbey Road,General,How many herbs and spices are used in kentucky fried chicken,Eleven,Sports & Leisure,What sport/game is Chris Evert associated with?,Tennis +General,Who wrote The Deceiver 1991 and The Fist of God 1993,Frederick Forsyth, History & Holidays,What did Rudolph never get to join in ,All the other reindeer games ,Music,Which was the last album recorded by the Beatles?,Abbey Road +General,The Sam Maguire Trophy is played for in which sport,Gaelic Football,General,In what country did stamp collecting start,France,General,"What's the international radio code word for the letter ""Z""",Zulu +General,In What Year Did Blackburn Rovers Last Win The FA Cup Final,1928,General,Ilex is the botanical name of which shrub,Holly,General,"There Are 3 Countries In The World Where Chinese Is The Native Language ""China & Taiwan"" Are 2 Name The Other",Singapore +General,In Australian slang what kind if food is a mystery bag,Sausage,General,What word connects a blacksmith and the human ear,Anvil,General,Who invented the first safety razor in 1895,King Camp Gillette +General,"What are Grapnel, Bruce, Danforth, Plough types of?",Anchor,General,Rawdon Crawley was a character in what classic novel,Vanity Fair,General,Who wrote the supernatural tale The Turn of the Screw,Henry james +General,When is turkey traditionally eaten in america,Thanksgiving,Entertainment,"What actor appeared in all three of these films, Straw Dogs, Midnight Cowboy, and The Graduate?",Dustin Hoffman,General,In a recent survey what % of US wives thought husband cheating,90% +General,Which European country has the largest percentage of forest and woodland,Finland,Toys & Games,Which game has 361 intersections ?,Go,General,Name the Italian dish made from pasta squares filled with meat,Ravioli +Food & Drink,What two fruits grow on palm trees? ,Coconuts and dates ,Music,Which Group Won The Prestigious Mercury Music Prize In 2006?,Arctic Monkeys,General,What year did the first baby boomers turn 50,1996 +Geography,What Is The Most Popular English City? ,London ,General, Any object worn as a charm may be called a(n) _______.,Amulet,General,Who was the last communist leader of East Germany,Egon krenz +General,By royal decree in Jidda 1979 women banned from using what,Hotel swimming pools,General,Can you name the 5 original MTV VJ's?,"Martha Quinn,J.J. Jackson,Mark Goodman,Alan Hunter and Nina Blackwood",Science & Nature,What Type Of Car Did Micheal Caine Drive In The Film Get Carter ,Jaguar Mk 2 3.4  +General,What does the dalmatian have on each individual hair follicle,Barb,General,"Which 1995 film, starring Tom Hanks, used the publicity blurb ""Houston, we have a problem.""",Apollo 13,Science & Nature,Which breed of dog has a name derived from the old name for Greece ?,Greyhound +General,What treaty ended the American revolution,Treaty of paris,General,The correct name for the voice box is the _________,Larynx, History & Holidays,She was the first woman to fly the Atlantic solo.,Amelia Earhart +General,Johnny rivers sang 'secret ______ man',Agent,Geography,Which is the largest lake in the world? ,Caspian Sea ,Geography,Name The Straight Between The North And South Islands Of New Zealand ,Cook Strait  +General,Where is alsace-lorraine,France, History & Holidays,Who Released The 70's Album Entitled Cant Buy a Trill ,Steely Dan ,General,What nocturnal bird is traditionally believed to be wise,Owl +General,"A structure usually attached to a building, such as a porch, consisting of a roof supported by piers or columns.",Portico,Science & Nature, Camels were used as pack animals in __________ and Arizona as late as 1870.,Nevada,Sports & Leisure,In Which County Is Lord's Cricket Ground ,Middlesex  +General,A small crown,Coronet,General,"Somerset Maugham, A J Cronin, Richard Gorden - in common",Not Writers - Doctors,General,"What story features flopsy, mopsy and cottontail",Peter rabbit +Food & Drink,What is a `rosti'? ,A pancake of fried grated potatoes,General,What was the name of the Roman God of sleep,Somnos,General,What kind of animal was Rikki Tikki Tavi in The Jungle Book,Mongoose +General,"What was the name of the Duke of Wellington, who defeated Napoleon at the battle of Waterloo",Arthur Wellesley,General,What is the capital city of Australia?,Canberra,Science & Nature,What appears when the sun activates melanocytes?,Freckles +General,What is the 'bishop's stone',Amethyst,General,A Vexilliologist is an expert in what,The history of flags,General,How many days do dragon flies live for on average?,One +General,A sumptuous formal dinner,Banquet,General,The largest _______ bottle is a 170 ft. tall water tower,Ketchup,General,Who was the founder of Judaism,Abraham +Sports & Leisure,Which female gymnast won 3 golds and a silver at the 1972 Olympics? ,Olga Korbut ,General,Daniel Defoes first novel was published in which year,1719,Toys & Games,"In Monopoly, What is the cost to buy Electric Company",150 +Art & Literature,Who wrote 'The Time Machine'?,H.G. Wells,General,The Naira is the currency of which African country,Nigeria, History & Holidays,What late night show replaced Tom Synder's show? ,David Letterman  +Sports & Leisure,How Much Did NewCastle Pay For Alan Shearer ,15 Million ,General,Which U.S. President won the Nobel Peace Prize in 1919 for securing the League of Nations covenant at Versailles,Woodrow wilson,Science & Nature,What Is The Name Of The Phenomenon In Which Light Bends When Passing Through a Lens ,Refraction  +General,A Tiercel is the correct name for a male what,Hawk or Falcon,General,What's the capital of oman,Muscat,General,The Amati family were famous for making what,Violins +Science & Nature, The owl is the only bird to drop its upper eyelid to wink. All other birds raise their lower __________,Eyelids,General,What scandinavian capital begins and ends with the same letter,Oslo,General,Whose song 'Keep the Home Fires Burning' was one of the most successful during WWl,Ivor novello +Religion & Mythology,Who is the greek equivalent of the roman god Venus ?,Aphrodite,General,Pulkovo airport serves which Russian city,Lenningrad,General,Which Country's Parliament Is Called The Storting,Norways +General,Who invented the electric razor in 1928,Jacob schick,General,With what branch of medicine is Franz Mesmer associated?,Hypnotism,General,Trees give off excess water through microscopic holes in leaves called,Stomata +General,"In ""The Karate Kid"" what color did Daniel have to paint Miagi's house, as part of his training",Green, Geography,What is the capital of Cambodia ?,Phnom Penh,Science & Nature,What Is A Diamond Made Of ,Carbon  +General,What is the unit of currency in Thailand,Baht,General,What is a group of this animal called: Goat,Herd tribe trip,General,What theologian claimed he could drive away the devil with a fart,Martin Luther +General,"Whe Beat ""Martina Navratilova"" In The Wimbledon Woman's Singles Final In 1994",Conchita Martinez,General,Whose recent books include 'Shattered' and 'Second Wind',Dick francis,General,What are Berner Florin Parisian frill types of,Canaries + Geography,In what country is the Mekong River Delta?,Vietnam,General,This fingerlike projection is attached to the large intestine,Appendix,Sports & Leisure,Which Sport Commences With A (Face Off ) ,Ice Hockey  +General,What alcoholic drink is distilled from molasses,Rum,General,Which country has the most daily newspapers,India,General,What role did ken osmond play on leave it to beaver,Eddie haskell +General,What is the pope's pontificial ring,Fisherman's ring,General,What is the world's largest lake,Caspian sea,General,What letter is probably on most cold water taps in frankfurt,K +General,Little Jimmy Osmond topped the charts with 'Long Haired Lover from___,Liverpool,Geography,What is the capital of Yugoslavia,Belgrade,General,What is a flowering plant that lives three or more years called,Perennial +General,Who rode a horse called Morengo,Napoleon at Waterloo,Music,Which Band Had A Hit With Banana Republic In 1980,The Boomtown Rats,Sports & Leisure,In Which Year Did Daley Thompson Win BBC Sports Personality Of The Year? ,1982  +General,What river is spanned by the George Washington Bridge,Hudson,Geography,What Was The River Zaire Formerly Known As ,The Congo ,Music,"How Were New Kids On The Block Billed After A 2 Year Absence From The Charts With Their 1994 Efforts ""Dirty Dawg"" & ""Never Let You Go""",NKOTB +General,What country's currency is the Bolivar?,Venezuela,General,Which cathedral features on the back of a UK £20 bank note?,Worcester Cathedral, History & Holidays,In Us Dollars How Much Did America Pay Russia For The State Now Known As Alaska ,7.2 Million  +General,To what instrument family do french horns belong,Brass,General,Tequila Cointreau (triple sec) and lime make which cocktail,Margarita, History & Holidays,"What was the nationality of the prisoners in the ""Black hole of Calcutta""",British +General,How many johns have been pope,21,General,"The Oklahoma bombing suspect obtained a copy of the ""Turner Diaries,"" a book which advocates the violent overthrow of government, from where",Internet,General,And what happened to Tigris 3rd April 1978,He burned it anti war protest +Music,Name Culture Clubs Debut No.1,Do You Really Want To Hurt Me,General,Smooth barked glossy leaved tree,Beech,General,What nationality was Pontius Pilot by birth Scottish -,Fortingale nr Dunkeld +General,What boxer was nicknamed The Boston Strong Boy,John L Sullivan,General,What is the young of this animal called: Swan,Cygnet,Science & Nature,Which Aply Named Spider Devours Its Partnet After Mating ,The Black Widow  +General,Which comedian created the character of maude frickert,Jonathan winters,General,"The greatest snowfall ever in a single storm was ___ inches at the Mount Shasta ski bowl in february, 1959",189,Science & Nature," Hamadryas __________, in ancient Egypt, were believed to be companions and oracles of the god Thoth. They were given the honor of being mummified when they died.",Baboons +General,The hamburger was invented in what year,1900,General,What beating victim's 23-lawyer defense team handed the city of los angelesfor $4.4 million?,Rodney king,Science & Nature,Which 3 Colours Are Used To Form The Picture On A TV Screen ,"Red, Green, Blue " +Music,Pat & Greg Made A Bit Of A Hue & Cry With Which 1993 Remix Of Their 1987 Hit,Labour Of Love,General,Zoophobia is the fear of ______,Animals,Sports & Leisure,At which sporting venue can you visit the oldest sports museum n the world? ,Lord's ( MCC Museum )  +General,Who burned atlanta in 1864,General sherman,Science & Nature," a camel with one hump is a dromedary, while a camel with two humps is a __________",Bactrian,General,Who owns: Right Guard deodorant,Gillette +General,What is a group of apes,Shrewdness, History & Holidays,Who Released The 70's Album Entitled Germ Free Adolescents ,X-Ray Spex ,General,What band recorded the 1978 hit album: 'Briefcase Full of Blues',The Blues +Geography,In which continent would you find the volga river ,Europe ,General,Which literary prize started in 1968,Booker McConnell,General,Popular term for RCMP officers.,Mounties +General,Which actress has the real name Diane Hall,Diane keaton,General,What is the more common name of nitrous oxide,Laughing gas,Science & Nature,What is the name given to the art of miniaturising trees and maintaining their small size?,Bonsai +General,FDA regard 5 Fruit fly maggots 3oz per can acceptable - what,Canned Mushrooms,General,Lygophobia is the fear of,Darkness,General,Who was dr zhivago's great love,Lara +Entertainment,Who is married to Eddie Van Halen?,Valerie Bertanelli,Music,"Who Duetted On The 1982 Christmas Hit ""Peace On Earth - Little Drummer Boy""",David Bowie & Bing Crosby,General,Name all of Bob the Builders friends?,"Wendy, Muck, Roley, Loftey and Dizzy" +General,"What is produced by putting a whole ""maris piper"" in an oven until it goes soft inside",Baked potato,General,"Which group sang the song ""Fuel""?",Metallica,General,What city do batman and robin patrol,Gotham city + Geography,What is the basic unit of currency for Cyprus ?,Pound,General,What do you have alot of if you are hisute,Hair,General,Which British City Was Crowned Curry Capital Of The The UK In 2006 & 2007,Glasgow +Geography,Where do tangerines live ,"Tangier, morocco ",General,What's malcolm x's real name,Malcolm little,General,Which SF author wrote The Day it Rained Forever,Ray Bradbury +Music,Who Had A Hit In 1985 With the Gambler,Madonna,Science & Nature,Linen is obtained from the fibers of what plant?,Flax,General,What does al capone's headstone say,My jesus mercy + History & Holidays,What colour was Rudolph the Reindeer's nose? ,Red ,General,Who was upper Canada's first chief justice,William osgoode, History & Holidays,Who Released The 70's Album Entitled For Your Pleasure ,Roxy Music  +General,Which magazine is subtitled 'The International Magazine for Men',Penthouse,General,Edward Ricardo Braithwait wrote what novel,To Sir with Love, History & Holidays,Who was patricia hurst with the night she was kidnapped ,Steven weed  +General,What WW II British medal carries the words For Gallantry,George Cross,General,What do humans shed about 1.5 pounds of every year,Skin, History & Holidays,"Who is ""The Iron Lady""?",Margaret Thatcher +General,If you ain't done it by age 40 chances are you never will - what,Go to Prison,General,Collective nouns a Host of,Sparrows,General,Which Famous Pop Groups Name Means Far From These Things,Procal Harem +Geography,Montevideo Is The Capital Of Which South American Country? ,Uruguay ,General,Which Bird Is The Symbol For The RSPB,Avocet,General,Which television and radio personality wrote the historical novel Credo in 1996,Melvyn bragg +Sports & Leisure,Who Won His Only World Snooker World Championship In 1979? ,Terry Griffiths ,General,On what street in New York does the city's famous Easter parade take place ,5th Avenue ,General,What came down on jesus' head after he was baptised,Dove +General,What is a group of this animal called: Ferret,Business fesnyng,General,In 1848 The London Daily News carried the worlds fist what,Weather report / forecast,Sports & Leisure,What Is The Only Sport Where The Defending Team Are Always In Possession Of The Ball & The Attacking Team Can Score Without Even Touching It ,Baseball  +General,In Illinois you can get three years for eavesdropping on who,Your conversation,General,What links Wade - Spode - Misen - Delft,Pottery, History & Holidays,Which british comedian was the first man to appear on the cover of 'playboy'?,Peter Sellers +General,For what movie did Humphrey Bogart win his only Oscar?,The African Queen,General,What creature can live up to one year without eating ( you? ),Bedbug,General,What author married Leon Trotsky’s secretary in 1924,Arthur Ransom +Food & Drink,How many herbs and spices are used in Kentucky Fried Chicken ,Eleven ,General,In North Carolina $50 fine for having what furniture on front porch,Upholstered,General,Who was Jungle Jim's pet dog,Skipper + History & Holidays,Whose Scary Movie Character Has The real name is Charles Lee Ray ,Chucky ,General,Whats the international radio code word for the letter O,Oscar,General,What Type Of Person Lives In A Krall,A Zulu +General,What was Buddy Hollies current single when he died,It doesn't matter any more,General,"Who, at USA customs declared, nothing but my genius",Oscar Wilde,General,Where are one quarter of the bones in the human body,Feet +General,In the body where would you find your villus,Small Intestine,General,In which U.S. TV soap opera were the 'Barnes Family',Dallas,General,What countries are known as the abc powers,Argentina brazil chile +Science & Nature,Who Designed The VW Beetle ,Ferdinand Porsche ,Sports & Leisure,Where were the 1952 Olympics held ?,"Helsinki, Finland",General,In Equatorial Guinea its illegal to name your child what,Monica +General,Wedding rings are normally worn on what finger of your hand?,Ring finger,Art & Literature,Who had decieved the Lord of Rohan for a number of years,Wormtongue,General,Franklin was a US state till 1796 what's it now called,Tennessee +General,"Which Famous Composer Wrote The Famous Opera ""Carmen""",Bizet,General,The famous Woodstock music festival took place in what year,1969,Sports & Leisure,The Phrase 'You Cannot Be Serious'' Was Made Famous By Which Former Tennis Star ,John McEnroe  +General,Who is harold lloyd jenkins,Conway twitty,Music,"Who Had A Hit In 1996 With ""Don't Look Back In Anger""",Oasis,General,What legendary table seats 150,King arthur's round table + History & Holidays,Between which countries was the shortest war in history?,Zanzibar and England,General,Gone to Texas by Forest Carter is the basis for what film,The Outlaw Josey Wales,General,Who wrote the myth series,Robert asprin +General,What is the flower that stands for: poverty,Evergreen clematis,General,Which Steven Spielberg film was based on a book by Peter Benchley,Jaws,General,"Which Mediterranean island has coasts on three seas - Mediterranean, Ionian, and Tyrrhenian",Sicily +General,What Team Were Liverpool Playing When The Hillsborough Disaster Occurred In 1989 ?,Nottingham Forest?, Geography,How many countries have an area less then 10 square miles ?,"Four (Vatican City, Monaco, Nauru and Tuvalu)",General,Dari is a dialect of which language,Persian - farsi +General,What english city was known to the romans as venta bulgarum,Winchester,Music,Which Single Has Been Released By The Greatest Number Of Artists,The Beatles / Yesterday,General,Ancient Carthage is in what modern country,Tunisia +General,Who sang 'all right now',The free,General,What is the more common name of the Chaparral Cock,The Road Runner,General,What creature will only mate if the females mouth is full,Spiders +Sports & Leisure,What Score In Darts Is Known As The Madhouse ,Double One ,Music,"In Which Year Did ""Country House"" Go Straight In At Number One For ""Blur""",1995,General,The mathematical notation for a summation is designated by what greek letter,Sigma +General,What is measured in Darwin's,Rate of evolutionary change,General,What is the norway maple often mistaken as,Hard maple,General,"In psalm 46, what is the 46th word from the first word",Shake +General,Which acid gives nettles their sting,Formic acid, Geography,Khartoum is the capital of which country?,Sudan,General,Whih royal wedding of the eighties is now scandelously coming apart?,Charles and Di +Music,Who Blew Toni Basil's Mind In 1982,Mickey,General,What palindromic grass grows at the seashore,Marram,General,Stew of meat and vegetables seasoned with paprika,Goulash +General,What was the name of Australia's first girlie magazine in 1936,Men, History & Holidays,Axl Rose was an anagram for what phrase? ,Oral Sex ,General,"Chamberlain whose funeral was attended by more than 100,000 in new york city in 1926",Rudolph valentino +General,Hawaiian Pia Polish Piwo Hungarian Sor - what is it,Beer,General,What did Peter Sellers use as an ink blotter in the Pink Panther,A Cat,General,What Spanish artists surrelistic paintings feature items such as clock faces,Salvador dali +General,What was lucy's maiden name on 'i love lucy',Mcgillicuddy,General,What is the main constituent of natural gas,Methane,General,"Give another name for hydrocyanic acid (HCN), sometimes wrongly called cyanide",Prussic acid +Science & Nature," Because birds carrying messages were often killed in flight by hawks, medieval Arabs made a habit of sending important messages __________",Twice,Food & Drink,What is ciabatta? ,Italian bread ,Food & Drink,What are the ingredients in the most famous trick pie? (in English literature) ,4 and 20 black birds. (Sing a song of 6 pence)  +General,"A cathedral built between 1554 and 1560 for Ivan the Terrible, to which saint is this Russian Orthodox church in Moscow dedicated",Saint basil,General,"In a famous opera, siegfried understood the speech of birds after tasting ______",Dragon's blood,Music,"Who Spent 14 Weeks In The Charts In 1989 With ""Love Changes Everything""",Micheal Ball +General,Which very select organisation has a table as its logo,Mensa,General,In which country would you find McLaks (grilled salmon sandwich) on the McDonalds menu,Norway,Toys & Games,Number of points lost for scratching off the blue ball in snooker,5 +General,On a carving in Coventry Cathedral what did Lady Godiva ride,A Goat,General,"Whose nicknames included "" The Idol of the American Boy """,Babe Ruth,Food & Drink,Pilaf is this cooked in a broth of meat or poultry.,Rice +Sports & Leisure,At Which Sport Did James Bond Play Auric Goldfinger Waging A Gold Bar On The Outcome? ,Golf ,General,"Who recorded ""Sixteen Candles"" in 1958",Crests,General,What is the most common surname in the Barcelona telephone directory,Garcia +General,Who in 1958 was the first British Formula one champion,Mike Hawthorne,General,In a poll newlyweds spend most time on honeymoon doing what,Arguing or Fighting,General,What countries people had the longest life expectation,Iceland +General,"In religion, the assumption of an earthly form by a god",Incarnation,General,"By finding what, can the slope of a curve at any given point be determined",Derivative,General,In which film did Elvis Presley play an Indian,Stay away joe +General,Who was the first to climb Mt Everest,Sir edmund hillary,General,Which is the largest of the Italian lakes,Garda,General,Carrantual is the highest peak in which country,Ireland +General,"According to the title of the film, what is the profession of Ace Ventura?",Pet Detective,Music,With Which Teen Band Did Bobby Brown Sing With Before Going Solo,New Edition,General,In Connecticut a pickle must do what to be legal,Bounce +Religion & Mythology,He led the Israelites out of Egypt.,Moses,General,Name the actress who played the pretty blond girl Elliot danced with in E.T. The Extra-Terrestrial?,Erika Eleniak,Science & Nature,Which substance has the chemical formula NaOH?,Sodium Hydroxide +General,Which Yorkshire town has the same name as the capital of Nova Scotia,Halifax,Art & Literature,Who Is Pips Benefactor In Dickens's Great Expectations ,Abel Magwitch ,General,What is the flower that stands for: color of my life,Coral honeysuckle +Sports & Leisure,In The 1998 World Cup Which 3 Teams Were Knocked Out On Penalties (Point For Each) ,"England , Italy , Holland ",Entertainment,In which film did Jay Leno play 'Mookie'?,American Hot Wax,General,Who did the music for the 1970's film 'saturday night fever',Bee gees +Science & Nature," Within the hawk, or birds of prey, family, there are __________ species _ eagles, hawks, kites, and Old World vultures, which are found nearly worldwide.",208,General,Rocketbuster make the worlds largest authentic what,Cowboy boots,Science & Nature,What Is A Clavicle ,The Collar Bone  + Geography,What is the capital of Mauritius?,Port Louis,General,"According to a Beatles song, where is the place where nothing is real",Strawberry Fields,General,"What's the international radio code word for the letter ""S""",Sierra +General,What was the name of the home that Sofia Patrillo lived in before moving in with her daughter on the Golden Girls.,Shady Pines,General,What was the name of Ernest Shackleton's ship which was trapped and crushed by polar ice in 1915,Endurance,General,What starts the breakdown of food when it is still in the mouth,Enzymes +People & Places,Which Politician Is Known As Tarzan ,Micheal Hestletine ,General,What is the symbol for tin,Sn,General,George Stephenson was born in what year,1781 +Entertainment,Who married Mutt Lange?,Shania Twain,General,In 1659 Massachusetts outlawed what,Christmas - it was illegal,General,What company owns Rolls Royce motors,Volkswagen +General,What first appeared on Page 1 of the Times 3 May 1966,News stories,General,Pasteur developed a vaccine for rabies in which year,1885,General,What was the name of Dorothy Parkers Parrot,Onan - He spilled his seed on the ground +Science & Nature,What is the only sign in the zodiac which doesn't represent a living thing,Libra,General,Messina was damaged by an earthquake in what year,1908,General,What would you do with a maris piper,Eat it - it’s a potato +General,"What types can be saddle, plane or pivotal",Body Joints,General,Judeophobia is a fear of ______,Jews, History & Holidays,"The first successful organ transplant occurred in 1954. What organ was it A= Kidney, B= Liver, C= Heart? ",A= Kidney  +Science & Nature,How Do Procumbent Plants Grow ,They Spread Overground ,People & Places,Who was The First Man To Run A Mile In Under 4 Minutes ,Roger Bannister ,General,"In an average lifetime, the average american wears 7,500 ___",Diapers +General,"The male gypsy moth can ""smell"" the virgin female gypsy moth from how far away (its a decimal)",1.8 miles,General,What Was Discovered In 1824 By Jean Baptiste Joseph Fourier?,The Greenhouse Effect,General,"Common ore of iron, and one of the most commonly occurring minerals in nature",Goethite +General,What did Americans call the first Cuban in space,Castronaut,General,The foghorn of the QE2 plays in what note,A Flat, History & Holidays,Which London Theatre Referring To The 2nd World War Used The Slogan (We Are Never Closed) ,The Windmill  +General,What word describes the dominance of one state over a group of other states?,Hegemony, History & Holidays,Of which country did Robert Menzies become Prime Minister in April 1939? ,Australia ,General,What was the name of the character on the 1st Garbage Pail Kids Pack?,Blasted Billy or Adam Bomb +General,The Musee de Orsay in Paris was originally what,Railway Station,General,"Which TV Presenter First Hosted The TV Show ""Ready Steady Cook""",Fern Britton,General,What colour is an aircraft's 'black box' flight recorder,Orange +General,"In Which Play Tennesee William Do You Encounter The Character ""Big Daddy""",Cat On A Hot Tin Roof,General,What is the flower that stands for: shyness,Vetch,General,Richard Bachman is a pseudonym of which author,Steven King +General,Frank Oz was the voice of who,Miss Piggy in the Muppets,General,What would a gardener do with secateurs,Prune plants,General,The Marie Celeste sailed from which port,New York +General,What is the oldest town in belgium,Tongeren,General,This is the capital of Ukraine?,Kiev,General,Who wrote The Screwtape Letters,C S Lewis +General,What country lost the Crimean War,Russia,General,What is the medical name for 'hardening of the arteries',Atherosclerosis,General,Which part of your body might suffer from a stigmatism,Eyes +General,What city ranks second after New York as America's advertising hub,Chicago,General,What is the name for the study of cells,Cytology,General,Which country is the only one to have won the Rugby Union World Cup twice,Australia +General,"Devil's tower, the first us national monument, is located in what state",Wyoming,General,Narrow saw on frame for cutting thin wood in patterns,Fretsaw,General,"As a result of their wearing high leather collars to protect their necks from sabres, as what were the first U.S. marines known",Leathernecks +General,What is the society for the advancement of science also known as,British,Art & Literature,"An artwork humoously excaggerating the qualities, defects, or pecularities of a person or idea. ",Caricature, History & Holidays,"How are Caspar, Balthazar and Melchior better known ",The Three Wise Men  +General,Who on TV played Jeeves to Hugh Lawrie's Bertie Wooster,Stephen fry,General,What line on a map connects all points of the same elevation,Contour line, Geography,In what city is the Leaning Tower?,Pisa +Music,Who Spent Four Weeks In The Charts As The High Numbers,The Who,General,What was the name of the ant people created by zeus,Myrmidons,Sports & Leisure,Which Football Team Won the 2005 FA Cup ,Arsenal  +General,"Space indiana jones: what did drinking from the grail ""grant""",Immortality,General,What kind of plane is a CT-114 Tutor,Jet trainer,Entertainment,Who played the 'Universal Soldier'?,Jean-Claude Van Damme +General,"According to the Acts of the Apostles, from where did Christ's Ascension into Heaven take place",Olivet mount of olives,People & Places,Who Is Actress Charlotte Rampling Married To ,Jean Michelle Jarre ,General,At least a quarter of humanity is what,Short Sighted +General,"Who live longer, on average men or women",Women, History & Holidays,Who succeeded Churchill when he resigned in 1955?,Sir Anthony Eden,Geography,In Which 4 Countries Are The Alps Located (PFE) ,"France , Italy , Switzerland , Austria " +General,Which cricketer has played more tests than any other,Allan border,Art & Literature,Whose Smile Remained After The Rest Of It Had Vanished ,The Cheshire Cat's , History & Holidays,He shot Lee Harvey Oswald.,Jack Ruby +General,What is Little Red Riding Hoods name,Blanchette,General,What is the day before Ash Wednesday,Shrove tuesday,General,What is the most dense planet in our solar system,Earth +General,What is the largest ocean,Pacific ocean,General,What Was First Tested By Brian Trubshaw,Concorde,Geography,What u.s state boasts the carlsbad caverns national park ,New mexico  +General,What cartoon show's record prime time run of 6 years was beaten in 1996 by The Simpsons,The Flintstones,General,"What is unkindly described as ""Australia's only contribution to international cuisine""",Pavlova,General,The only country beside Azerbaijan that starts with a but doesn't end with a,Afghanistan +General,Coco de Mer fruits take how long to ripen?,10 Years,Geography,Into what bay does the Ganges River flow,Bengal,General,Who is the Patron Saint of authors,Saint Paul +General,"Which Labour politician did Aneurin Bevan call ""a dessicated calculating machine""",Hugh gaitskell,General,Frank Sinatra John Wayne Paul Newman rejected what role,Dirty Harry Callahan,Music,Who Sang The Theme To The Bond Movie From Russia With Love,Matt Munro +General,What does c.i.a stand for,Central intelligence agency,General,"Mark, rouble and escudo are all types of what",Currency,General,Which Asian Capital City Literally Means Gods Gift ” ?,Baghdad +Music,Which Popular Tv Programmes Theme Tune Reached No.8 In 1962,Z Cars,General,Which British city traditionally has its own independent telephone network system?,Hull,General,"Introduced in Switzerland in 1938, the first brand of instant coffee",Nescafe +General,What boxer played the lead in the broadway musical buck white,Muhammad ali,General,"Who wrote Flauberts Parrot and England, England",Julian barnes,Science & Nature, The gastric juices of a snake can digest bones and teeth _ but not __________,Fur hair +General,Crème de Menthe Crème de Cacao an light cream what drink,Grasshopper,General,The Bald Eagle is Americas bird - What is Britain's,Robin,General,Anna Maria Louisa Italiano became famous as who,Ann Bancroft +General,Which Country Had Their World Cup Debut In France 1998 And Ended Up Finishing 3rd,Croatia,Science & Nature, It is estimated that manatees live a maximum of 50 to 60 __________,Years,General,An aubade or alborda is a song - but what type,Mourning +General,What is a native of tangiers called,Tangerine,Science & Nature,Name the heaviest flying bird of prey.,Condor,General,In 1992 2421 US people were injured at home by what,Their Houseplants +Science & Nature, The ears of the Asiatic __________ are larger than those of other bear species.,Black bears,Science & Nature,What is the atomic number of uranium?,92,General,What is the fear of nuclear weapons known as,Nucleomituphobia +General,Which compound forms about 70% of the human body,Water,General,When was apartheid introduced in south africa,1948,General,Fruit flavored candy pieces made with ju_ju gum.,Jujubes +General,What was the title of polanski's horror spoof,Dance of the vampires,General,What was first built in the Place de Greve in 1792,The Guillotine,General,Who nicknamed his gun Lucrettia Borgia cos it killed everything,Buffalo Bill +Entertainment,"At the end of ""Planet of the Apes"" what protruded from the rocks?",Statue of Liberty,General,What country is the Hellenic republic,Greece,Art & Literature,Who was Winnie the Pooh's neighbour?,Piglet +General,Name Greek Goat Amaltheas horns that good things flowed from,Cornucopia,General,Over 75% of the earth's surface is covered by some form of _____,Water,Music,How Is Ladonna Andrea Gaines Better Known,Donna Summer +General,"What is the gift on the seventh day of christmas in the ""twelve days of christmas""",Seven swans a swimming,General,Who was the first Republican-President of the United States,Abraham lincoln,General,Colourful shrub with drooping flowers,Fushsia +General,Harbour who wrote 'the invisible man',H.g wells,General,"Branch of linguistics concerned with the production, physical nature, and perception of speech sounds",Phonetics, History & Holidays,Which Dark Wizard Was Portrayed By Christopher Lee In Lord Of The Rings ,Saruman  +General,Organisation in the US was co-founded by Ballington Booth,Volunteers of America,General,In Alberta its illegal to play craps if you are using what,Dice,General,Haydn's 'Creation' was based on Genesis and which other piece of literature,Paradise lost +General,"Why Did ""Mathias Rust"" Hit Global Headlines In 1987 An Event That Very Nearly Cost Him His Life",Flew Plane Into Red Square,Sports & Leisure,Field of Dreams is a film about what? ,Baseball ,General,What did mathematician John Napier invent in 1614,Logarithms +General,Which retail entrepreneur founded The Body Shop in 1976,Anita roddick,General,What does the royal family use as confetti,Rose petals,General,"Who wrote ""The Agony & the Ecstasy""",Irving stone +General,If you have a buccula what have you got,Double Chin,General,What animals eye is larger than its brain,Ostrich,General,What happened to golfer Lee Travino on green 13 27 June 1975,Hit by Lightning +Science & Nature,Paedology is the study of ______ ?,Soil,Sports & Leisure,Basketball: The Denver ______?,Nuggets,Geography,Which country has Ankara as its capital,Turkey +Food & Drink,What are the two main ingredients of a Hollandaise sauce? ,Egg yolks and butter , History & Holidays,How Many Men Were There In A Roman Legion? ,6000 ,Sports & Leisure,Which Sporting Couple's Autobiography Is Entitled 'Facing The Music''? ,Jane Torvill & Christopher Dean  +General,What is the Capital of: Guatemala,Guatemala,General,Ilich Ramirez Sanchez became notorious as who,Carlos the Jackal,General,"Marfona, Romano and Pentland Javelin varieties of what",Potatoes +General,"What's the international radio code word for the letter ""N""",November,General,UK snooker players call it doubling what do US pool players say,Banking,General,Disneyland opened in what year,1955 +General,Who played the role of Margaret Schlegel in the film Howard's End,Emma thompson,General,Which pigment produces the colour of hair or skin in animals,Melanin,Food & Drink,From What Is The Red Food Dye Cochineal Derived ,Beetles  +Sports & Leisure,Which Uk Football Team Are Known As The Owls ,Sheffield Wednesday , History & Holidays,In 1986 the prime minister of which European country was assassinated on his way home from the cinema with his wife?,Sweden (Olof Palme) ,General,What does soviet mean,Workers Council +Food & Drink,What Does The Term 'A La Carte' Actually Mean ,From The Menu ,General,What swims at 1/8 inch an hour,Sperm,General,The word 'struthious' refers to something that resembles or is related to which animal,Ostrich +General,The Heights of Abraham is in which Canadian province,Quebec,General,The chemical formula H2O2 refers to what,Hydrogen Peroxide,General,Which country is named after its highest point,Kenya +General,This is said to be history's greatest military evacuation,Dunkirk,General,Name the Wright brothers 1903 plane,Flyer,General,Similes: as white as a _____,Sheet +General,What country has the lowest teen pregnancy rate Western world,Netherlands,General,PD James wrote thrillers what does PD stand for,Phyllis Dorothy,General,A bowl of _______ contains twice as much sodium as a bowl of potato chips,Wheaties +General,Which planet has Prometheus as a satellite,Saturn,General,Where was judas buried,Potters field,Religion & Mythology,"In Greek mythology, who was Medea's husband?",Jason +General,Who got the world's worst circumcision,John bobbit,Music,"His Hits Include ""I'll Be Home"" ""Friendly Persuasion"" & ""Love Letters In The Sand"" Who Is He",Pat Boone,General,What geyser erupts every hour at yellowstone national park,Old faithful +General,What percentage of canadians dine out regularly,70%,General,If you were acomoclitic what would turn you on,Shaven Pubes, History & Holidays,On Tv In The 80's What Would The Initials 'HHOH' Signify ,Hammer House Of Horrors  +Music,"""Dead End Street"" By The Kinks Was Originally Released 1962, 1966, 1968",1966,General,Which of the Seven Wonders of the ancient world was built by a ruler's sister/widow,Mausoleum of halicarnassus,General,What marx brother had real name julius henry,Groucho +General,What was Walt Disney's first cartoon character,Oswald the Rabbit,General,What does hours d'oeurve literally mean,Out of course hence Extra Dish,General,What is the symbol for copper,Cu +General,Who sang about cars and girls in 1988,Prefab sprout,General,Relating to food what is 'pancetta' a type of,Bacon,Science & Nature, The dog and the turkey were the only two domesticated animals in ancient __________,Mexico +General,Which sacred volcano last erupted in 1707,Mount fuji or fujiyama,General,What was the worlds first passenger jet aircraft,Comet,General,Substance that is a poor conductor of electricity and that will sustain the force of an electric field passing through it.,Dielectric +Music,Who Were Bobby Hatfield & Bill Medley,The Righteous Brothers,Entertainment,What are the separators on a guitar neck called?,Frets,General,Plant with pungent bulb used in cookery,Garlic +Music,Who sang lead vocals for Lynyrd Skynyrd?,Ronnie Van Zandt,Art & Literature,What School Of Poets Was John Donne Attributed To ,The Metaphysical Poets ,General,Cannibals are famous for eating what,Human flesh +General,Which Italian dish consists of filled tubes of pasta baked in a sauce,Cannelloni,General,Name the organs that are involved in a triple transplant,Heart lungs and liver,General,"Physics: Energy and Momentum are never lost, they are ___. ?",Conserved +General,What is the capital of north vietnam,Hanoi,Art & Literature,Which Novel Features Perks The Station Porter? ,The Railway Children ,Art & Literature,"The visual and tactile quality of a work based on the particular way the materials are handled; also, the distribution of tones or shades of a single color.",Texture +General,What country invented the fiber tip marker in 1962,Japan,General,What do the initials 'crt' stand for among computer buffs,Cathode ray tube,General,Guatemala is the capital of ______,Guatemala +General,What sport is played on the largest field,Polo,General,The African and French marigolds are native to what country,Mexico,General,Meridian is a shade of what colour,Green +Science & Nature,These attach muscles to bones or cartilage.,Tendons,Music,"On the Beatles song ""Dear Prudence"", who's Prudence?",Mia Farrow's Sister,General,What kind of creature is the griffin,"Half eagle, half lion" +General,Who ruled France during the Franco-Prussian War,Napoleon iii,General,How often does a sesquicentennial occur,Every 150 years,Music,"Who Sang About ""Angie Baby""",Helen Reddy +General,What is a wind with a speed of 74 miles or more,Hurricane,General,Who wrote One flew over the Cuckoos Nest,Ken Kesey,General,Who was clyde barrow's partner,Bonnie parker +Sports & Leisure,Which racehorse houses the burial site of Red Rum? ,Aintree ,Geography,What's the capital of east germany ,East berlin ,General,About what year was the first steam ship built,1787 +General,What is an s curve,Ogee curve,General,What continent is part of both the east and west hemispheres,Antarctica,General,Beveley Hills Cop was Eddie Murphy but who was it intended for,Sylvester Stallone +General,Name The Female Vocalist That Performed With Kenny Rogers On The Song ” We've Got Tonight ”,Sheena Eastern,General,Name the strait joining the Atlantic Ocean and the Mediterranean Sea,Strait,Food & Drink,Chives are a member of which family of vegetables? ,Onion  +General,The Welcome Stranger 173 lb was largest what ever found 1869,Gold Nugget,General,Mike Nesmith From The Pop Group “ The Monkees ” And His Mother Are Responsible For Founding Two Of Americas Most Famous & Successful Companies.,Tipex & MTV,General,Yaounde is the capital of ______,Cameroon +General,"The little lump of flesh just forward of your ear canal, right next to your temple, is called a(n) ______",Tragus,General,Food prepared with wild plants is known as 'cuisine,Sauvage,Music,What Girls Did Queen Sing About In 1978,Fat Bottomed +General,Sitophobia is the fear of,Food eating,General,Liticaphobia is the fear of,Lawsuits, History & Holidays,In Which Welsh Village Where 144 Adults And Children Killed By A Land Slippage? ,Aberton  +General,Arthur Jefferson better known as who,Stan Laurel,Science & Nature,"The ""canebrake"", ""timber"" and ""pygmy"" are types of what",Rattlesnake,General,Barring rain - in which athletics event would you get wet,Steeplechase +General,"What president's hobbies included pitching hay, fishing, and golf",Calvin,General,Monetary unit of Greece,Drachma, History & Holidays,"Which rock star died when the Mini car, driven by his girlfriend crashed into a tree? ",Marc Bolan  +General,"In 1984, who sang 'girls just want to have fun'",Cyndi lauper,General,Abbreviations-short forms: fbi stands for _____?,Federal bureau of investigation,General,What is BSE in humans called,Cjd +General,"Which American Actor Once Had A Trial For ""The Green Bay Packers"" American Football Team",Bill Cosby,General,What colour is the mineral malachite,Green,General,What is the capital of north dakota,Bismarck +General,Who was William Claude Dukenfield better known as,W C Fields,General,Of what is the word 'fortnight' a contraction,Fourteen nights,General,What planet has the longest day,Venus +General,Who invented scissors,Leonardo Da Vinci, History & Holidays,"Which of the following was not a gift from a magi? (Diamonds,Gold, Frankincense,Myrrh) ",Diamonds ,General,A flat round Dutch cheese,Gouda +General,Who is mia farrow's mother,Maureen o'sullivan,People & Places,Who Famously Said 'You Cannot Be Serious' ,John McEnroe ,Food & Drink,"Who, according to the TV commercial, `makes exceedingly good cakes'? ",Mr. Kipling  +General,How much drinking water did a scuttlebutt hold,A day's supply,General,Which French book was written without using the letter 'E' once ?,La Disparition,General,Cass elliot was part of which 'monday monday' group,Mamas and papas +General,"Who was given temporary custody of the ""emerald orb"" (ds9)",Benjamin sisko,General,Which King is known as The Suicide King,King of Hearts Sword through head,General,If you are born between June 23rd and July 23rd what star sign,Cancer +General,What family is a horse,Ungulate,General,Which Famous Austrailian Landmark Was Opened In 1932,The Sydney Harbour Bridge,General,"The observable activity of an ""individual.(________)",Behaviour +General,What term applies to the property of metals that allows them to be drawn out in to a thin wire,Ductile,People & Places,Which King Had A Dog Named After Him? ,King Charles ii ,General,What is the title of the Bond movie in which the 'baddie' is called Scaramanga,The man with the golden gun +General,"Named album of the year in 1981, which pop group's debut album was called ""Dare""",Human league, History & Holidays,Who forced 146 captured British officers into the Black Hole of Calcutta?,Indian troops,General,What is the young of this animal called: Dog,Puppy pup +General,What was the first USA TV series screened in the USSR,Fraggle Rock,General,Who fought in the 1967 Six Days War?,Arabs and Israelis,General,In which classic movies would you hear the phrase 'Stop calling me Shirley'?,Airplane or Airplane 2 +General,For what does O.P.E.C. stand?,The Organization of Petroleum Exporting Countries,Music,Who Was Born Concetta Maria Franco Nero In December 1938,Connie Francis,General,What name is given to farms which specialise in growing crops ?,Arable +Music,"Who Wrote The Manfred Man Single ""The Mighty Quinn""",Bob Dylan,General,Which actor studied as a priest then an architect before acting,Anthony Quinn,Music,"During the 1970's, which song and which group “Don't ask us to attend, 'cos we're not all there, Oh don't pretend 'cos I don't care, I don't believe illusions 'cos too much is real?",Sex Pistols / Pretty Vacant +General,In Australian slang what is a Coughie,Bad Umpiring,General,In ancient Japan what was used to clean teeth,Stale Urine,General,"Who said ""That's one small step for man, one giant leap for mankind""?",Neil Armstrong +General,"Who directed 'drive he said', 'goin' south' and 'the two jakes'",Jack,General,In Georgia its illegal for a barber to do what,Advertise Prices,General,Who portrayed sherlock holmes in 14 films between 1939 and 1946,Basil +General,The process of making cows milk safe for consumption is,Pasteurization,General,"Who sleeps in a matchbox at Geppetto's house, in pinocchio",Jiminy cricket,General,Who playes Captain Picard in Star Trek: the next generation,Patrick stewart +Music,Whose first hit was Rewind The Crowd Say Bo Selecta in 1999?,Artful Dodger/Craig David,General,What was the theme music to The Exorcist,Tubular Bells – Mike Oldfield,Sports & Leisure,Hockey: The Buffalo _________.,Sabres +General,What is the condition resulting from prolongued muscular or mental activity,Fatigue,General,What WW2 resistance movements name is Italian for thicket,Maquis, History & Holidays,For Which Allegedly Obscene Publication Were 'Calder & Boyars'' Prosecuted In 1967 ,Last Exit To Brooklyn  +General,Whats a community of ants called,A colony,Science & Nature,What Planet Is Between Mars & Saturn In The Solar System? ,Jupiter ,General,"In cookery, what does the term ""Julienne"" mean",In strips + History & Holidays,What was the name of the rich boy that Andie was asked to go to the senior prom with in the movie 'Pretty in Pink'? ,Blaine ,General,What German word is printed on the labels of high quality wine,Kabinett,General,Who Was The Mother Of Liza Minelli?,Judy Garland +General,What links Jerry Garcia Buster Keaton Boris Yeltsin,Missing a bit of finger,General,Cretinism is caused by a failure of what,Thyroid gland,General,Which American aircraft company makes the F15 Eagle,Mcdonnell douglas +General,The clusec is the unit measuring the power of what,Vacuum pumps,General,Who would use a swozzle,Punch and Judy man,General,The museum of what can be found at Pontedassio in Italy,Spaghetti +General,Which Famous Character Did “Jean De Brunhoff” Create In 1931?,BarBar The Elephant,Science & Nature,Which Is The Most Poisonous Toadstool? ,The Death Cap ,Sports & Leisure,In 1999 who was voted PFA player of the year? ,David Ginola  +General,What did William Addis invent in prison,Toothbrush,General,Who was the Greek goddess of love,Aphrodite,General,In which city was Abraham born,Ur +Music,"Who Re-Released Lionel Richies Classic ""Easy"" In 1993",Faith No More,General,The artist Seurat employed which technique,Pointillism,Religion & Mythology,This roman soldier pierced the crucified Christ on His side with his spear.,Longinus +Geography,In which continent would you find the congo river ,Africa ,General,What variety of quartz is amethyst,Violet,General,"What is the computer acronym for ""picture element""",Pixel +Geography,What is the capital city of Ethiopia? ,Addis Ababa ,General,Baseball: the houston ______,Astros,General,Which PC game shares it name with a Bond film character,Solitaire +Science & Nature, The flounder swims __________,Sideways,General,Which compound comes from the nux vomica tree,Strychnine,General,What was Elvis Presley's first UK number one,All shook up +General,Loincloth worn by male Hindus,Dhoti,General,In which city is The Abbey theatre,Dublin,General,Heroin is the brand name of morphine once marketed by who,Bayer +Geography,Which is the only US state to have a Z in it's name? ,Arizona ,General,What was the name of geppetto's son,Pinocchio,General,With which pop group is Jarvis Cocker the lead singer,Pulp +Music,From Which Us City Did Techno Originate,Detroit,General,What is is group of kangaroos,Troop,General,Name US mountain range comes from French At the Bows,Ozarks - Aux Arcs +Food & Drink,"German dish with roast beef marinated in vinegar, sugar, and seasonings.",Sauerbraten,Art & Literature,Which Characters Were The Longest Running In The Comic The Beano ,Lord Snooty And His Pals ,General,What is the modern equivalent of the english reeve,Sheriff +Entertainment,"Turn, Side and Why does it always rain on me are all songs from what UK band?",Travis,General,"Complete the Quote ""Alas! Poor Yorick! I knew him, ?""",Horatio,Entertainment,What Marlon Brando film was widely banned?,Last Tango In Paris +General,In heraldry animals addorsed are in what position,Back to Back,General,Which sea separates Turkey from Greece,Aegean sea,General,What is the most assigned book / play in English classes,Romeo and Juliet +Entertainment,"What was the Oscar-winning theme song from ""Breakfast at Tiffany's""?",Moon River,General,Which Norse God invulnerable to all else was killed by mistletoe,Baldur,Entertainment,"What country was the setting for ""The King and I""?",Siam (Thailand) +Sports & Leisure,In Which Country Were The 1992 Olympics Held? ,Spain , History & Holidays,Name the 80's band that named the sides of their first album Hardware and Software and also used samples from Star Trek movies in their songs. ,Information Society ,General,Which twins were the brothers of Helen of Troy,Castor & pollux +General,What fish can blink its eyes,Shark,Food & Drink,What does 'Marbling'' refer to? ,Flecks of fat in meat , History & Holidays,In 1613 Who Did Pocahontas Marry ,John Rolfe  +General,"In Shakespeare's Hamlet, who is the father of Ophelia",Polonius,General,Pinchbeck is an alloy of copper and what else,Zinc,General,Which film preceded 'magnum force' and 'the enforcer',Dirty harry +General,What in muslim countries is a' taj,Brimless hat,General,Disney's Pluto (nee Rover) was originally whose pet dog,Minnie Mouse,Science & Nature,What Are Small To Medium Sized Kangaroos Called? ,Wallabies  + History & Holidays,Which eighties musician got sued by a music related company for using their name as part of his pseudonym?* ,Thomas Dolby ,General,"William McIlvanney wrote the novel The Big Man, who played the title role in the film adaptation",Liam neeson, History & Holidays,What did Ed Peterson invent?,Egg Mcmuffin +Entertainment,"John Travolta, Samuel Jackson, Uma Thurman starred in which 1994 Quentin Tarantino film?",Pulp Fiction,General,Who invented the mini skirt,Mary quant,General,In Ecuador if you were served tronquito what have you eaten,Bull penis soup +Music,Name The 2 Members Of Hall & Oats,Daryl Hall & John Oates,Music,"Which LA group charted with ""Rock The Boat""?",The Hues Corporation,General,1858 Queen Victoria sent the first transatlantic telegram to who,President James Buchanan +Music,Whitney Houston Recorded Which Dolly Parton Song,I Will Always Love You,General,Which bird turns its head upside down to eat,Flamingo,General,What Is The Only Product Owned & Produced By The Virgin Corporation That Does Not Display The Virgin Name Or Logo?,Mates Condoms +General,Device which automatically maintains a motor vehicle at a chosen speed is called,Cruise control,General,What brand and color of underwear is Marty wearing in Back to the Future?,Purple Calvin Klein briefs,Music,Apart From Singing What Occupation Is Tony Bennet Famous For,Painting +General,Who was the first Briton to win the Nobel Prize for Literature,Rudyard kipling,General,Which 9-fingered pop pianist starred in the film Its all Happening?,Russ Conway,General,Stallone how many innings must a starting pitcher pitch to get a victory,Five +General,What fictional Englishman belongs to the Ganymede club,Jeeves,General,Peter Sellers played Clouseau but who dropped the role,Peter Ustinov,Sports & Leisure,Which sport uses stones and brooms,Curling +General,What is a giant sequoia,Redwood tree,General,Link Danny Fisher Charlie Rogers Vince Everett Chad Gates,Elvis Presley character names,General,"Three chemical elements are magnetic, iron, nickel & what",Cobalt +Entertainment,What was Keanu Reeves' computer world alias in 'The Matrix'?,Neo,General,The flower nasturtium is native to which country,Peru,Art & Literature,Which Famous Book Contains The Line 'It was the best of times it was the worst of times it was the age of wisdom' ,A Tale Of 2 Cities  +Art & Literature,Who wrote 'To Kill A Mockingbird'?,Harper Lee,Music,What Group Was Diana Ross A Member Of Before She Embarked On A Solo Career,The Supremes,General,What was Simple Simon fishing for in his mother's pail?,Whale +General,What is the most dangerous job in the u.s,Commercial fishing,Science & Nature,How Many Are There In A Clutch Of Blackbirds Egg's ,Usually Between 3 & 5 ,General,Who was the last monarch to wash the feet of the poor on Maundy Thursday as an act of humility in remembrance of Jesus washing the disciples' feet? ,James II  +Music,"""Big Spender"" Was A Hit In 1967 For Which Female Singing Star",Shirley Bassey,General,Collective nouns - what profession gather in a bench,Bishops,General,What connects kabul with peshawar,Khyber pass +General,How tall was the tallest man,"8' 11""",General,What is the most westerly county of Ireland,Kerry,Music,What Did Chers Baby do In 1966,He Shot Her Down +General,Which TV cartoon series based in Springfield started life on the Tracey Ullman show,The simpsons,General,In sexual terms what is a mastix,Female Sadist,Geography,What is the capital of Peru,Lima +General,What future yippie leader was the first male cheerleader at brandeis,Abbie,General,"Which statesman said that Germany's problems could only be resolved by ""blood and iron""",Bismarck, History & Holidays,Which Christmas carol contains the lines 'O Tidings of Comfort and Joy'' ,God Rest Ye Merry Gentlemen''  +Music,Name The Four Sections Of An Orchestra,"Strings, Wood Wind, Brass & Percussion",Food & Drink,Who wrote the book Charlie and the Chocolate Factory? ,Roald Dahl ,General,"The shortest war, between Britain & Zanzibar, happened in what year",1896 +General,Who led the revolt of roman slaves in A.D.73,Spartacus,Food & Drink,What Is Most Likely To Be Measured In A Hogshead ,Beer ,General,Ratatosk was what animal in Norse mythology - relayed insults,Squirrel +Food & Drink,What Kind Of Fruit Is A Canteloupe? ,Melon ,General,50% of Dutch men have never done what,Flown in a plane 28% fear it,General,What does Peter Pan call Captain Hook,Cod fish + History & Holidays,"Who plays the role of the Prime Minister, in the 2003 Christmas film 'Love Actually'' ",Hugh Grant ,General,What company made PacMan?,Bally Midway,General,Botany & zoology combined make up the science of _______.,Biology +General,What causes the tangy smell at the seaside,Rotting Seaweed,General,What ship's survivors were rescued by the liner Carpathia,Titanic,Music,What Was The Name Of The Club In Liverpool Where The Beatles First Performed,The Cavern Club +General,Lewis on what japanese city was the second atomic bomb dropped during world war ii,Nagasaki,Sports & Leisure,How Much Is The Yellow Ball Worth In A Game Of Snooker ,2 ,General,What plant family (strictly genus) do azaleas belong to,Rhododendron +Food & Drink,What is the most widely used seasoning?,Salt,General,What is the fear of the dark or night known as,Nyctophobia,General,In which city is the distinctive building of the saddledome,Calgary +General,"Town in the west bank, near Jerusalem, controlled since 1967 by Israel though administered since 1995 by the Palestinian authority",Bethlehem,Science & Nature,What part of a rhubarb plant is poisonous?,The Leaves,General,What is the flower that stands for: dejection,Lichen +Music,Name The Former Beatles Bass Player Who Died In 1962,Stewart Sutcliffe,General,Who Supplied The Voice To The Queen In The Movie Shrek 2,Julie Andrews,General,What numerical computer language doesn't have pointer or recursion,Fortran +General,Second city: Milwaukee (state),Madison,General,Ouagadougou is the capital of ______,Burkino faso,General,What is the common name for lysergic acid diethylamide?,LSD +Music,"Who Had A Hit In 1994 With ""Shaker Maker""",Oasis,General,What was the first film given the title Blockbuster,Jaws,General,A person living in solitude,Hermit +General,Who has written many books about Jack Aubrey and Stephen Maturin,Patrick o'brian,General,What word of greek origin means 'a general pardon',Amnesty,General,Florinzel is a character in which Shakespeare play,The Winters Tale +General,What degree is 'dds',Doctor of dental surgery,General,A Paris grocer was jailed for two years in 1978 stabbing wife what,A wedge of hard cheese,General,What is the flower that stands for: retaliation,Scotch thistle +Geography,What is the capital of Guatemala,Guatemala,General,Who wore 2 shirts at his execution so people would not think him afraid if he shivered,Charles i,General,Mintonette was the original name of what sport in 1891,Volleyball +General,What was the full name of Mangum P.I.?,Thomas Magnum,General,"What are noctilucent, cirrus, and cirrostratus categories of ?",Clouds,General,Fruit preserved in sugar,Glace +General,Who was the greek god of prophecy and archery,Apollo,General,What Was The First Science Fiction Movie To Cost Over A Million Dollars To Make,Forbidden Planet,General,Softball began being played in Chicago in what year,1888 +General,Edward de Bono - Maltese Doctor - developed what concept,Lateral Thinking,General,What Type Of Foodstuff Is Used In The Construction Of “ Dynamite,Peanuts,General,An alloy of Iron - Chromium and Nickel makes what,Stainless Steel +General,What male name comes from Greek meaning lover of horses,Phillip,General,What does most lipstick contain,Fish scales,General,"Fill in the blank: if ___________ was shorter, the face of the world would be changed",Cleopatras nose +General,What number does VII mean in roman numerals,Seven,General,"Alberta's shield on the coat of arms, bears the cross of",Saint george, Geography,What divides the American North from the South?,The Mason-Dixon Line +General,Frank Vos Bob Seagren Wolfgang Nordwig all held what,Pole vault record,General,Who played in the film 'ragtime' after 20 years offscreen,James cagney,General,Ian Fleming's house was called Goldeneye - which country,Jamaica +Music,Which Pianist Joined The Chicago Symphony Orchestra When He Was Just Nine Years Old,Liberace, History & Holidays,Who was the eighties group that was named after the inventor of the radio? ,Tesla ,General,A Paralian always lives near what,The Sea +General,Hydrosis is the medical term for what,Sweating,General,Which religions various branches claims 100% of Afghanistans population,Islam,General, What is the study of prehistoric plants and animals called,Paleontology +General,What is the word for 'chewing the cud',Ruminant,General,Which European city is served by Fiumicino airport,Rome,Music,"His Fourth Chart Sucess When Was ""Crocodile Rock"" A Hit For Elton John",1972 + Geography,What is the capital of Comoros ?,Moroni,Music,"Which Techno Artists Albums Include Accelerator, Lifeforms & Dead Cities",Future Sound Of London, Geography,What is the basic unit of currency for Mozambique ?,Metical +Music,"Name The Artist Who Had A Hit With The Song ""Papa's Got A Braned New Pigbag""",Pigbag,General,Indian clarified butter,Ghee,General,Wilhelm Beer and Johan von Madler first good map where 1830,The Moon +General,In what movie did Charlie Chaplin first speak on film?,The Great Dictator,Music,"Bill Clinton Used Which Group's ""Don't Stop Thinking About Tomorrrow"" As His Presidential Campaign Song?",Fleetwood Mac,General,Which car company makes the Almera,Nissan +General,What is dittology,Double meaning,General,Who is considered the doctor of medicine,Hippocrates,General,What was pirate Captain Kidd's first name,William + History & Holidays,"Those big black CD's that you see at garage sales that people call ""albums"" are made of _____.",Vinyl,General,H14 N2 is a poisonous alkaloid consumed daily by millions what,Nicotine,Music,Roger Taylor Played The Drums For Which Famous Group,Queen +General,Who was assassinated by her own bodyguard in 1984,Indira Ghandi,General,A square of rectangular area in a church between the apse and the crossing. ,Choir,General,Classical seven ages of man Infant Schoolboy Lover what's next,Soldier +General,"Wild flowers: what's blue, comes out in spring, and smells like hyacinth",Bluebells,Science & Nature,Which 2 Islands Are The Natural Habitat Of The Orang-Utan? ,Borneo & Sumatra ,Science & Nature, Cows provide 90 percent of the world's __________,Milk +General,What is the flower that stands for: bravery,Oak leaves,Music,Whose Rival Was Two Ton Ted From Teddington,Ernie (Fastest Milkman In West),General,In what HG Wells novel does Dr Griffin sometimes appear,The Invisible Man +Entertainment,Who sang 'Good Morning To You?,Mildred and Patty Hill,Art & Literature,"From which of Shakespeare's plays is this line: ""All the world's a stage___""",As You Like It,General,What does a gozzard have or own,Geese +General,What is Greece's second city after Athens,Selonika,General,Book of Shadows is the alternative title for which horror film,Blair Witch 2,General,Sean Mercer Was Jailed For 22 Years In 2008 AQfter Being Found Guilty Of What Crime,He Murdered Rhys Jones +Food & Drink,Which Meat Dish Was Named After A 19th Century Russian Count ,Beef Stroganoff ,General,"Who plays Captain Jean Luc Picard in the series Star Trek, The Next Generation",Patrick stewart,General,What is the flower that stands for: cheerfullness in old age,American starwort +Music,Who Were Bobby Hatfield & Bill Medley?,The Righteous Bros,General,Who is the historical figure most often portrayed in movies,Napoleon bonaparte,General,What animals name literally translates as earth pig,Aardvark + History & Holidays,December 26th is traditionally known as Boxing Day but it is also which Saints Holy Day ,Saint Stephen ,General,Why do lawyers traditionally wear black,Mourning Queen Mary Scotland,General,The treatment of disease by chemical substances which are toxic to the causative micro organisms is called ________,Chemotherapy +General,Petroselinum crispum is the Latin name of which herb,Parsley,General,Capital cities: Vanuatu,Port vila,Science & Nature,What Animal Is A Skua? ,A Bird  +General,"Who are the three annoying neighbors in ""Small Wonder""?","Brandon,Bonnie,and Harriet Brendel",General,"In Which Country Might The Jets Play The Mariners, & The Glory Take On The Roar At Football?",Australia,General,In what Australian state would you find Albury,New south wales nsw +Geography,On Which River Does New Orleans Stand ,The Mississippi ,General,What bird has the longest fledging period 360 days,King Penguin, History & Holidays,What 80's game show featured the 'Whammy'? ,Press Your Luck  +General,"In the film 'hackers', how old was 'zero_kool' when he was first arrested",Eleven,General,What was the first TV theme song to hit number one US charts,Davy Crocket,Science & Nature," A donkey is an ""ass"", but an ass is not always a donkey. The word ""ass"" refers to several hoofed mammals of the genus Equus, including the __________",Onager +General,What is a skin specialist called,Dermatologist,General,What was john lennon's original middle name,Winston,General,What Was The Name Of The Fifa 1994 World Cup Mascot?,Striker + History & Holidays,What did jack the ripper sign on his first note ,Yours truly ,General,"In art, what name is given to a quick sketch for a larger painting",Cartoon,General,Which small republic is situated on the slopes of Monte Titano in Italy,San marino +General,In which city is the Encyclopaedia Britannica based and published today,Chicago,General,What group were once called The Warlocks,The Grateful Dead,General,What nationality was Cleopatra,Greek +General, Gymnophobia is the fear of __________.,Naked bodies,Music,Which group had a number 1 in 1966 with 'Keep on Running'?,The Spencer Davis Group,General,"In Greek mythology, from where was a beam hewn on atlanta's ship",Speaking +General,The Dove awards are presented annually for what,Gospel music,General,What is the highest mountain in the Casacde range?,Mount Rainier,General,Hydrophobophobia is the fear of,Rabies +General,What city do the italians call the monaco of bavaria,Munich,General,On which planet would you find the Caloris Basin,Mercury,General,What angle do 90 degrees make,Right angle +General,What's the most popular name for a male pet cat,Tiger,Music,"Who Had A Hit With ""Heaven Is A Place On Earth""",Belinda Carlisle,General,The Taj Mahal was built on the southern bank of which river,Jumna +Geography,What is the capital of Ghana,Accra,General,Which one of the three tenors is not spanish,Pavarotti,Geography,Name the capital city of rhode island. ,Providence  +General,What african city is built on gold,Johannesburg,Entertainment,"When not fighting crime, what did Underdog do for a living",Shoeshine boy,General,Percy LeBaron Spencer invented what in 1945 in USA,Microwave Oven +General,Decoration of food,Garnish,General,What is the most important industry in Alaska,Fishing,General,What is the name of Shakespeare's first play,Titus Andronicus +General,Rita Kuti Kis represented Hungary in what sport,Tennis, History & Holidays,What Christmas item was first introduced by Henry Cole in 1843 ,The Christmas Card ,Science & Nature,Name A Type Of Plague Other Than Bubonic ,Pneumonic  +General,"In 1985, as what did 'people magazine' name Mel Gibson",Sexiest man of the year,General,What Prophet in the Bible had a talking donkey,Balaam,General,What novel recounts john blackthorne's adventures in 16th century japan,Shogun +General,Where did the judds spend years shopping demos recorded on a cassette recorder,"Nashville, tennessee", History & Holidays,13th July 1955 saw the last execution of woman in Britain - who was she ,Ruth Ellis ,Toys & Games,How much is the luxury tax (in dollars) in Monopoly,75 +General,Who in books and films was the man of bronze,Doc Savage,General,What is the flower that stands for: separation,Carolina jasmine,General,The brothers Don and Phil formed which singing group,Everly brothers + History & Holidays,"Neptune Was The Roman God Of The Oceans, What Name Did The Greeks Use? ",Poseidon ,General,What two ingredients make the dish angels on horseback,Oysters - wrapped in Bacon,General,Crystal City in Texas put up a statue to what cartoon character,Popeye - they are spinach growers +Science & Nature,How Long Does A May Fly Live? ,1 Day/ 24 Hours ,General,What is the capital of arizona,Phoenix,Music,"Duran Duran Were ""Hungry Like The What""",Wolf +General,Who was European correspondent New York Tribune 1851/62,Karl Marx,Music,"Oh is he more, too much more than a pretty face, It's so strange the way he talk, it's a disgrace, Well I know I've been out of style, For a short while, But I don't care how cold you are” From which 1970’s song?",David Essex / Gonna Make You A Star,Entertainment,"This was the sequel to ""Star Wars"".",The Empire Strikes Back + History & Holidays,What was Thailand formerly known as?,Siam,General,What is the Capital of: Jordan,Amman,General,"By 1983, 13 million of what ""labor-saving"" device operated worldwide",Computers +General,In which game is gruntilda the witch,Banjo-kazooie,General,Which modern country occupies the former Roman province of Lusitania,Portugal,General,What city has the most underground stations in the world?,New York +General,In The TV Show Different Strokes What Was The Name Of Arnolds Goldfish,Abraham,General,"In Dr Who, what was Tardis an abbreviation of ?",Time and relative dimensions in space,General,Name the toy that consisted of color pencils and plastic which you would put in the oven to create.,Shrinky Dinks +General,Who was the first American to make $100 million a year,Al Capone,Toys & Games,Name of Barbie's hot pink sports_car.,Vette, History & Holidays,"Before she was a pop star, why was Samantha Fox in the British tabloids? ",She was a page three girl  +General,What Nationality Would You Be If You Spoke Inukitut,Canadian,General,Who did caesar and cleo become,Sonny and cher,General,Victor Buono played what Batman villain in the original series,King Tut +General,"What is the federal district in southeastern Australia, bordered by the state of New South Wales",Australian capital territory,General,"In Mathematics, the Greek capital letter ""sigma"" is used to denote what process",The sum of (summation),General,The agen plum was the basis of what giant u.s industry,Prunes +General,To what country would a hiker go to assail mt ararat,Turkey,General,"What sport links Castle Cup, Red Stripe Cup, Ranji Trophy",Cricket,General,"Which spiritual song, made famous by Joan Baez, became the anthem for American Civil Rights movement in the 1960s",We shall overcome +General,Madame Pauline de Vere first female circus performer - do what,Head in Lions Mouth,General,"In Digimon,what do T.K. and Kari have that the other children don't?",Older brothers,General,The continental title baron is equal to which UK title,Earl +General,How many miles can a Pershing missile travel,400,General,What is the basis of the dish 'Hummus',Chick peas,General,What German city was the site of the war crime trials following WWII,Nuremberg +General,What can keep for up to 4 years if stored in a cool dark place,Standard Condoms,General,"An English folk dance that appeared in the fifteenth century, in which dancers wore bells on their legs and characters included a fool, a boy on a hobby horse, and a main in blackface.",Morris dance, History & Holidays,Which war drama was first seen on British Television in October 1972? ,Colditz  +General,Whose final opera was called Death in Venice,Benjamin Britain,General,What is the kitchen on a ship called,Galley,General,What American city is prone to strong winds,Chicago +Music,"A Holland Park Brothers Track, Name The 1981 UK Single From The Album ""Forever Michael""",Were Almost There,General,In which alcoholic spirit might you find a worm in the bottle,Tequila,General,How many names did the first telephone book ever issued contain,Fifty +General,"Which British physician, 1749 to 1823, developed the first effective vaccine against smallpox",Edward jenner,Geography,What city boasts the Copacabana Beach and Impanema,Rio,General,In California you can't legally buy a mousetrap without what,Hunting Licence +General,Who invented the first flush toilet,Thomas crapper,General,How many days where there in 1976?,366,General,"What name is given to a fibre from the husk of a coconut, used in rope making & matting",Coir +General,What is an instrument for indicating the depth of the sea beneath a moving vessel called,Bathometer,General,Which group had a British number one hit in the 1970s with I Don't Like Mondays,Boomtown rats,General,What takes a human 43 muscles to do,Frown +General,Who was 'uncle milty',Milton berle,General,What is located at 350 fifth avenue in New York City,Empire state building,General,What does GP mean on a music score when all players silent,General Pause +General,Who said sex is a bad thing it rumples the clothes,Jacqueline Kennedy Onassis,General,In the Old Testament what book comes between Obadiah - Micah,Jonah,General,Who directed Star Trek films 3 and 4,Leonard Nimoy +General,Each body cell contains how many chromosomes,46,Music,"Who had a 50's hit with 'To know him, is to love him' in 1958?",The Teddy Bears,Art & Literature,Who Wrote (Gone With The Wind) ,Margaret Mitchell  +General,Name the Apostle who replaced Judas Iscariot,Matthias,General,As what was sony's video recorder known,Beta-max beta max beta, Geography,What is the monetary unit of India?,Rupee +General,What was the name of the Monkeys only film made in 1969,Head, Geography,Meridians converge at the ________.,Poles,General,Which marsupial is native to North America,Opossum +Sports & Leisure,From Which Football Club Did Manchester United Sign Roy Keane In 1993? ,Nottingham Forest ,General,In what organ of the body is insulin produced,Pancreas,General,Which Steven Spielbereg film pits Dennis Weaver against a truck with an unseen driver,Duel +General,How many hours does an antelope sleep at night,One,General,Who succeeded Brezhnev as Soviet leader,Yuri andropov,Music,Who Were The 2 Members Of The Pop Duo Wham,George Michael & Andrew Ridgely +General,A Capriphiliac has sex with who or what,Goats,Science & Nature,Which Famous Series Of Aircraft Was Designed By Artem Mikoyan & Mikhail Gurevich ,MiG ,General,What is the common term for the condition monochromatism,Colour blindness +Entertainment,"Who plays the character of the only escapee from Alcatraz in the movie ""The Rock""?",Sean Connery,General,What was Hitchcock's first film in colour,Rope,General,What was the name of the baby in Three Men and a Baby,Mary +General,"Who sang the theme song to the ""Breakfast Club""?",Simple Minds,General,Who was nicknamed The Bronx Bull,Jake LaMotta,Music,"Which Singing Cowboy Was Born Leonard Slye , & Whose Early Career Was As Part Of The Sons & Pioneers Group",Roy Rogers +General,"Syd Barett, Roger Waters, Richard Wright, Nick Mason - Group",Pink Floyd,General,What is a spat,Baby oyster,General,"What did Emerson, Lake and Palmer burn on stage during their concerts",The +General,"Kitty, fire, draw and tuck in are terms in what sport",Green Bowls,General,Who was the first honorary US citizen,Winston Churchill,Science & Nature,Which sight problem occurs in men far more then in women ?,Colour Blindness +General,A Mai Tai is a cocktail literally meaning what in Tahitian,Out of the World,General,"Republic in southern central America, bounded on the north by Nicaragua, on the east by the Caribbean Sea, on the southeast by Panama, & on the southwest & west by the Pacific Ocean",Costa rica,Music,What Was Buddy Holly’s Real Christian Name,Charles +General,"Born Georgios Panayiotou in England in 1963, by what name is this singer better known",George michael, History & Holidays,Messala was the gad guy in which 50s blockbuster ? ,Ben Hur ,Toys & Games,"What number is on the opposite side of the ""five"" on dice?",Two +General,In what US city do they watch the most TV evangelists per cap,Washington DC, History & Holidays,Who tried to create the 'Great Society'?,Lyndon B Johnson,Science & Nature,What Is The Hardest Substance In The Body ,Tooth Enamel  +General,Heinrich Schliemann archaeologist famous for excavating where,Troy,General,What tv series from 1970-1974 starred susan dey,Partridge family,Science & Nature, The average __________ moves at a rate of approximately 0.000362005 miles per hour.,Snail +General,"In ballet, a position with the body at an oblique angle and the working leg crossing the line of the body.",Croisée,General,"What two countries were known as ""the yellow peril"" in the 1890's",China & japan japan & china,General,Which was the first of the National Parks in England and Wales,Peak district +General,Chloe the girls name means what,Green shoot,General,What is the medical term for word blindness,Dyslexia,Religion & Mythology,Who is the greek equivalent of the roman god Minerva,Athena +Science & Nature,Which Is The Second Heaviest Land Animal? ,The Hippopotamus ,General,Which animal is known as 'mouton' to the French and 'schaf' to the Germans,Sheep,General,Dead mans hand was Aces and Eights plus which other card,Queen of Hearts +General,Who was the first person to wear a baseball glove,David,General,Who was the first head of an arab nation to make peace with israel,Anwar, History & Holidays,Who is the first ghost seen by Scrooge in 'A Christmas Carol'' ,Jacob Marley  +General,What were the names of the first so-called 'Siamese Twins',Chang and eng,General,"What is the scientific name for a ""spy in the cab""",Tachograph,Sports & Leisure,In Which Sport Was Desmond Douglas Britain's Leading Competitor ,Table Tennis  +Science & Nature," Surviving all dangers, a wild __________ may live up to 20 years.",Cobra,General,In which European Palace are the State Apartments called the Hall of Mirrors,Versailles,General,What is the Capital of: Andorra,Andorra la vella +Music,Guess The Band From These Initials AFL / AF / BA / BU,Abba,General,What colour are the seats in the House of Lords,Red - Commons green, Geography,What south American country has both a Pacific and Atlantic coastline?,Colombia +Music,What Reason Did Peter Criss Give For Leaving Kiss In 1980,The Makeup Was Ruining His Complexion,General,What did wham! say to do before you go go,Wake me up,General,What greek phrase means 'the masses',Hoi polloi +Science & Nature,A phrenologist reads _________.,Skulls,Science & Nature,What is a male swan called?,Cob,Music,Which Hugely Successful 90’s Pop Duo Consisted Of “Marie Fredriksson And Per Gessle”,Roxette +General,In which UK city is the 'City Palace of Varieties'?,Leeds,General,"Which group starred with Oliver Reed in the film ""Tommy""",The who,Science & Nature," The world's largest rodent is the Capybara. An Amazon water hog that looks like a guinea pig, it can weigh more than __________ pounds.",100 +General,Poinephobia is the fear of,Punishment,Sports & Leisure,Who did Stephen Hendry replace as World No1 in the 1989-90 Snooker season? ,Steve Davis ,General,Who is the oldest female to win best actress oscar?,Jessica Tandy +General,What is the only counties national flag different both sides,Paraguay,Science & Nature,What metal is the major constituent of Rubies ?,Aluminium,General,What is a young duck called,Duckling +General,In which classic Western film did the character Will Kane appear,High noon,Geography,The name of which Central American country means The Saviour in English? ,El Salvador ,Entertainment,Hey! What was the name of the hit song released by 'The Romantics' in February 1980?,That's What I Like About You +General,"What year was the bacall-bogart movie ""the guys from milwaukee"" released",1946,General,What French blue cheese (similar to stilton) is made ewes milk,Roquefort,General,What Latin word means elsewhere,Alibi +Sports & Leisure,Who Won 7 gold Medals In Swimming At The 1972 Olympics ,Mark Spitz ,Art & Literature,In The (Rhyme Of The Ancient Mariner) Which Bird Is Shot ,An Albatross , History & Holidays,Who was the female star of in the tv version of 'Bewitched' ,Elizabeth Montgomery  +General,Which people used to settle legal disputes by head butting,Inuit - Eskimo,Music,"Who Wrote And Produced Minnie Rippertons No.1 Hit ""Loving You""",Stevie Wonder,Religion & Mythology,Which two books in the Old Testament list the ten commandments? (in order of appearance),Exodus and Deuteronomy +General,It came into football in 1923 men say women don’t understand?,The offside rule,General,In Massachusetts what's illegal unless bedroom window locked,Snoring,General,Which gangster escaped from jail using a wooden gun,John Dillinger +General,What type of food is Taramasalata,Cured /smoked cod roe,Geography,Which city in New Zealand is known both as the City of the Plains and the Garden City is it Wellington or Christchurch? ,Christchurch ,Science & Nature,"What is a small, flightless bird native to New Zealand?",Kiwi +Music,What Do You Call The Lines Upon Which Musical Notes Are Written,Staves,General,Who plays the boy in the film 'Billy Elliot',Jamie bell,General,"What three letters are overly used to indicate ""Laugh Out Loud""?",Lol +General,Where will children as young as 15 be jailed for cheating on their finals?,Bangladesh,General,Who invented the cash register in 1879,James ritty,General,What name is given to the socket in the skull which holds the eye,Orbit +Entertainment,Nick Nolte and Eddie Murphy star in this 1982 film.,48 Hours, History & Holidays,In The Movie 'Interview With The Vampire' Who Played The Vampire Being Interviewed ,Brad Pitt ,General,The Greeks used hexagonal or triangular ones of burnt clay what,Coffins +General,More Deaths Occur Each Year Participating In This Sport Than Any Other Sport What Is It?,Fishing,General,Roberts in which film was goldie hawn the body double for julia roberts,Pretty woman,General,What is the german parliament,Bundestag +General,Who was appointed Headmaster of Rugby School in 1828,Thomas arnold,General,The density of what is measured on the Rngelmann scale,Smoke,General,"What is the discharge of a liquid from a surface, usually pores or incisions",Exudation +Food & Drink,Name for an oblong cream puff filled and topped with icing.,Eclair,Music,James P Johnson Wrote The Signature To Which 1920's Dance Craze,The Charleston,Geography,The standard single oar used by gondoliers in ____________ is 14 feet long.,Venice +General,Nostophobia is the fear of,Returning home, Geography,In what city are the famous Tivoli Gardens?,Copenhagen,General,What was the name of the horse in Animal Farm,Boxer +General,Who was the losing Democratic candidate in the 1968 U.S. Presidential Election,Hubert humphrey,General,What is the fear of shellfish known as,Ostraconophobia,Food & Drink, What colour is the flesh of an avocado?,Green +General,"In norse mythology, who is the chief of the valkyries",Brunhilda,General,Who was the ponderosa's chinese cook,Hop sing,General,Where is mauna kea,Hawaii +Food & Drink,Who released the following 'edible' album 'Tigermilk' ,Belle and Sebastian ,General,What did the House of Saxe-Coburg-Gotha change it's name to ?,House of Windsor, Geography,Port Moresby is the capital of ______?,Papua New Guinea +General,Masked pantomime character in diamond patterned costume,Harlequin,Music,Was Silence Is Golden Credited To Brian Poole & The Tremeloes Or Just The Tremeloes,The Tremeloes,General,The Camorra was the forerunner of what organisation in the USA,The Mafia +Music,Tears On My Pillow Was A Hit In 1990 But Who Was The Singer,Kylie Minogue,General,The Black Death came to England from what port,Calais,General,"Rats, mice, beavers, and squirrels are all _______.",Rodents +General,Who played kevin hathaway on the soapie 'days of our lives',Pat sajak,General,Which song is performed at start Indiana Jones Temple of Doom,Anything Goes,General,"Jefferson what can be tulip, balloon or flute",Wine glasses +General,The Indian name Singh translates as what,Lion,Sports & Leisure,How Many Players Are There In A Volleyball Team ,6 Players ,General,What is a naevus (nevus),Birthmark +General,What was the name of the submarine which sank the General Belgrano during the Falklands conflict,Hms conqueror,General,What was the first auto part completely designed by a computer,Cadillac,General,What news anchor attended Reagan High School,Dan rather +Entertainment,What is the name of the whale that swallowed Pinocchio.,Monstro,Music,What Was Alice Coopers Biggest Hit Of The 80's,Poison,General,"Bread, cereals, fruit and vegetables are needed by the body as good sources of what",Fibre +General,What would you do with a hecklephone,Play it - type of woodwind,General,Havana is the capital of ______,Cuba,General,"Anvil, hammer and stirrup are all bones where",Ear +General,"Elizabeth I of England suffered from anthophobia, what is it",Fear of roses,General,What is the english word for 'zorro',Fox,General,"In a classical building, the triangular gable between the horizontal entablature and the sloping roof; in general, and architechtural feature over a door or window.",Pediment +General,In Dallas what was the name of the bar,Cattleman's Club,General,Ulan bator is the capital of ______,Mongolia, History & Holidays,Why is the day after Christmas named Boxing Day? ,The Rich Gave Presents To The Poor  +Music,In music what term is used to describe 3 or more notes played simultaneously?,Chord,General,"Who Originally Said Of Gerald Ford That ""He Was So Dumb He Couldn't Fart And Chew Gum At The Same Time""",Lyndon B Johnson,Geography,What is the capital of Nicaragua,Managua +General,What was the name of the police character played by Roy Scheider in the film Jaws,Martin brody,General,What new york thoroughfare is known as 'millionaires' row',Fifth avenue,Sports & Leisure,What was the Australian John Landy the second person to achieve? ,A Sub Four-Minute Mile  +General,Which musician of non-U.K. citizenship was awarded an honorary knighthood (KBE) in 1986,Bob geldof,General,Kevin Kline won best supporting actor Oscar which 1988 film,A Fish Called Wanda,Sports & Leisure,How many pounds does the Olympic hammer weigh? ,16  +Religion & Mythology,"Though the touch of gold was removed, Midas was forever cursed by Athena to have the ears of which animal?",Donkey,Entertainment,Secret Identities: Kirk Morrison,King mob,General,"What was the blue dye, used by ancient Britons to colour their skin, called",Woad +General,Who was born sarah jane fulks,Jane wyman reagan,Science & Nature,How Often Does Bamboo Bloom? ,Once Every 120 Years ,General,In the Phantom of the Opera how does the Phantom sign notes,OG - Opera Ghost +Music,Who Was The Midnight Rider,Paul Davidson,General,Who invented the windmill,Arabs, Geography,What is the worlds longest concrete dam?,Grand Coulee Dam +Music,"How Many Christian Brothers Were Originally In The Group ""The Christians""",Three,Music,"Who Sang ""Hazy Shade Of Winter"" From The Film ""Too Low For Zero""",The Bangles,Geography,"The nation of _______________, located in the Atlantic Ocean 450 miles west of the western tip of Africa, has no minerals except salt and pozzolana.",Cape verde +General,What does the 'x' mean when referring to the speed of a cd-rom (ie.. 32x),Times faster than a single speed,General,With what is charcoal and sulphur mixed to make gunpowder,Saltpetre,General,What Canadian broadcaster created the radio character old Rawhide,Max ferguson +Geography,Which is the least populated state in the USA? ,Wyoming ,General,Romanian soccer team wich won the European Champions League in 1986(in final with CF Barcelona),Steaua bucuresti,Science & Nature, Australia's __________ is the world's most dangerous jellyfish. Its toxin is more potent than cobra venom and can kill a person in minutes,Box jelly +General,How many olypmic medals has mark spitz won,Nine,General,"Which tennis player , when asked if he had learned anything from his US Open loss said, ""Yeah___I learned I needed to lose 15 pounds""?",Andre Agassi,General,"First emperor of Rome (27bc-14ad), who restored unity and orderly government to the realm after nearly a century of civil wars.",Augustus +General,What is measured on the Mohs scale,Hardness of minerals,General,What could an Australian win a Stanley for,Cartooning,Sports & Leisure,How many stitches are on a regulation baseball?,108 +General,Which drug can be extracted from the bark of the cinchona tree,Quinine,General,Who is the only host country not to win gold at its own summer olympics,Canada,General,The largest University is in which country??,"Paris, France" +General,Who wrote 'shogun',James clavell,General,"In common: see, carpet, hot, cent",Red,Science & Nature,Which alloy is made with nine parts tin and one part lead? ,Pewter  +General,What Is The Name Of Postman Pats Son?,Julian,General,In the UK what was the first product advertised on TV in colour,Birds Eye Peas,General,The variety of living organisms in a particular habitat or geographic area,Biodiversity +General,Where was Hawkeye Pierce's hometown in the show M,A S H ? Crab Apple Cove, Geography,What is the basic unit of currency for Spain ?,Peseta,General,Time Magazine named what as the Man of the Year 1982,The Computer +General,By what name is the skin complaint of 'comedo' better known,Blackheads,General,Furritus Latin for little thieves is the name for which creature,Ferrets,Music,Which band featured Paul McGuigan on bass?,Oasis +Music,"In Which Comedy Film Does Aretha Franklin, Star As A Proprietress Of A Soul Food Joint Sing Her 1968 Hit Think",The Blues Brothers,General,What was mozart's middle name,Amadeus,General,US what was the last brand of cigarettes to be advertised on TV,Virginia Slims +General,What year was a U2 pilot shot down for spying,1960,Music,"Little Richard's Early Hits Were Recorded For A Los Angeles Record Label, Which One",Speciality,Sports & Leisure,In 1995 Who Did Blackburn Rovers Loose To On The Final Day Of The Season Yet Still Managed To Win the Premiership ,Liverpool  +General,Who prescribed marijuana for queen victoria's menstrual cramps,Her doctor,General,What is the literal English translation of the French phrase 'cordon bleu',Blue ribbon,Science & Nature,The Ishihara Test Is Used To Determine Whether Or Not Somebody Is What ,Colour Blind  +General,In Cornwall where would you find two legged knockers,Tin Mines goblins,General,Who is Hamlet's tragic suicidal girlfriend,Ophelia,General,Which international footballer was known as 'Black Panther',Eusebio +Food & Drink,What Is Pumpernickel ,A Type Of Bread , History & Holidays,What Is Worn Around The Neck To Keep Vampires Away ,Garlic ,Music,"On What Label Was ""Down Down"" A Hit For Status Quo",Phonogram +Music,Vincent Fernier Is More Commonly Known As Which Rock Star,Alice Cooper,Toys & Games,What score is not possible for a cribbage hand,19,Food & Drink,What did Charles Jung invent?,Fortune cookies +General,Who began his career with 'the yardbirds' and is established as one of the best rock guitarists of his generation,Eric clapton,General,White-out was invented by who's mother,Mike nesmith,General,Who won the Nobel prize in physics in 1921,Albert einstein +General,What are kreplach,Jewish ravioli,General,Which detective novelist caused a sensation by disappearing for ten days in 1926,Agatha christie,General,In which sci-fi novel do the Morlocks live underground,The time machine +General,What is alfred e neumans motto,What me worry,Entertainment,"Who were the rivals of the T-Birds in the movie ""Grease""?",Scorpions,General,What was the name of the short lived spin-off of Three's Company?,The Ropers +Sports & Leisure,Which Scottish Football Club Boasted The First All Seater Football Stadium In Britain ? ,Aberdeen , History & Holidays,Who Directed The 1968 Film Rosemary's Baby ,Roman Polanski ,General,"The easybeats released ""friday on my _____""",Mind +General,What links Herbert Hoover and Richard Nixon - not the obvious,Both Quakers,General,"Americans say ""shades"", but canadians say",Blinds,General,Who refused the leading male role in Gone With the Wind,Gary Cooper +General,What digit does not exist in Roman Numerals,Zero, History & Holidays,What was the first American colony to legalise witchcraft?,Pennsylvania,General,33% of worlds population can't do what apparently simple thing,Snap their fingers +General,Emperor Haile Selassie of Ethiopia was finally diposed in 1974 after how many years in power,Fifty eight,General,Who is the subject of Irving Stone's The Origin,Charles darwin,General,Which American state has Boise as its capital,Idaho +Science & Nature,How Was The Misubishi A6M Fighter Aircraft Known ,Zero ,Geography,The Town Of Timbuktu Is In Which African Country ,Mali ,General,"Which Shakespeare play features Cordelia, Regan, and Goneril",King lear +Geography,What is the capital of Ireland,Dublin,General,What was or is a Waltzing Mathilda,Swagman’s Knapsack,Entertainment,What was the name of Speed Racer's car,The mach five +General,Where do pilgrims go to march around the Kaaba and kiss The Black Stone,Mecca,General,100 zeros after the number 1 is a very very large number called what,Googol,Music,In 1965 Who Went To Maggies Farm,Bob Dylan +General,According to folklore a windy christmas means _____,Good fortune,General,What current popular sitcom star played Michael P. Keaton's girlfriend Lauren Miller on Family Ties?,Courteney Cox,General,In Roman times what were Falerian Setine Alban Sorrentine,Wine Regions +Science & Nature,Who Invented The Spinning Jenny In 1764 ,James Hargreaves ,General,"Ginger Baker, Eric Clapton and Jack Bruce were the line-up to which band",Cream,Entertainment,What kind of dog is Scooby Doo?,Great dane +General,Which English composer was born near Worcester in 1857 and died in 1934,Edward elgar, History & Holidays,Who Released The 70's Album Entitled The Pleasure Principle ,Gary Newman ,General,Who once had a job as a coffin polisher,Sean Connery +Science & Nature,"Which disease is also known as ""Hansen's Disease""?",Leprosy,General,On the Thames they go swan upping annually what is it,Counting swan population,General,"Which sandwich which is served hot, consists of Corned Beef, Thousand Island dressing, Sauerkraut and Cheese?",Reuben Sandwich +General,Excluding the what word appears most in Bond film titles,Never,Entertainment,What type of plant does Broom Hilda sell?,Venus flytrap,People & Places,Whose Catchphraase Is 'Nice To See You To See You Nice' ,Bruce Forsythe  +General,Not obvious colours - what links orange silver purple,Can't rhyme in English,General,District of ancient Greece on the northern coast of the Gulf of Corinth,Aetolia,General,Wilma Rudolf first black US woman to win Olympic gold where,Rome in 1960 (3) +General,French revolution what did the prisoners travel to the guillotine in,Tumbrels,Geography,Name the capital of italy. ,Rome ,General,"A horse named Nita beat the first locomotive ever built in America in a famous race in 1830, what was the name of the train",Tom Thumb +General,How Are The Pop Duo Ben Watt And Tracy Thorn Collectively Known,Everything But The Girl,General,Where was the monty python film 'the life of brian' banned,Scotland,General,"What pop group said ""Were only in it for the volume""",Black Sabbath +General,What did Brian Epstein manage before the Beatles,A Record Shop,General,What author wrote the books Rage and The Sunbird,Wilber Smith, History & Holidays,Who Was The Only British Pope? ,Adrian IV  +Geography,Tourists who are eager to visit recently erupted volcanoes while on vacation should take heed. Volcanic ash has been known to remain hot for a period of nearly ______________,100 years,Music,What Is The Name Of Michael Jacksons Ranch,Neverland,Music,Who Recorded The Original Version Of Soft Cell's Tainted Love,Gloria Jones +General,In which form of Japanese theatre are all roles played by men,Kabuki,General,What country has the most Post Offices,India, Geography,What is the basic unit of currency for Saint Lucia ?,Dollar +General,Which cartoon character has a girlfriend named Petunia,Porky Pig,General,"Name the detective in My gun is Quick, Murder is my Business",Mike Hammer – Mickey Spillane, History & Holidays,The first dog in space was named _________.,Laika +General,"On the show Cheers,What was the name of the restaurant above the bar.",Melvilles,General,Who voices the female hyena in the lion king,Whoopee Goldberg,General,In which magazine did sarah ferguson make the 'worst dressed list' five times,People's magazine +General,Where is the base city for Porsche cars,Stuttgart Germany,Entertainment,What was Eddie Murphy's character name in 'Beverley Hills Cop'?,Axel Foley,Science & Nature,How Many Humps Has A Dromedary Got? ,One Hump  +Science & Nature,The fins of which fish are made into a soup?,Shark,General,"What, apart from air & fuel, is filtered in a car",Oil,Music,What Did Voice Of The Beehive Ask Us Not To Do In 1988,Don't Call Me Baby +General,What did Sally Rogers always wear in her hair?,A bow,Food & Drink,What Banned Substance Was Included In The Ingredients Of Coca Cola Until 1903 ,Cocaine ,General,"What do ensign, cadet, osprey and 505 have in common",Sailing Dinghy's +General,Two English versions of the 'Bridge of Sighs' are located in which two English cities?,Oxford and Cambridge,General,Which is the language that is most spoken in the world?,Chinese,General,In Greek mythology who was moirae's mother,Themes +General,What was gangster charles floyd's nickname,Pretty boy,General,The giant squid has the largest ____ in the world,Eyes,Science & Nature,Which company introduced the first 'instamatic' camera in 1963? ,Kodak  +General,"In order for a deck of cards to be mixed up enough to play with properly, at least how many times should it be shuffled",Seven times,Music,Name Pavarotti's 1990 Winner,Nessun Dorma,Geography,"Which American state's name is Spanish for ""colored"" or ""colored land""?",Colorado +Music,A Year Later It Was A Huge Top Ten Hit But Which INXS Single Only Got To No 58 In 1987,Need You Tonight,General,What did Percy Shaw invent,Cat's eyes,General,Which artist painted sixty two self portraits,Rembrandt Van Rinn +General,What is the young of this animal called: Tiger,Cub whelp,General,Do ahashya da is Navaho for what,I am Stupid,Geography,Where in Britain is Ronaldsway Airport? ,The Isle Of Man  +General,What sci-fi tv show of the 60's became a Filmation cartoon in the 70's?,Star Trek,General,A steep rugged rock,Crag,Entertainment,Singing without instrumental back up is called what?,Capella +People & Places,Who Was The Joan Collins Fan Club? ,Julian Clarey ,General,Dionysus was the greek god of ______,Wine,General,What Is The Name Of This Mr Man Character,Mr Spendy +General,In what sport might you see a stem-christie,Skiing,Music,Who Had A Hit With The Song “Vindaloo” in 1998?,Fat Les,General,What is the name of the Kellogg's cereal prefixed with the word 'healthwise',Bran flakes +General,Who was the youngest ever American President?,Theodore Roosevelt ,General,"In German Romanesque, a monumental entrance to a church consisting of towers, with a chapel above.",Westwork,General,Who was Prime Minister of Japan during World War 2,Hideki tojo +Science & Nature,A great wave resulting from an earthquakes is called a (an) ________.,Tsunami,General,What is pompoir,Vaginal muscle control,Music,From which country does black box originate from?,Italy +Science & Nature, Sir Walter Raleigh's black greyhound was named __________,Hamlet,General,"In The World Of Entertainment How Is ""Sylvester Sneakley"" More Commonly Known",The Hooded Claw,General,What Apple Computer cofounder sponsored the U.S. Festival,Steve wozniak +General,Azote is the old name for what element,Nitrogen,General,Who played Maria in the film West Side Story,Natalie wood,General,What is the Capital of: Morocco,Rabat +General,"Who said in 1951 - ""I married beneath me - All women do""",Lady Nancy Astor,Music,"Which Us Female Vocalist Reached The Top Ten By Saying ""Yes My Darling Daughter""",Eydie Gorme,General,What was the name of Garfield's teddy bear?,Pookie +Food & Drink,In which countries would you find the following city 'Cayenne'? ,French Guyana ,Science & Nature,Who Ran Naked Through The Street Shouting Eureka ,Archimedes ,General,Who is on the most popular us commemorative stamp,Elvis +Science & Nature," You could milk about __________ cows per hour by hand, but with modern machinery, you can milk up to 100 cows per hour.",6,General,Handel's Harmonious Blacksmith was written for what instrument,Harpsichord,General,Fonzarelli the swiss family _______,Robinson +General,What U.S. state includes the telephone area code 816,Missouri, History & Holidays,In The Charles Dickens Novel 'A Chrismas Carol' Who Was Scrooges Dead Business Partner ,Jacob Marley ,Art & Literature,"Ground chalk or plaster mixed with glue, used as a base coat for tempera and oil painting. ",Gesso +General,What is the best selling single artist ever,Garth brooks,Music,"Which Artist Along With His All Stars, Had A Hit With ""Im A Road Runner""",Junior Walker,General,"According to the ceremonial customs of orthodox Judaism, it is officially sundown when you cannot tell the difference between what",A black thread & a red one +General,What was the name of Ross' pet monkey on 'Friends',Marcell,Music,"Who Wrote The American Counterpart Single ""We Are The World""",Michael Jackson/Lionel Richie,General,What is a group of this animal called: Toad,Knot +General,Which Polish-Ukranian region shares its name with a Spanish region?,Galicia,General,"Who starred in the film ""Fried Green Tomatoes at the Whistlestop Cafe'",Jessica tandy,General,Anise flavored Greek liqueur,Ouzo +General,Band of decoration especially at the top of a wall,Frieze,General,Who is buried in a chapel near lake Stroganoff in Romania,Count Dracula,General,What is an eight sided polygon called,Octagon +General,In Michigan married couples can be imprisoned unless they what,Live Together,General,What is the opposite of Plenum,Vacuum,General,Which Singer At Birth Had The Forenames Katherine Dawn?,K.D Lang +General,In which Olympics did Mark Spitz achieve a new Olympic record by winning seven gold medals,1972, History & Holidays,"""What Did My True Love Give To Me On The """"Eleventh"""" Day Of Christmas"" ",11 Pipers Piping ,General,What would you do with a celesta,Play it – percussion instrument +General,An America reindeer,Caribou,General,The gaseous material surrounding the earth and other planets is called the ______,Atmosphere,General,Porn star Candida Royale was named after what,A vaginal yeast infection +General,What hotel hosted the first Oscar ceremony,Roosevelt Hotel,General,What actor played harry callahan,Clint eastwood,General,Scott Joplin's music used for the film The Sting name the tune,The Entertainer +General,“ Fornebu ” International Airport Is In Which European Country,Norway,General,Whats the name of the large wooded area in which Robin Hood was supposed to have lived,Sherwood forest,Art & Literature,"For Which Novel Was Boris Pasternak Awarded The 1958 Nobel Prize , An Award He Declined ",Dr Zhivago  +General,Edgar Cuthwellis was option but the author chose another what,Lewis Carrol,Science & Nature,The unit of electrical resistance is the _____,Ohm,General,What is the flower that stands for: safety,Traveller's joy +General,What are the English and German names for Easter or Ostern derived from ,The ancient name for the month of April ,General,What is the fear of outer space known as,Spacephobia, History & Holidays,Which Song By James Blunt Released In March 2006 Failed To Enter The UK Top 20 Despite Being Top Of The Radio 1 Playlist For That Month ,Wisemen  +Entertainment,Who played Dr. Kildare?,Richard Chamberlain,General,In Virginia its illegal for a man to do what to his wife,Pat her Derrieres,Science & Nature,To what disability can keratitis lead?,Blindness +General,Which British bird is the largest of the European grouse,Capercaillie,General,Mrs Hugh McCorquodale was famous as what literary figure,Barbara Cartland,General,What type of animal is an auklet,Bird +People & Places,Which Bad Boy Is Arsenals Top Scorer Ever ,Ian Wright ,General,"Saying: out of sight, out of___.",Mind,General,KLM is the national airline of which country,Holland +Religion & Mythology,What is the name of the field where Christ was crucified?,Calvary,General,Flutes made from what material do not expand with humidity so their owners are spared the nuisance of tuning them,Glass,General,What do americans traditionally eat on thanksgiving day,Turkey +General,Which Infamous Figure Was Executed Whilst In Prison In Indiana On The 11 th June 2001?,Timothy McVee (The Oklahoma Bomber),Food & Drink, From what animal do we get venison,Deer,Science & Nature,What Road Safety Device Did Percy Shaw Invent ,Cats Eyes  + History & Holidays,Santa Claus reportedly lives at the _____ Pole.,North,General,"What were Mouth and Chunk's real names in ""The Goonies""?",Clark and Lawrence,General,Until 1971 what was the name of Zaire,Congo +General,The Panama canal connects the pacific Ocean and which other body of water,Caribbean sea,General,Freetown is the capital of ______,Sierra leone,General,Poisonous plant with small white flowers,Hemlock +General,"Israel Tongue and who else devised the ""Popish Plot""",Titus oates,General,"In 1966, which woman became the first Briton to fly solo around the world",Sheila scott,General,"Colobus, vervet and mangabey are types of which animal",Monkey +People & Places,Which Presenter Of Tomorrows World Married Keith Chegwin ,Maggie Philpin ,Science & Nature,The spiral galaxy nearest ours is the _________ galaxy.,Andromeda,General,"Which artist, remembered more for his portraits than his landscapes, was born in Sudbury, Suffolk, in 1717",Gainsborough +General,"If someone gets out of a difficult situation, he is said to have saved his what",Bacon,General,Anger towards other road users experienced by a person when driving,Road rage,General,Open embroidery on usually white cotton or linen,Broderie anglaise +General,Which African country has the letters EAK as its international vehicle registration,Kenya,Science & Nature,"Of what does the typical man have 13,000?",Whiskers,Geography,What river has the largest drainage basin,Amazon + History & Holidays,In which year did the UK hand over Hong Kong sovereignty to China? ,1997 (June 30th) ,General,Logophobia is a fear of ______,Words,General,What are Tortoiseshell and Speckled Wood types of?,Butterfly +General,In what Australian state would you find Woomera,South australia,Science & Nature,What is a monotreme?,An egg laying mammal,General,Hera in Greece Juno in Rome Goddesses of what,Childbirth +General,In the movie what is Shafts first name,John,General,What actor died during the filming of Gladiator,Oliver Reed,General,Tonsurphobia is a fear of ______,Haircuts +General,Who was the first president to be televised,F D Roosevelt Worlds Fair 1939,General,When did the British Empire become the commonwealth,1931,General,What's the third largest lake in the world,Lake victoria +Science & Nature, __________ have three eyelids to protect themselves from blowing sand.,Camels, History & Holidays,How old was Alexander the Great when he died ?,32,General,What Castro trademark did the CIA try to lace with poison,His cigar cigar +General,Marie Curie won Nobel prizes in which two categories,Physics and chemistry,General,What star had a job as aircraft factory inspector,Marilyn Munroe,General,Who composed the ballets Sleeping Beauty and The Nutcracker,Tchaikovsky +General,"What does the Italian word ""Paparazzi"" mean",Little fleas,General,Chuck Berry Art Garfunkle Robert Redford had what job,Carpenters,General,What Is Daltonism,Colour Blindness +General,"American physicist & government adviser, who directed the development of the first atomic bombs",J robert oppenheimer,General,What was the beatles' second film,Help,General,Dr Teeth was the leader of the band Electric Mayhem - where,The Muppet Show +General,What colour is cerulean,Blue,Food & Drink,"Is Amontillado a sweet, medium or medium-dry sherry? ",Medium Dry ,General,Lake Tittikaka is in Peru and what other country,Bolivia +General,Who is Harry Potters main enemy,Lord Voldemort,Art & Literature,What Creatures Does Captain Ahab Become Obsessed With ,"Moby Dick, Great White Whale ",General,In West Virginia Nicholas County its illegal to do what in pulpit,Tell Jokes +Music,Who is Don Van Vliet better known as?,Captain Beefheart, Geography,On which river is the Aswan High Dam?,Nile,Food & Drink,What Type Of Sauce Traditionally Goes With Roast Beef ,Horseradish  +General,What number is used to represent satan,666,General,What metal is used in galvanizing,Zinc,General,What parts of a car have drums & shoes,Brakes +Music,What animal is on the cover of the Beach Boy's album Pet Sounds?,Goat,General,Brassiere comes from an old French word meaning what,Arm Protector,General,What nationality is Thor Heyerdahl,Norwegian +General,Liver disease caused by alcoholism,Cirrhosis,General,In which sport would they use the term crotch ball,Handball - ball hitting floor ceiling,General,In which film did jay leno play 'mookie',American hot wax +General,What is the flower that stands for: confession of love,Moss rosebud,General,What is the clotting protein in blood called,Fibrin,General,Who declared in 1962 that having a hole drilled through the cranium enabled people to reach a higher state of consciousness,Dr bart hughes +Music,Name The Legendary British Producer Who First Shot His Landlady Before Shooting Himself,Joe Meek,General,What trophy is awarded to the winner of the NHL play-offs,Stanley Cup,General,What is Pancetta,Bacon +Geography,"The U.S. coastline, comprised of the Atlantic, __________, and Gulf waters, involves 25 of the 48 mainland states.",Pacific, Geography,What is the capital of Poland ?,Warsaw,General,What was invented in Rome 63 bc by Marcus Tiro,Shorthand and the & sign +Music,Which Former Hit Single Did Status Quo Alter The Title Of And Re Release In 1988,They Changed Rockin To Running,Science & Nature,What Did James Naapier Develop ,Logarithms ,General,The world's youngest parents were ___ & 9 years old.,Eight +General,Who was the original voice of Darth Vader (hint: NOT James Earl Jones),David Prowse,General,Who claimed to be the first person to swim across the atlantic ocean,Guy, History & Holidays,"*Who loves ya, baby?'' was the catchphrase of which `70s TV sleuth?* ",Kojak  +General,Which great battle took place from July 1st to November 18th 1916?,The Battle of the Somme,Music,What Was The Title Of The (Not Very Apt) Album Released By Michael Jackson In 2001,Invincible,General,What river forms at the confluence of the Allegheny & the Monongahela,Ohio river ohio +General,Which year of the Chinese calander began in the year 2000,Dragon,General,What is the name of the office used by the president in the Whitehouse?,Oval office,General,What was the most popular Xmas gift in 1913,Erector sets – Meccano + History & Holidays,Strawberries are the undoing of an unhinged man in which 50s film ? ,The Caine Mutiny ,Science & Nature, A fox litter is typically 10 to 15 __________,Pups,General,"He belted out hits for Bad Company, The Firm and then went out on his own",Paul Rodgers + History & Holidays,What Are The Names Of The 3 Wise Men Who Brought Gifts To The Baby Jesus ,"Gaspar, Melchior, Balthasar ",General,This sport allows substitutions without a stoppage in play,Hockey, History & Holidays,On Tv How Is Eddie Fitzgerald More Commonly Known ,Cracker  +General,Bad before a German town name means what,It’s a Spa Town,Geography,How many stars are on the flag of New Zealand,Four,Sports & Leisure,What is the misshapen ear that boxers often have called?,Cauliflower ear +General,What was the name of the monster that attacked Luke in the trash compactor in Star Wars?,A dianogaIn,General,In which novel by George Eliot would you find the characters 'Maggie' and 'Tom Tulliver',The mill on the floss, Geography,What is the basic unit of currency for Chile ?,Peso +General,In the Little Mermaid fairy tale what happens to her,She Dies,General,The tenth wedding anniversary is commemorated with what,Tin,General,What's involved in 20% of car accidents in Sweden,A moose +Food & Drink,Which Celebrity Chef Owns A Chain Of Restaurants Called 'Fifteen'' ,Jamie Oliver ,General,Through the streets of what town did lady godiva ride naked,Coventry,General,Name Homer Simpsons bowling team,Pin Pals +General,What does a cat use to determine if a space is too small to squeeze through,Whiskers,General,Apart from drinks what used to be stored in pub cellars,Corpses - cold place,General,Which classic film was called production 9401 during filming,Psycho +General,What colour is Octopus blood,Blue,General,"What is the missing word in english with the letter combination 'uu' muumuu, duumvirate, residuum, vacuum, duumvir",Continuum,General,What was the name of the family featured in `Father Knows Best',Andrews +General,What does a bryologist study,Mosses,General,Barak O Bama Recently Became The First Black USA President But In Which US State Was He Born,Hawaii,General,What Australian slang for a simpleton is also a cockatoo,Galah +General,The transcendental number 'e' was named after what mathematician,Euler,General,What is the connection between Good Times and Different Strokes?,Janet Jackson,Food & Drink,Who invented fortune cookies?,Charles Jung +Entertainment,What entertainer is allowing one of his songs to be used in a government campaign to beat drunk driving?,Michael Jackson,General,In computing what does EPOS stand for,Electronic Point of Sale,Music,Which New Romantic Star Was Once Voted The Most Beautiful Man In The World,David Sylvian (Japan) +General,What was the name of the sad faced clown portrayed by Emmett Kelly,Weary,General,Who was the last king of Troy killed by Achilles son Pyrrhus,Priam,General,Rio de janeiro is the capital of ______,Argentina +General,Rank of a knight between bachelor and baron,Banneret,General,A Grice is a young what,Wild Boar,Entertainment,In which film did Paul Newman and Robert Redford hold hands and jump into a river?,Butch Cassidy and the Sundance Kid +General,Xenophobia is the fear of ______,Strangers,General,Kymophobia is the fear of,Waves,General,Which creatures lived in Arnold Bros (est. 1905),Nomes +General,The De Beaumont centre in London specialises in what sport,Fencing,General,What does Trivia literally mean,Three Roads,General,T or F The Peanut is a type of nut?,Flase (It's a Legume) +Music,"Which Female Trio Released The Multi Platinum ""Wide Open Spaces"" In 1998",Dixie Chicks,General,"Fill in the ""Bear"" Bryant remark: ""A tie is like kissing your """,Sister,Science & Nature,On a standard keyboard which is the largest key? ,The Space Bar  +General,Which Author Who Is Most Remembered for Writing Another Series Of Books. Also Wrote the Childrens Story Chitty Chitty Bang Bang,Ian Flemming,General,Who did the voice for the cartoon character betty boop,Mae questel,General,Wine brandy sherry almonds raisins orange glogg what country,Sweden a Christmas punch drink +Music,Which Musical Was The First To Feature A Mixed Black And White Cast On Stage,Show Boat,General,In the 1950s which film star was paid $5000 a week,Lassie,General,Barbie's measurements if she were life size:,39-23-33 +General,Which animal is known as 'zorro' in Spanish and 'volpe' in Italian,Fox,General,The women's world cup in tennis is played for what trophy,The Fed cup,General,In beer measurement 72 pints make a what,Firkin +General,What famous stone structure is located near Salisbury ?,Stonehenge,General,Where is Mount Rushmore,South dakota,Religion & Mythology,How long did it take God to create the Universe?,Six days - he rested on the seventh +General,How many keys are there on a grand piano?,Eighty eight,General,What is a troika a type of,Horse drawn vehicle,General,What united states president was in office during the civil war,Forty three +General,English charts: Clive Dunn had a hit called Grandad in which year,1971,Sports & Leisure,In Which Sport Might You See A Double Axel ,Ice Skating ,Science & Nature,These glands are located on top of the kidneys.,Adrenal +General,Who was the greek god of fire,Hephaestus,General,What New Zealand native invented bungee jumping,AJ Hackett,General,What nationality is footballer Lucas Radebe,South african +General,What was the first man made object to leave the solar system,Pioneer 10, Geography,What is the basic unit of currency for South Korea ?,Won,Art & Literature,"Faulkner penned this book with 4 distinctive sections: the Benjy section, Quentin's section, Jason and then Dilsey's sections.",The Sound and the Fury +General,What computer language was named after ada lovelace,Ada,General,What was the codename of the aborted German invasion of England in 1940,Operation sealion,General,Name Ernest Hemmingway's book dealing with bullfighting,Fiesta +General,"Which major land offensive began on the 1st July, 1916",Battle of the somme,General, The earth's atmosphere and the space beyond is known as _______.,Aerospace,People & Places,Who Was The Archbishop Of Canterbury In 1995 ,George Carey  +General,"What fashion designer was responsible for ""The New Look""",Christian Dior,General,George Bernard Shaw's Play “ Pygmalion ” Was Adapted To Become Which Musical?,My Fair Lady,General,What constellation is represented by a fish,Pisces + History & Holidays,What did President J. Buchanan not have,A wife,Science & Nature,What is the meaning of the name of the constellation Auriga ?,Charioteer,Entertainment,Secret Identities: Cliff Steele,Robotman +General,"In 1361 bc, who became the king of egypt at the age of nine",Tutankhamen,General,What was the first item made from aluminium,Rattle for Napoleon III,General,Where would you find an intrados,Inside curve of arch +General,Cookie what gas that animals exhale do plants utilize,Carbon dioxide,Music,Name The Two Singers Who Duetted On The Song “I Knew You Were Waiting For Me” In 1987 PFE,Aretha Franklin & George Michael,Science & Nature,What Part Of A Car Engine Mixes Fuel With Air ,The Carburettor  +General,A Treskilling Yellow sold for over $2 million in 1996 what is it,A Stamp,General,How Is The Character Of “ Paul Metcalfe ” Better Known?,Captain Scarlett,General,What were the Ghostbusters' names?,"Peter Venkman,Egon Spengler,Ray Stantz ,Winston Zedmore" +Entertainment,"An Andy Panda cartoon gave birth to a famous, cantankerous bird. Name him.",Woody woodpecker, History & Holidays,In the Christmas carol 'Away in a Manger' where was the little Lord Jesus asleep on ,The Hay ,General,Benjamin who was the first Lord Mayor of Dublin,Guinness +Music,"Which UK Band Had A Hit With ""Sonic Boom Boy""",Westworld,General,What colour is the cross on the Swiss national flag,White,Music,Which Beatle was barefoot on the cover of Abbey Road?,Paul +General,Oedipus was named after what - literal translation,Swollen feet, History & Holidays,Who sang the theme tune to the James Bond. Movie 'The Spy Who Loved Me ,Carly Simon ,General,In which country is Kamsai Airport?,Japan +General,Escaping convicts used to drop what to throw dogs off the scent,Red Herrings,Food & Drink,Which product was renamed `Arthur's' after the star of its advertising campaign? ,Kattomeat , Language,"Subject, verb and object are parts of a _________.",Sentence + Geography,Havana is the capital of which country?,Cuba,Entertainment,Who is stationed at Camp Swampy in the comic strips,Beetle bailey,General,Which language calls itself Vlaams,Flemish +General,"Who wrote ""When angry, count a hundred. When very angry, swear""",Mark twain,General,"Who Walked Out On The 1985 Blockbuster "" Beverly Hills Cop"" After His Demand For More Action Scenes Was Rejected",Sylvestor Stallone,General,"What is the missing word in english with the letter combination 'uu' muumuu, continuum, duumvirate, residuum, duumvir",Vacuum +General,What river does the Grand Coulee Dam dam,Columbia,General,Bortsch is a traditional dish from which country,Russia,Art & Literature,Who Created The Character Adrian Mole ,Sue Towsend  +Science & Nature,"In space talk, FTL is an acronomy for ______ ____ _____.",Faster than light,General,Most salad dressings derive the majority of their calories from____,Fat,General,What is a group of seals,Herd +Art & Literature,"This book, Oscar Wilde's only novel, was used as evidence in his sodomy trial?",The Picture of Dorian Gray,General,What is the English name for the constellation Hydra,Water Snake,General,What I did for Love came from which Broadway musical show,Chorus Line +General,What was the last chinese dynasty,Manchu,General,Who was the chief protagonist of He-Man in the cartoon series?,Lord Skeletor, Language,What ONE word fits ____hood; ____hole; ____date.,Man +General,How many organizational levels are there in the human body,Four,General,What oil does the flax plant produce,Linseed oil,General,Tres Hombres' was the 1973 release by which El Paso Texas band known for the beards,ZZ Top +General,What is the basis of the Indian dish 'Riata',Yoghurt,General,Which gemstone has the highest value per carat,Ruby,General,When something is completely different it is said to be one of these of a different color,Pale +General,Who owned a chimp called Chee-Chee,Dr Dolittle,Music,"Which Band Had Hits With ""Promised You A Miracle"" & ""Alive And Kicking""",Simple Minds,General,Which European city's name means home of the monks,Munich or Munchen +General,Royal Society Prevention Accidents 1991 7500 injured by what,Shopping Trolleys,General,Males outnumber females by 5 to 1 in what addiction,Alcoholism,General,Which company controls more than 80% of the world's rough diamond supply?,De Beers +General,Which is the most widely used expression in any language,Ok,Sports & Leisure,In Which Sport With You Find A Piece Of Wooden Apparatus Exactly 17ft Long ,(Caber Toss) ,General,The golden state warriors basketball team retired 42 which used to belong to _____,Nate thurmond +Science & Nature,Americans Call It A Caribou What Is It Called In Europe ,Reindeer ,General,What is a group kangaroos,Troop,General,"What news magazine boldly claimed: ""there's no substitute""",Time magazine +Science & Nature,"The Echidna, Or Spiny Anteater , & The Duck Billed Platypus Shares A Characteristic Which Does Not Apply To Any Other Mammal What Is It? ",They Lay Eggs ,General,Who did Leonardo DiCaprio play in the movie Titanic?,Jack Dawson, History & Holidays,What Is New Years Eve Caled It Scotland ,Hogmany  +Entertainment,Film Title: The Last Days of _________. (a city),Pompeii,General,What Is The Worlds Most Common First Name,Muhammad,General,What is a group of bobolinks,Chain +General,Who was the democratically elected President of Chile killed during a military coup in 1973,Salvador allende,General,Who was the Roman Goddess of peace,Pax,General,What film was playing at the drive-in in the film 'twister',The shining +Food & Drink,Which fruit has the most calories per gram?,Avocado,Music,"Who Sang ""My Jamaican Guy",Grace Jones,General,"Who said ""ability is useless without opportunity""",Napoleon +Sports & Leisure,What Does TT Stand For In Connection With The Isle Of Man Motorcycle Race? ,Tourist Trophy ,General,What is the name of the first test tube baby,Louise brown,General,One ragweed plant can release approximately how many grains of pollen,One +Music,Which Song Was A Hit For Both Westlife And Abba?,I Have A Dream,General,Chaplin ate a boot in the Gold Rush - what was it made of,Liquorice,Music,What Is The Proper Name For An English Horn,Cor Anglais +General,What did dr seuss' grinch steal,Christmas,General,In ancient Rome what was the triclinium,Dining Room, History & Holidays,Which Country Owns Christmas Island ,Australia  +General,What is the name of the gold-mining town in the musical Paint Your Wagon,No name city,General,What sport was standardized under the marquise of queensberry rules,Boxing,Sports & Leisure,Which Nation Did France Beat 3-0 In The Final Of The Football World Cup In 1998 ,Brazil  +General,Which beer was promoted with the slogan 'The Beer that men drink'?,Double Diamond,Geography,What Value Is A Storm On The Beaufort Scale ,Force 11 ,General,"In 1963, bobby darin released ""you're the reason _____""",I'm living +General,In 1890 the first electric what opened in London,Underground railway,General,What should a golfer shout as a warning,Fore,General,"What nation's 90 man army is the world's oldest, dating back to 1506",Vatican city's vatican citys vatican city +General,Iron statue of Vulcan looks down Red Mountain what US city,Birmingham Alabama,General,Who Was The Greek God Of Travellers And Thieves?,Hermes,General,Which disease is tested for using the 'Schick Test',Diphtheria + History & Holidays,What revelation did alexander butterfield make to the senate watergate committee ,The oval office bugging ,General,"In common: humor, mannered, suited, take",Ill,Geography,In which city is the wailing wall ,Jerusalem  +General,Which chemical element takes its name from a German word for goblin,Cobalt,General,"In Greek mythology, who was abducted by zeus to crete",Europa,Music,In Which Year did John Lennon Tell Us Happy Xmas (War Is Over),1972 +General,Singer paula ______,Abdul,General,What is the most mentioned name in the Bible,David - Jesus is second,General,Which country was the first to elect a woman as head of state,Iceland +General,Who played Annie Walker in Coronation Street,Doris speed,Food & Drink,What is the animal product used in the making of the Italian dessert 'cassata'?,Egg white,General,What are the seeds of the herb cilantro called used as a spice,Coriander +Geography,"Where would you find the Queen Alexandra, Queen Elizabeth and Queen Maud mountain ranges? ",Antarctica ,General,Who Sang The Theme Tune To The James Bond Movie “Moonraker”,Shirley Bassey,Science & Nature,Who invented the centigrade scale?,Anders Celsius +General,Benjamin Franklin suggested that __________ should be the U.S. national bird,Turkey,General,"Facts, Briefs, Destiny and Chance were early names for what",Time Magazine,General,What is the most common plastic surgery performed USA men,Breast Reduction +Sports & Leisure,Which sporting hero from the 70's died in October 1995 at the age of 30? ,Red Rum ,General,In the USA it’s the Oscars what is it in France,Caesars,General,"Who, in 1947, was the first man to break the sound barrier",Chuck yeager +Geography,"What is the largest city in Australia, in terms of population",Sydney,General,"This is a type of small, fried, Indian bread",Poori,General,What is the fear of overworking or of pain known as,Ponophobia +General,What are the roads of guam paved with,Coral,General,What colour is pure molten gold,Green,General,What comic strip duck is a billionare,Scrooge mcduck +General,What pastry is uded to make eclairs,Choux,General,What was the name (4 letters) of the New York night club that helped launch the career of several early new wave groups?,CBGB's,Science & Nature,Which meteor shower occurs on the 4th November ?,Taurids +Music,Whose Breakthrough Single Came With Messages?,Orchestral Manoeuvres In The Dark,General,Where are there over 58 million dogs,U.S.A.,General,Who wrote The Shining,Stephen king +Sports & Leisure,Which European club has won the most european cups in the 90s?,AC Milan,Entertainment,Who is Sally Brown's sweet baboo,Linus van pelt,General,What was the first film made in cinemascope,The robe +General,Where does the wine Mateus come from,Portugal,General,An instrument on a car to measure the distance travelled is called a(n) ________.,Odometer,General,Barrel size - what beer barrel contains 108 gallons,Butt +Sports & Leisure,What is the emblem on the All Black rugby shirt? ,Silver Fern ,General,What is the highest mountain in South America,Aconcagua,Food & Drink,How is a drink served when it is described as Frappe? ,With finely crushed ice  +General,What colour are the Amazon river dolphins,Pink,General,The word Sofa comes from the Arabic meaning what,Bench,Sports & Leisure,Name One Of The 3 Years In Which The Olympics Were Cancelled ,"1916, 1940, 1944 " +General,"Who described TV as ""Chewing gum for the masses""",Architect Frank Lloyd Wright,Geography,At Whaat Height Might You Find Cumulonimbus Cloud ,"20,000 - 30,000 Feet ",General,Electrical circuit made by depositing conductive material on the surface of an insulating base,Printed circuit board +General,"Which Billy Rose song, written with Al Jolson and Dave Dreyer, was a hit as a duet for Frank Sinatra and Sammy Davis Jnr. in 1962",Me and my shadow,Music,Which Entertainer Was Born Pricilla White,Cilla Black,Science & Nature,What Is The SI Unit Of Force ,The Newton  +General,The name of which Indian city means Village of Boiled Beans,Bangalore,General,Which singer who died in the 70s was born Ellen Naomi Cohen,Mama cass,Music,Who Had A Dose Of Bad Love In 1990,Eric Clapton + Language,What ONE word fits ____stream; ____hill; _____pour.,Down,General,"After which battle of the English Civil War did Charles II hide in an oak tree at Boscobel, to avoid capture by the Roundheads",Worcester,General,Which company invented the transistor radio in 1952,Sony +General,What is switzerland's official neutral name,Helvetic confederation,People & Places,Who Was Known As The Maid Of Orleans ,Joan Of Arc ,Science & Nature,What does the pancreas produce?,Insulin +General,Which planet is orbited by the moon Charon,Pluto,Science & Nature,Which Animal Is The Symbol Of The World Wide Fund For Nature ,Giant Panda ,General,Name the porceilan chair you sit on at least once a day.,Toilet +General,In Newark its illegal to sell what after 6pm unless Drs note shown,Ice Cream,General,Strong shoe used for walking,Brogue,Music,"In Which Musical Did Clint Eastwood Perform ""I Talk To The Trees""",Paint Your Wagon +General,A well in which water rises through natural pressure,Artesian,General,In what Australian state would you find Kiama,New south wales nsw,General,Who was the founder of Live Aid?,Bob Geldof +General,Who Owns The Cray Super Computer?,The Met Office,General,What is the flower that stands for: curiosity,Sycamore,General,What is the japanese currency,Yen +General,What is the name of the capital of Alberta (Canada),Edmonton,General,An Ounce whisky glass and a small keg what same name,Pony,General,What is the name for the part of the bone that fits into a socket to form hip and shoulder joints,Ball +General,Who coined the term security blanket,Charles Schulz,General,"In 'romeo and juliet', who says 'what must be shall be'",Juliet, Geography,Guayaquil is the largest city in what country?,Ecuador +General,Which European city is served by Galileo Galilei Airport,Pisa,General,In Urbana Illinois its illegal for who/what to enter the city limits,A Monster,General,"Whose autobiography is called ""Take it like a Man""",Boy George - or George O'Doud +General,What is the largest lizard on earth at ten feet long & up to 250 pounds,Komodo dragon,General,Who wrote the book on which Donizetti based his opera Lucia di Lammermoor,Sir walter scott,General,"According to Arthurian legend, what did King Arthur receive as a dowry, on his marriage to Guinevire",The round table +General,Homichloriphobia is the fear of what,Fog,Music,"Name The Band From The Following Members  Robert Plant, Jimmy Page, John Paul Jones, John Bonham",Led Zeppelin,General,The film Apocalypse Now was based on whose novel,Joseph conrad +Music,Name The Production Credit That Jagger & Richards Adopted From 1974 Onwards,The Glimmer Twins,General,The first power steering was in this car.,Mercedes_benz,General,Pierce Brosnens contract stops him doing what in any other film,Wearing a Tuxedo +General,In the Saint series of books what is Inspector Teal's full name,Claude Eustace Teal,General,Name the hero Len Deightons Ipcress File and Funeral in Berlin,Not named in books Harry Palmer in films,General,"In the British army, which rank is immediately above colonel",Brigadier +Food & Drink,Which Nation Has The Sweetest Tooth ,Holland ,General,What is the Capital of: Christmas Island,The settlement,General,The Stanley cup was not awarded in 1919 what stopped it,Influenza epidemic +General,What does cbs stand for,Columbia broadcasting system,General,In Superman what was the original name of The Daily Planet,The Daily Star,General,"In the 'nightmare on elm street' films, who played freddy krueger",Robert +Geography,Which German federal state surrounds the city of Berlin? ,Bradenburg ,General,Where Will You Find The Pyramids Of Malpighi?,In Your Kidneys,General,What is the capital of the Spanish province Cantabria,Santander +Science & Nature,What Does An Apiarist Keep? ,Bees ,Music,With Which Girl Group Did Louise Nurding First Come To Fame?,Eternal,General,UK band involved in a US court case - Subliminal messages 80s,Judas Priest +General,Not harmful to the environment,Eco-friendly,General,What is an olive in spanish,Aceituna,Music,Alice Cooper Has The Unusual Hobby Of Collecting What,Watches +General,The Aztecs Ayecotl is a forerunner of what current food,Haricot Beans, History & Holidays,What city was founded in 753 BC ?,Rome,General,What was used at Wimbledon for the first time in 1986,Yellow tennis balls +General,The stone what dietary problem were 65 percent of brazilians suffering in 1985,Malnutrition,General,"Besides gin, what other alcoholic drink is used to make a White Lady cocktail",Cointreau,General,What is the flower that stands for: purity and sweetness,White lily +General,Sigmund Freud had a phobia - what was he afraid of,Ferns,General,Which Book First Published In 1931 Was Sold Out Across The UK In March 1996,The Highway Code,General,"The Pentagon uses, on average, about 666 rolls of ___________ every day",Toilet paper +General,Third letter of the Greek alphabet,Gamma,General,Who was the director of the 2014 film 'Turner'?,Mike Leigh, History & Holidays,"Name the remake of a 1960's film with the aid of the following actors. The first actor was in the 60's original, the second actor played the same role in the remake Robert Mitchum and Robert de Niro ",Cape Fear  +General,Which British Queen was excommunicated by the Pope,Elizabeth i,Sports & Leisure,Who Might Use A Penholder Grip ,A Table Tennis Player ,Entertainment,"Who played Garth in ""Wayne's World""?",Dana Carvey +General,Which country was the setting for The Flame Trees of Thika,Kenya,General,What body of water is fed from the south by the Wadi Araba & from the north by the river Jordan,The dead sea dead sea,Music,Who Did Paul McCartney Perform a Duet With On The Song “Say Say Say”,Michael Jackson + History & Holidays,Name the toy that consisted of color pencils and plastic which you would put in the oven to create. ,Shrinky Dinks ,Sports & Leisure,"In A Game Of Snooker If You Achieved A Perfect 147 Break, How Many Times Would You Pot The Black Ball In Total ",16 ,General,By what process is rum created,Fermenting molasses + Geography,What state is the Golden State?,California,General,Who played captain james t kirk in star trek,William shatner,General,What was Vivian Leigh’s character won Oscar in her 30s,Blanche Dubois +General,Who Was The Childhood Playmate Of Little Jackie Papers,Puff The Magic Dragon,General,Roman soldiers were given slaves - what were they called,Addicts addicted means enslaved, History & Holidays,Which character of the 'Bloom County' comic strip ran for president even though he was dead at the time? ,Bill the Cat  +General,Which element has the lowest boiling point,Helium,General,Mixed diced vegetables in mayonnaise is what sort of salad,Russian,General,The Sound and the Fury took its title from what other work,Macbeth +General,"In the Bible, which city was destroyed on God's command to Joshua and the people of Israel, by walking around it seven times whilst blowing loudly on horns",Jericho,General,A menial working class in Old Japan and Greek letter what word,Eta,Food & Drink,Which fruit is a cross between a peach and a plum? ,Nectarine  +General,Fill in the blank: take me to your ___,Leader,General,Which ancient ship was brought up from the sea in 1982,The mary rose,General,What event led to Hirohito ascending to the Japanse throne in 1926,His fathers death +General,"Chinese call it little mouse, Danes Swedes elephants trunk?",The @ sign,Entertainment,"What's the name of the Mummy in the film ""The Mummy""?",Imhotep, History & Holidays,The video for which eighties song features nothing but 5 cheerleaders?(Name the artist too) ,Mickey Toni Basil  +General,The phillips head screwdriver was invented where,Oregon,General,A ballet bow or curtsy in which one foot is pointed in front and the body leans forward.,Révérence,General,"What links elephanta, bad-i-sad-o-bistroz, oe, whuly, zonda",All winds +General,What is a moab,Type of hat,General,If silence is golden what is silver,Speech,General,What nationality is footballer Davor Suker,Croatian +Geography,Bridgeport is the largest city in which state,Connecticut,General,What winged hindu god of love carries a bow and arrow,Kama,General,What is the art of fighting with gloves on the hand,Boxing +Entertainment,Which 1960's group sang a song inspired by 'Alice In Wonderland'?,The Jefferson Airplane,General,What was howdy doody's sister's name,Heidi doody,General,What was the operative name of WWI spy Geertruida Zelle,Mata hari + Geography,Which Californian desert drops below sea level?,Death Valley,General,Which King was born at Bolingbroke Castle and was nicknamed 'Bolingbroke'?,Henry IV,General,What is the largest BBS in the world,CompuServe +General,In Dukes of Hazard boys drove General Lee name Daisies jeep,Dixie,General,Close fitting knee length shorts,Bermuda,General,In which London Square is the American Embassy situated,Grosvenor square +General,If You Suffered From Lockiophobia What Would You Be Afraid Of,Child Birth,General,In The World Of The Music How Is 'Pauline Matthews' More Commonly Known,Kiki Dee,General,Palaeontology is the study of what,Fossil remains +Religion & Mythology,Who is the greek equivalent of the roman god Neptune,Poseidon,Food & Drink,Which two cheeses are layered in a Huntsman Cheese? ,Double Gloucester And Stilton ,General,"In Simon & Simon,what unusual thing did Rick live in?",In a boat +General,"Who said ""I like Beethoven especially the poems""",Ringo Starr, History & Holidays,What was ITV's live pop music show called? ,Ready Steady Cook ,General,Kynophobia is the fear of,Rabies +General,"In 1981, who's gold lp was called ""bella donna""",Stevie nicks, Geography,Which element makes up 5% of the Earth's crust ?,Iron,Sports & Leisure,In which sport would you see a Boston Crab or a Full Nelson ,Wrestling  +Science & Nature,"In The Wizard Of Oz, What Was The Lion Looking For ",Courage ,General,Shingle was the codename for what WW II Allied landing,Anzio,General,Which popular singer of the 80's has the real name Christopher Davidson,Chris de burgh +General,What is the most ordered item in American restaurants,French Fries,General,What are the names of the two famous disney chipmunks?,Chip & dale,General,Who followed Grover Cleveland as U.S. President in 1897,William mckinley +Sports & Leisure,What is 11-12 Inches Long and Can't Weigh Less Than 50 Grams? ,A Relay Batton ,Religion & Mythology,Who is the Norse god of justice ?,Forseti,General,What was the first offical international boat race,Hundred guineas cup +General,What is the name of the lead singer for the Smiths?,Morrisey, History & Holidays,Good King Wenceslas was king of which country? ,Bohemia ,General,What is the flower that stands for: chivalry,Great yellow daffodil +General,What Was Duran Duran First Ever Top 40 Single,Planet Earth,General,Who was the kitten with a whip,Middlebury,General,Who wrote the opera the Snow Maiden,Rimsky-korsakov +Music,"Who Recorded A Version Of ""It's A Hard Days Night"" In The Style Of Laurence Olivier's Richard iii",Peter Sellers,Entertainment,Who starred in the film version of 'To Kill A Mockingbird'?,Gregory Peck,General,What occurs in September and December more than any other month,Letter E +General,Where could you see a likeness of Pharaoh Khafres head,On the Sphinx,Music,Who was the first Spice Girl to have a solo chart single?,Geri Halliwell,General,Where does Dilbert think of inventions,In the bathtub +General,"Whose autobiography was ""Can you tell what it is yet""",Rolf Harris,Science & Nature,Who Invented The ZX80 The First Widely Available Personal Computer ,Sir Clive Sinclair ,Entertainment,What was Elvis Presley's twin brother's name?,Garon +General,Name the French blue-veined cheese that is ripened in limestone caves,Roquefort,Sports & Leisure,In Which Game Do You Twist ,Pontoon/Blackjack ,General,Elvis Presley collected statues of what famous woman,Joan of Arc +General,In music what is a chromatic scale,A scale made up of semi-tones,General,Where in England did Lady Godiva bare all,Coventry,General,In what athletic event is it illegal to carry weights,Long Jump +General,What is the name of the Turkish aniseed liqueur trans lions milk,Raki,General,Enola Gay dropped the first A bomb - what plane dropped 2,Bocks Car,General,If you were eating fragrant meat in Hong Kong what is it,Dog +Food & Drink, Natural vanilla flavoring comes from this plant.,Orchid,Music,“Oh Carolina” and “Boombastic” were UK hits for which Jamaican reggae star?,Shaggy,General,What Canadian province was the site of England's first overseas possession,Newfoundland +General,An iron hook with a handle used for landing large fish.,Gaff,Science & Nature,What is the violet variety of quartz otherwise known as?,Amethyst, History & Holidays,What Did Lord Carnavon & Howard Carter vDiscover In 1922 ,The Tomb Of Tutankhamen  +General,In music what does the term 'ff' mean,Very loud,Music,After A Public Scandal Which British Male Vocalist Reach No 35 On June 25th 1998,George Michael,Science & Nature,Why Was The Seismosaurus So Named ,Because Of It's Size Hence Earth Shaking Lizard  +General,What is the venue for the coursing Waterloo Cup,Altcar,Music,Which Rickie Told Us The Chuck E's In Love,Rickie Lee Jones, History & Holidays,Which King Was Overthrown As A Result Of The French Revolution ,Louis XVI  +Music,The Lead Singer Of Ultravox Was Called Midge Because That's His Real Name Or Because He Was Short,Because He Was Short,General,Which American clarinettist bandleader was known as 'The King of Swing',Benny goodman,General,Who was the chief spokesman for the lost generation,F scott fitzgerald +General,"In Shakespeare's The Taming of the Shrew , what is the name of the shrew",Katharina,General,What is the fear of machines known as,Mechanophobia,Food & Drink,What drink did Paul Daniels and Debbie McGee advertise singing off-key around a piano ,Heineken Lager  +General,What product sold 330 in the US in its first year,VW Beetle,General,"Who wrote 'Born Free', 'Living Free' & 'Forever Free'",Joy adamson,General,Jesus was born in Bethlehem what does Bethlehem mean,House of Bread +General,There are only two three letter herbs Rue is one what's the other,Bay,General,"Which Explorers Last Words Were ""I Have Not Told Half Of What I Saw""",Marco Polo,General,After how many years marriage do you celebrate your emerald wedding anniversary,55 +General,Astronomer Fred Hoyle coined which phrase,The Big Bang,Science & Nature, The digestive juices of crocodiles contain so much __________ that they have dissolved iron spearheads and 6_inch steel hooks that the crocodiles have swallowed.,Hydrochloric acid,General,What is produced in a ginnery,Cotton +General,U.S. Captials - North Dakota,Bismarck,General,What did little bo-peep lose,Her sheep,General,What day of the week was JFK assassinated on,Friday +Music,"Pat Benatar Asked You To ""Hit Her With Your"" What",Best Shot,General,What nickname did Imelda Marcos share with a heavey metal rock group,Iron butterfly,Science & Nature,In 1937 American Chester Carlson Invented A Process Called Xerography What Do We Know It As Today ,Photocopying  +Sports & Leisure,In Olympic Weight Lifting What Are The 2 Methods Of Lifting ,"Clean & Jerk , The Snatch ",General,Which Shakespeare play has an English placename in its title,The merry wives of Windsor,Music,"Name The Italian Composer That Provided The Scores To The Spaghetti Westerns ""The Good The Bad And The Ugly""",Ennio Morricone +General,"Who composed the title music to TV's ""Inspector Morse""",Barrington pheloung,Science & Nature,What Was The Intel 4004 ,The First Micro Processor ,Music,"What Was Pato Banton's Follow Up to His No.1 ""Baby Come Back"" Reaching No.15",Bubbling Hot +General,What was astronaut edwin aldrin's nickname,Buzz,General,"Football Team, pittsburgh ______",Steelers,General,What trade name was given to the phenol-formaldehyde resin developed as the first synthetic plastic in 1909,Bakelite +General,All American umpires wear what,Black underwear,Religion & Mythology,"In Greek mythology, what animal is associated with Athena?",Owl,General,Who married queen victoria,Prince albert +Music,Who Did Shirlie Drink To Success With In 1987 With The Single Heartache,Pepsi,General,Who was the first male tennis player to win 100 tournaments,Jimmy Connors,Science & Nature,What Is The Longest Living Mammel? ,Man  +General,Which private eye hero did the author Raymond Chandler create,Philip marlowe,Music,"Who Had A 1980 Hit With ""Echo Beach""",Martha And The Muffins,General,Which country became the second to issue postage stamps in 1843,Brazil +Geography,What Is The Capital Of Portugal ,Lisbon , Geography,What country is directly north of Israel?,Lebanon,Food & Drink,What fruits are usually served 'belle helene' ,Pears  +General,Alan Ginsberg is credited with inventing what 60s phrase,Flower Power,General,Envoid was the first what in the USA,Birth control pill available,Geography,Who Were The First Europeans To Reach Mount Kilimanjaro In Tanzania ,"2 Germans , Johannes Rebmann & Ludwig Krapf " +Mathematics,Two angles that total 180 degrees are called _______.,Supplementary,General,Oswestry founded in 1407 is Britain's oldest what,Public School Eton 1440 next,Science & Nature,Which Is The Largest Owl Found In Britain ,Snowy Owl  +Science & Nature, Milk snakes lay about 13 eggs _ in piles of animal __________,Manure,Science & Nature,How many degrees does the earth rotate each hour,Fifteen,General,What is the main ingredient of an edible faggot,Liver +General,"What film starred drew barrymore, mary-louise parker, and matthew mcconaughney",Boys on the side,Entertainment,Who were Lucy and Ricky's next door neighbours and best friends?,Fred and Ethel,Science & Nature,"Work done, equals force multiplied by ________.",Distance +Entertainment,Who produced 'Sgt Pepper's Lonely Hearts Club Band'?,George Martin,General,Which Famous Rock N Roll Band Were Formerly Known As The High Numbers,The Who,General,What is measured in units called jnd,Sensitivity Just noticeable difference +Music,"Name The Composer Who Supplied The Music To The Hitchcock Movies Psycho, North By Northwest & Vertigo",Bernard Hermann,General,In what Australian state would you find Geradlton,Western australia,General,"Which substance, occurring naturally in fruit, causes jams and preserves to set",Pectin +Music,Which group's first top 10 hit was Right Now in 1999?,Atomic Kitten,General,Who was the first man to win the Formula One motor racing championship,Farina,General,As sly as a ______,Fox +General,What is a group of pheasant,Nest,General,Where would you find a pintle,Hinge - it’s the pin holding it, History & Holidays,Who was the second man to set foot on the moon?,"Edwin ""Buzz"" Aldrin" +General,Whose wife was turned into a pillar of salt,Lot's,General,What is the first day of Lent,Ash Wednesday,Geography,Which City Is The Capital Of Pakistan ,Islamabad  +Science & Nature,"As the speed of a body approaches the speed of light, its mass approaches ________",Infinity,General,How many percent of our brains do we use,Ten percent, History & Holidays,The massacre at Kent State occurred as students protested the bombing of Cambodia and the _____ war.,Vietnam +General,"John Wayne called what film ""The most un-American thing ever""",High Noon,Science & Nature,Do You Become Long Or Shortsighted With Age ,Longsighted ,General,German mapmaker Martin Waldseemuller named what,America – after Amerigo Vespucci +General,"The Late Edward Smith ""That Means He's Dead"" Edward Smith Has What Historical Claim To Fame",Captain Of The Titanic, History & Holidays,Who Released The 70's Album Entitled Legalize It ,Peter Tosh ,General,"I'll Be There For You, sung by the Rembrandts, is the theme song of which American television comedy series",Friends +Entertainment,Famous Phrases: Who knows The ______.,Shadow,General,What name is given to a blood vessel which takes blood away from the heart,Artery,Sports & Leisure,In Horse Racing Which Of the 5 Classics Is Run Over The Longest Distance ,The St Leger  + History & Holidays,Who invented the thermometer in 1593? ,Galileo ,Food & Drink, What do you get when you add fresh fruit to red wine,Sangria,General,Septicaemia is better known as _____,Blood poisoning +Food & Drink,French for 'flight in the wind' ,Vol-au-vent ,General,Term applied to the group of plant or animal organs that are necessary for or that are accessory to the reproductive processes (reproduction),Reproductive system,General,What nation has its capital in Zagreb,Croatia +Entertainment,What was the name of Speed Racer's car?,The Mach Five,General,What area of London did Jack the Ripper frequent,Whitechaple,General,Sixteen pounds is the maximum legal weight of what sporting device,Bowling + Geography,What is the basic unit of currency for Finland ?,Markka,General,In Holland it used to take four years to train as what,Hat maker – surgeon only 3 years,Art & Literature,Who Kills Nancy In Dickens Novel Oliver Twist ,Bill Sykes  +General,Who wrote the novel The Money Changers,Arthur Hailey,General,On oometer measures what,Birds Eggs,General,What and where is the longest group of coral reefs in the world,Great +General,What river is represented by the blue stripe on the Gambian flag,Gambia,General,Who is likely to have a faster pulse a man or a woman,A woman,General,1k equals how many bytes,1024 +General,Who was the 16th century physician who revolutionized anatomy by performing post-mortems,Andreas vesalius, History & Holidays,In 1950 which French designer created the so-called New Look ,Chriatian Dior ,General,When was the record breaking flood of the North Sea,1953 +General,Ernest Evans became famous under what name,Chubby Checker,General,"Salad of sliced raw cabbage,carrot and apple",Coleslaw,General,Who had a major hit with joni mitchell's 'both sides now',Judy collins +General,Itamae have what job in Japan,Chef - In Front of cutting board,General,Israel's equivalant to the dollar is ______,Shekel,Technology & Video Games,"In the Mystical Ninja series, who is Goemon's sidekick? ",Ebisumaru +General,Which bird is famous for its ability to walk long distances and for killing snakes by stamping on them with its huge feet?,Secretary Bird,General,What is Alberta's most popular annual events,Calgary stampede,General,"Hugo Quotations: ""The greatest lesson in life is to know that even fools are right sometimes.""",Sir Winston Churchill +General,Mr Doberman developed the breed protection at work - what job,Tax Collector,General,Phasmophobia is the fear of,Ghosts,Mathematics,"Arc, radius, and sector are parts of a(n) _________.",Circle +Geography,What is the capital of Seychelles,Victoria,General,"In ""Ferris Beuller's Day Off,"" who plays the burnout at the police station that Jeanie kisses?",Charlie Sheen,General,What is the language of hungary,Magyar + History & Holidays,Who wrote the book 'The Shining' on which the 1980 Stanley Kubrick film was based ,Stephen King ,General,On which Shakespeare play was the film Forbidden Planet loosely based,The tempest,General,What is a person in his eighties,Octogenarian +General,Who designed the WW 1 plane Camel and co designed Hurricane,Thomas Octave Murdoch Sopwith,Science & Nature,What falls out with phalacrosis?,Hair,General,Division of geologic time in the Cenozoic era following the tertiary period (geology),Quaternary period +General,What is bell metal an alloy of,Tin & copper, History & Holidays,What group landed in America in 1620?,The Pilgrims, Geography,Where is Mount Washington?,New Hampshire +General,"What did Paul Benier leave in his locked getaway car while he robbed a bank in Swansea, Massachusetts",His car keys car keys the car keys,General,What 80's spin off of a 70's tv show did Martin Lawrence play on?,What's Happening Now,General,The word mattress what taken from which language,Arabic +General,Where would you find Argine Esther Judith and Pallas,Pack of cards – Queens names,General,What is the worlds warmest sea,Dead sea,General,"In mythology, which King of Cyprus fell in love with a statue",Pygmalion +Art & Literature,In Which Country Was John Constable Born ,Suffolk ,General,"In an average lifetime, the average american charges $120,875 on _____?",Credit cards,Music,"Who Had A Hit With ""Get Outa My Dreams Get Into My Car""",Billy Ocean +Entertainment,White Room' was a hit off which Eric Clapton album?,Cream,General,What is the flower that stands for: beauty always new,China rose,General,Who was the first person elected to US swimming hall fame,Johnny Weismuller +General,"What happens to Wanda in ""A Fish Called Wanda""?",Wanda is eaten,General,In Gustav Holsts Planets suite what planet is missing,Pluto not known then,General,What animal lives in a form,Hare +General,Which wedding anniversary is coral,Thirty fifth,General,To give full discretionary power,Carte blanche,Toys & Games,This is the lowest ranking suit in Bridge.,Clubs +Science & Nature,To make a car go backwards you have to put it in what gear?,Reverse,General,Atahualpa was the last ruler of who,Incas, History & Holidays,In which year was the Berlin Wall constructed? ,1961  +Sports & Leisure,"Which Football Team Managed To Loose The 1974,1978 World Cup Finals ",Holland ,General,Who did joan collins play in 'dynasty',Alexis carrington,Geography,"What unit of currency will buy you dinner in Iraq, Jordan, Tunisia and Yugoslavia",Dinar +General,What does the electrical abbreviation db stand for,Decibel,General,What are the worlds smallest trees - (not Bonsai),Dwarf Willows (Greenland) 2 inch,General,What did d.w griffith invent,False eyelashes +General,What is a french 'german shepherd',Alsatian,General,Concetta Franconeri became more famous as who,Connie Francis,General,Which seattle-based band had a hit with 'daughter',Pearl jam +Geography,"The state of __________ was once known as the ""Earmuff Capital of The World"". Earmuffs were invented there by Chester Greenwood in 1873.",Maine,General,In the Bible what was the name of the region of natural fertility promised to the Israelites by God,Land of milk & honey canaan,General,Meaning 'Black Knife' in Gaelic what is the dagger worn in the sock with full Highland Dress,Skean dhu +General,"What is the fear of chinese, chinese culture known as",Sinophobia,General,The video game character Mario made his debut appearance in.,Donkey kong,General,Kind of mild pale Welsh cheese,Caerphilly +General,What began in 1877 but banned women until 1884,Wimbledon Tennis,General,"In engineering, the measurement or control of equipment by fluid jet devices.",Fluidics,Geography,What is the capital of Burundi,Bujumbura +General,"The archbishop of krakow, in 1978, came to be known as whom",Pope john paul,General,Colonel Paul W Tibbets did it first - what,Dropped Atom Bomb,Science & Nature, A herd of sixty cows is capable of producing a ton of milk in less than a __________,Day +General,"Which film director's films include ""Midnight Express"" and ""Bugsy Malone""",Alan parker,Food & Drink,Gnocchi is a food from Italy. What is it? ,"Small dumplings made of potato, flour or semolina ",General,What kind of animal is a possum,Marsupial +General,How deep is a fathom,Six feet,General,What would a nidologist be interested in,Birds nests,Sports & Leisure,How many dimples does a golf ball have?,336 +General,Product name from the words Durability Reliability Excellence,Durex condoms,General,"What's the international radio code word for the letter ""O""",Oscar,Music,What Is The Name Given To The Type Of Singing That Mimics An Instrumental Solo,Scat Singing +People & Places,By what name is Allan Stewart Konisburg better known as?,Woody Allan,General,On what do approximately 100 people choke to death every year,Ballpoint pens,Food & Drink,What type of pastry is usually bought frozen in wafer thin slices ,Filo  +General,What is the young of this animal called: Hippo,Calf,General,Who reads skulls,Phrenologist,General,The Braves moved to Atlanta from where,Milwaukee +General,Which Country Has The Longest Coastline In Europe,Norway,General,"In 1902, which volcano erupted killing 30,000 people",Mount pelee,General,Which variety of apple is on the Beatles apple label,Granny Smith +General,Which form of light is used to treat skin diseases,Ultraviolet,Entertainment,Who is married to Valerie Bertanelli?,Eddie Van Halen,General,What was the name of Thomas Jefferson's home,Montecello +General,What is the flower that stands for: counterfeit,Mock orange, History & Holidays,In which body of water is Christmas Island? ,Indian Ocean ,General,In ancient India how were dead parents traditionally disposed of,Eaten by offspring as a sign of respect +General,What beverage named after the UK Prime Minister of the 1830s,Earl Grey Tea,General,What sport can take place on sand ice or water,Wind Surfing,General,Ward Green wrote the story for which famous film,Lady and the Tramp +General,What creature was the early symbol for christ,Fish,General,What is the windiest place on earth,Mount washington,General,"What is the animated videogame by Don Bluth, where the hero had to work his way through a trap infested castle",Dragons lair + Geography,"In which country is K2, the second-highest mountain in the world, located?",Pakistan,General,30 million people in the USA have diasima - what is it,Gap in front teeth,General,What is the estimated weight of the great pyramid of Egypt,"6,648,000 tons" + History & Holidays,"After The Fire made 'Der Kommissar' popular, but which eighties musician performed it originally? ",Falco ,General,Shirley Schrift became famous as which actress,Shelly Winters,General,U.S. Captials - South Carolina,Columbia +General,In which 1975 smash hit film did the male star own a fishing boat called Orca,Jaws,General,What note has a time value of two crotchets,Minim,Science & Nature,What does the symbol 'Am' represent?,Americium +Entertainment,What is Blondie's maiden name?,Oop,Science & Nature,This is the hardest naturally occurring substance.,Diamond,General,What is quicksilver better known as,Mercury +General,Of what was the first lightbulb filament made,Cotton,Music,Which Group Released An Album Called “Long Road Out Of Eden” In 2007,The Eagles,General,Gin - lemon Juice - Sugar - Soda make what cocktail,Tom Collins +General,What profession had Lemual Gulliver when he was shipwrecked,Ships surgeon,General,Selim Zilkha Is The Founder Of Which Large Chain Of Stores?,Mothercare,General,A receptacle for holy water is a(n) ____.,Font +Geography,"_______________ has 254 counties. Alaska, which is more than twice as large, hasn't any.",Texas,General,"In legend, who was the Roman goddess of war",Bellona,General,Tropical plant with large flowering bracts,Bougainvillaea +General,What pigment affects the colour of the hair & skin,Melanin, History & Holidays,The Worlds First Travel Agency Was Founded In 1850 By Whom? ,Thomas Cook ,General,Which American state passed a bill declaring Pi to be 3 ?,Indiana +General,What's pennsylvania's state tree,Mountain laurel,General,"In Greek mythology, mnemosyne is the mother of the ______",Muses,General,What painting depicts the sister & the dentist of artist Grant Wood as rural farm folk,American gothic +General,Who played the mutating fly in the film 'the fly',Jeff goldblum,Science & Nature, The crayfish isn't a fish at all _ it is related to the __________,Lobster,General,Singapore uses the colours blue and yellow at funerals to ward off ______,Evil spirits +General,"In siberia in 1994, a container full of what was discovered in the 2,000 year old grave of a scythian princess and priestess",Marijuana,General,Indiana jones: what creature did indy's father fear,Rats,General,Followers of the unification church are nicknamed _______,Moonies +Geography,Mount Victoria is the highest peak of this island country.,Fiji,General,In Saskatchewan it is illegal to watch what if drinking booze,Strippers – Exotic Dancers,General,What was the name of king arthur's castle,Camelot +Music,"""Baker Street"" Was A Hit In 1992 Who Sang It",Undercover,General,In Star Fleet Will Riker plays which musical instrument,Trombone,Entertainment,The Who' had a guiness world record for what?,Loudest Band +General,Who surpassed George Halas for most career NFL coaching victories in 1993,Don shula,People & Places,Who Was Elvis Presleys Manager ,Colonel Tom Parker ,General,The aardvark is the first animal in the dictionary what's second,Aardwolf +Music,Cherilyn Sarkisian Lapiere Is The Real Name Of Which Singer,Cher,Food & Drink,What Do The Initials U.H.T Refer To In Relation To Milk ,Ultra Heat Treated ,General,Where is mount augustus,Western australia +General,Are most cats right pawed or left pawed,Left,Science & Nature,Who First Demonstrated Television In 1926 ,John Logie Baird ,General,"What Indian word means ""big village""",Canada +General,What northern nation's geyser heated soil allows it to grow bananas,Iceland's icelands iceland,General,An accolade is something of praise what was original meaning,Shoulder sword touched knighting,Music,The Duo Erasure Consisted Of Vince Clark And Which Other Singer?,Andy Bell +General,Who was the eighties group that was named after the inventor of the radio?,Tesla,General,In astronomy what are Pallas Vesta and Davida,Asteroids,General,In the creation myth on the fourth day God made what,Sun Moon Stars +General,Brave New World - Aldus Huxley - where name from,Shakespeare's The Tempest,General,In 1829 Cyrill Damien invented which musical instrument,Accordion,General,Hamburgers were invented in what country,China +Science & Nature, There are about 40 different __________ in a birds wing.,Muscles, Geography,Which island country lies immediately to the East of Réunion?,Mauritius,General,What was the name of long john silver's parrot,Captain flint +General,"Who wrote ""The Corn is Green""",Emlyn williams,General,Nigel Neil created which famous UK Professor in the 50s,Professor Quatermass,General,Of what is cetology a study,Whales +General,"In common: nova, comet, galaxy, meteor",Cars named after stellar bodies car,General,Which famous couple were married in 1981,Charles and diana,General,"What did dr john pemberton invent in atlanta, georgia in 1886",Coca cola + Language,From what Irish words is 'Dublin' derived?,Dubh linn, Geography,In which state is the Kennedy Space Center?,Florida,General,What does a hotwalker do,Walks a hot racehorse +General,Two villains first appear in Batman Comics 1 - Joker and who,Cat - Later called Catwoman,General,"If you were anosmic, what would you lack",Sense of smell,Art & Literature,"Which Famous Book Begins With The Line 'Not long ago, there lived in London a young married couple of Dalmatian dogs named Pongo and Misses Pongo' ",101 Dalmations  +Entertainment,Mickey Mouse is known as what in Italy?,Topolino,Technology & Video Games,What happened to the innocent residents of the Mushroom Kingdom when Bowser Koopa took over? ,They became bricks and powerup blocks,Science & Nature,What is the name given to the green pigment in plants ,Chlorophyl  + Geography,What is the capital of Armenia ?,Yerevan, History & Holidays,"Who plays the character of Chris Kringle aka Santa Claus, in the 1994 film 'Miracle On 34th Street'' ",Richard Attenborough ,General,Who wrote The Unbearable Lightness of Being,Milan kundera +General,What is the more common name for the bird also known as the windhover,Kestrel,General,What is the fear of wet dreams known as,Oneirogmophobia,General,"In which tv series do james van der beek, katie holmes, joshua jackson and michelle williams play",Dawson's creek +General,What is the fear of flying known as,Pteromerhanophobia, History & Holidays,In which year did the demolition of the Berlin Wall begin? ,1989 ,General,What monster is said to be living in a Scottish lake,Loch ness monster +General,What is the name for the study of liquid flow,Hydraulics,General,In heraldry what shape is a pile,Inverted Pyramid,General,France has the highest per capita consumption of ______,Cheese +General,Where is the old quarter 'plaka',"Athens, greece",Music,"The Song 3 Men And A Baby Featured A Song entitled ""Back In Time"" By Which Band",Big Audio dynamite,General,When was the miss world competition founded,1951 +General,Fundador is a potent brandy made in what country,Spain,General,Which Famous Woman In The World Of Politics Was President Of The Oxford Union In 1977,Benazir Bhutto,General,"The Triassic, Jurrasic, and Cretaceous periods make up which era ?",Mesozoic Era +Sports & Leisure,"Kevin Keegan & Henry Cooper Both Have A Connection , What Is It ",Brut 33 ,General,"What was the name of the bartender on ""The Love Boat""?",Issac,People & Places,"What do Cleopatra, Margarett Thatcher, & Mia Farrow all have in common ",They All Had Twins  +General,What are the fundamental constituents of tissue building drugs,Anabolic steroids,General,In traditional wedding anniversaries what is given on the eighth,Bronze,Science & Nature,A non-cancerous tumor is said to be _______.,Benign +General,Who would wear a diadem,Kings headband,Sports & Leisure,What Colour Socks Do Chicago's Baseball Team Wear? (Chicago White Socks) ,White ,Science & Nature,Which metal was invented by British metallurgist Harold Brearley in 1912? ,Stainless Steel  +General,On The Drew Carey Show what foreign country did Mimi send Drew to?,China,General,How did Attila the Hun die on honeymoon,Booze – Honeymoon 30 day booze up,General,To which London club did Mycroft Holmes belong,Diogones +General,Where is Selfridges?,"Oxford Street, London",General,Acrophobia is a fear of ___________,Heights,General,Which character lived at 3 stable mews City of London,John Steed in the +General,Which Englishman Was Famously Beheaded On The 29 th October 1618 ?,Sir Walter Raleigh,Sports & Leisure,In which sport would you use the Eskimo Roll? ,Canoeing ,General,Which artist painted 'Guernica',Picasso +General,The first International floodlight football match took place in what year,1955,General,"Which group backs James Brown, the self-styled 'Godfather of Soul'",The famous flames,Science & Nature," Marine iguanas, saltwater crocodiles, sea snakes, and sea turtles are the only surviving seawater_adapted __________",Reptiles +General,"Three of the following U.S. Presidents appeared at one time or another on what's my line which one did not Jimmy Carter, Gerald Ford, Ronald Reagan, George Bush",George bush,Food & Drink,Good Rhine wines are bottled in what colour bottles?,Brown,General,Fifty who had the nickname 'golden bear',Jack nicklaus +General,In brewing what do the initials OG stand for,Original Gravity,General,Who was the original lead singer with the Moody Blues,Denny laine,General,What is the common name for the aurora borealis,Northern lights +General,Which German firm produced the World War Two plane the Condor,Fockewulf, History & Holidays,What Venetian traveler and explorer landed in China and reached Kublai Khan's court in 1275?,Marco Polo,General,What florentine held that the ends justified the means,Machiavelli +Science & Nature,The pivot point of a lever is called the _________.,Fulcrum,General,In which country is the secretariat of the European Parliament,Luxemburg,General,What bird has double-plumed feathers,Emu +General,The Salvation Army was founded in what year,1865,General,Who invented the exploding shell,Henry shrapnel, History & Holidays,"Samhain', literally means 'end of summer' and is a Gaelic language word. What is it's direct English equivalent ",November  +General,"Who, posthumously, was the Formula 1 Drivers' World Champion in 1970",Jochen rindt,General,Who invented Lava Lamps?,Craven Walker,Geography,What is the capital of Venezuela,Caracas +Food & Drink,What Type Of Cheese Was Traditionally Made From Buffaloes Milk? ,Mozzarella ,General,In James Bonds books what was Dr No's first name,Julius,Entertainment,"What was the setting for ""The Sound of Music""?",Austria +General,The normal temperature of a cat is _____ degrees (it's a decimal),101.5F,General,Principial male dancer.,Premieur danseur,General,Francis What prime-time soap opera debuted as a five-part miniseries in 1978,Dallas +Science & Nature,What is the chemical symbol for lead?,Pb,Entertainment,What's the name of B.B. King's guitar?,Lucille,General,What is the Capital of: Slovenia,Ljubljana +Mathematics,What is the minimum number of integer degrees in an obtuse angle?,91,Science & Nature,Which Member Of The Cat Family Cannot Retract Its Claws? ,The Cheetah ,General,What fabled monster has a lions head and a serpents tail,Chimera +General,"What is defined as ""The fruit of the oak, beech, chestnut, and other forest trees, on which swine feed""",Mast, History & Holidays,"What french port did 200,000 british troops flee on june 4th 1940 ",Dunkirk ,General,Who proclaimed thanksgiving a national holiday in 1863,Abraham lincoln +General,Logophobia is the fear of,Words,General,What links Grey Bean Canada Brent Barnacle,Types of Goose,General,"The Brodway version of Disney ""The Lion King"" uses more than ___________ puppets ",232 +General,The largest rough diamond is a?,Cullinan,Food & Drink,What is the name of the fruit sauce which is a traditional accompaniment to the Christmas Turkey? ,Cranberry ,Science & Nature,What are the units of measurement for Power ?,Watt +General,What is recorded with two beams of light,Holograph,General,In 1981 James Brady was shot attempting to assassinate whom,Ronald reagan,Science & Nature,On the Moh Hardness scale what has a hardness of 10?,Diamond +General,In Happy Days what was Potsies full name,Warren Webber,Sports & Leisure,Football: The Dallas ______?,Cowboys,Art & Literature,Which Author Wrote The 'Just So Stories''? ,Rudyard Kipling  +General,What line of latitude separates the Slave States form the Free States of the USA?,Mason-Dixon Line,General,Under what name did norma jean mortenson become famous,Marilyn monroe,Entertainment,Who discovered gold on the Witwatersrand?,George Harrison +Food & Drink,Which fruit has the scientific name of malus pumulia? ,The Apple ,General,This TV series ran for 78 episodes before it was scrapped - what,Star Trek,Science & Nature,What travels in gaggles,Geese +Science & Nature,What Do You Call A Birth Where A Baby's Feet Are Delivered First ,Breech Birth ,Entertainment,"What does Steven Spielberg try to include in every film he makes, claiming it is for luck?",Scene With A Shooting Star,General,What is the name of the largest South American lake,Lake maracaibo +General,In which American city can you get doctorate in hambugerology,Hamburger College – Chicago,General,What is a tennis shot hit in the air when close to the net,Volley,General,What was the first name of Captain Dobey on Starsky & Hutch?,Harold +Music,The Young Ones Was A Hit On Both Screen & Vinyl For Which British Legend,Cliff Richard,General,In what fictional vessel are characters Starbuck Stubb Fedallah,Pequod in Moby Dick,General,Where or what is a birds lore,Space between eye and beak + Geography,What is the basic unit of currency for Honduras ?,Lempira,General,Who designed the union buildings,Sir herbert baker,General,Who wrote Cliff Richards hit song Living Doll,Lionel Bart +General,"What term originally menat instruction in all branches of knowledge, or a comprehensive education in a specific subject",Encyclopedia,General,Where would you find Lunate Triquetral and Hamate,Bones in Wrist,Food & Drink,Which Fast Foon Advertised With The Slogan 'Wheres The Beef'' ,Wendy's  +Music,"Which Beatles Son Had A Hit With ""Too Late For Goodbyes""",John Lennon (Julian),Sports & Leisure,On Which Day Of The Week Was The 1997 Grand National Contested? ,Monday (Due To Bomb Scare) ,General,"Countries of the world: north western Africa, Nouakchott is the capital",Mauritania +General,What is the cube root of 64,Four,General,In wacky races who drove the converter car,Professor Pat Pending,General,As who is Cassius Clay now known,Mohammed Ali + History & Holidays,Who Released The 70's Album Entitled The Grand Tour ,George Jones ,General,Who was the actor who played lt columbo in the series columbo,Peter falk,General,A prostitute with wealthy or upper class connections,Courtesan +Sports & Leisure,Which british club became the first to have an all seater stadium? ,Aberdeen ,General,"Which Now Famous A List Hollywood Celebrity Appeared Briefly In The Video To The Paula Abdul Video ""Rush Rush""",Keanu Reeves,General,All Sikhs must possess five things - one is a Kangha - what is it,Comb +General,What two words make the word 'meld',Melt and weld, History & Holidays,"The Simpsons Halloween Episodes are an annual tradition in which there are three separate, self-contained pieces. By which title are these episodes known ",Treehouse of Horror ,General,What is the fear of being buried alive known as,Taphephobia +General,What two items make up the dish devils on horseback,Bacon Prunes,General,What did Jack Horner pull from his pie,Plum,General,Where is the 'whispering gallery'?,St. Paul's Cathedral +General,What are Arran Pilot Homeguard and Ulster Chieftain,Early types of Potato, History & Holidays,"On november, 16, 1907, oklahoma became the what number state ",46th ,General,What is the most populous domesticated creature in the US,Honey Bees +General,With which part of the body is dermatology concerned,Skin,Technology & Video Games,"Who wrote the song, ""Pac-Man Fever?"" ",Buckner and Garcia,General,If you were eating calemare - what are you snacking on,Squid +Food & Drink,"What name was given to the light, clear wine developed in medieval times along the coastal valleys of the Gironde river in the Bordeaux region? ",Claret ,General,"Who said ""Sometimes a cigar is just a cigar""",Sigmund Freud,General,What is a cyclone,Area of low pressure +General,What is the correct name for brown coal,Lignite,General,Who rejected the role of Riddler in Batman Forever,Robin Williams,Music,Who Released Their 2nd Album Rhythm & Stealth In 1999,Left field +General,Author of Good as Gold and Closing Time but famed for another,Joseph Heller Catch 22,General,Who is Julia Wells Better known as?,Julie Andrews,General,Marine creature with tusks,Walrus +Sports & Leisure,How Many Successive Wimbledon Titles Did Bjorn Borg Win ,5 ,General,What does the C stand for in the equation E=mc2,Speed of light,Sports & Leisure,Which is the only English football league team that has a name ending with the letter G? ,Reading  +Entertainment,An adventurous penguin named Tennessee Tuxedo had a sidekick named _______?,Chumley,General,What cult-fav eighties movie features John Lithgow from another dimension?,The Adventures of Buckaroo Banzai,General,Ecophobia is a fear of what,Home +Music,"Which Song From ""The White Album"" Was Originally Entitled ""Maharishi"" ?",Sexy Sadie,General,This stone enabled scholars to decipher Egyptian hieroglyphs,Rosetta stone,General,The fastest bird is a spine tailed swift. How fast can it fly (mph),106 +General,What is in the Red Data Book,Endangered Species,People & Places,Who is Paul Mc-Cartneys Fashion Designer Daughter ,Stella McCartney ,Science & Nature,What Is The Slowest Fish? ,The Sea Horse  +General,"What is tramp's nickname for lady in ""Lady and the Tramp""",Pidge,Entertainment,"In 1968, who released 'Carnival of life' and 'Recital'?",Lee Michaels,Toys & Games,"If you're involved in firing, throwing, and glazing, what do you do",Pottery +General,"The first international cricket match ever, was held between canada and _____",U.S.A.,General,What is the young of this animal called: Quail,Cheeper,General,Who dubbed australia 'the lucky country',Donald horne +General,Literature Scramble: lxrnaadee lzynsshietno (Author of Gulag Archipelago),Alexander Solzhenitsyn,General,Darwin Who owns: Head and Shoulders shampoo,Procter and gamble,General,"Which colourless, odourless light gas is used to lift airships",Helium +General,Which sailor dreamed of Toasted Cheese in Treasure Island,Ben Gunn,General,The Pindus mountains run north to south through which country,Greece,Sports & Leisure,Which record breaker won the first major Marathon she entered in 1996? ,Liz McColgan  +General,"In 1900, what was the average lifespan in the u.s",Forty seven,General, This instrument measures the velocity of the wind.,Anemometer,General,Billy the Kid was born in _____,1859 +General,Japanese profesional hostess and entertainer,Geisha,General,Boston Bruins hockey great _____ was inducted into the hall of fame in 1947,"Aubrey ""dit"" clapper",General,What is the Capital of: Nicaragua,Managua +General,Legal Terms: To steal property entrusted to one's care.,Embezzle,General,What does 'cc' stand for in motor mechanics,Cubic centimetre,General,What is the name of the Chicago baseball team based at Wrigley Field,Chicago cubs +General,Orange juice and what make an ambassador,Tequila,General,What was the name of the England team's now infamous 'goal celebration' performed at the 1996 European Championships in England?,The Dentist's Chair,General,In 1925 the worlds first what opened Luis Obispo California,Motel - Called Motel Inn +General,What system is based on the number 10,Decimal,Food & Drink,"What cocktail: Vodka, Galliano and Orange Juice? ",Harvey Wallbanger ,General,What is the only venomous british snake,Adder +General,Who was known as the clown prince of basketball,Meadowlark lemon,General,What word appears above George Washington's head on a quarter,Liberty,Music,"Which Big Voiced Singer Is Usually Associated With The Irving Berlin Song ""Theres No Business Like Showbusiness""",Ethel Merman +General,What is the maximum number of clubs a golfer may use in a round,Fourteen,General,In 1950 the Minnesota valley canning company became what,Jolly Green Giant,General,Which South African President was assassinated in 1966,Hendrik verwoerd +General,"On the fahrenheit scale, there are 180 degrees between boiling point and ______",Freezing point,Sports & Leisure,Who Was The First Boxer To Receive A Knighthood ,Sir Henry Cooper ,General,An Italian vinegar matured in wooden barrels,Balsamic +General,U.S. Captials - Illinois,Springfield,Geography,Which European country has the highest population density,Monaco,General,What is examined by an otoscope,The ear +General,What is the fear of politicians known as,Politicophobia,Science & Nature,Smuck is the collective noun for which marine creature? ,Jelly Fish ,Music,Which Band Accompanied Vic Reeves On His Chart Topper “Dizzy”?,The Wonder Stuff +General,The only us state that celebrates its own independence day,Texas,General,Who played Deanna Troi in 'Star Trek The Next Generation'?,Marina Sirtis,General,"What Ohio city is known as ""the rubber capital of the world""",Akron +General,What is the longest English word that only has one vowel,Strengths,General,Fallstaff first appears in what Shakespeare play,Henry IV part 1,General,What is the opposite of wet - if its not wet,Sweet +Art & Literature,"""The Diary of Anne Frank"" was first published in English under what title",The diary of a young girl,General,When was the shortest war in history,1896,General,Kleptophobia is the fear of,Stealing +General,What was the original name of Duran Duran?,RAF,General,Auctioneer's or judge's hammer,Gavel,General,What word originally meant a dark cosmetic eye powder,Alcohol from Al Kuhul antimony +General,Liza Minelli played what character in Cabaret,Sally Bowles,General,What is the first name of Agatha Christies Miss Marple,Jane,People & Places,Which Tennis Player Was Jimmy Connors Engaged To ,Chirs Evet  +General,In Raton New Mexico its illegal for a woman to ride wearing what,A Kimono,Music,The Song West End Girls Was A Number One Hit For Which Famous Pop Duo,The Pet Shop Boys, History & Holidays,Who Released The 70's Album Entitled Lark's Tongue in Aspic ,King Crimson  +Geography,What is the second largest of the United States,Texas,Music,Name Iron Maiden's famous mascot (depicted on the cover of Sanctuary standing over Margaret Thatcher's decapitated body)?,Eddie,General,"In Gaelic legend, who had a dog called Bran",Fingal +General,In the English legal system there are three Divisions in the High Court of Justice. One is the Family division. Name the other two,Queen's bench & chancery,General,What is the capital of the state of Virginia,Richmond,General,"Who played Sarah Connor in 1984's ""Terminator""?",Linda Hamilton +Science & Nature, Crabs and other crustaceans can escape danger by simply discarding an injured or trapped __________,Limb,Music,Which Member Of The Velvet Underground Produced Patti Smith's Debut Album Horses,John Cale,Music,"Which British Pop Star Named His Son After The Singer ""Sitting On The Dock Of The Bay""?",Bryan Ferry (Otis) +General,What sort of meat is used in the dish Guard of Honour,Neck of Lamb,Science & Nature,What Is The Soft Spot On A Babies Head Known As ,The Fontanelle ,Music,Name Buddy Holly's Texas Home Town,Lubbock +General,Which actor's real name is Emmanuel Goldenberg,Edward g robinson,General,In what country was aspirin invented,Germany,General,How did Virginia Woolf die,Committed suicide +General,How often does a quotidian thing occur,Daily,General,What Spanish title is equivalent to princess,Infanta, History & Holidays,When was D-day?,"June 6th, 1944" +Food & Drink,The geometric shape found on the Bass Pale Ale bottle,Triangle,Sports & Leisure,Name The Horse That Won The 2007 Grand National ,Silver Birch ,General,Toxicophobia is a fear of ______,Poison +General,What was the Grammy album of the year in 1967 (Full name),Beatles Sergeant Peppers,Geography,Name the continent that consists of a single country. ,Australia ,General,"On which story by Arthur C. Clarke was the film '2001 - A Space Odyssey"" based",The sentinel +Art & Literature,Who Penned The 1999 Autobiography (Managing My Life)? ,Alex Ferguson ,General,Bamboo harvester was the real name of what TV character,Mr Ed, History & Holidays,Which 1988 film saw Bruce Willis battling against a group of terrorists that crudely interrupted a Christmas party ,Die Hard''  +General,Off what coast was 'prison island',French guiana,Music,With Which Band Did Wyclef Jean & Lauryn Hill First Make Their Name,The Fugees,General,The highest temperature ever recorded occurred in Libya in 1922. What was the temperature (fahrenehit),136 +Entertainment,In 1962 Chubby Checker had a hit with a pop song and novelty dance that remains famous today. What was that dance?,The Twist,General,Pompeii was buried by Vesuvious in AD 79 - what other city,Herculaneum,General,The FEI govern what sport,Equestrian +General,Who invented the saxophone,Adolphe sax, History & Holidays,Rorke's Drift was a battle in which war?,The Zulu War,Music,Which Group Was Put Together For A TV Programme Based In Miami,S Club 7 +General,In which European country is Romansch one of the official languages,Switzerland,General,Which magazine was founded by the American Dewitt Wallace,Reader's digest,General,Who directed the film A Fistful of Dollars,Sergio leona +General,Hills and ridges composed of drifting sand are known as _____.,Dunes,General,The two_minute storm in the opening of Disney's ___________________(1989) took ten special effects artists over a year to complete. ,The little mermaid,General,Who was the girl in Peter Seller's soup,Goldie hawn +General,A Ten pound note depicts a scene from which Dickens novel,Pickwick papers,General,Who was the first host of truth or consequences,Ralph edwards, Geography,"Which country borders Italy, Switzerland, West Germany, Czechoslovakia, Hungary, Yugoslavia, and Liechtenstein?",Austria +Science & Nature,In Computing What Does The Word Modem Stand For ,Modulate Demodulate ,General,Who sings and plays the theme song for the tv show 'frasier',Kelsey grammar,General,What character did Disney add to Winnie the Pooh not in books,Gopher +General,What is the oldest country in Europe,San Marino 301 ad,General,Whose Pepsi commercial was dumped after complaints from Christian groups,Madonna's,General,Who is credited with inventing television,John baird +General,In which country are 'fajitas' a traditional dish,Mexico,General,"In 65 A.D., which Roman emperor forced his former tutor, Seneca the Younger, to commit suicide",Nero,General,"What, in Australian English is a coolabah",Tree +General,Addictive drug prepared from morphine,Heroin,General,Kosmikophobia is the fear of,Cosmic phenomenon,Music,"Who Had A Hit In 1981 With ""Mustang Sally""",The Commitments +People & Places,Which Cricketing Bad Boy Is Known As 'The Cat' ,Phil Tuffnel ,General,What is the fear of society known as,Sociophobia,General,Chysoprase is a shade of what primary colour,Red +General,What is a Winter Banana,A variety of Apple,General,What is the second bridge built across the thames,Westminster bridge,Sports & Leisure,Within Which Sport Might You Encounter The Cyclops System ,Tennis  +General,What is the technical name for the twisting of a stalk as it grows in response to a stimulus from a particular direction,Strophism,Music,"The First Version Of ""Je T'aime""___. Moi Non Plus (Suppressed Before It Could Be Released) Was Recorded As A Duet Between Serge Gainsbourg And Which Actress",Brigette Bardot,General,The first atom bomb was dropped on which Japanese city,Hiroshima +General,What is a group of this animal called: Hare,Down husk,General,In what country is the car model the Treka produced,New Zealand,General,Before it meant tall building what did skyscraper mean,Tall sailing ships masts +Sports & Leisure,"Who was the NBA's most valuable player in 1976, 1977 and 1980?",Kareem Abdul-Jabbar,Art & Literature,"Which US dramatist was once married to Marylin Monroe and penned the plays ""Death Of A Salesman"" and ""The Crucible""?",Arthur Miller,Music,Who's First Top Ten Hit Was “Harvest For The World” In 1998?,The Christians + History & Holidays,"Gangster Al Capone, boss of the Chicago underworld, was finally gaoled for 11 years for what crime?",Tax evasion,General,Where is the dalmatian coast,Croatia,General,2 Bands Signed Their Very First Recording Contracts On The Day Elvis Died Name Either,Kiss / The Buzzcocks +General,What war lasted from june 5 to june 11 1967,Six day war,Sports & Leisure,Where were the 1924 Olympics held ?,"Paris, France",General,"Over here we have a current account with a bank, what do the Americans call it",Checking account +Science & Nature,What chemical compound causes pain in muscles after exercise?,Lactic acid,People & Places,What Did Charles Dawson Appear To Have Found On Piltdown Common ,The Missing link , Geography,What is the capital of Wales??,Cardiff +Music,"What Was The First Record To Chart For US Supergroup ""The Eagles""",One Of These Nights,General,What is the sum of 95 x 1,95,Food & Drink,"If you wanted to buy liver sausage in the US, what product name should you look for? ",Liverwurst  +General,What are the Aki - Dai-shimizy - Seikan in Japan,Tunnels,General,In British army what ranks between Major General and Colonel,Brigadier,General,The word athletics comes from the Greek athlos meaning what,Contest + History & Holidays,3 Singers Have Sung The Opening Line The Christmas Favourite Do They Know Its Christmas (PFE) ,"Paul Young, Kylie Minogue, Chris Martin ",General,Thames water removes a ton of it monthly from sewage – what,Pubic Hair – system cant handle it,General,On which continent would you be standing if you were visiting the Republic of Surinam,South america +General,"Which independent family company is Britain's oldest brewer, having been brewing on the site in Faversham since 1698",Shepherd neame,General,What was the title of the winning song in the 1997 Eurovision Song Contest,Love shine a light,General,"Who said - ""The mass of men lead lives of quiet desperation""",Henry Thoreau +General,"What lake is approximately 394,000 sq km",Caspian sea,General,What is the Capital of: Burma,Rangoon yangon,General,Which is the only middle eastern county without a desert,Lebanon +General,What ship did jason sail on,Argo,Geography,Which Capital City Might You Associate With A Statue Of A Mermaid ,Copenhagen ,General,What country has the car registration letter T,Thailand +General,What's the capital of Uruguay,Montevideo,Music,"Although Successful, What Run Did ""Penny Lane"" Break For The Beatles & Why",A String Of 7 No.1's Preceeded It - (2),General,"Where, in 1955, was one of the worst accidents in motor racing history, when 82 spectators were killed",Le mans +General,"Aesthetic principles derived from the literature and art of ancient Greece & Rome, but found as ideals in all ages is called?",Classicism,General,Name both rival gangs in West Side Story,Sharks Jets,General,Which Composer Wrote The Classic Waltz 'The Blue Daube',Johann Strauss +Food & Drink,What is the brewery featured in Coronation Street? ,Newton and Ridley ,Music,Who Didn't Eric Clapton Shoot In 1974,The Deputy,General,What has 336 dimples,Golf ball +General,What is the third month of the year,March,General,"The grass on the prairie can be eaten up by 27 cattle in 6 weeks. If there are 23 cattle, the grass can be eaten up in 9 weeks. If there are 21 cattle, how many weeks would it take for the grass to be eaten",12 weeks,General,What do fennel leaves taste of,Aniseed +Music,Who were Paul Humphries & Andy Mc-Cluskey Better Known As,OMD,General,Who wrote the novel The Piranhas,Harold Robbins,General,"The spaceship voyager found geysers on triton, a moon of which planet",Neptune + History & Holidays,What Process Is Used For Dating Ancient Organic Objects? ,Radio Carbon Dating ,General,Nut used to make marzipan,Almond,General,"What does the lady in the cafe order after Sally finishes proving to Harry that he can't tell the difference between a real and a faked orgasm in ""When Harry Met Sally""?",I'll have what she's having +General,In the rules of golf what type of bad weather can stop play,Only Lightning,General,What is the capital of the African country of the Ivory Coast,Abidjan,Music,Which Patti Smith Album Featured A Cover Shot By Controversial Photographer Robert Mapplethorpe,Horses +Food & Drink,How many bottles of `champagne' are there in a Nebuchadnezzar? ,20 Bottles ,General,Which secret society refers to God as 'the great architect of the Universe'?,Freemasons,General,Mel gibson starred in the film version of which play that grossed the most,Hamlet +General,What is the twelfth month of the year,December,General,England its illegal for a boy under 10 to see a naked what,Mannequin,Geography,What is the capital of Tuvalu,Fanafuti + History & Holidays,What Year Saw The Imposition Of Martial Law In Poland ,1918 ,General,Which celebrity ran the equivalent of 43 marathons in 52 days in 2009?,Eddie Izzard, Geography,What is the capital of Cyprus?,Nicosia +General,A 'bunya-bunya' tree is native to which country,Australia,General,A haboob creates what,Sandstorm – Desert Wind,General,Type of glazed earthenware,Delft +General,Where did the Mafia originate ?,Sicily,Music,Which 1983 Duet Featuring Paul McCartney & Michael Jackson Has A 3 Word Title?,Say Say Say,General,Playing cards in which the pips are part of an art design are called this.,Transformation cards +General,In what film did Mary Poppins - Julie Andrews bare her nipples,S.O.B.,General,U.S. Captials - Kansas,Topeka,General,When did the series 'lost in space' premier on cbs,1965 + History & Holidays,Who was the female Prime Minister of England throughout the eighties? ,Marget Thatcher ,General,Which pretender to the English throne was hanged in 1499,Perkin warbeck,General,"When the Hoovers did not want to be overheard by White House guests, they spoke to each other in what language",Chinese +General,What was the. first bird that Noah sent from the Ark,Raven,Food & Drink,Which cereal is the only one grown standing in water? ,Rice , History & Holidays,Which English King Was Given The Title Defender Of The Faith By Pope Leo X? ,Henry VIII  +General,The Balfour Declaration by Great Britain was in what year,1917, Geography,What is the basic unit of currency for Macedonia ?,Denar,General,All inhabitants of Pitcairn island belong to what US religion,Seventh Day Adventists +General,"Who was Poet Laureate from 1843 to 1850, during which period he wrote no poetry at all",William wordsworth,General,What in the USA is celebrated on the fourth Thursday in November,Thanksgiving,Music,Which U2 Music Video Featured The Band Working Round The Streets Of Las Vegas,I Still Havent Found What Im Looking For + Geography,In which country is the Machu Picchu?,Peru,General,Who was the first baseball player to have his number (4) retired,Lou Gehrig,General,Stanley Burrell became famous as who,MC Hammer +General,Which drink did Bach enjoy so much he wrote a cantata for it,Coffee,General,Belgian and Swiss unit of currency,Franc,General,Where is the black forest,Germany +General,In the UK at least which products selling feature is a magic inch,E A Careys Pipes,General,What are the only numbers where they are the values of the numbers of factors they have,1 & 2,Art & Literature,What is the name of Hamlet's tragic admirer?,Ophelia + History & Holidays,The following is a line from which 1970's film 'There're all gonna laugh at you' ? ,Carrie ,General,Which country grows the most sugar,Brazil,General,Whose stories were illustrated.by 'Phiz',Charles dickens +General,The Associated Powers - the original proposed name of what,The United Nations,General,"In the movie ""Tootsie"" what was the name of the woman who was played by cross-dressing Dustin Hoffmann?",Dorthy Michaels,General,What is a Roman Catholic devotion consisting of prayers or services on nine consecutive days called,Novena +General,What kind of drum is beat beat in the first line of night and day,Tom-tom tom tom,General,Venus is the only planet that does what,Rotates Clockwise,General,What country does the Galapagos Islands belong to,Ecuador +General,"Which Movie Tough Guy Drove A Car That Had The Licence Plate ""Awsome 50""",Cobra,General,An animal that eats both plants and animals is known as an,Omnivore,General,Who was the only astronaut to lose his spacecraft,Gus grissom +General,A narrow crack or split,Fissure, Geography,What is the capital of Barbados ?,Bridgetown,General,What's the longest river in the british isles,Shannon +General,What is the ancient Chinese art of placement called,Feng shui,Geography,What is the capital of Mexico,Mexico city,General,In 1956 David Bryant became the first World Champion in which sport,Bowls +General,"In September of 1994, 852 people died when the ferry Estonia sank in what sea",Baltic Sea,General,Which US president was portrayed by Anthony Hopkins in a 1995 film?,Richard Nixon,General,What is a 'hotel-dieu' in france,Hospital +General,Not as soups what have gazpacho - vichyssoise in common,Served Cold,General,A more common name for an anthrophagist is,Cannibal,General,What film was a California cinema showing when it went on fire,The Towering Inferno +Music,How Many Violin Concertos Did Beethoven Write,One,General,What is the birthstone for september,Sapphire,General,Who was the first test-tube baby,Louise joy brown +General,What did 'Enigma' return to in January 1994,Innocence,General," What is a ""somnambulist""",A sleepwalk er,General,Middle of the Road' was recorded by which group in 1984,Pretenders +Music,"Which album is ""Only A Nothern Song"" on?",Yellow Submarine, Geography,In which continent would you find the Volga river ?,Europe,General,Canadians are sometimes thought of as,Canucks +General,What was the name of the robot on Buck Rogers?,Twiggy,General,What animal is a musophobic afraid of,Mice,General,What is the flower emblem of Wales?,Daffodil +General,Which beer was advertised as good for you,Guinness,General,What is the national symbol for India?,Lotus flower,General,Who wrote To Kill A Mockingbird,Harper lee +General,"The statue of which famous Englishman stands in the""Piazza of the British Library",Sir isaac newton,Science & Nature,Where Does Edelweiss Grow ,In The Alps At High Altitude ,General,What item were originally called Hanways,Umbrellas +General,Intense aerial bombing is called what,Saturation,General,Which U.S. state has the most vehicles and highways per square mile,New jersey,General,Who was the mother of Castor and Pollux,Helen of Troy +General,Antipater of Sidon first listed what 2nd Century AD,7 Wonders World,General,About how many miles separate the U.S. & Cuba,Ninety 90,General,What come in types Transverse Scimitar and Barchan,Sand Dunes +General,Who is known as the 'father of the atomic bomb',Robert oppenheimer,General,What was the title of Charlie Chaplin's last film,Limelight,Entertainment,This gypsy swing guitarist nearly had his left hand destroyed by fire as a child?,Django Reinhardt +General,What is the capitol of Venezuela,Caracas,General,What is Grimace of the McDonald's characters?,A tastebud,General,If you have a rhytidectomy what procedure has occurred,A Face Lift +Entertainment,What is Kenny G's real surname?,Gorelick,General,If eggs have been fried on one side only how are they said to be served,Sunny side up,General,What is china's sorrow,Yellow river +General,Little Larry Puny Pete Small Sam considered names of who,Dickens Tiny Tim,Music,What Was The Name Of Nick Cave's Band Before The Bad Seeds,The Birthday Party,General,Under the snow and ice Antarctica is actually a what,Desert +Science & Nature,And What Did Thomas Edison Later Invent In The Lab Set Up With The Prize Money Awarded To The Above Invention ,The Gramophone ,General,What kind of bird taught dr dolittle to talk to the animals,Parrot,Music,During the 1960's who recorded the Albums 'Exile On Main Street' & 'Beggars Banquet',The Rolling Stones + History & Holidays,"Which corporation opened it's new headquarters in Portland Place, London in May 1932? ",The BBC ,General,In the United States at the turn of the century there were how many states?,45,General,In the cartoon Scooby Do what is Scrappy Do's battle cry,Puppy Power +Science & Nature,What Do Invertebrates Lack? ,A Backbone ,General,Who wrote the hymn Hear my Prayer,Mendlesson,General,When did richard burton die,1984 +General,What does the electrical term 'ac' stand for,Alternating current,Food & Drink,In which city is the Coleman's Mustard Museum? ,Norwich ,General,Which liqueur has blackcurrant as its main source,Cassis +General,U.S. Captials - New York,Albany,General,What is the Capital of: Tuvalu,Funafuti,General,The Mariana Trench is the deepest point of the Pacific Ocean to what depth does it reach. Give answer in feet.,36160 +General,"What is a 'yesterday, today and tomorrow'",Shrub,General,What 1979 film won the Oscar for best visual effects,Alien,Sports & Leisure,Who won three ladies singles titles at Wimbledon in the 1950's? ,Maureen Connolly  +Music,What Is The Nickname Of Bruce Springsteen,The Boss,General,What is a group of hares,Husk, History & Holidays,Which Welsh actor won Christ's Robe in a dice game in The Robe? ,Richard Burton  +Entertainment,"In the film 'The Day Of The Jackal', who played the Jackal?",Edward Fox,General,What title was held by the governor of India before independence,Viceroy,General,What is the general term used for various forms of insanity and mental derangement,Mental illness +Science & Nature, There are 328 species of __________,Parrots,General,Which pen name does the author Harry Patterson also use,Jack higgins,General,What was the first commercial readymix food,Pancake mix +Art & Literature,What Famous Gothic Novel Was Written By Mary Shelley ,Frankenstein ,General,What creature is the symbol of medicine,Snake,Science & Nature, Koalas and humans are the only animals with unique prints. Koala prints cannot be distinguished from human __________,Fingerprints +General,Who was Poet Laureate during World War One,Robert bridges,General,In Yemen after a wedding what lasts an average 21 days,The wedding feast,Music,Who Had A Uk Top 20 Hit In 1990 With No More Mr Nice Guy,Megadeath + History & Holidays,Name the author of Dracula ,Bram Stoker,General,What is the currency of Vietnam?,Dong,General,Which word refers to the internal diameter of a gun barrel,Calibre +General,Of which State was Jimmy Carter Governor before he became president,Georgia,General,"Baby, Bonny, Boofuls, Bumper, Bubbles, Bigheart Are Collectively Known As What",The Jelly Babies,Music,"Name Four Of The Artists Attributed To The 1970 Version Of ""Black Knight""","Blackmore, Gillan, Glover, Lord & Paice" +General,Fear of thunder or thunderstorms is ______,Brontophobia,General,Collective nouns - a descent of what,Woodpeckers,Science & Nature,As what is a giraffe also known?,Camelopard +General,How far is Neptune from the sun,2793 million miles,General,Name two sports where the winner moves backwards,Rowing - Tug of War,Art & Literature,"The ____ ____ school of poetry includes poets such as Frank O'Hara, John Ashbery and Kenneth Koch",New York +Food & Drink,Savoy And Late Flat Dutch Are Both Varieties Of Which Vegetable? ,Cabbage ,Geography,What Is The Colour Of The Cross On The Finish Flag? ,Blue ,Music,"For What Do The Initials SYSLJFM Stand For In Joe Tex's ""The Letter Song""",Save Your Sweet Love Just For Me +General,What is the name of the infection of the gums that causes them to bleed,Pyorrhoea,General,What to a French or Spanish man is an OVNI,UFO,General,What is a Hummum,Turkish bath +General,Which biblical character had 12 sons,Jacob,General,What was the name of the space shuttle that exploded?,Challenger,General,"On Night Court,who was Harry's idol?",Mel Torme +General,"How Are Clyde , Ding-A-Ling, Zippy, Pockets, Snoozy, Softy & Yak Yak Collectively Known?",The Ant Hill Mob,General,What is the fear of cooking known as,Mageirocophobia,Food & Drink,What is the full name of the flavour enhancer MSG?,Monosodium glutamate +General,Who is the best-selling saxophonist,Kenny g,General,Where was the academy that plato founded in 387 bc,Athens,General,"What was a large cask with a lidded opening, used to hold day's supply of drinking water for a ship's crewmen",Scuttlebutt +General,N. African dish of cracked wheat steamed over broth,Couscous,Geography,What is the capital of Norway,Oslo,Food & Drink,A Tagine Is A Speciality Dish That Originated In Which Country ,Morocco  +Sports & Leisure,Which Sportsman's Autobiography Is Called 'My Side''? ,David Beckham ,General,U.S. captials Kentucky,Frankfort,General,What was the name of the skunk in Bambi,Flower +General,What was superhero Green Lantern vulnerable to,Anything Yellow,Science & Nature,How Do You Convert Centigrade To Fahrenheit ,Multiply By 9 Divide By Five Add 32 ,General,-isms: An economic system characterized by private ownership and competition.,Capitalism +General,What did Colonel Blood try to steal in 1671,British crown jewels,Mathematics,A line drawn from an angle of a triangle to the mid-point of the opposite side is a(n) _______.,Median,General,A large sea birdor greedy person,Gannet + History & Holidays,Which 60's Movie features The Line he's very clean ,A Hard Days Night ,General,Monology is the study of what,Stupidity,Music,What Is The Small 4 String Instrument Originally From Hawaii,The Ukulele +Food & Drink,"Which of the following brands of drink sold the most in the UK in the year 2000 - Ribena, Lucozade or Tango?* * ",Ribena ,General,What is the second of einstein's 1905 papers,Special theory of relativity,General,Who was the French sculptor of the Statue of Liberty,Frederic bartholdi +General,What is the phonetic alphabet word for letter P,Papa, Geography,What is the basic unit of currency for San Marino ?,Lira,Entertainment,"In the film 'Hackers', how old was 'zero_kool' when he was first arrested?",Eleven +General,"In 1948 A Personnel Officer At ICI Noted That An Applicant Was ""Headstrong, Obstinate & Dangerously Self Opinionated"" Which Potential Recruit Was Turned Down",Margaret Thatcher,General,Who was the first voice of mickey mouse,Walt disney,General,To His Friends He Is Known As “Eric Claudin” How Is He Better Known Throughout The World?,The Phantom Of The Opera +Sports & Leisure,Who Beat Arsenal In This Year To Become The UEFA Champions League Winner? ,FC Barcelona ,General,The constellations Canis Major and Canis Minor are said to represent the dogs of the Greek Hunter,Orion,Music,Who Had A Number One Hit In 1962 With the Song Telstar,The Tornadoes +General,Where in the world are most diamonds found,Australia,General,What spice comes from the curcuma plant,Turmeric,Music,Who Thanked Heaven For Little Girls In Gigi,Maurice Cevalier +General,What Soviet leader seized control from Khrushchev in 1964,Leonid Brezhnev 1984 till died 92,Music,"Name the Bee Gees ill fated young brother who charated with ""Shadow Dancing""?",Andy Gibb,Entertainment,The film 'The Wizard Of ______'?,Oz +Music,Who is better know as Vincent Damon Furnier?,Alice Cooper,Music,"Who plays the uncredited lead guitar on The Beatles Song ""While My Guitar Gently Weeps""?",Eric Clapton,Music,"Which American Metal Band Of The Late 1980's Featured The Singer ""Sebastian Bach""",Skid Row +General,Where do the White and Blue Niles join,Khartoum - in Sudan,General,Where would you find the phrase Annuit Coeptis,American Dollar Bill ,General,Name the Egyptian God of funerals,Anubis + History & Holidays,How Many Crusades Were There? ,Nine ,Geography,Which country has taken its name from a line of latitude? ,Ecuador ,Art & Literature,Gandalf's elven name.,Mithrandir +Science & Nature,Which Now Standard Fittings Were First Featured On A Car In 1916 ,Automatic Windscreen Wipers ,General,In Sport If You Were To Achieve A Bucket Of Nails What Sport Would You Be Playing,Darts,General,In 1949 what was introduced to cars for the first time,The Ignition key +General,What country declared itself first atheist state in 1967,Albania – banned religion,General,An Arab horse has less what than other horses,Bones - one vertebra less,Sports & Leisure,What type Of Creature Is Sonic In The Computer Game ,A Hedgehog  +General,"In mythology, who was the mother of Eros and Aeneas",Aphrodite,Music,Which Paul Nicholas album was named after a sitcom in which he starred?,Just Good Friends,General,In Minnesota it is illegal to tease what animal,Skunk +General,"What type of whisky is called ""old no 7""",Jack daniels,Music,Which Rolling Stone Married 16 Year Old Mandy Smith,Bill Wyman,General,What is the worlds third largest island,Borneo +General,Which city is the capital of the Austrian province of Tyrol,Innsbruck,General,The Frenchwoman Jeanne Poisson was better known by which name,Madame de pompadour,Music,Who had a hit in 1976 with “you to me are everything”?,The real thing +General,"What have woodpecker scalps, porpoise teeth and giraffe tails all been used as?",Money,Food & Drink,What do the letters VOSP on a brandy bottle stand for?,Very Special Old Pale,General,Grotesque carved spout usually projecting from the gutter of a building,Gargoyle +General,Who wasn't afraid to call James Joyce 'A greasy undergraduate',Virginia wolfe,Food & Drink,From which fish does Caviar come? ,Sturgeon ,General,What is the force that slows down or stops a moving thing,Friction + History & Holidays,What was Joseph's job? ,Carpenter ,General,"If, during a game of chess, you made a move ""en passant"", which piece would you be moving",A pawn,General,Vicky Pollard' is a character in which British TV comedy series?,Little Britain +General,What is the literal meaning of the Spanish word tapas (snacks),Cover or covers,General,By whom was julius caesar stabbed,Cassius,Music,In 1977 The Darts & Boney M Had Hits With Different Songs With The Same Title What Was The title,Daddy Cool +General,What does the painting The Battle of Gettysburg claim to be,Worlds largest,General,What river flows from Mount Hermon into the Dead Sea,Jordan via Sea of Galilee,General,"What is it that walks on four feet in the morning, two feet at noon, and three feet in the evening ?",Man +General,"In the movie ""The Goonies"" what brand of candybar did Chunk try to give to Sloth?",Baby Ruth,General,What is the more common name for blue corundum,Sapphire,General,Koniophobia is the fear of,Dust +General,"In Greek mythology, who did zeus place in the heavens as the constellation ursa major",Callisto,General,Who should have played Indiana Jones and dropped out,Tom Selleck,General,"What actor played woody allen's best friend in ""annie hall""",Tony roberts + History & Holidays,In Which Year Was The Battle Of Trafalgar Fought? ,1805 ,Sports & Leisure,How many points are awarded for a safety touch in football?,Two,General,What did charles jung invent,Fortune cookies +General,Which classical composer was nicknamed 'The Red Priest',Vivaldi,Science & Nature,This order of insects contains the most species.,Beetle,General,"Which author wrote ""If not actually disgruntled, he was far from gruntled""",P g wodehouse +General,"In the monty python parody 'search for the holy grail', what was used to kill the rabbit",Holy hand grenade of antioch,General,Why is rice grown in flooded paddy's,Drown weeds,General,What is the applied science to the study of society,Sociology +General,Port Louis is the capital of which island state in the Indian Ocean,Mauritius,General,What is ipsism another name for,Masturbation,General,"What country controls access to the North Sea from the rivers Schelde, Meuse & Rhine",Netherlands +General,France and which country contested first ever world cup match,Mexico,General,"In the cartoon series, what is the name of Bart Simpson's dog",Santa's little helper,General,What sea separate Naples and Algiers?,Meditteranean +General,What is the strongest land animal,Elephant,General,Alcoholic beverage produced by fermenting the juice of grapes,Wine,General,What is the main ore of aluminium,Bauxite +General,A green or yellow liqueur brandy,Chartreuse,General,In 1923 what new optional accessory was offered on cars,A Radio,Entertainment,What did Dagwood give up to marry Blondie?,A family inheritance +General,Which Italian won the 1998 Tour de France cycle race,Marco pantani,General,What did Atlanta pharmacist John Pemberton sell two-thirds of his interest in for 283 dollars and 29 cents in 1887,Coca cola,General,What is the seaport capital of Sardinia,Cagliari +General,A villanelle is a type of what,Poem,Entertainment,What Don Mclean song laments the day Buddy Holly died?,American Pie, Geography,What is the capital of the Dominican Republic?,Santo Domingo +General,Mans formal dress coat called tails is named after what bird,Swallow,General,In which Australian State is Kalgoorlie,Western australia,General,The original plan for Disneyland included a land called what,Lilliputland +General,Which French composer wrote 'The Sorcerer's Apprentice'?,Paul Dukas,Sports & Leisure,What type of racing has only two cars competing on the track at the same time? ,Drag Racing ,Music,"Which Band Released The Album ""Caught In The Act"" In 1984",Styx +General,Who was the first tennis player to achieve the grand slam,Donald Budge,Food & Drink,"Cocktails: Creme de Cacao, cream, and brandy make a(n) __________.",Brandy alexander,General,What is the currency of denmark,Krone +General,A Polyorchid has at least three what,Testicles,General,In Louisiana what personal act is illegal in public,Gargling,General,For which novel did A S Byatt win the Booker Prize in 1990,Possession +General,Who was La Purcelle of Voltair's poem,Joan of Arc,Science & Nature,What is hyperglycemia commonly known as ?,Diabetes,General,"Dominica, Mexico, Zambia, Kiribati, Fiji & Egypt all have what on their flags",Birds +General,What was the Queen Mother's maiden name?,Bowes Lyon,General,Capital cities: Papua New Guinea,Port moresby,General,Who wrote Gulliver’s Travels (both names),Jonathon Swift +General,What actress played Laura and Almonzo's niece on Little House on the Prairie?,Shannen Doherty,Geography,Into Where Does The Volga (Europes Longest River) Flow ,The Caspain Sea ,General,Nyse in Swedish Tusszents in Hungarian Kychat in Czeck what,Sneeze +General,In what Puccini opera does Scarpia appear,Tosca,General,What form of transport did Kirkpatrick Macmillan invent in 1839,Pedal bicycle,Art & Literature,A method of painting developed by Seurat and Signac in the 1880s. It used dabs of pure color that were intended to mix in the eyes of viewers rather than on the canvas. It is also called divisionism or neoimpressionism.,Pointillism +General,What does the word economy mean in original Greek,Home Management,Music,"Originally Known As The Dust Brothers, Which Unrelated Pair Topped The Charts In The 90's",The Chemical Brothers,General,What city boasts Leonardo's famed fresco of the Last Supper,Milan +General,What eventually killed Oliver Cromwell,Malaria,General,Every square inch of the human body has an average of 32 million ______,Bacteria,General,Which is Argentina's second most populous city,Cordoba +Science & Nature,What is the biggest disqualifying factor for prospective astronauts?,Eyesight,Music,"Which Legendary New York Punk Club Saw Appearances By The Ramones, Televsion & Blondie",CBGB's,General,What is the biggest disqualifying factor for prospective astronauts,Eyesight +Geography,What Is The Highest Peak In England And Wales ,Mount Snowdon ,General,The Nuer people come from which country,Sudan,Sports & Leisure,What Is The Name Of The Place Where Ice Hockey Players Are Sent For Breaking The Rules ,The Sin Bin  +Science & Nature,In which country was the match invented?,France,General,Which South American country has borders with only Colombia and Peru,Ecuador,Geography,In Which Country Is The Source Of The River Thames ,Gloucestershire  +General,What is the name of Princess Anne's second husband,Tim laurence,General,Who released 'pretty hate machine' in 1989,Nine inch nails,General,What are the names of the Simon brothers from Simon & Simon?,Rick & AJ +Music,Which member of Busted has left the group to join another band?,Charlie Simpson,General,In US Emergency rooms what toy is often found in rectums,Barbie – most common doll up ass,General,"Which liqueur gives the cocktail ""Tequila Sunrise"" its red glow",Grenadine +General,A haemodializar is a mechanical what,Kidney,General,What animal's milk does not curdle,Camel,General,What did Julia Ward Howe originate,Mothers day +General,In an authentic Chinese meal what is served last,Soup, History & Holidays,What was the first postage stamp ?,Penny Black,General,"Homo sapiens"" means:",Man of knowledge +Entertainment,Where does Gonzo from the Muppet Show come from?,Outer space,General,What was the Bikini originally called,The Atom,General,John Dunlop developed pneumatic tyres - what profession,Vet +General,"Irish-born English scientist, who was an early proponent of the scientific method and a founder of modern chemistry",Boyle,Music,Living On The Ceiling Was A Hit For Blancmange Or Lionel Richie,Blancmange,General,Steely Dan is a band but what was the original steely dan,Chrome dildo +General,Cosmetic brand gets it's name from Latin for as white as snow,Nivea, History & Holidays,In The Tradition of the song the 12 days of Christmas if you add up all the legs and take away all the beaks what number are we left with ,155 Confirmed by Lindsey ,General,What is the old term for a golfer's nine iron,Niblick +General,This software company produced hits such as 'Pagemaker' & 'Illustrator',Adobe,General,Branch of mathematics that deals with the relationships between the sides & angles of triangles & with the properties & applications of the trigonometric functions of angles,Trigonometry,General,How many pieces of bun are in a mcdonald's big mac,Three +Science & Nature,Which False Idol Was Worshipped By The Hebrews At The Foot Off Mount Sinai ,The Golden Calf ,General,Misogamy is a dislike or hatred of what,Marriage,Music,Name The Film In Which Spandau Ballet's Kemp Brothers Later Starred,The Krays +General,Indiana is known as ______,Hoosier state,General,The Phoenician symbol for mouth is now what letter of alphabet,P theirs was Pe,General,What is the longest golf course to stage the British Open,Carnoustie 7066 yards +General,Phallophobia is a fear of ______,Penis,Science & Nature,By what name is Lysergic acid diethylamide better known,Lsd,General,USA supreme court 1962 said who cant be imprisoned - illegal,Drug Addicts +General,Numbers Where's the famed Arch of Hadrian,Ahtens,Food & Drink,What Is Bisque ,A Rich Soup Usually Of Shellfish ,General,What singer did Elvis Presley say was the greatest in the world,Roy Orbison +General,Which French actor and crooner sings the theme tune to Disney's The Aristocats,Maurice chevalier,General,What Beano Character Had A Pet Crow Called Joe?,Roger The Dodger,General,If something is caseous what is it like,Cheese +Food & Drink,Which alcoholic beverage is advertised on TV by Tom the Dancing Cat? ,Bacardi Breezer ,Geography,Which Cambridge Bridge Took It's Name From An Architectural Masterpiece In Venice ,The Bridge Of Sighs (St Johns College) ,General,Spice made from the berry of the pimento plant,Allspice +General,As whom was Jan Ludvik Hoch better known,Robert maxwell,Science & Nature,What instrument measures blood pressure?,Sphygmomanometer,General,"A series of small, fast steps executed with the feet very close together.",Pas de bourrée +General,Whom did Joe Walcott defeat at age 37 to win the heavyweight title,Ezzard charles,Food & Drink,Which country produces the wine Vinho Verdi? ,Portugal ,General,What did physicist Lord Rutherford discover inside the nucleus of the atom,Protons +General,What colour does a chameleon turn when its angry,Black,General,In which county would you find Ars,Norway,Sports & Leisure,Which country won the World Cup of Soccer in 1982.,Italy +General,Who was the last reigning King of Egypt,Farouk,General,Albert Sauvy coined what term in the 1950s,The Third World,General,Which are the most used muscles in the body,Eye muscles +General,"By What Name Is ""Calcium Oxide"" More Commonly Known",Quicklime,General,What is the main source of vitamin C,Fruit,Music,Which Groups First Top Ten Hit Was Entitled “ Right Now ” And Released in 1999,Atomic Kitten +General,Ball is 38 millimetres in diameter weighs 2.5 grams what sport,Table Tennis,Music,Who Was Hal David's Famous Writing Partner?,Burt Bacharach,General,In 1899 the Eastman company in the USA produced first what,Kodak 1 - hand held roll film cam +General,What nationality was Fredrick Chopin,Polish,General,What is the fear of dreams known as,Oneirophobia,General,Which class of racing yacht has the same name as a Shakespeare play,Tempest +General,"Which dish consists of Steak, sprinkled with ground peppercorns, soaked in Brandy and set alight?",Steak Au Poivre,General,What is maryland's state song,Maryland my maryland,General,In Fresh Prince of Bel-Air what was the butlers name?,Gefforey +General,Line of hereditary rulers,Dynasty,General,"Who said ""Once you are dead you are made for life""",Jimmy Hendrix,General,The sliothar is a leather covered cork ball used in which sport,Hurling +General,Which famous building is housed at the former site of the Bankside Power Station?,Tate Modern Art Gallery,Music,"Which Singer Started As A Session Guitarist Working With The Likes Of Bobby Darin , Frank Sinatra, Dean Martin & The Beach Boys",Glen Campbell,Geography,The Salto Alto (Angel Falls) in ______________ is the highest waterfall known. It is more than twenty times higher than Niagara.,Venezuela +Science & Nature,Which Insect Transmits African Sleeping Sickness ,The Tsetse Fly ,Music,"The Silent Film ""MetroPolis"" Featured In The Video For Which Queen Hit?",Radio Ga Ga,General,Which two countries were involved in the 'Battle of the Thames',Britain & canada +Music,What Don't The Children Need In Pink Floyd's Another Brick In The Wall,An Education,Art & Literature,How many stories did enid blyton publish in 1959?,Fifty nine,General,What is the native form in which dna most commonly found,Double stranded helix +General,"In Gulliver's Travels, what is the name of the flying island inhabited by scientific theorists",Laputa,Sports & Leisure,4 Cities Have Hosted The Modern Day Summer Olympic Games Name Any 2 (PFE) ,"Los Angeles , London , Paris Or Athens ",Sports & Leisure,"Which famous sporting event first held in 1981, was won by Dick Beardsley? ",London Marathon  +Science & Nature," Bald eagles are not bald. The top of their head is covered with slicked_down white feathers; from a distance, they appear __________",Hairless,General,Often a bridesmaid never a bride advertised what,Listerine,Geography,The Monegasque _ natives of ________________ _ constitute only about 16 percent of the nation's population.,Monoco +General,"Which Boyband That Split Up In 2000 Released All Of Their Songs In The USA Under The Name ""Silk""",Another Level,General,Pliny the philosopher believed dead souls went into what,Beans,General,What event in the Bible occurred at Bethany,Raising of Lazarus +General,What is the most popular crop in U.S. home vegetable gardens,Tomatoes,General,U.S. Captials - New Mexico,Santa Fe,General,Type of precious stone has a name literally means blue rock,Lapis Lazuli +General,What is the attribution of human attributes to a deity,Anthropomorphism,General,"What Olympic aquatic event includes such positions as the flamingo, crane & fishtail",Synchronized swimming,General,Which Screen Legend Died On Christmas Day In 1977 In Switzerland,Charlie Chaplin +General,What is Barbie`s full name?,Barbara Millicent Roberts,General,The study of man and culture is known as __________.,Anthropology,Geography,In which country is loch ness ,Scotland  +Toys & Games,"In Monopoly, What is the cost to buy Vermont Avenue",100,Food & Drink,What 'B' Is A Mexican Dish That Translates Into English As Little Donkey ,Burrito ,General,"What 1980s TV series written by Alan Bleasdale dramatised the life of Percy Toplis, a First World War deserter?",The Monocled Mutineer +General,The oldest written plan of government in effect is in what country,United States of America,General,Howard Hughs used to store what in large metal containers,His Urine,General,Name Elvis Presley's twin brother,Aaron +Music,Whose Concerts Formed The Basis Of The Film Rattle & Hum,U2,General,Children which nhl franchise holds the record for the longest unbeaten streak,Philadelphia flyers,General,Relating to food what are 'quenelles' type of,Dumpling +General,Christian sacrement in which bread and wine are consecrated and consummed,Eucharist,Sports & Leisure,Who lost Wimbledon singles finals to Boris Becker and Pat Cash? ,Ivan Lendl ,Music,Which Actor Shares His Name With A 1984 Hit By Madness,Michael Caine +General,Who was the Roman equivalent of the Greek God Zeus,Jupiter,General,What was the name of Garfield's vet?,Liz,General,"While USSR sent Laika the dog into space, the USA sent Laska and Benjy, which were?",Mice + History & Holidays,"Who Described Russia As (A Riddle Wrapped In A Mystery, Inside An Enigma)? ",Winston Churchill ,General,"Which Czech village seas destroyed by the Germans in retaliation, following the assassination of Reinhard Heydrich in World War Two",Lidice,General,The Hart memorial trophy is awarded in which professional sport,Ice Hockey +General,What color is the car that Starsky & Hutch drive?,Red with a white swoop,Food & Drink,Which vegetable comes in `globe' and `Jerusalem' varieties? ,Artichokes ,Music,"Which Female Icon Brought Out A Book Entitled ""Sex""",Madonna +General,In which European country would you find the town of Eupen,Belgium,Geography,What country consists mainly of the jutland peninsula ,Denmark ,Geography,What Word Represents Q In The Phonetic Alphabet? ,Quebec  +General,What is the flower that stands for: declaration of love,Red tulip,General,Basmati is a type of what?,Rice,General,"What is made by heating carbon plasma to 20,000 degrees and condensing it under ultra high pressure",Diamonds +Sports & Leisure,Which British soccer club was managed by Christian Gross? ,Spurs ,General,What sport featured in the 1980 film Breaking Away,Cycling,General,George V1 Mozart Al Jolson Casanova - which organisation,Freemasons +General,Cinnabar is an ore of which metal,Mercury,Music,What Colour Was Prince's Little Corvette,Red,General,Why is the city La Paz in Bolivia safe from fire,To high - Not enough air to burn +General,Why two car thieves caught trying to sell stolen car in 1976,Tried to sell to owner,General,What was Betty Rubble's Maiden name,Betty Jean Mcbricker,Music,Which Comedy Duo Performed The Stonk In 1991,Hale & Pace +Sports & Leisure,Why do some English Football Clubs have triangular corner flags rather than square? ,Won the F.A Cup ,General,What is the little lump of flesh just forward of your ear canal right next to your temple,Tragus,People & Places,Who Was At The Centre Of Englands Match Fixing Scandal ,Hansie Cronje  +People & Places,In 1996 who did The Spice Girls say was their Girl Power role model? ,Margaret Thatcher ,General,In which city was Galileo born,Pisa,Music,"David Coverdale Was A Member Of ""Def Leppard"" Or ""Whitesnake""",Whitesnake +General,Ronald Wycherley became more famous than,Billy Fury,General,In what did someone squish her hands to make the sound of e.t walking,Jelly,General,These license plates are manufactured by prisoners in the state prison in Concord. What is the slogan on them,Live free or die +General,Who sang 'think' in the original 'blues brothers' film,Aretha franklin,General,"My dad was a big kid,a big RICH KID. What show was I on?",Silver Spoons,General,Who wrote the opera 'rigoletto',Guiseppe verdi +General,What Was The Very First Andrew Lloyd Webber Musical To Be Filmed,Jesus Christ Superstar,General,What common word in Morse is __ ___ __,MOM,Geography,Which city is on the east side of San Francisco Bay,Oakland +Science & Nature,What is the symbol for copper?,Cu,Geography,What American city is also known as Beantown?,Boston,General,Farok Pluto Bulsara became more famous as who,Freddie Mercury +General,Name the biggest hit for the Animals in 1964,House of the rising sun,General,Which metal comes in a form known as spelter?,Zinc,General,What is the fastest stroke in swimming,Freestyle +General,Where would you find the thickest layer of skin on the human body,Feet,General,Humphrey Bogart received his only Oscar for which film,The african queen,General,Riveting is a method of joining pieces of what,Metal +Music,In Which Country Does The Annual T In The Park Concert Take Place,Scotland, History & Holidays,Who said 'eureka'?,Archimedes,Music,Name The Only Climax Blues Band Hit Whose Title Seems A Fitting Epitaph To Their Following Career,Couldn't Get It Right +General,Who was the greek god of dreams,Morpheus,General,Miss Lemon is what detectives confidential secretary,Hercule Poirot,General,What m°a°s°h unit does Hawkeye Pierce operate in,4077th +General, A government in which power is restricted to a few is a(n) __________.,Oligarchy,General,What is likely to have a watermark,Paper,General,Who invented the dumb waiter,Thomas Jefferson +General,Hydrophobia is the fear of,Water,Food & Drink,What Is Sold At Billingsgate Market In London? ,Fish ,General,What does awol stand for,Absent without leave + History & Holidays,"If a girl puts the letters of the alphabet face down in water, how will she know who she will marry ",His initials will be face up next day ,General,In Grease Two what does stephanie want in a boyfriend?,A cool rider,General,What name's given to a number that exactly divides into another,Factor or Divisor +General,What ray bradbury novel is named for temperature at which paper catches fire,Fahrenheit 451,Music,"When Radio 2 compiled A List Of Songs Of The 20th Century, Which 1998 Movie Theme Was The Highest Placed Song Of The 1990's",Celine Dion / My Heart Will Go On,General,What color is the last and most valuable ball a snooker player must pocket,Black +General,What fraternity do the Lewis and Gilbert join in Revenge of the Nerds?,Lambda Lambda Lambda,General,Actor ______ Hackman,Gene,General,Who made the first commercial boeing 747 flight from new york to london,Pan +Music,"Which Former Member Of Genesis Had A Hit With ""One More Night""",Phil Collins,Sports & Leisure,In Which Year Was Shergar Kidnapped ,1983 ,General,"Which Long Running TV Show Began With a Pilot Episode Aptly Named ""Killer"" In 1983",Taggart + History & Holidays,London's Trafalgar Square Christmas tree is traditionally given by which country? ,Norway ,General,What was the first ABC TV series rated No 1 for full season,Marcus Welby M.D.,General,From the Greek meaning apple what do we call this fruit,Melon +Food & Drink, What type of drink is Perrier?,Mineral water,General,The democrats nominate Massachusetts governor _____ for president,Michael dukakis,General,What was Sherpa Tensing surname,Norgay +General,The only actor to receive France's Commander of Arts and Letters Award,Jerry,General,Sport resembling hang-gliding using a parachute like canopy attached to the body by a harness,Paragliding,General,"What is known as the "" Palace of the Peak""",Chatsworth house +General,What famous film maker was first to use the close up,David Wark Griffith Birth of a Nation,General,Which body of water seperates France from Great Britain,English channel,General,In which sport would you find turkeys and spares,Ten pin bowling +General,"Who ordered the Code Red in ""A Few Good Men""?",Colonel Nathan P. Jessup,General,What is the largest river in North America,Mississippi,General,What is the atomic weight of uranium,92 +General,Andrew John Woodhouse in fiction was who - Ira Levin novel,Rosemary's Baby,General,We've only just begun was background music advertising what,Bank - R Carpenter,General,30% of people quit this job in USA each year - what job,School Bus Driver +Music,Number of years that Beatlemania toured the United States,5,General,"What was the name of the woman who helped run the school for the blind on ""Little House on the Prarie?"" (Besides Mary Ingalls)",Hester Sue,Art & Literature,What word is Isaac Asimov famous for coining ?,Robotics +Science & Nature,What Is One Inch In Millimeteres ,25.4 mm ,General,"What part of you is probably type a, b, ab or o",Blood,General,What is a group of jellyfish,Smack +General,"Which Musical Act Went By The Fomer Name Of ""Two Tons Of Fun""",The Weathergirls,Science & Nature,This is the reading system used by the blind.,The braille system,General,Highly poisonous substance used in the extraction of gold and silver,Cyanide +General,Which country is known as the Hashemite Kingdom,Jordan,General,Grand prix what measures walking distance,Pedometer,General,"Which city is, in terms of population, the second largest in Mexico",Guadalajara +General,How many engines are on a b52 bomber,Eight,General,The Afghan Taliban use which colour of flag,White,General,Old Moore's Almanac' was founded in what year,1699 +Science & Nature,What is a male goose called?,Gander,Science & Nature, The color of the blood of an __________ is bluish_green.,Octopus,General,Anthropologists say what is the worlds oldest profession,Witch Doctor +General,Who plays jennifer lindley on 'dawson's creek',Michelle williams,General,In some parts of China what is the Long Nosed General,Pigs - unlucky to mention pig,Music,What Was The Name Of Bennie Hils Fastest Milkman In The West,Ernie + History & Holidays,What is New Year's Eve called in Scotland? ,Hogmanay ,Food & Drink,What is the name of the traditional english dish made from pork trimmings and pig's head? ,Brawn ,General,What does a month beginning with a sunday always have,Friday the 13th +Science & Nature,Of what do earthworms have five?,Hearts,General,Misty for me what statue did dobie gillis mimic while contemplating life and love,Rodin's,General,Nobody's perfect is the last line in which classic comedy film,Some Like it Hot +General,In the game of Bridge what are the first six tricks won called,The Book,General,Which animal has breeds called Briards and Griffons,Dog,Art & Literature,"Who wrote three books under the title ""Das Kapital"" ?",Karl Marx + History & Holidays,"Which drug, later found to have devastating side effects, was launched in 1958 to help pregnant women overcome morning sickness ",Thalidomide ,Entertainment,What night club did Ricky work at on 'I Love Lucy'?,The Tropicana,General,Nelophobia is the fear of,Glass +General,Which is the largest of Neptune's moons,Triton,General,Which chemical was introduced to US as a cough suppressant,Heroin,General,Ragdoll Korat Sphinx Tiffany - types of what,Cat Breeds +General,What was the codename of the Allied invasion of North Africa in 1942,Operation torch,General,What is divination by means of lines and figures drawn in the earth,Geomancy, History & Holidays,Old superstition Wearing socks inside out protection from what ,Witches  +Geography,What is the capital of Guinea_Bissau,Bissau,General,How many faces does a dodecahedron have?,12,Entertainment,Who was the villain in 'Star Wars'?,Darth Vader +General,What is a brickfielder,Hot SE Aussie wind,General,What is used to thicken gazpacho,Breadcrumbs,General,"Otto Titzling Is Often ""Wrongfully"" Credited As Being The Pioneer & Inventer Of Which Item Of Clothing",The Bra +General,Which company operates the Central Selling Organisation,DeBeers - diamonds,General,What is MacGyver's first name?,Stace,General,What is the flower that stands for: prudence,Mountain iash +General,"Who took ""everybody loves somebody"" to #1",Dean martin,General,The First Wembley Fa Cup Final To Need A Replay Occured In Which Year,1970,General,What pitcher was nicknamed oil can,Dennis boyd +General,"Which opera singer was known as ""La Stupenda""",Joan sutherland,General,Kate Mulgrew plays who in a Gene Roddenbery based series,Capt Katherine Janeway Voyager,General,What is the french phrase meaning 'on the contrary',Au contraire +General,What expression is used to describe a child who closely resembles a parent,Chip off the old block,General,With what is spangy played,Marbles,General,Which collection of stories was told by Scheherazade,The thousand and one nights +General,What was invented 1970 US Dr Buddy Lapidus marketed 1975,Odour Eaters,General,The Goldfish Is Native To Which Country,China,Sports & Leisure,What football team was formerly known as the Frankford Yellow Jackets?,Philadelphia Eagles +General,Who dies in every episode of the cartoon series South Park,Kenny,General,"A multistoried building, typically Asian, forming a tower with upward curving roofs over the individual stories.",Pagoda,General,Who was nicknamed The Great Communicator,Ronald Regan +General,How big a can of soda pop contains the equivalent of 9 teaspoons of sugar,Twelve ounce,General,What is the name for a special vibrator worn with straps,A Butterfly,General,What is the worlds largest rodent,Capybara +Music,What Did Bob Marley Have Surgically Removed In 1977,A Cancerous Toe,General,"In ""peanuts"", what is the surname of lucy and linus",Van pelt,General,What is the fifth largest country in the world,Brazil +General,What town has the highest post office in the US,Climax Colorado,General,With which contemporary issue is U.S. writer James Baldwin associated,Civil rights,General,Crazy Horse and Custer shared what childhood name,Curly +General,What is the fear of light flashes known as,Selaphobia,General,Which UK city is the home of the Halle Orchestra,Manchester,General,"A spout placed on the roof gutter of a Gothic building to carry away rainwater, usually carved in the shapes of fanciful animals and grotesque beasts.",Gargoyle +General,What is the common name for solid carbon dioxide,Dry ice,General,The St. Louis gateway arch had a projected death toll while it was being built. How many people died,0,Science & Nature,Which Metal Has the Chemical Symbol SN? ,Tin  +General,"What links stags tails, pickled worms, gallstones, tomatoes",Once thought to be Aphrodisiacs,General,What kind of seeds can cause drug tests to trigger false positives for opium,Poppy seeds,General,What is the state bird of 7 u.s states,Cardinal +General,"In darts, what is a score of 26",Bed and breakfast,General,Names from Jobs - what in the middle ages did a walker do,Clean Cloth,General,What is the most commonly used oil in Chinese cookery,Groundnut or Peanut +General,John Richie became famous under what name,Sid Vicious,General,What is a husky most likely to be pulling,Sled, Geography,What is the capital of Nauru ?,The district of Yaren +Music,Which movie is synonymous with the Adagietto from Mahler's fifth symphony?,Death In Venice,Music,Which singer had the first names Harry Lillis?,Bing Crosby,General,Who created the character Walter Mitty,James Thurber +General,What is the lead in pencils made from ?,Graphite,General,Who was the first chancellor of germany after WW II,Konrad adenauer,General,What is a group of baboons,Troop +General,In what Elvis film did he play a double role,Kissing Cousins,General,What year saw sugar jump from nine cents to fifty-eight cents a pound,1973,Entertainment,"Barbara Streisand was the female lead in ""Hello, Dolly"". Who was the male lead?",Walter Matthau +General,Who was the author of The Moonstone,Wilkie collins,General,What organization was the forerunner to the cia,Office of strategic services,General,What is connected to the throat by the eustachian tube,Ear +General,Julius Sturgis in 1861 built the first US factory making what,Pretzels,General,In what European country have most land battles been fought,Belgium, History & Holidays,"The Nordic countries (Denmark, Sweden, Norway notably) tend to celebrate Christmas chiefly on which date? ",24th December  + History & Holidays,What Are Monsters With Only One Eye Called ,Cyclops ,General,Who wrote the childrens work 'Peter and the Wolf',Sergei prokofiev,General,__________ comics were nearly banned in Finland because he doesn't wear pants. ,Donald duck +General,Who played 'Hawkeye' in Robert Altman's classic M.A.S.H,Donald sutherland,General,Radiation Military operations above the surface of the earth,Air warfare,Food & Drink,For What Would You Use A Kilner Jar ,Preserving Fruit/Vegetables  +Entertainment,"Which character sang, ""When you wish upon a star.."" in Disney's ""Pinocchio""?",Jiminy Cricket,Music,Who Else Features On Michael Jacksons 1983 Greatest Hits Album,The Jackson Five,General,Which u.s president said 'the buck stops here',Harry truman +General,Shakespeare character in The Tempest is the son of Sycorax,Caliban,General,Which game begins when the referee shouts draw,Lacrosse,General,Who made his name in 'i dream of jeannie',Larry hagman + Geography,In which city is the Wailing Wall?,Jerusalem,General,"What does the slang term ""all day & night"" mean to a prison inmate",A life sentence life sentence,General,What is Burgoo,Meat stew +General,Telephonophobia is a fear of ______,Telephones, History & Holidays,Who Released The 70's Album Entitled Fun House ,The Stooges ,General,William Buroughs coined what phrase used by Steppenwolf 1968,Heavy Metal - Born to be Wild +General,Who advocated the planting peanuts and sweet potatoes to replace cotton and tobacco (i.e. crop rotation),George Washington Carver,General,What caused the gremlins in the movie Gremlin to become evil?,Eating after midnight,Sports & Leisure,What Colour Shirts Were England Wearing When They Won The World Cup In 1966 ,Red  +General,A kind of tortoise in the galapagos islands has an upturned shell at its neck so it can reach its head up to eat what,Cactus Branches Cactus,General,Proverbially a man is as old as he feels a woman as old as she?,Looks,General,In England what is the most popular boys name of the 90s,Daniel +Food & Drink,From Where Does Spaghetti Bolognese ,Bologna ,General,"If yoU.S.uffered from ornithophobia, what would be your greatest fear",Birds,General,What was the name of Mary Pickford's and Douglas Fairbanks mansion,Pickfair +Science & Nature,What Is The Male Bee whose Sole Function Is To Mate With The Queen? ,The Drone ,Music,"Which Duo Had A Hit With The Song ""Islands In The Stream""",Kenny Rogers & Dolly Parton,General,"The word rodent comes from the italian 'rodere', which means",Gnaw +General,"Who did Chief Sitting Bull call ""Little sure shot""",Annie oakley,General,"If you heard the words ""Hey You Guys!"" what TV program was about to begin?",The Electric Company,Food & Drink,Foie Gras Comes From The Liver Of Which Animal ,Goose  +General,What is the longest running race at the olympic games,Marathon,General,"In Roman mythology, who was the wife of Jupiter",Juno,General,Marzipan comes from Marci Panis literally meaning what,Marks bread St Marks day 25 April +People & Places,Who was the first member of the Royal Family to graduate from University? ,Prince Charles ,General,Saponification is the process that makes what common product,Soap,General,A 'featherie' was an early form of' which piece of sports equipment,Golf ball +People & Places,Name Tthe Australian Actor Who Advertised Fosters Lager On Tv ,Paul Hogan ,Food & Drink,What type of pastry is used for profiteroles? ,Choux ,General,In which Western film did the character Will Munny appear,Unforgiven +Science & Nature, The world's fastest reptile (measured on land) is the spiny_tailed __________ of Costa Rica. It has been clocked at 21.7 miles per hour.,Iguana,Science & Nature,A flat-bottomed conical laboratory flask with a narrow neck is called a(n) __________.,Erlenmeyer flask,Geography,What is the name of the extinct volcano that rises above Edinburgh? ,Arthur's Seat  +General,Name Australia's highest mountain,Mount Kosciusko,Science & Nature,What word is used for a female fox,Vixen,Science & Nature,What Is The Largest Plant Without A Trunk? ,The Bananana  +General,What sort of food is a 'rollmop',Fish,General,1804 J M Jacquard invented first programmable device - what,Loom (programmed punch cards),General,What soft drink was developed as a hangover remedy,Pepsi Cola +General,Which kind of flower has the most species,Orchid,General,"If an animal has gills, what is it",Fish,General,What are the only two london boroughs that start with the letter 'e',Ealing +General,Why Do We Have Eggs At Easter ,They are a symbol of rebirth ,Sports & Leisure,"What sport do the following terms belong to - ""Touches & Lunges""?",Fencing,Entertainment,Who sang 'I'm A Believer'?,Monkees +Religion & Mythology,"In theology, the study of final things such as death, judgement and the end of the world is called:",Eschatology,General,"To within 3 mph, at what speed in m.p.h. does a wind become a hurricane",73,Music,Who Was Declared Dead On New Years Day 1953 At The Age Of 29?,Hank Williams +Food & Drink,Which vegetable did President George Bush senior declare publicly that he did not like? ,Broccoli ,General,Barak O Bama Became The First Black USA President In 2008 But In Which US State Was He Born,Hawaii,General,"Edgar allan poe introduced mystery fiction's first fictional detective, auguste c. dupin, in what 1841 story",The murders in the rue morgue +Entertainment,Name the apartments the Jetson's live in.,The skypad apartments,Science & Nature, Monkeys will not eat red meat or __________,Butter,Food & Drink,From what animal do we get venison?,Deer +Sports & Leisure,In Englands 2002 World Cup Squad Who Was The Heaviest Player? ,David Seaman ,Sports & Leisure,Which Cricketer Is Nicknamed Beefy And Captained England In 1980 & 1981? ,Ian Botham ,Music,Who Were The First Scottish Group To Have 3 No.1 Hits,Wet Wet Wet +General,"What does ""mit""stand for",Massachusetts institute of technology, History & Holidays,Who was Canada's first Prime Minister,John A. Macdonald,General,George Washington Thomas Jefferson Sam Adams all did what,Brewed own beer +General,In 1902 What did Mary Anderson invent,Windscreen Wipers,Sports & Leisure,With which sport is Muhammad Ali associated ?,Boxing,General,Name Roman soldier who is supposed to have stabbed Jesus,Longinius +General,"Monte Corno, at 9554 feet, is the highest point in which Italian mountain range",Apennines,General,What would you do with or what is a millers thumb,Eat it - type of fish,General,What type of shoes does the Pope usually wear,Moccasin +General,You dot your i - what is the dot called,Tittle,General,Topolino in Italy is who here,Mickey Mouse,General,In golf what do the Americans call an albatross,Double Eagle +General,In which musical did Barbra Streisand disguise herself as a man,Yentl,General,What was the first year that women could vote in norway,1913,General,Diamonds which band covered nilsson's 'one' and brought it to the top ten in 1969,Three dog night +General,What is the length of a bombardon,16 feet, Geography,What is the basic unit of currency for Comoros ?,Franc,General,Which Latin phrase translates into English as 'peace be with you',Pax vobiscum +Music,Slam Dunk (Da Funk) Was The First UK Hit For Which Boy Band,Five,General,What does a psephologist study,Voting - Elections, History & Holidays,U.S. President: Calvin _________.,Coolidge +Science & Nature,What is the meaning of the name of the constellation Ophiuchus ?,Serpent Bearer,General,What company invented abs brakes for cars,Bosch,Science & Nature,"Pulp, crown, and root are parts of a(n) ________.",Tooth + History & Holidays,What was the name of Eddie Murphy's character in Beverly Hills Cop? ,Axel Foley ,General,Until her recent death who held the record as the most prolific living author,Barbra cartland,General,Which Aerosmith song was re-made by Run D.M.C.?,Walk this way + Geography,Columbus is the capital of ______?,Ohio,Sports & Leisure,What Is A Cricket Umpire Signaling When He Raises Both Arms Aloft? ,A Six ,General,In Huston Texas they do it most 4.6 times per week - what,Eat Out +General,What is a group of this animal called: Hog,Drift parcel,Science & Nature,Which Is The Longest Railway Line In The World ,Trans Siberian Railway ,Sports & Leisure,Hockey: The Boston ___________.,Bruins +General,"Whose motto is "" Nation shall speak peace unto Nation """,BBC,Music,Which Former Model was A Smooth Operator,Sade,General,Thomas Gradgrind and Sissy Jupe appear in which of Charles Dickens' novels,Hard times +General,"What does the acronym ""ram"" stand for",Random access memory,Sports & Leisure,How Many Points Are Awarded For A Touch Down In American Football? ,Six ,General,What did Hercules use to clean the Augean stables,A river +General,The average American consumes 9lbs of what every year,Food Additives,General,Who collects banknotes,Notaphile,General,What part of the body does a chiropodist treat,Feet +General,Dalmatian dogs are born which colour or colours,White - spots come later,General,"Closely related to pascal, niklaus wirth also played a part in what computer language's creation",Modula,General,Whose first box office film was called Risky Business,Tom Cruise +General,Who directed the 1974 film Chinatown starring Jack Nicholson,Roman polanski, History & Holidays,Who Was President Of America At The Outbreak Of World War II? ,Franklin D Roosevelt ,General,Francis Galton first classified what,Fingerprints +General,What is the nickname for Pennsylvania,Keystone state,Art & Literature,Where Is The Worlds Largest Art Gallery ,Paris (Lourve) ,Music,Name One Of The 3 Labels The Sex Pistols Were On,"A&M, Emi, Virgin" +General,Dr C W Long was the first to use what (anaesthetic) in 1842,Ether,General,Who was immediately preceded by Breshnev and Andropov,Chernenko,General,What is the Capital of: Maldives,Male +Sports & Leisure,The Pittsburgh Steelers Won Which Major Sporting Event In 2006 ,The Superbowl ,General,"""Don't You Feel Like Cryin', Don't You Feel Like Cryin'"" what is the song name",Cry to me,Music,"Who Wrote And Had A Hit With ""Love Is All Around"" In 1967",Reg Presley Of The Troggs +Music,What Is The Connection Between Johnny Logan & Sean Sherrard,They Are The Samer Person,General,Name Helen of Troys husband,Menelaus,General,What beer is represented by a goat,Bocks Beer +General,What was Nancy Davis Reagan's birth name?,Anne Frances Robbins,General,Who wrote Heart of Darkness,Joseph Conrad,Science & Nature,Where Do Hydrophytic Plants Grow ,In Or Around Water  +Science & Nature,"A common sight in most high streets, what was invented in 1967 by Englishman John Shepherd- Barron? ",The Cash Dispenser ,General,What is the central administrative body of the catholic church?,Curia,General,"In 'romeo and juliet', who says 'make the bridal bed in that dim monument where tybalt lies",Juliet +General,What season is it in Australia when it is summer in England,Winter,General,Which metal is the best conductor of electricity,Silver, Geography,What is the basic unit of currency for France ?,Franc +General,What new invention was shown to Queen Victoria 14 Jan 1878,The telephone,General,Who wrote The Cruel Sea,Nicholas monsarrat,General,How long is a standard Olympic swimming pool,Fifty metres +General,If you were performing Christies or edging what are you doing,Skiing,People & Places,What Was Sir Roger Hollis The Head Of ,MI5 ,General,The average bank teller loses about how many dollars every year,250 +Music,What Does ELO Actually Stand For,Electric Light Orchestra,Technology & Video Games,How many gears do you have in 'Pole Position'? ,2,Science & Nature,Which Is The Largest Species Of Ape? ,Gorilla  +General,This cluster of stars is also known as the seven sisters,The pleiades,Art & Literature,"Who wrote ""The Wind in the Willows"" ?",Kenneth Grahame,General,Who was king arthur's foster-father,Ector +General,Which musical featured the song 'If I ruled the world',Pickwick,Geography,"The ___________ is the world's oldest desert, and the only desert inhabited by elephant, rhino, giraffe, and lion.",Namib,General,In MacDonald's what is served in a blue wrapper,Filet-O-Fish +General,Who 'imagined' a better world,John lennon,General,In Guernee Illinois women over 200lb are banned from what,Riding Horses wearing shorts,General,The penny black - worlds first stamp - what was second,Two penny Blue +Food & Drink,What Is The Stimulant Present In Tea & Coffee ,Caffeine ,General,Abraham Zapruder made the most scrutinised film all time what,Kennedy Assassination,General,In Hindu mythology who is the mother goddess,Kali +Sports & Leisure,How Many Times Did Michael Schumaker Win The World Formula One Drivers Championships? ,7 ,General,"In Mathematics, who devised a triangle to show the probability of various results occurring when any number of coins are tossed",Blaise pascal,General,Anhedonia is the inability to feel what,Pleasure +General,What does a phrenologist read,Skulls,General,Why did Louis Washkansky achieve world wide fame in 1967,First human heart transplant,General,On what common item would you find a worm,Corkscrew it’s the spiral part +General,Live Aid Took Place On The 13th July 1985 But Who Were The Very First Act To Take To The Stage,Staus Quo,Music,Who did the New Yardbirds become?,Led Zeppelin, Geography,What is the basic unit of currency for Bulgaria ?,Lev +Mathematics,Name the number system which uses only the symbols 1 and 0.,The binary system, Geography,What is the capital of United States ?,Washington,General,Sunrise Sunset came from which Broadway musical show,Fiddler on the Roof +General,What shape is a saggitated leaf,Arrow shaped What is the Oscar statuette holding Sword,General,Where did the bayonet originate,"Bayonne, france",General,White Fungus is the best selling canned what in China,Soup +General,Rock Groups: Tom Petty and the _____,Heartbreakers,General,Relating to food a Charentais is a variety of what,Melon,Sports & Leisure,What Is The Bed Of A snooker Table Made From ,Slate  +General,Where is the largest aquarium in the u.s,Chattanooga,General,What is the most rural state in the USA?,North Dakota,General,Rumpy and Rumpy Riser are both types of what creature?,Manx Cat +General,In which novel would Big Brother be watching you,1984,General,At Woodstock 1969 5550 what happened,Births,General,"Who was the father of Odin, Vile & Ve",Bor +General,Which eighties sitcom featured Tom Hanks in drag on a regular basis?,Bosom Buddies,General,In what is the Shannon trophy competed for,World Chess Trophy,Science & Nature,What is the common name for the larynx?,Voice box +General,Who wrote the opera' The Turn of the Screw',Benjamin britten,General,Who led the mongols,Genghis khan,General,"In Greek mythology, who was the father of Electra",Oceanus + Geography,Bismarck is the capital of ______?,North Dakota,General,Who wrote Pride and Prejudice,Jane austin, History & Holidays,What Nationality Was Hannibal? ,Carthaginian  +General,What make of car was driven by Nurse Gladys Emmanuel in the TV sitcom 'Open All Hours'?,Morris Minor,Technology & Video Games,What is the name of the video game inspired by the movie Alien? ,Xenophobe,Sports & Leisure,Who was the first player to score 100 goals in the Premiership? ,Alan Shearer  +General,Which singing King died in 1965,Nat king cole,Sports & Leisure,Name The 3 Female Characters In The Game Cluedo ,"Mrs White, Miss Scarlet, Mrs Peacock ",General,Singer Sarah Brightman was the second wife of which composer,Andrew lloyd webber +Music,Which British opera festival was started in 1934?,Glyndebourne,Sports & Leisure,Which darts player became the first in his sport to receive an MBE in 1989? ,Eric Bristow ,General,In sporting terms loose on left Tight on the right who is in centre,Hooker in Rugby +General,Mentu Egyptian Tyr Norse Gods of what,War,General,Clark Gable had what job before acting,Telephone Repairman,General,"Transport system in which trains glide above a track, supported by magnetic repulsion",Maglev +General,This term for those who oppose technological progress stems from 19th century working men who thought machinery would cause unemployment and societal degradation.,Luddites,General,Amanda by the Sea was a US version of what UK comedy show,Faulty Towers,General,Who is the sinister party man who apparently befriends Winston Smith in Orwell's 1984,O'brien +General,Where did gladiators fight professionally,Roman arenas,General,"Elementary in the house of lords, where does the lord chancellor sit",Woolsack, Geography,In which continent would you find the Yenisey river ?,Asia +General,In which film did orson welles play 'harry lime',The third man,Music,"Who Recorded The Albums ""The Blue Mask"", & ""Magic & loss""",Lou Reed,Sports & Leisure,"Which Striker Scored The 10,000 th Goal in the English Premier league for Tottenham Against Fulham in 2001 ",Les Ferdinand  +Sports & Leisure,With what did cricketer Mansoor Ali Khan Pataudi frequently play with in his hands?,Glass eye,General,Which Breed Of Dog Has Won Crufts The Most Number Of Times?,Cocker Spaniel,General,What school did Harry Potter goto,Hogwarts School of Witchcraft and Wizardry +General,From where is the music for the 'star spangled banner',Anacreon in heaven,General,Who wrote the novel SHE (both names),Rider Haggard,General,A Illinois law prohibit men from doing what in public,Having an erection +General,"Who WasThe First Male To Be Given The The Title Of ""Man Of The Year"" On Time Magazine",Charles Liindberg,General,What does 'yahoo' mean,Yet another hierarchical officious oracle,Music,Do I Do Was A Hit In 1982 For Whom,Stevie Wonder +Science & Nature, The __________ of a really famished camel may flop over and hang down the side of the body as the fat is used up.,Hump,General,On which island is Mount Suribachi,Iwo jima,General,"Who Did James Callaghan One Describe As ""The Sexiest Woman On TV""",Pat Phoenix +General,"Flattened, oblong organ that removes disease-producing organisms & worn-out red blood cells from the bloodstream",Spleen,General,"On maps, what is the 'you are here' arrow",Ideo locator,General,Which islands lie to the east of Kenya in the Indian Ocean,The seychelles + Geography,What is the capital of Michigan?,Lansing,General,Whose novels include 'The Ice-Cream Wars' and 'Brazzaville Beach',William boyd,Music,Which Queen Album Cover Is Silver With A Picture Of The Band In Leather Jackets,The Game +General,For what is tea from willow bark good for relieving,Pain,General,Which element has the symbol FE,Iron,General,"Traditional French blend ""fines herbes"" parsley chives chervil ?",Tarragon +General,In The Simpsons name the cat,Snowball,Art & Literature,Which Decorative Style Was popular In The 1920's & 1930's ,Art Deco ,General,Who began his professional career with black sabbath,Ozzy osbourne +General,"Germany's WW I allies were Austria-Hungary, Bulgaria, and ________",Turkey,Music,"Which Southampton Star Had UK No.1 In 2000 With ""Fill Me In"" And ""7 Days""",Craig David,Sports & Leisure,In Which British Newpaper Did The First Crossword Appear ,The Sunday Express  +General,"In 'dawson's creek', who does michelle williams play",Jennifer lindley,General,Which French underwater explorer invented the aqualung,Jacques cousteau,General,Clark Gable used to do it 4 or more times each day - do what,Shower + History & Holidays,What did Pennsylvania legalise before any other colony?,Witchcraft,Science & Nature," An eagle can attack, kill, and carry away an animal as large as a young deer. The Harpy eagle of South America feed on __________",Monkeys,General,Who wrote the 'father brown' crime stories,G k chesterton +General,Who led the raid on harper's ferry in 1859,John brown,Music,Limahl Had A Hit With What Kind Of Story,A Never Ending Story,General,What two things are used to stuff a welshman,Cheese Leek pastie + History & Holidays,Who did the singer Lulu marry in 1969? ,Maurice Gibb ,Music,"""Again"" Was A Hit In 1993 But Who Sang It",Janet Jackson,General,What is the state insect of Texas,Monarch Butterfly +General,Neil Tennant and Chris Lowe make up what pop band?,Pet Shop Boys,People & Places,In which country would you find the original Legoland? ,Denmark ,General,As foreplay what does a Ponapean male put in a woman's vulva,A Fish - then he licks it + Geography,In which county are all ten of England's highest peaks?,Cumbria,General,What sport still requires competitors to wear formal clothing,Snooker or Billiards,General,In what does michael jackson sleep,Cryochamber +General,Which African capital city is named from the Greek meaning 'three towns',Tripoli,Geography,In which state is Mount McKinley,Alaska,General,Louis Maxwell became well know for playing which role,Moneypenny in early Bond films +General,Who produced and directed the film 'Citizen Kane',Orson welles,Geography,How Many Bridges Are There In St Petersburg ,365 ,General,Who was the third and favourite son of David in Old Testament,Absolom +Sports & Leisure,Which Tennis Player Won Wimbledon Twice With An 8 Year Gap Between Victories ,Jimmy Connors ,General,In Willowdale Oregon a man cant do what while shagging wife,Curse swearing illegal,Music,"Who Sang ""My Heart Will Go On"" In 1998",Celine Dion +General,"Who was voted ""Time Magazine's"" man of the year in 1938?",Adolf Hitler,General,What is a group of this animal called: Rabbit,Nest,Music,Who Hosted The 1998 MTV Europe Music Awards,Jenny McCarthy +General,What was Lady Chatterley's first name,Constance,Sports & Leisure,With Which Sport Would You Associate A Lonsdale Belt ,Boxing , History & Holidays,"If a turkey evades the oven, what is its normal lifespan ",12 Years  +General,Garfunkel What is the smallest dinosaur so far discovered,Compsognathus,General,Deep purple started their career with which one-word song in 1968,Hush,General,What does the typical TV viewer do every three minutes and 42 seconds,Change +General,Who was the 3rd president of the U.S.,Thomas jefferson,General,What is a turkey's furcula commonly known as,Wishbone,General,Telephone poles are mostly made from what wood,Chestnut +General,J H Robertson invented what,Automatic Gearbox,General,Who sang 'bad case of loving you',Robert palmer,General,Of who were castor and pollux the twin sons,Zeus and leda +General,"What was the first name of Captain Bligh, of 'Mutiny on the Bounty' fame",William,Music,"Who Had A One Hit Wonder With ""Cotton Eye Joe"" In 1994",Rednex,Science & Nature,A relationship between two different types of organism which live together for their mutual benefit.,Symbiosis +General,Which artist is supposed to have used 1000 greens in painting,John Constable,General,What is the name of Alice's cat Disney Alice in Wonderland,Dinah,Food & Drink,Where Does Key Lime Pie Come From ,It Originated In Key West Florida  +Tech & Video Games,Who is Mega Man's creator? ,Dr. Light,General,Which south african oil company has estblished the only commercially proven 'oil from coal' operations in the world?,Sasol, History & Holidays,The Greek army under Leonides was annihilated here by Persians in 480BC.,Thermopylae +General,Symbolics.com was the worlds first what,Registered domain name,Food & Drink,"Cocktails: Triple sec, tequila, and lemon or lime juice make a(n) _________.",Margarita,General,What sporting contest Peter Christian win in Jan 77 with 1/16 oz,Angling - total (only) catch +Food & Drink,What beer was promoted with the line: 'Probably the best lager in the world'? ,Carlsberg ,General,A rare or unusual object,Curio,General,Where is calcutta,India + History & Holidays,This Roman killed himself after his defeat at Actium.,Marc Antony,Sports & Leisure,"This sport is called the ""American pastime"".",Baseball,General,Where on the human body is the skin the thinnest,Eye + History & Holidays,What Is The Significance Of The 1976 Film 'To The Devil _____ A Daughter' ,Last Horror Film Made By Hammer ,General,What is a nilgai,A large antelope,Music,Who Was Lead Guitarist With Mountain,Lesley West +General,Which sitcom helped launch Michael J. Fox's career by portraying him as a money-grubbing teenager?,Family Ties,Entertainment,Who is Warren Beatty's sister?,Shirley MacLaine,General,In which U.S. state is the Pentagon,Virginia +General,What mountain has the figures of three mounted confederate heroes of the civil war,Stone mountain,General,"Who, recently, has been appointed as patron saint of politicians",Thomas moore,General,"A gas produced by the incomplete combustion of coal in a mine fire is very poisonous, what is its chemical name",Carbon monoxide +General,Scoleciphobia is the fear of,Worms,General,Who replaced Bo and Luke Duke?,Coy and Vance, History & Holidays,Which Form Of Transport Was Used For The First Time By The British Police In 1967 ,Helicopters  +Entertainment,Kelsey Grammer sings and plays the theme song for which TV show?,Frasier,Science & Nature,Which Creature Has The Largest Brain In Proportion To Its Body? ,The Ant ,General,What is the young of this animal called: Beaver,Kit + Geography,What is the second highest peak in Africa?,Mt. Kenya,General,Donald duck comics were banned in finland because he didn't wear ______,Pants,Music,Which Pop Singer Was Desperately Seeking Susan On Film,Madonna +General,In the A Team name Murdoch’s invisible dog,Billy,Sports & Leisure,At Which Sporting Venue Are The Grace Gates ,Lords ,General,Which US state has the highest divorce rate,Arkansas +General,What type of pottery is the Collingwood Ontario area noted for,Blue mountain,General,What river was Francisco de Orellano the first to travel the length of,Amazon,General,What subject did 'mr chips' teach,Latin +People & Places,Which Actor Is An International Bridge Player ,Omar Sharif ,General,"What talk show hostess gave her guests the fewest opportunities to speak, according to a 1996 msU.S.urvey",Oprah winfrey,General,What is the atomic number for oxygen,Eight +General,"Who Was Responsible For Setting Up The Production Company ""Ardent"" In 1993",Prince Edward, History & Holidays,How many stab wounds did Julius Caesar have when he died ?,23,General,What capitol city stands on the Tagus River,Lisbon +Science & Nature,What are the pouched animals called,Marsupials,General,Where Does The Name Easter Come From ,An Anglo-Saxon goddess called Eastre ,Science & Nature,What is the chemical symbol for mercury?,Hg + History & Holidays,In which American state is George A. Romero's 1968 film 'Night Of The Living Dead' set ,Pennsylvania ,General,"Chub, gudgeon and perch are all types of what",Freshwater fish,Music,"The Musical Score To The Film ""Wall Street "" Was Written By Whch Former Member Of The Police",Stewart Copeland +General,What drink began in Morison's drug store Waco Texas in 1885,Dr Peppers,Music,Name The Record Company Formed By Berry Gordy,Motown,General,Billie Jean King competed in which modern sport,Tennis +General,Who is the current Secretary of State for Social Security,Alastair darling,General,The Shining' was the film playing at the drive-in in which film,Twister,General,If You Were To Be Awarded The “ Fields Medal ” What Would You're Occupation Be?,Mathmatician +General,In the 18th century Siberia used solid blocks of what as money,Tea,General,What animal was the symbol of freedom in ancient Rome,Cat,General,Which letter and number signyfy the vitamin riboflavin,B2 +General,In European city can you be jailed for not killing furry caterpillars,Brussels,General,Who is credited with inventing the fountain pen in 1848,Lewis waterman,General,The Spanish word Esposa means both wife and what,Handcuffs +General,What was the first food designed for the microwave,Popcorn,General,What is cerumen,Earwax,General,Do arteries carry blood towards or away from the heart,Away +Music,The Parents Of Which Rock N Roll Superstar Were Called “Vernon & Gladys”,Elvis,General,"Who said ""who knows what evil lurks in the hearts of men",The shadow,Music,The Album Simply Entitled The Beatles Is Usually Known By What Name,The White Album +Science & Nature, The electric eel's shocking power is so great that it can overtake its victims while __________,15 feet away,General,In which U.S. city does the Boeing aerospace company have its headquarters,Seattle,General,Albanian money and a grouse's mating display same word what,Lec +General,In science it can be up down strange top or bottom what can,Quark,Art & Literature,Who Wrote `The Hitchhikers Guide to the Galaxy'? ,Douglas Adams ,General,What was the first country to guarantee freedom of worship,Transylvania +General,What is meant by the cooking term farci,Stuffed,General,What is Iola cat that died on the episode of Mamma's Family.,Midnight,General,"What word can be a verb, noun, adjective, preposition, conjunction, interjection and a verbal auxiliary?",Like +General,"Which Former Chancellor Of The Exchequer Introduced The ""TESSA Savings Scheme",John Major,General,Who was the last English King to die on the battlefield,Richard III,General,"What is the purpose of ""caulking"" a boat",Making it watertight +General,In which country is the Commandaria wine region?,Cyprus,General,"On Three's Company,what's the first name of Mr. Furley's (landlord) tight wad brother who owned the building?",Bart,Technology & Video Games,What was the first Arcade game ever released? ,Computer Space +General,Who wrote 'across the river and into the trees',Ernest hemmingway,General,What country is North of Zambia,Zaire,General,What is another name for a football,Pigskin +Food & Drink,Greek Feta cheese is made from the milk of which animal? ,Ewe ,General,What part of the body does arthritis particularly affect,The bone joints, History & Holidays,Which silent film star was awarded a Knighthood in 1975? ,Charlie Chaplin  +General,In which Shakespeare play would you find Constable Elbow,Measure for measure,General,Are chemical compounds used to kill or inhibit the growth of infectious organisms,Antibiotics,Music,"Who Sang ""I Wanna Be Sedated"" From The Film ""National Lampoons Vacation""",The Ramones +Music,What was the first album Roger Waters released after leaving Pink Floyd?,The Pros and Cons of Hitch Hiking,Music,Which Number One Hit For Boyzone Was Written By Andrew Lloyd Webber?,No Matter What,General,"What nationally recognized day originated in Grafton, WV, in 1908",Mother's day +Music,"Which Band Recorded The Album Entitled ""Slippery When Wet""",Bon Jovi,General,Myrmecophobia is the fear of,Ants,General,What is the flower that stands for: esteem and love,Strawberry tree +General,Who is the only British Prime Minister to be assassinated?,Spencer Percival,Science & Nature," Mother prairie dogs will nurse their young only while __________ in the safety of the burrow. If an infant tries to suckle above ground, the mother will slap it.",Underground,General,Which is the largest lake in south america,Lake maracaibo +General,Where is guantanamo,Cuba,Technology & Video Games,What special track is unlocked in 'Excite Bike 64' if you finish the tutorial? ,Classic NES Excite Bike,General,Where is the monster Nessie said to live?,Loch Ness +General,What nationality was Johnny Weismuller,Romanian,General,In which city was the famous black hole,Calcutta,Science & Nature,Who Is Known As The Father Of Medicine ,Hippocrates  +General,Traffic lights were first used in London in which year,1869, Geography,What is the saltiest sea in the world?,The Dead Sea,General,George Lazenby played James Bond once in which film,On Her Majesty's Secret Service +General,"Which Singer Has Released Their 3rd Album In 2007, Entitled “ Life In Mono ” ?",Emma Bunton,Food & Drink,"Which English family brewer produces Eagle Bitter, Bombardier and also brews Red Stripe under licence? ",Charles Wells ,Food & Drink,Of what is bonito a variety? ,Tuna  +General,Who was time magazine's 'person of the year' for 1952,Queen elizabeth 2,General,I sing of arms and the man' is the first line of which famous work,The aeneid,Music,When The Monkees Re-United In The 80's What Was The Name Of Their First Single,That Was Then This Is Now + History & Holidays,"Who Had An 80's Hit With The Song 'Rock 'n' Roll High School,' ",The Ramones ,Music,On the 1974 Hit For The 3 Degrees What Does The Abbreviation TSOP Stand For,The Sound Of Philadelphia,General,Michael Jackson sang 'Ben' in what year,1972 +General,What's the longest river in the U.S.,Mississippi river mississippi,General,What amphibians do you raise if you run a ranarium,Frogs,Music,What Was Suzi Quatro's First Hit In 1973,Can The Can +General,What is the title of John Lennon's first published book,In his own write,Sports & Leisure,In Which Sport Might You Encounter A Pommel Horse ,Gymnastics ,General,What rank was George Armstrong Custer when he was killed,Lieutenant Colonel +General,In 1965 who became country music's first millionairess,Loretta Lynn,General,American composer who wrote the songs for the film 'Holiday Inn',Irving berlin,General,Japanese mattress used as a bed,Futon + Geography,This coutry holds the distinction of being the least densely populated in the world.,Mongolia,People & Places,What was Spike Milligan's real first name ,Terence , History & Holidays,"""In Which year Was The Band Aid Single """"Feed The World Released for The 2nd Time"""""" ",1989  +General,A lively Spanish dance in triple time performed with castanets or tambourines.,Fandango,General,"In Blazing Saddles, what is the last name of everybody in the town of Rock Ridge?",Johnson,General,Who first recognised the u.s in 1776,Croatia +Music,"According to the song ""Glass Onion"", who was the Walrus?",Paul,Food & Drink,How Are Truffles Found ,They Are Sniffed Out By Trained Pigs & Dogs ,General,What is the first thing that 97% of people will write when offered a new pen,Their name +Geography,What city does beacon hill light ,Boston ,General,"Cross garnet, Strap, Butt and Back flap types of what",Hinges,General,An Asian gecko and a sweet European wine what word fits both,Tokay +General,The main street in Back to the Future is also the main street in what other 80's movie?,Gremlins,General,What is the fear of wines known as,Oenophobia,General,Who is broomhilda's buzzard buddy,Gaylord +General,What is an object worn as a charm,Amulet,General,What indoor football game is named after the Latin Hobby Falcon,Subbuteo,General,Soceraphobia is the fear of,Parents-in-law +General,In literature who lived at 7 Savile Row,Phileas Fogg,General,What car manufacturers slogan is forward through technology,Audi – Vorsprung Durch Technik,General,Who are the only brothers to win the pga tournament,Lionel and jay hebert +Entertainment,As what is Merle Haggard also known as?,Okie from Muskogee,Geography,"The Bledowska Desert, in ____________ is the only true desert in Europe.",Poland,General,"What is the nickname for Charleston, South Carolina",Palmetto city +General,How long was the six day war,6 days,General,What Indian tribe did the army most often use as scouts,Crow,General,Who would you expect to find in Castle Gondolofo,The Pope +General,What is bovine spongiform encephalopathy more commonly known as,Mad cow disease,General,What was the world's first computer bug in 1946,A moth,Music,Which Toy Was The Title Of A Hit For Aqua?,Barbie Girl +General,What lasted 5 hours and twelve minutes in 1969 longest ever,Wimbledon TV match no tie break,General,Which King was the first to use the Royal We,Richard the Lionheart,General,Who is the Patron Saint of florists and gardeners,Saint Dorothy +General,What sort of tax system does Australia & New Zealand have,Goods & services tax gst,Food & Drink,What is the name of the syrup drained from raw sugar?,Molasses,General,"Who owns the nuclear power plant in the town of Springfield, where the Simpsons live",Montgomery burns +General,Saying: A bird in the hand is worth two in the,Bush,General,What does hepatitis affect,Liver,Music,Regarding The Tv Channel TMF What Do The Initials TMF Actually Stand For,The Music Factory +General,What country designed and developed the bayonet,France,General,Annika Hansen is which characters name in Star Trek Voyager,Seven of Nine,Sports & Leisure,What Number Is Often Called As (Clickety Click) In Bingo ,66  +Music,She Was The Greatest Of All Blues Singers But Nobody Placed A Headstone On Her Grave Until Janis Joplin Helped Fund A Memorial Many Years After Her Death Who Was She,Bessie Smith,General,What is the SI unit of capacitance,Farad,General,Which instrument does a cymbalist play,Piano +General,Alex Raymond created which comic strip character in 1934,Flash Gorden,People & Places,Who is David John Cornwell better known as?,John Le Carré,General,What is another name for gristle,Cartilage +General,What composer wrote the Pomp and Circumstance marches,Edward Elgar, Geography,What is the basic unit of currency for Senegal ?,Franc,Toys & Games,"In Monopoly, What is the cost to Get Out of Jail",50 + History & Holidays,"The American comedian and actor William Claude Dukenfield, died on Christmas Day in 1946. How was he better known? ",WC Fields ,General,What is the best wood for making pencils,Incense Cedar,Science & Nature,"Botanists, Gardeners & Chefs Know Their Allium Cepas What Is An Allium Cepa ",An Onion  +General,Business on what was the world's first adhesive used,Postage stamp,General,"Two creatures support the Royal Coat of Arms, one is a lion what is the other",Unicorn,General,In the Pink Panther films what is Inspector Clouseau's first name,Jaques +Geography,Which country has the longest land border,China,General,"Hundred thousand during the u.s civil war, how many union army blacks gave their lives",30,General,The Isle Of Man Belonged To 2 Countries Before It Underwent Uk Administration In 1765 Scotland Was 1 Name The Other,Norway +General,Sir Charles Babbage Was The Creator Of Which Modern Day Device?,The Computer,Music,"Name the composer of red,red wine?",Neil Diamond,Technology & Video Games,"In Ultimate Mortal Kombat 3, what animal does Mileena turn into in her Animality? ",A Skunk +Entertainment,Name Alvin & Simon's brother was ________,Theo,General,"Who won the Oscar for Best Director for the 1988 film ""Rainman""",Barry levinson,General,The screwdriver was invented before the ______,Screw +General,What is a funambulist,Tightrope walker,Sports & Leisure,"In horse racing, which three race courses stage the five English (Classics)? ","Doncaster , Epsom & Newmarket ",General,What do you give for a 55 year wedding anniversary,Emerald +General,Where do hippopotamuses do 80%of their vocalizations,Underwater,General,What key do most toilets flush in,E flat,General,"Who invented the word ""assassination""",Shakespeare +General,What is a group of eagles,Convocation,Art & Literature,The study of building design is ________.,Architecture,General,What is the fear of fog known as,Nebulaphobia +General,In which verdi opera does violetta sing 'sempre libera',La traviata, Geography,"Denmark, Norway and Sweden combine to make what ?",Scandinavia,Science & Nature,The Chemical Symbols For Titanium & Sodium Spell Out The Letters To Which Girls Name ,Tina  +General,What desert lies in Mongolia,Gobi, History & Holidays,In Which 1982 Film Did The Freeling Family Move To A Haunted House ,Poltergeist ,Religion & Mythology,"In Norse myth, there were two separate races of gods: the Aesir gods which included Odin and Thor, and the ____ gods from whom descended Freya.",Vanir +General,What is the fear of germs or contamination or dirt known as,Mysophobia,Science & Nature, Milk delivered to the store today was in the cow __________,Two days ago, History & Holidays,Which Law Was Passed At The End Of The Eighteenth Century Making Trade Unions Illegal? ,The Combination Acts  + History & Holidays,With With Horror Film Would You Associate The Character Leatherface ,Texas Chansaw Massacre ,Geography,Which us state would you be in if you were in chicago ,Illinois ,Sports & Leisure,What Is The Most Landed On Square In A Game Of Monopoly ,Jail  +General,"In which novel would you find reference to ""The Cracks of Doom""",Lord of the Rings,General,What is or who carries a flabellum,The Popes fan at ceremonies,Geography,What is the capital of Belarus,Minsk +General,In 1845 Boston it was illegal to do what without a doctors note,Bathe,General,What was the name of the backing group of Junior Walker?,The All Stars,Geography,What is the second longest river in the world? ,Amazon  +General,What is jimmy carter's middle name,Earl, History & Holidays,How long does it take a Christmas tree to grow before it's harvested? ,6-8 Years ,Music,"Who Had A Hit In 1966 With ""Tell It Like It Is""",Aaron Neville +Geography,"The streets of _________ were lit by gaslights for the first time in 1807. Before that, torches were used.",London,General,What nickname is attached to the US stealth fighter aircraft the f-117a?,Nighthawk,General,Who was the first of queen elizabeth's children to marry,Queen anne +General,What is a community of ants called,Colony,Sports & Leisure,The German athlete Jurgen Hingsen was always runner-up to which British athlete in major competition? ,Daley Thompson ,General,Delivery of child by cutting into the abdomen,Cesarean +General,Ships known as The First Fleet transported what in 1788,Convicts to Australia,General,Jupiter ii was the name of the robinson's spaceship on what series,Lost in, History & Holidays,"""In The Movie """"Miracle On 34 th Street"""" Kris Kringle Is Hired To Play Santa Claus In Which Department Store Is It FAO Swartz, Marshall Field's, Macy's Or Gimble's?"" ",Macy's  +General,Where does the abbreviation for pound lb come from,Libra the scales,Science & Nature, A 4_inch_long __________ can grip a rock with a force of 400 pounds. Two grown men are incapable of prying it up.,Abalone,Geography,In which city is the canale grande ,Venice  +Science & Nature, The __________ _ a relative of the mole _ is the smallest mammal in North America. It weighs 1/14 ounce _ less than a dime.,Pigmy shrew,Food & Drink,Which cooking term stems from the French word for coal ? ,Braise ,General,How many is a baker's dozen?,Thirteen +Geography,Warsaw is the capital of what country,Poland,General,The quetzal is the currency of ______,Guatemala,Entertainment,"What was the working title for The Beatles' song, 'Yesterday'?",Scrambled Eggs +Music,Who Wrote The Music To The 1994 Movie “The Lion King”,Elton John,General,Which British warrior was the queen of the Iceni,Boudicca,General,Who's first book was Pebble in the Sky,Isaac Asimov +General,If you suffer from diplopia what have you got,Double vision, History & Holidays,What country is poinsettia native to? ,Mexico ,General,Vaduz is the capitol of where,Liechtenstein +General,I get around was a hit for which group,Beach Boys,General,Name the dark lord in Lord of the Rings,Sauron,General,In Heraldry what symbol is a lymphad,Ship with oars +General,In Japan what is Jigali,Female Suicide,Music,"How Many Hours Did It Take The Beatles To Record Their First LP ""Please Please Me""",10 Hours,General,What is in the tyres of a commercial airline,Nitrogen - Air freezes +Food & Drink,This cut of beef lies between tenderloin & rump.,Sirloin,General,In what city is the Encyclopaedia Britanica based and published,Chicago,General,The minimum number of members required to be legal is known as a,Quorum +General,A mountain is the symbol of which film company,Paramount, History & Holidays,Who Released The 70's Album Entitled Slayed? ,Slade ,General,Jerome McElroy Is The Real Name Of Which TV Animated Star,Chef +General,Does a cat groom itself more in cold weather or in warm weather,Warm,General,In which sport was Argentinian Juan Fangio associated,Motor racing,General,"Conway Who, according to a song, damaged her foot on a piece of wood and fell into a raging torrent",Clementine +General,For what is 'gravidity' the medical condition,Pregnancy,General,Who painted the picture The Light of the World in 1854,Holman hunt,General,Dover is the State Capitol of which US state,Delaware +General,Diplomat living abroad as representative of their country,Ambassador,Science & Nature,What is the most common animal ?,Sea worm,General,Who is credited with invention of the nuclear reactor,Enrico Fermi +General,"What was the make and model of the villian in ""The Terminator"" 1984?",T-800 Cyberdyne Systems Model 101,General,Who was the last Scottish Prime Minister of Britain?,Sir Alec Douglas Home,General,How many masts does a Ketch have?,2 +General,Where is your Popliteal Fossa,Back of Knee,General,Which organisation began in a converted toilet in central London,Lords cricket,General,In heraldry what is a horizontal line dividing a shield called,Fess +Sports & Leisure,In Football Who Was Liverpool's Captain When They First Won The European Cup Final In 1977 ,Emlyn Hughes ,General,What two colours are most colour blind people unable to distinguish between,Red & green, Geography,St. George's is the capital city of what island country?,Grenada +General,Which sport allows substitutions without stoppage in play,Hockey,General,What is the flower that stands for: boldness,Pink,General,Which designer markets clothes under the 'Emporio' label,Giorgio armani +General,Which is the only king in a deck of cards without a moustache,King of hearts,General,What's the curved line between any two points on a circle,Arc,General,What is the flower that stands for: presumption,Snapdragon +General,What's a family group of lions called,Pride,Sports & Leisure,Name The 3 Balls Used In A Game Of Billiards ,"White,Spot,Red ",General,Jas Mann Was The Lead Singer Of Which Group?,Babylon Zoo +General,What is the most commonly used isotope of Uranium in nuclear fission chain reactions?,Uranium 235,General,Who wrote 'alice in wonderland',Lewis carroll,Geography,Name the last province to become part of Canada.,Newfoundland +Religion & Mythology,"In Greek mythology, who did Jocasta marry?",Oedipus, History & Holidays,"""What did my true love give to me on the """"Seventh"""" day of Christmas"" ",7 Swans A Swimming ,General,Which planet circles the sun every 84 years,Uranus +General,19-19-19 who's vital statistics,Olive Oyl,General,Who was the greek goddess of peace,Irene,General,What is 'html',Hypertext markup language +Science & Nature,Name the slowest moving land mammal.,Sloth,General,What planet in our system is not named after a god,Earth,Food & Drink,"If you were served crudit?s as a starter before your main meal, what would you be eating? ",Raw Vegetables  +General,Whose shrewish wife was named Xantippe,Socrates,General,What is the flavour of the herb Fennel,Anise,General, Androphobia is the fear of __________.,Male +General,Where were the hanging gardens,Babylon,General,A Romana Café features what liqueur,Sambuca,General,What colour is the gemstone peridot,Green +General,What kind of cake is a wedding cake normally,Fruit cake, History & Holidays,*What creature attacked President Jimmy Carter in a boat in 1979?* ,Duck ,General,What hairs are the last to lose their colour with age,Eyelashes +General,What is the state capital of Alaska,Juneau,Science & Nature,What Is Measured In Farads ,Cpacitance ,Science & Nature,"Which Sea Bird Has A Black & White Body And A Very Large , Bright Yellow & Red Beak ",Atlantic Puffin  +Music,"Which Beatle wife did Eric Clapton fall in love with, and write the song ""Layla"" for?",Patti Boyd Harrison,General,As what is 'Cape Town' also known,Kaapstad,General,What creature gets its name from the Greek word for womb,Dolphin from Delphis +Entertainment,Actor: ________ Hackman.,Gene,General,Which car manufacturer was the first to fit seat belts to their vehicles,Volvo,Geography,What is the capital of Zambia,Lusaka +General,Chambre means what when referring to wine,Serve at room temperature,General,A mixture of wine and soda water is known as a what,Spritzer,Sports & Leisure,Who Succeed Joe Fagan As Manager Of Liverpool? ,Kenny Dalglish  +Sports & Leisure,Who did Gazza flick the ball over for the Euro 96 goal against Scotland? ,Colin Hendry ,General,Scientists took the word quark from which authors work,James Joyce,Art & Literature,Who wrote the 'Father Brown' crime stories?,G.K. Chesterton +General,Comic strip character 1920s name means meek person in USA,Casper Milquetoast,General,Shooting Rabbits Talking German Cutting Finger old terms what,Farting – Victorian euphemism,General,Whose advert slogan was You press the button we do the rest,Kodak +General,"Who was radio's ""all american boy""",Jack armstrong,General,Great woman opera singer,Diva,General,Who runs the Spirit Foundation - Aged Abused Orphaned,Yoko Ono +General,During which war did the Battle of Jutland take place,World war 1,Science & Nature,What Was Once Thought To Give Rise To Influenza ,(Influenced By) The Stars ,Music,Which Song Was Performed By Julie Covington In 1973 And By Madonna In 1996,Don’t Cry For Me Argentina +General,What does the black and white BMW logo represent,Spinning Propeller – BMW made planes,General,"Pintado, Pochard, Scaup, Scoter and Smee types of what",Ducks,General,What is the hottest chile in the world,Habanero +General,Lyssophobia is the fear of what,Going Mad,General,What is the medical name for the thigh bone,Femur,General,"Guinness Modern ballroom dance, of Argentinian origin",Tango +General,What in Paris was erected to celebrate the anniversary of the French revolution,The eiffel tower,General,1991 Gulf War: What missiles did the Allies use to intercept Iraqi SCUDS,Patriots,Music,What Distinguished Bobby McFerrins 1988 Hit Dont Worry Be Happy,He Performed It A Capella +General,What is the name of the second highest mountain in Africa,Mount Kenya,Geography,What is the basic unit of currency for United Kingdom,Pound,General,Form of visible electric discharge between rain clouds or between a rain cloud and the earth (Electricity),Lightning +General,Which canal links Lakes Ontario and Erie,Welland,General,Who most often played James Bond's chief,Bernard Lee,General, The study of human pre_history is ___________.,Archaeology +Art & Literature,"From which Shakespeare play is this line taken: What in a name That which we call a rose, by any other name would smell as sweet.",Romeo and juliet,General,He died 28th July 1750 and had 20 children 6 survived name him,Johan Sebastian Bach,Art & Literature,"Who wrote ""The count of Monte-Christo""?",Dumas +General,Betty Boo was doing 'the doo' in what year,1990,General,In the language of flowers giving mint meant what,Virtue,General,"Who was the Swedish god of skiing, bowshooting & hunting",Ull +General,Rouget de Lisle did what to make him famous,Composed Marseillaise,Sports & Leisure,In 1993 Who Became Youngest Player To Be Crowned UK Snooker Champion? ,Ronnie O'Sullivan ,General,Who was the first Roman Emperor to adopt Christianity,Constantine i +General,In 1918 what were Jelly Babies renamed,Peace Babies,General,Everest climb 1953 put flags UK UN Nepal and where on top,India,Sports & Leisure,What number wood is a driver in golf,One + History & Holidays,In What Year Was The General Strike ,1926 ,General,Nucleomituphobia is the fear of,Nuclear weapons, History & Holidays,Which Revolution Began In 1789 ,The French Revolution  +General,What is a male cat,Tom,General,Who succeeded Charles de Gaulle as president of France?,Georges Pompidour,General,Which Ancient Greek character Fulfilled Prophecy By Killing His Father And Marrying His Mother,Oedipus +General,What did William Seward buy from Russia in 1867,Alaska,General,What is the Capital of: Egypt,Cairo,General,What family used to live in Bug Tussel,The Clampets +General,What is the name of the bone in the lower leg,Tibia,Sports & Leisure,"Which Brothers Represented England In The 1995 Rugby Union World Cup And Also Appeared With Their Mother In An Advert For Pizza Hut, All We Need Is Their Surname ",Rory & Tony Underwood ,General,"Fedora, bowler and boater are all types of what",Hats +General,Italian painter Jacopo Robusti is better known as who,Tintoretto,General,The caterpillar is the larval stage of which animal,Butterfly,General,What is the French name for a tart of cheese and bacon in a cream and egg filling,Quiche lorraine +General,Which was David Platt's first professional club,Manchester united,Sports & Leisure,Which Famous Ship Do The Runners Pass After Completing 10km Of The London Marathon? ,Cutty Sark ,General,"In Which Film Will You Find The Charcters ""Major Chip Hazzard, Butch Meat Hook, Brick Bazooka & Archer""",Small Soldiers +General,"Who recorded ""after the gold rush"" in 1970",Neil young,Music,"Who Had A Hit In 1983 With ""Feel like Makin Love""",George Benson,General,Which musical was based on the play The Matchmaker,Hello Dolly +General,The word 'marmalade' comes from the Portuguese word for what,Quince jam,Science & Nature,"Peritonitis, affects the ________.",Abdomen,General,What word is from the anglo-saxon 'dyppan',Dip +General,Collective nouns - a pace of what creatures,Donkeys,General,Good King Wenceslas was the King of which country,Bohemia (Germany),General,What job would a Foley Artist do,Incidental movie sound effects +General,Mysophobia is the fear of,Germs contamination dirt, History & Holidays,Which military battle took place in 1815,Waterloo,General,System what are fields of rice called,Paddies +General,The name of which disease comes from the Italian meaning 'bad air',Malaria,General,As loud as _______,Thunder,Toys & Games,"Name the only woman suspect in the game of ""Cluedo"" who isn't married.",Miss scarlett +General,Euphrates Who did Adolf Hitler dictate Mein Kampf to while in prison,Rudolf Hess,General,License Plates: What sport does KICKS enjoy,Soccer,General,Name the author who created Hannibal Lecter,Thomas Harris +Music,Which Band Was Formed In The 1970's By Fomer Miles Davis Band Members Joe Zawinul & Wayne Shorter,Weather Report,General,In the UK marmite is a spread but what is a marmite in France,Tall straight cook pot,Geography,Where are the pyramids located,Egypt +General,"In the game 'banjo-kazooie, who is tooty's big brother",Banjo,General,Where are the two steepest streets in the u.s.a,San francisco,Science & Nature,What Is The Drug Aceta-Minophen Better Known As ,Paracetamol  +General,Craig Evans Hit The Headlines In Sensational Style In May 2001 But Can You Tell Me Why,The Man Who Threw Egg At John Prescott,General,The film Midnight Express is set in which country,Turkey,General,"Mixture, blend alloy of any metal with mercury",Amalgam +General,What weapon is named from musical instrument inv Bob Burns,Bazooka,General,Three what appear on the Connecticut state flag,Grape Vines,General,What heavyweight boxer was nicknamed The Cinderella Man,James J Braddock +General,Who was the English born Surveyor-General of India who completed the first trigonometrical survey of the Sub-Continent,Sir george everest,General,The Semites are a group descended from whom,Shem - Noah's son,General,Name the First Arcade Game Manufactured By Atari,Pong +Art & Literature,"This magazine used to boast a circulation of 7,777,777.",Better homes and gardens,Geography,Which Strait Separates Alaska From Russia? ,The Bering Strait ,Food & Drink,What Vegetable Is Also Known As Zuchinni ,Cougettes  +General,Saint Bibiana is the Patron Saint of what,Hangovers,General,"In the film, Jerry McGuire, what was the name of Jerry's stepson?",Ray,General,What was the connection between Family Matters & Full House?,Steve Urkel +General,What is the connection between Jeffersons and Good Times?,Janet Dubois,Food & Drink,Which countrys does one associate with the following foods or drinks: 'Lussekatter' ,Sweden ,General,Which American city is nicknamed The Birthplace of Aviation,Dayton Ohio +General,In what Australian state would you find Launceston,Tasmania,General,The mouth arachnophobia is a fear of ______,Spiders,General,Who owned the research ship 'calypso',Jacques cousteau +General,Bogota is the capital of ______,Colombia,Art & Literature,Dr. Seuss wrote this book: The Cat in the ______.,Hat,General,Of the Somme With what acid do nettles cause irritation,Formic acid +General,"Who wrote ""Paradise Lost""",John milton, History & Holidays,What city was destroyed by little boy ,Hiroshima , History & Holidays,"Which theatre, that `never closed', closed in the sixties for good? ",The Windmill  +General,What is the flower that stands for: dauntlessness,Sea lavendar,Science & Nature,What Is Unusual About Goats When They Sleep? ,They Don't Close Their Eyes ,Music,"With Whom Did Louis Armstrong Sing About Virtues Of Jazz In The Musical ""High Society""",Bing Crosby +General,Who created the fictional character 'Tristram Shandy'?,Laurence Stern, History & Holidays,How Old Was Elvis Presley When He Achieved His First UK No.1 With 'All Shook Up'' ,22 ,Science & Nature,"Name the fastest land animal over a prolonged distance (1,000 yd. plus)",Antelope +General,Where are the guards who wear bearskins,Buckingham palace,Sports & Leisure,In which country did the game of ice hockey originate? ,Canada ,Geography,Which country are the Galapagos Islands part of,Ecuador +General,A Suffragan has what job,Bishop - no parish – helps other,General,What was Casanovas day job,Librarian,General,What is the flower that stands for: unconscious beauty,Burgundy rose +General,"In law, a formal document stating that a person (defendant) is notified to appear in court and answer a complaint or charge brought against him or her by another party (plaintiff).",Summons,General,"In London what are The Cavalry, Marlborough and Savile",Private Members Clubs,Music,Life Is A Rollercoaster Is The Biography Of Which Member Of Boyzone,Ronan Keating +General,What nhl star was known as the thinking man's goalie,Ken dryden,General,The Black Prince was the first holder of which Royal title,Duke of cornwall,General,What is the most played song on radio USA in 20th century,You've lost that loving feeling +Language,What does N.A.S.A stand for?,National Aeronautics and Space Administration,General,Which NATO country is the world's largest producer of Uranium,Canada,General,Although the 'Fabulous Thunderbirds' sang 'I Thank You' it was originally released by what soul duo in 1968.,Sam and Dave +General,What is Homer Simpson's middle name,Jay,General,Phil Collins played what character on the London stage 1960s,Artful Dodger,General,Riyadh is the capital of ______,Saudi arabia +General,"In a famous opera, who understood the speech of birds after tasting dragon's blood",Siegfried,General,The first one was 5 x 20 the first what,Spreadsheet,General,The Indestructible Iron man fights against the Electronic Gang Hong Kong translation of what film ,A View to a Kill +General,What is activated for freckles to appear,Melanocytes,Art & Literature,Which Outlaw Rode A Horse Called Black Bess ,Dick Turpin ,Sports & Leisure,In Which Year Did Torville & Dean Win Olympic Gold With Their Bolero Routine? ,1984  +Sports & Leisure,What is the name given to a rower who competes in an individual event?,Sculler,Music,Which Actress Was Paul McCartneys Girlfriend Before Linda Eastman,Jane Asher,General,Gemorrah who killed goliath,David +General,Water containing carbon dioxide under pressure is called ________,Soda water,General,Keraunophobia is the fear of,Thunder,General,Who invented the ice cream sundae,William garwood +General,68% of Americans do what (Trying to be punctual),Set their watches ahead,General,"In 1939, Albert Einstein wrote a letter to Roosevelt urging him to develop what",Nuclear bomb,Music,If You Were To Dance With The Guitar Man Who Would Be Your Partner,Duane Eddy +General,The Old English word 'fneosan' means what nowadays,To sneeze,General,"""Open Road"" Was The Debut Album For Which Well Known Singer / Songwriter",Gary Barlow,General,"Gregory Pincus, John Rock, Gerhart Domangk developed what",Oral Contraceptive +Music,Which Group Would You Associate With Steve Craddock,Ocean Colour Scene,General,What writer was paid $5 for writing thanks,Rudyard Kipling,General,Which recreational activity causes the most bone fractures,Aerobic Dancing +General,What is the base twenty numbering system,Vigesimal,General,Nudophobia is a fear of ______,Nudity,General,What is the Capital of: Colombia,Bogota +Music,Who Is Or Was The Oldest Member Of The Beatles,Ringo Starr,General,What did Benjamin Franklin claim as his trade,Printer,General,Who is known as The father of English poetry - 1340 - 1400,Geoffrey Chaucer +General,What is the only silent film to win best picture Oscar,Wings,General,What arthurian knight had the strength of ten because his heart was pure,Sir,Music,Who Recorded A Track In 1984 That Went To No.1 In 1993 Owing To A V W Advert,The Bluebells / Young At Heart + History & Holidays,In which US City was Martin Luther King assassinated in 1968? ,Memphis ,General,"Which actress said ""Being a sex symbol is like being a convict""",Raquel Welch in 1979,General,In Fiction Her Maiden Name Was Lily Evans But Who Is Her World Famous Offspring,Harry Potter +General,Where did the dormouse finish up at the Mad Hatter's tea party,In the teapot,General,Gulyas soup is a delicacy in what country,Hungary,Food & Drink,Who Invented Muesli ,"Dr Muesli, Who Was Swiss " +General,What is the oldest most widely used drug on earth,Alcohol,General,In Britain what year was the Forth Railway Bridge opened,1890,General,What major city is served by Gatwick Airport,London +General,Who did 'tennis world' name rookie of the year in 1974,Martina navratilova,General,Stephano and Trinculo characters in which Shakespeare play,The Tempest,Music,Where Did Buddy Holly Play His Final Show Before Taking Off On That Ill Fated Flight,Clear Lake +Science & Nature,Which is the largest known butterfly?,Queen Alexandra's Birdwing,General,Chablis comes from what major wine producing area of france,Burgundy,General,Which game was called Beano till Edwin Lowe renamed it,Bingo +General,What was the most valuable thing ever stolen,The Mona Lisa,Music,Francis Rossi Is The Lead Vocalist With Which Band,Status Quo,Music,"Who Had A Hit With ""Good Thing""",Fine Young Canibals +General,In what traditional entertainment does the dog Toby appear,Punch and Judy,General,What were Cinderella's slippers originally made from,Fur - changes to glass in 1600s,General,"What drink is made of rum, coconut milk and pineapple",Pina colada +General,A statue of Lady Godiva stands in the centre which English city,Coventry,Music,"In Which Year Were The Following All UK Chart Hits: ""To Cut A Long Story Short"" By Spandau Ballet, ""Pulling Mussels From The Shell"" By Squeeze And ""Perfect Cousin"" By The Undertones?",1980,General,Who followed Grover Cleveland as U.S. President in 1889,Benjamin harrison +Food & Drink,What Is Aspic ,Jelly Made From Stock , History & Holidays,Who Were 'Kissing In The Back Row'' Of A Movie In 1974 ,The Drifters ,Entertainment,"Who directed ""Jurassic Park III?""",Joe Johnston +General,Patricia McCormick became USA first what January 20th 1957,Bullfighter Ciudad Juarez Mexico,General,"Who's Songs Appear On The Entire Soundtrack To The Movie ""When Harry Met Sally""",Harry Connick Junior,General,Paul Keating was elected prime minister of Australia in which year,1991 + History & Holidays,"What Did Frosty The Snow man Have For A nose (Carrot, Button,Cherry,Coal) ",Button ,Music,Which song was a number two hit for The Osmonds in 1974 and a number one hit for Boyzone in 1994?,Love Me For A Reason,General,"What Part Of A Persons body Would You Be Afraid Of If You Had ""Geniophobia""",Their Chin +Entertainment,"In the film 'Pretty Woman', for who was Goldie Hawn the body double?",Julia Roberts,General,What insect does an isopterpophobic homeowner fear,The termite termite, History & Holidays,"In Charles Dickens' novel A Christmas Carol, who was Scrooge's dead business partner? ",Jacob Marley  +General,"Although not all come from France, ______ fries.",French,General,Who is the chief of lamaism in tibet and mongolia,Dalai lama,General,What is a group of this animal called: Elephant,Herd +Science & Nature,The visible spectrum of light ranges from red to ________.,Violet,General,Zapateodo is a rhythmic device used in what music style,Flamenco,General,In a famous Disney film who are Flora Fauna and Merryweather,Fairy Godmothers Sleeping Beauty +General,The Vatican is the worlds smallest country what's second,Monaco,General,Which actor made his debut in the 1958 film Cry Baby Killer,Jack Nicholson,Science & Nature, A cat's __________ can't move sideways.,Jaw +General,"In Welsh place names Llan- is a common feature, what does it mean",Church,General,"Ken Thompson & Dennis Ritchie, colleagues at Bell labs, teamed up & wrote the second version of which operating system",Unix,General,What is the name for the theoretical end product of the gravitational collapse of a massive star,Black hole +General,U.S. Captials - Oklahoma,Oklahoma City,General,In the Wizard of Oz name the Good Witch of the North,Glinda,General,The average male loses a lb (weight ) of what in 10 years,Beard +Sports & Leisure,Who Was The First Player To Score 100 Goals In The Football Premiership League? ,Alan Shearer ,General,Who was the founder and conductor of the Black and White Minstrels,George mitchell,General,Fill in the blank: ____ the lonely,Only +Food & Drink,What colour top do bottles of unpasteurized milk have? ,Green ,General,For what would you use zener cards,To test for ESP,General,In Utah where is it illegal to fish,From Horseback +Science & Nature,What are the two kinds of blood corpuscles in vertebrates ,Red and white ,General,Capital cities: Cameroon,Yaounde,Music,"In Which Year Did Bianca Become Mrs Mick Jagger (1970, 1972, 1974, 1976)",1970 + Geography,On what island is Pearl Harbor?,Oahu,General,What does bbiab mean,Be back in a bit,Music,"Which UK Instrumental Duo Are Responsible For ""Chime"", ""Satan"" And The Box",Orbital +Music,"When Aretha Franklin Got Married In 1978, Which Stevie Wonder Song Did The 4 Tops Sing As She Walked Down The Aisle",Isn't She Lovely,General,What is changed into a coach for Cinderella,Pumpkin,Geography,In which country is madras ,India  + Geography,What is the capital of Tennessee?,Nashville,General,What countries head of state has been dead for years,North Korea - Mr Kim,General,In which sport is there a York round,Archery +General,What Dickens work features Mr Wardle,Pickwick papers,General,Which character was born in Riverside Iowa,James Tiberius Kirk,People & Places,"Who Once Said Of The Navy 'It's Only Traditions Were Rum, Sodomy & The Lash' ",Winston Churchill  + Geography,What is the capital of Antigua and Barbuda ?,Saint John's,Food & Drink,What Type Of Meat Usually Goes Into Frankenfurters? ,Pork ,General,What is a star called that has a fainter companion,Double star +General,What is Gerber's most popular flavour of baby food,Mashed Bananas,General,The average American does what for 52 minutes a day,Read a Newspaper,General,Standard Italian dances and their music of the fifteenth and sixteenth centuries.,Ballo + History & Holidays,Which famous actor is honored in a statue in Leicester Square?,Charlie Chaplin,General, Brontophobia is the fear of _________.,Thunder,Geography,What is the capital of Angola,Luanda +General,What was the White House formerly known as,Executive Mansion,General,Who won Best Director for Reds,Warren beatty, Geography,In what country is Thunder Bay?,Canada +General,Analogy bull-cow as fox- __________,Vixen,Science & Nature,Which Port Handles The Greatest Amount Of Trade In The World ,"Rotterdam, The Netherlands ",General,What's the more common name of the thyroid cartilage,Adams Apple +General,Vegetable with greenish flower heads,Broccoli,General,Name Stanley Kubrick's last film as director before his death,Eyes Wide Shut,General,"What landmark bears an inscription that ends, ""I lift my lamp beside the golden door""",Statue of liberty +Music,Who Hosted The 2007 Brit Awards?,Russell Brand,Food & Drink,What breakfast cereal was invented at Battle Creek Sanitarium ,Cornflakes ,Food & Drink,What Type Of Foodstuff Is A 'Rollmop'' ,Fish (Pickled Herring)  + History & Holidays,Which 1950s films took place in Chicago and Miami 1929 ,Some Like it Hot ,Science & Nature,In computing what does the general everyday term ISP stand for? ,Internet Service Provider ,Food & Drink,What was taken ver by Ray Kroc in 1954? ,McDonald's  +Science & Nature,Why Was The Edmontosaurus So Named ,It's Remains Were First Discovered In Edmonton ,General,What does a piscivorous creature eat,Fish,General,Which TV series starred Leslie Phillips and donald Sinden as clergymen,Our Man At Saint Marks +General,The vernal euinox is the beginning of ________,Spring, History & Holidays,"What do Karen, Richard and Joseph all have in common ",All Carpenters ,General,What is the young of this animal called: Zebra,Foal +Food & Drink,The Queensland nut or bush nut is more commonly known as this.,Macadamia,General,Which constellation is represented by a goat?,Capricorn, History & Holidays,"In 1956, Tunisia gained independence from which country? ",France  +General,"Heavier-than-air craft that derives its lift not from fixed wings like those of conventional airplanes, but from a power-driven rotor or rotors, revolving on a vertical axis above the fuselage",Helicopter,General,What nation is nicknamed the 'regaa boyz',Jamaica,Geography,On which River does the City of Cairo stand ,The Nile  +Art & Literature,In Which City Is The Encyclopediaa Britanica Published ,Chicago , History & Holidays,She was Queen of Egypt and mistress of Julius Caesar.,Cleopatra,General,According To Official EU Trade Figures Name The European Country That Produces The Most Computer Software,Ireland +Sports & Leisure,Over Which Distance Did Steve Ovett Win Olympic Gold? ,800 Meters ,Sports & Leisure,Which Boxer Had The Nickname Of The Dark Destroyer? ,Nigel Benn ,Religion & Mythology,Who is the greek equivalent of the roman god Discordia,Eris +General,In Athens they can remove your driving licence if found what,Poorly dressed or unbathed,Food & Drink,What type of fruit is a damson? ,Plum ,General,What was the most popular semi automatic hand gun in Nazi Germany,The luger luger +General,On which of the Canary Island would you find the holiday destination of Corralejo,Fuerteventura,General,Whose is supposed to have had sex with his nanny when aged 9,Lord Byron,Science & Nature," The Alaskan __________ is the largest deer of the New World. It attains a height at the withers in excess of 7 feet and, when fully grown, weighs up to 1,800 pounds.",Moose +General,In approximately what year was steel first made,500 BC,Sports & Leisure,What's the nickname of the University of Georgia football team?,Bulldogs,Geography,Which country had the world's tallest habitable building as of 2006? ,Taiwan (Tapei 101)  + Geography,In which continent would you find the Niger river ?,Africa,General,Chapman Root designed it based on a Hoople skirt - what,Coca Cola bottle,General,Santa fe is the capital of what state,New mexico +General,Crossword Clues: Shriveled-up and dry. (4),Sere, Geography,What is the capital of North Korea ?,Pyongyang,General,What president had a slave for a mistress,Thomas jefferson + Geography,Which is the Earth's largest continent ?,Asia,General,"Which Eurovision Song winning group's line-up was Mike Nolan, Bobby G, Jay Ashton and Cheryl Baker",Buck's fizz,General,Ouranophobia is the fear of,Heaven +General,What is the term for a person who designs dance routines,Choreographer,General,What NHL star was known as The Roadrunner,Yvan cournoyer,General,Who sometimes used the pseudonym Al Brown,Alphonse Capone +General,The drink Absinthe is also known as?,Wormwood,General,Which Arthur first conceived the idea of geostationary satellites,Arthur c clarke,General,What scientific field did John Tebbutt excel at,Astronomy +General,What is the Capital of: Sao Tome and Principe,Sao tome,People & Places,Can You Name The 4 Stars Of The Goon Show ,"Michael Bentine, Peter Sellers, Spike Milligan, Harry Secombe ",General,Which book of words has a latin name that means 'treasure',Thesaurus +Geography,Which Bridge In Scotland Replaced The Much Loved(Ferry At The Kyle Of Lochalsh) ,The Skye Bridge ,General,What is the left side of a ship called,Port,Food & Drink,In which country did edam cheese originate?,Holland +General,"Which Iconic Rock Music Frontman Sang Backing Vocals On Carly Simons 1973 Hit ""You're So Vain""",Mick Jagger,Sports & Leisure,How Many Dominoes Are There In A Full Set ,28 ,General,Who wrote the novel Heidi,Johannes Spyri + History & Holidays,What Happened At Max Yasgur's Dairy Farm In New York State During 15-17th August 1969 ,Woodstock Festival ,General,What is the point value of the 'f' in scrabble,Four,General,Whose most commercially successful album was 'court and spark' in 1974,Joni +General,In which country would you find the motor racing circuit called Kyalami,South africa,General,"At which conference in 1944 was the International Monetary System, including the I.M.F. and the World Bank, set up",Bretton woods,General,In 1929 the first what happened on an aircraft,Birth +General,Which is the deepest mine,Western deep levels mine,General,"Who sang the Song ""Beautiful Day""?",U2,Geography,What is the capital of Kyrgyzstan,Bishkek + Geography,Jakarta is located on which Indonesian island?,Java,General,What did joseph priestley invent,Carbonated soda water, Language,"The name for this semi_precious stone comes from the Latin for ""sea water""",Aquamarine +General,What country does Queen Beatrix rule,Netherlands,General,Odele and Odette appear in what Tchaikovsky ballet,Swan Lake,General,Philematology is the science of what,Kissing +Geography,The northernmost point in mainland Australia is on this geographic feature,Cape york,General,Babba Louey was the sidekick of which cartoon character,Quick Draw McGraw,General,In the USA what is Marine One,Presidents Helicopter +General,Americans spent roughly how much dining out in 1993,$267 billion,General,"Which Sport Took Place At ""Lords Cricket Ground"" When London Hosted The 2012 Olympic Games",Archery,Music,Who Was The First Beetle To Have A Solo No.1 Hit,George Harrison +Religion & Mythology,Roman god of doorways and passages. Two headed deity from which we get the name of one of our months?,Janus,General,In what country are the Drakesberg mountains,South Africa,General,What is a group of piglets,Litter +Music,In One Of Her First Major Acting Roles Who Was Madonna Desperately Seeking,Susan,Science & Nature,The remains of prehistoric organisms that have been preserved in rocks are called ________.,Fossils,General,The pop group 'The Cardigans' hail from which country?,Sweden +General,Who composed the Goldberg Variations,J. s. bach,General,Hal Jordan was the original alter ego of which comic super hero,Green Lantern now Kyle Rayner,General,What was the connection between The Facts of Life and ER,George Clooney +General,In which country are Mariachi bands traditional,Mexico,Music,Which Tv Theme Reached No.15 In 1971Featuring the Vienna Philharmonic Orchestra,The Onedin Line,General,Gene Hackman sheriff Big Whiskey - got Oscar - What film,Unforgiven +Science & Nature,Which Part Of The Body Is Affected By Rhinitis ,The Nose ,General,"Other than the 'Tarzan' series of films, which other film and television character was created by Johnny Weismuller",Jungle jim,General,What does the lacrimal gland produce,Tears +General,What firm markets the B25 microcomputer,Burroughs,Sports & Leisure,Which British man ran the fastest mile in the 80's? ,Steve Cram ,General,What is dram,Dynamic random access memory +General,Terrance Nelhams became better knows as who,Adam Faith,General,Is dublin in northern or southern ireland,Southern,Sports & Leisure,This is the most coveted trophy in Candian football.,Grey cup +General,Which actor took the male lead in the Hitchcock thriller The Birds,Rod taylor,General,On what part of the body are campers prone to being bitten by vampire bats,Big toe,General,What is the Capital of: Lebanon,Beirut +General,By what name is the reed pipe of the bagpipes known,Chanter,General,Which sort of court case causes the most perjury,Contested Divorce,General,What was the first penal colony in New South Wales,Botany Bay + Language,"What word contains the combination of letters: ""xop""",Saxophone,Music,Brother Records Was Formed By Which Group,The Beach Boys,General,"Lewis 1994 - How many copies has the #3 ""Eagles Greatest Hits"" album sold",Fourteen +General,What does the word Desert (from Latin desertus) translate as,Abandoned,General,Khu-fu is more commonly known as,Cheops,Music,"Who Had Hits With ""You Keep Me Hanging On"" & Cambodia",Kim Wilde +General,In the USA what are the TV equivalent of the Oscars,Emmys, History & Holidays,Who was the first chancellor of West Germany after WW II?,Konrad Adenauer,General,What is the telephone's u.s patent number,174465 +Sports & Leisure,In Which Game Are Projectiles Thrown At Stakes Called Hobs ,Quoits ,Music,What Was Otis Redding's Biggest Hit Coming After His Death In 1967,Sitting On The Dock Of A Bay,General,What is the flower that stands for: change,Pimpernel +General,Button Gwinnett Born In Gloucester England On Aril 10th 1735 Was The First Person In History To Do What,Sign The Declaration Of Independence,General,Which two colours appear on the Vietnam flag,Yellow & red,General,Musashi was the first Japanese to use two what simultaneously,Swords - famous for it +General,"The great gothic cathedral of Milan was started in 1386, & wasn't completed until what year",1805,General,"Four European countries keep Greenwich Mean Time. The UK and Ireland are two, name either of the others",Iceland Portugal,General,In which country was Nelson Piquet born,Brazil +General,Pharmacophobia is a fear of ______,Drugs,General,What Disney film was released on December 21st 1960,Swiss Family Robinson,Science & Nature,"Which Mammal Fires A Mixture Of Methane, Butane And Sulphur From Its Scent Glands? ",The Skunk  +General,In what year was Prince Harry born?,1984,General,Musical groups: commander cody and his _____,Lost planet airmen,General,"In wich year was formed in Germany, the disco band Boney M",1976 +Geography,Which Of The Worlds Continents Has The Highest Population ,Asia ,General,What does ETA stand for,Estimated time of arrival,General,A Woman to Remember was the worlds first what in Feb 1947,TV Soap Opera +General,Who would you expect to see in the Leftorium,Ned Flanders shop The Simpson's,General,What does 'i.b.m' stand for,International business machines,General,What year did Brasilia become capital of Brazil,1960 +General,How long was jesus' temptation in the desert,40 days,General,"In the culinary world, what is passata",Sieved tomatoes,General,Which part of the body is affected by encephalitis,Brain +People & Places,Who Is The Heaviest Member Ever To Have Sat In Parliment ,Cyril Smith ,General,The name Mark translates a what,Hammer,General,"On Dec 25th 1989 David Hasselhoff Topped The Bill Of A Concert In Which He Performed His Most Successful Hit ""Looking For Freedom"" But Where EXACTLY Did He Perform The Song",On Top Of The Berlin Wall +Music,Humble Pie Sang About A Natural Born What In The Late 60's,Bugie,General,What band leader did singer Jo Stafford marry,Paul weston,General,Calvados' is a brandy made from what,Apples +General,Name the largest artery in the human body.,Aorta,General,"What do you have if you feel ""crapula""",Hangover,General,Which country produces Franconia wine,Germany +General,What drug is obtained from the cinchona tree,Quinine,General,"""They're gr_r_r_r_eat!"" is this this cereal's slogan.",Frosted flakes,Food & Drink,In The Dish 'Angels On Horseback' Of What Are The Angels Made ,Oysters  +General,Who captained the hms beagle,Charles darwin,General,What is a portuguese man o' war,Jellyfish,Sports & Leisure,How High In Feet Is The Net In A Game Of Badminton ,5ft  +General,Who won a Best Actress Academy award for her performance in Annie Hall,Diane keaton,General,What was Ethiopia formerly known as,Abyssinia,General,Which insect is so-called because it was dedicated to the Virgin Mary in the middle ages,Ladybird + Geography,Which element makes up 2.6% of the Earth's crust ?,Magnesium,General,"What society in england, dating from 1617, has it's own degree which allows a person to practice medicine",Apothecaries,General,"Who wrote the thrillers 'Harry's Game' and ""A Song in the Morning""",Gerald seymour +Music,Which Prince Song Did Age Of Chance Later Decide To Cover,Kiss,Geography,Which Country Has The International Car Registration EAK? ,Kenya ,General,Hungarian doctor Karolyn Maria Beekert coined what word 1869,Homosexual +General,In Denver Colorado it is illegal to mistreat who / what,Rats,Sports & Leisure,At Which Sporting Venue Do Competitors Travel Down The Brabham Strait? ,Brands Hatch ,General,A long broad tree lined street,Boulevard +Sports & Leisure,"Who was the NBA MVP in 1976, 1977 and 1980?",Kareem Abdul-Jabbar,General,What show was Just the 10 of us spin off of?,Growing Pains,General,"In folklore, supernatural, sea-dwelling creature with the head and upper body of a beautiful woman and the lower body of a fish",Mermaid + History & Holidays,What American feminist went bust as a silver dollar,Anthony,General,What is a corduroy road made from,Logs laid down on swampy ground,General,Which American chat show hostess apppeared in the film 'The Color Purple',Oprah winfrey +Religion & Mythology,Who is the greek equivalent of the roman god Cupid ?,Eros,General,How many laps are there in a Speedway race,Four,General,A male racehorse can do it in 14 seconds - what,Copulate as can any male horse +General,In which region of France is the red wine Chateauneuf du Pape produced,Rhone valley,General,Which 2003 Movie Saw Hugh Grant Playing The Role Of The British Prime Minister ,Love Actually ,General,What is the Roman Numeral for 1000,M +General,What is the UKs best selling chocolate snack bar,Kit Kat,General,A Blue Tits breast is what colour,Yellow,Science & Nature,"This Latin word meaning ""iron"" is the reason for iron's modern day chemical symbol (Fe).",Ferrum +Music,What 2 Parts Of The Body Are The First To Be Mentioned In The Song You've Lost That Loving Feeling,Eyes & Lips,General,Degrees who was the indian maiden in johnny preston's 'running bear',Little white,General,License Plates: What school does HOYA6 attend,Georgetown university +General,"On ""Three's Company"" what was Larry's (the upstairs neighbor) last name?",Dallas,General,Part of a city occupied by a minority group,Ghetto,General,The Ladies Mercury in 1693 was the worlds first what,Women's magazine +Music,Which Artists Wore A Peacock Suit,Paul Weller,General,The only rock that floats in water is what,Pumice,Music,Which T-Rex Song Did Power Station Cover,Get It On +Science & Nature,What 2 Word Term Is Given To A Simulated 3D Environment Used in Computer Graphics ,Virtual Reality ,General,How many legs has a woodlouse,Fourteen,General,Who Was The First White Act To Be Signed By The Predominantly Black Motown Label?,Kiki Dee +General,What Disney character was voiced by Pinto Colvig,Sleepy,Music,John Waite Was Lead Singer For Which Band,Bad English,General,Maniophobia is a fear of ______,Insanity +General,"Calypso, catteleya and pogonia are types of which flowering plant",Orchid,General,How Many Gold Medals Did Jesse Owens Win At The 1936 Olympics In Germany,Four,General,What does an orometer measure,Height above sea level +Science & Nature,What is the fourth state of matter?,Liquid crystals,General,What disease is carried by the tsetse fly?,Sleeping sickness,Music,Who Was The Lead Singer With Tubeway Army,Gary Numan +General,On Beverly Hills 90210 What was the name of Brandon's first car?,Mondale,General,"What was the name of the ship, the survivors of which were rescued by Grace Darling, her father and others",Forfarshire, Geography,What is the basic unit of currency for Pakistan ?,Rupee +Sports & Leisure,Back in the 1890's which football club used to wear pink shirts? (Still In Premiership) ,Everton ,Science & Nature,Where Might You Find Rods and Cones ,In The Eye Light Sensitive Cells , History & Holidays,What was the name of the B_29 used at Hiroshima to drop the bomb,Enola gay +Sports & Leisure,Which Player Lost Successive World Snooker Finals From 1990 To 1994? ,Jimmy White ,General,What battle resulted in the largest number of german pow's,Battle of,General,At Prince Charles's wedding who was the best man,Nobody – brothers were supporters +General,What city provided the setting for One day at a time?,Indianapolis,General,Geotropism affects what,Plants its gravity growth response,Art & Literature,"According To ""The Hitchhikers Guide To The Galaxy"" what number is the answer to everything?",Forty Two + Geography,Kathmandu is the capital of ______?,Nepal,General,"What disease is also known as ""rubella""",German measles, History & Holidays,Neil Armstrong And Buzz Aldrin Landed On The Moon But Who Stayed Behind In The Command Capsule? ,Michael Collins  +Entertainment,Who did Pat Sajak play on the soapie 'Days Of Our Lives'?,Kevin Hathaway, History & Holidays,"Which of these is NOT one of Santa's reindeer? (Donner, Dixen, Comet, Dasher) ",Dixen ,General,In 1943 Canadian Army troops arrive in,North africa +Sports & Leisure,Baseball: The Florida ______?,Marlins,General,Whose magazine is called The Watchtower,Jehovah Witnesses,General,What was the name of the principal on Saved By the Bell?,Mr. Richard Belding +Entertainment,Film Title: An Officer and a(n) _________.,Gentleman,General,As what is New Jersey also known,Garden state,General,"New Year's Eve, or December 31st, is dedicated to which saint",St sylvester +General,Which English King rode a horse called White Surrey,Richard III,General,What do the letters F.D. on British coins mean ?,Defender of the Faith,General,What trees live in wet salty swamps,Mangrove +General,What is the Capital of: San Marino,San marino,Geography,The longest river in Western Europe is _________,Rhine,General,Who was Michelle's first boyfriend on Full House?,Howie +General,Where would you find your purlicue,Space between thumb and finger,General,Who played harry lime in the film 'the third man',Orson welles,General,Georgius Panayiotou became famous under what name (both),George Michael +General,Who refused the Nobel Literature prize in 1958,Boris Pasternak,General,In what does the fda allow an average of 30 insect fragments and 1 rodent hair per 100 grams,Peanut butter,General,Chernoble is in which Russian province,Ukraine +General,What is British Columbias capital,Victoria,General,What is the fear of slime known as,Myxophobia,General,What is a group of cockroaches,Intrusion +General,Who founded the McDonalds Chain?,Ray Kroc,General,Jason Robards won the Oscar for Best Supporting Actor in 1976 and in 1977. Name either of the films. all the presidents men,Julia,General,The dot over the letter 'i' is called what,Tittle +Music,"Who Wrote Chaka Khans 1984 Hit ""I Feel For You""",Prince,General,In Jones Chapel Alabama illegal guy take gal where till 4th date,Horseback Riding,Sports & Leisure,In A 1990 Test Match Against India Graham Gooch Scored A Treble Nelson. How Many Runs Did He Score? ,333  +General,Basketball: the los angeles ______,Lakers,General,Who authored 'ivanhoe',Sir walter scott,Sports & Leisure,Which Steeplechase Did Party Politics Win In Election Year 1992? ,Grand National  +General,Who created the cartoon character Droopy,Tex Avery,General,What tropical root is used to flavor soft drinks,Ginger, History & Holidays,What was the name of Spandau's only prisoner ,Leslie Joseph  +General,What is a group of mallards in flight,Sord,Geography,What is the capital of Monaco,Monaco,General,In what sport would you see a Chistera,Pelota +General,Which magazine declared bankruptcy in the early 1990s,Success,Music,Which Artists Was Backed By The Attractions,Elvis Costello, History & Holidays,"Which popular Christmas carol was originally a piece written by King Henry VIII? A= God Rest Ye Merry Gentlemen, B= We 3 Kings Of Orient Are, C= What Child Is This, D=Good King Wenceslas ",C= What Child Is This  +General,"In Greek mythology, who were jason's companions",Argonauts,General,A spunder or drift is the name for a group of what animals,Swine,General,Jamie Farr played what role in MASH,Corporal Clinger +General,"Who wrote ""the maltese falcon""",Dashiell hammett,General,Who had a hit with Devil Woman,Cliff Richard,General,Which group sang the theme tune to the James Bond film The Living Daylights,Aha +General,What is the only USA state without a natural lake,West Virginia,General,The term Quartocentennial represents how many years ?,25,General,Name the moody blues first lp,Lose your money +Religion & Mythology,"According to the Bible, how many years did Methuselah live?",969,General,"Which place, now an airport, once staged the Grand National",Gatwick,General,Whats the worlds largest sea,Mediterranean +General,What is the most popular decoration on top of a toilet tank,Scented seashells,General,California Dolls Is A Film About Which sport,Wrestling,General,Who was the original presenter of 'Points of View',Robert robinson +General,In golf what is the maximum number of clubs allowed in your bag,Fourteen,General,What was the first english play written exclusively for children,Peter pan,Music,Which Famous Soul Artist Was Once The Drummer Of Harold Melvin And The Blue Notes,Teddy Pendergrass +General,What was the first creature put on the endangered species list,Peregrine Falcon,General,Where were bagpipes invented,Iran – then Persia, Geography,Kingston is the capital of which country?,Jamaica +General,How many feet in a fathom,Six,Music,Dee Snider Was The Singer For Which Band,Twisted Sister,General,"Wyatt Earp, Frank James, Abraham Lincoln what actor links",Henry Fonda +General,What Turkish city has spread to both sides of the Bosporus Strait,Istanbul,General,"Berry Gordy The Founder Of Motown, Had A One Hot Wonder Son Who Was He",Rockwell,General,And what was that movie,Conan Doyle's – The Lost World + History & Holidays,"Who said: ""Let them eat cake.""",Marie antoinette,General,What is the full name of the creator of Peter Pan,James Mathew Barrie,General,What Was Jason Donovan's Last UK No.1 Hit Single Of the 1980's,Sealed With A Kiss +General,The tenth sign of the Zodiac,Capricorn,General,The GRA govern which sport,Greyhound Racing Association,Geography,In which English county is Yeovil? ,Somerset  + History & Holidays,"""Who banned Christmas Carol's in England between the years of 1649 and 1660? """"Oliver Cromwell"""", """"King Charles II"""", """"Queen Victoria"""", """"Queen Elizabeth I"""" "" ",Oliver Cromwell ,Entertainment,Who was Fred Flinstone's best friend?,Barney Rubble,Music,What Colour Onions Were The Subject Of A Booker T And The Mg's Song,Green +Food & Drink,"Popularised in the USA, Australia and New Zealand, which thick drink is made with fresh fruit pur?ed with milk, yoghurt, or ice cream? ",Smoothie ,General,Who directed Sharky's Machine,Burt Reynolds,General,What part of an aircraft is the empennage,Tail Unit +General,Which vegetable got its name from a precious stone,Onion - Latin unio large pearl, History & Holidays,He allowed the bugging of the Democratic Committee headquarters.,Richard Nixon,General,Who is the roman counterpart of heracles,Hercules +Food & Drink,What part of a wine bottle is the punt? ,The indentation in the base ,Entertainment,Who is Robert van Winkle?,Vanilla Ice,Geography,In which southern US state is Dodge City? ,Kansas  +General,What is the square root of 16,Four,General,What is a Tambura,An Indian long necked lute,General,Who could distinguish 140 forms of tobacco ash,Sherlock holmes +Sports & Leisure,At Which Sport Might You See A Crucifix ,Gynmastics (The Rings) ,General,"What plant maybe 'black', 'green' or 'deadly'",Nightshade,General,What is a catalogue of languages called?,Ethnologue +Science & Nature,Which Acronym Is Used For The Long Range Radar Surveillance & Control Centre For Air Defence Developed Originally In The USA ,AWACS ,Music,"Suffering From ""Teenage Depression"" Who Would Join Eddie If He Quit His Town",The Hotrods, History & Holidays,Who Directed Terence Stamp In His First Starring Role In The Film Billy Budd In 1962 ,Peter Ustinov  +General,"Formerly with spencer davis, he went on the form traffic with dave mason",Steve winwood,Entertainment,What song did Elton John and George Michael sing as a duet?,Don't Let The Sun Go Down On Me,General,John Lennon named The Quarrymen after what,His old school +People & Places,Who Did Princess Anne marry In 1992 ? ,Tim lawrence ,Music,"Who Recorded The Albums ""Planet Waves"", ""Self Portrait"" & ""Shot Of Love""",Bob Dylan,General,In 1921 Turkey makes peace with,Armenia +General,What is Homer Simpsons greatest fear,Sock Puppets,General,Who was King Solomon's mother,Bathsheba,General,Syphilophobia is a fear of ______,Syphilis +General,Which European city is regarded as the clock making capital of the world,Geneva,General,What is the most widely cultivated plant,Wheat,Art & Literature,What Shakespearean play features the line: A plague on both your houses?,Romeo and Juliet +General,The Ideal toy company was the first to mass produce what item,Teddy Bears,Entertainment,Who was the black assistant of Mandrake the Magician?,Lothar, History & Holidays,"""In the song """"The Twelve Days Of Christmas"""", what did my true love give to me on the 12th day?"" ",12 Drummers Drumming  +General,"The first book of __________ was introduced on april 10, 1924",Crosswords, History & Holidays,Which 60's Movie features The Line by right she should be taken out and hung for the cold blooded murder of the English tongue ,My Fair Lady ,General,Who coached The Bad News Bears,Morris buttermaker + History & Holidays,In Medievil England What Name Was Given To The Area Presided Over By A Lord ,Manor ,General,"In Greek mythology, what did ariadne help theseus to escape",Labyrinth,General,In Salem Oregon its illegal for women to do what,Wrestle +General,Where is crystal palace,London,General,Tarquin the Proud was the last king of where,Rome,General,What is the term used to describe a container-grown plant whose roots have filled the container,Pot bound +General,"If you were going to a chiropractor for treatment, what would be affected",Backbone,General,Yoga (the meditation) is a Sanskrit word meaning what,Union,General,Where was nelson mandela in prison,Robben island +General,In music who decided that an octave should have eight notes,Pythagoras,Science & Nature,Which Water Bird Has Brown & Black Feathers With A White Flank Slash & A Red Beak Shield ,Moorhen ,General,Which drug is used in medicine to dilate the pupils of the eyes,Atropine +General,In which country would you find the Asiatic lion living in the wild,India,Music,"Name The Probable First And Last Single From 1,300 Drums Feat The Unjustified Ancients Of Mu",OOh AAh Cantona,General,What is the flower that stands for: reserve,Maple +Art & Literature,What Was Shakespear's Last Completed Play ,The Tempest ,General,The minimum wage in 1938 was how many cents per hour,25,General,"In ancient Rome what could be candida, picta, pulla or virilis",Types of Toga styles +General,Which authors books are most borrowed from libraries,Catherine Cookson,General,Who wrote the novel 'Orlando',Virginia woolf,General,Which diminutive Hollywood villain was born Laszlo Loewenstein in 1904,Peter lorre +General,What is the fear of learning known as,Sophophobia,General,In Italian translation who is Mr Kiss Kiss Bang Bang,James Bond,General,"Who said ""All I can say is that I'm not a Marxist""",Karl Marx +General,What's the name of the extinct volcano near hawaii's waikiki beach,Diamond,General,What's another name for a mountain lion,Puma,General,Who From The World Of Music Died On Sep 18th 1970 In London England,Jimmy Hendrix +General,Film title ' ______ leagues under the sea',20000,Technology & Video Games,Who is the third opponent in 'Super Smash Brothers'? ,Fox McCloud,General,Who was the first to use rubber gloves during surgery,Dr w.s halstead +Music,The Song Evergreen Was A Big Hit For Barbara Streisand Did She Write The Words Or The Music,Music,General,"Which American president said, 'It is not best to swap horses when crossing streams'",Abraham lincoln,General,Where was it once against the law to slam your car door,Switzerland + History & Holidays,"According to the bible, who were the baby Jesus's first visitors ",The Shepherds ,General,What does a phlebotomist do,Draws blood samples,Sports & Leisure,Which title has been won by the rider who wears the polka dot jersey in the Tour De France? ,King Of The Mountains  +General,"A part of a church or a separate building, often octagonal or round, in which baptisms take place.",Baptistery,General,In which Latin American country is the Quetzal the main unit of currency,Guatemala,Art & Literature,Which Shakespearean Play Is Set In The Forest Of Arden ,As You Like It  +General,What is or was a Portuguese moidore,A Gold Coin,General,Ferdinand and Imelda Marcos were exiled in what year,1986,General,Velveteen is made from a mixture of Silk and which other fibre,Cotton +Science & Nature,Which bird became extinct in 1861 ?,Dodo,General,Transposition of the letters of a word or phrase to form a new word or phrase.,Anagram,General,What animal originated Groundhog Day,Badgers - in Germany +General,In which country do the Ashanti people live in the Province of Ashanti,Ghana,Music,Jon Moss Was The Drummer With Which 80's Band,Culture Club,General,1579 the Netherlands achieved independence from what country,Spain +General,In the 70s The Bahamas gained independence from who,Great Britain,General,What kind of chair sits on curved runners,Rocking chair,Music,"In ""Ob-la-di, Ob-la-da"", what kind of ring did Desmond gave to Molly?",20 carats +General,U.S. captials Nebraska,Lincoln,Entertainment,"Who was C3PO's sidekick in ""Star Wars""?",R2D2,General,"Which actor played in all of these films Shadowlands, Nixon and The Mask of Zorro",Anthony hopkins +General,On Tv How Is Alistair Graham Better Known?,Ali G,General,In which city is the Alhambra Palace,Granada,General,What is 240 minutes in hours,Four +General,This Is Bon Jovi's latest album?,Crush,General,"What travels faster than light a laser beam, sound or nothing",Nothing,Music,For Their Debut Single What Did The O'Jays Warn Us About In 1972,Back Stabbers +General,What was keanu reeves' first big film,Point break,General,British politician John Montigue is credited with inventing what,4th Earl of Sandwich,General,What is the name of the Australian Film Institute Award,Longford – (Raymond) +Geography,In what mountain range is Kicking Horse Pass,Rockies,General,Approximately how many years old is the first known written advertisement?,Three thousand,General,Which country singer made an appearance in the film True Grit,Glen campbell +Music,"The Single ""Respect Yourself"" Was Released By Which Die Hard Music Lover",Bruce Willis,General,Maurice Micklewhite became famous as who,Michael Caine,Music,Live You're Life Be Free Was A 90's Hit For Who,Belinda Carlisle +General,Name captain smollett's ship in treasure island,Hispaniola,General,What plant's bulb can make you cry,Onion,General,What appears when the sun activates melanocytes,Freckles +Music,"Who Had A Hit With ""Sledgehammer"" & ""Big Time""",Peter Gabriel,Sports & Leisure,Which weight division in boxing lies between flyweight and featherweight?,Batamweight,General,"A cylindrical vertical support usually consisting of a base, shaft and capital.",Column +General,With which creature was the Egyptian God Horus identified,Falcon,General,Which 2 countries will host the 2002 Soccer World Cup finals,Japan - South Korea, Geography,What is the deepest land gorge in the world?,Grand Canyon +Food & Drink,French fries come from this country.,Belgium,General,Who directed the film Silence of the Lambs,Jonathan demme,Music,What Was Singer Chris Hamill's Stage Name,Limahl +Art & Literature,Which Famous Sculptor Was Refused Entry ToThe French Academy 3 Times ,August Rodin ,General,Who was the Roman god of agriculture,Saturn,Geography,Brussels is the capital of which country,Belgium +General,The art of tracing designs and taking impressions of them is ___________.,Lithography,General,French: done and past arguing about,Fait accompli,Science & Nature," The average minimal speed of birds in order to remain aloft in flight is reported to be about 161/2 feet per second, or about __________ miles per hour.",11 +General,What did AJ stand for in Simon & Simon's AJ Simon?,Andrew Jackson,Geography,In which state is Appomattax,Virginia, History & Holidays,Which black athelete's successes at the Berlin Olympics caused Hitler to storm out of the stadium? ,Jesse Owens  +General,"Name beginning with ""A"" meaning "" moon shine",Aysel,General,In what Hitchcock film does he NOT appear,Lifeboat,General,Indiana jones: what state was indy raised in,Utah +General,Thumper was a rabbit from which film,Bambi,General,What is a group of this animal called: Chicks,Clutch chattering,General,Ha'aretz is a newspaper in which country,Israel +General,What emperor ordered St Peter crucified,Nero,General,What figure in greek mythology gave fire to man,Prometheus,Geography,What is the capital of Austria,Vienna +Music,To Which City Move It's Headquarters In 1971,Los Angeles,Music,"Which Band Performed ""Oh Yeah"" From The ""Ferris Buellers Day Off"" Soundtrack",Yello,Geography,In what island group is corregidor ,Philippine  +General,Hillary What bird lays an egg that is roughly a quarter of its body weight,Kiwi,General,What does the medical abbreviation LD stand for,Lethal dose,General,When was Abraham Lincoln elected,1860 +Sports & Leisure,What Type Of Bowler Might Use A Chinaman ,A Legspin Bowler ,General,Name Jennifer Anniston's Godfather,Telly Savalas,Art & Literature,Which US clarinetist players real name was Arthur Jacob Shaw? ,Artie Shaw  +General,Barry Allen was the alter ego of which DC comic superhero,The Flash,Music,Under What Name Did Yaron Cohen Win The Eurovision Song Contest In 1998,Dana International,General,Who Broke Jackie Stewarts Record Of 27 Formula One Wins,Alain Prost +General,What is a group of this animal called: Deer,Herd,General,Which former footballer is the Brazilian Sports Minister,Pele,General,What is the name of Wordsworth's cottage in Grasmere,Dove cottage +General,What is the flower that stands for: departure,Sweet pea,General,Who was the second president of the U.S.,John adams,General,Who founded the Ballet Russe,Sergei Pavlovich Diaghilev +Science & Nature,Where Is The World's Largest Atomic Establishment ,Underground At CERN Between France & Switzerland ,General,What did Dr Godfrey invent in 1762,Fire extinguisher,Sports & Leisure,"In Billiards, How Many Points Are Awarded For Potting The Red ",Three  +Sports & Leisure,Name The Rugby Union Trophy For Which England & Scotland Compete ,Calcutta Cup ,General,At whose court was Merlin the wizard,King Arthur Arthur,General,Where was the 1st u.s federal penitentiary,"Leavenworth, kansas" +Science & Nature,What Animal Is A Chester White? ,A Pig ,Music,"In the World Of Music Which Band is made up with the members Jim, Andrea, Caroline and Sharon",The Corrs,General,Dinotopia's illustrator,James gurney +General,What does sctv stand for,Second city television,General,Which actor was born Maurice Micklewhite,Michael caine,Music,"Who Had A 1972 Hit With ""Ooh Wakka Doo Wakka Day""?",Gilbert O'Sullivan + Geography,What is the basic unit of currency for Philippines ?,Peso,General,What is the minimum number of degrees in a reflex angle,180,General,Which British stage and film director won an Oscar in 2000,Sam mendes +General,According to 36% of Americans they have done what,Spoken with God,General,In Maryland its illegal to frighten who or what,A Pigeon,General,The eohippus was an early form of which animal,Horse +Entertainment,What film marked James Cagney's return to the screen after 20 years?,Ragtime,General,Katagelophobia is the fear of,Ridicule,Music,"""Going Nowhere"" Was A Hit In 1993, Who Sang It",Gabrielle + History & Holidays,Mark David Chapman was famous for what in 1980? ,Shooting John Lennon , History & Holidays,Name the author of Frankenstein ,Mary Shelley ,Music,How Did Several Spinal Tap Drummers Perish Prior To The Arrival Of Mick Shrimpton,All Spontaneously Combusted +Science & Nature,What Do The Initals A.I.D.S. Stand For ,Acquired Immune Deficiency Syndrome ,General,Blanchard and Jeffries crossed the English Channel in 1785 using what means of transport,Hydrogen balloon,General,Which Italian tractor maker tried making cars in 1960s,Ferruchio Lamborghini +Science & Nature, It takes an average of 345 squirts to yield a gallon of milk from a cow's __________,Udder,General,What is the flower that stands for: amiability,Jasmine,General,What's the capital of Malaysia,Kuala lumpur +General,We have used the Latin phrase ad hoc - what literally mean,For this special purpose,General,What is the fear of hospitals known as,Nosocomephobia,General,In which 1970's U.S. TV show would you meet the characters 'Radar' and 'Hot Lips',Mash +General,The Pindus is the main mountain range in what country,Greece,General,What is the only bird that can see the color blue,The owl,General,John McClane was fighting terrorists in a skyrise in which eighties movie?,Die Hard +General,What city is the queen of the pacific,San francisco,General,Who won each of golfs four grand slam tournaments at least three times,Jack nicklaus,General,What name is given to the process by which rocks break when they absorb water,Hydration + History & Holidays,This Spaniard conquered Mexico.,Hernando Cortez,General,"The three number systems commonly used in computers are binary, decimal and _________.",Hexadecimal,General,For which film did Humphrey Bogart win an Oscar in 1951,The african queen +General,"Flat, round, brown spots on the skin that contain an excess of melanin, the human skin pigment",Freckles,General,In Lang Kansas illegal ride a donkey in public unless it has what,A Straw hat on,General,How many legs does a crab have,Ten +General,What does the word khaki mean ?,Dusty,General,Which books chief rival is the Encyclopaedia Galactica,Hitchhikers Guide to the Galaxy,General,What does a cadastral map show,Large scale individual properties +General,What is the stage name of roberta anderson,Joni mitchell,General,"What American general declared ""I shall return""",Douglas macarthur,General,What sport considers it a foul to hold an opponent's head under water,Water polo +General,Karl Landsteiner Won The Nobel Prize For Medicine In 1930 For His Discovery Of What,Blood Groups,General,"On a suit of armour, the couter would protect which part of the body?",Elbow,General,What is the flower that stands for: estranged love,Lotus flower +General,What is the young of this animal called: Goat,Kid,General,During menstruation the sensitivity of woman's what is reduced,Middle Finger,Food & Drink,Webb's Wonderful and Winter Density are varieties of which vegetable ,Lettuce  +General,"In 1962 - cost 20,000 - size of a small suitcase - what",Portable computer,General,In golf what is the penalty for playing with your opponents ball,Two strokes added on,Music,On which label did all Sylvester's 1970s hits appear?,Fantasy +Sports & Leisure,Who did Sue Barker replace as host of the BBC quiz show (A Question Of Sport)? ,David Coleman , History & Holidays,From Which Country Did Saint Nicholas Originate ,Turkey ,General,What are the two main gases that make up our breathable air,Oxygen and nitrogen +Science & Nature,The process of water changing to water vapor is known as _______.,Evaporation,Science & Nature,What Chemical In Fireworks Gives A Yellow Flame ,Sodium ,General,80% of the worlds population wears shoes made in what country,China +Music,Name The Company Originally Started By (And Later Sold) By Richard Branson,Virgin,General,Why are we playing this game,Because we are bored,General,What kind of rock is marble,Metamorphic +General,Name the director of the film 'American Beauty',Sam mendes,General,Which footbal club won the European Cup the first 5 years in which it was held,Real madrid,General,What kind of nuts are ground up to make marzipan,Almonds +General,Jimmy Connors won the men's doubles at Wimbledon in the 1970s with which partner,Ilie nastase,Science & Nature,In which state is the Houston Space Centre?,Texas,General,Film title 'an officer and a ______',Gentleman +Food & Drink,Poteen is a distilled spirit made in the West of Ireland. What is it made from? ,Potatoes ,General,In Salt lake city it is illegal to carry an unwrapped what in street,Ukulele, Geography,What is the capital of Democratic Republic of the Congo ?,Kinshasa +General,"Ancient art practiced especially in the middle ages, devoted chiefly to discovering a substance that would transmute the more common metals into gold or silver & to finding a means of indefinitely prolonging human life",Alchemy,General,"The One Time Standard Computer Language ""ADA"" Was Named After The Daughter Of Which Famous Poet",Lord Byron,General,Who played the title role in the 1970's U.S. cop series Shaft,Richard roundtree +General,Who is donald duck's girlfriend,Daisy duck,General,Who said China is a big country inhabited by many Chinese,Charles de Gaulle,General,Which American city used to be called Yerba Buena,San Francisco +Science & Nature,Which Side Of The Brain Contributes To Rational Thought ,Left Side ,General,"Which Group sang the song ""November Rain""?",Guns n Roses,General,"Who said ""I’ve sometimes thought of marriage - then re-thought""",Noel Coward +Art & Literature,Which Stephen King Novel Is Set At The Overlook Hotel? ,The Shining ,General,What actor did time on a Georgia Chain Gang - Escaped 6 days,Robert Mitchum – Vagrancy,Music,What John Lennon/David Bowie single went to #1 in 1975?,Fame +Music,Amazing Grace Entered The Charts Eight Times In This Decade Sung By Whom,Judy Collins,General,Which magical city is located in the Valley of the Blue Moon,Shangri-La James Hilton Lost Horizon,General,"Isms: Public ownership of the basic means of production, distribution, and exchange",Socialism +General,"For which film did Anthony Quinn win an Oscar for Best Suporting Actor, in 1956",Lust for life,General,Where were the original loopholes,Castle walls – arrow firing slits,Music,According To The Nickelback Song Rockstar Exactly Where Do They Want Their Star On Hollywood Boulevard,Somewhere Between Cher & James Dean +General,What is the largest body of fresh water in the world,Lake superior,General,What wwii leader dallied with clara petacci,Benito mussolini,Sports & Leisure,Which American Football Team Won This Years SuperBowl (2006) ,New England Patriots  +General,Tsaritsyn in Russia used to be known as what,Volgagrad Stalingrad,Mathematics,A triangle with two equal sides is called __________.,Isosceles,Geography,Name the capital of argentina. ,Buenos aires  +General,What country has the largest armed force?,China,Science & Nature," __________ were domesticated around 4,000 years ago.",Camels, History & Holidays,*Arnold Schwarzenegger made his film debut in 1974 In What Movie * ,*Pumping Iron.*  +General,How many sayings did jesus say from the cross,Seven,General,What did john logie baird invent in 1925?,Television,Music,Which Beatles Classic Did Candy Flip Take Their Version Of To No.3,Strawberry Fields Forver +General,"At the 1991 World championships in Tokyo, which British hurdler ran the last leg of the 4x400 relay to win gold for Britain",Kris akabusi,General,"According to the saying, what speak louder than words",Actions,General,Which brass instrument has a slide,Trombone +General,In what form are the signals that a normal TV aerial receives,Analogue,General,Hockey the toronto ______,Maple leafs,Art & Literature,A representation of a human or an animal form. ,Figure +General,Aaron died on mount _____,Hor,General,What country is home to the Dresdner Bank,Germany,General,Which author created Fu Manchu,Sax Rohmer +General,"Assam, darjeeling and oolong are all types of what",Tea,General,What is the flower that stands for: energy in adversity,Camomile,Science & Nature,What is the chemical symbol for radon?,Rn +General,We've heard phrase I don’t give a toss - but Tos Greek for what,Bear, History & Holidays,"In film, the very secretative Alex Leamas came from where? ",In from the cold. The Spy Who Came In From The Cold . ,General,Barophobia is fear of,Gravity +General,With what body part is otology involved,Ear,Music,"In Which Disney Movie Were There Characters Called ""John, Paul, George & Ringo""",The Jungle Book, History & Holidays,Who is known as the high priest of revenge?,Philip Seldon +General,Which Country Owns Easter Island,Chile,General,Which famous financier is nicknamed 'The Sage of Omaha'?,Warren Buffet,General,Nikioli Poliakoff became famous under what name,Coco the clown +Geography,This Pacific island's puzzling monoliths attract ethnologists.,Easter island, History & Holidays,"Who graced the cover of the 1st 'Playboy' in 1953? A=Sophia Loren, B=Marilyn Monroe, C= Lauren Baccall ",B = Marilyn Monroe ,General,The capitol of Nigeria was Lagos what is it now,Abouga +General,What is the fear of mother-in-law known as,Pentheraphobia,General,In Scotland its illegal to be drunk in possession of what,A Cow,General,Jenny Von Westphalen was married to who,Karl Marx +Music,Which Was The Main Instrument Used By Vangelis On His Film Score Chariots Of Fire,Synthesizer,General,Luchiano Paverotti has what in his pocket for luck when singing,Bent Nail, Geography,"Wracked by heavy monsoon rains 3 to 4 months of the year, this is the wettest and most flood-prone nation in Asia.",Bangladesh +General,What was invented in the 1800s and sold as a diarrhoea cure,Tomato Ketchup,General,January in the USA is National what month,Soup,Music,Who Was The Manager Of The Sex Pistols,Malcolm McLaren +General,Apart From David Beckham Who Is The Only English Footballer To Be Sent Off During The World Cup Finals,Ray Wilkins,General,"In the movie ""Mall Rats"", What famous author was signing comic books",Stan,General,What is the world tallest horse,Shire Horse +Geography,By What Name Do Argentina Refer To The Falkland Islands ,The Malvinas ,General,Where would you find a corbicula,Honey bee – sacks carry pollen,Music,What Did Geri Write By Her Name On The Cover Notes For The Album “Spice”,Girl Power +General,Australian Clement Wragge instituted what,Naming Hurricanes, Geography,Mexico City is the capital of ______?,Mexico,General,"The Creature With The Largest Penis On Earth Is The Blue Whale , What Is The Largest Penis Ever Recorded Of This Species (Feet And Inches)",12 Foot 11 Inches +Food & Drink,What is made at St James's Gate in Dublin? ,Guiness ,Science & Nature,What is the name of the bone in the lower leg?,Tibia,Science & Nature, Atlantic __________ are able to leap 15 feet high.,Salmon +General,What were the nicknames of the dean brothers who pitched for the st louis cardinals,Daffy and dizzy,General,"What name is given to the traditional custom in Japan of viewing and enjoying flowers, especially Cherry Blossom?",Hanami,General,"Whose last words were ""lets do it""",Garry Gilmore +General,What U.S. state includes the telephone area code 503,Oregon,Music,"Who did Earth, Wind and Fire link with for ""Boogie Wonderland""?",The Emotions,Sports & Leisure,Who was the first bowler to take over 300 wickets in test cricket? ,Fred Truman  +General,Whisky and Drambuie mix to form what sickly cocktail,Rusty Nail,General,What beach provided the most resistance to Allied forces on D Day,Omaha,General,Which date is inscribed on the book held by the Statue Of Liberty?,July 4 1776 +General,From what language is the word 'mummy' derived,Persian,General,What Was The First Cd To Be Released In The USA,Born In The USA,General,In 1924 the worlds first book of what was published,Crosswords +General,What us state has sagebrush as its state flower,Nevada,General,"Who famosuly quoted ""Cogito, Ergo Sum""?",Rene Descartes,General,Who or what would be looked after in a creche,Children +General,In Winnipeg there is a statue to which bear,Winnie the Pooh,General,Where is westminster abbey,London,General,What dance was developed from the rumba and african dances,Conga +General,Pooley's Guide is used by amateurs and professionals in which profession and hobby,Flying,General,A mule is sired on a mare by an ass. What is the offspring of a stallion and a female ass,Hinny, Geography,What is the largest exclusively Indonesian island?,Sumatra +General,"What food links Dundee, Chorley and Eccles",Cake,General,What creature is nicknamed the old man of the sea,Sea Otter - it's white whiskers,General,In what country are most baseballs made,Haiti +General,What are the two cities in charles dickens' 1859 novel a tale of two cities?,London & paris,Music,"""I'll Be Missing You"" Was A Hit For Who In 1999",Puff Daddy,General,In Winston-Salem N Carolina its illegal under 7 year olds do what,Go to College +General,Film based PK Dick story We can remember it for you wholesale,Total Recall,General,Syngenesophobia is a fear of ______,Relatives,General,What writing implement was invented by John T. Loud in 1888,It wasn't until 1938 that a hungarian made a successful cheap working version ball point pen +Sports & Leisure,How many seams are there on a football (American),Four,General,In what French district do most of the best clarets come from,Medoc,General,Who painted 'irises',Vincent van gogh + History & Holidays,What Christmas Item Was Invented By Liondon Bakery And wedding Cake Specialist 'Tom Smith' In 1847 ,Christmas Crackers ,General,Crossair originated in what European country,Switzerland,General,Which 80s song is still the most requested at US weddings,Endless love +General,"On Airwolf, what instrument does Hawke play",Cello,Music,"Who Did ""Phil Oakley"" Team Up With For The Hit ""Together In Electric Dreams""",Giorgio Moroder,General,Whose film debut was Jennings in Revenge of the Creature 1955,Clint Eastwood +General,In 1926 Japan deleted 800000 feet from US films showing what,Kissing - it was unclean,General,Baseball: The Boston Red______,Sox,Food & Drink,A cluster or bunch of bananas is called a ?,Hand +Art & Literature,"An etching tecnique in which a solution of asphalt or resin is used on the plate. It produces prints with rich, gray tones. ",Aquatint,Music,"Who Recorded The 70's Disco Classic “You're My First, My Last, My Everything”?",Barry White,General,"Following earlier failures on TV, Danny Thomas successfully returns in",Make +General,Meaning person who overcomes name the ancient Indian religion,Jainism,General,"Pooler Jones, Lazy Plate, Jayne Hill and Buckthorn types of what",Barbed Wire,General,Which type of wheat yields flour used to make best quality spaghetti,Durum +General,In what country was the paperclip invented,Norway,General,If you were using Highroller Snooker 7 Lukki what are you doing,Fishing artificial flies, Geography,What is the capital of Washington state?,Olympia +General,Who starred in the film Willie Wonka and the Chocolate Factory,Gene wilder,General,In the Philippines what is lumpia,Spring Rolls,General,Which nursery rhyme is supposed to be based in the 'Black Death',Ring o ring o roses +General,Who created the children's land of Narnia and Lion Witch Wardrobe,Clive Staples Lewis,General,Historical usually Spanish warship,Galleon,General,The west what song did elton john and george michael sing as a duet,Don't let the sun +Religion & Mythology,The sea gods had a three_pronged spear called a(n) ________.,Trident,General,"Greek philosopher, who profoundly affected Western philosophy through his influence on Plato",Socrates,Music,"Joan O Neil Who Sings With The Tina Turner Tribute Band ""River Deep"" Is Also The Mother Of Which Very Famous Pop Star",Melanie Sport Spice Chisholm +Music,"Which 3 Family Members Released The Album ""Priority""",The Pointer Sisters,Music,Name Black Sabbath's Debut Hit,Paranoid,General,What does the girls name Linda mean,Serpent – meaning wisdom +General,An Arizona prostitutes organisation is called TWATS meaning,Tucson Whores and Tricks, History & Holidays,What year did British Honduras become Belize? ,1973 ,General,How many throwing events are there in a decathlon,Three +Food & Drink,What type of milk is a basic ingredient of Thai cookery? ,Coconut milk ,Science & Nature, A snail speeding along at three inches per minute would need 15 days to travel __________,One mile,General,In what unit is electrical current measured,Amperes +General,What actress played Rhoda,Valerie harper,General,Which part of his body did Charlie Chaplin insure,Feet,General,If sand is melted with limestone and sodium carbonate what is formed,Glass + History & Holidays,Who discovered Christmas Island on Christmas Eve 1777 ,Captain Cook ,General,The constellation Lacerta has what English name,Lizard,General,What was the first version of microsoft windows,Windows 286 +Geography,In which city is Red Square,Moscow,General,Michael di lorenzo was one of the lead dancers on which michael jackson video,Beat it,General,"Which Fruit Is Affected By A Grey Type Of Fungus Known Commonly As ""Noble Rot""",Grapes +General,What is the name given to part of the large intestine,Colon, History & Holidays,"After the fall of the manchu dynasty, there existed three political parties in china: The KMT, Nationalists and The ______ ?",Communists,General,Zorro the heroes name means what in Spanish,The Fox +General,Chopin played what instrument as a child,Piano,General,What does a notaphile collect?,Banknotes,Science & Nature,Which Bird Is The Emblem Of The United States ,Bald Eagle  +General,When was the maiden voyage of the 'Titanic',1912,Entertainment,What is the name of Duddley Do_Right's horse,Horse,Art & Literature,"In 'Romeo and Juliet', who gave a long monologue about Queen Mab?",Mercutio +General,What three European countries begin with the letter 'A' (alphabetically)?,"Albania, Andorra and Austria",General,The Hound of the Baskervilles was published in which year,1903,General,This is the fear of enclosed spaces.,Claustrophobia +General,What is removed with an orchidectomy,Testicle, History & Holidays,What famous soap opera duo reigned on General Hospital in the eighties? ,Luke and Laura ,Sports & Leisure,Which Sport Has The Most Umpires Per Player ,Tennis (15-1)  +General,What is the flower that stands for: benevolence,Potato flower,General,"Who first said ""Publish and be Damned""",Wellington re Harriot Wilson mistress,General,Spielberg named the shark in Jaws Bruce why,After his Lawyer +General,The study of human behaviour is __________.,Psychology,General,Who is minnie mouse's boyfriend,Mickey mouse,General,How Many Champagne Bottles Is A Jeroboam?,4 Bottles +People & Places,Which singer wrote the autobiography 'Take It Like A Man'? ,Boy George ,General,What links Pauldron Crisse Gorget and Tassle,Knights Armour, History & Holidays,How many gifts are given in total in the song The Twelve Days of Christmas? ,364  +Geography,In what city is the smithsonian institute ,Washington ,General,"Who wrote""poems are made by fools like me but only god can make a tree""",Joyce kilmer,General,Who won the world soccer championship in 1954,West germany +General,Pooh loves honey but which creature loves watercress,Roo,Sports & Leisure,What trophy is awarded to the winner of the NHL play-offs?,Stanley Cup,Food & Drink,What is Armagnac? ,"A dry, brown, brandy " +General,Who starred in the 1952 film 'niagara',Marilyn monroe,General,May 21st 1881 Clara Barton founded what,American Red Cross,General,What male name comes from Greek meaning defender of men,Alexander +General,What is a group of lapwings,Deceit,Science & Nature,Which American Car Of The 1940's Had A 3rd Central Headlight That Swivelled With The Front Wheels ,Tucker Torpedo ,General,Where is your Puricle,Space thumb extended forefinger +Music,Whose 1993 Debut Single Was Bombtrack,Rage Against The Machine,General,England Periodic Table: what is Rn,Radon,General,The rock band Metallica were famously involved in a legal wrangle with which online music sharing site?,Napster + Geography,What is the capital of Equatorial Guinea ?,Malabo,General,Ideophobia is a fear of ______,Ideas,General,Who invaded England in 55 BC?,Romans +General,"Which territory of North Africa, situated on the southern side of the Strait of Gibraltar, belongs to Spain",Ceuta,General,Who was the first black actress to win an oscar,Hattie macdaniel,General,"During the 1812 war between canada and the u.s, what did canadian troops invade and burn",White house +General,Oasis Decided To Split In August 2009 But What Song Was The Bands Very First UK Number One Single,Some Might Say,General,Its branches are Soto Renzai members ponder Koans what is it,Zen Buddhism,Geography,"Yuma, ____________ has the most sun of any locale in the U.S. _ it averages sunny skies 332 days a year.",Arizona +General,Which U.S. states way of life and culture is described as Cajun,Louisiana,General,Who in mythology was condemned to sit under a sword suspended by a hair,Damocles,Geography,In which city is the Mathematical Bridge? ,Oxford  +Science & Nature,What is an Ishihara Test used for? ,Check Colour Blindness ,Music,"Give Me Love Was The Plea From Which ""Rock Band""",Alcatrazz,Art & Literature,"Stephen King's: "" Pet ________"".",Cemetary +General,Who was king juan carlos' predecessor,General francisco franco,Art & Literature,What Is (Onomatopoeia) ,"The Use Of Words Which Sound Like The Event They Describe, Such As Bang ",General,What is the only mammal capable of true flight,Bat +General,In the Tom and Jerry cartoons name the other mouse,Nibbles or Tuffy,General,"What Word In the English Language Originates From The Czech Meaning ""To Work Very Hard""",Robot,General,What is a tombstone inscription called,Epitaph +General,Balneology Is The Study Of Which Creatures?,Whales,General,"Who sang that he was an ""okie from muskogee""",Merle haggard,Food & Drink,How many people in the world are classified as chronically undernourished ? ,800 million  +General,A Comte France Landgraf Germany Conde Italy what England,Earl,Science & Nature,What Is A Woman's Last Period Known as ,The Menopause , Geography,What is the capital of Rwanda ?,Kigali +General,"What New York Yankee's 1927 jersey fetched a record $360,000",Lou gehrig,General,Who was the sponsor for the tv show Wayne's World?,Noah's Arcade,Food & Drink,Rioja comes from which country? ,Spain  +General,"Agent Blue is a herbicide that was used agaainst the Vietnamese, what type of poisonous compound does it contain",Arsenic,General,In what country is The Duma part of parliament,Russia,Food & Drink,"Boston butt, jowl, and picnic ham are all parts of a what ",Pig  +General,The 1961 Mercedes 300sx had two firsts name either,Gull Wing doors – fuel injection,General,Unit of weight measuring fineness of silk and nylon,Denier, History & Holidays,"The world was declared safe from which virus in 1979, after it had killed more than 1 billion people? ",Smallpox  +General,What is the flower that stands for: early friendship,Blue periwinkle,General,Chicago Transit Authority is now known as which group,Chicago,General,Which vitamin is also known as pyridoxine?,Vitamin B6 +Music,Who Is Beck Hansen,Beck,General,Capital cities: Romania,Bucharest,General,Which word meaning crop growing comes from Latin to plough,Arable + History & Holidays,How Did Darth Vader Know What Luke Skywalker Got For Christmas ,He Felt His Presents ,Food & Drink,Thin round Mexican maize cake ,Tortilla ,General,How many folds does a monopoly board have,One +Sports & Leisure,The 1976 Summer Olympics were held in this city.,Montreal,General,Who lit the flame 1956 Olympics and then broke 8 world records,Ron Clark, History & Holidays,Who Invented The Spinning Frame In 1769? ,Sir Richard Arkwright  +General,What is Harry Houdini famous for being ?,Escapologist,General,What is the last book of the Old Testament,Malachi,Music,Who Had A Hit In 1992 With Hazard,Richard Marx +General,What is the Capital of: Iraq,Baghdad,General,"According to a recent survey among women, what is the least favorite part of the male body",Feet,Sports & Leisure,Which woman swimmer won an individual silver medal for Great Britain in the 1980 Moscow Olympics? ,Sharron Davies  +Music,What Was The Name Of Bill Haley's Best Known Band,The Comets,General,What real person has been played most often in films,Napoleon Bonaparte,General,Retsina is a wine from which country,Greece +General,What's the United Kingdom's largest ethnic minority,The scots scots,General,In the New Testament publicans had what job,Tax Collectors,General,Which pop group had a hit with Silence is Golden,Tremaloes +Science & Nature, A snake's __________ is located in the front one_fifth portion of its body.,Stomach,General,What animal does the adjective 'cervine' refer to,Deer,Sports & Leisure,"Until 2001, Southampton Played At The Dell But Have Now Moved To Where? ",St Mary's  +Music,Almost Unreal By Roxette Was The Original Soundtrack To Which Film,Super Mario Bros,General,The musical Chu Chin Chow is based on what fable,Ali Baba and the forty thieves,General,"What links Ciampino, Lod and Waalhaven",Airports Rome Tel Aviv Rotterdam +Science & Nature,In Which Country Was Fuel Injection For Cars Inroduced In 1954 ,Germany ,Music,"Which Band Recorded The Album ""Zenyatta Mondatta""",The Police,Science & Nature," When young abalones feed on red seaweed, their shells turn __________",Red +Music,Which Band Sang About Their Mothers In 1997,Spice Girls,General,What unusual item can you buy - vending machine Paris Metro,Levi 501s in 10 sizes,General,Who is the Roman equivalent of the Greek goddess Nike,Victoria +Science & Nature," Javelinas are very noisy animals among each other and squeal, snort, woof, and click their teeth to __________",Communicate,General,What nationality was Louis Braille,French,General,K-mart. Definately. Definately K-mart.,Rainman +Sports & Leisure,"What is the connection between Alex Higgins, Barry McGuigan & Jimmy White? ",Nicknames Connected With Wind ,General,"What is ""the other white meat""",Pork,General,Who was the oldest man to hold a boxing title,Archie moore +General,What animal was believed to be a cross camel - leopard,Giraffe,General,Collective nouns - a group of what is a charm,Goldfinches,General,What auto company raised a giant three pointed star above the Stuttgart skyline,Daimler benz + History & Holidays,"1955 saw the worst ever disaster in motor racing history when a Mercedes Benz hurtled into the crowd at which French race track, killing 86 people in total ",Le Mans ,General,Which tube line runs to Heathrow Airport,Piccadilly line,General,Kipros in Greek Kibris in Turkish what is it in English,Cyprus +Geography,What is the capital of Florida,Tallahassee,Science & Nature,An animal is a fish if it has _________.,Gills,General,Who is the mother of apollo and artemis,Leto +General,Beethoven's third symphony is nicknamed what,The Eroica, History & Holidays,Who succeeded Winston Churchill as Prime Minister of England?,Anthony Eden,General,What year's Winter Olympics introduced the super giant slalom,1988 +Geography,In which city is the C.N. Tower,Toronto,Science & Nature,Emphysema Affects Which Part Of The Body? ,Lungs ,General,Which astronaut did Tom Hanks play in the film Apollo 13,Jim lovell +Geography,Across Which River Is The Longest Single Span Suspension Bridge ,The Humber ,General,Generation X Toys: Design tool for budding Gloria Vanderbilts,Fashion plates, Geography,Burma was the former name of which country ?,Myanmar +People & Places,Who sacked Jonathon Shallit as her manager in 2000? ,Charlotte Church ,General,What do Al Gore and Tommy Lee Jones have in common,At Harvard Together,General,"Vivaldi, Purcell and Handel's music is what type",Baroque +General,What is a strong aversion to beards known as,Pognophobia,General,What Is The Collective Term For A Group Of Apes,A Shrewdness,General,Where is madras,India +General,What is Switzerland's official name,Swiss Confederation,General,The Afghani tribesmen drink Khoona on wedding night what is it,Watered down warm bulls semen,Geography,What country is Santo Domingo the capital of,Dominican republic +General,The De Beers group of companies controls more than 80% of the world's supply of ______?,Rough diamonds,Music,Which Pop Group Were The Central Characters In The 1990 Film “Seeing Double”,S Club 7,Entertainment,Killed Superman,Doomsday +General,"What is the literal meaning of the Dutch word brandewijn"" which we call brandy",Burnt wine,General,Where did anne frank die,Belsen,General,Two possible answers - in which country would you find Shit,Iran or Ethiopia +General,Who wrote most of the new testament books,Paul,Religion & Mythology,"In Greek mythology, who were Achilles' parents?",Peleus and Thetis,General,What company was the first to mass produce watches in 1893,Ingersoll - sold $1 each then + History & Holidays,Which 60's Movie features The Line back in your gilded cage Melanie Daniels ,The Birds ,General,What Japanese word means good for nothing,Yakuza,General,"What comic immortalized the line, ""take my wife, please""",Henny youngman +General,What democratic country gives military aid to both iran and iraq,United,General,"Who was the star of the movie ""Running Brave""",Robby benson,Science & Nature,Where Do Swallows Go When They Migrate From Britain In The Winter ,South Africa  +General,Who did squeaky fromme try to assassinate,Gerald ford,General,What is the Australian name for a long narrow ox bow lake,Billabong,Food & Drink,Which Alcoholic drink is the main ingredient of the cocktail 'Zombie'? ,Rum  +Music,"Which Artist One Had A Musical Alto Ego Called ""The Thin White Duke""",David Bowie,Music,In Addition To Co-Directing West Side Story With Robert Wise What Was Jerome Robbins Other Contribution To The Film,Choreography,Food & Drink,What is the name of cold Spanish soup made from peppers and tomatoes? ,Gazpacho  +Science & Nature,How man legs does a crab have,Ten,General,"American artist, Grant Wood, depicts his dentist, B.H. McKeeby, and his sister Nan as a farmer-preacher and daughter in which 1930 painting",American gothic,General,"South what actor did barbara walters ask: ""do you take steroids""'",Arnold +Music,"Who Sang The Song ""Sweet Child O Mine"" In The 90's",Guns N Roses,General,What is a Sybian,Vibrating saddle dildo,General,Narton is a mixture of baking soda and salt what was it used for,Mummification +Music,The KLF Had Their First No.1 Under A Different Name What Was It,The Timlords / Doctorin The Tardis,Food & Drink,Which vegetable is also known as the spinach beet or seakale beet? ,Swiss Chard ,General,St Gerard is the Patron Saint of who,Pregnant Women +Science & Nature,"Hammer, anvil, and stirrup are parts of the ________.",Ear,General,"In the tv series 'the fall guy', who did lee majors play",Colt seavers, Geography,To what country do the Faeroe Islands belong?,Denmark +Toys & Games,This is the strongest poker hand you can get.,Royal flush, Geography,Which imaginery line approximately follows the 180 degree meridian through the Pacific Ocean?,International Date Line,General,What is the fear of heaven known as,Ouranophobia +Music,"Confide In Me Was A Hit In 1994, Who Was The Singer",Kylie Minogue,General,"Countries of the world:north eastern Europe & northern Asia, major cities include St. Petersburg & Novosibirsk",Russia,General,Which saint was the first Bishop of Paris,St denis +General,"In what is food surrounded with dry, hot, circulated air",Convection oven,General,"Which capital city was originally scheduled to hold the 1940 Summer Olympics, and had to wait for twenty-four years",Tokyo,General,Rice-Kellogg invented what in 1924,Loudspeakers +General,What Japanese dish consists of Tofu Beef and vegetables,Sukiyaki,General,What does conus stand for,Continental united states,General,What sport you would tick tack or walk the dog in,Skateboarding +Music,From which musical did ‘One Night in Bangkok’ & ‘I Know Him So Well’ come?,Chess,General,Mendelssohn's symphony number 4 is nicknamed what,The Italian,Science & Nature," A hibernating woodchuck breathes only ten times per hour. An __________ woodchuck breathes 2,100 times an hour.",Active +Science & Nature,Of Which Animal Is The Gander The Female? ,Goose ,Food & Drink,What drink was invented by John Pemberton in 1886? ,Coca Cola ,General,Who designed the first jet engine - flew in 1941,Sir Frank Whittle +General,What is 2000 in Roman Numerals,MM,General,"Which Spice Is Featured On The Flag Of ""Grenada"" The Isle Of Spices",Nutmeg, Geography,What is the basic unit of currency for Croatia ?,Kuna +General,"In Coronation Street, which room in the Rover's Return was regularly used by Ena Sharples and friends",Snug,Entertainment,"Who played the lead in the movie ""Snatch""?",Brad Pitt,General,Children take SATs what does SAT stand for,Standard assessment tasks +General,What is the correct term of address to the Pope,Your Holiness,General,"What instrument did jazz musician, Chet Baker, play",Trumpet, Geography,What is the capital of Burundi ?,Bujumbura +General,What is a person with numerophobia afraid of,Numbers,General,"Who does the statue, the Colossus of Rhodes, depict ?",Helios,Music,What Nickname Did Radio 1's DLT Give To Olivia Newton John,Olivia Neutron Bomb +General,When is the only time a flag should be flown upside down,Emergency,General,In what Australian state would you find Dubbo,New south wales nsw,General,Parliament what is the capital of bulgaria,Sofia +Science & Nature,What is the chemical name for quicksilver?,Mercury,General,Which chess player was beaten by IBM's 'Deep Blue' in 1997,Gary kasparov,General,Who wrote the music for the film ET (both names),John Denver +Sports & Leisure,In Which Card Game Might You Be Dealt A Yardborough ,Bridge (No Card Over 9) ,General,In Which Former Soviet Republic Do 100 Luma Make A Dram,Armenia,Music,What Are The Names Of The 4 Members Of U2,"Bono, The Edge, Adam Clayton, Larry Mullen" +General,What is a group of grasshoppers,Getting bald,General,What's the most common first name in the world,Muhammad,General,Who was the only World Heavyweight boxing Champion to go undefeated throughout his career?,Rocky Marciano +General,What not so hep disease had its virus finally identified in 1984,Hepatitis,General,Seplophobia is the fear of,Decaying matter,People & Places,Who was The Naked Chef ,Jamie Oliver  +General,Which British King had the-longest reign,George iii,General,In 1969 Sport was the first magazine to run an ad for what,Condoms,General,The average person eats 800 in their lifetime 800 what,Chickens +General,"On December 1, 1917, who opened Boys Town, a farm village for wayward boys, near Omaha, Nebraska",Father Edward Flanagan,General,"Inderan, Delgado, LeMond and Fignon names in what sport",Cycling,General,What paper is used to test acid and alkali,Litmus +General,What is the flower that stands for: coldheartedness,Lettuce,Geography,What is the Administrative Centre of East Sussex ,Lewes ,General,What does the computer term 'mmu' mean,Memory management unit +General,In what sport do you find 'coursing',Greyhound racing,Geography,Brussels is the capital of which country ,Belgium ,Sports & Leisure,Which club did Jose Mourinho leave to become manager of Chelsea in 2004? ,Porto  +General,"In The World Of Music How Is ""Leslie Charles"" More Commonly Known",Billie Ocean, History & Holidays,Who played an elf called Patch in the film 'Santa Claus the Movie'' ,Dudley Moore ,General,La Sila lies in which region of Italy,Calabria +Music,Whose Operas Include Tanhauser & Tristan Und Isolde,Wagner,General,What is the strongest muscle,Tongue,Music,From Which City Do The Smiths Hail,Manchester +General,What actor was once fruit picker iceman truck driver propman,John Wayne,General,What was Anita Bryant known as because of the product she advertised,Coke girl,Entertainment,How many Oscars did Ben Hur win?,Eleven +General,Name the Duke of Wellingtons horse at Waterloo,Copenhagen,General,What is the commonest symbol on flags of the world,Star,General,Whose memoirs were called There and back again,Bilbo Baggins The Hobbit +General,Who composed the Symphonies Fantastique,Hector Berlioz,Entertainment,What was the first CD pressed in the USA?,Born In The USA,General,"Palm, Olive, Cyprus and Cedar what is the biblical link",Woods cross made +General,Whom did David Steel succeed as leader of the Liberal Party,Jeremy thorpe,Science & Nature, The life expectancy of the average mockingbird is __________,10 years,General,Where is the arch of hadrian,Athens +General,How many hurdles are there in a 400 metres hurdle race,Ten,General,What are put into an omelette in a Hangtown Fry,Oysters,Entertainment,What was Daffy Ducks usual closing line,You're dispicable ! +General,Who tells the story in The Arabian Nights,Sheherazade,General,What is the Capital of: Burundi,Bujumbura,General,What TV personality did Doritos commercials?,Jay Leno +General,Tanzania is the country that resulted from the union of zanzibar and ______,Tanganyika,General,"Which Game Invented By Keen Birdwatcher ""Peter Adolh"" & Named After The Latin For Hobby Hawk Was Axed By It's Creator's In The Year 2000 After A Run Of 53 Years",Subbuteo,Entertainment,Andre Rieu and the Johann Strauss Orchestra are famous for what musical love piece,Romeo and Juliet +General,"In common: henry ford, adolf hitler, frank sinatra, orville and wilbur wright",Secondary school dropouts dropped out of school school drop out,General,"Which chemical element emits a greenish glow in air, and, unless kept in water, burns of its own accord",Phosphorus,General,Greeks Romans regarded what herb as symbol immortality,Tansy +General,Which detective was played by Jack Webb in Dragnet,Sgt joe friday,Music,"What Instrument Do You Associate With ""Yehudi Menuhin""",Violin,Music,"Who Was The Subject Of The Autobiography ""Tainted Life""?",Marc Almond +General,What do americans call chick peas,Garbanzo beans,General,Small parrot often kept as a pet,Budgerigar,Food & Drink,Where Might You Find 'Dead Mans Fingers' ,In A Crab  +Food & Drink,Which French District Does Claret Come From ,Bordeaux ,General,What sort of creature is a bariroussa,A pig,General,"In Greek mythology, as what were apricots known",Golden apples +General,"The Church of Santa Maria delle Grazie is a World Heritage Site, in which Italian city would you find it",Milan,General,A Seattle ordinance says goldfish in bowls must do what in buses,Stay still - not move, Geography,What is the capital of Luxembourg ?,Luxembourg + History & Holidays,"""What Native Language Would Jesus Christ Have Spoken ? (""""Hebrew, Sanskrit, Arabic, Aramaic"""" ) "" ",Aramaic , History & Holidays,In What Year Did The French Revolution Begin ,1789 ,Science & Nature,What Is PH A Measure Of ,Acidity Or Alkalinity  +General,What religion was Adolf Hitler,Roman Catholic,Music,"How In The World Of Music Is ""Helen Folosade"" Adu More Commonly Known",Sade,General,"What Liverpool band popularized the Doors' ""People Are Strange?"" in the 80's?",Echo and the Bunnymen +Music,"Which Member Of Duran Duran Produced ""Kajagoogoo's"" First Album",Nick Rhodes,General,Anothe name for an artists workshop or studio,Ateller,Sports & Leisure,"England Played Football At The Weekend Or Tried To But Only Managed A 0-0, Draw Who Did They Play Against ",Macedonia  + History & Holidays,Which Belgian noble woman was sentenced to death for bathing in the blood of servant girls she had murdered in order to keep her young ,Countess Elizabeth Bathory ,General,Shakespeare play set in Ephesus has two pairs identical twins,Comedy of Errors,General,How did Indiana previously know Marion in Raiders of the Lost Ark?,He studied with her Father +General,What is a coral island consisting of a ring of rock enclosing a central lagoon,Atoll,General,Which wine grape variety is most planted in California,Chardonnay,General,"British rock-music group that rivaled the popularity of the group's early contemporaries, The Beatles",The rolling stones +Sports & Leisure,A Snooker Player Makes A Break Of Eight Points. Which Three Colours Are Potted? (PFE) ,"Red, Yellow & Brown ",Music,Which Female Vocalist Would You Most Associate With Fairground Attraction,Eddi Reader,General,In which movie is happy bob barker's partner,Happy gilmore +General,"In the 1980 Summer Olympics, Nadia Comaneci was judged to have given a perfect performance on the asymmetrical bars and which other exercise",The beam,General,Who was the first U.S. secretary of the treasury,Alexander hamilton,General,The first section of railway track in Britain ran between which 2 places?,Stockton to Darlington +General,What fish was the subject of dispute Spain Canada in 1995,Turbot,General,Where is the only bone not broken because of a skiing accident,Inner ear,Music,Who Sang The Theme For The Bond Film “For Your Eyes Only”?,Sheena Easten +General,A roofed gallery with an open arcade or colonnade on at least one side.,Loggia,General,Yamoussoukro is the capital of which country,Cote d'ivoire,General,"What's a ""semaphore""",Arm signals +Sports & Leisure,In which game would you have a pitchers mound and an outfield? ,Baseball ,General,"Who famously had a sign on his desk saying ""The Buck Stops here""",Harry truman,General,In the Vietnam war Comic Books was US military slang for what,Maps + History & Holidays,Which Queen Had An Extra Finger? ,Anne Boleyn ,General,"When Saigon fell, the signal for all Americans to evacuate was this Bing Crosby song playing on the radio.",White Christmas,Science & Nature,Where do you find the medulla oblongata?,Brain +Science & Nature," __________ don't fly by flapping their wings up and down. The motion is more forward and backward, like a figure eight on its side.",Birds,General,Light brown cane sugar,Demerara,General,What given out celebrate birthday of King Bjumbal Thailand 1983,Free Vasectomies +General,"What goes with ruby, emerald and sapphire to make up the world's most valuable gems",Diamond,General,"Which horticultural fungicide, consisting of equal parts of copper sulphate and lime, has the same name as a French port",Bordeaux mixture,Sports & Leisure,Which Country Hosted The 2004 Summer Olympics ,Greece / Athens  +General,In which country were Lada cars manufactured?,Russia,General,Who was the only boxer to knock out mohammed ali,Larry holmes,General,In the Archie comics what is Jughead's first name,Forsyth +Music,"Who Released The Original Recording Of ""Lets All Chant""",The Michael Zager Band, Geography,What is the basic unit of currency for Togo ?,Franc,Music,What Do The Initials ELO Stand For,Electric Light Orchestra +General,"What links Iguanas, Koalas and Kimono Dragons",Two Penises,General,William Tayton was the first man to do what,Appear on TV – at Bairds demo,People & Places,What Was Evonne Cawley's Madien name ? ,Goolangong  +General,The song Proud Mary was writted by ___.?,Creedence Clearwater Revival,People & Places,Who Was The Young German Who Landed His Plane In Red Square ? ,Mathias Rust ,General,How many frets on a stratocaster?,22 +General,Who was the Prime Minister of Malta between 1971 and 1984,Dom mintoff,Sports & Leisure,Which formula one team does Juan Pablo Montoya drive for? ,Mclaren ,Art & Literature,In Which Shakespearean Play Would You Find The Clown Costard ,Love's Labour Lost  +Music,The Tramps came from which US city?,Philadelphia, History & Holidays,Which pie originates from medieval Christmas time? ,Humble Pie ,General,Who was Humphry Bogart's first wife,Helen Mencken +General,Who played the girl on the motorcycle in a film of same name,Marianne Faithful,General,Gymnophobia is a fear of __________,Naked bodies,General,Who was the latin bombshell that played Selena in the Selena bio-pic?,Jennifer Lopes +General,Robin Hood: Prince of Thieves was released in what year,1992,General,Isobella Mary Mayson born 1836 remembered as who,Mrs Beeton,General,The Kalahari desert is mainly in which country,Botswana +General,Whose music featured in The Clockwork Orange,Beethoven,Geography,What is the capital of Germany,Berlin,General,What is a pugilist,Boxer +General,What bites campers on the big toe,Vampire bat,General,What subject Ben Franklins letter to Royal Academy - Brussels,Perfuming Farts,General,Who was Sir Laurence Olivier's second wife,Vivien leigh +General,The song I Talk to the Trees comes from what musical,Paint Your Wagon, Language,What ONE word fits? ____hood; ____hole; ____date.,Man, Geography,Which country has the fourth largest population ?,Indonesia +General,A catalogue of words and synonyms?,Thesaurus,Science & Nature,Which Is The Smallest Of All Birds ,"Bee, Hummingbird (2 Grams) ",General,What is Manchester United's home stadium called,Old trafford +General,"Solar time what's the usual age for a jewish boy to celebrate his ""bar mitzvah""",Thirteen,Science & Nature,What term is given to the center of a black hole?,Singularity,General,What did steve martin once call terre haute,Nowhere u.s.a + Geography,In which state are the Everglades ?,Florida,General,The television detective Banacek was played by whom,George peppard,General,What is Conway Twitty's real name?,Harold Lloyd Jenkins +General,Who was the first woman to swim from port angeles to victoria,Marilyn bell,Music,"Who Recorded The Albums ""Emotions"" & ""Music Box""",Mariah Carey, Geography,Of which country does the Kalahari Desert cover 84%?,Botswana +General,Front which part of a pig do we get ham,Hind leg,Food & Drink,Which bean is used to produce a tin of baked beans? ,Haricot ,Music,Maxx Had Two Top Ten Hits In 1994 Name One Of Them,"""Get-A-Way"" And ""No More (I Cant Stand It)" +Geography,Name the largest island in the world. ,Greenland ,Art & Literature,"A European style of the late eighteenth and early nineteenth centuries. Its elegant, balanced works revived the order and harmony of ancient Greek and Roman art. ",Neoclassicism,General,Which prop item did MGM ban from film sets in the early 50s T,V sets +General,Who Was The First Actress To Be Featured On A Postage Stamp?,Grace Kelly,General,What was the colour of the 1985 film that gave Whoopi Goldberg her first Oscar,Purple,General,"Who was lord mayor of london in 1398, 1406 and 1419, and is most famous for his legendary cat",Richard whittington +General,What is the minimum IQ score for the genius category,140, History & Holidays,Who Played The Role Of Flick In The Tv Soap Neighbours ,Holly Valance ,General,"In which 1952 film, directed by John Ford, did the character Sean Thornton appear",The quiet man +General,"During The 1980's Patrick Bossert, Was The First Person On The Planet To Do What",Solve The Rubiks Cube,General,"Which Now Decased Actor & Comedian Provided The Voice Of ""Pipkin"" In The 1976 Movie Watership Down",Roy Kinear,Music,Which 2 Records Reached No.1 For Olivia Newton John And John Travolta,You're The One That I Want / Summer Nights +General,What king was murdered by macbeth,Duncan,Music,Appetite For Destruction Was The Debut Album For Which Group?,Guns N Roses, Geography,What is the basic unit of currency for Germany ?,Deutsche mark +Geography,Which Islands Were The Subject Of A War Between Argentina And Britain in 1982? ,Falklands ,General,"Which group sang the song ""Smells Like Teen Spirit""?",Nirvana, History & Holidays,Who is best remembered as the mistress of Lord Nelson? ,Emma Hamilton  +People & Places,What Is Ronald Biggs Famous For ,Being A Great Train Robber ,Sports & Leisure,Who was stabbed on court at Wimbledon by a fan of her chief rival ,Monica Seles ,General,"From Which Common Condition Would You Be Suffering If You Had ""PodobromhidosIs""",Smelly Feet +General,Who wrote Moon River used in Breakfast at Tiffanies,Henry Mancini,Music,Who Had A Massive 90's Hit With Rhythm Is A Dancer,Snap,General,In heraldry what shape is a pile?,Inverted Pyramid +General,What is the currancy unit of El Salvador,Colon,General,What was the symbol for the house of lancaster,Red rose,Toys & Games,What is another name for the card game 'Twenty-one'?,Blackjack +Art & Literature,What Dutch master painted 64 self-portraits?,Rembrandt,General,"In Greek mythology, danae was the mother of ______",Perseus,Science & Nature,Who is the only person two win two nobel prizes?,Marie Curie + Geography,What 2 countries have square flags?,Switzerland and the Vatican,General,"A ballet with a plot, usually tragic, to bring dramatic coherence to the performance of ballet.",Ballet d'action,General,"Smoke, fog, & mist are all___.",Aerosols +General,Which disease was once known as white plague,Tuberculosis,General,What is the area code for cruise ships in the Atlantic Ocean,871,General,Unusual (for him) role Bogart play in The Return of Doctor X 1939,A Vampire +Food & Drink,"Carrots Are Rich In which Vitamin Is It A,B,C,D ",A ,Entertainment,"Who directed the movie ""Blade Runner""?",Ridley Scott,General,A Renifleur gets aroused from doing what,Sniffing underwear +Science & Nature, An __________ may weigh as much as 300 pounds. Its intestinal tract is 45 feet long.,Ostrich,General,Name the subject Harry Harrison's 1977 book Great Balls of Fire,Sex in Science Fiction,General,What is the heraldic term for a small square division on a shield,Canton +General,"A famous RAH novel, as well as a number believed to be cursed",The number of the beast,General,How do frogs breathe under water,Through the skin,General,Mary Leta Dorothy Slaton became famous as who,Dorothy Lamour +General,Who signed the declaration of independence,John hancock and charles thomson,General,"In living organisms, what do lysins destroy",Cells,Music,Name The Australian Who Represented The Uk At The 1996 Eurovision Song Contest?,Gina G +General,What is football called in Italy,Calico,General,Who was Jesus betrayed by? ,Judas Iscariot ,General,What does Beetlejuice eat when he reaches out of his grave in the scale model of the town?,A Fly +General,"Which city's music was featured in ""I Feel Fine""",Liverpool's,General,"Who wrote 2001, A Space Odyssey",Arthur c clarke,Music,Name Golden Earings 70's Hit,Radar Love +Music,Who Asked Do You Want To Know A Secret,Billy J Kramer & The Dakotas,General,Someone who treats feet and their ailments,Chiropodist,Technology & Video Games,The Nintendo 64 was titled under what name during production? ,Project Reality +General,Napoleon had a fear of what - Aelurophobia,Cats,Art & Literature,Where Is The Prado Gallery ,Madrid ,General,For bangla desh What's Bill Cosby's full name on The Cosby Show,Heathcliff huxtable +General,What nation has had its own scandels dubbed muldergate & inkathagate,South africa, History & Holidays,In Which Tim Burton Film Did Johnny Depp's Character Have To Confront The Legend Of An Axe Wielding Headless Horseman ,Sleepy Hollow ,General,Who is the Patron Saint of Artists,St Luke +General,"In the dice game 'craps', what is a throw of double one called",Snakeeye,General,What's a liuor of apricot pits,Amaretto,Music,What Musical Instrument Does The Leader Of An Orchestra Always Play,Violin +General,Which Charcter Did “ Neil Hamilton ” Play In The Original Tv Series Batman?,Chief Commisioner Gordon,Music,"Who were Mark Farner, Mel Schacher and Don Brewer?",Grand Frank Railroad,General,"In the monty python parody 'search for the holy grail', what was the first thing the keeper of the bridge of death asked",What is your name +General,Who invented Fingerprint Classification?,Francis Galton,General,In Portrait Gallery there's a picture of Livingstone sitting on what,A Rhinoceros,General,What is the Capital of: Kiribati,Tarawa +General,Who was the last native-born Prince of Wales,Owen glendower,General,Ligyrophobia is the fear of,Loud noises,General,Which country has the most cars per mile of road?,England +Science & Nature,Which Contemporary Scientist Is Suffering From From Motorneurone Disease ,Steven Hawkins ,Entertainment,Who sang 'Any Way You Want Me'?,Elvis Presley,General,"Blender Music Artists: Who did ""Ticket to Ride"" in 1965",Beatles +General,"A turn on one leg, with the toe of the other leg touching the knee of the turning leg.",Pirouette,Geography,Where Is The Bridge Of Sighs ,Venice , Geography,What is the basic unit of currency for Djibouti ?,Franc +General,What's the official state sport of alaska,Dog sledding,General,What's the name of the second book in the Bible,Exodus,General,In what field was Terence Donovan famous,Photography +General,What is the Capital of: Ireland,Dublin,General,What was Winston Churchill's favourite participation sport,Polo,Language,What does the acronym 'scuba' mean?,Self Contained Underwater Breathing Apparatus +General,"Several different ways in what printing may be accomplished, such as lithography, letterpress, flexography, gravure, & screen printing",Printing techniques,Food & Drink,From Which Fruit Is Grenadine Made ,Pomegranate ,General,Which part of the tree produces the aromatic bitter of Angostura Bitters,Bark +General,What is the oldest bridge over the Seine in Paris,Pont Neuf, History & Holidays,In the Christmas song 'Let It Snow' what was the weather described as? ,Frightful ,General,Where is the original Spa,Liege Belgium +General,"Encounter on March 5, 1770, five years before the American Revolution, between British troops and a group of citizens of Boston",Boston Massacre,General,What animal helped free the trapped lion in Aesop's fable,Mouse,General,Zambia and Zimbabwe used to be called what,Rhodesia +General,Shbat is Arabic for what month,February,General,Cher Ami a homing pigeon won the DSC and had what item,A Wooden Leg,General,What is an Ulster,Long overcoat +General,What was the name of Hamlets father,Hamlet,General,What does the navajo term 'kemo sabe' mean,Soggy shrub,Science & Nature,Which British Sports Car Company Was Founded In 1962 By Jem March & Frank Costin Whose Names Inspired The More Continental Sounding Company Name ,Marcos  +Entertainment,"Name the town that Fred, Wilma, Barney, and Betty lived in.",Bedrock,General,Somewhere Out There is a song from what movie,An american tail,General,Conventional rules of social behaviour,Etiquette +Music,"Whose Debut Album ""I Should Coco"" Reached No.1 In The UK Album Charts",Supergrass,General,What sport is legal in only 16 American States,Greyhound Racing,Music,Which Was The Only Status Quo Record To Go Top 10 In The 90's,The Anniversary Waltz (Part 1) +Science & Nature,What Is Dry Ice ,Solid Carbon Dioxide ,General,Who won the russian power struggle to succeed lenin,Stalin, History & Holidays,Who was the first woman in space?,Valentina Tereshkova +General,In Morrisville Pennsylvania women need permit to wear what,Cosmetics,General,What Bond film was entitled The Dead Slave in Japan,Live and let Die,General,Where could you spend a Metical - capital Maputo,Mozambique +Entertainment,"Who says, ""Th-th-th-that's all folks!""",Porky Pig,General,What make of car is famously driven by Mr. Bean?,Mini,General,What song did bobby hebb sing to his brother in 1966,Sunny +General,18% of Americans mention who in their will,Their pet,Music,"How Many Weeks Did Abba Spend In The UK Charts In The 70's (106, 140, 181, 241)",181,General,Which organisation has been selling racehorses since 1176,Tattersalls +General,Who was both the 22nd & the 24th president of the US,Stephen grover cleveland,Music,"Which Loser Was Winning In Both ""The Square"" & The Charts In 1986",Nick Berry,General,How many lines make up a number on a digital clock?,Seven +General,"Who said ""all you need to fly are lovely things and fairy dust""",Peter pan,General,Hippoglossus-hippoglossus is which common sea fish,Halibut,General,WG Grace captained England at cricket and what else,Bowls +General,What is never shown in a Las Vegas Casino,The Time - No Clocks,Sports & Leisure,Which Former World Boxing Champion Was Jailed In May 2006 For Dangerous Driving? ,Naseem Hamed ,General,Lady when you're with me I'm smiling' were lyrics from which group's 1973 second album,Styx +General,Which London landmark is now the depository of the royal coronation regalia,The tower of london,Science & Nature,Paleontologist Sir Richard Owen Is Credited With Coining The Name 'Dinosaur' In 1841. From Greek Origin What Is The Rough Translation Of The Term ,Terrible Lizard (Deinos Saurus) ,General,In which sport does a player address the ball,Golf +General,The average adult human has 32 of these,Teeth,General,"From which movie comes the song ""Under the Sea""?",Little Mermaid,General,Who Was The First Group To Clock Up 50 Appearances On Top Of The Pops?,Status Quo +Entertainment,What term is used for the speed at which a piece of music is played ?,Tempo,General,"The first U.S. marines wore high leather collars to protect their necks from sabres, hence the term __________",Leathernecks, Geography,Monaco has the same flag as what other country?,Indonesia +General,What is a virgule,The / slash,General,Which car company makes the Celica,Toyota,Science & Nature,What scale is used to measure wind speed ?,Beaufort +General,Who was the Greek god of sleep,Hypnos,General,What colour are lobsters,Dark Blue/green pink when cooked,General,"Who composed the title music to the film ""The Deerhunter""",Stanley myers +General,Which Science Fiction author created The Triffids,John Wyndom,General,What country calles themselves Siomi?,Finland, History & Holidays,One who fought professionally in Roman arenas was a(n) ____________.,Gladiator + Geography,What is the capital of Mongolia?,Ulan Bator,General,What is the birthstone for may,Emerald,General,Between 1804 - 1873 1676 patents issued for what item,Washing Machine +General,Where was printing by movable type invented,China,Food & Drink,The hot dog was originally imported to the US from which city? ,Frankfurt ,Entertainment,Who was the Cisco Kid's faithful sidekick?,Pancho +General,Slang word for huge or enormous,Humongous, History & Holidays,Who did Tom Cruise marry on Christmas Eve 1990 ,Nicole Kidman ,General,Which British town is famous for its cutlery production,Sheffield +Sports & Leisure,Which Country Won The Most Gold Medals At This Years Olympics (Bonus Pt For How many) ,China (51) ,General,Who is the Roman God of Fire,Vulcan,General,"Crusher Which word is derived from ""user of hashish""",Assassin +Sports & Leisure,Nordic And Alpine Are Two Main Categories Of Which Sport? ,Skiing ,General,In The Classic Tv Show The Muppets What Was The Name Of The Chicken & Also Gonzo's Love Interest,Camilla,Science & Nature,Who Invented The Lift ,Elisha Otis  +Geography,"What famed ranch can you see from ranch road no. 1 near stonewall, texas ",The lbj ranch ,Food & Drink,How is steak tartare cooked? ,It is served raw ,General,Where would a soldier wear a Havelock,Head in Desert +General,"In the opera, what kind of factory did Carmen work in",Cigarette,General,"Which American newspaper has the slogan ""All the news that's fit to print""",New york times,Music,What is the name of the lead singer in Status Quo?,Francis Rossi +General,Hashimoto's disease is the malfunctioning of which gland,Thyroid,General,Endorphins are produced in the brain and in the pituitary gland. What is their function,Pain killers,General,"In the bible, who was asked to sacrifice his only son",Abraham +General,Iris Somerville - killed London 1982 - Lightning struck what,Metal Support on her Bra, Geography,What is the capital of India?,New Delhi,Entertainment,Which singer/songwriter worked in a factory making toilets for airplanes before he recorded 'Aint No Sunshine'?,Bill Withers +Food & Drink,Which country consumes more Coca Cola per head than anywhere else in the world? ,Iceland ,Sports & Leisure,In A Decathlon What Is The Total Number Of Metres A Competitor Has To Run? ,"2110 (100,110,400,1500) ",General,Soling Star' and 'Fin' are categories in which sport,Yachting +General,Buffalo River National Park is in which state,Arkansas,Music,Who recorded 'Be True to your School' in 1963?,The Beach Boys,General,What was Elvis Presley's home town,Tupelo +Toys & Games,"This popular card game's name is spanish for ""one.""",Uno,General,What was originally called the pluto platter,Frisbee,General,What are siblings,Brothers & sisters by blood +Food & Drink,In which countries would you find the following city 'Tabasco'? ,Mexico ,General,"What is the name for music that is transmitted orally or aurally (taught through performance rather than with notation, and learned by hearing)",Folk music,General,Sled dog first bred by Eskimos,Malemute +Science & Nature," The Rufous is the only species of hummingbird to nest in Alaska. They migrate 2,000 miles to Mexico each winter, and then back to Alaska in the __________",Spring,General,"How Many Times Has Liz Taylor Been Married 6, 7, 8, 9 ",8 Times ,General,Shakespeare wrote Cruel only to be kind in what play,Hamlet +General,What do the initials NASA stand for,National aeronautics & space administration,General,Anna Magnani won the 1955 Best Actress Oscar for the film version of which play by Tennessee Williams,The rose tattoo,Science & Nature,Of What Is Ecology The Study Of? ,The Study Of The Relationship Between Organisms And Their Enviroment  +General,Name the heaviest flying bird of prey,Condor,General,Traumatophobia is a fear of ______,Injury,General,Bibliophobia is a fear of __________,Books +Music,"""My Ever Changing Moods"" Was A Hit For Which UK Band",The Style Council,General,Franz Kafka wrote in German what nationality was he,Czeck,General,Which old time TV cop had a middle name of Aloysius,Steve McGarret – Hawaii 5 0 +General,"Who painted ""The Persistence of Memory""",Salvador dali,Science & Nature," In Russia, dogs have been trained to sniff out ore deposits that contain __________",Iron sulfides,General,A long or indefinite period,Aeon +General,How many years did Nelson Mandela spend in prison?,27 Years,General,Gavrilo Princip assassinated who,Archduke Ferdinand,General,What is opposition to the jewish race called,Anti-semitism + History & Holidays,Which Band Was Rod Stewart A Member Of Before Going Solo ,The Faces ,General,What common item has 32 points,The compass,General,Station which south american country has both a pacific and atlantic coastline,Colombia + History & Holidays,Who was the first Windsor monarch of the UK? ,George V (reigned 1910-1936) ,General,If you were Cock Throppling what you be doing,Bending a horses windpipe,General,What Annual Event Occurs At Coopers Hill In Gloucestershire In May,Cheese Rolling +People & Places,Which Famous Actor was Charged with Lewd Behavior in 1995 ,Hugh Grant ,General,For the city A method of resolving questions of conscience by applying moral principles or laws to concrete cases,Casuistry, History & Holidays,What was the name of Plato's school,Academy +General,What major league baseball team has a moose for a mascot,The seattle mariners seattle mariners,General,"In 'The Wizard of Oz', which character sang ""Come out, come out, wherever you are""",Glinda good witch of the north,General,From which musical does the song On The Street Where You Live come,My fair lady +General,Whose ghost haunted Scrooge with clanking chains & wierd sounds,Jacob marley,General,"The study of the earth's physical divisions into mountains, seas, etc. is _________.",Geography, History & Holidays,Which French Port Belonged To England Between 1347 & 1558? ,Calais  +Food & Drink,Which Dessert Has A Custard Base And A Burnt Caramelised Top? ,Cr?me Brulee ,General,What is the correct topping for a pastitsio,Béchamel sauce,General,"Countries of the world:western coast of Africa, major cites include Yamoussoukro & Abidjan",Ivory coast +General,Old Irish law what boys name give to fine murderer paid compo,Eric,Music,"Who Was The Backing Group To Sam The Sham On ""Woolly Bully""",The Pharaohs,General,What did the ancient Romans throw at weddings,Wheat +General,"Born In 1962 Who In The World Of Music Has The Real Name ""Stanley Kirk Burrell""",MC Hammer,General,Women's international gymnastics Beam Box Floor and what,Asymmetric bars,General,Who was John Dawkins better known as,Artful Dodger +General,"Who wrote the novel The Last of the Mohicans, first published in 1820",James fenimore cooper,General,In which ship did John Cabot sail to the New World in 1497,Mathew,General,What was the title of Charles Dicken's unfinished novel,The mystery of edwin drood +General,Which major horserace is held in Australia on the first Tuesday in November,The melbourne cup,General,Toho film studios produced most of which series of films,Godzilla,General,"When a coffee seed is planted, it takes how many years to yield consumable fruit",Five +General,"Collectively, what name is given to the first ten of the twenty six amendments to the United States' Constitution",The bill of rights,Music,"Who Sang ""Happy Xmas (War Is Over)"" With John Lennon",Yoko Ono, History & Holidays,With Which Band Did The Jimi Hendrix Experience 's Record Producer/Manager Chas Chadler Make His Name ,The Animals  +Music,In Which song did Procal Harem Sing About 16 vestal virgins,A Whiter shade of pale,Geography,In which continent would you find the yenisey river ,Asia ,General,Collective nouns a group of geldings is called what,A Brace +General,Who's only person have Dewey Decimal class named after him,William Shakespeare,Sports & Leisure,How many feet high is a basketball net,Ten,General,What is a group of monkeys called,Troop +Music,Morten Harket Is The Lead Singer Of Which Band,Aha,Music,"Freddie Mercury Had A Hit With ""Love Kills "" Or ""Love Pills""",Love Kills,General,A group of chicken is called a,Brood +General,In the language of flowers what does straw mean,Agreement,General,In Which Country Was Chris De Burgh Born,Argentina,General,What does CMOS stand for in a computer,Complimentary metal Oxide semi-conductor +General,In what field is Romuald Rat a well-known name,Paparazzi photographer,General,There are over 400 recognised breeds of what,Dog,Music,Stacy Lattisaw Had A UK Top 3 Hit In 1980 With The Song Jump To The Beat But Who Got To No 8 In The UK Charts With Their Version Of It In 1991,Danni Minogue +General,In the Chinese horoscope what animal comes first alphabetically,Boar,Food & Drink,"Sauce containing mushrooms shallots, white wine and herbs ",Chausseur , Geography,What is the capital of Fiji ?,Suva +General,Daimants Sur Canape French translation of what film,Breakfast at Tiffanys,General,Who is the hindu god of good fortune,Ganesha,General, In 1903 Frank Hanaway was the first US what in The Great Train Robbery ,Stuntman +General,Who owned a cat called Bismarck,Florence Nightingale, History & Holidays,Transformers are whats in disguise? ,Robots ,Entertainment,How many strings are there on a standard guitar?,Six +General,The French say Bis - what word do the English use,Encore,General,Brittany Spears - what is her favourite drink,Sprite,General,What Rock group are named for a split paper match splif holder,Jefferson Airplane +General,Founded In 1992 From Which Country Does The Airline Kiwi International Airlines Originate,USA,Entertainment,Casper the Friendly Ghost frolicked with which witch?,Wendy,General,"Which land was discovered by Peter, Susan, Edmund and Lucy",Narnia + Geography,Which US state gets the most rainfall?,Hawaii,General,Mintonette was the original name of what sport,Volleyball,Geography,Which capital is known as the Glass Capital of the World,Toledo +Geography,______________ is the only continent without reptiles or snakes,Antarctica,General,Which country uses the international vehicle registration letters ET,Egypt,General,What club did joe hunt lead before being sentenced to life in prison,Billionaire boys club +Religion & Mythology,Who is the greek equivalent of the roman god Diana,Artemis,General,What herbivore sleeps one hour a night,Antelope,General,What is the more usual name for green beryl,Emerald + Geography,What was the hightest mountain in the world before the discovery of everest?,Mount Everest,Music,"Which pianist/composer/conductor was born in Berlin in 1929, took American citizenship in 1943 and became a well-known face on British TV in the 70's?",Andre Previn,General,Which musician fronted the Tubeway Army,Gary numan +General,What South American cities inhabitants are called portenos,Buenos Aires,General,What New York edifice is named after an Italian navigator,Verrazano Narrows Bridge,General,How many feet are in a nautical mile ?,6080 +General,What is a group of ducks paddling,Team, Geography,Which country is also known as Suomi ?,Finland,General,"What three letter word can be placed before these words to make a new word - ""light"" ""break"" ""time""?",Day +General,What country celebrated its National Day on 1st March?,Wales,Music,"Whose Songs Have Been Successful For Guns N Roses, Manfred Man & Jimi Hendrix",Bob Dylan,General,What is the only 'real food' astronauts can take into space,Pecan nuts +Science & Nature,What's another name for tetanus ,Lockjaw ,Science & Nature,Which Bird Of Prey Can Often Be Seen Hovering Above Hedges & Ditches At The Road Side ,Kestrel ,General,What Closed Down In 1963 After 54 Years In Service,Alcatraz Prison +Music,How Is The Double Act Of David Peacock & Charles Hodges Better Known?,Chas & Dave,Mathematics,How many corners are there in a cube,Eight, History & Holidays,Who Said (Will No-One Rid Me Of This Turbulent Priest)? ,Henry II  +Science & Nature,Why Is The Mole Cricket So Called ,Spends Most Of Its Time Underground ,General,What is the name of the steam train which gained the world speed record in 1938,Mallard,General,Which spies name translates as Eye of the Dawn,Mata Hari +General,What is the largest country wholly within Europe,France,General,What is the flower that stands for: bonds,Convolvulus,General,What was the Saved by the bell series with Hayley Mills originally called?,"Good Morning,Mis Bliss" +General,Who flew too close to the sun,Icarus,General,What city is the setting for the U.S. sitcom Cheers,Boston,Art & Literature,In What book would you find a Hefalump?,Winnie the Pooh + History & Holidays,On which date is Epiphany celebrated in the traditional Western calendar? ,6th January ,General,In Maryland it's illegal play what Randy Newman song on radio,Short People,General,Name the 3 headed dog in Harry Potter and Philosophers stone,Fluffy +Science & Nature,How many hours a day does a ferret sleep?,20,General,Name the two festive islands that lie in the Pacific Ocean,Christmas & easter islands,General,Which U.S. president is on the five_dollar bill,Lincoln +General,Which Oscar-winning actor's only film as a director was the 1961 Western One-Eyed Jacks,Marlon brando,General,In Roseanne what was Roseanne's gay boss/employee?,Leon,General,What's the largest airport in the US,"Dallas, fort worth" +General,"In badminton, how many points win a singles game",Fifteen points,General,"Which album contains the tracks ""New Kid in Town' and 'Life in the Fast Lane'",Hotel california,General,Who directed the movie Wall Street 1987,Oliver Stone + History & Holidays,What Became The Tallest Building In England In October 1965 ,Post Office Tower ,General,Which Actor Of Films & TV Died When He Fell Of His Horse While Filming In Spain,Roy Kinear,General,"Whose first book, in 1962, was ""The lpcress File""",Len deighton +General,"James Stewart starred in Hitchcock's Rear Window, who was his female co-star",Grace kelly,General,"What's the international radio code word for the letter ""B""",Bravo,General,Ochophobia is the fear of,Vehicles +General,What do the tendons attach to the bones or cartilage,Muscles,General,What does a CAT scan stand for,Computerised Axial Topography,General,Hrvataska is what the natives call what country,Croatia +General,What is the name of the Chinese leader who died in 1997,Deng xiao-ping,General,What trees only produce acorns when they are fifty,Oak,General,Polish plum brandy,Slivovitz +General,What event marked the 1954 french grand prix,Return of mercedes,General,Who has played in the most consecutive baseball games,Cal ripken jr,General,Collective nouns - a mustering of what,Storks +General,Which Long Running Tv Show Was the Brain Child Of Former RAF Gunner Bill Wright,Mastermind,Music,Which Cover Of An 80’s Song  Was A Surprising Xmas No.1 In 2003,Mad World / Gary Jules,General,Who is the spouse of the Duke of Normandy,Philip Duke of Edinburgh +General,What do deciduous trees do,Lose their leaves in winter,General,The hit 'Homeward Bound' by Simon and Garfunkel was said to have been written at which UK railway station even though the exact story is somewhat shrouded?,Widnes,Music,"From the 1970’s which song and artist “, You lay your bets and then you pay the price”?",10CC / Things We Do For Love +General,Who first noticed that the sun had spots,Galileo,Music,"Who Wrote The Music To Cats, Jesus Christ Superstar & Phantom Of The Opera",Andrew Lloyd Webber,Food & Drink,What herb is used most often in garnishes? ,Parsley  +General,What nationality is Yehudi Menuhin,American,General,Which country has the most telephones per 100 inhabitants,Sweden,General,What was george custer's horses' name,Commanche +General,What is the flower that stands for: advice,Rhubarb, History & Holidays,Which frontiersman died at the Alamo?,Davy Crockett,General,Who invented Cornflakes,John harvey kellog +Sports & Leisure,Where were the 1956 Olympics held ?,"Melbourne, Australia",General,Tachophobia is a fear of ______,Speed, History & Holidays,"Who Had An 80's Hit With The Song 'Never Say Never,' ",Romeo Void  +General,What does the energiser bunny wear on his feet,Flip flops,Music,Who Sang The Girl From Ipanema,Stan Getz,General,Moscow is the capital of ______,Russia +Music,"""High Class Baby"" & ""Travellin Ligh"" Were Hits For Which Singer",Cliff Richard,Music,By What Name Are The Four Lovers Better Known,The Four Seasons,Music,Name One Of The Other 2 Groups ThatLuke Haines Has Made Albums With Since Forming The Auters In 1991,Baader Meinhof & Black Box Recorder +General,What was jimmy carter operated on for while serving as president,Haemorrhoids,General,"Which film star made his screen debut in the film ""Steamboat Willie"" in 1928",Mickey mouse,General,What town and stream in West Australia same name pop group,Abba River +Food & Drink,What Does 'CAMRA' Stand For ,The Campaign for Real Ales ,General,The artist Marc Chagall was born in what modern-day country?,Belarus,General,What is the purpose of an analgesic drug,To reduce pain +General,Give either of poet E.E.Cummin s' christian names. edward,Estlin,General,What two factors do meteorologists combine to determine the heat index,Temperature & humidity humidity & temperature,Entertainment,"This actress appeared in ""St. Elmo's Fire"", ""The Scarlett Letter"", and ""Striptease"".",Demi Moore + History & Holidays,What Were Frosty The Snowmans Final Words ,I'll Be Back Again Someday ,General,What is the motto of the three investigators,We investigate anything,Sports & Leisure,"In Rhythmic Gymnastics, competitors have four pieces of apparatus; rope, hoop & ball, what is the other piece? ",Ribbon  +General,In which drink will a raisin in a glass keep floating to the top and sinking to the bottom,Champagne,General,Into what sea does the river Volga flow,Caspian,Sports & Leisure,"AQOS is now the longest running sports quiz on TV when was it first broadcast? 1969, 1970, 1971,1972 ",1970  +General,"Which bony structure includes the zygomatic, ethmoid, and vomer bones'",Skull,General,"Mechanical device that produces a force, or thrust, along the axis of rotation when rotated in a fluid, gas or liquid",Propeller,General,What is green in a pure molten form,Gold +General,What meteorological significance is traditionally attached to cows lying down,Rain,General,Maseru is the capital of which African country,Lesotho, History & Holidays,"What was the nickname of President Duvalier of Haiti, who died in 1971? ",Papa Doc  +General,The Swiss spend the worlds most money per capita on what,Insurance,Sports & Leisure,"In which sport is the term ""love"" used",Tennis,General,"What war began in 1899 with the invasion of natal, and ended in 1902 with the peace of vereeniging",Boer war +General,"Which character did Mel Gibson play in the film ""Braveheart""",William wallace,General, A person in his eighties is called a(n) __________.,Octogenarian,General,Walt Disney World is home to the largest working wardrobe in the world with over ________________ costumes in its inventory,2.5 million +General,What was the largest town in Britain before it became a city in 1992,Sunderland,General,Graham Hill won 1968 world championship in which make of car,Lotus,General,Who was the Roman Goddess of the land,Terra +General,Who was known as The King of Pop,Michael Jackson,General,Which British car was the first to sell over 1 million models,Morris Minor,General,Who was the first person to wear a wristwatch,Queen Elizabeth 1st +General,In 1999 470 Chinese were injured by what,Exploding beer bottles,General,What's the most abundant metal element in the earth's crust,Aluminum,General,William Hartnell was the first to play what TV character,Dr Who +General,A stitch in time saves ____,Nine,Geography,In which country is Sapporo,Japan,General,300000 American teenagers get what every year,Venereal disease + Geography,What is the capital of Belgium ?,Brussels,General,What do you do with a hassock,Kneel on it in church,General,Which Latin American dance involves forming a chain,Conga +General,"In Which Film Did Madonna Play A Character Called ""Gloria Tatlock""",Shanghai Surprise,General,Who earned a Nobel Prize for peace for his role in ressolving the Russo Japanese War,Theodore roosevelt,General,"In Greek mythology, what is the alternate name for polydeuces",Pollux +General,In 1973 Roland Ohisson was buried in a coffin made of what,Chocolate,General,Where - accident 1953 - motor sport killed 83 spectators,Le Mans, History & Holidays,"He is a pumpkin whose top and stem have been cut out and interior removed, leaving a hollow shell that is then decoratively carved. Twelve Letters ",Jack O' Lantern  +General,What state is only part of the United States by treaty?,Texas,General,What is a group of this animal called: Cattle,Herd drove,General,What is the Capital of: Malaysia,Kuala lumpur +General,"President richard m nixon called what songstress an ""ambassador of love""",Pearl baily,Sports & Leisure,How many minutes is each period of hockey?,Twenty,General,Matador and Sniff are two varieties of what game,Dominoes +Music,Name The Legendary Australian Outlaw Portrayed On Film By Mick,Ned Kelly,General,Thomas Sullivan in New York in 1908 introduced what,The Tea Bag, Geography,What is the longest mountain range in the world?,Andes +General,In which film do the blue meanies attack pepperland and are defeated by the beatles,Yellow submarine,General,Alfred Bailey started what annual publication in mid 19th century,Whos Who, Geography,In which state is Stone Mountain?,Georgia +General,Who Was The First US President To Take Up Residence In The White House,John Adams,General,What attaches the muscles to the bones or cartilage,Tendons,Science & Nature,"Predating The Model T Ford Which Was The Worlds First Car To Be Made In Large Quantities 19,000 Being Sold Between 1902 & 1906 ",The Curved Dash Oldsmobile  +General,How was the greek city of troy penetrated,Wooden horse,Sports & Leisure,Why Did Henry Viii (8 th ) Ban Bowls ,To Force People To Take Up Archery ,General,"Who was the lead actress in the famous bomb ""Grease 2""?",Michelle Pfiffer +General,Mexico city is the capital of ______,Mexico,General,Where was the Hesperus wrecked,Massachusetts Normans Woe Glos.,General,An area of low pressure is called a what,Cyclone +General,What celebrated photographer snapped shots of Yosemite for 67 straight years,Ansel adams,Music,Which Band Backed Herb Alpert,Tijuana Brass,General,Who played sherlock holmes in the hounds of baskerville in 1978,Peter cook +Geography,Which River Forms A Border Between France & Germany ,The Rhine ,General,A fimetarious organism grows where,In faeces or shit,General,What animals head appears on the label of Gordon's Gin,Boar +General,"In the game 'banjo-kazooie', who is banjo's little sister",Tooty,General,Which Piece Of Sporting Equipment Used To Be Called A BattleDore?,Badminton Racket,General,"What us academy is located in colorado springs, colorado",Air force +General,What are affected by phylloxera,Vines, History & Holidays,Which Band Turned Down A 1 Million Pound Offer To Play At The Shea stadium In 1967 ,The Beatles ,General,For which country is the lotus flower the national symbol,India + Geography,What tunnel connects France and Italy ?,Mont Blanc Tunnel,General,In India what is 'pachisi',Board game,General,What is the name of the Russian triangular guitar,Balalaika +Music,Who Wrote The Collection Of Short Stories On Which Fiddler On The Roof Was Based,Sholom Aleichem,Music,Who Had A Heart Attack On Stage In 1975 & Remained In A Coma Until His Death 1984,Jackie Wilson,General,What is a group of caterpillars,Army +Sports & Leisure,Baseball: The Chicago ______?,Cubs,General,Collective nouns - A Labour of what,Moles,General,Homophobia is the fear of,Sameness +General,Which White Blues Singer Attended The University Of Texas In The Early 60's & Was Voted Ugliest Man On Campus,Janis Joplin,Sports & Leisure,Where Were The 1964 Olympics Held ,Tokyo ,General,"What word can be added to Fae, Fen, Bil, Goose to make fruit",Berry +Entertainment,"Who played the lead in the movie ""The Mask""?",Jim Carrey,General,Which artery supplies the kidneys with blood,The renal,General,Do frogs or toads move faster,Frogs + History & Holidays,To what position was John Masefield appointed in May 1930? ,Poet Laureate ,General,"Traffic Trivia: Red means stop, _____ means go.",Green,General,What is the nickname for Missouri,Show me state +General,Which of Shakespeares plays involves a pound of flesh,The merchant of venice,General,Who lost her sheep,Little bo peep,General,"How does a wine become a ""fortified"" wine",Brandy is added +General,What is the Capital of: Mauritania,Nouakchott,Food & Drink,What delicacy comes from fattened livers of geese? ,Pate de Foie Gras ,General,What does P stand for in the abbreviation PLO,Palestine +Music,Which Beautiful Capital By The Sea Is Wonderful,Wonderful Wonderful Copenhagen,General,Who invented the rocking chair,Benjamin Franklin,General,What's the only movie Alfred Hitchcock make twice,The man who knew too much +General,What do you call the person who carries a golfer's clubs,Caddie,General,Which country with over 800 languages is the woirld's most linguistically diverse country?,Papua New Guinea,General,Who was the 4th president of the U.S.,James madison +Science & Nature,"When a tumour is cancerous, what is it said to be?",Malignant,General,"The Term ""Money Is The Root Of All Evil "" Features In Which Book Of The Bible",Timothy,General,Tubetti lunghi is what pasta shape,Elbows +General,What common word comes from the Latin for who are you,Quiz,General,Thomas Savery & Thomas Newcomen invented the___.,Steam engine,General,1910 act illegal to transport woman state line immoral purposes,Mann Act +Music,Freda Payne Enjoyed Her Only Major Chart Hit In 1970. Name It?,Band Of Gold,General,The human body contains enough of what to make 900 pencils,Carbon,Music,Which Drug Culture Record Took The Shamen To No.1 In 1992,Ebeneezer Goode +General,Joan O Neill Who Sings With The Tina Turna Tribute Band Is Actually The Mother Of Which Very Successful Pop Star,Mel C (NOT MEL B), History & Holidays,Where did the first atomic bomb explode ,"Trinity site, new mexico ",General,Where is the fourth most popular place on a ship to have sex,The lifeboat +General,"Who wrote the classic science fiction novel ' Hellicona Spring""",Brian aldiss,General,What type of animal is a markhor,Wild Goat,General,Who was the driver for the jordan team in the 1998 grand prix,Damon hill +General,In what sport would you use spikes and blocks,Athletics,Sports & Leisure,Name The Snooker Player Who Is Nicknamed 'The Rocket''? ,Ronnie O'Sullivan ,General,With what type of meat would you make the dish Marengo,Chicken +General,What country - largest earthquake of 20th cent 8.6 Richter 1906,Colombia,General,Who starred as an alien in the 1970's film 'The Man Who Fell to Earth,David bowie,General,A Sitophilliac gets sexually aroused from what,Food in sex play +Music,What Is Chopin's Piano Sonata In B Flat Minor Also Known As,The Funeral March,General,Which year were the Jesse Owen Olympic games,1936,General,What is made from the bark of the cinchona tree,Quinine +General,Which U.S. State is known as the Mother of Presidents,Virginia,General,What is the most used expression in any language on earth,Ok,General,What’s the only alt therapy fully recognised Western medicine,Osteopathy +Music,Who recorded 'I Can't Explain' in 1965?,The Who, History & Holidays,Who Released The 70's Album Entitled Exile on Main Street ,The Rolling Stones ,General,When was the Kon-Tiki expedition,1947 +Geography,Where were the Pillars of Hercules located,Gibraltar,General,In The Simpsons what is the first name of Chief of Police Wiggum,Clancy,Science & Nature, Thirty thousand monkeys were used in the massive three_year effort to classify the various types of __________,Polio +General,Where could you have a kip - then spend it,Laos its currency,General,"Founded in 1896, what was IBM formerly called",Tabulating machine company, Geography,What is the capital of Libya ?,Tripoli +Food & Drink,Which Supermarket Chain Uses Jamie Oliver To Advertise It's Products ,Sainsburys ,General,Who designed the Statue of Liberty?,Frederic Bartholdi,General,Which airline has the registration prefix 'VR'?,Cathay Pacific +General,What language do gypsies speak,Romany,General,What is a person who has made a pilgimage to mecca,Hajji,General,Who played the title role in the U.S. sitcom,Rhoda' valerie harper +General,In which sea would you find the island of Bornholm,Baltic sea,General,Odontophobia is fear of ______,Teeth,General,What city does orly airport serve,Paris +General,Approximately how many genes are there on one human DNA molecule,80,General,What U.S. city is home to the Pentagon,Washington,General,What is the fear of light known as,Photophobia + Geography,In what country do people speak the Language they call Nihongo?,Japan,General,Of what did israel acquire 4 out of 7 in 1955,Dead sea scrolls,General,"Crested, Smooth, & Palmate Are All Types Of What Sort Of Amphibious Creatures ?",Newts +General,What candy did president reagan keep on his desk in the white house,Jelly,General,What is the name of the Austrian monk who experimented with peas and is considered the founder of genetics,Gregor mendel,General,Who devoured a cake with the words 'eat me' written on it in currants,Alice +General,"What comedy act started as ""the six musical mascots""",Marx brothers,General,"Until the 18th century, who produced almost all the world's diamonds",India,General,What's the capital of peru,Lima +General,An upright masonry support.,Pier,General,Which African country has the shilling as it's currency?,Kenya, History & Holidays,Which Musical Opened On Broadway In 1956 And Starred Julie Andrews ,My Fair Lady  +General,Collective nouns - What are a group of greyhounds called,A Leash,Science & Nature,What is the SI unit for frequency? ,Hertz ,General,Ida morgenstern was played by what actress in the sitcom rhoda,Nancy walker +General,"Popular in the south of the United States, which peculiar meat was the original basis of a 'Brunswick Stew'?",Squirrel,Music,According To Alan Price What Did Simon Smith Have,Simon Smith & His Amazing Dancing Bear,General,Who wrote The Rime of the Ancient Mariner,Samuel taylor coleridge +General,Rhus Radicans shrub green flowers white berries common name,Poison Ivy,General,What sport is played at Smiths Lawn,Polo,General,In 1605 Japanese Emperor made what compulsory in schools,Learning swimming +Science & Nature,What is the abbreviation for trinitrotoluene?,TNT,General,"""Mageirocophobia"" Is The Fear Of What",Cooking, Geography,What is the capital of Saudi Arabia ?,Riyadh +Science & Nature,What Is The Most Common Venereal Disease ,Gonorrhoea ,General,Which French brothers were responsible for the development of cinematography,Lumiere,General,Who wrote The Symphony of a Thousand,Mahler his eighth +General,Average woman's 1.5 times bigger than average mans - what,Circumference of Thighs,General,What kind of energy powers most communications satellites,Solar energy,Science & Nature,What is a female deer called?,Doe +General,"Who was the father of Ham, Shem and Japheth in the bible?",Noah,Science & Nature, The venom of the king cobra is so deadly that one gram of it can kill __________. Just to handle the substance can put one in a coma.,150 people,Geography,"In the country of __________________, the basic monetary unit is the tala.",Western samoa +General,Which country has the smallest birth rate,Vatican City,General,What is the name of a small computer introduced in 1975 by Micro Instrumentation Telemetry Systems of New Mexico,Altair 8800,General,To what can keratitis lead,Blindness +General,Which film producer said 'Anyone who goes to a psychiatrist should have his head examined,Samuel goldwyn,General,Lord byron had a ____ foot,Club,General,How are the first five books of the bible known collectively,The pentateuch +General,Manufacturer of the Pentium microprocessor,Intel,Mathematics,"What is the name given to a curve that approaches a line, but never quite touches it?",Asymptote,General,What peoples name literally translates as ordinary,Maori +General,What's the principal river of ireland,Shannon,Music,"Which Former Stooge Was A ""Real Wid Child"" In 1986",Iggy Pop, History & Holidays,Which US Singer Duetted With Donna Summer On The US Number One 'No More Tears'' ,Barbara Streisand  +Science & Nature,In the field of psychiatry this term means self-love.,Narcissism,General,What is the capital of Austria?,Vienna,General,Whose motto is 'be prepared',Boy scouts +Music,"In 1992 Who Famously Described Himself As A ""Bisexual Who's Never Had A Homosexual Experience""",Brett Anderson,General,"Ramshorn, Wandering & Dwarf Are All Varieties Of Which Creatures",Snails,General,Where would you find pedals a resonator and a piller,On a Harp +General,Railings on a balcony are called a..,Baulstrade,General,In 1994 314 Americans had what type of surgery,Buttock Lift,Science & Nature,Name The Only Monkey Living In Freedom In Europe? ,"Barbary Ape, Gibraltor " +General,Ancient Rome / Greece what Temple was dedicated to all Gods,Pantheon,General,Who founded troy,Cadmus,People & Places,By Which Stage Name Was Norma Jean Baker More Commonly Known ,Marilyn Monroe  +General,Which classical composer wrote the Hungarian Rhapsody,Franz Liszt,General,A Spanish country estate is known as a _____,Hacienda,Sports & Leisure,Who Was The Dutchman Who Became Embassy World Darts Champion In 1998? ,Raymond Barneveld  +Entertainment,Who sang 'Mull of Kintyre'?,Wings,Music,Which group went on a “Teenage Rampage”?,The Sweet,General,Who took his library wherever he went,Abdul kassem ismael +General,"What show did the catch phrase, ""Yeah, That's The Ticket"" originate on?",Saturday Night Live,Music,The Model Iman Is Married To Which Music Star?,David Bowie,General,What Organization Was Founded In 1865 By General Bedford Forest,Ku Klux Klan + Geography,What is the capital of Central African Republic ?,Bangui,General,Which sugar is found in milk?,Lactose,General,Hemmingway's Old Man and the Sea is set in which country,Cuba +General,What Is Phobophobia The Fear Of,Fear Of Fears,General,Whose original name was Jasper,Tom - From Tom and Jerry,General,How many equal angles has a scalene triangle,None +General,Daniel Keys wrote which 1959 Hugo award winning SF novel,Flowers for Algernon,General,"In Which American City Will You Find The ""Gateway Arch""",St Louis, History & Holidays,What was name of the Titanic's sister ship?,Lucitania +General,U.S. Captials - Vermont,Montpelier,General,During US recessions which group have the most unemployment,Automobile assembly workers,General,Which Himalayan hybrids of yaks and cows are invaluable to Scrabble players,Zho +General,What was the name of the train on the tv series petticoat junction,Cannonball,Geography,In What County Is Broadmoor Located ,Berkshire ,General,In Medieval China children up to 7 years old would do what,Breastfeed +Geography,"The _____________ is the lowest body of water on Earth at 1,315 feet below sea level at its lowest point.",Dead sea,General,The locals call it Al-Magrib what do we call this country,Morocco,Sports & Leisure,Which Disaster Re United The 1966 England & German World Cup Teams In A Charity Match ,The Bradford Fire  +General,Mythophobia is a fear of ______,Stories,General,Spermophobia is a fear of ______,Germs,Geography,What Is Seismology ,The Study Of Earthquakes  +General,In football where was the World Cup played in 1970,Mexico,General,Who signed the Emancipation Proclamation,Abraham Lincoln,General,What canal does Port Said stand on,Suez canal +General,In Yuma Arizona what is the punishment for citrus fruit thieves,Lots of Castor Oil,Sports & Leisure,How Many Human Players Are There In A Polo Team ,Four ,Art & Literature,"Who said 'But, soft! what light through yonder window breaks'?",Romeo +General,How many great lakes are there,Five,General,Who invented doctor Who,Terry Nation,General,Of what is genetics the study,Heredity +Sports & Leisure,Was Jimmy Connors left or right handed? ,Left Handed ,General,What islands were named after Prince Philip of Spain,The philippines,General,Under what name did Michael Barratt have four number one hits in the 1980s,Shakin' stevens +Sports & Leisure,What was unusual about Ted Schraeder's appearance at Wimbledon in the 1949 tournament? ,He played whilst smoking a pipe ,General,Who Was The Youngest Ever Solo Singer To Win A Grammy Award,Leann Rimes,General,"What movie cast included James Garner, Richard Attenbourough, Steve McQueen, Charles Bronson, Donald Pleasance, James Coburn, Gordon Jackson, Angus McPhee among many others",The Great Escape +General,Which city is home to Coleman's mustard,Norwich,General,Who is the 20th century zoologist who wrote The Naked Ape,Desmond morris,General,What does the abbreviation 'UNICEF' stand for?,United Nations Childrens' Emergency Fund + Geography,Where is Queen Maud Land located?,Antarctica, Geography,What is the basic unit of currency for Bosnia and Herzegovina ?,Marka, Geography,"On which river is London, England?",Thames +General,What is the singular of dice,Die,General,To what fabric does the French city of Nimes give its name,Denim,General,What Is George W Bush's Middle Name,Walker +Food & Drink, Name the only fruit named for its color.,Orange, Geography,"In total, how many provinces and territories are there in Canada?",Thirteen,Music,"""Yellow"" Was Which Bands First Uk Top Ten Hit",Coldplay +General,What instrument is sometimes called the clown of the orchestra,Bassoon,General,In 1995 13 books every minute sold in US were on what subject,Star Trek,General,What broadway musical was inspired by cervantes's 'don quixote',Man of la +General,What is the flower that stands for: agreement,Straw,General,The Italians call it pesce what is it in English,Fish,General,What thrill flick master died at 80 in 1980,Alfred hitchcock +General,What was the name of Alistair Macleans first best selling novel,HMS Ulysses,General,Who Was The First Undisputed Heavyweight Boxing Champion To Be European,Max Schmelling,General,In the UK 3% of people store what in their fridges,Live Maggots +Music,Number of Beatles tribute bands that are currently performing,44,Entertainment,What did Dr. David Banner become when he got angry?,The Incredible Hulk,Science & Nature,How Many Chromosomes Does A Healthy Human Have ,46 (23 Pairs)  +General,"Where did we meet Newkirk, Carter, LeBeau and Kinchlow",Hogan's Heroes, History & Holidays,"The last line of this document is ""Working men of all countries, unite.""",Communist Manifesto,General,What was mixed with vodka in 1946 to make a Moscow Mule,Ginger ale +General,"In John's Gospel, which was the first named disciple to join Jesus",Andrew,Food & Drink,Which countrys does one associate with the following foods or drinks: 'Kasutera' ,Japan ,Entertainment,What does the statue of Oscar stand on?,A Reel of Film +General,Where did we see a snorkasaurus,Flintstones it was Dino,Music,The Soundtrack To Which Musical Topped The UK Album Charts For 70 Consecutive Weeks Between 1958 & 1960,South Pacific, Geography,What river is known as China's Sorrow?,Yellow +Music,"Who sang ""Never Can Say Goodbye"" in 1974?",Gloria Gaynor,Science & Nature,As what is a camelopard also known?,Giraffe,General,Who would use a claque,Actor - Paid audience clappers +General,Whose legs were insured for one million dollars,Betty grable,General,Who would use an ankus in their job,Mahout to goad an elephant,Sports & Leisure,"Competitive Swimming Has 4 Events Butterfly, Breast Stroke, Back Stroke, & What Other ",Freestyle  +General, Dendrochronology is better known as _________.,Ring dating,Toys & Games,"In which game or sport are ""Staunton"" pieces used?",Chess,General,What would you find on Pink Sheets,Bid Asked prices OTC stocks +Entertainment,What film did John Wayne win his only Oscar for?,True Grit,General,A bath of hot aerated water used for recreational or physical therapy,Hot tub,Sports & Leisure,Where were the 1968 Olympics held ?,"Mexico City, Mexico" +General,Who was known as the 'Lady of the Lamp',Florence nightingale,General,Ombrophobia is a fear of ______,Rain,General,Agnes the girls name means what,Chaste +General,"Countries of the world:south eastern Europe, the capital is Ljubljana",Slovenia,General,Where in Canada is its Dildo,Newfoundland Town,General,Who starred as History lecturer Jim Dixon in the film version of Kingsley Amis' novel Lucky Jim,Ian carmichael + Geography,What is the capital of Alaska?,Juneau,General,Where would you find the 'cornea',Eye,General,Who created 'yertle the turtle',Dr seuss +General,"Who Famously Spent Over $200,000 On Crockery In 1981",Nancy Reagan,General,"Hot chocolate believes in miracles, and wants to know where you're from ______",YoU.S.exy thing,Music,Who Was The Lead Singer With Spandau Ballet,Tony Hadley +Science & Nature,Which Disease Of The Liver Is Associated With Alcoholism ,Cirrhosis ,Music,"""Was I Wanna Dance With Somebody"" A Hit For Cyndi Lauper Or Whitney Houston",Whitney Houston,General,Which parent determines the gender of their offspring,Father +Science & Nature,What name is given to animals which only eat plants ?,Herbivore, Geography,Which Portuguese colony reverted to China in December 1999?,Macau,General,What is the fear of glaring lights known as,Photoaugliaphobia +People & Places,What Should You Call An Inhabitaant Of Newcastle ,A Novocastrian ,General,What is halloween,All hallow's eve,Entertainment,What did Dagwood give up to marry Blondie,A family inheritance +General,Who created The Scarlet Pimpernel,Baroness Orczy,General,What Is The Name Of The Successful Group That Evolved Out Of The Group “The Split Enz”?,Crowded House, Geography,Where were the Pillars of Hercules located?,Gibraltar +Music,What Nationality Was Mozart,Austrian,General,What fruit did elvis most often layer on his peanut butter sandwiches,Bananas,General,"Calamine, used as an ointment , contains a carbonate of which element",Zinc +General,What famous director makes a cameo appearance in the Blues Brothers?,Steven Spielberg,General,What term describes the gravitational boundary which encloses a black hole?,Event Horizon,General,"Plant with oval, usually purple fruit used as a vegetable",Aubergine +Music,"Who Recorded The Albums ""Change Everything"" & Waking Hours""",Del Amitri,General,When were 'instamatic' cameras first sold,1968,General,Merinthophobia is the fear of,Being bound tied up +General,In the bible to whom is the book of Lamentations attributed,Jeremiah,General,In what film was the first flushing toilet seen,Psycho,General,Who betrayed Samson to the Philistines,Delilah +General,Bob Ayling is the Chief Executive of which British company?,British Airways,Music,Which Southport Lad's Love Became Tainted In Later Life,Marc Almond,Science & Nature,"Granny smith, james grieve and egremont russet are all types of which fruit ",Apple  +General,Who would use a mashie niblick,Golfer,Geography,What capital city does the liffey river flow through ,Dublin ,General,"On George Martin's farewell album In My Life, which actor sings the title song",Sean connery +General,Whose last published novel was Murder from the Past,Agatha Christie,General,Who wrote the music for Carmen,Georges bizet,Entertainment,Whats the smallest size grand piano?,A Baby Grand +General,In 1929 Vatican City (world's Smallest Country) is made an enclave of,Rome,Geography,In Which Country Is The Famous Volcano Popacatapetl ,Mexico , Geography,Which country are the Galapagos Islands part of?,Ecuador +General,What milk is Pecorino cheese made from,Ewes, History & Holidays,What Christmas item was invented by London baker and wedding-cake specialist Tom Smith in 1847? ,Christmas cracker ,General,Which airline has the registration prefix 'vr',Cathay pacific + History & Holidays,What Was The Name Given To The French Goverment Which Collaborated With The Nazis? ,Vichy ,General,What type of beverage is Tio Pepe,Sherry,Science & Nature, It may take longer than two days for a chick to break out of its __________,Shell +Music,Which pop singer was known as ‘the king of the wild frontier’?,Adam Ant,Food & Drink,What P word is the name given to a German Black Rye Bread? ,Pumpernickel ,General,Who discovered gold on the witwatersrand,George harrison +General,What 80's cartoon was a showcase for 'New Wave' Music videos?,Kidd Video,General,Sea Cucumbers are a type of what,Animal,General,"What 1969 film last line Clint Eastwood ""I fall off em everywhere""",Where Eagles Dare +General,What sport would you be playing if the score was duece,Tennis,General,"Where did the clones have their laboratory in ""colony""",Germantown,Music,What was Hermans Hermits only No1 UK hit?,I'm into something good +General,Mel Gibson and Danny Glover appear in which series of films,Lethal weapon,Sports & Leisure,Which Wood Are Cricket Stumps Usually Made From? ,Ash ,Food & Drink,In Which Country Were Fortune Cookies Invented ,United States  +General,What does a person look like if described as 'wan',Pale-faced,General,Pif Paf Pof is the Dutch equivalent of which English phrase,Snap Crackle Pop,General,What is the metric word for a million,Mega +General,What is 'bobba' in english,Grandmother,General,"In the film 'jurassic park', what was the largest predator",Tyrannosaurus rex,General,Name Casper the friendly ghosts horse,Nightmare +General,What media format did the denon company help pioneer,Compact discs, Geography,Albany is the capital of _____?,New York, History & Holidays,Who led the attack on the Alamo?,Santa Ana +General,"""Baby It's You"" was just one of the many hits of the early 60's for this girl group",The Shirelles,General,The average Manhattan wife takes 14 minutes to do what in bed,Turn off Light, Geography,What is the capital of Mauritania ?,Nouakchott +Science & Nature,Electrum is a natural alloy of gold and what other metal?,Silver,People & Places,Name The Playwright Who Was Born On And Who Died On 23rd April ,Shakespeare ,General,"What was Dustin Hoffman's character's name in ""Rain Man?""",Raymond Babbitt +General,French in tennis love means zero but what did it originally mean,Eggs,General,"In Greek mythology who gave the ""eyes"" to the peacock",Argus,General,The Attock is a forbidden river that no pure who can pass,Hindu +Science & Nature," Researchers don't know why killer whales like to rub their sensitive stomachs on the bottom of shallow beaches, but they think it may be a form of __________",Grooming,General,The Nullarbor desert is in Western Australia what's it mean,No Trees Null Arbor,General,Who is the Patron Saint of TV,St Clare +General,What actor Howard Hughs call pay toilet didn't give shit nothing,Robert Mitchum,General,What is a group of this animal called: Emus,Mob,Sports & Leisure,"In 1985, Manchester United player Kevin Moran became the first player to do what in an FA Cup final? ",Get Sent Off  +General,Which footballer starred in the film Lock Stock and Two Smoking Barrels,Vinnie jones,General,What are mustard and ketchup,Condiments,General,"What creature, when drunk, always falls on its right side",An Ant + History & Holidays,How Many Films Were Made In The Jaws Series ,"4 (Jaws 1,2,3 & Jaws The Revenge) ",Music,"Which Irish Singer Got To No.3 With ""Tarzan Boy"" In 1985",Baltimora,Toys & Games,How many discs does each player have to start with in draughts (checkers),12 +General,What is a female cat,Queen,Food & Drink,Who released the following album 'Larks tongue in aspic' ,King Crimson ,Science & Nature,What is the abbreviation for trinitrotoluene,Tnt +General,What sport was called The Royal Sport,Cock Fighting, Geography,Kingston is the capital of ______?,Jamaica,General,What type of cloud is a thundercloud,Cumulonimbus +Science & Nature,What Was The Name Of The First Plastic Ever Developed ,Bakelite ,General,"On get smart, who was seldom called by his name thaddeus",Chief, History & Holidays,Good King Wenceslas was a 12th Century king of which country ,Bohemia  + History & Holidays,What was George A Custer's horses' name?,Comanche,General,"On what river is London, England",Thames,Sports & Leisure,Which Irishman was World Snooker Champion in 1997? ,Ken Doherty  +Art & Literature,Which Author's Father Was Imprisoned For Debt ,Charles Dickens ,General,What was the real name of the monk known as Rasputin,Grigori efimovich,General,What is a group of this animal called: Mallard,Sord +General,Which modern artist created the Mother and Child Divided,Damien Hurst,General,"Which group released the album ""Urban Hymns"" in 1997",The verve,General,"Who wrote Principia Mathernatica. with Alfred North Whitehead, between 1910 and 1913",Bertrand russell +Music,As Of 2006 Who Are The Only Brother And Sister To Have Recorded Separate UK Number One Hit Singles?,Daniel & Natasha Bedingfield,Food & Drink,How would you say 'house wine' in 'Spanish' ,Vino de la casa ,Sports & Leisure,Shirley Crabtree Was Better Known As Which Larger-Than-Life Character? ,Big Daddy  +General,"This weapon lends its name to a type of woman's shoe with a slender, tapered high-heel.",Stiletto,Art & Literature,Which British Painter Frequently Uses A Swimming Pool As A Theme ,David Hockney , History & Holidays,Which movie had a device known as a flux-capacitor? ,Back to the Future  +General,In what 1998 did film David Bowie play Pontius Pilot,Last Temptation of Christ,General,What is the international telephone code for the uk,44,General,Who is the presenter of the British TV quiz show 'Perfection'?,Nick Knowles +General,Which country are the current Olympic Rugby Champions 1924,USA,General,In which country did the first Christmas stamp appear in 1898,Canada,General,"In the Royal Navy, which rank is immediately above captain",Commodore +Music,"Which Classic 1960's Protest Includes The Lines ""Think Of All The Hate There Is In Red China, Then Take A Look Around At Selma, Alabama""",Eve Of The Destruction / Barry McGuire,General,What was the first USA team to win the Stanley Cup,Seattle Metropolitans,General,What is the largest animal that ever lived,Blue whale +Science & Nature,Which Is The Largest Member Of The Cat Family That Is Indigenous To The New World ,Jaguar ,General,Which English word comes from the French for candle,Chandelier,Geography,What Asian country has the highest population density,Singapore +General,What French designer introduced the sack dress in the '50's,Christian dior,General,The profits from the 1929 edition of Mein Kampf went to where,International Red Cross,General,How many ponies did the pony express use weekly,None - They used only horses +General,According to the cowboy encyclopaedia what is an orejano,Unbranded calf,General,Yellow alloy of copper and zinc,Brass,General,What is the fourth most common language in the USA,ASL American Sign Language +General,Who was Time Magazine woman of the year in 1936,Wallis simpson,General,What's the formula for the area of a rectangle,Length times width, History & Holidays,Which TV Family Adopted A Greyhound For Christmas? ,The Simpsons  +Art & Literature,Womens magazine launched by New York in the 70's.,Ms,General,The Archie Moore cup is competed for in which sport,Polo, Geography,What is the capital of the Italian province Lazio?,Rome +Science & Nature,What Colour Does Litmus Paper Turn When Dipped In Acid ,Red ,General,Pagophobia is the fear of,Ice frost,General,The inaugural London Marathon was run in which year?,1981 +General,What is ornamental work in silver or gold thread called?,Filigree,General,"In the first Scream, what was Stu's motive for the killings?",Peer Pressure,General,"In the Sherlock Holmes stories, of what subject was professor Moriarty a professor",Mathematics +General,"During pregnancy, how many times its normal size does the uterus expand",Five, History & Holidays,Who Was Marie Antoinette's Husband? ,Louis XVI ,General,Fuggles and Goldings are varieties of what,Hops +General,Which Movie Did 'Titanic' Overtake In The UK As The Highest Grossing Movie,The Full Monty,Art & Literature,Which Former Jockey Specialises In Novels Concerning Horse Racing ,Dick Francis ,Music,"Which Soul Singer Was Arrested, Charged And Fined For Putting On A Sexually Explicit Show Following A 1989 Gig In Georgia",Bobby Brown + History & Holidays,What country is Men Without Hats originally from? ,Canada ,General,Which planet has a moon called Charon,Pluto,Music,"Who Sang About A ""Wild Thing"" In 1966",The Troggs +General,The effect produced when sound is reflected back is known as a(n) _______.,Echo,Science & Nature, A group of foxes is called a __________,Skulk,Entertainment,Josie and the ________,Pussycats +General,Edradour is the smallest one in Scotland - what,Distillery,General,In what Australian state would you find Bendigo,Victoria,General,In the Bible who put Daniel in the lions den,King Darius +General,Which instrumental album launched the Virgin record label,Tubular bells,General,The metal gallium will melt in the heat of your ______,Hands,General,What kind of electricity is produced when you rub a balloon against your hair,Static electricity +Sports & Leisure,"In rugby, what is the equivalent of a hockey face-off?",Scrum,Science & Nature,Where are one quarter of the bones in the human body?,Feet,General,Who coined the term gossip column,Mark Twain +General,Dalmatian dogs originated in which country,Yugoslavia - Dalmatia,Entertainment,Who is the lead singer of 'The Doors'?,Jim Morrison,General,What is the last element - Alphabetically,Zirconium + History & Holidays,What was E.T.'s favorite candy? ,Recee's Pieces ,Entertainment,"He was known as the ""Elephant Man"".",Joseph Merrick,General,In ancient Sparta what was the penalty for bachelorhood,Can't watch women's gymnastics +Food & Drink,Company that was purveyor of Vodka to the Imperial Russian Court (1886_1917),Smirnoff,General,What recently independent country was formerly known as greenland?,Kalaalit nunaat,General,What is the literal translation of haute couture,High Sewing +General,The Mexican bearded and what are the only venomous lizards,Gila Monster,General,Which film stars Spencer Tracy as a war veteran with a mission to deliver a posthumous medal,Bad day at black rock,General, Hills and ridges composed of drifting sand are known as ________.,Dune + History & Holidays,"In 1647, the English parliament passed a law that: Let Prisoners spend Christmas At Home, Gave Santa Immunity To Burglary, Made Christmas An Official Holday, Made Christmas Illegal ",Made Christmas Illegal ,General,Tirana is the capital of ______,Albania,Sports & Leisure,What Occurs On The Glorious Twelth ,Start Of The Grouse Shooting Season  +General,There are more what in Italy than Canadians in Canada,Barbie Dolls,General,Parachutes were invented for what use,Fire escapes – people jump, Geography,What is the capital of Switzerland ?,Bern + Geography,Alba is the Celtic name for what country?,Scotland,Music,"Who Had A No.1 Album With ""Talk On Corners""",The Corrs, History & Holidays,He shot Lee Harvy Oswald.,Ruby +General,What TV role did actress Shirley Booth play,Hazel,General,What is the spanish word for 'fox',Zorro,General,What does AMSTRAD stand for,Alan Michael Sugar Trading +Entertainment,Porky Pig had a girlfriend named ________.,Petunia,General,Authority Music: What instrument does Ravi Shankar play,The Sitar,General,What is a group of bees,Swarm +Music,Who Was The First Member Of The Pop Group “Hear Say” To Leave The Band,Kim Marsh / Ryder,Music,Which Singer was “Lost in France” in 1976?,Bonnie Tyler,Music,According To His Song Whose Home Was Wherever he Laid His Hat,Paul Youngs +General,A(n) _________ is a person bound for a number of years to a master who undertakes to instruct him,Apprentice,Sports & Leisure,"Which Horse Won The Grand National In 1992, Winning With An Appropriate Name, As It Was Election Year? ",Party Politics ,General,In a museum La Crosse Kansas is Crandals Champion what,Barbed Wire museum +General,"In ballet, low, as in placement of arms.",En bas,General,In Japan they sell a last climax - what is it,Brand of tissues,General,Gina Hemphill carried the Olympic Torch at the opening ceremony of the 1984 Summer Olympic Games. Who was her famous grandfather,Jesse owens +General,What was Webster's adopted mom and dad's name,Poppadouupalus,General,In Connecticut it is specifically illegal to dispose of what,Used Razor Blades, History & Holidays,Who advocated planting peanuts and sweet potatoes to replace cotton and tobacco?,George Washington Carver +Science & Nature," Bird droppings are a chief export of Nauru, an island nation in the __________",Western pacific,General,"Football Team, san diego ______",Chargers,General,"Which F1 racing driver, after receiving the last rites, went on to win 2 world championships",Niki lauda +General,Babs Gorden is better know as what heroine,Batgirl,Music,The Song West End Girls Was A Number For Which Famous Pop Duo?,The Pet Shop Boys,General,Stenophobia is the fear of,Narrow things narrow places +General,Thomas Sweeny Was The Real Name Of Which Fictional TV Soap Character (Not The Actors Real Name),Sinbad (Brookside),General,What World First Occurred At Kill Devil Hill In 1903,First Powered Flight (Wright Brothers),General,Fox what is the capital of idaho,Boise +General,Racing who was the second male actor to refuse a best actor oscar,Marlon brando,General,Catherine the Great of Russia was born in which country,Poland,General,"Who reigns over Japan an emperor, a king or a queen",Emperor + History & Holidays,In which country do they eat 12 grapes as the clock strikes midnight (one each time the clock chimes) on New Year's Eve? ,Spain ,General,"What indian tribe is associated with ""the trail of tears""",Cherokee,General,"Where was first time, in 1894, the Mormons settled",Nevada +General,Joel Chandler Harris born December 1848 better known as who,Uncle Remus,General,What is the Capital of: Argentina,Buenos aires,General,In what Australian state would you find Cairns,Queensland +General,Who is the greatest boxer ever,Muhammid ali cassius clay,General,"During the u.s civil war, what did 22 union army blacks win",Medal of honour,General,Collective nouns - a siege of,Herons +General,What does a i stand for,Artificial intelligence,General,What river in Africa carries the most water,Congo - Zaire,General,What do the letters MG stand for on cars,Morris Garages +General,"Throat, foxing, & platform are parts of a(n) ________",Shoe,Sports & Leisure,What Is The Name Of Blackpool Football Club's Home Ground? ,Bloomfield Road ,General,What State was founded in 1948,Israel +General,Dilbert: How are Elbonian factories powered,Stationary bikes,Science & Nature, The smallest bird in the world is the Cuban bee __________. It is less than 2 inches long from tip of beak to tip of tail. It weighs 6/100ths of an ounce.,Hummingbird,General,Which motorway is seen as Manchester's equivalent to the M25?,M60 +General,What is the atomic number for palladium,Forty six,General,What is the chemical symbol for tin,Sn,Geography,Which element makes up 3.63% of the Earth's crust,Calcium +General,"This large mammal that is called a "" loxodonta africana "",and is also known as the (_____)",African elephant,General,To what is the chattanooga aquarium is devoted,Freshwater fish,General,Who invented the most common projection for world maps,Gerardus mercator +General,What does UNESCO stand for ?,"United Nation Educational, Scientific and Cultural Organization",General,Gladys knight and the _______,Pips,General,Which feature length Disney cartoon had a Scottish Terrier called Jock in it,Lady and the tramp +Music,Which Ex Rich Kid Played With Visage & Ultravox ?,Midge Ure,General,St Peter was the first Pope - Who was second,St Linus,General,"This military attack took place on Dec. 7, 1941",Pearl harbour +General,Every year there's a ton of it for every person in the world - what,Cement poured,General,What name did Vincent Van Gogh sign to his paintings,Vincent,Entertainment,"Mentor of Titan had two children in the Marvel comics, Thanos and ___?",Ero +General,Which detective lived in Cabot Cove Maine,Jessica Fletcher,General,All the Richard Hannay books got their titles from where,Bunyon's The Pilgrims Progress,General,How many engines are on a B52 bomber?,Eight +General,A ________ can eat only when its head is upside down,Flamingo,General,Fontana tv: barbara bel geddes won a emmy in 1980 as outstanding lead actress in what drama series,Dallas,General,Who introduces Channel 4's Time Team programme,Tony robinson +General,Emperor Claudius passed a law legalising what at banquets,Farting - for public health,General,"Thomas Keneally's Booker Prize-winning novel was made into a film, what was the novel called",Shindlers' ark,General,What is the aquatic nickname of Schubert's Piano Quintet in A,The trout quintet +Music,"Who Declared herself To Be ""Not That Kind Of Girl"" In 2001",Anastacia,General,Who starred in the film 'the dirty dozen',Lee marvin,General,Hagen what is a young lion called,Cub +General,Where would you find your natal cleft,Arse it’s the crack,Music,"What ""Size"" Was The Wonderstuff's First Uk Top Ten Hit",The Size Of A Cow,General,"What is this sign called ""&""",Ampersand + Geography,Approximately what percentage of the earth do the oceans cover?,71,General,What killed 23 people in Rostov Russia in July 1923,Giant Hailstones,General,"Who once famously quipped ""Do I Not like Orange""?",Graham Taylor +Geography,Where is mount kennedy ,Yukon ,Food & Drink,How many gallons of beer are there in a firkin? ,Nine ,Religion & Mythology,Who did the Norse god Odin have as handmaidens?,Valkyries +General,What family is a rhinocerous,Ungulate,Science & Nature,What Is a Woman's First Period Known As ,The Menarche ,Music,Which US Trio Had A 1996 Hit With The Song “Ready Or Not”,The Fugees +General,Fill in the blank: adding insult to____,Injury,General,"In which novel did we meet Jack, Ralph and asthmatic Piggy",Lord of the Flies, Geography,Ankara is the capital of ______?,Turkey +General,"What sport originally meant in French, look here",Tennis, History & Holidays,Which 1950s films took place in Altair-4 2280 ,The Forbidden Planet ,General,Which children's comic character lives at Bunkerton Castle?,Lord Snooty + History & Holidays,Who had a Christmas No 1 with 'There's No-One Quite Like Grandma'' ,St Winifred School Choir ,Music,Moby Is A Descendent Of Which Famous Author,Herman Melville,Music,Who wore the pink uniform on the cover of Sgt. Pepper's?,Ringo +Science & Nature,What Is A Female Goat Called? ,A Nanny ,Music,What Was Neil Diamonds First British Hit,Cracklin Rosie,General,All soldiers of every country do it - do what,Salute with right hand +General,"Who directed the films, Mrs Miniver, Ben Hur and Funny Girl",William wyler,Food & Drink,What Is The World's Best-Selling Chocolate Bar? ,Kit Kat ,General,What colour is the most popular eye shadow of all time,Max Factor's Powder Blue +Food & Drink,What type of fish is in an omelette Arnold Bennett? ,Smoked haddock ,Music,How does the death of Roger Peterson feature in the history of rock and roll,Pilot Of Buddy Hollys Plane,General,In which musical work of 1925 would you hear the song Summertime,Porgy and bess +General,Mycophobia is the fear of,Mushrooms,General,What is name of the tubes that connect the ear & throat,Eustachian,Sports & Leisure,Baseball: The Cleveland ______?,Indians +General,Who rehashes her triumphs and tragedies in the book Who's Sorry Now,Connie,General,Who allegedly killed officer JD Tippett,Lee harvey oswald,General,"Before Cartoon Characters ""Tom & Jerry"" Were Made Famous Tom Was Called By Another Name What Was It",Jasper +Geography,What is the capital of El Salvador,San salvador,Science & Nature,What Is The Damaging Film Which Builds Up On Teeth ,Plaque , Geography,What is the basic unit of currency for Central African Republic ?,Franc +General,The chief food for more than half the people in the world is ____,Rice,General,Jack Nicklaus named his course after his UK favourite what,Muirfield,General,"Born in Urbino in 1483, which Italian artist, with Leonardo and Michelangelo, is considered one of the three Masters of the High Renaissance",Raphael +General,Who sang 'god told abraham kill me your son. abe said man you must be puttin' me on',Bob dylan,General,What is the white of an egg,Albumen, History & Holidays,"Who were Balthazar, Melchior and Caspar ",The 3 Wise Men  +General,Kinpaku-iri sake contains what unusual ingredient,Flakes of gold,General,"American inventor, engineer, & steamboat builder",John stevens,Religion & Mythology,What mythical Scottish town appears for one day every 100 years?,Brigadoon +General,The paraclete is another name for which Christian religious item,The Holy Spirit,Music,"Who Had A Hit In 1995 With ""Hot Stepper""",Ini Kamoze,People & Places,Which former England international footballer was ordered to give his ex-wife over a third of his earnings in a land mark divorce case in 2004? ,Ray Parlour  +General,What is spain's biggest source of income,Tourism,General,Which boxer holds the record for the longest-reign as World Heavyweight Champion,Joe louis,General,"What is the oldest country in all Europe, & the oldest republic in the world",San marino +General,Nadine Gordimer was awarded the Nobel Prize for literature in which year,1991,General,In which city was the first of the Rocky films shot,Philadelphia,Music,Which Former Daily Mail Television Critic Wrote The Lyrics For Les Miserables,Herbert Kretzmer +General,"In which country is Tobruk, scene of heavy fighting during WW2",Libya,Music,"Which Band Was Famous For ""Burning Down The House""",Talking Heads,General,"Fish was lead singer with which band, before going solo",Marillion +General,From which country did the astromomer Tycho Brahe come,Denmark,General,What fell into the pool in Caddyshack which caused a major exodus?,A Baby Ruth candy bar,Music,Who Wrote Rhapsody In Blue,George Gershwin +Science & Nature,A medicine that hastens the emptying of the bowels is called a ________.,Laxative,General,Who's mugshot number was BK4454813?,Hugh Grant, History & Holidays,"Which popular 60s film took place in and around 17 Cherry Tree Lane, London, in 1910 ? ",Mary Poppins  +General,"After The Fire made ""Der Kommissar"" popular, but which eighties musician performed it originally?",Falco, Geography,Which state is the Wolverine State?,Michigan,General,The first cricket one-day international was held between england and ______,Australia +General,What us state is sixth alphabetically,Colorado,General,According to Guinness book what's measured in Milli-Helens,Beauty from Helen of Troy,General,What colour does the prefix 'leuco-' refer to,White +General,David Cook became famous as who,David Essex, History & Holidays,In which year did the Titanic sink? ,1912 ,General,Common non domestic animal is not mentioned in the Bible,Rats +General,Which Contribution To World Music Was Invented By Robert Hope Jones?,The Jukebox,General,"Gean, Northern bird and Dwarf all types of what",Cherry Tree,Sports & Leisure,Which Swimming Stroke Is The First Leg Of The Medley Relay Race? ,Backstroke  +General,What is a group of this animal called: Beaver,Colony,General,"Which famous museum is in Paris, France?",Louvre,General,To which city did the Lord tell Jonah to go and denounce its citizens' wickedness,"He tried to flee to spain instead, and was swallowed by the ""whale""nineveh" +Food & Drink,How many gallons are there in a firkin? ,Nine ,Entertainment,Porky Pig had a girlfriend named _______.,Pet,General,"Who said ""I’ve watched a lot of baseball - on the radio""",Gerald Ford +General,Where is the only digital rolex watch in the world,Wimbledon centre court,General,Windhoek is the capital of ______,Namibia,General,What does am fm do for a living,Disc jockey +General,"What color top hat does jiminy cricket sport, in pinocchio",Blue,General,Richmond is the capital of ______,Virginia,General,What did the contestants in the Greek olympics wear,Nothing +General,In El Monte California its illegal for who/what to sleep in bathtub,Horse - unless ridden,General,To who was cher married,Sonny bono,General,Per Ardua Ad Astra Is The Motto Of Which Organisation,The RAF +Sports & Leisure,The Green Jacket is presented to the winner of which event? ,US Masters ,General,What's Mauritania's official language,French,General,Libreville is the capital of ______,Gabon +Food & Drink,Zima iz made in thiz United Statez city.,Memphis,Music,"Which American R&B/Pop Quintet Released The Album ""No Strings Attached"" In 2000",N Sync,Food & Drink,What is laver? ,Seaweed  +Food & Drink,The Finest Strands Of Pasta Are Known As What ,Vermicelli ,General,In 1477 the first advert in English offered what for sale,Prayer Book,General,What is the flower that stands for: acknowledgement,Canterbury bell +General,Which food product did Henry Cooper advertise in 1984,Shredded wheat, Geography,What is the capital of Hungary?,Budapest,General,A curved structure used to span an opening.,Arch +General,What does a heliologist study (the),Sun,General,Who was the first American Vice President to resign?,John C. Calhoun,General,What was the average age of united states soldiers in the vietnam war,Nineteen +General,Who wrote the song 'See My Baby Jive',Roy wood,General,Who worked in a factory making toilets for airplanes before he recorded 'aint no sunshine when shes gone',Bill withers, History & Holidays,"""What Did My True Love Give To Me On The """"Second"""" Day Of Christmas"" ",2 Turtle Doves  +General,"In Greek mythology, dione was the mother of ______",Aphrodite,General,Sobek was an Egyptian god - in what form is he seen,Crocodile,General,What is the only number in English that has letters in alpha order,Forty +General,"The alcohol found in wine, beer & liquor is known as grain alcohol or what",Ethanol,General,The character Captain Queeg appeared in which film,The caine mutiny,General,The word amnesia (forgetfulness) derives from what language,Greek +General,Who is Bibendum better known as?,The Michelin Man,General,What is the Capital of: Niue,Alofi,Sports & Leisure,What sport would you helicopter to the Bugaboos for?,Skiing +General,Willie What do table tennis players change after five points,Service,General,"Any of as many as 50,000 marine, freshwater, & terrestrial species of mollusk",Snail, Language,What is the language of Hungary?,Magyar +General,What is the branch of medicine dealing with curing by operative procedures,Surgery,Science & Nature,Who ate watercress to dissolve gravel and stones in the bladder?,North American Indians,Science & Nature,What did Wilhelm Roentgen discover in 1895?,X-rays + History & Holidays,What was Max Headroom's network number ,23 ,General,Name the TV show which featured an average housewife teamed up with a secret agent.,Scarecrow and Ms. King,General,Schlionophobia is the fear of,School +Music,"Precocious Brats 2000 Hit ""Big Girl"" Featured Which Comedy Duo",Kevin & Perry,General,What type of rocks result from the wastage of pre-existing rocks,Sedimentary,General,A game played with rackets and shuttlecock,Badminton +General,What is the Great Smoo,Scotland's largest cave,General,Which pop group had the most US No 1 singles in the 70s,Abba,General,Who founded the Hospice movement in Britain in 1960?,Cicely Saunders +General,The white house had a telephone before it had an indoor ______,Bathroom,Sports & Leisure,Who was the 1978 Wimbledon Women's Singles champ,Martina navratilova,General,What is the flower that stands for: resolution,Purple columbine +Science & Nature,What is the chemical symbol for gold,Au,Music,"With Whom Did Boys 2 Men Record The Single ""One Sweet Day"" In 1995",Mariah Carey,General,Ville Marie was the original name of where,Montreal +General,"In ""Spaceballs"" what does the bumper sticker say on the back of Lonestar's ship?",I love Uranus,General,What place is called Rapa-nui by its native inhabitants,Easter Island,General,Which English Benedictine monk is known as the Apostle of Germany,St boniface +General,Who painted 'Christ in the Carpenter Shop'?,John Everett Millais,General,What was the name of Norse God Thor's hammer,Mjolnir, History & Holidays,In which country was Greenpeace founded in 1971? ,Canada  +General,What is a male swine called (giggle no ex boyfriends names___),Boar,General,What's the biggest living bird,Ostrich,General,Which former child star made his debut on the London stage in October 2000,Macaulay culkin +General,"Which Panamanian boxer's nickname, translated into English, means ""hands of stone"" or ""stone fists""",Roberto duran,Entertainment,Who did Larry Hagman portray in the TV series 'Dallas'?,J.R. Ewing,Science & Nature,What Is A Discovery? ,An Apple  +General,What is the largest land bird in europe,Bustard,General,What sport sees stones thrown at a house,Curling,General,What is a german 'alsatian',German shepherd +General,On A Movie Set What Is The Primary Job Of The Foley Artist,To Add Sound Effects,General,Ornithophobia is a fear of ______,Birds,General,Suez is at one end of the Suez canal what is at the other,Port Said +General,What is the Capital of: Latvia,Riga,General,Politicophobia is a fear of ______,Politicians,General,What color is the blood of an octopus,Pale bluish-green bluish green +Religion & Mythology,On which mountain did Moses receive the Ten Commandments?,Sinai,General,From what were balloons originally made,Animal bladders,General,What is the colour of lobster's blood,Blue +General,Where does the island of Surtesy lie,Off iceland,General,What's a 10-20 to a police officer,Location, Geography,What is the basic unit of currency for Tunisia ?,Dinar + History & Holidays,In the Bible who sent out a dove to find land ,Noah ,General,What country has three capital cities Admin Legislate Judicial,South Africa,General,"What Was Designed In The 1930's By The Englsi Architect ""Giles Gilbert Scott"" And Dubbed The K6",Red Telephone Box +Music,"Whose Version Of ""My Way"" Had A Perhaps Poignant Release Late In 1977",Elvis Presley,General,What completes this well known saying..'An Englishman's home,Is his castle,General,"What ""motowner"" was shot to death on april fool's day",Marvin gaye +General,Who owns corfu,Greece,Entertainment,Who released the double album 'Goodbye Yellow Brick Road' in 1973?,Elton John,General,What part of a person's body must be clutched to feel the 'biceps',Upper arm +General,The President of Gabon banned the use of what word in country,Pygmy,Science & Nature,What is the second hardest gem after diamond?,Sapphire,General,What African country gained independence in 1980,Zimbabwe +Science & Nature," The sea lion can swim 6,000 miles, stopping only to __________",Sleep, History & Holidays,Which racist organisation was formed in Tennessee in 1865?,Ku Klux Klan,General,What Does A Tegestologist collect?,Beer Mats + History & Holidays,Which Crime Did Sirhan Sirhan Say He Had No Knowledge Of But Was Later Executed For In 1969 ,Bobby Kennedy Assasination ,General,"In 'romeo and juliet', who says 'what must be must be'",Juliet,General,What was the second colour film to win best picture Oscar 1951,An American in Paris Gone with the wind 1 +Entertainment,Who sang Puff The Magic Dragon?,"Peter, Paul and Mary",Music,What Was Buddy Hollys First Hit Single,That'll Be The Day,General,"In 1983, who sang 'domo origato mr roboto'",Styx +General,What was a Pikelhaube used in WW I,Spiked German Helmet,General,A _______ is a pact between a secular authority & the church.,Concordat,General,What is the search for the existence of ghosts,Eidology +General,"What is the nickname for New Orleans, la.",Crescent city,General,What is the longest venomous snake,King cobra,General,What is the most common surname for Motel owners in the US,Patel +General,"Who composed the opera ""Boris Godunov""",Moussorgsky,General,"Who, with William Gaunt and Stuart Damon, played the lead parts in television's The Champions",Alexandra bastedo,General,Which Element's Name Is Derived From The Greek For “Lazy”,Argon +General,The Bear and the Dragon is the last novel by which writer,Tom clancey,General,Which musical includes the Barbara Dickson/Elaine Page song I Know Him So Well,Chess,General,Professor Kelp transformed into who,Buddy Love – Jerry Lewis Nutty Professor +Music,"With Their Album ""That Compact Disc By"" Who Are Crofts, Harry And Williams",Oceanic,General,Where did Simon & Simon take place?,San Diego,General,"Briton's say 'tarmac', americans say ________",Runway +General,“ St Georges Day ” Who We All Know Is Patron Saint Of England But Where Was He Born.,Turkey,Science & Nature," The Mola Mola, or Ocean Sunfish, lays up to __________ eggs at one time.",5000000,General,The US had 5% world population and 70% of worlds what,Lawyers +General,What countries highest award is The Order of the Elephant,Denmark,General,Which variety of cheese was invented by John Jossi,Brick - rhymes with dick,General,What was the name of the policeman in Enid Blyton's 'Noddy'?,PC Plod +General,There are over 130000 species of what on earth,Butterflies,General,What flower is the national symbol of India,Lotus,General,What is a group of this animal called: Hawk,Cast kettle +General,And which animals penis is prehensile,Dolphins,General,Piercing of holes other than the ear lobes,Body piercing,General,What russian word 'restructuring',Perestroika +General,What do the italians call munich,Monaco of bavaria,General,Collective nouns - A family of what,Sardines,General,What international airport is identified by the letters CCU,Calcutta +General,"In Greek mythology, what were Medusa, Stheno and Euryale collectively known as",The gorgons,General,The word electricity comes from the Greek word for what,Amber,General,What is the fear of beards known as,Pogonophobia +General,What country has the most elephants,Tanzania,Music,Who Is With Regard To Records Sold The Most Successful British Singer Of All Time,Cliff Richards,Food & Drink,"What country is the home of the relish called chutney? A=Korea, B= China, C= India, D= Japan ",C=India  +General,What does the abbreviation a.m. stand for?,Ante Meridian,General,What is the only state with an official state ship & hero,Connecticut,General,Who drew the comic 'the maxx',Sam keith +Sports & Leisure,What Colour flag indicates a fair throw in the shot put? ,White , Geography,The Nationalist Chinese occupy this island.,Taiwan,General,In the olden days what would you put in a large Bosom,Clothes - it’s a chest +General,Ashton In Northamtonshire Holds An Annual World Sporting Event In What Sport,Conkers,General,"On irc, what is a/s/l","Age, sex, location",General,The average size of what is a grain of sand,Meteor +General,"Where was the Armistice, between the Allies and Germany, signed on the 11th November 1918",Compiegne,General,Who sells more cars than Ford Chrysler Chevrolet and Buick,Matchbox toys,General,"In which Ealing Film Comedy do Alec Guinness, Herbert Lom and Peter Sellers try and fail to commit a major crime",The ladykillers +Music,"Glen Miller Received The First Ever Golden Record Award, For Which Single",Chattanooga Choo Choo, History & Holidays,Which enduring western made its television debut On Sep 12th 1959 and ran until Jan 17 1973? ,Bonanza ,Science & Nature,What planet is nearest the sun?,Mercury +General,The telephone country code 82 would connect you with,Korea,Science & Nature," When under extreme stress, such as when held in captivity, some octopuses will eat their own arms, which __________",Grow back,General,Zubin Mehta conducted who in concert,Three Tenors +Sports & Leisure,How many times did Ray Reardon win the snooker world championship? ,6 ,General,Approximately 3 million women in the USA have what,Tattoos,General,In Australia what is a Willy-Willy,Whirlwind +Music,What Do The Initials RCA Stand For,Radio Corporation Of America,General,What happened to the man who tried to hang himself over river,Rope broke – drowned in river,General,"Truck, Canton and Hoist are amongst others parts of what",Flag +General,"On the 1976 release, who 'wanted to fly like an eagle'",Steve miller band,Geography,In Which Ocean Is Greenland Located? ,Arctic ,General,"Real ______, kept below 55 degrees F, will sweat when brought too quickly to room temperature",Chocolate +Music,Which Female Trio Won 5 Grammy Awards In 2007?,The Dixie Chicks,General,Where were the 1964 winter Olympics held,Innsbruck Austria,Music,Number of Beatles conventions held each year worldwide,15 +General,In MASH what is Radars favourite drink,Grape Knee High,General,Which author wrote the novel Tom Jones,Henry fielding,Science & Nature,What breed of dog has an inability to bark ?,Basenji +General,In ancient Rome what was the tabularium,Hall of Public Records,General,What TV show with married couples and family life appealed to those over the age of 29?,Thirtysomething, History & Holidays,In what year did England's lease on Hong Kong expire,1997 +General,Deep freezing of bodies of people who have died of an incurable disease in the hope of a future cure,Cryonics,General,"Pine, Beach, Stone, Sable or American types of what creature",Martins,Entertainment,This Disney movie relies heavily on computer animation.,TRON + Geography,In what country is Mandalay?,Myanmar (formerly known as Burma), Geography,What is the capital of Portugal ?,Lisbon, Geography,What color are French Letter Boxes?,Yellow +General,What is the name used to describe materials that can be broken down by nature,Biogradable,Science & Nature,"In which organ is your ""hypothalmus"" located?",Brain,General,Which is the largest species of Tiger?,Siberian Tiger +General,Mother what is the real name of the painting 'whistler's mother',Arrangement in,Food & Drink,"Often eaten for breakfast, the egg comes from what barnyard animal?",Chicken,General,What is the fear of the moon known as,Selenophobia +General,A young what is called an Eyas,Hawk,General,In Alaska it is illegal to look at a moose from where,Window of any aircraft,General,"Musically, who described herself as 'The Last of the Red Hot Mommas'",Sophie tucker +General,What was the first song played on armed forces radio during operation desert shield,Rock the casbah,Entertainment,Who played Eddie in the pop-culture film 'The Rocky Horror Picture Show?,Meat Loaf,Geography,"As of 1990, __________________, Pennsylvania was the only U.S. city of the nation's largest 50 cities with a higher death rate than birth rate.",Pittsburgh +General,Who was the last king of france,Louis philippe,General,What is the highest commissioned rank in the Royal Navy,Admiral of the fleet,General,What kind of material is guipure,Lace +General,Which Country Has The Most Countries Bordering It,China (16),General,What is a 'rail',Bird,General,In 1925 at Windsor Bookies went on strike - against what,Betting Tax + History & Holidays,Which country did St Nicholas come from? ,Turkey ,Music,From which metal band was Dave Mustaine fired in 1982?,Metallica,Science & Nature,This organ is a small pouch that stores bile.,Gall bladder +General,Who won the 1990 Nobel Peace Prize,Gorbachev,General,"Mansard, Gambrel and Hip all types of what",Building roof,General,"When John F. Kennedy was president, who was his attorney general",Robert kennedy +General,Which Country Invented The Kilt,France,General,Who defended World heavyweight title twice on same night in 1906,Tommy Burns – both 1st Kos,General,What is studied in Aerology,Planet Mars +Sports & Leisure,Which Team Defeated The World Champions Argentina In The Opening Game Of 1990 Football World Cup? ,Cameroon ,Food & Drink,"Iceberg, Boston, and Bibb are types of _________.",Lettuce,Science & Nature,On what part of the body is an 'LTK procedure' performed?,Eyes +General,In Star Wars George Lucas modelled the Emperor on who,Richard Nixon,General,Fill in the blank: you get what you ____ for,Pay,General,What is ornamental work in silver or gold thread,Filigree +General,Did it my way what is the capital of kentucky,Frankfort,Science & Nature,What science does Professor Stephen Hawking study and teach?,Astrophysics,Music,The label is Bludgen Riffola Name the Band?,Def Leppard +Food & Drink,A Dining Chair With Arms Is Known As A What ,Carver ,General,What is formed when the Earth comes betwen the Sun and the Moon,Lunar eclipse,Music,What guitar company created the 'Flying V' guitar in the late 1950's,Gibson + History & Holidays,"If you were offered a 'spotted dick' after Christmas dinner, what could you expect? ",A Steamed Pudding ,Science & Nature,For what metal is 'Au' the chemical symbol?,Gold,General,What did peter minuit buy for the equivalent of 24 dollars,Manhattan island +Music,“The Man Who” Was The Second Album For Which Band?,Travis,General,1936 film started with world war and ended with space flight,Things to Come HG Wells,General,Which member of the Cabinet draws the largest salary,Lord chancellor +General,Alfred White was a famous author under which name,James Herriot,General,What makes the holes in Swiss cheese,Gas given off by bacteria,General,Jack lemmon's portrayal of businessman harry stoner in ___ wins him an oscar,Save the tiger +General,Hippophobia is a fear of ______,Horses,General,What name did the Romans give to Wales,Cambria,General,The Spinnaker Tower is a feature of which English city?,Portsmouth +General,In Biker Slang what is a Coupon,Traffic Ticket,General,Whose castle was camelot,King arthur,General,Gamaphobia Is The Fear Of What?,Marriage + History & Holidays,In which popular 1950s film is a character played by Audrey Hepburn given 24 hours to do as she pleases in the eternal city ? ,Roman Holiday , History & Holidays,In 1953 'Stalin Died'. Who ultimately succeeded him as the leader of the Soviet Union? ,Khrushchev. ,General,Who wrote the play Waiting for Godot in 1954,Samuel Beckett +General,Who signed the Magna Carta at Runnymead,No One - John sealed it illiterate,General,What is the white or yellowish substance obtained from the honeycomb of the bee,Beeswax,General,Which metallic element atomic no 83 soothes Gastric Ulcers,Bismuth +General,Name the Beatles first LP released in 1963,Please-please me,General,Name the test applied to computers to see if they can think,Turing test,General,The hop-low is the worlds smallest what,Mushroom +Food & Drink,How many pieces of bun are in a Mcdonald's Big Mac ,Three ,Music,"Paul McCartney wrote The Song ""Martha My Dear"". Who did he know who was named Martha?",His Dog,General,What vitamin found in carrots is good for the eyes,Vitamin a +General,"What goes with ruby, emerald & sapphire to make up the world's most valuable gems?",Diamond,Geography,Taiwan was known formerly as _________________,Formosa,General,Joplin Music: What band did Dion form in 1958,The Belmonts +General,What is the most extensively grown & eaten food,Wheat,General,"What is the lone ranger's ""real"" name",John reid,General,Who killed macbeth,Macduff +General,What instrument in an automobile measures distance travelled,Odometer,General,Alces Alces is the Latin name for what animal,Moose,General,Which Band Were Originally Called The Angel And The Snakes?,Blondie +General,What type of lettuce should be used in a classic Caesar Salad ?,Romaine,Entertainment,What did Sheryl Crow do before she became a singer?,Teach,General,In literature and films whose father was the Earl of Dorincourt,Little Lord Fauntleroy +Art & Literature,Who wrote 'The Rose Tattoo'?,Tennessee Williams,Music,By What Name Did Marion Elliot Become Famous Fronting The Band X-Ray Specs,Poly Styrene,General,What actress/singer once worked in a doughnut shop,Madonna +General,"In the television comedy series, what type of animal did Manuel think his pet rat Basil was",Siberian hamster, History & Holidays,What Canadian sketch comedy show helped launch John Candy's career? ,SCtv , Geography,Where is the Parthenon located?,Athens +Music,"Who Hat A Hit With ""Mah Na Mah Na""",Piero Umiliani,General,The Black Swan is native to which country,Australia,General,Space for bells in a church tower,Belfry +Sports & Leisure,What Sport Is Played By The Chicago Bulls ,BasketBall ,Food & Drink,"Booze Name: 1/2 oz. light rum, 1/2 oz. dark rum, 1 oz. orange, lime, pineapple juice",Pina colada,General,What is the proper name for the queen of spades,Palas +General,Formal invocation of the divine blessing upon people or things,Benediction,General,What song was on the B side of The Beatles We can work it out,Day Tripper,Music,What Kind Of Summer Did Bananarama Have In The 80's,Cruel +General,What was Hebe the goddess of,Youth,General,Old superstition Wearing socks inside out protection from what,Witches,Religion & Mythology,Persephone was the Greek goddess of ______?,Spring +General,What is the Capital of: Senegal,Dakar,General,"A clip, shaped like a bar to keep a woman's hair in place is a _______.",Barrette,General,"Who said 'when power corrupts, poetry cleanses'",John f kennedy +General,"The fastest animal an earth, if you try to get them to chase a mechanical rabbit, they will figure out to stop, face the other way & wait for the rabbit to lap",Cheetah,General,"What happened in tulbach on september 29, 1969",Earthquake,Sports & Leisure,Who Was The Fist Footballer To Receive A Knighthood ,Sir Stanley Matthews  +General,What was Queen Victoria's first name,Alexandria,General,Brigham Young was a pioneer of which institution,The mormon church,General,In 1961 U.S. breaks relations with,Cuba +Sports & Leisure,Name The Youngest And Oldest Members Of England's 2006 FIFA World Cup Squad ,Theo Walcott & David James ,General,What is the Capital of: Angola,Luanda,General,Mountain ranges like Sierra Nevada what does Nevada mean,Snow Topped +General,"A style that emerged in the 1970s characterized by references to and evocations of past architectural styles, particularly the classical tradition. It is frequently colorful and wittily ornamentive.",Postmodernism,General,Which writer of the 17th and 18th Centuries first penned the line 'For fools rush in where angels fear to tread.',Alexander pope,General,What was the name of Magnum PI's suave superspy alterego?,Sebastian Sabre +Science & Nature,What Was The Tomato's Original Name? ,Love Apple ,General,What technique records and reproduces three-dimmensional images using light from a laser but without the need for cameras or lenses,Holography,Food & Drink,The top fast food chain in the USA by revenue in 1995 was McDonalds. Which was second? ,Kentucky Fried Chicken  +General,"Barrier reef What famous writer was born May 25, 1803",Ralph waldo emerson,General,Which Rugby Union team play at Franklin Gardens?,Northampton,Food & Drink,What Does Chilli Con Carne Mean ,Spanish For Meat With Chilli Powder  +General,Who was the only man to knock out Muhammad Ali in a heavyweight title fight,Larry holmes,General,World what is ground being 'rested' for a season,Fallow,General,What is the chemical symbol for antimony,Sb +Sports & Leisure,On What Sport Was john Arlott A Noted Commentator ,Cricket ,General,"Which Frenchman published ""Centuries"" in 1555 containing rhyming prophesies up to the year 3797",Nostradamus,General,In around what year was Joan of Arc born,1412 +Geography,Of Which Settlement Did Local People Invite Sir Francis Drake To Become King ,San Francisco ,General,What is the second largest bone in the foot,Talus,General,Hitchhiker's Guide: What race writes the third worst poetry in the Universe,Vogons +General,What is a Dwarf Goby,Worlds smallest true fish,General,Who helped George Harrison produce 'My Sweet Lord',Phil spector,General,The spiral galaxy nearest ours is the ________,Andromeda +General,The fable The Hare and the Tortoise - what animal judges race,The Fox,General,"The four throwing events at the olympics are shotput, discus, javelin and ______",Hammer throw,Music,What Was The Name Of Junior Walkers Backing Group,The All Stars +General,Where were arabic numerals first used,India,Sports & Leisure,Which Country Does The Athlete Merlene Ottey Represent ,Jamaica ,Music,"What Did The Initials Stand For In The Name Of 1980's German Band ""DAF""",Deutsche Amerikanische Freundschaft + History & Holidays,What company made PacMan? ,Bally Midway ,General,Army officer ranked below major-general,Brigadier,General,"Racing Music Artists: Who did ""Unchained"" in 1981",Van halen +General,Which company produces the Sintra car,Vauxhal,Sports & Leisure,"She was ""Sports Illustrated's"" first female ""Sportsman of the Year"".",Billie Jean King,Technology & Video Games,Who invented Tetris ?,Alexi Pazhitnov +General,In the man from UNCLE what does UNCLE stand for,Utd. Net. Com. Law Enforcement,General,What elements name comes from the Greek meaning lazy,Argon,General,What country celebrates its National Day on 25th August?,Uruguay +General,Who's the lead singer of simply red,Mick hucknall,General,"What shuttle launch merited the time cover headline ""whew""",Discovery,General,What is a Googol?,The Largest Named Number +General,Two thirds of the worlds geysers are found where,Yellowstone Park,General,In Columbus Ohio its illegal for shops to sell what on Sundays,Cornflakes,General,Generation X Toys: Company that made Atari games like Kaboom and Megamania,Activision +Music,Name Any Three Of The Four Members Of The Ramones (PFE),"Joey, Dee Dee, Johnny, Tommy",General,What west coast U.S. state would you be in if you were honeymooning in Humptulips,Washington washington state,General,What word describes the scattering of the Jewish people around the world,Diaspora +General,In France what would you buy in a Boulangerie,Bread,Music,"Which Duo Did Bob Dylan Write ""Lay Lady Lay"" For Though They Never Recorded It",The Everly Brothers,General,From what country was the athlete Peter Snell,New zealand +General,What state is 'the gopher state',Minnesota,General,What is the common name for the 'pharynx',Throat,Science & Nature,These essential body cells do not contain nuclei?,Red Blood Cells +General,What job did Agatha Christies husband do,Archaeologist,General,Which authors first (unsuccessful) book was Inland Voyage,Robert Louis Stevenson,Sports & Leisure,Which Is The Only Non English Team To Have Won The FA Cup? ,Cardiff City  +Science & Nature,What is the longest venomous snake?,King cobra,General,What is a group of this animal called: Hound,Pack mute cry,General,Who called himself 8th wonder of world cos of his big dick,Charlie Chaplain +General,"Barbara, Carignan, Cinsaut, and Nebbilo are verities of what",Italian wine grapes,General,Which newspaper owner's name became an exclamation of surprise or disbelief,Gordon bennett,Sports & Leisure,Euro 2008 Gets Underway Later This Year But Can You Tell Me Either Of The Two Host Countries PFE ,Austria & Switzerland  +General,Nobody Does it Better was sung in which Bond film,The Spy Who Loved Me,General,Ursula is the name of the villain in which Disney film?,Little Mermaid,Music,What Is Elton Johns Real Name,Reginald Kenneth Dwight +General,What was the full name of Dr Henry Jekyll's alter ego,Edward Hyde,General,Jackdaws and magpies belong to which group of birds,Crows,General,A tayberry is a cross between which two fruits,Blackberry and raspberry raspberry and blackberry +General,OB is the international aircraft registration letters what country,Peru,Science & Nature,Which Is The Longest Bone In The Human Body ,"The Femur, Thighbone ",General,"Genus of annual and perennial herbs (Buttercup) containing about 20 species, grown for their showy flowers.",Adonis +General,What was the first version of microsoft windows to have networking capabilities,Windows for workgroups, Geography,What is the basic unit of currency for Tuvalu ?,Dollar,General,The buffalo weaver is the only bird to develop a false what,Penis to flash for mates +General,What did Charles Conrad of Apollo 12 become in November 1969,Third man on the moon,General,"What is this sign called ""*""?",Asterisk,General,Who created philip marlowe,Raymond chandler +General,The cob nut is the fruit of what tree,Hazel, History & Holidays,He ruled Rome when Christ was born.,Caesar augustus,General,In England it is illegal to drive a car without doing what,Sitting on front seat +Entertainment,Who patrols Gotham City?,Batman and Robin, History & Holidays,"Which computer manufacturer in 1984 advertised their new computer during the Super Bowl, and never re-used the commercial? ",Apple ,General,Planting of trees in urban or desert areas,Greening + History & Holidays,From which film do we get the song 'White Christmas'' ,Holiday Inn ,General,What does a spermologer collect,Trivia, Geography,Where is Euston Station?,London +General,Of which African country is Lilongwe the capital,Malawi,General,Whats the largest organ in the human body?,Skin,General,What is the name of the theme song for the film 'the highlander',Princes of +Geography,What is the capital of The Bahamas,Nassau,General,What name is given to the 4th Sunday in Lent,Refreshment sunday,General,What two countries is Andorra between,France & spain +Music,"Who Recorded The Albums ""Into The Fire"" & ""Waking Up The Neighbours""",Bryan Adams,General,In the Mohs scale of hardness what comes in at number eight,Topaz,General,Who is called the father of the h bomb,Edward teller +Science & Nature,Dense sea-water swamps along coasts of hot countries are called ________.,Mangroves,General,If you suffered from a luxating patella what wrong with you,Moving Kneecap,General,What hotel has been the target of the most take over bids,The Ritz - Paris + Geography,Which European country has the highest population density?,Monaco,General,Joan Sandra Molinsky became famous as who,Joan Rivers,General,Which country's troops sustained the greatest number of deaths in WWll,Russia +General,What ability has the silkworm moth lost through domestication,Flight,Music,What Was Mel B's First Solo Single,I Want You Back,Sports & Leisure,Which premiership football team are nicknamed 'The Toffees'' ,Everton  +General,Molysmophobia is the fear of,Dirt contamination,Science & Nature,How Many Teeth Does A Healthy Adult Usually Possess ,32 Teeth ,General,Who had hit records with 'You Love Us' and 'Kevin Carter',Manic street preachers +General,The Oedipus complex is the sexual love of a son for his mother. What is the equivalent complex of a daughter's sexual love for her father called,Electra complex,Music,"Who Was ""Horny, Horny, Horny""",Mousse T vs Hot n Juicy,Music,In Which Year Did & The Pacemakers Achieve Three No.1's,1963 +General,Who owns Weight Watchers?,Heinz Foods,General,"What famous actor is listed only as 'stud' in the credits for the 1970 film, 'myra breckinridge'",Tom selleck,General,Who was Canadian parliaments first Inuk member,Peter ittinuar +General,The RAF Red.Arrows have used British Aerospace Hawks since 1980. Which aircraft did they use prior to this date,Folland gnats,General,What are Swedish buns called?,Danishes,General,The tower over a castle draw bridge is a(n)___.,Barbican +Science & Nature,This fruit has its seeds on the outside.,Strawberry,General,How long is a paper anniversary,One year,General,Duffy: Quite a Year for Plums,Bailey white +Music,"In 1994, Who Returned To Hello Dolly For It's 30th Anniversary Revival",Carol Channing,General,"On Whose Gravestone Will You Find The Words ""My Jesus Mercy""",Al Capone,Science & Nature,How many times do your ribs move every year during breathing?,Five million +General,What was andrew jergens' profession,Lumberjack,General,What kind of animal was Black Beauty,Horse,General,Alfred Hitchcock's daughter appeared in Psycho - name her,Patricia Hitchcock +General,"Winner of four Oscars, which film featured the characters Joe Buck and Ratso",Midnight cowboy,General,"The small intestine is made up of the jejenum, the ileum & the ______",Duodenum,General,Who was imprisoned for faking howard hughes’s autobiography,Clifford irving +General,What european city is served by sheremetyevo and vnukova airports,Moscow,Science & Nature," With only a four_week gestation period, a cottontail __________ can produce 5 to 7 litters, and as many as 35 offspring per year.",Rabbit,General,Goitre is an enlargement of what gland,Thyroid + History & Holidays,What Colour Are Holly Berries ,Red ,Science & Nature,What is the fastest breed of dog after the greyhound?,Whippet,General,What countries people spend most private money on recreation,Taiwan's +Science & Nature,Which river made The Grand Canyon ?,Colorado,Sports & Leisure,Which animal is on top of rugby's Calcutta Cup? ,Elephant ,Religion & Mythology,Who is the greek equivalent of the roman god Venus,Aphrodite +General,"Who was bette davis' female co-star in hush, hush sweet charlotte",Olivia de,General,Who met in yalta in 1945,"Churchill, roosevelt and stalin", History & Holidays,"How old would Elvis have been on his birthday January 8, 2000? A=55, B=60, C=65 ",C=65  +General,"In painting, what is a Maulstick",Stick to steady the hand,General,What type of airplane did sky king use in sky king,Cessna,General,What kind of machines are floppy discs used in,Computers +General,What animals does a hippophobe fear,Horses, History & Holidays,"""Who Wrote The Christmas Story """"The Snowman""""?"" ",Raymond Briggs ,General,What foods name comes from the Tamil words for Pepper Water,Mulligatawny +General,Name the raven in George Orwell's Animal Farm,Moses,General,This statue was found on the Greek island of Melos in 1820,Venus de milo,General,Where is the lowest point in Europe,Caspian sea +General,What are formed by Orogeny,Mountains,General,Montana its illegal to have what in your cab without chaperone,A Sheep,General,What was the nickname given to marshall rommel of the german panzers,Desert +General,"Which novelist's latest chiller is called ""Bag of Bones""",Stephen king,General,Who created Woody Woodpecker,Walter Lantz, Geography,What country owns the island of Corfu?,Greece +Geography,In which county is Berwick-Upon-Tweed? ,Northumberland ,General,What are ceps morels and chantrelles,Mushrooms,General,Beatles were 1st UK group on Ed Sullivan who was second,The Searchers +General,In which field did Wayne Sleep achieve fame,Ballet, History & Holidays,Who was the second President of the USA? ,John Adams ,General,Thomas Chippendale mostly worked in what wood,Mahogany +Music,By what name is Beethoven's sixth symphony known?,The Pastoral Symphony,Music,Who Went For A Stroll Down Baker Street,Gerry Rafferty,Music,In 1969 Major Tom Appeared In Which Classic Track,Space Oddity +General,What does Stet mean to a printer,Let the Original stand,Science & Nature,If You Took The Number One Million & Converted It To Binary Form What Would It Equal ,64 ,General,And what is the least,Chocolate +General,Which Spanish painter has first exhibition at 16 - also 4 year blue,Pablo Picasso,General,For what was the acanthus plant used as a model,Corinthian columns,Music,Who Became Famous For Kung Fu Fighting,Carl Douglas +General,What is the capital of virginia,Richmond,General,In 1923 the BBC first broadcast what on the radio,Opera,General,What body organs did mae west say could be an asset if you hide them,Brains +General,In the Bible Cain built a city named after his son what,Enoch,General,What Is The Currency Of Seychelles,The Rupee,General,The word tragedy is Greek what does it literally mean,Goat Song - used to sacrifice goats + History & Holidays,Where Was The Roman's First Colony Based In England? ,Colchester ,Music,"Whose Record Label Was ""Fire"" By The Prodigy Released On",XL Recordings,General,Where did Cajun music originate,Louisiana +General,What was the name of Abraham Lincolns dog Stabbed to death,Fido,Geography,"What European country has ""Vaduz"" as its capital city",Liechtenstein,General,Which writer created the character Harriet Vane?,Dorothy L. Sayers +General,Heavy material stabilizing a ship,Ballast,General,Which states share Death Valley in the USA,California and nevada,General,Admiral Donitz succeeded Hitler in which year,1945 +General,"An adverb can modify a verb, an adjective, or ______",Adverb,Geography,Which Is The Largest Sea ,The South China Sea ,Science & Nature,One ragweed plant can release approximately how many grains of pollen?,One billion +Sports & Leisure,Which Grand Slam tennis event is played on a clay surface?,French Open,Music,"What Was The Royal Guardsmen's Follow Up To ""Snoopy Vs The Red Baron""",Return Of The Red Baron,Science & Nature,Who Made The Worlds First Over The Ocean Flight In A Heavier Than Aircraft ,Louis Bleriot  +Music,Son Of My Father Is Credited As Being The First Song Ever To Feature Which Instument,A Synthesizer,General,Who was the discoverer of the vaccine for polio,Jonas salk,Music,In 1996 Take That And Boyzone Both Had Number One Singles With Covers Of Songs By Which Band,The Bee Gees +General,Who wrote the book on which the Oscar winning film 'The Godfather' was based,Mario puzo,General,When did chuck berry's first rock and roll classic hit the charts,1957,Music,What Is The Name Of The Small Pipe With Finger Holes On The Set Of Bagpipes,The Chanter +General,In what 1967 film did gene hackman earn his first oscar nomination,Bonnie,General,"Complete the saying, 'He who praises everybody ____________'",Praises nobody,General,Of what was snow white's coffin made,Glass +Music,"Wrote the songs for ""Joseph and the Amazing Technicolor Dreamcoat""",Andrew lloyd webber,General,What was Lady Chatterlys first name,Constance,General,E H Shepherd illustrated which series of stories,Winnie the Pooh +General,"What Disney film features the song ""Give a little Whistle""",Pinocchio,Music,The Perfecto All Stars Are Better Known As Who,Paul Oakenfold & 808 State,General,"What molecule, known by three letters, controls heredity",Dna +General,Where in the US by law do you not have to pay taxes,An Indian Reservation,Science & Nature,Which Element Has The Chemical Symbol K? ,Potassium ,General,Duran What 1969 film did Glenn Campbell appear in,True grit +General,Which ultimately disgraced person was 'Surveyor of the Queen's Pictures' until 1972,Anthony blunt,Entertainment,"Of the new Supermen, this one was a villain",Cyborg,Music,"Which Vocal Group , Formed In 1954 As The Four Aims, Gave The Motown It's First Ever UK Number 1 Single In 1966",The Four Tops +General,Buckroe Beach Virginia illegal put what in someone's swimsuit,A Dead Fish,General,Which two Latin words nearly always abbreviated mean 'course of life',Curriculum vitae, History & Holidays,In which country do people wear masks to burn on December 31 to drive away bad luck? ,Ecuador  +General,One third of Taiwanese funeral processions include what,A stripper,General,"Which Pop Duo Consist Of The Members ""Graham Russell & Russell Hitchcock""",Air Supply,Sports & Leisure,"In which sport would you find the ""slapshot"".",Hockey +General,Which kitchen appliance was invented by fred waring,Blender,General,What was the eighth month in the ancient Roman Calendar,October,General,Their technical name are hydrometeors what's common name,Hailstones +General,How long must the first word in a Scrabble game be,2 letters,General,Authority charged with the disposition of legal actions involving children,Juvenile court,General,From what material are snooker balls made,Chrystallite +General,What salad vegetable would you be eating if you were having a 'French Breakfast',Radish,General,"Bistre, Sorrel and Vandyke are shades of which colour",Brown,General,Which comic book hero rode a horse called storm,Aquaman storm was a seahorse +General,Board game with pieces moved according to throw of dice,Backgammon,General,"""Earth has not anything to show more fair"" is the first line of a poem by whom",Wordworth,General,"Double diamond, Croquet and Rover terms in which sport/game",Croquet +General,In 1952 The Airfix Company Produced Their First Injection Moulded Construction Kit Of Which Famous Ship,The Golden Hind,General,In what game would you use a baguette,Boule - measuring / marking,General,When did the 'live aid' concerts take place,1985 +General,Evangeline Booth became the first woman general in what army,Salvation Army,General,What country borders libya on the east,Egypt, History & Holidays,In which Fox TV show did Johnny Dep play an undercover cop in high school? ,21 Jump Street  +General,Who was the first woman to be elected to the House of Commons?,Countess Markewicz,General,What's the smallest state (in area) west of the Mississippi,Hawaii,General,How long is a sesquicentennial,150 years +General,In 1820 what was taxed in Missouri,Bachelors,General,What's the study of gases in motion called,Aerodynamics, History & Holidays,"If you found a button in your Christmas pudding what would it symbolize Good luck, Poverty, Bachelor, Famous ",A Bachelor  +General,"Who wrote ""the life and opinions of tristram shandy, gent""",Laurence sterne,Music,Which Band Is Jarvis Cocker Associated With,Pulp,General,Where did Howdy Doody live?,Doodyville +General,Fuel especially methane poduced by fermentation of organic matter,Biogas,General,Which is sculptor Gutzon Borghun's most famous work,Mount rushmore,General,You have to run 360 feet if you hit a ______,Home run +General,In Which British City Did Body Shop Founder Annita Roddick Open Her Very First Store,Brighton,General,What is a group of swine,Sounder,Geography,What is the capital of China,Beijing +People & Places,Name Lloyd Bridges Two Sons ? ,Jeff & Beau ,General,If you landed at Balice airport where would you be,Cracow Poland,Art & Literature,"""Now is the winter of our discontent"" is a line from which Shakespearian play?",Richard III +General,Whose novels include 'The Joy Luck Club' and The Kitchen God's Wife,Amy tan,General,Phthisiophobia is the fear of,Tuberculosis,General,What is the sun's most abundant element,Hydrogen +Geography,What is a piece of shallow saltwater seperated from the deeper sea by coral or sand called? ,A Lagoon ,General,The Gulfs of Taranto and Corinth are inlets of which sea,Ionian sea,General,"Which company, formed by Cecil Rhodes in 1888, was an amalgamation of several diamond companies and is still going strong",De beers +General,Who had a No. 1 hit record in 1965 with 'I'm Alive',The hollies,General,The story above the cornice of a building.,Attic,Food & Drink,Name the only fruit named for its color.,Orange +General,Marley what did actor john wayne win from rudd weatherswax in a poker game,Lassie,General,Where does nessie live,Loch ness,General,"In The World Of Sport ""Spencer Gore"" Became The First Man To Do What In 1876",Win Wimbledon +General,Eric Arthur Blaire was the real name of which author,George Orwell,General,In the Flintstones what order did Fred and Barney belong to,Water Buffalos,General,Who drafted most of the american declaration of independence,Thomas +General,In what country does the cow tree grow - sap looks tastes milk,Venezuela,General,Which Russian Tsar died at Ekaterinburg in 1918,Nicholas ii, History & Holidays,What caused the computer in Electric Dreams to become alive? ,Spilled Champagne  +Entertainment,"Who sang the song ""Pretty Woman?""",Roy Orbison,General,Who was the first male to appear on the cover of Playboy in 1964?,Peter Sellers,General,"Who composed the songs ""The Old Folks at Home"" and ""Beautiful Dreamer""",Stephen foster +General,How many herrings are in a Warp ?,Four,General,Which boys name means - he who resembles God,Michael,General,What holiday is called Head of the World in the Jewish faith,Rosh Hashanah +General,Which country had The Dauphin as a ruler,France,General,In which film does Elvis Presley play an American soldier based in Germany,Gi blues,General,What did Francis Bacon call The Purest of Pleasures,The Garden +General,From Which Country Did The USA Buy The Virgin Islands In 1973,Denmark,General,What city is on the west end of lake ontario,Hamilton,General,What are a group of gulls called,Colony +Music,Who Had A Hit In 1985 With Walking In The Air,Aled Jones,General,In US only 8 % of women do it - but it changes their lives - what,Propose marriage to boyfriend,General,The Japanese art of growing miniature trees is called _____,Bonsai +General,How was Mark Feld better known in the 1970s when he had four number one hits with his band,Marc bolan,General,What is the capital of nevada,Carson city,General,Nebraska what is the mascot of the u.s naval academy,Goat + Geography,What is the capital of Macedonia ?,Skopje,General,What is Cape Kennedy now called,Cape canaveral,Art & Literature,"A decorative art movement that emerged in the late nineteenth century. Characterized by dense assymmetrical ornamentation in sinuos forms, it is often symbolic and of an erotic nature. ",Art noveau +General,In Norse mythology Odin traded an eye for What,Wisdom,General,"What century was the setting for tv's ""star trek""",23rd century,Geography,"Which Viking Chief , Banished From Iceland, Founded The Norse Colonies On Greenland ",Erik The Red  +Sports & Leisure,What is it called when a football team loses possession of the ball due to a misplay,Turnover,Food & Drink,How would you say 'house wine' in 'Italian' ,Vino della casa ,General,Which car manufacturer makes the Xantia model,Citroen +Sports & Leisure,Which Sport Is Often Termed As (The Sport Of Kings) ,Horse Racing ,General,Metrophobia is the fear of,Poetry, History & Holidays,Who Became President Of France After Charles De Gaulle? ,Georges Pompidou  +General,"Which city is directly northwest of Windsor, Ontario",Detroit,People & Places,Of What Were Lee Bowyer & Jonathan Woodgate Of Leads Accused ,A Racial Assault ,General,Tefnut was the Egyptian goddess of what,Rain +General,What is a gurdwara,A Sikh temple,Music,"Which Female Vocalist Sang With Take That On Their Single ""Relight My Fire""",Lulu,Science & Nature,What Fruit Is A Cantaloupe? ,Melon  +General,Sinophobia is the fear of,"Chinese, chinese culture",General,Queen Victoria was born in this year.,1819,General,What Do You Suffer From If You Have Diplophia,Double Vision +General,"Who wrote the book ""Computer Power & Human Reason""",Joseph weizenbaum,General,What are shaggy mane and pigs ear,Mushrooms or fungi,Sports & Leisure,What is Ian Botham's middle name? ,Terrence  +General,Hylephobia is the fear of,Materialism,General,After California what US state produces the most wine,New York, History & Holidays,What did Francis Crick and James Watson build a molecular model of in 1953? ,DNA  +General,According to his business card what job did Al Capone do,Sell second hand furniture,General,"What was discovered at Sutter's Mill, California in 1848",Gold,General,Churchill what was john lennon's first girlfriend's name,Thelma pickles +General,In 1964 A Power Cut Ruined The Opening Night Of Which Event,The Launch Of BBC2,Music,"What was the name of Van McCoy's orchestra on his ""The Hustle"" instrumental?",Soul City Symphony,General,East Pakistan is the former name of which modern republic,Bangladesh +Geography,What Was The Former Name Of Kampuchea ,Cambodia ,General,Who was the last king of Troy,Priam,General,Thomas Keneally wrote which book (Oscar winning film),Schindler's Ark +General,"What was the name of He-man's legless,wizard friend?",Orco,General,Which planet was discovered by William Herschel in 178l,Uranus,Sports & Leisure,Who Won The Womans Final At The Australian Open At The Weekend ,Maria Sharapova  +Sports & Leisure,Who Won BBC Sports Personality Of The Year In 2006 ,Zara Phillips ,General,Where is big ben,London,General,A talus is what geographical feature,Boulders fallen from mountain +Geography,On what river is the capital city of Canada,Ottawa,General,"During annual spring floods, this waterfall can become so loud as to break windows six miles away.",Victoria Falls, Geography,What is the basic unit of currency for Albania ?,Lek +Entertainment,"This female artist enjoyed sucess on both popular and country & western stations with such tunes as ""Let Me Be There"" and ""Have You Never Been Mellow.""",Olivia Newton-John,Music,"Which Motown Artist Wrote The Songs ""My Guy By Mary Wells"" & ""My Girl By The Temptations""",Smokey Robinson,General,What's a Coelacanth,Fish +General,What is the brightest star always in the Northern sky,Vega,Music,Also Reaching No.2 Which Technotronic Single Followed Pump Up The Jam,Get Up (Before The Night Is Over),General,Who printed 500 million stamps with elvis presley on the face,U.s postal +General,Name given to a fine woollen cloth,Barethea,Science & Nature,What Is The Offspring Of A Male Donkey And A Female Horse Called? ,A Mule ,General,King Thibaw - imprisoned by the British - last king of where,Burma +General,What are the stinging cells of a man of war called,Nematocysts,General,Who led the U.S. Air Force band in Europe during WWll,Glen miller,General,Thomas Holden was the first to top what list,FBI ten most wanted +General,Which South American country has borders only with Brazil and Argentina,Uruguay,General,What is the study of heredity called?,Genetics,General,What is a group of swans,Bevy +General,What is the most common sexual fantasy act,Oral Sex,General,In which game do players change service after five points,Table tennis,General,A petrologist studies what,Rocks history formation etc +General,The closure of which British nuclear re-processing plant was announced in 1998,Dounreay,General,"What is the name of the rabbit in the film, bambi",Thumper,General,Frederick Austerlitz became famous as who,Fred Astair +Sports & Leisure,Which West Indian Fast Bowler Died In 1999 Aged Just 41 ,Malcolm Marshall ,General,Cain & Abel were two of the sons of Adam & Eve. Name the third,Seth,General,Where can you find London bridge today,USA ( Arizona ) +General,The title of whose book translates as my struggle,Adolf Hitler,General,In Greek mythology which woman's name means all gifts,Pandora,General,What is the name for a woman who is superior of a convent in certain religious orders,Abbess +General,How many stars are there on the New Zealand flag?,Four,General,A gazette - is obvious what was a gazetta where word comes,A small Italian coin – pay for news,Sports & Leisure,Which sport do the Detroit Red Wings play? ,Ice Hockey  +General,How many lashes did jesus receive,39,General,In which sport is the Melbourne Cup awarded,Horse racing,General,What is the main ingredient in a Navarin stew,Mutton or Lamb +Music,Who Is Thomas Jones Woodward Better Known As,Tom Jones,General,Who was the Roman goddess of the hearth,Vesta,Entertainment,In what city does Fat Albert live?,Philadelphia +General,"In the oil industry, what do the letters VLCC mean",Very large crude carrier,General,Newspeak - Portable Handheld Communications Transcribers,A Pencil,General,What did Alfred Hormel invent,Spam +General,Eleutherophobia is a fear of _______.,Freedom,Sports & Leisure,Silly Point is a position in which sport? ,Cricket ,Music,"Which Megastar covered Don Mclean's ""American Pie""",Madonna + Geography,What country is Mt Etna in?,Italy,General,"In a Spanish bar, what are topaz?",Snacks,General,This word is used as the international radio distress call,Mayday +General,"When ocean tides are at their lowest, they are call what",Neap tides,General,In the Old Testament what is the first book of Moses,Genesis - first 5 all Moses books,General,Which element has the chemical symbol Sr; capital S lower-case r,Strontium +Science & Nature,Which plant is known for attracting hummingbirds?,Hibiscus,General,"In Greek Mythology, who stole fire from the Gods and brought it back to Earth hidden in a fennel stalk",Prometheus,General,Which two countries fought the Winter War of 1939?,Russia and Finland +General,"Who played the best friend of Sarah Jessica Parker in ""Girls Just Wanna Have Fun""?",Helen Hunt,Science & Nature,What Is Another Name For The White Poppy ,Opium Poppy ,General,"Who, when he won the Formula 1 World Championship in 1972, was the youngest driver to win it",Emerson fittipaldi +Music,"""The Obvious Child"" Was A 1990 Hit For Whom",Paul Simon,General,What name is given to a vertical bar dividing a window,Mullion,Music,Who released the album Auberge?,Chris Rea +Entertainment,Who played the first James Bond?,Sean Connery,General,"What is a combination of chopped and boiled pigs heads, feet, hearts and tongues, held together in a loaf shaped by gelatin",Head cheese,Sports & Leisure,On what type of surface are the tennis matches at Wimbledon played?,Grass +General,What canadian professional snooker player is nicknamed the grinder?,Cliff thorburn,General,Who is the patron saint of music,St Cecilia,General,Who wrote the book The Complete Angler in 1653,Isaac Walton +General,What is the square root of one quarter,One half,General,This word is used as the international radio distress call.,Mayday,General,In which sport would you hear the term shilling,Archery measure of arrows weight +General,September should be seventh month by name why is it ninth,Its 7th year used to start in March,General,What does an agrologist study,Soil,General,An Antarctic island was named after which cartoon character,Huckleberry Hound +General,In dry measure 16 pints make up a what,Peck,General,Who starred in the film The Man from Laramie,James stewart,Music,"Name One Of 2 Hits From Italian Singer ""Rita Pavone""",Heart & You Only You +Entertainment,How does Wonder Woman control her invisible airplane?,Mental powers,General,"What In The World Of Science Is The Russian ""Dmitri Mendelev"" Credited With Creating",The Periodic Table,General,What was Boucan that gave Buccaneers their name,Dried meat +General,What is the flower that stands for: cheerfulness under adversity,Chrysanthemum,General,Acetylsalicylic acid is the active ingredient in which well known drug,Aspirin,General,What nationality is toho film studios,Japanese +Science & Nature,How Many Legs Does A Wood Louse Possess? ,14 Legs ,General,"In mythology, which soil of Juno was considered to be the most Roman of gods",Mars,General,Who was the greek god of wine,Dionysus +Music,"Whose 1980 Debut Album Included The Song ""Thankfully Not Living In Yorkshire It Doesn't Apply""",Dexy's Midnight Runners,General,Promotion of friendly relations between countries,Bridge-building,Science & Nature,"Imperial, Buck, and Luna are types of _________.",Moth +General,In which Shakespeare. play does Lancelot Gobbo appear,The merchant of venice,General,Bert and ernie of 'sesame street' were named after bert and ernie in which frank capra film,It's a wonderful life,General,Who played guitar on 'Goodbye Yellow Brick Road',Davey johnstone +General,"Which Gloucestershire town, famous for its abbey, lies on the confluence of the Severn and Avon",Tewkesbury,General,"What company was formed by the Swede, Ingvar Kamprad",Ikea,General,Bwana means Sir in which language,Swahili +General,In which sport would you hear the term Intente,Jai Alai – Players Manager,General,What punishment was meted out to english poachers in the time of richard the lionheart,Castration,General,Spacephobia is the fear of,Outer space +General,Mithra is the persian god of ______,Light,General,What legendary monster does Seattle secretary Katie Martin believe to be the father of her furry faced son,Bigfoot,Music,Which DJ Is Associated With The Dance Act Clock,Stu Allen +General,Lampedusa is a small island lying between Malta and the coast of which North African country,Tunisia,General,What is the name of the body part which seperates the nostrils,Septum,Science & Nature,What does a Geiger counter measure? ,Radioactivity  +General,What UK gentleman's club first in 1891 to admit lady members,Reform club,General,What male name comes from the German meaning army rule,Harold,General,Whose rule is used to solve simultaneous linear equations by using determinants,Cramer +Science & Nature,"Nuclear membrane, cytoplasm, and nucleus are parts of a(n) __________.",Cell,General,What started in early 1900s to improve sales sports newspaper,Tour de France Le Petite Journal illustre,General,"Who said 'but, soft! what light through yonder window breaks'",Romeo +Music,The Single “Respect Yourself”  Was Released In The 1990’s By Which Die Hard Music Lover,Bruce Willis,General,Collective nouns a Toc of what,Capercailzie,Technology & Video Games,What was the first version of Microsoft Windows to have networking capabilities?,Windows for Workgroups +General,Ranidaphobia is a fear of ______,Frogs,General,If you were given a pot of vermicide what would you use it for,Killing worms,General,Sheriff' is actually a contraction of which words,Shire's Reeve +General,What record did Bill Haley and the Comets release in 1955,Rock around the clock,People & Places,By what name is Allen Konigsberg better known?,Woody Allen,Geography,What's the highest mountain in the 48 contiguous U.S. states,Mount Whitney +General,Where is 'old faithful',Yellowstone national park,General,Who Was The First Black Footballer To Score A Hat Tick Whilst Playing For England,Luther Blissett,General,Who played 'Banacek' in the 1970's TV series of the same name,George peppard + History & Holidays,Which Act Gave People Temporary Release From From Prison To Prevent Starvation? ,The Cat & Mouse Act , History & Holidays,Who was the last American president to sport facial hair?,Taft,General,Social phobia is the fear of,Being evaluated negatively +General,Who accused lancelot of sleeping with guinevere,Gawain, History & Holidays,"Which 1996 TV Christmas special, saw two of the stars dress as Batman and Robin, for a fancy dress party ",Only Fools & Horses ,Food & Drink,Thick spicy fish soup from Provence ,Bouillabaisse  +General,What is a group of this animal called: Caterpillar,Army,General,What dog shares his owner with Garfiled the Cat,Odie,General,Who invented dynamite,Alfred nobel +General,What does a Jingling Johnny do in Australia,Manually shear sheep,General,What is unusual about the nobody crab,Transparent appears No Body, History & Holidays,What Scary Movie Takes Place At The Overlook Hotel ,The Shining  +General,"Name the sitcom that featured Judd Hirsch, Andy Kaufman, Tony Danza and Danny Devito.",Taxi,General,Who painted The Naked Maja,Goya,General,On average man uses 2000 - woman 7000 what a day,Words +General,Which coin weighs exactly one Troy ounce,Krugerrand,General,"In Greek mythology, who judged a beauty contest on Mount Ida, between Hera, Athene and Aphrodite",Paris,Food & Drink,Which chicken dish is named after a battle in the Napoleonic wars? ,Chicken Marengo  +General,In Kentucky a man cannot purchase what without his wife,A Hat,Music,"Who Recorded The Album ""Eat To the Beat""",Blondie,Food & Drink,Which brewery makes 'Abbot Ale'? ,Greene King  +Science & Nature,What does a palaeontologist study ,Fossils ,General,A small naval escort vessel,Corvette,Music,"Who composed the overture, Peter and the Wolf?",Prokofiev +General,What is sometimes added to softdrinks to make them sweeter,Coal,General,What 'saturday night live' star played in the film 'stripes',Bill murray,General,In North Dakota it is illegal to sleep with what on,Your Shoes +General,What animal's milk is more than 54% fat,Humpback whale,Geography,Between Which 2 Countries Would You Find The Gulf Of Bothnia ,Sweden & Finland ,Music,Which Country Won The Eurovision Song Contest In 2007,Serbia +General,Which WW2 Norwegian collaborator's name became synonymous with treachery,Quisling,General,What is the fear of vegetables known as,Lachanophobia,Geography,__________ is home to the world's most remote weather station. Its Eureka weather station is 600 miles from the North Pole.,Canada +General,Domingo is Spanish for what,Sunday,General,The St. Moritz ski resort is in which European country,Switzerland,Science & Nature,Who is known as the father of genetics?,Gregor Mendel +Geography,What is the principal river of Ireland,Shannon,General,"What Animals Suffer From A Disease Called ""Strangles""",Horses,General,The pointed arch used in Gothic architecture.,Ogive +General,What are the surnames of the two men who determined the structure of DNA,Crick and watson,General,What scene is included for good luck in most of Spielberg films,A Shooting star,General,Where would you find Queen Maud Land,Antarctica +General,"ON Beverly Hills 90210,where did the Walshes live before they moved to Beverly Hills?",Minnesota,General,Mediolanum was the Roman name for what Italian city,Milan,Entertainment,What is the name of the Indian musical instrument made popular in western rock by The Beatles and Ravi Shankar?,Sitar +Music,"Who Had A Hit In 1996 With ""Wannabe""",The Spice Girls,Science & Nature,In Which City Was The First Heart Transplant Carried Out ,"Capetown, South Africa ",General,Who was Barbara Streisands first husband,Elliot Gould +General,What is the colour of the italian liqeuer galliano,Yellow,General,"Who wrote the christmas story, ""A Visit From St Nicholas""",Clement moore,General,Which motor company has the emblem of the prancing horse,Ferrari +Music,Who Had A Hit In 1984 With Dancing In The Streets,Shalamar,General,What is the part of a horse between fetlock and hoof called,Pastern,Sports & Leisure,In Which Sport Are The Ashes Trophy Set ,Cricket  +General,Who was the Christian missionary portrayed in Chariots of Fire,Eric Liddle,Sports & Leisure,In Which Sport Might You Encounter A Face Off ,Ice Hockey ,General,What U.S. state is completely surrounded by the Pacific Ocean,Hawaii +Music,"Who Had A Hit With The Song ""Don't Pay The Ferryman""",Chris De Burgh, Geography,In which country would you find the Yucatan Peninsula?,Mexico,General,What was the name of the pinball machine in the film 'tommy',Wizard +General,"What character did george burns play in 'oh, god'",God,General,Who wrote 'The Hitch-Hikers Guide to the Galaxy',Douglas adams,General,What is the astringent lotion obtained from North American trees,Witch hazel +Music,Name The Folk Group That Gerry Rafferty & Billy Connolly Were In,The Humbledums,General,What do toads do before mating,Sing,General,What is the medical term for the sudden and complete loss of memory,Amnesia +General,What is the flower that stands for: recall,Silver-leaved geranium,Religion & Mythology,What religion was founded by Siddhartha Gautama ?,Buddhism,General,"What was the name of captain geoffrey thorpe's pirate ship in ""the sea hawk",Albatross +General,"Prince George of England and his brother, Prince Albert Victor, witnessed this legendary ghost ship in 1881.",The Flying Dutchman,General,"In ""Alice in Wonderland"", who sings of ""Soup of the evening, wonderful soup""",Mock turtle,Music,Which Status Quo Album Cover Featured A Red Hot Stove Element As A Turntable,If You Cant Stand The Heat +General,Irrumation is what sexual practice,Fellatio - Blowjob,Sports & Leisure,In the world of sport what has the maximum dimensions of 60 metres by 30 metres? ,An Ice Skating Rink ,Science & Nature,What Did Prokofiev's Peter Catch & Take To The Zoo ,A Wolf  + History & Holidays,Israel occupied the West Bank. It belonged to _______.,Jordan,General,Skadi is the Norse Goddess of what,Winter and the Hunt,General,The average person spends 8 years of their life doing what,Being ill +General,In what Australian state would you find Maryborough,Queensland,Sports & Leisure,"In 1994, Who Collided With Damon Hill, Shattering His Chance Of Winning The World Title, At The Adelaide Grand Prix? ",Michael Schumacher ,General,"What ocean's area is 64,186,000 square miles",Pacific ocean +General,"On clothing, what does the symbol of a circle inside a square indicate",Can be tumble dried,General,What was Mother Theresa's Christian name - before she became a nun,Agnes,General,The Dolby sound system was introduced in which year,1967 +General,What type of bullet was named after an arsenal near Calcutta,Dum dum,General, The study of the composition of substances and the changes that they undergo is __________.,Chemistry,General,What country is also known as helvetia,Switzerland +General,What does the placement of a donkey's eyes enable it to see,All four feet at,General,Any material that hardens & becomes strongly adhesive after application in plastic form,Cement,General,What name is given to the unit of mass that weighs about 2.2046 pounds,Kilogram +Entertainment,Who was the original voice of Mickey Mouse,Walt disney,General,"What country is surrounded by brazil, argentina and bolivia",Paraguay,General,What stretch of water seperates Australia from Tasmania,Bass strait +General,In which Dickens novel does the character Fezziwig appear,Christmas Carol – Scrooges Boss,General,Who was the original Peeping Tom looking at,Lady Godiva, History & Holidays,According to the UK National Meteorological Office what year (prior to 2007) was the last White Christmas in Britain? ,2004  +General,As fit as a ______,Fiddle,Music,Name The Youthful Singer Who Headed The Teenagers,Frankie Lymon,General,Who released the double album 'yellow brick road' in 1973,Elton john +General,In Chicago its illegal to fish wearing what,Your Pyjamas, Geography,What is the largest island in the Philippines?,Luzon,General,A place name including worth e.g. Tamworth what's worth mean,Homestead +Science & Nature," A __________ focuses its eye by changing the angle of its head, not by changing the shape of the lens of the eye, as humans do.",Horse,General,Who was Mr. Wizard,Don Herbert,General,What is the flower that stands for: pensive beauty,Laburnum + Geography,What is the capital of Andorra ?,Andorra la Vella,General,In which country is Gander airport,Canada,General,Mitre Dovetail Jig and Hack are types of what,Saw +General,In mathematics which prefix refers to 10 to the power of minus 9,Atto,General,On which coast of australia is sydney,East,General,60's tv: who lived at 000 cemetery lane,Addams Family +General,Part of the human body can expand 20 times its normal size,Stomach 0.5 litres to 5 litres,Science & Nature,What are the units of measurement for Pressure ?,Pascal,General,In WW2 what kind of aircraft was a horsa,A glider +General,What is missing from a navel orange,Seeds,General,In what Dickens novel is there a case of spontaneous combustion,Bleak House,General,What is the flower that stands for: suspicion,Mushroom +General,What is a Merkin - There are two possible correct answers,Artificial Vagina – Pubic Wig,General,On which item of clothing are the letters YKK often found?,Zipper,Music,"Who Had A Revival With ""Nothing Has Been Proved"" From The Film Scandal",Dusty Springfield +General,What marlon brando film was widely banned,Last tango in paris,People & Places,Which singer won the Eurovision Song Contest in 1999? ,Charlotte Nilsson ,General,"Of what are an arc, radius and sector a part",Circle +General,What is the mascot of the US naval academy?,Goat,General,Which is the largest cathedral,St peter's, History & Holidays,Who was the first woman to be shot by the FBI? ,Bonnie Parker (of Bonnie and Clyde fame)  +General,What did david stirling found,Sas,Food & Drink,What Do You Wrap Beef In When Preparing Boeuf Wellington ,Pastry ,General,In astrology which heavenly body rules the sign of Cancer,The Moon +General,Who is the tallest telletubbie,Tinky Winky,General,What peninsula does Mexico occupy,Yucatan peninsula yucatan,General,What is the name of the metal discs in the rim of a tambourine,Jingles +General,In what precinct did Barney Miller work,12th,General,What Song Was The Last Christmas No.1 Of The Millenium (1999-2000),I Have A Dream / Seasons In The Sun,Music,Name the British Prime Minister linked with a Cream LP?,Disraeli ( Disraeli Gears) +Religion & Mythology,"He was condemned in Hades to forever push a boulder uphill, only for it to come rolling down before it reached the top.",Sisyphus,General,The branch of medicine dealing with curing by operative procedures is ________,Surgery,Music,Who brought fame to a disco lady in 1976?,Johnnie Taylor +General,What is a wallaby,Kangaroo, History & Holidays,Oklahoma has more man-made ___ that any other state ,Lakes , History & Holidays,Who Preceded Mao Tse-Tung? ,Chiang Kai-Chek  +General,U.S. captials New Hampshire,Concord,General,President fd roosevelt appeared in the 1943 comedy princess o'rourke as what,Himself, Geography,What is the largest city in China?,Shanghai +General,Who would use or what are Tittums,Bellringers changes,General,Of what material was the hairspring made in early watches,Pigs Hair,People & Places,Who Was Killed By Indians At Little Big Horn ,General Custer  +Art & Literature,Where Was El Grecho Born ,Crete ,General,What nationality was alexander graham bell,Scottish,General,"In food preparation, what term is used for the removal of peas from the pod, or the green calyx from strawberries",Hulling +General,Who was the 1958 Cha-Cha champion of Hong Kong,Bruce Lee,General,"Much loved by Scrabble players, what kind of creature is a zebu",Ox,Entertainment,Who played Queen Amidala in the latest 'Star Wars' film?,Natalie Portman +Geography,In what country is the highest point in South America,Argentina, History & Holidays,"Germany's WW I allies were Austria_Hungary, Bulgaria, and ________.",Turkey,General,Who was the science advisor on the first Star Trek film,Isaac Asimov +General,Swine is a Chinese brand name of which food,Chocolate,Food & Drink,By what name is the love apple better known? ,Tomato ,General,In 1897 James Henry Atkinson Unveiled To The World His Prototype To Which Everyday Device Still Popular Today,The Mouse Trap +Art & Literature,A movement of the 1960s and 1970s that emphasized the artistic idea over the art object. It attempted to free art from the confines of the gallery and the pedestal.,Conceptual art,General,In Milton's Paradise Lost what was the lowest point of Hell,Pandemonium (Hells capitol),Science & Nature," Reportedly, __________ mate for life.",Beavers +General,Zane Grey the western writer had what initial profession,Dentist,General,Who is the roman god of light and sky,Jupiter,General,He was R C Robinson in 1948 what name famous as now,Ray Charles +General,Morello Is Known As Which Variety Of Fruit,Cherry,Geography,In which country is the great victoria desert ,Australia ,General,What is the sum of 444 x 2 x 2 - 1700,76 +Music,What Is The Best Selling Album Of All Time Anywhere,Michael Jackson's Thriller,General,Every year 11000 Americans are injured doing what,Trying out bizarre sex positions,General,In France what is eau de vie,Brandy +General,Who died in a porsche spyder,James dean, History & Holidays,Who Released The 70's Album Entitled Abraxas ,Santana ,General,Which artist painted The Scream,Edvard Munch +General,What country's entire population was condemned to death by the Spanish Inquisition,The Netherlands,General,What song was originally 'good morning to you' before the words were changed and it was published in 1935,Happy birthday to you, Geography,In which continent would you find the Ob river ?,Asia +Geography,Which town in Strathclyde was created a new town in 1955? ,Cumbernauld ,General,"On which river does Melbourne, Australia, stand",Yarra, History & Holidays,What was the name of Garfield's vet? ,Liz  +General,What is absinthe traditionally flavoured with,Wormwood,General,In Belgium if you are eating waterzooi what is it,Creamy fish stew,General,The 1984 winter olympics were held at what site,Sarajevo +General,What Was Epic Records First Million Selling Single (Sold Around 6 Million),Careless Whisper,General,With what charge was Al Capone imprisoned,Tax evasion,Entertainment,Which beatle was the first to release a solo record?,Ringo Starr +Science & Nature, A __________ can fall from a 5_story building without injury.,Rat,General,What became a full Olympic sport in 1992,Badminton,Music,Which Band Did Billy Idol Front Before Going Solo,Generation X +Science & Nature,What is the study of the earth's physical divisions termed?,Geography,General,The first telephone call was made in what year,1876,Sports & Leisure,Who holds the NHL record for the most goals scored during a regular season?,Wayne Gretzky +Geography,Where Are The Atlas Mountains ,"Algeria, North Africa ", Geography,In which country would you find Ayers Rock ?,Australia,General,In Indiana what is illegal in winter,Bathing +General,Dove where is most of the vitamin c in fruits,Skin,General,7:25 pm In military time is how many hours,1925,Music,Which Blur Hit Album Shares Its Name With A Classic War Film?,The Great Escape +Sports & Leisure,Name the sport with Valentino Rossi as a leading competitor? ,Motorcycling ,General,1838 Los Angeles man needed a licence to do what to a woman,Serenade her,General,Death of body tissue usually caused by bad circulation,Gangrene +General,In what professional sport did bob hope participate as packy east,Boxing,Music,"Who Had A Hit In 1988 With The Song ""Je Ne Sais Pas Pourquoi""",Kylie Minogue,Music,Which Journalist Used To Sing With The Group Gaz & The Gonads,Gary Bushell +Music,What Was Jamaican Singer Barry Biggs Highest Entry Making No.3 In 1976,Sideshow,General,Which explorer discovered the island of Spitsbergen,Willem barents,General,What is the name of the Chairman of the European Central Bank,Wim duisenberg +General,"In egyptian mythology, who married two of her brothers",Cleopatra,General,At what temperature should 'rice wine' be served,Warm,General,In Christian tradition what Saint is the Virgin Mary's mother,Saint Ann + History & Holidays,What Was Anne Frank Famous For? ,Writing A Diary About Her Hide From The Germans In WW2 ,General,Which was the first credit card,Diners club,General,What is a group of ferrets,Business +General,Who owned the newspaper in Lou Grant - Nancy Marchand,Mrs Pyncheron,General,"What artist was nicknamed ""Jack the Dripper"" action painting",Jackson Pollock,General,Periodic table: what is np,Neptunium + History & Holidays,Who Released The 70's Album Entitled Pretzel Logic ,Steely Dan ,General,Who is the Roman Goddess of flocks and herds,Pales,General,Who is the roman counterpart of poseidon,Neptune +General,The small tree Camellia Sinensis provides us with which digestible product,Tea,General,Who buried the treasure on Treasure Island,Captain Flint,Mathematics,How many nickles are there in 2.25?,Forty five +General,The W H O recons there are 100 million what each day,Sex acts,General,Who took over as Fuhrer after Hitler's death till his arrest 1945,Admiral Karl Donitz, History & Holidays,What was the name of the man who pioneered the above method of freezing food? ,Clarence Birdseye  +General,"Who said ""If a lie is told in the Whitehouse Nixon gets a royalty""",Richard Nixon,General,Whose single season strikeout record did Nolan Ryan beat by one,Sandy koufax,General,Which Charles Dickens novel was brought to the screen by David Lean,Oliver twist + Geography,What is the capital of Idaho?,Boise,Music,"Which Was The First Glen Miller Record To Sell 1,000,000 Copies",Chattanooga Choo Choo,General,What is a bushranger,Australian bandit + History & Holidays,Who sang No More Heroes? ,The Stranglers ,General,In terms of hair what does FSH stand for,Follicle Stimulating Hormone,General,"Which former Spanish soldier founded the Society of Jesus, commonly known as the Jesuits",Ignatius loyola +Music,Which British Guitarist Fronted The Mahavishnu Orchestra,John McLaughlin, History & Holidays,What are you supposed to remove after kissing someone under the mistletoe? ,"A berry from the mistletoe, for luck ",General,What Nationality Was The 1 st Person To Build The First Bicycle Propelled By Pedals?,Scottish (Kirkpatrick Mcmillan) +Tech & Video Games,What entertainment product did Nintendo make before entering the video game business? ,Playing cards,Music,"In 1995 The Japanese Stock Market Temporarily Collapsed Following Mistranslated Reports That Ronald Reagan Was Undergoing Heart Surgery, Which Veteran British Singer Was Actually The Patient",Lonnie Donegan,General,Which poet wrote Jerusalem,William blake +Science & Nature," February 18, 1930 marks the first flight by a __________ in an airplane.",Cow,General,Eiffel designed the Eiffel tower - what was his first name,Gustave,General,The worst sporting disaster was in Hong Kong when the stands collapsed at a race course killing 604 people in what year,1918 +General,Which book by Peter Wright did Margaret Thatcher try to supress,Spycatcher,General,Any of various scientific recording devices designed to register a person's bodily responses to being questioned?,Polygraph,General,What is the flower that stands for: absence,Wormwood +Science & Nature,What is the chemical symbol for curium?,Cm,General,What is an extract of fermented and dried orchid pods,Vanilla,Science & Nature,"Where Might You Find Hell, Julius Caesar, Birmingham And Billy? ",On the Moon  +General,During which month is the longest day in the Southern hemisphere?,December,General,Name both families in Soap,Tates Campbells,Sports & Leisure,"In which sport is a ""hole-in-one"" possible?",Golf +General,Name only sports team to play professionally seven continents,Harlem Globetrotters,General,"What is the method of resolving disputes without resorting to law, strikes or lock-outs",Arbitration,Sports & Leisure,St Andrews is home to which English Football Club? ,Birmingham City  +General,What is the Capital of: Lithuania,Vilnius,General,Where is Albuquerque,New mexico,General,Name the smurf spin off characters that live underwater,Snorks +Toys & Games,"In which sport or game is the term ""rook"" used?",Chess,Entertainment,In what film did Whoopi Goldberg make her screen debut?,The Color Purple,General,"In March 1785, what future president succeeded Ben Franklin as minister to France",Thomas Jefferson +Food & Drink,What is the basic ingredient of the Indonesian dish Nasi Goreng? ,Rice ,General,Besides the stones which group had the longest touring career until the founder's death in 1995,Grateful dead,Music,Jethro Tull Had Two Top 10 Hits In 1969 Name One Of Them,Living In The Past / Sweet Dream + Geography,What is the longest river in Australia?,Darling,People & Places,What Did Al Capone Die From ,Syphilis ,General,With which organ does a snake hear,Tongue +General,What does a Zamboni do,Machine cleans ice hockey games,Science & Nature,In which organ is a pulmonary disease located?,Lung,General,"In Greek mythology, who was the son of peleus and thetis",Achilles +General,What percentage of the Earth's crust is salty,92,General,What Country's Currency Is A Baht?,Thailand,General,What is the fear of being bound or tied up known as,Merinthophobia +General,"Which American Blues singer originally recorded ""Got my Mojo Working""",Muddy waters,General,Who owned a sword called crocea mors or yellow death,Julius Caesar,General,The Eiffel Tower receives a fresh coat of paint every _ years,Seven +General,In Bavaria what is defined as a staple food,Beer,General,Churchill It’s a Riddle wrapped in a Mystery in an Enigma what,Russia,General,Scurvy is caused by a lack of ___.,Vitamin c +General,As what is Beethoven's piano sonata in C-sharp minor more commonly known,The moonlight sonata,General,RCMP stands for___,Royal canadian mounted police,General,What would you do with an Edzell blue,Eat it - it’s a potato +Food & Drink,What Is The Ginger Like Root Crop Associated With Thai Cooking ,Galangal ,General,Oikophobia is the fear of,Home surroundings house,General,Hierosolymitan is of Greek origin and pertains to what city,Jersualem +General,How many children did Adam & Eve have,Three,General,Limnophobia is the fear of,Lakes,General,Parorexia is the desire for what,Strange Foods + Geography,On what sea is the Crimea?,Black Sea,General,Patricia Holm is the girlfriend of what famous fictional character,Simon Templar The Saint,General,Who made a TV advertisment for Southern Maid Doughnuts,Elvis Presley + History & Holidays,"The three buildings of the Acropolis are the Propylaea, the Erectheum, and the _________.",Parthenon, Language,What does the Greek root word 'chrom' mean?,Color,General,Rabat is the capital of ______,Morocco +General,What does M.A.S.K. stand for?,Mobile Armoured Strike Kommand, Geography,What Central American country extends furthest north?,Belize,General,Which side did britain support in the us civil war,Confederacy +Science & Nature,Animals and plants which produce light are said to be:,Bioluminescent,General,"Skopelos, Thasos and Andros are all what",Greek islands,Food & Drink,Which sweet treat is often found at the Ambassador's parties? ,Ferrero Rocher  +General,Of what is petrology the study,Rocks,General,"Which composer's second symphony was called the Resurrection, his tenth was unfinished",Mahler,Geography,What u.s. national park contains gumbo limbo trail ,Everglades national park  +General,In France what animal is specially trained to sniff out truffles,Pig,General,Which U.S. State is known as the Pelican State,Louisiana,General,What mammals fly,Bats +Science & Nature,On what do honeybees have a type of hair?,Eyes,General,How Did Stuart Lockwood From Worcester Make International Headlines On 23rd August 1990,Yound Boy Held By Sadham,General,What type of singing was associated with St Gregory the Great,Gregorian chant +General,Which acid is found in yoghurt,Lactic,General,Killer tomatoes what album holds the world record for copies sold,Thriller,Sports & Leisure,England play their home rugby union matches at which venue? ,Twickenham  +General,What did marconi transmit across the atlantic,Radio signals,General,"Which number, when doubled, exceeds its half by nine",Six,General,Who was offered and rejected the role of Indiana Jones,Tom Selleck too busy +Music,How Many Of The Original 5 Members Of The Spice Girls Have Not Had A True Solo No.1 Single? (Disregard Any Duets Or Collaborations),"Mel B, Victoria (2)",General,"When two words are combined to form a single word (e.g., Motor + hotel = motel, breakfast + lunch = brunch) what is the new word called",A portmanteau,General,Who was born in Wattenscheid Germany November 11th 1920,James Bond +Geography,What Is The Capital Of Ethiopia Called ,Addis Ababa , History & Holidays,Which country did China invade on Christmas day 1950 ,Tibet ,General,What song was the beatles first attempt at social commentary,Nowhere man +General,Collective nouns - A Troubling of what,Goldfish,General,Which country has the most cellular phones per capita,Sweden,General,Going Undergound' was a hit for The Jam in which year,1980 +General,What is the name given to the dish of prunes wrapped in bacon,Devils on horseback,General,In Gustav Holsts planets suite which planet is the magician,Uranus,Geography,What is the smallest of the Central American countries,El salvador +People & Places,In 1955 Tim Berners Lee Invented something fantasic What was It ,World Wide Web ,People & Places,Where Might You Come Across Walloons ,Belgium ,General,"What piano man used to play for Bette Middler and then went on to his own career and made Hits like ""Mandy"" and ""Copacabana""",Barry manilow +General,"Giovannie in the opera 'don giovanni', who was leporello",Servant,General,"Which fictional detective appears in the novel ""Farewell My Lovely""",Philip marlowe,Music,Who Lost Her Heart To A Star Ship Trooper,Sarah BrightMan + Geography,What is the capital of Niger ?,Niamey,General,Asparagus is a member of which family,Lily,General,To the ancient Greeks what was an agora,Public meeting place / market (forum) +General,For what principal purpose are UHF radio waves used,Transmission of television signals,General,"In mythology, who was the wife of Jupiter",Juno,Science & Nature,What is the symbol for tin?,Sn +General,USSR saying No ugly women in world just shortage of what,Vodka,General,In which 20th century decade was the first angle-poise lamp sold,1930's,Food & Drink,Which Spirit Is Pimm's No 1 Based On? ,Gin  +General,What is the fear of mushrooms known as,Mycophobia,General,In Greek mythology who invented the lyre,Hermes, History & Holidays,What country was formerly known as Siam?,Thailand +General,What was the full name of the butler in soap - later spin off,Benson Dubois,Food & Drink,A tayberry is a cross between which two fruits ,Blackberry and Raspberry ,Music,"Which Female Singer Had A Hit With ""Crazy For You""",Madonna +General,Where would you find an ideo locator,Map - You are here arrow,General,"Which Celebrity Is The Founder Of ""The Spirit Foundation"" For The Aged Abused And Orphaned",Yoko Ono, History & Holidays,"Who was the man convicted of masterminding the 1969 LaBianca-Tate murders, later to become known as the Helter Skelter killings?",Charles Manson +General,What year was film introduced to replace glass in making photographic negatives,1891,General,What are tiny cracks in the glaze of pottery,Crackle, History & Holidays,Paul Anka's Puppy Love is written to what star? ,Annette Funicello  + History & Holidays,Which Apollo space mission put the first men on the moon ?,Apollo 11,Geography,Prior to 1935 what was Iran known as ,Persia ,Science & Nature, The normal body temperature of the __________ horse is 101 degrees Fahrenheit (38 degrees Celsius).,Clydesdale +Geography,Which City Was Formerly Called (New Amsterdam) ,New York , History & Holidays,Who was the first incumbent u.s. president to survive being shot ,Ronald reagan ,General,What was the wwii verbal code meaning message received or will comply,Roger +General,What is the study of insects,Entomology,General,Adolf Hitler was fascinated by _____,Hands,Sports & Leisure,"The Terms Acid, Blunt, Casper & Spine Are All Associated With Which Sport ",Skate Boarding  +General,According CIA what language is most common in Afghanistan,Persian,General,Who had a hit with Sylvia's Mother,Dr Hook,Sports & Leisure,What Is The Proper Oriental Name For The White Suits Often Used In Karate And Judo ,GI (Geeeeee)  +General,"Who recorded such songs as 'toys in the attic' and 'angel', and also did the music for the film 'armageddon'",Aerosmith,Art & Literature,"A flat board used by a painter to mix and hold colors, traditionally oblong, with a hole for the thumb; also, a range of colors used by a particular painter. ",Palette, History & Holidays,In the song 'We wish you a Merry Christmas' what pudding was asked for? ,Figgy Pudding  +Sports & Leisure,Which Football Team Plays At The Baseball Ground ,Derby County ,General,Soyuz was a soviet spacecraft but what's it literally mean,Union,General,Which art gallery would you visit to see Botticelli's ' Birth of Venus',"The uffizi, florence" +Music,From A Distance Was A Hit In 1990 & 1991 For Whom,Bette Midler,General,Gentlemen what hairstyle did chris evert sport in her first us open tennis champions,Pigtails,Geography,What's The Capital Of Zimbabwe ,Harare  +Science & Nature, __________ and short_tailed shrews get by on only two hours of sleep a day.,Elephants,General,A poem written to celebrate a wedding is called a(n) ___________,Mercury,General,What Domestic appliance was invented By Charles Strite?,The Toaster +General,What century saw the War of the Roses,The 15th century,Religion & Mythology,What is the holy book of Islam ?,Koran,Entertainment,Who is Melanie Griffith's mother?,Tippi Hedren +Food & Drink,Where was Budweiser first brewed ,St Louis ,Religion & Mythology,In what city does a certain church forbid burping or sneezing?,"Omaha, Nebraska",Music,Peter Cetera Was Originally A Member Of Which Band,Chicago +Music,"Who Recorded The Albums ""Non Stop Erotic Cabaret"" And The ""Art Of Falling Apart""",Soft Cell,General,What Is Manufactured From The Sapodilla Tree?,Chewing Gum,Music,"Name The Kiss Member Whose Solo Album Featured Cher, Bob Seger, Donna Summer, Helen Reddy & Janis Ian",Gene Simmons +General,What is the fear of glass known as,Nelophobia,General,A cat has how many muscles in each ear,32,General,Viscous black liquid produced in the destructive distillation of coal to make coke & gas,Coal tar +General,In what play do we follow Aaron a Moor beloved of Tamora,Titus Andronicus,General,What canadian horse won the 1964 kentucky derby,Northern dancer,General,What is the earth's layer just below the crust,Mantle + Geography,In what city is the Smithsonian Institute?,Washington,General,Which Country Currently Holds The Record For The Heaviest Rainfall Ever Recorded,India,Music,Whose first chart album was called Concerto For Group and Orchestra?,Deep Purple + History & Holidays,As what was Taiwan formerly known?,Formosa,General,The medical journal Practitioner 1923 said it will never happen?,Teaching of contraception,General,Which car company manufactured the leganza,Daewoo +General,This is the Southeast Asian method of dying fabric using wax to create designs?,Batik,General,What fish has its head at right angles to its body,Sea Horse,Sports & Leisure,Which Is Always The 3 rd Grand Slam Event In The Tennis Calendar? ,Wimbledon  +Music,"Jimmy Young, The Righteous Brothers, Robson & Jerome. Who Comes Next?",Gareth Gates,General,The Romans called it Cambria - what do we call it,Wales,General,What is the name of the fruit that looks like a hairy lychee,Rambutan +General,Label for person whose i.q is 110-120,Superior,General,"In Greek mythology, who abducted europa to crete",Zeus,General,Saturn is the only planet that is less dense than ______,Water +Entertainment,Where did George of the Jungle live?,Imgwee Gwee Valley,General,Which ship did charles darwin captain,Hms beagle,General,According To The 2000 Census What Is Now The Least Populated State In The United States Of America,Wyoming +General,"The wrist, or the wrist bones",Carpus,General,Scelerophibia is the fear of,Bad men burglars,Technology & Video Games,Who is the main character in the 'DeathQuest' series? ,Lucretzia +General,The three toed sloth only does it every 10 days - what,Defecate - or crap,General,Prophesied the Chalus the Greek - Die on day - did of what,Laughing cos he was not dead,General,With what is sulphur and charcoal mixed to make gunpowder,Saltpetre +General,Van gogh what does oestrogen protect against,Heart attacks,People & Places,Who Was The First American Chess Champion ? ,Bobby Fischer ,General,How many letters in the roman alphabet,26 +General,How does paella get its name,From cooking pan,Geography,What prison island was off the coast of French Guiana,Devil,General,Who kept the book 'curious george' in his suitcase,Forrest gump +General,What books original title was Murder in the Calais Coach,Murder on the Orient Express,General,"Who recorded the album ""wish you were here"" in 1975",Pink floyd,General,What is the maximum number of degrees in an acute angle,89 degrees +Entertainment,What is Dennis the Menace's last name?,Mitchell,Art & Literature,"What was the sequel to Louisa May Alcott's ""Little Women""?",Little Men,General,Who did Zola Budd trip in the 1984 Los Angeles Olympics?,Mary Decker +General,What science deals specifically with plant & animal life in the sea,Marine biology,General,What is Palermo the capital of,Sicily,Music,Which Word Appeared Three Times In The Title Of A 1973 Gary Glitter Single,"Love (I Love, You Love, Me Love)" +General,What date is the 'ides' of march,Fifteenth,General,What was extracted through the nasal passages of dead pharaohs,Brain,General,Who performed the first successful heart transplant,Christian barnard +General,What's a bee's home called,Hive,General,Leader of the Iroquois Indians same name as what car,Chief Pontiac,General,Frank Heyes 1923 on Sweet Kiss only jockey ever to do what,Win a race after death - heart failed during race +Entertainment,"The film ""Crouching Tiger, Hidden Dragon"" takes place in which dynasty (- the ' )",Ching, Language,Name the soda that is often confused with a drug.,Coke,General,Harold Leek became famous as who,Howard Keel +General,Which dancer died in 1927 strangled by scarf on car wheel,Isadora Duncan,General,In Breton Alabama there is a law against riding what down street,Motorboat,General,What is the most popular theater in Japan called,Kabuki +General,What lives in a holt,An Otter,General,What product put its logo on Dover cliffs - Act Parliament get off,Quaker Oats Man,General,What does pp on a music score mean,Very quietly +General,What animals name translate from Arabic as He who walks fast,Giraffe – from Xirapha,General,"Elizabethan women had three what modest, rascal and secret",Petticoats worn modest outside,General,Rangoon is the capital of ______,Burma +Science & Nature, __________ can clock an amazing 31 mph at full speed and cover about 3 times their body length per leap.,Kittens,General,When was the magnetic telegraph invented,1837,General,The body of an aircraft,Fuselage +General,Who was the son of Zeus and Maia - Gods Messenger,Hermes,General,"Round, flat, filbert or sword types / shapes of what tool",Paintbrush,Science & Nature," When a snail hatches from an egg, it is a miniature adult, shell and all. The shell grows with the snail, and the snail never leaves the __________",Shell +Music,Who had 60's hits with 'Glad All Over' & 'Bits & Pieces',Dave Clark Five, History & Holidays,Which comic strip animal devised by Otto Mesmer first appeared in 1931? ,Felix the Cat ,General,"Who did the new york jets sign to a 427,000 dollars contract on january 2, 1965",Joe namath +General,Who started his film career as Anglo Saxton type 2008 in 1930s,David Niven,General,What is the most mountainous country in europe,Switzerland,Geography,St. George's is the capital city of what island country,Grenada +General,What does a Coprophobe fear,Crap - Shit - Faeces,General,Who Released The Album Anarchy In The UK?,The Sex Pistols,General,Who created 'horton',Dr seuss + History & Holidays,"What was the maiden name of Wallis Simpson, for whom Edward VIII abdicated in 1936? ",Warfield ,General,What is the junction between two nerve cells called,Synapse,Music,"Who Wrote The Musical ""Theres No Business Like Show Business""",Irving Berlin +General,How many cells die in the human body every minute,300000000,General,In Nevada it is illegal to drive what on the highway,A Camel,General,How did the little match girl die,Froze to death froze +General,French farmers get help from this barnyard animal to dig out truffles.,Pig,General,"Throat, foxing, and platform are parts of a(n) ________.",Shoe,General,The 'windflower' is the common name for which flowering bulb,Anemone +General,What's most commonly used password on computer systems,Password,Technology & Video Games,The Hylians come from what game series? ,The Legend of Zelda,Music,Which Beatles Song Includes Mick Jagger & Eric claptojn On Backing Vocals,All You Need Is Love +General,What yummy snack is used in the construction of dynamite,Peanuts,General,Thomas Watson in 1943 there is a worlds market for 5 - what,Computers – he chairman of IBM,General,Which films are about the corleone family,The godfather +General,Frank Hornby Found Fame As The Founder Of Hornby Model Railways & Dinky Toys But Which Other Famous Very Toy Brand did He I(nvent And Patent In 1907,Mechano,General,What was the first Hanna-Barbera cartoon,Ruff and Reddy,General,"In alphabet radio code, what word is used for 'f'",Foxtrot +General,Autolycus - accomplished invisible thief Greek myth whose son,Hermes,Music,"Who Teamed Up With Queen To Record ""Under Pressure""",David Bowie, History & Holidays,What Was The Name Of The 1971 TV Show In Which Adam Faith Played Ronald Bird ,Budgie  +General,What hath God Wrought was first message sent by Who 1844,S Morse Washington to Baltimore,Science & Nature,Which Engine Powered Both The Spitfire & The Hurricane Fighters Of World War 2 ,Rolls Royce Merlin ,Music,The Vocal Group Ladysmith Black Mambazo Are From Which African Country,South African +General,"The film ""High Society"" was a musical remake of which 1940 film",The philadelphia story,General,What is the fear of stuttering known as,Psellismophobia,General,What is a cello's real name,Violincello +General,Who did Perseus turn into stone with the Gorgons head,Atlas,General,What movie stars morgan freeman as a pimp known as fast black,Streetsmart,Entertainment,"He starred in, ""City Lights"".",Charlie Chaplin +Music,"Wheels Cha Cha & ""March Of The Mods"" Were Hits For Which Society Band Leader",Joe Loss,General,On what continent would you find ash trees,North america, Geography,What is the capital of Saint Vincent ?,Kingstown +General,Who starred as Rocky Balboa,Sylvester Stallone,General,What's the most unusual official sporting event in China,Granade Throwing,General,What is Shakespeare's play 'Twelfth Night' also known as,What you will +Geography,What U.S. city is named after Saint Francis of Assisi,San francisco,Music,Which Labels First Crop Of Artists Included Elvis costello & Lene Lovivh,Stiff,General,Moving On Up' by M-people was released in what year,1993 +General,What was Michelangelo's only signed sculpture,The Pieta,General,"On Mr Ed, what was Wilbur's last name",Post,General,"Who was the nba, mvp in 1976, 1977 and 1980",Kareem abdul-jabbar +General,"Any free-moving liquid in outer space will form itself into a sphere, because of it's surface ______",Tension,General,What hormone is produced by the adrenal glands,Adrenaline,General,Samuel Pepys wife always slept with what in her hand,His Dick +General,In USA / Britain give finger - What do you show in Thailand,Sole of foot,General,Over which islands does the spanish flag fly,Canary islands,General,"What was the name of the white gang in ""west side story""",Jets +Food & Drink,What Does IPA Stand For ,India Pale Ale ,General,What do people use to propel kayaks,Paddles,Art & Literature,Who wrote the 'Myth' series?,Robert Asprin +General,What Was The First British Gameshow Adapted For Screening In The USA,The Krypton Factor,Geography,What American state is also called the 'Garden State'?,New Jersey, History & Holidays,Which groups first album was entitled 'Piper At the Gates Of Dawn'' ,Pink Floyd  +General,"Who was known as ""the sultan of swat""",Babe ruth,Science & Nature,What Colour Will Litmus Paper Turn When Dipped In Acid ,Red ,General,Where was Antonio Vivaldi born,Venice +General,What is the second month of the year,February,General,"Capital city of Arizona & seat of Maricopa County, located on the salt river in the south central part of the state",Phoenix,General,Who built the taj mahal,Shah jahan +General,Which Mediterranean island is named after the soldiers who were skilled in the use of slings,The balearics,General,Who was the defeated Socialist Prime Minister in the Spanish General Election of March 1996,Felipe gonzalez,Science & Nature,Which German Company Introduced The Interrupter Gear Enabling A Machine Gun To Fire Through An Aeroplanes Propeller ,Fokker  +Science & Nature,Which Bird Can Fly Backwards? ,The Humming Bird ,General,How many women know the formula of Coca Cola,None - not allowed,General,Who wrote The Day of the Jackal,Frederick forsyth +Geography,"There are approximately 100,000 glaciers in ______________",Alaska,General,"What does captain furillo's main squeeze, joyce davenport, do for a living",Assistant da,General,What kind of animal is a carnivore,A meat eater +General,"In cornish folklore, what is a bucca",Sea spirit,Toys & Games,"In a game of horseshoes, how many feet apart must the stakes be?",Forty,General,Bascule cantilever suspension all types of what,Bridge +General,In what city was the first US circus April 3rd 1793,Philadelphia,General,What is the fear of memories known as,Mnemophobia,General,"Which group sang the song ""Everything you want""?",Vertical Horizon +General,In which film did the Rolls Royce have the number plate AU1,Goldfinger,Science & Nature,Which Aircraft Manufactuer Produced The Spitfire ,Supermarine ,General,Gephydrophobia is a fear of _____,Bridges +General,In Italian pasta cusine what does al dente literally mean,To the teeth,General,What is the acronym for 'yet another hierarchical officious oracle',Yahoo,General,The wide wall built along the banks of rivers to stop flooding is a(n) _____.,Levee +General,What tiny vessel connects an artery with a vein?,Capillary,General,If you had aprosexia what would be impaired or reduced,Ability to study,General,Which 2006 'heist' movie featured Clive Owen as the architect of a bank robbery on a New York bank?,Inside Man +General,On which river is the city of Mandalay situated,Irrawaddy,Entertainment,"""Joy to the World"" was a hit in 1971 for what band with three lead vocalists?",Three Dog Night,General,What welsh singer used to work as a condom tester,Shirley Bassey + History & Holidays,Who does Jamie Lee Curtis play in 'Halloween 20 years later (H20) ,Kere Tate ,Art & Literature,"An eighteenth-century European style, originating in France. In reaction to the grandeur and massiveness of the baroque, it employed refined, elegant, highly decorative forms. ",Rococco,General,"What husband wife team starred in ""a turkey for the president""",Ronald and + Geography,What is the capital of Ukraine ?,Kiev,Entertainment,The theme tune for 'Monty Python's Flying Circus' was written by which composer?,John Philip Sousa,General,A pogonip is what type of weather condition,Heavy winter fog with ice crystals +General,What fashion designer is credited with the Bob hairstyle,Mary Quant,Geography,"Water is so scarce in the arid regions of _________ that, in the grasslands, the people never take baths, and sometimes must wash their faces in yak's milk.",China,Music,"How Are Jake Shears, Baby Daddy, Ana Matronic, Del Maquis & Paddy Boom Better Known",Scissor Sisters +General,Jeffery Archer wrote Kane and Abel what was the sequel called,The Prodigal Daughter,Art & Literature,What subject did 'Mr. Chips' teach?,Latin,General,"Name Shakespeare play Ariel, Miranda and Prospero appear",The Tempest +General,Graham Kerr became famous under what nickname,Galloping Gourmet,General,Who invented the clockwork radio,Trevor bayliss,General,Who was the last Indian chief to die in battle at Wounded Knee,Big Foot + Geography,What is the smallest Canadian province?,Prince Edward Island, Language,What do the initials 'VCR' stand for?,Video Cassette Recorder,General,Who is the Commonwealth Secretary General,Emeka anyaoku +General,Peridot is the birthstone for ______?,August,General,What was unusual about the drawings of artist Cesar Ducornet,Drawn with feet – he had no arms,General,What Does Dc Stand For In Washington DC,District Of Columbia +General,In 1896 1st modern Olympic Games officially opens in,Athens,General,Who directed the film of Ray Bradbury's Fahrenheit 451,Francois Trufeau,General,"Nicholson what u.s president's home is located in columbia, tennessee",James polk +General,"Before Finding Fame Under Another Name Which Girl Band Were Originally Known As ""The Colours""",The Bangles,General,"Who wrote the book ""Prisoner of Desire""",Jennifer blake,General,What is the name of the Boston baseball team based at Fenway Park,Boston red sox +General,What number does a heart denote on a Moroccan bank note,Five,General,Which is the longest bone in the body,Femur,Toys & Games,"What number is on the opposite side of the ""five"" on dice",Two +General,What shape are playing cards in india,Round,General,Where would you find a Mihrab,Mosque Niche show Mecca direction,General,In a Gynocracy - who rules,Women +General,"Whose epitaph reads ""He snatched the lightning from the skies and the sceptre from tyrants""",Benjamin franklin,General,Waving a yellow flag is the international signal for what,Infectious disease,Science & Nature,The Spinning Wheel Arrived In Europe In The 13th Century From Which Country Did It Supposedly Originate ,India  +Music,What Was The Gang Name Of John Travolta And His Cohorts In The Movie Grease,The T-Birds,Music,What 4 Letter Word Is Tattooed Of The Body Of Former Spice Girl Emma Bunton?,Baby,Science & Nature,What is the most widely accepted theory for the creation of the universe?,Big Bang +Geography,In which ocean or sea are the seychelles ,Indian ,General,What is the atomic number of cesium,55,General,What is san francisco's equivalent to sydney's 'city to surf' race,Bay to + History & Holidays,Who played Norman Bates in the 1998 remake of 'Psycho' ,Vince Vaughn ,General,Portuguese West Africa is now known as what,Angola,General,"What Part Of The Human Body Is Studied By A ""Myologist""",Muscles +General,In York its legal to kill a Scotsman (not Sunday) what weapon,Bow and Arrow,General,Which film won the best story and best song Oscars in 1969,Butch Cassidy and Sundance Kid,General,What is a group of widgeons,Company +General,Who was the first female monster to appear on film,Bride of Frankenstein,General,Who invented the compact disc or CD?,Philips,General,"What U.S. state gave the world Louis Armstrong, Fats Domino, Mahalia Jackson and Jelly Roll Morton (and, dare I say, Britney Spears)",Louisiana +Geography,Myanmar was known as _____________ until 1989.,Burma,General,Who won Euro song contest Save All Your Kisses For Me,Brotherhood of Man,General,What metal do fools mistake iron pyrites for,Gold + History & Holidays,How many years were between the creation of the Magna Carta and the American Declaration of Independence ?,561,General,From where to where did the first railway on the witwatersrand run,Johannesburg to springs,General,Weissmuller what do you call the hollow spaces in the bones surrounding your nose,Sinuses +Music,In The 9 Weeks Following John Lennons Death He Spent 7 Weeks At No.1 What Novelty Single Interrupted His Run At The Top Of The Charts,Theres No-One Quite Like Grandma,Music,"Born James Osterburg, Who Is Called The Godfather Of Punk",Iggy Pop,General,"North American Indian language family including languages of Alaska and north-western Canada, the Pacific coast and the south-western United States.",Athapascan + History & Holidays,"""In the TV show """"The Simpsons"""", who or what is Santa's Little Helper?"" ",Their Pet Dog ,General,How did Van Gogh dispose of his ear,Gave it to prostitute,Geography,Name the longest river in Asia.,Yangtze +General,"Who visited australia and new zealand, then surveyed the pacific coast of north america",Captain george vancouver,General,In Which Country Was Ice Cream Invented,China,General,A chinese imperial dragon has how many toes?,Five +Art & Literature,"A late-nineteenth-century French school of painting. It focused on transitory visual impressions, often painted directly from nature, with an emphasis on the changing effects of light and color. ",Impressionism,General,"In the series of 'Doctor' films based on the works of Richard Gordon, which actor played the part of Sir Lancelot Spratt?",James Robertson Justice,General,What two states in the U.S. do NOT observe daylight saving's time,Hawaii and +General,In which country is Dominion Day celebrated,Canada, History & Holidays,In Which City Did Chamberlain Make His(Wind Of Change) Speech In 1960? ,Cape Town ,Science & Nature,What aminal is the logo of the World Wildlife Fund?,Panda +Sports & Leisure,In What Sport Did Nolan Ryan Once Throw A Ball At A Record Speed Of 100.9 Miles Per Hour ,Baseball ,General,What is the flower that stands for: riches,Corn, History & Holidays,He killed Jesse James.,Ford +General,"The second longest suspension bridge in the world is the Ismit Bay, located where",Turkey,General,What became legal in 1901 in the UK,Boxing,General,Beirut is the capital of ______,Lebanon +Sports & Leisure,What Country Does Motor Racing's Jacques Villeneuve Come From? ,Canada ,General,"What continent is bounded on the north by the Timor Sea, The Arafura Sea & the Torres Straights",Australia,Music,"Whose Albums Include ""Steel Town"" & ""Peace In Our Time""",Big Country +Sports & Leisure,Which tennis star wore denim shorts during matches?,Andre Agassi,General,What does AIDS stand for,Acquired immune deficiency syndrome,Science & Nature,This bird lays its eggs in the nests of other birds.,Cuckoo +General,Melons are made up of approximately what percentage of water,90,General,In which sport are left handed people banned from playing,Polo,General,What is a group of this animal called: Bee,Swarm grist hive +General,Who killed his grandfather with a quoit at the Larrisan games,Perseus,Science & Nature,What animal lives in a form?,Hare,General,"Who wrote the thrillers, ""Sheba"" and ""Year of the Tiger""",Jack higgins +General,Which author wrote about the Cornish seaside boarding school Malory Towers?,Enid Blyton,General,Who is the first cartoon character to ever have been made into a balloon for a parade,Fruit,Science & Nature," Arctic terns found in North America and the Arctic migrate each year as far south as Antarctica and back, a round trip of over 18,000 miles. Theirs is probably the longest __________",Migratory flight +Food & Drink,If you were served crudit?s as a starter before your main meal what would you be eating? ,Sliced or shredded raw vegetables,General,What is a group of badgers,Cete,General,Where in a woman would you find the pisiform bone,Wrist +Food & Drink,Which red jelly is a traditional accompaniment to lamb? ,Redcurrant ,General,Who wrote the music to the film The Odessa File,Andrew Lloyd Webber,General,Who was the author of the story Pinnochio,Carlo collodi +General,What would you do with a naked lady,Plant it – its Colchinium,General,What Is The Worlds Oldest Known Vegetable?,The Garden Pea,Food & Drink,"Booze Name: Vodka, tomato juice, lemon, tabasco sauce, salt, pepper, celery salt.",Bloody mary +Music,"Their Most Famous Single Is ""Don't Fear"" Who Are They",Blue Oyster Cult,Music,"Whose Debut Album Was Titled ""Rafis Revenge""",Asiandubfoundation, Geography,What is the capital of Kiribati ?,Bairiki +General,"What composer boasted ""I could set a laundry list to music""",Rossini,Entertainment,Beethoven's Sixth Symphony shares it's popular name with a method of animal farming. What is it?,Pastoral,General,What plant has the largest seed,Coconut +Science & Nature,Which Disease Is Carried By The Tsetse Fly ,Sleeping Sickness , History & Holidays,In 1939 which pub game was banned in Glasgow for being too dangerous? ,Darts ,Music,Of Which Record Label Was Ahmet Ertegun The Co-Founder,Atlantic Records +Science & Nature,Where Is The Worlds Biggets Lathe ,"In Rosherville South Africa, It Can Machine Components Wighing 300 Tonnes ",General,What weapon was invented by Ernest Swinton used in 1916,Tank,Music,Donnie Wahlvberg Was A Member Of Which Band,New Kids On The Block +General,Name Mary Quant's shop that led the 60s fashion revolution,Bazaar,General,Between 1956 and 1960 which song made top 40 seven times,Mack the Knife Bobby Darin best,Food & Drink,What Milky Drink Is Often Drank On New Years Eve ,Egg Nog  + Geography,Which is the most populated state/territory in Australia?,New South Wales,General,A social dance of American origin in duple time.,Fox-trot,Sports & Leisure,How many points is the bullseye worth in outdoor archery? ,25  +Art & Literature,"Which English Writer Divided His Novels Into 3 Categories, Novels Of Character & Environment, Romances & Fantasies & Novels Of Ingenuity ",Thomas Hardy ,General,Whose only loss in 1983 was to kathy horvath,Martina navratilova,Music,Which Now Legendary Sports Chant Reached No.1 In 1996,Three Lions +General,What is the capital of nova scotia,Halifax,General,Something that containing both letters and numbers,Alphanumeric,General,Who replaced Johnny Carson as host on 'The Tonight Show' in the US,Jay leon +Geography,What is the capital of Panama,Panama city,General,For what purpose was the chow chow dog originally bred,As food or Chow,General,What is the name of the CIA agent played by Harrison Ford in Patriot Games and Clear and Present Danger,Jack ryan +General,What type of wine was Napoleons favourite,Burgundy Chambertin,General,"What is the Capital of: Man, Isle of",Douglas,General,On what hobby is most money spent,Gardening + History & Holidays,From which phrase is the word Halloween Derived ,All Hallows Eve ,Music,"Which Singer Was Born In Nutbush, Tennessee In 1939?",Tina Turner,General,Debby Boone sang the No 1 song of the 70s name it,You light up my life + History & Holidays,On what did Marley's ghostly face first appear to Scrooge in Dickens' A Christmas Carol ,A door knocker ,Music,Which Oxford Trio Began Their Career As The Jennifers,Supergrass,General,In which city is the worlds oldest tennis court from 1496,Paris +General,What Olympic event only takes place at 70 and 90 meters,Ski Jumping – official ramps,General,Which Politician Caused Outrage When They Famously Said That Northerners Die Of Ignorance And Crisps,Edwina Currie,General,"Who said 'The greater our knowledge increases, the more our ignorance unfolds' ?",John F. Kennedy +General,"Channels In 1978, whose music did Def Leppard like to cover in small clubs",Thin lizzy, Geography,Which of the 48 contiguous states extends farthest north?,Minnesota, History & Holidays,What Christian holiday is celebrated immediately after Halloween ,All Saints Day  +General,John Downland was 16th century composer for which instrument,The Lute,General,What does a chronometer measure,Time,Geography,What is the capital of Hungary,Budapest +General,Nazi secret police,Gestapo,General,Musical instrument is named from the Greek wooden sound,Xylophone,General,Where did the Pied Piper play,Hamlin +General,What is Joeys favourite food in Friends,Sandwiches,Geography,What Is The Largest Ocean On The Planet ,Pacific ,General,Who plays kevin arnold on 'the wonder years',Fred savage +Music,In Which Year Did Pink Floyd Achieve Their Only No.1,1979 (Another Brick In The Wall),General,Extreme fear of open spaces,Agoraphobia,Science & Nature,What is the mathematical term used to describe the shape of a cell in a honeycomb ,Hexagon  +General,With what are camel hair brushes made,Squirrel hairs,Sports & Leisure,Which sport do you associate with the Russian woman Maria Sharapova ,Tennis ,General,Soteriophobia is the fear of,Dependence on others +Sports & Leisure,Which Golfer Won Over $9 Million Prize Money In 2000 ,Tiger Woods ,General,What is the main ingredient of mock turtle soup,Calf's head,Music,"Which Duo Performed The No.1 Song ""I Know Him So Well""",Elaine Paige / Barabara Dickson +General,In the Vietnam war what was the signal US to evacuate Saigon,Bing Crosby White Xmas on Radio,Food & Drink,In Monty Pythons The Meaning Of Life What was Mr. Creosote's very last course? ,A wafer thin mint ,General,How did Buffalo Bill stick to one glass whisky a day,Quart glass +General,Who was the title character in The Merchant of Venice?,Antonio,General,What is a long wire wound in a close-packed helix and carrying a current,Solenoid,General,Where could you spend a Kyat,Burma +General,What did Lorraine Chase famously advertise on TV,Campari,Geography,______________ is the world's oldest black republic. The major religion there is voodoo.,Haiti,General,Skopje is the capitol of where,Macedonia +Tech & Video Games,Which character was introduced in 'Super Street Fighter II'? ,Cammy,General,U.S. captials North Carolina,Raleigh,People & Places,Whose Real Name Is 'Arthur Stanley Jefferson' ,Stan Laurel  +General,Which group recorded the albums Regatta de Blanc and Ghost in the Machine,The police,General,If you were severed a dish 'belle h'elen what fruit would it be,Pears,Science & Nature,With Which Animals Do You Associate The Disease Myxomatosis? ,Rabbits  +General,What is the fear of step mother known as,Novercaphobia,General,Duo who had a hit song with 'I got you babe',Sonny and cher,General,Name Any Year in The Life Of Davy Crocket King Of The Wild Frontier,1786 – 1836 Lived Till He Was 50 +General,What foreign country's phone book is alphabetized by first name,Iceland,General,Who is the american inventor of photographic materials,George eastman,Geography,In which country did acupuncture originate? ,China  +General,Where does the u.s government keep it's supply of silver,West point academy,Entertainment,What was Keanu Reeves' first big film?,Point Break, History & Holidays,The name of which place of biblical significance actually means 'House of Bread'' ,Bethlehem  +Science & Nature,Which Animal Can Jump The Highest? ,The Whale , History & Holidays,Which Group Had A 60s Hit With The Song 'She's Not There'' ,The Zombies ,General,Xavier Roberts was the original creator of which toy,Cabbage Patch Dolls +Science & Nature,What is the world's longest snake,Python,General,"In 'southpark', what is chef obsessed with",Sex,General,E J Allen led spy team to South Civil war what name better known,Alan Pinkerton +Science & Nature," While dangerous to swimmers, the fact remains that __________ are much less dangerous than sharks.",Barracudas, History & Holidays,What is Zimbabwe's President Robert Mugabe's middle name ,Gabriel ,General,What carbonated beverage started out life in the 1890's as 'Brad's Drink',Pepsi +General,What is a group of this animal called: Swift,Flock,General,In what magazine does Alfred E Newman appear,MAD,Science & Nature,This parasite lives in the intestines of man and animals.,Tapeworm +General,What was Blondies name before she married Dagwood,Boopadoop,General,What word can mean a RC prayer Blood Clot extra calendar day,Embolism,General,The tumblebug is an alternative name for which insect,Dung Beetle +Sports & Leisure,In Horse Racing How Many Furlongs Are In A Mile? ,Eight ,General,"In Which Country Was The ""Lords Of The Rings"" Creator J.R.R Tolkien Born",South Africa,Music,"Written By Jagger And Richards Who Had A Hit With ""Out Of Time"" In 1966",Chris Farlowe +General,What is sometimes referred to as Zulu time,Greenwich mean time,General,"Who discovered their dream girls, the chipettes in an 1987 flick",Chipmunks,Science & Nature,Name the heaviest breed of domestic dog.,St. Bernard +General,A young what is called a Cheeper,Grouse Partridge Quail,General,What plant does the Colorado beetle attack,Potato,Geography,Which City Is The Capital Of Bulgaria? ,Sofia  +General,Which WW2 fighter-bomber did de Havilland's make out of wood,Mosquito,General,What is the collective noun for a group of Rhino?,Crash,General,"This disease consists of a purposeless, continual growth of white blood cells",Leukemia +General,How many players are there in a men's lacrosse team,Ten,General,If you landed at Merignac airport - where are you,Bordeaux, History & Holidays,Who led the children of Israel out of Egypt?,Moses +General,Are yabbies found in fresh or salt water,Fresh,General,Cassius Marcellus Coolidge painted which famous paintings,Dogs playing pool cards etc,General,What did the old woman who lived in a shoe give her children for supper,Broth without any bread +General,What was Junko Tabei the first woman to reach,Summit of mount everest,General,What nationality was dr bart hughes,Dutch,General,"What's a new yorker trying to bribe a way into when offering ""key money""",Apartment +General,Fatty Arbuckle was the first filmed recipient of what in 1913,Custard Pie, Geography,What is the basic unit of currency for Indonesia ?,Rupiah,General,What is the name for the sacred circle in Tantric Buddhism,Mandala +Geography,What is the capital of Burma,Rangoon,General,Which country is alphabetically last,Zimbabwe,General,"Up To & Including Tony Blair, How Many Prime Ministers Have Served Under Queen Elizabeth II",Ten +General,A myomancer predict the future by studying what,The shape of mice,General,What was Maggie Seaver's maiden name on Growing Pains?,Maggie Malone,General,"Pinky Punky, Who Was Known To Have A Bit Of A Nasty Streak, Belonged To Which Entertainer.",Timmy Mallets Mallet +General,Where was the first football world cup,Uraguay,General,What animal has the worlds shortest sperm,Hippopotamus,Art & Literature,"A movement of the 1920s and 1930s that began in France. It explored the unconscious, often using images from dreams. It used spontaneous techniques and featured unexpected juxtapositions of objects. ",Surrealism +Music,Name The Band That Lost 4 Members In The Plane Crash That Killed Otis Redding,The Bar Kays,General,Which mythological monster had nine heads,Hydra,General,The first 'talking' film was released in what year,1927 + History & Holidays,What 'IR' Does Michael Fish Say To His Wife When The Heavens Open On Christmas Day ,It's Reindeer ,General,What is the young of this animal called: Whale,Calf,General,"On Diff'rent Strokes,what pet did Arnold keep in his room?",Goldfish + History & Holidays,"The current image of Santa Claus as a plump man with white beard and red and white tunic dates from a 1931 Advertising campaign, for which product ",Coca-Cola ,Science & Nature,What Is The Horn Of A Rhinoceros Made From? ,Keratin (Hair) ,Music,Which American President Was Fighting In The Ring In The Frankie Goes To Hollywood Video Two Tribes,Ronald Reagan +General,"In the 15th Century, in which ship did John Cabot sail to Canada",Matthew,General,What Grim Discovery Was Made By An Electrician Fitting A Burglar Alarm In Seattle In 1994,Kurt Cobains Body,General,Metathesiophobia is the fear of,Changes +General,What is Belgium's national Airline,Sabina,General,"Which hero of tv and cinema fights an unending battle for 'truth, justice, and the American way",Superman, History & Holidays,"Name the sitcom that featured Judd Hirsch, Andy Kaufman, Tony Danza and Danny Devito. ",Taxi  +General,In ancient China what was hung outside a bad doctors house,Lantern for each dead patient,General,What is the world's fastest land animal,Cheetah,General,Who was Captain Edward J. Smith,Captain of the titanic +General,Illyngophobia is the fear of,Veritgo,Science & Nature,North American Indians ate watercress to dissolve what in the bladder?,Gravel and stones, History & Holidays,Which type of vegetable went on sale in frozen form in Massachusetts in June 1930? ,Peas  +General,In traditional wedding anniversaries what is given on the seventh,Wool,Science & Nature,Who Is Considered As The Originator Of The Concept Of A Computer ,Sir Charles Babbage ,General,Who were the main combatants in the punic wars,Rome and carthage +General,The base of the great pyramid of Egypt is large enough to cover how many football fields,10,General,Who was Arthur Scargill's predecessor as President of the NUM,Joe gormley,General,What is the worlds largest sand island northeast of Brisbane,Fraser island +Music,"Which Artist Topped The Us Billboard Singles Chart, The Latino Singles & Album Chart All In The Same Week",Christina Aguilara,General,Robert de Niro won Best Actor Oscar for which film in 1980,Raging bull,Technology & Video Games,What does ASCII stand for?,American Standard code for information interchange +General,What is the Capital of: Iran,Tehran,General,Complete the proverb: There's many a good tune,Played on an old fiddle,General,What is the better known name of writer Madame Dudevant,George sand +General,What is the fastest racket sport - over 200 mph,Badminton,General,What is a corrosive substance,Acid,General,The 'Grand Pensionary' was the most important official of which country during the time of the United Provinces?,Holland +Food & Drink,What Is Black Velvet ,A Mixture Of Stout & Champagne ,General,What are the world's tallest trees,Coast redwoods,General,What eighties TV show starred Tom Hanks in women's clothing?,Bosom Buddies +General,With what band was harvey schmidt,The fantasticks, History & Holidays,What does Beetlejuice eat when he reaches out of his grave in the scale model of the town? ,A Fly ,General,A game of pool is referred to as a _____,Frame +General,Who's Autobiography Is Entitled “Catch A Fire”?,Mel B (Spice Girls),Geography,Which Island Lies At The Most South Westerly Part Of The United Kingdom ,Bishop Rock ,General,Which old English time unit is 1.5 minutes long,A Moment +General,What liqueur is flavoured with the rind of bitter oranges,Cointreau,Music,When The Supremes Hit The No.1 Spot In The UK in 1964 With Baby Love It Was Not Released On Motown But On Which Other Label,Stateside,General,From what material are millefiori ornaments made,Glass +General,Where was it once against the law to have a pet dog,Iceland, History & Holidays,Which royal couple were married on the 29th July 1981 ,Charles and Diana ,General,The dial tone of a normal telephone is in what key,F +General,What is the flower that stands for: self-esteem,Poet's narcissus,General,"""Dragon Whiskers"" is a type of tea from what country",China,General,Hippopotomonstrosesquippedaliophobia Is the fear Of What?,Long Words +General,Who appeared on the first US postage stamps (both names),Washington - Franklin,Music,"Was Maniac A Hit From ""Flashdance"" Performed By ""Richie Sembello"" Or Glen Menderios",Richie Sembello,Music,"Which DJ Championed The Undertones Naming ""Teenage Kicks"" As His All Time Favourite",John Peel +Entertainment,Before Olive Oil met Popeye she was engaged to someone. Who was he,"Ham gravyzzcomment: olive oyl was wrong, jono82 said",General,Where did the Angel falls get its name,Pilot Jimmy Angel crashed 1937,Music,By what name is Don Van Vliet better known?,Captain Beefheart +General,What shoe brand were all 39 members of the Heaven's Gate cult wearing when they committed suicide in 1997,Nike,General,Who patented the first photograph,Thomas edison, Geography,What is the basic unit of currency for Vietnam ?,Dong +Music,"Which Group Took ""My Perfect Cousin"" To No.9 At The Start Of The 80's",The Undertones,Sports & Leisure,At Which Venue Does The Us Open Tennis Tournament Take place ,Flushing Meadows ,General,A thoroughgoing & complete republican might be called a dyed-in-the___what kind of republican,Wool +General,Who was lead singer and principal songwriter with the American pop group Bread,David gates,General,What viral skin condition does folklore say is caused by handling toads,Warts,Music,A Daughter Was Born To Mick & Bianca Jagger In October 1970 What Did They Name Her,Jade +Sports & Leisure,In which sport is the America's Cup awarded,Sailboat racing,Geography,How many u.s. states border the pacific ocean ,Five ,Religion & Mythology,The sea gods had a three-pronged spear called a(n) ________.,Trident +Science & Nature,What animal has red patches on its rear?,Mandrill,General,In which film did Groucho Marx play the veterinary doctor Hugo Z. Hackenbush',A day at the races,General,What does MILK1 do for a living,Dairy farmer + History & Holidays,How many astronauts manned each Apollo flight?,Three,General,Who founded the boy scouts of America,Daniel Beard,General,Who did yoko ono marry,John lennon +General,What profession has four times the average aids in USA,Catholic Priests,General,"Who said 'when the going gets tough, the tough get going'",Knute rockne,General,What is the name of the cryptography machine used by the german's in WW2?,ENIGMA +General,What is a cross between a blackberry and a raspberry,Tayberry,Science & Nature," __________ gather in groups to sleep through the winter. Sometimes up to 1,000 of them will coil up together to keep warm.",Rattlesnakes, History & Holidays,"""What Did My True Love Give To Me On The """"Fourth"""" Day Of Christmas"" ",4 Calling Birds  +General,What animal has the best hearing,Bats,Music,In Which Year Did Abba Win The Eurovision Song Contest,1974,General,What cartoon characters first name is Quincy,Mr Magoo +General,What countries native name is Land of the long white cloud,Aoteraroa – New Zealand Maori,General,Kind of Swiss cheese with holes in,Gruyere,Music,Which 60's Icon Released Lovesick In 1998,Bob Dylan + Geography,In which city is the Sistine Chapel ?,Vatican City,General,The locals call it Metohkangmi what do we call it,Abominable Snowman or Yeti,General,In what sport would you find a Hosel,Golf - Hole in club shaft fits into +General,Yggdrasil is what in Norse mythology,Tree,General,In CHIPS what make of motorcycles did they ride,Kawasaki,General,What candy received it's name because the machine that makes them looks like it is kissing the conveyor belt,Hershey kisses +General,What is on a 5000 acre landfill at the head of jamaica bay near new york city,John f kennedy airport, History & Holidays,In what year of WW II did Russia declare war on Japan,1945,General,Which two great physicists developed calculus independently of each other?,Isaac Newton and Leibniz +General,Ireland's River Shannon flows into which ocean,Atlantic,General,Who was the lone ranger's indian companion,Tonto,General,If you were eating a Prunis Domesticia what would it be,Plum +Science & Nature,What is a young swan called?,Cygnet, History & Holidays,"What song, popular in the First World War, was written by George and Felix Powell? ",Pack up your Troubles in your Old Kit Bag ,General,Who wrote A Farewell to Arms,Ernest hemingway +General,Who Played The Role Of Father “Noel Furlong” In The TV Show Father Ted,Graham Norton, Geography,What is the capital of Bahrain ?,Manama,General,What constellation is represented by a crab,Cancer +General,What word means 'to chew the cud'?,Ruminate,General,Which County Inflicted Englands First Ever Football Defeat On Home Soil,Ireland,General,Who sailed in the Golden Hind,Sir Francis Drake +General,Each unit on the Richter scale is euivalent to a power factor of how much,Thirty two 32, History & Holidays,In 1453 it is alleged that a witch flew on a broomstick for the first time but was it a male or female witch ,Male ,Sports & Leisure,In which sport is the Davis Cup awarded,Tennis +General,What is extracted from the ore caserite,Tin,General,"Eddie Bauer, Erehwon, and North Face are these types of stores.",Outfitters,General,Prosophobia is the fear of,Progress +Sports & Leisure,"Which Race Over 200 Laps Is Started With The Words, 'Ladies And Gentlemen, Start Your Engines''? ",The Indianapolis 500 ,General,What word appears in more film titles than any other,Love,General,How many ounces of orange juice contains the minimum daily requirement for vitamin c,Six ounces +General,What is ccdos,Chinese character disk operating system,General,Roger Moore's first appearance as James Bond was in which film,Live and let die,Food & Drink,"Almond liqueur that comes from the Italian for ""bitter"", not ""love""",Amaretto +General,In Japan Trade Unions collect dues from what unusual source,Robots in factories pay dues,General,What is 'anacreon in heaven',Old english drinking song,General,How many books are there in anne rice's vampire series,Five +General,Who baptised jesus,John the baptist,Geography,Who Found The True Source Of The River Nile ,Sir Richard Burton & John Speke ,General,In roman numerals what does the letter M with a Bar over it stand for?,One Million +General,John Pierpoint wrote what seasonal ditty,Jingle Bells,General,Sculptors which of the four teenage mutant ninja turtles was named after an artists and/or sculptor that did not occur in the same time period as the other three,Donatello,General,What was the original filling of the savaloy sausage,Pigs Brains +Food & Drink,How do you prevent a black rim forming round the yolk of a boiled egg? ,Plunge into cold water after boiling,General,Which novel of the Russian Revolution did Boris Pasternak write,Dr zhivago,Science & Nature,What word is used for a female sheep,Ewe +General,Which hollywood actor combined gibraltar and a river to create his name,Rock,General,In 1950 what character was on first metal lunchbox in the US,Hopalong Cassidy,General,What material did Michelangelo carve most of his sculptures out of,Marble +General,Which museum in Washington DC is said to be the largest in the world,The smithsonian institute,General,Paul Robeson the singer of old man river had what profession,Lawyer,Entertainment,Who shot Bruce Wayne's parents,Chill +General,What sport is played 11 a side on ice with a ball - variable pitch,Bandy,Music,"Which Freeway Did ""Aretha Franklin"" Sing About",Freeway Of Love,General,Which shakespearean character provided the plot for verdi's only comedy,Falstaff +General,What 2 countries border the Dead Sea,Israel and jordan,General,Where was the first Three Tenors concert held,Rome,Technology & Video Games,What was Nintendo's first Arcade game? ,Radarscope +General,In Hindu philosophy what does Yoga literally mean,Union,General,Who played garp's wife in the movie the world according to garp,Mary beth,General,5 items a Sikh must have Comb Dagger Hair Metal Bracelet and,Knee length undershorts 5Ks +Sports & Leisure,"What football player rushed for 2,003 yards in 1973",O j simpson,General,What is the Ikurrina,Basque Flag,General,Banaba (or Ocean Island) is found in which country,Kiribati +General,Which countries wine might be labelled DOCG,Italy,General,Pancho was whose faithful sidekick,Cisco kid's,General,What ship was blown up at the end of The African Queen,The Louisa +General,A light iron-tipped S.African spear,Assegal, History & Holidays,In the 9th century which city had several thousand bookstores ?,Baghdad,General,What links Cary Grant Mohammed Ali Prince Charles,All amateur Magicians +General,What does the 'c' in the equation e=mc2 stand for,Speed of light,General,Which musical note is the longest,Breve,General,Simpson's what was Homers nickname as baseball team mascot,Dancing Homer +Music,What Happened To Lisa Selson At The Tibetan Freedom Concert In 1998,She Was Struck By Lightning,General,"This actor played Blondie in The Good, The Bad, and The Ugly?",Clint Eastwood,General,Where in the body would you find the carpals,Wrist +General,What is Thalassophobia a fear of,The Sea,General,Who coined the term 'assassination',William shakespeare,General,How many cigars did sir winston churchill ration himself to a day,Fifteen + History & Holidays,"Who was kidnapped on the night of March 1, 1932?",Charles Lindbergh Jr,Geography,Why Did David Livingstone Go To Africa ,He Was A Missionary ,Art & Literature,Who wrote 'The Female Eunuch'?,Germaine Greer +General,What Hebrew word means 'so be it'?,Amen,Entertainment,Name Donald Duck's girlfriend?,Daisy,Food & Drink,What alcoholic beverage is produced by the Solera method? ,Sherry  + History & Holidays,"Which name belongs rightfully to The Craft Is It Bonnie, Shelly, Sally, Penny ",Bonnie ,Food & Drink,Which sauce is named after a river and state in Mexico? ,Tabasco ,General,Dark volcanic rock,Basalt +Music,In Which Year Did Abba's Benny & Frida Get Divorced,1981,General,Amy the girls name means what,Beloved, History & Holidays,Who wrote 'The Starry Messenger'?,Galileo +General,In every show that tom jones and harvey schmidt there is at least one song about ______,Rain,General,"Copeland, Mason, Dux and Bow all types of what",Pottery,General,Barajas is the main airport - where,Madrid + Geography,"This country is divided into two parts: Sabah and Sarawak on the island of Borneo, and a peninsula north of Singapore.",Malaysia,General,"Name the oppressive hot dry wind on the north coast of Africa, that comes from the Sahara during the spring and summer.",Sirocco,General,In which country is the cheese Bolbo produced,France +General,Mead is made from this,Honey,General,Poison oak and ivy belong to which general family,Cashew,General,Baptista is Katherine's father in which Shakespeare play,The Taming of the Shrew +General,What was the worlds first X rated cartoon,Fritz the cat,General,What U.S. state includes the telephone area code 704,North Carolina,General,"According to U.S. law, what may not be granted on a useless invention, on a method of doing business, on mere printed matter, or on a device or machine that will not operate",Patent +General,Where did the flower Lupin originate,Canada,General,St Boniface is the Patron Saint of which country,Germany,Entertainment,Where does George Jetson work,Spacely sprockets +General,"To what was colonel potter, of 'mash' fame, allergic",Tomato juice,General,"In religious art, which Saint is associated with an ox",Saint Luke,Music,Which Female Vocalist Sang On Eminem's Hit Song “Stan”?,Dido +Music,Which Group Was Formed By The Original Members Of Joy Division?,Joy Division, Geography,On what river is Liverpool?,Mersey,General,To which planet does the moon iapetus belong,Saturn +Entertainment,Who is lead guitarist for Guns'n'Roses?,Slash,General,What are Black Bulger Lawyers Wig Penny Bun types of,Fungi,General,"Cuirass, greave and pauldron are all pieces belonging to what",Armour +General,The volume of the earth's moon is the same as the volume of what ocean,Pacific ocean,Sports & Leisure,Baseball: The New York ______?,Mets,General,Where were the 1956 Summer Olympics held?,"Melbourne, Australia" +Food & Drink,What fruit flavour is used in crepes-suzette ,Orange ,General,Armand Tarmizan is the 'real' name what cartoon character,Principle Skinner,General,In which 1971 film did Jane Fonda play a prostitute fearing for her life,Klute +General,What is the monty python parody of the legend of king arthur called,Search, Geography,Where is the statue 'Le Petit Pissoir'?,Brussels,People & Places,King Richard The 1 st Is Sometimes Refered To As What ,Richard The Lionheart  +General,Nina Post and Louise Gordon of which group released 'Seether' off 'American Thighs' in 1994,Veruca Salt,General,Which chemical element has the ancient name Stannum,Tin,Music,In which pop band does Damon Alburn sing?,Blur +General,Tia Maria - Vodka and Coke make what cocktail,Black Russian,General,Who wrote the 39 steps (both names),John Buchan,Music,Kylie Minogue Had To Back Out Of Which Venue Due To Finding Out That She Had Breast Cancer?,Glastonbury +General,Which 1949 comedy film featured Scottish islanders looting a stricken ship laden with Scotch,Whisky galore,General,With what do camels protect themselves from blowing sands,Three eyelids, Geography,What is the capital of Guinea-Bissau ?,Bissau + History & Holidays,Who Was Henry Bolingbroke's Father ,John Of Gaunt (The Duke Of Lancaster) ,General,"Which famous horse race was won Urban sea, Carnegie, Lammtarra",Prix de l'arc de Triomphe,General,What is the chemical name for vitamin c,Ascorbic acid +Science & Nature," Every bird must eat at least half its own weight in food each day to survive. Young birds need even more. A young robin, for example, eats as much as 14 feet of __________ a day.",Earthworms,Music,What Was The Prodigys First Uk Hit Single,Charlie,Science & Nature,What name is given to a female calf?,Heifer +General,"Name used interchangeably for a disease of rye, for the fungus causing the disease, for the sclerotium (compact hardened mycelium, or fruiting surface) of the fungus, & for the dried sclerotium, what contains certain valuable drugs",Ergot,General,What was the pharon,Lighthouse,Science & Nature,Linseed oil is obtained from the seed of which plant,Flax +General,What year did the first motel open,1925,Technology & Video Games,The Minus World of Super Mario Bros. is a never-ending version of what stage? ,2-Feb,General,"On december 1, 1990, workers from france and england met in what structure in the middle of the english channel",Chunnel +General,The Name Of Which Creature Meant “ Little Thief ” In Latin ?,Ferret,Music,Which 1960 Vocalist Changed His Name From Ernest To Evans,Chubby Checker,General,Lagos is the capital of ______,Nigeria +General,"The aniseed-flavoured drink absinthe contained, originally, an extract of which plant",Wormwood,General,What is the Capital of: Liberia,Monrovia,General,In which country did the turnip originate,Greece +Science & Nature,Which Creature Had A Short Nose Horn & Two Larger Horns On The Brow ,Triceratops ,General,What gets its name from the Greek meaning large catapult,Howitzer,General,What U.S. state includes the telephone area code 706,Georgia +Music,"Which Musical Do The Songs ""Look At Me I'm Sandra Dee"" & ""Beauty School Drop Out"" Come From",Grease,Food & Drink,From what is the Mexican dish huevos rancheros made? ,Baked Eggs ,Food & Drink,What is the main ingredient of the Korean Soup Bosintang? ,Dog  +Music,"Who Wrote ""I Wanna Be Your Man"" The Stones 2nd UK Hit",Lennon McCartney,Sports & Leisure,"In pro football a ""sudden death"" period lasts how many minutes long",15,General,"What Was Discovered By ""Garcia Lopez De Cardenas"" In 1540",The Grand Canyon +General,Laliophobia is the fear of,Speaking,General,"In a hospital, what would the O & G Department be",Obstetrics and gynaecology, History & Holidays,Which King of France was known as the (Sun King)? ,Louis XIV  +Music,Chuck Berry Had His Only No.1 In The UK In 1972 With What,My Ding-A-Ling,General,Where is the worlds largest Chinese settlement outside Asia,San Francisco – Chinatown,General,In Detroit wilfully destroying your old what is illegal,Radio +General,Which 19th century French artist painted 'Bathers at Asnieres',Georges seurat,General,When did Andy Warhol die,1987,General,"Potentially dangerous to human life on earth, what is filtered out by the Ozone Layer",Ultra violet radiation +Entertainment,Who was the only songwriter to win the Eurovision Song Contest twice?,Johnny Logan,Food & Drink,If You Asked For Scraps In A Chippy What Would You Get ,Bits Of Fish Batter , Geography,Seoul is the capital of which country?,South Korea + History & Holidays,In which country was Adolf Hitler born?,Austria,General,What is the Capital of: Syria,Damascus,Music,"Which Rap Band Originally Entitled Their 1986 Debut Album ""Don't Be A Faggot"" Until Their Distributor Refused To Accept It",The Beastie Boys +General,Armadillos can walk where,Underwater,Science & Nature, __________ have been trained to have recognition vocabularies of 100 to 200 words. They can distinguish among different grammatical patterns.,Chimpanzees,General,Who wrote Man are from Mars Women are from Venus,John Grey +Sports & Leisure,At Which Motor Racing Circuit Was Ayrton Senna Killed ,Imola ,General,A Group of Whale is called a,Pod,General,Who was the first european explorer to reach India by sea,Vasco da gama +General,The westernmost point in the contiguous U.S. is where,"Cape alava, washington",General,"Who said ""The child is the father of the man""",Wordsworth,General,In 1760 what means of personal transport was invented,Roller Skates +General,What structure in the back of the brain governs motor control,Cerebellum, Language,What is the last letter in the Greek alphabet?,Omega,Music,Which Major 90's Dance Act Was Fronted By Heather Small,M People +General,The human bodies got 45 miles of them - what,Nerves,Sports & Leisure,Who was sent off on Saturday in the match between Arsenal and Bolton? ,El-Hadji Diouf ,General,Information about what subject is recorded in Wisden,Cricket +General,What is Burma now known as,Myanmar,General,What is the name of the desert region of south east Ethiopia over which Somalia also claims sovereignty,Ogaden, History & Holidays,What was the name of the domestic videocassette tape recorder system introduced by Sony in 1975? ,Betamax  +General,Whose military autobiography was titled Crusade in Europe,Dwight d eisenhower,General,"Founded in 1608 by Champlain, it was the capital of New France",Quebec,General,Which king married Charlotte of Mecklenberg Strelitz,George the third +General,What number did Levi Strauss affix to the first pair of jeans sold,501, History & Holidays,On What Japanese City Was The First Atomic Bomb Dropped ,Hiroshima ,General,What is a group of giraffes,Tower +Sports & Leisure,Football: The Dallas _________.,Cowboys,General,What is the capital of tennessee,Nashville,Sports & Leisure,Which 3 Athletics Events Do Woman Not Participate In ,"Hammer, Pole Vault, Triple Jump " +General,"What has 1,792 steps in it?",Eiffel tower,General,"Who said ""I think therefore I am""?",Rene Descartes,General,How did folk singer Roy Harper catch Toxoplasmosis,Kiss of life - to a sheep +General,Vermicelli pasta literally translates as what,Little worms,General,Jean Claude Killy famous in which sport,Skiing,General,"A projecting support built into or against the external wall of a building, typically used in Gothic buildings. A flying … is an arch that transfers the thrust of a vault to a lower support.",Buttress + History & Holidays,In 1953 Queen Elizabeth II was crowned following the death of her father George VI but what was his 'proper'' first name; David - Albert - Louis - Edward - George ,Albert ,General,Lucus Dominitus Ahenobarbus was better known as who,Nero,Music,In Which Country Was Olivia Newton John Born,"Cambridge, England (Went To Oz Aged 5)" +Music,Before Eddie Money Became Famous Was He A New York Cop Or A Pastrty Chef,A New York Cop,General,Who still receives an estimated 25 pieces of junk mail per year at Walden Pond?,Thoreau,Music,"Only 2 Singers Appeared On Band Aid Versions Of ""Do They Know Its Christmas"" What Group Were Thay Both In",Bananarama (Disputable) +Art & Literature,Where is the Louvre located,Paris,Music,What Was Virgin Records First LP Release Back In 1973,Mike Oldfield / Tubular Bells,General,"In an average lifetime, the average American visits a ___ 22 times",Pediatrician + History & Holidays,Which country is the largest exporter of Christmas trees? ,Canada ,Food & Drink,What is the fruit flavour of Cointreau? ,Orange ,General,The thickness of what is given an 'swg' rating,Wire +General,The oldest one in America still working opened in 1829 what,Brewery, History & Holidays,Don Mintoff became which countries first Prime Minister after it became a republic in 1974? ,Malta ,General,Where were tommy lee jones and al gore freshman roommates,Harvard +General,What company used the little aligators as it's symbol on clothing?,Izods, History & Holidays,Who was the leader of the Khmer Rouge?,Pol Pot,Science & Nature, Most tropical marine fish could survive in a tank filled with __________,Human blood +General,Who narrated Jeff Wayne's War of the Worlds,Richard Burton,General,Which English King holds the official record of bastards 21,Henry I,General,In what country would you find Timbouctou,Mali +General,Which war was ended by The Congress of Westphalia,Thirty Years War,General,What year was the Habitat company founded,1971,General,"Which Soap Boasted A Cafe Called ""The Hot Biscuit""",Dallas +General,The constellation Mensa has what English name,Table, History & Holidays,"What type of shoes did Run-D.M.C. sing about, which were what most rappers in the early eighties were into? ",Addidas ,General,"For every tree that is cut for lumber, how much is sold as timber",One eighth +General,"Who, in World War Two, was the Japanese equivalent of 'Lord Haw Haw'",Tokyo rose,General,Cardinal and Ordinal are types of what,Numbers - 1 2 3 - 1st 2nd 3rd,General,What song is sung the most,Happy Birthday +General,Which is the stately home of the Devonshire family,Chatsworth,General,How many blades are there on a kayak paddle,Two,Sports & Leisure,"Who was known as the ""Sultan of Swat""",Babe ruth +General,What line on a weather map links all points of equal pressure,Isobar,General,In the original Star Trek the Horta was a life form based on what,Silicon,General,"Until 1947, what did 'gripe water' contain",Opium + History & Holidays,To What Did The Chant Hell No We Won't Go Refer ,The Vietnam War ,General,What is the term for the distance around a circle,Circumference,Music,"Which White Motown Singer/Songwriter Had Hits With ""Indiana Wants Me"" & ""Theres A Ghost In My House""",R Dean Taylor +General,Aescapalious emblem staff snake Greek Roman god of what,Medicine,General,In what sport is the Charles Brownlow award for fairest player,Aussie Rules Football,General,What is the sum of 4 x 4 x 24 x 44,16896 +General,In what Australian state would you find Bathurst,New south wales nsw,General,There are 15 peaks in Europe higher than Mont Blanc. In which mountain range are they,Caucasus,General,Who had a hit with the song '24 hours from Tulsa',Gene pitney +General,Another name for guardian angels is,Watchers,General,What sport appears in the phonetic alphabet,Golf,General,What does a Hafiz know,Koran by Heart +People & Places,With Whome Do You Associate Lady Falkender ,Harold Wilson ,General,"What do Christians call the place which the Hebrews called Golgotha, (Place of Skulls)",Calvary,Music,Which Group Were Drowning In Berlin In 1982,The Mobiles +General, What is a device to stem the flow of blood called,A tourniquet,General,What country is coffee originally from,Ethiopia,General,What is the fear of new drugs known as,Neopharmaphobia +General,Mel Blanc Provides The Voice Of Which Famous Cartoon Character?,Porky Pig & Others,General,Iguaca National Park lies on the border between Argentina and which other country,Brazil,General,Who wrote the opera Zaide,Mozart +Music,Which Band Took Their Name From A Type Of Fire Engine Manufactured In The 1920's,REO Speedwagon,General,"American money with serial #'s beginning with ""B"" are printed where",New York,Geography,What Is The Worlds Largest Ocean By Area ,Pacific  +Science & Nature,Who discovered x-rays in 1895 ,Rontgen ,Science & Nature,What was the first animal on the endangered species list?,Peregrine falcon,General,Which painters work is the most stolen,Pablo Picasso +General,What shoemaker was an underwriter for rock's 1988 human rights now tour,Reebok,General,Beethoven reportedly poured this over his head to stimulate his brain,Water,Entertainment,What group refused to have their pictures taken while they were not in their makeup?,Kiss +General,Aphallatosis is a mental disorder caused by the lack of what,Sex life,General,Singapore is the capital of ______,Singapore,Music,What Song was George Harrisons first composition on the A Side of a Beatles single?,Something +General,Who invented the radio,Reginald fessenden,General,What german military leader of the afrika korps was known as 'the desert fox',Erwin rommel,General,What is a drug or other substance used to produce unconciousness and insensibility to pain,Anaesthetic +General,Name the first British actress to appear on a British stamp 1985,Vivien Leigh,General,"Who wrote ""words of love"" that the beatles recorded",Buddy holly,General,What is 'mother's ruin',Gin +General,What is a Sam Browne,Military belt,General,The temperature at which a liquid gives off a vapour which can be ignited is called it's ______,Flashpoint,General,Gerry Dawsy became more famous as who,Englebert Humperdinck +General,Which British rock group released a 1990s album called Parklife,Blur,General,In Ireland what is a Gombeen Man,Moneylender,General,Chase chevy chase was the first original cast member to leave which show,Saturday +General,Where is the rock and roll hall of fame,"Cleveland, ohio",General,"Which behaviorist conducted the ""Little Albert"" experiment?",John Watson,Science & Nature,Where Might You Find Your Temporal Lobe ,On Your Brain  +General,Schubert always slept with what on,Spectacles - in case he got idea,General,"Who was known as ""The Ace of Spies""",Sydney reilly,General,What do we more commonly know a 'Tup' as?,Ram +General,U.S. Captials - Rhode Island,Providence, History & Holidays,"According to the lyrics of the famous Christmas song, what was Frosty The Snowman's nose made from? ",A Button ,Music,"Which Song Had The Working Title ""Scrambled Eggs""",Yesterday +Tech & Video Games,"What is the name of the cloud-riding, glasses-wearing koopa in the Super Mario Bros. series? ",Lakitu,Geography,What is the capital of Thailand,Bangkok, History & Holidays,Which T - Rex hit contains the line 'Take a black cat and sit it on your shoulder' ,Ride A White Swan  +General,In which U.S. state is the city of Minneapolis,Minnesota,General,In the Commedia del'Arte who was the daughter of Pantaloon,Columbine,Geography,What country is located between Panama and Nicaragua,Costa rica + Geography,As what is Constantinople now known?,Istanbul,General,Tempera uses water and what to paint with,Egg Yoke,General,What nationally was Mata Hari shot as a spy,Dutch +General,"What U.S. highway is the longest, starting in Cape Cod, Massachusetts going through 14 states, & ending in Bishop, California",Route 6,General,"Who stood at the top with ""stand by your man",Tammy wynette,General,Which American state has a Union Jack on its flag,Hawaii +General,Carson City in Nevada - dubious distinction first what 1924,Gas Chamber used,General,In Ghandi who played the General caused massacre Amritsar,Edward Fox General Reginald Dyer, History & Holidays,Who Became Chancellor Of Germany In 1933 ,Adolf Hitler  +General,In Paris there are two islands - Ile de la Cite and what,Ile St-Louise,General,What is Holland's largest ever flood control project called,Delta plan,General,"Which genetic trait, often passed from mother to son, was first recognised by John Dalton, who was himself a sufferer",Colour blindness +Science & Nature,What Is A Prosthetic ,An Artificial Body Part ,People & Places,By what name is Richard Starkey better known as?,Ringo Starr,General,On a racing form what does the letter u indicate,Unseated rider +Geography,What was the former name of Manhattan's Park Avenue? ,Fourth Avenue ,General,"Acronym for microwave amplification by stimulated emission of radiation, a device that amplifies or generates microwaves or radio waves",MASER,Entertainment,"What was the name of Han Solo's spaceship in ""Star Wars""?",Millennium Falcon +General,St Luke followed what profession before joining Jesus,Medicine,General,Epiphany Christian feast 6th Jan translates from Greek as what,Manifestation,General,Virginia Woolf always did it standing up - did what,Wrote her books +General,The sewing machine was patented in what year,1846,Food & Drink,"A Cocktail Known As TNT Comprises Of Tequila, Neapolitan Brandy, But What Does The Second T Stand For? ",Tia Maria ,General,How did Queen Victoria ease her menstrual cramp pain,Used Marijuana +General,How did captain cook lose 41 of his 98 crew on his first voyage to the south pacific in 1768,Scurvy,General,With what country is prince rainier iii associated,Monaco,General,What pole is colder the South Pole or the North Pole,The south pole +General,What does a hygrometer measure,Humidity,Food & Drink,"Which country house in the south of England gives its name to a summer drink of claret, soda, and sugar? ",Badminton ,General,"What is the name of a floor in a building between floors, especially between the ground and first floors",Mezzanine +General,"Thick, light yellow portion of milk from which butter is made",Cream,General,Who was adolf hitler's mistress,Eva braun,General,In the UK 60% of pets have what,Health Insurance +General,Indians where is sand creek,Colorado,General,To whom is Sleeping Beauty betrothed,Prince charming,General,Hades was god of what,The underworld +General,"Which comedian said'If they liked you, they didn't applaud - they just let you live'",Bob hope,Sports & Leisure,Prior To Joining Manchester Utd In 2007 Which Club Did Cristiano Ronaldo Play For? ,Sporting Lisbon ,General,What is a Kakapoo,Nocturnal New Zealand Parrot +General,Potatoes were fist sold as what,Ornamental Plants,Music,What Title Connects A Hit For The Police And A Film Starring Steve Martin?,Roxanne,General,If a doctor gave you an Ishihara test what is he testing for,Colour blindness +General,Almonds - the nuts - are members of what general family,Peach,General,Company what is the former name of the tonka metalcraft company,Mound metalcraft,General,Koinoniphobia is the fear of,Rooms +General,How long did the thirty years war last,Thirty years 30 years,General,Ignatius Loyola founded which organisation,Jesuits,Geography,What is the largest island in the Indian Ocean? ,Madagascar  +General,For which team will Jensen Button be driving in the forthcoming Formula 1 World Championship motor racing season,Benneton,Food & Drink,Cocktails: Cognac (brandy) and white creme de menthe make a(n) _____________.,Stinger,General,What dictator was the first to be abducted prosecuted USA drugs,General Manual Noriega- Panama +Music,"""Word Up"" Was A Hit For Which Band In 1986",Cameo,General,What two colours are blood cells,Red & white,General,What is the largest dinosaur,Brachiosaurus +General,Who wrote 'a christmas carol',Charles dickens,General,"What did people desperately tried to avoid getting on ""Press Your Luck?""",The Whammy,General,Collective nouns - a spring of what,Pheasants +General,What is a group of this animal called: Antelope,Herd, Geography,Is Dublin in Northern or Southern ireland?,Southern,General,If you were in Lou Grants office what city are you in,Los Angeles +General,Whose original back up group were The Blue Moon Boys,Elvis Presley,General,Mens world championships started 1903 but 1934 women what,Gymnastics,Geography,What is the capital of Mozambique,Maputo +General,What kind of tree did James Markham obtain a patent for,Peach,Science & Nature,What color spots has the common ladybird ,Black ,Science & Nature,Snakes are reptiles. What are frogs?,Amphibians +General,What healthful practice is chewing an acacia twig a substitute for in India,Brushing teeth,General,Who won Best Actress for her role in Hud,Patricia neal,Art & Literature,A russian abstract movement begun in the early twentieth century. It employs an analytic vision based on fragmentation and multiple viewpoints.,Cubism +General,Which region of France do Germans call Lothringen,Lorraine,General,What celestial body gets its name from the Greek long haired,Comet,People & Places,Who Was the First Woman To Be Officially Ranked A Billionaire In The USA? ,Oprah Winfrey  +General,Who was king of England from 1422 until 1461,Henry Vl,General,What was the name of the first U.S. atomic submarine,Nautilus,General,What is a group of turkeys,Rafter +Science & Nature," In Pakistan, goats are often sacrificed to improve the performance of the __________",Stock market,General,If an Italian was having Pranzo what would they be having,Lunch,General,Which level of the Earth's atmosphere is the closest layer to its core?,Hydrosphere +General,"Which novel begins ""The family of Dashwood had been long settled in Sussex""",Sense and sensibility,General,On which river does Berlin stand,Spree,General,A word like 'NASA' formed from the initials of other words is a(n) _________.,Acronym + Geography,Name the longest river in Nigeria.,Niger,General,Augusto Pinochet was the ruler of which country,Chile,Food & Drink,"Which Food Has A Name, Which Literally Means On A Skewer? ",Kebab  +General,Who developed the method school of acting,Konstanstin Stanislavsky,General,From what plant is opium derived,Poppy,General,What is the flower that stands for: assiduous to please,Sprig of ivy with tendrills +General,"Country in central Europe, bounded on the north by the North Sea, Denmark, & the Baltic Sea; on the east by Poland & the Czech Republic; on the south by Austria & Switzerland; & on the west by France, Luxembourg, Belgium, & the Netherlands",Federal republic of germany,General,Mottephobia is the fear of,Moths,General,The pop group Satan's Jesters found fame under what name,The Rolling Stones +Music,Who had a Number 1 in 1999 with the song 'Fly Away'?,Lenny Lravitz, History & Holidays,"Which U.S. president said, ""The buck stops here""?",Truman,General,What show was a spin-off of Transformers?,Go-Bots +General,She is the Chinese name of what year (animal),Serpent,General,Which sportswear company is named after a Greek Goddess,Nike,General,A biography written by the subject is called a ______,Autobiography +Geography,What Is The National Airline Of Greece ,Olympic Airways ,General,Which famous building in New York lights up a red heart each St Valentine's Day ,Empire State Building ,Music,Teardrop Explodes Was Fronted By Who,Julian Cope +General,A structure that forms the arms of a cross-shaped church.,Transept,General,In 1965 they urged you to keep on dancing,The gentrys,General,What was the profession of the character who heard the Blow Out,Horror film +Food & Drink,What sort of fish is a kipper? ,Herring ,General,Ilium is the Latin name for what ancient city,Troy,General,The Welland Canal links Lake Erie to which other of the Great Lakes,Lake ontario +General,How many hours a day does a ferret sleep,20,General,The human body contains enough carbon to make ______,900 pencils,General,Which Comedian Used To Have A Cemedy Partner Known As Fanny The Wonderdog,Julian Clary +General,Any month that starts on a Sunday will have a ______________ in it,Friday the 13th,General,In which country is the style of beer called LAMBIC brewed,Belgium,Food & Drink,In The World Of Music How Are 'Cheryl James & Sandra Denton'' More Commonly Known ,Salt & Pepper  +Science & Nature,What is a shark's skeleton made of ,Cartilage ,Music,What Were The 2 Top Ten Hits For Actor Turned Singer Bruce Willis,Respect Yourself & Under The Boardwalk,General,Deuteronomy what is the capital of illinois,Springfield + Geography,"Where is the land of 10,000 lakes?",Minnesota,Food & Drink,Which drink is served in a Schooner? ,Sherry ,General,The Shining Path is a revolutionary movement in which country,Peru +Science & Nature, All mammals have __________,Tongues,General,What is the worlds largest airline,Aeroflot,General,"In an average lifetime, the average American ___ 410078 times",Laughs +General,What is another name for Plexiglass,Perspex,General,Who was the first woman to swim the English channel in both directions,Florence chadwick,General,Slang:A promiscuous woman,Slapper +General,Androphobia is the fear of,Males,General,To which U.S. state would you travel to sample authentic Cajun cooking,Louisiana, History & Holidays,1955 saw the first ever Disneyland open in which U.S. state ,California (Annaheim)  +General,What countrys national flower is the wattle,Australia,General,What shop outnumbers MacDonald's 3 to 1 in the USA,Adult Bookshops,Science & Nature,What Is The Largest Living Bird? ,The Ostrich  +Music,"Which 60's British Record Label Used The Slogan ""Proud To Be Part Of The Industry Of Human Happiness""",Immediate,General,In what village do Tom Sawer and Huckleberry Finn live,St Petersburg,General,What nationality was tennis player Michael Chang,American +General,What is the only duty of police Gracthenvissers in Amsterdam,Motorists in canals,General,Dantes Inferno what crime was done by those in the lowest level,Betrayal,General,Vladamere Ashkenazy plays what musical instrument,Piano +General,"Whose big band's signature tune was ""One O'Clock Jump""",Count basie,General,Who was the female star of 'The Graduate',Anne bancroft,General,How long does it take for sunlight to reach earth,Eight minutes +General,Who composed and played the score for the film Genevieve,Larry Addler,General,"What allowed mexican, andean and some north american indians to hurl their spears a great distance",Atlatl, History & Holidays,What was the name of the show that featured Sniglets? ,Not Necessarily The News  +General,"What does ""Rx"" mean to a pharmacist",A prescription prescription,General,Pigs Eye was the original name of what Minnesota city,St Paul,Science & Nature,What Sort Of Animal Was Tarka ,An Otter  +General,What is the full name of Batman's butler,Albert Pennyworth, History & Holidays,In what war did the jet fighters first battle each other?,The Korean War,General,What is a group of gulls called,Colony +General,What was the name of princess leia's home planet,Alderon,General,"Of what country did Napoleon make his brother, Louis, king",Holland,Music,What Song Features The Lyric “ Stars In Your Eyes Little One Where Do You Go To Dream,Land Of Make Believe +Science & Nature,This comet appears every 76.3 years.,Halley,General,Who invented the difference engine,Charles babbage,General,What is the super bowl trophy called,Vince Lombardy trophy +General,Who led the children of israel out of egypt,Moses,General,"Which Eastender Provided The Voice Of ""Dipsy"" In The Teletubbies",Rudolph Waker / Patrick Truman,General,"In the recent television series Batman, who played the part of the siren",Joan collins +General,A person who sells fruit etc. from a barrow,Costermonger,General,Bunyon wrote Pilgrims Progress - where,Bedford Jail, History & Holidays,"Which word, associated with Christmas comes from a Greek word meaning 'we can act anything'' ",Pantomime  +General,What nationality is golfer Paul Azinger,American,General,Of which Spanish province is Seville the capital city,Andalucia,Science & Nature,Which archaic imperial unit of measure is equivalent to 54 gallons? ,A Hogshead  +Toys & Games,Large plastic animals gobbled marbles in this game.,Hungry hungry hippos,Science & Nature," Not all leeches are bloodsuckers. Many are predators which eat earthworms, etc. The nearest relatives of leeches are __________",Earthworms,General,"This is the animal that never learns, ""Trix are for kids!""",Rabbit +General,Which car company tried harder because they were number two,Avis,Art & Literature,What Was The Only Novel To Be Written By Margaret Mitchell ,Gone With The Wind ,General,Where on the body is the vena cava,Heart +Sports & Leisure,In What Make Of Car Did Michael Schumacher Make His Formula One Debut In 1991 ,Jordan ,General,Who said I have had a talent for irritating women since I was 14,Marilyn Munroe, History & Holidays,What Is The Name Given To A Group Of Witches ,A Coven  +Art & Literature,Sherlock Holmes lived at 221b _____ street?,Baker,Music,Which Radio Station Bought XFM In 1998,Capital Radio,General,George Simenon created Maigret - what nationality was he,Belgian +General,What's the name of the largest area of the brain,Cerebrum,General,In what shaped ring does sumo wrestling take place,Circular,Geography,What Mountain Range Seperates Europe & Asia ,The Urals  +General,What island country do Cyprians call home,Cyprus,General,63% of Americans spend five minutes a day looking for what,TV remote control,Science & Nature," According to experts, __________ don't like to head straight for anything. For safety, they may run past and sweep around from the side.",Squirrels +Music,In 1975 Which Female Singer Was Officially Crowned The Queen Of Disco By The Mayor Of New York City,Gloria Gaynor,General,In 1989 michael bolton won a grammy with this song that asked a question,How,Food & Drink,What is the main flavour in the following alcohols 'Cassis'? ,Blackcurrant  +General,What American state has the most outhouses,Alaska,General,"The first Modern Olympics were held in Athens in 1896, where in 1900 were the second games held",Paris,General,Until 1998 by law The QE hotel must do what if you rent a room,Feed your horse freely +Science & Nature,What Do The Initials MG Stand For ,Morris Garages ,General,What is the name of harvard university's satirical newspaper,Lampoon,General,Aruba is an island under which kingdom,Netherlands +Science & Nature,"What was the claim to fame of laika, the russian bitch ",The first dog in space ,General,Which U.S. state is nick named 'the First State' because it was the first to ratify the American Constitution in 1787,Delaware,General,Phthiriophobia is the fear of,Lice +Geography,"Formally called Kiritimati, ____________________ in the Indian Ocean is 52 square miles.",Christmas island,General,What is a female deer,Doe,General,When was the gas turbine invented,1849 +General,"In ""Running Scared,"" how much money does Billy Crystal's character inherit from his dead aunt Rose?",50000,General,What do enzymes in a beer's mash convert the starch into,Sugar,Music,Which Style Of Jazz Was Pioneered By Charlie Parker & Dizzy Gillespie,Bebop +General,What is the richest natural vegetable food,Soya bean,General,Which eighties fashion accessory consisted of a saftey pin and small beads?,Friendship pins,Music,"From the 1980's name the song and artist “Cowboy No. 1, A born-again poor man's son, On the air America, I modelled shirts by Van Heusen-yeah”",Frankie / Two Tribes +Music,"Which Songwriters Connect David Bowie, Chris Farlowe , Melanie & Marianne Faithful",Mick Jagger & Keith Richards,General,What do the words 'par avion' on the outside of an envelope mean,Airmail,General,In Greek mythology who rowed the dead across the river Styx,Charon +General,What did Vasco Nunez de Balboa discover when he crossed the Panama in 1513,Pacific ocean,General,Who was responsible for the American style of spelling ?,Noah Webster,General,"While experimenting with speeding up a tape, david seville created three voice characters and released a christmas song in 1958. which nutty group was this",Chipmunks +General,"Who composed ""Invitation to the Dance "" in 1819",Weber,General,"Whuch Band Before Becoming Famous Used To Go By The Name Of ""Feedback""",U2,Music,What was the original name of the group 'Chicago'?,Chicago Transit Authority +General,What branch of mathematics is named for the Latin for pebble,Calculus, History & Holidays,This racist organization was formed in Tennessee in 1865.,Ku Klux Klan,General,What is the shortest French word with all five vowels,Oiseau +General,What is the name for the group of men who elect a Pope,College of Cardinals,General,"What's the largest island in the British Isles, with 84,200 square miles",Great britain,Music,Which Member Of Boyzone Has Had solo Hits With “When You Saying Nothing At All” & “If Tomorrow Never Comes”?,Ronan Keating +General,Which American female vocalist had a hit in 1985 with 'We Don't Need Another Hero',Tina turner,General,What is the Capital of: Australia,Canberra,General,What was the name of the drug used as an anti-cancer agent and extracted from the blue periwinkle,Vincristine +General,How many teeth does a walrus have,Eighteen,General,Which city in Central Alabama was the first capital of the Confederacy (1861) during the Civil War,Montgomery,Food & Drink,What is Japanese Drink 'Sake' Made From ,Rice  +Music,"Who Started The 70's On A Sober Note With ""When I'm Dead And Gone""",McGuiness Flint,General,"The Film ""10 Things I Hate About About You"" Is Based On Which Shakespeare Play",The Taming Of The Shrew,Music,Who Had Love In Their Tummy With Yummy Yummy Yummy,Ohio Express +General,The screw was invented after the _______,Screwdriver,General,What national flag has the largest animal emblem - a lion,Sri Lanka,General,"The ""Oyster"" watch is a famous model produced by what top Swiss watchmaker",Rolex +Music,Whose debut album was called Different Class?,Pulp, History & Holidays,Also In 1968 Which British Coin Was Introduced To Replace The Ten Shilling Note ,50 Pence Piece ,General,The human body contains enough of what to kill all the fleas on an average dog,Sulphur +General,Ichabod mudd was what to captain midnight,Mechanic,General,"Any substance that produces disease conditions, tissue injury, or otherwise interrupts natural life processes when in contact with or absorbed into the body",Poison,General,Lily Cauchoin became famous as who,Claudette Colbert +General,Britain's first _______ was installed in Harrods in 1878,Escalator,General,Who was the giant wife of the Norse god Njord,Sonja, History & Holidays,In which year did the Queen make her infamous 'Annus Horriblis'' Christmas broadcast ,1992  + History & Holidays,Who killed who in 1963 in what is generally regarded as the first live televised murder? ,Jack Ruby killed Lee Harvey Oswald ,General,What kind of car was Kitt in Knight Rider,Pontiac Trans Am,General,In Frankfort Kentucky its illegal to shot what off a policeman,His Tie + Geography,What is the capital of Nicaragua?,Managua,General,What language (not dialect) has the most characters in it,Cambodian,General,Jacinth or Hyacinth are alternative names of what mineral,Zircon +General,Who's Auto Biography Is Entitled “Polly Wants A Zebra”,Michael Aspel,General,In which year did three German emperors reign?,1888,General,Crystallite is used in what sport,Snooker - Pool balls made from it +General,"What's short for ""light amplification by stimulated emission of radiation""",Laser,General,A shroff is an expert in what,Testing coins,General,"In the film 'titanic', who did leonardo dicaprio and kate winslet play",Jack +General,Japanese Soya noodles are made from what,Buckwheat,Technology & Video Games,Who invented the Telephone ?,Alexander Graham Bell,Technology & Video Games,What country did the operating system 'Linux' come from?,Finland +General,Reed Richards was a member of which Superhero team?,Fantastic Four, History & Holidays,Who got slimmed first in Ghostbusters? ,Peter Venkman ,Music,"Which Over The Top Performer Played Piano On The 1986 Wham Hit ""Edge Of Heaven""",Elton John +General,What tree is mentioned just once in the Bible,Poplar,Sports & Leisure,In Which Sport Do Players Sweep Ice ,Curling ,General,Red flags flown by French ships - Joli Rouge origin of what name,Jolly Rodger +General,"On maps, what is the technical name for the 'you are here' arrow?",Ideo locator, History & Holidays,What Event Killed On In Four People In Europe In The Fourteenth Century? ,The Black Death Or Bubonic Plague ,General,The yo yo both the toy & its name originated where,Philippines + History & Holidays,Which group released the album 'Dark Side of the Moon' ,Pink Floyd ,General,Which californian desert drops below sea level,Death valley,Science & Nature,What is the chemical symbol for tungsten?,W +General,In Greek mythology what event did Paris trigger when he took his lover Helen home with him,Trojan war,General,The dog breed borzoi gets it name from the Russian for what,Fleet or swift,General,Atephobia is a fear of what,Imperfection +Sports & Leisure,Which Football Manager Was Reprimanded In 2006 after Criticising Female Officials? ,Mike Newell ,General,Cat's _____ glows under a black-light,Urine, History & Holidays,Which English Monarch used radio to start the tradition of The Christmas Broadcast ,George V (1932)  +General,Who sang 'all i want to do',Sheryl crow,General,Who was the American psychologist known for his experiments with hallucinogenics in the 60s,Timothy Leary,Science & Nature,Horse Is To Equine As Pig Is To What? ,Porcine  +Music,Name 3 Of The Four mebers Of Culture Club,"Mickey Craig, Boy George, Roy Hay, Jon Moss",General,What is the currency of ecuador,Sucre,General,Only dead people can be on US postage stamps except who,Astronauts +General,In the USA on January 1 you have the greatest risk of what,Being Murdered,General,The word 'hacienda' comes from which language,Spanish,General,What does the term 'dj' mean,Disc jockey +General,Popular 1970's tv series '_____ and the man',Chico,General,Who marched his elephants through the Pyrenees & the Alps in 218 b.c.,Hannibal,General,Which classic British car company of the 1940s and 1950s now produces armoured vehicles for the British military,Alvis +Geography,Artificial PKN Fertilisers Were Introduced In Britain In 1926. What Are The Constituent Parts ,"Phosphorus, Potash & Nitrogeon ",General,Each day 3000 Americans do what for the first time,Start Smoking,General,Which Popular Fictional & TV And Movie Characters Live At 118 West Wallaby Street,Wallace & Gromit +General,Majorca belongs to which island group,Balearic Islands,General,America what is the second most common word said before die,Shit,General,What is a figure with eight equal sides called?,Octagon +General,To which family of fishes does the Sprat belong,Herring,Music,Which Festival Was Notoriously Captured On Film In Gimme Shelter,Altamont,General,"________ was the first feature length production which was created by Walt Disney feature Animation, Florida, which is located at Disney/MGM Studios at Walt Disney World.",Mulan +General,What country's border would an Azerbaijani reach by hiking due south across rhe Talish Mounains,Iran's irans iran,General,Which winter game is known as the roaring game,Curling,Science & Nature,"In 1905, Albert Einstein wrote that E=mc2. What theory is commonly associated with this equation? ",The Theory Of Relativity  +General,What is the unabomber's real name,Ted kaczynski,Art & Literature,Who Wrote The Murder Of Roger Ackroyd ,Agatha Christie ,Sports & Leisure,What Numbers Are Either Side Of 20 On A Dartboard ,5 And 1  +General,Douglas Adams said what is the best spacecraft propellant,Bad News - it travels the fastest,Entertainment,Charles Boyer inspired a cartoon skunk. Who,Pepe le pew,General,What French city is famous for its champagne,Reims +General,Baseball: the Milwaukee _______,Brewers,General,Lovely Rita meter maid appeared on which Beatles album,Sergeant Peppers,Geography,The official state musical instrument in South Dakota is the _____________,Fiddle +General,What's the capital of Senegal,Dakar,Mathematics,If you cut through a solid sphere what shape will the flat area be?,Circle,General,What musical show discovered leslie uggams,Sing along with mitch +General,"Candy named for Mars and Murrie, the company's founders.",Mms,General,Which Countries Men Use The Largest Amount Of Deodorant,Japan,General,The clavicle is more commonly known as which bone?,Shoulder blade +General,What is a twenty five year anniversary,Silver anniversary,General,What large sea is between Europe and Africa,Mediterranean,General,What was the first animal on the endangered species list,Peregrine falcon +General,Windor Castle Employs A Fendersmith What Is The Job Of A Fendersmith,Tends & Lights The Fires,Art & Literature,How many books are there in Anne Rice's vampire series?,Five,General,Which song was a hit for Elvis Costello in 1979,Oliver's army +General,"Who was the attorney who defended jack ruby, oswald's assassin",Melvin belli,General,"Maiden, Mother of All, Footman all parts of what",A Spinning wheel,General,In the English legal system how many judges form a quorum in the Court of Appeal,Three +General,Who were the legendry founders of Rome,Romulus and remus,General,What novel did Daphne du Mauruer write about Cornish Shipwreckers,Jamaica inn,General,"Which Shakespeare Play Features The Line ""All The Worlds A Stage And The Men And Woman"" Merely Players",As You Like It +General,"Which former drama critic of the Observer devised the revue ""Oh Calcutta""",Kenneth tynan,General,A cough releases an explosive charge of air that moves at speeds up to how many miles an hour,60,General,"Other than england, which european country took part in the 1996 cricket world cup",Netherlands +General,Coq Bang can be found in which country,Vietnam,General,Who named a city after his horse Bucephalus,Alexander the Great,General,What film found bruce willis at 'flotsam paradise',Fifth element +General,"After Caesar, who did Cleopatra woo",Mark antony,General,In downtown Lima Peru there is a brass statue of who,Winnie the Pooh,General,"Who was nicknamed ""Joltin Joe""",Joe dimaggio +General,Who won the World Series in 1987,Minnesota twins,General,Who won the world soccer championship in 1974,West germany,General,Which country is indicated by the car identification letters WAL,Sierra leone +General,"Carole king sings 'you just call out my name, and you know wherever i am i'll come running ..'. what is the song title",You've got a friend,General,"What are Cobol, Fortran and Ada types of",Computer languages,General,Which city is served by Ringway Airport?,Manchester +Music,Mike Oldfield Had A Shadow Of Light Or A Moonlight Shadow,Moonlight Shadow,Sports & Leisure,How Long does a game of field hockey last ,70 Mins , Geography,What country has the biggest population?,China +General,"What does the abbreviation BP, used in hospitals, stand for",Blood pressure,General,Of which country was Andreas Papandreou prime minister,Greece,Music,Which UK Number One By Marvin Gaye Was Written By Norman Whitfield & Barret Strong,I Heard It Through The Grapevine +General,Prospective Italian grave diggers have to take what test,Exhume - dig up a body, Geography,What is the basic unit of currency for Bahamas ?,Dollar,General,Which song was released by Billy Joe Royal and written by Joe South,Down in +Science & Nature,Which Italian Companys Emblem Features A Raging Bull ,Lamborghini ,General,Which Band Have Spent The Most Weeks In The UK Singles Charts In One Year With An Incredible 134 Weeks (2008),Oasis,General,What in Japan is a Mawashi,Sumo wrestlers belt +Music,Name 2 Of The 4 Original Members Of The Ramones,"Joey, Johnny, Dee Dee & Tommy",General,What is a group of herons,Darkness,General,Who was alexander the great's wife,Roxana + History & Holidays,What was the name of the government newspaper in ancient Rome ?,Acat Diurna (Daily Happenings),Sports & Leisure,The name of the only weapon in women's fencing?,Foil,General,What gemstone was reputed to heal eye ailments,Emerald +General,What country was once named New France,Canada,General,How many days can a us tourist stay in south korea without a visa,Fifteen,General,Which was the first animated full-length cartoon,Snow white +Science & Nature,What element has the periodic table name Sb ?,Antimony,General,What name did Octavius adopt on becoming the first Roman Emperor in 27 B.C.,Augustus,General,Franz Halls The Laughing Cavalier - what's the paintings real title,Portrait of a man +Science & Nature, Minnows have teeth in their __________,Throat,Food & Drink,"Cocktails: Bourbon, sugar and mint make a(n) ___________.",Mint julep,General,What job does the Gaffer do in the film industry,Chief Electrician + History & Holidays,Where were the first books printed?,China,General,What does RAMDAC stand for,Random access memory digital to analogue,General,"What product built hershey, pennsylvania",Chocolate +Geography,The highest point in Pennsylvania is lower than the lowest point in ______________,Colorado,General,Operation Urgent Fury was the US invasion of where,Granada,General,Who Was Boxing's First Ever Black World HeavyWeight Champion,Jack Johnson +General,Where is the house of seven gables located,Salem,General,What is the fear of fears known as,Pantophobia,Science & Nature, A cow can't __________ until she's given birth to a calf.,Give milk +Food & Drink,Which village in County Antrim gives its name to a brand of Irish whiskey produced there and at a companion distillery in Coleraine? ,Bushmills ,General,"In The Early 1980's Which Television Personality Was Briefly A Backing Singer For The Band ""Dawn Chorus"" And The Blue Tits",Carol Vorderman,Music,Who guested with Dire Straits on the Money For Nothing track?,Sting +General,What is a group of squirrels called,Dray,General,What actor was freshman college roommate to Al Gore,Tommy Lee Jones,General,Barrel sizes - there are 216 gallons in a what,Tun +General,"What was the name of the Other short-lived spinoff of ""Three's Company""","""Three's a Crowd""",General,"This sport gave us the term ""Hang Ten.""?",Surfing,General,The first what was called The Original,Purpose built lifeboat +General,"What is Dulce Base, New Mexico infamously associated with?",Secret Alien/Human Base for Experiments on Humans,Sports & Leisure,In which city is the Hockey Hall of Fame located?,Toronto, Geography,What is the basic unit of currency for Syria ?,Pound +Geography,What river is Liverpool on,Mersey,Science & Nature,What Trick Does The Mirror Orchid Play On The Male Bee ,Flower Resembles A Female Bee ,General,"What ship was found drifting in 1872, but all the crew had disappeared",Marie celeste +General,Where did Napoleon suffer his final defeat,Waterloo,Food & Drink,Approximately How Many Coffee Beans Make Up An Expresso Measure ,40 ,General,"In the TV series The Six Million Dollar Man, what kind of man was Steve Austin",Bionic +General,"Actress Fenella Fielding's late brother was a famous comedian, what was his name",Marty feldman,Music,"Who Sang Songs About Rosie, Caroline & Desiree","Neil Diamond (Cracklin Rosie, Sweet Caroline, Desiree)",General,Which month was named after julius caesar,July +Music,Hand On Your Heart Was A Hit In 1989 Name The Singer,Kylie Minogue,General,"What does the acronym ""cpu"" stand for",Central processing unit,General,"In alphabet radio code, what word is used for 'c'",Charlie +General,How long is a diamond anniversary,Fifty years,General,What was Hilary Clinton's maiden name,Rodham, History & Holidays,"Which Bill Haley song, which featured in the 1954 film _The Blackboard Jungle_, became the U.K.'s first Rock and Roll number one ",Rock Around The Clock  + History & Holidays,In Sabrina The Teenage Witch What Is The Name Of Sabrina's Cat ,Salem ,General,Which 19th century Russian chemist formulated the Periodic Table of Elements,Mendeleyev,Food & Drink,From what is the liqueur kirsch made?,Cherries +General,How many hurdles are there in a women's hurdle sprint,Ten,Music,"Whose Greatest Hits Album Was Entitled ""Don't Bore Us With The Chorus""",Roxette,General,Which state forms an enclave at the heart of the city of Rome,Vatican city +General,Which Fashion designer launched the 'Space Age Look' in the 1960's?,Pierre Cardin,General,Specifically ( And You Must Be Very Specific ) If You Comitted The Crime Of Uxoricide What Very Naughty Thing Have You Done?,Killing Your Wife,General,Who was abdul kassem ismael in tenth century persia,Grand vizier of persia + History & Holidays,Who was the founder of Live Aid? ,Bob Geldof ,General,Lucius Tarquinius Superbus was the last king of where,Rome,General,What was the worlds first televised murder,Ruby killing Oswald +General,In which 1949 film does Alec Guinness portray 8 members of the D'Ascoyne family,Kind hearts and coronets,General,By Law Lexington Kentucky what can't you carry in your pocket,Ice Cream,General,For what is the Italian town of Carrara world famous,Marble +General,If you dial 123 in the UK what service do you get,The speaking clock,General,Who opened the circus 'The Greatest Show on Earth' in Brooklyn in 1871,Phineas barnum,General,"Which boxer's nickname was ""Smokin' Joe""",Joe frazier +General,Dodie Smith wrote what book (later filmed by Disney),101 Dalmatians,General,What bit of Bobby Goldsboro syrup focused on a dying young wife?,Honey,Music,Which Song Gave Hanson A Transatlantic No.1 In 1997,Mmmm Bop +Sports & Leisure,In which sport must the ball have a diameter of at least 42.67 millimetres and weigh no less than 45.93 grams? ,Golf Ball ,General,What is Chinese checkers played with,Marbles,Science & Nature,What Do The Initials DB Stand For In Conjunction With Aston Martin ,David Brown (1 Time Owner)  +Science & Nature, A __________ cannot jump if its tail is lifted off the ground. It needs its tail for pushing off.,Kangaroo,General,And he was born in which country,India,General,Medicinal root of a plant found in E.Asia and N.America,Ginseng +General,Blanco Gaucho Excelsior Nutcracker Cassette types of what,World Cheeses,Music,"Which Bands Reunion Album Was Titled ""Hell Freezes Over""",The Eagles,General,How many times was Joe diMaggio named most valuable player,Three +General,"Aarchie Moore, was world champion in what sport from 1952 1962",Boxing,General,"On the london underground, which station has a different name on two of its platforms",Bank and monument,General,The commander of the Apollo 11 command module & the first woman shuttle commander share this same last name,Collins +General,Which dog is named for the German word for muzzle,Schnauzer,General,Who had a hit with 'Stand By Your Man',Tammy wynette,General,What are the two main ingredients of a Lyonnaise sauce,White wine & onions +General,What was the name of the dog owned by nick and nora charles in the thin man,Asta,General,What American Indian tribe currently has over 300000 members,Cherokee,Food & Drink,What color is a Remy Martin bottle,Green + History & Holidays,Which Actress Played Emma Peel In The Avengers ,Diana Rigg ,General,What kind of 'mate' produces a tie in a chess game,Stalemate, History & Holidays,What Is Traditionally Supposed To Happen On Halloween ,Witches Come Out  +General,Who was Dan Dare's greatest enemy in the Eagle,Mekon,General,What was the name of Facts of Life's Mrs. Garret's gourmet food shop?,Edna's Edibles,General,What are truffles - highly prized as food,Fungi +Music,Which 3 Surnames Name The Group Responsible For The Resurrection Shuffle,"Ashton, Gardner And Dyke",General,What australian city had the country's first steam and electric trains,Melbourne,Science & Nature,What Do The Initials IATA Stand For ,International Air Transport Association  +General,What is a south african coin containing 1 troy ounce of gold called?,Krugerrand,General,Which Somerset Maughn novel is considered autobiographical,Of Human Bondage,General,What make and model of car does Nash Bridges drive?,A 1971 Plymouth Barracuda convertible. +General,Which UN Secretary-General was killed in an air crash in 1961,Dag hammarskold,General,Who sang the theme tune to the James Bond film Tomorrow Never Dies,Sheryl crow,General,What releases an explosive charge of air that moves at speeds up to 60 mph,Cough +Science & Nature,What Colour Are The Egg's Of The Redstart ,Usually Blue With Greenish Tinge ,General,Where is the famous Blaze nightclub located?,Melbourne,General,What is the most common atom in the universe,Hydrogen +General,From what group of wild plants are cereals derived,Grasses,General,Arthur Paul designed which icon - appears on a magazine,Playboy bunny, History & Holidays,Who Released The 70's Album Entitled Live at Leeds ,The Who  +Science & Nature,What is the proper name for falling stars,Meteors,General,Who was accused of smuggling $225 million out of the Philippines,Imelda marcos,General,What inside corn makes it pop,Water +General,"Where was the earthquake on september 29, 1969",Tulbach,Technology & Video Games,"In 'Adventure', which castle had a dark maze in it when you played on any but the easiest difficult level? ",The Black Castle,General,Destruction of the natural environment,Ecocide +General,What canadian province was largely taken over by irish rebels for a month in the 19th century,Ontario,General,"Bechemel, espagnole and bearnaise are types of which food",Sauce,General,Who starred in 'city lights',Charlie chaplin +General,What shrinks on some birds to allow them to carry additional food on long flights,Brains,General,And which word comes second,Paris,General,In Arkansas it is illegal to serve what drink at a party,Petrol - Gasoline +General,"Common, English, Flemish, Running and Stack types of what",Brick wall bonds – how laid,General,What is the first name of Mr Toad - in Toad of Toad Hall,Thaddeus,Science & Nature,What branch of science studies the motion of air and the forces acting on objects in air?,Aerodynamics +General,What late motowner was memorialized in the diana ross hit missing you,Marvin,General,"Which sea, with no placename in its name, lies between Korea and Shanghai",Yellow,General,Who was the only survivor of custer's last stand,His horse +General,What is the winter counterpart to estivation,Hibernation,General,Who did henry winkler play in 'happy days',Digital video disc,Science & Nature," The fastest dog, the __________, can reach speeds of up to 45 miles per hour. The breed was known to exist in ancient Egypt more than 5,000 years ago.",Greyhound +General,This city is the capital of Armenia?,Yerevan,General,"On the chinese calendar, the year 2000 will be the year of the what",Dragon,General,Nossa Senhora da Aparecida is Patron Saint of which country,Brazil +General,"What is the first sign of the zodiac, symbolized by the ram",Aries,General,What is the common name for corporations formed to act as trustees according to the terms of contracts known as trust agreements,Trust companies,General,From which animal is 'ambergris',Sperm whale +General, This word is used as the international radio distress call.,Mayday, History & Holidays,*How many people resided in the Brady house on The Brady Bunch?* ,9 , Geography,What is the basic unit of currency for Georgia (country)?,Lari + History & Holidays,Who was the first African-American female to win a Wimbledon Tennis event? In both 1957 And 1958 ,Althea Gibson ,General,What combination of nitrogen and hydrogen is in part produced commercially as a by-product of coal-gas manufacturing,Ammonia,General,What Milton Bradley game wants to make you a millionaire tycoon,Life +General,What head of Government was the first to give birth in office,Benazir Bhutto – Pakistan,General,Only 55% of men do what,Wash hands after toilet visit,General,Which element has the chemical symbol Cs; capital C lower-case s,Caesium +People & Places,Which British Jockey Was Jailed For Tax Evasion ,Lester Piggot ,General,Name Ladysmith Black Mambazo's chart-topping album of 1986,Graceland,General,What ailing founding father was carted to the Constitutional Convention in a sedan chair carried by four prisoners,Benjamin franklin +General,"Infectious virus disease of the central nervous system, sometimes resulting in paralysis?",Poliomyelitis,General,What is the Capital of: Germany,Berlin,General,In 1961 Anton Geesink was the first non Japanese to do what,Win a Judo title +General,The half wit Smike appears in which Dickens novel,Nicholas Nickleby,General,Who married prince albert,Queen albert,General,How many continents must a sport be played on before the IOC will consider making it an Olympic event for men,Four +General,In Australian slang what is a dishlicker,Dog,General,Who wrote the Booker Prize-winning novel Oscar and Lucinda,Peter carey,Music,"In What Year Was The Song ""Hello Goodbye"" At No.1",1967 + Geography,"What lake is approximately 394,000 sq. km in area?",Caspian Sea,General,As what is America Online better known?,AOL,General,"The 1996 Movie ""The Cable Guy"" Was Actually Directed By Which Other Actor / Comedian",Ben Stiller +General,11:37 pm In military time is how many hours,2337,General,What was the earliest known symbol of christianity,Fish,General,How long passed from the making of the first zipper & its marketing,Fifty five years 55 years +General,Where is the space needle,Seattle,General,What was the penalty (in britain) in 1810 for stealing a pocket handkerchief,Hanging,General,What is a group of squirrels,Dray +General,What is a group of ants,Colony,General,There are more bald eagles in what Canadian province then there are in the whole U.S.,British columbia,General,What colour is the danger flag in motor racing,Yellow +Sports & Leisure,Who threw their Olympic Gold Medal in a Kentucky River after being refused a meal at a diner because of his colour ,Casius Clay / Muhamed Ali ,General,In USA 20s Mary 50s Linda 70s Michelle what most pop 90s name,Ashley,Science & Nature," The word ""struthious"" refers to something that resembles or is related to __________",Ostriches +Music,"Who Composed The Rag, The Entertainer Used As The Theme To The Sting",Scott Joplin,General,In the theatre what do the initials FOH stand for,Front of House,General,In which American State is Stanford University',California +General,How Is “ Alberto De Salvo ” Better Known?,The Boston Strangler,General,Which was the first japanese city bombed in 1945,Hiroshima,General,Name the largest gland in the human body.,Liver +General,What are padmasana sirsasana and savasana,Yoga Positions,General,What river were the Joan of Arc's remains cast into,The seine seine,General,"In Greek mythology, who had a ship with a beam hewn from the 'speaking oaks of dodona'",Atlanta +General,If you have a viral infection of the parotid glands what is it,Mumps,General,Under Mississippi law there cannot be a female what,Peeping Tom,Science & Nature,What Type Of Bird Is A Teal ,Duck  +General,The Italian for tail what comes at the end of a musical score,Coda, Geography,Which mountains are regarded as the east border of Europe ?,Ural,General,"Who directed the film ""One Flew Over the Cuckoo's Nest""",Milos forman +General,What is the flower that stands for: poetry,Eglantine,General,Which island is known to its inhabitants as Kerkyra,Corfu,Food & Drink,"What is the name of the turkish dish of vine leaves filled with rice, chopped meat and onions? ",Dolmas/ Dolmades  + Geography,On the banks of which river is the Taj Mahal?,River Jumna,General,Who wrote the Summa Theologica,Thomas aquinas,General,Bib-label Lithiated Lemon-Lime Soda better known as what,Seven Up + History & Holidays,Snarf' and 'Liono' are both characters from what famous 80s cartoon show? ,Thundercats ,Sports & Leisure,Hockey: The Calgary __________.,Flames,General,Who was Bonnie Parker's partner,Clyde barrow +General,Peter Sellers is best known for his role as Inspector _________,Clouseau,General,In Chinese mythology what is Taimut,A Dragon,General,What blonde was the subject of the four most expensive Andy Warhol works sold at auction,Marilyn monroe +General,In what Australian state would you find Moe,Victoria,General,Term applied to a migratory people of southern China,Hakka,Geography,In which city is saint paul's cathedral ,London  +General,What 17th Century pirate ended up a governor of Jamaica,Sir Henry Morgan,General,What would you be doing if you were suffering from somnambulism,Sleep Walking,General,Your eyeballs are 3.5% what,Salt +Sports & Leisure,Name 1 Country Where Badminton Is The National Sport ,Malaysia & Indonesia ,General,What is another name for crude oil,Black gold,General,What is the top selling candy bar from vending machines,Snickers +General,Which two South American countries do not share a land boundary with Brazil,Chile & ecuador,General,Who Has Made The Most Appearnces On Jackanory?,Bernard Cribbins, Geography,"""Yellow River"" is the common name for which Chinese river?",Hwang Ho + Geography,In which continent would you find the Nile river ?,Africa,Science & Nature,Where are the 4 major moons of Jupiter discovered by?,Galileo,General,In 1903 Sir Arthur Conan Doyle published which book,The hound of the baskervilles +General,Who invented the aerosol,Erik rotheim,General,What was built on the site of the old Waldorf Astoria Hotel in New York,Empire state building,General,Which Part Of His Body Did Bob Marley Havwe Surgically Removed In 1977,His Toe +General,What U.S. state includes the telephone area code 614,Ohio,General,"Whose latest album is called ""Forever""",Spice girls,General,What was Richard Bach's best selling book,Jonathan livingston seagull +General,What sort of writing system did the ancient Egyptians use,Hieroglyphics,General,Which Saints day is the 23rd April,Saint George of England,General,Average US male does it in 11.4 mins but female takes 13 what,Shower +General,Which American President Once Worked As A Male Model Before Occupying The White House?,Gerald Ford,Geography,The peacock is the national bird of _________,India,General,Who is the bad boy that leads Pinocchio astray,Lampwick +General,What actor played john wayne's son in red river,Montgomery clift,General,Poseidon was the greek god of the ______,Sea,Music,"Who plays the uncredited lead guitar on ""While My Guitar Gently Weeps""?",Eric Clapton +General,Who wrote the opera Pagliacci,Leoncavallo,General,In Casablanca what is the name of the nightclub,Rick's,Entertainment,"Number of new Supermen after his ""death""",Four +General,Who wrote 'the scarlatti inheritance',Robert ludlum,General,What is the fear of theatres known as,Theatrophobia, History & Holidays,The following is a line from which 1970's film 'It's as big as a house' ? ,Close Encounters of the 3rd Kind  +General,"Other than susan b anthony, which two women have been represented on u.s currency",Martha washington and pocahontas,Music,With Which Football Club Is Noel Gallagher Linked,Manchester City,General,Who allowed the bugging of the democratic committee headquarters.,Richard + History & Holidays,In which Massachusetts town were 20 people executed for witchcraft in 1692 ,Salem ,General,2112' was the first in a long line of gold and platinum albums for which canadian trio,Rush,General,"Of waterloo Common name for the family comprising a peculiar group of spiny, fleshy plants native to America",Cactus + History & Holidays,Which State Became The 47th US State In 1912 ,New Mexico ,General,What colour are mickey mouse's gloves,White, History & Holidays,Who led the attack on the Alamo,Santa anna +General,Who is the Egyptian God of the dead,Anubis,General,A spiral scroll used on Ionic and Corinthian capitals. ,Volute,Entertainment,Who wrote and preformed the soundtrack for Live and let die?,Paul McCartney and Wings +Music,"Besides playing with the Beatles, what was Stu's real artistic ambition?",Painter,Food & Drink, What is the name of the syrup drained from raw sugar,Molasses,General,"What's the international radio code word for the letter ""E""",Echo +General,In Tennessee it is illegal to sell what on a Sunday,Bologna,General,"What geographic entity ""shrunk"" more than 1300 feet in 1980",Mount st helens, Geography,In which modern day country is ancient Troy?,Turkey +General,To which elemetary school did tv's 'the brady bunch' go,Dixie canyon, History & Holidays,Who Released The 70's Album Entitled Machine Head ,Deep Purple ,Science & Nature,Which Mammal Has The Longest Gestation Period? ,The African Elephant  +General,Corporals Henshaw and Barbella report to which sergeant,Sergeant Bilko,Entertainment,"What Procol Harem tune was based on the Bach cantata ""Sleepers Awake""?",A Whiter Shade of Pale,General,What does a mosquito vibrate to make its buzzing sound,Wings + History & Holidays,"`Is this the real life, is this just fantasy', is the opening line of which hit song? ",Bohemian Rhapsody ,General,What is the Capital of: Mauritius,Port louis,General,"China Sun-Fin-Chin, Russia bayan, Norway trekspill what is it",Accordion +Music,"Who Had A Hit In 1988 With The Song ""First Time""",Robin Beck,Music,"Who Group Dismissed Love As ""Just A Silly Phase Im Going Through""",10cc,General,What vitamin is also called ascorbic acid,Vitamin c +General,What is the atomic number for hydrogen (h),1,General,"What word is used in a balance sheet to mean ""Everything a company owns""",Assets,Art & Literature,Which St Louis Born Novelist & Poet Became A British Subject In 1927 ,T S Eliot  + History & Holidays,Which 1993 Disney Movie Starred Bette Midler As A Witch ,Hocus Pocus ,General,What percentage of alcohol is contained in a 100 proof mixture,Fifty 50,General,Frass is the correct word for what,Insect faces - bug shit +General,In contract bridge a hand called chicane has what,No trump cards,General,Which organisation was founded by Nathan Bedford Forrest in 1865,Ku klux klan,Food & Drink,"What is the cocktail called, that consists of four parts of tequila to two parts of lemon or limejuice? ",Margarita  +General,Florence nightingale was known as 'the lady of the ______',Lamp,Sports & Leisure,Who Were The First British Football Club To Have Under Soil Heating? ,Everton ,Music,Who was known as 'The Killer' in the 1950's?,Jerry Lee Lewis + History & Holidays,Which famous Tapestry Commemorates The Conquering Of England? ,Bayeux Tapestry ,General,On a darts board what number is directly opposite 10,Fourteen, History & Holidays,This U.S. President suffered from polio during WWII.,Franklin D Roosevelt +Music,Who Had A Hit In 1988 With I Get Weak,Belinda Carlisle,Music,Whom Would You Expect To Find Being Chase Across The Moors By The Hounds Of Love,Kate Bush,General,"Which group sang the Song ""Hotel California""?",Eagles +General,What was Princess Diana's maiden name,Spencer,General,The american flag first flew over a foreign fort in what country?,Libya, History & Holidays,1970 Saw Which Treaty Between USSR & Germany ,The Moscow Treaty  +General,Peter Parker is the alter ego of which superhero,Spiderman,General,What colour is the number 13 on a roulette wheel,Black,General,Gene Kelly Michael York Joss Ackland all played who,D'Artagan +Science & Nature," The bottle_nosed __________ can dive to a depth of 3,000 feet in two minutes.",Whale,General,What sport do you compete for Currie cup and Ranfurly Shield,Rugby Union,General,"Which writer created Tabitha Twitchet, Babbity Bumble, Mr Tod",Beatrix Potter +General,Mythical race of female warriors,Amazons,General,What is the Capital of: Cape Verde,Praia,General,What is the name of the element whose symbol is Pm,Prometheum +General,What 1994 olympic gold-winning figure skater was an orphan,Oksana baiul, History & Holidays,Where did Bill and Hilary Clinton switch on Christmas lights in 1995?,"Belfast, Northern Ireland",General,Where is the annual all-american soap box derby held,"Akron, ohio" +General,What shape is Farfallini pasta,Small Butterflies,Science & Nature," A cat uses its whiskers to determine if a space is too small to squeeze through. The whiskers act as feelers or __________, helping the animal to judge the precise width of any passage.",Antennae,General,Scooby Do is what breed of dog,Great Dane +General,What did Wilhelm Roentgen discover in 1895,X rays,General,What means both 'hello' & 'goodbye' in Hawaii,Aloha,General,What is a group of colts,Team +General,Who would get an award known as the purple cross,Animal bravery by RSPCA,General,Which two teams automatically qualified for the France '98 soccer world cup?,France and Brazil,General,In which sport do they compete for the Iroquois Cup,Lacrosse +People & Places,Who Was Known As Scarface ,Al Capone , History & Holidays,Who sang 'I Want My MTV' on the Dire Straits song 'Money For Nothing'? ,Sting ,Science & Nature,These essential body cells do not contain nuclei.,Red blood cells +General,Alcoholic beverage made from rice,Arrack,Science & Nature,What is the meaning of the name of the constellation Serpens ?,Serpent,General,What modern dance was supposed to cure a spiders bite,Tarantella +Music,Who had a UK No.1 hit in 1982 with “goodie two shoes”?,Adam And The Ants,General,Where was Room 222,Walt whitman high school,General,Who married john lange,Shania twain +Science & Nature,What is the name for the theoretical end-product of the gravitational collapse of a massive star?,Black hole,Science & Nature, Some species of earthworms in __________ can measure more than ten feet in length.,Australia,General,Back in the Habit' is the sub-title to which film sequel,Sister act +General,What is the only word in the English language that ends in the letters 'mt',Dreamt,General,What is mauna kea,Volcano,Music,"In 1978 Who Had Their First Hit With ""I Lost My Heart To A Starship Trooper""",Sarah Brightman +Tech & Video Games,Who has the world's largest double-decker tram fleet?,Hong Kong,Geography,"__________ was admitted to the U.N. in May 1993, making it the smallest country represented there.",Monaco,Religion & Mythology,"In Greek mythology, who turned Arachne into a spider?",Athena +General,Christopher Proudfoot owns the worlds largest collection of what,Lawnmowers,General,"Does a 'milliner' make & sell flour, hats or windmills",Hats,General,What were the first names of the comedy duo Laurel and Hardy,Stan and oliver +Sports & Leisure,Which Colour Ring Surrounds The Gold Center Of An Archery Target ,Red ,General,Virginia Patterson Hensley became more famous as who,Patsy Cline,Science & Nature, The fastest animal on four legs is the __________,Cheetah +General,Top USA food consumption days - Xmas Thanksgiving and what,Super Bowl Sunday,General,Who was united nation's first general secretary,Trygve lie,General,In New Jersey what can't be sold on a Sunday,Cabbage +Entertainment,The maiden names of which two cartoon characters are Slaghoople and Mcbricker?,Wilma Flintstone and Betty Rubble,General,USA UK and Irish women golfers play for which trophy,Curtis cup,General,Who invented the electric cooking range,Thomas ahearn + History & Holidays,On what date did 'The Wall' fall? ,1989 ,General,What is the oldest brewery in the u.s,Yuengling brewery,Geography,What is the capital of Uzbekistan,Tashkent +General,Which Canadian city was originally called Bytown,Ottawa,Sports & Leisure,Who Was Captain Of England In 1966 When They Won The World Cup ,Bobby Moore ,General,"Annoying, litigious (and possibly duplicitous) owner of Boston Beer Company",Jim koch +General,What is the capital of New Mexico,Santa fe, Geography,What London borough does the Prime Meridian pass through?,Greenwich,General,Which is the second largest city in Norway,Bergen +General,Who was the last surviving signer of the declaration of independence,Charles,General,Brothers Jacob and Wilhelm were librarians and professors of language in 19th Century Germany. What was their surname,Grimm,General,In the US civil war what were graybacks,Body lice +General,Soundtrack(Kelly/Ellis/Hare),Joey b elis & tynette hare,General,What was voted toy of the 20th century,Lego,General,Who controls more than 80% of the world's rough diamond supply,De beers +General,Germany's equivalant to the dollar is ______,Deutchmark,General,What ape is the best acrobat,Gibbon,General,What small region at end of medulla oblongata serves as 'bridge' to brain,Pons +People & Places,Whose Real Name Is 'Cherilyn Sarkisian La Pierre' ,Cher ,General,Which country invented the clothing button in the 13th century,France,General,Ancient mariner who flew too near the sun waering wings attached with wax,Icarus +Science & Nature,Does The Water Run Clockwise Or Anti Clockwise Down A Plug Hole In Britain ,Anti Clockwise ,General,Which mountain range reaches from the Black Sea through Georgia to the Caspian Sea,Caucasus kavkaz,General,By which name is Eric Claudin better known,Phantom of the opera +General,In which French island territory would you find the towns Bastia and Calvi,Corsica,General,What country has the third most satellites in orbit?,France,General,Grevys and Burchells are types of what animal,Zebra +General,"Which Group Were The First Group To Have A UK No.1 From The ""Stock, Aitken & Waterman"" Hit Factory",Dead Or Alive,General,"Who sang of ""great balls of fire""",Jerry lee lewis,General,"Poet Robert Frost & the white mountains, both call this state home",New hampshire +Science & Nature,Which Russian chemist (1834-1907) founded our modern periodic table?,Dmitry Ivanovich Mendeleyev,General,What Chinese city is the world's largest noncapital,Shanghai,General,What was the famous line uttered by an old woman in Wendy's ads?,Where's The Beef? + History & Holidays,"Who Had An 80's Hit With The Song 'Other Woman,' ",Ray Parker Jr ,General,In which American state is Wankers Corner,Oregon,General,What is the fastest bird,Spine tailed swift +Science & Nature,By what chemical process do plants manufacture food?,Photosynthesis,Music,What Rush album cover features rabbits and a magician's hat?,Presto,General,"What did Dr Samuel Mudd do that your inspired ""name is mud""",Treated J W Booth Life imprison +General,Who didn't invent the passenger lift in 1852 but did invent a safety device which made them safe enough for general use,Elisha g. otis,General,Who played Johnny Yuma in the series The Rebel,Nick adams, History & Holidays,"According to the bible, who are Gaspar, Balthazar and Melchior ",Three Wise Men  +General,What were king arthur's knights called,Knights of the round table,General,Who is the patron saint of Gypsies,St Sarah,General,What did John F Kennedy claim was his biggest mistake as president,Bay of pigs invasion +General,Capital cities: Finland,Helsinki,Entertainment,Which British group holds the record for the album to remain in the US Billboard charts for the longest time?,Pink Floyd, History & Holidays,Name the original eight reindeer from the 'Twas the night Before Christmas' poem. ,"Comet, Cupid, Dasher, Dancer, Prancer, Vixen, Donner, Blitzen (or Dunder and Blixem) " +Music,"Which Ex Spice Girl Had A Hit With ""Lift Me Up""",Geri Halliwell,General,Lamb where were the 1956 summer olympics,Melbourne,Geography,Where is le figaro published ,Paris  +General,"What kind of animals are impalas, elands & kudus",Antelopes,Sports & Leisure,How many referees work a soccer game?,One,Science & Nature,Who discovered the four largest moons of Jupiter?,Galileo +General,At which battle did General James Scarlett lead the Charge of the heavy Brigade,Balaclava,General,Canberra in Australia has 2 meanings meeting place and what,Female breasts,General,In Oxford university what can you not take into the library by rule,Sheep +General,Where is brest,France,General,What two seasons do the equinoxes occur in,Spring & autumn,Toys & Games,"In roulette, what number is green?",Zero +General,Which famous building was built by Shih Huang Ti ?,Great Wall of China,General,What Is The Best Selling Copyright Book Of All Time?,The Guiness Book Of Records,Science & Nature,As what is haemophilia also known?,Royal disease +General,What song did Rick ask Sam to play in Casablanca,As Time Goes By,General,Collective nouns - A tiding of what,Magpies,Music,What is the real name of singer Meatloaf?,Marvin Lee Aday + History & Holidays,Which Character On TV Has The Real Name Of Eddie Fitzgerald ,Cracker ,General,Which Nobel Prize is not awarded annually in Stockholm,Peace,General,What is the square root of 81,Nine +General,Boccaccios collection of ten stories are known as what,Decameron,Entertainment,Who played Bobby Ewing in the TV series 'Dallas'?,Patrick Duffy,General,Who does the voice for yoda in the star wars films,Frank oz +General,Sir Francis Drake named it New Albion what is it today,Oregon USA,Science & Nature,"Native To America , What Type Of Bird Are Lewis's Red Bellied , Ladder-Backed & Nuttall's ",Woodpeckers ,General,How Are Tom Rowlands & Ed Simmons Better Known?,The Chemical Brothers +General,The Easter lily is a native plant of which country,Japan, Geography,What is the basic unit of currency for Guyana ?,Dollar,General,Who was the King of Swing,Benny Goodman +General,"In the 1983 movie ""National Lampoon's Vacation,"" where were the Griswolds headed on their cross country trip?",WalleyWorld,General,Capital of Scotland,Edinburgh,General,At which American University were four students shot dead in 1970 whilst protesting against the Vietnam War,"Kent state, ohio" +General,In Lynch Heights Delaware its illegal to do what in an airplane,Sneeze,General,What is a group of crocodiles,Bask,General,What US state has no motto,Alaska +Music,"Whose Debut Solo Album Was ""I Got Dem Kosmic Blues Again Mama"" In 1969",Janis Joplin,Geography,What island is pearl harbour on ,Ohau ,General,What dance is usually performed to Orpheus in the Underworld,Can Can +General,What is the capital of the Australia's Northern Territory,Darwin,General,Where would you experience serious pain if someone dropped a concrete block on your hallus,Your big toe big toe,Science & Nature," There are 1,600 known species of __________ in the world.",Starfishes +General,Aulophobia is a fear of what,Flutes,General,Which actor has played Fagin on both the stage and in the 1968 film Oliver!,Ron moody,General,Which Fictional Character Played On Screen Has A Relative Named Aunt Lucy,Paddington Bear +General,Where is angel falls,Venezuela,Art & Literature,Where Might You Find The Museum Of Modern Art ,New York ,General,Cocktails: Vodka and lime juice make a,Gimlet +General,Who recorded 'a boy named sue',Johnny cash,General,In What Country Did Horse Radish Sauce Originate,Japan,General,"What was the name of the actress who played ""Melonie"" on the show ""Webster and Melonie""?",Heather O' Rourke +General,If you are at Comiskey Park what sport would you be watching,Baseball,General,In Disney's The Lady and the Tramp what kind of dog is 'Lady',Cocker spaniel,General,Eras are divided into units called ________,Periods +General,What was the name of the dog in Fraggle Rock?,Sprocket, History & Holidays,Which chapter of the 'Phantasm' series was entitled 'Oblivion' ,Phantasm 4 ,General,Who followed Henry VIII to the throne,Edward vi +General,Gamophobia is fear of ______,Marriage,General,A young what is called a blinker,Mackerel,General,What is the title of the sequel to the book Gentlemen Prefer Blondes by Anita Loos,Gentlemen marry brunettes +General,Ostraconophobia is the fear of,Shellfish,General,Which US President did Anthony Hopkins play in a 1995 film?,Richard Nixon,Food & Drink,What colour is extra-virgin olive oil ,Green  + Geography,What is the capital of West Virginia?,Charleston,Music,Who Appeared On The Cover Of A June 98 NME Dressed As A Tiger,Damon Albarn,Sports & Leisure,"Lincoln, East End, Manchester & London Are All Types Of What ",Dartboards  +Entertainment,What was the first cartoon to feature sound?,Steamboat Willie,General,What is the flower that stands for: reward of virtue,Garland of roses, History & Holidays,What Family Live At 1313 Cemetary Lane ,The Adams Family  +General,"A lively social dance popular during the 1930s; it originated at the Savoy Ballroom in Harlem in 1928, where it was known as the Lindy.",Jitterbug,General,Which German city stands on the confluence of the Rhine and Moselle rivers,Coblenz,Science & Nature,A heavenly body moving under the attraction of the Sun and consisting of a nucleus and a tail is a(n) _______.,Comet +General,What kind of bones once stiffened corsets,Whale,General,The worlds first was 69.5 feet long and took a year to make ?,Oil Well,General,In which country are the most flowers bought per capita,Netherlands +General,"Alec Issigonis Is Credited With Designing The Mini, The Morris Minor And Which Other Classic British Car",Austin 1100 / Austin 7,Sports & Leisure,The very first Olympics were part of a festival to honor which God? ,Zeus ,General,Texas prisons have banned death row prisoners last what,Cigarette - bad for their health +General,What do the four quarters of a hot cross bun symbolise,Moon phases,General,What country is home for Europe's largest glacier,Switzerland,General,What berries give gin its flavour,Juniper berries +General,Under which rules was boxing standardised,Marquise of queensberry,General,What job links Kris Kristoffensen and Gene Roddenbery,Both worked as pilots,General,Michael Cain starred as Carter in the film Get Carter. Who plays Carter in this year's remake,Sylvester stallone +General,In which song does frank sinatra sing 'i travelled each and every highway',I,Food & Drink,Which Restaurant Crtic Took Over From Jonathan Meades In The Times ,Giles Coran ,General,What film is about the migration of poor workers from the dust bowl to the california fruit valleys,Grapes of wrath +General,An anemometer measures ____ ________.,Wind velocity,Science & Nature,What is the name given to a group of stars?,Constellation,General,A woman has Hisdoy syndrome what has she got,A Moustache +General,What sound made by people can be almost as loud as the noise of a pneumatic drill,Snore,Science & Nature," A robin has nearly 3,000 __________",Feathers,General,"Vincent Van Gogh sold exactly one painting while he was alive, what was it",Red vineyard at arles +General,What is a group of fish,Shoal,General,What's the only crime that the church would not grant sanctuary,Sacrilege, Language,What does 'alma mater' mean in English?,Bountiful mother +Science & Nature,What Is Ten In Binary Notation ,1010 , History & Holidays,"Child star Jimmy Boyd sang which hugely popular 1950's Christmas song, which was initially banned by the Catholic Church in Boston because it supposedly mixed sex and Christmas? ",I saw Mommy kissing Santa Claus ,General,"In 1933, mickey mouse, an animated cartoon character, received 800,000 of these",Fan letters +General,Where do Grand Prix drivers put their cars at the beginning of a race,Grid, Geography,What is the basic unit of currency for Zimbabwe ?,Dollar,General,What does peritonitis affect,Abdomen +General,What Countries Capital City When Translated Literally Means “ Gods Gift ”?,Bagdad,Music,"Who Had A Hit With ""Rock The Boat"" In 1974",The Hues Corporation,Geography,Which Is The 3rd Most Widely Spoken Laanguage ,Russian  +General,"In the contract that gave cuba freedom from the us, what was required",Permanent us navy base there permanent naval base,General,"What jockey was nicknamed ""Wee Willie""",Willie shoemaker,General,What is a geoduck,Clam +General,What city was martin luther king jr assassinated in,Memphis,General,Hundred kilometres from what do camels protect themselves with three eyelids,Blowing sand,Science & Nature,From 1979 until 2000 the most distant planet from the earth was ________.,Neptune +General,The Merry Go Round is Broken Down - whose melody is that,Looney Tunes,General,Odysseus captured by Cyclops Polyphemus what false name,Nobody,General,Englishman John Woodhouse created which fortified Italian wine,Marsala +General,In you called Jl52020 or 555 2020 in film who would answer,Ghostbusters,General,Ambrosia the food of the Gods from the Greek Ambroata means,Immortal,Science & Nature, Baby beavers are called kits or __________,Kittens +General,What shakespearean play refers to the date of epiphany,Twelfth night,General,What is the shortest and bloodiest of Shapespeare's plays,Macbeth,General,"Which French mathematician, ""the father of Modem Mathematics"", invented analytical or co-ordinate geometry",Rene descartes +General,What's in 5 groups Today Arts People Well being Outdoors,Classifications Girl Scout Badges, History & Holidays,"Santa Claus is based on St Nicholas, where was he born ",Turkey ,General,What does the initials NMT on a prescription mean,Not More Than – usually narcotics +General,"What is the name of the ""Oklahoma Bomber""",Timothy mcveigh, Geography,In which continent would you find the Mackenzie river ?,North America,General,CaCo3 is the chemical formula of what common item,Calcium Carbonate – Chalk +General,What Was The First British Tv Show To Give Away 1 Million Pounds,T.F.I - Friday,Music,What was Dina Carolls first solo top ten hit?,Don't be a stranger,Science & Nature,What did Einstein get the nobel prize for?,The Photelectric effect +General,Rafflesia flowers smell like what to attract pollinators,Rotting Meat,General,What's the largest museum in the world,Louvre,General,The words 'dungarees' and 'jungle' originate from which language,Hindi +General,Who was the Angel in Milton's Paradise Lost,Beelzebub,Food & Drink,Who wrote the novel Cakes And Ale? ,W. Somerset Maughan ,Sports & Leisure,What Is The Name Of The (War Dance) The All Blacks Perform Before A Match ,The Haka  +Music,Which Soul Pairing Duetted On “Endless Love” In 1981?,Lionel Ritchie & Diana Ross,General,What is a group of this animal called: Teal,Spring,Science & Nature,What Road Safety Device Was Invented By Percy Shaw In 1943 ,The Cats Eye  +General,Who served under Nelson commanding the Glatton 1801,William Bligh,General,By what name was outlaw Harry Longbaugh better known,Sundance kid,General,What is a group of this animal called: Badger,Cete +General,Which political party was founded in West Germany in 1972 by the late Petra Kelly,The green party,General,We call them Turkeys what do the Turks call them,American Birds,Science & Nature,"What does the ""lithosphere"" refer to",The earth's crust +General,What numbers does the binary system use,One and zero,Sports & Leisure,What Is The Only Sport In Which You Can See Teams Defending Goals Of Different Sizes ,Water Polo ,General,"Where Exactly Is The 89,000 Feet High Olympus Mons Volcano?",Mars +General,Necrophobia is the fear of,Death,General,What is a female ferret,Jill,General,For Which London Club Did David Seaman Begin His Professional Football Career,Queens Park Rangers +General,Tribology is the study of what,Friction,Geography,Lake Baikal in ______________ is the only lake in the world that is deep enough to have deep_sea fish.,Siberia,General,What is the name of jaleel white's character in the tv series 'family ties',Steve erkel +General,Stanley Gibbons started as a chemist but changed to what,Stamp Dealers,General,Who was the first president born in a hospital,Jimmy carter,Sports & Leisure,Which sport is Toxophily? ,Archery  +General,91% of Americans do what regularly,Lie,General,"What creature's name was derived from French words meaning ""spiny pig""",The porcupine porcupine,General,"""It's a Shame About Ray"" was released in 1992 and rereleased later with a cover of Simon and Garfunkel's Mrs Robinson. which band was it",Lemonheads +General,Routine is what shaped pasta,Wheels,General,What is the Capital of: Palau,Koror,General,"The symbol on the ""pound"" key () is called a(n) _________",Octothorpe +General,Which creatures name translates as the lizard in Spanish,Alligator,General,Amuhea Princess of Medes was the wife of who,Nebuchadnezzars,General,"Who, with 90 years, is the longest ruling monarch in history",Pepi ii of egypt +General,"This dry, warm wind flows eastward down the slopes of the Rocky Mountains",Chinook,General,Who made the first solo round the world flight,Wiley post,General,What does the girls name Deborah mean,Bee - from Hebrew +General,In 1741 Robert Keeler first to commercially manufactured what,Marmalade in Dundee Scotland,Food & Drink,"What nationality is the lager producer, `grolsch'? ",Dutch ,General,Who took eight days to do the first solo round the world flight in 1933,Wylie post + History & Holidays,Who Released The 70's Album Entitled Trafalgar ,Bee Gees ,General,What are the three winter months in the southern hemisphere,"June, july and",Music,Which Troggs hit from 1967 was a worldwide smash in 1994 for another bandf?,Love Is All Around (Wet Wet Wet) +General,"Eddie and his father's last name in ""The Courtship of Eddie's Father""",Corbett,General,A stupa is a shrine to the memory of whom,Buddha, Geography,What is the capital of Burkina Faso?,Ouagadougou +General,Reykjavik translates into what,Smoky Bay,Entertainment,Who played the mayor of the munchkins in 'The Wizard of Oz'?,Charlie Becker,General,In which country was it once against the law to slam your car door?,Switzerland +General,"Name 18th Century playwrite of The Rivals, School for Scandal",Richard Brindsley Sheridan,General,In Sesame street name the two headed friendly monster,Frank and Stein,General,Who is the longest serving Head of State who is not a member of a Royal Family,Fidel castro +General,What keeps one from crying when peeling onions,Chewing gum,General,Okamoto in Japan is the worlds largest maker of what,Condoms,General,Who is the Patron Saint of Young Boys,St Pancreas +Music,What Was The Title Of David Essex's Not Very Successful Stage Musical,Mutiny,General,What is the holiest day in the Jewish calendar,Yom Kippur,Music,What Do The Initials DMC Stand For With Regard To The Hip Hop Band Run DMC,Dynamic Microphone Control +General,Who was disqualified after winning the 1976 british grand prix,James hunt,General,Why would women dislike using a West Indian Dildo,Its a cactus, Geography,What is the basic unit of currency for Ecuador ?,Sucre +General,Motorphobia is a fear of ______,Automobiles,General,"In musical notation, what is the effect of placing a dot immediately after a note",Increases its length by half,General,How many steps are there to the top of the eiffel tower,1007 +General,Who was a bullfrog and a great friend of mine,Jeremiah,Science & Nature,Which Snake Is Also Known As A Hamadryad ,King Cobra ,General,"On which object would you find a crown, a waist, a sound-bow and a clapper",Bell +Music,Which British Group Won The 1981 Eurovision Song Contest,Bucks Fizz,Science & Nature, All porcupines float in __________,Water, History & Holidays,What Name Was Given To The Practise Of Killing Every Tenth Man In A Mutinous Roman Cohort? ,Decimate  +Music,"Which Talented Us Producer Reached No.14 With ""Ai No Corrida"" (I-No-Ko-Ree-Da)",Quincy Jones,General,What lucky charm does Luciano Pavarotti carry in his pocket whilst performing,A bent nail,General,What is unusual about the number 8549176320,Digits alpha order +Food & Drink,What Is Karahi ,A Type Of Wok Used In Asian Cooking ,Music,Who Covered Wayne Fontana's A Groovy Kind Of Love In 1988,Phil Collins,General,When did William Lyon Mackenzie King retire,1948 +General,What is the largest lake in South America,Lake titicaca,General,There are over 800 brands of what for sale in the USA,Bottled Water,General,Cliff Richards She's so Beautiful who played every instrument,Stevie Wonder + Geography,What is the capital of Zambia ?,Lusaka,Music,"Which British R&B Band Were Featured In The 1969 Norman Wisdoms ""What's Good For The Goose""",The Pretty Things,General,What is the most common name in the Bible,Zachariah +General,Which medical condition is detected using the Ishiharo Test,Colour blindness,Food & Drink,What Do You Call Champagne Mixed With Orange Juice ,Bucks Fizz ,General,Inspector Bucket appears in which Dickens novel,Bleak House +General,What country was the first in the world to allow women voters,New Zealand,General,Who was known as the Queen of Folk Music,Joan Baez,General,"Which English Football Team Used To Be Known As "" Newton Heath ""?",Manchester United +General,What's a dead body of an animal called,Carcass,General,Who got his 100-meter dash gold medal stripped away due to his steroid use in the 1988 Olympics?,Ben Johnson,General,What food stuffs name come from the Italian for Pick me Up,Tiramisu +General,France the bliss of mrs blossom starred what actress in the title role,Shirley,General,The assault on Starfleet by the Borg was at,Wolf 359,General,What is the most popular jukebox song of all time,Crazy - Patsy Cline +General,Les Paul and Charlie Christian were exponents of which musical instrument,Guitar,Music,"The 3 Degrees Recorded ""Ta ke Good Care Of Yourself"" In What Yeat 1972,1973,1974,1975",1975,General,Give either of the two men who completed the first circumnavigation of the globe in a balloon,Bertrand piccard brian jones +Sports & Leisure,Who holds 3 World Heavyweight boxing belts? ,Lennox Lewis , History & Holidays,"Double, double toil and trouble; Fire burn, and cauldron bubble. Comes from which of Shakespeare's plays ",Macbeth ,General,Whats the capital of Bermuda,Hamilton +General,Who lived at 1431 North Beachwood,The Monkeys,General,What is a group of mares,Stud,General,What was mildred ella didrikson's nickname,Babe + History & Holidays,Who Released The 70's Album Entitled Twelve Dreams of Dr. Sardonicus ,Spirit ,General,At what does singapore use the colors blue and yellow to ward off evil spirits,Funerals,General,Dishabiliophobia is a fear of ______,Undressing in front of someone +General,From which plant do we get linseed oil,Flax,Science & Nature,What planet boasts the Great Red Spot,Jupiter,General,"In Greek mythology, who was aphrodite's mother",Dione +General,Beans who was the only actor to become president of the u.s.a,Ronald reagan,General,What is the Capital of: New Zealand,Wellington,General,"What name did George Eastman invent in 1888 because it was easy to memorize, pronounce & spell",Kodak +General,"Texas Chainsaw Massacre, there was a guy in a wheelchair. What was his name?",Franklin,General,Who was the sun king,Louis xiv,General,"What is the Capital of: Gambia ,The",Banjul +General,"What is this Italian dessert, made from sponge cake, mascarpone cheese and flavoured with coffee and brandy called",Tiramisu,Food & Drink,What is the main flavour in the following alcohol 'Amaretto' ? ,Almond ,Sports & Leisure,What do the letters ERA mean in baseball?,Earned Run Average +General,What novel by Geoffrey Household was about an attempt to kill Hitler,Rogue male,General,In which U.S. state is the Lowell Observatory,Arizona,General,Bridge River Kwai - Bridges Toki Rio - what actor links films,William Holden +General,What two words were merged to create the word 'meld',Melt and weld,Science & Nature,"Dinosaurs Lived In The Mesozoic Era Between 65 & 290 Million Years Ago. Split Into 3 Periods, Which Came Between The Triassic & Cretaceous Periods ",The Jurassic Period ,General,Who plays joey potter on 'dawson's creek',Katie holmes +General,What are the separators on a guitar neck called,Frets, History & Holidays,"""Who invented the Christmas Cracker? (""""George Cracker"""", """"Tom Smith"""", """"Thomas Edison"""", """"John Bell"""" "" ",Tom Smith ,General,"In which mountains would you find Mount Logan, the highest mountain in Canada?",St Elias Mountains +General,What carries sensations from the tongue to the brain,Lingual nerve,Geography,"The _______________ is not a sea, but a landlocked salt lake, 45 miles long by 9 miles wide.",Dead sea,General,"In 1924, pope urban viii threatened to excommunicate people who used what",Snuff +General,What links Vespasian Titus Domitian Nerva,Rulers Roman Empire 69 - 98,General,Who would use an 'embouchure' in their work,Musician,Science & Nature,Secret Research Into What Was Nicknamed The Manhattan Project ,The Atom Bomb  +General,"What is the nickname of fifth avenue, new york",Millionaires' row,Music,From Which Country did The Band Black Box Originate,Italy, History & Holidays,The Tradition Of Dressing Up On Halloween Started Because ,It Was Believed If You Looked Like A Ghost Then You Would Be Tormented By A Real One  +General,"What riddle asked: ""what is it that walks on four legs, then on two legs, & then on three""",The riddle of the sphinx,General,What is in a Ballini cocktail,Peaches and Brut Champagne,General,Which SF author invented the idea of the com Satellite,Arthur C Clark +General,What was Procul Harem's greatest hit,Whiter shade of pale,General,Who was Jack the Ripper's first victim,Mary ann nichols,People & Places,Which actress played the younger Diana Dors in the 1999 TV film The Blonde Bombshell? ,Keeley Hawes  +General,Christine Jorgensen in 1952 was the worlds first what,Sex Change Operation,General,"What is a standard 7'8"" x 3'2"" x 6'",Grave,General,Who advised us to 'break on through to the other side',Doors +Entertainment,Where does Yogi Bear Live,Jellystone park,Music,Who Wrote The Firebird Suite & The Ebony Concerto For A Swing Band,Igor Stravinsky,General,What's the capital of Nicaragua,Managua +Toys & Games,Building tool named after Civil War president.,Lincoln logs,General,As what is a moose also known,Algonquin,General,Henry Harley Arnold was the first US pilot to do what,Carry Mail +General,In 1967 what new safety measure was introduced to the UK,Breathalyser,Geography,Which motorway connects London with Cardiff? ,The M4 ,General,"What is the common name for ""tinea pedis""",Athletes foot +Science & Nature," The flamingoes of East Africa have few natural enemies. In general, the only predators an adult flamingo need fear are the fish eagle and the __________",Marabou stork,General,Where did the greek gods live,Mount olympus,General,What is the most popular name for a dog in the U.S.,Rover +General,"After leaving '10000 maniacs', who released her first solo album 'Tiger Lily' in 1995",Natalie Merchant,General,Who was the last person executed by the guillotine,Hamida djandoubi,General,Which buff coloured cotton comes from China,Nankeen +General,Britain France and who fought the battle of Trafalgar,Spain,Science & Nature, __________ crumble leaves in their mouths to make a type of sponge to sop up water from the hollows in trees when they can't reach the water with their lips.,Chimpanzees,General,Patusnaya and mallasol types of what,Caviar +General,What flowers name translates from the Greek as Water Vessel,Hydrangea,General,"Author of 'Coming of Age in Samoa', the mostly widely read book in the field of anthropology",Margaret mead,General,Where is the Longchamps race track,Paris +General,A snooker game needs how many balls,22,Entertainment,What is Super Chicken's partners name?,Fred,General,In which film was Charlie Chaplin first heard to speak,The great dictator +General,What Does The MP Stand For In The Abbreviation MP3,Motion Picture / Moving Picture It Does*NOT* Stand For Music Player,General,What Is Measured By A Pluviometer,Rain,General,What is a marcupium,A marsupials pouch +Music,Billy Joel Gave An An Exhaustive List Of 20th Century Events In Which Song,We Didn't Start The Fire,General,Who wrote the children's book Bedknobs and Broomsticks,Mary Norton, Geography,What is the only borough of New York City that is not on an island?,Bronx + History & Holidays,Which was the first magazine to publish a hologram on its cover?,National Geographic,General,On Full House Uncle Jesse had a last name before it became Katsopolis what was it?,Cochran,General,What 1998 film broke the opening weekend box-office record,The waterboy +General,What letters are not on the telephone dial,Q and z,General,Which former dishwasher had his movie premiere in 1921,Rudolph valentino, History & Holidays,What does Dorothy have to steal from the wicked witch in oz ,Her Broomstick  +General,Who was killed in The Little Bastard,James Dean - his cars nickname,General,Tropical tree bearing edible orange fruit,Guava,Music,"Elvis Costello's ""Watching The Detectives"" Was The First Hit For Which Label",Stiff +Sports & Leisure,Polo consists of 8 periods called what?,Chukkers,General,What is a tucket,Baseball organ music,General,Name the brand of the first sour mash whiskey made in 1835,Old Crow +General,West Side Story tells about the West side of what or where,Fifth Avenue,Music,Which american singer had 35 consecutive uk top ten hits between 1984 and 1994?,Madonna,General,What is the'Literary' connection between the wife of a Beatle and a character played by Michael Elphick on TV ,Mills and Boon (Heather Mills-McCartney and Ken Boon)  +General,What Type Of Creature Is A SilverBack?,A Gorilla,Art & Literature,"A European movement beginning in France. Gothic sculpture emerged c. 1200, Gothic painting later in the thirteenth century. The artworks are characterized by a linear, graceful, elegant style more naturalistic than that which had existed previously in Europe. ",Gothic,General,What Is The Name Of This Mr Man Character,Mr Nonsense +General,In what country was Che Guevara born,Argentina,General,Dream of jeannie license plates: what film series does ob wan enjoy,Star wars,General,What astrological star sign covers July 24 - August 23,Leo + Geography,What is the largest island in the Caribbean?,Cuba,Science & Nature, The king crab walks __________,Diagonally,General,Where did 24 democratic and republican national conventions take place,Chicago +People & Places,"In which European country is Dalmatia , from where the Dalmatian Dog gets it's name? ",Croatia ,Entertainment,What song was originally 'Good Morning To You' before the words were changed and it was published in 1935?,Happy Birthday To You,Religion & Mythology,Who is the Greek messenger god?,Hermes +General,Who was the legendary front man for the band roxy music,Brian ferry,General,What's the capital of Wyoming,Cheyenne, History & Holidays,"Which novel, published in1932, was a vision of the future as a sanitised society? ",Brave New World  +General,Which was the first country to host 2 soccer world cups,Mexico,General,A bone specialist is a________,Osteopath,General,What is a group of this animal called: Mule,Barren span +Geography,Which country at the southern tip of the Arabian Peninsula was previously known as Aden? ,Yemen ,General,Who is reported to have invented The Blow Up Doll?,Adolf Hitler,General,Who led 900 followers in a mass suicide in 1979,Jim jones +General,What is the fifth day of the week,Thursday,Sports & Leisure,In Which Game Would You Use A Spider? ,Snooker ,General,"Who wrote ""the marriage of figaro""",Mozart +Geography,What is the capital of Malawi,Lilongwe,General,What is the smallest species of penguin,The Fairy Penguin,General,Linus Torwalds invented and wrote what,Linux computer operating system +Entertainment,"The song ""Matchmaker, Matchmaker"" came from which musical play?",Fiddler On The Roof,General,Which country has a plain green flag?,Libya,General,Name Chewbacca's son - seen Star wars holiday special 1978,Lumpy +General,When did Pope Paul Vl say that fasting was still obligatory on certain days,1966,General,British policemen have truncheons what is USA equivalent,Nightstick,General,What country celebrates its National Day on 25th June?,Slovenia +Geography,What is the second largest country in the world? ,Canada ,Toys & Games,This game (involving a net) was introduced in 1874 as sphairistike,Tennis,Science & Nature,A hot spring which shoots steam into the air is a _______.,Geyser +General,"Whose life story is titled 'fly me, i'm freddie!'",Freddie laker,General,On which ground did Brian Lara score 501 not out,Edgbaston,General,"What links a Gig, Spider and Phaeton",Horse drawn carriages +General,"In 1864, who was massacred at sand creek detention camp in colorado",300,Music,"How Are The Band Aston, Marvin JB & Oritse More Commonly Known",JLS,General,Name the Japanese Stock Exchange Index.,Nikkei +General,Arnold Schwarzenegger played Doug Quaid in which 1990 film?,Total Recall,General,What does the word antediluvian mean,Before the flood,General,What is the U.S. equivalent of the S.A.S.,Delta force +General,Who is supposed to be buried under Kings Cross station,Boadicea, Geography,How many time zones are there in China?,One,General,Who is on a U.S. $50 bill,Ulysses s grant +General,How many legs does odin's horse have,Eight,General,What was Emperor Napoleon Bonaparte's official emblem,Bumblebee,General,What us state includes the telephone area code 607,New york +General,Whose nickname was slowhand (both names),Eric Clapton,General,What is a 300th anniversary called,Tercentenary,General,Peniaphobia is the fear of,Poverty +General,Taken literally what should you see in a Hippodrome,Horses,General,What does a.n.c stand for,African national congress,Geography,What canal connects lake ontario and lake erie,Welland canal +General,Tradionally What Does A Fletcher Make,Arrows,Science & Nature,Is Your Stomach Positioned Above Or Below Your Intestines ,Above ,General,Which film of the 70s received the most Oscars,Cabaret 1972 +General,In which state would you find the geographical centre of the contiguous United States of America,Kansas,General,The word 'traitor' comes from which wwii norwegian who collaberated with the germans,Quisling,General,"When he died, the Romans re named September after him, but soon turned back to September. Who was he",Tiberius +General,Who founded the Church of Scientology,L. ron hubbard,Music,"The Song ""Party All The Time"" Was Released By Which Famous Actor",Eddie Murphy,General,What is the second largest city in Ireland,Cork +Music,"""Axl Rose, Izzy Stradlin, Steve Adler, Slash, Duff McKagen"" All Make Up Which Group",Guns N Rose,General,Who was the only player to win mvp in both leagues,Frank robinson,General,To where in France do the sick make pilgrimages,Lourdes + Geography,What is the capital of Canada ?,Ottawa,General,For what feat is Alexei Leonov famous,First space walk,General,What is a group of this animal called: Dog,Pack +General,What is silviculture,Forestry,General,Which date in the year is seen as the traditional high spot of the Protestant marching season in Northern Ireland?,12th July,General,"What country lifted a ban on Aristotle, Shakespeare & Dickens in 1978",China +General,An astronomical unit is the standard measurement taken from the earth to where,The sun,General,"Which group sang the song ""Ordinary World""?",Duran Duran,General,What is a group of chickens,Brood +Science & Nature,The Worlds First Motorway Opened In 1924 In Which Country ,Italy ,General,Who makes barrels,Cooper,General,On what side should yoU.S.leep to improve digestion,Right +People & Places,What Do O.J Simpsons Initials Stand For ,Orenthal James ,General,In the 18th century what would a pencil be,Brush,General,What neck of water do you cross when sailing from Malaga to Tangier,Strait of gibralta +Science & Nature,What Is Another Name For The Leopard? ,The Panther ,General,Excluding religious works what is the worlds top selling book,Guinness Book of Records,General,Who wrote The French Lieutenant's Woman,John fowles +General,What is used to flavour Kriek Belgian beer,Cherries,General,The Little Shop of Horrors (1986) takes place in what kind of shop,Florist shop,General,Where would you find a Dry Bible,Heart chamber of a ruminant +General,"To what family of plants do the following belong apples, pears, plums, cherries, almonds, peaches and apricots",Rose family,General,What is the largest environmental organisation in the world,Greenpeace,Music,"Who Had A Hit In 1993 With ""For Whom The Bell Tolls""",The Bee Gees +General,"Which Actor Played The Character Of ""Abdul"" In The Beatles Movie ""Help!""",Warren Mitchell,Science & Nature, Ninety percent of all species that have become extinct have been __________,Birds,General,Which father and daughter starred in the film 'Paper Moon',Ryan and tatum o'neal +General,Santo domingo is the capital of ______,Dominican republic,General,What is the main ingredient of Scotch Woodcock,Anchovies,General,How many claws does a housecat have,Eighteen +General,In England it is specifically illegal to be drunk where,In a pub,Music,Ice In The Sun Was An Early Hit For Which Rock Legends,Status Quo,Art & Literature,What publication was subtitled The What's New Magazine,Popular science +General,What is the Olympic motto in the original Latin?,"Citius, altius, fortius", Geography,Where is Angel Falls?,Venezuela,General,Car racing and what sport were banned in the USA during WW2,Horse Racing +Music,Who Had A Hit In 1980 With Captain Beaky,Keith Mitchell,Science & Nature," The hummingbird's tiny __________, 4.2% of its body weight, is proportionately the largest in the bird kingdom.",Brain,General,An animal is a fish if it has _________,Gills +General,"Which Long Running TV Show Had It's First And Only Female Winner ""Katy Cropper"" In 1990",One Man And His Dog,General,The Great Salt Lake lies in which American state,Utah,General,This band was formed in Germany in 1976 by writer and producer Frank Farian,Boney m +General,90% of Americans consider themselves what,Shy,General,Recycling one glass jar saves enough energy to watch TV for how many hours?,Three,Geography,What is the capital of Zimbabwe,Salisbury +General,Which brothers published the storybook entitled 'Household Tales' in the 19th century,The brothers grimm,General,Where are three quarters of the world's pineapples grown,Hawaii,General,Dominica supplies a good part of the world with Rose's,Lime juice +General,Who saved Andromeda from the sea monster,Perseus,Science & Nature, A bear in hibernation loses up to 25 percent of its __________,Body weight,General,Albert Finney turned down which role - Peter O Tool - Oscar,Laurence of Arabia +General,What was William H. Bonney's nickname,Billy the kid,Sports & Leisure,What game features the largest ball?,Earthball,General,What was T E Lawrence better known as,Lawrence of arabia +General,Outside the work is the literal meaning of which snack food,Hors Derves,General,What is the fear of becoming bald known as,Phalacrophobia,General,"In Greek mythology, who defeated Athene in a weaving contest",Arachne +General,Go down on me football the denver ____,Broncos,General,Whats a Sultans wife called,Sultana,General,What is a triangle whose sides are all of different lengths,Scalene +General,"Which painter, famous for his pop-art, died in 1997",Roy lichtenstein,General,"Who wrote a diary entitled ""Five Years of my Life"" at the beginning of this century",Alfred dreyfus,Sports & Leisure,What does TKO stand for,Technical knock out +General,What does the Latin RIP stand for ?,Requiescat in pace,General,Who was the longest serving president in French history,Francois mitterand,General,Who played lulu hogg on dukes of hazzard,Pearl shear +General,What did My Favorite Martian have to do before he could become invisible,Raise his antenna,General,What is the name of the elevated semi-desert region found in the northern and western Cape provinces of South Africa,Karoo,Geography,What is the capital of Mali,Bamako +Music,Which Group Had The Last Xmas Number One Of The 1980's,Band Aid 2,General,"Whose car, when found in Dallas in 1963, contained brass knuckles, a pistol holder, and a newspaper detailing JFK's motorcade route",Jack Ruby,General,"What were the names of Kevin's best friend and girl friend on ""The Wonder Years?""",Paul and Winnie +General,"Of what are Bristol, Rockingham, Chelsea, and Minton types",Pottery Porcelain China,Music,"What's The Connection Between Tom Jomes, Shirley Basey & The Alarm",Wales,Geography,"Unlike most African nations, ________ was never a European colony.",Ethiopia +General,What did Gene Autry name his ranch,Melody ranch, Geography,What is the capital of Chad ?,N'Djamena,General,"How did Mork, in ""Mork and Mindy"" say hello?",Nanoo nanoo +Geography,"___________ is smaller than the state of Montana (116,304 square miles and 147,138 square miles, respectively).",Italy,General,The sports manufacturing company 'Butterfly' are most famously associated with which sport?,Table Tennis,General,What is a zeppelin,Dirigible +General,What was first sold at the 1904 St Louis worlds fair,Ice cream cones,General,The Clifton Suspension Bridge spans which river,Avon,Geography,Which element makes up 2.5% of the Earth's crust,Potassium +Geography,Suez lies at one end of the Suez Canal which city is at the other end? ,Port Said ,Music,"Which American Singer Was Married To Debbie Reynolds , Elizabeth Taylor & Connie Stevens",Eddie Fisher,General,"What Greek wrote Meteorologica, popularizing that name for the study of weather",Aristotle +General,Amor Vincit Omnia a Latin phrase meaning what,Love conquers all,General,In which European city is the Arch of Titus,Rome,Science & Nature,From Which Country Did The Leek Originate ,Switzerland  +General,Pyrophobia is the fear of,Fire,Science & Nature,What does the Binet test measure,Intelligence,General,What is the first book in the vampire chronicles,Interview with a vampire +Science & Nature,What's the technical name for a three legged frog?,Ai,General,What is the young of this animal called: Eagle,Eaglet,General,What craft uses a kiln and a kick wheel,Pottery +General,"Soprano Galli-Marie created the title role in which opera by Bizet, at its premiere at the Opera Comique in Paris, on the third of March 1875",Carmen, Geography,In which city is Westminster Abbey?,London,General,Yeovil Yown Football Club wear shirts which closely resemble which famous team?,Celtic +General,What was the subtitle of Police Academy Six,City Under Siege,General,What does the boys name Paul mean,Small - from Latin,General,Who is the heroine of Silence of the Lambs and Hannibal. (Full name),Clarice starling +General,What is the name of the strait located between the Italian and Balkan peninsulas?,Straits of Otranto,Music,"Charlie Watts Books ""Ode To A Flying Bird"" Is A Tribute To Which Jazzman",Charlie Parker,General,Which famous person said 'Hell is other people',Jean-paul sartre +General,Who was the first pilot to fly faster than the speed of sound,Chuck yeager,General,What is the force that brings moving bodies to a halt,Friction,Music,Which Songwriters & Musicians Are The Core Of Steely Dan,Walter Becker & Donald Fagen +General,"Name the female civilian teacher killed in the ""Challenger"" shuttle disaster",Christa mcauliffe,General,"In the law of torts, oral defamation or use of the spoken word to injure another's reputation, as distinguished from libel or written defamation.",Slander,Science & Nature,What Colour Does Litmus Paper Change To When Dipped In Acid ,Red  +Music,"""Don't Leave Me This Way"" was a 1976 success for both Harold Melvin and the Blue Notes and Thelma Houston. But who scored with a cover version in 1986?",The Communards,General,All windmills turn counter clockwise except where,Ireland,General,What U.S. general was known as old blood & guts,George s patton jr +General,Feijoada is the national dish of what country,Brazil,General,When does macau revert to china,1999,General,What was hg well's first novel,Time machine +General,Glycyrrhiza Glabra is better known as what,Liquorice,General,Whose autobiographical novel was called The Bell Jar,Sylvia plath,General,Rim Butte sounds like something sexual - in which US state,Alaska +General,Who was Prime Minister at the end of World War One,Lloyd george,Entertainment,Who sang 'Beauty and the Beast'?,Celine Dion,General,Ellen Marrenner became more famous as who,Susan Hayward + History & Holidays,"He said, ""I have nothing to offer but blood, tears, toil and sweat.""",Sir winston churchill,General,What was Wilma Flintstone's maiden name,Wilma Slaghoopal,Science & Nature,What is the name of the brand of mathematics that deals with the sides and angles of a triangle ,Trigonometry  +General,Which Canadian city is know as The Steel City,Hamilton - Ontario,General,Violetta Valery is better know as who in the world of opera,La Traviata - by Verdi,General,In the Bible Jesus walked on water who else did this,St Peter - to Jesus from boat +Music,"Who Recorded The Album ""Too Low For Zero""",Elton John,General,Who founded The Society Of Jesus,Ignatius loyola,General,Jackson Whipps Showalter was a US champion at what,Chess +General,Sukkot is a festival in which religion,Jewish,Science & Nature,Paper is made from the pulp of _____.,Wood,Sports & Leisure,What Is The Maximum Score Possible In A Game Of Ten Pin Bowling ,300 Points  +General,1960 Orange bowl was first appearance of which sporting giant,Goodyear Blimp,General,"What links a bick, throat, half swage, punching hole",Anvil they are parts of it,Music,"What Song Features The Lyric ""We Dont Need No Education, We Don't Need No Thought Control""",Another Brick In The Wall +General,Margarita Carmen Casino became famous as who,Rita Heyworth,Music,Under What Name Does Georgious Panayiotu Perform,George Michael, History & Holidays,"According to the nursery rhyme, who, 'sat in the corner eating a Christmas Pie'' ",Little Jack Horner  +General,"Which Pop Group Were Originally Called ""The Frantic Elevators""?",Simply Red,General,1894 Orville Gibson started worlds oldest company make what,Electric Guitars,Music,When I Need You” Was A U.K Hit For which Singer?,Leo Sayer +Sports & Leisure,Which position is usually played by the tallest member on a basketball team,Centre,General,Which operas last lines are Mimi. Mimi,La Boheme,General,What does an artist's easel support,Canvas +Sports & Leisure,With Which Sport Would You Associate Colin Montgomerie? ,Golf ,General,This sport is called camogie women play what's it when men do,Hurling,Art & Literature,Which Gilbert and Sullivan Opera is about the Emperor of Japan? ,The Mikado  +General,What did joseph priestely discover,Oxygen,General,What was the name of the Titanic's sister ship,Olympic,General,When did Sir Walter Scott write Ivanhoe,1819 +General,What does IRS stand for?,Internal Revenue Service,General,Certain marbles are called 'alleys' because they are made of ______,Alabaster,General,Taliban women required by law to wear what on left arms,Tattoo of husbands name +General,Anthesis means what in relation to plants,In Flower - blooming,General,What nationality is the designer Galliano,British,Music,"In Which Musical Did Donald O Connor Perform The Breathtaking ""Make Em Laugh"" Routine",Singing In The Rain +General,Cagney shoves grapefruit Mae Clarke face was going to be what,An Omelette,Technology & Video Games,What does the acronym COBOL stand for?,Common Business Oriented Language,General,BOZ was the penname if which writer,Charles Dickens +General,As easy as ______,Pie 3.14159,General,Which Car Manufacturer Was Founded By Sir William Lyons In 1922?,Jaguar,General,"An ""omniscient"" person has unlimited __________",Knowledge +General,The okapi is most closely related to what african mammal?,Giraffe,General,"What is the name of the sign which is put over the letter 'n' in Spanish, to give a 'nya' sound",Tilde,Science & Nature,Name the only native North American marsupial.,Opossum +General,"In The World Of Entertainment How Is ""Dana Owens"" More Commonly Known",Queen Latifah,General,Which Famous Novel Starts With The Line….. “ I Never Knew My Father ”?,Tarzan,General,In which decade did Jacques Garnerin make the first parachute descent (from a balloon),1790's +General,What was the name of the nightclub owner in 'Casablanca',Rick stein,General,Pennsylvania was the first colony to legalise what,Witchcraft,Entertainment,Who did Charlie Becker play in 'The Wizard of Oz'?,The mayor of the munchkins +General,In which century was the Ming Dynasty founded in China,Fourteenth,General,Where was Keanu Reeves born,Beirut - Lebanon,General,What do spiders and ticks have in common,Eight legs + History & Holidays,Which famous ship sank in 1912 ?,Titanic,General,"Who won the best Actor Oscar for ""Scent of a Woman""",Al pacino,General,What did the sparrow kill Cock Robin with,Bow & arrow + History & Holidays,What Did British Troops Destroy In Washington DC In 1814 ,The President's House ,General,What is the state flower of South Dakota,Pasqueflower, History & Holidays,Which Pop Singer Married Maurice Gibb Of The Bee Gees In February Of 1969? ,Lulu  +General,Collective nouns - what group af animals are a labour,Moles,General,The classical music term fugue comes from Latin meaning what,Flight,General,What is the sum of 27 + 52,79 +General,Who created Popeye,Elzie Seger,General,Which two countries made up the 'Dual Alliance' between 1879-1918?,Germany and Austria,General,Famous painter that paints with both his right & left hands,Leonardo da vinci da vinci +General,Nothing's so loyal as love whose headstone reads 'looking into the portals of eternity teaches that the brotherhood of man is inspired by god's word; then all prejudice of race vanishes away',George washington,General,What is drawing wild but politically favorable electoral districts,Gerrymandering,General,Which dish gets its name from the French meaning to stir,Ratatouille +General,What magazine has the largest unpaid circulation in the US,Disney Channel Magazine,General,"In Greek mythology, who was the mother of perseus",Danae,General,"""All human life is there"" - a quotation from Henry James - was used to promote which British Sunday newspaper in the 1950s",News of the world +General,What is the fear of things to the left side of the body known as,Levophobia,People & Places,Who Was Caught On Film Kissing The Duchess Of Yorks Toes ,John Bryan ,Music,Prince Was Going To Party Like It Was What Year,1999 +General,Who released 'love roller coaster' in 1984,Ohio players,General,Which soul singer died in 1984 after lying in a coma for eight years?,Jackie Wilson,General,The word 'cop' is an abbreviation for what,Constable on patrol +Science & Nature,What is the only man-made structure on earth that can been seen from space?,Great Wall Of China,General,Which actress had a job putting cosmetics on corpses,Whoopee Goldberg,General,Whose Dictionary of the English language was published in 1755,Samuel johnson +Food & Drink,"Crushed grain, nuts and dried fruit ",Muesli ,General,What newspaper do the Flintstones read?,The Daily Slate,Music,"Sinead O Connors Song ""Nothing Compares 2 U"" Was Actually A cover Of A Song By Which Artist",Prince +General,In which field of science is the history of the universe studied ?,Cosmology, Geography,What is the capital of Liechtenstein ?,Vaduz,General,What animal does the adjective 'talpine' refer to,Mole +General,What do spanish dancers hold in their hands,Castanets,General,61 is the international telephone dialling code for what country,Australia, History & Holidays,What was the name of the company that the characters on Taxi worked for? ,Sunshine Cab Company  +Music,"Which Jimi Hendrix classic features the line ""Scuse me while I kiss the sky""?",Purple Haze,Art & Literature,"Who wrote the book ""The Origin of Species"" ?",Charles Darwin,General,What does the typical American eat 263 of each year?,Eggs +Science & Nature,Which Creature Got It's Name Because Scientists Felt That It Fed On Eggs ,Oviraptor ,General,In The Dukes of Hazard who was the sheriff,Roscoe P Coltrane,General,"In the Hollywood classic 'The Greatest Show on Earth', who plays 'Buttons' the clown",James stewart +Food & Drink,What is pumpernickel? ,German Black Bread ,General,Who was the first man in space,Yuri gagarin,General,In pro-football how long does a 'sudden death' period last,Fifteen minutes +General,Mao had the red book who did the green book on African unity,Moammar Qaddhaffi,General,What natural phenomenon can never be seen at noon,Rainbow - sun must be 40 deg or less,Music,Who Is Loretta Lynn's Younger Sister,Crystal Gayle +General,Which Fruit Is Affected By A Grey Type Of Fungus Known Commonly As Noble Rot,Grapes,General,"Name the children on ""Just the Ten of Us"". Bonus: Name the dog.","Cindy,Wendy,Marie,Connie,J.R.,Heidi,the twins: Melissa and Harvey",Music,Sung By John Taylor Which Single Was Also The Theme To The Movie 9 And A Half Weeks,I Do What I Do +Music,"Which Tv Soap's Theme Was ""Sold As Anyone Can Fall In Love""",Eastenders,General,What colour graded slope do expert skiers use,Black,General,Who succeeded Caligula as Roman Emperor,Claudius + History & Holidays,What Was The First Reich? ,The Holy Roman Empire ,General,Beelzebub is Hebrew for which phrase - also a novels title,Lord of the Flies,Sports & Leisure,Which Australian cricketer was sent home from the cricket World Cup in February 2003 after failing a drug test? ,Shane Warne  + Geography,What country has the highest per capita tea consumption?,Ireland,Geography,In what country is the Jutland peninsula,Denmark,General,What was originally called Eskimo Pie,Chocolate Ices +General,Which mummified tendon was auctioned at Christies in 1969,Napoleons Penis,General,Who kissed the girls & made them cry,Georgie porgie,Music,"What Was Unusual About The Archies, Who Had A Number One Hit With Sugar Sugar",They Were Cartoon Characters +General,"Dandy Dinmont, Bedlington, Sealyham are what types of dog",Terriers,General,What is the name of the Flintstones cat,Baby-Puss,General,What is a castrated ram,Wether +General,White Room' was a hit off which Eric Clapton album,Cream,General,A 'morel' is what type of vegetable,Mushroom,General,What Was The Very First Product Made By The Philips Company?,Light Bulb +General,One eighth of the US population have done what,Worked in MacDonald's, History & Holidays,Which 50's Movie features the Line 'We've become a race of peeping toms' ,Rear Window ,General,According to USA today what is the favourite luxury car,1 Lexus 2 Mercedes 3 BMW +General,Who was born Sarah Jane Fulks?,Jane Wyman Reagan,Music,"Which British Female Singer Had An International Hit In 1981 With The Single ""Cambodia""",Kim Wilde,Geography,The tallest building in the Southern Hemisphere is in which city?,Melbourne +General,Who made their first royal visit to Canada in 1951,Princess elizabeth,General,"What are mazurka, fandango and polonaise types of",Dances,General,What is the Chinese word for wind,Feng - Shui is water +General,What is the capital of guinea-bissau,Bissau,General,"Members Of The Band ""Split Enz"" Went Onto Form Which Popular Band",Crowded House,General,What was originally made of bamboo then a plastic called grex,Hula Hoops by Arthur Melin 1958 +General,In TV series The Prisoner what's the name of the giant balloon,Rover,General,What is the flower that stands for: bantering,Southernwood,General,What poster queens's record did et beat with sales of ten million in 1982,Farrah fawcett-majors farrah fawcett +General,The Mabinogion is a collection of legends from which country,Wales,General,French racing driver Jean Behra kept a spare what in his pocket,Plastic right ear,General,What name in Hebrew means to add,Joseph +General,What is the common name for the condition dyspepsia?,Indigestion,General,What links a Sylvester Stallone character and Panama,Balboa Panama cash Rocky name,General,Who wrote Beau Geste,P C Wren +General,Only one woman's lifespan is given in the Bible - Who,Sarah Wife Abraham 127 Genis 23,General,In Greek mythology any of 3 snake haired sisters able to turn people to stone,Gorgon, Geography,What is the capital of Nigeria?,Abuja +General,What sexual practice is maritate,Female masturbation,General,Which Gallantry Award Bears The Inscription “For Bravery In The Field?,The Millitary Medal, Geography,What is the basic unit of currency for Kyrgyzstan ?,Som +General,What animal is dr. dolittle's pushmi-pullyu,Two-headed llama,General,"In the House of Lords, where does the Lord Chancellor sit?",Woolsack,Science & Nature,What percentage of the population has an iq above 100 ,Fifty percent  +General,When was george jones inducted into the country music hall of fame,1992,General,"Which musician wrote the book ""twixt twelve and twenty"" in 1958",Pat boone,General,What famous reference work is illegal in Texas,Britannica - It shows beer making +General,"What was the top technology manual of 1996, at over one million sales",Windows 95 for dummies,General,How one would describe Jeremiah Peabody's pills,Poly unsaturated quick dissolving fast acting pleasant tasting green and purple,General,Unu and Ne Win have been leading figures in the post- 1945 history of which country,Burma +Food & Drink,Black German rye bread ,Pumpernickel ,General,What Did Buisnessman Peter De Savray Buy For £6.7 Million Pounds In 1987,Lands End,General,Its illegal to do what in the French vineyards,Land a Flying Saucer +General,Basketball: the utah ______,Jazz, History & Holidays,What Chinese dynasty was overthrown in 1911?,Manchu,General,"On the 11th day of christmas, my true love gave to me",Eleven pipers piping +General,Who was signed by motown when he was 5,Michael jackson,General,Copenhagen is the capital of ______,Denmark,Geography,What is the capital of West Virginia,Charleston +Science & Nature," A species of __________ known as the Linckia columbiae can reproduce its entire body _ that is, grow back completely _ from a single severed pieces less than a half_inch long.",Starfish,General,A healthy person does it 16 times a day - what,Farts,General,What film won the best picture Oscar in 1967,In the heat of the night +General,What character first appeared in the film The Wise Little Hen,Donald Duck,General,Collective nouns - A wiggle of what,Tadpoles,Music,Bark At The Moon Was An Album For Ozzy Osbourne Or Meatloaf,Ozzy Osbourne +General,All US presidents have worn what,Glasses - not in public,General,"In science, which term refers to the number of protons in the nucleus of an atom",Atomic number,Geography,What does the river seine empty into ,The english channel  +General,"What was master po's name for young cain in the tv series ""kung fu""",Grasshopper,General,Who or what is the Empress of Blandings,A pig,Music,Who Had A Hit In 1979 With A Super Speedy Version Of The Banana Splits (The Tra La La Song),The Dickies +Music,What was the first single released on the Beatles Apple label?,Hey Jude,Geography,In which county is the market town of St Austell ,Cornwall , History & Holidays,Which Act Has Had More Christmas Number One Singles In The UK Than Any Other? ,The Beatles  +General,Who did Roger Bannister beat at the Commonwealth Games of 1954,John landy,Sports & Leisure,"In showjumping, how many points are incurred for knocking down a fence?",Four,Sports & Leisure,Which piece of sporting equipment is a `biased'? ,A Crown Green Bowl  +Music,Which British Producer Worked In Tandem With Madonna On Her Ray Of Light Album,William Orbit,General,"Which apostle is the patron of bankers, book-keepers and tax collectors",Matthew,General,A musical instrument and the French word for paper clip what,Trombone +Science & Nature,"Fandible, lateral line, and dorsal fin are parts of a(n) ________.",Fish,Sports & Leisure,How Many Furlongs Are There In A Mile? ,8 ,General,What was the name of the dog in RCA Victor's trademark?,Nipper +General,Colourless volatile liquid formerly used as an anaesthetic,Chloroform,General,What is the art of bell-ringing called,Campanology,General,"Which product was advertised as 'tested by dummies, driven by the intelligent'",Volvo cars +General,43% of women want to try sadomasochism after smelling what,Vanilla extract,General,By what name is the bird Pica Pica better known,Magpie,General,In Greek cookery what are 'sheftalia',Minced lamb kebabs +General,George the boys name means what,Farmer,General,Which U.S. actress gave her name to an inflatable life jacket,Mae west,Music,"Although Re-Issued In The 70's ""Shotgun Wedding"" Was A One Hit Wonder For Whom In 1966",Roy C +General,MacDonald farm Sheep Cows Pigs Chicks Ducks Donkeys and what,Turkeys, Geography,What is the basic unit of currency for Brazil ?,Real,Music,"What Connects 10cc, Mick Hucknall, Oasis",Manchester + History & Holidays,"In 1893, this country was the first to give women the vote.?",New Zealand,General,Which historical event is depicted on the Bayeux tapestry,Norman conquest of,General,In the body what do the Islets of Langerhans do,Secrete Insulin +General,How is 75% of petrol in an engine wasted?,Combustion,General,In which London street is the US embassy,Grosvener Square,General,German wine is made from mainly what grape,Riesling +General,What is a travelator,Horizontal Escalator,General,As Of 2009 Which Musical Act Have Been The Most Successful In One Night At The Brit Awards,Blur,General,What was the name of the charter granted by King John,Magna carta +General,What nationality is designer Karl Lagerfield,German,Science & Nature,What Sort Of Animal Is A Fer De Lance ,Snake ,General,Who is the largest toy distributor in the world,McDonalds +Food & Drink,Sea Urchin Sushi Is Known As What In Japanese ,Uni ,General,Alhambra is a strong lager brewed in what country,Spain,General,What was the name of the Bjork-fronted 80's band?,The Sugarcubes +Sports & Leisure,What is Tiger Woods real first name? ,Eldrick Woods ,General,What dog appears in the wacky races,Muttley,General,Who is the greek equivalent of the roman god amor,Eros +Music,Who Partnered David Bowie On A Christmas Hit In 1982,Bing Crosby,Food & Drink,Vodka and Kahlua make up which type of cocktail ,Black Russian ,General,What type of number describes the ratio of the speed of a plane to the speed of sound,Mach +General,Where is a 'crossbuck',Railroad crossing,General,A French wine described as doux is what,Medium Sweet,General,What authors only detective work was The Red House Mystery,A A Milne +Sports & Leisure,How Many Times Are A Team Allowed To Touch A Volleyball Before It Crosses The Net? ,Three ,General,A group of oysters is know as a(n) _____,Bed,General,What or where was original deadline,USA Civil war prison +Music,The Notting Hillibillies Featured Famous Guitarist Eric Clapton Or Mark Knopfler,Mark Knopfler,General,"Where would you find a parlour, scriptorium, dorter and cellarium",A Monastery,General,As what is -40 degrees C the same as what in F,Minus forty degrees -40 minus fourty + Geography,What is the basic unit of currency for Bhutan ?,Ngultrum,Science & Nature,What is the chemical element Pa?,Protactinium,General,The Bering Strait divides Russia from where,Alaska +General,Of what is agrostology the study,Grasses,General,"Montpelier, vermont is the only u.s state capital without a _____",Mcdonald's,General,Who Is The Most Nominated Actor For An Oscar ?,Jack Nicholson +General,What countries women most likely to have sex on a first date,Australia 13%,General,In 1976 this former humble pie singer and guitarist came alive,Peter,General,Who dictated the Koran to Mohamed,The Angel Gabriel +General,What fantasia suite features autumn fairies and frost fairies,Nutcracker,General,"What famous chinese philosopher wrote the book ""the art of war""",Sun tzu,General,A Group of Cattle is called a,Herd +Entertainment,"Which 1980's Pink Floyd album was made into a film that starred Bob Geldof, and featured the artwork of cartoonist Gerald Scarfe?",The Wall, Geography,This is the bridge with the longest span in the U.S.A.,Verrazano Narrows,Music,"Nena, Who Reached The No.1 Spot In The UK Came From Which Country",Germany +General,Who was French Prime Minister at the end of World War 1,Georges clemenceau,General,Narcolepsy is the uncontrollable need to ______,Sleep,General,What is the scientific study of the structure of living things,Anatomy +Sports & Leisure,Who was the first to win the grand slam of tennis,Don budge,General,If you landed at Mirabel international airport where are you,Montreal,General,Which planet is nearest the sun?,Mercury +General,Phobophobia is a fear of ______,Phobias,Sports & Leisure,How Many Players Comprise An Ice Hockey Team? ,Six ,Music,"Who Is ""Reginald Dwight"" Better Known As",Elton John +General,The word melee comes from what sport,Football village play,General,Other than fruit what is the only natural food made without killing,Honey,General,"In Greek mythology, who were the personification of the forces of nature",Titans +General,What does ebcdic mean,Extended binary coded decimal interchange code,General,What two olympic events require competitors to travel backwards in order to win,Rowing & backstroke,Science & Nature,What Did Sir John Harrington Invent In 1589 ,The Flushing Toilet  +General,What title has the wife of an earl,Countess,General,In the classical format there are strictly only five positions - what,Ballet,General,Calvin Broadus is better known under what name,Snoop Doggy Dogg + History & Holidays,What 'CP' Is One Of The Ghosts From A Christmas Carol ,Christmas Present / Past ,Music,What Was the First Number One Hit Single For “The Jam”?,Going Underground,Food & Drink,What are the essential ingredients of a daiquiri?,Rum and lemon +General,What Was The Name Of The Poodle That Once Resided At The Queen Vic In Eastenders,Roly / Rollie,People & Places,Who is Julie Andrews Married To ,Blake Edwards ,General,Huge battle at the end of the world,Armageddon +General,What is six inches in height and no bigger by the rules,Table Tennis Net,Science & Nature,What Is Measured In Amps ,Electric Current ,General,What is the main ingredient of risotto,Rice +Art & Literature,"In sculpture, the projection of an image or form from its background. Sculpture formed in this manner is described as high relief or low relief (bas-relief), depending on the degree of projection. In painting or drawing, the apparent projection of parts conveying the illusion of three dimensions. ",Relief,General,What is a group of falcons called,Cast,General,Where did the group 10cc get their name,Average sperm in ejaculation + History & Holidays,Which country blew up a Greenpeace ship in New Zealand?,France,Geography,San Francisco Bay is located near what city,San francisco,General,Name one of the three most common names in china,Chang +General,Someone who is prejudiced against Jews,Anti-semetic,Music,Name Either Of Marc Almonds 2 Groups That He Formed After The Demise Of Soft Cell,"Marc & The Mambas, The Willing Sinners",General,What is Carambola,Starfruit +Music,An Untypical Motown Record Which Single Gave Singer Charlene A UK No.1 In 1982,I' ve Never Been To Me,General,Which British aircraft company produced a classic car marque and also made buses and trucks in the post war era,Bristol,General,"The game preserve featured in the TV series ""Daktari""",Wameru +Music,1970’s Super Group Led Zeppelin Recorded “Stairway To Heaven” But Who Had An Unexpected Hit With In During The 1990’s,Rolf Harris,Music,What Is The Name Of The Lead Singer Of Staus Quo,Francis Rossi,General,What magazine did feminist expert Shere Hite pose for in 1971,Playboy +General,Which impresionist artist was known for his series of paintings based around Hampton on the river Thames?,Alfred Sisley,General,"The novel ""Weir of Hermiston"" was an unfinished novel by which writer",R l stevenson,General,Which duo has won seven Oscars,Tom and Jerry +General,In mythology which giant made of brass guarded Crete,Talus,General,"What ancient symbol's German name translates as ""hooked cross""",The swastika swastika,General,In New York it is illegal to shoot what from a moving trolley car,Rabbits +General,In which city are the Headquarters of INTERPOL located,Lyons,General,Smallest particle of a substance having the specific chemical properties of that substance,Molecule,General,"Goose-geese, passerby- ______",Passersby +General,Name the control centre beneath Derby House in Liverpool where the Battle of the Atlantic was plotted during World War 2,Western approaches, History & Holidays,"Which Date On The Christian Calendar Marks The Epiphany, When Jesus, Mary And Joseph Met The Three Wise Men? ",January 6 th ,General,Who opened the first unattended 24 hour self service laundromat,Nelson puett +General,Spumador was whose horse,King Arthur,General,Tiede Peak is a volcano on which island,Tenerife,Sports & Leisure,"Which annual sporting event attracts easily the most spectators (i.e. present, not watching television ) of any in the world with around 10 million? ",The Tour De France  +General,"What city in Nepal translates as ""wooden temples""",Katmandu,Sports & Leisure,Which Sporting Event Was BBC2's First Ever Colour Tranmission In 1967 ,Wimbledon ,General,What is the largest lake in Central America,Lake nicaragua +General,Where are you most likely to have a serious accident,In your home,General,Extortion of payment in return for silence,Blackmail,Music,"What Was The Instrument That Produced The Weird Noises On The Beach Boys ""Good Vibrations""",Theremin +General,In traditional Chinese thought what is the opposite of 'yin',Tang,General,What was the name of the castle that gave He-Man his powers?,Greyskull.,Science & Nature,Which substance has the chemical formula H3PO4?,Phosphoric acid + Language,"What does ""c'est la vie"" mean",That's life,General,"What's next in the series 2, 3, 4, 6, 8, 12, 14, 18, 20, ?",24, History & Holidays,Which horse won the 1964 Derby ,Santa Claus  +General,"Billie Holiday, James Dean, Eva Peron, Janis Joplin - Common",All were Prostitutes,General,With which island is the puffin associated,Lundy island,General,What is the name of the crispbread traditionally eaten by Jews at Passover,Matzo +General,Who's the bitty apprentice of the evil witch in the little lulu comics,Little itch,General,What do humans completely shed and regrow every 27 days,Skin,General,What is the only edible orchid,Vanilla +General,"What group has more gold, platinum & multi platinum albums than any other",The rolling stones rolling stones,General,What was the first movie filmed in sensurround,Earthquake,General,What does a.m stand for,Ante meridian +General,In Madagascar its illegal for pregnant women to do what,Wear Hats or Eat eels,General,What is the musical style called that mixes jazz with pop or other styles,Fusion,Sports & Leisure,How Many Epsom Derbies did Lester Piggott Win? ,Nine  +General,If you have dysmorphia what do you hate,A body part,General,Doha is the capital of which gulf state,Qatar,General,What 265m high peak is located in wyoming,Devil's tower + History & Holidays,"After the fall of the iron curtain, Russian leader Mikhail Gorbachev introduced a period of restructuring known as ________.",Perestroika,General,What kind of condition is 'protanopia',Colour blindness,General,Who was the Pope who served for the shortest time,Urban vii +General,What happened to the first traffic lights outside HP 1868,They exploded,General,Who formed the nhl players' association,Alan eagleson,General,What is Corvus another name for,Fellatio (Blowjob) +General,What is the more usual name for the Egg Plant or Guinea Squash,Aubergine,Science & Nature,This poisonous gas is in the exhaust fumes from cars.,Carbon monoxide,Religion & Mythology,What is Greek muse of dance and choral song?,Terpsichore +General,Which country had the first women MPs 19 in 1907,Finland, History & Holidays,Which Institution Was Founded By The Roman Catholic Church In The 13th Century To Root Out Heresay? ,The Inquisition ,General,What is the Capital of: Bolivia,La paz + History & Holidays,"Who Coined The Phrase (Goverment Of The People, By The People And For The People)? ",Abraham Lincoln ,General,Where are the finger lakes,New york,General,What did louis cartier invent,Wristwatch +Art & Literature,Who Wrote The Novels 'The Hunt For Red October'' And 'Clear And Present Danger'' ,Tom Clancy ,People & Places,Who is Ashton Kutcher Famous Older woman wife? ,Demi Moore ,Entertainment,What song's words were changed and then published in 1935 as 'Happy Birthday To You'?,Good Morning To You +General,What is a baby rabbit called,Kit or Kitten,General,Craven Walker invented what 60s fashionable icon,Lava lamp,General,What does a copoclephist collect,Key Rings +General,What is the flower that stands for: blackness,Ebony,General,Phobos and Deimos are moons of which planet,Mars,General,A young horse or related animal,Foal +General,How much of the Earth's surface is covered by water,70,Music,"In Which Year Were The Following All Chart Hits :""Ghostbusters"" By Ray Parker Junior, ""Jump"" By Van Halen And ""Smalltown Boy"" By Bronski Beat?",1984,Art & Literature,Which Em Forster Novel Features The Schlegal Sisters ,Howards End  +General,"Who is advised ""not to carry the world upon his shoulders""",Jude,Sports & Leisure,Hockey: The St. Louis __________.,Blues,General,What type of musical instrument are 'Timbales'?,Latin American Drums +General,What is the largest lake in Australia called,Lake eyre,General,Which president married Martha Dandridge Custis,George washington,General,America's country's first commercial oil well was located in what state,Pennsylvania +General,In Long Beach California where is specifically illegal to curse,Miniature Golf Course,General,The Dayak people are indigenous to which country or region?,Borneo or Sarawak,General,Name the only Senator whose parents had also served in the Senate,Russell +General,What did the name 'saxe-coburg' become,Windsor,General,Which common item was banned by law in Bermuda until 1948,Motor Cars,Sports & Leisure,In Which Sport Might You Win The Curtis Cup ,Golf  +General,What is a group of teal,Spring,General,Adolf Dasler created which company,Adidas,General,50% of 17 year old Americans can't do what according 700 club,Read + History & Holidays,"U.S. President, Herbert C. _________.",Hoover,General,Horses are Equine from the Greek Equus what's it mean,Quickness,General,"In The World Of Music How Is ""Jiles P Richardson"" More Commonly Known",The Big Bopper +General,Boise is the capital of ______,Idaho,General,What ten volume tome did Victor Hugo give the world in 1862,Les Miserables,General,"______, the story of prize fighter Jake Lamotta, packs a real punch",Raging Bull +Science & Nature," __________ eggs which are incubated below 85º F (29.5º C) hatch into females, while those incubated above 95º F (35º C) hatch into males.",Crocodile,General,Sedimentary and igneous are types of what,Rock,General,"In literature, what was Long John Silver's status when on board ship",The cook +General,Erotophobia is a fear of ______,Sexual love,Entertainment,Who released 'Tuesday Night Music Club' in 1993?,Sheryl Crow,General,Ann Franklin in 1792 was the first woman to do what,Newspaper Editor in Newport USA +Music,Which Female Vocalist Guested With Massive Attack On Their 3rd Album Mezzanine,Elizabeth Frazier,General,What is the game we call Noughts and Crosses called in America,Tic tac toe, History & Holidays,Who was forced by Indian troops into the Black Hole of Calcutta?,British officers +General,What is a group of ptarmigans,Covey,Music,"Whose Only UK No.1 Single Was ""My Ding A Ling"" In 1972",Chuck Berry,General,What common allergens are the male sex cells of plants,Pollen +Sports & Leisure,What sport is sometimes called 'rugger'?,Rugby union,General,The starting point of the muslim era dates back to the time when Muhammad moved to which city?,Medina,General,Light Venetian canal boat,Gondola +General,Who was the leader of the bad guys on Hulk Hogan's Rock N Wrestling that annoyed Hulk Hogan and his freinds?,Rowdy Roddy Piper,General,What country was called Botany Bay and New Holland until 1820,Australia,Geography,"America purchased Alaska from __________ in 1867 for $7,200,000 _ about 2 cents an acre.",Russia +General,"In South Africa, what is 'biltong'",Dried meat,General,What medication discovered in 1928 but introduced 1940,Penicillin,General,Reginald Truscott-Jones became famous as who,Ray Miland +General,"Who directed the 1998 gangster film Lock, Stock and Two Smoking Barrels",Guy ritchie,General,What car has been voted European car of the Century,Mini,General,What is the flower that stands for: elegance and grace,Yellow jasmine + History & Holidays,"What Connects Richard Nixon, Lyndon B Johnson, Hubert Humphrey & Spiro Agnew ",4 US Vice Presidents During The 60's ,Music,Who Led The Blue Caps,Gene Vincent,General,Something that is fistular is what shape,Pipe shaped +General,How many states joinded the confederacy,Eleven,Science & Nature,How many stars make up the 'Southern Cross'?,5,General,What queen did edmund spenser dedicate his faerie queene to,Elizabeth i +General,What rock star joined the cast of General Hospital,Rick springfield,General,Duffy: Johnny Got His Gun,Dalton trumbo,Science & Nature, Gorillas and __________ sleep about fourteen hours a day.,Cats +General,Ventura county California who/what cant have sex without permit,Cats Dogs,General,What colour habit do Franciscan monks wear,Grey,People & Places,Which former Radio 1 DJ used to broadcast 'Our Tune'? ,Simon Bates  +General,Who founded the 'sas',David stirling,Music,Which Lucky Man Spent 7 Weeks In The Charts With Olivia And 16 With Sarah,Cliff Richard, History & Holidays,Which major international organization was created in 1945 ?,United Nations +Geography,The beautiful Antibes on the _________________ is the luxury_yacht capital of the world. Antibes also hosts one of the largest antique shows in Europe each spring.,French riviera,General,What does a tsiologist study,Tea,General,What was George Washington's favorite horse's name,Lexington +General,What Car Manufactuer Were The First To Install Seatbelts In Their Vehicles In 1849?,Volvo,General,What country did Italy invade in 1935,Abyssinia - Ethiopia,Music,He Died Playing Russian Roulette And Has Since Been Celebrated In Song By Paul Simon Who Was He,Johnny Ace +General,In which sport do you need to score five to win,Fencing - five hits,General,Alexander the Great suffered from what malady,Epilepsy,General,Who was the mastermind behind the 'ahead of its time mother of inventions',Frank zappa +General,What's the biggest source of pollution in Lake Ontario,Lake Erie,General,"What U.S. state was named for Lord de la Warr, early governor of Virginia",Delaware,Sports & Leisure,"What was won in 1995 by a Spaniard, in 1996 by a Dane, in 1997 by a German, in 1998 by an Italian & in 1999 by an American? ",The Tour De France  +General,"If you drive on a parkway, you park on a _______?",Driveway,People & Places,Which British Director Attempted To Appear In All The Films He Directed? ,Alfred Hitchcock ,General,What was the country of Botswana called before 1966,Bechuanaland +General,"Which hard substance, closely resembling bone, makes up the bulk of a tooth",Dentine,General,What was Robin Williams paid for Disney's Aladdin in 1982,Scale $485 day + Picasso Painting,General,"Ailsa Craig, Bedford Champion and Rijnsburger varieties of what",Onions +General,What tennis player earned the nickname the Swedish steel,Bjorn borg,General,What does a polythesistic person believe in,Many Gods,General,Element 4 Is The Theme Tune To Which Hugely Popular Television Show,Big Brother +General,"Hammer, anvil, and stirrup are parts of ________.",Ear,General, A device used to change the voltage of alternating currents is a ______.,Transformer,General,The jealous Athena turned who into a spider,Arachne +General,In 1821 Jacob Fusel worlds fist commercial factory making what,Ice Cream,General,Where did the celts believe dead heroes went,Avalon,Music,"Who Recorded The 1960's Albums ""Have Twangy Guitar Will Travel"", ""The Roaring Twangies"" , ""Twangsville & Water Skiing""",Duane Eddy +General,Whose autobiography was called Tall Dark and Gruesome,Christopher Lee,General,How many years does it take for Saturn to orbit the Sun,29,Art & Literature,Who wrote the epic poem Odyssey ?,Homer + History & Holidays,What Reference Book Went On Sale For The First Time In 1955 ,Guinness Book Of Records ,General,"What is the gift on the eighth day of christmas in the ""twelve days of christmas""",Eight maids a milking,General,What is the literal meaning of the word 'Pharaoh',Great house +Sports & Leisure,What Is The First Event In The Heptathalon ,100 Metres Hurdles ,General,"What happened to lady Jawara , the President of Gambia's wife when he was at Prince Charles wedding",She was kidnapped,General,The Seven Pillars of Wisdom comes from where,Proverbs in Bible +General,What is the capital of delaware,Dover, History & Holidays,Who was 'The Elephant Man'?,Joseph Merrick,Music,"What Did Sigue Sigue Sputniks ""Flaunt It"" Album Feature In Between Each Of Their Songs",Commercials / Adverts +General,Capital of egypt and the largest city in africa,Cairo,General,"What colour is angelica, used in decorating cakes",Green,General,"In the tv series 'happy days', what was the fonz's full name",Arthur +Science & Nature,What is the only insect that can turn its head?,Praying mantis, History & Holidays,What was the name of the nuclear missle defense system Reagan proposed? ,Star Wars ,General,Wife Beware in 1933 was the first film shown where,A Drive in theatre +Entertainment,What was used for blood in the film 'psycho'?,Chocolate syrup,General,What is the first race in the Grand Prix season,Brazilian,General,Charles l was brought to trial in which year,1649 +General,Where was the first shopping mall opened,Saint louis,General,What does a pedometer measure,Walking distance,General,What is the tallest bird in the world,Ostrich +General,What is Steganography,Invisible ink writing,Music,"The Film ""Sixteen Candles"" Featured In A Song Called ""If You Were Here"" By Which Band",The Thompson Twins,General,Chachi was a character in Happy days whats it mean in Korean,Penis +General,What is the first event in the Decathlon,100 metres,General,Who was the brave Norse god of war & justice,Tyr,General,Which comedian and actors real first name were Leslie Townes,Bob Hope +General,"What were the B-52's named after? (Hint, it's not a plane)",Beehive,General,What is the name of Captain Ahab's ship,Peaquod,General,"From Which Language Does The Word ""Typhoon"" Originate",Chinese +General,What is the name of the wave generator in a microwave overt',Magnetron,General,What vegetable offers the highest source of calcium,Spinach,General,An anaesthetic injected close to the spinal cord,Epidural +General,What type of animal is a Samoyed,Dog,General,Of what did sigmund freud have a morbid fear,Ferns,General,What is the boy scout motto,Be prepared +Religion & Mythology,Who is the Norse god of the sky and thunder ?,Thor,General,What is the circle of the earth at 0 degrees latitude,Equator,General,Parturiphobia is the fear of,Childbirth +General,Who was fred flinstone's best friend,Barney rubble,General,What play was being performed when Abraham Lincoln was shot,Our american,General,Who was the first French women's designer to design for men,Pierre Cardin +General,Name the second most commonly spoken language in Australia,Italian,General,Bunny Austin first British male to do what at Wimbledon in 1933,Wear Shorts,Entertainment,"""He's So Fine"", ""One Fine Day"" and ""A Love So Fine"" where hits for what fine group?",The Chiffons +Geography,What is the capital of Mongolia,Ulaanbaatar,General,What is the correct name for a baby otter,Kitten,General,Ness What are panatelas,Cigars +General,Egg Fu is the enemy of what super hero,Wonder Woman,General,Of marlboro what latin word is used for information of little use,Trivia,General,Who is The Incredible Hulks girlfriend,Betty Ross +General,In the streets of Elko Nevada walkers are meant to wear what,Masks,General,Patsy cline is the most noted with pop-country crossovers. which other singer should not be overlooked for her hits 'break it to me gently' and 'fool no. 1',Brenda lee,General,Wired Digital Incorporated created which search engine,Hotbot +General,In Biker Slang what is a Belly Shover,A Racing bike,Science & Nature,Who Propsed The Theory Of Relativity ,Albert Einstein ,Music,Which Early Rock N Roller Is Known As The Killer,Jerry Lee Lewis +General,What country would you go to find Pervy Shag,Russia,General,What colour lenses are required to view a Anaglyph 3-D film?,Red and (blue or green),General,"In The Movie ""Back To The Future "" Marty McFly Travelled Back In Time To 1955 But What Was The Exact Date",Nov 05 / 1955 +General,Where is the grande canal,Venice,Art & Literature,Which Tavern Was The Favourite Haunt Of Falstaff In Shakespear's Henry IV ,The Boar's Head ,General,"Who has been married to joan, joanne, joanna and alexis",Johnny carson +General,In film who is the alter ego of Daniel Hillard,Mrs Doubtfire,General, What is a female swan called,Pen,Entertainment,What comic strip character is Beetle Bailey's sister,Lois (of hi and lois ) +Tech & Video Games,What was the name of the first plane ever to fly ?,Flyer,General,Where would you find your corrugator,Your Forehead,Sports & Leisure,Jaques Villeneuve replaced whom as a driver for Williams? ,David Coultarde  +General,What is a group of this animal called: Hen,Brood,General,What is a group of this animal called: Magpie,Tiding,Science & Nature,What type of animal is pulex irritans ,Flea  +General,What is the atomic number of tungsten,74,General,In what city is the Olympic torch first lit?,Olympia,General,"What sport has sprint, tandem and team pursuit events",Cycling +General,Which Apostle refused to believe in Christ's resurrection until he had seen His wounds,Thomas,Food & Drink,"According to the TV Ad, which beer is said to be 'Good for you'? ",Guiness ,General,Which 1996 Movie Was The First To Be Relased On DVD,Twister +General,What was Skippy ( on TV ),The bush kangaroo,General,In which month of the year are Nobel Prizes presented,December,Music,Dionne Warwicks Cousin Is A Famous Vocalist Name Her,Whitney Houston +Food & Drink,Which Italian city gave its name to a cheese and a type of ham? ,Parma ,Music,"Who Had A Hit In 1989 With ""I Want It Alll""",Queen,General,In which USA state is Churchill Downs racetrack,Louisville Kentucky +General,Which state is called the pelican state,Louisiana, History & Holidays,Who said: there is nothing in the bible that says i must wear rags ,Billy graham ,General,In what Australian state would you find Mackay,Queensland + Geography,What is the capital of Madagascar ?,Antananarivo,General,Who wrote the Royal Firework Music,George Friedric Handel,Science & Nature,"Excluding man, what is the longest_lived land mammal",Elephant +General,What shakespearean king was actually king of scotland for 17 years,Macbeth,General,What disney film boasts the song that's what uncle remus said,Song of the,General,Who became the oldest rookie in the major leagues at age 42,Satchel paige +Sports & Leisure,Basketball: The Denver _________.,Nuggets,General,Who plays the part of Inspector Gadget in the film 'Gadget',Matthew broderick,General,What do you call a man who has never been married,Bachelor +General,"In the body, what is the CNS",Central nervous system,Sports & Leisure,"Other than England, which european country took part in the 1996 cricket World Cup?",Netherlands,General,Which term is used in the Christian calendar for the last few days leading up to Easter Sunday,Holy week +General,Film character played by 4 people head body voice breathing,Darth Vader,General,What instrument is also called the octave flute,Piccolo,General,Which is the only part of an oar that should get wet,Blade +General,"Popular venues for which sport are at Romford, Hackney and Wimbledon",Greyhound racing,General,Most people associate the colour green with which flavour,Mint,General,Massacre who was the narrator of the film 'texas chainsaw massacre',John larroquette +Religion & Mythology,Who is the greek equivalent of the roman god Vesta ?,Hestia,General,Which actor played the title role in the mad max series of films,Mel gibson,Entertainment,Who is the lead vocalist of U2?,Bono + History & Holidays,John F. Kennedy Airport used to be called __________.,Idlewild,Music,Who composed Rhapsody In Blue in 1924?,George Gershwin,General,What statuette is awarded annually to the best TV commercial,Clio +General,Which American state is nicknamed the 'Evergreen' state,Washington,General,What is a popular name for the wood hyacinth,Bluebell,Music,What Is The Spanish Dance Music Normally Associated With Castanets,Flamenco +Art & Literature,"A figurative movement that emerged in the United States and Britain in the late 1960s and 1970s. The subject matter, usually everyday scenes, is portrayed in an extremely detailed, exacting style. It is also called superrealism, especially when referring to sculpture.",Photorealism,Music,Which John Lennon Song Did Roxy Music Take To Number One The Year After His Death?,Jealous Guy,Music,From 1971 What Was The Name Of Lobo's Dog,Boo (Me & You And A dog Named Boo) +General,Port moresby is the capital of ______,Papua new guinea,Music,Which 10CC Hit Was Covered By Jhonny Logan In 1987,Im Not In Love,Science & Nature,What Does A Manometer Measure ,Pressure  +Food & Drink,What do American's call an Aubergine? ,Eggplant ,General,Who is the first character to speak in 'star wars',C3po, History & Holidays,What was the name of the teenage showgirl who caused MP John Profumo to resign? ,Christine Keeler  + History & Holidays,Which is the most ancient walled city?,Jericho,Music,I'm Going Slightly Mad Was a Hit In 1981 For Whom,Queen,General,In Gulliver's Travels name the flying island,Laputa + History & Holidays,This U.S. Secretary of State won the Nobel Peace Prize in 1973.,Henry Kissinger, Geography,What is the basic unit of currency for Uganda ?,Shilling,Sports & Leisure,Which boxer is known as the Dark Destroyer? ,Nigel Benn  +General,Mr Mybug was only interested in sex with Flora in what book,Cold Comfort Farm,General,"Of what are corolla, filament and stigma a part of",Flower, Geography,What American city is known as Little Havana?,Miami +Art & Literature,"A painting technique using pigments mixed with egg yolk and water. It produces clear, pure colors.",Tempera,General,What is shed when you desuamate,Skin,General,Every year 8800 people injure themselves with what,Toothpick +General,Louisa Adams was the only first lady to be what,Born outside USA,Geography,What Was The First Town Founded By Europeans In Southern Africa ,Cape Town ,General,What type of tests were National League umpires ordered to undergo in 1911,Eye tests eye test an eye test +Science & Nature,What Word Is Used To Describe Bell Shaped Flowers ,Campanulate ,General,10% (by weight) of the worlds land animals are what species,Ants, Geography,What is the basic unit of currency for Mali ?,Franc +People & Places,Who Invented The Minors Lamp ? ,Humphery Davy ,General,What does a callipygian person have,Pretty shaped buttocks,General,Collective nouns - A bale of what,Turtles +General,Which professional wrestler popped both of John Stossel's ear drums during a 20/20 interview?,"""Dr.D",Sports & Leisure,"In hockey, what is the equivalent of a rugby scrum?",Face-off,General,"What was the first country to legalize abortion, in 1935",Iceland +General,Who started Laugh O Gram productions,Walt Disney,General,Who was responsible for the infamous assination attempt on then President Reagan?,John Hinkley Jr.,General,Obsessive desire to lose weight by dieting,Anorexia nervosa +Music,Whistling As He Left Although It Got Him Down Name The Man And The Northern Town,Roger Whittaker & Durham Town, Geography,What country is Santo Domingo the capital of?,Dominican Republic,General,Who is the U.S. of America named after,Amerigo vespucci +General,Between where does the ureter carry urine,Kidneys and bladder,General,What was landscape gardener Lancelot Brown's nickname,Capability,Art & Literature,Which Of The Bronte Sisters Married The Reverend A B Nicholls In 1854 ,Charlotte  +General,What was Buddy Hollies real first name,Charles,General,Which companies first product was an electric rice cooker,Sony It shocked fired, History & Holidays,What was the name of Grotbags the witches pet in the TV show 'Emu's World' ,Croc  +Science & Nature,What is the chemical symbol for californium?,Cf,General,Taphophobia is fear of what,Buried Alive,General,Bob Cummings played which character (both names),Maxwell Smart +Sports & Leisure,In 1984 Which American Equalled Jesse Owens Four Gold Medals? ,Carl Lewis ,General,What is basmati,Rice,People & Places,Which Television Presenter And Ex-Spurs & England Footballer Became An Alcoholic ,Jimmy Greaves  +Entertainment,Who played Hopalong Cassidy?,William Boyd,General,Which sport awards the Maurice Podoloff trophy,Basketball NBA MVP,General,What French actors catch phrase Come with me to the Casbah,Charles Boyer – in Algiers +Art & Literature,"From which Shakespeare play is this line taken: ""What in a name? That which we call a rose, by any other name would smell as sweet.""",Romeo and Juliet,General,How many teeth does a mosquito have,Forty seven,General,"Advertising-supported cable television network founded in 1980 by American businessman Ted Turner and wholly owned by Turner Broadcasting System, Incorporated, which is based in Atlanta, Georgia",CNN +Sports & Leisure,"If You Potted Red, Black & Pink In Snooker What Would You Score? ",15 ,General,Which town was designated the first 'New Town' in Lancashire in 1961?,Skelmersdale,General,Who is the greek counterpart of neptune,Poseidon +General,What 42 year-old Panamanian boxer won his 89th pro fight in 1993,Roberto,General,"Which English football ground is the oldest professional football ground in the world, having hosted football since 1861, a year before Bramhall Lane?",Field Mill (Mansfield) now the One Call Stadium, History & Holidays,In which city were the Hanging Gardens?,Babylon +General,Which actor was born in Chiuhauha Mexico,Anthony Quinn, Geography,Where is the Blue Grotto - la Grotta Azzurra ?,"Capri, Italy",General,In a survey what is the most popular UK kids TV programme,The Simpsons +General,"In 1983 Which Car Manufacturer Ran A ""April Fools"" Ad For An Open Top Car That Supposedly ""Kept Out The Rain""",BMW,Science & Nature,What Does E C T Stand For ,Electroconvulsive Therapy , History & Holidays,What 'NP' Is An Opinion Survery Conducted In Alaska ,North Pole  +General,What year was the Battle of Bannockburn,1314,Geography,"The ___________ River has 1,100 tributary streams.",Amazon,Art & Literature,"Which Famous Book Begins With The Line 'On January 6, 1482, the people of Paris were awakened by the tumultuous clanging of all the bells in the city' ",The Hunchback Of Natre Damme  +General,Nine inches in nautical measure is called what,A Span,General,Name UK TV show with Jim Hacker and Sir Humphry Appleby,Yes Minister,Science & Nature," Garter snakes, though reptiles, do not __________. They bear young, just as mammals do.",Lay eggs +Science & Nature, Pandas spend about 12 hours a day eating __________,Bamboo,Food & Drink,What can be six litres of Champagne or the oldest person mentioned in the Bible? ,Metuselah ,Entertainment,"Name the band - songs include ""Strange Brew, White Room""?",Cream +General,Brent blend is a widely traded commodity - what is it,Oil,Geography,"Panama, because of a bend in the isthmus, is the only place in the world where one can see the sun rise on the Pacific Ocean and set on the ______________",Atlantic,General,E is the international car registration plate for which country,Spain (Espania) +General,As what is a swimming pool also known,Natatorium,General,What term is given to that part of the Earth which can support life,Biosphere,General,South africa is the biggest producer and exporter of ______,Mohair +Science & Nature,What Make Of Car Did Don Mclean Drive To The Levy In American Pie ,Chevy (Chevrolet) ,General,Sixty what lives in a fornicary,Ants,Geography,"Myrtle Beach, __________________ has the most mini_golf courses per area in the U.S. At last count, there were 47 in a 60 mile radius.",South carolina +General,Church law once mandated death for believing in what,A vacuum,General,"Rising in Lesotho and flowing east to the Atlantic, which is the longest river in South Africa",The orange,General,What phrase did Quantum Leap's Sam Beckett use in every show,Oh Boy +Sports & Leisure,"What connects Ray Reardon, Geoff Capes and Christopher Dean? ",Were All Policeman ,General,A person in his eighties is called a(n) ____________.,Octogenarian,Art & Literature,"In sculpting, the cutting of a form from a solid, hard material such as stone or wood, in contrast to the technique of modeling. ",Carving +General,What was originally called flowmatic,Cobol,General,The plant Gypsophila Paniculata is grown predominantly for making bouquets what is its more common name,Babies breath, History & Holidays,Who Was The First To Sing On The 1984 Band Aid Single ,Paul Young  +General,Similes as neat as a ______,Pin,General,What is the Capital of: Taiwan,Taipei,General,Who was adam and eve's third child,Seth +General,Who sang the theme song for The Love Boat,Jack Jones,General,"A flat, round hat sometimes worn by soldiers is a _____.",Beret,General,What do you call the stock market that is on the rise,Bull market +General,What jethro tull lp cover featured minstrels playing,Minstrel in the gallery,General,In 1969 what category was added to the Nobel prizes,Economics,General,Killing a person painlessly especially one suffering from an incurable disease,Euthanasia +Geography,Which Country Sent The Most Explorers To Africa ,Scotland ,People & Places,Where Do Moaris Come From ? ,New Zealand ,General,Fado is a musical style popular in which country,Portugal +General,What was the first video played on MTV Europe,Money for Nothing – Dire Straits,General,What is the Capital of: Haiti,Port-au-prince,General,What is a male cougar called,Tom +General,Where do they speak Malagasy,Madagascar,Science & Nature,"What are Helium, Neon, Argon, Krypton and Xenon ?",Noble Gases,General,How much does a baby whale gain in weight every day,Two hundred pounds +General,"From 1980 to 1995, which country produced the largest amount of crude oil",Saudi arabia,General,What country consist of 700 islands,Bahamas,General,Operation Olympic was a WW2 invasion plan not used where,Japan surrendered +General,Anthocyanins are compounds which produce what,Colors,General,Which European Country Has The Most Volcanoes?,Iceland,General,What is the scent on the artificial rabbit that is used in greyhound races,Anise +General,What became a national holiday in the u.s in 1890,Christmas, History & Holidays,"In the song The Twelve Days of Christmas, '___my true love brought to me nine___' what? ",Ladies dancing ,General,1964 Iowa City had one Tokyo the other only ones in world what,Sperm Banks +General,Which verdi opera has the aria 'la donna e mobile',Rigoletto,General,Bor and Bestla his parents Vili and Ve his brothers name him,Odin chief Norse God,Music,"Robbie Williams Has Had By Far The Most Success Of The Former Members Of Take That , Gary Barlow & Mark Owen Have Had Some Success But What Are The Names Of The Other Least Successful 2",Jason Orange & Howard Donald +Food & Drink,What Do You Add To Advocat To Make A Snowball ,Lemonade ,Music,This Is My Truth Tell Me Yours Was The 5th Manic Street Preachers Album Name The Previous 4,"Generation Terrorists, Gold Against The Soul , The Holy Bible, everything Must Go",Sports & Leisure,What Sport Is Played By The Minnesota Twins? ,Baseball  +General,Boob Day in Spain is what day in Britain (practical jokes played),April Fools Day 1st April,General,"In the TV series 'Absolutely Fabulous, who played the part of 'Bubbles'",Jane horrocks,Science & Nature," Some __________ Pretend to be dead when captured, but quickly hop away when let go.",Bullfrogs +Sports & Leisure,Who Won A Seventh World Snooker Title In 1994? ,Alison Fisher , History & Holidays,Which series of films features a habitual serial killer called Michael Myers ,The Halloween series of films ,General,James Outram invented what,Tramways +General,What is the worlds most popular green vegetable,Lettuce,Science & Nature," The average porcupine has more than 30,000 quills. Porcupines are excellent swimmers because their quills are hollow and serve as pontoons to keep them __________",Afloat,General,The liver of what is highly intoxicating to eskimos,Polar bear +General,What do koala bears dine on,Eucalyptus leaves,Sports & Leisure,How Many Consecutive Wimbledons Men's Singles Titles Did Bjorn Borg Win ,5 ,General,"Who Is The Famous Offspring Of ""Lara Lor Vari""",Superman +Music,Who Was The First Person To Cover A Lennon And McCartney Song,"Kenny Lynch ""Misery""",General,Lake Nyasa forms most of which country's eastern border,Malawi,General,Where is the largest gold refinery,South africa +General,Whose autobiography was entitled The Sport of Queens,Dick Francis,General,Which American author wrote The turn of the Screw,Henry James,General,"In a roman basilika, the central aisle. In a church, the main section extending from the entrance to the crossing.",Nave +General,What title character of a children's book turned his enemies into butter,Sambo,General,Ncaa: which team lost the men's basketball championship in 1955,La salle,People & Places,Who Was Faamous For Her 'Pomes' ,Pam Ayers  +General,Fangio the greatest ever F1 driver once had what job,Bus Driver,Geography,Which King Hired Henry Stanley To Carve Out An Empire For Him In Central Africa ,King Leopold II Of Belgium ,Geography,What Is The Worlds Busiest Seaport Handling The Most Tonnage Of Cargo ,Rotterdam  +General,Why are we playing trivia,Because we are bored for fun,General,Which member of the Monty Python team turned up as an English sheriff in the spoof western Silverado,John cleese,General,Who is the dog in 'the grinch who stole christmas',Max +Religion & Mythology,"In Greek mythology, into what did Athena turn Arachne?",Spider,General,Which norse god had the valkyries as handmaidens,Odin,Science & Nature,What are the units of measurement for Force ?,Newton +General,Fabled creature Head Man Body Lion Tail Scorpion Pork quills,Manticore,General,Cous-cous is Iranian for what,Vagina,Geography,Which Countries Are Divided By The 49th Parallel ,The Usa & Canada  +General,To what is coal sometimes added for sweetening,Softdrinks, History & Holidays,On May 1st 1931 the world's then tallest building was opened in New York City. which building was it? ,Empire States Building ,General,Who directed the films 'La Strada' and 'La Dolce Vita',Fellini +General,Which is the largest river forming part of the u.s-mexico border,Rio grande, History & Holidays,Which famous halloween party game originated from a custom to establish who would get married first ,Bobbing for apples. ,General,Who was captured in the third last row of the texas theatre,Lee harvey +General,Whose record did Babe Ruth break when he hit 60 home runs in 1927,His own babe ruth babe ruth's his,General,In what US state is area 51,Colorado,General,What colour is Spock's blood,Green +General,Who invented the potato chip,George crum,Geography,In which state is mount vernon ,Virginia ,General,Which industry uses the gravure method,Printing + Geography,What is the basic unit of currency for Libya ?,Dinar,General,Mosi-oa-Tunya - Smoke that Thunders - what natural feature,Victoria falls,General,What was Socrates wife's name,Xanthippe +General,"Plant community, predominantly of trees or other woody vegetation, occupying an extensive area of land",Forest,General,What was Barnaby Jones usual tipple,Milk,General,What did Lilius invent< Clavius complete and Pope Gregory XIII introduce,Gregorian calander +Food & Drink,What fruit contains the most calories? ,The Avocado Pear ,General,Who is the roman god of agriculture,Saturn,Toys & Games,A poker hand consisting of three of a kind and a pair is called a _______.,Full house +General,Poona was the original name of what sport/game,Badminton, History & Holidays,Name The First Steam Engine To Run Between Stockton & Darlington? ,Locomotion No1 ,General,As what was Istanbul previously known,Constantinople +General,At what angle above the horizon must the sun be to create a rainbow,Forty 40,General,What countries national anthem is The Bayambo song,Cuba,General,Ophidiophobia is the fear of,Snakes +Tech & Video Games,Linux is a clone of what operating system?,UNIX,Art & Literature,What Was The Name Of William Wordworths Sister ,Dorothy ,Science & Nature,Ethylene glycol is frequently used in automobiles.. How,Anti_freeze +Art & Literature,Who created Winnie the Pooh?,A. A. Milne,Music,"Mark Knopfler Produced An Album For Which Of These Acts ""Aztec Camera"" Or ""Huey Lewis""",Aztec Camera,General,What shape is formed by one side of a pyramid,Triangle +Science & Nature,The latin word for lips is:,Labia,General,Vor was the Norse Goddess of what,Truth,General,Xizang (spelt X-I-Z-A-N-G) is now a province of China. What is its other name,Tibet +General,What strongman was the original 97 pound weakling,Charles atlas,General,Here we go round the mulberry bush - what was original bush,Tree in Wakefield prison walked round,General,With what name did pablo picasso sign his paintings,Picasso +General,The term Septiquinquennial represents how many years ?,75,General,Sieze control of vehicle,Hijack,General,Orpheus went into the underworld to rescue who,Eurydice + Geography,On what island is Pearl Harbour?,Oahu,General,What was exchanged in Fair Exchange,Teenage daughters,General,Israel Baline became more famous under what name,Irving Berlin +Science & Nature,On A Standard Computer Keyboard Which Key Is The Largest ,The Space Bar ,General,Dr Susan Lark recommend what cure for menstrual cramps,Orgasm – increased blood flow helps,General,"Where would you see rope, hoop, ball and ribbon used",Modern rhythmic gymnastics +General,Name the country that starts with A but does not end with A,Afghanistan,General,"Which female vocalist released an album entitled ""Drag""",K d lang,Art & Literature,Who wrote 'Rendezvous with Rama'?,Sir Arthur C. Clarke +Geography,Which Famous Egyptian Queen Ordered The Expedition To Punt & Insisted On Dressing As A King While Wearing A False Beard ,Hatshepsut ,General,"What hardcore rock group sings, 'blind' and 'clown'",Korn,Music,"What was the working title of The Beatles Song ""With A Little Help From My Friends""?",Bad Finger Boogie + Geography,What is the basic unit of currency for Argentina ?,Peso,General,In the acronym BASIC for what does the letter B stand,Beginners',General,Tomblike monument to persons whose remains are elsewhere,Cenotaph +Geography,In which continent would you find the niger river ,Africa ,Geography,What is the capital of Tajikistan,Dushanbe,Sports & Leisure,Which Acid Is Produced In The Muscles During Strenuous Exercise ,Lactic  +General,"Hammer, anvil, and stirrup are parts of the ______.",Ear,General,Tigers have stripped fur - what colour is their skin,Stripped,General,In which country do the Sumi people live,Lapland +General,In which European country is Tokay wine produced,Hungary,General,Java is part of what country,Indonesia,General,Shoot a Waco was the original name for what drink,Dr Peppers +General,Analysis and manipulation of an image,Processing,Sports & Leisure,According To A Survey Conducted In 2000 Which 2 People Were Voted The Greatest Male & Female Tennis Players Of All Time Respectively? ,Bjorn Borg Martina Navratlilova ,General,The true seals are a diverse & widely distributed group of mostly marine & ___________,Aquatic mammals +General,Galt MacDermot wrote what 1967 musical stage show,Hair,Geography,Name the capital of Argentina.,Buenos aires,General,Which of the Apostles is traditionally pictured with a purse,Matthew taxman +General,Who conquered mexico,Hernando cortez,General,What is a group of this animal called: Pigeon,Flock flight,General,What is the name of Porky Pigs nephew,Cicero +Science & Nature," Hippopotamuses have killed more people in Africa than all the lions, elephants, and water buffalo combined, usually by __________",Trampling,General,White river is the principal tributary of which indiana river,Wabash river,General,What's was the nickname of New York's 28th street in 1920s,Tin Pan Alley +General,What country was the setting for 'the king and i',Siam,Food & Drink,Of which vegetable are Globe and Jerusalem varieties? ,Artichoke ,General,What is the name given to thin pieces of crisp toast,Melba +General,What area in the US translates from the Dutch as Broken Valley,Brooklyn,General,Who deposed Helmut Kohl as Chancellor of the Federal German Republic in 1998,Gerhard schroder,General,David Hasselhof spent most of his time driving a car on which eighties tv show?,Knight Rider +General,What's the name of the worlds first National theatre Paris 1680,Comedie Francaise,Art & Literature,In The Canterbury Tales At Which Tavern Do The Story Tellers Assemble ,The Tabard ,Science & Nature,What Is Alicante A Variety Of? ,Tomato  +General,"What is a ""tail piece"" rounding off a musical composition called",Coda,General,Kenneth Daigneau won $100 for naming which product,Spam,General,Speed skating started in which country,Netherlands + History & Holidays,Where Was A Famous (Tea Party) Held In 1773? ,Boston Tea Party ,General,Where are the Descartes highlands,On the moon,General,What was the name of Papa Doc Duvaliers secret police Haiti,Tonton Macoute +General,Who is the last Italian-born driver to have been Motor Racing Formula One world champion,Mario andretti,General,The berlin air lift was known as,Operation vittles,General,What is a snood,A kind of hairnet + History & Holidays,"In What Year Was The French Declaration Of The Rights Of Man (Liberty, Equality, Fraternity) ",1789 ,General,What is the only country in Southeast Asia to never be ruled by a European nation,Thailand,General,Operation Dracula in WWII freed what city,Rangoon Burma +Science & Nature,How many colors are there in a rainbow ,Seven ,General,What did Canada's Grand Falls change their name to 1964,Churchill,General,What's the square root of one-quarter,One half +Art & Literature,Homer wrote this account of the Trojan War.,Iliad, History & Holidays,Which actor won an Oscar in 1955 for their role in the movie 'The King and I'' ,Yul Brynner ,General,"What's the international radio code word for the letter ""R""",Romeo +General,What was the date in 1955 did Marty from Back to the Future arrive on.,"November 5th,1955",General,Which country was host to the 1999 cricket world cup?,England,General,A bending of the knees in any of the five positions.,Plié + Geography,What is the capital of Albania?,Tirana,General,The Adventure Gallery was whose ship,Captain Kidd,General,The italian bread focaccia gets its name from the Latin word for what,Hearth +General,The length of what is approximately 1/10th circumference of earth,Great wall of China,General,Who wrote 'the happy prince',Oscar wilde,Sports & Leisure,What sport has a hooker in a scrum?,Rugby +General,How many countries border the black sea,Four,Sports & Leisure,In A Game Of Tennis What Score Follows 'Deuce'' ,Advantage ,General,Which dessert is named after a ballerina,Pavlova +General,Which member of lily family is named from Greek for sprout,Asparagus,General,Who Is The Greek Goddess Of Love ,Aprodite ,Science & Nature, Male __________ lose the hair on their heads in the same manner men do.,Monkeys +General,The stinking cedar and yellow-wood trees are also known as what,Gopher wood,Entertainment,Who was Dr. Zhivago's great love?,Lara,General,The opening lines of which classic Russian novel are in French,War and Peace eh bien mon prince +General,Where is the grave of Oscar Schindler,Jerusalem, History & Holidays,Which Famous Venetian Merchant Travelled To China And Worked For Kublai Khan? ,Marco Polo ,General,"In Which Disney Movie Were There Characters John, Paul, George, & Ringo",The Jungle Book +Entertainment,"A graphical representation of the guitar fingerboard, used to teach someone to play a guitar without actually learning how to read musical notes.?",Tablature,General,For who was deana carter named,Dean martin, Geography,What is the basic unit of currency for Costa Rica ?,Colon +Music,She Blinded Thomas Dolby Wioth What,Science,General,The Chinese were using aluminum to make things as early as ___ ad,300,General,Which Country Were The First Ever Winners Of The European Championship Football Finals In 1960,Russia +General,What is a group of hens called,Brood,General,Where was the newspaper 'pravda' first published,Russia,General,Valentine Dyall was the voice of the computer `Deep Thought' in which 1980s TV series ,The Hitchhiker's Guide to the Galaxy  +General,Who is identified with the word 'eureka',Archimedes, History & Holidays,How many oklahoma land runs were there ,Six ,General,What is the name of the lift used to raise boats from the River Weaver to the canal system called,Anderton lift +General,What is the German word for poison,Gift,General,What size is A-0 paper?,One Square Meter,General,"What was the name of the rich boy that Andie was asked to go to the senior prom with in the movie ""Pretty in Pink""?",Blaine +General,Aconite the poison is obtained from what plant,Wolf's-bane,General,In legend who killed the mobster Grendel,Beowulf,General,Which group had their first U.K. number one hit ill 1973 with Rubber Bullets,Ten cc +General,Actor was known as Singing Sandy (dubbed) early in career,John Wayne,General,"What is the number 174,465 that belongs to the telephone",U.s patent number,General,What small island is in the bay of naples,Isle of capri + History & Holidays,Which US president said 'the buck stops here'?,Harry Truman,General,In Tucson Arizona it is illegal for a woman to wear what,Pants,General,What star once sold lingerie door to door,Burt Lancaster +General,Which Country Purchases The Largest Amount Of Condoms Each Year & Has Done So Since 1982,Japan,Music,Rick Olasek Was The Lead Singer Of Which Band,The Cars,General,Which Breed Of Dog Shares It's Name With A Costal Reagion Of Canada,Labrador +General,What is a 'niblick',Golfer's nine iron,General,"Who Played Harmonica On The Eurythmics Hit ""There Must Be An Angel Playing With My Heart""",Stevie Wonder,Food & Drink,Which sauce is made with mayonnaise and chopped pickled gherkin? ,Tartare Sauce  +General,Mckinley what u.s secretary of state bought alaska from russia for 7.2 million dollars,Seward,Sports & Leisure,What does it mean if a racehorse is described as a maiden? ,It hasn`t won a race yet ,General,What is geophagy,Practise of eating soil +General,85% of American people will eat what this year,Spam,General,What is the Capital of: Saint Lucia,Castries,General,What was the first movie to have a sequel - 1933,King Kong - Son of Kong +Geography,"Which is the most famous castle in County Cork, Ireland? ",Blarney Castle ,Music,Number of Beatles biographies registered at the Library of Congress,"177= Beatles, John=69, Paul=23, George=6, Ringo=2",General,"First produced in 1960, which Lerner and Loewe musical contains the songs - What Do the Simple Folk Do and If Ever I Would Leave You",Camelot +General,In Florida its against the law to put what on the school bus,Livestock,General,Shirley Manson Is The Lead Singer With Which Group,Garbage,General,A flower with brightly coloured daisy like flowers,Aster +General,"Name both of the planets that can be seen, from the Earth, 'in transit' - i.e. passing in front of the sun",Mercury & venus,Science & Nature,"What Is Meant By The Name Saxifrage , Which Grows In Cracks In Rocks ",Stone Breaker ,General,The film 'The Bishop's Wife' was released in what year,1947 +General,Who founded digital research,Gary kildall,General,Capital of Ghana,Accra,General,Which Christian Feast is celebrated on 1st November,All saints +General,The average human body contains enough phosphorous to make how many match heads,2000,General,What was checkpoint Charlie named after,Charlie – Phonetic Alphabet,General,"Which Actor/Comedian Provided The Voice Of ""Zazu"" In The Movie The Lion King",Rowan Atkinson +Food & Drink,What Wine Is Traditionally Matched With Pate And Brioche ,Sauternes ,General,Which Footballer Became The Very First European Footballer Of The Year,Stanley Matthews,General,Baby's Breath' is the common name for which plant,Gypsophila +General,Which Character of Tv & Film Had The Maiden Name Of Betty Jean McBricker,Betty Rubble,General,What stadium is home to the Seattle Mariners,Kingdome,General,Who is the chairman of the U.S. Federal Reserve Bank,Alan greenspan +General,What is the largest island in Asia,Borneo,Sports & Leisure,In 1980 An Official Was Knocked Unconscious Whilst Measuring In The WHHC What Does The Acronym WHHC Stand For ,World Haggis Hurling Championships ,General,In which Italian city would you find the headquarters of the Pirelli company,Milan +General,What is produced using the Kroll process,Titanium,General,What does an anthropophagist eat,People,General,What does an agriologist study,Primitive cultures +General,What is a group of moles,Labor,Music,What Did Air Supply Run Out Of In 1980,Love (All Out Of Love),General,"Where did two Boeing 747 aeroplanes collide on a runway in 1977, with 583 fatalities",Tenerife +General,Why were women barred from original Olympic Games,Male entries nude,Religion & Mythology,What is the shortest verse in the bible? (John 11:35),Jesus wept.,Technology & Video Games,"In Super Mario Bros. 2, how many extra lives do you get for spinning three 7s on the slot machine? ",10 + History & Holidays,In which film does a Moloko Plus loving Ludwig van fan live with his 'M' and 'P' ? ,A Clockwork Orange ,Music,"Which Gender Bender Had His Only Hit With ""Calling Your Name"" In 1983",Marilyn,General,Which actor once finished as 'runner-up' in the Le Mans 24 Hour Race,Paul Newman +General,Emeralds come from which mineral,Beryl,General,Who wrote The Little Prince,Antoine de saint-exupery,General,The port of Chittagong is in which country,Bangladesh +Food & Drink,Naughtiness Drug' is an anagram of which drink? ,Draught Guinness ,Science & Nature,Apart From Humans There Is Only One Other Creature That Has Sex For Pleasure Can You Name It ,Dolphin ,Science & Nature,The spot on the Earth's surface directly above an earthquake's focus is called the ______.,Epicenter +General,What sentence uses every letter of the alphabet,The quick brown fox jumps over the lazy dog,General,Who took the first space walk,Aleksei leonov,Music,"Singles Featuring Paul Mccartney Have Sold More Than 20, 60 Or 100 Million",100 Million +General,What famous building did sir john vanbrugh design?,Blenheim palace,General,The sprat belongs to what fish family,Herring,General,Where did cuneiform originate,Sumer +General,"What links Pythagoras, Hitler, GB Shaw and Henry Thoreau",Vegetarians,General,What is the most common surname in the world?,Chang, History & Holidays,What did President J. Buchanan not have?,A wife +Science & Nature,"On the Periodic Table, what is the Last element in alphabetical order?",Zirconium,Music,Which Henry Mancini/ Johnny Mercer Song Was Sung By Audrey Hepburn In Breakfast At Tiffany's,Moon River,General,What is the name given to a group of porpoises,School +General,What was The King of Trains and The Train of Kings,The Orient Express,General,In the language of flowers what does yellow lily mean,Falsehood,General,Al Borak was a flying horse owned by whom,Mohammed +Science & Nature,Which Electronics Company Dominates The Dutch City Of Eindhoven ,Philips ,Religion & Mythology,Who replaced Moses as the prophet of the Israelites?,Joshua,General,Who won an Oscar for best supporting actor in Spartacus 1960,Peter Ustinov +General,What Book Was Mark David Chapman Carrying When He Shot John Lennon In New York 1980?,The Catcher In The Rye,General,De Witt Wallace founded what,Readers Digest,General,The elephant can smell water up to how many miles away,Three +General,Which silent films stars contract forbade him smiling on screen,Buster Keaton,General,"Who, as of October 1998, is the Prime Minister of Israel",Benjamin netanyahu,General,Noctiphobia is a fear of ______,Night +General,What is epidaurus famous for,Greek theatre,General,What is a group of cats,Clowder,General,What tropic passes through Australia,Tropic of capricorn +General,How much did the hammer weigh that john henry swung,9 pounds,General,What is a group of foxes,Shulk,Music,"Who Co Wrote Band Aids Single ""Do They Know It's Christmas"" With Bob Geldof",Midge Ure +General,The first example of which type of brain-teaser appeared in the New York World newspaper in 1913,Crossword,General,Who makes Kleenex tissues,Kimberly Clark,General,What does GNP stand for ?,Gross National Product +General,Sir Richard Marsh was Chairman of British Rail in the 1970s and 1980s. He had earlier been prominent in what field,Politics (cabinet minister),General,Which Highly Sucessful Tv Show Of The 1980's Featured A Dog Called Muffit,Batllestar Galactica,Music,Who Won Best International Group At The Brit Awards In 1994,Crowded House +General,What is a group of rhinocerouses,Crash, Geography,Which is the only South East Asian country that is a member of the British Commonwealth?,Malaysia,Science & Nature,Where Does A Gopher Make Its Home? ,Underground  +General,The Westminster is the oldest and biggest what in the USA,Dog Show,General,More that I/3 adults do what average 3 time each morning,Hit Snooze button on alarm clock,Music,Who Helped Echo Have A Bunch Of Hits In The 80's,The Bunnymen +General,How did Liverpool football club get Anfield,Everton evicted not paying rent,Sports & Leisure,In Which British Sport Is A Leather Ball Hit By A Gloved Hand ,Fives ,General,How did Joy Friedericke Victoria Adamson die in 1985,Murdered in Kenya +General,Danakill tribe Ethiopia - mans grave 1 stone for each what he did,Man Killed,Music,What Was Love For Pat Benatar,A Battlefield,General,Which of the Great Train Robbers became a florist outside Waterloo station,Buster edwards +General,Peter Falk plays Lt Colombo but who was first offered role,Bing Crosby, History & Holidays,Who Played Bowls Before Engaging The Spanish Armada? ,Sir Francis Drake , Geography,What is the basic unit of currency for Micronesia ?,Dollar +General,In what profession is a 'ruderal,Gardening,People & Places,This Person Voiced The Muppet Characters Fozzie Bear & Kermit The Frog He Also Supplied The Voice To Yoda In The Star Wars Movies What was His Name? ,Frank Oz ,Music,Which Beatles song links tangerine trees and marmalade skies?,Lucy In The Sky With Diamonds +General,A 25 to 31 mph wind on the Beaufort scale is called what,Strong Breeze, History & Holidays,In the film 'The Santa Clause' who plays Santa? ,Tim Allen ,Art & Literature,"Author of such works as Gravity's Rainbow, V, The Crying of Lot 49 and most recently, Mason & Dixon?",Thomas Pynchon +General,A severe skin abscess or a bright red jewel,Carbuncle,General,What is the alternative name for rabies,Hydrophobia,General,What colour is yak's milk,Pink +General,In The Movie Toy Story 2 Who Proved The Voice Of Stinky Pete The Prospector,Kelsey Grammer,General,"In the film 'the lovebug', what number was painted on the side of 'herbie' the vw bug",Fifty three,General,What rodent is famous for building dams,Beaver +Music,"Who Had A Hit In 1983 With ""Hold Me Now""",Thompson Twins,Music,Which Record Label Did Abba Release All Of Their 70's Singles On,Epic,Sports & Leisure,What sport do the Harlem Globetrotters play,Basketball +General,What links Caprino Ziegenkase and Gaiskasli,Goats Cheese, History & Holidays,When did Christmas become a Federal holiday in the U.S.? ,1831 , Geography,What is the capital of Ghana ?,Accra + History & Holidays,Where did 'The Mayflower' take the pilgrims?,New World,Music,Which English punk band had Rat Scabies on drums?,The Damned,General,What unit of currency is used in Italy,Lira +General,"Knife, Clown and Pencil are types of",Tropical fish,General,In Britain a suffragette threw herself under the kings horse in what year,1913,General,A member of the largest group of algae in the golden algae phylum,Diatom +General,"What Was John Hinckley Famous For In 1981, Even Though He Wasn't Successful In His Task?",Shot Ronald Regan,General,What device did Henry Doherty patent in 1972,Pooper Scooper,General,What is a mud puppy,American Salamander +Sports & Leisure,Which sport takes place in a velodrome? ,Cycling ,Sports & Leisure,In 1985 who became the first unseeded man to win Wimbledon? ,Boris Becker ,General,"Which connects Delft, Sevres, Wedgwood, Chelsea",Porcelain +General,What is the fear of bees known as,Melissophobia,General,The Voyage of the Beagle told of which scientist's discoveries,Charles darwin,Science & Nature,What is the name of the world wide organization initiated by jean henri dunant in 1962 ,The red cross  +General,Men are ten times more likely than women to have what,Colour Blindness,General,Walt Disney in an interview admitted he was scared of what,Mice,General,In Greek mythology who built the labyrinth,King Daedalus + Geography,As what is Formosa now known?,Taiwan,General,What is the study of weather,Meteorology,Music,"From the 1970's which song and artist “You're a rhapsody, a comedy, you're a symphony and a play. You're every love song ever written, but honey what do you see in me”?",Rod Stewart / You're In My Heart +General,What was rhoda's maiden name,Morgenstern,General,In Elizabethan England rich people carried their own folding what,Spoons to Banquets,General,"Before going solo, the singer Louise was a member of which pop group",Eternal +General,How often must one perform a quotidian task,Daily,General,What acid is produced in the stomach,Hydrochloric,General,"Largest, rarest, and most powerful anthropoid ape",Gorilla +Food & Drink,"Apparently resembling a marine creature, which morsel of meat found in a fowl's back is reckoned by some to be the tastiest? ",Oyster ,Geography,Where Is The Veldt ,South Africa ,General,For which English king did Handel compose his Water Music,George the first +General,Which American Indians modern name in Spanish means village,Pueblo,General,Bumper Harris - wooden leg - what Job on London Underground,Ride new escalators,General,What is a bandy bandy,A Snake +Geography,Which Is The Longest River In Britain ,The Severn , History & Holidays,"As at November 2005, who is the only British Prime Minister to have been appointed and dismissed four times, serving four separate terms in office? ",William Gladstone ,Music,What Is The Best Selling Rolling Stones Single In Britain,Honky Tonk Woman +Music,"Who Had A Huge Hit Expecially In The States With ""Breathe Again""",Adam Rickett,General,Which TV Heartthrob Was The First Choice To Play Indiana Jones On Film But Missed Out Due To Commitments In His TV Contract,Tom Selleck,General,Who was the first to sign the U.S. declaration of independence,John hancock +Sports & Leisure,"In Olympic Competitions, What Is The Height Of A Diving Board ",10m ,Toys & Games,Board game involving rapid climbing and tragic sliding.,Chutes(Snakes) and ladders,General,Where was the first speed limit regulation of 20 mph set,England +General,What was the name of the toy spaceman in Toy Story,Buzz lightyear,General,Distaff is the female family side - what is the male,Spear,General,What first happened on January 4th 1885 Davenport Iowa,Appendectomy +Music,Name The 1990 Western Which Won John Barry An Oscar For Best Film Score,Dances With Wolves,Geography,What Is The Capital Of Iran ,Teheran ,General,Who coined the phrase 'good to the last drop',Theodore roosevelt +General,Inanna is the sumerian goddess of ______,"Love, fertility and war",General," This word means ""split personality"".",Schizophrenia,General,What kind of bird is a 'poussin',Chicken +Music,Who Changed His Name From Mark McLoughlin,Marti Pellow,General,A ships officer in charge of equipment and crew,Boatswain, History & Holidays,Which was the sacred animal of ancient egypt ,Cat  +General,Who starred as Bill Bittinger in Buffalo Bill,Dabney coleman,General,In 1912 Chinese republic proclaimed in,Tibet,Science & Nature,What Is AG The Chemical Symbol For ,Silver  +Music,French Kissin In The USA Was A Hit For Who,Dennie Harry,General,In 1947 it began at Callao Peru ended Tuamotu Island what did,Kon Tiki Expedition,General,"You've got a lot of repressed feelings, don't you (..)? Must be what keeps your hair up. (.. Name with held to protect the innocent)",Dragnet +General,"Who played the inventor in 'honey, i shrunk the kids'",Rick moranis,Sports & Leisure,Which Class Is The Lowest Weight In Professional Boxing ,Straw Weight , History & Holidays,What was the most Eastern country in Alexander The Great's command at his peak?,India +Music,"Name The Year Hotel California, Elvis Presley Dies, Ronan Keating Born",1977,General,What's the only day named for a planet,Saturday,General,In what city was Americas first stock exchange built,Philadelphia +General,What is dendrochronology,Tree ring dating,General,"In the u.s, for how long is a patent good",Seventeen years,Music,"He's Luke He's Five & His Dads Bruce Lee, But What Does His Dad Spend Most Of His Time Doing",Driving His JCB +General,Who won the world soccer championship in 1982,Italy,Music,With Which Artist Is The Funk Band The New Power Generation Associated,Prince,General,Which male mammals have the highest rate of homosexuality,Bats +General,Banjul is the capital of ______,Gambia,General,"A continuous aisle in a building, especially around the apse in a church. ",Ambulatory,General,A U.S. dime is worth ___ cents.,10 +Food & Drink,Which cooking term is used to describe vegetables cut into very thin strips and cooked slowly in butter? ,Julienne ,General,Who did lennox lewis represent at the olympics,Canada,General,What country was formerly called Ceylon?,Sri Lanka +General,He began his career as a lead vocalist with the group 'them' during the early british invasion.,Van morrison,General,Small piece of bread or pastry with a savoury topping,Canape,General,In the Bible what was the sixth plague of Egypt,Water into Blood +Music,What Is The Name Of Dolly Partons Theme Park In Tennesse,Dollywood,General,Which people slide down a pole to help them to get to work quickly,Firemen,Science & Nature,In Computing Esp Concerning The Internet What Do The Initials HTTP Stand For ,Hyper Text Transfer Protocal  +Entertainment,What famous singer was known to give automobiles to complete strangers?,Elvis Presley,General,Another name for wood alcohol is___.,Methanol,General,What is the longest typed word (found so far) that alternates hands,Skepticisms +General,Where did the ancient Egyptians paint pictures of their enemies,Foot of Sandals,General,Baron Silas Greenback was the enemy of which character,Dangermouse,Food & Drink,How many noggins in a pint? ,4  +General,"Launfal, Pelleas and Tristram were part of what group",Knights of the Round Table,General,"______, the first felt-tip pen, is introduced to the market",Pentel,General,Who was The Man of Destiny in George Bernard Shaw's play of that name,Napoleon bonaparte +General,What was alaska before 1867,Russian america, Geography,What is the basic unit of currency for Denmark ?,Krone,Food & Drink,What Is The Fruit From The Ananas Comosus Called ,Pineapple  +General,What is the flower that stands for: enchantment,Holly herb,General,"Any structure of animals, plants, or insects that produces chemical secretions or excretions",Gland,General,In the game Tomb Raider what's Lara Crofts profession,Archaeologist +General,What is the nickname for North Dakota,Sioux state,Science & Nature,The silkworm only eats the leaves of what plant?,Mulberry,General,Montana whom did aristotle onasis ditch for jacqueline kennedy,Maria callas +General,What is the most popular meal ordered in US restaurants,Fried Chicken,General,What year was the Americans first atomic submarine launched,1954,General,"As of November 2000, which golfer has won the most prize money in international competition",Greg norman +General,This planet has an atmosphere that is 98 percent Helium?,Mercury,General,Which protein forms nails and hair,Keratin,General,What band recorded the 1978 hit album: 'Briefcase Full of Blues'?,The Blues Brothers +Sports & Leisure,In what sport did the word 'crestfallen' originate?,Cockfighting,Food & Drink,"Cornflakes were invented in What Year 1863, 1890 or 1915 ",1890 ,General,What name is given to a division of a Hells Angels club,Chapter +General,Which Greek philosopher was appointed tutor to Alexander the Great in 342 B.C.,Aristotle,General,Funeral March of a Marionette theme tune to what old TV show,Alfred Hitchcock Presents,General,Who abolished christmas in 1647,English parliament +General,Valois wine is produced by which European country,Switzerland,General,"When asked what she did for a living, ""I'm a housekeeper-I get married-I get divorced-I keep the house.""",Zsa zsa gabor,General,The Punjab is an area of India meaning what,Five Rivers +General,Who reputedly first said - if in doubt tell the truth,Mark Twain,General,Except Australia 1 New Zealand 1 USA all since 1870 want?,America's Cup,General,Wadsworth In what year was Diet Pepsi introduced,1965 +General,What nationality is the racing driver Jean Alesi,French,General,What was the name of the regimental tune of the 7th cavalry,Garryowen,General,In which South American country does the plateau of Borborema lie,Brazil +General,Who wrote the line East is East and West is West,Rudyard Kipling,General,Which magician did lothar assist,Mandrake,Art & Literature,Who wrote 1984 ?,George Orwell +General,Which famous female character of TV and films was played by a male when she was first introduced to our screens in 1943?,Lassie,General,What is a poker hand consisting of three of a kind and a pair,Full house,General,A sailor who has not yet crossed the equator is referred to by what name,Pollywog +General,Approximately how many concertos were composed by Antonio Vivaldi,500,General,The very first bomb dropped by the allies on berlin during world wwii killed the only ________ in the berlin zoo,Elephant,Art & Literature,"The representation of inanimate objects in painting, drawing or photography. ",Still life +Music,"""Lost In America"" Was A 1994 Hit Who Sang It",Alice Cooper,General,"Who recorded the album ""Rio"" in 1983",Duran duran, Geography,As what is Krung Thep is more commonly known?,Bangkok +General,What is Bugs Bunny’s catch phrase?,What's up doc?,Science & Nature,This cell organelle is responsible for protein production?,Ribosome,General,Who wrote the Thin Man in 1934 (both names),Dashiell Hammett +General,What's the name of the technique for measuring at a distance,Telemetry,General,Translated literally what does television mean,Far Seeing,General,What kind of creature is a Lorikeet,A parrot +General,What trumpeter led all other jazz musicians in Ed Sullivan Show appearances,Louis armstrong,Food & Drink,"Mustard, ketchup and onions on a hotdog are all __________.",Condiments,General,"In the movie ""Better Off Dead"", what was the name of Lane's younger brother?",Badger +Sports & Leisure,Who Is Presently The President Of The Football Association? ,Prince William ,General,What does a thurifer do,Swing censor Anglican high mass,General,Who wrote Goodbye Mr Chips,James hilton +General,How many prongs are there on a dinner fork?,4,Entertainment,Who 'imagined' a better world?,John Lennon,General,What was the first known lighthouse,Pharon +Geography,What is the capital of Washington state,Olympia, Geography,"On what island is the U.S. naval base, Guantanamo?",Cuba,Art & Literature,Who Painted Flatford Mill ,Constable  +General,"What carmaker's ads boast ""we build excitement""",Pontiac,General,What is the term ph an abbreviation for,Potential of hydrogen,General,What is the tribal african word for dowry?,Lobola +General,In The Marriage of Figaro - who did Figaro marry,Sussanah,General,About 10% of the worlds population is what,Left handed,General,There is one gallon of water in every cubic mile of what,Fog +Sports & Leisure,What was Jack Nicklaus' nickname?,Golden Bear,General,What term is used by the British military to describe an incidence of friendly fire?,Blue on Blue,General,In Portland Maine it is illegal to put what under a girls chin,Feather Duster +General,Which modern author wrote The Regeneration trilogy,Pat barker,General,"In Greek mythology, gave fire to mankind",Prometheus,General,Hot springs are known as geysers after the great geysir that is located where,Iceland +General,Which once-common disease was also known as the 'white death',Tuberculosis,General,Who sang the theme tune to the latest James Bond film 'Tornorrow never dies',Sheryl crow, History & Holidays,In which country is a bell struck 33 times? ,Korea  +General,The game pailemalle played on grass evolved into what,Billiards,Science & Nature,What Type Of Creature Is The Most Poisonous In The World? ,A Frog (The 1&1/2 Inch Golden Poison Frog ) , Geography,What is the basic unit of currency for Andorra ?,Peseta +Food & Drink,Armagnac Is A Brandy Originating From Which Part Of France ,Gascony , Geography,What is the capital of Rwanda?,Kigali,General,What hospital did Dr Kildare work at,Blaire General +General,What organisation did C T Russell found,Jehovah Witnesses,Music,"Who Recorded Ther Albums ""Popped In Souled Out"" And ""Holding Back The River""",Wet Wet Wet,Science & Nature,What Does An Ammeter Measure ,Electrical Current  + History & Holidays,Who Released The 70's Album Entitled Tusk ,Fleetwood Mac ,Music,What Is The Reeded Wood-Wind Instrument Comprising A Wooden Tube Doubled Back On Itself,The Bassoon,General,What does 'anasazi' mean,The ancient ones + History & Holidays,Who Released The 70's Album Entitled The Scream ,Siouxsie and the Banshees ,Music,What Was ELO Short For,Electric Light Orchestra,General,What insect depends on sight to locate mates,Firefly +General,Where was it once against the law to have a pet dog?,Iceland,Music,Who Managed The Sex Pistols,Malcolm Mclaren,General,A squid found in New Zealand had the biggest what ever seen,Eye 15.75 inches +General,What is the Capital of: Burkina Faso,Ouagadougou,General,"The mathematical study of properties of lines, angels, etc., Is ________",Geometry,General,Where was Bob Dylan born,Duluth Minnesota +Art & Literature,Who won a Pulitzer Prize for Angela's Ashes? ,Frank McCourt ,General,"First Defence, In Home or Second Attack positions what sport",Lacrosse,General,"What line precedes ""You're the salt in my stew""",You're the cream in my +General,Whats the German name for their own country,Deutschland,General,Who experimented with the idea of contact lenses,Leonardo da vinci,Religion & Mythology,Who is the greek equivalent of the roman god Jupiter ?,Zeus +Music,Which Female SingerSpent Most Weeks In The Charts In 1995 & 1996,Celine Dion,General,"In the television series The Prisoner, what seas Patrick McGoohan's number",Six,General,"Vega mechanical device that produces a force, or thrust, along the axis of rotation when rotated in a fluid, gas or liquid",Propeller +General,What colour is natural cheddar cheese,White – it’s dyed red,General,"What islands were once called the ""fortunate islands"" or ""isles of the blest""",Canary islands canary,General,Baseball: the milwaukee ______,Brewers +General,"In the USA, for how many years is a patent good?",Seventeen,People & Places,Who Was The First Ever Man Seen On Channel 4? ,Richard Whitely ,General,Which month has a diamond as a birthstone?,April + History & Holidays,What's the resting place of those buried at sea?,Davey Jones's Locker,General,What do the letters 'r.e.m.' stand for,Rapid eye movement, Geography,What is the basic unit of currency for Lebanon ?,Livre +General,Which European country eats the most breakfast cereal,Britain,General,What does the family name Perkins mean,The Rock,General,Where are phalanges,Hand +General,Scotophobia is the fear of what,Being seen by others,General,"What Was The First UK Top Ten Hit For ""Adam And The Ants""",Dog Eat Dog,General,"The name ""Lego"" comes from which Danish words ?",Leg godt +Music,"Who As A Joint Artist Has Had Hits With Marvin Gaye, Lionel Richie & Julio Iglesias",Diana Ross,General,Earl Grey is a China tea flavoured with what,Bergamot,General,When was nelson mandela released from robben island,1990 +General,Who directed the 1962 film Lawrence of Arabia,David Lean,General,Who played the forger in the film The Great Escape,Donald pleasance,General,Who were the learned class of the ancient Celts whose name means 'knowing the oak tree',Druids +Music,"Which Controversial Star Had A No.1 Hit With ""Stan""",Eminem,General,How many hearts do earthworms have,Five,General,Cry Freedom was Richard Attenborough's film about who,Steve Biko +General,Who discovered Tasmania,Abel tasman, Geography,Which Canadian province extends farthest north?,Quebec,General,What 1955 fad was good for New York City fur dealers,Coonskin hats +General,What is a young whale,Calf,General,What country has the worlds largest merchant navy,Liberia,Science & Nature,What Type Of Creatures Are Cetaceans? ,"Whales,Dolphins And Porpoises " +General,Which infectious disease is known as Kissing Disease,Glandular fever,General,In Tennis where is the Australian Open played,Flinders Park,Technology & Video Games,"The quote ""You spoony bard!"" is from what game? ",Final FantasyIV +General,"What is made with a mix of charcoal, saltpetre and sulphur",Gunpowder,Religion & Mythology,In ancient Egypt which animal was considered sacred ?,Cat,General,Who was elected prime minister of Australia in 1991,Paul keating +General,Who was the sun god,Ra,Music,What Was The First Broadway Musical,The Black Crook In 1866,General,Samuel de Champlain founded which city,Quebec +Geography,What is the capital of Liberia,Monrovia,General,What is the Capital of: Serbia and Montenegro,Belgrade,General,The worst circus fire was in the USA in what year,1944 +General,In 'star wars' david prowse was darth vader's ______,Body,General,"On who's show would yoU.S.ee the german character, keeglefarven",Red buttons,General,In literature who is the alter ego of Percy Blakney,Scarlet Pimpernel +General,Quebec and newfoundland are the only two canadian provinces which do not allow ______,Personalised licence plates,Music,What Was The Title Of Mousse T And Tom Jones Hit Of 2000?,Sex Bomb,Science & Nature,What is the second derivative of distance ?,Acceleration +General,A typical bed usually houses over how many dust mites,6 billion,General,What Italian city had the Roman name Mediolanum?,Milan,General,Equator's favourite dish is Seco de Chivo - what is it,Goat stew on rice +General,What fruit is the basis for guacamole,Avocado,General,Sarah Jane Fulks - Anne Francis Bobbins both married who,Ronald Reagan J Wyman N Davis,General,Regnat Populus - The people rule - motto of what US state,Arkansas +General,It costs $30 a day in Austria to do this nude in winter - what,Cross country skiing,General,What is dennis the menace's surname,Mitchell,Geography,What is the capital of Nepal,Kathmandu +General,Chokan Moyogi Shakan Han Kengai and Kengai styles of what,Bonsai - styles, Language,What does the word 'karate' translate to in English?,Open hand,General,What microscopic animals name comes from Greek little Staff,Bacteria +General,"What organization helped defend earth in ""ultra man""",Science patrol,General,Between 1916 and 23 Clarence Saunders opened 2800 what,Piggly Wiggly – self service stores,General,A man's beard grows fastest when he ______,Anticipates sex +General,What makes Kirminski church in Finland unique,World biggest wooden church,General,Who is Warner Brothers oldest cartoon character,Porky Pig,Science & Nature,There Are 5 Elements In The Periodic Table That Conttain 4 Letters In Their Name Iron & Lead Are 2 Of Them 1 Point Each For Any Of The Other 3 ,"Gold, Neon, Zinc " + Geography,In which state are the Finger Lakes?,New York,Science & Nature,This animal is armed with bony plates and rolls up into a ball if frightened.,Armadillo,Music,"In 1998 ""Q"" Magazine Compiled A List Of The 100 Richest Rock Stars In Britain Name 3 Of The Top Five","Paul McCartney, Elton John, Mick Jagger, Phil Collins, David Bowie" +Sports & Leisure,How many hurdles must a runner jump over in the 110m men's hurdles race? ,10 ,General,What is the fear of property known as,Orthophobia,Science & Nature,Ethylene glycol is frequently used in automobiles.. How?,Anti-freeze +General,What alkaloid is derived principally from the bark of the cinchona tree,Quinine,Entertainment,This is a classic film about a huge gorilla.,King Kong,General,How many cards are there in each suit of a standard deck,Thirteen +General,A large patterned handkerchief,Bandanna,Food & Drink,Which region of France does claret come from? ,Bordeaux ,General,What was the name of the magazine on Suddenly Susan?,The Gate +General,"In What Year Did Russian Computer Programmer ""Alexi Pachitnov"" Design & Program The Video Game Tetris",1985, History & Holidays,What was the name of David Hasselhoff's talking car in Knight Ridder? ,Kitt , Geography,In which city is the Bridge of Sighs?,Venice +General,What is the fifth month of the year,May,General,What are non-precious metals called,Base metals,General,What ruined Cesar Borgia's honeymoon night,Mate gave him Laxatives +Sports & Leisure,"Which Sport Event Combines Riding, Revolver, Shooting, Fencing, Swimming & Running? ",The Pentathlon ,Religion & Mythology,Who founded Mormonism?,Joseph Smith,General,What is a group of this animal called: Snipe,Walk wisp +Sports & Leisure,The Gates To The Entrance Of Wimbledon Are Named After Which Player? ,Fred Perry ,General,What is thought to be the oldest English Cheese,Cheshire,Music,"""David Howell Evans"" Is Which Guitarists Real Name",The Edge + History & Holidays,"Which 1776 American Affirmation Asserted The Basic Rights Of All To (Life, Liberty & The Pursuit Of Happiness) ",American Declaration Of Independence ,Science & Nature,What forms when a diamond is cut with a laser?,Graphite dust,General,What was the name of Vanessa's last boyfriend on The Cosby Show?,Dabnes +Science & Nature," Flamingos are not naturally pink. They get their color from their food, tiny green algae that turn pink during __________",Digestion, History & Holidays,Which legendary American fire fighter was sent to extinguish the oilfields of Kuwait in 1991? ,Red Adair ,Geography,In Which City Might You Travel By Gondola ,Venice  +Sports & Leisure,In A Pefect 147 Snooker Break How Many Points Would Have Been Scored By Potting The Black ,112 ,General,In what game would you nurdle scrunge or carnovsky,Tiddlywinks , History & Holidays,What did David Stirling found?,SAS +General,What comes in varieties freestone and clingstone,Peaches, History & Holidays,"If someone gave you a frumenty at Christmas would you eat it, drink it or wear it ",Eat It (A spiced porridge) ,General,Yvon Petra was the last man to do what at Wimbledon,Wear flannels +General,A scholar who studies the Marquis de Sade is called a what,Sadian, History & Holidays,John McClane was fighting terrorists in a skyrise in which eighties movie? ,Die Hard ,General,What is the Capital of: Canada,Ottawa +General,With what did dricketer mansoor ali khan pataudi play with for a long time,Glass eye,General,What is a male ass,Jack,General,"In Italy what is a ""Zuppa Inglese""",Desert +General,You have head of ebay.com what's ebay translate to in Russian,Fuck off – imperative of word fuck,General,What's the best possible poker hand,Royal flush,General,What is fumet,Concentrated stock +General,Saudi Arabia law women get divorce if husband don’t give her it,What? - Coffee, History & Holidays,Who won the 1968 oregon democratic primary ,Eugene mccarthy ,General,What was the world’s first high level programming language 1957,IBM FORTRAN +General,Who wrote Sparkling Cyanide and Death on the Nile,Agatha christie,General,How many continents must a sport be regularly played in before it is accepted into the olympics,Five,General,Which biscuit is names after an Italian revolutionary leader,Garibaldi +General, This is the fear of enclosed spaces.,Claustrophobia,General,Which breed of dog was popularised from the name of a character in the Walter Scott novel 'Guy Mannering'?,Dandie Dinmont,General,The maiden names of which two cartoon characters are slaghoople and mcbricker,Wilma flintstone and betty rubble +General,What is a group of geese flying,Skein,General,Dianne Fossy the naturalist is famed working with what animals,Gorillas,General,In which country was Chopin born,Poland +General,"According to Espen Lind, who cries when 'she cries a rain storm, she cries a river, she cries a hole in the ground'",Susannah,General,What do opposite charges do,Attract,General,Who ordered the killing of the last incan king of peru,Francisco pizarro +General,Jobs from names - what did a Wayne do,Wagon Maker,Geography,"Ruby Falls, America's highest underground waterfall open to the public, is located on historic Lookout Mountain in Chattanooga, _______________",Tennessee,Food & Drink,"Duke Richelieu brought it to France after visiting Mahon, city on Minorca.",Mayonnaise +General,Which is the largest of the Egyptian Pyramids ?,Pyramid of Cheops,General,"What two characters from Sesame Street got their names from the movie ""It's a Wonderful Life""",Bert and ernie,General,Which game uses the largest ball,Earthball +General,"In agriculture, what is ground being 'rested' for a season called?",Fallow,General,What is a spoodle,Kitchen instrument spoon ladle cross,General,What was the Spice Girls' first single after the departure of Geri Halliwell,Viva forever + Geography,What is the capital of Czech Republic ?,Prague,Geography,Which Continent Did Captain Cook Discover In 1768 ,Australia ,Food & Drink,The lack of what vitamin causes beriberi (numbness in the hands and feet)?,B1 +General,Who makes the impulse se camera,Polaroid,Music,"Who Had A 1990 Hit With ""Tell Me There's A Heaven""?",Chris Rea,General,What is the common name for Carbolic Acid,Phenol +General,What term is applied to the natural process by which molecules will disperse evenly throughout a particular substance,Diffusion,General,Who wrote The Hunt for Red October,Tom clancy,General,What does the acronym CIA stand for?,Central Intelligence Agency +General,Where is the Salvador Dali museum located,St Petersburg Florida,General,In Peter Pan what were the names of Wendy's brothers,Michael John,General,Which country has a plain green flag,Libya + History & Holidays,What was The Eldest Son Of Edward III Better Known As? ,The Black Prince ,General,What's the connection between the Carol Burnett Show and Mama's Family,Eunice and Mama,General,What items name translates as distant voice,Telephone +General,Where is milk the most popular beverage,North america,General,In Islam what is the third piller of wisdom - there's 5 in total,Charity - 2.5 % of income,General,What drug is obtained from the poppy plant,Opium +Music,Who was lead guitarist with Mountain?,Leslie West,General,What is the official language of india,Hindi,General,A Hodophile gets sexually aroused by what,Travelling +General,John Simon Richie became famous as who,Sid Vicious,General,Who was disqualified during the 1908 Olympic Marathon,Dorando pietri,General,What is the common name for the astyeroidea,Starfish +General,"When used to describe a camera, what does SLR stand for",Single lens reflex,General,How many horses are there on a polo team,Four,General,What arts/literary movement founded by Tristan Tzara in 1915,Dadaism +General,Kevlar Is Tradionaly Used In The Manufacture Of Which Item Of Attire,Bullet Proof Vest,General,Whose tomb was discovered by Howard Carter in 1922,Tutankhamen,General,"In The TV Show ""Jamie And The Magic Torch"" What Was The Name Of Jamies Dog",Wordsworth +General,Which film won the best sound effects Oscar in 1990,Hunt for Red October, History & Holidays,Which famous horror film featured a killer wearing a mask which was actually a Captain Kirk mask painted white ,Halloween ,General,Snakephobia is the fear of,Snakes +Science & Nature,The Kelvin scale is used to measure _________.,Temperature,General,"In Shakespeare's Macbeth, which wood appears to move",Birnham wood,General,The King Cobra is the only snake that does what,Builds a Nest +Science & Nature,Of Which Plant Family Is The Leek A Member? ,The Lily ,Art & Literature,What DoYou Call A Picture That Is Made Of Various Materials Stuck Together ,A Collage ,General,In 1978 which punk rocker commited a serious crime in Manhattan,Sid vicious +General,Who was john lennon married to,Yoko ono,Science & Nature,Which planet was discovered in 1930 ?,Pluto,General,Lake Tiberius is better known by what name,Sea of Galilee +General,"In which film did Marlene Dietrich sing ""See What the Boys in the Backroom Will Have""",Destry rides again,Mathematics,What geometric shape has 4 equal sides?,Square,General,What is 42% carbohydrates 5% protein and 53% fat,Chocolate +Science & Nature,Which Detective Tried To Find Out Who Framed Roger Rabbit ,"Eddie Valiant, (Bob Hoskins) ",General,Cyprus lies in which sea,Mediterranean,General,"What does ""zucchero"" mean in italian?",Sugar +General,What geographical point did Robert Peary claim to have reached,North pole,General,What type of craft is the u.s's airforce one,Boeing 747, Geography,What is the basic unit of currency for Ivory Coast ?,Franc +General,Who was born in Chicago 5th December 1901 died 1966,Walt Disney,General,"Which City Has More Rolls Royce Cars ""Per Capita"" Than Any Other",Tokyo / Japan,Music,YWhich Former US President Does Dream Academy 's Life In A Northern Town Mention,John F Kennedy +General,Naturally we got to find a place called Fuka - Where,Zaire,General,Who recorded the song 'Rocket Man',Elton john,General,What world championship is the 'bermuda bowl',Bridge +Music,"How Many Chart Entries Did The Jam Have Between March & August 1980 (2,4,6 Or 8)",8,General,What was first work of fiction blessed by the Pope,Ben Hur – Lew Wallace,General,Ceres was the roman goddess of ______,Grain +General,Into which bay does the golden gate strait lead,San francisco bay,General,What is a group of hyenas,Cackle, History & Holidays,Who Founded The Open University? ,Harold Wilson  +General,Agriculture incorporating the cultivation and conservation of trees,Agroforestry,General,Which US states name means meadowland,Kentucky, History & Holidays,Which US state in 1907 was the last to declare Christmas a legal holiday? ,Oklahoma  + History & Holidays,""" What Letter Comes Next In The Following Sequence """"B, C, C, D, D, D, P, R"""" 'Blitzen, Comet, Cupid, Dancer, Dasher, Donner, Prancer, Rudolph, Vixen'' "" ",V= Santa's Reindeers in Alphabetical Order ,General,Where would you find the stuffing box and sucking rod,Oil Well Pump,General,What is the name of the spaceship in the film Alien?,Nostromo +General,In which Charles Dickens novel does Herbert Pocket appear,Great expectations,Food & Drink,What is the main flavour in the following alcohols 'Cointreau' ? ,Orange , History & Holidays,What ocean was amelia earhart flying over when she disappeared ,The pacific  +General,The relative speed of the TRS-80 to the ENIAC is __:1,20,Sports & Leisure,Who Scored Six Of England's Seven Goals At The 1986 World Cup Football Finals? ,Gary Lineker ,General,What organ did Aristotle think the blood cooled,Brain +General,"Which Famous Actor Played The Role Of ""Blake Carrington"" In The Pilot Episode Of Dynasty",George Peppard,Science & Nature,Dermatitis affects the __________.,Skin,General,Who composed the opera 'Macbeth',Verdi +Art & Literature,Who Is Novelist Helen Fieldings Most Famous Character? ,Bridget Jones ,General,What is a scut,A short tail,General,What is the flower that stands for: criticism,Cucumber +Music,Longest chart life of any Beatles single: 19 weeks,19 Weeks / Hey Jude,General,In Brainard Minnesota every man must do what by law,Grow a Beard,General,Janine Deckers suicided 1985 had top 10 hit 1962 as who,The Singing Nun Sister Luc Gabrielle + Geography,What is the capital of Missouri?,Jefferson City,Science & Nature,What Is The SI Unit Of Energy ,Joule ,Food & Drink,In 1981 Which Producer Of The Bond Movies Received The Irving G Thalberg Memorial Award? ,Cubby Broccoli  +General,What Russian writer was deported from the Soviet Union in 1974,Alexander,General,What produces the natural gas which is New Zealand's contibution to the greenhouse effect,Sheep,General,What leader was cremated on the banks of the Ganges river on 1/31/1948,Mahatma gandhi +Music,"Who Played Franz List In Ken Russells Film ""Lisztomania""",Roger Daltry,General,"Dows, Grahams and Warres famous producers of what wine",Port,General,"In ""The Blues Brothers"", what does SCMODS stand for?",State County Municipal Offender Data Systems +General,Mathew Webb swam the channel - where did he drown,Niagara Falls, History & Holidays,In Which War Was Pork Chop Hill Fought Over? ,Korean ,General,The Great Barrier Reef is how many miles long,1250 +Music,Which Band Released The Song Breakfast In America,Supertramp,General,In which weight category did John Conteh fight for the world title,Light heavyweight,Science & Nature," The armor of the __________ is not as tough as it appears. It is very pliable, much like a human fingernail.",Armadillo +General,Dendrologists worship what,Trees,General,What is Montreal's subway known as,Metro,Music,"Who Sang The Opening Song In The Film ""The Italian Job""",Matt Monroe + Geography,What is the capital of Indonesia?,Jakarta,General,Whose business cards identified the holder as a furniture dealer,Al capone,General,What did Johnny Staccato do when he wasn't busy as a private eye,Jazz pianist +General,"In 'dawson's creek', who does joshua jackson play",Pacey witter,General,Who is daisy duck's boyfriend,Donald duck,General,"Which car manufacturer built a model called 'Oxford', first produced in 1913",Morris motors +Science & Nature,What Type Of Insect Is A Weevil ,A Beetle , History & Holidays,Who Released The 70's Album Entitled Electric Warrior ,T. Rex ,General,Rudolf Rasendil is the hero of what novel and film,The Prisoner of Zenda +General,What is the capital of michigan,Lansing,General,Name the Lieutenant Colonel who was first black US in space,Bluford,General,Pitcairn Airlines were the first to provide what in 1922,Air Sick Bags +General,Which organ secretes insulin?,Pancreas,General,The head of which organisation is known as The Black Pope,The Jesuits,General,You'll Never Walk Alone came from which 1945 musical show,Carousel +Music,Who Was Lead Singer With Van Halen But Went Solo In 1985,David Lee Roth,General,"By 70 years of age, an average person will have shed how many pounds of skin",105,General,"Most digital watches have lcd displays, what does lcd mean?",Liquid crystal display +General,What word is spelled out in Morse by - .. - dash dot dot dash,TIT,Science & Nature, The smell of a __________ can be detected by a human a mile away.,Skunk,Science & Nature,This animal's name is the same as that given to a high church official.,Cardinal +General,McDonalds and Burger King put what on their fries,Sugar to brown colour,General,What is the Capital of: Vietnam,Hanoi,General,Whats the birthstone for July?,Ruby +General,What is the most popular street name in the u.s,Park street,General,What sin have inhabitants of Dantes first circle of hell committed,Pride - they are crushed by stones,General,Dapsang is another name for which major mountain,K2 +General,What country owns corsica,France,General,In which film does Kim Bassinger play a hooker who looks like Veronica Lake,LA Confidential,Entertainment,Who sang 'Islands In The Stream' with Dolly Parton?,Kenny Rogers +Entertainment,Who played the title role in the 'Mad Max' series of films?,Mel Gibson,Entertainment,Where did Clark Kent attend college,Metropolis university,General,What flower produces blue flowers in acidic soil,Hydrangea +Science & Nature," The oyster is usually __________. It begins life as a male, then becomes a female, then changes back to being a male, then back to being female. It may go back and forth many times.",Ambisexual,General,A ballet movement in which the dancer repeatedly crosses his or her legs in the air.,Entrechat,General,In which town did F.W. Woolworth open his first store in 1879,Utica +General,Carlo Collodi created which famous children's character,Pinocchio,General,What are Zap Spirit Crazylegs and Chuckles,G I Joe figures,General,Sysyem used especially in tape recording to reduce hiss,Dolby +Music,"Which UK Instrumental Group Reached Number 5 With ""Sabre Dance""",Love Sculpture, History & Holidays,Who was Captain of the Titanic on her maiden voyage? ,Edward J. Smith ,Science & Nature,What Is Believed To Be The Most Stupid Bird? ,The Turkey  +General,In Merryville Missouri woman cannot by law wear what,Corsets - men have right to admire,General,The average human uses which muscles most,The eye eye,General,Names what portable object is the teleram t-3000,Computer +Music,Which Roberta Flack Album Track Became A Hit After An Association With Clint Eastwood,The First Time Ever I Saw Your Face,General,With what are frogs often confused,Toads, History & Holidays,Which slasher film series featured masked killer Michael Myers ,Halloween  +General,Traditionally Lamborghini Miura and Diablo are named for what,Fighting Bulls, Geography,What symbol is on the flag of Vietnam?,Star,General,"Original inhabitants of New Zealand, of Polynesian stock",Maori +General,Candlemaker soapmaker merged in Cincinnati 1837 making what,Procter and Gamble,General,"By the end of 1983, how many computers were in use in the world","Thirteen million 13,000,000",General,"What's the international radio code word for the letter ""T""",Tango +General,"What actor was famous for the line ""nanoo nanoo""?",Robin Williams,General,What Shakespeare play has Portia as the heroine,Merchant of venice,General,"A basic movement in the technique of Martha Graham, based on breath inhalation and exhalation.",Contraction +General,Dr. Feelgood' was which group's last album with Vince Neill,Motley Crue,General,By what name is Harry Angstrom known in the titles of John Updike's trilogy,Rabbit,General,Who created john blackthorne,James clavell +General,Which countries name translates as place with a great river,Paraguay,General,"We had joy, we had fun ..' what is the song title",Seasons in the Sun,General,What animal lives in a holt,Otter +General,What word starts and ends with und,Underground,People & Places,In Which Ocean Is The Island Group The Maldives ,Indian ,General,Which Batman TV villain leads the Molehill Mob?,Riddler +Music,"What Song Features The Lyric ""Ive Been Going Out With A Girl Her Name Is Julie""",Jilted John,Science & Nature,Which Bird Is Resident Of The American Southwest & Has Been Immortilized In A Cartoon Series ,Roadrunner ,General,The Model T Ford was produced from 1909 until 1927. Which revolutionary innovation was introduced in 1913,Mass production +General,"Unfriendly, cold and sexually unresponsive",Frigid,Music,"Which American Band Comprised Jeffrey Hyman , John Cummings, Douglas Colvin, & Tommy Erdelyi, Although All Were Known Professionaly By The Same Surname",The Ramones,General,Who is known in Argentina as The Filthy Satanic Whore,Madonna +General,How long is the longest tunnel,169 kms,General,What animal only blinks one eye at a time,Hampster,General,"On 'the roseanne show', who was d.j",David jacob +General,What is Harold Lloyd Jenkins' stage name?,Conway Twitty,General,Does Botany Bay lie to the north or south of Sydney,South,Science & Nature,What is the biggest criterion for prospective astronauts?,Eyesight +General,In Trenton NJ its specifically illegal to throw what in street,Bad Pickle,General,"In Greek mythology, who was minos",King of crete,General,"Which Comedians Single Entitled “ The Magic Roundabout” Was Banned By The BBC In 1975, Due To It's Sexual Content",Jasper Carrot +General,What is the flower that stands for: pure love,Single red pink,General,What is a war between parties of the same country,Civil war,General,What was the name of the principal in archie comics??,Weatherbee +Geography,In which state is Stone Mountain,Georgia, History & Holidays,In Victorian England what people were popularly called robins because of their red uniforms? ,Postmen , History & Holidays,Who did David Bowie duet with on the Christmas Hit 'Little Drummer Boy'' ,Bing Crosby  +General,Juan What type of animal is a wallaby,Kangaroo,Science & Nature,What Do Aardvarks Eat? ,Termites Or Ants ,General,In which film did Paul Newrnan eat 50 hard-boiled eggs,Cool hand luke +General,What short lived TV western did Rod Serling produce after Twilight Zone,Loner,General, The effect produced when sound is reflected back is known as a(n) _______.,Echo,General,Sextilis was the original name for what,August +Music,John Lennon Once Caused Outrage When He Said The Beatles Were More Popular Than Who?,Jesus,Sports & Leisure,What sport has a hooker in a scrum,Rugby,General,What Is Elton Johns Middle Name,Hercules + History & Holidays,Who was King Of England During The Battle Of Agincourt? ,Henry V ,Music,"With Which Boyband Did Mariah Carey Cover The Phil Collins Hit ""Against All Odds""",Westlife,Music,Before Judging On American Idol Which US Singer Had A Hit With The Song Opposites Attract,Paula Abdul +General,International car registration letters what country is IS,Iceland,General,Which is the largest planet in our solar system,Jupiter,Science & Nature," You can tell the sex of a horse by its teeth. Most males have 40, females have __________",36 +General,A company called Symbol owns patent to what common item,Bar Code,General,"Who wrote the 1994 biography ""Princess in Love""",Anna Pasternak,General,What poet won the pulitzer prize 4 times,Robert frost +General,The word cruise comes from which language,Dutch Kruisen,Geography,On what peninsula are Spain and Portugal located,The iberia n peninsula,Music,"What Is Pictured On The Album Cover ""Band On The Run""",A Group Of Convicts Caught In A Spotlight +General,What is a group of this animal called: Gnat,Cloud horde,Sports & Leisure,Where were the 1964 Olympics held ?,"Tokyo, Japan",General,What's Krypton's state at standard temperature & pressure,Gaseous +Music,Which Trio Wrote The Diana Ross Hit Chain Reaction,The Bee Gees,General,What is a Chuckwalla,Lizard,Music,His Incomplete 8th Symphony Is World Famous As The Unfinished Name This Prolific Austrian Composer,Franz Schuberts +Geography,The tenge is the basic monetary unit of which country? ,Kazakhstan (1 tenge = 100 teims),General,What group founded Brother Records,Beach boys,General,Calabrese is a form of which vegetable,Broccoli + Geography,What is the capital of Burma?,Rangoon,Food & Drink,What Drink Has The Words 'Quality Tennessee Sour Mash'' Written On The Label ,Jack Daniels ,General,"In 1976 Howard Hughes reclusive billionaire, dies at",72 +General,Who wrote The Magician's Nephew,C s lewis,General,Uranus' moons are all named after,Shakespearean women,General,On 21st July Neil Armstrong Became The 1st Man To Walk On The Moon But What Song Was A Uk No.1 At The Time,Thundercalap Newman / Something In The Air +General,What is the birthstone for August?,Peridot,General,A __________________ measures blood pressure,Sphygmomanometer,General,In Seattle women can get six months for doing what to men,Sit on lap bus/train without pillow +General,Scapegoat meaning blame taker comes from what religion,Jewish Yom Kippur,Art & Literature,His many Romantic odes include Ode to Melancholy and Ode to a Graecian Urn?,Keats,General,"What sport is associated with the clubs of Essendon, Hawthorn and Collingwood",Australian rules football +General,By Law in North Carolina you can't do what,Sing out of Tune,General,A Russian person famous as a horseman,Cossack,Art & Literature,Which Famous Book Contains The Line 'It is a truth universally acknowledged that a single man in possession of a good fortune must be in want of a wife' ,Pride & Prejudice  +General,Jake Burns was associated with which British punk band?,Stiff Little Fingers,General,What is the name given to a solid figure with 12 plane faces,Dodecahedron,General,What was the name of the bartender in the tv series 'cheers',Sam malone +General,What are catalogued under the Dewey decimal system?,Books,General,Where is the largest church in the world,Vatican in Rome,General,Who starred in 1950s Circus Boy then moved on to pop music,Mickey Dolenz +General,Clothes designer John Galliano works for which fashion house,Christian dior,General,Which creature taught Dr. Doolittle how to talk to the animals,His parrot,General,In any one month 36% of Americans eat what for breakfast,Cold left over pizza +General,Which playing card is known as The Devils Bedpost,Four of Clubs,General,What are or were Wix Fibs and Fax,Brands of Tampons,General,"On a monopoly board, what lies between st charles place and states ave",Electric company +General,What drink was named after 5th child Henry 8 Catherine of Aragon,Bloody Mary,General,What do Hansel & Gretel push the witch into,The oven,General,What's the most mountainous country in europe,Switzerland +General,What pigment is responsible for the red color in leaves,Anthocyanins,General,How do you Rizzle something,Sun dry,General,When are you most likely to see a penumbra,During an Eclipse +Music,"What was the original working title of the movie ""Help""?",Eight Arms To Hold You,Science & Nature, Cougars can kill animals __________ times their size,8,General,The term Quadricentennial represents how many years ?,400 +Music,"Which Saxophonist Was Born Kenneth Gorlick On June 6, 1956",Kenny G,General,What is the sacred animal of india,Cow,General,In what capacity did Ernest Hemingway take part in the first World War,Ambulance driver +General,What was the first product sold in aerosol sprays,Insecticides,General,What is the maximum length permitted for the blade of a goalies hockey stick,Fifteen and a half inches 15 and a half inches,General,Which World Champion heavyweight boxer held the title for the longest,Joe louis +Geography,"Japan consists of the four large islands of Hokkaido, __________, Shikoku, Kyushu, and about three thousand smaller islands.",Honshu,Music,Which Irish Band Is Led By Dolores O Riordan?,The Cranberries,General,The Sea Cook was the original title of what famous novel,Treasure Island + History & Holidays,What was the second carry on film? ,Carry on Nurse ,General,Logizomechanophobia is the fear of,Computers,General,What is a horse's foot called,Hoof +General,What ship sank off ireland in 1915,The lusitania,Music,"Which Groups Had Hits With ""At The Hop"" & ""Rock & Roll Is Here To Stay""",Danny & the Juniors,General,If you has caries who would you consult,Dentist - its tooth decay +General,What is a regurgitation of acid from the stomach into the aesophagus,Heartburn,Sports & Leisure,In Which Sport Might You See A Hiplock Or Flying Mare? ,Wrestling ,General,Where did you find Shadow Speedy Bashful Pokey,Pac man Blinky Pinky Inky Clyde + Geography,What is the capital of Senegal?,Dakar,General,A thick revolving cylinder for winding cables,Capstan,General,What is the flower that stands for: sincerity,Fern + History & Holidays,What was the war during Regan's first term that took place on an island in the carribean? ,Grenada ,General,In which country were pizzas made first?,Italy,Music,Which Label Shares It's Name With A Famous Hollywood Tower,Capitol +General,Who is the norse god of mischief,Loki,Music,What was the only hit song for the band 'It's a Beautiful Day'?,White Bird,General,Its usual diameter is 4.5 inches what is it,Golf Hole +General,"Who said, ""People don't credit me with much of a brain, so why should I disillusion them.""?",Sylvester Stallone,General,What book of the old testament has the least chapters (1),Obadiah,General,What was the title of Mantovani's signature tune,Charmaine +General,The only domestic animal not mentioned in the Bible is what,Cat,General,What part of a human takes about 6 months to grow from base to tip,Nails,General,Collective nouns - A Chatter of what,Budgerigars +General,What African countries capitol is named after a US president , Liberia - Monrovia – James Monroe,General,What airport in Uganda was the scene of a rescue drama in 1977,Entebbe,General,In what American state do most fail to graduate,Georgia +Entertainment,What came out of Milton's head,Steam,Science & Nature,What Widely Used Kitchen Item Was First Marketed By The General Electric Company In 1909 ,The Electric Toaster ,General,The Devonian period is also known as the age of __________,Fish +General,In Which English Citty Was Cary Grant Born,Bristol,General,"What was the first name of dom perignon, the monk who gave us champagne",Pierre,General,What war was fought by the houses of York & Lancaster,War of the roses +General,From what is rum distilled,Sugar cane,General,Where did 'the mayflower' take the pilgrims,New world,Science & Nature,What Is Bit Short For ,Binary Digit  + History & Holidays,What father/daughter duo made 'Gag Me With a Spoon' a household phrase during the eighties? ,Frank & Moon Unit Zappa ,General,In Glendale Arizona it is illegal for a car to do what,Reverse - Back up,Music,By What Name Are Chicago Transit Authority Better Known,Chicago +General,1 in 20 women say they have never touched what,Shovel, History & Holidays,How many points does a snowflake have? ,Six ,Science & Nature,What pistol took its name from the person who patented it in 1835 ,Colt  +General,"According to the King James version of the twenty-third psalm, 'yea, though i walk through the valley of the shadow of death, i shall fear no ___________'",Evil,General,What is the fear of food or eating known as,Sitophobia, Geography,Which ocean has an area of approximately 166 sq. km?,Pacific Ocean +Science & Nature,In Computing What Does HTTP Stand For ,Hyper Text Transfer Protocal ,General,Rhodopsis original Egyptian Cinderella had what job,Prostitute - bird stole her shoe,General,The making of what well known dessert item was perfected by Sicilian Francisco Procopio in 1659,Ice cream +General,What is a group of bitterns,Sedge,General,Artist - The Monarch of the Glen in 1850 - Lions Trafalgar square,Sir Edwin Henry Landseer,Geography,In what country is banff national park ,Canada  +Science & Nature,"Which British Car Was Driven By Paddy Hopkirk To Win The 1964 Monte Carlo Rally , And Also Featured In The Film The Italian Job ",Mini Cooper ,General,What's the only property an orthodox Hindu woman can own,Jewelry,General,What are the names of the Ninja Turtles in alphabetical order,Donatello + History & Holidays,Who captained the HMS Beagle?,Charles Darwin,General,"In Greek mythology, which titans forged thunderbolts for zeus",Cyclops,General,What is Frances longest river,Loire +Music,Which Rock Legends Did The Harlem Shuffle In The 1980's,The Rolling Stones,General,What three counties were Eliza Dolittle taught to pronounce,Hertford Hereford Hampshire,General,The computer language LISP means what,List Processing +General,Who directed 'the shining',Stanley kubrick,General,What is the worlds highest island mountain,Mauna kea,General,What word is derived from the arabic word al-kimia,Chemistry +General,"North American tribe, of the Iroquoian linguistic family and the Southeast culture area",Cherokee,General,Which country has no public toilets,Peru,Music,Mike Batt Was The Voice Behind Who,The Wombles +General,In Which Country Might The Jets Play The Mariners & The Glory Take On The Roar At Football,Australia,General,With which four words did Samuel Pepys end his diary each day,And so to bed,General,Which animals Latin name is Cricetus-cricutus,The Hamster +General,Name USA city it's illegal to have a nude shop dummy on display,New York,General,What country is the setting for The Thorn Birds,Australia,General,"Who murdered Leno & Rosemary Labianca on August 10, 1969",Manson family +General,In what key do american car horns beep,F,Geography,"Many cities in our country bear the names of other countries. The U.S. city of Mexico can be found in the states of Indiana, Maine, and ________________",Missouri,General,The chrysanthemum is the flower for what month,November +General,At German country weddings the couple had to do what together,Saw through a log,General,"What actor played trapper john, md",Pernell roberts,General,The stems and leaves of the tomato plant are,Poisonous +General,What Greek slave wrote fables?,Aesop,Science & Nature,Which scale is based on the speed of sound ?,Mach,General,What is the Mexican dish 'huevos rancheros' made from,Baked eggs +General,Who played Judge Dredd on film in 1996,Sylvester stallone,Art & Literature,An italian movement c.1909-1919. It attempted to integrate the dynamism of the machine age into art.,Futurism,Entertainment,The standard major scale is also known as the _______ mode.,Ionian +General,"What was the name of the old fireman on ""leave it to beaver",Gus,Music,"Which Band Had A Hit With The Song ""Dr Mabuse""",Propaganda,General,It was finally abolished in Britain in 1948 - what was,Flogging +General,"""7x"" was used to refer to the secret ingredient of what drink",Coca cola,General,An ounce of gold can be stretched into a wire how many miles long,Fifty 50,Food & Drink,"Which Childrens Tv Show Featured Segments Entitled 'Danger Island, Arabian Knights, Micro Ventures & The 3 Musketeers'' ",The Banana Splits  +General,What meat do Muslims not eat,Pork, History & Holidays,Halloween originated from a festival celebrated by what ancient European tribe is it A Druids B Huns or is it C Celts ,C = Celts ,General,The angles inside a square total _______ degrees,360 +General,"What links Millionaires, Metropolitans, Black Hawks, Silver seven",Stanley Cup winners Ice Hockey,General,What loses colour if kept in dimly lit or running water,Goldfish, History & Holidays,Who resigned as Russian Premier on Christmas day 1991 ,Mikhail Gorbachev  +General,In which year was the British Dog License abolished?,1987,General,Where is lake maracaibo,Venezuela,Music,In 2001 Which Group Recorded A Cover Version Of The Michael Jackson Hit Smooth Criminal?,Alien Ant Farm +Geography,Which Is The Largest State In Australia ,Western Australia ,Music,Which Former Cambridge Footlights Comedian Made A Mint Out Of Me And My Girl,Stephen Fry,General,Who won the first Nobel prize for Physics in 1901 - gave away,Wilhelm Roentgen +General,After who was the month of August named,Augustus Caesar,General,Plant of the lily family with edible shoots,Asparagus,Music,Father Mckenzie Features In The Lyrics To Which Beatles Song,Eleanor Rigby +General,"Which group released the album ""O.K. Computer"" in 1997",Radiohead,General,"In sailing, what is a warp",Rope,People & Places,Who Is The Sister Of Warren Beatty ? ,Shirley Mclaine  +General, The study of man and culture is known as ________.,Anthropology,General,Martina Hinges represents what country at tennis,Switzerland,General,What future yippie leader was the first male cheerleader at brandeis?,Abbie hoffman +General,Leonard Sly became famous as who,Roy Rodgers,General,What is the flower that stands for: devotion,Heliotrope,General,The French Laurousse Gastronomique 9 recopies cooking what,Camels +Science & Nature,Which British Pop Group Had A Hit With Brontosaurus ,The Move ,General,What is the most essential tool in astronomy,Telescope,General,In Virginia its against the law for people to bribe except who,Political Candidates +Science & Nature, Hens do not have to be impregnated to lay eggs. The __________ is necessary only to fertilize the egg.,Rooster,General,This was the site of worse nuclear accident in history,Chernobyl,General,Which Sport Features A Series Of Bouts Known As A Barrage,Fencing +General,What process involves the heating of milk to a temperature of about 60 degrees,Pasteurisation,General,In Italy where are Monterosa Ave Park of Victory Constantine Ave,On a Monopoly board,General,Beneath which Paris monument is the tomb of France's unknown soldier,Arc de triomphe +General,"What are Jean Bernard, Pierre St-Martin and Berger in France",Worlds deep caves,General,Time ____ when your having fun,Flies,Music,Whose Albums Include Better Living By Chemistry & You've Come A Long Way Baby,Fatboy Slim +General,In medieval France a persons rank was shown by the length of ?,Shoe points bigger=higher,General,Who played Bonnie in the film 'Bonnie & Clyde',Faye dunaway,General,Britannia female embodiment of Britain who is the French,Marianne +Geography,"___________ is the most densely populated non_island region in the world, with more than 1,970 humans per square mile.",Bangladesh,General,What country is the worlds oldest functioning democracy,Iceland, History & Holidays,"Who said ""Veni, vidi, vici"" (I came, I saw, I conquered)?",Julius Caesar +General,Seretse Khama was the first President of which country,Botswana,General,The name of which constellation me 'harp',Lyra,General,Rudy Stevens became famous under which name,Barbara Stanwyck +General,Link Achtung Adagio Bravo Butterfly Gong Polo Rondo Zebra,Typefaces or Fonts,General,Who was the Bad in the spaghetti westerns,Lee van Cleef,General,"According to the Beatles, where could you find 10,000 holes","Blackburn, lancashire" +General,What was the profession of Ted Bundy,Attorney,Sports & Leisure,What Material Is The Puck Made Out Of In A Game Of Ice Hockey ,Rubber ,Science & Nature,Which metal is used to Galvanise Steel? ,Zinc  +Geography,_____________ is the largest Western European country. Its area is slightly less than twice the size of Colorado.,France,General,"Which mythological creature was half man, half horse",Centaur,General,What colour is cinnabar,Red +Music,"On The England New Order Song ""World In Motion"" Which Member Of The England Squad Provided The Rap",John Barnes,General,What Type Of Foodstuff Is A Dunlop,Cheese,General,Which Egyptian prime minister nationalized the Suez Canal,Nasser + History & Holidays,Who Was Prime Minister When Edward VIII Abdicated? ,Stanley Baldwin ,General,Over what place in india is it forbidden to fly an airplane,Taj mahal,General,Which tree is sacred to Apollo (Daphne changed into one),Laurel +General,Thomas Magnum's dad was played by what actor?,Robert Pine,General,Where is the columella,Nose,General,What food did the Romans call Pointed Stick,Broccoli - from Brocca +General,Single celled micro-organism,Bacterium, History & Holidays,How many reindeer does Santa Have? ,8 / (9 With Rudolph) ,General,"In the song 'Skip To My Lou', in what beverage are the flies?",Buttermilk +General,What precious metal did alchemists call luna,Silver,General,Through which ocean does the international date line approximately follow the 180 degree meridian,Pacific ocean, History & Holidays,Who Released The 70's Album Entitled Horses ,Patti Smith  +Food & Drink,"According to some, 'Dutch courage' in battle came from the over consumption of what ? ",Gin (Genever is Dutch for juniper) ,Sports & Leisure,In Which Surrey Town Is The British National Shooting Centre ,Bisley ,General,What is the essential characteristic of aerobic exercise,Oxygen is consumed +General,What is black gold,Crude oil,General,"What snack food was portrayed in claymation dancing to ""Heard it Through the Grapevine""?",Raisins,Science & Nature,A Boomer Is The Male Of Which Animal? ,Kangaroo  + History & Holidays,The Christmas period of 1813-14 saw the last what in London? ,Christmas Fair on a frozen River Thames (known as a Frost Fair) ,General,Mandoura Greece Zampogna Italy Corenmuse France what is it,Bagpipes,General,Where do the italians host the grand prix,Monza +General,What gems name means sea water in Latin,Aquamarine,General,British soldiers mentioned in despatches get which bronze award,The Oak Leaf,General,What is sericulture,Growing Silkworms +General,In Sioux language the Paha-sapa is what place,Black Hills - Dakota,General,When was Clinton first elected president of the U.S.,1992,Art & Literature,"In 'A Christmas Carol', how many ghosts visited Scrooge?",Four +General,Which bird is sometimes referred to as a 'pigeon hawk'?,Merlin,Science & Nature,"What is the only animal, other than a human that can catch leprosy?",The Armadillo,General,What were the occupations of the three men in the tub,"Butcher, baker and" +General,"Vast region comprising the eastern part of the Asian portion of Russia, bounded on the west by the Ural mountains; on the north by the Arctic Ocean; on the east by the Pacific Ocean; & on the south by China, Mongolia, & Kazakstan",Siberia,Music,Which Heavy Metal Guitarist Provided The Soundtrack To The Movie Death Wish 2,Jimmy Page,General,"What is the common name of the family of plants which includes potatoes, peppers and tomatoes",Nightshades +General,Which motorway links Bradford with the M62?,M606,General,Where would you find the medulla oblongata,Brain,General,What was the first frozen food available in Britain in 1937,Asparagus +General,Whats the telephone area code for Chicago,312,General,Which cities name comes from Algonquin meaning traders,Ottawa,Music,Woody Left Madness To Join Which Group,Voice Of The Beehive +General,"A ""pigskin"" is another name for a(n) ________.",Football,General,Which islands way of life and culture is described as Bajun,Barbados,General,What worms will eat themselves if they can't find any food,Ribbon worms +General,Instrument with windbag for pumping air through reeded pipes,Bagpipes,General,A Queef is the name for what,Fanny Fart,General,What is the clavicle,Collarbone +General,"Who recorded the album ""the point"" in 1970",Harry nilsson,General,What was tokyo originally known as,Edo,General,The average American eats 5666 what in their lifetime,Fried Eggs +General,Where is noah's ark thought to have landed,Mount ararat,General,What word was created by merging the words 'melt and weld',Meld,General,Bruce Philip in 1985 recorded what sporting first,Rowed for Oxford and Cambridge +General,"What 80's group sang the theme song to ""Square Pegs"".",The Waitresses,Sports & Leisure,Which European Football Club Plays Its Home Matches At The Stadio Delle Alpi? ,Juventus ,General,Which English Mill Town Once Produced 99% Of The Worlds Cotton,Oldham +General,How many bends in a standard paperclip?,Three,Music,Who Was The Lead Singer Of The Plasmatics,Wendy O Williams,General,What is used to measure the depth of water,Bathometer +General,What is a figure with eight equal sides called,Octagon,General,What is the term for unlimited authority,Carte blanche,General,On the border between Brazil and which other South American country would you find the Itaipu Dam,Paraguay +Music,Which Legend Spent More Weeks In The Charts Than Any Other Artist In Both 1970 & 1971,Elvis Presley,General,What is the correct name for an animal's pouch,Marsupium,General,What is a group of bass,Shoal +General,In which sport are the trainees traditionally bricklayers,Bullfighting,Music,Which American singer sang “ Thank God I’m a Country Boy”?,John Denver,General,The bridge connecting boston and cambridge via massachusetts avenue is commonly know as the ______,Harvard bridge +General,What nationality was Goya,Spanish,General,Dr George Wander invented what drink in Switzerland 1860s,Ovaltine,General,Who composed The Karelia Suite,Sibelius +General,What is the study of the cosmos called,Cosmology,General,At Which Olympic Games Was The Modern Olympic Flag Introduced,Antwep 1920,Music,"Who Is The Oldest Of These A) Brett Anderson, B) Keith Flint, C) Robbie Williams",Brett Anderson +General,"Who Played The Psychotic ""Norman Bates"" In The 1998 Remake Of The Classic Movie ""Psycho""",Vince Vaughan, History & Holidays,In What Year Did The Spanish Civil War Begin ,1936 ,General,Drakes Golden Hind was originally called what,The Pelican +General,"What father/daughter duo made ""Gag Me With a Spoon"" a household phrase during the eighties?",Frank & Moon Unit Zappa,General,Which Prime Minister introduced Income Tax,Pitt the younger,General,What is a triangle with three equal sides called,Equilateral +Entertainment,Benny and Cecil were at odds with whom?,John,General,In 1975 Nationalist Chinese leader Chiang Kai-Shek died at the age of,87,Science & Nature,"This disease consists of a purposeless, continual growth of white blood cells.",Leukemia +General,"Empire Strikes Back' when the ghost of Obi Wan Kenobi said that Luke was their last hope against the Empire, who was Yoda refering to when he said: ""No, there is another""?",Princess Leia,General,"What science is celebrated on mole day, every october 23rd",Chemistry,General,In what Australian state would you find Newcastle,New south wales nsw +General,Dover is the capital of ______,Delaware,General,Which Shakespeare play contains the line 'if music be the food of love play on' ,Twelth Night ,General,"Who said ""Forgive your enemies, but never forget their names""?",John F. Kennedy +Music,Which Beatle`s album was the bestselling album on the 1960s in the UK?,Sgt. Pepper`s Lonely Hearts Club Band,People & Places,Who Replaced Betty Boothroyd As Speaker Of The House Of Commons ,Michael Martin ,General,Hyundai and which other car manufacturer are located in Korea,Kia +General,The chemical formula for rubidium bromide is rbbr. It is the only chemical formula known to be a what,Palindrome,General,"The French TGV train is called Traine Griende Viesta, what does that mean in english?",Train With Great Speed,Science & Nature,What is the term for a mass of mood moving from your mouth to your stomach called?,Bolus +General,Who sang with 'the dakotas',Billy j kramer,General,Who succeeded Queen Elizabeth l to the throne in 1603,James i,Food & Drink,What name is given to a cockerel that is castrated and fattened for the table? ,Capon  +General,What is the fear of worms known as,Scoleciphobia,General,James Hoban designed what,The White House,General,When did the first World Series game take place,1903 +General,How tall are the bearskins worn by the guards at buckingham palace,20,General,Which seven letter word in English contains all five vowels,Sequoia,Music,Which Indian Instrument Has Nineteen Strings?,Sitar + History & Holidays,How old was John F. Kennedy when he became president,43,General,Which Pop Singer Was Born “ Leslie Sebastian Charles ” On 21 st Jan 1950,Billy Ocean,Music,Whose Life Story Featured In The Movie Amadeus,Mozart +General,On average 100 people a year choke to death on what,Ballpoint Pens,General,What pop group took their name from a Herman Hess novel,Steppenwolff,General,What country finally adopted the Gregorian calendar in 1752,England +General,Homichlophobia is the fear of,Fog,General,Toxophily Is More Commonly Known As What?,Archery,Sports & Leisure,Who Won The 2008 Lakeside World Darts Championships ,Mark Webster  +General,Which Russian choreographer (1904-1983) moved to the USA and became a founder member of the School of American ballet,Balachine,General,C2 H5 OH is the formula of what,Alcohol,General,In which country were antibiotics first used,Egypt - used mouldy bread +General,What Italian word means swank,Mafia,General,Where Exactly (And You Must Be Specific) Will You Find The Words “Standing On The Shoulders Of Giants”,On The Edge Of A £2 Coin, Geography,Brussels is the capital of which country?,Belgium +General,Who was The Little Playful One,Pocahontas,Music,How Many Consecutive No.1's Did The Beatles Have Between 1963 & 1966,11,Religion & Mythology,What is God called in the Muslim faith?,Allah +Food & Drink,The Term 'Dutch Courage' Usually Comes From The Consumption Of Which Spirit? ,Gin ,General,What comic strip is set at Camp Swampy?,Beetle Bailey,Geography,On What Continent Is The Kalahari Desert ,Africa  +General,Yet Another Hierarchical Officious Oracle better known as what,YAHOO,General,What are FAQs,Frequently asked questions,General,From who did malta gain independence in 1964,Britain +General,What martial art's name means 'leisure time',Kung fu,General,In Florida it is illegal for a single woman to do what on Sunday,Skydive,General,Simon Peter and his brother were both disciples. What was the brother's name,Andrew +General,Who designed Madonna's famous pointed corset,Jean paul gaultier,General,What university was founded in 1160??,Oxford University,General,Which organ in the body is inflamed in nephritis,Kidney +General,"To 10 Years Either Way, In What Year Did Sir Francis Drake Begin His Voyage Around The World ?",1577,Sports & Leisure,Which Sporting Event Of 1985 Attracted 18.6 Million Viewers In The Early Hours Of The Morning? ,Embassy World Snooker (Taylor & Davis) ,Science & Nature,What Organ In The Human Body Produces Insulin? ,The Pancreas  +General,A person who eats only fruit,Fruitarian,General,What was James Bonds fathers name,Andrew,Technology & Video Games,"Which was the first console ""Duke Nukem' game? ",Duke Nukem 64 +Sports & Leisure,Name A Sporting Team Even In Which The Winning Team Only Move Backwards ,Rowing / Tug O War ,General,What is the flower that stands for: bashful shame,Deep red rose,Music,"Conga, Bongo's & Tabla Are All Forms Of Which Instrument",Drums +General,What is used in a tempera painting,Egg yoke and water,General,In Heraldry if things are accosted what position are they in,Side by Side,General,A rich deep red colour,Crimson +General,Lagnoperissia is a fancy name for what sexual condition,Nymphomania,General,Which of the Old Testarnent prophets was taken up to Heaven in a fiery chariot,Elijah,General,Philadelphia cream cheese was introduced in what city,New York +General,Alice Dormouse Mad Hatter who is missing from the Tea Party,March Hare,Science & Nature,"On the Periodic Table, what is the first element in alphabetical order?",Actinium,General,"The rover, ""Sojourner"", explored the surface of what place",Mars +General,Which Guitarist was in the original line up of Thin Lizzie,Eric bell,Sports & Leisure,"Did The Game Of Polo Originate In India, Persia, Or Argentina ",Persia (Iran) ,General,What do the letters 'T' and 'S' stand for in T. S. Eliot's name,Thomas stearns +General,"Which entertainers feet were insured for $650,000",Fred astaire,General,Which playwright and dissident became President of Czechoslovakia in 1989,Vaclav havel,General,Who committed the first daytime robbery,Frank and jesse james +General,Which Male Athelete Ran The Fastest 100 Metres Of The 20th Century,Maurice Greene,General,In cookery how is something julienne prepared,Thin Strips,General,An “Anoctothorpe” Is Another Name For What Everyday Monetary Item?,"The Pound Sign ""£""" +General,"What's the nickname for Paris, France",City of light,Science & Nature,Which Is Generally Considered To Be The Most Intelligent Breed Of Dog? ,The Border Collie , History & Holidays,In Which Country Did The Tradition Of Trick Or Treating Actually Originate ,England  +General,For what metal is 'au' the chemical symbol,Gold,General,What vegetables are sometimes called 'spuds',Potatoes,General,In what class of animals is a Herpetologist interested,Reptiles +General,In what sport did Jeffery Archer win an Oxford Blue (USA Letter),Athletics,General,What is best viewed from two tourist viewpoints - the North Rim and the South Rim,Grand canyon,General,"""Nuuk"" Is The Capital City Of Which European Country",Greenland +General,"There were no squirrels on Nantucket Island, Massachusetts until what year",1989,General,Who is the only person to win oscars for best actress and best song?,Barbara Streisand,General,What did Lippershey invent in 1608 that Galileo often gets the credit for,Refracting telescope +General,Hercules had to clean the stables in one night - whose,Aegean Stables,General,What is the word Taxi short for,Taximeter,General,In which book would you find the manservant Pas Partout,Around the world in 80 Days +General,Who was the first U.S. president to have been divorced,Ronald reagan,Music,How Much To Within £5 Was A Weekend Ticket For The Reading 98 Concert,£75,General,What is the real name of the 'man of arms' in 'He man and the Masters of the Universe',Duncan +General,What is having a hole drilled through the cranium supposedly enabling people to reach a higher state of consciousness,Trepanning,General,"In Greek mythology, what were the fifty daughters of nerius and doris called",Nereids,General,In Kansas City its illegal to what with more than 12 potatoes,Juggle with them +General,What animal has no vocal chords,Giraffe,Science & Nature,What Symptom Does The Affliction Tinnitus Cause ,Ringing In The Ears , Geography,What is the capital of Kuwait?,Kuwait +General,The construction of which famous New York City building was completed in 1931,The empire state,General,"Which English poet wrote 'No man is an island, entire of itself.'",John donne,Religion & Mythology,Who is the Norse god of mischief?,Loki +General,What is the name of the asteroid that was believed to have killed the dinosaurs,Chixalub,Music,What 1987 Movie Theme Is The Only No.1 Ever To Be Sung Entirely In Spanish,La Bamba,Music,By What Name Do We Know & Love George O Dowd,Boy George +General,"Acute infectious disease of the upper respiratory tract, caused by more than 100 kinds of viruses",Common cold,General,Before 1938 toothbrushes were made using hairs from what,Beaver,General,In which city was the first public opera house opened,Venice +General,Collective nouns - a Dule of what,Doves,General,"In which city would you find Tuff Gong International Studios, built by the late Bob Marley",Kingston,General,Which literary traveller was accompanied by the dog Toto,Dorothy +General,What year did 'Papa Doc' president of Haiti die,1967,General,Who is swee'pea's adopted father,Popeye,Science & Nature,Which Newspaper Had It's Own Office On The 2nd Floor Of The Eiffel Tower ,Le Figaro  +Geography,In what city are the famous Tivoli Gardens,Copenhagen,General,Who composed the Air for the G string (init and name),JS Bach,General,Frontenac what university dismissed timothy leary for involvement with drugs,Harvard +Science & Nature,What Is The Only Even Prime Number ,2 ,General,What day of the week did Solomon Grundy die,Saturday,Food & Drink,From Which Birds Nest Is Birds Nest Soup Made ,The Swift  +General,"In 1984, a canadian farmer began renting advertising space on his what",Cow,General,Who was the title star of Meet the Veep,Alben w barkley,General,What is another name for a tightrope walker?,Funambulist +General,"A meander bend in a river, named from river meander - where",Turkey,Entertainment,"In 1981, who won song of the year with 'Sailing'?",Christopher Cross,General,What country is the largest per capita consumer of beer,Germany +General,Who was Prime Minister of France at the outbreak of World War I I,Edouard daladier,Music,What Is Johnny Rottens Real Name,John Lydon,Science & Nature, A carnivore is a meat_eating animal. A __________ is a fruit_eating animal.,Frugivore +General,What tanker caused a severe oil spill in 1989,Exxon Valdez (Alaska),Entertainment,Who was the original voice of Mickey Mouse?,Walt Disney,General,The first nuclear chain reaction in 1942 by Enrico Fermi & Arthur Compton was achieved in what U.S. city,Chicago +Music,In The Everly Brothers Song  “Wake Up Little Suzie” What time do they wake up (when the movie’s over)?,4 O Clock,General,The study of light and its relation to sight is called ______.,Optics,General,Who was the Confederate commander at Chickamauga,Braxton bragg +General,Nebulaphobia is the fear of,Fog,General,What does Monaco get most of its income from,Gambling Casinos etc,General,What is the study of prehistoric plants and animals,Paleontology +People & Places,"Whose Children Are Trixiebell, Little pixie, & Peaches ",Bob Geldof & Paula Yates ,General,What country did the operating system 'linux' come from,Finland,Music,In Which US City was the first US live Beatles Concert?,Washington D.C. +General,In what key is the dialtone of a telephone,F,Entertainment,Who directed the Movie 'Psycho' from Robert Bloch?,Alfred Hitchcock,Music,Monty Pythons Flying Circus Proved A One Hit Wonder Act With Which Song,Always Look On The Bright Side Of Life +Science & Nature,In What Year Were Tissue Cells First Grown Outside The Body By Us Biologist Ross Harrison ,1905 ,General,What city's name is derived from 'dubh linn',Dublin,General,Which Charity Began In 1934 And Started From A Lock Up Garage In Wallasey Cheshire,Guide Dogs For The Blind +General,A large cage or building for keeping birds,Aviary,General,What is the main ingredient of boxty bread,Potatoes,Food & Drink,What Is Calvados ,A French Apple Brandy  +General,How many spectators can cram into Strahov Stadium (world's largest),240000,General,DELAG was the worlds first what Oct 16 1909,Airline - by Zeppelin,General,Which poet described autumn as 'the season of mists and mellow fruitfulness',John keats +General,"Who is the only singer to have no. 1 hits in the 50's, 60's, 70's, 80's and 90's",Cliff richard,General,The shape of plant collenchyma cells & the shape of the bubbles in beer foam are the same. What are they called,Orthotetrachidecahedrons,General,What is an ogee curve,S-curve + History & Holidays,In the harbour of which city was the Greenpeace flagship Rainbow Warrior sunk in 1985? ,Auckland ,General,"In the 1700s, european women achieved a pale complexion by eating 'complexion wafers' which were actually what poison",Arsenic,General,In what country did Bridge originate,Turkey +Science & Nature,What Is The Microwave Device In An Oven Called ,The Magnetron , Geography,In which country was natural gas first discovered ?,Greece,General,What does Thermos mean in greek?,Hot +Music,"By What Collective Name Were Steve Cropper, Donald Duck Dunn, & Al Jackson Known",The Mg's,General,On ER what is the character name of Mark Greens daughter,Rachel,General,What is the largest constellation in the sky,Hydra +General,What is the Capital of: Netherlands,Amsterdam,Food & Drink,Who invented the Egg McMuffin ,Ed Peterson ,General,Name the main horse in Animal Farm,Boxer +General,How many years did the Hundred Years War last?,115 Years (1337-1453),General,Advertising slogan - No one ever got fired for buying what,IBM,General,In which country are the transylvanian alps,Rumania +General,What was the Latin word for wheel - now a common transport,Truck,General,What kind of water well is under natural pressure,Artesian,General,Whose day long funeral in february 1989 was attended by 55 heads of state,Emperor hirohito +General,What was Richard Wagner's second Opera - idea during sea trip,The Flying Dutchman,General,What metal forms one twelfth of the earth's crust,Aluminium,General,Badminton star Liem Swie King represented which country,Indonesia +General,Muisical Duo Chas & Dave Wrote & Performed The Theme Tune To Which Classic Tv Game Show,Crackerjack,General,Who sang the 1975 remake of the Flamingos' I Only Have Eyes For You?,Art Garfunkel,General,The Lebanese flag bears which tree?,Cedar +Food & Drink,Which Are More Nutritious Brown Or White Eggs ,They Are Both The Same ,Food & Drink,What Is Amontillado ,Sherry ,General,What does BMW stand for,Bavarian Motor Works +Music,Which Band Does Jim Kerr Front,Simple Minds,General,Old Testament two non humans can speak the serpent and who,Balsam's Ass Numbers 22,General,In which country could you spend a Kwanza,Angola +General,What are followers of the unification church,Moonies,General,Who famously owned a yacht called Lady Ghislaine?,Robert Maxwell,General, What is a castrated rooster called,A capon +General,What is the name on fake credit cards,J L Webb,General,Lotta and Vassar were the first two brands of what,Wrigley's Gum,Food & Drink,What bottles of Chianti are traditionally covered with,Straw +Sports & Leisure,On Tv's A Question Of Sport Who were the first two team Captains? ,Henry Cooper Cliff Morgan ,General,What is the Capital of: Bosnia and Herzegovina,Sarajevo,General,"""NUUK"" Is The Capital Of Which Country",Greenland +General,From which fruit is the liqueur Kirsh made,Cherries,Sports & Leisure,Which Football Club Are The Only Team To Go Through A Premiership Season Unbeaten ,Arsenal ,General,Which actress co-starred with Michael Douglas in the film War of the Roses,Kathleen turner +General,Which golfer has won the British Open most times since its inception,Harry vardon,Geography,What is the capital of Bhutan,Thimphu,Science & Nature,"He invented ""bifocal"" lenses for eyeglasses.",Benjamin franklin +General,Axl Rose was an anagram for what phrase?,Oral Sex,General,Harry Rosoll created which famous bear,Smokey the Bear,Science & Nature,"Which Company Started As The Pacific Aero Products Company In 1916 , Was Given Its Present Name In 1917 & Is Today The Largest Aviation Compnay In The World ",Boeing  +General,Jacob German in 1899 got the worlds first what in New York,Speeding Ticket – 12 mph,Music,Who replaced Syd Barrett of Pink Floyd?,David Gilmour,Music,Who Formed The Million Dollar Quartet,"Elvis Presley, Jerry Lee Lewis, Carl Perkins & Johnny Cash" +Science & Nature,What type of paper is used to test for acidity and alkalinity?,Litmus,General,Which very fast ball game is also known as Jai Alai,Pelota, History & Holidays,Why do we decorate with holly at Christmas? ,Represents the crown of thorns worn by jesus  +General,Which profession drinks the most coffee,Health care,General,Where in the world are the most roses grown,Texas,General,Carvel and Clinker are methods of making what,Boats +Sports & Leisure,Who in 2003 aged 33 became the oldest male tennis player to be number one in the ATP's entry rankings? ,Andre Agassi ,General,Polio is also known as _____,Infantile paralysis,Music,What Type Of Band Was Freda Payne Associated With In 1970,A Band Of Gold +General,"Who was ""the face that launched a thousand ships""",Helen of troy,General,A Jocko is what type of animal,Chimpanzee or ape,General,As tough as _______,Nails +Science & Nature," Young birds such as ducks, geese, and shore birds are born with their eyes __________",Open,Music,What Fictional Band From The Film Of The Same Name Did Dan Aykroyd & John Belushi Make Up,The Blues Brothers,General,Who wrote Servants of the Wankh in 1969,Jack Vance SF + History & Holidays,What fell into the pool in Caddyshack which caused a major exodus? ,A Baby Ruth candy bar ,Geography,To what country does the Gaza Strip belong,Egypt, History & Holidays,In The Tv Show Til Death Us Do Part First Aired In 1965 What Football Team Did Alf Support ,West Ham  +People & Places,Which Manager Clipped A Couple Of Hooligans Ears Round The Ear ,Brian Clough ,General,"In the Arthurian legend, who was the son of a demon and a nun?",Merlin,General,Who sang about desmond and molly jones,Beatles +General,Quinsy is the inflammation of which body organ due to abscess,Tonsils,General,What is the characteristic of an aphyllus plant,No leaves,General,"In music, which major scale contains just one flat",F major +General,What was known as the Ox Box,Xerox Photocopier, History & Holidays,Who Released The 70's Album Entitled 2112 ,Rush ,General,Who was the second man to win the Formula One motor racing championship,Ascari +Geography,What state borders Alabama to the north,Tennessee,General,What is the flower that stands for: appointed meeting,Everlasting pea,General,Hydrated Magnesium Silicate is better known as what,Meerschaum or Sepiolite +General,What biblical towns name means House of Bread in Hebrew,Bethlehem,General,How many legs does a crab have?,Ten,General,Bill gates was the founder of which company,Microsoft +Music,Over 4 Decades Which Drummer Led A Band Called The Jazz Messengers,Art Blakey,General,Name the Hotel in Arthur Haley's novel / film of same name,St Gregory, History & Holidays,Who Released The 70's Album Entitled Sticky Fingers ,Rolling Stones  +General,Fatima is a Christian shrine pilgrimage place in which country,Portugal,General,What does a compass needle point to,North,General,What is the fear of kissing known as,Philemaphobia +General,What is a group of this animal called: Leopard,Leap, History & Holidays,Southern Rhodesia became what country in 1980? ,Zimbabwe (Independent Nation of Zimbabwe) ,General,If you were drinking castle beer in what country would you be,South Africa +General,Victor Emmanuel III was the last king of what country,Italy,General,This large bean_shaped lymph gland can expand and contract as needed.,Spleen,General,What languages appear on the Rosetta stone,Egyptian Greek +General,Who invented the first electric razor,Jacob schick, History & Holidays,In What Year Was The United Nations Organisation Formed ,1945 ,Science & Nature," The giraffe's __________ is huge; it weighs 25 pounds, is 2 feet long, and has walls up to 3 inches thick.",Heart +Science & Nature,Which Sex Is Prone To Suicide ,The Male Sex ,General,What symbol on the Bacardi symbol is there because the soil where the sugar cane grows is fertile from the excessive guano,The bat,General,What single colour is the Libyan flag,Green +Science & Nature,What Was The Largest Of All The Carnivorous Dinosaurs ,Tyrannosaurus Rex ,General,Football is the favourite sport of which family,Kennedy,General,What is the decimal equivalent of the binary number 10011,Seventeen +General,The song Honolulu Baby appears in which Laurel and Hardy film,Sons of the desert,General,A kindle is the name for a group of what young animals,Kittens,General,On what date is U.S Independence day?,July 4th +General,What is the birthstone for November,Topaz, History & Holidays,"Born on Christmas Day 1899, which actor starred in 'Casablanca'' and 'The Big Sleep'' ",Humphrey Bogart ,General,What was JFK's nickname for his daughter Caroline,Buttons +General,"Which word is used to mean a serious meditative poem, especially a lament for the dead",Elegy,Science & Nature,A Jacaranda is a type of what? ,Tree , History & Holidays,By who was Gerald Ford almost assassinated?,Squeaky Fromme + Geography,Where are the two steepest streets in the USA?,San Francisco, Geography,Name the city at the west end of Lake Superior.,Duluth, History & Holidays,Which Building In Berlin Was Burned Down In 1933? ,The Reichstag  +Geography,Meridians converge at the What. ,Pole s ,General,What boxer was nicknamed The Ambling Alp,Primo Carnera,General,What was the name of the pub in The Dukes of Hazard,Boars Nest +General,Who was the head of the National Unity party in Norway in 1942,Quisling,General,Charles Russell Is The Founder Of Which Worldwide Religious Organisation?,Jehovas Witnesses,General,What made the crew sick in the movie Airplane?,The fish + History & Holidays,"Which U. S. President insisted on putting lit candles on the White House Christmas tree, even though it was a fire hazard? Calvin Collidge, Herbert Hoover, Franklin Delano Roosevelt, Theodore Roosevelt, ",Franklin Delano Roosevelt ,Music,"According To The Police ""Every Little Thing She Did"" Was What",Magic,General, What is the study of heredity called,Genetics +General,In a recent survey women disliked what part of male body most,Feet,General,Terry Knight was the original producer of what group,Grand funk railroad,Science & Nature,Name The Kitchen Development Patented By American Radar Engineer Percy Spencer In 1945 ,Microwave Oven  +General,What method of transportation did Webster use to get from the upper level of the house to the lower level?,The dumbwaiter,General,What is the commonest item traded internationally,Petroleum and its by products,Art & Literature,In What Science Fiction Novel Do Duke Leto Atreidea & The Harkonnens Feature ,Dune  +Geography,"Who Established A Colony At Roanoke Island & Christened It (Virginia) In Honour Of Elizabeth I, The Virgin Queen ",Sir Walter Raleigh ,General,Where was the home of the flintstones,Bedrock, History & Holidays,In the song the 12 days Of Christmas what did my true love give on the 5th day ,5 Gold Rings  +General,The Italian Chianina is recognises as being the oldest what,Breed of Cattle,General,What is 'hoi polloi' in english,The masses,General,"He played ""grizzly adams""",Dan haggerty +General,How tall was thumbelina,One inch, Geography,What is the capital of Ecuador ?,Quito,Geography,"Yuletide_named towns in the United States include Santa Claus, located in Arizona and Indiana, Noel in Missouri, and Christmas in both Arizona and _____________",Florida +General,The Atomium is a famous tourist attraction in which city,Brussells,General,What is Indiana Jones first name,Henry,Music,What Did The Chairmen Of The Board Ask For In Their 1970 Smash,Give Me Just A Little More Time +General,"Ideally, what should be the total of your cards in Baccarat",Nine, History & Holidays,December 26th is known as Boxing Day but it is also which Saints Holy Day ,St. Stephen ,Music,What is Paul's Mccartney's real birth name?,James Paul McCartney +General,What is grandpa Simpsons first name,Abraham,General,How was Boris Karloff listed in the credits 1931 Frankenstein,As ?,General,John Palmer is buried in York what better known name,Dick Turpin +General,What nation invented the toilet seat,Egyptian,General,A fellmonger deals in what items,Animal skins,General,What type of food is Otak Otak in Malaysia,Grilled Fish Pate +General,What is the eardrum,Tympanic membrane,General,The Guarani is the unit of currency in which South American country,Paraguay,General,Which is the second largest city in Sweden,Gothenburg +General,Dr Seuss created the first animated TV ad for which company,Ford,Music,"Who Recorded The Albums ""In The Army Now"" And ""Whatever You Want""",Status Quo,Food & Drink,What Is The Best Selling Flavour Of Haagen Dazs Ice Cream ,Vanilla  +Sports & Leisure,What Piece Of Sporting Equipment Is 5 Meters Long And 10 Centimetres Wide? ,Gymnastics Beam ,General,What us national park contains gumbo limbo trail,Everglades,General,In which ruins was the first known written advertisement found,Thebes +General,On which island is the Sir Garfield Sober Sports Stadium,Barbados,General,Which country's national symbol is the harp,Ireland,General,Who presided over the trial of jesus,Pontius pilate +General,What is the heraldic term for a diamond shape,Lozenge,General,Which 19th century battle UK / USA fought after peace signed,Battle of New Orleans,Music,"Whose 1996 Debut Album Was Entitled ""1997""",Ash +Toys & Games,How many squares are there on a chessboard?,64,General,Daisy Hawkins original name of which Beatles hit song,Eleanor Rigby,General,Who Narrated The Childrens Tv Show The Magic Roundabout Whn It Resumed Broadcast In The 1990's,Nigel Planner +General,Which Sport Was Invented By Doctor James Naismith In 1891?,Basketball,General,Brownie Wise first cover woman on Business week developed?,Tupperware Party,General,What aerosmith song was written about vince neil of motley crue,Dude looks +Science & Nature,What is a young swan called,Cygnet,General,What ingredient is always found in a carciofo sauce,Artichokes,General,"Which film actress, in 1991, appeared nude and in a state of late pregnancy on the cover of 'Vanity Fair' magazine",Demi moore + History & Holidays,What did the Romans call Wales? ,Cambria ,General,Records show four Popes died doing what,Having Sex,General,What do you give for a one year wedding anniversary,Paper +General,Who walked the Via Dolorosa - literally Dolorous Way,Jesus from court to Crucifixion,General,Who Sang With Elton John On His Only Top Ten Hit Of 1996,Luciano Pavarotti,General,Who is the only director to win 3 Oscars within five years in 30s,Frank Capra 1934 1936 1938 +General,What do the auricularis muscles move,Ears,General,"Of what are Karakul, Texel, Romney Marsh types",Sheep,General,When was the first Mad Max film released,1979 +General,"What is the song title of neil diamond's 'vanilla soup, a double scoopie'",Porcupine pie,General,Which hereditary form of anaemia largely affects people of sub-Saharan African descent,Sickle cell anaemia,Music,Who Wrote The Song Coat Of Many Colours About A Girl Ridiculed About Her Coat Made From Fabric Scraps,Dolly Parton +Music,Name Fats Domino's Home Town,New Orleans, Geography,What US state has the most tornadoes on average?,Texas,Geography,What is the capital of The Netherlands,Amsterdam +General,What is the common name for the Aurora Borealis?,Northern lights,Science & Nature,Which Colour Is At Top Of A Rainbow? ,Red ,Entertainment,This term means to play smoothly.,Legato +Science & Nature,Name the mammal living at the highest altitude.,Yak,General,"Who invented the idea of ""infantile sexuality"" or the Oedipus complex",Sigmund freud,General,Panthophobia is the fear of,Suffering and disease +Entertainment,Who sang 'Bad Case Of Loving You'?,Robert Palmer,Sports & Leisure,"Which snooker player was fined 20,000 for assaulting an official in 1996? ",Ronnie O'Sullivan ,Science & Nature,What does URL stand for in computing? ,Uniform Resource Locater  +General,What does a pilot drop to slow an airplane?,Flaps,General,What doctor discovered the source of malaria in 1880,Charles laveran,General,Which charismatic but controversial European never won Wimbledon but was the first ATP number one ranked male tennis player in 1973,Ilie nastase +People & Places,Which MP & Former Leader Of The GLC Keeps Salamanders ,Ken Livingstone ,General,Who was corsica's most famous son,Napoleon,General,What is the combination of fine clay and sand for making pottery,Terracotta +General,C T Russell founded what organisation,Jehovah's Witnesses,Entertainment,What are the names of Donald Duck's nephews?,Huey Dewey and Louey,Music,According To The Beatles Who is Dr. Winston O'Boogie?,John Lennon +General,What was the first recorded message,Mary had a little lamb,General,Actor ______ Savalas,Telly,General,Name bald head character that peers over walls saying wot no,Chad +General,What does a pteridologist study,Ferns,Geography,What is the capital of Guinea,Conakry,General,Who composed the opera Ill Travatore,Guiseppe Verdi +General,Progressive opacity of eye lens,Cataract,Music,"Whose 1973 Album Was Titled ""Jonathan Livingston Seagull""",Neil Diamond,General,"In Terms Of Population Which Is The Largest City In The EU, Not To Be It's Country's Capital City ?",Hamburg +General,1978 a 100 yard long queue outside Peeking bookstore selling?,Works of Shakespeare 25 cents,Science & Nature,The Carloline Institute of Stockholm won the nobel prize for what?,Physiology and Medicine,General,"What TV comedian with no ID card was asked to give a Tarzan yell to verify her identity, at Bergdorf Goodman",Carol Burnett +Music,"Which Famous Pop Star Guess Starred Along Side Mr.T In An Episode Of ""The A Team""",Boy George,General,What was the former name of the Czech town of Kalovy Vary,Carlsbad,General,Hawaii lies on the Tropic of,Cancer +General,"""Do unto others as you would have others do unto you"" is also called what",Golden rule,General,Complete the proverb: 'What you lose on the swings..',You gain on the roundabouts,Geography,"Which country uses the ""yen"" for currency",Japan +General,What is a sphygmomanometer used to measure,Blood pressure,General,Who spells his name T I double grr,Tigger,General,"Which Fashion Label Has It's Headquarters In ""Treviso"" In Italy",Benetton +General,Name the one person who has won a Nobel and an Oscar,G B Shaw Lit 1925 Pygmalion 1938,General,What TV show takes place in Sunnydale,Buffy the Vampire Slayer,General,"Triplets: love, honor, and",Obey +General,"The Great Lakes are: Lake Superior, Lake Erie, Lake Huron, Lake Michigan & Lake",Ontario,General,Who commanded the English fleet which defeated the Spanish Armada,Admiral howard,General,Who was the father of Icarus,Daedalus +Entertainment,Where do Rocky and Bullwinkle play football?,What'samatta University,General,Elmo Lincoln was the screens first what 1918,Tarzan,General,"Which ancient town, capital of the legendary King Minos, was excavated and reconstructed by Sir Arthur Evans between 1899 and 1935",Knossos +General,"Who wrote the Opera ""Love of Three Oranges""",Prokofiev, History & Holidays,What was Marilyn Monroe's last film? ,The Misfits ,General,"Belushi What branch of science studies heat, electricity and magnetism",Physics +General,What can you find on California's Mount Cook,Hollywood sign,General,In which country would you find the 'Angel Fall's,Venezuela,Art & Literature,"From the french word 'fauve', meaning 'wild beast'. A style adopted by artists associated with Matisse, c. 1905-1908. They painted in a spontaneous manner, using bold colors.",Fauvism +Tech & Video Games,Who programmed Roller Coaster Tycoon? ,Chris Sawyer,General,In which country would you find the Ida mountains,Turkey,General,What toy was in short supply for the 1983 Chirstmas season?,Cabbage Patch Kid +General,How Is Barbara Millicent Roberts Better Known?,Barbie (Barbie Doll),General,Which nut is used to make Marzipan,Almond,General,What Apollo lunar landing was canceled after a tank explosion,Apollo 13 +Religion & Mythology,What animal's meat can a Muslim not eat?,Pig,General,What direction is the sahara expanding in by a half a mile a year,South,General,Who created 'The Muppet Show',Jim henson +General,In mythology what is the offspring of a God and mortal called,Hero,General,"Down, husk, trace, trip all collective nouns what creature",Hares,General,Entomophobia is a fear of ______,Insects +General,Where was george a custer defeated,Battle of little bighorn,General,"What is the name of the strong, heavy grating lowered to block the entrance to a castle",Portcullis,Science & Nature,What Solid Has The Biggest Volume For A Given Surface Area ,A Sphere  +Food & Drink,Which contribution to western tea culture was introduced at the St. Louis world fair in 1904 ,Iced Tea ,General,Overhead metal bar strengthening the frame of a vehicle and protecting the occupants if it overturns,Roll bar,General,Who is fond of saying 'i will gladly pay you tuesday for a hamburger today',Wimpey +General,Who Designed The Original Blue Peter Badge That Has Been The Shows Logo For Over 25 Years,Tony Hart, History & Holidays,Who Released The 70's Album Entitled Parallel Lines ,Blondie ,General,Which singer died a happy man on a Spanish golf course in 1977,Bing crosby +General,"Isaac Newton dropped out of school when he was a teenager, at his mother's request. She hoped he would become a successful?",Farmer,General,"Which Sportsman Got His Nickname From A Character From The Movie ""Cat On A Hot Tin Roof""",Big Daddy, History & Holidays,"""What Did My True Love Give To Me On The """"Sixth"""" Day Of Christmas"" ",6 Geese A Laying  +General,In which book did four ghosts visit scrooge,A christmas carol, History & Holidays,Which song was a Christmas No 1 in both 1975 and 1991 ,Queen's 'Bohemian Rhapsody ,Geography,What country formed the union of Tanganyika and Zanzibar,Tanzania +Music,Manhattan Transfer Sang A Song About A Boy From Chicago Or New York City,New York City,Sports & Leisure,"In baseball, how many outs are there in an inning?",Six,General,"What role does mickey mouse play in ""fantasia""",Sorcerer's apprentice +General,Who wrote and starred in the 1922 version of Robin Hood,Douglas Fairbanks,General,Which pop singer was Glad to be Gay,Tom Robinson, History & Holidays,Who Directed The Original Halloween Movie ,John Carpenter  + History & Holidays,When was Julius Caesar murdered?,Ides of March,General,Which bird is sometimes nicknamed 'Mother Carey's Chicken' especially by sailors?,Storm Petrel,General,What scandinavian country owned iceland from 1262 to 1944,Denmark +General,Nat King Cole died in which year,1965,General,"Who wrote the book ""Couples""",John updike,General,Gerald Gardner in the 50s founded which pagan organisation,Wicker +General,Ireland has the highest per capita consumption of ______,Tea,General,"If an animal has feathers, what is it",Bird,General,"Who starred in 'billy madison', 'happy gilmore', 'the wedding singer' and 'the water boy'",Adam sandler +General,"In the movie 'Aliens', what was Newt's real name?",Rebecca Jorden,Food & Drink,From which country does the dish paella originate? ,Spain ,General,Who wrote the thriller Jurassic Park,Michael crichton +General,"What kind of garment is a 'Sloppy Joe""",Sweater,General,He was 'dancing on the ceiling',Lionel richie,General,"Of which island group is Viti Levu, the largest",Fiji +General,What's white sugar mixed with to make brown sugar,Molasses, History & Holidays,Who started the second Punic war?,Carthage,Entertainment,"With which period in music do we associate composers such as Tchaikovsky, Mendelssohn, and Chopin?",Romantic period +General,Clinophobia is the fear of what,Beds,General,What is the marseillaise,French national anthem,General,What is the covering on the tip of a shoelace called?,Aglet +General,What is a group of this animal called: Duck,Brace paddling team,General,Hansenosis is more commonly known as___.,Leprosy,General,Lobster Newberg was invented at what famous restaurant,Delmonicos +General,Seawise University burned 9 Jan 1972 used to be called what,Queen Elizabeth,General,The bagpipe was originally made from the whole skin of a ______,Dead sheep,Science & Nature," A good milking cow will give nearly 6,000 quarts of __________ every year.",Milk +Music,Lou Gramm Was A Member Of Which Band,Foreigner,General,Rene Raymond is better known as which author,Raymond Chandler,General,"Of which island do Ireland, Britain, Iceland and Norway dispute ownership?",Rockall +Science & Nature," Every hour, nearly 12,500 puppies are born in the __________",United states,General,Which American clarinettist and bandleader created the jazz idiom known as swing,Benny goodman,Music,Who Sang The Theme To The Bond Movie A View To A Kill,Duran Duran +General,In what Australian state would you find Wodonga,Victoria,Music,What is the very first track on Spice?,Wannabe,General,How many birthday candles were Michael Jackson & Madonna each obliged to blow out in 1998,40 +General,"Who lived at 704 hauser street, queen's, new york",Bunkers,Science & Nature,What does the computer language For-Tran stand for? ,Formula Translation ,General,Which city was built in the design of a union flag,Khartoum +Science & Nature,What Do The Initials S.A.D Stand For ,Seasonal Adjustment Disorder , Geography,Name the large mountain chain in the eastern U.S.A.,The Appalachians,Science & Nature,What animal has the longest lifespan in captivity ,The giant tortoise  +General,The dogon are an _____,African tribe,General,In WW2 what linked members of the Caterpillar club,Life saved parachute,Music,The Beatles first major recording audition was at what label?,Decca +General,If music was played leggiero how should it be done,Lightly,Sports & Leisure,Who Beat Muhammad Ali In 1978 To Assume His World Heavyweight Title ,Leon Spinks ,General,Collective nouns - A train of what,Camels +Geography,"The border between _________ and the U.S. is the world's longest frontier. It stretches 3,987 miles (6,416 km).",Canada, Geography,Which island was born near Iceland in 1963 ?,Surtsey,People & Places,"In September 2003, Fathers 4 Justice campaigner Jason Hatch caused an embarrassing security breach at Buckingham Palace dressed as who? ",Batman  +General,"In the film 'pretty woman', for who was goldie hawn the body double",Julia, History & Holidays,"What was the instrument of execution during the ""Reign of Terror""",Guillotine,General,"In Greek mythology, who did oedipus marry",Jocasta +General,What is a necklace of flowers called in hawaii,Lei,General,Which is the geological period before the Ordovician,Cambrian,Music,Which Singer Is The Younger Sister Of Loretta Lynn?,Crystal Gayle +People & Places,Where would you find the Sea of Tranquility ,On The Moon ,General,What Was The Character Sonny Crocketts Actual First Name In The TV Series Miami Vice,James,Science & Nature,How man legs does a crab have?,Ten +General,"In the 1990 film 'The Krays', who played Violet Kray, the mother of the Kray brothers",Billie whitelaw,General,What is the capital city of Botswana,Gaberone,General,Which author wrote the belgariad series,David eddings + Geography,What is the basic unit of currency for Japan ?,Yen,General,Where on the body is the jugular vein,Neck,General,Who invented the telescope,Galileo galilei +General,Omolagnia is sexual arousal from what,Nudity,General,"Where would you find Fetcher, Mac, Babs, Ginger, Bunty And Fowler",Chicken Run (Movie), History & Holidays,"A German tradition is to hide a certain food in a Christmas tree, and the first one to find it gets a special gift. What is the certain food they hide? ",A Pickle  +Geography,What body of water borders Saudi Arabia to the east,Persian gulf,General,In Bexley Ohio its illegal to put what in an outhouse,Slot Machines,Science & Nature," __________, like grasshoppers _ feel no pain. They have a decentralized nervous system with no cerebral cortex, which in humans is where a reaction to painful stimuli proceeds.",Lobsters +Sports & Leisure,Where were the 1936 Olympics held ?,"Berlin, Germany",General,What city became the U.S. federal capital in 1789,New york,General,Substance that neutralizes alkalis and turns litmus paper red,Acid +General,Bangkok is which country's capital,Thailand,General,"What is made up of England, Scotland, Wales & Ireland",United kingdom,General,In June 1906 where was the worlds first grand prix held,Le Mans in France +General,What east european country's people spend stotinki,Bulgaria,General,Who composed the opera Billy Budd in 1951 (both names),Benjamin Britain,General,When performing pop artist Marshall Mathers uses which name,Eminen +General,"In Which Capital City Was The Crime Fighting Organisation ""Interpol"" Actually Founded",Vienna,Science & Nature,What Is Anaemia ,A Deficiency Of Red Blood Cells ,Music,Who Had A White Riot In 1977,The Clash +General,Scotophobia is the fear of,Darkness,General,Ransom Eli Olds started this company in 1897.,Oldsmobile,Music,Cathy McGowan Was The Presenter Of Which Popular Show,Ready Steady Go +Science & Nature,How Many Bits In A Byte ,8 ,Science & Nature,What is activated for freckles to appear?,Melanocytes,Music,What Was The Jams Town Called,Malice +Science & Nature,He is the Most Durable TV Astronomer,Patrick Moore,Music,Which Was The First Beatles Album To Consist Entirely Of Original Material,A Hard Days Night,Food & Drink,Name the only fruit named for its color ,Orange  +General,"What is the name of jefferson's home, pictured on the back of a us nickel",Monticello,Music,Which Beach Boy accompanied the Beatles to visit the Maharishi?,Mike Love,General,"If you flew due east from new york city, what would be the first country you would reach",Portugal +General,"Who ghost-wrote Xaviera Hollander's ""The Happy Hooker""",Yvonne dunleavy,General,On the Munsters what was Lillie's maiden name,Lilly Dracula,General,What was the profession of Lancelot 'Capability' Brown,Landscape gardener + Geography,What is the capital of Texas?,Austin,General,Who was shot only hours after Annie Leibovitz took a nude photo of him for Rolling Stone,John lennon,General,What was the name of the beatles' corporation after it was known as 'the beatles and company',Apple +General,Rosetta Jacobs became famous under what name,Piper Laurie,General,Who played the switchboard operator in cutter to houston,John nicholson,General, The treatment of disease by chemical substances which are toxic to the causative micro_organisms is called ________.,Chemotherapy +General,Where was the septuagint written,Alexandria,General,Photophobia is the fear of,Light,General,What are train drivers called in the U.S.A.,Engineers +General,"Which children's character famously lived at 52, Festive Road?",Mr. Benn,General,What type of bears are the best swimmers,Polar bears,General,What groups demo rejected by EMI in 1965 cos not own songs,The Who +General,What is the Capital of: Albania,Tirana,General,By feeding hens certain ____ they can be made to lay eggs with varicolored yolks,Dyes,General,What's the more common name of the Crux Gammata,Swastika +General,"This cereal's commercials feature a ""Cookie Crook"" and a ""Cookie Cop""",Cookie crisp,Science & Nature,Name the second_largest planet in the solar system.,Saturn,General,What mistake did Coca-Cola make in 1985?,New Coke +General,"Germany's allies in wwi were austria-hungary, bulgaria and ______",Turkey, History & Holidays,Good King Wenceslas Was The Duke & Later King Of Where? ,Bohemia ,General,Ghost who calls munich the 'monaco of bavaria',Italians +General,Who was it that 'the untouchables' were formed to stop,Al capone, History & Holidays,Which Series Of Horror Movies Were Greatly Influenced By A Painting By Norwegian Artist Edvard Munch ,Scream ,General,Bangalore and Jaipur are in which country,India +General,What musical was produced on London stage 30 years after film,Singing in the Rain Tommy Steel 1983,People & Places,Which cosmetic magnate died in 2004 at the age of 97? ,Estee Lauder ,General,What did Tommie Smith wear at his victory ceremony in the 1968 Olympics,Black glove +Music,What Was The Name Given To Duels Between Soloists Where They Would Improvise Head To Head Until One Took A Clear Advantage,Cutting Contests, History & Holidays,Of What Tribe Was Boadicea The Head? ,The Iceni ,Religion & Mythology,"In Greek mythology, what did Daedalus construct for Minos?",Labyrinth +General,In what sport did the word 'crestfallen' originate,Cockfighting,General,"American money with serial's beginning in ""b"" are printed in _____",New york,General,What is the name of a quarter of jerusalem that can be translated as 'hundred gates',Mea shearim +Music,What was REO Speedwagon's 1983 chartbusting LP?,High Infidelity,General,On what number is the sexagesimal system based,Sixty,General,Name the singer who won the 1998 Eurovision Song Contest with the song Diva,Dana international +General,What are woolly and spider types of,Monkey,Science & Nature," The __________ can travel up to 45 miles per hour, whereas the rabbit can achieve an average speed of just 35 miles per hour.",Hare,General,"What film starred helen hunt, gary elwes and bill paxton",Twister +General,What was lech walesa's job before he founded solidarity,Electrician,General,What is the southernmost country in continental europe,Spain,General,It is against the law in Albania to play what on Sundays,Dominos +General,"Who, in 1865, was the first U.S. president to be assassinated",Abraham lincoln,General,What is the name for the cutting and training of shrubs into decorative shapes,Topiary,General,What's unusual about the moons of Uranus,Named Shakespeare characters +General,With what are crocodiles often confused,Alligators,General,What planet has a magnetic field 50 times stronger than that of Earth,Uranus,General,The WWSU governs what sport,World Water Skiing Union +General,What is the capital of Dominica?,Roseau,General,Spelling counts - what is the singular of scampi,Scampo,General,The Irish Province of Connaught contains five counties. Sligo and Galway are two. Name one of the others. leitrim,Mayo roscommon +General,Which vegetable is used to make Moussaka,Aubergine,General,What is the last letter of the Greek alphabet (written out in english),Omega,General,What do you get when you add fresh fruit to red wine,Sangria +Science & Nature, Marie Antoinette's dog was a spaniel named __________,Thisbe,Sports & Leisure,"In Water Polo, Which Player Wears A Red Cap ",The Goalkeeper ,General,"Open in the early 20th century, what was used to treat epilepsy",Rattlesnake venom +General,Where in Italy is the wine Marsala made,Sicily,General,What's a ginkgo,Tree,Science & Nature," Hummingbirds are the smallest birds _ so tiny that one of their enemies is an insect, the __________",Praying mantis +General,Any of a group of composite organisms made up of a fungus and an alga living in symbiotic association (symbiosis)?,Lichen,General,The longest bike weighed how much,More than a ton,General,In the Bible name Isaacs wife,Rebekah +General,Women 375 - 1 Men 1400 - 1 chance of doing what,Living to 100,General,The Millennial Dawnists changed their name to what,Jehovah's Witnesses,Art & Literature,What Was Author George Elliots Real Name ,Mary Ann Cross (Nee Evans)  +General,"Which dramatist, who died in 1950, said ""'I'm only a beer teetotaller - not a champagne teetotaller""",G b shaw, History & Holidays,Where did the battle of Waterloo take place?,Belgium,Science & Nature,What name is given to a male bee ?,Drone + History & Holidays,Which duo had a 1987 Christmas hit with a cover of the Elvis song 'Always On My Mind'' ,The Pet Shop Boys ,General,Where will the 2002 Winter Olympics be held,Salt Lake - USA,Religion & Mythology,Neptune was the Roman god of the ______?,Sea +General,What duo lost their Grammy for Best New Artist from the eighties?,Milli Vanilli,General,What language does 'danke' mean 'thank you' in,German, History & Holidays,How Many Ships Came Sailing In On Christmas Day ,Three  +General,How many runs are scored in cricket if the ball is hit over the boundary without bouncing,Six,Science & Nature,How many bones are there in the human body?,206,Geography,"The countries of Belgium, Netherlands, and Luxembourg are together called ________.",Benelux +General,What cartoon characters are at 6 flags theme park,Looney tunes,General,"What product first produced in germany had a formula that combined alcohol, lemon spirits, orange bitters and mint oil",Cologne,General,What is the fear of thinking known as,Phronemophobia +General,Which TV detective series is set in the town of Denton,Frost,General,A lido is open air swimming pool where was the original lido,Venice,General,Which actor started his film career as a giggling psychotic killer in the gangster movie Kiss of Death,Richard widmark +General,What is the bass Xylophone called,Marimba,General,What is the name of the bacterium that causes food poisoning in contaminated food,Salmonella, History & Holidays,In 1954 the Salk vaccine was introduced - a vaccine which effectively wiped out which devastating disease ,Polio  +Entertainment,What was the name of the two space shuttles in 'Armegeddon'?,Freedom and Independence,General,The Jeffersons was a spinoff from what show?,All in the Family, History & Holidays,Name the first black nation to gain freedom from European colonial rule.,Haiti +General,Hebrew comes from a Babylonian word meaning what,Vagrant, Geography,What small island is in the bay of Naples?,Isle of Capri,General,Who was irma's first roommate in my friend irma,Jane stacy +General,Kangaroos and Emus can't do what,Walk backwards,Music,"Who was lead singer with Van Halen, but went solo in 1985?",David Lee Roth,General,In mythology Odysseus blinded which Cyclops,Polyphemus +Art & Literature,How Many Tales Are There In Chaucers Canterbury Tales ,23 ,General,Which Football club were the first in modern times to set a trend of building a purpose-built stadium away from the town centre?,Scunthorpe United,General,Odynephobia is the fear of,Pain +General,Which gland is enlarged in the condition known as 'goitre',Thyroid gland,General,Who posted the highest score on 'celebrity jeopardy' in november 1994,Norman,General,"Translate ""simba"" from swahili into english",Lion +Geography,What Are The Northern Lights Otherwise Known As ,Aurora Borealis ,General,"Who had top ten hits in the 1960s with 'Together, Somewhere' and 'Hold Me'",P.j. proby,Science & Nature, A male baboon can kill a __________,Leopard +General,What is the part of the sole between the heel & the ball of the foot,Shank,General,What area in NY is named after a barrier built to keep out Indians,Wall Street,General,This re opened in 1975 after being closed for 8 years,Suez canal +General,Who's Eric Cartman's father?,His mother,General,What is the official language of somalia,Somali,Geography,In which city is the Coliseum located,Rome +General,Which American Box Office Smash Movie Is The Most Profitable Comedy Of All Time,Beverly Hills Cop,General,What Was Mother Teresa's Real First Name,Agnus,Art & Literature,Who Wrote The Novel Emma ,Jane Austen  +General,What is the young of this animal called: Cat,Kitten,General,"In 1943, a Swiss chemist accidentally discovered which drug",Lsd,General,"According to folklore, the Giant's Causeway was originally built to link to which Scottish island?",Staffa +General,Bugs Bunny was a caricature of what actor,Clark Gable,General,What is a group of this animal called: Oyster,Bed,Music,"Who Was The Lead Singer Of ""Go West""",Peter Cox +General,Which microbe produces alcohol,Yeast - alcohol is yeast piss,General,"In Entertainment ""Julia Smith & Tony Holland Are Both Famous For Creating What",Eastenders,General,In Shakespeare King Lear was king of where,Britain +Music,Who Had The First British No.1 Single Of The 1970's,Rolf Harris,General,What is added to brandy to make a sidecar,Cointreau or Triple Sec, History & Holidays,In Which Year Did Abba Win The Eurovision Song Contest ,1974  +General,"What were the names of the host and his sidekick on ""Fantasy Island""?",Mr.Rourke and Tattoo, Geography,What is the capital of Mali ?,Bamako,General,"What kind of flower is called bog torch and frog spear""",Orchid +General,British playwright wrote the screenplay for The French Lt Woman,Harold Pinter,General,Species what country are yoU.S.tuck in if you're doing hard time at boniato prison,Cuba,General,What is a Tibetan priest,Lama +General,Who wrote the Opera Madam Butterfly,Puccini,General,What is a group of this animal called: Snake,Nest,General,Plane figure with 7 sides and angles,Heptagon +General,What cartoon featured a boy with a community of tiny people living in his wall?,The Littles,General,In dating agency adverts what does the 'S' represent in the abbreviation 'NSP',Smoker,General,This South African surgeon led the team that performed the first human heart transplant in 1968,Dr. christiaan n. barnard +General,The correct name for the voice box is the _________.,Larynx,Entertainment,Secret Identities: Lyta Trevor,Fury,Science & Nature,Which substance has the chemical formula HNO3?,Nitric Acid +Music,"Along With A Raunchy Video Who Had A Hit With ""Physical"" In 1981",Olivia Newton John,General,In 1814 1st abdication of Napoleon; he is exiled to,Elba,Geography,______________ is the fourth_largest island in the world. It is approximately the same size as the state of Texas.,Madagascar +General,What is the flower that stands for: rustic oracle,Dandelion,General,What country does French toast come from,Italy - Rome,Music,Who recorded 'Long Tall Sally' in 1956?,Little Richard +General,How did Billie Holiday get the nickname Lady,Refused to pickup tips with vagina,General,What town did Billy Joel call home,Long island,Science & Nature,Which Insect Is Popular With Gardeners Because It Feeds On Aphids ,The Ladybird  +General,Where did Little Miss Muffet sit?,On her tuffet,General,"What vegetable varieties include snowball, white horse, & igloo",Cauliflower,General,In what famous US building would you find Broadway,Alcatraz - main prison block +General,Who shared the 1978 Nobel Peace Prize with Menachem Begin,Anwar sadat,General,"What is the process whereby one metal is coated with a thin layer of another, more reactive metal",Galvanizing,General,"Turks head, Granny and Bowline are types of what",Knot +General,"Also known as the Chile Pine, what is the common name of the tree Araucaria araucana",Monkey puzzle,General,Which spirit is the base of a White Lady Cocktail,Gin,General,Who was the first american born child of english settlers,Virginia dare +General,What are chanterelles and morels,Mushrooms,General,If someone said they were from Hellas - which country,Greece,General,In Florida women can be fined for falling asleep under what,Hair Drier +General,Who owned the sword Joyeuse,Charlemagne, Geography,In which city is the C.N. Tower?,Toronto,General,What group of people founded Liberia in 1847,Freed american slaves +Art & Literature,"Who wrote ""Animal Farm""?",George Orwell,Music,Name The Composer Of Red Red Wine,Neil Diamond,General,What month was the Frankenstein monster created,November +General,What is the Capital of: Belize,Belmopan,General,Which groups third album was 'sports' and featured 'heart and soul' and 'i want a new drug',Huey lewis and the news,General,A broom made of twigs,Besom +Science & Nature,The name for the Russian equivalent of Skylab is ________.,Salyut,General,"These come in types like breakfast, pork, lamb, spiced, beef and thick",Sausage,General,What is the name for a chicken less than one year old,A Pullet + History & Holidays,How Long Was The American Civil War ,Four Years ,Sports & Leisure,Which Is Longer A Baseball Bat Or A Tennis Racket? ,A Baseball Bat ,Music,Killing Me Softly Went Straight To No.1 In 1996 Who Sang It,Fugees +General,"Complete this title a novel by George Eliot ""Daniel",Deronda,General,"French bread filled with meat, cheese and salad is called a what",Hero sandwich, History & Holidays,"The following is a line from which 1970's film 'You Came In That Thing, You're braver than I Thought' ? ",Star Wars  +General,What are pug marks,Tiger paw prints,Music,"Who played the electric piano on ""Get Back""?",Billy Preston, History & Holidays,Who invented the aerosol?,Erik Rotheim +General,If You Follow The FINA Rules Which Spoert Do You Practice,Swimming,General,What is the worlds longest mountain range,The Andes,General,Whats the largest lake in Africa,Lake victoria +General,The closest relative to the spiny anteater is what,Platypus,Sports & Leisure,Who Did France Beat To Win The Euro 2000 Football Final? ,Italy ,General,What sport takes place over a distance of 440 yards,Drag Racing + History & Holidays,Which singers Christmas favourite is having his `nuts roasting on an open fire___'? ,Nat King Cole ,General,2.47105 acres is equal to what SI unit,Hectare, History & Holidays,"What Was Julie Christie's Home Town A=Bradford, B= Birmingham, C=Bath ",B= Birmingham  + History & Holidays,What was the name (4 letters) of the New York night club that helped launch the career of several early new wave groups? ,CBGB's ,Music,How Many Grammys Did Christopher Cross Win In 1980,Five,General,What is 'pollo' on a menu in rome,Chicken +General,What is the fear of solitude or being alone known as,Monophobia,General,What is the young of this animal called: Eel,Elver,General,"On What Type Of Object (Be Specific) Will You Find The Phrase ""Annuit Coeptis""",A Dollar Bill (Back) +General,What printer did seiko develop for the 1964 tokyo olympics,Dot matrix,General,Which grand prix circuit is only 1.95 miles long,Monaco,General,Which dinosaur ahd a huge sharp claw on its foot,Veloceraptor +General,Who sang 'mr sandman',Andrews sisters,General,What artistic term was named after a French finance minister,Silhouette,General,What is Indiana Jones main weapon?,His whip + Geography,What country is directly west of Spain?,Portugal, Geography,What is the capital of Kazakhstan ?,Astana,General,The Greek Goddess Ephesus is the Goddess of what,Chastity +Science & Nature,What is the meaning of the name of the constellation Coma Berenices ?,Berenice's Hair,General,Who was Andromedas mother,Cassiopeia,General,"This country's flag has a large ""r"" on it",Rwanda +General,What Famous congressman was on an episode of the Golden Girls?,Sonny Bono,Geography,What is the capital of Burkina Faso,Ouagadougou,General,"Two 1.5 volt batteries, when connected in series, produces _ volts",Three +Food & Drink,What Are You Doing If You Are Shucking An Oyster? ,Opening It ,General,Where did the philosopher Plato teach,Academia,Sports & Leisure,On Tv's A Question Of Sport Who has replaced John Parrot as one of the team captains? ,Frankie Dettorie  +General,If you sailed due West from Japan what country would you hit,Korea,General,What job involves walking an average 60 miles in a 5 day week,Waiter,Mathematics,What is the only digit that has the same number of letters as its value?,Four +Entertainment,Who was the frontman of Nirvana?,Kurt Cobain,General,Who wrote and directed the 1977 film 'Annie Hall',Woody allen,General,In which Irish county is Bantry Bay,Cork +General,Jodie Foster directed the film 'Little Man Tate' in what year,1991,Music,Whose Real Name Is Robert Zimmerman,Bob Dylan, Geography,What continent is the home to the greatest number of countries?,Africa +Art & Literature,Who writes the discworld novels?,Terry Pratchett,General,What is a runcible spoon,A broad Pickle fork,General,What is the Capital of: Marshall Islands,Majuro +Sports & Leisure,Which former Olympian lit the Olympic flame at the 1996 Atlanta Games? ,Muhammad Ali ,General,Shane Fenton became famous as who,Alvin Stardust,General,What is Europe's largest port,Rotterdam +General,What was u2's first album released in the u.s,Boy,General,What name is given to the division between the nostrils,Septum,General,Which famous poem features a hamlet called Auburn,Goldsmith's the deserted village +General,Who is Woodie Woodpeckers girlfriend,Winnie Woodpecker,General,What is the medical term given to the study of the brain and nervous system,Neurology,General,Name the actor who played Ben Casey (both Names),Vince Edwards +Geography,The world's largest lake has a very misleading name. What is it? ,Caspian Sea ,Technology & Video Games,What was the first cartridge that had a battery backed save feature? ,The Legend of Zelda,General,Basketball: the boston ___________,Celtics +General,Varietal name applied to two different minerals,Alabaster,Music,What is the longest Beatles song on record?,I Want You (She's So Heavy),General,Medieval wine measurement there are two buts to a what,Tun + Geography,What is the basic unit of currency for Ukraine ?,Hryvnia,General,Hugh Hefner and Katherine Hepburn both had degrees in what,Psychology,Music,"Whose Debut Hit ""No No No"" Featured Ex Fugee Wyclef Jean",Destiny's Child +General,Which Queen Of England Actually Never Set Foot Here,Berengaria (Wife Of Richard I),General,What is Kabuki in Japan,Common peoples theatre,General,What historical event was referred to as Black 47,Irish Potato Famine +Music,Today Was An Album Released In 1961 By Which British Rock N Roll Star,Cliff Richard,General,Who said the quickest way of ending a war is to lose it,George Orwell, History & Holidays,"Who Had An 80's Hit With The Song 'Kiss On My List', ",Hall and Oates  +General,Which country has won the most nobel peace prizes?,USA,General,The word Calendar comes from Latin and means what,To Call Out,Science & Nature,In Which Country Did Charles Lindbergh Land After The First Solo Transatlantic Flight In 1927 ,France  +General,What is the oldest honor society in the U.S. founded in 1776,Phi beta kappa,General,"Which flying mouse, a cross between Superman and Mickey Mouse, appears in over seventy short cartoons in the Terrytoons series",Mighty mouse,Science & Nature,What Is Blader Wrack? ,Seaweed  +General,What bird uses its tongue to spear food,Woodpecker,Sports & Leisure,In Metres How Long Is An Olympic Size Swimming Pool ,50 Metres ,General,Yosemite Park is in which American state,California +General,20% of Japanese publications are what,Comic Books,General,Norse mythology Asgard was home of the Gods what's Midgard,Earth,General,"German scientist, who is generally regarded as the founder of the science of mineralogy",Agricola +General,Which bird is sometimes known as Peewit?,Lapwing,Science & Nature,Which is the only planet that rotates clockwise?,Venus,General,Who was the first Grand Prix driver to used a safety belt in 1967,Jackie Stuart +General,Where was a Soviet Republic formed in 1919 which lasted only 4 weeks,Bavaria,General,How many people were killed in the battle of lexington,Eight,General,The pop act '2 Unlimited' were from which country?,Netherlands +General,Blitz Boondock Bristol Scrunge Squop terms in what game,Tiddlywinks,General,What surrey town is famed for its salts,Epsom,General,Who wrote the words that are engraved on the Statue of Liberty,Emma lazarus +General,A vampires favourite place to sleep (and it's portable!),Coffin,General,What does the syrinx help a bird to do,Sing,General,Animal's body that the mythical griffin has,Lion +General,Where did the incas live,Peru,General,What is the chemical symbol for tungsten,W,General,The word rodent comes from the latin word 'rodere' meaning what,To gnaw +General,What Ceased To Be Legal At Midnight On December 31st 1960,The Farthing,General,"Pride, Avarice, Envy, Gluttony, Sloth, Lust what's missing",Wrath, History & Holidays,He is said to have fiddled while Rome burned.,Nero +Geography,What is the capital of Botswana,Gaborone,General,What colour was Mrs Bates dress in Psycho,Periwinkle Blue, History & Holidays,Which Series Of Films Features A Habitual Killer Called Michael Myers ,Halloween  +General,What's the location for newhart,Vermont,General,What fictional planet orbited the red star Negus 12,Superman's Krypton, History & Holidays,Elvis Presley Died On August 16th 1977 But Where EXACTLY Was He When He Died ,On The Toilet  +General,Whose head was given to Herod's wife on a plate,John the baptist,General,Who composed Peter and the Wolf in 1936 (both names),Sergai Prokofiev,General,"Who, or what, is Petrushka in Stravinsky's ballet of the same name",A puppet +General,Hang on Sloopy' was the official rock song of which band,Ohio,General,Which Australian was the only man to win the tennis Grand Slam twice,Rod laver,Geography,What Central American country's name means 'many fish'? ,Panama  +General,On which island is it a criminal offence to shout 'ship ahoy' when there is no ship in sight,Picarn island,General,What magazine was the first to be distributed widely through grocery stores,Family circle,General,Which Austrian president was engulfed in a storm over his Nazi past in 1988,Kurt waldheim +General,What domesticated pet is never mentioned in the Bible,Cats,Science & Nature,20% of what is in the metal part at the end of a pencil?,Sulphur,General,In Israel on Saturday its illegal to do what,Pick Your Nose + History & Holidays,"What country produced the world's first Christmas postage stamp, and when? ",Canada ,Science & Nature,What Type Of Triangle Has 2 Sides Of Equal Length ,An Isosceles Triangle ,General,Which ethnic group of people lived in the biblical city of Nineveh before it was sacked in 612 B.C.,Assyrians +General,What product changed its original name from the soundabout,The walkman,General,Lepcha people Tibet consider it proper to pay teachers in what,Alcohol is acceptable,General,What plant has been used to relieve migraines,Feverfew + Geography,"What city was the setting for ""Gone With the Wind""?",Atlanta,General,"Albert Einstein couldn't talk properly until he was nine, and was thought to be suffering from ______",Dyslexia,General,Paraskavedekatriaphobia is the fear of,Friday the 13th +General,Who was the U.S. president at the time of the 'Wall Street Crash',Hoover,General,What is Christmas Disease,Mild Haemophilia,General,Conifer with dark foliage,Cypress +General,What does a kymograph measure,Oscillations,General,"In mythology, which maiden was saved from a sea by Perseus",Andromeda,General,What is kaolin,Pure china clay +Food & Drink,What Percentage Of Total Production Does The Arabic Coffee Bean Represent ,70% ,General,"When angered, the ears of what animal turn a pinkish red",Tazmanian devil,General,"According to folklore, how does santa get into houses on christmas eve",Down the chimney +General,What is the fear of stooping known as,Kyphophobia,General,What two word term is considered the lowest possible temperature?,Absolute zero,General,How did Bobby Beach - broke all bones over Niagara in barrel die,Slipped Banana skin +General,Where was Oceanus Hopkins born in 1620,On the Mayflower, History & Holidays,How in the world of music is Richard Starkey more commonly known? ,Ringo Starr ,General,What was margaret thatcher's nickname,Iron lady +General,Shirley Bassey sang three Bond themes - which 3 films,"Goldfinger, Diamonds are Forever, Moonraker",General,What was the name of Russian bear mascot 1980 Olympics,Mischa,General,What is graphically illustrated in a karnaugh diagram,Logic +Sports & Leisure,How Many Players Are There In A Netball Team ,7 ,General,"The Cassegranian, Gregorian and Schmidt are types of what",Telescopes,General,What is the flower that stands for: reward of merit,Bay wreath +General,What desert covers most of southern Mongolia,The gobi desert gobi desert,General,What is the only breed of dog that gets gout,Dalmatian,Science & Nature,What Part Of Its Body Does A ButterFly Use To Taste ,It's Feet  +General,What name is given to a marriage in which the wife does not acquire her husband's rank and the offspring do not inherit the title,Morganatic,General,Chitlins are made from what part of the pig,Intestines,General,What is the Capital of: Kuwait,Kuwait +General,What are fawn or pale brown cows called,Jersey cows,General,What is the name for the outer part of a citrus fruit,Zest,Geography,"On which river is London, England",Thames +General,What originates from the dalmatian coast,Dalmatians,General,"What reggae great sang ""get up, stand up, burnin' and lootin'",Bob marley,General,"What are Muharram, Rajab and Safar",Months in the muslim calendar +General,Which year were the 'Jesse Owens' olympic games,1936,General,Whose hamburger patties weigh 1.6 oz,Mcdonald's, History & Holidays,"Nikolai Caucescau, was executed on Christmas Day 1989, in which former Communist country was he President ",Romania  +Sports & Leisure,Who Sobbed On The Duchess Of Kent's Shoulder When She Lost Her Wimbledon Final To Steffi Graf? ,Jana Novotna ,General,What system do the blind use for reading,Braille,General,Who was the first black entertainer to win an Emmy award,Harry Bellefonte +General,When was the tuberculosis bacterium discovered,1932,General,What german philosopher claimed morality required a belief in god and freedom,Immanuel kant,General,Who sang 'beat it',Michael jackson +General,Which country was invaded by Soviet troops in December 1979,Afghanistan,General,What is the speech at the beginning of a play called,Prologue,Art & Literature,In Which Novels Does Bilbo Baggins Appear ,The Hobbit/ Lord Of The Rings  +Sports & Leisure,In which city is the Cotton Bowl played,Dallas,General,Who was Dr Zhivago’s love,Lara,General,This instrument measures atmospheric pressure.,Barometer +General,Who was assassinated in Mexico in 1940,Trotsky,General,"What is the life span of a housefly two weeks, two months or two years",Two weeks,Music,"Which 1960's Band Was Formed By Eric Clapton, Ginger Baker And Jack Bruce?",Cream +Entertainment,In 1975 Jack Nicholson won the best actor Oscar for his role in this film.,One Flew Over the Cuckoo's Nest,Music,From What Country Did The Band Black Box Originate,Italy,General,Sherlock Holmes lived in Baker St - What other Detective did,Sexton Blake 1893 +General,What did dr david banner become when he got angry,Incredible hulk,General,What Is The One-Humped Type Of Camel Called,A Dromedary,Music,Name Nat King Coles Hit Making Daughter,Natalie Cole +Music,How Many No.1 Singles Did Stevie Wonder Have In The 60's,None,General,Who was the first head of an arab nation to make peace with israel?,Anwar sadat,General,"What links Willie Brant, Lech Walesa, Yasser Arafat",Nobel Peace Prize +General,What is the Roman numerals for 3000,MMM,General,There Are Now More Billionaires In This Capital City (As Of 2006) Can You Name It,Moscow,Music,"Who Had 2002 Hits With ""Compilcated"" And ""Skater Boy""?",Avril Lavigne +Geography,On Which Line Of Latitude Could You Sail Around The World Without Touching Land? ,60 Degrees South ,General,"At which Air Show in 1973 did Russia's supersonic ""Konkordski"" aircraft crash",Paris,General,US civil war which states buttons had a Palmetto tree on them,South Carolina +Music,"The Bonzo Dog Band's Only Hit Single ""Im The Urban Spaceman"" Was Produced By Apollo C Vermouth, Who Was Hiding Behind This Pseudonym",Paul McCartney,General,What animal does the adjective 'meline' refer to,Badger,General,Kate winslet in which film did leonardo dicaprio and kate winslet play 'jack' and 'rose',Titanic +General,"What are a Galliard, Sarabande, Morisca and Courente",Dance Types,Entertainment,What was the last movie of the late Brandon Lee?,The Crow,General,Which famous battle was fought this day In 490 BC,Marathon +Entertainment,"Name the band - songs include ""Get Down & Get With It, Mama We're All Crazy Now""?",Slade,General,Who was supposed to play Betelgeuse in the movie,Sammy Davis Junior,General,Who developed the first nuclear submarine,Soviet union +General,"Hares, cats, toads, newts, owls, ferrets all accused of what",Being witches familiars,General,What mammal moves so slowly that green algae can grow undisturbed on it's fur,Sloth,General,To pick the best from a group,Cherry-pick +General,Who had the part of Dirty Harry - hurt hand - dropped out,Frank Sinatra,People & Places,Who Was Maradona Playing For When He Was First Caught Taking Cocaine ,Napoli ,General,In What Country Will You Find The Largest Active Volcano In The World,USA (Hawaii) +Music,Which Former Punk Maestro Recorded An Album Of Pop Opera Songs With The Bootzilla Orchestra,Malcolm McLaren,General,Dhaka is the capital of ______,Bangladesh,General,The marimba is a African form of what musical instrument,Xylophone +General,"What is the narrow, inland sea, separating the Arabian peninsula, western Asia, from northeastern Africa?",Red sea,General,"Circular bands used to decorate ears, toes, noses, or, most often, fingers",Rings,General,What tourist attraction in rome has 138 steps,Spanish steps +Science & Nature,What Is The Lightest Solid Element ,Lithium , History & Holidays,Which 20th century American icons died on Halloween ,River Phoenix ,General,What is the call name for Soviet mission control,Zarya +General,"Who played the mermaid Madison in the film ""Splash""",Darryl hannah,General,"Who wrote ""The Learning Tree""",Gordon parks jr,General,Nevada and Canberra are varieties of which vegetable,Cauliflower +General,"Which Chemical Element Get's It's Name From The Greek For ""Violent""",Iodine,General,What is the name of the airline that operates the ill-fated flight from LA to Chicago in the movie Airplane!?,Trans American, Language,"What word contains the combination of letters: ""xop""?",SaXOPhone +General,Who played Flint in the spy movie Our Man Flint,James coburn,Technology & Video Games,Who was the founder of Lotus Cars Ltd.?,Colin Chapman, Geography,In which city is Saint Paul's Cathedral?,London + History & Holidays,Which Singer Starred In The Title Role Of The Film 'Merry Christmas Mr Lawrence'' ,David Bowie ,General,"In Homer's Iliad, who was the King of Troy",Priam,General,Whose family name is Zimprsquzzntwlfb,Mr Spock in Star Trek +Sports & Leisure,Sergey Bubka has set a world record in which field event over 30 times? ,Pole Vault ,General,Hudson how many points are awarded to the winning driver of a formula 1 grand prix race,Ten,General,What weapons did the indians use to defeat custer?,Winchester Specials + Geography,What is the basic unit of currency for Fiji ?,Dollar,General,Which is cape town's major choir,Philharmonic choir,General,Where are the most expensive seats at a bullfight,Shade - Sombra +General,What musical instrument did jack benny play,Violin,General,What's the circulation of winds around a low pressure system called,Cyclone,General,In church terms what are saucers,Domes +Science & Nature,What is the chemical symbol for iron?,Fe,Art & Literature,Who Wrote The Novels Slaughterhouse Five And Breakfast Of Champions ,Kurt Vonnegut ,General,Who rode 'Party Politics' to win the Grand National in 1992,Carl llewellyn +Geography,The Airline Danair Is Based Where? ,Denmark ,General,What american indian tribe drank 'tizwin',Apache,Food & Drink,What Is The Name Given To The Flat Unleavened Indian Bread Resembling A Pancake ,Chapati  +Science & Nature,A calm ocean region near the equator.,Doldrum, History & Holidays,Who was the first woman elected to lead a european democracy ,Margaret thatcher ,General,What is an 'islet',Small island +Music,What Was The Name Of The Television Show That Featured Mick Jagger As A Ringmaster,Rock N Roll Circus,General,From which kind of organism is the indicator litmus extracted,Lichens,General,What is the Capital of Costa Rica?,San Jose +Geography,Name the most north_easterly of the 48 contiguous states.,Maine,General,Who wrote the novel The Seventh Scroll,Wilber Smith,Music,Which Beatles song was inspired by a picture drawn by Julian Lennon?,Lucy In The Sky With Diamonds +General,Who were defeated by the Mets in the 1969 world series,The orioles, History & Holidays,What Did The Bank Of England Issue For The First Time In 1791? ,Banknotes ,General,Who was forced by indian troops into the black hole of calcutta,British +General,What colour is worn for funerals in Egypt,Yellow,General,"There are two perennial vegetables, asparagus & ______",Rhubarb,General,Ben Affleck played CT on what 80's science education television show?,Voyage of the Mimi +General,Outfit/costume first seen 1914 film Kid Auto Races at Venice,Chaplin's Tramp outfit,General,Which country has no national monetary unit of it's own,Andorra,Music,What Type Of Guitar Does Hank Marvin Use,A Fender Stratocaster +General,"A ""gyre"" is another term for what shape?",Coil,General,What kind of bird is a capercaillie,Grouse,General,"In 1998, Andrew Flintoff scored 34 runs from an over bowled by which Surrey bowler",Alex tudor +Science & Nature,This is the main food of the blue whale.,Plankton,General,"On a suit of armour, the poleyn would protect which part of the body?",Knee,General,The country name for which bird is the 'ruddock',Robin +Music,Which Singer Was Known As “The Walrus Of Love“,Barry White,General,Capable of being decomposed by the action of light,Photodegradable,General,What is the flower that stands for: age,Guelder rose +Religion & Mythology,What religion was founded by Guru Nanak ?,Sikhism,General,What vegetable is the essential ingredient in borsch,Beetroot,Sports & Leisure,At Which Scottish Golf Venue Are The Eden Course & The Jubilee Course ,St Andrews  +Music,"Who played the electric piano on the Beatles Track ""Get Back""?",Billy Preston,General,Boston Red Sox are The Pilgrims but what were they before,The Somerset's,Religion & Mythology,What is the name given to the supreme reality in Hinduism?,Brahman +General,What character was invented to respond to questions from Gold Medal Flour customers,Betty crocker,Music,"Who Went For ""All Or Nothing"" Reaching No.1 In 1966",The Small Faces,General,What Was The Top Grossing Comedy Film Of 1990,Home Alone +Science & Nature,"Found On Plants , What Is The Protective Bubbly Section Of The Nymphs Of The Spittle Bug Commonly Called ",Cuckoo Spit ,General,The Lipari islands are a group of seven volcanic islands to the northeast of which major island in the Mediterranean Sea,Sicily,General,What island did Balki from Perfect Strangers call home?,Mypos +Music,"Which DJ & Tv Presenter Co-Founded ""Time Out"" Magazine",Bob Harris, Geography,What is the basic unit of currency for South Africa ?,Rand,General,Who played the doctor in the rock opera Tommy,Jack Nicholson +General,Israel has the highest per capital consumption of ______?,Turkey,General,What kind of carpenter's tools come in jig & coping styles,Saws,Science & Nature,Which Bird Brought Back A Twig To Noah To Signal The End Of The Flood ,The Dove  +Music,Which Vocalist Used To Front The Q-Tips,Paul Young,General,Michael Bond created which children's character,Paddington Bear,General,"Who did Davy Crockett beat in a keelboat race, from Disney's perspective",Mike fink +General,Who was the first european to set foot on north america,Leif erikson, History & Holidays,"Which traditional song contains the lines 'He's making a list, He's checking it twice, Gonna find out whose naughty or nice'' ",Santa Claus Is Coming To Town , Geography,Of what are Quemoy and Matsu part?,Taiwan +General,Name the first self contained home computer -,A Commodore Pet,General,This racist organisation was formed in Tennessee in 1865,Ku klux klan,General,Where is the Devil's Tower,Wyoming usa +General,In What Country Did The Rather Prestigious Sport Of Polo Originate,Iran,General,What is the point to which rays of light converge,Focus,Entertainment,Hang On Sloopy' was the official rock song of which band?,Ohio +General,What links Colorado and Wyoming,Rectangles on USA map,Music,"In The World Of Music How Are Kelly, Michelle, Jessica, Tony & Kevin More Commonly Known",Liberty X,General,Who starred in the film The Outlaw Josey Wales,Clint eastwood +General,In which of Coleridge's poems does an albatross appear,Rime of the ancient,General,What are conifers,Cone-bearing trees,General,What colour is the number 10 on 10 Downing street,White +General,"What is the nickname for Boston, Massachusetts",Hub of the universe,General,What is the North American word for aluminium,Aluminum,General,What is unusual about the elements mercury & bromime,Liquid at room temperature +General,How many country's have a life expectancy of over 80 years?,"Four (Andorra, San Marino, Australia, Japan)",Art & Literature,What was Dante's last name?,Alighieri,Music,Juice Newton Was the Queen Of What,Hearts +General,"""The Death Car of Sarajevo"", killed 15 people over 12 years, including him.",Archduke ferdinand,Music,"Who Did Michael Jackson Team Up With For The Hit ""The Girl Is Mine""",Paul McCartney,General,"Of sin, cos or tan, which graph is not differentiable at all points",Tan +Music,Which James Bond Theme Was A Hit For Sheena Easton,For Your Eyes Only,General,"What in Queensland Australia , is the worlds longest at 3,450 miles",Fence,Science & Nature, A __________ consumes about 33 percent of its body weight in a single meal.,Pelican +Music,Seven Seas Of Rhye Was The First Hit For Which Rock Band,Queen,General,Which is the largest Scandinavian country,Sweden,General,What type of creature is a niffe?,Fish +General," An ""omniscient"" person has unlimited __________.",Knowledge,Music,"Which Singer / Songwriter, Appeared In The Movie, The Player, Where He Met His Wife To Be Julia Roberts",Lyle Lovett,General,What nationality was teiichi igarashi,Japanese +General,Eva Shain was the first woman to do what,Judge pro heavy boxing Match,General,Fragrant Harbour is the translation of which cities name,Hong Kong, History & Holidays,Which Was The Only Song To Spend 6 Weeks As a UK No.1 In 1979 It Also Featured In A 1978 Movie ,Bright Eyes  +General,What is the most common food allergen,Nuts,Entertainment,Randy Travis said his love was 'deeper than the ______'?,Holler,General,Fahrenheit as what is minus forty degrees fahrenheit the same,Minus forty degrees +General,Which Russian (1880-1942) choreographed the dying swan for,Pavlova kokine,General,"What is the range, in miles, of an Aim-7 Sparrow?",28,General,Pavarti or Uma is the wife of who in Hindu religion,Shiva +General,Scrutinise Swirl Sniff Sip - what are you doing,Wine Tasting,Music,"According to the 1984 Bananarama Song, who was waiting",Robert De Niro,General,What are white dwarfs and red giants,Stars + History & Holidays,What period is also known as the age of fish?,Devonian period,General,What was the name of the character played by Marilyn Monroe in the film Some Like It Hot,Sugar kane,General,The linden tree is also called what,Lime tree + History & Holidays,Which famous Giant Panda died at London Zoo in 1972? ,Chi Chi ,General,What was the name of Dr Doolittle's pet duck,Dab dab,General,The opera Peter Grimes was written by whom,Benjamin britten +General,"""Oliver's Story"" was the sequel to which best-seller by Erich Segal",Love story,General,William Joyce US born of Irish descent famous as who in WW2,Lord Haw-Haw – executed 1946,General,In what film - Charlie Chaplain have his first speaking part 1940,The Great Dictator – Adenoid Hinkel +General,Purina how often do chimpanzees build new sleeping nests,Nightly,General,Who built the worlds first film studio,Thomas Alva Edison,Science & Nature, Gophers are __________,Hermits + History & Holidays,How Many Times Was Queen Elizabeth I Married ,None , History & Holidays,The Inquisition forced him to recant his belief in the Copernican Theory.,Galileo,General,Rock groups: dion and the _____,Belmonts +Music,Who Gave Sid Vicious His Name,Johnny Rotten,General,Microbiophobia is a fear of ______,Microbes,Music,What Instruments Were Duelling On The Deliverance Film Soundtrack,Banjos +General,Coffee made under steam pressure,Espresso,General,In Kiplings poem Gunga Din what job had Gunga Din,Water Carrier,General,Which country is known as the roof of the world,Tibet +Entertainment,"Who recorded the 1969 hit ""Space Oddity""?",David Bowie,General,How many arondissements make up the city of Paris?,20, Geography,Raleigh is the capital of ______?,North Carolina +General,Sexophobia is the fear of,The opposite sex,General,The property of matter that causes it to resist any change of its motion in either direction or speed,Inertia,General,With what acid do nettles cause irritation?,Formic acid +General,What is the only head bone that a normal human can move,Jawbone, History & Holidays,What was the first game show on Mtv? ,Remote Control ,General,In Newport Rhode Island its illegal to do what after dark,Smoke a pipe +General,The horned dinosaur Torosaurus had the biggest what on land,Head - Skull nine feet long,General,From Memphis restaurants its illegal to take what away,Any Pie,General,Where was cornflakes invented,Battle creek sanitarium +General,Balsa wood gets its name from Spanish what's literal meaning,Raft,General,In a 1988 survey 12 million Americans don’t know what,Washington DC was capitol,General,What is an integer that is greater than 1 and divisible only by itself and 1,Prime number +General,To Those Of Us That Are Not Scientists How Is The Chemical Symbol “ H202 ” Better Known?,Hydrogen Peroxide,General,Brass instrument resembling a trumpet,Cornet,General,The United Nations in New York were originally where,San Francisco +General,BaseBall: The Chicago ____,Cubs, History & Holidays,Which is the oldest University in the USA? ,"Harvard (founded 1636, in Cambridge Massachusetts) ",Entertainment,Who was the alter ego of 'The Incredible Hulk'?,Dr. David Banner +General,Which building commemorates the great fire of london,Monument,General,A sun-dried grape is known as a(n) ______.,Raisin,Sports & Leisure,What year did Red Rum first win the Grand National ,1973  +General,"What is an illness caused, or made worse by mental factors",Psychosomatic,General,Who was the first person to win the Indianapolis 500 four times,Aj foyt,General,Who did Hercules persuade to go and get the Golden Apples for him,Atlas + History & Holidays,Who in 1893 defined vegtables as plants eaten in a meal and fruits as plants eaten as dessert ?,United States Supreme Court,General,What instrument does phil lynott of thin lizzy play,Guitar,General,What is Gluhwein,Mulled wine +Music,In Which Decade Did Rod Stewart Have His Last No.1,"The 80's, 1983 (Baby Jane)",General,Who wrote 'The Sun Also Rises',Ernest hemingway,General,Harold H Hilton only Englishman to do what in golf in 1911,Won UK and US open same year +General,Who was fred flintstone's best friend,Barney rubble,General,Wax like substance from the sperm whale used in perfumes,Ambergris,Sports & Leisure,In 1977 Which Horse was Guest Of Honour At The Opening Of The Steeplechase Ride At Blackpoll Pleasure Beach? ,Red Rum  +General,What Was The Name Of The UK's Best Selling Album Of 2005,Back To Bedlam (James Blunt),General,Novices are called tumblers experienced shiners what job,Window Cleaners,General,The old French Royal family - Boy Scouts share what symbol,Fleur-de-lis +General,"Twenty-five years after first playing James Bond, Sean Connery won an Oscar. For his part in which film",The untouchables,General,Which novelist died of typhoid after drinking water in Paris,Arnold bennett,General,"Fredcrick the First, 1657 to 1713, was the first king of which country",Prussia +General,"What do stock market vets call october 19, 1987",Black monday,Food & Drink,What berries give gin its flavour?,Juniper berries,General,James Bond flew Little Nellie in which film,You Only Live Twice +General,John Lilburne was a leader of which group,Levellers, History & Holidays,"Name the female comedy star who once had a show on Fox, that had a pop hit in the early eighties ",Tracy Ullman ,Music,"What Is The Connection Between Gina G, Bucks Fizz,",The Eurovision Song Contest +General,What is the moons astronomical name,Moon,General,"According to John Aubrey's Brief Lives , what card game did the English poet, Sir John Suckling, invent in 1630",Cribbage,General,An Aria from a Handel opera is Ombra mai fu - what other name,Largo +Sports & Leisure,What Do You Call A Boxer Who Leads With His Right ,A South Paw ,General,Who has won the most Oscars,Walt Disney,General,In what game would you use a squidger,Tiddlywinks – Big disc + History & Holidays,Which 1945 Ealing Film Is A Sequence Of Supernatural Stories Told By A Group Of People In A Country House ,Dead Of Night ,General,In Happy Days name Fonzies dog,Spunky,Music,Which Singer Kicked Of Their 3rd World Tour Entitled “Blonde Ambition” In 1989,Madonna +General,What is the square root of 144,12,General,This city is known as the 'Venice of the orient',Osaka,General,What is the number of the beast,666 +Food & Drink,What are the two main ingredients of cock-a-leekie soup? ,Boiling fowl and leeks. ,Music,What 80's band included members from Bad Company and Led Zeppelin?,The Firm,General,What is the chronicle of the nazi siege of leningrad,The 900 days +People & Places,Which Supermodel Is Nicknamed 'The Body'? ,Elle McPherson ,General,"In the famous scene from Ghost, where Patrick Swayze and Demi Moore are sculpting clay, what is the song playing in the background ",Unchained Melody ,General,Coca-cola was named for the extracts of which of its two 'medicinal' ingredients,Coca leaves and kola nuts +Music,"Did Sheena Easton Like Travelling By ""Fast Car"" Or ""Early Morning Train""",Early Morning Train,General,What is the term for a small umbrella used to protect a person from the sun,Parasol,Sports & Leisure,"Which sporting event starts at Putney, finishes at Mortlake and covers a distance of just 4 miles? ",The Boat Race  +Entertainment,Who directed the film 'Ordinary People'?,Robert Redford,General,What TV character lived in Waratah National Park,Skippy,Science & Nature,By what process is rock worn down by the weather?,Erosion +General,A punishment by caning on the soles of the feet,Bastinado,General,What is the world's leading importer of iron ore,Japan,Music,Who Had A Hit With Bright Eyes,"""Simon & Garfunkel""" +General,Lisbon lies on which river,Tagus,Art & Literature,Which is the only book written by Margaret Mitchell?,Gone With The Wind,General,What name is given to fish cooked in browned butter,Meuniere + History & Holidays,What was the contribution of actress Mercedes Mccambridge to Linda Blairs performance in The Exorcist ,She provided the devils voice ,General,"What is the nickname for Birmingham, Alabama",Pittsburg of the south,General,"What name is given to the 1913 show of artists' work in New York, which is often regarded as the beginning of public interest in progressive art in the U.S.A.",The armory show +General,What's the name of Blur's frontman?,Damon Albarn, Geography,What is the basic unit of currency for Iraq ?,Dinar,Music,"In The 1980's Which Actor Became Motown Records ""Biggest Selling White Artist Ever""",Bruce Willis +General,"In the movie 'happy gilmore', who is happy's partner",Bob barker,General,What U.S. state boasts the world's largest mass of exposed granite,Georgia,General,Who played Jo's sailor boyfiend on the Facts of Life?,Robby Benson +General,In Yugoslavian Belgrade is called Beograd what does it mean,White City,Music,How Old Was The Dancing Queen According To The Lyrics Of The Abba Song Of The Same Name,17,General,"Annapolis & Minneapolis contain the suffix ""polis"", which in Greek means ____",City +General,Who or what was Rosanna Arquette seeking in 1985,Susan,Food & Drink,Vermicelli literally means ___________.,Little worms,General,What does a tailor do with his plonker,Press suits + History & Holidays,"Which city, famous for its 'Christmas Market', is also Known as 'The Gingerbread Capital of the World'? (Geneva, Bucharest,Nurumberg,Prague) ",Nuremburg ,People & Places,Where is Sir Herbert Baker buried?,Westminster Abbey,General,In the human body what is produced by the parotid glands,Saliva +Music,"Who Had A Hit With Costello's ""Girls Talk""",Dave Edmunds,General,From what did the ghan railway get its name,Camel drivers, Geography,The Volta is the largest river in which country?,Ghana +General,What was Al Capone's favorite bullet proof car,Cadillac,Art & Literature,"Principally , Of What Nationality Were The Impressionist Painters ",French ,General,What does the girls name Irene mean,Peace - Greek +General,From which large South American country do the beers 'Brahma Chopp' and 'Antarctica' come,Brazil,Geography,What is the state capital of Louisiana? ,Baton Rouge ,Geography,"The majestic ______________, popular honeymoon site for newlyweds located in both New York Ontario, was named after the Mohawk Indian word meaning ""thunder of waters"".",Niagara falls + History & Holidays,Which City Was Engulfed In Lava From Mount Vesuvius In AD79? ,Pompeii ,Science & Nature,Of What Is Ethylene Glycol The Main Ingredient ,Anti Freeze ,Music,"Who Had A Hit In 1982 With ""Hot In The City""",Billy Idol +General,Pat Sullivan created which cartoon character,Felix the cat,General,Name the Greek equivalent of the Roman god Saturn,Cronos,Food & Drink,"Unlike other oranges, what does a navel orange not have?",Seeds +General,"Buddy holly released a solo 'peggy sue' that, by the end of 1957 was challenging which song recorded with the crickets",Oh boy,General,I love lucy: what was ricky's profession,Band leader, History & Holidays,What year was the great drought in Britain? ,1976  +General,What type of creatures belong to the order Chelonia,Turtle terrapin tortoise,General,What name is given to a settlement which is clustered around a central point ?,Nucleated,Science & Nature,How Long Does The Average Hair On Your Head Grow In A Single Year ,12cm (4.75in)  +Entertainment,Who played the title role in the 1978 version of 'Superman'?,Christopher Reeve,General,Legendary Jamaican sprinter Merlenle Ottey started competing for which country in 2002?,Slovenia,General,In 'star trek' jean ______ picard,Luc +General,What does an abecedarian study,Alphabet,General,"Black, whooper and Berwick all varieties of what",Swan, History & Holidays,What was the challanging method of catching a fly in Karate Kid? ,Using chopsticks  +General,"A stream may disappear down a sink-hole, by what other name is this known",Swallow hole,General,Romans used a sharp pointed stick to drive cattle Modern word,Stimulus,Music,"A Taste of Honey's ""Boogie Oogie Oogie"" was a major hit in which year?",1978 + History & Holidays,What was the name of the scandal that resulted in the resignation of president Nixon?,Watergate, History & Holidays,Who was the first (and last) catholic president?,John Fitzerald Kennedy,Geography,What is the capital of New Zealand,Wellington +General,Who died on Saint Helena?,Napoleon,General,How many times did Fred Archer win the English Derby,Five, History & Holidays,What Was The Duchess Of Windsor's Name Before She Married The Duke? ,Wallace Simpson  +General,Name the most downloaded cyberpet over 14 million,MOPy fish screensaver,General,Who was the first computer millionaire,Herman Hollerith,Music,"Who Had A Hit With ""A Rose Has To Die"" In 1978",The Dooleys +General,"Who rerecorded the old classic ""Respect Yourself"" in 1987",Bruce willis,Science & Nature,What Is The Nearest Star To Earth? ,The Sun ,General,"When someone is a hanger on or serves no purpose, he is called this",Fifth wheel +General,If a chemical is 'anhydrous' what does it not contain,Water,Food & Drink,"Baked Alaska has meringue on the outside , what does the meringue cover? ",Ice cream ,General,Which two letter word is the most sacred in Hinduism,Om +Sports & Leisure,Where Might You Come Across A Chicane ,On A Motor Race Track ,General,What is the name for a person who you correspond with regularly,Penpal,General,In the 13th century European children were baptised with what,Beer +General,"Burt reynolds was a gunsmoke co-star for many years, playing the role of",Blacksmith quint asper quint blacksmith,General,Mimi is the first name of which Warner Bros cartoon character,Roadrunner,Entertainment,Which singer is a former school teacher?,Sheryl Crow +Sports & Leisure,What Does The Term (Kung Fu) Mean ,Leisure Time ,General,Women do it 4 times more often than men - do what,Shoplift,General,How did Mark Chapman shock the world,Shot john lennon +Music,During the 1990’s who recorded the Albums 'Out of Time' & 'New Adventures in Hi Fi'?,REM, History & Holidays,"Another plane crash, on February 3rd 1959, resulted in the deaths of which three rock stars of the day ","Buddy Holly, Big Bopper, Richie Valens ",General,"These bacteria spores that I love, commonly grown in feaces",Mushroom +Entertainment,What was Marilyn Monroe's given name at birth?,Norma Jean Mortenson,Music,What Was Connor Reeves First Single,My Fathers Son,General,"How was ""Abu Yusuf Ya'qub ibn Is-haq ubn as-Sabbah ibn 'omran ibn Ismail al-Kindi"" better known ?",The philosopher of the Arabs +Science & Nature,In Which Year Did The Volkswagen Beetle Make Its First Appearance ,1945 ,Music,With What Song Did Abba Win The Eurovision Song Contest In 1974,Waterloo,General,Who is Al Gores running mate in the 2000 Presidential election campaign,Joe liebermann +General,Why would a train spotter want to see number 4468,The Mallard – record steam train,General,Ann Ziegler was the singing partner of which film star,Webster Booth,General,Which organist who died in 1985 was traditionaly associated with Blackpool Tower?,Reg Dixon +General,Which famous mystery writer created a mystery by disappearing in 1926,Agatha christie,General,What is the Capital of: Saint Helena,Jamestown,Music,Which 1970 Album Cover Featured A Former Beatle And His New Born Child,McCartney / By Paul McCartney +General,Pok ta Pok started in Mexico - what modern game/sport is it,Basketball,Geography,Which City Is The Capital Of Canada ,Ottawa ,General,What direction do many northern birds fly when autumn arrives,South +General,"Which Famous Duo Lived At ""Hawthorn Hill, Dayton, Ohio""",The Wright Bros,General,In 1000 bc Israelites paid their taxes in what,Raisins,General,What is the paint remover that is made from pine resin,Turpentine +Language,Which two fruits are an anagram of each other?,Lemon and melon,General,What menacing character was best friends with Tommy Anderson,Dennis the,General,What is the official national sport of Bulgaria,Weigthlifting +General,The stress in Hungarian words always falls on what syllable,First,General,Can you name all of the Bradford's on Eight is Enough?,"Tom,Abbey,David,Mary,Joni,Susan,Nancy,Tommy,Elizabeth,Nicholas", History & Holidays,He was defeated at the Battle of Little Bighorn.,General custer +Science & Nature," There are close to 4,000 known species of frogs, including __________",Toads,General,Where was the first police force established in 1667,Paris,General,If you have otophobia what are you afraid of,Opening ones eyes +General,Who was the little gentleman in velvet - death William III,A Mole,General,The penny red was the first postage stamp to have what,Perforations,Music,"""If I Could Turn Back Time"" Was A Hit for Which One Named Artist",Cher +General,What kind of creature was Sam on the Muppet Show,Eagle,General,"Which British statesman, Minister of Labour in the National Government (1940-1945) became Foreign Secretary in Attlee's Cabinet (1945-1951)",Ernest bevin,General,Who directed the film Rain Man,Barry levinson +General,Which stretch of water seperates Tierra del Fuego from the rest of South America,Strait of magellan,General,"In Greek mythology, who visited leda in the form of a swan",Zeus,General,What is the name of the cranial bone just above your ear,Temporal + History & Holidays,1954 again - in which English city did Roger Bannister become the first man to run a mile in under 4 minutes ,Oxford (Iffley Rd) ,General,When did the new york mets win their first world series,1969,General,What is the meaning of the Sioux word Tonka - used for toys,Great + History & Holidays,What Do 99% Of Pumpkins End Up As ,Lanterns ,General,A dog is canine - what animal is ovine,Sheep,General,Where is Dronning Maud land to be found,Antarctica +General,What is tattooed on the Marlboro mans hand,An Eagle,Music,What Do Boyzone & Bewitched Have In Common,Both Irish,General,Name the first teddy bear in space,Mishka 1980 Olympic mascot +General,"What entertainer's nickname is ""the thief of bad gags""",Milton berle,General,The Merryman and his Maid alt title what G&S operetta,The Yeomen of the Guard,General,Which Was The First Ever Bond Movie?,Dr No +General,What is the largest volcano,Cotopaxi,Music,Which Boyband Released An Album Entitled “Walthamstow” In 1992,East 17,Music,"Whose 1982 Debut Album Included The Classic Song ""Sex Dwarf""",Soft Cell +General,What two body organs benefit from cardiovascular exercise?,Heart & lungs,Science & Nature,"This alkaloid extracted from chincona bark, ______ is commonly used in malaria therapy.",Quinine,Music,Name Two Of The 3 Top 10 Hits For ABC In 1982,"Poison Arrow, Look Of Love, All Of My Heart" +General,To a golfer what's a frosty,Score of 8 on a hole,General,What occupation would use a dibber,Gardener - to make planting holes,Entertainment,Who was Carl in Five Easy Pieces before going to Walton's Mountain?,Waite +General,Snakes are reptiles. what are frogs,Amphibians,Science & Nature,What Type Of Hook & Eye Fastening Was Invented By Swiss Engineer Georges De Mestral In 1948 & Introduced Commercially In 1958 ,Velcro ,Music,"In The 1960's who Released The Albums ""Twist"", ""Twistin Round The World"", ""Your Twist Party""",Chubby Checker +General,Which book of the Bible tells of Goliath's slaying by David,Samuel,General,Who were the two commanders who directed the forces in the battle of el alamein,Montgomery and rommel,General,Which Olympics did the U.S. boycott?,980 +General,Who invented the game of Bingo?,Edwin Lowe,General,What was Vincent Price's first horror film,House of Wax,General,"The Single ""Respect Yourself"" Was Released In 1987 By Which Die Hard Music Lover",Bruce Willis +General,Who directed the 1946 'It's A Wonderful Life',Frank capra,General,Who first appeared in the film A Tale of two Kitties in 1942,Tweety Pie,Sports & Leisure,He holds the NHL record for the most goals scored during the regular season.,Wayne Gretzky +General,What Was The First TV Show To Feature A Lesbian Kiss,LA Law,General,Who wrote '1984',George orwell,General,Who was the youngest of the anderson children in father knows best,Kathy +General,Who was Secretary General of the UN from 1962 - 1971,U thant,General,Theophilus Van Kannal invented what in 1888 in Philadelphia,Revolving Door,General,Rio's Maracarria stadium has what unusual feature,A Moat +General,William Kemmlar in 1890 was the worlds first what,Executed by electric chair,General,What was the name of the shepherd that got Tremponina Pallidium,Syphilus,Music,To Which City Did Dione Warwick Ask The Way In 1968,San Jose +General,What colour is the 'Cookie Monster' from the programme Sesame Street,Blue,General,"The Egyptian hieroglyph for 100,000 is what",Tadpole,General,What makes plants green,Chlorophyll +General,"Billycock, Wideawake, Gibus and Mitre all types of what",Hat,General,This county has the lowest point in South America,Argentina,General,Isms: a severe or unfavorable judgment,Criticism +General,What state fought more battles in the revoltionary war than any other,South,General,What is the most common fear people have,Public Speaking,General," Throat, foxing, and platform are parts of a(n) ________.",Shoe +Music,Who Composed The Enigma Variations,Sir Edgar Elgar,General,Which theatre styles name translates as skills or talents,Noh,General,"Who always ended her PBS cooking shows with ""Bon Appetit""",Julia Childs +General,Who Was President Of The USA When The Decision Was Made To Declare War On Germany In The First World War,Woodrow Wilson,General,Manner of walking,Gait,General,Zymurgy is a branch of chemistry concerning what process,Brewing fermentation + Geography,Lilongwe is the capital of ______?,Malawi,General,"Fleshy fruit of trailing cucumber like plant, often dried",Gourd,Science & Nature,What is the chihuahua named after?,A Mexican state +General,"Which Us State Capital Is Served By ""Logan"" International Airport",Boston,Science & Nature,What is the meaning of the name of the constellation Corona Borealis ?,Northern Crown,General,What was the name of the ship on which Dracula reputedly arrived in England in 1897?,Demeter +General,What is a group of this animal called: Turtle,Bale,General,"A typhoon struck which island in Japan in 1934, killing 4,000 people",Honshu,General,Which animals meatphorically constitute a heavy rainstorm,Cats and dogs +General,What's the favorite food of dragonflies,Mosquitoes,Science & Nature,What element is lacking in a diet when goitre occurs?,Iodine,General,If You Suffered From Selaphobia What Would You Be Afraid Of,Flashing Lights +General,What was the SR-NI which made its first English Channel crossing on 25th July 1959,Hovercraft, Geography,What is the basic unit of currency for Lesotho ?,Loti,General,"British chemist, who isolated and described several gases, including oxygen, and who is considered one of the founders of modern chemistry because of his contributions to experimentation",Joseph priestley +Science & Nature,Which Hi-Tech Innovation Is Celebrating Its 25 th Anniversary In 2007 ,The Compact Disk / Player ,Religion & Mythology,"What is the name of the most famous of the rivers in the Underworld, the river of 'Hate' which dead souls must cross over?",Styx,Music,"Which Group Spent Five Weeks At No.1 With ""Chirpy Chirpy Cheep Cheep""",Middle Of The Road +General,There are butterflies that smell like what,Chocolate,General,In 1984 BA stewardess called police she'd left what in cupboard,Husband in Bondage,General,"The Bible: ""The Prodigal Son"". What does ""prodigal"" mean",Wasteful lavish +Music,"Who Sung The Original Version Of ""It's In His Kiss""",Betty everett,General,"In which European city is Shakespeare's play, 'Measure for Measure' set?",Vienna,General,"Who's Autobiography Is Entitled "" Memoirs Of An Unfit Mother""?",Anne Robinson +General,What is a loss of memory,Amnesia,General,An elephant is called a pachyderm what does it literally mean,Thick Skinned,People & Places,Who Is Actress Betty Joan Perske Better Known As ,Lauren Bacall  +General,Nebkheperura was his first name what do we call him today,Tutankamen,General,Anna Mary Robinson - famous American painter - what name,Grandma Moses,General,The film 10 Rillington Place is based on which British serial killer,Reginald Christie +General,What is the tallest island,New guinea, Geography,Which island country lies to the West of Australia?,Mauritius,General,What USA city is also a slang name for a pineapple,Chicago +General,What is a Major Mitchell,Australian Cockatoo,General,Who ordered the building of the Tower of London,William the Conqueror,General,In 1813 Rubber is,Patented +General,Who is the dog on the crackerjack box,Bingo,General,"Who were the fabled twins, raised by a wolf that supposedly founded Rome",Romulus & remus,Sports & Leisure,Who died in 1995 and was the last Brit to win the Wimbledon Mens Singles Title ,Fred Perry  +General,How many rooms are there in buckingham palace,602,Art & Literature,What Was Sherlock Holmes' 7% solution in 'The Sign of Four'?,Cocaine,General,Which animals can live longest without water,Rats +Music,Who Found His Thrill On A Hill In 1956,Fats Domino / Blueberry Hill,General,What is a water taxi known as in Venice?,Gondola,General,What two materials were the first aeroplance wings made of,Cloth & wood +General,"After the Second World War, in what year did clothes rationing end",1949,General,Through what were dead egyptian pharaohs' brains extracted,Nasal passages,General,Sacred carvings is the literal translation of what word,Hieroglyphics +General,Which is the worlds tallest grass,Bamboo,General,How many hours are there in a week,168,Science & Nature,Which element has the chemical symbol U? ,Uranium  +General,"What's the composition of ""dry ice""",Carbon dioxide,Geography,What country produces the orginal edam cheese ,The netherlands ,General,Tocophobia is the fear of what?,Childbirth +General,"The study of how nerve cells, or neurons, receive & transmit information",Neurophysiology,General,Name the three main bands featured on the cartoon series 'Jem' during it's entire 3 year run.,"Jem and The Holograms,The Misfits,The Stingers",General,What is the pupa of a moth or butterfly in a cocoon called,Chrysalis +General,FINA is the governing body of what amateur sport,Swimming,General,Which science studies weather?,Meteorology,General,"What follows beta, gamma and delta",Epsilon +Art & Literature,"An abstract movement in Europe and the United States, begun in the mid-1950s, based on the effect of optical patterns.",Op art,General,What river had 40 million fish killed by insecticide in 1969,Rhine,General,What is the name given to a group of doves,Flight + History & Holidays,Where did the real St. Nicholas live? ,Turkey ,General,"What is the fourth dimension (apart from length, depth & width",Time,General,Microbiophobia is the fear of,Microbes +General,Where would you find racettes,Lines on wrist in palmistry,General,What did the person chained to wall in Goonies want?,A Baby Ruth candy bar,General,What is the equivalent Spanish acronym for ufo,Ovni +Entertainment,Who began his professional career with Black Sabbath?,Ozzy Osbourne,General,What is viewed during a a pyrotechnic display?,Fireworks,General,Who is the Roman Goddess of sorcery hounds and crossroads,Trivia + Geography,What is the capital of Nicaragua ?,Managua,General,Who makes Miller Lite beer,Philip Morris,General,Androphobia is the fear of _____.,Males + Geography,As what was the Taj Mahal originally built?,Tomb,General,Peter Lorre was born in what year,1904,Science & Nature,What Is The Home Of A Badger Called? ,Sett  +General,"In Physics, what type of length is represented by the small Greek letter ""lambda""",Wavelength,General,"In The 1997 Movie ""George Of The Jungle"" What Was The Name Of The Ape Voiced By John Cleese",Ape (Believe It Or Not),General,Who was the first civilian astronaut to fly,Neil armstrong + Geography,As what is the South Pole also known?,Amundsen Scott Station,General,Large 3 sided S.American nut,Brazil,General,What is the fastest creature raced for sport,Pigeon +General,Who first appeared in The Mysterious Affair at Styles,Hercule Poirot,General,Lewis Wilson was the first actor to play which character,Batman,General,In California more what are raised than in any other state,Turkeys +General,What part of the body is a busby worn on,Head,General,Which 'first lady of jazz' died in June 1996,Ella fitzgerald,General,What's the oldest university in rhode island,Brown university +General,In the novel Goldfinger name the boss of The Cement Mixers,Pussy Galore,General,The name of which dog breed is also the name for an animals footprint,Pug,General,Erica is the Latin name for what shrub,Heather +General,In Animal Farm what was the name of the farm,Manor Farm,General,Rene Laennec invented which aid to medicine in 1819,Stethoscope,Music,"Which Bands Abbrevated Name WAs ""Carter TUSM""",Carter The Unstoppable Sex Machine +Sports & Leisure,What Do The Initials TT Stand For In Connection With The Isle Of Man Motorcycle Race? ,Tourist Trophy ,General,What does 'majuba' mean,Place of rock pigeons,Entertainment,Who did Patrick Duffy portray in the TV series 'Dallas'?,Bobby Ewing +General,"What is the missing word in english with the letter combination 'uu' continuum, duumvirate, residuum, vacuum, duumvir",Muumuu,General,What is the coldest capital city in the world,Ulan Bator Mongolia,Geography,What Is The Capital Of Hawaii? ,Honolulu  +General,Which Conservative MP wrote the recent novel 'The Clematis Tree,Ann widdecombe,Music,Matt & Luke Goss Were 2 Of The 3 Members Of The Boyband Bros Craig Was The 3rd Member But What Was His Surname,Logan,General,"Opening in 1972, the action in which musical takes place at Rydell High School",Grease +Geography,Kingston is the capital of which country ,Jamaica ,General,What was the name of Thor Heyerdahl's bamboo and balsa wood boat,Kon-tiki,General,What are emblazoned on the jolly roger,Skull & crossbones +General,Who wrote 'a clockwork orange',Anthony burgess,General,Name of which household object comes from Latin to wonder at,Mirror, Geography,What prison island was off the coast of French Guiana?,Devil's Island +General,A boat or raft with two parallel hulls,Catamaran, History & Holidays,What do the five rings on the Olympic flag represent ,5 Continents ,General,"If you go blind in one eye, you'll only lose about twenty percent of your vision, but all of your ______",Depth perception +Toys & Games,Leading manufacturer of toy cars.,Hot wheels,Geography,"In ______________, Domino's Pizza has a reindeer sausage pie on its menu.",Iceland, History & Holidays,Who Released The 70's Album Entitled 461 Ocean Boulevard ,Eric Clapton  +Science & Nature,When Was The First Transcontinental Railroad In North America Completed ,1869 ,General,What is the name of the ocean southeast of Australia,Tasman sea,General,What is the Capital of: Somalia,Mogadishu + History & Holidays,What Was Jackie Kennedy's Name Before She Married John F Kennedy ,Jackie Onnasis ,General,"Doubly-ionised helium atoms, when emitted by some radioactive nuclei, are known as what",Alpha particles,General,What is the common name of scrivener's palsy,Writer's cramp +Science & Nature," Hedgehog quills are not barbed or poisonous. Hedgehogs do apply a foamy, foul_tasting saliva to their quills, which protects the animals from __________",Predators,General,What hybrid do an ass & horse produce,Mule,Religion & Mythology,Who killed Goliath?,David +General,Hockey the calgary _______,Flames,General,Seven million of these are thrown away each day - what,Pennies,General,When was Big Ben completed,1858 +General,Name the son / daughter of Eliot / Ruth Handler,Barbie / Ken yes those dolls,General,Natasha Gurdin became famous as who,Natilie Wood,General,What was crocketts first name on Miami Vice?,Sonny +General,How did Stonewall Jackson die,Shot by own troops – by mistake,General,Which is the best Poker hand,Royal flush,General,Rocky and Bullwinkle enemies are Boris Badenov and who,Natasha Fatale +Science & Nature,Which Creatures Name Has Passed Out Of Usage After It Was Discovered That Its Fossils Proved To Be Identical To The Previously Name3d Apatosaurus ,Brontosaurus ,General,The chupacabra is a legendary Mexican animal what in English,Goat Sucker,Music,Name Two Of The 3 Members Of The Rolling Stones Who Were Fined In 1965 For Urinating Against A Garage Wall,"Mick Jagger, Brian Jones, Bill Wyman" +General,What is the capital of togo,Lome,Entertainment,What is the mother's name in Family Circus,Thelma,General,What did the Celts consider sacred because it communicated moisture from the ground into the air,Trees +Sports & Leisure,In which year was Ascot built? ,1711 ,People & Places,Who Wrote A Diary About Her Attempts To Avoid The Nazi's ,Anne Frank ,Music,"Which Stevie Wonder Song Featured In The Film ""The Woman In Red""",I Just Called To Say I Love You +Art & Literature,Name The Philadelphian Artist Who Introduced The World To (Pop Art) ,Andy Warhol ,General,In which Australian state or territory is the Flinders Range and Lake Eyre,South australia,Music,Which 1979 ACDC Album Cover Features A Band Member With Horns And A Tail,Highway To Hell +General,Southern Comfort is made from a base of Bourbon whiskey and flavouring from which fruit,Peach,General,What are the youngest letters in the English language alphabetically,"J, v, w",General,Who directed Four Weddings and a Funeral,Mike Newell + History & Holidays,"What is the all-time best-selling Christmas recording? (White Xmas, Jingle Bells, Frosty The Snowman, Rudolph the Red Nose Reindeer) ",White Christmas ,Music,"Ian Dury Asked To Be Hit With What ""Your Best Shot"" Or ""Your Rhythm Stick""",Your Rhythm Stick,General,In which country is the volcano Popocatepetl,Mexico +General,In Hood river Oregon what do you need a licence to do,Juggle,General,On which river does Amsterdam stand,Amstel,Music,"Who Checked Into A ""Blue Hotel"" In 1991",Chris Isaak +General,What Do You Fear If You Have Medorthophobia,Erect Penis,Music,Which Star Is Don Mc-Leans Song American Pie Dedicated To,Buddy Holly, History & Holidays,Who was the instigator of the breakaway World Series Cricket in 1977? ,Kerry Packer  +General,Leprophobia is the fear of,Leprosy,General,When was the first leap year,46 bc,Sports & Leisure,Football: The Buffalo _______.,Bills +General,Who is edson arantes do nascimento,Pele,Sports & Leisure,Over What Distance Does Drag Racing Occur ,1/4 Mile ,General,Where in France is the summer residence of thle French President,Fontainebleau +General,The yellow food colouring tartrazine comes from what,Petrol - Gas in USA, Geography,What is the river capital of the world?,Akron,Science & Nature," The hippopotamus has skin an inch_and_a_half thick, so solid that most __________ cannot penetrate it.",Bullets +Geography,What is the capital of North Korea,Pyongyang,General,S.American cowboy,Gaucho,People & Places,Who Founded His Own Production Company Called Ginger Productions? ,Chris Evans  +General,What colour thread is used for filigree?,Silver or gold,General,What film starred barbara streisand and walter matthau,Hello dolly,General,In which U.S. state do the most bald eagles live,Alaska +Entertainment,Who was the sexy star of Barberella?,Jane Fonda,General,The famous 'To be or not to be' speech is in which Shakespeare play,Hamlet,General,Synonymous with obituary; a list of recently deceased.,Necrology +General,OD international aircraft registration letters of what country,Lebanon,Music,Which Glam Rocker Had A Hit With The Quirky Laughing Gnome In 1973,David Bowie,General,What flavour is framboise liqueur,Raspberry +General,English writer - Died Typhoid - Drank Paris water - Prove safe,Arnold Bennett 1931,Music,On The Beautiful South's 1996 album what is the colour?,Blue,General,"The rebuilding of the Globe Theatre in London was the 'brainchild' of which American actor, who died before the project was completed",Sam wanamaker +Music,Who Is Simply Reds Lead Vocalist,Mick Hucknall,General,Manutius's Virgil printed 1501 was the first time what was used,Italic letters,General,Japan's equivalant to the dollar is ______,Yen +General,The modern African republic of Tanzania was formed when Tanganyika and which other nation were combined,Zanzibar,General,"What do bullet proof vests, fire escapes, windshield wipers & laser printers all have in common",All invented by women,Music,Where Was The Concert For Life Held In 1992,Wembley +General,What is the name of the board at a race track showing odds and results,Tote,General,What is the fear of tornadoes and hurricanes known as,Lilapsophobia,General,International dialling codes - where is 672,Antarctica +Music,Which Label Turned Down The Beatles But Signed The Rolling Stones,Decca,General,What is the height travelled by the world's longest escalator,60 minutes, Geography,What color does the bride wear in China?,Red +General,Hours how many times do your ribs move every year during breathing,Five million,General,"What one word makes sense when it precedes age, class and east",Middle,Food & Drink,How many pieces of bun are in a Mcdonald's Big Mac?,Three +General,Who was the first British Royal to become a motorist,Prince of Wales Edward VII,Entertainment,"Who had, next to Samuel Jackson, a leading roll in 'Unbreakable'?",Bruce Willis,Music,"From Guyana, Which 80's Reggae Icon Recorded His Three Greatest Hits On The Ice Label",Eddy Grant + History & Holidays,Which 50's Movie features the Line 'Hey Stella!' ,A Streetcar Named Desire ,General,Who is Mother Goose's son?,Jack,Religion & Mythology,Where did Robin Hood supposedly live,Sherwood forest +General,"Apart from eggs, what is the other essential ingredient in 'Eggs Florentine'",Spinach,General,If you had crabites what have you got,Collection of fossil crabs,General,John Flynn invented what service in Australia,Flying Doctors +General,What is a loosely woven fabric generally used for clothing called,Flannel,General,How old was the world's oldest man?,141,General,Name a mainland South American country where cars are normally driven on the left hand side of the road.,Guyana surinam +General,"Whats ""Americas favorite fun car""",Mustang,General,"In the children's tv series 'sesame street', what two characters were roomates",Bert and ernie,General,Kong Zi is better known as who,Confucius +Music,The Song “You Make Me Feel Mighty Real” Filled The Dancefloors For Which Act In 1978,Sylvester,General,"This Irish republic political movement founded in 1905 to promote Ireland's independence, is translated as ""Ourselves Alone."" What is it commonly called?",Sinn Fein,General,What is the most popular sport in england,Darts +General,What is a group of this animal called: Clam,Bed,Entertainment,"What was ""Rocky's"" last name?",Balboa,General,"What were Tricity Triumph, Kelvinator, Lec De Lux",Refrigerators + Geography,Five US states border which ocean?,Pacific Ocean,Food & Drink,"What, on a bottle of wine, is the punt? ",The dent on the base ,General,How did Miss Piggy tell the difference between love and lust,By spelling +General,What radioactive element is used to power modern heart pacemakers,Polonium,Music,"The Single ""Heart & Soul"" Was A Hit For Which Band A) T'Pau, B) Mr Mister, C) Neil Diamond",T'Pau,General,In food what does UHT mean on the carton,Ultra Heat Treatment +General,What Shakespeare character is it considered bad luck to mention in a theater,Macbeth,Religion & Mythology,Which Saint killed the dragon?,George,Art & Literature,"In painting, a work made of several panels or scenes joined together. A diptych has two panels; a triptych, three. ",Polyptych + Geography,What is the capital of Syria ?,Damascus,Science & Nature," Out of the 650 known species of leeches, Hirudo medicinalis is the most common used by __________",Doctors,General,What bird is sometimes called the Yaffle,Woodpecker +General,Which Oxford College's Chapel is also Oxford's Cathedral,Christ church,General,In which country would you find the Pripyet Marshes,Belarus,General,In Shakespeare Hamlet who is Ophelia's brother,Laertes +General,"In 1980, who recorded ""Another One Bites the Dust""",Queen,General,Which Pop Group Were Signed By Decca Records On The Very Same Day In 1962 That The Beatles Were Turned Down,Brian Poole & The tremeloes,General,What Would You Be Suffering From If You Had Halitosis,Bad Breath +Music,Which American Malke Singer Reached No.2 With The Song Hey Baby 1962,Bruce Channel,General,How long does it take the average person to fall asleep,Seven minutes,General,What is the most populous borough of New York?,Brooklyn +Music,Who Is The Only Female Singer To Have Recorded Under 9 Different Record Labels,Lulu,General,In 1752 Mr Blake and Callahan raced and began which sport,Steeplechase,General,Slow Ride' was Foghat's biggest hit from which album released in 1975,Fool +General,What is the meaning of the mercedes benz motto 'das beste oder nichts',The,General,Humphrey Bogart won his only oscar for his part in which film,The african queen,General,Display abbreviations-short forms: fbi stands for _____,Federal bureau of +Music,Fabrice Morvan & Rob Pilatus Formed Which Pop Duo,Milli Vanilli,General,The band Steely Dan are named after what,Slang for Penis,General,Large shop selling many different types of goods,Megastore +General,Controversial diet guru nathan pritikin dies in a new york hospital at age,69,General,"What did Watson, Crick and Wilkins discover",Dna,General,Products What was the only nation to register zero births in 1983,Vatican City +General,Hamlet was the prince of ______,Denmark,General,In the UK they are butter beans what in the USA,Lima Beans,General,Where is the Guggenheim Museum?,New York + History & Holidays,What did 18 British Museums & Art Galleries Start To Do In 1974 ,Charge Admission ,General,Eye for Eye - Tooth For Tooth what comes next,Hand 4 Hand - Foot 4 Foot Ex 21 24,Music,"What country did the Beatles flee after ""snubbing"" their first lady?",The Phillipines +Entertainment,Actor: __________ Savalas.,Telly,People & Places,Who Was The Joan Collins Fan Club ,Julian Clarey ,Sports & Leisure,Which Country Does Chelsea Striker Didier Drogba Represent At International Level? ,Ivory Coast  +General,What is the oldest city in North America,Quebec city,General,What is the young of the beaver called,Kitt,General,What is the only fish that can blink at you with both eyes?,Shark +General,Of which country is Amharic an official language,Ethiopia,General,What kind of clock has no moving parts,Sundial,Art & Literature,In Which Novel Was It The Job Of The (Fireman) To Burn Books ,Fahrenheit 451  +Science & Nature,"Often hunted for its fur, this South American rodent bathes in dust and is often sold in the pet trade.",Chinchilla,General,"In Greek mythology, who were achilles' parents",Peleus and thetis,Sports & Leisure,With which sport would you most associate the commentator Ted Lowe? ,Snooker  +General,What is the largest city south of the equator,Buenos aires,General,What is the highest-pitched woodwind musical instrument,Piccolo,General,Socrates was trained into what profession,Stonecutter +General,What river acts as a natural boundary between the counties of Norfolk and Suffolk?,River Waveney,General,Who is the author of Dune,Frank herbert,General,Various methods used to prevent pregnancy from occurring,Birth control +General,Bert Berns produced what song for Van Morrison,Brown eyed girl,General,What is the flower that stands for: surprise,Truffle,General,Astrakhan comes from which animal,Sheep +General,What word describes one tenth of a nautical mile,Cable,General,What two biblical cities did God destroy with fire & brimstone,Sodom & gemorrah,Mathematics,What geometric shape has 4 equal sides,Square +General,Who secretly married Sara Lowndes in November 1965,Bob Dylan,General,"In wartime, what is the right of a belligerent warship to stop neutral merchant vessels on the high seas in order to ascertain the nature of the cargo and the ownership of the vessel and thus determine its liability to capture",Right of Search,Music,Who was asked in 1874 by Henrik Ibsen to write incidental music to Peer Gynf?,Edvard Grieg +General,"What is the largest bay in the world, (larger than England) bordering only one country Canada, & only two provinces & a territory",Hudson bay,General,What is the fear of eyes known as,Ommetaphobia,Music,"From the 1960's from which song and which artist “When at last my dreams come true, Darling this I'll know, Happiness will follow you, Everywhere you go”?",Elvis / Love Me Tender +General,Which film star's biography was called Neither Shaken Nor Stirred,Sean connery,General,"Who said 'So far as the laws of mathematics refer to reality they are not certain. And so far as they are certain, they do not refer to reality.' ?",Albert Einstein,General,What was Disney's first non animated live action film,Treasure Island +General,What is the Capital of: Norway,Oslo,General,What famous priest ministered to the molokai lepers from 1873 until his death,Father damien,General, _____ of Love' by Frankie Lymon,ABC's +Geography,Name the largest lake in Australia,Eyre,General,Who was the Norse god of poetry,Bragi,Music,Which singer went solo after performing with the Commodores?,Lionel Richie +General,What country would you have to visit to see the ruins of Troy,Turkey,General,What is the spiral galaxy nearest ours,Andromeda,General,What is a group of this called: Bacteria,Culture +General,What is the flower that stands for: audacity,Larch,General,If you shout Tsuki what sport are you practicing,Kendo,General,This teen was sentenced to a public caning in singapore in 1994,Michael fay +Science & Nature," A ""winkle"" is an edible __________",Sea snail,General,Which battle was fought at Senlac hill,Battle of Hastings,General,Lygophobia is the fear of what,Darkness +General,Hussey a hippo can open its mouth wide enough to fit a 4 foot tall ______,Child,General,What name is given to an isolated mountain peak protruding through an ice sheet,Nunatuk,General,What was the loneliest part of the Cross Canada run for Terry Fox,Nova +General,If a dog is canine what is cirvine,Deer,General,Richard I was the son of which English monarch,Henry ii,Music,"Which ""Pop Idol"" contestant was the first to reach number one in the UK singles charts?",Will Young +General,"In ""the hobbit"", what is the name of the wizard",Gandalf,Science & Nature,"Ulna, radius, and clavicle are types of __________.",Bone,General,What company was founded by Sir Allan Lane in 1935,Penguin books +General,"The word ""Hellas"" appears on the stamps of which country",Greece,General,What's the most common color of Topaz,Yellow,General,"Which Famous Tv Couple Lived At ""46 Peacock Crescent, Hampton Wick""",George & Mildred +General,98% of Japanese citizens are what,Cremated,General,Old Eng law - you can't beat wife with anything wider than what,Your thumb – Thus rule of Thumb,General,To which team did marlboro switch its backing from brm in the 1974 season,Mclaren +General,If you had 2 eight enders in one bonspiel what are you playing,Curling,General,Who was Israel's only woman prime minister,Golda meir,Geography,Which Country Has The Most Countries Bordering It ,China (16)  +General,"In the 1988 film Twins, what were the character names of Arnold Schwarzenneger and Danny De Vito?",Julius and Vincent,General,"What is the Roman numeral for 400, which is also the most popular way to purchase music",Cd,Entertainment,This term means to play moderately slow and gracefully?,Adagio +General,Defecolagnia is sexual arousal from what act,Crapping,Geography,The old Roman province of Lusitania is now called ____________. Some parts of Lusitania are also found in Spain.,Portugal,General,What book did Christians often place on their foreheads to cure insomnia in medieval times,The bible bible +General,What book was given to all officers in the Confederate army,Les Miserables,General,What was the name of Milla Jovovich's character in the Fifth Element?,Lelu, Geography,What is the capital of Brunei ?,Bandar Seri Begawan +General,Which region of Spain has Barcelona as its capital,Catalonia,General,From what language is the term 'finito',Italian,Science & Nature,Lateral Epicondylitus Is The Medical Name For Which Common Medical Condition ,Tennis Elbow  +General,What ancient Roman buildings name means Place for a Giant,Coliseum,General,Who was the roman god of the sea,Neptune,Science & Nature,What is the chemical symbol for einsteinium?,Es +General,What's the US equivalent to the Department of Health in the UK,Surgeon General,General,"Book of the Old Testament, third of the five biblical books called the Pentateuch",Leviticus,General,What creatures call an apiary home,Bees +General,"In egyptian mythology, who is the god of the underworld",Cherti,General,"Hattie McDaniel was the first black actress to win an Oscar, for which film",Gone with the wind,General,What nationality was pablo picasso,Spanish +Music,"Which British Politician Sued ""The Move"" In 1967 And Won The Royalties To ""Flowers In The Rain""",Harold Wilson,General,What would you use you zygomaticus muscle for,Smiling,General,What was adolf hitler's religion,Roman catholic +Geography,Into what bay does the ganges river flow ,Bengal ,Entertainment,Who directed citizen kane?,Orson Welles,General,Where is tupelo,Mississippi +General,What is a male whale called,Bull,General,Who was Mussolini's favourite cartoon character,Donald Duck,General,What was the code name for USA landings Morocco in 1942,Operation Torch +General,Richard Adams wrote the novel Shardik what was Shardik,A Bear,General,"Who said ""Canada? I don't even know what street Canada is on""",Al Capone,General,What alternative scale (not Richter) measures earthquakes,Mercalli +General,A photic sneeze is caused by___.,Sunlight,General,"Which one of rc, rl and rlc circuits which one oscillates charge",Rlc,General,"In Independence Day, What does Will Smith yell as he blasts off in the space ship?",I gotta get me one of these +General,Basie Ronnie Bond drummed with what group,Troggs,Music,Number of slices sacrificed to film Ringo's Pizza Hut commercial,12,General,What is a group of this animal called: Cub,Litter +Music,What 1958 song was The Coaster's only #1 hit?,Yakkety Yak, History & Holidays,What was named after Amerigo Vespucci?,America,Sports & Leisure,What Diameter Is The Circle From Which You A Discus ,2.5m  +General,Which bone in the human body is at the front but sounds like it should be at the back,Sternum,General,"The Silver Phyllis, a Boeing 707, belonged to what financier",Robert vesco,Music,"Who Released An Album Entitled ""Bookends""",Simon & Garfunkel +General,What is the flower that stands for: early attachment,Thornless rose,General,Who wrote four consecutive number one songs in 1978,Barry Gibb,General,"5% of Canadians don't know the first seven words of the Canadian anthem, but know the first nine words of which anthem?",The American anthem +Religion & Mythology,Who founded the People's Temple Commune?,Jim Jones,Science & Nature, Native peoples of South America catch __________ and use their razor_sharp teeth to make tools and weapons.,Piranha,Art & Literature,"A movement in American painting and sculpture that originated in the late 1950s. It emphasized pure, reduced forms and strict, systematic compositions. ",Minimalism +General,Vitamin B2 has what other name ,Riboflavin, History & Holidays,Who Released The 70's Album Entitled All Mod Cons ,The Jam ,General,Mbabane is the capital of ______,Swaziland +General,Official taxis in London are usually what colour,Black,General,Another word for direct confrontation,Face-off,Science & Nature," The first __________ dragons to breed in the western world are at the National Zoo at the Smithsonian Institute in Washington, D.C.",Komodo +General,What is a group of elephants,Herd,General,In 1985 William Beckman paid no tax on £100000 profit why,William was a dog – cant pay tax,General,In Oklahoma it is illegal to wear what in bed,Your Boots +General,If you were given a sitooterie what would you do with it,Sit out in it gazebo/summerhouse,General,In Morrisville Penn a woman must have a permit to do what,Wear Cosmetics,General,In which city was Hugh Grant arrested with Divine Brown?,Los Angeles +General,What make and model of car was Christine in the book and film,Plymouth Fury,General,What's unusual about phone directory in Iceland,Alphabetical by forename,General,"What's the common name for leucocytes, found in the blood",White blood cells +General,Who wrote 'The Canterbury Tales',Geoffrey chaucer,General,Amerigo Vespucci airport is in which city,Florence,General,Who starred in the 1968 film Funny Girl,Barbra streisand +General,In Czarist Russia it was illegal to do what,Smoke,General,Emission of energy as electromagnetic waves in the portion of the spectrum just beyond the limit of the red portion of visible radiation,Infrared Radiation,General,"In The 2000 Film ""Rugrats In Paris"" Who Supplied The Voice Of ""Coco La Bouche""",Susan Sarandon +Food & Drink,The two ingredients of a Black Velvet are stout and,Champagne,Music,"In Which Year Did Donna Summers Disco Anthem ""I Feel Love"" Get To No.1",1977,General,An isohel on a map joins place of equal what,Sunshine +General,Who sat on her tuffet,Little miss muffet,General,Apart from a caber by rule what is compulsory in caber tossing,Wearing a kilt,General,In the middle ages you could be fined four pence murdering who,Travelling Musician +General,What Polish composer established piano as a solo instrument free from choral or orchestral influence,Chopin,Sports & Leisure,How many players are there on a water polo team?,Seven,Sports & Leisure,"In Darts From What Number Under 100 , Is It Not Possible To Finish With 2 Darts ",99  +General,What planet is nearest in size to Earth,Venus - 5% smaller,General,What is Michael Jackson's middle name,Joseph,General,Where do arboreal animals live,Trees +Science & Nature,Which Is The Largest Land Living Carnivore ,Brown Or Grizzly Bear ,Music,Name 2 Of The 3 Stranglers Hits That Made The Top Ten In 1977,"Peaches, Go Buddy Go, Something Better",General,Who Became The Main Presenter Of Record Breakers After Roy Castle Retired,Kris Akabussi +General,What is the leading cause of death in Papua New Guinea,Falling out of Trees,Sports & Leisure,What Olympic Sport Prohibits The Wearing Of A Beard? ,Boxing ,General,Jim Backus was the voice of Mr. ______?,Magoo +General,Which American astronaut played golf on the moon,Alan shepard,Food & Drink,What is bottled in Jeroboams? ,Champagne ,General,Who is the one eyed giant of Greek mythology,Cyclops +General,What is the Capital of: Bahrain,Manama,General,James Cagney won an oscar for his part in which musical,Yankee doodle dandy, History & Holidays,Who Played The Title Role In The Abdomibale Dr Phibes ,Vincent Price  +General,Who was the Designer of the Vickers Wellington bomber aircraft,Barnes wallace,Music,"At What Age Did Michael Jackson Record ""Got To Be There""",13,General,Rams Horn Wandering Bladder Prickly Herald types of what,Freshwater Snails +Science & Nature,This moon is our solar system's most cratered satellite:,Callisto,Entertainment,"Who recorded the lengthy song: ""In-A-Gadda-Da-Vida"" in 1969?",Iron Butterfly,General,Who wrote the satire Candide published in 1759,Voltaire +General,"Who was said to be the final casualty of the U.S. Civil War, killed five days after it ended",Abraham lincoln,Music,In whose band is Madonna Wayne Gacy the keyboard player?,Marilyn Mason,General,"This cereal's box features an empty bowl and the phrase ""more, please.""",Cracklin oat bran +General,What East Coast city was Arthur Fiedler born in,Boston,General,What is the flower that stands for: crime,Tamarisk,General,What is finely ground powdered sugar called,Icing sugar +General,Mares' tails are examples of which type of cloud,Cirrus,Music,"Which Jimi Hendrix Classic Features The Line ""Scuse Me While I Kiss The Sky""",Purple Haze,General,Milk chocolate covered peanuts.,Goobers +General,What do table tennis players change after five points,Service,General,Giacomo Agostini - 122 Grand Prix 15 world titles what sport,Motorcycle Racing, History & Holidays,Who Formed The First Labour Goverment? ,Ramsey MacDonald 1924  +General,"Who wrote ""hedda gabler""",Henrik ibsen,General,"Who was the Russian-born French painter, one of the originators of abstract art, who died in 1944",Vasily kandinsky,Food & Drink,Where were the first European coffee houses opened? ,Vienna  +General,What was the original name for Meccano?,Mechanics Made Easy,Food & Drink,Poppin Fresh' Serves As The Mascot For Which International Food Brand? ,Pillsbury Dough Co ,Music,What was Kate Bush's first hit recording?,Wuthering Heights +General,"Which female vocalist had a number one U.K. hit in 1955 with Softly, Softly",Ruby murray,Art & Literature,Who wrote 'Alice In Wonderland'?,Lewis Carroll,Music,Name the film that featured a spaced-out George Harrison soundtrack?,Wonderwall +General,In what Australian state would you find shepparton,Victoria,General,Sesquipedalophobia is the fear of,Long words,General,"Countries of the world:chain of more than 3,000 islands, major cities include Yokohama & Osaka",Japan +General,In what sport was Eddie Rickenbacker a top contender before WW II,Auto,General,What is the special significance of the asteroid Ceres,Believed to be the largest one,General,What is the capitol of Ghana,Accra +Science & Nature, __________ eagles hunt over a range of 100 square miles to feed their young.,Golden,General,The are 16 of these on a dollar bill - 16 what,Number One,General,Which stringed instrument is blown to produce sound,Aeolian Harp +General,"In 1971, Intel released the world's first single chip Microprocessor, what number was it known by?",4004,Geography,"At over 3 million square kilometres, Sakha is the largest federal subject of which country? ",Russia ,Music,According To The Song What Were Dire Straits The Sultans Of,Swing +General,What do enzymes start while food is still in the mouth,Breakdown of food,General,Who was the first unseeded man to win Wimbledon,Boris becker,General,Marcellite Garner was the first voice for what Disney character,Minnie Mouse +Science & Nature,An animal is a bird if it has _______.,Feathers,Entertainment,Hanna-Barbera rose to fame by creating what duo for MGM?,Tom and Jerry,General,What does an armadillo taste like,Pork + History & Holidays,The American M4 tank is better known as what?,The Sherman tank,General,Matthew Perry plays which character in the television series Friends,Chandler,General,"Alcatraz: this inmates nickname was ""machine gun""",George kelly +Science & Nature,What Kind Of Animal Was Willy In Free Willy ,A Killer Whale ,General,Which U.S. baseball player was known as the 'Yankee Clipper',Joe di maggio,Technology & Video Games,Who is the CEO of Apple computers?,Steve Jobs +General,"Boothby blonde, China long, straight 8 varieties of what",Cucumber,Art & Literature,What Rank Was Biggles ,Major , Geography,What is the capital of Guatemala ?,Guatemala +General,"What was Babe Ruth, before he was a baseball player",Bartender,General,Of what species is the firefly a member,Beetle,General,Who has a tattoo saying Starland Vocal Band,Homer Simpson +General,"Which group did lou reed front, who, in the 60's recorded 'heroin' and 'sweet jane'",Velvet underground,General,On FRIENDS what was the name of Ross's monkey?,Marcel,General,What is the Capital of: British Virgin Islands,Road town +Sports & Leisure,Which Sportswear Company Is The Brand With The Three Stripes? ,Adidas , History & Holidays,Who played the younger scrooge in the Classic with Alistair Sim? ,George Cole ,Music,Name Two Of The Most Famous Members Of The Red Hot Chili Peppers,"Anthony Keidis, John Frusciante, Flea, Chad Smith" +General,"What was Tom Cruise's call sign in the movie ""Top Gun""?",Maverick,General,400 what per week permanently close in the USA,Churches,General,Who composed the ballets - The Firebird and The Rite of Spring,Igor Stravinsky +General,What colour is a giraffes tongue,Black,General,Coffee haematophobia is a fear of ______,Blood,General,In Matilda what was the name of Matilda's teacher?,Jennifer Honey +General,Not try this yourself who was the emcee for tvs let's make a deal,Monty hall,General,SF are the international car registration letters for which country,Finland, Geography,Abuja is the capital of ______?,Nigeria +General,What invaders from the north was the Great Wall built to repel,The mongols mongols,General,If a woman maritates what is she doing,Female Masturbating,General,"In Dickens' novel 'David Copperfield', what is the Christian name of his old nurse Peggotty",Clara +General,What gift is associated with the 60th Wedding Anniversary?,Diamonds,General,What year was Geronimo captured in,1886,General,An aneroid is a kind of___.,Barometer +General,The Golden Rain is the common name of what tree,Laburnum,General,Ponophobia is the fear of,Overworking of pain,Science & Nature,Which 2 Towns Were Linked By The Worlds First Commercial Railway ,Stockton & Darlington  +Entertainment,What is the official birthplace of country music?,Bristol,General,Oil can Henry is the enemy of which cartoon character,Mighty Mouse,General,Richard Gere was married to which model,Cindy crawford +General,A hippo can open its mouth wide enough to fit a 4 foot tall ______,Child,General,In which opera does the U.S. naval officer Lieutenant B.F. Pinkerton appear,Madam butterfly,Art & Literature,Which Novel Deals With The Events Of One Day In Dublin In June 1904 ,Ulysses By James Joyce  +General,What is Sir John De Mentieth famous for,Capturing sir william wallace,General,"The name of what style of painting was taken from, the title of a Claude Monet work",Impressionism,General,Who invented the phonograph,Thomas Edison +Entertainment,Who wrote the song 'Do They Know It's Christmas' with Midge Ure?,Bob Geldof,General,July 14th is a national day of celebration in what country,France - Bastille Day - from 1789,General,Punk Drummer Chris Miller what name when he Damned in 70s,Rat Scabies +General,Who was the father of english poetry,Geoffrey chaucer,General,"Who wrote ""The Emporers New Clothes""",Hans christian anderson,Science & Nature,What Creatures Were Frequently Used To Bleed Patients In The Nineteeth Century ,Leeches  +General,What is the symbol of the democratic party,Donkey,Music,Which pop group took their name from the Arabic word for black?,Aswad,General,What are the largest oceans in order of size,"Pacific, atlantic, indian" +General,What is the largest Island in The Greater Antilles,Cuba, History & Holidays,Who played Jack Nicholsons wife in The Shining ,Shelley Duvall ,Geography,Colorado's capital of __________ is the largest metro city in a 600_mile radius _ an area almost the size of Europe.,Denver +General,Whose autobiography was The long walk to Freedom,Nelson Mandela,General,"Where was the record for most snowfall in a day, on february 7 1916",Alaska,General,What is Daffy Ducks middle name,Dumas +Science & Nature,What is the only dog that doesn't have a pink tongue?,Chow,General,Les Gray was the lead singer of which group,Mud,People & Places,Who Said 'We Have Become A Grandmother' In 1989 ,Margaret Thatcher  +General,"Who wanted you to ""shake your groove thing""",Peaches & herb,People & Places,Which writer caused a massive stir in 1926 when she went missing for eleven days? ,Agatha Christie ,General,The Rambunctious and Clever Ones what films name in Taiwan,Wayne's World +General,What food item did Pythagoras advise his followers to avoid,Beans,General,Baseball: the texas ______,Rangers,Geography,Which country was previously called Abyssinia? ,Ethiopia  +General,What is the southernmost city in the u.s,"Brownsville, texas",General,Who wrote The Caine Mutiny,Herman Wouk,General,What is the Capital of: Tonga,Nuku'alofa +General,What is the Capital of: Jersey,Saint helier,General,If you have Dorophillia what turns you on,Animal Skins or furs,General,"What is the largest lake in South America, which is half freshwater, half saltwater",Maracaibo +General,What sport begins in front of the south stake,Croquet,General,The last day of public operation of the Liverpool Overhead Railway was December the 30th of which year,1956,General,Who was the founder of microsoft,Bill gates +Science & Nature,Which Nation First Invented The Wheelbarrow ,The Chinese C Ad 200 ,General,Which is the only sea below sea level,Dead sea,General,Which comic strip did sam keith draw,The maxx +General,In which field was Dame Ellen Terry famous,Acting,General,Where is it polite to stick your tongue out at your guests,Tibet,General,"In The 80's Which Soap Character Was Voted Britains Most Popular Person Ahead Of The Queen, Princess Diana, And The Queen Mother",Hilda Ogden +People & Places,Who was Born Farouk Bulsara? ,Freddie Mercury ,General,Who died at Gravesend in March 1617 when she was about to embark for her homeland in America,Pocahontas,General,What piece of music commemorates military action that took place on 16th May 1943,The dam busters +General,What commercial was Michael Jackson singing for when his hair caught on fire?,Pepsi,Food & Drink,What Are Escargots ,Snails ,General,How much longer is a day on mars than a day on earth,Thirty minutes +General,What drink is named after the queen of england who was famous for her 'sanguinary' persecution of the protestants,Bloody mary,Entertainment,What is the name of the Family Circus's dog?,Barf,Music,Louise Nurding Dropped Her Surname On Leaving Which Group,Eternal +General,What are squirrel hairs used to make,Camel hair brushes,General,What are followers of the Unification Church called,Moonies,Food & Drink,What is the flavour of the Greek drink 'ouzo'? ,Aniseed  +General,Viticulture is the growing of what plants,Vines,General,What made Michael Milken famous and rich?,Junk Bonds,Music,Who was barefoot on the cover of Abbey Road?,Paul +General,"What fabric are harris, lewis and donegal examples of",Tweed,General,Who wrote 'psycho',Alfred hitchcock,General,What is the most recent year that can be written upside-down and rightside-up and still look the same,1961 +General,What do scuba divers ditch first if they have to ascend in a hurry,Weight belts,General,"Who counted himself out on March 31, 1968",Lyndon johnson,Art & Literature,Spanish modernist and cubist Pablo _____,Picasso +Science & Nature,What type of creature is a sidewinder ,A snake ,General,What body of water rises in the Yukon & flows south to British Columbia,Lizard river,General,What does an anemologist study,Wind +General,Name the u.s. state with the smallest population,Alaska,General,"This word comes from a knight whose lance was free for hire, (i.e. Not pledged to one master).",Freelance, Mathematics,"The mathematical study of properties of lines, angels, etc., is ________.",Geometry +General,A soft toffee like sweet,Fudge,Music,In Which Decade Did Earth Wind And Fire Have Their Biggest Hit,The 80's Lets Groove In 1981, History & Holidays,This U.S. Secretary of State won the Nobel Peace Prize in 1973,Kissinger +General,The town of Pisa lies in which Italian region,Tuscany,General,What is the collective noun for a group of bears,Sloth,General,"Valentine in an average lifetime, the average american gives flowers 136 times on __",Mother's day +General,The international date line is in the,Atlantic ocean,General,What is the common name for white soft limestone,Chalk,General,In which organ is a pulmonary disease located,Lung + Geography,What is the official language of Egypt?,Arabic,General,"Who, with the First Edition, had a number two hit in 1969 with 'Ruby, Don't Take Your Love To Town'",Kenny rogers,General,Who was lauren bacall's first husband,Humphrey bogart +General,Shower what is a tokinese,Cat,Music,With Which Mod Revival Band Did The Style Councils Mick Talbot Play Keyboards,The Stranglers,General,Port louis is the capital of ______,Mauritius +General,What is the flower that stands for: belief,Passion-flower,General,"Who is the persian goddess of water, fertility and war",Anahita,Food & Drink,What does the abbreviation A.B.V. Stand for in alcoholic drinks? ,Alcohol By Volume  +General,The Rikkesmuseum is in what European city,Amsterdam,General,How many bulls are slaughtered in a normal bullfight,Six,Music,Which 60's Band Had A Hit With Waterloo Sunset,The Kinks + Geography,"What is the largest city in Australia, in terms of population?",Sydney,Geography,What state was the home to Mayberry,North carolina,General,Thanatos in Greek Mors in Roman Gods of what,Death +Food & Drink,What crop is attacked by the Colorado beatle? ,Potato ,Art & Literature,"This early American statesman and inventor wrote the book, ""Fart proudly""?",Benjamin Franklin,General,"What is further from the equator Tasmania, Tanzania or Transylvania",Transylvania +General,"Chimpanzees, gorillas and orang-utans what is missing",Gibbons,General,The word 'poltergeist' comes from what language,German,Science & Nature,This condition characterized by the swelling of the thyroid gland is caused by an iodine deficiency.,Goitre +General,Garland who was the unnamed executive producer of the 1980 film 'elephant man',Mel,General,Which job was so dangerous staff adverts wanted orphans only,Pony Express,Music,Which Canadian folk singer wrote the classic 70’s hit “big yellow taxi”,Joni Mitchell +General,The longest tunnel connects new york and ______,Delaware,General,Who wrote the Man in the Iron Mask,Alexander Dumas,General,The Amazon river rises in which country,Peru +General,In Australia what is the no 1 topping for pizzas,Eggs,General,"Which Medical Complaint Was The ""Jacuzzi"" Originally Developed To Help",Arthritis,General,Which Irish political parties name translates as we ourselves,Sein Fein +Art & Literature,Which Algerian Born French Authors Works Included L'Etranger & La Peste ,Albert Camus ,General,Potophobia is the fear of,Alcohol,General,Which famous artist took up painting with his left hand when he lost the use of his right hand at the age of sixty,Leonardo da Vinci +General,Which sitcom was set in Dunns River Connecticut,Soap,General,What was the favorite expression of captain marvel,Holy moley,General,In US what sporting good outsells Base Basket and Footballs,Frisbees sales beat all combined +General,Which artist painted Venus of Urbino,Titian,General,What is a myocardial infarct,Heart attack,General,What is the word (derived from Malay) for unhusked rice,Paddy +Science & Nature, The average cow produces __________ glasses of milk each day.,40,General,What is the food tofu made from,Soya Bean Curd – via Soya milk, History & Holidays,In What Year Did World War I Begin ,1914  +General,"In bowling, what is it if you knock down all the pins with two balls",Spare,General,From which type of rock is marble formed,Limestone,General,In Japan what is Raku,Biscuit fired Pottery + History & Holidays,What name did the indians give the black soldiers that were fighting against them in the late 1800's ,Buffalo soldiers ,Sports & Leisure,What sport did James Naismith invent ?,Basketball,General,Who was the president of France 1947 54,Vincent auriol +General,"In ballet, the ability of a dancer to remain suspended in air during a jump; elasticity in jumping.",Ballon,Music,"What Kind Of Cats Did ""The Cure"" Have",Love Cats,General,Dwight Eisenhower was the first president to hold what,Pilots Licence +General,Which city has a ram as its emblem,Derby,Sports & Leisure,What Height Is The Crossbar Of A Goal In Professional Football ,8 Feet ,General,What is apache indian corn beer,Tizwin +General,Who did Peeping Tom peep at?,Lady Godiva,General,"A mind-boggingly stupid, but very very ravenous, animal",Ravenous bugblatter beast of traal,General,"Lawn game with hoops, wooden balls and mallets",Croquet +General,What is the young of this animal called: Lion,Cub,General,"Whose catchphrase was ""Stop messing About""",Kenneth Williams,General,Who composed a symphony nicknamed The Hen,Joseph Hayden +General,Saint Stephens day is better known as what,Boxing day,General,After which war did cigarette smoking become fashionable,Crimean war,General,The singer Brandon Flowers is assoociated with which band?,The Killers +Sports & Leisure,Football: The Miami __________.,Dolphins,General,What is the correct name for food permitted under Moslem laws,Hal-al,General,Who had a no 1 Album 60s 70s 80s 90s,Barbara Strisand +General,How many seconds comprise a day,86400,General,In Scandinavian mythology what bridge linked heaven and Earth,Bifrost,General,What is the old name of the city Istanbul,Constantinople +Entertainment,What instrument does Woody Allen play?,Clarinet,General,What nationality was the first person who walked in space,Russian,Music,The Pretenders Were Back On What Kind Of Gang,Chain +General,What document is needed for one to enter a foreign country,Passport,General,What Was Invented By Adolf Flick In 1887,The Contact Lens,General,What does the musical term 'pesante' mean,Heavy +General,Where are Gene Roddenberrys ashes,Sent into Space,General,"What does the latin ""luna"" mean ?",Moon, History & Holidays,Where were the ancient script of Linear A and Linear B found ?,Crete +General,"In ballet, a jump from one to both feet, usually landing in fifth position.",Assemblé, Geography,What is the capital of Yugoslavia ?,Belgrade,General,"Purely coincidental, Disneyland and Walt Disney World amusement parks are in counties with the same name. The former is in Orange County, California; the latter is in Orange County, _________ ",Florida +Food & Drink,What Are Petit Fours ,Small Iced Cakes & Sweets ,Science & Nature,In Computing And Hi_Tech Gadetry What Doe The Initials WAP Stand For ,Wireless Access Point ,Science & Nature,Who Had Anaesthesia During ChildBirth Thereby Making It Acceptable ,Queen Victoria  +General,By Law Portland Oregon a minister cannot marry a couple where,Ice Rink, History & Holidays,He was stabbed by Cassius.,Julius caesar,General,What medicine hastens the emptying of the bowels,Laxative +General,Bill gates invented which os,Windows,General,Which group of animals are called a cete,Badgers,General,"Any of a large group of chemicals almost exclusively organic in nature, used for the coloring of textiles, inks, food products, & other substances",Dyes +General,What is the Capital of: Qatar,Doha,General,Stygiophobia is a fear of ______,Hell,Science & Nature,From What Metals Is Brass Made ,Copper & Tin  +General,Santa Cruz airport serves which city,Bombay,General,Small shell fish clinging to rocks,Barnacle,General,Of which ship was miles standish captain,Mayflower +General,Which prime minister established canada's own flag,Lester pearson,Music,Susanna Hoofs & Vick & Debbi Peterson Were Members Of Which All Girl Band,The Bangles,General,Who invented the food processor in 1947,Kenneth wood +General,What bumbling andy griffith show character has the middle initial b?,Barney fife,Sports & Leisure,How many referees work a soccer game,One,General,What singer was nicknamed by his fans The Lizard King `,Jim Morrison +General,Who wrote The History of Mr Polly,H G Wells,General,Urophobia is the fear of ______,Urinating,General,Larry Page is a co-founder of which Internet giant?,Google +General,Short legged long bodied dog,Dachshund,General,What is meant in computing terminology by the acronym WYSIWYG,What yoU.S.ee is what you get,General,"If british mp's want to have a private session, what does one of them say when they point to the public gallery",I spy strangers +General,In Louisiana by law who can't be charged more than 25c haircut,Bald Men,General,"In law, the criminal offense of marrying while one is still a partner in a valid earlier marriage.",Bigamy, History & Holidays,Which of Santa's reindeers shares its name with a mythological God of Love ,Cupid  +General,What is the outermost planet of the solar system,Pluto,General,What does Scotland export to Saudi Arabia,Sand,General,Which Charles Dickens novel features a case of 'spontaneous combustion'?,Bleak House +Music,Which 1980's boy band was “Donnie Wahlberg” a member of?,New Kids On The Block, Geography,Port Louis is the capital of ______?,Mauritius,Entertainment,Jerry Lee Lewis had Great _____ Of Fire.,Balls! +General,What is a wood pussy,Slang for skunk,Toys & Games,You meld groups of three cards or more of the same rank or suit in this game.,Rummy,General,What type of animal was Nana the nursemaid in 'Peter Pan',Dog +General,If an Australian had trouble with his donk who would he call on,Mechanic - Donks an engine,General,Walloons speak what language,French in Belgium,Science & Nature,The practice of joining the parts of two plants to make them grow as one is called ________.,Grafting +General,What was the American Pie in Don Macleans song?,Buddy Holly's Crashed Airplane, History & Holidays,In the year 2000 which companies CEO was the worlds second richest man ?,Oracle,Food & Drink,The founder of Weight Watchers.,Jean nidetch +General,Orthoepy is the study of what,Word pronunciation,General,What is the ability to move objects with mind power called,Telekinesis,Sports & Leisure,What Do The Letters PB Next To An Athlete's Name Indicate ,Personal Best  +General,"The first international cricket match ever, was held between Canada & _____",U.S.A.,General,An entertainment with an educational aspect,Edutainment, Geography,Which state has the most hospitals?,California +General,Who built the first steam engine to run on rails,Richard trevithick,General,Which British Military leader had a horse called Copenhagen,Duke of wellington,General, The symbols used on a map are explained by the ________.,Legend +Music,"Was George Clintons Nickname ""Dr Funkenstein"" Or ""The Godfather Of Funk""",Dr Funkenstein,General,What popular american writer coined the word nerd,Doctor seuss,Music,"According To The Song 'The House of the Rising Sun' By The Animals, In Which Us City Is The House Of The Rising Sun?",New Orleans +General,Carl and the Passions changed band name to what,Beach Boys,Geography,In which city is the Canale Grande,Venice,Entertainment,"Who played Brad Pitt's cop partner in the movie ""Seven""?",Morgan Freeman +General,The human body contains enough sulphur to kill ______,All the fleas on a dog,General,What part of a bathroom tap most often needs to be replaced,Washer,General,Which singer had top ten hits in the 1980s with Sledgehammer and Games Without Frontiers,Peter gabriel +General,Orange who wrote 'a clockwork orange',Anthony burgess,General,Who was the first commoner to appear Royal Mail pack 1964,William Shakespeare,General,Which city lies at the eastern terminus of the Trans-Siberian railway,Vladivostock +Sports & Leisure,What Is The White Ball In Bowls Called ,The Jack ,Science & Nature,Who first transmitted radio signals across the Atlantic?,Enrico Marconi,General,What is the fear of bulls known as,Taurophobia +General,Who Released An Autobiography Called Making Waves?,David Hasselhoff,General,Captain Macmorris only ever Irishman in what Shakespeare play,Henry V,General,In what Australian state would you find Surfers Paradise,Queensland +General,In the next 7 days 800 Americans will be injured by what,Their Jewellery,Science & Nature,What large herbivore sleeps only one hour a night?,Antelope,General,If you had Tritonopia what would you not see,Colour Blue +Entertainment,"In the film 'Home Alone', who played the baddies?",Joe Pesci and Daniel Stern,General,"In football, what are the white marks intersecting each five-yard line",Hashmarks,General,Who is the roman counterpart of zeus,Jupiter +General,Which country uses the international vehicle registration letters MA,Morocco,Entertainment,Who wrote the Nutcracker Suite ?,Tchaikovsky,Geography,What Was The Name Of Sir Francis Drakes Most Famous Ship ,The Golden Hind  +Food & Drink,What Type Of Vegetable Is A Marrow Fat ,A Pea ,General,"What childhood disease did 312 Americans have in 11993, a record low",Measles,General,"What are the Sirocco, Mistral and Chinook",Winds +General,The town of Whitby in North Yorkshire stands on what river?,River Esk,Sports & Leisure,Who Inflicted Amir Khan's First Ever Professional Defeat On The 6th September 2008 ,Breidis Prescott ,General,VH international airline registration letters what country,Australia +Music,"The Title Of Brian Eno's Song ""Kings Lead Hat"" Is An Anagram Of Which Group's Name",Talking Heads,General,What is a young lion called,Cub,General,"What links Brazil, Uruguay, Mozambique and Angola",Colonies of Portugal +Tech & Video Games,What is the signature move of the Bombs in the Final Fantasyseries? ,Exploder,General,What two body organs benefit from cardiovascular exercise,Heart and lungs,General,Annie Mae Bullock became famous under which name (both),Tina Turner +General,U.S. Captials - Mississippi,Jackson,Entertainment,"Who released 'Time, Love and Tenderness' in 1981?",Michael Bolton,Geography,"The U.S. state of __________ has 3,500 miles of coastline.",Maine +General,"Which singer is known by the nickname of ""Fat Lucy""",Luciano pavarotti,General,What food was invented in a sanatorium in 1890,Kellogg corn flakes,General,As clear as a _______,Bell +Food & Drink,In which Dickens novel would one find this service 'United Metropolitan Improved Hot Muffin and Crumpet Baking and Punctual Delivery Company ' ,Nicholas Nickelby ,Music,The Pop Trio Dee Lite Are From Which 3 Countries (PFE),"USA, USSR, Japan", History & Holidays,What actor was killed in a car crash on the way to participate in a sports car race? ,James Dean  +Entertainment,Name Dennis the Menace's next door neighbors.,Mr and Mrs Wilson,General,What catholic bishop was killed in rome on february 14 in 270 ad,St,General,What was/is the giant Musashi built in Japan in 1974,A Crane +Science & Nature,This organ of the excretory system is composed of small tubules called nephridia?,Kidney,Food & Drink,What Is Tabasco ,A Hot Soup From Mexico ,General,What pigment is missing from the skin of human albinos,Melanin +General,What 3 inventions do Americans say they cant live without,Car - Lightbulb – Telephone,General,Light silvery metallic element,Aluminium,General,"In Greek mythology, actaeon was torn apart by artemis' ______",Dogs +General,Nova scotia is the new name for which canadian province,Acadia,General,What british comedian was the first man to appear on the cover of 'playboy',Peter sellers, History & Holidays,"Which notorious murderer, hanged in July 1953, had his story told in the film _Ten Rillington Place_ ",John Reginald Christie  +Music,Who Had Hits With The Songs “Right Said Fred” & “Hole In The Ground”,Bernard Cribbins,Entertainment,What was the first James Bond Film?,Dr. No,Music,"Who In The World Of Music Is Known As ""The Boss""",Bruce Springsteen +General,The term 'lupine' refers to which animal,Wolf,General,What mountain was Reinhold Messner the first to climb solo in 1980,Mount everest,Art & Literature,"Which Famous Book Begins With The Line 'The mole had been working very hard all the morning, spring-cleaning his little home' ",The Wind In The Willows  +General,On whose 1989 'trash' release were aerosmith and bon jovi guest performers,Alice cooper,Food & Drink,In which South American country is the city of fray Bentos? ,Uruguay ,Geography,Which is the Earth's fourth largest continent,South america +General,In 1979 what did President Carter apologise to Australia about,Skylab falling on their country,General,What magazine started in America March 1923,Time,Art & Literature,"A single print made from a metal or glass plate on which an image has been represented in paint, ink, etc. ",Monotype +General,In which country is the drink Pulque made?,Mexico,General,Which stars name means chained maiden,Andromeda,General,The feeling of having experienced something before is known as _______.,Deja vu +General,Who invented the Spinning Frame,Richard arkwright,General,"Translate: latin ""opus magnum""",Masterpiece,Music,What Relation Is Eagle Eye Cherry To Neneh Cherry,Half Brother +General,What does an aronophobe fear,Internet,General,The young of which animals are called 'brockets',Deer,Entertainment,Name the fastest mouse in all of Mexico,Speedy gonzalez +Art & Literature,What were the dolls in the novel 'Valley Of The Dolls'?,Pills,General,"Which ""daring young man on the flying trapeze"" gave his name to a garment",Jules leotard,Technology & Video Games,"In the Soul Edge series, who is known as ""The Dandy of the South Seas?"" ",Cervantes + Geography,Riyadh is the capital of ______?,Saudi Arabia,General,This science deals with the motion of projectiles,Ballistics,General,Which society founded in 1884 still provides much of the intellect and political stimulus of the Labour Party?,Fabian Society +General,"In Greek mythology, who was jason's wife",Medea,General,Here comes the judge is from what TV show,Rowan and Martins Laugh In,Music,Which Song Was Michael Jackson Performing When Jarvis Cocker Invaded the Stage In 1996?,Earth Song +General,What is a group of tigers,Streak,General,Which herb is used to flavour the tomato based sauce on a Pizza,Oregano,General,What nationality was the person who ordered the killing of the last incan king of peru,Spanish +General,In what sport is the cy young trophy awarded,Baseball,General,On the Omen series Damien was the devil - what second name,Thorne,General,In Greek mythology Clio was the muse of what,History +General,If a Turkish judge breaks a pencil what does it mean,Your death sentence,General,Where was the first Miss World contest held in 1951,London,People & Places,Who Did Cynthia Powell Marry In 1962? ,John Lennon  +Science & Nature,This ancient attempt to transmute base metals into gold was called _______.,Alchemy,Art & Literature,In Which Gilbert & Sullivan Operetta Does Nakipoo Appear ,The Mikado ,General,What is the flower that stands for: attachment,Indian jasmine +Food & Drink,A Chablis wine comes from which French wine region ,Burgundy ,General,What was the nickname of William I of England,The conqueror,General,What does the name Tabitha mean,Gazelle +General,A native of the east end of London,Cockney, Geography,What is the largest of the pyramids in Egypt?,Cheops,Music,Guess The Band From These Initials FM / RT / BM / JD,Queen +General,Who declined a Pulitzer Prize for his book Arrowsmith,Sinclair Lewis,Sports & Leisure,What Was Officially Recognised As A Summer Olympic Sport At The 1992 Barcelona Olympics ,Badminton ,General,Swiss manufacturer of the Crunch bar.,Nestle +Food & Drink,Which brand of beer does Homer Simpson drink regularly? ,Duff ,General,Which artists work entitled '' My Bed'' shocked the art world when exhibited at the Tate Gallery in 1999,Tracey emin,General,Name one of the countries to join the Commonwealth in 1995.cameroon,Mozambique +Science & Nature," In ancient Rome, auburn_haired puppies were sacrificed to ensure a plentiful __________",Corn crop,Sports & Leisure,In Boxing What Name Is Given To An Illegal Punch To The Back Of The Head ,A Rabbit Punch ,General,What was Goebbels' role when he served under Hitler during WWll,Minister for propaganda +General,In America's Wild West Who Rode A Horse Called Target,Annie Oakley,Sports & Leisure,Over what distance is the Melbourne Cup horse race ran (in metres)?,3200,Science & Nature,Why Was The Jaguar Xj220 So Named ,Top Speed Was 220  +General,Which desert is in south-east california,Mojave desert,General,In what TV series did we meet Perry Masonry,Flintstones lawyer never lost case,Music,Who admitted in a radio interview during The Noughties that she had smoked cannabis in the 1960s?,Cilla Black +Science & Nature,What Is The Number Ten In Binary Notation? ,1010 ,Food & Drink," Iceberg, Boston, and Bibb are types of _________.",Lettuce,General,Caipirina means someone's drink - whose drink,Peasants +General,What make and model of car is closely associated with Nurse Gladys Emmanuel,Morris minor,General,"The last line of which TV show was ""Good bye Margaret""",MASH,General,Vince Lombardi & Brian Picollo both died in _____,1970 +General,What metal impurity makes rubies red and emeralds green,Chromium,General,Who patented the sewing machine,Isaac singer,General,What is the flower that stands for: divine beauty,American cowslip +Sports & Leisure,Whose nickname is Beefy? ,Ian Botham ,General,A flat V shaped missile that returns,Boomerang,General,Who invented the word pandemonium,John Milton – capitol of hell +General,The earliest paper written in Latin is a woman's writing what is it,Invitation to a party,General,What colour is a sunburned turnip,Green,Music,"Which Country Singer Is Known As ""The Coal Miners Daughter""",Loretta Lynn +General,What's unusual about a bobhouse,Its on skis,Music,"Who was the recording engineer on The Beatles Album ""Sgt. Pepper's Lonely Hearts Club Band""?",Geoff Emerick,Food & Drink,What are the two main ingredients to Kedgeree ,Smoked Fish & Rice  +General,What is the ruling planet of the astrological sign Taurus,Venus,General,What day commemorates the Gallipoli Campaign,Anzac day,Sports & Leisure,Who Is The Only English Cricketer To Have Taken All 10 Wickets In 1 Innings At A Test Match ,Jim Laker  +General,What type of animal is a vmi-vmi,Very small pig,Entertainment,What came out of Milton's head?,Steam,General,The cecum is part of the,Large intestine +General,Paraphobia is the fear of,Sexual perversion,General,What is a group of dotterel,Trip,General,Which American state produces the most potatoes,Idaho +General,Lycopersicum esculentum is what common food,Tomato,General,"In 'star wars, james earl jones was darth vader's ______",Voice,General,What is kermit d frog's girlfriend's name,Miss piggy +Geography,Where did the most powerful explosion ever witnessed on Earth occur,Krakatoa,General,Which two fighting ships other than the 'oklahoma' were sunk at pearl harbor,Arizona and utah,General,"What, in general terms, accelerates a chemical reaction without itself being changed",Catalyst +General,"Who cremated on the banks of the ganges river on january 31, 1948",Mahatma,Music,Who Had A No.5 Hit With Da Doo Ron Ron Ron In 1963,The Crystals,Music,"Who Composed The Song ""Happy Birthday Tou You""",Mildred & Patty Hill +Music,Name The Country Music Instrument That Looks Like A Guitar Has A Resonator & Was Invented By The Dopera Brothers Of California,Dobro,General,In what country are Fuji Film rolls made,Germany,Science & Nature,What Is An Analgesic ,A Pain Killer  +General,Which English scientist invented the electric light bulb,Joseph swan,Art & Literature,Who Wrote Gullivers Travels ,Jonathan Swift ,General,"In 'star wars', who was darth vader's body",David prowse +General,Which country is due south of Serbia,Albania,General,How did Rose Nilin's husband Charlie die on The Golden Girls?,He died of a heart Attack while making love to Rose.,General,What creature called reem in Hebrew mentioned in Deuteronomy,Unicorn - but reem has 2 horns + History & Holidays,Who Released The 70's Album Entitled Muswell Hillbillies ,Kinks ,General,Catholic calendar what is the 50 days following Easter called,Pentecost, History & Holidays,From what country did the U.S. buy the Virgin Islands,Denmark + History & Holidays,When Did Napoleon Bonaparte Begin His Reign As Emperor Of France ,1804 ,Entertainment,"When Tweety exclaimed, ""I thought I saw a putty tat!"", who did he see",Sylvester,General,What does a philographist collect?,Autographs +General,What author was born in Petrovichi USSR in 1920,Isaac Asimov,General,Which actor and comedian once appeared as a spoon salesman guest of the hotel in an episode of Fawlty Towers?,Bernard Cribbins,Music,"In 2006 The Group Lordi Won The Eurovision Song Contest, Which Country Did They Represent",Finland +Music,Who Topped the Album Charts in 1990 With “But Seriously”,Phil Collins,General,If a prescription said b.i.d. what would it mean,Twice Daily,Technology & Video Games,How much money do you start with in the game 'Wall Street Kid'? ,500000 +Sports & Leisure,Where are the U.S. Tennis Open Championships held?,"Flushing Meadows, NY", Geography,In which country is the Dalai Lama's palace?,Tibet,General,Shrine margaret thatcher becomes the first woman elected prime minister of,Great +General,Who was the Chancellor of the Exchequer during the 1956 Suez Crisis,Harold macmillan,General,Psychophobia is the fear of,Mind,Music,"Which 1996 Single Sold A Then Record 420,000 Copies In Its First Week Of Release",Babylon Zoo / Spaceman +Music,"Name the group that had a hit with ""Inside"" the musical backdrop to a Levi's TV Ad?",Stiltskin,General,What was the first computer software company to go public on the New York Stock Exchange,Cullinet,General,Heston the american flag first flew over a foreign fort in what country,Libya +General,Where are queen mary's gardens,Regents park,General,When was the first daytime bank robbery,"February 14, 1866",General,What was the first UK TV series filmed in colour 1964/5,Stingray - a puppet series +General,Who became the first father-and-son team to win oscars for the film 'treasure of sierra madre',Walter and john huston,General,A mistress among polygamous people,Concubine,General,Who knows what evil lurks in the hearts of men,The Shadow +General,Which country suffered bomb attacks by ETA,Spain,General,"At any given time, there are 1,800 ______ going on around the world",Rainstorms, History & Holidays,Which U.S. President _banned _ Christmas trees from the White House because he believed cutting them harmed the environment? ,Theodore Roosevelt  +General,"Visual representation of individual people, distinguished by references to the subject's character, social position, wealth, or profession.",Portraiture,Music,Which Group Was The Labels Biggest Selling Act Of The 1990's,Boys 2 Men,General,Greek Myth Clotho spun Lachesis measured Atropos cut what,Thread of a mans life +General,In Greek mythology Atlas was a member of what group,The Titans,General,The country name for which bird is 'merle',Blackbird,General,For what genre of book is isaac asimov famous,Science fiction +General,What does a chromophobic fear,Certain colors,General,Anthobia is the fear of What?,Fear Of Flowers,General,Skimbleshanks was a T S Elliot cat what was his area,The Railway Cat +Entertainment,To gradually decrease in volume?,Decrescendo,General,L'Equipe is French daily newspaper covering mainly what,Sport, Geography,What is the capital of Austria ?,Vienna +General,"Completed in 1966, which is the world's largest opera house",Metropolitan in new york,General,There are 72 scenes on what famous article,Bayeaux Tapestry,General,Name the two main families on Soap?,The Tates and The Campbells +General,Who wrote the play Heartbreak House,George bernard shaw,General,What is the name of the present king of Saudi Arabia,Fahd,General,"In Greek mythology, who was the goddess of the moon",Phoebe +General,What Wimbledon finalist (loser 1879) murdered his wife,St Leger Gould,Science & Nature,"What disease is also known as ""rubella""?",German measles,General,Who used the pseudonym Ellis Bell,Emily Bronte +General,Who is the most filmed author,Shakespeare over 300,General,What was the royal surname 'windsor' originally,Saxe-coburg, Geography,What is the basic unit of currency for Jamaica ?,Dollar +General,The second Beale code was encoded using what ?,The American Declaration of Independence,General,What better name is Mary Westmacott better known,Agatha Christie,Science & Nature,The filament of a regular light bulb is usually made of ________.,Tungsten +General,What British art gallery features an area known as the Turbine Hall?,Tata Modern,General,Which country has the highest % of women in their legislature,Cuba,General,"What did a ship called the Ancon travel through first, on August 15, 1914",The panama canal panama canal + Geography,What country has the oldest constitution that still exists?,America,General,What is the flower that stands for: childishness,Buttercup,General,If you suffered from ozostomia what have you got,Bad Breath - Halitosis +General,Who was a knight of the order of the inverted dragon in Hungary,Count Dracula following his father,General,What channel seperates Denmark from Norway,Skagerrak,General,What food item literally translates as little donkey,Burrito +Art & Literature,Who wrote the 'Dragonriders Of Pern' series?,Anne McCaffrey,General,What's the common term for a cubic decimetre,Litre,General,Who was given the only nobel peace prize award during wwi,International red +Geography,In which state is Walla Walla,Washington,Music,Which 1930's Folk Singer Is Attributed As Having The Biggest Influence On Bob Dylan,Woody Guthrie,General,In which film did jennifer lopez play selena,Selena +General,Name the Marx brothers in alphabetical order,Chico harpo & zeppo,General,"Who played Ming of Mongo in ""Flash Gordon""?",Max Von Sydow,General,Whose equations first suggested that mass and energy are interchangeable,Einstein's +Science & Nature,What does a normal Human Being have 206 of? ,Bones ,General,Lili Hayden played a character (not seen) in a spin off from what,Colombo (Mrs), History & Holidays,What did Harry Potter get for Christmas in his first semester at Hogwarts School? ,An Invisibility Cloak  + Geography,What is the smallest independent state in the world?,Vatican City,General,What name is given to the day after Christmas Day,Boxing day, History & Holidays,Which American Gangster was shot dead outside a Chicago cinema in July 1934? ,John Dillenger  +General,What is the Greek version of the old testament called,The septuagent,Geography,In which state is Hoover Dam,Arizona,Food & Drink,"Mind your p's and q's' was a phrase meant to watch how much you had to drink, but what did the letters 'p' and 'q' stand for? ",Pints And Quarts  +Geography,What is the capital of Dominican Republic,Santo domingo,General,What space craft mapping Venus named 15/16 century explorer,Magellan,General,From what platform does the 'chattanooga choo choo leave pennsylvania station,29 +General,What was ben stiller's character called in 'mystery men',Mr furious,General,"Which opera, based on Bizet's Carmen, is set in a parachute factory",Carmen jones, History & Holidays,"In Which 1984 Film, Did A Young Man Called Zach, Receive A Mogwai For Christmas ",Gremlins  +General,Who flew above sleeping beauty castle from 1961 to 1977,Tinker bell,General,"In Disney's Jungle Book, what kind of animal was 'King Louis'",Orang-utan,General,"In 'startrek', who did william shatner play",Captain james t kirk +General,Which part of the human eye controls the size of the pupil,Iris,Music,"Who Had A Hit In 1989 With ""Fergus Sings The Blues""",Deaccon Blue,Science & Nature,What is a group of whales called?,Pod +General,What are the two hottest months at the equator,March and september,General,What Is Austin Powers Job When He Is Not Being An International Man Of Mystery?,A Fashion Photographer,General,In what city did 8 year old Mozart compose his first symphony,London + Language,What two words make the word 'meld'?,Melt and weld,General,What is the first name of the inventor of braille,Louis,General,What percentage of the earths weight is taken up by the oceans?,Less than 1 percent +General,Old Honiton Genoese and Mechlin all types of what,Lace, History & Holidays,Who Was Henry VIII's Third Wife? ,Jane Seymour ,General,What is the Capital of: Malawi,Lilongwe +General,In WW1 what were Lucifer's,Matches,General,Who is the C E O of Apple Computers,Steve jobs,General,Which animal lays eggs,Duck billed platypus +General,The origin of the word penis is Latin meaning what,Tail,General,Utah the states name comes from Navaho meaning what,Upper,General,Which Rubber Based Product Was Patented In The United States In 1869,Chewing Gum +Food & Drink,Which popular beverage's name is German for 'to store' ? ,Lager , History & Holidays,"In which 1971 Oscar winning film is the main character a tough, no nonsense, pork pie hat wearing New York City detective ? ",The French Connection ,General,What is an angle less than 90 degrees,Acute +Science & Nature, A hippopotamus can run faster than a __________,Man,Science & Nature,"The four stages in the life_cycle of an insect are: egg, adult, pupa, and ________.",Larva, History & Holidays,What did Napoleon have built to commemorate his victories?,Arc de Triomphe +General,What show has the character mike stivic,All in the family,General,What classic rock band sang the song 'Paint it Black',Rolling Stones,General,Which was the first 'indiana jones' film,Raiders of the lost ark +General,"What nation moved its borders eastward and named an island ""Millennium Island"" in order to be the first nation to welcome the new millennium",Kiribati,Music,How Many Faces Are There On The Cover Of The Album “Sgt. Pepper's Lonely Hearts Club Band”,75,General,Techniques & equipment used to extinguish fires & limit the damage caused by them,Fire fighting +Music,What Is the Stage Name Of Harry Webb?,Cliff Richard,General,What Is A Beauty Of Bath?,Apple,Science & Nature,Whats the technical name for the skull ?,Cranium +General,"Where The Devil Are My Slippers"" Are The Final Words Spoken In Which Movie",My Fair Lady,General,Which star is the nearest to Earth?,The Sun,General,A computer does a POST what is a post,Power on self test +Science & Nature,What is the boiling point of water in Fahernheit? ,212 Degrees ,General,"In Empire Strikes Back, what was chewing on the power cables of the Millenium Falcon while Han Solo and company were hiding inside the asteroid cave?",Mynocks,General,"As Of 2010 Just 3 Countries Have Managed To Win The Eurovision Song Contest Back To Back, Ireland Is One Can You Name Any Of The Other Two","Luxembourg, Israel" +General,On night Court what was Bull's IQ?,181,General,What is a group of magpies,Tiding,General,Biological community of interacting organisms and their physical environment,Ecosystem +Music,Who Made A Special Brew In 1980,Bad Manners,General,The author Jules Verne was born in which city,Nantes,General,Who introduced racial segregation in south africa,National party +Mathematics,If you cut through a solid sphere what shape will the flat area be,Circle,Science & Nature,Developed In The Us In 1930 By What Name Is PolyVinyl Chloride Better Known ,PVC , Language,"How do you say ""I Love You"" in German?",Ich liebe Dich + History & Holidays,Israel occupied the Golan Heights. Whose territory was it,Syria,General,Margin Guaging Angle Corner Flooring types of what tool,Trowel,General,Pupik means belly button in what language,Yiddish +General,We know what a veto is but what does it literally mean,I forbid,Art & Literature,Who Is Responsible For Painting The Mona Lisa ,Leonardo Da Vinci ,General,Who wrote _Dido_ and _Pa,Joan aiken +Sports & Leisure,In Formula One what does it mean when they are waving a black flag? ,A Driver Has Been Disqualified ,General,Impressionism comes from painting Impression Sunrise - Artist,Claude Monet,Science & Nature, The hippopotamus has the world's shortest __________,Sperm +General,What was the name of the first man made object to orbit the Earth,Sputnik i,General,What country has the worlds oldest National Anthem,Netherlands,General,What is the collective term for a body of goods and monies from which future income can be derived,Capital + History & Holidays,What Is The Name Of The Serial Killer In The Film Halloween ,Michael Myers ,General,"Carolyn Weston's novel Poor, Poor Ophelia was the basis for what show",Streets of san francisco,General,Which chemical element is named after the Greek for 'rose'?,Rhodium +Art & Literature,Which novel by Michael Crichton was (best selling paperback) in 1993 ,Jurassic Park ,General,Reconnaissance squadron into where does the 53rd weather reconnaissance squadron fly,Tropical storms, Geography,Which is the Earth's fifth largest continent ?,Antarctica +General,Who released 'tuesday night music club' in 1993,Sheryl crow,General,A fleet of small ships,Flotilla,Music,Name The 3 Members Of Genesis Who All Went On to Have Successful Solo Careers,"Phil Collins, Peter Gabriel, Mike Rutherford" +General,What did Moses receive on Mount Sinai,Ten commandments ,General,Proportionately which creature has the largest brain,The Ant,General,In Indonesian cookery what name is given to meat kebabs served with a peanut sauce,Satay +General,Oedipus married his mother - who was she,Jocasta, Geography,What is the capital of Mexico?,Mexico City,Music,Which Musical Won The Best Picture Oscar In 1968,Oliver! +General,A dark brown infesting insect,Cockroach,General,Who wrote the novel the Cyborg - Basis of the $6 million man,Martin Caidin,General,What term is applied to ethyl alcohol that has been treated with poison to make it unfit for human consumption,Denatured +General,In which British city would you find Murrayfield stadium,Edinburgh,General,Who did madeline kahn play in the film 'paper moon',Trixie delight,General,Who would use an orange stick,Manicurist +General,Where would you find a Fumerole or Solfatara,Hole side Volcano,General,Jacks what's an egg that floats on water,Very old,General,Apollo was the greek god of ______ and _______,Prophecy and Archery Archery and Prophecy Archery Prophecy Prophecy Archery +General,"In military terms, what is the opposite of ""advance""",Retreat,People & Places,Which Comedian Wore A Huge Banana Shaped Shoes On Stage In The 1970's ,Billy Connolly ,General,Ananas is French for what food,Pineapple +Science & Nature,What is another name for tuberculosis?,Consumption,General,Where would you see CDEFLOPDZ,US Eye test chart,General,Where was coinage invented,Lydia +General,Which leader was renowned for his 'Blood and Iron' speech?,Bismarck,General,What protein makes blood red,Haemoglobin,General,"Creamy sauce of butter,egg yolks and vinegar",Hollandaise +General,What is the flower that stands for: activity,Thyme,General,Novel gave Hemmingway nick speaker of the lost generation,The Sun Also Rises,General,Where are the hausa and ibo tribes,Nigeria + History & Holidays,What age preceded the Iron Age,The bronze age,General,What did NASA launch a$100 million search for in 1992,Aliens,Art & Literature,"The play ""Our Town"" is set where?",Grover's Corners +General,What is the name for the creamy liver of lobster eaten as a delicacy,Tomalley,General,Where is sir herbert baker buried,Westminster abbey,General,What is the provincial bird of Yukon,Raven +General,Swansons introduced them in 1953 - what,TV Dinners,General,From what is velvet made ?,Silk,General,In which book of the Bible did Moses die,Deuteronomy +General,Which former Archbishop of Canterbury was burned alive in 1556,Thomas cranmer,General,What does ICBM stand for,Intercontinental ballistic missle,General,Roosevelt had a landslide victory over Hoover in which year,1932 + Geography,What is the capital of Honduras?,Tegucigalpa,General,Country singer Hank Wangford had what profession,Gynaecologist,General,An _____ clock usually wakes you in the morning,Alarm +General,What does 3 d mean,Three dimensional,General,What is the SI unit of illumination,Lux,Music,Who Composed La Boheme,Puccini +General,In London when was the first cricket match held at Lords,1814,General,Peach pear and plum all members of which family of plants,Roses,Entertainment,"Which comedy duo did the famous, ""Who's on first?"" routine?",Abbott and Costello +General,"What film starred rosie o'donnell, rita wilson and meg ryan",Sleepless in,General,What film studio produced most of the 'godzilla' films,Toho,General,Tour de France what colour jersey best Hill Climber wear,Red Polka dot +General,Irene was the greek goddess of ______,Peace,Music,Who Founded Reprise Records,Frank Sinatra,General,Which Cartooon characters name did Orville Burrel adopt as his stage name,Shaggy +Sports & Leisure,"In Judo, What Colour Belt Follows Yellow ? ",Orange ,General,What is sushi,Raw fish,General,Which soccer team in the 1994 world cup had all 11 players' surnames ending with the letters 'ov',Bulgaria +Food & Drink, What kind of nuts are used in marzipan,Almonds,General,"Which company promises ""The Best to you each Morning""",Kellogg's,People & Places,Whose Ear Did Mike Tyson Bite Off ,Evander Holyfield  +General,"Who had a 1999 hit single with ""I Try""",Macy gray,General,What state was founded by Mohammed Ali Jinnah,Pakistan,General,"In 'my favourite martian', who did ray walston play",Uncle tim +General,By law in Denmark before driving you must check car for what,Children Underneath,General,Where was the worlds first air raid in 1849 from hot air balloons,Venice,General,Harold Sakata was badly burned playing what character,Odd Job in Goldfinger +General,Whose first wife was actress Jayne Wyman,Ronald Regan,General,The Diary of Anne Frank' was first published in English under what title,Diary of a Young Girl,General,What is the young of this animal called: Shark,Cub +General,Who is the Patron Saint of desperation,St Jude,General,Whose novels include 'Restoration' and 'The Way I Found Her',Rose tremain,General,What does cyprus have on its flag,Map +General,"In The World Of Television What Was Created By ""Julia Smith & Tony Holland""",Eastenders,General,All of the clocks in what movie are stuck on 4:20?,Pulp Fiction,General,"Which Classic Tv Character Was Played By ""Sorrell Brooke"" Between (1979-1985) & On Film By Burt Reynolds",Boss Hog + Geography,What U.S. city is known as Insurance City?,Hartford,General,Xmas UK children hang stockings what do Dutch children use,Shoes,Food & Drink,In which countries would you find the following city 'Gin Gin '? ,Australia  +General,Which famous U.S. crooner died on a golf course in Spain,Bing crosby,Music,How Many Siblings Does Dolly Parton Have,11,Entertainment,Name Hagar the Horrible's dog.,Snert +Science & Nature, Frogs never drink. They absorb water from their surroundings by __________,Osmosis,General,What 6th century Greek poet is the father of drama,Thespis,General,Who was Ben Hurs rival in the great chariot race,Messala +Science & Nature," The hippopotamus is, next to the elephants, the heaviest of all land mammals. It may weigh as much as 8,000 pounds. It is also a close relative of the __________",Pig,Art & Literature,"Reducing or distorting in order to represent three-dimensional space as perceived by the eye, according to the rules of perspective. ",Foreshortening,Music,Who Recorded The Albums Our Favourite Shop & Home And Abroad,The Style Council +General,The energy which a body possesses by virtue of its motion is called ________,Kinetic energy,General,"What is the world's largest desert, as determined by the least amount of precipitation",The antarctic antarctic,Music,Which Group Had Hits With Pray & Babe In 1993,Take That +General,Theatre at the stairway is the translation of which place,La Scala Milan,Entertainment,"In the opera 'Don Giovanni', what was Leporello?",Servant,Religion & Mythology,"In Greek mythology, who solved the riddle of the Sphinx?",Oedipus +General,The Rausing brothers of Sweden became rich from manufacturing which packaging product,Tetra pack containers milk cartons,General,When was the first play staged at Londons Globe Theatre,1599, Geography,What is the basic unit of currency for Zambia ?,Kwacha +General,Which peculiar bird had a shoe-polish named after it,Kiwi,General,Who founded the 'Habitat' company in 1971,Sir terence conran,General,Japanophobia is the fear of,Japanese +Geography,"Various U.S. cities are named after other countries. You can visit the U.S. city of ________ in the states of Maine, Nebraska, and New York.",Peru,General,"Who wrote ""To err is human to forgive divine""",Alexander Pope essay on criticism,General,What's is the correct name for a female Badger,Sow + History & Holidays,After which famous person in history was the teddy bear named? ,Theodore Roosevelt ,Science & Nature,"In a car, what might be disc or drum? ",Brakes ,General,This instrument measures the velocity of the wind,Anemometer +General,Mnemophobia is the fear of,Memories,General,What does a Pangram contain,All letters in alphabet,Music,"Is This Desire In 1998 Was PJ Harveys Fourth Album, Name One Of The Previous Three","Dry, Rid Of Me, To Bring You My Love" +General,Who were the 'star crossed lovers',Romeo & juliet,Music,What was the Beach Boys first UK No1?,Good Vibrations,Sports & Leisure,In Which Game Might You Come Across A (Dummy) ,Bridge  +General,In 1789 Mutiny on HMS,Bounty,General,What are the identification letters for Chicago's O'hare airport,Ord,General,What is the bookmakers term for betting chances,Odds +Geography,What is the capital of Malaysia,Kuala lumpur,General,The process of splitting atoms is called,Fission,General,"The first ""technology"" corporation to move into California's Silicon Valley was Hewlett_Packard, in 1938. Stanford University engineers Bill Hewlett and Dave Packard started their company in a Palo Alto garage with $1,538. Their first product was an audio oscillator bought by Walt Disney Studios for use in the making of ____________ ",Fantasia +General,The first puck used in an ice hockey game was made of what,Frozen cow shit,General,Which crime novelist has a hero called 'Mike Hammer',Mickey spillane,General,What is the flower that stands for: unfading beauty,Gillyflower +General,Who has a statue in leicester square,Charlie chaplin,General,What lollies are well known for rolling down the aisles at the movies,Jaffas,Science & Nature,"He is known for his theory of ""Evolution"".",Charles darwin +General,Which country borders Russia Sweden and Norway,Finland,Science & Nature,As what is Polaris also known?,North Star,General,What is 65% of sixty,39 +General,"In ""Hamlet"", how does Ophelia die",She drowns,General,In cricket how many times does a full toss bounce,None,Science & Nature,Name the wild dogs of Australia.,Dingo +General,The lack of calcium in the diet causes what condition,Rickets,Entertainment,"In the children's tv series 'Sesame Street', what two characters were roomates?",Bert and Ernie,General,What is a group of this animal called: Mare,Stud +General,Which modern musical instrument evolved from the Sackbut?,Trombone,General,Which country has a map on it's flag,Cyprus,General,What is a group of this animal called: Ape,Shrewdness +General,International Phonetic Alphabet: C,Charlie,General,What would an antipyrhettic drug be used for,Reduce temperature,General,Bognor Java gets is on average 322 days annually what,Lightning strikes +General,Berlin stands on which river,Spree,Music,Ray Parker Junior Used To Perform With A Band Called Raydio Or The Juniors,Raydio, History & Holidays,How many gloves did Michael Jackson wear? ,One  +General,In which country was Ferdinand Porsche born,Austria,General,"In ballet, a pose in which one leg is raised in back or in front with knee bent, usually with one arm raised.",Attitude,General,Who spent years in nashville shopping demos recorded on a cassette recorder,Judds +General,Who had a number one hit with the song 'Two Little Boys',Rolf harris,General,Who shot abraham lincoln,John wilkes booth,Music,Open Road Was The Debut Solo Album Of Which Former Take That Member,Gary Barlow +General,What southern U.S. state capital is said to have been named for the Western & Atlantic Railroad,Atlanta,General,Walter raleigh 1992 - What Democratic hopeful's birth date received a draft lottery #311,Bill clinton,General,"A ""double sheet bend"" is a type of what",Knot +General,What game of chance is physically the most demanding for the loser?,Russian roulette,General,Who played the title role in the 1978 version of 'superman',Christopher, Geography,"What country is surrounded by Brazil, Argentina and Bolivia?",Paraguay +General,What is an Entr'acte in France,Interval in Theatre,General,Who are the Diascuri,Castor and Pollux,General,What magazine says We are Number one in a field of One,Mad +Geography,In Which City Is Marco Polo Airport? ,Venice ,Science & Nature, A newborn Chinese water __________ is so small it can almost be held in the palm of the hand.,Deer,General,Juliette binoch won an academy award for best supporting role in which film,English patient +General,Which King had Daniel thrown into the Lion's den,Darius,General,Oaks of dodona what did the white house have before it had an indoor bathroom,Telephone,General,"In July 1992, Tim Taylor married which member of the royal family",Lady helen windsor +Science & Nature,Which Car Was First Patented In 1909 ,Model T Ford ,General,Spheksophobia is the fear of,Wasps,General,"What type of creatures are the mythical Gelert, Argos, Luath and Covall?",Dogs +General,What's the only bird that can fly upside down,The hummingbird hummingbird,General,Which river flows through Munich,Isar,General,What genre of fiction is honored by the Nebula awards,Science fiction sci fi sci fi +Music,For Which Group Is Charlie Watts The Drummer,The Rolling Stones,General,"Which town in Brazil, 1,000 miles up the Amazon, was at one time the major port for the rubber trade",Manaus,General,Toxiphobia is a fear of _________,Poisons + History & Holidays,Which group had a Christmas No 1 with 'Lilly The Pink'' ,The Scaffold ,General,Which screenwriter has received the most Oscar nominations,Woody Allen,General,The Statue of Liberty stands on which island,Liberty island +General,Name Pluto's moon,Charon,General,British call this bird species tits - what do Americans call them,Chickadees,Geography,Name the sea west of Alaska.,Bering +Entertainment,"Who are the lead singers of the band ""Full Metal Racquets""?",John Mcenroe and Pat Cash,General,"Cocaine purified by heating with ether, and inhaled or smoked",Freebase,General,Alanis Morrisette appeared on what 80's cable children's show?,You Can't Do that On Television +General,Collective nouns a rag of what,Colts,General,Whose name translates as Emperor of all,Genghis Khan,Science & Nature,A group of kangaroos is known as a _______.,Troop +Music,In Which Country Was Bryan Adams Born,Canada,General,Persia became Iran in 1935 what was it before it was Persia,Iran,General,In what country is Legoland,Denmark +General,What is a fear of germs,Spermophobia,General,Who played queen amidala in the latest 'star wars' film,Natalie portman,General,Goldaming in Surrey was the first English town to have what,Electric street lighting +Science & Nature, The albatross drinks sea water. It has a special desalinization apparatus that strains out and excretes all excess __________,Salt,General,Photoaugliaphobia is the fear of,Glaring lights,General,"Who recorded the album ""free for all"" in 1976",Ted nugent +General,A fylfot is a heraldic name for what symbol,Swastika,General,What was unique about Pope Adrian IV,Englishman Nicholas Breakspear,General,What's krypton's state at standard temperature and pressure,Gaseous +General,The word coyote comes from which language,Aztec, History & Holidays,Who Released The 70's Album Entitled Rumours ,Fleetwood Mac ,General,Which company produced the first nylon,Dupont +General,Who created WinnieThe Pooh,A a milne,General,Ageusia is the loss of which sense,Taste,General,Moons of the faithful is the Chinese translation of what fruit,Apricot +General,"In england, what is the speaker of the house not allowed to do",Speak,General,What Is Collected By A Deltiologist,Postcards,Food & Drink,What is the world's best selling wine? ,Lambrusco  + Geography,What is the second largest ocean?,Atlantic Ocean, Geography,What is the basic unit of currency for Nigeria ?,Naira,Music,"Who thought up the name ""Beatles""?",Stu Sutcliffe +General,Which singer/actor was born with the christian names Harry Lillis,Bing crosby,General,What is the flower that stands for: alas! for my poro heart,Deep red carnation,General,Who became the first Prime Minister of Tanganyika in 1961,Julius nyerere +Music,Name The Brothers In Sparks,Ron & Russell Mael, History & Holidays,"In England Children Traditionally Put Out A Christmas Stocking For Santa, But In Holland What Do Dutch Children Traditionally Put Out For Santa To Fill ",A Clogs Or Sabots ,Sports & Leisure,How Many Consecutive Wimbledon Titles Did Bjorn Borg Win ,Five  +General, One who tells fortunes by the stars is a(n)________.,Astrologer, Geography,Which country has Ankara as its capital?,Turkey,General,What has 32 panels and 642 stitches,A football (soccer) +General,Fifty nine what is calcium oxide,Lime,General,The word planet comes from Greek what's it literally mean,Wanderers,Geography,Name the second largest country in South America.,Argentina +General,What is a pregnant goldfish,Twit,Music,"Which Song Recorded By Johnny Cash Starts With The Line ""I close my eyes and picture the emerald of the sea""",Forty Shades Of Green,Music,Jimmy Sommerville Of Bronski Beat Went On To Form What Band,The Communards +Science & Nature, There are about 130 species of __________,Owl,General,What is the nocturnal firefly,Beetle,General,Who once vowed to erect a statue to adolf hitler in kampala,Idi amin + History & Holidays,Which British prime minister died in 1965 ?,Winston Churchill,General,Where did Hendrik the Dutch boy stick his finger,Dyke,General,Which band reached number two in the charts in 1988 with the album The First of a Million Kisses,Fairground attraction +General,Hartford is the capital of which American state,Connecticut, History & Holidays,Who Sang El Condor Pasa (If I Could) ,Simon & Garfunkel ,Science & Nature, Hummingbirds cannot glide or soar as other bird do. They are the only bird that can __________,Hover continuously +General,What do you call a French Canadian pork pie,Tortierre,General,San Francisco by law you can't clean your car with what,Used Underware,General,Beaver What is Barbi's full name,Barbara Millicent Roberts +General,Evil Lynn Was The Girlfriend Of Which Very Popular 80's Cartoon Character,Skeletor,General,What is the common name for a Japanese dwarf tree?,Bonsai, Geography,In which country is Loch Ness?,Scotland +General,On which Far eastern island did Mick Jagger marry Jerry Hall,Bali,General,In what country did red onions originate,Italy,General,Sixty six mhz italy's equivalant to the dollar is ______,Lira +General,What does the hummingbird weigh less than,A penny, History & Holidays,On what day of the week did Solomon Grundy die?,Saturday,General,San Francisco lies on which dangerous geological feature,San andreas fault +General,In which country were modern banknotes first used,Sweden,General,"Who warned: ""there's a cancer growing on the presidency""",John dean iii,General,"If You Suffered From “ Peladophobia ” There Is Something About Certain People That You Really Really Do Not Like, What Is It?",Bald People +General,What name is given to the broad gap between the outermost and the brightest of Saturn's rings,Cassini division,Food & Drink,Which ingredient gives Earl Grey tea its distinctive flavour? ,Oil of Bergamot ,General,In heraldry an animal reguardant is doing what,Looking backwards +General,Of what are walrus tusks made,Ivory,Music,Which Song Ends With The Line God Speed Your Love To Me,Unchained Melody,General,Celcius what is an osteopath,Bone specialist +General,How many sheep are used to produce one angora sweater,None Angora comes from rabbits,Science & Nature,Where Is The Carotid Artery ,In The Neck ,General,In Maryland it is illegal to maltreat which creature,Oyster +General,What type of food is 'Limburger',Cheese,General,"Who composed ""The Fountains of Rome"" and ""The Pines of Rome""",Respighi,General,What part of your body contains vertebrae,Your spine +General,"Which mythical creature has a lion's head, a goat's body, and a dragon's tail",Chimaera,Art & Literature,"In the Dr Seuss books, which elephant hatched an egg?",Horton,Science & Nature,What Is Bronze An Alloy Of ,Copper & Zinc  +General,Little Eva introduced which dance in 1962,The Locomotion,General,What kind of wood is used on Rolls Royce dashboards,Walnut,General,The liquor Curacao is flavoured with what,Orange +Music,What Connects UK Number One Singles For Ozzy Osbourne And Frank Sinatra In 2003 And 1967 Respectively?,Dueted With Their Daughters,General,Which word links a type of bread and a cut of precious stone,Baguette, History & Holidays,Who was the most famous leader of the Carthagenians?,Hannibal +General,James Edgar in 1890 was the worlds first store what,Santa Clause,Science & Nature, Sharks can be dangerous even before they are born. Scientist Stewart Springer was bitten by a sand tiger shark embryo while he was examining its __________,Pregnant mother,Music,"What Was The Highest UK Chart Position Reached By U2 In 1981 With The Single ""Gloria""?",55 +General,What is the fear of smells known as,Olfactophobia,General,"Which car company, originally called Horch after its founder, changed its name to its Latin equivalent",Audi,General,Nossa Senhora da Aparecida is the Patron Saint which country,Brazil +General,What is a Bodhran used in Ireland,Irish Drum over a frame,General,The giant Kvasir in Norse mythology has what power,Wisdom,General,In California it is illegal to do what in a hotel room,Peel an Onion +General,Heroin is derived from which plant?,Opium poppy,Geography,In what country is lahore ,Pakistan ,General,What sport introduced the term southpaw,Baseball +General,The Caucasus lie between which two stretches of water,Black and caspian sea's,General,What was abolished in the british empire in 1807,Slave trading,Music,Who Walked Off Stage On A 1995 USA Tour After Having A Pair Of Glasses Thrown At Him,Liam Gallagher +Science & Nature,From What Nut Would You Get Copra? ,Coconut ,Music,"""Peasants, Pigs & Astonauts"", Released In 1999 Was The Second Album By Which Band",Kula Shaker,General,Which Dickensian character had a nurse called Clara Peggotty,David copperfield +Science & Nature,Which month has a diamond as a birthstone,April,Entertainment,"What band recorded the 1978 hit album: ""Briefcase Full of Blues""?",The Blues Brothers,General,Who died whilst attempting to discover the source of the Nile,Stanley livingstone +Food & Drink,Which Country Is The Source Of Stilton Cheese ,England ,General,U.S. Captials - Ohio,Columbus,Entertainment,"In 'Star Wars', who was C3P0's sidekick?",R2D2 +General,"Who was the first lady to have made the ""old blue dress"" she wore to an inauguration",Rosalynn carter, History & Holidays,Frankish ruler Charles the Great is better known as _________.,Charlemagne,Sports & Leisure,Who did Boris Becker Beat to Win his first Wimbledon title ,Kevin Curren  +General,What is the widest river,Amazon,Geography,"Located in West Africa on the Gulf of Guinea, the nation of Côte d'Ivoire has been officially known as such since 1986. Previously, it was called the ______________",Ivory coast,General,As Of 2008 Which Country Has Had More Winners Of The Eurovision Song Contest Than Any Other,India +General,Egypt Masbout - Armenia Sourg - Japan Koohii what is it,Coffee,Food & Drink,From Which Country Does Edam Cheese Originate ,Holland / The Netherlands ,Science & Nature,Which American Architect/Engineer Invented Geodesic Dome Construction ,Buckminster Fuller  +General,"""The Althing"" Is The Parliament Of Which Country",Iceland,General,Which hereditary genetic disorder is sometimes called Christmas Disease?,Heamophilia,Food & Drink,"Once upon a time, the consumption of foods made with what, often caused ergot poisoning (Holy fire or St. Anthony's fire), resulting in progressive gangrene and/or mental derangement ? Three letters ",Rye  +General,"Four legs good, two legs bad' is a quotation from which novel",Animal farm,General,Philophobia is the fear of,Falling in love,General,"In Greek mythology, whose companions were the argonauts",Jason +General,Tenzin Gyatso became what in 1937,Dali Lama,General,Who killed chicago,Al capone,General,"Which Shakespeare play features the characters Demetrius, Lysander, Helena and Hermia",A midsummer night's dream +Sports & Leisure,Who are the only 3 countries to have taken part in all of the summer & winter Olympic Games (PFE) ,"Great Britain , France and Switzerland ",General,Rennet' is an extract from the stomachs of which animal,Calves,General,"In the nursery rhyme, who were the three men in a tub","Butcher, baker," +General,Which Hollywood smoothie was born Dino Crocetti in 1917,Dean martin,Science & Nature,This term means 'cone-bearing trees'.,Conifers, History & Holidays,Which Founder Of The Organisation For Afro American Unity Was Murdered In Harlem On 21 February 1965 ,Malcolm X  + Geography,In which city is the Arch of Hadrian?,Athens,General,Where was the agen plum first planted,California,People & Places,Which Hollywood Actor Was Taken Into Custody At Sydney Airport In 2006 For Carrying Banned Products Into The Country In his Suitcase? ,Sylvester Stallone  +General,Sicily is the traditional source of which element,Sulphur,General,Scottish Hebrides island is defined a big enough sustain what,One Sheep,General,Duffel bags were made in Duffel - what country,Belgium +General,What is the fear of metal known as,Metallophobia,General,What is the young of this animal called: Pig,Piglet shoat farrow suckling,General,By US Congress law 1832 citizens should do what annually,Fasting and prayer +General,What Norse explorer introduced Christianity to Greenland around 1000 A.D.,Leif ericsson,General,What can last longer without water than a camel can,Rat,General,How many stars are there on the new zealand flag,Four +Food & Drink,What Animal Do You Obtain Venison From ,Deer ,Technology & Video Games,"What does the acronym ""NES"" stand for? ",Nintendo Entertainment System,General,Leonid Kravchuk became president of where in 1991,Ukraine +Food & Drink,Rickets is caused by a lack of which vitamin?,Vitamin D,Geography,Which South American Capital City Is Also The Name For A Variety Of Bean? ,Lima ,Food & Drink,What is the salted roe of a sturgeon called? ,Caviar  +General,A mouse's is bigger than an elephants - what,Sperm,General,In 1959 First astronauts - selection announced by,Nasa,General,What is a halberd a type of,Spear +Art & Literature,"Who is the author of ""Brave New World"" ?",Aldous Huxley,General,"According to the saying , what causes shepherd's delight",Red sky at night,General,Which houses fought the war of the roses,Lancaster and york + History & Holidays,What show featured Nell Carter as a larger than life housekeeper? ,Gimme a Break ,General,How many cylinders does a V-8 engine have,Eight,General,Archimedes lived in which city,Syracuse +General,Herman Zapf designed what,Dingbats font,Music,What is Paul McCartney's real first name?,James,General,"Football Team, buffalo _______",Bills + History & Holidays,*'Afternoon Delight'' was the only major hit for which one-hit-wonder group?* ,Starland Vocal Band ,General,Francis Octavia Smith rode Buttercup in 1950s TV who was she,Dale Evans Mrs Roy Rogers,General,The Academie Francaise ensures the purity of what,The French language +General,What us navy rank is indicated by three stripes,Commander, Geography,Guatemala is the capital of ______?,Guatemala,General,The Process Of Lachrymation Produces What,Tears +General,What rock group uses roman numerals on all of its album covers,Chicago,General,Who made the first practical microscope,Anton van leenwenhoek,General,The French call it L' Herbe Royal what do we call it,Basil +General,What New England peninsula did Bartholomew Gosnold mistakenly name a cape in 1602,Cape cod,Food & Drink,"Often Used In The Production Of Spirits, What Is The Fruit Of The Blackthorn Tree Called? ",A Sloe , History & Holidays,Who ordered the persecution of the Christians in which Peter and Paul died?,Nero +Science & Nature,What Is Dipsomania Another Name For ,Alcholism ,Music,"Which Steve ""Silk"" Hurley Single Was The First House Record To Top The Uk Charts In 1987",Jack Your Body, History & Holidays,At which hospital was the first British Heart Transplant operation performed in 1979? ,"Papworth, Cambridge " +General,In Baltimore it is illegal to take what to the movies,A Lion,Art & Literature,Who wrote 'Gone With The Wind'?,Margaret Mitchell,Music,"What Were The Band ""Yes "" Owners Of",A Lonely Heart +General,What is the fear of smells or odors known as,Osmophobia, History & Holidays,In What Year Was Queen Elizabeth The I Born ,1533 ,Music,What Instrument Does Sheila E Play,Drums / Percussion +General,What shakespearean play features iago,Othello,General,Which European city spends the most on the arts each year,Frankfurt,Music,Who Wrote The Lyrics To Starlight Express,Richard Stillgoe +General,Which fresh water fish has the Latin name 'Esox Lucius,Pike,Music,"Barry Manilow's ""Mandy"" Was The First Major Hit For Which Label",Arista,General,Who succeeded charles de gaulle as president of france,Georges pompidour +Sports & Leisure,What Are The Periods Of Play Called In A Game Of Polo? ,Chukkas ,General,"Which 1946 Louis Reard creation, was nicknamed ""four triangles of nothing""?",The Bikini,General,The Chronic Argonauts was the original title of what SF book,The Time Machine +Tech & Video Games,What does 'IBM' stand for?,International Business Machines,General,"What is defined as 'a dramatic but unstaged musical composition for soloists, chorus and orchestra, based on a religious theme'",Oratorio, History & Holidays,Is an icicle a Stalagmite or a Stalagtite ,Stalagtite  +Art & Literature,What was Lestat's last name?,De Lioncourt,General,What's the largest continent in the world,Asia,General,What country is the world's largest exporter of frog's legs,Japan +Entertainment,"He wrote the operas ""The Magic Flute"" and ""The Marriage of Figaro""",Wolfgang Amadeus Mozart,General,What is the longest running prime time cartoon show,The simpsons,Food & Drink,"Triple sec, tequila, and lemon or lime juice make up which type of cocktail ",Margarita  + Language,"Multiple Meanings: Slamming your hands together quickly, or a venereal disease.",Clap,General,Hugh Lofting created which fictional character,Dr Dolittle,Music,Weird Al Yankovic Had A Hit With A Parady Of A Michael Jackson Song What Was It,Beat It / Eat It +Geography,The Entrance To Which Bay Is Spanned By (The Golden Gate Bridge) ,San Francisco Bay ,General,What is the state bird of Nebraska,Western meadowlark,General,Whose flag has the national arms on one side and the treasury seal on the other,Paraguay +General,What planet did Gustav Holst omit from the planets suite,Earth,General,Whose books describe the career of 'Horatio Hornblower',C s forester,General,"Which area of Dorset which is not an island, is home to Corfe Castle and the town of Swanage?",Isle of Purbeck +General,The Bazuki is a traditional musical instrument of what country,Greece,General,In the film Bambi what is Bambi's first word,Bird,General,Where would you find the Forte and the Foible strong - weak,Sword blade – near hilt and tip +Food & Drink,This meat is used to make scaloppine.,Veal,General,"In a standard game of cricket, what is the maximum number of people that can be on the field of play at any one time during play",Seventeen,General,What does the latin 'carpe diem' mean ?,Seize the day +Music,Chain Reaction Was A Hit For Tina Turner Or Diana Ross,Diana Ross,Science & Nature, The __________ eagle of Africa hunts over a territory of 250 square miles a day.,Bateleur,General,A screwdriver becomes a fuzzy screw when you add what,Peach Schnapps +General,Bob Dylan said you should never trust anyone what?,Over 30,General,In which 1970's U.S. soap did actor John Forsythe star as 'Blake Carrington',Dynasty,General,What Nationality Is Lou Bega Of Mambo No.5 Fame,German +General,Who was the writer of the novel' Anna of the Five Towns',Arnold bennett,General,"In which 1949 film do Spencer Tracy and Katherine Hepbum play, married opposing lawyers in an attempted murder trial",Adam's rib,General,What is the major german airline (member of star alliance)?,Lufthansa +General,When was the Eiffel Tower completed,1859,General,In what country does Disney's Beauty and the Beast take place,France,General,What was the philosophy of Jean Paul Sartre called,Existentialism + History & Holidays,Which Spanish King Sent An Armada Against England In 1588? ,Philip II ,Sports & Leisure,How many players are there on a soccer team?,Eleven,General,Famous fictional character first appeared in Meet the Tiger 1928,Simon Templar Saint +Science & Nature," Guinea pigs were first domesticated by the __________, who used them for food, in sacrifices, and as household pets.",Incas,Science & Nature, There is no mention of cats or rats in the __________,Bible,General,What is the only land mammal native to New Zealand,Bat +General,What does 'n.f.l' mean,National football league,Science & Nature,What travels in gaggles?,Geese,Art & Literature,"What story features flopsy, mopsy and cottontail?",Peter Rabbit +General,Place where weapons are made or stored,Arsenal, History & Holidays,Where was Napoleon defeated?,Waterloo, History & Holidays,Which Singer Was 'Driving Home For Christmas'' In 1988 ,Chris Rea  +General,What was the only team to win two world series in the 1980s,Los angeles,General,In what Australian state would you find Ipswich,Queensland,General,The application of science to law?,Forensic science +General,Where would you find the Spanish steps,Rome,General,"Which flag is similar in design to the Union Jack but is made up of red, white and green?",Basque Flag,General,In what TV series did we meet Admiral Nelson,Voyage to the bottom of the sea +General,What country officially limits women to one child,China,Music,Who made his debut in the singles chart 26 years ago with ‘Watching the Detectives’?,Elvis Costello,General,"Which Saint was canonised in 1920, 489 years after she was burned at the stake",Joan of arc +General,"What are or were Tester, Royal, Mark and Noble",Old English coins,General,What do you call substances that will not let thermal heat pass through them,Insulators,General,"Extensive cultivation of plants to yield food, feed, or fiber; to provide medicinal or industrial ingredients; or to grow ornamental products",Crop farming +Science & Nature,What type of animal is a wallaby?,Marsupial,General,What Form Of Transport Still In Use Today Was Invented Back In 1760 By Joseph Merlin?,Roller Skate,General,Who became president of south africa in 1989,F.w de klerk +General,In the Flintstones what was Betty Rubbles maiden name,Betty Jean McBricker,General,Neptune was the roman god of the ______,Sea,General,"What does thumper call ice in ""bambi""",Stiff water +General,Kyphophobia is the fear of,Stooping,General,Clearly the best player on this channel is?,Mingtea,General,Who invented the smallpox vaccine,Edward jenner +General,What is the Capital of: Venezuela,Caracas,General,Which Country's Flag (Bizarrely) Has A Garden Hoe And A Kalashnikov Rifle Inside A Star,Mozambique,General,In Morse code what letter is represented by .- Dot Dash,Letter A +General,Air under pressure greater than that of the atmosphere,Compressed air,General,Who was the first person to present an Oscar (Academy Award) to himself?,Walt Disney,General,A pearmain is what type of fruit,Apple +General,"Whoose lasting testament was ""Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et generaliter nullam in infinitum ultra quadratum potestatem in duos ejusdem nominis fas est dividere: cujus rei demonstrationem mirabilem sane detexi. Hanc marginis exiguitas non caperet."" ?",Fermat,General,Which is the only liquid metal element at room temperature,Mercury,General,"After spending hours working at a computer display, what colour will a blank piece of white paper probably appear to be",Pink +General,What first crossed the English Channel in 1959,Prototype hovercraft,General,How loud can a human snore be,69 decibels,General,What is Winnie The Pooh's real name,Edward bear + Geography,Where is area 51 generally said to be?,Groom Lake,General,Who was the star of The Sixth Sense,Bruce Willis,General,For what did the knights of the round table search,The holy grail +Entertainment,"In 'La Traviata', who sings 'Sempre Libera'?",Violetta,General,What tv show was hosted by wine nipping culinary artist graham kerr,Galloping gourmet,General,The Aga Khan is the leader of the largest branch of which religious sect?,Ismali Muslims +General,Which dance in treble time originated as a peasant dance in France and was adopted by the French court in the 18th century,Minuet,General,"Countries of the world:central Europe, the capital is Bratislava",Slovakia,General,What is the name given to the switching of letters in an expression (e.g. saying Jag of Flapan instead of Flag of Japan)?,Spoonerism +Food & Drink,What Was The Original Name For The Sweets Now Known As Starbursts? ,Opal Fruits ,Science & Nature,Coal is composed of which element? ,Carbon ,General,Who invented the geodesic dome,Buckminster fuller +General,Which American state is known as the Lone Star State ?,Texas,Sports & Leisure,Which Austrian Won Four World Downhill Championships In The 1970's ,Franz Klammer ,Science & Nature,The Komodo Dragon is native to which country? ,Indonesia  +General,"The ""o"" when used as a prefix in irish surnames means what",Descendant of,General,Who wrote the Brandenburg Concertos,J s bach,Music,With Which Particular Instrument Would You Associate Gerry Mulligan,Baritone Sax +General,Name the science fiction writer who lives in Sri Lanka,Arthur C Clark,General,Name the eating disorder that suppresses the urge to eat causing the sufferer to lose huge ammounts of weight,Anorexia,General,"Which British king said 'I don't like abroad, I've been there'",George v +General,Nosemaphobia is the fear of,Illness,Sports & Leisure,The Pen Hold Grip Is Often Used In Which Sport? ,Table Tennis ,Science & Nature,What sort of creature is a fluke? ,Worm  +General,The city of Rouen stands on which river,Seine,Sports & Leisure,What does TKO stand for?,Technical Knock Out,General,"On The London Underground What Is The Only Line To Connect To Every Other Line On The System, At Some Point",Jubilee Line +General,The nationalist Chinese occupy this island,Taiwan formosa,General,What make of car is an espace,Renault,Science & Nature,What are the pouched animals called?,Marsupials +General,What is the Capital of: Iceland,Reykjavik,General,"Which character did Lewis Carroll's Alice meet, sitting on a mushroom smoking a 'long hookah'",The caterpillar,General,Which balding actor donned a pigtail for his role in the film Medicine Man,Sean connery +General,"Which film, directed by Sydney Pollack, won the 1985 Academy Award for Best Picture",Out of africa,General,In Which US City Were The Band REM Formed,Anthens (Georgia),General,"What TV actress co-starred in 1986's ""Howard The Duck""?",Lea Thompson +General,"In cooking, what are Brochettes",Pieces of meat cooked on a skewer,General,"On a dartboard, what number is on top",20,General,What is the second longest word in the English language,"Antidisestablishmenterianism""" +General,"The leaces of the tomato plant are poisonous, they contain ___.?",Strychnine,Sports & Leisure,Which cricket player holds the world record for the highest individual score in first-class cricket?,Brian Lara,General,How many letters are used for roman numerals,Seven + Geography,What is the basic unit of currency for Tonga ?,Pa'anga, History & Holidays,Which band had a 1977 summer No 1 hit with `So You Win Again'? ,Hot Chocolate ,General,What's the only element capable of tarnishing and damaging gold,Mercury +General,In a Horney Monkey there is banana crème de menthe and what,Baileys,General,What's the official language of st lucia,English,Music,"Which Singer Was Born In Brixton London, On The 8 th January 1947?",David Bowie +Entertainment,What do the initials B.B. stand for in B.B. King's name?,Blues Boy,General,Mr Cat Poop was the Chinese translation what Nicholson film,As Good as it Gets,General,What nationality was tennis star vitas gerulaitis,American +General,Which album is on the Billboard top 200 the longest since 1973,Pink Floyd Dark side of the Moon,Religion & Mythology,Who was the Greek god of wine?,Dionysus,Geography,What city was filmore west in ,San francisco  +General,An elephants penis is shaped like what,Letter S,General,In what game might you use a flat stick called a kip,Two Up,Entertainment,Which TV horse could talk?,Mr. Ed +Entertainment,Name the musical film named after a state.,Oklahoma,General,"In 1940, Walt Disney's _________ was the first film in history to use stereophonic sound. ",Fantasia,Geography,Which Is The Largest Of The Greek Islands ,Crete  +General,Whose victories in the 1936 Olympics upset Hitler,Jesse owens,Music,What Was Carly Simons Biggest UK Hit Allegedly Written About Warren Beatty,You're So Vain,General,What is the fear of taking medicine known as,Pharmacophobia +General,The pharaoh hound is the only dog that does what,Blushes - Nose and ears redden,Geography,Which Italian City Is Famous For Its Leaning Tower ,Pisa ,Science & Nature," There are no wild deer of any kind in Australia, and the small red deer is the only one found in __________",Africa +General,The Prince of Demons in the new testament was called ___________,Beelzebub,General,Where is the fashion capital,"Milan, italy",General,Andy Green first broke the sound barrier on land in what car,Thrust SSC +General,"Why is so hard to start a fire in La Paz, Bolivia",Little oxygen, Language,What is the Old English word for 'sneeze'?,Fneasan, Geography,In which city is Wembley Stadium?,London +General,"Which is the nearest galaxy to our own, excluding the Magellanic Clouds",Andromeda,General,Which 19th Century novelist's writings included travel sketches called Pictures From Italy and American Notes,Charles dickens,General,What links - Goa - Kerula - Assam - Bihar,India +General,What is the essential ingredient in a Mornay sauce,Cheese,General,What is a group of oysters,Bed,General,In which film did Gregory Peck pretend to have his hand eaten by a rock,Roman holiday +General,Name the largest Mediterranean island,Sicily,Entertainment,In this 1968 film the husband of an unsuspecting young wife becomes involved with a witch's coven.,Rosemary's Baby,General,Thomas and Martha were the parents of which hero,Batman +Music,Which Famous Fashion Designer Was Co-Owner Of Malcolm McLarens Sex Cloths Shop In Londons Kings Road,Vivienne Westwood, Geography,What is the capital of Equatorial Guinea?,Malabo,General,Who ran the first marathon,Phidipedes +General,What currency consists of 100 Groschen,Austrian Schilling,General,Effect A complex alcohol constituent of all animal fats and oils,Cholesterol,General,What was the first event decided at the 1896 olympics,Triple jump +General,With which musical instrument do you associate Jacqueline Du Pre,Cello,General,When was insulin discovered,1922,General,What is a Caldera,Depression left by volcano +General,What are Ren and Stimpy's last names?,Ren Hoëk Stimpy Cadoogan,Music,Number of Beatles kids,10; grandkids: 1,Music,In Which Year Did Bucks Fizz Win The Eurovision Song Contest,1981 +General,The international vehicle registration letters RCH denotes which country,Chile,Toys & Games,Frank Lloyd Wright son John invented these after watching workers move timber.,Lincoln logs, Geography,What is the capital of Iran ?,Tehran +General,In Star Trek who rules the Ferengi,Grand Nagus,General,"In solo whist, how many tricks must a player take in order to win a call of ""Abundance",Nine,General,"What - advertised phrase ""Even your best friends wont tell you""",Listerine mouth wash +General,"In 1986 which Woody Allen film won Oscars for Best Supporting Actor and Best Supporting Actress, as well as for Best Original Screenplay",Hannah and her sisters,General,What country consumes the most coffee per capita 25 Lb,Finland,General,"Creation of programs, databases etc.for computer applications",Authoring +General,Who wrote The World According to Garp,John irving,General,What would be the defining characteristic of a narcissistic person,Vanity, History & Holidays,Whose Death In 1965 Prompted President Johnson That All American Flags The World Over Be Flown At Half Mast A Mark Of Respect Never Before Accorded To A Foreigner ,Winston Churchill  +General,What language gave us the words kimono & futon,Japanese, History & Holidays,Who was the first u.s president born outside the orginal 13 states ,Abraham lincoln ,General,What is 'old faithful',Geyser +General,Who wrote the vampire series that featured lestat as the main character,Anne,General,In Verdi's opera Rigoletto what is the occupation of Rigoletto,Court jester,Art & Literature,Who Wrote The Adventures Of Huckleberry Finn ,Mark Twain  +General,Only humans and what primate can have blue eyes,Black Lemurs,General,In what city would you find the Spanish Riding School,Vienna,General,Which group's first release was 'i ain't gonna eat out my heart anymore',Rascals +Music,Which American Singer Came Out As A Lesbian At A 1993 Presidential Inauguration Ball For Bill Clinton,Melissa Etheridge,General,The Jelbukk is a Swedish Xmas decoration - what is it,Straw Goat,General,What kind of creature is a centaur,"Half man, half horse" +General,What 80s band had a hit with Tainted Love,Soft-Cell,Science & Nature,How many chromosomes do each body cell contain?,Forty six,General,"Using morse code, what is trasmitting using 3 dots, 3 dashes and 3 dots",Distress signal +General,"Buddy Holly Surprisngly Only Topped The Uk Singles With One Song ""Can You Name It""",Doesn't Really Matter Anymore,Entertainment,"Name the Disney cartoon in which the character ""Belle"" appears.",Beauty and the Beast,General,Vegetarians do what more than carnivore / omnivores,Fart +General,How did Scotland's Robert the Bruce die,From Leprosy,Music,"5 Members Of The Original Human league Line Up, Split To Form 2 Other Bands (PFE)","Heaven 17, ABC",General,In the acronym 'laser' what does the L stand for,Light +General,The Greek version of what is called the Septuagint,Old Testament,General,The average man or woman spends one year of their life - what,On the Telephone,Food & Drink,Which US state exports the most wine? ,California  +General,What animal tailhairs are traditionally used in making violin bows,Horse,General,A female pigeon cannot lay an egg unless she sees what,Another pigeon – or reflection,Food & Drink,What Traditionally Might You Eat On Shrove Tuesday ,Pancakes  +General,Who directed the film Gandhi,Richard attenborough,General,What character was played by phil silvers on the phil silvers show,Sgt ernie,Science & Nature,Which Bird Was Depicted On The Rear Of The Farthing Coin ,Wren  +General,"In 1793, _____ reaches the pacific",Alexander mackenzie,General,Name the wife who survived Henry VIII,Catherine parr,General,"Which Famous Singer Fronted A Band Called ""Grand Central"" With Prince On Guitar",Alexander O Neil +General,What is an eidograph used for?,Enlarging or Reducing Drawings,People & Places,Which President Of America Was A Peanut Farmer ,Jimmy Carter ,Sports & Leisure,Who was Ben Johnson running for when disqualified in Seoul? ,Canada  +General,In which USA state is Raleigh the capital,North carolina,Science & Nature,In 1888 What Did George Eastman Call His Hand Held Box Camera The First Of Its Kind ,The Kodak Camera ,Science & Nature,In What Environment Did Ichthysaurs Live ,Water  +General,What is the bundestag,German parliament,Music,What Was A Castrato,"A Male Singer, Castrated As A Boy To Maintain A Child's Pitch",General,What was Bogart's full characters name in Casablanca,Rick Blain + Geography,What river has the largest drainage basin?,Amazon,General,"In Which Movie Will You Find The Characters “Mikey Walsh, Lawrence Cohen, Clark Devereaux Stef Steinbrenner & Richard Wang”",The Goonies,General,Who sang for Lauren Bacall in To Have and Have Not,Andy Williams + History & Holidays,Who Released The 70's Album Entitled In Rock ,Deep Purple ,General,Where would you find the yoruba people,Nigeria, History & Holidays,What Country Is Dracula Said To Come From ,Transylvania  +General,"On Three's Company,what is the name ""Chrissy"" is short for?",Christmas,Food & Drink, Is a schnitzel a sweet or a savoury?,Savoury,General,What is the Capital of: Cocos Islands,West island +Geography,In Which US State Is The City Of New Orleans? ,Louisiana ,General,Who wrote the book 'Portrait of a Lady',Henry james,General,What is the fear of russians known as,Russophobia +General,Every citizen of Kentucky must do what by law annually,Take a bath,General,"Which Us State Goes By The Nickname ""The Sooner stae""",Oklahoma,General,Name the British painter who is the grandson of Sigmund Freud,Lucian freud +Music,Name The Scaffold First Chart Single,Lily The Pink,Sports & Leisure,The Americas Cup Is In Which Sport ,Sailing ,General,Parathesia is a medical condition with what common term,Pins and Needles +Geography,Which of the U.S. states borders only one other state,Maine,General,Which comic character was dynamited to death in issue 428,Robin,General,Montezuma's nephew Cuitlahac name means what,Plenty of Excrement +Music,"Instrumentally , What's The Connection Between Jimmy Smith, Booker T, & Billy Preston",Organ,Music,Who Founded The Motown Label,Berry Gordy Jnr,General,Berry Gordy Was The Founder Of What Famous Corporation,Motown +Music,What Does The M Stand For In The Band M People?,Manchester,General,Which Swedish naturalist developed the system of plant and animal classification which is still used today,Carolus linnaeus,General,How did Lavan in Utah get its name,Its Naval backwards middle of Utah +General,A lion and a sword appear on what countries flag,Sri Lanka,Music,Cash & Carry First Recorded The Birdie Song But Who Had The Hit,"""The Tweets""",People & Places,What Is Elton Johns Middle Name ,Hercules  +General,"What famous brothers lived at hawthorn hill in dayton, ohio",Wright brothers,General,Tokyo is the capital of ______,Japan,Music,When Sandie Shaw Won The Eurovision Song Contest in 1967 There Was Something Unusual About Her Performance Which Has Never Been Repeated What Was It,She Performed Barefoot +General,In Dyersberg Tennessee its illegal for a woman to what to a man,Call for a date,General,"""Myosotis"" Is The Latin Name For Which Type Of Flower",Forget Me Not,General,Theatrophobia is a fear of ______,Theatres +General,"Who Provided The Backing Vocals On Lionel Richies Song ""All Night Long""",Richard Marx,General,Who failed his entrance exams to school aged 16,Einstein – Zurich poly,General,Temporary encampment without tents,Bivouac +General,In which ocean would you find the islands of Sao Tome and Principe,Atlantic,General,The Jewish prayer for the dead Kaddish is in what language,Written Hebrew but in Aramaic,Science & Nature,What name is given to a chemical reaction which gives out heat ?,Exothermic +Music,"Who Endorsed ""Love In An Elevator""",Aerosmith,Science & Nature,What Is Charles E 'Chuck' Yeagers Claim To Fame ,First Man To Fly Faster Than Sound ,General,What literary character was born on September 22 1290,Hobbit Bilbo Baggins +General,Dephlogisticated air' was the name given by Joseph Priestley to which gas,Oxygen,General,At Waterloo who commanded the Prussian troops,Marshal Blucher,General,Sperrylite is the ore what is extracted,Platinum +General,What country invented cheesecake,Greece,Sports & Leisure,How Many Diciplines Are There In Mens Gymnastics ,"6 (Vault, Rings, Floor, High Bar, Para Bars, Horse) ",General,Wool sorters disease is actually what,Anthrax +General,What term was coined July 17th 1942 issue of Yank magazine,G I Joe,General,As what is the Devonian period also known,Age of fish,General,In Texas it is illegal to have oral sex with what,A Chicken +General,What name is given to small or immature cucumbers usually grown to be pickled,Gherkins,Music,"""Love And Understanding"" Was A Hit In 1991 For Which Singer",Cher,General,"Who wrote the opera ""the trojans""",Hector berlioz +Religion & Mythology,"Who, in Egyptian mythology, is the god of the dead?",Anubis,Science & Nature,"Epidermal cells, palisade cells, and veins are parts of a(n) ________.",Leaf,General,What was the name of the Jester in As You Like It,Touchstone +General,Charcoal Sulphur Saltpetre make what,Gunpowder,General,What product was advertised by the first commercial to portray nudity on us televison (1987),Viola brassieres,Music,Which Fiddle Playing Singer Is Backed By His Band Union Station,Alison Krauss +Science & Nature, A skunk will not __________ and throw its scent at the same time.,Bite,General,The letter B comes from Egyptian hieroglyphics meaning what,House,Science & Nature,Name the largest living bird.,Ostrich + History & Holidays,"Which Coronation street character, played by Jean Alexander, made her last appearance on Christmas Day 1987 ",Hilda Ogden ,General,"Admiral', 'soda', & 'zero' ultimately derive from which language",Arabic,General,"Who was known as ""The Father of the H Bomb""",Edward teller +Science & Nature, An ostrich's eye is bigger than its __________,Brain,General,If you were drinking Cobra beer in what country would you be,India,Geography,Which City Is The Capital Of The Phillipines ,Manilla  +General,In Topeka Kansas its illegal to install what in your house,Bathtubs,General,Which Company Published The Harry Potter Novels,Bloomsbury,General,What is the young of this animal called: Beasts of prey,Whelp +General,Who is the only real person to ever have been the head on a pez dispenser,Betsy ross,Food & Drink,Cumberland Sauce Is Tradiotionally Served Cold With What ,Venison ,Music,"Which Guitarist Joined RCA In 1947 & Was Put In Charge Of The Label's Nashville Studio In 1955, Where He Helped Further The Careers Of Among Others, Jim Reeve's , Don Gibson, Elvis Presley",Chet Atkins +General,Newkirk c3p0 is the first character to speak in which film,Star wars,General,How much does a cubic meter of water weigh?,One Ton,General,What english county has the smallest perimeter,Isle of wight +General,Guy Blaine is the latest boyfriend of which famous lady,Barbie,General,What is the name of the cat in the smurfs?,Azriel, History & Holidays,What 'SN' Do You Call An American Holy Man Who's Strapped For Cash ,Saint Nickle-Less  +General,What album cover (by the Rolling Stones) had a zip on the side,Sticky Fingers,General,The English city of Salisbury stands on which river,Avon,General,Kaka means parrot in which language,Maori +Art & Literature,Who was the human companion of Willow?,Mad Mardigan,General,What was the name of Rip Van Winkles dog,Wolf,General,Who built the 'Cherokee' and 'Comanche' aircraft?,Piper +General,In Which Country Will Yound Find The Worlds Largest Whiskey Distillery,Japan (Suntori),General,What is the fear of bald people known as,Peladophobia,General,How many strings on a seven string guitar?,Seven +General,What sport uses a map & a compass,Orienteering,General,The word Sahara is Arabic for what,Desert,General,In Hebrew what does Am rotze min mean (ah-knee ro-tsay),I want sex +General,Kerkira is the Greek name for what Island,Corfu,General,What famous Wham! frontman went on to record a multiplatinum record in 1987?,George Michael,General,What is the smallest frog,Gold frog +General,What is the capital of california,Sacramento,Music,"Who Had A Hit With ""Heaven Is A Place On Earth"" In 1987",Belinda Carlisle,General,Which quiz program never had contestants only contenders,Mastermind +General,"Who, in egyptian mythology, is the god of the dead",Aker, History & Holidays,In Which Tv Show Might You Encounter 'The Mystery Machine' ,Scooby Doo ,Geography,Which Is The Largest Island In The Caribbean? ,Cuba  +People & Places,Who Is Gazza ,Paul Gascoigne , Geography,What is the capital of The Bahamas ?,Nassau,General,What country do Great Danes come from,Germany +General,What vegetable comes last in the dictionary,Zucchini,General,"What is the ""bubonic plaque"" is know as the_________",Black death,Art & Literature,"In 'Romeo and Juliet', who said 'I have a faint cold, fear thrills through my veins'?",Juliet +General,Branch of biology dealing with the development of the animal embryo,Embryology,General,What has albumen and a yolk,Egg,General,What U.S. state borders the most Canadian provinces,Montana +General,On which river does the U.S. capital Washington D.C. stand,Potomac, History & Holidays,Which Famous Horror Actor Provided The Narration to Michael Jacksons Song & Short Mobie 'Thriller' ,Vincent Price ,General,What wwi carrier pigeon helped save the lost battalion,Cher ami +General,What is the sum of 2a + 5a,7a,General,Who was the greek god of the sea,Poseidon,Music,Who Had A Hit With Long Haired Lover From Liverpool,Jimmy Osmond +General,The name Jesse means what in Hebrew,Wealth,General,Spain's equivalant to the dollar is ______,Peseta,General,"What is the English nickname of Beethoven's opus 91, written to commemorate Wellington's victory at Vitoria",Battle symphony +Music,The Appleby Sisters From The Stock Aitken And Waterman Stable Were Better Known As Who,Mel & Kim,Science & Nature, __________ need about 2 tablespoonfuls of blood each day. The creature is able to extract its dinner in approximately 20 minutes.,Vampire bats,General,The Uffizi is a museum and art gallery in which European city,Florence +General,"What is the Capital of: Bahamas, The",Nassau, History & Holidays,Where Was Napoleon Bonaparte Born? ,Corsica ,General,Where did little miss muffet sit,On her tuffet +General,What is the singular of gladioli,Gladiolus,General,What is the Zodiac,A series of twelve fixed stars 12 fixed stars,General,Which order of monks are known as 'Blackfriars'?,Dominicans +General,"With A Border Of 233,000 Miles What Is The Largest Country In Europe","Ukraine (603,000) Sq Km",General,All the pictures of which king are always shown in profile,King of Diamonds,Music,"""You Might Think"" Was A Hit for The Cars Or Billy Joel",The Cars +General,Which new-wave band rocked the Casbah,The Clash,General,What was the canadian literacy rate in 1987,99%,General,Cape Comorin is the most southerly point of where,India +General,What is commited if a person murders his father,Patricide,General,June 1611 what English navigator was cast adrift by mutineers,Henry Hudson,General,"What links Sissinnius, Zosimus, Liberius, Sergius V1,Victor II",Popes +Music,Whose Debut Album Was Entitled Leisure In 1991,Blur,General,Which author whose name means flowering tree got OBE,Ngaio Marsh – mystery writer,General,"In The World Of Science ""Ceres"" Is The Largest Known What",Asteroid +Music,"Who Had A Hit With The Song ""The Greatest Love Of All""",Whitney Houston, History & Holidays,Where did the bayonet originate?,"Bayonne, France",General,Diving sea bird with black plumage,Cormorant +General,"Which us state is named from the spanish for ""snow clad""",Nevada, Geography,Helena is the capital of ______?,Montana,General,American comes from USA what someone from Monaco called,Monagasque +Science & Nature,What Type Of Fish Is The Dog Fish? ,A Shark ,General,And again in 1995 what was the one given to boys in USA,Michael,Food & Drink,What Is Usually Served With A Fruit Fondue ,Chocolate  +General,What Does An Ungulate Animal Have?,Hooves,Geography,In which continent would you find the mekong river ,Asia ,General,With which country did Britain break off diplomatic relations in February 1991,Iraq +General,"Football Team, seattle ________",Seahawks,General,What shook San Francisco in 1906,Earthquake,General,In Germany what are the Neubaustrecke,High Speed Railways +General,What is the study of the composition of substances and the changes they undergo,Chemistry,General,"In one of donald horne's novels, who was 'the lucky country",Australia,Entertainment,"Who played the lead in the movie ""Castaway""?",Tom Hanks +Music,What Was Elton Johns Chart Topping Album Of 1990,Sleeping In The Past,Music,According To the Lyrics Of One Of His Songs Where Did Paul Young Call Gome,Wherever He Laid His Hat,Music,"Who Released An Album In The Guise Of A Fictitious Singer Entitled ""In The Life Of Chris Gaines"" In 1999",Garth Brooks +Science & Nature," The black bear is not always black. It can be brown, cinnamon, __________, and sometimes a bluish color.",Yellow,General,This french actor appeared in 'The Big Blue' and 'The Professional'.,Jean Reno,General,This is required to make all electric things work,Electricity +General,"Vietnam, laos, cambodia, thailand and malaysia is known by what name",Indochina,Geography,What two countries do tyroleans come from ,Austria and italy ,General,This date is the date in the middle of the year.?,2nd July +General,The Dome of the Rock is a shrine in which Middle Eastern city,Jerusalem,Science & Nature,"What Mammal Of Northern Europe, Asia & North America Is Sometimes Called A Glutton ",Wolverine ,General,Jacqueline Du Prey is a master on what instrument,Cello +General,What were the old hecklers in the Muppet show named after,New York Hotels Waldorf Stadler,General,Who's first film (THX1138) flopped in 1971,George Lucas,Science & Nature,Water freezes at __ degrees Fahrenheit.,32 +General,USA has most cars what country has second most,Japan,General,Who is the Patron Saint of bricklayers,St Steven,Science & Nature, There are more than 450 species of __________ throughout the world.,Finches +General,What is an insect called while it is changing its form inside a cocoon,Pupa,Geography,"What continent contains queen maud land, wilkes land and bird land ",Antarctica ,General,A cantaloupe is a small what,Melon +General,Which country in the world produces the most mangos,India,General,Year that Viking I landed on Mars,1976,Science & Nature,At 3 Feet In Diameter The Rafflesia Has The Largest Single Flower Of Any Plant In The World But Where In The World Does It Grow ,Borneo & Sumatra  +Music,"Chuck Berry, Bo Diddley, Howlin' Wolf Name The Label That Linked Them",Chess,Science & Nature,What is the astronomical name for a group of stars?,Constellation, Geography,What is the capital of Hawaii?,Honolulu +Sports & Leisure,How many hoops are used in a game of croquet? ,6 ,General,Between 15 and 20% of what disappear from shops each year,Supermarket Trolleys,General,Who proposed the theory of natural selection,Charles darwin +Science & Nature,What is a group of whales called,A pod,General,What short-lived tv series starred ice-cube,The watcher,General,What greeting did lionel richie take to the top in 1984,Hello +General,In Star Trek Generation what does Captain Picard drink,Earl Grey Hot,General,What nationality is the keyboards wizard Vangelis,Greek,Technology & Video Games,Which Writer established the three laws of robotics?,Isaac Asimov +Science & Nature,How Far Could You Draw Out One Gram Of Gold ,"2,400 Metres ",Science & Nature,The teeth used for biting or cutting are known as _______.,Incisors,Sports & Leisure,Who Was The First Professional Footballer To Score 100 Goals In Both Scotland And England? ,Kenny Dalglish  +General,What is the Capital of: Trinidad and Tobago,Port-of-spain,General,"Who recorded the 1997 album ""Flaming Pie""",Paul mccartney, Geography,"What river is called ""Old Man River""?",Mississippi +Art & Literature,Who Penned the Novel 'The Pelican Brief'' That Was Made Into A Film Starring Julia Roberts And Denzil Washington? ,John Grisham ,General,Who owns: almond joy candy,Hershey,General,Who was napoleon's first wife,Josephine +General,"Who said about criticism "" I cry all the way to the bank """,Liberace,General,"Who sang the Song ""American Pie""?",Don Mclean,Science & Nature,This is the heaviest naturally occurring element.,Uranium +General,Yellow gold contains 10% of,Copper,Music,Who released an album titled Forever Changes?,Love, History & Holidays,Which city is believed to be the birthplace of Jesus of Nazareth? ,Bethlehem  +General,What is abyssinia known as today,Ethiopia,Art & Literature,Who Painted The Last Supper ,Leonardo Da Vinci ,People & Places,Which Actor Was Charged With Lewd Behavoir In 1995 ,Hugh Grant  +Music,Who was Glad All Over in their No1 hit from 1963?,Dave Clark Five,Entertainment,"Who produced, directed and starred in ""Citizen Kane""?",Orson Welles,Music,"Who Dished Up ""Summer In The City""",The Lovin Spoonful +General,"Whose grandson got the first phone call from a commercial cellular system, in 1983",Alexander graham bell's alexander graham bells alexander graham bell,General,Where in the world can you see the sun rise Pacific set Atlantic,Panama on Isthmus,General,An Intente is a players manager in what sport,Jai Alai +Science & Nature," A hippopotamus has a stomach 10 feet long, capable of holding 6 bushels of __________",Grass,Science & Nature,Where Do Birds Of Paradise Come From? ,New Guinea ,General,In an orchestra which instrument has the greatest number,Violins +Art & Literature,What Type Of Animal Is Rupert The Bears Best Friend Bill? ,Badger ,General,Grolsch lager comes from what country,Holland,General,What is a Rocky mountain canary,A Donkey +General,What is measured on the Torro scale,Tornados,Sports & Leisure,Who was the first black athelete to captain Great Britain's men's team? ,Kris Akabusi ,General,"What ""W"" word describes the money people earn for their work",Wages + History & Holidays,Which British coin ceased to be legal tender in 1960? ,Farthing ,Science & Nature,A flat_bottomed conical laboratory flask with a narrow neck is called a(n) __________.,Erlenmeyer,Music,What Was The Stranglers Biggest Hit,Golden Brown +General,Opaque 2 is a modern variety of which cereal crop,Maize,Sports & Leisure,Which Darts World Champion Of The 80's Had the First Names Of John Thomas? ,Jocky Wilson ,General,In 1952 PM Winston Churchill announced that Britain had its own,Atomic bomb +General,Which film actor's real name was Reginald Truscott-Jones,Ray milland,General,Complete this saying 'All ship shape and',Bristol fashion,General,What french word describes the painting technique which uses separate dots of pure colour instead of mixed pigments,Pontillism +Entertainment,Which magician did Lothar assist?,Mandrake,General,What Pulitzer prize winning novelist ran for mayor of New York City,Norman mailer,General,Jack Ketch 1663 1686 had what job,Hangman + Geography,The Little Mermaid is found in the harbour of which city?,Copenhagen,Music,What Was The Title Of John Lennons 2nd Book,A Spaniard In The Works,General,What is the fear of rectum known as,Proctophobia +General,On Earth what would you find in an orbit,Eyeball - orbits eye socket skull,Science & Nature,The tides on the earth's oceans are actually created by gravitational pull from the ____.,Moon,Food & Drink,What Type Of Star Is Awarded To Restaurants Where The food Is Of Exceptional Quality ,Michelin Star  +Sports & Leisure,How Long Does A Game Of Field Hockey Last? ,70 Minutes ,General,Married men do it twice as often as single men – what,Change Underwear,General,"Which Cartoon Character Is Knon As ""Oui Oui"" In France",Noddy +General,What became the biggest use for aluminum starting in 1960,Tin cans,Science & Nature, Ostriches live about 75 years and can reproduce for __________,50 Years, History & Holidays,Who Was The First Us President To Take Up Residence In The Whitehouse ,John Adams  +General,Who wrote the novel 'Les Miserables',Victor hugo,General,What chemical element's discovery in 1774 made bleached paper possible,Chlorine,General,In the 70s who played The Bionic Woman,Lindsey Wagner +General,What is a group of starlings,Murmuration,Science & Nature,What Is The Main Difference Between A Monkey And An Ape? ,Monkeys Have Tails ,General,Instrument for measuring radio activity,Geiger counter +General,What links Mozart's Don Giovanni and Bizet's Carmen location,Set in Seville in Spain,General,With which of his novels did Michael Odantje jointly win the Booker Prize,The english patient,Music,Who Wrote & Performed My Sweet Lord In 1971,George Harrison +General,What was the first nationally released film with a PG 13 rating,Red Dawn,General,What is the scientific study of the body of man and lower animals called,Anatomy,General,Where is Shangri-La supposed to be,Himalayas +General,What is the world's largest water fowl,Trumpeter swan,Music,"Whose First Record Was A Rocker Called ""Ooby Dooby""",Roy Orbison,Food & Drink,What drink did Dom Perignon invent? ,Champagne  + History & Holidays,Which two European leaders signed a non-aggression pact on 23 August 1939? ,Hitler & Stalin ,General,"In Greek mythology, themes was the mother of ______",Moirae, History & Holidays,The ______ Tea Party?,Boston +General,Which artist painted the picture Tahitian Women,Paul gaugin,General,In what tv series is sideshow bob,The simpsons,General,What is another name for the coyote,Prairie wolf +General,What li'l abner animal gave milk & laid eggs,Schmoo,General,"Whose Reading Of Roald Dahl's ""George's Marvellous Medicine"" In 1998 Caused A Storm Of Protest To The Jackanory Office",Rik Mayall,General,What parts of a building does a glazier instal,Windows +Music,Which Singer From Yes Performed With Vangelis,Jon Anderson,Geography,Where was the 1939 world's fair ,New york city ,General,Which 3 olympic events restrain throwers in a circle,"Shot, hammer & discus" +General,For what did the marquis de sade serve 27 years in prison,Sexual offenses,General,Which English King was crowned on Christmas day,William the Conqueror in 1066,General,What has 121 holes,Chinese Checkerboard +General,"About which Prime Minister was it said ""A modest little man with much to be modest about""",Clement attlee, Geography,What is the capital of Lebanon ?,Beirut,General,Who or what might be given an Apgar rating,Baby +General,Which letters denote Jesus Nazareth King of the Jews,INRI,Music,What instrument does Ravi Shankar play?,The Sitar,General,What is the state song of California,I love you California +General,Who was the only horse ever to beat the legendary Man O War,Upset 12 August 1919,Sports & Leisure,Hockey: The Detroit ________.,Red Wings,General,Enid Blyton character name changed to white beard for PC USA,Big Ears +General,What was the royal residence after st james court,Buckingham palace,Music,"Whose Biggest Hit Was A 1982 Single ""Party Fears Two""",The Associates,Science & Nature,Which German Chemist Won The Nobel Prize For Chemistry In 1944 For Discovering Nuclear Fission ,Otto Hahn  +General,Liquid Sunshine was the original slogan of which product,Doctor Peppers,General,Myrastica fragrens is what common spice,Nutmeg,General,The temple at Ephesus was sacred to who,Diana +General,Corson and Stoughton are the inventors of what,CS Gas,Geography,In what country is the highest point in south america ,Argentina , History & Holidays,In which 1970's films does Dustin Hoffman play the following characters ' Carl Bernstein '? ,All the Presidents Men  +General,What is the ninth month of the year,September,General,What is the most famous mausoleum in India,Taj mahal,General,What is the only country that has never imposed censorship for adult films?,Belgium +General,What is the collective noun for a group of crows,Murder,General,Springfield is the capital of ______,Illinois,General,Who Was The 1st Ever Male Centrefold To Appear In Cosmo?,Burt Reynolds + History & Holidays,Biko was involved in what protest movement? ,Apartheid ,General,"What do butterflies eat grubs, nectar or nothing",Nectar,General,What is a dried plum,Prune + History & Holidays,Germany was split into two zones by which agreement?,Yalta agreement,General,What country celebrated its National Day on 15th March?,Hungary,General,What country invented castanets,Egypt +General,What two airlines fly the concorde,British airways and air france british,General,What is the study of animals known as,Zoology,General,What was Bruce Lees first Hollywood produced film,Enter the Dragon +General,A muster is a group of which birds,Peacocks,General,What does one square inch of human skin contain 625 of,Sweat glands,General,"Christie what are mother mary's ""whispered words of wisdom""",Let it be +General,Who was the Chancellor of West Germany in 1989 when the wall came down,Helmut kohl,General,Which Mozart opera is subtitled School for Lovers,Cosi fan Tuti,Music,What Was The First Song To Be Performed In Outer Space,Happy Birthday (During An Apollo Mission) + Geography,What is the capital of Australia ?,Canberra,Science & Nature,What Does BCG Stand For ,Bacillus Of Calmette And Guerin ,General,What is the flavour of a piri-piri sauce,Curry +General,Who wrote the children's novel Swallows and Amazons,Arthur Ransom,General,What do you call the process of stamping a blank coin with a design,Minting,Religion & Mythology,Where did Robin Hood supposedly live?,Sherwood Forest +Music,Who Had A Hit in 1982 With Friends,Shalamar,General,What is a group of woodpeckers,Descent,General,A yamashita would be performed on which piece of gymnastic equipment?,Vaulting Horse +Sports & Leisure,Which Team Have Lost the Most Fa Cup Semi finals ,Leicester City ,General,Who was Ben Casey's boss,Dr Zorba,General,In what kind of restaurant might you be offered 'kulfi' as a dessert,Indian +Geography,Which Gulf Lies Between Iran And Saudi Arabia ? ,Persian Gulf ,General,What was keanu reeves' computer world alias in 'the matrix',Neo,General,What did jack the ripper sign on his first note,Yours truly +General,In which country was guitarist Django Reinhardt born,Belgium,General,The highest USA rank killed WW2 Lt General who killed him,US Army Air Corps,Food & Drink,What Is The Most Popular Purchase At The Grocers ,Coca-Cola  +Art & Literature,"Which poet, in his ""Elegy Written in a Country Churchyard"" told us that ""Full many a flower is born to blush unseen / And waste its sweetnes on the desert air.""?",Thomas Gray,General,How many of the islands of hawaii are inhabited,Eight,General,Victoria is the capital of ______,Hong kong +General,"What colour are the properties kentucky, illinois, and indiana in monopoly",Red,General,Hominophobia is the fear of,Men,Music,"According To Tina Turner ""We Don't Need Another"" What",Hero +Science & Nature,What Part Of The Body Does Thoracic Medicine Deal With ,The Chest ,General,What symbol did 87 year old Arthur Eisenmenger design,The Euro note graphic,Food & Drink,What is the main flavour in the following alcohols 'Sambuca' ? ,Elderberry / aniseed  + History & Holidays,Which Scottish actor famously sang in the film Darby O Gill & the little people? ,Sean Connery ,General,Jack and Jill went up a ____ to fetch a pail of water,Hill,General,This is a type of carpet or a breed of cat,Persian +Sports & Leisure,Football: The Cleveland ________.,Browns,General,Monastic order that established the California wine industry,Franciscan,General,Who was the first American in space,Alan shepard +General,What playwright wrote The Three Sisters and The Cherry Orchard,Anton Chekov,Music,"Who did Chris De Burgh Write His Hit Song ""Lady In Red"" For",His Wife Diane,General,"In the nursery rhyme, who visited the person with a little nut tree",The king of spain's daughter +General,NAOH is the chemical formula for what,Sodium Hydroxide,General,What did erik rotheim invent in 1926,Aerosol,General,Daniel Arap Moi is the leader in which country,Kenya +General,Material world who played the male lead in the 1965 film entitled the war lord,Charlton,General,What Group Of Animals Are Collectively Known As A Rafter,Turkeys,Art & Literature,Who wrote 'Weird Harold and Fat Albert'?,Bill Cosby +General,From what animal is venison,Deer,General,A renaissance doctor - what treatment excluding bleeding,Enemas,Science & Nature,Peanuts are one of the ingredients of?,Dynamite +General,Which film covers the life of George Cohan,Yankee doodle dandy, History & Holidays,"Who, in 1909, became the first man to fly across the English Channel ",Louis Bleriot ,Food & Drink,Which Restaurant Was Jamie Oliver Working In Before He Found Fame As The Naked Chef ,River Cafe  +Geography,"Linz, austria is a leading port on which river ",Danube ,Science & Nature," The snail mates only once in its entire life. When it does mate, however, it may take as long as __________ to consummate the act.",12 hours,General,Joan Peters became famous as who,Carol Lombard +Science & Nature,What Is The Main Poisonous Gas In Car's Exhausts ,Carbon Monoxide ,General,"What group said they had ""no time"" for ""no sugar tonight""",Guess who,Entertainment,Who played commander Riker in 'Star Trek'?,Jonathan Frakes +General,"A folded tortilla filled with meat, cheese, beans etc. is called what",Taco,General,"What's the international radio code word for the letter ""G""",Golf,General,Which racing circuit is nicknamed the brickyard,Indianapolis +General,"Wallace in greek and roman mythology, what food of the gods was said to make immortal anyone who ate it",Ambrosia,Geography,Which County Is Maidstone In? ,Kent ,General,Grammy Awards: What album by various artists won the grammy in 1972,Concert +General,The island of Formosa is now known by which name,Taiwan,General,"Which Well Known Singer And Actress Was Once The Lead Singer Of The Band ""Eighth Wonder""",Patsy Kensit,Art & Literature,In Which City Will You Find The Largest Opera House In The World ,"New York , The Metropolitan Opera " +General,African French Bur Fig Marsh Pot types of what plant,Marigold,General,How many pairs of jaws does a crab have,Six,General,"Blossoms 'now look at them yo-yo's, that's the way you do it ..' what is the dire straits song title",Money for nothing + Geography,Bangkok is the capital of ______?,Thailand,General,What were the first false teeth made from,Ivory,General,What is the correct name for the flower the Michaelmass Daisy,Aster +General,Donald Baxter McMillan compiled the first what dictionary,Eskimo - English,General,What is the fear of theology known as,Theologicophobia,General,What is the decimal equivalent of the binary number 11011,27 +General,The 1982 football strike lasted how many days,57,General,If you were eating a Malus Pumila what would it be,Apple,General,Which pop singer was nicknamed The Groover from Vancouver,Brian Adams +General,"David, Adam, Paul And Larry Are The First Names Of Which Long Running Band",U2,General,"What is the name of the cartoon that had ponies of all colors of the rainbow,and had a design on their butt,there were pegasus,unicorns,and earth ponies and they talked?",My Little Ponies,General,What did Farters collect,Pigs - its an old English word +General,What is the most easy thing to recycle,1 Aluminium 2 Glass 3 Paper,General,William Sydney Porter is better known as who (literature),O'Henry,General,Simpsons: what does nelson say when something bad happens,Ha ha +General,In the 1951 movie the desert fox who played rommel,James mason,General,What is the common name for a birds Ventriculus,Gizzard,Food & Drink,Which Type Of Beans Are Used To Make Baked Beans? ,Harocot Beans  +General,"In Edward Lear's poem, upon which fruit did the Owl and the Pussycat dine",Quince,General,Who was the first U.S. president to attend monday night football,Jimmy carter,General,Quercus is the generic name for which tree,Oak +General,What was st. paul's trade before he converted,Tent-maker,General,Name one of the birds which Noah released from the Ark,Raven or dove,General,Which singer was known as Little Miss Dynamite,Brenda Lee +Science & Nature,Who Make Macintosh Computers ,Apple ,General,"Where, on a horse are its withers",Shoulder,Sports & Leisure,How Old are the horses in the Epsom Derby? ,Three Years Old  +Science & Nature,What Name Was Given To The Boeing B-52 Heavy Bomber That First Flew In 1952 ,Stratofortress ,General,Who Was described By The Radio Times As The “ Scummiest To rag In The Laundry Basket Of English History ”,Blackadder,General,David Suchet gives a brilliant TV portrayal of this mustachioed detective,Hercule poirot +General,First Byzantine emperor,Justinian, History & Holidays,"Oklahoma comes from two choctaw words, okla and humma, and literally means what ",Red people ,General,This animal is the symbol of the U.S. Republican party,Elephant +General,Betty Joan Perske is better known as who,Lauren Bacall,General,What city does Orly airport serve?,Paris,General,What is 'shogun' in english,Military governer +General,Which Italian City Is The Home Of The Car Manufacturer Fiat,Turin,General,What period is the age of fish,Devonian,General,Robert Fitzroy captained which famous ship,The Beagle +General,Fill in the blank: knock me for a ___,Loop,Sports & Leisure,Who was the first boxer to defeat Lennox Lewis in a professional bout? ,Oliver McCall ,General,What American spoiled Hitler's Aryan dream at the 1936 Olympics,Jesse owens +General,Vestiophobia is the fear of what,Clothing,General,In which city was Mozart born,Salzburg,General,"Who recorded the album ""Freedom of Choice"" in 1980",Devo +Music,The Osmonds Enjoyed Their Only UK Number One Hit Single In 1974. Name It?,Love Me For A Reason, Geography,Kuwait City is the capital of ______?,Kuwait,General,What is the capitol of Morocco,Rabat +General,In which opera does leporello entertain a vengeful jilted lover,Don,General,"Three of Shakespeare's plays contain a ghost - Hamlet is one, name either of the others",Macbeth julius caesar,General,What Is As Of (2008) The Largest Landlocked Country In The World,Kazakhstan +Music,Which Wrestler Had A Hit With Im The Leader Of The Gang In 1993,Hulk Hogan,General,"What game usually starts with 'is it animal, vegetable or mineral'?",Twenty questions,Food & Drink,What Are Pontefract Cakes Made From ,Licorice  +General,What is the Capital of: Japan,Tokyo, Geography,Where is most of America's gold located ?,Fort Knox,Geography,What Is The Westernmost Point Of England ,Land's End  +General,The star Antares is in which constellation,Scorpius,General,What is a spermologer interested in,Trivia,General,What ingredient is contained in beer but not ale,Hops +General,Which port on the River Douro is the second largest city in Portugal,Oporto,General,What is the name of Paul McCartney's official fan club,Club Sandwich,General,"Woodpusher, fish and patzer derogatory words for a bad what",Chess player +General,Half years who made her show business debut at the age of 2 1/2 as part of her family's vaudeville act on the 'new grand theater stage',Judy garland,Science & Nature,What is the lightest known gas ,Hydrogen ,Food & Drink,From What Kind Of Animal Is Rennet Obtained ,A Calf  +General,To whom was the Eroica dedicated,Napoleon Bonaparte, Geography,Dhaka is the capital of ______?,Bangladesh,Science & Nature,What Is The Principal Crop Of The United Arab Emirates ,Dates  +General,Twos company threes a crowd what do four and five make,Four and five make nine,General,An Albert chain is usually attached to what,Watch,General,Which town in the U.S. had Clint Eastwood as its mayor,Carmel + Geography,Name the largest city in Canada.,Toronto, History & Holidays,What liqueur goes into making a 'snowball' cocktail? ,Advocaat ,General,"A slow and graceful dance, the most popular dance of the eighteenth century, characterized by symmetrical figures and elaborate curtsys and bows.",Minuet +General,Who's album 'Pearl' was released posthumously,Janis joplin,General,What is a mayonnaise flavoured with garlic called,Aioli,Sports & Leisure,So Far (2008) There Have Been 6 Players To Achieve A 147 Break At The Crucible Ronnie O Sullivan & Ali Carter Both Managed It in 2008 Can You Name The Other Four (PFE) ,"Cliff Thorburn, Jimmy White, Mark Williams, Steven Hendry " +General,What's the primary function of two thirds of a shark's brain,Detecting aromas,General,Collective nouns - A leap of what,Leopards,General,What does the george washington bridge span,Hudson river +General,What current cast member of ER was on an 80's show of the same name?,George Clooney,Music,Springing Into The Charts At No.4 In 1963 What Was Dusty Springfields First Single,I Only Want To Be With You,Music,Adam Clayton Plays Bass Guitar For Which Band,U2 +General,Pott's Disease is a form of tuberculosis which affects which part of the body?,Spine,General,Name the knot used to shorten a rope without cutting it,Sheepshank, History & Holidays,"Which English Silver Coin, Worth Four Pennies, Was Taken Out Of Circulation In The 17th Century? ",The Groat  +Geography,The aurora borealis is most commonly observed in which country,Canada,General,The famous train 'The Clansman' ran from London to which city?,Inverness,General,The front of a building,Facade +General,Whats the name of El Ninos lesser known little sister,La nina,Science & Nature,Where Is The Natural Habitat Of The Cheese Plant ,Central America Rainforest ,General,What was John Lennon's middle name before he changed it to Ono,Winston +General,What is the longest river in the world,Nile,General,Which man has the most monuments erected in his honour?,Buddha,General,"Which Scary Movie Takes Place In The Town Of ""Burkittsville"" Maryland",The Blair Witch Project +General,Hoplophobia is the fear of,Firearms,Geography,"Of the 3,000 islands of the Bahama chain in the ____________, only 20 are inhabited.",Caribbean,General,What is the flower that stands for: restoration,Persicaria +General,International direct dialling codes what country has 353,Republic of Ireland,General,"How many cooks spoil the broth, according to a well known saying",Too many,General,Name Buck Rodgers' pal,TWIKI +Art & Literature,What Are The Christian Names Of JK Rowling Author Of The Harry Potter Books? ,Joanne Kathleen ,Music,"Before Forming Sigue Sigue Sputnik, Tony James Was In Which Band With Billy Idol",Generation X,General,With what did david kill goliath,Slingshot +General,Which country was the first to abolish capitol punishment 1826,Russia/Siberia,General,When was the descent of the holy spirit on the apostles,Pentecost,General,The Southern Alps are found in which country,New zealand +Food & Drink,Which Order Of Friars Is Coffee Topped With Steam Milk Named After ,Capuchin , Geography,What is the capital of Jamaica?,Kingston, Geography,Cheyenne is the capital of ______?,Wyoming +Sports & Leisure,What Colour Is The 8 Ball In A Game Of Pool ,Black ,General,In the 1920s cars built in Bennington had what safety device,A Saint Christopher medal,Science & Nature,These flowerless plants grow on bare rocks and tree stumps.,Lichen +General,What is the name of the policeman who appears in the TV series 'Postman Pat'?,PC Selby,General,Who transmitted radio signals across the atlantic,Marconi,General,What was the first manufactured item to be sold on Hire Purchase,Singer sewing machine in 1850s +General,What is haggis,Sheep stomach,General,Name the dagger which lends its name to a type of women's shoes with slender pointed heels?,Stiletto,General,Strong spirit distilled from wine or fermented fruit juice,Brandy +Art & Literature,"In The Book The Secret Diary Of Adrian Mole, What Was The Name Of Adrian's Girllfriend ",Pandora ,General,"What trade did Bonito, Calico Jack, and Dick Hatteraick follow",Pirates,General,What was Napoleon Bonaparte's official emblem,Bumblebee +General,What country are the ships Sun Viking & Nordic Prince registered in,Norway,Science & Nature,"With age, what organ shrinks faster in males than in females?",Brain,General,In the Hindu religion what is a Mandir,Temple +Music,Which legendary Beach boys album was never completed?,Smile,General,Roe made the first flight in to Britain in a British plane in which year,1909,Entertainment,How many strings are there on a violin?,Four +General,What was the name of David Hasselhoff's talking car in Knight Ridder?,Kitt,Music,"If You Were To Take A Walk Down Lonely Street, Where Would You Find Yourself",Heartbreak Hotel,General,What Football Team Plays At The Ibrox Stadium,Glasgow Rangers (Rangers)     +General,Who plays Data in Star Trek the Next Generation,Brent Spiner,General,Analogy: goose - geese as passerby - ___________,Passersby,Entertainment,"What is the name of the skunk in the film, ""Bambi""?",Flower +General,Give either of poet W.H.Auden's Christian names,Wystan hugh,General,What are Hamilton House and Petronella,Scottish country dances,General,"The young are called widgets, females fifinellas what are they",Gremlins +General,Who ran unsuccesfully against Regan in 1984?,Walter Mondale,General,Ruth Eisemann-Schier was the first woman to get on what,FBI ten most wanted list,Entertainment,This film starring Julie Andrews and Christopher Plummer wont he best picture Oscar for 1965.,Sound of Music +People & Places,Who Was Married To Robin Givens until 1989 ,Mike Tyson ,General,Who Released An Album In 1992 Entitled “ Diva ”,Annie Lennox,General,What profession did Handel originally study,The Law +Science & Nature, A young male fur __________ that is kept from the breeding grounds by the older males is called a bachelor.,Seal,General,"How many karats is pure, unalloyed gold",24,General,What was the name of buffy's doll in the 1970's show 'family affair',Mrs +General,Changing of an employees job or working conditions to force resignation,Constructive dismissal,Sports & Leisure,What is the score of a forfeited baseball game?,Sep-00,General,Bonanza: what was the name of eric's horse,Chub +General,Who discovered blood circulation,William Harvey,General,"In the Gregorian calendar after 10,000 years by how many days will the calendar be wrong by ?",Three,General,For who did eric clapton write 'layla',Linda mccartney + History & Holidays,The Charge Of The Light Brigade Occured During Which War ,Crimean ,General,British king was known to family friends as David his last name,King Edward the 8th,General,Where is Las Palmas,Canary islands + History & Holidays,"From which era does the custom of carol singing originate? (Edwardian, Victorian, Middle Ages) ",The Middle Ages ,General,What does a stoat wear in winter,Ermine,Entertainment,"Through 1963 this duo's total record sales exceeded 18 million with successes including ""Cathy's Clown"" and ""Wake Up Little Suzie"".",The Everly Brothers +General,"Give one of the forenames of baseball player, Babe Ruth",George herman,General,Who was defeated at the Battle of Zama by Scipio Africanus,Hannibal,General,"Which breed of large dog, used by German nobility in the 17th Century to hunt boar and stags, is known there as the Deutsche Dog",Great dane +General,"In The World Of Comedy How Is The Comedian ""Chris Collins"" More Commonly Known",Frank Skinner,General,"Name the church, believed to be the site of Christ's crucifixion",The church of the holy sepulchre,General,"In the 1987 film Roxanne, who played the role of Roxanne",Darryl hannah +General,Which Canadian won the 1957 Nobel Peace Prize for Suez,Lester Pearson,Science & Nature,What is the meaning of the name of the constellation Vulpecula ?,Fox, History & Holidays,"In 1962, for what did Britain and France sign an agreement to build together?",Concorde +General,In which Bond novel did he first want Martinis shaken not stirred,Diamonds are Forever,Food & Drink,Which vegetable is also a Welsh emblem? ,A Leek ,General,When was the first jet aircraft flown,1941 +Geography,What Does Copenhagen Mean In English ,Merchants Haven , Geography,How many countries have a population over 130 million ?,"Seven (Pakistan, Russia, Brazil, Indonesia, United States, India and China)",General,What are studied by hymenopterists,Bees + History & Holidays,Who was the mother of Elizabeth I?,Anne Boleyn,General,On Donavan's Mellow Yellow who did the whispering vocal,(Quite right slick) Paul McCartney,General,Whose music was on the soundtrack of When Harry met Sally,Harry Connick Jr +People & Places,Which City Is Home To The Little Mermaid Statue ,Copenhagen ,Geography,Which Is The Smallest Continent ,Europe ,General,In which war did the battle of Sedan take place,Franco-prussian +Food & Drink,A.K.A juniper juice?,Gin,General,Who was the first leader of the canadian federal ndp,Tommy douglas,General,Otalgia is what condition,Earache +General,From what did alexander the great suffer,Epilepsy,General,The group Simply Red were named after what,Man Utd football club,General,What is the link between the actresses Mia Farrow & Maureen O'Sullivan,Mother and daughter +General,What is the largest man made lake in the world,Lake mead,General,"In which island group would you find Rarotonga, Palmerston and Aitutaki?",Cook Islands,Music,What was the title of the first album released by The Rolling Stones?,The Rolling Stones +General,What type of candy is banned in Washington state,Lollypops,Science & Nature,The chemical compound sodium chloride is often sprinkled on food before ingestion. What is it's common name?,Salt,General,"Stage role, written for a man, took 80 years to be played by one",Peter Pan RSC 1982 +General,What James Bond film used the space shuttle,Moonraker,Music,Who Was The Original Lead Singer Of AC/DC,Bon Scott,General,Herodotus the Greek is known as the father of what,History +Food & Drink,Mexican dish with minced and seasoned meat packed in cornmeal and corn husks.,Tamale,Geography,Name the only Central American country without an Atlantic coastline.,El salvador,General,What does an ombrometer measure?,Rainfall +General,What is an elver,Baby eel,General,Who crashed out of the 1995 Tour de France after just 92 seconds,Chris boardman,General,Who took dictation from perry mason,Della street + History & Holidays,What Was The Colonial Name Of Ghana ,Gold Coast ,General,In which English county is Castle Drogo?,Devon,Geography,Name the desrt located in south_east California.,Mojave +General,Which famous person lived at Brantwood from 1872 until his death in 1900?,John Ruskin,General,Which london street is famous for its hotels,Park lane,General,"In the film '101 dalmatians', what animal is sergeant tibbs",Cat +Music,"Who Scored A Hit With ""Head Over Heels In Love"" In 1979",Kevin Keegan,General,What is a resident of liverpool,Liverpudlian,General,What fictional detective was created by Lesley Charteris,Simon templar the saint +Food & Drink,Which Australian Opera Singer had a dessert and a kind of bread named after her? ,"Nellie Melba ( Peach Melba, Melba Toast ) ",General,What game Johnny Archer Chang Feng-Pang been world champs,Nine Ball Pool,General,Michael Jackson caught fire while filming a commercial for which carbonated beverage?,Pepsi +General,Where will you find twenty moons on the human body,The base of the nails,General,What does the VO on a bottle of Seagrams stand for,Very Old,General,"Which poet wrote the poem, ""The Soldier""",Rupert brooke +Geography,Which state is the Wolverine State,Michigan,General,St Snithney is the patron saint of what,Mad Dogs,General,In which Italian city is the original of Leonardo da Vinci's Last Supper to be seen,Milan +General,In the Chinese calendar what year follows Monkey,Chicken,General,How is Venom put into the body?,Bites or stings,General,The Undiscovered Country a Star Trek title taken from where,Shakespeare's Hamlet +General,What is the mode of transport in Venice,Gondola,General,What word from the Persian means perfumed,Attar,General,In which city is Napier University,Edinburgh +Science & Nature,What insect has a type of hair on it's eyes?,Honeybees,General,What is a one-party system of government in which control is maintained by force and regimentation?,Fascism,Science & Nature,Where Does Bracket Fungus Grow ,On The Trunk Of A Tree  +Science & Nature,How many chambers does the human heart have?,Four,Music,What Song Did David Bowie And Mick Jagger Sing Together At Live Aid,Dancing In The Street,General,"What's the most effective insulator brick, fibreglass or wood",Fibreglass +General,Two weeks after hatching what is 3000 times its birth weight,Monarch Butterfly,General,What elton john record label did kiki dee join in 1973,Rocket records,Food & Drink,Which two fruits are crossed to make an ugly fruit? ,Grapefruit & Tangerine  +General," The study of natural phenomena: motion, forces, light, sound, etc. is called ______.",Physics, History & Holidays,In What Year Was The Old Age Pension Introduced In The UK ,1908 ,General,Sotheby's sold a 200 year old bit of Tibetan what $1500 in 1993,Cheese +General,"What's the international radio code word for the letter ""W""",Whiskey,Music,What Did Billy Idol Think It Was A Nice Day For In 1985,White Wedding,General,What was shown over parliament on a Canadian 2 dollar bill,An American Flag +General,What colour is the Black Box carried in aircraft,Orange,Music,How In The World Of Music Are Julia Volkova & Lena Katina More Commonly Known,Tatu,General,What does a petrologist study,Rocks + History & Holidays,What country started the tradition of exchanging gifts? ,Italy (Romans) ,General,The Colossus of Rhodes was a statue of who,Helios the sun god,General,In Wyoming its illegal for women to stand within 5 feet of what,Bar when drinking +Music,Which 1980's Band Took Their Name From A Vulvan In Star Trek,T'Pau,General,Who appeared on the British WWl recruitment poster,Lord kitchener,Sports & Leisure,How many minutes is a major penalty in hockey?,Five +General,What Type Of Creature Is A Shaded Cameo,A Cat,General,What was the name of the last silent movie made 1929,The Four Feathers,Food & Drink,Bruschetta is what? ,Fried or toasted bread  +General,Astronauts cannot do what in space,Cry - no gravity for tears to flow,Art & Literature,"He wrote Ulysses, Giacomo Joyce, Dubliners and Finnegans Wake, among others.",James Joyce, History & Holidays,1957 and the first artificial satellite to be launched into space (by the Soviet Union) had what name ,Sputnik 1  +General,"Lord Harry, Old Billy, Queed, Skipper, Toast nicknames for who",The Devil or Satan,General,What's the region immediately below the earth's crust,Mantle,General,What size of paper measures 297 x 420mm,A3 +General,Who had the gift of prophecy and the curse of not being believed,Cassandra Daughter of Priam of Troy,Science & Nature,What type of creature is a Whydah? ,A Bird , Geography,What is the capital of Cyprus ?,Nicosia +General,What is the echidna's favorite food,Ants,General,What did people use before the hearing aid was invented?,Ear trumpet,Geography,On which river is the Aswan High Dam,Nile +General,Who was Howard Carter's sponsor during his discovery of the tomb of Tutankhamen,Lord carnarvon,General,58% of people like what during sex,Dirty Talk,General,If you are in your birthday suit what are you wearing,Nothing +General,What is the main ingredient of a booyah,Chicken,General,What are spraints,Otter droppings (shit),General,In which 1973 film did Yul Brynner play an indestructible robot gunslinger,Westworld +Music,In 1997 who became the first family group to reach number one with their debut single?,Hanson,Music,Who Was Born In New York In 1981 And Had Great First Time Success With The Album “Songs In A Minor”?,Alicia Keyes,Sports & Leisure,Which 2004 darts champion is known as The Viking because of his appearance? ,Andy Fordham  +General,What is the shape of the U.S. President's office,Oval,General,What would you find a sally on the end of,Bell rope,General,Name the chief port of Iraq,Basra + History & Holidays,"What is the common name for cercis canadensis, the state tree of oklahoma ",Redbud ,General,On which Athens hill top would you find the Parthenon,The acropolis, Geography,What is the basic unit of currency for Algeria ?,Dinar +Science & Nature,Mercury's period of orbit takes how many earth days?,Eighty eight,General,"What us president was born july 11, 1767",John quincy adams,General,Geographic on what river is blackpool,River fylde +General,Which island left the E.E.C. in 1985,Greenland,General,"In 1918, what illness caused 20 million deaths",Influenza,General,"Who is the author of the science fiction book I, Robot",Isaac asimov +Science & Nature,"What Can Be Ball, Forked Or Sheet? ",Lightning ,General,Maria Magdelana Von Losch Beyyer know as who,Marlene Dietrich, History & Holidays,His ship was the H.M.S. Beagle.,Charles Darwin +General,Public Speaking is the most common fear what's the second,Heights,Geography,What is the capital of Iraq,Baghdad,General,What is the fear of fatigue known as,Kopophobia +Music,Which Record Was The First Chart Success For A Young David Essex In 1973,Rock On,General,Irian Jaya is the name for the western part of which island,New guinea,Geography,In Which Country Is The Schilling The Unit Of Currency ,Austria  +General,A branched hanging support for lights,Chandelier,General,"Greenmantle, Three Hostages, Island of Sheep which character",Richard Hannay,Entertainment,"Which character sang ""Come Out, Come Out, Wherever You Are"" in ""The Wizard of Oz""?",Glinda +General,The Boomtown Rats were averse to which day of the week,Monday,General,1200 in Roman numerals gives what sporting body,MCC, History & Holidays,What pope died 33 days after his election ,John paul i  +Science & Nature,What Is The Home Of A Hare Called? ,A Form ,General,"Who sang the song ""I Want You""?",Savage Darden,General,What type of charge do electrons carry,Negative +General,How long was the love affair between dashiel hammett and lillian hellman,30 years,General,Who invented the electric shaver,Jacob schick,General,"Which fort did Custer depart from on May 17, 1876, with the Seventh Cavalry on his way to the Little Big Horn",Fort Abraham Lincoln + Geography,What is the capital city of New Zealand?,Wellington,General,"Who was the author of ""Journal of the Plague Year""",Daniel defoe,Music,Who Funked For Jamaica,Tom Browne +General,What is the smallest dinosaur so far discovered?,Compsognathus,General,"By What Name Is ""No.30 St Mary's Axe"" London More Commonly Known",The Gherkin,General,In 1980 the Yellow Pages listed a Funeral Home under what,Frozen Food +General,"What type of storm has a central calm area, called the eye, which has winds spiraling inwardly",Hurricane,Music,What Principle Did Janet Jackson Sing About,The Pleasure Principle,General,Collective nouns - A Fall of what,Woodcocks +General,Who sang a song inspired by 'alice in wonderland',Jefferson airplane,General,"What is the name of the Android in ""Star Trek - The Next Generation""",Data,General,"Which 'brothers' had a hit with, 'This old heart of mine'",Isley brothers +General,Europhobia is the fear of what,Female Genitals,General,What are the Amish also known as,Pennsylvania dutch,General,Where would you find Giacomo Marconi airport,Bologna +General,A poltroon is a(n)___.,Coward,General,North Andover Massachusetts its illegal to have what weapon,Space Gun,General,Which Fruit That Starts Contains The Most Calories When Eaten RAW,Avocado +General,In Which US State Is Mount Rushmore ?,South Dakota,General,JRR Tolkein wrote The Lord of the Rings what the JRR stand for,John Robert Reuel,General,Williamine' is a liqueur made from what,Pears +Mathematics,Approximately how many inches are there in one meter?,Thirty nine,General,What is the Capital of: Paraguay,Asuncion,General,Who played the female lead in the Alien films,Sigourney weaver +General,Sinophobia is a fear of ______,Anything chinese,General,In Greece who were the Hetaerae,High class Prostitutes,General,What is a Maine Coon once thought to be extinct,A 20 lb cat +General,C.F.C.'s are said to be damaging to the ozone layer. For what do the initials C.F.C. stand,Chloro fluoro carbon,Science & Nature,Who Invented The First Self Lighting Match ,John Walker In 1827 ,General,Who shot Mr. Burns?,Maggie Simpson +General,"Which 60's folk artist sang the lyrics ""god told abraham kill me your son. abe said man you must be puttin me on""",Bob dylan,Geography,What is the capital of Equatorial Guinea,Malabo,General,Terry Bollea became famous under what name,Hulk Hogan +General,"Sage mountain on Tortola, is the highest point at 1,781 feet, of which British Caribbean dependency",British virgin islands,General,What is the name for a match of 3 games in bridge,Rubber,Music,"According to the Beatles song ""Glass Onion"", who was the Walrus?",Paul +General,Ten 1000 virgins bought insurance against what in 2000,Immaculate conception,Food & Drink,What does 'V.S.O.P' Stand For On A Bottle Of Wine Or Sherry ,Very Special Old Pale ,General,What is the flower that stands for: dangerous pleasures,Tuberose +General,What Type Of Food Stuff Do Humans Eat The Most Of,Rice,General,What is the fear of waves known as,Kymophobia,General,Who was the original Hollywood 'it' girl,Clara bow +General,What color is the lion on the side of the Detroit Lions football helmet,Blue,General,What does sputnik literally mean,Fellow Traveller,Music,"Who was Pink Floyd's ""Crazy Diamond""?",Syd Barrett +People & Places,Who Nickname is Beefy ? ,Ian Botham ,General,Candy bar named for a celestial object.,Milky way,Music,Who Had a Hit In 1986 With Absolute Beginners,David Bowie +General,Which Handheld Everyday Object Was Invented In Oslo In 1926 By Erik Rotheim,Aerosol Can,General,Im Westen Nicht Neues what famous novel 20s later film,All Quiet on the Western Front,General, The science of preparing and dispensing drugs is ________.,Pharmacy +General,"In the show Cheers,what was the name of the bar that always played practical jokes on the gang at Cheers?",Gary's Old Time Tavern,Art & Literature,Which School Did Billy Bunter Attend ,Greyfriars ,General,What sign is the water carrier the zodiacal symbol for,Aquarius +General,What is the collarbone,Clavicle,General,Lalo Schifrin composed which famous TV series theme,Mission Impossible – plus others,General,With what team did hank aaron finish his major league career,Milwaukee +General,What is the state bird of New Jersey,Eastern goldfinch, History & Holidays,What is an assembly of witches called ,A Coven ,Technology & Video Games,"If you're killing a goomba, what game are you playing? ",Super Mario Bros. +Music,Jason Orange Is A Member Of Which British Boy Band,Take That,General,Which fashion designer said - A woman is as old as her knee,Mary Quant,General,The song There is nothing like a dame appears in which musical,South Pacific +General,Panchaguni is the Indian God of what art,Palmistry,General,Ismene and Antigone are whose daughters,Oedipus, History & Holidays,What year was Margaret Thatcher elected British Prime Minister? ,1979  +General,What is the first letter of the Russian alphabet,A,General,What is another name for the prairie wolf,Coyote,General,Acrophobia is a fear of _______.,Heights +General,In what George Bizet opera do Zugra and Nbadir appear,The Pearl Fishers,Geography,"What city was the setting for ""Gone With the Wind""",Atlanta,General,Which Mammal has the highest blood pressure,Giraffe +General,What World First Took Place On Saturday The 19th November 1994,First Uk National Lottery,General,On a UK ordinance survey map what is shown by a blue star,Major shopping centre,Music,Who Was Down On The Streets In 1984,Shakatak +General,"In the equation E=MC2, what does the C stand for",Speed of light,General,What is the capital city of the Middle East state of Qatar,Doha,General,What Hollywood starlet's pinup graced the most GI barracks in World War II,Betty Grable +General,What shape is something that is reniform,Kidney shaped,General,Python film Your mother was a hamster father smelt of what,Elderberries,General,What were the lone three fighter planes which defended Malta in World War II nicknamed,"Faith, hope & charity" + History & Holidays,In what year was the first English translation of the Bible completed? ,1388 ,General,What is the main food of the Oyster catcher bird,Mussels,General,Where was napoleon defeated,Waterloo +General,In what tv series did ron howard play richie,Happy days,General,Mork and Mindy was a spinoff of what TV show?,Happy Days,General,In Tennessee age of consent is at 16 unless the girl is what,A Virgin then its 12? +General,The average Britain in their lifetime eats 4907 what,Loafs of Bread,Geography,Which Irish city is famous for its crystal,Waterford,General,"Wide muscular partition separating the thoracic, or chest cavity, from the abdominal cavity",Diaphragm +General,"What word spelled the same French, English, German, Swedish",Taxi,General,"The name ""yo yo"" comes from what language",Tagalog,General,"The world's costliest coffee, at $130 a pound , is called what",Kopi luwak +General,What sexually arouses a Jactitator,Bragging about sex,General,What is the only sport that has a rule against left handed players,Polo,General,Hypermetropic people are what,Long Sighted +General,Which actor won an Oscar for his portrayal of Gauguin in the film Lust For Life,Anthony quinn,General,Where was paper money first used,China,Geography,What Is A Sextant Used To Measure ,The Angle Of The Sun Or Stars Above A Horizon  +General,"Which UK city, other than London, has a station called Charing Cross",Glasgow,General,Which are the twin cities,Minneapolis and saint paul,Art & Literature,What's Penthouse's sister publication for women?,Viva +General,"When going on stage, actors consider it bad luck to be wished what",Good luck,General,A group of rhinos are called a(n) __________.,Crash,General,What war was rudely interrupted by the bubonic plague,The hundred years war hundred years war hundred years +General,Who starred in the film version of 'hamlet' that grossed the most,Mel gibson,General,What term describes the study of the behaviour of materials and substances at very low temperatures,Cryogenics,General,"On november 26, 1941, what did president roosevelt declare will always be celebrated on the fourth thursday in november",Thanksgiving +General,What is the most popular Saints name,Felix - 67 John 65,Music,Who Composed The Pastoral Symphony,Beethoven,Science & Nature,This is like an airplane but has its propeller on top instead.,Helicopter +General,Where was jesus born,Bethlehem,General,Capers are pickled flower seeds of what plant,Nasturtium,General,The leach has 32 what - humans only got one,Brains +General,"What is the fear of lockjaw, tetanus known as",Tetanophobia,General,In Greek what does Eunuch literally translate as,Bed Watcher,General,What is a two-bit moon,First quarter + History & Holidays,"She was called ""The Maid of Orleans"".",Joan of arc,General,Name the character played by David Cassidy in television's Partridge Family series of the 1970s,Keith partridge,General,"The ""Maxima"" was a model of which car",Nissan +Sports & Leisure,Which Scottish Golfer Was Captain Of Europes 2002 Ryder Cup Team ,Sam Torrance ,General,Whose cases were Empty House Copper Beeches Black Peter,Sherlock Holmes,General,What time do Spanish bullfights start,5:00 PM +General,Where in Europe it is illegal to flush the toilet after 10 P.M. if you live in an apartment?,Switzerland,General,"In units of measurement, how many rods are there in a chain",Four,General,Which European country is a Grand Duchy,Luxembourg +General,What held up a Cricket test Match between England Pakistan,Mouse on pitch,General,Intelligents Report a quarterly magazine in US which subject,UFO organisation,General,What links Jack Loving Girls and Nurse,Carry on Films +Music,By what 4-letter name is U2’s front man Paul Hewson better known?,Bono,General,What part of the body is affected by rhinitis,Nose,General,This island country is south of Malaysia.,Singapore +General,"Who wrote the book ""wouldn't take nothing for my journey now""?",Maya angelou,General,What is the Hebrew word for adversary,Satan,Geography,Which is the Earth's third largest continent,North america +Food & Drink,Is wholemeal bread brown or white?,Brown,General,Where did judy garland's family have their vaudeville act,New grand theater,General,What on average is two inches wide but 2 miles long,A lightning bolt +General,What does epcot stand for,Experimental prototype community of tomorrow,Toys & Games,This cube puzzle was invented by a Hungarian mathematician in 1974.,Rubik, History & Holidays,Who Preceded Harold Wilson As Prime Minister? ,Sir Alec Douglas Home  +General,What was the first American state to enter the union 7 Dec 1787,Delaware Pennsylvania second,General,What is the name given originally by Greek rhetoricians to a literary illustration,Parable,Science & Nature,What is acute nasopharyngitis?,A cold + History & Holidays,"Who sang Its a Heartache, nothing but a heartache? ",Bonnie Tyler ,General,The fish eating bulldog is what type of creature,A Bat,General,What colour Tuesday did the Rolling Stones sing about 1977,Ruby +General,"Who sang the theme song to the James Bond movie, ""A View to A Kill""?",Duran Duran,General,What's the only 5 point letter in scrabble,K,General,After who is the 'Ramses' brand condom named,Pharaoh Ramses II +Science & Nature,Which British Car Companys First Car Was The 2 Seater Oxford In 1913 ,Morris ,General,What does a galactophagist drink,Milk,Music,"Which Bespectacled Scots Were ""King Of The Road""",The Proclaimers +General,What is the name of the four holy books of the Hindus,The Vedas,General,What is a group of this animal called: Fox,Skulk leash,General,What process does animal hide have to undergo to become leather,Tanning +General,"In 'star wars', who was darth vader's voice",James earl jones,General,If an Australian had a Bingle what would it be,Car Accident,General,Relating to or using signals over a range of frequencies,Broadband +General,Which of Charles Dickens' novels in set partly in America,Martin chuzzlewit, History & Holidays,Which gangster died on the 25th January 1947 ?,Al Capone,General,Who leaked the pentagon papers to the new york times,Daniel ellsberg +General,What was elvis presley's twin brother's first name,Garon,Music,Which Single From Pras Michel Featuring ODB And Mya Made The Top Ten In 1998,Ghetto Superstar (That Is What You Are),General,What is known as 'the father of waters',Mississippi river +General,Cheyenne is the capital of ______,Wyoming,General,What was johanna spyri's story about a little alpine lass,Heidi,General,"In what sport would you find the terms hack, tee and hog line",Curling +General,Elizabethan England what was Lift leg Dragons Milk Angel food,Names for Beer,Music,"Icicle Works Had A Hit With ""Love Is A Wonderful Colour"" Or ""It's A Wonderful Life""",Love Is A Wonderful Colour,General,What is the leading cause of death in China,Respiratory Disease +General,What is the only flavour Jell-o containing any real fruit,Cranberry,General,In what Australian state would you find Ballarat,Victoria,General,Polyphobia is the fear of,Many things +Geography,Which element makes up 2.83% of the Earth's crust,Sodium,General,Does elizabeth ii face to the left or right on a british coin,Right,General,The Falketing is the parliament of which country,Denmark +General,Who was swallowed by a whale,Jonah,General,Where did the mutineers of the Bounty settle,Pitcairn Islands,General,Saint Lydwina is the Patron Saint of what sport,Ice Skating +General,Who is the current monarch of Belgium,Albert ii,Food & Drink,Polish cake filled with candied fruits and nuts.,Babka,General,What is the term for a person who reads a usenet newsgroup but never contributes to it,Lurker +General,What comprises than 54% of humpback whale's milk,Fat, Geography,What is the basic unit of currency for Peru ?,Sol,General,Mary magdalene is the patron saint of ______,Prostitutes +Science & Nature, Sharks and rays are the only animals known to man that cannot succumb to cancer. Scientists believe this is related to the fact that they have no bone _ only __________,Cartilage,Food & Drink,What is another name for the carambula ,Star Fruit ,General,What country is the world's biggest coffee exporter,Brazil +General,Who portrayed Mrs Cleaver,Barbara billingsly,Food & Drink,By Value What Is The Most Popular Food Or Drink Consumed ,Beer ,General,"What is Japanese ""sake"" made from",Rice +General,In St Croix Wisconsin its illegal for women to wear what publicly,Anything Red,General,In Beaconsfield Quebec it is illegal to own what,Log Cabin,Art & Literature,"A movement, c. 1915-23, that rejected accepted aesthetic standards. It aimed to create antiart and nonart, often employing a sense of the absurd.",Dadaism +General,What was Beaver Cleaver's first name,Theodore,General,Who was the youngest elected president of the U.S.,John f. kennedy,General,"The Movie Company ""Icon Productions"" Was Founded In 1989 By Which Hollywood Actor / Director",Mel Gibson +General,Which element is alloyed with steel to make control rods for Nuclear reactors?,Boron (Boron Carbide also used),Religion & Mythology,A catholic minister is known as a?,Priest,General,What is the flower that stands for: delicacy,Cornflower +General,"Which species of animal has sub-species that include Burchell's, Grant ""s and Chapman's",Zebra,Music,How Many M's Were There In The Title Of The Crash Test Dummies 90's No.2 Hit,12,General,Who says 'you'd be surprised how much it costs to look this cheap',Dolly +General,In the Bible what was an adamant,Diamond,Technology & Video Games,Mega Man's traditional nemesis in the Mega Man series is whom? ,Dr. Wily,General,What is the first letter of the Greek alphabet,Alpha +Music,Chris Difford & Glen Tilbrook Write Songs For Which Group,Squeeze,General,What is the largest city on the South island of New Zealand,Christchurch,General,Ancel Keys developed which US soldiers item,K rations +General,How many tunnels under the mersey link liverpool to the wirral,Two,General,Only two elements liquid room temperature - mercury and what,Bromine,General,"In 'alice in wonderland', with what were the words 'eat me' written on the cake",Currants +General,Who wrote the poem The Lady of Shallot,Alfred lord tennyson,General,Who was the first English Poet Laureate in 1616,Benjamin Jonson,Science & Nature,To Which Family Does The Jay Belong ,Crow  +General,What nationality is Henry Kissinger by birth,German,General,Who wrote the autobiography 'Managing My Life',Sir alex ferguson,Science & Nature,This two ton animal can gallop at over 50 miles an hour.,Rhinoceros +General,What gift is associated with the 30th Wedding Anniversary?,Pearls,General,Five named Beatles on Abbey Road cover J P G R and who,Volkswagen,General,What does a cartographer do,Makes maps +General,In what language was Bambi originally published,German,General,Pit Straight - Lesmo Bend - Roggia Bend - which Grand Prix,Monza Italy,Music,What Nationality Are Bjorn Again A Very Successful Abba Tribute Band,Australian +Music,"Which Singer Got His Break When His Band The Shadows, Deputized For Buddy Holly After He Died In A Plane Crash",Bobby Vee,Music,"In 1998 Who Wrote A Forward To An Edition Of ""The Gospel According To St Mark""",Nick Cave,General,In what city is the bridge of sighs,Venice +General,In military slang which word means to carry heavy equipment on foot over difficult terrain,Yomp,General,Who wrote the hit musical West Side Story,Leonard bernstein,General,"In which city will you find the epitaph ""Rest in peace, the mistake shall not be repeated""",Hiroshima +General,When was the Watergate break in,1972,General,What's the largest bay in the world,Hudson bay,Entertainment,An alien creature in a funny hat has opposed both Bugs Bunny and Daffy Duck. Where is he from,Mars +General,When was the first Australian cricket tour to England,1868,General,What scientist developed the modern theory of evolution,Charles darwin,General,"What was the first name of the Reverebd Varah, the founder of the Samaritans",Chad +General,The thistle is the national flower of ______,Scotland,General,Evening Star no 92220 was the last what,Steam Loco built by British Railways,Technology & Video Games,Konami's classic shooter Salamander is better known in the U.S. as what? ,Life Force +General,Which car manufacturer was the first to introduce front wheel drive in 1934,Citroen,General,What creatures do the Galapagos islands take their name from,Tortoises,Food & Drink,What Traditionally Should Accompany Haggis ,"Tatties, Neeps & And A Dram " +General,What kind of creature is a redback,A spider,General,Who invented the cotton gin,Eli whitney,General,Frank and Jesse James father had what job,Minister +Science & Nature,What is the most commonly eaten insect by humans in the world? ,Grasshoppers ,General,What's the point scored immediately after deuce in tennis,Advantage,General,"Who lamented about ""No chocolate-covered candy hearts to give away and no wedding Saturday within the month of June""?",Stevie Wonder +General,What is the second most common international crime,Art theft,General,Queen Victoria in 1837 was the first English monarch to do what,Live in Buckingham,Sports & Leisure,Who Is The Billionaire Owner Of Formula One ,Bernie Ecclestone  + History & Holidays,Who did dita beard work for ,Itt ,General,Alexander Hamilton was shot by Aaron Burr where,In the groin,General,According To Insurance Companies What Occupation Do They Regard As The Highest Risk,Astronaut +Music,"""Up On The Roof"" Was The B-Side Of Which Robson & Jerome Single",I Believe,Music,"The Beatles Song ""Till There Was You"" was originally from which Broadway musical?",The Music Man,General,Harrison Ford played CIA agent Jack Ryan - who else has,Alec Baldwin +General,"In which type of rock would you find features called ""Clints"" and ""Grikes""",Limestone,Science & Nature,In 1665 the first _____ ___________ was performed by Dr Richard Lower.?,Blood Transfusion,Food & Drink,In which country was ice cream invented? ,China  +General,What is the name of the wrought iron tower in Paris?,Eiffel Tower,General,What is the flower that stands for: rivalry,Rocket,Music,Which Rat Pack Music Legend Died In 1998,Frank Sinatra +General,Pigs can become what - like humans,Alcoholics,Science & Nature,At which time of year do children grow fastest?,Springtime,General,What is a shubunkin,A goldfish +General,In the acronym MASER what does the 'M' represent,Microwave,General,"In the monty python parody 'search for the holy grail', what was the name of the enchanter",Tim,General,Who Was The President Of The USA Prior To Richard Nixon,Lyndon Bains Johnson +Sports & Leisure,Who made 11 appearances for Scunthorpe United between 1979 & 1984 but is far better known outside of football? ,Ian Botham ,Sports & Leisure,How Many Points Is The Yellow Ball Worth In Snooker ,Two ,Religion & Mythology,Followers of the Unification Church are called ________.,Moonies +General,Volney was in hundreds of films where can you see him,MGM films he was the lion,Science & Nature, Genuine ivory does not only come from elephants. It can come from the tusks of a boar or a __________,Walrus,Geography,What Is The Longest River In The Western Hemisphere ,The Amazon  +General,The musical word scherzo comes from Italian meaning what,Joke,General,Which UK Chain Of Stores Shares It's Name With A Greek City,Argos,Food & Drink,From which country does the 'lassi' originate?,India +General,Lachanophobia is the fear of,Vegetables,Entertainment,"What kind of eyes did the girl in ""Lucy In The Sky With Diamonds"" have?",Kaleidoscope,Music,"A Re-Issue In 1986, Which Grace Jones Track Reached No.12 In That Year",Pull Up To The Bumper +General,What do we call in English the type of painting known to the French as 'nature morte',Still life,General,What did the three little kittens lose,Their mittens,Science & Nature,"Which Dogs, If Any,Have Colour Vision? ",None  +General,"What Montreal brewer built Canada's first steamboat, in 1809",John molson, History & Holidays,What did Louis Cartier invent?,Wristwatch,General,Who played the telephone operator on laugh-in,Lily tomlin +General,What word in English has the most synonyms,Drunk,General,Greek mythology women of Lemnos did what to their husbands,Murder,General,A small pickled cucumber,Gherkin + Geography,What is the basic unit of currency for Guinea-Bissau ?,Franc,General,"Of what has alberta, canada been completely free since 1905",Rats,General,The longest side in a right-angled triangle is called the ______.,Hypotenuse +Food & Drink,"Mustard, ketchup and onions on a hotdog are all types of what ",Condiments ,General,Who wrote the Science Fiction novel Slaughterhouse Five,Kurt Vonnegut,General,Carolina what alice cooper album simulated an execution during a song,Killer +General,In the USA where would you see a crossbuck,X on railroad crossing,General,If you were caught pandiculating what were you doing,Yawning,Music,"Which Album Opened With ""Start Me Up""",Tattoo You +General,In which sport is the Wingate trophy awarded,Lacrosse,Geography,What's the opposite of the orient ,The occident ,General,In music what is a pentatonic scale,A scale of five notes only +General,In which African country is the city of Bulawayo,Zimbabwe,General,What are pulex irritans,Human fleas,General,What name is given to the smallest type of liquor glass,Cordial +General,Ferdinand and Isabella were joint rulers in which country,Spain, History & Holidays,In What Year Was The Tomb Of Tutankhamen Discovered ,1922 ,General,What is the Capital of: Azerbaijan,Baku baki +General,The Paramours changed their name to what gaining fame,Righteous Brothers,Geography,What is the capital of Georgia,Tbilisi, History & Holidays,With Which Ship Did Francis Drake Circumnavigate The World? ,"The Pelican, Renamed The Golden Hind After Clearing Cape Horn " +Sports & Leisure,With which sport is Babe Ruth associated ?,Baseball,Entertainment,He directed the movie E.T.,Stephen Spielberg,General,From which date does the legal term ' Time Immemorial' apply,1189 the death of henry ii +General,In what musical note do most toilets flush,E Flat,General,What company makes Pampers disposable diapers,Proctor & gamble,General,"Who In The World Of Music Has The Real Name ""Artis Ivey Jnr""",Coolio +General,What was Jane Wyman Reagan's birth name?,Sarah Jane Fulks,Technology & Video Games,Who is Mega Man's sister? ,Roll,General,Where in the body is the labrynth,Ear +Music,How Many Of The Top Ten Singles Of The 60's Were By The Beatles,5,General,Who starred as Jimmy Porter in the film version of John Osborne's play Look Back in Anger,Richard burton,General,Where would you find lagan,In sea overboard attached to float +General,Phagophobia is the fear of,Swallowing,General,What Device Is Edwin T Holmes Credited With Inventing in 1958,The Burglar Alarm,General,What is the fear of everything known as,Panophobia +General,"For a Non-Muslimans , the tour to Mecca is",Prohibited,General,What was the traditional ancient Persian new years day gift,Eggs,General,What is celebrated on April 1st?,April Fools Day +General,"On three's company,what was Chrissy's father's ocupation?",A Reverend,General,In which sport would you compete for the Grand Challenge Cup,Rowing,General,"In the show ""The Equalizer"",what did the hero (McCall) call his former superior from the ""agency""?",Control +General,How old was shirley temple when she made her last film,21,General,"In 1864, where were over 300 indians massacred colorado",Sand creek,General,"Where was 'g.i joe' introduced on february 9, 1964",Annual american +General,"What were the first names of T E Lawrence, known as Lawrence of Arabia",Thomas edward,General,What artificial waterway links the black sea to the baltic via leningrad,Volga-baltic canal,General,What was the first day of the year in the Roman calendar,25th March +General,When did Castro take power in Cuba,1959,General,"Who was the author of ""Shirley Valentine""",Willy russell,General,What swings in a grandfather clock,Pendulum +General,Who does the Beatles song The fool on the Hill refer too,Galileo,Geography,Which Central American country extends furthest north,Belize,Geography,What major city is on an island in the st lawrence river ,Montreal  +General,What were more than 99.9% of all the animal species that have ever lived on earth before the coming of man,Extinct,General,What city stands on the Maas River,Rotterdam,Science & Nature,Which Is The Coldest Planet In The Solar System? ,Pluto  +People & Places,Who Did John Mcenroe Play 3 Times In Wimbledon Finals ,Bjorn Borg ,Entertainment,In which film did Henry Fonda play a fallen priest?,The Fugitive,General,For what new england woman's college was the first brand of wrigley's gum named,Vassar +General,Who wrote the books on which the television series The Darling lands of May was based,H e bates,General,What name is given to a full length mirror on a swivel,Cheval, History & Holidays,Which doctor offered good advice in When Youre In Love With A Beautiful Woman? ,Dr Hook  +General,What is Chartres Cathedral in France famous for having 160 of,Stained glass windows,General,"On Three's Company,what was Jack's retaurant that he opened and was head chef ?",Jack's Bistro,General,"Who appears on the 10,000 dollar (US) note?",Salmon Chase +General,Who built the concorde,Britain and france,General,"What's the international radio code word for the letter ""X""",Xray,General,What Nationality Was Henry The VIII's Fourth Wife Anne Of Cleeves,German +General,The Beatles film Help was dedicated to the inventor of what,Sewing Machine – Elias Howe,General,In which city is the Blue Mosque,Istanbul, History & Holidays,Marvin Lee Aaday Is The Real Name Of Which Rock Star Who Also Appeared As Eddie The Ex Delievery Boy In The Movie The Rocky Horror Picture Show ,Meatloaf  +General,What flower is the symbol of culture,The Lotus, Entertainment,What Magician walked through the great wall of china?,David Copperfield,General,Which garment gets it's name from the Latin to cover,Toga +Science & Nature,Which Is The Most Common Plant In The World? ,Grass ,Music,Name The Musical Major Whose Plane Dissapeared Somewhere Over The English Channel In December 1944,Glen Miller,People & Places,After Which Famous Person In History Was The Teddy Bear Named ,Theodore Roosevelt  +General,What was Robert Browning's pet name for his wife Elizabeth,Portuguese, History & Holidays,Who was captain of 'The Mayflower'?,Miles Standish,General,In what prison did Nelson Mandela spend 19 of 27 years in jail,Robben Island +General,Where were Archie and Edith Bunker's chairs enshrined?,The Smithsonian Institute, Geography,Bogota is the capital of ______?,Colombia,Entertainment,In the cartoons who was Hokie Wolf's sidekick?,Ding +General,Someone with initials DD after their name has what qualification,Doctor of Divinity,Music,"Who Had A Hit With With ""Ebeneezer Goode""",The Shamen,Geography,What is the capital of Jordan,Amman +General,Who courageously proved that lightning was electricity,Benjamin franklin,General,"Brahma, Vishnu & Shiva are gods in which religion",Hindu,General,In which language does God Jul mean happy Xmas,Swedish +General,Mumbai is the modern name of which city,Bombay,Music,"""Noddy Holder, Dave Hill, Jimmy Lea, Don Powell) Were All Members Of Which Popular Band",Slade, Geography,What is the basic unit of currency for Cuba ?,Peso +General,"What's the sky king's home, near the town of grover, called",Flying crown,General,In WW2 in what French city did the Germans surrender,Reims,General,A monetary inflation at a very high rate,Hyperinflation +General,What is a baby eel,Elver,General,What puppet was based on the creators former wife Sylvia,Lady Penelope,Music,"Who Were Dane, Wayne, Mark And Bobak",Another Level +Entertainment,On what T.V. show could Tom Terrific be found?,Captain Kangaroo,General,In Hawaii what is the annual Kona festival,Coffee picking contest,General,What author criticised evangelism in his novel Elmer Gantry,Sinclair Lewis +General,"In Greek mythology, who helped theseus escape the labyrinth",Ariadne,General,Which Norwegian politicians name became a word for traitor,Vidkun Quisling,Science & Nature,The process of removing salt from sea water is known as ________.,Desalination +Food & Drink,"Add this to milk, eggs, and sugar to make a Tom and Jerry.",Rum,General,The Davis Strait lies between Canada and where,Greenland,General,"Who Provided The Voice Of ""Bruce"" In The Disney Animated Movie ""Finding Nemo""",Barry Humphries +General,Approximately how many times a minute does lightning strike the earth?,Six thousand,General,Who wrote 'the time machine',H.g wells,General,"John Paul Getty, world's richest man had what in his house",A Payphone +Science & Nature,Which Is The Disease Tetanus Also Known As ,Lockjaw ,General,What is PLO an abbreviation for ?,Palestine Liberation Organization,General,In the food industry what is TVP - i.e. what's it stand for,Textured Vegetable Protein +General,"In the year 1000, leif erikson was the first european to set foot on ______",North america, Geography,What is the capital of Montana?,Helena,General,A charge of dwai is for what,Driving while ability impaired +General,How did Alice get into the land of the living chess pieces,Through the looking glass,General,Phobia What is the fear of being evaluated negatively known as,Social, Geography,What is the capital of Colombia?,Bogota +Geography,In what country is the mekong river delta ,Vietnam ,General,What was gary puckett's backup band,Union gap,General,What is another word for a female sheep?,Ewe +Science & Nature,Who Was Inspired By An Apple Falliing ,Sir Isaac Newton ,General,What symbols name derives from Greek for star,Asterisk,General,Who was nicknamed The Admiral of the Mosquitoes,Christopher Columbus +Science & Nature,What Is The Male Sex Hormone Called ,Testosterone ,Sports & Leisure,Who was the first footballer to be knighted in this country? ,Sir Stanley Matthews ,General,The bander macaque has which commoner name,Rhesus Monkey +General,What is the fear of pellagra known as,Pellagrophobia, History & Holidays,Where were numerous French nuclear tests conducted?,Muraroa Atoll,Art & Literature,In Which City Is Leonardo Da Vinnci's (Last Supper) Displayed ,Milan  +General,What was willie mosconi famed for shooting,Pool,General,In the famous song my true love sent me nine what,Drummers drumming,General,What is the capital of Romania?,Bucharest + Geography,Who is regarded as the most influential monarch of Russian Romanov Dynasty?,Peter I,Geography,What Is The Capital Of The Philippines? ,Manila ,General,Kathisophobia is the fear of,Sitting down +General,Which organic compound is the psychoactive ingredient in Budweiser?,Ethanol,Sports & Leisure,Who was the first american to win the Formula 1 championship?,Phil Hill,General,What two states are not connected to the main part of U.S.,Hawaii & alsaka alaska & hawaii +General,Scottish sailor Alexander Selkirk became inspiration for what novel,Robinson,General,Who is the Patron Saint of Accountants,Saint Matthew,Geography,Name the smallest of the Great Lakes.,Ontario +Food & Drink,Who invented the Egg Mcmuffin?,Ed Peterson,General,Who wrote The Ipcress File,Len deighton,General,What should you give after 15 years of marriage,Crystal +Sports & Leisure,How many points are awarded for a safety touch in football,Two,General,Who is the roman goddess of childbirth,Carmenta,General,In which US state is its highest mountain,Alaska - Mount McKinley +General,In Baseball slang who would use The tools of Ignorance,Catchers Equipment,Music,Who Had A Really Good Saturday Night In 1994,Whigfield,Sports & Leisure,"Which Footballer Played For The La Aztecs, Fort Lauderdale Strikers & San Jose Earthquakes? ",George Best  +General,In the suburbs of which modern Egyptian city would you find the pyramids,Cairo,General,What is the more popular narne of the plants belonging to the genus galanthus,Snowdrop,General,In the Simpsons who is the godfather of the Springfield mafia,Don Vittorio +General,What is an 'aceituna' in english,Olive,Music,"When Did Pop Innovators Kraftwerk Drive Autobahn To No 11 (1973, 1975, 1977, 1979)",1975,Music,"George Michaels ""Father Figure"" Was Released In 1986 Or 1988",1988 +Music,Name Paula Abduls First Ever Album,Forever Your Girl,General,A can of orange crush appears on every episode what TV series,ER,General,What alloy do copper and tin form,Bronze + History & Holidays,Who did the los angeles times endorse in the 1964 preisential election ,Barry goldwater ,General,"She was a reletive of Sir Philip Sydney and the Countess of Pembroke, authored the famous sonnet sequence Pamphilia to Amphilanthus?",Lady Mary Wroth,General,What's the second most spoken language on earth,English +General,What is the longest river in western Europe,The Volga,General,Which car won the 1953 italian grand prix,Maserati,General,What is the study of heredity called,Genetics +Music,Name Disco Tex's group?,The Sex-O-Lettes,General,What soviet republic was devastated by an earthquake in 1988,Armenia,Science & Nature,What Is A Micron ,One Millionth Of A Meter  +General, An animal stuffer is a(n) ________.,Taxidermist,General,What organisation recently banned in Russia as paramilitary,Salvation Army,Music,In Which Year Did Janis Joplin Die,1970 +General,What has a type of hair on their eyes,Honeybees,General,On MTV's Blame Game what is the name of the Judge?,Judge Reed,General,Which was the first 'spaghetti western' starring Clint Eastwood,A fistful of dollars +Music,Who Was The Lead Singer Of Bad Manners,Buster Bloodvessel,General,Aegis belonged to Zeus what was Aegis,A Shield, History & Holidays,Who is Prince Vladimir Tepes better known as?,Dracula +General,Desert antelope that originally ranged from the western Sahara and Mauritania to Egypt and the Sudan,Addax,General,Who replaced moses as the prophet of the israelites,Joshua,Food & Drink,Which European nation were the first to drink tea? ,The Dutch  +General,A tornado at sea is called a ___,Waterspout, History & Holidays,He received the Nobel Peace Prize in 1964 for his civil rights leadership.,Martin luther king jr,General,The u.s has never lost a war where they used ______,Mules +General,Only one world team horseracing event at Ascot what trophy,Shergar Cup,General,"Ornament of ribs, bars, etc. in panels or screens, as in the upper part of a Gothic window.",Tracery,General,What is the Capital of: Indonesia,Jakarta +Mathematics,Benoit Mandelbrot discovered what mathematical structures?,Fractals,Science & Nature," Cattle branding in the United States did not originate in the West. It began in __________ in the mid_19th century, when farmers were required by law to mark all their pigs.",Connecticut,General,In Japan what colour car is reserved for the royal family only,Maroon +General,How many days where there in 1976,366,General,What crime did Sid Vicious commit in 1978,Murder,General,Where were numerous french nuclear tests conducted,Muraroa atoll +General,Pocrescophobia is the fear of,Gaining weight,General,In what series of books did The Empress of Blandings appear,Jeeves and Wooster a pig,General,Who was nominated for a BAFTA Award in 1997 as a result of an interview with the Duchess of York,Ruby wax +General,Which French athlete won both the 200m and the 400m on the track at the 1996 Atlanta Olympic Games,Maria-jose perec, History & Holidays,Composer George Gershwin died in1937 at the age of 38. How did he die? ,Brain Tumour , History & Holidays,What time of day is known as the 'Devils Dancing Hour' ,Midnight  +General,Which Yorkshire river is formed by the confluence of the Swale and Ure,Ouse,General,"Acute, infectious, contagious disease of the respiratory tract, especially the trachea",Influenza,General,X only letter in alphabet that there is no name for who using,The Devil +General,Ed Moses won the 400 metre hurdle title in which year,1984,General,What falls out with phalacrosis,Hair,General,Who wrote the music for the 1948 film 'Scott of the Antarctic' and made a symphony out of it,Vaughan williams +General,"What part of the body has a crown, a neck & a root",Tooth,General,Smith most common USA name what's second,Johnson,General,Which Greek island is said to be the birthplace of Apollo,Delos +General,The majority of small toothed whales are called_______,Dolphins,Music,What Beatles song won a Grammy as the Best Song of 1966?,Michelle,Music,"She Was Backed By The New Bohemians And Had A Hit With ""What I Am"". Name Her?",Edie Brickell +General,A group of ducks is called,Brace,General,Followers of which religion use prayer wheels,Buddhism,General,Of what is 98% of the weight of water made,Oxygen +General,What was built by the inmates of Changi Prison Camp,Burma Railroad,General,What were the names of Amanda's boys on Scarecrow and Mrs. King?,Phillip and Jamie,General,Who is Christina Claire Ciminella otherwise known as?,Wynonna Judd +General,What sport features the fastest moving ball,Jai alai,General,Louis Pasteur developed a vaccine for what,Rabies,General,Fall Down' was a hit for which Santa Barbara band,Toad the Wet Sprocket +General,PG Woodhouse books Bertie Wooster used what London Club,Drones,General,ABC Is One Of The National Newspapers In Which City?,Madrid,General,"What Dodger struck out 2,396 batters in 2,324 innings",Sandy koufax +Science & Nature,Who devised the periodic table of elements?,Mendelev,General,In India what is a khidmutgar,A Waiter,General,What is the english equivalent of the name ian,John +General,Wakame Tengusa and Mozuku are Japanese what,Edible seaweed,General,What is the fear of relatives known as,Syngenesophobia, History & Holidays,What Left Lisbon On May 28 th 1588 & Did Not Return Intact ,The Spanish Armada  +General,Giovidi is what day in Italian,Thursday,General,Which country makes the most films per year,India,Geography,Where is Beacon Street,Boston +Sports & Leisure,As We All Know David Beckham Now Plays For US Team LA Galaxy But Who Is The Coach For La Galaxy ,Ruud Gullit ,General,What does the name Barbara mean - from Greek,Strange or Foreign,General,"Who's the ""me"" in the book elvis and me",Priscilla presley +General,"On IRC, how do you ask age, sex, location?",Asl,General,What was the former German name of the Czech town of Ceske Budejovice,Budweis,Sports & Leisure,What is the heaviest class of weight-lifting?,Super heavyweight +Music,Name The Only Act To Have Had 3 Christmas No.1's In The 90's?,The Spice Girls,General,What common British river name come from Celtic for river,Avon,General,"On The Day Of Elizabeth II's Coronation, Another Major Historical Event Took Place What Was It?",Mount Everest Conquered +Food & Drink,From which fruit is the liqueur Kirsh made?,Cherry,Geography,What American city is known as Little Havana,Miami,Food & Drink,"From Where Does Sherry, The Fortified Wine Originate ",Jerez In Spain  +General,Who starrs in the show 'moesha',Brady,General,Animal or plant without the normal pigmentation of its species,Albino,Sports & Leisure,Who Was The First Football Player To Score A Hat -Trick In The World Cup finals? ,Geoff Hurst  + History & Holidays,Which Wild West legend was born Henry McCarty? ,"Billy the Kid, alias William H Bonney ", History & Holidays,What American city was called New Amsterdam in the early 17th century?,New York, History & Holidays,"Who Had An 80's Hit With The Song 'Two of Us,' ",Grover Washington with Bill Withers  +General,"In denmark, who takes the place of santa claus",Nisse,Art & Literature,Who created 'Horton' the elephant?,Dr. Seuss,General,Why was Boris Pasternak's Nobel Prize for literature in 1958 excetional,He refused +General,Which World Famous news agency began life in 1850 using carrier pigeons,Reuters,General,"Which US state was known as ""The mother of Presidents""",Virginia 4 out of 1st 5,General,Name given to the religious of Iran,Ayatollah +General,"Which country's national flag consists of five-pointed yellow stars, one large and four smaller, in the top left corner on a red field",China,General,What Did Dick Whittington Achieve That Jeffrey Archer Didn't,Lord Mayor Of London,Science & Nature,The smallest portion of a substance capable of existing independently and retaining its original properties is a(n) __________.,Molecule +General,What Was The first TV Show To Give Away One Million Pounds,TFI Friday,General,What book starts with the words - Call me Ishmael,Moby Dick,General,In what city is the Uffizi art gallery,Florence +General,Sports: what do the letters al stand for,American league,General,What colour lenses are required to view a 3-d film,Red and green,Science & Nature,Which Is The Only Mucsle Not Atteched At Both Ends ,The Tongue  +General,"Who portrayed Jeannie in ""I Dream Of Jeannie""",Barbara eden,Geography,"What is the largest country in Africa, by area",Sudan,Geography,What city is associated with Alcatraz,San francisco + Geography,Which state is the Evergreen State?,Washington,General,"Black, cementlike material varying in consistency at room temperature from solid to semisolid",Asphalt, Geography,What is the basic unit of currency for Mexico ?,Peso +General,To who did boris spassky lose in the world chess championship,Bobby fischer,General,What was the name of the high school in the movie Grease,Rydell,Entertainment,What was Garth's last name in 'Wayne's World'?,Algar +General,What is the capital of connecticut,Hartford,General,What Italian building material translates as baked earth,Terracotta,General,Which artist painted the 'Arnolfini Wedding'?,Jan Van Eyck +General,"In arabian mythology, what is the spirit of a murdered man seeking to avenge his death",Afrit,General,Who occupies taiwan,Nationalist chinese,General,In Wisconsin its illegal to do what during your wife's orgasm,Fire a Gun +General,Which TV gangster owns the nightclub called The Ba Da Bing,Tony Soprano,Music,Now That Weve Found Love Was A Hit For Which Jamaican Group,Third World,Music,"Elvis Costello's First Single ""Less Than Zero"" Was About Which Fascist Leader",Oswald Moseley +General,What colour is the cross on the Greek Flag,White, History & Holidays,Which re-released Queen anthem topped the Christmas charts in 1991 ,Bohemian Rhapsody , Geography,What is the world's largest desert?,Sahara Desert +General,Development of agricultural ecosystems intended to be complete and self sustaining,Permaculture,General,What 1970 movie hit was banned on military bases,MASH,Music,"Father gets up late for work, Mother has to iron his shirt, Then she sends the kids to school, Sees them off with a small kiss, She's the one they're going to miss",Madness / Our House +Art & Literature,Who wrote The Canterbury Tales?,Geoffrey Chaucer,General,Cleveland four u.s presidents have served entire terms without having a ______,Vice,General,"In Greek mythology, where did perseus kill his grandfather",Larrisan games +General,A disease of the brain,Encephalopathy,Science & Nature,What is an organism called that lives on or in a host animal?,Parasite,General,Actor “John Altman” Is Best Known For Playing Which TV Soap Character ?,Nasty Nick Cotton (Eastenders) +General,What is embolia,Hesitations in speech, History & Holidays,What Space Rocket Was Destroyed During Ground Tests At Cape Canaveral In 1967 ,Apollo 1 ,General,"Famous for blues and jazz, in which city is Basin Street",New orleans +General,And what the least - two tablespoons per person,Egypt,General,Which gas forms bubbles in the bloodstream when a diver gets the bends,Nitrogen,General,Name the dogs in Magnum PI,Zeus Apollo +Religion & Mythology,What 'S' was a king of israel who was famous for his wisdom?,Solomon, History & Holidays,Who first conceived the idea of a Christmas tree ,Prince Albert ,General,The character Charley Allnut appeared in which film,The african queen +People & Places,Which individual bought one of Leonardo Da Vinci's notebooks for over 19 million pounds in 1994? ,Bill Gates ,Sports & Leisure,In Football Who Was Liverpool's Captain When They Won The European Cup Final In 1977? ,Emlyn Hughes ,Geography,"The smallest U.S. state in area, west of the Mississippi River, is _____________",Hawaii +General,What is the Capital of: Papua New Guinea,Port moresby,General,What does 4wd on a car indicate,Four wheel drive,General,What's the locale for the farmer's daughter,Washington dc +Art & Literature,Who did Macduff kill?,Macbeth,General,What is the flower that stands for: always lovely,Indian double pink,General,"In 1981, who won best actress emmy for the tv sitcom 'the jeffersons'",Isabel +Science & Nature,What Temperatutre Is Absolute Zero ,-273 Degrees C Or Kelvin - 459 Degrees F ,General,International Airline Registrations SX is what country,Greece,General,"A robin's egg is blue, but if you put it in vinegar for thirty days, what color does it turn",Yellow + History & Holidays,Who sailed to the new world in 'The mayflower'?,Pilgrims,General,What is a group of this animal called: Bird,Flock flight congregation volery,General,What was the capitol of Russia before Moscow,Saint Petersburg +General,What is myrmecology the study of,Ants,General,"Which city is a 'player with railroads, and the nation's freight handler'?",Chicago,Music,Name The First Big UK Hit For The Chiffons In 1963,He's So Fine + History & Holidays,"Who said: ""Let them eat cake""?",Marie Antoinette, History & Holidays,Who Released The 70's Album Entitled Layla & other assorted love songs ,Derek and the Dominos ,General,Credits on all Bond films finish with 4 words James Bond what,James Bond will return +General,Trunk lid name the only president to be married for the first time while in office,Grover cleveland,General,Of what is pedology the study,Soil,Music,What song got to number 1 in 1993 by the 'Bluebells'?,Young At Heart +General,The US uses up 7000 tons of what annually,Currency - it is shredded,Music,Who did Michael Jackson Marry In 1994,Lisa Marie Presley,Sports & Leisure,Which American Football Team Are Known As The Chargers? ,San Diego Chargers  +Science & Nature, It takes about 50 hours for a snake to digest one __________,Frog,General,In Disney's Bedknobs and Broomsticks what magic words used,Bibbity Bobbity Boo,General,Jack and Jill went up a ____ to fetch a pail of water?,Hill +General,Which country did boxer Lennox Lewis represent in the 1988 Olympics,Canada,General,Name the only actress with 4 Best Drama Actress awards,Tyne Daly,General,What is the square root of 4096,64 +General,How many records does an album have to sell in the UK to go Platinum?,"300,000",General,Whose capture by the russians brings the bulging warrior back to rambo iii,Colonel trautman's col trautman's,General,Who was the last Emperor of France,Napoleon III +General,What's the fastest sea dwelling mammal,The dolphin,General,"What are Blur Crow, Brimstone, Owl and Ringlet types of",Butterflies,General,The force that brings moving bodies to a halt is _________,Friction +Entertainment,What was Ben Stiller's character called in 'Mystery Men'?,Mr. Furious,General,Who is credited with inventing the transistor,Dr william shockley,General,Which group had the hit album 'White on Blonde',Texas +General,At which olympics did Zola Budd accidentally trip Mary Decker,1984,Science & Nature,What Is Another Name For The Large Intestines ,The Colon ,General,What did victorian women try to enlarge by bathing in strawberries,Breasts +General,In Penny Lane what is the nurse selling from a tray,Poppies,General,What's spain's biggest source of income,Tourism, History & Holidays,This Nazi leader had his six children poisoned prior to his own death.,Joseph Goebbels +General,What religion does a 'rabbi' practise,Judaism,General,"Eight thousand in the middle ages, where did people believe the seat of intelligence was",Heart,Science & Nature,What Do Koala Bears Live On? ,Eucalyptus Leaves  +General,What is the common name for hydrogen oxide,Water,General,Collective nouns - A Descent of what creatures,Woodpeckers,Music,Peter Sellers Recorded A Spoof Of Which Beatles Song In The Style Of Laurence Oliviers Richard 3rd,A Hard Days Night +General,What is the ninth letter of the Greek alphabet,Iota,General,In New Zealand what is morepork,A Bird - call sounds like morepork,Toys & Games,A bridge hand with no cards in one suit is said to have a _______.,Void +General,"What caused a separation of Baja, California and the rest of Mexico",The San,General,In the bible who slew a quarter of the worlds population,Cain killing Abel,Music,Which Song Did Dennis Waterman Sing As The Theme Tune To Minder,I Could Be So Good For You +Music,By What Name Is Pianist Ferdinand Joseph Lemott Remembered,Jelly Roll Morton,Geography,On which River does the City of New York stand ,The Hudson ,General,What country makes Sukhindol wine,Bulgaria +General,What european language is most closely related to mongolian,Finnish,General,What kind of pendulum twists instead of swings,Torsional,General,Accra is the capital of ______,Ghana +Science & Nature,For what is the chemical formula H2O2?,Hydrogen peroxide,General,Which animal secretes the pigment sepia,Cuttlefish,General,If the doctor gave you salversan he would be treating your what,Syphilis +General,What state is 'the hoosier state',Indiana,General,What us state includes the telephone area code 615,Tennessee,General,In Which Sport Would You Perform A Fliffus?,Trampolening +General,Who wrote the children's story The Old man of Lochnagar,Prince Charles,Music,Which British band was led by Tim Booth?,James,General,Name of the Major General who invented the exploding shell,Henry Shrapnel +Music,"Which Was The First Group Beginning With The Letter ""V"" To Have A UK No.1 Hit",Village People,General,What is the first name of the French painter Matisse,Henri,General,Chinese cooking what's special about Wolfs hearts Dogs lungs,Only things not used +General,What is e.g. an abbreviation of ?,Exempli gratia,Art & Literature,Who dubbed Australia 'the lucky country'?,Donald Horne, History & Holidays,What was Elvis Presley's first motion picture? ,Love Me Tender  +General,Which Animated Movie Became The First To Receive An Oscar Nomination For Best Picture,Beauty And The Beast,General,Which Brand Had To Make An Apology When They Unveiled Their Logo As It Looked Very Similar To The Arabic Symbol For Allah?,Nike,General,Thread made from intestines of sheep used for strings of musical instruments,Catgut +Music,What Was The Name Of Elvis Presley's Backing Group,The Jordinaires,Sports & Leisure,"What sport has four different color codes for the balls, ranging from yellow for hot conditions to blue for cold ",Squash ,General,Name both of the cities to represent a letter in the phonetic alphabet,Lima & quebec +General,Fear of dryness is called,Xerophobia,General,Whats the highest mountain in the Alps,Mont blanc,General,What is a braquette,Fake Dick extension 14th century +General,"If body temperature was 86 degrees, how many years would a man man live?",200,General,In Texas its illegal to swear in front of what,A Corpse,General,Legal Terms: The people chosen to render a verdict in a court.,Jury +General,Whose yacht was called Honey Fitz,John Fitzgerald Kennedy,General,The wwii air attack on the Ploesti oil field in Romania was known as,Operation soapsuds, History & Holidays,What purged the Great Plague of London ?,Great Fire of London +General,What Does The Initial 'T' Stand For In Cat Scan,Tomography,General,Bengal Dimension 6 Falcon 3 possible names what was chosen,Nike - but others were considered options,General,What is the name of the spaceship in the film 'Alien',Nostromo + Geography,What is the capital of Croatia?,Zagreb,General,Which country controlled Angola prior to its independence,Portugal,General,Col Meriweather Lewis Clark Jr developed rules for what in US,Horseracing +General,Who are the two most translated English writers,Shakespeare – Agatha Christie,General,Fill in the blank: when in ____ do as the Romans do,Rome, History & Holidays,Who Released The 70's Album Entitled My Aim is True ,Elvis Costello  +General,Captain Flint buried his treasure where ( Ben Gunn dug it up ),Skeleton Island,Entertainment,"Who played ""Robin"" to Val Kilmer's ""Batman""?",Christopher O'Donnell,General,"According to tradition, which animals desert a sinking ship",Rats +General,What is the full name of lake tonka,Lake minnetonka,General,Ganesha is the hindu god of ______,Good fortune,General,A container for carrying a corpse from the scene of an accident etc.,Body bag +General,What Type Of Acid Is Contained Within A Bee Sting,Formic,General,U.S. captials Arizona,Phoenix,General,Earth's outer layer of surface soil or crust is called the,Lithosphere +General,1777 George Macintosh created a red dye cudbear what is it,Litmus – Ammonia and lichens,Food & Drink,Traditionally what type of meat is used to make a mousakka? ,Lamb ,General,Which long motor race is held in France every June,Le mans 24 hour +Music,David Sylvian Was The Former Frontman Of Which Group,Japan,General,What does a notaphile collect,Bank notes, History & Holidays,What does LL Cool J.'s name stand for? ,Ladies Love Cool James  +General,What is the national religious folk cult of haiti,Voodoo,General,Where will you find the greater & lesser trochanters,The femur femur,General,What averted an arab boycott of the 1948 summer olympics,Israel's exclusion +General,Lobster like freshwater crustacean,Crayfish,General,Lentigines is the medical term for what,Freckles,Sports & Leisure,In 1996 Who Lit The Olympic Flame In Atlanta Georgia ,Muhammad Ali  +Music,What was Buddy Holly’s real first name?,Charles Hardin Holly,General,An octopus has how many hearts,Three, Geography,In what Province Is Dublin?,Leinster +General,What did Sir Arnold Lunn begin in Switzerland,Slalom skiing,General,During what was alcohol was made illegal,Prohibition,General,The first battle in the War of American Independence took place where,Lexington +General,What is a group of horses,Herd,General,What Chinese zodiac sign is this year?,Snake,General,Jean-Claude Killy was a famous name in which sport?,Skiing +General,What is a hypocaust,Roman Heating System,General,Which vegetable is also a flower,Broccoli,General,Kinetophobia is a fear of ______,Movement +General,"Who said - ""A woman only a woman - good cigar is a smoke""",Rudyard Kipling,Sports & Leisure,Which piece of sports equipment is a Louisville slugger? ,A Baseball Bat ,General,"In cookery, what term is used for the sprinkling of food lightly and evenly with flour, sugar, etc",Dredging +General,What is the Latin word for to roll,Volvo,General,What number is at 6 oclock on a dartboard,Three,General,What is the worlds largest sea (in area),South China +General,What was found by the River Deben in East Anglia in 1939?,Sutton Hoo Burial Ship,Science & Nature,Hydrogen Hydroxide is more commonly known as what,Water,General,In Which Sport Would You Stand At Silly Point,Cricket +General,What colour is angelica,Green,General,Who directed the film M,A s h robert altman,General,Who was the last Roman Catholic King of England,James II +General,"Name of George Clinton's corporation which put out ""One NAtion Under A Groove""",Parliafunkadelicament thang,General,In which film were the entire cast nominated for oscars,Who's afraid of,General,Name was Richard Kimble's favourite alias in the TV series,Jim +Music,"In Which Year Was YMCA First Released 1975, 1977 Or 1979",1979,Science & Nature,Who wrote 'A Brief History of Time'?,Stephen Hawking,General,Maieusiophobia is the fear of,Childbirth +General,Which European country suffered one of the world's highest ever rates of hyper-inflation in 1946?,Hungary,General,What flowers name derives from the Greek word for testicle,Orchid,General,What is a group of this animal called: Gorilla,Band +General,Who wrote the book - Call of the Wild,Jack London,General,What mammal has hair - on the soles of its feet,Polar Bear,General,What is the only country that is also a continent,Australia +Science & Nature,What animal can hop as fast as 40 mph?,Kangaroo,General,Who founded Methodism in 1738,John Wesley,Art & Literature,Which publishing company was founded in London in 1935 by Allen Lane? ,Penguin  +General,Ingemar Stenmark won record 85 world cup races in what sport,Skiing,General,Cockroaches will eat anything except what,Cucumbers,General,What is a group of cranes,Termites +General,In what sport do you need brooms and brushes,Curling,Music,Which Singer Fronted Bronski Beat And The Communards,Jimmy Sommerville,General,Your nares are your _____,Nostrils +General,Traditional English dance in which dancers form two facing lines.,Country dance,General,Kinetophobia is the fear of,Movement motion,Entertainment,"With which period in music do we associate composers such as Beethoven, Mozart and Haydn?",Classical period +Music,Which Instrument Connects Glen Miller & Tommy Dorsey,Trombone,General,What killed half the US soldiers in WW1,1918 Flu Epidemic,General, The distance around the outside of a circle is its ________.,Circumference +General,Jockey / Author Dick Francis what injury on his wedding day,Broken collar bone,Science & Nature, Dead sponges can resist bacterial decay for more than five years when submerged in __________,Fresh water,General,What is a group of leopards called,Leap +General,Which major car manufacturer will take over the Benetton Formula 1 team next years,Renault,General,"Who played the respectable hooker in ""From here to Eternity""",Donna Reed,General,Where is appomattox,Virginia +General,What are the two primary ingredients in cracker jack,Popcorn and,Sports & Leisure,Which 2 steves were record breaking middle distance runners in the 70's 80's ,Steve Cram & Steve Ovett , History & Holidays,Which Famous Female Solo Artist Was Born On Christmas Day 1971 ,Dido  +General,Bob Clampett created which character in 1938,Bugs Bunny, History & Holidays,In which country was Adolf Hitler born,Austria, History & Holidays,Which Lancashire Town Has A Name That Means A Small Brass Wind Instrument Used For Summoning Witches & Warlocks ,Oswaldtwistle  +General,"What Japanese word ironically means "" May you live forever """,Banzai,General,Tom sawyer was the first novel written on a ______,Typewriter,General,1987 A Philadelphia Councillor bill banned carrying what in public,Snakes +General,"In his play The Birds, what name did Aristophanes give to the birds' kingdom in the sky",Cloud cuckoo land,General,What causes baker's itch,Yeast,General,John Books was the final role of which actor,John Wayne in The Shootist +General,A sun-dried grape is known as a(n) ___________,Raisin,General,"Moussaka"" is a traditional dish from which country",Greece,People & Places,Who Was Known As The Forces Sweetheart ,Dame Vera Lynn  +Music,How Many Uk No 1’s Did The Beatles Have In Total,18,General,"In international car registrations, which country has the letters RA",Argentina,General,Which building commemorates the Great Fire of London?,Monument +General,What does an ecclesiophobic fear,Churches,Science & Nature,How Does A Giraffe Clean Its Ears? ,With Its Tongue ,General,What does the boys name Neil mean,Champion - Irish + History & Holidays,Which emperor made his horse a senator?,Caligula,General,"Bear, Bird, Goat, Eagle, Swan and Rabbit what links in Ireland",All Islands,General,"According to the Gospel of Saint John, what 'lay on the other side of the Brook of Cedron'",Garden of gethsemane +General,On which great lake are buffalo and cleveland,Lake erie,Geography,What continent is sierre leone in ,Africa ,General,What vegetable gets its name from old French / Latin for milk,Lettuce +General,What is the first ingredient in most soda pops,Carbonated water,General,What is the criminal number of jean valjean in 'les miserables',24601,Sports & Leisure,In Which Sport Do The Participants Wear Sheep Skin Nose Bands ,Horse Racing  +General,"In Ferris Bueller's Day Off, what is the Principal's name?",Mr. Ed Rooney,General,Who devised the idea of a flat rate postal charge,Charles Babbage,General,What is the nickname for New Orleans,Crescent city +General,In the song My Darling Clemantine how did Clemantine die,Drowning,General,What are 35% of people using personal ads for dating,Married,General,Pnigophobia is the fear of,Choking being smothered +General,Who or what was introduced to the USA in 1964,G I Joe,General,What crime causes the second most number arrests in USA,Drink Driving,General,Anita Lonsborough Became The First Female To Win What In 1962 (She Won Medals At The Olympics But Wasnt The First),BBC Sports Personality Of The Year +Sports & Leisure,Which Increasingly Popular Winter Sport Was Introduced As An Official Event At The 1998 Games ,Snow Boarding ,Science & Nature,What Disorder Results In Compulsive Eating And Induced Vomiting ,Bulimia ,General,"What large fish earned the nickname ""cheetah of the sea"" for its speed in the water",Tuna the tuna +General,What is a group of this animal called: Colt,Rag,General,What is vodka made from,Grain or potatoes,General,"In America during Prohibition, what name was given to an illegal drinking bar",Speakeasy +Music,"Who ""Beat The Clock"" To Number 10 In 1979",Sparks,General,1949 Popular Mechanics said that future ones less 1.5 tons?,Computers,General,April 20 1896 was the first time people paid to do what,See a movie in NY +General,Quinine is obtained from what part of an evergreen tree,Dried Bark Bark,General,What animal has no natural predators,Tiger,General,Robin Williams dressed in drag for which 1993 film,Mrs Doubtfire +General,"What is a flat, round hat sometimes worn by soldiers",Beret,Science & Nature," __________, an essential ingredient of many expensive cosmetics, is, in its native form, a foul_smelling, waxy, tarlike substance extracted from the fleece of sheep.",Lanolin,Science & Nature,In Internet Chatroom Speak If You Wrote The Letters 'YGLT'' What Would It Mean ,You're Gonna Love This  +General,What company developed the dot matrix printer for 64 Olympics,Seiko,General,Where did georgo and laszlo biro invented the ball point pen,Hungary,Geography,Of Which Country Is Lusaka The Capital ,Zambia  +Music,"What was the real name of Kool,of Kool and the Gang fame?",Robert Bell,Art & Literature,Name The Tibetan Mountain Retreat Featured In (The Lost Horizon) ,Shangri-La ,General,"What former riverboat pilot & author called the Mississippi ""the crookedest river in the world""",Mark twain +General,What song was The Pittsburgh Pirates anthem,We are Family – Sister Sledge, History & Holidays,Who was the British Prime Minister at the outbreak of the Second World War? ,Neville Chamberlain ,General,Have you ever danced with the devil in the pale moonlight?,Batman +General,"What links Fitzroy, Essenden, Collingswood and Carlton",Aussie rules football teams,Music,On Which Label Did Elvis Launch His Career,Sun,General,A thin Indin cake of unleavened bread,Chapatti +General,Between what ages is a brandy or port described as VSO,12 to 17 years,General,"Queequeg, Daggoo, Tashteego had what job on the Peaquod",Harpooners,General,What do people in cold climates add to the water in a car's radiator in winter,Anti freeze +General,Who did mozart marry,Constance weyburn,Music,"With Which Spice Girl Did Missy ""Misdemeanor"" Elliot Sing On ""I Want You Back""",Mel B / Scary Spice,General,"Who wrote ""everyone lives by selling something""",Robert louis stevenson +Tech & Video Games,"What is a ""koopa?"" ",A turtle,General,Name the chauffeur of the car in which the Princess of Wales died,Henri paul,General,"What, in the second line of Longfellow's poem, stands ""Under a spreading chestnut tree""",The village smithy +General,In Italy what is Provolone,Smoked hard cheese, Geography,What is the capital of Ghana?,Accra, History & Holidays,In which 1947 Christmas film do a lovestruck couple pretend that the snowman is Parson Brow ,Winter Wonderland  +General,Sir Wilfred is the real name of which eponymous character,Ivanhoe,General,How did Pope Hadrian IV die,Choked on a fly,General,"Who has been known as the ""mother of country music""",Maybelle carter +General,The roadrunner belongs to what family of birds,Cuckoo,Food & Drink,Tequila is made from an extract of which species of cactus?,Agave,General,The Parthenon is named after Athena Parthenon what's it mean,Virgin or Maiden +Food & Drink,What Was Sweet Stout Known As Until Banned Under The Trade Description Act ,Milk Stout , Geography,Ouagadougou is the capital of ______?,Burkina Faso,General,Which Record Was The Last UK No.1 Of The 1970's And Obviously The First No.1 Of The 80's,Another Brick In The Wall +General,In Schulter Okalahoma illegal for towel wearing women do what,Gamble,General,The study of plants is ______.,Botany,General,"What comes in varieties called Duncan, Burgundy and Marsh",Grapefruit +General,Aquatic mollusc with hinged double shell,Bivalve,General,What is the name of the princess in Sleeping Beauty,Aurora,General,The Ionian islands are nearest what country,Greece +General,What would the ancient Greeks do with an Apodesm,Wear it type of bra,General,"In 'star trek', who was the captain of the 'enterprise c'",Rachel garret,Food & Drink,What Type Of Wheat Is Pasta Traditionally Made From ,Durum Wheat  +General,With what would you rock the baby or walk the dog,A Yoyo,General,"In Greek mythology, who did diemos personify",Dread,General,What does the girls name Amy mean,Fit to be loved – from French +General,"Who wrote the poem ""The Pied Piper of Hamlin""",Robert Browning,General,What did sheryl crow do before she became a singer,Teach,General,Where was the first skyscraper in the world built,Chicago +Sports & Leisure,On what type of surface are the tennis matches at Wimbledon played,Grass,General,Ancient Egyptians shaved their eyebrows to mourn the deaths of what,Their cats,General,"He said 'i have nothing to offer but blood, tears, toil and sweat'?",Winston churchill +General,What are the membranes enveloping the brain and spinal cord called,Meninges,General,What does 'dvd' mean,Digital video disc,Science & Nature, The male __________ sheds its antlers every winter and grows a new set the following year.,Moose +Food & Drink,From which fruit is the liqueur Kirsh made ,Cherry ,General,Which two colours appear on the Bangladesh flag,Green & red,General,"In the confederate army, who were given copies of 'les miserables'",Officers +Music,"Who Supported John Lennon On ""Whatever Gets You Through The Night""",Elton John,Food & Drink,When It Comes To Food & Drink What Does The Scorville Scale Measure? ,The Hotness Of Chillies , Geography,In which continent would you find the Yellow river ?,Asia +General,What country consumes the most coal each year,China,General,What is the study of bumps on the head called,Phrenology,Sports & Leisure,Which football player has played first team football for Liverpool 658 times and for Wales 67 times? ,Ian Rush  +Music,"Henry Rollins Was A member Of Which Punk Band ""Black Flag"" Or ""The Sex Pistols""",Black Flag,General,Which group were originally known as the Russellites,Jehovah's Witnesses,General,Chemical got from coal tar and used as a solvent,Benzene +Entertainment,What is the name of Yogi Bear's best freind,Boo Boo,General,The USA has the most railtrack - what country second,Canada,General,What is the oldest known infectious disease,Leprosy + History & Holidays,What Was The Function Of Press Gangs In The Early Nineteenth Century? ,To Press Or Recruit Men Into The Royal Navy , History & Holidays,"Three of the names of Santa's reindeer begin with the letter 'D'', name two of them ","Dancer, Dasher, Donner ",General,On what is an espadrille worn,Foot +General,Herb Caen is credited with inventing what word,Beatnik,General,What is the closest living relative to the T Rex,The Chicken,Science & Nature,First marketed in 1958 by the Kodak company as 'Eastman 910' by what name is their cyanoacrylate adhesive better known? ,Superglue  +General,He led the mormons to the great salt lake,Brigham young, History & Holidays,"""Who played the elf called Patch in the film 'Santa Claus: The Movie'? """"Danny De Vito"""" - """"Michael J Fox"""" - """"Bob Hoskins"""" - """"Dudley Moore"""" "" ",Dudley Moore ,General,A C-Curity was the original name of what common object,Zip Fastener +General,Who wrote the science fiction books Consider Her Ways and Chocky,John wyndham,General,What was the city of Istanbul called before 330 AD,Byzantium,General,"Who said ""He who opens a school door, closes a prison""?",Victor Hugo +General,What tennis ball company's logo is 'you've seen one you've seen them all',Penn,General,Which character has been played by the most actors,Sherlock Holmes,General,And what does she charge,Five cents +General,What is the active ingredient in Chinese birds nest soup,Bird Spit,General,Which firm manufactures 'Shreddies',Nestle,General,Who was dipped into the river styx,Achilles +General,"Who said ""If you want something said, ask a man. If you want something done, ask a woman."" ?",Margaret Thatcher,General,What has been called The most unnatural of all perversions,Celibacy,Technology & Video Games,On aircraft what does VTOL stand for?,Vertical Take off and landing +General,"Which author, famous for such novels as 'The Naked Lunch', died in 1997",William s burroughs,General,What are loose rocks on a mountainside called,Scree,General,With which musical instrument is Dizzy Gillespie chiefly associated,Trumpet +General,Ancient Roman brides wore a wedding dress - what colour,Yellow,Music,Which Chart Position Did Mirror Man Achieve For The Human League,No.2,Music,"Who Had A Hit With ""Holding Back The Years""",Simply Red +General,"Long jump, High Jump, Triple Jump what missing",Pole Vault Olympic jumping events,General,What is the Capital of: Rwanda,Kigali,General,Who was the main plotter in the Gunpowder Plot 1605,Robert Catesby +Science & Nature,What Do Silk Worms Feed On? ,Mulberry Leaves ,General,Which capital city is built on the site of ancient Tenochtitlan,Mexico city,General,An illegal 1920s saloon,Speakeasy +General,The Spear Leek was the original name of what food item,Garlic,Sports & Leisure,How many Olympic Gold medals did Carl Lewis win? ,9 ,General,What airline started 24th September 1946 single DC3 - Betsy,Cathay Pacific +General,With which hand do soldiers salute,Right hand,General,What is the English name for the constellation Mensa,Table,Art & Literature,Who's last words were 'Thus with a kiss I die'?,Romeo +General,Who was poisoned - shot - and drowned river Neva 1916,Rasputin,General,Holiday resort of Marmaris is in what country,Turkey,General,The city of Tours stands on which river,Loire + History & Holidays,What Is The Name Of The Bad Guy In The Friday The 13th Series ,Jason ,General,From What Country Does The Singer Sandi Thom Come From,Scotland,General,Where in France do claret wines come from,Bordeaux + History & Holidays,"""What Did My True Love Give To Me On The """"Third"""" Day Of Christmas"" ",3 French Hens ,General,"Who supposedly said ""Father I cannot tell a lie""",George washington,General,What TV family lived at 1124 Morning Glory Circle Westport Con,Stevens in Bewitched +General,In gardening what would you use a 'trug' for,Carrying things,General,What is the official language of Cambodia,Khmer,Entertainment,Charles Laughton played Quasimodo in this epic film.,Hunchback of Notre Dame +General,Officers in which army were given copies of 'les miserables',Confederate,General,Who founded the Greek theatre,Thespis,General,What is the fear of sinning known as,Peccatophobia +Food & Drink, Vermicelli literally means ___________.,Little worms,Science & Nature,What Causes A Jumping Bean To Jump? ,A Moth Grub Moving Inside The Bean ,Music,Which Single Gave Alessi Their One And Only Claim To Fame Reaching No 8 In June 1977,Oh Lori +General,"Germany's allies in wwii were japan, italy, hungary, bulgaria, finland, libya and ______",Rumania,General,What is the principal mountain chain in Romania,The carpatians,General,Who Wrote The Play 'Shirley Valentine' ,Willy Russell  +Entertainment,Who sang 'Rescue Me'?,Fontella Bass, History & Holidays,The Romans built these to convey water.,Aqueduct,People & Places,What Was The Nickname Of The Assassin Carlos Martinez ,The Jackal  +Art & Literature,How Old Was Adrian Mole When He Began Writing His Secret Diary ,13 & Three Quarters ,General,What is the Capital of: Mexico,Mexico,General,Demeter was the Greek god of what (Ceres Roman),Harvest +General,Name the original comic strip Bill The Cat appeared in.,Bloom County,Art & Literature,"Whose Life Was The Subject Of James Boswell's Biography, Published In 1791 ",Samuel Johnson ,General,Which group publishes the most monthly magazines,Hearst +General,"On The 19 th November 1994, Paul Merlon Became The First Person Ever To Do What",Win The Uk National Lottery,General,Who made wings for himself and his son to escape from the island of crete,Daedalus,General,This was (until the Alaska purchase) the largest real estate deal in U.S. history,The Louisiana Purchase + History & Holidays,"For how many years did the 30 Years War last? 27, 30 or 36? ",30 Years ,Music,Bill Idol Sang About Eyes Without A What,A Face,General,Business and Advertising: This brand boasts 57 varieties?,Heinz +General,What is a group of otters,Romp,General,The Swathling Cup is played for in what sport,Table Tennis,Music,"David Crosby, of Crosby, Stills and Nash, was previously with which other popular 60's group?",The Byrds +General,"What part of your body is elastic, waterproof, washable & fits you very well",Skin,Science & Nature,"What is the name used to describe the ""minor planets""",Asteroid,General,What athlete released the photo book rare air in 1993?,Michael jordan +Food & Drink,Legend says bats lived in the rum distillery and one of them is on the label.,Bacardi,General,What is the name of the official residence of the president of France,The elysee palace,General,Who did pocahontas entertain in the nude,Colonists +General,What was Erich Weiss better known as,Harry Houdini,Geography,In Which Town In London Will You Find The William Morris Gallery ,Walthamstow ,General,Who created the musical 'Paint Your Wagon'?,Loener and Lowe +General,"Madness released a hit single in 1979 titled ""the _____""",Prince,General,What is an informal term for the clothing industry,Rag trade,Food & Drink,What Is The Main Ingredient In Meringue ,Egg Whites  +General,What is the most frequently used word in the english language,The,General,In Los Angeles its illegal to do what on the witness stand,Cry,Art & Literature,"A printing process in which ink impressions are taken from a flat stone or metal plate prepared with a greasy substance, such as an oily crayon. ",Litography +General,The Travelmate was designed to allow women to do what,Pee Standing Up,General,Who was the first president of the Royal Academy,Sir joshua reynolds,Art & Literature,Who wrote the Discworld series ?,Terry Pratchett +General,What was Britain called - before it was Britain,Albion,General,Which Argentinian in 1967 became the oldest-ever winner of the Open Golf Championship at the age of 44,Roberto de vicenzo,General,"Which cocktail is made from creme de cacao, cream and brandy",Brandy +General,Vanilla is the extract of fermented & dried pods of several species of what,Orchids,General,A bind is a group of what type of fish,Salmon, Language,What does 'majuba' mean?,Place of rock pigeons +General,"Which newspaper owner's career inspired the film ""Citizen Kane""",William randolph hearst,Geography,In what country is the Waterloo battlefield,Belgium,General,Which country was invaded by Soviet troops in August 1968,Czechoslovakia +General,"In Greek mythology, who was the first woman on earth",Pandora,General,Which woman has the most monuments erected in her honour,Virgin mary,Sports & Leisure,Which sport uses stones and brooms?,Curling +General,What was unusual about the Gossamer Albatross aeroplane,Man (pedal) powered,General,What is a group of this animal called: Crane,Sedge siege,General,What state is mount mckinley in,Alaska +General,What is added to a Welsh Rarebit to make a Buck Rarebit,Poached egg,General,Which bomb used in the Dambuster raids was invented by Barnes Wallis,Bouncing bomb,General,"Who wrote ""The Glass Menagerie""",Tennessee williams +General,"What is the robots name in the movies ""Short Circuit 1 and 2?""",Johnny,General,Who is the 'invisible' star of the film ' Hollow Man',Kevin bacon,General,How Is The Fictional Character The “ Duchess of St Bridget ” Otherwise Known ?,Lara Croft +General,What city has the world's largest black population,New york,Science & Nature,Nitrous oxide is better known as __________.,Laughing gas,General,With which 19th century plot was Arthur Thistlewood associated,Cato street conspiracy +General,"In ballet, a jump off one foot that is 'broken' by a beating of the legs in the air.",Brisé,People & Places,Whose final film as director was Eyes Wide Shut? ,Stanley Kubrick ,Music,"Which Ex Model Featured On The Full Force Single ""Naughty Girls""",Samantha Fox +People & Places,What Is Andrew Lloyd Webbers Brothers Name ? ,Julian ,General,"Which Language Do The Words ""Kiosk, Tulip & Cavia"" Come From",Turkish,General,"Where, apart from the wild, would you find bulls bears and stags",Stock Exchanges + History & Holidays,Pop Star Marty Wilde Had A Baby Girl In 1960 What Did He And His Wife Call Her ,Kim (Kim Wilde) ,General,What is the fear of sexual perversion known as,Paraphobia,General,"Igneous, sedimentary and metamorphic are all types of what",Rock +General,What is a ziggurat,Mesopotainian Temple tower,Science & Nature,What Was Sellafields Former Name ,Windscale ,General,What is the world's oldest university ?,Fez University +Science & Nature," The owl parrot can't fly, and builds its nest under tree __________",Roots,General,The Greek for circle of animals gives it name to what,Zodiac,Science & Nature,Which Bird Is An Emblem Of The French Nation ,The Rooster  +Music,Which Beatles album was not produced by George Martin?,Let it Be,General,In Denmark what is a Svangerskabsforebyggendemiddel,A Condom,Sports & Leisure,In 1986 Which Boxer Became The Youngest Ever World Heavyweight Champion ,Mike Tyson  +Music,The First Family Group To Reach Number One With Their Debut Single Happened In 1997. Who Was That Act?,Hanson,Science & Nature," The __________ is the only bird that can swim, but not fly. It is also the only bird that walks upright.",Penguin,General,The willow ptarmigan is the state bird of ______,Alaska +Entertainment,What was Betty Grable's nickname?,The Legs,General,"National park what telephone company calls itself ""the right choice""",Atandt,Sports & Leisure,What Type Of Rock Is Used To Make Curling Stones ,Granite  +General,What is Interpol short for ?,International Criminal Police Commission,General,"Which American female vocalist had a hit in 1980 with ""Call Me""",Debbie harrie,General,Name Steve McQueen's Karate teacher - later an actor,Chuck Norris +General,Who would use a Snellen chart in his/her work,Optician,General,Where (Specifically) On The Human Body Will You Find The Columella,Skin The Seperates The Nostrils,General,Which was the last rascals hit in 1968,People got to be free + Geography,How many Great Lakes are there?,Five,Sports & Leisure,Basketball: The New York __________.,Knicks,General,On the TV show Frazier what was the dads dogs name,Eddie +General,What word for a cigar type is also Italian for a small loaf,Panatela, Geography,Which state is the Garden State?,New Jersey,Entertainment,What character did Tex Avery first create upon arriving at MGM,Screwball squirrel +Science & Nature,What Does A.M Stand For On Radios ,Amplitude Modulation ,General,What does url stand for,Uniform resource locator,Music,Name The Best Selling Single Of 1960,Elvis Presley / Its Now Or Never +Geography,What Forms The Natural Border Between Europe & Asia ,The Ural Mountains ,General,Brontophobia is a fear of ______,Thunder or thunderstorms,General,What record does the Khaki Campbell breed of duck hold,Egg laying +General,Iain Stewart - dropped - looked too normal - what pop group,The Rolling Stones,General,Lack of what is the cause of the deficiency disease 'kwashiorkor',Protein,Science & Nature,What Were Brunel's First Names ,Isambard Kingdom  +Music,Whats The Nickname Of Emma Bunton,Baby Spice,General,How big is the city of london,One square mile,General,What is a casaba,A Melon +General,Which aircraft was the first jet-powered bomber of the RAF?,English Electric Canberra,General,"Treasure Island, Angel Island and Alcatraz can all be seen from which bridge",Golden gate,General,What football team has won the most Rose Bowls,Southern california +Music,"Who helped them to get married in ""The Ballad of John and Yoko""?",Peter Brown,General,Who won the Booker Prize for the novel 'The Bone People',Keri hulme,Science & Nature," The woolly __________, extinct since the Ice Age, had tusks almost 16 feet high.",Mammoth +Music,"Who Had A Novelty Hit With ""The Laughing Gnome"" In 1973",David Bowie, Geography,What continent is part of both the East and Aest hemispheres?,Antarctica, History & Holidays,Which sitcom helped launch Michael J. Fox's career by portraying him as a money-grubbing teenager? ,Family Ties  +General,Near what falls did jimmy angel crash his plane in 1937,Angel falls,Music,What Kind Of Shack Did The B52's Sing About,A Love Shack,General,What colours was the ferrari formula 1 car in the 1964 u.s.a grand prix,Blue +Art & Literature,What other name does Stephen King write under?,Richard Bachman,General,Of what is 'FM' an abbreviation in FM Radio?,Frequency modulation,General,What English brand of sherry is considered the King desert wine,Harvey's Bristol Cream +General,The Blur Max medal was named after Max who,Max Immelmann,General,What reusable drawing pad notched its 50 millionth sale in 1985,Etch-a-sketch,Food & Drink,Which Country Drinks The Most Coca Cola ,Iceland  +General,Where did Mr Badger live in _The Wind in the Willows,The wild wood,General,Who was the first man to set foot on all five continents,Captain Cook,Sports & Leisure,Which Football Club's Badge Depicts A Golden Lion On A Claret And Blue Background? ,Aston Villa  +General,What 70s pop group was originally called The Engaged Couples,Abba,General,"Which fantasy writer's latest book is called ""The Fifth Elephant""",Terry pratchett,Science & Nature," A male __________ becomes fully feathered when he is three years old, but can mate earlier.",Peacock +General,When is 'trick or treat',Halloween, Geography,What famous geyser erupts regularly at the Yellowstone National Park?,Old Faithful, Geography,What is the largest city in Ecuador?,Guayaquil +Sports & Leisure,What famous race was established in 1903?,The Tour de France,General,"A leap from one leg to the other in which one leg is thrown to the side, front or back. ",Jeté,General,What does the rankine scale measure,Temperature +General,"What Famous Landmark Was Discovered By ""Garcia Lopez De Cardenas"" In 1540",The Grand Canyon,General,International car registration letters what country is RA,Argentina,General,Who was the human companion of willow,Mad mardigan +General,How many lines are there in a Clerihew?,Four,General,"In Beverly Hills Cop, how does Axel Foley escape the police car that is sent to follow him?",A banana in the tailpipe,Science & Nature,The vernal equinox is the beginning of ________.,Spring + Geography,What is the basic unit of currency for El Salvador ?,Colon,General,Siddhartha Gautama became better known as who,Buddha,General,Dr. Suess wrote _____________ after his editor dared him to write a book using fewer than fifty different words,Green eggs & ham +Music,Who Wrote The Flight Of The Bumblebee Which Later Turned Up As A Pop Hit Called Nutrocker,Nicolai Rimsky Korsakov,Science & Nature, A full_grown __________ may be 8 feet high at the shoulder and weigh almost a ton.,Moose,General,In the book Little Women what is the sisters surname,March +General,If you were crapulous what would you be,Drunk,People & Places,What Was The Name Of The Australian Outlaw Who Wore Metal Body Armour ,Ned Kelly ,General,Which wine has varieties called malmsey and sercial,Maderia +General,Teaching what subject banned Oxbridge Unis King George VI,Astrology,General,Who Was Sentenced To Life Imprisoment On The 14th June 1964,Nelson Mandella,General,C10 H16 Is The Actual Chemical Symbol For Which Everyday Item,Turpentine +General,What percentage of a peanut is fat,47,Food & Drink,Valpoliccella Is A Red Wine That Originated From Which Region Of Italy ,Veneto , History & Holidays,What TV show with married couples and family life appealed to those over the age of 29? ,Thirty something  +General,What is the metal part of a lamp surrounding the bulb and supporting the shade,Harp,General,The 'green mountain state' is___.,Vermont,General,Harry Weinstein Became A World Champion In 1985 But With What More Familiar Name Is He Known,Gary Kasparov +General,What is a group of this animal called: Partridge,Covey,Music,"In ""I Am The Walrus"", who were they kicking?",Edgar Allen Poe,General,"According to superstition, what are yoU.S.upposed to do when yoU.S.ee a lone magpie",Salute it +General,"Other than germany, whose official language is german",Austria, Geography,Where is the city of Brotherly Love?,Philadelphia,Geography,What is the capital of Vatican City,Vatican city +General,What is the stinky gas called hydrogen sulphide said to smell like,Rotten eggs,General,Harold H Lipman received a patent in 1858 for what invention,Gluing a rubber on a pencil, History & Holidays,George Washington Carver advocated planting what to replace cotton and tobacco?,Peanuts and sweet potatoes +Music,"Name The ""Fade To Grey"" Ban Fronted By Steve Strange",Visage,General,In Cockney rhyming slang what is a butchers (butchers hook),Look,General,What nationality was Oddjob,Korean +General,What is the imaginary line on the surface of the earth approximately parallel to the geographical equator,Aclinic Line,Sports & Leisure,What Sport Features In The Stella Artois Tournament? ,Tennis / Queens ,Sports & Leisure,The Latin Phrase 'Citius Altius Fortius'' Is The Motto For Which Sporting Event? ,The Olympic Games  + History & Holidays,Which pain relieving product was subjected to a public relations scare in the eighties? ,Tylenol ,General,What is the Japanese currency?,Yen,General,Thermophobia is a fear of ______,Heat +Food & Drink,How Are Tandoori Dishes Cooked ,In A Clay Oven (Tandoor) ,General,Jan Lodvik Hock changed his name to what,Robert Maxwell,General,Sqaure cap worn by RC priests,Biretta +Science & Nature,Where Would You Find The Trapezius Muscle ,Neck & Shoulder Area ,Music,"Which Famous Female Singer Did Don Joohnson Of ""Miami Vice"" Later Release A Duet With",Barbara Streisand,General,Who wrote the children's novel The Secret Garden,Francis hodgson burnett +General,What is the process which gives a high lustre to cotton called,Mercerising,General,To what Patron Saint would you pray if you had a headache,Saint Dennis,General,What is 'Irish Moss',Seaweed +General,Who sang the theme song for Rawhide,Frankie Lane,General,In music if F major is the key what is the relative minor,D minor, Geography,With which country is Prince Rainier III identified?,Monaco +General,"Who was defendant in the so called ""monkey trial""",John t scopes,General,In which country are you most likely to die from a scorpion sting,Mexico (1000 a year),General,What are the playing pieces of dominoes,Bones +Music,Who Sang Here Comes The Night Featuring Van Morrison In 1965,Them,General,Which million dollar building cost more than a million dollars,Sydney opera,Sports & Leisure,Since Steven Mclaren Took Over The Reigns As England Manager As Of The End Of 2006 Who Was The Teams Highest Goal Scorer ,Peter Crouch  +General,"Which poem begins ""If I should die, think only this of me""",The soldier,General,"ACII, Stockless, Mushroom and Plough types of what",Anchors,General,Who was john wayne's musical co-star in true grit,Glen campbell +Food & Drink,Which flavouring is added to brandy and egg yolks to make advocaat? ,Vanilla ,General,Who was the first American in sub-orbital space flight,Alan shepard, Geography,What is the highest mountain in Canada?,Mt. Logan +General,What is the fear of naked bodies,Gymnophobia,General,"When ocean tides are at their highest, they are called what",Spring tides,General,Whose last unfinished novel was The Last Tycoon,F Scott Fitzgerald +General,Strabismus is the medical term for which complaint,Crossed eyes,General,Jello Biafra is the principal singer-songwriter for what irreverent San Francisco punk band,The Dead Kennedys,Music,Who Played The Lead Role In The Screen Adaptation Of Dennis Potter's Pennies From Heaven,Steve Martin +General,Who was the wise cat in TS Eliot's book of cats,Old Deuteronomy,Entertainment,What was Elvis Presley's wife's name?,Priscilla,Geography,In which city is red square ,Moscow  +General,Lizard able to change colour for camouflage,Chameleon,General,What General Motors plastic bodied car was built in Tennessee,Saturn, Geography,What is the basic unit of currency for Laos ?,Kip +General,What is the colour of the maple leaf on the Canadian flag,Red,General,In Brockton Mass you must have a licence to enter where,Towns Sewers,Geography,Name the capital city of Rhode Island.,Providence +General,Seth Wheeler patented it in 1871 - what,Wrapping Paper,General,In Red Dwarf what did the H stand for on Rimmers head,Hologram,General,Which is the only Shakespeare play not to contain a song,The Comedy of Errors + Geography,Vaduz is the capital of ______?,Liechtenstein,General,What group had a 70s hit with Ride a White Swan,T Rex,General,Who succeeded Bonar Law as British Prime Minister in 1923,Stanley baldwin +General,What is the name of the scale measuring depth of coma (GCS),Glasgow Coma Scale,General,Wreath of flowers used as a decoration,Garland, History & Holidays,What Would You Traditionally 'Bob' For At A Halloween Party ,Apples  +Entertainment,"Name the band - songs include ""Psycho Killer, Road To Nowhere""?",Talkingheads,General,Selva is another name for what,Tropical rain forest,General,"Who was the first person to complete 'The Adventurer's Grand Slam', of climbing the highest peak on each continent",David hempleman-adams +General,Which department of the us government did eliot ness work for,Treasury,Sports & Leisure,At which sport did Magic Johnson excel? ,Basketball ,General,What was garth's last name in 'wayne's world',Algar +Entertainment,Name Alley Oop's girl friend.,Oola,General,What is a group of this animal called: Swallow,Flight,General,Where is bill gates' company based,"Redmond, washington" +General,What do the initials in j.r.r tolkien's name mean,John ronald reuel,General,Negatives what is the term for the union of two dissimilar sexual cells or gametes to form a new individual,Fertilisation,General,In what town was Leonardo Da Vinci born,Vinci +General,In The World Of Movies How Is 'Jack Napier' More Commonly Known,The Joker (Batman),General,Which is Edvard Munch's most famous painting,The scream,General,"In the board game Cluedo, which room is situated directly between the kitchen and the conservatory",The ballroom +Art & Literature,"Stephen King's: ""Salem's _________"".",Lot,General,"Which sport is featured in the film ""Raging Bull""",Boxing,General,Kool-aid was a deadly cocktail for the inhabitants of a temple in which Guyanese town,Jonestown +General,What are the names of the two famous disney chipmunks,Chip and dale, History & Holidays,Which president was responsible for the Louisiana Purchase?,Jefferson,General,Two out of every five American women do what,Dye their Hair +General,"Whose last novel was 'Portrait of an Artist, as an Old Man'?",Joseph Heller,General,What is the name of the tissue layer which covers a growing antler,Velvet,Science & Nature,What word describes part of your hand and a type of tree?,Palm +General,What is the young of this animal called: Birds,Fledgling nestling, History & Holidays,Which of the Wise Men was said to have brought the gift of gold for the baby Jesus? ,Melchior ,Science & Nature,What name is given to the single super-continent that existed 200 million years ago ?,Pangaea +General,Which former 'Neighbours' star had a hit with 'Any Dream Will Do',Jason donovan,Music,Tommy Lee Of Motley Crue Married Who In The 90's,Pamela Anderson,General,Who composed Symphonie Fantistique,Berlioz +Science & Nature,Which Glands Are Tears Produced By ,Lachrymal Glands ,Sports & Leisure,Which major sporting venue is located in Richmond Upon Thames? ,Twickenham ,General,"Name of a clothing line, or sport whose periods are called 'chuckers'.",Polo +Music,"Who Had A Hit With ""Someday I'm Coming back in 1992",Lisa Stansfield,General,What is the longest recorded flight of a chicken,13 seconds,General,Olof Sonderblom conceived and patented what technology,Token Ring Networking +Food & Drink, At which stage of a meal would you have an hors d'oeuvre?,Beginning,General,Where did bill and hilary clinton switch on christmas lights in 1995,"Belfast, ireland",Music,How Many Revolutions Per Minute Does An LP Play At?,33 & A Third +General,Alister Allan and Malcolm Cooper won Olympic medals in which sport,Shooting,General,Lewis Ernest Watts became famous under what name,John Mills, History & Holidays,Who Was Quoted As Saying 'Ask Not What Your Country Can Do For You But What You Can Do For Your Country'' ,John F Kennedy  +General,What is the third day of the week,Tuesday,General,Ornithophobia is the fear of,Birds,General,What queen banned mirrors as she got older,Elizabeth 1st +General,Where on your body would you find your Rasceta,Creases on inside of wrist,General,What type of creature was Pylorus Jack,A Dolphin – saved sailors,Music,"In Which Year Was Cilla Looking For ""Anyone Who Had A Heart"" 1961, 1964, 1967",1964 +General,Which musical term denotes that the pitch of a note is lowered by a semitone,Flat,General,In what country is the worlds largest pyramid,Mexico - Quetzalcoatl,General,Thalia is one of the muses - what's her subject,Comedy +Entertainment,French impressionist Claude _______,Debussy,General,What are the roads of Guam paved with?,Coral,General,Where do Eskimos wear mukluks,Feet +General,A can of Pepsi holds __ fluid ounces.,12,General,Which strait separates Russian and Alaska,Bering strait,General,Where was Bacardi originally made,Cuba +General,What European capitol stands on the river Aare,Berne Switzerland,General,Complete advertising phrase from 1935 My Goodness,My Guinness,General,Which Sport Was First Introduced Into The olympics In 1964 At The Request Of The Host Nation,Judo +General,Who shot and killed himself while painting 'wheatfield with crows',Vincent,General,What made up the Bouquet in the 70's TV series starring Susan Penhaligon,Barbed wire,General,"""City Lights"" was the name of a film by whom",Charlie Chaplin Chaplin +Music,Which American Soprano Became The First Singer To Give A Solo Proms Recital In August 2000,Jessye Norman,General,"""And the big wheel keep on turning neon burning up above and I'm just high on the world come on and take the low ride with me girl on the_____"" What's the Dire Straits song title",Tunnel of love,General,What is a negus - named after inventor,Port Lemon hot sweet spiced +Science & Nature,The planet closest to the sun is _______.,Mercury,General,Norman Maine is a character in what remade twice film,A Star is Born,General,This company uses the slogan AOL,America on line america online + History & Holidays,What 'HHH' Does Santa Do In His 3 Gardens ,"Ho, Ho, Ho ",Music,Which Father Daughter Duo Had A Hit With “Something Stupid” In 1967?,Frank & Nancy Sinatra,General,"In nautical terms, what name is given to the upper edge of a ship's side",Gunwhale +Religion & Mythology,On which day was the resurrection of Christ?,Easter Sunday,Music,What Was The Last Single Released By John Lennon In His Lifetime,(Just Like) Staring Over,General,"What instrument did jazz musician, Woody Herman, play",Clarinet +Music,Alan MceGee Signed Oasis To Which Label In 1993,Creation,General,"What was the first u.s state to seccede from the union on december 20, 1860",South carolina,General,What is the flower of September,Aster or Morning Glory +General,"Who recorded ""maybellene"" in 1955",Chuck berry,Art & Literature,Which Movement Spanned The Period From The 17th Century To The Early 18th ,Baroque ,General,What was the name of the bar/restaurant on THREE'S COMPANY?,Regal Beagle + History & Holidays,This Chinese dynasty lasted from 1368 to 1644.,Ming,Food & Drink,Who Was The Weather Man Who Famously Denied Reports Of A Possible Hurricane In 1987 ,Michael Fish ,General,How many children did adam and eve have,Three +General,What cocktail is made from vodka and kahlua,Black russian,General,How long passed from the making of minute rice & its marketing,18 years,General,In what book does 'Schahriah' appear,Thousand & one nights +Music,In What Country Was David Byrne Born,Scotland, Geography,What is the capital of Sweden ?,Stockholm,General,California illegal to shoot game from moving vehicle except what,Whale +Sports & Leisure,"Outcrop, Big Wall And Crag Are All Forms Of What ",Rock Climbing ,General,What is the smallest book in the Library of Congress,Old King Cole – thumbnail size,Sports & Leisure,What is the name of a golf stroke that is 2 under Parr for the hole? ,Eagle  +General,Who was the greek goddess of the hunt,Artemis,Sports & Leisure,In Which Sport Might You Start From Pole Position ,Motor Racing ,General,What is the name of the detective in john dickson carr novels,Gideon fell +General,What is a group of peacocks,Muster,General,Which country produces Dao wine,Portugal,General,How long is the memory span of a goldfish,3 seconds +General,Where are the Appalachians,North america,General,What does ALF stand for?,Alien Life Form,General,Ho was singer of the QUEEN?,Freddie mercury +General,"American motion-picture actor, writer, director, and producer, a performer of great versatility and range, known for his enigmatic, faintly menacing grin and his skill in portraying nonconformist loners",Jack Nicholson,General,What was the nationality of sir winston churchill's mother,American, History & Holidays,"""What Did My True Love Give To Me On The """"Fifth"""" Day Of Christmas"" ",5 Gold Rings  +General,If an Australian called you a cadbury what would he mean,Cheap Drunk,General,What is the state bird of alaska,Willow ptarmigan,Science & Nature," Because its tongue is too short for its beak, the __________ must juggle its food before swallowing it.",Toucan +Food & Drink,"The 18th amendment to the Constitution was introduced in 1919, repealed in 1933 and is the only one to have been repealed, what did it relate to? ",Prohibition of alcohol , Geography,What is the capital of Azerbaijan ?,Baku,General,Timbucktoo is in which country,Mali +General,What hats were worn by British troops during the Napoleonic wars of the early 1800s?,Shakos,General,Who invented 'bifocal' lenses for eyeglasses,Benjamin franklin,General,What bred of dog is snoopy,Beagle +Geography,Who Was Head Of State Of Spain Before King Juan Carlos? ,General Franco ,General,Tsar Paul I decreed death by flogging to anyone mentioned what,His Baldness,General,Queen Victoria said it the saddest place in all Christendom where,Mearsyside + History & Holidays,Great Britain Was The First Country To Issue Postage Stamps But In What Year ,1840 ,General,What is brimstone,Sulphur,Art & Literature,Which Author Wrote The Spy That Came In From The Cold ,John Le Carre  +General,Who was the dragon in the film 'dragonheart',Draco,General,"If a pope has not been elected, what color smoke is seen",Black,General,Fax is short for what,Facsimile +Music,With Which Singer Does Bernie Taupin Co Write,Elton John,General,Philip Pirrip is the main character in which Charles Dickens novel,Great expectations,General,Breed of rock pigeon that is specially trained to return swiftly to its home,Homing pigeon +Science & Nature,In Computing Terms What Does The Abbreviation I.R.C Mean? ,Internet Relay Chat ,General,What is the first month of the year,January,Geography,What is the capital of Tunisia,Tunis +General,What is the fear of many things known as,Polyphobia,General,Who wrote the official biography of Lester Piggott,Dick Francis,General,Which pop song contains a similar melody to Tchaikovsky's Symphony in E minor,Annie's song +General,What country spends the most per capita in casinos,Australia,General,What is the name of the capital of Ontario (Canada),Toronto,Music,Who had her first UK top 10 hit with What Have You Done For Me Lately? in 1986 at the age of nineteen?,Janet Jackson +General,What nationality was the first man to die in a plane crash,French – Orville Wrights passenger,General,Cindy Crawford Elle Macpherson and Madonna all done what,Appeared in Playboy,General,Bartommelo Christofori invented what,Piano +General,"What Well Known Organisation Was Founded In 1844 By ""William Booth""",The Salvation Army, History & Holidays,What was the first newspaper produced in the United States ?,Publick Occurences,General,According 1890s doctors women eat mustard vinegar do what,Masturbate too much +Science & Nature,Name Three Of The Four Types Of Adult Teeth ,"Incisors, Canines, Premolars & Molars ",General,In Brookings South Dakota its illegal for a cat to live where,In a dogs house – if dogs there,General,What is the olympic motto,"Citius, altius, fortius" +General,Who wrote 'How to Win Friends and Influence People',Dale carnegie,General,What weapon was used by the Germans against Russia in 1915,Tear gas,Food & Drink,By What is the seasoned jellied loaf made from the head of a pig generally known? ,Brawn  +General,"Which group sang the song ""Go Let It Out""?",Oasis,General,Eighty one what is the minimum number of degrees in an acute angle,One degree,General,What does Zip stand for in the American Zip Code,Zone Improvement Plan +General,Cows clean their noses with their ______,Tongue,General,"Who wrote the novel Enigma in 1995, about the wartime German coding machines",Robert harris,General,What are born with fur and their eyes open,Jackrabbits +General,What is the unit of currency of Venezuela,The bolivar,Science & Nature,What Is The Japanese Art Of Growing Dwarf Trees Called? ,Bonsai ,General,In USA early last century what were Comet Star Sun Moon,Motor Car Manufacturers +General,In the language of flowers what does oak leaves mean,Bravery,General,What is the tallest dinosaur,Brachiosaurus,Science & Nature,Who developed the laws of electrolysis?,Michael Faraday +General,Which architect was responsible for many of Barcelona's famous buildings,Antonio gaudi,Music,"Which Album Was Dedicated To The Late Ian Stewart, The Pianist Who Played On So Many Stones Dates",Dirty Work,General,According to truck drivers which US state has the worst drivers,California +Sports & Leisure,Who was the only boxer to knock out Mohammed Ali?,Larry Holmes,General,"What is the real name of the 'Boston Strangler' who admitted to 13 murders, and was sentenced to life imprisonment in 1967",Albert de salvo,General,Who wrote The Joy of SexAlex,Comfort +General,Nicknamed The Tiger who lead France at the end of WW1,George Clemenceau,General,In what county did the Aryan race originate,India, History & Holidays,Which Architect Designed The Whitehouse ,James Hoban  +General,What country celebrates its National Day on 6th June?,Sweden,General,What does an Alexandra taste of,Chocolate,General,What are the siberian prison islands also known as,Gulag archipelego +General,Nick Nolte played the Poor Man who played the Rich Man,Peter Strauss, Geography,What is the capital of Dominica ?,Roseau,General,During which month is the shortest day in the Southern hemisphere?,June + History & Holidays,In What Year Did The Great Fire Of London Occur ,1666 ,General,What Is The Only Spin Off To Date From The TV Show Coronation Street,Pardon The Expression,Science & Nature,How many nipples does an echidna have?,None +General,Name the heaviest breed of domestic dog,St bernard,General,Who played dr kildare,Richard chamberlain,Sports & Leisure,"Which England Rugby Union international was fined 15,000 pounds for bringing the game into disrepute? ",Lawrence Dallaglio  +Food & Drink,What is the name given to the dish of fruit stewed or preserved in syrup? ,Compote ,Sports & Leisure,"On A Yacht, What Are Sheets ",Ropes ,General,"Which European Country Shares England's National Anthem ""God Save The Queen""",Lichtenstein +General,Who Is Sean Coombes Better Known As?,Puff Daddy / P Diddy,General,How many stars on the european union flag?,12,General,Who was king arthur's father,Uther pendragon +Food & Drink,The Vanilla Plant Is Native To Which Country ,Mexico ,General,What U.S. state includes the telephone area code 508,Massachusetts,General,What's the first sign of the zodiac,Aries +General,What pigment allows humans to see better at night,Rhodopsin,General,Who recorded 'hey jealousy',Gin blossoms,General,"In a computer, what is a CPU?",Central Processing Unit +Music,Who Had A Hit With Downtown,Petula Clark,Music,Which Family Heart Throb Told Us He Was The Author Of The Record In 1975,David Cassidy / I Write The Songs,Music,"Who Ran Up The Charts With ""Walking Back To Happiness"" In 1961",Helen Shapiro +General,What do the seven stripes on the American flag represent,The seven original states,General,What is a device to stem the flow of blood called?,Tourniquet,General,Crazy Horse and Sitting Bull were born in which US state,South Dakota +General,In which country do the Sinhala people live,Sri lanka,General,"Which singer got to number one over Christmas 1988 with ""Mistletoe and Wine""",Cliff richard,General,What is the name of the background screen on which windows is displayed,Desktop +Art & Literature,Which Famous Book Contains The Line 'Once Upon A Time There Was A Little Chimney Sweep And His Name Was Tom' ,The Water Babies ,Music,Which Song Provided The Glam Rock Band “The Sweet” With Their Only No1 Hit,Blockbuster,General,Ommetaphobia is the fear of,Eyes + History & Holidays,How many witches in a coven ,13 ,Sports & Leisure,"In which sport is the term ""wishbone"" used",Football,General,What president could write Latin with one hand & Greek with the other,James garfield +General,Name 1st Disney cartoon film based on the life of a real person,Pocahontas,People & Places,Which Engineer's Statue Watches Over The Trains At Paddington Station In London? ,Isambard Kingdom Brunel ,General,From the milk of which animal is ricotta made,Sheep +Music,Which 90'S Group Were Named After A Bernard Cribbins Hit Of The 60'S?,Right Said Fred,General,What could Victorian advertisements not show,Beds - hidden behind curtains, Geography,What is the capital of China ?,Beijing +General,"An ornamented canopy over an altar, tomb or throne. ",Baldachin, History & Holidays,"Which movie is the line 'Snakes, I hate snakes' from? ",Raiders Of The Lost Ark ,General,Edmund Dante is what eponymous hero,The Count of Monte Christo +General,Dom Perignon invented champagne - what else,Corks in bottles,General,Name Indiana Joneses dog,Indiana,General,"Does a wild rabbit live 10, 15 or 20 years",Ten +Geography,What is the capital of Lesotho,Maseru,General,"Who appeared in 'st. elmo's fire', 'the scarlett letter' and 'striptease'",Demi moore,General,"The railway executive, Sir William Cornelius (1843 - 1915), is associated with the construction of which railway",The canadian-pacific +General,Who was Led Zeppelins original lead singer,Robert Plant,General,What do Americans call the vegetable swede,Rutabaga,General,A flageolet is another name for what musical instrument,A penny whistle +Music,"The Title Of The Manic Street Preachers Album ""This Is My Truth, Tell Me Yours"" Came From A Slogan Coined By Which Old Labour Politician",Aneurin Bevan,Music,Susanna Hoffs Was A Member Of Which Band,The Bangles,Music,What guitar company created the 'Flying V' guitar in the late 1950's?,Gibson +General,The Gravindex Test Is Used To Detect What,Pregnancy,General,"Since 1600, 109 species and subspecies of what have become extinct",Birds,General,Who first played Flash Gordon on film,Buster Crabbe +Geography,Approximately 70 percent of the Earth is covered with water. Only 1 percent of the water is _______________,Drinkable,General,What's the most powerful card in Euchre,Right bower,General,What is 3 for a child 6 a woman 9 for a man,Funeral Bell Tolls +General,"What portable device did James Spengler invent in 1907, using a soap box, pillow case, a fan & tape",The vacuum cleaner vacuum cleaner a vacuum cleaner,Music,"Who Had Her Only Top 10 Hit With ""See The Day"" In 1985",Dee C Lee,Music,"Which Theme Tune Sang By Matt Monro & Composed By John Barry , Came To Us With Love",From Russia With Love +General,Where was the bloodiest one day battle fought in the u.s civil war,Antietam,General,Which team won five Stanley cups during the 60s,Montreal,Music,What was John's father's name?,"Alfred ""Freddie"" Lennon" +General,"What is a system of government in which states are united for certain purposes, but for others are independent",Federalism,General,"Where would you find a breast, fore, spring and after spring",4 of 6 mooring lines tying up ship,Geography,The highest temperature ever recorded on Earth was in which country,Libya +General,In musical notation there are five lines in a what,Stave,General,24% of American adults admitted to participating in what,Illegal Gambling, Geography,Name the most north-easterly of the 48 contiguous states.,Maine +General,The Soldiers Song is the National Anthem of what Country,Republic of Ireland,Entertainment,Film Title: ______ (a number) Leagues Under the Sea.,"20,000",General,To which animal is the adjective caprine refer,Goat +General,In which country do the Khmer people live,Cambodia kampuchea,Technology & Video Games,Who invented the Hovercraft ?,Sir Christopher Cockerell,General,What is the first day of the week,Sunday +Geography,In Which Country Was The Battle Of Waterloo Fought ,Belgium ,General,Coco-cola' was invented in which year,1886,General,A giraffes long tongue is what colour,"Blue, blue black really" +General,Fluctat nec Mergitor - Wave tossed but not sunk - Cities Motto,Paris,General,What was the name of the movement founded by the Pole Lech Walesa,Solidarity,General, The study of the manner in which organisms carry on their life processes is ________.,Physiology +General,In Albany NY in winter children were arrested for what illegal act,Sledging and sledges were broken,General,What was the first 30 minute animated Disney show,Duck Tales,General,In which sport might yoU.S.ee an Axel-Paulsen and a double Lutz,Ice skating +Science & Nature, __________ is one American breed of hardy hogs having drooping ears _ it was allegedly named after the horse owned by the hog's breeder.,Duroc,Music,Which girls make the Beatles scream and shout?,Moscow girls,General,"When was the Berlin Wall erected, and when was it dismantled",1961 1990 1961 +General,What was the d-day invasion password,Mickey mouse,General,How Did St Valentine Die ,Beheaded ,General,Pedophobia is a fear of ______,Children +General,Treat religious name or subject irreverently,Blaspheme,General,What type of food is pecorino?,Cheese,General,Name either of the only sisters to both win acting Oscars,Joan fontaine olivia de haviland +General,What is the largest gold refinery,Rand refinery, Geography,What is the capital of Uruguay?,Montevideo,General,Who was the mad monk,Rasputin +General,What continent is home to jaguars,South america,General,What human organ houses your amygdala & thalamus,The brain brain your brain, Geography,In which country is Chennai (formerly Madras)?,India +General,"In the 15th century, what was the war between the houses of lancaster and york",War of the roses,General,How many pints of blood does the average human have in his/her body,12,Food & Drink,How many herbs and spices are used in Kentucky Fried Chicken?,Eleven +General,What beating victim's 23-lawyer defense team handed the city of los angelesfor million,Rodney king,General,What links Martha Corey Brigit Bishop Mary Easty in 17th cent,Salem Witchcraft trial,General,The left lung is smaller than the right lung to make room for what,Heart +General,In Kingsville Texas its illegal for who/what to shag on airport land,Pigs,General,What is Calvados,Apple Brandy,General,"A clip, shaped like a bar to keep a woman's hair in place is a _______",Barrette +General,The most expensive car produced by the Chrysler Corporation.,Dodge viper,General,Where was Bonnie Prince Charlie born,Rome,General,"What group of minerals are corundum, sapphire and ruby a part",Alumina +General,"In the US 20,000 what are made for children each year",TV ads 7000 cereals,General,"The first Corvette rolled off the Chevrolet assembly line in Flint, MI. in what year",1953,Geography,What Do You Call A Narrow Strip Connecting 2 Land Masses ,An Isthmus  +General,What is the fear of rabies or of becoming mad known as,Lyssophobia,General,In formula one grand prix what does a black flag waving mean,Car go into pits,General,"Which group sang the song ""Thank You For Loving Me""?",Bon Jovi +General,Who wrote the words of the hymn 'Amazing Grace',John newton, History & Holidays,"In the movie 'Home Alone', Macaulay Culkin plays a boy who is accidentally left alone at Christmas. What is the name of this character? ",Kevin ,Sports & Leisure,Baseball: The Baltimore ________.,Orioles +Music,What Was The B52's Chrysler As Big As,A Whale,Sports & Leisure,At Which Grand Prix Circuit Did Aryton Senna Lose His Life? ,"San Marino , Italy ", Geography,The Palk Strait runs between which two countries?,India and Sri Lanka +Music,Brian Harvey Was The Lead Singer With Which 90's Boyband,East 17,General,"Who co-starred with julie andrews in ""mary poppins""",Dick van dyke,General,What name is popularly applied to twins congenitally united in a manner not incompatible with life or activity,Siamese twins +General,Who wrote the Booker Prize-winning novel The Old Devils,Kingsley amis,Music,"Which Group Gave Us The ""Four Bacharach & David Songs"" EP",Deacon Blue,General,What is the capital of South Dakota,Pierre +Geography,In what country is Banff National Park,Canada,General,"Can You Name Any Year In The Life Of ""Nostradamus""",1503 - 1564,General,Napoleons life was saved by a dog what breed – and he hated dogs,Newfoundland – saved from drowning +General,Name Lancashire town first test tube baby born,Oldham,General,Games Slater invented what,Fibreglass,General,"Which country has won the most Olympic gold medals at 10,000 metres",Finland +Science & Nature,This large bean-shaped lymph gland can expand and contract as needed.,Spleen,General,Who said 'Give me a firm place to stand and I will move the Earth' ?,Archimedes,General,"Who is the elder statesman of 'british blues', and fronted 'the bluesbreakers'",John mayall +General,What nationality are the most immigrants to the USA,Mexican,General,Who was atahualpa,King of the incas,General,Where are the cheddar caves,Mendips +General,What is the fear of speed known as,Tachophobia,General,"In Greek mythology, the riddle of what did oedipus solve",Sphinx,Science & Nature,What are the moufflon & bighorn ,Sheep  +General,Capital cities name translates as City of Islam,Islamabad,Geography,What Separates Spain From Morocco ,The Straits Of Gibralter ,Art & Literature,Who Wrote The Opera (The Flying Dutchman) ,Wagner  +General,What was added to Band Aids in 1940,Red opening string,General,In Tennessee it is illegal to drive if you are what,Asleep, Geography,What is the capital of Russia ?,Moscow +General,A blunt thick needle for sewing with thick thread or tape,Bodkin,General,"""Gherkins"" are a type of ______",Pickle,General,Name origins Baker obvious but what had a Palmer done,Pilgrimage returned with palm leaf +General,Rosalind Julia Portia Viola Cymbeline what links not obvious,Heroines dress as men,General,Which group of islands lie between Iceland and the UK,The faeroes,Science & Nature,What is the meaning of the name of the constellation Equuleus ?,Colt +Art & Literature,What Is The Earliest Known Drawing Medium ,Charcoal ,General,Where is the painted desert,Arizona,General,What is the only bird in the world that can fly backwards,Hummingbird +General,MDMA is another name for which illegal drug,Ectasy,Science & Nature,What Was Invented By An American Firm Called Texas Instruments In 1958 ,The Silicone Chip ,General,Who owns all the swans in england,Queen elizabeth +General,What do you call the three wires on a transistor,"Emitter, Base, Collector",General,In Judo if the referee calls Sono-mama what does it mean,Players must freeze in position,General,Which south african oil company has estblished the only commercially proven 'oil from coal' operations in the world,Sasol +Music,"Which Danish Duo Rode Their ""Little Donkey"" Up The Charts In 1960",Nina & Frederick,Geography,What Was Istanbul Formerly Known As ,Constantinople ,Entertainment,What was the relationship between Superman and Supergirl,Cousin +Science & Nature,What is the latin name for the top set of vertebrae?,Cervical,Music,"Grandmaster Melle Melle's ""White Lines"" Has Been Released 4 Times On Which Occasion Did It Reach It Highest Chart Position",2nd,General,Who is Shirley Mclean's brother,Warren Beatty +General,Name the British General in charge of the 1916 offensive WW1,Sir Douglas Haig,General,What is the study of word origins,Etymology,General,"Which German actress appeared in the film ""Witness for the Prosecution",Marlene dietrich +General,Where would one eat a taco?,Mexico,General,Which musical features the song Some Enchanted Evening,South pacific,General,Who is mother goose's son,Jack +General,Which red-head won Star Search and then went on a shopping mall singing tour that took America by storm in 1987 and 1988?,Tiffany,General,What was king arthur's mother's name,Igraine,Music,Who Composed The Music For The 1994 Muisical Copacobana,Barry Manilow +General,Where are swedish buns found,Denmark,General,U.S. Captials - Georgia,Atlanta,General,In which sport might yoU.S.ee a googly,Cricket +General,Which instrument developed from African and Latin-American origins features in a modern orchestra as a bass xylophone,Marimba, History & Holidays,Which War Involving The UK Began In 1982 ,The Falklands War ,Music,Which 2 Artists Also Died When Buddy Holly's Plane Crashed,"Richie Valens & The Big Bobber," +Sports & Leisure,Which Football Team Are Known As The Toffees ,Everton ,General,"Queen Victoria was a carrier of which disease that was responsible for the death of her son, Leopold",Haemophilia,General,What are the two main islands of the Philippines,Mindanao & luzon +General,What do Yoni worshipers worship,Female Genitals, History & Holidays,What is the former name of Sri Lanka?,Ceylon,Music,"The Rolling Stones First Single ""Come On"" Was A Cover Of A B Side By A Famous US Rock N Roller Name Him",Chuck Berry +Music,"Kylie Minogues First Single Was ""I Should Be So Lucky"" Or ""The Locomotion""",I Should Be So Lucky,Entertainment,In which Verdi opera does Violetta sing 'Sempre Libera'?,La Traviata,General,Which country do Sinologists study,China +General,In what field were Louis Daguerre and William Henry Fox-Talbot pioneers,Photography,General,Sericulture involves raising which animals,Silkworms,General,Yasser Arafat has been leader of the PLO since what year,1969 +General,"Given 5 Years Either Way, In What Year Did Sir Francis Drake Begin His Voyage Around The World",1577,Music,"Musician Ray Brown Was Married To Ella Fitzgerald, Which Instrument Did He Play",Bass,Technology & Video Games,What does the CAT in CAT-SCAN stand for?,Computerised Axial Tomography +General,Old superstitions - it is bad luck to do what in the morning,Sing,General,What part of a chicken is the 'parson's nose',Rump,General,What can go without water longer than a camel,Giraffe +Entertainment,"In the TV sitcom 'Married With Children', what is the dog's name?",Buck,General,In China big wigs have four but lesser men only two what,Outside Pockets,General,What Beatle reportedly slept with a light on,John lennon +General,Who invented condensed milk,Gail borden,General,"In the film 'american hot wax', who played the 'mookie'",Jay leno,General,Premises used for prostitution,Brothel +General,What is the smallest member of the weasel family,Skunk,Science & Nature," __________ instinctively know their own endurance and will refuse to move beyond it. If their masters try to drive them farther, they will lie down and refuse to budge.",Camels,Music,In 1932 Edward Elgar recorded his violin concerto. Who was the sixteen-year-old soloist?,Yehudi Menuhin +Geography,What is the capital of Laos,Vientiane,Sports & Leisure,When Maradona performed his Hand of God trick which goalkeeper did he beat? ,Peter Shilton ,General,Which Hollywood star was born Fredrick Austerlitz,Fred astaire +General,Clemantine Campbell became famous under what name,Cleo Lane,Geography,Which Is The Largest Freshwater Lake ,Lake Superior ,Mathematics,The space occupied by a body is called its ______.,Volume +Music,"The Doors Frontman “Jim Morisson” Passed Away In 1971 , But Can Anyone Tell Me Where Exactly Was The Body When He Was Found.",In The Bath,General,Whose horse was called Traveller,Robert E Lee,General,Solanum Tuberosum is the Latin name for what plant,Potato +General,In the theatre what is behind Barn Doors,Electricity Sockets,General,What is the shortest event in speed skating,500 metres,General,"Who said ""Go ahead - make my day"" (character name)?",Harry Callaghan +Music,Brotherhood Of Man Won The Eurovision Song Contest For Which Country In 1976,UK,General,What is the banking system in the u.s known as,Federal reserve system, History & Holidays,Which European Country Withdrew Its Forces From NATO In 1975? ,Greece  +General,After who was the month of July named,Julius Caesar,General,What escape route did John Brown run,Underground railroad,General,Who is reginald dwight known as,Elton john +General,She is famous for her airbrushed woman-animal art,Olivia,General,Samuel Hahnermann developed what type of therapy,Homeopathy,Music,"In The UK, Which Has Been James Brown's Only Top Ten Hit?",Living In America + History & Holidays,What is the 15' by 18' cell that 146 captured british officers were forced into by indian troops in the 19th century called?,Black Hole of Calcutta,General,Bangkok is the capital of ______,Thailand,Music,Which Star Was Don Mclean's Hit American Pie Dedicated To,Buddy Holly +General,"In Hindu mythology, who is kali",Mother goddess,General,"Associated with 'Blues' music, which instrument is nicknamed a 'Mississippi saxophone'",Harmonica,General,In what Australian state would you find Mildura,Victoria +Science & Nature,Lester B Pearson International Airport Is In Which Canadian City ,Toronto ,General,"Detroit, Skokie Illinois St Paul Minnesota what official instrument",Accordion,General,Dobby Selvages and Filling are terms used in which process,Weaving +General,ORD are the identification letters of what airport,O'Hare Chicago,Science & Nature, The spines on a newborn __________ start to appear within 24 hours.,Hedgehog,People & Places,Which Famous actress Once Said: ' I am a marvelous housekeeper. Every time I leave a man I keep his house '? ,Zsa Zsa Gabor  +General,Who was george washington's vice-president,John adams,General,Name the muscle at the front of the shoulder and above the biceps which serve to raise the arm laterally,Deltoid deltoideus,General,What do supertankers carry,Oil +Science & Nature,"Hyraxes, Which Grow To Weigh A Maximum Of 5kg & And Are Conies Mentioned In The Bible, Have A Shared Ancestry With Which Large Mammal ",Elepahnt ,General,Who was the only starting pitcher in a world series to bat anywhere but ninth,Babe Ruth,General,Mary Surratt what the first woman to do what in US,Be Hanged +Sports & Leisure,What sport was invented in 1973 by a group of drinkers at a pub in West Sussex? ,Lawnmower Racing ,Science & Nature,What is the name of the minute organisms found drifting near the surface of seas and lakes ,Plankton ,Art & Literature,What Was The Title Of The First Ever James Bond Novel ,Casino Royale  +Geography,KLM Is The National Airline Of Which Country ,Holland / The Netherlands ,General,Which u.s state has the smallest population,Alaska,Music,Which Merseyside Group Who Had 3 No.1 Hits During 1963 & 1964 Took Their Name From The Title Of A 1956 Movie Starring John Wayne,The Searchers +Sports & Leisure,Who was supposed to present Jesse Owens his 100 metre Gold Medal at the Berlin Olympics? ,Adolf Hitler ,General,"What Christina Crawford book was originally titled ""the hype""",Mommie dearest,General,Mantle what is the official name of michael knight's car,Knight Industries 2000 +General,"Of what are epidermal cells, palisade cells and veins a part",Leaf,Sports & Leisure,Name the famous Italian football team who play in Black and White stripped shirts? ,Juventus ,General,What was Pierce Brosnan's first James Bond film in 1995,Goldeneye +Music,What Musical Instrument Does Elton John Play,Piano,Food & Drink,Who released the following 'edible' album 'Shaved fish' ,John Lennon ,Music,"""Move On Up"" Was The Only Major Hit For Which US Male Vocalist",Curtis Mayfield +General,In Disney's Fantasia what is the Sorcerers name,Yensid – Disney reversed, History & Holidays,What Is The Name Of The Cake Traditionally Eaten In Italy At Christmas Time ,Panettone ,General,William Robert Greer (1909-1985) Is Famous For Undertaking Which Famous Historical Journey,Kennedys Driver +General,In 1976 what show appeared on TV for the first time,Charlie Angels,General,Who was king arthur's champion,Sir lancelot,General,What country drink the most milk per capita,Iceland +General,Desire for more cows is the translation what Sanskrit word,War - same in English,General,What is the more common name for Alkane,Paraffin or Kerosene, History & Holidays,Which King Wore Two Shirts At His Execution So As Not To Shiver And Appear Frightened? ,Charles I  +General,Who wrote the Savoy Operas,Gilbert and Sullivan,General,What British actor got 2.25 percent of the profits from STAR WARS,Alec Guinness, Geography,What is the second highest peak in Mexico?,Popocatepetl +Science & Nature,The Big Dipper is part of what constellation,Ursa major,General,The author of Moll Flanders wrote which more famous work,Robinson Crusoe,Entertainment,Who starred as 'ouboet' in the first TV series of 'Orkney Snork Nie'?,Frank Opperman +General,What is the white trail behind a jet plane made from,Ice Crystals,Science & Nature,Who discovered Vitamin C?,Linus Pauling,General,Where were the first glass mirrors made in Europe circa 1300,Venice +Food & Drink,What type of food is Port Salut? ,A Cheese ,General,Phobos is a moon of which planet,Mars,General,"In what game would you see Stamen, Blackwood and Gerber",Bridge + History & Holidays,Where does Santa land his sleigh? ,On The Roof ,Food & Drink,This liquor brand accents a Scarlet O' Hara or a Rhett Butler,Southern comfort,General,The Althing rules in which country,Iceland +General,Which vegetable is 91% water,Cabbage,Food & Drink,What is 'water of life' in 'Latin' ,Aqua vitae ,General,What's used to make grenadine syrup,Pomegranate +General,Which cellular structures are composed of DNA,Chromosomes, History & Holidays,Who was George Washington's vice_president,Adams,General,Harold the Fairhead was the first supreme ruler of where,Norway +General,What colour are an American porcupines teeth,Orange,Sports & Leisure,Which Sport Is Played By The Philadelphia Flyers ,Ice Hockey ,General,What is the maximum number of degrees in an obtuse angle,179.9 +General,What is the Capital of: American Samoa,Pago pago,General,What name is given to the part of the sundial that casts the shadow,Gnomon,Food & Drink,What is Britain's top selling fruit flavoured soup? ,Tomato  +General,What train leaves pennsylvania station at quarter to nine,Chattanooga choo,General,What is the world's oldest newspaper(the name is swedish) ?,Post och Inrikes Tidningar,General,To which family of birds does the 'Linnet' belong,Finch +General,"Sun-dried brick used in places with warm, dry climates, such as Egypt and Mexico; also, the structures built out of these bricks. ",Adobe, Geography,What is the second largest continent in the world?,Africa,General,A mass of flowers on a tree,Blossom +General,Who discovered penicillin,Sir alexander fleming,General,How Is “Ethylene Glycol” Better Known?,Antifreeze,General,What was the name of the city in the Bible which was built by Cain and named after his son,Enoch +General,"Stones hewn, squared, and smoothed for use in building, as distinguished from rough building stones.",Ashlar,General,In the movie Next Friday what is the name of the vicious dog?,Chico,General,Martin Fallon Hugh Marlow James Graham Harry Patterson who,Jack Higgins +General,25% of the adult male population of the UK are what,Over 6 feet tall,General,Which two countries made up the 'Dual Monarchy' which existed from 1867-1918?,Austria and Hungary,General,What is the fear of pregnancy or childbirth known as,Tocophobia +General,What bird has the maximum recorded life span 36 years,Herring Gull – Royal Albatross,Geography,In Which Country Can You Find The Sperrin Mountains ,United Kingdom ,Music,Whose Body Was Stolen And Cremated In The Desert,Gram Parsons +General,Consumption was the former name of which disease,Tuberculosis,General,Katy Mirza was the first Indian woman to do what,Feature in Playboy,General,Taidje Khan became famous under which name,Yul Brynner +Music,Which Well Known Pop Group founding The Company Polar Music,Abba,Religion & Mythology,Who is the greek equivalent of the roman god Neptune ?,Poseidon, History & Holidays,How many ghosts appeared to Scrooge in Dicken's 'A Christmas Carol'' ,Four  + History & Holidays,What daughter of czar nicholas ii is said to have escaped death in the russian revolution ,Anastasia ,General,For how many years was Queen Elizabeth the First on the throne of England,45 years,General,Whats the most abundant element in the sun,Hydrogen +General,"Petula clark sings 'if you're feeling sad and lonely, there's a service i can render'. what is it",Call me,General,"What tiny animal, valued for its pale grey fur, lives higher than any other in the andes",Chinchilla,General,What is the first name of Sean Connery's actor son,Jason +Music,What Was Roxy Music's Only No.1 Hit,Jealous Guy,Food & Drink,Which scottish river supplies over 90% of the water used in whiskey manufacture? ,Spey ,Music,Who Was The Drummer In “The Dave Clark Five”?,Dave Clark +Music,What Is The Smallest Instrument In An Orchestra,A Piccolo,General,If you suffered from varicella what have you got,Chickenpox,Science & Nature, A tiger's paw prints are called __________. A tiger's forefeet have five toes and the hind feet have four toes. All toes have claws. The claws are 80 to 100 mm in length.,Pug marks +General,In wacky races who drove the Creepy Coop,Gruesome Twosome,Geography,What is the capital of Lebanon,Beirut,General,What lager reached the parts other beers could not reach,Heineken +General,What was alanis morissette's first album,Jagged little pill, History & Holidays,"Which directors films included Shivers, Videodrome and The Fly ",David Cronenberg ,Music,Which 1985 Hit  For Dire Straits Features 2 Members Of The Phonetic Alphabet,Romeo & Juliet +Music,"Who Sang The Seductive ""I Wana Sex You Up""",Color Me Bad,General,What is a group of dogs,Pack,General,Dr F Lanchester invented what motor safety aid in 1902,Disc Brakes +General,Who was the 10th president of the U.S.,John tyler,Geography,Which County Is Stratford - Upon - Haven In? ,Warwickshire ,General,What does michael jackson call his home,Wonderland +General,What is the sixth month of the year,June,Music,Which Band Featured Ex New York Dolls Guitarist Johnny Thunders,The Heartbreakers,General,"Where would you find a Rocker, Eight, Loop and Three",Figures in Ice Skating +General,Which war was formally concluded at Paris in 1856,Crimean,General,Who composed the music for the ballet 'l'apres-midi d'un faune',Claude,General,What two countries contain the Sierra Nevada mountains,"Spain, usa spain" + Geography,What is the basic unit of currency for Malta ?,Lira,General,Who wrote the book Forest Gump,Winston Groom,Entertainment,Green Lantern's alter ego,Hal jordan +General,William Golding won the Nobel Prize for literature in which year,1983,General,In the pasa doble what is the female dancer supposed to be,A bullfighters cloak,General,What was the first rap group to appear on 'american bandstand',Run d.m.c +General,Magnum PI wore a baseball cap supporting what team,Detroit Tigers,General,What is the name of the capital of Quebec,Quebec City,General,"The longest time someone has typed on a typewriter continuously is 264 hours, set by whom",Violet Burns +General,President Kennedy was shot in Dallas in what type of car,Lincoln,General,According to strain theory crime is mainly committed by who,The lower classes,Geography,What is the capital of San Marino,San marino +Music,What 1958 Eddie Cochran song became his biggest US hit and a rock classic?,Summertime Blues,General,What is the most commonly spoken language in India,Hindi,General,What bird builds a nest about 12 feet deep and eight and a half feet wide,Bald eagle +Science & Nature,What is a Blue Moon?,2nd full moon in 1 month, Geography,Which U.S. state has the least rainfall?,Nevada,General,"""I Melt With You"" was this band's signature hit, released on their after the snow album in 1982",Modern English +General,What English meadow saw the signing of the Magna Carta,Runnymede,Food & Drink,"Which Vegetable Can Be Oyster, Chestnut, or Shitaki? ",Mushrooms ,Science & Nature,What animal can get the disease 'heaves'?,Horse +Art & Literature,Who Told Stories About Brer Rabbit & Brer Fox ,Uncle Remus ,General,In The World Of Literature Who Live At No.7 Savile Row (London),Phileas Fogg,Food & Drink,What drink is named after the queen of England who was famous for her 'sanguinary' persecution of the protestants?,Bloody Mary +General,Kolpeuryntomania is what sexual activity,Stretching the vagina, History & Holidays,During Which Monarch's Reigns Did Shakespeare Live? ,Elizabeth I & James I ,Food & Drink,From Which British City Do Balti Dishes Often Used In Indian Cuisine Originate From ,Birmingham  +General,AG Bell opened school in Boston in 1872 for Teachers of what,The Deaf,Toys & Games,Klondike is the most popular form of this game.,Solitaire,Geography,"______________ has the greatest number of islands in the world: 179,584.",Finland +General,Dagon is the mesopotamian god of ______,Vegetation,General,Where would you find an Oculus,Dome central opening,General,What was introduced to the UK 1799 as a temporary measure,Income Tax +General,Second city: Brisbane (state),Rockhampton,Sports & Leisure,With Which Sport Would You Associate Jahangir Khan ,Squash ,General,Approximately how long after the atomic bombs were dropped on Japan did they surrender,One week +General,"Who wrote The Bad Child's Book of Beasts, first published in 1896",Hilaire belloc,General,Which country invented the mariners compass,China,General,In the language of flowers what does the cucumber flower mean,Criticism +General,What u.s president was castigated for picking up his pet beagles by the ears,Lyndon johnson, Geography,Where is the original London Bridge located?,"Lake Havasu, Arizona", Geography,What country was once known as Gaul?,France +Music,Which James Bond Film Duran Duran Provide The Theme Music For,A View To A Kill,Sports & Leisure,Which Scottish Athlete Struck Gold In The 100m At The 1980 Moscow Olympics ,Alan Wells ,General,Who is Yogi Bears girlfriend,Cindy Bear +General,Who cut off Samson's Hair (King James Edition),An Unnamed Man, History & Holidays,Who Released The 70's Album Entitled Blood on the Tracks ,Bob Dylan ,General,Who was the author ofn the novel 'The Crow Road'?,Iain Banks +General,Who Was The First Female Presenter Of The Old Grey Whiste Test,Annie Nightingale,General,What is the term of an animal with pure white skin & hair & pink eyes,Albino,General,What play marked marlon brando's last broadway appearance,A streetcar named +Food & Drink,What Type Of Oranges Are Traditionally Used To Make Marmalade ,Seville Oranges ,Entertainment,Who played in the film 'Ragtime' after 20 years offscreen?,James Cagney,Geography,___________________ is the only Central American country not bordering the Caribbean Sea.,El salvador +General,Police in Winchester - got call - Man being held by wife - how,Hidden artificial leg,General,The eyes of which animal have rectangular pupils,Goat,Geography,"Which Country Has Borders With France, Germany, Austria, Lichtenstein & Italy ",Switzerland  +Entertainment,Who was the first voice of Mickey Mouse?,Walt Disney,General,"Jagger, Richards, Wyman, Jones, Watts, Stewart - which band",The Rolling Stones,General,Who shot j.r ewing in the tv series 'dallas',Kristin shepard +Tech & Video Games,What is the most powerful whip in 'Castlevania 2'? ,Flame Whip,General,Purple green and gold are the official colours which annual event,Mardi Gras,General,"On 'dragnet', who played officer bill gannon",Harry morgan +General,Men women compete 3 Olympic sports Equestrian Shooting ?,Yachting,General,Which cartoon company is based in Walla Walla Washington,Acme in Roadrunner,General,"In which children's pantomime does the character ""Widow Twankey"" appear",Aladdin +Science & Nature, A __________ can remember a specific tone far better than can a human.,Dolphin, History & Holidays,Whose army did Admiral Nelson defeat at the battle of Trafalgar?,Napoleon,Music,"Boyzones First UK Hit Was ""Love Me For A Reason"" Who Had A Hit With The Original",The Osmonds +Food & Drink,By which name are English Truffles known? ,Pignuts ,General,How were 'Mr Barrow' and 'Miss Parker' better known,Bonnie and clyde,Entertainment,On what T.V. show could Tom Terrific be found,Captain kangaroo +General,Iophobia is the fear of,Poison,General,What is a Vitrine,Glass Display Cabinet,General,"On which Scottish island is the malt whisky, Talisker, produced",Skye +General,How many weeks until Christmas Day if it is the 25th September?,13 weeks,Sports & Leisure,David Campeze Was the Leading try Scorer for Which country ,Australia ,General,"In 'snow white and the seven dwarfs', what was the occupation of the seven dwarfs",Miners +Sports & Leisure,In 1992 Kevin Keegan became manager of which football club? ,Newcastle United ,General,Of what were ancient egyptian pillows made,Stone,General,While grass grows the horse ___.?,Starves +Music,Which Track Became Elvis Presley's Final UK No.1 In The Weeks After His Death,Way Down,Music,How Old Was Marvin Gaye When He Was Shot Dead By His Father,44 Years Old, History & Holidays,Which English King was crowned on Christmas Day ,William the Conqueror (1066)  +General,Which Artist Has Had More Hits Dead Than Alive?,2Pac Shakur,Sports & Leisure,With which sport do you associate `Flushing Meadows`? ,Tennis ,General,Peladophobia is the fear of what,Bald People +General,What kind of creature is a funnel web,Spider,Music,"Before Belinda Carlisle Found ""Heaven On Earth"" She Was Part Of Which All Girl Group",The Go Go's,Sports & Leisure,"Steve Davis Reached The Final Of Every World Championship Between 1983 & 1989, Loosing On Just 2 Occasions 1 Point For Each Of The Players Who Beat Him ","Dennis Taylor, 1985 Joe Johnson, 1986 " +Music,What Was The Name Of Prince's Band In The 80's,The Revolution,General,To who did the sword excalibur belong,King arthur,General,Who was the only unmarried president,James buchanan +General,In British Columbia is illegal to kill what,Sasquatch,Music,Which Band Released The Best Selling Album Rumours In 1977,Fleetwood Mac,Sports & Leisure,Colin Hendry left Blackburn in 1998 to join which Scottish Club? ,Rangers  +General,In which country is the city of Omdurman,Sudan,Geography,Which City Is The Capital Of Australia ,Canberra ,General,Ancient Roman hall with colonnades,Basilica +Geography,"In which country would you find the Angel Falls, the highest waterfall in the world? ",Venezuela ,Toys & Games,This is a French named car_racing card game.,Mille borne,General,A paddling is a group of which animals,Ducks +General,A Hobbits will requires seven signatures in what,Red Ink,Geography,The water of the ___________ is seven to eight times saltier than ocean water.,Dead sea,General,"In psalm 46, what is the 46th word from the last word",Spear +General,Which computer firm made a model called Amiga,Commodore,Sports & Leisure,Between 1961 & 1990 a win in Formula One was worth how many points? ,Nine ,General,What is an ancient word for a pharmacist,Apothecary + History & Holidays,The following is a line from which 1970's film 'Blessed are the cheese makers' ? ,Life of Brian ,General,"The star of ""Police Woman"" was married to Burt Bacharach. What was her name",Angie dickenson, Geography,What is the capital of Panama ?,Panama City +General,What is podobromhidrosis?,Smelly feet,General,What did Gregor Mendel study,Heredity, History & Holidays,How many British officers were forced by Indian troops into the Black Hole of Calcutta?,146 +General,What did Scott find at the North Pole,Nothing - he never went there,General,"This spikey succulent, native of Africa is often an additive in creams and lotions?",Aloe Vera,General,What's the fifth largest country in the world,Brazil +General,What was Oliver Mellors' daytime job,Gamekeeper,General,What final event did Gladstone and Churchill share,State funeral,People & Places,Which Romanian Tennis Player Was Famous For His Tantrums ,Ilie Nastase  +General,Hellenologophobia is a fear of ______,Greek terms,Music,Which Prodigy Album Cover Features The Screaming Metallic Like Face Photographed By S Haygarth,Music For The Jilted Generatiojn,Geography,"The citizens of _________________, Brazil are called ""Cariocas.""",Rio de janeiro +General,On the show FRIENDS what was Phoebes twin sisters name?,Eursela,General,"Who wrote the poem, the lines were composed a few miles above, Tintern Abbey",William wordsworth,Music,"Which Band Had Magical Results With ""Abrcadabra"" In the Early 80's",The Steve Miller Band +General,What is a 'tandoor',Clay oven, History & Holidays,Is A Pumpkin A Vegetable Or A Fruit ,A Fruit It Grows On Vines ,Music,Name The Andrae True Connection's only top ten hit?,"More, More, More" + Geography,What is the capital of Seychelles ?,Victoria, Geography,What European country administers the island of Martinique?,France,Science & Nature,In 1976 JVC Introduced The VHS Video Format What Does VHS Stand For ,Video Home System  + History & Holidays,"Who was the New Zealand mountaineer who in 1953, together with Sherpa Tenzing Norgay, became the first person to reach the summit of Mount Everest ",Sir Edmund Hillary ,General,What northern country Helsinki the capital of,Finland,General,What pop group saw their first 5 singles enter UK charts at No 1,Westlife +General,What's a computer screen's display made up of,Pixels,Music,Introductory interest rate that's charged on the Ringo-designed Discover card,5.9 percent,General,What comes after the year of the snake - Chinese calendar,Horse +General,What name was given to the 8th century Muslim invaders of Spain,Moors,Music,"Who Had Late 60's Hits With ""Aint Nothing But A House Party"" And ""Eeny Meeny""",The Showstoppers,General,What are the cutting teeth called,Incisors +General,The sackbut developed into which modern instrument,Trombone,General,Collective nouns - a romp of what animals,Otters,General,What is known as Radishes in Denmark,Cartoon strip Peanuts +Art & Literature,Where Is The Louvre ,"Paris, France ",General,Prior to 1998 who was the last non-American to win the U.S. Masters golf championship,Nick faldo,Toys & Games,"Name the only flexible murder weapon in the game of ""Cluedo"".",Rope +Music,Which Pair Had A Leaky Bucket In 1961,Harry Belafonte & Odetta,General,How many eyeballs does a four-eyed fish have?,2, Geography,Rome is the capital of ______?,Italy +General,There is a museum in Philadelphia 211 North 3rd Street to what,Pretzels,Mathematics,What is the maximum number of integer degrees in an acute angle?,89,General,"Who sang the song ""Another Brick In The Wall""?",Pink Floyd +General,Who was Poet Laureate during World War Two,John masefield,General,Which island do the nationalist Chinese occupy?,Taiwan,General,In which country did the Battle of Plassey in 1756 take place,India +General,Concord Is The Capital Of Which US State?,New Hampshire,General,What is the chemical name for water,Hydrogen oxide,General,Which group of hobbyists spend the most money on it,Gardeners +General,Board in what country would you find the world's largest pyramid,Mexico,Sports & Leisure,Which Tennis Player Has Announced they Are To Quit The Sport After It Emerged That They Tested Positive For Cocaine At This Years Wimbledon? ,Martina Hingis ,Science & Nature,Which Member Of The Orchid Family Is Used Commercially As A Flavouring? ,Vanilla  +General,The Japanese word 'karate' can be translated as,Open hand,Music,With Which Instrument Is Les Paul Associated,Guitar,General,The average child wears out 730 by age ten 730 what,Crayons +General,Polyhexamethyleneadipamide is better known as what,Nylon,General,What was Motowns biggest hit in 1968,Heard it through the Grapevine,General,The Dead Sea Scroll was discovered in what year,1947 +General,In which European country is the Dalmation coast?,Croatia,General,Who was the first black American in space,Guion bluford,Music,"Ballad Of Bonnie & Clyde"" Was A No.1 For Which Keyboard Playing Singer",Georgie Fame +General,What is the Capital of: Uganda,Kampala,General,Complete the saying 'What can't be cured must be _______________.',Endured,Food & Drink,What Is The Most Popular Chocolate Bar In The UK ,Kit Kat  +General,What was rembrandt's surname,Van rijn,Religion & Mythology,Who is referred to in the New Testament as 'the disciple Jesus loved'?,John,General,"What one word may be added to news, carbon and wall",Paper +Sports & Leisure,In Formula One what is the race course called in Italy? ,Monza ,General,What is the flower that stands for: severity,Branch of thorns,Geography,Which Cambridge Bridge Was Put Together In The 18th Century Without The Use Of A Nail ,The Mathematical Bridge (Queens College)  +General,What was the name of the hollow hole-covered plastic ball kids used to hit instead of a baseball?,Wiffle ball,Music,No.3 In The Charts Was A Lucky Number For Whom,Lene Lovich,General,What is Dick Grayson better known as,Robin (Batman and Robin) +General,"Which Country Did The 1998 Eurovision Song Contest Winner ""DANA INTERNATIONAL"" Come From",Israel, History & Holidays,"What type of shoes were favored by skateboarders? (Hint, it's 4 letterslong) ",Vans ,General,The word philosophy comes from Greek literally meaning what,Love of wisdom +General,"Who was the American master of decorative ""art nouveau"" glass, especially lamps",Louis comfort tiffany,Music,"What In 1966 Was A Secretary Not, According To Frank Loesser","""A Toy"" How To Succeed In Business",General,What is wild rice,Grass +General,"In the play ""The Entertainer"" what is the name of the Entertainer",Archie rice,Science & Nature,What period Came after the triassic?,Jurassic,Science & Nature,What is Nitrous Oxide better known as? ,Laughing Gas  +General,Which city had the world first public bus service,Paris,General,What name is given to a window that opens and shuts by moving up and down,Sash window,General,"Which poet wrote ""Fools rush in where angels fear to tread""",Alexander Pope essay on criticism +Entertainment,Where does young Anakin Skywalker come from?,Tatooine,General,An exclusive group of people,Clique,General,There are two general types of skiing Alpine and what,Nordic +General,"What are the sava, drava and soca",Rivers,Sports & Leisure,"Which sport has a movement called a ""telemark""?",Skiing,General,Silky case spun by larva to protect it as a pupa,Cocoon +Geography,Where is Queen Maud Land located,Antarctica, History & Holidays,This F.B.I agent headed the investigation of Al Capone.,Elliot ness,General,What does the acronym vhs mean,Video home system +General,In traditional wedding anniversaries what is given on the twelfth,Silk,Music,"Butthole Surfers Were Formerly Known As ""Ashtray Baby Heads"" Or ""Aliens Must Die""",Ashtray Baby Heads,General,Who invented the light bulb,Thomas edison +Entertainment,Name Cathy's on again/off again boy friend?,Irving,General,"Relating to food, what is 'Polenta' made from",Cornmeal,Geography,What is the capital of Kansas,Topeka +General,Whose epitaph reads Lived a philosopher died a Christian,Casanova,General,How much does the cullinan diamond weigh,3100 carats,General,From which city can yoU.S.ee Table Mountain,Cape town +General,What is the only word in English that ends in mt,Dreamt,General,Who is the alter ego of Henry the mild mannered janitor,Hong Kong Phooey,General,The film 'Dancer in the dark' features which pop star,Bjork +General,The Treaty of Paris in 1856 ended which war,The Crimean war,General,Spanish or Italian name for a bar or wine shop,Cantina,Art & Literature,What Is The Name Of The Bird In The Peanuts Strip ,Woodstock  +Music,Paul Hewson Is the Real Name Of Which Rock Star?,Bono,General,"What planet was pummeled by pieces of Shoemaker levy 9 in 1994, producing 2000 mile high fireballs",Jupiter,General,On average it rains 4 days a week in what European capital,Amsterdam or Brussels wettest +General,What is officially the poorest US state,Mississippi,General,"Napoleon had connections with three islands, he was imprisoned on Elba and died on St. Helena, where was he born",Corsica,General,What is a triangle with two equal sides called,Isosceles +Entertainment,Where do Rocky and Bullwinkle play football,What'samatta u niversity,General,What kind of aircraft is the c46 Commando built by Curtiss Wright,Cargo,Science & Nature,How Does A Snake Smell? ,With Its Tongue  +General,"In the film ""bringing up baby"", the baby is what",A leopard,General,If you landed at Carthage airport where would you be,Tunis,General,This term refers to any crown-shaped structure. It's also the name of a beer.,Corona +General,How did the Greek dramatist Aeschalys die,Eagle dropped tortoise on head,General,Who wrote the book 'Something Happened',Joseph heller,General,What is earwax,Cerumen +Sports & Leisure,"What comes next in the following sequence (Red, Blue, White, Black, Orange) ",Black and White (Grey Hounds) ,Science & Nature,What is the more scientific name for quicksilver,Mercury,Sports & Leisure,Of What Was Pluto Platter The Original Name ,The Frisbee  + Geography,What is the capital of El Salvador ?,San Salvador,General,Who's Auto Biography Is “Who Does She Think She Is”,Martine McCutcheon,General,Who initiated the works of the Bible,King solomon +Music,"In His Joy Division Days Bernard Sumner Used Two Other Surnames Before Settling On Sumner, Name Any","Dicken, Albrecht",General,American burrowing animal with plated body,Armadillo,General,What idea began in London in 1764,House Numbers +General,"Who Are Known To The British Secret Service With The Codenames ""Potus & Flotus""",Us President & First Lady,General,Margaret Thatcher day is 10th January in what area,Falklands Islands,Science & Nature,As what is an algonquin more commonaly known?,Moose +Music,Which Band Wanted to Know What Love Is,Foreigner,Art & Literature,"What play by Shakespeare features the following characters: Cornwall, Gloucester, Regan, and Goneril?",King Lear,Sports & Leisure,What Is The Last Event In A Decathlon? ,The 1500 Metres  +General,What is a period of play in polo,Chukka,General,Gynelophilous people get aroused from what,Pubic Hair,General,In Knoxville Tennessee it is illegal to lasso what,Fish +General,On what japanese city was the second atomic bomb dropped during world war ii?,Nagasaki,General,What did the Perthians conquer in 141 bc,Mesopotamia,Music,"According To Jonathan King, Where Was Everybody In 1965",On The Moon (Everyones Gone To The Moon) +General,What is the Capital of: Spain,Madrid,People & Places,Which Tory MP Made Multiple BT Share Applications ,Keith Best ,General,"Who wrote Sleeping beauty, Mother Goose, Puss in Boots",Charles Perrault +General,"On Friday 23rd Sept 1955 ITV First Began Boadcasting , But What Other Major Media Event Occurred On That Very Same Night!!!!!!!!",Grace Archer Died On Radio,People & Places,Whose Catchphrase Was 'Here's Another Fine Mess You've Gotten Me Into' ,Oliver Hardy ,Art & Literature,What Dutch master painted 64 self_portraits,Rembrandt +General,Who composed the classical piece Peter and the Wolf,Sergei Prokofiev,General,"What do camels store in their humps water, fat or milk",Fat,General,Whose porridge did Goldilocks eat,Three bears +Food & Drink,How Many Million Barrels Of Beer Are Sold Annualy In The UK ,36 Million ,General,Where was paper invented,China,General,Birdseye' introduced fish fingers into the UK in what year,1955 +General,"Originating around 1830 as a social dance, by 1844 it had become a raucous dance performed in French music halls.",Cancan,General,What did the roman emperors and the rich people of rome carried on,Litters,Geography,"Translation of _____________ is ""beautiful country"".",Sri lanka +Science & Nature,"Excluding man, what is the longest-lived land mammal?",Elephant,General,An altimeter measures what,Altitude,General,What is the flower that stands for: bluntness,Borage +General,What football team was previously known as the frankford yellow jackets,Philadelphia eagles, History & Holidays,This fearsome creature lived on Grympen Myre and was the principal character in which book ,The Hound Of The Baskervilles ,General,What is the fear of names known as,Nomatophobia +General,Whose headstone reads 'my jesus mercy',Al capone,General,"Who was a member of 'crosby, stills and nash' and 'the hollies'",Graham nash,General,The first toilet ever seen on television was on what show,Leave It To Beaver +Music,What Was Rodgers & Hammerstein's First Musical,Oklahoma!, History & Holidays,Who was the first British monarch to broadcast a Christmas message to the nation? ,George the Fifth (in 1932) ,General,What sort of garment is a dirndl,Skirt +General,What is the fear of hearing a certain word known as,Onomatophobia,General,What used to be measured in Gillettes,Laser Strength – no blades drilled,General,"What are blombergs, oak and fire bellied types of",Toads +Music,Known To Millions As Hank Marvin What Is His Actual Christian Name,Brian,General,From Which Country Does The Tomato Originate,Peru,General,Orient Express restarted in 1982 going from London to where,Venice +General,"Tissue or organ of the animal body characterized by the ability to contract, usually in response to a stimulus from the nervous system",Muscle,Music,"Which Group Sang The 80's Hit ""Fade To Grey""",Visage,General,"Best or nothing in the song 'skip to my lou', in what beverage are the flies",Buttermilk +General,Which Country Has The Highest Population Of Mobile Phone Users?,Finland,Music,In The Hit Song Tell Laura I Love Her What Was The Young Mans Name,Tommy,Science & Nature,What Is The Equivalent Of Rust Which Occurs On Copper ,Verdigris  +Science & Nature,The deficiency of which vitamin causes scurvy ,C ,General,What is Fonzies full name on Happy Days,Arthur Herbert Fonzerelli,Music,What Type Of Eyes Did Kim Carnes Sing About,Bette Davis Eyes +General,What flavouring is in frangelico liqueur,Hazelnuts,General,Hoy and Rousay are part of which British island group?,Orkneys,General,What is a sirocco,Wind +General,What is the real name of the actor who plays the voice of the spaceship in Flight Of The Navigator?,Paul Reubens,General,What part of the eye is affected by cataracts,Lens,General,Who had a 30-year love affair with dashiel hammett,Lillian hellman +Science & Nature, Baby opossums _ upon birth when they move to the mother's pouch _ are smaller than honeybees. An entire litter can fit in a __________,Teaspoon, Geography,"What European country has ""Vaduz"" as its capital city?",Liechtenstein,General,What is the lowest level of the Earth's atmosphere called,Troposphere +General,Which former Soviet Republic in Central Asia has Alma Ata as its capital,Kazakhstan,General,"Charing Cross in London was built in commemoration of Queen Eleanor, who was the wife of which British King",Edward i,General,Anreas Cornelis van Kujik was who’s manager,Elvis Col Tom Parker +General,Who wrote the book Interview with a Vampire,Ann Rice,General,Which British City Was Known To The Romans As Luguvallium,Carlisle,Sports & Leisure,How Many Minutes Is A Golfer Allowed To Search For A Lost Ball ,Five  + History & Holidays,Which 1964 Movie Had The Alternative Title 'How I Learned To Stop Worrying & Love The Bomb'' ,Dr Strangelove ,General,Of what did the poet John Milton die,Gout,General,What is a group of hawks called,Cast +General,How many days of Sodom were there according to the Marquis de Sade?,120,General,What portuguese territory will revert to china in 1999,Macao,General,In which British county is the land of the Prince Bishops,County durham +General,What animals cannot walk backwards,Emus,General,What is armagnac?,Brandy,General,What word the highest string on an instrument and a mushroom,Chanterelle +General,"The upper part of an entablature, extending beyond the frieze; also, ornamental molding projecting along the top of a building or wall.",Cornice,General,Which Fictional Character The Sister Of Lucy Was Raised In The Town Of Pitsdale By Her Parents Sam & Ella,Lois Lane,General,Captain Hans Langsdorff captained which German Battleship,Graf Spee +Food & Drink,Capers Are Derived From What ,The Pickled Flower Buds Of A Mediterranean Plant ,General,The Weir of Hermiston - last unfinished novel of who,Robert Louis Stevenson,General,"What us war killed brent and stuart, the tarleton twins",Civil war +General,International Airline Registrations N is what country,America,General,Which monarch was the first to appear on a postage stamp,Queen victoria,General,"Bond, Cotton Fabric and Tablet are types of what",Paper +General,"What country was ruled by pol pot, leader of the khmer rouge party",Cambodia,General,Students at Cambridge - no dogs - what Lord Byron keep,Bear,General,In a survey US ERs what is the most commonly broken bone,Clavicle or Collar Bone +General,All US Presidents were Federalists Republicans Democrats or what,Whigs 1841 - 45 1850 -53,General,What's a disk of gas orbiting a star of black hole called,Accretion disk,General,Into what bay does the golden gate strait lead,San francisco bay +Geography,What is the capital of Kenya,Nairobi,General,With what is 'Grand Marnier' flavoured,Orange,General,Which modem country was formerly Nyasaland,Malawi +General,Who was the youngest American President,Theodore Roosevelt,General,Kiki Haakenson a policeman's daughter was the worlds first what,Miss World,Music,Which day did the Boomtown Rats not like,Monday +General,Who is the most-capped scottish footballer,Kenny dalglish,General,Which Award Winning Movie Ended With The Line Hey Can I Try On Your Yellow Dress,Tootsie,General,UK football Derby County home the Baseball Ground nickname,Rams +General,"Which screen role has been played by, among others, Elliot Gould and Humphrey Bogart",Philip marlowe,General,What is the title of the wife of a Marquis,Marchioness,General,The Tony awards are named for what person,Antoinette perry +General,"The Louvre in Paris, was originally a what, before it was changed to a museum durung the French Revolution?",Palace,General,What was the name of Dion's Group,Belmonts,Music,"Who Did Jennifer Warnes Team Up With For The Hit ""Up where We Belong""",Joe Cocker +Food & Drink,"The Dish Of Rice, Flaked Fish And Hard Boiled Eggs Is Known As What ",Kedegree ,General,The Atlanta Braves baseball team has retired # 35 which used to belong to _____,Phil niekro,General,Hundred and ninety five litres what toy was originally made from the bladder of an animal,Balloon +General,Name the aboriginal detective in the novels of Arthur W Upfield,Napoleon Bonaparte,General,How long is a one year anniversary,Paper anniversary,General,What is the Canadian equivalent of the krugerrand,Maple leaf +General,Who is the supreme head of the Church Of England?,Queen Elizabeth II,Entertainment,"Who played the lead in the movie ""Braveheart""?",Mel Gibson,General,Which wine grape variety is nicknamed The King of Grapes,Cabernet Sauvignon +General,The Patella is commonly known as what?,Kneecap,General,What's Iceland's main industry,Fishing,Music,"Frank Zappa Launched 2 Labels Of His Own, Name One",Straight Or Bizarre +General,Lack of what vitamin causes pellagra,B3,General,Whose autobiography was entitled Past Imperfect,Joan Collins,Religion & Mythology,What religious movement was founded by William Booth?,Salvation Army +General,To which part of the body does the adjective 'pulmonary' refer,Lungs, Geography,What U.S. city is named after Saint Francis of Assisi?,San Francisco,General,Name Hong Kong Phooey's cat,Spot +General,"In Greek mythology, as which constellation did zeus place callisto",Ursa,Sports & Leisure,"As at August 2004, who is England's all time top goal scorer? ",Bobby Charlton ,General,"Who In The World Of Entertainment Has The Real First Name ""Clifford Joseph Price""",Goldie +General,The long term effect of the sun's radiation on the rotating earth's varied surface & atmosphere,Climate,Music,In Which Musical Does The Song Old Man River Feature?,Showboat,General,Days of the week - whats the only day named for a planet,Saturday +General,"What was the name of the bar that the characters from ""Three's Company"" frequented?",Regal Beagle,General,What longtime cleveland browns quarterback was shipped off to dallas in 1993,Bernie kosar,Music,Name 2 Celebrities Appearing On The Band On The Run Album With Wings,"Michael Parkinson, James Coburn, Clement Freud, Christopher Lee, John Conteh, Kenny Lynch" +Food & Drink,Who is `The Naked Chef'? ,Jamie Oliver ,General,The Simplon Tunnel runs between which two countries,Italy & switzerland,Science & Nature, The Bactrian camel is the only land mammal on Earth that can survive on __________,Salt water +General,Which company developed the Laser Printer,Cannon,General,What talent did Dumbo the elephant have?,He could fly,Science & Nature,Of what are walrus tusks made?,Ivory +Entertainment,Who was Dick Dastardley's pet,Muttley,General,On what river will you find Kew Gardens,Thames,General,What is the more usual name for blue beryl,Aquamarine +General,What does iron deficiency cause,Anaemia,Music,Who Had A Hit With Under The Bridge In 1998,All Saints,General,What name is given to the effect that the Earth is gradually becoming warmer,Global warming +General,Who was the first man to fly across the channel,Louis Bleriot,General,"Who wrote the lyrics for the song ""Chestnuts Roasting By An Open Fire""",Mel, History & Holidays,Who Eastenders Character Left Albert Square On Christmas Day 2005 ,Alfie Moon  +General,What are the busiest days for hospital emergency departments,Mondays,General,What literary work gave Geoffrey Chaucer something to do from 1387 to 1400,The canterbury tales canterbury tales,Music,"Which Now Famous Actress Did Bruce Springsteen Pull On Stage During His ""Dancing In The Dark"" Video",Courtney Cox +Food & Drink,Who released the following album 'Whipped cream and other delights ' ,Herb Albert and the Tijuana brass ,General,Jayne Austin had five brothers and one sister name her,Cassandra,General,Who invented 'bifocal' lenses for eyeglasses?,Benjamin Franklin +General,Highway 9 is the official name of what thoroughfare,Broadway New York,General,"In Halloween, what is Michael Myers' middle name?",Audrey, Geography,What is the capital of Kuwait ?,Kuwait City +Sports & Leisure,Who said if i had to choose between staying married & Snooker it would be snooker everytime ,Ray Reardon ,General,When were false eyelashes invented,1916,Geography,In which continent would you find the nile river ,Africa  +General,To which family of birds does the fieldfare belong,Thrush,General,Who played Commissioner Dreyfus in the Pink Panther films,Herbert lom,General,Peridot is the birthstone for ______,August +General,Who is the boss of UNCLE,Mr Waverley,Science & Nature,As what is sulphur also known?,Brimstone,General,In 1933 the U.S. formally recognized which country,Ussr +General,Which battle was fought by Wellington two days before Waterloo,Quatre bras,General,What is a Bunt a part of,Section of sail,General,Where is the world's largest library,Washington dc +Music,How Many Sisters Are There In The Group “Sister Sledge”,4,General,In which play and film does Jean Valjean appear,Le Miserables,Music,"Name The Two Members Of The Duo ""Tears For Fears""",Curt Smith & Roland Orzabal +General,Borborygmus is the medical name for what,Gas noises in gut,General,What was Max Headroom's network number,23,General,French word for jewel or trinket,Bijou +General,How many litres of air is in an adult lung,Five,General,What was the scorpions first lp called,Lonesome crow, Geography,Richmond is the capital of ______?,Virginia +General,We have heard of the Renaissance - what's it literally mean,Rebirth,General,When was Mount Everest first climbed,1953,General,"Who, in the Holy Bible, was the father of David",Jesse +General,In which American city can the Liberty Bell be found,Philadelphia,General,What is the more common name of the fruit the Chinese Gooseberry,Kiwi fruit,Sports & Leisure,What Nationality Is Tennis Player Gabriela Sabatini? ,Argentine  +General,Where was Napoleon born Ajaccio -,Corsican capitol,General,Which mountain peak is the highest in the Western Hemisphere,Aconcagua,General,If you Manuxorate what are you doing,Male masturbating with hand +General,With the development of which computer language was grace hopper associated,Cobol,General,What team won the Super Bowl XII,Dallas cowboys, Geography,Tegucigalpa is the capital of ______?,Honduras +Sports & Leisure,Which Tennis Player In 2007 Equalled Bjorn Borg's Record Of Five Consecutive Men's Wimbledon Titles? ,Roger Federer ,Entertainment,What musical instrument did Jack Benny play?,Violin,General,Where in Europe can you find wild monkeys,Gibraltar +General,"Who painted the picture, entitled Mares and Foals in a Landscape, in 1762",George stubbs,General,Coffee How long is a baby kangaroo at birth,One inch 1 inch,Music,"Who Sang ""I Want To Be Free"" In 1981",Toyah +General,In mythology Romulus and Remus were brought up by which animal,Wolf,General,Which actor is common to Magnificent 7 and Dirty Dozen,Charles Bronson,General,Gephydrophobia is a fear of___.,Bridges +General,"What product is sold with ""just for the taste of it""'",Diet coke,General,What does basf stand for,Baden aniline & soda factory,General,What is the flower that stands for: afterthought,Michaelmas daisy +General,Which famous talk show host made a guest apearence on Laverne & Shirley?,Jay Leno,General,What astronomical unit of distance is used for measurements beyond the solar system,Parsec,General,What is the Capital of: Ethiopia,Addis ababa + Geography,In which state is the Painted Desert?,Arizona,General,What fruit do viticulturists grow,Grapes,General,"Fill in the missing word - balloon____, gold ____, sword ____?",Fish +General,"Every plant in Tomorrowland at Disneyland in Anaheim, California, is edible. Plants in this section of the amusement park include __________, strawberries, tomatoes, and more. Guest are more than welcome to pick their fill. ",Bananas,General,The Locals Call It “The Goddess Mother” What Do We Know It As?,Mount Everest,Religion & Mythology,What was the first sign shown to Moses by God according to the Bible?,Burning bush +General,Who was proclaimed empress of India in 1877,Queen victoria, History & Holidays,After who was Mickey Mouse named?,Mickey Rooney,General,"During the American revolutionary war, what country declared war on Great Britain to help the colonies",Spain +General,Name the porceilan chair yoU.S.it on at least once a day,Toilet,General,"Betta Splendens, known for its labrynth gills that allow it to live in small plastic cups, it's long bright fins and aggression to males of the same species is also called the:",Siamese Fighting Fish,General,"What kind of rocks are basalt, granite and obsidian",Igneous +General,The amazing Spider Man' was one of the first comic books to have a story in which one of the supporting characters ____,Dies,Music,They started out as the Warlocks. By what name did they become world famous?,The Grateful Dead,General,"What links the trees Bodhi, Peepul and Ailento",They are all sacred to someone +General,In 1995 what was the most common name given to girls in USA,Ashley,General,Who wrote 'The Camomile Lawn',Mary wesley,General,Who painted - A Girl Asleep - The Letter - The Kitchen Maid,Jan Vermeer +General,Whose teachings are collected in the Hadith,Muhammad,Science & Nature,Which animal has the largest eyes?,Giant squid,General,What is Samsoe a type of,Cheese +General,What is the birthstone for august,Peridot,Music,What Is Elvis Presleys Middle Name,Aaron,Science & Nature,If Your Were Travelling at Mach 2 How Fast Would You Be Travelling ,2x 761mph is speed of sound  +General,Margie Belcher provided the body movements - which character,Disney Snow White,General,Lucy Johnson became famous under what name,Ava Gardner,General,"Wrought-iron tower in Paris, a landmark and an early example of wrought-iron construction on a gigantic scale",Eiffel tower +General,What common legal item literally means under penalty,Subpoena, History & Holidays,Where Was Thomas Becket Murdered? ,Canterbury Cathedral ,General,What was the real name of the children's TV character Bananaman?,Eric Wimp +General,Which imaginery line approximately follows the 180 degree meridian through the pacific ocean,International date line,General,A Japanese dance drama featuring stylized narrative choreographic movements.,Kabuki,General,Excrement of sea birds used as manure,Guano +General,In what TV series did we see Del Floria tailors shop,The man from UNCLE,General,Electronic device that allows the passage of current in only one direction,Diode,General,What are the main ingredients of the Irish dish 'colcannon',Green cabbage and potatoes +General,14% of Americans could not identify which country on a map,America,General,Triskaidekaphobia is a fear of ______,Number thirteen,General,How was hamida djandoubi executed,Guillotine +Geography,The first country along the great circle route due south from San Francisco,Canada,Music,By what name is Antonin Dvorak's ninth symphony known?,From The New World,General,In what country was the espresso machine invented in 1822,France +General,Where would you find the Ponte de Sospiri,Venice - Bridge of Sighs,Music,"The All Saint's Hit ""Pure Shores"" Was Featured On The Soundtrack To Which Movie",The Beach,General,What band recorded the 1990'2 hit 'slide',Goo goo dolls +Art & Literature,What Is The Most Performed Opera In The UK ,La Boheme ,General,What one word fits ____stream; ____hill; _____pour,Down,General,Until when was california a part of mexico,1846 +Music,"Which Band Relaased The Albums “Bleach, Nevermind & The Muddy Banks Of Wishkah”",Nirvana,Sports & Leisure,Where were the 1900 Olympics held ?,"Paris, France",Entertainment,The first 18 minutes of this movie is black and white.,Wizard of Oz +General,What was the name of the helicopter service that was the cover for Airwolf?,Santini Air,Geography,In Which Country Will You Find The Bay Of Pigs? ,Cuba ,General,In airline slang what is a 365,Eggs Bacon served any day or time +General,A short thick post used for securing ropes on a quay,Bollard,General,"Punt, coracle and kayaks are all types of what",Boat,General,What cartoon character was 5 foot 6 inches tall,Popeye +General,"At the Montreal Olympics, Nelli Kim was judged to have given a perfect performance in the floor exercise and which other discipline",Vault,General,Who was the japanese detective in novels by john phillips marquand,Mr moto,General,What do Stacey Keach and Oscar Wilde have in common,Reading Jail +General,Lepidoptera (from the Greek) literally means what,Scaly Winged,General,Portugal has had six Kings with what first name,John,General,"On the fahrenheit scale, there are 180 degrees between freezing point and ______",Boiling point +General,November 18th is who's birthday,Mickey Mouse,General,Which novel has the longest sentence in literature 823 words,Les Miserables – Victor Hugo,General,"Which substance causes milk to curdle, and is used to make cheese",Rennet +General,Which vegetable has the highest sugar content,Onion,General,What was the language if ancient Rome,Latin,General,Who was the first athlete to hit a major league home run and make a professional football touchdown in the same week,Jim Thorpe +General,Which Country Was The First To Win The Eurovision Song Contest In 1956,Switzerland,General,Who sang 'have you ever seen the rain',Creedence clearwater revival,General,Which was the first apostle to be stoned to death,Stephen +General,"In Star Trek, what is Mr Spock's pulse rate?",242 Beats Per Minute,General,Which American state is nicknamed 'The Sioux State' or 'Flickertail State',North dakota,Sports & Leisure,Which Shooting Season Begins On (The Glorious Twelth) ,Grouse Shooting  +General,In Strongville Ohio what book is banned by law,Catch 22,General,What country did the USA defend in the Spanish American war,Cuba, History & Holidays,At which battle was the Charge Of The Light Brigade? ,Balaclava  +Food & Drink,What Food Do You Use Up More Calories Eating Than You Gain Through Consumption? ,Celery ,General,What country was ruled by the Schleswig-Holstein dynasty,Greece,General,What is the fear of brain disease known as,Meningitophobia +General,What Do We Call A Squirrels Home,A Drey,General,What are a chessboard's vertical rows called?,Files,Music,What Was Rod Stewarts 2nd Number One After Maggie May,You Wear It Well + History & Holidays,When Cook Discovered The Hawaiian Islands What Did He Name Them? ,Sandwich Islands , Language,What does 'AOL' stand for?,America Online,General,When was chewing gum patented,1869 +General,What fictional archaeologist trained under Prof Abner Ravenwood,Indiana Jones,General,Ball and ______,Chain,General,What neighbouring country did Iraq go to war with in 1980,Iran +General,"What was broadcast causing U.S. radio listeners, to flee for their lives fearing an attack by aliens",The war of the worlds,General,Who was the son of Poseidon and Ampherite,Triton,General,What English king was killed with a red hot poker up his arse,Edward 2nd +General,What is the largest city in africa,Cairo,General,What country did Germany invade on September 1st 1939,Poland,General,"What is the region loosely defined by geography and culture, located in southwestern Asia and northeastern Africa",Middle East +Science & Nature," __________ are freeze_tolerant and spend winters frozen on land, only to thaw in the spring and begin their breeding process in vernal ponds.",Wood frogs, History & Holidays,"What Was Introduced Into The UK First, The Christmas Card Or The Christmas Tree ",Christmas Cards ,General,How many sheets of paper are there in a ream,500 +General,Who did Jimmy Carter defeat to win the Presidency of the USA,Gerald ford,Science & Nature,Of What Is Fe The Chemical Notation ,Iron ,General,"Whats the meaning of the Latin battle cry 'ad arma, ad arma",To arms to arms +Science & Nature,What Vaccine Did Edward Jenner Develop ,The Smallpox Vaccine ,General,Who was the female star of the film 'Barefoot in the Park',Jane fonda,Food & Drink,"Rum, lime, and cola drink make up which type of cocktail ",Cuba Libre  +Tech & Video Games,Which legendary games designer/producer created the Super Mario Brothers franchise for Nintendo?,Shigeru Miyamoto,General,What sequence is this the start of: 1 3 12 60 360 2520,Even permutations,Music,Who Is Mark King The Lead Singer With,Level 42 +General,Porphyrophobia is the fear of,The color purple,General,What is the fear of the opposite sex known as,Sexophobia,Music,How Was Norma Dolores Egstrom Better Known,Peggy Lee +General,French artist Edward Degas noted for what particular subject,Ballet Dancers,Sports & Leisure,"Sir Paul Fox created which programme, that is shown on the BBC every year? ",Sports Personality Of The Year ,General,Name the two movies that Michael Crichton made (before Jurrasic Park )about a theme park out of control.,"""West World"" and ""Future World""" +General,Which novel by Michael Crichton was the number one best-selling paperback in 1993,Jurassic park,People & Places,Who Said Of Touring Pakistan 'I Wouldn't Even Send My Mother In Law There' ,Ian Botham ,General,"In The Movie ""Slumdog Millionaire"" 'Jemal' Was The Name Of The Main Character But What Was The Name Of His Older Brother",Salim +General,"Who averaged receiving 21,000 letters a day in 1993",Bill clinton,General,On which Saint's day was the battle of Agincourt fought in 1415,St crispin,General,"Football Team, Denver ____",Broncos +Sports & Leisure,What's the highest possible finishing score in a game of darts ,167 , History & Holidays,Which James Bond Film Featured A Bond Girl Called Doctor Christmas Jones ,The World Is Not Enough , History & Holidays,The Eldest Sons Of The Kings Of Which Country Had The Title (Dauphin)? ,France  +Music,Which American state featured in the title of a 1971 hit for Olivia Newton John,Ohio (Banks Of The Ohio),General,What name is given to the Jewish candlestick with special religious meaning? ,Menorah,General,Who was an ordained priest in the Church of England,Sir isaac newton +General,"When olive oil is described as 'extra virgin', what has happened to it to make it so",First pressing of the olive,General,At what theme park are the Looney Toons associated with,Six flags,General,Grace Robin was the first model - to model what in 1930,Contact Lenses +General,46% of women say this is better than sex - what,A good nights sleep,General,What is the widest-ranging ocean bird,Albatross, History & Holidays,"What was the third country to get the ""bomb""?",Britain +Music,In Which Year Was Saturday Night Fever Released,1978,Science & Nature, The pupil of an octopus's eye is __________,Rectangular,General,Who was america's first public enemy no 1,John dillinger +General,C17 H21 N04 is the chemical formula for what,Cocaine,Geography,What river is known as China's Sorrow,Yellow,General,A bear is ursine - what bird is pavonine,Peacock +Music,Who Wrote The Lyrics To The Songs In West Side Story,Stephen Sondeim,Music,The Living Years Was A Hit For Which Band,Mike And The Mechanics,General,Who wrote Whip Hand Proof and Flying Finish,Dick Francis +Food & Drink,Which Country Consumes The Most Cornflakes ,Ireland (Per Head) ,General,Mare Nostrum was the Roman name for what,Mediterranean Sea (Our Sea),General,Whose first release was 'talking heads 77',Psycho killer +General,Harry Patterson is the real name of which author,Jack Higgins,General,What is the Capital of: Bhutan,Thimphu, History & Holidays,"This military attack took place on Dec. 7, 1941.",Pearl harbour +Music,Who Claimed To Be The Wild One,Bobby Rydell,Sports & Leisure,Which Famous Sporting Team First Began In 1926 When They Were Known As The Savoy Big Five? ,The Harlem Globetrotters ,General,Who sang about Saturday Night at the Movies,The Drifters +General,Old Lyme Connecticut has a museum dedicated to what,Nuts,General,Name bar John Wilkes Booth got pissed in before killing Lincoln,Star Saloon,Geography,In which country is the Blarney Stone? ,Ireland (or Eire) at Blarney Castle near Cork  +General,What city hosted the 1936 summer olympics,Berlin,General,What county has a national dog (only one country has one),Netherlands,General,What Queen of Ogygia detained Odysseus for seven years,Calypso +General,What do you call the act of putting a word inside another (ie: abso bloody lutely.),Tmesis,Music,In Which Year Were The Housemartins Having A Happy Hour,1986,General,"In Greek mythology, who was the only mortal gorgon",Medusa +General,What presidential ticket was dubbed bozo and the pineapple,Gerald ford and, Geography,What is the capital of Jamaica ?,Kingston,General,"Whose last words were - ""Clito I owe a cock to Asclepius""",Socrates +General,A tool with screw point for boring holes in wood,Auger,General,What is the great mass of stone trees in the Painted Desert in Arizona called,The petrified forest,General,"The Sicilian, French and Alekhines are all used in which game",Chess +General,Who is on a U.S. $20 bill,Andrew jackson,General,Lucille Langhanke born 1906 won an Oscar as who in 1941,Mary Astor,Music,With What Name Was Duke Ellington Christened,Edward Kennedy Ellington +General,The Fields Medal is equal to a Nobel prize in what area,Mathematics, History & Holidays,What type of natural disaster hit the American Midwest in 1935? ,Dust Storms ,General,The scale used to measure the magnitude of earthquakes,Richter +General,"Clarissa Luard, Mariane Wiggins & Elizabeth West. Have All Been Married To Which Famous Author ?",Salman Rushdie,General,Who wrote the book Gremlins in 1943 - later filmed,Roald Dahl,General,Which American state is nicknamed the 'Beehive' state,Utah +General,Who won the best actor award for Marty in 1955,Ernest Borgnine,General,Who wrote the novel brighton rock,Graham greene,General,In 1954 George Cowling was the first British TV what,Weatherman +General,Led Deighton trilogy Game Set Match What 3 Capitals,Berlin MexicoLondon, History & Holidays,What did Victorian women bathe in to try to enlarge their breasts?,Strawberries,General,What is the birthplace (city) of the late John Candy,Toronto + Geography,What continent is Cyprus considered to be part of?,Asia, History & Holidays,From what did Alexander the Great suffer?,Epilepsy,General,What was signed on 15th June 1215?,The Magna Carta +Music, Who had a Hit in 1958 with the Song 'Hoots Man',Lord Rockingham's XI,General,The space occupied by a body is called its ______,Volume,General,In a year the average person walks four miles doing what,Making their bed +General,The Perils of Penelope and Dastardly and Mutley spin offs what,Wacky Races,General,What domestic cat enjoys swimming,Angora,General,Fault What Italian city had the Roman name Mediolanum,Milan +General,A mustelidae family member is a(n)___.,Weasel,Science & Nature,What Parasitic Infection Is The Most Wide-Spread ,Malaria , History & Holidays,What was the name of the balloon three americans piloted across the atlantic in 1978 ,The double eagle ii  +General,What were the two forenames of dramatist and novelist J.M. Barrie,James matthew,General,How many seats did the Scottish National Party win at the 2015 General Election?,56,General,What animal can sleep 3 years but only mates once - 12 hours,Snails +General,What is the flower that stands for: rural happiness,Yellow violet,General,What keeps one from crying when peeling onions?,Chewing gum,Art & Literature,"In 'Alice In Wonderland', who never stopped sobbing?",Mock Turtle +General,What Group Performed On The First CD To Sell Over A Million Copies Worldwide,Dire Straits,General,Amundsen reached the South Pole in which year,1911,Sports & Leisure,In which city is the Cotton Bowl played?,Dallas +General,Urea is only found in humans and what other animal,Dalmatian Dogs, History & Holidays,Who Played The Title Role In The 1965 British Film The Nanny ,Bette Davis ,Art & Literature,Which American Author Wrote Jaws ,Peter Benchley  +General,What derivative of cocaine is often used in Dental surgeries as an anaesthetic?,Lidocaine,General,In Which Country Was Greenpeace Founded In 1971,Canada,General,As who is Terry Bollea known,Hulk Hogan +Geography,On Which Side Of The Road Do They Drive In India ,The Left ,General,What did Barbie do in 1977,Smile,General,"Which particles, when emitted by radioactive nuclei, are known as beta particles",Electrons or positrons +Music,Mark Knopfler Was A Member Of Which Band,Dire Straits,General,What is the square root of zero,Zero 0,General,Who composed the opera Lakme,Delibes +General,What Japanese city was the scene of a devastating earthquake in 1994,Kobe,Sports & Leisure,Who won the Ladies singles title at Wimbledon in 2004? ,Maria Sharapova ,General,What is the medical term for short sightedness,Myopia +General,What distinguished the 9th and 10th Cavalry,All Black Regiments,General,Branch of mathematics using letters to represent numbers,Algebra,General,Which letter of the alphabet is used in cameras to describe the aperture setting?,F +Music,Guess The Band From These Initials MO / HD / RW / JO / GB,Take That,General,What is agoraphobia,Fear of open spaces,General,Minerals that are treasured for their beauty & durability,Gemstones +General,"What islands got their name from the Spanish 'baja mar', meaning 'shallow water'",Bahamas,General,Who was born on the island of korcula,Marco polo,Science & Nature,What Is The Cos A Type Of? ,Lettuce  +Music,"Who Began The Decade Riding In A ""Big Yellow Taxi""",Joni Mitchell,General,What note sounds at 261.6 hertz,Middle c,General,What famous religious hymn by Augustus Montague Toplady,Rock of Ages +General,"Which ex-Neighbours star released the No.2 hit single ""Tom"" in October 1997",Natalie imbruglia,General,When is the shortest day in the northern hemisphere,December, Geography,Which country's ships fly under the Union Jack ?,Great Britain +Geography,Which city is furthest north in terms of its line of latitude? ,Rome ,Science & Nature,Which Common Flower Has The Scientific Name Bellis Perennis & Has A Connection Via A Song With A Bicycle ,Daisy ,General,Who was the first gymnast to score a perfect 10 in Olympics,Nadia Comaneci +General,"According to the title of a famous novel, there are how many 'Years of Solitude",100,Food & Drink,What type of pulses are used in humous? ,Chick Peas ,General,What is the inscription on the squadron badge of 617 Squadron,Apres moi le deluge +General,Who wrote The Hitchhikers Guide to the Galaxy,Douglas Adams,Science & Nature,Where Does The Arctic Tern Migrate To? ,The Antartic ,General,40000 Americans are injured each year where,In the toilet +General,"Milk, cheese and meat are good sources of which nutrient needed for a healthy diet",Protein,Food & Drink,Which herb is used to flavour Pernod? ,Anise,General,What traditionally happened on Ash Wednesday? ,People put ash on their foreheads  +Food & Drink,How would you serve Gezpacho Soup ,Cold ,General,What was Marilyn Munroes original last name,Mortenson,General,Which Soviet Republic Was the first To Declare Independence from Moscow In 1991?,Lithuania +General,8% of people in the world have an extra what,Rib,General,The Bank of Italy changed its name to what,The Bank of America,General,Reginald Carey became famous as who,Rex Harrison +General,Sport variable ground size 120x150yd min 170x200 max,Aussie rules football,General,53 is the international dialling code for what country,Cuba,General,What was the first credit card called?,Diner´s Club +General,Who played the stepmother in 'my stepmother is an alien',Kim bassinger,Toys & Games,Term for any number between 19 & 36 in Roulette,Passe,General, The study of light and its relation to sight is called ________.,Optics +General,Which Films Theme Was The Biggest Selling Song Of The Year 1979,Bright Eyes,General,"In ballet, a gliding step which usually connects two steps.",Glissade,General,Elaine Bookbinder became more famous as who,Elkie Brooks +General,In which European city is Templehof airport,Berlin,General,In Youngstown Ohio it's illegal to run out of what,Gas or petrol,General,Who won the Best Actress Oscar in 1987 for her role in Moonstruck,Cher +Music,"In 1975 Who Discovered To His Horror That His New Single ""Honky Tonk Angels"" Was About Prostitutes And Promptly Banned The Record Himself",Cliff Richard,General,Russian blue and Turkish brown are types of what,Cats,General,"Name the female version of the Jewish Bar Mitzvah, celebrating a girl reaching the age of thirteen",Bat mitzvah +General,Where is nuuk,Greenland,General,Which stretch of water separates Denmark from Sweden,Kattegat,General,Fritz Von Werra was the only German pilot WW2 to do what Escape,The one that got away +General,Who was the author of 'dracula',Bram stoker,Toys & Games,This term denotes a chess move in which both the king and the rook are moved.,Castling,General,16th century husband had to stop doing what to wives after 10pm,Beating Them + History & Holidays,What cult-fav eighties movie features John Lithgow from another dimension? ,The Adventures of Buckaroo Banzai ,Geography,What is the basic unit of currency for United States,Dollar,General,"Which Italian painter noted for ""red"" canvases died in Venice of the plague in 1576, aged about 99",Titian +General,Caruso put what in Nellie Melbas hand singing tiny hand frozen,Hot Sausage,Music,With Which Renowned American Composer Did Elvis Costello Collaborate On The 1998 Album Painted From Memory,Burt Bacharach,General,Who set a world water speed record over 70 mph at age 72,Alexander Graham Bell + History & Holidays,Who is known for his 'theory of evolution'?,Charles Darwin,Geography,Which City Is The Capital Of Switzerland ,Berne ,General,Name the person who caused Chicago kids to get school milk,Al Capone +General,The cultivation of grapes,Viticulture,Music,What Are The First Names Of The Eurovision Winners Bucks Fizz? (PFE),"Cheryl, Mike, Bobby & Jay",General,What was stolen from a Hotel Garden in Britain in 1991,Onion Crop +People & Places,Whose Real Name Is 'Gordon Sumner' ,Sting ,General,Which two books in the old testament list the ten commandments,Exodus and,General,Who wrote Three Men in a Boat,Jerome K Jerome +Science & Nature,What Is A Kumquat? ,A Small Japanese Variety Of Orange ,Music,"Who Had A Hit In 1994 With ""Wonderman""",Right Said Fred,General,"Who said ""Tha tha that's all folks""?",Porky Pig +General,What do astronomers call the red sky before sunrise,Aurora,Music,"Who Had A 1985 Hit With ""Fever""?",Peggy Lee,General,"Which forename, deriving from the Germanic 'rulehard', has been held by three English kings",Richard +General,Grunge music originated in which American city,Seattle,General,"His hit, Calendar Girl, features the lyrics ""April-You're The Easter Bunny When YoU.S.mile""",Neil Sedaka,General,What Is Nosocomephobia The Fear Of,Hospitals + Geography,Bissau is the capital of ______?,Guinea-Bissau,General,In Phoenix Arizona you cant walk through a hotel wearing what,Spurs,General,Where does the annual poker world series take place,Las vegas +General,In which county is Corfe Castle,Dorset,Entertainment,What was the average age of United States soldiers in the Vietnam war?,Nineteen,General,The 1st personal computer went on sale in what year,1977 +General,John McEnroe won Wimbledon doubles with what partner,Peter Fleming,General,Ankara is the capital of ______,Turkey,Geography,What Tourist Attraction Is Wearing Away At A Rate Of 5 Feet Per Year ,Niagra Falls  +General,In which play did Shakespeare write a whole scene entirely in French,Henry v,General,U.S. Captials - Indiana,Indianapolis,General,What is the flower that stands for: aversion,Indian single pink +General,What was the first product to have a bar code on it?,Wrigley's gum,Art & Literature,Who Painted The Creation Of Adam ,Michelangelo ,General,What is a cancerous tumour,Malignant +General,Who played alexis carrington in the tv series 'dynasty',Joan collins,Food & Drink,How Did Caprese Salad Get It's Name ,It Came From The Island Of Capri,Geography,"The Federated States of ___________, located at the Eastern Caroline Islands in the northwest Pacific Ocean, has more than 600 islands and 40 volcanos.",Micronesia +Sports & Leisure,Which swimming stroke is named after an insect?,Butterfly,General,"What did jim morrison do on march 1, 1969",Exposed himself get naked on,Science & Nature,What is the meaning of the name of the constellation Ursa Minor ?,Little Bear +General,Who wrote the song 'Anything Goes',Cole porter,General,What was mussogorsky's profession,Composer,Science & Nature," The basenji is a mid_sized dog with a silky copper coat. Although they are considered a barkless dog, they are known to __________ when they are happy.",Yodel +Food & Drink,"What name is given to a savoury of oysters wrapped in bacon slices, served on toast? ",Angels on hoseback ,General,What russian emigre to the us is credited with inventing the helicopter,Igor,General,What is the fear of suffering and disease known as,Panthophobia +General,"Italy Schiaffetoni, Rosetti and Crusetti in Sicily what pasta type",Cannelloni,General,Who were the group Hue and Cry looking for,Linda,General,The bridge connecting Boston & Cambridge via Massachusetts Avenue is commonly know as the ______,Harvard bridge +General,In Hazledon Pen a lecturer can't legally do what while working,Sip Carbonated Drinks,Science & Nature,What Is The Function Of The Pituitary Gland ,It Controls The Production Of Hormones ,Science & Nature, The chameleon has a tongue that is 1.5 times the length of its __________,Body +General,"Before coming a full time author, what was the profession of Frederick Forsyth",Journalist,General,The vaccine MMR offers protection against which diseases,Measles mumps and rubella,General,What element does the symbol 'at' represent,Astatine +General,In the Bible where was Jesus when he ascended into heaven,Bethany,Music,"Who Had A Hit With ""Heartbreaker"" In 1982",Dionne Warwick,Science & Nature,These limestone deposits rise from the floor of caves.,Stalagmites +General,What does cpr stand for,Cardiopulmonary resuscitation,General,In what country would you find New Brunswick,Canada,Entertainment,What was the first network series devoted entirely to rock and roll?,American Bandstand +General,Which 1956 film caused riots in cinemas,Rock around the clock,General,The first telephone exchange was opened in the U.S. in what year,1878,General,The 25th U.S. president always wore a red carnation. He was _______ ______,William mckinley +General,What Was Michael O'Briens Claim To Fame,First Ever British Streaker,General,In WW2 Air corps non flying members given what nickname,Kiwis - Non Flying,Science & Nature,Sydneys Main Airport Is Named After Which Great Australian Aviator ,Sir Charles Kingsford Smith  +General,A booklet containing descriptive information,Brochure,General,What Food Do You Use Up More Calories Eating Than You Gain Through Consumption,Celery,General,When was the cathode ray tube invented,1878 +General,What is the capitol of the United Arab Emirates,Abu Dhabi,General,Who gave the u.s.a the statue of liberty,France, History & Holidays,Who Released The 70's Album Entitled Catch a Fire ,Bob Marley and the Wailers  +General,Syngenesophobia is the fear of what,Your relatives,Music,What instrument did bob dylan play in his recording debut,Harmonica,Science & Nature,What Is The Most Common Element In The Universe ,Hydrogen  + History & Holidays,"The General Strike, Called In 1926, Supported Which Union? ",Mine Workers Union ,General,Who wrote 'the joy of sex',Alex comfort,General,What was the name of the party dog that that was Budwiser's mascot in the late eighties?,Spuds McKenzie +General,"During which war did the term ""Fifth Column"" originate",Spanish civil war,Toys & Games,In poker five cards of the same suit is called a(n) __________.,Flush,General,What is the name in a planet's orbit when it is nearest to the sun,Perihelion +General,What pope died 33 days after his election,John paul i,General,Metallophobia is the fear of,Metal,General,Which Athenian philosopher wrote nothing - immortalised by Plato,Socrates +Sports & Leisure,This traditional Japanese wrestling sport takes place in a circular ring.,Sumo,General,What is the nickname for Maine,Pine tree state,General,When is the shortest day in the southern hemisphere,June +General,In 1927 what ceased to be a weapon in the British Army,The Lance,General,What vegetable is found in the dish chicken divan,Broccoli,General,Six copies of what are stored under bell jars in Serves France,The Kilogram in platinum iridium +Entertainment,Which famous male actor made his name in 'I Dream Of Jeannie'?,Larry Hagman,General,"Colorless, corrosive liquid that has the chemical formula HNO3",Nitric Acid,General,What color are an albino elephant's toenails,White +General,As what are male bees also known?,Drones,General,Who won the 1982 soccer world cup,Italy,General,What is the Capital of: Sweden,Stockholm +General,What are garbanzo beans also known as,Chick peas,General,Where would you find your Coxa,Hip Joint,Music,The Big Bopper and Ritchie Valens died in a plane crash with which rock and roll legend,Buddy Holly +General,A small village without a church,Hamlet,General,What russian leader was killed with an icepick?,Trotsky,General,What job does up & down do for a living,Elevator operator +General,Who was the first woman golfer to earn a million dollars,Kathy witworth,General,What was the location for the first Winter Olympics in 1924,Chamonix,General,What animal can smell a virgin same type from 1.8 miles away,Gypsy Moth +General,What did the Romans call the tenth part of a legion - between 300 and 600 men,A cohort,General,A smurf is this tall,3 apples,General,What is rayon made from,Wood pulp +Music,In Which Film Did Fred Astaire & Ginger Rogers Team Up,Flying Down To Rio,Geography,Which Country Produces The Most Pears Each Year ,China ,General,Which is the longest river system in Australia,The murray-darling +General,"The ""final solution"" dealt with what people",Jews, Geography,Bamako is the capital of ______?,Mali,General,Michael Jackson sing this song in 1987,Smooth criminal +General,"Shovelhead, Knucklehead, Panhead types of what",Harley Davidson,General,Telesphobia is a fear of what,Being Last,General, A person with a strong desire to steal is a(n) ________.,Kleptomaniac +Sports & Leisure,Who was the first woman to compete in the World Snooker Champion? ,Alison Fisher ,General,Who played ashley wilkes in gone with the wind,Leslie howard, Geography,What is the capital of Nebraska,Lincoln +General,Tennis - who won the us open in 1989,Boris becker,Music,"Who Had A Hit In 1988 With ""Good Life""",Inner City,Art & Literature,Which book featured the miser Scrooge?,A Christmas Carol +General,In which U.S. state would you find Tulsa,Oklahoma,General,What was the first cd pressed in the u.s,Born in the u.s.a,Sports & Leisure,Who was the first manager from outside the British Isles to win the English FA Cup? ,Ruud Gullit  +General,Trick what country suffered the most combat deaths in world war ii,Soviet union,General,What has a palimped got,Webbed Feet,General,"In 1968, who released 'carnival of life' and 'recital'",Lee michaels +General,What has 12000 eyes,A Butterfly,General,Debussy how tall was the shortest british monarch charles i,"4'9""",General,"The indoor theatre at the new Globe, being built in London, will be named after which historical person",Inigo jones +Science & Nature,What are the larvae of flies called ,Maggots ,General,Which of the Greek islands is closest to Turkey?,Rhodes, History & Holidays,What style of dancing was popularized with rap music? ,Break Dancing  +General,Amaxophobia is the fear of what,Riding in a vehicle,General,"Chaconne, Rigadoon, Passepied are all types of what",Old style dances,General,"The longest suspension bridge in the world is the Akashi Kaikyo, located where",Japan +General,Soccer new york ______,Cosmos,General,If you are on the Choke mountains what country are you in,Ethiopia,General,Aleksei Leanov was the first to do what,Space walk +Religion & Mythology,"In Greek mythology, the riddle of what did Oedipus solve?",Sphinx, Geography,What is the capital of the United Arab Emirates?,Abu Dhabi,Food & Drink,"Not to be confused with spatchcock, which creature might be split and grilled or fried in a manner known as spitchcock? ",Eel  +General,The Madeira islands lie in which ocean,Atlantic,Music,"Which Of Madonna's Albums Featured A Duet With Prince Called ""Love Song""",Like A Prayer,General,"In Shakespeare's play, who tamed the shrew",Petruchio +General,"In Christian churches, the universal rite of initiation, performed with water.",Baptism,Religion & Mythology,Who is the greek equivalent of the roman god Mercury ?,Hermes,General,Which Singer Released Their 4th Album Entitled “ This Time ” In 2007,Melanie C +General,Cavendish is the family name for which Duke,Devonshire,General,What happened in Britain Sept 3rd 1752,Nothing - day never existed,General,What do the initials U.F.O stand for?,Unidentified Flying Object +General,Who played 'Cricket Blake' in the 1960s T.V. series Hawaiian Eye,Connie stevens,General,"Who is known as the ""George Washington"" of South America",Simon bolivar, History & Holidays,Who Was The Youngest British Prime Minister? ,William Pitt (The Younger)  +General,In astronomy what are rapidly rotating neutron stars called,Pulsars,General,"Who composed ""Messiah""",Handel,General,What is the flower that stands for: stoicism,Box tree +General,Under My Wheels' and 'Be My Lover' were cuts of whose 1971 'Killer' release,Alice Cooper,General,One of the worst fires in American history gutted the twenty-six storey MGM Grand Hotel in 1988. In which city was the hotel situated,Las vegas,People & Places,Who Is Married To Tatum O' Neil ,John Mcenroe  +General,Which American city was named after a British Prime Minister,Pittsburgh,General,"What are or were The Adena, Cayuga, Haida and Nootka",North American native Indian tribes,General,In Kansas by law you cannot drive what down the street,Buffalo +General,A Chinese eunuch invented what in the second century,Paper,General,Where would you find the titmus test,ICC cricket test for bowling action,General,A very tall center and a real ladies man,Wilt the stilt +General,Who wrote the Paris and Prague symphonies,Mozart,General,"A slender, lofty tower with balconies, attached to a Muslim mosque.",Minaret,General,What is the flower that stands for: conjugal love,Lime +General,Which western writer created Hopalong Cassidy,Louis L'Amour,Science & Nature,What Is The Longest Side Of A Right Angled Triangle Called ,It's Hypotenuse ,Science & Nature, Baby rattlesnakes are born in August and __________,September +General,Which singer is known as the 'Walrus of Love',Barry white,Mathematics,Who proved Fermat's Last Theorem ?,Andrew Wiles, History & Holidays,During Which War Did The Charge Of The Light Brigade Occur? ,Crimean  +Science & Nature,Which Is The Smallest Living Relative Of The Dinosaur ,The Bee Hummingbird ,General,What symbol appeared in green on white flags flown by U.S. relief ships during the Irish potato famine,Shamrock,Sports & Leisure,Which Chess Piece Always Remains On The Same Colour Square ,Tthe Bishop  +General,The dollar was established as the official currency of the U.S. in what year,1785,Science & Nature, The lungfish can live out of water in a state of suspended animation for __________,3 years,General,Jocasta was the wife of Laius and the mother of who,Oedipus +General,"On irc, how do you ask age, sex, location",A/s/l,General,The term Tercentennial represents how many years ?,300,Entertainment,"Gadzookie has a large, green friend. Who is he?",Godzilla +General,War heroine Violette Bushell was better known by her married surname - what is it,Szabo,General,Small country has more 1000 dialects and two official languages,Philippines,General,The Sweater Shop International was a competition which sport,Snooker +General,Medical treatment involving needles,Acupuncture,General,What body of water separates Australia and Papua New Guinea,Torres Strait,General,What is the flower that stands for: blushes,Marjoram +General,Energy waves produced by the oscillation or acceleration of an electric charge. Electromagnetic waves have both electric and magnetic components.,Electromagnetic radiation,General,Japan what is the capital of kenya,Nairobi,General,What does the Fleetwood Mac inspired plaque on Bill Clinton's desk read,Don't stop thinking about tomorrow +General,Epitaxis Is The Correct Medical Term For Which Common Condition,Nose Bleed,General,"Republic in southeastern Europe, bounded on the north by Austria, on the northeast by Hungary, on the south by Croatia, and on the west by Italy.",Slovenia,General,Who was hanged & decapitated two years after he had died,Oliver cromwell +General,What did scientists build in a squash court under a football stadium at the university of Chicago in 1942,Nuclear reactor,General,"Who said, ""the best way to resist temptation is to yield to it""",Oscar wilde,General,"On an Indian menu, Aloo is what type of food?",Potato +General,Glossitis is inflammation of which part of the body?,Tongue,General,Which television island gave well-heeled guests the chance to live out their dreams in the 1970's,Fantasy island,Music,What was the first single released on the Apple label?,"Hey Jude""/Revolution" +General,Who said I've have take more out of alcohol that it has out of me,Sir Winston Churchill, Geography,Where is the tallest building in the world?,"Kuala Lumpur, Malaysia",General,As close as two ______ in a pod,Peas +General,Whats the oldest brewery in continuous operation in North America,Molson,Science & Nature,Which planet is known as the red planet ?,Mars,General,Dainty or cute to an affected degree,Cutesy +General,Name the Indian version of Barbie,Monica,General,Which motor company produces the Alhambra,Seat,General,Marie Stopes discovered what in British Museum after marriage,Should not be virgin +General,Who wrote the music for the ballets Firebird and Rites of Spring,Igor Stravinsky,General,"What mouseketeer's first hit song was ""tall paul""?",Anette funicello,General,Literal translation what Persian word is leg garment,Panamas +Science & Nature,Scotch Tape Was Invented In 1930 By American Richard Drew For Which Company Did He Work? ,3M ,Food & Drink,From Which Country Does Bulls Blood Originate ,Hungary ,Science & Nature,These marine crustaceans often attach themselves to the hulls of ships.,Barnacle +General,Ovine refers to what kind of animal,Sheep,General,In the Chinese New Year what year follows Rat,Ox,Science & Nature," The maximum life span of __________ has been documented to be over 200 years in exceptional cases. The average life span of the large colorful fish, however, is 25 to 35 years.",Koi +General,What is a group of quail,Covey,Entertainment,In the cartoons who was Hokie Wolf's sidekick,Ding,General,What financial item was introduced to UK in September 1963,American Express card +General,In London what would you find at 87-135 Brompton Road,Harrods,General,"Which star, the brightest in the constellation Taurus, is known as the 'Eye of Taurus'?",Aldebaran,Art & Literature,"This subject is covered in the magazine ""Bondage"" (two words)",James bond +General,Which meat do Hindus not eat,Beef,Geography,Into Which Sea Does The river Danube Flow ,The Black Sea ,Science & Nature,What is the sixth planet from our sun?,Saturn +General,What body parts are oversized in a man suffering from gynecomastia,Breasts,Music,"Who Recorded The Album ""The Dream Of The Blue Turtles""",Sting,General,How is a zither played,Plucked +General,In the UK sport of Kings what is significant about the number 18,Max letters in a racehorses name,Music,"Following His Honorary Knighthood, What Letters Is Bob Geldof Entitled To Put After His Name",KBE,General,What union did duran duran sing of in 1983,Union of the snake +General,"Who was the author of ""The Tin Drum""",Gunther grass,Geography,What Is The Largest Island In The World ,Greenland ,General,In which country is the world's largest National Park?,Greenland +General,Who created the 'grinch',Dr seuss,General,Name the group that organist barry andrews left xtc to join,League of,Music,"Who Sung Their Own Theme Before Moving On To A ""Superfly Guy""",S Express +Sports & Leisure,Which Football Club Play Their Home Games At The Walkers Stadium ,Leicester City ,General,What is the fear of tuberculosis known as,Phthisiophobia,General,"Who said 'there, i guess king george can read that'",John hancock +Science & Nature,In Which Country Are The Worlds Deepest Mines ,"South Africa, Near Carletonville ; They Are 3,777m Deep ",Geography,The city of Los Angeles is more than one_third the size of the entire state of ______________,Rhode island,Food & Drink,What is the flavour of the liqueur Kahlua? ,Coffee  +Music,"Who Wrote The Song ""Manic Monday"" For The Bangles",Prince,General,What is a zinfandel,White grape variety,General,Which fictional city is Superman's home,Metropolis +Entertainment,What actress has received the most Oscar nominations?,Katherine Hepburn,General,The term Bicentennial represents how many years ?,200,Geography,What is the capital of Latvia,Riga +General,King Mongut had aprox 9000 wife's/concubines what country,Siam - Thailand,Entertainment,Who sang about 'The Boogie Woogie Bugle Boy Of Company B'?,The Andrews Sisters,General,Who starred in the 'hard to kill' series of films,Steven segal +Geography,What is the capital of Mauritius,Port louis,General,In Spain St John Bosco is the Patron Saint of what,Cinema,General,Who appeared solo at the woodstock festival after leaving 'the lovin' spoonful',John sebastian +Science & Nature, The Cairn terrier is great at catching __________,Rats,Science & Nature,The Chemical Symbols For Titanium & Sodium Spell Out The Letters To Which Girls Name? ,Tina ,General,The world's largest department store is a feature of which world city?,Seoul +Science & Nature,The planet closest to the sun is _________.,Mercury, History & Holidays,What's the resting place of those buried at sea,Davey jones's locker,General,The secret police of which country were known as the 'Ton Ton Macoute',Haiti +General,What is the only real food that U.S. astronauts are allowed to take into space,Pecan nuts,General,Cheers exterior shots featured a real bar - what's it name,Bull & Finch, History & Holidays,Which Band Were Dropped By Their Label EMI In 1978 On Grounds Of Unacceptable Behavior ,The Sex Pistols  +Sports & Leisure,When Kevin Keagan Left Liverpool To Which Club To Move To ,Hamburg ,General,"What are Unaone, Soxisix and Novenine",International phonetic numbers 169,Music,Blue Note Records Began In A) 1938 - B) 1949 - C) 1958,A = 1938 +General,Baked beans were originally served in what sauce,Treacle - molasses,General,Stingray Bay named by Cook is now known as what,Botany Bay,General,This chocolaty cereal features Fred Flintstone and Barney Rubble,Cocoa pebbles +General,Book of information on many subjects,Encyclopedia,General,What was the name of the seasick sea serpent,Cecil,General,Who led the U N forces in the gulf war,General colin powell +General,James Abbott McNeill were the first names of which artist,Whistler,General,Imperial Airways in 1925 was the first to do what,Show an in flight movie,General,What is Barbi's full name,Barbara Millicent Roberts +General,What song on the Rubber Soul album became No 1 Overlanders,Michelle - 1966,General,Hebrew and what are the official languages in Israel,Arabic,General,In Beast Wars what is the name of the Maximal Ship?,The Axalon +General,"What sport was described as ""Chess with muscles""",Fencing, History & Holidays,Which horror film star was portrayed by Martin Landau in Tim Burtons 1994 film about cult film maker Ed Wood ,Bela Lugosi ,Science & Nature,What Is The Commonest Metal On Earth ,Aluminium  +General,How many championship divisions are there in boxing,Eight,General,Who recorded such popular songs as 'policy of truth' and 'personal jesus',Depeche mode,Entertainment,TV series: 'American ______'?,Bandstand +General,Canthopterygian is a(n)___.,Fish,Music,Which Song From Grease Was A No.1 Hit In The UK & USA,You're The One That I Want,Music,Highest amount a Beatles-related item sold for at auction,"$2.3 million (in 1985, for John's 1965 psychedelic Rolls-Royce)" +General,What is the Capital of: Macedonia,Skopje,General,What is the fourth day of the week,Wednesday,General,"Which of these is NOT a computer: MANIAC, SILLIAC, BRAINIAC,ILLIAC, JOHNNIAC",BRAINIAC +General,Who was the first male to appear on the cover of Playboy,Peter Sellers,General,What colour is puke,Dark Green,General,In Switzerland it is illegal to do what in an apartment after 10pm,Flush Toilet +General,Who was Prime Minister of China 1949 to 1976,Chou En-Lai,General,"In the US, what is a 'flapjack' a type of",Pancake,General,What kind of car does Nick Nolte's character in 48 hours?,A sky blue Cadillac convertible +General,What london palace was destroyed by fire in 1936,Crystal palace,General,"Derived from the latin 'australis', what does Australia mean",Southern,General,A salt enema was given to children to rid them of ______,Threadworm +General,"In the film version of Willy Russell's play, who played Shirley Valentine",Pauline collins,General,What is the main flavouring in a Greek Tzataili sauce,Garlic,General,Which house did Winston Churchill live in from 1922 to his death,Chartwell +People & Places,"What Did Churhill, Cromwell, & Shakespeare Have In Common ? ",They all Had Red Hair ,Music,In Whose Band Is Madonna Wayne Gacy The Keyboardist,Marilyn Manson,General,"Which female vocalist had a top ten hit in 1988 with ""Je ne sais pourquoi""",Kylie minogue +General,In what country was Bonnie Prince Charlie born,Italy,General,What was the name of the detective agency in Moonlighting?,Blue Moon Detective Agency,General,The average what is designed to last for 180 wearings,Bra +General,Who was on the throne at the time of The Great Fire Of London,Charles II,General,"Who said that all matter comes from fire, water, earth & air",Aristotle,General,A computer small and light enough to be held in one hand,Palmtop +General,"How many fires erupted in the april 18, 1906 san francisco earthquake",Fifty, Geography,On what island is the Blue Grotto?,Capri,General,Who was the Lady of the Lamp,Florence nightingale +General,In area what is the largest South American country,Brazil,Music,In Which Keyboard Instrument Are The Strings Plucked Not Struck,The Harpsichord,General, Acrophobia is a fear of ___________.,Heights +Food & Drink,"The Italian Desert Made By Whisking Egg Yolks, Wine And Sugar Is More Commonly Known As What ",Zabaglione ,General,What is a Dandie Dinmont,Dog - Borders Terrier,General,What sea creature resembles a knight in chess,Seahorse + History & Holidays,What company used the little aligators as it's symbol on clothing? ,Izods ,General,What is the Capital of: Ghana,Accra,General,What cocktail is made of rum and lemon,Daiquiri +Entertainment,Who sang 'In The Air Tonight'?,Phil Collins,General,What did john augustus larson invent,Lie detector,General,Ford Prefect came from a star in which constellation,Orion (Betelgeuse) +General,"What Police Resource Was First Used In The ""Jack The Ripper"" Investigation",Bloodhounds,General,What does a.d. actually stand for,Anno domini,General,In WWW terms what does i.e. mean on a domain name,Ireland +General,"What's the international radio code word for the letter ""C""",Charlie,Music,Who Was Calling Gloria In 1982,Laura Branigan, History & Holidays,What was the first name of the baby girl who fell down the well? ,Jessica  + Geography,Into what ocean does the Zambezi River empty?,Indian Ocean,General,What colour shirts must table tennis players wear in official competition,Black, Geography,What is the basic unit of currency for Liechtenstein ?,Franc +General,"In which sport would yoU.S.ee a dolphin bent knee, a walk over front and a catalina",Synchronized swimming,Toys & Games,A Royal Flush is the best hand you can get in which game,Poker, History & Holidays,In 1959 Who Established A Communist Government In Cuba ,Fidel Castro  + Geography,What is the capital of Vanuatu ?,Port-Vila,General,Satanic Majesties Request Music: What was the only hit song for the band 'It's a Beautiful Day',White,General,"Short, simple, descriptive poem idealizing country life",Idyll +General,To which S American country does Easter Island belong,Chile,General,A Blue Imperial or a New Zealand white types of what,Rabbits,General,Which temperature scale begins at minus 273.15 degrees Celsius,Kelvin absolute thermodynamic +General,What industry would use a mordant,Dying - to fix a colour,General,Which is the larger of the rhinocerous,White,General,Which Duo's Fan Club Is Known As The Sons Of The Desert ?,Laurel & Hardy +General,Man shall not live by bread alone - Which NT book,Matthew 4.4,General,The soft areas of the cartilage on a baby's head where the skull bones havn't joined is called what,Fontanelle,Geography,Seoul is the capital of which country,South korea +Science & Nature, The individual hair of a chinchilla is so fine that __________of them equal the thickness of a single human hair.,500, History & Holidays,"The Christmas story is only told in two of the four gospels, Luke is one which is the other ",Matthew ,General,Tip Throat Vamp Collar Shank are parts of what object,Woman's Shoe +General,In computing what is Ram short for?,Random Access Memory,General,Robin Goodfellow alternative name which Shakespeare character,Puck,General,Numerophobia is the fear of,Numbers +General,"In ballet, a rising with a spring movement to point or demi-point.",Relevé,General,What is the flower that stands for: bonds of affection,Gillyflower,General,The star Spica is in which constellation,Virgo +General,The Italian Date is a common name of what fruit,Tamarind,General,The Witches Curse alternative name which G&S operetta,Ruddigore,General,What is the most popular dogs name in the US,Max +General,"What links The Reivers, Grapes of Wrath, Humboldt’s Gift",Pulitzer Prize winners,General,African tree with massive trunk and edible fruit,Baobab,General,What term is given to the cooling method applied to Metals in order to relieve strains which have occured during heat treatment??,Annealing + History & Holidays,In Which Year Did The Battle Of Hastings Take Place? ,1066 ,General,What one word fits ____hood; ____hole; ____date,Man,General,What is the acronym for an image produced by aligning molecular crystals,Lcd +General,What's Penthouse's sister publication for women,Viva,Sports & Leisure,Which Country Produces The Most Successful Rally Drivers ,Finland ,General,Charles Adrian Wettach became famous as what clown,Grock +General,"In an alphabetical list of countries in the world, what middle eastern country comes between portugal and romania",Qatar,General,"Which ""Coronation Street"" character was played by Ian Mercer",Gary mallett,General,"In Greek mythology, who is the mother of the muses",Mnemosyne +General,What was the name of the attempted invasion of Cuba in 1961,The bay of pigs, Language,What is 'bountiful mother' in Latin?,Alma mater,General,Which port is the capital of the Italian region of Liguria,Genoa +Sports & Leisure,What Is The Name Of The Controlling Body In Flat Racing ,The Jockey Club ,General,Celtic language of Scots or Irish,Gaelic,General,What's the fourth book of the new testament,John +Geography,What is the capital of Vanuatu,Vila,General,Who would take silk as part of their job,Barrister,Science & Nature,To Which Family Does The Kookaburra Belong? ,Kingfisher  +General,What is Alberta's most important tree,Spruce,General,Baseball: star Joe Dimaggio married which actress,Marilyn monroe,Food & Drink,On the dessert menu what is the name given to a mixture of Fresh or dried fruit in syrup? ,Comp?te  +Music,Which Future Pop Star Attended The Scene Of Eddie Cochran's Fatal Car Crash In 1960 In His Job As A Police Officer,Dave Dee,General,Which fast suburban railway system in Ireland runs from Howth in the North to Bray in the South,Dart,General,"Who Drove The Car ""The Compact Pussycat""",Penelope Pitstop +Geography,"The City of Bridges in _________ are to be found in Saskatoon, Saskatchewan. The city of Saskatoon was named for the red berries that grew along its riverbank",Canada,Entertainment,"In 'La Traviata', what does Violetta sing?",Sempre Libera,General,What part of the body does a neuro-surgeon specialise in,Nervous system +General,What name is given to the fleshy part of a horses tail,The dock,General,Which Spanish town is noted for high quality steel swords,Toledo,General,Woodbury soap was the first to show what in its advertisements,Full length nude woman 1936 +General,What are Misty Rain Sunshine Blue Honey Rose,Porn Stars,General,Worlds largest numismatic publication is___.,Coinage,General,The Tehran hostages were released in 1980 after how many days of captivity,Four hundred and forty four +General,What was romeo's family name,Montague,General,"In tokyo, for what are toupees sold",Dogs,General,Whats the worlds longest snake,Python +Music,"What Live Record Did ""Rolling Stone"" Readers Vote The Best Album Of 1976",Frampton Comes Alive / Peter Frampton,General,What does a fishmonger do for a living,Sells fish,General,Dimaggio what band did james brown tour and record with in the 1950's,Famous flames +General,Which metal is in liquid state at ordinary room temperature,Mercury,General,Three Scottish kings and eight Popes share what name,Alexander,Music,"Who Had A Hit In The 90's With ""Boom Boom Boom""",Outhere Brothers +General,Who recorded the albums Blonde on Blonde and Blood on the Tracks,Bob dylan,General,Moriaphillia is sexual arousal from what,Telling dirty jokes,General,If a bird nidifies what has it just done,Built a nest +General,What European capital city is NOT on a river,Madrid,Entertainment,In which film was Goldie Hawn the body double for Julia Roberts?,Pretty Woman,General,In what film did alec guinness play eight parts,Kind hearts and coronets +Food & Drink,What Do The Initials U.H.T Refer To In Relation To Milk? ,Ultra Heat Treated ,General,Wimpy was the working title of what classic movie,Psycho,General,What's the world's largest fresh-water island,Manitoulin +General,How many lives did Herb Philbrick lead,Three,General,Naturally occuring community of flora and fauna adapted to the conditions in which they occur,Biome, History & Holidays,On the 3rd January 1959 which state became America's 49th ,Alaska  + History & Holidays,What tree is mentioned in the Christmas song 'Twelve Days of Christmas' ? ,A Pear tree , History & Holidays,"What is the name of Tiny Tim's father in the story, 'A Christmas Carol'? ",Bob Crachit ,Food & Drink,Which is the most eaten fruit in the world? ,Banana  +General,In which county is Combe Martin,Devon,General,"Which Japanese suicide technique translates to the English ""belly cutting"" ?",Hara-kiri,General,What was the name of Nero's murdered mother,Agrippina +General,The 'Wife of Bath' is a character from what tale,Canterbury tales,Music,What Type Of Music Would One Associate With The West Indies,Calypso,Geography,"______________, in Russia, is the largest city north of the Arctic Circle.",Murmansk +General,"Who recorded the album ""moving finger"" in 1971",Hollies,Entertainment,Name Jerry Garcia's long lived group.,The Grateful Dead,General,What kind of shoe is nailed above the door for good luck,Horseshoe +Science & Nature," Flatfishes form a unique and widespread group that includes about 130 American species, common in both the Atlantic and __________",Pacific ocean,General,Who speaks Quechua,Peruvian Indians,Music,Who was Ringo's first wife?,Maureen Cox +General,"Who Provided The Voice Of Esmerelda In The Disney Animated Movie ""The Hunchback Of Notre Damme""",Demi Moore,General,In what field was Erie Shipton famous,Mountaineering,General,Britain what's the adhesion of molecules to the surfaces of solids called,Adsorption +Geography,"In Which County Is Chequers , The PM's Official Residence? ",Buckinghamshire ,General,Brings your ancestors back to life - translated advert for what,Come alive with Pepsi - in China,General,"Which British City Was Known As ""Luguvallum"" To The Romans",Carlisle +Music,"What Is The Connection Between Swing Bandleaders - Woody Herman, Artie Shaw, & Benny Goodman",They All Lead On Clarinet,General,Abel Magwitch and Biddy appear in which Dickens book,Great Expectations,General,Chinese bean sprouts are usually the sprouts of which bean,Mung bean +General,Which 'tarzan' swimmer was the first man to swim a hundred yards in less than a minute,Johnny weismuller,Sports & Leisure,Who was the first spin bowler to take over 500 test wickets? ,Shane Warne , Language,"""faux pas"" means ___________.",Mistake +General,What phrase did the nazi adopt in the 1920s to label their new order,The third reich,General,What is a turkey's wishbone,Furcula,General,What is the fear of sleep known as,Somniphobia +General,Who Was The Comic Partner Of The Man Born Arthur Jefferson?,Oliver Hardy,General,Which Holds The Record For The Novelist Most Borrowed From Public Libraries ?,Catherine Cookson,General,What can you buy in a bar that Japanese farmers massage into their cows to make the meat tender,Gin + History & Holidays,How Old Was Edward VI When He Became King? ,9 Years Old ,General,Which playing card is called the Curse of Scotland,Nine of Diamonds,Entertainment,Name the ranger who was always after Yogi Bear.,Rick +General,"Name the fat, rich detective with a passion for beer, food and orchids?",Nero wolfe,General,Climbing boys were banned what did sweeps drop down chimneys,Live Chickens,General,Do trees grow more quickly or slowly at night,More slowly + History & Holidays,Who was nick named Hanoi Jane in 1972 because of her enemy propaganda broadcasts from Vietnam? ,Jane Fonda ,General,What type of charge does a proton carry,Positive,General,What is the atomic mass of bromine,79.9 +General,"In Common: Himalayan, Rex, Manx, Maine Coon",Breeds of cats kinds of,General,Local law - Atwood con - cant play what if waiting politician speak,Scrabble,General,Lutraphobia is the fear of,Otters +General,In animal terms what is a dude,A camels penis,General,Who starred in Ceiling Zero as a pilot,James Cagney,General,How did Marc Quinquadron die while setting a new world record,Food Poisoning ate 7 snails 3 min +General,What is the young of this animal called: Horse,Foal yearling colt filly,Science & Nature,What is a young whale called?,Calf,General,What is the flower that stands for: am i forgotten?,Holly +General,What is a place where bees are kept called,Apiary, History & Holidays,Who sang Happy Birthday to John F. Kennedy for his 45th?,Marilyn Monroe,General,Which band member was Boy George allegedly seeing in Culture Club during the eighties? (Just name the instrument he plays),The Drummer +General,In 1959 1211-kg great white shark becomes largest fish ever caught on a,Rod, History & Holidays,What country did Abel Tasman discover in 1642 ?,New Zealand,General,Which word refers to all the animal life of a specific place or time,Fauna +Science & Nature,Which science studies animal behaviour in natural habitats ?,Ethology,Geography,What country borders Egypt on the west,Libya,General,Cat stevens 'want's to try to love again but ______',The first cut is the +General,"Which Common Everyday Item Was Introduced To The World By ""Ralph Scneider""",The Credit Card,General,Dish served between fish and meat courses,Entree,Art & Literature,Whose ghost appears at the dinner table in 'Macbeth'?,Banquo's +General,Jefferson how did leonardo da vinci's alarm clock wake a sleeper,Rubbing the feet,General,The sleeve of which album was the first to feature lyrics,Beatles Sergeant Peppers,General,What does a sacerdotal person study for,The priesthood +General,"To within 30 feet, how tall is the Eiffel Tower",984,General,What is Xylography,Wood Engraving,General," The word ""cumulus"" refers to a type of ___________.",Cloud + History & Holidays,Name The Ship In Which Columbus Discovered America? ,Santa Maria (Pinta & Nina Were Sister Ships) ,Music,"Which Album Was At One Time Going To Be Called ""Everest""",Abbey Road, Geography,What country formed the union of Tanganyika and Zanzibar?,Tanzania +General,What city has Kogoshima as its airport,Tokyo,General,What is the main food of mosquitoes,Nectar,General,What is a 'kartoffel' in germany,Potato +General,What weighs less than a penny,Hummingbird, History & Holidays,"He is identified with the expression, ""Eureka"".",Archimedes,General,What is the flower that stands for: assignation,Pimpernel +General,What is 40 in Roman numerals,XL,Music,Name The 2 No.1 Singles For The Seekers In The 60's,I'll Never Find Another You / The Carnival Is Over,Science & Nature, __________ have scent glands between their hind toes. The glands help them leave scent trails for the herd. Researchers say the odor smells cheesy.,Reindeer +General,Who said of Marilyn Monroe 'Kissing her is like kissing Hitler,Tony cutis,General,What was the lone ranger's real name,John reid,General,What do you kiss to be endowed with great powers of persuasion,Blarney stone +General,What US states name means long river in Indian,Connecticut,General,"What industry is symbolized by the term ""madison avenue""",Advertising,General,Rich crescent shaped roll,Croissant +General,Which flying pioneer was nicknamed the lone eagle,Charles Lindbergh,People & Places,Which Former Goodie Is Into Bird Watching ,Bill Oddie ,Science & Nature,What is it that turns blue litmus paper red,Acid +Food & Drink,If A Sparkling Wine Is Labelled As 'Brut' How Will It Taste ,Dry ,General,What is the national airline of the Netherlands called,Klm,General,In photography what does S.L.R stand for?,Single Lens Reflex +General,What is the study of the earth's physical divisions,Geography,Science & Nature,This organic gem is a deep red secretion from a marine animal.,Coral,Science & Nature,In Computing Quite Simply What Does The Abbreviation USB Stand for ,Universal Serial Bus  +Geography,What European country administers the island of Martinique,France,Music,"What Connects Catatonia, Super Furry Animals, Budgie",Wales, Geography,What is the capital of Lithuania ?,Vilnius +Sports & Leisure,In Which County Is The Epsom Derby Raced? ,Surrey ,Food & Drink,"Which cocktail consists of Tia Maria, vodka and coke? ",A Black Russian ,Food & Drink,"Vodka, orange juice and Galliano make up which type of cocktail ",Harvey Wallbanger  +General,What Historical Even Occurred on the 22 January 1901 ?,Death Of Queen Victoria,Geography,Which city has the largest rodeo in the world,Calgary,Geography,Can you give me the two former names of the modern Turkish capital of Istanbul? ,"Constantinople, Byzantium " +General,"Carpet, coral and pilot are all types of which animal",Snakes,General,Anthony Daniels played who in a series of films,C-P3O,General,The Windhover Is The Country Name For Which Bird?,Kestrel +General,What county has its map on its flag,Cyprus,General,On TV what team worked out of Iolani Palace,Hawaii 5 0,Science & Nature, A 42_foot sperm whale has about 7 tons of __________ in it.,Oil +General,George C Scott - what does the C stand for,Campbell,General,In WW2 what was the British equivalent of the German E-Boat,MTB or Motor Torpedo Boat,General,Who wrote Finnegan's Wake in 1939,James joyce +Sports & Leisure,The US Tennis Open takes place at which venue? ,Flushing Meadows ,General,Which spice comes in hands,Ginger,General,Who wrote mirc,Khaled mardam-bey +General,The same Louis did not consummate his marriage 7 years - why,Overgrown Foreskin,General,"What was the name of the comic strip that ""Henry Rush"" (Too Close for Comfort)wrote?",Cosmic Cow,General,What is the Greek word for Egyptian,Coptic +General,The Parthenon in Athens is built in which architectural style,Doric,General,A person putting a lot of effort into a task is said to be using,Elbow grease,Science & Nature,Which Has A Black Bill The Crow Or The Rook ,Crew  +General,In George Orwell's Animal Farm what type of animal was Muriel,The Goat,Science & Nature, The jackrabbit is not a rabbit; it is a __________,Hare,Technology & Video Games,What color is the 1-up mushroom in Super Mario Bros.? ,Green +Geography,___________ has 150 recognized ecosystems.,Hawaii,Entertainment,"When not a Birdman, what does Ray Randall do for a living",Police officer,General,What do English speakers call the region that the Spanish know as 'el Pais Vasco',The basque country +General,System of winds producing fine weather,Anticyclone,General,What is the average lifespan of a major league baseball,Five to seven, History & Holidays,This Indian group ruled in early Peru.,Inca +Science & Nature,Why Is Louise Brown Famous ,First Test Tube Baby ,General,Siderophobia is the fear of,Stars,General,Hemp plant part of which can be used as a narcotic,Cannabis +General,What is a group of this animal called: Finches,Charm,General,To an Australian what is an esky,Portable Beer Cooler,General,Phalacrophobia is the fear of,Becoming bald +General,"What story features Flopsy,Mopsy and Cottontail",Tales of peter rabbit,General,Name the ship in which Sir Ernest Shackleton sailed to the Antarctic,Endurance,General,The most shoplifted book in the world is ________,The bible +General,What did dr bart hughes create,Trepanning,General,Who was 45 when he became the oldest heavyweight boxing champion,George,Sports & Leisure,Who Was The First Black Footballer To Captain England ,Paul Ince  + History & Holidays,Who invented the wristwatch?,Louis Cartier,General,What part of a camera clicks when a photograph is taken,Shutter,Music,In 1977 Manhattan Tranfer Had A Uk Hit With The Song “Chanson D’Amour” But What Does It Mean When Translated Into English,Love Song Or The Song Of Love +Entertainment,"Which of Beethoven's symphonies was the legendary ""Incomplete""?",The 9th Symphony,General,What is the Capital of: Poland,Warsaw,Food & Drink,Gazpacho is a type of what? ,Soup  +General,Which television programme did Roy Castle present for 22 years,Record breakers,Music,What Is A Small Flute With A Higher Pitch Called,A Piccolo,General,What was the name of the show that featured Larry Appleton and his zany foreign cousin?,Perfect Strangers +General,For how many radio stations was john cage's 'imaginary landscaper no 4' scored,12,Science & Nature,What is the meaning of the name of the constellation Aquila ?,Eagle,General,"Who wrote the thriller ""The Eagle has Landed""",Jack higgins +General,"What's the largest island in the arctic ocean, with 195,928 square miles",Baffin,Science & Nature,"Which Canal , The Brainchild Of Ferdinand De Lesseps, Was Opened , To Great Aclaim, In November 1869 ",The Suez Canal ,General,What is the 13.5 ton chime on london's tower clock,Big ben +Entertainment,"Bugs always finds himself at the wrong end of a gun, usually toted by either Elmer Fudd or who",Yosemite sam,General,Whose 1986 hit was 'Walk Like an Egyptian',The bangles,General,"What, during World War Two, was the German or Nazi equivalent to the Japanese Kempei Tai",Gestapo +General,"In 1981, who won song of the year with 'sailing'",Christopher cross,Music,According To Cyni Lauper What Do Girls Just Want To Do,Have Fun,General,What chemical symbol is used for the element actinium,Ac +Entertainment,"Name the band - songs include ""Let's Stick Together, The Price of Love""?",Bryan Ferry,General,"Biology: In Linnaean classifcation, the group which comes directly under Kingdom.",Phylum,General,What Frenchman was the king of chefs and chef of Kings,August Escoffier +General,What Sanskrit word means great king,Maharaja,General,There is approximately one what for each person in the world,Chicken,General,Who rode a horse called Magnolia,George Washington +General,What sort of bone is broken as wishes are made,A wishbone,General,In which country is the U.S. naval base of Guantanamo,Cuba,Art & Literature,Which Famous Book Contains The Line 'In A Hole In The Ground There Lived A' ,The Hobbit  +General,Which is the largest province of canada,Quebec,General,"What are the Anatolian, Atacama, Nafud and Zirreh",Deserts,General,Yellow translucent fossil resin,Amber +General,Skeleton is derived from Greek - what is its literal translation,Dried Up,Science & Nature,What Disorder Is Characterised By Appetite And Weight Loss ,Anorexia Nervosa ,General,Italians often eat a whole what to cover garlicky breath,Coffee Bean +General,What is the name of the berlin cabaret where lola lola sang,Blue angel,Music,"Which Band Had Hits With ""Heaven"" & ""Cherry Pie""",Warrant,Food & Drink,What is a sorbet? ,A Water Ice  +General,Which teacher did Maggie Smith play in the 1969 film of Muriel Spark's novel,Miss jean brodie,General,What was the name of tim holt's horse,Duke,General,Which Dickens novel is considered an autobiography,David Copperfield +General,Collective nouns a Blessing of what animal group,Unicorn,General,"Which sign placed before a note in music, lowers it by a semitone?",Flat,Entertainment,Secret Identities: Jonn Jonzz,Martian manhunter +Science & Nature,What's known as the bishop's stone ,Amethyst ,Art & Literature,"Stephen King's: ""Pet ________"".",Semetary,General,In ancient Japan public contests were held to find what,Best Farter loud and long +Music,Were Cabaret Voltaire Named After A French Fashion Magazine Or An Art Movement,Art Movement,General,What was discovered in the northern tip of Vancover Island in 1835,Coal,General,Plant what city did general sherman burn in 1864,Atlanta +General,The telephone country code 852 would connect you with _____,Hong kong,Music,"In Which City Would You Find Jazz Clubs Called ""Birdland, The Blue Note, & The Village Vanguard""",New York,General,Indianapolis is the capital of ______,Indiana +Music,In Which City were The Group Pulp Formed,Sheffield,Music,"Who had a massive hit in 1993 with ""I'm Easy/ Be Aggressive""?",Faith No More,General,Who is the biggest landowner in New York city,Catholic Church +General,Massachusetts in April the law states that dogs will have what,Rear Legs Tied,General,In the twelve labours of Hercules what did he do third,Capture Arcadian Stag,General,French cup of coffee flavored with apple brandy,Cafe calvados +General,At what atoll in the South Pacific did the U.S. do bomb nuclear bomb test in 1946,Bikini atoll bikini,General,"In the film 'hercules', whose voice is danny devito",Phil,General,John Lennon was shot outside of what New York building,The dakota +General,Palindromic word means raise to the ground or a mine passage,Level,Geography,In what country is the Mekong River Delta,Vietnam,General,Hercules performed twelve labours what was number seven,Capture of the Cretan Bull +General,What was the first name of the Israeli man who invented a rapid fire weapon in 1953,Uzi,General,Whitcome Judson in 1891 invented what for fastening shoes,Zip Fastener,General,Signal or time after which people must remain indoors,Curfew +General,Hamida Djandoubi in 1977 was the last one - what,Person Guillotined in France,General,Selma Lagerlof of Sweden in 1909 first woman to get what,Nobel Prize for Literature, History & Holidays,In which country was paper money first used?,China +General,What was Auguste Bartholdis most famous work 1886,Statue of Liberty,Science & Nature,What Are A Whales Breathing Organs Called ,Lungs ,General,"What First Did American ""Gertrude Ederle's"" Achieve In 1926",1st Woman To Swim Channel +General,Which Hugely Popular Tv Show Was The Creation Of Peter Fluck And Roger Law,Spitting Image,General,What Are Goldie & Isis,Reserve Boats For The Boat Race,General,Where is gorky park,Moscow +Music,"What Song Features The Lyric ""Then Your Children Will Be Next""",If You Tolerate This,General,What monarch observed a jubilee in 1977,Queen elizabeth,General,Picture representing word or syllable,Heiroglyph +Music,For Which Famous Rock N Roller Did Scotty Moore Play Guitar In The 1950's,Elvis Presley,General,For what tv sitcom did isabel sanford get her 1981 best actress emmy,Jeffersons, History & Holidays,"In 1972 which flash new motor was advertised with the slogan, `The car you always promised yourself'? ",Ford Capri  +General,What is the all-time best selling paperback book,Baby and child care baby,General,What is the most common sexual complaint of females over 50,Vaginal Dryness,General,"What Beatles album spent the longest time atop the charts, at 15 weeks?",Sgt. pepper's lonely hearts club band +Science & Nature,What lies between mars and jupiter?,The asteroid belt,General,What is a female swan called,Pen,Music,Who Used The Pseudonym Lieutenant Lush For Early Stage Appearances As A Member Of Bow Wow Wow,Boy George +General,Orthography is the study of what,Mountains,Science & Nature,A point to which rays of light converge is called a(n) ________.,Focus, Geography,What is the second largest of the United States?,Texas +General,Who played Louis Armstrong in 1954 film The Glen Miller Story,He played himself,General,"Who ""Loved not to wisely but too well"" Shakespeare play",Othello,General,What word for taking tissue for microscopic examination was coined by French dermatologists in 1879,Biopsy a biopsy +General,Meteorology is the study of ______,Weather,General,Whats the term for a resident of Liverpool,Liverpudlian,General,Brother Benedict translated name of what port and food product,Fray Bentos +Mathematics,Solve this: 10*3+2?,32, Geography,Frankfort is the capital of which US state?,Kentucky,General,An addition to a will is called a,Codicil +General,Religious initiation of Jewish boy at 13,Bar mitzvah,Music,What musical does the song “There's No Business Like Show Business” Come From?,Annie Get Your Gun,General,Bon Jovi and Ritchie Sambora both list this band as their influence?,The Beatles +General,In which modern country is the biblical land of Sheba,Yemen,General,What does the symbol 'am' represent,Americium,General,The Aztecs reckoned it was the food of the gods what was,Chocolate +General,The black and white episode of Chicago Hope is a tribute to who,Alfred Hitchcock,General,Which Country Inflicted “Sven Goran Eriksons” First Defeat As England Manager?,Holland,General,Anti tank rocket launcher,Bazooka +General,Flower of the blessed night is the local name of which plant,Poinsettia,General,"Which ""fishy"" character, in an opera by Benjamin Britten, is elected May King, spending his prize money on a debauch",Albert herring,General,"According to Hindu myth, what river once flowed through heaven",The ganges ganges +General,"What did Iroquois Indians mix with willow bark and call ""kinickinick""",Tobacco,General,In Main what is it illegal to step out of,Flying Plane,General,This award is the mystery writers equivalent of an Oscar,Edgar +General,Who invented the magnifying glass,Friar roger bacon,General,"The Theme Tune To Which Classic TV Show Was Performed By ""The Dickes""",The Banana Splits,General,The Shadows first record went straight to no 1 - what was it,Apache +General,Lilapsophobia is the fear of,Tornadoes and hurricanes,General,"Of which australian band, still rocking after 20 years, is angus young the brains",Ac/dc,General,If you had a Brassica Rapa what vegetable would you have,Turnip +General,A burning oil lamp is the symbol of which organisation,Gideons,General,How many children did queen anne have,Seventeen,Music,"What connects composers such as Haydn, Mozart, Beethoven and Schubert with the city of Vienna?",They All Died There +General,What is a draped female figure supporting an entablature,Caryatid,General,The name for the group of stars which form a hunter with a club & shield is ________,Orion,General,The gigantic Badshashi mosque is in which city in Pakistan,Lahore +General,What's the apparent gap between Saturn's A & B rings called,Cassini division,General,What kind of teeth did george washington have,Wooden,General,What is a '/',Virgule or solidus +General,Zeusophobia is the fear of ______,Gods,General,4000 people each year are injured by what household item,Tea Pots,Geography,In which continent would you find the yangtze river ,Asia  +General,Hitihita is a character in what book and film,Mutiny on the Bounty - Tahiti chief,General,What more attractive name do fishmongers use for dogfish,Rock salmon,General,Which is the second largest of the Japanese islands,Hokkaido +General,What sea is directly north of Poland,Baltic sea,Food & Drink,Who released the following 'edible' album 'Buddah and the chocolate box' ,Cat Stevens ,General,Which country (capital Kiev) lies just south of Belarus,Ukraine + Geography,Who owns the island of Bermuda?,Britain,General,A heavy winter fog containing ice crystals is a _____,Pogonip,General,What instrument do doctors usually have around their necks,Stethoscope +Science & Nature," The average adult male __________, the world's largest living bird, weighs up to 345 pounds.",Ostrich,General,What is the nickname for Idaho,Gem of the mountains,General,Which country consumes the most wine per capita 16.7 gal per,Luxemburg +Science & Nature,How many litres of air is in an adult lung?,Five,General,What were v1s and v2s supposed to do upon landing,Explode,General,What two natural resources are used to make steel,Coal & limestone limestone & coal +General,"In the television series, who owned the High Chapparal ranch",John cannon,General,How many Great Lakes do not border Michigan,One,Geography,What is the capital of Eritrea,Asmara +General,What is the capital of Lichtenstein,Vaduz,General,In New York where is it illegal to talk to a stranger,An Elevator,General,This is the only animal that can't jump.,Elephant + Geography,On which river is Rome located?,Tiber,Music,"Who Started His Chart Career In 1983With ""Black Heart"" Billed As Marc And The Mambas",Marc Almond,General,The Germans call them Stumphhose - what are they,Tights + History & Holidays,Whose Election slogan Was (You've Never Had It So Good)? ,"Harold Macmillian, 1959 ", History & Holidays,What is the name of the cake traditionally eaten in Italy at Christmas? ,Panettone ,Sports & Leisure,Which player was thrown out of Wimbledon in 1995 after he belted a ball in anger and it hit a Ball Girl? ,Tim Henman  + History & Holidays,Muhammad Ali Took On George Foreman In The 'Rumble In The Jungle'' In what Country Did This Take Place ,Zaire ,General,Supposed paranormal force moving objects at a distance,Telekinesis,General,Caractacus Potts drove what car,Chitty Chitty Bang Bang +Science & Nature,In Geology What Period followed The Cretaceous ,Jurassic ,General,Don Hoeffler coined what phrase in Electronic News in 1971,Silicon Valley,Science & Nature,What Do We Call A 3D Picture Created By Lasers ,Hologram  +Geography,In which state are Gettysburg and the Liberty Bell,Pennsylvania,General,What does an animal have if it is a bird,Feathers,General,"1B.In the Roman Catholic church, what is a devotion of prayers or services on nine consecutive days called",A novena +General,What sort of celestial body is Uranus,Planet,General,Who is the patron saint of lovers?,Saint Valentine,General,What organization's launch was FDR preparing to attend when he died,The united nations united nations +Geography,What is the capital of Andorra,Andorra la vella,General,After water what is the most consumed beverage,Tea,Music,"Who Played Anna In The 1956 Film ""The King And I""",Deborah Kerr +Science & Nature,Of What Illness Is Petit Mal A Form ,Epilepsy ,Music,Everything Changes And Nobody Else Were Hit Albums For Which 90's Band,Take That,General,What uniform number was worn by Larry Bird & Kareem Abdul Jabbar,33 +General,The average Britain consumes 4907 what in their lifetime,Loaves of bread,Music,"Who Released An Album Of Woody Guthrie Lyrics Entitled ""Mermaid Avenue""",Billy Bragg & Wilco, History & Holidays,Where Was The Royalist HQ During The English Civil War? ,Oxford  +General,"Point He released the parody ""oh you ate one too"" in 1988 which included the song ""Cabo Wabo""",Van Halen,Science & Nature,Which Flightless Bird Lays The World's Largest Eggs? ,Ostrich ,General,Where is ancient troy,Turkey +Food & Drink,What is the main vegetable ingredient of moussaka? ,Aubergines ,General,Who won the first Grand Prix World Motor-racing Championship in 1950,Guiseppe farina,Music,What Was The No.1 hit For Early Eighties Band Tight Fit,The Lion Sleeps Tonight + Geography,What are the worlds four oceans - alphabetically?,"Arctic, Atlantic, Indian and Pacific",General,In SF California by law what is guaranteed to the masses,Sunshine,General,There are 42 what in a standard deck of cards (exclude jokers),Eyes +General,In which 1956 film did Elvis Presley make his debut,Love me Tender,General,Richard Roundtree played what detective in three 70s films,John Shaft,General,Where do the english monarchs live,Buckingham palace +General,What is the common name for Larus argentatus,The herring gull,Music,"Bosendorfer, Steinway, & Bechstein Are All Makes Of Which Instrument",Piano,Music,"Which Band Sang ""Love My Way"" On The ""Valley Girl"" Soundtrack",Psychadelic Furs +General,"Football Team, minnesota _______",Vikings,Music,"Which Soul Artist Died At The Very End Of 1999, Having Been Confined To A Wheelchair Since 1990 After A Stage Lighting Rig Fell On Him",Curtis Mayfield,General,What is the name of the group of Muslim scholars who have fought for control of Afghanistan in recent years,Taliban +Food & Drink,From which plant is tequila derived?,Cactus,General,What word is used to describe a Muslim who has completed a pilgrimage to Mecca,Hadji,General,How many volumes were in abdul kassem ismael's library,117 000 +General,This marsupial native to Australia feeds on eucalyptus leaves.,Koala,General,In Morse code one dash four dots what number,Six,General,What was the surname of the family in The Grapes of Wrath,Joad +General,In what year did the Royal Navy's daily rum ration end?,1970,Sports & Leisure,"In Indoor Athletic Races, Over What Distance Is The Shortest Sprint Race Run ",60 m ,General,"Who said the line ""Dr Livingstone, I presume!"" ?",Henry Morton Stanley +General,Any solo performance in a ballet.,Variation, History & Holidays,What did Erik Rotheim invent in 1926?,Aerosol,General,In what city was Handel's Messiah first performed,Dublin +General,"Excessive discharge of blood from blood vessels, caused by pathological condition of the vessels or by traumatic rupture of one or more vessels",Haemmorage,General,The film 'the wizard of ______',Oz,Music,"Which Beatles Song Did Frank Sinatra Describe As ""The Greatest Love Song Of The past 50 Years""",Something +Science & Nature,What Hormone Controls The Supply Of Sugar From The Blood To The Muscles ,Insulin , History & Holidays,Who created an 86-letter syllabary for the cherokee language ,Sequoyah ,Music,Name The Guitar Like Instrument With A Circular Belly Of Streched Parchment,The Banjo +General,Which bottled water originates in Dovedale,Ashbourne,General,Tessenjutsu is a martial art based on the use of what item,A Fan,General,There are 625 sweat glands in one square inch of human ______,Skin +General,Processed Galena produces which metal,Lead,General,Janette MacDonald was nicknamed the Iron what,Butterfly,General,In which 1992 film was Kevin Costner hired to look after Whitney Houston,The bodyguard +Toys & Games,What are a chessboard's horizontal rows called?,Ranks,General,What is considered history's greatest military evacuation,Dunkirk,General,Bacardi and Carioca rums come from what country,Costa Rico + History & Holidays,Elizabeth I was the daughter of which king?,Henry VIII,General,The rabbit's foot came to be considered a_____,Good luck charm,Sports & Leisure,Who Won The 2007 US Super Bowl? ,Indianapolis Colts  +General,"In computing, what is 'bit' an abbreviation for",Binary digit,General,Capital cities: Oman,Muscat,General,What shape is canestrelli pasta,Little Baskets +General,Which Italian hard cheese is especially good for grating,Parmesan,Science & Nature, Most cows give more milk when they __________,Listen to music,Music,"Who Saw ""Life Thru A Lens"" On His 1998 Album",Robbie Williams + History & Holidays,By what name was Baron Manfred von Richthofen better known? ,The Red Baron ,General,John McLaughlin made sold McLaughlin's Belfast style – what,Canada Dry Ginger,Sports & Leisure,Where were the 1980 Olympics held ?,"Moscow, U.S.S.R." +General,In 1986 Graceland was the Grammy album of the year – who’s,Paul Simon,General,What is a group of doves,Dule,Sports & Leisure,In which year will London host the Olympic Games ,2012  +Art & Literature,Whose First Collection Of Short Stories Entitled In Our Time Was Published In 1925 ,Ernest Hemmingway ,General,What colour on black produces the most visible combination,Yellow,General,"Who wrote the first song ""Come On"" Rolling Stones recorded 63",Chuck Berry +Science & Nature,What is the study of prehistoric plants and animals?,Paleontology,General,Paul Morel was a character in which classic novel,Sons and Lovers,General,Which astronomer discovered the planet Uranus in 1781,William herschel + Language,What does the Irish 'dubh linn' mean?,Black pool,Food & Drink,What flavour is the drink Kahlua? ,Coffee ,General,Hank Ballard and the Midnights first released what in 1960,The Twist +Science & Nature,"Where Will You Find The Characters Blinky, Pinky, Inky & Clyde ",Pacman (The Ghosts) ,Sports & Leisure,Who Won The 800 Metres At The Moscow Olympics (1980) ,Steve Ovett ,General,Ixchel is the mayan patroness of ______,Pregnant women +People & Places,By what name is Reginald Dwight better known as?,Elton John,Music,"How Is The World Of Music Is ""Anna Mae Bullock"" Known To The World",Tina Turner,Music,What Band Didn't Want To Miss A Thing In 1998?,Aerosmith + History & Holidays,"According to the Christmas song what was the only present asked for? 2 Front Teeth, 5 Bath Cubes, 4 Bars Of Soap, 3 Pairs Of socks ",2 Front Teeth ,General,Which word describes a male singer singing in a high register,Falsetto,Music,"Ollies Gang Made Me Go To A Flower Show In London, Who Am I",Elvis Costello +General,Sex shop survey what's most popular flavour eatable knickers,Cherry,General,As what was the Taj Mahal built,Tomb,General,"What Nationality Was Saint Valentine, The Patron Saint of Lovers ",Italian  +General,"What kind of animal was Cleo in the famous story of ""Pinocchio""",A fish,General,"In Which Sport Did The Term ""Hat Trick"" Originate",Cricket,General,Who owns: folger coffee,Procter and gamble +General,Which Asian Country Banned Lewis Carrol's Book Alice In Wonderland,China,General,What is the frog's name in 'the muppet show',Kermit d frog,General,Who was the first chief justice of the us supreme court,John jay +General,Which pie consists of a long pork pie stuffed with a boiled egg?,Gala Pie,General,In 1965 Gambia achieved independence from which country,United Kingdom,General,"Who was the actress that made waves in 1984's ""Splash""?",Darryl Hannah +General,In Star Trek what is Chekov's first name,Pavel,General,Captain Jean Luc-Picard keep a fish called what,Livingston,Music,Brian Jones Came From Which Gloustershire Town,Cheltenham +Music,"""Le Freak"" and ""Good Times"" were hits for which New York group?",Chic,General,By what other name is the mountain K2 called,Mount godwin austen,General,What are a tiger's paw prints,Pug marks +General,Greyhound racing in UK what colour does the No one dog wear,Red,Music,Who Shot Up The Charts With Eloise In 1986,The Damned,General,"Which football team has been nicknamed the ""Orange Crush""",Denver broncos +Music,"Who Recorded The Album ""Handsworth Revolution""",Steel Pulse,Technology & Video Games,What was the first lighthouse ?,Pharos of Alexandria,General,Which book was first published in Edinburgh in 1768 and subsequently moved its home to Chicago?,Encyclopedia Britannica +General,A digitabulist collects what,Thimbles,General,Brigham Young University offers what unusual Major,Ballroom Dancing,General,Of gibraltar In this team sport each player gets a chance to play every position,Volleyball + History & Holidays,"""Which of these was not one of the twelve apostles ? """"Jason,Bartholomew, Phillip, Thaddaeus"""" "" ",Jason ,General,In the siege of Mafeking who led the defenders,Robert Baden Powell, History & Holidays,The Mask In The Film Scream Became A Popular Costume What Famous Painting By Edward Munich Was It Based On,The Scream  +General,Longacre square is now better known as what,Times Square - NY, Geography,What island has Hamilton as its capital?,Bermuda,Geography,To Which Country do The Balearic Islands Belong? ,Spain  +General,Whose associate was J. Wellington Wimpy,Popeye,General,During their lifetime the average person eats four what,Spiders in their sleep,General,In what city is the worlds largest carpet manufacturer,Kashmir +General,The term Centennial represents how many years ?,100, History & Holidays,In What Us City Was President John F Kennedy Assasinated In 1963 ,Dallas / Texas ,General,What was the original meaning of the word harlot,Tramp +Geography,Where Is Sugar Loaf Mountain ,Rio De Janeiro ,General,Every human first spent about half an hour as a single what,Cell,Entertainment,"Which comic strip was banned from ""Stars and Stripes""",Beetle bailey +Sports & Leisure,"Which is the heaviest, An Ice Hockey Puck or a Baseball? ",An Ice Hockey Puck , History & Holidays,How Long Did The 100 Years War Last ,116 Years ,Music,Which Single Was Recorded EMI's Studios In Paris In 1964,Can't Buy Me Love +General,Which country eats the most turkey per capita?,Israel,General,Which famous ship had a total crew numbering 430,Star ship Enterprise – Captain Kirks,General,Panophobia is the fear of,Everything +Music,"Who Declared ""I Second That Emotion"" In 1967",Smokey Robinson & The Miracles,Sports & Leisure,"What sport do the following terms belong to - ""Hotdog & Bottom Trun""?",Surfing,General,Calico cloth was invented in which country,India +General,What product sold 25 bottles in its first year for $50 cost $75,Coca Cola a 50% loss,General,Who was the French revolution leader who was later guillotined by Robespierre,Danton,General,The world's longest natural gas pipeline is in what country,Canada +General,Who was the leader of the good Transformers?,Optimus Prime,Music,"Who thought up the band name ""The Beatles""?",Stu Sutcliffe,General,Bubba is Yiddish for what,Grandma +General,Who was known as 'the peanut president',Jimmy carter,Music,Whose Debut Single Was A Cover Of A Smokey Robinson Tune And Former No.1 For The Temptations,Otis Redding / My Girl,Music,"Who Won The 1995 Mercury Music Prize For The Album ""Dummy""",Portishead +General,Japanophobia is a fear of ______,Anything japanese,Science & Nature,What is the chemical symbol for copper,Cu,General,What is Pennsylvania's official drink,Milk +General,Which is the largest african bird of prey,Lammergeyer,General,A poem of fourteen lines is called a..,Sonnet,General,What is 'perestroika' in english,Restructuring +General,What has approximately 1/4 pound of salt in every gallon,Seawater,Science & Nature,What is a chuckwalla ,A lizard ,Geography,What is the capital of Russia,Moscow +Science & Nature,What's the curved line between any two points on a circle ,An arc ,General,Oikophobia is the fear of what,Houses,General,51 what were destroyed by the Great fire of London,Churches +General,What is the last word of the old testament,Curse,General,A race is won by four laps of the track - which sport,Speedway,General,What paris restaurant is at 3 rue royal,Maxim's +Food & Drink,Wher Might You Be Offered Ouzo ,Greece ,General,Eric Morley (Creator Of Miss World) Was Also Responsible For Which Hugely Successful TV Show,Come Dancing,General,Collective nouns - A Desert of what,Lapwings +General,How many children are in the 'american dream',2.5,General,"In Shakespeare, who killed Tybalt",Romeo,Sports & Leisure,Basketball: The Boston ___________.,Celtics +General,How many degrees in an interior angles of an equilateral triangle?,Sixty,General,You ordered unagi in a Japanese restaurant what would you get,Eel,General,What is the character of French wine described as 'mousseaux',Sparkling +Music,Whose 1979 Debut Album Was Called Inflammable Material,Stiff Little Fingers,Sports & Leisure,"In ten-pin bowling, how many points does a perfect game consist of?",Three hundred,General,Who has been serving apple pie for more than 100 years,Yale college +General,"Who was the lead singer for creedence clearwater revival, and recently released 'blue moon swamp'",John fogerty,Food & Drink,Taramasalata Is Made From The Roe Of Which Fish ,Cod Or Grey Mullet ,General,Which group had a hit with Mr Tambourine Man,The Byrds +Science & Nature,What is the term that refers to the search for the existence of ghosts?,Eidology,Food & Drink,An American frying pan is called what? ,A skillet ,General,Who composed Clair de Lune,Debussy + Geography,What is the capital of Zimbabwe?,Harare,General,Which American state has the longest borders with Canada,Montana,General,Which city was the location for the 1994 Winter Olympics,"Lillehammer, norway" +General,The petawatt is the worlds largest what,Laser,General,French riot police were ordered to the Rivera to deal with what,Hundreds topless women (1971),Toys & Games,Whist is an early form of this card game.,Bridge +General,Which instrument did jazz musician Louis Bellson play,Drums,General,Again in Ecuador if you were served cuy what have you eaten,Guinea pig,General,What is Radio Shack's main brand name,Realistic +Entertainment,What is the name given to the type of West Indian music made famous by artists such as Bob Marley and Peter Tosh?,Reggae,General,What tropical disease does an insect of the Anophales genus transmit,Malaria,General,What country issued a banana shaped stamp,Tonga +Sports & Leisure,Who won the 1982 soccer world cup?,Italy,General,In Venezuela lovers use pink what,Envelopes - post half price,General,Which German city was the setting of Auf Wierdersehn Pet,Dusseldorf +General,"What does ""kindergarten"" mean in german",Children's garden,Science & Nature,What Is The Condenser Better Known As ,A Capacitor ,Entertainment,What instrument are you playing when you perform a rim shot?,Drums +General,Who composed 'Symphonia Antarctica',Vaughan williams,Music,"Which Crew Was ""I Just Died In Your Arms"" A Hit For",Cutting Crew,General,"Who was ""The father of magazine science fiction""",Hugo Gernsback +General,What animal has a forked penis,Possum,Science & Nature,What Was The First Animal To Be Domesticated? ,Dog ,Science & Nature,What Figure Has Four Sides All The Same Length But No Right Angles ,A Rhombus  +General,In which actual city is the television series Casualty filmed,Bristol,General,What was King George VI first name,Albert - but Victoria said no king Albert ,General,Kiss on My List' was which duo's second number one hit,Hall and Oates +General,In Minnesota what pleasurable activity is totally illegal,Oral Sex,General,What counrty would you visit to ski in the Dolomites,Italy,General,Capital cities: Australia,Canberra +General,"Bezique, piquet and pinochle are all types of what",Card games, History & Holidays,He taught Alexander the Great.,Aristotle,General,Famous Flames Music: What 1958 song was The Coaster's only #1 hit,Yakkety Yak +Sports & Leisure,In Which Type Of Card Game Might You Make A Meld ,Rummy Or Canasta ,General,On which mountain did noah's ark run aground,Mount ararat, Geography,What is the highest point in South America?,Aconcagua +Food & Drink,Name The Woman Who Was In The Vanguard Of Tv Crooks ,Fanny Craddock ,Food & Drink,From Which Nut Is Marzipan Derived ,Almonds , Geography,In which state is Tupelo?,Mississippi +General,The ledge between a parapet & a moat is a(n)..,Berm,General,What was Canada's first chartered bank,Bank of montreal,General,"On irc, what does a/s/l mean?",Age/sex/location +General,The ponderosa pine is the state tree of which US state,Montana,Entertainment,"Name the band - songs include ""Aqualung, Thick as a Brick""?",Jethro Tull,General,"Fescue, Foxtail, Ruppia and Quitch are types of what",Grass +General,What was the name of Norm's wife on Cheers?,Vera, History & Holidays,"Who Had An 80's Hit With The Song 'Funky Cold Medina,' ",Tone Loc ,General,"Foreman at 45, what did george foreman win",Heavyweight championship +Music,What Did Adolphe Sax Invent,Saxophone,General,In the grounds of which house is the largest private tomb/mausoleum in England,Castle howard,General,How many children did adam and eve parent together,Three +General,Which of Henry VIII's wives was his siter-in-law,Catherine of aragon,General,Cuffs and Buttons was a cocktail in the 19th century what it now,Southern Comfort,Science & Nature,What bird is associated with Lundy Island?,Puffin +General,How many players are there in a water polo team,Seven,General,Who wrote Never Love a Stranger,Harold Robbins,General,"Excluding man, what is the longest lived land mammal",Elephant +Music,What Was The Lennon-McCartney Composition To Feature Ringo On Lead Vocals,I Wanna Be Your Man,General,What london street is named after the forerunner of croquet,Pall mall,General,What does bovine mean,Cowlike +General,For which film did art carney win best actor oscar in 1974,Harry and tonto,General,Which famous bell ringer killed Archdeacon Frollo,Quasimodo,General,In which 1960s kids show is The Hood the supervillian,Thunderbirds +General,Kind of white heron,Egret,General,"Common name for a tropical tree (Mulberry), grown on the islands of the South Pacific Ocean",Breadfruit, Geography,What is the capital of Solomon Islands ?,Honiara +General,What short sighted cartoon character had a nephew - Waldo,Mr Magoo,General,Titanic music score sales are greatest which film did it beat,Chariots of Fire – Vangelis,Science & Nature,What Is The Chemical Notation For Hydrogen ,H  +General,What is the oldest known vegetable,Pea,General,Gustav Vasa was the King of which Scandinavian country,Sweden,Food & Drink,What is sauerkraut? ,A German dish of pickled cabbage +General,Luang prabang was the capital of ______,Laos,General,Justitia is the roman goddess of ______,Justice,Art & Literature,What were the two cities in 'A Tale Of Two Cities'?,London and Paris +Science & Nature,Who discovered Penicillin ?,Alexander Fleming, Geography,What is the capital of Malawi?,Lilongwe,Music,According To The Song “Nellie The Elephant”  To Which Country Does Her Travelling Circus Visit,India (Bombay) +Music,"What Group Consisted Of James Dean Bradfield, Nick Wire & Sean Moore",The Manic Street Preachers,General,Federal law prohibits the recycling of used - what in USA,Eyeglasses,General,In what American state do most people walk to work,Alaska +General,What is mosquitoes main food,Nectar from flowers,Science & Nature,"Which Small Breed Of Cattle Is Found Wile In The Tibetan Plateu , North Of The Himalayas ",Yak ,General,Osmophobia is the fear of,Smells odors +General,Where was the last major american indian resistance to white settlement,Wounded knee,Geography,What Is The More Famous Name Of The Dancing (St Benezet Bridge) ,Le Pont D'Avignon ,General,Dating Back As Far Back As 1469 Who / What Is Britains Oldest Publisher,Oxford University Press +General,What are the two stone lions in front of the new york public library,Patience and fortitude,General,"What george harrison lp featured the single ""give me love""",Living in the,General,What did the republicans call the platform they hyped in the 1994 congressional elections,Contract with america +Music,"Which Two Journalists Co Wrote The Book ""The Boy Looked At Johnny"" The Orbituary Of Rock & Roll",Tony Parsons & Julie Burchill,General,What late night news show became popular in the eighties after the Iranian Hostage takeover?,Nightline,General,"Which American, who died in 1910, wrote ""Water - taken in moderation - cannot harm anybody""",Mark twain + Geography,What is the highest mountain in Europe?,Mont Blanc, History & Holidays,What Was The Name Of Scotland's First King? ,Kenneth ,Technology & Video Games,"What's the first video game ever to contain an ""Easter Egg""? ",Adventure +General,"What kind of music imitates the fanfares, drum rolls, and commotion of a battle",Battaglia,General,What state is 'the golden state',California,General,License plates: what does do loop do for a living,Computer programmer +General,What is the fear of narrow things or narrow places known as,Stenophobia, History & Holidays,"""The carol 'Silent Night' was first played on what instrument? """"Harpsichord,Guitar,Pipe Organ, Piano"""""" ",Guitar ,General,What shoe company has the slogan no slogans,Reebok +General,Which song did aretha franklin sing in the original 'blues brothers' film,Think,General,"What did the Greeks call Roman goddess, minerva",Athena,General,There are 625 sweat glands in what area of human skin,One square inch + Geography,The longest river in Western Europe is _________?,Rhine,Toys & Games,What is the most popular sport in england?,Darts,Geography,What Is The Capital Of Australia ,Canberra  +General,At what age does a filly become a mare and a colt become a horse,Five years,General,In which building in Washington D.C. does the United States Congress meet,The capitol,Food & Drink,How Is Goose Known In France ,Foie  +General,"Which American president said, 'The only thing we have to fear is fear itself",F d roosevelt,General,Mary Ann Nichols was the first - the first what,Victim of Jack the Ripper,General,What famous classical composer continued to compose great music after becoming deaf,Beethoven +Geography,What Is The Airline Of Luxembourg ,Luxair ,General,Who was the first person to be buried in London's St. Paul's Cathedral,Sir christopher wren,General,Who wrote the series of Palisair novels,Anthony Trollop +General,In the US flamingos are only outnumbered by what similar thing,Plastic flamingos, History & Holidays,"After years of flirtation, the U.S. began direct military involvement in Vietnam in what year? ",1964 ,General,"In the USA, what is an estate agent known as",Realtor +General,Game in which participants simulate military combat using airguns to shoot paint capsules at each other,Paintball,Music,Name the band whose debut album is titled 'Suck on This'.,Primus, History & Holidays,In What Year Did Mikhail Gorbachev Become Leader Of The Soviet Union ,1985  +General,"Which TV Show Has The Theme Tune ""The Best Of Both Worlds""",Hanah Montana,Music,Which 2 Members Of The Group 10CC Left To Form A Duo,Kevin Godley & Lol Creme,General,What is the flower that stands for: despair,Cypress and marigold +General,Which U.S. singer is nicknamed the 'Queen of Soul',Aretha franklin,General,What job did Ernest Hemmingway do in WW1,Ambulance Driver,Science & Nature,Why Does The Woodpecker Peck Wood? ,To Get Insects Below The Bark  +Sports & Leisure,What Is A Petanque Better Known As ,Boules ,Music,In Which Australian Film Did Mick Jagger Appear In 1970,Ned Kelly,Geography,Kingston is the capital of which country,Jamaica +Science & Nature,What engery does an Eolic power station?,Wind Power, History & Holidays,The Battle of Rorke's Drift in 1879 featured in which war? ,Zulu War (or Zulu Wars). ,General,Japanese hi tec toilets auto wipe buts using what,Lasers +General,Nanook is a Canadian word for what animal,Polar Bear,General,At a Quaker wedding it is forbidden to do what,Take photographs,General,What links Bob Hope John Huston Ryon O'Neil Bo Diddley,Boxers early in life +General,Which country invented the bedsprings,Greeks,General,What is the only 15 letter word that can be spelled without repeating a letter,Uncopyrightable,General,"Complete the line: ""step on a crack""",Break your back + History & Holidays,Which police-dog was one of Stalin's favourites?,Berya,Food & Drink,What the V.O. in Seagrams V.O. stands for?,Very old,General,"Which number psalm begins, 'The Lord is my Shepherd I shall not want'",23rd +General,What is the name given to a cocktail of rum and lime juice,Daiquiri,General,"Which company, based at Westgate Brewery, Bury St. Edmonds, produces Abbot Ale",Greene king,General,Who owns: Rise shave lathers,Carter wallace +General,Which great Asian river flows into the sea near Karachi,The indus, History & Holidays,Who Brought Back Tobacco And Potatoes From The Americas? ,Sir Walter Raleigh , History & Holidays,Canadian Prime Minister: Pierre Elliott __________.,Trudeau +Food & Drink,The Popular Breakfast Meat Bacon Comes From Which Type Of Animal ,Pig ,Science & Nature,What type of rock is marble?,Metamorphic,Sports & Leisure,Who Did David Gower Overtake As England's Most Prolific Run Scorer In 1992? ,Geoffrey Boycott  +General,What is tattooed on glen campbell's arm,Dagger,General,"Triassic, jurassic, & cretaceous are three periods during which era",Mesozoic, History & Holidays,"In the period 978-1016 England was ruled by which ""Unready"" king ?",Ethelred +General,What does 'jejune' mean,Dry, History & Holidays,Who had a Message To Rudy? ,The Specials ,General,Obesophobia is the fear of,Gaining weight +General,What Lake is the biggest in europe?,Ladoga,Science & Nature,What phenomenon is caused by the gravitational attraction of the moon?,Tides,General,After who is the 'Ramses' brand condom named?,Pharaoh Ramses II +General,What loaded gaming devices were found in the ruins of Pompei,Dice,Science & Nature,What Is Sin? A+ Cos? Equal To? ,1 ,General,"A movement that developed in the 1920s, characterized by a regularized surface, a lightening of mass, and often large expanses of glass. ",International style +Science & Nature,What planet position is the Earth from the Sun?,3rd,General,In WW2 what sort of weapon was a kaiten,Japanese manned torpedo,General,There are more in Los Angeles than in all France - what,Judges +General,"Barnacle, Canada and Brent are all types of what",Geese,Science & Nature,What is the atomic number of Molybdenum?,Forty two,People & Places,Which Daughter Of A Lighthouse Keeper Rescued Survivors From A Shipwreck ,Grace Darling  +General,What should you ask for in the U.S. if you want jam,Jelly,Music,How Old Was Leann Rhymes When She Had Her First Number One Country Album,Thirteen,Music,Break Machine Had 3 Hits In The Mid Eighties Name Two Of Them,"Street Dance, Breakdance Party, Are You Ready" +Music,"Robin Zander Was A Member Of ""Cheap Trick"" Or ""Chicago""",Cheap Trick,General,Charles Lindbergh took only four of these to eat with him on his famous transatlantic flight.,Sandwiches,Entertainment,What was Rocky's nickname in the ring?,The Italian Stallion +General,Which Boxwer Carried The Olympic Flame Into The Stadium At The 1996 Atlanta Games,Evander Holyfield,General,The study of sound is _________.,Acoustics,Geography,What city is the Kremlin located,Moscow +General,Who failed his music class at school,Elvis Presley,General,Which Punjabi city is famous for its Golden Temple,Amr1tsar,General,"Which was the 1st winner of the academy award for best picture, and the only silent film to achieve that honor",Wings +General,What is the name of the knot which is used to make a fixed loop that should not slip or jam,Bowline, History & Holidays,Who Released The 70's Album Entitled Machine Gun Etiquette ,The Damned ,Science & Nature,"What is the name of the lowermost portion of the earth's atmosphere, where clouds occur? ",The Troposphere  +General,What is the Capital of: Singapore,Singapore,Music,Bobby Farrell was the only male member of which chart topping group?,Boney M,Science & Nature,How many teeth does a walrus have?,Eighteen +General,Which African country had its capital transferred to Dodoma,Tanzania, History & Holidays,Who was defeated at the Battle of Little Bighorn?,George A. Custer,Entertainment,Alvin & Simon had a brother called ____.,Theodore +People & Places,In Which Us State was John F Kennedy Shot ,Texas ,General,"In The World Of Music How Is ""Johnny Allen"" More Commonly Known",Jimi Hendrix,General,This racist organization was formed in tennessee in 1865,The ku klux klan +People & Places,Which Famous Woman Always Carried Pet Owl In Her Pocket ? ,Florence Nightingale ,Science & Nature," Canned herring were dubbed __________ because the canning process was first developed in Sardinia, Italy.",Sardines,General,Which Oliver Stone film won the Oscar for the best film in 1986,Platoon +General,Christobal Colon is better known as who,Christopher Columbus,Science & Nature,Where Might You Find A Hammer And Anvil In The Human Body ,The Ear ,General,In England what year was the Lynmouth flood disaster,1952 +General,Avron Hirsch Goldbogen changed his name to what,Mike Todd Married Liz Taylor,General,"Which best selling car with a production spanning some 30 years is to be replaced by the ""Focus""",Ford escort,General,In Shakespeare's Hamlet what herb is said to be for remembrance,Rosemary +General,"Where would you find a canton, halyard and field",On a Flag,General,Astana Is The Capital City Of Which Asian Country,Kazakhstan,General,Duffy: A Confederacy of Dunces,John kennedy toole +General,Where is the wabash river,Indiana,General,"Whose show claimed to have ""the world's most dangerous band""",David letterman,General,By law who require a cert. of health before entering Kentucky,Bees must have one +General,You could be executed for drinking what in ancient Turkey,Coffee,General,Which of Shakespeare's plays contains the most number of words,Hamlet,General,"Backfall, diapason, pallet, gamba, sticker all parts of what",Pipe Organ +General,Which sportswear company was founded in Germany by a certain Dr. Dassler,Adidas,General,Chaucer's Canterbury Tales were written in which century,Fourteenth,General,"Who Wrote The Autobiography ""Don't Laugh At Me""",Norman Wisdom +General,What was lestat's last name,De lioncourt,Music,Which Beatles Song Gave Billy Bragg His Only Uk No.1,She's Leaving Home,General,When did Baden Powell found the Boy Scout movement,1907 + Geography,What US Citys name means 'straits' or 'channel'?,Detroit,General,What is a line of columns supporting a horizontal or arched superstructure,Colonnade,Food & Drink,Which Chain Of Restaurants Have The Nickname 'The Golden Arches'' ,Mc Donalds  +General,"What mega group had the hit ""black dog""",Led zeppelin,General,What season is it in Australia when Santa Claus drops in,Summer,Art & Literature,Frodo is chosen to deliver The Ring into the heart of what,Mount doom +General,When was flavored soda pop invented,1807,General,"Eggplant is not a vegetable, but is actually a member of what plant family",Thistle,General,Staurophobia is the fear of,Crosses the crucifix +General,In what country was Mother Theresa born,Albania,General,To whom does the island of St Helena belong,United kingdom,General,The Kina is the unit of currency in which country?,Papua New Guinea +Sports & Leisure,The Duckworth Lewis Method Is Used To Keep The Score In Which Sport ,Cricket ,Music,Which Country Does Enya Come From,Ireland,Science & Nature,What Is Measured In Decibels ,Sound Intensity  +Music,"Which Family Group Had Hits On The Stax Label With ""Respect Yourself"" & I'll Take You There",The Staple Brothers, Geography,What is the basic unit of currency for Seychelles ?,Rupee,General,For her performance in which film did Jane Wyman win the 1948 Best Actress Oscar,Johnny belinda +General,What is the flower that stands for: argument,Fig,General,In 1951 these were invented what were,Disposable diapers – nappies,General,In Arizona you must register with state before becoming what,Illegal Drug Dealer +General,"Who Became The Youngest Ever Member Of The ""GLC"" In 1969",Jeffrey Archer,General,Are yoU.S.tanding or sitting when you put your coccyx on the floor,Sitting,Science & Nature, The largest species of seahorse measures __________,8 inches +General,What is the national symbol for india,Lotus flower,General,Which film finds Jack Nicholson in the Los Angeles oriental district,Chinatown,General,Who or what lives in a formicarium,Ants +General,A couple dogging are having sex with others watching where,In their car, History & Holidays,In What Year Did India Gain Independence Marking The Start Of Decolonisation ,1947 ,General,Victor Barna was world champion five times at what sport,Table Tennis +Music,Who Were Inducted Into The Rock & Roll Hall Of Fame In 1993 Two Years After Their Biopic,The Doors,General,"Which london station handles trains directly to the continent, through the channel tunnel",Waterloo,General,Hippopotomonstrosesquippedaliophobia is the fear of what,Big words +General,What does bbs mean,Be back soon,Science & Nature,This is the only mammal with four knees.,Elephant,General,Where would one have borscht,Poland or russia +Geography,What city is on Lake Erie at the mouth of the Cuyahoga River,Cleveland,Sports & Leisure,"In Which Sport Did Britains Nigel Short, Compete For The World Championship in 1991 ",Chess , History & Holidays,What particular gimmick was used in the 1958 film House of Wax ,It was made in 3D  +Music,"Which British Singer Produced The First Album By The Specials & ""Red Roses For Me""",Elvis Costello, History & Holidays,What was the fight between Argentina and Great Britan over? ,Falkland Islands ,General,What is the unit of Imperial measure equal to 1/10 of a nautical mile,Cable +General,Who is the Barber of Seville,Figaro,General,"Nunaat Island, southern Indonesia, one of the Lesser Sunda Islands, in the Indian Ocean",Bali,General,Which island's roads are paved with coral,Guam +General,Which countries leader was an extra in Hollywood,Fidel Castro,Geography,Which Country Has The Longest Coastline ,Canada ,Science & Nature,What Is Toxicology The Study Of? ,Poisons  +General,What is the first capital of laos,Luang prabang,General,"The blesbok, a south african antelope, is almost the same color as",Grapefruit, History & Holidays,What's 'Stir-Up Sunday'? ,The Day To Make Xmas Pudding  +General,A speed stick measure the speed of what,Cricket balls,General,Who received 800000 fan letters in 1933,Mickey Mouse,General,What is a group of this animal called: Crow,Murder +Science & Nature,What is the study of the composition of substances and the changes they undergo?,Chemistry,Geography,____________________ has 570 miles of shoreline.,New york city,General,What is the Capital of: Bulgaria,Sofia +People & Places,By what name is Francis Gumm better known as?,Judy Garland,General,What did the Israelites eat in the desert after the exodus,Manna,General,"In which organ is a clear watery solution known as the ""aqueous humor"" found",Eye +General,Which English book was written without using the letter 'E' once ?,A Void,Music,What Is The Singers Bjork Nationality,Icelandic,General,What city's magazine broke the Iran contra scandal in a 1986 article,Beirut +General,Which film star has his statue in Leicester Square,Charlie Chaplin,General,By Law in Tulsa you need a licensed engineer to open what,Soda Bottle,General,Which annual world championship is held at Coxheath Kent,Custard Pie throwing +General,What part of the body has a thin drum like membrane stretched across a tube,Ear, Geography,What is the largest ocean?,Pacific Ocean,General,Where would you find a crossjack and a spanker,Sailing ship there sails +General,The Passion Play is performed every 10 years where,Oberammergau,General,What famous building is located on the banks of the river Jumna?,Taj Mahal,General,What kind of car was the General Lee in Dukes of Hazard,Dodge Charger +Food & Drink,What Is The Swedish Name For A Hot Or Cold Food Served As A Buffet? ,Smorgasbord ,Sports & Leisure,Which Country Is Set To Host The 2010 World Cup Finals ,South Africa ,General,"Which classic French dish contains chicken, bacon and red wine",Coq au vin +Music,Apollo 9 Was A Hit In 1984 For Whom,Adam Ant,Music,"""Planet Earth"" Was Duran Duran's First Hit, But What Was Their First Top Ten Entry",Girls On Film,Geography,What direction does the nile river flow ,North  +Food & Drink,What Are Globe And Jerusalam Both Examples Of ,Artichoke ,General,Carly Simon sang the theme song to which James Bond film,The spy who loved me,Geography,What is the capital of Vietnam,Hanoi +General,Which Game Was Was Named After The Latin Name For A Hawk,Subbuteo,Science & Nature,What is the meaning of the name of the constellation Canes Venatici ?,Greyhounds,General,What was the home of Douglas Fairbanks and Mary Pickford called,Pickfair +Sports & Leisure,At Which Sporting Venue Would You Find Melling Road? ,Aintree Race Course ,General,What is a one humped camel called,Dromedary,Science & Nature, The smallest frog is the Gold frog (Psyllophryne Didactyla) of __________. It grows to only 9.8 mm (3/8 inch).,Brazil +General,Which sauce or paste is found in the Japanese dish Sashimi?,Horseradish,General,"What did Lyndon Johnson declare war against on January 8, 1964",Poverty,General,Cockney Rhyming Slang: trouble and strife,Wife +Science & Nature,What did Lewis E. Waterman invent in 1884,Fountain pen, History & Holidays,What did pre-Christian pagans call Xmas time ,Yule ,General,"In ballet, the third and final part of the classical pas de deux.",Coda +Music,What Is The Singer Prince's Real First Name (He He He),Prince,Geography,What Is The Oldest Inhabited City In The World ,"Damascus, Syria ",Science & Nature, The rear portion of the head of a horse is called the __________,Poll +Science & Nature,What Is The Largest Cell ,The Ovum , Geography,What is the capital of Dominican Republic ?,Santo Domingo,General,Where was napoleon born,Corsica +Geography,What Is The Deepest Lake In England ,Wastwater , History & Holidays,"Which Group Had 3 Consecutive Christmas Number One's In 1996,1997,1998 ",The Spice Girls , History & Holidays,Who played the policeman in The Blue Lamp? ,Jack Warner  +General,"On Three's Company,what city did the trio live in?",Santa Monica, History & Holidays,"Which Christmas Carol, tells the tale of a 10th Century Duke of Bohemia ",Good King Wenceslas ,General,For what song did country & western singer marty robbins win a grammy?,El paso +General,What is the stage name of actress Demetria Guynes,Demi Moore, Geography,In which continent would you find the Congo river ?,Africa,General,What has yale college been serving for more than 100 years,Apple pie +General,What was Heindrich Himler's job before lead Gestapo in WW2,Chicken Farmer,General,What is the fifth most popular meal ordered in sit down restaurants in the U.S.,Baked ham,General,In 1990 there were 99 public executions Suadi Arabia - Drugs How,Beheading +General,Which band did david bowie and the 'sons of soupy sales' form,Tin machine, History & Holidays,James Dean died during the filming of which film in 1955? ,Giant ,General,"In alphabet radio code, what word is used for 'x'",X ray +Food & Drink,What is it called when fat and juices from the roasting tin are spooned over meat while it is cooking? ,Basting ,Music,"Who Had A Hit With ""She Drives Me Crazy""",Fine Young Cannibals,General,What swimming stroke is named after an insect,Butterfly +Music,Bryan Ferry Was Originally the Lead Singer Of Which Band,Roxy Music,General,In Which Country Will You Find The Highest Concentration Of Japanese People Outside Japan,Brazil,Geography,What city is served by tempelhof airport ,West berlin  + History & Holidays,What animal is Snowball in George Orwell's book Animal Farm? ,A pig ,General,What were the first tennis balls stuffed with,Human Hair,Science & Nature,What name is given to animals which only eat meat ?,Carnivore +General,Kwame Nkrumah was the first president of which country,Ghana,General,Name the word an anti mine device towed from bows of a ship,Paravane,General,What gift is given on behalf Saudi Arabia King to Mecca pilgrims,Small Bottle Extra Virgin Olive Oil +General,What is the hardest bone in the human body,Jawbone,General,What is banned by law in Japanese restaurants,Tipping,General,Who played the pawnbroker in the film of that name,Rod Stiger +Science & Nature, Gorillas do not know how to __________,Swim,General,What is the official language of Mozambique,Portuguese, History & Holidays,Which Saint's Day is celebrated on Boxing Day ,St Stephen  + Geography,What is the basic unit of currency for Poland ?,Zloty,General,Which country owns the Hen and Chicken islands,North island New Zealand,General,What is used as an antidote for poison arrows and as a thickener in cooking,Arrowroot + History & Holidays,Who was the President of Egypt in 1956 who nationalized the Suez Canal causing British and French troops to invade the region ,Gamal Abdul Nasser ,General,Brisbane is the state capital of which SE Australian state,Queensland,General,What is Venezuela named after,Venice +General,On 17 th Nov 1970 German “Stephanie Rahn” Became The First Woman Ever To Do What???,Topless Page 3 Model,Geography,In Which European Capitaal Is The Famous Mankin Pis? ,Brussels ,Music,How Were Showaddywaddy Discovered,On The TV Show New Faces +General,What is the fear of ghosts known as,Phasmophobia,General,Zorro was the secret identity of what wealthy landowner,Don Diego De La Vega,General,The mathematical notation for a product is designated by what greek letter,Pi +General,Which island is also known as the apple isle,Tasmania,Geography,Havana is the capital of which country,Cuba,General,Samhainophobia is the fear of,Halloween +General,What is the only Shakespeare play that mentions America,The Comedy of Errors (Act III Scene ii),Music,"In which Year Did George McCrae Release His No.1 Hit ""Rock Your Baby""",1974,General,What is a Gambrel - One of two answers,Sloping Roof or Butchers Hook + History & Holidays,Which Instrument Of Torture Had a Hinged Case Full Of Spikes? ,The Iron Maiden ,General,Pogonophobia is a fear of ______,Beards,General,Sun Which U.S. president bought a place in Colorado to ski during vacations,Gerald +Music,"Mary Hopkin's ""Those Were The Days"" & The Beatles ""Hey Jude"" Were Early Hits For Which Label",Apple Records,General,What country celebrates its National Day on 12th October?,Spain,Geography,What country owns the island of Corfu,Greece +General,Which character was portrayed by Robert Redford in the film Out of Africa,Dennis finch hatton,General,"The large optics diamond turning machine has severed a single human hair, lenghtwise, how many times",3000,General,Where is Stone Mountain?,Atlanta +General,What does britain lose the lease on in 1997,Hong kong,General,"If yoU.S.aw the word 'aloo' on an Indian menu, which vegetable would it stand for",Potato,Science & Nature,What is the unit used to measure supersonic speed ,Mach  +General,William Hurt won best actor Oscar for which 1985 film,The Kiss of the Spiderwoman,General,California is to Eureka as New York is to,Excelsior,Geography,Which element makes up 27.72% of the Earth's crust,Silicon +Geography,In which country is the Great Victoria Desert,Australia,General,What was karl marx's term for wage laborers in modern capitalist systems,Proletariat,Food & Drink,Which Are Larger Winkles Or Whelks ,Whelks  +Sports & Leisure,Which Japanese Martial Art Uses Bamboo Swords? ,Kendo ,General,Where would you see analog watches showing 10.10,On most watch advertisements,General,What is a group of parrots,Company +Science & Nature,A piece of glass that separates light into the visible spectrum is called a _____.,Prism,General,In your body where is the macula,Eye centre of the retina,Geography,What is the capital of Namibia,Windhoek +General,"Every person generates approximately 3.5 pounds of rubbish a day, most of it being ______",Paper,General,What does the Greek word eureka mean,I have found it,General,On Average North American women do what 83 times a year,Have Sex +General,"Like fingerprints, what other print is individual",Tongue,Music,"Which Of These Shadows Records Was Not A No.1 Apache, FBI, Kon Tiki, Dance On, Foot Tapper""",FBI,Geography,Name the expanse of water between New Zealand's North and South Islands.,Cook strait +General,November 28th 1948 what was the first TV western in the USA,Hopalong Cassidy,General,"On the Bullwinkle Show, what was Boris's last name",Badenov, History & Holidays,In 1952. 'Elizabeth Becomes Queen'. Who was her late father? ,George VI  +General,Facility of stopping a videotape in order to wiew a motionless image,Freeze-frame,General,In the original Wizard of Oz what colour were the slippers,Silver,Food & Drink,How is a herring turned into a kipper? ,Soaked (in brine) & smoked (over an oak fire)  +General,What year was the first commercial opera house opened,1637,Sports & Leisure,In Which Sport Could You Take Long And Short Corners? ,Hockey ,General,What does a person suffering from kleptomania want to do,Steal +General,"Which Chart Topping Diva Was Born ""Yvette Marie Stevens"" In 1953",Chaka Khan,General,What religious movement did joseph smith found,Mormonism,General,Names - Baker - Cook Smith easy - what did a Chandler do,Make Candles +General,Do your pores open or close when your body is hot,Open,Music,"How Are ""Rod Stewart, Ron Wood, Ian McLagan, Ronnie Lane & Kenny Jones"" More Commonly Known",The Faces,Entertainment,Who directed '2001: A Space Odyssey' and 'A Clockwork Orange'.,Stanley Kubrick +Science & Nature,Meteorology is the study of ______?,Weather,General,Which year was the St. Valentines Day Massacre,1929,General,Who had the vision that resulted in the book of revelations,John +General,Who would use a hammer & tongs,A blacksmith,General,"What is the nickname for Allentown, Pennsylvania",Cement city,General,"What song is dire straits singing about 'that ain't working, that's the way you do it, get your money for nothing, get your ______'",Chicks for free +General,"Who appears on the 100,000 dollar (US) note?",Woodrow Wilson,General,What is a group of coots,Cover,General,Which stone fruit has a kernel that can be used as a flavouring,Apricot +Food & Drink,"Cocktails: Rum, lime, and cola drink make a(n) ____________.",Cuba libre,General,What Boston craftsman made George Washington's false teeth,Paul revere,General,What occupation had the most fatal work injuries in US in 1994,Truck Drivers +General,Who was said to have used a monocle made of emerald to improve his vision at the arena,Emperor nero,General,Jean Vander Pyl Who Died In 1999 Aged 79 Provided The Voice Of Which Well Known Cartoon Character,Wilma Flintstone,General,This animal can't jump?,Elephant +Geography,Name the large mountain chain in the eastern u.s.a. ,The appalachians ,General,"Joe Frazier Was The First Ever Boxer To Defeat Muhammed Ali , But Who Was The 2nd?",Leon Spinks,General,Who is Prime Minister of Canada,Jean chretien +Music,Which Lennon & McCartney Number Was A Hit For The Rolling Stones In 1963,I Wanna Be Your Man, History & Holidays,"The official song and anthem of the state of oklahoma is ___,composed and written by richard rodgers and oscar hammerstein ",Oklahoma ,General,Pine Eyes is the literal translation of which characters name,Pinocchio + History & Holidays,How many times was franklin roosevelt elected president ,Four ,General,What does gastritis affect,Stomach,Sports & Leisure,In 1994 Which West Indian Batsman Scored 501 Not Out In One Innings ,Brian Lara  +General,"Before she was a pop star, why was Samantha Fox in the British tabloids?",She was a page three girl,General,In Which Month Of The Year Does The Superbowl Take Place,January,General,Never look a gift horse in the ___.?,Mouth +General,What argentinian boxer was shot dead outside a nevada brothel in may 1976,Oscar bonavena,Geography,What is the capital of Estonia,Tallinn,Entertainment,Paul Mccartney played these instruments?,Bass guitar and piano +General,20s Robert 50s Robert 70s Michael what US top boys name 90s,Michael,General,Dresses first incorporated this in the 1930's,Zipper,General,What grew when the doll 'growing up skipper''s arm was turned,Breasts +General,Which creatures transmit Bubonic Plague,Rat fleas,General,"How is ""Maria Kalageropoulos"" better known",Maria callas,General,What does the sun in SUN Microsystems stand for,Stanford University Network +General,What fluid lubricates the eye,Lacrimal fluid,General,What Italian city is the Monza grand prix held in,Monza,General,What indiana city did steve martin once call 'nowhere u.s.a',Terre haute +General,What does the abbreviation a.m. stand for,Ante meridian, History & Holidays,Who are the four ghosts in Charles Dickens' A Christmas Carol? ,"Christmas Past, Present, Yet to Come, and Jacob Marley ",General,Who was crowned King of England on Christmas Day 1066,William the conqueror +General,In a survey what food did Americans say they hated most,Tofu,General,"The two rival gangs in ""west side story"" were the sharks and the _________",Jets,General,What was the title of Oliver Cromwell when he was head of the Commonwealth,Lord protector +General,What was the name of jacques cousteau's research ship,Calypso,General,"The fastest bird is the _____ ______ _____, clocked at speeds of up to 106 miles per hour",Spine tailed swift,General,Isopterophobia is the fear of,Termites +General,What is the fear of myths or stories or false statements known as,Mythophobia,General,A vaulted roof of circular or polygonal shape.,Dome,Food & Drink,What fruits are usually served 'belle helene'?,Pears +General,What is the fear of speaking known as,Laliophobia,General,25 points were given to a laff-alympics team for placing first in a given event. How many for placing second,Fifteen,Food & Drink,Where Might You Encounter A Balti ,At An Indian Restaurant Or Meal  +Music,"Which Late Great Singer Was Christened, Ellen Naomi Cohen",Mama Cas,General,Frampton with what vegetable song did dee dee sharp score big,Mashed potato time,General,What was the Italian Umberto Nobile first to do in 1926,Airship Crossing North Pole +General,In the world of communications for what do the letters U R L stand for,Uniform resource locator,General,In which Fritz Lang film of 1926 do impoverished workers live beneath a city,Metropolis,General,What Opera's story is about a female cigar factory worker,Carmen +General,What continent is sierre leone in,Africa,Art & Literature,"A movement that began in Britain and the United States in the 1950s. It used the images and techniques of mass media, advertising, and popular culture, often in an ironic way.",Pop art,General,Harrods was the first UK store to install what,An escalator +General,What is the largest city in canada,Toronto,General,This cereal is the only one that features a frog as its mascot,Sugar smacks,General,St John the Divine wrote which book of the Bible,Revelations +Geography,What is the capital of Saint Lucia,Castries,General,What was Charles Dickens last (unfinished) novel,Mystery of Edwin Drood,Art & Literature,"Movement in painting, originating in New York City in the 1940s. It emphasized spontaneous personal expression, freedom from accepted artistic values, surface quallities of paint, and the act of painting itself. ",Abstract expressionism +General,When was Sean Connery born,1930,General,Superstition if a woman sees a robin Valentines day marry who,Sailor,General,Who wrote the Earth's children series,Jean manuel +General,Which famous artist painted The Potato Eaters,Vincent van gogh,General,"In ""alice through the looking glass"", who played humpty dumpty",Jimmy durante,General,Where would you be most likely to see a 'gazebo',Garden +General,What is the capital of Venezuela?,Caracas,General,"A commonly used process for oxide and nitride deposition, CVD, stands for:",Chemical vepor deposition,General,What is the Capital of: Niger,Niamey +General,Who wrote the series of novels with the hero Sharp,Bernard Cornwell, Geography,What is the capital of Maldives ?,Male,General,What is the flower that stands for: regard,Daffodil +Entertainment,"In the film 'American Hot Wax', who played the 'Mookie'?",Jay Leno,General,Which country has the largest orthodox church,Russia,General,Which dance in 2/4 time originated in Bohemia in the early 19th century,Polka +Science & Nature,What period takes place shortly after chilbrith?,Postpartum,General,What is the main language of Liechtenstein,German,General,In the Winnie the Pooh books what name is over Poohs door,Mr Sanders +General,Which actress played the Bond girl Honeychile Rider in Doctor No,Ursula andress,Sports & Leisure,With which sport is Bobby Moore associated ?,Soccer,General,What is a nephron,Filtering unit in the kidney +Sports & Leisure,Which tycoon failed to buy Manchester United in 1998? ,Rupert Murdoch ,General,What natural hydrocarbon polymer comes from the hevea brasiliensis tree,Rubber,Science & Nature,What Is Measured Using The Troy System ,Precious Metals  +General,What is the fear of decaying matter known as,Seplophobia, Geography,What is the capital of Bulgaria ?,Sofia, Geography,With what country is Fidel Castro associated?,Cuba +General,George washington carver advocated planting what to replace cotton and tobacco,Peanuts and sweet potatoes,General,Who was the 26th president of the U.S.,Theodore roosevelt,General,In the US women own 35% of what,All businesses +General,In 1934 Percy Shaw Came Up With An Invention That Is Still In Use Today What Was It?,Cats Eyes,General,In 2000 what state removed confederate flag - statehouse dome,South Carolina,People & Places,As Of 2000 In Britain John Is Still The Commonest Boys Name What Is The Girls ,Elizabeth  +General,What is the abnormal fear of feathers known as,Pteronophobia,General,From what is banana oil made,Petroleum,General,Those born on April Fools day are what star sign,Aires +General,What can you shag in Georgia but its illegal in Florida,A Porcupine,General,A cat is feline - what animal is murine,Mouse or Rat,General,"You get a shiver in the dark, it's raining in the park ___' What's the Dire Straits song title",Sultans of Swing +General,Who founded the Boy Scouts?,Lord Baden Powell,General,Game or fly fishing is for salmon and which other fish,Trout,Music,Name The 1983 ZZ Top Album Featuring The Car Of The Same Name On The Cover,Eliminator +Science & Nature,The koala bear eats the leaves from this tree.,Eucalyptus,General,What gives piggy banks their name,Pygg - type of clay their made from,General,Who served 27 years in prison for sexual offenses,Marquis de sade +People & Places,Which Actor Was Charged With Lewd Behavior In 1995 ,Hugh Grant ,Music,"From the 1970's, which song and which group “Listen to the ground: there is movement all around, There is something goin' down and I can feel it, On the waves of the air, there is dancin' out there”?",The Bee Gees / Night Fever,General,In The 1980's Which Actor Became Motown's Biggest Selling White Artist,Bruce Willis +General,"He was the worlds most prolific inventor in the 1970's and 1980's with inventions such as Calculators, Digital Watch, Home Computer and Pocket Televisions?",Sir Clive Sinclair,General,Cavalier in French Springer in German what in English,Chess Knight,Geography,"Savannah, ________________ was founded in 1733 as a haven for British debtors.",Georgia +Science & Nature,What Is The Most Common Element On Earth ,Oxygen ,General,Crossword Clues: On the sheltered side (4),Alee,General,Which fictional character was introduced in A Study in Scarlet,Sherlock holmes +General,What do you get when you mix blue and yellow?,Green,General,"From Which Country Does The Energy Drink ""Red Bull"" Originate",Thailand,General,Which of Shakespeare's plays is set in the Forest of Arden,As you like it +General,What's the capital of ecuador,Quito,Art & Literature,Which Poet Preceded Ted Hughes As Poet Laureate ,Sir John Betjeman ,General,Name the author of 'the pumphouse gang',Tom wolfe +General,Which two fighting ships other than the 'arizona' were sunk at pearl harbor,Oklahoma and utah,General,In the first voyager program who were the Maquis fighting,The Cardassians,General,Mizaru Mikazaru and Mazaru are better known as who,Three Monkeys +General,What are lentigines,Freckles,General,What symbolises the election of a new Pope,White smoke,General,What U.S. spacecraft landed on the planet mars in 1976,Viking 2 +General,A female fox is a vixen what is the male fox called,Dog,General,What is the fear of parasites known as,Parasitophobia,General,What fast-food chain was named for brothers Forrest and LeRoy Raffel in 1964,Arby's +General,Which leader died in St Helena,Napoleon Bonaparte,General,What is the world's highest waterfall,Angel falls,General,It is the region where most of electrons are apt to be found,Orbital +General,In which European country is gambling illegal,Sweden,General,Who was the Chief Designer of the Supermarine Spitfire,Reginald mitchell,General,"Which group sang the Song ""The Call""?",Backstreet Boys +General,In which everyday household products would find alkyl benzene sulphonate,Detergents,General,What is the oldest college in the U.S.,Harvard,General,Which country was the proposed target in the German 'Operation Tannenbaum' in World War II?,Switzerland + Geography,Which element makes up 27.72% of the Earth's crust ?,Silicon,Geography,What is the capital of Guyana,Georgetown,General,In Tampa Bay Florida its illegal who who/what to leave ships,Rats +General,What is the Western ( cowboy ) name for a motherless calf,Dogie,General,Two former members of what group formed hot tuna,Jefferson airplane,General,Who painted the picture Mr. and Mrs. Andrews circa 1750,Gainsborough +Science & Nature, An average_size __________ weighs about 150 pounds.,Aardvark,General,"What was the name of the cheaply made, ultra popular car prevalent in East Germany during the Cold War years?",Trabant,General,Which country had the guns of Naverone installed,Turkey +General,"What is the dish made from peppers, aubergines, courgettes and tomatoes called",Ratatouille,General,In Mississippi it is still legal to kill who,Ones Servant,General,"The Canadian province of ____ ______ leads the world in exporting lobster, wild blueberries, & christmas trees",Nova scotia +General,Which chemical element is named after the 1959 winner of the Nobel Prize for Physics,Laurencium,Music,"Which 1983 Song, Shot To The Top Of The UK Charts After Mike Reid Refused To Play It On His Radio Show Due To It’s Sexual Content",Frankie / Relax, History & Holidays,Which Roman Emperor Followed Claudius? ,Nero  +General,"How many children did JR Ewing knowingly have that lived on ""Dallas""?",3,General,What does the entire economy of the island of Nauru depend on,Bird shit - Guano fertiliser,General,The Bovespa is the stock exchange in which country,Brazil +General,What is a goup of clams,Bed,General,High speed passenger train is called a,Bullet train,Sports & Leisure,The Colours of the All England Lawn Tennis Club are green and what else ,Purple  +Religion & Mythology,"In Egyptian mythology, who was Isis the wife of?",Osiris, History & Holidays,Who starred in the 1954 Musical remake of 'A star is born' ? ,Judy Garland ,General,What is Radar from MASH home town,Ottumwa - Iowa +General,Which wedding anniversary is wood,Fifth,General,Who plays dawson leary on 'dawson's creek',James van der beek,General,Corzetti is what shaped pasta,Coin shaped +General,Why did Roselin Franklin (pre discovered dna helix) no Nobel,She was dead,General,U.S. captials Delaware,Dover,General,What European language is unrelated to any other language,Basque +General,Who invented carbonated soda water,Joseph priestley,General,Which is the largest of the National Parks of England and Wales,Lake district,Art & Literature,Which Painter Reputedly Cut Of His Own Ear ,Vincent Van Goch  +General,"What, according ot the saying, makes waste",Haste,Music,Elton John Is His Stage Name What Is His Real One,Reginald Dwight,General,What common phrase originated when 'bedsprings' were ropes woven through wooden bed frames and needed to be kept from sagging by using a key to stretch them,Sleep tight +General,What is a group of this animal called: Cow,Kine,General,What countries language is Magyar,Hungary,General,This order of insects contains the most species,Beetles +Art & Literature,Which Word Created By JK Rowling Gained Entry Into The Oxford English Dictionary In 2003? ,Muggle ,General,In what country was fashion designer Yves St Laurent born,Algeria,General,What is the main ingredient of the Indian dish dahl,Lentils +General,Who moved his 1960's variety series to miami,Jackie gleason,Music,Which City Not State Is The Home Of Motown Records,Detroit,General,In ancient China people committed suicide by eating what,Salt One lb will kill you + History & Holidays,Alexander the Great was king of which country ?,Macedonia,General,"Which foodstuff has types called ""blanket"" and ""honeycomb""",Tripe,Science & Nature,What Was The Main Competitor To VHS Video ,Betamax  +General,Which movie had a device known as a flux-capacitor?,Back to the Future,General,"What actress/dancer/politician went ""out on a limb""",Shirley maclaine,Music,In The UK Which Artist Became The First Act To Go To Number One On Download Sales Alone?,Gnarls Barkley's - Crazy +Sports & Leisure,What was Baron Pierre De Coubertin's contribution to sport? ,He founded the Olympic movement in 1892 ,General,Which is the highest peak in the Andes,Aconcagua,General,Savage garden took 13 nominations and 10 wins at which awards,Aria awards + Geography,What's the capital of Poland?,Warsaw,General,J K Rowling wrote the Harry Potter series what do the JK mean,Joanne Kathleen,General,Name the North African spicy dish of steamed semolina served with meat stew,Cous cous +General,Mrs Darell Waters (translated 128 languages) pen name,Edith Blyton,Entertainment,Who wrote the opera 'The Masked Ball'?,Giuseppe Verdi,General,Ordinary seaman Able Seaman what comes next,Leading Seaman +General,In The Caine Mutiny Bogart played Cap Queeg who first choice,Richard Widmark,Food & Drink,What type of pastry would be used for a jam roly poly? ,Suet ,General,What are terra cotta objects baked from,Clay +General,Which Australian state capital was named in honour of a British Prime Minister,Melbourne,Science & Nature,What Is The Largest Species Of Deer? ,The Moose ,General,Mark mcguire tied and went on to beat whose record of 61 home runs in one major league season,Roger maris +General,What country has the highest voting age 25,Andorra,General,"Who was the 4th child on ""Growing Pains?""",Chrissy,General,What links Fantasy Devils Coral and Christmas,Islands +Music,"Which Female Singer Married In 1969, Divorced In 1975, & Recorded One Last Album With Her Ex Husband In 1995",Tammy Wynette,Science & Nature,What Is Hydrophobia Better Known As ,Rabies , History & Holidays,This was the largest real estate deal in U.S. history.,Louisiana purchase +General,Noologists study what,The Mind,Music,"The Film ""Chances Are"" Featured A Song Called ""After All"" By Which Male Singer",Peter Cetera,General,What TV program first used the word hell,Star Trek lets get the hell outa here +General,What is the name given to the fortified gateway of a castle,Barbican,General,Who was Speaker of the House of Commons immediately prior to Betty Boothroyd,Bernard weatherill,General,Who painted The Haywain,John Constable +General,"Who said ""The reports of my death are greatly exaggerated.""?",Mark Twain,General,"Who said ""Iv'e never had an accident worth talking about""",Captain E J Smith of the Titanic,Music,What Links Boney M In 1976 And David Grey In 2000?,Babylon +General,What king dissolved the English monastries,Henry viii,General,Coco Channel a fashion star had what real first name,Gabrielle,General,What is classified on the Fujita scale,Tornados 1 to 5 +Entertainment,"A set of graduated steel bars set in a frame and hit with a hammer, used in the orchestra.",Glockenspiel,General,At which Paris terminus does the Eurostar train from London arrive,Gare du nord,Food & Drink,Kale is a variety of which winter vegetable? ,Cabbage  +General,Which actress starred in the original King Kong in 1933 (both),Fay Wray,General,What is the smallest lake in the world,Vanern,Food & Drink,"In Greek Mythology, what was the food of the Gods? ",Ambrosia  +General,What word do we get from shah mat,Checkmate,General,"One gram of 2,4 d, a common household herbicide can contaminate __________ litres of drinking water",Ten million,General,"How many legs do crabs, lobsters & shrimp each have",Ten +General,Who directed Dr Strangelove - 2001 - The Shining (full name),Stanley Kubrick,General,What country has two AK47 assault rifles on it's flag,Mozambique,General,"What Film Was Advertised With The Slogan ""Don't Give Away The Ending It's The Only One We Have""",Psycho +Music,"In Which Year Were The Following All UK Chart Hits: ""Solsbury Hill"" By Peter Gabriel, ""Silver Lady"" By David Soul And ""Pearl's A Singer"" By Elkie Brooks?",1977,General,Who invented painting by numbers?,Palmer paint company,General,What is 'tilapia' a type of,Fish +General,"In cornish folklore, what is a sea spirit that lives among fisherman",Bucca,General,Who formulated the laws which first explained the movements of the planets properly,Kepler,Religion & Mythology,Who was Hercules' stepmother?,Hera +Entertainment,What did George Harrison discover on the Witwatersrand?,Gold,General,Rod Temperton From Cleethorpes Is One Of The Richest Men In Music But What Is His Claim To Fame,He Wrote Micheal Jacksons Thriller,Music,What song did Lennon and McCartney give to the Rolling Stones in 1963?,I Wanna Be Your Man +General,When do children grow fastest,Springtime,General,What is the food of the secretary bird,Snakes,General,What were China 14 Raduga 14 Himwari 3,Orbiting satellites +General,What is the flower that stands for: prettiness,Pompon rose,Music,Which Pink Creation From Noel's House Party Had A UK No.1 In 1993,Mr Blobby,Food & Drink,Where were Cornflakes invented?,Battle Creek Sanitarium +General,How is the tree with the botanical name Fagus better known,Beech,General,"What word is used in a balance sheet to mean ""What a company owes to its suppliers and lenders""",Liabilities,General,In New Jersey what is it illegal for a man to do fishing season,Knit +General,What is the young of this animal called: Codfish,Codling sprat,Food & Drink,In which country did edam cheese originate ,Holland ,General,Where in the body would you find the Bowman's capsule,Kidney +Science & Nature,What Metal Are Light Bulb Filaments Made From ,Tungsten ,General,When was the Berlin wall erected,1961,General,The Computer Language ADA Was Named After The Daughter Of Which Famous Poet,Lord Byron +General,Methyphobia is the fear of,Alcohol,General,Who protects themselves from from blowing dust with three eyelids,Camels,Sports & Leisure,Who Was Britains Golden Girl In 2004 Winning Both The 800 & 1500 Metres ,Kelly Holmes  +General,What name is given to a stone female figure who supports a ceiling on her head,Caryatid,General,What's the smallest known planet in our solar system,Pluto,General,Large brown seed with edible white lining enclosing milky juice,Coconut +Music,Which 1998 Double Album Has One For The Heart And One For The Feet,George Michael / Ladies & Gentlemen,General,Polite society man does it 2 legs woman sitting dog 3 legs what,Shake hands,General,Name either of the two giant stars in the constellation of Orion,Rigel betelgeuse +General,Who wrote The Prime of Miss Jean Brodie,Muriel Spark,General,He assassinated john lennon on december 8 1980,Mark chapman,General,Jimmy Carter was the first US president to have done what,Born in a Hospital +General,"In France what take place at Auteuil, Saint-Cloud and Chantilly",Horse Racing – they are courses,General,Kong what member of ac/dc died in 1980,Bon scott,General,"In Coronation Street, what was the name of Ken Barlow's first wife, played by Anne Reid",Valerie +Music,Name The Orchestral Instrument That Lent Its Name To A Mike Oldfield Album,Tubular Bells,General,What animal is represented by the constellation Lacerta,Lizard,General,Paris is the administrative centre of which French region,Ile de france +Sports & Leisure,Which Uk Leasure Activity Has Caused The Most Fatalities ,Fishing ,General,Name Tina Turners solo comeback album of 1984,Private Dancer,General,In California it is illegal to eat what while bathing,Oranges +General,Geneva stands on what river,Rhone, History & Holidays,"What movie featured Reece's Pieces as part of the story, because the director couldn't obtain the rights to use M&M's? ",E.T. ,Toys & Games,What game or sport is Bobby Fischer identified with?,Chess +General,In the 60s a Yellow Golliwog worn by a girl symbolised what,Proud of non virginity,People & Places,Whose Nickname Was Satchmo ,Louis Armstrong ,General,Who started life as Dippy Dawg,Goofy +General,Where Are The Maxwell Gap And The Keeler Gap Located,Between The Rings Of Saturn,General,In fiction who's mother Monique Delacroix died when he was 11,James Bond,General,"In 1979, who recorded ""London Calling""",Clash +Food & Drink,How Many Legs Does A Bombay Duck Have? ,None Its A Fish!! ,General,"What served as a cowboy's wash cloth, dust mask & water filter",A bandana bandana,Science & Nature,What Is An Exoskeleton? ,An Insects External Skeleton  +General,What was the name of the ranch in the tv series 'bonanza',Ponderosa,General,Short actors stand on what wooden object - to appear bigger,Pancake,Art & Literature,"Who wrote 'little lamb, who made thee'?",William Blake +General,Which Grand Prix driver was also a champion claw pigeon shot,Jackie Stewart UK clay 5 times, History & Holidays,"Electric Christmas tree lights were first used in what year? 1878,1895, 1903, 1907 ",1895 ,Sports & Leisure,Which Heavweight Boxer Is Known As The Real Deal ,Evander HolyField  +General,In what Australian state would you find Cessnock,New south wales nsw,General,Which acid was first prepared from distilled red ants,Formic acid,General,"""Don't worry, unless they're strange, your kids will eat them!""",Life +General,Strabismus is the correct name for what condition,A Squint,General,To grill meat on a rack over charcoal,Charbroil,Sports & Leisure,Which Game Makes Use Of Hoops ,Croquet  +General,What US city buys the most blond hair dye,Dallas Texas,General,A charm worn against evil,Amulet,General,Who is Dick Tracey's girlfriend,Tess Trueheart +General,Ancient Chinese thought what fruit a symbol long life immortality,Peach,Science & Nature,"How many Earths would it take to match the size of the sun? Over 1000, 100,000 or 1,000,000?","1,000,000",General,Which book first featured the character Felix Leiter,Casino royale +General,The Volga River flows into what sea,Caspian sea,General,What is the name of the colourful cathedral on Red Square in Moscow,St basil's,General,"Whose headstone reads 'at rest, an american soldier and defender of the constitution'",Jefferson davis +General,Which Saint translated the Vulgate bible,Jerome,General,"An electrical device for removing suspended impurities such as dust, fumes, or mist, from air or other gases",Electrostatic precipitator,General,How many beams of light are used to record a holograph,Two +Music,What Do Gary Lineker & John Lennon Have In Common,Both Have The Middle Name Winston,General,Which Country Scored The First Ever “ Golden Goal ” At The World Cup Finals,France,General,What was the capital of Ethiopia,Addis Ababa +Food & Drink,"What is Japanese ""sake"" made from?",Rice,General,Picea is the generic name for which tree,Spruce,Science & Nature,What kind of poisoning is known as plumbism?,Lead poisoning +General,Fred Waller patented what sporting equipment,Water Skis,General,The sacred book of which religion is divided into 114 chapters called Suras,Islam,General,What are bright regions of the sun's photosphere,Facula +Sports & Leisure,With which sport is Chris Evert Lloyd identified,Tennis,Music,The Label Is Bludgen Riffola Name The Band,Def Leopard,Music,"Who Released The Top Selling Album ""The Good Will Out""",Embrace +Music,Which Member Of The Buzzcocks Went On To Form The Band Magazine,Howard Devoto,Entertainment,Secret Identities: Jim Corrigan,The spectre,General,"What is a ""ruderal""",Weed +General,What is the worlds longest race,The Whitbread round the world,General,What was the best chariot route from rome to brindisi,Via appia,General,Who killed Laura Palmer on Twin Peaks?,"Her father,Leland Palmer." +General,"What german city do italians call ""the monaco of bavaria""",Munich,General,Ronald MacDonald is worldwide except Japan what's he there,Donald MacDonald,General,Fill in the blank: chang and eng were the most famous,Siamese twins +General,"In which South African town were over sixty people murdered by the police, during a campaign against the Pass laws in March 1960",Sharpeville,Geography,"There are approximately 320,000 _____________ in the world.",Icebergs,General,"What rock group recorded ""smokin'"" and ""more than a feeling""",Boston +Music,What Was John Lennons First No.1 Single In 1980,(Just Like) Starting Over,General,"In 1909, which U.S. President became the first to be depicted on a coin?",Abraham Lincoln,Art & Literature,What was Michelangelo's first name? ,Michelangelo!!  +General,What is the largest & most complicated joint in the body,Knee,Science & Nature,What Is Blackwater Fever A Complication Of ,Malaria ,General,Sterlet is the rarest most expensive what,Caviar - tiny eggs +General,Who painted The Gleaners,Jean Francis Millet,General,What show was Family Matters a spin-off of?,Perfect Strangers.,General,What was the first sport to be filmed,Boxing by Thomas Edison 1894 +General,In Japan what is Shogi,Japanese Chess,General,You can who sang 'I'm a believer',Monkees,Science & Nature,Which Camera Was Invented By Edwin Land ,The Polaroid Camera  +General,In which city were the summer Olympic Games of 1900 held,Paris,General,What element is azoth the ancient name for,Mercury,Music,What Is The Best Selling Album Of All Time In The UK,Sergeant Pepper's Lonely Hearts Club Band- The Beatles +General,In cooking something made Veronique must contain what,Grapes,General,Which south-east Asian city is served by Kimpo International airport,Seoul,General,Helen Mitchell became famous as what soprano,Nellie Melba +General,Which TV Show Was Set In The Fictional Location Of Kings Oak,Crossroads,General,Who did Catherine Parr marry after the death of King Henry VIII?,Thomas Seymour, History & Holidays,From Which Country Does The Poinsettia Plant Originate ,Mexico  +General,What's a farm that has both cattle and crops called,Mixed farm,General,What is the name given to the dish of beef coated in pate and wrapped in pastry,Beef wellington,General,"The internal diameter of a gun, diameter of a bullet",Calibre +General,What mountain - Greeks believe was the home of the Muses,Helicon,General,The assassin in The Man With the Golden Gun was played by who,Christopher,General,"Name the evil slave owner and villain in Harriet Becher Stowe's novel ""Uncle Tom's Cabin"".",Simon legree +General,E W Hornung created which fictional character,Raffles,Food & Drink,What Does A Charcuterie Sell ,Pork Products ,Mathematics,2.7182 is the approximation for which variable used in logarithms?,E +General,Who is the master of the Shakespearean brute Caliban,Prospero,General,"What was Tom Clancy's blockbuster first novel, published in 1984",The Hunt,General,"Raspberry, dewberry and blackberry are all members of what family",Rose +General,What is a group of gnats,Mothers-in-law,General,Which tennis winner also won a Winter Olympics silver medal,Jaraslav Drobny,General,What is the worlds oldest desert - country named after it,Namib +General,What bird is depicted on the Canadian $1 coin,The loon – coin nickname is loonie,General,When was gas lighting invented,1792,General,The Wadomo tribe in Zimbabwe have what physical oddity,Two toes each foot +General,"In horse racing, what is the straight opposite the one with the finish line",Backstretch,Geography,What is the capital of Kuwait,Kuwait city,General,What does a myologist study,Muscles +General,Name the author who created Brer Rabbit and Brer Fox,Joel Chandler Harris,General,What insects do isopterpophobics fear,Termites,General,Geococcyx Californicus is what (cartoon) animal,Road Runner +General,Large South American constrictor snake,Anaconda,General,What international award was worth 365 dollars mineral value in 1988,Olympic,General,In computing what was the first ironbed,A Scanner +General,After which Italian is America named,Amerigo Vespucci,General,"Until the dinner plate was introduced in the 15th century, it was customary to eat on what",Bread,General,Hadephobia is a fear of ______,Hell +General,What is the longest tunnel,Water supply tunnel,General,What port lies at the mouth of the Swan river,Freemantle (Perth),Art & Literature,What Were The Tree Like Creatures In The Lord Of The Rings Called ,The Ents  +General,What do Ombrophobes fear,Rain,General,"Which group of Atlantic Islands were, for a time, known as the Flemish Islands, after Faial was gifted to Isabella of Burgundy in 1466",The azores,General,Name the only two mammals with hymens,Humans and horses +General,"Who wrote ""The Black Prince"", ""The Sea The Sea"", and ""The Philosopher's Pupil""",Iris murdoch,Music,"Who had his first UK top 40 hit single in 2004 with Magic, thirty years after his death at the age of 26?",Nick Drake,General,Manu National Park Peru has 1300 different species of what,Butterfly +General,Name the home city of the US football team nicknamed Falcons,Atlanta,General,On the body what is a calcaneium,Heel bone,Geography,What is the capital of Comoros,Moroni +General,What actress played mrs margaret williams in the danny thomas show,Jean, History & Holidays,What Christmas delicacy has blue veins running through it and tastes cheesy'? ,Stilton Cheese ,General,One person in Texas is killed annually doing what,Painting road lines +Sports & Leisure,What Is The Longest Athletics Race In The Olympic Games? ,50 Km Walking Race ,Sports & Leisure,In Which English City Will You Find Lords Cricket Ground ,London ,General,Which is the only English word to both begin and end with the letters U-N-D,Underground +General,Who played Billy the Kid in The Left Handed Gun,Paul Newman,General,"Which 1978 film from the book of the same name by Ira Levin, tell of the cloning of Adolf Hitler",The boys from brazil,General,Which educational establishment in Utah is known as B.Y.U.,Brigham young university +Art & Literature,Which British Artist Is Noted For His Numerous Paintings Of Horses ,George Stubbs ,General,In the USA a police 10-31 is the code for what,Crime in progress,General,Powdered red pepper,Cayenne +General,Which was the sacred animal of ancient egypt,Cat,Science & Nature,What is the only dog that doesn't have a pink tongue,Chow,Music,Suzanne Hoffs Was The Lead Singer Of Which Very Successful Band,The Bangles +General,In what bodily system are Schwann cells found?,Peripheral Nervous System,Science & Nature," Of all known forms of animals life ever to inhabit the Earth, only about __________ still exist today.",10%,General,Who wrote the book Coma,Robin Cook +General,What foodwise is a Fieldlane Duck,Baked Sheep's Head,General,Whose novels include 'The Cement Garden' and 'Comfort of Stangers',Ian mcewan,General,Suriphobia is the fear of,Mice +Toys & Games,D&D stands for this.,Dungeons and dragons,General,Name either of the songs which were big hits for Shirley Ross and Bob Hope in two musical films in 1937/8. Thanks for the memory,Two sleepy people,General,What did Pope John XX1 use as effective eyewash,Babies Urine + History & Holidays,What Was The Name Of The North Sea Oil Rig That Caught Fire In 1988 ,Piper Alpha ,General,"Serotine, Leislers and Noctule are all varieties of which nianinial",Bat, History & Holidays,What Is A Male Witch Known As ,Warlock  +General,"On Punky Brewster,how was she abandoned by her mother?",Her mother went in to the grocery store and never came out., Geography,Which U.S. state borders a Canadian territory?,Alaska,Sports & Leisure,Which Baseball Team Won A Record 37 World Series In The 20th Century ,New York Yankees  +General,Which group had a 1970s UK number one hit with Oh Boy,Mud,Mathematics,What is next in the series 1 8 27 ?? 125 216?,64,General,Whose girl friend was Virginia Hill,Bugsy siegel +Geography,What is the capital of Spain,Madrid,General,Of what is apiphobia the fear,Bees,General,Which country has the longest coastline,Canada +General,How many years in a vicennial?,20,General,Etymology is the study of what,Word origins,General,Name the cowardly member of Dick Dastardly's squadron,Zilly +General,What unit is used to measure electrical resistance,Ohm,General,What is the last animal in the dictionary,Zorille,General,Who wrote the thriller The Name of the Rose,Umberto eco +General,What company did actor Brad Pitt model for,Levis,General,"In an average lifetime, the average american visits a ___ 5 times",Psychiatrist,General,In which of Aristophanes plays do the women refuse sex,Lysistrata +Music,"Who Had A Hit In 1997 With ""Torn""",Natalie Imbruglia,Art & Literature,Which Nobel Prize Winner Wrote (A History Of The English Speaking Peoples) ,Winston Churchill ,General,Bam Yat and Holon are in which country,Israel +Sports & Leisure,"What football player rushed for 2,003 yards in 1973?",OJ Simpson,General,In the rhyme about magpies what do 5 represent,Silver,General,"Who has daughters named Jade, Elizabeth, Scarlett and Georgia",Mick jagger +General,In Kiplings Jungle Book Ikki was what type of creature,Porcupine,General,The only U.S. State that ends with 3 consonants,Massachusetts,General,"Did you know that if ________________ had not been shot, & not convicted for killing JFK, he would have been convicted for killing Officer Tippet",Lee harvey oswald +Geography,Where is George Washington buried,"Mt. vernon, virginia",General,To refuel your car you go to a _____ station.,Petrol/Gas,General,What is dick turpin's horse's name,Black bess +Sports & Leisure,What Is The Value Of (K) In Scrabble ,5 Points ,General,Former Royal coat of arms of France,Fleur-de-lis,General,What year did Emily Pankhurst chain herself to 10 Downing Street,1907 +Music,Name Any Three Of The Five Members Of The New Seekers,"Eve Graham, Lyn Paul, Peter Doyle, Marty Kristian, & Paul Layton",Geography,Name the largest cathedral in the world. ,St. peter's ,Geography,"He visited australia and new zealand, then surveyed the pacific coast of north america. ",Vancouver  +Music,"Who Recorded The Album ""Never Too Much"" Was It Luther Vandross Or George Benson",Luther Vandross,Music,Which Famous British Composer Wrote A Sea Symphony & At The Age Of 80 The Score For A Film About Scott's Antarctic Expedition,Ralph Vaughan Williams,Geography,Which Town Is Situated Close To The Geographic Centre Of Australia ,Alice Springs  +General,What is between panama and nicaragua,Costa rica,General,Which country has the greatest number of islands?,Finland,Science & Nature,How many Astronaughs crewed the Gemini series of Spacecraft?,Two +Music,"Who's First Top Ten LP Was Entitled ""Wood Face""",Crowded House,General,What instrument does kenny g play,Saxophone, History & Holidays,What type of sweet did Mars and Murrie develop in 1941? ,M & Ms  + History & Holidays,Who was Joseph Merrick?,The Elephant Man,General,"Who publishes the newspaper, The Australian",Rupert murdoch,Science & Nature,A heavenly body moving under the attraction of the Sun and consisting of a nucleus and a tail is a(n) _____.,Comet +General,What actress was born frances ethel gumm,Judy garland,General,Which native Indian tribe never signed a peace treaty US govern,Seminoles,General,What planet did the Thundercats live on?,Third Earth. +Music,Which Spice Girl Upset Anti Fur Campaigners By Wearing Furs In A 1997 Feature In Tatler,Victoria Beckham,General,Which people invented the compass?,Chinese,General,Of what was Charlie Chaplain's cane made,Bamboo +General,"What was the final destination of the first u.s. paddle wheel steamboat, what departed from pittsburgh?",New orleans, History & Holidays,Who wanted to make a coat out of 101 Dalmatians ,Cruella Devil ,General,Robert Langford Modini became more famous as who,Robert Stack + Geography,Where are the 'wallops'?,Hampshire,Geography,Which Country Grows The Most Potatoes Per Annum ,Russia ,Music,Which Song Was A Hit For Both Tom Jones And Prince?,Kiss +Art & Literature,Who Wrote (Robinsonn Crusoe) ,Daniel Defoe , History & Holidays,In 1968 Which Country Did The USSR Invade To Stamp Out Liberal Reform ,Czechoslovakia ,General,"Hybrid offspring of the jackass (male ass) and the mare, much used and valued in many parts of the world as a beast of burden.",Mule +Geography,What are the worlds four oceans _ alphabetically,"Arctic, atlantic, indian and pacific",General,"In 'a christmas carol', how many ghosts visited scrooge",Four,Toys & Games,"In which sport are terms ""spare"" and ""gutter"" used?",Tenpin bowling +General,"In which Kent town is the Indian princess, Pocahontas, buried",Gravesend,General,What sport are barbells used in,Weightlifting,General,The SF award the Hugo is named after Hugo who?,Gernsbeck +General,"What did Emerson, Lake & Palmer burn on stage during their concerts?",The American flag,General,What is the general term used for various forms of insanity & mental derangement,Mental illness,General,Illusion of hearing or seeing something not actually present,Hallucination +General,What is the largest country in central america,Nicaragua,General,What is the flower that stands for: sadness,Dead leaves,General,David John Moore Cornwell became famous as who,John Le Carre +Geography,What Is The Largest Of The Channel Islands ,Jersey ,General,Memonites are what Christian sect founded 1525 Conrad Grebel,Anabaptists like Amish,General,What drug is obtained from the dried bark of an evergreen tree native to south america,Quinine +General,What ernest hemingway novel depicts the lives of rebels during the spanish civil war,For whom the bell tolls,General,Tubular sac attached to the large intestine,Appendix,General,Where were George Mallory & Andrew irvine heading when they were last seen alive,Summit of everest +Geography,Which Famous Cocktail Was Invented At The Raffles Hotel In The Far East Around 1910 ,The Singapore Sling ,General,What is the capital of the state of Ohio,Columbus,General,Where is the Isle of Pelicans,Alcatraz +Music,On which Beatles album would you find “Ticket To Ride”,Help, Geography,What is the capital of Jordan ?,Amman,General,"In Greek mythology, who solved the riddle of the sphinx",Oedipus +Music,Blackie Lawless Was The Singer With Which Band,Wasp,General,Who was the founder of the Irish political party Sinn Fein?,Arthur Griffith,General,In Missouri a man must have a permit to do what,Shave +Toys & Games,What is the Chess ranking system called ?,Elo,Entertainment,"In the TV series 'The Brady Bunch', what was Cindy's toy doll's name?",Kitty Carrie All,General,"Which actor plays the part of Dr. Frasier Crane in the Channel 4 series ""Frasier""'",Kelsey grammer + Geography,In what country is Lahore?,Pakistan,General,Minerva is the Goddess of what,Wisdom,General,Khartoum' Is The Capital City Of Which African Country,Sudan +Entertainment,For whom did Colonel Tom Parker act as manager?,Elvis Presley,General,What is the name of the computer in Stanley Kubrick's '2001: A Space Odyssey,Hal, History & Holidays,"In 'A Christmas Carol'', what is Tiny Tim's surname ",Cratchett  +General,What was the name of the nuclear missle defense system Reagan proposed?,Star Wars,General,Baile Atha Cliath - Official name what capitol city,Dublin - its Irish Gaelic,Entertainment,"Who sang for 'Bad company' and 'Free', then went out on his own?",Paul Rodgers +General,Who was the first christian emperor of rome,Constantine,Science & Nature,The name for the group of stars which form a hunter with a club and shield is _____.,Orion,Sports & Leisure,Who was the first unseeded player to win Wimbeldon?,Boris Becker +General,Which country is the world's biggest gold producer?,South Africa,General,"Any type of self-propelled vehicle used by railroads to pull or push other types of rolling stock, including passenger, freight, & work cars",Hovercraft,General,The Roman festival of Hilaria is equal to what modern day,April Fools Day +Music,"In ""Lucy In The Sky With Diamonds"", what were the colors of the Celophane Flowers?",Yellow and green,Geography,What is the most sacred river in India,Ganges, History & Holidays,"The birthplace of Napoleon, also the capital of Corsica, is?",Ajaccio +General,"Who painted ""The Shriek"" and ""The Kiss""",Edvard munch,Art & Literature,Where is the Louvre located?,Paris,General,Football - the Philadelphia ______,Eagles +General,Which is the last book of the Old Testament,Malachi,General,On common ailments Charles Osborne had what for 69 years,Hiccups, History & Holidays,"""Who Is The First Ghost To Appear To Scrooge In """"A Christmas Carol""""?"" ",Marley's Ghost  +General,"Lassa fever is named after the village in which it was first detected in 1969, where is Lassa",Nigeria,Music,"Who Joined Genesis From ""Flaming Youth""",Phil Collins,General,"Who wrote the play "" What the Butler saw """,Joe Orton +General,Crime of getting married again whilst still legally married,Bigamy,General,"Cumulus, cirrus and stratus are all types of what",Clouds,General,What food is the leading source of salmonella poisoning,Chicken +General,Which is the highest poker hand 2 pairs or three of a kind,Three of a kind,General,The British colonial law in India monopolizing salt production provoked whose rebellious march to the sea,Mahatma ghandi, History & Holidays,Where did the famous witch trials of New England take place ,Salem  +General,What form of light comes at wavelengths below 360 nanometers,Ultra violet,General,What Zimbabwe beer is named after a river,Zambezi,General,"Where would you find a nave, apse, atrium and narthex?",Basilica +Music,What Was The Best Selling Dire Straits Single In The UK,Private Investigation,General,Who hired the Mormon Mafia to prevent contamination,Howard Hughs,General,What plant is snuff made from,Tobacco +General,What did the Victorians call servant regulators,Alarm Clocks,Geography,"Which country borders Italy, Switzerland, West Germany, Czechoslovakia, Hungary, Yugoslavia, and Liechtenstein",Austria,General,In Greek mythology who were the guardian spirits of the sea,Neriads +General,"In Imperial measurement, how many square yards are in a square chain",484, History & Holidays,Who played the lead role in the 1960 Hammer production The Curse of the Werewolf ,Oliver Reed ,General,A ___ can not move it's jaw side to side,Cat +General,What country calls themselves Nipon?,Japan,General,"Wafter Re-Uniting In The 80's The Song ""That Was Then This Is Now"" Was The First Single released By Which Group",The Monkees,General,"In baseball, how far do you have to run if you hit a home run",Three hundred +General,What is The Adi Granth,Sacred Scriptures Sikhs,General,Which SF author created the character Lazarus Long,Robert Heinlein,Food & Drink,What Is The Third Most Popular Soft Drink In The UK ,Lucozade  +Geography,"Which American state's name is Spanish for ""flowered"" or ""flowery land""?",Florida,General,What was Cecil B. De Mille's Middle name,Blount,Music,Who Was Just A Step From Heaven In 1994,Eternal +General,What would you expect to find in a vespiary,Wasps,General,What inert gas is used in fluorescent lights,Argon,General,What is a 'funambulist'?,A tightrope walker +General,How many members are in the 'fairfield four',Five, History & Holidays,What was the name of the multi-colored cube you had to re-organize? ,Rubik Cube ,General,"What actor mouthed the line ""Whatch you talkin' 'bout Willis?""",Gary Coleman +General,Baseball: the cleveland ______,Indians,General,An orchidectomy involves what procedure,Removal of testicle,General,Whose was the first voice to appear on the British TV station Channel 4?,Paul Coia +General,What language is klutz an insult in,Yiddish,General,What is the capital of pennsylvania,Harrisberg,General,Which nationality calls Munich the 'Monaco of Bavaria'?,Italians +General,"In ballet, a leap in which the lower leg beats against the upper one at an angle, before the dancer lands again on the lower leg.",Cabriole,Music,What Was Former Spice Girl Victoria's Maiden Name Before She Married David In 1999,Adams,General,Which 16th century Italian wrote The Prince,Machiavelli +General,Nicosia is the capital of ______,Cyprus,General,In Paris what are FD Roosevelt Stalingrad Louis Blanc,Metro Stations, Geography,What's the highest mountain in the 48 contiguous U.S. states?,Whitney +General,For what was Joan of Arc made a Saint,Her Virginity,General,"In 1987, who released her second album 'solitude standing'",Suzanne vega,General,Which notorious British murderer appears in Alban Berg's unfinished opera Lulu,Jack the ripper +General,Which US city was once named Porkopolis,Cincinnati,General,What soap was about the trials and tribulations of a vampire,Dark shadows,Food & Drink,What is the main ingredient of Paella? ,Rice  + Geography,What is the basic unit of currency for Antigua and Barbuda ?,Dollar,General,Which abbey is located near Barrow Upon Humber in North Lincolnshire?,Thornton Abbey,General,26% of McDonalds Ontario employees admit doing what,Putting bodily fluids in food +General,What is the only day named for a planet,Saturday,General,Most of these animals are bisexual - what animal,Giraffe,Music,Amount ABC reportedly paid for rights to the Beatles Anthology documentary,$20 million +General,Zymase and Glucose combine to form what drug,Alcohol,General,What is the literal meaning of 'pince-nez',Pinch nose,General,Who was the female lead in 'hello dolly',Barbara streisand +General,Which international companies logo is exactly 42 dots,Sony,General,In what film did rick moranis make his big-screen debut,Strange brew,General,"How often does something recur, that recurs ""quotidian""",Daily +General,Psychologist say when a woman wears red she wants what,Another Woman,Art & Literature,"The two races in HG Well's ""The Time Machine"" are the child-like Eloi and the subterannean ______?",Morlocks,Music,Who composed the music for the German national anthem?,Joseph Haydn +General,"A social dance in ¾ time that became widely popular in the nineteenth century. It developed from the Landler, a German-Austrian turning dance.",Waltz,General,"Which us city is called the ""windy city""",Chicago,General,Marinated limbs of fowl,Chicken wings +General,In 1891 what city held the first weightlifting world championship,London,General,What does a month beginning with a Sunday always have?,Friday the 13th,Sports & Leisure,In 1994 Oliver McCall Defeated Who To Become World Heavyweight Boxing Champion ,Lennox Lewis  +Religion & Mythology,Who is the greek equivalent of the roman god Mercury,Hermes,General,Only three grape varieties can be used to make champagne. Pinot Noir and Pinot Meunier are two. Name the third,Chardonnay,General,In Greek mythology who killed the Gorgon,Perseus +General,Dacca is the capital of which country,Bangladesh,Music,Which Guitarist Replaced Bernie Leadon In The Eagles In 1975,Joe Walsh,General,Who created the myrmidons,Zeus +Science & Nature,What device was used to determine a ship's latitude ?,Sextant,General,In Mork and Mindy what was the capitol of Ork,Kork,General,In what WW1 battle were tanks first used in 1916,Somme +Sports & Leisure,Who was the 1978 Wimbledon Women's Singles champ?,Martina Navratilova,Geography,What Is The Name Of The Highest Mountain In Japan ,Mount Fujiyama Or Mount Fuji ,General,What is the medical term for pain and aching in the lower back,Lumbago +General,Whom did Colin Baker replace as Doctor Who on television,Peter davison,General,Schschpiel is what game in Germany,Chess,Sports & Leisure,In Which Sport Would You Encounter A Bully Off? ,Hockey  + History & Holidays,Who had a hit with Schools Out in 1972? ,Alice Cooper ,General,What is the lifespan of a turkey that evades the cooking pot,Twelve years,General,Who built a loyal following with the release of 'the pretender' in 1976,Jackson browne +General,"Which Country Now Have A Parliament Called ""The Bee Hive""",New Zealand,General,The lutra-lutra is which semi aquatic animal,Otter,General,What is the flower that stands for: early youth,Primrose +General,What type of birds (Hugin + Munin) sit on the shoulders of Odin,Ravens,General,Tennis: what area falls between the baseline & the service line,Back court, Geography,What is the capital of Angola ?,Luanda +General,Dutchphobia is a fear of ______,Anything dutch,Food & Drink,In What Decade Did Sliced Bread First Appear? ,1930's ,Science & Nature,"If you're in the northern hemisphere, Polaris, the North Star, can be found by looking which direction?",North +General,What is the 100 year old safe in washington dc,Centennial safe,General,The false plane tree is better known as what,The Sycamore,General,"In the 1995 movie, To Wong Foo, Thanks for Everything, Julie Newmar, what were the names of the characters portrayed by Patrick Swayze, Wesley Snipes and John Leguizamo?","Vida Boeheme, Noxema Jackson, and Chi Chi Rodriguez" +People & Places,"What Do Eric Clapton, Marilyn Monroe, And Laarry Grayson All Have In Common ",They All are Or Were Illegitimate ,Music,"Whose Songs Include Night & Day, Begin The Beguine & Just One Of Thoese Things",Cole Porter,Music,"In Which Year Was ""Crying In The Chapel"" A No.1 Hit For Elvis",1965 +Tech & Video Games,What does CR-ROM stand for?,Compact Disk Read Only Memory,General,In the Bible who built the ancient city of Babylon,Nimrod,Music,Who Composed The Planet Sweet,Gustav Holst +General,Where is Londons whispering gallery,St pauls cathedral,General,What links Socrates Aristotle Janis Joplin,Bisexuals,General,"What colour is bistre, a pigment used in artist's paints and inks",Brown +Religion & Mythology,Who is the Roman goddess of destiny?,Fortuna,General,What kind of small glass bottle turns liquid perfume into a fine spray,Atomiser,Technology & Video Games,What was the hybrid pinball/video game in the Pac-Man series? ,Baby Pac-Man +Geography,What is the largest of the countries in Central America,Nicaragua,Music,For Which Record Company Did The Beatles Record Most Of Their Work,Parlophone,General,The longest tunnel connects delaware and ______,New york +General,Who won the world soccer championship in 1934,Italy,General,"Name the evil slave owner and villain in ""Uncle Tom's Cabin""",. simon legre,Music,"""Sally"" Was A 1970 Hit For Which Male Singer",Gerry Monroe +General,Which film links novelist Ira Levin and Sharon Stone,Sliver,Science & Nature," __________ never walk or trot, but always hop or leap.",Rabbits,General,What instrument are you playing when you perform a rim shot,Drums +General,"What president's hobbies included pitching hay, fishing, and golf?",Calvin coolidge,Food & Drink,Who was the female russian dancer who had a desert named after her? ,Anna pavlova ,General,To which U.S. city would you travel to see the Liberty Bell,Philadelphia +Science & Nature," The only __________ to ever appear in a Shakespearean play was Crab in ""The Two Gentlemen of Verona.""",Dog,General,Capital cities: Western Samoa,Apia,Technology & Video Games,What was launched on the 4th December 1996 ?,Mars Pathfinder +General,Pushin' Too Hard' was the notable song recorded by which group in the 60's,Seeds, Geography,What is the basic unit of currency for Turkmenistan ?,Manat,General,From which musical did the songs 'If my friends could see me now' and 'There's got to be something better than this' come,Sweet charity +General,When Prince Edward Married Sophie Rhys Jones In 1999 What Title Did She Adopt,Countess Of Wessex,General,How is 75% of petrol in an engine wasted,Combustion,General,What team has won the most NBA championships,Boston celtics +General,Who was Britain's second Labour Prime Minister,Clement attlee,General,What does a croque Madame have that a croque monsieur don’t,Egg on top – Cheese Ham Toast,Geography,Which County Is Glastonbury In? ,Somerset  +Music,"Who Was The First Woman To Be Inducted Into The Country Music Hall Of Fame In 1973, 10 Years After Her Death",Patsy Cline,Science & Nature,"The Komodo Dragon, the biggest known lizard to science, is endemic to the Komodo islands of what country?",Indonesia,General,Generation X Was A Band Fronted By Which Famous Rocker,Billy Idol +Sports & Leisure,Which rugby ground is sometimes referred to as the Cabbage Patch? ,Twickenham ,General,Benthos are plants and animals living where,Water,General,Round which planet do the moons Ganymede and Callisto orbit,Jupiter +Food & Drink,"Iceberg, Boston, and Bibb are types of What ",Lettuce ,Technology & Video Games,What object did you need to get to the secret room in Atari's 'Adventure'? ,The dot,General,Roller coasters originated in what country,Russia - ice on sleds +General,Aaron Copeland wrote a ballet about which American folk hero,Billy the Kid,General,Strabismus is another name for which affliction,Squint,General,What is the Capital of: Martinique,Fort-de-france +General,Which superstar film actor's real narne is T.C. Mapother IV,Tom cruise,General,What was Britain's first colony (annexed in 1583),Newfoundland,General,What is Colombo's first name,Phillip +Geography,In Which English City Will You Find The Crucible Theatre ,Sheffield , Geography,What US state is completely surrounded by the Pacific Ocean?,Hawaii, History & Holidays,Which Mutiny Took Place In 1789? ,Mutiny On The Bounty  +General,"Humans are 10,000 times more sexually active that what animal",Rabbits,General,What did the song '867-5309/jenny' spawn for tommy tutone,Lawsuit,General,What are double sixes called on dice,Boxcars +General,What pop group was originally named 'The Quarrymen',The Beatles,General,How many air force one(s) are there,Two,General,"Where is a nave, apse, atrium and narthex",Basilica +General,Film is to Oscar as Mystery novel is to,Edgar,General,The capital of Romania,Bucuresti,General,What is the Capital of: El Salvador,San salvador +General,Who painted' A Bar at the Folies Bergere' (1882),Edouard manet,General,Name the river that flows through Baghdad,Tigris,General,"If it's 12.00 noon GMT - what time is it in Sydney, Australia?",10.00pm + Geography,What country was once known as 'The Breadbasket of Russia'?,Ukraine,General,In The World Of Dancing From Which Country Does The Tango Originate ?,Argentina,General,Who is tippi hedren's daughter,Melanie griffith +General,Who was the 2nd president of the U.S.,John adams,General,"Cornbread, Turkey and Sweet potatoes made up the first what",TV Dinner,General,Part of a theatre for the audience,Auditorium +General,Which computer firm made a model numbered ZX80,Sinclair,General,What is the largest cell in a human body,Ovum,General,In which country are condoms most commonly used,Japan +General,What is the Capital of: Wallis and Futuna,Mata-utu,General,Wo Fat was the enemy of which TV detective,Steve MacGarett,Science & Nature,What is the instrument used to measure atmospheric pressure ,Barometer  +General,Egyptian god credited with first making beer,Osiris,General,"If a dog is canine, cat feline what creature is accipitrine",A hawk,Music,"Who Wrote The Theme Tune To The James Bond Movie ""A View To A Kill""",Duran Duran +General,What meets the Atlantic Ocean at the Cape of Good Hope,Indian ocean,Sports & Leisure,What was Evonne Goolagong's married name ,Cawley ,General,What is the formal corporate slogan of Google?,Don't Be Evil +General,Who created 'trepanning',Dr bart hughes,General,Who played The Talented Mr Ripley,Matt Damon,General,Who had a hit with Tiger Feet,Mud +General,What is the Capital of: Zambia,Lusaka,General,"What can be Inline, flat or v",Car cylinders, Geography,What is the capital of Japan ?,Tokyo + History & Holidays,Which Chinese Dynasty Lasted From 1368-1644? ,Ming ,Music,In 1989 Bobby Brown Was the First American Solo Male Artist To Spend The Most Weeks Since Who In 1977,Elvis Presley,General,"Scuba: if a marine animal cannot be identified, a diver should _____",Avoid it completely +Geography,What is the capital of Singapore,Singapore,Science & Nature,And Where Did His Brother Repeat The Experiment A Few Days Later ,"Fort Meyer, Virginia ",General,Which group had a hit in 1985 with Everybody wants to Rule the World,Tears for fears +General,What is the Capital of: United States,Washington dc,General,Which Famous Musician Married Star Wars Actress Carrie Fisher On Aug 16 1983,Paul Simon,General,The chako war 1932 1935 was between which two countries,Bolivia and Paraguay +General,A crested parrot,Cockatoo, History & Holidays,Which island was Napoleon from?,Corsica,Technology & Video Games,Final Fantasy Legend 1-3 are known in Japan as what? ,SaGa 1-3 +General,"Which company produces PageMaker, Photoshop and Acrobat",Adobe,Geography,Where Is The Worlds Busiest International Airport ,London Heathrow ,General,On which river does Washington D. C. stand?,Potomac +General,Of The Known Planets Which One Is Nearest The Sun,Mercury,General,What arabian peninsula nations recently merged under communist leadership,Yemen,General,"What is the common name for tinea, a fungal skin condition, when it attacks the feet",Athlete's foot +General,After whose rebellion were the 'Bloody Assizes' presided over by the infamous Judge Jeffreys,Duke of monmouth, History & Holidays,Who Lead The British Labour Party Before Harold Wilson? ,Hugh Gaitskell ,General,NaOH Is The Chemical Symbol For What,Sodium Hydroxide Or Caustic Soda +General,Who Provided The Voice Of Buzby In The Old BT Adverts Of The 1980's,Bernard Cribbins,General,What is the SI base unit of electrical current,Amperes,General,The Sublime Porte was the Court of Government of which Empire,Turkish/ottoman +Music,What Was Rolf Harris 's First Hit,Tie Me Kangaroo Down Sport, History & Holidays,Who Was Shot In Dallas On 24 th November 1963 ,Lee Harvey Oswald ,General,Men average 12 a year women 18 a year - what,Read Books +General,Which BBC Newsreader Advertised Cow & Gate Baby Food In The 1980‘s?,Martin Lewis,General,What scale measures temperature,Rankine scale,General,Which sport is played by the Sheffield Steelers,Ice hockey +Mathematics,Who invented logarithms ?,John Napier, History & Holidays,"In which 1984 film does the character of Billy Peltzer bring Christmas chaos to the town of Kingston Falls, when he inadvertently feeds someone he shouldn't after midnight ",Gremlins ,General,This assassin of Julius Caesar was his friend,Marcus brutus +General,Of what is 'soweto' in south africa an abbreviation,South west township,General,Which group had hits with Fernando and Angelo,Brotherhood of Man,Geography,"According to a Fortune magazine survey conducted a few years ago, _________ topped the list of best major U.S. cities to balance work and family.",Seattle +General,What in reality was Dr McCoy's medical scanner in Star Trek,A Salt Shaker,General,What is the first name of the son of David and Victoria Beckham,Brooklyn,General,Who sang Move em out head em up head em up move em on,Frankie Lane Rawhide +Science & Nature,What Does BASIC Stand For In Computing Terms ,Beginners All Purpose Symbolic/Standard Instruction Code ,General,What is the flower that stands for: do not abuse,Saffron,General,What was the original scumbag,Condom +Geography,Which European country has the lowest population density,Iceland,General,The feeling of having experienced something before is known as _______,Deja vu,Music,William Bailey Is The Real Name Of Whch Singer,Axl Rose +General,A Baseball travels 9% faster in which US city,Denver - thinner air,General,Which object was known as a Churchwarden,Long clay pipe,General,What English china company has a lion as it's logo,Royal Doulton +General,What is an octothrope,The # symbol,Music,"Who Had A Hit In 1996 With ""Naked""",Louise,General,The Oedipus Complex is the sexual love of a son for his mother. What is the sexual love of a daughter for her father called,Electra complex +General,What muscle is joined by the lingual nerve to the brain,Tongue,General,A gritty psychedelic version of Dale Hawkin's 'Suzy Q' was on which group's first album in 1968,Creedence Clearwater Revival,Food & Drink,What Is The Name Of The Dish Of Cold Meat Served In Aspic ,Ballotine  +Science & Nature," Because of the giant panda's large size and the small size of their offspring, it is difficult to tell when a panda is __________",Pregnant,Music,Woody Cook Born In December 2000 Is The Son Of Which Couple,Norman Cook & Zoe Ball,General,Which Prime Minister of Pakistan was deposed in a military coup in October 1999,Nawaz sharif +Science & Nature,Which part of a cat's eye reflects light ?,Tapetum,General,In 1987 U2 won Grammy for best album of year name it,The Joshua Tree,General,There can be only one.,Highlander +General,Where is maxim's restaurant,Paris,General,"Medically, what are lentigenes",Freckles,General,What time was it when the mouse ran down the clock,One o'clock +General,"Is right music: who recorded ""sos""",Abba,General,From the coast of which county does the padstow lifeboat get launched,Cornwall,General,What is the name of the spicy paste that accompanies dishes such as couscous in North African cuisine,Harissa +General,What is a group of owls,Parliament,General,Why did the chicken cross the road,To get to the other side,General,"In the old gag, where is prince albert",In a can +Music,"Who had a hit in 1987 with the single, Tonight, Tonight, Tonight?",Genesis,General,To whom did denmark sell the virgin islands,United states,General,In New Jersey 1879 stopping a constable doing what $25 fine,Catching a runaway goat +General,What singer's February 6th birthday is a national holiday in Jamaica,Bob,General,Which famous building was built by Shah Jehan ?,Taj Mahal,General,What is the state fish of Hawaii - in Hawaiian,Humunuku Apua'a +Science & Nature,Where Did The Chrysanthemum Originate? ,Japan ,General,Which mountain is called Goddess of the Harvest by the locals,Annapurna in Nepal,General,From what animal is mutton,Sheep +General,Who founded a monastery on Lindisfarne in AD 635,Saint aidan,General,Who sang the song 'Jack your Body' in 1987,Steve 'silk' hurley,General,Thorburn what canal connects lake ontario and lake erie,Welland canal +General,Which edible nut of the American hickory tree is similar to a Walnut,Pecan,General,In the Bible who was Adam and Eves youngest child,Seth or Shet,Music,"""With You Im Born Again"" Reached No.2 In 1979 For Which Pair",Billy Preston & Syreeta + Geography,What is the basic unit of currency for Mauritania ?,Ouguiya,General,What job has the longest lifespan in the USA average age 77,Nuns,General, Legal Terms: A formal agreement enforceable by law.,Contract +General,"""Sindinology"" Is A Well Known Historical Debate Concerning The Origins Of What",The Holy Shroud Of Turin,Science & Nature,"Due to a lack of vitamin C, sailors used to contract this disease.",Scurvy,General,"An old, short, large bored gun",Blunderbuss +General,Which company is owned by Bill Gates,Microsoft, Geography,What Asian city was once called Edo?,Tokyo, History & Holidays,What Are The Names Of The Ghosts In The Classic 80's Video Game Pac Man ,Inky Pinky Blinky & Clyde  +Food & Drink,Small cubes of fried or toasted bread ,Croutons ,Music,Eddie Vedder Is Lead Singer With Which Band,Pearl Jam,General,Who's Sporting Comeback Had The Most Viewers On The 18th FEB 1994 Attracting A Whopping 23.9 Million People,Torvill & Dean +Religion & Mythology,Who is the greek equivalent of the roman god Juno,Hera,Science & Nature,In what body part does an osteopath specialise?,Bones,General,What uses 28 calories if done for one minute,Kiss +General,Which Greek philosopher wrote The Republic and The Laws,Plato,Music,Which Pirate Died In 1966 Aged 26,Johnny Kidd,General,"Which well known novelist, has written a children book called 'Haroun and the Sea of Dreams",Salman rushdie +Music,Which British Band Released Two Hit Albums In The 1970's With Titles Taken From Marx Bros Film,Queen (A Night At The Opera & A Day At the Races),General,Where is antofagasta,Chile,General,The American Soceity of Magicians has status of being what?,The Largest Magic Organistation + History & Holidays,Who Had An 80's Hit With The Song 'Queen of Hearts' ,Juice Newton ,General,Who sang 'kind of a drag' in 1966,Buckinghams,General,Which racist organisation was formed in tennessee in 1865,Ku klux klan +General,In which novel did Edgar Linton marry Catherine Earnshaw,Wuthering heights,General,What world capital city is heated by volcanic springs,Reykjavik (Iceland),General,What is 'tizwin',Indian corn beer +Music,What Country Did The Pop Duo Roxette Come From,Sweden,General,The star Castor is in which constellation,Gemini,General,The countries of Sarawak and Brunei lie on which island,Borneo +General,The Harlem Globetrotters had what signature tune,Sweet Georgia Brown,General,The star constellation Grus has what English name,The Crane,General,Who has the world's largest double-decker tram fleet,Hong kong +General,"Alphabetically, what were the two cities in 'A Tale of Two Cities'",London & paris,General,What is the english equivalent of the name ivan,John,Music,"Who Recorded Albums Entitled ""Mystery"", ""The Beat Goes On"" And ""Near The Beginning""",Vanilla Fudge +General,In what city is the worlds largest medieval cathedral,Seville,General,Approx 800 people died at a firework display in Paris in what year,1770,General,7% of Irelands annual barley crop is used for what,Making Guinness +Science & Nature,The closest living relative of this African mammal is the giraffe.,Okapi, History & Holidays,The Devonian Period is also known as the Age of __________.,Fish,Science & Nature,Who was the first to use rubber gloves during surgery?,Dr. W. S. Halstead +Food & Drink,What is added to Whisky to make is Whisky Mac? ,Giner Wine ,Religion & Mythology,Which ancient continent is said to be submerged,Atlantis,General,What is the right side of a ship called,Starboard +Science & Nature,What is the name given to the short tail of a rabbit ,Scut ,General,"An American folk dance with an even number of couples forming a square, two lines, or a circle. The dance is comprised of figures announced by a caller.",Square dance,Sports & Leisure,"The Nursery End, The Pavilion End & St John's Road Are All Linked With Which Sporting Ground? ",Lords  +General,The process of adding a number to itself a certain number of times is called?,Multiplication,General,Yabusame is the Japanese version of what sport,Archery,General,"Social dances usually performed by couples, including the fox-trot, waltz, tango, rumba and cha cha.",Ballroom dances +General,What do Christians call the period of fasting before Easter,Lent,General,In 18th century England what would you do with whim wham,Eat it Cream sponge,General,"Of what are throat, foxing and platform parts?",Shoe +General,"In Greek mythology, what did daedalus construct for minos",Labyrinth,General,Whats the chemical formula CH3COOH commonly known as?,Vinegar,General,What product only sold 8 in its first year in the USA,Remmington Typewriters + History & Holidays,"What treaty, signed in 1713, ended the War of the Spanish Succession?",Treaty of Utrecht,Science & Nature,What element has the periodic table name Sn ?,Tin,General,Who invented the Windows o s,Bill gates +General,How were the camels carrying abdul kassem ismael's library trained to walk,In alphabetical order,General,Who was the founder of Boy's Town,Father flanagan,Art & Literature,What trilogy did J.R.R. Tolkien write ?,Lord of the Rings +General,"The cooking term ""parmentier"" indicates which vegetable",Potatoes,General,"What Type Of Foodstuff Is A ""Turophile"" Particularly Fond Of ""Some Are Addicted""",Cheese,General,What Venetian traveler's printed journal did Columbus have a copy of,Marco polo's marco polos marco polo +General,What is an ideo locator,You are here' arrow,General,Which religious festival begins with the sighting of the new moon,Ramadan,General,What is the flower that stands for: precocity,May rose +Entertainment,Tess Trueheart married which plainclothes detective?,Dick Tracy,General,Jamestown is the capital and chief port of which Atlantic island,St. helena,General,What is the highest scoring english word in 'scrabble',Quartzy +General,Textophobia is a fear of ______,Certain fabrics,General,What rock magazine that came out in the eighties is now Rolling Stone's major competitor?,Spin,Music,"Who Recorded The Album ""Nylon Curtain""",Billy Joel +Music,What Was Donny Osmonds Real First Name,Donald,General,What did Grace Kelly become in 1956,Princess,General,When was the greek alphabet first used,800 bc +General,The medical journal The Lancet was established in which year,1823,General,Which language never have spelling contests - spell as sounded,Russian,Music,"Name The Band: Andy Summers, Gordon Sumner, Stewart Copeland",The Police +Geography,With what country is Fidel Castro associated,Cuba,Food & Drink,What Is A Kipper ,A Smoked Herring ,General,The latin qed spells out in full as,Quod erat demonstrandum +General,Which is the only species of mammal that can't jump?,Elephant,General,In Florida what is banned in public places after 6pm Thursdays,Farting,General,In which modern country is the region known as Cappadocia,Turkey +Religion & Mythology,Who is the mother of Apollo and Artemis?,Leto, History & Holidays,Halloween is celebrated on the eve of what Christian holiday is it A All Saints Day B St Swithins Day or C Feast of the Tabernacle ,A = All Saints Day ,General,The bering strait lies between russia and ______,Alaska +General,"Who recorded ""burning bridges"" in 1960",Jack scott,General,Which Female Icon Was Created By Ruth Handler In 1959,Barbie,General,What is the young of this animal called: Kangaroo,Joey +General,What is a group of this animal called: Raven,Unkindness, Geography,What is the capital of Saint Lucia ?,Castries,Music,Rockin Dopsie Queen Ida & Clifton Chenier Are All Cajun Music Legends. How Are They Connected Instrumentally,Accordion +Geography,What is the capital of Qatar,Doha,General,Greek Roman Apollo Babylonian Marduk Indian Vishnu gods ?,Healing,General,Grand Turk is the name of the ship - where would you see it,Old Spice cologne bottle +General,What is a Dolly Varden,Large Hat,General,What controversial book did germaine greer write,The female eunuch,General,When was the first republic of France was proclaimed in,1792 +General,Poor whites in florida and georgia are called 'crackers' because of the name of their principal staple food which is what,Cracked corn,General,What do the letters std stand for,Sexually transmitted disease,Geography,What is the capital of Bolivia,La paz +General,What happened to you if you get a nosicomial condition,Infection caught in hospital,General,The average person in a lifetime grows 7 foot of what,Nose Hair,General,What is the flower that stands for: esteem,Garden sage +Art & Literature,"Author of ""The Lighthouse"" and ""Eminent Victorians""?",Virginia Woolf,General,What is Wynonna Judd's real name?,Christina Clair Ciminella,General,Semiotics is the study of what,Signs or symbols +General,What is the fear of movement or motion known as,Kinetophobia,General,Milo what is the binot-simon scale used to measure,Intelligence,General,What river divides the dutch capital of amsterdam in two,Amstel +Entertainment,What is the name of Pearce Brosnan's first James Bond film?,Goldeneye,General,What is a baby squirrel called,Kit or Kitten,General,Luke Halpin Sandy Tommy Norden Bud what 1960s TV show,Flipper +General,What word means an attack of nerves suffered by sportsman especially golfers,Yips,General,What was the number of the M.A.S.H. unit in the film and television series of the same name,4077,General,"On Laverne & Shirley,what was Laverne's favorite drink?",Milk and Pepsi +General,"Who recorded the album ""there and back"" in 1980",Jeff beck,General,In a church who would use the chancel,Choir they sit there,General,What is the only country with a bible on its flag,Dominican republic +General,"The study of natural phenomena: motion, forces, light, sound, etc. Is called ______",Physics,General,What is the name of the syrup drained from raw sugar,Molasses,General,In which novel of 1874 does Sergeant Troy appear,Far from the madding crowd +General,What is Challa,Bread - often plaited,General,"From which Congreve play comes the line, 'Music has charms to soothe a savage breast'",The mourning bride,General,Aerofloat Is The National Airline Of Which Country,Russia +General,What Canadian Prime Minister retired in 1984,Pierre Trudeau,General,A person 100 or more years old,Centenarian,General,Who was Douglas Elton Ullman better known as,Douglas Fairbanks Senior +General,Gabbro is which type of rock,Igneous,Science & Nature, The __________ continually grows new sets of teeth to replace old teeth. It also cannot move its tongue. The tongue is rooted to the base of its mouth.,Crocodile,General,What part of the polar bear is highly intoxicating to eskimos,Liver +General,"What nation's populace watches the most TV, per capita",Japan's japans japan,General,What links Dr Spock Errol Flynn and Emperor Nero,Olympics Rowing Boxing Chariot,General,In 1789 Britain legally adopted what officially,Hanging - before it was burning +Music,"""Blaze Of Glory"" Was A Hit In 1990 For Whom",Jon Bon Jovi,General,What is a percoid,A bony fish,General,Triplane driving: what country is identified by the letter i,Italy +General,What is the fear of dirt or contamination known as,Molysmophobia, Geography,What is the basic unit of currency for Namibia ?,Dollar,Music,"Who Sang About A Denim ""Demi-God"" In 1962",Mark Wynter / Venus In Blue Jeans +General,What state is the second largest of the U.S.,Texas,Music,"Whose Albums Include ""Reproduction"", ""Love And Dancing"", ""Dare""",The Human League,General,"In which State is the Little Bighorn, scene of Custer's last stand",Montana +General,What is the young of this animal called: Sheep,Lamb lambkins,General,What is another name for the carambula,Star fruit,General,"Where was Mohammed, the founder of Islam born",Mecca +General,What U.S. state includes the telephone area code 702,Nevada,General,Which European city is served by Leonardo Da Vinci Airport??,Rome,Sports & Leisure,In which sport is the America's Cup awarded?,Sailing +General,What's the capital of Majorca?,Palma,Science & Nature, The leech will gorge itself up to __________ its body weight and then just fall off its victim.,Five times,General,What is the zodiacal symbol for Capricorn?,Goat +General,What is the capitol of Belarus,Minsk,Technology & Video Games,What is the name of the Star Fox Team's cocky wingman? ,Falco Lombardi,General,Who was the Greek equivalent of the Roman god Vulcan,Hephaestus +General,What is the young of this animal called: Deer,Fawn yearling,General,What Does A Dendrologist Study,Trees & Shrubs,General,"What sport uses stumps, bails & bats",Cricket +General,What is an angle greater than 180 degrees and less than 360 degrees,Reflex,Art & Literature,What Is The Name Given To The Painting Medium Involving Egg Yolks ,Tempera ,General,What has a range of 28 miles,Aim-7 sparrow +General,How many months are there in a year,12,General,Approximately 40 million of what are consumed each year,Bananas,General,As what did Al Capone's business cards identify him,Furniture Dealer +General,Portable underwater breathing apparatus,Aqualung, History & Holidays,What was the last Port of call for the Titanic?,Queenstown, Geography,On which river was Rome built ?,Tiber +Food & Drink,Which countrys does one associate with the following foods or drinks 'Barmbrack' ,Ireland ,General,Jimmy Carter once thought he saw a UFO; what was it,Venus,General,Sam Weller was whose servant in a Dickens book,Samuel Pickwick +Food & Drink,Which countrys does one associate with the following foods or drinks: 'Sachertorte' ,Austria ,General,Who discovered the circulation of blood,William harvey,General,Who owned jerusalem before israel,Jordan +General,"The Russian revolutionary, Leon Trotsky, was exiled three times and eventually assassinated in which country",Mexico,Entertainment,Who sang about 'Commitment'?,Leann Rhimes, History & Holidays,Which great Renaissance figure had the surname Buonarroti? ,Michelangelo  + History & Holidays,Presepe in Italy refers to what Christmas tradition? ,Nativity scene (literally meaning crib) ,General,Who met at Ujiji in 1871,H M Stanley and Dr. Livingstone, History & Holidays,In 1960 who did Bobby Darin marry. ,Sandra Dee  +General,"Cardinal, Barlinka and Napoleon are varieties of what",Black Grapes, History & Holidays,What Is The Title Of The 1992 Belgian Film About A Crew Following The Expolits Of A Serial Killer ,Man Bites Dog ,General,Type of Dodge also a snake,Viper +General,What toy is manufactured to a tolerance of 5 thou of a millimetre,Lego,General,What Is The Auto Pilot Called In An Aeroplane,George,General,What was the first 90 minute TV series in 1962,The Virginian +General,What tropic passes through Mexico,Cancer,General,What is the most consumed fruit in the US,Coffee Bean,General,What sex is a filly,Female +Music,Concrete And Clay Was A 1964 Hit For Which UK Band,Unit Four Plus Two,General,Anna M Jarvis of Philadelphia instituted what she never qualified,Mothers Day May 1908 Childless,General,How many cars are permitted to take part in a Formula One race,26 +General,What's the worlds widest river,Amazon,Sports & Leisure,What sport do neither participents or spectators know the score until the end?,Boxing,Food & Drink,Which country is the ancient home of chocolate? ,Mexico  + Geography,In which continent would you find the Yangtze river ?,Asia,General,What term is used to describe the mind functioning as the center of thought and behavior,Psyche,General,What is the Taj Majal made of?,Marble +General,How many possible opening moves does white have at the start of a game of chess,20,Geography,Which Pacific Nation Gained Independence From New Zealand In 1962 ,Western Samoa ,General,Names from jobs - Baker Cook obvious what was a Pallistair,Fence maker +General,In what direction does the nile river flow,North,General,"What are chrysolite, beryl, jasper & tourmaline",Gems,General,48 extras from what Oscar win film died within a year making it,Babe - all pigs +General,"Brian eno released a us 1978 lp titled ""before and after _____""",Science,General,What is the correct name for The Laughing Jackass,Kookaburra,General,What is the name given to a white sauce flavoured with cheese,Mornay +Geography,Chicago stands on the banks of which lake? ,Michigan ,General,90% of bird species are what,Monogamous,General,Jesse Owens is associated with what sport,Athletics +General,Who said 'History is bunk'',Henry ford,General,Which musical instrument was originally called a busine,13th century trumpet,General,U.S. captials Washington,Olympia +General,Sylvester and Tweety Pie - Cat Bird started out as what,Clown and Baby,General,"Who published ""Poor Richards Almanack""",Benjamin franklin,General,Which country won the gold medal for football at the 1996 Atlanta Olympics,Nigeria +General,In Greek mythology Deianeira was the wife of who,Hercules,General,On which river does St. Petersburg in Russia stand,Neva,General,Path or trajectory of a body through space,Orbit +General,"Who was the backup singer on alice cooper's ""muscle of love"" lp",Liza,General,What were Twinkletoes - Lucky Jim (stuffed cats) first to do,Fly across Atlantic Alcock Brown,Entertainment,Who sang about Desmond and Molly Jones?,The Beatles +General,S J Perelman wrote the screen plays for many of the films starring which zany comedians,Marx brothers,General,Who developed the laws of electrolysis,Michael faraday,Music,Which Record Label Has Simply Red Recorded Under In The 90's,East West +General,Who invented the Parking Meter,Carlton c mcgee,General,"Which country has regions named Cyreniaca, Tripolitania and Fezzan",Libya,General,To who is a polar bear's liver highly intoxicating,Eskimos +General,Johnson who married george harrison's former wife,Eric clapton,General,"Who sculpted Rima, Genisis and Ecco Homo",Jacob Epstein,General,How many trouser legs are there in a standard Japanese kimono,None +General,What is the name of the smaller currency unit that an Austrian schilling is divided into,Groschen,Geography,"A whirlpool below ____________ iced over for the first time on record, on March 25, 1955.",Niagara falls,General,In which country would you find the Kremista Dam,Greece +General,Which dancing represents fertility through death and rebirth,Morris dancing,General,When was black the most common colour for automobiles,During the depression,General,Not Indians what links Cherokee Apache Arapaho Comanche,Piper Aircraft +Sports & Leisure,What Are The 6 Weapons Used In A Game Of Cluedo ,"Candlestick,Lead Pipe, Dagger, Spanner, Rope, Revolver ",General,"What are Eastern, Central, Mountain, and Pacific ?",American Time Zones,General,Which is the smallest u.s state,Rhode island + History & Holidays,What made the crew sick in the movie Airplane? ,The fish ,Music,"Name The Year Relax Reaches No.1 In The UK, ""This Is Spinal Tap"", Born In The USA",1984,General,In which country is Mount Ossa,Greece +General,Which religions name means The way of the Gods,Shinto,Music,"Who Recorded The 1969 Album ""Goodbye""",Cream,General,Which of the United States is nicknamed the Equality State,Wyoming +General,In what country is the town of Limoges,France,General,What whisky brand was advertised with two terrier dogs,Black & White,General,The average one has 248 muscles in its head - what,Caterpillar +General,Raw vegetebles served as an hors d'oeuvre are called what,Crudites,Science & Nature,Who made the first phone call to the moon?,Richard Nixon,General,What is the sum of 91 x 452,41132 +General,Which companies name translates as rising sun,Hitachi,General,Which composer and pianist became Prime Minister of Poland in 1919,Paderewski,General,What was an Egyptian king called,Pharaoh +General,What is the Capital of: Mayotte,Mamoutzou,General,In Greek mythology what type of creature was Chiron,Wise Centaur,Toys & Games,How many dots are on a twister mat?,Thirty +Science & Nature,Of What Is Renolgy The Area Of Study ,The Kidneys ,General,"Which species of animal has sub-species which include Maasai, Reticulated and Rothschild's",Giraffe,General,The first skyscraper in the United States was built in which city?,Chicago +General,Hotfoot Teddy was the original name of what icon,Smokey the Bear,General,Who won the 1979 Nobel Peace Prize,Mother teresa,Music,"Which Group Is Attributed To Introducing Dina Carrol On ""It's Tooi Late""",Quartz + Geography,Under what river does the Holland Tunnel run?,Hudson,General,In Portsmouth Ohio who does the law rank with vagrants thieves,Baseball Players suspicious chars,General,What do the initials j.p. stand for after a person's name,Justice of the peace +General,"What pop group had a ""Message in a Bottle""",Police,Geography,What is the capital of Taiwan,Taipei,General,George Washington became the first president of the U.S. in what year,1789 +General,In what city was the final of the 1991 Canada Cup played,Hamilton,General,Where is honshu island,Japan,General,What city was chosen but refused the 1976 Winter Olympics,Denver +General,If You Suffer From Aulophobia What Are You Afraid Of?,Flutes,General,According to a survey what is Americans favourite smell,Bananas,General,What is the Morse code representation for the letter T,Single dash +General,Which Former England International Was The First Player To Be Born After England's 1966 World Cup Victory,Tony Adams,General,Which club did Eusebio play for throughout the 1960s,Benfica,General,The blood of mammals is what color,Red +General,Who was the pop-up figure in the fun house in The Man With the Golden Gun,Al,General,What did Webster call his adoptive parents?,Ma'am and George,General,What was James Shalto Douglas claim to sporting fame,Marquis of Queensbury - Boxing +General,English law males should do it 2 hours week watched by vicar,Archery Practice,Sports & Leisure,What did jockey John White do in 1993 for which he is likely to be best remembered? ,Won Grand National that was void after false starts ,Geography,What is the capital of Greece,Athens + Language,What is the only English word formed by the first three letters of the alphabet?,Cab,Sports & Leisure,Who Were The Only 2 Players To Score For England In The 1986 Football World Cup Finals? (PFE) ,Gary Lineker & Peter Beardsley ,General,What is Romaic,The modern Greek language +General,"Which Singer Sang The Title Song To The Disney Movie ""The Aristocats""",Maurice Chevalier,General,What is unusual about mating in the whiptail lizard,No Males - females, History & Holidays,Which of Courteney Coxs co-stars from the Scream films is now her husband ,David Arquette  +General,Bell View in Manchester built in 1928 was Britain's first what,Greyhound Stadium,General,"Which film ends with 'after all, tomorrow is another day'",Gone with the wind,General,What was the first team to play in what is now Wrigley Field,Chicago whales +General,What numbers can be found on the back of the U.S. $5 dollar bill in the bushes at the base of the Lincoln Memorial,172,General,Teichi Igarashi climbed what mountain at the age of 99,Mount fuji,General,"Who said 'We're not in the hamburger business, we're in showbusiness'?",Ray Kroc +General,"What City is nicknamed the ""Glass capital of the world""","Toledo, Ohio", History & Holidays,What was the name of Norm's wife on Cheers? ,Vera ,General,A Scatologist studies what Excrement,Crap - Shit +General,What term is used for the technique of growing plants without soil,Hydroponics,General,Which U.S. president is on the five-dollar bill?,Abraham Lincoln,General,"What does a king cobra normally eat beetles, rodents or snakes",Snakes +General,What is the name of the largest gold refinery?,Rand Refinery,General,What was the first Disney animated film released on video,Dumbo,General,"Carnivorous mammal, native to the northern regions of North America, Europe, & Asia, whose habits are much like those of the badger",Wolverine +General,Whose record breaking performance in winning Olympic Gold in 1968 stood as a World record for 23 years,Bob beamon,General,What physical feature gives the platypus it's name,Webbed feet,General,Harry S Truman and Gerald Ford what's the non obvious link,Left handed +General,What were comfrey baths were believed to restore,Virginity,Music,Who was the first Beatles drummer?,Pete Best,General,What type of craft is the US's Airforce One?,Boeing 747 +General,The juice of which berry can be used to prevent and treat urinary tract problems,Cranberry,General,What continent boasts the greatst number of Roman Catholics,South america,General,"Which Band Formed in Sheffield Before Hitting The Big Time Were Previously Known As ""Vice Vesa""",ABC +General,What is a better known name of Switzerland's Mont Cervin,The matterhorn,Science & Nature,What genus of tree does the wattle belong?,Acacia,General,Ignoring cats what large animals purr when they are happy,Brahma Bulls + Geography,Which city is on the east side of San Francisco Bay?,Oakland,General,Les Reed wrote which famous song for a Welsh singer,Its not Unusual – Tom Jones,General,Name Donald Ducks father,Quackemore +General,The Algarve is in what country,Portugal, Geography,What ocean is found along the East border of Asia?,Pacific Ocean,Science & Nature,Which Country Is The Natural Habitat Of The Emu ,Australia  + Geography,Which South American country has both a Pacific and Atlantic coastline?,Colombia,General,Who appeared on the back of a u.s banknote in 1875,Pocahontas,Music,"Who Sang ""The Futures So Bright I Gotta Wear Shades""",Timbuk 3 +General,What British birds lay only one egg during the nesting season,Fulmar or Guillemot,Music,"What Year Was Prince's ""When Doves Cry"" Released",1984,General,In What Country Will You Find The Largest Waterfall In Europe,Norway +General,What fruit did Eve give to Adam in the bible,Fig – Apple mistranslation,General,Which Famous Newspaper Is Sometimes Referred To As “ The Old Grey Lady ”?,New York Times,General,Where would you find bead wires wrapping and sipes,On Tyres +General,Carara in Tuscany is famous for producing what,Marble,Geography,What is the capital of Kazakhstan,Alma_ata,Food & Drink,What French Word Is Used For The Water Ice Often Served Between Courses To Refresh The Palette ,Sorbet  +General,What is the answer to the meaning of life according to Douglas Adams in 'The Hitchhikers Guide to the Galaxy?,42,Music,Who Did Andy Bell Perform With In The Group Erasure,Vince Clark,General,What was the royal name 'mountbatten' originally,Battenberg +General,Parton what is the official birthplace of country music,Bristol,General,In the Old Testament who married his cousins Leah and Rachel,Jacob,Music,On Which Record Label Did The Bee Gees Release Their Many Saturday Night Fever Hits,RSO +General,Beijing drivers fined 40 Yuan doing what at pedestrian crossing,Stopping its illegal,Religion & Mythology,Who is the greek equivalent of the roman god Juno ?,Hera,Sports & Leisure,Which Sport Has The Davis Cup ,Tennis  + Geography,What is the most sacred river in India?,Ganges,General,What was the first British instrumental to top the USA charts,Telstar by The Tornados,General,What is known as amundsen scott station,South pole +General,Collective nouns - A nye of what,Pheasants,Music,What is Ringo Starr's real name?,Richard Starkey,General,What is the state capital of Washington,Olympia +Science & Nature,What is 'Nitrous Oxide' more commonly known as ,Laughing Gas ,Music,"Which 2 Are Missing From Dozy, Beaky & Tich",Dave Dee & Mick,Entertainment,This movie is about the migration of poor workers from the dust bowl to the Californian fruit valleys.,The Grapes of Wrath +General,What is the collective name for a group of larks called,Exaltation,General,Richard Carlisle invented an early vending machine selling what,Books,General,The furcula is what part of a bird,Wishbone +Food & Drink,"Booze Name: 1 1/2 oz. light rum, 1 lime, powdered sugar, fruit juice, blend w/crushed ice",Daiquiri,General,"Who was the last ""Emperor of India""",George vi,Art & Literature,Bob Kane created who?,Batman +General,What is the distance from earth to the sun,93 million miles,Music,Name One Of Big Country's Two Top Ten Hits Of 1983,Fields Of Fire & Chance,General,Marilyn Munroe was the model for which Disney Character,Tinker Bell +Music,Reaching No 3 What Was The first Singles Chart Entry For Smokie,If You Think You Know How You Love Me,Science & Nature,He founded our modern periodic table. Surname only?,Mendeleev, History & Holidays,In Which Year Was The Law Abolished That Allowed Witches To Be Burned ,1736  +Sports & Leisure,This sport allows substitutions without a stoppage in play.,Hockey,General,Which Italian city is at the heart of its fashion industry,Milan,General,In 19th century USA what was The Mongolian Curse,Opium +Religion & Mythology,Who is the Norse Watchman of the Gods ?,Heimdall,General,Before 1883 who were called kranks,Baseball fans – fan invented then,General,What building in New York has 43600 windows,World Trade Centre +Sports & Leisure,How many minutes is each period of hockey,20,General,Lack of vitamin B1 causes what condition,Beri Beri,General,What is the worlds largest herb,Banana +Music,Who Backing Group Is The Range,Bruce Hornsby, Geography,Boise is the capital of ______?,Idaho,General,In the book Treasure Island which character owns the Spyglass Inn,Long john silver +General,What is the original meaning of the word Shambles,Abattoir butcher),Music,"Name The Guitarist Thet Performed On Belinda Carlisles ""Mad About You""",Andy Taylor,Sports & Leisure,What Type Of Fruit Is Depicted On Top Of The Men's Singles Trophy At Wimbledon? ,A Pineapple  +General,On an ordinance survey map what does a H in circle represent,Hospital,General,What can stop the Duke of Earl,Nothing,General,The Thunderbirds boys were named after what theme,Apollo Astronauts + History & Holidays,He discovered the Grand Canyon.,Francisco Coronado,General,Which country had the first state victims compensation scheme,New Zealand in 1960,General,"In ""Family Ties"" who was Alex P. Keaton's idol?",Richard Nixon +General,What countries flag has two bars white top red bottom,Poland,General,From Which Country Do French Fries Originate?,Switzerland,General,Who composed the Brandeberg concertos full names,Johan Sebastian Bach +Music,"Who Asked ""Are You Gonna Go My Way"" In 1993",Lenny Kravitz,General,What media icon invaded Network 23?,Max Headroom,General,What is a group of nightingales,Watch +General,What is a group of this animal called: Rat,Pack swarm,Science & Nature,"Complex Numbers Have 2 Components , Name Them ",Real & Imaginary ,General,Who was the founder of the Quakers (both names),George Fox +General,"At one time, 6 white beads of this Indian currency were worth one penny",Wampum,General,Spiral slide at a funfair,Helter-skelter,General,Nessus killed Hercules - What was Nessus,Centaur +General,What was the name of the medical journal established by Dr Thomas Wakely,The lancet, History & Holidays,Apart From Anthony Perkins Which Other Performer From Psycho Also Appeared In Psycho 2 (23 Years Later) ,Vera Miles ,General,Phagophobia is a fear of ______,Swallowing +General,"""7X"" was used to refer to the secret ingredient of which drink?",Coca Cola,Music,Estimate of Paul's worth,$600 million,General,"Man At Saint Marks Her headline hit in 1984 was ""Girls Just Want To Have Fun""",Cyndi lauper +General,What was Paul the Apostles real name,Saul,General,"What's the international radio code word for the letter ""D""",Delta,General,What is a group of rabbits,Warren +Sports & Leisure,"What in sport, is 7 feet 9 quarter inches(2.375 metres) in length? ",The Oche in darts ,General,In Heraldry there are 60 varieties of what,Cross,Science & Nature,What Is The Common Name for Alopecia ,Baldness  +Geography,What lake is the source of the white nile ,Lake victoria ,Science & Nature,Who First Used Electric Ignition By Battery & Coil In 1886 ,Karl Benz ,General,"In the film 'star trek first contact', when picard shows lilly she is orbiting earth, australia and papua new guinea are clearly visible, but which country is missing",New zealand +General,"Kaleidoscope. Daddy, Palomino and Changes name the author",Danielle Steele,General,Who Won The BBC Sports Personality Of The Year In 1992,Paul Gascoigne,Music,Which Musician Actually Created Mantovani's Unique Sound,Ronald Binge +General,In 1940s California it was illegal to serve alcohol to who,Homosexuals,Food & Drink,Who Sang The 1967 Hit 'Drink Up Thy Zider' ,The Wurzels ,General,In Elkhart Indiana it's illegal for a barber to threaten to do what,Cut off youngsters ears +General,What show/game has characters such as bulbasaur and pikachu,Pokemon,Music,Who is Dr. Winston O'Boogie?,John Lennon,People & Places,How is Meg Lake better known? ,Mystic Meg  +General,Which group of professionals use computer dating the most,Teachers,Science & Nature,What is the smallest irreducible constituent of a chemical element known as? ,An Atom ,General,In Prokoviev's Peter and the Wolf what instrument is the wolf,Horn +General,What is another name for termites,White ants,Sports & Leisure,What Is The Name Of The White Ball In A Game Of Bowls ,The Jack ,Food & Drink,"If food is `au naturel', what does this mean? ",Plainly cooked or uncooked  +Music,"Who Had A Hit In 1991 With ""Bitter Tears""",Inxs,General,Which poet wrote A thing of beauty is a joy forever in Endymion,Keats, History & Holidays,What is the name for a noisy ghost or spirit ,Poltergeist  +General,Tintoretto did most of his painting in what city,Venice,General,Fragrant flowering African bulb,Freesia,General,"Which group sang the song ""Its Gonna Be Me""?",N'Sync +General,"Cheyenne, Navahoe and Arapaho are all what",Native american tribes,General,"In which musical is the song, 'Hey Big Spender' featured",Sweet charity,General,Give Me Just a Little More Time' was recorded by which group in 1970,Chairman of the Board +General,Arachnoid refers to what kind of creature,Spider,General,Which British poet was also a Jesuit priest,Gerald Manley Hopkins,General,"Which book begins ""Christmas wouldn't be Christmas without any presents""",Little women +Music,What was the title of Ike and Tina Turners only album?,"River Deep,Mountain high",General,What is the flower that stands for: refusal,Striped carnation,General,What do the letters IMF mean,International monetary fund +Music,"Which label released Donna Summer""s ""Love To Love You Baby""?",Casablanca,Science & Nature,"This poisonous, oily liquid occurs in tobacco leaves.",Nicotine,General,In what case did Perry Mason make his first appearance,The case of the Velvet Claws +General,In the Bible from whom did David steal his wife Bathsheba,The Hittite warrior Uriah,Music,What Was Chuck Berrys Only Number One,My Ding A Ling,General,In 1967 an Australian had one 11lb in weight - what,Carrot +Art & Literature,In which British national daily newspaper does Rupert The Bear appear? ,The Daily Express ,General, A person who starts fires maliciously is a(n) _________.,Arsonist,Toys & Games,You rotated blocks and matched up the colors to solve this puzzle.,Rubiks cube +Music,Billy Idol Quit Which Band To Embark On His Solo Career,Generation X,General,Which mountain was the home of the Greek Gods,"Mount olympus,", History & Holidays,On Dec 1st 1955 A Woman from Montgomery Alabama hit the headlines after refusing to give up her seat on a bus to make way for a white person what was her name? ,Rosa Parks  +General,"In 1986, what was the maximum fuel capacity imposed in formula 1 racing",One,Geography,In which state is the Mayo Clinic,Minnesota,General,"A mashie, niblick and wedge are types of what",Golf clubs +General,What is the name of the theme song for The People's Court,The Big One,General,Petrography is concerned with what,Rocks,General,What is the young of this animal called: Partridge,Cheeper +Science & Nature,What Is The Most Abundant Radioactive Element ,Uranim (238) ,Science & Nature," Two rats can become the progenitors of 15,000 rats in less than",1 year,General,Leather Apron was an alternative name for what famous figure,Jack the Ripper +General,Which island was the site of the Australian Grand Prix,Philip Island,General,Dutch cheese with red rind,Edam,Geography,In what city is the leaning tower ,Pisa  +General,In French legend who is the lover of Abelard,Heloise,General,What are the ingredients of a daiquiri,Rum and lemon,General,The word opera is a plural of opus meaning what,Grand Work +General,The religious text Tripitaka comes from which religion,Buddhism,General,The ________ kills more people world wide than all the poisonous snakes combined,Honeybee,General,Where can you find Clippit,Excel help assistant paperclip +General,In Feng Shui what colour inspires passion,Pink, Geography,What is the basic unit of currency for Trinidad and Tobago ?,Dollar,General,"In Judaism there are 3 cardinal sins Idolatry, Adultery and what",Murder +Sports & Leisure,Which Football Team Were Originally Nicknamed The Biscuitmen? ,Reading ,General,"What presidential ticket featured the slogan: ""get america moving again""",Carter and mondale carter mondale carter and mondale,General,What is the most popular type of holiday greeting card mailed in the united states,Christmas +Sports & Leisure,What Is The Official Circumference Of A Football ,27-28 Inches ,General,The fennec is the smallest of its species - the smallest what,Fox,General,Which country produces the most full length feature films,India +General,Which Australian writer won the Nobel prize in 1973,Patrick White,General,What was the name of the submarine that sank in the Barents Sea in August this year,Kursk, History & Holidays,What was marco polo's home town ,Venice  +Tech & Video Games,How many fighters are playable in 'Street Fighter II'? ,8,General,What season begins with the vernal equinox,Spring,General,The first one was delivered in 1933 - the first what,Singing Telegram +General,"Who recorded the soundtrack for the movie ""flash gordon""",Queen,General,In the Solar system there are 2 Mount Olympus's Greece and where,Mars,General,What common word comes from two Greek art/craft area study,Technology tekhne logia +General,What sort of ship was the Marie Celeste,Brigantine,General,What is the product for the slogan 'it has seven natural fruit juices in it',Hawaiian punch,Music,"Which Band Proclaimed ""We Don't Need No Education""",Pink Floyd +General,"In Jumanji, a stampede is released. What is the slowest animal?",Rhinoceros,General,Who wrote The Ugly Duckling,Hans christian anderson,Music,"What Song Features The Lyric ""I Was Working As A Waitress In A Cocktail Bar""",Don't You Want Me Baby +People & Places,Which Footballer Famously Slept With Several Miss Worlds Before Leaving Manchester United & Moving To Fulham ,George Best ,Science & Nature,Which Family Of Plants Sends Up Shoots In The Shape Of Croziers ,The Fren ,Science & Nature, A horse can sleep __________,Standing up +General,"What submarine had logged over 100,000 miles before being refueled in 1958",Uss nautilus,General,In which park are Queen Mary's gardens?,Regents Park,General,Name was the first city to mint its own gold coins in 1252,Florence – florin +General,As who is Vincent Furnier known?,Alice Cooper,General,What is attribution of divine honours on persons (living or dead) called,Apotheosis,General,George washington carver advocated planting peanuts and sweet potatoes to replace what,Cotton and tobacco +General,What is the highest waterfall in the Alps,The Richenbach Falls,General,"What character has been portrayed by reginald owen, alistair sim and albert finney",Scrooge,General,Petilent wine is what,Slightly sparkling +General,What was the name of the saloon in Gunsmoke,Longbranch,General,What is the legislative capital of South Africa,Cape town,General,The hood on a monks habit is call a,Cowl +General,Which Was The First University To Win University Challenge?,Leicester,Food & Drink,A Calzone Is A Folded Stuffed What? ,Pizza ,General,What does a racoon do before eating its food,Washes it in water +Science & Nature, The pronghorn __________ can run up to 61 miles per hour.,Antelope,Geography,Only one Canadian province borders at least one Great Lake: _________,Ontario,General,Which East African leader gave himself the title of 'Conqueror of the British Empire',Idi amin +General,What is the Welsh name for Wales,Cymru,General,How many black union soldiers were awarded the Congressional Medal of Honor,Sixteen,General,Francophobia is a fear of ______,Anything french +General,To camber something is to ______ it:,Curve,Food & Drink,What is severe Vitamin C deficiency called? ,Scurvy ,General,"The leaves of the tomato plant are poisonous, they contain ________?",Strychnine +General,On which canal is the Bingley Five Rise series of locks?,Leeds-Liverpool Canal,General,What does the red blood cell not have,Nucleus, History & Holidays,This frontiersman and politician was killed at the Alamo.,Davey crockett +General,What Canadian sketch comedy show helped launch John Candy's career?,SCtv,General,Who captured esquire's 'most dubious man of the year' award in 1980,Billy,Music,What Nationality Is KD Lang,Canadian +General,Jerome Siberman became famous as who,Gene Wilder, History & Holidays,Who fiddled while Rome burned?,Nero,General,Grenadine is a syrup from the juice of which fruit,Pomegranate +General,Which animal has the latin name 'Bos grunniens',Yak,General,The' Long John Silver Collection' housed on the Cutty Sark is the nations largest collection of what,Ship's figureheads,General,What does 'unicef' mean,United nations childrens' emergency fund +General,Palas is the correct name for what playing card,Queen of spades, History & Holidays,What did Peter Minuit buy in 1626 ?,Manhattan Island,General,Axe with an arched blade at right angle to handle,Adze +General,"The play ""Our Town"" is set where",Grover's corners,General,"In hockey, what is the equivalent of a rugby scrum",Face-off,Science & Nature,What is the name given given to the lowest temperature theoretically possible ,Absolute zero  +General,Which gas was named after the Greek word for 'sun',Helium,General,At which university did Spike Lee teach,Harvard,General,A Parthenophobe has a fear of what,Young Girls - Virgins +Music,Whose Real Name Is Cherilyn Sarkisian La Pier,Cher,General,What is the fear of contracting poliomyelitis known as,Poliosophobia,General,How long is an emerald anniversary,Sixty years +General,"Reaching number two in the UK charts in 1991, which was the first hit single for Right Said Fred",I'm too sexy,Sports & Leisure,Which English football team are nicknamed the Tractor Boys? ,Ipswich , History & Holidays,Which 60s film was based on the true 'life' story of Robert Stroud ,The Birdman of Alcatraz  +Music,"Who Had A Hit With The Song ""Say You Say Me""",Lionel Richie,Music,Hole Singer Courtney Love Was Married To Which Nirvana Star,Kurt Cobain,Geography,What continent's westernmost point is called cape verde ,Africa  +General,What was the name of the ship that brought Dracula to England,Demeter,General," A clip, shaped like a bar to keep a woman's hair in place is a _______.",Barrette, Geography,What is the correct name of Bangkok?,Krung Thep +Science & Nature,For What Is NA The Chemical Symbol ,Sodium ,Art & Literature,Who Wrote Jurassic Park ,Michael Crichton ,Geography,In which state is walla walla ,Washington  +Science & Nature,Where Were The Worlds First Windmills? ,In Iran In The 7th Century ,Science & Nature," Elephants perform greeting ceremonies when a member of the group returns after a long time away. The welcoming animals spin around, flap their ears, and __________",Trumpet,General,Who's Christian names inc Johannes Chrysostomus Theophilus,Wolfgangus Mozart +General,Who did Valerie Solernis shoot on Jun 3rd 1968 in New York,Andy Worhole,General,Which part of the body is operated on in a menisectomy,Knee, Geography,What is the basic unit of currency for Myanmar ?,Kyat +General,Of which country was Anastasio Somoza president during the 1960s and '70s,Nicaragua,Entertainment,Donald Duck comics were banned in Finland because he didn't wear ______?,Pants,Food & Drink,Which alcoholic drink is named after a Welsh Pirate? ,Captain Morgan  +General,In which battle were 4 japanese carriers destroyed,Midway,General,Who was Elia,Charles lamb,General,"Name the title which, together with Berlin Game and London Match completes Len Deighton's trilogy",Moscow set +Food & Drink,Which seed is the basic ingredient for tahina paste ? ,Sesame ,Food & Drink,What Are Prunes ,Dried Plums ,General,What is the SI unit of force,Newton +General,Sissy Jupe adopted by Thomas Gradgrind which Dickens novel,Hard Times,General,What does Vodka literally mean,Little Water,General,Trypanophobia is fear of what,Inoculations – Injections +General,What are you forbidden to fly an airplane over in india,Taj mahal,Entertainment,"In 1958, who had a pop music hit with 'Willie and the Hand Jive'?",Johnny Otis,General,Where were the 1952 Olympic games held,Helsinki +Science & Nature,Which Is The Largest Of The Poisonous Snakes? ,The King Cobra ,General,What is Martha's Vineyard,An island,Music,"For Which Group Did ""Candy Girl"" Reach No.1 In 1983",New Edition +General,"What was the primary occupation of characters in ""Falcon Crest""?",Vineyard owners.,General,What brassy songstress played lola lasagne on tv's batman,Ethel merman,General,What does a marsupial mouse have that other mice don't,A pouch pouch +General,Zerelda was the first name of what outlaws wife and mother,Jesse James,General,Who played steve erkel in 'family matters',Jaleel white,General,"In Which Movie Blockbuster Does ""Susan Backlinie"" Play The Character Christine ""Chrissy"" Watkins",Jaws +General,How long did Yuri Gagarin's first orbit round the Earth take,108 minutes,General,Who was igraine,King arthur's sister,Religion & Mythology,Which ancient continent is said to be submerged?,Atlantis +Tech & Video Games,How many bits are in a nibble ?,4,General,U.S. Captials - Nevada,Carson City,General,"What product is sold with the slogan ""just for the taste of it""'",Diet coke +General,As what is Constantinople now known,Istanbul,Food & Drink,"Which drink, one of Shakespeare's favourite tipples, is distilled from honey? ",Mead ,General,"Investigation what group's top selling lp was ""rumors""",Fleetwood mac +Art & Literature,Which French Authors Novel Germinal Depicts Life In A Mining Community ,Emile Zola ,General,Every 12 seconds in USA someone does what in a Holiday Inn,Steals a towel,General,What percent of ticket sales did arthur rubenstein demand from concerts,70 +General,A Regatta is a boat races - where was the original Regatta,Venice,General,Where is the US masters golf tournament always played,Augusta Georgia,Geography,"Name the sea north of Murmansk, Russia.",Barents +Science & Nature,What Is Caused Through A Spasm In The Diaphram ,A Hiccough ,General,"A ""Howards Lancer"" Is A Variety Of Which Type Of Fruit",Gooseberry,General,Glascow was voted the European City of Culture in which year,1990 +General,Torme Music: Who recorded Heartbreak Hotel in 1956,Elvis presley,General,"Who assisted behind the bar in ""Cheers"" before Woody",Coach,Music,Madonna Has Been In The Public Eye For Several Years But What Does Madonna Actually Mean?,My Lady +General,Who first wrote about the myth of Atlantis,Plato,General,In military terms what is a SLR,Self Loading Rifle,Sports & Leisure,"Which sport has a movement called a ""telemark""",Skiing +General,Slugs have 4 ______,Noses,General,What did neptune hold in his hand,Trident,General,The young Raphael sketched which famous work by Leonardo da Vinci while it was being painted,The mona lisa +General,What is a group of ravens,Unkindness,General,What north american indian peoples' name meant 'the ancient ones',Anasazi,General,"Which country cancelled national beauty contests 1992, claiming they were degrading",Canada +Sports & Leisure,What sport is played at Wrigley Field in Chicago Illinois - ,Baseball ,Science & Nature," The __________ snake found in the state of Arizona is not poisonous, but when frightened, it may hiss loudly and vibrate its tail like a rattlesnake.",Gopher,Music,Which 2 Records Both Reached No 12 For Judas Priest In 1980,Living After Midnight / Breakin The Law +Music,"Who Had Hits With The Songs ""Cambodia"" & ""You Keep me Hanging On""",Kim Wilde,General,"On 'the roseanne show', as what was david jacob known",D.j,General,What is another name for a fruit that is often called a pawpaw,Papaya +General,Coal is sometimes added to softdrinks to make them ______,Sweeter,Science & Nature, __________ of South and Central America and the Caribbean lay their eggs in February and March.,Iguanas,General,What bumbling andy griffith show character has the middle initial b,Barney +General,What is the process by which certain animals are able to reproduce themselves in successive female generations without intervention of a male of the species,Parthenogenesis,General,N. American wild dog,Coyote,General,Nephophobia is the fear of,Clouds +General,Hagiology is the study of what,Saints,General,Who is the silhouette on the major league baseball logo,Harmon killebrew,General,"Who is a successful recording artist, talented landscape artist, and author of children's books",Ricky van shelton +Science & Nature,Which Term Is Used For Electronic Intruments For Use In Aviation ,Avionics ,Music,Which Beatles song is credited to Both George Harrison & John Lennon?,Cry For A Shadow,General,What was the first TV show in colour,The Cisco Kid +General,Who is also known as Mr. Warmth?,Don Rickles,General,What is a group of kittens,Litter,Geography,What is the capital of Slovenia,Ljubljana +Sports & Leisure,What Is The Value Of The Letter (Q) In Scrabble ,10 points ,Sports & Leisure,From Which Football Club Did Arsenal Sign Thierry Henry? ,Juventus ,General,What is the only country in the world that starts with the letter O in English,Oman + History & Holidays,What is the weight of the largest pumpkin in the world is it A 1046 lbs B 1246 lbs or is it C 1446 lbs ,C 1446 lbs ,Geography,What is the capital of Moldova,Kishinev,General,What is the longest undammed river west of the Mississippi,Yellowstone river +Science & Nature, Deer have no __________,Gall bladders,General,Which mountain range forms a geographical boundary between Europe and Asia,Urals,General,Raiders what is the oakland raiders' motto,Commitment to excellence +General,What was the first battleship powered by steam turbines 1906,HMS Dreadnought,General,"New Zealand's National Day, the 6th February, is named after a Treaty of 1840. What is the day called",Waitangi day,General,Guiseppe Verdi wrote Aida - in what city was it premiered,Cairo + History & Holidays,What was the name of Charles Lindbergh's plane in which he completed the first non-stop solo trans-Atlantic flight? ,Spirit of St Louis (achieved in 1927) ,Entertainment,"Tarzan had a chimpanzee, what was his name?",Cheetah,General,In which country was Lord Beaverbrook born,Canada +General,What was a Royal Navy frigate accused throwing Cod War 1973,Carrots at Icelandic Gunboat,General,Country celebrates Aug 11 as independence day from France,Chad,Food & Drink,Which Famous Chef Released The Controversial Cookbook 'How To Cheat'' In 2008 ,Delia Smith  +General,Capital of Argentina,Buenos aires,General,What us state is columbia university located in,New york,General,Given 10 Years Either Way In Which Year Was The Mona Lisa Started,1503 +Geography,What is the capital of Gambia,Banjul, History & Holidays,What was the rank of Mark Phillips when he was married to Princess Anne in 1973? ,Lieutenant ,General,Whish actress was nominated for an Oscar for her part as a prostitute in the 1995 film Leaving Las Vegas,Elizabeth shue +Entertainment,"Who did the voices of Bugs Bunny, Sylvester and Tweety Pie?",Mel Blanc,Sports & Leisure,In 1970 What Did Football Referees Get That They Didn't Have Before ,Red & Yellow Cards ,Geography,In which city is the c.n. tower ,Toronto  + Language,What is the Israeli 'knesset'?,Parliament,General,What instrument does woody allen play,Clarinet,Entertainment,Name the fastest mouse in all of Mexico.,Speedy Gonzalez +General,Which is Britain's largest native carnivore,Badger,General,In cookery a ganache is made from cream and what,Chocolate,General,"In football, where are the hashmarks",Five-yard lines +General,What country calls itself Suomi,Finland,General,Which US born poet won the Nobel Prize for Literature in 1984?,TS Eliot,General,Who was captain of the Titanic,Edward smith +General,What is the 'word' used for multiple personality disorder,Mpd,General,What is singer Elaine Paige's nickname,Leather Lungs,General,Pip' is the hero in which novel by Charles Dickens,Great expectations +General,Lampy is the worlds oldest 1840s insured 1 million oldest what,Garden Gnome,General,Canaan Banana was the first president of where,Zimbabwe,Science & Nature,In what does a rhinologist specialise?,Human nose +General,Who directed 'the breakfast club',John hughes,Food & Drink,At That Temperature Centigrade Does Water Boil At ,100 Degrees C ,General,Leonardo Michelangelo Raphael How many squares are on a Shogi (Japanese chess) board,81 +General,Struthio Cameus is the Latin name of which creature,The Ostrich,General,Henry Fords first car lacked something we expect - what,Reverse gear,General,What is a tourbillion,Whirlwind +General,"What African country's name is Latin for ""free""",Liberia's liberias liberia,General,Name Eleanor Roosevelt's job 1932 husband became president,Editor of magazine Babies,General,Which Roman Emperors name means little boats,Caligula +General,President Hayes 1878 started which annual White house event,Easter Egg Roll,General,In 1900 caterer Harry Stevens introduce what words to language,Hot Dogs,General,20% of women first look at a mans what,Butt not face +General,Which Victorian became the world's most famous sufferer of neurofibromatosis,John merrick/elephant man,General,Where were both donny and marie osmond born,"Ogden, ut",Geography,Which European Country Generates The Highest Proportion Of It's Electricty By Hydroelectric Power Stations ,Norway  +Music,Who Was Jailed For His Behavoir On A Flight From Paris To Manchester In february 1998,Ian Brown,General,Nocturnal burrowing animal with black and white striped head,Badger,General,"Australian highwaymen or robbers who, in the late 18th and early 19th centuries, terrorized settlers in the New South Wales and Victoria regions",Bushrangers +People & Places,By what name is Robert Zimmerman better known as?,Bob Dylan,General,Who composed the musical work Fingal 's Cave,Mendelssohn,General,Whose autobiography is titled 'groucho and me',Groucho marx +General,What is measured on the Cephalic Index,Human head,Food & Drink,What are the main ingredients of a Horse's Neck? ,Brandy & Ginger ,General,In what Australian state would you find Swan Hill,Victoria +General,Men play it at 40 feet women 30 feet what game,Horseshoes,General,In wacky races what were the gang of criminals called,The Ant Hill Mob,General,What is the Capital of: Italy,Rome +General,The world's fastest reptile (21.7 mph) is a type of what,Iguana,Geography,Is The Tropic Of Capricorn North Or South Of The Equator ,South ,Sports & Leisure,Which sport is played by the Minnesota Twins? ,Baseball  +General,What is used as the basis of tequila,Cactus,Music,Which Pop Trio Disbanded When Charlie Simpson Announced He Was Leaving The Group?,Busted,Science & Nature," A jynx is a __________, also know as the wryneck because of its peculiar habit of twisting its neck.",Woodpecker +Entertainment,What does the term 'DJ' mean?,Disc Jockey, History & Holidays,Who was George Washington's vice-president?,John Adams,Geography,The _________________ is the lowest country in the world. It is estimated that 40 percent of the land is below sea level.,Netherlands +General,What is Pogonophobia the fear of,Beards,Music,"""From The Cradle"" Was A No.1 Album For Which British Legend In 1994",Eric Clapton,General,Of what are the central processing units of modern computers mostly composed,Silicone +General,With what sport was mildred ella didrikson associated,Athletics,General,Who played richie in 'happy days',Ron howard,General,What is the fear of stars known as,Siderophobia +Geography,What Is The National Airline Of Russia ,Maya Island Air ,General,John Glen first USA to orbit earth was in which service,US Marine Corps,Music,"""All That She Wants"" Was A Hot Hit For Which Swedish Pop Group",Ace Of Base +General,Clarence Birds Eye had what job,Fur Trader,General,What month was named after Latin for to open,April,General,What was Donald Fagen's first solo album title (1982),The Nightfly +General,What Gift Would You Buy Someone Who Is Celebrating Their 15th Wedding Anniversary,Crystal,General,Porgie in which book did the heroine serve as governess to the ward of the mysterious and moody edward rochester,Jane eyre, History & Holidays,"Who was the lead singer for Creedence Clearwater Revival, and recently released 'Blue Moon Swamp'?",John Fogerty +Toys & Games,"These are the two highest valued letters in ""Scrabble"". ""Q"" and _____.",Z,General,What is a group of this animal called: Mole,Labour,General,Which Fictional Literary Character Has Been Transformed Into The Most Movies?,Dracula +General,Which band recorded the live album 'Live and Dangerous',Thin lizzie,General,What's the most popular sport in South America,Soccer,Science & Nature,What fleshy muscular organ is joined to the hyoid bone?,Tongue +General,The Simpsons are the longest running cartoon who is second,The Flintstones,Geography,Which state is divided into two parts by a large lake,Michigan,Food & Drink,What jelly is traditional accompaniment to Lamb ,Redcurrant  +General,"""There and back Again"" is an alternative title of which novel",The Hobbit,Science & Nature,A rhinoceros has __________ toes on each foot.,3,General,Phallophobia is the fear of,Penis +Food & Drink,What type of food is a bloomer? ,Bread ,Sports & Leisure,Colin McRae joined which rally team in 1991? ,Subaru ,Music,Which Donovan hit was inspired by the rumour that you could get high by smoking dried bananas?,Mellow Yellow +General,The Daleks come from what planet ,Skaro,General,Old Trek: Who is above and below Scotty in the Enterprise's chain of command,Spock sulu spock and sulu,General,In Which Classic 80’s Tv Show Would You Find A Shepherd and His Lost Sheep?,The Dukes Of Hazzard +General,What is the oldest known science,Astronomy,Food & Drink,What is a cross between a blackberry and a raspberry ,Tayberry ,General,Who was Hiawatha's father,Mudjekeewis – The West Wind +Geography,In which English county is the town of Crook? ,Durham ,Entertainment,"In which Disney movie is the song ""So This Is Love""?",Cinderella,General,In England what is the most popular girls name of the 90s,Rebecca +Food & Drink,"A food labelled ""Florentine"" is prepared with this.",Spinach,General,What flavour sweet was created for Ronald Regan,Blueberry Jelly Babies,General,Joe Yule jr born 1920 became famous as who,Mickey Rooney +General,What is the name for Russian beet soup,Borscht,General,"Devil who played god in 'oh god, book ii'",George burns,Science & Nature, The bite of a leech is painless due to its own __________,Anaesthetic +General,Poenosis is what medical condition,Chilblains, Geography,What is the windiest place on earth?,"Mount Washington, New Hampshire",General,The major religion in Haiti is?,Voodoo + Geography,What is the basic unit of currency for North Korea ?,Won,General,Who might use ruddle or what is it,Shepherds dye marking sheep,General,"Where was the capital, established in 1862, of the Confederate States of America","Richmond, virginia" +Entertainment,What is Vanilla Ice's real name?,Robert van Winkle,General,Year Martin Luther was born,1483,General,What film won the best sound effects Oscar in 1985,Back to the Future +General,Night live to where did jackie gleason move his 1960's variety series,Miami,Music,Murray Head Spent One Night Where,In Bangkok,General,What is the maximum distance between the moon and the earth,"253,000 miles" +General,Who tells stories about Brer Rabbit,Uncle remus,Music,Which Gallagher Brother Is The Principal Songwriter In Oasis,Noel,General,Alfred Hitchcock admitted to being terrified of what,Policeman +General,"Who married antonio banderas, her co-star in the film too much",Melanie Griffith,Food & Drink,What Is Ravioli ,Pasta Parcels Filled With Meat Sauce ,Sports & Leisure,If you bet a Lady Godiva what would be your stake? ,5 Pound  +General,With what are alligators often confused,Crocodiles,General,The Jasmine Revolution took place in which country?,Tunisia,General,What is a leprechaun's usual job,Cobbler or shoemaker +General,Which Pop Singer Was Born Leslie Sebastian Charles,Billy Ocean,General,What was the name of the largest british battleship in wwii,Hms vanguard,Science & Nature,What type of animal is a wallaby,Kangaroo +General,What was duke ellington's real name,Edward kennedy,General,Who Was The First Footballer To Score 100 Goals In Englands Premiership,Dwight Yorke,General,What is a sorcerer who deals in black magic called,Necromancer +General,What black magazine was founded by john h johnson in 1947,Ebony,General,Which planet in the solar system has the longest day?,Venus,General,What does a phycologist study,Algae +Science & Nature,A group of gorillas is known as a ___________.,Band,General,In 1810 in England Peter Durand invented what,Tin can (food),General,Greek mythology which character was raised by centaur Chiron,Jason - of Argonauts +Music,Which Road Could The Nashville Teens Be Found On,Tobacco Road, Geography,What is the basic unit of currency for Somalia ?,Shilling,General,North Fork Roe River - worlds shortest - which US state,Montana +General,Vladimir Nabokov wrote Lolita in what language,English,General,What is the largest moon of Saturn called,Titan,Music,Don Henley & Glenn Frey Were Both Members of which band,The Eagles +Geography,Which of the 48 contiguous states extends farthest north,Minnesota,Food & Drink,What is gazpacho? ,Cold Soup ,General,What did the three bears leave to cool when they went for a walk,Porridge +General,Where is tabasco,Mexico,General,What thick metal plate is thrown in the olympics,Discus,General,Somewhere My Love was the theme song of which movie,Doctor Zhivago +General,What colour is the purple finch,Crimson,General,Lara Croft is a character in which computer game,Tomb raider,General,What herb gets its name from Latin for jewel of the sea,Rosemary – Ros Marinas +General,What name is given to a male witch ?,Warlock,General,Who played Pink in the movie The Wall,Bob Geldorf,General,"What group of american indians formerly ranged from texas and arizona, south to the mexican state of durango, until rounded up in 1886",Apache + History & Holidays,Orson Wells directed Charlton Heston and Janet Leigh in which 1958 film? ,Touch Of Evil ,General,Who wrote The Dong with the Luminous Nose and The Jumblies,Edward Lear,General,Name the eighties sitcom in which Bob Ueker was upstaged by an obese butler regularly.,Mr. Belvedere +Music,"The Man Responsible For St Celia's 1971 Hit ""Leap Up And Down"" (Wave Your Knickers In The Air) Had Earlier Received Had Earlier Received A Degree From Trinity College Cambridge Who was He",Jonathan King,General,Who wrote the 1977 TV mini-series Roots?,Alex Haley,Science & Nature," are the only truly social cat species, and usually every female in a pride, ranging from 5 to 30 individuals, is closely related.",Lions +Sports & Leisure,Football: The Minnesota _______.,Vikings,General,What tribe of American indians lent their name to a punk rock haircut,Mohawks, Geography,What is the capital of Cape Verde ?,Praia +General,What is sometimes nicknamed Adams Profession,Gardener,Science & Nature,What Name Is Given To The Study Of Insects ,Entomology ,General,Detective Philip Marlow smokes what brand,Camels +General,What is a group of this animal called: Greyhound,Leash,General,Did you know that there are ______ flavored pez,Coffee,General,Jane Taylor 1783 1824 wrote what famous verse,Twinkle-Twinkle Little Star +General,"About which Prime Minister was it said ""He could never see a belt without hitting below it""",Lloyd george, History & Holidays,The Character 'Jack Skellington' Appears In Which 1993 Film ,The Night Before Christmas ,General,According to Gene Kelly who was his favourite dancing partner,Fred Astair +General,"The Little Bighorn, scene of Custer's last stand, is in which U.S. State",Montana,Science & Nature, __________ have the best eyesight of any breed of dog.,Greyhounds,General,"Sung by the bee gees in 1978, the song is called 'stayin' ______'",Alive +General,West End Girls was the debut hit for what pop duo in the 80s,Pet shop boys, History & Holidays,Who Released The 70's Album Entitled Red Headed Stranger ,Willie Nelson ,General,Sigmund Freud used a dog to help his psychoanalysis what breed,Jo-Fi a Chow +Music,"Who Joined Rod Stewart And Bryan Adams On The 1994 Hit ""All For Love""?",Sting,General,"Who is Private Eyes ""First Lady of Fleet St""",Glenda slagg,General,What is a logogram - a $ sign is one,Sign representing a complete word +General,Which river runs through St Petersburg,Neva,General,What civil war was fought between 1936 and 1939,Spanish civil war,General,What term comes from the catholic church practice of appointing someone to give opposing views on a nominee for sainthood,Devil's advocate +General,What is the capital of colombia,Bogota,General,In what area of Washington does the State Dept hang out,Foggy Bottom,General,In Which European City Was The First Ever Motorway Built In 1921,Berlin + History & Holidays,What was the board game introduced in the eighties which featured six categories of questions and little pie shaped pieces you had to collect? ,Trivia Pursuit ,General,Where was president truman born,"Lamar, missouri lamar missouri",Sports & Leisure,Which is the only position in soccer allowed to handle the ball,Goalie +Music,Who Was The Original Baasit In The Bay City Rollers,Alan Longmuir, Geography,In which state is Appomattax?,Virginia,General,Name the cold south-west wind which blows from the Andes across the South American pampas to the Atlantic Ocean.,Pampero +General,The food of the Greek & Roman gods,Ambrosia,People & Places,Whose Real Name Is 'Paul Hewson' ,Bono ,General,What famous playwrite has his birthday on 23rd April?,William Shakespeare +General,The UIT govern what sport,International shooting union,Entertainment,How many strings does a harp have?,47,General,In Spain what is manchego,Sheep's cheese +General,What does the average person do approximately 15 times a day,Laugh,General,Who was the second U.S. president,John adams,General,Who wrote the poem It was the night before Christmas,Clement Moore +Music,Who Had A Hit With the Song Umbrella In 2007,Rihanna,General,Which film won the best visual effects Oscar in 1984,Indiana Jones Temple of Doom,General,What republic would a Finn reach by paddling due south from Helsinki,Estonia +General,Who was ben hur's rival in the great chariot race,Messala,General,In 1931 the British Empire became the what,Commonwealth,Sports & Leisure,Which Middleweight Boxer Is The Subject Of The Film 'Raging Bull''? ,Jake Le Motta  +General,Narita is the main airport of which city,Tokyo,General,How many dimples does a golf ball have,336,General,Alex Pierrepoint & His Assistant Royston Pickard Executed Who At Holloway Prison On 12th July 1955,Ruth Ellis +General,Service where is the largest circulation of sunday papers,Los angeles,General,Who was the greek goddess of spring,Persephone,General,Robert Alan Zimmerman real name of who,Bob Dylan +General,Tarom Airlines is the national carrier of which country,Romania,General,What is a group of this animal called: Dove,Dule, History & Holidays,"Which famous explorer visited Australia and New Zealand, then surveyed the Pacific coast of North America?",Captain George Vancouver +General,In what Australian state would you find Rockhampton,Queensland,General,This word can refer to either an Irish accent or an Irish shoe,Brogue,General,Generation X Toys: Building tool named after Civil War president,Lincoln logs +Music,What Is The 3 Stringed Russian Instrument Called With The Triangular Body,Balalaika,Science & Nature,Water containing carbon dioxide under pressure is called ____ _____.,Soda water,Technology & Video Games,What new female fighter is introduced in 'Virtua Fighter 4'? ,Vanessa Lewis + History & Holidays,Name Jacques Cousteau's research ship.,Calypso,General,Cushion for kneeling on in church,Hassock,General,What film was the last featuring mel blanc's voice,Jetsons +General,When did leif erikson set foot on north america,1000 ad,General,What is the boy scout motto?,Be prepared,General,What was Ghengis Khans first job,Goatherd +General,Which USA record producer played maracas Stones 1st album,Phil Spector,General,Art of growing dwarfed trees or shrubs,Bonsai,General,"In Magnum PI,what kind of car did Higgins drive?",An audi +Food & Drink,Name the dessert named after a Derbyshire town? ,Bakewell tart ,General,Who is known as the father of geometry,Euclid,General,In which month is the Munich beer festival held,October +Science & Nature,The Vicufia Is A Member Of Which Family Of Mammals ,Llama ,Science & Nature,Which species of Elephant has the largest ears ?,African,General,In which song did hall and oates tell you to watch out for that sharkey girl,Man eater +People & Places,Which King Reputedly Tried To Turn Back The Sea ? ,King Canute , Geography,Which Irish city is famous for its crystal?,Waterford,General,Sherrinford was hero Ormond Sacker assistant names changed To,Holmes and Watson +Food & Drink,Who Was The First Man To Bring Cocoa Beans To Europe ,Juan De Cardenas ,Sports & Leisure,Who Missed The Penalty That Put England Out Of The 1996 European Championships? ,Gareth Southgate ,General,Where would you find a coffin joint,Horses foot +General,What was given on the fourth day of Christmas,Calling birds,Sports & Leisure,Which Sport Combines Rifle Shooting & Cross Country Skiing ,The Biathlon ,General,For the development of a vaccine against which disease is Jonas Edward Salk best remembered,Poliomyelitis (polio) +General,Who wrote the opera Tosca,Puccini,General,What European country's most common last name is Martin,France's frances france,General,Slow and steady wins the ___.?,Race +General,What is the Hungarian word for pepper,Paprika,General,In the film Tommy who played The Acid Queen,Tina Turner,General,Where were Tommy Lee Jones and Al Gore freshman roommates?,Harvard University +General,What's Mulder's nickname?,SPOOKY,Geography,What continent is the home to the greatest number of countries,Africa,General,The most abundant metal in the earths crust is what,Aluminum +General,What is 32 decimal expressed in hex,20,Sports & Leisure,Who became the first black manager of a premiership club when he took over in 1996? ,Ruud Hullit ,General,Dawson City was replaced by Whitehorse as the capital where,Yukon + History & Holidays,Who Released The 70's Album Entitled Sheet Music ,10cc , Geography,What city has the world's largest black population?,New York City,General,Virginitiphobia is the fear of ______,Rape +Music,Whose Tribute To Soul Singer Geno Washington Reached No.1 In 1980,Dexy's Midnight Runners,General,What is vanilla ice's real name,Robert van winkle,Music,"On the Single “ Do They Know It's Christmas ” Only One Person Performed On Both The Original And The 20 Year Remake Band Aid 20, Who Was It?",Bono / U2 +General,Where does the univerity of maryland's football team play its home games,Byrd stadium, History & Holidays,Where Were The McDonalds Massacred By The Campbells And The English In 1692? ,Glencoe ,Geography,In which country is Brest (NOT Breast;_),France +Music,Gene Vincent Was Injured In A 1960 Car Crash But Who Was Killed In That Very Same Crash,Eddie Cochran,General,What element is present in all organic compounds,Carbon, History & Holidays,Who Released The 70's Album Entitled Toys in the Attic ,Aerosmith  +Food & Drink,Which Country Drinks The Most Beer Per Annum ,Germany ,General,Einstein never wore what if he could avoid it,Socks,General,"Which musical features the song ""Sit Down, You're Rocking the Boat""",Guys and dolls +General,Which nation won the most medals in the 1994 Winter Olympics,Norway, History & Holidays,What Did New Zealand Vote To Abolish In 1961 ,The Death Penalty ,General,Who starred as Daisy in The Great Gatsby,Mia Farrow +General,Santiago is the capitol of Chile what does it mean,Saint Iago - or Saint James,Science & Nature, A racehorse averages a weight loss of between 15 and 25 pounds during a __________,Race,General,When did England's lease on Hong Kong expire,1997 +General,On a poll 50% men said sex in bed favourite 20% fem what fem,Reading 23%, History & Holidays,Which Sport Is Governed By The Queensbury Rules ,Boxing ,General,Vladivostok stands on what body of water,Sea of Japan +Toys & Games,How many numbers are on the spinner in the game of 'Life'?,Ten,Toys & Games,What game or sport is Bobby Fischer identified with,Chess,Music,"Gilbert & Sullivan, Which One Wrote The Music",Sir Arthur Sullivan +Entertainment,Name the dog in the Yankee Doodle cartoons.,Chopper,General,What is the Capital of: Monaco,Monaco,General,What is mainly extracted from pitchblende,Uranium +General,What is the common name for the larynx,Voice box,Food & Drink,From which fish do we get caviar? ,Sturgeon ,General,"Karl Landsteiner was awarded the Nobel Prize for his findings in the field of haematology, what was his discovery",Human blood groups + History & Holidays,"""Which Is The Only Football Team In The 2006/2007 Premiership Which Doesn't Contain Any Of The Letters Of """"Santa"""" In It's Name?"" ",Liverpool ,General,Antoine Domino is better known the world by what name,Fats Domino,General,"What links Edegra, Cavetra and Erix",Brand-names for Viagra +General,Which cowboys middle names were Berry Stapp,Wyatt Earp,Science & Nature,Approximately how many years old are oak trees before they produce acorns?,Fifty,General,Where would you find a Taoiseach,Eire - Head of State +General,Iatrophobia is the fear of,Going to the doctor,General,"A young what can be called a Boyet, Eyas or Nyas",Hawk,General,How many points is the green ball worth in snooker,Three +General,Who played Adenoid Hynkel in the film The Great Dictator,Chalie chaplin,General,Why was Mary Mallen locked up from 1915 to 1938,Typhoid Mary,General,"Often Found On A Computer Keyboard What Symbol Is Known As ""The Octothorpe""",Hash / Gate +General,What was Edison’s first practical invention,Tick a Tape for stockmarket,General,In Which City In The Us Did The St Valentines Day Massacre Occur ,Chicago ,General,What is Britain's most commonly planted tree,Sitka Spruce + History & Holidays,What changed in 1752 which caused Britain to have a White Christmas less frequently thereafter? ,The calendar ,Geography,"Which country developed ""Tae_Kwan_Do""",Korea,General,Who wore clothes made of camel's hair,John the Baptist +General,What happened to Charles Brooks in 1982,First legal death by lethal Injection,General,In which city did the 1998 Tour de France cycle race begin,Dublin,General,Year in which the Battle of Balaklava took place,1854 +Geography,Which 2 Countries Have Coastlines On The Bay Of Biscay ,France & Spain , Language,What city's name is derived from the words 'dubh linn'?,Dublin,General,Which 1993 Disney film starred Bet Middler as a witch,Hocus Pocus +General,What animal eats rests and sleeps on its back,Sea Otter,General,What is the seventh day of the week,Saturday,Science & Nature,What is a marsupium?,Pouch +General,How many moons does Jupiter have,Sixteen,People & Places,Which Author Had A 'Fatwa' Issued Against Him ,Salman Rushdie ,Toys & Games,What are a cheesboard's vertical rows called?,Files + Geography,What is the capital of Ohio?,Columbus,General,In what florida city was the ibm pc conceived,Boca raton,Sports & Leisure,Which Darts World Champion Is Nicknamed The Viking? ,Andy Fordham  +General,"Desire what mouseketeer's first hit song was ""tall paul""",Anette funicello,General,What 1960 movie inspired the rash of beach party movies,Where the boys are,Sports & Leisure,Two under par on a hole of golf is called a(n) ________.,Eagle +Sports & Leisure,"In Baseball, What Name Is Given To The Completion Of A Circuit Of Bases On One Hit ",A Home Run ,General,Where is the modern site of the biblical land of Goshen,Egypt the nile delta,General,"Which African Countries Flag Contains A Machete, A Cog And A Star",Angola +Music,What Was Supertramps First Uk Hit,Dreamer,General,What device changes the voltage of alternating currents,Transformer,Music,Come On Was The Stones First Single But Who Wrote And Recorded The Song,Chuck Berry +General,What is the term for a castrated rooster?,Capon,General,We've used Xerox but from Greek what does it literally mean,Dry,General,PY are the international car registration letters of which country,Paraguay +General,Sir Henry Cole got John Callcot Horsley design ? to save time,Christmas card 1843,General,Sylvester Stallone film shares name with Paris art movement,Cobra,General,In which book of the New Testament is Armageddon named as the gathering place of the Day of Judgement,Revelations +General,What were the two birds that noah sent out from the ark,Raven and dove,Science & Nature,What Are Insects Chewing Jaws Called ,Mandibles ,General,In France it is illegal to sell what doll,ET - Dolls human faces only +General,Who was the Greek cup-bearer to the gods,Ganymede,Science & Nature, The blue whale is maintained by its blubber and can go up to half a year without __________,Eating, Geography,What is the capital of Kyrgyzstan ?,Bishkek +General,Who played the mayor of the munchkins in 'the wizard of oz',Charlie becker,General,What bridge links a Palace with a State Prison,Bridge of Sighs – Venice,General,What did epicurus found,Epicurean philosophy +General,Which Was The First Film Remake To Win The Title Of Best Picture Oscar?,Ben Hur,General,"To what group of elements do cerium, praesiodymium and promethium belong",Rare earths,General,System of compulsory enrollment of men and women into the armed forces,Conscription +General,What reptilian feature evolved in feathers,Scales,General,Which jazz trumpet virtuoso is credited as having invented scat' singing,Louis Armstrong,Science & Nature,"In which organ is a clear watery solution known as the ""aqueous humor"" found?",Eye +General,"What did Wilde describe as ""A man who knows the price of everything and the value of nothing""",Cynic,Sports & Leisure,With what sport is Jack Nicklaus associated?,Golf,General,"A sauce described as 'a la soubise', primarily contains what vegetable?",Onions +General,A nuclear reactor was built beneath a Chicago football stadium in which year,1942,Art & Literature,Who wrote '1984'?,George Orwell,General,Who played the lead Male & Female roles in the movie 'Romancing The Stone' (Point For Each) ,Michael Douglas And Kathleen Turner  +Music,Who Sang The Theme To The Bond Movie Diamonds Are Forever,Shirley Bassey,General,In which game could you 'plink with your plonker' and 'squopp' your opponent,Tiddleywinks,General,How many suicides are recorded in the bible,Seven +General,Which of Charles Dickens' novels is mainly set in the Marshalsea Prison,Little dorritt,General,Who study & predict earthquakes,Seismologists,General,What is Rice Paper made from,A Tree - The Rice Paper Tree pith +General,What is the flower that stands for: simplicity,American sweet-brier,General,Who wrote The Young Persons Guide to the Orchestra,Benjamin Britain,General,What's the national sport of Japan,Sumo wrestling +Music,"To Where Did Steely Dan Bid ""Toodle-oo""",East St Louis,General,What Scientific breakthrough was created by a group of Edinburgh scientists in 1996,The Cloning Of Dolly The Sheep,General,What was elvis' mother's name,Grace +Food & Drink,Which beer sponsors a pre-Wimbledon Tennis Tournament ,Stella Artois ,Sports & Leisure,How Many Pieces Per Player Are There In A Game Of Backgammon ,15 Pieces ,General,What has become a custom from armored knights raising their visors to identify themselves when they rode past their king,Salute +Food & Drink,What country is the largest consumer of beer per head? ,Germany ,General,"A Sevillian gypsy dance, possibly originating in India, also with Moorish and Arabian influences, originally accompanied by songs and clapping and later by the guitar, and characterized by its heelwork.",Flamenco,Sports & Leisure,Baseball: The Boston ______?,Red Sox +General,JVC launched VHS in 1976 what does VHS stand for,Video Home System,Music,With Which Band Was Julian Cope The Front Man,The Teardop Explodes,General,An Enologist studies what,Wine +General,What Country Is The Only Country To Host The Summer Olympic Games & Not Win A Single Medal,Canada,General,Dressed man what song spawned a lawsuit for tommy tutone,867-5309/jenny,General,What woman has the most statues of her,Joan of Arc 40000 +Art & Literature,What Was Shakespeare's First Play ,Henry VI , History & Holidays,What do the Irish call a spirit whose wailing signals imminent death ,Banshee ,General,Dogs & humans are the only animals with what,Prostrates +General,"Who wrote 'i, claudius'",Robert graves,Science & Nature,In Computing What Do The Initials WWW Stand for ,World Wide Web ,General,Who was the second woman in space,Savitskaya +General,"What group of minerals are beryl, emerald and aquamarine a part",Beryllium,General,Someone able to use both hands equally well,Ambidextrous,General,What film did Rock Hudson and Doris Day star in together in 1959,Pillow talk +General,With what were scrabble tiles first made,Pocket knife,General,Tracy Marrow a former convict born 1958 changed name to what,Ice-T,General,Which U.S. city was bombed by terrorists in 1995,Oklahoma city +General,Who wrote The Swiss Family Robinson,J r wyss,General,A strong twilled cloth often used for raincoats,Gaberdine,Food & Drink,What is the UK's best selling chocolate bar? ,Kit Kat  +General,What is the name given to Indian food cooked over charcoal in a clay oven,Tandoori,General,What 38 years old heavyweight couldn't answer the 11th round bell in 1980,Muhammad ali,General,Who was the first original 'saturday night live' cast member to leave,Chevy +General,What country has a Bible on its flag,Dominican Republic,General,A phalophilliac has a fetish about what,Large Penises,Music,"""Rattle And Hum"" Was A Hit Album For Which Group",U2 +General,In the song who told Laura he loved her in 1960,Ricky Valance,General,"Conrad classical music: who composed ""rhapsody in blue""",George gershwin,General,What Country Won The First Ever Eurovision Song Contest,Switzerland +General,Who was offered the presidency of Israel in 1952 (turned down),Albert Einstein,Science & Nature,What is the royal disease?,Haemophilia,General,Who wrote 'Sexual Behavior In The Human Male' in 1948?,Alfred Kinsey + History & Holidays,The Song 'White Christmas ' Was' Performed In Which 1942 Film ,Holiday In ,General,"The left lung is smaller than the right lung, to make room for which organ",Heart,Geography,In Which Country Are The Appenine Mountains ,Italy  +Music,Which No.1 Hit Was Cliff Richards First Million Seller,Living Doll,Music,Which Member Of Guns N Roses Was Born In Stoke England,Slash,General,"Who said ""People only see what they are prepared to see""",Ralph W Emmerson +General,Who hired The Jackal,Oas, History & Holidays,"""What 'NN' Is """"ABCDEFGHIJKMNOPQRSTUVWXYZ & ABCDEFGHIJKMNOPQRSTUVWXYZ"""""" ",Noel Noel ,General,What speed record has remained unbroken since 1938,Steam Train - Mallard +General,What did joseph smith found,Mormonism,General,The earth pig is what animal,Aardvark,General,"What is Cuba's national sport, at which Castro himself was proficient",Baseball +General,Sherlock Holmes paid 55 shillings for what,His Stradivarius violin,General,How may Oscars did Richard Burton win,None – Seven Nomination, History & Holidays,"Who founded the Salvation Army in London, 1865? ",William Booth  +General,Who was the first actor to appear on cover of Time magazine,Charlie Chaplin,General,Where would you find Volans,Southern Sky – Const Flying Fish,Music,"""Mr Mojo Risin"" Is An Anagram Of Which American Rock Singer",Jim Morrison +General,"What kind of gun does the movie's ""dirty harry"" pack",Magnum,General,What is a group of woodcocks,Fall,Music,Stanley Burrell Is The Real Name Of Which Popular 90's Singer,MC Hammer +General,What make of car is a 'thunderbird',Ford,General,Which famous sporting venue is above NewYorks Pennsylvania Station,Madison square gardens,General,What organ endured its first us transplant in 1954,Kidney + History & Holidays,Israel occupied the West Bank. It belonged to _______.,Jordan,General,Ophthalmophobia is the fear of,Being stared at,General,"It's Metallica now,but what band's name originally appeared on Beavis's T-shirt?",Slayer +General,What is the study of elections called,Psephology, History & Holidays,Who introduced bagpipes to the British Isles?,Romans,General,Who played Dr McCoy in the original Star Trek series,Deforest Kelly +Science & Nature," Off the coast of southern California, around 200 __________ still roam in Catalina Island's hinterlands, descendants of a few brought there in the 1920s for a movie and left there.",Bison,Religion & Mythology,What animal's meat can a Hindu not eat?,Cow,General,Who resigned as Chancellor of the Exchequer in 1947 over a Budget leak,Hugh dalton +General,Aromatic plant used for seasoning and salads,Coriander, Geography,What is the capital of Germany ?,Berlin,General,Ignoring Queen name the only woman to appear on UK currency,Florence Nightingale +Music,Which Country Does The Singer Enya Come From,Ireland,General,What is a group of mules,Span,General,Name UK General who defeated Montcalm on Plains Abraham,James Wolfe +Music,Which Area Of New York City Was Immortalised In The Dance Song By Bob & Earl,Harlem (Harlem Shuffle),General,"Of Which Country Is ""Vilnius"" The Capital City",Lithuania,General,The musical instrument piccolo means what in Italian,Small +General,What fruit produces a herb,Banana,General,Which city in Lombardy is the second largest city in Italy,Milan,General,On what show was 'run d.m.c' the first rap group,American bandstand + History & Holidays,The following is a line from which 1970's film 'It's pronounced Fronkensteen' ? ,Young Frankenstein ,General,What is the Capital of: Greece,Athens,Music,In 1992 A Cover Of The The From M*A*S*H (Suicide Is Painless) Gave Which Band Their First Top Ten Hit,Manic Street Preachers +General,What country has the longest chairlift in europe,Switzerland,Geography,Of Which Country Is Katmandu The Capital ,Nepal ,General,What Was The Name Of The Person Who Won The UK's First Ever Big Brother Contest,Craig Phillips +Science & Nature,What is a male deer called?,Buck,General,What plant did St. Patrick use to explain the Trinity,Shamrock,General,The Hardy boys & ______,Nancy drew +General,What is the official title of the ambassador of the Pope,Nuncio,General,If you climbed the Dolomites what country are you in,Italy, Geography,What is the basic unit of currency for Oman ?,Rial +General,"Cancha, Cesta, Cinta terms in which sport",Jai Alai - Court Glove Tape,Sports & Leisure,How many times did Red Rum win the Irish Grand National? ,None ,General,What is a group of plovers in flight,Wing +General,As what was John F. Kennedy airport formerly known,Idlewild,General,In mythology who slew the nine headed hydra,Hercules,Entertainment,Which film preceded 'Magnum Force' and 'The Enforcer'?,Dirty Harry +General,In which Puccini opera of 1896 is the Christmas Duet,La boheme, History & Holidays,In What Year Was The Berlin Wall Erected ,1916 ,General,Felinophobia is a fear of ______,Cats +General,Relating to food what is porcini a type of,Mushroom,General,What bird lays the largest egg,Ostrich,General,What is the most common name for US cities 66 of them,Fairview +Science & Nature," At seven inches long, the Wilson's storm petrel is the smallest bird to breed on the __________",Antarctic continent,General,The Mason-Dixon line separates Pennsylvania and what state,Maryland,General,What do Australians call dust storms,Willy-willies +General,The island of Krakatoa was almost completely destroyed by a volcanic eruption in which year,1883,General,Which garden plant has varieties called Nelly Moser and Hagley Hybrid,Clematis,General,What shape is cansonsei pasta,Little Britches +General,What was the name of Eddie Murphy's character in Beverly Hills Cop?,Axel Foley,General,What is the name of the winged horse in greek mythology,Pegasus,General,Which American state has the motto Esto Perpetua - its forever,Idaho (Gem State) +General,On which city did Charles Darrow base his original game of Monopoly,Atlantic city,General,In Kansas what can a waiter not do in a teacup (legally),Serve wine,General,What does a butterfly clip usually hold in position,Hair +General,Where is King Arthur supposed to be resting,Avalon, History & Holidays,In Which Year Did The Oil Tanker Torrey Canyon Come Aground Near Lands End Causing A Major Environmental Disaster ,1967 ,Music,Raphael Ravenscroft Plays Which Musical Instrument,Saxophone +Food & Drink,What vegetable did Mark Twain describe as 'Cabbage with a college education'? ,Cauliflower ,General,What would you do with a Romeo's Rouser,Drink it - its a real ale,Geography,What is the capital of Brunei,Bandar seri +General,For making what is the abalone shell used,Jewellery,General,How old are oak trees before they produce acorns,Fifty,People & Places,Who was famously convicted of horse stealing at York Assizes in 1739 and subsequently hanged? ,Dick Turpin  +Geography,What is the capital of Qatar? ,Doha ,Music,"Easter, Easter, Easter, Easter Which Group Am I",Echo And The Bunnymen,General,Which drink was advertised as 'The Wodka from Varrington',Vodka +General,"In Germany who were known as ""Dick und Doof""",Laurel and Hardy,General,In Italy a man can be arrested if found wearing what,A Skirt,General,Which country was known as 'the Cockpit of Europe',Belgium +General,What cartoon character had no feathers until the censors decided he 'looked naked',Tweety,General,Which is the tallest breed of dog,Irish wolfhound,Entertainment,Where is the Rock and Roll Hall Of Fame?,"Cleveland, Ohio" +General,What year did Laurel and Hardy first perform together,1926,General,The average person does it thirteen times a day - what,Laughs,General,What is located on Boothia's peninsula in Canada,North Magnetic Pole +General,What product only sold 1200 bottles in its first year,Liquid Paper - Tippex,General,What was the first x-rated animated cartoon film,Fritz the cat,General,What was the name of Jodie Foster's 1991 film that she directed,Little man tate +General,Troy McClure appears in which cartoon series,The Simpsons, History & Holidays,What energetic singer married the 13 year old daughter of his bass player? ,Jerry Lee Lewis ,Entertainment,What was the original name Charles Schultz had for Peanuts?,Li'l Folks +General,What French automobile company merged with American Motors,Renault,General,What X rated movie won an Oscar,Midnight Cowboy,General,If you were drinking Tiger beer in what country would you be,Singapore +General,"In Save by the Bell,what were Zack and Kelly dressed up as the night they broke up?",Romeo and Juliet,Music,Come As You Are Was A 1992 Hit For Whom,Nirvana,Geography,What Is An Archipelago ,A Group Of Islands  + Language,This animal is found at the beginning of an (English) encyclopedia,Aardvark,General,Ncaa: who were the finalists in the men's basketball championship in 1947,Holy cross & oklahoma,Food & Drink,In which country did the word 'biscuit' originate ,France  +General,"In egyptian mythology, who is known as the god of the desert",Ash,Music,There Is Just One Colour Of The Rainbow Never To Feature As Yet (16/09/08) In The Title Of A UK Top 40 Song What's The Colour,Indigo,General,Jason and the Argonauts sailed to where looking for the fleece,Colchis +General,U.S. Captials - Massachusetts,Boston,General,Who originally made the jack-o-lantern,Ancient celts,General,In which Australian State is Wagga Wagga,New south wales +General,"What infamous bank robber was nicknamed ""willie the actor""",Willie sutton,General,Which area of water separates India from Sri Lanka,Palk Strait,General,Of what is hippophobia a fear,Horses + History & Holidays,Which film first featured a character later named Pinhead ,Hellraiser ,General,The windhover is an alternative name for which bird,Kestrel,General,Which organisation is officially known as Holy Spirit Association,Unification Church – Moonies +General,Specifically to what does Episcopal refer,Bishops,Food & Drink,How Did Chocolate Get It's Name ,Spanish Imitation Of Aztech Word Tchocolatl ,General,In 1979 Ugandan dictator Idi Amin overthrown; Tanzania takes,Kampala +Music,"Which Beatles album is The Song ""Only A Nothern Song"" on?",Yellow Submarine,Music,Which Single Topped The Charts In 1961 For Frankie Vaughan,Tower Of Strength,General,What is Lieutenant Colombo's first name,Philip +Science & Nature,What is the gestation period of a cow*9 months ,Nine months ,General,What inventor blew up a telegraph station in a battery experiment,Thomas edison,General,Whose autobiography is called ' All of me',Barbara windsor +General,In which town or city in the north-west is the Ashton memorial,Lancaster, History & Holidays,"What did a shepherd boy discover at qumram, jordon in 1947 ",The dead sea scrolls ,General,Bacillophobia is a fear of ______,Microbes +General,Who drove a Rolls Royce with a number plate FAB 1,Parker in Thunderbirds,General,Who was shown on the most popular US postal stamp of 1998,Sylvester and Tweety,General,"Who played victoria barkley on the tv show, ""the big valley""",Barbara +General,Numbers What's the capital of Wyoming,Cheyenne,General,What was the political constituency of Harold Wilson?,Huyton,General,Who was the future political pair to star in Hellcats of the Navy,Ronald +General,"Which group sang the song ""Learn To Fly""?",Foo Fighters,Geography,Where in the world is Rock English spoken? ,Gibraltar ,General,The science of preparing & dispensing drugs is ________,Pharmacy +General,What is an enclosed car hung from a cable at a ski slope,Gondola,General,What is it illegal to pawn in New York,American flag,General,"Which pop group's only U.K. chart entry, ""Sugar Sugar"" got to No. 1 in 1969",The archies +General,To what is the harvest moon nearest,Autumnal equinox,General,If You Were An Aronophobe Or Had Aronophobia What Would You Be Afraid Of,The Internet,Music,Who Took A Drive To The Ocean With Their Kin to See The Ship Safely In,Lighthouse Family + History & Holidays,This organization was founded by William Booth.,Salvation army,Entertainment,"This band's highly original video for ""Whip it,"" characterized by red flower pot hats was criticized for being both sado-masochistic and racist.",Devo,General,Who is the Patron Saint of brewers,Saint Nicholas +People & Places,Who was the Director of the C.I.A during 1976-77 ? ,George Bush ,General,Operation Chastise during WW2 better known as what,Dambusters Raid,Food & Drink,Australian city home of Castlemaine XXXX Export Lager,Brisbane + History & Holidays,"Who wrote the book, that was the basis for the 1982 animated film, 'The Snowman'' ",Raymond Briggs ,General,What country calls itself Republika Shqiperise,Albania,General,Mechanophobia is a fear of ______,Machines +Music,According To The Beatles What color was Lucy's sky?,Marmalade,General,"At Mcdonalds in New Zealand, what kind of pies do they serve instead of cherry ones",Apricot,Music,Chantelle Recently Announced Her Engagement To Preston. But Of Which Group Is Preston A Member?,Ordinary Boys +General,What was the number of the squadron which flew the Dambusters mission in 1943,617,General,What is the flower that stands for: shame,Peony,Geography,"Before 1918, ___________ belonged to Austria_Hungary; from 1918 to 1991, it was part of Yugoslavia. It declared its independence on June 25, 1991.",Slovenia +General,Name the only bird that can swim but can't fly,Penguin,General,Which Game Takes Its Name From The Latin For “Bird Of Prey?,Subbuteo,General,"A small structure on top of a dome, tower or roof often open to admit light below.",Lantern +General,Which arteries carry blood to the head and neck,Carotid,Entertainment,What is Super Chicken's partners name,Fred,General,"ELVIS: What was the actual running time of the film ""Wild in the Country""",114 minutes +Geography,Which Country Has The Lowest Birthrate ,Italy ,General,For what does the letter 'S' stand in the initials ISBN,International standard book number,Entertainment,Who was Lauren Bacall's first husband?,Humphrey Bogart +General,Which Female Act (Non Solo) Holds The Record For The Most Consecutive Weeks At The Top Of The Uk Singles Chart,Shakespeares Sister,General,The name of which countries capital means good air,Argentina – Buenos Aires,Sports & Leisure,How Old Was Steve Davis When He Won His First Snooker World Title ,23  +General,Which non alcoholic cordial is made from pomegranates,Grenadine,General,In Japanese cooking what name is given to small pieces of raw fish served with cold rice,Sushi,General,Hitchcock film he did not appear Lifeboat 1944 who female star,Tallulah Bankhead +Science & Nature,The tides on the earth's oceans are actually created by gravitational pull from the ____,Moon,General,What was the 'unfinished symphony' meant to be played for,A requiem,Sports & Leisure,How Many Balls Are On The Table At The Start Of A Game Of Snooker ,22 Including Cue Ball  +General,What stretch of water separates Italy and Sicily,Straights of Messina,General,What is the registry number of the enterprise in the original Star Trek?,NCC 1701,General,Which metal gets its name from Swedish for heavy stone,Tungsten +General,A sufferer from boanthropy believes he is what,An Ox,General,Siderodromophobia is the fear of,Trains,Music,"Name The Instrument Most Readily Associated With Gary Burton, Milt Jackson, & Lionel Hampton",Vibraphone +Music,Who Had A 1982 Hit With Heartbreaker,Dionne Warwick,General,Marmolada nearly 11000 feet highest peak what mountain range,Dolomites,General,What is a group of this animal called: Woodcock,Fall +General,Film title' 'fahrenheit ______',451, Geography,What is the capital of Uganda ?,Kampala,Food & Drink,Which Mixer Was Used To Take Quinine ,Tonic Water  +General,What city hosted the 1976 summer olympics,Montreal,General,What kind of spider devours it's mate after mating,Black widow,Science & Nature,Which has longer wavelengths X-Rays or Microwaves? ,Microwaves  +Music,"Which American Vocal Group Scored Their First UK Top Ten Hit In 1996 With ""We Got It Going On""",Backstreet Boys,General,In which opera is the Anvil Chorus,Il trovatore,Science & Nature," In England, the most commonly used guide dog for the blind is the Yellow __________",Labrador retriever +Food & Drink,How Many Gallons In A Firkin ,9 Gallons ,General,In the Beverley Hillbillies what was a potpasser,Pool Cue at fancy eating table,General,"By What Name Was Singer “ Charles Weedon Westover ” Better Known, Who Commited Suicide In 1990 By Shooting Himself?",Del Shannon +Music,According To The Beatles What Did “The Girl That's Driving Me Mad” Have,A Ticket To Ride,General,Term applied to the ripened ovule of a seed plant before germination,Seed,General,What was the third leading cause of death in 1900,Diarrhoea +General,Who won the 1959 u.s grand prix,Bruce mclaren,Science & Nature,This cluster of stars is also known as the Seven Sisters.,Pleiades,General,According to a survey what is the US top family food,Spaghetti – then Chicken +General,What is the only part of the human body that cannot repair itself,Teeth,General,What word describes the inverse indentation often found on the bottom of a wine bottle?,Punt,General,Amnesiac and The Bends albums of which UK group,Radiohead +General,"Who wrote the novel ""To Kill a Mockingbird""",Harper lee,Music,Which Australian Had A Number One Hit In 1969 And Received A CBE In 2006?,Rolf Harris,General,What was the name of the Addams family's giant man-eating plant,Cleopatra + History & Holidays,Who gave john f. kennedy a dog named pushinka ,Nikita khrushchev ,Music,Let There Be ________ Was A Hit For Sandy Nelson,Drums,General,What elements name comes from the Greek word for violet,Iodine +Food & Drink,What is compote? ,Fruit preserved or stewed in syrup,Science & Nature,What is made by a Bessemer converter? ,Steel ,Music,Name The Production Genius Behind Philles Records,Phil Spector +General,The longest recorded one lasted 51.5 minutes - what,Tennis rally1029 strokes,General,"What is the name of rock group chicago's boulder, colorado recording studio",Caribou ranch,General,"John Young, Gemini 3, 1968 first to do what in space",Eat a hamburger +General,"Who's the voice of ""this week in baseball""",Mel allen,Music,What pop stars first and middle names are Katherine Dawn?,K D Lang,General,The video for which eighties song features nothing but 5 cheerleaders?(Name the artist too),Mickey Toni Basil +Science & Nature,What Is The Function Of White Blood Cells ,To Combat Disease And Infection ,General,"In The Year 2000 ITV Revived The TV Soap ""Crossroads"" But What Long Running TV Show Did It Replace",Home & Away (Moved To Ch 5),Science & Nature,"Which Famous Aircraft Of World War 2, Had A Frame Of Wood & A Skin Of Plywood & Was Glued & Screwed Together In England Canada & Australia? ",De Havilland Mosquito  +General,"On Who's Tombstone Will You Find The Words ""She Did It The Hard Way""",Bette Davis,General,Who was THE woman to Sherlock Holmes,Irene Adler,General,"What place is nicknamed ""The City of Lilies""",Florence +General,"In The World Of Sport What First Did ""Uriah D Rennie"" Achieve In 2001",First Black Ref To Dictate A Game,General,The planet pluto was discovered in what year,1930,General,What kind of charcters were MTV's Sifl and Olly?,Sock puppets +Geography,What colour is the leftmost stripe on the French flag? ,Blue ,People & Places,Who Broke Into The Queens Bedroom For A Chat ,Micheal Fagan ,General,What president's wife saw him elected but died before his inauguration,Andrew jackson +General,Who sang 'born in the u.s.a',Bruce springsteen,General,In the US money is still top marital argument what is second,What to watch on TV,General,Slugs and snails belong to which class of molluscs,Gastropods +General,What is the flower that stands for: boaster,Hydrangea,General,What is the sum of 236 - 145,91,General,Which planet with at least eighteen known moons is sixth closest to the Sun,Saturn +General,Who had hiccups for 69 years,Charles osborne,General,Rubies are a red variety of corundum. What name is given to corundum gemstones of any other colour,Sapphires,Science & Nature,When light waves pass from one medium into another they change direction. This is called _________.,Refraction +General,What first appeared in New York World 21st December 1913,Crossword,General,The ashes of the average cremated person weighs how many pounds,Nine,General,24% of British men have no what,Real teeth +Entertainment,Before Olive Oil met Popeye she was engaged to someone. Who was he?,Ham Gravy,General,"Fettucini, linguini and tagliatelle are all types of what",Pasta,General,Shagaganda sounds like molesting a goose - but where is it,Peru +General,"Who said ""No sex is better than bad sex""",Germaine Greer,Entertainment,Who is the main character in 'Touched By An Angel'?,Monica,General,"The School ""Northbridge High"" Featured In Which Popular Early 90's TV Show",Press Gang +General,After which creature is the Californian island of Alcatraz named,Pelican,General,Wrestling who was the girl in peter sellers' soup,Goldie hawn,General,This animal can be found in sub Saharan Africa & Gir National Forest in India.,Lion +General,"What is ""Magic"" Johnson's first name",Earvin,General,What is the most common name in italy,Mario rossi,General,Which statue is missing its arms,Venus de milo +General,Where did the birkenhead sink,Danger point,General,Southpaw to us means a boxer who leads with his right hand; but in which sport did the term originate,Baseball,General,"Who Played The Role Of Merlin In The Tom Cruise Classic ""Top Gun""",Tim Robbins +General,In 1907 who was the first English writer win Nobel prize literature,Rudyard Kipling,General,In Connecticut by law restaurants must provide separate what,Nose blowing – not blowing tables,General,What is the fear of the sea known as,Thalassophobia +General,Who sang the theme song in From Russia with Love,Matt Munroe,General,What type of animal is an 'agama',Lizard,Sports & Leisure,What Is The Name Of Chicago's American Football Team? ,Bears  +People & Places,Who Was Famously Quoted As Saying 'The Only Thing She Wears In Bed Is Channel No.5' ,Marilyn Monroe ,Science & Nature,For What Illness Did Louis Pasteur Develop A Cure ,Rabies ,General,What two astrological signs begin with the letter a,Aquarius & aries +Sports & Leisure,California Dolls Is A Movie That Was Inspired By And Features Which Sport ,Wrestling ,General,"The sinking of the 'Herald of Free Enterprise' was one of Europe's worst shipping disasters, from which port did the ship sail",Zeebrugge,General,What is Cumberland sauce usually served with,Game +General,In China what colour does the bride traditionally wear,Red,General,What is the Capital of: Guyana,Georgetown,General,What is the Capital of: Madagascar,Antananarivo +General,Computers - mhz stands for________,Megahertz,General,"Which of the stars of ""outrageous fortune"" also starred in tv's ""cheers""",Shelley long,General,A spat is a baby what,Oyster +Science & Nature,What is the heaviest snake?,Anaconda,General,In what winter sport does one lie on ones back,Luge,Sports & Leisure,"Who Accompanied Steve Redgrave In The Last Three Of His Five Olympic Gold Medals Winning Rows, And Went On To Win A Fourth Gold Himself In 2004? ",Matthew Pinsent  +Tech & Video Games,Who is the Linux operating system named after ?,Linus Torvalds,General,Which Scottish Quarter day is on August 1st,Lammas,General,Who wrote The Last Picture Show,Larry McMurty +General,Traditionally in what months should you not eat oysters,May to august,General,Who invented the pneumatic tyre from a section of garden hose,John dunlop,General,The Fool in French and the Runner in German what in English,Chess Bishop - Fou – Laufer +General,What is the capital of the seychelles,Victoria,People & Places,Which US University is in the city of Cambridge? ,Harvard ,Music,Amount for which the Beatles sued Nike for using the song ''Revolution'' in a 1987 commercial,$15 million (terms of the settlement are confidential) +General,On what book was 'three days of the condor' based,Six days of the condor,General,In Rebel Without a Cause name the High School,Dawson,General,"Which rock group of the eighties gave away a silver keychain at every concert, which was supposed to bring good luck to whoever caught it?",ZZ Top +General,What is the fear of meteors known as,Meteorophobia,General,What does the word chicane mean in the context of a game of bridge,A hand without any trumps,General,Brazaville Is The Capital Of Which African Country,The Congo +General,Which two vegetables are used in vichyssoise,Leek Potato,Music,Who Released An Album Entitled Zenyatta Mondatta,The Police,General,In which city did Zlata Filipovic write a diary in the 1990's which began just before her 11th birthday,Sarajevo +Food & Drink,Beef dish named after a 19th century Russian diplomat ,Stroganoff , Geography,What is the capital of Saudi Arabia?,Riyadh,General,If a doctor said you had claudication what have you got,A limp or lameness +General,Frank Zappa was lead singer with which band,Mothers of invention,General,When was the dynamic loudspeaker invented,1924,Science & Nature,What was given to children to rid them of threadworm?,Salt enema +General,Coronation Street's Kabin is located in which street,Ross street,Sports & Leisure,Hockey: The Toronto ______?,Maple Leafs,General,By what name is Maurice Micklewhite better known,Michael caine +Food & Drink,What Does 'Vol-au-vent' Actually Mean In French ,Vol-Au-Vent Is French For 'Flying On The Wind' They Are Puff Pastry Cases ,Religion & Mythology,On which mountain did Moses receive the Ten Commandments,Sinai,General,Frigophobia fear of what,Being Cold + Language,What is the first letter in the Greek alphabet?,Alpha,General,Who are the largest candy consumers per capita,Danes then Irish,General,"What's the international radio code word for the letter ""F""",Foxtrot +Food & Drink,Which spirit is used to fortify red wine in creating port? ,Brandy ,General,"Physiological systems that enable organisms to live in harmony with the rhythms of nature, such as the cycles of day & night & of the seasons",Biological clocks,Art & Literature,Which Painter Is Noted For His Works Depicting The Moulin Rouge ,Toulouse- Lautrec  +General,Who wrote 'a tale of two cities',Charles dickens,General,Which cathedral has 4440 statues,Milan,General,"What do the skunk, magpie and otter have in common they are all",Black and white +Music,Which Saxophonist Was Known As Bird,Charlie Parker,General,Which museum now occupies the site of the old Bedlam Hospital in London,Imperial war museum,General,What is the national airline of Indonesia?,Garuda +Science & Nature,What Is The Fluid Which Surrounds A Foetus Known As ,Aminiotic Fluid ,General,Who Was Sentenced To Life Imprisonment On 14 th June 1964 ?,Nelson Mandella,General,"The words concert, opera and carnival have their roots in which language",Italian +People & Places,Who Was Known As The Old Groaner ,Bing Crosby ,General,On a German white wine what does the word 'Spatlese' (pronounced 'shpate-laser') signify,Late picked,Geography,"There are sand dunes in Arcachon, ___________ that are 350 feet high.",France +General,Zephyr' is the poetic name for what,West wind,Entertainment,Michael di Lorenzo was one of the lead dancers on which Michael Jackson video?,Beat It, History & Holidays,How many presidents of the United States fought in the Civil War?,Six +General,What is the wingspan of a condor,Nine feet,General,Which bird is known as the laughing jackass,The kookaburra,General,"Where was the painting by Michaelangelo , valued at £8 million pounds recently found, after remaining unrecognised for more than 250 years",Castle howard +General,"What is a ""hiatus""",Brief period of rest,General,The Saffir-Simpson scale measures the intensity of what,Hurricanes,General,In which country was film star Ray Milland born,Wales +General,Who created the character Parker Pyne,Agatha Christie,Food & Drink,"What type of food is associated with 4th July celebrations, is it a curry, a Roast Turkey, a picnic, or a BBQ? ",Picnic or BBQ ,Science & Nature,Which Are The Three Species Of Animal With Stereoscopic Vision? ,"Monkeys, Apes And Man " +General,What NFL team was formerly known as the Portsmouth Spartans,Detroit,General,Which religion's holiest shrine is the Golden Temple at Amritsar,Sikh,Sports & Leisure,What Name Is Given To The Electronic Eye Used At Wimbledon Tennis Tournaments ,Cyclops  +General,What 80's cartoon theme song did Ricky Martin sing?,Rubic-the Amazing Cube,General,Octophobia is the fear of,The figure 8,General,Chase and Sandborn first sold what in tins in the US,Coffee +General,Butterfly Falcon Fun Glen Lark Penguin If Topper types of what,Yacht Dingy Classes,General,What is the most commonly prosecuted illegal act,Speeding,General,How much wood can a wood chuck chuck if a woodchuck could chuck wood,All the +General,What were wilma flintstone and betty rubble's maiden names,Slaghoople and,General,Who is the author of I'll Be Seeing You released in 1993,Mary higgins clark, History & Holidays,Who Released The 70's Album Entitled Stardust ,Willie Nelson  +General,In Which Country Was Tennis Legend John McEnroe Born,Germany,Religion & Mythology,Who is the Norse Watchman of the Gods,Heimdall,Geography,"On what island is the U.S. naval base, Guantanamo",Cuba +General,A block of compressed coal dust used as fuel,Briquette,Music,What Is The Sound Producing Equipment Element Of Many Woodwind Instrument Called,The Reed,General,Dinner Time by Paul Terry was which cartoon first,Talkie +Sports & Leisure,Which Sport Do You Associate With The Commentator Ted Lowe? ,Snooker ,General,Which film star was the first to appear on a postage stamp,Grace Kelly,General,Which male name means God will judge,Daniel +General,Whats the better known name of Edson Arantes do Nascimento,Pele,Music,Swords Of A Thousand Men Is The Only song Many Will Remember Which Group By,Tenpole Tudor,General,What instrument was played by jazz musician Chet Baker,Trumpet +Entertainment,How many strings are there on a bass guitar?,Four,General,Where on the human skeleton would you find the ilium,Hip,General,85% of women do what,Wear wrong bra size +General,IBM was founded in what year,1896, Geography,What volcano showers ash on Sicily?,Etna,General,Lack of iron makes a person what,Anaemic +General,Hedera Helix is better known by what name,Ivy,General,Who murdered julius caesar,Marcus brutus, Geography,What is the capital of the country Georgia ?,Tbilisi +Sports & Leisure,How did Catherine McTavish make Wimbledon history in 1979 ,First Female Empire ,General,If you have polythelia what have you got,Three nipples,General,Who assassinated president kennedy,Lee harvey oswald +General,Ernest Breaux a chemist created which product in 1921,Channel No 5,General,In what European city was the first book in English published,Bruges - Belgium,General,What mythological animal was the insignia of UK Airborne WW II,Pegasus – carrying Beleraphon +General,In Norse mythology Thor's chariot is pulled by two what,Goats name Tanngrisni Tanngnost,General,Budapest is the capital of ______,Hungary,General,Who read casey at the bat for her tv debut on the ed sullivan show,Lauren +General,Which bird turns it head upside down to eat,Flamingo,General,"Who wrote and performed the song ""Poisoning Pigeons in the Park""",Tom lehrer,General,Star Wars - Darth ____,Mual Vader +General,Film - who was the star of the doctor,William hurt,Science & Nature,What is the world's largest insect?,Goliath beetle,General,The song Mack the Knife comes from what stage show,Threepenny Opera +Music,Reaching No 4 In 1994 Give The Full Title Of Cyndi Laupers Re- Released 80's Classic,Hey Now (Girls Just Want To Have Fun),General,"What actress said ""I dress for women - Undress for men""",Angie Dickinson,General,What nationality was Morse inventor of the famous code,American +Food & Drink,The Subterranean Fungus Highly Prized In Cooking Is Known As What ,Truffle ,General,What was al capone finally arrested for,Tax evasion,General,What is Brussels best known statue,The Mannequin Pis +General,Church bells of Maralnello Ring Sundays Public Hols and when,Ferrari team win,People & Places,What Is The Only American State That Starts With The Letter 'A' But Doesn't End With The Letter 'A' ,Arkansas ,General,"Countries of the world: landlocked country in southern Africa, the capital is Gaborone",Botswana +General,What's a single unit of quanta called,Quantum,General,Series of chemical elements that share similar electron orbital structures and hence similar chemical properties,Transition Elements,General,Who sang 'in the air tonight',Phil collins +General,Of who was Oedipus king,Thebes,General,"Who was the Norse god of light, beauty & peace",Balder,General,In Animal Farm Benjamin was what type of creature,Donkey +General,"Which Literary Fictional Family Lived At ""Hayworth Rectory"" In Yorkshire",The Borrowers,Geography,What is the capital of Algeria,Algiers,General,"What contagious disease was scheduled for complete eradication on June 30, 1999",Smallpox +Science & Nature,What Is Calcium Carbonate ,Chalk ,General,Which is the most westerly South American country through which the Equator passes,Ecuador,General,Alice Springs is a place in which country,Australia +General,What company is the largest aircraft manufacturer in the world,Boeing,General,What countries does the Mont Blanc Tunnel join,France and italy,General,"In mythology, who was the wife of Odysseus",Penelope +General,What is the fear of daylight or sunshine known as,Phengophobia,Food & Drink,What First Went On Sale In America In 1886 ,Coca Cola ,General,A study in shades of grey in the name of what picture,Whistlers Mother +General,What new territory was created in Canada in 1999,Nunavut,Geography,What color does the bride wear in China,Red,General,What country is the worlds largest exporter of Frogs Legs,Japan +History & Holidays,"In Halloween, Michael Meyers wore a Halloween mask of what famous character?",Captain Kirk mask,General,"In the game 'banjo-kazooie', what is gruntilda",Witch,Entertainment,"Popeye's chief adversary has two names, Bluto and ______",Brutus +Music,Where is Blue Jay Way?,Los Angeles,General,Who did Adolf Hitler dictate Mein Kampf to while in prison?,Rudolf Hess,Music,What Was Mud's First No.1 Single,Tiger Feet +General,"Austrian physicist and Nobel Laureate, best known for his mathematical studies of the wave mechanics of orbiting electrons",Schrodinger,General,Who composed the symphonic poems Swan of Tuonela and Kullervo,Sibelius,Toys & Games,"In which game or sport are ""Staunton"" pieces used",Chess +General,What is the integral of the magnetic field with respect to the area,Magnetic,Science & Nature,In What Country Was The Portable Domestic Electric Room Heater First Marketed In 1912 ,England ,General,What is the fear of ice or frost known as,Pagophobia +Music,Name The Three Greats That Played Guitar With The Yardbirds,"Eric Clapton, Jeff Beck, Jimmy Paige", Geography,What is the capitol of Iceland?,Reykjavik,General,What rock is formed from layers of mud & clay,Shale +General,What is the name of the fin on a fish's back,Dorsal fin,General,"How many ice ages have there been in the last 150,000 years",Two,General,What disease was once known as the white plague,Tuberculosis +General,If you had podobromhidrosis what would you have,Smelly Feet,General,"In Italy their I Puffi, In Hungary Torpok, Samafu in Japan - what",Smurfs,General,Chief electrician in a film unit,Gaffer +Music,"Who Took ""Smoke Gets In Your Eyes"" To No.1 In The UK In 1959",The Platters, History & Holidays,What Toy Still Hugely Popular Today First Went On Sale In 1959 Costing Just $3 ,Barbie ,General,What was Fred Astaire's real name,Frederick austerlitz +Religion & Mythology,"In Greek mythology, who had nine heads?",Hydra,Geography,With which country is Prince Rainier III identified,Monaco,General,What is the young of this animal called: Bear,Cub + History & Holidays,Who Released The 70's Album Entitled Blue ,Joni Mitchell ,General,Who was shot and killed in saloon number ten,Wild Bill Hickock,General,In Ohio by law pets have to carry what,Lights on tails at night +Art & Literature,Which novel by Mary Shelley was subtitled 'The Modern Prometheus'?,Frankenstein,General,Which radioactive gas is emitted by granite rock formations,Radon,General,I was Born under a Wand'rin Star' came from which film,Paint your wagon + History & Holidays,Who married actress Nancy Davis?,Ronald Reagan,Geography,What's the largest museum in the world ,The louvre ,General,In which country is it polite to stick your tongue out at your guests?,Tibet + History & Holidays,Who built the Lambarene missionary station?,Albert Schweitzer,General,U.S. Captials - Connecticut,Hartford,General,What US ports name means in Choctaw long haired people,Pensacola in Florida +General,Where's america's oldest zoo,Philadelphia,Art & Literature,Which Author Wrote Novels Apon Which The Tv Series (All Creatures Great & Small) Was Based ,James Herriot ,General,Vehicle designed for the transportation of the sick or injured,Ambulance +General,What is the Hindu Kush,Mountain Range,General,"The Modern Pentathlon event includes the disciplines fencing, cross-country running, riding, shooting and which other event",Swimming,General,License Plates: Who are SHEMP1's favorite comedians,Three stooges +General,Selenophobia is the fear of,The moon,General,"Harp, Elephant and Leopard all types of what",Seal,General,What is the fear of large things known as,Megalophobia +General,Californian law no shooting any animal - moving car except what,Whale,General,Who Wrote The Novel 101 Dalmations?,Dody Smith,General,"American inventor and teacher of the deaf, most famous for his invention of the Telephone",Alexander Graham Bell +General,What is a group of turtles,Bale,Science & Nature,Which Is The Largest Living Rodent ,Capybara ,General,What was the name of prince Hamlet's father in the play Hamlet,Hamlet +General,Clomipramine an anti depressant had what unusual side effect,Orgasm when yawn,General,What word - last arrow in archery contest or the final outcome,Upshot,General,"Whose final words were ""It hurts""",Charles De Gaulle +General,Pablo picasso was abandoned by the midwife just after his birth because she though he was stillborn. by who was he saved,An uncle,General,In film making what does a Blimp do,Covers camera – reduce noise,General,Who became king of Scotland two years before Edward the Confessor took over as king of England?,MacBeth +General,Who invented the yo yo,Donald f duncan,Entertainment,"What classic rock band sang the song 'Paint It, Black'?",Rolling Stones,General,"Converter ""Slow Ride"" was Foghats biggest hit from this album released in 1975",Fool +General,"In the film Friday The Thirteenth, which character was the killer",Mrs. vorhees,General,"7% of Americans don't know the first nine words of the American anthem, but know the first seven words of which anthem?",Canadian anthem,Science & Nature,The Third space shuttle was named __________.,Discovery +General,Randy travis said his love was 'deeper than the ______',Holler,General,"Name the artist/band that recorded this song ""No Limit""",2 unlimited,General,In Schulter Oklahoma nude women cannot do what,Gamble +General,The U.S. Congress comprises the Senate and which other house or chamber,House of representatives,Science & Nature, An ox is a castrated bull. A mule is a sterile cross between a male ass and a __________,Female horse,General,What is another name for a lexicon,Dictionary + History & Holidays,What Was Founded In The Reign Of Henry VIII For The Protection Of The Royal Person? ,Yeoman Of The Guard Or Beefeaters ,Sports & Leisure,Who Was The only Australian To Win The Men's Singles At Wimbledon In The 1980's? ,Pat Cash ,Music,In 1998 David Trimble & John Hume Joined Which Band On A Belfast Stage In Support Of The Good Friday Peace Agreement,U2 +Music,Whose Real Name Is William Michael Albert Broad,Billy Idol,General,What kind of shark has killed the most people,Great white,Science & Nature,What Is The Algebra Involved In Set Theory Called ,Boolean Algebra  +General,What spice is used to make a whiskey sling,Nutmeg,General,What is a Paradiddle,Drum Roll,General,"What is the sudy of humankind, societies and customs",Anthropology +Geography,The largest city on the Mississippi River is _________________,"Memphis, tennessee",General,With what does Dr. Seuss' name rhyme,Rejoice,Music,"Prince's ""Baby Im A Star"" Featured On The Sou8ndtrack To Which Film",Purple Rain +Music,"Mason, Wright & Waters Who Is Missing From This 1972 Line Up",Gilmour (Pink Floyd),General,Which US state gets the most overseas visitors,California – Florida second,General,What links Hof Malomo Zagreb Belgrade Porto Varna Vevey,International film festivals +Science & Nature, Many corals receive nourishment from algae which grow inside their __________,Tissue,Food & Drink,The Prongs On A Fork Are Actually Known As What ,Tines ,General,Which timepiece has the most moving parts,Egg timer +General,ROK international car registrations which country,Korea,General,What is the Capital of: Afghanistan,Kabul,Music,Who Was The Drummer With The Osmonds,Jay Osmond +General,What does the girls name Rebecca mean,Noose,Music,Which Group Had A Hit With The Final Countdown,Europe,General,What does btU.S.tand for,British thermal unit +General,"Who barely received passing grades for some of his film performances, but had a no. 1 hit with 'young love' in january 1957",Tab hunter,General,"""Faux pas"" means ___________",Mistake,General,Parcheesi is the national game of which country,India +Entertainment,Who is Sally Brown's sweet baboo?,Linus,General,Only 6 people died in what historic event,Fire of London 1666,General,A male singer whose sexual organs have been modified is known as a what,Castrato +General,What year was the first tooth extraction under anaesthetic performed,1846, History & Holidays,In what country did John Lennon and Yoko Ono start their honeymoon? ,Holland/Amsterdam ,General,"Countries of the world: western Asia, Amman is the capital",Jordan +Music,"What Was Unique About Depeche Mode's Single ""Personal Jesus""",First Of Their Singles To Feature Guitars, Geography,Where is Calcutta?,India,Food & Drink,Which Fruit Is The Main Ingredient Of The Relish Guacamole? ,Avocado  +Art & Literature,What Was The Name Of The Author Who Released A Guide To Baby And Child Care In 1946 ,Dr Spock (No Kidding) ,General,Family lived small farm Walnut Grove Plumb Creek Minnesota,Ingles - Little House on Prairie,General, What is the common name for a Japanese dwarf tree,Bonsai +Food & Drink,Name the two dishes named after an opera singer? ,"Peach Melba, Melba Toast ",General,What used to be caught in a fanny trap,Foxes Mid ages fem fox nick fanny,General,There are 33 words on the back of a bottle of what beer,Rolling Rock +General,Cinnamon flavored candies with a Mexican theme.,Hot tamales,Entertainment,What color was Bullitt's car?,Green,Music,Which Duo Went Straight In At No.1 In The Charts On May 20 1995,Robson & Jerome +Science & Nature,What Does E Stand For In E- Numbers? ,European ,Science & Nature,What well known marsupial is the wallaby related to?,Kangaroo,General,What is the extinct animal Megantheron better known as,Sabre Toothed Tiger +Entertainment,Who wrote the opera 'norma'?,Vincenzo Bellini,General,Who signed the 'thanksgiving proclamation',Abraham lincoln,General,What name is given to the Friday in holy week,Good friday +General,Until 1965 what was illegal for Connecticut married couples,Contraception,General,What is the name of the Formula One Grand Prix racing circuit located in Brazil?,Interlagos,General,The study of man & culture is known as ________,Anthropology +General,What were sonny and cher originally called,Caesar and cleo,General,From what country do we get Avia wines,Yugoslavia,General,Bismarck is the capital of ______,North dakota +General,Which religion believes in the Four Noble Truths,Buddhism,General,What is a group of roe deer,Bevy,General,"Name the European hit, now an animated series about underwater people",The snorks +Geography,What city is the Christian Science Monitor based in,Boston,General,Mexico's equivalent to the dollar is the ______?,Peso,General,Prosopography is the study of what,Careers +General,Who invented the toothbrush ?,William Addis,Science & Nature," More than one million stray dogs and over 500,000 stray cats live in the __________",New york city,General,In English packs it's the Jack or Knave what in French packs,Valet +General,Whose hit I Will Survive became an anthem for Women's Lib,Gloria gaynor, History & Holidays,What sort weapon was used in The Shining ,An Axe ,General,Collective nouns - a giggle of what animals,Hyenas +Music,How Are The Trio Of Ferguson Pinkney And Holliday Better Known,The 3 Degrees,General,What was the distress call before SOS,CQD - come quick danger,General,What animal became officially extinct in 1681,Dodo +General,What is soccer star pele's real name,Edson arantes do nascimento,General,What 19th century explorer translated the Kama Sutra,Sir Richard Burton,Toys & Games,Where does the annual Poker World Series take place?,Las Vegas +General,Nenen-Kona is sold in Russia - what do we call it,Pepsi-Cola,General,Who was the daughter of Mary Wollstonecraft & William Godwin,Mary shelley,General,Who makes robitussin cough syrup,Ah robins +Music,"Go West Had A Hit With ""We Close Our Eyes"" Or ""Eyes Wide Shut""",We Close Our Eyes,General,What star sign is Harry Potter,Leo,General,What do the Italians call Munich?,Monaco of Bavaria +Music,Which Was The First Group To Appear In Madame Tussauds As Waxwork Models,The Beatles,Music,What Was Elvis Presleys Middle Name,Aaron,General,"What are Claymore, Thistle and Piper",North Sea Oil Fields +Music,Which rock singer was badly injured whilst riding his quad bike in 2003,Ozzy Osbourne,General,Name of the Roman hippodrome used for chariot races,Circus Maximus,Music,What Group Did Ray And Dave Davis Form In 1962,The Kinks +General,On what do approximately 100 people choke to death every year?,Ballpoint pens,General,What is the basic flavouring of kahlua,Coffee,General,"Who is known as ""the world's oldest teenager""?",Dick Clark +General,What was Trotsky's first name,Leon,General,In what does a steganographer write messages,Invisible ink,General,How often does Halley's comet become visible,Every 76 years +General,Fred Silverman invented the name Scooby Do named after who,Frank Sinatra,General,What is the international telphone dialing code for Antarctica,672,General,"According to the Bible, what kind of woman had a price above rubies",A virtuous woman +Religion & Mythology,In what month is Christmas observed,December,Science & Nature,What word is used for a male duck,Drake,General,Name the largest island in the world,Australia +Music,What Was Arrested Development's First UK Chart Entry,Tennessee,General,Ondinism is arousal from what,Urine,Music,What Did The Human League Keep Feeling,Fascination +General,What authors (unused) final two names are Bower Yin,Leslie Charteris,People & Places,Who Was Eric Sykes On Screen Sister ,Hattie Jaques ,General,In the original Star Trek who has unrequited love for Mr Spock,Nurse Chapel +Music,"Paul Hardcastle's Hit ""19"" Was About Which War",Vietnam,General,Where is the world's largest desert,North africa,General,Whom did Isabel Peron succeed as President of Argentina in 1974,Juan peron +General,Who ran after the farmer's wife,Three blind mice,General,"Widely used designation in the United States, Canada, and several other countries for the association of more than 2000 community-based organizations that work to meet local health and human-care needs",United Way,Science & Nature,Which Animal Has Young Called Elver? ,The Eel  +Sports & Leisure,What Is The Most Valuble Property In A Game Of Monopoly ,Mayfair ,General,The book called 'the cocktail party' was written by _________,Ts eliott,General,Simmons film - who was the female star of bugsy malone,Jodie foster +General,The North and South Islands of New Zealand separated by what,Cook Strait,General,"On a human body, hair grows out of pits in the skin. What are these pits called",Follicles,General,In which decade if the 20 century was the first Filofax sold,1920's +General,Peter Goldmak invented what in 1948,LP record,General,Little Jumping Flea literal trans of what Hawaiian instrument,Ukulele,Science & Nature,He transmitted radio signals across the Atlantic in 1901.,Enrico Marconi +Sports & Leisure,Who Was The Only Female To Win A BBC Sports Personality Of The Year Award In The 1990's? ,Liz McColgan ,Art & Literature,What Is The Surname Of Cathy In Wuthering Heights ,Earnshaw ,Music,"Born Roberta Joan Anderson Who Had A 1970 Hit With ""Big Yellow Taxi""",Joni Mitchell +General,Ecuador was named after who / what,The Equator,General,Which singers first band was called The Spiders,Alice Cooper,Music,In December 1990 Who Became The Only Act To Top The Charts In 5 Separate Decades?,Cliff Richard +General,What country used the first aircraft equipped bomber in war,Italy Italian Turkish war 1912,General,What meat outsells mutton and lamb combined in Sweden,Horse meat,Music,"This Was A Highly Successful Film In 1961, A Flop Broadway Musical In 1966, And A Top Ten UK Hit In 1996, What Is It?",Breakfast At Tiffany's +General,Oil is the most traded product in the world what is the second,Coffee,General,Who is the Patron Saint of Grave diggers,St Anthony,Food & Drink,What three main ingredients are added to mayonnaise to make a Waldorf salad? ,"Apple, celery and walnuts " + History & Holidays,In which country do people go to the beach to jump seven waves and throw flowers in the sea while making a wish? ,Brazil ,General,What is the many- legged mythological sea creature of Scandinavia,Kraken,General,Who sang about 'commitment',Leann rhimes +Geography,Where are the headquarters of the CIA?,"Langley, Virginia",General,A pugilist is a _____.,Boxer,General,Catherine the Great kept who in an iron cage in her bedroom,Wigmaker +Sports & Leisure,How Many Times Did Lester Piggot Win The Derby ,Nine ,General,John Chapman was the real name of someone famous in Ohio?,Johnny Appleseed,General,What is a group of snakes,Nest +General,In what Australian state would you find Bundaberg,Queensland,General,Melophobia is the fear of,Music,General,After homes and jobs where do Americans spend most time,Shopping Malls +General,Who did david kill,Goliath,General,Dish what is the thread used in surgery to tie a bleeding blood vessel,Ligature,General,Who was jethro tull,Agriculturist +People & Places,For What Is Peter Roget Famous ,His Thesaurus Of Words ,General,What is the Capital of: Turks and Caicos Islands,Grand turk,General,"Until 1947, what mixture used for calming babies contained opium",Gripe water +Music,"Who Had A Hit In 1985 With ""The Heat Is On""",Glen Frey,General,In Shakespeare who is Romeos love - before Juliet,Rosaline,General,"DNA analysis suggests there are three distinct species of this animal, African forest, African savanna & Indian. What animal",Elephant +General,Which U.S. president was fatally shot in 1881,Garfield,General,What is the clarified butter used in Indian cuisine,Ghee,General,Which company manufactures Calvin Kline's Obsession,Unilever +General,"Who In The World Of Music Has The Real Name ""Wladziu Valentino"" (Pronounced) ""Vlad Zoo""",Liberace,General,Legal Terms: A formal agreement enforceable by law.,Contract,General,Which animal has the largest eyes,Giant squid +Science & Nature,What animal can live several weeks without its head?,Cockroach,General,Which eponymous character was Thane of Cawder Glaimes,Macbeth,General,Pope Clement VII made it illegal for anyone else to eat what,Mushrooms +General,"Which of Shakespeare's kings cries: ""A horse! A horse! My kingdome for a horse!""",Richard iii,General,Marconi transmitted radio signals across which ocean,Atlantic ocean,General,Which painting medium is an emulsion of egg yolks and water,Tempera +General,St Florian is the patron saint of which profession?,Firefighters,General, Legal Terms: To steal property entrusted to one's care.,Embezzle,General,What was the recently closed Wembley Stadium called in 1923,The Empire Stadium +General,Who did Sitting Bull call little sure shot,Annie oakley, Language,What does SOS stand for,Save Our Souls,Food & Drink,What was Bretts last supper in the film Pulp Fiction ? (food and beverage) ,A Big Kahuna (cheese)burger and Sprite  +General,From which country does spinach originate,Iran,General,The earliest recorded one held 1887 Sheen House Richmond ?,Motorcycle Race,General,What is the drink 'Southern Comfort' flavoured with,Peaches +Sports & Leisure,The Golden Gloves Championship is in Which Sport ,Boxing , Geography,What is the basic unit of currency for Benin ?,Franc,General,"Who wrote the book ""wouldn't take nothing for my journey now""",Maya +General,Who is the roman messenger god,Mercury,General,What would you do with a wandering sailor,"Plant it, it’s a plant",General,How many member states are there in the United Arab Emirates,Seven +General,Who directed 'The French Connection' and 'The Exorcist',William friedkin,General,"Where did you play with Ben, Pauline and Michele",Microsoft Windows Hearts game,Geography,Which country has the internet domain .me? ,Montenegro  +General,At which grand prix did Nikki Lauda make his comback,Italian,General,"If you left Oklahoma by crossing the Red river in a southerly direction, which American state would you enter",Texas,General,If you are born in March what is your Flower,Violet +General,What is the capital of the Spanish region of Aragon,Zaragoza,General,Who Was The Last British Player To Win A Singles Title At Wimbledon?,Virginia Wade In 1977, History & Holidays,Which 50's Movie features the Line 'Tomorrow you'll be one of us' ,The Invasion of the Body Snatchers  +General,38 million Americans one in five don’t like what,Sex,General,Lucknow is a city in India - and what other country,Canada,General,Cirrus is a cloud type - what literal translation of its Latin name,Lock of Hair +General,What was painted on peter fonda's helmet motorcycle helmet in 'easy rider',Stars and stripes,General,"What woman is the wife of prince phillip, the mother of anne, andrew, charles and edward, and the daughter of george vi",Elizabeth ii,General,What is a gharial,Fish eating Nile Crocodile +General,Where do men play each year for the Challenge Cup,Wimbledon,General,What name is given to a countries song played on official occasions,National anthem,General,What country was the setting of 'you only live twice',Japan +General,What is the largest city in switzerland,Zurich, History & Holidays,"When is the Feast of St. Nicholas? Dec 6th, Dec 18th, Dec 25th, Dec 27th ",Dec 6th ,General,What singer sang the song Spank Me,Madonna +Entertainment,"In the film ""Bright Eyes"", Shirley Temple sang about this boat.",The Good Ship Lollipop,Sports & Leisure,Who Has A Best Friend who is known to his close pals as Five Bellies ,Paul Gascoigne ,General,Which singer went from 'The Libertines' to 'Babyshambles'?,Pete Doherty +Geography,Which Metallic Element Has The Symbol EU ,Europium ,General,Duffy: The Good Earth,Buck,General,What should you give on a 35th wedding anniversary,Coral +Music,Who is the oldest Beatle?,Ringo Starr,General,What is the southernmost country on the Balkan peninsula,Greece,General,Trudeau 3399 what sport do you rack your balls in,Billiards +General,What is the number of blue razor blades a given beam can puncture,Gillette,Science & Nature,Which colour has highest wavelength in the visible spectrum?,Red,Art & Literature,"A method of producing images or letters from sheets of cardboard, metal, or other materials from which forms have been cut away. ",Stenciling +Music,"What Group Consisted Of Marc Bolan, Steve Peregrine Took",T-Rex,General,Who's Best of Album is called Paint the Sky With Stars,Enya,Geography,What symbol is on the flag of Vietnam,Star +General,Name a quadruped beginning with the letter N,Newt - Nutria – Nagli,General,Which fictional character lived at Montague street before moving,Sherlock Holmes, History & Holidays,"His wife was Roxana, his horse was Bacephalus, he was?",Alexander the Great +General,When do parallel lines meet,Never,General,Who sailed in the Nina - Pinta and Santa Maria,Christopher Columbus,General,When were fortune cookies invented,1918 +General,Who wrote the opera Der Rosenkavalier,Richard Strauss,General,In which language was the poem 'Beowulf' written,Old english,General,What is the smallest bone in the body,Stirrup bone +Science & Nature,Which Dog Bites Human Beings More Than Any Other? ,The Alsatian ,General,Where is the Nep stadium,Budapest,Science & Nature,What Do You Call A Person Who Studies Earth Quakes ,Seismologist  +General,Technical theater: what's the instantaneous killing of all stage lights,Blackout,Science & Nature,"What nationality is Erno Rubik,inventor of the 80's craze the Rubik's Cube? ",Hungarian ,Music,Upon Which Shakespearean Play Is The Musical West Side Story Based,Romeo & Juliet +General,What is the product for the slogan 'a crown appears on your head and trumpets sound when you taste it',Imperial margarine,General,The Invisible Empire is better known as what,Klu Klux Klan,Music,"There Have Only Been 2 No.1 Singles That Have Rhymed ""Eskimo"" With ""Arapahoe"" Name Them",Hit Me With Your Rhythm Stick & Chicken Song +General,In what film did Tommy Lee Jones make his debut,Love Story - As Hank,General,Which musical was based on H.G.Wells' novel 'Kipps',Half a sixpence,General,Who wrote Breakfast at Tiffany's,Truman Capote +General,What emergency safety device was first used in 1945,Ejector Seat,General,The Lambada dance originated in which country?,Brazil,General,Old apothecaries weights 20 grains in a what,Scruple +General,Who hit the first golf shot on the moon?,Alan Sheppard,General,Three under par on a hole of golf is called a(n) __________,Albatross,General,Who was Robbin Island Prison's most famous inmate ?,Nelson Mandella +General,What is the fear of stealing known as,Kleptophobia,General,"This artist cut off his ear & sent it to his lover, before shooting himself dead in a cornfield",Vincent van gogh,General,What is a group of buzzards,Wake +General,What flavouring is used in the Belgian beer Kriek,Cherries,General,What is the flower that stands for: be mine,Four-leaved clover,Geography,The Little Mermaid is found in the harbour of which city,Copenhagen +General,What is the National Bird of India,Peacock,General,Legend says tortellini was created to honour what part of Venus,Her belly button,Religion & Mythology,Who is the greek equivalent of the roman god Vesta,Hestia +General,12 letter word: fancy name for fireworks,Pyrotechnics,General,Myosotis Sylvestris is the Latin name of which common plant,Forget me Not,Sports & Leisure,How many times did stirling moss win the world championship? ,Never  +General,What year did was the great stock market crash that lead to the great depression,1929,General,Camellia Sinesis evergreen shrub better known as what,Tea,General,Captain cook lost almost half his crew in 1768 on his first voyage to ______,South pacific +Music,Who Fell To His Death From An Amsterdam Window In 1988,Chet Baker,Geography,Where Is The Original London Bridge Now Situated ,At Lake Havasu Arizona ,Food & Drink,Plashing' is a term used to describe the collecting or gathering of what kind of nuts? ,Walnuts  +General,Which dance means in Portuguese snapping of a whip,Lambada,Music,The Heartbreakers Backed Which American Singer?,Tom Petty,General,What happens to 12% of Americans each year,Arrested +Science & Nature," Unlike dolphins, porpoises are not very __________",Sociable,General,"A flat, round hat sometimes worn by soldiers is a _________.",Beret,General,What is the Capital of: Guinea,Conakry +Sports & Leisure,What is the 'perfect score' in a game of Ten Pin Bowling? ,300 ,People & Places,In Which UK City Was Dick Turpin Hanged? ,York ,General,Who composed 'the four seasons',Vivaldi +People & Places,Who Founded The Playboy Magazine? ,Hugh Hefner ,General,Who designed the uniforms for the vatican city's swiss guards,Michelangelo,General,Capital cities: Taiwan,Taipei +General,Which French General left Napoleon to become King of Sweden,Jean-Baptiste Jules Bernadotte,General,A kipper is what type of smoked fish,Herring,General,"Cocktails: gin, blue curacao & lemonade make a _______",Blue lagoon +General,"What police show featured Officers Webster, Gillis and Danko",Rookies,Food & Drink,From Which Fruit Was Marmalade Orginally Made ,Quinces ,General,Frodo. Gandalf and Bilbo Baggins are all characters from what,The lord of the rings +Music,Which Group Was Banned From The Official Washington 4th July Celebrations In 1983 Because Secretary Of The Interior Said They Would Attract An Undesirable Element,The Beach Boys,Music,Name The 2 Members Of Wham,George Michael & Andrew Ridegley,General,FIDE govern what game,Chess +General,Name Frosty the Snowman's son,Chilly – Millie Daughter Crystal wife,General,Who was the tallest man,Robert wadlow,General,What is the transformation of inhospitable planets into hospitable ones,Terraforming +Music,What year did Chet Atkins release his first solo album?,1953,General,Nervous as a long tailed cat in a room full of ______,Rocking chairs,General,Madame Tussard the waxwork founder was born in which city,Strasbourg +Tech & Video Games,Who was the main character in the original 'Street Fighter'? ,Ryu,Music,Who were the original four members of the pop group Genesis? Point for each,"Peter Gabriel,Phil Collins, Mike Rutherford & Tony Banks",Music,"Who Recorded The Million Selling Album ""No Parlez""",Paul Young + Geography,What is the capital of Latvia ?,Riga,General,Who had a hit in the UK singing about the Streets of London,Ralph McTell,General,What is the state bird of New York,Bluebird +General,Who Is The Patron Saint Of Lovers ,St Valentine ,General,What was the last item shown on British TV before WW2,Mickey Mouse,Food & Drink,What Is Aioli ,Garlic Mayonnaise  +General,Defender of the constitution whose headstone reads '..that nothing's so sacred as honor and nothing's so loyal as love',Wyatt earp,General,What's the state bird of utah,Sea gull,General,What sport uses rubber cushions and slate beds,Billiards +Science & Nature, The crocodile is a cannibal; it will occasionally eat other __________,Crocodiles,General,What is the royal house of Monaco,Grimaldi,General,What was the name of the company that the characters on Taxi worked for?,Sunshine Cab Company +General,Who gets bitten by vampire bats on the big toe,Campers,General,Daysypgal people suffer from what,Hairy Arse,Music,Name the group that linked with Motorhead on the St Valentine's Day Massacre EP?,Girlschool +General,"In England what links Arden, Dean, Kielder and New",Forests,General,Why did certain busses in Staffordshire refuse to pick up people,Keeping to schedule,General,When a satellite is closest to Earth its position is called what,Perigee +General,When did Nostradamus live,1503 1566,Religion & Mythology,Who is the Norse god of thunder and war?,Thor,General,Which country has the longest coastline?,Canada +General,Hippophagic society members support what,Eating horsemeat,General,Artemis is Greek Goddess of what - only one among all Gods,Virginity and Chastity,General,Barbary Apes live on what Mediterranean feature,Rock of gibraltar +Geography,"This country occupies the ""horn of Africa"".",Somalia,Science & Nature,Which Astronaut piloted Apollo 11 while Armstrong and Aldrin walked on the moon ,Michael Collins ,Sports & Leisure,What Sport Would You Expect To See In A Velodrome? ,Cycling  +General,Bozzoli is what shape of pasta,Cocoons, Geography,What country are the Islands of Quemoy and Matsu part of?,Taiwan,Sports & Leisure,Which British Boxer Was First To Win 3 Lonsdale Belts Outright? ,Henry Cooper  +General,"Name the connection bewteen the ""A-Team"" and ""Battle Star Gallactica?""",Dirk Bennidic,General,In The Austin Powers Movies What Is The First Name Of Austin Powers Arch Enemy Dr. Evil,Dougie,General,What don mclean song laments the day buddy holly died,American pie +General,What does a lepidopterist collect,Butterflies,Sports & Leisure,"Is The Oaks Race , A Race For Colts Or Fillies ",Fillies ,People & Places,After Whome is the Teddy Bear Named ,Theodore Roosevelt  +Music,"In ""Penny Lane"", what did the Fireman keep in his pocket?",A portrait of the Queen,General,"In which constellation is the night's brightest star, Sirius",Canis major,General,Whose first single released July 1961 was Buttered Popcorn,The Supremes +General,From which Shakespeare play does the line 'The course of true love never did run smooth.' come,A midsummer night's dream,General,"Name of the ""cow town"" Joseph McCoy developed in the 1860s",Abaline,General,What's the largest and most powerful of the american cats,Jaguar +Sports & Leisure,Which premiership team used to play at the baseball ground? ,Derby County ,Science & Nature,Quinine is added to water to make _______.,Tonic water,General,Pellagrophobia is the fear of,Pellagra +Music,In what year did Bob The Builder have a U.K. Xmas No1 hit with “Can We Fix It”?,2000,General,Most Jell-o contains crushed what,Hooves,General,"On The Subject Of Fine Wines, Or In This Case Not So Fine , Which Country If The Origin Of The Word “Plonk”?",Australia +General,What is the bone at the end of the spine,Coccyx,General,"What late television commentator closed with ""good night and good luck""",Edward r murrow,General,Which Country Was The First To Allow Woman To Vote?,New Zealand +General,What is a Caryatid,Building support woman shaped,General,Who was the Greek philosopher who decided he'd rather drink hemlock than deny his beliefs,Socrates,General,In Which Tv Show Were “ Henry Davenport & Sally Smedley ” Newsreaders,Drop The Dead donkey +General,"Which city is a 'player with railroads, and the nation's freight handler'",Chicago,General,Who wrote Tropic of Cancer and Tropic of Capricorn,Henry miller,General,"Who plays the part of Satan in the film The Devil's Advocate, released in 1998",Al pacino +General,"What fictional character was ""The Napoleon of Crime""",Professor Moriaty,General,The gluteus maximus muscle is the bulk of the,Buttock ass butt,General,What does sub rosa mean,Not literally but in plain english in secret +General,Who was puff the magic dragon's human friend,Little jackie paper,Religion & Mythology,Who was the ancient Egyptian goddess of the sky and queen of heaven ?,Hathor,Science & Nature,This ugly creature has patches of red on his rear-end.,Mandrill +Food & Drink,"Booze Name: 2 oz. gin, juice of 1/2 lemon, 1 tsp sugar, top with soda water.",Gin fizz,General,"Mauna Loa, Paricutin, Surtsey and Susya are all what",Volcanoes, Geography,What is the capital of Estonia?,Tallin +Geography,Which country administers Christmas Island,Australia,General,How much do nine pennies weigh?,One ounce,Entertainment,The initials of the band NIN stand for?,Nine Inch Nails +Music,"Who Did Stevie Wonder Duet With On The Track ""My Love"" In 1988",Julio Iglesias,General,What is the young of this animal called: Rabbit,Bunny kit,General,What is a chihuahua named after,Mexican state +General,What was the title of Joe Loss's signature tune,In the mood,General,How did Alfred Nobel make his money,He invented Dynamite,Sports & Leisure,What Animal is Featured On a Ferrari Badge ,Horse/Stallion  +General,The peace of Aix-la-Chapelle was celebrated by which piece of music,Music for the royal fireworks,General,What is the geographic centre of the u.s,South dakota,General,What is the only country to have a single color flag,Libya +Food & Drink,Booze Name: 1 oz. gin and 1 oz. orange juice.,Orange blossom,General,In 1976 in USA 23 people got swine fever and died from what,The Treatment,General,"Who did time magazine nickname the ""first lady of radio"" in 1939",Kate smith +General,What USA state drinks the most beer,California,General,The Washington Post received the 1973 Pulitzer prize for reporting what,Watergate scandal,General,"Who said ""never kick a fresh turd on a hot day""",Harry S Truman +General,Where did you find cherry strawberry orange apple grape bird,Pac Man speed up things,Music,Who Enjoyed Chart Success In 1991 With Diamonds And Pearls?,Prince,General,"By raising your legs slowly and laying on your back, in what can you not sink",Quicksand +Music,"Which One Of These Songs Was A Hit For Madness ""Love Is Enough"", Baggy Trousers, Enough Is Enough""",Baggy Trousers,General,Which American As Of 2010 Has Spent The Longest Term In Office,Franklin D Roosevelt,General,What legume did George Washington Carver use as a basis for over 300 inventions,Peanuts +General,"What are mother mary's ""whispered words of wisdom""",Let it be,General,What teaching aid is made mainly from soft limestone,Chalk,General,Who is the roman equivalant of the greek god eros,Amor +General,Eddie Irvine is contracted to drive for which car company in 2001,Jaguar,General,What is pure china clay called?,Kaolin,General,The date of which christian festival was fixed in 325 ad by the council of nicaea,Easter +Geography,What is the capital of Bangladesh,Dhaka,Food & Drink,What is the French word for a cake shop? ,Patisserie , History & Holidays,What is the next line 'Christmas is coming the goose is getting fat …'' ,Please put a penny in the old mans hat  +General,In Which Shakespeare Play Does A Ghost Not Appear,A Midsummer Nights Dream,Music,What Is The Only Song From Michael Jackson's Thriller Album To Reach The No.1 Spot?,Billie Jean,General,Perfume from the fruit of a dwarf orange tree,Bergamot +General,Who is the abandoned man cub in The Jungle Book,Mowgli,Music,"Which Timeless Vocalist Recorded The Album ""Private Collection"" In 1988",Cliff Richaqrd,General,What bird lays an egg that is roughly a quarter of its body weight?,Kiwi +Music,"Which Queen Of Soul Did George Michael Team Up With For The Song ""I Knew You Were Waiting For Me""",Aretha Franklin,General,34-40 knots wind speed signifies what weather condition,Gale,Music,Which Northern UK English city Did The Beatles Come From,Liverpool +General,What is chiengora,Dog Hair spun into yarn,General,Dakar Is The Capital City Of Which Country,Senagal, History & Holidays,What is the name of the 'big con'' that Paul Newman and Robert Redford carry out in _The Sting_? ,The Wire  +Sports & Leisure,Who on the F.A. Cup the most times during the 70's? ,Arsenal , Geography,Which state is divided into two parts by a large lake?,Michigan,Music,"What artist had most weeks in the UK chart in 2000 was it Bob The Builder, Westlife, Craig David or Britney Spears?",Craig David +General,Who founded ASH ( Action on Smoking and Health ) in 1971,Royal College of Physicians, History & Holidays,What rock magazine that came out in the eighties is now Rolling Stone's major competitor? ,Spin ,General,What do we call what the Japanese call Oshugatsu,New Year +General,"In Hinduism, which God is known as the Preserver",Vishnu,Music,What Single GaveThe Human League A Transatlantic Number One,Dont You Want Me Baby,General,Who is the unit of sound named after,Alexander Graham Bell - Decibel +General,"Who was nicknamed ""Queen of the Swashbucklers""",Maureen O' Hara,General,Raleigh is the capital of ______,North carolina,General,"How Many Sides Does A ""Enneadecagon"" Have",19 +General,The British Patent Office since 1949 banned patents for what,Perpetual Motion Machine,General,Joseph Adams served as the minister at Newington for how many years,68,Art & Literature,To What Name Was The Hall Of Arts & Sciences Changed ,The Royal Albert Hall  +General,"What Type Of Food Stuff Comes From The French Language Literally Meaning ""Eat All""",Mange Tout,Science & Nature," Toothed __________ whales live in extended family units that, for families, constitute life_long associations. They differ from baleen whales, which form only temporary bonds.",Sperm,General,The baby ruth candy bar was named after which u.s president's daughter,Grover cleveland +General,Philippe Pages is the real name of what pianist,Richard Clayderman,General,Where does the Iditarod dog sled race take place,Alaska,General,"""One shaft of light that shows the way, no mortal man can win this day"" what is the Queen song title",A Kind of Magic +General,A man who commits Pseudogyny is doing what,Uses woman's name to deceive,General,"When using a telephone, you must wait for a ____ tone before starting your call",Dial,General,What vehicles are involved in the 'Tour de France',Bicycles +General,What kind of triangle has a hypotenuse,Right angle triangle,General,Who is christina claire ciminella,Wynonna judd,Music,Who was best man at John and Cynthia's wedding?,Brian +General,Who did macduff kill,Macbeth,Entertainment,How many freckles did Howdy Doody have?,Forty eight,General,The liquid measure of a capacity equal to 1/8 of a fluid ounce is a(n) _________.?,Fluid Dram +Music,What is Leslie West's nickname?,Mountain,General,Margaret Herrick named it in 1931 what,The Oscar – looked like her uncle,Science & Nature,Aspirin was originally obtained from the bark of which tree? ,Willow  +Food & Drink,"If you wanted your eggs fried without cooking the yolks, how should you order them in? ",Sunny side up , Geography,What is the capital of Papua New Guinea ?,Port Moresby,General,Peter Piper picked a peck of what?,Pickled peppers +Music,"Which Comedy Duo Made The Song ""Bring Me Sunshine"" Their Own",Morecambe & Wise,General,"What philosopher stated ""Hell is other people""",Jean Paul Sarte,Music,"Gloria Estefan's Band Was Called ""The Miami Sound Machine"" Or Was It ""The Furious Five""",The Miami Sound Machine + History & Holidays,Where did Gerald Durrell open his zoo in 1958? ,Jersey ,Science & Nature,In The 1933 Film Which Actress Is Held Aloft By King Kong ,Fay Wray ,General,How was Tristram Shandy (fictional character) circumcised,A Sash window fell on it +General,What Edwin Budding invention began changing the face of English landscapes in the 1820's,The lawn mower lawn mower,Music,Whose Magnificent Bald Head Featured On The Cover Of His Hot Butterd Soul Album,Isaac Hayes,General,"In Greek mythology, what were the golden apples",Apricots +General,A fagotist is a person who does what,Plays the Bassoon,General,And what was his first US number one,Heartbreak Hotel,General,What country does Gotland belong to,Sweden +Music,Name The Label Owned By Madonna,Maverick,General,"Which Famous Person Was Born In Grantham, Linconshire On 13th October 1925",Margaret Thatcher,General,The name Gregory is from the Greek meaning what,Watchman +General,Theologicophobia is a fear of ______,Theology,General,A crapulous person is full of it - what,Alcohol - it means drunk,General,"Salad plant, its root can be roasted and ground and used as coffee",Chicory +General,"Which Shop Of Jewish Origin Used To Have The Motto "" Don't Ask The Price - Tis A Penny """,Marks & Spencer,General,What do you call a female calf,Heifer,Science & Nature,"What fruit bear the latin name ""citrus grandis""?",Grapefruit +General,A common belief is that John Lennon says 'I Buried Paul' at the end of 'Strawberry Fields Forever' on the 'Magical Mystery Tour' album. What did he actually say,Cranberry sauce,General,In the Chinese horoscope what animal comes last alphabetically,Tiger,Food & Drink,Which drink is sometimes referred to as Adam's ale? ,Water  +General,In China if you order white tea - what do you get,Boiled Water,General,Which Swiss town hosts the annual Golden Rose Television Festival,Montreux,General,With what instrument is the jazz musician Chet Baker associated,Trumpet + Geography,"Located above and just below the Arctic Circle, this region became an official territory of Canada in April 1999.",Nunavut,General,A motorist who enjoys driving fast,Speed merchant,Music,"Which 60's Model Starred In Ken Russell's Film Of The Musical ""The Boyfriend""",Twiggy +General,What conc. product carried in tankers has a hazardous sign,Coca Cola,General,Which capital city stands near the delta of the Irrawaddy river,Rangoon,General,What sport do players 'tee off' in,Golf +General,Who was the actor who played Bobby Ewing in Dallas,Patrick duffy,General,What makes a noise middle octave key of F,Housefly buzz,General,What is a cachalot,A Sperm Whale +General,"Dutch-born Swiss scientist, who discovered basic principles of fluid behavior",Daniel bernoulli,Sports & Leisure,Which annual race is 4 miles and 374 yards long ,The Boat Race ,General,Where is the National Motor Museum,Beaulieu +General,Which animal sleeps with one eye open,Dolphin, History & Holidays,In what year did man first set foot on the moon,1969,Food & Drink,Devils on horseback are prunes stuffed with almonds and wrapped in what? ,Bacon  +General,Who believed that a prism split light because of 'corpuscles' of varied mass,Isaac newton,General,"What was the British equivalent of ""We Are The World?""",Do They Know It's Christmas,General,In Japan what is jigai,Female suicide +General,Which rock musician committed suicide in Scattle on 5th April 1994,Kurt cobain,Sports & Leisure,What Do The Opposite Sides Of A Standard Dice Always Total ,7 ,General,What organisation is known as the Society of Friends,Quakers +General,What's the words most popular brand of malt whisky,Glenfiddich,General,"In english mythology, who caused the death of the lady of shallot",Sir,General,Charles Bingley was a character in what classic novel,Pride and Prejudice + Geography,What is the capital of Tuvalu ?,Fongafale (Funafuti atoll is the location of the capital Fongafale! So pls don't complain about the correction ;-)),General,As what was Louis XIV also known,Sun king,General,Sticky tape was produced in which year,1928 +Sports & Leisure,Where were the 1928 Olympics held ?,"Amsterdam, The NetherlandsERROR: Ret: amsterdam, (the netherlands|holland)",General,License plates: what's cheops's profession,Egyptologist, Geography,What is the basic unit of currency for Nicaragua ?,Cordoba +General,In which county does the River Mersey rise,Derbyshire,General,"In an isosceles triangle, if the two equal angles are 35 degrees, what is the third angle",110 degrees,Music,What Is The Name Of Bill Wymans Knightsbridge Restaurant,Sticky Fingers +General,Who composed the opera I Pagliacci,Leoncavallo,General,Denmark Has Twice As Many “ What? ”Than It Does People?,Pigs,General,What film is generally considered the worst film ever made,Attack of the +Music,What was Buddy Holly's First Hit Record as the Crickets reaching No.1 in the UK & USA?,That'll Be The Day, History & Holidays,Who caused outrage with the following quote 'We are more popular than Jesus now'' I don't know which will go first Rock n Roll or Christianity'' ,John Lennon ,General,"What is the meaning of dc in ""Washington DC""",District of columbia +General,"What was ""The gun that tamed the West""",Winchester rifle,General,In Which US State Does The TV Show 'High School Musical' Take Place,New Mexico,Geography,Gore is also known as the Brown Trout Capital of the World and New Zealand's ___________,Country music capital +General,How fast can a kangaroo hop,Forty mph,Sports & Leisure,What Was Alex Ferguson's Team Before He Left To Manage Manchester United? ,Aberdeen ,Food & Drink,Ethanoic (or acetic) acid is the major constituent of which everyday condiment?,Vinegar +General,Bohemian Rhapsody was on what Queen album,A Night at the Opera,General,What flavours root beer,Sarsaparilla,General,What state is only part of the U.S. by treaty,Texas +Sports & Leisure,Which famous duo won the BBC Sports Personality Of The Year In 1984? ,Torvill & Dean ,General,West Indian cricketer Laurence Rowe gave up 1976 mid test why,Allergic to Grass,General,"What rock music term was coined by william burroughs in his book ""the soft machine""",Heavy metal +General,What is measured in units called phon,Sound,Music,"With 12 Hits In This Era Only ""Silly Love"" Did Not Make The Top Ten For Which Group",10CC,Religion & Mythology,"In Egyptian mythology, who was Horus' mother?",Isis +General,She starred in Broadcast News & The Piano.,Holly Hunter,General,We know what lasagne is - what is a lassagnum its named from,A Cooking Pot,General,What animals name comes from the Sanskrit to steal,Mouse - Musha +General,Who voiced Mr Spock in the cartoon version of Star Trek,Leonard Nimoy,General,Who was the eldest member of The Beatles,John lennon,General,When was the Fascist party founded in Italy,1919 +Music,Which Group Was Originally Called The Primettes,The Supremes,General,Who wrote the Dune series of SF novels,Frank Herbert,General,Who recorded such popular songs as 'Whose Zoomin' Who' and is known as the Queen of Soul,Aretha Franklin +General,"A series of arches supported by columns or piers, or a passageway formed by these arches. ",Arcade,Music,Which Beatles song was written by American Country singer Buck Owens?,Act Naturally,General,"Football Team, the baltimore ______",Colts +General,"Known as 'The Ace of Aces', who was the leading American fighter pilot of World War One",Eddie rickenbacker,General,Who played richie cunningham in the tv show happy days,Ron howard,General,Norma Talmage in 1927 made the first - the first what,Footprints concrete Grumman theatre +Food & Drink,What TV sitcom about Liverpool family life did Carla Lane write? ,Bread ,Entertainment,How does Wonder Woman control her invisible airplane,Mental powers,General,What nationality is athlete Greta Waitz,Norwegian +General,"Who started a giant fast food chain in 1928 in a hut near Bradford, Yorkshire",Harry ramsden,General,Which is the longest river in Mexico,Rio grande,Music,Who Was The Fictional Band In The Famous Movie Of The Same Name That Made Fun Of Heavy Metal Bands,Spinal Tap +General,"Who sung the Song ""Fast Car""?",Tracy Chapman,General,In the Snoopy cartoons what does Lucy offer in her booth,Psychiatric help,General,What was invented in 1855 45 years later than it was needed,Can Opener +Science & Nature,Do You Use More Muscles To Smile Or To Frown ,Frown , History & Holidays,"In the movie 'Miracle on 34th Street', Kris Kringle is hired to play Santa Claus in what large department store? ",Macys ,General,"Countries of the world:eastern coast of Baltic Sea in northeastern Europe, the capital is Vilnius",Lithuania +General,The earth's circumference is approximately how many miles,24870,General,"In logic, what is the form of reasoning by which a specific conclusion is inferred from one or more premises",Deduction,General,To what is the area of a human lung equal,Tennis court +General,Which Hollywood Actress Was Responsible For Uttering The First Words Of Maggie Simpson ?,Elizabeth Taylor,General,Something navicular is shaped like what,A Boat,General,What is ambergis used in the making of,Perfume +Music,Name The Singer Whose Body Was Found In A Hollywood Hotel Room On 4th October 1970 Eighteen Hours After She Had Died,Janis Joplin,General,Which modern city was once called Byzantium,Istanbul,General,Francesco Seraglio invented what in Australia in early 1960s,The Woolmark logo +General,What do you get by mixing gin & vermouth,Martini,General,Brave Belt was the original name of what group,Bachman Turner Overdrive,General,"Wheeled vehicle, specifically, a vehicle for carrying persons, designed to be drawn by one or more draft animals",Carriage +General,What does Dorethy have to steal from the wicked witch in oz,Her Broomstick,General,Which liqueur is added to Whisky to make a rusty nail cocktail?,Drambuie,Food & Drink,From Which Fish Do You Obtain Cavia ,The Sturgeon  + Geography,What is the name of the famous large coral reef located off the coast of northeastern Australia?,Great Barrier Reef,General,Bibliophobia is a fear of _____.,Books,General,Roger Bannister ran the first sub 4 minute mile who ran 2nd,John Landie of New Zealand +Music,What do the initials FRAM stand for?,Fellow Of The Royal Academy Of Music,General,In what country would you dance The Sirtaki,Greece, History & Holidays,What were the earliest tree ornaments? ,Apples & Fruit  +Art & Literature,How many plays is Shakespeare generally credited with today,37,General,What is a male sheep,Ram,General,"Indiana Jones: What company handled the ""special effects""",Industrial light + Geography,This island group is off the east coast of southern South America.,Falkland Islands,General,What plane did Aerospatiale of France & the British Aircraft Corp. develop,The concord concord,Sports & Leisure,What colour signifies a difficult slope in skiing? ,Black  +Music,Name One Of The 3 Major record Companies That Simultaneously Launched The Compact Disc In March 1983,"Sony, Phillips, Polygram",General,"Where in the solar system would an observer see the ""Cassini's Division""",Rings of saturn,General,What is the nickname for Maryland,Free state + Geography,What is the capital of Afghanistan ?,Kabul, History & Holidays,Which London building was gutted by fire in November 1936? ,The Crystal Palace ,General,Which city was superman born in,Kryptonopolis +General,How many times has the character Ross from the show friends been married?,Twice,General,What is the latin word 'trivia' in english,A junction of three roads,Science & Nature,Which metal is the best conductor of electricity? ,Silver  +General,What is the world's fastest passenger aircraft,Concorde,General,The Algonquin Indians believed the earth was on what,Giant tortoise,General,Illustrator Florence K Upton created what (now not PC),Golliwog +General,What female name comes from the Greek for foreign woman,Barbara,General,In the Bolshoi ballet what does the word Bolshoi mean,Big,General,What's the oed,Oxford english dictionary + Geography,In which ocean or sea are the Seychelles?,Indian Ocean,Science & Nature,What Base Are Hexadecimals ,16 ,Science & Nature,In Computing what do the initials Jpeg stand for ,Joint Photographic Experts Group  +General,What is a group of this animal called: Rhino,Crash herd,General,"Catlike leap in which one foot follows the other into the air, knees bent; the landing is in the fifth position.",Pas de chat,General,Whose biography is entitled 'lady sings the blues',Billie holliday +General,Which song composed by Sir Arthur Sullivan in sorrow over his brother's death became the most popular ballad of the nineteenth century,The lost chord, History & Holidays,England Opened Its First Nudist Beach In 1979 At Which Seaside Resort ,Brighton ,Geography,Which port of Sicily shares its name with a city in New York State? ,Syracuse  +Music,What Was The First Number One Album For Oasis?,Definately Maybe,General,Pomona was the Roman Goddess of what,Fruit Trees,General,Vampire bats prefer to bite what part of a sleeping person,Toe +General,Who invented painting by numbers,Palmer paint company,Music,"Who Wrote The Theme Song For The James Bond Movie ""Live & Let Die""",Paul McCartney,General,Carson who succeeded joseph stalin as russian premier,Georgy malenkov +General,What was the name of Dr Dolittle’s Parrot,Polynesia,Music,"Janis Joplin Died In 1970, How Did She Die",From A Heroin Overdose,General,Baseball: the chicago ______,Cubs + History & Holidays,Where did Guinevere retire to die?,Amesbury,General,Menace What Disney film do Parisians know as Blanche Neige Aet les Sept Nains,Snow,General,Myxophobia is the fear of,Slime +General,What was The Liberty Bell manufactured 1900s Charles Fey,Fruit Machine,Science & Nature, A __________ can squeeze through an opening no larger than a dime.,Rat,General,What colour is Queen Elizabeth's blotting paper,Black +General,"In Greek mythology, into what did athena turn arachne",Spider,Food & Drink,Which Foodstuff Links Gary Lineker and The Spice Girls? ,Walker 's Crisps ,General,When was the laser invented,1960 +General,What is a group of this animal called: Cat,Clowder clutter,General,What is a young zebra called,Colt,Geography,What Is The Unit Of Cuurrency In India ,The Rupee  +General,If you take a before meal aperitif what's an after meal one called,Digestif,General,Who recorded the original song 'the twist',The beatles,General,What Sign Of The Zodiac Is Represented By A Centaur,Sagittarius +General,Candy bar promoted by Bart Simpson.,Butterfingers,General,"The 1st U.S. federal holiday honoring Martin Luther King, jr. Was in what year",1986, History & Holidays,Which great ocean liner was launched in 1934? ,The Queen Mary  +General,What's the unit of measure for a racehorse's height,Hand,Geography,He invented the most common projection for world maps.,Mercator,General,Who was the wife of Moses?,Zipporah +General,Who hated mozart with a deadly passion,Salieri,General,What is the name of the body part that separates the abdomen from the thorax?,Diaphragm,General,What was invented by Ludovic Zamenhof ?,Esperanto + History & Holidays,"In Scotland, what vegetable was traditionally carved into a jack-o-lantern was it A Potato B Turnip C Pumpkin ",B = Turnip ,General,"Etna, Hekla and Popocate a petl are all types of what",Volcanoes,General,"Paul Revere was a silversmith, copper engraver and what",Dentist + History & Holidays,Which Singer Born On Christmas Day Released A Debut ASlbum Entitled No Angel ,Dido ,General,Who won the 2000 Booker prize,Margaret atwood,Science & Nature,What colour is a robin's egg?,Blue +General,Muckle John was the last official royal one in England - what,Fool or Jester,Art & Literature,Which Author Did Hitler Acclaim As The Phrophet Of Right Wing Authoritarianism ,Nietzsche ,Science & Nature,What Would You Be Most Likely To Be Chewing If It Were Flavoured With Mentha Piperita Or Mentha Viridris ,"Chewing Gum , Peppermint, Spearment " +General,Who had a hit with the song Loco-Motion,Little Eva,General,What is the name of Porky Pigs father,Phineas Pig,Music,"Who Did The Avalon Boys Accompany On The 1976 Hit ""The Trail Of The Lonesome Pine""",Laurel & Hardy +General,Germany's equivalent to the dollar is the ______?,Deutsche mark,Science & Nature,What does a camel store in its hump?,Fat,General,Sinbad faced this arabian mythical bird of prey name the bird,Roc +General,How much jelly fills a proper dunkin donuts' munchkin,One half ounce 1/2 ounce,Science & Nature,Name the largest web-footed bird.,Albatross,General,Who wrote The Last Frontier first published in 1959,Alistair Maclean +General,Whose face is on the front of a quaker oat box,William penn,Music,"Who Reached No.1 In 1962 With ""I Remember You""",Frank Ifield,General,Maracaibo is the second largest city in which South American country,Venezuela +General,Who wrote 'valley of the dolls',Jacqueline susann,General,"In The TV Show ""The Young Ones"" What Was The Name Of Viviens Pet Hamster",SPG (Special Patrol Group),General,Which musical stage show ( and film ) uses tunes by Borodin,Kismet +General,What were the russian atrocities against the jews called,Pogroms,Science & Nature,What do you call a group of rhinocerus?,A Crash,General,"Which philosopher's last words, were: ""Go on, get out! Last words are for fools who haven't said enough.""",Karl marx +General,"Which film star was described as ""A vacuum with nipples""",Marilyn Munroe,General,What is a hajji,A pilgrim to mecca,Science & Nature,In Which American State Is The Dinosaur Valley Museum ,Colorado  +General,In which modern country is the site of the Battle of Balaklava,Ukraine,General,What is the flower that stands for: revenge,Birdsfoot trefoil,General,The Romans called it Mamcunium what is this English city,Manchester +General,How were the bodies of dead crusaders brought home for christian burial,Chopped up and boiled,General,"What are ""the four nightingales"" better known as",The marx brothers,General,On which day of the year would you 'firstfoot',January 1st +Religion & Mythology,Who is the Linux operating system named after,Linus torvalds,Science & Nature,How Did Nylon Get It's Name ,Developed In New York & London ,General,Which man's name means Bearer of Christ,Christopher +Entertainment,Who is Reginald Dwight known as?,Elton John,Music,Who Duetted With Celine Dion On The 1997 Hit Tell Him,Barbara Streisand,General,What was the last name of the american that invented root beer soft drink,Hires +General,Which is the closest town to Ayres Rock,Alice springs,General,Paul French George E Dale pseudonyms for what SF author,Isaac Asimov,General,What is the proper name for 'mad cow disease',Bovine spongiform +General,What did the massachusetts bay colony outlaw in 1641,Lawyers,General,What happened on screen for the first time in india in 1977,Screen kiss,General,"What small fish, often tinned, was named after a Mediterranean island",Sardine + Geography,What is the capital of Djibouti?,Djibouti,Science & Nature,What are the units of measurement for Frequency ?,Hertz, History & Holidays,What was the first fighting vehicle?,War chariot + History & Holidays,Who was the first person to swim the English Channel?,Captain Matthew Webb,General,Who returned to Britain in 2001 after 35 years on the run?,Ronnie Biggs,Food & Drink,How Many 75cl Bottles Of Champagne Would You Get From A Jeroboam ,4 Bottles  +General,What are bloaters a type of,Fish,Geography,What is the capital of Morocco,Rabat,General,What did aristotle believe the heart was,Seat of intelligence +General,What Shakespeare play was the basis of The Forbidden Planet,The Tempest,General,What does a panaphobe fear,Everything,General,A word like 'NASA' formed from the initials of other words is a(n) ______.,Acronym +General,What sort of animal is donkey kong of video game fame,Gorilla,General,The artist Abbott Thayer's developed what for military use,Camouflage colours,General,"The understanding of speech through observation of the lips, tongue and facial expressions is known as ______",Lip reading + History & Holidays,What was the name of the vehicle in which Sir Malcom Campbell broke the 300 mph barrier in 1935? ,Bluebird ,General,"QANTAS, the name of the airline, is an acronym for___",Queensland and northern territory aerial services,General,As what was Lincoln Park in chicago originally used,Cemetary +General,Which London's church's other name is the Collegiate Church of St. Peter,Westminster abbey,Science & Nature,What Killed Cleopatra ,An Asp ,General,The theme from Top Gun was a hit for which European sounding group,Berlin +Geography,In Which Indian City Is The Taj Mahal ,Agra ,General,In 1901 who first transmitted radio signals across Atlantic,Marconi,General,What are the moufflon & bighorn,Sheep +General,The _____ Revolution began in 1905 on 'Bloody Sunday' when troops fired at demonstrators,Russian,General,What Is The Oldest Inhabited City In The World,"Damascus, Syria",General,What religion follows the teachings of the prophet Mohammed,Islam +Music,Who Warned Us About Dirty Diana,Micheal Jackson,General,Harvard college was founded in this year,1636,General,What part of your body can have a gastric ulcer,Your stomach +General,What is the real name of Tony Curtis,Bernard schwartz,General,To what language is the gypsy romany language related,Sanskrit, History & Holidays,In Which Series Of Films From 1984 Onwards Did Robert Englund Star As Freddy Krueger ,A Nightmare On Elm Street  +General,What is the fear of gravity,Barophobia, History & Holidays,*What 70s television show launched the career of David Cassidy?* ,The Partridge Family ,Science & Nature,What is the heaviest element that can be formed by regular fusion reactions in the core of a star?,Iron +General,What do the five olympic rings represent,Continents,General,What two time all big eight defensive back at Colorado won three U.S. Open golf titles,Hale irwin,Science & Nature," Wolf packs could be found in all the forests of Europe, and in 1420 and 1438, wolves roamed the streets of __________",Paris + History & Holidays,Who was given the only Nobel Peace Prize award during WWI?,International Red Cross,General,In what state was the largest cavalry battle in the civil war,Virginia Brandy Station (20000 men),General,Which country is widely acknowledged to have the largest Jewish population,United states + Geography,What city is the Christian Science Monitor based in?,Boston,General,"In which U.S. state is Quantico, home to an FBI training and research facility and a similar one for the Marine Corps",Maryland,General,What is the Capital of: Yemen,Sanaa + History & Holidays,Which 1950s films took place in Burma 1943 ,The Bridge on the River Kwai ,General,On Which Fictitious Island Does Thomas The Tank Engine Operate,Sodor, History & Holidays,Which Magician Was An Advisor To King Arthur ,Merlin  +Food & Drink,Lack of Vitamin D causes which disease?,Rickets,Music,Why Did The Jackson 5 Change Their Name To The Jacksons When They Left Motown,Motown Owned The Name The Jackson 5,General,What's the most common term of endearment in the U.S.,Honey +General,Whats the nearest galaxy to our own,Andromeda,General,The Ngorogoro crater is in what Tanzanian National Park,Serengeti,General,Pampas grass is a native plant of which continent,South america +Geography,To The Nearest 100 Million Years How Old Is The Earth ,"4,540 Million Years ",General,Branch of science that applies physical principles to the study of the earth,Geophysics,Music,"Which British Pop Star Had A Hit With The Song "" Anyone Who Had A Heart """,Cilla Black +Geography,What is the capital of Central African Republic,Bangui,General,18th century French dance,Gavotte,General,A web designer might use CGI scripts what does CGI stand for,Common Gateway Interface +General,In The Muppet Movie who sang for Miss Piggy,Johnny Mathis,General,Which film won the best screenplay Oscar in 1970,MASH,Entertainment,What was the name of Barney and Betty Rubble's son?,Bam Bam +General,Which European airport has the international code LIS,Lisbon,General,Tuberculophobia is the fear of ______,Tuberculosis,Entertainment,Which country and western singer is known as the 'okie from muskogee'?,Merle Haggard +General,Who shot J.R. Ewing?,Kristin,General,Which animal sleeps on its back,Only man or women,General,What Country Smokes The Largest Amount Of Cigarettes Worldwide,China +General,What river flows through 8 countries and four capitols,Danube, History & Holidays,Which Lancs Town Has A Name That Means A Small Brass Wind Instrument Used For Summoning Witches & Warlocks,Oswaldtwistle ,General,How many episodes were there in the original Star Trek series?,75 + History & Holidays,Which Band Were At The UK Christmas Number One Spot In December 1979 ,Pink Floyd / Wall ,General,"Who was the first U.S. president to travel in a car, plane and submarine",Theodore roosevelt,Music,Which Famous Rapper Was Born “Stanley Kirk Burrel” in 1962?,MC Hammer +General,Derived from Greek what does alias literally mean,Otherwise,General,"What larry niven character was known as the ""hindmost leader""",Nessus,General,Where is the district 'elephant and castle',London + Geography,What is the basic unit of currency for Turkey ?,Lira,Sports & Leisure,What Is The Sign Language Used By Bookies Known As ,Tic Tac ,General,Which was the first rock group to use lasers in a live performance?,The Who +General,What's the name of Disney's Little Mermaid,Aerial,People & Places,Who Invented The Electric Light Bulb ,Thomas Edison ,General,What is the characters name of Agent 86,Maxwell Smart +General,Who piloted the first flight across the English channel,Louis Bleriot,Food & Drink,Which Famous Wine Region Is Centred On Reims ,Champagne ,Music,Which Singer Had A Blonde Ambition Tour In 1990,Madonna +Food & Drink,In which country did Scampi Originate? ,Italy ,Music,"From the 1980’s, which song and which artist “Her weapons were her crystal eyes, Making every man a man, Black as the dark night she was, Got what no-one else had, Wa!”?",Bananarama / Venus,General,On TV P.C. McGarry Is The Local Bobby Of Which Village,Camberwick Green +General,Who was the father of modern bullfighting,Juan belmonte,General,"In The World Of Music How Is ""James Jewel Osterberg"" Better Know",Iggy Pop,Music,Which Fair Was The Subject Of A Simon & Garfunkel Song,Scarborough O Fair +Science & Nature,What is measured by Moh's Scale? ,Hardness ,General,Apparatus that converts molecules into ions and then separates the ions according to their mass-to-charge ratio,Mass Spectrometer,General,"Who wrote ""Dracula""",Bram stoker +Sports & Leisure,How many points are awarded to the winner of a Formula One Grand Prix ,Ten Points ,Science & Nature,Which is the only animal other than humans that can get leprosy?,Armadillos,General,What is the fear of germs known as,Spermophobia +General,Who wrote the Robocomm computer program,Dan parsons,General,In heraldry a lybbard is a lion panther cross symbolising what,Wildness,Music,Who Wrote The Music For The Ballets Swan Lake & Sleeping Beauty,Tchaikovsky +General,For which Olympic athletic event is there no official world record,Marathon same distance diff courses,Music,Sad Movies Norman & Some People Were All Hits For Which Early 60's Singer,Carol Deene,Music,"Which Early 80's Group Sang ""You're Lying "" And ""Intuition""",Linx +General,What chain did ray kroc build,Mcdonald's,General,What is the Capital of: Ukraine,Kiev,General,"Air is 21% oxygen, 78% ______, and 1% other gases",Nitrogen +General,Which British city had the first pavements (sidewalks) in 1688,Edinburgh in High St and Cowgate,General,"In norse mythology, who is brunhilda chief of",Valkyries,General,The Flavian Amphitheatre is better known as what,Colosseum in Rome +General,"During The Beatles Run Of 7 Consecutive No.1 Singles, What Was The Name Of The Song That Failed To Make It 8 In A Row For Them?",Paperback Writer,General,In the tv series 'happy days' what was arthur fonzarelli's nickname,The fonz,General,"Hammer, anvil and stirrup are parts of ______",Ear +General,Mageiricophobia is the fear of what,Cooking,General,How many facets has a snowflake,Six,Entertainment,"Who sang 'Forever and Ever, Amen'?",Randy Travis +General,What country's people developed the crossbow,China,General,Harry Longbaugh became better known under what name,The Sundance Kid,General,Gold Discs Platinum Discs but who won first Rhodium Disc,Paul McCartney +General,To make all the text align against the left or right margins is called,Justification,Science & Nature,What Is The Most Common Colour Of A Great Dane? ,Fawn ,General,Where was steel first made,India +General,What do the Chinese regard as the highest form of visual art,Calligraphy,Technology & Video Games,Manjimaru is the main character of which game? ,Kabuki Klash,General,Who Did Steve Davis Defeat In His First Ever World Snooker Final In 1981,Doug Mountjoy +General,In Ancient Rome what creature was the symbol of liberty,The Cat, History & Holidays,What is the traditional dessert in England on December 25? ,Christmas Pudding ,General,What is the oldest swimming stroke,Breaststroke 16th century +General,Who founded the miss world competition,Eric morley,General,People answered the first telephones by saying what,Ahoy there,General,February 25th 1990 what was banned in the US,Smoking on all domestic flights +Entertainment,"She starred in the 1952 film, ""Niagara"".",Marilyn Monroe,General,What is a group of this animal called: Boar,Sounder,General,Which Country Owns Easter Island ?,Chile +General,"In 'a christmas carol', what was the name of the miser",Scrooge,General,In Korea what is ssirum,Sumo Wrestling,General,Eliza Doolittle is a character in which George Bernard Shaw play,Pygmalion +General,What's the longest river in Asia,Yangtze,Sports & Leisure,Name Both The Start / Finish Points For The University Boat Race ,Putney & Mortlake ,Science & Nature,The violet variety of quartz is called ________.,Amethyst +General,What volcanic peak is visible from naples,Mount vesuvius,General,What phrase is another way of saying covered in bruises,Black and blue,General,Agatha Christie's elderly female crimesolver,Miss marple +General,De'cappo means what in music,Repeat from start,General,The Snake River lies entirely in which country?,USA,General,Cum Granlo Salis is Latin for what phrase,With a grain of salt +General,Fromage is french for,Cheese,General,The araucaria has what more common name,The Monkey Puzzle tree,General,What European countries national anthem has no official words,Spain + History & Holidays,"""What Did My True Love Give To Me On The """"Twelth"""" Day Of Christmas"" ",12 Drummers Drumming ,General,What is the value of pi to three decimal places?,3.142,General,Triangular part of wall at the end of a ridged roof,Gable +Music,"Who had a 1976 hit with ""Young Hearts Run Free""?",Candi Staton,General,Vanilla flavouring is derived from which flower,Orchid,Music,"Which Member Of The Monkees Went On To Front ""The First National Band""",Mike Nesmith +General,The quetzal is the currency of ______?,Guatemala,Music,Who Did The New Yardbirds Become,Led Zeppelin,Music,Whose Real Name Is Roberta Anderson,Joni Mitchell +General,What category of Oscar was the only award for the film Star Wars,Sound track,General,On what is the Mona Lisa painted,Wood,General,What county is chicago,Cook county +General,What is enuresis,Bed-wetting,General,Name the cartoon that featured characters who had messages on their clothes to express their feelings.,Shirt Tales,General,Who was the egyptian god of war and divine vengeance,Sakhmet +General,What piece of land did France sell to America in 1803,Louisiana,General,The Beverley Hillbillies came from what Ozarks town,Hooterville,General,How was crystal palace destroyed,Fire +General,Which sporting event was won 5 times by Eddy Merckx,Tour de france,General,In Greek mythology who was Queen of the underworld,Persephone, History & Holidays,Which 50's Movie features the Line ' This is war. This is not a game of cricket'. ,The Bridge on the River Kwai  +General,What was casey jones's real name,John luther jones, History & Holidays,In which century was (The Black Death)? ,Fourteenth ,Sports & Leisure,Who Did Tiger Woods Replace As The World's Number One Ranked Golfer In 1998? ,Greg Norman  + History & Holidays,Which former Labour MP formed the New Party in 1931? ,Sir Oswald Mosley ,General,What is a group of this animal called: Sheep,Drove flock, History & Holidays,What 'SK' Do You Call A Quiet Armour Wearer ,Silent Knight  +General,What is the Capital of: Svalbard,Longyearbyen,Science & Nature,Prosthetics deals with the making of __________.,Artificial limbs,General,What is the only letter of the English alphabet with three syllables (no hints please),W +General,At what seaside resort did Britain’s first legal casino open on the 2nd June 1962?,Brighton,General,English ships carried limes protect scurvy what US ships carry,Cranberries,General,Who became king of Saudi Arabia in 1982?,King Fahd +General,Whose home was mount vernon,George washington,Music,What is Paul's middle name?,Paul,General,Juliet binoche won an academy award for best supporting actress in which film,The english patient +Food & Drink,What is the other ingredient in a pink gin? ,Angostora Bitters ,General,What happened to Laika first dog in space,Suffocated no air burned re-entry,General,Candlestick maker what is the capital of iran,Teheran +Music,Gerard Hugh Sayer Is Better Known As Whom,Leo Sayer,General,Paris born Philippe Pages changed his name to what,Richard Clayderman,General,What is the state bird of Alabama,Yellowhammer +General,"Who Is The Only Person To Hold The Post Office Of Prime Minister, Home Secretary, Foreign Secretary And Chancellor",James Callaghan,General,"What was first successfully tested in Alamogordo, New Mexico?",Atomic bomb, History & Holidays,From which country does the poinsettia plant originate? ,Mexico  +Science & Nature,Where Did Wilbur Wright Demonstrate Flight In Public For The First Time ,Northern France ,General,Vancover Island & British Columbia became one colony in,1866,Science & Nature,"Featured In The Film Jurassic Park, Which Creatures Name Means 'Quick Plunderer' ",Velociraptor  +Science & Nature,What Colour Is The Flower Of The Amazon Lily When It Opens ,White ,General,When does a full moon rise,Sunset,General,What is a dhoti,An Indian male loincloth +General,Which two countries' national anthem does not mention the country's name,United states and holland, History & Holidays,Which Sci-Fi Sitcom star like to eat cats? ,Alf ,Sports & Leisure,Jockey Lester Piggot Once Served 3 Years In Prison For What Crime ,Tax Evasion  +General,In which country would you find the towns of Lausanne and Locarno,Switzerland,General,What do ornithologists study,Birds,General,Who recorded as Dib Cochran and the Earwigs,Marc Bolan and David Bowie +General,Alphonso D'Abruzzo became famous as who,Alan Alda,General,A primate called a Galago has what more common name,Bush Baby,General,"In 1960, ray charles released the hit ""i can't stop _____""",Loving you +General,Who wrote the three act opera The Rakes Progress,Igor Stravinsky,General,Janis joplin started her career with which group backing her up,Big brother,Music,"What Is The Rest Of This James Brown Song Title ""Say It Loud_________""",I'm Black & I'm Proud +General,For which decoration do the letters C.G.M. stand,Conspicuous gallantry medal,General,Name the first ship from which Gulliver was shipwrecked in 'Gulliver's Travels',Antelope,General,Hans Christian Anderson had what job before writing,Actor +Sports & Leisure,With what sport is Gabriela Sabatini associated?,Tennis,General,"In 'dawson's creek', who does james van der beek play",Dawson leary,General,In which American State was the first atomic bomb test carried out in July 1945,New mexico +General,What's unusual about evangelist Amy Semple McPhersons coffin,Contains Telephone,General,What is The Southern most Point Of The English Mainland?,Lizard Point / The Lizard,General,Who wrote the music for Showboat,Jerome Kern +General,Basketball: the Chicago ______,Bulls,General,What was the name of the world's tallest man who still remains firmly entrenched in the Guinness Book of Records some 70 years plus after his death?,Robert Pershing Wadlow,General,Whom did Michael Portillo succeed as M.P. for Kensington and Chelsea in November 1999,Alan clark +General,What type of aircraft did the red baron fly during World War I,Fokker,General,What animal head appears on the badge of the RCMP,Bison,General,What year did Roger Maris hit 61 homeruns,1961 +General,What is another name for a tombstone inscription?,Epitaph,Science & Nature,How Many Colours Are The In A Rainbow ,7 ,General,What actor links Von Ryan's Express - Magnificent Seven,Brad Dexter +General,What is the active ingredient in oxy anti pimple cream,Benzoyl peroxide, Geography,Into what sea does the Mackenzie River flow?,Beaufort Sea,General,Nacre is more commonly known as what,Mother of Pearl +General,Who built the hurricane aircraft,Hawker,General,"In the 'twelve days of christmas', how many items in total are sent by 'my true love'",78,General,Which Band Had The Last UK Number One Hit Of The Millenium,Westlife +General,What does a carpophagus animal feed on,Fruit,General,"What boys' toy was introduced at the annual american international toy fair in new york on feb. 9, 1964",G.i joe,Sports & Leisure,What Is The Bed Of A Snooker Table Made From? ,Slate  +General,Narrow strip of cloth attached to a string round waist for covering genitals,G-string, History & Holidays,"Which 1978 film starring Roger Moore, Richard Burton and Richard Harris, is a bout a group of British mercenaries in Central Africa ",The Wild Geese ,General,Which composers third symphony is nicknamed the Polish,Pytor Illyich Tchaikovsky +Sports & Leisure,Who had the nickname 'Golden Bear'?,Jack Nicklaus,Art & Literature,Which Woman Had More Portarits Painted Of Her Thaan Anyone Else ,Queen Elizabeth II ,General,Where would you find line of Mars - Girdle of Venus,Palm - lines in Palmistry +General,In which novel does the character Quebec Bagnet appear,Bleak House,General,In which film did Elvis Presley sing the song 'Wooden Heart',G.i. blues,General,On 'southpark' what usually happens to kenny,He gets killed +Science & Nature,What Do You Use To Tell A Horses Age? ,Check Its Teeth ,General,By what name is Salicylic Acid better known as?,Aspirin,General,What was the first safety feature patented for an automobile and attached to a front fender,Safety net +General,A person learning a trade by working for an agreed period,Apprentice,General,"What creature can be Indian, white or broad lipped",Rhinoceros,General,Who invented false eyelashes,D.w griffith +General,"Rod, chain and furlong are all units of what",Length,General,Branch of mathematics that deals with the properties & relationships of numbers (number),Number theory,General,In what novel does Dr Hannibal Lecter first appear,Red Dragon +General,Which two fighting ships other than the 'utah' were sunk at pearl harbor,Arizona and oklahoma,General,What is the lowest title handed down from father to son,Baronet,General,"In 1927, the first words spoken in a film were ""Wait a minute, wait a minute!"" Which five words followed",You ain't heard nothing yet +General,Who wrote the novel Love Story (Both Names),Erich Segal,Music,"Cicane's 2000 No.1 Hit ""Dont Give Up"" Featured Which Rock Megastar",Bryan Adams,General,Who composed the Overture to the Wasps,Vaughan williams +General,Who was the Russian Foreign Minister from 1957 to 1985,Gromyko,People & Places,Which Former Python Traavlled Round The World ,Micheal Palin ,General,What's the Eskimo word for a moccasin type boot,Mukluk +Music,What Was The Title Of The UK No.1 Single Released By Enya In 1988,Orinocco Flow,General,Who Was The Very First Recipient Of The MALE Version Of The Rear Of The Year Award,Michael Barrymore,General,In South Carolina what's barred Fountain Inn without wear pants,Horses +Music,Who Died In The Same Air Crash As Cowboy Copas & Hawkshaw Hawkins,Patsy Cline,General,Who is the greek counterpart of mercury,Hermes,General,Who composed The Planets suit (both names),Gustav Holst +General,What was killed by the first bomb dropped by the allies on berlin during wwii,Elephant,Geography,What is the capital of Turkmenistan,Ashkhabad,General,Who followed up beetlejuice with clean and sober,Michael keaton +Geography,What island has Hamilton as its capital,Bermuda,Food & Drink,"Ireland, the UK and Turkey rank first, second and third in the world in the consumption of what? ",Tea ,General,What is a Lampyris Noctiluca better known as,A Glow-Worm ( European ) +General,Walter Wagers novel 58 minutes was the basis for which film,Die Hard 2,General,What is the second largest country in south america,Argentina,General,Who is married to eddie van halen,Valerie bertanelli +General,What city was originally called edo,Tokyo, History & Holidays,Which county cricket ground can be found at St Johns Wood in London ,Lords ,Science & Nature,What are the units of measurement for Energy ?,Joule +General,"Eastwood what are cocci, spirilla and streptococci",Bacteria,General,When is a crepuscular animal active,Twilight,General,Handel's Harmonious Blacksmith is played on what instrument,Harpsichord +General,Gary Boker Bobby Harrison Ray Rodger were in what pop group,Procul Harem,Sports & Leisure,What Did Eddie The Edwards Had To Have 2 Inches Taken Off His What When He Represented England At The 1988 Winter Olympics ,His Chin ,General,To what family does the hippopotamus belong,Pig +Geography,What is the capital of Israel,Jerusalem,Sports & Leisure,Which football team lost both the 1998 and 1999 FA Cup Finals? ,Newcastle United ,General,What is the longest river in Scotland,Tay +General,What do we call the study of fossils,Palaeontology,General,In which novel is Phoebe Caulfield the hero's younger sister,Catcher in the rye,General,"What palace was build by the sun king ,louis xiv in the previous century",Versailles +Science & Nature,The fins of which fish are made into a soup,Shark,General,A Vigule or Solidus is what character,Slash / - not backslash,Music,"Which Record Company Had It's First US Hit With Carl Perkins's ""Blue Suede Shoes""",Sun Records + Geography,Which is the Earth's fourth largest continent ?,South America,Food & Drink,What pastry is used to make Profittaroles ,Choux ,General,What is the highest mountain in North America,Mount mckinley +General,Which animal lives in a holt,Otter,General,What is a sun dried grape,Raisin,General,Which country is ruled by two Captains Regent,San marino +Music,Which song did Don McLean write about the death of Buddy Holly,American Pie,General,Rhett Butler of Gone With the Wind was born where,Charleston,General,What nursery rhyme character was arachnaphobic,Little miss muffet +Music,"Which song begins, “Heaven. I’m in Heaven”?",Cheek To Cheek,General,Where would one eat a taco,Mexico,Sports & Leisure,Name The 4 Stations In Monopoly ,"Fenchurch Street, Liverpool Street, Kings Cross, Marylebone " +General,"Which nation, on average, takes the longest time over its meals",France,General,"Who was the famous individual who originated the catch phrase ""Just Say No""",Nancy Reagan,General,Breakers footrace what does the irish 'dubh linn' mean,Blackpool + History & Holidays,A mysterious cloud of pesticide and radiation causes a disturbing reversal for Scott Carey in which classic 50s film ? ,The Incredible Shrinking Man ,Sports & Leisure,What Name Is Given To The Pitch On Which American Football Is Played ,The Gridiron ,General,Who wrote the poem 'Ozymandias'?,Percy Bysshe Shelley +General,Any sufficiently advanced technology indistinguishable from magic,Arthur C Clark – Report planet 3,General,In liquid measure a pipe is made up of two what,Hogsheads,General,There are 300 distinct different types of what food,Honey +Sports & Leisure,"In which game or sport is a ""Zamboni"" used",Hockey,Science & Nature,"Owing to the sound it makes, what is the alternative name for the Australian bird the Kookaburra? ",The Laughing Jackass ,General,"Which South American Country Has The ""Balboa"" As It's Currency",Panama +Science & Nature,What Is Trinitoluene Better Known As ,TNT ,General,What peter sellers movie did ringo starr appear in,Magic christian,Art & Literature,Edgar Allen Poe wrote a famous poem about his animal.,Raven +General,Which country occupies the horn of africa,Somalia,General,What was the title of the novel which won the 1998 Booker prize,Amsterdam,General,Name the first king to ever make an official visit to communist China,Juan +General,Who refereed the 1876 Sharky Fitzsimmons fight with a gun,Wyatt Earp,Entertainment,In which James Bond film does the villain cheat at golf?,Goldfinger,General,What was unique about all the mens foil winners 1952 Olympics,All Left Handed +People & Places,Which Comedian Is Maried To Caroline Quentin ,Paul Merton ,General,Who won best actress for her role in darling,Julie christie,General,What countries still have not signed a peace treaty for wwii,Russia and +General,Montpelier is the capital of what state,Vermont,General,Who played the scarecrow in the Wiz (all black wiz of oz),Michael Jackson,General,The is no known language without a word for this creature - what,Butterfly +General,What is a Godeminche,Penis dildo with balls, Geography,Vilnius is the capital of which country?,Lithuania,General,What sort of winds lie either side of the Doldrums,Trade +General,Who won the Superbowl in 1989,San Francisco 49 ers,General,What was the name of John Glens first orbiting craft,Friendship 7,Music,Which Queen album shares its name with a newspaper?,News Of The World +General,What is the fear of falling in love known as,Philophobia,General,"What are elementary particles originating in the sun and other stars, that continuously rain down on the earth",Cosmic rays,Music,According To The Lonny Donnegan Classic “The Cumberland Gap” How Many Miles Long Was The Cumberland Gap?,Fifteen +Music,Who Was The Front Man & Lead Vocalist For Dawn,Tony Orlando,General,What is the name of a device used to stem the flow of blood,Tourniquet,General,Which of the Wombles was named after a city in Russia?,Tomsk + Geography,In which city is the Colliseum located?,Rome,General,This country consumes more coca cola per capita than any other.,Iceland,Food & Drink,What colour is caffine?,White +General,The average Britain in their life consumes 1000 lb of what,Carrots,General,What is the currency of Venezuela?,Bolivar,General,What is the us city with the highest murder rate,Detroit +General,What is the Capital of: Equatorial Guinea,Malabo,General,Whos Woodstocks beagle buddy,Snoopy,Toys & Games,Which chess piece is usually valued as 5 points?,Rook +General,"In the film 'selena', who did jennifer lopez play",Selena,General,Who became a genius whenever he put on the fabulous kurwood derby,Bullwinkle,Science & Nature,Which Bird Was The Emblem Of Prussia ,The Eagle  +Music,"What Was The Cult Originally Known As ""Southern Death Cult"" Or ""Sudden Death""",Southern Death Cult,Entertainment,Who played the role of Richard Blaine in Casablanca?,Humphrey Bogart, History & Holidays,What is the collective name for a group of Angels ,A Host Of Angels  +Mathematics,What is the maximum number of integer degrees in an obtuse angle?,179,Art & Literature,Who wrote 'A Christmas Carol'?,Charles Dickens,Sports & Leisure,"What vehicles are involved in the ""Tour de France""?",Bicycles +General,What is the traditional theatrical greeting before a performance to wish 'good luck',Break a leg,Toys & Games,There are this many red_letter cubes in Perquackey.,3,General,Pentheraphobia is the fear of,Mother-in-law +Science & Nature, The __________ can travel up to 9 miles per hour.,Chicken,General,Which of Henry VIII Wives Was Elizabeth I Mother,Anne Boleyn,General,"Sirocco, mistral and chinook are all types of what",Winds +Sports & Leisure,Football: The Cincinnati _________.,Bengals,General,Sterling Holloway was original voice of which Disney character,Winnie the Pooh,Science & Nature,What has approximately 1/4 pound of salt in every gallon?,Seawater +General,In England what would you buy or get at a Mop Fair,Servants for Hire,General,"What shape is a crystal of table salt a cube, oval or sphere",Cube,General,"In the film Copycat , which entertainer played serial killer Daryll Lee",Harry connick jnr +General,What was the first grand slam tennis title won by steffi graf,French open,Entertainment,Which comic is drawn by Sam Keith?,The Maxx,General,Which is the most ancient walled city,Jericho +General,Who wrote Auld Lang Syne,Robert burns,Entertainment,Name Cathy's on again/off again boy friend,Irving,General,The German Opel family was known for the manufacture of what,Cars +Art & Literature,What Dance Is Associated With Vienna ,The Waltz ,Sports & Leisure,Which English Race Course Is Overlooked By Trundle Hill? ,Good Wood ,General,In what European country are the villages Vomitville and Fukking,Austria +General,In Greek mythology who was the husband of Helen of Troy,Menelaus,General,James Drury starred in which TV western series,The Virginian,General,"Other than pocahontas, which two women have been represented on u.s currency",Susan b anthony and martha washington +Science & Nature," Sheep will not drink from running water. Hence, the line in the Twenty_third Psalm: ""He leadeth me beside the __________",Still waters,General,Name the book - first line - It was a pleasure to burn,Fahrenheit 451 Ray Bradbury,General,Which group govern in a plutocracy,The most wealthy +General,Which fruit is the symbol of hospitality,Pineapple,General,What is the 'x' on a railroad crossing,Crossbuck, Geography,What is the capital of United Kingdom ?,London +General,Brand name was translated as Bite the wax tadpole in Russian,Coca Cola,Science & Nature,By what name is Lysergic acid diethylamide better known?,LSD,General,The British Mediterranean Fleet defeated Italian naval forces at which battle in May 1941,Cape matapan +General,From which station does the 'chattanooga choo choo leave,Pennsylvania,General,Who won the Tour de France 4 times 1961 to 1964,Jacques Anquetil,Music,"On Which Record Label Was ""Apache"" Released By The Shadows In 1960",EMI +General,Who invented the television?,John Logie Baird, History & Holidays,"""Which popular Christmas song was actually written for Thanksgiving? (White Christmas, Jingle Bells, Silent Night, Away In A Manger"""" "" ",Jingle Bells ,Science & Nature,What Was Joy Adamson's Lion Cub Called ,Elsa  +Entertainment,Who does the voice for Yoda in the Star Wars films?,Frank Oz,General,What are horseshoe crabs,Spiders and scorpions,General,From which alphabet do all western alphabets originate,Phoenician +General,How many peas grace the average pod,Eight,General,What musical direction comes from the Italian meaning cheerful,Allegro,General,"In the TV series ""The Beverley Hillbillies"", who played Jed Clampett",Buddy ebsen +Music,Name Three Mebers Of The Boy Band Five,"Scott Robinson, Rich Neville, Abs, Sean Conian, Jason Brown",General,Satanophobia is the fear of,Satan,Sports & Leisure,"What is the average viewing figure for The TV Show (A Question Of Sport) 7, 8, 9, or 10 Million ",8 Million  +Science & Nature,"In the 1940s George Gamow developed a theory that a giant explosion, 10-20,000 million years ago, began the expansion of the universe. What is the name given to this theory? ",The Big Band Theory ,General,What is the popular name for the drug 'drinamyl',Purple hearts,General,The arteries & veins surrounding the brain stem are called what,Circle of willis +General,"In terms of area, which is the largest city in Africa",Cairo, History & Holidays,What were men conscripted to work down mines in World War Two usually known as? ,Bevin Boys ,General,Woodwind Instrument size between Clarinet and Bassoon,Cor-anglais +Geography,What country has the world's most southerly city,Chile,Geography,Which rocky island group off the Northumberland coast is associated with Grace Darling and St Cuthbert ,The Farne Isles , Geography,What is the capital of Venezuela ?,Caracas +General,Aldeberan is the brightest star in which constellation,Taurus,General,The World's First Motorway Was Opened In 1929. In What Country Did This Take Place ?,Italy,General,What is a group of asses,Pace +General,Who is the Patron Saint of thieves,St Nicholas,General,Whats the worlds second most spoken language,English,Religion & Mythology,"Who was Ancient Egyptian goddess of fertility, love and beauty ?",Bastet +Music,Where In Mayfair Did A Nightingale Sing,Berkeley Square,General,What yellow fossilized resin was used in jewellery by the greeks and romans,Amber,General,What style of dancing was popularized with rap music?,Break Dancing +General,"In the field of entertainment, by what name is Emma Bunton better known",Baby spice,General,The average person has 1460 what each year,Dreams,Entertainment,"She played a Polish refugee in ""Sophie's Choice"".",Meryl Streep +General,In which country is the worlds longest road tunnel,Switzerland,General,What capital city does the liffey river flow through,Dublin,Science & Nature," While the bones of most airborne birds are hollow for lightness, __________ are endowed with solid bones for ballast when they dive, sometimes to 850 feet or more.",Penguins + History & Holidays,In which 1970's films does Dustin Hoffman play the character ' Babe Levy' ? ,Marathon Man ,Music,The Legend Of Xanadu Reached No.1 In 1968 Who Sang It,"Dave, Dee, Dozy , Mick & Tich", Geography,"Which country developed ""Tae-Kwan-Do""?",Korea +Music,"Whose Albums Included ""Notorious"" & ""Arena""",Duran Duran,General,"Who, in the Bible, was taken up to heaven in a fiery chariot",Elisha elias,General,"Who wrote the story of ""the nutcracker""",Eta hoffmann +Music,Which Of Marvin Gayes Singing Partners Died Of A Brain Tumour In 1970,Tammi Terrel,General,Edmonton is the capital of which province,Alberta, Geography,Yaounde is the capital of ______?,Cameroon +General,What does basic stand for,Beginner's all purpose symbolic instruction code,General,Who was the star of the film Bachelor Party?,Tom Hanks,General,Who hit a golf shot on the moon,Alan sheppard +Science & Nature,From What Was Aspirin Orginally Extracted ,Tree Bark , History & Holidays,What Is The Star Sign Of A Person Born On Christmas Day ,Capricorn ,General,The Rhine rises in which country,Switzerland +General,What do Beavis and Butt-head have on their T shirts,Metallica - AC/DC,General,Which middle eastern country's official name is Al-Lubnan,Lebanon,General,"Gibraltar Point, an area especially associated with wildlife is located south of which seaside resort?",Skegness +Music,Which Polish Composer Was Portrayed By Cornell Wilde In The Film A Song To Remember,Fryderyk Chop,General,Which of the Seven Wonders of the World was constructed by the sculptor Phidias about 430 B.C.,Statue of zeus, History & Holidays,"In 1951 Howard Hawks produced The Thing. Who directed the 1982 remake, which starred Kurt Russell ",John Carpenter  +General,"What candy bar was actually named after Grover Cleveland's baby daughter, Ruth",Baby ruth,Science & Nature,"What Was The Name Of The Java Born Dutch Aircraft Manufactuerer who, During World War I, Produced More Than 40 Types Of Airplanes For The German High Command ",Anthony Fokker ,Science & Nature," A male __________ that has been neutered is known as a ""wether.""",Goat +General,What was the first motion picture to have a synchronized musical score,Don,General,In Astrology what is the ruling planet of communication,Mercury,General,What are the more common terms for the maxilla and mandible,Upper and lower jaw +General,Why was turkey primarily barred from e.u entry,Human rights abuse,General,What is the provincial flower of Saskatchewan,Tiger lilly,General,Santiago is the capital of ______,Chile +General,What is the part of the nose that separates the two nostrils,Columella,General,In western palmistry the index finger is linked to which planet,Jupiter,Science & Nature,Who invented dynamite ?,Alfred Nobel +Sports & Leisure,Who Was The First Woman To Run A Mile In Under 5 Minutes ,Diane Leather ,General,What is the name of Beatrix Potter's fishing frog,Jeremy fisher,General,What is the fear of symbolism known as,Symbolophobia +General,Which comic is drawn by sam keith,The maxx,General,What waterway did Britain buy a share of in 1875,Suez canal, Geography,The Andaman Islands belong to which country?,India +Science & Nature,What is another term for a black leopard,Panther,General,What is the sacred river of hinduism,Ganges,General,Who was Jimmy Carter's Vice-President,Walter mondale +General,What is a group of this animal called: Elk,Gang,General,What event supposedly occurred in the Coenaculum or Cenacle,The Last Supper,Music,"Who Recorded The Album ""Reckless""",Bryan Adams +General,Who succeeded Georges Pompidou as President of France,Valery giscard d'estaing,General,"In a famous opera, what did siegfried understand after tasting dragon's blood",Bird's speech,Food & Drink,Which food crop does the Colorado beetle most often attack? ,Potato  +General,In Florida public singing is illegal if you are wearing what,Swimsuit, Geography,On what island is Honolulu?,Oahu,General,What word is given to a baby echidna?,Puggle +General,What is excessive enthusiasm or zeal for a cause,Fanaticism, Geography,What is the capital of Botswana ?,Gaborone,General,What did table tennis balls used to be made from,Cork +General,What future famous actors played Spicolli's sidekicks in Fast Times at Ridgemont High?,Eric Stolz and Anthony Edwards,General,"In the movie Office Space, what item did Peter pull from the rubble?",A red stapler,Science & Nature,In Which Year Was The Boeing 747 Put Into Regular Service ,1970  +General,Asian moon rat is the only animal smells like a veg which one,Onion,General,What is Kumiss made from in Asia,Fermented Cows Milk,Art & Literature,"This magazine features departments ""Picks & Pans"" and ""Chatter""",People +General,Which Russian word means openness,Glasnost,Science & Nature,Which Was The First International Airline To Launch A Sevice ,Klm (London To Amsterdam) ,Music,"On Which Label Was The Sex Pistols First Single ""Anarchy In The UK""",EMI +General,What is the escape velocity from the earth's gravity (mph),"25,000 mph",General,What is the fear of string known as,Linonophobia, History & Holidays,Little boy & fat man were the first ,Atomic bombs  +General,What is the Capital of: Israel,Jerusalem,General,Which song won first Oscar when the category was intro 1934,The Continental,General,"In the Bible, how is the Decalogue more commonly known",Ten commandments +General,Goddess sprang full grown from the forehead of her father Zeus,Athena,General,The average human produces how many quarts of spit in a lifetime,25000, Geography,What is the world's highest waterfall?,Angel Falls +Entertainment,Who is stationed at Camp Swampy in the comic strips?,Beetle Bailey,General,"Who composed the opera ""The Rake's Progress""",Stravinsky,General,What was the name of the first presidential aircraft,Sacred Cow +General,Find sources of light in the word spelled - psalm,Lamps,General,Who formed Microsoft with Bill Gates,Paul allen,General,"As who, was Lev Davidovitch Bronstein better known",Leon trotsky +General,How Is Thomas Mapother IV Better Known,Tom Cruise,General,What are the lime deposits hanging from the ceiling of a cave called,Stalactites, Geography,Kinshasa is the capital of ______?,Democratic Republic of the Congo +General,Where did Wolfe defeat the French under Montcalm,Quebec,General,What countries presidents Yusof Bin Ishak and Wee Kim Wee,Singapore,General,What sport do you throw bombs in,Football +General,Which mineral has the chemical formula FeS2,Iron pyrites,General,With what are toadstools often confused,Mushrooms,Science & Nature, All cows are females; the males are called __________,Bulls +General,"Who wrote, 'This is the way the world ends, not with a bang but with a whimper'",T s eliot,General,What is the concave dish shape of a liquid inside a glass or tube,Meniscus,General,What is a bone specialist,Osteopath +General,What instrument does an organ grinder play,Hurdy gurdy,General,Conventionally middle class materialist,Bourgeois,General,What does mel blanc's headstone say,That's all folks +Food & Drink,Agar-agar is often used in cooking. What is it? ,A type of gelatine ,General,What city is also known as beantown,Boston,General,In Which Country Was Cliff Richard Born,India +Entertainment,Who starred in the film 'The Man With Two Brains'?,Steve Martin,Science & Nature,"""Scotch pine"", ""Douglas fir"", ""Noble fir"", ""Fraser fir"" are all commonly used as what?",Christmas trees,General,What is the flower that stands for: affectation,Morning glory +Geography,What is the smallest independent state in the world,Vatican,General,Whose girlfriend was Virginia Hill - he killed her B Hills 1947,Bugsy Siegel,General,Small vegetable like a marrow,Courgette +General,What passenger train once ran between London and Edinburgh,Flying scotsman,General,What device converts alternating current into direct current,Rectifier,General,What girls name is a type of Australian throwing stick,Kylie +Music,What Was Mike Berry's Comeback Single Reaching No.9 In 1980,The Sunshine Of Your Smile,General,Gin Triple Sec (quantreau) and pineapple juice what cocktail,Aloha,General,Festival on January 6th commemorating the visit of the Magi to Christ,Epiphany +General,Which novelist gave their name to a slang word for 2000 pounds,Jeffrey archer,General,What is a low layer of cloud called,A nimbostratus,Geography,Which River Flows Through Paris ,The Seine  + History & Holidays,King Abdullah was assassinated in 1951. Of which country was he king? ,Jordan ,General,Brewers at what time did the hiroshima bomb detonate,8:15 AM,General, What does a brandophile collect,Cigar bands +General,What are the most commonly ordered item from sex catalogues,Novelty Condoms,General,Who had a number one hit in 1970 with In the Summertime,Mungo jerry,Music,Who recorded 'Blue Morning Blue Day' in 1978?,Foreigner +General,In 1957 which actress recorded 'tammy' from one of her films,Debbie reynolds,General,Tomography is a scanning technique which uses X-rays for photographing particular slices of the what,Body, History & Holidays,What was the name of the party dog that that was Budwiser's mascot in the late eighties? ,Spuds McKenzie  +Toys & Games,What is the tallest piece on a chessboard?,King,General,In Texas it's illegal for what profession to be communists,Pharmacists,General,Who recorded the album 'Hotel California',The eagles +General,"Greeks longest, Japans shortest and Saudi Arabia none what?",National Anthem,General,Americans use 16000 tons of what each year,Aspirin,General,With what is a diamond cut if it forms graphite dust,Laser +General,"What name is given to the blend of Black China and Darjeeling teas, flavoured with oil of Bergamot",Earl grey,Sports & Leisure,Which 2004 Darts Champion Is Known As The Viking? ,Andy Fordham ,General,"In which country are Cape Otway, Mount Gambier, and the Grampian Mountains",Australia +General,31% of people said what was the most disgusting personal habit,Spitting,General,What tennis star founded the magazine women's sports,Billie jean king,General,What is the Capital of: Kenya,Nairobi +General,What part of the body is most bitten by insects,The Foot,General,Lord Lovat was the last in England to do what,Be judicially beheaded, History & Holidays,"Complete the verse - 'Twas the night before christmas, when all through the house. Not a creature was stirring not even a ",Mouse  +General,Post boxes in the UK are red what colour are they in France,Yellow,Science & Nature,What is the symbol for silver,Ag,Music,"Who Released Their Debut ""The Prince"" On 2 Tone In 1979",Madness +General,What is the medical term for whooping cough,Pertussis,General,Someone with firmly fixed opinions is said to be dyed in what,The wool,General,Of what country is the monetary unit the rupee,India +General,70% people would stamp barefoot broken glass that watch what,Riverdance for 24 hours,General,Jimmy stewart is driven by revenge in the movie _____?,Man who shot liberty valence,General,Who said - Give us the tools and we will finish the job Feb 1941,Winston Churchill +Entertainment,"Which planet was the ""Planet of the Apes""?",Earth,General,"What completed a journey of 19,500 miles with only three stops in August 1929",The graf zeppelin,General,What does the distress signal SOS stand for?,Save our Souls +General,Steenburgen What sheriff claimed to be Walking Tall,Buford pusser,Science & Nature,What Is The Phenomenon Called In Which Light Bends When Passing Through A Lens ,Refraction ,Sports & Leisure,What is the regulation height for a pin in tenpin bowling? (in inches),Fifteen +General,Where did a shire have a reeve,England,General,What mythical scottish town appears for one day every 100 years,Brigadoon,General,How many apostles did Jesus choose,12 +General,"In the world of animals, where would you find a 'martingale'",On a horse,General,What was used for blood in the film 'psycho',Chocolate syrup,General,Bourbon Miss restaurant by law what must be served with water,One Small onion per glass +General,Who was the cause of the Trojan wars,Helen of troy,Geography,This country lost the largest percentage of its men in a single war (~70%).,Paraguay, History & Holidays,What historic event does the nursery rhyme (Ring-a-ring of roses) commemorate? ,The Great Plague  +General,In parts of Siberia wives threw what at men to show wanted sex,Worms or slugs,General,What colour is named after a battle fought in Italy in 1859,Magenta,General,What animal's penis is four feet long when erect,Giraffe +General,What was the name of the scandal that resulted in the resignation of president nixon,Watergate,General,"Who once told Playboy magazine he ""committed adultry in his heart""",Jimmy carter,General,Staurophobia is a fear of ______,Crosses +Music,"Which Famous Kenny Was ""Coward Of The County""",Kenny Rogers,General,"Any animal lacking a vertebral column, or backbone",Invertebrate,General,What's a microchip made of,Silicon + Geography,What is the capital of North Dakota?,Bismarck,General,What is the anatomical name for the bones of a human's fingers and toes,Phalanges,General,Who was the richest man to be vice president of the U.S.,Nelson rockefeller +General,In which 'james bond film does the villain cheat at golf,Goldfinger,General,Which country did Chiang Kai-shek found in 1949,Taiwan,General,In Friends where does Joey keep his favourite book,In the Freezer +General,In which country would you now find the site of the World War One Battle of Caporetto,Italy,General,What is the highest mountain in Canada called,Mount logan,General,In Massachusetts it is illegal to duel with what,Water Pistols + History & Holidays,"Churchill, F.D. Roosvelt and Stalin met here in 1945.",Yalta,General,Ferdinand is a story about a ____,Bull,General,"Tarsus, metatarsus, and phalanges are parts of a(n) _________.",Foot +Science & Nature," The Mojave ground __________, found mainly in the American West, hibernates for two_thirds of every year.",Squirrel, History & Holidays,"""What drink was adapted to become the American Christmas drink 'Egg Nog'? Scandanavian Drink Glogg , Austrian Drink Gluwein, French Drink """"Lait De Poule"""" The German Drink """"Biersuppe"""" "" ",The French Drink Lait De Poule ,General,"Dreams are good friends, when you're ______",Lonely +Music,"""Don't Forget Me When I'm Gone"" Was A Hit For Which Canadian Rockers",Glass Tiger,General,Traffic jam affecting a whole network of intersecting streets,Gridlock,General,In the world of computing what does the acronym P.D.F. stand for?,Portable Document File +Food & Drink, Which red jelly is a traditional accompaniment to lamb?,Redcurrant,General,In what Australian state would you find Moree,New south wales nsw,Music,What is Ozzy Osbourne's real first name?,John +General,On a prescription the Latin expression Ter in Die means what,Three times daily,General,"In the movie ""Mall Rats"", What famous author was signing comic books?",Stan Lee,General,Which actor used to sweep out lions cages for a living,Sylvester Stallone +General,"In the movie 'happy gilmore', who is bob barker's partner",Happy,Entertainment,Which actor won Oscars twice for 'best male performance' in the '90s?,Tom Hanks,General,Parthenophobia is the fear of,Virgins young girls +General,Where did dorothy's house land in 'the wizard of oz',On the wicked witch of,Entertainment,Who sang 'Jet Airliner'?,Steve Miller Band,Music,What did Rocky Racoon find in his room?,Gideon's Bible +General,How many sacraments are there in the roman catholic church,Seven,Music,What Was The Stage Name Of Kajagoogoo's Chris Hamill,Limahl,General,What phantom ship is said to haunt The Cape of Good Hope,The Flying Dutchman +Science & Nature,At what age does a filly become a mare?,Five,Food & Drink,"What is a vitamin D deficiency called ? Seven letters, fifth letter is an 'E' ",Rickets ,Sports & Leisure,On Tv's A Question Of Sport Who was the other Capt with Ian Botham? ,Bill Beaumont  +General,The music in Walt Disney's 'Sleeping Beauty' is based upon which composers version of the classic story,Tchaikovsky,General,St Helier Is The Capital Of Which Island,Jersey,General,What is the princess's name in the story The Sleeping Beauty,Aurora +General,"Norwegian Sigerson, Spy Altamont, Sea Captain Basil all who",Sherlock Holmes disguises, Geography,In which state is Cape Hatteras?,North Carolina,General,A small herring or sprat,Brisling +General,Which American Blues singer originally recorded 'Smokestack Lightning',Howlin' wolf,Science & Nature,For what did Pieter Zeeman receive the Nobel prize in Physics?,Zeeman effect,General,"In Which Country Is The Worlds Tallest Building (2005) Taipei 101 (509 Ft), Petronas (452 Ft), Sears (442 Ft)","Taipei 101,Petronas,Sears" +General,Which date starts the astrological year?,21-Mar, History & Holidays,Which 50's Movie features the Line 'I always get the fuzzy end of the lollipop' ,Some Like it Hot ,Music,What was the Shadows first hit without Cliff Richard,Apache +Entertainment,What was Eric Claptons Nick Name?,Slowhand,General,Who first used acupuncture,Emperor shen-nung,General,What is the largest exclusively indonesian island,Sumatra + History & Holidays,The first ever Olympic Games to be held south of the equator opened in which city in 1956 ,Melbourne , History & Holidays,How Old Was Margaret Thatcher When She Took Up The Role Of British Prime Minister In 1979 ,53 ,Food & Drink,Which drink was advertised with the slogan 'There's a Humphrey about'? ,Milk  +General,In British nobility which title is the highest,Duke,General,Which list did sarah ferguson make five times in people's magazine,Worst,Science & Nature,What Type Of Creature Is An Aardvark? ,An Anteater (Edentata)  +General,Who had a number one hit in 1969 with Something in the Air,Thunderclap newman,General,As what were 'The Supremes' originally known,Primettes,Music,William Michael Albert Broad Is The Real Name Of Which Singer,Billy Idol +Music,Formed In 1981 The Girl Band The Colours Charted & Had Success Under What Other Name,The Bangles,General,What is magma,Hot molten rock,General,In what country is Peter II of Yugoslavia buried,America only euro monarch there +General,Clark who is the smallest member of the european union,Luxembourg,Geography,What's the oldest capital city in the americas ,Mexico city ,General,Who invented the predecessor to today's computers,Charles babbage +Science & Nature,Which mammals fly?,Bats,General,What is the term for the area in front of a fireplace,Hearth,General,Who must you kill to be convicted of patricide,Your father +General,What ancient man's brain was larger than that of modern man,Neanderthal,General,By what more common name do we know Major Boothroyd,Q in the Bond films,General,Which country has the worlds biggest (on land) National Park,Canada – Wood Buffalo 17300s ml +General,"The 'stickman', 'boxer' and 'shooter' are three of the participants in which casino game",Craps or dice, Geography,Where is the Taj Mahal?,India,General,Packy Ease an amateur boxer - what name become famous,Bob Hope + History & Holidays,What toy was in short supply for the 1983 Chirstmas season? ,Cabbage Patch Kid ,Music,Which Band Went “Dizzy” With Vic Reeves In 1991,The Wonder Stuff, History & Holidays,What is the Roman numeral for fifty?,L +Toys & Games,How much does Park Place cost in Monopoly (in US Dollars)?,450,General,Who played the part of Sam Malone in TV 's 'Cheers',Ted danson,Science & Nature,If You Were On An Internet Chat Room And You Wrote The Letters BBIAM What Would You Mean ,Be Back In A Minute  +General,Of which country is Maseru the capital,Lesotho,General,Brussels is the capital of _______,Belgium,General,Greek mythology King Minos of Crete got what annual payment,7 men + Women to feed to Minator +Science & Nature," So that it can pull its lithe body into a tight, prickly little ball for defense, the hedgehog has a large muscle running along its __________",Stomach, History & Holidays,In which 1988 film remake of 'A Christmas Carol'' does Bill Murray play a cynically selfish TV executive ,Scrooged ,General,What was the war during Regan's first term that took place on an island in the carribean?,Grenada +General,Who made dance hits of Bizarre Love Triangle and True Faith,New order,General,What is the common name of the plant Pelargonium,Geranium, History & Holidays,What's fidel castro's brother's name ,Raoul  +General,Stasibasiphobia is the fear of,Standing walking,General,Which herb did the Romans eat top prevent drunkenness,Parsley,General,What is the Capital of: Bangladesh,Dhaka +Science & Nature,What is the stratosphere immediately above?,Troposphere,General,What is the fear of death or dying known as,Thanatophobia,General,Hymen in Greek Genius in Roman Gods of what,Fertility and Marriage +General,What is Admiral Sir Miles Messervy usually known as,M (Bond films),Sports & Leisure,Basketball: The Utah ________.,Jazz,General,Which quirky brunette was one of the first MTv VJ's?,Martha Quinn +Sports & Leisure,"With Which Sport Do You Associate Harvey Smith, Nick Skelton And Rob Hoekstra ",Show Jumping ,General,A dance involving high kicks,Cancan, History & Holidays,Who Pioneered Nursing Whilst Serving In The Crimean War? ,Florence Nightingale  +Science & Nature,Which Animal Has The Largest Eyes? ,The Giant Squid ,General,Aretha franklin sang 'Think' in which film,Blues Brothers,General,In what game/sport terms Bobble Boom Drop Giraffe Pique Twist,Real Tennis types of service +Geography,Which Rock Is On The South Coast Of Spain? ,Gibraltar ,General,"Who, alongside Frank Williams, was the co-founder of the Williams Formula One team?",Patrick Head, Geography,Which is the Earth's third largest continent ?,North America +General,What is the main ingredient of a moussaka,Aubergines,General,What is the architectural term for a representation of leaves at the top of a column?,Acanthus,General,Who invented the 'Spinning Jenny',James hargreaves +Sports & Leisure,Who Played The Part Of Muhammad Ali In The Bio-Pic Of His Life 'The Greatest''? ,Muhammad Ali!! ,Entertainment,What is the only X Rated film to have won the best film Oscar?,Midnight Cowboy,General,Iceland Glima Iran Kushti Turkey Yagli Russia Sambo what is it,Wrestling +Entertainment,What two words are normally at the end of most movies?,The End,Music,"Who directed the film ""Let It Be""?",Michael Lindsay-Hogg,General,What was the name of the first Wings album,Wild Life +Sports & Leisure,What number is by the side of the number 16 on a dart board? (either Side) ,8 or 7 ,General,Long What was Dorothy's aunt's name in The Wizard of Oz,Aunt em, Geography,What is the capital of Illinois?,Springfield +General,The Victorians began circumcision of male babies to stop what,Masturbation,General,Which film director described actors as cattle,Alfred Hitchcock, Language,Merging the words 'melt' and 'weld' created which word?,Meld +Music,Who Told Us Its Grim Up North,The KLF,Music,Prince & The Revolution Said Her Beret Was What Colour,Rasberry,General,In what Australian state would you find Horsham,Victoria +General,What is hulk hogan's real name,Terry bollea,General,What screen cowboy rode Tony the Wonder Horse,Tom Mix,General,In what country did the sepoy mutiny occur,India +Entertainment,What was the name given to the popular genre of rock that arose in the Pacific Northwest (Seattle) in the early 1990s.,Grunge,Music,In Which Year Did The Band Play A Free Concert In Londons Hyde Park,1969,General,Ebenezer HowaWhat does the musical term 'pesante' mean,Rd is most associated with the foundation of what garden cities +General,If you practice Encraty what are you doing,Abstinence or Self Restraint,General,What is the longest river in Italy,Po,General,Israel has the highest per capital consumption of ______,Turkey +General,Which Singer Was Born Natalie McIntyre?,Macy Gray, History & Holidays,In which 1970's films does Dustin Hoffman play the character ' Lenny Bruce' ? ,Lenny ,General,How long did it take god to create the universe,Seven days +General,What does a phobophobe fear,Fear,General,What type of bird is a 'Beltsville',Turkey,General,In what sport do women compete for the Uber cup,Badminton +General,What were the 1948 Olympic games known as,The austerity games,General,P L Travers created which famous character,Mary Poppins,General,"A silicate mineral, heat resistant and insulating",Asbestos +General,In Australia what is the second Sunday in May,Mothers day,Science & Nature,"Which Lays Blue Egg's , The Song Thrush Or The Mistle Thrush ",Song Thrush ,General,What insurance company in the u.s stopped using their slogan 'own a piece of the rock' after rock hudson died of aids,Prudential life +Religion & Mythology,He was the father of Zeus,Cronus,General,The name Europe comes from where,Greek Mythology,General,What animals are on the australian coat of arms,Emu and kangaroo +Sports & Leisure,What Sport Are Torvill & Dean Famous For ,Ice Skating / Ice Dance ,General,Which 1997 Movie Held The Record For The Biggest Opening For A British Film At The US Box Office Taking 2.5m Dollars In Just 3 Days,Mr Bean (The Ultimate Disaster Movie),General,Originating In Mexico A “Pok Ta Pok” Is Known In Modern Times As What Sporting Item?,A Basketball +Science & Nature,"In the animal kingdom, if reptiles are in class reptilia, then birds are in class ____",Aves,General,After Paris which is the largest French speaking city in the world,Montreal,General,"Other than the U.K. and Eire, name a European country where cars are driven on the left hand side of the road.",Cyprus malta +General,Who was the first female to enter the billboard charts in 1985,Whitney,General,In Minneapolis what is the maximum penalty for double parking,The Chain Gang,General,What year was the first oreo sold in,1912 +General,Who wrote the classic novel' Anne of Green Gables',L m montgomery,General,Thomas Edison had a phobia what was it,The Dark,General,Which Italian composer's funeral in 1924 was said to have brought Rome to a standstill,Puccini +General,In Utah you can get a licence to hunt what,Dinosaurs,General,Who was British Prime Minister at the time of the General Strike in 1926,Stanley baldwin,General,-ologies: Study of the sound system of languages; the analysis and classification of phenomes,Phonology +General,Who was the dictator of Portugal from 1932 to 1968,Salazar,General,What instrument does Grumpy play in Disney's Snow White,Organ,Entertainment,Who were the 2 lead characters in the movie Life?,Eddie Murphy and Martin Lawrence +General,Who was the first American astronaut to orbit the Earth,John glenn,General,What is a Pygmy Blue,Smallest butterfly,General,What position do baseball's 'cy young award' winners play,Pitcher +General,The average Britain consumes 14571 what in their lifetime,Pints of beer,General,A hot spicy root used in cooking,Ginger,General,Vaduz is the capital of ______,Liechtenstein +General,What is the most commonly used condiment in the world,Mustard,General,What type of animal is a 'godwit',Bird,General,What's the smallest mammal,Shrew + Geography,What is the basic unit of currency for Yemen ?,Rial,General,Who was the male star when The Mousetrap first played,Richard Attenbourough,General,Which U.S. golfer was killed when his plane crashed in 1999,Payne stewart + History & Holidays,"Famed as a member of the Rat Pack, who died on Christmas day 1995 ",Dean Martin ,General,Where was/is the original Penthouse,In a Real Tennis Court,General,What is the fear of shadows known as,Sciophobia +General,What is the popular word for the umbilicus,Belly button, History & Holidays,Sir Stamford Raffles founded which minor Asian nation?,Singapore,Science & Nature, Snails have __________. They are arranged in rows along the snail's tongue and are used like a file to saw or slice through the snail's foot.,Teeth + History & Holidays,Which novel opens and closes with the letters of Robert Walton ,Frankenstein ,General,Which sea on Earth has no beaches,Sargasso sea,General,What is a sorcerer who deals in black magic called?,Necromancer +General,Which chess piece could be a member of the church,Bishop,General,The longest key on your keyboard is the _____ bar,Space,General,Which Jane Austen novel was originally entitled First Impressions,Pride and prejudice +General,At which address will you find the White House,1600 pennsylvania avenue,General,Does sound travel faster in warmer or colder air,Warmer,General,Indians eat using which hand?,Right +General,A suspended bed of canvas or netting,Hammock,Geography,In which country is normandy ,France ,Music,What was Sam Cooke's only #1 song?,You Send Me +General,What is the world's deepest lake,Lake Baikal,General,Sinatra What are the primary colors,Red yellow blue blue red yellow blue yellow,General,The interior of what is called the paste,Cheese round +General,What is the name of the office used by the president in the whitehouse,Oval,General,The artist formerly known as prince had a both a hit song & movie by this title,Purple rain,Science & Nature," The word ""puppy"" comes from the French __________, meaning ""doll.""",Poupee +Science & Nature,In Computing What Does The Word Modem An Abbreviation Of ,Modulate Demodulate ,General,"The symbol on the ""pound"" key (#) is called a(n) _________",Octothorpe,General,Which oceanic plate lies off the Andes,Nazca +General,Who is the Patron Saint of dancers and actors,St Vitas, History & Holidays,Where Was The Only British Prime Minister To Be Assassinated Killed? ,The Lobby Of The House Of Commons ,General,"When a female horse & male donkey mate, the offspring is called a what",Mule +General,What are the sandals called that are worn in ceremonial japanese tradition,Tabi,Sports & Leisure,Which Former BBC Sports Presenter Gave Up His Career In Pursuit Of Spiritual & Religious Beliefs & Was Once Called (As Nutty As A Box Of Frogs) By A Talk Show Host ,David Icke , History & Holidays,Who took over from Kennedy after he was assassinated? ,Lyndon B Johnson  +General,Name the author of Gone with the Wind,Margaret mitchell, Geography,What is the capital of Singapore ?,Singapore,General,What takes place at Montlhery France and Zandvoort Holland,Motor car racing +General,What is the name of the Kellogg's cereal prefixed with the word 'Optima',Fruit and fibre,General,Dressed list who paid miss ussr four cartons of marlboro to be on his show,David,General,"In ballet, shifting weight from one foot to the other.",Dégagé +Science & Nature,For what is Na the chemical symbol? ,Sodium ,General,What does n.a.s.a stand for,National aeronautics and space administration,General,Who is the female presenter of the TV programme Fort Boyard,Melinda messenger +General,There's Something About Mary' starring Cameron Diaz was released in what year,1998, History & Holidays,Which Country Celebrates 'The Day Of The Dead' Instead Of Halloween Which Includes The Traditionn Of Paassing A Live Person In A Coffin Through The Streets ,Mexico ,General,"What award, founded in 1901, is funded with the help of the Bank of Sweden",The Nobel Prize +General,"Who, in literature, is eligible to be nominated for the annual award called the Carnegie Medal",Authors of children's books,General,Who wrote 'roll over beethoven',Chuck berry, History & Holidays,"""Who was the author of """"A Christmas Carol""""?"" ",Charles Dickens  +General,Trouw is a newspaper in what country,Netherlands,General,Who released the smash hit 'she drives me crazy',Fine young cannibals,General,"Who composed ""Sinfonia Antartica""",Ralph vaughan williama +General,What is the main use of tinder,Lighting a fire, History & Holidays,"The original Halloween film directed by John Carpenter in 1978 cost just $320,000 to make. It ended up making how much world wide was it A 40M, B 50M or was it C 60M ",B (50 Million) ,General,Nations What actor was married to Carole Lombard between 1939 to 1942,Clark gable +General,Dr Deidrich Knickerbocker invented which famous character,Rip Van Winkle,General,"Whose last words were ""Thomas Jefferson still survives""",John adams,Science & Nature,How Is Frozen Carbon Dioxide More Commonly Known? ,Dry Ice  +General,What have men played with for longer than anything else,Dice,General,What Planet Is Sometimes Refered To As Lucifer,Venus,Music,What Is The Barber Of Sevilles Name,Figaro +General,A university has a campus what does it literally mean,A Field,Music,What Is Bo Diddley's Real Name,Elias Bates (Elias McDaniel),General,What's the common name for hydrogen hydroxide,Water +General,Spain's equivalent to the dollar is the ______?,Peseta,General,"A polish national dance in triple time with an accent on the second beat, characterized by proud bearing, clicking of heels, and holubria, a special turning step.",Mazurka,General,"Magazine wrote ""Fighting for peace"" is like fucking for chastity",Knave March 1977 +General,What is the only Bible book referred to in a Shakespeare play,Numbers in Henry V, History & Holidays,"Beginning in the 1820s, who from the southeastern united states were relocated to indian territory over numerous routes ",The five civilized tribes ,General,What is the criminal number of sideshow bob in 'the simpsons',24601 + History & Holidays,Who was Cary Grant's female lead in the movie 'To catch a thief? ,Grace Kelly ,General,97% of all paper money in the USA has traces of what on it,Cocaine,General,Who was given a ticker tape parade in new york after his 1927 flight to Paris,Charles lindbergh +General,What is the official language of Mexico,Spanish,General,What TV show had a character named Potsie?,Happy Days,General,What Roman galley was Judah Ben Hur a slave oarsman on,Astrea +Food & Drink,Which village in Leicestershire gives its name to a hunt notorious among animal rights protesters and a food popular with vegetarians? ,Quorn ,General,A horse's hoof is also known by what grim term,Coffin,General,Graphite dust is formed when what is cut with a laser,Diamond + History & Holidays,Who Released The 70's Album Entitled Autobahn ,Kraftwerk ,General,Who wrote the musical The Desert Song,Sigmund Romberg,General,What would you be if you were a coryphée,Ballet Dancer + History & Holidays,Which Mother & Daughter Both Appeared In John Carpenters Film The Fog ,Janet Leigh & Jamie Lee Curtis ,Music,Herbie Mann Is Best Known For Playing Which Instrument,Flute,General,What is the Capital of: Mali,Bamako +General,In The World Of Sport Wendy Toms Was The First Woman To Do What?,First Female Referee,General,What are the sacred Hindu texts called,Vedas,General,What elaborate white marble tomb did India's shah Jahan build in memory of his favourite wife,Taj mahal +General,"What are Demy, Medium, Royal, Double Crown",Paper Sizes,General,What is the tallest piece on a chessboard,King,General,The Silkworm Lives On A Diet Of Leaves From Only One Type Of Plant What Is It,Mulberry +General,What is the name given to sole cooked in white wine and cream with grapes,Sole veronique,General,What is the fear of ants known as,Myrmecophobia,General,Whose flag has the national arms on one side and the treasury seal on the other?,Paraguay +General,Who did Vivian Vance play on 'the lucy show',Vivian Bagley,General,Who was the losing Democratic candidate when Dwight D Eisenhower was re-elected as U.S. President in 1956,Adlai stevenson,Science & Nature,Waves 'break' when their height is how much more than the depth of the water?,Seven tenths +General,On Scooby Doo what was Shaggys real name,Norville,General,Who wrote 'gone with the wind',Margaret mitchell,General,U.S. Captials - Idaho,Boise +General,Which former British colony is now called Ghana,Gold coast,General,Who is the largest builder of fire engines in the united states,Ward la,General,What was Adam Sandler's occupation in Big Daddy?,A tollbooth worker +General,What first appeared on the USA domestic market in 1960,Canned Coca Cola,General,Hydrargyophobia is the fear of,Mercurial medicines, History & Holidays,What country did the allies invade in world war ii's operation avalanche ,Italy  +General,What happend in the final episode of M.A.S.H. hence the theme tune?,They All Commited Suicide The Theme Is Called 'Suicide Is Painless',General,Psychrophobia is the fear of,Cold,General,Countries of the world:about 700 islands between Florida & Haiti,Bahamas +General,Implement for clearing up dog excretment,Pooper scooper,Entertainment,What is Dennis the Menace's surname?,Mitchell,General,When does the southern Autumn Equinox occur,42075 +General,French for until we meet again,Au revoir,General,What kind of clay can potters heat to a higher temperature earthenware or stoneware,Stoneware,General,Who killed goliath,David +General,SOS the distress message stands for what,Nothing - NOT save our souls,General,Piano Man' was which man's first successful album in 1974,Billy Joel,Music,What Song did david Bowie & Mick Jagger Have A Hit With As A Duo,Dancing In The Street +General,"Who played detective, Frank Cannon, in the TV series 'Cannon'",William conrad,General,"When solid substances in the blood are removed, what name is given to the remaining liquid",Plasma,Science & Nature,What Were Pterosaurs ,Warm Blooded Flying Reptiles Related To Dinosaurs  +General,"Which writer's latest work, Birds of Prey , features the Courtneys - the family that appeared in his first, When the Lion Feeds , published in 1964",Wilbur smith,General,Who was the only person to win world titles on bikes and cars,John Surtees,General,Who wrote the opera 'tosca',Giacomo puccini +General,What is a Ha Ha,Sunken Fence,Art & Literature,What Is The Name Of The 7th And Final Harry Potter Book ,Harry Potter & The Deathly Hallows ,General,Darwin She starred in Broadcast News and The Piano.,Holly Hunter +General,Who played Tchaikovsky in the 1971 film The Music Lovers,Richard chamberlain,Science & Nature,Which Year Was The Orbiting Hubble Telescope Launched ,1990 ,Music,Which Song Gave The Group Hanson A Trans Atlantic Hit in 1997,MMM Bop +General,At what sport did Italian Giacomo Agostini compete,Motor cycling,General,What year was Picasso born,1881,General,"What does the typical man have 13,000 of",Whiskers +Music,Which Former Beatle Became A Travelling Wilbury?,George Harrison,General,Who is the norse god of lightning,Odin,General,Any member of a group of protozoa (single-celled animals) that are blood parasites,Trypanosome +Music,"Which Singer Has Appeared On Stage Acting As Jesus, CheGuevara, Lord Byron & Fletcher Christian",David Essex,Music,"From Her 1994 Hit When did Sheryl Crow ""Like A Good Beer Buzz""",Early In The Morning, History & Holidays,Who killed Jesse James?,Robert Ford +General,What does the name Dracula mean in Romanian,Son of the Devil,General,"What beach boys song blathers ""rah rah rah rah, sis boom bah""",Be true to,General,What is Ordune,Arousal by nude pics +Geography,What is the capital of Kiribati,Bairiki,General,As a performer what one thing would Elvis never do,An Encore,General,The Florida Martins won the World series baseball in this year,1997 +Music,Who Left Eastenders In 1998 To Pursue A Singing Career,Martine McCutcheon,General,"What was a big hit for the pipes, drums, and military band of the Royal Scots Dragoon Guards in 1972",Amazing grace,Music,"Who Said ""I Don't Wanna Fight"" Was The Ideal Theme For Her Autobiography",Tina Turner +General,Who was the first female prime minister of india,Indira gandhi,General,When was the resurrection,Easter,Science & Nature,What animal's milk is more than 54% fat?,Humpback whale +General,What former british colony's largest retailer was duty free shoppers,Hong,General,First Impressions was the original title of what classic novel,Pride and Prejudice,Music,Which Band Of The 1970's Got Their Name From Vital Elements In The Astrological Chart,"Earth, Wind & Fire" +General,What is the heaviest element,Uranium,General,Who climbed mount fuji at age 99,Teiichi igarashi,General,Which Great Lake has tides,Superior +General,With what city is alcatraz associated,San francisco,Science & Nature," The female king crab incubates as many as 400,000 young for 11 months in a brood pouch under her __________",Abdomen,General,Kenny Rogers sang Someone Who Cares for what film,Fools +Sports & Leisure,Which German Golfer Won The Us Masters In 1993 ,Benhard Langer ,Entertainment,"When Tweety exclaimed, ""I thought I saw a putty tat!"", who did he see?",Sylvester, Geography,What is the basic unit of currency for Greece ?,Drachma +General,"Who painted ""Big Raven""",Emily carr,General,What is the current vat rate in the uk,17.5%,General,Pali is the sacred language of who,Buddhists in India +General,What is dirty harry's surname,Callahan,Music,What Message Did Michael Ward Bring Us In His One And Only Chart Success,Let There Be Peace On Earth,General,"What is this sign called ""&""?",Ampersand +General,What is the biggest tourist attraction in Zambia,Victoria falls,General,By Winning The World Snooker Championship In 1998 Who Knocked Stephen Hendry Of The No.1 Spot,John Higgins,Geography,To Which Country Does Easter Island Belong ,Chile  +General,Catriona was a sequel to which famous novel,Kidnapped R L Stevenson,General,What was the top grossing film of the 60s,The Sound of Music,General,Venus and Adonis was whose first published work,William Shakespeare +General,"Pantelleria, Ustica and Lampedusa are all islands belonging to which country",Italy,General,In Norse mythology what is the name of the ultimate battle,Ragnarok,Art & Literature,"What Was The Famous Novel Written In Gaol, By John Bunyan ",Pilgim's Progress  +General,The basic ingredients of a ploughmans lunch is bread & what,Cheese,General,According to Hite report masturbating women like to use what,Candles,General,"Roald Amundsen Achieved A World First On ""Dec 14 th – 1911” What Was It?",First Person To Reach The South Pole +General,What city is April wine from,Montreal,General,"What Is The Only Spin Off To Date From The TV Show ""Coronation Street""",Pardon The Expression,Music,"Who Bounced Back To No.1 In 1999 With ""Maria""",Blondie +General,What does a catoptrophic narcissist fear,Mirrors,General,When Sharon Davis Appeared On Th TV Show Gladiators What Was Her Gladiator Name,Amazon,Art & Literature,Which Novel Was Originally Going To Be Titled Elinor And Marianne? ,Sense And Sensibility  +General,Hamburg lies on which river,The elbe,General,"Name Any Year In The Life Of Socrates, He Lived Til The Age Of 70",469-399 BC,General,The Windmills of your Mind was a theme song in what film,The Thomas Crown Affair +Entertainment,This was the Beatle's first film.,A Hard Day's Night,General,The automobile company that makes beetles,Volkswagon,General,"In which constellation in the night sky is there to be found the only confirmed ""Black Hole""",Cygnus +General,Which Latin motor-manufacturing name was originally that of a subsidiary of S.K.F. - the bearing makers - for whom the car's design engineers worked,Volvo,General,Who was the author of catcher in the rye,Jd salinger,Music,Where did Horst Jankowski Take His Piano For A Walk In 1965,For A Walk In the Black Forest +General,What is the English equivalent of the Spanish boob day,April fool's day,Science & Nature,Which meteor shower occurs on the 12th August ?,Perseids,General,For which film did Michael Caine win an Oscar in 2000,The cider house rules + History & Holidays,What was the name of the first ironclad warship ever launched?,HMS Warrior,General,What does LL Cool J.'s name stand for?,Ladies Love Cool James,General,Whose ghost appears in Shakespeare's Julius Caesar,Caesar's Ghost +General,The Wright brothers made aircraft but what was their other job,Bicycle manufacturers,General,Where were the ancient olympics held,Olympia,General,"According to Espen Lind when __________________ cries, she cries a rain storm, she cries a river she cries a hole in the ground",Susannah +General,How is the mathematically related structure of beads strung on parallel wires in a rectangular frame better known ?,Abacus,General,What city is known as The worlds chocolate capital,Hershey Pennsylvania,General,Name the implement that removes water from your windshield on your car?,Wiper +General,What countries monetary unit is also the bird on its flag,Guatemalan Quetzal,Entertainment,What instrument does an organ grinder play?,Hurdy gurdy,General,Who said Tis better to have loved and lost etc,Alfred Lord Tennyson In Memoriam +General,"In Astronomy What Are Pallas, Vesta & Davida?",Asteroids,Music,With Which Band Did Hugh Cornwell Sing Lead Vocals,The Stranglers,Science & Nature,What Is The Term Used For The Disintegration Of A Nuclear Reactor ,Meltdown  +General,Susseration is what,Whispering,Music,Which Band Did Peter Murphy Of Bauhaus & Mick Karn Of Japan Form,Dali's Car,General,"Which Poet Laureate declared 'I must go down to the sea again, to the lonely sea and sky'",John masefield +General,What is a very thin pastry used in Mediterranean cooking,Filo,General,Name the three children on Kate and Ali.,"Jenny,Chip and Emma",Religion & Mythology,"In English mythology, who caused the death of the Lady of Shallot?",Sir Lancelot +General,In WW2 the Graf Spee was forced into what harbour,Montevideo,General,Where could you see or do an Ollie and a McTwist,Skateboarding,General,What is Belleek,Type of Pottery +Art & Literature,Which Shakespeare play contains the line 'if music be the food of love play on''? ,Twelfth Night ,General,"Broadway 59 music Ordinary Couple, Preludium, Processional",The Sound of Music,Sports & Leisure,"According to Olympic rules, what number of feathers must a badminton bird (shuttlecock) have?",Fourteen +General,To what did john lennon change his middle name,Ono,Music,Name the 2 former Beatles who played with the band in their early tour of Germany?,Pete Best & Stuart Suttclife,Religion & Mythology,"In Greek mythology, who hired Daedalus to construct the labyrinth?",Minos +General,For what condition is the drug Mogadon prescribed,Sleeplessness,Food & Drink,What equivalent English course is antipasto? ,Starters or Hors D'Oeuvres ,Music,"Ever Glamorous She Died Aged 34, On 26th October 1966 Ian Dury Later Called Her The Best Popular Singer Britain Has Ever Had Who Was She",Alma Cogan +Food & Drink,"According to Harry Belafonte, what kind of water is 'good for your daughter' ? ",Coconut water ,General,What was the name of the sad faced clown portrayed by Emmett Kelly?,Weary Willie,General,Salix Alba produces which drug,Aspirin - from the willow +General,At the end of which siege in 1954 were the French finally beaten in Vietnam,Dien bien phu,General,"In Greek mythology, Pasiphae gave birth to which creature",Minotaur,General,How many hearts has an octopus,Three +Music,"Who Had A Hit In 1981 With ""I Surrender""",Tight Fit,People & Places,Which Famous London Landmark Was Designed by Sir Norman Foster ,The Millenium Bridge ,Science & Nature,What hormone is produced by the adrenal glands?,Adrenaline +General,Softball is a variation on which game,Baseball,General,What food was almost non-existent in ireland in the 1840's,Potatoes,General,Which Millais painting was later used in adverts for soap,Bubbles +General,What does blt stand for,"Bacon, lettuce, tomato",Science & Nature,What is the main component of air,Nitrogen,General,What was the first country to use TV as a mass info media,Germany +General,"Which song is used in the movie ""Loser""?",Teenage Dirt Bag,Music,What Was Pop Duo Jemini's “Famous Achievement” Of 2003?,Last In Eurovision Song Contest With No Points,General,What country is ruled by King Hans Adam II,Liechtenstein +General,Who wanted to play Brody in Jaws but Spielberg rejected him,Charlton Heston,Sports & Leisure,The Greek God Apollo Accidently Killed His Friend Hyacinthus While Practising For Which Sporting Event ,The Discus ,General,"It is commonly called the funny bone, where is it",Elbow +General,Who wrote the 'Unfinished Symphony',Wolfgang Amadeus Mozart Mozart,Science & Nature,Which meteor shower occurs on the 16th November ?,Leonids,General,Star which TV show really a programmer written 20 books on it,Buffy Vampire Slayer Sarah Michelle Geller +General,What is the capital city of the newly created country South Sudan?,Juba,General,The words 'coffee' and 'yoghurt' originate from which language,Turkish,General,What does an ichthyologist study,Fish +General,What is 7 days and 10 hours in minutes,10680,General,Which acid builds up in the body during excessive exercise,Lactic, History & Holidays,"""From the Christmas Carol """"Good King Wenceslas"""", where was Good King Wenceslas the King of?"" ",Bogemia  +General,What is the staple food of one third of the worlds population,Rice,General,White and the seven dwarfs Where is the Plumas National Forest,California,Science & Nature,What did North American Indians eat to dissolve gravel and stones in the bladder?,Watercress +General,Wine barrels - There are 83 gallons in a what,Puncheon,General,"Primrose, Montgomery, Petunia, Zinnia And Victoria Were 5 Of The 6 Children That Featured In Which TV Series",The Darling Buds Of Me,General,What ancient languages writing has no spaces between words,Ancient Greek + History & Holidays,"What did presidents Madison, Monroe, Polk, and Garfield have in common","The first name "" james """,General,Many ancient mayan ruins are located on which peninsula,Yucatan peninsula,General,Who was the Roman god of field boundaries,Terminus +General,What river did the Pied Piper drown the rats in,Weser,General,What was jane wyman reagan's birth name,Sarah jane fulks,General,What is anaemia,Iron deficiency +Music,"Name The Title Song From The Movie ""Back To The Future""",The Power Of Love,General,Who cut the US flag to pieces and was honoured for it,Robert Peary left bits at North Pole,General,U.S. captials Wisconsin,Madison +Science & Nature,Where & When Did People First Use Spectacles ,In China In The 1200's , Geography,Which country occupies the 'horn' of Africa?,Somalia,Geography,Ouagadougou Is The Capital Of Which Country ,Burkino Faso (Upper Volta)  +General,"What do scientists say would be the best ""space food"" for astronauts",Insects,General,In medieval Spain which city was noted for its quality steel,Toledo,Sports & Leisure,Baseball: The Kansas City ______?,Royals +General,Monophobia is a fear of ______,Solitude,General,"Who wrote the opera ""the Thieving Magpie'",Rossini,General,Which beatle song features sweet loretta,Get back +Art & Literature,"In Which Book Which Is Also A Film Has The Characters Pod, Arrietty, Homily & Peagreen? ",The Borrowers ,General,When The National Lottery First Introduced It's Midweek Draw Who Presented The Show,Carole Smille,General,"What sport is identified with the movie, ""kansas city bomber""",Roller derby +General,In which Gilbert and Sullivan opera did Col. Fairfax marry Elsie,Yeoman of the guard,General,What is armagnac,Brandy,General,Kimgazer is a newspaper in what country,Cyprus +General,The collection of poems called Barrack Room Ballads and Other Verses was written by whom in 1892,Rudyard kipling,General,Name actor fired as no star quality because of big Adams apple,Clint Eastwood,Art & Literature,"Who wrote the long religious epic, ""Paradise Lost""?",John Milton +Science & Nature, You can identify a __________ bear's mark by the sign of five claws. A black bear will lacerate a tree trunk with four claws.,Grizzly, Geography,What is the basic unit of currency for New Zealand ?,Dollar,General,In TS Elliot's book of practical cats name the mystery cat,Macavity +General,Which country first used the fountain pen,Egypt,Sports & Leisure,Where are the U.S. Tennis Open Championships held,"Flushing meadows, ny",General,"Which Invention Often Associated With Fashion Was Invented In 1906 By ""Karl Ludwig Nessler""",The Perm +General,What sport would you see at Arthur Park,Grand Prix Racing,Music,Name The Group That Linked With Motorhead On The St Valentines Day Massacre EP,Girlschool,General,What is the best selling personal computer game of all time,Myst +Music,"What Song Features The Lyric ""But I Didn't Shoot The Deputy""",I Shot The Sheriff,General,"The Olduvai Gorge, famous for its fossils, is in which country",Tanzania, Geography,Surfing is believed to have originated here.,Hawaii +General,In Georgia its illegal to do what with a fork,Eat fried chicken,Entertainment,Who was the first female to enter the Billboard charts in 1985?,Whitney Houston,Science & Nature,Which Sex Is The Only One To Suffer From Haemophilia ,Male  +General,What is the tribal african word for dowry,Lobola,Geography,What is the capital of Tonga,Nuku'alofa,General,What is Sekkusu in Japan,Sex +General,What are most of the solar system's planets named for,Roman gods,General,We have all heard Hari Krishna - what does Krishna mean,Dark as a cloud,Music,"When Elvis Presley Returned To Civilian Life After Army Duties, He Appeared In A ""Welcome Home Elvis"" Television Spectacular Who Hosted The Show",Frank Sinatra + History & Holidays,Which Country Was Awarded The George Cross For Civilian Bravery In 1942? ,Malta , Geography,What is the basic unit of currency for Sierra Leone ?,Leone, History & Holidays,What was Maggie Seaver's maiden name on Growing Pains? ,Maggie Malone  +General,Jewish boys have a Barmitsva at 13 what do girls get at 12,A Batmitsva,Music,"Paul wrote the song ""Hey Jude"" for:",Julian Lennon,General,A character named 'Spearchucker Jones' was deleted from this famous American television show's cast after only five episodes,MASH +General,The word puppy comes from the French poupee - literally what,Doll,Sports & Leisure,Which Martial Art Made It's Debut At The 2000 Olympic Games ,Tae Kwon Do ,General,What is the 1983 bryan adams album which features the hit 'cuts like a knife',Cuts like a knife +General,Thieves Liars Magicians and who were in Dantes 8th circle Hell,Politicians,General,"Slip, square and surgeon all types of what",Knot,General,What was Rizzo's real name in Grease?,Betty + Geography,What is the Southernmost country in continental Europe?,Spain,Music,What Is Kylie Minogues Middle Name,Ann,Sports & Leisure,In Which Game Might You Get A Royal Flush ,Poker  +General,Who was eaten by dogs in the Old Testament,Jezebel,General,Hurt driving: what country is identified by the letter h,Hungary,Music,"Which Group Were A One Hit Wonder With ""Danger Games""",The Pinkees +General,Who did patrick duffy portray in the tv series 'dallas',Bobby ewing,General,If something is coked en brochette how is it done,On a skewer,General,Which wood was traditionally used to make English longbows?,Yew + Language,Multiple Meanings: Drinking utensils or sight-enhancers.,Glasses,General,"Name the French boxer who was World Heavyweight Champion from 1920 to 1922, best remembered for his title fight with Jack Dempsey in 1921",Georges carpentier,General,Into which body of water does the river Danube flow,Blacksea +Music,Which Artist Spent Most Weeks On Chart In 1987,Madonna,General,Which is the only musical bird that can fly backwards,Hummingbird,General,For which team will Neil Hodgson be riding in the forthcoming World Superbike Motorcycling season,Ducatti +General,With what line of work was mandy rice-davies associated,Prostitution,General,What does the name of Computer giant Intel stand for in full?,Integrated Electronics,General,"To the nearest one million miles, how far is the Sun from the Earth",93 +General,A change in frequency observed when light is scattered in a transparent material,Raman effect,General,Who is anne mae bullock,Tina turner, History & Holidays,Who killed 5 london prostitutes in 1888 ,Jack the ripper  +Music,Which Type Of girls make the Beatles scream and shout?,Moscow girls,General,"Who said all things were made up of air, earth, fire, water",Aristotle,Music,"Who Sang With Michael Jackson On ""Say Say Say""",Paul McCartney +General,Which Welsh island is called the Isle of the Saints,Bardsey island, History & Holidays,"Born Florian Armstrong, on Christmas Day 1971, which female singer dueted with Eminem on the 2000 hit 'Stan'' ",Dido ,Music,"On What Label Was ""Sgt Peppers Lonely Hearts Club Band"" Released",Parlophone +General,What Form Of Transport Was Pioneered By Igor Sikorsky?,Helicopter,General,The city council of Chico California set a $500 fine for what,Exploding nuclear bomb in city,General,Which female name means worth of love,Amanda +Music,Which Beatle Divorced His Wife Cynthia In 1968,John Lennon,Sports & Leisure,What is the formula one course called in Canada ? ,Montreal ,General,In what Australian state would you find Coffs Harbour,New south wales nsw +General,What is the Capital of: Norfolk Island,Kingston,General,Which actor was Howard Hughes describing when he said 'His ears make him look like a taxi cab with both dooors open',Clarke gable,Science & Nature,Which Company Began Manufacturing The Jeep In 1943 ,Willys  +People & Places,Which British Director Attempted To Appear In All The Films He Directed ,Alfred Hitchcock ,Entertainment,In the 70s Hit Captain Scarlet and the Mysterons what is the name of the company Scralet works for?,Spectrum,General,Lome is the capital of ______,Togo +General,What was the name of the first space shuttle ever built?,Enterprise,General,Palmer From what is rum distilled,Sugar cane,General,What was the first publicly televised sporting event in Japan,A Baseball Match +General,Only three Angels are named in Bible Gabriel Michael and who,Lucifer,General,"What are Nissan, Av, Adar and Tishrei",Jewish months,Music,"Who recorded the Lennon/McCartney song ""World Without Love""?",Peter and Gordon +General,What in the wild west was known as The Peacemaker?,Colt 45,Music,"Whose 1998 ""Best Of"" Compilation Was Entitled ""Hatful Of Rain""",Del Amitri,General,The Teddy bear was named after which US president?,Theodore Roosevelt +Science & Nature, __________ can withstand water pressure of up to 850 pounds per square inch.,Seals,Music,"Which Band Leader/Composer/Producer Wrote The Score For The 1967 Movie ""In The Heat Of The Night"" Featuring Ray Charles On The Title Song",Quincy Jones,Science & Nature,Which Has The Longer Wavelength Xrays Or Microwaves ,Microwaves  +General,Sourj is Armenian for what,Coffee,General,What city is served by Narita Airport,Tokyo,General,Adolf Hitler took nude photos of Ava Brown - What part why,Arse - so she not recognised +Music,"""Edge Of Heaven"", ""Last Christmas"" & ""I'm Your Man"" Were Hits for Which 80's Duo",Wham,General,Diameter Who plays shortstop for Charlie Brown's baseball team,Snoopy,General,From where was ricky in 'i love lucy',Cuba +General,Which event of 1995 was advertised by the complete day's print of 'The Times' newspaper being offered to the public free of charge,Launch of windows 95,Entertainment,What was the name of George of the Jungle's pet elephant,Shep,Entertainment,Where did Mighty Mouse get his superpowers,The supermarket +General,What's the correct name for a male turkey,Tom,General,What does the name Tokyo mean,Eastern City,General,What is Cornelius Mcgillicuddy's stage name,Connie Mack +General,"Room for daddy What cat sniffed: ""Cute rots the intellect""",Garfield,General,"In the Bible, which ethnic group took Samson prisoner in Gaza, resulting in him pushing down the temple of Dagon",Philistines, Geography,What is the capital of Samoa ?,Apia +Science & Nature,What type of animal lives in a formicary?,Ant,General,The name for this semi precious stone comes from the latin for sea water,Aquamarine,Science & Nature,What word is used for a male deer,Buck +Music,Which Ex Member Of Televsion Fronted The Voidroids,Richard Hell,General,Collective nouns - a streak of what,Tigers,General,"According to his nazi dossier, what color are Rick's eyes in Casablanca",Brown +General,In what city would you find The Jacques Cartier bridge,Montreal,Music,Which Famous British Pair Could Be Found Dancing In The Street,Mick Jagger & David Bowie,General,A Gila monster is a type of what,Lizard +General,What Pope started the Inquisition,Gregory 9th,General,Which U.S. President ordered the dropping of the atom bombs on Hiroshima and Nagasaki,Truman,General,A catholic minister is known as a,Priest +General,What sport/game is chris evert associated with,Tennis,Entertainment,Who played 'Johnny Mnemonic'?,Keanu Reeves,General,Where is the septum linguae,Tongue +General,What is an ornithorhynchus,Platypus,General,U.S. captials Oregon,Salem,General,A typical American eats how many pigs in his/her lifetime,28 +General,After what were the B52 bombers named?,A fifties hairdo,General,Who was barney rubble's best friend,Fred flintstone,General,Who had a job as a Grave Digger,Rod Stuart +General,At which town did Livingstone and Stanley meet in 1871,Ujiji,Music,Whose ballet music includes Petrushka and The Rite of Spring?,Stravinsky,General,An Intravenuspylorigram is used to detect the presence of what in the human body?,Kidney Stones +General,What is the drug that is used to treat Parkinson's disease,Dopamine,General,An anemometer measures _________,Wind velocity,General,"Because the emu and the kangaroo cannot walk backwards, they are on the Australian ______?",Coat of arms +Tech & Video Games,What did the letters in ROB (the old NES peripheral) stand for? ,Robotic Operating Buddy,Science & Nature,Of what did Aristotle say all things were made up?,"Air, earth, fire, and water",General,Mary Kelly was the last known who,Victim Jack Ripper +General,David Cornwell is better known as which author,John Le Carr,General,Whose portrait appeared on the reverse of the last £1 note,Isaac newton,General,"In The World Of Sport How Did ""Guenter Parche"" Hit The Headlines In 1993",Fan Who Stabbed Monica Seles +General,70% of Americans have done what,Visited Disneyland / world,General,What social insects are the favourite food of numbats,Termites,Sports & Leisure,What Is The Name Of The Player In Baseball Who Is Positioned Behind The Home Plate ,The Catcher  + History & Holidays,What Was Formerly Called Stingray Harbour? ,Botany Bay ,General,How many days can a cockroach live without water,30,General,A mirliton is another name for what musical instrument,A Kazoo + Geography,What is the capital of Hungary ?,Budapest,General,What is a group of hogs,Passel,Music,Under What Name Had Punk Band PVS-2 Previously Had A No.1 Hit,Silk (Forever & Ever) +General,Character in a movie series named Sanskrit word warrior what,Yoda from Yoddha,General,What is the Chinese practice of treating illness by inserting needles into the body called,Acupuncture,General,What Philippine volcano erupted in 1991,Mt pinatubo +Music,Number of weeks Beatles albums have spent at No. 1,147,Sports & Leisure,How is British sportsman Francis Thompson more usually known? ,Daley Thompson ,General,"The greyhound, along with this smaller relative, is used in the sport of coursing?",Whippet +General,In Which European City Will You Find The Headquarters Of Interpol,Leon, Geography,What is the basic unit of currency for Ghana ?,Cedi,Geography,What country is Phnom Penh the capital of,Cambodia +General,And who wrote the song,Cat Stephens,General,What colour is the cap given to an England cricket player,Blue,General,What is the word for hallucinations & delusions,Schizophrenia +Sports & Leisure,"Liverpool Won Champions League , Who Scored The First 3 Liverpool Goals (PFE)? ","Stephen Gerrard, Vladimar Smicer & Xabi Alonsco ",General,"In The World Of Music How Is ""Saul Hudson"" More Commonly Known",Slash / Guns n Roses,General,What letter ends all Japanese words not ending with a vowel,N +General,Libya achieved independence from which country in 1951?,Italy,General,What country has the worlds most vending machines per capita,Japan,Food & Drink,What B Is A Mexican Dish That Translates Into English As Little Donkey? ,Burrito  +General,A flat bottomed boat on canal or river,Barge,General,Brian Warner is better known as who,Marilyn Manson,General,"What did Einstein call ""the most difficult thing to understand""",Income Taxes +General,What kills 100000 Americans each year,Reactions to meds,Art & Literature,What other name does Stephen King write under,Richard bachman,General,Everyone is familiar with the RCA logo with nipper the dog listening to the rca grammaphone. But the original picture had both the dog & the grammaphone sitting on what,His dead master's casket +People & Places,Which Publisher Was Known As Cap' N Bob ,Robert Maxwell ,General,Saint Lidwina is the Patron Saint of who,Skaters,General,Reverend Marcus Morris founded which UK comic in the 50s,Eagle +Science & Nature,What is a male swine called,Boar,Food & Drink,Who released the following 'edible' album 'Moldy peaches' ,The Moldy Peaches ,General,In the Dickens' novel 'Great Expectations' what was the Christian name of the convict 'Magwitch',Abel +General,"In ballet, a position with one leg extended at an oblique angle while the body is also at an oblique angle.",Écarté,General,A Canadian person loyal to the british crown in the 1780's,Loyalist,General,The largest sundial was constructed in which year,1724 +General,Who sang 'respect',Aretha franklin,General,Which city is on the east coast of australia,Sydney,General,Who taught George Harrison to play the sitar,Ravi Shankar +Geography,What English city does the Prime Meridian pass through,Greenwich,General,In Oxford Ohio its illegal for a woman to disrobe where,Before a mans picture,General,How many pints will the 27 inch Americas cup hold,None - its bottomless +General,The Stoner Case was a Sherlock Holmes play - later what story,The Speckled Band,General,What is the term for a painting of a person,Portrait,General,What is the former name of the Russian city Volgograd,Stalingrad +Food & Drink,Where were fortune cookies invented?,United States,General,What does Good Friday mark? ,The crucifixion of Jesus ,General,A napiform thing is shaped like what,A turnip +General,In MASH who planted the vegetable garden,Father Mulcahy,General,In which city were the 1932 Summer Olympic Games held,Los angeles,General,What is the world's oldest song ?,Shadouf chant +General,What's the capital of western samoa,Apia,General,"As a result of their wearing high leather collars to protect their necks from sabres, as what were the first US marines known?",Leathernecks,General,Give either first name of the author P.G.Wodehouse,Pelham grenville +General,"What 1991 film won best film, actor, actress, director Oscars",Silence of the Lambs,General,What amateur Reached Wimbledon Semi final in 1977,John McEnroe (aged 18),Entertainment,"Fifties rock ""n"" roll was revived by what greased hair, T-shirted, TV frequenting group?",Sha Na Na +Music,What Was The Name Of Bruce Springsteens Backing Group In The 1970's & 1980's,The E Street Band,General,Issac and Willian Fuld invented and patented what in 1892,Oiuja Board French German Oui Ya,Science & Nature,When light waves pass from one medium into another they change direction. This is called _________.,Refraction +General,"Anthropoid (ape) of equatorial Africa that, physically & genetically, is the animal most closely related to humans",Chimpanzee,General,"On British & Us Television How Is ""Jo Frost"" More Commonly Known",Super Nanny,General,An average American consumes 600 what a year,Cans of Soda +General,Traditionally what should be given on a 20th anniversary,China,General,"What is the fibre from the husk of the coconut, now used in soil-less composts, called",Coir,General,Who was lancelot's son in arthurian legend,Galahad + Geography,This Canadian island is the world's fifth largest.,Baffin,Religion & Mythology,Who was the Greek god of prophecy and archery?,Apollo,General,Which portuguese colony reverts to china in december 1999,Macau +General,"Which 1986 film, from the book of the same name by Umberto Eco, tells of a 10th century detective and monk",The name of the rose,General,Who is the greek counterpart of venus,Aphrodite,General,This Tony Award-winning musical is actually a thinly veiled biography of Diana Ross and the Supremes.,Dreamgirls +Entertainment,What do the initials of the band NIN stand for?,Nine Inch Nails,General,Which magazine always features an obituary on its last page?,Rolling Stone,General,What is the pH value of a neutral solution,Seven +General,"In Which 1985 Movie Did Arnold Schwarzenegger"" Play The Character ""Kalidor""",Red Sonja,Music,"In Which Film Does Julie London Sing ""Cry Me A River""",The Girl Can't Help It,General,Which English name produces the most nicknames,Elizabeth +General,Who invented mass car production,Henry ford,General,"The 100 Acre Wood, a fictional place in the 'Winnie the Pooh' stories, is actually inspired from a real place known as 500 Acre Wood in which English forest?",Ashdown Forest,General,Who is popeye's adopted son,Swee'pea +Music,"Which Band Sang Britney Spears ""Baby One More Time"" At Glastonbury 2000",Travis,General,Who is the prime Minister of France,Lionel jospin,General,Who wrote the best selling book on which the film 'Jurassic Park was based',Michael crichton +Entertainment,Which Beatle wrote The Octopus's song?,Ringo Starr,General,Which world saving puppets lived on 'Tracy Island',Thunderbirds,Science & Nature, The __________ is one of the few land animals that does not need water to supplement its food.,Koala +General,Which author created The Saint (both names),Leslie Charteris,General,What do the STP Corporation's initials stand for,Scientifically Tested,Music,Which Singer Made His First Appearance On Adamski's Killer In 1992,Seal +General,"What would be kept in an ""aviary""?",Birds,General,Surrounding or blocking of a place by an enemy,Blockade,General,In medicine what is an Anomaloscope used for,Test for colour blindness +General,What would a car have if its specification included P.A.S.,Power assisted steering,General,Whats another name for tetanus,Lockjaw,General,What is the most unbelievable part of the warren report,Magic bullet theory +General,Where did Napoleon die,St. helena,Music,Who had a hit in Jul 1975 with 'Blanket on the Ground' ?,Billy Joe Spears,General,What is the birthstone for September?,Sapphire +General,Which company brewed harp lager,Guinness,General,What is the traditional gift for a 13th wedding anniversary,Lace,General,45% of wives say husbands do it 5% husbands admit it - what,Snoring +General,6000 American teenagers do it daily - what,Lose their virginity,General,Bibendum is whose real name,Michelin Tyre Man,General,Silent movie star Ben Turpin insured his what for $500000,His Squint +General,"In January 1997, who survived for five days in an upturned boat, in the south Pacific Ocean",Tony bullimore,General,Muscatel literally means what in Italian,With Flies on it,General,What is the cure of disease by faith in divine power,Faith healing +Science & Nature," The fastest of all fish in the sea is the __________, streaming forward at speeds near 68 miles per hour.",Swordfish,Science & Nature, A __________ in the wild usually makes no more than 20 kills a year.,Lion,Science & Nature,Which Bone Is The Scapula ,The Shoulder Blade  +General,What European country has no rail lines,San Marino,General,The opera The Tsar Sultan contains what famous musical piece,The Flight of the Bumblebee, History & Holidays,"Who was the composer of 'The Nutcracker' ballet? (Handel, Bach, Tchaikovsky, Schubert) ",Tchaikovsky  +General,"The Chrysler Concorde, Dodge Intrepid, and Eagle Vision are known as___",Lh sedans,General,"After Solomon's death, the Kingdom of Israel split into two; Israel with its capital at Shechem, and Judah. Which city was the capital of Judah",Jerusalem,Science & Nature,Who Were Chang And Eng Bunker ,The First Documented Siamese Twins  +Music,"What Nationality Was Sylvia Who Had A 1974 Hit With ""Y Viva Espania",Swedish,General,How many one legged popes have there been,None,General,Which Graham Green book and film had Pinky as the star,Brighton Rock +General,What is Warren Beatty's first name,Henry,General,What is the fear of trains known as,Siderodromophobia,General,What's the capital of Montana,Helena +General,How many seconds are in a day ?,86400,General,What is the tibia,Shin bone,Geography,Who Accidently Sailed Around The Cape Of Good Hope In 1488 ,Bartolomeu Dias  +Geography,Which city is known as the Windy City,Chicago,General,What is tatooed on John Bon Jovi`s arm?,Superman, History & Holidays,Which city did Crocket and Tubbs spend most of their time in? ,Miami Vice  +Music,"Who had a UK top twenty hit in 1990 with ""No More Mr Nice Guy""?",Megadeath,General,"The spinning wheel, introduced to Europe in the middle ages, was invented where",India,General,Albrecht Durer drew what - without ever seeing one,Rhinoceros +General, What is the name for a branch of a river,Tributary,General,The name of which plant comes from the Greek meaning 'earth-apple',Camomile,Science & Nature,Who discovered Uranus?,Friedrich Wilhelm Herschel +Entertainment,Who wrote the opera 'Tosca'?,Giacomo Puccini,Music,Chris Montez Had 2 Hits In The 60's What Were They (PFE),"Let's Dance, The More I See You",General,What is the traditional trade of aspiring bullfighters,Bricklaying +Music,By What Archetypal Punk Name Was Drummer Chris Miller Known,Rat Scabies,Music,In Which Year Did Phil Collins Join Genesis,1970,General,Who was the first Canadian woman to swim across Lake Ontario,Marilyn bell +Sports & Leisure,"In which sport is a ""hole_in_one"" possible",Golf,General,How long is the Danube River,"1,750 miles",General,What is a young pigeon called,Squab +General,In the Muppet band Zoot plays which instrument,Saxophone,Sports & Leisure,"What sport do the following terms belong to - ""Jerk & Snatch""?",Weightlifting,General,What is the product for the slogan 'silly rabbit',Trix cereal +General,"What actor said, ""love means never having to say you're sorry""",Ryan o'neal,Music,What Was Talking Heads First Top Ten Album,Little Creatures,General,How many gallons of diesel does the cruise liner 'queen elizabeth ii' use for each six inches it moves,One gallon + History & Holidays,In which 1998 remake did Matthew Broderick play Dr.Niko Tatopoulos (Pronounced Tat-Oh-Pool-Loss) ,`Godzilla' ,General,"During supersonic flight, what temperature does the skin on the nose of a concorde reach",260 degrees fahrenheit,General,The term warts and all comes from a portrait of who,Oliver Cromwell +General,Who told the evil king Schahriah stories,Sheherazade,General,What does a road sign showing the rear of a car & wavy lines mean,Slippery when wet,General,Who only author to have a book in every Dewy-Decimal category,Isaac Asimov +Music,What Prince Single Was Also The Name Of His Semi Autobiographical Film,Purple Rain,Geography,How many avenues radiate from the arc de triomphe ,Twelve ,General,"In the tv series 'seinfeld', who plays kramer",Michael richards +General,What is the fear of bearing a deformed child known as,Teratophobia,General,"If venus is the second planet from the sun, what is jupiter",Fifth,General,Which comet struck Jupiter in July 1994,Shoemaker-levy + Geography,What is the capital of Chile?,Santiago,General,"What was the name of the robot girl on ""Small Wonder""?",Vicki,Entertainment,What did Peppermint Patty always call Charlie Brown,Chuck +General,Beethoven's fifth piano concerto is nicknamed what,The Emperor,Art & Literature,Who Wrote The Barsetshire Novels? ,Anthony Trollope ,Music,Who Celebrated His 50th Birthday In 1997 In New York With Lou Reed And The Cure,David Bowie +Sports & Leisure,"What sport do the following terms belong to - ""Pull & Lolly""?",Cricket,Music,Number of Beatles songs with a woman's name in the title,18,Sports & Leisure,In Which Sport Is The Brit (Mucky Maureen) From Barnsley The Current World Champion ,Mud Wrestling  +General,By law in China to go to school you must be what,Intelligent,Entertainment,What was George of the Jungle always running in to?,A tree,General,Which other left wing faction did the Bolsheviks defeat to take control after the Russian revolution,Mensheviks +Music,"Which British Female Singers Real Name Is ""Gaynor Hopkins""",Bonnie Tyler,Science & Nature,What Is A Red Admiral? ,Butterfly ,Geography,Frankfort is the capital of which state,Kentucky +General,Who was bruce lee's son,Jason lee, Geography,What is the capital of Laos ?,Vientiane,General,If you saw Cave Canem written what would you know,Beware of the Dog +Sports & Leisure,What Did Both Arsenal & Chelsea Have In 1928 That No Other Team Had? ,Numbered Shirts ,General,Saturday is named for which planet,Saturn,General,What is the latin Olympic motto?,Citius Altius Fortius +Music,Three Stars A Hit For Runy Wright In Britain And Tommy Dee In The States Relates To The Death Of Which Singers,"Buddy Holly, Big Bopper, Richie Valens",Art & Literature,Where Would You Find The Elgin Marbles ,The British Museum ,General,Who spoiled Muhammand Ali's 1980 comeback,Larry holmes +Geography,Which Is The Largest Country In Africa ,Sudan ,General,In China its 19 grams but in Denmark 42 grams average what,Weight of mans testicles,General,Who is the roman counterpart of aphrodite,Venus +General,Which film covers the life of John Reed,Reds,Music,What Fact Of Life Did Jewel Akens Tell Us About In 1965,The Birds And The Bees, History & Holidays,What computer game featured a disco leftover looking for love? ,Leisure Suit Larry  +General,Of what is keratitis an inflammation,Cornea,General,A small hound used for hunting hares,Beagle, History & Holidays,In 1969 Richard Cawston Made A Documentary For The BBC About Which Famous Family ,The Royal Family  +General,Djibouti is the capital of ______,Djibouti,Music,"Which American Band Had Their Biggest UK Success With A Spiky Version Of The Rolling Stones ""Satisfaction""",Devo,General,"What song from Bambi begins"" ""Drip, drip, drop""",Little april showers +General,"What group's top selling lp was ""rumors""?",Fleetwood mac,General,"Because Moses felt he was ""slow of speech and slow of tongue"", who often acted as his spokesperson",Aaron,General,What typewriter brand was invented by a man whose father made a well known flintlock rifle,Remington +General,What do all the seven dwarfs except dopey have,Beard,General,Someone who talks too much is said to 'talk the hind legs,Off a donkey,General,Ignoring obvious what links Minotaur Harpy Centaur and Sphinx,All part human +General,Gondolas in Venice are traditionally what colour,Black, History & Holidays,When is the 'Feast of Stephen'? ,December 26th ,General,Greek mythology what underground river souls drink and forget,Lethe +General,What is the deepest lake in the world,Lake baikal,Science & Nature,What is the hardest bone in the human body?,Jawbone,General,"After she recorded ""the nitty gritty"" this lady released the rhyming ""name game""?",Shirley ellis +General,Whose only novel was The Cardinals Mistress,Benito Mussolini,Music,The Singer Song Writer Damon Gough Performs Under What Name?,Badly Drawn Boy,Music,"In The Scorsese Movie Of The Same Name Who Sang ""New York New York""",Liza Minelli +General,With what are mushrooms often confused,Toadstools,General,Who entered a contest to find his own look-alike and came 3rd,Charlie Chaplin,General,Which lawyer broke the law by refusing to be finger-printed in the Transvaal during 1907,Gandhi +General,Victoria Australia law illegal wear pink what after noon Sunday,Pink Hot Pants,General,What TV did 44 million USA watch while 27m Eisenhower sworn in,I Love Lucy,General,Which female name means bright or illustrious,Clare or Clara +General,What sort of ship has a runway on its deck,Aircraft carrier,Music,"Name 2 Of The Three Artists Who Had ""Rubber Ball"" In The Charts In 1961","Bobby Vee, The Avons, Marty Wilde",General,In Helsinki Finland what's the police alternative to parking tickets,Deflate cars tyres +General,"The literal translation for the swedish word 'smorgasbord' is ""board for ______________(two words)",Buttered bread,General,Who painted The Night Watch and The Mill,Rembrandt,Music,"Which singer has appeared in films playing characters called ""Danny Fisher"", ""Glenn Tyler"" and ""Lucky Jackson""?",Elvis Presley +General,At the Alamo its illegal to drop what nut shells on the ground,Pecan,Music,In which American City did Motown music begin?,Detroit,Sports & Leisure,In 2006 Who Was Named As The Highest Paid Female In The World Of Sport? ,Maria Sharapova  +Music,"Who Recorded The Albums ""Toys In The Attic"" & ""Draw The Line""",Aerosmith,General,Name first female top US single / album charts simultaneously,The Singing Nun Sis Luc Gabrielle,General,What do airplane mechanics refer to as 'pickle juice',Motor oil +General,Which element is extracted from the ore Sphalerite,Zinc,General,"A bell tower, usually not actually attached to a church.",Campanile,Science & Nature,What Flower Is Depicted On The Flag On India ,The Lotus  +Tech & Video Games,"In the original Contra, what gun would ""S"" get you? ",Spread Gun,General,"Which group's first single didn't even scrape the bottom of the top 40, but their second, '867-5309/jenny' peaked and went gold in 1982",Tommy tutone,General,St Boniface is the Saint of what,Sodomy +General,What was the worlds highest structure until 1930,Eiffel Tower,General,What U.S. state has the worlds champion chili cookoff every year,Texas,General,What was the name of inn in Treasure Island,Admiral Benbow +General,Lahnaphophobia is the fear of what,Vegetables,Music,In The World Of Music Ziggy Stardust Is Otherwise Known As Who?,David Bowie, History & Holidays,Which famous Arab / Israeli war took place in 1973? ,Yom Kippur war  +Entertainment,Who was always trying to get rent from Andy Capp?,Percy,General,What was the first ready-to-eat breakfast cereal,Shredded cereal,General,In Norse mythology Tyr is the god of what,War +General,What does i.r.c stand for,Internet relay chat,General,A giraffes eyelashes are what colour,Black,General,A game in which small balls are struck into holes on inclined board,Bagatelle +Tech & Video Games,Samus Aran is the femme-fatale of which series of games? ,Metroid,General,What term is used to describe the process of extracting poison from snakes,Milking,Sports & Leisure,What is the final event in a decathlon? ,1500 metres  +General,What is the oldest known alcoholic beverage,Mead,General,"A ""cameleopard"" is an archaic term for what animal",Giraffe,General,Who did general francisco franco designate as his successor in spain in 1969,King juan carlos +General,Which Gilbert and Sullivan opera is subtitled 'The Merryman and His Maid'?,The Yeoman of the Guard,Geography,Which British County Has The Longest Coastline ,Cornwall ,General,In ancient Athens every third man worked with what,Marble +General,What caused the Crab nebula,Supernova,Geography,What is the capital of Saudi Arabia,Riyadh,General,What was the name of the helicopter on Riptide?,The Mimi +General,The only place in the world where one can see the sun rise on the Pacific Ocean and set on the Atlantic is in which country?,Panama,General,Carter what do goldfish lose if kept in dimly lit or running water,Colour,General,"What's the international radio code word for the letter ""P""",Papa +General,Who played Commodious in the Oscar winning Gladiator,Joaquin Phoenix,General,The only McDonald’s restaurant in the world built on a horse-racing course is in?,Hong Kong,Religion & Mythology,Who was Ancient Egyptian moon god ?,Khonsu +General,"Of which country was Faisal, assassinated in 1975, king",Saudi arabia,Music,Amount of time photographer Iain Macmillan was given to shoot the cover of Abbey Road,10 Mins,General,We have used a cassette but what does it literally mean,Small Box +General,Where are the crown jewels kept,Tower of london,General,"In mythology, who was turned into a laurel bush",Daphne,General,Prescribed as cure Beri Beri it cured scrotal dermatitis - what,Marmite +General,"A style of English architecture prevalent from 1485-1558 transitional between Gothic and Palladian, with emphasis on country manors.",Tudor,General,After the US civil war what was known as the soldiers disease,Morphine addiction,Geography,"The town of Hamilton, Ontario, Canada, is closer to the equator than it is to the _____________",North pole +General,Which film starring Julie Andrews and Christopher Plummer won the Oscar for best picture in 1965?,The Sound Of Music, History & Holidays,What was the name of Plato's school?,Academy,General,What is the captain of a yacht usually called,Skipper +General,In the original version of the fly what was the fly saying as the movie ended,Help me,Science & Nature,What is the scientific name for earth's outer layer of surface soil or crust?,Lithosphere, Geography,In which state is Walla Walla?,Washington +Geography,In which state is Mount St. Helens,Washington,General, What is the tombstone inscription called,Epitaph,General,How many Wimbledon titles does Billie-Jean King possess,20 +General,What coverted Greek items lie in the British Museum,The elgin marbles,General,The Spanish abbreviation for ufo is what,Ovnis,General,Which childrens classic was written by Anna Sewell,Black beauty +General,Which Cartoon Character Made Their Tv Debut In 1935,Porky The Pig,General,"In 1877, Edison demonstrated his first gramophone with a recording of himself reciting which nursery rhyme",Mary had a little lamb,Science & Nature," __________ are the smallest breed of dog used for hunting. They are low to the ground, which allows them to enter and maneuver through tunnels easily.",Dachshunds +General,What is the name for a male ferret,Jack,General,In North Africa a mouflon is a wild what,Sheep,General,Large wicker en-cased bottle,Demijohn +General,What was the name for british military aviation during world war i,Royal,General,What were the 3 Chipettes names on Alvin and the Chipmunks?,"Brittney,Jeanette,and Eleanor",General,"Amnesic, Diarrhetic and Paralytic main types of what poisoning",Shellfish +General,What does the name Stephen mean - from the Greek,Crown,General,Name only non electrical musical instrument invented 20th cent,Steel Drums,General,On what common item would you find a harp,Lamp metal part supporting shade +General,"Hathor Egyptian, Branwen Celtic, Frigga Norse Goddess of what",Love,General,What title to the Ambassadors of Britian Get given?,Ambassador to the Court of Saint James,Science & Nature,Which element is also known as Quicksilver ?,Mercury +General,In Greek what does the word climax mean,Ladder,Food & Drink,Petit Fours means literally 'little ovens'. What are they? ,Small french cakes or biscuits ,General,"On which river is linz, austria a leading port",Danube +Science & Nature,A mouth-like opening into the body; also the porous openings on the surface of leaves.,Stomata,Food & Drink,What Is Tofu Also Known As ,Bean Curd ,Sports & Leisure,What Number Lies Between 15 & 6 On A Dartboard ,10  +General,What Is The English Equivalent Of OVNI In Both France And Spain,UFO,General,Who is the Jolly Green Giant's helper,Little green sprout,General,Which German town hosts the annual Wagner Festival,Bayreuth +General,What did the acronym VHS stand for in the former standard system used for TV video recording equipment?,Video Home System,Music,Monica & Gabriella Make Up Which Pop Duo,The Cheeky Girls,General,When Yuri Gagarin Became The First Man In Space What Was The Name Of His Spacecraft,Vostok 1 +Science & Nature,The Word Ophidian Refers To What Sort Of Animal ,Snake ,Food & Drink,Which two popular fruits are anagrams of each other? ,Lemon and Melon ,General,What Popular Artist In 2004 Released The Album Slicker Than Your Average?,Craig David +Science & Nature, Only tom turkeys __________. Hen turkeys make a clicking noise.,Gobble,General,Camille Pizarro the impressionist was born in which country,West Indies,General,In which county would you find Fuqing,A port in Taiwan +General,Who did author leslie charteris create,The saint,Geography,In which state are gettysburg and the liberty bell ,Pennsylvania ,General,Pocahontas was baptised and given what English name,Rebecca +General,Which british leader was born in a ladies' room during a dance?,Winston Churchill,Science & Nature," A woodchuck breathes only ten times per hour while __________, while an active woodchuck breathes 2,100 times an hour.",Hibernating,Music,Who played stand-up bass on Elvis' songs for Sun Records?,Bill Black +Sports & Leisure,How Many Straight Lines Will You Find On An English Football Pitch? ,17 ,General,What city has the most canals,Birmingham,General,What symbol appears on the tails of Qantas jets,Flying kangaroo +General,"Which Labour politician, according to Harold Wilson, ""immatures with age""",Tony benn,Music,"Which Jamaican Band Rose To Brief Fame With ""Black & White"" & ""Moon River""",Greyhound,General,What does a crapulent person suffer from,Drunkenness +Music,"Who Recorded The Albums ""Nevermind"", ""Incesticide"", An ""In Utero""",Nirvana, History & Holidays,Ritchie Valens and The Big Bopper were killed in a plane crash in 1959. Which other famous singer was killed in that crash?,Buddy Holly,General,"What kind of music does an ""MOR"" radio station play",Middle of the road + History & Holidays,In What Year Did Neil Armstrong First Step Foot On The Moon ,1969 ,Music,Who had 90's hits with 'Julia Says' & 'If I Never See You Again',Wet Wet Wet,General,Who made wings for himself & his son to escape from the island of Crete,Daedalus +General,What kind of animal has a tail pinned on it in a birthday party game,Donkey,General,"Part of a womans dress, above the waist",Bodice,General,Who became World Speedway Champion in 2013?,Tai Woofinden +General,"In The Harry Potter Movies What Is ""Ron Weesley's"" Middle Name",Bilius,General,John Wayne was nicknamed duke - but after what,His favourite dog, History & Holidays,In 1974 who was sacked from his position of England football manager? ,Alf Ramsey  +General,Which is the world's largest monolith,Mount augustus,General,In what Australian state would you find Wangaratta,Victoria,Food & Drink,"In Australia, what are a 'middy' and a 'schooner'? ",Glasses of beer  +General,In what film did alec guinness play eight parts?,Kind hearts & coronets,Food & Drink,"If I'm mixing you a drink using ginger ale, grenadine and a cherry, what am I making? ",Shirley Temple ,Geography,_____________ itself was formed by the activity of undersea volcanoes.,Hawaii +General,Gene Hackman starred in the French Connection what was the name of the character he played,Popeye doyle,General,"Bauxite is the principal ore of Aluminium, which other element is combined with the metal in this ore",Oxygen,General,"Mlb: he said ""if i ain't startin' i ain't departin'"" when named an all-star",Garry templeton +General,Name the actor who plays the police pathologist Quincy,Jack klugman,General,In the comic Fantastic Four what was Things character name,Benjamin Grimm,General,In ancient Rome what was the triclinium?,Dining Room +General,Which game was illegal in Elizabethan England,Bowls,General,Where is frostbite falls,Minnesota,General,Who starred in 'conan the barbarian',Arnold schwarzenegger +General,What is used to make a classic wiener schnitzel,Veal, Geography,Near what major city is Mount Fuji?,Tokyo,Music,Who Also Succeeded Under The Name Shane Fenton,Alvin Stardust +General,National drink of Peru,Pisco,General,1994 Christies sold what piece of Elvis memorabilia for $41400,Amex credit card,General,What do the locals call the cloud that covers Table Mountain in Cape Town,Tablecloth +General,What is the name of an animal that can pass on bacteria without being affected by the disease itself,Vector,General,Phoebe Anne Mozee better known as who,Annie Oakley,General,What peninsula does Denmark occupy,Jutland +Music,Which Is The Most Broadcast Record Ever And Has Held This Record Since 1977 And Why,"The Carpenters / Calling Occupants, Because It's Broadcast By N.A.S.A Every 3 Minutes",General,Bambi Woods Was The Central Character In Which Classic Movie Of The 1970's,Debbie Does Dallas,General,A person refusing to join a strike,Blackleg +Science & Nature,What Frightened Miss Muffet Away ,A Spider ,General,The Newbery Medal is given annually for what,Best Children's book,General,"Countries of the world: western coast of Africa, the capital is Porto-novo",Benin +General,What medical treatment was first used by chinese emperor shen-nung in 2700 bc,Acupuncture,General,Which of the seven hills of Rome gives its name to the Italian presidential palace,Quirinal hill,General,Which principality has been ruled by the Grimaldi family since the 15th century,Monaco +General,Liam Devlin often appears in novels by which author,Jack Higgins,General,What does a phyllophagus animal eat,Leaves,General,What is a group of coots called,Covert +General,"Who claimed that, in the Garden of Eden, God spoke Swedish, Adam spoke Danish, & the serpent spoke French",Swedish philologist,General,How many times was johnny carson married,Four,General,What is the international cry for help,Mayday +General,Seattle which is the largest theme resort hotel,Lost city,General,What is a group of this animal called: Goldfince,Charm,General,"The Song “ Don't Go Breakin My Heart ” Was Performed By Elton John & Kiki Dee But Can You Tell Me What Was Kiki Dee's Profession, Before She Became Famous",Prostitute +General,Urea is essentially made up of carbon dioxide and which other chemical,Ammonia, Geography,What is the basic unit of currency for Niger ?,Franc,General,International dialling codes what county is 20,Egypt +General,Ranma's father in Ranma 1/2 is transformed into this animal,Panda,General,What is earth's galaxy called,Milky way,General,What is the legal term for a formal agreement enforceable by law,Contract +General,Name either of the last men on the Moon (last name only),Cernan schmitt,General,Whats the name of Jacques Cousteaus research ship,Calypso, History & Holidays,What volcano destroyed Pompeii,Vesuvius +Science & Nature,What Is A CarCinogen ,A Cancer Producing Substance ,General,What product sells best in US supermarkets 98.2% of shoppers,Toilet paper,General,The French call it The Casserole what do we call it,The Big Dipper + History & Holidays,Who directed the 1976 movie 'The Omen' - The first part of the omen trilogy ,Richard Donner ,General,The Muses were the goddesses of poetry and song. How many of them were there,Nine,Science & Nature,"A shallow dish with a cover, used for science specimens is a(n) ________.",Petri dish +General,What is an arras,A wall hanging e.g. tapestry,General,What did composer Berlioz originally study,Medicine,General,"What was the nickname of bank robber Charles Floyd, killed in a shoot-out in 1934",Pretty boy +General,In what country did the rumba originate,Cuba,General,In which 1960 film did Elvis Presley play a mixed race character,Flaming Star,General,Van Gogh started to draw at the age of?,27 +Geography,_________________ has two official national anthems.,South africa,General,"The song ""Trail of the Lonesome Pine"" appears in which Laurel and Hardy film",Way out west,General,Who won the Nobel peace prize in 1964,Martin luther king jr +Science & Nature,What term is applied to ethyl alcohol that has been treated with poison to make it unfit for human consumption?,Denatured,General,______ is the brand name of morphine once marketed by bayer?,Heroin,General,Lazy Susans are named after who,Thomas Edison's daughter +General,Which film links Harrison Ford and novelist Scott Turon,Presumed innocent,General,Who's current album is entitled 'Sing when you're winning',Robbie williams,General,Where did Hamlet send his girlfriend Ophelia,A Nunnery +General,Us Captials - Texas,Austin,General,Mussolini invaded this country in 1935,Ethiopia,General,What eighties TV show starred Bruce Willis in a detective agency?,Moonlighting +General,"What are banon, mimolette and samosoe types of",Cheese,General,"Football Team, wolverhampton ______",Wanderers,General,Who speaks romany,Gypsies + History & Holidays,"In 1978 which famous comedy returned for a second series, four years after the first had been shown? ",Fawlty Towers ,General,What word is given to the distance within which a sound can be heard,Earshot,General,Which Biblical heroes name meant splendid sun,Samson +General,In 2001 Boy George Performed On A Celebrity Edition Of Stars In Their Eyes But Who Did He Impersonate,David Bowie,General,"Beethoven, Brahms, Chopin, Handel, Liszt, Ravel what in common",Bachelors,Religion & Mythology,"In Greek mythology, who did Minos hire to construct the labyrinth?",Daedalus +Music,Give The Full 8 Word title Of The 1975 Classic From Steve Harley & Cockney Rebel,Come Up And See Me Make Me Smile,General,What is the world's warmest sea,The Red Sea, History & Holidays,What Was The Goverment Of Oliver Cromwell Called? ,The Commenwealth Goverment  +Music,"In The World Of Music Who Am I ""Gergios Kyriacos Panayiotou""",George Michael,General,What is the flower that stands for: education,Cherry tree,Geography,What is the capital of Denmark,Copenhagen +General,Which worlds city is known as The Golden City,Prague Czech,Science & Nature,What Does WYSIWYG Mean In The Field Of Computing? ,What You See Is What You Get , History & Holidays,"""In Charles Dickens' """"A Christmas Carol"""", how many different ghosts visited Scrooge?"" ",4  +General,Richard Attenbourough and wife were the first leads in what play,The Mousetrap,General,What kind of nuts were originally used in the old shell game,Walnuts,General,Trismus is a muscular spasm where in the body,Jaw - like lockjaw + History & Holidays,Name The British Liner That Sunk On Its Maiden Voyage In 1912? ,The Titanic ,Music,"""Kiss From A Rose"" Was A Hit In 1994 And 1995 For Whom",Seal,Sports & Leisure,Which Sport Is Played Over 4 Periods Of 15 Minutes Where Only Two Of The Seven Players Can Score? ,Net Ball  +General,"The still of the nite what's the location of the ""circus hall of fame""",Sarasota florida,Sports & Leisure,The Ski Resort Of Zermatt Stands At The Foot Of Which Swiss Mountain ,The Matterhorn ,General,Which two man series of space missions preceded the Apollo missions,Gemini +General,In Britain pool and snooker players call it side - what USA name,English,Food & Drink,The Commercial distribution of which meat was banned by law in Germany in 1986? ,Dog meat - (allowed since 1941) ,General,"Who was the American millionaire, given an honorary knighthood in 1980",John paul getty jnr +General,What is the latin name for the top set of vertebrae,Cervical,General,As what is the north star also known,Polaris,General,Who was the director of the cult film 'Casablanca'?,Michael Curtiz + History & Holidays,"What was The poem A night before christmas originally called (The Night Visitor, Santa Claus Is Coming To Town, A Visit From St Nicholas, A Christmas Visit) ",A Visit From St Nicholas ,Music,Who Was The First Non British Act To Perform A “James Bond Theme”,Nancy Sinatra / You Only Live Twice,General,Egg beaters operate on what type of gear,Bevel +General,Satori is a term in which religion,Zen Buddhism,General,How did Buddy Holly die,Aeroplane crash,General,In Bali they observe noebi a day of what,Silence +General,What name is given to the abominable snowman ?,Yeti,Entertainment,Miss Buckley is secretary to what commanding officer?,General Halftrack,General,What is the world's longest tunnel?,The Water Supply Tunnel +General,What is the current vat rate in south africa,14%,Geography,What country is known as the Hellenic Republic,Greece,General,In boxing what comes between bantam and light weight,Feather weight +General,In which traditional pantomime does Dandini appear,Cinderella,General,What is the name of the people's car,Volkswagen,General,Seen on Egyptian rivers what is a Shadoof used for,Water bucket on pole +General,Which singer is the son of former bandleader and singer Ross McManus,Elvis costello, Geography,What is capital of Ukraine?,Kiev,Music,Which Queen Album Went Straight To No.1 In The Charts In 1995,Made In Heaven +General,In which 1916 film did seena owen play,Intolerance,General,"What was the original title of the ""let it be"" documentary movie",Get back, History & Holidays,Who was the mother of James I of England (VI of Scotland )? ,Mary Queen Of Scots  +General,What book was banned in Ireland in 1932,Brave New World – Aldus Huxley,General,"The karyotype describes the number, shape and size of which structures within a cell",Chromosomes,General," This instrument is used for measuring the distance between two points, on a curved surface.",Calliper +General,Mother Carey's Chickens sailors slang for what bird(s),Storm Petrels,Food & Drink,If you ordered a dish of Mountain Oysters or Cape Cod Turkey what would you receive? ,Sheep's testicles ,General,What river provided water to feed the Hanging Gardens of Babylon,The +General,In what part of New York did the Great Gatsby live,West Egg,General,Cary Grant and Noel Coward both rejected which famous role,James Bond,General,Where in London is Rotten Row,Hyde park +General,What product ranks number one in consumer brand loyalty,Cigarettes,General,The French musical instrument The Viola has what other name,The Alto,General,Where was the 1999 rugby world cup held,Wales + History & Holidays,John F. Kennedy Airport in New York used to be called __________.,Idlewind,General,37% of women prefer what to sex,Shoe shopping,General,What was the former name of Burkina Faso in Africa,Upper volta +General,Where was Salman Rushdie born,Bombay,Music,"How Old Was Smokey Robinson When The Miracles Released ""Got a Job""",It Was Released On His 18th Birthday,General,What year was DNA first determined,1953 +General,Who was the villain in 'star wars',Darth vader,General,Which King was the intended target of the Gunpowder plot,James 1st,Sports & Leisure,"In Rhythmic Gymnastics Competitors Have Five Pieces Of Apparatus The Rope, Hoop, Ball, Club And What Else? ",Ribbon  +General,Who was shot as he left the Washington Hilton in 1981,Ronald reagan,Entertainment,"Which actress won the 2002 Academy Award for best actress in a leading role, for her part in the movie, 'Monster's Ball'?",Halle Berry,General,Who is the original master of the Shakespearean jester Lancelot Gobbo,Shylock +Music,What Was Elton John's Uk Hit Single,Your Song,General,If you were misocapnic what do you hate,Tobacco Smoke, History & Holidays,How Old Was Playboy Founder Hugh Hefner When He Launched The First Edition Of 'Playboy'' ,27  +Geography,By How Many Degrees Does True North Vary To Magnetic North In Britain ,8 Degrees West ,Science & Nature,What gland secretes fluid that washes the eyes?,Tear gland,General,What is the chemical symbol for oxygen,O +General,Who was known as the 'Peekaboo Girl'?   ,Veronica Lake,General,An empelomaniac has a desire to do what,Hold public office,General,"Who appears on the 5,000 dollar (US) note?",James Madison +Sports & Leisure,Which British athlete's autobiography is entitled (A Time To Jump)? ,Jonathan Edwards , History & Holidays,In The Song 'The Twelve Days Of Christmas' My True Love Brought Me Nine What ,Ladies Dancing ,General,What was Norman Bates hobby in Psycho,Stuffing birds +General,Who produces the male fragrance EaU.S.auvage,Christian dior,General,"Which actor played 'Marcus Welby, MD'",Robert young,Music,"Which Eastender Reached No 10 In 1975 With ""The Ugly Duckling""",Mike Reid +General,According To Experts In Evolution Should The Human Rave Become Extinct What Creatures Do They Think Will Be The Next To Rule The Planet,Bumble Bee's, Geography,What is the capital of Marshall Islands ?,Dalap-Uliga-Davrit,General,Which author wrote 'The Cruel Sea'?,Nicholas Monsarrat + History & Holidays,General Sherman burned this city in 1864.,Atlanta,Music,Which 3 Classic Tracks Appeared On Free's 1978 EP,"All Right Now, My Brother Jake, Wishing Well",General,Who wrote the opera La Traviata,Verdi +General,What does v.i.r.c stand for,Visual internet relay chat,General,Until 1990 what was still legal tender in East Germany,Sausages,Music,Name Micks First Solo Album,She's The Boss +Music,"What Was The Name Of The Band Formed By Sisters ""Ann & Nancy"" Wilson",Heart,Sports & Leisure,Which first division football team has been promoted to play in the Premiership next season? ,Sunderland ,General,Name Clint Eastwoods first film made in 1955,Francis in the Navy -1955 +Food & Drink,Who released the following 'edible' album 'Flaming pie' ,Paul McCartney ,General,Who was the people's commissar of foreign affairs (foreign minister) before Vyacheslav Molotov,Maxim litvinov, History & Holidays,Who was the cult leader of the Waco Siege in 1993? ,David Koresh  +General,What are the names of the two cats in Disney's Lady + Tramp,Si and Am,Food & Drink,"Booze Name: 1 ounce of light rum and the juice of 1/2 lime, top with cola.",Cuba libre,General,In the US what was the first TV test symbol,Dollar sign +Sports & Leisure,What is a GS in netball ,Goal Shooter ,General,The force that brings moving bodies to a halt is ___________.,Friction,General,An artist supports his canvas on a(n) _____.,Easel + Geography,Lome is the capital of ______?,Togo,General,What is blackpool in irish,Dubh linn,General,Who is Eric Twinge better known as?,Bananaman +Mathematics,What is the square root of one quarter?,One half,General,"In The World Of Music How Is ""David Howell Evans"" More Commonly Known",The Edge (U2),General,What do you call the underground systems in both paris and newcastle,Metro +Art & Literature,Who Wrote The Gothic Novel Dracula ,Bram Stoker ,General,What is the fear of bad men or burglars known as,Scelerophibia,Food & Drink,In which country did chocolate originate? ,Mexico  +General,"Who recorded the album ""never a dull moment"" in 1972",Rod stewart,Science & Nature,Which Is The Largest Muscle In The Human Body ,The Buttocks (Gluteus) ,General,What animal lives in a warren,Rabbits +General,A 12 ounce can of soda pop contains the equivalent of 9 teaspoons of what,Sugar,Geography,"Which European Country has a name that literally means, lower lands? ",Netherlands ,General,The name of which of the seven hills of Rome is the origin of the word 'palace',Palatine hill +Sports & Leisure,What Is A Mashie ,A Type Of Golf Club , History & Holidays,Who is the only one who understands John Shaft ? ,His woman ,General,Which car make has a double diamond as its logo,Renault + Geography,What is the capital of Trinidad and Tobago ?,Port-of-Spain,General,What is the flower that stands for: beware of excess,Saffron,Food & Drink,Which mammal appears on the Bacardi Logo? ,Bat  +General,France Chevalier Germany Ritter Spain Caballero what English,Knight,General,What is the common name for lysergic acid diethylamide,Lsd,Geography,Where is Euston Station,London +General,Who is Charlie Browns favourite baseball player (fictional),Joe Shlabotnik,Music,What was the first place the Beatles worked in Hamburg?,The Indra Club,General,What is the name of the tar like substance derived from petroleum,Bitumen +General,A person with a strong desire to steal is a(n) ________,Kleptomaniac,General,What is the Capital of: Reunion,Saint-denis,General,Where does the embryo implant itself in a tubal pregnancy,A fallopian tube fallopian tube the fallopian tube +Music,"In What Year Were Stock, Aitken & Waterman Taking A Ferry Crosss The Mersey",1989,General,In the Bible Jael murdered Sisera using what weapon,Tent Peg,General,What was Babe Ruth's uniform number,Three +Music,Who was Jesus on the LP version of 'Jesus Christ Superstar'?,Ian Gillan,Music,Who Was The First Female DJ On Radio One,Anne Nightingale,General,What bird is the offspring of a cob and a pen,Swan +General,What did john glenn name the first mercury capsule to orbit the earth,Friendship 7,Music,What Was The Name On The Front Of The First Motown Office And Studio Building In Detroit,Hitsville USA,General,Pertussis has what more common name,Whooping Cough +General,Who is the lead singer of 'the doors',Jim morrison,Sports & Leisure,In Which Sport Might You Come Across A Boston Crab ,Wrestling ,General,"What was the name of the guy who couldn't enunciate on ""The Fat Albert Show?""",Mushmouth +General,In 1939 which countries invaded Poland,Germany - Russia,Music,In Which Year Was Roy Orbison The Only American Solo Artist to Be No.1 In The UK,1964,General,"Which European Country Did The Romans Call ""Lusitania""",Portugal +General,What are lime deposits growing up from the floor of a cave called,Stalacmites,General,American Literary Association gives Caldicott medal for what,Illustration,General,What Country As Of 2005 Has The Highest Per Capita (Par Head) Wine Consumption In The World,The Vatican City +Music,"Who Wrote The Songs ""Oh Lonesome Me, Sweet Dreams, & I Can't Stop Loving You""",Don Gibson,General,What oath do doctors take,Hippocratic,General,What word do British people use to describe that which an American would call a 'teeter-totter',Seesaw +People & Places,Where was Albert Einstein Born ,Germany ,General,Which country introduced the worlds first diesel loco in 1912,Germany,General,The French call it pomplamouse what do we call it,Grapefruit +Science & Nature,What name is given to a female calf,Heifer,General,What is the technical name for a jigger of grain alcohol,A Pony,People & Places,"What nationality was the film legend Greta Garbo, Swedish, German or Polish ",Swedish  +General,All my Yesterdays is which actors autobiography,Edward G Robinson,Science & Nature,What Is The Positive Particle In An Atom Called ,A Proton ,General,Name the month in the French Republican calendar which is also the title of a novel by Emile Zola,Germinal +Science & Nature,"Which Creature Had 2 Rows Of Plates Running Down It's Neck, Back & Tail Culminating In Large Spikes At The End ",Stegosaurus ,Food & Drink,What cooking term is given to a garnish of spinach? ,Florentine ,People & Places,What was Madam Tussaud's First name ? ,Marie  +Science & Nature,"In The Food Processing Industry , What Do The Initials MRM Stand For? ",Mechanically Recovered Meat ,General,In which American state is the 'Hamptons' resort area located?,New York,General,Synonymous with obituary; a list of recently deceased.?,Necrology +General,Whose cat was sold for $153000 in an Arizona auction,Adolf Hitler's,General,What is the fear of swallowing known as,Phagophobia,Geography,"The world's biggest meteor crater is located in New Quebec, ______________",Canada + History & Holidays,Where did the Bay Of Pigs take place?,Cuba,Food & Drink,What cocktail is based on rum and lemon?,Daiquiri,General,Why did the state of Indiana ban Robin Hood in 1953,Communist – rob rich +Geography,____________ is one_quarter the size of the state of Maine.,Israel,General,In December 1999 which was the last foreign colony to revert to Chinese rule,Macao,General,What are the two largest cities in scotland,Glasgow edinburgh +General,Teutophobia is a fear of ______,Anything german,General,Denver is the capital of ______,Colorado,General,In 1779 Abraham Darby built the worlds first what,Metal Bridge +Science & Nature,How many large holes are in your head,7,General,In the body where would you find your diverticula,Large Intestine,General,Azote was the original name of what element,Nitrogen +Sports & Leisure,How Many Players In A Rugby League Team ,13 Players ,General,What do you have plenty of if you are hirsute,Hair,General,Where are the Luxemburg gardens,Paris +Toys & Games,"In pool, what color is the eight ball",Black,General,What is the name of the tree of the world in Norse mythology,Yggdrasil,Music,"1976 Proved To Be The Year For This Group With The Song ""Now Is The Time""",Jimmy James & The Vagabonds +General,How does a male koala attract a mate,Belching,General,Boreas is the Greek God of what,North Wind,General,What tennis player had trials with Bayern Munich soccer club,Boris Becker +Science & Nature," A __________ has no color vision, it sees only in black and white. Every part of its field of vision, however, is in perfect focus, not just straight ahead, as with humans.",Squirrel, Geography,What is the capital of Djibouti ?,Djibouti,General,What pop group were dedicated followers of fashion,The Kinks +General,What did Dr John S. Pemberton concoct in a three-legged pot in his backyard in 1886?,Coca Cola,General,From what country does soave wine originally come,Italy,General,Of what did Sigmund Freud have a morbid fear?,Ferns +General,What name from the French to quibble means a no trump hand,Chicane,General,Miso a basic ingredient in Japanese cooking is made of what,Soybean paste,General,It's impossible to sneeze with your eyes ___.?,Open +General,Whats the official language of Morocco,Arabic,Geography,In what country is Lahore,Pakistan,General,"Who sings 'the moon may be high, but i can't see a thing in the sky, i only have eyes for you'",Flamingos +General,Where is the bridge of san luis rey,Peru,General,U.S. Captials - Tennessee,Nashville,Science & Nature," Ostriches are such fast runners, they can outrun a horse. Male ostriches can _________",Roar like a lion +Art & Literature,"What Irish playwright and author, wrote ""The Importance of Being Ernest"" and ""A Picture of Dorian Grey"" among others?",Oscar Wilde,General,Which travel company cater exclusively for the over 50s,Saga,General,Who was the first premier of russia and served from 1917 to 1924?,Nikolai lenin +General,Collective nouns - a hedge of what,Herons,Geography,What country was once known as Gaul,France,General,Which spicy soup literally means 'pepper water',Mulligatawny + History & Holidays,""" Which of the following was not one of the Three Kings? """"Balthazar"""" - """"Melchior"""" - """"Soloman"""" - """"Caspar"""" "" ",Soloman ,General,If you were eating Olea Europea what would it be,Olive,Geography,What is the capital of Cyprus,Nicosia +General,Capital cities: Zambia,Lusaka,General,What was the name of the dog which featured in the Jerome K. Jerome novel 'Three Men in a Boat'?,Montgomery, History & Holidays,"In the 1855 Russian War, what were used to transport Torpedos ?",Kites +Geography,What is the capital of Maldives,Male,General,DNA stands for what,Deoxyribonucleic acid,General,"In 1974, whose first album featured 'can't get enough' and 'ready for love'",Bad company + History & Holidays,What did the person chained to wall in Goonies want? ,A Baby Ruth candy bar ,Science & Nature,What are the only other animals on which the pill works?,Gorillas,Art & Literature,"In 'Romeo and Juliet', who says 'what must be must be'?",Juliet +General,Who wrote Brave New World (full name),Aldus Huxley,General,What 19th century novelist spent his last days as an inspector at New York's Customs House?,Herman Melville,General,In what book is jean valjean,Les miserables + History & Holidays,What Chinese dynasty was overthrown in 1911,Manchu,General,What rivers name translates as river of hate,Styx in Hades,General,"According to mens health magazine, what does the average man do 12 to 20 times a day",Break wind fart +General,What is a group of rooks,Building,General,Which nineteenth century author is buried in Samoa,Robert louis stevenson,General,Pentagon doublespeak what is combat emplacement evacuator,A shovel +General,"In romeo and juliet', who gave a long monologue about queen mab",Mercutio,General,Houston who sang 'rescue me',Fontella bass,General,In the Superman comics name the shrunken city in a bottle,Kandor +Geography,What Nationality Was Vasco Da Gama ,Portugese ,General,Who is said to have brought the Holy Grail from Palestine to England,Joseph of arimathea,General,The ore pitchblende is the major source of which element,Uranium radium +General,"These rabbits are prized for their long, soft fur, used to make very expensive sweaters",Angorra, History & Holidays,What product was the first TV advert advertising ?,Toothpaste,General,"With whom do you associate: ""mom always liked you best""",Tommy smothers +General,Gotcha Was A Headline In The Sun Newspaper On May 4 th 1982 Relating To Which “ Specific ” Event,Sinking Of The General Belgrano,General,What name is given to a circular coral reef,Atoll,Music,"Who Is Regarded As ""The Godfather Of Soul""",James Brown +General,"Which country, per capita, uses the most umbrellas",England,General,"What are Acheron, Cocytus and Phlegethon",Rivers of Hell,General,What is the russian equivalent of the name john,Ivan +General,What is the capital of Belize?,Belmopan,General,"Which composer, born in Russia in 1882, became a French citizen in 1934, and died in New York in 1971",Stravinsky,General,What was the profession of our man higgins,Butler +Music,"The Film ""The Ryan White Story"" Featured A Song Called ""I'm Still Standing"" By Which Male Singer",Elton John,General,Which word describes 2 lines which are always the same distance apart,Parallel,General,What is Ben Matlock's trademark outfit?,A grey suit +General,What was the former name of the Southern African country of Zambia,Northern rhodesia,General,"What was Massachusetts' logical choice for an official state dessert, in 1996",Boston cream pie,General,Why was convict 2599 unusual in Pen State prison 1924,Dog doing life for killing cat +Music,In Which Year Did The Stranglers Achieve Three Top Ten Hits,1977,General,What form of execution did St Stephen the Martyr suffer,Stoning,Sports & Leisure,How many sides does a home-plate have?,Five +General,Who invented fortune cookies,Charles jung, Geography,Where is Lake Maracaibo?,Venezuela,General,The Charleston was indroduced in which year,1925 +General,Phalacrophobia is the fear of what,Going Bald,General,Who was Mallory Keaton's fiance?,Nick Moore, Geography,What is the basic unit of currency for Czech Republic ?,Koruna +Entertainment,"Which band included rock greats Roy Orbison, Tom Petty, George Harrison, and Bob Dylan?",The Travelling Wilburys,General,What license plate number is on the volkswagon on the cover of the beatles' 'abbey road' album,281f,General,What pop singer was born in Lucknow India,Cliff Richard +General,Who is the only man to have been both chief justice and president of the u.s,Taft,General,Name Glen Millers signature tune,Moonlight Serenade,Toys & Games,What would you buy from a Gibbons' catalogue,Stamps +General,"According to Dr. Johnson, what should be well sliced, dressed with pepper and vinegar, and then thrown out as good for nothing",Cucumber,General,Who Appeared In A Series Of Ads For Tesco's In Which He Was Chasing Chickens Round France,Dudley Moore,General,Which western entertainments name literally means go round,Rodeo +Science & Nature, The horned lizard of the American southwest may squirt a thin stream of __________ from the corners of its eyes when frightened.,Blood,General,Who created 'dennis the menace',Hank ketcham,General,In which country is Zug,Switzerland – smallest Canton +General,Of what are xenophobics afraid?,Strangers,Art & Literature,Who Is Noted For His (Nonsense Verse) ,Edward Lear ,Science & Nature,Who Developed The First Aautomatic Telephone Exchange ,Strowger  +General,Who was the last King of Italy,Humbertii umberto ii,General,Which acid dissolves glass,Hydrofluoric Acid,General,In Japan what is Seppuku,Hari Kari - suicide +Mathematics,"If you count from 1 to 100, how many 7's will you come across?",20,Sports & Leisure,Which Country Turned The Five Nations Into Six In Rugby? ,Italy ,General,How many species of frogs are there in the UK,Three +Music,"Who Duetted With Steve Harley On The 1986 Hit ""Phantom Of The Opera""?",Sarah Brightman, History & Holidays,Who banned Christmas in 1647 ,Oliver Cromwell ,General,The second piller of Islam Salah involves what,Daily prayers +General,What product was introduced as a cure for urinatary problems,Pepsi,Science & Nature,Which is the most sensitive finger?,Forefinger,General,Oliver was fed gruel - its made from water and what,Oatmeal +General,Which prime minister's wife created a scandal with her antics,Margaret,General,Name of the road system links 17 capitals in South America,Pan American Highway,Music,"Who Shot Into The Charts In 1983 With Their ""68 Guns""",The Alarm +General,Who played the title role in the film 'The Outlaw Josey Wales',Clint eastwood, History & Holidays,"Which U.S. president said, ""The buck stops here""",Truman,Music,Ma! (He’s Making Eyes At Me) Was A Hit For Which Child Prodigy,Lena Zavaroni +General,Ceres was the Roman goddess of what,Agriculture,Geography,What mountain range separates Europe from Asia,Urals,General,What kind of cat is used in purina(tm) commercials,White persian +General,What is the largest soviet republic,Russian republic,General,Bird with characteristic cry and the habit of laying eggs in other birds nests,Cuckoo,General,What 2 countries share the Khyber pass,Afghanistan and pakistan + Geography,What is the basic unit of currency for Suriname ?,Guilder,General,Whose autobiography was entitled My Wicked Wicked Ways,Errol flynn,General,"In relation to its size, which bird has, understandably, the thickest skull",Woodpecker +General,0191 is the telephone dialling code of which British region?,Tyne and Wear,General,"Name the French artist who died in 1863, famous for his painting Liberty Guiding the People",Delacroix,General,"The compound of this sect was under siege in Waco, Texas.",Branch Davidians +General,"What term was given to the taxing policy brought in by Henry VII's chief advisor, which forced taxes on people regardless of their wealth?",Morton's Fork,General,"What band sang the theme song to ""The Breakfast Club?""",Simple Minds,General,"""Tashkent"" Is The Capital City Of Which Country",Uzbekistan +General,In what film did Bruce Willis play a time travelling criminal,Twelve Monkeys,General,"What kind of pie does ""snow white"" start to bake for the dwarfs",Gooseberry,Entertainment,Who is Donald Duck's uncle,Scrooge +General,What is unusual about a Racoons penis,Contains a bone,General,Where did edam cheese originate,Holland,General,What was the first motion picture to have a synchronized musical score?,Don Juan +General,"Countries of the world:south eastern Europe, major cities include Thessaloniki & Piraeus",Greece,Music,In Which Year Did Procol Harum Open Pandora's Box,1975,General,When Virginia Wade Won The 1977 Wimbledon Singles Final Who Was Her Opponent,Betty Stove +General,In his will who left his wife his second best bed,William Shakespeare, History & Holidays,"Currently the world's longest serving leader, he ousted General Batista in January 1959 - who is he ",Fidel Castro , Geography,What is the capital of South Korea ?,Seoul +General,"Translate into English the opera title ""Die Dreigroschenoper""",The threepenny opera,Science & Nature," __________ turtles may breed for the first time when they are between 25 to 50 years old. This figure varies, depending upon the creature's range and the diet of the maturing turtle.",Green,General,"Which dwarf is the leader of the dwarf's in ""snow white""",Doc +Music,Which Country Did Canadian Born Celine Dion Represent At the Eurovision Song Contest,Switzerland,Sports & Leisure,With which sport is Bjorn Borg associated ?,Tennis,General,Nomadic Arab of the desert,Bedouin +General,What position does a sloth spend its day in,Upside down,General,What Classic Tv Show Had A Tv Theme Entitled “ Make A Difference ” ?,Knight Rider,General,The elevated stronghold in ancient Greek cities.,Acropolis +General,The 900 Days' is a chronicle about what group's siege of Leningrad,Nazi,Music,According To The Beatles What did Rocky Racoon find in his room?,Gideon's Bible,Technology & Video Games,As what was Sony's video recorder known?,Betamax +General,What was 1990s most populous U.S. state,California,General,Mandarin and Peter Pan are which parts of a garment,Collars,General,Names Cook Baker obvious what did a Chandler do,Make Candles +General,What does VAX stand for,Virtual Access eXtension,Music,Which Instrument did Dave Of The Dave Clark Five Play,The Drums,General,Rickenbacker who gave millions of dollars to britain in 1930,Edward s harkness +General,The girls'll go crazy for a___.' what is the name of this ZZ-Top tune,Sharp,Science & Nature,What did Lewis E. Waterman invent in 1884?,Fountain pen,Geography,What is the capital of Armenia,Yerevan +General,Impaired normal blood clotting is the main symptom of which disease,Haemophilia,General,What is an example of a totally untraceable poison,Acetylcholine,General,Pete Sampras Became The First Wimbledon Winner Of The Millenium But Who Dod He Beat In The Final,Pat Rafter +General,What is the name of the eating disorder where binging is followed by deliberate vomiting and purging,Bulimia, History & Holidays,What percentage of pumpkins grown are used on halloween is it A 97% B 98% or is it C 99% ,C 99% ,General,Who coached president eisenhower for tv,Robert montgomery +Sports & Leisure,Where were the 1920 Olympics held ?,"Antwerp, Belgium",General,Broccoli belongs to what family of plants,Cabbage,General,In Gone With the Wind name Ashley Wilkes plantation,Twelve Oaks +Science & Nature,Chemical Element Pa?,Protactinium,General,Who did Babe the pig work for,Farmer Hoggett,General,In The Man with the Golden Gun name Scaramangas assistant,Nick Nack +General,In a Rugby League team which player wears the number nine shirt,The hooker,General,What is the study of whales,Cetology,General,Mitsibushi - now cars - planes during war - literally means what,Three Diamonds +General,Every person has a unique _____ print,Finger,General,Mass murder especially among a particular race or nation,Genocide,General,How Did Susan Brown Make Sporting History In 1981,First Female In The Boat Race +General,Who played the lead role in charlie varrick,Walter matthau,General,What fruit is used to flavour Southern Comfort,Peach,General,Who is the Greek Goddess of the moon,Selene +General,What is the heart rate of the blue whale,Nine beats per minute,General,"In the 'james bond' books, who is m's secretary",Miss moneypenny,General,Who is the spokesperson for the exercise tapes 'Tae Bo'?,Billy Blanks +General,66% of Americans reading on the toilet read what,Readers Digest,General,From Which Country Do Chinese Gooseberries Come From,New Zealand,General,What is the name of the large resort lake in the center of Disneyworld,Buena vista lake +Entertainment,"What movie starred Nicholas Cage and John Travolta, one as a police officer, the other as a villain?",Face Off,General,What is an SUV to off roaders,Sports utility vehicle,General,Who composed 'peter and the wolf',Sergei prokofiev +General,Who takes the starring role in the 1970's TV medical drama Quincy,Jack klugman,General,Siamese Dream' was which chicago-based group's breakthrough second album,Smashing Pumpkins, History & Holidays,"In the 'Twelve days of christmas', how many items in total are sent by 'my true love'?",78 +General,What was Doris Lessing's first novel,The grass is singing,Sports & Leisure,At which venue did Steve Redgrave win his first gold? ,Los Angeles ,General,Thomas Edison demonstrated the electric light for the first time in what year,1879 +Music,"Who Had A Massive Hit With Her 3rd Album Entitled ""Jagged Little Pill""",Alanis Morissette,Geography,What u.s state is the home of the headwaters of the mississippi river ,Minnesota ,General,Greek gods of mythology: which goddess personified the earth,Gaea +Music,The Dickies Sung The Theme Tune To Which Crazy Cult American Childrens Show,The Banana Splits,Sports & Leisure,What is Linford Christies best time for the 100 Metres ,9.87 ,Science & Nature,In Which Country Did Polythene Plastic Go Into Commercial Production In 1939 ,Britain  +Science & Nature,How Many Sides Does A Heptagon Have ,Seven ,General,What is 'grandmother' in yiddish,Bobba,General,In San Jose California where is it illegal to sleep without permit,Neighbour's Outhouse + Geography,What is the capital of Finland ?,Helsinki,General,What was Charlemagne also known as,Charles the great,General,A series of 15 radioactive elements in the periodic table (periodic law) with atomic numbers 89 through 103,Actinide series +General,Who was the jeweller to the Russian Court famous Easter eggs,Faberge, History & Holidays,This word describes the Nazi annihilation of Jews.,Holocaust,General,Who was known as the maid of orleans,Joan of arc +General,Scotch mist is a type of what,Rain, History & Holidays,She was the first woman to swim the English channel.,Gertrude Caroline Ederle,General,What books does me jane read,Tarzan +General,Most people bob for apples what do Adams family bob for,Crabs,Music,"He Was Born In Cologne But Shocked Paris With His Rollicking Music Including The Famous Can Can From His Orpheus In The Underworld, What Was His Name",Jacques Offenbach, History & Holidays,"Bruce Willis spends Christmas fighting terrorists in the 1988 film 'Die Hard'', which American city is this set in ",Los Angeles  +Science & Nature,What does breaking the sound barrier cause?,A sonic boom,General,On a prescription what does PO mean,By mouth,Geography,How Tall Is Everest To The Nearest Thousand Feet ,"29,000 Feet " +General,The King of Barataria alternative name what G&S operetta,The Gondoliers,General,What is an angle called if it is less than 90 degrees,Acute,General,"Who's first book was ""Down and Out in Paris and London""",George Orwell +Science & Nature,How many pints of blood does the average human have in his/her body?,12,General,In Okalahoma City its illegal for a prisoner to wear what,Pink Bikini Underwear,General,In which city is the European Court of Justice located,Luxembourg +General,Who lived in Honalee,Puff the magic dragon,General,Who was born michael bolotin,Michael bolton,General,For what did robert montgomery coach president eisenhower,Television +General,"Every finalest in the 100-meter dash of the last four olympics, has been of what descent",West african, Geography,Which is the smallest independent country?,Vatican City,Science & Nature,What planet boasts the Great Red Spot?,Jupiter +General,Charles Portis wrote the novel of the film John Wayne's Oscar,True Grit,General,Which food item contains the most residual pesticides,Peaches,Food & Drink,Blanket And Honeycomb Are Both Varieties Of Which Food Stuff? ,Tripe  +General,A snake has two penises but only one what other organ,Lung,General,What countries women are most likely to have sex daily,Russia 20% 74% satisfactory,General,Which Fundamental Part Of Computer Technology Was Patented In The Usa In 1961?,Silicone Chip +General,Which bird is the symbol of the RSPB?,Avocet,Music,A spinet is a variation of which instrument?,Harpsichord,General,"Who sang of their ""magic carpet ride""",Steppenwolf +General,If you were indulging in Sciomanchy what are you doing,Imaginary Combat,Sports & Leisure,"In which sport is the term, ""Hang ten"" used",Surfing,General,Who Was The First Ever Presenter Of ITV's World Of Sport,Eamonn Andrews +Science & Nature,What is the study of insects called ?,Entomology, History & Holidays,What seasonal name was given to Bart Simpson's dog ,Santa's Little Helper ,Music,On Whose Short Stories Was Guys And Dolls Based,Damon Runyan's +General,Name for an idler in fashionable society,Lounge lizard,General,What play marked marlon brando's last broadway appearance?,A streetcar named desire,Food & Drink,Which spice is obtained from crocuses? ,Saffron  +General,"In roulette, what number is green",Zero,Music,Whose Album Songbird Became A Posthumous Hit In 2001,Eva Cassidy,General,As what is Polaris also known,North Star +Music,"What Is On The Screen Featured On The Album Cover Of 10cc's ""The Original Soundtrack""",A Cowboy,General,What is the flower that stands for: austerity,Common thistle,Sports & Leisure,Which motorcyclist is known as (Foggy)? ,Carl Foggarty  +General,Phengophobia is the fear of,Daylight sunshine,General,What are young herrings,Sardines,General,Food served nivernaise has what ingredient,Carrots + History & Holidays,What did Sir Howard Carter discover in 1922? ,Tutankhamens Tomb ,General,Who was the bully who terrorized Arnold on Different Strokes?,The Gooch,General,"A ""light year"" measures",Distance + History & Holidays,"What was the nationality of the prisoners in the ""Black hole of Calcutta""?",British,General,"Books who wrote the book ""The Wives of Henry VII""",Antonia fraser,General,Helena is the capital of ______,Montana +General,What is 'mpd',Multiple personality disorder,General,In what epic film did charles laughton play quasimodo,The hunchback of notre,General,What was the name of the South African Prime Minister murdered in 1966,Hendrik verwoerd +General,Florence Nightingale Graham better known as who,Elizabeth Arden,General,From Which Tree Or Shrub Do Pecan Nuts Come?,Hickory,Music,"Which Girl Group Consisted Of ""Esther Bennett, Vernie Bennett, Louise Nurding, Kelle Bryan""",Eternal +General,How many days long is a year on the planet Mercury,Eighty eight,Music,Which 80's Hit Was Dedicated By Her Boyfriend To Christie Brickley,Uptown Girl,General,Can Anyone Tell Me What The Sexual Act Of Felching Involves ,Sticking Rodents Up Your Arse  +Music,Name The Album That Resulted From A 1972 Jam Session With Ry Cooder & Nicky Hopkins,Jammin With Edward,General,What animal can get the disease heaves,Horse,General,Fill in the blank: I'm a little _____ short & stout,Teapot +General,The opera Falstaff was written by whom,Verdi,Science & Nature,What is the meaning of the name of the constellation Cepheus ?,Cepheus,General,Christopher Jones was the captain of which famous ship?,Mayflower +General,Karl Lienstater discovered which medical breakthrough in 1901,ABO Blood Groups, History & Holidays,David Hasselhof spent most of his time driving a car on which eighties tv show? ,Knight Rider ,Science & Nature,What is the troposphere immediately lower than?,Stratosphere +General,Rosencrantz and Guilderstern are dead - name playwright,Tom Stoppard,General,"Brandy, decoy and landscape all come from which language",Dutch,General,In Franz Kafkas Metamorphosis Gregor Samsa wakes up as?,An Insect + History & Holidays,Who Released The 70's Album Entitled Out of the Blue ,ELO ,General,Psellismophobia is the fear of,Stuttering,General,Which island do the nationalist chinese occupy,Taiwan +General,Who wrote the nonsense poem The Jabberwocky,Lewis carroll,General,What is a group of hounds,Cry,Geography,What Does A Dotted Line Represent On An Ordinance Survey Map ,A Footpath  +General,Through what do insects breathe,Spiracles,General,Concordia is the roman goddess of ______,Harmonious relations,General,What are the two cities in charles dickens' 1859 novel a tale of two cities,London and paris +General,What size ball is used in an official Table Tennis match?,40mm,General,Which ship canal by-passes the Niagara Fails,Welland canal,General,What do you have to break to make omelettes,Eggs +General,I what year was Britains' general strike,1926,General,What common item in India are Round,Playing Cards,General,What is the largest inhabited castle,Windsor castle +General,Which dyeing process originated in Indonesia & involves the use of wax masking techniques?,Batik,General,40% of Americans have never been where,To a Dentist,General,How many VCs were awarded in the Falklands War,Two +Music,Which artist produced the Velvet Underground's debut album?,Andy Warhol,Sports & Leisure,Who Quit As Liverpool's Manager On February 22 nd 1991? ,Kenny Dalglish ,Music,Who Had A Number One Hit With Puppy Love In 1972?,Donny Osmond +Science & Nature,How Many Chambers Are There In Your Heart ,4 Chambers ,General,Which character in TV's Red Dwarf is obsessed with fashion,Cat,General,Where did the exxon valdez oil spill occur,Prince william sound +General,Any comparatively small body of land completely surrounded by water,Island,General,Antimacassars were fitted to chairs - what is macasser,Hair oil,General,A cat is feline but what's leporine,Rabbit +General,What is the name of Toys R Us Giraffe,Geoffrey,General,In which 1975 film does Richard Dreyfuss paly a marine biologist called 'Hooper',Jaws,General,In Total There Were 17 Apollo Mission Only One Of Which Resulted In The Loss Of Human Life Which Was It,Apollo 01 (First Mission) +Food & Drink,What is a cross between a blackberry and a raspberry?,Tayberry,General,"What is the only English word that ends in the letters ""mt""",Dreamt,General,Name the first US president to serve ice cream at a state dinner,Thomas Jefferson + Language,What does 'A&W' (of root beer fame) stand for ?,Allen & Wright,Food & Drink,With which flavour is the liqueur Cr?me de Cassis associated? ,Blackcurrant ,Music,Which German composer's best known work is the opera Hansel and Gretel?,Engelbert Humperdinck's +General,Sam Barraclough owned which film star,Lassie,General,Nancy Astor Became The first British What On The 28th November 1919,Member Of Parliament,General,What's the apparent gap between saturn's a and b rings called,Cassini +General,"What is the Capital of: Congo, Dem. Rep. of the",Kinshasa,Food & Drink,What Does 'OG' Stand For In The Brewing Industry ,Original Gravity ,General,The human body has about sixty thousand miles of ______,Blood vessels +General,As what did Kotex first manufactured in WWI,Bandages,General,"What george harrison lp featured the single ""give me love""?",Living in the material world,General,"Segmental, Primitive, Doucine, Elliptical are types of what",Arch (in construction) +Food & Drink,"I belong to the same family as the potato, and more of me are canned than any other fruit or vegetable. What am I? ",Tomato ,General,What imaginary line encircling the earth is 90 degrees from both poles at every point,Equator,Music,Who Had A Hit With The Song Come On Eileen,Dexy's Midnight Runners +General,Organ of the digestive system,Stomach,General,What is the white semicircle on a fingernail,Lunula,General,What battle cry inspired Texas troops after the Alamo fell,Remember the alamo +General,What would you do at a table in Greece,Banking it’s a bank,General,Who refused to shake Jessie Owens' hand at the 1936 summer olympics,Adolf hitler, History & Holidays,What is the nickname of the ship's computer in the 1979 film _Alien_? ,Mother  +General,What is the national flower of scotland,Thistle,General,"Time magazine named what its ""Man of the Year"" in 1982?",Computer,Music,Poor Me Topped The charts For Which Uk Artists In The 1960's,Adam Faith +Science & Nature,Which substance has the chemical formula H2SO4?,Sulfuric acid,General,Where did Indian ink originally come from,China,Music,"Who Is The Lead Singer Of The Band ""Destiny's Child""",Beyonce Knowles +General,Whats the symbol of the zodiacal sign Gemini,Twins,General,"Who wrote ""titus groan""",Mervyn peake,General,What is the world's most popular non-alcoholic organic beverage,Coffee +Geography,What is the capital of Chechnya? ,Grozny ,General,What did denmark sell to the u.s,Virgin islands,General,Is wholemeal bread brown or white,Brown +General,What is a triangle with a 90 degree angle in it called,Right angled triangle,General,On TV what was the name of the Beverly Hillbillies bank manager,Mr drysdale,General,In the Batman comics what is the full real identity of the Riddler,Edward Enigma E Enigma +General,What other common name is given to a rook in chess,Castle,General,What do the initials DIY usually stand for,Do it yourself,General,"1996 3 highest earning sportsman Michael's Jordan, Tyson and who",Schumacher +General,Inciticus was a horse (and Senator) owned by whom,Caligula,Science & Nature,What word is used for a male duck?,Drake,General,A breed of black and white dairy cattle,Friesian +Food & Drink,To What Plant Family Do The Radish & Turnip Belong ,Mustard ,General,Magnetic field Comics: What color suit does Clark Kent always wear,Blue,Geography,What is the capital of Dominica,Roseau +General,Who was the first premier of russia and served from 1917 to 1924,Nikolai, History & Holidays,Who Was The 4th Wife Of Henry Viii ,Anne Of Cleeves ,Science & Nature," The kinkajou's tail is twice as long as its body. Every night, it wraps itself up in its tail and uses it as a __________",Pillow +General,"Dragoon, Antwerp, Poulter, Tumbler, Horseman types of what",Pigeon,General,Andrea Hollen was the first woman in US to do what,Graduate West Point,General,"Recycling one glass jar, saves enough energy to watch t.v for _____",Three +Music,Which Small Faces album originally came in a circular sleeve and later in a round tin?,Ogden's Nut Gone Flake,General,"In Which Tv Show Will You Frequently Hear The Voice Of ""John Briggs""",The Weakest Link,Sports & Leisure,Over What Distance Is A Steeple Chase Run? ,"3,000 Metres " +General,"If you flew due west from portugal, what is the first place you would reach",New york city, Geography,Springfield is the capital of ______?,Illinois,General,"On Happy Days,what animal did Fonzie jump his motorcycle over?",A shark +General,A small computer introduced in 1975 by micro instrumentation telemetry systems of new mexico,Altair 8800,General,By what name is the 2nd day of February called in the U.S.A.,Ground hog day,Music,On Which Interstate Highway Did Chuck Berry Get His Kicks,Route 66 +General,"In ""Innerspace"", what did the license plate on Igoe's BMW say?",SNAPON,General,A Bohemian folk dance in duple time with a hop on the fouth beat. It became a popular ballroom dance in the mid-nineteenth century.,Polka, Geography,What is the basic unit of currency for Yugoslavia ?,Dinar + Geography,What is the capital of Swaziland ?,Mbabane,General,"On Wings,what were the names of the 2 airlines at the airport?",Sandpiper Air and AeroMass,General,"Countries of the world:equatorial country in central Africa, Kinshasa is the capital",Congo +General,What holiday islands have no rivers or lakes - rain water only,Bermuda,General,Who was hebert c hoover's vice president,Charles curtis,General,"Martial Arts Masters ""Ray Park"" Played Which Well Known Movie Character In 1999",Darth Maul (Star Wars Episode I) +General,A pork product that's often served for breakfast,Bacon,General,"Rising about 100 miles northwest of Valencia and flowing east to the Atlantic, which is the longest river of the Iberian peninsula",The tagus,General,Wild marjoram is also known as what,Oregano +General,In Greek legend what was eaten on the Island of Jerba,Lotuses,Art & Literature,"In a general sense, refers to objective representation. More specifically, a nineteenth century movement, especially in France, that rejected idealized academic styles in favor of everyday subjects. ",Realism,Toys & Games,How many balls are used in a game of snooker including the cue ball?,22 +General,In which German city does the annual 'Oktoberfest' beer festival take place,Munich,General,What is the flower that stands for: beautiful eyes,Variegated tulip,Music,"What Song Features The Lyric ""He's Got Crazy Flipper Fingers Never Seen Him Fall""",Pinball Wizard +General,"In computing terminology, what does D.P.I. stand for",Dots per inch,General,The name of which animal means does not drink,Koala,General,What does the latin Olympic motto - Citius Altius Fortius stand for?,Faster Higher Stronger +General,George Jung of Los Angeles in 1916 invented what,Fortune Cookies,General,What does 'karate' mean,Open hand,General,When was Braille printing invented,1829 +Art & Literature,What is the name of Gandalf's horse,Shadowfax,General,A latin American dance usually performed in single file,Conga, History & Holidays,The Cranberries song 'Zombie' was released in which year ,1994  +General,"What cocktail is made from rum, lime and cola",Cuba libre,General,Le Poireau is what type of vegetables,Leek,General,In the Dictionary of Vulgar Tongue 1811 what is a wasp,An infected prostitute sting in tail +General,Tartuffo in Spain Kartoffel in Germany and Russia what is,Potato,General,Out of what did the ancient celts make the jack-o-lantern,Turnips,General, The weight at the end of a pendulum is a(n) ________.,Bob +General,What is the Curia,Administration of the Catholic Church,General,What is the atomic mass of platinum,195.09,Music,Released In 1975 By EMI Name This Monster Hit Written & Performed By An Ex Beatle,Imagine +General,What protruded from the rocks at the end of the film 'planet of the apes',Statue of liberty,General,"Which company claimed to be ""one step ahead of the rest""",Panasonic, History & Holidays,Why did Richard John Bingham make the news in the 1970s? ,Lord Lucan  +General,The typical American eats 263 _____ in a year,Eggs,General,This team won their first world series in 1969,New york mets,General,What would Americans call a spring onion,Scallion +General,What is a group of puppies,Litter,General,What fruits are usually served 'belle helene',Pears,General,Tiger beer is brewed in which Commonwealth country,Singapore +Geography,In which city is Saint Paul's Cathedral,London,General,In the UK today 16000 people die annually from what,Illness caught in hospital,Science & Nature, Adélie __________ employ yawning as part of their courtship ritual.,Penguins +General,In what country would you buy Kingfisher lager,India,General,Cents in a Dollar Pennies in a Pound what in a French Franc,Centimes, History & Holidays,What did Regan due to the striking air traffic controllers? ,Fired them  +General,The addition of what turns a Welsh rarebit into a Buck rarebit,Poached egg,General,Bat Masterson was a sportswriter for what paper,Morning telegraph,General,What was the first film to team jack lemmon and walter matthau,Fortune +General,Who was the mascot of Kellogg's Sugar Frosted Flakes when they were launched in 1942,Tony the tiger,Music,"Who Sang ""Neutron Dance"" From The Movie Beverly Hills Cop""",The Pointer Sisters,Geography,In which Asian country is the Hindu Kush? ,Afghanistan  +General,What did Joseph Gayetty invent in 1857,Toilet Rolls, History & Holidays,Name Queen Elizabeth I's Mother? ,Anne Boleyn ,General,"Which Football Club Founded In 1874 Were Originally Called ""Christchurch FC""",Bolton Wanderers + Geography,What is the capital of Qatar ?,Doha,General,"What country's note pictures 6 eskimos, 2 kayaks and 2 harpoons",Canada,General,Which famous canine cartoon character was created by Hanna-Barbera in 1958 for the first all-animated television series,Huckleberry hound +General,Who links a western gambler and a private eye,James Garner Maverick Rockfort,General,What very lightweight wood is often used for rafts & model aeroplanes,Balsa,General,What are the two official languages of Finland,Finnish and Swedish +General,What tv cowboy taught president sukarno's son how to twirl a six-shooter,Roy,General,What was Hitchcock's last film made in 1976,The Family Plot,General,Where is normandy,France +General,Who became the first Prime Minister of Malawi in 1964,Hastings banda,General,What product orginally sold as 'the esteemed brain tonic & intellectual beverage',Coca cola,General,Roosevelt won the 1932 election - who lost it,Herbert Hoover +General,What was the first team sport played in the modern Olympics,Water Polo,General,Mouselike desert rodent with long hind legs,Gerbil,General,What was the nickname of the basketball player Earvin Johnson,Magic +General,"What literary character pokes fun at his most prominent feature with: ""When it bleeds, the Red Sea""",Cyrano de bergerac,General,Which US state flag is triangular in shape,Ohio,Music,Length of shortest Beatles track,23 Secs / Her Majesty / from Abbey Road +General,Hyelophobia is the fear of,Glass,General,Eppie Cass is a central character in which |novel by George Eliot,Silas marner,Science & Nature,Our galaxy is commonly known called _________.,Milky way +General,What is classified by the A B O system,Blood Groups,General,"The middle part of an entablature, often decorated with sculpture. ",Frieze,Sports & Leisure,Who In 1968 Made What Is Known As 'The Jump Into The 21 st Century'' ,Bob Beamon  + History & Holidays,"He was assassinated on Dec. 8, 1980 in New York City.",John lennon,Music,Which Dance Is Frequently Mentioned In The Wham Song Wake Me Up Before You Go Go,The Jitterbug,Music,What function did the composer Franz Schubert perform at Beethoven's funeral?,He Was A Pallbearer + History & Holidays,Name the U.S.S.R. leader with a birthmark on his forehead? ,Gorbachev ,General,What year were the two atomic bombs dropped on Japan,1945, History & Holidays,In Which Year Was Joan Of Arc Burned At The Stake? ,1431  +General,In the Bible John the Baptist lived on wild honey and what,Locusts,General,What is Milan's opera house called,La Scala,General,Which size of paper measures 210 x 297mm,A4 +General,Tamarack Idaho can't buy what after dark without sheriff permit,Onions,Science & Nature,"To The Nearest Half Pint, How Much Gastric Juice Do You Have ",2.5 Pints ,General,"The tips of fingers & the soles of feet are covered by a thick, tough layer of skin called the what",Stratum corneum +General,The native inhabitants of Australia are called _____,Aborigines,General,Fill in the blank: old Mother _____,Hubbard, History & Holidays,Which comedy trio made a 'Daft Noise For Christmas'' ,The Goodies  +Science & Nature,There are _ planets in this solar system.,9,General,What does a numismatist collect,Coins,Sports & Leisure,How many sides does a baseball homeplate have?,Five +General,Where in the body would you find your olfactory lobes,Nose,Geography,This is the port city serving Tokyo.,Yokohama,General,What was the Statue Of Liberty originally named?,Liberty Enlightening The World +General,"What name is given to the giant American transport plane, the C5",Galaxy,People & Places,Which Hollywood Actress Was Convicted Of Shop Lifting In 2002? ,Winona Ryder ,General,What sense is most closely linked to memory,Smell +General,The national flag of which South American country bears a circle depicting stars in the night sky,Brazil,General,How long was the marquis de sade in prison for sexual offences,27,Science & Nature,Which Stage Of An Insects Life Cycle Comes Between Egg And Pupa ,Larva  +General,"What cartoon characters catchphrase was ""Exit stage left""",Snagglepuss,General,The system of racial segregation in south africa was called _____,Apartheid,General,In 1979 who sang about Walking on the Moon,Police +General,Physics: The color white is the absence or presence of all color?,Presence, History & Holidays,Who directed the 1946 classic 'It's A Wonderful Life'' ,Frank Capra ,Sports & Leisure,Which Wrestler Got His Name From A Tennessee Williams Character ,Big Daddy  +Music,When Did The Beatles First Release Strawberry Fields,1967,General,"What Movie Earned Composer ""John Williams"" His First Oscar For Best Muiscal Score",Fiddler On The Roof,General,Chocolate crisp introduced in 1935 was renamed what 1937,Kit Kat +General,Mario first appeared in which video game,Donkey Kong,General,Which French artist designed ballet sets for Diaghilev,Henri Matisse,General,Which country was the first to introduce old age pensions,Germany +General,What is a group of lions,Pride,General,Which crime fiction writer created Inspector Lynley?,Elizabeth George,General,Which is the largest city of the Balearic Islands,Palma +Geography,Which is the Earth's largest continent,Asia,General,What is Foghorn Leghorn's favourite song,Camptown Races, History & Holidays,Who Made The First Non-Stop Transatlantic Crossing In An Aeroplane? ,Alcock & Brown  + History & Holidays,How many wise men where there? ,3 ,General,"In 1848 Mexico sells U.S. Texas, California, New Mexico and",Arizona,General,Who or what was Black Betsy,Babe Ruth's 44oz Baseball Bat +General,What was pegasus in greek mythology,Winged horse,General,In 1829 Walter Hunt invented what common item,Safety Pin,General,What soft white sweets can be roasted over a fire,Marshmallows +General,Disney's Sleeping Beauty what is the name of the Queen witch,Maleficent, History & Holidays,What did 'DMZ' stand for in the vietnam war?,Demilitarized Zone,Science & Nature," An __________ is nearly 6 feet long, yet its mouth is only an inch wide.",Anteater +Music,"Which British Band Had Hits In 1974, With ""Minuetto Allegretto"" & ""Banana Rock""",The Wombles,General,"The Palio, held in Siena, is what kind of race",Horse,General,Who was dictator of Spain from 1937 to 1975,Francisco franco +General,What is the national airline of Brazil called,Varig,General,What is the flower that stands for: satire,Prickly pear, Food & Drink,What nut is used to make marzipan?,Almond +General,What is the world's most popular spice,Pepper,General,From which musical does the song Surrey With the Fringe on Top come,Oklahoma,Music,"Who Had A 2002 Hit With ""Take Down The Union Jack""?",Billy Bragg +General,Star Wars - Who is C3-PO's sidekick,R2 D2,General,What was the fat boy's nickname in Lord of the Flies,Piggy,General,David kills Goliath in which book of the Bible,Samuel +General,"Who recorded the album ""Get Lucky"" in 1982",Loverboy,General,What country calls themselves Cymru?,Wales,General,What U.S. state includes the telephone area code 517,Michigan + Geography,What is the capital of Togo?,Lome,General,What is the most abundant mineral in the human body,Calcium,General,"Island, southern Indonesia, one of the Lesser Sunda Islands, in the Indian Ocean?",Bali +General,Dickinson where was emily dickinson's home,Massachusetts,General,In what subject did artist George Stubbs specialize,Horses,General,In what film did Sean Connery sing Pretty Irish Girl,Darby O Gill and the Little People +General,What instrument on a car measures distance,Odometer,General,What is a group of monkeys,Troop,Sports & Leisure,Why was Matthew Simmons in the news in 1995? ,He was kung fu kicked by Eric Cantona  +General,In which film did Frank Sinatra win his only Oscar in an acting role,From here to eternity,General,What colour was Ian Flemming's typewriter,Gold,General,In what month did the Russian October revolution take place,November +Science & Nature,In which decade was the Breathalyzer invented? ,1930's ,General,"Which Comedian Owned The 1994 Grand National Winner ""Minnehoma""",Freddie Star,General,What job does Charlie Browns father do,Barber +General,Cagliari is the capital of which mediterranean island,Sardinia,General,Which famous university is in paris,Sorbonne,General,Which African country was founded by Americans,Liberia +Music,Which Single Charted At No.2 For Terence Trent D'Arby In 1988,Sign Your Name,General,Who was the father of Alexander the Great,Philip II of Macedon,Science & Nature," Between the mid_1860's and 1883, the __________ population in North America was reduced from an estimated 13 million to a few hundred.",Bison +Food & Drink,What is the Swedish name for hot or cold dishes served as a buffet? ,Smorgasboard ,General,Who often solved a three pipe problem,Sherlock Holmes,General,In 1983 a Japanese artist copied the Mona Lisa in what material,Toast +Science & Nature,What is a death cap ,Toadstool ,General,What planet does the moon charon orbit,Pluto,General,"If yoU.S.aw a segment of ""Mathnet,"" what show were you watching?",Square One +General,What is the top layer of a wedding cake,Groom's cake,General,"Which Actress Released Her Own Brand Of Perfume In 2008 Entitled ""Lovely""",Sarah Jessica Parker,General,What is the largest city in Texas,Houston +Science & Nature,Whose Job As A Book Binder On An Edition Of The Encyclopedia Britannica Led To The Discovery Of Electricity ,Michael Farraday ,General,"In ballet, a bend from the waist to the side or to the back.",Cambré, History & Holidays,Who Released The 70's Album Entitled Arrival ,ABBA  +General,Which sport is derived from the Indian game of Poona?,Badminton,Entertainment,Savage Garden took 13 nominations and 10 wins at which awards?,ARIA awards,General,How many dots are on a twister mat,30 +Food & Drink,Six ounces of orange juice contains the minimum daily requirement for which vitamin?,Vitamin C,General,From which French word does the 'mayday' distress signal come,M'aidez,General,Which man has the most monuments erected in his honour,Buddha +Toys & Games,"This is the game that ""Ties you up in knots"".",Twister,General,Which French revolutionary was stabbed in his bath by Charlotte Corday,Jean paul marat, History & Holidays,Who was the first man to reach the North Pole ?,Robert Edwin Peary +General,Antanananarivo is the capitol of where,Madagascar,General,The word nylon is made up from what,New York - London,General,"Who wrote the opera ""pagliacci""",Ruggiero leoncavallo +Music,When Did Ike & Tina Turner Split,1976,General,What is the thing that wives do that annoy most husbands,Nag,General,Half the population of China is what,Short Sighted +General,Arab terrorists hijack Italian ocean liner _____ & kill an American passenger,Achille lauro,General,The Foudrinier machine is used to manufacture what,Paper,Sports & Leisure,Which American Heavyweight Boxer Nicknamed 'The Atomic Bull'' Gained The WBC World Title In 1994? ,Oliver McCall  +General,House on what part of the body is an 'ltk procedure' performed,Eyes,General,At the borders of reality is French translation what TV show,The X Files,Science & Nature,The largest pure gold nugget (weighing 70.92kg) ever discovered was called___?,The Welcome Stranger +Sports & Leisure,"What sport do the following terms belong to - ""Toucher & Dead Length""?",Lawn or Indoor Bowls,General,"A total of 1,670 people died in a theatre fire in China in what year",1845,Music,Which Beatle Who wore the pink uniform on the cover of Sgt. Pepper's?,Ringo +General,What wood was the cross supposed to be made of,Mistletoe,General,The Air Canada Silver Broom is won in which sport,Curling,General,What is the largest species of flatfish,Halibut +Art & Literature,"He penned the founding novel of the utopian genre, ""Utopia.""?",Sir Thomas More,General,Quart bottle or vessel for wine,Flagon,Sports & Leisure,In 2005 Who Was Named BBC Sport Personality Of The Year? ,Andrew Murray  +General,Batrachophobia is a fear of what,Frogs and Toads,General,Which Greek Philosopher taught at the lyceum,Aristotle,General,Name the traditional Jewish dish of stuffed fish cooked in a broth,Gefilte fish +Music,Which Future Star Once Won A Hayley Mills Lookalike Contest,Olivia Newton John,General,"Before Becoming President Of The USA ""Bill Clinton"" Was Governor Of Which State",Arkansas,Geography,Which Canadian province extends farthest north,Quebec +Art & Literature,Who is karen Blixen better known as?,Isaak Dinesen,General,What do Fromologists collect,Cheese labels,General,Which tennis player was sued by his fan club,Jimmy Connors + History & Holidays,When Was The Abolition Of The Death Penalty Made Permanent In Great Britain ,1969 , History & Holidays,"In Finland Santa traditionally doesn't use his sleigh what does he ride instead, is it, Rudolph, Santa's servant Black Peter, A goat named Ukko or Thirteen Elves ",A goat named Ukko , History & Holidays,Who Wrote 'Auld Lang Syne' ,Robery Burn / Henry Viii  +Sports & Leisure,In Which Weight Category = Did The Boxer Barry McGuigan Become A World Champion ,Featherweight ,General,In Haifa Israel its illegal to take what to the beach,A Bear,Technology & Video Games,What is the name of the Playstation controller that uses two analog joysticks? ,Dual Shock +Food & Drink,What Is The Main Ingrediant Of Guacamole? ,Avacados ,Geography,In Which Sea Is The Country Of Cuba Located ,Caribbean ,General,What is the flower that stands for: distrust,Lavender +General,In 1949 the Thought Police first appeared in what novel,1984 George Orwell, History & Holidays,Who was Scrooge's dead business partner in 'A Christmas Carol' ,Jacob Marley , Geography,Where is Westminster Abbey located?,London +General,"Belly, Block, Blout, Nut, Rib and waist are all parts of what",A Violin, Geography,What is the largest island in the world?,Greenland,General,What is a group of this animal called: Rattlesnake,Rhumba +Geography,In which continent would you find the yellow river ,Asia ,Music,Which Childrens author appears on the cover of the beatles album Sgt Peppers Lonely Hearts Club Band,Lewis Carol,General,"Which comic actor who died in 1977 entered a competition to find his look alike, anonymously, and only came third",Charlie chaplin +General,What operating system in used on an ibm as400,Os400,General,"Who said ""I disapprove of what yoU.S.ay, but defend to the death your right to say it."" and ""If God did not exist, it would be necessary to invent Him""",Voltaire,General,Through what canadian city does the red river run,Winnipeg +General,What is SAD,Seasonal affective disorder,General,Apart from a compass what is always found in a ships binnacle,Magnets,General,Which author published 59 new books in 1955,Enid Blyton +General,Which classical composer wrote Hark the Herald Angels Sing,Felix Mendelssohn,Food & Drink,What was the first ever chocolate bar produced by the Mars Company? ,Milky Way ,General,"In art, what is impasto",Laying paint on thickly +Food & Drink,What is another name for the carambula?,Star fruit,Music,Which Singer Was Discovered In The 1930's After Winning A Singing Contest At Harlem's Apollo Theatre,Ella Fitzgerald,General,"What does ""papillion"" mean",Butterfly +General,What rare metal melts at 86 degress Fahrenheit,Gallium,General,“Oh What A Beautiful Morning” Is The Opening Song To Which Famous Musical,Oklahoma,General,Where is capitol hill,Washington dc +General,What was neil young's first film,Journey through the past,General,What do breeders and trainers use to identify dogs,Nose prints,Music,Five Members Of The Pearson Family Formed Which Band In 1983,Five Star + History & Holidays,"In Halloween, what is the middle name of Michael Myers ",Audrey ,General,"What is made in shapes called finger, petticoat and thistle",Scottish Shortbread,General,Who wrote 'the female eunuch',Germaine greer +General,Which actor played Alain Chanier in The French Connection,Fernando rey,Sports & Leisure,Soccer: The New York _________.,Cosmos,Food & Drink,Which stout is Dublin world-famous for? ,Guiness  +General,"Because metal was scarce during world war ii, of what were the oscars made",Wood,Science & Nature,What is name applied to the study of soil?,Paedology, History & Holidays,In Which Year Did The Falklands Conflict Begin ,1982  +Entertainment,Who was the oldest member of The Beatles?,Ringo Starr,General,What Was The Last Carry On Movie In The Series Which Was Released In 1992,Carry On Columbus,General,What is the main unit of currency in Saudi Arabia,Riyal +General,What type of creature lives in a sett,Badger,General,Who is the only greek god in greek mythology with a mortal mother and an immortal father,Dionysus,General,"Who sang the song Raindrops keep falling on my Head"" in the film Butch Cassidy and the Sundance Kid""",B.j. thomas + History & Holidays,The St. Valentine's Day massacre took place in this city.,Chicago,General,John F. Kennedy Airport used to be called __________,Idlewild, History & Holidays,Who led the mongols?,Genghis Khan +Music,Which knight is conductor with the Berlin Philharmonic Orchestra?,Sir Simon Rattle,General,What license must Californians acquire before they can legally set up a mousetrap,A hunting license hunting license,General,A golfer can only do it for five minutes - what,Look for his lost balls +Science & Nature,For What Is Sn The Chemical Notation ,Tin ,General,Dimitri Mendeleyev is credited with the discover of what,Periodic Table, History & Holidays,How Old Was John F Kennedy When He Was Assassinated ,46  +General,If you travel by 'Shanks's pony' how do you go,On foot,General,Who Composed The Music To The Movies Jaws And Starwars,John Williams,General,What U.S. town was the site of the last battle between Britain and the US,New orleans +General,Collective nouns - An obstinacy of what,Buffalos,General,Which is the only animal other than humans that can get leprosy,Armadillos,Music,"Who Played Lead Guitar On George's ""While My Guitar Gently Weeps""",Eric Clapton + History & Holidays,In which country do people wear white clothes in order to have good luck during the new year? ,Brazil ,General,What is the capital of the Canadian province of British Columbia,Victor1a,General,By What Name Is Patrick Clifton Better Known?,Postman Pat +General,What metal in it's purest state is so soft that it can be molded with the hands,Gold,General,What is the tympanic membrane,Eardrum, Geography,In which country is Angel Falls?,Venezuela +General,What are the four colours of croquet balls,"Red, yellow, blue, black",Religion & Mythology,In China why were kites flown on the ninth day of every month ?,To banish evil,General,What does a traveler suffer from if he has Nostomania?,Intense homesickness +People & Places,Who was elected as MP for Belfast West in 1997? ,Gerry Adams ,General,What system do the blind use for reading?,Braille,General,What animal is represented by the constellation Monoceros,Unicorn +General,Which British prime minister said 'You never had it so good',Harold macmillan,General,In the opera Tosca what was Tosca's profession,Opera Singer,General,Dover is the capital of which U.S. state,Delaware +General,"In Greek mythology, who was the sister of Apollo",Artemis,General,"Arborio, patna and basmati are all types of what",Rice,General,What plants name means wild growing by the Volga,Rhubarb - Rha – Volga Barb - Wild +General,Dismus and Gestas were who,Robbers next to Jesus,Music,Which Track Did Charles Aznavour Re-Issue Three Times In The 1970's,The Old Fashioned Way,General,Who built The Flamingo hotel in Las Vegas,Bugsy Siegel +Science & Nature," The Dalmatian dog is named for the Dalmatian Coast of __________, where it is believed to have been originally bred.",Croatia,General,In what Australian state would you find Bunbury,Western australia,Music,Gloria Maria Fajardo Is The Real Name Of Which Singer,Gloria Estefan +General,What is 'sapodilla' a type of,Fruit,General,Who got his name because of a yellow-and-black striped shirt he wore until it literally fell apart,Sting,People & Places,For Which Club Does Paul Merson Now Play ,Aston Villa  +General,In Nicholas Nickleby name the headmaster of Dotheboys Hall,Wackford Squeers,General,What is the biggest criterion for prospective astronauts,Eyesight,General,"Who composed the opera, ""The Queen of Spades""",Tchaikovsky +General,Who sang 'friends in low places' and 'thunder rolls',Garth brooks,General,What is the larynx,Voice box,Food & Drink,From what is mead made? ,Honey  +General,What is a group of frogs,Army,General,What's the most common Christian name of U.S. presidents,James,General,If You Suffer From Cynanthropy What Do You Think You Are,A Dog +General,What is the fear of blindness in visual field known as,Scotomaphobia,Music,He Was Born Robert Walden Cassotto & Suffered From A Week Heart All His Life And Died Aged 37 Who Was He,Bobby Darin,Science & Nature,Which woman scientist was awarded two nobel prizes in the early part of the 20th century ,Marie curie  +General,Which Country Was Formally Known As Formosa?,Taiwan,General,"In the U.S.A., this food is known as 'granola'",How is it known in europe muesli,General,"A plotless work composed of pure dance movements, although the composition may suggest a mood or subject.",Abstract dance +People & Places,Which establishments don't have windows or watches?,Casino's,General,"Specifically, what is the westernmost point in the contiguous u.s",Cape,General,Who pulls Wayne over in Wayne's World?,Robert Patrick +General,"Who recorded ""Ferry 'Cross the Mersey"" in 1965",Gerry and the,General,By what name was the American William Cody better known,Buffalo bill,Art & Literature,"Captain Hook, Tiger Lily, and Tinker Bell are characters in what story",Peter pan +Religion & Mythology,Who is the greek equivalent of the roman god Pluto ?,Hades,General,What office supply item does Dilbert count,Staples,General,Collective nouns - a tribe or trip of what,Goats +General,In 1904 May Sutton Brandy was the first US woman to do what,Win Wimbledon singles,General,"Which Trilogy Of Movies 3rd Part Had The Title ""The Final Insult""",The Naked Gun,General,What is the approximate speed of light,"186,000 miles per second" +General,Who penned five of the ten best-selling children's books in u.s history by 1994,Dr seuss,General,"What Does The Letter ""P"" Stand For With Regard To The Medical Organisation ""BUPA""",British United Provident Association,General,Who sings the theme song to the television series Friends,Rembrandts +General,What is Indiana's state bird,Cardinal,Science & Nature,From which language does the term eureka come ,Greek ,General,Name the Belgian crime writer who created 'Inspector Maigret',Georges simenon +People & Places,Who Resigned After His Affair With Christine Keeler ,John Profumo ,General,"What, according the Mark Twain, is a stomach Steinway",Accordion,General,What takes a human 17 muscles to do,Smile +General,Who was the 4th U.S. president to be assassinated in office,John f. kennedy,Music,What record label issued the first Beatles LP in the US?,VeeJay Records,General,Give Me Any Year In The Life Of Joan Of Arc,1412-1431 +Tech & Video Games,Which brothers built a home-made supercomputer to calculate the digits of Pi ?,Chudnovsky,General,The site of Troy is in which modern country,Turkey,General,A meaningless distraction is a ___ herring.,Red +General,What former us vice president was the title star of meet the veep,Alben w,Food & Drink,"In Shakespear's Hamlet, which herb is said to be 'for rememberance' ? ",Rosemary ,General,The presence of greater than five digits on the hands or feet is called,Polydactylia +Music,What Inflatable Animal Featured In Pink Floys Famous 1977 Tour,A Pig,General,In the sport of archery what are the arrows usually made from,Aluminium tubes,General,Elba Island is located in what sea,Tyrrhenian sea +Music,"Who Sang On The Chemical Brothers Hit ""Setting Sun""?",Noel Gallagher,General,International dialling codes - What country has 33 as its code,France,General,Team a maryland t-shirt slogan that parodied 'virginia is for lovers' read what,Maryland is for crabs +General,"Named after a European country, which strait lies between Iceland and Greenland",Denmark strait,General,Sailors round the horn - off which country are they doing it,Chile,General,"""Broom"" Bromden an Indian narrates which famous book",One Flew over the Cuckoos Nest +General,What is the sum of 2741 + 3562,6303,General,Who was king arthur's wife,Guinevere, History & Holidays,Where Was The German Fleet Scuppered At The End Of World War I? ,Scapa Flow  +Music,"Who Sang To ""All The Young Dudes"" In 1972",Mott The Hoople,Religion & Mythology,Who is the greek equivalent of the roman god Discordia ?,Eris,Sports & Leisure,What nationality is Gabriela Sabatini?,Argentinian +General,What disease's sufferers were required to carry rattles to warn people of their approach in 15th century England,Leprosy's leprosy,General,"What movie featured Reece's Pieces as a crucial part of the story, because the director couldn't obtain the rights to use M&M's?",E.T.,Science & Nature,Who Was The First Woman To Fly Solo Across The Atlantic Ocean ,Beryl Markham  +General,Who sang the hit 'mister bass man',Johnny cymbal,Geography,What Is An Isthmus ,A Narrow Neck Of Land Connecting 2 Pieces Of Land ,Music,"Which group had three consecutive UK Christmas number ones in 1996, 97 and 98?",Spice Girls +General,"In Magnum PI,what was the name of the charter service that TC ran?",Island Hoppers, History & Holidays,"""Who wrote the Christmas story, """"The Snowman""""?"" ",Raymond Briggs ,General,Misophobia is the fear of,Being contaminated with germs +General,RCA and what other company launched the first vinyl records,Columbia,General,What Sidney Sheldon novel included nuns among its main characters,Sands of time,Music,David Robert Jones Is The Real Name Of Which Singer,David Bowie +General,Valentine Michael Smith is the central character in which book,Heinlein stranger in a strange land,General,Spirits of Salt is an alternative name for which acid?,Hydrochloric Acid,General,What is the worlds oldest snack food - 610 AD,The Pretzel +Geography,Before The Discovery Of Mount Everest What Was The Tallest Mountain In The World ,Mount Everest , History & Holidays,What Was Launched In 1960 & Was Closely Linked With The Sexual Attitudes Of The Swinging 60's ,The Pill ,General,On a pencil what do the initials HB stand for,Hard Black +Science & Nature, There are __________ that nest in trees. These creatures may spend their whole life without ever touching the ground.,Mice,Toys & Games,"In which game might a person have a ""full house""",Poker,General,What are the annual awards for the best billboards (obies) named after,Obelisks +General,According to English Church what's legal only tween 8 am 6 pm,Getting Married Canonical Law,General,What is another name for the beak of a bird,Bill,General,Do the bones of a pigeon weigh more or less than its feathers,Less +General,What was the name of Haile Salassie before he was crowned,Ras Tafarri,Music,Steve Jones Was Guitarist With Which Punk Band,The Sex Pistols,General,"From Which Plant Does ""Vanilla"" Come From",Orchid +General,In Australia what is it considered rude to do?,Wink,General,A young what is called a squeaker,Pigeon,Sports & Leisure,Which Athlete Was Known As The Buckeye Bullet ,Jesse Owens  +General,A semicircular area at the end of a church; in most churches it contains the altar. ,Apse,General,The worlds first opened in Los Angles April 2nd 1902 - what,Motion picture theatre,General,What is a group of storks,Mustering + History & Holidays,Who Released The 70's Album Entitled Hunky Dory ,David Bowie ,General,"Who was the male lead in ""Key Largo""",Humphrey bogart,General,What film introduced the song 'the first time ever i saw your face',Play +General,When was the photoelectric cell invented,1895,Geography,What country is directly west of Spain,Portugal,General,What product did farfel the dog advertise,Nestle's chocolate +Food & Drink,Spice that a bartender would dust your Brandy Flip with,Nutmeg,Sports & Leisure,Name The 4 Cities Beginning With The Letter A That Have Hosted The Summer Olympics? (PFE) ,"Athens , Amsterdam , Antwerp & Atlanta ",Religion & Mythology,He led the Mormons to the Great Salt Lake.,Young +Sports & Leisure,In Horse Racing What Is A 'Ringer''? ,Illegally Replacing A Horse ,General,What sport uses clowns to protect the competitors,Rodeo,General,Which French philosopher hid in alleys mooning passers by,Rousseau + History & Holidays,"""Which Oscar Winning Actor Played Scrooge In """"The Muppet Christmas Carol"""""" ",Michael Caine ,Art & Literature,What was the name of Mother Goose's son?,Jack,Science & Nature,Which Bird Is Renowned For Taking Over The Nests Of Other Species ,Cuckoo  + Geography,Which European country has the lowest population density?,Iceland,General,In which decade was Mohandas Karamchand Gandhi born,1860s (1869),General,What is the literal English translation for China’s Tiananmen Square?,Heavenly Peace +General,On which temperature scale does water freeze at 0 degrees and boil at 80 degrees,Reaumur,General,"The ___________ to the picturesque, 77_foot_tall Sleeping Beauty's Castle in Disneyland in southern California, actually works. It was lowered on the opening day of the park, July 17, 1955 ",Drawbridge,General,Where were the 1980 Olympic games held,Moscow +General,Which British scientist invented a safety lamp for miners,Davy,General,Who did hank ketcham create,Dennis the menace,General,Jason sailed in the Argo but who steered the ship,Argus +General,Whats the third most common language in the world,Spanish,General,Which Dickens novel features Waxford Squears,Nicholas Nickleby,General,Annuit Coeptis - Novis ordo seclorum - mottos on what item,Reverse Great Seal USA +General,What country had the highest investments in china in 1937,Britain,General,Which U.S. President won the Nobel Peace Prize in 1906 for his mediation in the Russo-Japanese War,Theodore ROOSEVELT,Geography,Madrid and Lisbon are both located near this river.,Tagus +General,How many gloves did Michael Jackson wear?,One,General,September 22 is National what appreciation day,Elephants,General,What probably caused the craters on the moon,Meteors +General,"Common name for flightless, aquatic birds of the southern hemisphere",Penguin,Geography,In which state is mount st. helens ,Washington ,People & Places,Where is the biggest mosque in the world?,"Madina, Saudi Arabia" +General,"What holiday do spaniards celebrate with cries of ""feliz ano nuevo""",New,Food & Drink,A Skillet Is A Pan Generally Made From Which Material ,Cast Iron ,Music,Who Is The Lead Singer With REM?,Michael Stipe +General,"Whose motto is ""we learn by doing""",The 4 h club,Sports & Leisure,The Rockets Basketball Team & The Astros Baseball Team Both Hail From Which US City ,Houston ,General,What zodiacal sign is represented by a bull,Taurus +General,In which Fox TV show did Johnny Dep play an undercover cop in high school?,21 Jump Street,Science & Nature, A garter snake can give birth to __________,85 babies,Science & Nature,What Is An Angle Of Less Than 90 Degrees Known As ,An Acute Angle  +General,What is the flower that stands for: mature elegance,Pomegranate flower,Music,Name All 3 Andrews Sisters,"Patti, Maxine & La Verne",Science & Nature,"What are these: Ceres, Juno, Iris, and Flora?",Asteroids +General,Who got a gold single for the song Daniel in 1973,Elton John,General,"In 1960 Francis Gary Powers, flew into a row with Russia and was shot down allegedly for what",Spying,General,Whats the capital of Texas,Austin +General,"Whose first published book, in 1961, was 'Call for the Dead'",John le carre,General,Who played Julie's best friend in I still know what you did last summer?,Brandy, History & Holidays,The 1954 movie White Christmas was the first to be made using what new Paramount film format? ,VistaVision  +General,What is the product for the slogan 'the quicker-picker-upper',Bounty,General,Jimmy stewart is driven by revenge in the movie _____,Man who shot liberty,Science & Nature,"What vitamin complex includes thiamine, niacin and riboflavin ",Vitamin b  +General,Dilbert: Which test does the Gruntmaster 6000 fail,Armageddon,General,Which heraldic term means sleeping,Dormant,General,What did God create on the fifth day (both),Sea creatures and birds + History & Holidays,How many days after john f. kennedy's assassination was lee harvey oswald shot ,Two ,Geography,Where is the Holy Kaaba,Mecca,General,What is the common name of the disease Varicella,Chicken pox +Entertainment,What is Smokey Stover's job,Fireman,Food & Drink,What causes baker's itch? ,Yeast ,General,Which sea is sometimes called the Euxine Sea,Black Sea +General,What colour is viridian,Green,Music,"Who Had Their Only UK Top 10Hit With ""Love Missile F1-11""",Sigue Sigue Sputnik, Geography,Which river contains the most fresh water?,Amazon +General,"What was the Oscar-winning theme song for ""Breakfast at Tiffany's""",Moon,Music,"In Which Musical Would You Hear The Song ""Edelweiss""",The Sound Of Music,Sports & Leisure,Whats the main feature of a speedway motorbike?,No Brakes + Geography,What is the basic unit of currency for Latvia ?,Lats,General,If you suffered Harpaxophillia what turns you on,Being Robbed,Music,Which Group Were “All Out Of Love” In 1980?,Air Supply +General,What word may be used to refer to a group of gnats,Horde,General,Which dinosaurs name translates as double beam,Diplodocus,General,"The Steps Singer Lisa Scott Lee Is Married To Johnny Shentall"" Of Which Reality TV Show Band Was He A Member",Hear Say +Food & Drink,"Which citrus fruit, possibly a combination of sweet lime and sour orange, grows predominantly in Italy and is used in Earl Grey tea and eau de Cologne ? ",Bergamot ,General,Which Nigerian won the Booker Prize in 1991 with 'The Famished Road',Ben okri,General,Maniaphobia is the fear of,Insanity +General,"The city of Mt. Vernon, Washington grows more ______ than the entire country of Holland",Tulips,General,What is the name of the belief in the existence of many gods or divine beings,Polytheism,Music,Gary Numan Sang An 80's Hit About Which Mode Of Transport,Cars +General,Who wrote the book The Amazing Mr Ripley,Patricia Highsmith,General,"What time would you watch ""Late Night With David Letterman"" on NBC?",12:30-1:30,General,What is a mamba,A snake +General,"In 1965, lyndon b johnson enacted a law requiring cigarette manufacturers to put what on their packages",Health warnings,General,What was the statue of liberty originally named,Liberty enlightening the,General,What do the letters 's.a.m' mean in sam missiles,Surface to air missiles +General,Who controlled canada's hudson bay company,British,General,Whose latest album is G.O.A.T. (Greatest of all time),L l cool j,General,The word Matrix in the Bible means what,Womb +General,What singer's February 6th birthday is a national holiday in Jamaica?,Bob Marley,General,Complete the name of the 1970s group Bachman Turner ___,Overdrive,Science & Nature,Deoxyribonucleic acid is better known as __________.,DNA +General,This band was named band of the year in America in 1969?,Creedence Clearwater Revival,Music,"Who Had A Hit With ""Kiss On My List""",Hall & Oats,General,Who is the egyptian goddess of love,Isis +Music,"Which Band Had Hits With ""These Dreams"" & ""What About Love""",Heart, History & Holidays,In 1911 Roald Amundsen became the first person to reach where ?,South Pole,General,"Roy orbison says 'you have to pick up your feet, you've got a deadline to meet, because you're ______'",Working for the man +General,Sir Walter Raleigh found what odd lake in Trinidad,Lake of Tar or Asphalt,General,"Press Hybrid offspring of the jackass (male ass) and the mare, much used and valued in many parts of the world as a beast of burden.",Mule,Music,How many keys are on a standard concert piano?,88 +General,The sale of what counterfeit delicacy outranged the French,Truffles - White dyed Black,General,Who was candice bergen's father,Edgar bergen,General,Ariztid Olt was what early name used by a famed actor,Bela Lugosi +General,Which Band Took Their Name From The Villan In The Film Barbarella,Duran Duran,General,What is the flower that stands for: death,Cypress,Food & Drink,From what German region do Hock wines come? ,The Rhine  +General,Beverly Hillbillies Bank managers secretary - full name,Jane Hathaway,Entertainment,"Name the singer - songs include ""Me & Bobby McGee, Mercedes Benz""?",Janis Joplin,General,Who is known as the high priest of revenge,Philip seldon +General,Who did Richard Nixon call the ayatollah of the press corps,Sam donaldson,Music,On Which Street Did Elvis Take A Walk According To The Lyrics Of  “Heartbreak Hotel”,Lonely,Geography,What Is The Capital Of China ,Beijing  +General,Any of several soft metal alloys used to line bearings and bushings in order to reduce friction,Babbitt Metal,General,"""The Coffee Cantata"" was written by who?",Johann Sebastian Bach,General,It's now the Birmingham Royal Ballet but it used to be what,Saddlers Wells Opera +General,What principality has the house of grimaldi ruled since the middle ages,Monaco,General,What do the quarters of a hot cross bun symbolise,Four Seasons,General,Chemical compounds or mixtures that undergo rapid burning or decomposition with the generation of large amounts of gas and heat and the consequent production of sudden pressure effects.,Explosives +Music,"Which Musical Features The Song ""Sixteen Going On Seventeen""",The Sound Of Music,General,Is the product of Greece and the Greek colonies from about 1100B bc to the 1st century bc,Greek Art and Architecture,General,What is the sum of 32 + 49 + 765,846 +General,As A Child Jonathan Ross Appeared In A TV Ad Advertising What Product,Rice Krispies,General,FITA are the governing body of what sport,Archery,General,Which West Indies and Hampshire fast bowler died of cancer in 1999,Malcolm marshall +Music,"What Have The Singles Relax, God Save The Queen , I Want Your Sex All Got In Common",They Were All Banned By The BBC,General,Fill in the blank: birds of a ______,Feather,General,Felix Salten wrote which Disney cartoon,Bambi +Religion & Mythology,Who founded the Salvation Army?,William Booth,General,Of what country is Tripoli capital,Libya,General,Which country grows the most potatoes,Russia +General,Whose legs were shown in place of julia roberts' legs in 'pretty woman',Goldie hawn,General,In which country is the importation of bubble gum illegal?,Singapore,Music,What Was Diana Ross's Last Hit With The Supremes?,Someday We'll Be Together +Science & Nature,PC World Have Announced That They Will No longer Be Selling What Due To Lack Of Demand ,Floppy Disks ,General,What was a ducat,Coin,General,Gregorys powder is a type of what,Laxative +General,What would a Scotsman do with a spurtle,Eat porridge (it’s a spoon),General,"In 1962, for what did britain and france sign an agreement to build together",Concorde,General,In what U.S. state is Fort Knox,Kentucky +General,Who ran through the streets naked crying Eureka,Archimedes,General,British Standard BS2724 might protect what body part,Eyes - Sunglasses,People & Places,Which Is The Only US State That Has A Name Ending With Three Vowels? ,Hawaii  +Music,What Did Susan Maughan Yearn To Be According To Her 1962 Hit,Bobbys Girl,General,Helena Rollason Was The First Female PresenterOf Which Long Running Tv Show?,Grandstand,Science & Nature,Rimsky Korsakov Composed A Short Piece Of Music About Whose Flight ,The Bumblebee  +General,What was the only nation to register zero births in 1983?,Vatican City, History & Holidays,What organization was founded by the rev. jerry falwell ,Moral majority ,General,Where did De Gaulle flee to after France surrendered to germany,Britain +General,Who created the comic strip 'Doonesbury',Garry trudeau,Food & Drink,Individual bananas are called ?,Fingers,General,The Selenas Valley was the rejected title what Steinbeck book,East of Eden +General,"The numbat, quokka and yapok are all types of what",Marsupials,General,What do you call a person whose iq is between 110-120,Superior,General,What was the title of the first album by U2?,Boy +General,What is a dwile,A Dishcloth,General,According to the proverb which fruit tastes sweetest,Forbidden,General,"He produced the Beatles ""Sgt. Peppers Lonely Hearts Club Band""",George martin +General,How many semihemidemisemiquavers are there in a breve,256,General,The Eiffel Tower is how many feet high,984 feet,General,A thin pancake with savoury or sweet filling,Crepe +Music,Which South coast Town Do The Levellers Come From,Brighton, Geography,What is the capital of Spain ?,Madrid,Art & Literature,Who Composed The Ballet (Romeo & Juliet) ,Prokofiev  + Geography,In what state is Concord?,New Hampshire,General,Hypsiphobia is the fear of,Height,General,Who is the greek counterpart of jupiter,Zeus +Music,"Who was ""Giving It All Away""",Roger Daltrey,General,Which album released on august 11 1969 was a collaboration between donovan and jeff beck,Barabajagal,General,"In 1865, Nathan Forrest became the first leader of which American secret society",Ku klux klan +General,Who wrote the novel Tom Jones in 1749,Henry Fielding,General,"Who is the anti-heroine of Thackeray's ""novel without a hero""",Becky sharp,General,What is a female calf called?,Heifer +General,Who painted the portrait La Gioconda,Leonardo da vinci,General,"Who said ""The hunger for love is much more difficult to remove than the hunger for bread.""?",Mother Teresa, History & Holidays,Who Released The 70's Album Entitled Aja ,Steely Dan  +Science & Nature,By what chemical process do plants manufacture food,Photosynthesis,General,The name of the underworld in Greek mythology,Tartarus,General,Name the first automobile racetrack in america,Indianapolis motor speedway +Sports & Leisure,Which Football Team Was The First To Install An Artifcial Pitch ,Queens Park Rangers ,General,"Who wrote ""Farwell to Arms""",Ernest hemingway,Entertainment,Which German duo have sold over 85 million records?,Modern Talking +Music,Who Declared In 1989 That They Would Rather Jack Than Fleetwood Mac,The Reynolds Girls,General,Philip K Dick's novel 'Do Androids Dream of Electric Sleep was made into what film,Blade runner,General,What tv show did john lennon and yoko ono co-host in 1972,Mike douglas show +Art & Literature,What book is the film Blade Runner based on ?,Do Androids Dream of Electric Sheep,General,The Dick Test Is Used To Test A Persons Immunity Against Which Disease?,Scarlet Fever,Sports & Leisure,Where is Capitol Hill?,Washington DC +General,Who wrote Catch 22 (both names),Joseph Heller,General,Kleenex tissues were originally intended as what in 1915,WW1 Gas mask filters,General,What is the element symbol for hydrogen,H +People & Places,After Whom Is The Teddy Bear Named ,Theodore Roosevelt ,General,What sort of creature is a tarantula hawk,Wasp,Geography,In Which London Park Would You Find Birdcage Walk? ,St James's Park  +General,Who owned and lived in the castle Joyous Guard,Lancelot,General,What is the more popular name for the plant convallaria majalis,Lily of the valley,General,The 1st U.S. minimum wage law was instituted in what year,1938 + Geography,What is also known as Amundsen Scott Station?,South Pole,General,When is Superman's birthday,29th February,General,Pal Hewson became more famous as who,Bono - singer in U2 +General,In what field of study are 'flying buttresses',Architecture,General,V.O. and Pedigree whiskeys made by what Canadian company,Seagrams,Music,"OMD's ""Enola Gay"" Was About A Famous World War 2 ""Peace Activist"" Or ""Bomber""",Bomber +Music,Who Hammered Out A Classic Animated Video In 86 And Got All Steamed Up In 93,Peter Gabriel,General,Ejaculation comes from the Latin meaning what,Throwing Out,General,"""Erse"" is another name for which language",Gaelic +Mathematics,An integer that is greater than 1 and is divisible only by itself and 1 is known as a(n) _______.,Prime,General,Who was the governor of New South Wales in 1808,Captain William Bligh,Science & Nature,What Do The Initials E.C.G Stand For ,Electrocardiograph  +Mathematics,Approximately how many inches are there in one meter,39,General,What is the only bird that can swim but can't fly,Penguin,General,What's the last name of the most famous ornithologist,Audubon +General,How fast does the tip of a rotary mower travel,200 km/hr,General,What did victorian women bathe in to try to enlarge their breasts,Strawberries,General,What is the main ingredient of the French dish cassoulet,Haricot beans +General,Who founded the boy scouts,Lord baden powell,General,The Pampero blows over which mountains,Andes,Entertainment,What was Daffy Duck's favorite insult?,You're dispicable! + History & Holidays,"The three wise men allegedly brought gifts of Gold, Frankincense and Myrrh, spell Myrrh ",Myrrh ,General,"In poker, what are five cards of the same suit",Flush,General,The word gospel from Anglo Saxon literally means what,Good Tidings +General,In Chicago it is illegal to eat in a restaurant that is what,On Fire,Music,Which Group Was Formed In The 1980's & 90's By Evan Dando,The Lemonheads,General,"Who was the Egyptian god of the Nile, depicted in human form with a beard, large belly, & a crown of aquatic",Hapi + Geography,What is the capital of Guyana ?,Georgetown,Music,"Which Video From David Bowies ""Lets Dance"" Was Censored For Nudity",China Girl,General,Rhabdophobia is the fear of,Being severely punished beaten by a rod +Food & Drink,Hazelnut liqueur named for the mysterious monk that made it 300 years ago,Frangelico,Entertainment,"With which period in music do we associate with composers such as Bach, Handel and Vivaldi?",Baroque Period,General,At which sport do Oxford and Cambridge Universities compete for the Bowring Bowl,Rugbyunion + Geography,What is the basic unit of currency for Israel ?,Sheqel,General,"In the old Dick and Jane 'Primary Reader' school books, what was the name of the little sister",Sally,General,What is Lygeristia (a limiting thing),Arousal in darkness +General,Tom hallick was the first male host of which show,Entertainment tonight,General,What U.S. state includes the telephone area code 603,New hampshire,General,What is the Capital of: Luxembourg,Luxembourg +General,Who received the first ever Gold Disc,Glen Miller – Chatanooga cho cho, History & Holidays,What was the gift from the gods in the movie The Gods Must Be Crazy? ,A coke bottle , History & Holidays,What Tax Was Levied Between 1695 & 1851? ,Window Tax  +General,Clamart means what food will be used in the dish,Peas,General,From what material is the ring made in Sumo Wrestling,Clay,Science & Nature," The ring_tailed __________, a primate found only in Madagascar, meows like a cat.",Lemur +General,What is the fear of technology known as,Technophobia,General,Rolf Harris was Australian junior champion at what,Swimming – Backstroke, History & Holidays,Which country was first to operate an old age pension scheme? ,Germany (1891)  +General,"Who was born when pluto, the astrological sign for death, was directly above dallas, texas",John f kennedy,Music,Taco Were Putting On The What,Ritz,General,"Who originally recorded ""friday on my mind""",Easybeats +People & Places,Which Argentinean General Ordered The Invasion Of The Falkland Islands ,General Galtieri ,General,Fish: what is morone mississippiensis,Yellow bass,General,In The Italian Job Noel Coward played what character in prison,Mr Bridger +Science & Nature," Pigs, walruses, and light_colored horses can be __________",Sunburned,General,Where are the deepest mines?,South Africa,General,To what type of animal does the adjective 'ovine' refer,Sheep +Science & Nature,What russian physiologist went to the dogs to write conditioned reflexes ,Ivan pavlov ,General,What colour is a crabÆs blood?,Blue,General,Who's head did Courtney Cox say was on her body in Scream 2?,Jennifer Aniston +General,Walter Gropius founded what art / design movement,Bauerhus in Germany,General,Which German town is called Aix-la-Chapelle by the French,Aachen,General,The US eat most ice cream per capita what country second,New Zealand +Tech & Video Games,"Name the final boss from each Final Fantasy game (1-8, in that order). ","Chaos, Emperor from Hell, Dark Cloud, Zeromus, Deathgyunos, Kefka, Sephiroth, and Ultimecia",General,Which cricketer played in 52 test matches and averaged 99.94 runs,Sir don bradman,General,Any of various viruses that are parasites of bacteria,Bacteriophage +People & Places,Which Cricketer wenbt by the Name of Beefy ,Ian Botham (Beefy Botham) ,General,What did Simon of Cyrene do in The Bible,Carry Christ's cross, History & Holidays,Which best selling American writer wrote and directed the sci-fi film Westworld ? ,Michael Crichton  +General,Smell of lavender liquorice chocolate doughnuts increase what,Blood flow to penis,General,What percentage of Antarctica is ice?,98%,General,Which people invented the compass,Chinese +General,"In '64, whom did J Edgar Hoover call America's ""most notorious liar""",Martin luther king jr,General,What religious elements is voodoo a mixture of,Roman catholicism,General,What product was originally called drybak,Duct Tape +General,What was the first fighting vehicle,War chariot, Geography,What is the world's highest city?,Lhasa,General,Soundman Who was the king of England during the American Revolution,George iii +General,Name the Shakespeare play with a city in Greece in name,Timon of Athens,General,Who invented the ferris wheel,Ferris, History & Holidays,Who Was Ousted By Corazon Aquino In 1986 ,Ferdinand Marcos  +General,If a wine is described as alcooleux what has it got,Overwhelming alcohol,General,Ixchel is the mayan goddess of ______,Earth and moon,General,What nationality is the singer Celine Dion,Canadian +General,What did the word hussy originally mean,Housekeeper,General,What does a librettist do,Write words to opera,Music,What Did Corey Hart Wear At Night,Sunglasses +General,What fish can hold objects in its tail,Sea Horse,General,An analgesic and anti-inflammatory drug,Iburofen, History & Holidays,"Who Had An 80's Hit With The Song 'Paranomia,' ",Art of Noise feat Max Headroom  +General,Jupiter was the roman god of ______,Light and sky,Music,What was the title of the album by the Woodpeckers?,Emmerdance,General,Oenophobia is the fear of,Wines +General,For how much did an american urologist buy napoleon's penis,Three thousand,General,What's excision of the breast called,Mastectomy,General,"""The Voyage of The Beagle"" told of which scientist's discoveries",Charles Darwin Darwin +General,In the Modern 1896 Olympics what was the first event decided,Triple Jump,General,Onassis driving: what country is identified by the letters ma,Morocco,General,What was the name of the declaration in 1981 which created the SDP political party?,Limehouse Declaration +Sports & Leisure,Which former British world boxing champion has the middle name Livingstone? ,Chris Eubank ,General,A lump of pure gold the size of a matchbox can be flattened into a sheet the size of what,A tennis court, History & Holidays,"""On the Single """" Do They Know It's Christmas """" Only One Person Performed On Both The Original And The 20 Year Remake Band Aid 20, Who Was It?"" ",Bono / U2  +Music,What Was The Name Of Singer Charlotte Church's Debut Album,Voice Of An Angel, History & Holidays,Who was crowned King of England at Westminster Abbey on Christmas day 1066 ,William The Conqueror ,General,What pop star served a football apprenticeship with Brentford,Rod Stuart +General,What was the Beatles first UK top ten single,Please-please me,Entertainment,Which movie is the highest grossing movie of all time?,Titanic,General,209 Popes have been Italian which country has the 2nd highest,France + Geography,"What country was the setting for ""Casablanca""?",Morocco, Geography,What is the basic unit of currency for Tajikistan ?,Rouble,General,A golden _____ removed from King Tut's tomb was still sharp enough to be used,Razor +Music,Did Elvis Presley Ever Tour The Uk,No,Science & Nature,What is the symbol for iron in chemistry,Fe,General,What's the Cray-2 immersed in,Subzero flurocarbon fluid +General,Krusty the Clown and Chandler in Friends have an extra what,Nipple,General,Hugh O'Brian played the lead in what Old Western series,Wyatt Earp, History & Holidays,Who died three days before Groucho Marx?,Elvis Presley + Geography,Which is the only sea below sea level?,Dead Sea,General,In Greek mythology who created man,The demigod Promethus,General,Who speak a language called Mudderschproch,Amish (Pennsylvania Dutch) +Sports & Leisure,Ryan gigs's dad was a professional in which sport ,Rugby League ,Music,Which 3 ZZ Top Music Videos Featured The Infamous Trio Of Highly Sexed Vamps,"Gimme All Your Lovin, Sharp Dressed Man, Legs",General,Which instrument did the Jazz musician Dizzy Gillespie play,Trumpet +General,"It's a mystery to me, the game commences for the usual fee ___"" What's the Dire Straits song title",Private Investigations,General,With which 17th century plot was Francis Tresham associated,Gunpowder plot,General,"Who were the famous feuding hillbilly families in Pike County, Kentucky",Hatfields & mccoys +General,According to The Beano Comic Strip Who Is Dennis The Menace's Favourite TV Hero?,The Lone Ranger, History & Holidays,"Which rock group of the eighties gave away a silver keychain at every concert, which was supposed to bring good luck to whoever caught it? ",ZZ Top,General,What plant is sometimes called the poor mans weatherglass,Scarlet Pimpernel +General,"What is 9 metres high, 7 metres wide and 2,500 kilometres long?",Great Wall of China,General,Which 1963 Film Musical Was Inspired By The Hysteria Of Elvis Fans Following His Induction Into The Army,Bye Bye Birdie, History & Holidays,What was rudolf hess' sentence at the nuremburg trials ,Life Imprisonment  +General,"Which product used the slogan: ""keep that schoolgirl complexion""",Palmolive,General,If you were studying Iatrology what would you be studying,Science of Medicine,General,Who wrote the post nuclear war novel On the Beach,Neville Schute +General,What colour is the bull on an archery target,Gold,Science & Nature,A thread used in surgery to tie a bleeding blood vessel is called a(n) _________.,Ligature,General,Who played william wallace in 'braveheart',Mel gibson +General,Mexico's equivalant to the dollar is ______,Peso,General,"A 300,000 pound wedding dress made of platinum was once exhibited and in the instructions from the designer was a warning, what was it",Do not iron,Sports & Leisure,How Many Hurdles Are There In The Men's 110metres Hurdles? ,10  +General,What was first sold as a cold cure - not what we use it for now,Alka-Seltzer,General,Where did venetian blinds originate,Japan,General,What is a female sheep,Ewe +General,According to a survey what people have the most hated job UK,Double Glazing Salesmen,General,"According to the proverb, what should not be washed in public",Dirty linen,Music,Which 2 Motown Artists Recorded Duets With Paul McCartney,Stevie Wonder & Michael Jackson +General,What is the medical term for the cartilage in the nose,Septum,General,In what film were Aldeberan Antares Atair Rigel seen,Ben Hur - horses on his chariot,Entertainment,Miss Buckley is secretary to what commanding officer,General halftrack +General,Acinonyx Jubatus is what big pussy,Cheetah,Geography,What southern u.s. city was named for king charles ii ,Charleston , Geography,Which English county has the smallest perimeter?,Isle of Wight +Entertainment,About which family are the Godfather films?,Corleone,General,Where do love birds Tom Hanks and Meg Ryan Have their fateful meeting at the end of the film Sleepless in Seattle ,The Empire State Building , Geography,What is the basic unit of currency for Malawi ?,Kwacha +General,A P Herbert editor of Punch once wrote a cheque on what,Side of a cow,General,Advocating or practising total abstinence from alcohol,Teetotal,General,What is the fear of surgical operations known as,Tomophobia +General,"Krone, yuan and ringgit are all units of what",Currency,General,When were bank holidays first indroduced into Britain,1871,Music,From Which Band Did The Beatles Recruit Ringo Starr,Rory Storm & The Hurricanes +General,Which Country Was The First To Produce Steel?,India,General,What animal is the symbol of long life in Korea,Deer,General,What movie actor was (among other jobs) a bridge painter,Paul Hogan +Food & Drink,Which countrys does one associate with the following foods or drinks: 'Kvass' ,Russia ,General,What disney film stars bette davis,Watcher in the woods,General,This world leader was born in 1870?,Lenin +General,Methyphobia is the fear of what,Alcohol,General,What film marked james cagney's return to the screen after 20 years,Ragtime,General,How many toes does the rhea have,Three +Science & Nature,What is the most essential tool in astronomy?,Telescope,General,This is the Southeast Asian method of dying fabric using wax to create designs.,Batik,General,"The treatment of which famous mythical ruling, by the artist Max Klinger in 1887, caused a storm of protest from art lovers",Judgment of paris +Art & Literature,"Meaning 'fool the eye' in french. In painting, the fine, detailed rendering of objects to convey the illusion that the painted forms are real and three-dimensional.",Trompe l'oeil,General,Why did pirate wear earrings,Improve their eyesight, History & Holidays,In 1979 which English art historian was exposed as a one-time Soviet spy and stripped of his knighthood? ,Anthony Blunt  +General,Something arousing sexual desire,Aphrodisiac,General,Pedophobia is the fear of,Children,Entertainment,"Crosby, Stills and Nash's debut album included a song about a girl and the colour of her eyes. Name that song.",Sweet Judy Blue Eyes + Geography,Santiago is the capital of ______?,Chile,General,Which century saw the construction of the Taj Mahal,Seventeenth,General,What nationality is gabriela sabatini,Argentinian +General,"Princess Victoria, Duchess of Vastergotland, is the heir presumptive to the throne of which European country",Sweden, History & Holidays,"Who as Lord Protector of England, banned Christmas in 1647 ",Oliver Cromwell ,General,A kind of expanded polystyrene,Styrofoam +General,What cities gothic cathedral started 1386 was completed 1805,Milan,General,What's a natatorium,Swimming pool,General,Introduced in 1964 name Barbie's sister,Skipper +General,What began 24 Jan 1848 thanks to John Marshall at Sutters mill,The Gold Rush in California,Geography,Name the largest river forming part of the U.S. _ Mexican border.,Rio grande,General,In Britain since 1300 gold and silver hallmarked what in 1975,Platinum +General,"Decorations what shadow team driver was killed testing, prior to the 1974 south african grand prix",Peter revson,General,In Britain Stanstead airport was formally opened in which year,1991,Sports & Leisure,Which was the second club to win the English Premier League? ,Blackburn Rovers  +Entertainment,Current leader of Justice League America,Wonder woman,General,What colour is a Grasshoppers blood,White,General,Christian Commercial Travellers' Association of America are who,Gideons +General,What does it mean in the UK if a telephone number starts 0800,It’s a free call - toll free,General,How many consecutive years was the ed sullivan show on tv,23,General,What is the proper name for a watch pocket,Fob pocket +People & Places,"10 Matthew Street, Liverpool is the address of which famous venue? ",The Cavern ,Food & Drink,What Is Borsch ,A Russion Soup Including Beetroot ,General,Phil Collins appeared in which Spielberg film with Robin Williams?,Hook +Sports & Leisure,"Steve Davis reached every final of the Snooker World Championship between 1983 and 1989, but who were the two players who beat him (PFE) ? ",Dennis Taylor & Joe Johnson ,Music,"What Song Features The Lyric ""Please Please Tell Me Now""",Is There Something I Should Know,Religion & Mythology,"In Egyptian mythology, what is the life force called?",Ka +General,Which stretch of water separates Sardinia and Corsica,Strait of bonifaccio,General,"Who was kidnaped on the night of March 1, 1932?",Charles Lindbergh Jr,Geography,Which is the most northerly town in the British Isles? ,Lerwick  +General,"Countries of the world:north eastern South America, the capital is Georgetown",Guyana,Toys & Games,What bowling term means three straight strikes,Turkey,General,"In the nursery rhyme, Simple Simon met a pieman going where",To the fair +General,February 1999 what was the fastest growing religion in the US,Sikh,General,In which English county is Tolpuddle,Dorset,General,Who plays 'Norman Bates' in Hitchcock's classic thriller Psycho,Anthony perkins +General,Emerald is the birth stone for which month,May,General,Who had a No. 1 hit record in 1964 with 'Don't Throw Your Love Away',The searchers,General,Morris Frank brought Buddy from Swiss in 1928 what was Buddy,First Seeing eye dog in USA +General,What make of car is a carrera,Porsche,Food & Drink,Cognac (brandy) and white creme de menthe make up which type of cocktail ,A Stinger ,General,-ologies: Study of the history and development of words?,Etymology +General,What was h.g wells' first novel,The time machine,Geography,Which state has the most hospitals,California,Music,Silk & Steel Went To No.1 In 1986 For Which Family Act,Five Star +General,The square root of 1 is,1, History & Holidays,What employment did patricia hearst claim when booked ,Urban terrorist ,General,What 17th century explorer was buried with a pipe and a box of tobacco,Sir +Science & Nature,Which Gland Produces Insulin ,The Pancreas ,General,Who owns the licensing rights to all star wars merchandise,George lucas,General,"What country is bounded in part by the Indian Ocean, Coral and Tasmen seas",Australia +Sports & Leisure,Which Super Bike Champion Acquired The Nickname 'The Lion King''? ,Carl Fogarty ,General,What recently independent country was formerly known as greenland,Kalaalit,Music,Which Superstar Was Subject To Child Abuse Allegations In 1994,Micheal Jackson +General,"What don't Gary and Wyatt do when they take a shower with Lisa in ""Weird Science?""",Take off their pants,General,"In 1964, who recorded ""baby love""",Supremes,General,"In Greek mythology, who defeated atlanta in a footrace",Melanion +General,The study of angles & their properties is called,Trigonometry,General,A dance with a fast or moderate tempo.,Allegro,General,What Was The First Single To Sell Over A Million Copies In The UK,Bill Haley / Rock Around The Clock + Geography,In what country is the currency named after the water being called the Pula?,Botswana,General,What was Potsie's last name on Happy Days,Weber,Music,Which Pop Duo Featured In All 3 Of Tottenham Hotspur’s UK Top Ten Hits,Chas & Dave +Music,"Dion DiMucci Headed A Group Named After An Avenue In The Bronx, What Was The Group Called",The Belmonts,General,What does a phillumenist collect,Matchbox labels,General,In The Classic 1969 Blue Peter Clip An Elephant Decides To Relieve Itself On Live TV What Was It's Name?,Lulu +General,All hospitals in Singapore use what brand name product,Pampers,General,What is the most common name for Playboy centrefolds,Susan,General,Which insect is the most eaten as a delicacy,Grasshopper +General,"Ernest Hemmingway, Oscar Wilde, Ranier Maria Rilke - Common",Mothers made them Crossdress,Science & Nature,What Animals Name Literally Means 'Man Of The Forest'? ,Orang-utan ,General,Which of Henry the Eights wives was the widow of elder brother,Catherine of Aragon +General,How many squares on shogi (japanese chess) board,81,Science & Nature, The mudskipper is a fish that can actually __________,Walk on land,Technology & Video Games,What does the acronym HTML stand for?,Hyper Text Markup Language +Sports & Leisure,What Sport Do The Harlem Globetrotters Play ,Basketball ,General,"Americans spend more than how much a year on cosmetics, toiletries, beauty parlors & barber shops",52 million,Geography,What is the most populous city in North America,Mexico +Food & Drink,What kind of nuts are used in marzipan ,Almonds ,General,In Star Wars what is the Emperors last name,Palpatine,General,Where did george harrison discover gold,Witwatersrand +General,Which Actor's Auto Biography Was Called “The Moon's A Balloon?,David Niven,General,What is canis lupus,Wolf,Entertainment,"Bugs always finds himself at the wrong end of a gun, usually toted by either Elmer Fudd or who?",Yosemite Sam +General,Writing with light is the literal meaning of what word from Greek,Photography,General,Who were the twin sons of zeus and leda,Castor and pollux,Music,Men At Work Came From A Land Down Where,Under + History & Holidays,How did John Belushi die? ,Drug and Alcohol overdose ,Music,In 2000 Oxide & Nautrina Had A Hit With The Theme To Which Saturday Night TV Series?,Casualty,General,Which silent film actor was known as 'The Great Stone Face',Buster keaton +General,What Ferrari model was named after the makers son,Ferrari Dino,Geography,The Auckland Islands belong to which country,New zealand,General,What is the chemical symbol for silicon,Si +General,Who wrote the famous poem daffodils,William wordsworth, History & Holidays,Who was responsible for the infamous assination attempt on then President Reagan? ,John Hinkley Jr. ,Music,"Who Had A Hit With The Song ""Forever Now"" In 1984",Level 42 +General,Arthur Flegenheimer became notorious under what name,Dutch Schultz,General,In superstition if you marry on Saturday you will have what,No luck at all,General,Who is married to valerie bertanelli,Eddie van halen +General,What is the term for the brain's nerve cells,Neurons neuron,General,"Football Team, cleveland ______",Browns,General,In a church what is a galilee,Porch +General,What is a group of this animal called: Pheasant,Nest nide bouquet,General,What is Supergirls Kryptonian name,Kara,General,The cortex & medulla are parts of what organ,Kidney +General,A brown crayon is what color,Brown,General,"Who said ""The die is cast"" (on crossing the Rubicon)",Julius Caesar,General,Microphobia is the fear of,Small things +General,Which waterfall has the highest drop in the world,Angel falls,General,Born January 21st to February 19th what star sign are you,Aquarius,General,"Who portrayed the uncle on tv's ""family affair""",Brian keith +General,How many strigs can have a banjo,Four five,General,What's the capital of the central african empire,Bangui,General,What date was V E day,May 8 1945 +Geography,"Which country administers South Georgia, a last stop before Antarctica?",Great britain,General,Which group believes in The Great Architect of the Universe,Freemasons,Science & Nature,What chemical element gets is name from the greek word meaning 'stranger'?,Xenon +General,Who featured on the cover of the first Rolling Stone magazine,John Lennon,General,The worship of a material image that is held to be the abode of a superhuman personality,Idolatry,General,Who Was The First Ever Person Ever To Sing A James Bond Theme,Matt Munroe +General,What is a Charollais,Type of Cattle,General,What is specifically defined as 1/48th of an inch,A Hairsbreadth,General,Which cereal diseases can cause food poisoning and gangrene of the fingers if consumed,Ergot +General,Who played Miss Marple in 6 films (both names),Margaret Rutherford,General,What part of las vegas has been designated an official scenic byway,The, History & Holidays,"Three of Santa's reindeers have a name beginning with the letter D, Dasher, Dancer and who else ",Donner  +General,Old Arabic word for palm of the hand is what in modern sport,Racket,General,Who drove the McLaren to its first grand prix victory in 1968,Bruce McLaren himself,Music,Who Wrote White Christmas While Sunning Himself In Californa,Irving Berlin +General,What's the term for the geographical dividing line N/S Korea,38th Parallel,General,What is the literal meaning of Cenotaph,Empty Tomb,General,In what month is Bastille Day,July +General,"American inventor, whose development of a practical electric light bulb, electric generating system, sound-recording device, & motion picture projector had profound effects on the shaping of modern society",Thomas edison,General,An IVP is used to detect what medical condition,Kidney Stones Intravenuspylorigram, History & Holidays,Which Islands In The Indian Ocean Gained Their Independence From Britain In 1976 ,The Seychelles  +General,This poisonous gas is in the exhaust fumes from cars,Carbon monoxide,General,What country has the third most satellites in orbit,France,Art & Literature,Which is the largest museum in the world?,Louvre +General,Marilyn Louis born 1922 changed her name to what star of 40s,Rhonda Fleming,General,What is switching letters (e.g saying jag of flapan instead of flag of japan),Spoonerism,General,The Blue Mountains can be found on which Caribbean island,Jamaica +General,In the Beatles White Album who was Martha my Dear,Paul McCartney's Sheepdog,General,"Which French artist, in 1883, gave up his job as a stockbroker to paint full time",Paul gauguin,General,What is keranothenatophobia,Fear of artificial satellites falling on one's head +Music,Which ex-Animal was Jimi Hendrix's manager?,Chas Chandler,General,A bantling is a(n)___.,Young child, History & Holidays,When does Santa deliver presents? ,Christmas Day  +Entertainment,What is the name of Duddley Do-Right's horse?,Horse,Geography,"The _______________ comprises an area as large as Europe. Its total land mass is some 3,565,565 square miles.",Sahara desert,Science & Nature,What Is The Scientific Study Of Birds Called? ,Ornithology  +General,"Who was the authorship of ""Jogfree of Canada"" credited to",Charlie farquharson,General,In the original Star Trek series name Spock's mother,Amanda,Music,"Who Recorded The Albums ""Cats Without Claws"" & ""Another Place In Time""",Donan Summer +General,The French army were the first to use this type of communication,Semaphore,General,What is India's National Flower,Lotus, Geography,What is the capital of Cuba ?,Havana +General,Which country was the proposed target for the German 'Operation Ikarus' in World War II?,Iceland,Food & Drink,What Is The Name Of The Baked Batter Portions Traditionally Served With Roast Beef In Britain ,Yorkshire Pudding ,General,Alkaloid derived from morphine and used as a pain killer,Codeine +General,What was the name of the newspaper published by charles foster kane,Enquirer,Science & Nature, __________ can travel up to 40 miles per hour.,Sharks,General,What country had the first banknotes,Sweden China paper not banknotes +General,In Charleston by law carriage horses must have what,Diapers on,Science & Nature,Hydrogen Hydroxide is more commonly known as what?,Water,General,Which Very Famous Person In World History Married “Clementine Ogilvy Hozier” In 1908?,Sir Winston Churchill +Music,What Is The Very First Word To The Tammy Wynette Classic “Stand By Your Man”,Sometimes,General,"After Russia and Kazakhstan, what is the third largest of the former Soviet republics",Ukraine,General,What agency began as a group of mercenary guards,Texas Rangers +General,In Odessa Texas Star of David and Peace symbol are what,Banned satanic symbols,General,What building defect was the title of along running TV comedy series,Rising damp,General,The marimba is a deeper toned form of which instrument,Xylophone +General,What utopian community in Florida experienced its first armed robbery in 1998,Celebration,Music,Who Sang The Title Track To The Blockbuster Movie Ghostbusters,Ray Parker Jnr,General,Musa acuminata is what favourite food item,Banana + Geography,What country is located between Panama and Nicaragua?,Costa Rica,Science & Nature," Today's oldest form of horse is the Przewalski, or Mongolian Wild Horse. Survivors of this breed were discovered in the Gobi Desert in __________",1881,General,Which Roman Emperor made Sunday a religious holiday,Constantine +General,What kind of floor covering is 'lino' a short form for,Linoleum,General,What sort of animal is an anopheles,Mosquito,Music,Which Nenry Mancini Song Does Andy Williams Employ As His Theme Song,Moon River +General,Hockey: what divides the rink laterally into three equal areas,Blue line,General,What is the fear of specters or ghosts known as,Spectrophobia,General,The Irawaddy runs through which country,Myanmar +General,If you suffered from pruritus - what would be wrong,Itching,Music,"Which 2 Groups Combined On The 1969 Hit ""I'm Gonna Make You Love Me""",Diana Ross & The Supremes & The Temptations,General,Only 15% of French wines have what on the label,Appellation Controlee +General,Who said 'eureka',Archimedes,General,Who presents the ITV show 'Fool Britannia'?,Dom Jolly,Technology & Video Games,"Nintendo's mascot, Mario, has a last name. What is it? ",Mario +General,"The biggest beer selling establishment in the world is the Mathaser. In what city, the capital of Bavaria, is the Malthaser located",Munich, History & Holidays,What was the name given to the atom bomb that was dropped on Hiroshima?,Fat Man,General,According to one estimate Benjamin Franklin had 24 what,Bastards – Illegitimate children + History & Holidays,What snack food was portrayed in claymation dancing to 'Heard it Through the Grapevine'? ,Raisins ,General,What's the zodiacal symbol for Virgo,Virgin,General,What is the common name for a hammer with a wooden head,Mallet +General,Enterprise Alabama they erected a monument to which insect,Boll weevil no cotton better cash crops,General,Who was the first American in space - twice,Gus Grissom,General,Whose last words were 'thank god i have done my duty',Horatio nelson +General,The pyramids are built of which rock,Limestone,General,Epaminondas was the general of which ancient state,Thebes,Sports & Leisure,In which sport does Lee Westwood play? ,Golf  +General,What planet was Spock brought back to life on?,Genesis,Food & Drink,If you were eating escargots in a French restaurant what would you be eating? ,Snails ,General,Stalingrad who was the only sister on petticoat junction to marry,Betty jo +General,"In what sport do the participants compete in the ""grand nationals""",Auto,General,In what country did the word plonk meaning wine originate,Australia,General,The Bronte sisters had a brother name him,Branwell +General,What was the name of the school mistress in The facts of Life?,Mrs. Edna Garrett,General,A web site with sa in the name is in what country,Saudi Arabia,Music,Who were Dancing Naked In The Rain In 1990,Blue Pearl +General,Which Argentinian politician commanded the invasion of the Falkland Islands,General galtieri,Science & Nature," In Alaska, it is legal to shoot __________. However, waking a sleeping bear for the purpose of taking a photograph is prohibited.",Bears,General,What game show had people dressed up funny?,Let's Make A Deal +Science & Nature,How Frequently Are Twins Born In The UK ,About Every 80 Births ,General,What is the nickname for Oregon,Beaver state,General,Who was the father of King Richard 1,Henry ii +General,What year was the dental drill invented,1790,Science & Nature,A baby doctor is a _________.,Pediatrician,General,"The protection & improvement of the health of the public through community action, primarily by governmental agencies",Public health +Music,"Til Tuesday Had A Hit With The Song ""Voices Carry "" Or ""Carry Me Home""",Voices Carry,General,In Which US Statet Was The Classic TV Show The Dukes Of Hazzard Actually Set,Georgia,Geography,Rabat is the capital of which country,Morocco +General,What is measured by a chronometer?,Time,General,A Rafter is a collection of what creatures,Turkeys,General,What colour was diana spencer's engagement photograph suit,Blue +General,What are the 2 background colours of the Welsh flag?,Green and White,Sports & Leisure,Who Was the First Britain To Hold A World Javelin Record ,Fatima Whitbread ,General,In 1951 which (of two) car companies introduced power steering,Buick - Chrysler +General,A calander usually withastronomical data is called ..,Almanac,General,The Valentine Brothers had a 1983 US hit with `Money's too tight (to mention)' but who took the song to number 13 in the UK 2 years later ,Simply Red ,Music,What Nationality Is Tina Arena,Australian +General,What is The Daily Planet in Australia,Biggest Brothel – Listed company,Music,"Which Songwriting Duo Wrote A String Of Rock N Roll Classics Including ""Hound Dog, Stand By Me, Up On The Roof & Broadway""",Jerry Leiber & Mike Stoller,General,Johnny and the Moondogs was the original name of which pop group,The beatles +General,Jacqueline du Pre was a soloist on which instrument,Cello,General,Name Captain Nemo's pet seal,Esmeralda,General,"What do you call an emasculated ram, whether or not he wears a bell",Wether +General,Which character was portrayed by Meryl Streep in the film Out of Africa,Karen blixen,Entertainment,"What is the name of the Volkswagen in the film, ""The Love Bug""?",Herbie,General,What creature is depicted on the Welsh Flag?,A Red Dragon + History & Holidays,Who Released The 70's Album Entitled Oxygene ,Jean Michel Jarre ,Science & Nature,"Who said all things were made up of air, earth, fire, and water?",Aristotle, History & Holidays,In which Hitchcock classic does James Stewart play a man hired to follow Kim Novak? ,Vertigo  +General,Which author sold most books in the first half of 20th century,Edgar Rice Burroughs,General,The Egg and I was whose first film,Marilyn Munroe,General,When were film audiences treated to a flushing toilet for the first time,Psycho +General,In squash what colour dot indicates the slowest ball used,Yellow,General,What was Roscoe's dogs name on the Dukes of Hazzard?,Flash, History & Holidays,*What was the name of the computer in TV's Wonder Woman?* ,IRAC  +General,Who invented the air brake,George westinghouse,Music,Name The 2 Mebers Of The Eurythmics,Dave Stewart & Annie Lennox,General,Which part of the human body contains the most gold,Toenails +General,Flying fish is the national dish of which country,Barbados,General,"Founded in the 6th Century B.C., which religion has two schools - the Hinayana, or Lesser Vehicle, and the Mahayana, or Greater Vehicle",Buddhism,General,What is a group of this animal called: Parrot,Company + History & Holidays,What is the only national spectator sport originating entirely in the united states ,Rodeos ,General,1937 saw the first BBC TV broadcast of which event,Wimbledon Tennis,Science & Nature,Of what is keratitis an inflammation?,Cornea +General,Which is the second largest city in england,Birmingham,General,Singapore lies at the end of which peninsula,Malayan,General,What is the fear of menstruation known as,Menophobia +General,What is the most common street name in the cities of the USA,Park street,General,"General ""Choi Hong Hi"" Is Responsible For Developing Which Martial Art",Taekwondo,General,The pH scale is a logarithmic scales used to measure acidity in solutions. What does pH stand for?,Potential of hydrogen +General,In legend what did Cleopatra have her mattress stuffed with,Fresh rose petals nightly,General,What is the motto of the rolling stone magazine,All the news that fits,General,"In 1967, which airline became the first all-jet airline",TWA +General,New delhi is the capital of ______,India,General,Who founded the Samaritans in 1953 (both names),Chad Varah, Geography,What is the basic unit of currency for Macao ?,Pataca +Geography,What south American country has both a Pacific and Atlantic coastline,Colombia,Geography,Name the capital of brazil. ,Brazilia ,General,James Doohan became famous playing what character,Montgomery Scott +General,Strasbourg is the administrative centre of which French region,Alsace,General,Who turned down the TV role of Doctor Kildare,William Shatner, History & Holidays,Name the author of The Novel Frankenstein ,Mary Shelly  +General,Which country is reputed to have the world's oldest flag design?,Denmark,General,What links Sergeant Joe Friday and Babe Ruth,714 - Fridays badge no Ruth record,Science & Nature,For What Is NaCI The Chemical Formula ,Salt  +General,What should be given on the 9th wedding anniversary,Pottery - some say willow,General,What is a male deer,Buck,Music,Crimson & Clover Or Crimson & Lace Was A Hit For Joan Jett,Crimson & Clover +General,Calamine is the ore what is the product,Zinc, History & Holidays,"Designed By Robert Fulton, Which Weapon Was Tested In The Seine In 1801? ",The Submarine ,General,Which American city is served by Logan International Airport,Boston +General,Who was jason lee's father,Bruce lee,Entertainment,"This term means to play crisply, with the notes separated?",Staccato,General,"Who wrote ""alexander's ragtime band""",Irving berlin +General,What is a group of elk,Gang,General,All methods used to prevent or reduce detrimental effects of floods,Flood control,General,Apart from eggs what is in an Arnold Bennett omelette,Smoked Haddock +General,US Fed laws specify what colour underwear for Crash Dummies,Tea Rose,General,What is the fear of penis known as,Phallophobia, History & Holidays,Who rode naked through the streets of Coventry?,Lady Godiva +General,What are you doing if you 'yarn over' or 'pop corn',Knitting,Toys & Games,"In which game might a person have a ""full house""?",Poker,Music,How Many Years Were There Between Blondies Fifth & Sixth No.1 Hits,17 + History & Holidays,Where was the Rosetta stone found ?,Cairo,General,"Apart from milk, what is the chief ingredient of Lyonnaise sauce",Onions,Entertainment,"Who plays many voices, such as Dr Nick, and Moe on 'The Simpsons'?",Hank Azaria +General,"Vodka, Orange juice and Cranberry juice make which cocktail",Madras,General,Where did 'paella' originate,Spain,General,What's the only insect that produces food humans eat,The honeybee honeybee +Geography,"Arguably the largest state in the world, _______________________ covers one_third of the Australian continent. It spans over 2.5 million square kilometers (1 million square miles).",Western australia,General,Which ocean lies north of Guyana,Atlantic,Geography,What Is The Currency Of Israel ,The Shekel  +General,What james dickey novel tells the story of an ill-fated canoe trip,Deliverance,General,Valence when did the first man walk on the moon,1968,Art & Literature,In which John Steinbeck story features the slow-witted Lennie and his friend George? ,Of Mice & Men  + History & Holidays,What was the sister ship of the olympic ,The titanic ,General,Where is the worlds oldest university,Fez Morocco – founded 859,Music,"Who In The World Of Music Was Born With The Name ""Declan McManus""",Elvis Costello +General,Which U.S. president gave the 'four freedoms of democracy' speech- ie freedom from want; freedom from fear; freedom of worship and freedom of speech,Franklin d roosevelt,General,Chili peppers he worked in a factory making toilets for airplanes before he recorded 'aint no sunshine when shes gone',Bill withers,General,Which European country is ruled jointly by the Spanish Bishop of Urgel and the President of France,Andorra +General,Smetana in what novel do we find winston smith living in the nation of oceania and battling doublespeak and the thought police,1984,Music,"What was launched on September 30th , 1967?",Radio One,General,"In cookery, what does the term ""Macedoine"" mean",Diced + History & Holidays,Name The Queen Of England Who Ruled For Nine Days? ,Lady Jane Grey ,General,Who rode a horse called Copenhagen,Duke of wellington,General,What is the flower that stands for: elevation,Scotch fir +General,Cernan and Scmitt were the last men to do what,Walk on the moon,General, What is a dried plum called,Prune,Food & Drink,Which fish is smoked and cured and called 'finnan'? ,Haddock  +General,Every human has one of these on their tummies,Navel,General,"Whose epitaph says ""If you seek his monument look around you""",Sir Christopher Wren,General,Which country has won the most Nobel Prizes for Literature,France +General,Mafalde is what shaped pasta,Corrugated Strips,Entertainment,Who sang 'Born In The USA'?,Bruce Springsteen,General,Who created the round table,Merlin +Entertainment,"The two rival gangs in ""West Side Story"" were the Sharks and the _________.",Jets,Science & Nature,"What bird is an excellent swimmer, but can't fly?",Penguin,General,"Flying corps what word comes from latin ""monachus"", meaning ""one who lives alone""",Monk +General,Sildenafil Citrate Is the Chemical Name For What,Viagra,General,When was the first black mayor of chicago elected,1983,General,Whats the first book of the Old Testament,Genesis +General,How was uther pendragon killed,Ambush,General,What is a lipid,Fat,General,In Heraldry what is a mullet,A Star +General,If a flower is described as 'stellate' what shape would it be,Star shaped,General,"What one word may be added to news, carbon & wall",Paper,General,In Pennsylvania it is illegal to sell fireworks to who,Pennsylvania residents +General,Jonathan Buttall is well known in art by what name,Gainsborough's Blue Boy,General,Which skill uses things called chain singles and doubles,Crocheting,General,Where was rory gallagher born,Ireland +General,What island is due south of Corsica,Sardinia,Music,"What Was The Christian Name Of ""Moon The Loon""",Keith,Art & Literature,What is an Icelandic epic called?,Saga +General,Jacques Cousteau made his name in underwater exploration. What was the name of his scientific survey ship,Calypso,General,What is the highest mountain in Romania,Mt moldoveanu,General,What is the Capital of: Saint Kitts and Nevis,Basseterre +General,What was the police chiefs name in the first two Jaws films,Martin Brody,Sports & Leisure,Which female tearful athlete was BBC Sports Personality of the Year in 1987? ,Fatima Whitbread ,General,If someone Inpissates a soup what have they done,Thickened it +Music,Which Instruments Were Played By Motown Session Men James Jamerson & Benny Benjamin Respectively,Bass & Drums,General,Kenophobia is the fear of,Voids,General,What's the middle day of a non-leap year,July 2nd +General,What is the english for umbrella,Little shaded area,General,What is the flower that stands for: esteem but not love,Spiderwort,General,Name the hardest substance in the human body.,Enamel +General,What is a group of this animal called: Whale,School gam pod,Sports & Leisure,Why is September 28 th 1996 a famous date in horse racing? ,Frankie Dettori won 7 races in one day at Ascot ,General,Long billed wading bird with a musical cry,Curlew +Music,"Who Had Hits With ""It's The End Of The World As We Know It"" & ""Orange Crush""",REM,General,"Which entertainer has the nickname ""Slowhand""",Eric clapton,Art & Literature,Who Composed The Opera (Oedipus Rex) ,Stravinsky  +General,Suomi is the name the natives give to what country,Finland,General,In the Middle Ages people threw what at the Bride and Groom,Eggs,Food & Drink,Which fruit is traditionally covered with toffee at a fairground? ,Apple  +General,Alexey Pajitnov Created A Very Famous Video Game During 1985-1986 What Was It Called.,Tetris,Music,"Who Had A Hit In 1983 With ""Ziggy Stardust""",Bauhaus,General,What is the flower that stands for: belle,Orchis + History & Holidays,Of which ship was Miles Standish captain?,The Mayflower,General,What author was the first published by Bantam paperbacks,Mark Twain Life on the Mississippi,General,The Emperors cup is awarded in what sport,Sumo wrestling +General,What new england state would be home if you laid down roots in 'bald head',Maine,General,What state is 'the garden state',New jersey,General,From which musical does the song Getting to Know You come,The king and i +General,What is a cyclonic tropical storm with winds at the centre in excess of 74 miles per hour,Hurricane,Music,How Many UK Number Ones Has Britney Spears Had,3,General,What is the most chemically complex food - over 300 chemicals,Chocolate +Toys & Games,In this game players take turns placing disks on an 8x8 board.,Othello,General,Three hundred million ofwhat die in the human body every minute,Celles,General,President six u.s presidents never had ______,Children +General,Stevie Wonder won the 1984 Oscar for Best Song for I Just Called to Say I Love You. For which film was this song written,The woman in red,General,The U.S. patent was issued to Samuel Hopkins for what,Potash,General,Omphalitis is an infection of what part of the body,Navel +Science & Nature, A dog's __________ has over 200 scent receiving cells.,Nose,General,Which British driver won five British Grand Prix races in the 1960s,Jim clark, History & Holidays,"Who was ""The Mad Monk""",Rasputin +General,Who 'schusses' down a piste,Snow skiers,General,Jazz Trumpeter John Birks was better known as who,Dizzy Gillespie,General,In what sport did Prince Leopold of Bavaria compete,Motor Racing +Music,Which Funny Girl Sang About A Re-Cycled Flower In 1966,Barbara Striesand (Secondhand Rose),General,What remarkable record was set in 1997 by Jeanne Calment in France,Oldest ever person,General,"Which architect designed the Royal Observatory in Greenwich, UK",Sir christopher wren +General,What year did the first nudist colony open,1903,General,In cooking what does MSG stand for,Monosodium glutamate,Science & Nature,In which branch of science are monocotyledon and dicotyledon terms?,Botany +Food & Drink,Which sweet treat could be found at the Ambassador's parties? ,Ferraro Rocher , Geography,What two US states are rectangular?,Colorado and Wyoming,General,What country is the largest Spanish-speaking country in the world?,Mexico +Music,Which Song Has Spent The Most Weeks In The Singles Chart Regardless Of Artist,My Way,General,Whats the common name for the drug acetaminophen,Paracetamol,General,Who was the only prophet that never died,Elijah +Food & Drink,What nationality is the lager giant 'Grolsch'? ,Dutch ,General,What is the tail fin of a fish called,Caudal Fin,General,Which Italian seaport city is called 'La Superba',"It's not venice, as you might think.genoa" +General,Who wrote The Sign of Four and The Valley of Fear,Conan doyle,General,What is penicillin made of,Cheese mould,General,What was gangsters George Nelsons nickname,Baby Face +General,"What are Blue Professor, Bottle Imp and Rat Faced McDougal",Fishing Flies, History & Holidays,What is a Witches Familiar ,A Black Cat ,General,What Is The Study Of Very Low Temperatutes Called,Cryogenics +General,Phobos and Deimos are moons of Mars - what do names mean,Fear and Terror,General,What is the heaviest breed of domestic dog,St. bernard,General,The Belcher Islands are in what bay,Hudson + Language,What is the official language of Senegal?,French,General,What was the name of the vehicle driven by the Anthill Mob in the Wacky Races series of cartoons??,Bulletproof Bomb,Music,Which British Prime Ministers Are Mentioned In A Beatles Song,Mr Wilson & Mr Heath In Taxman +General,Madam Hooch teaches what at Hogwarts school,Quidditch,General,Mickey Mouse's Pluto had what name when he first appeared,Rover,General,What fruit is missing seeds,Navel orange +General,Triomphe what is the pope's pontificial ring,Fisherman's ring,General,On average the French eat 20 million what a year,Frogs,General,What is Australia's Barossa valley known for,Wine production +Geography,"From the 1830s to 1960s, the Lehigh River in eastern _____________, was owned by the Lehigh Coal and Navigation Co., making it the only privately owned river in the United States.",Pennsylvania, History & Holidays,"In March 1979, where did a major nuclear accident occur ?",Three Mile Island,General,What translates as The fist foot way,Taekwondo +Food & Drink,What words do SPAM take its name from? ,Shoulder Pork And Ham ,Music,"Who Had Hits With ""I'm Walking Backwards For Christmas"" & ""The Ying Tong Song""",The Goons,Geography,Which Bridge Features On The Poster To The Woody Allen Film Manhattan ,Queensboro Bridge  +General,How long did the israelites wander in the desert after the exodus,Forty,General,Which country has the juno awards,Canada,General,What are the two basic aids in orienteering,Map and compass +General,Fingal O'Flaherty Wills is better knows as who,Oscar Wilde,Science & Nature,"From what animal is ""ambergis"" obtained",Sperm whale,General,Pteronophobia is the fear of,Being tickled by feathers + Geography,What is the capital of Nigeria ?,Abuja,Sports & Leisure,Baseball: The Houston ______?,Astros, Geography,What is the capital of California?,Sacramento +General,The universe what steve martin film had him go from rags to riches to rags,The jerk,General,What is the ancient religious language of india,Sanskrit,General,Who was the last Roman Emperor of the Julio-Claudian family,Nero +General,Andr'e Gide the writer was expelled from school for what crime,Masturbating during lessons,General,Which animal always grows new teeth to replace the old,Crocodile, History & Holidays,Xavier Roberts was the name associated with which eighties toy? ,Cabbage Patch Kids  +Entertainment,"What is the title of the 1996 sequel to ""Terms of Endearment""?",Morning Star,General,At what does Singapore use the colours blue & yellow to ward off evil spirits,Funerals,General,"Stan laurel, Mickey Rooney, Lana Turner what in common",8 marriages +General,Which film starring julie andrews and christopher plummer won the oscar for best picture in 1965,Sound of music,General,What is the worlds most widely used vegetable,Onion,General,Who was assasinated by Gavrilo Princip to spark the beginning of World War I,Archduke Ferdinand +General,"The upper horizontal part of a classical order, between a capital and the roof; it consists of the architrave, frieze, and cornice.",Entablature,Entertainment,Russian modernist Igor _________,Stravinsky,General,Stronger how many legs does a crab have,Ten +People & Places,What Nationalaty Was Hans Christian Andersen ? ,Danish ,General,What Saint said - Lord grant me Chastity - but not ye t,St Augustine,People & Places,Which Hollywood Star Sparked Outraged In 2006 When They He Declared That He Planned To Eat The Placenta Of His Then Unborn Child ,Tom Cruise  + History & Holidays,"How many sides does a snowflake have? 6,8,10,12 ",6 ,Sports & Leisure,Who was the first unseeded man to win Wimbledon? ,Boris Becker ,General,Termites eat wood twice as fast if what is happening,Heavy Metal Music Playing +General,"Jon Ansell, Matt Stiff, Michael Christie & Ben Thapa Are Better Known As What",G4 (The Pop Group),Music,Which Members Of The Royal Family Attended The Opening Of The Live Aid Concert,The Prince & Princess Of Wales,General,What hobby was developed by the Palmer Paint Company?,Painting by numbers +General,Which movie about a TV news show won Peter Finch a posthumous Oscar,Network,General,What's the northernmost scandinavian country,Norway,General,Who first starred in the film The Boy in the Plastic Bubble,John Travolta + History & Holidays,Who Led A 125 Mile March Of Child Worker To Theodore Roosevelt's Vacation Home On Long Island ,Mary Harris Jones ,Science & Nature,The Perfection Of Which Invention Led To The Industrial Revolution ,The Watt Steam Engine ,General,The praying mantis is the only insect that can do what,Turn its head +Science & Nature,What Does Mass Multiplied By Velocity Give You ,Momentum ,Entertainment,Which instruments is used to tune the orchestra?,Oboe,General,The Templeton prize is awarded annually for progress in what,Religion +General,"How does the mermaid buy the gift for tom hanks in ""splash""",Her necklace,Science & Nature, Wildlife biologists estimate that as many as five out of six fawns starve to death during a hard winter in __________,Vermont,General,Whose nose grew when he told a lie,Pinocchio +General,What's Prince Charles' favorite cereal,Cornflakes,General,Naval escort vessel,Frigate,Geography,Which Carthaginian Navigator Founded 6 Colonies Along The African Coast & Reach The Bight Of Benin ,"Admiral Hanno, Who Set Sail In 425 Bc " +Entertainment,Who was Daisy the dog's owners,Blond,Art & Literature,Who Was William Shakespear's Wife ,Anne Hathaway ,Food & Drink,Where is the best brandy bottled?,Cognac +Science & Nature,"During pregnancy, how many times its normal size does the human uterus expand?",Five hundred,Music,"Which Band Had Hits With ""Goody Two Shoes"" & ""Stand And Deliver""",Adam And The Ants, Geography,What is the world's largest sea?,Mediterranean +Food & Drink,What Is Black & Tan ,A Mixture Of Beer & Stout ,General,Which three words complete the full title of Shakespeare's play Hamlet - - -,Prince of denmark,General,Who would use a Syllabary,Arabs - their alphabet + Geography,In which country is the largest active volcano in the world?,Ecuador,General,What is the proper term for a guinea-pig,Cavy,Music,"Name The Band: Wyclef Jean, Lauryn Hill, Pras MKichel",The Fugees +General,What is the flower that stands for: menatl beauty,Clematis,General,Which herb is traditonally associated with forgetfulness?,Rue,General,What is the most populous city in the US state of Missouri?,Kansas City +General,When Saigon fell the signal for all Americans to evacuate was what song by Bing Crosby being played on the radio,White christmas, History & Holidays,"Which 50's Movie features the Line ' I'll follow him around the Horn, and around the Norway Maelstrom, and around perditions flames' ",Moby Dick ,General,Where is taipei,Taiwan + Geography,What is the world's highest mountain ?,Mount Everest,Music,What Is Beethoven's Third Symphony Entitled,Erocia,General,"In The TV Show ""Birds Of A Feather"" What Is Doriens Husband Called",Marcus +General,What is a group of this animal called: Marten,Richness, History & Holidays,Which reindeer isn't mentioned in the poem 'The Night Before Christmas'' ,Rudolph ,Geography,"The ________ river has frozen over at least twice, in 829 and 1010 A.D.",Nile +General,In Oct 1992 Bernard Lavery of Wales grew an 18lb 3oz what,Brussels Sprout,General,February 1865 only month ever not to have what,Full Moon,General,In which field was Maria Montessori famous,Education +General, This instrument measures atmospheric pressure.,Barometer,General,"Which ""Shakespearean"" horse won the Champion Hurdle in 1973 and 1975",Comedy of errors, History & Holidays,For what country did Columbus make his historic voyage,Spain +General,In which county in the UK are the Heights of Abraham,Derbyshire,Music,"Who Was Lead Singer With Harold Melvin & The Bluenotes On Such Songs As ""The Love I Lost"" & ""If You Don't Know Me By Now""",Teddy Pendergrass, Geography,What is the largest island in the Mediterranean?,Sicily +General,What Is The Only Country With A Bible On It's Flag,Dominican Republic,General,"Mortuary custom, the art of preserving bodies after death, generally by the use of chemical substances",Embalming,General,What is the common name given to the larvae of a crane fly,Leatherjackets +General,In what country is the Penina golf course,Portugal,General,"In London what links Lambeth, St James and Westminster",All Palaces,Food & Drink,What Type Of Drink Lended Itself To The Title Of A Number One Single For All Saints? ,Black Coffee  +General,In the film Jumping Jack Flash what is Jack's code key,B Flat,Science & Nature," The hides of mature female blue __________ are more than twice as thick as those of males, probably as a protection against courtship bites.",Sharks,General,Which Classic Cartton Character Had A Sidekick Named Dum Dum,Touche Turtle +General,"Who recorded the album ""zoot allures"" in 1976",Frank zappa,Geography,"In 1932, Porto Rico was renamed ______________",Puerto rico,General,"Who sang ""help me i think i'm falling""",Joni mitchell +General,How many islands make up hawaii,20,General,Name Stephen King's first published novel,Carrie,General,How many chambers does the human heart have,Four + History & Holidays,Which popular poem was alternatively known as A Visit from St Nicholas? ' ,Twas The Night before Christmas ,General,Who are Patience and Fortitude at New Yorks Public Library,Stone Lions Outside, Geography,In what country is the Jutland peninsula?,Denmark +General,The sequel to which famous novel was called 20 years after,The Three Musketeers,General,What is the name given to a chemical that is used when a dye will not fix directly on to the fabric,Mordant,General,"Who, in 1889, painted the picture called The Starry Sky",Vincent van gogh +Music,Who Plays Drums For Fleetwood Mac,Mick Fleetwood,General,Which Football Team Held On To The FA Cup For 7 Years Despite Only Winning It Once,Portsmouth,Sports & Leisure,What is the only Swimming Stroke not Started by a dive? ,BackStroke  +Entertainment,Who recorded 'A Boy Named Sue'?,Johnny Cash,General,"One Tin Soldier' recorded by Coven, was the theme song for what movie",Billy Jack,Science & Nature, Komodo dragons eat deer and wild __________,Boar +General,Where was the 'Australian' swimmer John Konrads born,Latvia,General,What TV show was set in Wentworth Detention Centre,Prisoner Cell Block H,General,Which athletic event requires five judges,Triple Jump +General,The world's longest tunnel connects Delaware and ______?,New York,General,In The World Of Literature “Oliver Mellors” Is Better Known As Who?,Lady Chaterlys Lover,General,Who was the only civil war soldier executed for war crimes,Major henry +General,What is the correct name for a castrated pig,Barrow,General,Who played Axel Foley's best friend in Detroit?,Paul Riser,Music,Whose real name is Robert Zimmerman?,Bob Dylan +General,Homophobia is a fear of ______,Monotony,Food & Drink,"Cocktails: Whiskey, hot coffee, and whipped cream make a(n) _________.",Irish coffee,General,Chrissie Hynde was in which early eighties group?,The Pretenders +General,Where Will You Find The Character “ Jake The Jailbird ”?,On A Monopoly Board,General,The detective Miss Marple was created by which writer,Agatha christie,General,Knock-Knock was the first cartoon starring what character 1940,Woody Woodpecker +General,A Myologist studies what,Muscles,General,Traditional Advent candles are two colours pink and what,Purple,General,"In To Kill a Mockingbird, who played the role of Boo Radley",Robert duvall +General,What did John Montague invent,The sandwich – He was 4th Earl,Science & Nature,Who first used antiseptics ?,Joseph Lister,General,"Which writer's first novel, published in 1948, was Never Love a Stranger - his 23rd and latest, published in 1997, is Tycoon",Harold robbins +General,Lorne Green has only one what (an alligator ate the other),Nipple,General,Who made his screen debut in Mad Dog Col 1961 as a cop,Gene Hackman,General,What song links Little Eva and Kylie Minogue,The Locomotion +General,What is a half of a half of a half of a half,A Sixteenth, Geography,What is the capital of Congo ?,Brazzaville,General,Which cosmetics giant began in 1886 as the California Perfume company,Avon +Tech & Video Games,When was the first 'Final Fantasy' created? ,1987,General,Where does the American flag fly 24 / 7 never taken down,Moon,General,The second space shuttle is named __________,Challenger +Music,What were the Beatles billed as when they recorded with Tony Sheridan?,The Beat Brothers,General,"In The Tv Show ""The Simpson's"" Who Provides The Voice Of Homers Mother",Glen Close,General,What does cat stand for,Computerised axial tomography +General,Who was the star of 'riverdance' and 'lord of the dance',Michael flatly,Religion & Mythology,"Of the 266 popes, how many died violently?",Thirty three,General,With what day does a month start if it has a Friday the 13th?,Sunday +General,"Lb What was the nationality of the abstract artist, Mondrian",Dutch,Music,Who Did The Byrds Ask To “Play A Song For Me” In 1965,Mr Tambourine Man,Music,Who Was The Owner Of Manchesters Hacienda Club & Factory Records,Tony Wilson +General,Which mythical UK creature becomes an evil boggart if annoyed,Brownie,Sports & Leisure,Who's the current womens world darts champion? ,Trina Gulliver ,General,"Administrative division of the kingdom of great britain, occupying the northern third of the island of great britain",Scotland +General,Who wrote the song 'do they know it's christmas' with bob geldof,Midge ure,General,What was Capability Brown's real first name,Launcelot,General,Where is Judge Dread a judge,Mega City +Science & Nature,What's the more familiar name of the himalayas' yeti ,The abominable snowman ,Geography,What is the world's third largest sea? ,Mediterranean ,General,What does cobol stand for,Common business oriented language +Food & Drink,Which is Britain's top selling fruit flavoured soup? ,Tomato ,Art & Literature,"In 'Romeo and Juliet', who says 'make the bridal bed in that dim monument where Tybalt lies?",Juliet,General,What food was regarded as an aphrodisiac in the Middle Ages,Chicken Soup +Science & Nature," Tunas will suffocate if they ever stop __________. They need a continual flow of water across their gills to breathe, even while they rest.",Swimming,General,What Hollywood actress was the Laurence Olivier of Orgasms,Hedy Lamarr,General,What was the first LP record to sell over 1 million copies,Calypso by Harry Belafonte +Music,Of Which Rock Group Is Bono A Member,U2,Food & Drink,What is the main flavour in the following alcohols 'Curacao' ? ,Orange ,General,Where were the Toltecs from?,Mexico +General,Capital cities: Ghana,Accra,General,What is the national flower of Mexico,Dahlia,General,"Who was the guitarist in the 70's and 80's band, Be-Bop Deluxe",Bill nelson +Food & Drink,For What Drink Was Chicory A War Time Substitue ,Coffee , Geography,What is the capital of Guinea ?,Conakry,General,Which explores trip was sponsored by Ferdinand and Isabella,Christopher columbus +Geography,In Which Country Is The Suez Canal ,Egypt ,General,By what nickname is the well-known American singer Bruce Springsteen often referred,The boss,General,Who was the founder of Lotus cars?,Colin Chapman +Sports & Leisure,The white marks intersecting each five yard line are called ________.,Hash marks,Sports & Leisure,Hockey: The Calgary _______?,Flames,General,What Roman soldier defeated and destroyed Spartacus,Crassus - not Pompey +General,"What links Pacer Burton, Chad Gates, Walter Gulik",Elvis film characters,General,Which Top Hollywood Actor Was Responsible For Directing The Movie “ Stayin Alive ” The Sequel To Saturday Night Fever,Sylvester Stallone, Geography,What country is situated between Panama and Nicaragua?,Costa Rica +General,What is cl the chemical symbol for,Chlorine,General,What is the official language of austria,German,Food & Drink," Often eaten for breakfast, bacon is actually the flesh of what barnyard animal",Pig +General,Tinkerbelle ( A Chihuahua ) Is The Famous Pet Of Which Party Animal,Paris Hilton,General,In Norse mythology who was Odin's wife,Frigga,General,Collective nouns a bask of what creatures,Crocodiles +General,"Anatomically speaking, what is your hallux'",Big toe,General,What is the worlds tallest grass,Bamboo,General,"Treatment of disease & correction of deformity or defect by manual & operative procedures, with or without the use of drugs",Surgery +General,What animals are on the Australian coat of arms?,Emu and kangaroo,General,80% of household dust is actually what material,Dead skin,Science & Nature,What is the second largest bone in the foot?,Talus +General,Indian dish of of fried vegetables,Bhaji,General,What's the Swiss Family Robinson's destination when shipwrecked,America,Music,"Name The Band: Vince clark, Andy Bell",Erasure +General,On what was the declaration of independence written,Hemp paper,General,"Greek mythology monster with lions head, goats body and serpents tail",Chimera,General,Which literary traveller was accompanied by Passepartout,Phileas fogg +Science & Nature,As what is minus forty fahrenheit the same?,Minus forty celcius,General,Are barnacles plant life or animals,Animals,Music,James Todd Smith Is The Real Name Of Which Rap Artist,LL Cool J +Music,Telstar By The Tornadoes Is Said To Be The Favourite Song Of Which Former Prime Minister,Margaret Thatcher,General,"""Boy"", as in Tallboy and Lowboy, is derived from the French word for what",Wood, Geography,Which country administers Christmas Island?,Australia +General,Circuits can be wired in parallel or ______,Series,Music,Which 1984 musical was created by Tim Rice and the male members of ABBA?,Chess,Music,"""Beaucoup Fish"" In 1999 Was Underworlds 3rd Album Name One Of The Previous Two","""Dubnobasswithmyheadman"" & 2nd Toughest In The Infants" +Geography,What is built on the former home of the Earl of Shrewsbury? ,Alton Towers ,General,At the request of EMI who was painted out Sarge Peppers cover,Mahatma Gandhi,General,"On clothing, what does the symbol of a circle crossed-out indicate",Do not dry clean +General,What is a crime worse than a misdemeanor,Felony,General,Why is St. Paul's cathedral unique amongst English cathedrals,Only one with a dome,Entertainment,How old was Leann Rhimes when she became a country music star?,Fourteen +Music,What Was Robbie Williams First Solo UK Number One,Millenium,General,"In the movie ""One Crazy Summer"" what was the movie that was being made on the island called?",Foam 2,General,What colour is iridium,Steel Grey +General,Which tennis player defected to the west in 1975,Martina navratilova,General,What country is the world leader in Cobalt Mining,Zaire,General,What did Beethoven do before composing,Put head in cold water +General,Who wrote 'la traviata',Guiseppe verdi,General,"Who directed the film, fame",Alan parker,General,What is the main ingredient of tahini used in the Middle East,Sesame seed (paste) +General,Buffalo Bill's real name was___.,William f cody,General,Capital cities: Iceland,Reykjavik,General,In Australian slang what is underground Mutton,Cooked Rabbit +General,Distinguished Information Cross is whose highest bravery award,The CIA,General,A Dorset shop sells bookends made from 140 mill year old what,Fossil Dinosaur Shit,General,What's the cause of surface ocean currents,Winds +General,Russian triangular musical instrument,Balalaika, History & Holidays,What was the name of the host of Double Dare? ,Mark Summers ,General,"In Which Book Of The Bible Will You Find The Quote ""The Love Of Money Is The Root Of All Evil""",Timothy +Geography,"Filled with water, gas, electric, telephone, cable, steam, and sewer lines, _____________ is the most dense underground site in the United States.",Manhattan,General,Herbert Charles Angelo Kuchacevich ze Schluderpacheri - who,Herbert Lom,General,"Field of science concerned with the origin of the planet earth, its history, its shape, the materials forming it, & the processes that are acting & have acted on it",Geology +Science & Nature," Strange creatures, jellyfish are comprised mostly of water _ more than 95 percent _ and have no brain, heart, or bones, and no actual __________",Eyes,General,"In 1984 Los Angeles hosted the Summer Games, which city hosted the Winter Games that year",Sarajevo,General,Name the first Alsatian dog film star 1921,Strongheart - Rin Tin Tin 1922 +Geography,In which country is Normandy,France,General,What is another name for the card game 'twenty one',Blackjack,General,The Battle of Inkerman took place in which war,Crimean +Geography,"What country was the setting for ""Doctor Zhivago""",Russia,General,Alfred Nobel invented dynamite what did father Immanuel invent,Plywood,General,How long does a game of ice hockey last,60 minutes +Sports & Leisure,In What Year did Steve Redgrave Win His First Gold Medal At The Olympics? ,1984 ,General,After who was Mickey Mouse named,Mickey Rooney,Entertainment,Orson Wells was nominated for four oscars for which legendary movie?,Citizen Kane +Music,Which Band Named Themselves After A Milk Drink Featured In A Clockwork Orange,Moloko,Entertainment,For which cartoon character was Beethoven a favourite composer?,Shroeder,Science & Nature,Meningitis affects the _________.,Brain + Geography,In which state is Mount St. Helens?,Washington,Music,What is Morrisseys Christian name?,Stephen,General,What is a resident of manchester,Mancunian +General,What is the standard time for boiling a soft boiled egg,3 minutes,General,In North Carolina its against the law for who / what to fight,Cats and Dogs,General,What is the act of founding settlements abroad,Colonisation +General,Where in the south pacific did the u.s do nuclear bomb tests in 1946,Bikini,Science & Nature," The sound a __________ makes is called ""nuzzing"".",Camel,General,What Game Was Invented By William G Morgan?,Volleyball +Science & Nature," Mussels can thrive in __________ because of an inborn ability to purify bacteria, fungi, and viruses.",Polluted water,Science & Nature,What Is Nyctophobia The Fear Of ,The Dark ,General,What is the atomic weight of arsenic,75 +General,What is a young otter called,Whelp,Sports & Leisure,"At The 1976 Olympics, Only One Competitor Was Excused The Compulsory Sex Test. Who Was That Competitor? ",Princess Anne ,Music,"Who Recorded The Albums ""Maon On The Line"", ""Crusader"" & Flying Colours",Chris De Burgh +General,Who was the last New Zealander to win the Olympic 1500 metres track race,John walker, History & Holidays,What was the first ship to reach the Titanic after it sank?,Carpathia,General,Which European city was the bride of the sea,Venice + History & Holidays,"Who was, ""First in war, first in peace and first in the hearts of his countrymen""",George washington,Science & Nature," __________ can swim for a 1/2 mile without resting, and they can tread water for 3 days straight.",Rats,General,Of which country is the flame flower a native,South africa +General,What invention was nicknamed the Noisy Serpent in 1902,Vacuum Cleaner,Entertainment,Secret Identities: Boston Brand,Deadman,General,What is the fear of poison known as,Toxiphobia +Science & Nature,What does the lack of iodine in the diet cause?,Goitre,General,Which actress was jailed in 1982 for tax evasion,Sophia Loren,General,Which principal conductor of both the New York Philharmonic Orchestra and the Halle Orchestra died in 1970,Sir john barbirolli +General,Bahina de los Cochinos is better known as what,Bay of Pigs,Food & Drink,What Foodstuff Is The Most Advertised In The UK ,Breakfast Cereals ,General,The average Britain in their life consumes 18 lb of what,Dirt bad washed food +General,What were the names of the Saturday morning cartoon critters who lived in air-vents and befriended a boy who kept their secret hidden?,The Littles,General,Where does queen elizabeth live when she's in london,Buckingham palace,General,What job would regularly use kerfs,Carpenter - first cut to guide saw +General,What is a sound below 20 cycles a second called,A Woof,Geography,In which country is brest (not breast) ,France ,General,What is the largest lake that is entirely within Canada,Great bear lake +General,Where were the 1960 summer olympics held,Rome,General,What is growing plants in liquids rather than soil,Hydroponics,Music,"Who Had A Hit In 1986 With ""In The Army Now""",Status Quo +General,On whose death did Shelley write his elegy Adonais,Keats,General,What sort of lines never meet,Parallel,General,In Dick Dastardly's Vulture squadron name the mechanic,Clunk he did not talk but whistled +General,What food Scots once refuse to eat cos it was not in the Bible,Potato,General,Bunc was the first name for what product,Coffee,Sports & Leisure,Which Golfer Is Nicknamed El Ninio ,Sergio Garcia  +Science & Nature,Which Is The Brighest Star Visible From Earth? ,The Dog Star ,General,"There is a word in the english language with only one vowel, which occurs six times. What is it",Indivisibility,General,To which bird family does the canary belong,Finch +General,"What are fallows, lutinos and opalines",Budgerigars,General,What is a baby oyster,Spat,General,What does the word Grizzly mean - as in Grizzly bear,Grey - but they are yellow brown +General,What did Marie Antoinette and Jayne Mansfield have in common,Bust size,General,What nationality was actress Greta Garbo,Swedish,Sports & Leisure,Which brothers represented England in the 1995 rugby union World Cup? ,Rory and Tony Underwood  +General,Which Scent Was Advertised In Posters Featuring The Naked Sophie Dahl?,Opium,General,What do the letters in the name of the technology company EE stand for?,Everything Everywhere,General,"The basis of all scientific agriculture, what involves six essential practices: proper tillage; maintenance of a proper supply of organic matter in the soil; maintenance of a proper nutrient supply; control of soil pollution; maintenance of the correct soil acidity; & control of erosion",Soil management +Food & Drink,What Is The Main Ingredient Of The Spanish Dish Paella? ,Aniseed ,General,The locals call it Kaapstad what do we call it,Capetown,Music,How Many Movements Traditionally Make Up A Concerto,3 +General,What was Alfred Hitchcock's first sound film,Blackmail,Geography,What is the most air polluted city in the United States?,Los Angeles,General,The man known as the Red baron was famous in which field,Aviation +General,La Picardy is a region of which European country,France,General,With which group is Keith Flint the lead singer,Prodigy,General,Which term describes the divisions of a lobster or crab's gills?,Dead Man's Fingers +General,This is the main food of the blue whale,Plankton,Food & Drink,Which two stouts are brewed in cork? ,Murphy's and Beamish ,General,In which city was Gianni Versace shot dead,Miami +General,Edith Knight wrote which classic story (later filmed),Lassie come home,General,Who was king of macedonia from 336 to 323 b.c,Alexander the great,Music,What Chauvinistic Comment Was A Hit For The Monks,Nice Legs Shame About The Face + History & Holidays,Who was the last of the apache warrior chiefs ,Geronimo , History & Holidays,In What Year Did The American Civil War End ,1865 ,Science & Nature," When a __________ exerts itself, gets angry, or stays out of the water for too long, it exudes red, sweatlike mucus through its skin.",Hippopotamus +General,Who was tommy lee jones' freshman roommate at harvard,Al gore,General,"Barrel, breech and stock are all parts of a what",Gun,General,Phobophobia is the fear of,One's own fears +General,"Who Directed The 1973 Film ""American Graffiti""",George Lucas, Geography,What is the capital of Vietnam ?,Hanoi, History & Holidays,"At Christmas, it is customary to exchange kisses beneath a sprig of which plant? ",Missletoe  +General,What weapon is known as the gun that won the west,Winchester rifle,General,"Which science fiction writer is credited with ""inventing"" communication satellites",Arthur c clarke,General,What is the longest river in the u.s,Mississippi +Food & Drink,"Cocktails: Vodka, orange juice and Galliano make a(n) ___________.",Harvey Wallbanger,General,International Aircraft Registration what country is SU,Egypt,Geography,Which is the Earth's second smallest continent,Europe +General,What's bottled in jeroboams,Champagne,General,In 1956 16 tons topped the UK charts who was the singer,Tennessee Ernie Ford, Geography,What is the capital of Gabon ?,Libreville +Science & Nature, __________ eat only moving prey.,Toads, Geography,What is the capital of Haiti ?,Port-au-Prince,General,How is the cucurbita pepo better known,Marrow +General,Brian Connolly was the lead singer of which 70s group,The Sweet,Science & Nature,This membrane controls the amount of light entering the eye.,Iris,Music,"Which Country Singer Had A Song Named After Him On Prefab Sprouts 1985 Album ""Steve Mcqueen""",Faron Young +Sports & Leisure,Which Grade Follows A Yellow Belt In Judo ,Orange ,Geography,Where can you find the first iron bridge ever built? ,"Ironbridge, near Telford in Shropshire, England ",General,"The Good Friday agreement, signed on Good Friday 1998, is also known as what ",The Belfast agreement  +General,This normally has 4 legs & your butt is parked in it right now,Chair,Science & Nature, A pregnant goldfish is called a __________,Twit,General,What university would you find in the city of Palo Alto in western California,Stanford +General,What is a pinto a type of,Piebald horse,General,What do Mcdonalds & Burger King do to their fries so they will turn golden brown,They sugar coat them,Science & Nature,Who Was Dubbed 'Queen Of The Air' By The British Press In The 1930's After Her Solo Flight From England To Australia ,Amy Johnson  +General,If you landed at Hanedi airport where are you,Tokyo,General,A soft smooth cheese similar to yoghurt is known as,Fromage frais,General,In which ship did Captain Scott sail to the Antarctic on his ill-fated expedition of 1910 to 1912,Terra nova +General,Where can you drive your car on the Nippon Clip On,Auckland Harbour Bridge,General,In which country would you find the River Meander,Turkey,Sports & Leisure,With which sport would you have associated Jocky Wilson ,Darts  +General,How Long's The Course Is An Autobiography Written By Which Athlete,Roger Black,General,Where is cape hatteras,North carolina,General,Fried fish lettuce spinach is a traditional Xmas eve meal where,Armenia +General,Disney question - who are Daisy Ducks three nieces,April May June, History & Holidays,Which country donates the Christmas tree in Trafalgar Square ,Norway ,General,What is the biggest bay on the north American continent,Hudson bay +General,"Mayfair, london is a district of little streets near ______",Hyde park,Music,Name The 3 Middle Of The Road Hits From 1971 Which All Had Repeated Words In Their Titles,"Chirpy Chirpy Cheep Cheep, Tweedle Dee Tweedle Dum, Soley Soley",General,What is an unfledged pigeon called,Squab +General,Who flew the rocket-powered bell x-ia at a record mach 2.5,Chuck yaeger,General,Who was Tasmania's famous swashbuckler,Errol flynn, History & Holidays,Who popularized the Christmas tree in Britain and the U.S.? ,Queen Victoria  +General,In medicine what is procaine used for,Anaesthetic,Music,What group did Steve Howe join in 1982?,Asia,General,What is the Capital of: Gabon,Libreville +General,Name Bilbo Baggins mother,Belladonna Took,Music,Which Band Had A 1985 Number One Hit With The Single “I Wanna Know What Love Is”?,Foreigner,General,The Tv Show Mork & Mindy Was Actually A Spin Off From Which Otther US Sitcom,Happy Days +General,These teeth grow especially long in vampires.,Incisors,General,Name the Hong Kong stock exchange,Hang Seng,General,With which musical instrument is Julian Bream chiefly associated,Guitar + Geography,Santo Domingo is the capital of ______?,Dominican Republic,Science & Nature,What Value Is PI To 6 Significant Figures ,3.141592 ,General,Which Eastender Provided The Voice Of Dipsy In The Teletubbies,Rudolph Walker (Patrick Trueman) +General,What is a doucet,A Stags Testicle,Music,Mick Hucknall Is The Lead Singer With Which Group,Simply Red,General,Who wrote the novel ' Anna of the Five Towns',Arnold bennett +General,Who left his five day tenure as West Point's superintendent to lead the Confederate army,Tavern,General,What is the young of this animal called: Hawk,Eyas,General,Which Creature Lays The Largest Eggs,A Shark +General,"Who said ""In future, everyone will be famous for 15 minutes""",Andy warhol,General,Caroline of Ansbach was the wife of which British King?,George II,General,Whose alphabet was called the Futhark,Vikings +Geography,What is the capital of Haiti,Port_au_prince,Food & Drink,From Which Fruit Is The Liqueur Kirsch ,Cherries ,Science & Nature, A Holstein cow's spots are like a __________ or a snowflake; no two cows have exactly the same pattern of spots.,Fingerprint +Music,"Which Birmingham Band Previosly Known As Earth Made Their Single Debut With ""Evil Woman Don't Play Your Games With Me""",Black Sabbath,Music,Which actor Was The Narrator On Michael Jacksons Thriller,Vincent Price,Science & Nature,Name The SI Unit Of Pressure ,Pascal  +General,Beaufort - the wind scale man - had what job,Sailor (Admiral),General,Which Australian city is known as 'The City of Light',Adelaide,General,In which state would you find the Jack Daniels distillery,Tennessee +General,What are the ridges on corduroy called,Wales,General,What kind of steak is named after a French writer and statesman,Chateaubriand,General,Name the hummingbird in Disney's Pocahontas,Flit +General,Last Words I am Dying with the help of too many Physicians,Alexander the Great,Music,"Which football team Had A Top 5 Record With ""Ossies Dream""",Tottenham Hotspur,Food & Drink,What Is The Country Of Origin Of Red Stripe Lager? ,Jamaica  + History & Holidays,Oklahoma statehood in 1907 became a sure thing. in part due to the discovery of what ,Oil ,General,"Which Actor Played The Role Of ""Danny Zuco"" In the Musical Grease When It Opened On The London Stage In 1973",Richard Gere,General,Who wrote A Town Like Alice,Nevil Shute +Science & Nature,Who Invented The Gramophone ,Thomas Edison ,General,If a Ghanian says Afishapa what have you been told,Merry Christmas,General,These establishments don't have windows or clocks?,Casinos +General,Which famous person invented the cat flap,Isaac Newton,General,Baseball: the kansas city ______,Royals,General,"What kind of creature is a redpoll, other than a kind of cattle",Bird +General,"The last line of which document is 'working men of all countries, unite'",Communist manifesto,General,"In Greek mythology, who did ariadne help to escape the labyrinth",Theseus,General,The first nude Playboy centerfold was,Marilyn monroe +General,"Hurricane, typhoon and tornado are all types of what",Fighter aircraft,General,Someone abstaining from sexual relations,Celibate,General,What famous bird first appeared on tv september 1957,Nbc peacock +General,What did alexander graham bell invent,Telephone,Science & Nature,"What are romney marsh, suffolk and swaledale ",Sheep ,General,"Who was in the charts in 1992 with ""Be My Baby'",Vanessa paradis +Music,"Who Recorded The Classic 1987 Song ""If I Was Your Girlfriend""",Prince,General,To which group of islands does Rhodes belong,Dodecanese,Science & Nature,The Tenrec Is A Small Insectivore That Is Only Found On Which Large Island ,Madagascar  +General,In what does a rhinologist specialise,Human nose,Music,Which Jackson Left To Turn The Jackson 5 Into The Jacksons,Jermaine,General,What are the snaffle Pelham and Weymouth,Horse bits +General,Columbus is the capital of ______,Ohio,General,"Although it doesn't sound like a dog, ____dust is ornamental wood chips often placed in flowerbeds.",Bark, History & Holidays,Who proclaimed thanksgiving a national holiday in 1863 ,Abraham lincoln  +General,What nation has had a monarchy the longest,Japan,General,Hundred and nine whose official neutral name is 'the helvetic confederation',Switzerland,General,What is a cancer causing substance,Carcinogen +General,Who played the lead role in the first Tarzan movie,Elmo lincoln,General,What is the first day of lent called,Ash wednesday,General,What do we call the noise made by a sudden spasm closing the windpipe,Hiccup +General,First public supply in Britain from river Wey in 1881 what,Electricity,General,"Lake Tana, the source of the Blue Nile, lies in which country",Ethiopia,General,What are the deepest parts of the oceans,Ocean trenches + History & Holidays,What Colour Are The Berries Of The Mistletoe Plant ,White ,General,What is the correct name of bangkok,Krung thep,Music,Which Group Has An Exclusive Range Of Clothing Available Through The K-9 Club,East 17 +General,Shepherd's pie is meat hash covered with a layer of this (2 wds),Mashed potatoes,General,Toheroa Soup' is a traditional dish from which country,New zealand,General,Which animal floats in water,Porcupine +General,In which city were the 1928 Summer Olympic Games held,Amsterdam,General,What is the science that deals with the motion of projectiles,Ballistics,General,Israel Beer Josaphat founded what famous service in 1851,Reuters +Food & Drink,Which Bewer Produces Owd Roger ,Marstons ,General,"In Italy, as what is mickey mouse known",Topolino,General,What year did the 'The Bay of Pigs' take place,1961 +General,"Who was the first cricketer to score 10,000 Test match runs",Sunil gavaskar,Food & Drink,"Whiskey, hot coffee, and whipped cream make up which type of drink ",Irish Coffee ,Geography,In what island group is Corregidor,Philippine +General,What country boasts the Chrysanthemum Dynasty,Japan,General,What hobby was developed by the palmer paint company,Painting by numbers,General,Where are gettysburg and the liberty bell,Pennsylvania +General,A doromaniac had a compulsion to do what,Give gifts,Music,"""I Love My Radio (My Dee Jays Radio) Was A One Hit Wonder For Which Female Singer",Taffy,General,What U.S. state includes the telephone area code 509,Washington +Sports & Leisure,How many feet high is a basketball net?,Ten,General,In 1959 Able Baker first put out in space by USA what were A/B,Spider Monkeys,General,What type of food is tortellini,Pasta +Geography,What's the longest river in the americas ,The amazon ,General,"In the Breakfast Club, What did Brian try to kill himself with?",A flare gun,Art & Literature,In Which Book Would You Find The Characters Desinov & Dolokhov ,Tolstoy's War & Peace  +Geography,______________ has a sand desert with dunes over 100 feet high. It is located along the flatland of the Kobuk River in the northwestern part of the state.,Alaska,General,What is the main ingredient of a Maron glace,Chestnuts,General,Homonyms: To burn or char/a prophet or diviner. (answer in the form of word1/word2),Sear/Seer +General,Put the worlds most common forename and surname together,Mohammed Chen,General,"What guitar company created the ""flying v"" guitar in the late 1950's",Gibson,Science & Nature,To which family does the coffee plant belong?,Madder +Music,Who Had A Hit With “Run To The Hills” And “The Evil Men Do”?,Iron Maiden,General,"What game challenges you to ""double in"" & ""double out""",Darts,Science & Nature, The hummingbird is the only bird that can __________,Fly backwards +Food & Drink,In Regard To Food What Do The Initials 'GM'' Actually Stand For ,Genetically Modified ,Music,"Who Had A Massive Hit In 1993 With ""Im Easy / Be Aggressive""",Faith No More,Science & Nature,What is the name of sugar found in fruit ,Fructose  + History & Holidays,The fear of halloween is known by which other name (Fourteen letters) ,Samhainophobia ,Music,Name The Lead Singer Of Dire Straits,Mark Knopfler,Science & Nature, The __________ eats nothing but eucalyptus leaves.,Koala +General,In which John le Carre novel does George Smiley first appear,Call for the dead,Science & Nature," Male cockatoos can be taught to speak, but females can only chirp and __________",Sing,Food & Drink,Which American Chef Wrote 'Kitchen Confidential' ,Anthony Bourdain  +General,How many days were the american hostages held in iran,444,General,Who would use a plessor,Doctor reflex hammer,General,Who sang Shattered Dreams in 1987,Johnny Hates Jazz +General,What kind of peach has a smooth skin,Nectarine,General,In Greek mythology who did athena turn into a spider,Arachne,General,It's not over till the _____ sings,Fat lady +General,Which novelist created pathologist Kay Scarpetta,Patricia cornwell,Food & Drink,What Ingredient Is Added To Gin To Make It Pink ,Angostura Bitters ,General,What is the fear of returning home known as,Nostophobia +General,"What's the international radio code word for the letter ""J""",Juliet,General,"Which archangel is the patron of television and radio workers, messengers and postal workers",Gabriel,Science & Nature,Which Piece of science history was created at the Roslin Institute in Scotland on 5th July 1996? ,Dolly The Sheep Was Created  +Science & Nature," The three_toed __________ of tropical America can swim easily, but it can only drag itself across bare ground.",Sloth,General,18% of USA coins 7% of notes have what on them,Dangerous Bacteria,General,An organisation of business and professional men was founded in Chicago in 1905 out of a weekly luncheon club. What is it called,Rotary + History & Holidays,Who Was The Mother Of James 1st Of England? ,Mary Queen Of Scots ,General,"What islands get their name from the Spanish word ""cayo"", meaning rock or islet",Florida keys,Music,What song by Frankie Avalon went to #1 in 1959?,Venus +Sports & Leisure,Whose record did Brian Lara beat when he scored 400 not out in a test match in April 2004? ,Matthew Hayden's ,Science & Nature,What is a word for a castrated ram?,Wether, History & Holidays,"According to 'A Christmas Song'', what would you find roasting over an open fire ",Chestnuts  +General,"In Greek mythology, which goddess was Apollo's sister",Arte'mis,General,What country was formerly called Ceylon,Sri lanka,Music,Who Were The Only Band To Have 2 Records Named By Melody Maker As Album Of The Year During Th 80's,The Cure +General,What was Napoleons mothers name,Laticia,Geography,In which English county is Dartmoor? ,Devon ,Food & Drink,What was the name of the highly diluted rum that was once given to British sailors? ,Grog  + History & Holidays,Who ruled rome when Christ was born?,Augustus Caesar,General,What does 'faux pas' mean,Mistake,General,What instrument measures walking distance?,Pedometer +General,In Braille which letter uses the least number of raised dots,S,General,Who took out a $5000 life insurance before dying in battle,George Armstrong Custer,General,Evelyn Glennie is the world's first full time soloist on which part of the orchestra,Drums +General,Who was the egyptian god of the dead,Anubis,General,"This quiz show, emceed by allen ludden, featured a 60 second ""lightning",Password,General,Uncle what is the name of the cord joining a mother and her unborn child,Umbilical +General,What is the unit of currency in Bulgaria?,Lev,General,In Greek myth who was the first woman,Pandora,Sports & Leisure,In What Year was (come on you reds) a no.1 single for Man Utd ,1994  +General,The average human has seven what each day,Sex Fantasies,General,Juliet Gordon Low founded what in Savannah Georgia 1912,Girl Guides,Science & Nature,What is considered to be the least nutritious fruit or vegetable ,Cucumber  +General,What physical disability is also known as nanism,The condition of being a dwarf,Entertainment,"Name the band - songs include ""Sex & Drugs & Rock & Roll, I Want To Be Straight""?",Ian Drury and The Blockheads,General,Relating to food of what is 'lollo biondo' a variety,Lettuce +General,Who was albert einstein's father,Hermann einstein,General,What is consumption,Tuberculosis,General,What one metallic element could critically harm superman,Kryptonite +General,In 982 eric the red discovered _____,Greenland,General,By law In Washington State a concealed weapon must be what,Under 6 foot long –,Entertainment,Who invented James Bond ?,Ian Fleming +General,What is the closest relative of the manatee,Elephant,General,Santa Clause works in USA but who delivers gifts in Syria,A Wise mans Camel,General,Which pop duo had a hit in 1983 with 'Club Tropicana',Wham +Science & Nature,"He wrote ""Sexual Behaviour in the Human Male"" in 1948.",Kinsey,General,"Which actor/singer wrote the autobiography 'Parcel Arrived Safely, Tied With String'",Michael crawford,General,This is the lowest ranking suit in bridge,Clubs +General,Dik Browne is the author of which cartoon strip,Hagar the Horrible,General,In 1995 what were the men of Pilsen asked to donate in exchange for half a litre of the city's best know brew,Blood,General,What shape is Millerighi pasta,Tubes +Art & Literature,"In what field of study would you find ""flying buttresses""?",Architecture,General,"During WW I, what day of the week was the recommended meatless day",Tuesday,Science & Nature," The largest order of mammals, with about 1,700 species, is __________. Bats are second with about 950 species.",Rodents +General,In English superstition what bird should you wish good day,Magpie else bad luck,Religion & Mythology,This Greek mountain was known as the home of the gods.,Olympus,Science & Nature,What Is The Study Of Earthquakes Called ,Seismology  +General,What is a group of hippopotamuses,Bloat,General,What is a group of this animal called: Turkey,Rafter,General,What is the national game of the basques,Pelota +General,"In music, what does the term legato mean",One note leading smoothly to the next,Entertainment,From which station does the 'Chattanooga Choo Choo' leave?,Pennsylvania station,General,Who was he last Tudor monarch of England,Elizabeth I +General,"What is the common term for a ""somnambulist""?",Sleepwalker,General,What is the flower that stands for: concealed merit,Coriander,General,A baby oyster is called a(n) ____,Spat +Food & Drink,Which sweets were reintroduced to Britain in1994 after a 10-year gap? ,Spangles ,General,In 1908 A'Ecu d'Or became the worlds first what,Pornographic film,General,Cucullus' is the latin word for what part of a monk's robe or a woman's sweater,Cowl +General,"Which jazz cornettist composed and recorded ""Davenport Blues"" in 1925",B1x beiderbecke,General,Ben Hur won most Oscars 11 what film comes second with 10,West Side Story,General,In which month of the year is is the Lord Mayor's Show in London traditionally held?,November +General,The Moluccas are better known as where,Spice Islands,General,What are the only two commonly ingested items pure enough to be absorbed into the bloodstream directly through the stomach walls,Honey and alcohol,General,The New Testament originally written in what language,Greek +General,In New Jersey it is illegal to frown at who,Police Officers,General,An ahuehuete is a,Tree,General,What Globally Successful Product What Invented By Dr John Pemberton,Coca Cola +Science & Nature,What Were The Four Elements Proposed By The Ancient Greeks ,"Earth, Water, Fire & Air ",Music,On Which Label Was The Human Leagues Greatest Hits Released In 1988,Virgin,General,"Who said ""For every action there is an equal and opposite reaction""?",Albert Eienstein +Toys & Games,Personal merry_go_round guaranteed to make you dizzy.,Sit n spin,General,What was Alka-Seltzer first marketed as,Cold Cure,Music,"On their first visit to the US, at which hotel in New York City did the Beatles stay?",The Plaza Hotel +General,Zen is the form of Buddhism in which country,Japan,General,"Scalene, isosceles and equilateral are all types of what",Triangles,General,If the skin under someone's finger nails turned blue this would be the first symptom of what,Cyanide poisoning +General,What character in the Jungle Books name means frog,Mougli,General,Nevada means what when translated from Indian,Snow,General,On average it takes 1.5 hours to do what,Fully cremate a corpse +General,"Who was the inventor of the ""stop sign""",William phelps eno,General,What was French frigate Isere's most famous cargo,Statue of Liberty,Entertainment,"What was Dorothy's last name in ""The Wizard of Oz""?",Gale +General,Pope John Paul II played for the Polish national team what sport,Rugby,General,Dendrochronology is better known as __________.,Ring dating,General,Who was the future political pair to star in Hellcats of the Navy?,Ronald Reagan and Nancy Davis +Geography,What American state is 'The Golden State'?,California,General,Which singer famously said 'You aint heard nothing yet',Al jolson,General,What country has the worst roads averaging 10 deaths per mile,Portugal +General,Capability Brown was a famous Landscape Gardener 1st name,Lancelot,Sports & Leisure,Which two London clubs did Bobby Moore play for? ,West Ham & Fulham , Geography,Which island country lies immediately to the West of Mauritius?,Réunion +Entertainment,What was the name of George of the Jungle's pet elephant?,Shep,General,In money circles what does ERM mean,Exchange rate mechanism,General,"Who was assassinated on december 8, 1980 in new york city",John lennon +General,The George Cross was awarded to the entire population of which island,Malta,General,What city stands on the Hooghly river,Calcutta,General,What Northeastern European country's capital is Tallinn,Estonia +General,What is the name of the source of the Nile,Ripon falls,General,What is the only borough of new york city that is not on an island,Bronx,General,Nyctophobia is the fear of,The dark night +General,Which city was the first with one million inhabitants,London,General,Who makes Pringles,Proctor and Gamble,General,Where is the natchez trail,Mississippi +General,What company used to be called The Haloid Company,Xerox,General,What is a vein or fissure in a rock containing mineral deposits called,Lode,General,"In Scandinavian mythology, which was the 'Tree of Life'",Ash +General,What did plato found in 387 bc,The academy,Music,"Which Band Featured Among It's Members Joe Sample , Wayne Henderson, & Wilton Felder",The Crusaders,Music,Only You Was A Hit Was For Whome,Yazoo/ The Flying Pickets +General,What are dense seawater swamps along coasts of hot countries,Mangroves,General,Ocean is NOT recognised International Hydrographic Bureau,Antarctic Ocean,General,What is the Capital of: Cote d'Ivoire,Yamoussoukro +Entertainment,What kind of dog is Scooby Doo,Great dane,General,What country has the worlds highest golf course,Peru,General,Who was the hero of the old TV cop series Dragnet,Sergeant Joe Friday +Music,3 Singers Have Sung The Opening Line The Christmas Favourite Do They Know Its Christmas (PFE),Paul Young,General,What country celebrates its National Day on 17th June?,Iceland,General,In literature who married Mary Morstan,Dr John Watson +Art & Literature,"Who wrote the novel ""The Strange Case of Dr. Jekyll and Mr Hyde"" ?",Stevenson,General,You ordered nem in Vietnamese restaurant what would you get,Spring Roll,General,What famous battle was fought on 4th June 1942?,The Battle of Midway +General,David Stirling was the founder of which organisation,S a s,General,What Was The First Country Ever To Issue A Bank Note,Sweden,General,"Who said ""let them eat cake""",Marie antoinette +General,San jose is the capital of ______,Costa rica,Science & Nature," Sue, the world's largest, most complete, and best preserved __________, made her grand debut to the public on May 17, 2000 at the Field Museum in Chicago, Illinois.",Tyrannosaurus rex,General,Scolionophobia is the fear of,School +Entertainment,"Name the band - songs include ""Baby I Don't Care, I Want Your Love""?",Transvision Vamp,Geography,Which Country Has The longest Individual Railway ,Canada ,General,An Arab/Israeli band Abu Hafla - record called Humping meaning,Enjoyable Gathering +General,Six verified copies of his signature survive - who is he,William Shakespeare,General,"Who wrote the sonnet, ""Much have I travell'd in the realms of gold""",John,General,"In area, which is the largest country in Africa",Sudan +Geography,Name the capital of Italy.,Rome,General,What actor played the title role in monsignor,Christopher reeve,General,Parsley is a member of which family,Carrot +General,Corduroy literally translated means what,Cloth of the King, Geography,What is the basic unit of currency for Guatemala ?,Quetzal,Science & Nature,Where Do Yaks Live? ,Tibet  +General,Of what metal are meteorites composed,Iron,Sports & Leisure,Over How Many Laps Is The Indianapolis 500 Contested ,200 ,Music,"Whose 1976 debut hit was ""You To Me Are Everything""?",The Real Thing +Sports & Leisure,With which sport is Jack Nicklaus associated ?,Golf,Geography,What is the capital of Belize,Belmopan,General,Which constellation is the water-bearer,Aquarius +General,"Who used the statement: ""my fellow americans""",Lyndon johnson,General,"In which sport would you find, in a team of ten, a cover point, a goalkeeper, and a third home",Lacrosse,General,Dr. Hermann Rorschach is credited with developing what kind of tests,Inkblot +Food & Drink,How many teaspoons in a tablespoon (UK) ? ,3 ,General,Which shellfish is sometimes called a Queenie,Scallop,Sports & Leisure,"What sport do the following terms belong to - ""Behind & Banana Kick""?",Australian Football League +Music,Gordon Matthew Sumner Is The Real first Name Of Which Singer,Sting,General,What gives onions their distinctive smell,Sulphur - taken in when growing,General,What translates high-level languages into machine language,A compiler +General,Gus Grissom Was The First Man In History To Do What Twice,Go Into Space,General,What U.S. state's marijuana fields were busted most often in 1987,Hawaii,General,What are fungal remains in coal,Sclerotinite +Sports & Leisure,What game might you use a (Flick Serve)? ,Badminton ,General,"What can come in types Blue, Spear, Couch and Arrow",Grass,General,Persephone was the greek goddess of ______,Spring +General,Which of the twelve largest islands in the World lies directly between Borneo and New Guinea,Celebes,General,What would you do with a hispano-suiza,Drive it,Music,"What Was On The Other Side Of Boney M's Double A Side ""Brown Girl In The Rain""?",Rivers Of Babylon +General,If you had distrix what condition would you have,Hair - split ends, History & Holidays,What were 'Little Boy' and 'Fat Man'?,Atom Bombs,Science & Nature,What Is The Significance Of The Megalosaurus ,It Was The First Recognised Dinosaur To Be Named  +General,What was the first James Bond book,Casino Royal,General,The only river that flows both north and south of the equator is the?,Congo,General,Waves 'break' when their height is how much more than the depth of the water,Seven tenths +General,"In the film 'dragonheart', who did the voice of the dragon",Sean connery,General,What kind of music is Struss's 'Die Fledermaus',Operetta,General,What South American country produces the most coffee,Brazil +General,In 19th century Florence it was illegal for women to wear what,Buttons,General,With which natural phenomena are Baily's Beads associated,Solar eclipse,General,What other relative of Travolta's made a cameo in Saturday Night Fever,His sister +General,Joseph Levitch became famous as who,Jerry Lewis,General,What is a one-party system of government in which control is maintained by force and regimentation,Fascism,General,What do Christians celebrate on Easter Sunday? ,The resurrection of Jesus Christ  +General,Who was Prime Minister at the start of World War One,Asquith,General,What is the flower that stands for: bury me amid nature's beauties,Persimmon,General,"Who, in 1655, discovered Saturn's rings",Christiaan huygens +General,In which literary work does Mrs Do-As-You-Would-Be-Done-By appear,Water babies,General,What is the birthstone for February?,Amethyst,General,In India in 1994 who were finally allowed to vote,Eunuchs +General,"The word 'boondocks' comes from the tagalog (filipino) word 'bundok,' which means",Mountain,Music,"""Words"" Was A No.1 Hit In 1996 For Whom",Boyzone,General,These are the two highest valued letters in scrabble,Q and z + Geography,What is the basic unit of currency for Vanuatu ?,Vatu,Geography,In which city is wembley stadium ,London ,General,Dallol Ethiopia has what claim to fame,Worlds hottest average place 94 +General,Which best-selling author writes novels featuring the pathologist Dr. Kay Scarpetta,Patricia cornwell,Food & Drink,How many bottles of champagne are in a magnum? ,Two ,Music,What Was Stevie Wonders First Album On Gaining Artistic Control Of His Own Output In 1972,Music On My Mind +Music,In The Beatles Song “ Ob Lah Di Ob Lah Dah “What Did Desmond Say To Molly,Girl I Like Your Face,General,What companies cars are nicknamed mopars,Chrysler,Music,Who Was The First Black Singer To Host His Own TV Programme,Nat King Cole +Tech & Video Games,Circuits can be wired in parallel or ______?,Series, Geography,What is the basic unit of currency for Armenia ?,Dram,General,A biographical film,Biopic +General,What do butchers call the edible internal part of an animal,Offal,General,The scientific word 'quark' was first used in which novel,Finnigans wake,General,Ignoring obvious what links Venus and Mercury,No Moons +General,A Hop Low is the world smallest - what,Mushroom, History & Holidays,Which Surrey cricketer made history at Old Trafford in 1956 when he took 19 wickets in the test match against Australia ,Jim Laker ,General,On what street did dennis the menace live,Elm street + History & Holidays,Who Moved Into His Factory On New Yorks East 47th Street In 1963 ,Andy Warhol ,General,Wolfram is the alternative name for which element,Tungsten,Entertainment,This film was an ambitious concert sequence of cartoons by Walt Disney.,Fantasia + Geography,On what river is the capital city of Canada?,Ottawa,General,"What government issue item did sensitive G I's dub ""sandpaper""",Toilet paper,General,Who did dick van dyke play on the 'dick van dyke show',Rob petrie +General,What was Martin Frobisher looking for,North west passage,General,What is generally thought to be the oldest breed of dog,Chow-Chow,General,"What yellow, fossilized resin did the Greeks and Romans use in jewelry",Amber +General,What happens if you get pepper in your proboscis,YoU.S.neeze sneeze,General,Licking what uses one tenth of a calorie,Stamp,General,If a doctor says you have ecchymosis what have you got,A Bruise +General,What kind of person would have had a twat on,A Nun part of habit,General,Who was the first black mayor of chicago,Harold washington,General,Who was 'hooked on a feeling',Blue suede +General,What is the correct name for the Union Jack Flag?,The Union Flag,General,Ra is the chemical symbol for which element,Radium,General,"Which spirit is the characteristic ingredient of ""Planter's Punch""",Rum +General,What was the first Olympic sport to include women,Tennis - Paris 1900,General,Acrotomphillia is having sex with who or what,Amputees,Music,Where was Tony Christie asking directions to in 1971,Amarillo (Is this the way to Amarillo) +General,Which Beatles song is associated with the Manson family,Helter Skelter,Geography,Which Instrument Measures Atmospheric Pressure ,A Barometer ,General,What does the acronym CIA stand for,Central intelligence agency +General,What do dieters say is the most difficult food to give up,Cheese,General,What does an agrostologist study,Grass,General,Henry Ford used assembly line in 1908 but someone before 1901,Ransome Olds +Entertainment,Which actor played the role of Chewbacca in the Star Wars trilogy?,Peter Mayhew,General,What is the name of the bone at the base of the human spine,Coccyx,General,Who is the greek god of shepherds and flocks,Pan +General,What are knackers to a Norwegian,Crisp bread,General,An infant whale is called a what,Calf,General,"Who wrote ""Stardust""",Hoagy carmichael +General,What in Dickensian London was the Marshalsea,Debtors prison,General,How long can it take for the insecticide DDT to break down in nature,Eight years,General,The bite from which insect caused the death of the poet Rupert Brook,"Mosquito," +General,"Which group sang the song ""Unforgiven""?",Metallica,People & Places,Where Do Sinhalese People Come Frome ,Sri Lanka ,General,In sport what is exactly 5 foot 8 inches off he ground,Bullseye on Dartboard +Art & Literature,Who wrote 'The Canterbury Tales'?,Geoffrey Chaucer,General,Anthony Pratt invented what in the 1940s,Cluedo,General,"Which fiberous, sulphur-rich protein occurs naturally in hair, horns, hooves and feathers",Keratin +General,What Is The Most Wideley Eaten Fruit In The World,A Banana,Music,What Are Maracas,Shaken Latin Percussion Instruments Made From Gourds With Beans Or Peas Inside,General,"What is the missing word in english with the letter combination 'uu' vacuum, muumuu, continuum, duumvirate, residuum",Duumvir +General,What Was The First Bond Movie NOT To Star Sean Connery In The Leading Role,On Her Majestys Secret Service,General,What is bed-wetting,Enuresis,Food & Drink,What is the most expensive type of Bottled Brandy ,Cognac  +Geography,"What river is called ""Old Man River""",Mississippi,General,The northern part of north america lies within the ______,Arctic circle, History & Holidays,Who was the second to set foot on the moon,Buz aldrin +General,In song who is the man who made Eastwood such a star,The fall guy – unknown stuntman,General,When is St Swithens day,15th July,General,Which of Jesus disciples was the treasurer,Judas Escariot +General,An 18th century elegant style of furniture,Chippendale, History & Holidays,Which Comedian Hit The Front Page Of The Sun On March 13 th 1986 For Allegedly Eating A Hamster ,Freddie Star ,Geography,To The Nearest 500 Million How Many People Are Alive Today ,"5,5 Billion People " +Science & Nature,What Does The BCG Vaccine Immunise Against ,Tuberculosis , History & Holidays,How Many Days After Hiroshima Did Nagasaki Receive The Second Atom Bomb ,Three , History & Holidays,Who received the nobel peace prize in 1964 for civil rights leadership?,Martin Luther King Jr + Geography,What is the basic unit of currency for Monaco ?,Franc,General,"Hamlet, Macbeth Othello which Shakespeare tragedy is missing",King Lear,General,"If you had some gentles, jig, gag and coop what are you doing",Fishing + History & Holidays,Who Was The Last King To Sit On The English Throne ,George VI ,General,Who wrote Das Kapital,Karl marx,General,What's the Spanish word for cottonwood tree,Alamo +Science & Nature,What element is represented by the symbol W?,Tungsten,General,Pediophobia is the fear of,Dolls,Music,Which Singer Did Diana Ross Play In The Movie “Lady Sings The Blues”,Billie Holliday +General,"What age of exploration began october 4, 1957",Space age,Science & Nature,Which astronomer first observed 4 moons of Jupiter in 1610?,Galileo Galilei,Sports & Leisure,Which Premiership footballer has the real name of Sulzeer? ,Sol Campbell  +Science & Nature,Which Metal Expands The Most When Heated ,Caesium ,General,Who fought the 100 years war?,France and England,General,Cockney Rhyming Slang: apples and pears,Stairs +General,Zelda Sayre was the wife of which U.S. novelist,F scott fitzgerald,Science & Nature,Who Did Jean De Brunhoff Create ,Babar The Elephant ,Food & Drink,What were the two major crops used by the Aztecs ? ,Maize (corn) and Amaranth  +General,What animal is thought to have inspired the myth of the unicorn,Rhinocerous,General,What does o.p.e.c stand for,Organisation of petroleum exporting countries,General,What is Charles E Chuck Yeagers Claim To Fame In The Field Of Aviation,First Man To Fly At Speed Of Sound +General,"Dorethy Parker said ""Scratch an actor and you will find"" what",An Actress,General,Where in the world would you find Adelie Land,Antarctica,General,What city in the USA means First People in Indian,Biloxi +Music,"""Epic"", ""From Out Of Nowhere"" And ""Falling To Pieces"" Were Early 90's Tracks From Which US Band",Faith No More,General,What is improved if yoU.S.leep on your right side,Digestion,Toys & Games,"If you ""peg out"" what game are you playing",Cribbage +Science & Nature,What Were Dachsunds Originally Bred To Hunt? ,Badgers ,General,Who was the final mayflower survivor,Mary allerton cushman,General,"A member of the Commonwealth, which is the most southerly African country through which the Greenwich meridian passes",Ghana +Sports & Leisure,"In the 1983 Wimbledon men's final who did John McEnroe beat 6-2,6-2,6-2? ",Chris Lewis ,General,In what religion are the Vedas a central part,Hindu,Science & Nature,Name the largest planet in the solar system.,Jupiter +Sports & Leisure,"When England Won The Five Nations Grand Slam In 1980, Who Was The Captain? ",Bill Beaumont ,General,"What insect family has the most varieties, making up 290,000 of the 751,000 insect classes",Beetle the beetle,General,What U.S. state includes the telephone area code 317,Indiana +General,Which word literally meaning sweet paste is a breakfast item,Marmalade,Music,What Was The Name Of Sean Ryder Post Happy Mondays Group,Black Grape,General,In the song who killed Cock Robin,Sparrow +Food & Drink,"Booze Name: A manhattan, but with scotch whiskey.",Rob roy,Sports & Leisure,Why Was Mathew Simmons In The News in 1995? ,Eric Cantona Kung Fu Kick ,General,"Which Band That Had Just One No.1 Hit In The 1980's Were Originally Called ""The Nightmares In Wax""",Dead Or Alive +Geography,What Record Does The 2nd Lake Pontchartrain Causeway Hold ,It Is The Longest Bridge In The World 23.9 M / 38.4 KM ,General,Cognac must be at least 5 years old before it's labelled what,Napoleon,General,What part of Britain is St. David the patron saint of,Wales +Geography,Which North Yorkshire village is famous for both its Racecourse and its Barracks ,Catterick ,General,A spectacular movement in which the dancer propels himself or herself around a supporting leg with rapid movements of the other leg while remaining in a fixed spot.,Fouetté en tournant,Food & Drink,Coquille St Jaques is a fancy name for what? ,Scallops  +General,What is the only place on earth that does not have a time zone?,Antarctica,Art & Literature,Whose Autobiography Was Entitled (Dear Me) ,Peter Ustinov's ,General,"In 1975, what re-opened after an 8 year closure",Suez canal +General,What country invented Phonecards,Italy,General,What does the German word Panzer literally mean,Armour,General,What sport's umpire sat in a padded rocking chair before 1859,Baseball +General,The first official baseball hat was made of what,Straw,Sports & Leisure,In 1998 Which ManUtd Player Became The Worlds Most Expensive Defender ,Jaap Stam ,General,Mendavoy and Martinez are characters in which TV show,NYPD Blue +General,Who is the Patron Saint of Young Virgins,St Agnes,General,What Sex Are Responsible For The Sales Of The Most Valentines Day Cards Is It Men Or Woman ,Woman,General,Joe Yule became famous as who,Mickey Rooney +General,Thief what hospital established the first us school of nursing,Bellevue,General,Name any of the demonstration sports at the Sydney Olympics,Ballroom Dancing – Snooker,General,Who wrote the original sherlock holmes stories,Sir arthur conan doyle +General,"Who recorded ""yakkety yak"" in 1958",Coasters,General,What show did Claire Danes get her start on?,My So-Called Life,General,How many members make up the House of Represetatives in the USA?,435 +Geography,What is the capital of Ukraine,Kiev,Science & Nature,The fourth planet from the sun is ____.,Mars,General,Who was hercules' father,Zeus +General,A persuasive or flattering person is said to have the gift of what,The gab,General,"In the Gasden Purchase, the USA bought territory from which country",Mexico,General,Where were the first books printed,China +General,What would a rhinotillexomaniac be doing,Picking their nose,General,From where do alsatians originate,Alsace-lorraine,General,"In Wallace and Grommit's 'The Wrong Trousers', what is the name of the penguin who steals the trousers",Feathers mcgraw +General,Silver hallmarks - what object is stamped on Birmingham items,Anchor,Music,"Which Band Had Hits With ""Dance Hall Days"" & ""Everybody Have Fun Tonight""",Wang Chung,General,"Which Country Has Won The Eurovision Song Contest The Most Times Behind ""Ireland""",France +Geography,In 1904 Danish Biologist Johannes Schmidt Discovered The Sargasso Sea In The North Atlantic To Be The Breeding Ground Of Which Fish ,European Eel ,Music,"Which Of The Gold Diggers Movies Contains The Song ""We're In The Money""",Gold Diggers Of 1933,General,"Name actor Von Ryan's Express, The Third Man, Brief Encounter",Trevor Howard +General,"Which country had the hottest temparture ever recorded in Sept. 13, 1922, at 136 F / 58 C",Libya (El Azizia),General,"What kind of music imitates the fanfares, drum rolls, & commotion of a battle?",Battaglia,Music,"With Which Singer Is ""Get Up I Feel Like A Sex Machine Associated""",James Brown +General,U.S. Captials - New Jersey,Trenton,General,Ferdinand Magellan Was The First Person In History Wo Do What,Sail Around The World,Music,Which Lennon & McCartney Tune Was A 1966 Hit For Cliff Bennett & The Rebel Rousers,Got To Get You Into My Life +General,Depeche Mode the 80s groups name translates as what,Fast Fashion,Entertainment,"In 'Coronation Street', who is Ken and Denise's son?",Daniel,General,Who danced with Gene Kelly in Anchors Aweigh in 1945,Jerry Mouse +General,The study of word origins is called what,Etymology,General,David Harold Mayer became famous as who,David Janssen,General,How is the Olympic torch lit,By the sun in Greece +General,What was voted the best monopoly piece in 1998,The Car,General,Who was the voice of Scooby Doo,Mickey Dolenz,General,What did J Edgar Hoover call home of disease bribery rape,Motels +General,What game tiles were first made with a pocket knife,Scrabble,Music,Who Were Guilty In 1980,Barbara Streisand & Barry Gibb,General,In the Bible what is the first mentioned colour,Green +General,The asteroidea are which order of creatures,Starfish,General,What is a peruke,A wig,General,What links Yul Bryner Burt Lancaster WC Fields Joe E Brown,Circus Performers +General,University Western Australia developed a robot to do what,Shear Sheep,General,Mo Moreland Was The Most Recognisable Member Of Which Dance Troupe Seen On UK TV In The 1970's & 1980's?,The Roly Polys,General,Where was president kennedy assassinated,"Dallas, texas" +Food & Drink,"Which drink, invented in 1886, was first marketed as the 'Esteemed Brain Tonic and Intellectual Beverage'? ",Coca Cola ,General,Johnny Depp is afraid of what,Clowns, Geography,What is the capital of Gambia?,Banjul +General,A stone slab at the top of a classical column aiding the support of the architecture. ,Abacus,General,What year was Walt Disney born,1901,Music,What's The Connection Between Robert Palmer & Duran Duran,The Power Station +Religion & Mythology,Who is the Norse god of the sky and thunder,Thor,Sports & Leisure,Who was first to win the US Masters five times? ,Jack Nicklaus , Geography,What is the basic unit of currency for Swaziland ?,Lilangeni +General,Film - who played sue snell in carrie,Amy irving, History & Holidays,Who introduced Xmas trees to England in 1841 ,Prince Albert ,General,What inventor said Genious is 1 percent inspiration and 99 percent perspiration,Thomas edison +Food & Drink,What name is given to the aniseed flavoured spirit drunk in Turkey? ,Raki ,General,Which spice gives curry its colour,Turmeric,General,Who wrote the novel 'The Picture of Dorian Grey',Oscar wilde +General,What country consumes the most tea per capita,Ireland,General,The Three Stars is the national ice hockey team which country,Sweden,General,What does a mosque's mihrab indicate the direction to,Mecca +Food & Drink,How many bottles of Champagne are there in a Magnum? ,Two Bottles ,General,Mail old trek: the providers live on what planet,Triskelion,General,Which Japanese picture-puzzle game is based on an initially blank grid of squares?,Hanjie +General,"In one of donald horne's novels, as what was australia dubbed",Lucky country,Music,Which Singer Was The Father Of Linda Womack,Sam Cooke,General,What is the sum of 99 - 33 - 33 - 33,Zero 0 +General,In the Balanta tribe women stayed married until what happened,Wedding dress wore out,General,Which literary character lives at 4 Privet Road,Harry Potter,General,"In Greek mythology, europa was the mother of ______",Minos + Geography,What is the largest of the countries in Central America?,Nicaragua, Geography,In which state is the source of the Mississippi River?,Minnesota,General,How many of the u.s states have similar names (like virginia and west virginia),Six +General,Who Did Andre Agassi Defeat When He Won His First Ever Wimbledon Singles Title,Goran Ivanisevic,General,In Indiana it's illegal for liquor stores to sell what,Cold milk/soft drinks warm ok,Science & Nature,What Is A Number Called That Is Equal To The Sum Of All The Numbers By Which It Is Divisible ,A Perfect Number  +Music,Who Was Tommy Steele's Original Backing Group,The Steelmen,Music,Who Did Sid Vicious Replace In The Sex Pistols,Glen Matlock,General,The Color of Money starring Paul Newman was released in this year,1986 + History & Holidays,"""In the Christmas song 'Rudolph the Red Nosed Reindeer' how is his nose described? """"You would even say it ___"""" "" ",Glows ,Sports & Leisure,Who Won The 2007 Masters Snooker Final On Sunday Night? ,Ronnie O''Sullivan ,General,"On ""Sesame Street,"" what is the name of Big Bird's teddy bear",Radar +General,The word 'bandit' comes from which language,Italian,General,Which group of Marvel superheroes fought Doctor Doom,Fantastic Four,General,Who was the narrator in Herman Melville's Moby Dick,Ishmael +Food & Drink," Even though it tastes nothing like grapes, a __________ is often eaten for breakfast.",Grapefruit,General,In what Australian state would you find Shellharbour,New south wales nsw,General,If you suffer from epistaxis what is wrong,Nosebleed + Geography,What is the basic unit of currency for Iceland ?,Krona,General,Which country (capital Luanda) lies just south of Zaire,Angola,General,What woman is thought of as the greatest trick shot artist of all time,Annie oakley +General,Stephanie Powers was Girl from UNCLE characters name,April Dancer, History & Holidays,Who was the female star of 'Bewitched' ,Elizabeth Montgomery ,General,The Bosphorus links which two seas,Black and marmara +General,In what language is the Magna Carta written,Latin,General,"What Was Designed In 1997 By Art Teacher ""Bruce Rushin"" To Symbolise The Progress Of Technology Within British Industry",The £2 Coin,Art & Literature,Who wrote the 'Noddy' books?,Enid Blyton +General,What is a group of this animal called: Swan,Bevy herd lamentation wedge,General,What did Sir Rowland Hill introduce in Britain in 1840,Envelopes,General,When did Brazil move its capital from Rio de Janeiro to Brasilia,1960 +General,Who wrote the book Billy Budd also Moby Dick,Herman Melville,General,What is the proper name for a falling star,Meteor,General,On what continent did the Incas live,South america +General,"Which disease is also known as ""Hansen's Disease""",Leprosy,General,What is the name of the horse in the black stallion,The black,General,In 19th century England what was a Snickerdoddle,Cookie +General,He gave us malted milk,William horlick, History & Holidays,What made Michael Milken famous and rich? ,Junk Bonds ,Music,Which instrument is principally used in Boogie-Woogie?,Piano +Science & Nature,What Colour Are A Puffin's Feet In Summer? ,Red ,General,What is the punishment for drunk driving in Norway,Three months of Government lectures,General,What extends from the Arctic to the Aral Sea in Russia,Ural mountains +General,Who succeeded Pompidou as French president,Valery giscard d'estaing,Music,In 1981 Who Was Arrested By The Indian Army On Suspicion Of Spying During A Round The World Flight,Gary Numan,General,Mother maybelle and the carter family were regulars in this variety show,Johnny cash show +General,Dolores Ibarruri was better known under what name during the Spanish Civil War,La passionaria,General,What is a non-cancerous tumour,Benign, Geography,Where are the Nazca Lines?,Peru +General,In what country was John McEnroe born,Germany,General,"What is an example of a notable ""football stadium""",Wembley stadium,General,Whose headstone reads 'that's all folks',Mel blanc +General,What is the currency of Guatemala?,Quetzal,General,From Which Movie Did The Pop Group “Duran Duran” Get Their Name?,Barbarella,General,The Swiss Royal Exchange building is commonly known by what epiphet?,Gherkin +General,What is the botanical name of the flower known as 'Autumn Crocus',Colchicum,General,Actor Larry Hagman Is Actually The Son Of Which Very Famous Actress,Mary Martin,General,Persian Sultan Selim hanged 2 doctors - advised him stop what,Drinking Coffee +General,In which constellation is the star cluster Pleiades,Taurus,General,In sailing ship days who often acted as the ships doctor,Cook,General,"In telephony, what do the initials ADSL stand for",Asymmetric digital subscriber line +General,"With age, what organ shrinks faster in males than in females",Brain,Geography,On what mountain are four presidents' faces carved,Rushmore, History & Holidays,"How many witches were burned at the state in salem, massachusetts ",None  +General,"Who recorded ""why"" in 1960",Frankie avalon,Science & Nature,What Is A Common Name For The Trachea ,The Windpipe ,General,"Quakers Natural, Prewetts Honey, California Revival - types what",Museli + History & Holidays,What Was The Name Given To Those Who Campaigned For Social Reform In England Between 1836 & 1858? ,The Chartists ,Religion & Mythology,Whose name did God change to Israel?,Jacob,General,Which two countries formed tanzania,Tanganyika and zanzibar + History & Holidays,In which 1944 film were Trick and Treat children to be seen ,Meet me in St. Louis. ,General,What do you give on the third wedding anniversary,Leather, History & Holidays,What does the name Dracula mean in Romanian,Son of the Devil  +General,Where did bagpipes originate,Middle east,Science & Nature,Who is known as the father of genetics,Gregor mendel, Geography,Nassau is the capital of which country?,Bahamas +General,Which country has the most southerly city,Chile,General,In which of Goldsmith's Comedies does Kate Hardcastle appear,She stoops to conquer,General,What is the modern name of the Roman town of Lutetia,Paris + History & Holidays,Approximately how many children did pharaoh Ramses II father?,160,General,Where can Americans always see the time as 4.10,$100 bill Independence hall clock, History & Holidays,Who developed the first nuclear submarine?,Soviet Union +General,In Which Classic 80's Tv Show Would You Find A Sheppard and His Lost Sheep?,The Dukes Of Hazzard,General,Zipporah was the wife of who in the Bible,Moses,General,The busiest muscles in the body are found where,Eyelids +General,"A row of columns, usually equidistant, supporting a beam or entablature. ",Colonnade,Sports & Leisure,Which football team plays it's home games at The Dell? ,Southampton ,General,What does 180 degrees make,Straight line +General,In Life of Brian what name does Stan want to be known as now,Loretta,General,Who is the Norse god of thunder & war,Thor,General,What is the Capital of: Mongolia,Ulaanbaatar +General,Which spirit is added to sugar and egg yolks to make Advocaat,Brandy, History & Holidays,In the Christmas song who 'are getting fat'? ,Geese/Goose ,General,What was invented by Dr Edward Land in 1947,Polaroid +General,What gets nine inches longer when its up,Concord – heat expansion,General,"King Farouk the First of Egypt was overthrown in 1952 and spent his exiled years mainly in which country, of which he became a citizen",Monaco,General,What country eats the most cereal per capita,Turkey + History & Holidays,Who Was The First Roman Emperor ,Augustus (Gaius Julius Octavianus) ,General,What is the tube that carries urine from the kidneys to the bladder,Ureter,General,Who wrote A History of English Speaking Peoples,Winston churchill +General,Where could you spend a Lempira,Honduras,General,Name the ruling family of the Austro-Hungarian Empire in 1914.,Habsburg,General,In 1982 San Francisco became the first US city to do what,Ban sale possession of handguns +General,What is the state tree of Idaho,White Pine,General,Two door car with a hard roof and slopping back,Coupe,General,Who directed the film 'bowfinger',Frank oz +General,If food has been 'devilled' how would you expect it to taste,Hot,General,What common word comes from Knights after the Crusades,Freelance,General,Which Pope died on today's date in 1978,John paul 1 +General,What island group is off the east coast of southern South America,Falkland islands,General,"Which film, directed by Oliver Stone, won Best Film Oscar in 1986",Platoon,General,Which UK punk group had hits White Riot and London Calling,The Clash +General,Which country was split into two zones by the Yalta agreement,Germany,General,"Who wrote ""the french lieutenant's woman""",John fowles,General,Relating to food what is 'halloumi',Cypriot cheese +Religion & Mythology,"According to the Bible, who were the brothers of Jesus?",James and John,Music,"Whose Hit Singles Album Was Entitled ""Around The World"" The Journey So Far",East 17,General,"Which soldier, the last-surviving British soldier of World War I, is immortalised in a song by Radiohead?",Harry Patch +General,There are how many miles of nerves in the skin of a human being,45,Music,"""Seven And The Ragged Tiger"" Was An Album By Which 80's Band",Duran Duran,Science & Nature,What element has the periodic table name Au ?,Gold +General,What did the 1980 U.S. naval academy class have for the first time in history,Women graduates,General,In Disney's 1973 animated Robin Hood what creature was Robin,A Fox like Marion,Science & Nature,How Many Bones Are There In The Adult Human Body ,206 Bones  +Food & Drink,Who is McDonald's mascot(full name),Ronald mcdonald,Music,"Which Solo Artist Won A Brit For Best Single At The 2005 Brits For The Song ""Your Game""",Will Young,General,In thermodynamics what word describes the disorder of a system,Entropy +General,Who was the director of 'terminator and titanic',James cameron,Music,"Rleased By Island In 1984 ""Legend"" Was A Collection Of Whose Singles",Bob Marley, Language,What is the literal meaning of 'pince-nez'?,Pinch nose +Science & Nature,What material was invented by Belgian-born US chemist Leo Baekeland in 1905? ,Bakelite ,Mathematics,2 + 5 x 6 = ?,32,General,Sameer Bhatia from Bangalore began what service,Hotmail +General,In what Australian state would you find Hobart,Tasmania,General,Blackburn Manager (2008) Paul Ince Has A Very Famous Sporting Cousin Who Is It,Nigel Benn,General,Which singer was given the nickname 'The Killer',Jerry lee lewis +General,How many legs does a spider have,Eight,Geography,What Is The Largest Island In The Mediterranean ,Sicily ,General,Istanbul and Constantinople what else was it called,Byzantium + History & Holidays,Who helps Santa make toys? ,Elves ,General,What is a group of ducks on the water,Raft,General,What gas in the blood of divers can cause the bends,Nitrogen +General,Picasso was almost left for dead when he was born. who saved his life,An,General,The ghost of Anne Boleyn might be expected to walk in which London palace,Hampton court,General,What's the basic unit for measuring electric energy,Joule +General,Who created the muppets,Jim henson,General,"Whose work is never done, according to a popular saying",A woman's,General,What happened to women who tried to watch original Olympics,Thrown off cliff +Art & Literature,"A group of English painters formed in 1848. These artists attempted to recapture the style of painting preceding Raphael. They rejected industrialized England and focused on painting from nature, producing detailed, colorful works.",Pre-Raphaelite Brotherhood,Toys & Games,Eva Gabor and Johnny Carson popularized this game by climbing over each other.,Twister,Music,Number of permutations of the band Wings,6 +General,Who wrote the novel The Betsy,Harold Robbins,Art & Literature,What was sherlock holmes most famous novel?,The Hound of The Baskervilles,General,What is the violet variety of quartz,Amethyst +Food & Drink,What Is A Bouquet Garni ,Mixed Herbs In A Perforated Sac ,General,Who married shania twain,John lange,General,Who wrote 'sexual behavior in the human male' in 1948,Alfred kinsey +General,Star wars: what was the name of the planet destroyed by the death star,Alderaan,Music,Which 60's Tv Comedy Show is Mentioned On The Sargeant Peppers Lonely Hearts Club Band Album,Meet The Wife On (Goo Morning Good Morning), History & Holidays,Which 1976 Movie features the character 'Travis Bickle'' ,Taxi Driver  +General,Where did guinevere retire to die,Amesbury,General,In which country do the Indri and Sifaka Lemurs live,Madagascar,General,Who went on to become an Eastern Communist leader after working as a pastry chef at London's Carlton Hotel,Ho Chi Minh +Science & Nature," Bats are the only mammals that are able to fly. The ""flying squirrel"" can only do what the gliding opposum does _ glide for short __________",Distances,Science & Nature,Myositis affects the _________.,Muscles,General,How Many Claws Does A Domestic Cat Have,18 + History & Holidays,"""When visiting Finland, Santa leaves his sleigh behind and rides on a: """"BroomStick, Rudolph, A Goat Named Ukko, A White Goose"""" "" ",A Goat Named Ukko ,General,What designation denotes the hardest type of pencil lead?,9H,Science & Nature,What is the fastest breed of dog ?,Greyhound +Science & Nature,"This animal is normally measured in ""hands"".",Horse,General,Which tube line goes to Brixton,Victoria,General,Whose legs were banned from metro posters too distracting,Marlene Dietrich +Music,Which Song That Has A Drink Mentioned In The Title Was A Xmas No.1 Way Back In 1988,Missletoe & Wine,General,Aretha Franklin sang this song in the original Blues Brothers movie?,Think,General,"Which word, associated with Nuclear Physics, is a variation of a Greek word meaning 'final cut'",Atom +Geography,"What Is Common To The Republic Of Ireland, Northern Ireland, Scotland, Wales, Monaco, Denmark & Portugal ",They All Have A Coastline And A Land Border With Only 1 Other Country ,General,In which English city is the Martyr's Memorial,Oxford,General,What did William the Conqueror regard as a sign of victory in the battle of Hastings and also appears on the Bayeux Tapestry?,Halley's Comet +Science & Nature, The poison_arrow frog has enough poison to kill about __________,"2,200 people",General,"Who produced the album In My Life, featuring covers of Beatles' songs by various artists",Sir george martin,General,Which part of a boar is called a wreath,Its tail +General,The Mercedes Benz car company is based in which German city,Stuttgart,Entertainment,"Mel Blanc, the voice of Bugs Bunny, was allergic to what?",Carrots,General,Navy ranks: stars a 'commodore' has,1 +General,What Olympic event was dropped in 1920,Tug of War -1900 to 1920,General,Who was the first woman to receive The Order of Merit 1907,Florence Nightingale,General,Who wrote The Rights of Man - and The Age of Reason,Thomas Paine +General,Who composed the Christmas Oratorio,J S Bach,General,Which Magazine Was Founded In 1979 By “Chris & Simon Donald”,Viz,Music,According To Barry Manilow What Is The Name Of The Showgirl At The Copacabana?,Lola +Music,Which Rock Star Took His Stage Name From An Anagram of Oral Sex?,Axl Rose,General,In what film was line A mans gotta do what a mans gotta do,Alan Ladd Shane,General,What are the Buckingham Palace guards commonly known as,Beefeaters +General,Which Former Radio Luxembourg DJ Found Lasting TV Fame In 1966 In The Person Of A Memorable Comic Creation,Warren Mitchell As Alf Garnett,General,What is 'murgh' in an Indian restaurant?,Chicken,General,"The 'Three Graces' in Liverpool consist of the Liver Building, Port of Liverpool building and which other?",Cunard Building + Geography,What is the basic unit of currency for Belize ?,Dollar,Entertainment,"Which native of Flint, Michigan, once advised us to ""drive your Chevrolet through the USA""?",Pat Boone,General,"This song by the Shirelles, fought its way to the top of the pop chart",Soldier boy +General,What is an angle greater than 90 degrees,Obtuse,General,From which village did the pop group 'The Village People' hail,Greenwich village,General,Which Bonn musician left for Vienna in 1792 to study Haydn,Beethoven +Music,Roland Gift Was The Lead Singer Of Which British Group,Fine Young Cannibals,Science & Nature,What toe is the foot reflexology pressure point for the head?,Big toe,Entertainment,What license plate number is on the Volkswagon on the cover of The Beatles' 'Abbey Road' Album?,281F +General,Which creature has a carapace and a plastron,Turtles shell up low,General,A connoisseur of good food,Gourmet,Science & Nature,Which meteor shower occurs on the 21st April ?,Lyrids +General,"Which Greek did Cicero call ""The Father of History""",Herodotus,General,The Aphrodite of Melos has a more famous name - what,Venus de Milo,General,"Whats the computer term ""bit"" short for",Binary digit +Music,"Who Had A Hit With ""(Ive Had) The Time Of My Life""",Bill Medley & Jennifer Warnes,General,So far 11 US presidents have been what,Generals,General,In the UK 9 out of 10 people live within walking distance of what,A Bus Stop +General,Sofia is the capital of ______,Bulgaria,People & Places,"Who Wrote A personal cheque for $23,000,000 when buying RKO ? ",Howard Hughes ,General,What saint is the huge church in the Vatican named after,St peter +General,What was the name of the first space shuttle ever built,Enterprise,Music,Which Influential Electro Band Were Founded In Sheffield In The 1970's By Stephen Mallinder & Richard H Kirk,Cabaret Voltaire,Music,Which Record Went Top 10 For Roy Orbison In This Decade,I Drove All Night +General,3 chemical elements most % human body O 65% C 18% and ?,Hydrogen 10%,Science & Nature,These animals were once used to bleed the sick.,Leeches,General,Gossima was the original name of what game,Table Tennis +Science & Nature,In The 19th Century Who Was The Railway King ,The Financier George Hudson ,General,"What was Paul Tibbett's claim to fame, established on August 6th 1945",Pilot of the plane which dropped atom bomb on hiroshima,General,What is the fastest land animal,Cheetah +General,"Which comedian's catch phrase was I won't take my coat oft, I'm not stopping",Ken platt,General,Which is the U.S. Mormon State,Utah,Food & Drink,"What did Queen Elizabeth I, most Popes and Adolf Hitler all insist on having at their dinner table ? ",Taster  + History & Holidays,Which two words are inscribed on a Victoria Cross? ,For Valour ,General,What expression did clark kent's newspaper boss like to use,Great caesar's,General,The tenth part of a Roman legion consisting of 600 men was known as what,A cohort +General,With what sport is jack nicklaus associated,Golf,General,"In the Star Trek: Voyager series, name the ex-Borg who is a member of the 'Voyager' crew",Seven of nine,General,What insect in Spain is known as La Cucaracha,The Cockroach +General,What name is given to the volcano that broods over Catania,Mount etna,Music,Which Beatles movie won the Oscar for Best Original Score?,Let It Be,General,What movie memorabilia sold at Christies in 1987 for £82500,Chaplains Hat Cane +General, The study of religion is _________.,Theology,General,Which group of people first used gold fillings,Incas of Peru, History & Holidays,What Office Did Churchill Hold For The Longest Continous Period? ,Prime Minister  +General,The study of the composition of substances & the changes that they undergo is __________,Chemistry,General,A cholecyst is more commonly known as a(n)___.,Gallbladder, History & Holidays,"U.S. President, John Quincy ________.",Adams +General,What do the cheeses Gruyere and Emmenthal have in common,Holes,Music,What Is The First Word Of The Classic Gloria Gaynor Hit “I Will Survive”,At (First I Was Afraid),General,What is the highest mountain in the world,Mt everest +General,Which Danish philosopher's name translates as 'churchyard',Kierkegaard,General,"What in London are the Whitechaple, Courtald and Heywood",Art Galleries,Entertainment,Who did Vivian Vance play on 'The Lucy Show'?,Vivian Bagley +Food & Drink,"If you ordered Cherries Jubilee, how would you expect them to be cooked? ",Flambeed (in kirsch) ,General,What is the decimal equivalent of binary 1111,Fifteen,General,Who won an Oscar for the African Queen,Bogart +Art & Literature,Who Wrote The Tin Tin Stories ,(Georges Remi ) Herge ,General,"After leaving 'The Belmonts', which was Dion's first hit on the Laurie label in October 1960",Lonely Teenager,General,What is the crime of embracery,Jury Bribing +General,39% of women admit doing this to their boyfriend,Throwing a shoe at him,General,Which is the last of the four Grand Slam tennis tournaments to be played in the year,Us open,Music,"In 1967, Who Became The First Singer To Win The Eurovision Song Contest For The UK?",Sandie Shaw +General,Who invented the chair,Egyptians, Geography,What river is Liverpool on?,Mersey,Sports & Leisure,What are the only two colours a table tennis ball is allowed to be in competitions?,White and Yellow +General,The main circle and trilithons at Stonehenge are built of which rock,Sandstone,General,"Particularly fond of dahlias and chrysanthemums, which garden insect has the scientific name forficula auricularia",Earwig,General,What name is given to the bonds which link amino acid molecules together to form proteins,Peptide bond +General,Who sang the song 'Ironic'?,Alanis Morisette, History & Holidays,What Was The Name Of The Film Which First Featured Bing Crosby's Song 'White Christmas'' ,Holiday Inn ,General,Who was the first British SF author to win a Hugo award,Eric Frank Russell Allamagoosa +Science & Nature, __________ chinchillas were brought from the Andes Mountains in South America in the 1930's. All chinchillas presently in North America are descended from these __________ chinchillas.,11,General,Whose character name was also the title of the show?,Punky Brewster,General,There are 12 bottles of champagne in a what,Salmanazar +General,Who was nicknamed The First Lady of Song,Ella Fitzgerald,General,Who introduced bagpipes to the british isles,Romans,General,Which ocean has an area of approximately 166 sq km,Pacific ocean +General,Which English actress was most famous for her role in Wilde's The Importance of Being Earnest,Dame edith evans,General,Where could you spend a Markka,Finland,General,What is the size of each angle in an equilateral triangle,Sixty degrees +Music,"Who Did Paul McCartney write the song ""Hey Jude"" For",Julian Lennon,Sports & Leisure,"He Wears a No.10 Jersey, I Thought It Was His Position But It Turns Out To Be His IQ Who Was George Best Describing ",Paul Gascoigne ,General,What sea is situated between Vietnam & the Philippines,South china sea +Science & Nature," A rodent's teeth never stop growing. They are worn down by the animal's constant gnawing on bark, leaves, and other __________",Vegetable matter,Science & Nature,Which star is nearest the Earth ?,Sun, History & Holidays,Which Queen Of England Was Known As Bloody Mary? ,Mary I  +General,What are the three main types of Greek columns,"Doric, ionic & corinthian",General,What is a group of this animal called: Geese,Flock gaggle skein,Entertainment,"Who played Dorothy in ""The Wizard of Oz""?",Judy Garland +Sports & Leisure,What Did The Inter Cities Fairs Cup Change It's Name To ,UEFA Cup ,General,In the film Gremlins what is the true name for Gizmos race,Mogwi,General,"What theme park is located in buena park, california",Knotts berry farm +General,How much water would a human drink in a lifetime,500 million litres,General,Why do Tibetans grow long nails on little fingers,To pick noses efficiently,Music,What Is The Pop Star Darius's Sir Name,Danesh +General,Rene Lalique - Art Nouveau designer worked what material,Glass,General,"Dire straits sings 'here i am again in this mean old town, and you're ______'",So far away from me, History & Holidays,In Which Scary Movie Will You Find The Character 'Dr Egon Spengler' ,Ghostbusters  +General,The famous last words: 'go away___ I'm alright' were uttered by whom,H.g. wells wells,Science & Nature,In 1997 There Was A Mid Air Collision Between A Saudi Boeing 747 And A Kazak Airliner That Claimed The Lives Of All 349 People On Board Near Which City Did It Occur ,New Delhi ,General,A road known as a corniche would be located where?,On the Coast +General,"Which single didn't get banned by the BBC despite the line, ""Candy never lost her head even when she was giving head""",Walk on the wild side,General,Which chenical element has the highest melting point?,Carbon,General,Quarter oz whiskey half ounce sloe gin makes a black what,Hawk +General,Who was the first trapeze artist to perform without a net,Jules Leotard,Music,In What Year Did Wet Wet Wet Top The UK Singles Chart For 15 Weeks With “Love Is All Around”?,1994,General,Who signed the USA for Africa poster with his thumbprint,Stevie wonder + Geography,What is the capital of Nepal ?,Kathmandu,General,“ Gracie Fields ” Was Born In A Fish N Chip Shop On January 9 th 1898 In Which North West Town,Rochdale,General,"Who is famous for writing 'interview with the vampire', the first book in the vampire chronicles",Anne rice +Religion & Mythology,Which Norse god had the Valkyries as handmaidens?,Odin,Entertainment,Name Alley Oop's girl friend,Oola,General,What is the third book of the Old Testament,Leviticus +General,What's the average number of murders a child has seen on tv by the age 16,"18,000",General,From what language does the word alphabet come,Greek -alpha beta,General,Ennisophobia is fear of what,Criticism +General,Who would use a barny to reduce noise,Film Cameraman cosy over cam,General,What character did robert urich play in SWAT,Jim street,Music,Guess The Band From These Initials VA / MB / MC / EB / GH,The Spice Girls +General,On Monday Morning July 16th 1945 The World Was Changed Forever When The Worlds First Atomic Bomb Was Tested In An Isolated Area Of Where,New Mexico,Science & Nature," __________ sea otters spend almost all of their time in the water. Alaska sea otters often sleep, groom, and nurse on land.",California,Music,Who Wrote The Score To West Side Story,Leonard Bernstein +General,What is the medical term for 'loss of memory',Amnesia,General,Books original title 4.5 years struggle against lies stupidity & co,Mein Kampf,General,What actor dropped out university to be a dishwasher,Warren Beaty +General,Who won an Oscar posthumously,Peter Finch – for Network,General,Which Is The Only English Football Team To Play In The Welsh Football League,Owestry FC,General,Colgate (the toothpaste) translates to what in Spanish,Go hang yourself +General,What was the name of richard dawson's character on 'hogan's heroes',Peter,General,Who performed the worlds first human heart transpant,Dr christian barnard,General,Who was the leader of the wolf pack in The Jungle Book,Akala +General,Who wrote the novel 'Little Women',Louisa may alcott,Sports & Leisure,What number wood is a driver in golf?,One,Music,Who Directed The Stage Version Of The Lion King,Julia Taymor +Music,What Was The Beatles Best Selling Single In Britain,She Loves You,Music,What Was The Bangles Only Number One Hit In The UK?,Eternal Flame,Food & Drink,What Is The Hemispherical Pan Used In Chinese Cooking ,A Wok  +General,In astrology what was the age before Aquarius,Age of Pisces,General,What was the club house of the Get Along Gang?,A red Caboose,Science & Nature,The process of removing salt from sea water is known as ___________.,Desalination +General,What country must import river sand from Scotland for construction & camels from North Africa,Saudi arabia, History & Holidays,What was Brenda Lee doing Around The Christmas tree in 1958 ,Rocking ,Music,_______ Rain Was A Minor Hit For Bruce Hornsby In 1987,Mandolin +General,A study of physics and chemistry of celestial bodies,Astrophysics,General,The hardy boys and ______,Nancy drew,Music,Which Famous Composer Did Falco Sing About,Amadeus +General,What year was Archduke Ferdinand assassinated,1914,General,What is the closest planet to the sun,Mercury,Music,"Which Tv Detective Sang About A ""Silver Lady"" In 1977",David Soul +Art & Literature,Who Wrote The Gulag Archipelago ,Alexander Solzhenitsyn ,General,What was Rocky Balboa's nickname in the ring,The italian stallion,General,In 1896 the speed limit for horseless carriages was raised from 4 mph to what,14 mph +General,In what city was the worlds first blood bank opened 1940,New York – Richard Charles Drew,General,"Who said ""sex older women best they think its their last time""",Ian Fleming,Entertainment,Who wrote 'Roll Over Beethoven'?,Chuck Berry +Science & Nature,What Vitamin is Thiamine?,Vitamin B1,General,What is the Capital of: Dominican Republic,Santo domingo,General,Peniaphobia fear of what,Being Penniless +General,What Beatle is left handed,Paul mccartney,General,Which song was Britain's first Eurovision Song Contest winner,Puppet on a string,Science & Nature," Zebus are humped cattle found in India, China, and northern Africa. Zebubs are tsetse_like flies found in __________",Ethiopia +General,"A flattened, shallow column or pier projecting from a wall. It usually has a base, shaft, and capital but is decorative rather than structural. ",Pilaster, History & Holidays,What 'MBC'' Was A Christmas Number One In December 1978 ,Mary's Boy Child ,General,What is the name of Mighty Mouse's girl friend,Pearl Pureheart +General,Wadi al Muli is better known as where,Valley of the Kings,General,How many tail feathers has a Kiwi,None,General,A coffee pot with a plunger that pushes the grounds to the bottom,Cafetiere +General,International Phonetice Alphabet: F,Foxtrot,General,What is a group of this animal called: Kangaroo,Troop mob,General,In ancient Greece young brides had to sacrifice what,Their Dolls – show they were grown up +General,What was the name of King Arthur's sword,Excalibur,Music,"Who Wrote The Songs Manic Monday, Nothing Compares To You And I Feel For You",Prince,General,As what is a camelopard also known,Giraffe +Music,"Now Immensely Famous ""She's A Superstar"" Was The First Chart Entry For Who In 1992",The Verve,General,"In which movie, in 1962, did Muhammad Ali appear",Requiem for heavyweight,General,Country and Western music star Harold Jenkins is better know as what,Conway twitty +General,Ichthyology is a study of ______,Fish,General,Whose middle names are Charles Linton,Tony blair,General,Fredericton is the capital of which Canadian province,New brunswick +General,Robert Brown M Caroline Bliss Moneypenny who was Bond,Timothy Dalton,General,How long did the hundred years war last,116 years,General,In which of the world's continents would you find Mount Erebus,Antarctica +Geography,What is the capital of Switzerland,Bern,General,Who created Tarzan (all names) in 1914,Edgar Rice Burroughs,General,"A male dancer who performs the 'princely' roles of the classical ballet, such as the Prince in Swan Lake.",Danseur noble +General,Maoni Vi of Capetown has the world longest what at 28 inches,Pubic Hair,General,Poland has a coastline along which sea,Adriatic,Science & Nature,What is the scientific name for a turkey's wishbone?,Furcula + History & Holidays,Which Comedien started Writing For Private Eye Magazine Ensuring Its Success ,Peter Cook ,General,Where is the world's largest restaurant,Bangkok,Sports & Leisure,"What goes to the F.A. cup final every year but never gets used, and never will get used? ",The Losers Ribbons  +General,What does 'i.o.u' mean,I owe unto,Science & Nature,Whose Report Led To A Much Reduced British National Rail system The 1960's ,Dr Beeching ,General,What is the young of this animal called: Elephant,Calf +General,What is a group of this animal called: Crocodile,Float,Science & Nature,The green variety of beryl is called ________.,Emerald,General,What did marie curie die of on 4th july 1934,Radiation poisoning +General,"What 1945 film won best picture, actor, director Oscars",The Lost Weekend,General,"When the students in 'dead poets' society' stood on their desks and said 'o captain! my captain', who were they quoting",Walt whitman,General,What's the process of splitting atoms called,Fission +General,"Which word is used to mean, malicious enjoyment at the misfortunes of others ?",Schadenfreude,General,"Plus fours, the type of trousers much loved by golfers, are an example of which type of men's fashion",Knickerbockers,General,He Marquis of Blandford is heir to which British Peerage,Duke of marlborough +Science & Nature,What Part Of The Body Is The Axilla ,The Armpit ,General,Chuck McKinley was the only American to do what in the 60s,Win Wimbledon,General,In England placing what upside down is considered treason,A Postage stamp Monarchs head +General,Who sells turbo c++,Borland,General,What kind of animal is named after the botanist PeterPallas,Cat,General,"Who said ""More people would be alive if we had a death penalty""",Nancy Reagan +General,67% of dog owners do what at holiday time,Buy a present for dog,Music,What was Tom Jones' first UK number one single,It's Not Unusual,General,When did Levi Strauss produce the first jeans,1850 +General,What is the most common atom,Hydrogen,General,What is the fear of changes known as,Metathesiophobia,General,Who was executed in 1936 for the kidnapping and murder of Charles Lindbergh Junior,Bruno hauptman +General,What city was named for it's mining and trading of salt,Salzburg,General,The Canadian air force won the 1948 olympic gold medal for what sport,Hockey, History & Holidays,Who Released The 70's Album Entitled Cosmo's Factory ,Creedence Clearwater Revival  +General,What sea creature uses its chest as a table while floating on its back,Sea,General,What lollies are named after the safety devices they look like,Life savers,General,Homilophobia is the fear of,Sermons +General,Who was the last Roman Catholic monarch of England,James the second,General,What year did Neil Armstrong land on the moon,1969,General,Hanoi is the capital of ______,North vietnam + History & Holidays,"With two words, complete this BBC radio signal to the French Resistance in the film The Longest Day. The long sobs of autumns violins wound my heart with _____ ",Monotonous langour ,General,What is the common name for Sturnus vulgaris,Starling,General,"Name the character played by John Cleese in ""A Fish called Wanda""",Archie leach +General,"Ares, Thor and Mars are all what",Gods of war,General,What do you put on bread & butter to make fairy bread,Hundreds & thousands,General,James O'Barr authored this graphic novel which was made into a movie,The crow +General,American indians used beads as currency. What was it called,Wampum,Science & Nature,What Colour Does Litmus Paper Become If It Is In Contact With An Alkaline? ,Blue ,General,What is 6 inches bigger in Summer,Eiffel tower +General,Live Aid was to benefit which starving country?,Ethopia,General,Alexandria MN if wife asks man must do what before sex by law,Brush Teeth,General,What was the Roman name for Colchester?,Camulodunum +Food & Drink,What do you call the Irish dish of Mashed Potato's with chopped Spring Onions ,Champ ,General,Which is the largest aquatic bird,Albatross,General,All of the officers in the confederate army were given copies of what book,Les Miserables + History & Holidays,"Since 1923, the National Christmas Tree on the White House lawn has been lit every year _except _ one. What year was that, and why wasn't it lit? ",John F Kennedy ,General,"What is the most widely spoken member of the Iranian branch of the Indo-Iranian languages, a subfamily of the Indo-European languages",Persian,General,"Any dance to slow music; also, part of the classical pas-de-deux in ballet.",Adagio +General,"What German siren sang ""99 Luftballons"" (known in the U.S. as ""99 Red Balloons"")?",Nena,General,What U.S. city is called the Gateway to the West,St louis,General,Which aromatic rice is traditionally used in Indian cookery,Basmati +Science & Nature,What is the horn of a rhinoceros made of?,Hair, History & Holidays,"Who Uttered The Immortal lines 'They Think It's All Over, It Is Now'' ",Kenneth Wolstenholme ,General,Kallium is the old name for which element,Potassium + History & Holidays,How did the bum convince the family dog to start eating again in Down and Out in Beverly Hills? ,He ate the dog's food ,General,What Connecticut town introduced the first telephone directory in 1878,New,General,A Philargyrist Is A Person Who Falls In Love With & Obsesses About What,Money +General,Of what were 500 million printed with elvis presley on the face,Stamps, History & Holidays,Calpurnia Was The 3rd Wife Of Which Roman Emperor ,Julias Caesar ,General,"Boxing, Wrestling and which Olympic event still exclude women",Weight Lifting +Music,"Which Singer Had A Worldwide Hit In 1991 With His Solo Debut ""Crazy""",Seal,General,In a bar in which European country might you be served 'tapas',Spain,General,Who Was The 1st American Actor To Be Nominated For 3 Emmy Awards For Playing The Same Character In 3 Different Shows,Kelsey Grammer +General,In the Bible who did God appear to on Mount Horab,Moses, Language,"What does ""c'est la vie"" mean?",That's life,General,Who composed Appalachian Spring,Aaron Copeland +General,Process of printing developed in 1798 by the german map inspector aloys senefelder,Lithography,General,"The cocktail ""Daiquiri"" contains limejuice and which spirit",Rum,General,What is a toque,A Canadian winter hat +General,"What's the name of the cat in the ""Nine Lives"" TV commercials",Morris,Music,Who Is The Lead Singer And Prolific Songwriter Of Babybird,Stephen Jones,Music,"What was the on flip side of the 45 of Beatles track ""She Loves You""?",I'll Get You +General,"Which German film director made the film M, in 1951",Fritz lang,General,China 300 bc you could not speak to the Emperor without what,Clove in your mouth,Music,"Phil Collins First Name Is Actually A) Eddie, B) Phil, C) George",Phil +General,A Klazomaniac cant stop doing what,Shouting,General,A flea can jump how many times its own length,100, History & Holidays,In the 17th century which country started taxing beards ?,Russia +General,What do chickens do during a total solar eclipse,Sleep,General,In which film did we meet Baron Numpsi as the villain,Eddie Murphy's The Golden Child, History & Holidays,"Which Australian cricketer, playing for New South Wales against Queensland, scored a then world record first-class innings of 452 not out? ",Donald Bradman  +General,What now famous painter was once so impoverished that he kept warm by burning his own paintings?,Pablo Picasso,General,What film did the Beatles make for television?,Magical Mystery Tour,Toys & Games,Mundane toy that involved sticking plastic pieces on scenes.,Colorforms +Sports & Leisure,Which Yorkshire football team plays at home at oakwell ,Barnsley ,Entertainment,Which british group recorded the 1983 hit 'Owner Of A Lonely Heart'?,Yes,General,Morrissey was the lead singer of which band,The Smiths +Science & Nature," Cats, not dogs, are now the most common pets in America. Approximately 66 million cats to 58 million dogs are family pets, with parakeets ""flying"" a distant third at __________","14,000,000",General,"In musical notation, what is the term for the symbol at the beginning of a staff",Clef,Music,"""Kimono My House"" Was The Debut Album For Which Group",Sparks +General,Tosca is the heroine of the opera but what is her first name,Floria,Music,What Is The First Name Of The Pop Superstar Known As Prince,Prince,General,Eric Claudin is better known by which eponymous title,The Phantom of the Opera +General,Which gas is the most abundant in the atmosphere,Nitrogen,General,Abracadabra comes from what language,Hebrew,General,Russophobia is a fear of ______,Anything russian +General,To who did father damien minister from 1873 to his death,Molokai lepers,Music,Which Famous Band Were Formed In 1977 & Took Their Name From Their Current Financial Situation,Dire Straits,General,What sea does the Danube river flow into,Black sea +General,Which drink was designed as a malaria cure,Benedictine,General,"In egyptian mythology, who was horus' mother",Isis,Food & Drink,How Did Garam Masala Get It's Name ,It Is Hindi For 'Hot Spice'  +General,Triskadeccaphobia is the fear of what,Number 13,Music,Which Legendary Reggae Artist Died In 1981,Bob Marley,General,Who led the mormons to the great salt lake,Brigham young +General,What part of the body is affected by conjunctivitis,The eye,General,Who wrote the children's novel The Story of Doctor Doolittle,Hugh lofting,Music,Which Former Yes Member Recorded Journey to The Center Of The Earth,Rick Wakeman +Music,"Cheryl, Mike, Bobby & Jay Are Collectively Known As Which Successful 80's Band?",Bucks Fizz,General,What was the name of the ruling house of France at the time of the French Revolution,Bourbon,Entertainment,Who advised us to 'break on through to the other side'?,Jim Morrison (of The Doors) +General,"In alphabet radio code, what word is used for 'h'",Hotel,General,What is the Capital of: Namibia,Windhoek,General,What does punch do for a living,Boxer +Music,"Who Had A Hit In 1988 With ""I Saw Him Standing There""",Tiffany,General,"Deadly nightshade, drug obtained from this",Belladonna,General,What is the name given to a loaf of bread which is made from two round lumps with the smaller on the top,Cottage loaf +General,Steps performed on the floor. It is the opposite of en l'air.,Par terre,General,An omniscient person has unlimited ______,Knowledge,General,What is measured on the Gay-Lussac scale,Alcohol strength +General,Where were Belgian waffles invented,Luxembourg,General,The Old Aztecs played ollamalitzi what game does it resemble,Basketball,General,What Egyptian word means life force was on title Wheatly novel,Ka +General,What word means to go to a party uninvited,Gatecrash,General,In which modern country is Mesopotamia,Iraq, Geography,What is the capital of Tonga ?,Nuku'alofa +General,"On ""Out of This World"" what planet was Troy from?",Anterrius,General,If you are using Prime Tierce and Octave what are you doing,Fencing, History & Holidays,Who had a 1970 No 1 with 'I Hear You Knockin'. '' ,Dave Edmunds  +General,What key is music written in if it has five flats,D flat,Sports & Leisure,The two tire manufacturers in F1 are Bridgestone and ________?,Michelin,General,In Arkansas it is illegal to keep what in your bathtub,Alligator +General,What famously happened on 7th December 1941,Japanese attack on pearl harbour,General,"In chess, what name is given to the only move involving two pieces",Castling,Science & Nature,Which fruit has the latin name of malus oumila ,Apple  +General,What do four quadrant make,A circle,General,Ops was the wife of which Roman God,Saturn,Food & Drink,What Is A Roux ,A Basic White Sauce  +General,The cast iron plant is another name for which pot plant,Aspidistra,General,Hormephobia is the fear of,Shock,General,For which country is the lotus flower the national symbol?,India +General,"These fighters always began a bout by saying, ""Hail Emperor, those about to die salute you!""?",Gladiators,General,What city do the Italians call the Monaco of bavaria?,Munich,General,"What are Cats tail, Cocks foot and Sheep's fescue",Types of grass +Science & Nature,Absolute zero (zero degrees kelvin) is only theoretical. The lowest laboratory temperature achieved is 280 picoKelvin. In which Scandinavian country was this produced?,Finland,General,It is the English Channel but what do the French call it,La Manche,General,What was the name of the exclusive dress store that the clampetts bought,The house of renee +Geography,"The Oregon Trail (1840_1860), the route used during the westward migrations of the United States, started in _____________ and ended in Oregon and was about 2,000 miles long.",Missouri,General,What does 'The Monument' in London commemorate?,Great Fire of London,General,Who is President of the European Central Bank,Wim duisenberg + History & Holidays,"If you attended a Swedish Christmas Party and were given 'Glogg'' what would you do with it, eat it, drink it or put it on your head ","Drink it, it's a hot spiced drink made from wine or spirit ",General,"What year was the film ""The Abyss"" released",1989,Food & Drink, Rum is made from this plant.,Sugar cane +Music,"Who Sang The James Bond Theme ""For Your Eyes Only""",Sheena Easton,General,Pepper's lonely hearts club band Energy waves produced by the oscillation or acceleration of an electric charge. Electromagnetic waves have both electric and magnetic components.,Electromagnetic radiation,Music,What Was Madonna's First Uk Number One Single,Get Into The Groove +Food & Drink,The Stomach Of A Cow Or Sheep Is Known As What ,Tripe ,Food & Drink, What type of food is pitta (pita)?,Bread,General,Who is the roman goddess of peace,Pax +Science & Nature,In Computing Especially Concering The Internet What Does The Acronym 'BLOG'' Actually Stand For ,Weblog ,General,In 1860 Napoleon III banquet - serving dishes dearer gold - what,Aluminium,General,Owls are the only birds that can see what colour,Blue +General,What literary prize ( worth £30000 ) is for women authors only,Orange Award,General,What is the first word sung in Queens Bohemian Rhapsody,Is ( Is this the real life),General,Into which bay does the ganges river flow,Bay of bengal +General,"Helsinki 52, Melbourne 56 , Rome 60 what comes next",Tokyo 64 - venues Olympics,General,What is the astrological sign for death,Pluto, History & Holidays,When The Conservative Party Were Known As Tories Who Were Their Chief Opponents? ,The Whigs  +General,How was the mausoleum at Halicarnassus destroyed,Earthquake,General,Which French revolutionary said 'The surest way to remain poor is to be honest',Napoleon bonaparte, Geography,What is the basic unit of currency for United Arab Emirates ?,Dirham +General,In Hindu mythology Meru is equal to what Greek site,Olympus,General,What is the longest canal in the world,Grand canal of china,General,Which country in Africa has the largest area,Sudan +General,In 1988 there was a shortage of what exotic food fish,Swordfish,General,What was the working title of the TV series Dallas,Houston,Food & Drink,In An American Restaurant What Are Home Fries? ,Potatoes Cooked In Bacon Fat  +Music,Which Label Is Associated With A Palm Tree Logo,Island,General,What is the condensed water vapour in the sky left behind jets,Contrail,Music,Who Has The Christian Names Christina Ciminella,Wynonna Judd +General,What Texan slammed back more bourbon and branch water than any character in TV history,J. r. ewing,General,What is a sixty year anniversary,Emerald anniversary,General,If you suffered from acronyx what have you got,Ingrown Toenail +General,In to what substance is barley converted before it can be made into beer,Malt,General,George Formby ” Singer Of The Classic “ When I'm Cleaning Windows ” But Can You Tell Me Where In The North West Of England Was He Born?,Wiigan,General,Who was the first Roman Emperor,Augustus +General,In 1839 what innovation was added to bicycles,Pedals,General,What country has the highest kidnapping rate?,Colombia,Science & Nature," One in ten Dalmatians is born __________, and the breed lacks the ability to process urine completely, so they need a special diet low in flesh protein.",Deaf +General,What is a group of bears,Sleuth,General,What is the name of the cold Spanish soup made from peppers and tomatoes,Gazpacho,General,The drug digitalis is obtained from which plant,Foxglove +Music,Which Reclusive Glaswegian Band Were Unexpectedly Voted Best Newcomers At The 1999 Brit Awards,Belle & Sebastian,General,What opera premiered in Paignton Devon 30th December 1879,Pirates of Penzance,Music,What Was Aha's First Album Called,Hunting High And Low +General,Who was the shortest ever mature human,Gul Mohammed,General,"Which capital city was scheduled as second choice to hold the 1944 Summer Olympics, and had to wait for another twelve years",Helsinki,General,In ancient Greece what was a hoplite,Soldier +General,Who captained the English soccer team during the 1966 World Cup,Bobby moore,Music,Who Gave Us The Story Of The Blues,The Mighty Wah,General,He developed the theory of the 'collective unconscious' and was also interested in dream interpretation.,Carl Gustav Jung +General,Testophobia is a fear of ______,Tests,General,Americans spent over how much in 1982 to avoid having bad breath,$360 million,General,With what is rainfall measured,Ombrometer +Geography,Nassau is the capital of which country,Bahamas,General,What is kaolin?,Pure china clay,Technology & Video Games,"In what game do you collect Jiggies, Jinjos, and Feathers (amongst other things)? ",Banjo-Kazooie +General,The Alley Cats was the working title of which TV show,Charlie's Angels,General,Which Fictional Character Opened The New York Stock Exchange On June 8th 1999,Noddy,General,Which country left the Commonwealth in 1949 and has not rejoined,Eire +General,"Who was the only president born in Illinois, the land of lincoln",Ronald reagan, Geography,What is the capital of Suriname ?,Paramaribo,General,What instrument of execution was used on Marie Antoinette,Guillotine +General,Who was the first south african golfer to win the british open,Bobby locke,General,What didn't wolfman jack do until the 1970's,Reveal his face,General,Who was the Greek goddess of the rainbow,Iris +General,On TV who worked at Otto's Auto Orphanage,The Fonz,General,What was David Leans first film,Oliver Twist,General,When was the leaning Tower of Pisa completed,1350 +Science & Nature, The flying __________ of Java and Malaysia is able to flatten itself out like a ribbon and sail like a glider from tree to tree.,Snake,Food & Drink,What kind of nuts are used in marzipan?,Almonds,General,"What are Luster, Moreen, Mungo and Nankeen types of",Material or fabric +General,What fictional doctor employed a butler named Poole,Dr Jekyll,General,What is the study of prehistoric plants and animals called?,Paleontology,General,What Is the First Name Of Parker In The TV Show Thunderbirds,Alo +General,Collective Nouns - a Cast of what,Falcons,General,Houari Boumedienne was president of which country from 1965 to 1978,Algeria,General,What us city briefly dropped the last letter of its name,Pittsburgh +General,47 people worked on a committee to produce what work,Authorised version of Bible,General,What is the most tattooed product in the world,Harley Davidson,General,When was the Alaskan Highway officially opened,"November 21, 1942 11/21/42" +Art & Literature,"In what field of study would you find ""flying buttresses""",Architecture,General,Dry ice consists of,Carbon dioxide,General,Whose recent books include 'Crisis Four' and 'Firewall',Andy mcnab +Art & Literature,What Nationality Was Jospeh Conrad ,Polish ,General,What is the flower that stands for: silliness,Fool's parsley,General,What dinosaurs name translates as roof lizard,Stegosaurus +Art & Literature,Who Wrote The Play (The Mousetrap) ,Agatha Christie ,General,What country do Brazil nuts come from,Bolivia,General,Trees: which tree has catkins in the spring and edible nuts in the autumn,Hazel +Toys & Games,Which chess piece is usually valued as 5 points,Rook,People & Places,"Who Was Banned In Germany, Italy & The USSR In The 1930's ",Mickey Mouse ,General,The Sailor Who Fell From Grace With the Sea_,Yukio mishima +General,What describes one complete turn of a rotating object,Revolution,General,"In egyptian mythology, what is the life force called",Ka, History & Holidays,Who died three days after Elvis Presley?,Groucho Marx +General,Spanish restaurant if you ask for La Quenta what do you get,The Bill,Science & Nature, Male __________ may have more than 100 wives and sometimes go three months without eating.,Sea lion, History & Holidays,Where was the big British pop festival held in 1969? ,The isle of wight  +General,Who composed Boris Godunov,Mussorgsky,General,Who wore a cabbage leaf under his cap,Babe ruth,Entertainment,"The director of Jaws, Raiders of the Lost Ark",Stephen spielberg +General,Who started the Dragonlance series,Margaret weiss & tracy hickman,General,RL international car registration plate which country,Lebanon,General,In 1965 which song by sam the sham and the pharaohs peaked at number 2,Woolly bully +General,Who invented the telephone,Alexander graham bell,Music,"What Was On The Flip Side Of The 1991 Re-Issue Of ""Bohemian Rhapsody""",These Are The Days Of Our Lives,General,Two in every three buyers pays the sticker price for what item without arguing,Automobile +General,What is Lolita's surname in Vladimere Nabokovs novel,Haze,Music,What band did Dion form in 1958?,The Belmonts,General,Name the Capital of the Ukraine,Kiev +General,Hypnophobia is a fear of ______,Sleep,General,In London who designed Marble Arch,John nash,Geography,In which state is the painted desert ,Arizona  +General,In which film did Robert Shaw play Captain Quint,Jaws,General,The average West German does it every seven days - what,Changes washes underwear,General,In London in 1985 a man was convicted of stealing two what,Crime Prevention Posters +General,Who wrote the novel The French Lieutenants Woman,John Fowles,General,What Every Day Invention Was Patended By Mary Anderson,Windscreen Wipers,General,Who was the Scottish mathematician who drew up the first logarithmic tables,John napier +Sports & Leisure,Which Country Hosted The 1994 World Cup? ,USA ,General,What do Italians call Rome,Roma,General,What is the fear of numbers known as,Numerophobia +General,Walt Disney had an obsessive compulsion with what,Cleanliness - washed hands every 5 min,General,What U.S. city contains a full-scale replica of the ancient Parthenon,Nashville,General,In 1782 in France what was louison or louisette,The Guillotine before renaming it +General,What is the scapula,Breastbone,General,Which nerve forms the link between the eye and the brain?,Optic Nerve,General,What was Oscar Wilde's only novel,The picture of Dorian Grey +General,"What can be rigid, semi-rigid, or non-rigid",Airships,General,Collective nouns - a group of police officers,A Mess, History & Holidays,"To which island was Napoleon exiled, after his loss at Waterloo ?",St Helena +General,What grape variety is used to make champagne,Chardonnay,Art & Literature,A large painting or decoration done on a wall. ,Mural,General,Oryza sativa is what staple food item,Rice +Food & Drink,"The dessert Pavlova is made from fresh fruit, cream and which other ingredient? ",Meringue ,General,What do airplane mechanics call motor oil,Pickle juice,Entertainment,"Besides ""Auld Lang Syne"" and ""For He's a Jolly Good Fellow"", what is the most frequently sung song in English?",Happy Birthday +Science & Nature,Whose Son Flew Too Close To The Sun On Waxen Wings ,Daedalus's ,Entertainment,"Gadzookie has a large, green friend. Who is he",Godzilla,General,What are 'Finnan haddies',Smoked haddock +General,In the acronym LASER what does the 'E' represent,Emission,General,Something free of charge,Buckshee,General,What is a group of donkeys called,Herd +General,What was the board game introduced in the eighties which featured six categories of questions and little pie shaped pieces you had to collect?,Trivia Pursuit,General,In 1960 Dr Thomas Creighton Was The First Person In The World To Receive What Punishment?,A Parking Ticket, History & Holidays,What show was 'Whatcha talkin' 'bout Willis?' a standard catch phrase? ,Different Strokes  + History & Holidays,Who Released The 70's Album Entitled Grievous Angel ,Gram Parsons ,General,In what state did the 1862 Sioux uprising start,Minnesota,Food & Drink,What does the HP stand for in HP sauce? ,Houses Of Parliament  +Geography,Which Is Europe's Largest Country After Russia? ,Ukraine ,Science & Nature," Some more names for groups of animals___ a bale of turtles, a clowder of cats, a charm of goldfinches, a gam of whales, a knot of toads, a __________ of tigers.",Streak,Music,In Which Year Did Kraftwerk Achieve Their First UK No.1,1982 +General,Who lives at the Neverland Valley ranch,Michael jackson,General,Who rode a horse called Lamri,King Arthur,General,With what sport is chris boardman associated,Cycling + History & Holidays,Which British city was named Deva by the Romans? ,Chester ,General,What was the name of Buffy's doll in the 1970's show 'Family Affair'?,Mrs. Beasley,Music,The Lovin Spoonful Were Cats From Which Town In 1967,Nashville +General,In what city was the first playboy club opened in 1960,Chicago,Entertainment,What was Jethro Tull before donating his name to a British epic rock group?,Agriculturist,Entertainment,Secret Identities: Kay Challis,Crazy jane +Sports & Leisure,What is the maximum number of clubs a golfer may use in a round?,Fourteen,General,Who invented the zip fastener in 1893,Whitcomb judson,General,If you suffered Hierophillia what turns you on,Sacred Objects +Music,"What Group Consisted Of Edele Lynch, Keavy Lynch, Sinead O Carroll, Lindsay Armaou",Bewitched,Science & Nature,Name the largest web_footed bird.,Albatross,General,Who is the Greek God of the sky and the universe,Uranus +General,"Whose hit ""We Are Family"" became an anthem for gay rights",Sister sledge,General,Who wrote Gone with the Wind,Margaret Mitchell,General,Name of Honda's variable valve timing system.,Vtec +General,What's the largest meseum in the world,Louvre,General,Indian song withimprovised usually topical words,Calypso,General,In Malibu California its illegal to do what in a theatre,Laugh out loud +Geography,What is the capital of Somalia,Mogadishu,General,What is the state bird of West Virginia,Cardinal,Art & Literature,Who Wrote (Old Possums Book Of Practical Cats) ,T.S. Eliot  +General,What animal lives in a drey,Squirrel,General,"Countries of the world:northern part of central American Isthmus, major cities include Quezaltenango & Escuintla",Guatemala,General,"In 'romeo and juliet', who said 'i have a faint cold fear thrills through my veins'",Juliet +General,What sport do the Kansas City monarchs participate in,Baseball,Music,Hugh Gregg Is The Real Name Of Which Singer,Huey Lewis,General,What is the only French speaking republic in Americas,Haiti +General,In Japanese It Is Known As “Leisure Time” What Do The Rest Of The World Know It As?,Kung Fu,General,What drink is a mixture of white wine and blackcurrant syrup or liqueur,Kir,General,"Ingvar Kamprad Is One Of The The Richest Men In The World, With What Brand Name Is He Connected?",Ikea +General,In 1937 the BBC first televised which sporting event,Wimbledon,General,Name the clothing wrinkle remover that sounds like a kind of metal.,Iron,Sports & Leisure,Which country always leads the opening Olympic procession?,Greece +General,With what invention is the name James Oughtred associated,Slide rule,General,"What do the ships resemble on the cover of the ""Boston"" LP in 1976",Guitars,General,British sailors got lime juice US cranberry what Danish get,Sauerkraut to stop scurvy +General,73% of what is produced and consumed in the USA,Internet Pornography,General,"Which Multi Platinum Singer Is The ""Niece"" Of Soul Singer ""Gladys Night""",Aaliyah,General,Who sang the song from the Disney movie 'Can You Feel The Love Tonight,Elton john +Sports & Leisure,Which Sport Is Played With The Heaviest Ball ,(Ten Pin Bowling) ,Religion & Mythology,"In Greek mythology, who was Jason's wife?",Medea,General,Adolf hitler became chancellor of Germany in which year?,1933 +General,Unimak is the largest island in which group,Aleutians,General,What was the real name of the Jazz legend Bix Beiderbecke?,Leon,General,"Snow Crash: Name the hacker who is infected with Snow Crash, owner of The Black Sun:",Da5id +General,Its cold enough to freeze balls off a Brass Monkey - what is it,Stack brass cannonballs,General,What do cricket umpires take off to show that it is time for lunch,Bails,General,"What city did the ""motley crue"" form in",Hollywood +General,One of the worst hotel fires in history erupts at the ___ in san juan,Dupont,Science & Nature, A group of owls is called a __________,Parliament,Sports & Leisure,Which position is usually played by the tallest member on a basketball team?,Centre +General,What did Oliver Pollock invent in 1778,The dollar sign $,General,In Kiplings Jungle Book Mang was what type of creature,Bat,General,What was the name of the neighbors that lived next door to ALF??,Raquel and Trevor Achmanic +General,Who wrote the novel The African Queen,C S Forester,General,From which plant family do vanilla pods come,Orchid orchidaceae,Entertainment,Which band does Eddie Vedder with?,Pearl Jam +General,Sol Sacs created what classic US TV show of the 60s 70s,Bewitched,General,What was Sheena Easton's original name,Shenne Ore,General,45 is the International Telephone dialling code for what country,Denmark +General,What is the currency of venezuela,Bolivar,General,"In Greek mythology, who was heracles' mother",Alcemene,Entertainment,Which basketball star played a genie in 'Kazaam'?,Shaquille O'Neal +General,"In 'romeo and juliet', about what was mercutio's long monologue",Queen mab,Geography,The Great Pyramid Presides Over Which Plateau On The Outskirts Of Cairo ,Giza ,General,HG Wells invisible man had what physical oddity,Albino +General,A Librocuricularist does what in bed,Read,Food & Drink,In 1989 in which brand of confectionary did blue replace brown? ,Smarties ,General,Jean Francois Gravelet is better remembered as who,Blondin Tightrope walker +General,In Australian slang what are apricots,Testicles,General,What was the name of Roy Rogers dog (now stuffed),Bullet,General,What is a guanaco,Wild llama +General,"What Edible Object Comes From The Latin Language Meaning ""Large Pearl""",An Onion,General,Where are American presidents carved in stone,Mount rushmore,General,Alectryomanchy is another name for what banned UK 1849,Cockfighting +General,Where is roast camel usually served,Bedouin feasts,General,A Greek or Roman two handled jar,Amphora,General,"Which creature was half horse, half man",Centaur +General,What do lentic fish like that lotic fish don’t,Lentic still waters lotic flowing,General,What are Russian astronauts called ?,Cosmonauts,General,What are hills and ridges composed of drifting sand,Dunes +Geography,In which country is the Calabria region,Italy,General,In the 19th century what was known as inheritance powder,Arsenic – as poison,General,Diana ross sings 'noone knows what goes on behind ______',Closed doors +Geography,What is the capital of Oman,Muscat,Entertainment,Famous Phrases: Who knows? The ______.,Shadow,General,Tanzania is the country that resulted from the union of tanganyika and ______,Zanzibar +General,What is the name of Snoopy's sister,Belle,General,On what tv show was 'the ponderosa',Bonanza,Sports & Leisure,What Sport Is Carried Out at Bisley In Surrey ,Rifle Shooting  +General,"Which famous opera house in milan, italy rose to its greatest heights under toscanini",La scala,General,What is the most eaten food in the US,Milk and Cream,Art & Literature,Who wrote the book (Brave New World) ,Aldous Huxley  +General,"What did Anna Sage ""The lady in Red do""",Betray John Dillinger,General,"What was Verdi's last Opera, first performed in 1893",Falstaff,General,What was John Denvers only solo UK number one,Annies Song +General,What Do You Dislike If You Suffer From Cusaphobia,Bad Language / Swearing,General,What is a king of egypt,Pharaoh,General,Who was the male lead in 'hello dolly',Walter matthau +General,What movie starred Lee Marvin as twins Kid Shelleen & Tim Strawn,Cat ballou,Science & Nature,"What Bird , Sometimes Called The Peewit, Has Dark Green Plumage With White Neck & Underside & A Distinctive Green Crest ",The Lapwing ,General,What Nationality Was Madame Tussard Founder Of The Famous Waxwork Exhibition,Swiss +General,Who played the title role in the 1921 film 'The Sheik',Rudolf valentino, Geography,What is the capital of Turkmenistan ?,Ashgabat,Science & Nature," Dogs that do not tolerate small children well are the St. Bernard, the Old English sheep dog, the Alaskan malamute, the bull terrier, and the toy __________",Poodle +General,Ted Hughes poet laureate died in which year,1998,General,What food are astronauts prohibited before a mission,Beans - Farts damage spacesuits,General,"The science of providing men, equipment and supplies for military operations is called ________.",Logistics +General,S. American rodent with soft grey fur,Chinchilla,People & Places,What Was Air Chief Marshall Arthur Harris Called ,Bomber Harris ,General,Who was would-be presidential assassin john hinckley jr was infatuated with,Jodie foster +General,What's the psychological term for self-love,Narcissism,General,What was robert montgomery's profession,Actor,General,What is the name of the UK's primary Fighter Aircraft?,Eurofighter Typhoon +General,In Somalia its illegal to carry old chewing gum where,Stuck on your nose,General,Which English agriculturist invented the seed drill,Jethro tull,Art & Literature,H.G. Wells novel where Earth is invaded by Martians.,War of the worlds +Sports & Leisure,How Many Numbers Are There On A Roulette Wheel ,37 - (0-36) ,Sports & Leisure,If A Cricket Umpire Raises His Arms Above His Head How Many Runs Is He Signalling ,6 Runs , History & Holidays,"Who Was President Of The USA In 1952 A) Harry Truman, B) Dwight Eisenhower, C= Lyndon Johnson ",A= Harry Truman  +Science & Nature,"What is the most venomous snake (no, it's not the king cobra!)?",Inland Taipan,General,What is the flower that stands for: a smile,Sweet william,General,What do Moscow residents substitute for toilet paper during shortages,Newspaper + History & Holidays,Johnny Depp Made His Movie Debut In Which Scary Movie ,A Nightmare on Elm Street , History & Holidays,Which actor of the silent era was known as The Man of a Thousand Faces ,Lon Chaney , History & Holidays,The 14th July 1789 marked the start of what ?,French Revolution +General,What shape is something that is cuneiform,Wedge shaped,Entertainment,Who was the Indian maiden in Johnny Preston's 'Running Bear'?,Little White Dove,General,Choo rock groups: frankie lymon and the _____,Teenagers + Geography,What is the basic unit of currency for Venezuela ?,Bolivar,General,Whose horse was Black Nell,Wild Bill Hickoks,General,Lee Marvin won the best actor Oscar for what 1965 film,Cat Ballou +General,Who was the lead singer of the Cure?,Robert Smith,Science & Nature,Linseed oil is obtained from the seed of which plant?,Flax,Science & Nature,What name is given to the point where two rivers join ?,Confluence +General,What is the flower that stands for: a belle,Orchis,General,A card game similar to rummy,Canasta,General,Pernell Roberts played which character in a TV western series,Adam Cartwright +General,What name is given to farms which specialise in rearing animals ?,Pastoral,General,Which book of The Bible is also a title of a Bob Marley album,Exodus,Science & Nature, Clams have a row of __________ around their shells.,Eyes +Food & Drink,Who released the following 'edible' album 'Tupelo honey' ,Van Morrison ,Sports & Leisure,In 1981 Sue Brown became the first woman to compete in which annual sporting event ,The Boat Race ,Music,Which Singer Is Known As The Man In Black,Johnny Cash +Geography,What European country did Romans call Lusitania? ,Portugal ,General,And what's their greatest turn off,Egoists and liars,General,"What was Rosanna Arquette's character's name in ""Desperately Seeking Susan""?",Roberta Glass +Science & Nature,Which meteor shower occurs on the 14th November ?,Andromedids,General,Where were the 1952 Olympics held,Helsinki,General,Which scientist invented the Universal Joint which enabled movement in all directions?,Robert Hooke +General,What is the young of this animal called: Duck,Duckling,General,"Triple star system, also called rigil kent, in the constellation Centaurus",Alpha centauri,General,What Italian stew literally translates as Bone with a Hole,Osso Bucco + Geography,What is the basic unit of currency for Estonia ?,Kroon,General,Which was the first James Bond book written by Ian Fleming in 1953,Casino royale,General,Who is the Roman Goddess of orchards and gardens,Pomonia +General,Where was Rin Tin Tin set,Fort Apache - Arizona,General,What does a funambulist do,Tightrope walker,General,What are the names of the Ninja Turtles in alphabetical order?,Donatello Leonardo Michelangelo Raphael +General,What was the name of Bob Crosby's band in the 1940's,The bobcat5,General,Which country is the biggest consumer of wine,France,Science & Nature, The process of a snake shedding its skin or a crustacean casting off its outer shell is called __________,Ecdysis +Geography,What is the capital of Uganda,Kampala,General,What was the name of Norman Beaton's barber's shop which was also the title of the TV series,Desmond's,General,If you were in a bar and someone gave you a 'mickey Finn' what would you be drinking?,A drugged Drink +General,A newborn bactrian camel has how many humps,None,Music,Which American City Is Associated With Motown,Detroit,General,What race in the sport called coursing,Greyhound +General,To which god was mount Parnasus sacred,Apollo,General,What is the Capital of: Algeria,Algiers,General,What US president was nicknamed Miss Nancy,James Buchanan +General,Saint Louis police department first used it in 1904 - what,Fingerprinting,General,Crosby stills and nash's debut album included which about a girl and the colour of her eyes,Sweet judy blue eyes, Geography,What is the capital of Peru ?,Lima +Science & Nature, __________ that are seen wandering around in the wild do not make good pets. These are sexually mature males at the end of their life cycle _ they will die within a few weeks or months.,Tarantulas,General,"Who played saxophone on ""The Girl From Ipanema""",Stan getz,General,Which film was based on the story of 'Francis Swamp Mario'?,The Patriot +Food & Drink,What are sometimes called Chinese Gooseberries?' ,Kiwi Fruit ,General,For what does i.f.p stand,Inkatha freedom party,Sports & Leisure,Who is known as Pistol Pete? ,Pete Sampras  +General,Which film actor was nicknamed 'The Duke',John wayne,General,What capitol is on the slopes of the volcano Pichincha,Quito Ecuador,General,CH car international registration plate which country,Switzerland +General,By what country was the first concentration camp setup during the Boer wars,England,General, What is measured by a chronometer,Time, History & Holidays,On What Date Is Saint Stephens Day Celebrated ,26th Dec  +General,What country owns the island of sicily,Italy,General,What year did coca-cola first go on sale,1886,General,What plant was used as a model for corinthian columns,Acanthus +General,How many sides does a dodecagon have,12,General,Dick Turpin the highwayman served and apprenticeship as what,Butcher,Music,Who Titled His 1977 Top Ten Album After The 2nd Book In The Bible,Bob Marley +General,What is the derivation of the word vanilla,From Latin Vagina resembled pod,General,Englands Stonehenge is how many years older than Rome's colosseum,"1,500 years",General,What French emperor died in Kent,Napoleon iii +General,"What year was the movie ""The Love Bug"" released",1969,General,Borsch is based on what vegetable,Beetroot,General,What animal lives in a lodge,Beaver +General,How did the bum convince the family dog to start eating again in Down and Out in Beverly Hills?,He ate the dog's food,General,Where would you find a gambrel,On a horse it’s a horses leg,General,Declan McManus became famous as who,Elvis Costello +General,What is a shallow dish with a cover that is used for science specimens,Petri, History & Holidays,What famous protest took place in Britain in 1936? ,The Jarrow March ,General,Around the alter of which God were the early Greek plays done,Dionysus +Entertainment,Peter Sellers is best known for his role as Inspector _________.,Clouseau,Religion & Mythology,"In Greek mythology, how many heads did Hydra have?",Nine,Music,"Where Did Scott McKenzie Visit, And What He Tell Us To Make Sure We Wore",San Francisco (Some Flowers In Your Hair) + Geography,"This mountain, found in the California Cascades, is famous for it's twin peaks?",Mount Shasta,Science & Nature,What Name Is Given To A Creature Equally At Home On Land And In Water? ,Amphibian ,Sports & Leisure,How Many Pips On A Standard Set Of Dominoes ,168 Pips  +General,What fruits do Elephants eat to get pissed ( ferment inside ),Marula,General,What did alchemists seek to turn base metals into gold,Philosophers Stone,Music,Xylophone Is Derived From The Greek Words For What,Wood & Sound +General,Between 1659 and 1681 illegal celebrate what in Massachusetts,Christmas,General,What Utah town plays host to the Sundance Film Festival,Park city,General,What would you have if you had rutilism,Red hair + History & Holidays,Which woman flew from England to Australia in 1930? ,Amy Johnson ,Geography,In which country is the Machu Picchu,Peru,General,Who was the first actor to receive an Oscar postumously,Peter finch +General,What ocean contains the Cape Verde Basin,Atlantic,General,"Who first incorporated amorphous phosphorus in the manufacture and use of safety matches, by 1855",Lundstrom brothers,Music,Which Band Have An Album At Number One At The Moment Called “We are The Night”?,Chemical Brothers +General,Who received the nobel peace prize in 1964 for civil rights leadership,Martin luther king jr,Geography,What is the capital of Madagascar,Antananarivo,General,Which state had the largest population increase between 1847 and 1860,California +General,Who wrote the 'dragonriders of pern' series,Anne mcaffrey, Geography,Which islands were named after Prince Philip of Spain?,The Philippines,General,What is the most popular pizza topping in South Korea,Tuna +General,What did Al Capone's business card say he was,Used furniture dealer,General,Which country has the currency 'yen',Japan,General,Ancient Egyptians worshiped what food item,Cabbage +General,What is a group of martins,Richness,Science & Nature,What Is Nitros Oxide Better Known As ,Laughing Gas ,General,James Dean Bradfield is the lead singer with which pop group,Manic street preachers +General,In Omaha Nebraska its illegal for a barber to shave what,A mans chest,General,Ophthalmophobia is a fear of ______,Being stared at,General,Who wrote Tom Jones,Henry fielding +General,What year was the first motorcycle race run,1907,General,What is the Texas Christians football team's nickname,Horned frogs,General,In heraldry if an animal is passant what is it doing,Walking +Music,"Who had a UK number three hit in 1980 with ""Take That Look Off Your Face""?",Marti Webb,Geography,"The Italian city of ___________, where Shakespeare's lovers Romeo and Juliet lived, receives about 1,000 letters addressed to Juliet every Valentine's Day.",Verona,General,Which Film Won The Category Of Best Film At The 2000 Oscars,Gladiator +Sports & Leisure,Where were the 1992 Olympics held ?,"Barcelona, Spain",General,In medicine what is nicknamed a blue pipe,A Vein,General,Which musical did the song Send in the Clowns come from,A Little Night Music +General,"Which Band Released The 1992 Album Entitled ""Automatic For The People""",REM,General,What is a Mexican Black Howler,A Monkey,Science & Nature,This Russian scientist used dogs to study conditioned reflexes.,Ivan Pavlov +General,Who were the guests on Johnny Carson's final tonite show,Bette midler and, Geography,What is the capital of Saint Kitts and Nevis ?,Basseterre,General,Grass from SE Asia yeilding a fragrant oil,Citronella +General,Who gave excalibur to king arthur,Lady of the lake,General,What did sailors often have tattooed on backs to stop flogging,A Cross,Food & Drink,What Happens If You Add Water To Pernod ,It Goes Cloudy  +General,What spice is essential in a New England Clam Chowder,Thyme,General,Worlds oldest existing treaty of 1373 between England and who,Portugal,General,What is the flower that stands for: anticipation,Gooseberry +General,What's round in London and Paris but Square in New York,Underground / metro / tube tunnels,Science & Nature,Who coined the theory that the earth revolves around the sun?,Nicolaus Copernicus,General,The French call it nature morte the Spanish bodegon what is it,Still Life painting +General,In which country did a foot deodorant get elected into office,Equator,General,What do you call an exaggerated fear of gravity,Barophobia,General,"Fill in the blank: swing low,sweet _____",Chariot +General,What happened on screen for the first time in India in 1977?,Screen kiss,People & Places,Who was known as the 'Maid Of Orleans'? ,Joan Of Arc ,General,What's the only alcoholic beverage on sale before noon in finland,Beer +General,In the language of flowers giving a mushroom meant what,Suspicion,Science & Nature,Which nation invented Paper ,China ,Music,About Which Jazz Player Was Ain't Misbehavin,Fats Waller +Science & Nature,"What is the name used to describe the ""minor planets""?",Asteroids,General,Tucson Arizona what's officially known as pavement deficiencies,Potholes in the road,General,Kan Pei - Terveydeksi - Op Je gazonheid 3 ways saying what,Good Health - Cheers +Food & Drink,In the drink Gin and 'It' - what's 'It'? ,Italian Vermouth ,General,What Nationality Was Ruby Murray,Irish,General,In Greek mythology whose dogs tore actaeon apart,Artemis +General,"What derogatory name is derived from a remark given by the Kaiser to members of the British Expeditionary Force under Sir John French, in 1914",Old contemptibles,Sports & Leisure,Name The Squash Shot Where The Ball Hits The Side Of The Wall First ,A Boast ,Science & Nature," In Korea, the deer is a symbol of long life, and is often portrayed in the company of __________",Immortals +General,Where is the world's biggest prison camp,Siberia, History & Holidays,Which English daily newspaper became a tabloid in 1971? ,The Daily Mail ,General,Who sang 'dreamy eyes' in 1962,Johnny tillotson + History & Holidays,Which Band Had A UK Christmas Number 1 With The Song 'Stay Another Day' ,East 17 ,General,Where does the spice saffron come from,The Crocus,Music,"What Was The Name Of Suzi Quatro's Character In ""Happy Days""",Leather Trocadero +General,Actor ______ Nimoy,Leonard,General,Name the capitol of Libya,Tripoli,Science & Nature," In one year, hens in America lay enough eggs to encircle the globe a __________",100 times +General,What is a group of sparrows,Host,Food & Drink,What is another name for the star fruit ,Carambula ,General,JFK was actually baptised JFFK what was the other F for,Francis +General,Who In The World Of Film Founded The Production Company Eon Productions,Albert R Broccoli (James Bond),General,What is the name of Jupiter's largest moon,Ganymede,Sports & Leisure,Which Britain Came Last In The 1988 Olympic Ski Jump Finals ,Eddie (The Eagle) Edwards  +General,Crusoe In what state is the Theodore Roosevelt national park,North dakota,General,Who did the norse god odin have as handmaidens,Valkyries,People & Places,In Which Us State Is Yosemite National Park ,California  +General,What is a word for a sorcerer who deals in black magic?,Necromancer,Music,Who Had The Best Selling Debut Album By A Female Artist In The 20 th Century,Whitney Houston,General,What kind of creature is a redpoll if it isn't a red cow,Bird +General,Which book of the bible tells of the death of moses,Deuteronomy,General,Where is a sumo wrestling tournament held,A Basho,General,Worldwide most capitol cities begin with which letter,B +Toys & Games,"In which sport or game are the terms: 'pin', 'fork', and 'skewer' used?",Chess,Geography,Which European City's Name Means Black Pool ,Dublin ,General,What is Gohan,Japanese Rice +Music,Who Played Che Guevara In The Film Version Of Evita,Antonio Banderas,Geography,What is the capital of Congo,Brazzaville,General,90% of New York cabbies are what,New immigrants +Geography,What's the former name of Istanbul,Constantinople,Science & Nature,What carries sensations from the tongue to the brain?,Lingual nerve,General,Who was the roman god of war,Mars +Music,"Who Had A Hit With ""Summer Night City"" In 1978",Abba,General,What part of the body is particularly affected by pneumonia,Lungs,General,Who eats the most turkey per capita,Israel +Sports & Leisure,Which Was The Only One Of The Four Tennis Grand Slam Tournaments That Pete Sampras Failed To Win In Men's Singles? ,The French Open , Geography,What is the basic unit of currency for Palau ?,Dollar,General,Growing Up Skipper Mattel doll 1970 what happened arm turned,Breasts Grew +General,"Which duo's best-selling album, The Innocents, topped the album charts in January 1989",Erasure,General,What muscles move the ears,Auricularis,General,A cigarette brand that fits these blanks; _&_,Benson & hedges +General,What name is given to the warning issued by American policeman prior to an arrest being made?,Miranda Warning,General,Jodie Foster and Barbara Harris appeared in which Disney film,Freaky Friday, History & Holidays,He was the U.S. president during the Civil War.,Abraham Lincoln + History & Holidays,"Which vehicle designed by Ferdinand Porsche, went into mass production in 1936? ",The Volkswagen ,General,What is borscht,Soup,General,What does a brandophile collect?,Cigar bands +General,What does letaba mean,Sandy river,General,"Countries of the world:north western Africa, major cities include Casablanca & Marrakech",Morocco,Music,"What Band Comprised Pal Waaktaar, Mags Furuholmen, & Morten Harket",Aha + History & Holidays,Which country ruled Cambodia immediately before WWII?,France,General,Where did the story of Cinderella originate,China,General,"In the tv series 'the adventures of hercules', what is hercules' companion's name",Iolos +Food & Drink,What is the name given to the watery part of milk left after making cheese?,Whey,General,"Who painted ""The Naked Maja""",Goya,Music,The Ever Changing Latin Teen Group Menudo Saw One Of Their Old Boys Enjoy Worldwide Success In 1999 Who Is He,Rick Martin +General,"Insertion, Heap, Topological and Bubble types of which IT thing",Sorting methods in programming,Science & Nature,What Type Of Plant Is A Ladys Slipper ,An Orchid ,Science & Nature,This planet's diameter is most equal to that of the earth's.,Venus +General,What is the most filmed story,Dracula,General,"Who lives at 1 Snoopy Place in Santa Rosa, California",Charles schulz,General,A bird in the hand is worth ______,Two in the bush +General,By Law In Washington it is illegal to do what,Ride an ugly Horse,General,What is a bridge hand with no cards in one suit called,Void, History & Holidays,What contribution to our Christmas heritage did Ralph E. Morris make in 1895? ,Christmas Lights  +Music,"By What Name Is John Graham Mellor , Born In Ankara In 1952 Better Known",Joe Strumer,General,The boondocks J Worthington Foulfellow was the fox in what Disney cartoon,Pinocchio,General,Who plays Ali McBeal,Callista Flockhart +General,The study and exploration of caves is known as what,Speleology,General,What Was The First Single To Sell Over A Million Copies In The UK ?,Bill Haley (Rock Around The Clock),Technology & Video Games,Who manufactured the first home video game system? ,Magnavox +General,How many spices are mixed in allspice,None it’s single spice,Sports & Leisure,In Field Athletics What Is Thrown The Shortest Distance ,Shot ,General,He discovered phobos,Asaph hall +Sports & Leisure,The Melbourne Cup horse race is held on the first _______ in November.?,Tuesday,General,What is a wumph,Deep thud or noise,General,Dendrophobia is a morbid fear of what,Trees +General,Kakorraphiaphobia is the fear of,Failure,General,Who was the ruthless chief pig in Orwell's Animal Farm,Napoleon,Art & Literature,What series has Robert Jordan written ?,Wheel of Time +General,What do you add to a screwdriver to get a harvey wallbanger,Galliano,Geography,"Which American state's name is Spanish for ""warm"" or ""warm land""",California,General,What was Americas first organised sport,Horse Racing in 1664 +General,Long green fleshy fruit used in salads,Cucumber,Science & Nature,How many degrees does the earth rotate each hour?,Fifteen,General,"What's a ""banger"" in London",Sausage +General,What is the Tibetan Yab Yum,A sexual position,Music,Don Henley And Glen Frey Were Singers With Which Group,The Eagles,General,Who 'mixes it with love and makes the world go around',Candyman +General,Which insect is the symbol of female potency,The Honey Bee,General,Baron de Coubertin won Gold at the Olympics 1912 for what,Literature,General,How long is Camptown racetrack,Five miles +Art & Literature,What Shakespearean play features the line: A plague on both your houses,Romeo and juliet,General,Who won the Best Actor Oscar in 1984 for his role in Amadeus,F murray abraham,Geography,In which continent would you find the lena river ,Asia  +General,What Disney animated feature was the first with end credits,Alice in Wonderland,Music,"Who Is Generally Regarded As Producing The First Rock N Roll Single ""Rocket 88"" In 1950",The Kings Of Rhtym, Geography,What is the smallest state in the USA?,Rhode Island + Geography,Which country is known as the roof of the world?,Tibet,General,What is a group of sheep,Flock,General,A childs hat tied under the chin,Bonnet +Entertainment,Which Elton John song was re-recorded as a requiem for Lady Diana Spencer?,Candle In The Wind,General,What branch of mathematics was devised by Sir Isaac Newton,Calculus,General,Which Christian Feast is celebrated on 15th August,Assumption of virgin mary +Art & Literature,What is the art of tracing designs and making impressions of them called?,Lithography,General,In which novel would you find the characters 'Amelia Sedley' and 'Becky Sharp',Vanity fair, Geography,What is the largest island in the East Indies?,Borneo +General,With which instrument is Leon Goossens associated,Oboe,General,In the Flintstones in what county is Bedrock,Cobblestone County,General,Whose slogan was plop plop fizz fizz,Alka Seltzer + Geography,What is the capital of Australia?,Canberra,General,Whats the point value of the bullseye outer on a dartboard,25,Music,"Who Had A 1978 Hit With ""Heads Down No Nonsense Mindless Boogie""",Alberto Y Los Trios Paranois +General,How many seams are there on an american football,Four,General,Who created 'peter rabbit',Beatrix potter,General,What did Welch's grape juice become a favorite substitute for when the 18th amendment passed,Wine + History & Holidays,What was the name of Punky Brewster's dog ,Brandon ,Music,"Sisters Of Mercy Had A Hit With The Song ""this Corossion"" Or ""This Emotion""",This Corrosion,General,What is a Umiak,Eskimos large open skin boat +General,If you had rubella what would you have caught,German Measles,General,How often does a Hebdomadal Council meet,Weekly, History & Holidays,In which English town was William Shakespeare born?,Stratford-Upon-Avon +General,Which insect has the best eyesight,Dragonfly,General,What is a relationship between two different types of organisms living together for mutual benefit,Symbiosis,Geography,The largest island on the west coast of North America is,Vancouver +General,Does Barry Manilow know you raid his wardrobe?,Breakfast Club,General,What word is in 1200 different languages without changing,Amen,Entertainment,Who is James Bonds Recurring Foe?,Ernst Stavro Blofeld +General,What soup would be on the menu in poland or russia,Borscht,General,Where was El Greco born,Greece,General,Who read the original writing on the wall,Daniel - in the Bible +General,Where is the oldest known restaurant in the world,Madrid,General,What's dichloro diphenyl trichloro ethane better known as,Ddt,Geography,What mountains are located on the border of tennessee & north carolina ,Smokey mountains  + History & Holidays,In the world of music how is 'Jiles P Richardson'' more commonly known? ,The Big Bopper ,General,What is the capital of tahiti,Papeete, Geography,What is the basic unit of currency for Grenada ?,Dollar +General,Who was emil jellinek's daughter,Mercedes,Music,The 1960's Television Show Flying Colours Featured A Singer Named Gerry Dorsey Who Did He Become,Engelbert Humperdinck,General,What type of soup is a dubarry,Cream of cauliflower +General,Uther pendragon was the father of ______,King arthur,General,Whose tees are closer to the green in golf men's or women's,Women's,General,Of what was charlie chaplin's cane made,Bamboo +General,What is the name of the Nike sports logo,Swoosh,General,What is the hardest naturally occuring substance,Diamond,General,What does a cobbler mend,Shoes +General,In what Austrian city was Mozart born,Salzburg,General,How many colonies fought in the American war of Independence,Thirteen,General,Which fruit did Columbus discover in Guadalupe in 1493,Pineapple +General,Who Did Steve Davis Beat In The Final When He First Became World Snooker Champion,Doug Mountjoy,General,How many members were in the group Bread,Three,General,Which company built the first spiral escalator in the 1980's,Mitsubishi +General,"In Greek mythology, who had nine heads",Hydra,General,Mount Citlalteptl is the tallest mountain in which Latin country,Mexico,General,Who would use an opisometer,A cartographer to measure + History & Holidays,Who was the Taj Mahal built in memory of?,Mumtaj Mahal,General,A small narrow headband,Bandeau,General,"What television character was born july 1, 21 bc in pompeii",Jeannie +Science & Nature," According to several studies, less than 3 percent of the __________ population become man_eaters.",Tiger,General,Who Is Judith Sheindlin Better Known As,Judge Judy,Geography,Which is the Earth's smallest continent,Oceania +General,Fortymile creek was the location of what alaskan discovery in 1886,Gold,General,What was banned from New York schools in 1962,Reading of Prayers, History & Holidays,Which was the first Chinese dynasty?,Shang +General,"Which group sang the song ""I Have A Dream""?",Westlife,Food & Drink," What is Japanese ""sake"" made from",Rice,General,What was the name of Aristotle's school ?,Lyceum +General,"In a general sense, one who pleads for another in a court of law or other tribunal.",Advocate,General,Mark Chapman was carrying what book when he killed Lenon,Catcher in the Rye,General,In 1996 what was the most common use for a computer 46%,Bookkeeping invoicing WP 45% +Art & Literature,As what did H.G. Wells refer to Adolf Hitler?,A certifiable lunatic,General,In the USA what was the first prime time cartoon show,The Flintstones,Art & Literature,"A European style developed in France in the late eleventh century. Its sculpture is ornamental, stylized and complex. ",Romanesque +General,"Which singer got to number one over Christmas 1985 with ""Merry Christmas Everyone""",Shakin stevens,General,In which book is 'big brother',1984,Food & Drink,For Which Occasion Was Vichyssoise Soup Created ,"The Opening Of The Roof Garden At The Old Ritz Carlton Hotel, New York City " +General,Who appeared for the first time in Beetons Christmas Annual,Sherlock Holmes,General,What does a pomologist study,Fruit,General,"Robert Redford, Steve McQueen Paul Newman reject $4m role",Superman - C Reeve paid 200K +General,Saintpaulia is the botanical name for which houseplant,African violet,Sports & Leisure,In Boxing What Is A TKO? ,Technical Knock Out ,General,What is salicyclic acid better known as,Aspirin +General,Wild canine animal with red or grey fur,Fox,General,When was the polaroid land camera invented,1948,General,Who calculated the amount of energy that appears when matter disappears,Albert einstein +General,What is the fastest growing religion in Ireland,Buddhism,General,Brazzaville is the capital of ______,Congo,Music,"Whose 1998 Album ""When We Were The New Boys"" Included Covers Of Oasis ""Cigarettes & Alcohol"" & Primal Screams ""Rocks""",Rod Stewart +General,90 is the International Telephone dialling code what country,Turkey,General,Which English monarch was first to make Xmas day broadcast,George V,General,Whose designer leisure wear carry the symbol of a crocodile,Rene Lacoste +Art & Literature,"Referring to the principles of Greek and Roman art of antiquity with its emphasis on harmony, proportion, balance, and simplicity. In a general sense, it refers to art based on accepted standards of beauty.",Classicism,General,What country has the highest population density,Monaco,General,What was the first staple that the U.S. government rationed during World War II,Sugar + History & Holidays,Where does Santa Claus live? ,Lapland ,Music,How many miles high were the Byrds on their fifth single?,Eight,General,Ancient Romans dyed their hair with what waste product,Bird Shit +Sports & Leisure,On Which Race Course Is The Scottish Grand National Run ,Ayr ,General,Which German word means lightning war used in WW2,Blitzkrieg,General,What do the dots on a pair of dice add up to?,42 +Music,Their most famous single is (Don't Fear) The Reaper. Who are they?,Blue Oyster Cult,General,The plant Lunaria Annua is grown mainly for its seed cases and used in flower arranging by what name is it more commonly known,Honesty,General,In Star Trek Voyager what is the shuttlecrafts name,The Delta Flyer +Science & Nature,What Was The Significance Of The Number 40 In Relation To The Ford GT40 ,It Stood 40 Inches Tall ,General,On what was pennsylvania incorrectly spelled,Liberty bell,Music,"Who Recorded The Albums ""Fantastic, Make It Big, & Final""",Wham +General,Which Shakespeare character had a daughter called Jessica,Shylock,Sports & Leisure,What Caused The Cancellation Of The Cheltenham Festival In 2001? ,Foot & Mouth Disease ,General,"Dating back to the 1600s, thermometers were filled with what instead of mercury?",Brandy +Music,Who Was The Youngest Female Artists To Have A UK Million Selling Single?,Britney Spears,General,Lisa Kudrow plays which character in the television series Friends,Phoebe,Music,"Who Had A Hit In 1983 & 1989 With ""Cruel Summer""",Bananarama +Sports & Leisure,How many seams are there on a football? (American),Four,General,The first person other than royalty to be portrayed on a British stamp was?,William Shakespeare,General,What is the Capital of: French Guiana,Cayenne +General,Selenophobia is a fear of ______,Moon,General,What Was The First Country In The World To Make Seatbelts Compulsory,Czechoslovakia, History & Holidays,Who Released The 70's Album Entitled Reggatta de Blanc ,The Police  +General,Sister car of the Nissan Quest.,Mercury villager,General,A trained fighter in ancient Rome,Gladiator,General,In what sport is the term 'terminal speed',Drag racing +Music,What Does Adagio Mean,Slow & Leisurely,Music,"With Regeard To The Beatles, What Is The Significance Of The Date 6 July 1957",Day John Lennon Met Paul McCartney,General,A Draughts player starts with how many pieces?,12 +General,In Missouri it's illegal for anyone to do what on Sunday,Play Hopscotch,General,What is always served early in a formal Japanese meal,Sashimi - raw fish,General,In Thailand its illegal to step on what,Nation's Currency +Music,"The Musical Chess Released Several Singles , Which Was The Most Successful """,Murrayhead / One Night In Bangok,General,Who wrote the song 'There's no business like show business',Irving berlin,General,Jimmy Carter's family grew goober peas in Georgia. What's their more familiar name,Peanuts +General,You are a saucy boy comes from what Shakespeare play,Romeo and Juliet,General,What is the most common disease in the world,Tooth decay,General,What city was known as Christiana until 1925,Oslo - Sweden +Science & Nature, Catnip can affect lions and tigers as well as house cats. It excites them because it contains a chemical that resembles an excretion of the dominant female's __________,Urine,Sports & Leisure,"In baseball, who won their first world series in 1969?",New York Mets,General,A mature Japanese sea squirt eats what,It’s own Brain +General,Who said - Toe err is human - But it feels divine,Mae West,Music,Glen Frey & Bernie Leadon Are Original Mebers Of Which Group,The Eagles,General,Who was the president of the merchant bank of Beverly Hills,John cushing +General,Jacques Garnerin made the first in 1797 the first what,Parachute Jump,Science & Nature," Though human noses have an impressive 5 million olfactory cells with which to smell, sheepdogs have 220 million, enabling them to smell 44 times better than __________",Man,General,This type of insect is named after a month,June bug +General,A Welsh opera singer is particularly associated with which online company in a series of British TV advertisements?,Go Compare,General,Who played bobby ewing in the tv series 'dallas',Patrick duffy,Music,What Was The 1962 Instrmental No.1 For B Bumble And The Stingers,Nut Rocker +General,What did pennsylvania legalise before any other colony,Witchcraft,General,What is the oldest word in the English language,Town,General,Papaphobia is a fear of ______,Popes +Food & Drink,What Is The Most Common Pub Name ,The Red Lion ,General,Carl Switzer was the real name of which of the Our Gang characters,Alfalfa,General,In which television series do the characters Doctor Green and Doctor Lewis appear,E r +General,Wilco name the only animal with retractable horns,Snail,General,Which instrument did jazz musician Jack Teagarden principally play,Trombone,Geography,What Is An Anthracite ,A Type Of Coal  +General,Where are the grapes for the wine Lacrima Christi grown,Mount Vesuvious,Entertainment,How old was Leann Rhimes when she recorded her first album?,Eleven,General,Which is the largest city in India,Calcutta +General,What state officially recognized baked beans as the state food,Massachusetts,Religion & Mythology,How old was Sarah when she had a child?,90, History & Holidays,What was the name of the first nuclear powered submarine? ,Nautilus  +General,The polar explorer Sir Ranulph Fiennes has a triple barrelled surname. Give either of the other parts of his surname.,Twistleton wykeham,General,Which drink was advertised on TV by David Bowie's wife Iman,Tia maria,General,Tyrian Purple is a strong dye made from what,Mussels and Whelks +General,What is the traditional (watson crick) base pair for adenine in dna,Thymine,Geography,What language do the locals speak in Bogota? ,Spanish (Bogota is the capital of Columbia) ,General,Who wrote the utopian novel Woman on the Edge of Time,Marge piercy +Music,How Did The Blues Artist Muddy Waters Get His Name,As A Child He Fished & Played In Muddy Creak,General,The British army used to wear puttees - what's it literally mean,Bandages from Hindu,General,"What religion links Weasak, Dhrammacacka, and Bhodi day",Buddhist +General,What New York street is famous for its theatres,Broadway,Science & Nature," The __________ whale is often referred to as the ""sea canary"" because of the birdlike chirping sounds it makes.",Beluga,General,What Van Gogh painting did the Getty Museum pick up after Alan Bond was unable to pay the 53.9 million he agreed to pay Sotheby's,Irises +General,What Is Studied By A Speleologist,Caves,Music,Memory Almost Full Was A 2007 Album Released By Who?,Paul McCartney,General,What is the more common name for triatomic oxygen,Ozone +Geography,What is the worlds most southerly capital?* * ,"Wellington, New Zealand ",General,What modern word comes from the Latin Dilatare - open wide,Dildo,General,Which biblical prophet was sawn in half inside a hollow log,Isaiah +Music,What Is The Very First Word In The Paul Young Song “Land Of The Common People”,Living,General,What is a volcano that is neither active nor extinct?,Dormant,General,Which band comprises Shaznay and Melanie plus sisters Natalie and Nicole,All saints + History & Holidays,What city was john f. kennedy nominated for president in ,Los angeles ,General,"Who first said ""The Games Afoot""",William Shakespeare,Music,Which Bon Jovi album was best kept dry?,Slippery When Wet +Music,What Was The Monkees Debut Album Called,The Monkees,Geography,______________ was the U.S. Confederacy's largest city.,New orleans,Music,XYZ Was A Solo Album By Which Former Member Of The Police,Andy Summers +Art & Literature,Who is the protagonist of Milton's Paradise Lost?,Satan,General,What does a necrographer do,Writes Obituaries,General,"What was on the B side of the Beatles 1968 ""Hey Jude""",Revolution +General,What battle resulted in the largest number of german pow's?,Battle of stalingrad,Geography,______________ is the largest French_speaking city in the Western Hemisphere.,Montreal,General,Where in the body is the tiniest human muscle,Ear +General,What was on B side of The Rolling Stones Ruby Tuesday,Lets Spend the Night Together, Geography,What is the highest waterfall in the USA?,Yosemite,Sports & Leisure,How many games must you win to win a normal set in tennis,Six +General,From which country are the European Space Agency Ariane rockets launched,French guiana,Food & Drink,Company that brews Michelob beer,Anheuser_busch,General,Approximately how many pounds of cereal will the average american/canadian eat every year?,12 +General,What model of car did Starsky and Hutch drive?,Red Torino,Music,What Was At No.1 For Fifteen In 1994,Wet Wet Wet / Love Is All Around,General,Who was a member of 'the great society' before she joined 'jefferson airplane',Grace slick +General,The judds spent years in nashville shopping demos recorded on a ______,Cassette recorder,General,Which flag is raised when a ship is about to leave port?,Blue Peter,Music,Live And Dangerous Is A Classic Live Album By Which Hard Rock Band,Thin Lizzy +Tech & Video Games,What was the first version of Microsoft Windows?,Windows 286,General,What is the river capital of the world,Akron,General,The Character Mary Goodnight was the Bond girl in which film,The man with the golden gun +General,Where in the world is Radwick racecourse situated,Sydney Australia, History & Holidays,Who assassinated president Kennedy?,Lee Harvey Oswald,General,Long Legged Hannah and Marty's Express what pastime involved,Line Dancing +Science & Nature,Name the longest venomous snake.,Cobra,Science & Nature,What is the name of brightest asteroid visible from earth?,Vesta,General,Who sang about the fall of man in 'the tall oak tree',Dorsey burnette +General,"At 45, what did boxer, George Foreman win",Heavyweight championship,General,During what period in american history did thousands of people die or go blind from drinking bad liquor,Prohibition,General,U.S. captials Maine,Augusta +Tech & Video Games,Which company made the original 'Donkey Kong'? ,Nintendo,General,What wonder stood 32m high in rhodes harbour,Colossus of rhodes,Sports & Leisure,Which Team Knocked Out Liverpool In This Years Fa Cup ,Burnley  + History & Holidays,Which 'following' word did Frederico Fellini coin and use for the first time in his film La dolce vita ? ,Paparazzi , History & Holidays,If You Were Born On Christmas Day What Star Sign Would You Be? ,Capricorn ,General,Vienna is the setting for what Shakespeare play,Measure for Measure +General,Sidney Poitier became the first black actor to win a Best Actor Oscar when he got the award for which film,Lilies of the field,General,What sort of an animal is an anchovy,Fish,Geography,In what state is concord ,New hampshire  +General,What is the flower that stands for: distinction,Cardinal flower,General,What was the punishment in Ancient Rome for water pollution,100 lashes,General,What year was Aaron Copeland born,1900 +General,In what book was the final score 4-2 after casey struck out,Casey at the bat,Food & Drink,What country produces Wiibroe beer? ,Denmark ,Music,What Was Phyllis Nelson's Big UK Hit,Move Closer +Food & Drink, From which fish is caviar obtained,Sturgeon,General,An area seperating potential belligerents,Buffer zone,General,Nonage is what reason to stop a marriage,Underage one or both +General,Mary Isobel Catherine O'Brian born 1939 better known as who,Dusty Springfield, History & Holidays,Who was the first fully Danish king of England ?,Canute the Great,Music,Who Was To Blame According To Bewitched In 1999,The Weatherman +General,Who succeeded winston churchill as prime minister of england,Anthony eden,General,2 categories of ballroom dance are used in competition Latin and,Smooth,General,What is a high ranking chess player called,Grandmaster +General,Coolidge what heisman trophy winner returned his first nfl kickoff for a touchdown,Tim brown,General,"In which film starring Humphrey Bogart and set in Martinique, did he play a character called Harry Morgan",To have and have not,General,What has a rectangular pupil,Goat + Geography,In which country is Brest? (NOT Breast!),France,General,What is the u.s military's newspaper,Stars and stripes,General,What is the young of this animal called: Guinea fowl,Keet +General,Unfortunate names - Pansy was a brand of what sold in China,Mens Underwear,People & Places,Who Became Archbishop Of Caterbury in 1980 ,Robert Runcie ,People & Places,What Was Judge Jefferies Nickname ? ,The Hanging Judge  +General,"Name the British novelist, a successful member of the Bloomsbury Group, who drowned herself in 1942",Virginia woolf,General,Which group had the hit album 'Urban Hymns',The verve,General,"Which TV Quiz Show Has The Theme Music Entitled ""Approaching Menace""",Master Mind +Food & Drink,What Are The Ingredients Of A Harvey Wallbanger Cocktail ,"Vodka, Orange Juice, Galliano ",General,How many standard bottles of wine are there in a Jereboam,Four,General,What is measured in grains - four grains to a carat,Pearls +General,What is the first line on Mel Blanc's tombstone,That’s All Folks, History & Holidays,What Hugely Successful Chain Of Stores First Opened It's Doors In 1954 In California ,McDonalds ,General,"Which eighties cartoon ended with the phrase: ""And knowing is half the battle?""",G.I.Joe +General,In what language was the first complete bible in US printed,Algonquin Indian,General,Building occupied by monks,Abbey,General,Arias And Rasberries' Was An Autobiography Penned By Whom,Harry Secombe +General,Who has the most cars per mile of road,England,General,"During WW I, it was at this city & battle where the Germans introduced mustard gas.",Ypres,General,Who recorded their first song under the names Tom and Jerry,Simon and Garfunkle +Music,"Which Band Released The Album ""Dark Side Of The Moon"" In 1972",Medicine Head (Not Pink Floyd That Was L8r),Sports & Leisure,Who 2 Members Of The England Football Team Missed Penalties Against Portugal In Euro 2004? (PFE) ,David Beckham & Darius Vassell ,General,"With which band did Boy George sing, besides Culture Club",Bow wow wow +Science & Nature,What is the common name for the scapula?,Shoulder blade,General,Hanoi stands on the Song-Koi - what's it mean,Red River,General,Barrel sizes - there are 18 gallons in a what,Kilderkin +General,"Hockey compared to the earth, how much gravity does the moon have",One eighth,General,What do strikers call those that refuse to strike,Scabs,General,"What place was nicknamed ""The Pearl of the Orient""",Manilla - Philippines +General,What was banned by law 16th 17th century Venice,High heels – Prostitutes drowned,General,What did shirley temple always have in her hair,Curls,General,Which group's 1986 hit was 'Waiting for the Ghost Train',Madness + History & Holidays,Who Was The First Catholic President Of The United States ,Catholic F ,Music,"Which Group Took Their Version Of ""Help"" To No.3 In The Uk In 1989",Bananarama,General,When is the top layer of a wedding cake usually eaten,First anniversary +Sports & Leisure,"In which game or sport is a ""Zamboni"" used?",Hockey,General,Who is the dog on the crackerjack box?,Bingo,Science & Nature,Which Is The Smallest Mammal In Europe ,Pygmy Shrew  +General,What is the European equivalnet of a Nonillion ?,Quintillion,General,What name is given to an equilateral parallelogram which contains a right angle,A square,Sports & Leisure,In 1976 Sue Barker won her only Grand Slam Final. Which one? ,The French Open  +Science & Nature,Who Is Accepted As The Discoverer Of Penicillin ,Sir Alexander Fleming ,General,"A salad containing diced apple, celery, walnuts and mayonnaise is known as what",Waldorf salad,General,Who produced the Tom and Jerry cartoons until 1956,Fred Quimby +Music,What Was Lita Roza's 1953 Dog Related No1 Hit?,How Much Is That Doggie In The Window,General,For what is spirits of salt another name,Hydrochloric acid,Science & Nature,What Is Meant By A Low Wing Bearing ,A Low Body Weight To Wing Ratio +Science & Nature,What Part Of A Car Engine Mixes Fuel With Air? ,The Carburetor ,General,Pharmacophobia is the fear of,Taking medicine, Geography,In which country was the first Zoo ?,China +General,What is the largest landlocked country,Mongolia,General,Who was the only man to win non-consecutive terms as u.s president,Grover,General,In which us state is the painted desert located,Arizona +General,"Houston, Waco and Fort Worth are all in which state",Texas, Geography,What is the basic unit of currency for Mauritius ?,Rupee,Geography,"This is called the ""Honeymoon Capital"" of the world.",Niagara falls +Sports & Leisure,How Many clubs were there in the Premier League in 1997/8 ,20 ,General,What is the misshapen ear that boxers often have,Cauliflower ear,General,Who painted the ceiling of the sistine chapel,Michelangelo + History & Holidays,What city holds the distinction of opening the world's first public library in 1747?,"Warsaw, Poland",General,"Which band had a 1999 hit single with ""Flying Without Wings""",Westlife,General,Who is the first person a newly elected Pope meets,His tailor to measure him +General,A Pascal is the SI unit of what,Pressure,General,"Film - who was the star of ""a boy and his dog""",Don johnson,Music,"In The World Of Boybands How Are Danny, David, Anthony & James Better Known",Eton Road +General,What would you find in a vivarium,Snakes,General,Lusophone describes countries whose main language is what,Portuguese,General,What are male crabs known as,Jimmies +Food & Drink,What name is given to the red wines from the Bordeaux region? ,Claret ,General,Who was nicknames The desert Fox (both Names),Erwin Rommel,General,"In Greek mythology, who was minos' mother",Europa +General,James H Pierce was the last silent film actor to play who,Tarzan,General,Who was woden to the anglo-saxons,Chief god,General,Name the Greek dish using aubergines and minced lamb,Moussaka \ No newline at end of file diff --git a/TriviaBot/TriviaBot.cpp b/TriviaBot/TriviaBot.cpp new file mode 100644 index 0000000..584e12c --- /dev/null +++ b/TriviaBot/TriviaBot.cpp @@ -0,0 +1,5 @@ +#include + +int main(int argc, char* argv[]) { + +} \ No newline at end of file diff --git a/TriviaBot/TriviaBot.vcxproj b/TriviaBot/TriviaBot.vcxproj new file mode 100644 index 0000000..694fa4d --- /dev/null +++ b/TriviaBot/TriviaBot.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {E400396F-0C05-413E-B83E-1A4F41D86442} + TriviaBot + 8.1 + + + + Application + true + v140 + MultiByte + + + Application + false + v140 + true + MultiByte + + + Application + true + v140 + MultiByte + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3;$(IncludePath) + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3\x64;$(LibraryPath) + + + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3;$(IncludePath) + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3\x64;$(LibraryPath) + + + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3\x86;$(LibraryPath) + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3;$(IncludePath) + + + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3\x86;$(LibraryPath) + C:\Users\Jack\Documents\Visual Studio 2015\Projects\TriviaBot\lib\sqlite3;$(IncludePath) + + + + Level3 + Disabled + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + sqlite3.lib;%(AdditionalDependencies) + /SUBSYSTEM:CONSOLE %(AdditionalOptions) + + + + + Level4 + Disabled + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + sqlite3.lib;%(AdditionalDependencies) + /SUBSYSTEM:CONSOLE %(AdditionalOptions) + + + + + Level3 + MaxSpeed + true + true + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + true + sqlite3.lib;%(AdditionalDependencies) + /SUBSYSTEM:CONSOLE %(AdditionalOptions) + + + + + Level4 + MaxSpeed + true + true + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + true + sqlite3.lib;%(AdditionalDependencies) + /SUBSYSTEM:CONSOLE %(AdditionalOptions) + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TriviaBot/TriviaBot.vcxproj.filters b/TriviaBot/TriviaBot.vcxproj.filters new file mode 100644 index 0000000..b30b254 --- /dev/null +++ b/TriviaBot/TriviaBot.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/TriviaBot/test.db b/TriviaBot/test.db new file mode 100644 index 0000000..afa859e Binary files /dev/null and b/TriviaBot/test.db differ diff --git a/lib/sqlite3/shell.c b/lib/sqlite3/shell.c new file mode 100644 index 0000000..5148177 --- /dev/null +++ b/lib/sqlite3/shell.c @@ -0,0 +1,5590 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code to implement the "sqlite" command line +** utility for accessing SQLite databases. +*/ +#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) +/* This needs to come before any includes for MSVC compiler */ +#define _CRT_SECURE_NO_WARNINGS +#endif + +/* +** If requested, include the SQLite compiler options file for MSVC. +*/ +#if defined(INCLUDE_MSVC_H) +#include "msvc.h" +#endif + +/* +** No support for loadable extensions in VxWorks. +*/ +#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION +# define SQLITE_OMIT_LOAD_EXTENSION 1 +#endif + +/* +** Enable large-file support for fopen() and friends on unix. +*/ +#ifndef SQLITE_DISABLE_LFS +# define _LARGE_FILE 1 +# ifndef _FILE_OFFSET_BITS +# define _FILE_OFFSET_BITS 64 +# endif +# define _LARGEFILE_SOURCE 1 +#endif + +#include +#include +#include +#include +#include "sqlite3.h" +#if SQLITE_USER_AUTHENTICATION +# include "sqlite3userauth.h" +#endif +#include +#include + +#if !defined(_WIN32) && !defined(WIN32) +# include +# if !defined(__RTP__) && !defined(_WRS_KERNEL) +# include +# endif +# include +# include +#endif + +#if HAVE_READLINE +# include +# include +#endif + +#if HAVE_EDITLINE +# include +#endif + +#if HAVE_EDITLINE || HAVE_READLINE + +# define shell_add_history(X) add_history(X) +# define shell_read_history(X) read_history(X) +# define shell_write_history(X) write_history(X) +# define shell_stifle_history(X) stifle_history(X) +# define shell_readline(X) readline(X) + +#elif HAVE_LINENOISE + +# include "linenoise.h" +# define shell_add_history(X) linenoiseHistoryAdd(X) +# define shell_read_history(X) linenoiseHistoryLoad(X) +# define shell_write_history(X) linenoiseHistorySave(X) +# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) +# define shell_readline(X) linenoise(X) + +#else + +# define shell_read_history(X) +# define shell_write_history(X) +# define shell_stifle_history(X) + +# define SHELL_USE_LOCAL_GETLINE 1 +#endif + + +#if defined(_WIN32) || defined(WIN32) +# include +# include +# define isatty(h) _isatty(h) +# ifndef access +# define access(f,m) _access((f),(m)) +# endif +# undef popen +# define popen _popen +# undef pclose +# define pclose _pclose +#else + /* Make sure isatty() has a prototype. */ + extern int isatty(int); + +# if !defined(__RTP__) && !defined(_WRS_KERNEL) + /* popen and pclose are not C89 functions and so are + ** sometimes omitted from the header */ + extern FILE *popen(const char*,const char*); + extern int pclose(FILE*); +# else +# define SQLITE_OMIT_POPEN 1 +# endif +#endif + +#if defined(_WIN32_WCE) +/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() + * thus we always assume that we have a console. That can be + * overridden with the -batch command line option. + */ +#define isatty(x) 1 +#endif + +/* ctype macros that work with signed characters */ +#define IsSpace(X) isspace((unsigned char)X) +#define IsDigit(X) isdigit((unsigned char)X) +#define ToLower(X) (char)tolower((unsigned char)X) + +#if defined(_WIN32) || defined(WIN32) +#include + +/* string conversion routines only needed on Win32 */ +extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); +extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); +extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); +#endif + +/* On Windows, we normally run with output mode of TEXT so that \n characters +** are automatically translated into \r\n. However, this behavior needs +** to be disabled in some cases (ex: when generating CSV output and when +** rendering quoted strings that contain \n characters). The following +** routines take care of that. +*/ +#if defined(_WIN32) || defined(WIN32) +static void setBinaryMode(FILE *file, int isOutput){ + if( isOutput ) fflush(file); + _setmode(_fileno(file), _O_BINARY); +} +static void setTextMode(FILE *file, int isOutput){ + if( isOutput ) fflush(file); + _setmode(_fileno(file), _O_TEXT); +} +#else +# define setBinaryMode(X,Y) +# define setTextMode(X,Y) +#endif + + +/* True if the timer is enabled */ +static int enableTimer = 0; + +/* Return the current wall-clock time */ +static sqlite3_int64 timeOfDay(void){ + static sqlite3_vfs *clockVfs = 0; + sqlite3_int64 t; + if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); + if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ + clockVfs->xCurrentTimeInt64(clockVfs, &t); + }else{ + double r; + clockVfs->xCurrentTime(clockVfs, &r); + t = (sqlite3_int64)(r*86400000.0); + } + return t; +} + +#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) +#include +#include + +/* VxWorks does not support getrusage() as far as we can determine */ +#if defined(_WRS_KERNEL) || defined(__RTP__) +struct rusage { + struct timeval ru_utime; /* user CPU time used */ + struct timeval ru_stime; /* system CPU time used */ +}; +#define getrusage(A,B) memset(B,0,sizeof(*B)) +#endif + +/* Saved resource information for the beginning of an operation */ +static struct rusage sBegin; /* CPU time at start */ +static sqlite3_int64 iBegin; /* Wall-clock time at start */ + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer ){ + getrusage(RUSAGE_SELF, &sBegin); + iBegin = timeOfDay(); + } +} + +/* Return the difference of two time_structs in seconds */ +static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ + return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + + (double)(pEnd->tv_sec - pStart->tv_sec); +} + +/* +** Print the timing results. +*/ +static void endTimer(void){ + if( enableTimer ){ + sqlite3_int64 iEnd = timeOfDay(); + struct rusage sEnd; + getrusage(RUSAGE_SELF, &sEnd); + printf("Run Time: real %.3f user %f sys %f\n", + (iEnd - iBegin)*0.001, + timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), + timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER endTimer() +#define HAS_TIMER 1 + +#elif (defined(_WIN32) || defined(WIN32)) + +/* Saved resource information for the beginning of an operation */ +static HANDLE hProcess; +static FILETIME ftKernelBegin; +static FILETIME ftUserBegin; +static sqlite3_int64 ftWallBegin; +typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, + LPFILETIME, LPFILETIME); +static GETPROCTIMES getProcessTimesAddr = NULL; + +/* +** Check to see if we have timer support. Return 1 if necessary +** support found (or found previously). +*/ +static int hasTimer(void){ + if( getProcessTimesAddr ){ + return 1; + } else { + /* GetProcessTimes() isn't supported in WIN95 and some other Windows + ** versions. See if the version we are running on has it, and if it + ** does, save off a pointer to it and the current process handle. + */ + hProcess = GetCurrentProcess(); + if( hProcess ){ + HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); + if( NULL != hinstLib ){ + getProcessTimesAddr = + (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); + if( NULL != getProcessTimesAddr ){ + return 1; + } + FreeLibrary(hinstLib); + } + } + } + return 0; +} + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer && getProcessTimesAddr ){ + FILETIME ftCreation, ftExit; + getProcessTimesAddr(hProcess,&ftCreation,&ftExit, + &ftKernelBegin,&ftUserBegin); + ftWallBegin = timeOfDay(); + } +} + +/* Return the difference of two FILETIME structs in seconds */ +static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ + sqlite_int64 i64Start = *((sqlite_int64 *) pStart); + sqlite_int64 i64End = *((sqlite_int64 *) pEnd); + return (double) ((i64End - i64Start) / 10000000.0); +} + +/* +** Print the timing results. +*/ +static void endTimer(void){ + if( enableTimer && getProcessTimesAddr){ + FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; + sqlite3_int64 ftWallEnd = timeOfDay(); + getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); + printf("Run Time: real %.3f user %f sys %f\n", + (ftWallEnd - ftWallBegin)*0.001, + timeDiff(&ftUserBegin, &ftUserEnd), + timeDiff(&ftKernelBegin, &ftKernelEnd)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER endTimer() +#define HAS_TIMER hasTimer() + +#else +#define BEGIN_TIMER +#define END_TIMER +#define HAS_TIMER 0 +#endif + +/* +** Used to prevent warnings about unused parameters +*/ +#define UNUSED_PARAMETER(x) (void)(x) + +/* +** If the following flag is set, then command execution stops +** at an error if we are not interactive. +*/ +static int bail_on_error = 0; + +/* +** Threat stdin as an interactive input if the following variable +** is true. Otherwise, assume stdin is connected to a file or pipe. +*/ +static int stdin_is_interactive = 1; + +/* +** On Windows systems we have to know if standard output is a console +** in order to translate UTF-8 into MBCS. The following variable is +** true if translation is required. +*/ +static int stdout_is_console = 1; + +/* +** The following is the open SQLite database. We make a pointer +** to this database a static variable so that it can be accessed +** by the SIGINT handler to interrupt database processing. +*/ +static sqlite3 *globalDb = 0; + +/* +** True if an interrupt (Control-C) has been received. +*/ +static volatile int seenInterrupt = 0; + +/* +** This is the name of our program. It is set in main(), used +** in a number of other places, mostly for error messages. +*/ +static char *Argv0; + +/* +** Prompt strings. Initialized in main. Settable with +** .prompt main continue +*/ +static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ +static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ + +/* +** Render output like fprintf(). Except, if the output is going to the +** console and if this is running on a Windows machine, translate the +** output from UTF-8 into MBCS. +*/ +#if defined(_WIN32) || defined(WIN32) +void utf8_printf(FILE *out, const char *zFormat, ...){ + va_list ap; + va_start(ap, zFormat); + if( stdout_is_console && (out==stdout || out==stderr) ){ + char *z1 = sqlite3_vmprintf(zFormat, ap); + char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); + sqlite3_free(z1); + fputs(z2, out); + sqlite3_free(z2); + }else{ + vfprintf(out, zFormat, ap); + } + va_end(ap); +} +#elif !defined(utf8_printf) +# define utf8_printf fprintf +#endif + +/* +** Render output like fprintf(). This should not be used on anything that +** includes string formatting (e.g. "%s"). +*/ +#if !defined(raw_printf) +# define raw_printf fprintf +#endif + +/* +** Write I/O traces to the following stream. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static FILE *iotrace = 0; +#endif + +/* +** This routine works like printf in that its first argument is a +** format string and subsequent arguments are values to be substituted +** in place of % fields. The result of formatting this string +** is written to iotrace. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ + va_list ap; + char *z; + if( iotrace==0 ) return; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + utf8_printf(iotrace, "%s", z); + sqlite3_free(z); +} +#endif + + +/* +** Determines if a string is a number of not. +*/ +static int isNumber(const char *z, int *realnum){ + if( *z=='-' || *z=='+' ) z++; + if( !IsDigit(*z) ){ + return 0; + } + z++; + if( realnum ) *realnum = 0; + while( IsDigit(*z) ){ z++; } + if( *z=='.' ){ + z++; + if( !IsDigit(*z) ) return 0; + while( IsDigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + if( *z=='e' || *z=='E' ){ + z++; + if( *z=='+' || *z=='-' ) z++; + if( !IsDigit(*z) ) return 0; + while( IsDigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + return *z==0; +} + +/* +** A global char* and an SQL function to access its current value +** from within an SQL statement. This program used to use the +** sqlite_exec_printf() API to substitue a string into an SQL statement. +** The correct way to do this with sqlite3 is to use the bind API, but +** since the shell is built around the callback paradigm it would be a lot +** of work. Instead just use this hack, which is quite harmless. +*/ +static const char *zShellStatic = 0; +static void shellstaticFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( 0==argc ); + assert( zShellStatic ); + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); +} + + +/* +** Compute a string length that is limited to what can be stored in +** lower 30 bits of a 32-bit signed integer. +*/ +static int strlen30(const char *z){ + const char *z2 = z; + while( *z2 ){ z2++; } + return 0x3fffffff & (int)(z2 - z); +} + +/* +** This routine reads a line of text from FILE in, stores +** the text in memory obtained from malloc() and returns a pointer +** to the text. NULL is returned at end of file, or if malloc() +** fails. +** +** If zLine is not NULL then it is a malloced buffer returned from +** a previous call to this routine that may be reused. +*/ +static char *local_getline(char *zLine, FILE *in){ + int nLine = zLine==0 ? 0 : 100; + int n = 0; + + while( 1 ){ + if( n+100>nLine ){ + nLine = nLine*2 + 100; + zLine = realloc(zLine, nLine); + if( zLine==0 ) return 0; + } + if( fgets(&zLine[n], nLine - n, in)==0 ){ + if( n==0 ){ + free(zLine); + return 0; + } + zLine[n] = 0; + break; + } + while( zLine[n] ) n++; + if( n>0 && zLine[n-1]=='\n' ){ + n--; + if( n>0 && zLine[n-1]=='\r' ) n--; + zLine[n] = 0; + break; + } + } +#if defined(_WIN32) || defined(WIN32) + /* For interactive input on Windows systems, translate the + ** multi-byte characterset characters into UTF-8. */ + if( stdin_is_interactive ){ + char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); + if( zTrans ){ + int nTrans = strlen30(zTrans)+1; + if( nTrans>nLine ){ + zLine = realloc(zLine, nTrans); + if( zLine==0 ){ + sqlite3_free(zTrans); + return 0; + } + } + memcpy(zLine, zTrans, nTrans); + sqlite3_free(zTrans); + } + } +#endif /* defined(_WIN32) || defined(WIN32) */ + return zLine; +} + +/* +** Retrieve a single line of input text. +** +** If in==0 then read from standard input and prompt before each line. +** If isContinuation is true, then a continuation prompt is appropriate. +** If isContinuation is zero, then the main prompt should be used. +** +** If zPrior is not NULL then it is a buffer from a prior call to this +** routine that can be reused. +** +** The result is stored in space obtained from malloc() and must either +** be freed by the caller or else passed back into this routine via the +** zPrior argument for reuse. +*/ +static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ + char *zPrompt; + char *zResult; + if( in!=0 ){ + zResult = local_getline(zPrior, in); + }else{ + zPrompt = isContinuation ? continuePrompt : mainPrompt; +#if SHELL_USE_LOCAL_GETLINE + printf("%s", zPrompt); + fflush(stdout); + zResult = local_getline(zPrior, stdin); +#else + free(zPrior); + zResult = shell_readline(zPrompt); + if( zResult && *zResult ) shell_add_history(zResult); +#endif + } + return zResult; +} + +#if defined(SQLITE_ENABLE_SESSION) +/* +** State information for a single open session +*/ +typedef struct OpenSession OpenSession; +struct OpenSession { + char *zName; /* Symbolic name for this session */ + int nFilter; /* Number of xFilter rejection GLOB patterns */ + char **azFilter; /* Array of xFilter rejection GLOB patterns */ + sqlite3_session *p; /* The open session */ +}; +#endif + +/* +** Shell output mode information from before ".explain on", +** saved so that it can be restored by ".explain off" +*/ +typedef struct SavedModeInfo SavedModeInfo; +struct SavedModeInfo { + int valid; /* Is there legit data in here? */ + int mode; /* Mode prior to ".explain on" */ + int showHeader; /* The ".header" setting prior to ".explain on" */ + int colWidth[100]; /* Column widths prior to ".explain on" */ +}; + +/* +** State information about the database connection is contained in an +** instance of the following structure. +*/ +typedef struct ShellState ShellState; +struct ShellState { + sqlite3 *db; /* The database */ + int echoOn; /* True to echo input commands */ + int autoExplain; /* Automatically turn on .explain mode */ + int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ + int statsOn; /* True to display memory stats before each finalize */ + int scanstatsOn; /* True to display scan stats before each finalize */ + int countChanges; /* True to display change counts */ + int backslashOn; /* Resolve C-style \x escapes in SQL input text */ + int outCount; /* Revert to stdout when reaching zero */ + int cnt; /* Number of records displayed so far */ + FILE *out; /* Write results here */ + FILE *traceOut; /* Output for sqlite3_trace() */ + int nErr; /* Number of errors seen */ + int mode; /* An output mode setting */ + int cMode; /* temporary output mode for the current query */ + int normalMode; /* Output mode before ".explain on" */ + int writableSchema; /* True if PRAGMA writable_schema=ON */ + int showHeader; /* True to show column names in List or Column mode */ + unsigned shellFlgs; /* Various flags */ + char *zDestTable; /* Name of destination table when MODE_Insert */ + char colSeparator[20]; /* Column separator character for several modes */ + char rowSeparator[20]; /* Row separator character for MODE_Ascii */ + int colWidth[100]; /* Requested width of each column when in column mode*/ + int actualWidth[100]; /* Actual width of each column */ + char nullValue[20]; /* The text to print when a NULL comes back from + ** the database */ + char outfile[FILENAME_MAX]; /* Filename for *out */ + const char *zDbFilename; /* name of the database file */ + char *zFreeOnClose; /* Filename to free when closing */ + const char *zVfs; /* Name of VFS to use */ + sqlite3_stmt *pStmt; /* Current statement if any. */ + FILE *pLog; /* Write log output here */ + int *aiIndent; /* Array of indents used in MODE_Explain */ + int nIndent; /* Size of array aiIndent[] */ + int iIndent; /* Index of current op in aiIndent[] */ +#if defined(SQLITE_ENABLE_SESSION) + int nSession; /* Number of active sessions */ + OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ +#endif +}; + +/* +** These are the allowed shellFlgs values +*/ +#define SHFLG_Scratch 0x00001 /* The --scratch option is used */ +#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ +#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ + +/* +** These are the allowed modes. +*/ +#define MODE_Line 0 /* One column per line. Blank line between records */ +#define MODE_Column 1 /* One record per line in neat columns */ +#define MODE_List 2 /* One record per line with a separator */ +#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ +#define MODE_Html 4 /* Generate an XHTML table */ +#define MODE_Insert 5 /* Generate SQL "insert" statements */ +#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ +#define MODE_Csv 7 /* Quote strings, numbers are plain */ +#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ +#define MODE_Ascii 9 /* Use ASCII unit and record separators (0x1F/0x1E) */ +#define MODE_Pretty 10 /* Pretty-print schemas */ + +static const char *modeDescr[] = { + "line", + "column", + "list", + "semi", + "html", + "insert", + "tcl", + "csv", + "explain", + "ascii", + "prettyprint", +}; + +/* +** These are the column/row/line separators used by the various +** import/export modes. +*/ +#define SEP_Column "|" +#define SEP_Row "\n" +#define SEP_Tab "\t" +#define SEP_Space " " +#define SEP_Comma "," +#define SEP_CrLf "\r\n" +#define SEP_Unit "\x1F" +#define SEP_Record "\x1E" + +/* +** Number of elements in an array +*/ +#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) + +/* +** A callback for the sqlite3_log() interface. +*/ +static void shellLog(void *pArg, int iErrCode, const char *zMsg){ + ShellState *p = (ShellState*)pArg; + if( p->pLog==0 ) return; + utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); + fflush(p->pLog); +} + +/* +** Output the given string as a hex-encoded blob (eg. X'1234' ) +*/ +static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ + int i; + char *zBlob = (char *)pBlob; + raw_printf(out,"X'"); + for(i=0; i0 ){ + utf8_printf(out,"%.*s",i,z); + } + if( z[i]=='<' ){ + raw_printf(out,"<"); + }else if( z[i]=='&' ){ + raw_printf(out,"&"); + }else if( z[i]=='>' ){ + raw_printf(out,">"); + }else if( z[i]=='\"' ){ + raw_printf(out,"""); + }else if( z[i]=='\'' ){ + raw_printf(out,"'"); + }else{ + break; + } + z += i + 1; + } +} + +/* +** If a field contains any character identified by a 1 in the following +** array, then the string must be quoted for CSV. +*/ +static const char needCsvQuote[] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + +/* +** Output a single term of CSV. Actually, p->colSeparator is used for +** the separator, which may or may not be a comma. p->nullValue is +** the null value. Strings are quoted if necessary. The separator +** is only issued if bSep is true. +*/ +static void output_csv(ShellState *p, const char *z, int bSep){ + FILE *out = p->out; + if( z==0 ){ + utf8_printf(out,"%s",p->nullValue); + }else{ + int i; + int nSep = strlen30(p->colSeparator); + for(i=0; z[i]; i++){ + if( needCsvQuote[((unsigned char*)z)[i]] + || (z[i]==p->colSeparator[0] && + (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ + i = 0; + break; + } + } + if( i==0 ){ + putc('"', out); + for(i=0; z[i]; i++){ + if( z[i]=='"' ) putc('"', out); + putc(z[i], out); + } + putc('"', out); + }else{ + utf8_printf(out, "%s", z); + } + } + if( bSep ){ + utf8_printf(p->out, "%s", p->colSeparator); + } +} + +#ifdef SIGINT +/* +** This routine runs when the user presses Ctrl-C +*/ +static void interrupt_handler(int NotUsed){ + UNUSED_PARAMETER(NotUsed); + seenInterrupt++; + if( seenInterrupt>2 ) exit(1); + if( globalDb ) sqlite3_interrupt(globalDb); +} +#endif + +/* +** When the ".auth ON" is set, the following authorizer callback is +** invoked. It always returns SQLITE_OK. +*/ +static int shellAuth( + void *pClientData, + int op, + const char *zA1, + const char *zA2, + const char *zA3, + const char *zA4 +){ + ShellState *p = (ShellState*)pClientData; + static const char *azAction[] = { 0, + "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", + "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", + "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", + "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", + "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", + "DROP_TRIGGER", "DROP_VIEW", "INSERT", + "PRAGMA", "READ", "SELECT", + "TRANSACTION", "UPDATE", "ATTACH", + "DETACH", "ALTER_TABLE", "REINDEX", + "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", + "FUNCTION", "SAVEPOINT", "RECURSIVE" + }; + int i; + const char *az[4]; + az[0] = zA1; + az[1] = zA2; + az[2] = zA3; + az[3] = zA4; + raw_printf(p->out, "authorizer: %s", azAction[op]); + for(i=0; i<4; i++){ + raw_printf(p->out, " "); + if( az[i] ){ + output_c_string(p->out, az[i]); + }else{ + raw_printf(p->out, "NULL"); + } + } + raw_printf(p->out, "\n"); + return SQLITE_OK; +} + + +/* +** This is the callback routine that the shell +** invokes for each row of a query result. +*/ +static int shell_callback( + void *pArg, + int nArg, /* Number of result columns */ + char **azArg, /* Text of each result column */ + char **azCol, /* Column names */ + int *aiType /* Column types */ +){ + int i; + ShellState *p = (ShellState*)pArg; + + switch( p->cMode ){ + case MODE_Line: { + int w = 5; + if( azArg==0 ) break; + for(i=0; iw ) w = len; + } + if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); + for(i=0; iout,"%*s = %s%s", w, azCol[i], + azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); + } + break; + } + case MODE_Explain: + case MODE_Column: { + static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; + const int *colWidth; + int showHdr; + char *rowSep; + if( p->cMode==MODE_Column ){ + colWidth = p->colWidth; + showHdr = p->showHeader; + rowSep = p->rowSeparator; + }else{ + colWidth = aExplainWidths; + showHdr = 1; + rowSep = SEP_Row; + } + if( p->cnt++==0 ){ + for(i=0; icolWidth) ){ + w = colWidth[i]; + }else{ + w = 0; + } + if( w==0 ){ + w = strlen30(azCol[i] ? azCol[i] : ""); + if( w<10 ) w = 10; + n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); + if( wactualWidth) ){ + p->actualWidth[i] = w; + } + if( showHdr ){ + if( w<0 ){ + utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], + i==nArg-1 ? rowSep : " "); + }else{ + utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], + i==nArg-1 ? rowSep : " "); + } + } + } + if( showHdr ){ + for(i=0; iactualWidth) ){ + w = p->actualWidth[i]; + if( w<0 ) w = -w; + }else{ + w = 10; + } + utf8_printf(p->out,"%-*.*s%s",w,w, + "----------------------------------------------------------" + "----------------------------------------------------------", + i==nArg-1 ? rowSep : " "); + } + } + } + if( azArg==0 ) break; + for(i=0; iactualWidth) ){ + w = p->actualWidth[i]; + }else{ + w = 10; + } + if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ + w = strlen30(azArg[i]); + } + if( i==1 && p->aiIndent && p->pStmt ){ + if( p->iIndentnIndent ){ + utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); + } + p->iIndent++; + } + if( w<0 ){ + utf8_printf(p->out,"%*.*s%s",-w,-w, + azArg[i] ? azArg[i] : p->nullValue, + i==nArg-1 ? rowSep : " "); + }else{ + utf8_printf(p->out,"%-*.*s%s",w,w, + azArg[i] ? azArg[i] : p->nullValue, + i==nArg-1 ? rowSep : " "); + } + } + break; + } + case MODE_Semi: { /* .schema and .fullschema output */ + utf8_printf(p->out, "%s;\n", azArg[0]); + break; + } + case MODE_Pretty: { /* .schema and .fullschema with --indent */ + char *z; + int j; + int nParen = 0; + char cEnd = 0; + char c; + int nLine = 0; + assert( nArg==1 ); + if( azArg[0]==0 ) break; + if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 + || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 + ){ + utf8_printf(p->out, "%s;\n", azArg[0]); + break; + } + z = sqlite3_mprintf("%s", azArg[0]); + j = 0; + for(i=0; IsSpace(z[i]); i++){} + for(; (c = z[i])!=0; i++){ + if( IsSpace(c) ){ + if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; + }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ + j--; + } + z[j++] = c; + } + while( j>0 && IsSpace(z[j-1]) ){ j--; } + z[j] = 0; + if( strlen30(z)>=79 ){ + for(i=j=0; (c = z[i])!=0; i++){ + if( c==cEnd ){ + cEnd = 0; + }else if( c=='"' || c=='\'' || c=='`' ){ + cEnd = c; + }else if( c=='[' ){ + cEnd = ']'; + }else if( c=='(' ){ + nParen++; + }else if( c==')' ){ + nParen--; + if( nLine>0 && nParen==0 && j>0 ){ + utf8_printf(p->out, "%.*s\n", j, z); + j = 0; + } + } + z[j++] = c; + if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ + if( c=='\n' ) j--; + utf8_printf(p->out, "%.*s\n ", j, z); + j = 0; + nLine++; + while( IsSpace(z[i+1]) ){ i++; } + } + } + z[j] = 0; + } + utf8_printf(p->out, "%s;\n", z); + sqlite3_free(z); + break; + } + case MODE_List: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout,"%s%s",azCol[i], + i==nArg-1 ? p->rowSeparator : p->colSeparator); + } + } + if( azArg==0 ) break; + for(i=0; inullValue; + utf8_printf(p->out, "%s", z); + if( iout, "%s", p->colSeparator); + }else{ + utf8_printf(p->out, "%s", p->rowSeparator); + } + } + break; + } + case MODE_Html: { + if( p->cnt++==0 && p->showHeader ){ + raw_printf(p->out,""); + for(i=0; iout,""); + output_html_string(p->out, azCol[i]); + raw_printf(p->out,"\n"); + } + raw_printf(p->out,"\n"); + } + if( azArg==0 ) break; + raw_printf(p->out,""); + for(i=0; iout,""); + output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); + raw_printf(p->out,"\n"); + } + raw_printf(p->out,"\n"); + break; + } + case MODE_Tcl: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout,azCol[i] ? azCol[i] : ""); + if(iout, "%s", p->colSeparator); + } + utf8_printf(p->out, "%s", p->rowSeparator); + } + if( azArg==0 ) break; + for(i=0; iout, azArg[i] ? azArg[i] : p->nullValue); + if(iout, "%s", p->colSeparator); + } + utf8_printf(p->out, "%s", p->rowSeparator); + break; + } + case MODE_Csv: { + setBinaryMode(p->out, 1); + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout, "%s", p->rowSeparator); + } + if( nArg>0 ){ + for(i=0; iout, "%s", p->rowSeparator); + } + setTextMode(p->out, 1); + break; + } + case MODE_Insert: { + p->cnt++; + if( azArg==0 ) break; + utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); + if( p->showHeader ){ + raw_printf(p->out,"("); + for(i=0; i0 ? ",": ""; + utf8_printf(p->out, "%s%s", zSep, azCol[i]); + } + raw_printf(p->out,")"); + } + raw_printf(p->out," VALUES("); + for(i=0; i0 ? ",": ""; + if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ + utf8_printf(p->out,"%sNULL",zSep); + }else if( aiType && aiType[i]==SQLITE_TEXT ){ + if( zSep[0] ) utf8_printf(p->out,"%s",zSep); + output_quoted_string(p->out, azArg[i]); + }else if( aiType && (aiType[i]==SQLITE_INTEGER + || aiType[i]==SQLITE_FLOAT) ){ + utf8_printf(p->out,"%s%s",zSep, azArg[i]); + }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ + const void *pBlob = sqlite3_column_blob(p->pStmt, i); + int nBlob = sqlite3_column_bytes(p->pStmt, i); + if( zSep[0] ) utf8_printf(p->out,"%s",zSep); + output_hex_blob(p->out, pBlob, nBlob); + }else if( isNumber(azArg[i], 0) ){ + utf8_printf(p->out,"%s%s",zSep, azArg[i]); + }else{ + if( zSep[0] ) utf8_printf(p->out,"%s",zSep); + output_quoted_string(p->out, azArg[i]); + } + } + raw_printf(p->out,");\n"); + break; + } + case MODE_Ascii: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; i0 ) utf8_printf(p->out, "%s", p->colSeparator); + utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); + } + utf8_printf(p->out, "%s", p->rowSeparator); + } + if( azArg==0 ) break; + for(i=0; i0 ) utf8_printf(p->out, "%s", p->colSeparator); + utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); + } + utf8_printf(p->out, "%s", p->rowSeparator); + break; + } + } + return 0; +} + +/* +** This is the callback routine that the SQLite library +** invokes for each row of a query result. +*/ +static int callback(void *pArg, int nArg, char **azArg, char **azCol){ + /* since we don't have type info, call the shell_callback with a NULL value */ + return shell_callback(pArg, nArg, azArg, azCol, NULL); +} + +/* +** Set the destination table field of the ShellState structure to +** the name of the table given. Escape any quote characters in the +** table name. +*/ +static void set_table_name(ShellState *p, const char *zName){ + int i, n; + int needQuote; + char *z; + + if( p->zDestTable ){ + free(p->zDestTable); + p->zDestTable = 0; + } + if( zName==0 ) return; + needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; + for(i=n=0; zName[i]; i++, n++){ + if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ + needQuote = 1; + if( zName[i]=='\'' ) n++; + } + } + if( needQuote ) n += 2; + z = p->zDestTable = malloc( n+1 ); + if( z==0 ){ + raw_printf(stderr,"Error: out of memory\n"); + exit(1); + } + n = 0; + if( needQuote ) z[n++] = '\''; + for(i=0; zName[i]; i++){ + z[n++] = zName[i]; + if( zName[i]=='\'' ) z[n++] = '\''; + } + if( needQuote ) z[n++] = '\''; + z[n] = 0; +} + +/* zIn is either a pointer to a NULL-terminated string in memory obtained +** from malloc(), or a NULL pointer. The string pointed to by zAppend is +** added to zIn, and the result returned in memory obtained from malloc(). +** zIn, if it was not NULL, is freed. +** +** If the third argument, quote, is not '\0', then it is used as a +** quote character for zAppend. +*/ +static char *appendText(char *zIn, char const *zAppend, char quote){ + int len; + int i; + int nAppend = strlen30(zAppend); + int nIn = (zIn?strlen30(zIn):0); + + len = nAppend+nIn+1; + if( quote ){ + len += 2; + for(i=0; idb, zSelect, -1, &pSelect, 0); + if( rc!=SQLITE_OK || !pSelect ){ + utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, + sqlite3_errmsg(p->db)); + if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; + return rc; + } + rc = sqlite3_step(pSelect); + nResult = sqlite3_column_count(pSelect); + while( rc==SQLITE_ROW ){ + if( zFirstRow ){ + utf8_printf(p->out, "%s", zFirstRow); + zFirstRow = 0; + } + z = (const char*)sqlite3_column_text(pSelect, 0); + utf8_printf(p->out, "%s", z); + for(i=1; iout, ",%s", sqlite3_column_text(pSelect, i)); + } + if( z==0 ) z = ""; + while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; + if( z[0] ){ + raw_printf(p->out, "\n;\n"); + }else{ + raw_printf(p->out, ";\n"); + } + rc = sqlite3_step(pSelect); + } + rc = sqlite3_finalize(pSelect); + if( rc!=SQLITE_OK ){ + utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, + sqlite3_errmsg(p->db)); + if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; + } + return rc; +} + +/* +** Allocate space and save off current error string. +*/ +static char *save_err_msg( + sqlite3 *db /* Database to query */ +){ + int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); + char *zErrMsg = sqlite3_malloc64(nErrMsg); + if( zErrMsg ){ + memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); + } + return zErrMsg; +} + +#ifdef __linux__ +/* +** Attempt to display I/O stats on Linux using /proc/PID/io +*/ +static void displayLinuxIoStats(FILE *out){ + FILE *in; + char z[200]; + sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); + in = fopen(z, "rb"); + if( in==0 ) return; + while( fgets(z, sizeof(z), in)!=0 ){ + static const struct { + const char *zPattern; + const char *zDesc; + } aTrans[] = { + { "rchar: ", "Bytes received by read():" }, + { "wchar: ", "Bytes sent to write():" }, + { "syscr: ", "Read() system calls:" }, + { "syscw: ", "Write() system calls:" }, + { "read_bytes: ", "Bytes read from storage:" }, + { "write_bytes: ", "Bytes written to storage:" }, + { "cancelled_write_bytes: ", "Cancelled write bytes:" }, + }; + int i; + for(i=0; iout ){ + + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Memory Used: %d (max %d) bytes\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", + iCur, iHiwtr); + if( pArg->shellFlgs & SHFLG_Pagecache ){ + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Number of Pcache Pages Used: %d (max %d) pages\n", + iCur, iHiwtr); + } + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", + iCur, iHiwtr); + if( pArg->shellFlgs & SHFLG_Scratch ){ + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Number of Scratch Allocations Used: %d (max %d)\n", + iCur, iHiwtr); + } + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Largest Allocation: %d bytes\n", + iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", + iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", + iHiwtr); +#ifdef YYTRACKMAXSTACKDEPTH + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", + iCur, iHiwtr); +#endif + } + + if( pArg && pArg->out && db ){ + if( pArg->shellFlgs & SHFLG_Lookaside ){ + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, + &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, + "Lookaside Slots Used: %d (max %d)\n", + iCur, iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, + &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Successful lookaside attempts: %d\n", + iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, + &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Lookaside failures due to size: %d\n", + iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, + &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", + iHiwtr); + } + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", + iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); + raw_printf(pArg->out, "Page cache hits: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); + raw_printf(pArg->out, "Page cache misses: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); + raw_printf(pArg->out, "Page cache writes: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", + iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); + raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", + iCur); + } + + if( pArg && pArg->out && db && pArg->pStmt ){ + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, + bReset); + raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); + raw_printf(pArg->out, "Sort Operations: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); + raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); + raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); + } + +#ifdef __linux__ + displayLinuxIoStats(pArg->out); +#endif + + /* Do not remove this machine readable comment: extra-stats-output-here */ + + return 0; +} + +/* +** Display scan stats. +*/ +static void display_scanstats( + sqlite3 *db, /* Database to query */ + ShellState *pArg /* Pointer to ShellState */ +){ +#ifndef SQLITE_ENABLE_STMT_SCANSTATUS + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(pArg); +#else + int i, k, n, mx; + raw_printf(pArg->out, "-------- scanstats --------\n"); + mx = 0; + for(k=0; k<=mx; k++){ + double rEstLoop = 1.0; + for(i=n=0; 1; i++){ + sqlite3_stmt *p = pArg->pStmt; + sqlite3_int64 nLoop, nVisit; + double rEst; + int iSid; + const char *zExplain; + if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ + break; + } + sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); + if( iSid>mx ) mx = iSid; + if( iSid!=k ) continue; + if( n==0 ){ + rEstLoop = (double)nLoop; + if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); + } + n++; + sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); + sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); + sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); + utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); + rEstLoop *= rEst; + raw_printf(pArg->out, + " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", + nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst + ); + } + } + raw_printf(pArg->out, "---------------------------\n"); +#endif +} + +/* +** Parameter azArray points to a zero-terminated array of strings. zStr +** points to a single nul-terminated string. Return non-zero if zStr +** is equal, according to strcmp(), to any of the strings in the array. +** Otherwise, return zero. +*/ +static int str_in_array(const char *zStr, const char **azArray){ + int i; + for(i=0; azArray[i]; i++){ + if( 0==strcmp(zStr, azArray[i]) ) return 1; + } + return 0; +} + +/* +** If compiled statement pSql appears to be an EXPLAIN statement, allocate +** and populate the ShellState.aiIndent[] array with the number of +** spaces each opcode should be indented before it is output. +** +** The indenting rules are: +** +** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent +** all opcodes that occur between the p2 jump destination and the opcode +** itself by 2 spaces. +** +** * For each "Goto", if the jump destination is earlier in the program +** and ends on one of: +** Yield SeekGt SeekLt RowSetRead Rewind +** or if the P1 parameter is one instead of zero, +** then indent all opcodes between the earlier instruction +** and "Goto" by 2 spaces. +*/ +static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ + const char *zSql; /* The text of the SQL statement */ + const char *z; /* Used to check if this is an EXPLAIN */ + int *abYield = 0; /* True if op is an OP_Yield */ + int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ + int iOp; /* Index of operation in p->aiIndent[] */ + + const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", + "NextIfOpen", "PrevIfOpen", 0 }; + const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", + "Rewind", 0 }; + const char *azGoto[] = { "Goto", 0 }; + + /* Try to figure out if this is really an EXPLAIN statement. If this + ** cannot be verified, return early. */ + if( sqlite3_column_count(pSql)!=8 ){ + p->cMode = p->mode; + return; + } + zSql = sqlite3_sql(pSql); + if( zSql==0 ) return; + for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); + if( sqlite3_strnicmp(z, "explain", 7) ){ + p->cMode = p->mode; + return; + } + + for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ + int i; + int iAddr = sqlite3_column_int(pSql, 0); + const char *zOp = (const char*)sqlite3_column_text(pSql, 1); + + /* Set p2 to the P2 field of the current opcode. Then, assuming that + ** p2 is an instruction address, set variable p2op to the index of that + ** instruction in the aiIndent[] array. p2 and p2op may be different if + ** the current instruction is part of a sub-program generated by an + ** SQL trigger or foreign key. */ + int p2 = sqlite3_column_int(pSql, 3); + int p2op = (p2 + (iOp-iAddr)); + + /* Grow the p->aiIndent array as required */ + if( iOp>=nAlloc ){ + if( iOp==0 ){ + /* Do further verfication that this is explain output. Abort if + ** it is not */ + static const char *explainCols[] = { + "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; + int jj; + for(jj=0; jjcMode = p->mode; + sqlite3_reset(pSql); + return; + } + } + } + nAlloc += 100; + p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); + abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); + } + abYield[iOp] = str_in_array(zOp, azYield); + p->aiIndent[iOp] = 0; + p->nIndent = iOp+1; + + if( str_in_array(zOp, azNext) ){ + for(i=p2op; iaiIndent[i] += 2; + } + if( str_in_array(zOp, azGoto) && p2opnIndent + && (abYield[p2op] || sqlite3_column_int(pSql, 2)) + ){ + for(i=p2op; iaiIndent[i] += 2; + } + } + + p->iIndent = 0; + sqlite3_free(abYield); + sqlite3_reset(pSql); +} + +/* +** Free the array allocated by explain_data_prepare(). +*/ +static void explain_data_delete(ShellState *p){ + sqlite3_free(p->aiIndent); + p->aiIndent = 0; + p->nIndent = 0; + p->iIndent = 0; +} + +/* +** Disable and restore .wheretrace and .selecttrace settings. +*/ +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) +extern int sqlite3SelectTrace; +static int savedSelectTrace; +#endif +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) +extern int sqlite3WhereTrace; +static int savedWhereTrace; +#endif +static void disable_debug_trace_modes(void){ +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) + savedSelectTrace = sqlite3SelectTrace; + sqlite3SelectTrace = 0; +#endif +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) + savedWhereTrace = sqlite3WhereTrace; + sqlite3WhereTrace = 0; +#endif +} +static void restore_debug_trace_modes(void){ +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) + sqlite3SelectTrace = savedSelectTrace; +#endif +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) + sqlite3WhereTrace = savedWhereTrace; +#endif +} + +/* +** Run a prepared statement +*/ +static void exec_prepared_stmt( + ShellState *pArg, /* Pointer to ShellState */ + sqlite3_stmt *pStmt, /* Statment to run */ + int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ +){ + int rc; + + /* perform the first step. this will tell us if we + ** have a result set or not and how wide it is. + */ + rc = sqlite3_step(pStmt); + /* if we have a result set... */ + if( SQLITE_ROW == rc ){ + /* if we have a callback... */ + if( xCallback ){ + /* allocate space for col name ptr, value ptr, and type */ + int nCol = sqlite3_column_count(pStmt); + void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); + if( !pData ){ + rc = SQLITE_NOMEM; + }else{ + char **azCols = (char **)pData; /* Names of result columns */ + char **azVals = &azCols[nCol]; /* Results */ + int *aiTypes = (int *)&azVals[nCol]; /* Result types */ + int i, x; + assert(sizeof(int) <= sizeof(char *)); + /* save off ptrs to column names */ + for(i=0; icMode==MODE_Insert ){ + azVals[i] = ""; + }else{ + azVals[i] = (char*)sqlite3_column_text(pStmt, i); + } + if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ + rc = SQLITE_NOMEM; + break; /* from for */ + } + } /* end for */ + + /* if data and types extracted successfully... */ + if( SQLITE_ROW == rc ){ + /* call the supplied callback with the result row data */ + if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ + rc = SQLITE_ABORT; + }else{ + rc = sqlite3_step(pStmt); + } + } + } while( SQLITE_ROW == rc ); + sqlite3_free(pData); + } + }else{ + do{ + rc = sqlite3_step(pStmt); + } while( rc == SQLITE_ROW ); + } + } +} + +/* +** Execute a statement or set of statements. Print +** any result rows/columns depending on the current mode +** set via the supplied callback. +** +** This is very similar to SQLite's built-in sqlite3_exec() +** function except it takes a slightly different callback +** and callback data argument. +*/ +static int shell_exec( + sqlite3 *db, /* An open database */ + const char *zSql, /* SQL to be evaluated */ + int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ + /* (not the same as sqlite3_exec) */ + ShellState *pArg, /* Pointer to ShellState */ + char **pzErrMsg /* Error msg written here */ +){ + sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ + int rc = SQLITE_OK; /* Return Code */ + int rc2; + const char *zLeftover; /* Tail of unprocessed SQL */ + + if( pzErrMsg ){ + *pzErrMsg = NULL; + } + + while( zSql[0] && (SQLITE_OK == rc) ){ + static const char *zStmtSql; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); + if( SQLITE_OK != rc ){ + if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } + }else{ + if( !pStmt ){ + /* this happens for a comment or white-space */ + zSql = zLeftover; + while( IsSpace(zSql[0]) ) zSql++; + continue; + } + zStmtSql = sqlite3_sql(pStmt); + while( IsSpace(zStmtSql[0]) ) zStmtSql++; + + /* save off the prepared statment handle and reset row count */ + if( pArg ){ + pArg->pStmt = pStmt; + pArg->cnt = 0; + } + + /* echo the sql statement if echo on */ + if( pArg && pArg->echoOn ){ + utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); + } + + /* Show the EXPLAIN QUERY PLAN if .eqp is on */ + if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ + sqlite3_stmt *pExplain; + char *zEQP; + disable_debug_trace_modes(); + zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); + rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); + if( rc==SQLITE_OK ){ + while( sqlite3_step(pExplain)==SQLITE_ROW ){ + raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); + raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); + raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); + utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); + } + } + sqlite3_finalize(pExplain); + sqlite3_free(zEQP); + if( pArg->autoEQP>=2 ){ + /* Also do an EXPLAIN for ".eqp full" mode */ + zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); + rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); + if( rc==SQLITE_OK ){ + pArg->cMode = MODE_Explain; + explain_data_prepare(pArg, pExplain); + exec_prepared_stmt(pArg, pExplain, xCallback); + explain_data_delete(pArg); + } + sqlite3_finalize(pExplain); + sqlite3_free(zEQP); + } + restore_debug_trace_modes(); + } + + if( pArg ){ + pArg->cMode = pArg->mode; + if( pArg->autoExplain + && sqlite3_column_count(pStmt)==8 + && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 + ){ + pArg->cMode = MODE_Explain; + } + + /* If the shell is currently in ".explain" mode, gather the extra + ** data required to add indents to the output.*/ + if( pArg->cMode==MODE_Explain ){ + explain_data_prepare(pArg, pStmt); + } + } + + exec_prepared_stmt(pArg, pStmt, xCallback); + explain_data_delete(pArg); + + /* print usage stats if stats on */ + if( pArg && pArg->statsOn ){ + display_stats(db, pArg, 0); + } + + /* print loop-counters if required */ + if( pArg && pArg->scanstatsOn ){ + display_scanstats(db, pArg); + } + + /* Finalize the statement just executed. If this fails, save a + ** copy of the error message. Otherwise, set zSql to point to the + ** next statement to execute. */ + rc2 = sqlite3_finalize(pStmt); + if( rc!=SQLITE_NOMEM ) rc = rc2; + if( rc==SQLITE_OK ){ + zSql = zLeftover; + while( IsSpace(zSql[0]) ) zSql++; + }else if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } + + /* clear saved stmt handle */ + if( pArg ){ + pArg->pStmt = NULL; + } + } + } /* end while */ + + return rc; +} + + +/* +** This is a different callback routine used for dumping the database. +** Each row received by this callback consists of a table name, +** the table type ("index" or "table") and SQL to create the table. +** This routine should print text sufficient to recreate the table. +*/ +static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ + int rc; + const char *zTable; + const char *zType; + const char *zSql; + const char *zPrepStmt = 0; + ShellState *p = (ShellState *)pArg; + + UNUSED_PARAMETER(azCol); + if( nArg!=3 ) return 1; + zTable = azArg[0]; + zType = azArg[1]; + zSql = azArg[2]; + + if( strcmp(zTable, "sqlite_sequence")==0 ){ + zPrepStmt = "DELETE FROM sqlite_sequence;\n"; + }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ + raw_printf(p->out, "ANALYZE sqlite_master;\n"); + }else if( strncmp(zTable, "sqlite_", 7)==0 ){ + return 0; + }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ + char *zIns; + if( !p->writableSchema ){ + raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); + p->writableSchema = 1; + } + zIns = sqlite3_mprintf( + "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" + "VALUES('table','%q','%q',0,'%q');", + zTable, zTable, zSql); + utf8_printf(p->out, "%s\n", zIns); + sqlite3_free(zIns); + return 0; + }else{ + utf8_printf(p->out, "%s;\n", zSql); + } + + if( strcmp(zType, "table")==0 ){ + sqlite3_stmt *pTableInfo = 0; + char *zSelect = 0; + char *zTableInfo = 0; + char *zTmp = 0; + int nRow = 0; + + zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); + zTableInfo = appendText(zTableInfo, zTable, '"'); + zTableInfo = appendText(zTableInfo, ");", 0); + + rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0); + free(zTableInfo); + if( rc!=SQLITE_OK || !pTableInfo ){ + return 1; + } + + zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); + /* Always quote the table name, even if it appears to be pure ascii, + ** in case it is a keyword. Ex: INSERT INTO "table" ... */ + zTmp = appendText(zTmp, zTable, '"'); + if( zTmp ){ + zSelect = appendText(zSelect, zTmp, '\''); + free(zTmp); + } + zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); + rc = sqlite3_step(pTableInfo); + while( rc==SQLITE_ROW ){ + const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); + zSelect = appendText(zSelect, "quote(", 0); + zSelect = appendText(zSelect, zText, '"'); + rc = sqlite3_step(pTableInfo); + if( rc==SQLITE_ROW ){ + zSelect = appendText(zSelect, "), ", 0); + }else{ + zSelect = appendText(zSelect, ") ", 0); + } + nRow++; + } + rc = sqlite3_finalize(pTableInfo); + if( rc!=SQLITE_OK || nRow==0 ){ + free(zSelect); + return 1; + } + zSelect = appendText(zSelect, "|| ')' FROM ", 0); + zSelect = appendText(zSelect, zTable, '"'); + + rc = run_table_dump_query(p, zSelect, zPrepStmt); + if( rc==SQLITE_CORRUPT ){ + zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); + run_table_dump_query(p, zSelect, 0); + } + free(zSelect); + } + return 0; +} + +/* +** Run zQuery. Use dump_callback() as the callback routine so that +** the contents of the query are output as SQL statements. +** +** If we get a SQLITE_CORRUPT error, rerun the query after appending +** "ORDER BY rowid DESC" to the end. +*/ +static int run_schema_dump_query( + ShellState *p, + const char *zQuery +){ + int rc; + char *zErr = 0; + rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); + if( rc==SQLITE_CORRUPT ){ + char *zQ2; + int len = strlen30(zQuery); + raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); + if( zErr ){ + utf8_printf(p->out, "/****** %s ******/\n", zErr); + sqlite3_free(zErr); + zErr = 0; + } + zQ2 = malloc( len+100 ); + if( zQ2==0 ) return rc; + sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); + rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); + if( rc ){ + utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); + }else{ + rc = SQLITE_CORRUPT; + } + sqlite3_free(zErr); + free(zQ2); + } + return rc; +} + +/* +** Text of a help message +*/ +static char zHelp[] = + ".auth ON|OFF Show authorizer callbacks\n" + ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" + ".bail on|off Stop after hitting an error. Default OFF\n" + ".binary on|off Turn binary output on or off. Default OFF\n" + ".changes on|off Show number of rows changed by SQL\n" + ".clone NEWDB Clone data into NEWDB from the existing database\n" + ".databases List names and files of attached databases\n" + ".dbinfo ?DB? Show status information about the database\n" + ".dump ?TABLE? ... Dump the database in an SQL text format\n" + " If TABLE specified, only dump tables matching\n" + " LIKE pattern TABLE.\n" + ".echo on|off Turn command echo on or off\n" + ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" + ".exit Exit this program\n" + ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" + ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" + ".headers on|off Turn display of headers on or off\n" + ".help Show this message\n" + ".import FILE TABLE Import data from FILE into TABLE\n" + ".indexes ?TABLE? Show names of all indexes\n" + " If TABLE specified, only show indexes for tables\n" + " matching LIKE pattern TABLE.\n" +#ifdef SQLITE_ENABLE_IOTRACE + ".iotrace FILE Enable I/O diagnostic logging to FILE\n" +#endif + ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" +#ifndef SQLITE_OMIT_LOAD_EXTENSION + ".load FILE ?ENTRY? Load an extension library\n" +#endif + ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" + ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" + " ascii Columns/rows delimited by 0x1F and 0x1E\n" + " csv Comma-separated values\n" + " column Left-aligned columns. (See .width)\n" + " html HTML code\n" + " insert SQL insert statements for TABLE\n" + " line One value per line\n" + " list Values delimited by .separator strings\n" + " tabs Tab-separated values\n" + " tcl TCL list elements\n" + ".nullvalue STRING Use STRING in place of NULL values\n" + ".once FILENAME Output for the next SQL command only to FILENAME\n" + ".open ?FILENAME? Close existing database and reopen FILENAME\n" + ".output ?FILENAME? Send output to FILENAME or stdout\n" + ".print STRING... Print literal STRING\n" + ".prompt MAIN CONTINUE Replace the standard prompts\n" + ".quit Exit this program\n" + ".read FILENAME Execute SQL in FILENAME\n" + ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" + ".save FILE Write in-memory database into FILE\n" + ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" + ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" + " Add --indent for pretty-printing\n" + ".separator COL ?ROW? Change the column separator and optionally the row\n" + " separator for both the output mode and .import\n" +#if defined(SQLITE_ENABLE_SESSION) + ".session CMD ... Create or control sessions\n" +#endif + ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" + ".show Show the current values for various settings\n" + ".stats ?on|off? Show stats or turn stats on or off\n" + ".system CMD ARGS... Run CMD ARGS... in a system shell\n" + ".tables ?TABLE? List names of tables\n" + " If TABLE specified, only list tables matching\n" + " LIKE pattern TABLE.\n" + ".timeout MS Try opening locked tables for MS milliseconds\n" + ".timer on|off Turn SQL timer on or off\n" + ".trace FILE|off Output each SQL statement as it is run\n" + ".vfsinfo ?AUX? Information about the top-level VFS\n" + ".vfslist List all available VFSes\n" + ".vfsname ?AUX? Print the name of the VFS stack\n" + ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" + " Negative values right-justify\n" +; + +#if defined(SQLITE_ENABLE_SESSION) +/* +** Print help information for the ".sessions" command +*/ +void session_help(ShellState *p){ + raw_printf(p->out, + ".session ?NAME? SUBCOMMAND ?ARGS...?\n" + "If ?NAME? is omitted, the first defined session is used.\n" + "Subcommands:\n" + " attach TABLE Attach TABLE\n" + " changeset FILE Write a changeset into FILE\n" + " close Close one session\n" + " enable ?BOOLEAN? Set or query the enable bit\n" + " filter GLOB... Reject tables matching GLOBs\n" + " indirect ?BOOLEAN? Mark or query the indirect status\n" + " isempty Query whether the session is empty\n" + " list List currently open session names\n" + " open DB NAME Open a new session on DB\n" + " patchset FILE Write a patchset into FILE\n" + ); +} +#endif + + +/* Forward reference */ +static int process_input(ShellState *p, FILE *in); +/* +** Implementation of the "readfile(X)" SQL function. The entire content +** of the file named X is read and returned as a BLOB. NULL is returned +** if the file does not exist or is unreadable. +*/ +static void readfileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zName; + FILE *in; + long nIn; + void *pBuf; + + UNUSED_PARAMETER(argc); + zName = (const char*)sqlite3_value_text(argv[0]); + if( zName==0 ) return; + in = fopen(zName, "rb"); + if( in==0 ) return; + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc64( nIn ); + if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ + sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); + }else{ + sqlite3_free(pBuf); + } + fclose(in); +} + +/* +** Implementation of the "writefile(X,Y)" SQL function. The argument Y +** is written into file X. The number of bytes written is returned. Or +** NULL is returned if something goes wrong, such as being unable to open +** file X for writing. +*/ +static void writefileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + FILE *out; + const char *z; + sqlite3_int64 rc; + const char *zFile; + + UNUSED_PARAMETER(argc); + zFile = (const char*)sqlite3_value_text(argv[0]); + if( zFile==0 ) return; + out = fopen(zFile, "wb"); + if( out==0 ) return; + z = (const char*)sqlite3_value_blob(argv[1]); + if( z==0 ){ + rc = 0; + }else{ + rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); + } + fclose(out); + sqlite3_result_int64(context, rc); +} + +#if defined(SQLITE_ENABLE_SESSION) +/* +** Close a single OpenSession object and release all of its associated +** resources. +*/ +static void session_close(OpenSession *pSession){ + int i; + sqlite3session_delete(pSession->p); + sqlite3_free(pSession->zName); + for(i=0; inFilter; i++){ + sqlite3_free(pSession->azFilter[i]); + } + sqlite3_free(pSession->azFilter); + memset(pSession, 0, sizeof(OpenSession)); +} +#endif + +/* +** Close all OpenSession objects and release all associated resources. +*/ +#if defined(SQLITE_ENABLE_SESSION) +static void session_close_all(ShellState *p){ + int i; + for(i=0; inSession; i++){ + session_close(&p->aSession[i]); + } + p->nSession = 0; +} +#else +# define session_close_all(X) +#endif + +/* +** Implementation of the xFilter function for an open session. Omit +** any tables named by ".session filter" but let all other table through. +*/ +#if defined(SQLITE_ENABLE_SESSION) +static int session_filter(void *pCtx, const char *zTab){ + OpenSession *pSession = (OpenSession*)pCtx; + int i; + for(i=0; inFilter; i++){ + if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; + } + return 1; +} +#endif + +/* +** Make sure the database is open. If it is not, then open it. If +** the database fails to open, print an error message and exit. +*/ +static void open_db(ShellState *p, int keepAlive){ + if( p->db==0 ){ + sqlite3_initialize(); + sqlite3_open(p->zDbFilename, &p->db); + globalDb = p->db; + if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){ + sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0, + shellstaticFunc, 0, 0); + } + if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ + utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", + p->zDbFilename, sqlite3_errmsg(p->db)); + if( keepAlive ) return; + exit(1); + } +#ifndef SQLITE_OMIT_LOAD_EXTENSION + sqlite3_enable_load_extension(p->db, 1); +#endif + sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, + readfileFunc, 0, 0); + sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, + writefileFunc, 0, 0); + } +} + +/* +** Do C-language style dequoting. +** +** \a -> alarm +** \b -> backspace +** \t -> tab +** \n -> newline +** \v -> vertical tab +** \f -> form feed +** \r -> carriage return +** \s -> space +** \" -> " +** \' -> ' +** \\ -> backslash +** \NNN -> ascii character NNN in octal +*/ +static void resolve_backslashes(char *z){ + int i, j; + char c; + while( *z && *z!='\\' ) z++; + for(i=j=0; (c = z[i])!=0; i++, j++){ + if( c=='\\' && z[i+1]!=0 ){ + c = z[++i]; + if( c=='a' ){ + c = '\a'; + }else if( c=='b' ){ + c = '\b'; + }else if( c=='t' ){ + c = '\t'; + }else if( c=='n' ){ + c = '\n'; + }else if( c=='v' ){ + c = '\v'; + }else if( c=='f' ){ + c = '\f'; + }else if( c=='r' ){ + c = '\r'; + }else if( c=='"' ){ + c = '"'; + }else if( c=='\'' ){ + c = '\''; + }else if( c=='\\' ){ + c = '\\'; + }else if( c>='0' && c<='7' ){ + c -= '0'; + if( z[i+1]>='0' && z[i+1]<='7' ){ + i++; + c = (c<<3) + z[i] - '0'; + if( z[i+1]>='0' && z[i+1]<='7' ){ + i++; + c = (c<<3) + z[i] - '0'; + } + } + } + } + z[j] = c; + } + if( j='0' && c<='9' ) return c - '0'; + if( c>='a' && c<='f' ) return c - 'a' + 10; + if( c>='A' && c<='F' ) return c - 'A' + 10; + return -1; +} + +/* +** Interpret zArg as an integer value, possibly with suffixes. +*/ +static sqlite3_int64 integerValue(const char *zArg){ + sqlite3_int64 v = 0; + static const struct { char *zSuffix; int iMult; } aMult[] = { + { "KiB", 1024 }, + { "MiB", 1024*1024 }, + { "GiB", 1024*1024*1024 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "K", 1000 }, + { "M", 1000000 }, + { "G", 1000000000 }, + }; + int i; + int isNeg = 0; + if( zArg[0]=='-' ){ + isNeg = 1; + zArg++; + }else if( zArg[0]=='+' ){ + zArg++; + } + if( zArg[0]=='0' && zArg[1]=='x' ){ + int x; + zArg += 2; + while( (x = hexDigitValue(zArg[0]))>=0 ){ + v = (v<<4) + x; + zArg++; + } + }else{ + while( IsDigit(zArg[0]) ){ + v = v*10 + zArg[0] - '0'; + zArg++; + } + } + for(i=0; i=0; i++){} + }else{ + for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} + } + if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); + if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ + return 1; + } + if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ + return 0; + } + utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", + zArg); + return 0; +} + +/* +** Close an output file, assuming it is not stderr or stdout +*/ +static void output_file_close(FILE *f){ + if( f && f!=stdout && f!=stderr ) fclose(f); +} + +/* +** Try to open an output file. The names "stdout" and "stderr" are +** recognized and do the right thing. NULL is returned if the output +** filename is "off". +*/ +static FILE *output_file_open(const char *zFile){ + FILE *f; + if( strcmp(zFile,"stdout")==0 ){ + f = stdout; + }else if( strcmp(zFile, "stderr")==0 ){ + f = stderr; + }else if( strcmp(zFile, "off")==0 ){ + f = 0; + }else{ + f = fopen(zFile, "wb"); + if( f==0 ){ + utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); + } + } + return f; +} + +/* +** A routine for handling output from sqlite3_trace(). +*/ +static void sql_trace_callback(void *pArg, const char *z){ + FILE *f = (FILE*)pArg; + if( f ){ + int i = (int)strlen(z); + while( i>0 && z[i-1]==';' ){ i--; } + utf8_printf(f, "%.*s;\n", i, z); + } +} + +/* +** A no-op routine that runs with the ".breakpoint" doc-command. This is +** a useful spot to set a debugger breakpoint. +*/ +static void test_breakpoint(void){ + static int nCall = 0; + nCall++; +} + +/* +** An object used to read a CSV and other files for import. +*/ +typedef struct ImportCtx ImportCtx; +struct ImportCtx { + const char *zFile; /* Name of the input file */ + FILE *in; /* Read the CSV text from this input stream */ + char *z; /* Accumulated text for a field */ + int n; /* Number of bytes in z */ + int nAlloc; /* Space allocated for z[] */ + int nLine; /* Current line number */ + int cTerm; /* Character that terminated the most recent field */ + int cColSep; /* The column separator character. (Usually ",") */ + int cRowSep; /* The row separator character. (Usually "\n") */ +}; + +/* Append a single byte to z[] */ +static void import_append_char(ImportCtx *p, int c){ + if( p->n+1>=p->nAlloc ){ + p->nAlloc += p->nAlloc + 100; + p->z = sqlite3_realloc64(p->z, p->nAlloc); + if( p->z==0 ){ + raw_printf(stderr, "out of memory\n"); + exit(1); + } + } + p->z[p->n++] = (char)c; +} + +/* Read a single field of CSV text. Compatible with rfc4180 and extended +** with the option of having a separator other than ",". +** +** + Input comes from p->in. +** + Store results in p->z of length p->n. Space to hold p->z comes +** from sqlite3_malloc64(). +** + Use p->cSep as the column separator. The default is ",". +** + Use p->rSep as the row separator. The default is "\n". +** + Keep track of the line number in p->nLine. +** + Store the character that terminates the field in p->cTerm. Store +** EOF on end-of-file. +** + Report syntax errors on stderr +*/ +static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ + int c; + int cSep = p->cColSep; + int rSep = p->cRowSep; + p->n = 0; + c = fgetc(p->in); + if( c==EOF || seenInterrupt ){ + p->cTerm = EOF; + return 0; + } + if( c=='"' ){ + int pc, ppc; + int startLine = p->nLine; + int cQuote = c; + pc = ppc = 0; + while( 1 ){ + c = fgetc(p->in); + if( c==rSep ) p->nLine++; + if( c==cQuote ){ + if( pc==cQuote ){ + pc = 0; + continue; + } + } + if( (c==cSep && pc==cQuote) + || (c==rSep && pc==cQuote) + || (c==rSep && pc=='\r' && ppc==cQuote) + || (c==EOF && pc==cQuote) + ){ + do{ p->n--; }while( p->z[p->n]!=cQuote ); + p->cTerm = c; + break; + } + if( pc==cQuote && c!='\r' ){ + utf8_printf(stderr, "%s:%d: unescaped %c character\n", + p->zFile, p->nLine, cQuote); + } + if( c==EOF ){ + utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", + p->zFile, startLine, cQuote); + p->cTerm = c; + break; + } + import_append_char(p, c); + ppc = pc; + pc = c; + } + }else{ + while( c!=EOF && c!=cSep && c!=rSep ){ + import_append_char(p, c); + c = fgetc(p->in); + } + if( c==rSep ){ + p->nLine++; + if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; + } + p->cTerm = c; + } + if( p->z ) p->z[p->n] = 0; + return p->z; +} + +/* Read a single field of ASCII delimited text. +** +** + Input comes from p->in. +** + Store results in p->z of length p->n. Space to hold p->z comes +** from sqlite3_malloc64(). +** + Use p->cSep as the column separator. The default is "\x1F". +** + Use p->rSep as the row separator. The default is "\x1E". +** + Keep track of the row number in p->nLine. +** + Store the character that terminates the field in p->cTerm. Store +** EOF on end-of-file. +** + Report syntax errors on stderr +*/ +static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ + int c; + int cSep = p->cColSep; + int rSep = p->cRowSep; + p->n = 0; + c = fgetc(p->in); + if( c==EOF || seenInterrupt ){ + p->cTerm = EOF; + return 0; + } + while( c!=EOF && c!=cSep && c!=rSep ){ + import_append_char(p, c); + c = fgetc(p->in); + } + if( c==rSep ){ + p->nLine++; + } + p->cTerm = c; + if( p->z ) p->z[p->n] = 0; + return p->z; +} + +/* +** Try to transfer data for table zTable. If an error is seen while +** moving forward, try to go backwards. The backwards movement won't +** work for WITHOUT ROWID tables. +*/ +static void tryToCloneData( + ShellState *p, + sqlite3 *newDb, + const char *zTable +){ + sqlite3_stmt *pQuery = 0; + sqlite3_stmt *pInsert = 0; + char *zQuery = 0; + char *zInsert = 0; + int rc; + int i, j, n; + int nTable = (int)strlen(zTable); + int k = 0; + int cnt = 0; + const int spinRate = 10000; + + zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); + rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); + if( rc ){ + utf8_printf(stderr, "Error %d: %s on [%s]\n", + sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), + zQuery); + goto end_data_xfer; + } + n = sqlite3_column_count(pQuery); + zInsert = sqlite3_malloc64(200 + nTable + n*3); + if( zInsert==0 ){ + raw_printf(stderr, "out of memory\n"); + goto end_data_xfer; + } + sqlite3_snprintf(200+nTable,zInsert, + "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); + i = (int)strlen(zInsert); + for(j=1; jdb, zQuery, -1, &pQuery, 0); + if( rc ){ + utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); + break; + } + } /* End for(k=0...) */ + +end_data_xfer: + sqlite3_finalize(pQuery); + sqlite3_finalize(pInsert); + sqlite3_free(zQuery); + sqlite3_free(zInsert); +} + + +/* +** Try to transfer all rows of the schema that match zWhere. For +** each row, invoke xForEach() on the object defined by that row. +** If an error is encountered while moving forward through the +** sqlite_master table, try again moving backwards. +*/ +static void tryToCloneSchema( + ShellState *p, + sqlite3 *newDb, + const char *zWhere, + void (*xForEach)(ShellState*,sqlite3*,const char*) +){ + sqlite3_stmt *pQuery = 0; + char *zQuery = 0; + int rc; + const unsigned char *zName; + const unsigned char *zSql; + char *zErrMsg = 0; + + zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" + " WHERE %s", zWhere); + rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); + if( rc ){ + utf8_printf(stderr, "Error: (%d) %s on [%s]\n", + sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), + zQuery); + goto end_schema_xfer; + } + while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ + zName = sqlite3_column_text(pQuery, 0); + zSql = sqlite3_column_text(pQuery, 1); + printf("%s... ", zName); fflush(stdout); + sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); + if( zErrMsg ){ + utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); + sqlite3_free(zErrMsg); + zErrMsg = 0; + } + if( xForEach ){ + xForEach(p, newDb, (const char*)zName); + } + printf("done\n"); + } + if( rc!=SQLITE_DONE ){ + sqlite3_finalize(pQuery); + sqlite3_free(zQuery); + zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" + " WHERE %s ORDER BY rowid DESC", zWhere); + rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); + if( rc ){ + utf8_printf(stderr, "Error: (%d) %s on [%s]\n", + sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), + zQuery); + goto end_schema_xfer; + } + while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ + zName = sqlite3_column_text(pQuery, 0); + zSql = sqlite3_column_text(pQuery, 1); + printf("%s... ", zName); fflush(stdout); + sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); + if( zErrMsg ){ + utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); + sqlite3_free(zErrMsg); + zErrMsg = 0; + } + if( xForEach ){ + xForEach(p, newDb, (const char*)zName); + } + printf("done\n"); + } + } +end_schema_xfer: + sqlite3_finalize(pQuery); + sqlite3_free(zQuery); +} + +/* +** Open a new database file named "zNewDb". Try to recover as much information +** as possible out of the main database (which might be corrupt) and write it +** into zNewDb. +*/ +static void tryToClone(ShellState *p, const char *zNewDb){ + int rc; + sqlite3 *newDb = 0; + if( access(zNewDb,0)==0 ){ + utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); + return; + } + rc = sqlite3_open(zNewDb, &newDb); + if( rc ){ + utf8_printf(stderr, "Cannot create output database: %s\n", + sqlite3_errmsg(newDb)); + }else{ + sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); + sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); + tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); + tryToCloneSchema(p, newDb, "type!='table'", 0); + sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); + sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); + } + sqlite3_close(newDb); +} + +/* +** Change the output file back to stdout +*/ +static void output_reset(ShellState *p){ + if( p->outfile[0]=='|' ){ +#ifndef SQLITE_OMIT_POPEN + pclose(p->out); +#endif + }else{ + output_file_close(p->out); + } + p->outfile[0] = 0; + p->out = stdout; +} + +/* +** Run an SQL command and return the single integer result. +*/ +static int db_int(ShellState *p, const char *zSql){ + sqlite3_stmt *pStmt; + int res = 0; + sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ + res = sqlite3_column_int(pStmt,0); + } + sqlite3_finalize(pStmt); + return res; +} + +/* +** Convert a 2-byte or 4-byte big-endian integer into a native integer +*/ +unsigned int get2byteInt(unsigned char *a){ + return (a[0]<<8) + a[1]; +} +unsigned int get4byteInt(unsigned char *a){ + return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; +} + +/* +** Implementation of the ".info" command. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ + static const struct { const char *zName; int ofst; } aField[] = { + { "file change counter:", 24 }, + { "database page count:", 28 }, + { "freelist page count:", 36 }, + { "schema cookie:", 40 }, + { "schema format:", 44 }, + { "default cache size:", 48 }, + { "autovacuum top root:", 52 }, + { "incremental vacuum:", 64 }, + { "text encoding:", 56 }, + { "user version:", 60 }, + { "application id:", 68 }, + { "software version:", 96 }, + }; + static const struct { const char *zName; const char *zSql; } aQuery[] = { + { "number of tables:", + "SELECT count(*) FROM %s WHERE type='table'" }, + { "number of indexes:", + "SELECT count(*) FROM %s WHERE type='index'" }, + { "number of triggers:", + "SELECT count(*) FROM %s WHERE type='trigger'" }, + { "number of views:", + "SELECT count(*) FROM %s WHERE type='view'" }, + { "schema size:", + "SELECT total(length(sql)) FROM %s" }, + }; + sqlite3_file *pFile = 0; + int i; + char *zSchemaTab; + char *zDb = nArg>=2 ? azArg[1] : "main"; + unsigned char aHdr[100]; + open_db(p, 0); + if( p->db==0 ) return 1; + sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); + if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ + return 1; + } + i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); + if( i!=SQLITE_OK ){ + raw_printf(stderr, "unable to read database header\n"); + return 1; + } + i = get2byteInt(aHdr+16); + if( i==1 ) i = 65536; + utf8_printf(p->out, "%-20s %d\n", "database page size:", i); + utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); + utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); + utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); + for(i=0; iout, "%-20s %u", aField[i].zName, val); + switch( ofst ){ + case 56: { + if( val==1 ) raw_printf(p->out, " (utf8)"); + if( val==2 ) raw_printf(p->out, " (utf16le)"); + if( val==3 ) raw_printf(p->out, " (utf16be)"); + } + } + raw_printf(p->out, "\n"); + } + if( zDb==0 ){ + zSchemaTab = sqlite3_mprintf("main.sqlite_master"); + }else if( strcmp(zDb,"temp")==0 ){ + zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); + }else{ + zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); + } + for(i=0; iout, "%-20s %d\n", aQuery[i].zName, val); + } + sqlite3_free(zSchemaTab); + return 0; +} + +/* +** Print the current sqlite3_errmsg() value to stderr and return 1. +*/ +static int shellDatabaseError(sqlite3 *db){ + const char *zErr = sqlite3_errmsg(db); + utf8_printf(stderr, "Error: %s\n", zErr); + return 1; +} + +/* +** Print an out-of-memory message to stderr and return 1. +*/ +static int shellNomemError(void){ + raw_printf(stderr, "Error: out of memory\n"); + return 1; +} + +/* +** Compare the string as a command-line option with either one or two +** initial "-" characters. +*/ +static int optionMatch(const char *zStr, const char *zOpt){ + if( zStr[0]!='-' ) return 0; + zStr++; + if( zStr[0]=='-' ) zStr++; + return strcmp(zStr, zOpt)==0; +} + +/* +** If an input line begins with "." then invoke this routine to +** process that line. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int do_meta_command(char *zLine, ShellState *p){ + int h = 1; + int nArg = 0; + int n, c; + int rc = 0; + char *azArg[50]; + + /* Parse the input line into tokens. + */ + while( zLine[h] && nArgdb, shellAuth, p); + }else{ + sqlite3_set_authorizer(p->db, 0, 0); + } + }else + + if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) + || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) + ){ + const char *zDestFile = 0; + const char *zDb = 0; + sqlite3 *pDest; + sqlite3_backup *pBackup; + int j; + for(j=1; jdb, zDb); + if( pBackup==0 ){ + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + sqlite3_close(pDest); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else{ + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + rc = 1; + } + sqlite3_close(pDest); + }else + + if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ + if( nArg==2 ){ + bail_on_error = booleanValue(azArg[1]); + }else{ + raw_printf(stderr, "Usage: .bail on|off\n"); + rc = 1; + } + }else + + if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ + if( nArg==2 ){ + if( booleanValue(azArg[1]) ){ + setBinaryMode(p->out, 1); + }else{ + setTextMode(p->out, 1); + } + }else{ + raw_printf(stderr, "Usage: .binary on|off\n"); + rc = 1; + } + }else + + /* The undocumented ".breakpoint" command causes a call to the no-op + ** routine named test_breakpoint(). + */ + if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ + test_breakpoint(); + }else + + if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ + if( nArg==2 ){ + p->countChanges = booleanValue(azArg[1]); + }else{ + raw_printf(stderr, "Usage: .changes on|off\n"); + rc = 1; + } + }else + + if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ + if( nArg==2 ){ + tryToClone(p, azArg[1]); + }else{ + raw_printf(stderr, "Usage: .clone FILENAME\n"); + rc = 1; + } + }else + + if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ + ShellState data; + char *zErrMsg = 0; + open_db(p, 0); + memcpy(&data, p, sizeof(data)); + data.showHeader = 1; + data.cMode = data.mode = MODE_Column; + data.colWidth[0] = 3; + data.colWidth[1] = 15; + data.colWidth[2] = 58; + data.cnt = 0; + sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); + if( zErrMsg ){ + utf8_printf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + } + }else + + if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ + rc = shell_dbinfo_command(p, nArg, azArg); + }else + + if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ + open_db(p, 0); + /* When playing back a "dump", the content might appear in an order + ** which causes immediate foreign key constraints to be violated. + ** So disable foreign-key constraint enforcement to prevent problems. */ + if( nArg!=1 && nArg!=2 ){ + raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n"); + rc = 1; + goto meta_command_exit; + } + raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); + raw_printf(p->out, "BEGIN TRANSACTION;\n"); + p->writableSchema = 0; + sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); + p->nErr = 0; + if( nArg==1 ){ + run_schema_dump_query(p, + "SELECT name, type, sql FROM sqlite_master " + "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" + ); + run_schema_dump_query(p, + "SELECT name, type, sql FROM sqlite_master " + "WHERE name=='sqlite_sequence'" + ); + run_table_dump_query(p, + "SELECT sql FROM sqlite_master " + "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 + ); + }else{ + int i; + for(i=1; iwritableSchema ){ + raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); + p->writableSchema = 0; + } + sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); + sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); + raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); + }else + + if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ + if( nArg==2 ){ + p->echoOn = booleanValue(azArg[1]); + }else{ + raw_printf(stderr, "Usage: .echo on|off\n"); + rc = 1; + } + }else + + if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ + if( nArg==2 ){ + if( strcmp(azArg[1],"full")==0 ){ + p->autoEQP = 2; + }else{ + p->autoEQP = booleanValue(azArg[1]); + } + }else{ + raw_printf(stderr, "Usage: .eqp on|off|full\n"); + rc = 1; + } + }else + + if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ + if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); + rc = 2; + }else + + if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ + int val = 1; + if( nArg>=2 ){ + if( strcmp(azArg[1],"auto")==0 ){ + val = 99; + }else{ + val = booleanValue(azArg[1]); + } + } + if( val==1 && p->mode!=MODE_Explain ){ + p->normalMode = p->mode; + p->mode = MODE_Explain; + p->autoExplain = 0; + }else if( val==0 ){ + if( p->mode==MODE_Explain ) p->mode = p->normalMode; + p->autoExplain = 0; + }else if( val==99 ){ + if( p->mode==MODE_Explain ) p->mode = p->normalMode; + p->autoExplain = 1; + } + }else + + if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ + ShellState data; + char *zErrMsg = 0; + int doStats = 0; + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.cMode = data.mode = MODE_Semi; + if( nArg==2 && optionMatch(azArg[1], "indent") ){ + data.cMode = data.mode = MODE_Pretty; + nArg = 1; + } + if( nArg!=1 ){ + raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + rc = sqlite3_exec(p->db, + "SELECT sql FROM" + " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" + " FROM sqlite_master UNION ALL" + " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " + "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " + "ORDER BY rowid", + callback, &data, &zErrMsg + ); + if( rc==SQLITE_OK ){ + sqlite3_stmt *pStmt; + rc = sqlite3_prepare_v2(p->db, + "SELECT rowid FROM sqlite_master" + " WHERE name GLOB 'sqlite_stat[134]'", + -1, &pStmt, 0); + doStats = sqlite3_step(pStmt)==SQLITE_ROW; + sqlite3_finalize(pStmt); + } + if( doStats==0 ){ + raw_printf(p->out, "/* No STAT tables available */\n"); + }else{ + raw_printf(p->out, "ANALYZE sqlite_master;\n"); + sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", + callback, &data, &zErrMsg); + data.cMode = data.mode = MODE_Insert; + data.zDestTable = "sqlite_stat1"; + shell_exec(p->db, "SELECT * FROM sqlite_stat1", + shell_callback, &data,&zErrMsg); + data.zDestTable = "sqlite_stat3"; + shell_exec(p->db, "SELECT * FROM sqlite_stat3", + shell_callback, &data,&zErrMsg); + data.zDestTable = "sqlite_stat4"; + shell_exec(p->db, "SELECT * FROM sqlite_stat4", + shell_callback, &data, &zErrMsg); + raw_printf(p->out, "ANALYZE sqlite_master;\n"); + } + }else + + if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ + if( nArg==2 ){ + p->showHeader = booleanValue(azArg[1]); + }else{ + raw_printf(stderr, "Usage: .headers on|off\n"); + rc = 1; + } + }else + + if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ + utf8_printf(p->out, "%s", zHelp); + }else + + if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ + char *zTable; /* Insert data into this table */ + char *zFile; /* Name of file to extra content from */ + sqlite3_stmt *pStmt = NULL; /* A statement */ + int nCol; /* Number of columns in the table */ + int nByte; /* Number of bytes in an SQL string */ + int i, j; /* Loop counters */ + int needCommit; /* True to COMMIT or ROLLBACK at end */ + int nSep; /* Number of bytes in p->colSeparator[] */ + char *zSql; /* An SQL statement */ + ImportCtx sCtx; /* Reader context */ + char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ + int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ + + if( nArg!=3 ){ + raw_printf(stderr, "Usage: .import FILE TABLE\n"); + goto meta_command_exit; + } + zFile = azArg[1]; + zTable = azArg[2]; + seenInterrupt = 0; + memset(&sCtx, 0, sizeof(sCtx)); + open_db(p, 0); + nSep = strlen30(p->colSeparator); + if( nSep==0 ){ + raw_printf(stderr, + "Error: non-null column separator required for import\n"); + return 1; + } + if( nSep>1 ){ + raw_printf(stderr, "Error: multi-character column separators not allowed" + " for import\n"); + return 1; + } + nSep = strlen30(p->rowSeparator); + if( nSep==0 ){ + raw_printf(stderr, "Error: non-null row separator required for import\n"); + return 1; + } + if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ + /* When importing CSV (only), if the row separator is set to the + ** default output row separator, change it to the default input + ** row separator. This avoids having to maintain different input + ** and output row separators. */ + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + nSep = strlen30(p->rowSeparator); + } + if( nSep>1 ){ + raw_printf(stderr, "Error: multi-character row separators not allowed" + " for import\n"); + return 1; + } + sCtx.zFile = zFile; + sCtx.nLine = 1; + if( sCtx.zFile[0]=='|' ){ +#ifdef SQLITE_OMIT_POPEN + raw_printf(stderr, "Error: pipes are not supported in this OS\n"); + return 1; +#else + sCtx.in = popen(sCtx.zFile+1, "r"); + sCtx.zFile = ""; + xCloser = pclose; +#endif + }else{ + sCtx.in = fopen(sCtx.zFile, "rb"); + xCloser = fclose; + } + if( p->mode==MODE_Ascii ){ + xRead = ascii_read_one_field; + }else{ + xRead = csv_read_one_field; + } + if( sCtx.in==0 ){ + utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); + return 1; + } + sCtx.cColSep = p->colSeparator[0]; + sCtx.cRowSep = p->rowSeparator[0]; + zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); + if( zSql==0 ){ + raw_printf(stderr, "Error: out of memory\n"); + xCloser(sCtx.in); + return 1; + } + nByte = strlen30(zSql); + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ + if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ + char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); + char cSep = '('; + while( xRead(&sCtx) ){ + zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); + cSep = ','; + if( sCtx.cTerm!=sCtx.cColSep ) break; + } + if( cSep=='(' ){ + sqlite3_free(zCreate); + sqlite3_free(sCtx.z); + xCloser(sCtx.in); + utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); + return 1; + } + zCreate = sqlite3_mprintf("%z\n)", zCreate); + rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); + sqlite3_free(zCreate); + if( rc ){ + utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, + sqlite3_errmsg(p->db)); + sqlite3_free(sCtx.z); + xCloser(sCtx.in); + return 1; + } + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + } + sqlite3_free(zSql); + if( rc ){ + if (pStmt) sqlite3_finalize(pStmt); + utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); + xCloser(sCtx.in); + return 1; + } + nCol = sqlite3_column_count(pStmt); + sqlite3_finalize(pStmt); + pStmt = 0; + if( nCol==0 ) return 0; /* no columns, no error */ + zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); + if( zSql==0 ){ + raw_printf(stderr, "Error: out of memory\n"); + xCloser(sCtx.in); + return 1; + } + sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); + j = strlen30(zSql); + for(i=1; idb, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rc ){ + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + if (pStmt) sqlite3_finalize(pStmt); + xCloser(sCtx.in); + return 1; + } + needCommit = sqlite3_get_autocommit(p->db); + if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); + do{ + int startLine = sCtx.nLine; + for(i=0; imode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; + sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); + if( i=nCol ){ + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ){ + utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, + startLine, sqlite3_errmsg(p->db)); + } + } + }while( sCtx.cTerm!=EOF ); + + xCloser(sCtx.in); + sqlite3_free(sCtx.z); + sqlite3_finalize(pStmt); + if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); + }else + + if( c=='i' && (strncmp(azArg[0], "indices", n)==0 + || strncmp(azArg[0], "indexes", n)==0) ){ + ShellState data; + char *zErrMsg = 0; + open_db(p, 0); + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.cMode = data.mode = MODE_List; + if( nArg==1 ){ + rc = sqlite3_exec(p->db, + "SELECT name FROM sqlite_master " + "WHERE type='index' AND name NOT LIKE 'sqlite_%' " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type='index' " + "ORDER BY 1", + callback, &data, &zErrMsg + ); + }else if( nArg==2 ){ + zShellStatic = azArg[1]; + rc = sqlite3_exec(p->db, + "SELECT name FROM sqlite_master " + "WHERE type='index' AND tbl_name LIKE shellstatic() " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type='index' AND tbl_name LIKE shellstatic() " + "ORDER BY 1", + callback, &data, &zErrMsg + ); + zShellStatic = 0; + }else{ + raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); + rc = 1; + goto meta_command_exit; + } + if( zErrMsg ){ + utf8_printf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + raw_printf(stderr, + "Error: querying sqlite_master and sqlite_temp_master\n"); + rc = 1; + } + }else + +#ifdef SQLITE_ENABLE_IOTRACE + if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ + SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); + if( iotrace && iotrace!=stdout ) fclose(iotrace); + iotrace = 0; + if( nArg<2 ){ + sqlite3IoTrace = 0; + }else if( strcmp(azArg[1], "-")==0 ){ + sqlite3IoTrace = iotracePrintf; + iotrace = stdout; + }else{ + iotrace = fopen(azArg[1], "w"); + if( iotrace==0 ){ + utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); + sqlite3IoTrace = 0; + rc = 1; + }else{ + sqlite3IoTrace = iotracePrintf; + } + } + }else +#endif + if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ + static const struct { + const char *zLimitName; /* Name of a limit */ + int limitCode; /* Integer code for that limit */ + } aLimit[] = { + { "length", SQLITE_LIMIT_LENGTH }, + { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, + { "column", SQLITE_LIMIT_COLUMN }, + { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, + { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, + { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, + { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, + { "attached", SQLITE_LIMIT_ATTACHED }, + { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, + { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, + { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, + { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, + }; + int i, n2; + open_db(p, 0); + if( nArg==1 ){ + for(i=0; idb, aLimit[i].limitCode, -1)); + } + }else if( nArg>3 ){ + raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); + rc = 1; + goto meta_command_exit; + }else{ + int iLimit = -1; + n2 = strlen30(azArg[1]); + for(i=0; idb, aLimit[iLimit].limitCode, + (int)integerValue(azArg[2])); + } + printf("%20s %d\n", aLimit[iLimit].zLimitName, + sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); + } + }else + +#ifndef SQLITE_OMIT_LOAD_EXTENSION + if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ + const char *zFile, *zProc; + char *zErrMsg = 0; + if( nArg<2 ){ + raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); + rc = 1; + goto meta_command_exit; + } + zFile = azArg[1]; + zProc = nArg>=3 ? azArg[2] : 0; + open_db(p, 0); + rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); + if( rc!=SQLITE_OK ){ + utf8_printf(stderr, "Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + } + }else +#endif + + if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ + if( nArg!=2 ){ + raw_printf(stderr, "Usage: .log FILENAME\n"); + rc = 1; + }else{ + const char *zFile = azArg[1]; + output_file_close(p->pLog); + p->pLog = output_file_open(zFile); + } + }else + + if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ + const char *zMode = nArg>=2 ? azArg[1] : ""; + int n2 = (int)strlen(zMode); + int c2 = zMode[0]; + if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ + p->mode = MODE_Line; + }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ + p->mode = MODE_Column; + }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ + p->mode = MODE_List; + }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ + p->mode = MODE_Html; + }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ + p->mode = MODE_Tcl; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); + }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ + p->mode = MODE_Csv; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); + }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ + p->mode = MODE_List; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); + }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ + p->mode = MODE_Insert; + set_table_name(p, nArg>=3 ? azArg[2] : "table"); + }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ + p->mode = MODE_Ascii; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); + }else { + raw_printf(stderr, "Error: mode should be one of: " + "ascii column csv html insert line list tabs tcl\n"); + rc = 1; + } + p->cMode = p->mode; + }else + + if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ + if( nArg==2 ){ + sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, + "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); + }else{ + raw_printf(stderr, "Usage: .nullvalue STRING\n"); + rc = 1; + } + }else + + if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ + sqlite3 *savedDb = p->db; + const char *zSavedFilename = p->zDbFilename; + char *zNewFilename = 0; + p->db = 0; + if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]); + p->zDbFilename = zNewFilename; + open_db(p, 1); + if( p->db!=0 ){ + session_close_all(p); + sqlite3_close(savedDb); + sqlite3_free(p->zFreeOnClose); + p->zFreeOnClose = zNewFilename; + }else{ + sqlite3_free(zNewFilename); + p->db = savedDb; + p->zDbFilename = zSavedFilename; + } + }else + + if( c=='o' + && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) + ){ + const char *zFile = nArg>=2 ? azArg[1] : "stdout"; + if( nArg>2 ){ + utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); + rc = 1; + goto meta_command_exit; + } + if( n>1 && strncmp(azArg[0], "once", n)==0 ){ + if( nArg<2 ){ + raw_printf(stderr, "Usage: .once FILE\n"); + rc = 1; + goto meta_command_exit; + } + p->outCount = 2; + }else{ + p->outCount = 0; + } + output_reset(p); + if( zFile[0]=='|' ){ +#ifdef SQLITE_OMIT_POPEN + raw_printf(stderr, "Error: pipes are not supported in this OS\n"); + rc = 1; + p->out = stdout; +#else + p->out = popen(zFile + 1, "w"); + if( p->out==0 ){ + utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); + p->out = stdout; + rc = 1; + }else{ + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); + } +#endif + }else{ + p->out = output_file_open(zFile); + if( p->out==0 ){ + if( strcmp(zFile,"off")!=0 ){ + utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); + } + p->out = stdout; + rc = 1; + } else { + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); + } + } + }else + + if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ + int i; + for(i=1; i1 ) raw_printf(p->out, " "); + utf8_printf(p->out, "%s", azArg[i]); + } + raw_printf(p->out, "\n"); + }else + + if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ + if( nArg >= 2) { + strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); + } + if( nArg >= 3) { + strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); + } + }else + + if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ + rc = 2; + }else + + if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ + FILE *alt; + if( nArg!=2 ){ + raw_printf(stderr, "Usage: .read FILE\n"); + rc = 1; + goto meta_command_exit; + } + alt = fopen(azArg[1], "rb"); + if( alt==0 ){ + utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); + rc = 1; + }else{ + rc = process_input(p, alt); + fclose(alt); + } + }else + + if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ + const char *zSrcFile; + const char *zDb; + sqlite3 *pSrc; + sqlite3_backup *pBackup; + int nTimeout = 0; + + if( nArg==2 ){ + zSrcFile = azArg[1]; + zDb = "main"; + }else if( nArg==3 ){ + zSrcFile = azArg[2]; + zDb = azArg[1]; + }else{ + raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_open(zSrcFile, &pSrc); + if( rc!=SQLITE_OK ){ + utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); + sqlite3_close(pSrc); + return 1; + } + open_db(p, 0); + pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); + if( pBackup==0 ){ + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + sqlite3_close(pSrc); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK + || rc==SQLITE_BUSY ){ + if( rc==SQLITE_BUSY ){ + if( nTimeout++ >= 3 ) break; + sqlite3_sleep(100); + } + } + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + raw_printf(stderr, "Error: source database is busy\n"); + rc = 1; + }else{ + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + rc = 1; + } + sqlite3_close(pSrc); + }else + + + if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ + if( nArg==2 ){ + p->scanstatsOn = booleanValue(azArg[1]); +#ifndef SQLITE_ENABLE_STMT_SCANSTATUS + raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); +#endif + }else{ + raw_printf(stderr, "Usage: .scanstats on|off\n"); + rc = 1; + } + }else + + if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ + ShellState data; + char *zErrMsg = 0; + open_db(p, 0); + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.cMode = data.mode = MODE_Semi; + if( nArg>=2 && optionMatch(azArg[1], "indent") ){ + data.cMode = data.mode = MODE_Pretty; + nArg--; + if( nArg==2 ) azArg[1] = azArg[2]; + } + if( nArg==2 && azArg[1][0]!='-' ){ + int i; + for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); + if( strcmp(azArg[1],"sqlite_master")==0 ){ + char *new_argv[2], *new_colv[2]; + new_argv[0] = "CREATE TABLE sqlite_master (\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")"; + new_argv[1] = 0; + new_colv[0] = "sql"; + new_colv[1] = 0; + callback(&data, 1, new_argv, new_colv); + rc = SQLITE_OK; + }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ + char *new_argv[2], *new_colv[2]; + new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")"; + new_argv[1] = 0; + new_colv[0] = "sql"; + new_colv[1] = 0; + callback(&data, 1, new_argv, new_colv); + rc = SQLITE_OK; + }else{ + zShellStatic = azArg[1]; + rc = sqlite3_exec(p->db, + "SELECT sql FROM " + " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" + " FROM sqlite_master UNION ALL" + " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " + "WHERE lower(tbl_name) LIKE shellstatic()" + " AND type!='meta' AND sql NOTNULL " + "ORDER BY rowid", + callback, &data, &zErrMsg); + zShellStatic = 0; + } + }else if( nArg==1 ){ + rc = sqlite3_exec(p->db, + "SELECT sql FROM " + " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" + " FROM sqlite_master UNION ALL" + " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " + "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " + "ORDER BY rowid", + callback, &data, &zErrMsg + ); + }else{ + raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); + rc = 1; + goto meta_command_exit; + } + if( zErrMsg ){ + utf8_printf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + raw_printf(stderr,"Error: querying schema information\n"); + rc = 1; + }else{ + rc = 0; + } + }else + +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) + if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ + sqlite3SelectTrace = integerValue(azArg[1]); + }else +#endif + +#if defined(SQLITE_ENABLE_SESSION) + if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ + OpenSession *pSession = &p->aSession[0]; + char **azCmd = &azArg[1]; + int iSes = 0; + int nCmd = nArg - 1; + int i; + if( nArg<=1 ) goto session_syntax_error; + open_db(p, 0); + if( nArg>=3 ){ + for(iSes=0; iSesnSession; iSes++){ + if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; + } + if( iSesnSession ){ + pSession = &p->aSession[iSes]; + azCmd++; + nCmd--; + }else{ + pSession = &p->aSession[0]; + iSes = 0; + } + } + + /* .session attach TABLE + ** Invoke the sqlite3session_attach() interface to attach a particular + ** table so that it is never filtered. + */ + if( strcmp(azCmd[0],"attach")==0 ){ + if( nCmd!=2 ) goto session_syntax_error; + if( pSession->p==0 ){ + session_not_open: + raw_printf(stderr, "ERROR: No sessions are open\n"); + }else{ + rc = sqlite3session_attach(pSession->p, azCmd[1]); + if( rc ){ + raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); + rc = 0; + } + } + }else + + /* .session changeset FILE + ** .session patchset FILE + ** Write a changeset or patchset into a file. The file is overwritten. + */ + if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ + FILE *out = 0; + if( nCmd!=2 ) goto session_syntax_error; + if( pSession->p==0 ) goto session_not_open; + out = fopen(azCmd[1], "wb"); + if( out==0 ){ + utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); + }else{ + int szChng; + void *pChng; + if( azCmd[0][0]=='c' ){ + rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); + }else{ + rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); + } + if( rc ){ + printf("Error: error code %d\n", rc); + rc = 0; + } + if( pChng + && fwrite(pChng, szChng, 1, out)!=1 ){ + raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", + szChng); + } + sqlite3_free(pChng); + fclose(out); + } + }else + + /* .session close + ** Close the identified session + */ + if( strcmp(azCmd[0], "close")==0 ){ + if( nCmd!=1 ) goto session_syntax_error; + if( p->nSession ){ + session_close(pSession); + p->aSession[iSes] = p->aSession[--p->nSession]; + } + }else + + /* .session enable ?BOOLEAN? + ** Query or set the enable flag + */ + if( strcmp(azCmd[0], "enable")==0 ){ + int ii; + if( nCmd>2 ) goto session_syntax_error; + ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); + if( p->nSession ){ + ii = sqlite3session_enable(pSession->p, ii); + utf8_printf(p->out, "session %s enable flag = %d\n", + pSession->zName, ii); + } + }else + + /* .session filter GLOB .... + ** Set a list of GLOB patterns of table names to be excluded. + */ + if( strcmp(azCmd[0], "filter")==0 ){ + int ii, nByte; + if( nCmd<2 ) goto session_syntax_error; + if( p->nSession ){ + for(ii=0; iinFilter; ii++){ + sqlite3_free(pSession->azFilter[ii]); + } + sqlite3_free(pSession->azFilter); + nByte = sizeof(pSession->azFilter[0])*(nCmd-1); + pSession->azFilter = sqlite3_malloc( nByte ); + if( pSession->azFilter==0 ){ + raw_printf(stderr, "Error: out or memory\n"); + exit(1); + } + for(ii=1; iiazFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); + } + pSession->nFilter = ii-1; + } + }else + + /* .session indirect ?BOOLEAN? + ** Query or set the indirect flag + */ + if( strcmp(azCmd[0], "indirect")==0 ){ + int ii; + if( nCmd>2 ) goto session_syntax_error; + ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); + if( p->nSession ){ + ii = sqlite3session_indirect(pSession->p, ii); + utf8_printf(p->out, "session %s indirect flag = %d\n", + pSession->zName, ii); + } + }else + + /* .session isempty + ** Determine if the session is empty + */ + if( strcmp(azCmd[0], "isempty")==0 ){ + int ii; + if( nCmd!=1 ) goto session_syntax_error; + if( p->nSession ){ + ii = sqlite3session_isempty(pSession->p); + utf8_printf(p->out, "session %s isempty flag = %d\n", + pSession->zName, ii); + } + }else + + /* .session list + ** List all currently open sessions + */ + if( strcmp(azCmd[0],"list")==0 ){ + for(i=0; inSession; i++){ + utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); + } + }else + + /* .session open DB NAME + ** Open a new session called NAME on the attached database DB. + ** DB is normally "main". + */ + if( strcmp(azCmd[0],"open")==0 ){ + char *zName; + if( nCmd!=3 ) goto session_syntax_error; + zName = azCmd[2]; + if( zName[0]==0 ) goto session_syntax_error; + for(i=0; inSession; i++){ + if( strcmp(p->aSession[i].zName,zName)==0 ){ + utf8_printf(stderr, "Session \"%s\" already exists\n", zName); + goto meta_command_exit; + } + } + if( p->nSession>=ArraySize(p->aSession) ){ + raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); + goto meta_command_exit; + } + pSession = &p->aSession[p->nSession]; + rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); + if( rc ){ + raw_printf(stderr, "Cannot open session: error code=%d\n", rc); + rc = 0; + goto meta_command_exit; + } + pSession->nFilter = 0; + sqlite3session_table_filter(pSession->p, session_filter, pSession); + p->nSession++; + pSession->zName = sqlite3_mprintf("%s", zName); + }else + /* If no command name matches, show a syntax error */ + session_syntax_error: + session_help(p); + }else +#endif + +#ifdef SQLITE_DEBUG + /* Undocumented commands for internal testing. Subject to change + ** without notice. */ + if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ + if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ + int i, v; + for(i=1; iout, "%s: %d 0x%x\n", azArg[i], v, v); + } + } + if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ + int i; sqlite3_int64 v; + for(i=1; iout, "%s", zBuf); + } + } + }else +#endif + + if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ + if( nArg<2 || nArg>3 ){ + raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); + rc = 1; + } + if( nArg>=2 ){ + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, + "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); + } + if( nArg>=3 ){ + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, + "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); + } + }else + + if( c=='s' + && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) + ){ + char *zCmd; + int i, x; + if( nArg<2 ){ + raw_printf(stderr, "Usage: .system COMMAND\n"); + rc = 1; + goto meta_command_exit; + } + zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); + for(i=2; iout, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]); + utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); + utf8_printf(p->out, "%12.12s: %s\n","explain", + p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); + utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); + utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); + utf8_printf(p->out, "%12.12s: ", "nullvalue"); + output_c_string(p->out, p->nullValue); + raw_printf(p->out, "\n"); + utf8_printf(p->out,"%12.12s: %s\n","output", + strlen30(p->outfile) ? p->outfile : "stdout"); + utf8_printf(p->out,"%12.12s: ", "colseparator"); + output_c_string(p->out, p->colSeparator); + raw_printf(p->out, "\n"); + utf8_printf(p->out,"%12.12s: ", "rowseparator"); + output_c_string(p->out, p->rowSeparator); + raw_printf(p->out, "\n"); + utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); + utf8_printf(p->out, "%12.12s: ", "width"); + for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { + raw_printf(p->out, "%d ", p->colWidth[i]); + } + raw_printf(p->out, "\n"); + }else + + if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ + if( nArg==2 ){ + p->statsOn = booleanValue(azArg[1]); + }else if( nArg==1 ){ + display_stats(p->db, p, 0); + }else{ + raw_printf(stderr, "Usage: .stats ?on|off?\n"); + rc = 1; + } + }else + + if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ + sqlite3_stmt *pStmt; + char **azResult; + int nRow, nAlloc; + char *zSql = 0; + int ii; + open_db(p, 0); + rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); + if( rc ) return shellDatabaseError(p->db); + + /* Create an SQL statement to query for the list of tables in the + ** main and all attached databases where the table name matches the + ** LIKE pattern bound to variable "?1". */ + zSql = sqlite3_mprintf( + "SELECT name FROM sqlite_master" + " WHERE type IN ('table','view')" + " AND name NOT LIKE 'sqlite_%%'" + " AND name LIKE ?1"); + while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); + if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue; + if( strcmp(zDbName,"temp")==0 ){ + zSql = sqlite3_mprintf( + "%z UNION ALL " + "SELECT 'temp.' || name FROM sqlite_temp_master" + " WHERE type IN ('table','view')" + " AND name NOT LIKE 'sqlite_%%'" + " AND name LIKE ?1", zSql); + }else{ + zSql = sqlite3_mprintf( + "%z UNION ALL " + "SELECT '%q.' || name FROM \"%w\".sqlite_master" + " WHERE type IN ('table','view')" + " AND name NOT LIKE 'sqlite_%%'" + " AND name LIKE ?1", zSql, zDbName, zDbName); + } + } + rc = sqlite3_finalize(pStmt); + if( zSql && rc==SQLITE_OK ){ + zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); + if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + } + sqlite3_free(zSql); + if( !zSql ) return shellNomemError(); + if( rc ) return shellDatabaseError(p->db); + + /* Run the SQL statement prepared by the above block. Store the results + ** as an array of nul-terminated strings in azResult[]. */ + nRow = nAlloc = 0; + azResult = 0; + if( nArg>1 ){ + sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); + }else{ + sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); + } + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + if( nRow>=nAlloc ){ + char **azNew; + int n2 = nAlloc*2 + 10; + azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); + if( azNew==0 ){ + rc = shellNomemError(); + break; + } + nAlloc = n2; + azResult = azNew; + } + azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); + if( 0==azResult[nRow] ){ + rc = shellNomemError(); + break; + } + nRow++; + } + if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ + rc = shellDatabaseError(p->db); + } + + /* Pretty-print the contents of array azResult[] to the output */ + if( rc==0 && nRow>0 ){ + int len, maxlen = 0; + int i, j; + int nPrintCol, nPrintRow; + for(i=0; imaxlen ) maxlen = len; + } + nPrintCol = 80/(maxlen+2); + if( nPrintCol<1 ) nPrintCol = 1; + nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; + for(i=0; iout, "%s%-*s", zSp, maxlen, + azResult[j] ? azResult[j]:""); + } + raw_printf(p->out, "\n"); + } + } + + for(ii=0; ii=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ + static const struct { + const char *zCtrlName; /* Name of a test-control option */ + int ctrlCode; /* Integer code for that option */ + } aCtrl[] = { + { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, + { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, + { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, + { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, + { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, + { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, + { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, + { "assert", SQLITE_TESTCTRL_ASSERT }, + { "always", SQLITE_TESTCTRL_ALWAYS }, + { "reserve", SQLITE_TESTCTRL_RESERVE }, + { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, + { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, + { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, + { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, + { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, + { "imposter", SQLITE_TESTCTRL_IMPOSTER }, + }; + int testctrl = -1; + int rc2 = 0; + int i, n2; + open_db(p, 0); + + /* convert testctrl text option to value. allow any unique prefix + ** of the option name, or a numerical value. */ + n2 = strlen30(azArg[1]); + for(i=0; iSQLITE_TESTCTRL_LAST) ){ + utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); + }else{ + switch(testctrl){ + + /* sqlite3_test_control(int, db, int) */ + case SQLITE_TESTCTRL_OPTIMIZATIONS: + case SQLITE_TESTCTRL_RESERVE: + if( nArg==3 ){ + int opt = (int)strtol(azArg[2], 0, 0); + rc2 = sqlite3_test_control(testctrl, p->db, opt); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + } else { + utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", + azArg[1]); + } + break; + + /* sqlite3_test_control(int) */ + case SQLITE_TESTCTRL_PRNG_SAVE: + case SQLITE_TESTCTRL_PRNG_RESTORE: + case SQLITE_TESTCTRL_PRNG_RESET: + case SQLITE_TESTCTRL_BYTEORDER: + if( nArg==2 ){ + rc2 = sqlite3_test_control(testctrl); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + } else { + utf8_printf(stderr,"Error: testctrl %s takes no options\n", + azArg[1]); + } + break; + + /* sqlite3_test_control(int, uint) */ + case SQLITE_TESTCTRL_PENDING_BYTE: + if( nArg==3 ){ + unsigned int opt = (unsigned int)integerValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, opt); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + } else { + utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" + " int option\n", azArg[1]); + } + break; + + /* sqlite3_test_control(int, int) */ + case SQLITE_TESTCTRL_ASSERT: + case SQLITE_TESTCTRL_ALWAYS: + case SQLITE_TESTCTRL_NEVER_CORRUPT: + if( nArg==3 ){ + int opt = booleanValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, opt); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + } else { + utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", + azArg[1]); + } + break; + + /* sqlite3_test_control(int, char *) */ +#ifdef SQLITE_N_KEYWORD + case SQLITE_TESTCTRL_ISKEYWORD: + if( nArg==3 ){ + const char *opt = azArg[2]; + rc2 = sqlite3_test_control(testctrl, opt); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + } else { + utf8_printf(stderr, + "Error: testctrl %s takes a single char * option\n", + azArg[1]); + } + break; +#endif + + case SQLITE_TESTCTRL_IMPOSTER: + if( nArg==5 ){ + rc2 = sqlite3_test_control(testctrl, p->db, + azArg[2], + integerValue(azArg[3]), + integerValue(azArg[4])); + raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); + }else{ + raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); + } + break; + + case SQLITE_TESTCTRL_BITVEC_TEST: + case SQLITE_TESTCTRL_FAULT_INSTALL: + case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: + case SQLITE_TESTCTRL_SCRATCHMALLOC: + default: + utf8_printf(stderr, + "Error: CLI support for testctrl %s not implemented\n", + azArg[1]); + break; + } + } + }else + + if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ + open_db(p, 0); + sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); + }else + + if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ + if( nArg==2 ){ + enableTimer = booleanValue(azArg[1]); + if( enableTimer && !HAS_TIMER ){ + raw_printf(stderr, "Error: timer not available on this system.\n"); + enableTimer = 0; + } + }else{ + raw_printf(stderr, "Usage: .timer on|off\n"); + rc = 1; + } + }else + + if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ + open_db(p, 0); + if( nArg!=2 ){ + raw_printf(stderr, "Usage: .trace FILE|off\n"); + rc = 1; + goto meta_command_exit; + } + output_file_close(p->traceOut); + p->traceOut = output_file_open(azArg[1]); +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) + if( p->traceOut==0 ){ + sqlite3_trace(p->db, 0, 0); + }else{ + sqlite3_trace(p->db, sql_trace_callback, p->traceOut); + } +#endif + }else + +#if SQLITE_USER_AUTHENTICATION + if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ + if( nArg<2 ){ + raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + if( strcmp(azArg[1],"login")==0 ){ + if( nArg!=4 ){ + raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], + (int)strlen(azArg[3])); + if( rc ){ + utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); + rc = 1; + } + }else if( strcmp(azArg[1],"add")==0 ){ + if( nArg!=5 ){ + raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_user_add(p->db, azArg[2], + azArg[3], (int)strlen(azArg[3]), + booleanValue(azArg[4])); + if( rc ){ + raw_printf(stderr, "User-Add failed: %d\n", rc); + rc = 1; + } + }else if( strcmp(azArg[1],"edit")==0 ){ + if( nArg!=5 ){ + raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_user_change(p->db, azArg[2], + azArg[3], (int)strlen(azArg[3]), + booleanValue(azArg[4])); + if( rc ){ + raw_printf(stderr, "User-Edit failed: %d\n", rc); + rc = 1; + } + }else if( strcmp(azArg[1],"delete")==0 ){ + if( nArg!=3 ){ + raw_printf(stderr, "Usage: .user delete USER\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_user_delete(p->db, azArg[2]); + if( rc ){ + raw_printf(stderr, "User-Delete failed: %d\n", rc); + rc = 1; + } + }else{ + raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); + rc = 1; + goto meta_command_exit; + } + }else +#endif /* SQLITE_USER_AUTHENTICATION */ + + if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ + utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, + sqlite3_libversion(), sqlite3_sourceid()); + }else + + if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ + const char *zDbName = nArg==2 ? azArg[1] : "main"; + sqlite3_vfs *pVfs; + if( p->db ){ + sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); + if( pVfs ){ + utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); + raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); + raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); + raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); + } + } + }else + + if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ + sqlite3_vfs *pVfs; + sqlite3_vfs *pCurrent = 0; + if( p->db ){ + sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); + } + for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ + utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, + pVfs==pCurrent ? " <--- CURRENT" : ""); + raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); + raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); + raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); + if( pVfs->pNext ){ + raw_printf(p->out, "-----------------------------------\n"); + } + } + }else + + if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ + const char *zDbName = nArg==2 ? azArg[1] : "main"; + char *zVfsName = 0; + if( p->db ){ + sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); + if( zVfsName ){ + utf8_printf(p->out, "%s\n", zVfsName); + sqlite3_free(zVfsName); + } + } + }else + +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) + if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ + sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; + }else +#endif + + if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ + int j; + assert( nArg<=ArraySize(azArg) ); + for(j=1; jcolWidth); j++){ + p->colWidth[j-1] = (int)integerValue(azArg[j]); + } + }else + + { + utf8_printf(stderr, "Error: unknown command or invalid arguments: " + " \"%s\". Enter \".help\" for help\n", azArg[0]); + rc = 1; + } + +meta_command_exit: + if( p->outCount ){ + p->outCount--; + if( p->outCount==0 ) output_reset(p); + } + return rc; +} + +/* +** Return TRUE if a semicolon occurs anywhere in the first N characters +** of string z[]. +*/ +static int line_contains_semicolon(const char *z, int N){ + int i; + for(i=0; iout); + zLine = one_input_line(in, zLine, nSql>0); + if( zLine==0 ){ + /* End of input */ + if( stdin_is_interactive ) printf("\n"); + break; + } + if( seenInterrupt ){ + if( in!=0 ) break; + seenInterrupt = 0; + } + lineno++; + if( nSql==0 && _all_whitespace(zLine) ){ + if( p->echoOn ) printf("%s\n", zLine); + continue; + } + if( zLine && zLine[0]=='.' && nSql==0 ){ + if( p->echoOn ) printf("%s\n", zLine); + rc = do_meta_command(zLine, p); + if( rc==2 ){ /* exit requested */ + break; + }else if( rc ){ + errCnt++; + } + continue; + } + if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ + memcpy(zLine,";",2); + } + nLine = strlen30(zLine); + if( nSql+nLine+2>=nAlloc ){ + nAlloc = nSql+nLine+100; + zSql = realloc(zSql, nAlloc); + if( zSql==0 ){ + raw_printf(stderr, "Error: out of memory\n"); + exit(1); + } + } + nSqlPrior = nSql; + if( nSql==0 ){ + int i; + for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} + assert( nAlloc>0 && zSql!=0 ); + memcpy(zSql, zLine+i, nLine+1-i); + startline = lineno; + nSql = nLine-i; + }else{ + zSql[nSql++] = '\n'; + memcpy(zSql+nSql, zLine, nLine+1); + nSql += nLine; + } + if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) + && sqlite3_complete(zSql) ){ + p->cnt = 0; + open_db(p, 0); + if( p->backslashOn ) resolve_backslashes(zSql); + BEGIN_TIMER; + rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); + END_TIMER; + if( rc || zErrMsg ){ + char zPrefix[100]; + if( in!=0 || !stdin_is_interactive ){ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, + "Error: near line %d:", startline); + }else{ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); + } + if( zErrMsg!=0 ){ + utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); + sqlite3_free(zErrMsg); + zErrMsg = 0; + }else{ + utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); + } + errCnt++; + }else if( p->countChanges ){ + raw_printf(p->out, "changes: %3d total_changes: %d\n", + sqlite3_changes(p->db), sqlite3_total_changes(p->db)); + } + nSql = 0; + if( p->outCount ){ + output_reset(p); + p->outCount = 0; + } + }else if( nSql && _all_whitespace(zSql) ){ + if( p->echoOn ) printf("%s\n", zSql); + nSql = 0; + } + } + if( nSql ){ + if( !_all_whitespace(zSql) ){ + utf8_printf(stderr, "Error: incomplete SQL: %s\n", zSql); + errCnt++; + } + } + free(zSql); + free(zLine); + return errCnt>0; +} + +/* +** Return a pathname which is the user's home directory. A +** 0 return indicates an error of some kind. +*/ +static char *find_home_dir(void){ + static char *home_dir = NULL; + if( home_dir ) return home_dir; + +#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ + && !defined(__RTP__) && !defined(_WRS_KERNEL) + { + struct passwd *pwent; + uid_t uid = getuid(); + if( (pwent=getpwuid(uid)) != NULL) { + home_dir = pwent->pw_dir; + } + } +#endif + +#if defined(_WIN32_WCE) + /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() + */ + home_dir = "/"; +#else + +#if defined(_WIN32) || defined(WIN32) + if (!home_dir) { + home_dir = getenv("USERPROFILE"); + } +#endif + + if (!home_dir) { + home_dir = getenv("HOME"); + } + +#if defined(_WIN32) || defined(WIN32) + if (!home_dir) { + char *zDrive, *zPath; + int n; + zDrive = getenv("HOMEDRIVE"); + zPath = getenv("HOMEPATH"); + if( zDrive && zPath ){ + n = strlen30(zDrive) + strlen30(zPath) + 1; + home_dir = malloc( n ); + if( home_dir==0 ) return 0; + sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); + return home_dir; + } + home_dir = "c:\\"; + } +#endif + +#endif /* !_WIN32_WCE */ + + if( home_dir ){ + int n = strlen30(home_dir) + 1; + char *z = malloc( n ); + if( z ) memcpy(z, home_dir, n); + home_dir = z; + } + + return home_dir; +} + +/* +** Read input from the file given by sqliterc_override. Or if that +** parameter is NULL, take input from ~/.sqliterc +** +** Returns the number of errors. +*/ +static void process_sqliterc( + ShellState *p, /* Configuration data */ + const char *sqliterc_override /* Name of config file. NULL to use default */ +){ + char *home_dir = NULL; + const char *sqliterc = sqliterc_override; + char *zBuf = 0; + FILE *in = NULL; + + if (sqliterc == NULL) { + home_dir = find_home_dir(); + if( home_dir==0 ){ + raw_printf(stderr, "-- warning: cannot find home directory;" + " cannot read ~/.sqliterc\n"); + return; + } + sqlite3_initialize(); + zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); + sqliterc = zBuf; + } + in = fopen(sqliterc,"rb"); + if( in ){ + if( stdin_is_interactive ){ + utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); + } + process_input(p,in); + fclose(in); + } + sqlite3_free(zBuf); +} + +/* +** Show available command line options +*/ +static const char zOptions[] = + " -ascii set output mode to 'ascii'\n" + " -bail stop after hitting an error\n" + " -batch force batch I/O\n" + " -column set output mode to 'column'\n" + " -cmd COMMAND run \"COMMAND\" before reading stdin\n" + " -csv set output mode to 'csv'\n" + " -echo print commands before execution\n" + " -init FILENAME read/process named file\n" + " -[no]header turn headers on or off\n" +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + " -heap SIZE Size of heap for memsys3 or memsys5\n" +#endif + " -help show this message\n" + " -html set output mode to HTML\n" + " -interactive force interactive I/O\n" + " -line set output mode to 'line'\n" + " -list set output mode to 'list'\n" + " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" + " -mmap N default mmap size set to N\n" +#ifdef SQLITE_ENABLE_MULTIPLEX + " -multiplex enable the multiplexor VFS\n" +#endif + " -newline SEP set output row separator. Default: '\\n'\n" + " -nullvalue TEXT set text string for NULL values. Default ''\n" + " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" + " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" + " -separator SEP set output column separator. Default: '|'\n" + " -stats print memory stats before each finalize\n" + " -version show SQLite version\n" + " -vfs NAME use NAME as the default VFS\n" +#ifdef SQLITE_ENABLE_VFSTRACE + " -vfstrace enable tracing of all VFS calls\n" +#endif +; +static void usage(int showDetail){ + utf8_printf(stderr, + "Usage: %s [OPTIONS] FILENAME [SQL]\n" + "FILENAME is the name of an SQLite database. A new database is created\n" + "if the file does not previously exist.\n", Argv0); + if( showDetail ){ + utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); + }else{ + raw_printf(stderr, "Use the -help option for additional information\n"); + } + exit(1); +} + +/* +** Initialize the state information in data +*/ +static void main_init(ShellState *data) { + memset(data, 0, sizeof(*data)); + data->normalMode = data->cMode = data->mode = MODE_List; + data->autoExplain = 1; + memcpy(data->colSeparator,SEP_Column, 2); + memcpy(data->rowSeparator,SEP_Row, 2); + data->showHeader = 0; + data->shellFlgs = SHFLG_Lookaside; + sqlite3_config(SQLITE_CONFIG_URI, 1); + sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); + sqlite3_config(SQLITE_CONFIG_MULTITHREAD); + sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); + sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); +} + +/* +** Output text to the console in a font that attracts extra attention. +*/ +#ifdef _WIN32 +static void printBold(const char *zText){ + HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; + GetConsoleScreenBufferInfo(out, &defaultScreenInfo); + SetConsoleTextAttribute(out, + FOREGROUND_RED|FOREGROUND_INTENSITY + ); + printf("%s", zText); + SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); +} +#else +static void printBold(const char *zText){ + printf("\033[1m%s\033[0m", zText); +} +#endif + +/* +** Get the argument to an --option. Throw an error and die if no argument +** is available. +*/ +static char *cmdline_option_value(int argc, char **argv, int i){ + if( i==argc ){ + utf8_printf(stderr, "%s: Error: missing argument to %s\n", + argv[0], argv[argc-1]); + exit(1); + } + return argv[i]; +} + +#ifndef SQLITE_SHELL_IS_UTF8 +# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) +# define SQLITE_SHELL_IS_UTF8 (0) +# else +# define SQLITE_SHELL_IS_UTF8 (1) +# endif +#endif + +#if SQLITE_SHELL_IS_UTF8 +int SQLITE_CDECL main(int argc, char **argv){ +#else +int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ + char **argv; +#endif + char *zErrMsg = 0; + ShellState data; + const char *zInitFile = 0; + int i; + int rc = 0; + int warnInmemoryDb = 0; + int readStdin = 1; + int nCmd = 0; + char **azCmd = 0; + + setBinaryMode(stdin, 0); + setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ + stdin_is_interactive = isatty(0); + stdout_is_console = isatty(1); + +#if USE_SYSTEM_SQLITE+0!=1 + if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ + utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", + sqlite3_sourceid(), SQLITE_SOURCE_ID); + exit(1); + } +#endif + main_init(&data); +#if !SQLITE_SHELL_IS_UTF8 + sqlite3_initialize(); + argv = sqlite3_malloc64(sizeof(argv[0])*argc); + if( argv==0 ){ + raw_printf(stderr, "out of memory\n"); + exit(1); + } + for(i=0; i=1 && argv && argv[0] ); + Argv0 = argv[0]; + + /* Make sure we have a valid signal handler early, before anything + ** else is done. + */ +#ifdef SIGINT + signal(SIGINT, interrupt_handler); +#endif + +#ifdef SQLITE_SHELL_DBNAME_PROC + { + /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name + ** of a C-function that will provide the name of the database file. Use + ** this compile-time option to embed this shell program in larger + ** applications. */ + extern void SQLITE_SHELL_DBNAME_PROC(const char**); + SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); + warnInmemoryDb = 0; + } +#endif + + /* Do an initial pass through the command-line argument to locate + ** the name of the database file, the name of the initialization file, + ** the size of the alternative malloc heap, + ** and the first command to execute. + */ + for(i=1; i0x7fff0000 ) szHeap = 0x7fff0000; + sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); +#endif + }else if( strcmp(z,"-scratch")==0 ){ + int n, sz; + sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( sz>400000 ) sz = 400000; + if( sz<2500 ) sz = 2500; + n = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( n>10 ) n = 10; + if( n<1 ) n = 1; + sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); + data.shellFlgs |= SHFLG_Scratch; + }else if( strcmp(z,"-pagecache")==0 ){ + int n, sz; + sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( sz>70000 ) sz = 70000; + if( sz<0 ) sz = 0; + n = (int)integerValue(cmdline_option_value(argc,argv,++i)); + sqlite3_config(SQLITE_CONFIG_PAGECACHE, + (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); + data.shellFlgs |= SHFLG_Pagecache; + }else if( strcmp(z,"-lookaside")==0 ){ + int n, sz; + sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( sz<0 ) sz = 0; + n = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( n<0 ) n = 0; + sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); + if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; +#ifdef SQLITE_ENABLE_VFSTRACE + }else if( strcmp(z,"-vfstrace")==0 ){ + extern int vfstrace_register( + const char *zTraceName, + const char *zOldVfsName, + int (*xOut)(const char*,void*), + void *pOutArg, + int makeDefault + ); + vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); +#endif +#ifdef SQLITE_ENABLE_MULTIPLEX + }else if( strcmp(z,"-multiplex")==0 ){ + extern int sqlite3_multiple_initialize(const char*,int); + sqlite3_multiplex_initialize(0, 1); +#endif + }else if( strcmp(z,"-mmap")==0 ){ + sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); + sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); + }else if( strcmp(z,"-vfs")==0 ){ + sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); + if( pVfs ){ + sqlite3_vfs_register(pVfs, 1); + }else{ + utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); + exit(1); + } + } + } + if( data.zDbFilename==0 ){ +#ifndef SQLITE_OMIT_MEMORYDB + data.zDbFilename = ":memory:"; + warnInmemoryDb = argc==1; +#else + utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); + return 1; +#endif + } + data.out = stdout; + + /* Go ahead and open the database file if it already exists. If the + ** file does not exist, delay opening it. This prevents empty database + ** files from being created if a user mistypes the database name argument + ** to the sqlite command-line tool. + */ + if( access(data.zDbFilename, 0)==0 ){ + open_db(&data, 0); + } + + /* Process the initialization file if there is one. If no -init option + ** is given on the command line, look for a file named ~/.sqliterc and + ** try to process it. + */ + process_sqliterc(&data,zInitFile); + + /* Make a second pass through the command-line argument and set + ** options. This second pass is delayed until after the initialization + ** file is processed so that the command-line arguments will override + ** settings in the initialization file. + */ + for(i=1; i=1400 +# define _CRT_RAND_S +#endif + +/* +** Include the header file used to customize the compiler options for MSVC. +** This should be done first so that it can successfully prevent spurious +** compiler warnings due to subsequent content in this file and other files +** that are included by this file. +*/ +/************** Include msvc.h in the middle of sqliteInt.h ******************/ +/************** Begin file msvc.h ********************************************/ +/* +** 2015 January 12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code that is specific to MSVC. +*/ +#ifndef _MSVC_H_ +#define _MSVC_H_ + +#if defined(_MSC_VER) +#pragma warning(disable : 4054) +#pragma warning(disable : 4055) +#pragma warning(disable : 4100) +#pragma warning(disable : 4127) +#pragma warning(disable : 4130) +#pragma warning(disable : 4152) +#pragma warning(disable : 4189) +#pragma warning(disable : 4206) +#pragma warning(disable : 4210) +#pragma warning(disable : 4232) +#pragma warning(disable : 4244) +#pragma warning(disable : 4305) +#pragma warning(disable : 4306) +#pragma warning(disable : 4702) +#pragma warning(disable : 4706) +#endif /* defined(_MSC_VER) */ + +#endif /* _MSVC_H_ */ + +/************** End of msvc.h ************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* +** Special setup for VxWorks +*/ +/************** Include vxworks.h in the middle of sqliteInt.h ***************/ +/************** Begin file vxworks.h *****************************************/ +/* +** 2015-03-02 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code that is specific to Wind River's VxWorks +*/ +#if defined(__RTP__) || defined(_WRS_KERNEL) +/* This is VxWorks. Set up things specially for that OS +*/ +#include +#include /* amalgamator: dontcache */ +#define OS_VXWORKS 1 +#define SQLITE_OS_OTHER 0 +#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1 +#define SQLITE_OMIT_LOAD_EXTENSION 1 +#define SQLITE_ENABLE_LOCKING_STYLE 0 +#define HAVE_UTIME 1 +#else +/* This is not VxWorks. */ +#define OS_VXWORKS 0 +#define HAVE_FCHOWN 1 +#define HAVE_READLINK 1 +#define HAVE_LSTAT 1 +#endif /* defined(_WRS_KERNEL) */ + +/************** End of vxworks.h *********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* +** These #defines should enable >2GB file support on POSIX if the +** underlying operating system supports it. If the OS lacks +** large file support, or if the OS is windows, these should be no-ops. +** +** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any +** system #includes. Hence, this block of code must be the very first +** code in all source files. +** +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch +** on the compiler command line. This is necessary if you are compiling +** on a recent machine (ex: Red Hat 7.2) but you want your code to work +** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2 +** without this option, LFS is enable. But LFS does not exist in the kernel +** in Red Hat 6.0, so the code won't work. Hence, for maximum binary +** portability you should omit LFS. +** +** The previous paragraph was written in 2005. (This paragraph is written +** on 2008-11-28.) These days, all Linux kernels support large files, so +** you should probably leave LFS enabled. But some embedded platforms might +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. +** +** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later. +*/ +#ifndef SQLITE_DISABLE_LFS +# define _LARGE_FILE 1 +# ifndef _FILE_OFFSET_BITS +# define _FILE_OFFSET_BITS 64 +# endif +# define _LARGEFILE_SOURCE 1 +#endif + +/* What version of GCC is being used. 0 means GCC is not being used */ +#ifdef __GNUC__ +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) +#else +# define GCC_VERSION 0 +#endif + +/* Needed for various definitions... */ +#if defined(__GNUC__) && !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif + +#if defined(__OpenBSD__) && !defined(_BSD_SOURCE) +# define _BSD_SOURCE +#endif + +/* +** For MinGW, check to see if we can include the header file containing its +** version information, among other things. Normally, this internal MinGW +** header file would [only] be included automatically by other MinGW header +** files; however, the contained version information is now required by this +** header file to work around binary compatibility issues (see below) and +** this is the only known way to reliably obtain it. This entire #if block +** would be completely unnecessary if there was any other way of detecting +** MinGW via their preprocessor (e.g. if they customized their GCC to define +** some MinGW-specific macros). When compiling for MinGW, either the +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be +** defined; otherwise, detection of conditions specific to MinGW will be +** disabled. +*/ +#if defined(_HAVE_MINGW_H) +# include "mingw.h" +#elif defined(_HAVE__MINGW_H) +# include "_mingw.h" +#endif + +/* +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T +** define is required to maintain binary compatibility with the MSVC runtime +** library in use (e.g. for Windows XP). +*/ +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \ + defined(_WIN32) && !defined(_WIN64) && \ + defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \ + defined(__MSVCRT__) +# define _USE_32BIT_TIME_T +#endif + +/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear +** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for +** MinGW. +*/ +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/ +/************** Begin file sqlite3.h *****************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the SQLite library +** presents to client programs. If a C-function, structure, datatype, +** or constant definition does not appear in this file, then it is +** not a published API of SQLite, is subject to change without +** notice, and should not be referenced by programs that use SQLite. +** +** Some of the definitions that are in this file are marked as +** "experimental". Experimental interfaces are normally new +** features recently added to SQLite. We do not anticipate changes +** to experimental interfaces but reserve the right to make minor changes +** if experience from use "in the wild" suggest such changes are prudent. +** +** The official C-language API documentation for SQLite is derived +** from comments in this file. This file is the authoritative source +** on how SQLite interfaces are supposed to operate. +** +** The name of this file under configuration management is "sqlite.h.in". +** The makefile makes some minor changes to this file (such as inserting +** the version number) and changes its name to "sqlite3.h" as +** part of the build process. +*/ +#ifndef _SQLITE3_H_ +#define _SQLITE3_H_ +#include /* Needed for the definition of va_list */ + +/* +** Make sure we can call this stuff from C++. +*/ +#if 0 +extern "C" { +#endif + + +/* +** Provide the ability to override linkage features of the interface. +*/ +#ifndef SQLITE_EXTERN +# define SQLITE_EXTERN extern +#endif +#ifndef SQLITE_API +# define SQLITE_API +#endif +#ifndef SQLITE_CDECL +# define SQLITE_CDECL +#endif +#ifndef SQLITE_STDCALL +# define SQLITE_STDCALL +#endif + +/* +** These no-op macros are used in front of interfaces to mark those +** interfaces as either deprecated or experimental. New applications +** should not use deprecated interfaces - they are supported for backwards +** compatibility only. Application writers should be aware that +** experimental interfaces are subject to change in point releases. +** +** These macros used to resolve to various kinds of compiler magic that +** would generate warning messages when they were used. But that +** compiler magic ended up generating such a flurry of bug reports +** that we have taken it all out and gone back to using simple +** noop macros. +*/ +#define SQLITE_DEPRECATED +#define SQLITE_EXPERIMENTAL + +/* +** Ensure these symbols were not defined by some previous header file. +*/ +#ifdef SQLITE_VERSION +# undef SQLITE_VERSION +#endif +#ifdef SQLITE_VERSION_NUMBER +# undef SQLITE_VERSION_NUMBER +#endif + +/* +** CAPI3REF: Compile-Time Library Version Numbers +** +** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header +** evaluates to a string literal that is the SQLite version in the +** format "X.Y.Z" where X is the major version number (always 3 for +** SQLite3) and Y is the minor version number and Z is the release number.)^ +** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer +** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same +** numbers used in [SQLITE_VERSION].)^ +** The SQLITE_VERSION_NUMBER for any given release of SQLite will also +** be larger than the release from which it is derived. Either Y will +** be held constant and Z will be incremented or else Y will be incremented +** and Z will be reset to zero. +** +** Since version 3.6.18, SQLite source code has been stored in the +** Fossil configuration management +** system. ^The SQLITE_SOURCE_ID macro evaluates to +** a string which identifies a particular check-in of SQLite +** within its configuration management system. ^The SQLITE_SOURCE_ID +** string contains the date and time of the check-in (UTC) and an SHA1 +** hash of the entire source tree. +** +** See also: [sqlite3_libversion()], +** [sqlite3_libversion_number()], [sqlite3_sourceid()], +** [sqlite_version()] and [sqlite_source_id()]. +*/ +#define SQLITE_VERSION "3.13.0" +#define SQLITE_VERSION_NUMBER 3013000 +#define SQLITE_SOURCE_ID "2016-05-18 10:57:30 fc49f556e48970561d7ab6a2f24fdd7d9eb81ff2" + +/* +** CAPI3REF: Run-Time Library Version Numbers +** KEYWORDS: sqlite3_version, sqlite3_sourceid +** +** These interfaces provide the same information as the [SQLITE_VERSION], +** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros +** but are associated with the library instead of the header file. ^(Cautious +** programmers might include assert() statements in their application to +** verify that values returned by these interfaces match the macros in +** the header, and thus ensure that the application is +** compiled with matching library and header files. +** +**
+** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
+** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
+** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
+** 
)^ +** +** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION] +** macro. ^The sqlite3_libversion() function returns a pointer to the +** to the sqlite3_version[] string constant. The sqlite3_libversion() +** function is provided for use in DLLs since DLL users usually do not have +** direct access to string constants within the DLL. ^The +** sqlite3_libversion_number() function returns an integer equal to +** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns +** a pointer to a string constant whose value is the same as the +** [SQLITE_SOURCE_ID] C preprocessor macro. +** +** See also: [sqlite_version()] and [sqlite_source_id()]. +*/ +SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; +SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void); +SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void); +SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void); + +/* +** CAPI3REF: Run-Time Library Compilation Options Diagnostics +** +** ^The sqlite3_compileoption_used() function returns 0 or 1 +** indicating whether the specified option was defined at +** compile time. ^The SQLITE_ prefix may be omitted from the +** option name passed to sqlite3_compileoption_used(). +** +** ^The sqlite3_compileoption_get() function allows iterating +** over the list of options that were defined at compile time by +** returning the N-th compile time option string. ^If N is out of range, +** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ +** prefix is omitted from any strings returned by +** sqlite3_compileoption_get(). +** +** ^Support for the diagnostic functions sqlite3_compileoption_used() +** and sqlite3_compileoption_get() may be omitted by specifying the +** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. +** +** See also: SQL functions [sqlite_compileoption_used()] and +** [sqlite_compileoption_get()] and the [compile_options pragma]. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName); +SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N); +#endif + +/* +** CAPI3REF: Test To See If The Library Is Threadsafe +** +** ^The sqlite3_threadsafe() function returns zero if and only if +** SQLite was compiled with mutexing code omitted due to the +** [SQLITE_THREADSAFE] compile-time option being set to 0. +** +** SQLite can be compiled with or without mutexes. When +** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes +** are enabled and SQLite is threadsafe. When the +** [SQLITE_THREADSAFE] macro is 0, +** the mutexes are omitted. Without the mutexes, it is not safe +** to use SQLite concurrently from more than one thread. +** +** Enabling mutexes incurs a measurable performance penalty. +** So if speed is of utmost importance, it makes sense to disable +** the mutexes. But for maximum safety, mutexes should be enabled. +** ^The default behavior is for mutexes to be enabled. +** +** This interface can be used by an application to make sure that the +** version of SQLite that it is linking against was compiled with +** the desired setting of the [SQLITE_THREADSAFE] macro. +** +** This interface only reports on the compile-time mutex setting +** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with +** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but +** can be fully or partially disabled using a call to [sqlite3_config()] +** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD], +** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the +** sqlite3_threadsafe() function shows only the compile-time setting of +** thread safety, not any run-time changes to that setting made by +** sqlite3_config(). In other words, the return value from sqlite3_threadsafe() +** is unchanged by calls to sqlite3_config().)^ +** +** See the [threading mode] documentation for additional information. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void); + +/* +** CAPI3REF: Database Connection Handle +** KEYWORDS: {database connection} {database connections} +** +** Each open SQLite database is represented by a pointer to an instance of +** the opaque structure named "sqlite3". It is useful to think of an sqlite3 +** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()] +** and [sqlite3_close_v2()] are its destructors. There are many other +** interfaces (such as +** [sqlite3_prepare_v2()], [sqlite3_create_function()], and +** [sqlite3_busy_timeout()] to name but three) that are methods on an +** sqlite3 object. +*/ +typedef struct sqlite3 sqlite3; + +/* +** CAPI3REF: 64-Bit Integer Types +** KEYWORDS: sqlite_int64 sqlite_uint64 +** +** Because there is no cross-platform way to specify 64-bit integer types +** SQLite includes typedefs for 64-bit signed and unsigned integers. +** +** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions. +** The sqlite_int64 and sqlite_uint64 types are supported for backwards +** compatibility only. +** +** ^The sqlite3_int64 and sqlite_int64 types can store integer values +** between -9223372036854775808 and +9223372036854775807 inclusive. ^The +** sqlite3_uint64 and sqlite_uint64 types can store integer values +** between 0 and +18446744073709551615 inclusive. +*/ +#ifdef SQLITE_INT64_TYPE + typedef SQLITE_INT64_TYPE sqlite_int64; + typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; +#elif defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 sqlite_int64; + typedef unsigned __int64 sqlite_uint64; +#else + typedef long long int sqlite_int64; + typedef unsigned long long int sqlite_uint64; +#endif +typedef sqlite_int64 sqlite3_int64; +typedef sqlite_uint64 sqlite3_uint64; + +/* +** If compiling for a processor that lacks floating point support, +** substitute integer for floating-point. +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# define double sqlite3_int64 +#endif + +/* +** CAPI3REF: Closing A Database Connection +** DESTRUCTOR: sqlite3 +** +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors +** for the [sqlite3] object. +** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if +** the [sqlite3] object is successfully destroyed and all associated +** resources are deallocated. +** +** ^If the database connection is associated with unfinalized prepared +** statements or unfinished sqlite3_backup objects then sqlite3_close() +** will leave the database connection open and return [SQLITE_BUSY]. +** ^If sqlite3_close_v2() is called with unfinalized prepared statements +** and/or unfinished sqlite3_backups, then the database connection becomes +** an unusable "zombie" which will automatically be deallocated when the +** last prepared statement is finalized or the last sqlite3_backup is +** finished. The sqlite3_close_v2() interface is intended for use with +** host languages that are garbage collected, and where the order in which +** destructors are called is arbitrary. +** +** Applications should [sqlite3_finalize | finalize] all [prepared statements], +** [sqlite3_blob_close | close] all [BLOB handles], and +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated +** with the [sqlite3] object prior to attempting to close the object. ^If +** sqlite3_close_v2() is called on a [database connection] that still has +** outstanding [prepared statements], [BLOB handles], and/or +** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation +** of resources is deferred until all [prepared statements], [BLOB handles], +** and [sqlite3_backup] objects are also destroyed. +** +** ^If an [sqlite3] object is destroyed while a transaction is open, +** the transaction is automatically rolled back. +** +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)] +** must be either a NULL +** pointer or an [sqlite3] object pointer obtained +** from [sqlite3_open()], [sqlite3_open16()], or +** [sqlite3_open_v2()], and not previously closed. +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer +** argument is a harmless no-op. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*); +SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*); + +/* +** The type for a callback function. +** This is legacy and deprecated. It is included for historical +** compatibility and is not documented. +*/ +typedef int (*sqlite3_callback)(void*,int,char**, char**); + +/* +** CAPI3REF: One-Step Query Execution Interface +** METHOD: sqlite3 +** +** The sqlite3_exec() interface is a convenience wrapper around +** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], +** that allows an application to run multiple statements of SQL +** without having to use a lot of C code. +** +** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded, +** semicolon-separate SQL statements passed into its 2nd argument, +** in the context of the [database connection] passed in as its 1st +** argument. ^If the callback function of the 3rd argument to +** sqlite3_exec() is not NULL, then it is invoked for each result row +** coming out of the evaluated SQL statements. ^The 4th argument to +** sqlite3_exec() is relayed through to the 1st argument of each +** callback invocation. ^If the callback pointer to sqlite3_exec() +** is NULL, then no callback is ever invoked and result rows are +** ignored. +** +** ^If an error occurs while evaluating the SQL statements passed into +** sqlite3_exec(), then execution of the current statement stops and +** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec() +** is not NULL then any error message is written into memory obtained +** from [sqlite3_malloc()] and passed back through the 5th parameter. +** To avoid memory leaks, the application should invoke [sqlite3_free()] +** on error message strings returned through the 5th parameter of +** sqlite3_exec() after the error message string is no longer needed. +** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors +** occur, then sqlite3_exec() sets the pointer in its 5th parameter to +** NULL before returning. +** +** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec() +** routine returns SQLITE_ABORT without invoking the callback again and +** without running any subsequent SQL statements. +** +** ^The 2nd argument to the sqlite3_exec() callback function is the +** number of columns in the result. ^The 3rd argument to the sqlite3_exec() +** callback is an array of pointers to strings obtained as if from +** [sqlite3_column_text()], one for each column. ^If an element of a +** result row is NULL then the corresponding string pointer for the +** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the +** sqlite3_exec() callback is an array of pointers to strings where each +** entry represents the name of corresponding result column as obtained +** from [sqlite3_column_name()]. +** +** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer +** to an empty string, or a pointer that contains only whitespace and/or +** SQL comments, then no SQL statements are evaluated and the database +** is not changed. +** +** Restrictions: +** +**
    +**
  • The application must ensure that the 1st parameter to sqlite3_exec() +** is a valid and open [database connection]. +**
  • The application must not close the [database connection] specified by +** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. +**
  • The application must not modify the SQL statement text passed into +** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. +**
+*/ +SQLITE_API int SQLITE_STDCALL sqlite3_exec( + sqlite3*, /* An open database */ + const char *sql, /* SQL to be evaluated */ + int (*callback)(void*,int,char**,char**), /* Callback function */ + void *, /* 1st argument to callback */ + char **errmsg /* Error msg written here */ +); + +/* +** CAPI3REF: Result Codes +** KEYWORDS: {result code definitions} +** +** Many SQLite functions return an integer result code from the set shown +** here in order to indicate success or failure. +** +** New error codes may be added in future versions of SQLite. +** +** See also: [extended result code definitions] +*/ +#define SQLITE_OK 0 /* Successful result */ +/* beginning-of-error-codes */ +#define SQLITE_ERROR 1 /* SQL error or missing database */ +#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ +#define SQLITE_PERM 3 /* Access permission denied */ +#define SQLITE_ABORT 4 /* Callback routine requested an abort */ +#define SQLITE_BUSY 5 /* The database file is locked */ +#define SQLITE_LOCKED 6 /* A table in the database is locked */ +#define SQLITE_NOMEM 7 /* A malloc() failed */ +#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ +#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ +#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ +#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ +#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ +#define SQLITE_FULL 13 /* Insertion failed because database is full */ +#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ +#define SQLITE_PROTOCOL 15 /* Database lock protocol error */ +#define SQLITE_EMPTY 16 /* Database is empty */ +#define SQLITE_SCHEMA 17 /* The database schema changed */ +#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ +#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ +#define SQLITE_MISMATCH 20 /* Data type mismatch */ +#define SQLITE_MISUSE 21 /* Library used incorrectly */ +#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ +#define SQLITE_AUTH 23 /* Authorization denied */ +#define SQLITE_FORMAT 24 /* Auxiliary database format error */ +#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ +#define SQLITE_NOTADB 26 /* File opened that is not a database file */ +#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ +#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ +#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ +#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ +/* end-of-error-codes */ + +/* +** CAPI3REF: Extended Result Codes +** KEYWORDS: {extended result code definitions} +** +** In its default configuration, SQLite API routines return one of 30 integer +** [result codes]. However, experience has shown that many of +** these result codes are too coarse-grained. They do not provide as +** much information about problems as programmers might like. In an effort to +** address this, newer versions of SQLite (version 3.3.8 and later) include +** support for additional result codes that provide more detailed information +** about errors. These [extended result codes] are enabled or disabled +** on a per database connection basis using the +** [sqlite3_extended_result_codes()] API. Or, the extended code for +** the most recent error can be obtained using +** [sqlite3_extended_errcode()]. +*/ +#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) +#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) +#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) +#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) +#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) +#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) +#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) +#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) +#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) +#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) +#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) +#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) +#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8)) +#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8)) +#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8)) +#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8)) +#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8)) +#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8)) +#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8)) +#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8)) +#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8)) +#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8)) +#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8)) +#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8)) +#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8)) +#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8)) +#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) +#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) +#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) +#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) +#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) +#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) +#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) +#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) +#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) +#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) +#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) +#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) +#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) +#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) +#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8)) +#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8)) +#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8)) +#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8)) +#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8)) +#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8)) +#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8)) +#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8)) +#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8)) +#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8)) +#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8)) +#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) +#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) +#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) +#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) + +/* +** CAPI3REF: Flags For File Open Operations +** +** These bit values are intended for use in the +** 3rd parameter to the [sqlite3_open_v2()] interface and +** in the 4th parameter to the [sqlite3_vfs.xOpen] method. +*/ +#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */ +#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */ +#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */ +#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */ +#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */ +#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */ +#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ +#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ +#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ +#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ +#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ + +/* Reserved: 0x00F00000 */ + +/* +** CAPI3REF: Device Characteristics +** +** The xDeviceCharacteristics method of the [sqlite3_io_methods] +** object returns an integer which is a vector of these +** bit values expressing I/O characteristics of the mass storage +** device that holds the file that the [sqlite3_io_methods] +** refers to. +** +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values +** mean that writes of blocks that are nnn bytes in size and +** are aligned to an address which is an integer multiple of +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means +** that when data is appended to a file, the data is appended +** first then the size of the file is extended, never the other +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that +** information is written to disk in the same order as calls +** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that +** after reboot following a crash or power loss, the only bytes in a +** file that were written at the application level might have changed +** and that adjacent bytes, even bytes within the same sector are +** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN +** flag indicate that a file cannot be deleted when open. The +** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on +** read-only media and cannot be changed even by processes with +** elevated privileges. +*/ +#define SQLITE_IOCAP_ATOMIC 0x00000001 +#define SQLITE_IOCAP_ATOMIC512 0x00000002 +#define SQLITE_IOCAP_ATOMIC1K 0x00000004 +#define SQLITE_IOCAP_ATOMIC2K 0x00000008 +#define SQLITE_IOCAP_ATOMIC4K 0x00000010 +#define SQLITE_IOCAP_ATOMIC8K 0x00000020 +#define SQLITE_IOCAP_ATOMIC16K 0x00000040 +#define SQLITE_IOCAP_ATOMIC32K 0x00000080 +#define SQLITE_IOCAP_ATOMIC64K 0x00000100 +#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 +#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 +#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 +#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000 +#define SQLITE_IOCAP_IMMUTABLE 0x00002000 + +/* +** CAPI3REF: File Locking Levels +** +** SQLite uses one of these integer values as the second +** argument to calls it makes to the xLock() and xUnlock() methods +** of an [sqlite3_io_methods] object. +*/ +#define SQLITE_LOCK_NONE 0 +#define SQLITE_LOCK_SHARED 1 +#define SQLITE_LOCK_RESERVED 2 +#define SQLITE_LOCK_PENDING 3 +#define SQLITE_LOCK_EXCLUSIVE 4 + +/* +** CAPI3REF: Synchronization Type Flags +** +** When SQLite invokes the xSync() method of an +** [sqlite3_io_methods] object it uses a combination of +** these integer values as the second argument. +** +** When the SQLITE_SYNC_DATAONLY flag is used, it means that the +** sync operation only needs to flush data to mass storage. Inode +** information need not be flushed. If the lower four bits of the flag +** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics. +** If the lower four bits equal SQLITE_SYNC_FULL, that means +** to use Mac OS X style fullsync instead of fsync(). +** +** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags +** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL +** settings. The [synchronous pragma] determines when calls to the +** xSync VFS method occur and applies uniformly across all platforms. +** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how +** energetic or rigorous or forceful the sync operations are and +** only make a difference on Mac OSX for the default SQLite code. +** (Third-party VFS implementations might also make the distinction +** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the +** operating systems natively supported by SQLite, only Mac OSX +** cares about the difference.) +*/ +#define SQLITE_SYNC_NORMAL 0x00002 +#define SQLITE_SYNC_FULL 0x00003 +#define SQLITE_SYNC_DATAONLY 0x00010 + +/* +** CAPI3REF: OS Interface Open File Handle +** +** An [sqlite3_file] object represents an open file in the +** [sqlite3_vfs | OS interface layer]. Individual OS interface +** implementations will +** want to subclass this object by appending additional fields +** for their own use. The pMethods entry is a pointer to an +** [sqlite3_io_methods] object that defines methods for performing +** I/O operations on the open file. +*/ +typedef struct sqlite3_file sqlite3_file; +struct sqlite3_file { + const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ +}; + +/* +** CAPI3REF: OS Interface File Virtual Methods Object +** +** Every file opened by the [sqlite3_vfs.xOpen] method populates an +** [sqlite3_file] object (or, more commonly, a subclass of the +** [sqlite3_file] object) with a pointer to an instance of this object. +** This object defines the methods used to perform various operations +** against the open file represented by the [sqlite3_file] object. +** +** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element +** to a non-NULL pointer, then the sqlite3_io_methods.xClose method +** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The +** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen] +** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element +** to NULL. +** +** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or +** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). +** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] +** flag may be ORed in to indicate that only the data of the file +** and not its inode needs to be synced. +** +** The integer values to xLock() and xUnlock() are one of +**
    +**
  • [SQLITE_LOCK_NONE], +**
  • [SQLITE_LOCK_SHARED], +**
  • [SQLITE_LOCK_RESERVED], +**
  • [SQLITE_LOCK_PENDING], or +**
  • [SQLITE_LOCK_EXCLUSIVE]. +**
+** xLock() increases the lock. xUnlock() decreases the lock. +** The xCheckReservedLock() method checks whether any database connection, +** either in this process or in some other process, is holding a RESERVED, +** PENDING, or EXCLUSIVE lock on the file. It returns true +** if such a lock exists and false otherwise. +** +** The xFileControl() method is a generic interface that allows custom +** VFS implementations to directly control an open file using the +** [sqlite3_file_control()] interface. The second "op" argument is an +** integer opcode. The third argument is a generic pointer intended to +** point to a structure that may contain arguments or space in which to +** write return values. Potential uses for xFileControl() might be +** functions to enable blocking locks with timeouts, to change the +** locking strategy (for example to use dot-file locks), to inquire +** about the status of a lock, or to break stale locks. The SQLite +** core reserves all opcodes less than 100 for its own use. +** A [file control opcodes | list of opcodes] less than 100 is available. +** Applications that define a custom xFileControl method should use opcodes +** greater than 100 to avoid conflicts. VFS implementations should +** return [SQLITE_NOTFOUND] for file control opcodes that they do not +** recognize. +** +** The xSectorSize() method returns the sector size of the +** device that underlies the file. The sector size is the +** minimum write that can be performed without disturbing +** other bytes in the file. The xDeviceCharacteristics() +** method returns a bit vector describing behaviors of the +** underlying device: +** +**
    +**
  • [SQLITE_IOCAP_ATOMIC] +**
  • [SQLITE_IOCAP_ATOMIC512] +**
  • [SQLITE_IOCAP_ATOMIC1K] +**
  • [SQLITE_IOCAP_ATOMIC2K] +**
  • [SQLITE_IOCAP_ATOMIC4K] +**
  • [SQLITE_IOCAP_ATOMIC8K] +**
  • [SQLITE_IOCAP_ATOMIC16K] +**
  • [SQLITE_IOCAP_ATOMIC32K] +**
  • [SQLITE_IOCAP_ATOMIC64K] +**
  • [SQLITE_IOCAP_SAFE_APPEND] +**
  • [SQLITE_IOCAP_SEQUENTIAL] +**
+** +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values +** mean that writes of blocks that are nnn bytes in size and +** are aligned to an address which is an integer multiple of +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means +** that when data is appended to a file, the data is appended +** first then the size of the file is extended, never the other +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that +** information is written to disk in the same order as calls +** to xWrite(). +** +** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill +** in the unread portions of the buffer with zeros. A VFS that +** fails to zero-fill short reads might seem to work. However, +** failure to zero-fill short reads will eventually lead to +** database corruption. +*/ +typedef struct sqlite3_io_methods sqlite3_io_methods; +struct sqlite3_io_methods { + int iVersion; + int (*xClose)(sqlite3_file*); + int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); + int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); + int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); + int (*xSync)(sqlite3_file*, int flags); + int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); + int (*xLock)(sqlite3_file*, int); + int (*xUnlock)(sqlite3_file*, int); + int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); + int (*xFileControl)(sqlite3_file*, int op, void *pArg); + int (*xSectorSize)(sqlite3_file*); + int (*xDeviceCharacteristics)(sqlite3_file*); + /* Methods above are valid for version 1 */ + int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**); + int (*xShmLock)(sqlite3_file*, int offset, int n, int flags); + void (*xShmBarrier)(sqlite3_file*); + int (*xShmUnmap)(sqlite3_file*, int deleteFlag); + /* Methods above are valid for version 2 */ + int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); + int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p); + /* Methods above are valid for version 3 */ + /* Additional methods may be added in future releases */ +}; + +/* +** CAPI3REF: Standard File Control Opcodes +** KEYWORDS: {file control opcodes} {file control opcode} +** +** These integer constants are opcodes for the xFileControl method +** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()] +** interface. +** +**
    +**
  • [[SQLITE_FCNTL_LOCKSTATE]] +** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This +** opcode causes the xFileControl method to write the current state of +** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], +** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) +** into an integer that the pArg argument points to. This capability +** is used during testing and is only available when the SQLITE_TEST +** compile-time option is used. +** +**
  • [[SQLITE_FCNTL_SIZE_HINT]] +** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS +** layer a hint of how large the database file will grow to be during the +** current transaction. This hint is not guaranteed to be accurate but it +** is often close. The underlying VFS might choose to preallocate database +** file space based on this hint in order to help writes to the database +** file run faster. +** +**
  • [[SQLITE_FCNTL_CHUNK_SIZE]] +** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS +** extends and truncates the database file in chunks of a size specified +** by the user. The fourth argument to [sqlite3_file_control()] should +** point to an integer (type int) containing the new chunk-size to use +** for the nominated database. Allocating database file space in large +** chunks (say 1MB at a time), may reduce file-system fragmentation and +** improve performance on some systems. +** +**
  • [[SQLITE_FCNTL_FILE_POINTER]] +** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer +** to the [sqlite3_file] object associated with a particular database +** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER]. +** +**
  • [[SQLITE_FCNTL_JOURNAL_POINTER]] +** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer +** to the [sqlite3_file] object associated with the journal file (either +** the [rollback journal] or the [write-ahead log]) for a particular database +** connection. See also [SQLITE_FCNTL_FILE_POINTER]. +** +**
  • [[SQLITE_FCNTL_SYNC_OMITTED]] +** No longer in use. +** +**
  • [[SQLITE_FCNTL_SYNC]] +** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and +** sent to the VFS immediately before the xSync method is invoked on a +** database file descriptor. Or, if the xSync method is not invoked +** because the user has configured SQLite with +** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place +** of the xSync method. In most cases, the pointer argument passed with +** this file-control is NULL. However, if the database file is being synced +** as part of a multi-database commit, the argument points to a nul-terminated +** string containing the transactions master-journal file name. VFSes that +** do not need this signal should silently ignore this opcode. Applications +** should not call [sqlite3_file_control()] with this opcode as doing so may +** disrupt the operation of the specialized VFSes that do require it. +** +**
  • [[SQLITE_FCNTL_COMMIT_PHASETWO]] +** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite +** and sent to the VFS after a transaction has been committed immediately +** but before the database is unlocked. VFSes that do not need this signal +** should silently ignore this opcode. Applications should not call +** [sqlite3_file_control()] with this opcode as doing so may disrupt the +** operation of the specialized VFSes that do require it. +** +**
  • [[SQLITE_FCNTL_WIN32_AV_RETRY]] +** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic +** retry counts and intervals for certain disk I/O operations for the +** windows [VFS] in order to provide robustness in the presence of +** anti-virus programs. By default, the windows VFS will retry file read, +** file write, and file delete operations up to 10 times, with a delay +** of 25 milliseconds before the first retry and with the delay increasing +** by an additional 25 milliseconds with each subsequent retry. This +** opcode allows these two values (10 retries and 25 milliseconds of delay) +** to be adjusted. The values are changed for all database connections +** within the same process. The argument is a pointer to an array of two +** integers where the first integer i the new retry count and the second +** integer is the delay. If either integer is negative, then the setting +** is not changed but instead the prior value of that setting is written +** into the array entry, allowing the current retry settings to be +** interrogated. The zDbName parameter is ignored. +** +**
  • [[SQLITE_FCNTL_PERSIST_WAL]] +** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the +** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary +** write ahead log and shared memory files used for transaction control +** are automatically deleted when the latest connection to the database +** closes. Setting persistent WAL mode causes those files to persist after +** close. Persisting the files is useful when other processes that do not +** have write permission on the directory containing the database file want +** to read the database file, as the WAL and shared memory files must exist +** in order for the database to be readable. The fourth parameter to +** [sqlite3_file_control()] for this opcode should be a pointer to an integer. +** That integer is 0 to disable persistent WAL mode or 1 to enable persistent +** WAL mode. If the integer is -1, then it is overwritten with the current +** WAL persistence setting. +** +**
  • [[SQLITE_FCNTL_POWERSAFE_OVERWRITE]] +** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the +** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting +** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the +** xDeviceCharacteristics methods. The fourth parameter to +** [sqlite3_file_control()] for this opcode should be a pointer to an integer. +** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage +** mode. If the integer is -1, then it is overwritten with the current +** zero-damage mode setting. +** +**
  • [[SQLITE_FCNTL_OVERWRITE]] +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening +** a write transaction to indicate that, unless it is rolled back for some +** reason, the entire database file will be overwritten by the current +** transaction. This is used by VACUUM operations. +** +**
  • [[SQLITE_FCNTL_VFSNAME]] +** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of +** all [VFSes] in the VFS stack. The names are of all VFS shims and the +** final bottom-level VFS are written into memory obtained from +** [sqlite3_malloc()] and the result is stored in the char* variable +** that the fourth parameter of [sqlite3_file_control()] points to. +** The caller is responsible for freeing the memory when done. As with +** all file-control actions, there is no guarantee that this will actually +** do anything. Callers should initialize the char* variable to a NULL +** pointer in case this file-control is not implemented. This file-control +** is intended for diagnostic use only. +** +**
  • [[SQLITE_FCNTL_VFS_POINTER]] +** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level +** [VFSes] currently in use. ^(The argument X in +** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be +** of type "[sqlite3_vfs] **". This opcodes will set *X +** to a pointer to the top-level VFS.)^ +** ^When there are multiple VFS shims in the stack, this opcode finds the +** upper-most shim only. +** +**
  • [[SQLITE_FCNTL_PRAGMA]] +** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] +** file control is sent to the open [sqlite3_file] object corresponding +** to the database file to which the pragma statement refers. ^The argument +** to the [SQLITE_FCNTL_PRAGMA] file control is an array of +** pointers to strings (char**) in which the second element of the array +** is the name of the pragma and the third element is the argument to the +** pragma or NULL if the pragma has no argument. ^The handler for an +** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element +** of the char** argument point to a string obtained from [sqlite3_mprintf()] +** or the equivalent and that string will become the result of the pragma or +** the error message if the pragma fails. ^If the +** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal +** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA] +** file control returns [SQLITE_OK], then the parser assumes that the +** VFS has handled the PRAGMA itself and the parser generates a no-op +** prepared statement if result string is NULL, or that returns a copy +** of the result string if the string is non-NULL. +** ^If the [SQLITE_FCNTL_PRAGMA] file control returns +** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means +** that the VFS encountered an error while handling the [PRAGMA] and the +** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA] +** file control occurs at the beginning of pragma statement analysis and so +** it is able to override built-in [PRAGMA] statements. +** +**
  • [[SQLITE_FCNTL_BUSYHANDLER]] +** ^The [SQLITE_FCNTL_BUSYHANDLER] +** file-control may be invoked by SQLite on the database file handle +** shortly after it is opened in order to provide a custom VFS with access +** to the connections busy-handler callback. The argument is of type (void **) +** - an array of two (void *) values. The first (void *) actually points +** to a function of type (int (*)(void *)). In order to invoke the connections +** busy-handler, this function should be invoked with the second (void *) in +** the array as the only argument. If it returns non-zero, then the operation +** should be retried. If it returns zero, the custom VFS should abandon the +** current operation. +** +**
  • [[SQLITE_FCNTL_TEMPFILENAME]] +** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control +** to have SQLite generate a +** temporary filename using the same algorithm that is followed to generate +** temporary filenames for TEMP tables and other internal uses. The +** argument should be a char** which will be filled with the filename +** written into memory obtained from [sqlite3_malloc()]. The caller should +** invoke [sqlite3_free()] on the result to avoid a memory leak. +** +**
  • [[SQLITE_FCNTL_MMAP_SIZE]] +** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the +** maximum number of bytes that will be used for memory-mapped I/O. +** The argument is a pointer to a value of type sqlite3_int64 that +** is an advisory maximum number of bytes in the file to memory map. The +** pointer is overwritten with the old value. The limit is not changed if +** the value originally pointed to is negative, and so the current limit +** can be queried by passing in a pointer to a negative number. This +** file-control is used internally to implement [PRAGMA mmap_size]. +** +**
  • [[SQLITE_FCNTL_TRACE]] +** The [SQLITE_FCNTL_TRACE] file control provides advisory information +** to the VFS about what the higher layers of the SQLite stack are doing. +** This file control is used by some VFS activity tracing [shims]. +** The argument is a zero-terminated string. Higher layers in the +** SQLite stack may generate instances of this file control if +** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled. +** +**
  • [[SQLITE_FCNTL_HAS_MOVED]] +** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a +** pointer to an integer and it writes a boolean into that integer depending +** on whether or not the file has been renamed, moved, or deleted since it +** was first opened. +** +**
  • [[SQLITE_FCNTL_WIN32_SET_HANDLE]] +** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This +** opcode causes the xFileControl method to swap the file handle with the one +** pointed to by the pArg argument. This capability is used during testing +** and only needs to be supported when SQLITE_TEST is defined. +** +**
  • [[SQLITE_FCNTL_WAL_BLOCK]] +** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might +** be advantageous to block on the next WAL lock if the lock is not immediately +** available. The WAL subsystem issues this signal during rare +** circumstances in order to fix a problem with priority inversion. +** Applications should not use this file-control. +** +**
  • [[SQLITE_FCNTL_ZIPVFS]] +** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other +** VFS should return SQLITE_NOTFOUND for this opcode. +** +**
  • [[SQLITE_FCNTL_RBU]] +** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by +** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for +** this opcode. +**
+*/ +#define SQLITE_FCNTL_LOCKSTATE 1 +#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 +#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 +#define SQLITE_FCNTL_LAST_ERRNO 4 +#define SQLITE_FCNTL_SIZE_HINT 5 +#define SQLITE_FCNTL_CHUNK_SIZE 6 +#define SQLITE_FCNTL_FILE_POINTER 7 +#define SQLITE_FCNTL_SYNC_OMITTED 8 +#define SQLITE_FCNTL_WIN32_AV_RETRY 9 +#define SQLITE_FCNTL_PERSIST_WAL 10 +#define SQLITE_FCNTL_OVERWRITE 11 +#define SQLITE_FCNTL_VFSNAME 12 +#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13 +#define SQLITE_FCNTL_PRAGMA 14 +#define SQLITE_FCNTL_BUSYHANDLER 15 +#define SQLITE_FCNTL_TEMPFILENAME 16 +#define SQLITE_FCNTL_MMAP_SIZE 18 +#define SQLITE_FCNTL_TRACE 19 +#define SQLITE_FCNTL_HAS_MOVED 20 +#define SQLITE_FCNTL_SYNC 21 +#define SQLITE_FCNTL_COMMIT_PHASETWO 22 +#define SQLITE_FCNTL_WIN32_SET_HANDLE 23 +#define SQLITE_FCNTL_WAL_BLOCK 24 +#define SQLITE_FCNTL_ZIPVFS 25 +#define SQLITE_FCNTL_RBU 26 +#define SQLITE_FCNTL_VFS_POINTER 27 +#define SQLITE_FCNTL_JOURNAL_POINTER 28 + +/* deprecated names */ +#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE +#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE +#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO + + +/* +** CAPI3REF: Mutex Handle +** +** The mutex module within SQLite defines [sqlite3_mutex] to be an +** abstract type for a mutex object. The SQLite core never looks +** at the internal representation of an [sqlite3_mutex]. It only +** deals with pointers to the [sqlite3_mutex] object. +** +** Mutexes are created using [sqlite3_mutex_alloc()]. +*/ +typedef struct sqlite3_mutex sqlite3_mutex; + +/* +** CAPI3REF: OS Interface Object +** +** An instance of the sqlite3_vfs object defines the interface between +** the SQLite core and the underlying operating system. The "vfs" +** in the name of the object stands for "virtual file system". See +** the [VFS | VFS documentation] for further information. +** +** The value of the iVersion field is initially 1 but may be larger in +** future versions of SQLite. Additional fields may be appended to this +** object when the iVersion value is increased. Note that the structure +** of the sqlite3_vfs object changes in the transaction between +** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not +** modified. +** +** The szOsFile field is the size of the subclassed [sqlite3_file] +** structure used by this VFS. mxPathname is the maximum length of +** a pathname in this VFS. +** +** Registered sqlite3_vfs objects are kept on a linked list formed by +** the pNext pointer. The [sqlite3_vfs_register()] +** and [sqlite3_vfs_unregister()] interfaces manage this list +** in a thread-safe way. The [sqlite3_vfs_find()] interface +** searches the list. Neither the application code nor the VFS +** implementation should use the pNext pointer. +** +** The pNext field is the only field in the sqlite3_vfs +** structure that SQLite will ever modify. SQLite will only access +** or modify this field while holding a particular static mutex. +** The application should never modify anything within the sqlite3_vfs +** object once the object has been registered. +** +** The zName field holds the name of the VFS module. The name must +** be unique across all VFS modules. +** +** [[sqlite3_vfs.xOpen]] +** ^SQLite guarantees that the zFilename parameter to xOpen +** is either a NULL pointer or string obtained +** from xFullPathname() with an optional suffix added. +** ^If a suffix is added to the zFilename parameter, it will +** consist of a single "-" character followed by no more than +** 11 alphanumeric and/or "-" characters. +** ^SQLite further guarantees that +** the string will be valid and unchanged until xClose() is +** called. Because of the previous sentence, +** the [sqlite3_file] can safely store a pointer to the +** filename if it needs to remember the filename for some reason. +** If the zFilename parameter to xOpen is a NULL pointer then xOpen +** must invent its own temporary name for the file. ^Whenever the +** xFilename parameter is NULL it will also be the case that the +** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. +** +** The flags argument to xOpen() includes all bits set in +** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] +** or [sqlite3_open16()] is used, then flags includes at least +** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. +** If xOpen() opens a file read-only then it sets *pOutFlags to +** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. +** +** ^(SQLite will also add one of the following flags to the xOpen() +** call, depending on the object being opened: +** +**
    +**
  • [SQLITE_OPEN_MAIN_DB] +**
  • [SQLITE_OPEN_MAIN_JOURNAL] +**
  • [SQLITE_OPEN_TEMP_DB] +**
  • [SQLITE_OPEN_TEMP_JOURNAL] +**
  • [SQLITE_OPEN_TRANSIENT_DB] +**
  • [SQLITE_OPEN_SUBJOURNAL] +**
  • [SQLITE_OPEN_MASTER_JOURNAL] +**
  • [SQLITE_OPEN_WAL] +**
)^ +** +** The file I/O implementation can use the object type flags to +** change the way it deals with files. For example, an application +** that does not care about crash recovery or rollback might make +** the open of a journal file a no-op. Writes to this journal would +** also be no-ops, and any attempt to read the journal would return +** SQLITE_IOERR. Or the implementation might recognize that a database +** file will be doing page-aligned sector reads and writes in a random +** order and set up its I/O subsystem accordingly. +** +** SQLite might also add one of the following flags to the xOpen method: +** +**
    +**
  • [SQLITE_OPEN_DELETEONCLOSE] +**
  • [SQLITE_OPEN_EXCLUSIVE] +**
+** +** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be +** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] +** will be set for TEMP databases and their journals, transient +** databases, and subjournals. +** +** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction +** with the [SQLITE_OPEN_CREATE] flag, which are both directly +** analogous to the O_EXCL and O_CREAT flags of the POSIX open() +** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the +** SQLITE_OPEN_CREATE, is used to indicate that file should always +** be created, and that it is an error if it already exists. +** It is not used to indicate the file should be opened +** for exclusive access. +** +** ^At least szOsFile bytes of memory are allocated by SQLite +** to hold the [sqlite3_file] structure passed as the third +** argument to xOpen. The xOpen method does not have to +** allocate the structure; it should just fill it in. Note that +** the xOpen method must set the sqlite3_file.pMethods to either +** a valid [sqlite3_io_methods] object or to NULL. xOpen must do +** this even if the open fails. SQLite expects that the sqlite3_file.pMethods +** element will be valid after xOpen returns regardless of the success +** or failure of the xOpen call. +** +** [[sqlite3_vfs.xAccess]] +** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] +** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to +** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] +** to test whether a file is at least readable. The file can be a +** directory. +** +** ^SQLite will always allocate at least mxPathname+1 bytes for the +** output buffer xFullPathname. The exact size of the output buffer +** is also passed as a parameter to both methods. If the output buffer +** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is +** handled as a fatal error by SQLite, vfs implementations should endeavor +** to prevent this by setting mxPathname to a sufficiently large value. +** +** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64() +** interfaces are not strictly a part of the filesystem, but they are +** included in the VFS structure for completeness. +** The xRandomness() function attempts to return nBytes bytes +** of good-quality randomness into zOut. The return value is +** the actual number of bytes of randomness obtained. +** The xSleep() method causes the calling thread to sleep for at +** least the number of microseconds given. ^The xCurrentTime() +** method returns a Julian Day Number for the current date and time as +** a floating point value. +** ^The xCurrentTimeInt64() method returns, as an integer, the Julian +** Day Number multiplied by 86400000 (the number of milliseconds in +** a 24-hour day). +** ^SQLite will use the xCurrentTimeInt64() method to get the current +** date and time if that method is available (if iVersion is 2 or +** greater and the function pointer is not NULL) and will fall back +** to xCurrentTime() if xCurrentTimeInt64() is unavailable. +** +** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces +** are not used by the SQLite core. These optional interfaces are provided +** by some VFSes to facilitate testing of the VFS code. By overriding +** system calls with functions under its control, a test program can +** simulate faults and error conditions that would otherwise be difficult +** or impossible to induce. The set of system calls that can be overridden +** varies from one VFS to another, and from one version of the same VFS to the +** next. Applications that use these interfaces must be prepared for any +** or all of these interfaces to be NULL or for their behavior to change +** from one release to the next. Applications must not attempt to access +** any of these methods if the iVersion of the VFS is less than 3. +*/ +typedef struct sqlite3_vfs sqlite3_vfs; +typedef void (*sqlite3_syscall_ptr)(void); +struct sqlite3_vfs { + int iVersion; /* Structure version number (currently 3) */ + int szOsFile; /* Size of subclassed sqlite3_file */ + int mxPathname; /* Maximum file pathname length */ + sqlite3_vfs *pNext; /* Next registered VFS */ + const char *zName; /* Name of this virtual file system */ + void *pAppData; /* Pointer to application-specific data */ + int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, + int flags, int *pOutFlags); + int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); + int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); + int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); + void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); + void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); + void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); + void (*xDlClose)(sqlite3_vfs*, void*); + int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); + int (*xSleep)(sqlite3_vfs*, int microseconds); + int (*xCurrentTime)(sqlite3_vfs*, double*); + int (*xGetLastError)(sqlite3_vfs*, int, char *); + /* + ** The methods above are in version 1 of the sqlite_vfs object + ** definition. Those that follow are added in version 2 or later + */ + int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*); + /* + ** The methods above are in versions 1 and 2 of the sqlite_vfs object. + ** Those below are for version 3 and greater. + */ + int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr); + sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName); + const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); + /* + ** The methods above are in versions 1 through 3 of the sqlite_vfs object. + ** New fields may be appended in future versions. The iVersion + ** value will increment whenever this happens. + */ +}; + +/* +** CAPI3REF: Flags for the xAccess VFS method +** +** These integer constants can be used as the third parameter to +** the xAccess method of an [sqlite3_vfs] object. They determine +** what kind of permissions the xAccess method is looking for. +** With SQLITE_ACCESS_EXISTS, the xAccess method +** simply checks whether the file exists. +** With SQLITE_ACCESS_READWRITE, the xAccess method +** checks whether the named directory is both readable and writable +** (in other words, if files can be added, removed, and renamed within +** the directory). +** The SQLITE_ACCESS_READWRITE constant is currently used only by the +** [temp_store_directory pragma], though this could change in a future +** release of SQLite. +** With SQLITE_ACCESS_READ, the xAccess method +** checks whether the file is readable. The SQLITE_ACCESS_READ constant is +** currently unused, though it might be used in a future release of +** SQLite. +*/ +#define SQLITE_ACCESS_EXISTS 0 +#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */ +#define SQLITE_ACCESS_READ 2 /* Unused */ + +/* +** CAPI3REF: Flags for the xShmLock VFS method +** +** These integer constants define the various locking operations +** allowed by the xShmLock method of [sqlite3_io_methods]. The +** following are the only legal combinations of flags to the +** xShmLock method: +** +**
    +**
  • SQLITE_SHM_LOCK | SQLITE_SHM_SHARED +**
  • SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE +**
  • SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED +**
  • SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE +**
+** +** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as +** was given on the corresponding lock. +** +** The xShmLock method can transition between unlocked and SHARED or +** between unlocked and EXCLUSIVE. It cannot transition between SHARED +** and EXCLUSIVE. +*/ +#define SQLITE_SHM_UNLOCK 1 +#define SQLITE_SHM_LOCK 2 +#define SQLITE_SHM_SHARED 4 +#define SQLITE_SHM_EXCLUSIVE 8 + +/* +** CAPI3REF: Maximum xShmLock index +** +** The xShmLock method on [sqlite3_io_methods] may use values +** between 0 and this upper bound as its "offset" argument. +** The SQLite core will never attempt to acquire or release a +** lock outside of this range +*/ +#define SQLITE_SHM_NLOCK 8 + + +/* +** CAPI3REF: Initialize The SQLite Library +** +** ^The sqlite3_initialize() routine initializes the +** SQLite library. ^The sqlite3_shutdown() routine +** deallocates any resources that were allocated by sqlite3_initialize(). +** These routines are designed to aid in process initialization and +** shutdown on embedded systems. Workstation applications using +** SQLite normally do not need to invoke either of these routines. +** +** A call to sqlite3_initialize() is an "effective" call if it is +** the first time sqlite3_initialize() is invoked during the lifetime of +** the process, or if it is the first time sqlite3_initialize() is invoked +** following a call to sqlite3_shutdown(). ^(Only an effective call +** of sqlite3_initialize() does any initialization. All other calls +** are harmless no-ops.)^ +** +** A call to sqlite3_shutdown() is an "effective" call if it is the first +** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only +** an effective call to sqlite3_shutdown() does any deinitialization. +** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^ +** +** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown() +** is not. The sqlite3_shutdown() interface must only be called from a +** single thread. All open [database connections] must be closed and all +** other SQLite resources must be deallocated prior to invoking +** sqlite3_shutdown(). +** +** Among other things, ^sqlite3_initialize() will invoke +** sqlite3_os_init(). Similarly, ^sqlite3_shutdown() +** will invoke sqlite3_os_end(). +** +** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success. +** ^If for some reason, sqlite3_initialize() is unable to initialize +** the library (perhaps it is unable to allocate a needed resource such +** as a mutex) it returns an [error code] other than [SQLITE_OK]. +** +** ^The sqlite3_initialize() routine is called internally by many other +** SQLite interfaces so that an application usually does not need to +** invoke sqlite3_initialize() directly. For example, [sqlite3_open()] +** calls sqlite3_initialize() so the SQLite library will be automatically +** initialized when [sqlite3_open()] is called if it has not be initialized +** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT] +** compile-time option, then the automatic calls to sqlite3_initialize() +** are omitted and the application must call sqlite3_initialize() directly +** prior to using any other SQLite interface. For maximum portability, +** it is recommended that applications always invoke sqlite3_initialize() +** directly prior to using any other SQLite interface. Future releases +** of SQLite may require this. In other words, the behavior exhibited +** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the +** default behavior in some future release of SQLite. +** +** The sqlite3_os_init() routine does operating-system specific +** initialization of the SQLite library. The sqlite3_os_end() +** routine undoes the effect of sqlite3_os_init(). Typical tasks +** performed by these routines include allocation or deallocation +** of static resources, initialization of global variables, +** setting up a default [sqlite3_vfs] module, or setting up +** a default configuration using [sqlite3_config()]. +** +** The application should never invoke either sqlite3_os_init() +** or sqlite3_os_end() directly. The application should only invoke +** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init() +** interface is called automatically by sqlite3_initialize() and +** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate +** implementations for sqlite3_os_init() and sqlite3_os_end() +** are built into SQLite when it is compiled for Unix, Windows, or OS/2. +** When [custom builds | built for other platforms] +** (using the [SQLITE_OS_OTHER=1] compile-time +** option) the application must supply a suitable implementation for +** sqlite3_os_init() and sqlite3_os_end(). An application-supplied +** implementation of sqlite3_os_init() or sqlite3_os_end() +** must return [SQLITE_OK] on success and some other [error code] upon +** failure. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void); +SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void); +SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void); +SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void); + +/* +** CAPI3REF: Configuring The SQLite Library +** +** The sqlite3_config() interface is used to make global configuration +** changes to SQLite in order to tune SQLite to the specific needs of +** the application. The default configuration is recommended for most +** applications and so this routine is usually not necessary. It is +** provided to support rare applications with unusual needs. +** +** The sqlite3_config() interface is not threadsafe. The application +** must ensure that no other SQLite interfaces are invoked by other +** threads while sqlite3_config() is running. +** +** The sqlite3_config() interface +** may only be invoked prior to library initialization using +** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. +** ^If sqlite3_config() is called after [sqlite3_initialize()] and before +** [sqlite3_shutdown()] then it will return SQLITE_MISUSE. +** Note, however, that ^sqlite3_config() can be called as part of the +** implementation of an application-defined [sqlite3_os_init()]. +** +** The first argument to sqlite3_config() is an integer +** [configuration option] that determines +** what property of SQLite is to be configured. Subsequent arguments +** vary depending on the [configuration option] +** in the first argument. +** +** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. +** ^If the option is unknown or SQLite is unable to set the option +** then this routine returns a non-zero [error code]. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...); + +/* +** CAPI3REF: Configure database connections +** METHOD: sqlite3 +** +** The sqlite3_db_config() interface is used to make configuration +** changes to a [database connection]. The interface is similar to +** [sqlite3_config()] except that the changes apply to a single +** [database connection] (specified in the first argument). +** +** The second argument to sqlite3_db_config(D,V,...) is the +** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code +** that indicates what aspect of the [database connection] is being configured. +** Subsequent arguments vary depending on the configuration verb. +** +** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if +** the call is considered successful. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3*, int op, ...); + +/* +** CAPI3REF: Memory Allocation Routines +** +** An instance of this object defines the interface between SQLite +** and low-level memory allocation routines. +** +** This object is used in only one place in the SQLite interface. +** A pointer to an instance of this object is the argument to +** [sqlite3_config()] when the configuration option is +** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. +** By creating an instance of this object +** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) +** during configuration, an application can specify an alternative +** memory allocation subsystem for SQLite to use for all of its +** dynamic memory needs. +** +** Note that SQLite comes with several [built-in memory allocators] +** that are perfectly adequate for the overwhelming majority of applications +** and that this object is only useful to a tiny minority of applications +** with specialized memory allocation requirements. This object is +** also used during testing of SQLite in order to specify an alternative +** memory allocator that simulates memory out-of-memory conditions in +** order to verify that SQLite recovers gracefully from such +** conditions. +** +** The xMalloc, xRealloc, and xFree methods must work like the +** malloc(), realloc() and free() functions from the standard C library. +** ^SQLite guarantees that the second argument to +** xRealloc is always a value returned by a prior call to xRoundup. +** +** xSize should return the allocated size of a memory allocation +** previously obtained from xMalloc or xRealloc. The allocated size +** is always at least as big as the requested size but may be larger. +** +** The xRoundup method returns what would be the allocated size of +** a memory allocation given a particular requested size. Most memory +** allocators round up memory allocations at least to the next multiple +** of 8. Some allocators round up to a larger multiple or to a power of 2. +** Every memory allocation request coming in through [sqlite3_malloc()] +** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, +** that causes the corresponding memory allocation to fail. +** +** The xInit method initializes the memory allocator. For example, +** it might allocate any require mutexes or initialize internal data +** structures. The xShutdown method is invoked (indirectly) by +** [sqlite3_shutdown()] and should deallocate any resources acquired +** by xInit. The pAppData pointer is used as the only parameter to +** xInit and xShutdown. +** +** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes +** the xInit method, so the xInit method need not be threadsafe. The +** xShutdown method is only called from [sqlite3_shutdown()] so it does +** not need to be threadsafe either. For all other methods, SQLite +** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the +** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which +** it is by default) and so the methods are automatically serialized. +** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other +** methods must be threadsafe or else make their own arrangements for +** serialization. +** +** SQLite will never invoke xInit() more than once without an intervening +** call to xShutdown(). +*/ +typedef struct sqlite3_mem_methods sqlite3_mem_methods; +struct sqlite3_mem_methods { + void *(*xMalloc)(int); /* Memory allocation function */ + void (*xFree)(void*); /* Free a prior allocation */ + void *(*xRealloc)(void*,int); /* Resize an allocation */ + int (*xSize)(void*); /* Return the size of an allocation */ + int (*xRoundup)(int); /* Round up request size to allocation size */ + int (*xInit)(void*); /* Initialize the memory allocator */ + void (*xShutdown)(void*); /* Deinitialize the memory allocator */ + void *pAppData; /* Argument to xInit() and xShutdown() */ +}; + +/* +** CAPI3REF: Configuration Options +** KEYWORDS: {configuration option} +** +** These constants are the available integer configuration options that +** can be passed as the first argument to the [sqlite3_config()] interface. +** +** New configuration options may be added in future releases of SQLite. +** Existing configuration options might be discontinued. Applications +** should check the return code from [sqlite3_config()] to make sure that +** the call worked. The [sqlite3_config()] interface will return a +** non-zero [error code] if a discontinued or unsupported configuration option +** is invoked. +** +**
+** [[SQLITE_CONFIG_SINGLETHREAD]]
SQLITE_CONFIG_SINGLETHREAD
+**
There are no arguments to this option. ^This option sets the +** [threading mode] to Single-thread. In other words, it disables +** all mutexing and puts SQLite into a mode where it can only be used +** by a single thread. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to change the [threading mode] from its default +** value of Single-thread and so [sqlite3_config()] will return +** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD +** configuration option.
+** +** [[SQLITE_CONFIG_MULTITHREAD]]
SQLITE_CONFIG_MULTITHREAD
+**
There are no arguments to this option. ^This option sets the +** [threading mode] to Multi-thread. In other words, it disables +** mutexing on [database connection] and [prepared statement] objects. +** The application is responsible for serializing access to +** [database connections] and [prepared statements]. But other mutexes +** are enabled so that SQLite will be safe to use in a multi-threaded +** environment as long as no two threads attempt to use the same +** [database connection] at the same time. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to set the Multi-thread [threading mode] and +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the +** SQLITE_CONFIG_MULTITHREAD configuration option.
+** +** [[SQLITE_CONFIG_SERIALIZED]]
SQLITE_CONFIG_SERIALIZED
+**
There are no arguments to this option. ^This option sets the +** [threading mode] to Serialized. In other words, this option enables +** all mutexes including the recursive +** mutexes on [database connection] and [prepared statement] objects. +** In this mode (which is the default when SQLite is compiled with +** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access +** to [database connections] and [prepared statements] so that the +** application is free to use the same [database connection] or the +** same [prepared statement] in different threads at the same time. +** ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to set the Serialized [threading mode] and +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the +** SQLITE_CONFIG_SERIALIZED configuration option.
+** +** [[SQLITE_CONFIG_MALLOC]]
SQLITE_CONFIG_MALLOC
+**
^(The SQLITE_CONFIG_MALLOC option takes a single argument which is +** a pointer to an instance of the [sqlite3_mem_methods] structure. +** The argument specifies +** alternative low-level memory allocation routines to be used in place of +** the memory allocation routines built into SQLite.)^ ^SQLite makes +** its own private copy of the content of the [sqlite3_mem_methods] structure +** before the [sqlite3_config()] call returns.
+** +** [[SQLITE_CONFIG_GETMALLOC]]
SQLITE_CONFIG_GETMALLOC
+**
^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which +** is a pointer to an instance of the [sqlite3_mem_methods] structure. +** The [sqlite3_mem_methods] +** structure is filled with the currently defined memory allocation routines.)^ +** This option can be used to overload the default memory allocation +** routines with a wrapper that simulations memory allocation failure or +** tracks memory usage, for example.
+** +** [[SQLITE_CONFIG_MEMSTATUS]]
SQLITE_CONFIG_MEMSTATUS
+**
^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, +** interpreted as a boolean, which enables or disables the collection of +** memory allocation statistics. ^(When memory allocation statistics are +** disabled, the following SQLite interfaces become non-operational: +**
    +**
  • [sqlite3_memory_used()] +**
  • [sqlite3_memory_highwater()] +**
  • [sqlite3_soft_heap_limit64()] +**
  • [sqlite3_status64()] +**
)^ +** ^Memory allocation statistics are enabled by default unless SQLite is +** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory +** allocation statistics are disabled by default. +**
+** +** [[SQLITE_CONFIG_SCRATCH]]
SQLITE_CONFIG_SCRATCH
+**
^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer +** that SQLite can use for scratch memory. ^(There are three arguments +** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte +** aligned memory buffer from which the scratch allocations will be +** drawn, the size of each scratch allocation (sz), +** and the maximum number of scratch allocations (N).)^ +** The first argument must be a pointer to an 8-byte aligned buffer +** of at least sz*N bytes of memory. +** ^SQLite will not use more than one scratch buffers per thread. +** ^SQLite will never request a scratch buffer that is more than 6 +** times the database page size. +** ^If SQLite needs needs additional +** scratch memory beyond what is provided by this configuration option, then +** [sqlite3_malloc()] will be used to obtain the memory needed.

+** ^When the application provides any amount of scratch memory using +** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large +** [sqlite3_malloc|heap allocations]. +** This can help [Robson proof|prevent memory allocation failures] due to heap +** fragmentation in low-memory embedded systems. +**

+** +** [[SQLITE_CONFIG_PAGECACHE]]
SQLITE_CONFIG_PAGECACHE
+**
^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool +** that SQLite can use for the database page cache with the default page +** cache implementation. +** This configuration option is a no-op if an application-define page +** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]. +** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to +** 8-byte aligned memory (pMem), the size of each page cache line (sz), +** and the number of cache lines (N). +** The sz argument should be the size of the largest database page +** (a power of two between 512 and 65536) plus some extra bytes for each +** page header. ^The number of extra bytes needed by the page header +** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ]. +** ^It is harmless, apart from the wasted memory, +** for the sz parameter to be larger than necessary. The pMem +** argument must be either a NULL pointer or a pointer to an 8-byte +** aligned block of memory of at least sz*N bytes, otherwise +** subsequent behavior is undefined. +** ^When pMem is not NULL, SQLite will strive to use the memory provided +** to satisfy page cache needs, falling back to [sqlite3_malloc()] if +** a page cache line is larger than sz bytes or if all of the pMem buffer +** is exhausted. +** ^If pMem is NULL and N is non-zero, then each database connection +** does an initial bulk allocation for page cache memory +** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or +** of -1024*N bytes if N is negative, . ^If additional +** page cache memory is needed beyond what is provided by the initial +** allocation, then SQLite goes to [sqlite3_malloc()] separately for each +** additional cache line.
+** +** [[SQLITE_CONFIG_HEAP]]
SQLITE_CONFIG_HEAP
+**
^The SQLITE_CONFIG_HEAP option specifies a static memory buffer +** that SQLite will use for all of its dynamic memory allocation needs +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and +** [SQLITE_CONFIG_PAGECACHE]. +** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled +** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns +** [SQLITE_ERROR] if invoked otherwise. +** ^There are three arguments to SQLITE_CONFIG_HEAP: +** An 8-byte aligned pointer to the memory, +** the number of bytes in the memory buffer, and the minimum allocation size. +** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts +** to using its default memory allocator (the system malloc() implementation), +** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the +** memory pointer is not NULL then the alternative memory +** allocator is engaged to handle all of SQLites memory allocation needs. +** The first pointer (the memory pointer) must be aligned to an 8-byte +** boundary or subsequent behavior of SQLite will be undefined. +** The minimum allocation size is capped at 2**12. Reasonable values +** for the minimum allocation size are 2**5 through 2**8.
+** +** [[SQLITE_CONFIG_MUTEX]]
SQLITE_CONFIG_MUTEX
+**
^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a +** pointer to an instance of the [sqlite3_mutex_methods] structure. +** The argument specifies alternative low-level mutex routines to be used +** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of +** the content of the [sqlite3_mutex_methods] structure before the call to +** [sqlite3_config()] returns. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** the entire mutexing subsystem is omitted from the build and hence calls to +** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will +** return [SQLITE_ERROR].
+** +** [[SQLITE_CONFIG_GETMUTEX]]
SQLITE_CONFIG_GETMUTEX
+**
^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which +** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The +** [sqlite3_mutex_methods] +** structure is filled with the currently defined mutex routines.)^ +** This option can be used to overload the default mutex allocation +** routines with a wrapper used to track mutex usage for performance +** profiling or testing, for example. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** the entire mutexing subsystem is omitted from the build and hence calls to +** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will +** return [SQLITE_ERROR].
+** +** [[SQLITE_CONFIG_LOOKASIDE]]
SQLITE_CONFIG_LOOKASIDE
+**
^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine +** the default size of lookaside memory on each [database connection]. +** The first argument is the +** size of each lookaside buffer slot and the second is the number of +** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE +** sets the default lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] +** option to [sqlite3_db_config()] can be used to change the lookaside +** configuration on individual connections.)^
+** +** [[SQLITE_CONFIG_PCACHE2]]
SQLITE_CONFIG_PCACHE2
+**
^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is +** a pointer to an [sqlite3_pcache_methods2] object. This object specifies +** the interface to a custom page cache implementation.)^ +** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.
+** +** [[SQLITE_CONFIG_GETPCACHE2]]
SQLITE_CONFIG_GETPCACHE2
+**
^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of +** the current page cache implementation into that object.)^
+** +** [[SQLITE_CONFIG_LOG]]
SQLITE_CONFIG_LOG
+**
The SQLITE_CONFIG_LOG option is used to configure the SQLite +** global [error log]. +** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a +** function with a call signature of void(*)(void*,int,const char*), +** and a pointer to void. ^If the function pointer is not NULL, it is +** invoked by [sqlite3_log()] to process each logging event. ^If the +** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. +** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is +** passed through as the first parameter to the application-defined logger +** function whenever that function is invoked. ^The second parameter to +** the logger function is a copy of the first parameter to the corresponding +** [sqlite3_log()] call and is intended to be a [result code] or an +** [extended result code]. ^The third parameter passed to the logger is +** log message after formatting via [sqlite3_snprintf()]. +** The SQLite logging interface is not reentrant; the logger function +** supplied by the application must not invoke any SQLite interface. +** In a multi-threaded application, the application-defined logger +** function must be threadsafe.
+** +** [[SQLITE_CONFIG_URI]]
SQLITE_CONFIG_URI +**
^(The SQLITE_CONFIG_URI option takes a single argument of type int. +** If non-zero, then URI handling is globally enabled. If the parameter is zero, +** then URI handling is globally disabled.)^ ^If URI handling is globally +** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], +** [sqlite3_open16()] or +** specified as part of [ATTACH] commands are interpreted as URIs, regardless +** of whether or not the [SQLITE_OPEN_URI] flag is set when the database +** connection is opened. ^If it is globally disabled, filenames are +** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the +** database connection is opened. ^(By default, URI handling is globally +** disabled. The default value may be changed by compiling with the +** [SQLITE_USE_URI] symbol defined.)^ +** +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]]
SQLITE_CONFIG_COVERING_INDEX_SCAN +**
^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer +** argument which is interpreted as a boolean in order to enable or disable +** the use of covering indices for full table scans in the query optimizer. +** ^The default setting is determined +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" +** if that compile-time option is omitted. +** The ability to disable the use of covering indices for full table scans +** is because some incorrectly coded legacy applications might malfunction +** when the optimization is enabled. Providing the ability to +** disable the optimization allows the older, buggy application code to work +** without change even with newer versions of SQLite. +** +** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]] +**
SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE +**
These options are obsolete and should not be used by new code. +** They are retained for backwards compatibility but are now no-ops. +**
+** +** [[SQLITE_CONFIG_SQLLOG]] +**
SQLITE_CONFIG_SQLLOG +**
This option is only available if sqlite is compiled with the +** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should +** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int). +** The second should be of type (void*). The callback is invoked by the library +** in three separate circumstances, identified by the value passed as the +** fourth parameter. If the fourth parameter is 0, then the database connection +** passed as the second argument has just been opened. The third argument +** points to a buffer containing the name of the main database file. If the +** fourth parameter is 1, then the SQL statement that the third parameter +** points to has just been executed. Or, if the fourth parameter is 2, then +** the connection being passed as the second parameter is being closed. The +** third parameter is passed NULL In this case. An example of using this +** configuration option can be seen in the "test_sqllog.c" source file in +** the canonical SQLite source tree.
+** +** [[SQLITE_CONFIG_MMAP_SIZE]] +**
SQLITE_CONFIG_MMAP_SIZE +**
^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values +** that are the default mmap size limit (the default setting for +** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. +** ^The default setting can be overridden by each database connection using +** either the [PRAGMA mmap_size] command, or by using the +** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size +** will be silently truncated if necessary so that it does not exceed the +** compile-time maximum mmap size set by the +** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ +** ^If either argument to this option is negative, then that argument is +** changed to its compile-time default. +** +** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] +**
SQLITE_CONFIG_WIN32_HEAPSIZE +**
^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro +** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value +** that specifies the maximum size of the created heap. +** +** [[SQLITE_CONFIG_PCACHE_HDRSZ]] +**
SQLITE_CONFIG_PCACHE_HDRSZ +**
^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which +** is a pointer to an integer and writes into that integer the number of extra +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. +** The amount of extra space required can change depending on the compiler, +** target platform, and SQLite version. +** +** [[SQLITE_CONFIG_PMASZ]] +**
SQLITE_CONFIG_PMASZ +**
^The SQLITE_CONFIG_PMASZ option takes a single parameter which +** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded +** sorter to that integer. The default minimum PMA Size is set by the +** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched +** to help with sort operations when multithreaded sorting +** is enabled (using the [PRAGMA threads] command) and the amount of content +** to be sorted exceeds the page size times the minimum of the +** [PRAGMA cache_size] setting and this value. +** +** [[SQLITE_CONFIG_STMTJRNL_SPILL]] +**
SQLITE_CONFIG_STMTJRNL_SPILL +**
^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which +** becomes the [statement journal] spill-to-disk threshold. +** [Statement journals] are held in memory until their size (in bytes) +** exceeds this threshold, at which point they are written to disk. +** Or if the threshold is -1, statement journals are always held +** exclusively in memory. +** Since many statement journals never become large, setting the spill +** threshold to a value such as 64KiB can greatly reduce the amount of +** I/O required to support statement rollback. +** The default value for this setting is controlled by the +** [SQLITE_STMTJRNL_SPILL] compile-time option. +**
+*/ +#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ +#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ +#define SQLITE_CONFIG_SERIALIZED 3 /* nil */ +#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ +#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ +#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ +#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ +#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ +#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ +/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ +#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ +#define SQLITE_CONFIG_PCACHE 14 /* no-op */ +#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ +#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ +#define SQLITE_CONFIG_URI 17 /* int */ +#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ +#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ +#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ +#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ +#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ +#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ +#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ +#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ + +/* +** CAPI3REF: Database Connection Configuration Options +** +** These constants are the available integer configuration options that +** can be passed as the second argument to the [sqlite3_db_config()] interface. +** +** New configuration options may be added in future releases of SQLite. +** Existing configuration options might be discontinued. Applications +** should check the return code from [sqlite3_db_config()] to make sure that +** the call worked. ^The [sqlite3_db_config()] interface will return a +** non-zero [error code] if a discontinued or unsupported configuration option +** is invoked. +** +**
+**
SQLITE_DBCONFIG_LOOKASIDE
+**
^This option takes three additional arguments that determine the +** [lookaside memory allocator] configuration for the [database connection]. +** ^The first argument (the third parameter to [sqlite3_db_config()] is a +** pointer to a memory buffer to use for lookaside memory. +** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb +** may be NULL in which case SQLite will allocate the +** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the +** size of each lookaside buffer slot. ^The third argument is the number of +** slots. The size of the buffer in the first argument must be greater than +** or equal to the product of the second and third arguments. The buffer +** must be aligned to an 8-byte boundary. ^If the second argument to +** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally +** rounded down to the next smaller multiple of 8. ^(The lookaside memory +** configuration for a database connection can only be changed when that +** connection is not currently using lookaside memory, or in other words +** when the "current value" returned by +** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. +** Any attempt to change the lookaside memory configuration when lookaside +** memory is in use leaves the configuration unchanged and returns +** [SQLITE_BUSY].)^
+** +**
SQLITE_DBCONFIG_ENABLE_FKEY
+**
^This option is used to enable or disable the enforcement of +** [foreign key constraints]. There should be two additional arguments. +** The first argument is an integer which is 0 to disable FK enforcement, +** positive to enable FK enforcement or negative to leave FK enforcement +** unchanged. The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether FK enforcement is off or on +** following this call. The second parameter may be a NULL pointer, in +** which case the FK enforcement setting is not reported back.
+** +**
SQLITE_DBCONFIG_ENABLE_TRIGGER
+**
^This option is used to enable or disable [CREATE TRIGGER | triggers]. +** There should be two additional arguments. +** The first argument is an integer which is 0 to disable triggers, +** positive to enable triggers or negative to leave the setting unchanged. +** The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether triggers are disabled or enabled +** following this call. The second parameter may be a NULL pointer, in +** which case the trigger setting is not reported back.
+** +**
SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
+**
^This option is used to enable or disable the two-argument +** version of the [fts3_tokenizer()] function which is part of the +** [FTS3] full-text search engine extension. +** There should be two additional arguments. +** The first argument is an integer which is 0 to disable fts3_tokenizer() or +** positive to enable fts3_tokenizer() or negative to leave the setting +** unchanged. +** The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled +** following this call. The second parameter may be a NULL pointer, in +** which case the new setting is not reported back.
+** +**
SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
+**
^This option is used to enable or disable the [sqlite3_load_extension()] +** interface independently of the [load_extension()] SQL function. +** The [sqlite3_enable_load_extension()] API enables or disables both the +** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. +** There should be two additional arguments. +** When the first argument to this interface is 1, then only the C-API is +** enabled and the SQL function remains disabled. If the first argment to +** this interface is 0, then both the C-API and the SQL function are disabled. +** If the first argument is -1, then no changes are made to state of either the +** C-API or the SQL function. +** The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface +** is disabled or enabled following this call. The second parameter may +** be a NULL pointer, in which case the new setting is not reported back. +**
+** +**
+*/ +#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ +#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ + + +/* +** CAPI3REF: Enable Or Disable Extended Result Codes +** METHOD: sqlite3 +** +** ^The sqlite3_extended_result_codes() routine enables or disables the +** [extended result codes] feature of SQLite. ^The extended result +** codes are disabled by default for historical compatibility. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff); + +/* +** CAPI3REF: Last Insert Rowid +** METHOD: sqlite3 +** +** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables) +** has a unique 64-bit signed +** integer key called the [ROWID | "rowid"]. ^The rowid is always available +** as an undeclared column named ROWID, OID, or _ROWID_ as long as those +** names are not also used by explicitly declared columns. ^If +** the table has a column of type [INTEGER PRIMARY KEY] then that column +** is another alias for the rowid. +** +** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the +** most recent successful [INSERT] into a rowid table or [virtual table] +** on database connection D. +** ^Inserts into [WITHOUT ROWID] tables are not recorded. +** ^If no successful [INSERT]s into rowid tables +** have ever occurred on the database connection D, +** then sqlite3_last_insert_rowid(D) returns zero. +** +** ^(If an [INSERT] occurs within a trigger or within a [virtual table] +** method, then this routine will return the [rowid] of the inserted +** row as long as the trigger or virtual table method is running. +** But once the trigger or virtual table method ends, the value returned +** by this routine reverts to what it was before the trigger or virtual +** table method began.)^ +** +** ^An [INSERT] that fails due to a constraint violation is not a +** successful [INSERT] and does not change the value returned by this +** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, +** and INSERT OR ABORT make no changes to the return value of this +** routine when their insertion fails. ^(When INSERT OR REPLACE +** encounters a constraint violation, it does not fail. The +** INSERT continues to completion after deleting rows that caused +** the constraint problem so INSERT OR REPLACE will always change +** the return value of this interface.)^ +** +** ^For the purposes of this routine, an [INSERT] is considered to +** be successful even if it is subsequently rolled back. +** +** This function is accessible to SQL statements via the +** [last_insert_rowid() SQL function]. +** +** If a separate thread performs a new [INSERT] on the same +** database connection while the [sqlite3_last_insert_rowid()] +** function is running and thus changes the last insert [rowid], +** then the value returned by [sqlite3_last_insert_rowid()] is +** unpredictable and might not equal either the old or the new +** last insert [rowid]. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*); + +/* +** CAPI3REF: Count The Number Of Rows Modified +** METHOD: sqlite3 +** +** ^This function returns the number of rows modified, inserted or +** deleted by the most recently completed INSERT, UPDATE or DELETE +** statement on the database connection specified by the only parameter. +** ^Executing any other type of SQL statement does not modify the value +** returned by this function. +** +** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], +** [foreign key actions] or [REPLACE] constraint resolution are not counted. +** +** Changes to a view that are intercepted by +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or +** DELETE statement run on a view is always zero. Only changes made to real +** tables are counted. +** +** Things are more complicated if the sqlite3_changes() function is +** executed while a trigger program is running. This may happen if the +** program uses the [changes() SQL function], or if some other callback +** function invokes sqlite3_changes() directly. Essentially: +** +**
    +**
  • ^(Before entering a trigger program the value returned by +** sqlite3_changes() function is saved. After the trigger program +** has finished, the original value is restored.)^ +** +**
  • ^(Within a trigger program each INSERT, UPDATE and DELETE +** statement sets the value returned by sqlite3_changes() +** upon completion as normal. Of course, this value will not include +** any changes performed by sub-triggers, as the sqlite3_changes() +** value will be saved and restored after each sub-trigger has run.)^ +**
+** +** ^This means that if the changes() SQL function (or similar) is used +** by the first INSERT, UPDATE or DELETE statement within a trigger, it +** returns the value as set when the calling statement began executing. +** ^If it is used by the second or subsequent such statement within a trigger +** program, the value returned reflects the number of rows modified by the +** previous INSERT, UPDATE or DELETE statement within the same trigger. +** +** See also the [sqlite3_total_changes()] interface, the +** [count_changes pragma], and the [changes() SQL function]. +** +** If a separate thread makes changes on the same database connection +** while [sqlite3_changes()] is running then the value returned +** is unpredictable and not meaningful. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*); + +/* +** CAPI3REF: Total Number Of Rows Modified +** METHOD: sqlite3 +** +** ^This function returns the total number of rows inserted, modified or +** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed +** since the database connection was opened, including those executed as +** part of trigger programs. ^Executing any other type of SQL statement +** does not affect the value returned by sqlite3_total_changes(). +** +** ^Changes made as part of [foreign key actions] are included in the +** count, but those made as part of REPLACE constraint resolution are +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers +** are not counted. +** +** See also the [sqlite3_changes()] interface, the +** [count_changes pragma], and the [total_changes() SQL function]. +** +** If a separate thread makes changes on the same database connection +** while [sqlite3_total_changes()] is running then the value +** returned is unpredictable and not meaningful. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*); + +/* +** CAPI3REF: Interrupt A Long-Running Query +** METHOD: sqlite3 +** +** ^This function causes any pending database operation to abort and +** return at its earliest opportunity. This routine is typically +** called in response to a user action such as pressing "Cancel" +** or Ctrl-C where the user wants a long query operation to halt +** immediately. +** +** ^It is safe to call this routine from a thread different from the +** thread that is currently running the database operation. But it +** is not safe to call this routine with a [database connection] that +** is closed or might close before sqlite3_interrupt() returns. +** +** ^If an SQL operation is very nearly finished at the time when +** sqlite3_interrupt() is called, then it might not have an opportunity +** to be interrupted and might continue to completion. +** +** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT]. +** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE +** that is inside an explicit transaction, then the entire transaction +** will be rolled back automatically. +** +** ^The sqlite3_interrupt(D) call is in effect until all currently running +** SQL statements on [database connection] D complete. ^Any new SQL statements +** that are started after the sqlite3_interrupt() call and before the +** running statements reaches zero are interrupted as if they had been +** running prior to the sqlite3_interrupt() call. ^New SQL statements +** that are started after the running statement count reaches zero are +** not effected by the sqlite3_interrupt(). +** ^A call to sqlite3_interrupt(D) that occurs when there are no running +** SQL statements is a no-op and has no effect on SQL statements +** that are started after the sqlite3_interrupt() call returns. +** +** If the database connection closes while [sqlite3_interrupt()] +** is running then bad things will likely happen. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*); + +/* +** CAPI3REF: Determine If An SQL Statement Is Complete +** +** These routines are useful during command-line input to determine if the +** currently entered text seems to form a complete SQL statement or +** if additional input is needed before sending the text into +** SQLite for parsing. ^These routines return 1 if the input string +** appears to be a complete SQL statement. ^A statement is judged to be +** complete if it ends with a semicolon token and is not a prefix of a +** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within +** string literals or quoted identifier names or comments are not +** independent tokens (they are part of the token in which they are +** embedded) and thus do not count as a statement terminator. ^Whitespace +** and comments that follow the final semicolon are ignored. +** +** ^These routines return 0 if the statement is incomplete. ^If a +** memory allocation fails, then SQLITE_NOMEM is returned. +** +** ^These routines do not parse the SQL statements thus +** will not detect syntactically incorrect SQL. +** +** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior +** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked +** automatically by sqlite3_complete16(). If that initialization fails, +** then the return value from sqlite3_complete16() will be non-zero +** regardless of whether or not the input SQL is complete.)^ +** +** The input to [sqlite3_complete()] must be a zero-terminated +** UTF-8 string. +** +** The input to [sqlite3_complete16()] must be a zero-terminated +** UTF-16 string in native byte order. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql); +SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql); + +/* +** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors +** KEYWORDS: {busy-handler callback} {busy handler} +** METHOD: sqlite3 +** +** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X +** that might be invoked with argument P whenever +** an attempt is made to access a database table associated with +** [database connection] D when another thread +** or process has the table locked. +** The sqlite3_busy_handler() interface is used to implement +** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout]. +** +** ^If the busy callback is NULL, then [SQLITE_BUSY] +** is returned immediately upon encountering the lock. ^If the busy callback +** is not NULL, then the callback might be invoked with two arguments. +** +** ^The first argument to the busy handler is a copy of the void* pointer which +** is the third argument to sqlite3_busy_handler(). ^The second argument to +** the busy handler callback is the number of times that the busy handler has +** been invoked previously for the same locking event. ^If the +** busy callback returns 0, then no additional attempts are made to +** access the database and [SQLITE_BUSY] is returned +** to the application. +** ^If the callback returns non-zero, then another attempt +** is made to access the database and the cycle repeats. +** +** The presence of a busy handler does not guarantee that it will be invoked +** when there is lock contention. ^If SQLite determines that invoking the busy +** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY] +** to the application instead of invoking the +** busy handler. +** Consider a scenario where one process is holding a read lock that +** it is trying to promote to a reserved lock and +** a second process is holding a reserved lock that it is trying +** to promote to an exclusive lock. The first process cannot proceed +** because it is blocked by the second and the second process cannot +** proceed because it is blocked by the first. If both processes +** invoke the busy handlers, neither will make any progress. Therefore, +** SQLite returns [SQLITE_BUSY] for the first process, hoping that this +** will induce the first process to release its read lock and allow +** the second process to proceed. +** +** ^The default busy callback is NULL. +** +** ^(There can only be a single busy handler defined for each +** [database connection]. Setting a new busy handler clears any +** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()] +** or evaluating [PRAGMA busy_timeout=N] will change the +** busy handler and thus clear any previously set busy handler. +** +** The busy callback should not take any actions which modify the +** database connection that invoked the busy handler. In other words, +** the busy handler is not reentrant. Any such actions +** result in undefined behavior. +** +** A busy handler must not close the database connection +** or [prepared statement] that invoked the busy handler. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); + +/* +** CAPI3REF: Set A Busy Timeout +** METHOD: sqlite3 +** +** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps +** for a specified amount of time when a table is locked. ^The handler +** will sleep multiple times until at least "ms" milliseconds of sleeping +** have accumulated. ^After at least "ms" milliseconds of sleeping, +** the handler returns 0 which causes [sqlite3_step()] to return +** [SQLITE_BUSY]. +** +** ^Calling this routine with an argument less than or equal to zero +** turns off all busy handlers. +** +** ^(There can only be a single busy handler for a particular +** [database connection] at any given moment. If another busy handler +** was defined (using [sqlite3_busy_handler()]) prior to calling +** this routine, that other busy handler is cleared.)^ +** +** See also: [PRAGMA busy_timeout] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms); + +/* +** CAPI3REF: Convenience Routines For Running Queries +** METHOD: sqlite3 +** +** This is a legacy interface that is preserved for backwards compatibility. +** Use of this interface is not recommended. +** +** Definition: A result table is memory data structure created by the +** [sqlite3_get_table()] interface. A result table records the +** complete query results from one or more queries. +** +** The table conceptually has a number of rows and columns. But +** these numbers are not part of the result table itself. These +** numbers are obtained separately. Let N be the number of rows +** and M be the number of columns. +** +** A result table is an array of pointers to zero-terminated UTF-8 strings. +** There are (N+1)*M elements in the array. The first M pointers point +** to zero-terminated strings that contain the names of the columns. +** The remaining entries all point to query results. NULL values result +** in NULL pointers. All other values are in their UTF-8 zero-terminated +** string representation as returned by [sqlite3_column_text()]. +** +** A result table might consist of one or more memory allocations. +** It is not safe to pass a result table directly to [sqlite3_free()]. +** A result table should be deallocated using [sqlite3_free_table()]. +** +** ^(As an example of the result table format, suppose a query result +** is as follows: +** +**
+**        Name        | Age
+**        -----------------------
+**        Alice       | 43
+**        Bob         | 28
+**        Cindy       | 21
+** 
+** +** There are two column (M==2) and three rows (N==3). Thus the +** result table has 8 entries. Suppose the result table is stored +** in an array names azResult. Then azResult holds this content: +** +**
+**        azResult[0] = "Name";
+**        azResult[1] = "Age";
+**        azResult[2] = "Alice";
+**        azResult[3] = "43";
+**        azResult[4] = "Bob";
+**        azResult[5] = "28";
+**        azResult[6] = "Cindy";
+**        azResult[7] = "21";
+** 
)^ +** +** ^The sqlite3_get_table() function evaluates one or more +** semicolon-separated SQL statements in the zero-terminated UTF-8 +** string of its 2nd parameter and returns a result table to the +** pointer given in its 3rd parameter. +** +** After the application has finished with the result from sqlite3_get_table(), +** it must pass the result table pointer to sqlite3_free_table() in order to +** release the memory that was malloced. Because of the way the +** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling +** function must not try to call [sqlite3_free()] directly. Only +** [sqlite3_free_table()] is able to release the memory properly and safely. +** +** The sqlite3_get_table() interface is implemented as a wrapper around +** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access +** to any internal data structures of SQLite. It uses only the public +** interface defined here. As a consequence, errors that occur in the +** wrapper layer outside of the internal [sqlite3_exec()] call are not +** reflected in subsequent calls to [sqlite3_errcode()] or +** [sqlite3_errmsg()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_get_table( + sqlite3 *db, /* An open database */ + const char *zSql, /* SQL to be evaluated */ + char ***pazResult, /* Results of the query */ + int *pnRow, /* Number of result rows written here */ + int *pnColumn, /* Number of result columns written here */ + char **pzErrmsg /* Error msg written here */ +); +SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result); + +/* +** CAPI3REF: Formatted String Printing Functions +** +** These routines are work-alikes of the "printf()" family of functions +** from the standard C library. +** These routines understand most of the common K&R formatting options, +** plus some additional non-standard formats, detailed below. +** Note that some of the more obscure formatting options from recent +** C-library standards are omitted from this implementation. +** +** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their +** results into memory obtained from [sqlite3_malloc()]. +** The strings returned by these two routines should be +** released by [sqlite3_free()]. ^Both routines return a +** NULL pointer if [sqlite3_malloc()] is unable to allocate enough +** memory to hold the resulting string. +** +** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from +** the standard C library. The result is written into the +** buffer supplied as the second parameter whose size is given by +** the first parameter. Note that the order of the +** first two parameters is reversed from snprintf().)^ This is an +** historical accident that cannot be fixed without breaking +** backwards compatibility. ^(Note also that sqlite3_snprintf() +** returns a pointer to its buffer instead of the number of +** characters actually written into the buffer.)^ We admit that +** the number of characters written would be a more useful return +** value but we cannot change the implementation of sqlite3_snprintf() +** now without breaking compatibility. +** +** ^As long as the buffer size is greater than zero, sqlite3_snprintf() +** guarantees that the buffer is always zero-terminated. ^The first +** parameter "n" is the total size of the buffer, including space for +** the zero terminator. So the longest string that can be completely +** written will be n-1 characters. +** +** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf(). +** +** These routines all implement some additional formatting +** options that are useful for constructing SQL statements. +** All of the usual printf() formatting options apply. In addition, there +** is are "%q", "%Q", "%w" and "%z" options. +** +** ^(The %q option works like %s in that it substitutes a nul-terminated +** string from the argument list. But %q also doubles every '\'' character. +** %q is designed for use inside a string literal.)^ By doubling each '\'' +** character it escapes that character and allows it to be inserted into +** the string. +** +** For example, assume the string variable zText contains text as follows: +** +**
+**  char *zText = "It's a happy day!";
+** 
+** +** One can use this text in an SQL statement as follows: +** +**
+**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
+**  sqlite3_exec(db, zSQL, 0, 0, 0);
+**  sqlite3_free(zSQL);
+** 
+** +** Because the %q format string is used, the '\'' character in zText +** is escaped and the SQL generated is as follows: +** +**
+**  INSERT INTO table1 VALUES('It''s a happy day!')
+** 
+** +** This is correct. Had we used %s instead of %q, the generated SQL +** would have looked like this: +** +**
+**  INSERT INTO table1 VALUES('It's a happy day!');
+** 
+** +** This second example is an SQL syntax error. As a general rule you should +** always use %q instead of %s when inserting text into a string literal. +** +** ^(The %Q option works like %q except it also adds single quotes around +** the outside of the total string. Additionally, if the parameter in the +** argument list is a NULL pointer, %Q substitutes the text "NULL" (without +** single quotes).)^ So, for example, one could say: +** +**
+**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
+**  sqlite3_exec(db, zSQL, 0, 0, 0);
+**  sqlite3_free(zSQL);
+** 
+** +** The code above will render a correct SQL statement in the zSQL +** variable even if the zText variable is a NULL pointer. +** +** ^(The "%w" formatting option is like "%q" except that it expects to +** be contained within double-quotes instead of single quotes, and it +** escapes the double-quote character instead of the single-quote +** character.)^ The "%w" formatting option is intended for safely inserting +** table and column names into a constructed SQL statement. +** +** ^(The "%z" formatting option works like "%s" but with the +** addition that after the string has been read and copied into +** the result, [sqlite3_free()] is called on the input string.)^ +*/ +SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char*,...); +SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char*, va_list); +SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int,char*,const char*, ...); +SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list); + +/* +** CAPI3REF: Memory Allocation Subsystem +** +** The SQLite core uses these three routines for all of its own +** internal memory allocation needs. "Core" in the previous sentence +** does not include operating-system specific VFS implementation. The +** Windows VFS uses native malloc() and free() for some operations. +** +** ^The sqlite3_malloc() routine returns a pointer to a block +** of memory at least N bytes in length, where N is the parameter. +** ^If sqlite3_malloc() is unable to obtain sufficient free +** memory, it returns a NULL pointer. ^If the parameter N to +** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns +** a NULL pointer. +** +** ^The sqlite3_malloc64(N) routine works just like +** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead +** of a signed 32-bit integer. +** +** ^Calling sqlite3_free() with a pointer previously returned +** by sqlite3_malloc() or sqlite3_realloc() releases that memory so +** that it might be reused. ^The sqlite3_free() routine is +** a no-op if is called with a NULL pointer. Passing a NULL pointer +** to sqlite3_free() is harmless. After being freed, memory +** should neither be read nor written. Even reading previously freed +** memory might result in a segmentation fault or other severe error. +** Memory corruption, a segmentation fault, or other severe error +** might result if sqlite3_free() is called with a non-NULL pointer that +** was not obtained from sqlite3_malloc() or sqlite3_realloc(). +** +** ^The sqlite3_realloc(X,N) interface attempts to resize a +** prior memory allocation X to be at least N bytes. +** ^If the X parameter to sqlite3_realloc(X,N) +** is a NULL pointer then its behavior is identical to calling +** sqlite3_malloc(N). +** ^If the N parameter to sqlite3_realloc(X,N) is zero or +** negative then the behavior is exactly the same as calling +** sqlite3_free(X). +** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation +** of at least N bytes in size or NULL if insufficient memory is available. +** ^If M is the size of the prior allocation, then min(N,M) bytes +** of the prior allocation are copied into the beginning of buffer returned +** by sqlite3_realloc(X,N) and the prior allocation is freed. +** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the +** prior allocation is not freed. +** +** ^The sqlite3_realloc64(X,N) interfaces works the same as +** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead +** of a 32-bit signed integer. +** +** ^If X is a memory allocation previously obtained from sqlite3_malloc(), +** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then +** sqlite3_msize(X) returns the size of that memory allocation in bytes. +** ^The value returned by sqlite3_msize(X) might be larger than the number +** of bytes requested when X was allocated. ^If X is a NULL pointer then +** sqlite3_msize(X) returns zero. If X points to something that is not +** the beginning of memory allocation, or if it points to a formerly +** valid memory allocation that has now been freed, then the behavior +** of sqlite3_msize(X) is undefined and possibly harmful. +** +** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(), +** sqlite3_malloc64(), and sqlite3_realloc64() +** is always aligned to at least an 8 byte boundary, or to a +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time +** option is used. +** +** In SQLite version 3.5.0 and 3.5.1, it was possible to define +** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in +** implementation of these routines to be omitted. That capability +** is no longer provided. Only built-in memory allocators can be used. +** +** Prior to SQLite version 3.7.10, the Windows OS interface layer called +** the system malloc() and free() directly when converting +** filenames between the UTF-8 encoding used by SQLite +** and whatever filename encoding is used by the particular Windows +** installation. Memory allocation errors were detected, but +** they were reported back as [SQLITE_CANTOPEN] or +** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. +** +** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] +** must be either NULL or else pointers obtained from a prior +** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have +** not yet been released. +** +** The application must not read or write any part of +** a block of memory after it has been released using +** [sqlite3_free()] or [sqlite3_realloc()]. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int); +SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64); +SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void*, int); +SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void*, sqlite3_uint64); +SQLITE_API void SQLITE_STDCALL sqlite3_free(void*); +SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*); + +/* +** CAPI3REF: Memory Allocator Statistics +** +** SQLite provides these two interfaces for reporting on the status +** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] +** routines, which form the built-in memory allocation subsystem. +** +** ^The [sqlite3_memory_used()] routine returns the number of bytes +** of memory currently outstanding (malloced but not freed). +** ^The [sqlite3_memory_highwater()] routine returns the maximum +** value of [sqlite3_memory_used()] since the high-water mark +** was last reset. ^The values returned by [sqlite3_memory_used()] and +** [sqlite3_memory_highwater()] include any overhead +** added by SQLite in its implementation of [sqlite3_malloc()], +** but not overhead added by the any underlying system library +** routines that [sqlite3_malloc()] may call. +** +** ^The memory high-water mark is reset to the current value of +** [sqlite3_memory_used()] if and only if the parameter to +** [sqlite3_memory_highwater()] is true. ^The value returned +** by [sqlite3_memory_highwater(1)] is the high-water mark +** prior to the reset. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void); +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag); + +/* +** CAPI3REF: Pseudo-Random Number Generator +** +** SQLite contains a high-quality pseudo-random number generator (PRNG) used to +** select random [ROWID | ROWIDs] when inserting new records into a table that +** already uses the largest possible [ROWID]. The PRNG is also used for +** the build-in random() and randomblob() SQL functions. This interface allows +** applications to access the same PRNG for other purposes. +** +** ^A call to this routine stores N bytes of randomness into buffer P. +** ^The P parameter can be a NULL pointer. +** +** ^If this routine has not been previously called or if the previous +** call had N less than one or a NULL pointer for P, then the PRNG is +** seeded using randomness obtained from the xRandomness method of +** the default [sqlite3_vfs] object. +** ^If the previous call to this routine had an N of 1 or more and a +** non-NULL P then the pseudo-randomness is generated +** internally and without recourse to the [sqlite3_vfs] xRandomness +** method. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P); + +/* +** CAPI3REF: Compile-Time Authorization Callbacks +** METHOD: sqlite3 +** +** ^This routine registers an authorizer callback with a particular +** [database connection], supplied in the first argument. +** ^The authorizer callback is invoked as SQL statements are being compiled +** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various +** points during the compilation process, as logic is being created +** to perform various actions, the authorizer callback is invoked to +** see if those actions are allowed. ^The authorizer callback should +** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the +** specific action but allow the SQL statement to continue to be +** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be +** rejected with an error. ^If the authorizer callback returns +** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] +** then the [sqlite3_prepare_v2()] or equivalent call that triggered +** the authorizer will fail with an error message. +** +** When the callback returns [SQLITE_OK], that means the operation +** requested is ok. ^When the callback returns [SQLITE_DENY], the +** [sqlite3_prepare_v2()] or equivalent call that triggered the +** authorizer will fail with an error message explaining that +** access is denied. +** +** ^The first parameter to the authorizer callback is a copy of the third +** parameter to the sqlite3_set_authorizer() interface. ^The second parameter +** to the callback is an integer [SQLITE_COPY | action code] that specifies +** the particular action to be authorized. ^The third through sixth parameters +** to the callback are zero-terminated strings that contain additional +** details about the action to be authorized. +** +** ^If the action code is [SQLITE_READ] +** and the callback returns [SQLITE_IGNORE] then the +** [prepared statement] statement is constructed to substitute +** a NULL value in place of the table column that would have +** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] +** return can be used to deny an untrusted user access to individual +** columns of a table. +** ^If the action code is [SQLITE_DELETE] and the callback returns +** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the +** [truncate optimization] is disabled and all rows are deleted individually. +** +** An authorizer is used when [sqlite3_prepare | preparing] +** SQL statements from an untrusted source, to ensure that the SQL statements +** do not try to access data they are not allowed to see, or that they do not +** try to execute malicious statements that damage the database. For +** example, an application may allow a user to enter arbitrary +** SQL queries for evaluation by a database. But the application does +** not want the user to be able to make arbitrary changes to the +** database. An authorizer could then be put in place while the +** user-entered SQL is being [sqlite3_prepare | prepared] that +** disallows everything except [SELECT] statements. +** +** Applications that need to process SQL from untrusted sources +** might also consider lowering resource limits using [sqlite3_limit()] +** and limiting database size using the [max_page_count] [PRAGMA] +** in addition to using an authorizer. +** +** ^(Only a single authorizer can be in place on a database connection +** at a time. Each call to sqlite3_set_authorizer overrides the +** previous call.)^ ^Disable the authorizer by installing a NULL callback. +** The authorizer is disabled by default. +** +** The authorizer callback must not do anything that will modify +** the database connection that invoked the authorizer callback. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the +** statement might be re-prepared during [sqlite3_step()] due to a +** schema change. Hence, the application should ensure that the +** correct authorizer callback remains in place during the [sqlite3_step()]. +** +** ^Note that the authorizer callback is invoked only during +** [sqlite3_prepare()] or its variants. Authorization is not +** performed during statement evaluation in [sqlite3_step()], unless +** as stated in the previous paragraph, sqlite3_step() invokes +** sqlite3_prepare_v2() to reprepare a statement after a schema change. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer( + sqlite3*, + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), + void *pUserData +); + +/* +** CAPI3REF: Authorizer Return Codes +** +** The [sqlite3_set_authorizer | authorizer callback function] must +** return either [SQLITE_OK] or one of these two constants in order +** to signal SQLite whether or not the action is permitted. See the +** [sqlite3_set_authorizer | authorizer documentation] for additional +** information. +** +** Note that SQLITE_IGNORE is also used as a [conflict resolution mode] +** returned from the [sqlite3_vtab_on_conflict()] interface. +*/ +#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ +#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ + +/* +** CAPI3REF: Authorizer Action Codes +** +** The [sqlite3_set_authorizer()] interface registers a callback function +** that is invoked to authorize certain SQL statement actions. The +** second parameter to the callback is an integer code that specifies +** what action is being authorized. These are the integer action codes that +** the authorizer callback may be passed. +** +** These action code values signify what kind of operation is to be +** authorized. The 3rd and 4th parameters to the authorization +** callback function will be parameters or NULL depending on which of these +** codes is used as the second parameter. ^(The 5th parameter to the +** authorizer callback is the name of the database ("main", "temp", +** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback +** is the name of the inner-most trigger or view that is responsible for +** the access attempt or NULL if this access attempt is directly from +** top-level SQL code. +*/ +/******************************************* 3rd ************ 4th ***********/ +#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ +#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ +#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ +#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ +#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ +#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ +#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ +#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ +#define SQLITE_DELETE 9 /* Table Name NULL */ +#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ +#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ +#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ +#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ +#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ +#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ +#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ +#define SQLITE_DROP_VIEW 17 /* View Name NULL */ +#define SQLITE_INSERT 18 /* Table Name NULL */ +#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ +#define SQLITE_READ 20 /* Table Name Column Name */ +#define SQLITE_SELECT 21 /* NULL NULL */ +#define SQLITE_TRANSACTION 22 /* Operation NULL */ +#define SQLITE_UPDATE 23 /* Table Name Column Name */ +#define SQLITE_ATTACH 24 /* Filename NULL */ +#define SQLITE_DETACH 25 /* Database Name NULL */ +#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ +#define SQLITE_REINDEX 27 /* Index Name NULL */ +#define SQLITE_ANALYZE 28 /* Table Name NULL */ +#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ +#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ +#define SQLITE_FUNCTION 31 /* NULL Function Name */ +#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ +#define SQLITE_COPY 0 /* No longer used */ +#define SQLITE_RECURSIVE 33 /* NULL NULL */ + +/* +** CAPI3REF: Tracing And Profiling Functions +** METHOD: sqlite3 +** +** These routines register callback functions that can be used for +** tracing and profiling the execution of SQL statements. +** +** ^The callback function registered by sqlite3_trace() is invoked at +** various times when an SQL statement is being run by [sqlite3_step()]. +** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the +** SQL statement text as the statement first begins executing. +** ^(Additional sqlite3_trace() callbacks might occur +** as each triggered subprogram is entered. The callbacks for triggers +** contain a UTF-8 SQL comment that identifies the trigger.)^ +** +** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit +** the length of [bound parameter] expansion in the output of sqlite3_trace(). +** +** ^The callback function registered by sqlite3_profile() is invoked +** as each SQL statement finishes. ^The profile callback contains +** the original statement text and an estimate of wall-clock time +** of how long that statement took to run. ^The profile callback +** time is in units of nanoseconds, however the current implementation +** is only capable of millisecond resolution so the six least significant +** digits in the time are meaningless. Future versions of SQLite +** might provide greater resolution on the profiler callback. The +** sqlite3_profile() function is considered experimental and is +** subject to change in future versions of SQLite. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); +SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*, + void(*xProfile)(void*,const char*,sqlite3_uint64), void*); + +/* +** CAPI3REF: Query Progress Callbacks +** METHOD: sqlite3 +** +** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback +** function X to be invoked periodically during long running calls to +** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for +** database connection D. An example use for this +** interface is to keep a GUI updated during a large query. +** +** ^The parameter P is passed through as the only parameter to the +** callback function X. ^The parameter N is the approximate number of +** [virtual machine instructions] that are evaluated between successive +** invocations of the callback X. ^If N is less than one then the progress +** handler is disabled. +** +** ^Only a single progress handler may be defined at one time per +** [database connection]; setting a new progress handler cancels the +** old one. ^Setting parameter X to NULL disables the progress handler. +** ^The progress handler is also disabled by setting N to a value less +** than 1. +** +** ^If the progress callback returns non-zero, the operation is +** interrupted. This feature can be used to implement a +** "Cancel" button on a GUI progress dialog box. +** +** The progress handler callback must not do anything that will modify +** the database connection that invoked the progress handler. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); + +/* +** CAPI3REF: Opening A New Database Connection +** CONSTRUCTOR: sqlite3 +** +** ^These routines open an SQLite database file as specified by the +** filename argument. ^The filename argument is interpreted as UTF-8 for +** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte +** order for sqlite3_open16(). ^(A [database connection] handle is usually +** returned in *ppDb, even if an error occurs. The only exception is that +** if SQLite is unable to allocate memory to hold the [sqlite3] object, +** a NULL will be written into *ppDb instead of a pointer to the [sqlite3] +** object.)^ ^(If the database is opened (and/or created) successfully, then +** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The +** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain +** an English language description of the error following a failure of any +** of the sqlite3_open() routines. +** +** ^The default encoding will be UTF-8 for databases created using +** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases +** created using sqlite3_open16() will be UTF-16 in the native byte order. +** +** Whether or not an error occurs when it is opened, resources +** associated with the [database connection] handle should be released by +** passing it to [sqlite3_close()] when it is no longer required. +** +** The sqlite3_open_v2() interface works like sqlite3_open() +** except that it accepts two additional parameters for additional control +** over the new database connection. ^(The flags parameter to +** sqlite3_open_v2() can take one of +** the following three values, optionally combined with the +** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE], +** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^ +** +**
+** ^(
[SQLITE_OPEN_READONLY]
+**
The database is opened in read-only mode. If the database does not +** already exist, an error is returned.
)^ +** +** ^(
[SQLITE_OPEN_READWRITE]
+**
The database is opened for reading and writing if possible, or reading +** only if the file is write protected by the operating system. In either +** case the database must already exist, otherwise an error is returned.
)^ +** +** ^(
[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]
+**
The database is opened for reading and writing, and is created if +** it does not already exist. This is the behavior that is always used for +** sqlite3_open() and sqlite3_open16().
)^ +**
+** +** If the 3rd parameter to sqlite3_open_v2() is not one of the +** combinations shown above optionally combined with other +** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits] +** then the behavior is undefined. +** +** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection +** opens in the multi-thread [threading mode] as long as the single-thread +** mode has not been set at compile-time or start-time. ^If the +** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens +** in the serialized [threading mode] unless single-thread was +** previously selected at compile-time or start-time. +** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be +** eligible to use [shared cache mode], regardless of whether or not shared +** cache is enabled using [sqlite3_enable_shared_cache()]. ^The +** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not +** participate in [shared cache mode] even if it is enabled. +** +** ^The fourth parameter to sqlite3_open_v2() is the name of the +** [sqlite3_vfs] object that defines the operating system interface that +** the new database connection should use. ^If the fourth parameter is +** a NULL pointer then the default [sqlite3_vfs] object is used. +** +** ^If the filename is ":memory:", then a private, temporary in-memory database +** is created for the connection. ^This in-memory database will vanish when +** the database connection is closed. Future versions of SQLite might +** make use of additional special filenames that begin with the ":" character. +** It is recommended that when a database filename actually does begin with +** a ":" character you should prefix the filename with a pathname such as +** "./" to avoid ambiguity. +** +** ^If the filename is an empty string, then a private, temporary +** on-disk database will be created. ^This private database will be +** automatically deleted as soon as the database connection is closed. +** +** [[URI filenames in sqlite3_open()]]

URI Filenames

+** +** ^If [URI filename] interpretation is enabled, and the filename argument +** begins with "file:", then the filename is interpreted as a URI. ^URI +** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is +** set in the fourth argument to sqlite3_open_v2(), or if it has +** been enabled globally using the [SQLITE_CONFIG_URI] option with the +** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option. +** As of SQLite version 3.7.7, URI filename interpretation is turned off +** by default, but future releases of SQLite might enable URI filename +** interpretation by default. See "[URI filenames]" for additional +** information. +** +** URI filenames are parsed according to RFC 3986. ^If the URI contains an +** authority, then it must be either an empty string or the string +** "localhost". ^If the authority is not an empty string or "localhost", an +** error is returned to the caller. ^The fragment component of a URI, if +** present, is ignored. +** +** ^SQLite uses the path component of the URI as the name of the disk file +** which contains the database. ^If the path begins with a '/' character, +** then it is interpreted as an absolute path. ^If the path does not begin +** with a '/' (meaning that the authority section is omitted from the URI) +** then the path is interpreted as a relative path. +** ^(On windows, the first component of an absolute path +** is a drive specification (e.g. "C:").)^ +** +** [[core URI query parameters]] +** The query component of a URI may contain parameters that are interpreted +** either by SQLite itself, or by a [VFS | custom VFS implementation]. +** SQLite and its built-in [VFSes] interpret the +** following query parameters: +** +**
    +**
  • vfs: ^The "vfs" parameter may be used to specify the name of +** a VFS object that provides the operating system interface that should +** be used to access the database file on disk. ^If this option is set to +** an empty string the default VFS object is used. ^Specifying an unknown +** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is +** present, then the VFS specified by the option takes precedence over +** the value passed as the fourth parameter to sqlite3_open_v2(). +** +**
  • mode: ^(The mode parameter may be set to either "ro", "rw", +** "rwc", or "memory". Attempting to set it to any other value is +** an error)^. +** ^If "ro" is specified, then the database is opened for read-only +** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the +** third argument to sqlite3_open_v2(). ^If the mode option is set to +** "rw", then the database is opened for read-write (but not create) +** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had +** been set. ^Value "rwc" is equivalent to setting both +** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is +** set to "memory" then a pure [in-memory database] that never reads +** or writes from disk is used. ^It is an error to specify a value for +** the mode parameter that is less restrictive than that specified by +** the flags passed in the third parameter to sqlite3_open_v2(). +** +**
  • cache: ^The cache parameter may be set to either "shared" or +** "private". ^Setting it to "shared" is equivalent to setting the +** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to +** sqlite3_open_v2(). ^Setting the cache parameter to "private" is +** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. +** ^If sqlite3_open_v2() is used and the "cache" parameter is present in +** a URI filename, its value overrides any behavior requested by setting +** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. +** +**
  • psow: ^The psow parameter indicates whether or not the +** [powersafe overwrite] property does or does not apply to the +** storage media on which the database file resides. +** +**
  • nolock: ^The nolock parameter is a boolean query parameter +** which if set disables file locking in rollback journal modes. This +** is useful for accessing a database on a filesystem that does not +** support locking. Caution: Database corruption might result if two +** or more processes write to the same database and any one of those +** processes uses nolock=1. +** +**
  • immutable: ^The immutable parameter is a boolean query +** parameter that indicates that the database file is stored on +** read-only media. ^When immutable is set, SQLite assumes that the +** database file cannot be changed, even by a process with higher +** privilege, and so the database is opened read-only and all locking +** and change detection is disabled. Caution: Setting the immutable +** property on a database file that does in fact change can result +** in incorrect query results and/or [SQLITE_CORRUPT] errors. +** See also: [SQLITE_IOCAP_IMMUTABLE]. +** +**
+** +** ^Specifying an unknown parameter in the query component of a URI is not an +** error. Future versions of SQLite might understand additional query +** parameters. See "[query parameters with special meaning to SQLite]" for +** additional information. +** +** [[URI filename examples]]

URI filename examples

+** +**
+**
URI filenames Results +**
file:data.db +** Open the file "data.db" in the current directory. +**
file:/home/fred/data.db
+** file:///home/fred/data.db
+** file://localhost/home/fred/data.db
+** Open the database file "/home/fred/data.db". +**
file://darkstar/home/fred/data.db +** An error. "darkstar" is not a recognized authority. +**
+** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db +** Windows only: Open the file "data.db" on fred's desktop on drive +** C:. Note that the %20 escaping in this example is not strictly +** necessary - space characters can be used literally +** in URI filenames. +**
file:data.db?mode=ro&cache=private +** Open file "data.db" in the current directory for read-only access. +** Regardless of whether or not shared-cache mode is enabled by +** default, use a private cache. +**
file:/home/fred/data.db?vfs=unix-dotfile +** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile" +** that uses dot-files in place of posix advisory locking. +**
file:data.db?mode=readonly +** An error. "readonly" is not a valid option for the "mode" parameter. +**
+** +** ^URI hexadecimal escape sequences (%HH) are supported within the path and +** query components of a URI. A hexadecimal escape sequence consists of a +** percent sign - "%" - followed by exactly two hexadecimal digits +** specifying an octet value. ^Before the path or query components of a +** URI filename are interpreted, they are encoded using UTF-8 and all +** hexadecimal escape sequences replaced by a single byte containing the +** corresponding octet. If this process generates an invalid UTF-8 encoding, +** the results are undefined. +** +** Note to Windows users: The encoding used for the filename argument +** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever +** codepage is currently defined. Filenames containing international +** characters must be converted to UTF-8 prior to passing them into +** sqlite3_open() or sqlite3_open_v2(). +** +** Note to Windows Runtime users: The temporary directory must be set +** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various +** features that require the use of temporary files may fail. +** +** See also: [sqlite3_temp_directory] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_open( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb /* OUT: SQLite db handle */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_open16( + const void *filename, /* Database filename (UTF-16) */ + sqlite3 **ppDb /* OUT: SQLite db handle */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_open_v2( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb, /* OUT: SQLite db handle */ + int flags, /* Flags */ + const char *zVfs /* Name of VFS module to use */ +); + +/* +** CAPI3REF: Obtain Values For URI Parameters +** +** These are utility routines, useful to VFS implementations, that check +** to see if a database file was a URI that contained a specific query +** parameter, and if so obtains the value of that query parameter. +** +** If F is the database filename pointer passed into the xOpen() method of +** a VFS implementation when the flags parameter to xOpen() has one or +** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and +** P is the name of the query parameter, then +** sqlite3_uri_parameter(F,P) returns the value of the P +** parameter if it exists or a NULL pointer if P does not appear as a +** query parameter on F. If P is a query parameter of F +** has no explicit value, then sqlite3_uri_parameter(F,P) returns +** a pointer to an empty string. +** +** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean +** parameter and returns true (1) or false (0) according to the value +** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the +** value of query parameter P is one of "yes", "true", or "on" in any +** case or if the value begins with a non-zero number. The +** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of +** query parameter P is one of "no", "false", or "off" in any case or +** if the value begins with a numeric zero. If P is not a query +** parameter on F or if the value of P is does not match any of the +** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0). +** +** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a +** 64-bit signed integer and returns that integer, or D if P does not +** exist. If the value of P is something other than an integer, then +** zero is returned. +** +** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and +** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and +** is not a database file pathname pointer that SQLite passed into the xOpen +** VFS method, then the behavior of this routine is undefined and probably +** undesirable. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam); +SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault); +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64); + + +/* +** CAPI3REF: Error Codes And Messages +** METHOD: sqlite3 +** +** ^If the most recent sqlite3_* API call associated with +** [database connection] D failed, then the sqlite3_errcode(D) interface +** returns the numeric [result code] or [extended result code] for that +** API call. +** If the most recent API call was successful, +** then the return value from sqlite3_errcode() is undefined. +** ^The sqlite3_extended_errcode() +** interface is the same except that it always returns the +** [extended result code] even when extended result codes are +** disabled. +** +** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language +** text that describes the error, as either UTF-8 or UTF-16 respectively. +** ^(Memory to hold the error message string is managed internally. +** The application does not need to worry about freeing the result. +** However, the error string might be overwritten or deallocated by +** subsequent calls to other SQLite interface functions.)^ +** +** ^The sqlite3_errstr() interface returns the English-language text +** that describes the [result code], as UTF-8. +** ^(Memory to hold the error message string is managed internally +** and must not be freed by the application)^. +** +** When the serialized [threading mode] is in use, it might be the +** case that a second error occurs on a separate thread in between +** the time of the first error and the call to these interfaces. +** When that happens, the second error will be reported since these +** interfaces always report the most recent result. To avoid +** this, each thread can obtain exclusive use of the [database connection] D +** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning +** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after +** all calls to the interfaces listed here are completed. +** +** If an interface fails with SQLITE_MISUSE, that means the interface +** was invoked incorrectly by the application. In that case, the +** error code and message may or may not be set. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db); +SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db); +SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*); +SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*); +SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int); + +/* +** CAPI3REF: Prepared Statement Object +** KEYWORDS: {prepared statement} {prepared statements} +** +** An instance of this object represents a single SQL statement that +** has been compiled into binary form and is ready to be evaluated. +** +** Think of each SQL statement as a separate computer program. The +** original SQL text is source code. A prepared statement object +** is the compiled object code. All SQL must be converted into a +** prepared statement before it can be run. +** +** The life-cycle of a prepared statement object usually goes like this: +** +**
    +**
  1. Create the prepared statement object using [sqlite3_prepare_v2()]. +**
  2. Bind values to [parameters] using the sqlite3_bind_*() +** interfaces. +**
  3. Run the SQL by calling [sqlite3_step()] one or more times. +**
  4. Reset the prepared statement using [sqlite3_reset()] then go back +** to step 2. Do this zero or more times. +**
  5. Destroy the object using [sqlite3_finalize()]. +**
+*/ +typedef struct sqlite3_stmt sqlite3_stmt; + +/* +** CAPI3REF: Run-time Limits +** METHOD: sqlite3 +** +** ^(This interface allows the size of various constructs to be limited +** on a connection by connection basis. The first parameter is the +** [database connection] whose limit is to be set or queried. The +** second parameter is one of the [limit categories] that define a +** class of constructs to be size limited. The third parameter is the +** new limit for that construct.)^ +** +** ^If the new limit is a negative number, the limit is unchanged. +** ^(For each limit category SQLITE_LIMIT_NAME there is a +** [limits | hard upper bound] +** set at compile-time by a C preprocessor macro called +** [limits | SQLITE_MAX_NAME]. +** (The "_LIMIT_" in the name is changed to "_MAX_".))^ +** ^Attempts to increase a limit above its hard upper bound are +** silently truncated to the hard upper bound. +** +** ^Regardless of whether or not the limit was changed, the +** [sqlite3_limit()] interface returns the prior value of the limit. +** ^Hence, to find the current value of a limit without changing it, +** simply invoke this interface with the third parameter set to -1. +** +** Run-time limits are intended for use in applications that manage +** both their own internal database and also databases that are controlled +** by untrusted external sources. An example application might be a +** web browser that has its own databases for storing history and +** separate databases controlled by JavaScript applications downloaded +** off the Internet. The internal databases can be given the +** large, default limits. Databases managed by external sources can +** be given much smaller limits designed to prevent a denial of service +** attack. Developers might also want to use the [sqlite3_set_authorizer()] +** interface to further control untrusted SQL. The size of the database +** created by an untrusted script can be contained using the +** [max_page_count] [PRAGMA]. +** +** New run-time limit categories may be added in future releases. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal); + +/* +** CAPI3REF: Run-Time Limit Categories +** KEYWORDS: {limit category} {*limit categories} +** +** These constants define various performance limits +** that can be lowered at run-time using [sqlite3_limit()]. +** The synopsis of the meanings of the various limits is shown below. +** Additional information is available at [limits | Limits in SQLite]. +** +**
+** [[SQLITE_LIMIT_LENGTH]] ^(
SQLITE_LIMIT_LENGTH
+**
The maximum size of any string or BLOB or table row, in bytes.
)^ +** +** [[SQLITE_LIMIT_SQL_LENGTH]] ^(
SQLITE_LIMIT_SQL_LENGTH
+**
The maximum length of an SQL statement, in bytes.
)^ +** +** [[SQLITE_LIMIT_COLUMN]] ^(
SQLITE_LIMIT_COLUMN
+**
The maximum number of columns in a table definition or in the +** result set of a [SELECT] or the maximum number of columns in an index +** or in an ORDER BY or GROUP BY clause.
)^ +** +** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(
SQLITE_LIMIT_EXPR_DEPTH
+**
The maximum depth of the parse tree on any expression.
)^ +** +** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(
SQLITE_LIMIT_COMPOUND_SELECT
+**
The maximum number of terms in a compound SELECT statement.
)^ +** +** [[SQLITE_LIMIT_VDBE_OP]] ^(
SQLITE_LIMIT_VDBE_OP
+**
The maximum number of instructions in a virtual machine program +** used to implement an SQL statement. This limit is not currently +** enforced, though that might be added in some future release of +** SQLite.
)^ +** +** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(
SQLITE_LIMIT_FUNCTION_ARG
+**
The maximum number of arguments on a function.
)^ +** +** [[SQLITE_LIMIT_ATTACHED]] ^(
SQLITE_LIMIT_ATTACHED
+**
The maximum number of [ATTACH | attached databases].)^
+** +** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]] +** ^(
SQLITE_LIMIT_LIKE_PATTERN_LENGTH
+**
The maximum length of the pattern argument to the [LIKE] or +** [GLOB] operators.
)^ +** +** [[SQLITE_LIMIT_VARIABLE_NUMBER]] +** ^(
SQLITE_LIMIT_VARIABLE_NUMBER
+**
The maximum index number of any [parameter] in an SQL statement.)^ +** +** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(
SQLITE_LIMIT_TRIGGER_DEPTH
+**
The maximum depth of recursion for triggers.
)^ +** +** [[SQLITE_LIMIT_WORKER_THREADS]] ^(
SQLITE_LIMIT_WORKER_THREADS
+**
The maximum number of auxiliary worker threads that a single +** [prepared statement] may start.
)^ +**
+*/ +#define SQLITE_LIMIT_LENGTH 0 +#define SQLITE_LIMIT_SQL_LENGTH 1 +#define SQLITE_LIMIT_COLUMN 2 +#define SQLITE_LIMIT_EXPR_DEPTH 3 +#define SQLITE_LIMIT_COMPOUND_SELECT 4 +#define SQLITE_LIMIT_VDBE_OP 5 +#define SQLITE_LIMIT_FUNCTION_ARG 6 +#define SQLITE_LIMIT_ATTACHED 7 +#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 +#define SQLITE_LIMIT_VARIABLE_NUMBER 9 +#define SQLITE_LIMIT_TRIGGER_DEPTH 10 +#define SQLITE_LIMIT_WORKER_THREADS 11 + +/* +** CAPI3REF: Compiling An SQL Statement +** KEYWORDS: {SQL statement compiler} +** METHOD: sqlite3 +** CONSTRUCTOR: sqlite3_stmt +** +** To execute an SQL query, it must first be compiled into a byte-code +** program using one of these routines. +** +** The first argument, "db", is a [database connection] obtained from a +** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or +** [sqlite3_open16()]. The database connection must not have been closed. +** +** The second argument, "zSql", is the statement to be compiled, encoded +** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() +** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2() +** use UTF-16. +** +** ^If the nByte argument is negative, then zSql is read up to the +** first zero terminator. ^If nByte is positive, then it is the +** number of bytes read from zSql. ^If nByte is zero, then no prepared +** statement is generated. +** If the caller knows that the supplied string is nul-terminated, then +** there is a small performance advantage to passing an nByte parameter that +** is the number of bytes in the input string including +** the nul-terminator. +** +** ^If pzTail is not NULL then *pzTail is made to point to the first byte +** past the end of the first SQL statement in zSql. These routines only +** compile the first statement in zSql, so *pzTail is left pointing to +** what remains uncompiled. +** +** ^*ppStmt is left pointing to a compiled [prepared statement] that can be +** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set +** to NULL. ^If the input text contains no SQL (if the input is an empty +** string or a comment) then *ppStmt is set to NULL. +** The calling procedure is responsible for deleting the compiled +** SQL statement using [sqlite3_finalize()] after it has finished with it. +** ppStmt may not be NULL. +** +** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK]; +** otherwise an [error code] is returned. +** +** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are +** recommended for all new programs. The two older interfaces are retained +** for backwards compatibility, but their use is discouraged. +** ^In the "v2" interfaces, the prepared statement +** that is returned (the [sqlite3_stmt] object) contains a copy of the +** original SQL text. This causes the [sqlite3_step()] interface to +** behave differently in three ways: +** +**
    +**
  1. +** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it +** always used to do, [sqlite3_step()] will automatically recompile the SQL +** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY] +** retries will occur before sqlite3_step() gives up and returns an error. +**
  2. +** +**
  3. +** ^When an error occurs, [sqlite3_step()] will return one of the detailed +** [error codes] or [extended error codes]. ^The legacy behavior was that +** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code +** and the application would have to make a second call to [sqlite3_reset()] +** in order to find the underlying cause of the problem. With the "v2" prepare +** interfaces, the underlying reason for the error is returned immediately. +**
  4. +** +**
  5. +** ^If the specific value bound to [parameter | host parameter] in the +** WHERE clause might influence the choice of query plan for a statement, +** then the statement will be automatically recompiled, as if there had been +** a schema change, on the first [sqlite3_step()] call following any change +** to the [sqlite3_bind_text | bindings] of that [parameter]. +** ^The specific value of WHERE-clause [parameter] might influence the +** choice of query plan if the parameter is the left-hand side of a [LIKE] +** or [GLOB] operator or if the parameter is compared to an indexed column +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. +**
  6. +**
+*/ +SQLITE_API int SQLITE_STDCALL sqlite3_prepare( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_prepare16( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ +); + +/* +** CAPI3REF: Retrieving Statement SQL +** METHOD: sqlite3_stmt +** +** ^This interface can be used to retrieve a saved copy of the original +** SQL text used to create a [prepared statement] if that statement was +** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()]. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Determine If An SQL Statement Writes The Database +** METHOD: sqlite3_stmt +** +** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if +** and only if the [prepared statement] X makes no direct changes to +** the content of the database file. +** +** Note that [application-defined SQL functions] or +** [virtual tables] might change the database indirectly as a side effect. +** ^(For example, if an application defines a function "eval()" that +** calls [sqlite3_exec()], then the following SQL statement would +** change the database file through side-effects: +** +**
+**    SELECT eval('DELETE FROM t1') FROM t2;
+** 
+** +** But because the [SELECT] statement does not change the database file +** directly, sqlite3_stmt_readonly() would still return true.)^ +** +** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK], +** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true, +** since the statements themselves do not actually modify the database but +** rather they control the timing of when other statements modify the +** database. ^The [ATTACH] and [DETACH] statements also cause +** sqlite3_stmt_readonly() to return true since, while those statements +** change the configuration of a database connection, they do not make +** changes to the content of the database files on disk. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Determine If A Prepared Statement Has Been Reset +** METHOD: sqlite3_stmt +** +** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the +** [prepared statement] S has been stepped at least once using +** [sqlite3_step(S)] but has neither run to completion (returned +** [SQLITE_DONE] from [sqlite3_step(S)]) nor +** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) +** interface returns false if S is a NULL pointer. If S is not a +** NULL pointer and is not a pointer to a valid [prepared statement] +** object, then the behavior is undefined and probably undesirable. +** +** This interface can be used in combination [sqlite3_next_stmt()] +** to locate all prepared statements associated with a database +** connection that are in need of being reset. This can be used, +** for example, in diagnostic routines to search for prepared +** statements that are holding a transaction open. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*); + +/* +** CAPI3REF: Dynamically Typed Value Object +** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value} +** +** SQLite uses the sqlite3_value object to represent all values +** that can be stored in a database table. SQLite uses dynamic typing +** for the values it stores. ^Values stored in sqlite3_value objects +** can be integers, floating point values, strings, BLOBs, or NULL. +** +** An sqlite3_value object may be either "protected" or "unprotected". +** Some interfaces require a protected sqlite3_value. Other interfaces +** will accept either a protected or an unprotected sqlite3_value. +** Every interface that accepts sqlite3_value arguments specifies +** whether or not it requires a protected sqlite3_value. The +** [sqlite3_value_dup()] interface can be used to construct a new +** protected sqlite3_value from an unprotected sqlite3_value. +** +** The terms "protected" and "unprotected" refer to whether or not +** a mutex is held. An internal mutex is held for a protected +** sqlite3_value object but no mutex is held for an unprotected +** sqlite3_value object. If SQLite is compiled to be single-threaded +** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) +** or if SQLite is run in one of reduced mutex modes +** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] +** then there is no distinction between protected and unprotected +** sqlite3_value objects and they can be used interchangeably. However, +** for maximum code portability it is recommended that applications +** still make the distinction between protected and unprotected +** sqlite3_value objects even when not strictly required. +** +** ^The sqlite3_value objects that are passed as parameters into the +** implementation of [application-defined SQL functions] are protected. +** ^The sqlite3_value object returned by +** [sqlite3_column_value()] is unprotected. +** Unprotected sqlite3_value objects may only be used with +** [sqlite3_result_value()] and [sqlite3_bind_value()]. +** The [sqlite3_value_blob | sqlite3_value_type()] family of +** interfaces require protected sqlite3_value objects. +*/ +typedef struct Mem sqlite3_value; + +/* +** CAPI3REF: SQL Function Context Object +** +** The context in which an SQL function executes is stored in an +** sqlite3_context object. ^A pointer to an sqlite3_context object +** is always first parameter to [application-defined SQL functions]. +** The application-defined SQL function implementation will pass this +** pointer through into calls to [sqlite3_result_int | sqlite3_result()], +** [sqlite3_aggregate_context()], [sqlite3_user_data()], +** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()], +** and/or [sqlite3_set_auxdata()]. +*/ +typedef struct sqlite3_context sqlite3_context; + +/* +** CAPI3REF: Binding Values To Prepared Statements +** KEYWORDS: {host parameter} {host parameters} {host parameter name} +** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding} +** METHOD: sqlite3_stmt +** +** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, +** literals may be replaced by a [parameter] that matches one of following +** templates: +** +**
    +**
  • ? +**
  • ?NNN +**
  • :VVV +**
  • @VVV +**
  • $VVV +**
+** +** In the templates above, NNN represents an integer literal, +** and VVV represents an alphanumeric identifier.)^ ^The values of these +** parameters (also called "host parameter names" or "SQL parameters") +** can be set using the sqlite3_bind_*() routines defined here. +** +** ^The first argument to the sqlite3_bind_*() routines is always +** a pointer to the [sqlite3_stmt] object returned from +** [sqlite3_prepare_v2()] or its variants. +** +** ^The second argument is the index of the SQL parameter to be set. +** ^The leftmost SQL parameter has an index of 1. ^When the same named +** SQL parameter is used more than once, second and subsequent +** occurrences have the same index as the first occurrence. +** ^The index for named parameters can be looked up using the +** [sqlite3_bind_parameter_index()] API if desired. ^The index +** for "?NNN" parameters is the value of NNN. +** ^The NNN value must be between 1 and the [sqlite3_limit()] +** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999). +** +** ^The third argument is the value to bind to the parameter. +** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() +** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter +** is ignored and the end result is the same as sqlite3_bind_null(). +** +** ^(In those routines that have a fourth argument, its value is the +** number of bytes in the parameter. To be clear: the value is the +** number of bytes in the value, not the number of characters.)^ +** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16() +** is negative, then the length of the string is +** the number of bytes up to the first zero terminator. +** If the fourth parameter to sqlite3_bind_blob() is negative, then +** the behavior is undefined. +** If a non-negative fourth parameter is provided to sqlite3_bind_text() +** or sqlite3_bind_text16() or sqlite3_bind_text64() then +** that parameter must be the byte offset +** where the NUL terminator would occur assuming the string were NUL +** terminated. If any NUL characters occur at byte offsets less than +** the value of the fourth parameter then the resulting string value will +** contain embedded NULs. The result of expressions involving strings +** with embedded NULs is undefined. +** +** ^The fifth argument to the BLOB and string binding interfaces +** is a destructor used to dispose of the BLOB or +** string after SQLite has finished with it. ^The destructor is called +** to dispose of the BLOB or string even if the call to bind API fails. +** ^If the fifth argument is +** the special value [SQLITE_STATIC], then SQLite assumes that the +** information is in static, unmanaged space and does not need to be freed. +** ^If the fifth argument has the value [SQLITE_TRANSIENT], then +** SQLite makes its own private copy of the data immediately, before +** the sqlite3_bind_*() routine returns. +** +** ^The sixth argument to sqlite3_bind_text64() must be one of +** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] +** to specify the encoding of the text in the third parameter. If +** the sixth argument to sqlite3_bind_text64() is not one of the +** allowed values shown above, or if the text encoding is different +** from the encoding specified by the sixth parameter, then the behavior +** is undefined. +** +** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that +** is filled with zeroes. ^A zeroblob uses a fixed amount of memory +** (just an integer to hold its size) while it is being processed. +** Zeroblobs are intended to serve as placeholders for BLOBs whose +** content is later written using +** [sqlite3_blob_open | incremental BLOB I/O] routines. +** ^A negative value for the zeroblob results in a zero-length BLOB. +** +** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer +** for the [prepared statement] or with a prepared statement for which +** [sqlite3_step()] has been called more recently than [sqlite3_reset()], +** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_() +** routine is passed a [prepared statement] that has been finalized, the +** result is undefined and probably harmful. +** +** ^Bindings are not cleared by the [sqlite3_reset()] routine. +** ^Unbound parameters are interpreted as NULL. +** +** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an +** [error code] if anything goes wrong. +** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB +** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or +** [SQLITE_MAX_LENGTH]. +** ^[SQLITE_RANGE] is returned if the parameter +** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails. +** +** See also: [sqlite3_bind_parameter_count()], +** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64, + void(*)(void*)); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt*, int, double); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt*, int, int); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt*, int); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, + void(*)(void*), unsigned char encoding); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); +SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64); + +/* +** CAPI3REF: Number Of SQL Parameters +** METHOD: sqlite3_stmt +** +** ^This routine can be used to find the number of [SQL parameters] +** in a [prepared statement]. SQL parameters are tokens of the +** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as +** placeholders for values that are [sqlite3_bind_blob | bound] +** to the parameters at a later time. +** +** ^(This routine actually returns the index of the largest (rightmost) +** parameter. For all forms except ?NNN, this will correspond to the +** number of unique parameters. If parameters of the ?NNN form are used, +** there may be gaps in the list.)^ +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*); + +/* +** CAPI3REF: Name Of A Host Parameter +** METHOD: sqlite3_stmt +** +** ^The sqlite3_bind_parameter_name(P,N) interface returns +** the name of the N-th [SQL parameter] in the [prepared statement] P. +** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA" +** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA" +** respectively. +** In other words, the initial ":" or "$" or "@" or "?" +** is included as part of the name.)^ +** ^Parameters of the form "?" without a following integer have no name +** and are referred to as "nameless" or "anonymous parameters". +** +** ^The first host parameter has an index of 1, not 0. +** +** ^If the value N is out of range or if the N-th parameter is +** nameless, then NULL is returned. ^The returned string is +** always in UTF-8 encoding even if the named parameter was +** originally specified as UTF-16 in [sqlite3_prepare16()] or +** [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int); + +/* +** CAPI3REF: Index Of A Parameter With A Given Name +** METHOD: sqlite3_stmt +** +** ^Return the index of an SQL parameter given its name. ^The +** index value returned is suitable for use as the second +** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero +** is returned if no matching parameter is found. ^The parameter +** name must be given in UTF-8 even if the original statement +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_name()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); + +/* +** CAPI3REF: Reset All Bindings On A Prepared Statement +** METHOD: sqlite3_stmt +** +** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset +** the [sqlite3_bind_blob | bindings] on a [prepared statement]. +** ^Use this routine to reset all host parameters to NULL. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*); + +/* +** CAPI3REF: Number Of Columns In A Result Set +** METHOD: sqlite3_stmt +** +** ^Return the number of columns in the result set returned by the +** [prepared statement]. ^This routine returns 0 if pStmt is an SQL +** statement that does not return data (for example an [UPDATE]). +** +** See also: [sqlite3_data_count()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Column Names In A Result Set +** METHOD: sqlite3_stmt +** +** ^These routines return the name assigned to a particular column +** in the result set of a [SELECT] statement. ^The sqlite3_column_name() +** interface returns a pointer to a zero-terminated UTF-8 string +** and sqlite3_column_name16() returns a pointer to a zero-terminated +** UTF-16 string. ^The first parameter is the [prepared statement] +** that implements the [SELECT] statement. ^The second parameter is the +** column number. ^The leftmost column is number 0. +** +** ^The returned string pointer is valid until either the [prepared statement] +** is destroyed by [sqlite3_finalize()] or until the statement is automatically +** reprepared by the first call to [sqlite3_step()] for a particular run +** or until the next call to +** sqlite3_column_name() or sqlite3_column_name16() on the same column. +** +** ^If sqlite3_malloc() fails during the processing of either routine +** (for example during a conversion from UTF-8 to UTF-16) then a +** NULL pointer is returned. +** +** ^The name of a result column is the value of the "AS" clause for +** that column, if there is an AS clause. If there is no AS clause +** then the name of the column is unspecified and may change from +** one release of SQLite to the next. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N); + +/* +** CAPI3REF: Source Of Data In A Query Result +** METHOD: sqlite3_stmt +** +** ^These routines provide a means to determine the database, table, and +** table column that is the origin of a particular result column in +** [SELECT] statement. +** ^The name of the database or table or column can be returned as +** either a UTF-8 or UTF-16 string. ^The _database_ routines return +** the database name, the _table_ routines return the table name, and +** the origin_ routines return the column name. +** ^The returned string is valid until the [prepared statement] is destroyed +** using [sqlite3_finalize()] or until the statement is automatically +** reprepared by the first call to [sqlite3_step()] for a particular run +** or until the same information is requested +** again in a different encoding. +** +** ^The names returned are the original un-aliased names of the +** database, table, and column. +** +** ^The first argument to these interfaces is a [prepared statement]. +** ^These functions return information about the Nth result column returned by +** the statement, where N is the second function argument. +** ^The left-most column is column 0 for these routines. +** +** ^If the Nth column returned by the statement is an expression or +** subquery and is not a column value, then all of these functions return +** NULL. ^These routine might also return NULL if a memory allocation error +** occurs. ^Otherwise, they return the name of the attached database, table, +** or column that query result column was extracted from. +** +** ^As with all other SQLite APIs, those whose names end with "16" return +** UTF-16 encoded strings and the other functions return UTF-8. +** +** ^These APIs are only available if the library was compiled with the +** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol. +** +** If two or more threads call one or more of these routines against the same +** prepared statement and column at the same time then the results are +** undefined. +** +** If two or more threads call one or more +** [sqlite3_column_database_name | column metadata interfaces] +** for the same [prepared statement] and result column +** at the same time then the results are undefined. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt*,int); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt*,int); +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int); +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int); + +/* +** CAPI3REF: Declared Datatype Of A Query Result +** METHOD: sqlite3_stmt +** +** ^(The first parameter is a [prepared statement]. +** If this statement is a [SELECT] statement and the Nth column of the +** returned result set of that [SELECT] is a table column (not an +** expression or subquery) then the declared type of the table +** column is returned.)^ ^If the Nth column of the result set is an +** expression or subquery, then a NULL pointer is returned. +** ^The returned string is always UTF-8 encoded. +** +** ^(For example, given the database schema: +** +** CREATE TABLE t1(c1 VARIANT); +** +** and the following statement to be compiled: +** +** SELECT c1 + 1, c1 FROM t1; +** +** this routine would return the string "VARIANT" for the second result +** column (i==1), and a NULL pointer for the first result column (i==0).)^ +** +** ^SQLite uses dynamic run-time typing. ^So just because a column +** is declared to contain a particular type does not mean that the +** data stored in that column is of the declared type. SQLite is +** strongly typed, but the typing is dynamic not static. ^Type +** is associated with individual values, not with the containers +** used to hold those values. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int); + +/* +** CAPI3REF: Evaluate An SQL Statement +** METHOD: sqlite3_stmt +** +** After a [prepared statement] has been prepared using either +** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy +** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function +** must be called one or more times to evaluate the statement. +** +** The details of the behavior of the sqlite3_step() interface depend +** on whether the statement was prepared using the newer "v2" interface +** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy +** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the +** new "v2" interface is recommended for new applications but the legacy +** interface will continue to be supported. +** +** ^In the legacy interface, the return value will be either [SQLITE_BUSY], +** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. +** ^With the "v2" interface, any of the other [result codes] or +** [extended result codes] might be returned as well. +** +** ^[SQLITE_BUSY] means that the database engine was unable to acquire the +** database locks it needs to do its job. ^If the statement is a [COMMIT] +** or occurs outside of an explicit transaction, then you can retry the +** statement. If the statement is not a [COMMIT] and occurs within an +** explicit transaction then you should rollback the transaction before +** continuing. +** +** ^[SQLITE_DONE] means that the statement has finished executing +** successfully. sqlite3_step() should not be called again on this virtual +** machine without first calling [sqlite3_reset()] to reset the virtual +** machine back to its initial state. +** +** ^If the SQL statement being executed returns any data, then [SQLITE_ROW] +** is returned each time a new row of data is ready for processing by the +** caller. The values may be accessed using the [column access functions]. +** sqlite3_step() is called again to retrieve the next row of data. +** +** ^[SQLITE_ERROR] means that a run-time error (such as a constraint +** violation) has occurred. sqlite3_step() should not be called again on +** the VM. More information may be found by calling [sqlite3_errmsg()]. +** ^With the legacy interface, a more specific error code (for example, +** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) +** can be obtained by calling [sqlite3_reset()] on the +** [prepared statement]. ^In the "v2" interface, +** the more specific error code is returned directly by sqlite3_step(). +** +** [SQLITE_MISUSE] means that the this routine was called inappropriately. +** Perhaps it was called on a [prepared statement] that has +** already been [sqlite3_finalize | finalized] or on one that had +** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could +** be the case that the same database connection is being used by two or +** more threads at the same moment in time. +** +** For all versions of SQLite up to and including 3.6.23.1, a call to +** [sqlite3_reset()] was required after sqlite3_step() returned anything +** other than [SQLITE_ROW] before any subsequent invocation of +** sqlite3_step(). Failure to reset the prepared statement using +** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from +** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began +** calling [sqlite3_reset()] automatically in this circumstance rather +** than returning [SQLITE_MISUSE]. This is not considered a compatibility +** break because any application that ever receives an SQLITE_MISUSE error +** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option +** can be used to restore the legacy behavior. +** +** Goofy Interface Alert: In the legacy interface, the sqlite3_step() +** API always returns a generic error code, [SQLITE_ERROR], following any +** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call +** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the +** specific [error codes] that better describes the error. +** We admit that this is a goofy design. The problem has been fixed +** with the "v2" interface. If you prepare all of your SQL statements +** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead +** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, +** then the more specific [error codes] are returned directly +** by sqlite3_step(). The use of the "v2" interface is recommended. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*); + +/* +** CAPI3REF: Number of columns in a result set +** METHOD: sqlite3_stmt +** +** ^The sqlite3_data_count(P) interface returns the number of columns in the +** current row of the result set of [prepared statement] P. +** ^If prepared statement P does not have results ready to return +** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of +** interfaces) then sqlite3_data_count(P) returns 0. +** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. +** ^The sqlite3_data_count(P) routine returns 0 if the previous call to +** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P) +** will return non-zero if previous call to [sqlite3_step](P) returned +** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum] +** where it always returns zero since each step of that multi-step +** pragma returns 0 columns of data. +** +** See also: [sqlite3_column_count()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Fundamental Datatypes +** KEYWORDS: SQLITE_TEXT +** +** ^(Every value in SQLite has one of five fundamental datatypes: +** +**
    +**
  • 64-bit signed integer +**
  • 64-bit IEEE floating point number +**
  • string +**
  • BLOB +**
  • NULL +**
)^ +** +** These constants are codes for each of those types. +** +** Note that the SQLITE_TEXT constant was also used in SQLite version 2 +** for a completely different meaning. Software that links against both +** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not +** SQLITE_TEXT. +*/ +#define SQLITE_INTEGER 1 +#define SQLITE_FLOAT 2 +#define SQLITE_BLOB 4 +#define SQLITE_NULL 5 +#ifdef SQLITE_TEXT +# undef SQLITE_TEXT +#else +# define SQLITE_TEXT 3 +#endif +#define SQLITE3_TEXT 3 + +/* +** CAPI3REF: Result Values From A Query +** KEYWORDS: {column access functions} +** METHOD: sqlite3_stmt +** +** ^These routines return information about a single column of the current +** result row of a query. ^In every case the first argument is a pointer +** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] +** that was returned from [sqlite3_prepare_v2()] or one of its variants) +** and the second argument is the index of the column for which information +** should be returned. ^The leftmost column of the result set has the index 0. +** ^The number of columns in the result can be determined using +** [sqlite3_column_count()]. +** +** If the SQL statement does not currently point to a valid row, or if the +** column index is out of range, the result is undefined. +** These routines may only be called when the most recent call to +** [sqlite3_step()] has returned [SQLITE_ROW] and neither +** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently. +** If any of these routines are called after [sqlite3_reset()] or +** [sqlite3_finalize()] or after [sqlite3_step()] has returned +** something other than [SQLITE_ROW], the results are undefined. +** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] +** are called from a different thread while any of these routines +** are pending, then the results are undefined. +** +** ^The sqlite3_column_type() routine returns the +** [SQLITE_INTEGER | datatype code] for the initial data type +** of the result column. ^The returned value is one of [SQLITE_INTEGER], +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value +** returned by sqlite3_column_type() is only meaningful if no type +** conversions have occurred as described below. After a type conversion, +** the value returned by sqlite3_column_type() is undefined. Future +** versions of SQLite may change the behavior of sqlite3_column_type() +** following a type conversion. +** +** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() +** routine returns the number of bytes in that BLOB or string. +** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts +** the string to UTF-8 and then returns the number of bytes. +** ^If the result is a numeric value then sqlite3_column_bytes() uses +** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns +** the number of bytes in that string. +** ^If the result is NULL, then sqlite3_column_bytes() returns zero. +** +** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() +** routine returns the number of bytes in that BLOB or string. +** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts +** the string to UTF-16 and then returns the number of bytes. +** ^If the result is a numeric value then sqlite3_column_bytes16() uses +** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns +** the number of bytes in that string. +** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. +** +** ^The values returned by [sqlite3_column_bytes()] and +** [sqlite3_column_bytes16()] do not include the zero terminators at the end +** of the string. ^For clarity: the values returned by +** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of +** bytes in the string, not the number of characters. +** +** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), +** even empty strings, are always zero-terminated. ^The return +** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. +** +** Warning: ^The object returned by [sqlite3_column_value()] is an +** [unprotected sqlite3_value] object. In a multithreaded environment, +** an unprotected sqlite3_value object may only be used safely with +** [sqlite3_bind_value()] and [sqlite3_result_value()]. +** If the [unprotected sqlite3_value] object returned by +** [sqlite3_column_value()] is used in any other way, including calls +** to routines like [sqlite3_value_int()], [sqlite3_value_text()], +** or [sqlite3_value_bytes()], the behavior is not threadsafe. +** +** These routines attempt to convert the value where appropriate. ^For +** example, if the internal representation is FLOAT and a text result +** is requested, [sqlite3_snprintf()] is used internally to perform the +** conversion automatically. ^(The following table details the conversions +** that are applied: +** +**
+** +**
Internal
Type
Requested
Type
Conversion +** +**
NULL INTEGER Result is 0 +**
NULL FLOAT Result is 0.0 +**
NULL TEXT Result is a NULL pointer +**
NULL BLOB Result is a NULL pointer +**
INTEGER FLOAT Convert from integer to float +**
INTEGER TEXT ASCII rendering of the integer +**
INTEGER BLOB Same as INTEGER->TEXT +**
FLOAT INTEGER [CAST] to INTEGER +**
FLOAT TEXT ASCII rendering of the float +**
FLOAT BLOB [CAST] to BLOB +**
TEXT INTEGER [CAST] to INTEGER +**
TEXT FLOAT [CAST] to REAL +**
TEXT BLOB No change +**
BLOB INTEGER [CAST] to INTEGER +**
BLOB FLOAT [CAST] to REAL +**
BLOB TEXT Add a zero terminator if needed +**
+**
)^ +** +** Note that when type conversions occur, pointers returned by prior +** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or +** sqlite3_column_text16() may be invalidated. +** Type conversions and pointer invalidations might occur +** in the following cases: +** +**
    +**
  • The initial content is a BLOB and sqlite3_column_text() or +** sqlite3_column_text16() is called. A zero-terminator might +** need to be added to the string.
  • +**
  • The initial content is UTF-8 text and sqlite3_column_bytes16() or +** sqlite3_column_text16() is called. The content must be converted +** to UTF-16.
  • +**
  • The initial content is UTF-16 text and sqlite3_column_bytes() or +** sqlite3_column_text() is called. The content must be converted +** to UTF-8.
  • +**
+** +** ^Conversions between UTF-16be and UTF-16le are always done in place and do +** not invalidate a prior pointer, though of course the content of the buffer +** that the prior pointer references will have been modified. Other kinds +** of conversion are done in place when it is possible, but sometimes they +** are not possible and in those cases prior pointers are invalidated. +** +** The safest policy is to invoke these routines +** in one of the following ways: +** +**
    +**
  • sqlite3_column_text() followed by sqlite3_column_bytes()
  • +**
  • sqlite3_column_blob() followed by sqlite3_column_bytes()
  • +**
  • sqlite3_column_text16() followed by sqlite3_column_bytes16()
  • +**
+** +** In other words, you should call sqlite3_column_text(), +** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result +** into the desired format, then invoke sqlite3_column_bytes() or +** sqlite3_column_bytes16() to find the size of the result. Do not mix calls +** to sqlite3_column_text() or sqlite3_column_blob() with calls to +** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16() +** with calls to sqlite3_column_bytes(). +** +** ^The pointers returned are valid until a type conversion occurs as +** described above, or until [sqlite3_step()] or [sqlite3_reset()] or +** [sqlite3_finalize()] is called. ^The memory space used to hold strings +** and BLOBs is freed automatically. Do not pass the pointers returned +** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into +** [sqlite3_free()]. +** +** ^(If a memory allocation error occurs during the evaluation of any +** of these routines, a default value is returned. The default value +** is either the integer 0, the floating point number 0.0, or a NULL +** pointer. Subsequent calls to [sqlite3_errcode()] will return +** [SQLITE_NOMEM].)^ +*/ +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt*, int iCol); +SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt*, int iCol); +SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt*, int iCol); +SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt*, int iCol); +SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt*, int iCol); +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt*, int iCol); +SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol); +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol); +SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol); +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol); + +/* +** CAPI3REF: Destroy A Prepared Statement Object +** DESTRUCTOR: sqlite3_stmt +** +** ^The sqlite3_finalize() function is called to delete a [prepared statement]. +** ^If the most recent evaluation of the statement encountered no errors +** or if the statement is never been evaluated, then sqlite3_finalize() returns +** SQLITE_OK. ^If the most recent evaluation of statement S failed, then +** sqlite3_finalize(S) returns the appropriate [error code] or +** [extended error code]. +** +** ^The sqlite3_finalize(S) routine can be called at any point during +** the life cycle of [prepared statement] S: +** before statement S is ever evaluated, after +** one or more calls to [sqlite3_reset()], or after any call +** to [sqlite3_step()] regardless of whether or not the statement has +** completed execution. +** +** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. +** +** The application must finalize every [prepared statement] in order to avoid +** resource leaks. It is a grievous error for the application to try to use +** a prepared statement after it has been finalized. Any use of a prepared +** statement after it has been finalized can result in undefined and +** undesirable behavior such as segfaults and heap corruption. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Reset A Prepared Statement Object +** METHOD: sqlite3_stmt +** +** The sqlite3_reset() function is called to reset a [prepared statement] +** object back to its initial state, ready to be re-executed. +** ^Any SQL statement variables that had values bound to them using +** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. +** Use [sqlite3_clear_bindings()] to reset the bindings. +** +** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S +** back to the beginning of its program. +** +** ^If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], +** or if [sqlite3_step(S)] has never before been called on S, +** then [sqlite3_reset(S)] returns [SQLITE_OK]. +** +** ^If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S indicated an error, then +** [sqlite3_reset(S)] returns an appropriate [error code]. +** +** ^The [sqlite3_reset(S)] interface does not change the values +** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Create Or Redefine SQL Functions +** KEYWORDS: {function creation routines} +** KEYWORDS: {application-defined SQL function} +** KEYWORDS: {application-defined SQL functions} +** METHOD: sqlite3 +** +** ^These functions (collectively known as "function creation routines") +** are used to add SQL functions or aggregates or to redefine the behavior +** of existing SQL functions or aggregates. The only differences between +** these routines are the text encoding expected for +** the second parameter (the name of the function being created) +** and the presence or absence of a destructor callback for +** the application data pointer. +** +** ^The first parameter is the [database connection] to which the SQL +** function is to be added. ^If an application uses more than one database +** connection then application-defined SQL functions must be added +** to each database connection separately. +** +** ^The second parameter is the name of the SQL function to be created or +** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 +** representation, exclusive of the zero-terminator. ^Note that the name +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. +** ^Any attempt to create a function with a longer name +** will result in [SQLITE_MISUSE] being returned. +** +** ^The third parameter (nArg) +** is the number of arguments that the SQL function or +** aggregate takes. ^If this parameter is -1, then the SQL function or +** aggregate may take any number of arguments between 0 and the limit +** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third +** parameter is less than -1 or greater than 127 then the behavior is +** undefined. +** +** ^The fourth parameter, eTextRep, specifies what +** [SQLITE_UTF8 | text encoding] this SQL function prefers for +** its parameters. The application should set this parameter to +** [SQLITE_UTF16LE] if the function implementation invokes +** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the +** implementation invokes [sqlite3_value_text16be()] on an input, or +** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8] +** otherwise. ^The same SQL function may be registered multiple times using +** different preferred text encodings, with different implementations for +** each encoding. +** ^When multiple implementations of the same function are available, SQLite +** will pick the one that involves the least amount of data conversion. +** +** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC] +** to signal that the function will always return the same result given +** the same inputs within a single SQL statement. Most SQL functions are +** deterministic. The built-in [random()] SQL function is an example of a +** function that is not deterministic. The SQLite query planner is able to +** perform additional optimizations on deterministic functions, so use +** of the [SQLITE_DETERMINISTIC] flag is recommended where possible. +** +** ^(The fifth parameter is an arbitrary pointer. The implementation of the +** function can gain access to this pointer using [sqlite3_user_data()].)^ +** +** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are +** pointers to C-language functions that implement the SQL function or +** aggregate. ^A scalar SQL function requires an implementation of the xFunc +** callback only; NULL pointers must be passed as the xStep and xFinal +** parameters. ^An aggregate SQL function requires an implementation of xStep +** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing +** SQL function or aggregate, pass NULL pointers for all three function +** callbacks. +** +** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL, +** then it is destructor for the application data pointer. +** The destructor is invoked when the function is deleted, either by being +** overloaded or when the database connection closes.)^ +** ^The destructor is also invoked if the call to +** sqlite3_create_function_v2() fails. +** ^When the destructor callback of the tenth parameter is invoked, it +** is passed a single argument which is a copy of the application data +** pointer which was the fifth parameter to sqlite3_create_function_v2(). +** +** ^It is permitted to register multiple implementations of the same +** functions with the same name but with either differing numbers of +** arguments or differing preferred text encodings. ^SQLite will use +** the implementation that most closely matches the way in which the +** SQL function is used. ^A function implementation with a non-negative +** nArg parameter is a better match than a function implementation with +** a negative nArg. ^A function where the preferred text encoding +** matches the database encoding is a better +** match than a function where the encoding is different. +** ^A function where the encoding difference is between UTF16le and UTF16be +** is a closer match than a function where the encoding difference is +** between UTF8 and UTF16. +** +** ^Built-in functions may be overloaded by new application-defined functions. +** +** ^An application-defined function is permitted to call other +** SQLite interfaces. However, such calls must not +** close the database connection nor finalize or reset the prepared +** statement in which the function is running. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_function( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +); +SQLITE_API int SQLITE_STDCALL sqlite3_create_function16( + sqlite3 *db, + const void *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +); +SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*), + void(*xDestroy)(void*) +); + +/* +** CAPI3REF: Text Encodings +** +** These constant define integer codes that represent the various +** text encodings supported by SQLite. +*/ +#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ +#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ +#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ +#define SQLITE_UTF16 4 /* Use native byte order */ +#define SQLITE_ANY 5 /* Deprecated */ +#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ + +/* +** CAPI3REF: Function Flags +** +** These constants may be ORed together with the +** [SQLITE_UTF8 | preferred text encoding] as the fourth argument +** to [sqlite3_create_function()], [sqlite3_create_function16()], or +** [sqlite3_create_function_v2()]. +*/ +#define SQLITE_DETERMINISTIC 0x800 + +/* +** CAPI3REF: Deprecated Functions +** DEPRECATED +** +** These functions are [deprecated]. In order to maintain +** backwards compatibility with older code, these functions continue +** to be supported. However, new applications should avoid +** the use of these functions. To encourage programmers to avoid +** these functions, we will not explain what they do. +*/ +#ifndef SQLITE_OMIT_DEPRECATED +SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context*); +SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt*); +SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); +SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_global_recover(void); +SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void); +SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), + void*,sqlite3_int64); +#endif + +/* +** CAPI3REF: Obtaining SQL Values +** METHOD: sqlite3_value +** +** The C-language implementation of SQL functions and aggregates uses +** this set of interface routines to access the parameter values on +** the function or aggregate. +** +** The xFunc (for scalar functions) or xStep (for aggregates) parameters +** to [sqlite3_create_function()] and [sqlite3_create_function16()] +** define callbacks that implement the SQL functions and aggregates. +** The 3rd parameter to these callbacks is an array of pointers to +** [protected sqlite3_value] objects. There is one [sqlite3_value] object for +** each parameter to the SQL function. These routines are used to +** extract values from the [sqlite3_value] objects. +** +** These routines work only with [protected sqlite3_value] objects. +** Any attempt to use these routines on an [unprotected sqlite3_value] +** object results in undefined behavior. +** +** ^These routines work just like the corresponding [column access functions] +** except that these routines take a single [protected sqlite3_value] object +** pointer instead of a [sqlite3_stmt*] pointer and an integer column number. +** +** ^The sqlite3_value_text16() interface extracts a UTF-16 string +** in the native byte-order of the host machine. ^The +** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces +** extract UTF-16 strings as big-endian and little-endian respectively. +** +** ^(The sqlite3_value_numeric_type() interface attempts to apply +** numeric affinity to the value. This means that an attempt is +** made to convert the value to an integer or floating point. If +** such a conversion is possible without loss of information (in other +** words, if the value is a string that looks like a number) +** then the conversion is performed. Otherwise no conversion occurs. +** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ +** +** Please pay particular attention to the fact that the pointer returned +** from [sqlite3_value_blob()], [sqlite3_value_text()], or +** [sqlite3_value_text16()] can be invalidated by a subsequent call to +** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], +** or [sqlite3_value_text16()]. +** +** These routines must be called from the same thread as +** the SQL function that supplied the [sqlite3_value*] parameters. +*/ +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value*); +SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value*); +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value*); +SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value*); +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value*); +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*); +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*); +SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*); + +/* +** CAPI3REF: Finding The Subtype Of SQL Values +** METHOD: sqlite3_value +** +** The sqlite3_value_subtype(V) function returns the subtype for +** an [application-defined SQL function] argument V. The subtype +** information can be used to pass a limited amount of context from +** one SQL function to another. Use the [sqlite3_result_subtype()] +** routine to set the subtype for the return value of an SQL function. +** +** SQLite makes no use of subtype itself. It merely passes the subtype +** from the result of one [application-defined SQL function] into the +** input of another. +*/ +SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value*); + +/* +** CAPI3REF: Copy And Free SQL Values +** METHOD: sqlite3_value +** +** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value] +** object D and returns a pointer to that copy. ^The [sqlite3_value] returned +** is a [protected sqlite3_value] object even if the input is not. +** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a +** memory allocation fails. +** +** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object +** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer +** then sqlite3_value_free(V) is a harmless no-op. +*/ +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); +SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); + +/* +** CAPI3REF: Obtain Aggregate Function Context +** METHOD: sqlite3_context +** +** Implementations of aggregate SQL functions use this +** routine to allocate memory for storing their state. +** +** ^The first time the sqlite3_aggregate_context(C,N) routine is called +** for a particular aggregate function, SQLite +** allocates N of memory, zeroes out that memory, and returns a pointer +** to the new memory. ^On second and subsequent calls to +** sqlite3_aggregate_context() for the same aggregate function instance, +** the same buffer is returned. Sqlite3_aggregate_context() is normally +** called once for each invocation of the xStep callback and then one +** last time when the xFinal callback is invoked. ^(When no rows match +** an aggregate query, the xStep() callback of the aggregate function +** implementation is never called and xFinal() is called exactly once. +** In those cases, sqlite3_aggregate_context() might be called for the +** first time from within xFinal().)^ +** +** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer +** when first called if N is less than or equal to zero or if a memory +** allocate error occurs. +** +** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is +** determined by the N parameter on first successful call. Changing the +** value of N in subsequent call to sqlite3_aggregate_context() within +** the same aggregate function instance will not resize the memory +** allocation.)^ Within the xFinal callback, it is customary to set +** N=0 in calls to sqlite3_aggregate_context(C,N) so that no +** pointless memory allocations occur. +** +** ^SQLite automatically frees the memory allocated by +** sqlite3_aggregate_context() when the aggregate query concludes. +** +** The first parameter must be a copy of the +** [sqlite3_context | SQL function context] that is the first parameter +** to the xStep or xFinal callback routine that implements the aggregate +** function. +** +** This routine must be called from the same thread in which +** the aggregate SQL function is running. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes); + +/* +** CAPI3REF: User Data For Functions +** METHOD: sqlite3_context +** +** ^The sqlite3_user_data() interface returns a copy of +** the pointer that was the pUserData parameter (the 5th parameter) +** of the [sqlite3_create_function()] +** and [sqlite3_create_function16()] routines that originally +** registered the application defined function. +** +** This routine must be called from the same thread in which +** the application-defined function is running. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*); + +/* +** CAPI3REF: Database Connection For Functions +** METHOD: sqlite3_context +** +** ^The sqlite3_context_db_handle() interface returns a copy of +** the pointer to the [database connection] (the 1st parameter) +** of the [sqlite3_create_function()] +** and [sqlite3_create_function16()] routines that originally +** registered the application defined function. +*/ +SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*); + +/* +** CAPI3REF: Function Auxiliary Data +** METHOD: sqlite3_context +** +** These functions may be used by (non-aggregate) SQL functions to +** associate metadata with argument values. If the same value is passed to +** multiple invocations of the same SQL function during query execution, under +** some circumstances the associated metadata may be preserved. An example +** of where this might be useful is in a regular-expression matching +** function. The compiled version of the regular expression can be stored as +** metadata associated with the pattern string. +** Then as long as the pattern string remains the same, +** the compiled regular expression can be reused on multiple +** invocations of the same function. +** +** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata +** associated by the sqlite3_set_auxdata() function with the Nth argument +** value to the application-defined function. ^If there is no metadata +** associated with the function argument, this sqlite3_get_auxdata() interface +** returns a NULL pointer. +** +** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th +** argument of the application-defined function. ^Subsequent +** calls to sqlite3_get_auxdata(C,N) return P from the most recent +** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or +** NULL if the metadata has been discarded. +** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL, +** SQLite will invoke the destructor function X with parameter P exactly +** once, when the metadata is discarded. +** SQLite is free to discard the metadata at any time, including:
    +**
  • when the corresponding function parameter changes, or +**
  • when [sqlite3_reset()] or [sqlite3_finalize()] is called for the +** SQL statement, or +**
  • when sqlite3_set_auxdata() is invoked again on the same parameter, or +**
  • during the original sqlite3_set_auxdata() call when a memory +** allocation error occurs.
)^ +** +** Note the last bullet in particular. The destructor X in +** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the +** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata() +** should be called near the end of the function implementation and the +** function implementation should not make any use of P after +** sqlite3_set_auxdata() has been called. +** +** ^(In practice, metadata is preserved between function calls for +** function parameters that are compile-time constants, including literal +** values and [parameters] and expressions composed from the same.)^ +** +** These routines must be called from the same thread in which +** the SQL function is running. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context*, int N); +SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); + + +/* +** CAPI3REF: Constants Defining Special Destructor Behavior +** +** These are special values for the destructor that is passed in as the +** final argument to routines like [sqlite3_result_blob()]. ^If the destructor +** argument is SQLITE_STATIC, it means that the content pointer is constant +** and will never change. It does not need to be destroyed. ^The +** SQLITE_TRANSIENT value means that the content will likely change in +** the near future and that SQLite should make its own private copy of +** the content before returning. +** +** The typedef is necessary to work around problems in certain +** C++ compilers. +*/ +typedef void (*sqlite3_destructor_type)(void*); +#define SQLITE_STATIC ((sqlite3_destructor_type)0) +#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) + +/* +** CAPI3REF: Setting The Result Of An SQL Function +** METHOD: sqlite3_context +** +** These routines are used by the xFunc or xFinal callbacks that +** implement SQL functions and aggregates. See +** [sqlite3_create_function()] and [sqlite3_create_function16()] +** for additional information. +** +** These functions work very much like the [parameter binding] family of +** functions used to bind values to host parameters in prepared statements. +** Refer to the [SQL parameter] documentation for additional information. +** +** ^The sqlite3_result_blob() interface sets the result from +** an application-defined function to be the BLOB whose content is pointed +** to by the second parameter and which is N bytes long where N is the +** third parameter. +** +** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N) +** interfaces set the result of the application-defined function to be +** a BLOB containing all zero bytes and N bytes in size. +** +** ^The sqlite3_result_double() interface sets the result from +** an application-defined function to be a floating point value specified +** by its 2nd argument. +** +** ^The sqlite3_result_error() and sqlite3_result_error16() functions +** cause the implemented SQL function to throw an exception. +** ^SQLite uses the string pointed to by the +** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() +** as the text of an error message. ^SQLite interprets the error +** message string from sqlite3_result_error() as UTF-8. ^SQLite +** interprets the string from sqlite3_result_error16() as UTF-16 in native +** byte order. ^If the third parameter to sqlite3_result_error() +** or sqlite3_result_error16() is negative then SQLite takes as the error +** message all text up through the first zero character. +** ^If the third parameter to sqlite3_result_error() or +** sqlite3_result_error16() is non-negative then SQLite takes that many +** bytes (not characters) from the 2nd parameter as the error message. +** ^The sqlite3_result_error() and sqlite3_result_error16() +** routines make a private copy of the error message text before +** they return. Hence, the calling function can deallocate or +** modify the text after they return without harm. +** ^The sqlite3_result_error_code() function changes the error code +** returned by SQLite as a result of an error in a function. ^By default, +** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error() +** or sqlite3_result_error16() resets the error code to SQLITE_ERROR. +** +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an +** error indicating that a string or BLOB is too long to represent. +** +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an +** error indicating that a memory allocation failed. +** +** ^The sqlite3_result_int() interface sets the return value +** of the application-defined function to be the 32-bit signed integer +** value given in the 2nd argument. +** ^The sqlite3_result_int64() interface sets the return value +** of the application-defined function to be the 64-bit signed integer +** value given in the 2nd argument. +** +** ^The sqlite3_result_null() interface sets the return value +** of the application-defined function to be NULL. +** +** ^The sqlite3_result_text(), sqlite3_result_text16(), +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces +** set the return value of the application-defined function to be +** a text string which is represented as UTF-8, UTF-16 native byte order, +** UTF-16 little endian, or UTF-16 big endian, respectively. +** ^The sqlite3_result_text64() interface sets the return value of an +** application-defined function to be a text string in an encoding +** specified by the fifth (and last) parameter, which must be one +** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]. +** ^SQLite takes the text result from the application from +** the 2nd parameter of the sqlite3_result_text* interfaces. +** ^If the 3rd parameter to the sqlite3_result_text* interfaces +** is negative, then SQLite takes result text from the 2nd parameter +** through the first zero character. +** ^If the 3rd parameter to the sqlite3_result_text* interfaces +** is non-negative, then as many bytes (not characters) of the text +** pointed to by the 2nd parameter are taken as the application-defined +** function result. If the 3rd parameter is non-negative, then it +** must be the byte offset into the string where the NUL terminator would +** appear if the string where NUL terminated. If any NUL characters occur +** in the string at a byte offset that is less than the value of the 3rd +** parameter, then the resulting string will contain embedded NULs and the +** result of expressions operating on strings with embedded NULs is undefined. +** ^If the 4th parameter to the sqlite3_result_text* interfaces +** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that +** function as the destructor on the text or BLOB result when it has +** finished using that result. +** ^If the 4th parameter to the sqlite3_result_text* interfaces or to +** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite +** assumes that the text or BLOB result is in constant space and does not +** copy the content of the parameter nor call a destructor on the content +** when it has finished using that result. +** ^If the 4th parameter to the sqlite3_result_text* interfaces +** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT +** then SQLite makes a copy of the result into space obtained from +** from [sqlite3_malloc()] before it returns. +** +** ^The sqlite3_result_value() interface sets the result of +** the application-defined function to be a copy of the +** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The +** sqlite3_result_value() interface makes a copy of the [sqlite3_value] +** so that the [sqlite3_value] specified in the parameter may change or +** be deallocated after sqlite3_result_value() returns without harm. +** ^A [protected sqlite3_value] object may always be used where an +** [unprotected sqlite3_value] object is required, so either +** kind of [sqlite3_value] object can be used with this interface. +** +** If these routines are called from within the different thread +** than the one containing the application-defined function that received +** the [sqlite3_context] pointer, the results are undefined. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(sqlite3_context*,const void*, + sqlite3_uint64,void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context*, double); +SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context*, const char*, int); +SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context*, const void*, int); +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context*); +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context*); +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context*, int); +SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context*, int); +SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context*, sqlite3_int64); +SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context*); +SQLITE_API void SQLITE_STDCALL sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64, + void(*)(void*), unsigned char encoding); +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); +SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*); +SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n); +SQLITE_API int SQLITE_STDCALL sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n); + + +/* +** CAPI3REF: Setting The Subtype Of An SQL Function +** METHOD: sqlite3_context +** +** The sqlite3_result_subtype(C,T) function causes the subtype of +** the result from the [application-defined SQL function] with +** [sqlite3_context] C to be the value T. Only the lower 8 bits +** of the subtype T are preserved in current versions of SQLite; +** higher order bits are discarded. +** The number of subtype bytes preserved by SQLite might increase +** in future releases of SQLite. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context*,unsigned int); + +/* +** CAPI3REF: Define New Collating Sequences +** METHOD: sqlite3 +** +** ^These functions add, remove, or modify a [collation] associated +** with the [database connection] specified as the first argument. +** +** ^The name of the collation is a UTF-8 string +** for sqlite3_create_collation() and sqlite3_create_collation_v2() +** and a UTF-16 string in native byte order for sqlite3_create_collation16(). +** ^Collation names that compare equal according to [sqlite3_strnicmp()] are +** considered to be the same name. +** +** ^(The third argument (eTextRep) must be one of the constants: +**
    +**
  • [SQLITE_UTF8], +**
  • [SQLITE_UTF16LE], +**
  • [SQLITE_UTF16BE], +**
  • [SQLITE_UTF16], or +**
  • [SQLITE_UTF16_ALIGNED]. +**
)^ +** ^The eTextRep argument determines the encoding of strings passed +** to the collating function callback, xCallback. +** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep +** force strings to be UTF16 with native byte order. +** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin +** on an even byte address. +** +** ^The fourth argument, pArg, is an application data pointer that is passed +** through as the first argument to the collating function callback. +** +** ^The fifth argument, xCallback, is a pointer to the collating function. +** ^Multiple collating functions can be registered using the same name but +** with different eTextRep parameters and SQLite will use whichever +** function requires the least amount of data transformation. +** ^If the xCallback argument is NULL then the collating function is +** deleted. ^When all collating functions having the same name are deleted, +** that collation is no longer usable. +** +** ^The collating function callback is invoked with a copy of the pArg +** application data pointer and with two strings in the encoding specified +** by the eTextRep argument. The collating function must return an +** integer that is negative, zero, or positive +** if the first string is less than, equal to, or greater than the second, +** respectively. A collating function must always return the same answer +** given the same inputs. If two or more collating functions are registered +** to the same collation name (using different eTextRep values) then all +** must give an equivalent answer when invoked with equivalent strings. +** The collating function must obey the following properties for all +** strings A, B, and C: +** +**
    +**
  1. If A==B then B==A. +**
  2. If A==B and B==C then A==C. +**
  3. If A<B THEN B>A. +**
  4. If A<B and B<C then A<C. +**
+** +** If a collating function fails any of the above constraints and that +** collating function is registered and used, then the behavior of SQLite +** is undefined. +** +** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() +** with the addition that the xDestroy callback is invoked on pArg when +** the collating function is deleted. +** ^Collating functions are deleted when they are overridden by later +** calls to the collation creation functions or when the +** [database connection] is closed using [sqlite3_close()]. +** +** ^The xDestroy callback is not called if the +** sqlite3_create_collation_v2() function fails. Applications that invoke +** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should +** check the return code and dispose of the application data pointer +** themselves rather than expecting SQLite to deal with it for them. +** This is different from every other SQLite interface. The inconsistency +** is unfortunate but cannot be changed without breaking backwards +** compatibility. +** +** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_collation( + sqlite3*, + const char *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*) +); +SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2( + sqlite3*, + const char *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDestroy)(void*) +); +SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16( + sqlite3*, + const void *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*) +); + +/* +** CAPI3REF: Collation Needed Callbacks +** METHOD: sqlite3 +** +** ^To avoid having to register all collation sequences before a database +** can be used, a single callback function may be registered with the +** [database connection] to be invoked whenever an undefined collation +** sequence is required. +** +** ^If the function is registered using the sqlite3_collation_needed() API, +** then it is passed the names of undefined collation sequences as strings +** encoded in UTF-8. ^If sqlite3_collation_needed16() is used, +** the names are passed as UTF-16 in machine native byte order. +** ^A call to either function replaces the existing collation-needed callback. +** +** ^(When the callback is invoked, the first argument passed is a copy +** of the second argument to sqlite3_collation_needed() or +** sqlite3_collation_needed16(). The second argument is the database +** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], +** or [SQLITE_UTF16LE], indicating the most desirable form of the collation +** sequence function required. The fourth parameter is the name of the +** required collation sequence.)^ +** +** The callback function should register the desired collation using +** [sqlite3_create_collation()], [sqlite3_create_collation16()], or +** [sqlite3_create_collation_v2()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed( + sqlite3*, + void*, + void(*)(void*,sqlite3*,int eTextRep,const char*) +); +SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16( + sqlite3*, + void*, + void(*)(void*,sqlite3*,int eTextRep,const void*) +); + +#ifdef SQLITE_HAS_CODEC +/* +** Specify the key for an encrypted database. This routine should be +** called right after sqlite3_open(). +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_key( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The key */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_key_v2( + sqlite3 *db, /* Database to be rekeyed */ + const char *zDbName, /* Name of the database */ + const void *pKey, int nKey /* The key */ +); + +/* +** Change the key on an open database. If the current database is not +** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the +** database is decrypted. +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_rekey( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The new key */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2( + sqlite3 *db, /* Database to be rekeyed */ + const char *zDbName, /* Name of the database */ + const void *pKey, int nKey /* The new key */ +); + +/* +** Specify the activation key for a SEE database. Unless +** activated, none of the SEE routines will work. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_activate_see( + const char *zPassPhrase /* Activation phrase */ +); +#endif + +#ifdef SQLITE_ENABLE_CEROD +/* +** Specify the activation key for a CEROD database. Unless +** activated, none of the CEROD routines will work. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod( + const char *zPassPhrase /* Activation phrase */ +); +#endif + +/* +** CAPI3REF: Suspend Execution For A Short Time +** +** The sqlite3_sleep() function causes the current thread to suspend execution +** for at least a number of milliseconds specified in its parameter. +** +** If the operating system does not support sleep requests with +** millisecond time resolution, then the time will be rounded up to +** the nearest second. The number of milliseconds of sleep actually +** requested from the operating system is returned. +** +** ^SQLite implements this interface by calling the xSleep() +** method of the default [sqlite3_vfs] object. If the xSleep() method +** of the default VFS is not implemented correctly, or not implemented at +** all, then the behavior of sqlite3_sleep() may deviate from the description +** in the previous paragraphs. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int); + +/* +** CAPI3REF: Name Of The Folder Holding Temporary Files +** +** ^(If this global variable is made to point to a string which is +** the name of a folder (a.k.a. directory), then all temporary files +** created by SQLite when using a built-in [sqlite3_vfs | VFS] +** will be placed in that directory.)^ ^If this variable +** is a NULL pointer, then SQLite performs a search for an appropriate +** temporary file directory. +** +** Applications are strongly discouraged from using this global variable. +** It is required to set a temporary folder on Windows Runtime (WinRT). +** But for all other platforms, it is highly recommended that applications +** neither read nor write this variable. This global variable is a relic +** that exists for backwards compatibility of legacy applications and should +** be avoided in new projects. +** +** It is not safe to read or modify this variable in more than one +** thread at a time. It is not safe to read or modify this variable +** if a [database connection] is being used at the same time in a separate +** thread. +** It is intended that this variable be set once +** as part of process initialization and before any SQLite interface +** routines have been called and that this variable remain unchanged +** thereafter. +** +** ^The [temp_store_directory pragma] may modify this variable and cause +** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, +** the [temp_store_directory pragma] always assumes that any string +** that this variable points to is held in memory obtained from +** [sqlite3_malloc] and the pragma may attempt to free that memory +** using [sqlite3_free]. +** Hence, if this variable is modified directly, either it should be +** made NULL or made to point to memory obtained from [sqlite3_malloc] +** or else the use of the [temp_store_directory pragma] should be avoided. +** Except when requested by the [temp_store_directory pragma], SQLite +** does not free the memory that sqlite3_temp_directory points to. If +** the application wants that memory to be freed, it must do +** so itself, taking care to only do so after all [database connection] +** objects have been destroyed. +** +** Note to Windows Runtime users: The temporary directory must be set +** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various +** features that require the use of temporary files may fail. Here is an +** example of how to do this using C++ with the Windows Runtime: +** +**
+** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
+**       TemporaryFolder->Path->Data();
+** char zPathBuf[MAX_PATH + 1];
+** memset(zPathBuf, 0, sizeof(zPathBuf));
+** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
+**       NULL, NULL);
+** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
+** 
+*/ +SQLITE_API char *sqlite3_temp_directory; + +/* +** CAPI3REF: Name Of The Folder Holding Database Files +** +** ^(If this global variable is made to point to a string which is +** the name of a folder (a.k.a. directory), then all database files +** specified with a relative pathname and created or accessed by +** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed +** to be relative to that directory.)^ ^If this variable is a NULL +** pointer, then SQLite assumes that all database files specified +** with a relative pathname are relative to the current directory +** for the process. Only the windows VFS makes use of this global +** variable; it is ignored by the unix VFS. +** +** Changing the value of this variable while a database connection is +** open can result in a corrupt database. +** +** It is not safe to read or modify this variable in more than one +** thread at a time. It is not safe to read or modify this variable +** if a [database connection] is being used at the same time in a separate +** thread. +** It is intended that this variable be set once +** as part of process initialization and before any SQLite interface +** routines have been called and that this variable remain unchanged +** thereafter. +** +** ^The [data_store_directory pragma] may modify this variable and cause +** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, +** the [data_store_directory pragma] always assumes that any string +** that this variable points to is held in memory obtained from +** [sqlite3_malloc] and the pragma may attempt to free that memory +** using [sqlite3_free]. +** Hence, if this variable is modified directly, either it should be +** made NULL or made to point to memory obtained from [sqlite3_malloc] +** or else the use of the [data_store_directory pragma] should be avoided. +*/ +SQLITE_API char *sqlite3_data_directory; + +/* +** CAPI3REF: Test For Auto-Commit Mode +** KEYWORDS: {autocommit mode} +** METHOD: sqlite3 +** +** ^The sqlite3_get_autocommit() interface returns non-zero or +** zero if the given database connection is or is not in autocommit mode, +** respectively. ^Autocommit mode is on by default. +** ^Autocommit mode is disabled by a [BEGIN] statement. +** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK]. +** +** If certain kinds of errors occur on a statement within a multi-statement +** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR], +** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the +** transaction might be rolled back automatically. The only way to +** find out whether SQLite automatically rolled back the transaction after +** an error is to use this function. +** +** If another thread changes the autocommit status of the database +** connection while this routine is running, then the return value +** is undefined. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*); + +/* +** CAPI3REF: Find The Database Handle Of A Prepared Statement +** METHOD: sqlite3_stmt +** +** ^The sqlite3_db_handle interface returns the [database connection] handle +** to which a [prepared statement] belongs. ^The [database connection] +** returned by sqlite3_db_handle is the same [database connection] +** that was the first argument +** to the [sqlite3_prepare_v2()] call (or its variants) that was used to +** create the statement in the first place. +*/ +SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*); + +/* +** CAPI3REF: Return The Filename For A Database Connection +** METHOD: sqlite3 +** +** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename +** associated with database N of connection D. ^The main database file +** has the name "main". If there is no attached database N on the database +** connection D, or if database N is a temporary or in-memory database, then +** a NULL pointer is returned. +** +** ^The filename returned by this function is the output of the +** xFullPathname method of the [VFS]. ^In other words, the filename +** will be an absolute pathname, even if the filename used +** to open the database originally was a URI or relative pathname. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName); + +/* +** CAPI3REF: Determine if a database is read-only +** METHOD: sqlite3 +** +** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N +** of connection D is read-only, 0 if it is read/write, or -1 if N is not +** the name of a database on connection D. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName); + +/* +** CAPI3REF: Find the next prepared statement +** METHOD: sqlite3 +** +** ^This interface returns a pointer to the next [prepared statement] after +** pStmt associated with the [database connection] pDb. ^If pStmt is NULL +** then this interface returns a pointer to the first prepared statement +** associated with the database connection pDb. ^If no prepared statement +** satisfies the conditions of this routine, it returns NULL. +** +** The [database connection] pointer D in a call to +** [sqlite3_next_stmt(D,S)] must refer to an open database +** connection and in particular must not be a NULL pointer. +*/ +SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Commit And Rollback Notification Callbacks +** METHOD: sqlite3 +** +** ^The sqlite3_commit_hook() interface registers a callback +** function to be invoked whenever a transaction is [COMMIT | committed]. +** ^Any callback set by a previous call to sqlite3_commit_hook() +** for the same database connection is overridden. +** ^The sqlite3_rollback_hook() interface registers a callback +** function to be invoked whenever a transaction is [ROLLBACK | rolled back]. +** ^Any callback set by a previous call to sqlite3_rollback_hook() +** for the same database connection is overridden. +** ^The pArg argument is passed through to the callback. +** ^If the callback on a commit hook function returns non-zero, +** then the commit is converted into a rollback. +** +** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions +** return the P argument from the previous call of the same function +** on the same [database connection] D, or NULL for +** the first call for each function on D. +** +** The commit and rollback hook callbacks are not reentrant. +** The callback implementation must not do anything that will modify +** the database connection that invoked the callback. Any actions +** to modify the database connection must be deferred until after the +** completion of the [sqlite3_step()] call that triggered the commit +** or rollback hook in the first place. +** Note that running any other SQL statements, including SELECT statements, +** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify +** the database connections for the meaning of "modify" in this paragraph. +** +** ^Registering a NULL function disables the callback. +** +** ^When the commit hook callback routine returns zero, the [COMMIT] +** operation is allowed to continue normally. ^If the commit hook +** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK]. +** ^The rollback hook is invoked on a rollback that results from a commit +** hook returning non-zero, just as it would be with any other rollback. +** +** ^For the purposes of this API, a transaction is said to have been +** rolled back if an explicit "ROLLBACK" statement is executed, or +** an error or constraint causes an implicit rollback to occur. +** ^The rollback callback is not invoked if a transaction is +** automatically rolled back because the database connection is closed. +** +** See also the [sqlite3_update_hook()] interface. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); +SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); + +/* +** CAPI3REF: Data Change Notification Callbacks +** METHOD: sqlite3 +** +** ^The sqlite3_update_hook() interface registers a callback function +** with the [database connection] identified by the first argument +** to be invoked whenever a row is updated, inserted or deleted in +** a [rowid table]. +** ^Any callback set by a previous call to this function +** for the same database connection is overridden. +** +** ^The second argument is a pointer to the function to invoke when a +** row is updated, inserted or deleted in a rowid table. +** ^The first argument to the callback is a copy of the third argument +** to sqlite3_update_hook(). +** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE], +** or [SQLITE_UPDATE], depending on the operation that caused the callback +** to be invoked. +** ^The third and fourth arguments to the callback contain pointers to the +** database and table name containing the affected row. +** ^The final callback parameter is the [rowid] of the row. +** ^In the case of an update, this is the [rowid] after the update takes place. +** +** ^(The update hook is not invoked when internal system tables are +** modified (i.e. sqlite_master and sqlite_sequence).)^ +** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified. +** +** ^In the current implementation, the update hook +** is not invoked when duplication rows are deleted because of an +** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook +** invoked when rows are deleted using the [truncate optimization]. +** The exceptions defined in this paragraph might change in a future +** release of SQLite. +** +** The update hook implementation must not do anything that will modify +** the database connection that invoked the update hook. Any actions +** to modify the database connection must be deferred until after the +** completion of the [sqlite3_step()] call that triggered the update hook. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +** ^The sqlite3_update_hook(D,C,P) function +** returns the P argument from the previous call +** on the same [database connection] D, or NULL for +** the first call on D. +** +** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()], +** and [sqlite3_preupdate_hook()] interfaces. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook( + sqlite3*, + void(*)(void *,int ,char const *,char const *,sqlite3_int64), + void* +); + +/* +** CAPI3REF: Enable Or Disable Shared Pager Cache +** +** ^(This routine enables or disables the sharing of the database cache +** and schema data structures between [database connection | connections] +** to the same database. Sharing is enabled if the argument is true +** and disabled if the argument is false.)^ +** +** ^Cache sharing is enabled and disabled for an entire process. +** This is a change as of SQLite version 3.5.0. In prior versions of SQLite, +** sharing was enabled or disabled for each thread separately. +** +** ^(The cache sharing mode set by this interface effects all subsequent +** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. +** Existing database connections continue use the sharing mode +** that was in effect at the time they were opened.)^ +** +** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled +** successfully. An [error code] is returned otherwise.)^ +** +** ^Shared cache is disabled by default. But this might change in +** future releases of SQLite. Applications that care about shared +** cache setting should set it explicitly. +** +** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0 +** and will always return SQLITE_MISUSE. On those systems, +** shared cache mode should be enabled per-database connection via +** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE]. +** +** This interface is threadsafe on processors where writing a +** 32-bit integer is atomic. +** +** See Also: [SQLite Shared-Cache Mode] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int); + +/* +** CAPI3REF: Attempt To Free Heap Memory +** +** ^The sqlite3_release_memory() interface attempts to free N bytes +** of heap memory by deallocating non-essential memory allocations +** held by the database library. Memory used to cache database +** pages to improve performance is an example of non-essential memory. +** ^sqlite3_release_memory() returns the number of bytes actually freed, +** which might be more or less than the amount requested. +** ^The sqlite3_release_memory() routine is a no-op returning zero +** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. +** +** See also: [sqlite3_db_release_memory()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int); + +/* +** CAPI3REF: Free Memory Used By A Database Connection +** METHOD: sqlite3 +** +** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap +** memory as possible from database connection D. Unlike the +** [sqlite3_release_memory()] interface, this interface is in effect even +** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is +** omitted. +** +** See also: [sqlite3_release_memory()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*); + +/* +** CAPI3REF: Impose A Limit On Heap Size +** +** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the +** soft limit on the amount of heap memory that may be allocated by SQLite. +** ^SQLite strives to keep heap memory utilization below the soft heap +** limit by reducing the number of pages held in the page cache +** as heap memory usages approaches the limit. +** ^The soft heap limit is "soft" because even though SQLite strives to stay +** below the limit, it will exceed the limit rather than generate +** an [SQLITE_NOMEM] error. In other words, the soft heap limit +** is advisory only. +** +** ^The return value from sqlite3_soft_heap_limit64() is the size of +** the soft heap limit prior to the call, or negative in the case of an +** error. ^If the argument N is negative +** then no change is made to the soft heap limit. Hence, the current +** size of the soft heap limit can be determined by invoking +** sqlite3_soft_heap_limit64() with a negative argument. +** +** ^If the argument N is zero then the soft heap limit is disabled. +** +** ^(The soft heap limit is not enforced in the current implementation +** if one or more of following conditions are true: +** +**
    +**
  • The soft heap limit is set to zero. +**
  • Memory accounting is disabled using a combination of the +** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and +** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. +**
  • An alternative page cache implementation is specified using +** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...). +**
  • The page cache allocates from its own memory pool supplied +** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than +** from the heap. +**
)^ +** +** Beginning with SQLite version 3.7.3, the soft heap limit is enforced +** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] +** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], +** the soft heap limit is enforced on every memory allocation. Without +** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced +** when memory is allocated by the page cache. Testing suggests that because +** the page cache is the predominate memory user in SQLite, most +** applications will achieve adequate soft heap limit enforcement without +** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. +** +** The circumstances under which SQLite will enforce the soft heap limit may +** changes in future releases of SQLite. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 N); + +/* +** CAPI3REF: Deprecated Soft Heap Limit Interface +** DEPRECATED +** +** This is a deprecated version of the [sqlite3_soft_heap_limit64()] +** interface. This routine is provided for historical compatibility +** only. All new applications should use the +** [sqlite3_soft_heap_limit64()] interface rather than this one. +*/ +SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N); + + +/* +** CAPI3REF: Extract Metadata About A Column Of A Table +** METHOD: sqlite3 +** +** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns +** information about column C of table T in database D +** on [database connection] X.)^ ^The sqlite3_table_column_metadata() +** interface returns SQLITE_OK and fills in the non-NULL pointers in +** the final five arguments with appropriate values if the specified +** column exists. ^The sqlite3_table_column_metadata() interface returns +** SQLITE_ERROR and if the specified column does not exist. +** ^If the column-name parameter to sqlite3_table_column_metadata() is a +** NULL pointer, then this routine simply checks for the existance of the +** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it +** does not. +** +** ^The column is identified by the second, third and fourth parameters to +** this function. ^(The second parameter is either the name of the database +** (i.e. "main", "temp", or an attached database) containing the specified +** table or NULL.)^ ^If it is NULL, then all attached databases are searched +** for the table using the same algorithm used by the database engine to +** resolve unqualified table references. +** +** ^The third and fourth parameters to this function are the table and column +** name of the desired column, respectively. +** +** ^Metadata is returned by writing to the memory locations passed as the 5th +** and subsequent parameters to this function. ^Any of these arguments may be +** NULL, in which case the corresponding element of metadata is omitted. +** +** ^(
+** +**
Parameter Output
Type
Description +** +**
5th const char* Data type +**
6th const char* Name of default collation sequence +**
7th int True if column has a NOT NULL constraint +**
8th int True if column is part of the PRIMARY KEY +**
9th int True if column is [AUTOINCREMENT] +**
+**
)^ +** +** ^The memory pointed to by the character pointers returned for the +** declaration type and collation sequence is valid until the next +** call to any SQLite API function. +** +** ^If the specified table is actually a view, an [error code] is returned. +** +** ^If the specified column is "rowid", "oid" or "_rowid_" and the table +** is not a [WITHOUT ROWID] table and an +** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output +** parameters are set for the explicitly declared column. ^(If there is no +** [INTEGER PRIMARY KEY] column, then the outputs +** for the [rowid] are set as follows: +** +**
+**     data type: "INTEGER"
+**     collation sequence: "BINARY"
+**     not null: 0
+**     primary key: 1
+**     auto increment: 0
+** 
)^ +** +** ^This function causes all database schemas to be read from disk and +** parsed, if that has not already been done, and returns an error if +** any errors are encountered while loading the schema. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata( + sqlite3 *db, /* Connection handle */ + const char *zDbName, /* Database name or NULL */ + const char *zTableName, /* Table name */ + const char *zColumnName, /* Column name */ + char const **pzDataType, /* OUTPUT: Declared data type */ + char const **pzCollSeq, /* OUTPUT: Collation sequence name */ + int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ + int *pPrimaryKey, /* OUTPUT: True if column part of PK */ + int *pAutoinc /* OUTPUT: True if column is auto-increment */ +); + +/* +** CAPI3REF: Load An Extension +** METHOD: sqlite3 +** +** ^This interface loads an SQLite extension library from the named file. +** +** ^The sqlite3_load_extension() interface attempts to load an +** [SQLite extension] library contained in the file zFile. If +** the file cannot be loaded directly, attempts are made to load +** with various operating-system specific extensions added. +** So for example, if "samplelib" cannot be loaded, then names like +** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might +** be tried also. +** +** ^The entry point is zProc. +** ^(zProc may be 0, in which case SQLite will try to come up with an +** entry point name on its own. It first tries "sqlite3_extension_init". +** If that does not work, it constructs a name "sqlite3_X_init" where the +** X is consists of the lower-case equivalent of all ASCII alphabetic +** characters in the filename from the last "/" to the first following +** "." and omitting any initial "lib".)^ +** ^The sqlite3_load_extension() interface returns +** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. +** ^If an error occurs and pzErrMsg is not 0, then the +** [sqlite3_load_extension()] interface shall attempt to +** fill *pzErrMsg with error message text stored in memory +** obtained from [sqlite3_malloc()]. The calling function +** should free this memory by calling [sqlite3_free()]. +** +** ^Extension loading must be enabled using +** [sqlite3_enable_load_extension()] or +** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL) +** prior to calling this API, +** otherwise an error will be returned. +** +** Security warning: It is recommended that the +** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this +** interface. The use of the [sqlite3_enable_load_extension()] interface +** should be avoided. This will keep the SQL function [load_extension()] +** disabled and prevent SQL injections from giving attackers +** access to extension loading capabilities. +** +** See also the [load_extension() SQL function]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_load_extension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Derived from zFile if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +); + +/* +** CAPI3REF: Enable Or Disable Extension Loading +** METHOD: sqlite3 +** +** ^So as not to open security holes in older applications that are +** unprepared to deal with [extension loading], and as a means of disabling +** [extension loading] while evaluating user-entered SQL, the following API +** is provided to turn the [sqlite3_load_extension()] mechanism on and off. +** +** ^Extension loading is off by default. +** ^Call the sqlite3_enable_load_extension() routine with onoff==1 +** to turn extension loading on and call it with onoff==0 to turn +** it back off again. +** +** ^This interface enables or disables both the C-API +** [sqlite3_load_extension()] and the SQL function [load_extension()]. +** Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..) +** to enable or disable only the C-API. +** +** Security warning: It is recommended that extension loading +** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method +** rather than this interface, so the [load_extension()] SQL function +** remains disabled. This will prevent SQL injections from giving attackers +** access to extension loading capabilities. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff); + +/* +** CAPI3REF: Automatically Load Statically Linked Extensions +** +** ^This interface causes the xEntryPoint() function to be invoked for +** each new [database connection] that is created. The idea here is that +** xEntryPoint() is the entry point for a statically linked [SQLite extension] +** that is to be automatically loaded into all new database connections. +** +** ^(Even though the function prototype shows that xEntryPoint() takes +** no arguments and returns void, SQLite invokes xEntryPoint() with three +** arguments and expects and integer result as if the signature of the +** entry point where as follows: +** +**
+**    int xEntryPoint(
+**      sqlite3 *db,
+**      const char **pzErrMsg,
+**      const struct sqlite3_api_routines *pThunk
+**    );
+** 
)^ +** +** If the xEntryPoint routine encounters an error, it should make *pzErrMsg +** point to an appropriate error message (obtained from [sqlite3_mprintf()]) +** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg +** is NULL before calling the xEntryPoint(). ^SQLite will invoke +** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any +** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. +** +** ^Calling sqlite3_auto_extension(X) with an entry point X that is already +** on the list of automatic extensions is a harmless no-op. ^No entry point +** will be called more than once for each database connection that is opened. +** +** See also: [sqlite3_reset_auto_extension()] +** and [sqlite3_cancel_auto_extension()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void)); + +/* +** CAPI3REF: Cancel Automatic Extension Loading +** +** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the +** initialization routine X that was registered using a prior call to +** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)] +** routine returns 1 if initialization routine X was successfully +** unregistered and it returns 0 if X was not on the list of initialization +** routines. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(void)); + +/* +** CAPI3REF: Reset Automatic Extension Loading +** +** ^This interface disables all automatic extensions previously +** registered using [sqlite3_auto_extension()]. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void); + +/* +** The interface to the virtual-table mechanism is currently considered +** to be experimental. The interface might change in incompatible ways. +** If this is a problem for you, do not use the interface at this time. +** +** When the virtual-table mechanism stabilizes, we will declare the +** interface fixed, support it indefinitely, and remove this comment. +*/ + +/* +** Structures used by the virtual table interface +*/ +typedef struct sqlite3_vtab sqlite3_vtab; +typedef struct sqlite3_index_info sqlite3_index_info; +typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; +typedef struct sqlite3_module sqlite3_module; + +/* +** CAPI3REF: Virtual Table Object +** KEYWORDS: sqlite3_module {virtual table module} +** +** This structure, sometimes called a "virtual table module", +** defines the implementation of a [virtual tables]. +** This structure consists mostly of methods for the module. +** +** ^A virtual table module is created by filling in a persistent +** instance of this structure and passing a pointer to that instance +** to [sqlite3_create_module()] or [sqlite3_create_module_v2()]. +** ^The registration remains valid until it is replaced by a different +** module or until the [database connection] closes. The content +** of this structure must not change while it is registered with +** any database connection. +*/ +struct sqlite3_module { + int iVersion; + int (*xCreate)(sqlite3*, void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVTab, char**); + int (*xConnect)(sqlite3*, void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVTab, char**); + int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); + int (*xDisconnect)(sqlite3_vtab *pVTab); + int (*xDestroy)(sqlite3_vtab *pVTab); + int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); + int (*xClose)(sqlite3_vtab_cursor*); + int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, + int argc, sqlite3_value **argv); + int (*xNext)(sqlite3_vtab_cursor*); + int (*xEof)(sqlite3_vtab_cursor*); + int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); + int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); + int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); + int (*xBegin)(sqlite3_vtab *pVTab); + int (*xSync)(sqlite3_vtab *pVTab); + int (*xCommit)(sqlite3_vtab *pVTab); + int (*xRollback)(sqlite3_vtab *pVTab); + int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), + void **ppArg); + int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); + /* The methods above are in version 1 of the sqlite_module object. Those + ** below are for version 2 and greater. */ + int (*xSavepoint)(sqlite3_vtab *pVTab, int); + int (*xRelease)(sqlite3_vtab *pVTab, int); + int (*xRollbackTo)(sqlite3_vtab *pVTab, int); +}; + +/* +** CAPI3REF: Virtual Table Indexing Information +** KEYWORDS: sqlite3_index_info +** +** The sqlite3_index_info structure and its substructures is used as part +** of the [virtual table] interface to +** pass information into and receive the reply from the [xBestIndex] +** method of a [virtual table module]. The fields under **Inputs** are the +** inputs to xBestIndex and are read-only. xBestIndex inserts its +** results into the **Outputs** fields. +** +** ^(The aConstraint[] array records WHERE clause constraints of the form: +** +**
column OP expr
+** +** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is +** stored in aConstraint[].op using one of the +** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ +** ^(The index of the column is stored in +** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the +** expr on the right-hand side can be evaluated (and thus the constraint +** is usable) and false if it cannot.)^ +** +** ^The optimizer automatically inverts terms of the form "expr OP column" +** and makes other simplifications to the WHERE clause in an attempt to +** get as many WHERE clause terms into the form shown above as possible. +** ^The aConstraint[] array only reports WHERE clause terms that are +** relevant to the particular virtual table being queried. +** +** ^Information about the ORDER BY clause is stored in aOrderBy[]. +** ^Each term of aOrderBy records a column of the ORDER BY clause. +** +** The colUsed field indicates which columns of the virtual table may be +** required by the current scan. Virtual table columns are numbered from +** zero in the order in which they appear within the CREATE TABLE statement +** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62), +** the corresponding bit is set within the colUsed mask if the column may be +** required by SQLite. If the table has at least 64 columns and any column +** to the right of the first 63 is required, then bit 63 of colUsed is also +** set. In other words, column iCol may be required if the expression +** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to +** non-zero. +** +** The [xBestIndex] method must fill aConstraintUsage[] with information +** about what parameters to pass to xFilter. ^If argvIndex>0 then +** the right-hand side of the corresponding aConstraint[] is evaluated +** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit +** is true, then the constraint is assumed to be fully handled by the +** virtual table and is not checked again by SQLite.)^ +** +** ^The idxNum and idxPtr values are recorded and passed into the +** [xFilter] method. +** ^[sqlite3_free()] is used to free idxPtr if and only if +** needToFreeIdxPtr is true. +** +** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in +** the correct order to satisfy the ORDER BY clause so that no separate +** sorting step is required. +** +** ^The estimatedCost value is an estimate of the cost of a particular +** strategy. A cost of N indicates that the cost of the strategy is similar +** to a linear scan of an SQLite table with N rows. A cost of log(N) +** indicates that the expense of the operation is similar to that of a +** binary search on a unique indexed field of an SQLite table with N rows. +** +** ^The estimatedRows value is an estimate of the number of rows that +** will be returned by the strategy. +** +** The xBestIndex method may optionally populate the idxFlags field with a +** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag - +** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite +** assumes that the strategy may visit at most one row. +** +** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then +** SQLite also assumes that if a call to the xUpdate() method is made as +** part of the same statement to delete or update a virtual table row and the +** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback +** any database changes. In other words, if the xUpdate() returns +** SQLITE_CONSTRAINT, the database contents must be exactly as they were +** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not +** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by +** the xUpdate method are automatically rolled back by SQLite. +** +** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info +** structure for SQLite version 3.8.2. If a virtual table extension is +** used with an SQLite version earlier than 3.8.2, the results of attempting +** to read or write the estimatedRows field are undefined (but are likely +** to included crashing the application). The estimatedRows field should +** therefore only be used if [sqlite3_libversion_number()] returns a +** value greater than or equal to 3008002. Similarly, the idxFlags field +** was added for version 3.9.0. It may therefore only be used if +** sqlite3_libversion_number() returns a value greater than or equal to +** 3009000. +*/ +struct sqlite3_index_info { + /* Inputs */ + int nConstraint; /* Number of entries in aConstraint */ + struct sqlite3_index_constraint { + int iColumn; /* Column constrained. -1 for ROWID */ + unsigned char op; /* Constraint operator */ + unsigned char usable; /* True if this constraint is usable */ + int iTermOffset; /* Used internally - xBestIndex should ignore */ + } *aConstraint; /* Table of WHERE clause constraints */ + int nOrderBy; /* Number of terms in the ORDER BY clause */ + struct sqlite3_index_orderby { + int iColumn; /* Column number */ + unsigned char desc; /* True for DESC. False for ASC. */ + } *aOrderBy; /* The ORDER BY clause */ + /* Outputs */ + struct sqlite3_index_constraint_usage { + int argvIndex; /* if >0, constraint is part of argv to xFilter */ + unsigned char omit; /* Do not code a test for this constraint */ + } *aConstraintUsage; + int idxNum; /* Number used to identify the index */ + char *idxStr; /* String, possibly obtained from sqlite3_malloc */ + int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ + int orderByConsumed; /* True if output is already ordered */ + double estimatedCost; /* Estimated cost of using this index */ + /* Fields below are only available in SQLite 3.8.2 and later */ + sqlite3_int64 estimatedRows; /* Estimated number of rows returned */ + /* Fields below are only available in SQLite 3.9.0 and later */ + int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */ + /* Fields below are only available in SQLite 3.10.0 and later */ + sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */ +}; + +/* +** CAPI3REF: Virtual Table Scan Flags +*/ +#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */ + +/* +** CAPI3REF: Virtual Table Constraint Operator Codes +** +** These macros defined the allowed values for the +** [sqlite3_index_info].aConstraint[].op field. Each value represents +** an operator that is part of a constraint term in the wHERE clause of +** a query that uses a [virtual table]. +*/ +#define SQLITE_INDEX_CONSTRAINT_EQ 2 +#define SQLITE_INDEX_CONSTRAINT_GT 4 +#define SQLITE_INDEX_CONSTRAINT_LE 8 +#define SQLITE_INDEX_CONSTRAINT_LT 16 +#define SQLITE_INDEX_CONSTRAINT_GE 32 +#define SQLITE_INDEX_CONSTRAINT_MATCH 64 +#define SQLITE_INDEX_CONSTRAINT_LIKE 65 +#define SQLITE_INDEX_CONSTRAINT_GLOB 66 +#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 + +/* +** CAPI3REF: Register A Virtual Table Implementation +** METHOD: sqlite3 +** +** ^These routines are used to register a new [virtual table module] name. +** ^Module names must be registered before +** creating a new [virtual table] using the module and before using a +** preexisting [virtual table] for the module. +** +** ^The module name is registered on the [database connection] specified +** by the first parameter. ^The name of the module is given by the +** second parameter. ^The third parameter is a pointer to +** the implementation of the [virtual table module]. ^The fourth +** parameter is an arbitrary client data pointer that is passed through +** into the [xCreate] and [xConnect] methods of the virtual table module +** when a new virtual table is be being created or reinitialized. +** +** ^The sqlite3_create_module_v2() interface has a fifth parameter which +** is a pointer to a destructor for the pClientData. ^SQLite will +** invoke the destructor function (if it is not NULL) when SQLite +** no longer needs the pClientData pointer. ^The destructor will also +** be invoked if the call to sqlite3_create_module_v2() fails. +** ^The sqlite3_create_module() +** interface is equivalent to sqlite3_create_module_v2() with a NULL +** destructor. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_module( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ + void *pClientData /* Client data for xCreate/xConnect */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ + void *pClientData, /* Client data for xCreate/xConnect */ + void(*xDestroy)(void*) /* Module destructor function */ +); + +/* +** CAPI3REF: Virtual Table Instance Object +** KEYWORDS: sqlite3_vtab +** +** Every [virtual table module] implementation uses a subclass +** of this object to describe a particular instance +** of the [virtual table]. Each subclass will +** be tailored to the specific needs of the module implementation. +** The purpose of this superclass is to define certain fields that are +** common to all module implementations. +** +** ^Virtual tables methods can set an error message by assigning a +** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should +** take care that any prior string is freed by a call to [sqlite3_free()] +** prior to assigning a new string to zErrMsg. ^After the error message +** is delivered up to the client application, the string will be automatically +** freed by sqlite3_free() and the zErrMsg field will be zeroed. +*/ +struct sqlite3_vtab { + const sqlite3_module *pModule; /* The module for this virtual table */ + int nRef; /* Number of open cursors */ + char *zErrMsg; /* Error message from sqlite3_mprintf() */ + /* Virtual table implementations will typically add additional fields */ +}; + +/* +** CAPI3REF: Virtual Table Cursor Object +** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} +** +** Every [virtual table module] implementation uses a subclass of the +** following structure to describe cursors that point into the +** [virtual table] and are used +** to loop through the virtual table. Cursors are created using the +** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed +** by the [sqlite3_module.xClose | xClose] method. Cursors are used +** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods +** of the module. Each module implementation will define +** the content of a cursor structure to suit its own needs. +** +** This superclass exists in order to define fields of the cursor that +** are common to all implementations. +*/ +struct sqlite3_vtab_cursor { + sqlite3_vtab *pVtab; /* Virtual table of this cursor */ + /* Virtual table implementations will typically add additional fields */ +}; + +/* +** CAPI3REF: Declare The Schema Of A Virtual Table +** +** ^The [xCreate] and [xConnect] methods of a +** [virtual table module] call this interface +** to declare the format (the names and datatypes of the columns) of +** the virtual tables they implement. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL); + +/* +** CAPI3REF: Overload A Function For A Virtual Table +** METHOD: sqlite3 +** +** ^(Virtual tables can provide alternative implementations of functions +** using the [xFindFunction] method of the [virtual table module]. +** But global versions of those functions +** must exist in order to be overloaded.)^ +** +** ^(This API makes sure a global version of a function with a particular +** name and number of parameters exists. If no such function exists +** before this API is called, a new function is created.)^ ^The implementation +** of the new function always causes an exception to be thrown. So +** the new function is not good for anything by itself. Its only +** purpose is to be a placeholder function that can be overloaded +** by a [virtual table]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); + +/* +** The interface to the virtual-table mechanism defined above (back up +** to a comment remarkably similar to this one) is currently considered +** to be experimental. The interface might change in incompatible ways. +** If this is a problem for you, do not use the interface at this time. +** +** When the virtual-table mechanism stabilizes, we will declare the +** interface fixed, support it indefinitely, and remove this comment. +*/ + +/* +** CAPI3REF: A Handle To An Open BLOB +** KEYWORDS: {BLOB handle} {BLOB handles} +** +** An instance of this object represents an open BLOB on which +** [sqlite3_blob_open | incremental BLOB I/O] can be performed. +** ^Objects of this type are created by [sqlite3_blob_open()] +** and destroyed by [sqlite3_blob_close()]. +** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces +** can be used to read or write small subsections of the BLOB. +** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes. +*/ +typedef struct sqlite3_blob sqlite3_blob; + +/* +** CAPI3REF: Open A BLOB For Incremental I/O +** METHOD: sqlite3 +** CONSTRUCTOR: sqlite3_blob +** +** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located +** in row iRow, column zColumn, table zTable in database zDb; +** in other words, the same BLOB that would be selected by: +** +**
+**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
+** 
)^ +** +** ^(Parameter zDb is not the filename that contains the database, but +** rather the symbolic name of the database. For attached databases, this is +** the name that appears after the AS keyword in the [ATTACH] statement. +** For the main database file, the database name is "main". For TEMP +** tables, the database name is "temp".)^ +** +** ^If the flags parameter is non-zero, then the BLOB is opened for read +** and write access. ^If the flags parameter is zero, the BLOB is opened for +** read-only access. +** +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored +** in *ppBlob. Otherwise an [error code] is returned and, unless the error +** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided +** the API is not misused, it is always safe to call [sqlite3_blob_close()] +** on *ppBlob after this function it returns. +** +** This function fails with SQLITE_ERROR if any of the following are true: +**
    +**
  • ^(Database zDb does not exist)^, +**
  • ^(Table zTable does not exist within database zDb)^, +**
  • ^(Table zTable is a WITHOUT ROWID table)^, +**
  • ^(Column zColumn does not exist)^, +**
  • ^(Row iRow is not present in the table)^, +**
  • ^(The specified column of row iRow contains a value that is not +** a TEXT or BLOB value)^, +**
  • ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE +** constraint and the blob is being opened for read/write access)^, +**
  • ^([foreign key constraints | Foreign key constraints] are enabled, +** column zColumn is part of a [child key] definition and the blob is +** being opened for read/write access)^. +**
+** +** ^Unless it returns SQLITE_MISUSE, this function sets the +** [database connection] error code and message accessible via +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. +** +** +** ^(If the row that a BLOB handle points to is modified by an +** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects +** then the BLOB handle is marked as "expired". +** This is true if any column of the row is changed, even a column +** other than the one the BLOB handle is open on.)^ +** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for +** an expired BLOB handle fail with a return code of [SQLITE_ABORT]. +** ^(Changes written into a BLOB prior to the BLOB expiring are not +** rolled back by the expiration of the BLOB. Such changes will eventually +** commit if the transaction continues to completion.)^ +** +** ^Use the [sqlite3_blob_bytes()] interface to determine the size of +** the opened blob. ^The size of a blob may not be changed by this +** interface. Use the [UPDATE] SQL command to change the size of a +** blob. +** +** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces +** and the built-in [zeroblob] SQL function may be used to create a +** zero-filled blob to read or write using the incremental-blob interface. +** +** To avoid a resource leak, every open [BLOB handle] should eventually +** be released by a call to [sqlite3_blob_close()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_open( + sqlite3*, + const char *zDb, + const char *zTable, + const char *zColumn, + sqlite3_int64 iRow, + int flags, + sqlite3_blob **ppBlob +); + +/* +** CAPI3REF: Move a BLOB Handle to a New Row +** METHOD: sqlite3_blob +** +** ^This function is used to move an existing blob handle so that it points +** to a different row of the same database table. ^The new row is identified +** by the rowid value passed as the second argument. Only the row can be +** changed. ^The database, table and column on which the blob handle is open +** remain the same. Moving an existing blob handle to a new row can be +** faster than closing the existing handle and opening a new one. +** +** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - +** it must exist and there must be either a blob or text value stored in +** the nominated column.)^ ^If the new row is not present in the table, or if +** it does not contain a blob or text value, or if another error occurs, an +** SQLite error code is returned and the blob handle is considered aborted. +** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or +** [sqlite3_blob_reopen()] on an aborted blob handle immediately return +** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle +** always returns zero. +** +** ^This function sets the database handle error code and message. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); + +/* +** CAPI3REF: Close A BLOB Handle +** DESTRUCTOR: sqlite3_blob +** +** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed +** unconditionally. Even if this routine returns an error code, the +** handle is still closed.)^ +** +** ^If the blob handle being closed was opened for read-write access, and if +** the database is in auto-commit mode and there are no other open read-write +** blob handles or active write statements, the current transaction is +** committed. ^If an error occurs while committing the transaction, an error +** code is returned and the transaction rolled back. +** +** Calling this function with an argument that is not a NULL pointer or an +** open blob handle results in undefined behaviour. ^Calling this routine +** with a null pointer (such as would be returned by a failed call to +** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function +** is passed a valid open blob handle, the values returned by the +** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *); + +/* +** CAPI3REF: Return The Size Of An Open BLOB +** METHOD: sqlite3_blob +** +** ^Returns the size in bytes of the BLOB accessible via the +** successfully opened [BLOB handle] in its only argument. ^The +** incremental blob I/O routines can only read or overwriting existing +** blob content; they cannot change the size of a blob. +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *); + +/* +** CAPI3REF: Read Data From A BLOB Incrementally +** METHOD: sqlite3_blob +** +** ^(This function is used to read data from an open [BLOB handle] into a +** caller-supplied buffer. N bytes of data are copied into buffer Z +** from the open BLOB, starting at offset iOffset.)^ +** +** ^If offset iOffset is less than N bytes from the end of the BLOB, +** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is +** less than zero, [SQLITE_ERROR] is returned and no data is read. +** ^The size of the blob (and hence the maximum value of N+iOffset) +** can be determined using the [sqlite3_blob_bytes()] interface. +** +** ^An attempt to read from an expired [BLOB handle] fails with an +** error code of [SQLITE_ABORT]. +** +** ^(On success, sqlite3_blob_read() returns SQLITE_OK. +** Otherwise, an [error code] or an [extended error code] is returned.)^ +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +** +** See also: [sqlite3_blob_write()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); + +/* +** CAPI3REF: Write Data Into A BLOB Incrementally +** METHOD: sqlite3_blob +** +** ^(This function is used to write data into an open [BLOB handle] from a +** caller-supplied buffer. N bytes of data are copied from the buffer Z +** into the open BLOB, starting at offset iOffset.)^ +** +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. +** Otherwise, an [error code] or an [extended error code] is returned.)^ +** ^Unless SQLITE_MISUSE is returned, this function sets the +** [database connection] error code and message accessible via +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. +** +** ^If the [BLOB handle] passed as the first argument was not opened for +** writing (the flags parameter to [sqlite3_blob_open()] was zero), +** this function returns [SQLITE_READONLY]. +** +** This function may only modify the contents of the BLOB; it is +** not possible to increase the size of a BLOB using this API. +** ^If offset iOffset is less than N bytes from the end of the BLOB, +** [SQLITE_ERROR] is returned and no data is written. The size of the +** BLOB (and hence the maximum value of N+iOffset) can be determined +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less +** than zero [SQLITE_ERROR] is returned and no data is written. +** +** ^An attempt to write to an expired [BLOB handle] fails with an +** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred +** before the [BLOB handle] expired are not rolled back by the +** expiration of the handle, though of course those changes might +** have been overwritten by the statement that expired the BLOB handle +** or by other independent statements. +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +** +** See also: [sqlite3_blob_read()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); + +/* +** CAPI3REF: Virtual File System Objects +** +** A virtual filesystem (VFS) is an [sqlite3_vfs] object +** that SQLite uses to interact +** with the underlying operating system. Most SQLite builds come with a +** single default VFS that is appropriate for the host computer. +** New VFSes can be registered and existing VFSes can be unregistered. +** The following interfaces are provided. +** +** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name. +** ^Names are case sensitive. +** ^Names are zero-terminated UTF-8 strings. +** ^If there is no match, a NULL pointer is returned. +** ^If zVfsName is NULL then the default VFS is returned. +** +** ^New VFSes are registered with sqlite3_vfs_register(). +** ^Each new VFS becomes the default VFS if the makeDflt flag is set. +** ^The same VFS can be registered multiple times without injury. +** ^To make an existing VFS into the default VFS, register it again +** with the makeDflt flag set. If two different VFSes with the +** same name are registered, the behavior is undefined. If a +** VFS is registered with a name that is NULL or an empty string, +** then the behavior is undefined. +** +** ^Unregister a VFS with the sqlite3_vfs_unregister() interface. +** ^(If the default VFS is unregistered, another VFS is chosen as +** the default. The choice for the new VFS is arbitrary.)^ +*/ +SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfsName); +SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); +SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*); + +/* +** CAPI3REF: Mutexes +** +** The SQLite core uses these routines for thread +** synchronization. Though they are intended for internal +** use by SQLite, code that links against SQLite is +** permitted to use any of these routines. +** +** The SQLite source code contains multiple implementations +** of these mutex routines. An appropriate implementation +** is selected automatically at compile-time. The following +** implementations are available in the SQLite core: +** +**
    +**
  • SQLITE_MUTEX_PTHREADS +**
  • SQLITE_MUTEX_W32 +**
  • SQLITE_MUTEX_NOOP +**
+** +** The SQLITE_MUTEX_NOOP implementation is a set of routines +** that does no real locking and is appropriate for use in +** a single-threaded application. The SQLITE_MUTEX_PTHREADS and +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix +** and Windows. +** +** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor +** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex +** implementation is included with the library. In this case the +** application must supply a custom mutex implementation using the +** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function +** before calling sqlite3_initialize() or any other public sqlite3_ +** function that calls sqlite3_initialize(). +** +** ^The sqlite3_mutex_alloc() routine allocates a new +** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() +** routine returns NULL if it is unable to allocate the requested +** mutex. The argument to sqlite3_mutex_alloc() must one of these +** integer constants: +** +**
    +**
  • SQLITE_MUTEX_FAST +**
  • SQLITE_MUTEX_RECURSIVE +**
  • SQLITE_MUTEX_STATIC_MASTER +**
  • SQLITE_MUTEX_STATIC_MEM +**
  • SQLITE_MUTEX_STATIC_OPEN +**
  • SQLITE_MUTEX_STATIC_PRNG +**
  • SQLITE_MUTEX_STATIC_LRU +**
  • SQLITE_MUTEX_STATIC_PMEM +**
  • SQLITE_MUTEX_STATIC_APP1 +**
  • SQLITE_MUTEX_STATIC_APP2 +**
  • SQLITE_MUTEX_STATIC_APP3 +**
  • SQLITE_MUTEX_STATIC_VFS1 +**
  • SQLITE_MUTEX_STATIC_VFS2 +**
  • SQLITE_MUTEX_STATIC_VFS3 +**
+** +** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) +** cause sqlite3_mutex_alloc() to create +** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE +** is used but not necessarily so when SQLITE_MUTEX_FAST is used. +** The mutex implementation does not need to make a distinction +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does +** not want to. SQLite will only request a recursive mutex in +** cases where it really needs one. If a faster non-recursive mutex +** implementation is available on the host platform, the mutex subsystem +** might return such a mutex in response to SQLITE_MUTEX_FAST. +** +** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other +** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return +** a pointer to a static preexisting mutex. ^Nine static mutexes are +** used by the current version of SQLite. Future versions of SQLite +** may add additional static mutexes. Static mutexes are for internal +** use by SQLite only. Applications that use SQLite mutexes should +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or +** SQLITE_MUTEX_RECURSIVE. +** +** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() +** returns a different mutex on every call. ^For the static +** mutex types, the same mutex is returned on every call that has +** the same type number. +** +** ^The sqlite3_mutex_free() routine deallocates a previously +** allocated dynamic mutex. Attempting to deallocate a static +** mutex results in undefined behavior. +** +** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. ^If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] +** upon successful entry. ^(Mutexes created using +** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. +** In such cases, the +** mutex must be exited an equal number of times before another thread +** can enter.)^ If the same thread tries to enter any mutex other +** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. +** +** ^(Some systems (for example, Windows 95) do not support the operation +** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() +** will always return SQLITE_BUSY. The SQLite core only ever uses +** sqlite3_mutex_try() as an optimization so this is acceptable +** behavior.)^ +** +** ^The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. The behavior +** is undefined if the mutex is not currently entered by the +** calling thread or is not currently allocated. +** +** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or +** sqlite3_mutex_leave() is a NULL pointer, then all three routines +** behave as no-ops. +** +** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. +*/ +SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int); +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex*); +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex*); +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex*); +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex*); + +/* +** CAPI3REF: Mutex Methods Object +** +** An instance of this structure defines the low-level routines +** used to allocate and use mutexes. +** +** Usually, the default mutex implementations provided by SQLite are +** sufficient, however the application has the option of substituting a custom +** implementation for specialized deployments or systems for which SQLite +** does not provide a suitable implementation. In this case, the application +** creates and populates an instance of this structure to pass +** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. +** Additionally, an instance of this structure can be used as an +** output variable when querying the system for the current mutex +** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. +** +** ^The xMutexInit method defined by this structure is invoked as +** part of system initialization by the sqlite3_initialize() function. +** ^The xMutexInit routine is called by SQLite exactly once for each +** effective call to [sqlite3_initialize()]. +** +** ^The xMutexEnd method defined by this structure is invoked as +** part of system shutdown by the sqlite3_shutdown() function. The +** implementation of this method is expected to release all outstanding +** resources obtained by the mutex methods implementation, especially +** those obtained by the xMutexInit method. ^The xMutexEnd() +** interface is invoked exactly once for each call to [sqlite3_shutdown()]. +** +** ^(The remaining seven methods defined by this structure (xMutexAlloc, +** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and +** xMutexNotheld) implement the following interfaces (respectively): +** +**
    +**
  • [sqlite3_mutex_alloc()]
  • +**
  • [sqlite3_mutex_free()]
  • +**
  • [sqlite3_mutex_enter()]
  • +**
  • [sqlite3_mutex_try()]
  • +**
  • [sqlite3_mutex_leave()]
  • +**
  • [sqlite3_mutex_held()]
  • +**
  • [sqlite3_mutex_notheld()]
  • +**
)^ +** +** The only difference is that the public sqlite3_XXX functions enumerated +** above silently ignore any invocations that pass a NULL pointer instead +** of a valid mutex handle. The implementations of the methods defined +** by this structure are not required to handle this case, the results +** of passing a NULL pointer instead of a valid mutex handle are undefined +** (i.e. it is acceptable to provide an implementation that segfaults if +** it is passed a NULL pointer). +** +** The xMutexInit() method must be threadsafe. It must be harmless to +** invoke xMutexInit() multiple times within the same process and without +** intervening calls to xMutexEnd(). Second and subsequent calls to +** xMutexInit() must be no-ops. +** +** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] +** and its associates). Similarly, xMutexAlloc() must not use SQLite memory +** allocation for a static mutex. ^However xMutexAlloc() may use SQLite +** memory allocation for a fast or recursive mutex. +** +** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is +** called, but only if the prior call to xMutexInit returned SQLITE_OK. +** If xMutexInit fails in any way, it is expected to clean up after itself +** prior to returning. +*/ +typedef struct sqlite3_mutex_methods sqlite3_mutex_methods; +struct sqlite3_mutex_methods { + int (*xMutexInit)(void); + int (*xMutexEnd)(void); + sqlite3_mutex *(*xMutexAlloc)(int); + void (*xMutexFree)(sqlite3_mutex *); + void (*xMutexEnter)(sqlite3_mutex *); + int (*xMutexTry)(sqlite3_mutex *); + void (*xMutexLeave)(sqlite3_mutex *); + int (*xMutexHeld)(sqlite3_mutex *); + int (*xMutexNotheld)(sqlite3_mutex *); +}; + +/* +** CAPI3REF: Mutex Verification Routines +** +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines +** are intended for use inside assert() statements. The SQLite core +** never uses these routines except inside an assert() and applications +** are advised to follow the lead of the core. The SQLite core only +** provides implementations for these routines when it is compiled +** with the SQLITE_DEBUG flag. External mutex implementations +** are only required to provide these routines if SQLITE_DEBUG is +** defined and if NDEBUG is not defined. +** +** These routines should return true if the mutex in their argument +** is held or not held, respectively, by the calling thread. +** +** The implementation is not required to provide versions of these +** routines that actually work. If the implementation does not provide working +** versions of these routines, it should at least provide stubs that always +** return true so that one does not get spurious assertion failures. +** +** If the argument to sqlite3_mutex_held() is a NULL pointer then +** the routine should return 1. This seems counter-intuitive since +** clearly the mutex cannot be held if it does not exist. But +** the reason the mutex does not exist is because the build is not +** using mutexes. And we do not want the assert() containing the +** call to sqlite3_mutex_held() to fail, so a non-zero return is +** the appropriate thing to do. The sqlite3_mutex_notheld() +** interface should also return 1 when given a NULL pointer. +*/ +#ifndef NDEBUG +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex*); +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*); +#endif + +/* +** CAPI3REF: Mutex Types +** +** The [sqlite3_mutex_alloc()] interface takes a single argument +** which is one of these integer constants. +** +** The set of static mutexes may change from one SQLite release to the +** next. Applications that override the built-in mutex logic must be +** prepared to accommodate additional static mutexes. +*/ +#define SQLITE_MUTEX_FAST 0 +#define SQLITE_MUTEX_RECURSIVE 1 +#define SQLITE_MUTEX_STATIC_MASTER 2 +#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ +#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ +#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ +#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ +#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ +#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ +#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ +#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */ +#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */ +#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */ +#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */ +#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */ +#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */ + +/* +** CAPI3REF: Retrieve the mutex for a database connection +** METHOD: sqlite3 +** +** ^This interface returns a pointer the [sqlite3_mutex] object that +** serializes access to the [database connection] given in the argument +** when the [threading mode] is Serialized. +** ^If the [threading mode] is Single-thread or Multi-thread then this +** routine returns a NULL pointer. +*/ +SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*); + +/* +** CAPI3REF: Low-Level Control Of Database Files +** METHOD: sqlite3 +** +** ^The [sqlite3_file_control()] interface makes a direct call to the +** xFileControl method for the [sqlite3_io_methods] object associated +** with a particular database identified by the second argument. ^The +** name of the database is "main" for the main database or "temp" for the +** TEMP database, or the name that appears after the AS keyword for +** databases that are added using the [ATTACH] SQL command. +** ^A NULL pointer can be used in place of "main" to refer to the +** main database file. +** ^The third and fourth parameters to this routine +** are passed directly through to the second and third parameters of +** the xFileControl method. ^The return value of the xFileControl +** method becomes the return value of this routine. +** +** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes +** a pointer to the underlying [sqlite3_file] object to be written into +** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER +** case is a short-circuit path which does not actually invoke the +** underlying sqlite3_io_methods.xFileControl method. +** +** ^If the second parameter (zDbName) does not match the name of any +** open database file, then SQLITE_ERROR is returned. ^This error +** code is not remembered and will not be recalled by [sqlite3_errcode()] +** or [sqlite3_errmsg()]. The underlying xFileControl method might +** also return SQLITE_ERROR. There is no way to distinguish between +** an incorrect zDbName and an SQLITE_ERROR return from the underlying +** xFileControl method. +** +** See also: [SQLITE_FCNTL_LOCKSTATE] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); + +/* +** CAPI3REF: Testing Interface +** +** ^The sqlite3_test_control() interface is used to read out internal +** state of SQLite and to inject faults into SQLite for testing +** purposes. ^The first parameter is an operation code that determines +** the number, meaning, and operation of all subsequent parameters. +** +** This interface is not for use by applications. It exists solely +** for verifying the correct operation of the SQLite library. Depending +** on how the SQLite library is compiled, this interface might not exist. +** +** The details of the operation codes, their meanings, the parameters +** they take, and what they do are all subject to change without notice. +** Unlike most of the SQLite API, this function is not guaranteed to +** operate consistently from one release to the next. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...); + +/* +** CAPI3REF: Testing Interface Operation Codes +** +** These constants are the valid operation code parameters used +** as the first argument to [sqlite3_test_control()]. +** +** These parameters and their meanings are subject to change +** without notice. These values are for testing purposes only. +** Applications should not use any of these parameters or the +** [sqlite3_test_control()] interface. +*/ +#define SQLITE_TESTCTRL_FIRST 5 +#define SQLITE_TESTCTRL_PRNG_SAVE 5 +#define SQLITE_TESTCTRL_PRNG_RESTORE 6 +#define SQLITE_TESTCTRL_PRNG_RESET 7 +#define SQLITE_TESTCTRL_BITVEC_TEST 8 +#define SQLITE_TESTCTRL_FAULT_INSTALL 9 +#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 +#define SQLITE_TESTCTRL_PENDING_BYTE 11 +#define SQLITE_TESTCTRL_ASSERT 12 +#define SQLITE_TESTCTRL_ALWAYS 13 +#define SQLITE_TESTCTRL_RESERVE 14 +#define SQLITE_TESTCTRL_OPTIMIZATIONS 15 +#define SQLITE_TESTCTRL_ISKEYWORD 16 +#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 +#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 +#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ +#define SQLITE_TESTCTRL_NEVER_CORRUPT 20 +#define SQLITE_TESTCTRL_VDBE_COVERAGE 21 +#define SQLITE_TESTCTRL_BYTEORDER 22 +#define SQLITE_TESTCTRL_ISINIT 23 +#define SQLITE_TESTCTRL_SORTER_MMAP 24 +#define SQLITE_TESTCTRL_IMPOSTER 25 +#define SQLITE_TESTCTRL_LAST 25 + +/* +** CAPI3REF: SQLite Runtime Status +** +** ^These interfaces are used to retrieve runtime status information +** about the performance of SQLite, and optionally to reset various +** highwater marks. ^The first argument is an integer code for +** the specific parameter to measure. ^(Recognized integer codes +** are of the form [status parameters | SQLITE_STATUS_...].)^ +** ^The current value of the parameter is returned into *pCurrent. +** ^The highest recorded value is returned in *pHighwater. ^If the +** resetFlag is true, then the highest record value is reset after +** *pHighwater is written. ^(Some parameters do not record the highest +** value. For those parameters +** nothing is written into *pHighwater and the resetFlag is ignored.)^ +** ^(Other parameters record only the highwater mark and not the current +** value. For these latter parameters nothing is written into *pCurrent.)^ +** +** ^The sqlite3_status() and sqlite3_status64() routines return +** SQLITE_OK on success and a non-zero [error code] on failure. +** +** If either the current value or the highwater mark is too large to +** be represented by a 32-bit integer, then the values returned by +** sqlite3_status() are undefined. +** +** See also: [sqlite3_db_status()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); +SQLITE_API int SQLITE_STDCALL sqlite3_status64( + int op, + sqlite3_int64 *pCurrent, + sqlite3_int64 *pHighwater, + int resetFlag +); + + +/* +** CAPI3REF: Status Parameters +** KEYWORDS: {status parameters} +** +** These integer constants designate various run-time status parameters +** that can be returned by [sqlite3_status()]. +** +**
+** [[SQLITE_STATUS_MEMORY_USED]] ^(
SQLITE_STATUS_MEMORY_USED
+**
This parameter is the current amount of memory checked out +** using [sqlite3_malloc()], either directly or indirectly. The +** figure includes calls made to [sqlite3_malloc()] by the application +** and internal memory usage by the SQLite library. Scratch memory +** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache +** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in +** this parameter. The amount returned is the sum of the allocation +** sizes as reported by the xSize method in [sqlite3_mem_methods].
)^ +** +** [[SQLITE_STATUS_MALLOC_SIZE]] ^(
SQLITE_STATUS_MALLOC_SIZE
+**
This parameter records the largest memory allocation request +** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their +** internal equivalents). Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
)^ +** +** [[SQLITE_STATUS_MALLOC_COUNT]] ^(
SQLITE_STATUS_MALLOC_COUNT
+**
This parameter records the number of separate memory allocations +** currently checked out.
)^ +** +** [[SQLITE_STATUS_PAGECACHE_USED]] ^(
SQLITE_STATUS_PAGECACHE_USED
+**
This parameter returns the number of pages used out of the +** [pagecache memory allocator] that was configured using +** [SQLITE_CONFIG_PAGECACHE]. The +** value returned is in pages, not in bytes.
)^ +** +** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] +** ^(
SQLITE_STATUS_PAGECACHE_OVERFLOW
+**
This parameter returns the number of bytes of page cache +** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] +** buffer and where forced to overflow to [sqlite3_malloc()]. The +** returned value includes allocations that overflowed because they +** where too large (they were larger than the "sz" parameter to +** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because +** no space was left in the page cache.
)^ +** +** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(
SQLITE_STATUS_PAGECACHE_SIZE
+**
This parameter records the largest memory allocation request +** handed to [pagecache memory allocator]. Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
)^ +** +** [[SQLITE_STATUS_SCRATCH_USED]] ^(
SQLITE_STATUS_SCRATCH_USED
+**
This parameter returns the number of allocations used out of the +** [scratch memory allocator] configured using +** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not +** in bytes. Since a single thread may only have one scratch allocation +** outstanding at time, this parameter also reports the number of threads +** using scratch memory at the same time.
)^ +** +** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(
SQLITE_STATUS_SCRATCH_OVERFLOW
+**
This parameter returns the number of bytes of scratch memory +** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] +** buffer and where forced to overflow to [sqlite3_malloc()]. The values +** returned include overflows because the requested allocation was too +** larger (that is, because the requested allocation was larger than the +** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer +** slots were available. +**
)^ +** +** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(
SQLITE_STATUS_SCRATCH_SIZE
+**
This parameter records the largest memory allocation request +** handed to [scratch memory allocator]. Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
)^ +** +** [[SQLITE_STATUS_PARSER_STACK]] ^(
SQLITE_STATUS_PARSER_STACK
+**
The *pHighwater parameter records the deepest parser stack. +** The *pCurrent value is undefined. The *pHighwater value is only +** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].
)^ +**
+** +** New status parameters may be added from time to time. +*/ +#define SQLITE_STATUS_MEMORY_USED 0 +#define SQLITE_STATUS_PAGECACHE_USED 1 +#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 +#define SQLITE_STATUS_SCRATCH_USED 3 +#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 +#define SQLITE_STATUS_MALLOC_SIZE 5 +#define SQLITE_STATUS_PARSER_STACK 6 +#define SQLITE_STATUS_PAGECACHE_SIZE 7 +#define SQLITE_STATUS_SCRATCH_SIZE 8 +#define SQLITE_STATUS_MALLOC_COUNT 9 + +/* +** CAPI3REF: Database Connection Status +** METHOD: sqlite3 +** +** ^This interface is used to retrieve runtime status information +** about a single [database connection]. ^The first argument is the +** database connection object to be interrogated. ^The second argument +** is an integer constant, taken from the set of +** [SQLITE_DBSTATUS options], that +** determines the parameter to interrogate. The set of +** [SQLITE_DBSTATUS options] is likely +** to grow in future releases of SQLite. +** +** ^The current value of the requested parameter is written into *pCur +** and the highest instantaneous value is written into *pHiwtr. ^If +** the resetFlg is true, then the highest instantaneous value is +** reset back down to the current value. +** +** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a +** non-zero [error code] on failure. +** +** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); + +/* +** CAPI3REF: Status Parameters for database connections +** KEYWORDS: {SQLITE_DBSTATUS options} +** +** These constants are the available integer "verbs" that can be passed as +** the second argument to the [sqlite3_db_status()] interface. +** +** New verbs may be added in future releases of SQLite. Existing verbs +** might be discontinued. Applications should check the return code from +** [sqlite3_db_status()] to make sure that the call worked. +** The [sqlite3_db_status()] interface will return a non-zero error code +** if a discontinued or unsupported verb is invoked. +** +**
+** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(
SQLITE_DBSTATUS_LOOKASIDE_USED
+**
This parameter returns the number of lookaside memory slots currently +** checked out.
)^ +** +** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(
SQLITE_DBSTATUS_LOOKASIDE_HIT
+**
This parameter returns the number malloc attempts that were +** satisfied using lookaside memory. Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]] +** ^(
SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
+**
This parameter returns the number malloc attempts that might have +** been satisfied using lookaside memory but failed due to the amount of +** memory requested being larger than the lookaside slot size. +** Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]] +** ^(
SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
+**
This parameter returns the number malloc attempts that might have +** been satisfied using lookaside memory but failed due to all lookaside +** memory already being in use. +** Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** [[SQLITE_DBSTATUS_CACHE_USED]] ^(
SQLITE_DBSTATUS_CACHE_USED
+**
This parameter returns the approximate number of bytes of heap +** memory used by all pager caches associated with the database connection.)^ +** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. +** +** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(
SQLITE_DBSTATUS_SCHEMA_USED
+**
This parameter returns the approximate number of bytes of heap +** memory used to store the schema for all databases associated +** with the connection - main, temp, and any [ATTACH]-ed databases.)^ +** ^The full amount of memory used by the schemas is reported, even if the +** schema memory is shared with other database connections due to +** [shared cache mode] being enabled. +** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. +** +** [[SQLITE_DBSTATUS_STMT_USED]] ^(
SQLITE_DBSTATUS_STMT_USED
+**
This parameter returns the approximate number of bytes of heap +** and lookaside memory used by all prepared statements associated with +** the database connection.)^ +** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. +**
+** +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(
SQLITE_DBSTATUS_CACHE_HIT
+**
This parameter returns the number of pager cache hits that have +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT +** is always 0. +**
+** +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(
SQLITE_DBSTATUS_CACHE_MISS
+**
This parameter returns the number of pager cache misses that have +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS +** is always 0. +**
+** +** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(
SQLITE_DBSTATUS_CACHE_WRITE
+**
This parameter returns the number of dirty cache entries that have +** been written to disk. Specifically, the number of pages written to the +** wal file in wal mode databases, or the number of pages written to the +** database file in rollback mode databases. Any pages written as part of +** transaction rollback or database recovery operations are not included. +** If an IO or other error occurs while writing a page to disk, the effect +** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The +** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. +**
+** +** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(
SQLITE_DBSTATUS_DEFERRED_FKS
+**
This parameter returns zero for the current value if and only if +** all foreign key constraints (deferred or immediate) have been +** resolved.)^ ^The highwater mark is always 0. +**
+**
+*/ +#define SQLITE_DBSTATUS_LOOKASIDE_USED 0 +#define SQLITE_DBSTATUS_CACHE_USED 1 +#define SQLITE_DBSTATUS_SCHEMA_USED 2 +#define SQLITE_DBSTATUS_STMT_USED 3 +#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 +#define SQLITE_DBSTATUS_CACHE_HIT 7 +#define SQLITE_DBSTATUS_CACHE_MISS 8 +#define SQLITE_DBSTATUS_CACHE_WRITE 9 +#define SQLITE_DBSTATUS_DEFERRED_FKS 10 +#define SQLITE_DBSTATUS_MAX 10 /* Largest defined DBSTATUS */ + + +/* +** CAPI3REF: Prepared Statement Status +** METHOD: sqlite3_stmt +** +** ^(Each prepared statement maintains various +** [SQLITE_STMTSTATUS counters] that measure the number +** of times it has performed specific operations.)^ These counters can +** be used to monitor the performance characteristics of the prepared +** statements. For example, if the number of table steps greatly exceeds +** the number of table searches or result rows, that would tend to indicate +** that the prepared statement is using a full table scan rather than +** an index. +** +** ^(This interface is used to retrieve and reset counter values from +** a [prepared statement]. The first argument is the prepared statement +** object to be interrogated. The second argument +** is an integer code for a specific [SQLITE_STMTSTATUS counter] +** to be interrogated.)^ +** ^The current value of the requested counter is returned. +** ^If the resetFlg is true, then the counter is reset to zero after this +** interface call returns. +** +** See also: [sqlite3_status()] and [sqlite3_db_status()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); + +/* +** CAPI3REF: Status Parameters for prepared statements +** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters} +** +** These preprocessor macros define integer codes that name counter +** values associated with the [sqlite3_stmt_status()] interface. +** The meanings of the various counters are as follows: +** +**
+** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]]
SQLITE_STMTSTATUS_FULLSCAN_STEP
+**
^This is the number of times that SQLite has stepped forward in +** a table as part of a full table scan. Large numbers for this counter +** may indicate opportunities for performance improvement through +** careful use of indices.
+** +** [[SQLITE_STMTSTATUS_SORT]]
SQLITE_STMTSTATUS_SORT
+**
^This is the number of sort operations that have occurred. +** A non-zero value in this counter may indicate an opportunity to +** improvement performance through careful use of indices.
+** +** [[SQLITE_STMTSTATUS_AUTOINDEX]]
SQLITE_STMTSTATUS_AUTOINDEX
+**
^This is the number of rows inserted into transient indices that +** were created automatically in order to help joins run faster. +** A non-zero value in this counter may indicate an opportunity to +** improvement performance by adding permanent indices that do not +** need to be reinitialized each time the statement is run.
+** +** [[SQLITE_STMTSTATUS_VM_STEP]]
SQLITE_STMTSTATUS_VM_STEP
+**
^This is the number of virtual machine operations executed +** by the prepared statement if that number is less than or equal +** to 2147483647. The number of virtual machine operations can be +** used as a proxy for the total work done by the prepared statement. +** If the number of virtual machine operations exceeds 2147483647 +** then the value returned by this statement status code is undefined. +**
+**
+*/ +#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 +#define SQLITE_STMTSTATUS_SORT 2 +#define SQLITE_STMTSTATUS_AUTOINDEX 3 +#define SQLITE_STMTSTATUS_VM_STEP 4 + +/* +** CAPI3REF: Custom Page Cache Object +** +** The sqlite3_pcache type is opaque. It is implemented by +** the pluggable module. The SQLite core has no knowledge of +** its size or internal structure and never deals with the +** sqlite3_pcache object except by holding and passing pointers +** to the object. +** +** See [sqlite3_pcache_methods2] for additional information. +*/ +typedef struct sqlite3_pcache sqlite3_pcache; + +/* +** CAPI3REF: Custom Page Cache Object +** +** The sqlite3_pcache_page object represents a single page in the +** page cache. The page cache will allocate instances of this +** object. Various methods of the page cache use pointers to instances +** of this object as parameters or as their return value. +** +** See [sqlite3_pcache_methods2] for additional information. +*/ +typedef struct sqlite3_pcache_page sqlite3_pcache_page; +struct sqlite3_pcache_page { + void *pBuf; /* The content of the page */ + void *pExtra; /* Extra information associated with the page */ +}; + +/* +** CAPI3REF: Application Defined Page Cache. +** KEYWORDS: {page cache} +** +** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can +** register an alternative page cache implementation by passing in an +** instance of the sqlite3_pcache_methods2 structure.)^ +** In many applications, most of the heap memory allocated by +** SQLite is used for the page cache. +** By implementing a +** custom page cache using this API, an application can better control +** the amount of memory consumed by SQLite, the way in which +** that memory is allocated and released, and the policies used to +** determine exactly which parts of a database file are cached and for +** how long. +** +** The alternative page cache mechanism is an +** extreme measure that is only needed by the most demanding applications. +** The built-in page cache is recommended for most uses. +** +** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an +** internal buffer by SQLite within the call to [sqlite3_config]. Hence +** the application may discard the parameter after the call to +** [sqlite3_config()] returns.)^ +** +** [[the xInit() page cache method]] +** ^(The xInit() method is called once for each effective +** call to [sqlite3_initialize()])^ +** (usually only once during the lifetime of the process). ^(The xInit() +** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^ +** The intent of the xInit() method is to set up global data structures +** required by the custom page cache implementation. +** ^(If the xInit() method is NULL, then the +** built-in default page cache is used instead of the application defined +** page cache.)^ +** +** [[the xShutdown() page cache method]] +** ^The xShutdown() method is called by [sqlite3_shutdown()]. +** It can be used to clean up +** any outstanding resources before process shutdown, if required. +** ^The xShutdown() method may be NULL. +** +** ^SQLite automatically serializes calls to the xInit method, +** so the xInit method need not be threadsafe. ^The +** xShutdown method is only called from [sqlite3_shutdown()] so it does +** not need to be threadsafe either. All other methods must be threadsafe +** in multithreaded applications. +** +** ^SQLite will never invoke xInit() more than once without an intervening +** call to xShutdown(). +** +** [[the xCreate() page cache methods]] +** ^SQLite invokes the xCreate() method to construct a new cache instance. +** SQLite will typically create one cache instance for each open database file, +** though this is not guaranteed. ^The +** first parameter, szPage, is the size in bytes of the pages that must +** be allocated by the cache. ^szPage will always a power of two. ^The +** second parameter szExtra is a number of bytes of extra storage +** associated with each page cache entry. ^The szExtra parameter will +** a number less than 250. SQLite will use the +** extra szExtra bytes on each page to store metadata about the underlying +** database page on disk. The value passed into szExtra depends +** on the SQLite version, the target platform, and how SQLite was compiled. +** ^The third argument to xCreate(), bPurgeable, is true if the cache being +** created will be used to cache database pages of a file stored on disk, or +** false if it is used for an in-memory database. The cache implementation +** does not have to do anything special based with the value of bPurgeable; +** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will +** never invoke xUnpin() except to deliberately delete a page. +** ^In other words, calls to xUnpin() on a cache with bPurgeable set to +** false will always have the "discard" flag set to true. +** ^Hence, a cache created with bPurgeable false will +** never contain any unpinned pages. +** +** [[the xCachesize() page cache method]] +** ^(The xCachesize() method may be called at any time by SQLite to set the +** suggested maximum cache-size (number of pages stored by) the cache +** instance passed as the first argument. This is the value configured using +** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable +** parameter, the implementation is not required to do anything with this +** value; it is advisory only. +** +** [[the xPagecount() page cache methods]] +** The xPagecount() method must return the number of pages currently +** stored in the cache, both pinned and unpinned. +** +** [[the xFetch() page cache methods]] +** The xFetch() method locates a page in the cache and returns a pointer to +** an sqlite3_pcache_page object associated with that page, or a NULL pointer. +** The pBuf element of the returned sqlite3_pcache_page object will be a +** pointer to a buffer of szPage bytes used to store the content of a +** single database page. The pExtra element of sqlite3_pcache_page will be +** a pointer to the szExtra bytes of extra storage that SQLite has requested +** for each entry in the page cache. +** +** The page to be fetched is determined by the key. ^The minimum key value +** is 1. After it has been retrieved using xFetch, the page is considered +** to be "pinned". +** +** If the requested page is already in the page cache, then the page cache +** implementation must return a pointer to the page buffer with its content +** intact. If the requested page is not already in the cache, then the +** cache implementation should use the value of the createFlag +** parameter to help it determined what action to take: +** +** +**
createFlag Behavior when page is not already in cache +**
0 Do not allocate a new page. Return NULL. +**
1 Allocate a new page if it easy and convenient to do so. +** Otherwise return NULL. +**
2 Make every effort to allocate a new page. Only return +** NULL if allocating a new page is effectively impossible. +**
+** +** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite +** will only use a createFlag of 2 after a prior call with a createFlag of 1 +** failed.)^ In between the to xFetch() calls, SQLite may +** attempt to unpin one or more cache pages by spilling the content of +** pinned pages to disk and synching the operating system disk cache. +** +** [[the xUnpin() page cache method]] +** ^xUnpin() is called by SQLite with a pointer to a currently pinned page +** as its second argument. If the third parameter, discard, is non-zero, +** then the page must be evicted from the cache. +** ^If the discard parameter is +** zero, then the page may be discarded or retained at the discretion of +** page cache implementation. ^The page cache implementation +** may choose to evict unpinned pages at any time. +** +** The cache must not perform any reference counting. A single +** call to xUnpin() unpins the page regardless of the number of prior calls +** to xFetch(). +** +** [[the xRekey() page cache methods]] +** The xRekey() method is used to change the key value associated with the +** page passed as the second argument. If the cache +** previously contains an entry associated with newKey, it must be +** discarded. ^Any prior cache entry associated with newKey is guaranteed not +** to be pinned. +** +** When SQLite calls the xTruncate() method, the cache must discard all +** existing cache entries with page numbers (keys) greater than or equal +** to the value of the iLimit parameter passed to xTruncate(). If any +** of these pages are pinned, they are implicitly unpinned, meaning that +** they can be safely discarded. +** +** [[the xDestroy() page cache method]] +** ^The xDestroy() method is used to delete a cache allocated by xCreate(). +** All resources associated with the specified cache should be freed. ^After +** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*] +** handle invalid, and will not use it with any other sqlite3_pcache_methods2 +** functions. +** +** [[the xShrink() page cache method]] +** ^SQLite invokes the xShrink() method when it wants the page cache to +** free up as much of heap memory as possible. The page cache implementation +** is not obligated to free any memory, but well-behaved implementations should +** do their best. +*/ +typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; +struct sqlite3_pcache_methods2 { + int iVersion; + void *pArg; + int (*xInit)(void*); + void (*xShutdown)(void*); + sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable); + void (*xCachesize)(sqlite3_pcache*, int nCachesize); + int (*xPagecount)(sqlite3_pcache*); + sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); + void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard); + void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, + unsigned oldKey, unsigned newKey); + void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); + void (*xDestroy)(sqlite3_pcache*); + void (*xShrink)(sqlite3_pcache*); +}; + +/* +** This is the obsolete pcache_methods object that has now been replaced +** by sqlite3_pcache_methods2. This object is not used by SQLite. It is +** retained in the header file for backwards compatibility only. +*/ +typedef struct sqlite3_pcache_methods sqlite3_pcache_methods; +struct sqlite3_pcache_methods { + void *pArg; + int (*xInit)(void*); + void (*xShutdown)(void*); + sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); + void (*xCachesize)(sqlite3_pcache*, int nCachesize); + int (*xPagecount)(sqlite3_pcache*); + void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); + void (*xUnpin)(sqlite3_pcache*, void*, int discard); + void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); + void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); + void (*xDestroy)(sqlite3_pcache*); +}; + + +/* +** CAPI3REF: Online Backup Object +** +** The sqlite3_backup object records state information about an ongoing +** online backup operation. ^The sqlite3_backup object is created by +** a call to [sqlite3_backup_init()] and is destroyed by a call to +** [sqlite3_backup_finish()]. +** +** See Also: [Using the SQLite Online Backup API] +*/ +typedef struct sqlite3_backup sqlite3_backup; + +/* +** CAPI3REF: Online Backup API. +** +** The backup API copies the content of one database into another. +** It is useful either for creating backups of databases or +** for copying in-memory databases to or from persistent files. +** +** See Also: [Using the SQLite Online Backup API] +** +** ^SQLite holds a write transaction open on the destination database file +** for the duration of the backup operation. +** ^The source database is read-locked only while it is being read; +** it is not locked continuously for the entire backup operation. +** ^Thus, the backup may be performed on a live source database without +** preventing other database connections from +** reading or writing to the source database while the backup is underway. +** +** ^(To perform a backup operation: +**
    +**
  1. sqlite3_backup_init() is called once to initialize the +** backup, +**
  2. sqlite3_backup_step() is called one or more times to transfer +** the data between the two databases, and finally +**
  3. sqlite3_backup_finish() is called to release all resources +** associated with the backup operation. +**
)^ +** There should be exactly one call to sqlite3_backup_finish() for each +** successful call to sqlite3_backup_init(). +** +** [[sqlite3_backup_init()]] sqlite3_backup_init() +** +** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the +** [database connection] associated with the destination database +** and the database name, respectively. +** ^The database name is "main" for the main database, "temp" for the +** temporary database, or the name specified after the AS keyword in +** an [ATTACH] statement for an attached database. +** ^The S and M arguments passed to +** sqlite3_backup_init(D,N,S,M) identify the [database connection] +** and database name of the source database, respectively. +** ^The source and destination [database connections] (parameters S and D) +** must be different or else sqlite3_backup_init(D,N,S,M) will fail with +** an error. +** +** ^A call to sqlite3_backup_init() will fail, returning NULL, if +** there is already a read or read-write transaction open on the +** destination database. +** +** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is +** returned and an error code and error message are stored in the +** destination [database connection] D. +** ^The error code and message for the failed call to sqlite3_backup_init() +** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or +** [sqlite3_errmsg16()] functions. +** ^A successful call to sqlite3_backup_init() returns a pointer to an +** [sqlite3_backup] object. +** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and +** sqlite3_backup_finish() functions to perform the specified backup +** operation. +** +** [[sqlite3_backup_step()]] sqlite3_backup_step() +** +** ^Function sqlite3_backup_step(B,N) will copy up to N pages between +** the source and destination databases specified by [sqlite3_backup] object B. +** ^If N is negative, all remaining source pages are copied. +** ^If sqlite3_backup_step(B,N) successfully copies N pages and there +** are still more pages to be copied, then the function returns [SQLITE_OK]. +** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages +** from source to destination, then it returns [SQLITE_DONE]. +** ^If an error occurs while running sqlite3_backup_step(B,N), +** then an [error code] is returned. ^As well as [SQLITE_OK] and +** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY], +** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code. +** +** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if +**
    +**
  1. the destination database was opened read-only, or +**
  2. the destination database is using write-ahead-log journaling +** and the destination and source page sizes differ, or +**
  3. the destination database is an in-memory database and the +** destination and source page sizes differ. +**
)^ +** +** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then +** the [sqlite3_busy_handler | busy-handler function] +** is invoked (if one is specified). ^If the +** busy-handler returns non-zero before the lock is available, then +** [SQLITE_BUSY] is returned to the caller. ^In this case the call to +** sqlite3_backup_step() can be retried later. ^If the source +** [database connection] +** is being used to write to the source database when sqlite3_backup_step() +** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this +** case the call to sqlite3_backup_step() can be retried later on. ^(If +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or +** [SQLITE_READONLY] is returned, then +** there is no point in retrying the call to sqlite3_backup_step(). These +** errors are considered fatal.)^ The application must accept +** that the backup operation has failed and pass the backup operation handle +** to the sqlite3_backup_finish() to release associated resources. +** +** ^The first call to sqlite3_backup_step() obtains an exclusive lock +** on the destination file. ^The exclusive lock is not released until either +** sqlite3_backup_finish() is called or the backup operation is complete +** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to +** sqlite3_backup_step() obtains a [shared lock] on the source database that +** lasts for the duration of the sqlite3_backup_step() call. +** ^Because the source database is not locked between calls to +** sqlite3_backup_step(), the source database may be modified mid-way +** through the backup process. ^If the source database is modified by an +** external process or via a database connection other than the one being +** used by the backup operation, then the backup will be automatically +** restarted by the next call to sqlite3_backup_step(). ^If the source +** database is modified by the using the same database connection as is used +** by the backup operation, then the backup database is automatically +** updated at the same time. +** +** [[sqlite3_backup_finish()]] sqlite3_backup_finish() +** +** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the +** application wishes to abandon the backup operation, the application +** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish(). +** ^The sqlite3_backup_finish() interfaces releases all +** resources associated with the [sqlite3_backup] object. +** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any +** active write-transaction on the destination database is rolled back. +** The [sqlite3_backup] object is invalid +** and may not be used following a call to sqlite3_backup_finish(). +** +** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no +** sqlite3_backup_step() errors occurred, regardless or whether or not +** sqlite3_backup_step() completed. +** ^If an out-of-memory condition or IO error occurred during any prior +** sqlite3_backup_step() call on the same [sqlite3_backup] object, then +** sqlite3_backup_finish() returns the corresponding [error code]. +** +** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() +** is not a permanent error and does not affect the return value of +** sqlite3_backup_finish(). +** +** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]] +** sqlite3_backup_remaining() and sqlite3_backup_pagecount() +** +** ^The sqlite3_backup_remaining() routine returns the number of pages still +** to be backed up at the conclusion of the most recent sqlite3_backup_step(). +** ^The sqlite3_backup_pagecount() routine returns the total number of pages +** in the source database at the conclusion of the most recent +** sqlite3_backup_step(). +** ^(The values returned by these functions are only updated by +** sqlite3_backup_step(). If the source database is modified in a way that +** changes the size of the source database or the number of pages remaining, +** those changes are not reflected in the output of sqlite3_backup_pagecount() +** and sqlite3_backup_remaining() until after the next +** sqlite3_backup_step().)^ +** +** Concurrent Usage of Database Handles +** +** ^The source [database connection] may be used by the application for other +** purposes while a backup operation is underway or being initialized. +** ^If SQLite is compiled and configured to support threadsafe database +** connections, then the source database connection may be used concurrently +** from within other threads. +** +** However, the application must guarantee that the destination +** [database connection] is not passed to any other API (by any thread) after +** sqlite3_backup_init() is called and before the corresponding call to +** sqlite3_backup_finish(). SQLite does not currently check to see +** if the application incorrectly accesses the destination [database connection] +** and so no error code is reported, but the operations may malfunction +** nevertheless. Use of the destination database connection while a +** backup is in progress might also also cause a mutex deadlock. +** +** If running in [shared cache mode], the application must +** guarantee that the shared cache used by the destination database +** is not accessed while the backup is running. In practice this means +** that the application must guarantee that the disk file being +** backed up to is not accessed by any connection within the process, +** not just the specific connection that was passed to sqlite3_backup_init(). +** +** The [sqlite3_backup] object itself is partially threadsafe. Multiple +** threads may safely make multiple concurrent calls to sqlite3_backup_step(). +** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() +** APIs are not strictly speaking threadsafe. If they are invoked at the +** same time as another thread is invoking sqlite3_backup_step() it is +** possible that they return invalid values. +*/ +SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init( + sqlite3 *pDest, /* Destination database handle */ + const char *zDestName, /* Destination database name */ + sqlite3 *pSource, /* Source database handle */ + const char *zSourceName /* Source database name */ +); +SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage); +SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p); +SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p); +SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p); + +/* +** CAPI3REF: Unlock Notification +** METHOD: sqlite3 +** +** ^When running in shared-cache mode, a database operation may fail with +** an [SQLITE_LOCKED] error if the required locks on the shared-cache or +** individual tables within the shared-cache cannot be obtained. See +** [SQLite Shared-Cache Mode] for a description of shared-cache locking. +** ^This API may be used to register a callback that SQLite will invoke +** when the connection currently holding the required lock relinquishes it. +** ^This API is only available if the library was compiled with the +** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined. +** +** See Also: [Using the SQLite Unlock Notification Feature]. +** +** ^Shared-cache locks are released when a database connection concludes +** its current transaction, either by committing it or rolling it back. +** +** ^When a connection (known as the blocked connection) fails to obtain a +** shared-cache lock and SQLITE_LOCKED is returned to the caller, the +** identity of the database connection (the blocking connection) that +** has locked the required resource is stored internally. ^After an +** application receives an SQLITE_LOCKED error, it may call the +** sqlite3_unlock_notify() method with the blocked connection handle as +** the first argument to register for a callback that will be invoked +** when the blocking connections current transaction is concluded. ^The +** callback is invoked from within the [sqlite3_step] or [sqlite3_close] +** call that concludes the blocking connections transaction. +** +** ^(If sqlite3_unlock_notify() is called in a multi-threaded application, +** there is a chance that the blocking connection will have already +** concluded its transaction by the time sqlite3_unlock_notify() is invoked. +** If this happens, then the specified callback is invoked immediately, +** from within the call to sqlite3_unlock_notify().)^ +** +** ^If the blocked connection is attempting to obtain a write-lock on a +** shared-cache table, and more than one other connection currently holds +** a read-lock on the same table, then SQLite arbitrarily selects one of +** the other connections to use as the blocking connection. +** +** ^(There may be at most one unlock-notify callback registered by a +** blocked connection. If sqlite3_unlock_notify() is called when the +** blocked connection already has a registered unlock-notify callback, +** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is +** called with a NULL pointer as its second argument, then any existing +** unlock-notify callback is canceled. ^The blocked connections +** unlock-notify callback may also be canceled by closing the blocked +** connection using [sqlite3_close()]. +** +** The unlock-notify callback is not reentrant. If an application invokes +** any sqlite3_xxx API functions from within an unlock-notify callback, a +** crash or deadlock may be the result. +** +** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always +** returns SQLITE_OK. +** +** Callback Invocation Details +** +** When an unlock-notify callback is registered, the application provides a +** single void* pointer that is passed to the callback when it is invoked. +** However, the signature of the callback function allows SQLite to pass +** it an array of void* context pointers. The first argument passed to +** an unlock-notify callback is a pointer to an array of void* pointers, +** and the second is the number of entries in the array. +** +** When a blocking connections transaction is concluded, there may be +** more than one blocked connection that has registered for an unlock-notify +** callback. ^If two or more such blocked connections have specified the +** same callback function, then instead of invoking the callback function +** multiple times, it is invoked once with the set of void* context pointers +** specified by the blocked connections bundled together into an array. +** This gives the application an opportunity to prioritize any actions +** related to the set of unblocked database connections. +** +** Deadlock Detection +** +** Assuming that after registering for an unlock-notify callback a +** database waits for the callback to be issued before taking any further +** action (a reasonable assumption), then using this API may cause the +** application to deadlock. For example, if connection X is waiting for +** connection Y's transaction to be concluded, and similarly connection +** Y is waiting on connection X's transaction, then neither connection +** will proceed and the system may remain deadlocked indefinitely. +** +** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock +** detection. ^If a given call to sqlite3_unlock_notify() would put the +** system in a deadlocked state, then SQLITE_LOCKED is returned and no +** unlock-notify callback is registered. The system is said to be in +** a deadlocked state if connection A has registered for an unlock-notify +** callback on the conclusion of connection B's transaction, and connection +** B has itself registered for an unlock-notify callback when connection +** A's transaction is concluded. ^Indirect deadlock is also detected, so +** the system is also considered to be deadlocked if connection B has +** registered for an unlock-notify callback on the conclusion of connection +** C's transaction, where connection C is waiting on connection A. ^Any +** number of levels of indirection are allowed. +** +** The "DROP TABLE" Exception +** +** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost +** always appropriate to call sqlite3_unlock_notify(). There is however, +** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement, +** SQLite checks if there are any currently executing SELECT statements +** that belong to the same connection. If there are, SQLITE_LOCKED is +** returned. In this case there is no "blocking connection", so invoking +** sqlite3_unlock_notify() results in the unlock-notify callback being +** invoked immediately. If the application then re-attempts the "DROP TABLE" +** or "DROP INDEX" query, an infinite loop might be the result. +** +** One way around this problem is to check the extended error code returned +** by an sqlite3_step() call. ^(If there is a blocking connection, then the +** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in +** the special "DROP TABLE/INDEX" case, the extended error code is just +** SQLITE_LOCKED.)^ +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify( + sqlite3 *pBlocked, /* Waiting connection */ + void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */ + void *pNotifyArg /* Argument to pass to xNotify */ +); + + +/* +** CAPI3REF: String Comparison +** +** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications +** and extensions to compare the contents of two buffers containing UTF-8 +** strings in a case-independent fashion, using the same definition of "case +** independence" that SQLite uses internally when comparing identifiers. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *, const char *); +SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *, const char *, int); + +/* +** CAPI3REF: String Globbing +* +** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if +** string X matches the [GLOB] pattern P. +** ^The definition of [GLOB] pattern matching used in +** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the +** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function +** is case sensitive. +** +** Note that this routine returns zero on a match and non-zero if the strings +** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. +** +** See also: [sqlite3_strlike()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zStr); + +/* +** CAPI3REF: String LIKE Matching +* +** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if +** string X matches the [LIKE] pattern P with escape character E. +** ^The definition of [LIKE] pattern matching used in +** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E" +** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without +** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0. +** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case +** insensitive - equivalent upper and lower case ASCII characters match +** one another. +** +** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though +** only ASCII characters are case folded. +** +** Note that this routine returns zero on a match and non-zero if the strings +** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. +** +** See also: [sqlite3_strglob()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc); + +/* +** CAPI3REF: Error Logging Interface +** +** ^The [sqlite3_log()] interface writes a message into the [error log] +** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. +** ^If logging is enabled, the zFormat string and subsequent arguments are +** used with [sqlite3_snprintf()] to generate the final output string. +** +** The sqlite3_log() interface is intended for use by extensions such as +** virtual tables, collating functions, and SQL functions. While there is +** nothing to prevent an application from calling sqlite3_log(), doing so +** is considered bad form. +** +** The zFormat string must not be NULL. +** +** To avoid deadlocks and other threading problems, the sqlite3_log() routine +** will not use dynamically allocated memory. The log message is stored in +** a fixed-length buffer on the stack. If the log message is longer than +** a few hundred characters, it will be truncated to the length of the +** buffer. +*/ +SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...); + +/* +** CAPI3REF: Write-Ahead Log Commit Hook +** METHOD: sqlite3 +** +** ^The [sqlite3_wal_hook()] function is used to register a callback that +** is invoked each time data is committed to a database in wal mode. +** +** ^(The callback is invoked by SQLite after the commit has taken place and +** the associated write-lock on the database released)^, so the implementation +** may read, write or [checkpoint] the database as required. +** +** ^The first parameter passed to the callback function when it is invoked +** is a copy of the third parameter passed to sqlite3_wal_hook() when +** registering the callback. ^The second is a copy of the database handle. +** ^The third parameter is the name of the database that was written to - +** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter +** is the number of pages currently in the write-ahead log file, +** including those that were just committed. +** +** The callback function should normally return [SQLITE_OK]. ^If an error +** code is returned, that error will propagate back up through the +** SQLite code base to cause the statement that provoked the callback +** to report an error, though the commit will have still occurred. If the +** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value +** that does not correspond to any valid SQLite error code, the results +** are undefined. +** +** A single database handle may have at most a single write-ahead log callback +** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any +** previously registered write-ahead log callback. ^Note that the +** [sqlite3_wal_autocheckpoint()] interface and the +** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will +** overwrite any prior [sqlite3_wal_hook()] settings. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook( + sqlite3*, + int(*)(void *,sqlite3*,const char*,int), + void* +); + +/* +** CAPI3REF: Configure an auto-checkpoint +** METHOD: sqlite3 +** +** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around +** [sqlite3_wal_hook()] that causes any database on [database connection] D +** to automatically [checkpoint] +** after committing a transaction if there are N or +** more frames in the [write-ahead log] file. ^Passing zero or +** a negative value as the nFrame parameter disables automatic +** checkpoints entirely. +** +** ^The callback registered by this function replaces any existing callback +** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback +** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism +** configured by this function. +** +** ^The [wal_autocheckpoint pragma] can be used to invoke this interface +** from SQL. +** +** ^Checkpoints initiated by this mechanism are +** [sqlite3_wal_checkpoint_v2|PASSIVE]. +** +** ^Every new [database connection] defaults to having the auto-checkpoint +** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT] +** pages. The use of this interface +** is only necessary if the default setting is found to be suboptimal +** for a particular application. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N); + +/* +** CAPI3REF: Checkpoint a database +** METHOD: sqlite3 +** +** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to +** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ +** +** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the +** [write-ahead log] for database X on [database connection] D to be +** transferred into the database file and for the write-ahead log to +** be reset. See the [checkpointing] documentation for addition +** information. +** +** This interface used to be the only way to cause a checkpoint to +** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()] +** interface was added. This interface is retained for backwards +** compatibility and as a convenience for applications that need to manually +** start a callback but which do not need the full power (and corresponding +** complication) of [sqlite3_wal_checkpoint_v2()]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); + +/* +** CAPI3REF: Checkpoint a database +** METHOD: sqlite3 +** +** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint +** operation on database X of [database connection] D in mode M. Status +** information is written back into integers pointed to by L and C.)^ +** ^(The M parameter must be a valid [checkpoint mode]:)^ +** +**
+**
SQLITE_CHECKPOINT_PASSIVE
+** ^Checkpoint as many frames as possible without waiting for any database +** readers or writers to finish, then sync the database file if all frames +** in the log were checkpointed. ^The [busy-handler callback] +** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. +** ^On the other hand, passive mode might leave the checkpoint unfinished +** if there are concurrent readers or writers. +** +**
SQLITE_CHECKPOINT_FULL
+** ^This mode blocks (it invokes the +** [sqlite3_busy_handler|busy-handler callback]) until there is no +** database writer and all readers are reading from the most recent database +** snapshot. ^It then checkpoints all frames in the log file and syncs the +** database file. ^This mode blocks new database writers while it is pending, +** but new database readers are allowed to continue unimpeded. +** +**
SQLITE_CHECKPOINT_RESTART
+** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition +** that after checkpointing the log file it blocks (calls the +** [busy-handler callback]) +** until all readers are reading from the database file only. ^This ensures +** that the next writer will restart the log file from the beginning. +** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new +** database writer attempts while it is pending, but does not impede readers. +** +**
SQLITE_CHECKPOINT_TRUNCATE
+** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the +** addition that it also truncates the log file to zero bytes just prior +** to a successful return. +**
+** +** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in +** the log file or to -1 if the checkpoint could not run because +** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not +** NULL,then *pnCkpt is set to the total number of checkpointed frames in the +** log file (including any that were already checkpointed before the function +** was called) or to -1 if the checkpoint could not run due to an error or +** because the database is not in WAL mode. ^Note that upon successful +** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been +** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero. +** +** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If +** any other process is running a checkpoint operation at the same time, the +** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a +** busy-handler configured, it will not be invoked in this case. +** +** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the +** exclusive "writer" lock on the database file. ^If the writer lock cannot be +** obtained immediately, and a busy-handler is configured, it is invoked and +** the writer lock retried until either the busy-handler returns 0 or the lock +** is successfully obtained. ^The busy-handler is also invoked while waiting for +** database readers as described above. ^If the busy-handler returns 0 before +** the writer lock is obtained or while waiting for database readers, the +** checkpoint operation proceeds from that point in the same way as +** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible +** without blocking any further. ^SQLITE_BUSY is returned in this case. +** +** ^If parameter zDb is NULL or points to a zero length string, then the +** specified operation is attempted on all WAL databases [attached] to +** [database connection] db. In this case the +** values written to output parameters *pnLog and *pnCkpt are undefined. ^If +** an SQLITE_BUSY error is encountered when processing one or more of the +** attached WAL databases, the operation is still attempted on any remaining +** attached databases and SQLITE_BUSY is returned at the end. ^If any other +** error occurs while processing an attached database, processing is abandoned +** and the error code is returned to the caller immediately. ^If no error +** (SQLITE_BUSY or otherwise) is encountered while processing the attached +** databases, SQLITE_OK is returned. +** +** ^If database zDb is the name of an attached database that is not in WAL +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If +** zDb is not NULL (or a zero length string) and is not the name of any +** attached database, SQLITE_ERROR is returned to the caller. +** +** ^Unless it returns SQLITE_MISUSE, +** the sqlite3_wal_checkpoint_v2() interface +** sets the error information that is queried by +** [sqlite3_errcode()] and [sqlite3_errmsg()]. +** +** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface +** from SQL. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of attached database (or NULL) */ + int eMode, /* SQLITE_CHECKPOINT_* value */ + int *pnLog, /* OUT: Size of WAL log in frames */ + int *pnCkpt /* OUT: Total number of frames checkpointed */ +); + +/* +** CAPI3REF: Checkpoint Mode Values +** KEYWORDS: {checkpoint mode} +** +** These constants define all valid values for the "checkpoint mode" passed +** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface. +** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the +** meaning of each of these checkpoint modes. +*/ +#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ +#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ +#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ +#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ + +/* +** CAPI3REF: Virtual Table Interface Configuration +** +** This function may be called by either the [xConnect] or [xCreate] method +** of a [virtual table] implementation to configure +** various facets of the virtual table interface. +** +** If this interface is invoked outside the context of an xConnect or +** xCreate virtual table method then the behavior is undefined. +** +** At present, there is only one option that may be configured using +** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options +** may be added in the future. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...); + +/* +** CAPI3REF: Virtual Table Configuration Options +** +** These macros define the various options to the +** [sqlite3_vtab_config()] interface that [virtual table] implementations +** can use to customize and optimize their behavior. +** +**
+**
SQLITE_VTAB_CONSTRAINT_SUPPORT +**
Calls of the form +** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, +** where X is an integer. If X is zero, then the [virtual table] whose +** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not +** support constraints. In this configuration (which is the default) if +** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire +** statement is rolled back as if [ON CONFLICT | OR ABORT] had been +** specified as part of the users SQL statement, regardless of the actual +** ON CONFLICT mode specified. +** +** If X is non-zero, then the virtual table implementation guarantees +** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before +** any modifications to internal or persistent data structures have been made. +** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite +** is able to roll back a statement or database transaction, and abandon +** or continue processing the current SQL statement as appropriate. +** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns +** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode +** had been ABORT. +** +** Virtual table implementations that are required to handle OR REPLACE +** must do so within the [xUpdate] method. If a call to the +** [sqlite3_vtab_on_conflict()] function indicates that the current ON +** CONFLICT policy is REPLACE, the virtual table implementation should +** silently replace the appropriate rows within the xUpdate callback and +** return SQLITE_OK. Or, if this is not possible, it may return +** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT +** constraint handling. +**
+*/ +#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1 + +/* +** CAPI3REF: Determine The Virtual Table Conflict Policy +** +** This function may only be called from within a call to the [xUpdate] method +** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The +** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL], +** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode +** of the SQL statement that triggered the call to the [xUpdate] method of the +** [virtual table]. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *); + +/* +** CAPI3REF: Conflict resolution modes +** KEYWORDS: {conflict resolution mode} +** +** These constants are returned by [sqlite3_vtab_on_conflict()] to +** inform a [virtual table] implementation what the [ON CONFLICT] mode +** is for the SQL statement being evaluated. +** +** Note that the [SQLITE_IGNORE] constant is also used as a potential +** return value from the [sqlite3_set_authorizer()] callback and that +** [SQLITE_ABORT] is also a [result code]. +*/ +#define SQLITE_ROLLBACK 1 +/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ +#define SQLITE_FAIL 3 +/* #define SQLITE_ABORT 4 // Also an error code */ +#define SQLITE_REPLACE 5 + +/* +** CAPI3REF: Prepared Statement Scan Status Opcodes +** KEYWORDS: {scanstatus options} +** +** The following constants can be used for the T parameter to the +** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a +** different metric for sqlite3_stmt_scanstatus() to return. +** +** When the value returned to V is a string, space to hold that string is +** managed by the prepared statement S and will be automatically freed when +** S is finalized. +** +**
+** [[SQLITE_SCANSTAT_NLOOP]]
SQLITE_SCANSTAT_NLOOP
+**
^The [sqlite3_int64] variable pointed to by the T parameter will be +** set to the total number of times that the X-th loop has run.
+** +** [[SQLITE_SCANSTAT_NVISIT]]
SQLITE_SCANSTAT_NVISIT
+**
^The [sqlite3_int64] variable pointed to by the T parameter will be set +** to the total number of rows examined by all iterations of the X-th loop.
+** +** [[SQLITE_SCANSTAT_EST]]
SQLITE_SCANSTAT_EST
+**
^The "double" variable pointed to by the T parameter will be set to the +** query planner's estimate for the average number of rows output from each +** iteration of the X-th loop. If the query planner's estimates was accurate, +** then this value will approximate the quotient NVISIT/NLOOP and the +** product of this value for all prior loops with the same SELECTID will +** be the NLOOP value for the current loop. +** +** [[SQLITE_SCANSTAT_NAME]]
SQLITE_SCANSTAT_NAME
+**
^The "const char *" variable pointed to by the T parameter will be set +** to a zero-terminated UTF-8 string containing the name of the index or table +** used for the X-th loop. +** +** [[SQLITE_SCANSTAT_EXPLAIN]]
SQLITE_SCANSTAT_EXPLAIN
+**
^The "const char *" variable pointed to by the T parameter will be set +** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] +** description for the X-th loop. +** +** [[SQLITE_SCANSTAT_SELECTID]]
SQLITE_SCANSTAT_SELECT
+**
^The "int" variable pointed to by the T parameter will be set to the +** "select-id" for the X-th loop. The select-id identifies which query or +** subquery the loop is part of. The main query has a select-id of zero. +** The select-id is the same value as is output in the first column +** of an [EXPLAIN QUERY PLAN] query. +**
+*/ +#define SQLITE_SCANSTAT_NLOOP 0 +#define SQLITE_SCANSTAT_NVISIT 1 +#define SQLITE_SCANSTAT_EST 2 +#define SQLITE_SCANSTAT_NAME 3 +#define SQLITE_SCANSTAT_EXPLAIN 4 +#define SQLITE_SCANSTAT_SELECTID 5 + +/* +** CAPI3REF: Prepared Statement Scan Status +** METHOD: sqlite3_stmt +** +** This interface returns information about the predicted and measured +** performance for pStmt. Advanced applications can use this +** interface to compare the predicted and the measured performance and +** issue warnings and/or rerun [ANALYZE] if discrepancies are found. +** +** Since this interface is expected to be rarely used, it is only +** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS] +** compile-time option. +** +** The "iScanStatusOp" parameter determines which status information to return. +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior +** of this interface is undefined. +** ^The requested measurement is written into a variable pointed to by +** the "pOut" parameter. +** Parameter "idx" identifies the specific loop to retrieve statistics for. +** Loops are numbered starting from zero. ^If idx is out of range - less than +** zero or greater than or equal to the total number of loops used to implement +** the statement - a non-zero value is returned and the variable that pOut +** points to is unchanged. +** +** ^Statistics might not be available for all loops in all statements. ^In cases +** where there exist loops with no available statistics, this function behaves +** as if the loop did not exist - it returns non-zero and leave the variable +** that pOut points to unchanged. +** +** See also: [sqlite3_stmt_scanstatus_reset()] +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus( + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ + int idx, /* Index of loop to report on */ + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ + void *pOut /* Result written here */ +); + +/* +** CAPI3REF: Zero Scan-Status Counters +** METHOD: sqlite3_stmt +** +** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. +** +** This API is only available if the library is built with pre-processor +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); + +/* +** CAPI3REF: Flush caches to disk mid-transaction +** +** ^If a write-transaction is open on [database connection] D when the +** [sqlite3_db_cacheflush(D)] interface invoked, any dirty +** pages in the pager-cache that are not currently in use are written out +** to disk. A dirty page may be in use if a database cursor created by an +** active SQL statement is reading from it, or if it is page 1 of a database +** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] +** interface flushes caches for all schemas - "main", "temp", and +** any [attached] databases. +** +** ^If this function needs to obtain extra database locks before dirty pages +** can be flushed to disk, it does so. ^If those locks cannot be obtained +** immediately and there is a busy-handler callback configured, it is invoked +** in the usual manner. ^If the required lock still cannot be obtained, then +** the database is skipped and an attempt made to flush any dirty pages +** belonging to the next (if any) database. ^If any databases are skipped +** because locks cannot be obtained, but no other error occurs, this +** function returns SQLITE_BUSY. +** +** ^If any other error occurs while flushing dirty pages to disk (for +** example an IO error or out-of-memory condition), then processing is +** abandoned and an SQLite [error code] is returned to the caller immediately. +** +** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. +** +** ^This function does not set the database handle error code or message +** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); + +/* +** CAPI3REF: The pre-update hook. +** +** ^These interfaces are only available if SQLite is compiled using the +** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. +** +** ^The [sqlite3_preupdate_hook()] interface registers a callback function +** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation +** on a [rowid table]. +** ^At most one preupdate hook may be registered at a time on a single +** [database connection]; each call to [sqlite3_preupdate_hook()] overrides +** the previous setting. +** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] +** with a NULL pointer as the second parameter. +** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as +** the first parameter to callbacks. +** +** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate +** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID] +** tables. +** +** ^The second parameter to the preupdate callback is a pointer to +** the [database connection] that registered the preupdate hook. +** ^The third parameter to the preupdate callback is one of the constants +** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to indentify the +** kind of update operation that is about to occur. +** ^(The fourth parameter to the preupdate callback is the name of the +** database within the database connection that is being modified. This +** will be "main" for the main database or "temp" for TEMP tables or +** the name given after the AS keyword in the [ATTACH] statement for attached +** databases.)^ +** ^The fifth parameter to the preupdate callback is the name of the +** table that is being modified. +** ^The sixth parameter to the preupdate callback is the initial [rowid] of the +** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is +** undefined for SQLITE_INSERT changes. +** ^The seventh parameter to the preupdate callback is the final [rowid] of +** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is +** undefined for SQLITE_DELETE changes. +** +** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], +** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces +** provide additional information about a preupdate event. These routines +** may only be called from within a preupdate callback. Invoking any of +** these routines from outside of a preupdate callback or with a +** [database connection] pointer that is different from the one supplied +** to the preupdate callback results in undefined and probably undesirable +** behavior. +** +** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns +** in the row that is being inserted, updated, or deleted. +** +** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to +** a [protected sqlite3_value] that contains the value of the Nth column of +** the table row before it is updated. The N parameter must be between 0 +** and one less than the number of columns or the behavior will be +** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE +** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the +** behavior is undefined. The [sqlite3_value] that P points to +** will be destroyed when the preupdate callback returns. +** +** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to +** a [protected sqlite3_value] that contains the value of the Nth column of +** the table row after it is updated. The N parameter must be between 0 +** and one less than the number of columns or the behavior will be +** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE +** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the +** behavior is undefined. The [sqlite3_value] that P points to +** will be destroyed when the preupdate callback returns. +** +** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate +** callback was invoked as a result of a direct insert, update, or delete +** operation; or 1 for inserts, updates, or deletes invoked by top-level +** triggers; or 2 for changes resulting from triggers called by top-level +** triggers; and so forth. +** +** See also: [sqlite3_update_hook()] +*/ +SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_preupdate_hook( + sqlite3 *db, + void(*xPreUpdate)( + void *pCtx, /* Copy of third arg to preupdate_hook() */ + sqlite3 *db, /* Database handle */ + int op, /* SQLITE_UPDATE, DELETE or INSERT */ + char const *zDb, /* Database name */ + char const *zName, /* Table name */ + sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ + sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ + ), + void* +); +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_count(sqlite3 *); +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_depth(sqlite3 *); +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); + +/* +** CAPI3REF: Low-level system error code +** +** ^Attempt to return the underlying operating system error code or error +** number that caused the most recent I/O error or failure to open a file. +** The return value is OS-dependent. For example, on unix systems, after +** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be +** called to get back the underlying "errno" that caused the problem, such +** as ENOSPC, EAUTH, EISDIR, and so forth. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_system_errno(sqlite3*); + +/* +** CAPI3REF: Database Snapshot +** KEYWORDS: {snapshot} +** EXPERIMENTAL +** +** An instance of the snapshot object records the state of a [WAL mode] +** database for some specific point in history. +** +** In [WAL mode], multiple [database connections] that are open on the +** same database file can each be reading a different historical version +** of the database file. When a [database connection] begins a read +** transaction, that connection sees an unchanging copy of the database +** as it existed for the point in time when the transaction first started. +** Subsequent changes to the database from other connections are not seen +** by the reader until a new read transaction is started. +** +** The sqlite3_snapshot object records state information about an historical +** version of the database file so that it is possible to later open a new read +** transaction that sees that historical version of the database rather than +** the most recent version. +** +** The constructor for this object is [sqlite3_snapshot_get()]. The +** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer +** to an historical snapshot (if possible). The destructor for +** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. +*/ +typedef struct sqlite3_snapshot sqlite3_snapshot; + +/* +** CAPI3REF: Record A Database Snapshot +** EXPERIMENTAL +** +** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a +** new [sqlite3_snapshot] object that records the current state of +** schema S in database connection D. ^On success, the +** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly +** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. +** ^If schema S of [database connection] D is not a [WAL mode] database +** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] +** leaves the *P value unchanged and returns an appropriate [error code]. +** +** The [sqlite3_snapshot] object returned from a successful call to +** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] +** to avoid a memory leak. +** +** The [sqlite3_snapshot_get()] interface is only available when the +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +*/ +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_get( + sqlite3 *db, + const char *zSchema, + sqlite3_snapshot **ppSnapshot +); + +/* +** CAPI3REF: Start a read transaction on an historical snapshot +** EXPERIMENTAL +** +** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a +** read transaction for schema S of +** [database connection] D such that the read transaction +** refers to historical [snapshot] P, rather than the most +** recent change to the database. +** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success +** or an appropriate [error code] if it fails. +** +** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be +** the first operation following the [BEGIN] that takes the schema S +** out of [autocommit mode]. +** ^In other words, schema S must not currently be in +** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the +** database connection D must be out of [autocommit mode]. +** ^A [snapshot] will fail to open if it has been overwritten by a +** [checkpoint]. +** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the +** database connection D does not know that the database file for +** schema S is in [WAL mode]. A database connection might not know +** that the database file is in [WAL mode] if there has been no prior +** I/O on that database connection, or if the database entered [WAL mode] +** after the most recent I/O on the database connection.)^ +** (Hint: Run "[PRAGMA application_id]" against a newly opened +** database connection in order to make it ready to use snapshots.) +** +** The [sqlite3_snapshot_open()] interface is only available when the +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +*/ +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_open( + sqlite3 *db, + const char *zSchema, + sqlite3_snapshot *pSnapshot +); + +/* +** CAPI3REF: Destroy a snapshot +** EXPERIMENTAL +** +** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. +** The application must eventually free every [sqlite3_snapshot] object +** using this routine to avoid a memory leak. +** +** The [sqlite3_snapshot_free()] interface is only available when the +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +*/ +SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*); + +/* +** CAPI3REF: Compare the ages of two snapshot handles. +** EXPERIMENTAL +** +** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages +** of two valid snapshot handles. +** +** If the two snapshot handles are not associated with the same database +** file, the result of the comparison is undefined. +** +** Additionally, the result of the comparison is only valid if both of the +** snapshot handles were obtained by calling sqlite3_snapshot_get() since the +** last time the wal file was deleted. The wal file is deleted when the +** database is changed back to rollback mode or when the number of database +** clients drops to zero. If either snapshot handle was obtained before the +** wal file was last deleted, the value returned by this function +** is undefined. +** +** Otherwise, this API returns a negative value if P1 refers to an older +** snapshot than P2, zero if the two handles refer to the same database +** snapshot, and a positive value if P1 is a newer snapshot than P2. +*/ +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_cmp( + sqlite3_snapshot *p1, + sqlite3_snapshot *p2 +); + +/* +** Undo the hack that converts floating point types to integer for +** builds on processors without floating point support. +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# undef double +#endif + +#if 0 +} /* End of the 'extern "C"' block */ +#endif +#endif /* _SQLITE3_H_ */ + +/******** Begin file sqlite3rtree.h *********/ +/* +** 2010 August 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +#ifndef _SQLITE3RTREE_H_ +#define _SQLITE3RTREE_H_ + + +#if 0 +extern "C" { +#endif + +typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; +typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info; + +/* The double-precision datatype used by RTree depends on the +** SQLITE_RTREE_INT_ONLY compile-time option. +*/ +#ifdef SQLITE_RTREE_INT_ONLY + typedef sqlite3_int64 sqlite3_rtree_dbl; +#else + typedef double sqlite3_rtree_dbl; +#endif + +/* +** Register a geometry callback named zGeom that can be used as part of an +** R-Tree geometry query as follows: +** +** SELECT ... FROM WHERE MATCH $zGeom(... params ...) +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback( + sqlite3 *db, + const char *zGeom, + int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*), + void *pContext +); + + +/* +** A pointer to a structure of the following type is passed as the first +** argument to callbacks registered using rtree_geometry_callback(). +*/ +struct sqlite3_rtree_geometry { + void *pContext; /* Copy of pContext passed to s_r_g_c() */ + int nParam; /* Size of array aParam[] */ + sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */ + void *pUser; /* Callback implementation user data */ + void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ +}; + +/* +** Register a 2nd-generation geometry callback named zScore that can be +** used as part of an R-Tree geometry query as follows: +** +** SELECT ... FROM WHERE MATCH $zQueryFunc(... params ...) +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback( + sqlite3 *db, + const char *zQueryFunc, + int (*xQueryFunc)(sqlite3_rtree_query_info*), + void *pContext, + void (*xDestructor)(void*) +); + + +/* +** A pointer to a structure of the following type is passed as the +** argument to scored geometry callback registered using +** sqlite3_rtree_query_callback(). +** +** Note that the first 5 fields of this structure are identical to +** sqlite3_rtree_geometry. This structure is a subclass of +** sqlite3_rtree_geometry. +*/ +struct sqlite3_rtree_query_info { + void *pContext; /* pContext from when function registered */ + int nParam; /* Number of function parameters */ + sqlite3_rtree_dbl *aParam; /* value of function parameters */ + void *pUser; /* callback can use this, if desired */ + void (*xDelUser)(void*); /* function to free pUser */ + sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */ + unsigned int *anQueue; /* Number of pending entries in the queue */ + int nCoord; /* Number of coordinates */ + int iLevel; /* Level of current node or entry */ + int mxLevel; /* The largest iLevel value in the tree */ + sqlite3_int64 iRowid; /* Rowid for current entry */ + sqlite3_rtree_dbl rParentScore; /* Score of parent node */ + int eParentWithin; /* Visibility of parent node */ + int eWithin; /* OUT: Visiblity */ + sqlite3_rtree_dbl rScore; /* OUT: Write the score here */ + /* The following fields are only available in 3.8.11 and later */ + sqlite3_value **apSqlParam; /* Original SQL values of parameters */ +}; + +/* +** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin. +*/ +#define NOT_WITHIN 0 /* Object completely outside of query region */ +#define PARTLY_WITHIN 1 /* Object partially overlaps query region */ +#define FULLY_WITHIN 2 /* Object fully contained within query region */ + + +#if 0 +} /* end of the 'extern "C"' block */ +#endif + +#endif /* ifndef _SQLITE3RTREE_H_ */ + +/******** End of sqlite3rtree.h *********/ +/******** Begin file sqlite3session.h *********/ + +#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) +#define __SQLITESESSION_H_ 1 + +/* +** Make sure we can call this stuff from C++. +*/ +#if 0 +extern "C" { +#endif + + +/* +** CAPI3REF: Session Object Handle +*/ +typedef struct sqlite3_session sqlite3_session; + +/* +** CAPI3REF: Changeset Iterator Handle +*/ +typedef struct sqlite3_changeset_iter sqlite3_changeset_iter; + +/* +** CAPI3REF: Create A New Session Object +** +** Create a new session object attached to database handle db. If successful, +** a pointer to the new object is written to *ppSession and SQLITE_OK is +** returned. If an error occurs, *ppSession is set to NULL and an SQLite +** error code (e.g. SQLITE_NOMEM) is returned. +** +** It is possible to create multiple session objects attached to a single +** database handle. +** +** Session objects created using this function should be deleted using the +** [sqlite3session_delete()] function before the database handle that they +** are attached to is itself closed. If the database handle is closed before +** the session object is deleted, then the results of calling any session +** module function, including [sqlite3session_delete()] on the session object +** are undefined. +** +** Because the session module uses the [sqlite3_preupdate_hook()] API, it +** is not possible for an application to register a pre-update hook on a +** database handle that has one or more session objects attached. Nor is +** it possible to create a session object attached to a database handle for +** which a pre-update hook is already defined. The results of attempting +** either of these things are undefined. +** +** The session object will be used to create changesets for tables in +** database zDb, where zDb is either "main", or "temp", or the name of an +** attached database. It is not an error if database zDb is not attached +** to the database when the session object is created. +*/ +int sqlite3session_create( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of db (e.g. "main") */ + sqlite3_session **ppSession /* OUT: New session object */ +); + +/* +** CAPI3REF: Delete A Session Object +** +** Delete a session object previously allocated using +** [sqlite3session_create()]. Once a session object has been deleted, the +** results of attempting to use pSession with any other session module +** function are undefined. +** +** Session objects must be deleted before the database handle to which they +** are attached is closed. Refer to the documentation for +** [sqlite3session_create()] for details. +*/ +void sqlite3session_delete(sqlite3_session *pSession); + + +/* +** CAPI3REF: Enable Or Disable A Session Object +** +** Enable or disable the recording of changes by a session object. When +** enabled, a session object records changes made to the database. When +** disabled - it does not. A newly created session object is enabled. +** Refer to the documentation for [sqlite3session_changeset()] for further +** details regarding how enabling and disabling a session object affects +** the eventual changesets. +** +** Passing zero to this function disables the session. Passing a value +** greater than zero enables it. Passing a value less than zero is a +** no-op, and may be used to query the current state of the session. +** +** The return value indicates the final state of the session object: 0 if +** the session is disabled, or 1 if it is enabled. +*/ +int sqlite3session_enable(sqlite3_session *pSession, int bEnable); + +/* +** CAPI3REF: Set Or Clear the Indirect Change Flag +** +** Each change recorded by a session object is marked as either direct or +** indirect. A change is marked as indirect if either: +** +**
    +**
  • The session object "indirect" flag is set when the change is +** made, or +**
  • The change is made by an SQL trigger or foreign key action +** instead of directly as a result of a users SQL statement. +**
+** +** If a single row is affected by more than one operation within a session, +** then the change is considered indirect if all operations meet the criteria +** for an indirect change above, or direct otherwise. +** +** This function is used to set, clear or query the session object indirect +** flag. If the second argument passed to this function is zero, then the +** indirect flag is cleared. If it is greater than zero, the indirect flag +** is set. Passing a value less than zero does not modify the current value +** of the indirect flag, and may be used to query the current state of the +** indirect flag for the specified session object. +** +** The return value indicates the final state of the indirect flag: 0 if +** it is clear, or 1 if it is set. +*/ +int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); + +/* +** CAPI3REF: Attach A Table To A Session Object +** +** If argument zTab is not NULL, then it is the name of a table to attach +** to the session object passed as the first argument. All subsequent changes +** made to the table while the session object is enabled will be recorded. See +** documentation for [sqlite3session_changeset()] for further details. +** +** Or, if argument zTab is NULL, then changes are recorded for all tables +** in the database. If additional tables are added to the database (by +** executing "CREATE TABLE" statements) after this call is made, changes for +** the new tables are also recorded. +** +** Changes can only be recorded for tables that have a PRIMARY KEY explicitly +** defined as part of their CREATE TABLE statement. It does not matter if the +** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY +** KEY may consist of a single column, or may be a composite key. +** +** It is not an error if the named table does not exist in the database. Nor +** is it an error if the named table does not have a PRIMARY KEY. However, +** no changes will be recorded in either of these scenarios. +** +** Changes are not recorded for individual rows that have NULL values stored +** in one or more of their PRIMARY KEY columns. +** +** SQLITE_OK is returned if the call completes without error. Or, if an error +** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. +*/ +int sqlite3session_attach( + sqlite3_session *pSession, /* Session object */ + const char *zTab /* Table name */ +); + +/* +** CAPI3REF: Set a table filter on a Session Object. +** +** The second argument (xFilter) is the "filter callback". For changes to rows +** in tables that are not attached to the Session oject, the filter is called +** to determine whether changes to the table's rows should be tracked or not. +** If xFilter returns 0, changes is not tracked. Note that once a table is +** attached, xFilter will not be called again. +*/ +void sqlite3session_table_filter( + sqlite3_session *pSession, /* Session object */ + int(*xFilter)( + void *pCtx, /* Copy of third arg to _filter_table() */ + const char *zTab /* Table name */ + ), + void *pCtx /* First argument passed to xFilter */ +); + +/* +** CAPI3REF: Generate A Changeset From A Session Object +** +** Obtain a changeset containing changes to the tables attached to the +** session object passed as the first argument. If successful, +** set *ppChangeset to point to a buffer containing the changeset +** and *pnChangeset to the size of the changeset in bytes before returning +** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to +** zero and return an SQLite error code. +** +** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, +** each representing a change to a single row of an attached table. An INSERT +** change contains the values of each field of a new database row. A DELETE +** contains the original values of each field of a deleted database row. An +** UPDATE change contains the original values of each field of an updated +** database row along with the updated values for each updated non-primary-key +** column. It is not possible for an UPDATE change to represent a change that +** modifies the values of primary key columns. If such a change is made, it +** is represented in a changeset as a DELETE followed by an INSERT. +** +** Changes are not recorded for rows that have NULL values stored in one or +** more of their PRIMARY KEY columns. If such a row is inserted or deleted, +** no corresponding change is present in the changesets returned by this +** function. If an existing row with one or more NULL values stored in +** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, +** only an INSERT is appears in the changeset. Similarly, if an existing row +** with non-NULL PRIMARY KEY values is updated so that one or more of its +** PRIMARY KEY columns are set to NULL, the resulting changeset contains a +** DELETE change only. +** +** The contents of a changeset may be traversed using an iterator created +** using the [sqlite3changeset_start()] API. A changeset may be applied to +** a database with a compatible schema using the [sqlite3changeset_apply()] +** API. +** +** Within a changeset generated by this function, all changes related to a +** single table are grouped together. In other words, when iterating through +** a changeset or when applying a changeset to a database, all changes related +** to a single table are processed before moving on to the next table. Tables +** are sorted in the same order in which they were attached (or auto-attached) +** to the sqlite3_session object. The order in which the changes related to +** a single table are stored is undefined. +** +** Following a successful call to this function, it is the responsibility of +** the caller to eventually free the buffer that *ppChangeset points to using +** [sqlite3_free()]. +** +**

Changeset Generation

+** +** Once a table has been attached to a session object, the session object +** records the primary key values of all new rows inserted into the table. +** It also records the original primary key and other column values of any +** deleted or updated rows. For each unique primary key value, data is only +** recorded once - the first time a row with said primary key is inserted, +** updated or deleted in the lifetime of the session. +** +** There is one exception to the previous paragraph: when a row is inserted, +** updated or deleted, if one or more of its primary key columns contain a +** NULL value, no record of the change is made. +** +** The session object therefore accumulates two types of records - those +** that consist of primary key values only (created when the user inserts +** a new record) and those that consist of the primary key values and the +** original values of other table columns (created when the users deletes +** or updates a record). +** +** When this function is called, the requested changeset is created using +** both the accumulated records and the current contents of the database +** file. Specifically: +** +**
    +**
  • For each record generated by an insert, the database is queried +** for a row with a matching primary key. If one is found, an INSERT +** change is added to the changeset. If no such row is found, no change +** is added to the changeset. +** +**
  • For each record generated by an update or delete, the database is +** queried for a row with a matching primary key. If such a row is +** found and one or more of the non-primary key fields have been +** modified from their original values, an UPDATE change is added to +** the changeset. Or, if no such row is found in the table, a DELETE +** change is added to the changeset. If there is a row with a matching +** primary key in the database, but all fields contain their original +** values, no change is added to the changeset. +**
+** +** This means, amongst other things, that if a row is inserted and then later +** deleted while a session object is active, neither the insert nor the delete +** will be present in the changeset. Or if a row is deleted and then later a +** row with the same primary key values inserted while a session object is +** active, the resulting changeset will contain an UPDATE change instead of +** a DELETE and an INSERT. +** +** When a session object is disabled (see the [sqlite3session_enable()] API), +** it does not accumulate records when rows are inserted, updated or deleted. +** This may appear to have some counter-intuitive effects if a single row +** is written to more than once during a session. For example, if a row +** is inserted while a session object is enabled, then later deleted while +** the same session object is disabled, no INSERT record will appear in the +** changeset, even though the delete took place while the session was disabled. +** Or, if one field of a row is updated while a session is disabled, and +** another field of the same row is updated while the session is enabled, the +** resulting changeset will contain an UPDATE change that updates both fields. +*/ +int sqlite3session_changeset( + sqlite3_session *pSession, /* Session object */ + int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ + void **ppChangeset /* OUT: Buffer containing changeset */ +); + +/* +** CAPI3REF: Load The Difference Between Tables Into A Session +** +** If it is not already attached to the session object passed as the first +** argument, this function attaches table zTbl in the same manner as the +** [sqlite3session_attach()] function. If zTbl does not exist, or if it +** does not have a primary key, this function is a no-op (but does not return +** an error). +** +** Argument zFromDb must be the name of a database ("main", "temp" etc.) +** attached to the same database handle as the session object that contains +** a table compatible with the table attached to the session by this function. +** A table is considered compatible if it: +** +**
    +**
  • Has the same name, +**
  • Has the same set of columns declared in the same order, and +**
  • Has the same PRIMARY KEY definition. +**
+** +** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables +** are compatible but do not have any PRIMARY KEY columns, it is not an error +** but no changes are added to the session object. As with other session +** APIs, tables without PRIMARY KEYs are simply ignored. +** +** This function adds a set of changes to the session object that could be +** used to update the table in database zFrom (call this the "from-table") +** so that its content is the same as the table attached to the session +** object (call this the "to-table"). Specifically: +** +**
    +**
  • For each row (primary key) that exists in the to-table but not in +** the from-table, an INSERT record is added to the session object. +** +**
  • For each row (primary key) that exists in the to-table but not in +** the from-table, a DELETE record is added to the session object. +** +**
  • For each row (primary key) that exists in both tables, but features +** different in each, an UPDATE record is added to the session. +**
+** +** To clarify, if this function is called and then a changeset constructed +** using [sqlite3session_changeset()], then after applying that changeset to +** database zFrom the contents of the two compatible tables would be +** identical. +** +** It an error if database zFrom does not exist or does not contain the +** required compatible table. +** +** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite +** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg +** may be set to point to a buffer containing an English language error +** message. It is the responsibility of the caller to free this buffer using +** sqlite3_free(). +*/ +int sqlite3session_diff( + sqlite3_session *pSession, + const char *zFromDb, + const char *zTbl, + char **pzErrMsg +); + + +/* +** CAPI3REF: Generate A Patchset From A Session Object +** +** The differences between a patchset and a changeset are that: +** +**
    +**
  • DELETE records consist of the primary key fields only. The +** original values of other fields are omitted. +**
  • The original values of any modified fields are omitted from +** UPDATE records. +**
+** +** A patchset blob may be used with up to date versions of all +** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), +** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, +** attempting to use a patchset blob with old versions of the +** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. +** +** Because the non-primary key "old.*" fields are omitted, no +** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset +** is passed to the sqlite3changeset_apply() API. Other conflict types work +** in the same way as for changesets. +** +** Changes within a patchset are ordered in the same way as for changesets +** generated by the sqlite3session_changeset() function (i.e. all changes for +** a single table are grouped together, tables appear in the order in which +** they were attached to the session object). +*/ +int sqlite3session_patchset( + sqlite3_session *pSession, /* Session object */ + int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */ + void **ppPatchset /* OUT: Buffer containing changeset */ +); + +/* +** CAPI3REF: Test if a changeset has recorded any changes. +** +** Return non-zero if no changes to attached tables have been recorded by +** the session object passed as the first argument. Otherwise, if one or +** more changes have been recorded, return zero. +** +** Even if this function returns zero, it is possible that calling +** [sqlite3session_changeset()] on the session handle may still return a +** changeset that contains no changes. This can happen when a row in +** an attached table is modified and then later on the original values +** are restored. However, if this function returns non-zero, then it is +** guaranteed that a call to sqlite3session_changeset() will return a +** changeset containing zero changes. +*/ +int sqlite3session_isempty(sqlite3_session *pSession); + +/* +** CAPI3REF: Create An Iterator To Traverse A Changeset +** +** Create an iterator used to iterate through the contents of a changeset. +** If successful, *pp is set to point to the iterator handle and SQLITE_OK +** is returned. Otherwise, if an error occurs, *pp is set to zero and an +** SQLite error code is returned. +** +** The following functions can be used to advance and query a changeset +** iterator created by this function: +** +**
    +**
  • [sqlite3changeset_next()] +**
  • [sqlite3changeset_op()] +**
  • [sqlite3changeset_new()] +**
  • [sqlite3changeset_old()] +**
+** +** It is the responsibility of the caller to eventually destroy the iterator +** by passing it to [sqlite3changeset_finalize()]. The buffer containing the +** changeset (pChangeset) must remain valid until after the iterator is +** destroyed. +** +** Assuming the changeset blob was created by one of the +** [sqlite3session_changeset()], [sqlite3changeset_concat()] or +** [sqlite3changeset_invert()] functions, all changes within the changeset +** that apply to a single table are grouped together. This means that when +** an application iterates through a changeset using an iterator created by +** this function, all changes that relate to a single table are visted +** consecutively. There is no chance that the iterator will visit a change +** the applies to table X, then one for table Y, and then later on visit +** another change for table X. +*/ +int sqlite3changeset_start( + sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ + int nChangeset, /* Size of changeset blob in bytes */ + void *pChangeset /* Pointer to blob containing changeset */ +); + + +/* +** CAPI3REF: Advance A Changeset Iterator +** +** This function may only be used with iterators created by function +** [sqlite3changeset_start()]. If it is called on an iterator passed to +** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE +** is returned and the call has no effect. +** +** Immediately after an iterator is created by sqlite3changeset_start(), it +** does not point to any change in the changeset. Assuming the changeset +** is not empty, the first call to this function advances the iterator to +** point to the first change in the changeset. Each subsequent call advances +** the iterator to point to the next change in the changeset (if any). If +** no error occurs and the iterator points to a valid change after a call +** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. +** Otherwise, if all changes in the changeset have already been visited, +** SQLITE_DONE is returned. +** +** If an error occurs, an SQLite error code is returned. Possible error +** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or +** SQLITE_NOMEM. +*/ +int sqlite3changeset_next(sqlite3_changeset_iter *pIter); + +/* +** CAPI3REF: Obtain The Current Operation From A Changeset Iterator +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this +** is not the case, this function returns [SQLITE_MISUSE]. +** +** If argument pzTab is not NULL, then *pzTab is set to point to a +** nul-terminated utf-8 encoded string containing the name of the table +** affected by the current change. The buffer remains valid until either +** sqlite3changeset_next() is called on the iterator or until the +** conflict-handler function returns. If pnCol is not NULL, then *pnCol is +** set to the number of columns in the table affected by the change. If +** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change +** is an indirect change, or false (0) otherwise. See the documentation for +** [sqlite3session_indirect()] for a description of direct and indirect +** changes. Finally, if pOp is not NULL, then *pOp is set to one of +** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the +** type of change that the iterator currently points to. +** +** If no error occurs, SQLITE_OK is returned. If an error does occur, an +** SQLite error code is returned. The values of the output variables may not +** be trusted in this case. +*/ +int sqlite3changeset_op( + sqlite3_changeset_iter *pIter, /* Iterator object */ + const char **pzTab, /* OUT: Pointer to table name */ + int *pnCol, /* OUT: Number of columns in table */ + int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ + int *pbIndirect /* OUT: True for an 'indirect' change */ +); + +/* +** CAPI3REF: Obtain The Primary Key Definition Of A Table +** +** For each modified table, a changeset includes the following: +** +**
    +**
  • The number of columns in the table, and +**
  • Which of those columns make up the tables PRIMARY KEY. +**
+** +** This function is used to find which columns comprise the PRIMARY KEY of +** the table modified by the change that iterator pIter currently points to. +** If successful, *pabPK is set to point to an array of nCol entries, where +** nCol is the number of columns in the table. Elements of *pabPK are set to +** 0x01 if the corresponding column is part of the tables primary key, or +** 0x00 if it is not. +** +** If argumet pnCol is not NULL, then *pnCol is set to the number of columns +** in the table. +** +** If this function is called when the iterator does not point to a valid +** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, +** SQLITE_OK is returned and the output variables populated as described +** above. +*/ +int sqlite3changeset_pk( + sqlite3_changeset_iter *pIter, /* Iterator object */ + unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ + int *pnCol /* OUT: Number of entries in output array */ +); + +/* +** CAPI3REF: Obtain old.* Values From A Changeset Iterator +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. +** Furthermore, it may only be called if the type of change that the iterator +** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the vector of +** original row values stored as part of the UPDATE or DELETE change and +** returns SQLITE_OK. The name of the function comes from the fact that this +** is similar to the "old.*" columns available to update or delete triggers. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_old( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ +); + +/* +** CAPI3REF: Obtain new.* Values From A Changeset Iterator +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. +** Furthermore, it may only be called if the type of change that the iterator +** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the vector of +** new row values stored as part of the UPDATE or INSERT change and +** returns SQLITE_OK. If the change is an UPDATE and does not include +** a new value for the requested column, *ppValue is set to NULL and +** SQLITE_OK returned. The name of the function comes from the fact that +** this is similar to the "new.*" columns available to update or delete +** triggers. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_new( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ +); + +/* +** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator +** +** This function should only be used with iterator objects passed to a +** conflict-handler callback by [sqlite3changeset_apply()] with either +** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function +** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue +** is set to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the +** "conflicting row" associated with the current conflict-handler callback +** and returns SQLITE_OK. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_conflict( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: Value from conflicting row */ +); + +/* +** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations +** +** This function may only be called with an iterator passed to an +** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case +** it sets the output variable to the total number of known foreign key +** violations in the destination database and returns SQLITE_OK. +** +** In all other cases this function returns SQLITE_MISUSE. +*/ +int sqlite3changeset_fk_conflicts( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int *pnOut /* OUT: Number of FK violations */ +); + + +/* +** CAPI3REF: Finalize A Changeset Iterator +** +** This function is used to finalize an iterator allocated with +** [sqlite3changeset_start()]. +** +** This function should only be called on iterators created using the +** [sqlite3changeset_start()] function. If an application calls this +** function with an iterator passed to a conflict-handler by +** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the +** call has no effect. +** +** If an error was encountered within a call to an sqlite3changeset_xxx() +** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an +** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding +** to that error is returned by this function. Otherwise, SQLITE_OK is +** returned. This is to allow the following pattern (pseudo-code): +** +** sqlite3changeset_start(); +** while( SQLITE_ROW==sqlite3changeset_next() ){ +** // Do something with change. +** } +** rc = sqlite3changeset_finalize(); +** if( rc!=SQLITE_OK ){ +** // An error has occurred +** } +*/ +int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); + +/* +** CAPI3REF: Invert A Changeset +** +** This function is used to "invert" a changeset object. Applying an inverted +** changeset to a database reverses the effects of applying the uninverted +** changeset. Specifically: +** +**
    +**
  • Each DELETE change is changed to an INSERT, and +**
  • Each INSERT change is changed to a DELETE, and +**
  • For each UPDATE change, the old.* and new.* values are exchanged. +**
+** +** This function does not change the order in which changes appear within +** the changeset. It merely reverses the sense of each individual change. +** +** If successful, a pointer to a buffer containing the inverted changeset +** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and +** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are +** zeroed and an SQLite error code returned. +** +** It is the responsibility of the caller to eventually call sqlite3_free() +** on the *ppOut pointer to free the buffer allocation following a successful +** call to this function. +** +** WARNING/TODO: This function currently assumes that the input is a valid +** changeset. If it is not, the results are undefined. +*/ +int sqlite3changeset_invert( + int nIn, const void *pIn, /* Input changeset */ + int *pnOut, void **ppOut /* OUT: Inverse of input */ +); + +/* +** CAPI3REF: Concatenate Two Changeset Objects +** +** This function is used to concatenate two changesets, A and B, into a +** single changeset. The result is a changeset equivalent to applying +** changeset A followed by changeset B. +** +** This function combines the two input changesets using an +** sqlite3_changegroup object. Calling it produces similar results as the +** following code fragment: +** +** sqlite3_changegroup *pGrp; +** rc = sqlite3_changegroup_new(&pGrp); +** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA); +** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB); +** if( rc==SQLITE_OK ){ +** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); +** }else{ +** *ppOut = 0; +** *pnOut = 0; +** } +** +** Refer to the sqlite3_changegroup documentation below for details. +*/ +int sqlite3changeset_concat( + int nA, /* Number of bytes in buffer pA */ + void *pA, /* Pointer to buffer containing changeset A */ + int nB, /* Number of bytes in buffer pB */ + void *pB, /* Pointer to buffer containing changeset B */ + int *pnOut, /* OUT: Number of bytes in output changeset */ + void **ppOut /* OUT: Buffer containing output changeset */ +); + + +/* +** Changegroup handle. +*/ +typedef struct sqlite3_changegroup sqlite3_changegroup; + +/* +** CAPI3REF: Combine two or more changesets into a single changeset. +** +** An sqlite3_changegroup object is used to combine two or more changesets +** (or patchsets) into a single changeset (or patchset). A single changegroup +** object may combine changesets or patchsets, but not both. The output is +** always in the same format as the input. +** +** If successful, this function returns SQLITE_OK and populates (*pp) with +** a pointer to a new sqlite3_changegroup object before returning. The caller +** should eventually free the returned object using a call to +** sqlite3changegroup_delete(). If an error occurs, an SQLite error code +** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. +** +** The usual usage pattern for an sqlite3_changegroup object is as follows: +** +**
    +**
  • It is created using a call to sqlite3changegroup_new(). +** +**
  • Zero or more changesets (or patchsets) are added to the object +** by calling sqlite3changegroup_add(). +** +**
  • The result of combining all input changesets together is obtained +** by the application via a call to sqlite3changegroup_output(). +** +**
  • The object is deleted using a call to sqlite3changegroup_delete(). +**
+** +** Any number of calls to add() and output() may be made between the calls to +** new() and delete(), and in any order. +** +** As well as the regular sqlite3changegroup_add() and +** sqlite3changegroup_output() functions, also available are the streaming +** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). +*/ +int sqlite3changegroup_new(sqlite3_changegroup **pp); + +/* +** Add all changes within the changeset (or patchset) in buffer pData (size +** nData bytes) to the changegroup. +** +** If the buffer contains a patchset, then all prior calls to this function +** on the same changegroup object must also have specified patchsets. Or, if +** the buffer contains a changeset, so must have the earlier calls to this +** function. Otherwise, SQLITE_ERROR is returned and no changes are added +** to the changegroup. +** +** Rows within the changeset and changegroup are identified by the values in +** their PRIMARY KEY columns. A change in the changeset is considered to +** apply to the same row as a change already present in the changegroup if +** the two rows have the same primary key. +** +** Changes to rows that that do not already appear in the changegroup are +** simply copied into it. Or, if both the new changeset and the changegroup +** contain changes that apply to a single row, the final contents of the +** changegroup depends on the type of each change, as follows: +** +** +** +** +**
Existing Change New Change Output Change +**
INSERT INSERT +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
INSERT UPDATE +** The INSERT change remains in the changegroup. The values in the +** INSERT change are modified as if the row was inserted by the +** existing change and then updated according to the new change. +**
INSERT DELETE +** The existing INSERT is removed from the changegroup. The DELETE is +** not added. +**
UPDATE INSERT +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
UPDATE UPDATE +** The existing UPDATE remains within the changegroup. It is amended +** so that the accompanying values are as if the row was updated once +** by the existing change and then again by the new change. +**
UPDATE DELETE +** The existing UPDATE is replaced by the new DELETE within the +** changegroup. +**
DELETE INSERT +** If one or more of the column values in the row inserted by the +** new change differ from those in the row deleted by the existing +** change, the existing DELETE is replaced by an UPDATE within the +** changegroup. Otherwise, if the inserted row is exactly the same +** as the deleted row, the existing DELETE is simply discarded. +**
DELETE UPDATE +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
DELETE DELETE +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
+** +** If the new changeset contains changes to a table that is already present +** in the changegroup, then the number of columns and the position of the +** primary key columns for the table must be consistent. If this is not the +** case, this function fails with SQLITE_SCHEMA. If the input changeset +** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is +** returned. Or, if an out-of-memory condition occurs during processing, this +** function returns SQLITE_NOMEM. In all cases, if an error occurs the +** final contents of the changegroup is undefined. +** +** If no error occurs, SQLITE_OK is returned. +*/ +int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); + +/* +** Obtain a buffer containing a changeset (or patchset) representing the +** current contents of the changegroup. If the inputs to the changegroup +** were themselves changesets, the output is a changeset. Or, if the +** inputs were patchsets, the output is also a patchset. +** +** As with the output of the sqlite3session_changeset() and +** sqlite3session_patchset() functions, all changes related to a single +** table are grouped together in the output of this function. Tables appear +** in the same order as for the very first changeset added to the changegroup. +** If the second or subsequent changesets added to the changegroup contain +** changes for tables that do not appear in the first changeset, they are +** appended onto the end of the output changeset, again in the order in +** which they are first encountered. +** +** If an error occurs, an SQLite error code is returned and the output +** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK +** is returned and the output variables are set to the size of and a +** pointer to the output buffer, respectively. In this case it is the +** responsibility of the caller to eventually free the buffer using a +** call to sqlite3_free(). +*/ +int sqlite3changegroup_output( + sqlite3_changegroup*, + int *pnData, /* OUT: Size of output buffer in bytes */ + void **ppData /* OUT: Pointer to output buffer */ +); + +/* +** Delete a changegroup object. +*/ +void sqlite3changegroup_delete(sqlite3_changegroup*); + +/* +** CAPI3REF: Apply A Changeset To A Database +** +** Apply a changeset to a database. This function attempts to update the +** "main" database attached to handle db with the changes found in the +** changeset passed via the second and third arguments. +** +** The fourth argument (xFilter) passed to this function is the "filter +** callback". If it is not NULL, then for each table affected by at least one +** change in the changeset, the filter callback is invoked with +** the table name as the second argument, and a copy of the context pointer +** passed as the sixth argument to this function as the first. If the "filter +** callback" returns zero, then no attempt is made to apply any changes to +** the table. Otherwise, if the return value is non-zero or the xFilter +** argument to this function is NULL, all changes related to the table are +** attempted. +** +** For each table that is not excluded by the filter callback, this function +** tests that the target database contains a compatible table. A table is +** considered compatible if all of the following are true: +** +**
    +**
  • The table has the same name as the name recorded in the +** changeset, and +**
  • The table has the same number of columns as recorded in the +** changeset, and +**
  • The table has primary key columns in the same position as +** recorded in the changeset. +**
+** +** If there is no compatible table, it is not an error, but none of the +** changes associated with the table are applied. A warning message is issued +** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most +** one such warning is issued for each table in the changeset. +** +** For each change for which there is a compatible table, an attempt is made +** to modify the table contents according to the UPDATE, INSERT or DELETE +** change. If a change cannot be applied cleanly, the conflict handler +** function passed as the fifth argument to sqlite3changeset_apply() may be +** invoked. A description of exactly when the conflict handler is invoked for +** each type of change is below. +** +** Unlike the xFilter argument, xConflict may not be passed NULL. The results +** of passing anything other than a valid function pointer as the xConflict +** argument are undefined. +** +** Each time the conflict handler function is invoked, it must return one +** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or +** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned +** if the second argument passed to the conflict handler is either +** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler +** returns an illegal value, any changes already made are rolled back and +** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different +** actions are taken by sqlite3changeset_apply() depending on the value +** returned by each invocation of the conflict-handler function. Refer to +** the documentation for the three +** [SQLITE_CHANGESET_OMIT|available return values] for details. +** +**
+**
DELETE Changes
+** For each DELETE change, this function checks if the target database +** contains a row with the same primary key value (or values) as the +** original row values stored in the changeset. If it does, and the values +** stored in all non-primary key columns also match the values stored in +** the changeset the row is deleted from the target database. +** +** If a row with matching primary key values is found, but one or more of +** the non-primary key fields contains a value different from the original +** row value stored in the changeset, the conflict-handler function is +** invoked with [SQLITE_CHANGESET_DATA] as the second argument. +** +** If no row with matching primary key values is found in the database, +** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] +** passed as the second argument. +** +** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT +** (which can only happen if a foreign key constraint is violated), the +** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] +** passed as the second argument. This includes the case where the DELETE +** operation is attempted because an earlier call to the conflict handler +** function returned [SQLITE_CHANGESET_REPLACE]. +** +**
INSERT Changes
+** For each INSERT change, an attempt is made to insert the new row into +** the database. +** +** If the attempt to insert the row fails because the database already +** contains a row with the same primary key values, the conflict handler +** function is invoked with the second argument set to +** [SQLITE_CHANGESET_CONFLICT]. +** +** If the attempt to insert the row fails because of some other constraint +** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is +** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. +** This includes the case where the INSERT operation is re-attempted because +** an earlier call to the conflict handler function returned +** [SQLITE_CHANGESET_REPLACE]. +** +**
UPDATE Changes
+** For each UPDATE change, this function checks if the target database +** contains a row with the same primary key value (or values) as the +** original row values stored in the changeset. If it does, and the values +** stored in all non-primary key columns also match the values stored in +** the changeset the row is updated within the target database. +** +** If a row with matching primary key values is found, but one or more of +** the non-primary key fields contains a value different from an original +** row value stored in the changeset, the conflict-handler function is +** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since +** UPDATE changes only contain values for non-primary key fields that are +** to be modified, only those fields need to match the original values to +** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. +** +** If no row with matching primary key values is found in the database, +** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] +** passed as the second argument. +** +** If the UPDATE operation is attempted, but SQLite returns +** SQLITE_CONSTRAINT, the conflict-handler function is invoked with +** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. +** This includes the case where the UPDATE operation is attempted after +** an earlier call to the conflict handler function returned +** [SQLITE_CHANGESET_REPLACE]. +**
+** +** It is safe to execute SQL statements, including those that write to the +** table that the callback related to, from within the xConflict callback. +** This can be used to further customize the applications conflict +** resolution strategy. +** +** All changes made by this function are enclosed in a savepoint transaction. +** If any other error (aside from a constraint failure when attempting to +** write to the target database) occurs, then the savepoint transaction is +** rolled back, restoring the target database to its original state, and an +** SQLite error code returned. +*/ +int sqlite3changeset_apply( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +); + +/* +** CAPI3REF: Constants Passed To The Conflict Handler +** +** Values that may be passed as the second argument to a conflict-handler. +** +**
+**
SQLITE_CHANGESET_DATA
+** The conflict handler is invoked with CHANGESET_DATA as the second argument +** when processing a DELETE or UPDATE change if a row with the required +** PRIMARY KEY fields is present in the database, but one or more other +** (non primary-key) fields modified by the update do not contain the +** expected "before" values. +** +** The conflicting row, in this case, is the database row with the matching +** primary key. +** +**
SQLITE_CHANGESET_NOTFOUND
+** The conflict handler is invoked with CHANGESET_NOTFOUND as the second +** argument when processing a DELETE or UPDATE change if a row with the +** required PRIMARY KEY fields is not present in the database. +** +** There is no conflicting row in this case. The results of invoking the +** sqlite3changeset_conflict() API are undefined. +** +**
SQLITE_CHANGESET_CONFLICT
+** CHANGESET_CONFLICT is passed as the second argument to the conflict +** handler while processing an INSERT change if the operation would result +** in duplicate primary key values. +** +** The conflicting row in this case is the database row with the matching +** primary key. +** +**
SQLITE_CHANGESET_FOREIGN_KEY
+** If foreign key handling is enabled, and applying a changeset leaves the +** database in a state containing foreign key violations, the conflict +** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument +** exactly once before the changeset is committed. If the conflict handler +** returns CHANGESET_OMIT, the changes, including those that caused the +** foreign key constraint violation, are committed. Or, if it returns +** CHANGESET_ABORT, the changeset is rolled back. +** +** No current or conflicting row information is provided. The only function +** it is possible to call on the supplied sqlite3_changeset_iter handle +** is sqlite3changeset_fk_conflicts(). +** +**
SQLITE_CHANGESET_CONSTRAINT
+** If any other constraint violation occurs while applying a change (i.e. +** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is +** invoked with CHANGESET_CONSTRAINT as the second argument. +** +** There is no conflicting row in this case. The results of invoking the +** sqlite3changeset_conflict() API are undefined. +** +**
+*/ +#define SQLITE_CHANGESET_DATA 1 +#define SQLITE_CHANGESET_NOTFOUND 2 +#define SQLITE_CHANGESET_CONFLICT 3 +#define SQLITE_CHANGESET_CONSTRAINT 4 +#define SQLITE_CHANGESET_FOREIGN_KEY 5 + +/* +** CAPI3REF: Constants Returned By The Conflict Handler +** +** A conflict handler callback must return one of the following three values. +** +**
+**
SQLITE_CHANGESET_OMIT
+** If a conflict handler returns this value no special action is taken. The +** change that caused the conflict is not applied. The session module +** continues to the next change in the changeset. +** +**
SQLITE_CHANGESET_REPLACE
+** This value may only be returned if the second argument to the conflict +** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this +** is not the case, any changes applied so far are rolled back and the +** call to sqlite3changeset_apply() returns SQLITE_MISUSE. +** +** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict +** handler, then the conflicting row is either updated or deleted, depending +** on the type of change. +** +** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict +** handler, then the conflicting row is removed from the database and a +** second attempt to apply the change is made. If this second attempt fails, +** the original row is restored to the database before continuing. +** +**
SQLITE_CHANGESET_ABORT
+** If this value is returned, any changes applied so far are rolled back +** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. +**
+*/ +#define SQLITE_CHANGESET_OMIT 0 +#define SQLITE_CHANGESET_REPLACE 1 +#define SQLITE_CHANGESET_ABORT 2 + +/* +** CAPI3REF: Streaming Versions of API functions. +** +** The six streaming API xxx_strm() functions serve similar purposes to the +** corresponding non-streaming API functions: +** +** +** +**
Streaming functionNon-streaming equivalent
sqlite3changeset_apply_str[sqlite3changeset_apply] +**
sqlite3changeset_concat_str[sqlite3changeset_concat] +**
sqlite3changeset_invert_str[sqlite3changeset_invert] +**
sqlite3changeset_start_str[sqlite3changeset_start] +**
sqlite3session_changeset_str[sqlite3session_changeset] +**
sqlite3session_patchset_str[sqlite3session_patchset] +**
+** +** Non-streaming functions that accept changesets (or patchsets) as input +** require that the entire changeset be stored in a single buffer in memory. +** Similarly, those that return a changeset or patchset do so by returning +** a pointer to a single large buffer allocated using sqlite3_malloc(). +** Normally this is convenient. However, if an application running in a +** low-memory environment is required to handle very large changesets, the +** large contiguous memory allocations required can become onerous. +** +** In order to avoid this problem, instead of a single large buffer, input +** is passed to a streaming API functions by way of a callback function that +** the sessions module invokes to incrementally request input data as it is +** required. In all cases, a pair of API function parameters such as +** +**
+**        int nChangeset,
+**        void *pChangeset,
+**  
+** +** Is replaced by: +** +**
+**        int (*xInput)(void *pIn, void *pData, int *pnData),
+**        void *pIn,
+**  
+** +** Each time the xInput callback is invoked by the sessions module, the first +** argument passed is a copy of the supplied pIn context pointer. The second +** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no +** error occurs the xInput method should copy up to (*pnData) bytes of data +** into the buffer and set (*pnData) to the actual number of bytes copied +** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) +** should be set to zero to indicate this. Or, if an error occurs, an SQLite +** error code should be returned. In all cases, if an xInput callback returns +** an error, all processing is abandoned and the streaming API function +** returns a copy of the error code to the caller. +** +** In the case of sqlite3changeset_start_strm(), the xInput callback may be +** invoked by the sessions module at any point during the lifetime of the +** iterator. If such an xInput callback returns an error, the iterator enters +** an error state, whereby all subsequent calls to iterator functions +** immediately fail with the same error code as returned by xInput. +** +** Similarly, streaming API functions that return changesets (or patchsets) +** return them in chunks by way of a callback function instead of via a +** pointer to a single large buffer. In this case, a pair of parameters such +** as: +** +**
+**        int *pnChangeset,
+**        void **ppChangeset,
+**  
+** +** Is replaced by: +** +**
+**        int (*xOutput)(void *pOut, const void *pData, int nData),
+**        void *pOut
+**  
+** +** The xOutput callback is invoked zero or more times to return data to +** the application. The first parameter passed to each call is a copy of the +** pOut pointer supplied by the application. The second parameter, pData, +** points to a buffer nData bytes in size containing the chunk of output +** data being returned. If the xOutput callback successfully processes the +** supplied data, it should return SQLITE_OK to indicate success. Otherwise, +** it should return some other SQLite error code. In this case processing +** is immediately abandoned and the streaming API function returns a copy +** of the xOutput error code to the application. +** +** The sessions module never invokes an xOutput callback with the third +** parameter set to a value less than or equal to zero. Other than this, +** no guarantees are made as to the size of the chunks of data returned. +*/ +int sqlite3changeset_apply_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +); +int sqlite3changeset_concat_strm( + int (*xInputA)(void *pIn, void *pData, int *pnData), + void *pInA, + int (*xInputB)(void *pIn, void *pData, int *pnData), + void *pInB, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changeset_invert_strm( + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changeset_start_strm( + sqlite3_changeset_iter **pp, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +); +int sqlite3session_changeset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3session_patchset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changegroup_add_strm(sqlite3_changegroup*, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +); +int sqlite3changegroup_output_strm(sqlite3_changegroup*, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); + + +/* +** Make sure we can call this stuff from C++. +*/ +#if 0 +} +#endif + +#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ + +/******** End of sqlite3session.h *********/ +/******** Begin file fts5.h *********/ +/* +** 2014 May 31 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Interfaces to extend FTS5. Using the interfaces defined in this file, +** FTS5 may be extended with: +** +** * custom tokenizers, and +** * custom auxiliary functions. +*/ + + +#ifndef _FTS5_H +#define _FTS5_H + + +#if 0 +extern "C" { +#endif + +/************************************************************************* +** CUSTOM AUXILIARY FUNCTIONS +** +** Virtual table implementations may overload SQL functions by implementing +** the sqlite3_module.xFindFunction() method. +*/ + +typedef struct Fts5ExtensionApi Fts5ExtensionApi; +typedef struct Fts5Context Fts5Context; +typedef struct Fts5PhraseIter Fts5PhraseIter; + +typedef void (*fts5_extension_function)( + const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ + Fts5Context *pFts, /* First arg to pass to pApi functions */ + sqlite3_context *pCtx, /* Context for returning result/error */ + int nVal, /* Number of values in apVal[] array */ + sqlite3_value **apVal /* Array of trailing arguments */ +); + +struct Fts5PhraseIter { + const unsigned char *a; + const unsigned char *b; +}; + +/* +** EXTENSION API FUNCTIONS +** +** xUserData(pFts): +** Return a copy of the context pointer the extension function was +** registered with. +** +** xColumnTotalSize(pFts, iCol, pnToken): +** If parameter iCol is less than zero, set output variable *pnToken +** to the total number of tokens in the FTS5 table. Or, if iCol is +** non-negative but less than the number of columns in the table, return +** the total number of tokens in column iCol, considering all rows in +** the FTS5 table. +** +** If parameter iCol is greater than or equal to the number of columns +** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. +** an OOM condition or IO error), an appropriate SQLite error code is +** returned. +** +** xColumnCount(pFts): +** Return the number of columns in the table. +** +** xColumnSize(pFts, iCol, pnToken): +** If parameter iCol is less than zero, set output variable *pnToken +** to the total number of tokens in the current row. Or, if iCol is +** non-negative but less than the number of columns in the table, set +** *pnToken to the number of tokens in column iCol of the current row. +** +** If parameter iCol is greater than or equal to the number of columns +** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. +** an OOM condition or IO error), an appropriate SQLite error code is +** returned. +** +** This function may be quite inefficient if used with an FTS5 table +** created with the "columnsize=0" option. +** +** xColumnText: +** This function attempts to retrieve the text of column iCol of the +** current document. If successful, (*pz) is set to point to a buffer +** containing the text in utf-8 encoding, (*pn) is set to the size in bytes +** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, +** if an error occurs, an SQLite error code is returned and the final values +** of (*pz) and (*pn) are undefined. +** +** xPhraseCount: +** Returns the number of phrases in the current query expression. +** +** xPhraseSize: +** Returns the number of tokens in phrase iPhrase of the query. Phrases +** are numbered starting from zero. +** +** xInstCount: +** Set *pnInst to the total number of occurrences of all phrases within +** the query within the current row. Return SQLITE_OK if successful, or +** an error code (i.e. SQLITE_NOMEM) if an error occurs. +** +** This API can be quite slow if used with an FTS5 table created with the +** "detail=none" or "detail=column" option. If the FTS5 table is created +** with either "detail=none" or "detail=column" and "content=" option +** (i.e. if it is a contentless table), then this API always returns 0. +** +** xInst: +** Query for the details of phrase match iIdx within the current row. +** Phrase matches are numbered starting from zero, so the iIdx argument +** should be greater than or equal to zero and smaller than the value +** output by xInstCount(). +** +** Usually, output parameter *piPhrase is set to the phrase number, *piCol +** to the column in which it occurs and *piOff the token offset of the +** first token of the phrase. The exception is if the table was created +** with the offsets=0 option specified. In this case *piOff is always +** set to -1. +** +** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM) +** if an error occurs. +** +** This API can be quite slow if used with an FTS5 table created with the +** "detail=none" or "detail=column" option. +** +** xRowid: +** Returns the rowid of the current row. +** +** xTokenize: +** Tokenize text using the tokenizer belonging to the FTS5 table. +** +** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback): +** This API function is used to query the FTS table for phrase iPhrase +** of the current query. Specifically, a query equivalent to: +** +** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid +** +** with $p set to a phrase equivalent to the phrase iPhrase of the +** current query is executed. Any column filter that applies to +** phrase iPhrase of the current query is included in $p. For each +** row visited, the callback function passed as the fourth argument +** is invoked. The context and API objects passed to the callback +** function may be used to access the properties of each matched row. +** Invoking Api.xUserData() returns a copy of the pointer passed as +** the third argument to pUserData. +** +** If the callback function returns any value other than SQLITE_OK, the +** query is abandoned and the xQueryPhrase function returns immediately. +** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. +** Otherwise, the error code is propagated upwards. +** +** If the query runs to completion without incident, SQLITE_OK is returned. +** Or, if some error occurs before the query completes or is aborted by +** the callback, an SQLite error code is returned. +** +** +** xSetAuxdata(pFts5, pAux, xDelete) +** +** Save the pointer passed as the second argument as the extension functions +** "auxiliary data". The pointer may then be retrieved by the current or any +** future invocation of the same fts5 extension function made as part of +** of the same MATCH query using the xGetAuxdata() API. +** +** Each extension function is allocated a single auxiliary data slot for +** each FTS query (MATCH expression). If the extension function is invoked +** more than once for a single FTS query, then all invocations share a +** single auxiliary data context. +** +** If there is already an auxiliary data pointer when this function is +** invoked, then it is replaced by the new pointer. If an xDelete callback +** was specified along with the original pointer, it is invoked at this +** point. +** +** The xDelete callback, if one is specified, is also invoked on the +** auxiliary data pointer after the FTS5 query has finished. +** +** If an error (e.g. an OOM condition) occurs within this function, an +** the auxiliary data is set to NULL and an error code returned. If the +** xDelete parameter was not NULL, it is invoked on the auxiliary data +** pointer before returning. +** +** +** xGetAuxdata(pFts5, bClear) +** +** Returns the current auxiliary data pointer for the fts5 extension +** function. See the xSetAuxdata() method for details. +** +** If the bClear argument is non-zero, then the auxiliary data is cleared +** (set to NULL) before this function returns. In this case the xDelete, +** if any, is not invoked. +** +** +** xRowCount(pFts5, pnRow) +** +** This function is used to retrieve the total number of rows in the table. +** In other words, the same value that would be returned by: +** +** SELECT count(*) FROM ftstable; +** +** xPhraseFirst() +** This function is used, along with type Fts5PhraseIter and the xPhraseNext +** method, to iterate through all instances of a single query phrase within +** the current row. This is the same information as is accessible via the +** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient +** to use, this API may be faster under some circumstances. To iterate +** through instances of phrase iPhrase, use the following code: +** +** Fts5PhraseIter iter; +** int iCol, iOff; +** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff); +** iCol>=0; +** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) +** ){ +** // An instance of phrase iPhrase at offset iOff of column iCol +** } +** +** The Fts5PhraseIter structure is defined above. Applications should not +** modify this structure directly - it should only be used as shown above +** with the xPhraseFirst() and xPhraseNext() API methods (and by +** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below). +** +** This API can be quite slow if used with an FTS5 table created with the +** "detail=none" or "detail=column" option. If the FTS5 table is created +** with either "detail=none" or "detail=column" and "content=" option +** (i.e. if it is a contentless table), then this API always iterates +** through an empty set (all calls to xPhraseFirst() set iCol to -1). +** +** xPhraseNext() +** See xPhraseFirst above. +** +** xPhraseFirstColumn() +** This function and xPhraseNextColumn() are similar to the xPhraseFirst() +** and xPhraseNext() APIs described above. The difference is that instead +** of iterating through all instances of a phrase in the current row, these +** APIs are used to iterate through the set of columns in the current row +** that contain one or more instances of a specified phrase. For example: +** +** Fts5PhraseIter iter; +** int iCol; +** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol); +** iCol>=0; +** pApi->xPhraseNextColumn(pFts, &iter, &iCol) +** ){ +** // Column iCol contains at least one instance of phrase iPhrase +** } +** +** This API can be quite slow if used with an FTS5 table created with the +** "detail=none" option. If the FTS5 table is created with either +** "detail=none" "content=" option (i.e. if it is a contentless table), +** then this API always iterates through an empty set (all calls to +** xPhraseFirstColumn() set iCol to -1). +** +** The information accessed using this API and its companion +** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext +** (or xInst/xInstCount). The chief advantage of this API is that it is +** significantly more efficient than those alternatives when used with +** "detail=column" tables. +** +** xPhraseNextColumn() +** See xPhraseFirstColumn above. +*/ +struct Fts5ExtensionApi { + int iVersion; /* Currently always set to 3 */ + + void *(*xUserData)(Fts5Context*); + + int (*xColumnCount)(Fts5Context*); + int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow); + int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken); + + int (*xTokenize)(Fts5Context*, + const char *pText, int nText, /* Text to tokenize */ + void *pCtx, /* Context passed to xToken() */ + int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ + ); + + int (*xPhraseCount)(Fts5Context*); + int (*xPhraseSize)(Fts5Context*, int iPhrase); + + int (*xInstCount)(Fts5Context*, int *pnInst); + int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff); + + sqlite3_int64 (*xRowid)(Fts5Context*); + int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn); + int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken); + + int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData, + int(*)(const Fts5ExtensionApi*,Fts5Context*,void*) + ); + int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*)); + void *(*xGetAuxdata)(Fts5Context*, int bClear); + + int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*); + void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff); + + int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); + void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); +}; + +/* +** CUSTOM AUXILIARY FUNCTIONS +*************************************************************************/ + +/************************************************************************* +** CUSTOM TOKENIZERS +** +** Applications may also register custom tokenizer types. A tokenizer +** is registered by providing fts5 with a populated instance of the +** following structure. All structure methods must be defined, setting +** any member of the fts5_tokenizer struct to NULL leads to undefined +** behaviour. The structure methods are expected to function as follows: +** +** xCreate: +** This function is used to allocate and inititalize a tokenizer instance. +** A tokenizer instance is required to actually tokenize text. +** +** The first argument passed to this function is a copy of the (void*) +** pointer provided by the application when the fts5_tokenizer object +** was registered with FTS5 (the third argument to xCreateTokenizer()). +** The second and third arguments are an array of nul-terminated strings +** containing the tokenizer arguments, if any, specified following the +** tokenizer name as part of the CREATE VIRTUAL TABLE statement used +** to create the FTS5 table. +** +** The final argument is an output variable. If successful, (*ppOut) +** should be set to point to the new tokenizer handle and SQLITE_OK +** returned. If an error occurs, some value other than SQLITE_OK should +** be returned. In this case, fts5 assumes that the final value of *ppOut +** is undefined. +** +** xDelete: +** This function is invoked to delete a tokenizer handle previously +** allocated using xCreate(). Fts5 guarantees that this function will +** be invoked exactly once for each successful call to xCreate(). +** +** xTokenize: +** This function is expected to tokenize the nText byte string indicated +** by argument pText. pText may or may not be nul-terminated. The first +** argument passed to this function is a pointer to an Fts5Tokenizer object +** returned by an earlier call to xCreate(). +** +** The second argument indicates the reason that FTS5 is requesting +** tokenization of the supplied text. This is always one of the following +** four values: +** +**
  • FTS5_TOKENIZE_DOCUMENT - A document is being inserted into +** or removed from the FTS table. The tokenizer is being invoked to +** determine the set of tokens to add to (or delete from) the +** FTS index. +** +**
  • FTS5_TOKENIZE_QUERY - A MATCH query is being executed +** against the FTS index. The tokenizer is being called to tokenize +** a bareword or quoted string specified as part of the query. +** +**
  • (FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX) - Same as +** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is +** followed by a "*" character, indicating that the last token +** returned by the tokenizer will be treated as a token prefix. +** +**
  • FTS5_TOKENIZE_AUX - The tokenizer is being invoked to +** satisfy an fts5_api.xTokenize() request made by an auxiliary +** function. Or an fts5_api.xColumnSize() request made by the same +** on a columnsize=0 database. +**
+** +** For each token in the input string, the supplied callback xToken() must +** be invoked. The first argument to it should be a copy of the pointer +** passed as the second argument to xTokenize(). The third and fourth +** arguments are a pointer to a buffer containing the token text, and the +** size of the token in bytes. The 4th and 5th arguments are the byte offsets +** of the first byte of and first byte immediately following the text from +** which the token is derived within the input. +** +** The second argument passed to the xToken() callback ("tflags") should +** normally be set to 0. The exception is if the tokenizer supports +** synonyms. In this case see the discussion below for details. +** +** FTS5 assumes the xToken() callback is invoked for each token in the +** order that they occur within the input text. +** +** If an xToken() callback returns any value other than SQLITE_OK, then +** the tokenization should be abandoned and the xTokenize() method should +** immediately return a copy of the xToken() return value. Or, if the +** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally, +** if an error occurs with the xTokenize() implementation itself, it +** may abandon the tokenization and return any error code other than +** SQLITE_OK or SQLITE_DONE. +** +** SYNONYM SUPPORT +** +** Custom tokenizers may also support synonyms. Consider a case in which a +** user wishes to query for a phrase such as "first place". Using the +** built-in tokenizers, the FTS5 query 'first + place' will match instances +** of "first place" within the document set, but not alternative forms +** such as "1st place". In some applications, it would be better to match +** all instances of "first place" or "1st place" regardless of which form +** the user specified in the MATCH query text. +** +** There are several ways to approach this in FTS5: +** +**
  1. By mapping all synonyms to a single token. In this case, the +** In the above example, this means that the tokenizer returns the +** same token for inputs "first" and "1st". Say that token is in +** fact "first", so that when the user inserts the document "I won +** 1st place" entries are added to the index for tokens "i", "won", +** "first" and "place". If the user then queries for '1st + place', +** the tokenizer substitutes "first" for "1st" and the query works +** as expected. +** +**
  2. By adding multiple synonyms for a single term to the FTS index. +** In this case, when tokenizing query text, the tokenizer may +** provide multiple synonyms for a single term within the document. +** FTS5 then queries the index for each synonym individually. For +** example, faced with the query: +** +** +** ... MATCH 'first place' +** +** the tokenizer offers both "1st" and "first" as synonyms for the +** first token in the MATCH query and FTS5 effectively runs a query +** similar to: +** +** +** ... MATCH '(first OR 1st) place' +** +** except that, for the purposes of auxiliary functions, the query +** still appears to contain just two phrases - "(first OR 1st)" +** being treated as a single phrase. +** +**
  3. By adding multiple synonyms for a single term to the FTS index. +** Using this method, when tokenizing document text, the tokenizer +** provides multiple synonyms for each token. So that when a +** document such as "I won first place" is tokenized, entries are +** added to the FTS index for "i", "won", "first", "1st" and +** "place". +** +** This way, even if the tokenizer does not provide synonyms +** when tokenizing query text (it should not - to do would be +** inefficient), it doesn't matter if the user queries for +** 'first + place' or '1st + place', as there are entires in the +** FTS index corresponding to both forms of the first token. +**
+** +** Whether it is parsing document or query text, any call to xToken that +** specifies a tflags argument with the FTS5_TOKEN_COLOCATED bit +** is considered to supply a synonym for the previous token. For example, +** when parsing the document "I won first place", a tokenizer that supports +** synonyms would call xToken() 5 times, as follows: +** +** +** xToken(pCtx, 0, "i", 1, 0, 1); +** xToken(pCtx, 0, "won", 3, 2, 5); +** xToken(pCtx, 0, "first", 5, 6, 11); +** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11); +** xToken(pCtx, 0, "place", 5, 12, 17); +** +** +** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time +** xToken() is called. Multiple synonyms may be specified for a single token +** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. +** There is no limit to the number of synonyms that may be provided for a +** single token. +** +** In many cases, method (1) above is the best approach. It does not add +** extra data to the FTS index or require FTS5 to query for multiple terms, +** so it is efficient in terms of disk space and query speed. However, it +** does not support prefix queries very well. If, as suggested above, the +** token "first" is subsituted for "1st" by the tokenizer, then the query: +** +** +** ... MATCH '1s*' +** +** will not match documents that contain the token "1st" (as the tokenizer +** will probably not map "1s" to any prefix of "first"). +** +** For full prefix support, method (3) may be preferred. In this case, +** because the index contains entries for both "first" and "1st", prefix +** queries such as 'fi*' or '1s*' will match correctly. However, because +** extra entries are added to the FTS index, this method uses more space +** within the database. +** +** Method (2) offers a midpoint between (1) and (3). Using this method, +** a query such as '1s*' will match documents that contain the literal +** token "1st", but not "first" (assuming the tokenizer is not able to +** provide synonyms for prefixes). However, a non-prefix query like '1st' +** will match against "1st" and "first". This method does not require +** extra disk space, as no extra entries are added to the FTS index. +** On the other hand, it may require more CPU cycles to run MATCH queries, +** as separate queries of the FTS index are required for each synonym. +** +** When using methods (2) or (3), it is important that the tokenizer only +** provide synonyms when tokenizing document text (method (2)) or query +** text (method (3)), not both. Doing so will not cause any errors, but is +** inefficient. +*/ +typedef struct Fts5Tokenizer Fts5Tokenizer; +typedef struct fts5_tokenizer fts5_tokenizer; +struct fts5_tokenizer { + int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); + void (*xDelete)(Fts5Tokenizer*); + int (*xTokenize)(Fts5Tokenizer*, + void *pCtx, + int flags, /* Mask of FTS5_TOKENIZE_* flags */ + const char *pText, int nText, + int (*xToken)( + void *pCtx, /* Copy of 2nd argument to xTokenize() */ + int tflags, /* Mask of FTS5_TOKEN_* flags */ + const char *pToken, /* Pointer to buffer containing token */ + int nToken, /* Size of token in bytes */ + int iStart, /* Byte offset of token within input text */ + int iEnd /* Byte offset of end of token within input text */ + ) + ); +}; + +/* Flags that may be passed as the third argument to xTokenize() */ +#define FTS5_TOKENIZE_QUERY 0x0001 +#define FTS5_TOKENIZE_PREFIX 0x0002 +#define FTS5_TOKENIZE_DOCUMENT 0x0004 +#define FTS5_TOKENIZE_AUX 0x0008 + +/* Flags that may be passed by the tokenizer implementation back to FTS5 +** as the third argument to the supplied xToken callback. */ +#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */ + +/* +** END OF CUSTOM TOKENIZERS +*************************************************************************/ + +/************************************************************************* +** FTS5 EXTENSION REGISTRATION API +*/ +typedef struct fts5_api fts5_api; +struct fts5_api { + int iVersion; /* Currently always set to 2 */ + + /* Create a new tokenizer */ + int (*xCreateTokenizer)( + fts5_api *pApi, + const char *zName, + void *pContext, + fts5_tokenizer *pTokenizer, + void (*xDestroy)(void*) + ); + + /* Find an existing tokenizer */ + int (*xFindTokenizer)( + fts5_api *pApi, + const char *zName, + void **ppContext, + fts5_tokenizer *pTokenizer + ); + + /* Create a new auxiliary function */ + int (*xCreateFunction)( + fts5_api *pApi, + const char *zName, + void *pContext, + fts5_extension_function xFunction, + void (*xDestroy)(void*) + ); +}; + +/* +** END OF REGISTRATION API +*************************************************************************/ + +#if 0 +} /* end of the 'extern "C"' block */ +#endif + +#endif /* _FTS5_H */ + + +/******** End of fts5.h *********/ + +/************** End of sqlite3.h *********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* +** Include the configuration header output by 'configure' if we're using the +** autoconf-based build +*/ +#ifdef _HAVE_SQLITE_CONFIG_H +#include "config.h" +#endif + +/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/ +/************** Begin file sqliteLimit.h *************************************/ +/* +** 2007 May 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file defines various limits of what SQLite can process. +*/ + +/* +** The maximum length of a TEXT or BLOB in bytes. This also +** limits the size of a row in a table or index. +** +** The hard limit is the ability of a 32-bit signed integer +** to count the size: 2^31-1 or 2147483647. +*/ +#ifndef SQLITE_MAX_LENGTH +# define SQLITE_MAX_LENGTH 1000000000 +#endif + +/* +** This is the maximum number of +** +** * Columns in a table +** * Columns in an index +** * Columns in a view +** * Terms in the SET clause of an UPDATE statement +** * Terms in the result set of a SELECT statement +** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. +** * Terms in the VALUES clause of an INSERT statement +** +** The hard upper limit here is 32676. Most database people will +** tell you that in a well-normalized database, you usually should +** not have more than a dozen or so columns in any table. And if +** that is the case, there is no point in having more than a few +** dozen values in any of the other situations described above. +*/ +#ifndef SQLITE_MAX_COLUMN +# define SQLITE_MAX_COLUMN 2000 +#endif + +/* +** The maximum length of a single SQL statement in bytes. +** +** It used to be the case that setting this value to zero would +** turn the limit off. That is no longer true. It is not possible +** to turn this limit off. +*/ +#ifndef SQLITE_MAX_SQL_LENGTH +# define SQLITE_MAX_SQL_LENGTH 1000000000 +#endif + +/* +** The maximum depth of an expression tree. This is limited to +** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might +** want to place more severe limits on the complexity of an +** expression. +** +** A value of 0 used to mean that the limit was not enforced. +** But that is no longer true. The limit is now strictly enforced +** at all times. +*/ +#ifndef SQLITE_MAX_EXPR_DEPTH +# define SQLITE_MAX_EXPR_DEPTH 1000 +#endif + +/* +** The maximum number of terms in a compound SELECT statement. +** The code generator for compound SELECT statements does one +** level of recursion for each term. A stack overflow can result +** if the number of terms is too large. In practice, most SQL +** never has more than 3 or 4 terms. Use a value of 0 to disable +** any limit on the number of terms in a compount SELECT. +*/ +#ifndef SQLITE_MAX_COMPOUND_SELECT +# define SQLITE_MAX_COMPOUND_SELECT 500 +#endif + +/* +** The maximum number of opcodes in a VDBE program. +** Not currently enforced. +*/ +#ifndef SQLITE_MAX_VDBE_OP +# define SQLITE_MAX_VDBE_OP 25000 +#endif + +/* +** The maximum number of arguments to an SQL function. +*/ +#ifndef SQLITE_MAX_FUNCTION_ARG +# define SQLITE_MAX_FUNCTION_ARG 127 +#endif + +/* +** The suggested maximum number of in-memory pages to use for +** the main database table and for temporary tables. +** +** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000, +** which means the cache size is limited to 2048000 bytes of memory. +** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be +** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options. +*/ +#ifndef SQLITE_DEFAULT_CACHE_SIZE +# define SQLITE_DEFAULT_CACHE_SIZE -2000 +#endif + +/* +** The default number of frames to accumulate in the log file before +** checkpointing the database in WAL mode. +*/ +#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT +# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000 +#endif + +/* +** The maximum number of attached databases. This must be between 0 +** and 125. The upper bound of 125 is because the attached databases are +** counted using a signed 8-bit integer which has a maximum value of 127 +** and we have to allow 2 extra counts for the "main" and "temp" databases. +*/ +#ifndef SQLITE_MAX_ATTACHED +# define SQLITE_MAX_ATTACHED 10 +#endif + + +/* +** The maximum value of a ?nnn wildcard that the parser will accept. +*/ +#ifndef SQLITE_MAX_VARIABLE_NUMBER +# define SQLITE_MAX_VARIABLE_NUMBER 999 +#endif + +/* Maximum page size. The upper bound on this value is 65536. This a limit +** imposed by the use of 16-bit offsets within each page. +** +** Earlier versions of SQLite allowed the user to change this value at +** compile time. This is no longer permitted, on the grounds that it creates +** a library that is technically incompatible with an SQLite library +** compiled with a different limit. If a process operating on a database +** with a page-size of 65536 bytes crashes, then an instance of SQLite +** compiled with the default page-size limit will not be able to rollback +** the aborted transaction. This could lead to database corruption. +*/ +#ifdef SQLITE_MAX_PAGE_SIZE +# undef SQLITE_MAX_PAGE_SIZE +#endif +#define SQLITE_MAX_PAGE_SIZE 65536 + + +/* +** The default size of a database page. +*/ +#ifndef SQLITE_DEFAULT_PAGE_SIZE +# define SQLITE_DEFAULT_PAGE_SIZE 4096 +#endif +#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE +# undef SQLITE_DEFAULT_PAGE_SIZE +# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE +#endif + +/* +** Ordinarily, if no value is explicitly provided, SQLite creates databases +** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain +** device characteristics (sector-size and atomic write() support), +** SQLite may choose a larger value. This constant is the maximum value +** SQLite will choose on its own. +*/ +#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE +# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192 +#endif +#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE +# undef SQLITE_MAX_DEFAULT_PAGE_SIZE +# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE +#endif + + +/* +** Maximum number of pages in one database file. +** +** This is really just the default value for the max_page_count pragma. +** This value can be lowered (or raised) at run-time using that the +** max_page_count macro. +*/ +#ifndef SQLITE_MAX_PAGE_COUNT +# define SQLITE_MAX_PAGE_COUNT 1073741823 +#endif + +/* +** Maximum length (in bytes) of the pattern in a LIKE or GLOB +** operator. +*/ +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000 +#endif + +/* +** Maximum depth of recursion for triggers. +** +** A value of 1 means that a trigger program will not be able to itself +** fire any triggers. A value of 0 means that no trigger programs at all +** may be executed. +*/ +#ifndef SQLITE_MAX_TRIGGER_DEPTH +# define SQLITE_MAX_TRIGGER_DEPTH 1000 +#endif + +/************** End of sqliteLimit.h *****************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* Disable nuisance warnings on Borland compilers */ +#if defined(__BORLANDC__) +#pragma warn -rch /* unreachable code */ +#pragma warn -ccc /* Condition is always true or false */ +#pragma warn -aus /* Assigned value is never used */ +#pragma warn -csu /* Comparing signed and unsigned */ +#pragma warn -spa /* Suspicious pointer arithmetic */ +#endif + +/* +** Include standard header files as necessary +*/ +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + +/* +** The following macros are used to cast pointers to integers and +** integers to pointers. The way you do this varies from one compiler +** to the next, so we have developed the following set of #if statements +** to generate appropriate macros for a wide range of compilers. +** +** The correct "ANSI" way to do this is to use the intptr_t type. +** Unfortunately, that typedef is not available on all compilers, or +** if it is available, it requires an #include of specific headers +** that vary from one machine to the next. +** +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). +** So we have to define the macros in different ways depending on the +** compiler. +*/ +#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X)) +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */ +# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X]) +# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0)) +#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X)) +#else /* Generates a warning - but it always works */ +# define SQLITE_INT_TO_PTR(X) ((void*)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(X)) +#endif + +/* +** A macro to hint to the compiler that a function should not be +** inlined. +*/ +#if defined(__GNUC__) +# define SQLITE_NOINLINE __attribute__((noinline)) +#elif defined(_MSC_VER) && _MSC_VER>=1310 +# define SQLITE_NOINLINE __declspec(noinline) +#else +# define SQLITE_NOINLINE +#endif + +/* +** Make sure that the compiler intrinsics we desire are enabled when +** compiling with an appropriate version of MSVC unless prevented by +** the SQLITE_DISABLE_INTRINSIC define. +*/ +#if !defined(SQLITE_DISABLE_INTRINSIC) +# if defined(_MSC_VER) && _MSC_VER>=1300 +# if !defined(_WIN32_WCE) +# include +# pragma intrinsic(_byteswap_ushort) +# pragma intrinsic(_byteswap_ulong) +# pragma intrinsic(_ReadWriteBarrier) +# else +# include +# endif +# endif +#endif + +/* +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. +** 0 means mutexes are permanently disable and the library is never +** threadsafe. 1 means the library is serialized which is the highest +** level of threadsafety. 2 means the library is multithreaded - multiple +** threads can use SQLite as long as no two threads try to use the same +** database connection at the same time. +** +** Older versions of SQLite used an optional THREADSAFE macro. +** We support that for legacy. +*/ +#if !defined(SQLITE_THREADSAFE) +# if defined(THREADSAFE) +# define SQLITE_THREADSAFE THREADSAFE +# else +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */ +# endif +#endif + +/* +** Powersafe overwrite is on by default. But can be turned off using +** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option. +*/ +#ifndef SQLITE_POWERSAFE_OVERWRITE +# define SQLITE_POWERSAFE_OVERWRITE 1 +#endif + +/* +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in +** which case memory allocation statistics are disabled by default. +*/ +#if !defined(SQLITE_DEFAULT_MEMSTATUS) +# define SQLITE_DEFAULT_MEMSTATUS 1 +#endif + +/* +** Exactly one of the following macros must be defined in order to +** specify which memory allocation subsystem to use. +** +** SQLITE_SYSTEM_MALLOC // Use normal system malloc() +** SQLITE_WIN32_MALLOC // Use Win32 native heap API +** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails +** SQLITE_MEMDEBUG // Debugging version of system malloc() +** +** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the +** assert() macro is enabled, each call into the Win32 native heap subsystem +** will cause HeapValidate to be called. If heap validation should fail, an +** assertion will be triggered. +** +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as +** the default. +*/ +#if defined(SQLITE_SYSTEM_MALLOC) \ + + defined(SQLITE_WIN32_MALLOC) \ + + defined(SQLITE_ZERO_MALLOC) \ + + defined(SQLITE_MEMDEBUG)>1 +# error "Two or more of the following compile-time configuration options\ + are defined but at most one is allowed:\ + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\ + SQLITE_ZERO_MALLOC" +#endif +#if defined(SQLITE_SYSTEM_MALLOC) \ + + defined(SQLITE_WIN32_MALLOC) \ + + defined(SQLITE_ZERO_MALLOC) \ + + defined(SQLITE_MEMDEBUG)==0 +# define SQLITE_SYSTEM_MALLOC 1 +#endif + +/* +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the +** sizes of memory allocations below this value where possible. +*/ +#if !defined(SQLITE_MALLOC_SOFT_LIMIT) +# define SQLITE_MALLOC_SOFT_LIMIT 1024 +#endif + +/* +** We need to define _XOPEN_SOURCE as follows in order to enable +** recursive mutexes on most Unix systems and fchmod() on OpenBSD. +** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit +** it. +*/ +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) +# define _XOPEN_SOURCE 600 +#endif + +/* +** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that +** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true, +** make it true by defining or undefining NDEBUG. +** +** Setting NDEBUG makes the code smaller and faster by disabling the +** assert() statements in the code. So we want the default action +** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG +** is set. Thus NDEBUG becomes an opt-in rather than an opt-out +** feature. +*/ +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif +#if defined(NDEBUG) && defined(SQLITE_DEBUG) +# undef NDEBUG +#endif + +/* +** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on. +*/ +#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG) +# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1 +#endif + +/* +** The testcase() macro is used to aid in coverage testing. When +** doing coverage testing, the condition inside the argument to +** testcase() must be evaluated both true and false in order to +** get full branch coverage. The testcase() macro is inserted +** to help ensure adequate test coverage in places where simple +** condition/decision coverage is inadequate. For example, testcase() +** can be used to make sure boundary values are tested. For +** bitmask tests, testcase() can be used to make sure each bit +** is significant and used at least once. On switch statements +** where multiple cases go to the same block of code, testcase() +** can insure that all cases are evaluated. +** +*/ +#ifdef SQLITE_COVERAGE_TEST +SQLITE_PRIVATE void sqlite3Coverage(int); +# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); } +#else +# define testcase(X) +#endif + +/* +** The TESTONLY macro is used to enclose variable declarations or +** other bits of code that are needed to support the arguments +** within testcase() and assert() macros. +*/ +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) +# define TESTONLY(X) X +#else +# define TESTONLY(X) +#endif + +/* +** Sometimes we need a small amount of code such as a variable initialization +** to setup for a later assert() statement. We do not want this code to +** appear when assert() is disabled. The following macro is therefore +** used to contain that setup code. The "VVA" acronym stands for +** "Verification, Validation, and Accreditation". In other words, the +** code within VVA_ONLY() will only run during verification processes. +*/ +#ifndef NDEBUG +# define VVA_ONLY(X) X +#else +# define VVA_ONLY(X) +#endif + +/* +** The ALWAYS and NEVER macros surround boolean expressions which +** are intended to always be true or false, respectively. Such +** expressions could be omitted from the code completely. But they +** are included in a few cases in order to enhance the resilience +** of SQLite to unexpected behavior - to make the code "self-healing" +** or "ductile" rather than being "brittle" and crashing at the first +** hint of unplanned behavior. +** +** In other words, ALWAYS and NEVER are added for defensive code. +** +** When doing coverage testing ALWAYS and NEVER are hard-coded to +** be true and false so that the unreachable code they specify will +** not be counted as untested code. +*/ +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) +#else +# define ALWAYS(X) (X) +# define NEVER(X) (X) +#endif + +/* +** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is +** defined. We need to defend against those failures when testing with +** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches +** during a normal build. The following macro can be used to disable tests +** that are always false except when SQLITE_TEST_REALLOC_STRESS is set. +*/ +#if defined(SQLITE_TEST_REALLOC_STRESS) +# define ONLY_IF_REALLOC_STRESS(X) (X) +#elif !defined(NDEBUG) +# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0) +#else +# define ONLY_IF_REALLOC_STRESS(X) (0) +#endif + +/* +** Declarations used for tracing the operating system interfaces. +*/ +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \ + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) + extern int sqlite3OSTrace; +# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X +# define SQLITE_HAVE_OS_TRACE +#else +# define OSTRACE(X) +# undef SQLITE_HAVE_OS_TRACE +#endif + +/* +** Is the sqlite3ErrName() function needed in the build? Currently, +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when +** OSTRACE is enabled), and by several "test*.c" files (which are +** compiled using SQLITE_TEST). +*/ +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \ + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) +# define SQLITE_NEED_ERR_NAME +#else +# undef SQLITE_NEED_ERR_NAME +#endif + +/* +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN +*/ +#ifdef SQLITE_OMIT_EXPLAIN +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS +#endif + +/* +** Return true (non-zero) if the input is an integer that is too large +** to fit in 32-bits. This macro is used inside of various testcase() +** macros to verify that we have tested SQLite for large-file support. +*/ +#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0) + +/* +** The macro unlikely() is a hint that surrounds a boolean +** expression that is usually false. Macro likely() surrounds +** a boolean expression that is usually true. These hints could, +** in theory, be used by the compiler to generate better code, but +** currently they are just comments for human readers. +*/ +#define likely(X) (X) +#define unlikely(X) (X) + +/************** Include hash.h in the middle of sqliteInt.h ******************/ +/************** Begin file hash.h ********************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the header file for the generic hash-table implementation +** used in SQLite. +*/ +#ifndef _SQLITE_HASH_H_ +#define _SQLITE_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct Hash Hash; +typedef struct HashElem HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, some of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +** +** All elements of the hash table are on a single doubly-linked list. +** Hash.first points to the head of this list. +** +** There are Hash.htsize buckets. Each bucket points to a spot in +** the global doubly-linked list. The contents of the bucket are the +** element pointed to plus the next _ht.count-1 elements in the list. +** +** Hash.htsize and Hash.ht may be zero. In that case lookup is done +** by a linear search of the global list. For small tables, the +** Hash.ht table is never allocated because if there are few elements +** in the table, it is faster to do a linear search than to manage +** the hash table. +*/ +struct Hash { + unsigned int htsize; /* Number of buckets in the hash table */ + unsigned int count; /* Number of entries in this table */ + HashElem *first; /* The first element of the array */ + struct _ht { /* the hash table */ + int count; /* Number of entries with this hash */ + HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. +** +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct HashElem { + HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + const char *pKey; /* Key associated with this element */ +}; + +/* +** Access routines. To delete, insert a NULL pointer. +*/ +SQLITE_PRIVATE void sqlite3HashInit(Hash*); +SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData); +SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey); +SQLITE_PRIVATE void sqlite3HashClear(Hash*); + +/* +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** Hash h; +** HashElem *p; +** ... +** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ +** SomeStructure *pData = sqliteHashData(p); +** // do something with pData +** } +*/ +#define sqliteHashFirst(H) ((H)->first) +#define sqliteHashNext(E) ((E)->next) +#define sqliteHashData(E) ((E)->data) +/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */ +/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */ + +/* +** Number of entries in a hash table +*/ +/* #define sqliteHashCount(H) ((H)->count) // NOT USED */ + +#endif /* _SQLITE_HASH_H_ */ + +/************** End of hash.h ************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include parse.h in the middle of sqliteInt.h *****************/ +/************** Begin file parse.h *******************************************/ +#define TK_SEMI 1 +#define TK_EXPLAIN 2 +#define TK_QUERY 3 +#define TK_PLAN 4 +#define TK_BEGIN 5 +#define TK_TRANSACTION 6 +#define TK_DEFERRED 7 +#define TK_IMMEDIATE 8 +#define TK_EXCLUSIVE 9 +#define TK_COMMIT 10 +#define TK_END 11 +#define TK_ROLLBACK 12 +#define TK_SAVEPOINT 13 +#define TK_RELEASE 14 +#define TK_TO 15 +#define TK_TABLE 16 +#define TK_CREATE 17 +#define TK_IF 18 +#define TK_NOT 19 +#define TK_EXISTS 20 +#define TK_TEMP 21 +#define TK_LP 22 +#define TK_RP 23 +#define TK_AS 24 +#define TK_WITHOUT 25 +#define TK_COMMA 26 +#define TK_OR 27 +#define TK_AND 28 +#define TK_IS 29 +#define TK_MATCH 30 +#define TK_LIKE_KW 31 +#define TK_BETWEEN 32 +#define TK_IN 33 +#define TK_ISNULL 34 +#define TK_NOTNULL 35 +#define TK_NE 36 +#define TK_EQ 37 +#define TK_GT 38 +#define TK_LE 39 +#define TK_LT 40 +#define TK_GE 41 +#define TK_ESCAPE 42 +#define TK_BITAND 43 +#define TK_BITOR 44 +#define TK_LSHIFT 45 +#define TK_RSHIFT 46 +#define TK_PLUS 47 +#define TK_MINUS 48 +#define TK_STAR 49 +#define TK_SLASH 50 +#define TK_REM 51 +#define TK_CONCAT 52 +#define TK_COLLATE 53 +#define TK_BITNOT 54 +#define TK_ID 55 +#define TK_INDEXED 56 +#define TK_ABORT 57 +#define TK_ACTION 58 +#define TK_AFTER 59 +#define TK_ANALYZE 60 +#define TK_ASC 61 +#define TK_ATTACH 62 +#define TK_BEFORE 63 +#define TK_BY 64 +#define TK_CASCADE 65 +#define TK_CAST 66 +#define TK_COLUMNKW 67 +#define TK_CONFLICT 68 +#define TK_DATABASE 69 +#define TK_DESC 70 +#define TK_DETACH 71 +#define TK_EACH 72 +#define TK_FAIL 73 +#define TK_FOR 74 +#define TK_IGNORE 75 +#define TK_INITIALLY 76 +#define TK_INSTEAD 77 +#define TK_NO 78 +#define TK_KEY 79 +#define TK_OF 80 +#define TK_OFFSET 81 +#define TK_PRAGMA 82 +#define TK_RAISE 83 +#define TK_RECURSIVE 84 +#define TK_REPLACE 85 +#define TK_RESTRICT 86 +#define TK_ROW 87 +#define TK_TRIGGER 88 +#define TK_VACUUM 89 +#define TK_VIEW 90 +#define TK_VIRTUAL 91 +#define TK_WITH 92 +#define TK_REINDEX 93 +#define TK_RENAME 94 +#define TK_CTIME_KW 95 +#define TK_ANY 96 +#define TK_STRING 97 +#define TK_JOIN_KW 98 +#define TK_CONSTRAINT 99 +#define TK_DEFAULT 100 +#define TK_NULL 101 +#define TK_PRIMARY 102 +#define TK_UNIQUE 103 +#define TK_CHECK 104 +#define TK_REFERENCES 105 +#define TK_AUTOINCR 106 +#define TK_ON 107 +#define TK_INSERT 108 +#define TK_DELETE 109 +#define TK_UPDATE 110 +#define TK_SET 111 +#define TK_DEFERRABLE 112 +#define TK_FOREIGN 113 +#define TK_DROP 114 +#define TK_UNION 115 +#define TK_ALL 116 +#define TK_EXCEPT 117 +#define TK_INTERSECT 118 +#define TK_SELECT 119 +#define TK_VALUES 120 +#define TK_DISTINCT 121 +#define TK_DOT 122 +#define TK_FROM 123 +#define TK_JOIN 124 +#define TK_USING 125 +#define TK_ORDER 126 +#define TK_GROUP 127 +#define TK_HAVING 128 +#define TK_LIMIT 129 +#define TK_WHERE 130 +#define TK_INTO 131 +#define TK_INTEGER 132 +#define TK_FLOAT 133 +#define TK_BLOB 134 +#define TK_VARIABLE 135 +#define TK_CASE 136 +#define TK_WHEN 137 +#define TK_THEN 138 +#define TK_ELSE 139 +#define TK_INDEX 140 +#define TK_ALTER 141 +#define TK_ADD 142 +#define TK_TO_TEXT 143 +#define TK_TO_BLOB 144 +#define TK_TO_NUMERIC 145 +#define TK_TO_INT 146 +#define TK_TO_REAL 147 +#define TK_ISNOT 148 +#define TK_END_OF_FILE 149 +#define TK_UNCLOSED_STRING 150 +#define TK_FUNCTION 151 +#define TK_COLUMN 152 +#define TK_AGG_FUNCTION 153 +#define TK_AGG_COLUMN 154 +#define TK_UMINUS 155 +#define TK_UPLUS 156 +#define TK_REGISTER 157 +#define TK_ASTERISK 158 +#define TK_SPAN 159 +#define TK_SPACE 160 +#define TK_ILLEGAL 161 + +/* The token codes above must all fit in 8 bits */ +#define TKFLG_MASK 0xff + +/* Flags that can be added to a token code when it is not +** being stored in a u8: */ +#define TKFLG_DONTFOLD 0x100 /* Omit constant folding optimizations */ + +/************** End of parse.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +#include +#include +#include +#include +#include + +/* +** If compiling for a processor that lacks floating point support, +** substitute integer for floating-point +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# define double sqlite_int64 +# define float sqlite_int64 +# define LONGDOUBLE_TYPE sqlite_int64 +# ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) +# endif +# define SQLITE_OMIT_DATETIME_FUNCS 1 +# define SQLITE_OMIT_TRACE 1 +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT +# undef SQLITE_HAVE_ISNAN +#endif +#ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (1e99) +#endif + +/* +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 +** afterward. Having this macro allows us to cause the C compiler +** to omit code used by TEMP tables without messy #ifndef statements. +*/ +#ifdef SQLITE_OMIT_TEMPDB +#define OMIT_TEMPDB 1 +#else +#define OMIT_TEMPDB 0 +#endif + +/* +** The "file format" number is an integer that is incremented whenever +** the VDBE-level file format changes. The following macros define the +** the default file format for new databases and the maximum file format +** that the library can read. +*/ +#define SQLITE_MAX_FILE_FORMAT 4 +#ifndef SQLITE_DEFAULT_FILE_FORMAT +# define SQLITE_DEFAULT_FILE_FORMAT 4 +#endif + +/* +** Determine whether triggers are recursive by default. This can be +** changed at run-time using a pragma. +*/ +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0 +#endif + +/* +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified +** on the command-line +*/ +#ifndef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 1 +# define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */ +#endif + +/* +** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it +** to zero. +*/ +#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0 +# undef SQLITE_MAX_WORKER_THREADS +# define SQLITE_MAX_WORKER_THREADS 0 +#endif +#ifndef SQLITE_MAX_WORKER_THREADS +# define SQLITE_MAX_WORKER_THREADS 8 +#endif +#ifndef SQLITE_DEFAULT_WORKER_THREADS +# define SQLITE_DEFAULT_WORKER_THREADS 0 +#endif +#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS +# undef SQLITE_MAX_WORKER_THREADS +# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS +#endif + +/* +** The default initial allocation for the pagecache when using separate +** pagecaches for each database connection. A positive number is the +** number of pages. A negative number N translations means that a buffer +** of -1024*N bytes is allocated and used for as many pages as it will hold. +*/ +#ifndef SQLITE_DEFAULT_PCACHE_INITSZ +# define SQLITE_DEFAULT_PCACHE_INITSZ 100 +#endif + +/* +** GCC does not define the offsetof() macro so we'll have to do it +** ourselves. +*/ +#ifndef offsetof +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) +#endif + +/* +** Macros to compute minimum and maximum of two numbers. +*/ +#ifndef MIN +# define MIN(A,B) ((A)<(B)?(A):(B)) +#endif +#ifndef MAX +# define MAX(A,B) ((A)>(B)?(A):(B)) +#endif + +/* +** Swap two objects of type TYPE. +*/ +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} + +/* +** Check to see if this machine uses EBCDIC. (Yes, believe it or +** not, there are still machines out there that use EBCDIC.) +*/ +#if 'A' == '\301' +# define SQLITE_EBCDIC 1 +#else +# define SQLITE_ASCII 1 +#endif + +/* +** Integers of known sizes. These typedefs might change for architectures +** where the sizes very. Preprocessor macros are available so that the +** types can be conveniently redefined at compile-type. Like this: +** +** cc '-DUINTPTR_TYPE=long long int' ... +*/ +#ifndef UINT32_TYPE +# ifdef HAVE_UINT32_T +# define UINT32_TYPE uint32_t +# else +# define UINT32_TYPE unsigned int +# endif +#endif +#ifndef UINT16_TYPE +# ifdef HAVE_UINT16_T +# define UINT16_TYPE uint16_t +# else +# define UINT16_TYPE unsigned short int +# endif +#endif +#ifndef INT16_TYPE +# ifdef HAVE_INT16_T +# define INT16_TYPE int16_t +# else +# define INT16_TYPE short int +# endif +#endif +#ifndef UINT8_TYPE +# ifdef HAVE_UINT8_T +# define UINT8_TYPE uint8_t +# else +# define UINT8_TYPE unsigned char +# endif +#endif +#ifndef INT8_TYPE +# ifdef HAVE_INT8_T +# define INT8_TYPE int8_t +# else +# define INT8_TYPE signed char +# endif +#endif +#ifndef LONGDOUBLE_TYPE +# define LONGDOUBLE_TYPE long double +#endif +typedef sqlite_int64 i64; /* 8-byte signed integer */ +typedef sqlite_uint64 u64; /* 8-byte unsigned integer */ +typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ +typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ +typedef INT16_TYPE i16; /* 2-byte signed integer */ +typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ +typedef INT8_TYPE i8; /* 1-byte signed integer */ + +/* +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value +** that can be stored in a u32 without loss of data. The value +** is 0x00000000ffffffff. But because of quirks of some compilers, we +** have to specify the value in the less intuitive manner shown: +*/ +#define SQLITE_MAX_U32 ((((u64)1)<<32)-1) + +/* +** The datatype used to store estimates of the number of rows in a +** table or index. This is an unsigned integer type. For 99.9% of +** the world, a 32-bit integer is sufficient. But a 64-bit integer +** can be used at compile-time if desired. +*/ +#ifdef SQLITE_64BIT_STATS + typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */ +#else + typedef u32 tRowcnt; /* 32-bit is the default */ +#endif + +/* +** Estimated quantities used for query planning are stored as 16-bit +** logarithms. For quantity X, the value stored is 10*log2(X). This +** gives a possible range of values of approximately 1.0e986 to 1e-986. +** But the allowed values are "grainy". Not every value is representable. +** For example, quantities 16 and 17 are both represented by a LogEst +** of 40. However, since LogEst quantities are suppose to be estimates, +** not exact values, this imprecision is not a problem. +** +** "LogEst" is short for "Logarithmic Estimate". +** +** Examples: +** 1 -> 0 20 -> 43 10000 -> 132 +** 2 -> 10 25 -> 46 25000 -> 146 +** 3 -> 16 100 -> 66 1000000 -> 199 +** 4 -> 20 1000 -> 99 1048576 -> 200 +** 10 -> 33 1024 -> 100 4294967296 -> 320 +** +** The LogEst can be negative to indicate fractional values. +** Examples: +** +** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 +*/ +typedef INT16_TYPE LogEst; + +/* +** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer +*/ +#ifndef SQLITE_PTRSIZE +# if defined(__SIZEOF_POINTER__) +# define SQLITE_PTRSIZE __SIZEOF_POINTER__ +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(_M_ARM) || defined(__arm__) || defined(__x86) +# define SQLITE_PTRSIZE 4 +# else +# define SQLITE_PTRSIZE 8 +# endif +#endif + +/* The uptr type is an unsigned integer large enough to hold a pointer +*/ +#if defined(HAVE_STDINT_H) + typedef uintptr_t uptr; +#elif SQLITE_PTRSIZE==4 + typedef u32 uptr; +#else + typedef u64 uptr; +#endif + +/* +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to +** something between S (inclusive) and E (exclusive). +** +** In other words, S is a buffer and E is a pointer to the first byte after +** the end of buffer S. This macro returns true if P points to something +** contained within the buffer S. +*/ +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) + + +/* +** Macros to determine whether the machine is big or little endian, +** and whether or not that determination is run-time or compile-time. +** +** For best performance, an attempt is made to guess at the byte-order +** using C-preprocessor macros. If that is unsuccessful, or if +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined +** at run-time. +*/ +#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ + defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER) +# define SQLITE_BYTEORDER 1234 +# define SQLITE_BIGENDIAN 0 +# define SQLITE_LITTLEENDIAN 1 +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE +#endif +#if (defined(sparc) || defined(__ppc__)) \ + && !defined(SQLITE_RUNTIME_BYTEORDER) +# define SQLITE_BYTEORDER 4321 +# define SQLITE_BIGENDIAN 1 +# define SQLITE_LITTLEENDIAN 0 +# define SQLITE_UTF16NATIVE SQLITE_UTF16BE +#endif +#if !defined(SQLITE_BYTEORDER) +# ifdef SQLITE_AMALGAMATION + const int sqlite3one = 1; +# else + extern const int sqlite3one; +# endif +# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ +# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) +#endif + +/* +** Constants for the largest and smallest possible 64-bit signed integers. +** These macros are designed to work correctly on both 32-bit and 64-bit +** compilers. +*/ +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) + +/* +** Round up a number to the next larger multiple of 8. This is used +** to force 8-byte alignment on 64-bit architectures. +*/ +#define ROUND8(x) (((x)+7)&~7) + +/* +** Round down to the nearest multiple of 8 +*/ +#define ROUNDDOWN8(x) ((x)&~7) + +/* +** Assert that the pointer X is aligned to an 8-byte boundary. This +** macro is used only within assert() to verify that the code gets +** all alignment restrictions correct. +** +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the +** underlying malloc() implementation might return us 4-byte aligned +** pointers. In that case, only verify 4-byte alignment. +*/ +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0) +#else +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0) +#endif + +/* +** Disable MMAP on platforms where it is known to not work +*/ +#if defined(__OpenBSD__) || defined(__QNXNTO__) +# undef SQLITE_MAX_MMAP_SIZE +# define SQLITE_MAX_MMAP_SIZE 0 +#endif + +/* +** Default maximum size of memory used by memory-mapped I/O in the VFS +*/ +#ifdef __APPLE__ +# include +#endif +#ifndef SQLITE_MAX_MMAP_SIZE +# if defined(__linux__) \ + || defined(_WIN32) \ + || (defined(__APPLE__) && defined(__MACH__)) \ + || defined(__sun) \ + || defined(__FreeBSD__) \ + || defined(__DragonFly__) +# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */ +# else +# define SQLITE_MAX_MMAP_SIZE 0 +# endif +# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */ +#endif + +/* +** The default MMAP_SIZE is zero on all platforms. Or, even if a larger +** default MMAP_SIZE is specified at compile-time, make sure that it does +** not exceed the maximum mmap size. +*/ +#ifndef SQLITE_DEFAULT_MMAP_SIZE +# define SQLITE_DEFAULT_MMAP_SIZE 0 +# define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */ +#endif +#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE +# undef SQLITE_DEFAULT_MMAP_SIZE +# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE +#endif + +/* +** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined. +** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also +** define SQLITE_ENABLE_STAT3_OR_STAT4 +*/ +#ifdef SQLITE_ENABLE_STAT4 +# undef SQLITE_ENABLE_STAT3 +# define SQLITE_ENABLE_STAT3_OR_STAT4 1 +#elif SQLITE_ENABLE_STAT3 +# define SQLITE_ENABLE_STAT3_OR_STAT4 1 +#elif SQLITE_ENABLE_STAT3_OR_STAT4 +# undef SQLITE_ENABLE_STAT3_OR_STAT4 +#endif + +/* +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not +** the Select query generator tracing logic is turned on. +*/ +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) +# define SELECTTRACE_ENABLED 1 +#else +# define SELECTTRACE_ENABLED 0 +#endif + +/* +** An instance of the following structure is used to store the busy-handler +** callback for a given sqlite handle. +** +** The sqlite.busyHandler member of the sqlite struct contains the busy +** callback for the database handle. Each pager opened via the sqlite +** handle is passed a pointer to sqlite.busyHandler. The busy-handler +** callback is currently invoked only from within pager.c. +*/ +typedef struct BusyHandler BusyHandler; +struct BusyHandler { + int (*xFunc)(void *,int); /* The busy callback */ + void *pArg; /* First arg to busy callback */ + int nBusy; /* Incremented with each busy call */ +}; + +/* +** Name of the master database table. The master database table +** is a special table that holds the names and attributes of all +** user tables and indices. +*/ +#define MASTER_NAME "sqlite_master" +#define TEMP_MASTER_NAME "sqlite_temp_master" + +/* +** The root-page of the master database table. +*/ +#define MASTER_ROOT 1 + +/* +** The name of the schema table. +*/ +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) + +/* +** A convenience macro that returns the number of elements in +** an array. +*/ +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0]))) + +/* +** Determine if the argument is a power of two +*/ +#define IsPowerOfTwo(X) (((X)&((X)-1))==0) + +/* +** The following value as a destructor means to use sqlite3DbFree(). +** The sqlite3DbFree() routine requires two parameters instead of the +** one parameter that destructors normally want. So we have to introduce +** this magic value that the code knows to handle differently. Any +** pointer will work here as long as it is distinct from SQLITE_STATIC +** and SQLITE_TRANSIENT. +*/ +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize) + +/* +** When SQLITE_OMIT_WSD is defined, it means that the target platform does +** not support Writable Static Data (WSD) such as global and static variables. +** All variables must either be on the stack or dynamically allocated from +** the heap. When WSD is unsupported, the variable declarations scattered +** throughout the SQLite code must become constants instead. The SQLITE_WSD +** macro is used for this purpose. And instead of referencing the variable +** directly, we use its constant as a key to lookup the run-time allocated +** buffer that holds real variable. The constant is also the initializer +** for the run-time allocated buffer. +** +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL +** macros become no-ops and have zero performance impact. +*/ +#ifdef SQLITE_OMIT_WSD + #define SQLITE_WSD const + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v))) + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config) +SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J); +SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L); +#else + #define SQLITE_WSD + #define GLOBAL(t,v) v + #define sqlite3GlobalConfig sqlite3Config +#endif + +/* +** The following macros are used to suppress compiler warnings and to +** make it clear to human readers when a function parameter is deliberately +** left unused within the body of a function. This usually happens when +** a function is called via a function pointer. For example the +** implementation of an SQL aggregate step callback may not use the +** parameter indicating the number of arguments passed to the aggregate, +** if it knows that this is enforced elsewhere. +** +** When a function parameter is not used at all within the body of a function, +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer. +** However, these macros may also be used to suppress warnings related to +** parameters that may or may not be used depending on compilation options. +** For example those parameters only used in assert() statements. In these +** cases the parameters are named as per the usual conventions. +*/ +#define UNUSED_PARAMETER(x) (void)(x) +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y) + +/* +** Forward references to structures +*/ +typedef struct AggInfo AggInfo; +typedef struct AuthContext AuthContext; +typedef struct AutoincInfo AutoincInfo; +typedef struct Bitvec Bitvec; +typedef struct CollSeq CollSeq; +typedef struct Column Column; +typedef struct Db Db; +typedef struct Schema Schema; +typedef struct Expr Expr; +typedef struct ExprList ExprList; +typedef struct ExprSpan ExprSpan; +typedef struct FKey FKey; +typedef struct FuncDestructor FuncDestructor; +typedef struct FuncDef FuncDef; +typedef struct FuncDefHash FuncDefHash; +typedef struct IdList IdList; +typedef struct Index Index; +typedef struct IndexSample IndexSample; +typedef struct KeyClass KeyClass; +typedef struct KeyInfo KeyInfo; +typedef struct Lookaside Lookaside; +typedef struct LookasideSlot LookasideSlot; +typedef struct Module Module; +typedef struct NameContext NameContext; +typedef struct Parse Parse; +typedef struct PreUpdate PreUpdate; +typedef struct PrintfArguments PrintfArguments; +typedef struct RowSet RowSet; +typedef struct Savepoint Savepoint; +typedef struct Select Select; +typedef struct SQLiteThread SQLiteThread; +typedef struct SelectDest SelectDest; +typedef struct SrcList SrcList; +typedef struct StrAccum StrAccum; +typedef struct Table Table; +typedef struct TableLock TableLock; +typedef struct Token Token; +typedef struct TreeView TreeView; +typedef struct Trigger Trigger; +typedef struct TriggerPrg TriggerPrg; +typedef struct TriggerStep TriggerStep; +typedef struct UnpackedRecord UnpackedRecord; +typedef struct VTable VTable; +typedef struct VtabCtx VtabCtx; +typedef struct Walker Walker; +typedef struct WhereInfo WhereInfo; +typedef struct With With; + +/* +** Defer sourcing vdbe.h and btree.h until after the "u8" and +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque +** pointer types (i.e. FuncDef) defined above. +*/ +/************** Include btree.h in the middle of sqliteInt.h *****************/ +/************** Begin file btree.h *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite B-Tree file +** subsystem. See comments in the source code for a detailed description +** of what each interface routine does. +*/ +#ifndef _BTREE_H_ +#define _BTREE_H_ + +/* TODO: This definition is just included so other modules compile. It +** needs to be revisited. +*/ +#define SQLITE_N_BTREE_META 16 + +/* +** If defined as non-zero, auto-vacuum is enabled by default. Otherwise +** it must be turned on for each database using "PRAGMA auto_vacuum = 1". +*/ +#ifndef SQLITE_DEFAULT_AUTOVACUUM + #define SQLITE_DEFAULT_AUTOVACUUM 0 +#endif + +#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */ +#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */ +#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */ + +/* +** Forward declarations of structure +*/ +typedef struct Btree Btree; +typedef struct BtCursor BtCursor; +typedef struct BtShared BtShared; + + +SQLITE_PRIVATE int sqlite3BtreeOpen( + sqlite3_vfs *pVfs, /* VFS to use with this b-tree */ + const char *zFilename, /* Name of database file to open */ + sqlite3 *db, /* Associated database connection */ + Btree **ppBtree, /* Return open Btree* here */ + int flags, /* Flags */ + int vfsFlags /* Flags passed through to VFS open */ +); + +/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the +** following values. +** +** NOTE: These values must match the corresponding PAGER_ values in +** pager.h. +*/ +#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ +#define BTREE_MEMORY 2 /* This is an in-memory DB */ +#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */ +#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */ + +SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int); +#if SQLITE_MAX_MMAP_SIZE>0 +SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64); +#endif +SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned); +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*); +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int); +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*); +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p); +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int); +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int); +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*); +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int); +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags); +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*); +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*); +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*); +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree); +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int); + +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *); +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *); +SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *); + +SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *); + +/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR +** of the flags shown below. +** +** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. +** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data +** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With +** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored +** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL +** indices.) +*/ +#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ +#define BTREE_BLOBKEY 2 /* Table has keys only - no data */ + +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int); + +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); + +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p); + +/* +** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta +** should be one of the following values. The integer values are assigned +** to constants so that the offset of the corresponding field in an +** SQLite database header may be found using the following formula: +** +** offset = 36 + (idx * 4) +** +** For example, the free-page-count field is located at byte offset 36 of +** the database file header. The incr-vacuum-flag field is located at +** byte offset 64 (== 36+4*7). +** +** The BTREE_DATA_VERSION value is not really a value stored in the header. +** It is a read-only number computed by the pager. But we merge it with +** the header value access routines since its access pattern is the same. +** Call it a "virtual meta value". +*/ +#define BTREE_FREE_PAGE_COUNT 0 +#define BTREE_SCHEMA_VERSION 1 +#define BTREE_FILE_FORMAT 2 +#define BTREE_DEFAULT_CACHE_SIZE 3 +#define BTREE_LARGEST_ROOT_PAGE 4 +#define BTREE_TEXT_ENCODING 5 +#define BTREE_USER_VERSION 6 +#define BTREE_INCR_VACUUM 7 +#define BTREE_APPLICATION_ID 8 +#define BTREE_DATA_VERSION 15 /* A virtual meta-value */ + +/* +** Kinds of hints that can be passed into the sqlite3BtreeCursorHint() +** interface. +** +** BTREE_HINT_RANGE (arguments: Expr*, Mem*) +** +** The first argument is an Expr* (which is guaranteed to be constant for +** the lifetime of the cursor) that defines constraints on which rows +** might be fetched with this cursor. The Expr* tree may contain +** TK_REGISTER nodes that refer to values stored in the array of registers +** passed as the second parameter. In other words, if Expr.op==TK_REGISTER +** then the value of the node is the value in Mem[pExpr.iTable]. Any +** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th +** column of the b-tree of the cursor. The Expr tree will not contain +** any function calls nor subqueries nor references to b-trees other than +** the cursor being hinted. +** +** The design of the _RANGE hint is aid b-tree implementations that try +** to prefetch content from remote machines - to provide those +** implementations with limits on what needs to be prefetched and thereby +** reduce network bandwidth. +** +** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by +** standard SQLite. The other hints are provided for extentions that use +** the SQLite parser and code generator but substitute their own storage +** engine. +*/ +#define BTREE_HINT_RANGE 0 /* Range constraints on queries */ + +/* +** Values that may be OR'd together to form the argument to the +** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint(): +** +** The BTREE_BULKLOAD flag is set on index cursors when the index is going +** to be filled with content that is already in sorted order. +** +** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or +** OP_SeekLE opcodes for a range search, but where the range of entries +** selected will all have the same key. In other words, the cursor will +** be used only for equality key searches. +** +*/ +#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */ +#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */ + +/* +** Flags passed as the third argument to sqlite3BtreeCursor(). +** +** For read-only cursors the wrFlag argument is always zero. For read-write +** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just +** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will +** only be used by SQLite for the following: +** +** * to seek to and then delete specific entries, and/or +** +** * to read values that will be used to create keys that other +** BTREE_FORDELETE cursors will seek to and delete. +** +** The BTREE_FORDELETE flag is an optimization hint. It is not used by +** by this, the native b-tree engine of SQLite, but it is available to +** alternative storage engines that might be substituted in place of this +** b-tree system. For alternative storage engines in which a delete of +** the main table row automatically deletes corresponding index rows, +** the FORDELETE flag hint allows those alternative storage engines to +** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK +** and DELETE operations as no-ops, and any READ operation against a +** FORDELETE cursor may return a null row: 0x01 0x00. +*/ +#define BTREE_WRCSR 0x00000004 /* read-write cursor */ +#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */ + +SQLITE_PRIVATE int sqlite3BtreeCursor( + Btree*, /* BTree containing table to open */ + int iTable, /* Index of root page */ + int wrFlag, /* 1 for writing. 0 for read-only */ + struct KeyInfo*, /* First argument to compare function */ + BtCursor *pCursor /* Space to write cursor structure */ +); +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void); +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*); +SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned); +#ifdef SQLITE_ENABLE_CURSOR_HINTS +SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...); +#endif + +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + BtCursor*, + UnpackedRecord *pUnKey, + i64 intKey, + int bias, + int *pRes +); +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*); +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags); + +/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */ +#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ +#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ + +SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, + const void *pData, int nData, + int nZero, int bias, int seekResult); +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize); +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*); +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt); +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt); +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize); +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*); + +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); +SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*); + +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); +SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); + +#ifndef NDEBUG +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); +#endif + +#ifndef SQLITE_OMIT_BTREECOUNT +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *); +#endif + +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int); +SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*); +#endif + +#ifndef SQLITE_OMIT_WAL +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); +#endif + +/* +** If we are not using shared cache, then there is no need to +** use mutexes to access the BtShared structures. So make the +** Enter and Leave procedures no-ops. +*/ +#ifndef SQLITE_OMIT_SHARED_CACHE +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*); +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*); +SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*); +SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*); +#else +# define sqlite3BtreeEnter(X) +# define sqlite3BtreeEnterAll(X) +# define sqlite3BtreeSharable(X) 0 +# define sqlite3BtreeEnterCursor(X) +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE +SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*); +SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*); +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*); +#ifndef NDEBUG + /* These routines are used inside assert() statements only. */ +SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*); +SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*); +SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); +#endif +#else + +# define sqlite3BtreeLeave(X) +# define sqlite3BtreeLeaveCursor(X) +# define sqlite3BtreeLeaveAll(X) + +# define sqlite3BtreeHoldsMutex(X) 1 +# define sqlite3BtreeHoldsAllMutexes(X) 1 +# define sqlite3SchemaMutexHeld(X,Y,Z) 1 +#endif + + +#endif /* _BTREE_H_ */ + +/************** End of btree.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include vdbe.h in the middle of sqliteInt.h ******************/ +/************** Begin file vdbe.h ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Header file for the Virtual DataBase Engine (VDBE) +** +** This header defines the interface to the virtual database engine +** or VDBE. The VDBE implements an abstract machine that runs a +** simple program to access and modify the underlying database. +*/ +#ifndef _SQLITE_VDBE_H_ +#define _SQLITE_VDBE_H_ +/* #include */ + +/* +** A single VDBE is an opaque structure named "Vdbe". Only routines +** in the source file sqliteVdbe.c are allowed to see the insides +** of this structure. +*/ +typedef struct Vdbe Vdbe; + +/* +** The names of the following types declared in vdbeInt.h are required +** for the VdbeOp definition. +*/ +typedef struct Mem Mem; +typedef struct SubProgram SubProgram; + +/* +** A single instruction of the virtual machine has an opcode +** and as many as three operands. The instruction is recorded +** as an instance of the following structure: +*/ +struct VdbeOp { + u8 opcode; /* What operation to perform */ + signed char p4type; /* One of the P4_xxx constants for p4 */ + u8 notUsed1; + u8 p5; /* Fifth parameter is an unsigned character */ + int p1; /* First operand */ + int p2; /* Second parameter (often the jump destination) */ + int p3; /* The third parameter */ + union p4union { /* fourth parameter */ + int i; /* Integer value if p4type==P4_INT32 */ + void *p; /* Generic pointer */ + char *z; /* Pointer to data for string (char array) types */ + i64 *pI64; /* Used when p4type is P4_INT64 */ + double *pReal; /* Used when p4type is P4_REAL */ + FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ + sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */ + CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ + Mem *pMem; /* Used when p4type is P4_MEM */ + VTable *pVtab; /* Used when p4type is P4_VTAB */ + KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ + int *ai; /* Used when p4type is P4_INTARRAY */ + SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ + Table *pTab; /* Used when p4type is P4_TABLE */ +#ifdef SQLITE_ENABLE_CURSOR_HINTS + Expr *pExpr; /* Used when p4type is P4_EXPR */ +#endif + int (*xAdvance)(BtCursor *, int *); + } p4; +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + char *zComment; /* Comment to improve readability */ +#endif +#ifdef VDBE_PROFILE + u32 cnt; /* Number of times this instruction was executed */ + u64 cycles; /* Total time spent executing this instruction */ +#endif +#ifdef SQLITE_VDBE_COVERAGE + int iSrcLine; /* Source-code line that generated this opcode */ +#endif +}; +typedef struct VdbeOp VdbeOp; + + +/* +** A sub-routine used to implement a trigger program. +*/ +struct SubProgram { + VdbeOp *aOp; /* Array of opcodes for sub-program */ + int nOp; /* Elements in aOp[] */ + int nMem; /* Number of memory cells required */ + int nCsr; /* Number of cursors required */ + int nOnce; /* Number of OP_Once instructions */ + void *token; /* id that may be used to recursive triggers */ + SubProgram *pNext; /* Next sub-program already visited */ +}; + +/* +** A smaller version of VdbeOp used for the VdbeAddOpList() function because +** it takes up less space. +*/ +struct VdbeOpList { + u8 opcode; /* What operation to perform */ + signed char p1; /* First operand */ + signed char p2; /* Second parameter (often the jump destination) */ + signed char p3; /* Third parameter */ +}; +typedef struct VdbeOpList VdbeOpList; + +/* +** Allowed values of VdbeOp.p4type +*/ +#define P4_NOTUSED 0 /* The P4 parameter is not used */ +#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ +#define P4_STATIC (-2) /* Pointer to a static string */ +#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ +#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ +#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ +#define P4_EXPR (-7) /* P4 is a pointer to an Expr tree */ +#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ +#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ +#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ +#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ +#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ +#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ +#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ +#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ +#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ +#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ +#define P4_TABLE (-20) /* P4 is a pointer to a Table structure */ +#define P4_FUNCCTX (-21) /* P4 is a pointer to an sqlite3_context object */ + +/* Error message codes for OP_Halt */ +#define P5_ConstraintNotNull 1 +#define P5_ConstraintUnique 2 +#define P5_ConstraintCheck 3 +#define P5_ConstraintFK 4 + +/* +** The Vdbe.aColName array contains 5n Mem structures, where n is the +** number of columns of data returned by the statement. +*/ +#define COLNAME_NAME 0 +#define COLNAME_DECLTYPE 1 +#define COLNAME_DATABASE 2 +#define COLNAME_TABLE 3 +#define COLNAME_COLUMN 4 +#ifdef SQLITE_ENABLE_COLUMN_METADATA +# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */ +#else +# ifdef SQLITE_OMIT_DECLTYPE +# define COLNAME_N 1 /* Store only the name */ +# else +# define COLNAME_N 2 /* Store the name and decltype */ +# endif +#endif + +/* +** The following macro converts a relative address in the p2 field +** of a VdbeOp structure into a negative number so that +** sqlite3VdbeAddOpList() knows that the address is relative. Calling +** the macro again restores the address. +*/ +#define ADDR(X) (-1-(X)) + +/* +** The makefile scans the vdbe.c source file and creates the "opcodes.h" +** header file that defines a number for each opcode used by the VDBE. +*/ +/************** Include opcodes.h in the middle of vdbe.h ********************/ +/************** Begin file opcodes.h *****************************************/ +/* Automatically generated. Do not edit */ +/* See the tool/mkopcodeh.tcl script for details */ +#define OP_Savepoint 0 +#define OP_AutoCommit 1 +#define OP_Transaction 2 +#define OP_SorterNext 3 +#define OP_PrevIfOpen 4 +#define OP_NextIfOpen 5 +#define OP_Prev 6 +#define OP_Next 7 +#define OP_Checkpoint 8 +#define OP_JournalMode 9 +#define OP_Vacuum 10 +#define OP_VFilter 11 /* synopsis: iplan=r[P3] zplan='P4' */ +#define OP_VUpdate 12 /* synopsis: data=r[P3@P2] */ +#define OP_Goto 13 +#define OP_Gosub 14 +#define OP_InitCoroutine 15 +#define OP_Yield 16 +#define OP_MustBeInt 17 +#define OP_Jump 18 +#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ +#define OP_Once 20 +#define OP_If 21 +#define OP_IfNot 22 +#define OP_SeekLT 23 /* synopsis: key=r[P3@P4] */ +#define OP_SeekLE 24 /* synopsis: key=r[P3@P4] */ +#define OP_SeekGE 25 /* synopsis: key=r[P3@P4] */ +#define OP_SeekGT 26 /* synopsis: key=r[P3@P4] */ +#define OP_Or 27 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ +#define OP_And 28 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */ +#define OP_NoConflict 29 /* synopsis: key=r[P3@P4] */ +#define OP_NotFound 30 /* synopsis: key=r[P3@P4] */ +#define OP_Found 31 /* synopsis: key=r[P3@P4] */ +#define OP_NotExists 32 /* synopsis: intkey=r[P3] */ +#define OP_Last 33 +#define OP_IsNull 34 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */ +#define OP_NotNull 35 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ +#define OP_Ne 36 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */ +#define OP_Eq 37 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */ +#define OP_Gt 38 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */ +#define OP_Le 39 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */ +#define OP_Lt 40 /* same as TK_LT, synopsis: if r[P1]=r[P3] goto P2 */ +#define OP_SorterSort 42 +#define OP_BitAnd 43 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ +#define OP_BitOr 44 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ +#define OP_ShiftLeft 45 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<>r[P1] */ +#define OP_Add 47 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ +#define OP_Subtract 48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ +#define OP_Multiply 49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ +#define OP_Divide 50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ +#define OP_Remainder 51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ +#define OP_Concat 52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ +#define OP_Sort 53 +#define OP_BitNot 54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */ +#define OP_Rewind 55 +#define OP_IdxLE 56 /* synopsis: key=r[P3@P4] */ +#define OP_IdxGT 57 /* synopsis: key=r[P3@P4] */ +#define OP_IdxLT 58 /* synopsis: key=r[P3@P4] */ +#define OP_IdxGE 59 /* synopsis: key=r[P3@P4] */ +#define OP_RowSetRead 60 /* synopsis: r[P3]=rowset(P1) */ +#define OP_RowSetTest 61 /* synopsis: if r[P3] in rowset(P1) goto P2 */ +#define OP_Program 62 +#define OP_FkIfZero 63 /* synopsis: if fkctr[P1]==0 goto P2 */ +#define OP_IfPos 64 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */ +#define OP_IfNotZero 65 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */ +#define OP_DecrJumpZero 66 /* synopsis: if (--r[P1])==0 goto P2 */ +#define OP_IncrVacuum 67 +#define OP_VNext 68 +#define OP_Init 69 /* synopsis: Start at P2 */ +#define OP_Return 70 +#define OP_EndCoroutine 71 +#define OP_HaltIfNull 72 /* synopsis: if r[P3]=null halt */ +#define OP_Halt 73 +#define OP_Integer 74 /* synopsis: r[P2]=P1 */ +#define OP_Int64 75 /* synopsis: r[P2]=P4 */ +#define OP_String 76 /* synopsis: r[P2]='P4' (len=P1) */ +#define OP_Null 77 /* synopsis: r[P2..P3]=NULL */ +#define OP_SoftNull 78 /* synopsis: r[P1]=NULL */ +#define OP_Blob 79 /* synopsis: r[P2]=P4 (len=P1) */ +#define OP_Variable 80 /* synopsis: r[P2]=parameter(P1,P4) */ +#define OP_Move 81 /* synopsis: r[P2@P3]=r[P1@P3] */ +#define OP_Copy 82 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ +#define OP_SCopy 83 /* synopsis: r[P2]=r[P1] */ +#define OP_IntCopy 84 /* synopsis: r[P2]=r[P1] */ +#define OP_ResultRow 85 /* synopsis: output=r[P1@P2] */ +#define OP_CollSeq 86 +#define OP_Function0 87 /* synopsis: r[P3]=func(r[P2@P5]) */ +#define OP_Function 88 /* synopsis: r[P3]=func(r[P2@P5]) */ +#define OP_AddImm 89 /* synopsis: r[P1]=r[P1]+P2 */ +#define OP_RealAffinity 90 +#define OP_Cast 91 /* synopsis: affinity(r[P1]) */ +#define OP_Permutation 92 +#define OP_Compare 93 /* synopsis: r[P1@P3] <-> r[P2@P3] */ +#define OP_Column 94 /* synopsis: r[P3]=PX */ +#define OP_Affinity 95 /* synopsis: affinity(r[P1@P2]) */ +#define OP_MakeRecord 96 /* synopsis: r[P3]=mkrec(r[P1@P2]) */ +#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */ +#define OP_Count 98 /* synopsis: r[P2]=count() */ +#define OP_ReadCookie 99 +#define OP_SetCookie 100 +#define OP_ReopenIdx 101 /* synopsis: root=P2 iDb=P3 */ +#define OP_OpenRead 102 /* synopsis: root=P2 iDb=P3 */ +#define OP_OpenWrite 103 /* synopsis: root=P2 iDb=P3 */ +#define OP_OpenAutoindex 104 /* synopsis: nColumn=P2 */ +#define OP_OpenEphemeral 105 /* synopsis: nColumn=P2 */ +#define OP_SorterOpen 106 +#define OP_SequenceTest 107 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ +#define OP_OpenPseudo 108 /* synopsis: P3 columns in r[P2] */ +#define OP_Close 109 +#define OP_ColumnsUsed 110 +#define OP_Sequence 111 /* synopsis: r[P2]=cursor[P1].ctr++ */ +#define OP_NewRowid 112 /* synopsis: r[P2]=rowid */ +#define OP_Insert 113 /* synopsis: intkey=r[P3] data=r[P2] */ +#define OP_InsertInt 114 /* synopsis: intkey=P3 data=r[P2] */ +#define OP_Delete 115 +#define OP_ResetCount 116 +#define OP_SorterCompare 117 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ +#define OP_SorterData 118 /* synopsis: r[P2]=data */ +#define OP_RowKey 119 /* synopsis: r[P2]=key */ +#define OP_RowData 120 /* synopsis: r[P2]=data */ +#define OP_Rowid 121 /* synopsis: r[P2]=rowid */ +#define OP_NullRow 122 +#define OP_SorterInsert 123 +#define OP_IdxInsert 124 /* synopsis: key=r[P2] */ +#define OP_IdxDelete 125 /* synopsis: key=r[P2@P3] */ +#define OP_Seek 126 /* synopsis: Move P3 to P1.rowid */ +#define OP_IdxRowid 127 /* synopsis: r[P2]=rowid */ +#define OP_Destroy 128 +#define OP_Clear 129 +#define OP_ResetSorter 130 +#define OP_CreateIndex 131 /* synopsis: r[P2]=root iDb=P1 */ +#define OP_CreateTable 132 /* synopsis: r[P2]=root iDb=P1 */ +#define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */ +#define OP_ParseSchema 134 +#define OP_LoadAnalysis 135 +#define OP_DropTable 136 +#define OP_DropIndex 137 +#define OP_DropTrigger 138 +#define OP_IntegrityCk 139 +#define OP_RowSetAdd 140 /* synopsis: rowset(P1)=r[P2] */ +#define OP_Param 141 +#define OP_FkCounter 142 /* synopsis: fkctr[P1]+=P2 */ +#define OP_MemMax 143 /* synopsis: r[P1]=max(r[P1],r[P2]) */ +#define OP_OffsetLimit 144 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ +#define OP_AggStep0 145 /* synopsis: accum=r[P3] step(r[P2@P5]) */ +#define OP_AggStep 146 /* synopsis: accum=r[P3] step(r[P2@P5]) */ +#define OP_AggFinal 147 /* synopsis: accum=r[P1] N=P2 */ +#define OP_Expire 148 +#define OP_TableLock 149 /* synopsis: iDb=P1 root=P2 write=P3 */ +#define OP_VBegin 150 +#define OP_VCreate 151 +#define OP_VDestroy 152 +#define OP_VOpen 153 +#define OP_VColumn 154 /* synopsis: r[P3]=vcolumn(P2) */ +#define OP_VRename 155 +#define OP_Pagecount 156 +#define OP_MaxPgcnt 157 +#define OP_CursorHint 158 +#define OP_Noop 159 +#define OP_Explain 160 + +/* Properties such as "out2" or "jump" that are specified in +** comments following the "case" for each opcode in the vdbe.c +** are encoded into bitvectors as follows: +*/ +#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */ +#define OPFLG_IN1 0x02 /* in1: P1 is an input */ +#define OPFLG_IN2 0x04 /* in2: P2 is an input */ +#define OPFLG_IN3 0x08 /* in3: P3 is an input */ +#define OPFLG_OUT2 0x10 /* out2: P2 is an output */ +#define OPFLG_OUT3 0x20 /* out3: P3 is an output */ +#define OPFLG_INITIALIZER {\ +/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\ +/* 8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\ +/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\ +/* 24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\ +/* 32 */ 0x09, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ +/* 40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\ +/* 48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\ +/* 56 */ 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01,\ +/* 64 */ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x02, 0x02,\ +/* 72 */ 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10,\ +/* 80 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\ +/* 88 */ 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\ +/* 96 */ 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\ +/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ +/* 112 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 120 */ 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\ +/* 128 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\ +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\ +/* 144 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\ +/* 160 */ 0x00,} + +/* The sqlite3P2Values() routine is able to run faster if it knows +** the value of the largest JUMP opcode. The smaller the maximum +** JUMP opcode the better, so the mkopcodeh.tcl script that +** generated this include file strives to group all JUMP opcodes +** together near the beginning of the list. +*/ +#define SQLITE_MX_JUMP_OPCODE 69 /* Maximum JUMP opcode */ + +/************** End of opcodes.h *********************************************/ +/************** Continuing where we left off in vdbe.h ***********************/ + +/* +** Prototypes for the VDBE interface. See comments on the implementation +** for a description of what each of these routines does. +*/ +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*); +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*); +SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...); +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); +SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int); +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) +SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N); +#else +# define sqlite3VdbeVerifyNoMallocRequired(A,B) +#endif +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); +SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); +SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8); +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); +SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); +SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr); +SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); +SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*); +SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); +SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*); +SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int); +SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int); +#endif +SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*); +SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); +SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*); +SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); +SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*); +SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); +SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8); +SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int); +#ifndef SQLITE_OMIT_TRACE +SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); +#endif +SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); + +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int); +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); + +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*); +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); + +#ifndef SQLITE_OMIT_TRIGGER +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); +#endif + +/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on +** each VDBE opcode. +** +** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op +** comments in VDBE programs that show key decision points in the code +** generator. +*/ +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...); +# define VdbeComment(X) sqlite3VdbeComment X +SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...); +# define VdbeNoopComment(X) sqlite3VdbeNoopComment X +# ifdef SQLITE_ENABLE_MODULE_COMMENTS +# define VdbeModuleComment(X) sqlite3VdbeNoopComment X +# else +# define VdbeModuleComment(X) +# endif +#else +# define VdbeComment(X) +# define VdbeNoopComment(X) +# define VdbeModuleComment(X) +#endif + +/* +** The VdbeCoverage macros are used to set a coverage testing point +** for VDBE branch instructions. The coverage testing points are line +** numbers in the sqlite3.c source file. VDBE branch coverage testing +** only works with an amalagmation build. That's ok since a VDBE branch +** coverage build designed for testing the test suite only. No application +** should ever ship with VDBE branch coverage measuring turned on. +** +** VdbeCoverage(v) // Mark the previously coded instruction +** // as a branch +** +** VdbeCoverageIf(v, conditional) // Mark previous if conditional true +** +** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken +** +** VdbeCoverageNeverTaken(v) // Previous branch is never taken +** +** Every VDBE branch operation must be tagged with one of the macros above. +** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and +** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch() +** routine in vdbe.c, alerting the developer to the missed tag. +*/ +#ifdef SQLITE_VDBE_COVERAGE +SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int); +# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__) +# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__) +# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2); +# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1); +# define VDBE_OFFSET_LINENO(x) (__LINE__+x) +#else +# define VdbeCoverage(v) +# define VdbeCoverageIf(v,x) +# define VdbeCoverageAlwaysTaken(v) +# define VdbeCoverageNeverTaken(v) +# define VDBE_OFFSET_LINENO(x) 0 +#endif + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); +#else +# define sqlite3VdbeScanStatus(a,b,c,d,e) +#endif + +#endif + +/************** End of vdbe.h ************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include pager.h in the middle of sqliteInt.h *****************/ +/************** Begin file pager.h *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite page cache +** subsystem. The page cache subsystem reads and writes a file a page +** at a time and provides a journal for rollback. +*/ + +#ifndef _PAGER_H_ +#define _PAGER_H_ + +/* +** Default maximum size for persistent journal files. A negative +** value means no limit. This value may be overridden using the +** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit". +*/ +#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT + #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1 +#endif + +/* +** The type used to represent a page number. The first page in a file +** is called page 1. 0 is used to represent "not a page". +*/ +typedef u32 Pgno; + +/* +** Each open file is managed by a separate instance of the "Pager" structure. +*/ +typedef struct Pager Pager; + +/* +** Handle type for pages. +*/ +typedef struct PgHdr DbPage; + +/* +** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is +** reserved for working around a windows/posix incompatibility). It is +** used in the journal to signify that the remainder of the journal file +** is devoted to storing a master journal name - there are no more pages to +** roll back. See comments for function writeMasterJournal() in pager.c +** for details. +*/ +#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) + +/* +** Allowed values for the flags parameter to sqlite3PagerOpen(). +** +** NOTE: These values must match the corresponding BTREE_ values in btree.h. +*/ +#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ +#define PAGER_MEMORY 0x0002 /* In-memory database */ + +/* +** Valid values for the second argument to sqlite3PagerLockingMode(). +*/ +#define PAGER_LOCKINGMODE_QUERY -1 +#define PAGER_LOCKINGMODE_NORMAL 0 +#define PAGER_LOCKINGMODE_EXCLUSIVE 1 + +/* +** Numeric constants that encode the journalmode. +** +** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY) +** are exposed in the API via the "PRAGMA journal_mode" command and +** therefore cannot be changed without a compatibility break. +*/ +#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */ +#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */ +#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */ +#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */ +#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */ +#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */ +#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ + +/* +** Flags that make up the mask passed to sqlite3PagerGet(). +*/ +#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */ +#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */ + +/* +** Flags for sqlite3PagerSetFlags() +** +** Value constraints (enforced via assert()): +** PAGER_FULLFSYNC == SQLITE_FullFSync +** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync +** PAGER_CACHE_SPILL == SQLITE_CacheSpill +*/ +#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */ +#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */ +#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */ +#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */ +#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */ +#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */ +#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */ +#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */ +#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */ + +/* +** The remainder of this file contains the declarations of the functions +** that make up the Pager sub-system API. See source code comments for +** a detailed description of each routine. +*/ + +/* Open and close a Pager connection. */ +SQLITE_PRIVATE int sqlite3PagerOpen( + sqlite3_vfs*, + Pager **ppPager, + const char*, + int, + int, + int, + void(*)(DbPage*) +); +SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); + +/* Functions used to configure a Pager object. */ +SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int); +#ifdef SQLITE_HAS_CODEC +SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*); +#endif +SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); +SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); +SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int); +SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64); +SQLITE_PRIVATE void sqlite3PagerShrink(Pager*); +SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned); +SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int); +SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int); +SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*); +SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*); +SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64); +SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*); +SQLITE_PRIVATE int sqlite3PagerFlush(Pager*); + +/* Functions used to obtain and release page references. */ +SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag); +SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno); +SQLITE_PRIVATE void sqlite3PagerRef(DbPage*); +SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*); +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*); + +/* Operations on page references. */ +SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*); +SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*); +SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int); +SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*); +SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); +SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); + +/* Functions used to manage pager transactions and savepoints. */ +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*); +SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int); +SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*); +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster); +SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*); +SQLITE_PRIVATE int sqlite3PagerRollback(Pager*); +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n); +SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint); +SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager); + +#ifndef SQLITE_OMIT_WAL +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); +SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); +SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); +# ifdef SQLITE_ENABLE_SNAPSHOT +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); +# endif +#endif + +#ifdef SQLITE_ENABLE_ZIPVFS +SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); +#endif + +/* Functions used to query pager state and configuration. */ +SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); +SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); +#endif +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); +SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int); +SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*); +SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); +SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*); +SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); +SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); +SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *); +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); + +/* Functions used to truncate the database file. */ +SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); + +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); + +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) +SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); +#endif + +/* Functions to support testing and debugging. */ +#if !defined(NDEBUG) || defined(SQLITE_TEST) +SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*); +SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*); +#endif +#ifdef SQLITE_TEST +SQLITE_PRIVATE int *sqlite3PagerStats(Pager*); +SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*); + void disable_simulated_io_errors(void); + void enable_simulated_io_errors(void); +#else +# define disable_simulated_io_errors() +# define enable_simulated_io_errors() +#endif + +#endif /* _PAGER_H_ */ + +/************** End of pager.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include pcache.h in the middle of sqliteInt.h ****************/ +/************** Begin file pcache.h ******************************************/ +/* +** 2008 August 05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite page cache +** subsystem. +*/ + +#ifndef _PCACHE_H_ + +typedef struct PgHdr PgHdr; +typedef struct PCache PCache; + +/* +** Every page in the cache is controlled by an instance of the following +** structure. +*/ +struct PgHdr { + sqlite3_pcache_page *pPage; /* Pcache object page handle */ + void *pData; /* Page data */ + void *pExtra; /* Extra content */ + PgHdr *pDirty; /* Transient list of dirty sorted by pgno */ + Pager *pPager; /* The pager this page is part of */ + Pgno pgno; /* Page number for this page */ +#ifdef SQLITE_CHECK_PAGES + u32 pageHash; /* Hash of page content */ +#endif + u16 flags; /* PGHDR flags defined below */ + + /********************************************************************** + ** Elements above are public. All that follows is private to pcache.c + ** and should not be accessed by other modules. + */ + i16 nRef; /* Number of users of this page */ + PCache *pCache; /* Cache that owns this page */ + + PgHdr *pDirtyNext; /* Next element in list of dirty pages */ + PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ +}; + +/* Bit values for PgHdr.flags */ +#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */ +#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */ +#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */ +#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before + ** writing this page to the database */ +#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */ +#define PGHDR_MMAP 0x020 /* This is an mmap page object */ + +#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */ + +/* Initialize and shutdown the page cache subsystem */ +SQLITE_PRIVATE int sqlite3PcacheInitialize(void); +SQLITE_PRIVATE void sqlite3PcacheShutdown(void); + +/* Page cache buffer management: +** These routines implement SQLITE_CONFIG_PAGECACHE. +*/ +SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n); + +/* Create a new pager cache. +** Under memory stress, invoke xStress to try to make pages clean. +** Only clean and unpinned pages can be reclaimed. +*/ +SQLITE_PRIVATE int sqlite3PcacheOpen( + int szPage, /* Size of every page */ + int szExtra, /* Extra space associated with each page */ + int bPurgeable, /* True if pages are on backing store */ + int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */ + void *pStress, /* Argument to xStress */ + PCache *pToInit /* Preallocated space for the PCache */ +); + +/* Modify the page-size after the cache has been created. */ +SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int); + +/* Return the size in bytes of a PCache object. Used to preallocate +** storage space. +*/ +SQLITE_PRIVATE int sqlite3PcacheSize(void); + +/* One release per successful fetch. Page is pinned until released. +** Reference counted. +*/ +SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag); +SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**); +SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage); +SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*); + +SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */ +SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */ +SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */ +SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */ +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*); + +/* Change a page number. Used by incr-vacuum. */ +SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno); + +/* Remove all pages with pgno>x. Reset the cache if x==0 */ +SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x); + +/* Get a list of all dirty pages in the cache, sorted by page number */ +SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*); + +/* Reset and close the cache object */ +SQLITE_PRIVATE void sqlite3PcacheClose(PCache*); + +/* Clear flags from pages of the page cache */ +SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *); + +/* Discard the contents of the cache */ +SQLITE_PRIVATE void sqlite3PcacheClear(PCache*); + +/* Return the total number of outstanding page references */ +SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*); + +/* Increment the reference count of an existing page */ +SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*); + +SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*); + +/* Return the total number of pages stored in the cache */ +SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*); + +#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) +/* Iterate through all dirty pages currently stored in the cache. This +** interface is only available if SQLITE_CHECK_PAGES is defined when the +** library is built. +*/ +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); +#endif + +#if defined(SQLITE_DEBUG) +/* Check invariants on a PgHdr object */ +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*); +#endif + +/* Set and get the suggested cache-size for the specified pager-cache. +** +** If no global maximum is configured, then the system attempts to limit +** the total number of pages cached by purgeable pager-caches to the sum +** of the suggested cache-sizes. +*/ +SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int); +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *); +#endif + +/* Set or get the suggested spill-size for the specified pager-cache. +** +** The spill-size is the minimum number of pages in cache before the cache +** will attempt to spill dirty pages by calling xStress. +*/ +SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int); + +/* Free up as much memory as possible from the page cache */ +SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*); + +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +/* Try to return memory used by the pcache module to the main memory heap */ +SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int); +#endif + +#ifdef SQLITE_TEST +SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); +#endif + +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); + +/* Return the header size */ +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); + +/* Number of dirty pages as a percentage of the configured cache size */ +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*); + +#endif /* _PCACHE_H_ */ + +/************** End of pcache.h **********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include os.h in the middle of sqliteInt.h ********************/ +/************** Begin file os.h **********************************************/ +/* +** 2001 September 16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file (together with is companion C source-code file +** "os.c") attempt to abstract the underlying operating system so that +** the SQLite library will work on both POSIX and windows systems. +** +** This header file is #include-ed by sqliteInt.h and thus ends up +** being included by every source file. +*/ +#ifndef _SQLITE_OS_H_ +#define _SQLITE_OS_H_ + +/* +** Attempt to automatically detect the operating system and setup the +** necessary pre-processor macros for it. +*/ +/************** Include os_setup.h in the middle of os.h *********************/ +/************** Begin file os_setup.h ****************************************/ +/* +** 2013 November 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains pre-processor directives related to operating system +** detection and/or setup. +*/ +#ifndef _OS_SETUP_H_ +#define _OS_SETUP_H_ + +/* +** Figure out if we are dealing with Unix, Windows, or some other operating +** system. +** +** After the following block of preprocess macros, all of SQLITE_OS_UNIX, +** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of +** the three will be 1. The other two will be 0. +*/ +#if defined(SQLITE_OS_OTHER) +# if SQLITE_OS_OTHER==1 +# undef SQLITE_OS_UNIX +# define SQLITE_OS_UNIX 0 +# undef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +# else +# undef SQLITE_OS_OTHER +# endif +#endif +#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) +# define SQLITE_OS_OTHER 0 +# ifndef SQLITE_OS_WIN +# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ + defined(__MINGW32__) || defined(__BORLANDC__) +# define SQLITE_OS_WIN 1 +# define SQLITE_OS_UNIX 0 +# else +# define SQLITE_OS_WIN 0 +# define SQLITE_OS_UNIX 1 +# endif +# else +# define SQLITE_OS_UNIX 0 +# endif +#else +# ifndef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +# endif +#endif + +#endif /* _OS_SETUP_H_ */ + +/************** End of os_setup.h ********************************************/ +/************** Continuing where we left off in os.h *************************/ + +/* If the SET_FULLSYNC macro is not defined above, then make it +** a no-op +*/ +#ifndef SET_FULLSYNC +# define SET_FULLSYNC(x,y) +#endif + +/* +** The default size of a disk sector +*/ +#ifndef SQLITE_DEFAULT_SECTOR_SIZE +# define SQLITE_DEFAULT_SECTOR_SIZE 4096 +#endif + +/* +** Temporary files are named starting with this prefix followed by 16 random +** alphanumeric characters, and no file extension. They are stored in the +** OS's standard temporary file directory, and are deleted prior to exit. +** If sqlite is being embedded in another program, you may wish to change the +** prefix to reflect your program's name, so that if your program exits +** prematurely, old temporary files can be easily identified. This can be done +** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. +** +** 2006-10-31: The default prefix used to be "sqlite_". But then +** Mcafee started using SQLite in their anti-virus product and it +** started putting files with the "sqlite" name in the c:/temp folder. +** This annoyed many windows users. Those users would then do a +** Google search for "sqlite", find the telephone numbers of the +** developers and call to wake them up at night and complain. +** For this reason, the default name prefix is changed to be "sqlite" +** spelled backwards. So the temp files are still identified, but +** anybody smart enough to figure out the code is also likely smart +** enough to know that calling the developer will not help get rid +** of the file. +*/ +#ifndef SQLITE_TEMP_FILE_PREFIX +# define SQLITE_TEMP_FILE_PREFIX "etilqs_" +#endif + +/* +** The following values may be passed as the second argument to +** sqlite3OsLock(). The various locks exhibit the following semantics: +** +** SHARED: Any number of processes may hold a SHARED lock simultaneously. +** RESERVED: A single process may hold a RESERVED lock on a file at +** any time. Other processes may hold and obtain new SHARED locks. +** PENDING: A single process may hold a PENDING lock on a file at +** any one time. Existing SHARED locks may persist, but no new +** SHARED locks may be obtained by other processes. +** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. +** +** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a +** process that requests an EXCLUSIVE lock may actually obtain a PENDING +** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to +** sqlite3OsLock(). +*/ +#define NO_LOCK 0 +#define SHARED_LOCK 1 +#define RESERVED_LOCK 2 +#define PENDING_LOCK 3 +#define EXCLUSIVE_LOCK 4 + +/* +** File Locking Notes: (Mostly about windows but also some info for Unix) +** +** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because +** those functions are not available. So we use only LockFile() and +** UnlockFile(). +** +** LockFile() prevents not just writing but also reading by other processes. +** A SHARED_LOCK is obtained by locking a single randomly-chosen +** byte out of a specific range of bytes. The lock byte is obtained at +** random so two separate readers can probably access the file at the +** same time, unless they are unlucky and choose the same lock byte. +** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. +** There can only be one writer. A RESERVED_LOCK is obtained by locking +** a single byte of the file that is designated as the reserved lock byte. +** A PENDING_LOCK is obtained by locking a designated byte different from +** the RESERVED_LOCK byte. +** +** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, +** which means we can use reader/writer locks. When reader/writer locks +** are used, the lock is placed on the same range of bytes that is used +** for probabilistic locking in Win95/98/ME. Hence, the locking scheme +** will support two or more Win95 readers or two or more WinNT readers. +** But a single Win95 reader will lock out all WinNT readers and a single +** WinNT reader will lock out all other Win95 readers. +** +** The following #defines specify the range of bytes used for locking. +** SHARED_SIZE is the number of bytes available in the pool from which +** a random byte is selected for a shared lock. The pool of bytes for +** shared locks begins at SHARED_FIRST. +** +** The same locking strategy and +** byte ranges are used for Unix. This leaves open the possibility of having +** clients on win95, winNT, and unix all talking to the same shared file +** and all locking correctly. To do so would require that samba (or whatever +** tool is being used for file sharing) implements locks correctly between +** windows and unix. I'm guessing that isn't likely to happen, but by +** using the same locking range we are at least open to the possibility. +** +** Locking in windows is manditory. For this reason, we cannot store +** actual data in the bytes used for locking. The pager never allocates +** the pages involved in locking therefore. SHARED_SIZE is selected so +** that all locks will fit on a single page even at the minimum page size. +** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE +** is set high so that we don't have to allocate an unused page except +** for very large databases. But one should test the page skipping logic +** by setting PENDING_BYTE low and running the entire regression suite. +** +** Changing the value of PENDING_BYTE results in a subtly incompatible +** file format. Depending on how it is changed, you might not notice +** the incompatibility right away, even running a full regression test. +** The default location of PENDING_BYTE is the first byte past the +** 1GB boundary. +** +*/ +#ifdef SQLITE_OMIT_WSD +# define PENDING_BYTE (0x40000000) +#else +# define PENDING_BYTE sqlite3PendingByte +#endif +#define RESERVED_BYTE (PENDING_BYTE+1) +#define SHARED_FIRST (PENDING_BYTE+2) +#define SHARED_SIZE 510 + +/* +** Wrapper around OS specific sqlite3_os_init() function. +*/ +SQLITE_PRIVATE int sqlite3OsInit(void); + +/* +** Functions for accessing sqlite3_file methods +*/ +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*); +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size); +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*); +SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*); +#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); +SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); +SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *); + + +/* +** Functions for accessing sqlite3_vfs methods +*/ +SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int); +SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); +SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); +#ifndef SQLITE_OMIT_LOAD_EXTENSION +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *); +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *); +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *); +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int); +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*); +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); + +/* +** Convenience functions for opening and closing files using +** sqlite3_malloc() to obtain space for the file-handle structure. +*/ +SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *); + +#endif /* _SQLITE_OS_H_ */ + +/************** End of os.h **************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include mutex.h in the middle of sqliteInt.h *****************/ +/************** Begin file mutex.h *******************************************/ +/* +** 2007 August 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the common header for all mutex implementations. +** The sqliteInt.h header #includes this file so that it is available +** to all source files. We break it out in an effort to keep the code +** better organized. +** +** NOTE: source files should *not* #include this header file directly. +** Source files should #include the sqliteInt.h file and let that file +** include this one indirectly. +*/ + + +/* +** Figure out what version of the code to use. The choices are +** +** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The +** mutexes implementation cannot be overridden +** at start-time. +** +** SQLITE_MUTEX_NOOP For single-threaded applications. No +** mutual exclusion is provided. But this +** implementation can be overridden at +** start-time. +** +** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix. +** +** SQLITE_MUTEX_W32 For multi-threaded applications on Win32. +*/ +#if !SQLITE_THREADSAFE +# define SQLITE_MUTEX_OMIT +#endif +#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP) +# if SQLITE_OS_UNIX +# define SQLITE_MUTEX_PTHREADS +# elif SQLITE_OS_WIN +# define SQLITE_MUTEX_W32 +# else +# define SQLITE_MUTEX_NOOP +# endif +#endif + +#ifdef SQLITE_MUTEX_OMIT +/* +** If this is a no-op implementation, implement everything as macros. +*/ +#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) +#define sqlite3_mutex_free(X) +#define sqlite3_mutex_enter(X) +#define sqlite3_mutex_try(X) SQLITE_OK +#define sqlite3_mutex_leave(X) +#define sqlite3_mutex_held(X) ((void)(X),1) +#define sqlite3_mutex_notheld(X) ((void)(X),1) +#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) +#define sqlite3MutexInit() SQLITE_OK +#define sqlite3MutexEnd() +#define MUTEX_LOGIC(X) +#else +#define MUTEX_LOGIC(X) X +#endif /* defined(SQLITE_MUTEX_OMIT) */ + +/************** End of mutex.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default +** synchronous setting to EXTRA. It is no longer supported. +*/ +#ifdef SQLITE_EXTRA_DURABLE +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE +# define SQLITE_DEFAULT_SYNCHRONOUS 3 +#endif + +/* +** Default synchronous levels. +** +** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1. +** +** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS +** OFF 1 0 +** NORMAL 2 1 +** FULL 3 2 +** EXTRA 4 3 +** +** The "PRAGMA synchronous" statement also uses the zero-based numbers. +** In other words, the zero-based numbers are used for all external interfaces +** and the one-based values are used internally. +*/ +#ifndef SQLITE_DEFAULT_SYNCHRONOUS +# define SQLITE_DEFAULT_SYNCHRONOUS (PAGER_SYNCHRONOUS_FULL-1) +#endif +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS +#endif + +/* +** Each database file to be accessed by the system is an instance +** of the following structure. There are normally two of these structures +** in the sqlite.aDb[] array. aDb[0] is the main database file and +** aDb[1] is the database file used to hold temporary tables. Additional +** databases may be attached. +*/ +struct Db { + char *zName; /* Name of this database */ + Btree *pBt; /* The B*Tree structure for this database file */ + u8 safety_level; /* How aggressive at syncing data to disk */ + u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */ + Schema *pSchema; /* Pointer to database schema (possibly shared) */ +}; + +/* +** An instance of the following structure stores a database schema. +** +** Most Schema objects are associated with a Btree. The exception is +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing. +** In shared cache mode, a single Schema object can be shared by multiple +** Btrees that refer to the same underlying BtShared object. +** +** Schema objects are automatically deallocated when the last Btree that +** references them is destroyed. The TEMP Schema is manually freed by +** sqlite3_close(). +* +** A thread must be holding a mutex on the corresponding Btree in order +** to access Schema content. This implies that the thread must also be +** holding a mutex on the sqlite3 connection pointer that owns the Btree. +** For a TEMP Schema, only the connection mutex is required. +*/ +struct Schema { + int schema_cookie; /* Database schema version number for this file */ + int iGeneration; /* Generation counter. Incremented with each change */ + Hash tblHash; /* All tables indexed by name */ + Hash idxHash; /* All (named) indices indexed by name */ + Hash trigHash; /* All triggers indexed by name */ + Hash fkeyHash; /* All foreign keys by referenced table name */ + Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ + u8 file_format; /* Schema format version for this file */ + u8 enc; /* Text encoding used by this database */ + u16 schemaFlags; /* Flags associated with this schema */ + int cache_size; /* Number of pages to use in the cache */ +}; + +/* +** These macros can be used to test, set, or clear bits in the +** Db.pSchema->flags field. +*/ +#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) +#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0) +#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P) +#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P) + +/* +** Allowed values for the DB.pSchema->flags field. +** +** The DB_SchemaLoaded flag is set after the database schema has been +** read into internal hash tables. +** +** DB_UnresetViews means that one or more views have column names that +** have been filled out. If the schema changes, these column names might +** changes and so the view will need to be reset. +*/ +#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ +#define DB_UnresetViews 0x0002 /* Some views have defined column names */ +#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ + +/* +** The number of different kinds of things that can be limited +** using the sqlite3_limit() interface. +*/ +#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1) + +/* +** Lookaside malloc is a set of fixed-size buffers that can be used +** to satisfy small transient memory allocation requests for objects +** associated with a particular database connection. The use of +** lookaside malloc provides a significant performance enhancement +** (approx 10%) by avoiding numerous malloc/free requests while parsing +** SQL statements. +** +** The Lookaside structure holds configuration information about the +** lookaside malloc subsystem. Each available memory allocation in +** the lookaside subsystem is stored on a linked list of LookasideSlot +** objects. +** +** Lookaside allocations are only allowed for objects that are associated +** with a particular database connection. Hence, schema information cannot +** be stored in lookaside because in shared cache mode the schema information +** is shared by multiple database connections. Therefore, while parsing +** schema information, the Lookaside.bEnabled flag is cleared so that +** lookaside allocations are not used to construct the schema objects. +*/ +struct Lookaside { + u32 bDisable; /* Only operate the lookaside when zero */ + u16 sz; /* Size of each buffer in bytes */ + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */ + int nOut; /* Number of buffers currently checked out */ + int mxOut; /* Highwater mark for nOut */ + int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ + LookasideSlot *pFree; /* List of available buffers */ + void *pStart; /* First byte of available memory space */ + void *pEnd; /* First byte past end of available space */ +}; +struct LookasideSlot { + LookasideSlot *pNext; /* Next buffer in the list of free buffers */ +}; + +/* +** A hash table for built-in function definitions. (Application-defined +** functions use a regular table table from hash.h.) +** +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. +** Collisions are on the FuncDef.u.pHash chain. +*/ +#define SQLITE_FUNC_HASH_SZ 23 +struct FuncDefHash { + FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ +}; + +#ifdef SQLITE_USER_AUTHENTICATION +/* +** Information held in the "sqlite3" database connection object and used +** to manage user authentication. +*/ +typedef struct sqlite3_userauth sqlite3_userauth; +struct sqlite3_userauth { + u8 authLevel; /* Current authentication level */ + int nAuthPW; /* Size of the zAuthPW in bytes */ + char *zAuthPW; /* Password used to authenticate */ + char *zAuthUser; /* User name used to authenticate */ +}; + +/* Allowed values for sqlite3_userauth.authLevel */ +#define UAUTH_Unknown 0 /* Authentication not yet checked */ +#define UAUTH_Fail 1 /* User authentication failed */ +#define UAUTH_User 2 /* Authenticated as a normal user */ +#define UAUTH_Admin 3 /* Authenticated as an administrator */ + +/* Functions used only by user authorization logic */ +SQLITE_PRIVATE int sqlite3UserAuthTable(const char*); +SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*); +SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*); +SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); + +#endif /* SQLITE_USER_AUTHENTICATION */ + +/* +** typedef for the authorization callback function. +*/ +#ifdef SQLITE_USER_AUTHENTICATION + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, + const char*, const char*); +#else + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, + const char*); +#endif + + +/* +** Each database connection is an instance of the following structure. +*/ +struct sqlite3 { + sqlite3_vfs *pVfs; /* OS Interface */ + struct Vdbe *pVdbe; /* List of active virtual machines */ + CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ + sqlite3_mutex *mutex; /* Connection mutex */ + Db *aDb; /* All backends */ + int nDb; /* Number of backends currently in use */ + int flags; /* Miscellaneous flags. See below */ + i64 lastRowid; /* ROWID of most recent insert (see above) */ + i64 szMmap; /* Default mmap_size setting */ + unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ + int errCode; /* Most recent error code (SQLITE_*) */ + int errMask; /* & result codes with this before returning */ + int iSysErrno; /* Errno value from last system error */ + u16 dbOptFlags; /* Flags to enable/disable optimizations */ + u8 enc; /* Text encoding */ + u8 autoCommit; /* The auto-commit flag. */ + u8 temp_store; /* 1: file 2: memory 0: default */ + u8 mallocFailed; /* True if we have seen a malloc failure */ + u8 bBenignMalloc; /* Do not require OOMs if true */ + u8 dfltLockMode; /* Default locking-mode for attached dbs */ + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ + u8 suppressErr; /* Do not issue error messages if true */ + u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */ + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ + int nextPagesize; /* Pagesize after VACUUM if >0 */ + u32 magic; /* Magic number for detect library misuse */ + int nChange; /* Value returned by sqlite3_changes() */ + int nTotalChange; /* Value returned by sqlite3_total_changes() */ + int aLimit[SQLITE_N_LIMIT]; /* Limits */ + int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */ + struct sqlite3InitInfo { /* Information used during initialization */ + int newTnum; /* Rootpage of table being initialized */ + u8 iDb; /* Which db file is being initialized */ + u8 busy; /* TRUE if currently initializing */ + u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */ + u8 imposterTable; /* Building an imposter table */ + } init; + int nVdbeActive; /* Number of VDBEs currently running */ + int nVdbeRead; /* Number of active VDBEs that read or write */ + int nVdbeWrite; /* Number of active VDBEs that read and write */ + int nVdbeExec; /* Number of nested calls to VdbeExec() */ + int nVDestroy; /* Number of active OP_VDestroy operations */ + int nExtension; /* Number of loaded extensions */ + void **aExtension; /* Array of shared library handles */ + void (*xTrace)(void*,const char*); /* Trace function */ + void *pTraceArg; /* Argument to the trace function */ + void (*xProfile)(void*,const char*,u64); /* Profiling function */ + void *pProfileArg; /* Argument to profile function */ + void *pCommitArg; /* Argument to xCommitCallback() */ + int (*xCommitCallback)(void*); /* Invoked at every commit. */ + void *pRollbackArg; /* Argument to xRollbackCallback() */ + void (*xRollbackCallback)(void*); /* Invoked at every commit. */ + void *pUpdateArg; + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + void *pPreUpdateArg; /* First argument to xPreUpdateCallback */ + void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */ + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64 + ); + PreUpdate *pPreUpdate; /* Context for active pre-update callback */ +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ +#ifndef SQLITE_OMIT_WAL + int (*xWalCallback)(void *, sqlite3 *, const char *, int); + void *pWalArg; +#endif + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*); + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*); + void *pCollNeededArg; + sqlite3_value *pErr; /* Most recent error message */ + union { + volatile int isInterrupted; /* True if sqlite3_interrupt has been called */ + double notUsed1; /* Spacer */ + } u1; + Lookaside lookaside; /* Lookaside malloc configuration */ +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth; /* Access authorization function */ + void *pAuthArg; /* 1st argument to the access auth function */ +#endif +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + int (*xProgress)(void *); /* The progress callback */ + void *pProgressArg; /* Argument to the progress callback */ + unsigned nProgressOps; /* Number of opcodes for progress callback */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + int nVTrans; /* Allocated size of aVTrans */ + Hash aModule; /* populated by sqlite3_create_module() */ + VtabCtx *pVtabCtx; /* Context for active vtab connect/create */ + VTable **aVTrans; /* Virtual tables with open transactions */ + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ +#endif + Hash aFunc; /* Hash table of connection functions */ + Hash aCollSeq; /* All collating sequences */ + BusyHandler busyHandler; /* Busy callback */ + Db aDbStatic[2]; /* Static space for the 2 default backends */ + Savepoint *pSavepoint; /* List of active savepoints */ + int busyTimeout; /* Busy handler timeout, in msec */ + int nSavepoint; /* Number of non-transaction savepoints */ + int nStatement; /* Number of nested statement-transactions */ + i64 nDeferredCons; /* Net deferred constraints this transaction. */ + i64 nDeferredImmCons; /* Net deferred immediate constraints */ + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + /* The following variables are all protected by the STATIC_MASTER + ** mutex, not by sqlite3.mutex. They are used by code in notify.c. + ** + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to + ** unlock so that it can proceed. + ** + ** When X.pBlockingConnection==Y, that means that something that X tried + ** tried to do recently failed with an SQLITE_LOCKED error due to locks + ** held by Y. + */ + sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */ + sqlite3 *pUnlockConnection; /* Connection to watch for unlock */ + void *pUnlockArg; /* Argument to xUnlockNotify */ + void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ + sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ +#endif +#ifdef SQLITE_USER_AUTHENTICATION + sqlite3_userauth auth; /* User authentication information */ +#endif +}; + +/* +** A macro to discover the encoding of a database. +*/ +#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc) +#define ENC(db) ((db)->enc) + +/* +** Possible values for the sqlite3.flags. +** +** Value constraints (enforced via assert()): +** SQLITE_FullFSync == PAGER_FULLFSYNC +** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC +** SQLITE_CacheSpill == PAGER_CACHE_SPILL +*/ +#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ +#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */ +#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */ +#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */ +#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */ +#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */ +#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */ +#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */ + /* DELETE, or UPDATE and return */ + /* the count using a callback. */ +#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ + /* result set is empty */ +#define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */ +#define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */ +#define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */ +#define SQLITE_VdbeAddopTrace 0x00001000 /* Trace sqlite3VdbeAddOp() calls */ +#define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */ +#define SQLITE_ReadUncommitted 0x0004000 /* For shared-cache mode */ +#define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */ +#define SQLITE_RecoveryMode 0x00010000 /* Ignore schema errors */ +#define SQLITE_ReverseOrder 0x00020000 /* Reverse unordered SELECTs */ +#define SQLITE_RecTriggers 0x00040000 /* Enable recursive triggers */ +#define SQLITE_ForeignKeys 0x00080000 /* Enforce foreign key constraints */ +#define SQLITE_AutoIndex 0x00100000 /* Enable automatic indexes */ +#define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */ +#define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ +#define SQLITE_LoadExtFunc 0x00800000 /* Enable load_extension() SQL func */ +#define SQLITE_EnableTrigger 0x01000000 /* True to enable triggers */ +#define SQLITE_DeferFKs 0x02000000 /* Defer all FK constraints */ +#define SQLITE_QueryOnly 0x04000000 /* Disable database changes */ +#define SQLITE_VdbeEQP 0x08000000 /* Debug EXPLAIN QUERY PLAN */ +#define SQLITE_Vacuum 0x10000000 /* Currently in a VACUUM */ +#define SQLITE_CellSizeCk 0x20000000 /* Check btree cell sizes on load */ +#define SQLITE_Fts3Tokenizer 0x40000000 /* Enable fts3_tokenizer(2) */ + + +/* +** Bits of the sqlite3.dbOptFlags field that are used by the +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to +** selectively disable various optimizations. +*/ +#define SQLITE_QueryFlattener 0x0001 /* Query flattening */ +#define SQLITE_ColumnCache 0x0002 /* Column cache */ +#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */ +#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */ +/* not used 0x0010 // Was: SQLITE_IdxRealAsInt */ +#define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */ +#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ +#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ +#define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ +#define SQLITE_Transitive 0x0200 /* Transitive constraints */ +#define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ +#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ +#define SQLITE_CursorHints 0x2000 /* Add OP_CursorHint opcodes */ +#define SQLITE_AllOpts 0xffff /* All optimizations */ + +/* +** Macros for testing whether or not optimizations are enabled or disabled. +*/ +#ifndef SQLITE_OMIT_BUILTIN_TEST +#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0) +#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0) +#else +#define OptimizationDisabled(db, mask) 0 +#define OptimizationEnabled(db, mask) 1 +#endif + +/* +** Return true if it OK to factor constant expressions into the initialization +** code. The argument is a Parse object for the code generator. +*/ +#define ConstFactorOk(P) ((P)->okConstFactor) + +/* +** Possible values for the sqlite.magic field. +** The numbers are obtained at random and have no special meaning, other +** than being distinct from one another. +*/ +#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ +#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */ +#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ +#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ +#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */ + +/* +** Each SQL function is defined by an instance of the following +** structure. For global built-in functions (ex: substr(), max(), count()) +** a pointer to this structure is held in the sqlite3BuiltinFunctions object. +** For per-connection application-defined functions, a pointer to this +** structure is held in the db->aHash hash table. +** +** The u.pHash field is used by the global built-ins. The u.pDestructor +** field is used by per-connection app-def functions. +*/ +struct FuncDef { + i8 nArg; /* Number of arguments. -1 means unlimited */ + u16 funcFlags; /* Some combination of SQLITE_FUNC_* */ + void *pUserData; /* User data parameter */ + FuncDef *pNext; /* Next function with same name */ + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */ + void (*xFinalize)(sqlite3_context*); /* Agg finalizer */ + const char *zName; /* SQL name of the function. */ + union { + FuncDef *pHash; /* Next with a different name but the same hash */ + FuncDestructor *pDestructor; /* Reference counted destructor function */ + } u; +}; + +/* +** This structure encapsulates a user-function destructor callback (as +** configured using create_function_v2()) and a reference counter. When +** create_function_v2() is called to create a function with a destructor, +** a single object of this type is allocated. FuncDestructor.nRef is set to +** the number of FuncDef objects created (either 1 or 3, depending on whether +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor +** member of each of the new FuncDef objects is set to point to the allocated +** FuncDestructor. +** +** Thereafter, when one of the FuncDef objects is deleted, the reference +** count on this object is decremented. When it reaches 0, the destructor +** is invoked and the FuncDestructor structure freed. +*/ +struct FuncDestructor { + int nRef; + void (*xDestroy)(void *); + void *pUserData; +}; + +/* +** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And +** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There +** are assert() statements in the code to verify this. +** +** Value constraints (enforced via assert()): +** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg +** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG +** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG +** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API +** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API +*/ +#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */ +#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */ +#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */ +#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */ +#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/ +#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */ +#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ +#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ +#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ +#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ +#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ +#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ +#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a + ** single query - might change over time */ + +/* +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are +** used to create the initializers for the FuncDef structures. +** +** FUNCTION(zName, nArg, iArg, bNC, xFunc) +** Used to create a scalar function definition of a function zName +** implemented by C function xFunc that accepts nArg arguments. The +** value passed as iArg is cast to a (void*) and made available +** as the user-data (sqlite3_user_data()) for the function. If +** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. +** +** VFUNCTION(zName, nArg, iArg, bNC, xFunc) +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag. +** +** DFUNCTION(zName, nArg, iArg, bNC, xFunc) +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and +** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions +** and functions like sqlite_version() that can change, but not during +** a single query. +** +** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) +** Used to create an aggregate function definition implemented by +** the C functions xStep and xFinal. The first four parameters +** are interpreted in the same way as the first 4 parameters to +** FUNCTION(). +** +** LIKEFUNC(zName, nArg, pArg, flags) +** Used to create a scalar function definition of a function zName +** that accepts nArg arguments and is implemented by a call to C +** function likeFunc. Argument pArg is cast to a (void *) and made +** available as the function user-data (sqlite3_user_data()). The +** FuncDef.flags variable is set to the value passed as the flags +** parameter. +*/ +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} } +#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} } +#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} } +#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \ + {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} } +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + pArg, 0, xFunc, 0, #zName, } +#define LIKEFUNC(zName, nArg, arg, flags) \ + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \ + (void *)arg, 0, likeFunc, 0, #zName, {0} } +#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}} +#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \ + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \ + SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}} + +/* +** All current savepoints are stored in a linked list starting at +** sqlite3.pSavepoint. The first element in the list is the most recently +** opened savepoint. Savepoints are added to the list by the vdbe +** OP_Savepoint instruction. +*/ +struct Savepoint { + char *zName; /* Savepoint name (nul-terminated) */ + i64 nDeferredCons; /* Number of deferred fk violations */ + i64 nDeferredImmCons; /* Number of deferred imm fk. */ + Savepoint *pNext; /* Parent savepoint (if any) */ +}; + +/* +** The following are used as the second parameter to sqlite3Savepoint(), +** and as the P1 argument to the OP_Savepoint instruction. +*/ +#define SAVEPOINT_BEGIN 0 +#define SAVEPOINT_RELEASE 1 +#define SAVEPOINT_ROLLBACK 2 + + +/* +** Each SQLite module (virtual table definition) is defined by an +** instance of the following structure, stored in the sqlite3.aModule +** hash table. +*/ +struct Module { + const sqlite3_module *pModule; /* Callback pointers */ + const char *zName; /* Name passed to create_module() */ + void *pAux; /* pAux passed to create_module() */ + void (*xDestroy)(void *); /* Module destructor function */ + Table *pEpoTab; /* Eponymous table for this module */ +}; + +/* +** information about each column of an SQL table is held in an instance +** of this structure. +*/ +struct Column { + char *zName; /* Name of this column, \000, then the type */ + Expr *pDflt; /* Default value of this column */ + char *zColl; /* Collating sequence. If NULL, use the default */ + u8 notNull; /* An OE_ code for handling a NOT NULL constraint */ + char affinity; /* One of the SQLITE_AFF_... values */ + u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */ + u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */ +}; + +/* Allowed values for Column.colFlags: +*/ +#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */ +#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */ +#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */ + +/* +** A "Collating Sequence" is defined by an instance of the following +** structure. Conceptually, a collating sequence consists of a name and +** a comparison routine that defines the order of that sequence. +** +** If CollSeq.xCmp is NULL, it means that the +** collating sequence is undefined. Indices built on an undefined +** collating sequence may not be read or written. +*/ +struct CollSeq { + char *zName; /* Name of the collating sequence, UTF-8 encoded */ + u8 enc; /* Text encoding handled by xCmp() */ + void *pUser; /* First argument to xCmp() */ + int (*xCmp)(void*,int, const void*, int, const void*); + void (*xDel)(void*); /* Destructor for pUser */ +}; + +/* +** A sort order can be either ASC or DESC. +*/ +#define SQLITE_SO_ASC 0 /* Sort in ascending order */ +#define SQLITE_SO_DESC 1 /* Sort in ascending order */ +#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */ + +/* +** Column affinity types. +** +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and +** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve +** the speed a little by numbering the values consecutively. +** +** But rather than start with 0 or 1, we begin with 'A'. That way, +** when multiple affinity types are concatenated into a string and +** used as the P4 operand, they will be more readable. +** +** Note also that the numeric types are grouped together so that testing +** for a numeric type is a single comparison. And the BLOB type is first. +*/ +#define SQLITE_AFF_BLOB 'A' +#define SQLITE_AFF_TEXT 'B' +#define SQLITE_AFF_NUMERIC 'C' +#define SQLITE_AFF_INTEGER 'D' +#define SQLITE_AFF_REAL 'E' + +#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) + +/* +** The SQLITE_AFF_MASK values masks off the significant bits of an +** affinity value. +*/ +#define SQLITE_AFF_MASK 0x47 + +/* +** Additional bit values that can be ORed with an affinity without +** changing the affinity. +** +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL. +** It causes an assert() to fire if either operand to a comparison +** operator is NULL. It is added to certain comparison operators to +** prove that the operands are always NOT NULL. +*/ +#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */ +#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */ +#define SQLITE_NULLEQ 0x80 /* NULL=NULL */ +#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */ + +/* +** An object of this type is created for each virtual table present in +** the database schema. +** +** If the database schema is shared, then there is one instance of this +** structure for each database connection (sqlite3*) that uses the shared +** schema. This is because each database connection requires its own unique +** instance of the sqlite3_vtab* handle used to access the virtual table +** implementation. sqlite3_vtab* handles can not be shared between +** database connections, even when the rest of the in-memory database +** schema is shared, as the implementation often stores the database +** connection handle passed to it via the xConnect() or xCreate() method +** during initialization internally. This database connection handle may +** then be used by the virtual table implementation to access real tables +** within the database. So that they appear as part of the callers +** transaction, these accesses need to be made via the same database +** connection as that used to execute SQL operations on the virtual table. +** +** All VTable objects that correspond to a single table in a shared +** database schema are initially stored in a linked-list pointed to by +** the Table.pVTable member variable of the corresponding Table object. +** When an sqlite3_prepare() operation is required to access the virtual +** table, it searches the list for the VTable that corresponds to the +** database connection doing the preparing so as to use the correct +** sqlite3_vtab* handle in the compiled query. +** +** When an in-memory Table object is deleted (for example when the +** schema is being reloaded for some reason), the VTable objects are not +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed +** immediately. Instead, they are moved from the Table.pVTable list to +** another linked list headed by the sqlite3.pDisconnect member of the +** corresponding sqlite3 structure. They are then deleted/xDisconnected +** next time a statement is prepared using said sqlite3*. This is done +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. +** Refer to comments above function sqlite3VtabUnlockList() for an +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect +** list without holding the corresponding sqlite3.mutex mutex. +** +** The memory for objects of this type is always allocated by +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as +** the first argument. +*/ +struct VTable { + sqlite3 *db; /* Database connection associated with this table */ + Module *pMod; /* Pointer to module implementation */ + sqlite3_vtab *pVtab; /* Pointer to vtab instance */ + int nRef; /* Number of pointers to this structure */ + u8 bConstraint; /* True if constraints are supported */ + int iSavepoint; /* Depth of the SAVEPOINT stack */ + VTable *pNext; /* Next in linked list (see above) */ +}; + +/* +** The schema for each SQL table and view is represented in memory +** by an instance of the following structure. +*/ +struct Table { + char *zName; /* Name of the table or view */ + Column *aCol; /* Information about each column */ + Index *pIndex; /* List of SQL indexes on this table. */ + Select *pSelect; /* NULL for tables. Points to definition if a view. */ + FKey *pFKey; /* Linked list of all foreign keys in this table */ + char *zColAff; /* String defining the affinity of each column */ + ExprList *pCheck; /* All CHECK constraints */ + /* ... also used as column name list in a VIEW */ + int tnum; /* Root BTree page for this table */ + i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */ + i16 nCol; /* Number of columns in this table */ + u16 nRef; /* Number of pointers to this Table */ + LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */ + LogEst szTabRow; /* Estimated size of each table row in bytes */ +#ifdef SQLITE_ENABLE_COSTMULT + LogEst costMult; /* Cost multiplier for using this table */ +#endif + u8 tabFlags; /* Mask of TF_* values */ + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ +#ifndef SQLITE_OMIT_ALTERTABLE + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + int nModuleArg; /* Number of arguments to the module */ + char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */ + VTable *pVTable; /* List of VTable objects. */ +#endif + Trigger *pTrigger; /* List of triggers stored in pSchema */ + Schema *pSchema; /* Schema that contains this table */ + Table *pNextZombie; /* Next on the Parse.pZombieTab list */ +}; + +/* +** Allowed values for Table.tabFlags. +** +** TF_OOOHidden applies to tables or view that have hidden columns that are +** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING +** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden, +** the TF_OOOHidden attribute would apply in this case. Such tables require +** special handling during INSERT processing. +*/ +#define TF_Readonly 0x01 /* Read-only system table */ +#define TF_Ephemeral 0x02 /* An ephemeral table */ +#define TF_HasPrimaryKey 0x04 /* Table has a primary key */ +#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ +#define TF_Virtual 0x10 /* Is a virtual table */ +#define TF_WithoutRowid 0x20 /* No rowid. PRIMARY KEY is the key */ +#define TF_NoVisibleRowid 0x40 /* No user-visible "rowid" column */ +#define TF_OOOHidden 0x80 /* Out-of-Order hidden columns */ + + +/* +** Test to see whether or not a table is a virtual table. This is +** done as a macro so that it will be optimized out when virtual +** table support is omitted from the build. +*/ +#ifndef SQLITE_OMIT_VIRTUALTABLE +# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) +#else +# define IsVirtual(X) 0 +#endif + +/* +** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn() +** only works for non-virtual tables (ordinary tables and views) and is +** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The +** IsHiddenColumn() macro is general purpose. +*/ +#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS) +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +#elif !defined(SQLITE_OMIT_VIRTUALTABLE) +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +# define IsOrdinaryHiddenColumn(X) 0 +#else +# define IsHiddenColumn(X) 0 +# define IsOrdinaryHiddenColumn(X) 0 +#endif + + +/* Does the table have a rowid */ +#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) +#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) + +/* +** Each foreign key constraint is an instance of the following structure. +** +** A foreign key is associated with two tables. The "from" table is +** the table that contains the REFERENCES clause that creates the foreign +** key. The "to" table is the table that is named in the REFERENCES clause. +** Consider this example: +** +** CREATE TABLE ex1( +** a INTEGER PRIMARY KEY, +** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) +** ); +** +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". +** Equivalent names: +** +** from-table == child-table +** to-table == parent-table +** +** Each REFERENCES clause generates an instance of the following structure +** which is attached to the from-table. The to-table need not exist when +** the from-table is created. The existence of the to-table is not checked. +** +** The list of all parents for child Table X is held at X.pFKey. +** +** A list of all children for a table named Z (which might not even exist) +** is held in Schema.fkeyHash with a hash key of Z. +*/ +struct FKey { + Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */ + FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */ + char *zTo; /* Name of table that the key points to (aka: Parent) */ + FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */ + FKey *pPrevTo; /* Previous with the same zTo */ + int nCol; /* Number of columns in this key */ + /* EV: R-30323-21917 */ + u8 isDeferred; /* True if constraint checking is deferred till COMMIT */ + u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */ + Trigger *apTrigger[2];/* Triggers for aAction[] actions */ + struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ + int iFrom; /* Index of column in pFrom */ + char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */ + } aCol[1]; /* One entry for each of nCol columns */ +}; + +/* +** SQLite supports many different ways to resolve a constraint +** error. ROLLBACK processing means that a constraint violation +** causes the operation in process to fail and for the current transaction +** to be rolled back. ABORT processing means the operation in process +** fails and any prior changes from that one operation are backed out, +** but the transaction is not rolled back. FAIL processing means that +** the operation in progress stops and returns an error code. But prior +** changes due to the same operation are not backed out and no rollback +** occurs. IGNORE means that the particular row that caused the constraint +** error is not inserted or updated. Processing continues and no error +** is returned. REPLACE means that preexisting database rows that caused +** a UNIQUE constraint violation are removed so that the new insert or +** update can proceed. Processing continues and no error is reported. +** +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the +** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign +** key is set to NULL. CASCADE means that a DELETE or UPDATE of the +** referenced table row is propagated into the row that holds the +** foreign key. +** +** The following symbolic values are used to record which type +** of action to take. +*/ +#define OE_None 0 /* There is no constraint to check */ +#define OE_Rollback 1 /* Fail the operation and rollback the transaction */ +#define OE_Abort 2 /* Back out changes but do no rollback transaction */ +#define OE_Fail 3 /* Stop the operation but leave all prior changes */ +#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ +#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ + +#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ +#define OE_SetNull 7 /* Set the foreign key value to NULL */ +#define OE_SetDflt 8 /* Set the foreign key value to its default */ +#define OE_Cascade 9 /* Cascade the changes */ + +#define OE_Default 10 /* Do whatever the default action is */ + + +/* +** An instance of the following structure is passed as the first +** argument to sqlite3VdbeKeyCompare and is used to control the +** comparison of the two index keys. +** +** Note that aSortOrder[] and aColl[] have nField+1 slots. There +** are nField slots for the columns of an index then one extra slot +** for the rowid at the end. +*/ +struct KeyInfo { + u32 nRef; /* Number of references to this KeyInfo object */ + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ + u16 nField; /* Number of key columns in the index */ + u16 nXField; /* Number of columns beyond the key columns */ + sqlite3 *db; /* The database connection */ + u8 *aSortOrder; /* Sort order for each column. */ + CollSeq *aColl[1]; /* Collating sequence for each term of the key */ +}; + +/* +** This object holds a record which has been parsed out into individual +** fields, for the purposes of doing a comparison. +** +** A record is an object that contains one or more fields of data. +** Records are used to store the content of a table row and to store +** the key of an index. A blob encoding of a record is created by +** the OP_MakeRecord opcode of the VDBE and is disassembled by the +** OP_Column opcode. +** +** An instance of this object serves as a "key" for doing a search on +** an index b+tree. The goal of the search is to find the entry that +** is closed to the key described by this object. This object might hold +** just a prefix of the key. The number of fields is given by +** pKeyInfo->nField. +** +** The r1 and r2 fields are the values to return if this key is less than +** or greater than a key in the btree, respectively. These are normally +** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree +** is in DESC order. +** +** The key comparison functions actually return default_rc when they find +** an equals comparison. default_rc can be -1, 0, or +1. If there are +** multiple entries in the b-tree with the same key (when only looking +** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to +** cause the search to find the last match, or +1 to cause the search to +** find the first match. +** +** The key comparison functions will set eqSeen to true if they ever +** get and equal results when comparing this structure to a b-tree record. +** When default_rc!=0, the search might end up on the record immediately +** before the first match or immediately after the last match. The +** eqSeen field will indicate whether or not an exact match exists in the +** b-tree. +*/ +struct UnpackedRecord { + KeyInfo *pKeyInfo; /* Collation and sort-order information */ + Mem *aMem; /* Values */ + u16 nField; /* Number of entries in apMem[] */ + i8 default_rc; /* Comparison result if keys are equal */ + u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */ + i8 r1; /* Value to return if (lhs > rhs) */ + i8 r2; /* Value to return if (rhs < lhs) */ + u8 eqSeen; /* True if an equality comparison has been seen */ +}; + + +/* +** Each SQL index is represented in memory by an +** instance of the following structure. +** +** The columns of the table that are to be indexed are described +** by the aiColumn[] field of this structure. For example, suppose +** we have the following table and index: +** +** CREATE TABLE Ex1(c1 int, c2 int, c3 text); +** CREATE INDEX Ex2 ON Ex1(c3,c1); +** +** In the Table structure describing Ex1, nCol==3 because there are +** three columns in the table. In the Index structure describing +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. +** The second column to be indexed (c1) has an index of 0 in +** Ex1.aCol[], hence Ex2.aiColumn[1]==0. +** +** The Index.onError field determines whether or not the indexed columns +** must be unique and what to do if they are not. When Index.onError=OE_None, +** it means this is not a unique index. Otherwise it is a unique index +** and the value of Index.onError indicate the which conflict resolution +** algorithm to employ whenever an attempt is made to insert a non-unique +** element. +** +** While parsing a CREATE TABLE or CREATE INDEX statement in order to +** generate VDBE code (as opposed to parsing one read from an sqlite_master +** table as part of parsing an existing database schema), transient instances +** of this structure may be created. In this case the Index.tnum variable is +** used to store the address of a VDBE instruction, not a database page +** number (it cannot - the database page is not allocated until the VDBE +** program is executed). See convertToWithoutRowidTable() for details. +*/ +struct Index { + char *zName; /* Name of this index */ + i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */ + LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */ + Table *pTable; /* The SQL table being indexed */ + char *zColAff; /* String defining the affinity of each column */ + Index *pNext; /* The next index associated with the same table */ + Schema *pSchema; /* Schema containing this index */ + u8 *aSortOrder; /* for each column: True==DESC, False==ASC */ + const char **azColl; /* Array of collation sequence names for index */ + Expr *pPartIdxWhere; /* WHERE clause for partial indices */ + ExprList *aColExpr; /* Column expressions */ + int tnum; /* DB Page containing root of this index */ + LogEst szIdxRow; /* Estimated average row size in bytes */ + u16 nKeyCol; /* Number of columns forming the key */ + u16 nColumn; /* Number of columns stored in the index */ + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ + unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ + unsigned bUnordered:1; /* Use this index for == or IN queries only */ + unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ + unsigned isResized:1; /* True if resizeIndexObject() has been called */ + unsigned isCovering:1; /* True if this is a covering index */ + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int nSample; /* Number of elements in aSample[] */ + int nSampleCol; /* Size of IndexSample.anEq[] and so on */ + tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ + IndexSample *aSample; /* Samples of the left-most key */ + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ +#endif +}; + +/* +** Allowed values for Index.idxType +*/ +#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */ +#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */ +#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */ + +/* Return true if index X is a PRIMARY KEY index */ +#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY) + +/* Return true if index X is a UNIQUE index */ +#define IsUniqueIndex(X) ((X)->onError!=OE_None) + +/* The Index.aiColumn[] values are normally positive integer. But +** there are some negative values that have special meaning: +*/ +#define XN_ROWID (-1) /* Indexed column is the rowid */ +#define XN_EXPR (-2) /* Indexed column is an expression */ + +/* +** Each sample stored in the sqlite_stat3 table is represented in memory +** using a structure of this type. See documentation at the top of the +** analyze.c source file for additional information. +*/ +struct IndexSample { + void *p; /* Pointer to sampled record */ + int n; /* Size of record in bytes */ + tRowcnt *anEq; /* Est. number of rows where the key equals this sample */ + tRowcnt *anLt; /* Est. number of rows where key is less than this sample */ + tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */ +}; + +/* +** Each token coming out of the lexer is an instance of +** this structure. Tokens are also used as part of an expression. +** +** Note if Token.z==0 then Token.dyn and Token.n are undefined and +** may contain random values. Do not make any assumptions about Token.dyn +** and Token.n when Token.z==0. +*/ +struct Token { + const char *z; /* Text of the token. Not NULL-terminated! */ + unsigned int n; /* Number of characters in this token */ +}; + +/* +** An instance of this structure contains information needed to generate +** code for a SELECT that contains aggregate functions. +** +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a +** pointer to this structure. The Expr.iColumn field is the index in +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate +** code for that node. +** +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the +** original Select structure that describes the SELECT statement. These +** fields do not need to be freed when deallocating the AggInfo structure. +*/ +struct AggInfo { + u8 directMode; /* Direct rendering mode means take data directly + ** from source tables rather than from accumulators */ + u8 useSortingIdx; /* In direct mode, reference the sorting index rather + ** than the source table */ + int sortingIdx; /* Cursor number of the sorting index */ + int sortingIdxPTab; /* Cursor number of pseudo-table */ + int nSortingColumn; /* Number of columns in the sorting index */ + int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */ + ExprList *pGroupBy; /* The group by clause */ + struct AggInfo_col { /* For each column used in source tables */ + Table *pTab; /* Source table */ + int iTable; /* Cursor number of the source table */ + int iColumn; /* Column number within the source table */ + int iSorterColumn; /* Column number in the sorting index */ + int iMem; /* Memory location that acts as accumulator */ + Expr *pExpr; /* The original expression */ + } *aCol; + int nColumn; /* Number of used entries in aCol[] */ + int nAccumulator; /* Number of columns that show through to the output. + ** Additional columns are used only as parameters to + ** aggregate functions */ + struct AggInfo_func { /* For each aggregate function */ + Expr *pExpr; /* Expression encoding the function */ + FuncDef *pFunc; /* The aggregate function implementation */ + int iMem; /* Memory location that acts as accumulator */ + int iDistinct; /* Ephemeral table used to enforce DISTINCT */ + } *aFunc; + int nFunc; /* Number of entries in aFunc[] */ +}; + +/* +** The datatype ynVar is a signed integer, either 16-bit or 32-bit. +** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater +** than 32767 we have to make it 32-bit. 16-bit is preferred because +** it uses less memory in the Expr object, which is a big memory user +** in systems with lots of prepared statements. And few applications +** need more than about 10 or 20 variables. But some extreme users want +** to have prepared statements with over 32767 variables, and for them +** the option is available (at compile-time). +*/ +#if SQLITE_MAX_VARIABLE_NUMBER<=32767 +typedef i16 ynVar; +#else +typedef int ynVar; +#endif + +/* +** Each node of an expression in the parse tree is an instance +** of this structure. +** +** Expr.op is the opcode. The integer parser token codes are reused +** as opcodes here. For example, the parser defines TK_GE to be an integer +** code representing the ">=" operator. This same integer code is reused +** to represent the greater-than-or-equal-to operator in the expression +** tree. +** +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, +** or TK_STRING), then Expr.token contains the text of the SQL literal. If +** the expression is a variable (TK_VARIABLE), then Expr.token contains the +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), +** then Expr.token contains the name of the function. +** +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a +** binary operator. Either or both may be NULL. +** +** Expr.x.pList is a list of arguments if the expression is an SQL function, +** a CASE expression or an IN expression of the form " IN (, ...)". +** Expr.x.pSelect is used if the expression is a sub-select or an expression of +** the form " IN (SELECT ...)". If the EP_xIsSelect bit is set in the +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is +** valid. +** +** An expression of the form ID or ID.ID refers to a column in a table. +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is +** the integer cursor number of a VDBE cursor pointing to that table and +** Expr.iColumn is the column number for the specific column. If the +** expression is used as a result in an aggregate SELECT, then the +** value is also stored in the Expr.iAgg column in the aggregate so that +** it can be accessed after all aggregates are computed. +** +** If the expression is an unbound variable marker (a question mark +** character '?' in the original SQL) then the Expr.iTable holds the index +** number for that variable. +** +** If the expression is a subquery then Expr.iColumn holds an integer +** register number containing the result of the subquery. If the +** subquery gives a constant result, then iTable is -1. If the subquery +** gives a different answer at different times during statement processing +** then iTable is the address of a subroutine that computes the subquery. +** +** If the Expr is of type OP_Column, and the table it is selecting from +** is a disk table or the "old.*" pseudo-table, then pTab points to the +** corresponding table definition. +** +** ALLOCATION NOTES: +** +** Expr objects can use a lot of memory space in database schema. To +** help reduce memory requirements, sometimes an Expr object will be +** truncated. And to reduce the number of memory allocations, sometimes +** two or more Expr objects will be stored in a single memory allocation, +** together with Expr.zToken strings. +** +** If the EP_Reduced and EP_TokenOnly flags are set when +** an Expr object is truncated. When EP_Reduced is set, then all +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees +** are contained within the same memory allocation. Note, however, that +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately +** allocated, regardless of whether or not EP_Reduced is set. +*/ +struct Expr { + u8 op; /* Operation performed by this node */ + char affinity; /* The affinity of the column or 0 if not a column */ + u32 flags; /* Various flags. EP_* See below */ + union { + char *zToken; /* Token value. Zero terminated and dequoted */ + int iValue; /* Non-negative integer value if EP_IntValue */ + } u; + + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no + ** space is allocated for the fields below this point. An attempt to + ** access them will result in a segfault or malfunction. + *********************************************************************/ + + Expr *pLeft; /* Left subnode */ + Expr *pRight; /* Right subnode */ + union { + ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */ + Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */ + } x; + + /* If the EP_Reduced flag is set in the Expr.flags mask, then no + ** space is allocated for the fields below this point. An attempt to + ** access them will result in a segfault or malfunction. + *********************************************************************/ + +#if SQLITE_MAX_EXPR_DEPTH>0 + int nHeight; /* Height of the tree headed by this node */ +#endif + int iTable; /* TK_COLUMN: cursor number of table holding column + ** TK_REGISTER: register number + ** TK_TRIGGER: 1 -> new, 0 -> old + ** EP_Unlikely: 134217728 times likelihood */ + ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. + ** TK_VARIABLE: variable number (always >= 1). */ + i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ + i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ + u8 op2; /* TK_REGISTER: original value of Expr.op + ** TK_COLUMN: the value of p5 for OP_Column + ** TK_AGG_FUNCTION: nesting depth */ + AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ + Table *pTab; /* Table for TK_COLUMN expressions. */ +}; + +/* +** The following are the meanings of bits in the Expr.flags field. +*/ +#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */ +#define EP_Agg 0x000002 /* Contains one or more aggregate functions */ +#define EP_Resolved 0x000004 /* IDs have been resolved to COLUMNs */ +#define EP_Error 0x000008 /* Expression contains one or more errors */ +#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */ +#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */ +#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */ +#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */ +#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */ +#define EP_Generic 0x000200 /* Ignore COLLATE or affinity on this tree */ +#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */ +#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */ +#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */ +#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ +#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ +#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */ +#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */ +#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */ +#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */ +#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ +#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */ +#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */ +#define EP_Alias 0x400000 /* Is an alias for a result set column */ + +/* +** Combinations of two or more EP_* flags +*/ +#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */ + +/* +** These macros can be used to test, set, or clear bits in the +** Expr.flags field. +*/ +#define ExprHasProperty(E,P) (((E)->flags&(P))!=0) +#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P)) +#define ExprSetProperty(E,P) (E)->flags|=(P) +#define ExprClearProperty(E,P) (E)->flags&=~(P) + +/* The ExprSetVVAProperty() macro is used for Verification, Validation, +** and Accreditation only. It works like ExprSetProperty() during VVA +** processes but is a no-op for delivery. +*/ +#ifdef SQLITE_DEBUG +# define ExprSetVVAProperty(E,P) (E)->flags|=(P) +#else +# define ExprSetVVAProperty(E,P) +#endif + +/* +** Macros to determine the number of bytes required by a normal Expr +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags +** and an Expr struct with the EP_TokenOnly flag set. +*/ +#define EXPR_FULLSIZE sizeof(Expr) /* Full size */ +#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */ +#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */ + +/* +** Flags passed to the sqlite3ExprDup() function. See the header comment +** above sqlite3ExprDup() for details. +*/ +#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */ + +/* +** A list of expressions. Each expression may optionally have a +** name. An expr/name combination can be used in several ways, such +** as the list of "expr AS ID" fields following a "SELECT" or in the +** list of "ID = expr" items in an UPDATE. A list of expressions can +** also be used as the argument to a function, in which case the a.zName +** field is not used. +** +** By default the Expr.zSpan field holds a human-readable description of +** the expression that is used in the generation of error messages and +** column labels. In this case, Expr.zSpan is typically the text of a +** column expression as it exists in a SELECT statement. However, if +** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name +** of the result column in the form: DATABASE.TABLE.COLUMN. This later +** form is used for name resolution with nested FROM clauses. +*/ +struct ExprList { + int nExpr; /* Number of expressions on the list */ + struct ExprList_item { /* For each expression in the list */ + Expr *pExpr; /* The list of expressions */ + char *zName; /* Token associated with this expression */ + char *zSpan; /* Original text of the expression */ + u8 sortOrder; /* 1 for DESC or 0 for ASC */ + unsigned done :1; /* A flag to indicate when processing is finished */ + unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */ + unsigned reusable :1; /* Constant expression is reusable */ + union { + struct { + u16 iOrderByCol; /* For ORDER BY, column number in result set */ + u16 iAlias; /* Index into Parse.aAlias[] for zName */ + } x; + int iConstExprReg; /* Register in which Expr value is cached */ + } u; + } *a; /* Alloc a power of two greater or equal to nExpr */ +}; + +/* +** An instance of this structure is used by the parser to record both +** the parse tree for an expression and the span of input text for an +** expression. +*/ +struct ExprSpan { + Expr *pExpr; /* The expression parse tree */ + const char *zStart; /* First character of input text */ + const char *zEnd; /* One character past the end of input text */ +}; + +/* +** An instance of this structure can hold a simple list of identifiers, +** such as the list "a,b,c" in the following statements: +** +** INSERT INTO t(a,b,c) VALUES ...; +** CREATE INDEX idx ON t(a,b,c); +** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...; +** +** The IdList.a.idx field is used when the IdList represents the list of +** column names after a table name in an INSERT statement. In the statement +** +** INSERT INTO t(a,b,c) ... +** +** If "a" is the k-th column of table "t", then IdList.a[0].idx==k. +*/ +struct IdList { + struct IdList_item { + char *zName; /* Name of the identifier */ + int idx; /* Index in some Table.aCol[] of a column named zName */ + } *a; + int nId; /* Number of identifiers on the list */ +}; + +/* +** The bitmask datatype defined below is used for various optimizations. +** +** Changing this from a 64-bit to a 32-bit type limits the number of +** tables in a join to 32 instead of 64. But it also reduces the size +** of the library by 738 bytes on ix86. +*/ +#ifdef SQLITE_BITMASK_TYPE + typedef SQLITE_BITMASK_TYPE Bitmask; +#else + typedef u64 Bitmask; +#endif + +/* +** The number of bits in a Bitmask. "BMS" means "BitMask Size". +*/ +#define BMS ((int)(sizeof(Bitmask)*8)) + +/* +** A bit in a Bitmask +*/ +#define MASKBIT(n) (((Bitmask)1)<<(n)) +#define MASKBIT32(n) (((unsigned int)1)<<(n)) +#define ALLBITS ((Bitmask)-1) + +/* +** The following structure describes the FROM clause of a SELECT statement. +** Each table or subquery in the FROM clause is a separate element of +** the SrcList.a[] array. +** +** With the addition of multiple database support, the following structure +** can also be used to describe a particular table such as the table that +** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL, +** such a table must be a simple name: ID. But in SQLite, the table can +** now be identified by a database name, a dot, then the table name: ID.ID. +** +** The jointype starts out showing the join type between the current table +** and the next table on the list. The parser builds the list this way. +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each +** jointype expresses the join between the table and the previous table. +** +** In the colUsed field, the high-order bit (bit 63) is set if the table +** contains more than 63 columns and the 64-th or later column is used. +*/ +struct SrcList { + int nSrc; /* Number of tables or subqueries in the FROM clause */ + u32 nAlloc; /* Number of entries allocated in a[] below */ + struct SrcList_item { + Schema *pSchema; /* Schema to which this item is fixed */ + char *zDatabase; /* Name of database holding this table */ + char *zName; /* Name of the table */ + char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ + Table *pTab; /* An SQL table corresponding to zName */ + Select *pSelect; /* A SELECT statement used in place of a table name */ + int addrFillSub; /* Address of subroutine to manifest a subquery */ + int regReturn; /* Register holding return address of addrFillSub */ + int regResult; /* Registers holding results of a co-routine */ + struct { + u8 jointype; /* Type of join between this able and the previous */ + unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */ + unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */ + unsigned isTabFunc :1; /* True if table-valued-function syntax */ + unsigned isCorrelated :1; /* True if sub-query is correlated */ + unsigned viaCoroutine :1; /* Implemented as a co-routine */ + unsigned isRecursive :1; /* True for recursive reference in WITH */ + } fg; +#ifndef SQLITE_OMIT_EXPLAIN + u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ +#endif + int iCursor; /* The VDBE cursor number used to access this table */ + Expr *pOn; /* The ON clause of a join */ + IdList *pUsing; /* The USING clause of a join */ + Bitmask colUsed; /* Bit N (1<" clause */ + ExprList *pFuncArg; /* Arguments to table-valued-function */ + } u1; + Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */ + } a[1]; /* One entry for each identifier on the list */ +}; + +/* +** Permitted values of the SrcList.a.jointype field +*/ +#define JT_INNER 0x0001 /* Any kind of inner or cross join */ +#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */ +#define JT_NATURAL 0x0004 /* True for a "natural" join */ +#define JT_LEFT 0x0008 /* Left outer join */ +#define JT_RIGHT 0x0010 /* Right outer join */ +#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */ +#define JT_ERROR 0x0040 /* unknown or unsupported join type */ + + +/* +** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() +** and the WhereInfo.wctrlFlags member. +** +** Value constraints (enforced via assert()): +** WHERE_USE_LIMIT == SF_FixedLimit +*/ +#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ +#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ +#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ +#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ +#define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ +#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ +#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ +#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ +#define WHERE_NO_AUTOINDEX 0x0080 /* Disallow automatic indexes */ +#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ +#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ +#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ +#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ +#define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ +#define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */ +#define WHERE_USE_LIMIT 0x4000 /* There is a constant LIMIT clause */ +#define WHERE_SEEK_TABLE 0x8000 /* Do not defer seeks on main table */ + +/* Allowed return values from sqlite3WhereIsDistinct() +*/ +#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */ +#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */ +#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */ +#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */ + +/* +** A NameContext defines a context in which to resolve table and column +** names. The context consists of a list of tables (the pSrcList) field and +** a list of named expression (pEList). The named expression list may +** be NULL. The pSrc corresponds to the FROM clause of a SELECT or +** to the table being operated on by INSERT, UPDATE, or DELETE. The +** pEList corresponds to the result set of a SELECT and is NULL for +** other statements. +** +** NameContexts can be nested. When resolving names, the inner-most +** context is searched first. If no match is found, the next outer +** context is checked. If there is still no match, the next context +** is checked. This process continues until either a match is found +** or all contexts are check. When a match is found, the nRef member of +** the context containing the match is incremented. +** +** Each subquery gets a new NameContext. The pNext field points to the +** NameContext in the parent query. Thus the process of scanning the +** NameContext list corresponds to searching through successively outer +** subqueries looking for a match. +*/ +struct NameContext { + Parse *pParse; /* The parser */ + SrcList *pSrcList; /* One or more tables used to resolve names */ + ExprList *pEList; /* Optional list of result-set columns */ + AggInfo *pAggInfo; /* Information about aggregates at this level */ + NameContext *pNext; /* Next outer name context. NULL for outermost */ + int nRef; /* Number of names resolved by this context */ + int nErr; /* Number of errors encountered while resolving names */ + u16 ncFlags; /* Zero or more NC_* flags defined below */ +}; + +/* +** Allowed values for the NameContext, ncFlags field. +** +** Value constraints (all checked via assert()): +** NC_HasAgg == SF_HasAgg +** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX +** +*/ +#define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */ +#define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */ +#define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */ +#define NC_InAggFunc 0x0008 /* True if analyzing arguments to an agg func */ +#define NC_HasAgg 0x0010 /* One or more aggregate functions seen */ +#define NC_IdxExpr 0x0020 /* True if resolving columns of CREATE INDEX */ +#define NC_VarSelect 0x0040 /* A correlated subquery has been seen */ +#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */ + +/* +** An instance of the following structure contains all information +** needed to generate code for a single SELECT statement. +** +** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0. +** If there is a LIMIT clause, the parser sets nLimit to the value of the +** limit and nOffset to the value of the offset (or 0 if there is not +** offset). But later on, nLimit and nOffset become the memory locations +** in the VDBE that record the limit and offset counters. +** +** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. +** These addresses must be stored so that we can go back and fill in +** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor +** the number of columns in P2 can be computed at the same time +** as the OP_OpenEphm instruction is coded because not +** enough information about the compound query is known at that point. +** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences +** for the result set. The KeyInfo for addrOpenEphm[2] contains collating +** sequences for the ORDER BY clause. +*/ +struct Select { + ExprList *pEList; /* The fields of the result */ + u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ + LogEst nSelectRow; /* Estimated number of result rows */ + u32 selFlags; /* Various SF_* values */ + int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ +#if SELECTTRACE_ENABLED + char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ +#endif + int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ + SrcList *pSrc; /* The FROM clause */ + Expr *pWhere; /* The WHERE clause */ + ExprList *pGroupBy; /* The GROUP BY clause */ + Expr *pHaving; /* The HAVING clause */ + ExprList *pOrderBy; /* The ORDER BY clause */ + Select *pPrior; /* Prior select in a compound select statement */ + Select *pNext; /* Next select to the left in a compound */ + Expr *pLimit; /* LIMIT expression. NULL means not used. */ + Expr *pOffset; /* OFFSET expression. NULL means not used. */ + With *pWith; /* WITH clause attached to this select. Or NULL. */ +}; + +/* +** Allowed values for Select.selFlags. The "SF" prefix stands for +** "Select Flag". +** +** Value constraints (all checked via assert()) +** SF_HasAgg == NC_HasAgg +** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX +** SF_FixedLimit == WHERE_USE_LIMIT +*/ +#define SF_Distinct 0x00001 /* Output should be DISTINCT */ +#define SF_All 0x00002 /* Includes the ALL keyword */ +#define SF_Resolved 0x00004 /* Identifiers have been resolved */ +#define SF_Aggregate 0x00008 /* Contains agg functions or a GROUP BY */ +#define SF_HasAgg 0x00010 /* Contains aggregate functions */ +#define SF_UsesEphemeral 0x00020 /* Uses the OpenEphemeral opcode */ +#define SF_Expanded 0x00040 /* sqlite3SelectExpand() called on this */ +#define SF_HasTypeInfo 0x00080 /* FROM subqueries have Table metadata */ +#define SF_Compound 0x00100 /* Part of a compound query */ +#define SF_Values 0x00200 /* Synthesized from VALUES clause */ +#define SF_MultiValue 0x00400 /* Single VALUES term with multiple rows */ +#define SF_NestedFrom 0x00800 /* Part of a parenthesized FROM clause */ +#define SF_MinMaxAgg 0x01000 /* Aggregate containing min() or max() */ +#define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */ +#define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */ +#define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */ +#define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */ +#define SF_IncludeHidden 0x20000 /* Include hidden columns in output */ + + +/* +** The results of a SELECT can be distributed in several ways, as defined +** by one of the following macros. The "SRT" prefix means "SELECT Result +** Type". +** +** SRT_Union Store results as a key in a temporary index +** identified by pDest->iSDParm. +** +** SRT_Except Remove results from the temporary index pDest->iSDParm. +** +** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result +** set is not empty. +** +** SRT_Discard Throw the results away. This is used by SELECT +** statements within triggers whose only purpose is +** the side-effects of functions. +** +** All of the above are free to ignore their ORDER BY clause. Those that +** follow must honor the ORDER BY clause. +** +** SRT_Output Generate a row of output (using the OP_ResultRow +** opcode) for each row in the result set. +** +** SRT_Mem Only valid if the result is a single column. +** Store the first column of the first result row +** in register pDest->iSDParm then abandon the rest +** of the query. This destination implies "LIMIT 1". +** +** SRT_Set The result must be a single column. Store each +** row of result as the key in table pDest->iSDParm. +** Apply the affinity pDest->affSdst before storing +** results. Used to implement "IN (SELECT ...)". +** +** SRT_EphemTab Create an temporary table pDest->iSDParm and store +** the result there. The cursor is left open after +** returning. This is like SRT_Table except that +** this destination uses OP_OpenEphemeral to create +** the table first. +** +** SRT_Coroutine Generate a co-routine that returns a new row of +** results each time it is invoked. The entry point +** of the co-routine is stored in register pDest->iSDParm +** and the result row is stored in pDest->nDest registers +** starting with pDest->iSdst. +** +** SRT_Table Store results in temporary table pDest->iSDParm. +** SRT_Fifo This is like SRT_EphemTab except that the table +** is assumed to already be open. SRT_Fifo has +** the additional property of being able to ignore +** the ORDER BY clause. +** +** SRT_DistFifo Store results in a temporary table pDest->iSDParm. +** But also use temporary table pDest->iSDParm+1 as +** a record of all prior results and ignore any duplicate +** rows. Name means: "Distinct Fifo". +** +** SRT_Queue Store results in priority queue pDest->iSDParm (really +** an index). Append a sequence number so that all entries +** are distinct. +** +** SRT_DistQueue Store results in priority queue pDest->iSDParm only if +** the same record has never been stored before. The +** index at pDest->iSDParm+1 hold all prior stores. +*/ +#define SRT_Union 1 /* Store result as keys in an index */ +#define SRT_Except 2 /* Remove result from a UNION index */ +#define SRT_Exists 3 /* Store 1 if the result is not empty */ +#define SRT_Discard 4 /* Do not save the results anywhere */ +#define SRT_Fifo 5 /* Store result as data with an automatic rowid */ +#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */ +#define SRT_Queue 7 /* Store result in an queue */ +#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */ + +/* The ORDER BY clause is ignored for all of the above */ +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue) + +#define SRT_Output 9 /* Output each row of result */ +#define SRT_Mem 10 /* Store result in a memory cell */ +#define SRT_Set 11 /* Store results as keys in an index */ +#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */ +#define SRT_Coroutine 13 /* Generate a single row of result */ +#define SRT_Table 14 /* Store result as data with an automatic rowid */ + +/* +** An instance of this object describes where to put of the results of +** a SELECT statement. +*/ +struct SelectDest { + u8 eDest; /* How to dispose of the results. On of SRT_* above. */ + char affSdst; /* Affinity used when eDest==SRT_Set */ + int iSDParm; /* A parameter used by the eDest disposal method */ + int iSdst; /* Base register where results are written */ + int nSdst; /* Number of registers allocated */ + ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */ +}; + +/* +** During code generation of statements that do inserts into AUTOINCREMENT +** tables, the following information is attached to the Table.u.autoInc.p +** pointer of each autoincrement table to record some side information that +** the code generator needs. We have to keep per-table autoincrement +** information in case inserts are done within triggers. Triggers do not +** normally coordinate their activities, but we do need to coordinate the +** loading and saving of autoincrement information. +*/ +struct AutoincInfo { + AutoincInfo *pNext; /* Next info block in a list of them all */ + Table *pTab; /* Table this info block refers to */ + int iDb; /* Index in sqlite3.aDb[] of database holding pTab */ + int regCtr; /* Memory register holding the rowid counter */ +}; + +/* +** Size of the column cache +*/ +#ifndef SQLITE_N_COLCACHE +# define SQLITE_N_COLCACHE 10 +#endif + +/* +** At least one instance of the following structure is created for each +** trigger that may be fired while parsing an INSERT, UPDATE or DELETE +** statement. All such objects are stored in the linked list headed at +** Parse.pTriggerPrg and deleted once statement compilation has been +** completed. +** +** A Vdbe sub-program that implements the body and WHEN clause of trigger +** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of +** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable. +** The Parse.pTriggerPrg list never contains two entries with the same +** values for both pTrigger and orconf. +** +** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns +** accessed (or set to 0 for triggers fired as a result of INSERT +** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to +** a mask of new.* columns used by the program. +*/ +struct TriggerPrg { + Trigger *pTrigger; /* Trigger this program was coded from */ + TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */ + SubProgram *pProgram; /* Program implementing pTrigger/orconf */ + int orconf; /* Default ON CONFLICT policy */ + u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */ +}; + +/* +** The yDbMask datatype for the bitmask of all attached databases. +*/ +#if SQLITE_MAX_ATTACHED>30 + typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8]; +# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0) +# define DbMaskZero(M) memset((M),0,sizeof(M)) +# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7)) +# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M) +# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0) +#else + typedef unsigned int yDbMask; +# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0) +# define DbMaskZero(M) (M)=0 +# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I)) +# define DbMaskAllZero(M) (M)==0 +# define DbMaskNonZero(M) (M)!=0 +#endif + +/* +** An SQL parser context. A copy of this structure is passed through +** the parser and down into all the parser action routine in order to +** carry around information that is global to the entire parse. +** +** The structure is divided into two parts. When the parser and code +** generate call themselves recursively, the first part of the structure +** is constant but the second part is reset at the beginning and end of +** each recursion. +** +** The nTableLock and aTableLock variables are only used if the shared-cache +** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are +** used to store the set of table-locks required by the statement being +** compiled. Function sqlite3TableLock() is used to add entries to the +** list. +*/ +struct Parse { + sqlite3 *db; /* The main database structure */ + char *zErrMsg; /* An error message */ + Vdbe *pVdbe; /* An engine for executing database bytecode */ + int rc; /* Return code from execution */ + u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */ + u8 checkSchema; /* Causes schema cookie check after an error */ + u8 nested; /* Number of nested calls to the parser/code generator */ + u8 nTempReg; /* Number of temporary registers in aTempReg[] */ + u8 isMultiWrite; /* True if statement may modify/insert multiple rows */ + u8 mayAbort; /* True if statement may throw an ABORT exception */ + u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ + u8 okConstFactor; /* OK to factor out constants */ + u8 disableLookaside; /* Number of times lookaside has been disabled */ + u8 nColCache; /* Number of entries in aColCache[] */ + int aTempReg[8]; /* Holding area for temporary registers */ + int nRangeReg; /* Size of the temporary register block */ + int iRangeReg; /* First register in temporary register block */ + int nErr; /* Number of errors seen */ + int nTab; /* Number of previously allocated VDBE cursors */ + int nMem; /* Number of memory cells used so far */ + int nSet; /* Number of sets used so far */ + int nOnce; /* Number of OP_Once instructions so far */ + int nOpAlloc; /* Number of slots allocated for Vdbe.aOp[] */ + int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */ + int iFixedOp; /* Never back out opcodes iFixedOp-1 or earlier */ + int ckBase; /* Base register of data during check constraints */ + int iSelfTab; /* Table of an index whose exprs are being coded */ + int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */ + int iCacheCnt; /* Counter used to generate aColCache[].lru values */ + int nLabel; /* Number of labels used */ + int *aLabel; /* Space to hold the labels */ + struct yColCache { + int iTable; /* Table cursor number */ + i16 iColumn; /* Table column number */ + u8 tempReg; /* iReg is a temp register that needs to be freed */ + int iLevel; /* Nesting level */ + int iReg; /* Reg with value of this column. 0 means none. */ + int lru; /* Least recently used entry has the smallest value */ + } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */ + ExprList *pConstExpr;/* Constant expressions */ + Token constraintName;/* Name of the constraint currently being parsed */ + yDbMask writeMask; /* Start a write transaction on these databases */ + yDbMask cookieMask; /* Bitmask of schema verified databases */ + int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ + int regRowid; /* Register holding rowid of CREATE TABLE entry */ + int regRoot; /* Register holding root page number for new objects */ + int nMaxArg; /* Max args passed to user function by sub-program */ +#if SELECTTRACE_ENABLED + int nSelect; /* Number of SELECT statements seen */ + int nSelectIndent; /* How far to indent SELECTTRACE() output */ +#endif +#ifndef SQLITE_OMIT_SHARED_CACHE + int nTableLock; /* Number of locks in aTableLock */ + TableLock *aTableLock; /* Required table locks for shared-cache mode */ +#endif + AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ + + /* Information used while coding trigger programs. */ + Parse *pToplevel; /* Parse structure for main program (or NULL) */ + Table *pTriggerTab; /* Table triggers are being coded for */ + int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */ + u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ + u32 oldmask; /* Mask of old.* columns referenced */ + u32 newmask; /* Mask of new.* columns referenced */ + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ + u8 disableTriggers; /* True to disable triggers */ + + /************************************************************************ + ** Above is constant between recursions. Below is reset before and after + ** each recursion. The boundary between these two regions is determined + ** using offsetof(Parse,nVar) so the nVar field must be the first field + ** in the recursive region. + ************************************************************************/ + + ynVar nVar; /* Number of '?' variables seen in the SQL so far */ + int nzVar; /* Number of available slots in azVar[] */ + u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */ + u8 explain; /* True if the EXPLAIN flag is found on the query */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + u8 declareVtab; /* True if inside sqlite3_declare_vtab() */ + int nVtabLock; /* Number of virtual tables to lock */ +#endif + int nAlias; /* Number of aliased result set columns */ + int nHeight; /* Expression tree height of current sub-select */ +#ifndef SQLITE_OMIT_EXPLAIN + int iSelectId; /* ID of current select for EXPLAIN output */ + int iNextSelectId; /* Next available select ID for EXPLAIN output */ +#endif + char **azVar; /* Pointers to names of parameters */ + Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */ + const char *zTail; /* All SQL text past the last semicolon parsed */ + Table *pNewTable; /* A table being constructed by CREATE TABLE */ + Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */ + const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */ + Token sNameToken; /* Token with unqualified schema object name */ + Token sLastToken; /* The last token parsed */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + Token sArg; /* Complete text of a module argument */ + Table **apVtabLock; /* Pointer to virtual tables needing locking */ +#endif + Table *pZombieTab; /* List of Table objects to delete after code gen */ + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ + With *pWith; /* Current WITH clause, or NULL */ + With *pWithToFree; /* Free this WITH object at the end of the parse */ +}; + +/* +** Return true if currently inside an sqlite3_declare_vtab() call. +*/ +#ifdef SQLITE_OMIT_VIRTUALTABLE + #define IN_DECLARE_VTAB 0 +#else + #define IN_DECLARE_VTAB (pParse->declareVtab) +#endif + +/* +** An instance of the following structure can be declared on a stack and used +** to save the Parse.zAuthContext value so that it can be restored later. +*/ +struct AuthContext { + const char *zAuthContext; /* Put saved Parse.zAuthContext here */ + Parse *pParse; /* The Parse structure */ +}; + +/* +** Bitfield flags for P5 value in various opcodes. +** +** Value constraints (enforced via assert()): +** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH +** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF +** OPFLAG_BULKCSR == BTREE_BULKLOAD +** OPFLAG_SEEKEQ == BTREE_SEEK_EQ +** OPFLAG_FORDELETE == BTREE_FORDELETE +** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION +** OPFLAG_AUXDELETE == BTREE_AUXDELETE +*/ +#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */ + /* Also used in P2 (not P5) of OP_Delete */ +#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ +#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ +#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ +#define OPFLAG_APPEND 0x08 /* This is likely to be an append */ +#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */ +#endif +#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ +#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ +#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ +#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */ +#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */ +#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */ +#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ +#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */ +#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */ + +/* + * Each trigger present in the database schema is stored as an instance of + * struct Trigger. + * + * Pointers to instances of struct Trigger are stored in two ways. + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the + * database). This allows Trigger structures to be retrieved by name. + * 2. All triggers associated with a single table form a linked list, using the + * pNext member of struct Trigger. A pointer to the first element of the + * linked list is stored as the "pTrigger" member of the associated + * struct Table. + * + * The "step_list" member points to the first element of a linked list + * containing the SQL statements specified as the trigger program. + */ +struct Trigger { + char *zName; /* The name of the trigger */ + char *table; /* The table or view to which the trigger applies */ + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */ + u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ + Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */ + IdList *pColumns; /* If this is an UPDATE OF trigger, + the is stored here */ + Schema *pSchema; /* Schema containing the trigger */ + Schema *pTabSchema; /* Schema containing the table */ + TriggerStep *step_list; /* Link list of trigger program steps */ + Trigger *pNext; /* Next trigger associated with the table */ +}; + +/* +** A trigger is either a BEFORE or an AFTER trigger. The following constants +** determine which. +** +** If there are multiple triggers, you might of some BEFORE and some AFTER. +** In that cases, the constants below can be ORed together. +*/ +#define TRIGGER_BEFORE 1 +#define TRIGGER_AFTER 2 + +/* + * An instance of struct TriggerStep is used to store a single SQL statement + * that is a part of a trigger-program. + * + * Instances of struct TriggerStep are stored in a singly linked list (linked + * using the "pNext" member) referenced by the "step_list" member of the + * associated struct Trigger instance. The first element of the linked list is + * the first step of the trigger-program. + * + * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or + * "SELECT" statement. The meanings of the other members is determined by the + * value of "op" as follows: + * + * (op == TK_INSERT) + * orconf -> stores the ON CONFLICT algorithm + * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then + * this stores a pointer to the SELECT statement. Otherwise NULL. + * zTarget -> Dequoted name of the table to insert into. + * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then + * this stores values to be inserted. Otherwise NULL. + * pIdList -> If this is an INSERT INTO ... () VALUES ... + * statement, then this stores the column-names to be + * inserted into. + * + * (op == TK_DELETE) + * zTarget -> Dequoted name of the table to delete from. + * pWhere -> The WHERE clause of the DELETE statement if one is specified. + * Otherwise NULL. + * + * (op == TK_UPDATE) + * zTarget -> Dequoted name of the table to update. + * pWhere -> The WHERE clause of the UPDATE statement if one is specified. + * Otherwise NULL. + * pExprList -> A list of the columns to update and the expressions to update + * them to. See sqlite3Update() documentation of "pChanges" + * argument. + * + */ +struct TriggerStep { + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ + u8 orconf; /* OE_Rollback etc. */ + Trigger *pTrig; /* The trigger that this step is a part of */ + Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */ + char *zTarget; /* Target table for DELETE, UPDATE, INSERT */ + Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */ + ExprList *pExprList; /* SET clause for UPDATE. */ + IdList *pIdList; /* Column names for INSERT */ + TriggerStep *pNext; /* Next in the link-list */ + TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */ +}; + +/* +** The following structure contains information used by the sqliteFix... +** routines as they walk the parse tree to make database references +** explicit. +*/ +typedef struct DbFixer DbFixer; +struct DbFixer { + Parse *pParse; /* The parsing context. Error messages written here */ + Schema *pSchema; /* Fix items to this schema */ + int bVarOnly; /* Check for variable references only */ + const char *zDb; /* Make sure all objects are contained in this database */ + const char *zType; /* Type of the container - used for error messages */ + const Token *pName; /* Name of the container - used for error messages */ +}; + +/* +** An objected used to accumulate the text of a string where we +** do not necessarily know how big the string will be in the end. +*/ +struct StrAccum { + sqlite3 *db; /* Optional database for lookaside. Can be NULL */ + char *zBase; /* A base allocation. Not from malloc. */ + char *zText; /* The string collected so far */ + u32 nChar; /* Length of the string so far */ + u32 nAlloc; /* Amount of space allocated in zText */ + u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ + u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ + u8 printfFlags; /* SQLITE_PRINTF flags below */ +}; +#define STRACCUM_NOMEM 1 +#define STRACCUM_TOOBIG 2 +#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */ +#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */ +#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */ + +#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0) + + +/* +** A pointer to this structure is used to communicate information +** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback. +*/ +typedef struct { + sqlite3 *db; /* The database being initialized */ + char **pzErrMsg; /* Error message stored here */ + int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */ + int rc; /* Result code stored here */ +} InitData; + +/* +** Structure containing global configuration data for the SQLite library. +** +** This structure also contains some state information. +*/ +struct Sqlite3Config { + int bMemstat; /* True to enable memory status */ + int bCoreMutex; /* True to enable core mutexing */ + int bFullMutex; /* True to enable full mutexing */ + int bOpenUri; /* True to interpret filenames as URIs */ + int bUseCis; /* Use covering indices for full-scans */ + int mxStrlen; /* Maximum string length */ + int neverCorrupt; /* Database is always well-formed */ + int szLookaside; /* Default lookaside buffer size */ + int nLookaside; /* Default lookaside buffer count */ + int nStmtSpill; /* Stmt-journal spill-to-disk threshold */ + sqlite3_mem_methods m; /* Low-level memory allocation interface */ + sqlite3_mutex_methods mutex; /* Low-level mutex interface */ + sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */ + void *pHeap; /* Heap storage space */ + int nHeap; /* Size of pHeap[] */ + int mnReq, mxReq; /* Min and max heap requests sizes */ + sqlite3_int64 szMmap; /* mmap() space per open file */ + sqlite3_int64 mxMmap; /* Maximum value for szMmap */ + void *pScratch; /* Scratch memory */ + int szScratch; /* Size of each scratch buffer */ + int nScratch; /* Number of scratch buffers */ + void *pPage; /* Page cache memory */ + int szPage; /* Size of each page in pPage[] */ + int nPage; /* Number of pages in pPage[] */ + int mxParserStack; /* maximum depth of the parser stack */ + int sharedCacheEnabled; /* true if shared-cache mode enabled */ + u32 szPma; /* Maximum Sorter PMA size */ + /* The above might be initialized to non-zero. The following need to always + ** initially be zero, however. */ + int isInit; /* True after initialization has finished */ + int inProgress; /* True while initialization in progress */ + int isMutexInit; /* True after mutexes are initialized */ + int isMallocInit; /* True after malloc is initialized */ + int isPCacheInit; /* True after malloc is initialized */ + int nRefInitMutex; /* Number of users of pInitMutex */ + sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */ + void (*xLog)(void*,int,const char*); /* Function for logging */ + void *pLogArg; /* First argument to xLog() */ +#ifdef SQLITE_ENABLE_SQLLOG + void(*xSqllog)(void*,sqlite3*,const char*, int); + void *pSqllogArg; +#endif +#ifdef SQLITE_VDBE_COVERAGE + /* The following callback (if not NULL) is invoked on every VDBE branch + ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE. + */ + void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */ + void *pVdbeBranchArg; /* 1st argument */ +#endif +#ifndef SQLITE_OMIT_BUILTIN_TEST + int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ +#endif + int bLocaltimeFault; /* True to fail localtime() calls */ +}; + +/* +** This macro is used inside of assert() statements to indicate that +** the assert is only valid on a well-formed database. Instead of: +** +** assert( X ); +** +** One writes: +** +** assert( X || CORRUPT_DB ); +** +** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate +** that the database is definitely corrupt, only that it might be corrupt. +** For most test cases, CORRUPT_DB is set to false using a special +** sqlite3_test_control(). This enables assert() statements to prove +** things that are always true for well-formed databases. +*/ +#define CORRUPT_DB (sqlite3Config.neverCorrupt==0) + +/* +** Context pointer passed down through the tree-walk. +*/ +struct Walker { + Parse *pParse; /* Parser context. */ + int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ + int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ + void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ + int walkerDepth; /* Number of subqueries */ + u8 eCode; /* A small processing code */ + union { /* Extra data for callback */ + NameContext *pNC; /* Naming context */ + int n; /* A counter */ + int iCur; /* A cursor number */ + SrcList *pSrcList; /* FROM clause */ + struct SrcCount *pSrcCount; /* Counting column references */ + struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ + int *aiCol; /* array of column indexes */ + } u; +}; + +/* Forward declarations */ +SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*); +SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*); +SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*); +SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*); +SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*); +SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*); + +/* +** Return code from the parse-tree walking primitives and their +** callbacks. +*/ +#define WRC_Continue 0 /* Continue down into children */ +#define WRC_Prune 1 /* Omit children but continue walking siblings */ +#define WRC_Abort 2 /* Abandon the tree walk */ + +/* +** An instance of this structure represents a set of one or more CTEs +** (common table expressions) created by a single WITH clause. +*/ +struct With { + int nCte; /* Number of CTEs in the WITH clause */ + With *pOuter; /* Containing WITH clause, or NULL */ + struct Cte { /* For each CTE in the WITH clause.... */ + char *zName; /* Name of this CTE */ + ExprList *pCols; /* List of explicit column names, or NULL */ + Select *pSelect; /* The definition of this CTE */ + const char *zCteErr; /* Error message for circular references */ + } a[1]; +}; + +#ifdef SQLITE_DEBUG +/* +** An instance of the TreeView object is used for printing the content of +** data structures on sqlite3DebugPrintf() using a tree-like view. +*/ +struct TreeView { + int iLevel; /* Which level of the tree we are on */ + u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */ +}; +#endif /* SQLITE_DEBUG */ + +/* +** Assuming zIn points to the first byte of a UTF-8 character, +** advance zIn to point to the first byte of the next UTF-8 character. +*/ +#define SQLITE_SKIP_UTF8(zIn) { \ + if( (*(zIn++))>=0xc0 ){ \ + while( (*zIn & 0xc0)==0x80 ){ zIn++; } \ + } \ +} + +/* +** The SQLITE_*_BKPT macros are substitutes for the error codes with +** the same name but without the _BKPT suffix. These macros invoke +** routines that report the line-number on which the error originated +** using sqlite3_log(). The routines also provide a convenient place +** to set a debugger breakpoint. +*/ +SQLITE_PRIVATE int sqlite3CorruptError(int); +SQLITE_PRIVATE int sqlite3MisuseError(int); +SQLITE_PRIVATE int sqlite3CantopenError(int); +#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__) +#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__) +#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__) +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3NomemError(int); +SQLITE_PRIVATE int sqlite3IoerrnomemError(int); +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__) +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__) +#else +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM +#endif + +/* +** FTS3 and FTS4 both require virtual table support +*/ +#if defined(SQLITE_OMIT_VIRTUALTABLE) +# undef SQLITE_ENABLE_FTS3 +# undef SQLITE_ENABLE_FTS4 +#endif + +/* +** FTS4 is really an extension for FTS3. It is enabled using the +** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call +** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3. +*/ +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3) +# define SQLITE_ENABLE_FTS3 1 +#endif + +/* +** The ctype.h header is needed for non-ASCII systems. It is also +** needed by FTS3 when FTS3 is included in the amalgamation. +*/ +#if !defined(SQLITE_ASCII) || \ + (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION)) +# include +#endif + +/* +** The following macros mimic the standard library functions toupper(), +** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The +** sqlite versions only work for ASCII characters, regardless of locale. +*/ +#ifdef SQLITE_ASCII +# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) +# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01) +# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06) +# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02) +# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04) +# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08) +# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)]) +# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80) +#else +# define sqlite3Toupper(x) toupper((unsigned char)(x)) +# define sqlite3Isspace(x) isspace((unsigned char)(x)) +# define sqlite3Isalnum(x) isalnum((unsigned char)(x)) +# define sqlite3Isalpha(x) isalpha((unsigned char)(x)) +# define sqlite3Isdigit(x) isdigit((unsigned char)(x)) +# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) +# define sqlite3Tolower(x) tolower((unsigned char)(x)) +# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`') +#endif +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +SQLITE_PRIVATE int sqlite3IsIdChar(u8); +#endif + +/* +** Internal function prototypes +*/ +SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*); +SQLITE_PRIVATE int sqlite3Strlen30(const char*); +SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*); +#define sqlite3StrNICmp sqlite3_strnicmp + +SQLITE_PRIVATE int sqlite3MallocInit(void); +SQLITE_PRIVATE void sqlite3MallocEnd(void); +SQLITE_PRIVATE void *sqlite3Malloc(u64); +SQLITE_PRIVATE void *sqlite3MallocZero(u64); +SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64); +SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64); +SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64); +SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*); +SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64); +SQLITE_PRIVATE void *sqlite3Realloc(void*, u64); +SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64); +SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64); +SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*); +SQLITE_PRIVATE int sqlite3MallocSize(void*); +SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*); +SQLITE_PRIVATE void *sqlite3ScratchMalloc(int); +SQLITE_PRIVATE void sqlite3ScratchFree(void*); +SQLITE_PRIVATE void *sqlite3PageMalloc(int); +SQLITE_PRIVATE void sqlite3PageFree(void*); +SQLITE_PRIVATE void sqlite3MemSetDefault(void); +#ifndef SQLITE_OMIT_BUILTIN_TEST +SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); +#endif +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); + +/* +** On systems with ample stack space and that support alloca(), make +** use of alloca() to obtain space for large automatic objects. By default, +** obtain space from malloc(). +** +** The alloca() routine never returns NULL. This will cause code paths +** that deal with sqlite3StackAlloc() failures to be unreachable. +*/ +#ifdef SQLITE_USE_ALLOCA +# define sqlite3StackAllocRaw(D,N) alloca(N) +# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N) +# define sqlite3StackFree(D,P) +#else +# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N) +# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N) +# define sqlite3StackFree(D,P) sqlite3DbFree(D,P) +#endif + +#ifdef SQLITE_ENABLE_MEMSYS3 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); +#endif +#ifdef SQLITE_ENABLE_MEMSYS5 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void); +#endif + + +#ifndef SQLITE_MUTEX_OMIT +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void); +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void); +SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int); +SQLITE_PRIVATE int sqlite3MutexInit(void); +SQLITE_PRIVATE int sqlite3MutexEnd(void); +#endif +#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP) +SQLITE_PRIVATE void sqlite3MemoryBarrier(void); +#else +# define sqlite3MemoryBarrier() +#endif + +SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int); +SQLITE_PRIVATE void sqlite3StatusUp(int, int); +SQLITE_PRIVATE void sqlite3StatusDown(int, int); +SQLITE_PRIVATE void sqlite3StatusHighwater(int, int); + +/* Access to mutexes used by sqlite3_status() */ +SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void); +SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void); + +#ifndef SQLITE_OMIT_FLOATING_POINT +SQLITE_PRIVATE int sqlite3IsNaN(double); +#else +# define sqlite3IsNaN(X) 0 +#endif + +/* +** An instance of the following structure holds information about SQL +** functions arguments that are the parameters to the printf() function. +*/ +struct PrintfArguments { + int nArg; /* Total number of arguments */ + int nUsed; /* Number of arguments used so far */ + sqlite3_value **apArg; /* The argument values */ +}; + +SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, const char*, va_list); +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...); +SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); +SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) +SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); +#endif +#if defined(SQLITE_TEST) +SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); +#endif + +#if defined(SQLITE_DEBUG) +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); +SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); +SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8); +#endif + + +SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*); +SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...); +SQLITE_PRIVATE void sqlite3Dequote(char*); +SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*); +SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int); +SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **); +SQLITE_PRIVATE void sqlite3FinishCoding(Parse*); +SQLITE_PRIVATE int sqlite3GetTempReg(Parse*); +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int); +SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int); +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int); +SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int); +#endif +SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int); +SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*); +SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*); +SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); +SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*); +SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*); +SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*); +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*); +SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*); +SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*); +SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int); +SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int); +SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*); +SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*); +SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*); +SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**); +SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**); +SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int); +SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*); +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int); +SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*); +SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*); +SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*); +SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**); +SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*); +SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*); +SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int); +SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*); +SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16); +SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int); +#if SQLITE_ENABLE_HIDDEN_COLUMNS +SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*); +#else +# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */ +#endif +SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*); +SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int); +SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int); +SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*); +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*); +SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*); +SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*); +SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*, + sqlite3_vfs**,char**,char **); +SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*); +SQLITE_PRIVATE int sqlite3CodeOnce(Parse *); + +#ifdef SQLITE_OMIT_BUILTIN_TEST +# define sqlite3FaultSim(X) SQLITE_OK +#else +SQLITE_PRIVATE int sqlite3FaultSim(int); +#endif + +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32); +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32); +SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32); +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32); +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*); +SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*); +SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*); +#ifndef SQLITE_OMIT_BUILTIN_TEST +SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*); +#endif + +SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int); +SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*); +SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64); +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64); +SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*); + +SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int); + +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) +SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*); +#else +# define sqlite3ViewGetColumnNames(A,B) 0 +#endif + +#if SQLITE_MAX_ATTACHED>30 +SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask); +#endif +SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); +#ifndef SQLITE_OMIT_AUTOINCREMENT +SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); +SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); +#else +# define sqlite3AutoincrementBegin(X) +# define sqlite3AutoincrementEnd(X) +#endif +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int); +SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*); +SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*); +SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*); +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int); +SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*); +SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, + Token*, Select*, Expr*, IdList*); +SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *); +SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*); +SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *); +SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*); +SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*); +SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*); +SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*); +SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**); +SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, + Expr*, int, int); +SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); +SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); +SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, + Expr*,ExprList*,u32,Expr*,Expr*); +SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); +SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int); +SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) +SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*); +#endif +SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); +SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); +SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); +SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*); +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*); +#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */ +#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */ +#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */ +SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int); +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnToReg(Parse*, Table*, int, int, int); +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); +SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int); +SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int); +SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*); +SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int); +SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); +SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); +SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int); +SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int); +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int); +SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8); +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); +SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int); +SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8); +#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ +#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ +#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */ +SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); +SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); +SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); +SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); +SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *); +SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*); +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); +SQLITE_PRIVATE void sqlite3Vacuum(Parse*); +SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*); +SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*); +SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int); +SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int); +SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int); +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*); +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*); +#ifndef SQLITE_OMIT_BUILTIN_TEST +SQLITE_PRIVATE void sqlite3PrngSaveState(void); +SQLITE_PRIVATE void sqlite3PrngRestoreState(void); +#endif +SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int); +SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int); +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb); +SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int); +SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*); +SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*); +SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*); +SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); +SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); +SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); +#ifdef SQLITE_ENABLE_CURSOR_HINTS +SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*); +#endif +SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); +SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); +SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); +SQLITE_PRIVATE int sqlite3IsRowid(const char*); +SQLITE_PRIVATE void sqlite3GenerateRowDelete( + Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int); +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); +SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int); +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int); +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, + u8,u8,int,int*,int*); +SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int); +SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*); +SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); +SQLITE_PRIVATE void sqlite3MultiWrite(Parse*); +SQLITE_PRIVATE void sqlite3MayAbort(Parse*); +SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8); +SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*); +SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*); +SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int); +SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); +SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); +SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*); +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int); +#if SELECTTRACE_ENABLED +SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*); +#else +# define sqlite3SelectSetName(A,B) +#endif +SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int); +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void); +SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); +SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); +SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*); +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*); +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int); + +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) +SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int); +#endif + +#ifndef SQLITE_OMIT_TRIGGER +SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*, + Expr*,int, int); +SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*); +SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int); +SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*); +SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask); +SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *); +SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *, + int, int, int); +SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int); + void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); +SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*); +SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); +SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*, + Select*,u8); +SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8); +SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); +SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*); +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); +SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int); +# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p)) +# define sqlite3IsToplevel(p) ((p)->pToplevel==0) +#else +# define sqlite3TriggersExist(B,C,D,E,F) 0 +# define sqlite3DeleteTrigger(A,B) +# define sqlite3DropTriggerPtr(A,B) +# define sqlite3UnlinkAndDeleteTrigger(A,B,C) +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I) +# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F) +# define sqlite3TriggerList(X, Y) 0 +# define sqlite3ParseToplevel(p) p +# define sqlite3IsToplevel(p) 1 +# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0 +#endif + +SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*); +SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int); +SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int); +#ifndef SQLITE_OMIT_AUTHORIZATION +SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*); +SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*); +SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*); +SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*); +SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int); +#else +# define sqlite3AuthRead(a,b,c,d) +# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK +# define sqlite3AuthContextPush(a,b,c) +# define sqlite3AuthContextPop(a) ((void)(a)) +#endif +SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*); +SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*); +SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); +SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); +SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); +SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); +SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*); +SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8); +SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*); +SQLITE_PRIVATE int sqlite3Atoi(const char*); +SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar); +SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte); +SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**); +SQLITE_PRIVATE LogEst sqlite3LogEst(u64); +SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst); +#ifndef SQLITE_OMIT_VIRTUALTABLE +SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double); +#endif +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ + defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ + defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) +SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst); +#endif + +/* +** Routines to read and write variable-length integers. These used to +** be defined locally, but now we use the varint routines in the util.c +** file. +*/ +SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64); +SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *); +SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *); +SQLITE_PRIVATE int sqlite3VarintLen(u64 v); + +/* +** The common case is for a varint to be a single byte. They following +** macros handle the common case without a procedure call, but then call +** the procedure for larger varints. +*/ +#define getVarint32(A,B) \ + (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B))) +#define putVarint32(A,B) \ + (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\ + sqlite3PutVarint((A),(B))) +#define getVarint sqlite3GetVarint +#define putVarint sqlite3PutVarint + + +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*); +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int); +SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2); +SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); +SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr); +SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8); +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*); +SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...); +SQLITE_PRIVATE void sqlite3Error(sqlite3*,int); +SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int); +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n); +SQLITE_PRIVATE u8 sqlite3HexToInt(int h); +SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); + +#if defined(SQLITE_NEED_ERR_NAME) +SQLITE_PRIVATE const char *sqlite3ErrName(int); +#endif + +SQLITE_PRIVATE const char *sqlite3ErrStr(int); +SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); +SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); +SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); +SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); +SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); +SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); +SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64); +SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64); +SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64); +SQLITE_PRIVATE int sqlite3AbsInt32(int); +#ifdef SQLITE_ENABLE_8_3_NAMES +SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*); +#else +# define sqlite3FileSuffix3(X,Y) +#endif +SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8); + +SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8); +SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8); +SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, + void(*)(void*)); +SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*); +SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); +SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); +SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); +SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); +SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); +#ifndef SQLITE_AMALGAMATION +SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[]; +SQLITE_PRIVATE const char sqlite3StrBINARY[]; +SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[]; +SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[]; +SQLITE_PRIVATE const Token sqlite3IntTokens[]; +SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config; +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions; +#ifndef SQLITE_OMIT_WSD +SQLITE_PRIVATE int sqlite3PendingByte; +#endif +#endif +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int); +SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*); +SQLITE_PRIVATE void sqlite3AlterFunctions(void); +SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*); +SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *); +SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); +SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*); +SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int); +SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); +SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*); +SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*); +SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*); +SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*); +SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); +SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*); +SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int); +SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *); +SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *); +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); +SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*); +SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); +SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); +SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); +SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); +SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); +SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int); +SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*); +SQLITE_PRIVATE void sqlite3SchemaClear(void *); +SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *); +SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *); +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int); +SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*); +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*); +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*); +#endif +SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, + void (*)(sqlite3_context*,int,sqlite3_value **), + void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), + FuncDestructor *pDestructor +); +SQLITE_PRIVATE void sqlite3OomFault(sqlite3*); +SQLITE_PRIVATE void sqlite3OomClear(sqlite3*); +SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); +SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); + +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*); +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char); +SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); +SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); +SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); + +SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *); +SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *); + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void); +SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*); +SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**); +SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*); +SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**); +#endif + +/* +** The interface to the LEMON-generated parser +*/ +SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64)); +SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*)); +SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*); +#ifdef YYTRACKMAXSTACKDEPTH +SQLITE_PRIVATE int sqlite3ParserStackPeak(void*); +#endif + +SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*); +#ifndef SQLITE_OMIT_LOAD_EXTENSION +SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*); +#else +# define sqlite3CloseExtensions(X) +#endif + +#ifndef SQLITE_OMIT_SHARED_CACHE +SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *); +#else + #define sqlite3TableLock(v,w,x,y,z) +#endif + +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*); +#endif + +#ifdef SQLITE_OMIT_VIRTUALTABLE +# define sqlite3VtabClear(Y) +# define sqlite3VtabSync(X,Y) SQLITE_OK +# define sqlite3VtabRollback(X) +# define sqlite3VtabCommit(X) +# define sqlite3VtabInSync(db) 0 +# define sqlite3VtabLock(X) +# define sqlite3VtabUnlock(X) +# define sqlite3VtabUnlockList(X) +# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK +# define sqlite3GetVTable(X,Y) ((VTable*)0) +#else +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*); +SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p); +SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*); +SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db); +SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db); +SQLITE_PRIVATE void sqlite3VtabLock(VTable *); +SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *); +SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*); +SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int); +SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*); +SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*); +# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) +#endif +SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); +SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*); +SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); +SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int); +SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*); +SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*); +SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*); +SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **); +SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); +SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); +SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); +SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); +SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); +SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); +SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); +SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); +SQLITE_PRIVATE void sqlite3ParserReset(Parse*); +SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); +SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); +SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); +SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*); +SQLITE_PRIVATE const char *sqlite3JournalModename(int); +#ifndef SQLITE_OMIT_WAL +SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*); +SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int); +#endif +#ifndef SQLITE_OMIT_CTE +SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*); +SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*); +SQLITE_PRIVATE void sqlite3WithPush(Parse*, With*, u8); +#else +#define sqlite3WithPush(x,y,z) +#define sqlite3WithDelete(x,y) +#endif + +/* Declarations for functions in fkey.c. All of these are replaced by +** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign +** key functionality is available. If OMIT_TRIGGER is defined but +** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In +** this case foreign keys are parsed, but no other functionality is +** provided (enforcement of FK constraints requires the triggers sub-system). +*/ +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) +SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int); +SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*); +SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int); +SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int); +SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*); +SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *); +#else + #define sqlite3FkActions(a,b,c,d,e,f) + #define sqlite3FkCheck(a,b,c,d,e,f) + #define sqlite3FkDropTable(a,b,c) + #define sqlite3FkOldmask(a,b) 0 + #define sqlite3FkRequired(a,b,c,d) 0 +#endif +#ifndef SQLITE_OMIT_FOREIGN_KEY +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*); +SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**); +#else + #define sqlite3FkDelete(a,b) + #define sqlite3FkLocateIndex(a,b,c,d,e) +#endif + + +/* +** Available fault injectors. Should be numbered beginning with 0. +*/ +#define SQLITE_FAULTINJECTOR_MALLOC 0 +#define SQLITE_FAULTINJECTOR_COUNT 1 + +/* +** The interface to the code in fault.c used for identifying "benign" +** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST +** is not defined. +*/ +#ifndef SQLITE_OMIT_BUILTIN_TEST +SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void); +SQLITE_PRIVATE void sqlite3EndBenignMalloc(void); +#else + #define sqlite3BeginBenignMalloc() + #define sqlite3EndBenignMalloc() +#endif + +/* +** Allowed return values from sqlite3FindInIndex() +*/ +#define IN_INDEX_ROWID 1 /* Search the rowid of the table */ +#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */ +#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */ +#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */ +#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */ +/* +** Allowed flags for the 3rd parameter to sqlite3FindInIndex(). +*/ +#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */ +#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */ +#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */ +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*); + +SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int); +SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *); +#ifdef SQLITE_ENABLE_ATOMIC_WRITE +SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *); +#endif + +SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p); +SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *); + +SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p); +#if SQLITE_MAX_EXPR_DEPTH>0 +SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *); +SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int); +#else + #define sqlite3SelectExprHeight(x) 0 + #define sqlite3ExprCheckHeight(x,y) +#endif + +SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*); +SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32); + +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY +SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *); +SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db); +SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db); +#else + #define sqlite3ConnectionBlocked(x,y) + #define sqlite3ConnectionUnlocked(x) + #define sqlite3ConnectionClosed(x) +#endif + +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *); +#endif + +/* +** If the SQLITE_ENABLE IOTRACE exists then the global variable +** sqlite3IoTrace is a pointer to a printf-like routine used to +** print I/O tracing messages. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; } +SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*); +SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...); +#else +# define IOTRACE(A) +# define sqlite3VdbeIOTraceSql(X) +#endif + +/* +** These routines are available for the mem2.c debugging memory allocator +** only. They are used to verify that different "types" of memory +** allocations are properly tracked by the system. +** +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of +** the MEMTYPE_* macros defined below. The type must be a bitmask with +** a single bit set. +** +** sqlite3MemdebugHasType() returns true if any of the bits in its second +** argument match the type set by the previous sqlite3MemdebugSetType(). +** sqlite3MemdebugHasType() is intended for use inside assert() statements. +** +** sqlite3MemdebugNoType() returns true if none of the bits in its second +** argument match the type set by the previous sqlite3MemdebugSetType(). +** +** Perhaps the most important point is the difference between MEMTYPE_HEAP +** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means +** it might have been allocated by lookaside, except the allocation was +** too large or lookaside was already full. It is important to verify +** that allocations that might have been satisfied by lookaside are not +** passed back to non-lookaside free() routines. Asserts such as the +** example above are placed on the non-lookaside free() routines to verify +** this constraint. +** +** All of this is no-op for a production build. It only comes into +** play when the SQLITE_MEMDEBUG compile-time option is used. +*/ +#ifdef SQLITE_MEMDEBUG +SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8); +SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8); +SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8); +#else +# define sqlite3MemdebugSetType(X,Y) /* no-op */ +# define sqlite3MemdebugHasType(X,Y) 1 +# define sqlite3MemdebugNoType(X,Y) 1 +#endif +#define MEMTYPE_HEAP 0x01 /* General heap allocations */ +#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */ +#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ +#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ + +/* +** Threading interface +*/ +#if SQLITE_MAX_WORKER_THREADS>0 +SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*); +SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**); +#endif + +#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST) +SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*); +#endif + +#endif /* _SQLITEINT_H_ */ + +/************** End of sqliteInt.h *******************************************/ +/************** Begin file global.c ******************************************/ +/* +** 2008 June 13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains definitions of global variables and constants. +*/ +/* #include "sqliteInt.h" */ + +/* An array to map all upper-case characters into their corresponding +** lower-case character. +** +** SQLite only considers US-ASCII (or EBCDIC) characters. We do not +** handle case conversions for the UTF character set since the tables +** involved are nearly as big or bigger than SQLite itself. +*/ +SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = { +#ifdef SQLITE_ASCII + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, + 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, + 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, + 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, + 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, + 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, + 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, + 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, + 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, + 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, + 252,253,254,255 +#endif +#ifdef SQLITE_EBCDIC + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */ + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */ + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */ + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */ + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */ + 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */ + 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */ + 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */ + 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */ + 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */ + 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */ + 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */ + 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */ + 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */ + 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */ +#endif +}; + +/* +** The following 256 byte lookup table is used to support SQLites built-in +** equivalents to the following standard library functions: +** +** isspace() 0x01 +** isalpha() 0x02 +** isdigit() 0x04 +** isalnum() 0x06 +** isxdigit() 0x08 +** toupper() 0x20 +** SQLite identifier character 0x40 +** Quote character 0x80 +** +** Bit 0x20 is set if the mapped character requires translation to upper +** case. i.e. if the character is a lower-case ASCII character. +** If x is a lower-case ASCII character, then its upper-case equivalent +** is (x - 0x20). Therefore toupper() can be implemented as: +** +** (x & ~(map[x]&0x20)) +** +** Standard function tolower() is implemented using the sqlite3UpperToLower[] +** array. tolower() is used more often than toupper() by SQLite. +** +** Bit 0x40 is set if the character non-alphanumeric and can be used in an +** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any +** non-ASCII UTF character. Hence the test for whether or not a character is +** part of an identifier is 0x46. +** +** SQLite's versions are identical to the standard versions assuming a +** locale of "C". They are implemented as macros in sqliteInt.h. +*/ +#ifdef SQLITE_ASCII +SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */ + 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */ + 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */ + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */ + 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */ + + 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */ + 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ + 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */ + 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */ + + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */ + + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */ +}; +#endif + +/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards +** compatibility for legacy applications, the URI filename capability is +** disabled by default. +** +** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled +** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. +** +** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally +** disabled. The default value may be changed by compiling with the +** SQLITE_USE_URI symbol defined. +*/ +#ifndef SQLITE_USE_URI +# define SQLITE_USE_URI 0 +#endif + +/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the +** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if +** that compile-time option is omitted. +*/ +#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN +# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 +#endif + +/* The minimum PMA size is set to this value multiplied by the database +** page size in bytes. +*/ +#ifndef SQLITE_SORTER_PMASZ +# define SQLITE_SORTER_PMASZ 250 +#endif + +/* Statement journals spill to disk when their size exceeds the following +** threashold (in bytes). 0 means that statement journals are created and +** written to disk immediately (the default behavior for SQLite versions +** before 3.12.0). -1 means always keep the entire statement journal in +** memory. (The statement journal is also always held entirely in memory +** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this +** setting.) +*/ +#ifndef SQLITE_STMTJRNL_SPILL +# define SQLITE_STMTJRNL_SPILL (64*1024) +#endif + +/* +** The following singleton contains the global configuration for +** the SQLite library. +*/ +SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { + SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */ + 1, /* bCoreMutex */ + SQLITE_THREADSAFE==1, /* bFullMutex */ + SQLITE_USE_URI, /* bOpenUri */ + SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ + 0x7ffffffe, /* mxStrlen */ + 0, /* neverCorrupt */ + 128, /* szLookaside */ + 500, /* nLookaside */ + SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ + {0,0,0,0,0,0,0,0}, /* m */ + {0,0,0,0,0,0,0,0,0}, /* mutex */ + {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */ + (void*)0, /* pHeap */ + 0, /* nHeap */ + 0, 0, /* mnHeap, mxHeap */ + SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */ + SQLITE_MAX_MMAP_SIZE, /* mxMmap */ + (void*)0, /* pScratch */ + 0, /* szScratch */ + 0, /* nScratch */ + (void*)0, /* pPage */ + 0, /* szPage */ + SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */ + 0, /* mxParserStack */ + 0, /* sharedCacheEnabled */ + SQLITE_SORTER_PMASZ, /* szPma */ + /* All the rest should always be initialized to zero */ + 0, /* isInit */ + 0, /* inProgress */ + 0, /* isMutexInit */ + 0, /* isMallocInit */ + 0, /* isPCacheInit */ + 0, /* nRefInitMutex */ + 0, /* pInitMutex */ + 0, /* xLog */ + 0, /* pLogArg */ +#ifdef SQLITE_ENABLE_SQLLOG + 0, /* xSqllog */ + 0, /* pSqllogArg */ +#endif +#ifdef SQLITE_VDBE_COVERAGE + 0, /* xVdbeBranch */ + 0, /* pVbeBranchArg */ +#endif +#ifndef SQLITE_OMIT_BUILTIN_TEST + 0, /* xTestCallback */ +#endif + 0 /* bLocaltimeFault */ +}; + +/* +** Hash table for global functions - functions common to all +** database connections. After initialization, this table is +** read-only. +*/ +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions; + +/* +** Constant tokens for values 0 and 1. +*/ +SQLITE_PRIVATE const Token sqlite3IntTokens[] = { + { "0", 1 }, + { "1", 1 } +}; + + +/* +** The value of the "pending" byte must be 0x40000000 (1 byte past the +** 1-gibabyte boundary) in a compatible database. SQLite never uses +** the database page that contains the pending byte. It never attempts +** to read or write that page. The pending byte page is set assign +** for use by the VFS layers as space for managing file locks. +** +** During testing, it is often desirable to move the pending byte to +** a different position in the file. This allows code that has to +** deal with the pending byte to run on files that are much smaller +** than 1 GiB. The sqlite3_test_control() interface can be used to +** move the pending byte. +** +** IMPORTANT: Changing the pending byte to any value other than +** 0x40000000 results in an incompatible database file format! +** Changing the pending byte during operation will result in undefined +** and incorrect behavior. +*/ +#ifndef SQLITE_OMIT_WSD +SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; +#endif + +/* #include "opcodes.h" */ +/* +** Properties of opcodes. The OPFLG_INITIALIZER macro is +** created by mkopcodeh.awk during compilation. Data is obtained +** from the comments following the "case OP_xxxx:" statements in +** the vdbe.c file. +*/ +SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER; + +/* +** Name of the default collating sequence +*/ +SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY"; + +/************** End of global.c **********************************************/ +/************** Begin file ctime.c *******************************************/ +/* +** 2010 February 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements routines used to report what compile-time options +** SQLite was built with. +*/ + +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + +/* #include "sqliteInt.h" */ + +/* +** An array of names of all compile-time options. This array should +** be sorted A-Z. +** +** This array looks large, but in a typical installation actually uses +** only a handful of compile-time options, so most times this array is usually +** rather short and uses little memory space. +*/ +static const char * const azCompileOpt[] = { + +/* These macros are provided to "stringify" the value of the define +** for those options in which the value is meaningful. */ +#define CTIMEOPT_VAL_(opt) #opt +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) + +#if SQLITE_32BIT_ROWID + "32BIT_ROWID", +#endif +#if SQLITE_4_BYTE_ALIGNED_MALLOC + "4_BYTE_ALIGNED_MALLOC", +#endif +#if SQLITE_CASE_SENSITIVE_LIKE + "CASE_SENSITIVE_LIKE", +#endif +#if SQLITE_CHECK_PAGES + "CHECK_PAGES", +#endif +#if SQLITE_COVERAGE_TEST + "COVERAGE_TEST", +#endif +#if SQLITE_DEBUG + "DEBUG", +#endif +#if SQLITE_DEFAULT_LOCKING_MODE + "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE), +#endif +#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc) + "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE), +#endif +#if SQLITE_DISABLE_DIRSYNC + "DISABLE_DIRSYNC", +#endif +#if SQLITE_DISABLE_LFS + "DISABLE_LFS", +#endif +#if SQLITE_ENABLE_8_3_NAMES + "ENABLE_8_3_NAMES", +#endif +#if SQLITE_ENABLE_API_ARMOR + "ENABLE_API_ARMOR", +#endif +#if SQLITE_ENABLE_ATOMIC_WRITE + "ENABLE_ATOMIC_WRITE", +#endif +#if SQLITE_ENABLE_CEROD + "ENABLE_CEROD", +#endif +#if SQLITE_ENABLE_COLUMN_METADATA + "ENABLE_COLUMN_METADATA", +#endif +#if SQLITE_ENABLE_DBSTAT_VTAB + "ENABLE_DBSTAT_VTAB", +#endif +#if SQLITE_ENABLE_EXPENSIVE_ASSERT + "ENABLE_EXPENSIVE_ASSERT", +#endif +#if SQLITE_ENABLE_FTS1 + "ENABLE_FTS1", +#endif +#if SQLITE_ENABLE_FTS2 + "ENABLE_FTS2", +#endif +#if SQLITE_ENABLE_FTS3 + "ENABLE_FTS3", +#endif +#if SQLITE_ENABLE_FTS3_PARENTHESIS + "ENABLE_FTS3_PARENTHESIS", +#endif +#if SQLITE_ENABLE_FTS4 + "ENABLE_FTS4", +#endif +#if SQLITE_ENABLE_FTS5 + "ENABLE_FTS5", +#endif +#if SQLITE_ENABLE_ICU + "ENABLE_ICU", +#endif +#if SQLITE_ENABLE_IOTRACE + "ENABLE_IOTRACE", +#endif +#if SQLITE_ENABLE_JSON1 + "ENABLE_JSON1", +#endif +#if SQLITE_ENABLE_LOAD_EXTENSION + "ENABLE_LOAD_EXTENSION", +#endif +#if SQLITE_ENABLE_LOCKING_STYLE + "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE), +#endif +#if SQLITE_ENABLE_MEMORY_MANAGEMENT + "ENABLE_MEMORY_MANAGEMENT", +#endif +#if SQLITE_ENABLE_MEMSYS3 + "ENABLE_MEMSYS3", +#endif +#if SQLITE_ENABLE_MEMSYS5 + "ENABLE_MEMSYS5", +#endif +#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK + "ENABLE_OVERSIZE_CELL_CHECK", +#endif +#if SQLITE_ENABLE_RTREE + "ENABLE_RTREE", +#endif +#if defined(SQLITE_ENABLE_STAT4) + "ENABLE_STAT4", +#elif defined(SQLITE_ENABLE_STAT3) + "ENABLE_STAT3", +#endif +#if SQLITE_ENABLE_UNLOCK_NOTIFY + "ENABLE_UNLOCK_NOTIFY", +#endif +#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT + "ENABLE_UPDATE_DELETE_LIMIT", +#endif +#if SQLITE_HAS_CODEC + "HAS_CODEC", +#endif +#if HAVE_ISNAN || SQLITE_HAVE_ISNAN + "HAVE_ISNAN", +#endif +#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX + "HOMEGROWN_RECURSIVE_MUTEX", +#endif +#if SQLITE_IGNORE_AFP_LOCK_ERRORS + "IGNORE_AFP_LOCK_ERRORS", +#endif +#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS + "IGNORE_FLOCK_LOCK_ERRORS", +#endif +#ifdef SQLITE_INT64_TYPE + "INT64_TYPE", +#endif +#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS + "LIKE_DOESNT_MATCH_BLOBS", +#endif +#if SQLITE_LOCK_TRACE + "LOCK_TRACE", +#endif +#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc) + "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE), +#endif +#ifdef SQLITE_MAX_SCHEMA_RETRY + "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY), +#endif +#if SQLITE_MEMDEBUG + "MEMDEBUG", +#endif +#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT + "MIXED_ENDIAN_64BIT_FLOAT", +#endif +#if SQLITE_NO_SYNC + "NO_SYNC", +#endif +#if SQLITE_OMIT_ALTERTABLE + "OMIT_ALTERTABLE", +#endif +#if SQLITE_OMIT_ANALYZE + "OMIT_ANALYZE", +#endif +#if SQLITE_OMIT_ATTACH + "OMIT_ATTACH", +#endif +#if SQLITE_OMIT_AUTHORIZATION + "OMIT_AUTHORIZATION", +#endif +#if SQLITE_OMIT_AUTOINCREMENT + "OMIT_AUTOINCREMENT", +#endif +#if SQLITE_OMIT_AUTOINIT + "OMIT_AUTOINIT", +#endif +#if SQLITE_OMIT_AUTOMATIC_INDEX + "OMIT_AUTOMATIC_INDEX", +#endif +#if SQLITE_OMIT_AUTORESET + "OMIT_AUTORESET", +#endif +#if SQLITE_OMIT_AUTOVACUUM + "OMIT_AUTOVACUUM", +#endif +#if SQLITE_OMIT_BETWEEN_OPTIMIZATION + "OMIT_BETWEEN_OPTIMIZATION", +#endif +#if SQLITE_OMIT_BLOB_LITERAL + "OMIT_BLOB_LITERAL", +#endif +#if SQLITE_OMIT_BTREECOUNT + "OMIT_BTREECOUNT", +#endif +#if SQLITE_OMIT_BUILTIN_TEST + "OMIT_BUILTIN_TEST", +#endif +#if SQLITE_OMIT_CAST + "OMIT_CAST", +#endif +#if SQLITE_OMIT_CHECK + "OMIT_CHECK", +#endif +#if SQLITE_OMIT_COMPLETE + "OMIT_COMPLETE", +#endif +#if SQLITE_OMIT_COMPOUND_SELECT + "OMIT_COMPOUND_SELECT", +#endif +#if SQLITE_OMIT_CTE + "OMIT_CTE", +#endif +#if SQLITE_OMIT_DATETIME_FUNCS + "OMIT_DATETIME_FUNCS", +#endif +#if SQLITE_OMIT_DECLTYPE + "OMIT_DECLTYPE", +#endif +#if SQLITE_OMIT_DEPRECATED + "OMIT_DEPRECATED", +#endif +#if SQLITE_OMIT_DISKIO + "OMIT_DISKIO", +#endif +#if SQLITE_OMIT_EXPLAIN + "OMIT_EXPLAIN", +#endif +#if SQLITE_OMIT_FLAG_PRAGMAS + "OMIT_FLAG_PRAGMAS", +#endif +#if SQLITE_OMIT_FLOATING_POINT + "OMIT_FLOATING_POINT", +#endif +#if SQLITE_OMIT_FOREIGN_KEY + "OMIT_FOREIGN_KEY", +#endif +#if SQLITE_OMIT_GET_TABLE + "OMIT_GET_TABLE", +#endif +#if SQLITE_OMIT_INCRBLOB + "OMIT_INCRBLOB", +#endif +#if SQLITE_OMIT_INTEGRITY_CHECK + "OMIT_INTEGRITY_CHECK", +#endif +#if SQLITE_OMIT_LIKE_OPTIMIZATION + "OMIT_LIKE_OPTIMIZATION", +#endif +#if SQLITE_OMIT_LOAD_EXTENSION + "OMIT_LOAD_EXTENSION", +#endif +#if SQLITE_OMIT_LOCALTIME + "OMIT_LOCALTIME", +#endif +#if SQLITE_OMIT_LOOKASIDE + "OMIT_LOOKASIDE", +#endif +#if SQLITE_OMIT_MEMORYDB + "OMIT_MEMORYDB", +#endif +#if SQLITE_OMIT_OR_OPTIMIZATION + "OMIT_OR_OPTIMIZATION", +#endif +#if SQLITE_OMIT_PAGER_PRAGMAS + "OMIT_PAGER_PRAGMAS", +#endif +#if SQLITE_OMIT_PRAGMA + "OMIT_PRAGMA", +#endif +#if SQLITE_OMIT_PROGRESS_CALLBACK + "OMIT_PROGRESS_CALLBACK", +#endif +#if SQLITE_OMIT_QUICKBALANCE + "OMIT_QUICKBALANCE", +#endif +#if SQLITE_OMIT_REINDEX + "OMIT_REINDEX", +#endif +#if SQLITE_OMIT_SCHEMA_PRAGMAS + "OMIT_SCHEMA_PRAGMAS", +#endif +#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS + "OMIT_SCHEMA_VERSION_PRAGMAS", +#endif +#if SQLITE_OMIT_SHARED_CACHE + "OMIT_SHARED_CACHE", +#endif +#if SQLITE_OMIT_SUBQUERY + "OMIT_SUBQUERY", +#endif +#if SQLITE_OMIT_TCL_VARIABLE + "OMIT_TCL_VARIABLE", +#endif +#if SQLITE_OMIT_TEMPDB + "OMIT_TEMPDB", +#endif +#if SQLITE_OMIT_TRACE + "OMIT_TRACE", +#endif +#if SQLITE_OMIT_TRIGGER + "OMIT_TRIGGER", +#endif +#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION + "OMIT_TRUNCATE_OPTIMIZATION", +#endif +#if SQLITE_OMIT_UTF16 + "OMIT_UTF16", +#endif +#if SQLITE_OMIT_VACUUM + "OMIT_VACUUM", +#endif +#if SQLITE_OMIT_VIEW + "OMIT_VIEW", +#endif +#if SQLITE_OMIT_VIRTUALTABLE + "OMIT_VIRTUALTABLE", +#endif +#if SQLITE_OMIT_WAL + "OMIT_WAL", +#endif +#if SQLITE_OMIT_WSD + "OMIT_WSD", +#endif +#if SQLITE_OMIT_XFER_OPT + "OMIT_XFER_OPT", +#endif +#if SQLITE_PERFORMANCE_TRACE + "PERFORMANCE_TRACE", +#endif +#if SQLITE_PROXY_DEBUG + "PROXY_DEBUG", +#endif +#if SQLITE_RTREE_INT_ONLY + "RTREE_INT_ONLY", +#endif +#if SQLITE_SECURE_DELETE + "SECURE_DELETE", +#endif +#if SQLITE_SMALL_STACK + "SMALL_STACK", +#endif +#if SQLITE_SOUNDEX + "SOUNDEX", +#endif +#if SQLITE_SYSTEM_MALLOC + "SYSTEM_MALLOC", +#endif +#if SQLITE_TCL + "TCL", +#endif +#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc) + "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE), +#endif +#if SQLITE_TEST + "TEST", +#endif +#if defined(SQLITE_THREADSAFE) + "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE), +#endif +#if SQLITE_USE_ALLOCA + "USE_ALLOCA", +#endif +#if SQLITE_USER_AUTHENTICATION + "USER_AUTHENTICATION", +#endif +#if SQLITE_WIN32_MALLOC + "WIN32_MALLOC", +#endif +#if SQLITE_ZERO_MALLOC + "ZERO_MALLOC" +#endif +}; + +/* +** Given the name of a compile-time option, return true if that option +** was used and false if not. +** +** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix +** is not required for a match. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName){ + int i, n; + +#if SQLITE_ENABLE_API_ARMOR + if( zOptName==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; + n = sqlite3Strlen30(zOptName); + + /* Since ArraySize(azCompileOpt) is normally in single digits, a + ** linear search is adequate. No need for a binary search. */ + for(i=0; i=0 && NaDb[] (or -1) */ + u8 nullRow; /* True if pointing to a row with no data */ + u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ + u8 isTable; /* True for rowid tables. False for indexes */ +#ifdef SQLITE_DEBUG + u8 seekOp; /* Most recent seek operation on this cursor */ + u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */ +#endif + Bool isEphemeral:1; /* True for an ephemeral table */ + Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */ + Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */ + Pgno pgnoRoot; /* Root page of the open btree cursor */ + i16 nField; /* Number of fields in the header */ + u16 nHdrParsed; /* Number of header fields parsed so far */ + union { + BtCursor *pCursor; /* CURTYPE_BTREE. Btree cursor */ + sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */ + int pseudoTableReg; /* CURTYPE_PSEUDO. Reg holding content. */ + VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */ + } uc; + Btree *pBt; /* Separate file holding temporary table */ + KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ + int seekResult; /* Result of previous sqlite3BtreeMoveto() */ + i64 seqCount; /* Sequence counter */ + i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ + VdbeCursor *pAltCursor; /* Associated index cursor from which to read */ + int *aAltMap; /* Mapping from table to index column numbers */ +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK + u64 maskUsed; /* Mask of columns used by this cursor */ +#endif + + /* Cached information about the header for the data record that the + ** cursor is currently pointing to. Only valid if cacheStatus matches + ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of + ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that + ** the cache is out of date. + ** + ** aRow might point to (ephemeral) data for the current row, or it might + ** be NULL. + */ + u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ + u32 payloadSize; /* Total number of bytes in the record */ + u32 szRow; /* Byte available in aRow */ + u32 iHdrOffset; /* Offset to next unparsed byte of the header */ + const u8 *aRow; /* Data for the current row, if all on one page */ + u32 *aOffset; /* Pointer to aType[nField] */ + u32 aType[1]; /* Type values for all entries in the record */ + /* 2*nField extra array elements allocated for aType[], beyond the one + ** static element declared in the structure. nField total array slots for + ** aType[] and nField+1 array slots for aOffset[] */ +}; + +/* +** When a sub-program is executed (OP_Program), a structure of this type +** is allocated to store the current value of the program counter, as +** well as the current memory cell array and various other frame specific +** values stored in the Vdbe struct. When the sub-program is finished, +** these values are copied back to the Vdbe from the VdbeFrame structure, +** restoring the state of the VM to as it was before the sub-program +** began executing. +** +** The memory for a VdbeFrame object is allocated and managed by a memory +** cell in the parent (calling) frame. When the memory cell is deleted or +** overwritten, the VdbeFrame object is not freed immediately. Instead, it +** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame +** list is deleted when the VM is reset in VdbeHalt(). The reason for doing +** this instead of deleting the VdbeFrame immediately is to avoid recursive +** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the +** child frame are released. +** +** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is +** set to NULL if the currently executing frame is the main program. +*/ +typedef struct VdbeFrame VdbeFrame; +struct VdbeFrame { + Vdbe *v; /* VM this frame belongs to */ + VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ + Op *aOp; /* Program instructions for parent frame */ + i64 *anExec; /* Event counters from parent frame */ + Mem *aMem; /* Array of memory cells for parent frame */ + u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ + VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ + void *token; /* Copy of SubProgram.token */ + i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ + AuxData *pAuxData; /* Linked list of auxdata allocations */ + int nCursor; /* Number of entries in apCsr */ + int pc; /* Program Counter in parent (calling) frame */ + int nOp; /* Size of aOp array */ + int nMem; /* Number of entries in aMem */ + int nOnceFlag; /* Number of entries in aOnceFlag */ + int nChildMem; /* Number of memory cells for child frame */ + int nChildCsr; /* Number of cursors for child frame */ + int nChange; /* Statement changes (Vdbe.nChange) */ + int nDbChange; /* Value of db->nChange */ +}; + +#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) + +/* +** A value for VdbeCursor.cacheValid that means the cache is always invalid. +*/ +#define CACHE_STALE 0 + +/* +** Internally, the vdbe manipulates nearly all SQL values as Mem +** structures. Each Mem struct may cache multiple representations (string, +** integer etc.) of the same value. +*/ +struct Mem { + union MemValue { + double r; /* Real value used when MEM_Real is set in flags */ + i64 i; /* Integer value used when MEM_Int is set in flags */ + int nZero; /* Used when bit MEM_Zero is set in flags */ + FuncDef *pDef; /* Used only when flags==MEM_Agg */ + RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ + VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ + } u; + u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ + u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ + u8 eSubtype; /* Subtype for this value */ + int n; /* Number of characters in string value, excluding '\0' */ + char *z; /* String or BLOB value */ + /* ShallowCopy only needs to copy the information above */ + char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ + int szMalloc; /* Size of the zMalloc allocation */ + u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */ + sqlite3 *db; /* The associated database connection */ + void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ +#ifdef SQLITE_DEBUG + Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ + void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ +#endif +}; + +/* +** Size of struct Mem not including the Mem.zMalloc member or anything that +** follows. +*/ +#define MEMCELLSIZE offsetof(Mem,zMalloc) + +/* One or more of the following flags are set to indicate the validOK +** representations of the value stored in the Mem struct. +** +** If the MEM_Null flag is set, then the value is an SQL NULL value. +** No other flags may be set in this case. +** +** If the MEM_Str flag is set then Mem.z points at a string representation. +** Usually this is encoded in the same unicode encoding as the main +** database (see below for exceptions). If the MEM_Term flag is also +** set, then the string is nul terminated. The MEM_Int and MEM_Real +** flags may coexist with the MEM_Str flag. +*/ +#define MEM_Null 0x0001 /* Value is NULL */ +#define MEM_Str 0x0002 /* Value is a string */ +#define MEM_Int 0x0004 /* Value is an integer */ +#define MEM_Real 0x0008 /* Value is a real number */ +#define MEM_Blob 0x0010 /* Value is a BLOB */ +#define MEM_AffMask 0x001f /* Mask of affinity bits */ +#define MEM_RowSet 0x0020 /* Value is a RowSet object */ +#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ +#define MEM_Undefined 0x0080 /* Value is undefined */ +#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ +#define MEM_TypeMask 0x81ff /* Mask of type bits */ + + +/* Whenever Mem contains a valid string or blob representation, one of +** the following flags must be set to determine the memory management +** policy for Mem.z. The MEM_Term flag tells us whether or not the +** string is \000 or \u0000 terminated +*/ +#define MEM_Term 0x0200 /* String rep is nul terminated */ +#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ +#define MEM_Static 0x0800 /* Mem.z points to a static string */ +#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ +#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ +#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ +#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */ +#ifdef SQLITE_OMIT_INCRBLOB + #undef MEM_Zero + #define MEM_Zero 0x0000 +#endif + +/* Return TRUE if Mem X contains dynamically allocated content - anything +** that needs to be deallocated to avoid a leak. +*/ +#define VdbeMemDynamic(X) \ + (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0) + +/* +** Clear any existing type flags from a Mem and replace them with f +*/ +#define MemSetTypeFlag(p, f) \ + ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) + +/* +** Return true if a memory cell is not marked as invalid. This macro +** is for use inside assert() statements only. +*/ +#ifdef SQLITE_DEBUG +#define memIsValid(M) ((M)->flags & MEM_Undefined)==0 +#endif + +/* +** Each auxiliary data pointer stored by a user defined function +** implementation calling sqlite3_set_auxdata() is stored in an instance +** of this structure. All such structures associated with a single VM +** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed +** when the VM is halted (if not before). +*/ +struct AuxData { + int iOp; /* Instruction number of OP_Function opcode */ + int iArg; /* Index of function argument. */ + void *pAux; /* Aux data pointer */ + void (*xDelete)(void *); /* Destructor for the aux data */ + AuxData *pNext; /* Next element in list */ +}; + +/* +** The "context" argument for an installable function. A pointer to an +** instance of this structure is the first argument to the routines used +** implement the SQL functions. +** +** There is a typedef for this structure in sqlite.h. So all routines, +** even the public interface to SQLite, can use a pointer to this structure. +** But this file is the only place where the internal details of this +** structure are known. +** +** This structure is defined inside of vdbeInt.h because it uses substructures +** (Mem) which are only defined there. +*/ +struct sqlite3_context { + Mem *pOut; /* The return value is stored here */ + FuncDef *pFunc; /* Pointer to function information */ + Mem *pMem; /* Memory cell used to store aggregate context */ + Vdbe *pVdbe; /* The VM that owns this context */ + int iOp; /* Instruction number of OP_Function */ + int isError; /* Error code returned by the function. */ + u8 skipFlag; /* Skip accumulator loading if true */ + u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ + u8 argc; /* Number of arguments */ + sqlite3_value *argv[1]; /* Argument set */ +}; + +/* +** An Explain object accumulates indented output which is helpful +** in describing recursive data structures. +*/ +struct Explain { + Vdbe *pVdbe; /* Attach the explanation to this Vdbe */ + StrAccum str; /* The string being accumulated */ + int nIndent; /* Number of elements in aIndent */ + u16 aIndent[100]; /* Levels of indentation */ + char zBase[100]; /* Initial space */ +}; + +/* A bitfield type for use inside of structures. Always follow with :N where +** N is the number of bits. +*/ +typedef unsigned bft; /* Bit Field Type */ + +typedef struct ScanStatus ScanStatus; +struct ScanStatus { + int addrExplain; /* OP_Explain for loop */ + int addrLoop; /* Address of "loops" counter */ + int addrVisit; /* Address of "rows visited" counter */ + int iSelectID; /* The "Select-ID" for this loop */ + LogEst nEst; /* Estimated output rows per loop */ + char *zName; /* Name of table or index */ +}; + +/* +** An instance of the virtual machine. This structure contains the complete +** state of the virtual machine. +** +** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() +** is really a pointer to an instance of this structure. +*/ +struct Vdbe { + sqlite3 *db; /* The database connection that owns this statement */ + Op *aOp; /* Space to hold the virtual machine's program */ + Mem *aMem; /* The memory locations */ + Mem **apArg; /* Arguments to currently executing user function */ + Mem *aColName; /* Column names to return */ + Mem *pResultSet; /* Pointer to an array of results */ + Parse *pParse; /* Parsing context used to create this Vdbe */ + int nMem; /* Number of memory locations currently allocated */ + int nOp; /* Number of instructions in the program */ + int nCursor; /* Number of slots in apCsr[] */ + u32 magic; /* Magic number for sanity checking */ + char *zErrMsg; /* Error message written here */ + Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ + VdbeCursor **apCsr; /* One element of this array for each open cursor */ + Mem *aVar; /* Values for the OP_Variable opcode. */ + char **azVar; /* Name of variables */ + ynVar nVar; /* Number of entries in aVar[] */ + ynVar nzVar; /* Number of entries in azVar[] */ + u32 cacheCtr; /* VdbeCursor row cache generation counter */ + int pc; /* The program counter */ + int rc; /* Value to return */ +#ifdef SQLITE_DEBUG + int rcApp; /* errcode set by sqlite3_result_error_code() */ +#endif + u16 nResColumn; /* Number of columns in one row of the result set */ + u8 errorAction; /* Recovery action to do in case of an error */ + bft expired:1; /* True if the VM needs to be recompiled */ + bft doingRerun:1; /* True if rerunning after an auto-reprepare */ + u8 minWriteFileFormat; /* Minimum file format for writable database files */ + bft explain:2; /* True if EXPLAIN present on SQL command */ + bft changeCntOn:1; /* True to update the change-counter */ + bft runOnlyOnce:1; /* Automatically expire on reset */ + bft usesStmtJournal:1; /* True if uses a statement journal */ + bft readOnly:1; /* True for statements that do not write */ + bft bIsReader:1; /* True for statements that read */ + bft isPrepareV2:1; /* True if prepared with prepare_v2() */ + int nChange; /* Number of db changes made since last reset */ + yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */ + yDbMask lockMask; /* Subset of btreeMask that requires a lock */ + int iStatement; /* Statement number (or 0 if has not opened stmt) */ + u32 aCounter[5]; /* Counters used by sqlite3_stmt_status() */ +#ifndef SQLITE_OMIT_TRACE + i64 startTime; /* Time when query started - used for profiling */ +#endif + i64 iCurrentTime; /* Value of julianday('now') for this statement */ + i64 nFkConstraint; /* Number of imm. FK constraints this VM */ + i64 nStmtDefCons; /* Number of def. constraints when stmt started */ + i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ + char *zSql; /* Text of the SQL statement that generated this */ + void *pFree; /* Free this when deleting the vdbe */ + VdbeFrame *pFrame; /* Parent frame */ + VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ + int nFrame; /* Number of frames in pFrame list */ + u32 expmask; /* Binding to these vars invalidates VM */ + SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ + int nOnceFlag; /* Size of array aOnceFlag[] */ + u8 *aOnceFlag; /* Flags for OP_Once */ + AuxData *pAuxData; /* Linked list of auxdata allocations */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + i64 *anExec; /* Number of times each op has been executed */ + int nScan; /* Entries in aScan[] */ + ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ +#endif +}; + +/* +** The following are allowed values for Vdbe.magic +*/ +#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ +#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ +#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ +#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ + +/* +** Structure used to store the context required by the +** sqlite3_preupdate_*() API functions. +*/ +struct PreUpdate { + Vdbe *v; + VdbeCursor *pCsr; /* Cursor to read old values from */ + int op; /* One of SQLITE_INSERT, UPDATE, DELETE */ + u8 *aRecord; /* old.* database record */ + KeyInfo keyinfo; + UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */ + UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */ + int iNewReg; /* Register for new.* values */ + i64 iKey1; /* First key value passed to hook */ + i64 iKey2; /* Second key value passed to hook */ + int iPKey; /* If not negative index of IPK column */ + Mem *aNew; /* Array of new.* values */ +}; + +/* +** Function prototypes +*/ +SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...); +SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); +void sqliteVdbePopStack(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*); +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); +#endif +SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); +SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8); +SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*); +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); +SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int); + +int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); +SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); +SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); +SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); +SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); +SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); +SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*); +SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); +SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64); +#ifdef SQLITE_OMIT_FLOATING_POINT +# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64 +#else +SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double); +#endif +SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16); +SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*); +SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int); +SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8); +SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); +SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); +SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); +SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8); +SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*); +SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); +SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); +SQLITE_PRIVATE const char *sqlite3OpcodeName(int); +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); +SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); +SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); +SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int); +#endif +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p); + +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *); +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *); +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *); +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *); +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *); +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *); +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *); + +#if !defined(SQLITE_OMIT_SHARED_CACHE) +SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*); +#else +# define sqlite3VdbeEnter(X) +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*); +#else +# define sqlite3VdbeLeave(X) +#endif + +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*); +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*); +#endif + +#ifndef SQLITE_OMIT_FOREIGN_KEY +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); +#else +# define sqlite3VdbeCheckFk(p,i) 0 +#endif + +SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); +#endif +SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem); + +#ifndef SQLITE_OMIT_INCRBLOB +SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); + #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) +#else + #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK + #define ExpandBlob(P) SQLITE_OK +#endif + +#endif /* !defined(_VDBEINT_H_) */ + +/************** End of vdbeInt.h *********************************************/ +/************** Continuing where we left off in status.c *********************/ + +/* +** Variables in which to record status information. +*/ +#if SQLITE_PTRSIZE>4 +typedef sqlite3_int64 sqlite3StatValueType; +#else +typedef u32 sqlite3StatValueType; +#endif +typedef struct sqlite3StatType sqlite3StatType; +static SQLITE_WSD struct sqlite3StatType { + sqlite3StatValueType nowValue[10]; /* Current value */ + sqlite3StatValueType mxValue[10]; /* Maximum value */ +} sqlite3Stat = { {0,}, {0,} }; + +/* +** Elements of sqlite3Stat[] are protected by either the memory allocator +** mutex, or by the pcache1 mutex. The following array determines which. +*/ +static const char statMutex[] = { + 0, /* SQLITE_STATUS_MEMORY_USED */ + 1, /* SQLITE_STATUS_PAGECACHE_USED */ + 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */ + 0, /* SQLITE_STATUS_SCRATCH_USED */ + 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */ + 0, /* SQLITE_STATUS_MALLOC_SIZE */ + 0, /* SQLITE_STATUS_PARSER_STACK */ + 1, /* SQLITE_STATUS_PAGECACHE_SIZE */ + 0, /* SQLITE_STATUS_SCRATCH_SIZE */ + 0, /* SQLITE_STATUS_MALLOC_COUNT */ +}; + + +/* The "wsdStat" macro will resolve to the status information +** state vector. If writable static data is unsupported on the target, +** we have to locate the state vector at run-time. In the more common +** case where writable static data is supported, wsdStat can refer directly +** to the "sqlite3Stat" state vector declared above. +*/ +#ifdef SQLITE_OMIT_WSD +# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat) +# define wsdStat x[0] +#else +# define wsdStatInit +# define wsdStat sqlite3Stat +#endif + +/* +** Return the current value of a status parameter. The caller must +** be holding the appropriate mutex. +*/ +SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){ + wsdStatInit; + assert( op>=0 && op=0 && op=0 && op=0 && opwsdStat.mxValue[op] ){ + wsdStat.mxValue[op] = wsdStat.nowValue[op]; + } +} +SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){ + wsdStatInit; + assert( N>=0 ); + assert( op>=0 && op=0 && op=0 ); + newValue = (sqlite3StatValueType)X; + assert( op>=0 && op=0 && opwsdStat.mxValue[op] ){ + wsdStat.mxValue[op] = newValue; + } +} + +/* +** Query status information. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_status64( + int op, + sqlite3_int64 *pCurrent, + sqlite3_int64 *pHighwater, + int resetFlag +){ + sqlite3_mutex *pMutex; + wsdStatInit; + if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ + return SQLITE_MISUSE_BKPT; + } +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; +#endif + pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex(); + sqlite3_mutex_enter(pMutex); + *pCurrent = wsdStat.nowValue[op]; + *pHighwater = wsdStat.mxValue[op]; + if( resetFlag ){ + wsdStat.mxValue[op] = wsdStat.nowValue[op]; + } + sqlite3_mutex_leave(pMutex); + (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */ + return SQLITE_OK; +} +SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ + sqlite3_int64 iCur, iHwtr; + int rc; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; +#endif + rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag); + if( rc==0 ){ + *pCurrent = (int)iCur; + *pHighwater = (int)iHwtr; + } + return rc; +} + +/* +** Query status information for a single database connection +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + sqlite3 *db, /* The database connection whose status is desired */ + int op, /* Status verb */ + int *pCurrent, /* Write current value here */ + int *pHighwater, /* Write high-water mark here */ + int resetFlag /* Reset high-water mark if true */ +){ + int rc = SQLITE_OK; /* Return code */ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + switch( op ){ + case SQLITE_DBSTATUS_LOOKASIDE_USED: { + *pCurrent = db->lookaside.nOut; + *pHighwater = db->lookaside.mxOut; + if( resetFlag ){ + db->lookaside.mxOut = db->lookaside.nOut; + } + break; + } + + case SQLITE_DBSTATUS_LOOKASIDE_HIT: + case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE: + case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: { + testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT ); + testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); + testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); + assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); + assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); + *pCurrent = 0; + *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; + if( resetFlag ){ + db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; + } + break; + } + + /* + ** Return an approximation for the amount of memory currently used + ** by all pagers associated with the given database connection. The + ** highwater mark is meaningless and is returned as zero. + */ + case SQLITE_DBSTATUS_CACHE_USED: { + int totalUsed = 0; + int i; + sqlite3BtreeEnterAll(db); + for(i=0; inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + Pager *pPager = sqlite3BtreePager(pBt); + totalUsed += sqlite3PagerMemUsed(pPager); + } + } + sqlite3BtreeLeaveAll(db); + *pCurrent = totalUsed; + *pHighwater = 0; + break; + } + + /* + ** *pCurrent gets an accurate estimate of the amount of memory used + ** to store the schema for all databases (main, temp, and any ATTACHed + ** databases. *pHighwater is set to zero. + */ + case SQLITE_DBSTATUS_SCHEMA_USED: { + int i; /* Used to iterate through schemas */ + int nByte = 0; /* Used to accumulate return value */ + + sqlite3BtreeEnterAll(db); + db->pnBytesFreed = &nByte; + for(i=0; inDb; i++){ + Schema *pSchema = db->aDb[i].pSchema; + if( ALWAYS(pSchema!=0) ){ + HashElem *p; + + nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( + pSchema->tblHash.count + + pSchema->trigHash.count + + pSchema->idxHash.count + + pSchema->fkeyHash.count + ); + nByte += sqlite3_msize(pSchema->tblHash.ht); + nByte += sqlite3_msize(pSchema->trigHash.ht); + nByte += sqlite3_msize(pSchema->idxHash.ht); + nByte += sqlite3_msize(pSchema->fkeyHash.ht); + + for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ + sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); + } + for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ + sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); + } + } + } + db->pnBytesFreed = 0; + sqlite3BtreeLeaveAll(db); + + *pHighwater = 0; + *pCurrent = nByte; + break; + } + + /* + ** *pCurrent gets an accurate estimate of the amount of memory used + ** to store all prepared statements. + ** *pHighwater is set to zero. + */ + case SQLITE_DBSTATUS_STMT_USED: { + struct Vdbe *pVdbe; /* Used to iterate through VMs */ + int nByte = 0; /* Used to accumulate return value */ + + db->pnBytesFreed = &nByte; + for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ + sqlite3VdbeClearObject(db, pVdbe); + sqlite3DbFree(db, pVdbe); + } + db->pnBytesFreed = 0; + + *pHighwater = 0; /* IMP: R-64479-57858 */ + *pCurrent = nByte; + + break; + } + + /* + ** Set *pCurrent to the total cache hits or misses encountered by all + ** pagers the database handle is connected to. *pHighwater is always set + ** to zero. + */ + case SQLITE_DBSTATUS_CACHE_HIT: + case SQLITE_DBSTATUS_CACHE_MISS: + case SQLITE_DBSTATUS_CACHE_WRITE:{ + int i; + int nRet = 0; + assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); + assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); + + for(i=0; inDb; i++){ + if( db->aDb[i].pBt ){ + Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); + sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); + } + } + *pHighwater = 0; /* IMP: R-42420-56072 */ + /* IMP: R-54100-20147 */ + /* IMP: R-29431-39229 */ + *pCurrent = nRet; + break; + } + + /* Set *pCurrent to non-zero if there are unresolved deferred foreign + ** key constraints. Set *pCurrent to zero if all foreign key constraints + ** have been satisfied. The *pHighwater is always set to zero. + */ + case SQLITE_DBSTATUS_DEFERRED_FKS: { + *pHighwater = 0; /* IMP: R-11967-56545 */ + *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; + break; + } + + default: { + rc = SQLITE_ERROR; + } + } + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/************** End of status.c **********************************************/ +/************** Begin file date.c ********************************************/ +/* +** 2003 October 31 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement date and time +** functions for SQLite. +** +** There is only one exported symbol in this file - the function +** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. +** All other code has file scope. +** +** SQLite processes all times and dates as julian day numbers. The +** dates and times are stored as the number of days since noon +** in Greenwich on November 24, 4714 B.C. according to the Gregorian +** calendar system. +** +** 1970-01-01 00:00:00 is JD 2440587.5 +** 2000-01-01 00:00:00 is JD 2451544.5 +** +** This implementation requires years to be expressed as a 4-digit number +** which means that only dates between 0000-01-01 and 9999-12-31 can +** be represented, even though julian day numbers allow a much wider +** range of dates. +** +** The Gregorian calendar system is used for all dates and times, +** even those that predate the Gregorian calendar. Historians usually +** use the julian calendar for dates prior to 1582-10-15 and for some +** dates afterwards, depending on locale. Beware of this difference. +** +** The conversion algorithms are implemented based on descriptions +** in the following text: +** +** Jean Meeus +** Astronomical Algorithms, 2nd Edition, 1998 +** ISBM 0-943396-61-1 +** Willmann-Bell, Inc +** Richmond, Virginia (USA) +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include */ +#include + +#ifndef SQLITE_OMIT_DATETIME_FUNCS + +/* +** The MSVC CRT on Windows CE may not have a localtime() function. +** So declare a substitute. The substitute function itself is +** defined in "os_win.c". +*/ +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ + (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) +struct tm *__cdecl localtime(const time_t *); +#endif + +/* +** A structure for holding a single date and time. +*/ +typedef struct DateTime DateTime; +struct DateTime { + sqlite3_int64 iJD; /* The julian day number times 86400000 */ + int Y, M, D; /* Year, month, and day */ + int h, m; /* Hour and minutes */ + int tz; /* Timezone offset in minutes */ + double s; /* Seconds */ + char validYMD; /* True (1) if Y,M,D are valid */ + char validHMS; /* True (1) if h,m,s are valid */ + char validJD; /* True (1) if iJD is valid */ + char validTZ; /* True (1) if tz is valid */ + char tzSet; /* Timezone was set explicitly */ +}; + + +/* +** Convert zDate into one or more integers according to the conversion +** specifier zFormat. +** +** zFormat[] contains 4 characters for each integer converted, except for +** the last integer which is specified by three characters. The meaning +** of a four-character format specifiers ABCD is: +** +** A: number of digits to convert. Always "2" or "4". +** B: minimum value. Always "0" or "1". +** C: maximum value, decoded as: +** a: 12 +** b: 14 +** c: 24 +** d: 31 +** e: 59 +** f: 9999 +** D: the separator character, or \000 to indicate this is the +** last number to convert. +** +** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would +** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-". +** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates +** the 2-digit day which is the last integer in the set. +** +** The function returns the number of successful conversions. +*/ +static int getDigits(const char *zDate, const char *zFormat, ...){ + /* The aMx[] array translates the 3rd character of each format + ** spec into a max size: a b c d e f */ + static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 }; + va_list ap; + int cnt = 0; + char nextC; + va_start(ap, zFormat); + do{ + char N = zFormat[0] - '0'; + char min = zFormat[1] - '0'; + int val = 0; + u16 max; + + assert( zFormat[2]>='a' && zFormat[2]<='f' ); + max = aMx[zFormat[2] - 'a']; + nextC = zFormat[3]; + val = 0; + while( N-- ){ + if( !sqlite3Isdigit(*zDate) ){ + goto end_getDigits; + } + val = val*10 + *zDate - '0'; + zDate++; + } + if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){ + goto end_getDigits; + } + *va_arg(ap,int*) = val; + zDate++; + cnt++; + zFormat += 4; + }while( nextC ); +end_getDigits: + va_end(ap); + return cnt; +} + +/* +** Parse a timezone extension on the end of a date-time. +** The extension is of the form: +** +** (+/-)HH:MM +** +** Or the "zulu" notation: +** +** Z +** +** If the parse is successful, write the number of minutes +** of change in p->tz and return 0. If a parser error occurs, +** return non-zero. +** +** A missing specifier is not considered an error. +*/ +static int parseTimezone(const char *zDate, DateTime *p){ + int sgn = 0; + int nHr, nMn; + int c; + while( sqlite3Isspace(*zDate) ){ zDate++; } + p->tz = 0; + c = *zDate; + if( c=='-' ){ + sgn = -1; + }else if( c=='+' ){ + sgn = +1; + }else if( c=='Z' || c=='z' ){ + zDate++; + goto zulu_time; + }else{ + return c!=0; + } + zDate++; + if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){ + return 1; + } + zDate += 5; + p->tz = sgn*(nMn + nHr*60); +zulu_time: + while( sqlite3Isspace(*zDate) ){ zDate++; } + p->tzSet = 1; + return *zDate!=0; +} + +/* +** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. +** The HH, MM, and SS must each be exactly 2 digits. The +** fractional seconds FFFF can be one or more digits. +** +** Return 1 if there is a parsing error and 0 on success. +*/ +static int parseHhMmSs(const char *zDate, DateTime *p){ + int h, m, s; + double ms = 0.0; + if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){ + return 1; + } + zDate += 5; + if( *zDate==':' ){ + zDate++; + if( getDigits(zDate, "20e", &s)!=1 ){ + return 1; + } + zDate += 2; + if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){ + double rScale = 1.0; + zDate++; + while( sqlite3Isdigit(*zDate) ){ + ms = ms*10.0 + *zDate - '0'; + rScale *= 10.0; + zDate++; + } + ms /= rScale; + } + }else{ + s = 0; + } + p->validJD = 0; + p->validHMS = 1; + p->h = h; + p->m = m; + p->s = s + ms; + if( parseTimezone(zDate, p) ) return 1; + p->validTZ = (p->tz!=0)?1:0; + return 0; +} + +/* +** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume +** that the YYYY-MM-DD is according to the Gregorian calendar. +** +** Reference: Meeus page 61 +*/ +static void computeJD(DateTime *p){ + int Y, M, D, A, B, X1, X2; + + if( p->validJD ) return; + if( p->validYMD ){ + Y = p->Y; + M = p->M; + D = p->D; + }else{ + Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ + M = 1; + D = 1; + } + if( M<=2 ){ + Y--; + M += 12; + } + A = Y/100; + B = 2 - A + (A/4); + X1 = 36525*(Y+4716)/100; + X2 = 306001*(M+1)/10000; + p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); + p->validJD = 1; + if( p->validHMS ){ + p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); + if( p->validTZ ){ + p->iJD -= p->tz*60000; + p->validYMD = 0; + p->validHMS = 0; + p->validTZ = 0; + } + } +} + +/* +** Parse dates of the form +** +** YYYY-MM-DD HH:MM:SS.FFF +** YYYY-MM-DD HH:MM:SS +** YYYY-MM-DD HH:MM +** YYYY-MM-DD +** +** Write the result into the DateTime structure and return 0 +** on success and 1 if the input string is not a well-formed +** date. +*/ +static int parseYyyyMmDd(const char *zDate, DateTime *p){ + int Y, M, D, neg; + + if( zDate[0]=='-' ){ + zDate++; + neg = 1; + }else{ + neg = 0; + } + if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ + return 1; + } + zDate += 10; + while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; } + if( parseHhMmSs(zDate, p)==0 ){ + /* We got the time */ + }else if( *zDate==0 ){ + p->validHMS = 0; + }else{ + return 1; + } + p->validJD = 0; + p->validYMD = 1; + p->Y = neg ? -Y : Y; + p->M = M; + p->D = D; + if( p->validTZ ){ + computeJD(p); + } + return 0; +} + +/* +** Set the time to the current time reported by the VFS. +** +** Return the number of errors. +*/ +static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ + p->iJD = sqlite3StmtCurrentTime(context); + if( p->iJD>0 ){ + p->validJD = 1; + return 0; + }else{ + return 1; + } +} + +/* +** Attempt to parse the given string into a julian day number. Return +** the number of errors. +** +** The following are acceptable forms for the input string: +** +** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM +** DDDD.DD +** now +** +** In the first form, the +/-HH:MM is always optional. The fractional +** seconds extension (the ".FFF") is optional. The seconds portion +** (":SS.FFF") is option. The year and date can be omitted as long +** as there is a time string. The time string can be omitted as long +** as there is a year and date. +*/ +static int parseDateOrTime( + sqlite3_context *context, + const char *zDate, + DateTime *p +){ + double r; + if( parseYyyyMmDd(zDate,p)==0 ){ + return 0; + }else if( parseHhMmSs(zDate, p)==0 ){ + return 0; + }else if( sqlite3StrICmp(zDate,"now")==0){ + return setDateTimeToCurrent(context, p); + }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ + p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); + p->validJD = 1; + return 0; + } + return 1; +} + +/* +** Compute the Year, Month, and Day from the julian day number. +*/ +static void computeYMD(DateTime *p){ + int Z, A, B, C, D, E, X1; + if( p->validYMD ) return; + if( !p->validJD ){ + p->Y = 2000; + p->M = 1; + p->D = 1; + }else{ + Z = (int)((p->iJD + 43200000)/86400000); + A = (int)((Z - 1867216.25)/36524.25); + A = Z + 1 + A - (A/4); + B = A + 1524; + C = (int)((B - 122.1)/365.25); + D = (36525*(C&32767))/100; + E = (int)((B-D)/30.6001); + X1 = (int)(30.6001*E); + p->D = B - D - X1; + p->M = E<14 ? E-1 : E-13; + p->Y = p->M>2 ? C - 4716 : C - 4715; + } + p->validYMD = 1; +} + +/* +** Compute the Hour, Minute, and Seconds from the julian day number. +*/ +static void computeHMS(DateTime *p){ + int s; + if( p->validHMS ) return; + computeJD(p); + s = (int)((p->iJD + 43200000) % 86400000); + p->s = s/1000.0; + s = (int)p->s; + p->s -= s; + p->h = s/3600; + s -= p->h*3600; + p->m = s/60; + p->s += s - p->m*60; + p->validHMS = 1; +} + +/* +** Compute both YMD and HMS +*/ +static void computeYMD_HMS(DateTime *p){ + computeYMD(p); + computeHMS(p); +} + +/* +** Clear the YMD and HMS and the TZ +*/ +static void clearYMD_HMS_TZ(DateTime *p){ + p->validYMD = 0; + p->validHMS = 0; + p->validTZ = 0; +} + +#ifndef SQLITE_OMIT_LOCALTIME +/* +** On recent Windows platforms, the localtime_s() function is available +** as part of the "Secure CRT". It is essentially equivalent to +** localtime_r() available under most POSIX platforms, except that the +** order of the parameters is reversed. +** +** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. +** +** If the user has not indicated to use localtime_r() or localtime_s() +** already, check for an MSVC build environment that provides +** localtime_s(). +*/ +#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \ + && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) +#undef HAVE_LOCALTIME_S +#define HAVE_LOCALTIME_S 1 +#endif + +/* +** The following routine implements the rough equivalent of localtime_r() +** using whatever operating-system specific localtime facility that +** is available. This routine returns 0 on success and +** non-zero on any kind of error. +** +** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this +** routine will always fail. +** +** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C +** library function localtime_r() is used to assist in the calculation of +** local time. +*/ +static int osLocaltime(time_t *t, struct tm *pTm){ + int rc; +#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S + struct tm *pX; +#if SQLITE_THREADSAFE>0 + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + sqlite3_mutex_enter(mutex); + pX = localtime(t); +#ifndef SQLITE_OMIT_BUILTIN_TEST + if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; +#endif + if( pX ) *pTm = *pX; + sqlite3_mutex_leave(mutex); + rc = pX==0; +#else +#ifndef SQLITE_OMIT_BUILTIN_TEST + if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; +#endif +#if HAVE_LOCALTIME_R + rc = localtime_r(t, pTm)==0; +#else + rc = localtime_s(pTm, t); +#endif /* HAVE_LOCALTIME_R */ +#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ + return rc; +} +#endif /* SQLITE_OMIT_LOCALTIME */ + + +#ifndef SQLITE_OMIT_LOCALTIME +/* +** Compute the difference (in milliseconds) between localtime and UTC +** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs, +** return this value and set *pRc to SQLITE_OK. +** +** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value +** is undefined in this case. +*/ +static sqlite3_int64 localtimeOffset( + DateTime *p, /* Date at which to calculate offset */ + sqlite3_context *pCtx, /* Write error here if one occurs */ + int *pRc /* OUT: Error code. SQLITE_OK or ERROR */ +){ + DateTime x, y; + time_t t; + struct tm sLocal; + + /* Initialize the contents of sLocal to avoid a compiler warning. */ + memset(&sLocal, 0, sizeof(sLocal)); + + x = *p; + computeYMD_HMS(&x); + if( x.Y<1971 || x.Y>=2038 ){ + /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only + ** works for years between 1970 and 2037. For dates outside this range, + ** SQLite attempts to map the year into an equivalent year within this + ** range, do the calculation, then map the year back. + */ + x.Y = 2000; + x.M = 1; + x.D = 1; + x.h = 0; + x.m = 0; + x.s = 0.0; + } else { + int s = (int)(x.s + 0.5); + x.s = s; + } + x.tz = 0; + x.validJD = 0; + computeJD(&x); + t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); + if( osLocaltime(&t, &sLocal) ){ + sqlite3_result_error(pCtx, "local time unavailable", -1); + *pRc = SQLITE_ERROR; + return 0; + } + y.Y = sLocal.tm_year + 1900; + y.M = sLocal.tm_mon + 1; + y.D = sLocal.tm_mday; + y.h = sLocal.tm_hour; + y.m = sLocal.tm_min; + y.s = sLocal.tm_sec; + y.validYMD = 1; + y.validHMS = 1; + y.validJD = 0; + y.validTZ = 0; + computeJD(&y); + *pRc = SQLITE_OK; + return y.iJD - x.iJD; +} +#endif /* SQLITE_OMIT_LOCALTIME */ + +/* +** Process a modifier to a date-time stamp. The modifiers are +** as follows: +** +** NNN days +** NNN hours +** NNN minutes +** NNN.NNNN seconds +** NNN months +** NNN years +** start of month +** start of year +** start of week +** start of day +** weekday N +** unixepoch +** localtime +** utc +** +** Return 0 on success and 1 if there is any kind of error. If the error +** is in a system call (i.e. localtime()), then an error message is written +** to context pCtx. If the error is an unrecognized modifier, no error is +** written to pCtx. +*/ +static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + int rc = 1; + int n; + double r; + char *z, zBuf[30]; + z = zBuf; + for(n=0; niJD += localtimeOffset(p, pCtx, &rc); + clearYMD_HMS_TZ(p); + } + break; + } +#endif + case 'u': { + /* + ** unixepoch + ** + ** Treat the current value of p->iJD as the number of + ** seconds since 1970. Convert to a real julian day number. + */ + if( strcmp(z, "unixepoch")==0 && p->validJD ){ + p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; + clearYMD_HMS_TZ(p); + rc = 0; + } +#ifndef SQLITE_OMIT_LOCALTIME + else if( strcmp(z, "utc")==0 ){ + if( p->tzSet==0 ){ + sqlite3_int64 c1; + computeJD(p); + c1 = localtimeOffset(p, pCtx, &rc); + if( rc==SQLITE_OK ){ + p->iJD -= c1; + clearYMD_HMS_TZ(p); + p->iJD += c1 - localtimeOffset(p, pCtx, &rc); + } + p->tzSet = 1; + }else{ + rc = SQLITE_OK; + } + } +#endif + break; + } + case 'w': { + /* + ** weekday N + ** + ** Move the date to the same time on the next occurrence of + ** weekday N where 0==Sunday, 1==Monday, and so forth. If the + ** date is already on the appropriate weekday, this is a no-op. + */ + if( strncmp(z, "weekday ", 8)==0 + && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8) + && (n=(int)r)==r && n>=0 && r<7 ){ + sqlite3_int64 Z; + computeYMD_HMS(p); + p->validTZ = 0; + p->validJD = 0; + computeJD(p); + Z = ((p->iJD + 129600000)/86400000) % 7; + if( Z>n ) Z -= 7; + p->iJD += (n - Z)*86400000; + clearYMD_HMS_TZ(p); + rc = 0; + } + break; + } + case 's': { + /* + ** start of TTTTT + ** + ** Move the date backwards to the beginning of the current day, + ** or month or year. + */ + if( strncmp(z, "start of ", 9)!=0 ) break; + z += 9; + computeYMD(p); + p->validHMS = 1; + p->h = p->m = 0; + p->s = 0.0; + p->validTZ = 0; + p->validJD = 0; + if( strcmp(z,"month")==0 ){ + p->D = 1; + rc = 0; + }else if( strcmp(z,"year")==0 ){ + computeYMD(p); + p->M = 1; + p->D = 1; + rc = 0; + }else if( strcmp(z,"day")==0 ){ + rc = 0; + } + break; + } + case '+': + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': { + double rRounder; + for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){} + if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){ + rc = 1; + break; + } + if( z[n]==':' ){ + /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the + ** specified number of hours, minutes, seconds, and fractional seconds + ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be + ** omitted. + */ + const char *z2 = z; + DateTime tx; + sqlite3_int64 day; + if( !sqlite3Isdigit(*z2) ) z2++; + memset(&tx, 0, sizeof(tx)); + if( parseHhMmSs(z2, &tx) ) break; + computeJD(&tx); + tx.iJD -= 43200000; + day = tx.iJD/86400000; + tx.iJD -= day*86400000; + if( z[0]=='-' ) tx.iJD = -tx.iJD; + computeJD(p); + clearYMD_HMS_TZ(p); + p->iJD += tx.iJD; + rc = 0; + break; + } + z += n; + while( sqlite3Isspace(*z) ) z++; + n = sqlite3Strlen30(z); + if( n>10 || n<3 ) break; + if( z[n-1]=='s' ){ z[n-1] = 0; n--; } + computeJD(p); + rc = 0; + rRounder = r<0 ? -0.5 : +0.5; + if( n==3 && strcmp(z,"day")==0 ){ + p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder); + }else if( n==4 && strcmp(z,"hour")==0 ){ + p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder); + }else if( n==6 && strcmp(z,"minute")==0 ){ + p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder); + }else if( n==6 && strcmp(z,"second")==0 ){ + p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder); + }else if( n==5 && strcmp(z,"month")==0 ){ + int x, y; + computeYMD_HMS(p); + p->M += (int)r; + x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; + p->Y += x; + p->M -= x*12; + p->validJD = 0; + computeJD(p); + y = (int)r; + if( y!=r ){ + p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder); + } + }else if( n==4 && strcmp(z,"year")==0 ){ + int y = (int)r; + computeYMD_HMS(p); + p->Y += y; + p->validJD = 0; + computeJD(p); + if( y!=r ){ + p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder); + } + }else{ + rc = 1; + } + clearYMD_HMS_TZ(p); + break; + } + default: { + break; + } + } + return rc; +} + +/* +** Process time function arguments. argv[0] is a date-time stamp. +** argv[1] and following are modifiers. Parse them all and write +** the resulting time into the DateTime structure p. Return 0 +** on success and 1 if there are any errors. +** +** If there are zero parameters (if even argv[0] is undefined) +** then assume a default value of "now" for argv[0]. +*/ +static int isDate( + sqlite3_context *context, + int argc, + sqlite3_value **argv, + DateTime *p +){ + int i; + const unsigned char *z; + int eType; + memset(p, 0, sizeof(*p)); + if( argc==0 ){ + return setDateTimeToCurrent(context, p); + } + if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT + || eType==SQLITE_INTEGER ){ + p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); + p->validJD = 1; + }else{ + z = sqlite3_value_text(argv[0]); + if( !z || parseDateOrTime(context, (char*)z, p) ){ + return 1; + } + } + for(i=1; iaLimit[SQLITE_LIMIT_LENGTH]+1 ); + testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); + if( n(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + return; + }else{ + z = sqlite3DbMallocRawNN(db, (int)n); + if( z==0 ){ + sqlite3_result_error_nomem(context); + return; + } + } + computeJD(&x); + computeYMD_HMS(&x); + for(i=j=0; zFmt[i]; i++){ + if( zFmt[i]!='%' ){ + z[j++] = zFmt[i]; + }else{ + i++; + switch( zFmt[i] ){ + case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break; + case 'f': { + double s = x.s; + if( s>59.999 ) s = 59.999; + sqlite3_snprintf(7, &z[j],"%06.3f", s); + j += sqlite3Strlen30(&z[j]); + break; + } + case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break; + case 'W': /* Fall thru */ + case 'j': { + int nDay; /* Number of days since 1st day of year */ + DateTime y = x; + y.validJD = 0; + y.M = 1; + y.D = 1; + computeJD(&y); + nDay = (int)((x.iJD-y.iJD+43200000)/86400000); + if( zFmt[i]=='W' ){ + int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ + wd = (int)(((x.iJD+43200000)/86400000)%7); + sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); + j += 2; + }else{ + sqlite3_snprintf(4, &z[j],"%03d",nDay+1); + j += 3; + } + break; + } + case 'J': { + sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0); + j+=sqlite3Strlen30(&z[j]); + break; + } + case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break; + case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break; + case 's': { + sqlite3_snprintf(30,&z[j],"%lld", + (i64)(x.iJD/1000 - 21086676*(i64)10000)); + j += sqlite3Strlen30(&z[j]); + break; + } + case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break; + case 'w': { + z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0'; + break; + } + case 'Y': { + sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]); + break; + } + default: z[j++] = '%'; break; + } + } + } + z[j] = 0; + sqlite3_result_text(context, z, -1, + z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC); +} + +/* +** current_time() +** +** This function returns the same value as time('now'). +*/ +static void ctimeFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + timeFunc(context, 0, 0); +} + +/* +** current_date() +** +** This function returns the same value as date('now'). +*/ +static void cdateFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + dateFunc(context, 0, 0); +} + +/* +** current_timestamp() +** +** This function returns the same value as datetime('now'). +*/ +static void ctimestampFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + datetimeFunc(context, 0, 0); +} +#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ + +#ifdef SQLITE_OMIT_DATETIME_FUNCS +/* +** If the library is compiled to omit the full-scale date and time +** handling (to get a smaller binary), the following minimal version +** of the functions current_time(), current_date() and current_timestamp() +** are included instead. This is to support column declarations that +** include "DEFAULT CURRENT_TIME" etc. +** +** This function uses the C-library functions time(), gmtime() +** and strftime(). The format string to pass to strftime() is supplied +** as the user-data for the function. +*/ +static void currentTimeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + time_t t; + char *zFormat = (char *)sqlite3_user_data(context); + sqlite3 *db; + sqlite3_int64 iT; + struct tm *pTm; + struct tm sNow; + char zBuf[20]; + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + + iT = sqlite3StmtCurrentTime(context); + if( iT<=0 ) return; + t = iT/1000 - 10000*(sqlite3_int64)21086676; +#if HAVE_GMTIME_R + pTm = gmtime_r(&t, &sNow); +#else + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + pTm = gmtime(&t); + if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); +#endif + if( pTm ){ + strftime(zBuf, 20, zFormat, &sNow); + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); + } +} +#endif + +/* +** This function registered all of the above C functions as SQL +** functions. This should be the only routine in this file with +** external linkage. +*/ +SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){ + static FuncDef aDateTimeFuncs[] = { +#ifndef SQLITE_OMIT_DATETIME_FUNCS + DFUNCTION(julianday, -1, 0, 0, juliandayFunc ), + DFUNCTION(date, -1, 0, 0, dateFunc ), + DFUNCTION(time, -1, 0, 0, timeFunc ), + DFUNCTION(datetime, -1, 0, 0, datetimeFunc ), + DFUNCTION(strftime, -1, 0, 0, strftimeFunc ), + DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), + DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), + DFUNCTION(current_date, 0, 0, 0, cdateFunc ), +#else + STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), + STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), + STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), +#endif + }; + sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)); +} + +/************** End of date.c ************************************************/ +/************** Begin file os.c **********************************************/ +/* +** 2005 November 29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains OS interface code that is common to all +** architectures. +*/ +#define _SQLITE_OS_C_ 1 +/* #include "sqliteInt.h" */ +#undef _SQLITE_OS_C_ + +/* +** If we compile with the SQLITE_TEST macro set, then the following block +** of code will give us the ability to simulate a disk I/O error. This +** is used for testing the I/O recovery logic. +*/ +#if defined(SQLITE_TEST) +SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ +SQLITE_API int sqlite3_diskfull_pending = 0; +SQLITE_API int sqlite3_diskfull = 0; +#endif /* defined(SQLITE_TEST) */ + +/* +** When testing, also keep a count of the number of open files. +*/ +#if defined(SQLITE_TEST) +SQLITE_API int sqlite3_open_file_count = 0; +#endif /* defined(SQLITE_TEST) */ + +/* +** The default SQLite sqlite3_vfs implementations do not allocate +** memory (actually, os_unix.c allocates a small amount of memory +** from within OsOpen()), but some third-party implementations may. +** So we test the effects of a malloc() failing and the sqlite3OsXXX() +** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro. +** +** The following functions are instrumented for malloc() failure +** testing: +** +** sqlite3OsRead() +** sqlite3OsWrite() +** sqlite3OsSync() +** sqlite3OsFileSize() +** sqlite3OsLock() +** sqlite3OsCheckReservedLock() +** sqlite3OsFileControl() +** sqlite3OsShmMap() +** sqlite3OsOpen() +** sqlite3OsDelete() +** sqlite3OsAccess() +** sqlite3OsFullPathname() +** +*/ +#if defined(SQLITE_TEST) +SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1; + #define DO_OS_MALLOC_TEST(x) \ + if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \ + void *pTstAlloc = sqlite3Malloc(10); \ + if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \ + sqlite3_free(pTstAlloc); \ + } +#else + #define DO_OS_MALLOC_TEST(x) +#endif + +/* +** The following routines are convenience wrappers around methods +** of the sqlite3_file object. This is mostly just syntactic sugar. All +** of this would be completely automatic if SQLite were coded using +** C++ instead of plain old C. +*/ +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){ + if( pId->pMethods ){ + pId->pMethods->xClose(pId); + pId->pMethods = 0; + } +} +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xRead(id, pBuf, amt, offset); +} +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xWrite(id, pBuf, amt, offset); +} +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){ + return id->pMethods->xTruncate(id, size); +} +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xSync(id, flags); +} +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xFileSize(id, pSize); +} +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xLock(id, lockType); +} +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){ + return id->pMethods->xUnlock(id, lockType); +} +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xCheckReservedLock(id, pResOut); +} + +/* +** Use sqlite3OsFileControl() when we are doing something that might fail +** and we need to know about the failures. Use sqlite3OsFileControlHint() +** when simply tossing information over the wall to the VFS and we do not +** really care if the VFS receives and understands the information since it +** is only a hint and can be safely ignored. The sqlite3OsFileControlHint() +** routine has no return value since the return value would be meaningless. +*/ +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ +#ifdef SQLITE_TEST + if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){ + /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite + ** is using a regular VFS, it is called after the corresponding + ** transaction has been committed. Injecting a fault at this point + ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM + ** but the transaction is committed anyway. + ** + ** The core must call OsFileControl() though, not OsFileControlHint(), + ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably + ** means the commit really has failed and an error should be returned + ** to the user. */ + DO_OS_MALLOC_TEST(id); + } +#endif + return id->pMethods->xFileControl(id, op, pArg); +} +SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){ + (void)id->pMethods->xFileControl(id, op, pArg); +} + +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){ + int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; + return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); +} +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ + return id->pMethods->xDeviceCharacteristics(id); +} +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ + return id->pMethods->xShmLock(id, offset, n, flags); +} +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){ + id->pMethods->xShmBarrier(id); +} +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){ + return id->pMethods->xShmUnmap(id, deleteFlag); +} +SQLITE_PRIVATE int sqlite3OsShmMap( + sqlite3_file *id, /* Database file handle */ + int iPage, + int pgsz, + int bExtend, /* True to extend file if necessary */ + void volatile **pp /* OUT: Pointer to mapping */ +){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); +} + +#if SQLITE_MAX_MMAP_SIZE>0 +/* The real implementation of xFetch and xUnfetch */ +SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ + DO_OS_MALLOC_TEST(id); + return id->pMethods->xFetch(id, iOff, iAmt, pp); +} +SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ + return id->pMethods->xUnfetch(id, iOff, p); +} +#else +/* No-op stubs to use when memory-mapped I/O is disabled */ +SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ + *pp = 0; + return SQLITE_OK; +} +SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ + return SQLITE_OK; +} +#endif + +/* +** The next group of routines are convenience wrappers around the +** VFS methods. +*/ +SQLITE_PRIVATE int sqlite3OsOpen( + sqlite3_vfs *pVfs, + const char *zPath, + sqlite3_file *pFile, + int flags, + int *pFlagsOut +){ + int rc; + DO_OS_MALLOC_TEST(0); + /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed + ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, + ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before + ** reaching the VFS. */ + rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut); + assert( rc==SQLITE_OK || pFile->pMethods==0 ); + return rc; +} +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + DO_OS_MALLOC_TEST(0); + assert( dirSync==0 || dirSync==1 ); + return pVfs->xDelete(pVfs, zPath, dirSync); +} +SQLITE_PRIVATE int sqlite3OsAccess( + sqlite3_vfs *pVfs, + const char *zPath, + int flags, + int *pResOut +){ + DO_OS_MALLOC_TEST(0); + return pVfs->xAccess(pVfs, zPath, flags, pResOut); +} +SQLITE_PRIVATE int sqlite3OsFullPathname( + sqlite3_vfs *pVfs, + const char *zPath, + int nPathOut, + char *zPathOut +){ + DO_OS_MALLOC_TEST(0); + zPathOut[0] = 0; + return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); +} +#ifndef SQLITE_OMIT_LOAD_EXTENSION +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + return pVfs->xDlOpen(pVfs, zPath); +} +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ + pVfs->xDlError(pVfs, nByte, zBufOut); +} +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){ + return pVfs->xDlSym(pVfs, pHdle, zSym); +} +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){ + pVfs->xDlClose(pVfs, pHandle); +} +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ + return pVfs->xRandomness(pVfs, nByte, zBufOut); +} +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ + return pVfs->xSleep(pVfs, nMicro); +} +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){ + return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; +} +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ + int rc; + /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() + ** method to get the current date and time if that method is available + ** (if iVersion is 2 or greater and the function pointer is not NULL) and + ** will fall back to xCurrentTime() if xCurrentTimeInt64() is + ** unavailable. + */ + if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ + rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); + }else{ + double r; + rc = pVfs->xCurrentTime(pVfs, &r); + *pTimeOut = (sqlite3_int64)(r*86400000.0); + } + return rc; +} + +SQLITE_PRIVATE int sqlite3OsOpenMalloc( + sqlite3_vfs *pVfs, + const char *zFile, + sqlite3_file **ppFile, + int flags, + int *pOutFlags +){ + int rc; + sqlite3_file *pFile; + pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); + if( pFile ){ + rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); + if( rc!=SQLITE_OK ){ + sqlite3_free(pFile); + }else{ + *ppFile = pFile; + } + }else{ + rc = SQLITE_NOMEM_BKPT; + } + return rc; +} +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){ + assert( pFile ); + sqlite3OsClose(pFile); + sqlite3_free(pFile); +} + +/* +** This function is a wrapper around the OS specific implementation of +** sqlite3_os_init(). The purpose of the wrapper is to provide the +** ability to simulate a malloc failure, so that the handling of an +** error in sqlite3_os_init() by the upper layers can be tested. +*/ +SQLITE_PRIVATE int sqlite3OsInit(void){ + void *p = sqlite3_malloc(10); + if( p==0 ) return SQLITE_NOMEM_BKPT; + sqlite3_free(p); + return sqlite3_os_init(); +} + +/* +** The list of all registered VFS implementations. +*/ +static sqlite3_vfs * SQLITE_WSD vfsList = 0; +#define vfsList GLOBAL(sqlite3_vfs *, vfsList) + +/* +** Locate a VFS by name. If no name is given, simply return the +** first VFS on the list. +*/ +SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfs){ + sqlite3_vfs *pVfs = 0; +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex; +#endif +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return 0; +#endif +#if SQLITE_THREADSAFE + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + sqlite3_mutex_enter(mutex); + for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ + if( zVfs==0 ) break; + if( strcmp(zVfs, pVfs->zName)==0 ) break; + } + sqlite3_mutex_leave(mutex); + return pVfs; +} + +/* +** Unlink a VFS from the linked list +*/ +static void vfsUnlink(sqlite3_vfs *pVfs){ + assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ); + if( pVfs==0 ){ + /* No-op */ + }else if( vfsList==pVfs ){ + vfsList = pVfs->pNext; + }else if( vfsList ){ + sqlite3_vfs *p = vfsList; + while( p->pNext && p->pNext!=pVfs ){ + p = p->pNext; + } + if( p->pNext==pVfs ){ + p->pNext = pVfs->pNext; + } + } +} + +/* +** Register a VFS with the system. It is harmless to register the same +** VFS multiple times. The new VFS becomes the default if makeDflt is +** true. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ + MUTEX_LOGIC(sqlite3_mutex *mutex;) +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return rc; +#endif +#ifdef SQLITE_ENABLE_API_ARMOR + if( pVfs==0 ) return SQLITE_MISUSE_BKPT; +#endif + + MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) + sqlite3_mutex_enter(mutex); + vfsUnlink(pVfs); + if( makeDflt || vfsList==0 ){ + pVfs->pNext = vfsList; + vfsList = pVfs; + }else{ + pVfs->pNext = vfsList->pNext; + vfsList->pNext = pVfs; + } + assert(vfsList); + sqlite3_mutex_leave(mutex); + return SQLITE_OK; +} + +/* +** Unregister a VFS so that it is no longer accessible. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + sqlite3_mutex_enter(mutex); + vfsUnlink(pVfs); + sqlite3_mutex_leave(mutex); + return SQLITE_OK; +} + +/************** End of os.c **************************************************/ +/************** Begin file fault.c *******************************************/ +/* +** 2008 Jan 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code to support the concept of "benign" +** malloc failures (when the xMalloc() or xRealloc() method of the +** sqlite3_mem_methods structure fails to allocate a block of memory +** and returns 0). +** +** Most malloc failures are non-benign. After they occur, SQLite +** abandons the current operation and returns an error code (usually +** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily +** fatal. For example, if a malloc fails while resizing a hash table, this +** is completely recoverable simply by not carrying out the resize. The +** hash table will continue to function normally. So a malloc failure +** during a hash table resize is a benign fault. +*/ + +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_OMIT_BUILTIN_TEST + +/* +** Global variables. +*/ +typedef struct BenignMallocHooks BenignMallocHooks; +static SQLITE_WSD struct BenignMallocHooks { + void (*xBenignBegin)(void); + void (*xBenignEnd)(void); +} sqlite3Hooks = { 0, 0 }; + +/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks +** structure. If writable static data is unsupported on the target, +** we have to locate the state vector at run-time. In the more common +** case where writable static data is supported, wsdHooks can refer directly +** to the "sqlite3Hooks" state vector declared above. +*/ +#ifdef SQLITE_OMIT_WSD +# define wsdHooksInit \ + BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks) +# define wsdHooks x[0] +#else +# define wsdHooksInit +# define wsdHooks sqlite3Hooks +#endif + + +/* +** Register hooks to call when sqlite3BeginBenignMalloc() and +** sqlite3EndBenignMalloc() are called, respectively. +*/ +SQLITE_PRIVATE void sqlite3BenignMallocHooks( + void (*xBenignBegin)(void), + void (*xBenignEnd)(void) +){ + wsdHooksInit; + wsdHooks.xBenignBegin = xBenignBegin; + wsdHooks.xBenignEnd = xBenignEnd; +} + +/* +** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that +** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc() +** indicates that subsequent malloc failures are non-benign. +*/ +SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){ + wsdHooksInit; + if( wsdHooks.xBenignBegin ){ + wsdHooks.xBenignBegin(); + } +} +SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){ + wsdHooksInit; + if( wsdHooks.xBenignEnd ){ + wsdHooks.xBenignEnd(); + } +} + +#endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */ + +/************** End of fault.c ***********************************************/ +/************** Begin file mem0.c ********************************************/ +/* +** 2008 October 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains a no-op memory allocation drivers for use when +** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented +** here always fail. SQLite will not operate with these drivers. These +** are merely placeholders. Real drivers must be substituted using +** sqlite3_config() before SQLite will operate. +*/ +/* #include "sqliteInt.h" */ + +/* +** This version of the memory allocator is the default. It is +** used when no other memory allocator is specified using compile-time +** macros. +*/ +#ifdef SQLITE_ZERO_MALLOC + +/* +** No-op versions of all memory allocation routines +*/ +static void *sqlite3MemMalloc(int nByte){ return 0; } +static void sqlite3MemFree(void *pPrior){ return; } +static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; } +static int sqlite3MemSize(void *pPrior){ return 0; } +static int sqlite3MemRoundup(int n){ return n; } +static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; } +static void sqlite3MemShutdown(void *NotUsed){ return; } + +/* +** This routine is the only routine in this file with external linkage. +** +** Populate the low-level memory allocation function pointers in +** sqlite3GlobalConfig.m with pointers to the routines in this file. +*/ +SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + static const sqlite3_mem_methods defaultMethods = { + sqlite3MemMalloc, + sqlite3MemFree, + sqlite3MemRealloc, + sqlite3MemSize, + sqlite3MemRoundup, + sqlite3MemInit, + sqlite3MemShutdown, + 0 + }; + sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); +} + +#endif /* SQLITE_ZERO_MALLOC */ + +/************** End of mem0.c ************************************************/ +/************** Begin file mem1.c ********************************************/ +/* +** 2007 August 14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains low-level memory allocation drivers for when +** SQLite will use the standard C-library malloc/realloc/free interface +** to obtain the memory it needs. +** +** This file contains implementations of the low-level memory allocation +** routines specified in the sqlite3_mem_methods object. The content of +** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The +** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the +** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The +** default configuration is to use memory allocation routines in this +** file. +** +** C-preprocessor macro summary: +** +** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if +** the malloc_usable_size() interface exists +** on the target platform. Or, this symbol +** can be set manually, if desired. +** If an equivalent interface exists by +** a different name, using a separate -D +** option to rename it. +** +** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone +** memory allocator. Set this symbol to enable +** building on older macs. +** +** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of +** _msize() on windows systems. This might +** be necessary when compiling for Delphi, +** for example. +*/ +/* #include "sqliteInt.h" */ + +/* +** This version of the memory allocator is the default. It is +** used when no other memory allocator is specified using compile-time +** macros. +*/ +#ifdef SQLITE_SYSTEM_MALLOC +#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) + +/* +** Use the zone allocator available on apple products unless the +** SQLITE_WITHOUT_ZONEMALLOC symbol is defined. +*/ +#include +#include +#include +static malloc_zone_t* _sqliteZone_; +#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x)) +#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x)); +#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y)) +#define SQLITE_MALLOCSIZE(x) \ + (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x)) + +#else /* if not __APPLE__ */ + +/* +** Use standard C library malloc and free on non-Apple systems. +** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined. +*/ +#define SQLITE_MALLOC(x) malloc(x) +#define SQLITE_FREE(x) free(x) +#define SQLITE_REALLOC(x,y) realloc((x),(y)) + +/* +** The malloc.h header file is needed for malloc_usable_size() function +** on some systems (e.g. Linux). +*/ +#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE +# define SQLITE_USE_MALLOC_H 1 +# define SQLITE_USE_MALLOC_USABLE_SIZE 1 +/* +** The MSVCRT has malloc_usable_size(), but it is called _msize(). The +** use of _msize() is automatic, but can be disabled by compiling with +** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires +** the malloc.h header file. +*/ +#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE) +# define SQLITE_USE_MALLOC_H +# define SQLITE_USE_MSIZE +#endif + +/* +** Include the malloc.h header file, if necessary. Also set define macro +** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize() +** for MSVC and malloc_usable_size() for most other systems (e.g. Linux). +** The memory size function can always be overridden manually by defining +** the macro SQLITE_MALLOCSIZE to the desired function name. +*/ +#if defined(SQLITE_USE_MALLOC_H) +# include +# if defined(SQLITE_USE_MALLOC_USABLE_SIZE) +# if !defined(SQLITE_MALLOCSIZE) +# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x) +# endif +# elif defined(SQLITE_USE_MSIZE) +# if !defined(SQLITE_MALLOCSIZE) +# define SQLITE_MALLOCSIZE _msize +# endif +# endif +#endif /* defined(SQLITE_USE_MALLOC_H) */ + +#endif /* __APPLE__ or not __APPLE__ */ + +/* +** Like malloc(), but remember the size of the allocation +** so that we can find it later using sqlite3MemSize(). +** +** For this low-level routine, we are guaranteed that nByte>0 because +** cases of nByte<=0 will be intercepted and dealt with by higher level +** routines. +*/ +static void *sqlite3MemMalloc(int nByte){ +#ifdef SQLITE_MALLOCSIZE + void *p = SQLITE_MALLOC( nByte ); + if( p==0 ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); + } + return p; +#else + sqlite3_int64 *p; + assert( nByte>0 ); + nByte = ROUND8(nByte); + p = SQLITE_MALLOC( nByte+8 ); + if( p ){ + p[0] = nByte; + p++; + }else{ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); + } + return (void *)p; +#endif +} + +/* +** Like free() but works for allocations obtained from sqlite3MemMalloc() +** or sqlite3MemRealloc(). +** +** For this low-level routine, we already know that pPrior!=0 since +** cases where pPrior==0 will have been intecepted and dealt with +** by higher-level routines. +*/ +static void sqlite3MemFree(void *pPrior){ +#ifdef SQLITE_MALLOCSIZE + SQLITE_FREE(pPrior); +#else + sqlite3_int64 *p = (sqlite3_int64*)pPrior; + assert( pPrior!=0 ); + p--; + SQLITE_FREE(p); +#endif +} + +/* +** Report the allocated size of a prior return from xMalloc() +** or xRealloc(). +*/ +static int sqlite3MemSize(void *pPrior){ +#ifdef SQLITE_MALLOCSIZE + assert( pPrior!=0 ); + return (int)SQLITE_MALLOCSIZE(pPrior); +#else + sqlite3_int64 *p; + assert( pPrior!=0 ); + p = (sqlite3_int64*)pPrior; + p--; + return (int)p[0]; +#endif +} + +/* +** Like realloc(). Resize an allocation previously obtained from +** sqlite3MemMalloc(). +** +** For this low-level interface, we know that pPrior!=0. Cases where +** pPrior==0 while have been intercepted by higher-level routine and +** redirected to xMalloc. Similarly, we know that nByte>0 because +** cases where nByte<=0 will have been intercepted by higher-level +** routines and redirected to xFree. +*/ +static void *sqlite3MemRealloc(void *pPrior, int nByte){ +#ifdef SQLITE_MALLOCSIZE + void *p = SQLITE_REALLOC(pPrior, nByte); + if( p==0 ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, + "failed memory resize %u to %u bytes", + SQLITE_MALLOCSIZE(pPrior), nByte); + } + return p; +#else + sqlite3_int64 *p = (sqlite3_int64*)pPrior; + assert( pPrior!=0 && nByte>0 ); + assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ + p--; + p = SQLITE_REALLOC(p, nByte+8 ); + if( p ){ + p[0] = nByte; + p++; + }else{ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, + "failed memory resize %u to %u bytes", + sqlite3MemSize(pPrior), nByte); + } + return (void*)p; +#endif +} + +/* +** Round up a request size to the next valid allocation size. +*/ +static int sqlite3MemRoundup(int n){ + return ROUND8(n); +} + +/* +** Initialize this module. +*/ +static int sqlite3MemInit(void *NotUsed){ +#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) + int cpuCount; + size_t len; + if( _sqliteZone_ ){ + return SQLITE_OK; + } + len = sizeof(cpuCount); + /* One usually wants to use hw.acctivecpu for MT decisions, but not here */ + sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0); + if( cpuCount>1 ){ + /* defer MT decisions to system malloc */ + _sqliteZone_ = malloc_default_zone(); + }else{ + /* only 1 core, use our own zone to contention over global locks, + ** e.g. we have our own dedicated locks */ + bool success; + malloc_zone_t* newzone = malloc_create_zone(4096, 0); + malloc_set_zone_name(newzone, "Sqlite_Heap"); + do{ + success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, + (void * volatile *)&_sqliteZone_); + }while(!_sqliteZone_); + if( !success ){ + /* somebody registered a zone first */ + malloc_destroy_zone(newzone); + } + } +#endif + UNUSED_PARAMETER(NotUsed); + return SQLITE_OK; +} + +/* +** Deinitialize this module. +*/ +static void sqlite3MemShutdown(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + return; +} + +/* +** This routine is the only routine in this file with external linkage. +** +** Populate the low-level memory allocation function pointers in +** sqlite3GlobalConfig.m with pointers to the routines in this file. +*/ +SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + static const sqlite3_mem_methods defaultMethods = { + sqlite3MemMalloc, + sqlite3MemFree, + sqlite3MemRealloc, + sqlite3MemSize, + sqlite3MemRoundup, + sqlite3MemInit, + sqlite3MemShutdown, + 0 + }; + sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); +} + +#endif /* SQLITE_SYSTEM_MALLOC */ + +/************** End of mem1.c ************************************************/ +/************** Begin file mem2.c ********************************************/ +/* +** 2007 August 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains low-level memory allocation drivers for when +** SQLite will use the standard C-library malloc/realloc/free interface +** to obtain the memory it needs while adding lots of additional debugging +** information to each allocation in order to help detect and fix memory +** leaks and memory usage errors. +** +** This file contains implementations of the low-level memory allocation +** routines specified in the sqlite3_mem_methods object. +*/ +/* #include "sqliteInt.h" */ + +/* +** This version of the memory allocator is used only if the +** SQLITE_MEMDEBUG macro is defined +*/ +#ifdef SQLITE_MEMDEBUG + +/* +** The backtrace functionality is only available with GLIBC +*/ +#ifdef __GLIBC__ + extern int backtrace(void**,int); + extern void backtrace_symbols_fd(void*const*,int,int); +#else +# define backtrace(A,B) 1 +# define backtrace_symbols_fd(A,B,C) +#endif +/* #include */ + +/* +** Each memory allocation looks like this: +** +** ------------------------------------------------------------------------ +** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | +** ------------------------------------------------------------------------ +** +** The application code sees only a pointer to the allocation. We have +** to back up from the allocation pointer to find the MemBlockHdr. The +** MemBlockHdr tells us the size of the allocation and the number of +** backtrace pointers. There is also a guard word at the end of the +** MemBlockHdr. +*/ +struct MemBlockHdr { + i64 iSize; /* Size of this allocation */ + struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ + char nBacktrace; /* Number of backtraces on this alloc */ + char nBacktraceSlots; /* Available backtrace slots */ + u8 nTitle; /* Bytes of title; includes '\0' */ + u8 eType; /* Allocation type code */ + int iForeGuard; /* Guard word for sanity */ +}; + +/* +** Guard words +*/ +#define FOREGUARD 0x80F5E153 +#define REARGUARD 0xE4676B53 + +/* +** Number of malloc size increments to track. +*/ +#define NCSIZE 1000 + +/* +** All of the static variables used by this module are collected +** into a single structure named "mem". This is to keep the +** static variables organized and to reduce namespace pollution +** when this module is combined with other in the amalgamation. +*/ +static struct { + + /* + ** Mutex to control access to the memory allocation subsystem. + */ + sqlite3_mutex *mutex; + + /* + ** Head and tail of a linked list of all outstanding allocations + */ + struct MemBlockHdr *pFirst; + struct MemBlockHdr *pLast; + + /* + ** The number of levels of backtrace to save in new allocations. + */ + int nBacktrace; + void (*xBacktrace)(int, int, void **); + + /* + ** Title text to insert in front of each block + */ + int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */ + char zTitle[100]; /* The title text */ + + /* + ** sqlite3MallocDisallow() increments the following counter. + ** sqlite3MallocAllow() decrements it. + */ + int disallow; /* Do not allow memory allocation */ + + /* + ** Gather statistics on the sizes of memory allocations. + ** nAlloc[i] is the number of allocation attempts of i*8 + ** bytes. i==NCSIZE is the number of allocation attempts for + ** sizes more than NCSIZE*8 bytes. + */ + int nAlloc[NCSIZE]; /* Total number of allocations */ + int nCurrent[NCSIZE]; /* Current number of allocations */ + int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */ + +} mem; + + +/* +** Adjust memory usage statistics +*/ +static void adjustStats(int iSize, int increment){ + int i = ROUND8(iSize)/8; + if( i>NCSIZE-1 ){ + i = NCSIZE - 1; + } + if( increment>0 ){ + mem.nAlloc[i]++; + mem.nCurrent[i]++; + if( mem.nCurrent[i]>mem.mxCurrent[i] ){ + mem.mxCurrent[i] = mem.nCurrent[i]; + } + }else{ + mem.nCurrent[i]--; + assert( mem.nCurrent[i]>=0 ); + } +} + +/* +** Given an allocation, find the MemBlockHdr for that allocation. +** +** This routine checks the guards at either end of the allocation and +** if they are incorrect it asserts. +*/ +static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ + struct MemBlockHdr *p; + int *pInt; + u8 *pU8; + int nReserve; + + p = (struct MemBlockHdr*)pAllocation; + p--; + assert( p->iForeGuard==(int)FOREGUARD ); + nReserve = ROUND8(p->iSize); + pInt = (int*)pAllocation; + pU8 = (u8*)pAllocation; + assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD ); + /* This checks any of the "extra" bytes allocated due + ** to rounding up to an 8 byte boundary to ensure + ** they haven't been overwritten. + */ + while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); + return p; +} + +/* +** Return the number of bytes currently allocated at address p. +*/ +static int sqlite3MemSize(void *p){ + struct MemBlockHdr *pHdr; + if( !p ){ + return 0; + } + pHdr = sqlite3MemsysGetHeader(p); + return (int)pHdr->iSize; +} + +/* +** Initialize the memory allocation subsystem. +*/ +static int sqlite3MemInit(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + assert( (sizeof(struct MemBlockHdr)&7) == 0 ); + if( !sqlite3GlobalConfig.bMemstat ){ + /* If memory status is enabled, then the malloc.c wrapper will already + ** hold the STATIC_MEM mutex when the routines here are invoked. */ + mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); + } + return SQLITE_OK; +} + +/* +** Deinitialize the memory allocation subsystem. +*/ +static void sqlite3MemShutdown(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + mem.mutex = 0; +} + +/* +** Round up a request size to the next valid allocation size. +*/ +static int sqlite3MemRoundup(int n){ + return ROUND8(n); +} + +/* +** Fill a buffer with pseudo-random bytes. This is used to preset +** the content of a new memory allocation to unpredictable values and +** to clear the content of a freed allocation to unpredictable values. +*/ +static void randomFill(char *pBuf, int nByte){ + unsigned int x, y, r; + x = SQLITE_PTR_TO_INT(pBuf); + y = nByte | 1; + while( nByte >= 4 ){ + x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); + y = y*1103515245 + 12345; + r = x ^ y; + *(int*)pBuf = r; + pBuf += 4; + nByte -= 4; + } + while( nByte-- > 0 ){ + x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); + y = y*1103515245 + 12345; + r = x ^ y; + *(pBuf++) = r & 0xff; + } +} + +/* +** Allocate nByte bytes of memory. +*/ +static void *sqlite3MemMalloc(int nByte){ + struct MemBlockHdr *pHdr; + void **pBt; + char *z; + int *pInt; + void *p = 0; + int totalSize; + int nReserve; + sqlite3_mutex_enter(mem.mutex); + assert( mem.disallow==0 ); + nReserve = ROUND8(nByte); + totalSize = nReserve + sizeof(*pHdr) + sizeof(int) + + mem.nBacktrace*sizeof(void*) + mem.nTitle; + p = malloc(totalSize); + if( p ){ + z = p; + pBt = (void**)&z[mem.nTitle]; + pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace]; + pHdr->pNext = 0; + pHdr->pPrev = mem.pLast; + if( mem.pLast ){ + mem.pLast->pNext = pHdr; + }else{ + mem.pFirst = pHdr; + } + mem.pLast = pHdr; + pHdr->iForeGuard = FOREGUARD; + pHdr->eType = MEMTYPE_HEAP; + pHdr->nBacktraceSlots = mem.nBacktrace; + pHdr->nTitle = mem.nTitle; + if( mem.nBacktrace ){ + void *aAddr[40]; + pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; + memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); + assert(pBt[0]); + if( mem.xBacktrace ){ + mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); + } + }else{ + pHdr->nBacktrace = 0; + } + if( mem.nTitle ){ + memcpy(z, mem.zTitle, mem.nTitle); + } + pHdr->iSize = nByte; + adjustStats(nByte, +1); + pInt = (int*)&pHdr[1]; + pInt[nReserve/sizeof(int)] = REARGUARD; + randomFill((char*)pInt, nByte); + memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); + p = (void*)pInt; + } + sqlite3_mutex_leave(mem.mutex); + return p; +} + +/* +** Free memory. +*/ +static void sqlite3MemFree(void *pPrior){ + struct MemBlockHdr *pHdr; + void **pBt; + char *z; + assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 + || mem.mutex!=0 ); + pHdr = sqlite3MemsysGetHeader(pPrior); + pBt = (void**)pHdr; + pBt -= pHdr->nBacktraceSlots; + sqlite3_mutex_enter(mem.mutex); + if( pHdr->pPrev ){ + assert( pHdr->pPrev->pNext==pHdr ); + pHdr->pPrev->pNext = pHdr->pNext; + }else{ + assert( mem.pFirst==pHdr ); + mem.pFirst = pHdr->pNext; + } + if( pHdr->pNext ){ + assert( pHdr->pNext->pPrev==pHdr ); + pHdr->pNext->pPrev = pHdr->pPrev; + }else{ + assert( mem.pLast==pHdr ); + mem.pLast = pHdr->pPrev; + } + z = (char*)pBt; + z -= pHdr->nTitle; + adjustStats((int)pHdr->iSize, -1); + randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + + (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); + free(z); + sqlite3_mutex_leave(mem.mutex); +} + +/* +** Change the size of an existing memory allocation. +** +** For this debugging implementation, we *always* make a copy of the +** allocation into a new place in memory. In this way, if the +** higher level code is using pointer to the old allocation, it is +** much more likely to break and we are much more liking to find +** the error. +*/ +static void *sqlite3MemRealloc(void *pPrior, int nByte){ + struct MemBlockHdr *pOldHdr; + void *pNew; + assert( mem.disallow==0 ); + assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ + pOldHdr = sqlite3MemsysGetHeader(pPrior); + pNew = sqlite3MemMalloc(nByte); + if( pNew ){ + memcpy(pNew, pPrior, (int)(nByteiSize ? nByte : pOldHdr->iSize)); + if( nByte>pOldHdr->iSize ){ + randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); + } + sqlite3MemFree(pPrior); + } + return pNew; +} + +/* +** Populate the low-level memory allocation function pointers in +** sqlite3GlobalConfig.m with pointers to the routines in this file. +*/ +SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + static const sqlite3_mem_methods defaultMethods = { + sqlite3MemMalloc, + sqlite3MemFree, + sqlite3MemRealloc, + sqlite3MemSize, + sqlite3MemRoundup, + sqlite3MemInit, + sqlite3MemShutdown, + 0 + }; + sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); +} + +/* +** Set the "type" of an allocation. +*/ +SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){ + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + struct MemBlockHdr *pHdr; + pHdr = sqlite3MemsysGetHeader(p); + assert( pHdr->iForeGuard==FOREGUARD ); + pHdr->eType = eType; + } +} + +/* +** Return TRUE if the mask of type in eType matches the type of the +** allocation p. Also return true if p==NULL. +** +** This routine is designed for use within an assert() statement, to +** verify the type of an allocation. For example: +** +** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); +*/ +SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ + int rc = 1; + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + struct MemBlockHdr *pHdr; + pHdr = sqlite3MemsysGetHeader(p); + assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ + if( (pHdr->eType&eType)==0 ){ + rc = 0; + } + } + return rc; +} + +/* +** Return TRUE if the mask of type in eType matches no bits of the type of the +** allocation p. Also return true if p==NULL. +** +** This routine is designed for use within an assert() statement, to +** verify the type of an allocation. For example: +** +** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); +*/ +SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){ + int rc = 1; + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + struct MemBlockHdr *pHdr; + pHdr = sqlite3MemsysGetHeader(p); + assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ + if( (pHdr->eType&eType)!=0 ){ + rc = 0; + } + } + return rc; +} + +/* +** Set the number of backtrace levels kept for each allocation. +** A value of zero turns off backtracing. The number is always rounded +** up to a multiple of 2. +*/ +SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){ + if( depth<0 ){ depth = 0; } + if( depth>20 ){ depth = 20; } + depth = (depth+1)&0xfe; + mem.nBacktrace = depth; +} + +SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){ + mem.xBacktrace = xBacktrace; +} + +/* +** Set the title string for subsequent allocations. +*/ +SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){ + unsigned int n = sqlite3Strlen30(zTitle) + 1; + sqlite3_mutex_enter(mem.mutex); + if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; + memcpy(mem.zTitle, zTitle, n); + mem.zTitle[n] = 0; + mem.nTitle = ROUND8(n); + sqlite3_mutex_leave(mem.mutex); +} + +SQLITE_PRIVATE void sqlite3MemdebugSync(){ + struct MemBlockHdr *pHdr; + for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ + void **pBt = (void**)pHdr; + pBt -= pHdr->nBacktraceSlots; + mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); + } +} + +/* +** Open the file indicated and write a log of all unfreed memory +** allocations into that log. +*/ +SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){ + FILE *out; + struct MemBlockHdr *pHdr; + void **pBt; + int i; + out = fopen(zFilename, "w"); + if( out==0 ){ + fprintf(stderr, "** Unable to output memory debug output log: %s **\n", + zFilename); + return; + } + for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ + char *z = (char*)pHdr; + z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; + fprintf(out, "**** %lld bytes at %p from %s ****\n", + pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); + if( pHdr->nBacktrace ){ + fflush(out); + pBt = (void**)pHdr; + pBt -= pHdr->nBacktraceSlots; + backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); + fprintf(out, "\n"); + } + } + fprintf(out, "COUNTS:\n"); + for(i=0; i=1 ); + size = mem3.aPool[i-1].u.hdr.size4x/4; + assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); + assert( size>=2 ); + if( size <= MX_SMALL ){ + memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); + }else{ + hash = size % N_HASH; + memsys3UnlinkFromList(i, &mem3.aiHash[hash]); + } +} + +/* +** Link the chunk at mem3.aPool[i] so that is on the list rooted +** at *pRoot. +*/ +static void memsys3LinkIntoList(u32 i, u32 *pRoot){ + assert( sqlite3_mutex_held(mem3.mutex) ); + mem3.aPool[i].u.list.next = *pRoot; + mem3.aPool[i].u.list.prev = 0; + if( *pRoot ){ + mem3.aPool[*pRoot].u.list.prev = i; + } + *pRoot = i; +} + +/* +** Link the chunk at index i into either the appropriate +** small chunk list, or into the large chunk hash table. +*/ +static void memsys3Link(u32 i){ + u32 size, hash; + assert( sqlite3_mutex_held(mem3.mutex) ); + assert( i>=1 ); + assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); + size = mem3.aPool[i-1].u.hdr.size4x/4; + assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); + assert( size>=2 ); + if( size <= MX_SMALL ){ + memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); + }else{ + hash = size % N_HASH; + memsys3LinkIntoList(i, &mem3.aiHash[hash]); + } +} + +/* +** If the STATIC_MEM mutex is not already held, obtain it now. The mutex +** will already be held (obtained by code in malloc.c) if +** sqlite3GlobalConfig.bMemStat is true. +*/ +static void memsys3Enter(void){ + if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){ + mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); + } + sqlite3_mutex_enter(mem3.mutex); +} +static void memsys3Leave(void){ + sqlite3_mutex_leave(mem3.mutex); +} + +/* +** Called when we are unable to satisfy an allocation of nBytes. +*/ +static void memsys3OutOfMemory(int nByte){ + if( !mem3.alarmBusy ){ + mem3.alarmBusy = 1; + assert( sqlite3_mutex_held(mem3.mutex) ); + sqlite3_mutex_leave(mem3.mutex); + sqlite3_release_memory(nByte); + sqlite3_mutex_enter(mem3.mutex); + mem3.alarmBusy = 0; + } +} + + +/* +** Chunk i is a free chunk that has been unlinked. Adjust its +** size parameters for check-out and return a pointer to the +** user portion of the chunk. +*/ +static void *memsys3Checkout(u32 i, u32 nBlock){ + u32 x; + assert( sqlite3_mutex_held(mem3.mutex) ); + assert( i>=1 ); + assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); + assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); + x = mem3.aPool[i-1].u.hdr.size4x; + mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); + mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; + mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; + return &mem3.aPool[i]; +} + +/* +** Carve a piece off of the end of the mem3.iMaster free chunk. +** Return a pointer to the new allocation. Or, if the master chunk +** is not large enough, return 0. +*/ +static void *memsys3FromMaster(u32 nBlock){ + assert( sqlite3_mutex_held(mem3.mutex) ); + assert( mem3.szMaster>=nBlock ); + if( nBlock>=mem3.szMaster-1 ){ + /* Use the entire master */ + void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster); + mem3.iMaster = 0; + mem3.szMaster = 0; + mem3.mnMaster = 0; + return p; + }else{ + /* Split the master block. Return the tail. */ + u32 newi, x; + newi = mem3.iMaster + mem3.szMaster - nBlock; + assert( newi > mem3.iMaster+1 ); + mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock; + mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2; + mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; + mem3.szMaster -= nBlock; + mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster; + x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; + mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; + if( mem3.szMaster < mem3.mnMaster ){ + mem3.mnMaster = mem3.szMaster; + } + return (void*)&mem3.aPool[newi]; + } +} + +/* +** *pRoot is the head of a list of free chunks of the same size +** or same size hash. In other words, *pRoot is an entry in either +** mem3.aiSmall[] or mem3.aiHash[]. +** +** This routine examines all entries on the given list and tries +** to coalesce each entries with adjacent free chunks. +** +** If it sees a chunk that is larger than mem3.iMaster, it replaces +** the current mem3.iMaster with the new larger chunk. In order for +** this mem3.iMaster replacement to work, the master chunk must be +** linked into the hash tables. That is not the normal state of +** affairs, of course. The calling routine must link the master +** chunk before invoking this routine, then must unlink the (possibly +** changed) master chunk once this routine has finished. +*/ +static void memsys3Merge(u32 *pRoot){ + u32 iNext, prev, size, i, x; + + assert( sqlite3_mutex_held(mem3.mutex) ); + for(i=*pRoot; i>0; i=iNext){ + iNext = mem3.aPool[i].u.list.next; + size = mem3.aPool[i-1].u.hdr.size4x; + assert( (size&1)==0 ); + if( (size&2)==0 ){ + memsys3UnlinkFromList(i, pRoot); + assert( i > mem3.aPool[i-1].u.hdr.prevSize ); + prev = i - mem3.aPool[i-1].u.hdr.prevSize; + if( prev==iNext ){ + iNext = mem3.aPool[prev].u.list.next; + } + memsys3Unlink(prev); + size = i + size/4 - prev; + x = mem3.aPool[prev-1].u.hdr.size4x & 2; + mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; + mem3.aPool[prev+size-1].u.hdr.prevSize = size; + memsys3Link(prev); + i = prev; + }else{ + size /= 4; + } + if( size>mem3.szMaster ){ + mem3.iMaster = i; + mem3.szMaster = size; + } + } +} + +/* +** Return a block of memory of at least nBytes in size. +** Return NULL if unable. +** +** This function assumes that the necessary mutexes, if any, are +** already held by the caller. Hence "Unsafe". +*/ +static void *memsys3MallocUnsafe(int nByte){ + u32 i; + u32 nBlock; + u32 toFree; + + assert( sqlite3_mutex_held(mem3.mutex) ); + assert( sizeof(Mem3Block)==8 ); + if( nByte<=12 ){ + nBlock = 2; + }else{ + nBlock = (nByte + 11)/8; + } + assert( nBlock>=2 ); + + /* STEP 1: + ** Look for an entry of the correct size in either the small + ** chunk table or in the large chunk hash table. This is + ** successful most of the time (about 9 times out of 10). + */ + if( nBlock <= MX_SMALL ){ + i = mem3.aiSmall[nBlock-2]; + if( i>0 ){ + memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); + return memsys3Checkout(i, nBlock); + } + }else{ + int hash = nBlock % N_HASH; + for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){ + if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ + memsys3UnlinkFromList(i, &mem3.aiHash[hash]); + return memsys3Checkout(i, nBlock); + } + } + } + + /* STEP 2: + ** Try to satisfy the allocation by carving a piece off of the end + ** of the master chunk. This step usually works if step 1 fails. + */ + if( mem3.szMaster>=nBlock ){ + return memsys3FromMaster(nBlock); + } + + + /* STEP 3: + ** Loop through the entire memory pool. Coalesce adjacent free + ** chunks. Recompute the master chunk as the largest free chunk. + ** Then try again to satisfy the allocation by carving a piece off + ** of the end of the master chunk. This step happens very + ** rarely (we hope!) + */ + for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){ + memsys3OutOfMemory(toFree); + if( mem3.iMaster ){ + memsys3Link(mem3.iMaster); + mem3.iMaster = 0; + mem3.szMaster = 0; + } + for(i=0; i=nBlock ){ + return memsys3FromMaster(nBlock); + } + } + } + + /* If none of the above worked, then we fail. */ + return 0; +} + +/* +** Free an outstanding memory allocation. +** +** This function assumes that the necessary mutexes, if any, are +** already held by the caller. Hence "Unsafe". +*/ +static void memsys3FreeUnsafe(void *pOld){ + Mem3Block *p = (Mem3Block*)pOld; + int i; + u32 size, x; + assert( sqlite3_mutex_held(mem3.mutex) ); + assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] ); + i = p - mem3.aPool; + assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); + size = mem3.aPool[i-1].u.hdr.size4x/4; + assert( i+size<=mem3.nPool+1 ); + mem3.aPool[i-1].u.hdr.size4x &= ~1; + mem3.aPool[i+size-1].u.hdr.prevSize = size; + mem3.aPool[i+size-1].u.hdr.size4x &= ~2; + memsys3Link(i); + + /* Try to expand the master using the newly freed chunk */ + if( mem3.iMaster ){ + while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){ + size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize; + mem3.iMaster -= size; + mem3.szMaster += size; + memsys3Unlink(mem3.iMaster); + x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; + mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; + mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; + } + x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; + while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){ + memsys3Unlink(mem3.iMaster+mem3.szMaster); + mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4; + mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; + mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; + } + } +} + +/* +** Return the size of an outstanding allocation, in bytes. The +** size returned omits the 8-byte header overhead. This only +** works for chunks that are currently checked out. +*/ +static int memsys3Size(void *p){ + Mem3Block *pBlock; + assert( p!=0 ); + pBlock = (Mem3Block*)p; + assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); + return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; +} + +/* +** Round up a request size to the next valid allocation size. +*/ +static int memsys3Roundup(int n){ + if( n<=12 ){ + return 12; + }else{ + return ((n+11)&~7) - 4; + } +} + +/* +** Allocate nBytes of memory. +*/ +static void *memsys3Malloc(int nBytes){ + sqlite3_int64 *p; + assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */ + memsys3Enter(); + p = memsys3MallocUnsafe(nBytes); + memsys3Leave(); + return (void*)p; +} + +/* +** Free memory. +*/ +static void memsys3Free(void *pPrior){ + assert( pPrior ); + memsys3Enter(); + memsys3FreeUnsafe(pPrior); + memsys3Leave(); +} + +/* +** Change the size of an existing memory allocation +*/ +static void *memsys3Realloc(void *pPrior, int nBytes){ + int nOld; + void *p; + if( pPrior==0 ){ + return sqlite3_malloc(nBytes); + } + if( nBytes<=0 ){ + sqlite3_free(pPrior); + return 0; + } + nOld = memsys3Size(pPrior); + if( nBytes<=nOld && nBytes>=nOld-128 ){ + return pPrior; + } + memsys3Enter(); + p = memsys3MallocUnsafe(nBytes); + if( p ){ + if( nOld>1)!=(size&1) ){ + fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]); + assert( 0 ); + break; + } + if( size&1 ){ + fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); + }else{ + fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, + i==mem3.iMaster ? " **master**" : ""); + } + } + for(i=0; i0; j=mem3.aPool[j].u.list.next){ + fprintf(out, " %p(%d)", &mem3.aPool[j], + (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); + } + fprintf(out, "\n"); + } + for(i=0; i0; j=mem3.aPool[j].u.list.next){ + fprintf(out, " %p(%d)", &mem3.aPool[j], + (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); + } + fprintf(out, "\n"); + } + fprintf(out, "master=%d\n", mem3.iMaster); + fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8); + fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8); + sqlite3_mutex_leave(mem3.mutex); + if( out==stdout ){ + fflush(stdout); + }else{ + fclose(out); + } +#else + UNUSED_PARAMETER(zFilename); +#endif +} + +/* +** This routine is the only routine in this file with external +** linkage. +** +** Populate the low-level memory allocation function pointers in +** sqlite3GlobalConfig.m with pointers to the routines in this file. The +** arguments specify the block of memory to manage. +** +** This routine is only called by sqlite3_config(), and therefore +** is not required to be threadsafe (it is not). +*/ +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){ + static const sqlite3_mem_methods mempoolMethods = { + memsys3Malloc, + memsys3Free, + memsys3Realloc, + memsys3Size, + memsys3Roundup, + memsys3Init, + memsys3Shutdown, + 0 + }; + return &mempoolMethods; +} + +#endif /* SQLITE_ENABLE_MEMSYS3 */ + +/************** End of mem3.c ************************************************/ +/************** Begin file mem5.c ********************************************/ +/* +** 2007 October 14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement a memory +** allocation subsystem for use by SQLite. +** +** This version of the memory allocation subsystem omits all +** use of malloc(). The application gives SQLite a block of memory +** before calling sqlite3_initialize() from which allocations +** are made and returned by the xMalloc() and xRealloc() +** implementations. Once sqlite3_initialize() has been called, +** the amount of memory available to SQLite is fixed and cannot +** be changed. +** +** This version of the memory allocation subsystem is included +** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. +** +** This memory allocator uses the following algorithm: +** +** 1. All memory allocation sizes are rounded up to a power of 2. +** +** 2. If two adjacent free blocks are the halves of a larger block, +** then the two blocks are coalesced into the single larger block. +** +** 3. New memory is allocated from the first available free block. +** +** This algorithm is described in: J. M. Robson. "Bounds for Some Functions +** Concerning Dynamic Storage Allocation". Journal of the Association for +** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499. +** +** Let n be the size of the largest allocation divided by the minimum +** allocation size (after rounding all sizes up to a power of 2.) Let M +** be the maximum amount of memory ever outstanding at one time. Let +** N be the total amount of memory available for allocation. Robson +** proved that this memory allocator will never breakdown due to +** fragmentation as long as the following constraint holds: +** +** N >= M*(1 + log2(n)/2) - n + 1 +** +** The sqlite3_status() logic tracks the maximum values of n and M so +** that an application can, at any time, verify this constraint. +*/ +/* #include "sqliteInt.h" */ + +/* +** This version of the memory allocator is used only when +** SQLITE_ENABLE_MEMSYS5 is defined. +*/ +#ifdef SQLITE_ENABLE_MEMSYS5 + +/* +** A minimum allocation is an instance of the following structure. +** Larger allocations are an array of these structures where the +** size of the array is a power of 2. +** +** The size of this object must be a power of two. That fact is +** verified in memsys5Init(). +*/ +typedef struct Mem5Link Mem5Link; +struct Mem5Link { + int next; /* Index of next free chunk */ + int prev; /* Index of previous free chunk */ +}; + +/* +** Maximum size of any allocation is ((1<=0 && i=0 && iLogsize<=LOGMAX ); + assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); + + next = MEM5LINK(i)->next; + prev = MEM5LINK(i)->prev; + if( prev<0 ){ + mem5.aiFreelist[iLogsize] = next; + }else{ + MEM5LINK(prev)->next = next; + } + if( next>=0 ){ + MEM5LINK(next)->prev = prev; + } +} + +/* +** Link the chunk at mem5.aPool[i] so that is on the iLogsize +** free list. +*/ +static void memsys5Link(int i, int iLogsize){ + int x; + assert( sqlite3_mutex_held(mem5.mutex) ); + assert( i>=0 && i=0 && iLogsize<=LOGMAX ); + assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); + + x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; + MEM5LINK(i)->prev = -1; + if( x>=0 ){ + assert( xprev = i; + } + mem5.aiFreelist[iLogsize] = i; +} + +/* +** Obtain or release the mutex needed to access global data structures. +*/ +static void memsys5Enter(void){ + sqlite3_mutex_enter(mem5.mutex); +} +static void memsys5Leave(void){ + sqlite3_mutex_leave(mem5.mutex); +} + +/* +** Return the size of an outstanding allocation, in bytes. +** This only works for chunks that are currently checked out. +*/ +static int memsys5Size(void *p){ + int iSize, i; + assert( p!=0 ); + i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); + assert( i>=0 && i0 ); + + /* No more than 1GiB per allocation */ + if( nByte > 0x40000000 ) return 0; + +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* Keep track of the maximum allocation request. Even unfulfilled + ** requests are counted */ + if( (u32)nByte>mem5.maxRequest ){ + mem5.maxRequest = nByte; + } +#endif + + + /* Round nByte up to the next valid power of two */ + for(iFullSz=mem5.szAtom,iLogsize=0; iFullSzLOGMAX ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte); + return 0; + } + i = mem5.aiFreelist[iBin]; + memsys5Unlink(i, iBin); + while( iBin>iLogsize ){ + int newSize; + + iBin--; + newSize = 1 << iBin; + mem5.aCtrl[i+newSize] = CTRL_FREE | iBin; + memsys5Link(i+newSize, iBin); + } + mem5.aCtrl[i] = iLogsize; + +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* Update allocator performance statistics. */ + mem5.nAlloc++; + mem5.totalAlloc += iFullSz; + mem5.totalExcess += iFullSz - nByte; + mem5.currentCount++; + mem5.currentOut += iFullSz; + if( mem5.maxCount=0 && iBlock0 ); + assert( mem5.currentOut>=(size*mem5.szAtom) ); + mem5.currentCount--; + mem5.currentOut -= size*mem5.szAtom; + assert( mem5.currentOut>0 || mem5.currentCount==0 ); + assert( mem5.currentCount>0 || mem5.currentOut==0 ); +#endif + + mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize; + while( ALWAYS(iLogsize>iLogsize) & 1 ){ + iBuddy = iBlock - size; + assert( iBuddy>=0 ); + }else{ + iBuddy = iBlock + size; + if( iBuddy>=mem5.nBlock ) break; + } + if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break; + memsys5Unlink(iBuddy, iLogsize); + iLogsize++; + if( iBuddy0 ){ + memsys5Enter(); + p = memsys5MallocUnsafe(nBytes); + memsys5Leave(); + } + return (void*)p; +} + +/* +** Free memory. +** +** The outer layer memory allocator prevents this routine from +** being called with pPrior==0. +*/ +static void memsys5Free(void *pPrior){ + assert( pPrior!=0 ); + memsys5Enter(); + memsys5FreeUnsafe(pPrior); + memsys5Leave(); +} + +/* +** Change the size of an existing memory allocation. +** +** The outer layer memory allocator prevents this routine from +** being called with pPrior==0. +** +** nBytes is always a value obtained from a prior call to +** memsys5Round(). Hence nBytes is always a non-negative power +** of two. If nBytes==0 that means that an oversize allocation +** (an allocation larger than 0x40000000) was requested and this +** routine should return 0 without freeing pPrior. +*/ +static void *memsys5Realloc(void *pPrior, int nBytes){ + int nOld; + void *p; + assert( pPrior!=0 ); + assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ + assert( nBytes>=0 ); + if( nBytes==0 ){ + return 0; + } + nOld = memsys5Size(pPrior); + if( nBytes<=nOld ){ + return pPrior; + } + p = memsys5Malloc(nBytes); + if( p ){ + memcpy(p, pPrior, nOld); + memsys5Free(pPrior); + } + return p; +} + +/* +** Round up a request size to the next valid allocation size. If +** the allocation is too large to be handled by this allocation system, +** return 0. +** +** All allocations must be a power of two and must be expressed by a +** 32-bit signed integer. Hence the largest allocation is 0x40000000 +** or 1073741824 bytes. +*/ +static int memsys5Roundup(int n){ + int iFullSz; + if( n > 0x40000000 ) return 0; + for(iFullSz=mem5.szAtom; iFullSz 0 +** memsys5Log(2) -> 1 +** memsys5Log(4) -> 2 +** memsys5Log(5) -> 3 +** memsys5Log(8) -> 3 +** memsys5Log(9) -> 4 +*/ +static int memsys5Log(int iValue){ + int iLog; + for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<mem5.szAtom ){ + mem5.szAtom = mem5.szAtom << 1; + } + + mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8))); + mem5.zPool = zByte; + mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom]; + + for(ii=0; ii<=LOGMAX; ii++){ + mem5.aiFreelist[ii] = -1; + } + + iOffset = 0; + for(ii=LOGMAX; ii>=0; ii--){ + int nAlloc = (1<mem5.nBlock); + } + + /* If a mutex is required for normal operation, allocate one */ + if( sqlite3GlobalConfig.bMemstat==0 ){ + mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); + } + + return SQLITE_OK; +} + +/* +** Deinitialize this module. +*/ +static void memsys5Shutdown(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + mem5.mutex = 0; + return; +} + +#ifdef SQLITE_TEST +/* +** Open the file indicated and write a log of all unfreed memory +** allocations into that log. +*/ +SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){ + FILE *out; + int i, j, n; + int nMinLog; + + if( zFilename==0 || zFilename[0]==0 ){ + out = stdout; + }else{ + out = fopen(zFilename, "w"); + if( out==0 ){ + fprintf(stderr, "** Unable to output memory debug output log: %s **\n", + zFilename); + return; + } + } + memsys5Enter(); + nMinLog = memsys5Log(mem5.szAtom); + for(i=0; i<=LOGMAX && i+nMinLog<32; i++){ + for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} + fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n); + } + fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc); + fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc); + fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess); + fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut); + fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount); + fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut); + fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount); + fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest); + memsys5Leave(); + if( out==stdout ){ + fflush(stdout); + }else{ + fclose(out); + } +} +#endif + +/* +** This routine is the only routine in this file with external +** linkage. It returns a pointer to a static sqlite3_mem_methods +** struct populated with the memsys5 methods. +*/ +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){ + static const sqlite3_mem_methods memsys5Methods = { + memsys5Malloc, + memsys5Free, + memsys5Realloc, + memsys5Size, + memsys5Roundup, + memsys5Init, + memsys5Shutdown, + 0 + }; + return &memsys5Methods; +} + +#endif /* SQLITE_ENABLE_MEMSYS5 */ + +/************** End of mem5.c ************************************************/ +/************** Begin file mutex.c *******************************************/ +/* +** 2007 August 14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement mutexes. +** +** This file contains code that is common across all mutex implementations. +*/ +/* #include "sqliteInt.h" */ + +#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) +/* +** For debugging purposes, record when the mutex subsystem is initialized +** and uninitialized so that we can assert() if there is an attempt to +** allocate a mutex while the system is uninitialized. +*/ +static SQLITE_WSD int mutexIsInit = 0; +#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */ + + +#ifndef SQLITE_MUTEX_OMIT +/* +** Initialize the mutex system. +*/ +SQLITE_PRIVATE int sqlite3MutexInit(void){ + int rc = SQLITE_OK; + if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ + /* If the xMutexAlloc method has not been set, then the user did not + ** install a mutex implementation via sqlite3_config() prior to + ** sqlite3_initialize() being called. This block copies pointers to + ** the default implementation into the sqlite3GlobalConfig structure. + */ + sqlite3_mutex_methods const *pFrom; + sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; + + if( sqlite3GlobalConfig.bCoreMutex ){ + pFrom = sqlite3DefaultMutex(); + }else{ + pFrom = sqlite3NoopMutex(); + } + pTo->xMutexInit = pFrom->xMutexInit; + pTo->xMutexEnd = pFrom->xMutexEnd; + pTo->xMutexFree = pFrom->xMutexFree; + pTo->xMutexEnter = pFrom->xMutexEnter; + pTo->xMutexTry = pFrom->xMutexTry; + pTo->xMutexLeave = pFrom->xMutexLeave; + pTo->xMutexHeld = pFrom->xMutexHeld; + pTo->xMutexNotheld = pFrom->xMutexNotheld; + sqlite3MemoryBarrier(); + pTo->xMutexAlloc = pFrom->xMutexAlloc; + } + assert( sqlite3GlobalConfig.mutex.xMutexInit ); + rc = sqlite3GlobalConfig.mutex.xMutexInit(); + +#ifdef SQLITE_DEBUG + GLOBAL(int, mutexIsInit) = 1; +#endif + + return rc; +} + +/* +** Shutdown the mutex system. This call frees resources allocated by +** sqlite3MutexInit(). +*/ +SQLITE_PRIVATE int sqlite3MutexEnd(void){ + int rc = SQLITE_OK; + if( sqlite3GlobalConfig.mutex.xMutexEnd ){ + rc = sqlite3GlobalConfig.mutex.xMutexEnd(); + } + +#ifdef SQLITE_DEBUG + GLOBAL(int, mutexIsInit) = 0; +#endif + + return rc; +} + +/* +** Retrieve a pointer to a static mutex or allocate a new dynamic one. +*/ +SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int id){ +#ifndef SQLITE_OMIT_AUTOINIT + if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; + if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; +#endif + assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); + return sqlite3GlobalConfig.mutex.xMutexAlloc(id); +} + +SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ + if( !sqlite3GlobalConfig.bCoreMutex ){ + return 0; + } + assert( GLOBAL(int, mutexIsInit) ); + assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); + return sqlite3GlobalConfig.mutex.xMutexAlloc(id); +} + +/* +** Free a dynamic mutex. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){ + if( p ){ + assert( sqlite3GlobalConfig.mutex.xMutexFree ); + sqlite3GlobalConfig.mutex.xMutexFree(p); + } +} + +/* +** Obtain the mutex p. If some other thread already has the mutex, block +** until it can be obtained. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){ + if( p ){ + assert( sqlite3GlobalConfig.mutex.xMutexEnter ); + sqlite3GlobalConfig.mutex.xMutexEnter(p); + } +} + +/* +** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another +** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){ + int rc = SQLITE_OK; + if( p ){ + assert( sqlite3GlobalConfig.mutex.xMutexTry ); + return sqlite3GlobalConfig.mutex.xMutexTry(p); + } + return rc; +} + +/* +** The sqlite3_mutex_leave() routine exits a mutex that was previously +** entered by the same thread. The behavior is undefined if the mutex +** is not currently entered. If a NULL pointer is passed as an argument +** this function is a no-op. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){ + if( p ){ + assert( sqlite3GlobalConfig.mutex.xMutexLeave ); + sqlite3GlobalConfig.mutex.xMutexLeave(p); + } +} + +#ifndef NDEBUG +/* +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are +** intended for use inside assert() statements. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex *p){ + assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); + return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); +} +SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){ + assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); + return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); +} +#endif + +#endif /* !defined(SQLITE_MUTEX_OMIT) */ + +/************** End of mutex.c ***********************************************/ +/************** Begin file mutex_noop.c **************************************/ +/* +** 2008 October 07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement mutexes. +** +** This implementation in this file does not provide any mutual +** exclusion and is thus suitable for use only in applications +** that use SQLite in a single thread. The routines defined +** here are place-holders. Applications can substitute working +** mutex routines at start-time using the +** +** sqlite3_config(SQLITE_CONFIG_MUTEX,...) +** +** interface. +** +** If compiled with SQLITE_DEBUG, then additional logic is inserted +** that does error checking on mutexes to make sure they are being +** called correctly. +*/ +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_MUTEX_OMIT + +#ifndef SQLITE_DEBUG +/* +** Stub routines for all mutex methods. +** +** This routines provide no mutual exclusion or error checking. +*/ +static int noopMutexInit(void){ return SQLITE_OK; } +static int noopMutexEnd(void){ return SQLITE_OK; } +static sqlite3_mutex *noopMutexAlloc(int id){ + UNUSED_PARAMETER(id); + return (sqlite3_mutex*)8; +} +static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; } +static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; } +static int noopMutexTry(sqlite3_mutex *p){ + UNUSED_PARAMETER(p); + return SQLITE_OK; +} +static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; } + +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){ + static const sqlite3_mutex_methods sMutex = { + noopMutexInit, + noopMutexEnd, + noopMutexAlloc, + noopMutexFree, + noopMutexEnter, + noopMutexTry, + noopMutexLeave, + + 0, + 0, + }; + + return &sMutex; +} +#endif /* !SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* +** In this implementation, error checking is provided for testing +** and debugging purposes. The mutexes still do not provide any +** mutual exclusion. +*/ + +/* +** The mutex object +*/ +typedef struct sqlite3_debug_mutex { + int id; /* The mutex type */ + int cnt; /* Number of entries without a matching leave */ +} sqlite3_debug_mutex; + +/* +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are +** intended for use inside assert() statements. +*/ +static int debugMutexHeld(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + return p==0 || p->cnt>0; +} +static int debugMutexNotheld(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + return p==0 || p->cnt==0; +} + +/* +** Initialize and deinitialize the mutex subsystem. +*/ +static int debugMutexInit(void){ return SQLITE_OK; } +static int debugMutexEnd(void){ return SQLITE_OK; } + +/* +** The sqlite3_mutex_alloc() routine allocates a new +** mutex and returns a pointer to it. If it returns NULL +** that means that a mutex could not be allocated. +*/ +static sqlite3_mutex *debugMutexAlloc(int id){ + static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; + sqlite3_debug_mutex *pNew = 0; + switch( id ){ + case SQLITE_MUTEX_FAST: + case SQLITE_MUTEX_RECURSIVE: { + pNew = sqlite3Malloc(sizeof(*pNew)); + if( pNew ){ + pNew->id = id; + pNew->cnt = 0; + } + break; + } + default: { +#ifdef SQLITE_ENABLE_API_ARMOR + if( id-2<0 || id-2>=ArraySize(aStatic) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + pNew = &aStatic[id-2]; + pNew->id = id; + break; + } + } + return (sqlite3_mutex*)pNew; +} + +/* +** This routine deallocates a previously allocated mutex. +*/ +static void debugMutexFree(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + assert( p->cnt==0 ); + if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ + sqlite3_free(p); + }else{ +#ifdef SQLITE_ENABLE_API_ARMOR + (void)SQLITE_MISUSE_BKPT; +#endif + } +} + +/* +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK +** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can +** be entered multiple times by the same thread. In such cases the, +** mutex must be exited an equal number of times before another thread +** can enter. If the same thread tries to enter any other kind of mutex +** more than once, the behavior is undefined. +*/ +static void debugMutexEnter(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); + p->cnt++; +} +static int debugMutexTry(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); + p->cnt++; + return SQLITE_OK; +} + +/* +** The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. The behavior +** is undefined if the mutex is not currently entered or +** is not currently allocated. SQLite will never do either. +*/ +static void debugMutexLeave(sqlite3_mutex *pX){ + sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX; + assert( debugMutexHeld(pX) ); + p->cnt--; + assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); +} + +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){ + static const sqlite3_mutex_methods sMutex = { + debugMutexInit, + debugMutexEnd, + debugMutexAlloc, + debugMutexFree, + debugMutexEnter, + debugMutexTry, + debugMutexLeave, + + debugMutexHeld, + debugMutexNotheld + }; + + return &sMutex; +} +#endif /* SQLITE_DEBUG */ + +/* +** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation +** is used regardless of the run-time threadsafety setting. +*/ +#ifdef SQLITE_MUTEX_NOOP +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + return sqlite3NoopMutex(); +} +#endif /* defined(SQLITE_MUTEX_NOOP) */ +#endif /* !defined(SQLITE_MUTEX_OMIT) */ + +/************** End of mutex_noop.c ******************************************/ +/************** Begin file mutex_unix.c **************************************/ +/* +** 2007 August 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement mutexes for pthreads +*/ +/* #include "sqliteInt.h" */ + +/* +** The code in this file is only used if we are compiling threadsafe +** under unix with pthreads. +** +** Note that this implementation requires a version of pthreads that +** supports recursive mutexes. +*/ +#ifdef SQLITE_MUTEX_PTHREADS + +#include + +/* +** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields +** are necessary under two condidtions: (1) Debug builds and (2) using +** home-grown mutexes. Encapsulate these conditions into a single #define. +*/ +#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX) +# define SQLITE_MUTEX_NREF 1 +#else +# define SQLITE_MUTEX_NREF 0 +#endif + +/* +** Each recursive mutex is an instance of the following structure. +*/ +struct sqlite3_mutex { + pthread_mutex_t mutex; /* Mutex controlling the lock */ +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) + int id; /* Mutex type */ +#endif +#if SQLITE_MUTEX_NREF + volatile int nRef; /* Number of entrances */ + volatile pthread_t owner; /* Thread that is within this mutex */ + int trace; /* True to trace changes */ +#endif +}; +#if SQLITE_MUTEX_NREF +#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0} +#elif defined(SQLITE_ENABLE_API_ARMOR) +#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 } +#else +#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } +#endif + +/* +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are +** intended for use only inside assert() statements. On some platforms, +** there might be race conditions that can cause these routines to +** deliver incorrect results. In particular, if pthread_equal() is +** not an atomic operation, then these routines might delivery +** incorrect results. On most platforms, pthread_equal() is a +** comparison of two integers and is therefore atomic. But we are +** told that HPUX is not such a platform. If so, then these routines +** will not always work correctly on HPUX. +** +** On those platforms where pthread_equal() is not atomic, SQLite +** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to +** make sure no assert() statements are evaluated and hence these +** routines are never called. +*/ +#if !defined(NDEBUG) || defined(SQLITE_DEBUG) +static int pthreadMutexHeld(sqlite3_mutex *p){ + return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); +} +static int pthreadMutexNotheld(sqlite3_mutex *p){ + return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; +} +#endif + +/* +** Try to provide a memory barrier operation, needed for initialization +** and also for the implementation of xShmBarrier in the VFS in cases +** where SQLite is compiled without mutexes. +*/ +SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ +#if defined(SQLITE_MEMORY_BARRIER) + SQLITE_MEMORY_BARRIER; +#elif defined(__GNUC__) && GCC_VERSION>=4001000 + __sync_synchronize(); +#endif +} + +/* +** Initialize and deinitialize the mutex subsystem. +*/ +static int pthreadMutexInit(void){ return SQLITE_OK; } +static int pthreadMutexEnd(void){ return SQLITE_OK; } + +/* +** The sqlite3_mutex_alloc() routine allocates a new +** mutex and returns a pointer to it. If it returns NULL +** that means that a mutex could not be allocated. SQLite +** will unwind its stack and return an error. The argument +** to sqlite3_mutex_alloc() is one of these integer constants: +** +**
    +**
  • SQLITE_MUTEX_FAST +**
  • SQLITE_MUTEX_RECURSIVE +**
  • SQLITE_MUTEX_STATIC_MASTER +**
  • SQLITE_MUTEX_STATIC_MEM +**
  • SQLITE_MUTEX_STATIC_OPEN +**
  • SQLITE_MUTEX_STATIC_PRNG +**
  • SQLITE_MUTEX_STATIC_LRU +**
  • SQLITE_MUTEX_STATIC_PMEM +**
  • SQLITE_MUTEX_STATIC_APP1 +**
  • SQLITE_MUTEX_STATIC_APP2 +**
  • SQLITE_MUTEX_STATIC_APP3 +**
  • SQLITE_MUTEX_STATIC_VFS1 +**
  • SQLITE_MUTEX_STATIC_VFS2 +**
  • SQLITE_MUTEX_STATIC_VFS3 +**
+** +** The first two constants cause sqlite3_mutex_alloc() to create +** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE +** is used but not necessarily so when SQLITE_MUTEX_FAST is used. +** The mutex implementation does not need to make a distinction +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does +** not want to. But SQLite will only request a recursive mutex in +** cases where it really needs one. If a faster non-recursive mutex +** implementation is available on the host platform, the mutex subsystem +** might return such a mutex in response to SQLITE_MUTEX_FAST. +** +** The other allowed parameters to sqlite3_mutex_alloc() each return +** a pointer to a static preexisting mutex. Six static mutexes are +** used by the current version of SQLite. Future versions of SQLite +** may add additional static mutexes. Static mutexes are for internal +** use by SQLite only. Applications that use SQLite mutexes should +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or +** SQLITE_MUTEX_RECURSIVE. +** +** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() +** returns a different mutex on every call. But for the static +** mutex types, the same mutex is returned on every call that has +** the same type number. +*/ +static sqlite3_mutex *pthreadMutexAlloc(int iType){ + static sqlite3_mutex staticMutexes[] = { + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER + }; + sqlite3_mutex *p; + switch( iType ){ + case SQLITE_MUTEX_RECURSIVE: { + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX + /* If recursive mutexes are not available, we will have to + ** build our own. See below. */ + pthread_mutex_init(&p->mutex, 0); +#else + /* Use a recursive mutex if it is available */ + pthread_mutexattr_t recursiveAttr; + pthread_mutexattr_init(&recursiveAttr); + pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&p->mutex, &recursiveAttr); + pthread_mutexattr_destroy(&recursiveAttr); +#endif + } + break; + } + case SQLITE_MUTEX_FAST: { + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + pthread_mutex_init(&p->mutex, 0); + } + break; + } + default: { +#ifdef SQLITE_ENABLE_API_ARMOR + if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + p = &staticMutexes[iType-2]; + break; + } + } +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) + if( p ) p->id = iType; +#endif + return p; +} + + +/* +** This routine deallocates a previously +** allocated mutex. SQLite is careful to deallocate every +** mutex that it allocates. +*/ +static void pthreadMutexFree(sqlite3_mutex *p){ + assert( p->nRef==0 ); +#if SQLITE_ENABLE_API_ARMOR + if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) +#endif + { + pthread_mutex_destroy(&p->mutex); + sqlite3_free(p); + } +#ifdef SQLITE_ENABLE_API_ARMOR + else{ + (void)SQLITE_MISUSE_BKPT; + } +#endif +} + +/* +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK +** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can +** be entered multiple times by the same thread. In such cases the, +** mutex must be exited an equal number of times before another thread +** can enter. If the same thread tries to enter any other kind of mutex +** more than once, the behavior is undefined. +*/ +static void pthreadMutexEnter(sqlite3_mutex *p){ + assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); + +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX + /* If recursive mutexes are not available, then we have to grow + ** our own. This implementation assumes that pthread_equal() + ** is atomic - that it cannot be deceived into thinking self + ** and p->owner are equal if p->owner changes between two values + ** that are not equal to self while the comparison is taking place. + ** This implementation also assumes a coherent cache - that + ** separate processes cannot read different values from the same + ** address at the same time. If either of these two conditions + ** are not met, then the mutexes will fail and problems will result. + */ + { + pthread_t self = pthread_self(); + if( p->nRef>0 && pthread_equal(p->owner, self) ){ + p->nRef++; + }else{ + pthread_mutex_lock(&p->mutex); + assert( p->nRef==0 ); + p->owner = self; + p->nRef = 1; + } + } +#else + /* Use the built-in recursive mutexes if they are available. + */ + pthread_mutex_lock(&p->mutex); +#if SQLITE_MUTEX_NREF + assert( p->nRef>0 || p->owner==0 ); + p->owner = pthread_self(); + p->nRef++; +#endif +#endif + +#ifdef SQLITE_DEBUG + if( p->trace ){ + printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); + } +#endif +} +static int pthreadMutexTry(sqlite3_mutex *p){ + int rc; + assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); + +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX + /* If recursive mutexes are not available, then we have to grow + ** our own. This implementation assumes that pthread_equal() + ** is atomic - that it cannot be deceived into thinking self + ** and p->owner are equal if p->owner changes between two values + ** that are not equal to self while the comparison is taking place. + ** This implementation also assumes a coherent cache - that + ** separate processes cannot read different values from the same + ** address at the same time. If either of these two conditions + ** are not met, then the mutexes will fail and problems will result. + */ + { + pthread_t self = pthread_self(); + if( p->nRef>0 && pthread_equal(p->owner, self) ){ + p->nRef++; + rc = SQLITE_OK; + }else if( pthread_mutex_trylock(&p->mutex)==0 ){ + assert( p->nRef==0 ); + p->owner = self; + p->nRef = 1; + rc = SQLITE_OK; + }else{ + rc = SQLITE_BUSY; + } + } +#else + /* Use the built-in recursive mutexes if they are available. + */ + if( pthread_mutex_trylock(&p->mutex)==0 ){ +#if SQLITE_MUTEX_NREF + p->owner = pthread_self(); + p->nRef++; +#endif + rc = SQLITE_OK; + }else{ + rc = SQLITE_BUSY; + } +#endif + +#ifdef SQLITE_DEBUG + if( rc==SQLITE_OK && p->trace ){ + printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); + } +#endif + return rc; +} + +/* +** The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. The behavior +** is undefined if the mutex is not currently entered or +** is not currently allocated. SQLite will never do either. +*/ +static void pthreadMutexLeave(sqlite3_mutex *p){ + assert( pthreadMutexHeld(p) ); +#if SQLITE_MUTEX_NREF + p->nRef--; + if( p->nRef==0 ) p->owner = 0; +#endif + assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); + +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX + if( p->nRef==0 ){ + pthread_mutex_unlock(&p->mutex); + } +#else + pthread_mutex_unlock(&p->mutex); +#endif + +#ifdef SQLITE_DEBUG + if( p->trace ){ + printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); + } +#endif +} + +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + static const sqlite3_mutex_methods sMutex = { + pthreadMutexInit, + pthreadMutexEnd, + pthreadMutexAlloc, + pthreadMutexFree, + pthreadMutexEnter, + pthreadMutexTry, + pthreadMutexLeave, +#ifdef SQLITE_DEBUG + pthreadMutexHeld, + pthreadMutexNotheld +#else + 0, + 0 +#endif + }; + + return &sMutex; +} + +#endif /* SQLITE_MUTEX_PTHREADS */ + +/************** End of mutex_unix.c ******************************************/ +/************** Begin file mutex_w32.c ***************************************/ +/* +** 2007 August 14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement mutexes for Win32. +*/ +/* #include "sqliteInt.h" */ + +#if SQLITE_OS_WIN +/* +** Include code that is common to all os_*.c files +*/ +/************** Include os_common.h in the middle of mutex_w32.c *************/ +/************** Begin file os_common.h ***************************************/ +/* +** 2004 May 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains macros and a little bit of code that is common to +** all of the platform-specific files (os_*.c) and is #included into those +** files. +** +** This file should be #included by the os_*.c files only. It is not a +** general purpose header file. +*/ +#ifndef _OS_COMMON_H_ +#define _OS_COMMON_H_ + +/* +** At least two bugs have slipped in because we changed the MEMORY_DEBUG +** macro to SQLITE_DEBUG and some older makefiles have not yet made the +** switch. The following code should catch this problem at compile-time. +*/ +#ifdef MEMORY_DEBUG +# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." +#endif + +/* +** Macros for performance tracing. Normally turned off. Only works +** on i486 hardware. +*/ +#ifdef SQLITE_PERFORMANCE_TRACE + +/* +** hwtime.h contains inline assembler code for implementing +** high-performance timing routines. +*/ +/************** Include hwtime.h in the middle of os_common.h ****************/ +/************** Begin file hwtime.h ******************************************/ +/* +** 2008 May 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains inline asm code for retrieving "high-performance" +** counters for x86 class CPUs. +*/ +#ifndef _HWTIME_H_ +#define _HWTIME_H_ + +/* +** The following routine only works on pentium-class (or newer) processors. +** It uses the RDTSC opcode to read the cycle count value out of the +** processor and returns that value. This can be used for high-res +** profiling. +*/ +#if (defined(__GNUC__) || defined(_MSC_VER)) && \ + (defined(i386) || defined(__i386__) || defined(_M_IX86)) + + #if defined(__GNUC__) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned int lo, hi; + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return (sqlite_uint64)hi << 32 | lo; + } + + #elif defined(_MSC_VER) + + __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ + __asm { + rdtsc + ret ; return value at EDX:EAX + } + } + + #endif + +#elif (defined(__GNUC__) && defined(__x86_64__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long val; + __asm__ __volatile__ ("rdtsc" : "=A" (val)); + return val; + } + +#elif (defined(__GNUC__) && defined(__ppc__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long long retval; + unsigned long junk; + __asm__ __volatile__ ("\n\ + 1: mftbu %1\n\ + mftb %L0\n\ + mftbu %0\n\ + cmpw %0,%1\n\ + bne 1b" + : "=r" (retval), "=r" (junk)); + return retval; + } + +#else + + #error Need implementation of sqlite3Hwtime() for your platform. + + /* + ** To compile without implementing sqlite3Hwtime() for your platform, + ** you can remove the above #error and use the following + ** stub function. You will lose timing support for many + ** of the debugging and testing utilities, but it should at + ** least compile and run. + */ +SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + +#endif + +#endif /* !defined(_HWTIME_H_) */ + +/************** End of hwtime.h **********************************************/ +/************** Continuing where we left off in os_common.h ******************/ + +static sqlite_uint64 g_start; +static sqlite_uint64 g_elapsed; +#define TIMER_START g_start=sqlite3Hwtime() +#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start +#define TIMER_ELAPSED g_elapsed +#else +#define TIMER_START +#define TIMER_END +#define TIMER_ELAPSED ((sqlite_uint64)0) +#endif + +/* +** If we compile with the SQLITE_TEST macro set, then the following block +** of code will give us the ability to simulate a disk I/O error. This +** is used for testing the I/O recovery logic. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_io_error_hit; +SQLITE_API extern int sqlite3_io_error_hardhit; +SQLITE_API extern int sqlite3_io_error_pending; +SQLITE_API extern int sqlite3_io_error_persist; +SQLITE_API extern int sqlite3_io_error_benign; +SQLITE_API extern int sqlite3_diskfull_pending; +SQLITE_API extern int sqlite3_diskfull; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) +#define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } +static void local_ioerr(){ + IOTRACE(("IOERR\n")); + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; +} +#define SimulateDiskfullError(CODE) \ + if( sqlite3_diskfull_pending ){ \ + if( sqlite3_diskfull_pending == 1 ){ \ + local_ioerr(); \ + sqlite3_diskfull = 1; \ + sqlite3_io_error_hit = 1; \ + CODE; \ + }else{ \ + sqlite3_diskfull_pending--; \ + } \ + } +#else +#define SimulateIOErrorBenign(X) +#define SimulateIOError(A) +#define SimulateDiskfullError(A) +#endif /* defined(SQLITE_TEST) */ + +/* +** When testing, keep a count of the number of open files. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_open_file_count; +#define OpenCounter(X) sqlite3_open_file_count+=(X) +#else +#define OpenCounter(X) +#endif /* defined(SQLITE_TEST) */ + +#endif /* !defined(_OS_COMMON_H_) */ + +/************** End of os_common.h *******************************************/ +/************** Continuing where we left off in mutex_w32.c ******************/ + +/* +** Include the header file for the Windows VFS. +*/ +/************** Include os_win.h in the middle of mutex_w32.c ****************/ +/************** Begin file os_win.h ******************************************/ +/* +** 2013 November 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code that is specific to Windows. +*/ +#ifndef _OS_WIN_H_ +#define _OS_WIN_H_ + +/* +** Include the primary Windows SDK header file. +*/ +#include "windows.h" + +#ifdef __CYGWIN__ +# include +# include /* amalgamator: dontcache */ +#endif + +/* +** Determine if we are dealing with Windows NT. +** +** We ought to be able to determine if we are compiling for Windows 9x or +** Windows NT using the _WIN32_WINNT macro as follows: +** +** #if defined(_WIN32_WINNT) +** # define SQLITE_OS_WINNT 1 +** #else +** # define SQLITE_OS_WINNT 0 +** #endif +** +** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as +** it ought to, so the above test does not work. We'll just assume that +** everything is Windows NT unless the programmer explicitly says otherwise +** by setting SQLITE_OS_WINNT to 0. +*/ +#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT) +# define SQLITE_OS_WINNT 1 +#endif + +/* +** Determine if we are dealing with Windows CE - which has a much reduced +** API. +*/ +#if defined(_WIN32_WCE) +# define SQLITE_OS_WINCE 1 +#else +# define SQLITE_OS_WINCE 0 +#endif + +/* +** Determine if we are dealing with WinRT, which provides only a subset of +** the full Win32 API. +*/ +#if !defined(SQLITE_OS_WINRT) +# define SQLITE_OS_WINRT 0 +#endif + +/* +** For WinCE, some API function parameters do not appear to be declared as +** volatile. +*/ +#if SQLITE_OS_WINCE +# define SQLITE_WIN32_VOLATILE +#else +# define SQLITE_WIN32_VOLATILE volatile +#endif + +/* +** For some Windows sub-platforms, the _beginthreadex() / _endthreadex() +** functions are not available (e.g. those not using MSVC, Cygwin, etc). +*/ +#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ + SQLITE_THREADSAFE>0 && !defined(__CYGWIN__) +# define SQLITE_OS_WIN_THREADS 1 +#else +# define SQLITE_OS_WIN_THREADS 0 +#endif + +#endif /* _OS_WIN_H_ */ + +/************** End of os_win.h **********************************************/ +/************** Continuing where we left off in mutex_w32.c ******************/ +#endif + +/* +** The code in this file is only used if we are compiling multithreaded +** on a Win32 system. +*/ +#ifdef SQLITE_MUTEX_W32 + +/* +** Each recursive mutex is an instance of the following structure. +*/ +struct sqlite3_mutex { + CRITICAL_SECTION mutex; /* Mutex controlling the lock */ + int id; /* Mutex type */ +#ifdef SQLITE_DEBUG + volatile int nRef; /* Number of enterances */ + volatile DWORD owner; /* Thread holding this mutex */ + volatile int trace; /* True to trace changes */ +#endif +}; + +/* +** These are the initializer values used when declaring a "static" mutex +** on Win32. It should be noted that all mutexes require initialization +** on the Win32 platform. +*/ +#define SQLITE_W32_MUTEX_INITIALIZER { 0 } + +#ifdef SQLITE_DEBUG +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \ + 0L, (DWORD)0, 0 } +#else +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } +#endif + +#ifdef SQLITE_DEBUG +/* +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are +** intended for use only inside assert() statements. +*/ +static int winMutexHeld(sqlite3_mutex *p){ + return p->nRef!=0 && p->owner==GetCurrentThreadId(); +} + +static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){ + return p->nRef==0 || p->owner!=tid; +} + +static int winMutexNotheld(sqlite3_mutex *p){ + DWORD tid = GetCurrentThreadId(); + return winMutexNotheld2(p, tid); +} +#endif + +/* +** Try to provide a memory barrier operation, needed for initialization +** and also for the xShmBarrier method of the VFS in cases when SQLite is +** compiled without mutexes (SQLITE_THREADSAFE=0). +*/ +SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ +#if defined(SQLITE_MEMORY_BARRIER) + SQLITE_MEMORY_BARRIER; +#elif defined(__GNUC__) + __sync_synchronize(); +#elif !defined(SQLITE_DISABLE_INTRINSIC) && \ + defined(_MSC_VER) && _MSC_VER>=1300 + _ReadWriteBarrier(); +#elif defined(MemoryBarrier) + MemoryBarrier(); +#endif +} + +/* +** Initialize and deinitialize the mutex subsystem. +*/ +static sqlite3_mutex winMutex_staticMutexes[] = { + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER, + SQLITE3_MUTEX_INITIALIZER +}; + +static int winMutex_isInit = 0; +static int winMutex_isNt = -1; /* <0 means "need to query" */ + +/* As the winMutexInit() and winMutexEnd() functions are called as part +** of the sqlite3_initialize() and sqlite3_shutdown() processing, the +** "interlocked" magic used here is probably not strictly necessary. +*/ +static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0; + +SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void); /* os_win.c */ +SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ + +static int winMutexInit(void){ + /* The first to increment to 1 does actual initialization */ + if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){ + int i; + for(i=0; i +**
  • SQLITE_MUTEX_FAST +**
  • SQLITE_MUTEX_RECURSIVE +**
  • SQLITE_MUTEX_STATIC_MASTER +**
  • SQLITE_MUTEX_STATIC_MEM +**
  • SQLITE_MUTEX_STATIC_OPEN +**
  • SQLITE_MUTEX_STATIC_PRNG +**
  • SQLITE_MUTEX_STATIC_LRU +**
  • SQLITE_MUTEX_STATIC_PMEM +**
  • SQLITE_MUTEX_STATIC_APP1 +**
  • SQLITE_MUTEX_STATIC_APP2 +**
  • SQLITE_MUTEX_STATIC_APP3 +**
  • SQLITE_MUTEX_STATIC_VFS1 +**
  • SQLITE_MUTEX_STATIC_VFS2 +**
  • SQLITE_MUTEX_STATIC_VFS3 +** +** +** The first two constants cause sqlite3_mutex_alloc() to create +** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE +** is used but not necessarily so when SQLITE_MUTEX_FAST is used. +** The mutex implementation does not need to make a distinction +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does +** not want to. But SQLite will only request a recursive mutex in +** cases where it really needs one. If a faster non-recursive mutex +** implementation is available on the host platform, the mutex subsystem +** might return such a mutex in response to SQLITE_MUTEX_FAST. +** +** The other allowed parameters to sqlite3_mutex_alloc() each return +** a pointer to a static preexisting mutex. Six static mutexes are +** used by the current version of SQLite. Future versions of SQLite +** may add additional static mutexes. Static mutexes are for internal +** use by SQLite only. Applications that use SQLite mutexes should +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or +** SQLITE_MUTEX_RECURSIVE. +** +** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() +** returns a different mutex on every call. But for the static +** mutex types, the same mutex is returned on every call that has +** the same type number. +*/ +static sqlite3_mutex *winMutexAlloc(int iType){ + sqlite3_mutex *p; + + switch( iType ){ + case SQLITE_MUTEX_FAST: + case SQLITE_MUTEX_RECURSIVE: { + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + p->id = iType; +#ifdef SQLITE_DEBUG +#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC + p->trace = 1; +#endif +#endif +#if SQLITE_OS_WINRT + InitializeCriticalSectionEx(&p->mutex, 0, 0); +#else + InitializeCriticalSection(&p->mutex); +#endif + } + break; + } + default: { +#ifdef SQLITE_ENABLE_API_ARMOR + if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + p = &winMutex_staticMutexes[iType-2]; + p->id = iType; +#ifdef SQLITE_DEBUG +#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC + p->trace = 1; +#endif +#endif + break; + } + } + return p; +} + + +/* +** This routine deallocates a previously +** allocated mutex. SQLite is careful to deallocate every +** mutex that it allocates. +*/ +static void winMutexFree(sqlite3_mutex *p){ + assert( p ); + assert( p->nRef==0 && p->owner==0 ); + if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ + DeleteCriticalSection(&p->mutex); + sqlite3_free(p); + }else{ +#ifdef SQLITE_ENABLE_API_ARMOR + (void)SQLITE_MISUSE_BKPT; +#endif + } +} + +/* +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK +** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can +** be entered multiple times by the same thread. In such cases the, +** mutex must be exited an equal number of times before another thread +** can enter. If the same thread tries to enter any other kind of mutex +** more than once, the behavior is undefined. +*/ +static void winMutexEnter(sqlite3_mutex *p){ +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + DWORD tid = GetCurrentThreadId(); +#endif +#ifdef SQLITE_DEBUG + assert( p ); + assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); +#else + assert( p ); +#endif + assert( winMutex_isInit==1 ); + EnterCriticalSection(&p->mutex); +#ifdef SQLITE_DEBUG + assert( p->nRef>0 || p->owner==0 ); + p->owner = tid; + p->nRef++; + if( p->trace ){ + OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", + tid, p, p->trace, p->nRef)); + } +#endif +} + +static int winMutexTry(sqlite3_mutex *p){ +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + DWORD tid = GetCurrentThreadId(); +#endif + int rc = SQLITE_BUSY; + assert( p ); + assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); + /* + ** The sqlite3_mutex_try() routine is very rarely used, and when it + ** is used it is merely an optimization. So it is OK for it to always + ** fail. + ** + ** The TryEnterCriticalSection() interface is only available on WinNT. + ** And some windows compilers complain if you try to use it without + ** first doing some #defines that prevent SQLite from building on Win98. + ** For that reason, we will omit this optimization for now. See + ** ticket #2685. + */ +#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 + assert( winMutex_isInit==1 ); + assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); + if( winMutex_isNt<0 ){ + winMutex_isNt = sqlite3_win32_is_nt(); + } + assert( winMutex_isNt==0 || winMutex_isNt==1 ); + if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ +#ifdef SQLITE_DEBUG + p->owner = tid; + p->nRef++; +#endif + rc = SQLITE_OK; + } +#else + UNUSED_PARAMETER(p); +#endif +#ifdef SQLITE_DEBUG + if( p->trace ){ + OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", + tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); + } +#endif + return rc; +} + +/* +** The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. The behavior +** is undefined if the mutex is not currently entered or +** is not currently allocated. SQLite will never do either. +*/ +static void winMutexLeave(sqlite3_mutex *p){ +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + DWORD tid = GetCurrentThreadId(); +#endif + assert( p ); +#ifdef SQLITE_DEBUG + assert( p->nRef>0 ); + assert( p->owner==tid ); + p->nRef--; + if( p->nRef==0 ) p->owner = 0; + assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); +#endif + assert( winMutex_isInit==1 ); + LeaveCriticalSection(&p->mutex); +#ifdef SQLITE_DEBUG + if( p->trace ){ + OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", + tid, p, p->trace, p->nRef)); + } +#endif +} + +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + static const sqlite3_mutex_methods sMutex = { + winMutexInit, + winMutexEnd, + winMutexAlloc, + winMutexFree, + winMutexEnter, + winMutexTry, + winMutexLeave, +#ifdef SQLITE_DEBUG + winMutexHeld, + winMutexNotheld +#else + 0, + 0 +#endif + }; + return &sMutex; +} + +#endif /* SQLITE_MUTEX_W32 */ + +/************** End of mutex_w32.c *******************************************/ +/************** Begin file malloc.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** Memory allocation functions used throughout sqlite. +*/ +/* #include "sqliteInt.h" */ +/* #include */ + +/* +** Attempt to release up to n bytes of non-essential memory currently +** held by SQLite. An example of non-essential memory is memory used to +** cache database pages that are not currently in use. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + return sqlite3PcacheReleaseMemory(n); +#else + /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine + ** is a no-op returning zero if SQLite is not compiled with + ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ + UNUSED_PARAMETER(n); + return 0; +#endif +} + +/* +** An instance of the following object records the location of +** each unused scratch buffer. +*/ +typedef struct ScratchFreeslot { + struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ +} ScratchFreeslot; + +/* +** State information local to the memory allocation subsystem. +*/ +static SQLITE_WSD struct Mem0Global { + sqlite3_mutex *mutex; /* Mutex to serialize access */ + sqlite3_int64 alarmThreshold; /* The soft heap limit */ + + /* + ** Pointers to the end of sqlite3GlobalConfig.pScratch memory + ** (so that a range test can be used to determine if an allocation + ** being freed came from pScratch) and a pointer to the list of + ** unused scratch allocations. + */ + void *pScratchEnd; + ScratchFreeslot *pScratchFree; + u32 nScratchFree; + + /* + ** True if heap is nearly "full" where "full" is defined by the + ** sqlite3_soft_heap_limit() setting. + */ + int nearlyFull; +} mem0 = { 0, 0, 0, 0, 0, 0 }; + +#define mem0 GLOBAL(struct Mem0Global, mem0) + +/* +** Return the memory allocator mutex. sqlite3_status() needs it. +*/ +SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){ + return mem0.mutex; +} + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** Deprecated external interface. It used to set an alarm callback +** that was invoked when memory usage grew too large. Now it is a +** no-op. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm( + void(*xCallback)(void *pArg, sqlite3_int64 used,int N), + void *pArg, + sqlite3_int64 iThreshold +){ + (void)xCallback; + (void)pArg; + (void)iThreshold; + return SQLITE_OK; +} +#endif + +/* +** Set the soft heap-size limit for the library. Passing a zero or +** negative value indicates no limit. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 n){ + sqlite3_int64 priorLimit; + sqlite3_int64 excess; + sqlite3_int64 nUsed; +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return -1; +#endif + sqlite3_mutex_enter(mem0.mutex); + priorLimit = mem0.alarmThreshold; + if( n<0 ){ + sqlite3_mutex_leave(mem0.mutex); + return priorLimit; + } + mem0.alarmThreshold = n; + nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); + mem0.nearlyFull = (n>0 && n<=nUsed); + sqlite3_mutex_leave(mem0.mutex); + excess = sqlite3_memory_used() - n; + if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); + return priorLimit; +} +SQLITE_API void SQLITE_STDCALL sqlite3_soft_heap_limit(int n){ + if( n<0 ) n = 0; + sqlite3_soft_heap_limit64(n); +} + +/* +** Initialize the memory allocation subsystem. +*/ +SQLITE_PRIVATE int sqlite3MallocInit(void){ + int rc; + if( sqlite3GlobalConfig.m.xMalloc==0 ){ + sqlite3MemSetDefault(); + } + memset(&mem0, 0, sizeof(mem0)); + mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); + if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 + && sqlite3GlobalConfig.nScratch>0 ){ + int i, n, sz; + ScratchFreeslot *pSlot; + sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); + sqlite3GlobalConfig.szScratch = sz; + pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; + n = sqlite3GlobalConfig.nScratch; + mem0.pScratchFree = pSlot; + mem0.nScratchFree = n; + for(i=0; ipNext = (ScratchFreeslot*)(sz+(char*)pSlot); + pSlot = pSlot->pNext; + } + pSlot->pNext = 0; + mem0.pScratchEnd = (void*)&pSlot[1]; + }else{ + mem0.pScratchEnd = 0; + sqlite3GlobalConfig.pScratch = 0; + sqlite3GlobalConfig.szScratch = 0; + sqlite3GlobalConfig.nScratch = 0; + } + if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 + || sqlite3GlobalConfig.nPage<=0 ){ + sqlite3GlobalConfig.pPage = 0; + sqlite3GlobalConfig.szPage = 0; + } + rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); + if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); + return rc; +} + +/* +** Return true if the heap is currently under memory pressure - in other +** words if the amount of heap used is close to the limit set by +** sqlite3_soft_heap_limit(). +*/ +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ + return mem0.nearlyFull; +} + +/* +** Deinitialize the memory allocation subsystem. +*/ +SQLITE_PRIVATE void sqlite3MallocEnd(void){ + if( sqlite3GlobalConfig.m.xShutdown ){ + sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); + } + memset(&mem0, 0, sizeof(mem0)); +} + +/* +** Return the amount of memory currently checked out. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){ + sqlite3_int64 res, mx; + sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); + return res; +} + +/* +** Return the maximum amount of memory that has ever been +** checked out since either the beginning of this process +** or since the most recent reset. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){ + sqlite3_int64 res, mx; + sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); + return mx; +} + +/* +** Trigger the alarm +*/ +static void sqlite3MallocAlarm(int nByte){ + if( mem0.alarmThreshold<=0 ) return; + sqlite3_mutex_leave(mem0.mutex); + sqlite3_release_memory(nByte); + sqlite3_mutex_enter(mem0.mutex); +} + +/* +** Do a memory allocation with statistics and alarms. Assume the +** lock is already held. +*/ +static int mallocWithAlarm(int n, void **pp){ + int nFull; + void *p; + assert( sqlite3_mutex_held(mem0.mutex) ); + nFull = sqlite3GlobalConfig.m.xRoundup(n); + sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); + if( mem0.alarmThreshold>0 ){ + sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); + if( nUsed >= mem0.alarmThreshold - nFull ){ + mem0.nearlyFull = 1; + sqlite3MallocAlarm(nFull); + }else{ + mem0.nearlyFull = 0; + } + } + p = sqlite3GlobalConfig.m.xMalloc(nFull); +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + if( p==0 && mem0.alarmThreshold>0 ){ + sqlite3MallocAlarm(nFull); + p = sqlite3GlobalConfig.m.xMalloc(nFull); + } +#endif + if( p ){ + nFull = sqlite3MallocSize(p); + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); + sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); + } + *pp = p; + return nFull; +} + +/* +** Allocate memory. This routine is like sqlite3_malloc() except that it +** assumes the memory subsystem has already been initialized. +*/ +SQLITE_PRIVATE void *sqlite3Malloc(u64 n){ + void *p; + if( n==0 || n>=0x7fffff00 ){ + /* A memory allocation of a number of bytes which is near the maximum + ** signed integer value might cause an integer overflow inside of the + ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving + ** 255 bytes of overhead. SQLite itself will never use anything near + ** this amount. The only way to reach the limit is with sqlite3_malloc() */ + p = 0; + }else if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_mutex_enter(mem0.mutex); + mallocWithAlarm((int)n, &p); + sqlite3_mutex_leave(mem0.mutex); + }else{ + p = sqlite3GlobalConfig.m.xMalloc((int)n); + } + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ + return p; +} + +/* +** This version of the memory allocation is for use by the application. +** First make sure the memory subsystem is initialized, then do the +** allocation. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int n){ +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return n<=0 ? 0 : sqlite3Malloc(n); +} +SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64 n){ +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return sqlite3Malloc(n); +} + +/* +** Each thread may only have a single outstanding allocation from +** xScratchMalloc(). We verify this constraint in the single-threaded +** case by setting scratchAllocOut to 1 when an allocation +** is outstanding clearing it when the allocation is freed. +*/ +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) +static int scratchAllocOut = 0; +#endif + + +/* +** Allocate memory that is to be used and released right away. +** This routine is similar to alloca() in that it is not intended +** for situations where the memory might be held long-term. This +** routine is intended to get memory to old large transient data +** structures that would not normally fit on the stack of an +** embedded processor. +*/ +SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ + void *p; + assert( n>0 ); + + sqlite3_mutex_enter(mem0.mutex); + sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n); + if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ + p = mem0.pScratchFree; + mem0.pScratchFree = mem0.pScratchFree->pNext; + mem0.nScratchFree--; + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); + sqlite3_mutex_leave(mem0.mutex); + }else{ + sqlite3_mutex_leave(mem0.mutex); + p = sqlite3Malloc(n); + if( sqlite3GlobalConfig.bMemstat && p ){ + sqlite3_mutex_enter(mem0.mutex); + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); + sqlite3_mutex_leave(mem0.mutex); + } + sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); + } + assert( sqlite3_mutex_notheld(mem0.mutex) ); + + +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) + /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch + ** buffers per thread. + ** + ** This can only be checked in single-threaded mode. + */ + assert( scratchAllocOut==0 ); + if( p ) scratchAllocOut++; +#endif + + return p; +} +SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ + if( p ){ + +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) + /* Verify that no more than two scratch allocation per thread + ** is outstanding at one time. (This is only checked in the + ** single-threaded case since checking in the multi-threaded case + ** would be much more complicated.) */ + assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); + scratchAllocOut--; +#endif + + if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ + /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ + ScratchFreeslot *pSlot; + pSlot = (ScratchFreeslot*)p; + sqlite3_mutex_enter(mem0.mutex); + pSlot->pNext = mem0.pScratchFree; + mem0.pScratchFree = pSlot; + mem0.nScratchFree++; + assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); + sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); + sqlite3_mutex_leave(mem0.mutex); + }else{ + /* Release memory back to the heap */ + assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + if( sqlite3GlobalConfig.bMemstat ){ + int iSize = sqlite3MallocSize(p); + sqlite3_mutex_enter(mem0.mutex); + sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); + sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); + sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); + sqlite3GlobalConfig.m.xFree(p); + sqlite3_mutex_leave(mem0.mutex); + }else{ + sqlite3GlobalConfig.m.xFree(p); + } + } + } +} + +/* +** TRUE if p is a lookaside memory allocation from db +*/ +#ifndef SQLITE_OMIT_LOOKASIDE +static int isLookaside(sqlite3 *db, void *p){ + return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); +} +#else +#define isLookaside(A,B) 0 +#endif + +/* +** Return the size of a memory allocation previously obtained from +** sqlite3Malloc() or sqlite3_malloc(). +*/ +SQLITE_PRIVATE int sqlite3MallocSize(void *p){ + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + return sqlite3GlobalConfig.m.xSize(p); +} +SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ + assert( p!=0 ); + if( db==0 || !isLookaside(db,p) ){ +#if SQLITE_DEBUG + if( db==0 ){ + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + }else{ + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + } +#endif + return sqlite3GlobalConfig.m.xSize(p); + }else{ + assert( sqlite3_mutex_held(db->mutex) ); + return db->lookaside.sz; + } +} +SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void *p){ + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + return p ? sqlite3GlobalConfig.m.xSize(p) : 0; +} + +/* +** Free memory previously obtained from sqlite3Malloc(). +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_free(void *p){ + if( p==0 ) return; /* IMP: R-49053-54554 */ + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); + if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_mutex_enter(mem0.mutex); + sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); + sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); + sqlite3GlobalConfig.m.xFree(p); + sqlite3_mutex_leave(mem0.mutex); + }else{ + sqlite3GlobalConfig.m.xFree(p); + } +} + +/* +** Add the size of memory allocation "p" to the count in +** *db->pnBytesFreed. +*/ +static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ + *db->pnBytesFreed += sqlite3DbMallocSize(db,p); +} + +/* +** Free memory that might be associated with a particular database +** connection. +*/ +SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ + assert( db==0 || sqlite3_mutex_held(db->mutex) ); + if( p==0 ) return; + if( db ){ + if( db->pnBytesFreed ){ + measureAllocationSize(db, p); + return; + } + if( isLookaside(db, p) ){ + LookasideSlot *pBuf = (LookasideSlot*)p; +#if SQLITE_DEBUG + /* Trash all content in the buffer being freed */ + memset(p, 0xaa, db->lookaside.sz); +#endif + pBuf->pNext = db->lookaside.pFree; + db->lookaside.pFree = pBuf; + db->lookaside.nOut--; + return; + } + } + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + sqlite3_free(p); +} + +/* +** Change the size of an existing memory allocation +*/ +SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ + int nOld, nNew, nDiff; + void *pNew; + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); + assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); + if( pOld==0 ){ + return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ + } + if( nBytes==0 ){ + sqlite3_free(pOld); /* IMP: R-26507-47431 */ + return 0; + } + if( nBytes>=0x7fffff00 ){ + /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ + return 0; + } + nOld = sqlite3MallocSize(pOld); + /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second + ** argument to xRealloc is always a value returned by a prior call to + ** xRoundup. */ + nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); + if( nOld==nNew ){ + pNew = pOld; + }else if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_mutex_enter(mem0.mutex); + sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); + nDiff = nNew - nOld; + if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= + mem0.alarmThreshold-nDiff ){ + sqlite3MallocAlarm(nDiff); + } + pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); + if( pNew==0 && mem0.alarmThreshold>0 ){ + sqlite3MallocAlarm((int)nBytes); + pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); + } + if( pNew ){ + nNew = sqlite3MallocSize(pNew); + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); + } + sqlite3_mutex_leave(mem0.mutex); + }else{ + pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); + } + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ + return pNew; +} + +/* +** The public interface to sqlite3Realloc. Make sure that the memory +** subsystem is initialized prior to invoking sqliteRealloc. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void *pOld, int n){ +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + if( n<0 ) n = 0; /* IMP: R-26507-47431 */ + return sqlite3Realloc(pOld, n); +} +SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return sqlite3Realloc(pOld, n); +} + + +/* +** Allocate and zero memory. +*/ +SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){ + void *p = sqlite3Malloc(n); + if( p ){ + memset(p, 0, (size_t)n); + } + return p; +} + +/* +** Allocate and zero memory. If the allocation fails, make +** the mallocFailed flag in the connection pointer. +*/ +SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ + void *p; + testcase( db==0 ); + p = sqlite3DbMallocRaw(db, n); + if( p ) memset(p, 0, (size_t)n); + return p; +} + + +/* Finish the work of sqlite3DbMallocRawNN for the unusual and +** slower case when the allocation cannot be fulfilled using lookaside. +*/ +static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){ + void *p; + assert( db!=0 ); + p = sqlite3Malloc(n); + if( !p ) sqlite3OomFault(db); + sqlite3MemdebugSetType(p, + (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); + return p; +} + +/* +** Allocate memory, either lookaside (if possible) or heap. +** If the allocation fails, set the mallocFailed flag in +** the connection pointer. +** +** If db!=0 and db->mallocFailed is true (indicating a prior malloc +** failure on the same database connection) then always return 0. +** Hence for a particular database connection, once malloc starts +** failing, it fails consistently until mallocFailed is reset. +** This is an important assumption. There are many places in the +** code that do things like this: +** +** int *a = (int*)sqlite3DbMallocRaw(db, 100); +** int *b = (int*)sqlite3DbMallocRaw(db, 200); +** if( b ) a[10] = 9; +** +** In other words, if a subsequent malloc (ex: "b") worked, it is assumed +** that all prior mallocs (ex: "a") worked too. +** +** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is +** not a NULL pointer. +*/ +SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ + void *p; + if( db ) return sqlite3DbMallocRawNN(db, n); + p = sqlite3Malloc(n); + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + return p; +} +SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ +#ifndef SQLITE_OMIT_LOOKASIDE + LookasideSlot *pBuf; + assert( db!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( db->pnBytesFreed==0 ); + if( db->lookaside.bDisable==0 ){ + assert( db->mallocFailed==0 ); + if( n>db->lookaside.sz ){ + db->lookaside.anStat[1]++; + }else if( (pBuf = db->lookaside.pFree)==0 ){ + db->lookaside.anStat[2]++; + }else{ + db->lookaside.pFree = pBuf->pNext; + db->lookaside.nOut++; + db->lookaside.anStat[0]++; + if( db->lookaside.nOut>db->lookaside.mxOut ){ + db->lookaside.mxOut = db->lookaside.nOut; + } + return (void*)pBuf; + } + }else if( db->mallocFailed ){ + return 0; + } +#else + assert( db!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( db->pnBytesFreed==0 ); + if( db->mallocFailed ){ + return 0; + } +#endif + return dbMallocRawFinish(db, n); +} + +/* Forward declaration */ +static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n); + +/* +** Resize the block of memory pointed to by p to n bytes. If the +** resize fails, set the mallocFailed flag in the connection object. +*/ +SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ + assert( db!=0 ); + if( p==0 ) return sqlite3DbMallocRawNN(db, n); + assert( sqlite3_mutex_held(db->mutex) ); + if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; + return dbReallocFinish(db, p, n); +} +static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ + void *pNew = 0; + assert( db!=0 ); + assert( p!=0 ); + if( db->mallocFailed==0 ){ + if( isLookaside(db, p) ){ + pNew = sqlite3DbMallocRawNN(db, n); + if( pNew ){ + memcpy(pNew, p, db->lookaside.sz); + sqlite3DbFree(db, p); + } + }else{ + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + pNew = sqlite3_realloc64(p, n); + if( !pNew ){ + sqlite3OomFault(db); + } + sqlite3MemdebugSetType(pNew, + (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); + } + } + return pNew; +} + +/* +** Attempt to reallocate p. If the reallocation fails, then free p +** and set the mallocFailed flag in the database connection. +*/ +SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ + void *pNew; + pNew = sqlite3DbRealloc(db, p, n); + if( !pNew ){ + sqlite3DbFree(db, p); + } + return pNew; +} + +/* +** Make a copy of a string in memory obtained from sqliteMalloc(). These +** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This +** is because when memory debugging is turned on, these two functions are +** called via macros that record the current file and line number in the +** ThreadData structure. +*/ +SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){ + char *zNew; + size_t n; + if( z==0 ){ + return 0; + } + n = sqlite3Strlen30(z) + 1; + assert( (n&0x7fffffff)==n ); + zNew = sqlite3DbMallocRaw(db, (int)n); + if( zNew ){ + memcpy(zNew, z, n); + } + return zNew; +} +SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ + char *zNew; + assert( db!=0 ); + if( z==0 ){ + return 0; + } + assert( (n&0x7fffffff)==n ); + zNew = sqlite3DbMallocRawNN(db, n+1); + if( zNew ){ + memcpy(zNew, z, (size_t)n); + zNew[n] = 0; + } + return zNew; +} + +/* +** Free any prior content in *pz and replace it with a copy of zNew. +*/ +SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ + sqlite3DbFree(db, *pz); + *pz = sqlite3DbStrDup(db, zNew); +} + +/* +** Call this routine to record the fact that an OOM (out-of-memory) error +** has happened. This routine will set db->mallocFailed, and also +** temporarily disable the lookaside memory allocator and interrupt +** any running VDBEs. +*/ +SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){ + if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ + db->mallocFailed = 1; + if( db->nVdbeExec>0 ){ + db->u1.isInterrupted = 1; + } + db->lookaside.bDisable++; + } +} + +/* +** This routine reactivates the memory allocator and clears the +** db->mallocFailed flag as necessary. +** +** The memory allocator is not restarted if there are running +** VDBEs. +*/ +SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){ + if( db->mallocFailed && db->nVdbeExec==0 ){ + db->mallocFailed = 0; + db->u1.isInterrupted = 0; + assert( db->lookaside.bDisable>0 ); + db->lookaside.bDisable--; + } +} + +/* +** Take actions at the end of an API call to indicate an OOM error +*/ +static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ + sqlite3OomClear(db); + sqlite3Error(db, SQLITE_NOMEM); + return SQLITE_NOMEM_BKPT; +} + +/* +** This function must be called before exiting any API function (i.e. +** returning control to the user) that has called sqlite3_malloc or +** sqlite3_realloc. +** +** The returned value is normally a copy of the second argument to this +** function. However, if a malloc() failure has occurred since the previous +** invocation SQLITE_NOMEM is returned instead. +** +** If an OOM as occurred, then the connection error-code (the value +** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. +*/ +SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ + /* If the db handle must hold the connection handle mutex here. + ** Otherwise the read (and possible write) of db->mallocFailed + ** is unsafe, as is the call to sqlite3Error(). + */ + assert( db!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ + return apiOomError(db); + } + return rc & db->errMask; +} + +/************** End of malloc.c **********************************************/ +/************** Begin file printf.c ******************************************/ +/* +** The "printf" code that follows dates from the 1980's. It is in +** the public domain. +** +************************************************************************** +** +** This file contains code for a set of "printf"-like routines. These +** routines format strings much like the printf() from the standard C +** library, though the implementation here has enhancements to support +** SQLite. +*/ +/* #include "sqliteInt.h" */ + +/* +** Conversion types fall into various categories as defined by the +** following enumeration. +*/ +#define etRADIX 0 /* Integer types. %d, %x, %o, and so forth */ +#define etFLOAT 1 /* Floating point. %f */ +#define etEXP 2 /* Exponentional notation. %e and %E */ +#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ +#define etSIZE 4 /* Return number of characters processed so far. %n */ +#define etSTRING 5 /* Strings. %s */ +#define etDYNSTRING 6 /* Dynamically allocated strings. %z */ +#define etPERCENT 7 /* Percent symbol. %% */ +#define etCHARX 8 /* Characters. %c */ +/* The rest are extensions, not normally found in printf() */ +#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ +#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', + NULL pointers replaced by SQL NULL. %Q */ +#define etTOKEN 11 /* a pointer to a Token structure */ +#define etSRCLIST 12 /* a pointer to a SrcList */ +#define etPOINTER 13 /* The %p conversion */ +#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ +#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ + +#define etINVALID 16 /* Any unrecognized conversion type */ + + +/* +** An "etByte" is an 8-bit unsigned value. +*/ +typedef unsigned char etByte; + +/* +** Each builtin conversion character (ex: the 'd' in "%d") is described +** by an instance of the following structure +*/ +typedef struct et_info { /* Information about each format field */ + char fmttype; /* The format field code letter */ + etByte base; /* The base for radix conversion */ + etByte flags; /* One or more of FLAG_ constants below */ + etByte type; /* Conversion paradigm */ + etByte charset; /* Offset into aDigits[] of the digits string */ + etByte prefix; /* Offset into aPrefix[] of the prefix string */ +} et_info; + +/* +** Allowed values for et_info.flags +*/ +#define FLAG_SIGNED 1 /* True if the value to convert is signed */ +#define FLAG_INTERN 2 /* True if for internal use only */ +#define FLAG_STRING 4 /* Allow infinity precision */ + + +/* +** The following table is searched linearly, so it is good to put the +** most frequently used conversion types first. +*/ +static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; +static const char aPrefix[] = "-x0\000X0"; +static const et_info fmtinfo[] = { + { 'd', 10, 1, etRADIX, 0, 0 }, + { 's', 0, 4, etSTRING, 0, 0 }, + { 'g', 0, 1, etGENERIC, 30, 0 }, + { 'z', 0, 4, etDYNSTRING, 0, 0 }, + { 'q', 0, 4, etSQLESCAPE, 0, 0 }, + { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, + { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, + { 'c', 0, 0, etCHARX, 0, 0 }, + { 'o', 8, 0, etRADIX, 0, 2 }, + { 'u', 10, 0, etRADIX, 0, 0 }, + { 'x', 16, 0, etRADIX, 16, 1 }, + { 'X', 16, 0, etRADIX, 0, 4 }, +#ifndef SQLITE_OMIT_FLOATING_POINT + { 'f', 0, 1, etFLOAT, 0, 0 }, + { 'e', 0, 1, etEXP, 30, 0 }, + { 'E', 0, 1, etEXP, 14, 0 }, + { 'G', 0, 1, etGENERIC, 14, 0 }, +#endif + { 'i', 10, 1, etRADIX, 0, 0 }, + { 'n', 0, 0, etSIZE, 0, 0 }, + { '%', 0, 0, etPERCENT, 0, 0 }, + { 'p', 16, 0, etPOINTER, 0, 1 }, + +/* All the rest have the FLAG_INTERN bit set and are thus for internal +** use only */ + { 'T', 0, 2, etTOKEN, 0, 0 }, + { 'S', 0, 2, etSRCLIST, 0, 0 }, + { 'r', 10, 3, etORDINAL, 0, 0 }, +}; + +/* +** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point +** conversions will work. +*/ +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** "*val" is a double such that 0.1 <= *val < 10.0 +** Return the ascii code for the leading digit of *val, then +** multiply "*val" by 10.0 to renormalize. +** +** Example: +** input: *val = 3.14159 +** output: *val = 1.4159 function return = '3' +** +** The counter *cnt is incremented each time. After counter exceeds +** 16 (the number of significant digits in a 64-bit float) '0' is +** always returned. +*/ +static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ + int digit; + LONGDOUBLE_TYPE d; + if( (*cnt)<=0 ) return '0'; + (*cnt)--; + digit = (int)*val; + d = digit; + digit += '0'; + *val = (*val - d)*10.0; + return (char)digit; +} +#endif /* SQLITE_OMIT_FLOATING_POINT */ + +/* +** Set the StrAccum object to an error mode. +*/ +static void setStrAccumError(StrAccum *p, u8 eError){ + assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); + p->accError = eError; + p->nAlloc = 0; +} + +/* +** Extra argument values from a PrintfArguments object +*/ +static sqlite3_int64 getIntArg(PrintfArguments *p){ + if( p->nArg<=p->nUsed ) return 0; + return sqlite3_value_int64(p->apArg[p->nUsed++]); +} +static double getDoubleArg(PrintfArguments *p){ + if( p->nArg<=p->nUsed ) return 0.0; + return sqlite3_value_double(p->apArg[p->nUsed++]); +} +static char *getTextArg(PrintfArguments *p){ + if( p->nArg<=p->nUsed ) return 0; + return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); +} + + +/* +** On machines with a small stack size, you can redefine the +** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. +*/ +#ifndef SQLITE_PRINT_BUF_SIZE +# define SQLITE_PRINT_BUF_SIZE 70 +#endif +#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ + +/* +** Render a string given by "fmt" into the StrAccum object. +*/ +SQLITE_PRIVATE void sqlite3VXPrintf( + StrAccum *pAccum, /* Accumulate results here */ + const char *fmt, /* Format string */ + va_list ap /* arguments */ +){ + int c; /* Next character in the format string */ + char *bufpt; /* Pointer to the conversion buffer */ + int precision; /* Precision of the current field */ + int length; /* Length of the field */ + int idx; /* A general purpose loop counter */ + int width; /* Width of the current field */ + etByte flag_leftjustify; /* True if "-" flag is present */ + etByte flag_plussign; /* True if "+" flag is present */ + etByte flag_blanksign; /* True if " " flag is present */ + etByte flag_alternateform; /* True if "#" flag is present */ + etByte flag_altform2; /* True if "!" flag is present */ + etByte flag_zeropad; /* True if field width constant starts with zero */ + etByte flag_long; /* True if "l" flag is present */ + etByte flag_longlong; /* True if the "ll" flag is present */ + etByte done; /* Loop termination flag */ + etByte xtype = etINVALID; /* Conversion paradigm */ + u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ + u8 useIntern; /* Ok to use internal conversions (ex: %T) */ + char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ + sqlite_uint64 longvalue; /* Value for integer types */ + LONGDOUBLE_TYPE realvalue; /* Value for real types */ + const et_info *infop; /* Pointer to the appropriate info structure */ + char *zOut; /* Rendering buffer */ + int nOut; /* Size of the rendering buffer */ + char *zExtra = 0; /* Malloced memory used by some conversion */ +#ifndef SQLITE_OMIT_FLOATING_POINT + int exp, e2; /* exponent of real numbers */ + int nsd; /* Number of significant digits returned */ + double rounder; /* Used for rounding floating point values */ + etByte flag_dp; /* True if decimal point should be shown */ + etByte flag_rtz; /* True if trailing zeros should be removed */ +#endif + PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ + char buf[etBUFSIZE]; /* Conversion buffer */ + + bufpt = 0; + if( pAccum->printfFlags ){ + if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ + pArgList = va_arg(ap, PrintfArguments*); + } + useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL; + }else{ + bArgList = useIntern = 0; + } + for(; (c=(*fmt))!=0; ++fmt){ + if( c!='%' ){ + bufpt = (char *)fmt; +#if HAVE_STRCHRNUL + fmt = strchrnul(fmt, '%'); +#else + do{ fmt++; }while( *fmt && *fmt != '%' ); +#endif + sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); + if( *fmt==0 ) break; + } + if( (c=(*++fmt))==0 ){ + sqlite3StrAccumAppend(pAccum, "%", 1); + break; + } + /* Find out what flags are present */ + flag_leftjustify = flag_plussign = flag_blanksign = + flag_alternateform = flag_altform2 = flag_zeropad = 0; + done = 0; + do{ + switch( c ){ + case '-': flag_leftjustify = 1; break; + case '+': flag_plussign = 1; break; + case ' ': flag_blanksign = 1; break; + case '#': flag_alternateform = 1; break; + case '!': flag_altform2 = 1; break; + case '0': flag_zeropad = 1; break; + default: done = 1; break; + } + }while( !done && (c=(*++fmt))!=0 ); + /* Get the field width */ + if( c=='*' ){ + if( bArgList ){ + width = (int)getIntArg(pArgList); + }else{ + width = va_arg(ap,int); + } + if( width<0 ){ + flag_leftjustify = 1; + width = width >= -2147483647 ? -width : 0; + } + c = *++fmt; + }else{ + unsigned wx = 0; + while( c>='0' && c<='9' ){ + wx = wx*10 + c - '0'; + c = *++fmt; + } + testcase( wx>0x7fffffff ); + width = wx & 0x7fffffff; + } + assert( width>=0 ); +#ifdef SQLITE_PRINTF_PRECISION_LIMIT + if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ + width = SQLITE_PRINTF_PRECISION_LIMIT; + } +#endif + + /* Get the precision */ + if( c=='.' ){ + c = *++fmt; + if( c=='*' ){ + if( bArgList ){ + precision = (int)getIntArg(pArgList); + }else{ + precision = va_arg(ap,int); + } + c = *++fmt; + if( precision<0 ){ + precision = precision >= -2147483647 ? -precision : -1; + } + }else{ + unsigned px = 0; + while( c>='0' && c<='9' ){ + px = px*10 + c - '0'; + c = *++fmt; + } + testcase( px>0x7fffffff ); + precision = px & 0x7fffffff; + } + }else{ + precision = -1; + } + assert( precision>=(-1) ); +#ifdef SQLITE_PRINTF_PRECISION_LIMIT + if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ + precision = SQLITE_PRINTF_PRECISION_LIMIT; + } +#endif + + + /* Get the conversion type modifier */ + if( c=='l' ){ + flag_long = 1; + c = *++fmt; + if( c=='l' ){ + flag_longlong = 1; + c = *++fmt; + }else{ + flag_longlong = 0; + } + }else{ + flag_long = flag_longlong = 0; + } + /* Fetch the info entry for the field */ + infop = &fmtinfo[0]; + xtype = etINVALID; + for(idx=0; idxflags & FLAG_INTERN)==0 ){ + xtype = infop->type; + }else{ + return; + } + break; + } + } + + /* + ** At this point, variables are initialized as follows: + ** + ** flag_alternateform TRUE if a '#' is present. + ** flag_altform2 TRUE if a '!' is present. + ** flag_plussign TRUE if a '+' is present. + ** flag_leftjustify TRUE if a '-' is present or if the + ** field width was negative. + ** flag_zeropad TRUE if the width began with 0. + ** flag_long TRUE if the letter 'l' (ell) prefixed + ** the conversion character. + ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed + ** the conversion character. + ** flag_blanksign TRUE if a ' ' is present. + ** width The specified field width. This is + ** always non-negative. Zero is the default. + ** precision The specified precision. The default + ** is -1. + ** xtype The class of the conversion. + ** infop Pointer to the appropriate info struct. + */ + switch( xtype ){ + case etPOINTER: + flag_longlong = sizeof(char*)==sizeof(i64); + flag_long = sizeof(char*)==sizeof(long int); + /* Fall through into the next case */ + case etORDINAL: + case etRADIX: + if( infop->flags & FLAG_SIGNED ){ + i64 v; + if( bArgList ){ + v = getIntArg(pArgList); + }else if( flag_longlong ){ + v = va_arg(ap,i64); + }else if( flag_long ){ + v = va_arg(ap,long int); + }else{ + v = va_arg(ap,int); + } + if( v<0 ){ + if( v==SMALLEST_INT64 ){ + longvalue = ((u64)1)<<63; + }else{ + longvalue = -v; + } + prefix = '-'; + }else{ + longvalue = v; + if( flag_plussign ) prefix = '+'; + else if( flag_blanksign ) prefix = ' '; + else prefix = 0; + } + }else{ + if( bArgList ){ + longvalue = (u64)getIntArg(pArgList); + }else if( flag_longlong ){ + longvalue = va_arg(ap,u64); + }else if( flag_long ){ + longvalue = va_arg(ap,unsigned long int); + }else{ + longvalue = va_arg(ap,unsigned int); + } + prefix = 0; + } + if( longvalue==0 ) flag_alternateform = 0; + if( flag_zeropad && precision=4 || (longvalue/10)%10==1 ){ + x = 0; + } + *(--bufpt) = zOrd[x*2+1]; + *(--bufpt) = zOrd[x*2]; + } + { + const char *cset = &aDigits[infop->charset]; + u8 base = infop->base; + do{ /* Convert to ascii */ + *(--bufpt) = cset[longvalue%base]; + longvalue = longvalue/base; + }while( longvalue>0 ); + } + length = (int)(&zOut[nOut-1]-bufpt); + for(idx=precision-length; idx>0; idx--){ + *(--bufpt) = '0'; /* Zero pad */ + } + if( prefix ) *(--bufpt) = prefix; /* Add sign */ + if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ + const char *pre; + char x; + pre = &aPrefix[infop->prefix]; + for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; + } + length = (int)(&zOut[nOut-1]-bufpt); + break; + case etFLOAT: + case etEXP: + case etGENERIC: + if( bArgList ){ + realvalue = getDoubleArg(pArgList); + }else{ + realvalue = va_arg(ap,double); + } +#ifdef SQLITE_OMIT_FLOATING_POINT + length = 0; +#else + if( precision<0 ) precision = 6; /* Set default precision */ + if( realvalue<0.0 ){ + realvalue = -realvalue; + prefix = '-'; + }else{ + if( flag_plussign ) prefix = '+'; + else if( flag_blanksign ) prefix = ' '; + else prefix = 0; + } + if( xtype==etGENERIC && precision>0 ) precision--; + testcase( precision>0xfff ); + for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} + if( xtype==etFLOAT ) realvalue += rounder; + /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ + exp = 0; + if( sqlite3IsNaN((double)realvalue) ){ + bufpt = "NaN"; + length = 3; + break; + } + if( realvalue>0.0 ){ + LONGDOUBLE_TYPE scale = 1.0; + while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} + while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } + while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } + realvalue /= scale; + while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } + while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } + if( exp>350 ){ + bufpt = buf; + buf[0] = prefix; + memcpy(buf+(prefix!=0),"Inf",4); + length = 3+(prefix!=0); + break; + } + } + bufpt = buf; + /* + ** If the field type is etGENERIC, then convert to either etEXP + ** or etFLOAT, as appropriate. + */ + if( xtype!=etFLOAT ){ + realvalue += rounder; + if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } + } + if( xtype==etGENERIC ){ + flag_rtz = !flag_alternateform; + if( exp<-4 || exp>precision ){ + xtype = etEXP; + }else{ + precision = precision - exp; + xtype = etFLOAT; + } + }else{ + flag_rtz = flag_altform2; + } + if( xtype==etEXP ){ + e2 = 0; + }else{ + e2 = exp; + } + if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ + bufpt = zExtra + = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); + if( bufpt==0 ){ + setStrAccumError(pAccum, STRACCUM_NOMEM); + return; + } + } + zOut = bufpt; + nsd = 16 + flag_altform2*10; + flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; + /* The sign in front of the number */ + if( prefix ){ + *(bufpt++) = prefix; + } + /* Digits prior to the decimal point */ + if( e2<0 ){ + *(bufpt++) = '0'; + }else{ + for(; e2>=0; e2--){ + *(bufpt++) = et_getdigit(&realvalue,&nsd); + } + } + /* The decimal point */ + if( flag_dp ){ + *(bufpt++) = '.'; + } + /* "0" digits after the decimal point but before the first + ** significant digit of the number */ + for(e2++; e2<0; precision--, e2++){ + assert( precision>0 ); + *(bufpt++) = '0'; + } + /* Significant digits after the decimal point */ + while( (precision--)>0 ){ + *(bufpt++) = et_getdigit(&realvalue,&nsd); + } + /* Remove trailing zeros and the "." if no digits follow the "." */ + if( flag_rtz && flag_dp ){ + while( bufpt[-1]=='0' ) *(--bufpt) = 0; + assert( bufpt>zOut ); + if( bufpt[-1]=='.' ){ + if( flag_altform2 ){ + *(bufpt++) = '0'; + }else{ + *(--bufpt) = 0; + } + } + } + /* Add the "eNNN" suffix */ + if( xtype==etEXP ){ + *(bufpt++) = aDigits[infop->charset]; + if( exp<0 ){ + *(bufpt++) = '-'; exp = -exp; + }else{ + *(bufpt++) = '+'; + } + if( exp>=100 ){ + *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ + exp %= 100; + } + *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ + *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ + } + *bufpt = 0; + + /* The converted number is in buf[] and zero terminated. Output it. + ** Note that the number is in the usual order, not reversed as with + ** integer conversions. */ + length = (int)(bufpt-zOut); + bufpt = zOut; + + /* Special case: Add leading zeros if the flag_zeropad flag is + ** set and we are not left justified */ + if( flag_zeropad && !flag_leftjustify && length < width){ + int i; + int nPad = width - length; + for(i=width; i>=nPad; i--){ + bufpt[i] = bufpt[i-nPad]; + } + i = prefix!=0; + while( nPad-- ) bufpt[i++] = '0'; + length = width; + } +#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ + break; + case etSIZE: + if( !bArgList ){ + *(va_arg(ap,int*)) = pAccum->nChar; + } + length = width = 0; + break; + case etPERCENT: + buf[0] = '%'; + bufpt = buf; + length = 1; + break; + case etCHARX: + if( bArgList ){ + bufpt = getTextArg(pArgList); + c = bufpt ? bufpt[0] : 0; + }else{ + c = va_arg(ap,int); + } + if( precision>1 ){ + width -= precision-1; + if( width>1 && !flag_leftjustify ){ + sqlite3AppendChar(pAccum, width-1, ' '); + width = 0; + } + sqlite3AppendChar(pAccum, precision-1, c); + } + length = 1; + buf[0] = c; + bufpt = buf; + break; + case etSTRING: + case etDYNSTRING: + if( bArgList ){ + bufpt = getTextArg(pArgList); + xtype = etSTRING; + }else{ + bufpt = va_arg(ap,char*); + } + if( bufpt==0 ){ + bufpt = ""; + }else if( xtype==etDYNSTRING ){ + zExtra = bufpt; + } + if( precision>=0 ){ + for(length=0; lengthetBUFSIZE ){ + bufpt = zExtra = sqlite3Malloc( n ); + if( bufpt==0 ){ + setStrAccumError(pAccum, STRACCUM_NOMEM); + return; + } + }else{ + bufpt = buf; + } + j = 0; + if( needQuote ) bufpt[j++] = q; + k = i; + for(i=0; i=0 && precisionn ){ + sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); + } + length = width = 0; + break; + } + case etSRCLIST: { + SrcList *pSrc = va_arg(ap, SrcList*); + int k = va_arg(ap, int); + struct SrcList_item *pItem = &pSrc->a[k]; + assert( bArgList==0 ); + assert( k>=0 && knSrc ); + if( pItem->zDatabase ){ + sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); + sqlite3StrAccumAppend(pAccum, ".", 1); + } + sqlite3StrAccumAppendAll(pAccum, pItem->zName); + length = width = 0; + break; + } + default: { + assert( xtype==etINVALID ); + return; + } + }/* End switch over the format type */ + /* + ** The text of the conversion is pointed to by "bufpt" and is + ** "length" characters long. The field width is "width". Do + ** the output. + */ + width -= length; + if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); + sqlite3StrAccumAppend(pAccum, bufpt, length); + if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); + + if( zExtra ){ + sqlite3DbFree(pAccum->db, zExtra); + zExtra = 0; + } + }/* End for loop over the format string */ +} /* End of function */ + +/* +** Enlarge the memory allocation on a StrAccum object so that it is +** able to accept at least N more bytes of text. +** +** Return the number of bytes of text that StrAccum is able to accept +** after the attempted enlargement. The value returned might be zero. +*/ +static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ + char *zNew; + assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ + if( p->accError ){ + testcase(p->accError==STRACCUM_TOOBIG); + testcase(p->accError==STRACCUM_NOMEM); + return 0; + } + if( p->mxAlloc==0 ){ + N = p->nAlloc - p->nChar - 1; + setStrAccumError(p, STRACCUM_TOOBIG); + return N; + }else{ + char *zOld = isMalloced(p) ? p->zText : 0; + i64 szNew = p->nChar; + assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); + szNew += N + 1; + if( szNew+p->nChar<=p->mxAlloc ){ + /* Force exponential buffer size growth as long as it does not overflow, + ** to avoid having to call this routine too often */ + szNew += p->nChar; + } + if( szNew > p->mxAlloc ){ + sqlite3StrAccumReset(p); + setStrAccumError(p, STRACCUM_TOOBIG); + return 0; + }else{ + p->nAlloc = (int)szNew; + } + if( p->db ){ + zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); + }else{ + zNew = sqlite3_realloc64(zOld, p->nAlloc); + } + if( zNew ){ + assert( p->zText!=0 || p->nChar==0 ); + if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); + p->zText = zNew; + p->nAlloc = sqlite3DbMallocSize(p->db, zNew); + p->printfFlags |= SQLITE_PRINTF_MALLOCED; + }else{ + sqlite3StrAccumReset(p); + setStrAccumError(p, STRACCUM_NOMEM); + return 0; + } + } + return N; +} + +/* +** Append N copies of character c to the given string buffer. +*/ +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ + testcase( p->nChar + (i64)N > 0x7fffffff ); + if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ + return; + } + assert( (p->zText==p->zBase)==!isMalloced(p) ); + while( (N--)>0 ) p->zText[p->nChar++] = c; +} + +/* +** The StrAccum "p" is not large enough to accept N new bytes of z[]. +** So enlarge if first, then do the append. +** +** This is a helper routine to sqlite3StrAccumAppend() that does special-case +** work (enlarging the buffer) using tail recursion, so that the +** sqlite3StrAccumAppend() routine can use fast calling semantics. +*/ +static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ + N = sqlite3StrAccumEnlarge(p, N); + if( N>0 ){ + memcpy(&p->zText[p->nChar], z, N); + p->nChar += N; + } + assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); +} + +/* +** Append N bytes of text from z to the StrAccum object. Increase the +** size of the memory allocation for StrAccum if necessary. +*/ +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ + assert( z!=0 || N==0 ); + assert( p->zText!=0 || p->nChar==0 || p->accError ); + assert( N>=0 ); + assert( p->accError==0 || p->nAlloc==0 ); + if( p->nChar+N >= p->nAlloc ){ + enlargeAndAppend(p,z,N); + }else{ + assert( p->zText ); + p->nChar += N; + memcpy(&p->zText[p->nChar-N], z, N); + } +} + +/* +** Append the complete text of zero-terminated string z[] to the p string. +*/ +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ + sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); +} + + +/* +** Finish off a string by making sure it is zero-terminated. +** Return a pointer to the resulting string. Return a NULL +** pointer if any kind of error was encountered. +*/ +SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ + if( p->zText ){ + assert( (p->zText==p->zBase)==!isMalloced(p) ); + p->zText[p->nChar] = 0; + if( p->mxAlloc>0 && !isMalloced(p) ){ + p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); + if( p->zText ){ + memcpy(p->zText, p->zBase, p->nChar+1); + p->printfFlags |= SQLITE_PRINTF_MALLOCED; + }else{ + setStrAccumError(p, STRACCUM_NOMEM); + } + } + } + return p->zText; +} + +/* +** Reset an StrAccum string. Reclaim all malloced memory. +*/ +SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ + assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); + if( isMalloced(p) ){ + sqlite3DbFree(p->db, p->zText); + p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; + } + p->zText = 0; +} + +/* +** Initialize a string accumulator. +** +** p: The accumulator to be initialized. +** db: Pointer to a database connection. May be NULL. Lookaside +** memory is used if not NULL. db->mallocFailed is set appropriately +** when not NULL. +** zBase: An initial buffer. May be NULL in which case the initial buffer +** is malloced. +** n: Size of zBase in bytes. If total space requirements never exceed +** n then no memory allocations ever occur. +** mx: Maximum number of bytes to accumulate. If mx==0 then no memory +** allocations will ever occur. +*/ +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ + p->zText = p->zBase = zBase; + p->db = db; + p->nChar = 0; + p->nAlloc = n; + p->mxAlloc = mx; + p->accError = 0; + p->printfFlags = 0; +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. +*/ +SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ + char *z; + char zBase[SQLITE_PRINT_BUF_SIZE]; + StrAccum acc; + assert( db!=0 ); + sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), + db->aLimit[SQLITE_LIMIT_LENGTH]); + acc.printfFlags = SQLITE_PRINTF_INTERNAL; + sqlite3VXPrintf(&acc, zFormat, ap); + z = sqlite3StrAccumFinish(&acc); + if( acc.accError==STRACCUM_NOMEM ){ + sqlite3OomFault(db); + } + return z; +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. +*/ +SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ + va_list ap; + char *z; + va_start(ap, zFormat); + z = sqlite3VMPrintf(db, zFormat, ap); + va_end(ap); + return z; +} + +/* +** Print into memory obtained from sqlite3_malloc(). Omit the internal +** %-conversion extensions. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){ + char *z; + char zBase[SQLITE_PRINT_BUF_SIZE]; + StrAccum acc; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( zFormat==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); + sqlite3VXPrintf(&acc, zFormat, ap); + z = sqlite3StrAccumFinish(&acc); + return z; +} + +/* +** Print into memory obtained from sqlite3_malloc()(). Omit the internal +** %-conversion extensions. +*/ +SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){ + va_list ap; + char *z; +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + return z; +} + +/* +** sqlite3_snprintf() works like snprintf() except that it ignores the +** current locale settings. This is important for SQLite because we +** are not able to use a "," as the decimal point in place of "." as +** specified by some locales. +** +** Oops: The first two arguments of sqlite3_snprintf() are backwards +** from the snprintf() standard. Unfortunately, it is too late to change +** this without breaking compatibility, so we just have to live with the +** mistake. +** +** sqlite3_vsnprintf() is the varargs version. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ + StrAccum acc; + if( n<=0 ) return zBuf; +#ifdef SQLITE_ENABLE_API_ARMOR + if( zBuf==0 || zFormat==0 ) { + (void)SQLITE_MISUSE_BKPT; + if( zBuf ) zBuf[0] = 0; + return zBuf; + } +#endif + sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); + sqlite3VXPrintf(&acc, zFormat, ap); + return sqlite3StrAccumFinish(&acc); +} +SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ + char *z; + va_list ap; + va_start(ap,zFormat); + z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); + va_end(ap); + return z; +} + +/* +** This is the routine that actually formats the sqlite3_log() message. +** We house it in a separate routine from sqlite3_log() to avoid using +** stack space on small-stack systems when logging is disabled. +** +** sqlite3_log() must render into a static buffer. It cannot dynamically +** allocate memory because it might be called while the memory allocator +** mutex is held. +** +** sqlite3VXPrintf() might ask for *temporary* memory allocations for +** certain format characters (%q) or for very large precisions or widths. +** Care must be taken that any sqlite3_log() calls that occur while the +** memory mutex is held do not use these mechanisms. +*/ +static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ + StrAccum acc; /* String accumulator */ + char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ + + sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); + sqlite3VXPrintf(&acc, zFormat, ap); + sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, + sqlite3StrAccumFinish(&acc)); +} + +/* +** Format and write a message to the log if logging is enabled. +*/ +SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...){ + va_list ap; /* Vararg list */ + if( sqlite3GlobalConfig.xLog ){ + va_start(ap, zFormat); + renderLogMsg(iErrCode, zFormat, ap); + va_end(ap); + } +} + +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) +/* +** A version of printf() that understands %lld. Used for debugging. +** The printf() built into some versions of windows does not understand %lld +** and segfaults if you give it a long long int. +*/ +SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ + va_list ap; + StrAccum acc; + char zBuf[500]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + va_start(ap,zFormat); + sqlite3VXPrintf(&acc, zFormat, ap); + va_end(ap); + sqlite3StrAccumFinish(&acc); + fprintf(stdout,"%s", zBuf); + fflush(stdout); +} +#endif + + +/* +** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument +** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. +*/ +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ + va_list ap; + va_start(ap,zFormat); + sqlite3VXPrintf(p, zFormat, ap); + va_end(ap); +} + +/************** End of printf.c **********************************************/ +/************** Begin file treeview.c ****************************************/ +/* +** 2015-06-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains C code to implement the TreeView debugging routines. +** These routines print a parse tree to standard output for debugging and +** analysis. +** +** The interfaces in this file is only available when compiling +** with SQLITE_DEBUG. +*/ +/* #include "sqliteInt.h" */ +#ifdef SQLITE_DEBUG + +/* +** Add a new subitem to the tree. The moreToFollow flag indicates that this +** is not the last item in the tree. +*/ +static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ + if( p==0 ){ + p = sqlite3_malloc64( sizeof(*p) ); + if( p==0 ) return 0; + memset(p, 0, sizeof(*p)); + }else{ + p->iLevel++; + } + assert( moreToFollow==0 || moreToFollow==1 ); + if( p->iLevelbLine) ) p->bLine[p->iLevel] = moreToFollow; + return p; +} + +/* +** Finished with one layer of the tree +*/ +static void sqlite3TreeViewPop(TreeView *p){ + if( p==0 ) return; + p->iLevel--; + if( p->iLevel<0 ) sqlite3_free(p); +} + +/* +** Generate a single line of output for the tree, with a prefix that contains +** all the appropriate tree lines +*/ +static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ + va_list ap; + int i; + StrAccum acc; + char zBuf[500]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + if( p ){ + for(i=0; iiLevel && ibLine)-1; i++){ + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); + } + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); + } + va_start(ap, zFormat); + sqlite3VXPrintf(&acc, zFormat, ap); + va_end(ap); + if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); + sqlite3StrAccumFinish(&acc); + fprintf(stdout,"%s", zBuf); + fflush(stdout); +} + +/* +** Shorthand for starting a new tree item that consists of a single label +*/ +static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ + p = sqlite3TreeViewPush(p, moreFollows); + sqlite3TreeViewLine(p, "%s", zLabel); +} + +/* +** Generate a human-readable description of a WITH clause. +*/ +SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){ + int i; + if( pWith==0 ) return; + if( pWith->nCte==0 ) return; + if( pWith->pOuter ){ + sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); + }else{ + sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); + } + if( pWith->nCte>0 ){ + pView = sqlite3TreeViewPush(pView, 1); + for(i=0; inCte; i++){ + StrAccum x; + char zLine[1000]; + const struct Cte *pCte = &pWith->a[i]; + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); + sqlite3XPrintf(&x, "%s", pCte->zName); + if( pCte->pCols && pCte->pCols->nExpr>0 ){ + char cSep = '('; + int j; + for(j=0; jpCols->nExpr; j++){ + sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName); + cSep = ','; + } + sqlite3XPrintf(&x, ")"); + } + sqlite3XPrintf(&x, " AS"); + sqlite3StrAccumFinish(&x); + sqlite3TreeViewItem(pView, zLine, inCte-1); + sqlite3TreeViewSelect(pView, pCte->pSelect, 0); + sqlite3TreeViewPop(pView); + } + sqlite3TreeViewPop(pView); + } +} + + +/* +** Generate a human-readable description of a the Select object. +*/ +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ + int n = 0; + int cnt = 0; + pView = sqlite3TreeViewPush(pView, moreToFollow); + if( p->pWith ){ + sqlite3TreeViewWith(pView, p->pWith, 1); + cnt = 1; + sqlite3TreeViewPush(pView, 1); + } + do{ + sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d", + ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), + ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags, + (int)p->nSelectRow + ); + if( cnt++ ) sqlite3TreeViewPop(pView); + if( p->pPrior ){ + n = 1000; + }else{ + n = 0; + if( p->pSrc && p->pSrc->nSrc ) n++; + if( p->pWhere ) n++; + if( p->pGroupBy ) n++; + if( p->pHaving ) n++; + if( p->pOrderBy ) n++; + if( p->pLimit ) n++; + if( p->pOffset ) n++; + } + sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); + if( p->pSrc && p->pSrc->nSrc ){ + int i; + pView = sqlite3TreeViewPush(pView, (n--)>0); + sqlite3TreeViewLine(pView, "FROM"); + for(i=0; ipSrc->nSrc; i++){ + struct SrcList_item *pItem = &p->pSrc->a[i]; + StrAccum x; + char zLine[100]; + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); + sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor); + if( pItem->zDatabase ){ + sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName); + }else if( pItem->zName ){ + sqlite3XPrintf(&x, " %s", pItem->zName); + } + if( pItem->pTab ){ + sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName); + } + if( pItem->zAlias ){ + sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias); + } + if( pItem->fg.jointype & JT_LEFT ){ + sqlite3XPrintf(&x, " LEFT-JOIN"); + } + sqlite3StrAccumFinish(&x); + sqlite3TreeViewItem(pView, zLine, ipSrc->nSrc-1); + if( pItem->pSelect ){ + sqlite3TreeViewSelect(pView, pItem->pSelect, 0); + } + if( pItem->fg.isTabFunc ){ + sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); + } + sqlite3TreeViewPop(pView); + } + sqlite3TreeViewPop(pView); + } + if( p->pWhere ){ + sqlite3TreeViewItem(pView, "WHERE", (n--)>0); + sqlite3TreeViewExpr(pView, p->pWhere, 0); + sqlite3TreeViewPop(pView); + } + if( p->pGroupBy ){ + sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); + } + if( p->pHaving ){ + sqlite3TreeViewItem(pView, "HAVING", (n--)>0); + sqlite3TreeViewExpr(pView, p->pHaving, 0); + sqlite3TreeViewPop(pView); + } + if( p->pOrderBy ){ + sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); + } + if( p->pLimit ){ + sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); + sqlite3TreeViewExpr(pView, p->pLimit, 0); + sqlite3TreeViewPop(pView); + } + if( p->pOffset ){ + sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); + sqlite3TreeViewExpr(pView, p->pOffset, 0); + sqlite3TreeViewPop(pView); + } + if( p->pPrior ){ + const char *zOp = "UNION"; + switch( p->op ){ + case TK_ALL: zOp = "UNION ALL"; break; + case TK_INTERSECT: zOp = "INTERSECT"; break; + case TK_EXCEPT: zOp = "EXCEPT"; break; + } + sqlite3TreeViewItem(pView, zOp, 1); + } + p = p->pPrior; + }while( p!=0 ); + sqlite3TreeViewPop(pView); +} + +/* +** Generate a human-readable explanation of an expression tree. +*/ +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ + const char *zBinOp = 0; /* Binary operator */ + const char *zUniOp = 0; /* Unary operator */ + char zFlgs[30]; + pView = sqlite3TreeViewPush(pView, moreToFollow); + if( pExpr==0 ){ + sqlite3TreeViewLine(pView, "nil"); + sqlite3TreeViewPop(pView); + return; + } + if( pExpr->flags ){ + sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); + }else{ + zFlgs[0] = 0; + } + switch( pExpr->op ){ + case TK_AGG_COLUMN: { + sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", + pExpr->iTable, pExpr->iColumn, zFlgs); + break; + } + case TK_COLUMN: { + if( pExpr->iTable<0 ){ + /* This only happens when coding check constraints */ + sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); + }else{ + sqlite3TreeViewLine(pView, "{%d:%d}%s", + pExpr->iTable, pExpr->iColumn, zFlgs); + } + break; + } + case TK_INTEGER: { + if( pExpr->flags & EP_IntValue ){ + sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); + }else{ + sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); + } + break; + } +#ifndef SQLITE_OMIT_FLOATING_POINT + case TK_FLOAT: { + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); + break; + } +#endif + case TK_STRING: { + sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); + break; + } + case TK_NULL: { + sqlite3TreeViewLine(pView,"NULL"); + break; + } +#ifndef SQLITE_OMIT_BLOB_LITERAL + case TK_BLOB: { + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); + break; + } +#endif + case TK_VARIABLE: { + sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", + pExpr->u.zToken, pExpr->iColumn); + break; + } + case TK_REGISTER: { + sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); + break; + } + case TK_ID: { + sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); + break; + } +#ifndef SQLITE_OMIT_CAST + case TK_CAST: { + /* Expressions of the form: CAST(pLeft AS token) */ + sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + break; + } +#endif /* SQLITE_OMIT_CAST */ + case TK_LT: zBinOp = "LT"; break; + case TK_LE: zBinOp = "LE"; break; + case TK_GT: zBinOp = "GT"; break; + case TK_GE: zBinOp = "GE"; break; + case TK_NE: zBinOp = "NE"; break; + case TK_EQ: zBinOp = "EQ"; break; + case TK_IS: zBinOp = "IS"; break; + case TK_ISNOT: zBinOp = "ISNOT"; break; + case TK_AND: zBinOp = "AND"; break; + case TK_OR: zBinOp = "OR"; break; + case TK_PLUS: zBinOp = "ADD"; break; + case TK_STAR: zBinOp = "MUL"; break; + case TK_MINUS: zBinOp = "SUB"; break; + case TK_REM: zBinOp = "REM"; break; + case TK_BITAND: zBinOp = "BITAND"; break; + case TK_BITOR: zBinOp = "BITOR"; break; + case TK_SLASH: zBinOp = "DIV"; break; + case TK_LSHIFT: zBinOp = "LSHIFT"; break; + case TK_RSHIFT: zBinOp = "RSHIFT"; break; + case TK_CONCAT: zBinOp = "CONCAT"; break; + case TK_DOT: zBinOp = "DOT"; break; + + case TK_UMINUS: zUniOp = "UMINUS"; break; + case TK_UPLUS: zUniOp = "UPLUS"; break; + case TK_BITNOT: zUniOp = "BITNOT"; break; + case TK_NOT: zUniOp = "NOT"; break; + case TK_ISNULL: zUniOp = "ISNULL"; break; + case TK_NOTNULL: zUniOp = "NOTNULL"; break; + + case TK_SPAN: { + sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + break; + } + + case TK_COLLATE: { + sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + break; + } + + case TK_AGG_FUNCTION: + case TK_FUNCTION: { + ExprList *pFarg; /* List of function arguments */ + if( ExprHasProperty(pExpr, EP_TokenOnly) ){ + pFarg = 0; + }else{ + pFarg = pExpr->x.pList; + } + if( pExpr->op==TK_AGG_FUNCTION ){ + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", + pExpr->op2, pExpr->u.zToken); + }else{ + sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); + } + if( pFarg ){ + sqlite3TreeViewExprList(pView, pFarg, 0, 0); + } + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_EXISTS: { + sqlite3TreeViewLine(pView, "EXISTS-expr"); + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); + break; + } + case TK_SELECT: { + sqlite3TreeViewLine(pView, "SELECT-expr"); + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); + break; + } + case TK_IN: { + sqlite3TreeViewLine(pView, "IN"); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); + }else{ + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); + } + break; + } +#endif /* SQLITE_OMIT_SUBQUERY */ + + /* + ** x BETWEEN y AND z + ** + ** This is equivalent to + ** + ** x>=y AND x<=z + ** + ** X is stored in pExpr->pLeft. + ** Y is stored in pExpr->pList->a[0].pExpr. + ** Z is stored in pExpr->pList->a[1].pExpr. + */ + case TK_BETWEEN: { + Expr *pX = pExpr->pLeft; + Expr *pY = pExpr->x.pList->a[0].pExpr; + Expr *pZ = pExpr->x.pList->a[1].pExpr; + sqlite3TreeViewLine(pView, "BETWEEN"); + sqlite3TreeViewExpr(pView, pX, 1); + sqlite3TreeViewExpr(pView, pY, 1); + sqlite3TreeViewExpr(pView, pZ, 0); + break; + } + case TK_TRIGGER: { + /* If the opcode is TK_TRIGGER, then the expression is a reference + ** to a column in the new.* or old.* pseudo-tables available to + ** trigger programs. In this case Expr.iTable is set to 1 for the + ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn + ** is set to the column of the pseudo-table to read, or to -1 to + ** read the rowid field. + */ + sqlite3TreeViewLine(pView, "%s(%d)", + pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); + break; + } + case TK_CASE: { + sqlite3TreeViewLine(pView, "CASE"); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); + break; + } +#ifndef SQLITE_OMIT_TRIGGER + case TK_RAISE: { + const char *zType = "unk"; + switch( pExpr->affinity ){ + case OE_Rollback: zType = "rollback"; break; + case OE_Abort: zType = "abort"; break; + case OE_Fail: zType = "fail"; break; + case OE_Ignore: zType = "ignore"; break; + } + sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); + break; + } +#endif + default: { + sqlite3TreeViewLine(pView, "op=%d", pExpr->op); + break; + } + } + if( zBinOp ){ + sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); + sqlite3TreeViewExpr(pView, pExpr->pRight, 0); + }else if( zUniOp ){ + sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + } + sqlite3TreeViewPop(pView); +} + +/* +** Generate a human-readable explanation of an expression list. +*/ +SQLITE_PRIVATE void sqlite3TreeViewExprList( + TreeView *pView, + const ExprList *pList, + u8 moreToFollow, + const char *zLabel +){ + int i; + pView = sqlite3TreeViewPush(pView, moreToFollow); + if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; + if( pList==0 ){ + sqlite3TreeViewLine(pView, "%s (empty)", zLabel); + }else{ + sqlite3TreeViewLine(pView, "%s", zLabel); + for(i=0; inExpr; i++){ + int j = pList->a[i].u.x.iOrderByCol; + if( j ){ + sqlite3TreeViewPush(pView, 0); + sqlite3TreeViewLine(pView, "iOrderByCol=%d", j); + } + sqlite3TreeViewExpr(pView, pList->a[i].pExpr, inExpr-1); + if( j ) sqlite3TreeViewPop(pView); + } + } + sqlite3TreeViewPop(pView); +} + +#endif /* SQLITE_DEBUG */ + +/************** End of treeview.c ********************************************/ +/************** Begin file random.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code to implement a pseudo-random number +** generator (PRNG) for SQLite. +** +** Random numbers are used by some of the database backends in order +** to generate random integer keys for tables or random filenames. +*/ +/* #include "sqliteInt.h" */ + + +/* All threads share a single random number generator. +** This structure is the current state of the generator. +*/ +static SQLITE_WSD struct sqlite3PrngType { + unsigned char isInit; /* True if initialized */ + unsigned char i, j; /* State variables */ + unsigned char s[256]; /* State variables */ +} sqlite3Prng; + +/* +** Return N random bytes. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){ + unsigned char t; + unsigned char *zBuf = pBuf; + + /* The "wsdPrng" macro will resolve to the pseudo-random number generator + ** state vector. If writable static data is unsupported on the target, + ** we have to locate the state vector at run-time. In the more common + ** case where writable static data is supported, wsdPrng can refer directly + ** to the "sqlite3Prng" state vector declared above. + */ +#ifdef SQLITE_OMIT_WSD + struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng); +# define wsdPrng p[0] +#else +# define wsdPrng sqlite3Prng +#endif + +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex; +#endif + +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return; +#endif + +#if SQLITE_THREADSAFE + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); +#endif + + sqlite3_mutex_enter(mutex); + if( N<=0 || pBuf==0 ){ + wsdPrng.isInit = 0; + sqlite3_mutex_leave(mutex); + return; + } + + /* Initialize the state of the random number generator once, + ** the first time this routine is called. The seed value does + ** not need to contain a lot of randomness since we are not + ** trying to do secure encryption or anything like that... + ** + ** Nothing in this file or anywhere else in SQLite does any kind of + ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random + ** number generator) not as an encryption device. + */ + if( !wsdPrng.isInit ){ + int i; + char k[256]; + wsdPrng.j = 0; + wsdPrng.i = 0; + sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); + for(i=0; i<256; i++){ + wsdPrng.s[i] = (u8)i; + } + for(i=0; i<256; i++){ + wsdPrng.j += wsdPrng.s[i] + k[i]; + t = wsdPrng.s[wsdPrng.j]; + wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; + wsdPrng.s[i] = t; + } + wsdPrng.isInit = 1; + } + + assert( N>0 ); + do{ + wsdPrng.i++; + t = wsdPrng.s[wsdPrng.i]; + wsdPrng.j += t; + wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; + wsdPrng.s[wsdPrng.j] = t; + t += wsdPrng.s[wsdPrng.i]; + *(zBuf++) = wsdPrng.s[t]; + }while( --N ); + sqlite3_mutex_leave(mutex); +} + +#ifndef SQLITE_OMIT_BUILTIN_TEST +/* +** For testing purposes, we sometimes want to preserve the state of +** PRNG and restore the PRNG to its saved state at a later time, or +** to reset the PRNG to its initial state. These routines accomplish +** those tasks. +** +** The sqlite3_test_control() interface calls these routines to +** control the PRNG. +*/ +static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng; +SQLITE_PRIVATE void sqlite3PrngSaveState(void){ + memcpy( + &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), + &GLOBAL(struct sqlite3PrngType, sqlite3Prng), + sizeof(sqlite3Prng) + ); +} +SQLITE_PRIVATE void sqlite3PrngRestoreState(void){ + memcpy( + &GLOBAL(struct sqlite3PrngType, sqlite3Prng), + &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), + sizeof(sqlite3Prng) + ); +} +#endif /* SQLITE_OMIT_BUILTIN_TEST */ + +/************** End of random.c **********************************************/ +/************** Begin file threads.c *****************************************/ +/* +** 2012 July 21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file presents a simple cross-platform threading interface for +** use internally by SQLite. +** +** A "thread" can be created using sqlite3ThreadCreate(). This thread +** runs independently of its creator until it is joined using +** sqlite3ThreadJoin(), at which point it terminates. +** +** Threads do not have to be real. It could be that the work of the +** "thread" is done by the main thread at either the sqlite3ThreadCreate() +** or sqlite3ThreadJoin() call. This is, in fact, what happens in +** single threaded systems. Nothing in SQLite requires multiple threads. +** This interface exists so that applications that want to take advantage +** of multiple cores can do so, while also allowing applications to stay +** single-threaded if desired. +*/ +/* #include "sqliteInt.h" */ +#if SQLITE_OS_WIN +/* # include "os_win.h" */ +#endif + +#if SQLITE_MAX_WORKER_THREADS>0 + +/********************************* Unix Pthreads ****************************/ +#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0 + +#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ +/* #include */ + +/* A running thread */ +struct SQLiteThread { + pthread_t tid; /* Thread ID */ + int done; /* Set to true when thread finishes */ + void *pOut; /* Result returned by the thread */ + void *(*xTask)(void*); /* The thread routine */ + void *pIn; /* Argument to the thread */ +}; + +/* Create a new thread */ +SQLITE_PRIVATE int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + int rc; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + /* This routine is never used in single-threaded mode */ + assert( sqlite3GlobalConfig.bCoreMutex!=0 ); + + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM_BKPT; + memset(p, 0, sizeof(*p)); + p->xTask = xTask; + p->pIn = pIn; + /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a + ** function that returns SQLITE_ERROR when passed the argument 200, that + ** forces worker threads to run sequentially and deterministically + ** for testing purposes. */ + if( sqlite3FaultSim(200) ){ + rc = 1; + }else{ + rc = pthread_create(&p->tid, 0, xTask, pIn); + } + if( rc ){ + p->done = 1; + p->pOut = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +/* Get the results of the thread */ +SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + int rc; + + assert( ppOut!=0 ); + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->done ){ + *ppOut = p->pOut; + rc = SQLITE_OK; + }else{ + rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; + } + sqlite3_free(p); + return rc; +} + +#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ +/******************************** End Unix Pthreads *************************/ + + +/********************************* Win32 Threads ****************************/ +#if SQLITE_OS_WIN_THREADS + +#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ +#include + +/* A running thread */ +struct SQLiteThread { + void *tid; /* The thread handle */ + unsigned id; /* The thread identifier */ + void *(*xTask)(void*); /* The routine to run as a thread */ + void *pIn; /* Argument to xTask */ + void *pResult; /* Result of xTask */ +}; + +/* Thread procedure Win32 compatibility shim */ +static unsigned __stdcall sqlite3ThreadProc( + void *pArg /* IN: Pointer to the SQLiteThread structure */ +){ + SQLiteThread *p = (SQLiteThread *)pArg; + + assert( p!=0 ); +#if 0 + /* + ** This assert appears to trigger spuriously on certain + ** versions of Windows, possibly due to _beginthreadex() + ** and/or CreateThread() not fully setting their thread + ** ID parameter before starting the thread. + */ + assert( p->id==GetCurrentThreadId() ); +#endif + assert( p->xTask!=0 ); + p->pResult = p->xTask(p->pIn); + + _endthreadex(0); + return 0; /* NOT REACHED */ +} + +/* Create a new thread */ +SQLITE_PRIVATE int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM_BKPT; + /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a + ** function that returns SQLITE_ERROR when passed the argument 200, that + ** forces worker threads to run sequentially and deterministically + ** (via the sqlite3FaultSim() term of the conditional) for testing + ** purposes. */ + if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){ + memset(p, 0, sizeof(*p)); + }else{ + p->xTask = xTask; + p->pIn = pIn; + p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); + if( p->tid==0 ){ + memset(p, 0, sizeof(*p)); + } + } + if( p->xTask==0 ){ + p->id = GetCurrentThreadId(); + p->pResult = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */ + +/* Get the results of the thread */ +SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + DWORD rc; + BOOL bRc; + + assert( ppOut!=0 ); + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->xTask==0 ){ + /* assert( p->id==GetCurrentThreadId() ); */ + rc = WAIT_OBJECT_0; + assert( p->tid==0 ); + }else{ + assert( p->id!=0 && p->id!=GetCurrentThreadId() ); + rc = sqlite3Win32Wait((HANDLE)p->tid); + assert( rc!=WAIT_IO_COMPLETION ); + bRc = CloseHandle((HANDLE)p->tid); + assert( bRc ); + } + if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; + sqlite3_free(p); + return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR; +} + +#endif /* SQLITE_OS_WIN_THREADS */ +/******************************** End Win32 Threads *************************/ + + +/********************************* Single-Threaded **************************/ +#ifndef SQLITE_THREADS_IMPLEMENTED +/* +** This implementation does not actually create a new thread. It does the +** work of the thread in the main thread, when either the thread is created +** or when it is joined +*/ + +/* A running thread */ +struct SQLiteThread { + void *(*xTask)(void*); /* The routine to run as a thread */ + void *pIn; /* Argument to xTask */ + void *pResult; /* Result of xTask */ +}; + +/* Create a new thread */ +SQLITE_PRIVATE int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM_BKPT; + if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ + p->xTask = xTask; + p->pIn = pIn; + }else{ + p->xTask = 0; + p->pResult = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +/* Get the results of the thread */ +SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + + assert( ppOut!=0 ); + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->xTask ){ + *ppOut = p->xTask(p->pIn); + }else{ + *ppOut = p->pResult; + } + sqlite3_free(p); + +#if defined(SQLITE_TEST) + { + void *pTstAlloc = sqlite3Malloc(10); + if (!pTstAlloc) return SQLITE_NOMEM_BKPT; + sqlite3_free(pTstAlloc); + } +#endif + + return SQLITE_OK; +} + +#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */ +/****************************** End Single-Threaded *************************/ +#endif /* SQLITE_MAX_WORKER_THREADS>0 */ + +/************** End of threads.c *********************************************/ +/************** Begin file utf.c *********************************************/ +/* +** 2004 April 13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains routines used to translate between UTF-8, +** UTF-16, UTF-16BE, and UTF-16LE. +** +** Notes on UTF-8: +** +** Byte-0 Byte-1 Byte-2 Byte-3 Value +** 0xxxxxxx 00000000 00000000 0xxxxxxx +** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx +** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx +** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx +** +** +** Notes on UTF-16: (with wwww+1==uuuuu) +** +** Word-0 Word-1 Value +** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx +** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx +** +** +** BOM or Byte Order Mark: +** 0xff 0xfe little-endian utf-16 follows +** 0xfe 0xff big-endian utf-16 follows +** +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include "vdbeInt.h" */ + +#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0 +/* +** The following constant value is used by the SQLITE_BIGENDIAN and +** SQLITE_LITTLEENDIAN macros. +*/ +SQLITE_PRIVATE const int sqlite3one = 1; +#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */ + +/* +** This lookup table is used to help decode the first byte of +** a multi-byte UTF8 character. +*/ +static const unsigned char sqlite3Utf8Trans1[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, +}; + + +#define WRITE_UTF8(zOut, c) { \ + if( c<0x00080 ){ \ + *zOut++ = (u8)(c&0xFF); \ + } \ + else if( c<0x00800 ){ \ + *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + } \ + else if( c<0x10000 ){ \ + *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \ + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + }else{ \ + *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \ + *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \ + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + } \ +} + +#define WRITE_UTF16LE(zOut, c) { \ + if( c<=0xFFFF ){ \ + *zOut++ = (u8)(c&0x00FF); \ + *zOut++ = (u8)((c>>8)&0x00FF); \ + }else{ \ + *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ + *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ + *zOut++ = (u8)(c&0x00FF); \ + *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ + } \ +} + +#define WRITE_UTF16BE(zOut, c) { \ + if( c<=0xFFFF ){ \ + *zOut++ = (u8)((c>>8)&0x00FF); \ + *zOut++ = (u8)(c&0x00FF); \ + }else{ \ + *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ + *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ + *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ + *zOut++ = (u8)(c&0x00FF); \ + } \ +} + +#define READ_UTF16LE(zIn, TERM, c){ \ + c = (*zIn++); \ + c += ((*zIn++)<<8); \ + if( c>=0xD800 && c<0xE000 && TERM ){ \ + int c2 = (*zIn++); \ + c2 += ((*zIn++)<<8); \ + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ + } \ +} + +#define READ_UTF16BE(zIn, TERM, c){ \ + c = ((*zIn++)<<8); \ + c += (*zIn++); \ + if( c>=0xD800 && c<0xE000 && TERM ){ \ + int c2 = ((*zIn++)<<8); \ + c2 += (*zIn++); \ + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ + } \ +} + +/* +** Translate a single UTF-8 character. Return the unicode value. +** +** During translation, assume that the byte that zTerm points +** is a 0x00. +** +** Write a pointer to the next unread byte back into *pzNext. +** +** Notes On Invalid UTF-8: +** +** * This routine never allows a 7-bit character (0x00 through 0x7f) to +** be encoded as a multi-byte character. Any multi-byte character that +** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd. +** +** * This routine never allows a UTF16 surrogate value to be encoded. +** If a multi-byte character attempts to encode a value between +** 0xd800 and 0xe000 then it is rendered as 0xfffd. +** +** * Bytes in the range of 0x80 through 0xbf which occur as the first +** byte of a character are interpreted as single-byte characters +** and rendered as themselves even though they are technically +** invalid characters. +** +** * This routine accepts over-length UTF8 encodings +** for unicode values 0x80 and greater. It does not change over-length +** encodings to 0xfffd as some systems recommend. +*/ +#define READ_UTF8(zIn, zTerm, c) \ + c = *(zIn++); \ + if( c>=0xc0 ){ \ + c = sqlite3Utf8Trans1[c-0xc0]; \ + while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \ + c = (c<<6) + (0x3f & *(zIn++)); \ + } \ + if( c<0x80 \ + || (c&0xFFFFF800)==0xD800 \ + || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ + } +SQLITE_PRIVATE u32 sqlite3Utf8Read( + const unsigned char **pz /* Pointer to string from which to read char */ +){ + unsigned int c; + + /* Same as READ_UTF8() above but without the zTerm parameter. + ** For this routine, we assume the UTF8 string is always zero-terminated. + */ + c = *((*pz)++); + if( c>=0xc0 ){ + c = sqlite3Utf8Trans1[c-0xc0]; + while( (*(*pz) & 0xc0)==0x80 ){ + c = (c<<6) + (0x3f & *((*pz)++)); + } + if( c<0x80 + || (c&0xFFFFF800)==0xD800 + || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } + } + return c; +} + + + + +/* +** If the TRANSLATE_TRACE macro is defined, the value of each Mem is +** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). +*/ +/* #define TRANSLATE_TRACE 1 */ + +#ifndef SQLITE_OMIT_UTF16 +/* +** This routine transforms the internal text encoding used by pMem to +** desiredEnc. It is an error if the string is already of the desired +** encoding, or if *pMem does not contain a string value. +*/ +SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ + int len; /* Maximum length of output string in bytes */ + unsigned char *zOut; /* Output buffer */ + unsigned char *zIn; /* Input iterator */ + unsigned char *zTerm; /* End of input */ + unsigned char *z; /* Output iterator */ + unsigned int c; + + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( pMem->flags&MEM_Str ); + assert( pMem->enc!=desiredEnc ); + assert( pMem->enc!=0 ); + assert( pMem->n>=0 ); + +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) + { + char zBuf[100]; + sqlite3VdbeMemPrettyPrint(pMem, zBuf); + fprintf(stderr, "INPUT: %s\n", zBuf); + } +#endif + + /* If the translation is between UTF-16 little and big endian, then + ** all that is required is to swap the byte order. This case is handled + ** differently from the others. + */ + if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ + u8 temp; + int rc; + rc = sqlite3VdbeMemMakeWriteable(pMem); + if( rc!=SQLITE_OK ){ + assert( rc==SQLITE_NOMEM ); + return SQLITE_NOMEM_BKPT; + } + zIn = (u8*)pMem->z; + zTerm = &zIn[pMem->n&~1]; + while( zInenc = desiredEnc; + goto translate_out; + } + + /* Set len to the maximum number of bytes required in the output buffer. */ + if( desiredEnc==SQLITE_UTF8 ){ + /* When converting from UTF-16, the maximum growth results from + ** translating a 2-byte character to a 4-byte UTF-8 character. + ** A single byte is required for the output string + ** nul-terminator. + */ + pMem->n &= ~1; + len = pMem->n * 2 + 1; + }else{ + /* When converting from UTF-8 to UTF-16 the maximum growth is caused + ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 + ** character. Two bytes are required in the output buffer for the + ** nul-terminator. + */ + len = pMem->n * 2 + 2; + } + + /* Set zIn to point at the start of the input buffer and zTerm to point 1 + ** byte past the end. + ** + ** Variable zOut is set to point at the output buffer, space obtained + ** from sqlite3_malloc(). + */ + zIn = (u8*)pMem->z; + zTerm = &zIn[pMem->n]; + zOut = sqlite3DbMallocRaw(pMem->db, len); + if( !zOut ){ + return SQLITE_NOMEM_BKPT; + } + z = zOut; + + if( pMem->enc==SQLITE_UTF8 ){ + if( desiredEnc==SQLITE_UTF16LE ){ + /* UTF-8 -> UTF-16 Little-endian */ + while( zIn UTF-16 Big-endian */ + while( zInn = (int)(z - zOut); + *z++ = 0; + }else{ + assert( desiredEnc==SQLITE_UTF8 ); + if( pMem->enc==SQLITE_UTF16LE ){ + /* UTF-16 Little-endian -> UTF-8 */ + while( zIn UTF-8 */ + while( zInn = (int)(z - zOut); + } + *z = 0; + assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); + + c = pMem->flags; + sqlite3VdbeMemRelease(pMem); + pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype)); + pMem->enc = desiredEnc; + pMem->z = (char*)zOut; + pMem->zMalloc = pMem->z; + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); + +translate_out: +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) + { + char zBuf[100]; + sqlite3VdbeMemPrettyPrint(pMem, zBuf); + fprintf(stderr, "OUTPUT: %s\n", zBuf); + } +#endif + return SQLITE_OK; +} + +/* +** This routine checks for a byte-order mark at the beginning of the +** UTF-16 string stored in *pMem. If one is present, it is removed and +** the encoding of the Mem adjusted. This routine does not do any +** byte-swapping, it just sets Mem.enc appropriately. +** +** The allocation (static, dynamic etc.) and encoding of the Mem may be +** changed by this function. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){ + int rc = SQLITE_OK; + u8 bom = 0; + + assert( pMem->n>=0 ); + if( pMem->n>1 ){ + u8 b1 = *(u8 *)pMem->z; + u8 b2 = *(((u8 *)pMem->z) + 1); + if( b1==0xFE && b2==0xFF ){ + bom = SQLITE_UTF16BE; + } + if( b1==0xFF && b2==0xFE ){ + bom = SQLITE_UTF16LE; + } + } + + if( bom ){ + rc = sqlite3VdbeMemMakeWriteable(pMem); + if( rc==SQLITE_OK ){ + pMem->n -= 2; + memmove(pMem->z, &pMem->z[2], pMem->n); + pMem->z[pMem->n] = '\0'; + pMem->z[pMem->n+1] = '\0'; + pMem->flags |= MEM_Term; + pMem->enc = bom; + } + } + return rc; +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, +** return the number of unicode characters in pZ up to (but not including) +** the first 0x00 byte. If nByte is not less than zero, return the +** number of unicode characters in the first nByte of pZ (or up to +** the first 0x00, whichever comes first). +*/ +SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){ + int r = 0; + const u8 *z = (const u8*)zIn; + const u8 *zTerm; + if( nByte>=0 ){ + zTerm = &z[nByte]; + }else{ + zTerm = (const u8*)(-1); + } + assert( z<=zTerm ); + while( *z!=0 && zmallocFailed ){ + sqlite3VdbeMemRelease(&m); + m.z = 0; + } + assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); + assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); + assert( m.z || db->mallocFailed ); + return m.z; +} + +/* +** zIn is a UTF-16 encoded unicode string at least nChar characters long. +** Return the number of bytes in the first nChar unicode characters +** in pZ. nChar must be non-negative. +*/ +SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){ + int c; + unsigned char const *z = zIn; + int n = 0; + + if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){ + while( n0 && n<=4 ); + z[0] = 0; + z = zBuf; + c = sqlite3Utf8Read((const u8**)&z); + t = i; + if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD; + if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD; + assert( c==t ); + assert( (z-zBuf)==n ); + } + for(i=0; i<0x00110000; i++){ + if( i>=0xD800 && i<0xE000 ) continue; + z = zBuf; + WRITE_UTF16LE(z, i); + n = (int)(z-zBuf); + assert( n>0 && n<=4 ); + z[0] = 0; + z = zBuf; + READ_UTF16LE(z, 1, c); + assert( c==i ); + assert( (z-zBuf)==n ); + } + for(i=0; i<0x00110000; i++){ + if( i>=0xD800 && i<0xE000 ) continue; + z = zBuf; + WRITE_UTF16BE(z, i); + n = (int)(z-zBuf); + assert( n>0 && n<=4 ); + z[0] = 0; + z = zBuf; + READ_UTF16BE(z, 1, c); + assert( c==i ); + assert( (z-zBuf)==n ); + } +} +#endif /* SQLITE_TEST */ +#endif /* SQLITE_OMIT_UTF16 */ + +/************** End of utf.c *************************************************/ +/************** Begin file util.c ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Utility functions used throughout sqlite. +** +** This file contains functions for allocating memory, comparing +** strings, and stuff like that. +** +*/ +/* #include "sqliteInt.h" */ +/* #include */ +#if HAVE_ISNAN || SQLITE_HAVE_ISNAN +# include +#endif + +/* +** Routine needed to support the testcase() macro. +*/ +#ifdef SQLITE_COVERAGE_TEST +SQLITE_PRIVATE void sqlite3Coverage(int x){ + static unsigned dummy = 0; + dummy += (unsigned)x; +} +#endif + +/* +** Give a callback to the test harness that can be used to simulate faults +** in places where it is difficult or expensive to do so purely by means +** of inputs. +** +** The intent of the integer argument is to let the fault simulator know +** which of multiple sqlite3FaultSim() calls has been hit. +** +** Return whatever integer value the test callback returns, or return +** SQLITE_OK if no test callback is installed. +*/ +#ifndef SQLITE_OMIT_BUILTIN_TEST +SQLITE_PRIVATE int sqlite3FaultSim(int iTest){ + int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; + return xCallback ? xCallback(iTest) : SQLITE_OK; +} +#endif + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** Return true if the floating point value is Not a Number (NaN). +** +** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. +** Otherwise, we have our own implementation that works on most systems. +*/ +SQLITE_PRIVATE int sqlite3IsNaN(double x){ + int rc; /* The value return */ +#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN + /* + ** Systems that support the isnan() library function should probably + ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have + ** found that many systems do not have a working isnan() function so + ** this implementation is provided as an alternative. + ** + ** This NaN test sometimes fails if compiled on GCC with -ffast-math. + ** On the other hand, the use of -ffast-math comes with the following + ** warning: + ** + ** This option [-ffast-math] should never be turned on by any + ** -O option since it can result in incorrect output for programs + ** which depend on an exact implementation of IEEE or ISO + ** rules/specifications for math functions. + ** + ** Under MSVC, this NaN test may fail if compiled with a floating- + ** point precision mode other than /fp:precise. From the MSDN + ** documentation: + ** + ** The compiler [with /fp:precise] will properly handle comparisons + ** involving NaN. For example, x != x evaluates to true if x is NaN + ** ... + */ +#ifdef __FAST_MATH__ +# error SQLite will not work correctly with the -ffast-math option of GCC. +#endif + volatile double y = x; + volatile double z = y; + rc = (y!=z); +#else /* if HAVE_ISNAN */ + rc = isnan(x); +#endif /* HAVE_ISNAN */ + testcase( rc ); + return rc; +} +#endif /* SQLITE_OMIT_FLOATING_POINT */ + +/* +** Compute a string length that is limited to what can be stored in +** lower 30 bits of a 32-bit signed integer. +** +** The value returned will never be negative. Nor will it ever be greater +** than the actual length of the string. For very long strings (greater +** than 1GiB) the value returned might be less than the true string length. +*/ +SQLITE_PRIVATE int sqlite3Strlen30(const char *z){ + if( z==0 ) return 0; + return 0x3fffffff & (int)strlen(z); +} + +/* +** Return the declared type of a column. Or return zDflt if the column +** has no declared type. +** +** The column type is an extra string stored after the zero-terminator on +** the column name if and only if the COLFLAG_HASTYPE flag is set. +*/ +SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){ + if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt; + return pCol->zName + strlen(pCol->zName) + 1; +} + +/* +** Helper function for sqlite3Error() - called rarely. Broken out into +** a separate routine to avoid unnecessary register saves on entry to +** sqlite3Error(). +*/ +static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ + if( db->pErr ) sqlite3ValueSetNull(db->pErr); + sqlite3SystemError(db, err_code); +} + +/* +** Set the current error code to err_code and clear any prior error message. +** Also set iSysErrno (by calling sqlite3System) if the err_code indicates +** that would be appropriate. +*/ +SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ + assert( db!=0 ); + db->errCode = err_code; + if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code); +} + +/* +** Load the sqlite3.iSysErrno field if that is an appropriate thing +** to do based on the SQLite error code in rc. +*/ +SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){ + if( rc==SQLITE_IOERR_NOMEM ) return; + rc &= 0xff; + if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ + db->iSysErrno = sqlite3OsGetLastError(db->pVfs); + } +} + +/* +** Set the most recent error code and error string for the sqlite +** handle "db". The error code is set to "err_code". +** +** If it is not NULL, string zFormat specifies the format of the +** error string in the style of the printf functions: The following +** format characters are allowed: +** +** %s Insert a string +** %z A string that should be freed after use +** %d Insert an integer +** %T Insert a token +** %S Insert the first element of a SrcList +** +** zFormat and any string tokens that follow it are assumed to be +** encoded in UTF-8. +** +** To clear the most recent error for sqlite handle "db", sqlite3Error +** should be called with err_code set to SQLITE_OK and zFormat set +** to NULL. +*/ +SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ + assert( db!=0 ); + db->errCode = err_code; + sqlite3SystemError(db, err_code); + if( zFormat==0 ){ + sqlite3Error(db, err_code); + }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ + char *z; + va_list ap; + va_start(ap, zFormat); + z = sqlite3VMPrintf(db, zFormat, ap); + va_end(ap); + sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); + } +} + +/* +** Add an error message to pParse->zErrMsg and increment pParse->nErr. +** The following formatting characters are allowed: +** +** %s Insert a string +** %z A string that should be freed after use +** %d Insert an integer +** %T Insert a token +** %S Insert the first element of a SrcList +** +** This function should be used to report any error that occurs while +** compiling an SQL statement (i.e. within sqlite3_prepare()). The +** last thing the sqlite3_prepare() function does is copy the error +** stored by this function into the database handle using sqlite3Error(). +** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used +** during statement execution (sqlite3_step() etc.). +*/ +SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ + char *zMsg; + va_list ap; + sqlite3 *db = pParse->db; + va_start(ap, zFormat); + zMsg = sqlite3VMPrintf(db, zFormat, ap); + va_end(ap); + if( db->suppressErr ){ + sqlite3DbFree(db, zMsg); + }else{ + pParse->nErr++; + sqlite3DbFree(db, pParse->zErrMsg); + pParse->zErrMsg = zMsg; + pParse->rc = SQLITE_ERROR; + } +} + +/* +** Convert an SQL-style quoted string into a normal string by removing +** the quote characters. The conversion is done in-place. If the +** input does not begin with a quote character, then this routine +** is a no-op. +** +** The input string must be zero-terminated. A new zero-terminator +** is added to the dequoted string. +** +** The return value is -1 if no dequoting occurs or the length of the +** dequoted string, exclusive of the zero terminator, if dequoting does +** occur. +** +** 2002-Feb-14: This routine is extended to remove MS-Access style +** brackets from around identifiers. For example: "[a-b-c]" becomes +** "a-b-c". +*/ +SQLITE_PRIVATE void sqlite3Dequote(char *z){ + char quote; + int i, j; + if( z==0 ) return; + quote = z[0]; + if( !sqlite3Isquote(quote) ) return; + if( quote=='[' ) quote = ']'; + for(i=1, j=0;; i++){ + assert( z[i] ); + if( z[i]==quote ){ + if( z[i+1]==quote ){ + z[j++] = quote; + i++; + }else{ + break; + } + }else{ + z[j++] = z[i]; + } + } + z[j] = 0; +} + +/* +** Generate a Token object from a string +*/ +SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){ + p->z = z; + p->n = sqlite3Strlen30(z); +} + +/* Convenient short-hand */ +#define UpperToLower sqlite3UpperToLower + +/* +** Some systems have stricmp(). Others have strcasecmp(). Because +** there is no consistency, we will define our own. +** +** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and +** sqlite3_strnicmp() APIs allow applications and extensions to compare +** the contents of two buffers containing UTF-8 strings in a +** case-independent fashion, using the same definition of "case +** independence" that SQLite uses internally when comparing identifiers. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){ + if( zLeft==0 ){ + return zRight ? -1 : 0; + }else if( zRight==0 ){ + return 1; + } + return sqlite3StrICmp(zLeft, zRight); +} +SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ + unsigned char *a, *b; + int c; + a = (unsigned char *)zLeft; + b = (unsigned char *)zRight; + for(;;){ + c = (int)UpperToLower[*a] - (int)UpperToLower[*b]; + if( c || *a==0 ) break; + a++; + b++; + } + return c; +} +SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ + register unsigned char *a, *b; + if( zLeft==0 ){ + return zRight ? -1 : 0; + }else if( zRight==0 ){ + return 1; + } + a = (unsigned char *)zLeft; + b = (unsigned char *)zRight; + while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } + return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; +} + +/* +** The string z[] is an text representation of a real number. +** Convert this string to a double and write it into *pResult. +** +** The string z[] is length bytes in length (bytes, not characters) and +** uses the encoding enc. The string is not necessarily zero-terminated. +** +** Return TRUE if the result is a valid real number (or integer) and FALSE +** if the string is empty or contains extraneous text. Valid numbers +** are in one of these formats: +** +** [+-]digits[E[+-]digits] +** [+-]digits.[digits][E[+-]digits] +** [+-].digits[E[+-]digits] +** +** Leading and trailing whitespace is ignored for the purpose of determining +** validity. +** +** If some prefix of the input string is a valid number, this routine +** returns FALSE but it still converts the prefix and writes the result +** into *pResult. +*/ +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ +#ifndef SQLITE_OMIT_FLOATING_POINT + int incr; + const char *zEnd = z + length; + /* sign * significand * (10 ^ (esign * exponent)) */ + int sign = 1; /* sign of significand */ + i64 s = 0; /* significand */ + int d = 0; /* adjust exponent for shifting decimal point */ + int esign = 1; /* sign of exponent */ + int e = 0; /* exponent */ + int eValid = 1; /* True exponent is either not used or is well-formed */ + double result; + int nDigits = 0; + int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ + + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); + *pResult = 0.0; /* Default return value, in case of an error */ + + if( enc==SQLITE_UTF8 ){ + incr = 1; + }else{ + int i; + incr = 2; + assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + for(i=3-enc; i=zEnd ) return 0; + + /* get sign of significand */ + if( *z=='-' ){ + sign = -1; + z+=incr; + }else if( *z=='+' ){ + z+=incr; + } + + /* copy max significant digits to significand */ + while( z=zEnd ) goto do_atof_calc; + + /* if decimal point is present */ + if( *z=='.' ){ + z+=incr; + /* copy digits from after decimal to significand + ** (decrease exponent by d to shift decimal right) */ + while( z=zEnd ) goto do_atof_calc; + + /* if exponent is present */ + if( *z=='e' || *z=='E' ){ + z+=incr; + eValid = 0; + + /* This branch is needed to avoid a (harmless) buffer overread. The + ** special comment alerts the mutation tester that the correct answer + ** is obtained even if the branch is omitted */ + if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ + + /* get sign of exponent */ + if( *z=='-' ){ + esign = -1; + z+=incr; + }else if( *z=='+' ){ + z+=incr; + } + /* copy digits to exponent */ + while( z0 ){ /*OPTIMIZATION-IF-TRUE*/ + if( esign>0 ){ + if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ + s *= 10; + }else{ + if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ + s /= 10; + } + e--; + } + + /* adjust the sign of significand */ + s = sign<0 ? -s : s; + + if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ + result = (double)s; + }else{ + LONGDOUBLE_TYPE scale = 1.0; + /* attempt to handle extremely small/large numbers better */ + if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ + if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ + while( e%308 ) { scale *= 1.0e+1; e -= 1; } + if( esign<0 ){ + result = s / scale; + result /= 1.0e+308; + }else{ + result = s * scale; + result *= 1.0e+308; + } + }else{ assert( e>=342 ); + if( esign<0 ){ + result = 0.0*s; + }else{ + result = 1e308*1e308*s; /* Infinity */ + } + } + }else{ + /* 1.0e+22 is the largest power of 10 than can be + ** represented exactly. */ + while( e%22 ) { scale *= 1.0e+1; e -= 1; } + while( e>0 ) { scale *= 1.0e+22; e -= 22; } + if( esign<0 ){ + result = s / scale; + }else{ + result = s * scale; + } + } + } + } + + /* store the result */ + *pResult = result; + + /* return true if number and no extra non-whitespace chracters after */ + return z==zEnd && nDigits>0 && eValid && nonNum==0; +#else + return !sqlite3Atoi64(z, pResult, length, enc); +#endif /* SQLITE_OMIT_FLOATING_POINT */ +} + +/* +** Compare the 19-character string zNum against the text representation +** value 2^63: 9223372036854775808. Return negative, zero, or positive +** if zNum is less than, equal to, or greater than the string. +** Note that zNum must contain exactly 19 characters. +** +** Unlike memcmp() this routine is guaranteed to return the difference +** in the values of the last digit if the only difference is in the +** last digit. So, for example, +** +** compare2pow63("9223372036854775800", 1) +** +** will return -8. +*/ +static int compare2pow63(const char *zNum, int incr){ + int c = 0; + int i; + /* 012345678901234567 */ + const char *pow63 = "922337203685477580"; + for(i=0; c==0 && i<18; i++){ + c = (zNum[i*incr]-pow63[i])*10; + } + if( c==0 ){ + c = zNum[18*incr] - '8'; + testcase( c==(-1) ); + testcase( c==0 ); + testcase( c==(+1) ); + } + return c; +} + +/* +** Convert zNum to a 64-bit signed integer. zNum must be decimal. This +** routine does *not* accept hexadecimal notation. +** +** If the zNum value is representable as a 64-bit twos-complement +** integer, then write that value into *pNum and return 0. +** +** If zNum is exactly 9223372036854775808, return 2. This special +** case is broken out because while 9223372036854775808 cannot be a +** signed 64-bit integer, its negative -9223372036854775808 can be. +** +** If zNum is too big for a 64-bit integer and is not +** 9223372036854775808 or if zNum contains any non-numeric text, +** then return 1. +** +** length is the number of bytes in the string (bytes, not characters). +** The string is not necessarily zero-terminated. The encoding is +** given by enc. +*/ +SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ + int incr; + u64 u = 0; + int neg = 0; /* assume positive */ + int i; + int c = 0; + int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ + const char *zStart; + const char *zEnd = zNum + length; + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); + if( enc==SQLITE_UTF8 ){ + incr = 1; + }else{ + incr = 2; + assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + for(i=3-enc; i='0' && c<='9'; i+=incr){ + u = u*10 + c - '0'; + } + if( u>LARGEST_INT64 ){ + *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; + }else if( neg ){ + *pNum = -(i64)u; + }else{ + *pNum = (i64)u; + } + testcase( i==18 ); + testcase( i==19 ); + testcase( i==20 ); + if( &zNum[i]19*incr /* Too many digits */ + || nonNum /* UTF16 with high-order bytes non-zero */ + ){ + /* zNum is empty or contains non-numeric text or is longer + ** than 19 digits (thus guaranteeing that it is too large) */ + return 1; + }else if( i<19*incr ){ + /* Less than 19 digits, so we know that it fits in 64 bits */ + assert( u<=LARGEST_INT64 ); + return 0; + }else{ + /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ + c = compare2pow63(zNum, incr); + if( c<0 ){ + /* zNum is less than 9223372036854775808 so it fits */ + assert( u<=LARGEST_INT64 ); + return 0; + }else if( c>0 ){ + /* zNum is greater than 9223372036854775808 so it overflows */ + return 1; + }else{ + /* zNum is exactly 9223372036854775808. Fits if negative. The + ** special case 2 overflow if positive */ + assert( u-1==LARGEST_INT64 ); + return neg ? 0 : 2; + } + } +} + +/* +** Transform a UTF-8 integer literal, in either decimal or hexadecimal, +** into a 64-bit signed integer. This routine accepts hexadecimal literals, +** whereas sqlite3Atoi64() does not. +** +** Returns: +** +** 0 Successful transformation. Fits in a 64-bit signed integer. +** 1 Integer too large for a 64-bit signed integer or is malformed +** 2 Special case of 9223372036854775808 +*/ +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ +#ifndef SQLITE_OMIT_HEX_INTEGER + if( z[0]=='0' + && (z[1]=='x' || z[1]=='X') + ){ + u64 u = 0; + int i, k; + for(i=2; z[i]=='0'; i++){} + for(k=i; sqlite3Isxdigit(z[k]); k++){ + u = u*16 + sqlite3HexToInt(z[k]); + } + memcpy(pOut, &u, 8); + return (z[k]==0 && k-i<=16) ? 0 : 1; + }else +#endif /* SQLITE_OMIT_HEX_INTEGER */ + { + return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); + } +} + +/* +** If zNum represents an integer that will fit in 32-bits, then set +** *pValue to that integer and return true. Otherwise return false. +** +** This routine accepts both decimal and hexadecimal notation for integers. +** +** Any non-numeric characters that following zNum are ignored. +** This is different from sqlite3Atoi64() which requires the +** input number to be zero-terminated. +*/ +SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){ + sqlite_int64 v = 0; + int i, c; + int neg = 0; + if( zNum[0]=='-' ){ + neg = 1; + zNum++; + }else if( zNum[0]=='+' ){ + zNum++; + } +#ifndef SQLITE_OMIT_HEX_INTEGER + else if( zNum[0]=='0' + && (zNum[1]=='x' || zNum[1]=='X') + && sqlite3Isxdigit(zNum[2]) + ){ + u32 u = 0; + zNum += 2; + while( zNum[0]=='0' ) zNum++; + for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ + u = u*16 + sqlite3HexToInt(zNum[i]); + } + if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ + memcpy(pValue, &u, 4); + return 1; + }else{ + return 0; + } + } +#endif + while( zNum[0]=='0' ) zNum++; + for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ + v = v*10 + c; + } + + /* The longest decimal representation of a 32 bit integer is 10 digits: + ** + ** 1234567890 + ** 2^31 -> 2147483648 + */ + testcase( i==10 ); + if( i>10 ){ + return 0; + } + testcase( v-neg==2147483647 ); + if( v-neg>2147483647 ){ + return 0; + } + if( neg ){ + v = -v; + } + *pValue = (int)v; + return 1; +} + +/* +** Return a 32-bit integer value extracted from a string. If the +** string is not an integer, just return 0. +*/ +SQLITE_PRIVATE int sqlite3Atoi(const char *z){ + int x = 0; + if( z ) sqlite3GetInt32(z, &x); + return x; +} + +/* +** The variable-length integer encoding is as follows: +** +** KEY: +** A = 0xxxxxxx 7 bits of data and one flag bit +** B = 1xxxxxxx 7 bits of data and one flag bit +** C = xxxxxxxx 8 bits of data +** +** 7 bits - A +** 14 bits - BA +** 21 bits - BBA +** 28 bits - BBBA +** 35 bits - BBBBA +** 42 bits - BBBBBA +** 49 bits - BBBBBBA +** 56 bits - BBBBBBBA +** 64 bits - BBBBBBBBC +*/ + +/* +** Write a 64-bit variable-length integer to memory starting at p[0]. +** The length of data write will be between 1 and 9 bytes. The number +** of bytes written is returned. +** +** A variable-length integer consists of the lower 7 bits of each byte +** for all bytes that have the 8th bit set and one byte with the 8th +** bit clear. Except, if we get to the 9th byte, it stores the full +** 8 bits and is the last byte. +*/ +static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ + int i, j, n; + u8 buf[10]; + if( v & (((u64)0xff000000)<<32) ){ + p[8] = (u8)v; + v >>= 8; + for(i=7; i>=0; i--){ + p[i] = (u8)((v & 0x7f) | 0x80); + v >>= 7; + } + return 9; + } + n = 0; + do{ + buf[n++] = (u8)((v & 0x7f) | 0x80); + v >>= 7; + }while( v!=0 ); + buf[0] &= 0x7f; + assert( n<=9 ); + for(i=0, j=n-1; j>=0; j--, i++){ + p[i] = buf[j]; + } + return n; +} +SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){ + if( v<=0x7f ){ + p[0] = v&0x7f; + return 1; + } + if( v<=0x3fff ){ + p[0] = ((v>>7)&0x7f)|0x80; + p[1] = v&0x7f; + return 2; + } + return putVarint64(p,v); +} + +/* +** Bitmasks used by sqlite3GetVarint(). These precomputed constants +** are defined here rather than simply putting the constant expressions +** inline in order to work around bugs in the RVT compiler. +** +** SLOT_2_0 A mask for (0x7f<<14) | 0x7f +** +** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 +*/ +#define SLOT_2_0 0x001fc07f +#define SLOT_4_2_0 0xf01fc07f + + +/* +** Read a 64-bit variable-length integer from memory starting at p[0]. +** Return the number of bytes read. The value is stored in *v. +*/ +SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ + u32 a,b,s; + + a = *p; + /* a: p0 (unmasked) */ + if (!(a&0x80)) + { + *v = a; + return 1; + } + + p++; + b = *p; + /* b: p1 (unmasked) */ + if (!(b&0x80)) + { + a &= 0x7f; + a = a<<7; + a |= b; + *v = a; + return 2; + } + + /* Verify that constants are precomputed correctly */ + assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); + assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); + + p++; + a = a<<14; + a |= *p; + /* a: p0<<14 | p2 (unmasked) */ + if (!(a&0x80)) + { + a &= SLOT_2_0; + b &= 0x7f; + b = b<<7; + a |= b; + *v = a; + return 3; + } + + /* CSE1 from below */ + a &= SLOT_2_0; + p++; + b = b<<14; + b |= *p; + /* b: p1<<14 | p3 (unmasked) */ + if (!(b&0x80)) + { + b &= SLOT_2_0; + /* moved CSE1 up */ + /* a &= (0x7f<<14)|(0x7f); */ + a = a<<7; + a |= b; + *v = a; + return 4; + } + + /* a: p0<<14 | p2 (masked) */ + /* b: p1<<14 | p3 (unmasked) */ + /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ + /* moved CSE1 up */ + /* a &= (0x7f<<14)|(0x7f); */ + b &= SLOT_2_0; + s = a; + /* s: p0<<14 | p2 (masked) */ + + p++; + a = a<<14; + a |= *p; + /* a: p0<<28 | p2<<14 | p4 (unmasked) */ + if (!(a&0x80)) + { + /* we can skip these cause they were (effectively) done above + ** while calculating s */ + /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ + /* b &= (0x7f<<14)|(0x7f); */ + b = b<<7; + a |= b; + s = s>>18; + *v = ((u64)s)<<32 | a; + return 5; + } + + /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ + s = s<<7; + s |= b; + /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ + + p++; + b = b<<14; + b |= *p; + /* b: p1<<28 | p3<<14 | p5 (unmasked) */ + if (!(b&0x80)) + { + /* we can skip this cause it was (effectively) done above in calc'ing s */ + /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ + a &= SLOT_2_0; + a = a<<7; + a |= b; + s = s>>18; + *v = ((u64)s)<<32 | a; + return 6; + } + + p++; + a = a<<14; + a |= *p; + /* a: p2<<28 | p4<<14 | p6 (unmasked) */ + if (!(a&0x80)) + { + a &= SLOT_4_2_0; + b &= SLOT_2_0; + b = b<<7; + a |= b; + s = s>>11; + *v = ((u64)s)<<32 | a; + return 7; + } + + /* CSE2 from below */ + a &= SLOT_2_0; + p++; + b = b<<14; + b |= *p; + /* b: p3<<28 | p5<<14 | p7 (unmasked) */ + if (!(b&0x80)) + { + b &= SLOT_4_2_0; + /* moved CSE2 up */ + /* a &= (0x7f<<14)|(0x7f); */ + a = a<<7; + a |= b; + s = s>>4; + *v = ((u64)s)<<32 | a; + return 8; + } + + p++; + a = a<<15; + a |= *p; + /* a: p4<<29 | p6<<15 | p8 (unmasked) */ + + /* moved CSE2 up */ + /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ + b &= SLOT_2_0; + b = b<<8; + a |= b; + + s = s<<4; + b = p[-4]; + b &= 0x7f; + b = b>>3; + s |= b; + + *v = ((u64)s)<<32 | a; + + return 9; +} + +/* +** Read a 32-bit variable-length integer from memory starting at p[0]. +** Return the number of bytes read. The value is stored in *v. +** +** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned +** integer, then set *v to 0xffffffff. +** +** A MACRO version, getVarint32, is provided which inlines the +** single-byte case. All code should use the MACRO version as +** this function assumes the single-byte case has already been handled. +*/ +SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ + u32 a,b; + + /* The 1-byte case. Overwhelmingly the most common. Handled inline + ** by the getVarin32() macro */ + a = *p; + /* a: p0 (unmasked) */ +#ifndef getVarint32 + if (!(a&0x80)) + { + /* Values between 0 and 127 */ + *v = a; + return 1; + } +#endif + + /* The 2-byte case */ + p++; + b = *p; + /* b: p1 (unmasked) */ + if (!(b&0x80)) + { + /* Values between 128 and 16383 */ + a &= 0x7f; + a = a<<7; + *v = a | b; + return 2; + } + + /* The 3-byte case */ + p++; + a = a<<14; + a |= *p; + /* a: p0<<14 | p2 (unmasked) */ + if (!(a&0x80)) + { + /* Values between 16384 and 2097151 */ + a &= (0x7f<<14)|(0x7f); + b &= 0x7f; + b = b<<7; + *v = a | b; + return 3; + } + + /* A 32-bit varint is used to store size information in btrees. + ** Objects are rarely larger than 2MiB limit of a 3-byte varint. + ** A 3-byte varint is sufficient, for example, to record the size + ** of a 1048569-byte BLOB or string. + ** + ** We only unroll the first 1-, 2-, and 3- byte cases. The very + ** rare larger cases can be handled by the slower 64-bit varint + ** routine. + */ +#if 1 + { + u64 v64; + u8 n; + + p -= 2; + n = sqlite3GetVarint(p, &v64); + assert( n>3 && n<=9 ); + if( (v64 & SQLITE_MAX_U32)!=v64 ){ + *v = 0xffffffff; + }else{ + *v = (u32)v64; + } + return n; + } + +#else + /* For following code (kept for historical record only) shows an + ** unrolling for the 3- and 4-byte varint cases. This code is + ** slightly faster, but it is also larger and much harder to test. + */ + p++; + b = b<<14; + b |= *p; + /* b: p1<<14 | p3 (unmasked) */ + if (!(b&0x80)) + { + /* Values between 2097152 and 268435455 */ + b &= (0x7f<<14)|(0x7f); + a &= (0x7f<<14)|(0x7f); + a = a<<7; + *v = a | b; + return 4; + } + + p++; + a = a<<14; + a |= *p; + /* a: p0<<28 | p2<<14 | p4 (unmasked) */ + if (!(a&0x80)) + { + /* Values between 268435456 and 34359738367 */ + a &= SLOT_4_2_0; + b &= SLOT_4_2_0; + b = b<<7; + *v = a | b; + return 5; + } + + /* We can only reach this point when reading a corrupt database + ** file. In that case we are not in any hurry. Use the (relatively + ** slow) general-purpose sqlite3GetVarint() routine to extract the + ** value. */ + { + u64 v64; + u8 n; + + p -= 4; + n = sqlite3GetVarint(p, &v64); + assert( n>5 && n<=9 ); + *v = (u32)v64; + return n; + } +#endif +} + +/* +** Return the number of bytes that will be needed to store the given +** 64-bit integer. +*/ +SQLITE_PRIVATE int sqlite3VarintLen(u64 v){ + int i; + for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } + return i; +} + + +/* +** Read or write a four-byte big-endian integer value. +*/ +SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ +#if SQLITE_BYTEORDER==4321 + u32 x; + memcpy(&x,p,4); + return x; +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && defined(__GNUC__) && GCC_VERSION>=4003000 + u32 x; + memcpy(&x,p,4); + return __builtin_bswap32(x); +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && defined(_MSC_VER) && _MSC_VER>=1300 + u32 x; + memcpy(&x,p,4); + return _byteswap_ulong(x); +#else + testcase( p[0]&0x80 ); + return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; +#endif +} +SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ +#if SQLITE_BYTEORDER==4321 + memcpy(p,&v,4); +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && defined(__GNUC__) && GCC_VERSION>=4003000 + u32 x = __builtin_bswap32(v); + memcpy(p,&x,4); +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && defined(_MSC_VER) && _MSC_VER>=1300 + u32 x = _byteswap_ulong(v); + memcpy(p,&x,4); +#else + p[0] = (u8)(v>>24); + p[1] = (u8)(v>>16); + p[2] = (u8)(v>>8); + p[3] = (u8)v; +#endif +} + + + +/* +** Translate a single byte of Hex into an integer. +** This routine only works if h really is a valid hexadecimal +** character: 0..9a..fA..F +*/ +SQLITE_PRIVATE u8 sqlite3HexToInt(int h){ + assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); +#ifdef SQLITE_ASCII + h += 9*(1&(h>>6)); +#endif +#ifdef SQLITE_EBCDIC + h += 9*(1&~(h>>4)); +#endif + return (u8)(h & 0xf); +} + +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) +/* +** Convert a BLOB literal of the form "x'hhhhhh'" into its binary +** value. Return a pointer to its binary value. Space to hold the +** binary value has been obtained from malloc and must be freed by +** the calling routine. +*/ +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ + char *zBlob; + int i; + + zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); + n--; + if( zBlob ){ + for(i=0; imagic; + if( magic!=SQLITE_MAGIC_OPEN ){ + if( sqlite3SafetyCheckSickOrOk(db) ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + logBadConnection("unopened"); + } + return 0; + }else{ + return 1; + } +} +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ + u32 magic; + magic = db->magic; + if( magic!=SQLITE_MAGIC_SICK && + magic!=SQLITE_MAGIC_OPEN && + magic!=SQLITE_MAGIC_BUSY ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + logBadConnection("invalid"); + return 0; + }else{ + return 1; + } +} + +/* +** Attempt to add, substract, or multiply the 64-bit signed value iB against +** the other 64-bit signed integer at *pA and store the result in *pA. +** Return 0 on success. Or if the operation would have resulted in an +** overflow, leave *pA unchanged and return 1. +*/ +SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){ + i64 iA = *pA; + testcase( iA==0 ); testcase( iA==1 ); + testcase( iB==-1 ); testcase( iB==0 ); + if( iB>=0 ){ + testcase( iA>0 && LARGEST_INT64 - iA == iB ); + testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); + if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; + }else{ + testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); + testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); + if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; + } + *pA += iB; + return 0; +} +SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ + testcase( iB==SMALLEST_INT64+1 ); + if( iB==SMALLEST_INT64 ){ + testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); + if( (*pA)>=0 ) return 1; + *pA -= iB; + return 0; + }else{ + return sqlite3AddInt64(pA, -iB); + } +} +#define TWOPOWER32 (((i64)1)<<32) +#define TWOPOWER31 (((i64)1)<<31) +SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){ + i64 iA = *pA; + i64 iA1, iA0, iB1, iB0, r; + + iA1 = iA/TWOPOWER32; + iA0 = iA % TWOPOWER32; + iB1 = iB/TWOPOWER32; + iB0 = iB % TWOPOWER32; + if( iA1==0 ){ + if( iB1==0 ){ + *pA *= iB; + return 0; + } + r = iA0*iB1; + }else if( iB1==0 ){ + r = iA1*iB0; + }else{ + /* If both iA1 and iB1 are non-zero, overflow will result */ + return 1; + } + testcase( r==(-TWOPOWER31)-1 ); + testcase( r==(-TWOPOWER31) ); + testcase( r==TWOPOWER31 ); + testcase( r==TWOPOWER31-1 ); + if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; + r *= TWOPOWER32; + if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; + *pA = r; + return 0; +} + +/* +** Compute the absolute value of a 32-bit signed integer, of possible. Or +** if the integer has a value of -2147483648, return +2147483647 +*/ +SQLITE_PRIVATE int sqlite3AbsInt32(int x){ + if( x>=0 ) return x; + if( x==(int)0x80000000 ) return 0x7fffffff; + return -x; +} + +#ifdef SQLITE_ENABLE_8_3_NAMES +/* +** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database +** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and +** if filename in z[] has a suffix (a.k.a. "extension") that is longer than +** three characters, then shorten the suffix on z[] to be the last three +** characters of the original suffix. +** +** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always +** do the suffix shortening regardless of URI parameter. +** +** Examples: +** +** test.db-journal => test.nal +** test.db-wal => test.wal +** test.db-shm => test.shm +** test.db-mj7f3319fa => test.9fa +*/ +SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ +#if SQLITE_ENABLE_8_3_NAMES<2 + if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) +#endif + { + int i, sz; + sz = sqlite3Strlen30(z); + for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} + if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); + } +} +#endif + +/* +** Find (an approximate) sum of two LogEst values. This computation is +** not a simple "+" operator because LogEst is stored as a logarithmic +** value. +** +*/ +SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ + static const unsigned char x[] = { + 10, 10, /* 0,1 */ + 9, 9, /* 2,3 */ + 8, 8, /* 4,5 */ + 7, 7, 7, /* 6,7,8 */ + 6, 6, 6, /* 9,10,11 */ + 5, 5, 5, /* 12-14 */ + 4, 4, 4, 4, /* 15-18 */ + 3, 3, 3, 3, 3, 3, /* 19-24 */ + 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ + }; + if( a>=b ){ + if( a>b+49 ) return a; + if( a>b+31 ) return a+1; + return a+x[a-b]; + }else{ + if( b>a+49 ) return b; + if( b>a+31 ) return b+1; + return b+x[b-a]; + } +} + +/* +** Convert an integer into a LogEst. In other words, compute an +** approximation for 10*log2(x). +*/ +SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){ + static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; + LogEst y = 40; + if( x<8 ){ + if( x<2 ) return 0; + while( x<8 ){ y -= 10; x <<= 1; } + }else{ + while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ + while( x>15 ){ y += 10; x >>= 1; } + } + return a[x&7] + y - 10; +} + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Convert a double into a LogEst +** In other words, compute an approximation for 10*log2(x). +*/ +SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){ + u64 a; + LogEst e; + assert( sizeof(x)==8 && sizeof(a)==8 ); + if( x<=1 ) return 0; + if( x<=2000000000 ) return sqlite3LogEst((u64)x); + memcpy(&a, &x, 8); + e = (a>>52) - 1022; + return e*10; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ + defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ + defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) +/* +** Convert a LogEst into an integer. +** +** Note that this routine is only used when one or more of various +** non-standard compile-time options is enabled. +*/ +SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){ + u64 n; + n = x%10; + x /= 10; + if( n>=5 ) n -= 2; + else if( n>=1 ) n -= 1; +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ + defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) + if( x>60 ) return (u64)LARGEST_INT64; +#else + /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input + ** possible to this routine is 310, resulting in a maximum x of 31 */ + assert( x<=60 ); +#endif + return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); +} +#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */ + +/************** End of util.c ************************************************/ +/************** Begin file hash.c ********************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the implementation of generic hash-tables +** used in SQLite. +*/ +/* #include "sqliteInt.h" */ +/* #include */ + +/* Turn bulk memory into a hash table object by initializing the +** fields of the Hash structure. +** +** "pNew" is a pointer to the hash table that is to be initialized. +*/ +SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){ + assert( pNew!=0 ); + pNew->first = 0; + pNew->count = 0; + pNew->htsize = 0; + pNew->ht = 0; +} + +/* Remove all entries from a hash table. Reclaim all memory. +** Call this routine to delete a hash table or to reset a hash table +** to the empty state. +*/ +SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){ + HashElem *elem; /* For looping over all elements of the table */ + + assert( pH!=0 ); + elem = pH->first; + pH->first = 0; + sqlite3_free(pH->ht); + pH->ht = 0; + pH->htsize = 0; + while( elem ){ + HashElem *next_elem = elem->next; + sqlite3_free(elem); + elem = next_elem; + } + pH->count = 0; +} + +/* +** The hashing function. +*/ +static unsigned int strHash(const char *z){ + unsigned int h = 0; + unsigned char c; + while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/ + h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; + } + return h; +} + + +/* Link pNew element into the hash table pH. If pEntry!=0 then also +** insert pNew into the pEntry hash bucket. +*/ +static void insertElement( + Hash *pH, /* The complete hash table */ + struct _ht *pEntry, /* The entry into which pNew is inserted */ + HashElem *pNew /* The element to be inserted */ +){ + HashElem *pHead; /* First element already in pEntry */ + if( pEntry ){ + pHead = pEntry->count ? pEntry->chain : 0; + pEntry->count++; + pEntry->chain = pNew; + }else{ + pHead = 0; + } + if( pHead ){ + pNew->next = pHead; + pNew->prev = pHead->prev; + if( pHead->prev ){ pHead->prev->next = pNew; } + else { pH->first = pNew; } + pHead->prev = pNew; + }else{ + pNew->next = pH->first; + if( pH->first ){ pH->first->prev = pNew; } + pNew->prev = 0; + pH->first = pNew; + } +} + + +/* Resize the hash table so that it cantains "new_size" buckets. +** +** The hash table might fail to resize if sqlite3_malloc() fails or +** if the new size is the same as the prior size. +** Return TRUE if the resize occurs and false if not. +*/ +static int rehash(Hash *pH, unsigned int new_size){ + struct _ht *new_ht; /* The new hash table */ + HashElem *elem, *next_elem; /* For looping over existing elements */ + +#if SQLITE_MALLOC_SOFT_LIMIT>0 + if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ + new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); + } + if( new_size==pH->htsize ) return 0; +#endif + + /* The inability to allocates space for a larger hash table is + ** a performance hit but it is not a fatal error. So mark the + ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of + ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() + ** only zeroes the requested number of bytes whereas this module will + ** use the actual amount of space allocated for the hash table (which + ** may be larger than the requested amount). + */ + sqlite3BeginBenignMalloc(); + new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); + sqlite3EndBenignMalloc(); + + if( new_ht==0 ) return 0; + sqlite3_free(pH->ht); + pH->ht = new_ht; + pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); + memset(new_ht, 0, new_size*sizeof(struct _ht)); + for(elem=pH->first, pH->first=0; elem; elem = next_elem){ + unsigned int h = strHash(elem->pKey) % new_size; + next_elem = elem->next; + insertElement(pH, &new_ht[h], elem); + } + return 1; +} + +/* This function (for internal use only) locates an element in an +** hash table that matches the given key. The hash for this key is +** also computed and returned in the *pH parameter. +*/ +static HashElem *findElementWithHash( + const Hash *pH, /* The pH to be searched */ + const char *pKey, /* The key we are searching for */ + unsigned int *pHash /* Write the hash value here */ +){ + HashElem *elem; /* Used to loop thru the element list */ + int count; /* Number of elements left to test */ + unsigned int h; /* The computed hash */ + + if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ + struct _ht *pEntry; + h = strHash(pKey) % pH->htsize; + pEntry = &pH->ht[h]; + elem = pEntry->chain; + count = pEntry->count; + }else{ + h = 0; + elem = pH->first; + count = pH->count; + } + *pHash = h; + while( count-- ){ + assert( elem!=0 ); + if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ + return elem; + } + elem = elem->next; + } + return 0; +} + +/* Remove a single entry from the hash table given a pointer to that +** element and a hash on the element's key. +*/ +static void removeElementGivenHash( + Hash *pH, /* The pH containing "elem" */ + HashElem* elem, /* The element to be removed from the pH */ + unsigned int h /* Hash value for the element */ +){ + struct _ht *pEntry; + if( elem->prev ){ + elem->prev->next = elem->next; + }else{ + pH->first = elem->next; + } + if( elem->next ){ + elem->next->prev = elem->prev; + } + if( pH->ht ){ + pEntry = &pH->ht[h]; + if( pEntry->chain==elem ){ + pEntry->chain = elem->next; + } + pEntry->count--; + assert( pEntry->count>=0 ); + } + sqlite3_free( elem ); + pH->count--; + if( pH->count==0 ){ + assert( pH->first==0 ); + assert( pH->count==0 ); + sqlite3HashClear(pH); + } +} + +/* Attempt to locate an element of the hash table pH with a key +** that matches pKey. Return the data for this element if it is +** found, or NULL if there is no match. +*/ +SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){ + HashElem *elem; /* The element that matches key */ + unsigned int h; /* A hash on key */ + + assert( pH!=0 ); + assert( pKey!=0 ); + elem = findElementWithHash(pH, pKey, &h); + return elem ? elem->data : 0; +} + +/* Insert an element into the hash table pH. The key is pKey +** and the data is "data". +** +** If no element exists with a matching key, then a new +** element is created and NULL is returned. +** +** If another element already exists with the same key, then the +** new data replaces the old data and the old data is returned. +** The key is not copied in this instance. If a malloc fails, then +** the new data is returned and the hash table is unchanged. +** +** If the "data" parameter to this function is NULL, then the +** element corresponding to "key" is removed from the hash table. +*/ +SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ + unsigned int h; /* the hash of the key modulo hash table size */ + HashElem *elem; /* Used to loop thru the element list */ + HashElem *new_elem; /* New element added to the pH */ + + assert( pH!=0 ); + assert( pKey!=0 ); + elem = findElementWithHash(pH,pKey,&h); + if( elem ){ + void *old_data = elem->data; + if( data==0 ){ + removeElementGivenHash(pH,elem,h); + }else{ + elem->data = data; + elem->pKey = pKey; + } + return old_data; + } + if( data==0 ) return 0; + new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); + if( new_elem==0 ) return data; + new_elem->pKey = pKey; + new_elem->data = data; + pH->count++; + if( pH->count>=10 && pH->count > 2*pH->htsize ){ + if( rehash(pH, pH->count*2) ){ + assert( pH->htsize>0 ); + h = strHash(pKey) % pH->htsize; + } + } + insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); + return 0; +} + +/************** End of hash.c ************************************************/ +/************** Begin file opcodes.c *****************************************/ +/* Automatically generated. Do not edit */ +/* See the tool/mkopcodec.tcl script for details. */ +#if !defined(SQLITE_OMIT_EXPLAIN) \ + || defined(VDBE_PROFILE) \ + || defined(SQLITE_DEBUG) +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG) +# define OpHelp(X) "\0" X +#else +# define OpHelp(X) +#endif +SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + static const char *const azName[] = { + /* 0 */ "Savepoint" OpHelp(""), + /* 1 */ "AutoCommit" OpHelp(""), + /* 2 */ "Transaction" OpHelp(""), + /* 3 */ "SorterNext" OpHelp(""), + /* 4 */ "PrevIfOpen" OpHelp(""), + /* 5 */ "NextIfOpen" OpHelp(""), + /* 6 */ "Prev" OpHelp(""), + /* 7 */ "Next" OpHelp(""), + /* 8 */ "Checkpoint" OpHelp(""), + /* 9 */ "JournalMode" OpHelp(""), + /* 10 */ "Vacuum" OpHelp(""), + /* 11 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), + /* 12 */ "VUpdate" OpHelp("data=r[P3@P2]"), + /* 13 */ "Goto" OpHelp(""), + /* 14 */ "Gosub" OpHelp(""), + /* 15 */ "InitCoroutine" OpHelp(""), + /* 16 */ "Yield" OpHelp(""), + /* 17 */ "MustBeInt" OpHelp(""), + /* 18 */ "Jump" OpHelp(""), + /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), + /* 20 */ "Once" OpHelp(""), + /* 21 */ "If" OpHelp(""), + /* 22 */ "IfNot" OpHelp(""), + /* 23 */ "SeekLT" OpHelp("key=r[P3@P4]"), + /* 24 */ "SeekLE" OpHelp("key=r[P3@P4]"), + /* 25 */ "SeekGE" OpHelp("key=r[P3@P4]"), + /* 26 */ "SeekGT" OpHelp("key=r[P3@P4]"), + /* 27 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), + /* 28 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), + /* 29 */ "NoConflict" OpHelp("key=r[P3@P4]"), + /* 30 */ "NotFound" OpHelp("key=r[P3@P4]"), + /* 31 */ "Found" OpHelp("key=r[P3@P4]"), + /* 32 */ "NotExists" OpHelp("intkey=r[P3]"), + /* 33 */ "Last" OpHelp(""), + /* 34 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), + /* 35 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), + /* 36 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), + /* 37 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), + /* 38 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), + /* 39 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), + /* 40 */ "Lt" OpHelp("if r[P1]=r[P3] goto P2"), + /* 42 */ "SorterSort" OpHelp(""), + /* 43 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), + /* 44 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), + /* 45 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<>r[P1]"), + /* 47 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), + /* 48 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), + /* 49 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), + /* 50 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), + /* 51 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), + /* 52 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), + /* 53 */ "Sort" OpHelp(""), + /* 54 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), + /* 55 */ "Rewind" OpHelp(""), + /* 56 */ "IdxLE" OpHelp("key=r[P3@P4]"), + /* 57 */ "IdxGT" OpHelp("key=r[P3@P4]"), + /* 58 */ "IdxLT" OpHelp("key=r[P3@P4]"), + /* 59 */ "IdxGE" OpHelp("key=r[P3@P4]"), + /* 60 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), + /* 61 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), + /* 62 */ "Program" OpHelp(""), + /* 63 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), + /* 64 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), + /* 65 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"), + /* 66 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), + /* 67 */ "IncrVacuum" OpHelp(""), + /* 68 */ "VNext" OpHelp(""), + /* 69 */ "Init" OpHelp("Start at P2"), + /* 70 */ "Return" OpHelp(""), + /* 71 */ "EndCoroutine" OpHelp(""), + /* 72 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), + /* 73 */ "Halt" OpHelp(""), + /* 74 */ "Integer" OpHelp("r[P2]=P1"), + /* 75 */ "Int64" OpHelp("r[P2]=P4"), + /* 76 */ "String" OpHelp("r[P2]='P4' (len=P1)"), + /* 77 */ "Null" OpHelp("r[P2..P3]=NULL"), + /* 78 */ "SoftNull" OpHelp("r[P1]=NULL"), + /* 79 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), + /* 80 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), + /* 81 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), + /* 82 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), + /* 83 */ "SCopy" OpHelp("r[P2]=r[P1]"), + /* 84 */ "IntCopy" OpHelp("r[P2]=r[P1]"), + /* 85 */ "ResultRow" OpHelp("output=r[P1@P2]"), + /* 86 */ "CollSeq" OpHelp(""), + /* 87 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"), + /* 88 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), + /* 89 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), + /* 90 */ "RealAffinity" OpHelp(""), + /* 91 */ "Cast" OpHelp("affinity(r[P1])"), + /* 92 */ "Permutation" OpHelp(""), + /* 93 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), + /* 94 */ "Column" OpHelp("r[P3]=PX"), + /* 95 */ "Affinity" OpHelp("affinity(r[P1@P2])"), + /* 96 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), + /* 97 */ "String8" OpHelp("r[P2]='P4'"), + /* 98 */ "Count" OpHelp("r[P2]=count()"), + /* 99 */ "ReadCookie" OpHelp(""), + /* 100 */ "SetCookie" OpHelp(""), + /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), + /* 102 */ "OpenRead" OpHelp("root=P2 iDb=P3"), + /* 103 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), + /* 104 */ "OpenAutoindex" OpHelp("nColumn=P2"), + /* 105 */ "OpenEphemeral" OpHelp("nColumn=P2"), + /* 106 */ "SorterOpen" OpHelp(""), + /* 107 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), + /* 108 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), + /* 109 */ "Close" OpHelp(""), + /* 110 */ "ColumnsUsed" OpHelp(""), + /* 111 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), + /* 112 */ "NewRowid" OpHelp("r[P2]=rowid"), + /* 113 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), + /* 114 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"), + /* 115 */ "Delete" OpHelp(""), + /* 116 */ "ResetCount" OpHelp(""), + /* 117 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), + /* 118 */ "SorterData" OpHelp("r[P2]=data"), + /* 119 */ "RowKey" OpHelp("r[P2]=key"), + /* 120 */ "RowData" OpHelp("r[P2]=data"), + /* 121 */ "Rowid" OpHelp("r[P2]=rowid"), + /* 122 */ "NullRow" OpHelp(""), + /* 123 */ "SorterInsert" OpHelp(""), + /* 124 */ "IdxInsert" OpHelp("key=r[P2]"), + /* 125 */ "IdxDelete" OpHelp("key=r[P2@P3]"), + /* 126 */ "Seek" OpHelp("Move P3 to P1.rowid"), + /* 127 */ "IdxRowid" OpHelp("r[P2]=rowid"), + /* 128 */ "Destroy" OpHelp(""), + /* 129 */ "Clear" OpHelp(""), + /* 130 */ "ResetSorter" OpHelp(""), + /* 131 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"), + /* 132 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"), + /* 133 */ "Real" OpHelp("r[P2]=P4"), + /* 134 */ "ParseSchema" OpHelp(""), + /* 135 */ "LoadAnalysis" OpHelp(""), + /* 136 */ "DropTable" OpHelp(""), + /* 137 */ "DropIndex" OpHelp(""), + /* 138 */ "DropTrigger" OpHelp(""), + /* 139 */ "IntegrityCk" OpHelp(""), + /* 140 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), + /* 141 */ "Param" OpHelp(""), + /* 142 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), + /* 143 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), + /* 144 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), + /* 145 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"), + /* 146 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), + /* 147 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), + /* 148 */ "Expire" OpHelp(""), + /* 149 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), + /* 150 */ "VBegin" OpHelp(""), + /* 151 */ "VCreate" OpHelp(""), + /* 152 */ "VDestroy" OpHelp(""), + /* 153 */ "VOpen" OpHelp(""), + /* 154 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), + /* 155 */ "VRename" OpHelp(""), + /* 156 */ "Pagecount" OpHelp(""), + /* 157 */ "MaxPgcnt" OpHelp(""), + /* 158 */ "CursorHint" OpHelp(""), + /* 159 */ "Noop" OpHelp(""), + /* 160 */ "Explain" OpHelp(""), + }; + return azName[i]; +} +#endif + +/************** End of opcodes.c *********************************************/ +/************** Begin file os_unix.c *****************************************/ +/* +** 2004 May 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains the VFS implementation for unix-like operating systems +** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. +** +** There are actually several different VFS implementations in this file. +** The differences are in the way that file locking is done. The default +** implementation uses Posix Advisory Locks. Alternative implementations +** use flock(), dot-files, various proprietary locking schemas, or simply +** skip locking all together. +** +** This source file is organized into divisions where the logic for various +** subfunctions is contained within the appropriate division. PLEASE +** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed +** in the correct division and should be clearly labeled. +** +** The layout of divisions is as follows: +** +** * General-purpose declarations and utility functions. +** * Unique file ID logic used by VxWorks. +** * Various locking primitive implementations (all except proxy locking): +** + for Posix Advisory Locks +** + for no-op locks +** + for dot-file locks +** + for flock() locking +** + for named semaphore locks (VxWorks only) +** + for AFP filesystem locks (MacOSX only) +** * sqlite3_file methods not associated with locking. +** * Definitions of sqlite3_io_methods objects for all locking +** methods plus "finder" functions for each locking method. +** * sqlite3_vfs method implementations. +** * Locking primitives for the proxy uber-locking-method. (MacOSX only) +** * Definitions of sqlite3_vfs objects for all locking methods +** plus implementations of sqlite3_os_init() and sqlite3_os_end(). +*/ +/* #include "sqliteInt.h" */ +#if SQLITE_OS_UNIX /* This file is used on unix only */ + +/* +** There are various methods for file locking used for concurrency +** control: +** +** 1. POSIX locking (the default), +** 2. No locking, +** 3. Dot-file locking, +** 4. flock() locking, +** 5. AFP locking (OSX only), +** 6. Named POSIX semaphores (VXWorks only), +** 7. proxy locking. (OSX only) +** +** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE +** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic +** selection of the appropriate locking style based on the filesystem +** where the database is located. +*/ +#if !defined(SQLITE_ENABLE_LOCKING_STYLE) +# if defined(__APPLE__) +# define SQLITE_ENABLE_LOCKING_STYLE 1 +# else +# define SQLITE_ENABLE_LOCKING_STYLE 0 +# endif +#endif + +/* Use pread() and pwrite() if they are available */ +#if defined(__APPLE__) +# define HAVE_PREAD 1 +# define HAVE_PWRITE 1 +#endif +#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) +# undef USE_PREAD +# define USE_PREAD64 1 +#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) +# undef USE_PREAD64 +# define USE_PREAD 1 +#endif + +/* +** standard include files. +*/ +#include +#include +#include +#include +/* #include */ +#include +#include +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +# include +#endif + +#if SQLITE_ENABLE_LOCKING_STYLE +# include +# include +# include +#endif /* SQLITE_ENABLE_LOCKING_STYLE */ + +#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ + (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) +# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ + && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) +# define HAVE_GETHOSTUUID 1 +# else +# warning "gethostuuid() is disabled." +# endif +#endif + + +#if OS_VXWORKS +/* # include */ +# include +# include +#endif /* OS_VXWORKS */ + +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE +# include +#endif + +#ifdef HAVE_UTIME +# include +#endif + +/* +** Allowed values of unixFile.fsFlags +*/ +#define SQLITE_FSFLAGS_IS_MSDOS 0x1 + +/* +** If we are to be thread-safe, include the pthreads header and define +** the SQLITE_UNIX_THREADS macro. +*/ +#if SQLITE_THREADSAFE +/* # include */ +# define SQLITE_UNIX_THREADS 1 +#endif + +/* +** Default permissions when creating a new file +*/ +#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS +# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 +#endif + +/* +** Default permissions when creating auto proxy dir +*/ +#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS +# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 +#endif + +/* +** Maximum supported path-length. +*/ +#define MAX_PATHNAME 512 + +/* +** Maximum supported symbolic links +*/ +#define SQLITE_MAX_SYMLINKS 100 + +/* Always cast the getpid() return type for compatibility with +** kernel modules in VxWorks. */ +#define osGetpid(X) (pid_t)getpid() + +/* +** Only set the lastErrno if the error code is a real error and not +** a normal expected return code of SQLITE_BUSY or SQLITE_OK +*/ +#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) + +/* Forward references */ +typedef struct unixShm unixShm; /* Connection shared memory */ +typedef struct unixShmNode unixShmNode; /* Shared memory instance */ +typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ +typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ + +/* +** Sometimes, after a file handle is closed by SQLite, the file descriptor +** cannot be closed immediately. In these cases, instances of the following +** structure are used to store the file descriptor while waiting for an +** opportunity to either close or reuse it. +*/ +struct UnixUnusedFd { + int fd; /* File descriptor to close */ + int flags; /* Flags this file descriptor was opened with */ + UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ +}; + +/* +** The unixFile structure is subclass of sqlite3_file specific to the unix +** VFS implementations. +*/ +typedef struct unixFile unixFile; +struct unixFile { + sqlite3_io_methods const *pMethod; /* Always the first entry */ + sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ + unixInodeInfo *pInode; /* Info about locks on this inode */ + int h; /* The file descriptor */ + unsigned char eFileLock; /* The type of lock held on this fd */ + unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ + int lastErrno; /* The unix errno from last I/O error */ + void *lockingContext; /* Locking style specific state */ + UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ + const char *zPath; /* Name of the file */ + unixShm *pShm; /* Shared memory segment information */ + int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ +#if SQLITE_MAX_MMAP_SIZE>0 + int nFetchOut; /* Number of outstanding xFetch refs */ + sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ + sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ + sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ + void *pMapRegion; /* Memory mapped region */ +#endif +#ifdef __QNXNTO__ + int sectorSize; /* Device sector size */ + int deviceCharacteristics; /* Precomputed device characteristics */ +#endif +#if SQLITE_ENABLE_LOCKING_STYLE + int openFlags; /* The flags specified at open() */ +#endif +#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) + unsigned fsFlags; /* cached details from statfs() */ +#endif +#if OS_VXWORKS + struct vxworksFileId *pId; /* Unique file ID */ +#endif +#ifdef SQLITE_DEBUG + /* The next group of variables are used to track whether or not the + ** transaction counter in bytes 24-27 of database files are updated + ** whenever any part of the database changes. An assertion fault will + ** occur if a file is updated without also updating the transaction + ** counter. This test is made to avoid new problems similar to the + ** one described by ticket #3584. + */ + unsigned char transCntrChng; /* True if the transaction counter changed */ + unsigned char dbUpdate; /* True if any part of database file changed */ + unsigned char inNormalWrite; /* True if in a normal write operation */ + +#endif + +#ifdef SQLITE_TEST + /* In test mode, increase the size of this structure a bit so that + ** it is larger than the struct CrashFile defined in test6.c. + */ + char aPadding[32]; +#endif +}; + +/* This variable holds the process id (pid) from when the xRandomness() +** method was called. If xOpen() is called from a different process id, +** indicating that a fork() has occurred, the PRNG will be reset. +*/ +static pid_t randomnessPid = 0; + +/* +** Allowed values for the unixFile.ctrlFlags bitmask: +*/ +#define UNIXFILE_EXCL 0x01 /* Connections from one process only */ +#define UNIXFILE_RDONLY 0x02 /* Connection is read only */ +#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ +#ifndef SQLITE_DISABLE_DIRSYNC +# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ +#else +# define UNIXFILE_DIRSYNC 0x00 +#endif +#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ +#define UNIXFILE_DELETE 0x20 /* Delete on close */ +#define UNIXFILE_URI 0x40 /* Filename might have query parameters */ +#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ + +/* +** Include code that is common to all os_*.c files +*/ +/************** Include os_common.h in the middle of os_unix.c ***************/ +/************** Begin file os_common.h ***************************************/ +/* +** 2004 May 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains macros and a little bit of code that is common to +** all of the platform-specific files (os_*.c) and is #included into those +** files. +** +** This file should be #included by the os_*.c files only. It is not a +** general purpose header file. +*/ +#ifndef _OS_COMMON_H_ +#define _OS_COMMON_H_ + +/* +** At least two bugs have slipped in because we changed the MEMORY_DEBUG +** macro to SQLITE_DEBUG and some older makefiles have not yet made the +** switch. The following code should catch this problem at compile-time. +*/ +#ifdef MEMORY_DEBUG +# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." +#endif + +/* +** Macros for performance tracing. Normally turned off. Only works +** on i486 hardware. +*/ +#ifdef SQLITE_PERFORMANCE_TRACE + +/* +** hwtime.h contains inline assembler code for implementing +** high-performance timing routines. +*/ +/************** Include hwtime.h in the middle of os_common.h ****************/ +/************** Begin file hwtime.h ******************************************/ +/* +** 2008 May 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains inline asm code for retrieving "high-performance" +** counters for x86 class CPUs. +*/ +#ifndef _HWTIME_H_ +#define _HWTIME_H_ + +/* +** The following routine only works on pentium-class (or newer) processors. +** It uses the RDTSC opcode to read the cycle count value out of the +** processor and returns that value. This can be used for high-res +** profiling. +*/ +#if (defined(__GNUC__) || defined(_MSC_VER)) && \ + (defined(i386) || defined(__i386__) || defined(_M_IX86)) + + #if defined(__GNUC__) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned int lo, hi; + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return (sqlite_uint64)hi << 32 | lo; + } + + #elif defined(_MSC_VER) + + __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ + __asm { + rdtsc + ret ; return value at EDX:EAX + } + } + + #endif + +#elif (defined(__GNUC__) && defined(__x86_64__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long val; + __asm__ __volatile__ ("rdtsc" : "=A" (val)); + return val; + } + +#elif (defined(__GNUC__) && defined(__ppc__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long long retval; + unsigned long junk; + __asm__ __volatile__ ("\n\ + 1: mftbu %1\n\ + mftb %L0\n\ + mftbu %0\n\ + cmpw %0,%1\n\ + bne 1b" + : "=r" (retval), "=r" (junk)); + return retval; + } + +#else + + #error Need implementation of sqlite3Hwtime() for your platform. + + /* + ** To compile without implementing sqlite3Hwtime() for your platform, + ** you can remove the above #error and use the following + ** stub function. You will lose timing support for many + ** of the debugging and testing utilities, but it should at + ** least compile and run. + */ +SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + +#endif + +#endif /* !defined(_HWTIME_H_) */ + +/************** End of hwtime.h **********************************************/ +/************** Continuing where we left off in os_common.h ******************/ + +static sqlite_uint64 g_start; +static sqlite_uint64 g_elapsed; +#define TIMER_START g_start=sqlite3Hwtime() +#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start +#define TIMER_ELAPSED g_elapsed +#else +#define TIMER_START +#define TIMER_END +#define TIMER_ELAPSED ((sqlite_uint64)0) +#endif + +/* +** If we compile with the SQLITE_TEST macro set, then the following block +** of code will give us the ability to simulate a disk I/O error. This +** is used for testing the I/O recovery logic. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_io_error_hit; +SQLITE_API extern int sqlite3_io_error_hardhit; +SQLITE_API extern int sqlite3_io_error_pending; +SQLITE_API extern int sqlite3_io_error_persist; +SQLITE_API extern int sqlite3_io_error_benign; +SQLITE_API extern int sqlite3_diskfull_pending; +SQLITE_API extern int sqlite3_diskfull; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) +#define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } +static void local_ioerr(){ + IOTRACE(("IOERR\n")); + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; +} +#define SimulateDiskfullError(CODE) \ + if( sqlite3_diskfull_pending ){ \ + if( sqlite3_diskfull_pending == 1 ){ \ + local_ioerr(); \ + sqlite3_diskfull = 1; \ + sqlite3_io_error_hit = 1; \ + CODE; \ + }else{ \ + sqlite3_diskfull_pending--; \ + } \ + } +#else +#define SimulateIOErrorBenign(X) +#define SimulateIOError(A) +#define SimulateDiskfullError(A) +#endif /* defined(SQLITE_TEST) */ + +/* +** When testing, keep a count of the number of open files. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_open_file_count; +#define OpenCounter(X) sqlite3_open_file_count+=(X) +#else +#define OpenCounter(X) +#endif /* defined(SQLITE_TEST) */ + +#endif /* !defined(_OS_COMMON_H_) */ + +/************** End of os_common.h *******************************************/ +/************** Continuing where we left off in os_unix.c ********************/ + +/* +** Define various macros that are missing from some systems. +*/ +#ifndef O_LARGEFILE +# define O_LARGEFILE 0 +#endif +#ifdef SQLITE_DISABLE_LFS +# undef O_LARGEFILE +# define O_LARGEFILE 0 +#endif +#ifndef O_NOFOLLOW +# define O_NOFOLLOW 0 +#endif +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +/* +** The threadid macro resolves to the thread-id or to 0. Used for +** testing and debugging only. +*/ +#if SQLITE_THREADSAFE +#define threadid pthread_self() +#else +#define threadid 0 +#endif + +/* +** HAVE_MREMAP defaults to true on Linux and false everywhere else. +*/ +#if !defined(HAVE_MREMAP) +# if defined(__linux__) && defined(_GNU_SOURCE) +# define HAVE_MREMAP 1 +# else +# define HAVE_MREMAP 0 +# endif +#endif + +/* +** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() +** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. +*/ +#ifdef __ANDROID__ +# define lseek lseek64 +#endif + +/* +** Different Unix systems declare open() in different ways. Same use +** open(const char*,int,mode_t). Others use open(const char*,int,...). +** The difference is important when using a pointer to the function. +** +** The safest way to deal with the problem is to always use this wrapper +** which always has the same well-defined interface. +*/ +static int posixOpen(const char *zFile, int flags, int mode){ + return open(zFile, flags, mode); +} + +/* Forward reference */ +static int openDirectory(const char*, int*); +static int unixGetpagesize(void); + +/* +** Many system calls are accessed through pointer-to-functions so that +** they may be overridden at runtime to facilitate fault injection during +** testing and sandboxing. The following array holds the names and pointers +** to all overrideable system calls. +*/ +static struct unix_syscall { + const char *zName; /* Name of the system call */ + sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ + sqlite3_syscall_ptr pDefault; /* Default value */ +} aSyscall[] = { + { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, +#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) + + { "close", (sqlite3_syscall_ptr)close, 0 }, +#define osClose ((int(*)(int))aSyscall[1].pCurrent) + + { "access", (sqlite3_syscall_ptr)access, 0 }, +#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) + + { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, +#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) + + { "stat", (sqlite3_syscall_ptr)stat, 0 }, +#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) + +/* +** The DJGPP compiler environment looks mostly like Unix, but it +** lacks the fcntl() system call. So redefine fcntl() to be something +** that always succeeds. This means that locking does not occur under +** DJGPP. But it is DOS - what did you expect? +*/ +#ifdef __DJGPP__ + { "fstat", 0, 0 }, +#define osFstat(a,b,c) 0 +#else + { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, +#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) +#endif + + { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, +#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) + + { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, +#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) + + { "read", (sqlite3_syscall_ptr)read, 0 }, +#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) + +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE + { "pread", (sqlite3_syscall_ptr)pread, 0 }, +#else + { "pread", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) + +#if defined(USE_PREAD64) + { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, +#else + { "pread64", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) + + { "write", (sqlite3_syscall_ptr)write, 0 }, +#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) + +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE + { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, +#else + { "pwrite", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ + aSyscall[12].pCurrent) + +#if defined(USE_PREAD64) + { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, +#else + { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ + aSyscall[13].pCurrent) + + { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, +#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) + +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE + { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, +#else + { "fallocate", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) + + { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, +#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) + + { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, +#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) + + { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, +#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) + + { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, +#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) + +#if defined(HAVE_FCHOWN) + { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, +#else + { "fchown", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) + + { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, +#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) + +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, +#else + { "mmap", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) + +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, +#else + { "munmap", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent) + +#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) + { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, +#else + { "mremap", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) + +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, +#else + { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) + +#if defined(HAVE_READLINK) + { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, +#else + { "readlink", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) + +#if defined(HAVE_LSTAT) + { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, +#else + { "lstat", (sqlite3_syscall_ptr)0, 0 }, +#endif +#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) + +}; /* End of the overrideable system calls */ + + +/* +** On some systems, calls to fchown() will trigger a message in a security +** log if they come from non-root processes. So avoid calling fchown() if +** we are not running as root. +*/ +static int robustFchown(int fd, uid_t uid, gid_t gid){ +#if defined(HAVE_FCHOWN) + return osGeteuid() ? 0 : osFchown(fd,uid,gid); +#else + return 0; +#endif +} + +/* +** This is the xSetSystemCall() method of sqlite3_vfs for all of the +** "unix" VFSes. Return SQLITE_OK opon successfully updating the +** system call pointer, or SQLITE_NOTFOUND if there is no configurable +** system call named zName. +*/ +static int unixSetSystemCall( + sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ + const char *zName, /* Name of system call to override */ + sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ +){ + unsigned int i; + int rc = SQLITE_NOTFOUND; + + UNUSED_PARAMETER(pNotUsed); + if( zName==0 ){ + /* If no zName is given, restore all system calls to their default + ** settings and return NULL + */ + rc = SQLITE_OK; + for(i=0; i=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; + osClose(fd); + sqlite3_log(SQLITE_WARNING, + "attempt to open \"%s\" as file descriptor %d", z, fd); + fd = -1; + if( osOpen("/dev/null", f, m)<0 ) break; + } + if( fd>=0 ){ + if( m!=0 ){ + struct stat statbuf; + if( osFstat(fd, &statbuf)==0 + && statbuf.st_size==0 + && (statbuf.st_mode&0777)!=m + ){ + osFchmod(fd, m); + } + } +#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) + osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); +#endif + } + return fd; +} + +/* +** Helper functions to obtain and relinquish the global mutex. The +** global mutex is used to protect the unixInodeInfo and +** vxworksFileId objects used by this file, all of which may be +** shared by multiple threads. +** +** Function unixMutexHeld() is used to assert() that the global mutex +** is held when required. This function is only used as part of assert() +** statements. e.g. +** +** unixEnterMutex() +** assert( unixMutexHeld() ); +** unixEnterLeave() +*/ +static void unixEnterMutex(void){ + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +static void unixLeaveMutex(void){ + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +#ifdef SQLITE_DEBUG +static int unixMutexHeld(void) { + return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +#endif + + +#ifdef SQLITE_HAVE_OS_TRACE +/* +** Helper function for printing out trace information from debugging +** binaries. This returns the string representation of the supplied +** integer lock-type. +*/ +static const char *azFileLock(int eFileLock){ + switch( eFileLock ){ + case NO_LOCK: return "NONE"; + case SHARED_LOCK: return "SHARED"; + case RESERVED_LOCK: return "RESERVED"; + case PENDING_LOCK: return "PENDING"; + case EXCLUSIVE_LOCK: return "EXCLUSIVE"; + } + return "ERROR"; +} +#endif + +#ifdef SQLITE_LOCK_TRACE +/* +** Print out information about all locking operations. +** +** This routine is used for troubleshooting locks on multithreaded +** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE +** command-line option on the compiler. This code is normally +** turned off. +*/ +static int lockTrace(int fd, int op, struct flock *p){ + char *zOpName, *zType; + int s; + int savedErrno; + if( op==F_GETLK ){ + zOpName = "GETLK"; + }else if( op==F_SETLK ){ + zOpName = "SETLK"; + }else{ + s = osFcntl(fd, op, p); + sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); + return s; + } + if( p->l_type==F_RDLCK ){ + zType = "RDLCK"; + }else if( p->l_type==F_WRLCK ){ + zType = "WRLCK"; + }else if( p->l_type==F_UNLCK ){ + zType = "UNLCK"; + }else{ + assert( 0 ); + } + assert( p->l_whence==SEEK_SET ); + s = osFcntl(fd, op, p); + savedErrno = errno; + sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", + threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, + (int)p->l_pid, s); + if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ + struct flock l2; + l2 = *p; + osFcntl(fd, F_GETLK, &l2); + if( l2.l_type==F_RDLCK ){ + zType = "RDLCK"; + }else if( l2.l_type==F_WRLCK ){ + zType = "WRLCK"; + }else if( l2.l_type==F_UNLCK ){ + zType = "UNLCK"; + }else{ + assert( 0 ); + } + sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", + zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); + } + errno = savedErrno; + return s; +} +#undef osFcntl +#define osFcntl lockTrace +#endif /* SQLITE_LOCK_TRACE */ + +/* +** Retry ftruncate() calls that fail due to EINTR +** +** All calls to ftruncate() within this file should be made through +** this wrapper. On the Android platform, bypassing the logic below +** could lead to a corrupt database. +*/ +static int robust_ftruncate(int h, sqlite3_int64 sz){ + int rc; +#ifdef __ANDROID__ + /* On Android, ftruncate() always uses 32-bit offsets, even if + ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to + ** truncate a file to any size larger than 2GiB. Silently ignore any + ** such attempts. */ + if( sz>(sqlite3_int64)0x7FFFFFFF ){ + rc = SQLITE_OK; + }else +#endif + do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); + return rc; +} + +/* +** This routine translates a standard POSIX errno code into something +** useful to the clients of the sqlite3 functions. Specifically, it is +** intended to translate a variety of "try again" errors into SQLITE_BUSY +** and a variety of "please close the file descriptor NOW" errors into +** SQLITE_IOERR +** +** Errors during initialization of locks, or file system support for locks, +** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. +*/ +static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { + assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || + (sqliteIOErr == SQLITE_IOERR_UNLOCK) || + (sqliteIOErr == SQLITE_IOERR_RDLOCK) || + (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); + switch (posixError) { + case EACCES: + case EAGAIN: + case ETIMEDOUT: + case EBUSY: + case EINTR: + case ENOLCK: + /* random NFS retry error, unless during file system support + * introspection, in which it actually means what it says */ + return SQLITE_BUSY; + + case EPERM: + return SQLITE_PERM; + + default: + return sqliteIOErr; + } +} + + +/****************************************************************************** +****************** Begin Unique File ID Utility Used By VxWorks *************** +** +** On most versions of unix, we can get a unique ID for a file by concatenating +** the device number and the inode number. But this does not work on VxWorks. +** On VxWorks, a unique file id must be based on the canonical filename. +** +** A pointer to an instance of the following structure can be used as a +** unique file ID in VxWorks. Each instance of this structure contains +** a copy of the canonical filename. There is also a reference count. +** The structure is reclaimed when the number of pointers to it drops to +** zero. +** +** There are never very many files open at one time and lookups are not +** a performance-critical path, so it is sufficient to put these +** structures on a linked list. +*/ +struct vxworksFileId { + struct vxworksFileId *pNext; /* Next in a list of them all */ + int nRef; /* Number of references to this one */ + int nName; /* Length of the zCanonicalName[] string */ + char *zCanonicalName; /* Canonical filename */ +}; + +#if OS_VXWORKS +/* +** All unique filenames are held on a linked list headed by this +** variable: +*/ +static struct vxworksFileId *vxworksFileList = 0; + +/* +** Simplify a filename into its canonical form +** by making the following changes: +** +** * removing any trailing and duplicate / +** * convert /./ into just / +** * convert /A/../ where A is any simple name into just / +** +** Changes are made in-place. Return the new name length. +** +** The original filename is in z[0..n-1]. Return the number of +** characters in the simplified name. +*/ +static int vxworksSimplifyName(char *z, int n){ + int i, j; + while( n>1 && z[n-1]=='/' ){ n--; } + for(i=j=0; i0 && z[j-1]!='/' ){ j--; } + if( j>0 ){ j--; } + i += 2; + continue; + } + } + z[j++] = z[i]; + } + z[j] = 0; + return j; +} + +/* +** Find a unique file ID for the given absolute pathname. Return +** a pointer to the vxworksFileId object. This pointer is the unique +** file ID. +** +** The nRef field of the vxworksFileId object is incremented before +** the object is returned. A new vxworksFileId object is created +** and added to the global list if necessary. +** +** If a memory allocation error occurs, return NULL. +*/ +static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ + struct vxworksFileId *pNew; /* search key and new file ID */ + struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ + int n; /* Length of zAbsoluteName string */ + + assert( zAbsoluteName[0]=='/' ); + n = (int)strlen(zAbsoluteName); + pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); + if( pNew==0 ) return 0; + pNew->zCanonicalName = (char*)&pNew[1]; + memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); + n = vxworksSimplifyName(pNew->zCanonicalName, n); + + /* Search for an existing entry that matching the canonical name. + ** If found, increment the reference count and return a pointer to + ** the existing file ID. + */ + unixEnterMutex(); + for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ + if( pCandidate->nName==n + && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 + ){ + sqlite3_free(pNew); + pCandidate->nRef++; + unixLeaveMutex(); + return pCandidate; + } + } + + /* No match was found. We will make a new file ID */ + pNew->nRef = 1; + pNew->nName = n; + pNew->pNext = vxworksFileList; + vxworksFileList = pNew; + unixLeaveMutex(); + return pNew; +} + +/* +** Decrement the reference count on a vxworksFileId object. Free +** the object when the reference count reaches zero. +*/ +static void vxworksReleaseFileId(struct vxworksFileId *pId){ + unixEnterMutex(); + assert( pId->nRef>0 ); + pId->nRef--; + if( pId->nRef==0 ){ + struct vxworksFileId **pp; + for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} + assert( *pp==pId ); + *pp = pId->pNext; + sqlite3_free(pId); + } + unixLeaveMutex(); +} +#endif /* OS_VXWORKS */ +/*************** End of Unique File ID Utility Used By VxWorks **************** +******************************************************************************/ + + +/****************************************************************************** +*************************** Posix Advisory Locking **************************** +** +** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) +** section 6.5.2.2 lines 483 through 490 specify that when a process +** sets or clears a lock, that operation overrides any prior locks set +** by the same process. It does not explicitly say so, but this implies +** that it overrides locks set by the same process using a different +** file descriptor. Consider this test case: +** +** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); +** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); +** +** Suppose ./file1 and ./file2 are really the same file (because +** one is a hard or symbolic link to the other) then if you set +** an exclusive lock on fd1, then try to get an exclusive lock +** on fd2, it works. I would have expected the second lock to +** fail since there was already a lock on the file due to fd1. +** But not so. Since both locks came from the same process, the +** second overrides the first, even though they were on different +** file descriptors opened on different file names. +** +** This means that we cannot use POSIX locks to synchronize file access +** among competing threads of the same process. POSIX locks will work fine +** to synchronize access for threads in separate processes, but not +** threads within the same process. +** +** To work around the problem, SQLite has to manage file locks internally +** on its own. Whenever a new database is opened, we have to find the +** specific inode of the database file (the inode is determined by the +** st_dev and st_ino fields of the stat structure that fstat() fills in) +** and check for locks already existing on that inode. When locks are +** created or removed, we have to look at our own internal record of the +** locks to see if another thread has previously set a lock on that same +** inode. +** +** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. +** For VxWorks, we have to use the alternative unique ID system based on +** canonical filename and implemented in the previous division.) +** +** The sqlite3_file structure for POSIX is no longer just an integer file +** descriptor. It is now a structure that holds the integer file +** descriptor and a pointer to a structure that describes the internal +** locks on the corresponding inode. There is one locking structure +** per inode, so if the same inode is opened twice, both unixFile structures +** point to the same locking structure. The locking structure keeps +** a reference count (so we will know when to delete it) and a "cnt" +** field that tells us its internal lock status. cnt==0 means the +** file is unlocked. cnt==-1 means the file has an exclusive lock. +** cnt>0 means there are cnt shared locks on the file. +** +** Any attempt to lock or unlock a file first checks the locking +** structure. The fcntl() system call is only invoked to set a +** POSIX lock if the internal lock structure transitions between +** a locked and an unlocked state. +** +** But wait: there are yet more problems with POSIX advisory locks. +** +** If you close a file descriptor that points to a file that has locks, +** all locks on that file that are owned by the current process are +** released. To work around this problem, each unixInodeInfo object +** maintains a count of the number of pending locks on tha inode. +** When an attempt is made to close an unixFile, if there are +** other unixFile open on the same inode that are holding locks, the call +** to close() the file descriptor is deferred until all of the locks clear. +** The unixInodeInfo structure keeps a list of file descriptors that need to +** be closed and that list is walked (and cleared) when the last lock +** clears. +** +** Yet another problem: LinuxThreads do not play well with posix locks. +** +** Many older versions of linux use the LinuxThreads library which is +** not posix compliant. Under LinuxThreads, a lock created by thread +** A cannot be modified or overridden by a different thread B. +** Only thread A can modify the lock. Locking behavior is correct +** if the appliation uses the newer Native Posix Thread Library (NPTL) +** on linux - with NPTL a lock created by thread A can override locks +** in thread B. But there is no way to know at compile-time which +** threading library is being used. So there is no way to know at +** compile-time whether or not thread A can override locks on thread B. +** One has to do a run-time check to discover the behavior of the +** current process. +** +** SQLite used to support LinuxThreads. But support for LinuxThreads +** was dropped beginning with version 3.7.0. SQLite will still work with +** LinuxThreads provided that (1) there is no more than one connection +** per database file in the same process and (2) database connections +** do not move across threads. +*/ + +/* +** An instance of the following structure serves as the key used +** to locate a particular unixInodeInfo object. +*/ +struct unixFileId { + dev_t dev; /* Device number */ +#if OS_VXWORKS + struct vxworksFileId *pId; /* Unique file ID for vxworks. */ +#else + ino_t ino; /* Inode number */ +#endif +}; + +/* +** An instance of the following structure is allocated for each open +** inode. Or, on LinuxThreads, there is one of these structures for +** each inode opened by each thread. +** +** A single inode can have multiple file descriptors, so each unixFile +** structure contains a pointer to an instance of this object and this +** object keeps a count of the number of unixFile pointing to it. +*/ +struct unixInodeInfo { + struct unixFileId fileId; /* The lookup key */ + int nShared; /* Number of SHARED locks held */ + unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ + unsigned char bProcessLock; /* An exclusive process lock is held */ + int nRef; /* Number of pointers to this structure */ + unixShmNode *pShmNode; /* Shared memory associated with this inode */ + int nLock; /* Number of outstanding file locks */ + UnixUnusedFd *pUnused; /* Unused file descriptors to close */ + unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ + unixInodeInfo *pPrev; /* .... doubly linked */ +#if SQLITE_ENABLE_LOCKING_STYLE + unsigned long long sharedByte; /* for AFP simulated shared lock */ +#endif +#if OS_VXWORKS + sem_t *pSem; /* Named POSIX semaphore */ + char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ +#endif +}; + +/* +** A lists of all unixInodeInfo objects. +*/ +static unixInodeInfo *inodeList = 0; + +/* +** +** This function - unixLogErrorAtLine(), is only ever called via the macro +** unixLogError(). +** +** It is invoked after an error occurs in an OS function and errno has been +** set. It logs a message using sqlite3_log() containing the current value of +** errno and, if possible, the human-readable equivalent from strerror() or +** strerror_r(). +** +** The first argument passed to the macro should be the error code that +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). +** The two subsequent arguments should be the name of the OS function that +** failed (e.g. "unlink", "open") and the associated file-system path, +** if any. +*/ +#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) +static int unixLogErrorAtLine( + int errcode, /* SQLite error code */ + const char *zFunc, /* Name of OS function that failed */ + const char *zPath, /* File path associated with error */ + int iLine /* Source line number where error occurred */ +){ + char *zErr; /* Message from strerror() or equivalent */ + int iErrno = errno; /* Saved syscall error number */ + + /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use + ** the strerror() function to obtain the human-readable error message + ** equivalent to errno. Otherwise, use strerror_r(). + */ +#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) + char aErr[80]; + memset(aErr, 0, sizeof(aErr)); + zErr = aErr; + + /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, + ** assume that the system provides the GNU version of strerror_r() that + ** returns a pointer to a buffer containing the error message. That pointer + ** may point to aErr[], or it may point to some static storage somewhere. + ** Otherwise, assume that the system provides the POSIX version of + ** strerror_r(), which always writes an error message into aErr[]. + ** + ** If the code incorrectly assumes that it is the POSIX version that is + ** available, the error message will often be an empty string. Not a + ** huge problem. Incorrectly concluding that the GNU version is available + ** could lead to a segfault though. + */ +#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) + zErr = +# endif + strerror_r(iErrno, aErr, sizeof(aErr)-1); + +#elif SQLITE_THREADSAFE + /* This is a threadsafe build, but strerror_r() is not available. */ + zErr = ""; +#else + /* Non-threadsafe build, use strerror(). */ + zErr = strerror(iErrno); +#endif + + if( zPath==0 ) zPath = ""; + sqlite3_log(errcode, + "os_unix.c:%d: (%d) %s(%s) - %s", + iLine, iErrno, zFunc, zPath, zErr + ); + + return errcode; +} + +/* +** Close a file descriptor. +** +** We assume that close() almost always works, since it is only in a +** very sick application or on a very sick platform that it might fail. +** If it does fail, simply leak the file descriptor, but do log the +** error. +** +** Note that it is not safe to retry close() after EINTR since the +** file descriptor might have already been reused by another thread. +** So we don't even try to recover from an EINTR. Just log the error +** and move on. +*/ +static void robust_close(unixFile *pFile, int h, int lineno){ + if( osClose(h) ){ + unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", + pFile ? pFile->zPath : 0, lineno); + } +} + +/* +** Set the pFile->lastErrno. Do this in a subroutine as that provides +** a convenient place to set a breakpoint. +*/ +static void storeLastErrno(unixFile *pFile, int error){ + pFile->lastErrno = error; +} + +/* +** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. +*/ +static void closePendingFds(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; + UnixUnusedFd *p; + UnixUnusedFd *pNext; + for(p=pInode->pUnused; p; p=pNext){ + pNext = p->pNext; + robust_close(pFile, p->fd, __LINE__); + sqlite3_free(p); + } + pInode->pUnused = 0; +} + +/* +** Release a unixInodeInfo structure previously allocated by findInodeInfo(). +** +** The mutex entered using the unixEnterMutex() function must be held +** when this function is called. +*/ +static void releaseInodeInfo(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; + assert( unixMutexHeld() ); + if( ALWAYS(pInode) ){ + pInode->nRef--; + if( pInode->nRef==0 ){ + assert( pInode->pShmNode==0 ); + closePendingFds(pFile); + if( pInode->pPrev ){ + assert( pInode->pPrev->pNext==pInode ); + pInode->pPrev->pNext = pInode->pNext; + }else{ + assert( inodeList==pInode ); + inodeList = pInode->pNext; + } + if( pInode->pNext ){ + assert( pInode->pNext->pPrev==pInode ); + pInode->pNext->pPrev = pInode->pPrev; + } + sqlite3_free(pInode); + } + } +} + +/* +** Given a file descriptor, locate the unixInodeInfo object that +** describes that file descriptor. Create a new one if necessary. The +** return value might be uninitialized if an error occurs. +** +** The mutex entered using the unixEnterMutex() function must be held +** when this function is called. +** +** Return an appropriate error code. +*/ +static int findInodeInfo( + unixFile *pFile, /* Unix file with file desc used in the key */ + unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ +){ + int rc; /* System call return code */ + int fd; /* The file descriptor for pFile */ + struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ + struct stat statbuf; /* Low-level file information */ + unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ + + assert( unixMutexHeld() ); + + /* Get low-level information about the file that we can used to + ** create a unique name for the file. + */ + fd = pFile->h; + rc = osFstat(fd, &statbuf); + if( rc!=0 ){ + storeLastErrno(pFile, errno); +#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) + if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; +#endif + return SQLITE_IOERR; + } + +#ifdef __APPLE__ + /* On OS X on an msdos filesystem, the inode number is reported + ** incorrectly for zero-size files. See ticket #3260. To work + ** around this problem (we consider it a bug in OS X, not SQLite) + ** we always increase the file size to 1 by writing a single byte + ** prior to accessing the inode number. The one byte written is + ** an ASCII 'S' character which also happens to be the first byte + ** in the header of every SQLite database. In this way, if there + ** is a race condition such that another thread has already populated + ** the first page of the database, no damage is done. + */ + if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ + do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); + if( rc!=1 ){ + storeLastErrno(pFile, errno); + return SQLITE_IOERR; + } + rc = osFstat(fd, &statbuf); + if( rc!=0 ){ + storeLastErrno(pFile, errno); + return SQLITE_IOERR; + } + } +#endif + + memset(&fileId, 0, sizeof(fileId)); + fileId.dev = statbuf.st_dev; +#if OS_VXWORKS + fileId.pId = pFile->pId; +#else + fileId.ino = statbuf.st_ino; +#endif + pInode = inodeList; + while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ + pInode = pInode->pNext; + } + if( pInode==0 ){ + pInode = sqlite3_malloc64( sizeof(*pInode) ); + if( pInode==0 ){ + return SQLITE_NOMEM_BKPT; + } + memset(pInode, 0, sizeof(*pInode)); + memcpy(&pInode->fileId, &fileId, sizeof(fileId)); + pInode->nRef = 1; + pInode->pNext = inodeList; + pInode->pPrev = 0; + if( inodeList ) inodeList->pPrev = pInode; + inodeList = pInode; + }else{ + pInode->nRef++; + } + *ppInode = pInode; + return SQLITE_OK; +} + +/* +** Return TRUE if pFile has been renamed or unlinked since it was first opened. +*/ +static int fileHasMoved(unixFile *pFile){ +#if OS_VXWORKS + return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; +#else + struct stat buf; + return pFile->pInode!=0 && + (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); +#endif +} + + +/* +** Check a unixFile that is a database. Verify the following: +** +** (1) There is exactly one hard link on the file +** (2) The file is not a symbolic link +** (3) The file has not been renamed or unlinked +** +** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. +*/ +static void verifyDbFile(unixFile *pFile){ + struct stat buf; + int rc; + + /* These verifications occurs for the main database only */ + if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; + + rc = osFstat(pFile->h, &buf); + if( rc!=0 ){ + sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); + return; + } + if( buf.st_nlink==0 ){ + sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); + return; + } + if( buf.st_nlink>1 ){ + sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); + return; + } + if( fileHasMoved(pFile) ){ + sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); + return; + } +} + + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +*/ +static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ + int rc = SQLITE_OK; + int reserved = 0; + unixFile *pFile = (unixFile*)id; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); + assert( pFile->eFileLock<=SHARED_LOCK ); + unixEnterMutex(); /* Because pFile->pInode is shared across threads */ + + /* Check if a thread in this process holds such a lock */ + if( pFile->pInode->eFileLock>SHARED_LOCK ){ + reserved = 1; + } + + /* Otherwise see if some other process holds it. + */ +#ifndef __DJGPP__ + if( !reserved && !pFile->pInode->bProcessLock ){ + struct flock lock; + lock.l_whence = SEEK_SET; + lock.l_start = RESERVED_BYTE; + lock.l_len = 1; + lock.l_type = F_WRLCK; + if( osFcntl(pFile->h, F_GETLK, &lock) ){ + rc = SQLITE_IOERR_CHECKRESERVEDLOCK; + storeLastErrno(pFile, errno); + } else if( lock.l_type!=F_UNLCK ){ + reserved = 1; + } + } +#endif + + unixLeaveMutex(); + OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); + + *pResOut = reserved; + return rc; +} + +/* +** Attempt to set a system-lock on the file pFile. The lock is +** described by pLock. +** +** If the pFile was opened read/write from unix-excl, then the only lock +** ever obtained is an exclusive lock, and it is obtained exactly once +** the first time any lock is attempted. All subsequent system locking +** operations become no-ops. Locking operations still happen internally, +** in order to coordinate access between separate database connections +** within this process, but all of that is handled in memory and the +** operating system does not participate. +** +** This function is a pass-through to fcntl(F_SETLK) if pFile is using +** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" +** and is read-only. +** +** Zero is returned if the call completes successfully, or -1 if a call +** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). +*/ +static int unixFileLock(unixFile *pFile, struct flock *pLock){ + int rc; + unixInodeInfo *pInode = pFile->pInode; + assert( unixMutexHeld() ); + assert( pInode!=0 ); + if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ + if( pInode->bProcessLock==0 ){ + struct flock lock; + assert( pInode->nLock==0 ); + lock.l_whence = SEEK_SET; + lock.l_start = SHARED_FIRST; + lock.l_len = SHARED_SIZE; + lock.l_type = F_WRLCK; + rc = osFcntl(pFile->h, F_SETLK, &lock); + if( rc<0 ) return rc; + pInode->bProcessLock = 1; + pInode->nLock++; + }else{ + rc = 0; + } + }else{ + rc = osFcntl(pFile->h, F_SETLK, pLock); + } + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +*/ +static int unixLock(sqlite3_file *id, int eFileLock){ + /* The following describes the implementation of the various locks and + ** lock transitions in terms of the POSIX advisory shared and exclusive + ** lock primitives (called read-locks and write-locks below, to avoid + ** confusion with SQLite lock names). The algorithms are complicated + ** slightly in order to be compatible with Windows95 systems simultaneously + ** accessing the same database file, in case that is ever required. + ** + ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved + ** byte', each single bytes at well known offsets, and the 'shared byte + ** range', a range of 510 bytes at a well known offset. + ** + ** To obtain a SHARED lock, a read-lock is obtained on the 'pending + ** byte'. If this is successful, 'shared byte range' is read-locked + ** and the lock on the 'pending byte' released. (Legacy note: When + ** SQLite was first developed, Windows95 systems were still very common, + ** and Widnows95 lacks a shared-lock capability. So on Windows95, a + ** single randomly selected by from the 'shared byte range' is locked. + ** Windows95 is now pretty much extinct, but this work-around for the + ** lack of shared-locks on Windows95 lives on, for backwards + ** compatibility.) + ** + ** A process may only obtain a RESERVED lock after it has a SHARED lock. + ** A RESERVED lock is implemented by grabbing a write-lock on the + ** 'reserved byte'. + ** + ** A process may only obtain a PENDING lock after it has obtained a + ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock + ** on the 'pending byte'. This ensures that no new SHARED locks can be + ** obtained, but existing SHARED locks are allowed to persist. A process + ** does not have to obtain a RESERVED lock on the way to a PENDING lock. + ** This property is used by the algorithm for rolling back a journal file + ** after a crash. + ** + ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is + ** implemented by obtaining a write-lock on the entire 'shared byte + ** range'. Since all other locks require a read-lock on one of the bytes + ** within this range, this ensures that no other locks are held on the + ** database. + */ + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; + unixInodeInfo *pInode; + struct flock lock; + int tErrno = 0; + + assert( pFile ); + OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, + azFileLock(eFileLock), azFileLock(pFile->eFileLock), + azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, + osGetpid(0))); + + /* If there is already a lock of this type or more restrictive on the + ** unixFile, do nothing. Don't use the end_lock: exit path, as + ** unixEnterMutex() hasn't been called yet. + */ + if( pFile->eFileLock>=eFileLock ){ + OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, + azFileLock(eFileLock))); + return SQLITE_OK; + } + + /* Make sure the locking sequence is correct. + ** (1) We never move from unlocked to anything higher than shared lock. + ** (2) SQLite never explicitly requests a pendig lock. + ** (3) A shared lock is always held when a reserve lock is requested. + */ + assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); + assert( eFileLock!=PENDING_LOCK ); + assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); + + /* This mutex is needed because pFile->pInode is shared across threads + */ + unixEnterMutex(); + pInode = pFile->pInode; + + /* If some thread using this PID has a lock via a different unixFile* + ** handle that precludes the requested lock, return BUSY. + */ + if( (pFile->eFileLock!=pInode->eFileLock && + (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) + ){ + rc = SQLITE_BUSY; + goto end_lock; + } + + /* If a SHARED lock is requested, and some thread using this PID already + ** has a SHARED or RESERVED lock, then increment reference counts and + ** return SQLITE_OK. + */ + if( eFileLock==SHARED_LOCK && + (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ + assert( eFileLock==SHARED_LOCK ); + assert( pFile->eFileLock==0 ); + assert( pInode->nShared>0 ); + pFile->eFileLock = SHARED_LOCK; + pInode->nShared++; + pInode->nLock++; + goto end_lock; + } + + + /* A PENDING lock is needed before acquiring a SHARED lock and before + ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will + ** be released. + */ + lock.l_len = 1L; + lock.l_whence = SEEK_SET; + if( eFileLock==SHARED_LOCK + || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLocknShared==0 ); + assert( pInode->eFileLock==0 ); + assert( rc==SQLITE_OK ); + + /* Now get the read-lock */ + lock.l_start = SHARED_FIRST; + lock.l_len = SHARED_SIZE; + if( unixFileLock(pFile, &lock) ){ + tErrno = errno; + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); + } + + /* Drop the temporary PENDING lock */ + lock.l_start = PENDING_BYTE; + lock.l_len = 1L; + lock.l_type = F_UNLCK; + if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ + /* This could happen with a network mount */ + tErrno = errno; + rc = SQLITE_IOERR_UNLOCK; + } + + if( rc ){ + if( rc!=SQLITE_BUSY ){ + storeLastErrno(pFile, tErrno); + } + goto end_lock; + }else{ + pFile->eFileLock = SHARED_LOCK; + pInode->nLock++; + pInode->nShared = 1; + } + }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ + /* We are trying for an exclusive lock but another thread in this + ** same process is still holding a shared lock. */ + rc = SQLITE_BUSY; + }else{ + /* The request was for a RESERVED or EXCLUSIVE lock. It is + ** assumed that there is a SHARED or greater lock on the file + ** already. + */ + assert( 0!=pFile->eFileLock ); + lock.l_type = F_WRLCK; + + assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); + if( eFileLock==RESERVED_LOCK ){ + lock.l_start = RESERVED_BYTE; + lock.l_len = 1L; + }else{ + lock.l_start = SHARED_FIRST; + lock.l_len = SHARED_SIZE; + } + + if( unixFileLock(pFile, &lock) ){ + tErrno = errno; + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); + if( rc!=SQLITE_BUSY ){ + storeLastErrno(pFile, tErrno); + } + } + } + + +#ifdef SQLITE_DEBUG + /* Set up the transaction-counter change checking flags when + ** transitioning from a SHARED to a RESERVED lock. The change + ** from SHARED to RESERVED marks the beginning of a normal + ** write operation (not a hot journal rollback). + */ + if( rc==SQLITE_OK + && pFile->eFileLock<=SHARED_LOCK + && eFileLock==RESERVED_LOCK + ){ + pFile->transCntrChng = 0; + pFile->dbUpdate = 0; + pFile->inNormalWrite = 1; + } +#endif + + + if( rc==SQLITE_OK ){ + pFile->eFileLock = eFileLock; + pInode->eFileLock = eFileLock; + }else if( eFileLock==EXCLUSIVE_LOCK ){ + pFile->eFileLock = PENDING_LOCK; + pInode->eFileLock = PENDING_LOCK; + } + +end_lock: + unixLeaveMutex(); + OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); + return rc; +} + +/* +** Add the file descriptor used by file handle pFile to the corresponding +** pUnused list. +*/ +static void setPendingFd(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; + UnixUnusedFd *p = pFile->pUnused; + p->pNext = pInode->pUnused; + pInode->pUnused = p; + pFile->h = -1; + pFile->pUnused = 0; +} + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +** +** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED +** the byte range is divided into 2 parts and the first part is unlocked then +** set to a read lock, then the other part is simply unlocked. This works +** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to +** remove the write lock on a region when a read lock is set. +*/ +static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ + unixFile *pFile = (unixFile*)id; + unixInodeInfo *pInode; + struct flock lock; + int rc = SQLITE_OK; + + assert( pFile ); + OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, + pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, + osGetpid(0))); + + assert( eFileLock<=SHARED_LOCK ); + if( pFile->eFileLock<=eFileLock ){ + return SQLITE_OK; + } + unixEnterMutex(); + pInode = pFile->pInode; + assert( pInode->nShared!=0 ); + if( pFile->eFileLock>SHARED_LOCK ){ + assert( pInode->eFileLock==pFile->eFileLock ); + +#ifdef SQLITE_DEBUG + /* When reducing a lock such that other processes can start + ** reading the database file again, make sure that the + ** transaction counter was updated if any part of the database + ** file changed. If the transaction counter is not updated, + ** other connections to the same file might not realize that + ** the file has changed and hence might not know to flush their + ** cache. The use of a stale cache can lead to database corruption. + */ + pFile->inNormalWrite = 0; +#endif + + /* downgrading to a shared lock on NFS involves clearing the write lock + ** before establishing the readlock - to avoid a race condition we downgrade + ** the lock in 2 blocks, so that part of the range will be covered by a + ** write lock until the rest is covered by a read lock: + ** 1: [WWWWW] + ** 2: [....W] + ** 3: [RRRRW] + ** 4: [RRRR.] + */ + if( eFileLock==SHARED_LOCK ){ +#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE + (void)handleNFSUnlock; + assert( handleNFSUnlock==0 ); +#endif +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE + if( handleNFSUnlock ){ + int tErrno; /* Error code from system call errors */ + off_t divSize = SHARED_SIZE - 1; + + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + lock.l_start = SHARED_FIRST; + lock.l_len = divSize; + if( unixFileLock(pFile, &lock)==(-1) ){ + tErrno = errno; + rc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, tErrno); + goto end_unlock; + } + lock.l_type = F_RDLCK; + lock.l_whence = SEEK_SET; + lock.l_start = SHARED_FIRST; + lock.l_len = divSize; + if( unixFileLock(pFile, &lock)==(-1) ){ + tErrno = errno; + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); + if( IS_LOCK_ERROR(rc) ){ + storeLastErrno(pFile, tErrno); + } + goto end_unlock; + } + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + lock.l_start = SHARED_FIRST+divSize; + lock.l_len = SHARED_SIZE-divSize; + if( unixFileLock(pFile, &lock)==(-1) ){ + tErrno = errno; + rc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, tErrno); + goto end_unlock; + } + }else +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ + { + lock.l_type = F_RDLCK; + lock.l_whence = SEEK_SET; + lock.l_start = SHARED_FIRST; + lock.l_len = SHARED_SIZE; + if( unixFileLock(pFile, &lock) ){ + /* In theory, the call to unixFileLock() cannot fail because another + ** process is holding an incompatible lock. If it does, this + ** indicates that the other process is not following the locking + ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning + ** SQLITE_BUSY would confuse the upper layer (in practice it causes + ** an assert to fail). */ + rc = SQLITE_IOERR_RDLOCK; + storeLastErrno(pFile, errno); + goto end_unlock; + } + } + } + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + lock.l_start = PENDING_BYTE; + lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); + if( unixFileLock(pFile, &lock)==0 ){ + pInode->eFileLock = SHARED_LOCK; + }else{ + rc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, errno); + goto end_unlock; + } + } + if( eFileLock==NO_LOCK ){ + /* Decrement the shared lock counter. Release the lock using an + ** OS call only when all threads in this same process have released + ** the lock. + */ + pInode->nShared--; + if( pInode->nShared==0 ){ + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + lock.l_start = lock.l_len = 0L; + if( unixFileLock(pFile, &lock)==0 ){ + pInode->eFileLock = NO_LOCK; + }else{ + rc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, errno); + pInode->eFileLock = NO_LOCK; + pFile->eFileLock = NO_LOCK; + } + } + + /* Decrement the count of locks against this same file. When the + ** count reaches zero, close any other file descriptors whose close + ** was deferred because of outstanding locks. + */ + pInode->nLock--; + assert( pInode->nLock>=0 ); + if( pInode->nLock==0 ){ + closePendingFds(pFile); + } + } + +end_unlock: + unixLeaveMutex(); + if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; + return rc; +} + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ +static int unixUnlock(sqlite3_file *id, int eFileLock){ +#if SQLITE_MAX_MMAP_SIZE>0 + assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); +#endif + return posixUnlock(id, eFileLock, 0); +} + +#if SQLITE_MAX_MMAP_SIZE>0 +static int unixMapfile(unixFile *pFd, i64 nByte); +static void unixUnmapfile(unixFile *pFd); +#endif + +/* +** This function performs the parts of the "close file" operation +** common to all locking schemes. It closes the directory and file +** handles, if they are valid, and sets all fields of the unixFile +** structure to 0. +** +** It is *not* necessary to hold the mutex when this routine is called, +** even on VxWorks. A mutex will be acquired on VxWorks by the +** vxworksReleaseFileId() routine. +*/ +static int closeUnixFile(sqlite3_file *id){ + unixFile *pFile = (unixFile*)id; +#if SQLITE_MAX_MMAP_SIZE>0 + unixUnmapfile(pFile); +#endif + if( pFile->h>=0 ){ + robust_close(pFile, pFile->h, __LINE__); + pFile->h = -1; + } +#if OS_VXWORKS + if( pFile->pId ){ + if( pFile->ctrlFlags & UNIXFILE_DELETE ){ + osUnlink(pFile->pId->zCanonicalName); + } + vxworksReleaseFileId(pFile->pId); + pFile->pId = 0; + } +#endif +#ifdef SQLITE_UNLINK_AFTER_CLOSE + if( pFile->ctrlFlags & UNIXFILE_DELETE ){ + osUnlink(pFile->zPath); + sqlite3_free(*(char**)&pFile->zPath); + pFile->zPath = 0; + } +#endif + OSTRACE(("CLOSE %-3d\n", pFile->h)); + OpenCounter(-1); + sqlite3_free(pFile->pUnused); + memset(pFile, 0, sizeof(unixFile)); + return SQLITE_OK; +} + +/* +** Close a file. +*/ +static int unixClose(sqlite3_file *id){ + int rc = SQLITE_OK; + unixFile *pFile = (unixFile *)id; + verifyDbFile(pFile); + unixUnlock(id, NO_LOCK); + unixEnterMutex(); + + /* unixFile.pInode is always valid here. Otherwise, a different close + ** routine (e.g. nolockClose()) would be called instead. + */ + assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); + if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ + /* If there are outstanding locks, do not actually close the file just + ** yet because that would clear those locks. Instead, add the file + ** descriptor to pInode->pUnused list. It will be automatically closed + ** when the last lock is cleared. + */ + setPendingFd(pFile); + } + releaseInodeInfo(pFile); + rc = closeUnixFile(id); + unixLeaveMutex(); + return rc; +} + +/************** End of the posix advisory lock implementation ***************** +******************************************************************************/ + +/****************************************************************************** +****************************** No-op Locking ********************************** +** +** Of the various locking implementations available, this is by far the +** simplest: locking is ignored. No attempt is made to lock the database +** file for reading or writing. +** +** This locking mode is appropriate for use on read-only databases +** (ex: databases that are burned into CD-ROM, for example.) It can +** also be used if the application employs some external mechanism to +** prevent simultaneous access of the same database by two or more +** database connections. But there is a serious risk of database +** corruption if this locking mode is used in situations where multiple +** database connections are accessing the same database file at the same +** time and one or more of those connections are writing. +*/ + +static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ + UNUSED_PARAMETER(NotUsed); + *pResOut = 0; + return SQLITE_OK; +} +static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return SQLITE_OK; +} +static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return SQLITE_OK; +} + +/* +** Close the file. +*/ +static int nolockClose(sqlite3_file *id) { + return closeUnixFile(id); +} + +/******************* End of the no-op lock implementation ********************* +******************************************************************************/ + +/****************************************************************************** +************************* Begin dot-file Locking ****************************** +** +** The dotfile locking implementation uses the existence of separate lock +** files (really a directory) to control access to the database. This works +** on just about every filesystem imaginable. But there are serious downsides: +** +** (1) There is zero concurrency. A single reader blocks all other +** connections from reading or writing the database. +** +** (2) An application crash or power loss can leave stale lock files +** sitting around that need to be cleared manually. +** +** Nevertheless, a dotlock is an appropriate locking mode for use if no +** other locking strategy is available. +** +** Dotfile locking works by creating a subdirectory in the same directory as +** the database and with the same name but with a ".lock" extension added. +** The existence of a lock directory implies an EXCLUSIVE lock. All other +** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. +*/ + +/* +** The file suffix added to the data base filename in order to create the +** lock directory. +*/ +#define DOTLOCK_SUFFIX ".lock" + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +** +** In dotfile locking, either a lock exists or it does not. So in this +** variation of CheckReservedLock(), *pResOut is set to true if any lock +** is held on the file and false if the file is unlocked. +*/ +static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { + int rc = SQLITE_OK; + int reserved = 0; + unixFile *pFile = (unixFile*)id; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); + reserved = osAccess((const char*)pFile->lockingContext, 0)==0; + OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); + *pResOut = reserved; + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +** +** With dotfile locking, we really only support state (4): EXCLUSIVE. +** But we track the other locking levels internally. +*/ +static int dotlockLock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + char *zLockFile = (char *)pFile->lockingContext; + int rc = SQLITE_OK; + + + /* If we have any lock, then the lock file already exists. All we have + ** to do is adjust our internal record of the lock level. + */ + if( pFile->eFileLock > NO_LOCK ){ + pFile->eFileLock = eFileLock; + /* Always update the timestamp on the old file */ +#ifdef HAVE_UTIME + utime(zLockFile, NULL); +#else + utimes(zLockFile, NULL); +#endif + return SQLITE_OK; + } + + /* grab an exclusive lock */ + rc = osMkdir(zLockFile, 0777); + if( rc<0 ){ + /* failed to open/create the lock directory */ + int tErrno = errno; + if( EEXIST == tErrno ){ + rc = SQLITE_BUSY; + } else { + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); + if( rc!=SQLITE_BUSY ){ + storeLastErrno(pFile, tErrno); + } + } + return rc; + } + + /* got it, set the type and return ok */ + pFile->eFileLock = eFileLock; + return rc; +} + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +** +** When the locking level reaches NO_LOCK, delete the lock file. +*/ +static int dotlockUnlock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + char *zLockFile = (char *)pFile->lockingContext; + int rc; + + assert( pFile ); + OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, + pFile->eFileLock, osGetpid(0))); + assert( eFileLock<=SHARED_LOCK ); + + /* no-op if possible */ + if( pFile->eFileLock==eFileLock ){ + return SQLITE_OK; + } + + /* To downgrade to shared, simply update our internal notion of the + ** lock state. No need to mess with the file on disk. + */ + if( eFileLock==SHARED_LOCK ){ + pFile->eFileLock = SHARED_LOCK; + return SQLITE_OK; + } + + /* To fully unlock the database, delete the lock file */ + assert( eFileLock==NO_LOCK ); + rc = osRmdir(zLockFile); + if( rc<0 ){ + int tErrno = errno; + if( tErrno==ENOENT ){ + rc = SQLITE_OK; + }else{ + rc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, tErrno); + } + return rc; + } + pFile->eFileLock = NO_LOCK; + return SQLITE_OK; +} + +/* +** Close a file. Make sure the lock has been released before closing. +*/ +static int dotlockClose(sqlite3_file *id) { + unixFile *pFile = (unixFile*)id; + assert( id!=0 ); + dotlockUnlock(id, NO_LOCK); + sqlite3_free(pFile->lockingContext); + return closeUnixFile(id); +} +/****************** End of the dot-file lock implementation ******************* +******************************************************************************/ + +/****************************************************************************** +************************** Begin flock Locking ******************************** +** +** Use the flock() system call to do file locking. +** +** flock() locking is like dot-file locking in that the various +** fine-grain locking levels supported by SQLite are collapsed into +** a single exclusive lock. In other words, SHARED, RESERVED, and +** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite +** still works when you do this, but concurrency is reduced since +** only a single process can be reading the database at a time. +** +** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off +*/ +#if SQLITE_ENABLE_LOCKING_STYLE + +/* +** Retry flock() calls that fail with EINTR +*/ +#ifdef EINTR +static int robust_flock(int fd, int op){ + int rc; + do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); + return rc; +} +#else +# define robust_flock(a,b) flock(a,b) +#endif + + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +*/ +static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ + int rc = SQLITE_OK; + int reserved = 0; + unixFile *pFile = (unixFile*)id; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); + + /* Check if a thread in this process holds such a lock */ + if( pFile->eFileLock>SHARED_LOCK ){ + reserved = 1; + } + + /* Otherwise see if some other process holds it. */ + if( !reserved ){ + /* attempt to get the lock */ + int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); + if( !lrc ){ + /* got the lock, unlock it */ + lrc = robust_flock(pFile->h, LOCK_UN); + if ( lrc ) { + int tErrno = errno; + /* unlock failed with an error */ + lrc = SQLITE_IOERR_UNLOCK; + storeLastErrno(pFile, tErrno); + rc = lrc; + } + } else { + int tErrno = errno; + reserved = 1; + /* someone else might have it reserved */ + lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); + if( IS_LOCK_ERROR(lrc) ){ + storeLastErrno(pFile, tErrno); + rc = lrc; + } + } + } + OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); + +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS + if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ + rc = SQLITE_OK; + reserved=1; + } +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ + *pResOut = reserved; + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** flock() only really support EXCLUSIVE locks. We track intermediate +** lock states in the sqlite3_file structure, but all locks SHARED or +** above are really EXCLUSIVE locks and exclude all other processes from +** access the file. +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +*/ +static int flockLock(sqlite3_file *id, int eFileLock) { + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; + + assert( pFile ); + + /* if we already have a lock, it is exclusive. + ** Just adjust level and punt on outta here. */ + if (pFile->eFileLock > NO_LOCK) { + pFile->eFileLock = eFileLock; + return SQLITE_OK; + } + + /* grab an exclusive lock */ + + if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { + int tErrno = errno; + /* didn't get, must be busy */ + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); + if( IS_LOCK_ERROR(rc) ){ + storeLastErrno(pFile, tErrno); + } + } else { + /* got it, set the type and return ok */ + pFile->eFileLock = eFileLock; + } + OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS + if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ + rc = SQLITE_BUSY; + } +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ + return rc; +} + + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ +static int flockUnlock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + + assert( pFile ); + OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, + pFile->eFileLock, osGetpid(0))); + assert( eFileLock<=SHARED_LOCK ); + + /* no-op if possible */ + if( pFile->eFileLock==eFileLock ){ + return SQLITE_OK; + } + + /* shared can just be set because we always have an exclusive */ + if (eFileLock==SHARED_LOCK) { + pFile->eFileLock = eFileLock; + return SQLITE_OK; + } + + /* no, really, unlock. */ + if( robust_flock(pFile->h, LOCK_UN) ){ +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS + return SQLITE_OK; +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ + return SQLITE_IOERR_UNLOCK; + }else{ + pFile->eFileLock = NO_LOCK; + return SQLITE_OK; + } +} + +/* +** Close a file. +*/ +static int flockClose(sqlite3_file *id) { + assert( id!=0 ); + flockUnlock(id, NO_LOCK); + return closeUnixFile(id); +} + +#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ + +/******************* End of the flock lock implementation ********************* +******************************************************************************/ + +/****************************************************************************** +************************ Begin Named Semaphore Locking ************************ +** +** Named semaphore locking is only supported on VxWorks. +** +** Semaphore locking is like dot-lock and flock in that it really only +** supports EXCLUSIVE locking. Only a single process can read or write +** the database file at a time. This reduces potential concurrency, but +** makes the lock implementation much easier. +*/ +#if OS_VXWORKS + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +*/ +static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { + int rc = SQLITE_OK; + int reserved = 0; + unixFile *pFile = (unixFile*)id; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); + + /* Check if a thread in this process holds such a lock */ + if( pFile->eFileLock>SHARED_LOCK ){ + reserved = 1; + } + + /* Otherwise see if some other process holds it. */ + if( !reserved ){ + sem_t *pSem = pFile->pInode->pSem; + + if( sem_trywait(pSem)==-1 ){ + int tErrno = errno; + if( EAGAIN != tErrno ){ + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); + storeLastErrno(pFile, tErrno); + } else { + /* someone else has the lock when we are in NO_LOCK */ + reserved = (pFile->eFileLock < SHARED_LOCK); + } + }else{ + /* we could have it if we want it */ + sem_post(pSem); + } + } + OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); + + *pResOut = reserved; + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** Semaphore locks only really support EXCLUSIVE locks. We track intermediate +** lock states in the sqlite3_file structure, but all locks SHARED or +** above are really EXCLUSIVE locks and exclude all other processes from +** access the file. +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +*/ +static int semXLock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + sem_t *pSem = pFile->pInode->pSem; + int rc = SQLITE_OK; + + /* if we already have a lock, it is exclusive. + ** Just adjust level and punt on outta here. */ + if (pFile->eFileLock > NO_LOCK) { + pFile->eFileLock = eFileLock; + rc = SQLITE_OK; + goto sem_end_lock; + } + + /* lock semaphore now but bail out when already locked. */ + if( sem_trywait(pSem)==-1 ){ + rc = SQLITE_BUSY; + goto sem_end_lock; + } + + /* got it, set the type and return ok */ + pFile->eFileLock = eFileLock; + + sem_end_lock: + return rc; +} + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ +static int semXUnlock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + sem_t *pSem = pFile->pInode->pSem; + + assert( pFile ); + assert( pSem ); + OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, + pFile->eFileLock, osGetpid(0))); + assert( eFileLock<=SHARED_LOCK ); + + /* no-op if possible */ + if( pFile->eFileLock==eFileLock ){ + return SQLITE_OK; + } + + /* shared can just be set because we always have an exclusive */ + if (eFileLock==SHARED_LOCK) { + pFile->eFileLock = eFileLock; + return SQLITE_OK; + } + + /* no, really unlock. */ + if ( sem_post(pSem)==-1 ) { + int rc, tErrno = errno; + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); + if( IS_LOCK_ERROR(rc) ){ + storeLastErrno(pFile, tErrno); + } + return rc; + } + pFile->eFileLock = NO_LOCK; + return SQLITE_OK; +} + +/* + ** Close a file. + */ +static int semXClose(sqlite3_file *id) { + if( id ){ + unixFile *pFile = (unixFile*)id; + semXUnlock(id, NO_LOCK); + assert( pFile ); + unixEnterMutex(); + releaseInodeInfo(pFile); + unixLeaveMutex(); + closeUnixFile(id); + } + return SQLITE_OK; +} + +#endif /* OS_VXWORKS */ +/* +** Named semaphore locking is only available on VxWorks. +** +*************** End of the named semaphore lock implementation **************** +******************************************************************************/ + + +/****************************************************************************** +*************************** Begin AFP Locking ********************************* +** +** AFP is the Apple Filing Protocol. AFP is a network filesystem found +** on Apple Macintosh computers - both OS9 and OSX. +** +** Third-party implementations of AFP are available. But this code here +** only works on OSX. +*/ + +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +/* +** The afpLockingContext structure contains all afp lock specific state +*/ +typedef struct afpLockingContext afpLockingContext; +struct afpLockingContext { + int reserved; + const char *dbPath; /* Name of the open file */ +}; + +struct ByteRangeLockPB2 +{ + unsigned long long offset; /* offset to first byte to lock */ + unsigned long long length; /* nbr of bytes to lock */ + unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ + unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ + unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ + int fd; /* file desc to assoc this lock with */ +}; + +#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) + +/* +** This is a utility for setting or clearing a bit-range lock on an +** AFP filesystem. +** +** Return SQLITE_OK on success, SQLITE_BUSY on failure. +*/ +static int afpSetLock( + const char *path, /* Name of the file to be locked or unlocked */ + unixFile *pFile, /* Open file descriptor on path */ + unsigned long long offset, /* First byte to be locked */ + unsigned long long length, /* Number of bytes to lock */ + int setLockFlag /* True to set lock. False to clear lock */ +){ + struct ByteRangeLockPB2 pb; + int err; + + pb.unLockFlag = setLockFlag ? 0 : 1; + pb.startEndFlag = 0; + pb.offset = offset; + pb.length = length; + pb.fd = pFile->h; + + OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", + (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), + offset, length)); + err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); + if ( err==-1 ) { + int rc; + int tErrno = errno; + OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", + path, tErrno, strerror(tErrno))); +#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS + rc = SQLITE_BUSY; +#else + rc = sqliteErrorFromPosixError(tErrno, + setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); +#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ + if( IS_LOCK_ERROR(rc) ){ + storeLastErrno(pFile, tErrno); + } + return rc; + } else { + return SQLITE_OK; + } +} + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +*/ +static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ + int rc = SQLITE_OK; + int reserved = 0; + unixFile *pFile = (unixFile*)id; + afpLockingContext *context; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); + context = (afpLockingContext *) pFile->lockingContext; + if( context->reserved ){ + *pResOut = 1; + return SQLITE_OK; + } + unixEnterMutex(); /* Because pFile->pInode is shared across threads */ + + /* Check if a thread in this process holds such a lock */ + if( pFile->pInode->eFileLock>SHARED_LOCK ){ + reserved = 1; + } + + /* Otherwise see if some other process holds it. + */ + if( !reserved ){ + /* lock the RESERVED byte */ + int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); + if( SQLITE_OK==lrc ){ + /* if we succeeded in taking the reserved lock, unlock it to restore + ** the original state */ + lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); + } else { + /* if we failed to get the lock then someone else must have it */ + reserved = 1; + } + if( IS_LOCK_ERROR(lrc) ){ + rc=lrc; + } + } + + unixLeaveMutex(); + OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); + + *pResOut = reserved; + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +*/ +static int afpLock(sqlite3_file *id, int eFileLock){ + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; + unixInodeInfo *pInode = pFile->pInode; + afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; + + assert( pFile ); + OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, + azFileLock(eFileLock), azFileLock(pFile->eFileLock), + azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); + + /* If there is already a lock of this type or more restrictive on the + ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as + ** unixEnterMutex() hasn't been called yet. + */ + if( pFile->eFileLock>=eFileLock ){ + OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, + azFileLock(eFileLock))); + return SQLITE_OK; + } + + /* Make sure the locking sequence is correct + ** (1) We never move from unlocked to anything higher than shared lock. + ** (2) SQLite never explicitly requests a pendig lock. + ** (3) A shared lock is always held when a reserve lock is requested. + */ + assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); + assert( eFileLock!=PENDING_LOCK ); + assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); + + /* This mutex is needed because pFile->pInode is shared across threads + */ + unixEnterMutex(); + pInode = pFile->pInode; + + /* If some thread using this PID has a lock via a different unixFile* + ** handle that precludes the requested lock, return BUSY. + */ + if( (pFile->eFileLock!=pInode->eFileLock && + (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) + ){ + rc = SQLITE_BUSY; + goto afp_end_lock; + } + + /* If a SHARED lock is requested, and some thread using this PID already + ** has a SHARED or RESERVED lock, then increment reference counts and + ** return SQLITE_OK. + */ + if( eFileLock==SHARED_LOCK && + (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ + assert( eFileLock==SHARED_LOCK ); + assert( pFile->eFileLock==0 ); + assert( pInode->nShared>0 ); + pFile->eFileLock = SHARED_LOCK; + pInode->nShared++; + pInode->nLock++; + goto afp_end_lock; + } + + /* A PENDING lock is needed before acquiring a SHARED lock and before + ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will + ** be released. + */ + if( eFileLock==SHARED_LOCK + || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLockdbPath, pFile, PENDING_BYTE, 1, 1); + if (failed) { + rc = failed; + goto afp_end_lock; + } + } + + /* If control gets to this point, then actually go ahead and make + ** operating system calls for the specified lock. + */ + if( eFileLock==SHARED_LOCK ){ + int lrc1, lrc2, lrc1Errno = 0; + long lk, mask; + + assert( pInode->nShared==0 ); + assert( pInode->eFileLock==0 ); + + mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; + /* Now get the read-lock SHARED_LOCK */ + /* note that the quality of the randomness doesn't matter that much */ + lk = random(); + pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); + lrc1 = afpSetLock(context->dbPath, pFile, + SHARED_FIRST+pInode->sharedByte, 1, 1); + if( IS_LOCK_ERROR(lrc1) ){ + lrc1Errno = pFile->lastErrno; + } + /* Drop the temporary PENDING lock */ + lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); + + if( IS_LOCK_ERROR(lrc1) ) { + storeLastErrno(pFile, lrc1Errno); + rc = lrc1; + goto afp_end_lock; + } else if( IS_LOCK_ERROR(lrc2) ){ + rc = lrc2; + goto afp_end_lock; + } else if( lrc1 != SQLITE_OK ) { + rc = lrc1; + } else { + pFile->eFileLock = SHARED_LOCK; + pInode->nLock++; + pInode->nShared = 1; + } + }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ + /* We are trying for an exclusive lock but another thread in this + ** same process is still holding a shared lock. */ + rc = SQLITE_BUSY; + }else{ + /* The request was for a RESERVED or EXCLUSIVE lock. It is + ** assumed that there is a SHARED or greater lock on the file + ** already. + */ + int failed = 0; + assert( 0!=pFile->eFileLock ); + if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { + /* Acquire a RESERVED lock */ + failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); + if( !failed ){ + context->reserved = 1; + } + } + if (!failed && eFileLock == EXCLUSIVE_LOCK) { + /* Acquire an EXCLUSIVE lock */ + + /* Remove the shared lock before trying the range. we'll need to + ** reestablish the shared lock if we can't get the afpUnlock + */ + if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + + pInode->sharedByte, 1, 0)) ){ + int failed2 = SQLITE_OK; + /* now attemmpt to get the exclusive lock range */ + failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, + SHARED_SIZE, 1); + if( failed && (failed2 = afpSetLock(context->dbPath, pFile, + SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ + /* Can't reestablish the shared lock. Sqlite can't deal, this is + ** a critical I/O error + */ + rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : + SQLITE_IOERR_LOCK; + goto afp_end_lock; + } + }else{ + rc = failed; + } + } + if( failed ){ + rc = failed; + } + } + + if( rc==SQLITE_OK ){ + pFile->eFileLock = eFileLock; + pInode->eFileLock = eFileLock; + }else if( eFileLock==EXCLUSIVE_LOCK ){ + pFile->eFileLock = PENDING_LOCK; + pInode->eFileLock = PENDING_LOCK; + } + +afp_end_lock: + unixLeaveMutex(); + OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); + return rc; +} + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ +static int afpUnlock(sqlite3_file *id, int eFileLock) { + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; + unixInodeInfo *pInode; + afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; + int skipShared = 0; +#ifdef SQLITE_TEST + int h = pFile->h; +#endif + + assert( pFile ); + OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, + pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, + osGetpid(0))); + + assert( eFileLock<=SHARED_LOCK ); + if( pFile->eFileLock<=eFileLock ){ + return SQLITE_OK; + } + unixEnterMutex(); + pInode = pFile->pInode; + assert( pInode->nShared!=0 ); + if( pFile->eFileLock>SHARED_LOCK ){ + assert( pInode->eFileLock==pFile->eFileLock ); + SimulateIOErrorBenign(1); + SimulateIOError( h=(-1) ) + SimulateIOErrorBenign(0); + +#ifdef SQLITE_DEBUG + /* When reducing a lock such that other processes can start + ** reading the database file again, make sure that the + ** transaction counter was updated if any part of the database + ** file changed. If the transaction counter is not updated, + ** other connections to the same file might not realize that + ** the file has changed and hence might not know to flush their + ** cache. The use of a stale cache can lead to database corruption. + */ + assert( pFile->inNormalWrite==0 + || pFile->dbUpdate==0 + || pFile->transCntrChng==1 ); + pFile->inNormalWrite = 0; +#endif + + if( pFile->eFileLock==EXCLUSIVE_LOCK ){ + rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); + if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ + /* only re-establish the shared lock if necessary */ + int sharedLockByte = SHARED_FIRST+pInode->sharedByte; + rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); + } else { + skipShared = 1; + } + } + if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ + rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); + } + if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ + rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); + if( !rc ){ + context->reserved = 0; + } + } + if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ + pInode->eFileLock = SHARED_LOCK; + } + } + if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ + + /* Decrement the shared lock counter. Release the lock using an + ** OS call only when all threads in this same process have released + ** the lock. + */ + unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; + pInode->nShared--; + if( pInode->nShared==0 ){ + SimulateIOErrorBenign(1); + SimulateIOError( h=(-1) ) + SimulateIOErrorBenign(0); + if( !skipShared ){ + rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); + } + if( !rc ){ + pInode->eFileLock = NO_LOCK; + pFile->eFileLock = NO_LOCK; + } + } + if( rc==SQLITE_OK ){ + pInode->nLock--; + assert( pInode->nLock>=0 ); + if( pInode->nLock==0 ){ + closePendingFds(pFile); + } + } + } + + unixLeaveMutex(); + if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; + return rc; +} + +/* +** Close a file & cleanup AFP specific locking context +*/ +static int afpClose(sqlite3_file *id) { + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; + assert( id!=0 ); + afpUnlock(id, NO_LOCK); + unixEnterMutex(); + if( pFile->pInode && pFile->pInode->nLock ){ + /* If there are outstanding locks, do not actually close the file just + ** yet because that would clear those locks. Instead, add the file + ** descriptor to pInode->aPending. It will be automatically closed when + ** the last lock is cleared. + */ + setPendingFd(pFile); + } + releaseInodeInfo(pFile); + sqlite3_free(pFile->lockingContext); + rc = closeUnixFile(id); + unixLeaveMutex(); + return rc; +} + +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ +/* +** The code above is the AFP lock implementation. The code is specific +** to MacOSX and does not work on other unix platforms. No alternative +** is available. If you don't compile for a mac, then the "unix-afp" +** VFS is not available. +** +********************* End of the AFP lock implementation ********************** +******************************************************************************/ + +/****************************************************************************** +*************************** Begin NFS Locking ********************************/ + +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +/* + ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock + ** must be either NO_LOCK or SHARED_LOCK. + ** + ** If the locking level of the file descriptor is already at or below + ** the requested locking level, this routine is a no-op. + */ +static int nfsUnlock(sqlite3_file *id, int eFileLock){ + return posixUnlock(id, eFileLock, 1); +} + +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ +/* +** The code above is the NFS lock implementation. The code is specific +** to MacOSX and does not work on other unix platforms. No alternative +** is available. +** +********************* End of the NFS lock implementation ********************** +******************************************************************************/ + +/****************************************************************************** +**************** Non-locking sqlite3_file methods ***************************** +** +** The next division contains implementations for all methods of the +** sqlite3_file object other than the locking methods. The locking +** methods were defined in divisions above (one locking method per +** division). Those methods that are common to all locking modes +** are gather together into this division. +*/ + +/* +** Seek to the offset passed as the second argument, then read cnt +** bytes into pBuf. Return the number of bytes actually read. +** +** NB: If you define USE_PREAD or USE_PREAD64, then it might also +** be necessary to define _XOPEN_SOURCE to be 500. This varies from +** one system to another. Since SQLite does not define USE_PREAD +** in any form by default, we will not attempt to define _XOPEN_SOURCE. +** See tickets #2741 and #2681. +** +** To avoid stomping the errno value on a failed read the lastErrno value +** is set before returning. +*/ +static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ + int got; + int prior = 0; +#if (!defined(USE_PREAD) && !defined(USE_PREAD64)) + i64 newOffset; +#endif + TIMER_START; + assert( cnt==(cnt&0x1ffff) ); + assert( id->h>2 ); + do{ +#if defined(USE_PREAD) + got = osPread(id->h, pBuf, cnt, offset); + SimulateIOError( got = -1 ); +#elif defined(USE_PREAD64) + got = osPread64(id->h, pBuf, cnt, offset); + SimulateIOError( got = -1 ); +#else + newOffset = lseek(id->h, offset, SEEK_SET); + SimulateIOError( newOffset = -1 ); + if( newOffset<0 ){ + storeLastErrno((unixFile*)id, errno); + return -1; + } + got = osRead(id->h, pBuf, cnt); +#endif + if( got==cnt ) break; + if( got<0 ){ + if( errno==EINTR ){ got = 1; continue; } + prior = 0; + storeLastErrno((unixFile*)id, errno); + break; + }else if( got>0 ){ + cnt -= got; + offset += got; + prior += got; + pBuf = (void*)(got + (char*)pBuf); + } + }while( got>0 ); + TIMER_END; + OSTRACE(("READ %-3d %5d %7lld %llu\n", + id->h, got+prior, offset-prior, TIMER_ELAPSED)); + return got+prior; +} + +/* +** Read data from a file into a buffer. Return SQLITE_OK if all +** bytes were read successfully and SQLITE_IOERR if anything goes +** wrong. +*/ +static int unixRead( + sqlite3_file *id, + void *pBuf, + int amt, + sqlite3_int64 offset +){ + unixFile *pFile = (unixFile *)id; + int got; + assert( id ); + assert( offset>=0 ); + assert( amt>0 ); + + /* If this is a database file (not a journal, master-journal or temp + ** file), the bytes in the locking range should never be read or written. */ +#if 0 + assert( pFile->pUnused==0 + || offset>=PENDING_BYTE+512 + || offset+amt<=PENDING_BYTE + ); +#endif + +#if SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this read request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offsetmmapSize ){ + if( offset+amt <= pFile->mmapSize ){ + memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); + return SQLITE_OK; + }else{ + int nCopy = pFile->mmapSize - offset; + memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); + pBuf = &((u8 *)pBuf)[nCopy]; + amt -= nCopy; + offset += nCopy; + } + } +#endif + + got = seekAndRead(pFile, offset, pBuf, amt); + if( got==amt ){ + return SQLITE_OK; + }else if( got<0 ){ + /* lastErrno set by seekAndRead */ + return SQLITE_IOERR_READ; + }else{ + storeLastErrno(pFile, 0); /* not a system error */ + /* Unread parts of the buffer must be zero-filled */ + memset(&((char*)pBuf)[got], 0, amt-got); + return SQLITE_IOERR_SHORT_READ; + } +} + +/* +** Attempt to seek the file-descriptor passed as the first argument to +** absolute offset iOff, then attempt to write nBuf bytes of data from +** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, +** return the actual number of bytes written (which may be less than +** nBuf). +*/ +static int seekAndWriteFd( + int fd, /* File descriptor to write to */ + i64 iOff, /* File offset to begin writing at */ + const void *pBuf, /* Copy data from this buffer to the file */ + int nBuf, /* Size of buffer pBuf in bytes */ + int *piErrno /* OUT: Error number if error occurs */ +){ + int rc = 0; /* Value returned by system call */ + + assert( nBuf==(nBuf&0x1ffff) ); + assert( fd>2 ); + assert( piErrno!=0 ); + nBuf &= 0x1ffff; + TIMER_START; + +#if defined(USE_PREAD) + do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); +#elif defined(USE_PREAD64) + do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); +#else + do{ + i64 iSeek = lseek(fd, iOff, SEEK_SET); + SimulateIOError( iSeek = -1 ); + if( iSeek<0 ){ + rc = -1; + break; + } + rc = osWrite(fd, pBuf, nBuf); + }while( rc<0 && errno==EINTR ); +#endif + + TIMER_END; + OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); + + if( rc<0 ) *piErrno = errno; + return rc; +} + + +/* +** Seek to the offset in id->offset then read cnt bytes into pBuf. +** Return the number of bytes actually read. Update the offset. +** +** To avoid stomping the errno value on a failed write the lastErrno value +** is set before returning. +*/ +static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ + return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); +} + + +/* +** Write data from a buffer into a file. Return SQLITE_OK on success +** or some other error code on failure. +*/ +static int unixWrite( + sqlite3_file *id, + const void *pBuf, + int amt, + sqlite3_int64 offset +){ + unixFile *pFile = (unixFile*)id; + int wrote = 0; + assert( id ); + assert( amt>0 ); + + /* If this is a database file (not a journal, master-journal or temp + ** file), the bytes in the locking range should never be read or written. */ +#if 0 + assert( pFile->pUnused==0 + || offset>=PENDING_BYTE+512 + || offset+amt<=PENDING_BYTE + ); +#endif + +#ifdef SQLITE_DEBUG + /* If we are doing a normal write to a database file (as opposed to + ** doing a hot-journal rollback or a write to some file other than a + ** normal database file) then record the fact that the database + ** has changed. If the transaction counter is modified, record that + ** fact too. + */ + if( pFile->inNormalWrite ){ + pFile->dbUpdate = 1; /* The database has been modified */ + if( offset<=24 && offset+amt>=27 ){ + int rc; + char oldCntr[4]; + SimulateIOErrorBenign(1); + rc = seekAndRead(pFile, 24, oldCntr, 4); + SimulateIOErrorBenign(0); + if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ + pFile->transCntrChng = 1; /* The transaction counter has changed */ + } + } + } +#endif + +#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this write request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offsetmmapSize ){ + if( offset+amt <= pFile->mmapSize ){ + memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); + return SQLITE_OK; + }else{ + int nCopy = pFile->mmapSize - offset; + memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); + pBuf = &((u8 *)pBuf)[nCopy]; + amt -= nCopy; + offset += nCopy; + } + } +#endif + + while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))0 ){ + amt -= wrote; + offset += wrote; + pBuf = &((char*)pBuf)[wrote]; + } + SimulateIOError(( wrote=(-1), amt=1 )); + SimulateDiskfullError(( wrote=0, amt=1 )); + + if( amt>wrote ){ + if( wrote<0 && pFile->lastErrno!=ENOSPC ){ + /* lastErrno set by seekAndWrite */ + return SQLITE_IOERR_WRITE; + }else{ + storeLastErrno(pFile, 0); /* not a system error */ + return SQLITE_FULL; + } + } + + return SQLITE_OK; +} + +#ifdef SQLITE_TEST +/* +** Count the number of fullsyncs and normal syncs. This is used to test +** that syncs and fullsyncs are occurring at the right times. +*/ +SQLITE_API int sqlite3_sync_count = 0; +SQLITE_API int sqlite3_fullsync_count = 0; +#endif + +/* +** We do not trust systems to provide a working fdatasync(). Some do. +** Others do no. To be safe, we will stick with the (slightly slower) +** fsync(). If you know that your system does support fdatasync() correctly, +** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC +*/ +#if !defined(fdatasync) && !HAVE_FDATASYNC +# define fdatasync fsync +#endif + +/* +** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not +** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently +** only available on Mac OS X. But that could change. +*/ +#ifdef F_FULLFSYNC +# define HAVE_FULLFSYNC 1 +#else +# define HAVE_FULLFSYNC 0 +#endif + + +/* +** The fsync() system call does not work as advertised on many +** unix systems. The following procedure is an attempt to make +** it work better. +** +** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful +** for testing when we want to run through the test suite quickly. +** You are strongly advised *not* to deploy with SQLITE_NO_SYNC +** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash +** or power failure will likely corrupt the database file. +** +** SQLite sets the dataOnly flag if the size of the file is unchanged. +** The idea behind dataOnly is that it should only write the file content +** to disk, not the inode. We only set dataOnly if the file size is +** unchanged since the file size is part of the inode. However, +** Ted Ts'o tells us that fdatasync() will also write the inode if the +** file size has changed. The only real difference between fdatasync() +** and fsync(), Ted tells us, is that fdatasync() will not flush the +** inode if the mtime or owner or other inode attributes have changed. +** We only care about the file size, not the other file attributes, so +** as far as SQLite is concerned, an fdatasync() is always adequate. +** So, we always use fdatasync() if it is available, regardless of +** the value of the dataOnly flag. +*/ +static int full_fsync(int fd, int fullSync, int dataOnly){ + int rc; + + /* The following "ifdef/elif/else/" block has the same structure as + ** the one below. It is replicated here solely to avoid cluttering + ** up the real code with the UNUSED_PARAMETER() macros. + */ +#ifdef SQLITE_NO_SYNC + UNUSED_PARAMETER(fd); + UNUSED_PARAMETER(fullSync); + UNUSED_PARAMETER(dataOnly); +#elif HAVE_FULLFSYNC + UNUSED_PARAMETER(dataOnly); +#else + UNUSED_PARAMETER(fullSync); + UNUSED_PARAMETER(dataOnly); +#endif + + /* Record the number of times that we do a normal fsync() and + ** FULLSYNC. This is used during testing to verify that this procedure + ** gets called with the correct arguments. + */ +#ifdef SQLITE_TEST + if( fullSync ) sqlite3_fullsync_count++; + sqlite3_sync_count++; +#endif + + /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a + ** no-op. But go ahead and call fstat() to validate the file + ** descriptor as we need a method to provoke a failure during + ** coverate testing. + */ +#ifdef SQLITE_NO_SYNC + { + struct stat buf; + rc = osFstat(fd, &buf); + } +#elif HAVE_FULLFSYNC + if( fullSync ){ + rc = osFcntl(fd, F_FULLFSYNC, 0); + }else{ + rc = 1; + } + /* If the FULLFSYNC failed, fall back to attempting an fsync(). + ** It shouldn't be possible for fullfsync to fail on the local + ** file system (on OSX), so failure indicates that FULLFSYNC + ** isn't supported for this file system. So, attempt an fsync + ** and (for now) ignore the overhead of a superfluous fcntl call. + ** It'd be better to detect fullfsync support once and avoid + ** the fcntl call every time sync is called. + */ + if( rc ) rc = fsync(fd); + +#elif defined(__APPLE__) + /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly + ** so currently we default to the macro that redefines fdatasync to fsync + */ + rc = fsync(fd); +#else + rc = fdatasync(fd); +#if OS_VXWORKS + if( rc==-1 && errno==ENOTSUP ){ + rc = fsync(fd); + } +#endif /* OS_VXWORKS */ +#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ + + if( OS_VXWORKS && rc!= -1 ){ + rc = 0; + } + return rc; +} + +/* +** Open a file descriptor to the directory containing file zFilename. +** If successful, *pFd is set to the opened file descriptor and +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined +** value. +** +** The directory file descriptor is used for only one thing - to +** fsync() a directory to make sure file creation and deletion events +** are flushed to disk. Such fsyncs are not needed on newer +** journaling filesystems, but are required on older filesystems. +** +** This routine can be overridden using the xSetSysCall interface. +** The ability to override this routine was added in support of the +** chromium sandbox. Opening a directory is a security risk (we are +** told) so making it overrideable allows the chromium sandbox to +** replace this routine with a harmless no-op. To make this routine +** a no-op, replace it with a stub that returns SQLITE_OK but leaves +** *pFd set to a negative number. +** +** If SQLITE_OK is returned, the caller is responsible for closing +** the file descriptor *pFd using close(). +*/ +static int openDirectory(const char *zFilename, int *pFd){ + int ii; + int fd = -1; + char zDirname[MAX_PATHNAME+1]; + + sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); + for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); + if( ii>0 ){ + zDirname[ii] = '\0'; + }else{ + if( zDirname[0]!='/' ) zDirname[0] = '.'; + zDirname[1] = 0; + } + fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); + if( fd>=0 ){ + OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); + } + *pFd = fd; + if( fd>=0 ) return SQLITE_OK; + return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); +} + +/* +** Make sure all writes to a particular file are committed to disk. +** +** If dataOnly==0 then both the file itself and its metadata (file +** size, access time, etc) are synced. If dataOnly!=0 then only the +** file data is synced. +** +** Under Unix, also make sure that the directory entry for the file +** has been created by fsync-ing the directory that contains the file. +** If we do not do this and we encounter a power failure, the directory +** entry for the journal might not exist after we reboot. The next +** SQLite to access the file will not know that the journal exists (because +** the directory entry for the journal was never created) and the transaction +** will not roll back - possibly leading to database corruption. +*/ +static int unixSync(sqlite3_file *id, int flags){ + int rc; + unixFile *pFile = (unixFile*)id; + + int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); + int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; + + /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ + assert((flags&0x0F)==SQLITE_SYNC_NORMAL + || (flags&0x0F)==SQLITE_SYNC_FULL + ); + + /* Unix cannot, but some systems may return SQLITE_FULL from here. This + ** line is to test that doing so does not cause any problems. + */ + SimulateDiskfullError( return SQLITE_FULL ); + + assert( pFile ); + OSTRACE(("SYNC %-3d\n", pFile->h)); + rc = full_fsync(pFile->h, isFullsync, isDataOnly); + SimulateIOError( rc=1 ); + if( rc ){ + storeLastErrno(pFile, errno); + return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); + } + + /* Also fsync the directory containing the file if the DIRSYNC flag + ** is set. This is a one-time occurrence. Many systems (examples: AIX) + ** are unable to fsync a directory, so ignore errors on the fsync. + */ + if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ + int dirfd; + OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, + HAVE_FULLFSYNC, isFullsync)); + rc = osOpenDirectory(pFile->zPath, &dirfd); + if( rc==SQLITE_OK ){ + full_fsync(dirfd, 0, 0); + robust_close(pFile, dirfd, __LINE__); + }else{ + assert( rc==SQLITE_CANTOPEN ); + rc = SQLITE_OK; + } + pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; + } + return rc; +} + +/* +** Truncate an open file to a specified size +*/ +static int unixTruncate(sqlite3_file *id, i64 nByte){ + unixFile *pFile = (unixFile *)id; + int rc; + assert( pFile ); + SimulateIOError( return SQLITE_IOERR_TRUNCATE ); + + /* If the user has configured a chunk-size for this file, truncate the + ** file so that it consists of an integer number of chunks (i.e. the + ** actual file size after the operation may be larger than the requested + ** size). + */ + if( pFile->szChunk>0 ){ + nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; + } + + rc = robust_ftruncate(pFile->h, nByte); + if( rc ){ + storeLastErrno(pFile, errno); + return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); + }else{ +#ifdef SQLITE_DEBUG + /* If we are doing a normal write to a database file (as opposed to + ** doing a hot-journal rollback or a write to some file other than a + ** normal database file) and we truncate the file to zero length, + ** that effectively updates the change counter. This might happen + ** when restoring a database using the backup API from a zero-length + ** source. + */ + if( pFile->inNormalWrite && nByte==0 ){ + pFile->transCntrChng = 1; + } +#endif + +#if SQLITE_MAX_MMAP_SIZE>0 + /* If the file was just truncated to a size smaller than the currently + ** mapped region, reduce the effective mapping size as well. SQLite will + ** use read() and write() to access data beyond this point from now on. + */ + if( nBytemmapSize ){ + pFile->mmapSize = nByte; + } +#endif + + return SQLITE_OK; + } +} + +/* +** Determine the current size of a file in bytes +*/ +static int unixFileSize(sqlite3_file *id, i64 *pSize){ + int rc; + struct stat buf; + assert( id ); + rc = osFstat(((unixFile*)id)->h, &buf); + SimulateIOError( rc=1 ); + if( rc!=0 ){ + storeLastErrno((unixFile*)id, errno); + return SQLITE_IOERR_FSTAT; + } + *pSize = buf.st_size; + + /* When opening a zero-size database, the findInodeInfo() procedure + ** writes a single byte into that file in order to work around a bug + ** in the OS-X msdos filesystem. In order to avoid problems with upper + ** layers, we need to report this file size as zero even though it is + ** really 1. Ticket #3260. + */ + if( *pSize==1 ) *pSize = 0; + + + return SQLITE_OK; +} + +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) +/* +** Handler for proxy-locking file-control verbs. Defined below in the +** proxying locking division. +*/ +static int proxyFileControl(sqlite3_file*,int,void*); +#endif + +/* +** This function is called to handle the SQLITE_FCNTL_SIZE_HINT +** file-control operation. Enlarge the database to nBytes in size +** (rounded up to the next chunk-size). If the database is already +** nBytes or larger, this routine is a no-op. +*/ +static int fcntlSizeHint(unixFile *pFile, i64 nByte){ + if( pFile->szChunk>0 ){ + i64 nSize; /* Required file size */ + struct stat buf; /* Used to hold return values of fstat() */ + + if( osFstat(pFile->h, &buf) ){ + return SQLITE_IOERR_FSTAT; + } + + nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; + if( nSize>(i64)buf.st_size ){ + +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE + /* The code below is handling the return value of osFallocate() + ** correctly. posix_fallocate() is defined to "returns zero on success, + ** or an error number on failure". See the manpage for details. */ + int err; + do{ + err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); + }while( err==EINTR ); + if( err ) return SQLITE_IOERR_WRITE; +#else + /* If the OS does not have posix_fallocate(), fake it. Write a + ** single byte to the last byte in each block that falls entirely + ** within the extended region. Then, if required, a single byte + ** at offset (nSize-1), to set the size of the file correctly. + ** This is a similar technique to that used by glibc on systems + ** that do not have a real fallocate() call. + */ + int nBlk = buf.st_blksize; /* File-system block size */ + int nWrite = 0; /* Number of bytes written by seekAndWrite */ + i64 iWrite; /* Next offset to write to */ + + iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; + assert( iWrite>=buf.st_size ); + assert( ((iWrite+1)%nBlk)==0 ); + for(/*no-op*/; iWrite=nSize ) iWrite = nSize - 1; + nWrite = seekAndWrite(pFile, iWrite, "", 1); + if( nWrite!=1 ) return SQLITE_IOERR_WRITE; + } +#endif + } + } + +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ + int rc; + if( pFile->szChunk<=0 ){ + if( robust_ftruncate(pFile->h, nByte) ){ + storeLastErrno(pFile, errno); + return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); + } + } + + rc = unixMapfile(pFile, nByte); + return rc; + } +#endif + + return SQLITE_OK; +} + +/* +** If *pArg is initially negative then this is a query. Set *pArg to +** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. +** +** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. +*/ +static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ + if( *pArg<0 ){ + *pArg = (pFile->ctrlFlags & mask)!=0; + }else if( (*pArg)==0 ){ + pFile->ctrlFlags &= ~mask; + }else{ + pFile->ctrlFlags |= mask; + } +} + +/* Forward declaration */ +static int unixGetTempname(int nBuf, char *zBuf); + +/* +** Information and control of an open file handle. +*/ +static int unixFileControl(sqlite3_file *id, int op, void *pArg){ + unixFile *pFile = (unixFile*)id; + switch( op ){ + case SQLITE_FCNTL_LOCKSTATE: { + *(int*)pArg = pFile->eFileLock; + return SQLITE_OK; + } + case SQLITE_FCNTL_LAST_ERRNO: { + *(int*)pArg = pFile->lastErrno; + return SQLITE_OK; + } + case SQLITE_FCNTL_CHUNK_SIZE: { + pFile->szChunk = *(int *)pArg; + return SQLITE_OK; + } + case SQLITE_FCNTL_SIZE_HINT: { + int rc; + SimulateIOErrorBenign(1); + rc = fcntlSizeHint(pFile, *(i64 *)pArg); + SimulateIOErrorBenign(0); + return rc; + } + case SQLITE_FCNTL_PERSIST_WAL: { + unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); + return SQLITE_OK; + } + case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { + unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); + return SQLITE_OK; + } + case SQLITE_FCNTL_VFSNAME: { + *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); + return SQLITE_OK; + } + case SQLITE_FCNTL_TEMPFILENAME: { + char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); + if( zTFile ){ + unixGetTempname(pFile->pVfs->mxPathname, zTFile); + *(char**)pArg = zTFile; + } + return SQLITE_OK; + } + case SQLITE_FCNTL_HAS_MOVED: { + *(int*)pArg = fileHasMoved(pFile); + return SQLITE_OK; + } +#if SQLITE_MAX_MMAP_SIZE>0 + case SQLITE_FCNTL_MMAP_SIZE: { + i64 newLimit = *(i64*)pArg; + int rc = SQLITE_OK; + if( newLimit>sqlite3GlobalConfig.mxMmap ){ + newLimit = sqlite3GlobalConfig.mxMmap; + } + *(i64*)pArg = pFile->mmapSizeMax; + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ + pFile->mmapSizeMax = newLimit; + if( pFile->mmapSize>0 ){ + unixUnmapfile(pFile); + rc = unixMapfile(pFile, -1); + } + } + return rc; + } +#endif +#ifdef SQLITE_DEBUG + /* The pager calls this method to signal that it has done + ** a rollback and that the database is therefore unchanged and + ** it hence it is OK for the transaction change counter to be + ** unchanged. + */ + case SQLITE_FCNTL_DB_UNCHANGED: { + ((unixFile*)id)->dbUpdate = 0; + return SQLITE_OK; + } +#endif +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) + case SQLITE_FCNTL_SET_LOCKPROXYFILE: + case SQLITE_FCNTL_GET_LOCKPROXYFILE: { + return proxyFileControl(id,op,pArg); + } +#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ + } + return SQLITE_NOTFOUND; +} + +/* +** Return the sector size in bytes of the underlying block device for +** the specified file. This is almost always 512 bytes, but may be +** larger for some devices. +** +** SQLite code assumes this function cannot fail. It also assumes that +** if two files are created in the same file-system directory (i.e. +** a database and its journal file) that the sector size will be the +** same for both. +*/ +#ifndef __QNXNTO__ +static int unixSectorSize(sqlite3_file *NotUsed){ + UNUSED_PARAMETER(NotUsed); + return SQLITE_DEFAULT_SECTOR_SIZE; +} +#endif + +/* +** The following version of unixSectorSize() is optimized for QNX. +*/ +#ifdef __QNXNTO__ +#include +#include +static int unixSectorSize(sqlite3_file *id){ + unixFile *pFile = (unixFile*)id; + if( pFile->sectorSize == 0 ){ + struct statvfs fsInfo; + + /* Set defaults for non-supported filesystems */ + pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; + pFile->deviceCharacteristics = 0; + if( fstatvfs(pFile->h, &fsInfo) == -1 ) { + return pFile->sectorSize; + } + + if( !strcmp(fsInfo.f_basetype, "tmp") ) { + pFile->sectorSize = fsInfo.f_bsize; + pFile->deviceCharacteristics = + SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ + SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until + ** the write succeeds */ + SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind + ** so it is ordered */ + 0; + }else if( strstr(fsInfo.f_basetype, "etfs") ){ + pFile->sectorSize = fsInfo.f_bsize; + pFile->deviceCharacteristics = + /* etfs cluster size writes are atomic */ + (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | + SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until + ** the write succeeds */ + SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind + ** so it is ordered */ + 0; + }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ + pFile->sectorSize = fsInfo.f_bsize; + pFile->deviceCharacteristics = + SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ + SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until + ** the write succeeds */ + SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind + ** so it is ordered */ + 0; + }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ + pFile->sectorSize = fsInfo.f_bsize; + pFile->deviceCharacteristics = + /* full bitset of atomics from max sector size and smaller */ + ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | + SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind + ** so it is ordered */ + 0; + }else if( strstr(fsInfo.f_basetype, "dos") ){ + pFile->sectorSize = fsInfo.f_bsize; + pFile->deviceCharacteristics = + /* full bitset of atomics from max sector size and smaller */ + ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | + SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind + ** so it is ordered */ + 0; + }else{ + pFile->deviceCharacteristics = + SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ + SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until + ** the write succeeds */ + 0; + } + } + /* Last chance verification. If the sector size isn't a multiple of 512 + ** then it isn't valid.*/ + if( pFile->sectorSize % 512 != 0 ){ + pFile->deviceCharacteristics = 0; + pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; + } + return pFile->sectorSize; +} +#endif /* __QNXNTO__ */ + +/* +** Return the device characteristics for the file. +** +** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. +** However, that choice is controversial since technically the underlying +** file system does not always provide powersafe overwrites. (In other +** words, after a power-loss event, parts of the file that were never +** written might end up being altered.) However, non-PSOW behavior is very, +** very rare. And asserting PSOW makes a large reduction in the amount +** of required I/O for journaling, since a lot of padding is eliminated. +** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control +** available to turn it off and URI query parameter available to turn it off. +*/ +static int unixDeviceCharacteristics(sqlite3_file *id){ + unixFile *p = (unixFile*)id; + int rc = 0; +#ifdef __QNXNTO__ + if( p->sectorSize==0 ) unixSectorSize(id); + rc = p->deviceCharacteristics; +#endif + if( p->ctrlFlags & UNIXFILE_PSOW ){ + rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; + } + return rc; +} + +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + +/* +** Return the system page size. +** +** This function should not be called directly by other code in this file. +** Instead, it should be called via macro osGetpagesize(). +*/ +static int unixGetpagesize(void){ +#if OS_VXWORKS + return 1024; +#elif defined(_BSD_SOURCE) + return getpagesize(); +#else + return (int)sysconf(_SC_PAGESIZE); +#endif +} + +#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ + +#ifndef SQLITE_OMIT_WAL + +/* +** Object used to represent an shared memory buffer. +** +** When multiple threads all reference the same wal-index, each thread +** has its own unixShm object, but they all point to a single instance +** of this unixShmNode object. In other words, each wal-index is opened +** only once per process. +** +** Each unixShmNode object is connected to a single unixInodeInfo object. +** We could coalesce this object into unixInodeInfo, but that would mean +** every open file that does not use shared memory (in other words, most +** open files) would have to carry around this extra information. So +** the unixInodeInfo object contains a pointer to this unixShmNode object +** and the unixShmNode object is created only when needed. +** +** unixMutexHeld() must be true when creating or destroying +** this object or while reading or writing the following fields: +** +** nRef +** +** The following fields are read-only after the object is created: +** +** fid +** zFilename +** +** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and +** unixMutexHeld() is true when reading or writing any other field +** in this structure. +*/ +struct unixShmNode { + unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ + sqlite3_mutex *mutex; /* Mutex to access this object */ + char *zFilename; /* Name of the mmapped file */ + int h; /* Open file descriptor */ + int szRegion; /* Size of shared-memory regions */ + u16 nRegion; /* Size of array apRegion */ + u8 isReadonly; /* True if read-only */ + char **apRegion; /* Array of mapped shared-memory regions */ + int nRef; /* Number of unixShm objects pointing to this */ + unixShm *pFirst; /* All unixShm objects pointing to this */ +#ifdef SQLITE_DEBUG + u8 exclMask; /* Mask of exclusive locks held */ + u8 sharedMask; /* Mask of shared locks held */ + u8 nextShmId; /* Next available unixShm.id value */ +#endif +}; + +/* +** Structure used internally by this VFS to record the state of an +** open shared memory connection. +** +** The following fields are initialized when this object is created and +** are read-only thereafter: +** +** unixShm.pFile +** unixShm.id +** +** All other fields are read/write. The unixShm.pFile->mutex must be held +** while accessing any read/write fields. +*/ +struct unixShm { + unixShmNode *pShmNode; /* The underlying unixShmNode object */ + unixShm *pNext; /* Next unixShm with the same unixShmNode */ + u8 hasMutex; /* True if holding the unixShmNode mutex */ + u8 id; /* Id of this connection within its unixShmNode */ + u16 sharedMask; /* Mask of shared locks held */ + u16 exclMask; /* Mask of exclusive locks held */ +}; + +/* +** Constants used for locking +*/ +#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ +#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ + +/* +** Apply posix advisory locks for all bytes from ofst through ofst+n-1. +** +** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking +** otherwise. +*/ +static int unixShmSystemLock( + unixFile *pFile, /* Open connection to the WAL file */ + int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ + int ofst, /* First byte of the locking range */ + int n /* Number of bytes to lock */ +){ + unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ + struct flock f; /* The posix advisory locking structure */ + int rc = SQLITE_OK; /* Result code form fcntl() */ + + /* Access to the unixShmNode object is serialized by the caller */ + pShmNode = pFile->pInode->pShmNode; + assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); + + /* Shared locks never span more than one byte */ + assert( n==1 || lockType!=F_RDLCK ); + + /* Locks are within range */ + assert( n>=1 && n<=SQLITE_SHM_NLOCK ); + + if( pShmNode->h>=0 ){ + /* Initialize the locking parameters */ + memset(&f, 0, sizeof(f)); + f.l_type = lockType; + f.l_whence = SEEK_SET; + f.l_start = ofst; + f.l_len = n; + + rc = osFcntl(pShmNode->h, F_SETLK, &f); + rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; + } + + /* Update the global lock state and do debug tracing */ +#ifdef SQLITE_DEBUG + { u16 mask; + OSTRACE(("SHM-LOCK ")); + mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<exclMask &= ~mask; + pShmNode->sharedMask &= ~mask; + }else if( lockType==F_RDLCK ){ + OSTRACE(("read-lock %d ok", ofst)); + pShmNode->exclMask &= ~mask; + pShmNode->sharedMask |= mask; + }else{ + assert( lockType==F_WRLCK ); + OSTRACE(("write-lock %d ok", ofst)); + pShmNode->exclMask |= mask; + pShmNode->sharedMask &= ~mask; + } + }else{ + if( lockType==F_UNLCK ){ + OSTRACE(("unlock %d failed", ofst)); + }else if( lockType==F_RDLCK ){ + OSTRACE(("read-lock failed")); + }else{ + assert( lockType==F_WRLCK ); + OSTRACE(("write-lock %d failed", ofst)); + } + } + OSTRACE((" - afterwards %03x,%03x\n", + pShmNode->sharedMask, pShmNode->exclMask)); + } +#endif + + return rc; +} + +/* +** Return the minimum number of 32KB shm regions that should be mapped at +** a time, assuming that each mapping must be an integer multiple of the +** current system page-size. +** +** Usually, this is 1. The exception seems to be systems that are configured +** to use 64KB pages - in this case each mapping must cover at least two +** shm regions. +*/ +static int unixShmRegionPerMap(void){ + int shmsz = 32*1024; /* SHM region size */ + int pgsz = osGetpagesize(); /* System page size */ + assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ + if( pgszpInode->pShmNode; + assert( unixMutexHeld() ); + if( p && ALWAYS(p->nRef==0) ){ + int nShmPerMap = unixShmRegionPerMap(); + int i; + assert( p->pInode==pFd->pInode ); + sqlite3_mutex_free(p->mutex); + for(i=0; inRegion; i+=nShmPerMap){ + if( p->h>=0 ){ + osMunmap(p->apRegion[i], p->szRegion); + }else{ + sqlite3_free(p->apRegion[i]); + } + } + sqlite3_free(p->apRegion); + if( p->h>=0 ){ + robust_close(pFd, p->h, __LINE__); + p->h = -1; + } + p->pInode->pShmNode = 0; + sqlite3_free(p); + } +} + +/* +** Open a shared-memory area associated with open database file pDbFd. +** This particular implementation uses mmapped files. +** +** The file used to implement shared-memory is in the same directory +** as the open database file and has the same name as the open database +** file with the "-shm" suffix added. For example, if the database file +** is "/home/user1/config.db" then the file that is created and mmapped +** for shared memory will be called "/home/user1/config.db-shm". +** +** Another approach to is to use files in /dev/shm or /dev/tmp or an +** some other tmpfs mount. But if a file in a different directory +** from the database file is used, then differing access permissions +** or a chroot() might cause two different processes on the same +** database to end up using different files for shared memory - +** meaning that their memory would not really be shared - resulting +** in database corruption. Nevertheless, this tmpfs file usage +** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" +** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time +** option results in an incompatible build of SQLite; builds of SQLite +** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the +** same database file at the same time, database corruption will likely +** result. The SQLITE_SHM_DIRECTORY compile-time option is considered +** "unsupported" and may go away in a future SQLite release. +** +** When opening a new shared-memory file, if no other instances of that +** file are currently open, in this process or in other processes, then +** the file must be truncated to zero length or have its header cleared. +** +** If the original database file (pDbFd) is using the "unix-excl" VFS +** that means that an exclusive lock is held on the database file and +** that no other processes are able to read or write the database. In +** that case, we do not really need shared memory. No shared memory +** file is created. The shared memory will be simulated with heap memory. +*/ +static int unixOpenSharedMemory(unixFile *pDbFd){ + struct unixShm *p = 0; /* The connection to be opened */ + struct unixShmNode *pShmNode; /* The underlying mmapped file */ + int rc; /* Result code */ + unixInodeInfo *pInode; /* The inode of fd */ + char *zShmFilename; /* Name of the file used for SHM */ + int nShmFilename; /* Size of the SHM filename in bytes */ + + /* Allocate space for the new unixShm object. */ + p = sqlite3_malloc64( sizeof(*p) ); + if( p==0 ) return SQLITE_NOMEM_BKPT; + memset(p, 0, sizeof(*p)); + assert( pDbFd->pShm==0 ); + + /* Check to see if a unixShmNode object already exists. Reuse an existing + ** one if present. Create a new one if necessary. + */ + unixEnterMutex(); + pInode = pDbFd->pInode; + pShmNode = pInode->pShmNode; + if( pShmNode==0 ){ + struct stat sStat; /* fstat() info for database file */ +#ifndef SQLITE_SHM_DIRECTORY + const char *zBasePath = pDbFd->zPath; +#endif + + /* Call fstat() to figure out the permissions on the database file. If + ** a new *-shm file is created, an attempt will be made to create it + ** with the same permissions. + */ + if( osFstat(pDbFd->h, &sStat) ){ + rc = SQLITE_IOERR_FSTAT; + goto shm_open_err; + } + +#ifdef SQLITE_SHM_DIRECTORY + nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; +#else + nShmFilename = 6 + (int)strlen(zBasePath); +#endif + pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); + if( pShmNode==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto shm_open_err; + } + memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); + zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; +#ifdef SQLITE_SHM_DIRECTORY + sqlite3_snprintf(nShmFilename, zShmFilename, + SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", + (u32)sStat.st_ino, (u32)sStat.st_dev); +#else + sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); + sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); +#endif + pShmNode->h = -1; + pDbFd->pInode->pShmNode = pShmNode; + pShmNode->pInode = pDbFd->pInode; + if( sqlite3GlobalConfig.bCoreMutex ){ + pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pShmNode->mutex==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto shm_open_err; + } + } + + if( pInode->bProcessLock==0 ){ + int openFlags = O_RDWR | O_CREAT; + if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ + openFlags = O_RDONLY; + pShmNode->isReadonly = 1; + } + pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); + if( pShmNode->h<0 ){ + rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); + goto shm_open_err; + } + + /* If this process is running as root, make sure that the SHM file + ** is owned by the same user that owns the original database. Otherwise, + ** the original owner will not be able to connect. + */ + robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); + + /* Check to see if another process is holding the dead-man switch. + ** If not, truncate the file to zero length. + */ + rc = SQLITE_OK; + if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ + if( robust_ftruncate(pShmNode->h, 0) ){ + rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); + } + } + if( rc==SQLITE_OK ){ + rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); + } + if( rc ) goto shm_open_err; + } + } + + /* Make the new connection a child of the unixShmNode */ + p->pShmNode = pShmNode; +#ifdef SQLITE_DEBUG + p->id = pShmNode->nextShmId++; +#endif + pShmNode->nRef++; + pDbFd->pShm = p; + unixLeaveMutex(); + + /* The reference count on pShmNode has already been incremented under + ** the cover of the unixEnterMutex() mutex and the pointer from the + ** new (struct unixShm) object to the pShmNode has been set. All that is + ** left to do is to link the new object into the linked list starting + ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex + ** mutex. + */ + sqlite3_mutex_enter(pShmNode->mutex); + p->pNext = pShmNode->pFirst; + pShmNode->pFirst = p; + sqlite3_mutex_leave(pShmNode->mutex); + return SQLITE_OK; + + /* Jump here on any error */ +shm_open_err: + unixShmPurge(pDbFd); /* This call frees pShmNode if required */ + sqlite3_free(p); + unixLeaveMutex(); + return rc; +} + +/* +** This function is called to obtain a pointer to region iRegion of the +** shared-memory associated with the database file fd. Shared-memory regions +** are numbered starting from zero. Each shared-memory region is szRegion +** bytes in size. +** +** If an error occurs, an error code is returned and *pp is set to NULL. +** +** Otherwise, if the bExtend parameter is 0 and the requested shared-memory +** region has not been allocated (by any client, including one running in a +** separate process), then *pp is set to NULL and SQLITE_OK returned. If +** bExtend is non-zero and the requested shared-memory region has not yet +** been allocated, it is allocated by this function. +** +** If the shared-memory region has already been allocated or is allocated by +** this call as described above, then it is mapped into this processes +** address space (if it is not already), *pp is set to point to the mapped +** memory and SQLITE_OK returned. +*/ +static int unixShmMap( + sqlite3_file *fd, /* Handle open on database file */ + int iRegion, /* Region to retrieve */ + int szRegion, /* Size of regions */ + int bExtend, /* True to extend file if necessary */ + void volatile **pp /* OUT: Mapped memory */ +){ + unixFile *pDbFd = (unixFile*)fd; + unixShm *p; + unixShmNode *pShmNode; + int rc = SQLITE_OK; + int nShmPerMap = unixShmRegionPerMap(); + int nReqRegion; + + /* If the shared-memory file has not yet been opened, open it now. */ + if( pDbFd->pShm==0 ){ + rc = unixOpenSharedMemory(pDbFd); + if( rc!=SQLITE_OK ) return rc; + } + + p = pDbFd->pShm; + pShmNode = p->pShmNode; + sqlite3_mutex_enter(pShmNode->mutex); + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); + assert( pShmNode->pInode==pDbFd->pInode ); + assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); + assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); + + /* Minimum number of regions required to be mapped. */ + nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; + + if( pShmNode->nRegionszRegion = szRegion; + + if( pShmNode->h>=0 ){ + /* The requested region is not mapped into this processes address space. + ** Check to see if it has been allocated (i.e. if the wal-index file is + ** large enough to contain the requested region). + */ + if( osFstat(pShmNode->h, &sStat) ){ + rc = SQLITE_IOERR_SHMSIZE; + goto shmpage_out; + } + + if( sStat.st_sizeh, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ + const char *zFile = pShmNode->zFilename; + rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); + goto shmpage_out; + } + } + } + } + } + + /* Map the requested memory region into this processes address space. */ + apNew = (char **)sqlite3_realloc( + pShmNode->apRegion, nReqRegion*sizeof(char *) + ); + if( !apNew ){ + rc = SQLITE_IOERR_NOMEM_BKPT; + goto shmpage_out; + } + pShmNode->apRegion = apNew; + while( pShmNode->nRegionh>=0 ){ + pMem = osMmap(0, nMap, + pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, + MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion + ); + if( pMem==MAP_FAILED ){ + rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); + goto shmpage_out; + } + }else{ + pMem = sqlite3_malloc64(szRegion); + if( pMem==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto shmpage_out; + } + memset(pMem, 0, szRegion); + } + + for(i=0; iapRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; + } + pShmNode->nRegion += nShmPerMap; + } + } + +shmpage_out: + if( pShmNode->nRegion>iRegion ){ + *pp = pShmNode->apRegion[iRegion]; + }else{ + *pp = 0; + } + if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; + sqlite3_mutex_leave(pShmNode->mutex); + return rc; +} + +/* +** Change the lock state for a shared-memory segment. +** +** Note that the relationship between SHAREd and EXCLUSIVE locks is a little +** different here than in posix. In xShmLock(), one can go from unlocked +** to shared and back or from unlocked to exclusive and back. But one may +** not go from shared to exclusive or from exclusive to shared. +*/ +static int unixShmLock( + sqlite3_file *fd, /* Database file holding the shared memory */ + int ofst, /* First lock to acquire or release */ + int n, /* Number of locks to acquire or release */ + int flags /* What to do with the lock */ +){ + unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ + unixShm *p = pDbFd->pShm; /* The shared memory being locked */ + unixShm *pX; /* For looping over all siblings */ + unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ + int rc = SQLITE_OK; /* Result code */ + u16 mask; /* Mask of locks to take or release */ + + assert( pShmNode==pDbFd->pInode->pShmNode ); + assert( pShmNode->pInode==pDbFd->pInode ); + assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); + assert( n>=1 ); + assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); + assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); + assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); + assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); + + mask = (1<<(ofst+n)) - (1<1 || mask==(1<mutex); + if( flags & SQLITE_SHM_UNLOCK ){ + u16 allMask = 0; /* Mask of locks held by siblings */ + + /* See if any siblings hold this same lock */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( pX==p ) continue; + assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); + allMask |= pX->sharedMask; + } + + /* Unlock the system-level locks */ + if( (mask & allMask)==0 ){ + rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } + + /* Undo the local locks */ + if( rc==SQLITE_OK ){ + p->exclMask &= ~mask; + p->sharedMask &= ~mask; + } + }else if( flags & SQLITE_SHM_SHARED ){ + u16 allShared = 0; /* Union of locks held by connections other than "p" */ + + /* Find out which shared locks are already held by sibling connections. + ** If any sibling already holds an exclusive lock, go ahead and return + ** SQLITE_BUSY. + */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( (pX->exclMask & mask)!=0 ){ + rc = SQLITE_BUSY; + break; + } + allShared |= pX->sharedMask; + } + + /* Get shared locks at the system level, if necessary */ + if( rc==SQLITE_OK ){ + if( (allShared & mask)==0 ){ + rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } + } + + /* Get the local shared locks */ + if( rc==SQLITE_OK ){ + p->sharedMask |= mask; + } + }else{ + /* Make sure no sibling connections hold locks that will block this + ** lock. If any do, return SQLITE_BUSY right away. + */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ + rc = SQLITE_BUSY; + break; + } + } + + /* Get the exclusive locks at the system level. Then if successful + ** also mark the local connection as being locked. + */ + if( rc==SQLITE_OK ){ + rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); + if( rc==SQLITE_OK ){ + assert( (p->sharedMask & mask)==0 ); + p->exclMask |= mask; + } + } + } + sqlite3_mutex_leave(pShmNode->mutex); + OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", + p->id, osGetpid(0), p->sharedMask, p->exclMask)); + return rc; +} + +/* +** Implement a memory barrier or memory fence on shared memory. +** +** All loads and stores begun before the barrier must complete before +** any load or store begun after the barrier. +*/ +static void unixShmBarrier( + sqlite3_file *fd /* Database file holding the shared memory */ +){ + UNUSED_PARAMETER(fd); + sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ + unixEnterMutex(); /* Also mutex, for redundancy */ + unixLeaveMutex(); +} + +/* +** Close a connection to shared-memory. Delete the underlying +** storage if deleteFlag is true. +** +** If there is no shared memory associated with the connection then this +** routine is a harmless no-op. +*/ +static int unixShmUnmap( + sqlite3_file *fd, /* The underlying database file */ + int deleteFlag /* Delete shared-memory if true */ +){ + unixShm *p; /* The connection to be closed */ + unixShmNode *pShmNode; /* The underlying shared-memory file */ + unixShm **pp; /* For looping over sibling connections */ + unixFile *pDbFd; /* The underlying database file */ + + pDbFd = (unixFile*)fd; + p = pDbFd->pShm; + if( p==0 ) return SQLITE_OK; + pShmNode = p->pShmNode; + + assert( pShmNode==pDbFd->pInode->pShmNode ); + assert( pShmNode->pInode==pDbFd->pInode ); + + /* Remove connection p from the set of connections associated + ** with pShmNode */ + sqlite3_mutex_enter(pShmNode->mutex); + for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} + *pp = p->pNext; + + /* Free the connection p */ + sqlite3_free(p); + pDbFd->pShm = 0; + sqlite3_mutex_leave(pShmNode->mutex); + + /* If pShmNode->nRef has reached 0, then close the underlying + ** shared-memory file, too */ + unixEnterMutex(); + assert( pShmNode->nRef>0 ); + pShmNode->nRef--; + if( pShmNode->nRef==0 ){ + if( deleteFlag && pShmNode->h>=0 ){ + osUnlink(pShmNode->zFilename); + } + unixShmPurge(pDbFd); + } + unixLeaveMutex(); + + return SQLITE_OK; +} + + +#else +# define unixShmMap 0 +# define unixShmLock 0 +# define unixShmBarrier 0 +# define unixShmUnmap 0 +#endif /* #ifndef SQLITE_OMIT_WAL */ + +#if SQLITE_MAX_MMAP_SIZE>0 +/* +** If it is currently memory mapped, unmap file pFd. +*/ +static void unixUnmapfile(unixFile *pFd){ + assert( pFd->nFetchOut==0 ); + if( pFd->pMapRegion ){ + osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); + pFd->pMapRegion = 0; + pFd->mmapSize = 0; + pFd->mmapSizeActual = 0; + } +} + +/* +** Attempt to set the size of the memory mapping maintained by file +** descriptor pFd to nNew bytes. Any existing mapping is discarded. +** +** If successful, this function sets the following variables: +** +** unixFile.pMapRegion +** unixFile.mmapSize +** unixFile.mmapSizeActual +** +** If unsuccessful, an error message is logged via sqlite3_log() and +** the three variables above are zeroed. In this case SQLite should +** continue accessing the database using the xRead() and xWrite() +** methods. +*/ +static void unixRemapfile( + unixFile *pFd, /* File descriptor object */ + i64 nNew /* Required mapping size */ +){ + const char *zErr = "mmap"; + int h = pFd->h; /* File descriptor open on db file */ + u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ + i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ + u8 *pNew = 0; /* Location of new mapping */ + int flags = PROT_READ; /* Flags to pass to mmap() */ + + assert( pFd->nFetchOut==0 ); + assert( nNew>pFd->mmapSize ); + assert( nNew<=pFd->mmapSizeMax ); + assert( nNew>0 ); + assert( pFd->mmapSizeActual>=pFd->mmapSize ); + assert( MAP_FAILED!=0 ); + +#ifdef SQLITE_MMAP_READWRITE + if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; +#endif + + if( pOrig ){ +#if HAVE_MREMAP + i64 nReuse = pFd->mmapSize; +#else + const int szSyspage = osGetpagesize(); + i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); +#endif + u8 *pReq = &pOrig[nReuse]; + + /* Unmap any pages of the existing mapping that cannot be reused. */ + if( nReuse!=nOrig ){ + osMunmap(pReq, nOrig-nReuse); + } + +#if HAVE_MREMAP + pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); + zErr = "mremap"; +#else + pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); + if( pNew!=MAP_FAILED ){ + if( pNew!=pReq ){ + osMunmap(pNew, nNew - nReuse); + pNew = 0; + }else{ + pNew = pOrig; + } + } +#endif + + /* The attempt to extend the existing mapping failed. Free it. */ + if( pNew==MAP_FAILED || pNew==0 ){ + osMunmap(pOrig, nReuse); + } + } + + /* If pNew is still NULL, try to create an entirely new mapping. */ + if( pNew==0 ){ + pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); + } + + if( pNew==MAP_FAILED ){ + pNew = 0; + nNew = 0; + unixLogError(SQLITE_OK, zErr, pFd->zPath); + + /* If the mmap() above failed, assume that all subsequent mmap() calls + ** will probably fail too. Fall back to using xRead/xWrite exclusively + ** in this case. */ + pFd->mmapSizeMax = 0; + } + pFd->pMapRegion = (void *)pNew; + pFd->mmapSize = pFd->mmapSizeActual = nNew; +} + +/* +** Memory map or remap the file opened by file-descriptor pFd (if the file +** is already mapped, the existing mapping is replaced by the new). Or, if +** there already exists a mapping for this file, and there are still +** outstanding xFetch() references to it, this function is a no-op. +** +** If parameter nByte is non-negative, then it is the requested size of +** the mapping to create. Otherwise, if nByte is less than zero, then the +** requested size is the size of the file on disk. The actual size of the +** created mapping is either the requested size or the value configured +** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. +** +** SQLITE_OK is returned if no error occurs (even if the mapping is not +** recreated as a result of outstanding references) or an SQLite error +** code otherwise. +*/ +static int unixMapfile(unixFile *pFd, i64 nMap){ + assert( nMap>=0 || pFd->nFetchOut==0 ); + assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); + if( pFd->nFetchOut>0 ) return SQLITE_OK; + + if( nMap<0 ){ + struct stat statbuf; /* Low-level file information */ + if( osFstat(pFd->h, &statbuf) ){ + return SQLITE_IOERR_FSTAT; + } + nMap = statbuf.st_size; + } + if( nMap>pFd->mmapSizeMax ){ + nMap = pFd->mmapSizeMax; + } + + assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); + if( nMap!=pFd->mmapSize ){ + unixRemapfile(pFd, nMap); + } + + return SQLITE_OK; +} +#endif /* SQLITE_MAX_MMAP_SIZE>0 */ + +/* +** If possible, return a pointer to a mapping of file fd starting at offset +** iOff. The mapping must be valid for at least nAmt bytes. +** +** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. +** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. +** Finally, if an error does occur, return an SQLite error code. The final +** value of *pp is undefined in this case. +** +** If this function does return a pointer, the caller must eventually +** release the reference by calling unixUnfetch(). +*/ +static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ +#if SQLITE_MAX_MMAP_SIZE>0 + unixFile *pFd = (unixFile *)fd; /* The underlying database file */ +#endif + *pp = 0; + +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFd->mmapSizeMax>0 ){ + if( pFd->pMapRegion==0 ){ + int rc = unixMapfile(pFd, -1); + if( rc!=SQLITE_OK ) return rc; + } + if( pFd->mmapSize >= iOff+nAmt ){ + *pp = &((u8 *)pFd->pMapRegion)[iOff]; + pFd->nFetchOut++; + } + } +#endif + return SQLITE_OK; +} + +/* +** If the third argument is non-NULL, then this function releases a +** reference obtained by an earlier call to unixFetch(). The second +** argument passed to this function must be the same as the corresponding +** argument that was passed to the unixFetch() invocation. +** +** Or, if the third argument is NULL, then this function is being called +** to inform the VFS layer that, according to POSIX, any existing mapping +** may now be invalid and should be unmapped. +*/ +static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ +#if SQLITE_MAX_MMAP_SIZE>0 + unixFile *pFd = (unixFile *)fd; /* The underlying database file */ + UNUSED_PARAMETER(iOff); + + /* If p==0 (unmap the entire file) then there must be no outstanding + ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), + ** then there must be at least one outstanding. */ + assert( (p==0)==(pFd->nFetchOut==0) ); + + /* If p!=0, it must match the iOff value. */ + assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); + + if( p ){ + pFd->nFetchOut--; + }else{ + unixUnmapfile(pFd); + } + + assert( pFd->nFetchOut>=0 ); +#else + UNUSED_PARAMETER(fd); + UNUSED_PARAMETER(p); + UNUSED_PARAMETER(iOff); +#endif + return SQLITE_OK; +} + +/* +** Here ends the implementation of all sqlite3_file methods. +** +********************** End sqlite3_file Methods ******************************* +******************************************************************************/ + +/* +** This division contains definitions of sqlite3_io_methods objects that +** implement various file locking strategies. It also contains definitions +** of "finder" functions. A finder-function is used to locate the appropriate +** sqlite3_io_methods object for a particular database file. The pAppData +** field of the sqlite3_vfs VFS objects are initialized to be pointers to +** the correct finder-function for that VFS. +** +** Most finder functions return a pointer to a fixed sqlite3_io_methods +** object. The only interesting finder-function is autolockIoFinder, which +** looks at the filesystem type and tries to guess the best locking +** strategy from that. +** +** For finder-function F, two objects are created: +** +** (1) The real finder-function named "FImpt()". +** +** (2) A constant pointer to this function named just "F". +** +** +** A pointer to the F pointer is used as the pAppData value for VFS +** objects. We have to do this instead of letting pAppData point +** directly at the finder-function since C90 rules prevent a void* +** from be cast into a function pointer. +** +** +** Each instance of this macro generates two objects: +** +** * A constant sqlite3_io_methods object call METHOD that has locking +** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. +** +** * An I/O method finder function called FINDER that returns a pointer +** to the METHOD object in the previous bullet. +*/ +#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ +static const sqlite3_io_methods METHOD = { \ + VERSION, /* iVersion */ \ + CLOSE, /* xClose */ \ + unixRead, /* xRead */ \ + unixWrite, /* xWrite */ \ + unixTruncate, /* xTruncate */ \ + unixSync, /* xSync */ \ + unixFileSize, /* xFileSize */ \ + LOCK, /* xLock */ \ + UNLOCK, /* xUnlock */ \ + CKLOCK, /* xCheckReservedLock */ \ + unixFileControl, /* xFileControl */ \ + unixSectorSize, /* xSectorSize */ \ + unixDeviceCharacteristics, /* xDeviceCapabilities */ \ + SHMMAP, /* xShmMap */ \ + unixShmLock, /* xShmLock */ \ + unixShmBarrier, /* xShmBarrier */ \ + unixShmUnmap, /* xShmUnmap */ \ + unixFetch, /* xFetch */ \ + unixUnfetch, /* xUnfetch */ \ +}; \ +static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ + UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ + return &METHOD; \ +} \ +static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ + = FINDER##Impl; + +/* +** Here are all of the sqlite3_io_methods objects for each of the +** locking strategies. Functions that return pointers to these methods +** are also created. +*/ +IOMETHODS( + posixIoFinder, /* Finder function name */ + posixIoMethods, /* sqlite3_io_methods object name */ + 3, /* shared memory and mmap are enabled */ + unixClose, /* xClose method */ + unixLock, /* xLock method */ + unixUnlock, /* xUnlock method */ + unixCheckReservedLock, /* xCheckReservedLock method */ + unixShmMap /* xShmMap method */ +) +IOMETHODS( + nolockIoFinder, /* Finder function name */ + nolockIoMethods, /* sqlite3_io_methods object name */ + 3, /* shared memory is disabled */ + nolockClose, /* xClose method */ + nolockLock, /* xLock method */ + nolockUnlock, /* xUnlock method */ + nolockCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +IOMETHODS( + dotlockIoFinder, /* Finder function name */ + dotlockIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + dotlockClose, /* xClose method */ + dotlockLock, /* xLock method */ + dotlockUnlock, /* xUnlock method */ + dotlockCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) + +#if SQLITE_ENABLE_LOCKING_STYLE +IOMETHODS( + flockIoFinder, /* Finder function name */ + flockIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + flockClose, /* xClose method */ + flockLock, /* xLock method */ + flockUnlock, /* xUnlock method */ + flockCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +#endif + +#if OS_VXWORKS +IOMETHODS( + semIoFinder, /* Finder function name */ + semIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + semXClose, /* xClose method */ + semXLock, /* xLock method */ + semXUnlock, /* xUnlock method */ + semXCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +#endif + +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +IOMETHODS( + afpIoFinder, /* Finder function name */ + afpIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + afpClose, /* xClose method */ + afpLock, /* xLock method */ + afpUnlock, /* xUnlock method */ + afpCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +#endif + +/* +** The proxy locking method is a "super-method" in the sense that it +** opens secondary file descriptors for the conch and lock files and +** it uses proxy, dot-file, AFP, and flock() locking methods on those +** secondary files. For this reason, the division that implements +** proxy locking is located much further down in the file. But we need +** to go ahead and define the sqlite3_io_methods and finder function +** for proxy locking here. So we forward declare the I/O methods. +*/ +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +static int proxyClose(sqlite3_file*); +static int proxyLock(sqlite3_file*, int); +static int proxyUnlock(sqlite3_file*, int); +static int proxyCheckReservedLock(sqlite3_file*, int*); +IOMETHODS( + proxyIoFinder, /* Finder function name */ + proxyIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + proxyClose, /* xClose method */ + proxyLock, /* xLock method */ + proxyUnlock, /* xUnlock method */ + proxyCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +#endif + +/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +IOMETHODS( + nfsIoFinder, /* Finder function name */ + nfsIoMethods, /* sqlite3_io_methods object name */ + 1, /* shared memory is disabled */ + unixClose, /* xClose method */ + unixLock, /* xLock method */ + nfsUnlock, /* xUnlock method */ + unixCheckReservedLock, /* xCheckReservedLock method */ + 0 /* xShmMap method */ +) +#endif + +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +/* +** This "finder" function attempts to determine the best locking strategy +** for the database file "filePath". It then returns the sqlite3_io_methods +** object that implements that strategy. +** +** This is for MacOSX only. +*/ +static const sqlite3_io_methods *autolockIoFinderImpl( + const char *filePath, /* name of the database file */ + unixFile *pNew /* open file object for the database file */ +){ + static const struct Mapping { + const char *zFilesystem; /* Filesystem type name */ + const sqlite3_io_methods *pMethods; /* Appropriate locking method */ + } aMap[] = { + { "hfs", &posixIoMethods }, + { "ufs", &posixIoMethods }, + { "afpfs", &afpIoMethods }, + { "smbfs", &afpIoMethods }, + { "webdav", &nolockIoMethods }, + { 0, 0 } + }; + int i; + struct statfs fsInfo; + struct flock lockInfo; + + if( !filePath ){ + /* If filePath==NULL that means we are dealing with a transient file + ** that does not need to be locked. */ + return &nolockIoMethods; + } + if( statfs(filePath, &fsInfo) != -1 ){ + if( fsInfo.f_flags & MNT_RDONLY ){ + return &nolockIoMethods; + } + for(i=0; aMap[i].zFilesystem; i++){ + if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ + return aMap[i].pMethods; + } + } + } + + /* Default case. Handles, amongst others, "nfs". + ** Test byte-range lock using fcntl(). If the call succeeds, + ** assume that the file-system supports POSIX style locks. + */ + lockInfo.l_len = 1; + lockInfo.l_start = 0; + lockInfo.l_whence = SEEK_SET; + lockInfo.l_type = F_RDLCK; + if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { + if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ + return &nfsIoMethods; + } else { + return &posixIoMethods; + } + }else{ + return &dotlockIoMethods; + } +} +static const sqlite3_io_methods + *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; + +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ + +#if OS_VXWORKS +/* +** This "finder" function for VxWorks checks to see if posix advisory +** locking works. If it does, then that is what is used. If it does not +** work, then fallback to named semaphore locking. +*/ +static const sqlite3_io_methods *vxworksIoFinderImpl( + const char *filePath, /* name of the database file */ + unixFile *pNew /* the open file object */ +){ + struct flock lockInfo; + + if( !filePath ){ + /* If filePath==NULL that means we are dealing with a transient file + ** that does not need to be locked. */ + return &nolockIoMethods; + } + + /* Test if fcntl() is supported and use POSIX style locks. + ** Otherwise fall back to the named semaphore method. + */ + lockInfo.l_len = 1; + lockInfo.l_start = 0; + lockInfo.l_whence = SEEK_SET; + lockInfo.l_type = F_RDLCK; + if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { + return &posixIoMethods; + }else{ + return &semIoMethods; + } +} +static const sqlite3_io_methods + *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; + +#endif /* OS_VXWORKS */ + +/* +** An abstract type for a pointer to an IO method finder function: +*/ +typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); + + +/**************************************************************************** +**************************** sqlite3_vfs methods **************************** +** +** This division contains the implementation of methods on the +** sqlite3_vfs object. +*/ + +/* +** Initialize the contents of the unixFile structure pointed to by pId. +*/ +static int fillInUnixFile( + sqlite3_vfs *pVfs, /* Pointer to vfs object */ + int h, /* Open file descriptor of file being opened */ + sqlite3_file *pId, /* Write to the unixFile structure here */ + const char *zFilename, /* Name of the file being opened */ + int ctrlFlags /* Zero or more UNIXFILE_* values */ +){ + const sqlite3_io_methods *pLockingStyle; + unixFile *pNew = (unixFile *)pId; + int rc = SQLITE_OK; + + assert( pNew->pInode==NULL ); + + /* Usually the path zFilename should not be a relative pathname. The + ** exception is when opening the proxy "conch" file in builds that + ** include the special Apple locking styles. + */ +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE + assert( zFilename==0 || zFilename[0]=='/' + || pVfs->pAppData==(void*)&autolockIoFinder ); +#else + assert( zFilename==0 || zFilename[0]=='/' ); +#endif + + /* No locking occurs in temporary files */ + assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); + + OSTRACE(("OPEN %-3d %s\n", h, zFilename)); + pNew->h = h; + pNew->pVfs = pVfs; + pNew->zPath = zFilename; + pNew->ctrlFlags = (u8)ctrlFlags; +#if SQLITE_MAX_MMAP_SIZE>0 + pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; +#endif + if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), + "psow", SQLITE_POWERSAFE_OVERWRITE) ){ + pNew->ctrlFlags |= UNIXFILE_PSOW; + } + if( strcmp(pVfs->zName,"unix-excl")==0 ){ + pNew->ctrlFlags |= UNIXFILE_EXCL; + } + +#if OS_VXWORKS + pNew->pId = vxworksFindFileId(zFilename); + if( pNew->pId==0 ){ + ctrlFlags |= UNIXFILE_NOLOCK; + rc = SQLITE_NOMEM_BKPT; + } +#endif + + if( ctrlFlags & UNIXFILE_NOLOCK ){ + pLockingStyle = &nolockIoMethods; + }else{ + pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); +#if SQLITE_ENABLE_LOCKING_STYLE + /* Cache zFilename in the locking context (AFP and dotlock override) for + ** proxyLock activation is possible (remote proxy is based on db name) + ** zFilename remains valid until file is closed, to support */ + pNew->lockingContext = (void*)zFilename; +#endif + } + + if( pLockingStyle == &posixIoMethods +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE + || pLockingStyle == &nfsIoMethods +#endif + ){ + unixEnterMutex(); + rc = findInodeInfo(pNew, &pNew->pInode); + if( rc!=SQLITE_OK ){ + /* If an error occurred in findInodeInfo(), close the file descriptor + ** immediately, before releasing the mutex. findInodeInfo() may fail + ** in two scenarios: + ** + ** (a) A call to fstat() failed. + ** (b) A malloc failed. + ** + ** Scenario (b) may only occur if the process is holding no other + ** file descriptors open on the same file. If there were other file + ** descriptors on this file, then no malloc would be required by + ** findInodeInfo(). If this is the case, it is quite safe to close + ** handle h - as it is guaranteed that no posix locks will be released + ** by doing so. + ** + ** If scenario (a) caused the error then things are not so safe. The + ** implicit assumption here is that if fstat() fails, things are in + ** such bad shape that dropping a lock or two doesn't matter much. + */ + robust_close(pNew, h, __LINE__); + h = -1; + } + unixLeaveMutex(); + } + +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) + else if( pLockingStyle == &afpIoMethods ){ + /* AFP locking uses the file path so it needs to be included in + ** the afpLockingContext. + */ + afpLockingContext *pCtx; + pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); + if( pCtx==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + /* NB: zFilename exists and remains valid until the file is closed + ** according to requirement F11141. So we do not need to make a + ** copy of the filename. */ + pCtx->dbPath = zFilename; + pCtx->reserved = 0; + srandomdev(); + unixEnterMutex(); + rc = findInodeInfo(pNew, &pNew->pInode); + if( rc!=SQLITE_OK ){ + sqlite3_free(pNew->lockingContext); + robust_close(pNew, h, __LINE__); + h = -1; + } + unixLeaveMutex(); + } + } +#endif + + else if( pLockingStyle == &dotlockIoMethods ){ + /* Dotfile locking uses the file path so it needs to be included in + ** the dotlockLockingContext + */ + char *zLockFile; + int nFilename; + assert( zFilename!=0 ); + nFilename = (int)strlen(zFilename) + 6; + zLockFile = (char *)sqlite3_malloc64(nFilename); + if( zLockFile==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); + } + pNew->lockingContext = zLockFile; + } + +#if OS_VXWORKS + else if( pLockingStyle == &semIoMethods ){ + /* Named semaphore locking uses the file path so it needs to be + ** included in the semLockingContext + */ + unixEnterMutex(); + rc = findInodeInfo(pNew, &pNew->pInode); + if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ + char *zSemName = pNew->pInode->aSemName; + int n; + sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", + pNew->pId->zCanonicalName); + for( n=1; zSemName[n]; n++ ) + if( zSemName[n]=='/' ) zSemName[n] = '_'; + pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); + if( pNew->pInode->pSem == SEM_FAILED ){ + rc = SQLITE_NOMEM_BKPT; + pNew->pInode->aSemName[0] = '\0'; + } + } + unixLeaveMutex(); + } +#endif + + storeLastErrno(pNew, 0); +#if OS_VXWORKS + if( rc!=SQLITE_OK ){ + if( h>=0 ) robust_close(pNew, h, __LINE__); + h = -1; + osUnlink(zFilename); + pNew->ctrlFlags |= UNIXFILE_DELETE; + } +#endif + if( rc!=SQLITE_OK ){ + if( h>=0 ) robust_close(pNew, h, __LINE__); + }else{ + pNew->pMethod = pLockingStyle; + OpenCounter(+1); + verifyDbFile(pNew); + } + return rc; +} + +/* +** Return the name of a directory in which to put temporary files. +** If no suitable temporary file directory can be found, return NULL. +*/ +static const char *unixTempFileDir(void){ + static const char *azDirs[] = { + 0, + 0, + "/var/tmp", + "/usr/tmp", + "/tmp", + "." + }; + unsigned int i = 0; + struct stat buf; + const char *zDir = sqlite3_temp_directory; + + if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); + if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); + while(1){ + if( zDir!=0 + && osStat(zDir, &buf)==0 + && S_ISDIR(buf.st_mode) + && osAccess(zDir, 03)==0 + ){ + return zDir; + } + if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; + zDir = azDirs[i++]; + } + return 0; +} + +/* +** Create a temporary file name in zBuf. zBuf must be allocated +** by the calling process and must be big enough to hold at least +** pVfs->mxPathname bytes. +*/ +static int unixGetTempname(int nBuf, char *zBuf){ + const char *zDir; + int iLimit = 0; + + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this + ** function failing. + */ + zBuf[0] = 0; + SimulateIOError( return SQLITE_IOERR ); + + zDir = unixTempFileDir(); + if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; + do{ + u64 r; + sqlite3_randomness(sizeof(r), &r); + assert( nBuf>2 ); + zBuf[nBuf-2] = 0; + sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", + zDir, r, 0); + if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; + }while( osAccess(zBuf,0)==0 ); + return SQLITE_OK; +} + +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) +/* +** Routine to transform a unixFile into a proxy-locking unixFile. +** Implementation in the proxy-lock division, but used by unixOpen() +** if SQLITE_PREFER_PROXY_LOCKING is defined. +*/ +static int proxyTransformUnixFile(unixFile*, const char*); +#endif + +/* +** Search for an unused file descriptor that was opened on the database +** file (not a journal or master-journal file) identified by pathname +** zPath with SQLITE_OPEN_XXX flags matching those passed as the second +** argument to this function. +** +** Such a file descriptor may exist if a database connection was closed +** but the associated file descriptor could not be closed because some +** other file descriptor open on the same file is holding a file-lock. +** Refer to comments in the unixClose() function and the lengthy comment +** describing "Posix Advisory Locking" at the start of this file for +** further details. Also, ticket #4018. +** +** If a suitable file descriptor is found, then it is returned. If no +** such file descriptor is located, -1 is returned. +*/ +static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ + UnixUnusedFd *pUnused = 0; + + /* Do not search for an unused file descriptor on vxworks. Not because + ** vxworks would not benefit from the change (it might, we're not sure), + ** but because no way to test it is currently available. It is better + ** not to risk breaking vxworks support for the sake of such an obscure + ** feature. */ +#if !OS_VXWORKS + struct stat sStat; /* Results of stat() call */ + + /* A stat() call may fail for various reasons. If this happens, it is + ** almost certain that an open() call on the same path will also fail. + ** For this reason, if an error occurs in the stat() call here, it is + ** ignored and -1 is returned. The caller will try to open a new file + ** descriptor on the same path, fail, and return an error to SQLite. + ** + ** Even if a subsequent open() call does succeed, the consequences of + ** not searching for a reusable file descriptor are not dire. */ + if( 0==osStat(zPath, &sStat) ){ + unixInodeInfo *pInode; + + unixEnterMutex(); + pInode = inodeList; + while( pInode && (pInode->fileId.dev!=sStat.st_dev + || pInode->fileId.ino!=sStat.st_ino) ){ + pInode = pInode->pNext; + } + if( pInode ){ + UnixUnusedFd **pp; + for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); + pUnused = *pp; + if( pUnused ){ + *pp = pUnused->pNext; + } + } + unixLeaveMutex(); + } +#endif /* if !OS_VXWORKS */ + return pUnused; +} + +/* +** This function is called by unixOpen() to determine the unix permissions +** to create new files with. If no error occurs, then SQLITE_OK is returned +** and a value suitable for passing as the third argument to open(2) is +** written to *pMode. If an IO error occurs, an SQLite error code is +** returned and the value of *pMode is not modified. +** +** In most cases, this routine sets *pMode to 0, which will become +** an indication to robust_open() to create the file using +** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. +** But if the file being opened is a WAL or regular journal file, then +** this function queries the file-system for the permissions on the +** corresponding database file and sets *pMode to this value. Whenever +** possible, WAL and journal files are created using the same permissions +** as the associated database file. +** +** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the +** original filename is unavailable. But 8_3_NAMES is only used for +** FAT filesystems and permissions do not matter there, so just use +** the default permissions. +*/ +static int findCreateFileMode( + const char *zPath, /* Path of file (possibly) being created */ + int flags, /* Flags passed as 4th argument to xOpen() */ + mode_t *pMode, /* OUT: Permissions to open file with */ + uid_t *pUid, /* OUT: uid to set on the file */ + gid_t *pGid /* OUT: gid to set on the file */ +){ + int rc = SQLITE_OK; /* Return Code */ + *pMode = 0; + *pUid = 0; + *pGid = 0; + if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ + char zDb[MAX_PATHNAME+1]; /* Database file path */ + int nDb; /* Number of valid bytes in zDb */ + struct stat sStat; /* Output of stat() on database file */ + + /* zPath is a path to a WAL or journal file. The following block derives + ** the path to the associated database file from zPath. This block handles + ** the following naming conventions: + ** + ** "-journal" + ** "-wal" + ** "-journalNN" + ** "-walNN" + ** + ** where NN is a decimal number. The NN naming schemes are + ** used by the test_multiplex.c module. + */ + nDb = sqlite3Strlen30(zPath) - 1; + while( zPath[nDb]!='-' ){ +#ifndef SQLITE_ENABLE_8_3_NAMES + /* In the normal case (8+3 filenames disabled) the journal filename + ** is guaranteed to contain a '-' character. */ + assert( nDb>0 ); + assert( sqlite3Isalnum(zPath[nDb]) ); +#else + /* If 8+3 names are possible, then the journal file might not contain + ** a '-' character. So check for that case and return early. */ + if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; +#endif + nDb--; + } + memcpy(zDb, zPath, nDb); + zDb[nDb] = '\0'; + + if( 0==osStat(zDb, &sStat) ){ + *pMode = sStat.st_mode & 0777; + *pUid = sStat.st_uid; + *pGid = sStat.st_gid; + }else{ + rc = SQLITE_IOERR_FSTAT; + } + }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ + *pMode = 0600; + } + return rc; +} + +/* +** Open the file zPath. +** +** Previously, the SQLite OS layer used three functions in place of this +** one: +** +** sqlite3OsOpenReadWrite(); +** sqlite3OsOpenReadOnly(); +** sqlite3OsOpenExclusive(); +** +** These calls correspond to the following combinations of flags: +** +** ReadWrite() -> (READWRITE | CREATE) +** ReadOnly() -> (READONLY) +** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) +** +** The old OpenExclusive() accepted a boolean argument - "delFlag". If +** true, the file was configured to be automatically deleted when the +** file handle closed. To achieve the same effect using this new +** interface, add the DELETEONCLOSE flag to those specified above for +** OpenExclusive(). +*/ +static int unixOpen( + sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ + const char *zPath, /* Pathname of file to be opened */ + sqlite3_file *pFile, /* The file descriptor to be filled in */ + int flags, /* Input flags to control the opening */ + int *pOutFlags /* Output flags returned to SQLite core */ +){ + unixFile *p = (unixFile *)pFile; + int fd = -1; /* File descriptor returned by open() */ + int openFlags = 0; /* Flags to pass to open() */ + int eType = flags&0xFFFFFF00; /* Type of file to open */ + int noLock; /* True to omit locking primitives */ + int rc = SQLITE_OK; /* Function Return Code */ + int ctrlFlags = 0; /* UNIXFILE_* flags */ + + int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); + int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); + int isCreate = (flags & SQLITE_OPEN_CREATE); + int isReadonly = (flags & SQLITE_OPEN_READONLY); + int isReadWrite = (flags & SQLITE_OPEN_READWRITE); +#if SQLITE_ENABLE_LOCKING_STYLE + int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); +#endif +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE + struct statfs fsInfo; +#endif + + /* If creating a master or main-file journal, this function will open + ** a file-descriptor on the directory too. The first time unixSync() + ** is called the directory file descriptor will be fsync()ed and close()d. + */ + int syncDir = (isCreate && ( + eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_MAIN_JOURNAL + || eType==SQLITE_OPEN_WAL + )); + + /* If argument zPath is a NULL pointer, this function is required to open + ** a temporary file. Use this buffer to store the file name in. + */ + char zTmpname[MAX_PATHNAME+2]; + const char *zName = zPath; + + /* Check the following statements are true: + ** + ** (a) Exactly one of the READWRITE and READONLY flags must be set, and + ** (b) if CREATE is set, then READWRITE must also be set, and + ** (c) if EXCLUSIVE is set, then CREATE must also be set. + ** (d) if DELETEONCLOSE is set, then CREATE must also be set. + */ + assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); + assert(isCreate==0 || isReadWrite); + assert(isExclusive==0 || isCreate); + assert(isDelete==0 || isCreate); + + /* The main DB, main journal, WAL file and master journal are never + ** automatically deleted. Nor are they ever temporary files. */ + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); + + /* Assert that the upper layer has set one of the "file-type" flags. */ + assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB + || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL + || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL + ); + + /* Detect a pid change and reset the PRNG. There is a race condition + ** here such that two or more threads all trying to open databases at + ** the same instant might all reset the PRNG. But multiple resets + ** are harmless. + */ + if( randomnessPid!=osGetpid(0) ){ + randomnessPid = osGetpid(0); + sqlite3_randomness(0,0); + } + + memset(p, 0, sizeof(unixFile)); + + if( eType==SQLITE_OPEN_MAIN_DB ){ + UnixUnusedFd *pUnused; + pUnused = findReusableFd(zName, flags); + if( pUnused ){ + fd = pUnused->fd; + }else{ + pUnused = sqlite3_malloc64(sizeof(*pUnused)); + if( !pUnused ){ + return SQLITE_NOMEM_BKPT; + } + } + p->pUnused = pUnused; + + /* Database filenames are double-zero terminated if they are not + ** URIs with parameters. Hence, they can always be passed into + ** sqlite3_uri_parameter(). */ + assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); + + }else if( !zName ){ + /* If zName is NULL, the upper layer is requesting a temp file. */ + assert(isDelete && !syncDir); + rc = unixGetTempname(pVfs->mxPathname, zTmpname); + if( rc!=SQLITE_OK ){ + return rc; + } + zName = zTmpname; + + /* Generated temporary filenames are always double-zero terminated + ** for use by sqlite3_uri_parameter(). */ + assert( zName[strlen(zName)+1]==0 ); + } + + /* Determine the value of the flags parameter passed to POSIX function + ** open(). These must be calculated even if open() is not called, as + ** they may be stored as part of the file handle and used by the + ** 'conch file' locking functions later on. */ + if( isReadonly ) openFlags |= O_RDONLY; + if( isReadWrite ) openFlags |= O_RDWR; + if( isCreate ) openFlags |= O_CREAT; + if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); + openFlags |= (O_LARGEFILE|O_BINARY); + + if( fd<0 ){ + mode_t openMode; /* Permissions to create file with */ + uid_t uid; /* Userid for the file */ + gid_t gid; /* Groupid for the file */ + rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); + if( rc!=SQLITE_OK ){ + assert( !p->pUnused ); + assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); + return rc; + } + fd = robust_open(zName, openFlags, openMode); + OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); + assert( !isExclusive || (openFlags & O_CREAT)!=0 ); + if( fd<0 && errno!=EISDIR && isReadWrite ){ + /* Failed to open the file for read/write access. Try read-only. */ + flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + openFlags &= ~(O_RDWR|O_CREAT); + flags |= SQLITE_OPEN_READONLY; + openFlags |= O_RDONLY; + isReadonly = 1; + fd = robust_open(zName, openFlags, openMode); + } + if( fd<0 ){ + rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); + goto open_finished; + } + + /* If this process is running as root and if creating a new rollback + ** journal or WAL file, set the ownership of the journal or WAL to be + ** the same as the original database. + */ + if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ + robustFchown(fd, uid, gid); + } + } + assert( fd>=0 ); + if( pOutFlags ){ + *pOutFlags = flags; + } + + if( p->pUnused ){ + p->pUnused->fd = fd; + p->pUnused->flags = flags; + } + + if( isDelete ){ +#if OS_VXWORKS + zPath = zName; +#elif defined(SQLITE_UNLINK_AFTER_CLOSE) + zPath = sqlite3_mprintf("%s", zName); + if( zPath==0 ){ + robust_close(p, fd, __LINE__); + return SQLITE_NOMEM_BKPT; + } +#else + osUnlink(zName); +#endif + } +#if SQLITE_ENABLE_LOCKING_STYLE + else{ + p->openFlags = openFlags; + } +#endif + +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE + if( fstatfs(fd, &fsInfo) == -1 ){ + storeLastErrno(p, errno); + robust_close(p, fd, __LINE__); + return SQLITE_IOERR_ACCESS; + } + if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { + ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; + } + if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { + ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; + } +#endif + + /* Set up appropriate ctrlFlags */ + if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; + if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; + noLock = eType!=SQLITE_OPEN_MAIN_DB; + if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; + if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; + if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; + +#if SQLITE_ENABLE_LOCKING_STYLE +#if SQLITE_PREFER_PROXY_LOCKING + isAutoProxy = 1; +#endif + if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ + char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); + int useProxy = 0; + + /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means + ** never use proxy, NULL means use proxy for non-local files only. */ + if( envforce!=NULL ){ + useProxy = atoi(envforce)>0; + }else{ + useProxy = !(fsInfo.f_flags&MNT_LOCAL); + } + if( useProxy ){ + rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); + if( rc==SQLITE_OK ){ + rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); + if( rc!=SQLITE_OK ){ + /* Use unixClose to clean up the resources added in fillInUnixFile + ** and clear all the structure's references. Specifically, + ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op + */ + unixClose(pFile); + return rc; + } + } + goto open_finished; + } + } +#endif + + rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); + +open_finished: + if( rc!=SQLITE_OK ){ + sqlite3_free(p->pUnused); + } + return rc; +} + + +/* +** Delete the file at zPath. If the dirSync argument is true, fsync() +** the directory after deleting the file. +*/ +static int unixDelete( + sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ + const char *zPath, /* Name of file to be deleted */ + int dirSync /* If true, fsync() directory after deleting file */ +){ + int rc = SQLITE_OK; + UNUSED_PARAMETER(NotUsed); + SimulateIOError(return SQLITE_IOERR_DELETE); + if( osUnlink(zPath)==(-1) ){ + if( errno==ENOENT +#if OS_VXWORKS + || osAccess(zPath,0)!=0 +#endif + ){ + rc = SQLITE_IOERR_DELETE_NOENT; + }else{ + rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); + } + return rc; + } +#ifndef SQLITE_DISABLE_DIRSYNC + if( (dirSync & 1)!=0 ){ + int fd; + rc = osOpenDirectory(zPath, &fd); + if( rc==SQLITE_OK ){ + if( full_fsync(fd,0,0) ){ + rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); + } + robust_close(0, fd, __LINE__); + }else{ + assert( rc==SQLITE_CANTOPEN ); + rc = SQLITE_OK; + } + } +#endif + return rc; +} + +/* +** Test the existence of or access permissions of file zPath. The +** test performed depends on the value of flags: +** +** SQLITE_ACCESS_EXISTS: Return 1 if the file exists +** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. +** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. +** +** Otherwise return 0. +*/ +static int unixAccess( + sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ + const char *zPath, /* Path of the file to examine */ + int flags, /* What do we want to learn about the zPath file? */ + int *pResOut /* Write result boolean here */ +){ + UNUSED_PARAMETER(NotUsed); + SimulateIOError( return SQLITE_IOERR_ACCESS; ); + assert( pResOut!=0 ); + + /* The spec says there are three possible values for flags. But only + ** two of them are actually used */ + assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); + + if( flags==SQLITE_ACCESS_EXISTS ){ + struct stat buf; + *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); + }else{ + *pResOut = osAccess(zPath, W_OK|R_OK)==0; + } + return SQLITE_OK; +} + +/* +** +*/ +static int mkFullPathname( + const char *zPath, /* Input path */ + char *zOut, /* Output buffer */ + int nOut /* Allocated size of buffer zOut */ +){ + int nPath = sqlite3Strlen30(zPath); + int iOff = 0; + if( zPath[0]!='/' ){ + if( osGetcwd(zOut, nOut-2)==0 ){ + return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); + } + iOff = sqlite3Strlen30(zOut); + zOut[iOff++] = '/'; + } + if( (iOff+nPath+1)>nOut ){ + /* SQLite assumes that xFullPathname() nul-terminates the output buffer + ** even if it returns an error. */ + zOut[iOff] = '\0'; + return SQLITE_CANTOPEN_BKPT; + } + sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); + return SQLITE_OK; +} + +/* +** Turn a relative pathname into a full pathname. The relative path +** is stored as a nul-terminated string in the buffer pointed to by +** zPath. +** +** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes +** (in this case, MAX_PATHNAME bytes). The full-path is written to +** this buffer before returning. +*/ +static int unixFullPathname( + sqlite3_vfs *pVfs, /* Pointer to vfs object */ + const char *zPath, /* Possibly relative input path */ + int nOut, /* Size of output buffer in bytes */ + char *zOut /* Output buffer */ +){ +#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) + return mkFullPathname(zPath, zOut, nOut); +#else + int rc = SQLITE_OK; + int nByte; + int nLink = 1; /* Number of symbolic links followed so far */ + const char *zIn = zPath; /* Input path for each iteration of loop */ + char *zDel = 0; + + assert( pVfs->mxPathname==MAX_PATHNAME ); + UNUSED_PARAMETER(pVfs); + + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this + ** function failing. This function could fail if, for example, the + ** current working directory has been unlinked. + */ + SimulateIOError( return SQLITE_ERROR ); + + do { + + /* Call stat() on path zIn. Set bLink to true if the path is a symbolic + ** link, or false otherwise. */ + int bLink = 0; + struct stat buf; + if( osLstat(zIn, &buf)!=0 ){ + if( errno!=ENOENT ){ + rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); + } + }else{ + bLink = S_ISLNK(buf.st_mode); + } + + if( bLink ){ + if( zDel==0 ){ + zDel = sqlite3_malloc(nOut); + if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; + }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ + rc = SQLITE_CANTOPEN_BKPT; + } + + if( rc==SQLITE_OK ){ + nByte = osReadlink(zIn, zDel, nOut-1); + if( nByte<0 ){ + rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); + }else{ + if( zDel[0]!='/' ){ + int n; + for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); + if( nByte+n+1>nOut ){ + rc = SQLITE_CANTOPEN_BKPT; + }else{ + memmove(&zDel[n], zDel, nByte+1); + memcpy(zDel, zIn, n); + nByte += n; + } + } + zDel[nByte] = '\0'; + } + } + + zIn = zDel; + } + + assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); + if( rc==SQLITE_OK && zIn!=zOut ){ + rc = mkFullPathname(zIn, zOut, nOut); + } + if( bLink==0 ) break; + zIn = zOut; + }while( rc==SQLITE_OK ); + + sqlite3_free(zDel); + return rc; +#endif /* HAVE_READLINK && HAVE_LSTAT */ +} + + +#ifndef SQLITE_OMIT_LOAD_EXTENSION +/* +** Interfaces for opening a shared library, finding entry points +** within the shared library, and closing the shared library. +*/ +#include +static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ + UNUSED_PARAMETER(NotUsed); + return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); +} + +/* +** SQLite calls this function immediately after a call to unixDlSym() or +** unixDlOpen() fails (returns a null pointer). If a more detailed error +** message is available, it is written to zBufOut. If no error message +** is available, zBufOut is left unmodified and SQLite uses a default +** error message. +*/ +static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ + const char *zErr; + UNUSED_PARAMETER(NotUsed); + unixEnterMutex(); + zErr = dlerror(); + if( zErr ){ + sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); + } + unixLeaveMutex(); +} +static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ + /* + ** GCC with -pedantic-errors says that C90 does not allow a void* to be + ** cast into a pointer to a function. And yet the library dlsym() routine + ** returns a void* which is really a pointer to a function. So how do we + ** use dlsym() with -pedantic-errors? + ** + ** Variable x below is defined to be a pointer to a function taking + ** parameters void* and const char* and returning a pointer to a function. + ** We initialize x by assigning it a pointer to the dlsym() function. + ** (That assignment requires a cast.) Then we call the function that + ** x points to. + ** + ** This work-around is unlikely to work correctly on any system where + ** you really cannot cast a function pointer into void*. But then, on the + ** other hand, dlsym() will not work on such a system either, so we have + ** not really lost anything. + */ + void (*(*x)(void*,const char*))(void); + UNUSED_PARAMETER(NotUsed); + x = (void(*(*)(void*,const char*))(void))dlsym; + return (*x)(p, zSym); +} +static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ + UNUSED_PARAMETER(NotUsed); + dlclose(pHandle); +} +#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ + #define unixDlOpen 0 + #define unixDlError 0 + #define unixDlSym 0 + #define unixDlClose 0 +#endif + +/* +** Write nBuf bytes of random data to the supplied buffer zBuf. +*/ +static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ + UNUSED_PARAMETER(NotUsed); + assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); + + /* We have to initialize zBuf to prevent valgrind from reporting + ** errors. The reports issued by valgrind are incorrect - we would + ** prefer that the randomness be increased by making use of the + ** uninitialized space in zBuf - but valgrind errors tend to worry + ** some users. Rather than argue, it seems easier just to initialize + ** the whole array and silence valgrind, even if that means less randomness + ** in the random seed. + ** + ** When testing, initializing zBuf[] to zero is all we do. That means + ** that we always use the same random number sequence. This makes the + ** tests repeatable. + */ + memset(zBuf, 0, nBuf); + randomnessPid = osGetpid(0); +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) + { + int fd, got; + fd = robust_open("/dev/urandom", O_RDONLY, 0); + if( fd<0 ){ + time_t t; + time(&t); + memcpy(zBuf, &t, sizeof(t)); + memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); + assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); + nBuf = sizeof(t) + sizeof(randomnessPid); + }else{ + do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); + robust_close(0, fd, __LINE__); + } + } +#endif + return nBuf; +} + + +/* +** Sleep for a little while. Return the amount of time slept. +** The argument is the number of microseconds we want to sleep. +** The return value is the number of microseconds of sleep actually +** requested from the underlying operating system, a number which +** might be greater than or equal to the argument, but not less +** than the argument. +*/ +static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ +#if OS_VXWORKS + struct timespec sp; + + sp.tv_sec = microseconds / 1000000; + sp.tv_nsec = (microseconds % 1000000) * 1000; + nanosleep(&sp, NULL); + UNUSED_PARAMETER(NotUsed); + return microseconds; +#elif defined(HAVE_USLEEP) && HAVE_USLEEP + usleep(microseconds); + UNUSED_PARAMETER(NotUsed); + return microseconds; +#else + int seconds = (microseconds+999999)/1000000; + sleep(seconds); + UNUSED_PARAMETER(NotUsed); + return seconds*1000000; +#endif +} + +/* +** The following variable, if set to a non-zero value, is interpreted as +** the number of seconds since 1970 and is used to set the result of +** sqlite3OsCurrentTime() during testing. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ +#endif + +/* +** Find the current time (in Universal Coordinated Time). Write into *piNow +** the current time and date as a Julian Day number times 86_400_000. In +** other words, write into *piNow the number of milliseconds since the Julian +** epoch of noon in Greenwich on November 24, 4714 B.C according to the +** proleptic Gregorian calendar. +** +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date +** cannot be found. +*/ +static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ + static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; + int rc = SQLITE_OK; +#if defined(NO_GETTOD) + time_t t; + time(&t); + *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; +#elif OS_VXWORKS + struct timespec sNow; + clock_gettime(CLOCK_REALTIME, &sNow); + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; +#else + struct timeval sNow; + (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; +#endif + +#ifdef SQLITE_TEST + if( sqlite3_current_time ){ + *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; + } +#endif + UNUSED_PARAMETER(NotUsed); + return rc; +} + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** Find the current time (in Universal Coordinated Time). Write the +** current time and date as a Julian Day number into *prNow and +** return 0. Return 1 if the time and date cannot be found. +*/ +static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ + sqlite3_int64 i = 0; + int rc; + UNUSED_PARAMETER(NotUsed); + rc = unixCurrentTimeInt64(0, &i); + *prNow = i/86400000.0; + return rc; +} +#else +# define unixCurrentTime 0 +#endif + +/* +** The xGetLastError() method is designed to return a better +** low-level error message when operating-system problems come up +** during SQLite operation. Only the integer return code is currently +** used. +*/ +static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ + UNUSED_PARAMETER(NotUsed); + UNUSED_PARAMETER(NotUsed2); + UNUSED_PARAMETER(NotUsed3); + return errno; +} + + +/* +************************ End of sqlite3_vfs methods *************************** +******************************************************************************/ + +/****************************************************************************** +************************** Begin Proxy Locking ******************************** +** +** Proxy locking is a "uber-locking-method" in this sense: It uses the +** other locking methods on secondary lock files. Proxy locking is a +** meta-layer over top of the primitive locking implemented above. For +** this reason, the division that implements of proxy locking is deferred +** until late in the file (here) after all of the other I/O methods have +** been defined - so that the primitive locking methods are available +** as services to help with the implementation of proxy locking. +** +**** +** +** The default locking schemes in SQLite use byte-range locks on the +** database file to coordinate safe, concurrent access by multiple readers +** and writers [http://sqlite.org/lockingv3.html]. The five file locking +** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented +** as POSIX read & write locks over fixed set of locations (via fsctl), +** on AFP and SMB only exclusive byte-range locks are available via fsctl +** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. +** To simulate a F_RDLCK on the shared range, on AFP a randomly selected +** address in the shared range is taken for a SHARED lock, the entire +** shared range is taken for an EXCLUSIVE lock): +** +** PENDING_BYTE 0x40000000 +** RESERVED_BYTE 0x40000001 +** SHARED_RANGE 0x40000002 -> 0x40000200 +** +** This works well on the local file system, but shows a nearly 100x +** slowdown in read performance on AFP because the AFP client disables +** the read cache when byte-range locks are present. Enabling the read +** cache exposes a cache coherency problem that is present on all OS X +** supported network file systems. NFS and AFP both observe the +** close-to-open semantics for ensuring cache coherency +** [http://nfs.sourceforge.net/#faq_a8], which does not effectively +** address the requirements for concurrent database access by multiple +** readers and writers +** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. +** +** To address the performance and cache coherency issues, proxy file locking +** changes the way database access is controlled by limiting access to a +** single host at a time and moving file locks off of the database file +** and onto a proxy file on the local file system. +** +** +** Using proxy locks +** ----------------- +** +** C APIs +** +** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, +** | ":auto:"); +** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, +** &); +** +** +** SQL pragmas +** +** PRAGMA [database.]lock_proxy_file= | :auto: +** PRAGMA [database.]lock_proxy_file +** +** Specifying ":auto:" means that if there is a conch file with a matching +** host ID in it, the proxy path in the conch file will be used, otherwise +** a proxy path based on the user's temp dir +** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the +** actual proxy file name is generated from the name and path of the +** database file. For example: +** +** For database path "/Users/me/foo.db" +** The lock path will be "/sqliteplocks/_Users_me_foo.db:auto:") +** +** Once a lock proxy is configured for a database connection, it can not +** be removed, however it may be switched to a different proxy path via +** the above APIs (assuming the conch file is not being held by another +** connection or process). +** +** +** How proxy locking works +** ----------------------- +** +** Proxy file locking relies primarily on two new supporting files: +** +** * conch file to limit access to the database file to a single host +** at a time +** +** * proxy file to act as a proxy for the advisory locks normally +** taken on the database +** +** The conch file - to use a proxy file, sqlite must first "hold the conch" +** by taking an sqlite-style shared lock on the conch file, reading the +** contents and comparing the host's unique host ID (see below) and lock +** proxy path against the values stored in the conch. The conch file is +** stored in the same directory as the database file and the file name +** is patterned after the database file name as ".-conch". +** If the conch file does not exist, or its contents do not match the +** host ID and/or proxy path, then the lock is escalated to an exclusive +** lock and the conch file contents is updated with the host ID and proxy +** path and the lock is downgraded to a shared lock again. If the conch +** is held by another process (with a shared lock), the exclusive lock +** will fail and SQLITE_BUSY is returned. +** +** The proxy file - a single-byte file used for all advisory file locks +** normally taken on the database file. This allows for safe sharing +** of the database file for multiple readers and writers on the same +** host (the conch ensures that they all use the same local lock file). +** +** Requesting the lock proxy does not immediately take the conch, it is +** only taken when the first request to lock database file is made. +** This matches the semantics of the traditional locking behavior, where +** opening a connection to a database file does not take a lock on it. +** The shared lock and an open file descriptor are maintained until +** the connection to the database is closed. +** +** The proxy file and the lock file are never deleted so they only need +** to be created the first time they are used. +** +** Configuration options +** --------------------- +** +** SQLITE_PREFER_PROXY_LOCKING +** +** Database files accessed on non-local file systems are +** automatically configured for proxy locking, lock files are +** named automatically using the same logic as +** PRAGMA lock_proxy_file=":auto:" +** +** SQLITE_PROXY_DEBUG +** +** Enables the logging of error messages during host id file +** retrieval and creation +** +** LOCKPROXYDIR +** +** Overrides the default directory used for lock proxy files that +** are named automatically via the ":auto:" setting +** +** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS +** +** Permissions to use when creating a directory for storing the +** lock proxy files, only used when LOCKPROXYDIR is not set. +** +** +** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, +** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will +** force proxy locking to be used for every database file opened, and 0 +** will force automatic proxy locking to be disabled for all database +** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or +** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). +*/ + +/* +** Proxy locking is only available on MacOSX +*/ +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE + +/* +** The proxyLockingContext has the path and file structures for the remote +** and local proxy files in it +*/ +typedef struct proxyLockingContext proxyLockingContext; +struct proxyLockingContext { + unixFile *conchFile; /* Open conch file */ + char *conchFilePath; /* Name of the conch file */ + unixFile *lockProxy; /* Open proxy lock file */ + char *lockProxyPath; /* Name of the proxy lock file */ + char *dbPath; /* Name of the open file */ + int conchHeld; /* 1 if the conch is held, -1 if lockless */ + int nFails; /* Number of conch taking failures */ + void *oldLockingContext; /* Original lockingcontext to restore on close */ + sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ +}; + +/* +** The proxy lock file path for the database at dbPath is written into lPath, +** which must point to valid, writable memory large enough for a maxLen length +** file path. +*/ +static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ + int len; + int dbLen; + int i; + +#ifdef LOCKPROXYDIR + len = strlcpy(lPath, LOCKPROXYDIR, maxLen); +#else +# ifdef _CS_DARWIN_USER_TEMP_DIR + { + if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ + OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", + lPath, errno, osGetpid(0))); + return SQLITE_IOERR_LOCK; + } + len = strlcat(lPath, "sqliteplocks", maxLen); + } +# else + len = strlcpy(lPath, "/tmp/", maxLen); +# endif +#endif + + if( lPath[len-1]!='/' ){ + len = strlcat(lPath, "/", maxLen); + } + + /* transform the db path to a unique cache name */ + dbLen = (int)strlen(dbPath); + for( i=0; i 0) ){ + /* only mkdir if leaf dir != "." or "/" or ".." */ + if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') + || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ + buf[i]='\0'; + if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ + int err=errno; + if( err!=EEXIST ) { + OSTRACE(("CREATELOCKPATH FAILED creating %s, " + "'%s' proxy lock path=%s pid=%d\n", + buf, strerror(err), lockPath, osGetpid(0))); + return err; + } + } + } + start=i+1; + } + buf[i] = lockPath[i]; + } + OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); + return 0; +} + +/* +** Create a new VFS file descriptor (stored in memory obtained from +** sqlite3_malloc) and open the file named "path" in the file descriptor. +** +** The caller is responsible not only for closing the file descriptor +** but also for freeing the memory associated with the file descriptor. +*/ +static int proxyCreateUnixFile( + const char *path, /* path for the new unixFile */ + unixFile **ppFile, /* unixFile created and returned by ref */ + int islockfile /* if non zero missing dirs will be created */ +) { + int fd = -1; + unixFile *pNew; + int rc = SQLITE_OK; + int openFlags = O_RDWR | O_CREAT; + sqlite3_vfs dummyVfs; + int terrno = 0; + UnixUnusedFd *pUnused = NULL; + + /* 1. first try to open/create the file + ** 2. if that fails, and this is a lock file (not-conch), try creating + ** the parent directories and then try again. + ** 3. if that fails, try to open the file read-only + ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file + */ + pUnused = findReusableFd(path, openFlags); + if( pUnused ){ + fd = pUnused->fd; + }else{ + pUnused = sqlite3_malloc64(sizeof(*pUnused)); + if( !pUnused ){ + return SQLITE_NOMEM_BKPT; + } + } + if( fd<0 ){ + fd = robust_open(path, openFlags, 0); + terrno = errno; + if( fd<0 && errno==ENOENT && islockfile ){ + if( proxyCreateLockPath(path) == SQLITE_OK ){ + fd = robust_open(path, openFlags, 0); + } + } + } + if( fd<0 ){ + openFlags = O_RDONLY; + fd = robust_open(path, openFlags, 0); + terrno = errno; + } + if( fd<0 ){ + if( islockfile ){ + return SQLITE_BUSY; + } + switch (terrno) { + case EACCES: + return SQLITE_PERM; + case EIO: + return SQLITE_IOERR_LOCK; /* even though it is the conch */ + default: + return SQLITE_CANTOPEN_BKPT; + } + } + + pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); + if( pNew==NULL ){ + rc = SQLITE_NOMEM_BKPT; + goto end_create_proxy; + } + memset(pNew, 0, sizeof(unixFile)); + pNew->openFlags = openFlags; + memset(&dummyVfs, 0, sizeof(dummyVfs)); + dummyVfs.pAppData = (void*)&autolockIoFinder; + dummyVfs.zName = "dummy"; + pUnused->fd = fd; + pUnused->flags = openFlags; + pNew->pUnused = pUnused; + + rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); + if( rc==SQLITE_OK ){ + *ppFile = pNew; + return SQLITE_OK; + } +end_create_proxy: + robust_close(pNew, fd, __LINE__); + sqlite3_free(pNew); + sqlite3_free(pUnused); + return rc; +} + +#ifdef SQLITE_TEST +/* simulate multiple hosts by creating unique hostid file paths */ +SQLITE_API int sqlite3_hostid_num = 0; +#endif + +#define PROXY_HOSTIDLEN 16 /* conch file host id length */ + +#ifdef HAVE_GETHOSTUUID +/* Not always defined in the headers as it ought to be */ +extern int gethostuuid(uuid_t id, const struct timespec *wait); +#endif + +/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN +** bytes of writable memory. +*/ +static int proxyGetHostID(unsigned char *pHostID, int *pError){ + assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); + memset(pHostID, 0, PROXY_HOSTIDLEN); +#ifdef HAVE_GETHOSTUUID + { + struct timespec timeout = {1, 0}; /* 1 sec timeout */ + if( gethostuuid(pHostID, &timeout) ){ + int err = errno; + if( pError ){ + *pError = err; + } + return SQLITE_IOERR; + } + } +#else + UNUSED_PARAMETER(pError); +#endif +#ifdef SQLITE_TEST + /* simulate multiple hosts by creating unique hostid file paths */ + if( sqlite3_hostid_num != 0){ + pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); + } +#endif + + return SQLITE_OK; +} + +/* The conch file contains the header, host id and lock file path + */ +#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ +#define PROXY_HEADERLEN 1 /* conch file header length */ +#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) +#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) + +/* +** Takes an open conch file, copies the contents to a new path and then moves +** it back. The newly created file's file descriptor is assigned to the +** conch file structure and finally the original conch file descriptor is +** closed. Returns zero if successful. +*/ +static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + unixFile *conchFile = pCtx->conchFile; + char tPath[MAXPATHLEN]; + char buf[PROXY_MAXCONCHLEN]; + char *cPath = pCtx->conchFilePath; + size_t readLen = 0; + size_t pathLen = 0; + char errmsg[64] = ""; + int fd = -1; + int rc = -1; + UNUSED_PARAMETER(myHostID); + + /* create a new path by replace the trailing '-conch' with '-break' */ + pathLen = strlcpy(tPath, cPath, MAXPATHLEN); + if( pathLen>MAXPATHLEN || pathLen<6 || + (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ + sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); + goto end_breaklock; + } + /* read the conch content */ + readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); + if( readLenh, __LINE__); + conchFile->h = fd; + conchFile->openFlags = O_RDWR | O_CREAT; + +end_breaklock: + if( rc ){ + if( fd>=0 ){ + osUnlink(tPath); + robust_close(pFile, fd, __LINE__); + } + fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); + } + return rc; +} + +/* Take the requested lock on the conch file and break a stale lock if the +** host id matches. +*/ +static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + unixFile *conchFile = pCtx->conchFile; + int rc = SQLITE_OK; + int nTries = 0; + struct timespec conchModTime; + + memset(&conchModTime, 0, sizeof(conchModTime)); + do { + rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); + nTries ++; + if( rc==SQLITE_BUSY ){ + /* If the lock failed (busy): + * 1st try: get the mod time of the conch, wait 0.5s and try again. + * 2nd try: fail if the mod time changed or host id is different, wait + * 10 sec and try again + * 3rd try: break the lock unless the mod time has changed. + */ + struct stat buf; + if( osFstat(conchFile->h, &buf) ){ + storeLastErrno(pFile, errno); + return SQLITE_IOERR_LOCK; + } + + if( nTries==1 ){ + conchModTime = buf.st_mtimespec; + usleep(500000); /* wait 0.5 sec and try the lock again*/ + continue; + } + + assert( nTries>1 ); + if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || + conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ + return SQLITE_BUSY; + } + + if( nTries==2 ){ + char tBuf[PROXY_MAXCONCHLEN]; + int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); + if( len<0 ){ + storeLastErrno(pFile, errno); + return SQLITE_IOERR_LOCK; + } + if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ + /* don't break the lock if the host id doesn't match */ + if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ + return SQLITE_BUSY; + } + }else{ + /* don't break the lock on short read or a version mismatch */ + return SQLITE_BUSY; + } + usleep(10000000); /* wait 10 sec and try the lock again */ + continue; + } + + assert( nTries==3 ); + if( 0==proxyBreakConchLock(pFile, myHostID) ){ + rc = SQLITE_OK; + if( lockType==EXCLUSIVE_LOCK ){ + rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); + } + if( !rc ){ + rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); + } + } + } + } while( rc==SQLITE_BUSY && nTries<3 ); + + return rc; +} + +/* Takes the conch by taking a shared lock and read the contents conch, if +** lockPath is non-NULL, the host ID and lock file path must match. A NULL +** lockPath means that the lockPath in the conch file will be used if the +** host IDs match, or a new lock path will be generated automatically +** and written to the conch file. +*/ +static int proxyTakeConch(unixFile *pFile){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + + if( pCtx->conchHeld!=0 ){ + return SQLITE_OK; + }else{ + unixFile *conchFile = pCtx->conchFile; + uuid_t myHostID; + int pError = 0; + char readBuf[PROXY_MAXCONCHLEN]; + char lockPath[MAXPATHLEN]; + char *tempLockPath = NULL; + int rc = SQLITE_OK; + int createConch = 0; + int hostIdMatch = 0; + int readLen = 0; + int tryOldLockPath = 0; + int forceNewLockPath = 0; + + OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, + (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), + osGetpid(0))); + + rc = proxyGetHostID(myHostID, &pError); + if( (rc&0xff)==SQLITE_IOERR ){ + storeLastErrno(pFile, pError); + goto end_takeconch; + } + rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); + if( rc!=SQLITE_OK ){ + goto end_takeconch; + } + /* read the existing conch file */ + readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); + if( readLen<0 ){ + /* I/O error: lastErrno set by seekAndRead */ + storeLastErrno(pFile, conchFile->lastErrno); + rc = SQLITE_IOERR_READ; + goto end_takeconch; + }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || + readBuf[0]!=(char)PROXY_CONCHVERSION ){ + /* a short read or version format mismatch means we need to create a new + ** conch file. + */ + createConch = 1; + } + /* if the host id matches and the lock path already exists in the conch + ** we'll try to use the path there, if we can't open that path, we'll + ** retry with a new auto-generated path + */ + do { /* in case we need to try again for an :auto: named lock file */ + + if( !createConch && !forceNewLockPath ){ + hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, + PROXY_HOSTIDLEN); + /* if the conch has data compare the contents */ + if( !pCtx->lockProxyPath ){ + /* for auto-named local lock file, just check the host ID and we'll + ** use the local lock file path that's already in there + */ + if( hostIdMatch ){ + size_t pathLen = (readLen - PROXY_PATHINDEX); + + if( pathLen>=MAXPATHLEN ){ + pathLen=MAXPATHLEN-1; + } + memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); + lockPath[pathLen] = 0; + tempLockPath = lockPath; + tryOldLockPath = 1; + /* create a copy of the lock path if the conch is taken */ + goto end_takeconch; + } + }else if( hostIdMatch + && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], + readLen-PROXY_PATHINDEX) + ){ + /* conch host and lock path match */ + goto end_takeconch; + } + } + + /* if the conch isn't writable and doesn't match, we can't take it */ + if( (conchFile->openFlags&O_RDWR) == 0 ){ + rc = SQLITE_BUSY; + goto end_takeconch; + } + + /* either the conch didn't match or we need to create a new one */ + if( !pCtx->lockProxyPath ){ + proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); + tempLockPath = lockPath; + /* create a copy of the lock path _only_ if the conch is taken */ + } + + /* update conch with host and path (this will fail if other process + ** has a shared lock already), if the host id matches, use the big + ** stick. + */ + futimes(conchFile->h, NULL); + if( hostIdMatch && !createConch ){ + if( conchFile->pInode && conchFile->pInode->nShared>1 ){ + /* We are trying for an exclusive lock but another thread in this + ** same process is still holding a shared lock. */ + rc = SQLITE_BUSY; + } else { + rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); + } + }else{ + rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); + } + if( rc==SQLITE_OK ){ + char writeBuffer[PROXY_MAXCONCHLEN]; + int writeSize = 0; + + writeBuffer[0] = (char)PROXY_CONCHVERSION; + memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); + if( pCtx->lockProxyPath!=NULL ){ + strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, + MAXPATHLEN); + }else{ + strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); + } + writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); + robust_ftruncate(conchFile->h, writeSize); + rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); + full_fsync(conchFile->h,0,0); + /* If we created a new conch file (not just updated the contents of a + ** valid conch file), try to match the permissions of the database + */ + if( rc==SQLITE_OK && createConch ){ + struct stat buf; + int err = osFstat(pFile->h, &buf); + if( err==0 ){ + mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | + S_IROTH|S_IWOTH); + /* try to match the database file R/W permissions, ignore failure */ +#ifndef SQLITE_PROXY_DEBUG + osFchmod(conchFile->h, cmode); +#else + do{ + rc = osFchmod(conchFile->h, cmode); + }while( rc==(-1) && errno==EINTR ); + if( rc!=0 ){ + int code = errno; + fprintf(stderr, "fchmod %o FAILED with %d %s\n", + cmode, code, strerror(code)); + } else { + fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); + } + }else{ + int code = errno; + fprintf(stderr, "STAT FAILED[%d] with %d %s\n", + err, code, strerror(code)); +#endif + } + } + } + conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); + + end_takeconch: + OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); + if( rc==SQLITE_OK && pFile->openFlags ){ + int fd; + if( pFile->h>=0 ){ + robust_close(pFile, pFile->h, __LINE__); + } + pFile->h = -1; + fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); + OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); + if( fd>=0 ){ + pFile->h = fd; + }else{ + rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called + during locking */ + } + } + if( rc==SQLITE_OK && !pCtx->lockProxy ){ + char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; + rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); + if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ + /* we couldn't create the proxy lock file with the old lock file path + ** so try again via auto-naming + */ + forceNewLockPath = 1; + tryOldLockPath = 0; + continue; /* go back to the do {} while start point, try again */ + } + } + if( rc==SQLITE_OK ){ + /* Need to make a copy of path if we extracted the value + ** from the conch file or the path was allocated on the stack + */ + if( tempLockPath ){ + pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); + if( !pCtx->lockProxyPath ){ + rc = SQLITE_NOMEM_BKPT; + } + } + } + if( rc==SQLITE_OK ){ + pCtx->conchHeld = 1; + + if( pCtx->lockProxy->pMethod == &afpIoMethods ){ + afpLockingContext *afpCtx; + afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; + afpCtx->dbPath = pCtx->lockProxyPath; + } + } else { + conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); + } + OSTRACE(("TAKECONCH %d %s\n", conchFile->h, + rc==SQLITE_OK?"ok":"failed")); + return rc; + } while (1); /* in case we need to retry the :auto: lock file - + ** we should never get here except via the 'continue' call. */ + } +} + +/* +** If pFile holds a lock on a conch file, then release that lock. +*/ +static int proxyReleaseConch(unixFile *pFile){ + int rc = SQLITE_OK; /* Subroutine return code */ + proxyLockingContext *pCtx; /* The locking context for the proxy lock */ + unixFile *conchFile; /* Name of the conch file */ + + pCtx = (proxyLockingContext *)pFile->lockingContext; + conchFile = pCtx->conchFile; + OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, + (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), + osGetpid(0))); + if( pCtx->conchHeld>0 ){ + rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); + } + pCtx->conchHeld = 0; + OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, + (rc==SQLITE_OK ? "ok" : "failed"))); + return rc; +} + +/* +** Given the name of a database file, compute the name of its conch file. +** Store the conch filename in memory obtained from sqlite3_malloc64(). +** Make *pConchPath point to the new name. Return SQLITE_OK on success +** or SQLITE_NOMEM if unable to obtain memory. +** +** The caller is responsible for ensuring that the allocated memory +** space is eventually freed. +** +** *pConchPath is set to NULL if a memory allocation error occurs. +*/ +static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ + int i; /* Loop counter */ + int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ + char *conchPath; /* buffer in which to construct conch name */ + + /* Allocate space for the conch filename and initialize the name to + ** the name of the original database file. */ + *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); + if( conchPath==0 ){ + return SQLITE_NOMEM_BKPT; + } + memcpy(conchPath, dbPath, len+1); + + /* now insert a "." before the last / character */ + for( i=(len-1); i>=0; i-- ){ + if( conchPath[i]=='/' ){ + i++; + break; + } + } + conchPath[i]='.'; + while ( ilockingContext; + char *oldPath = pCtx->lockProxyPath; + int rc = SQLITE_OK; + + if( pFile->eFileLock!=NO_LOCK ){ + return SQLITE_BUSY; + } + + /* nothing to do if the path is NULL, :auto: or matches the existing path */ + if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || + (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ + return SQLITE_OK; + }else{ + unixFile *lockProxy = pCtx->lockProxy; + pCtx->lockProxy=NULL; + pCtx->conchHeld = 0; + if( lockProxy!=NULL ){ + rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); + if( rc ) return rc; + sqlite3_free(lockProxy); + } + sqlite3_free(oldPath); + pCtx->lockProxyPath = sqlite3DbStrDup(0, path); + } + + return rc; +} + +/* +** pFile is a file that has been opened by a prior xOpen call. dbPath +** is a string buffer at least MAXPATHLEN+1 characters in size. +** +** This routine find the filename associated with pFile and writes it +** int dbPath. +*/ +static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ +#if defined(__APPLE__) + if( pFile->pMethod == &afpIoMethods ){ + /* afp style keeps a reference to the db path in the filePath field + ** of the struct */ + assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); + strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, + MAXPATHLEN); + } else +#endif + if( pFile->pMethod == &dotlockIoMethods ){ + /* dot lock style uses the locking context to store the dot lock + ** file path */ + int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); + memcpy(dbPath, (char *)pFile->lockingContext, len + 1); + }else{ + /* all other styles use the locking context to store the db file path */ + assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); + strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); + } + return SQLITE_OK; +} + +/* +** Takes an already filled in unix file and alters it so all file locking +** will be performed on the local proxy lock file. The following fields +** are preserved in the locking context so that they can be restored and +** the unix structure properly cleaned up at close time: +** ->lockingContext +** ->pMethod +*/ +static int proxyTransformUnixFile(unixFile *pFile, const char *path) { + proxyLockingContext *pCtx; + char dbPath[MAXPATHLEN+1]; /* Name of the database file */ + char *lockPath=NULL; + int rc = SQLITE_OK; + + if( pFile->eFileLock!=NO_LOCK ){ + return SQLITE_BUSY; + } + proxyGetDbPathForUnixFile(pFile, dbPath); + if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ + lockPath=NULL; + }else{ + lockPath=(char *)path; + } + + OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, + (lockPath ? lockPath : ":auto:"), osGetpid(0))); + + pCtx = sqlite3_malloc64( sizeof(*pCtx) ); + if( pCtx==0 ){ + return SQLITE_NOMEM_BKPT; + } + memset(pCtx, 0, sizeof(*pCtx)); + + rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); + if( rc==SQLITE_OK ){ + rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); + if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ + /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and + ** (c) the file system is read-only, then enable no-locking access. + ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts + ** that openFlags will have only one of O_RDONLY or O_RDWR. + */ + struct statfs fsInfo; + struct stat conchInfo; + int goLockless = 0; + + if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { + int err = errno; + if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ + goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; + } + } + if( goLockless ){ + pCtx->conchHeld = -1; /* read only FS/ lockless */ + rc = SQLITE_OK; + } + } + } + if( rc==SQLITE_OK && lockPath ){ + pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); + } + + if( rc==SQLITE_OK ){ + pCtx->dbPath = sqlite3DbStrDup(0, dbPath); + if( pCtx->dbPath==NULL ){ + rc = SQLITE_NOMEM_BKPT; + } + } + if( rc==SQLITE_OK ){ + /* all memory is allocated, proxys are created and assigned, + ** switch the locking context and pMethod then return. + */ + pCtx->oldLockingContext = pFile->lockingContext; + pFile->lockingContext = pCtx; + pCtx->pOldMethod = pFile->pMethod; + pFile->pMethod = &proxyIoMethods; + }else{ + if( pCtx->conchFile ){ + pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); + sqlite3_free(pCtx->conchFile); + } + sqlite3DbFree(0, pCtx->lockProxyPath); + sqlite3_free(pCtx->conchFilePath); + sqlite3_free(pCtx); + } + OSTRACE(("TRANSPROXY %d %s\n", pFile->h, + (rc==SQLITE_OK ? "ok" : "failed"))); + return rc; +} + + +/* +** This routine handles sqlite3_file_control() calls that are specific +** to proxy locking. +*/ +static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ + switch( op ){ + case SQLITE_FCNTL_GET_LOCKPROXYFILE: { + unixFile *pFile = (unixFile*)id; + if( pFile->pMethod == &proxyIoMethods ){ + proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; + proxyTakeConch(pFile); + if( pCtx->lockProxyPath ){ + *(const char **)pArg = pCtx->lockProxyPath; + }else{ + *(const char **)pArg = ":auto: (not held)"; + } + } else { + *(const char **)pArg = NULL; + } + return SQLITE_OK; + } + case SQLITE_FCNTL_SET_LOCKPROXYFILE: { + unixFile *pFile = (unixFile*)id; + int rc = SQLITE_OK; + int isProxyStyle = (pFile->pMethod == &proxyIoMethods); + if( pArg==NULL || (const char *)pArg==0 ){ + if( isProxyStyle ){ + /* turn off proxy locking - not supported. If support is added for + ** switching proxy locking mode off then it will need to fail if + ** the journal mode is WAL mode. + */ + rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; + }else{ + /* turn off proxy locking - already off - NOOP */ + rc = SQLITE_OK; + } + }else{ + const char *proxyPath = (const char *)pArg; + if( isProxyStyle ){ + proxyLockingContext *pCtx = + (proxyLockingContext*)pFile->lockingContext; + if( !strcmp(pArg, ":auto:") + || (pCtx->lockProxyPath && + !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) + ){ + rc = SQLITE_OK; + }else{ + rc = switchLockProxyPath(pFile, proxyPath); + } + }else{ + /* turn on proxy file locking */ + rc = proxyTransformUnixFile(pFile, proxyPath); + } + } + return rc; + } + default: { + assert( 0 ); /* The call assures that only valid opcodes are sent */ + } + } + /*NOTREACHED*/ + return SQLITE_ERROR; +} + +/* +** Within this division (the proxying locking implementation) the procedures +** above this point are all utilities. The lock-related methods of the +** proxy-locking sqlite3_io_method object follow. +*/ + + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, set *pResOut +** to a non-zero value otherwise *pResOut is set to zero. The return value +** is set to SQLITE_OK unless an I/O error occurs during lock checking. +*/ +static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { + unixFile *pFile = (unixFile*)id; + int rc = proxyTakeConch(pFile); + if( rc==SQLITE_OK ){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + if( pCtx->conchHeld>0 ){ + unixFile *proxy = pCtx->lockProxy; + return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); + }else{ /* conchHeld < 0 is lockless */ + pResOut=0; + } + } + return rc; +} + +/* +** Lock the file with the lock specified by parameter eFileLock - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** This routine will only increase a lock. Use the sqlite3OsUnlock() +** routine to lower a locking level. +*/ +static int proxyLock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + int rc = proxyTakeConch(pFile); + if( rc==SQLITE_OK ){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + if( pCtx->conchHeld>0 ){ + unixFile *proxy = pCtx->lockProxy; + rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); + pFile->eFileLock = proxy->eFileLock; + }else{ + /* conchHeld < 0 is lockless */ + } + } + return rc; +} + + +/* +** Lower the locking level on file descriptor pFile to eFileLock. eFileLock +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ +static int proxyUnlock(sqlite3_file *id, int eFileLock) { + unixFile *pFile = (unixFile*)id; + int rc = proxyTakeConch(pFile); + if( rc==SQLITE_OK ){ + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + if( pCtx->conchHeld>0 ){ + unixFile *proxy = pCtx->lockProxy; + rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); + pFile->eFileLock = proxy->eFileLock; + }else{ + /* conchHeld < 0 is lockless */ + } + } + return rc; +} + +/* +** Close a file that uses proxy locks. +*/ +static int proxyClose(sqlite3_file *id) { + if( ALWAYS(id) ){ + unixFile *pFile = (unixFile*)id; + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + unixFile *lockProxy = pCtx->lockProxy; + unixFile *conchFile = pCtx->conchFile; + int rc = SQLITE_OK; + + if( lockProxy ){ + rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); + if( rc ) return rc; + rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); + if( rc ) return rc; + sqlite3_free(lockProxy); + pCtx->lockProxy = 0; + } + if( conchFile ){ + if( pCtx->conchHeld ){ + rc = proxyReleaseConch(pFile); + if( rc ) return rc; + } + rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); + if( rc ) return rc; + sqlite3_free(conchFile); + } + sqlite3DbFree(0, pCtx->lockProxyPath); + sqlite3_free(pCtx->conchFilePath); + sqlite3DbFree(0, pCtx->dbPath); + /* restore the original locking context and pMethod then close it */ + pFile->lockingContext = pCtx->oldLockingContext; + pFile->pMethod = pCtx->pOldMethod; + sqlite3_free(pCtx); + return pFile->pMethod->xClose(id); + } + return SQLITE_OK; +} + + + +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ +/* +** The proxy locking style is intended for use with AFP filesystems. +** And since AFP is only supported on MacOSX, the proxy locking is also +** restricted to MacOSX. +** +** +******************* End of the proxy lock implementation ********************** +******************************************************************************/ + +/* +** Initialize the operating system interface. +** +** This routine registers all VFS implementations for unix-like operating +** systems. This routine, and the sqlite3_os_end() routine that follows, +** should be the only routines in this file that are visible from other +** files. +** +** This routine is called once during SQLite initialization and by a +** single thread. The memory allocation and mutex subsystems have not +** necessarily been initialized when this routine is called, and so they +** should not be used. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ + /* + ** The following macro defines an initializer for an sqlite3_vfs object. + ** The name of the VFS is NAME. The pAppData is a pointer to a pointer + ** to the "finder" function. (pAppData is a pointer to a pointer because + ** silly C90 rules prohibit a void* from being cast to a function pointer + ** and so we have to go through the intermediate pointer to avoid problems + ** when compiling with -pedantic-errors on GCC.) + ** + ** The FINDER parameter to this macro is the name of the pointer to the + ** finder-function. The finder-function returns a pointer to the + ** sqlite_io_methods object that implements the desired locking + ** behaviors. See the division above that contains the IOMETHODS + ** macro for addition information on finder-functions. + ** + ** Most finders simply return a pointer to a fixed sqlite3_io_methods + ** object. But the "autolockIoFinder" available on MacOSX does a little + ** more than that; it looks at the filesystem type that hosts the + ** database file and tries to choose an locking method appropriate for + ** that filesystem time. + */ + #define UNIXVFS(VFSNAME, FINDER) { \ + 3, /* iVersion */ \ + sizeof(unixFile), /* szOsFile */ \ + MAX_PATHNAME, /* mxPathname */ \ + 0, /* pNext */ \ + VFSNAME, /* zName */ \ + (void*)&FINDER, /* pAppData */ \ + unixOpen, /* xOpen */ \ + unixDelete, /* xDelete */ \ + unixAccess, /* xAccess */ \ + unixFullPathname, /* xFullPathname */ \ + unixDlOpen, /* xDlOpen */ \ + unixDlError, /* xDlError */ \ + unixDlSym, /* xDlSym */ \ + unixDlClose, /* xDlClose */ \ + unixRandomness, /* xRandomness */ \ + unixSleep, /* xSleep */ \ + unixCurrentTime, /* xCurrentTime */ \ + unixGetLastError, /* xGetLastError */ \ + unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ + unixSetSystemCall, /* xSetSystemCall */ \ + unixGetSystemCall, /* xGetSystemCall */ \ + unixNextSystemCall, /* xNextSystemCall */ \ + } + + /* + ** All default VFSes for unix are contained in the following array. + ** + ** Note that the sqlite3_vfs.pNext field of the VFS object is modified + ** by the SQLite core when the VFS is registered. So the following + ** array cannot be const. + */ + static sqlite3_vfs aVfs[] = { +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) + UNIXVFS("unix", autolockIoFinder ), +#elif OS_VXWORKS + UNIXVFS("unix", vxworksIoFinder ), +#else + UNIXVFS("unix", posixIoFinder ), +#endif + UNIXVFS("unix-none", nolockIoFinder ), + UNIXVFS("unix-dotfile", dotlockIoFinder ), + UNIXVFS("unix-excl", posixIoFinder ), +#if OS_VXWORKS + UNIXVFS("unix-namedsem", semIoFinder ), +#endif +#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS + UNIXVFS("unix-posix", posixIoFinder ), +#endif +#if SQLITE_ENABLE_LOCKING_STYLE + UNIXVFS("unix-flock", flockIoFinder ), +#endif +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) + UNIXVFS("unix-afp", afpIoFinder ), + UNIXVFS("unix-nfs", nfsIoFinder ), + UNIXVFS("unix-proxy", proxyIoFinder ), +#endif + }; + unsigned int i; /* Loop counter */ + + /* Double-check that the aSyscall[] array has been constructed + ** correctly. See ticket [bb3a86e890c8e96ab] */ + assert( ArraySize(aSyscall)==28 ); + + /* Register all VFSes defined in the aVfs[] array */ + for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ + sqlite3_vfs_register(&aVfs[i], i==0); + } + return SQLITE_OK; +} + +/* +** Shutdown the operating system interface. +** +** Some operating systems might need to do some cleanup in this routine, +** to release dynamically allocated objects. But not on unix. +** This routine is a no-op for unix. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ + return SQLITE_OK; +} + +#endif /* SQLITE_OS_UNIX */ + +/************** End of os_unix.c *********************************************/ +/************** Begin file os_win.c ******************************************/ +/* +** 2004 May 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code that is specific to Windows. +*/ +/* #include "sqliteInt.h" */ +#if SQLITE_OS_WIN /* This file is used for Windows only */ + +/* +** Include code that is common to all os_*.c files +*/ +/************** Include os_common.h in the middle of os_win.c ****************/ +/************** Begin file os_common.h ***************************************/ +/* +** 2004 May 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains macros and a little bit of code that is common to +** all of the platform-specific files (os_*.c) and is #included into those +** files. +** +** This file should be #included by the os_*.c files only. It is not a +** general purpose header file. +*/ +#ifndef _OS_COMMON_H_ +#define _OS_COMMON_H_ + +/* +** At least two bugs have slipped in because we changed the MEMORY_DEBUG +** macro to SQLITE_DEBUG and some older makefiles have not yet made the +** switch. The following code should catch this problem at compile-time. +*/ +#ifdef MEMORY_DEBUG +# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." +#endif + +/* +** Macros for performance tracing. Normally turned off. Only works +** on i486 hardware. +*/ +#ifdef SQLITE_PERFORMANCE_TRACE + +/* +** hwtime.h contains inline assembler code for implementing +** high-performance timing routines. +*/ +/************** Include hwtime.h in the middle of os_common.h ****************/ +/************** Begin file hwtime.h ******************************************/ +/* +** 2008 May 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains inline asm code for retrieving "high-performance" +** counters for x86 class CPUs. +*/ +#ifndef _HWTIME_H_ +#define _HWTIME_H_ + +/* +** The following routine only works on pentium-class (or newer) processors. +** It uses the RDTSC opcode to read the cycle count value out of the +** processor and returns that value. This can be used for high-res +** profiling. +*/ +#if (defined(__GNUC__) || defined(_MSC_VER)) && \ + (defined(i386) || defined(__i386__) || defined(_M_IX86)) + + #if defined(__GNUC__) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned int lo, hi; + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return (sqlite_uint64)hi << 32 | lo; + } + + #elif defined(_MSC_VER) + + __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ + __asm { + rdtsc + ret ; return value at EDX:EAX + } + } + + #endif + +#elif (defined(__GNUC__) && defined(__x86_64__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long val; + __asm__ __volatile__ ("rdtsc" : "=A" (val)); + return val; + } + +#elif (defined(__GNUC__) && defined(__ppc__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long long retval; + unsigned long junk; + __asm__ __volatile__ ("\n\ + 1: mftbu %1\n\ + mftb %L0\n\ + mftbu %0\n\ + cmpw %0,%1\n\ + bne 1b" + : "=r" (retval), "=r" (junk)); + return retval; + } + +#else + + #error Need implementation of sqlite3Hwtime() for your platform. + + /* + ** To compile without implementing sqlite3Hwtime() for your platform, + ** you can remove the above #error and use the following + ** stub function. You will lose timing support for many + ** of the debugging and testing utilities, but it should at + ** least compile and run. + */ +SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + +#endif + +#endif /* !defined(_HWTIME_H_) */ + +/************** End of hwtime.h **********************************************/ +/************** Continuing where we left off in os_common.h ******************/ + +static sqlite_uint64 g_start; +static sqlite_uint64 g_elapsed; +#define TIMER_START g_start=sqlite3Hwtime() +#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start +#define TIMER_ELAPSED g_elapsed +#else +#define TIMER_START +#define TIMER_END +#define TIMER_ELAPSED ((sqlite_uint64)0) +#endif + +/* +** If we compile with the SQLITE_TEST macro set, then the following block +** of code will give us the ability to simulate a disk I/O error. This +** is used for testing the I/O recovery logic. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_io_error_hit; +SQLITE_API extern int sqlite3_io_error_hardhit; +SQLITE_API extern int sqlite3_io_error_pending; +SQLITE_API extern int sqlite3_io_error_persist; +SQLITE_API extern int sqlite3_io_error_benign; +SQLITE_API extern int sqlite3_diskfull_pending; +SQLITE_API extern int sqlite3_diskfull; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) +#define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } +static void local_ioerr(){ + IOTRACE(("IOERR\n")); + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; +} +#define SimulateDiskfullError(CODE) \ + if( sqlite3_diskfull_pending ){ \ + if( sqlite3_diskfull_pending == 1 ){ \ + local_ioerr(); \ + sqlite3_diskfull = 1; \ + sqlite3_io_error_hit = 1; \ + CODE; \ + }else{ \ + sqlite3_diskfull_pending--; \ + } \ + } +#else +#define SimulateIOErrorBenign(X) +#define SimulateIOError(A) +#define SimulateDiskfullError(A) +#endif /* defined(SQLITE_TEST) */ + +/* +** When testing, keep a count of the number of open files. +*/ +#if defined(SQLITE_TEST) +SQLITE_API extern int sqlite3_open_file_count; +#define OpenCounter(X) sqlite3_open_file_count+=(X) +#else +#define OpenCounter(X) +#endif /* defined(SQLITE_TEST) */ + +#endif /* !defined(_OS_COMMON_H_) */ + +/************** End of os_common.h *******************************************/ +/************** Continuing where we left off in os_win.c *********************/ + +/* +** Include the header file for the Windows VFS. +*/ +/* #include "os_win.h" */ + +/* +** Compiling and using WAL mode requires several APIs that are only +** available in Windows platforms based on the NT kernel. +*/ +#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) +# error "WAL mode requires support from the Windows NT kernel, compile\ + with SQLITE_OMIT_WAL." +#endif + +#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 +# error "Memory mapped files require support from the Windows NT kernel,\ + compile with SQLITE_MAX_MMAP_SIZE=0." +#endif + +/* +** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions +** based on the sub-platform)? +*/ +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) +# define SQLITE_WIN32_HAS_ANSI +#endif + +/* +** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions +** based on the sub-platform)? +*/ +#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \ + !defined(SQLITE_WIN32_NO_WIDE) +# define SQLITE_WIN32_HAS_WIDE +#endif + +/* +** Make sure at least one set of Win32 APIs is available. +*/ +#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE) +# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\ + must be defined." +#endif + +/* +** Define the required Windows SDK version constants if they are not +** already available. +*/ +#ifndef NTDDI_WIN8 +# define NTDDI_WIN8 0x06020000 +#endif + +#ifndef NTDDI_WINBLUE +# define NTDDI_WINBLUE 0x06030000 +#endif + +#ifndef NTDDI_WINTHRESHOLD +# define NTDDI_WINTHRESHOLD 0x06040000 +#endif + +/* +** Check to see if the GetVersionEx[AW] functions are deprecated on the +** target system. GetVersionEx was first deprecated in Win8.1. +*/ +#ifndef SQLITE_WIN32_GETVERSIONEX +# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE +# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */ +# else +# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */ +# endif +#endif + +/* +** Check to see if the CreateFileMappingA function is supported on the +** target system. It is unavailable when using "mincore.lib" on Win10. +** When compiling for Windows 10, always assume "mincore.lib" is in use. +*/ +#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA +# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD +# define SQLITE_WIN32_CREATEFILEMAPPINGA 0 +# else +# define SQLITE_WIN32_CREATEFILEMAPPINGA 1 +# endif +#endif + +/* +** This constant should already be defined (in the "WinDef.h" SDK file). +*/ +#ifndef MAX_PATH +# define MAX_PATH (260) +#endif + +/* +** Maximum pathname length (in chars) for Win32. This should normally be +** MAX_PATH. +*/ +#ifndef SQLITE_WIN32_MAX_PATH_CHARS +# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH) +#endif + +/* +** This constant should already be defined (in the "WinNT.h" SDK file). +*/ +#ifndef UNICODE_STRING_MAX_CHARS +# define UNICODE_STRING_MAX_CHARS (32767) +#endif + +/* +** Maximum pathname length (in chars) for WinNT. This should normally be +** UNICODE_STRING_MAX_CHARS. +*/ +#ifndef SQLITE_WINNT_MAX_PATH_CHARS +# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS) +#endif + +/* +** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in +** characters, so we allocate 4 bytes per character assuming worst-case of +** 4-bytes-per-character for UTF8. +*/ +#ifndef SQLITE_WIN32_MAX_PATH_BYTES +# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4) +#endif + +/* +** Maximum pathname length (in bytes) for WinNT. This should normally be +** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR). +*/ +#ifndef SQLITE_WINNT_MAX_PATH_BYTES +# define SQLITE_WINNT_MAX_PATH_BYTES \ + (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS) +#endif + +/* +** Maximum error message length (in chars) for WinRT. +*/ +#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS +# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024) +#endif + +/* +** Returns non-zero if the character should be treated as a directory +** separator. +*/ +#ifndef winIsDirSep +# define winIsDirSep(a) (((a) == '/') || ((a) == '\\')) +#endif + +/* +** This macro is used when a local variable is set to a value that is +** [sometimes] not used by the code (e.g. via conditional compilation). +*/ +#ifndef UNUSED_VARIABLE_VALUE +# define UNUSED_VARIABLE_VALUE(x) (void)(x) +#endif + +/* +** Returns the character that should be used as the directory separator. +*/ +#ifndef winGetDirSep +# define winGetDirSep() '\\' +#endif + +/* +** Do we need to manually define the Win32 file mapping APIs for use with WAL +** mode or memory mapped files (e.g. these APIs are available in the Windows +** CE SDK; however, they are not present in the header file)? +*/ +#if SQLITE_WIN32_FILEMAPPING_API && \ + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) +/* +** Two of the file mapping APIs are different under WinRT. Figure out which +** set we need. +*/ +#if SQLITE_OS_WINRT +WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \ + LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR); + +WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T); +#else +#if defined(SQLITE_WIN32_HAS_ANSI) +WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \ + DWORD, DWORD, DWORD, LPCSTR); +#endif /* defined(SQLITE_WIN32_HAS_ANSI) */ + +#if defined(SQLITE_WIN32_HAS_WIDE) +WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \ + DWORD, DWORD, DWORD, LPCWSTR); +#endif /* defined(SQLITE_WIN32_HAS_WIDE) */ + +WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T); +#endif /* SQLITE_OS_WINRT */ + +/* +** These file mapping APIs are common to both Win32 and WinRT. +*/ + +WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T); +WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); +#endif /* SQLITE_WIN32_FILEMAPPING_API */ + +/* +** Some Microsoft compilers lack this definition. +*/ +#ifndef INVALID_FILE_ATTRIBUTES +# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) +#endif + +#ifndef FILE_FLAG_MASK +# define FILE_FLAG_MASK (0xFF3C0000) +#endif + +#ifndef FILE_ATTRIBUTE_MASK +# define FILE_ATTRIBUTE_MASK (0x0003FFF7) +#endif + +#ifndef SQLITE_OMIT_WAL +/* Forward references to structures used for WAL */ +typedef struct winShm winShm; /* A connection to shared-memory */ +typedef struct winShmNode winShmNode; /* A region of shared-memory */ +#endif + +/* +** WinCE lacks native support for file locking so we have to fake it +** with some code of our own. +*/ +#if SQLITE_OS_WINCE +typedef struct winceLock { + int nReaders; /* Number of reader locks obtained */ + BOOL bPending; /* Indicates a pending lock has been obtained */ + BOOL bReserved; /* Indicates a reserved lock has been obtained */ + BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ +} winceLock; +#endif + +/* +** The winFile structure is a subclass of sqlite3_file* specific to the win32 +** portability layer. +*/ +typedef struct winFile winFile; +struct winFile { + const sqlite3_io_methods *pMethod; /*** Must be first ***/ + sqlite3_vfs *pVfs; /* The VFS used to open this file */ + HANDLE h; /* Handle for accessing the file */ + u8 locktype; /* Type of lock currently held on this file */ + short sharedLockByte; /* Randomly chosen byte used as a shared lock */ + u8 ctrlFlags; /* Flags. See WINFILE_* below */ + DWORD lastErrno; /* The Windows errno from the last I/O error */ +#ifndef SQLITE_OMIT_WAL + winShm *pShm; /* Instance of shared memory on this file */ +#endif + const char *zPath; /* Full pathname of this file */ + int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */ +#if SQLITE_OS_WINCE + LPWSTR zDeleteOnClose; /* Name of file to delete when closing */ + HANDLE hMutex; /* Mutex used to control access to shared lock */ + HANDLE hShared; /* Shared memory segment used for locking */ + winceLock local; /* Locks obtained by this instance of winFile */ + winceLock *shared; /* Global shared lock memory for the file */ +#endif +#if SQLITE_MAX_MMAP_SIZE>0 + int nFetchOut; /* Number of outstanding xFetch references */ + HANDLE hMap; /* Handle for accessing memory mapping */ + void *pMapRegion; /* Area memory mapped */ + sqlite3_int64 mmapSize; /* Usable size of mapped region */ + sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */ + sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ +#endif +}; + +/* +** Allowed values for winFile.ctrlFlags +*/ +#define WINFILE_RDONLY 0x02 /* Connection is read only */ +#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ +#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ + +/* + * The size of the buffer used by sqlite3_win32_write_debug(). + */ +#ifndef SQLITE_WIN32_DBG_BUF_SIZE +# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) +#endif + +/* + * The value used with sqlite3_win32_set_directory() to specify that + * the data directory should be changed. + */ +#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE +# define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1) +#endif + +/* + * The value used with sqlite3_win32_set_directory() to specify that + * the temporary directory should be changed. + */ +#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE +# define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2) +#endif + +/* + * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the + * various Win32 API heap functions instead of our own. + */ +#ifdef SQLITE_WIN32_MALLOC + +/* + * If this is non-zero, an isolated heap will be created by the native Win32 + * allocator subsystem; otherwise, the default process heap will be used. This + * setting has no effect when compiling for WinRT. By default, this is enabled + * and an isolated heap will be created to store all allocated data. + * + ****************************************************************************** + * WARNING: It is important to note that when this setting is non-zero and the + * winMemShutdown function is called (e.g. by the sqlite3_shutdown + * function), all data that was allocated using the isolated heap will + * be freed immediately and any attempt to access any of that freed + * data will almost certainly result in an immediate access violation. + ****************************************************************************** + */ +#ifndef SQLITE_WIN32_HEAP_CREATE +# define SQLITE_WIN32_HEAP_CREATE (TRUE) +#endif + +/* + * This is cache size used in the calculation of the initial size of the + * Win32-specific heap. It cannot be negative. + */ +#ifndef SQLITE_WIN32_CACHE_SIZE +# if SQLITE_DEFAULT_CACHE_SIZE>=0 +# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE) +# else +# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE)) +# endif +#endif + +/* + * The initial size of the Win32-specific heap. This value may be zero. + */ +#ifndef SQLITE_WIN32_HEAP_INIT_SIZE +# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \ + (SQLITE_DEFAULT_PAGE_SIZE) + 4194304) +#endif + +/* + * The maximum size of the Win32-specific heap. This value may be zero. + */ +#ifndef SQLITE_WIN32_HEAP_MAX_SIZE +# define SQLITE_WIN32_HEAP_MAX_SIZE (0) +#endif + +/* + * The extra flags to use in calls to the Win32 heap APIs. This value may be + * zero for the default behavior. + */ +#ifndef SQLITE_WIN32_HEAP_FLAGS +# define SQLITE_WIN32_HEAP_FLAGS (0) +#endif + + +/* +** The winMemData structure stores information required by the Win32-specific +** sqlite3_mem_methods implementation. +*/ +typedef struct winMemData winMemData; +struct winMemData { +#ifndef NDEBUG + u32 magic1; /* Magic number to detect structure corruption. */ +#endif + HANDLE hHeap; /* The handle to our heap. */ + BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */ +#ifndef NDEBUG + u32 magic2; /* Magic number to detect structure corruption. */ +#endif +}; + +#ifndef NDEBUG +#define WINMEM_MAGIC1 0x42b2830b +#define WINMEM_MAGIC2 0xbd4d7cf4 +#endif + +static struct winMemData win_mem_data = { +#ifndef NDEBUG + WINMEM_MAGIC1, +#endif + NULL, FALSE +#ifndef NDEBUG + ,WINMEM_MAGIC2 +#endif +}; + +#ifndef NDEBUG +#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 ) +#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 ) +#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2(); +#else +#define winMemAssertMagic() +#endif + +#define winMemGetDataPtr() &win_mem_data +#define winMemGetHeap() win_mem_data.hHeap +#define winMemGetOwned() win_mem_data.bOwned + +static void *winMemMalloc(int nBytes); +static void winMemFree(void *pPrior); +static void *winMemRealloc(void *pPrior, int nBytes); +static int winMemSize(void *p); +static int winMemRoundup(int n); +static int winMemInit(void *pAppData); +static void winMemShutdown(void *pAppData); + +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void); +#endif /* SQLITE_WIN32_MALLOC */ + +/* +** The following variable is (normally) set once and never changes +** thereafter. It records whether the operating system is Win9x +** or WinNT. +** +** 0: Operating system unknown. +** 1: Operating system is Win9x. +** 2: Operating system is WinNT. +** +** In order to facilitate testing on a WinNT system, the test fixture +** can manually set this value to 1 to emulate Win98 behavior. +*/ +#ifdef SQLITE_TEST +SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; +#else +static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; +#endif + +#ifndef SYSCALL +# define SYSCALL sqlite3_syscall_ptr +#endif + +/* +** This function is not available on Windows CE or WinRT. + */ + +#if SQLITE_OS_WINCE || SQLITE_OS_WINRT +# define osAreFileApisANSI() 1 +#endif + +/* +** Many system calls are accessed through pointer-to-functions so that +** they may be overridden at runtime to facilitate fault injection during +** testing and sandboxing. The following array holds the names and pointers +** to all overrideable system calls. +*/ +static struct win_syscall { + const char *zName; /* Name of the system call */ + sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ + sqlite3_syscall_ptr pDefault; /* Default value */ +} aSyscall[] = { +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT + { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 }, +#else + { "AreFileApisANSI", (SYSCALL)0, 0 }, +#endif + +#ifndef osAreFileApisANSI +#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent) +#endif + +#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) + { "CharLowerW", (SYSCALL)CharLowerW, 0 }, +#else + { "CharLowerW", (SYSCALL)0, 0 }, +#endif + +#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent) + +#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) + { "CharUpperW", (SYSCALL)CharUpperW, 0 }, +#else + { "CharUpperW", (SYSCALL)0, 0 }, +#endif + +#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent) + + { "CloseHandle", (SYSCALL)CloseHandle, 0 }, + +#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "CreateFileA", (SYSCALL)CreateFileA, 0 }, +#else + { "CreateFileA", (SYSCALL)0, 0 }, +#endif + +#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \ + LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "CreateFileW", (SYSCALL)CreateFileW, 0 }, +#else + { "CreateFileW", (SYSCALL)0, 0 }, +#endif + +#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ + LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \ + SQLITE_WIN32_CREATEFILEMAPPINGA + { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, +#else + { "CreateFileMappingA", (SYSCALL)0, 0 }, +#endif + +#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ + DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) + +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) + { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, +#else + { "CreateFileMappingW", (SYSCALL)0, 0 }, +#endif + +#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ + DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "CreateMutexW", (SYSCALL)CreateMutexW, 0 }, +#else + { "CreateMutexW", (SYSCALL)0, 0 }, +#endif + +#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \ + LPCWSTR))aSyscall[8].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "DeleteFileA", (SYSCALL)DeleteFileA, 0 }, +#else + { "DeleteFileA", (SYSCALL)0, 0 }, +#endif + +#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent) + +#if defined(SQLITE_WIN32_HAS_WIDE) + { "DeleteFileW", (SYSCALL)DeleteFileW, 0 }, +#else + { "DeleteFileW", (SYSCALL)0, 0 }, +#endif + +#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent) + +#if SQLITE_OS_WINCE + { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 }, +#else + { "FileTimeToLocalFileTime", (SYSCALL)0, 0 }, +#endif + +#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \ + LPFILETIME))aSyscall[11].pCurrent) + +#if SQLITE_OS_WINCE + { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 }, +#else + { "FileTimeToSystemTime", (SYSCALL)0, 0 }, +#endif + +#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \ + LPSYSTEMTIME))aSyscall[12].pCurrent) + + { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 }, + +#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "FormatMessageA", (SYSCALL)FormatMessageA, 0 }, +#else + { "FormatMessageA", (SYSCALL)0, 0 }, +#endif + +#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \ + DWORD,va_list*))aSyscall[14].pCurrent) + +#if defined(SQLITE_WIN32_HAS_WIDE) + { "FormatMessageW", (SYSCALL)FormatMessageW, 0 }, +#else + { "FormatMessageW", (SYSCALL)0, 0 }, +#endif + +#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \ + DWORD,va_list*))aSyscall[15].pCurrent) + +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) + { "FreeLibrary", (SYSCALL)FreeLibrary, 0 }, +#else + { "FreeLibrary", (SYSCALL)0, 0 }, +#endif + +#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent) + + { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 }, + +#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent) + +#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) + { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 }, +#else + { "GetDiskFreeSpaceA", (SYSCALL)0, 0 }, +#endif + +#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \ + LPDWORD))aSyscall[18].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 }, +#else + { "GetDiskFreeSpaceW", (SYSCALL)0, 0 }, +#endif + +#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \ + LPDWORD))aSyscall[19].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 }, +#else + { "GetFileAttributesA", (SYSCALL)0, 0 }, +#endif + +#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 }, +#else + { "GetFileAttributesW", (SYSCALL)0, 0 }, +#endif + +#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent) + +#if defined(SQLITE_WIN32_HAS_WIDE) + { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 }, +#else + { "GetFileAttributesExW", (SYSCALL)0, 0 }, +#endif + +#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \ + LPVOID))aSyscall[22].pCurrent) + +#if !SQLITE_OS_WINRT + { "GetFileSize", (SYSCALL)GetFileSize, 0 }, +#else + { "GetFileSize", (SYSCALL)0, 0 }, +#endif + +#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent) + +#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) + { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 }, +#else + { "GetFullPathNameA", (SYSCALL)0, 0 }, +#endif + +#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \ + LPSTR*))aSyscall[24].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 }, +#else + { "GetFullPathNameW", (SYSCALL)0, 0 }, +#endif + +#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \ + LPWSTR*))aSyscall[25].pCurrent) + + { "GetLastError", (SYSCALL)GetLastError, 0 }, + +#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent) + +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) +#if SQLITE_OS_WINCE + /* The GetProcAddressA() routine is only available on Windows CE. */ + { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 }, +#else + /* All other Windows platforms expect GetProcAddress() to take + ** an ANSI string regardless of the _UNICODE setting */ + { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 }, +#endif +#else + { "GetProcAddressA", (SYSCALL)0, 0 }, +#endif + +#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \ + LPCSTR))aSyscall[27].pCurrent) + +#if !SQLITE_OS_WINRT + { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 }, +#else + { "GetSystemInfo", (SYSCALL)0, 0 }, +#endif + +#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent) + + { "GetSystemTime", (SYSCALL)GetSystemTime, 0 }, + +#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent) + +#if !SQLITE_OS_WINCE + { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 }, +#else + { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 }, +#endif + +#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \ + LPFILETIME))aSyscall[30].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "GetTempPathA", (SYSCALL)GetTempPathA, 0 }, +#else + { "GetTempPathA", (SYSCALL)0, 0 }, +#endif + +#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) + { "GetTempPathW", (SYSCALL)GetTempPathW, 0 }, +#else + { "GetTempPathW", (SYSCALL)0, 0 }, +#endif + +#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent) + +#if !SQLITE_OS_WINRT + { "GetTickCount", (SYSCALL)GetTickCount, 0 }, +#else + { "GetTickCount", (SYSCALL)0, 0 }, +#endif + +#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX + { "GetVersionExA", (SYSCALL)GetVersionExA, 0 }, +#else + { "GetVersionExA", (SYSCALL)0, 0 }, +#endif + +#define osGetVersionExA ((BOOL(WINAPI*)( \ + LPOSVERSIONINFOA))aSyscall[34].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ + SQLITE_WIN32_GETVERSIONEX + { "GetVersionExW", (SYSCALL)GetVersionExW, 0 }, +#else + { "GetVersionExW", (SYSCALL)0, 0 }, +#endif + +#define osGetVersionExW ((BOOL(WINAPI*)( \ + LPOSVERSIONINFOW))aSyscall[35].pCurrent) + + { "HeapAlloc", (SYSCALL)HeapAlloc, 0 }, + +#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \ + SIZE_T))aSyscall[36].pCurrent) + +#if !SQLITE_OS_WINRT + { "HeapCreate", (SYSCALL)HeapCreate, 0 }, +#else + { "HeapCreate", (SYSCALL)0, 0 }, +#endif + +#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \ + SIZE_T))aSyscall[37].pCurrent) + +#if !SQLITE_OS_WINRT + { "HeapDestroy", (SYSCALL)HeapDestroy, 0 }, +#else + { "HeapDestroy", (SYSCALL)0, 0 }, +#endif + +#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent) + + { "HeapFree", (SYSCALL)HeapFree, 0 }, + +#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent) + + { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 }, + +#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \ + SIZE_T))aSyscall[40].pCurrent) + + { "HeapSize", (SYSCALL)HeapSize, 0 }, + +#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \ + LPCVOID))aSyscall[41].pCurrent) + +#if !SQLITE_OS_WINRT + { "HeapValidate", (SYSCALL)HeapValidate, 0 }, +#else + { "HeapValidate", (SYSCALL)0, 0 }, +#endif + +#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \ + LPCVOID))aSyscall[42].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT + { "HeapCompact", (SYSCALL)HeapCompact, 0 }, +#else + { "HeapCompact", (SYSCALL)0, 0 }, +#endif + +#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION) + { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 }, +#else + { "LoadLibraryA", (SYSCALL)0, 0 }, +#endif + +#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent) + +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ + !defined(SQLITE_OMIT_LOAD_EXTENSION) + { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 }, +#else + { "LoadLibraryW", (SYSCALL)0, 0 }, +#endif + +#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent) + +#if !SQLITE_OS_WINRT + { "LocalFree", (SYSCALL)LocalFree, 0 }, +#else + { "LocalFree", (SYSCALL)0, 0 }, +#endif + +#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT + { "LockFile", (SYSCALL)LockFile, 0 }, +#else + { "LockFile", (SYSCALL)0, 0 }, +#endif + +#ifndef osLockFile +#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ + DWORD))aSyscall[47].pCurrent) +#endif + +#if !SQLITE_OS_WINCE + { "LockFileEx", (SYSCALL)LockFileEx, 0 }, +#else + { "LockFileEx", (SYSCALL)0, 0 }, +#endif + +#ifndef osLockFileEx +#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ + LPOVERLAPPED))aSyscall[48].pCurrent) +#endif + +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) + { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, +#else + { "MapViewOfFile", (SYSCALL)0, 0 }, +#endif + +#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ + SIZE_T))aSyscall[49].pCurrent) + + { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 }, + +#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \ + int))aSyscall[50].pCurrent) + + { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 }, + +#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \ + LARGE_INTEGER*))aSyscall[51].pCurrent) + + { "ReadFile", (SYSCALL)ReadFile, 0 }, + +#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \ + LPOVERLAPPED))aSyscall[52].pCurrent) + + { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 }, + +#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent) + +#if !SQLITE_OS_WINRT + { "SetFilePointer", (SYSCALL)SetFilePointer, 0 }, +#else + { "SetFilePointer", (SYSCALL)0, 0 }, +#endif + +#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \ + DWORD))aSyscall[54].pCurrent) + +#if !SQLITE_OS_WINRT + { "Sleep", (SYSCALL)Sleep, 0 }, +#else + { "Sleep", (SYSCALL)0, 0 }, +#endif + +#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent) + + { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 }, + +#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \ + LPFILETIME))aSyscall[56].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT + { "UnlockFile", (SYSCALL)UnlockFile, 0 }, +#else + { "UnlockFile", (SYSCALL)0, 0 }, +#endif + +#ifndef osUnlockFile +#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ + DWORD))aSyscall[57].pCurrent) +#endif + +#if !SQLITE_OS_WINCE + { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 }, +#else + { "UnlockFileEx", (SYSCALL)0, 0 }, +#endif + +#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ + LPOVERLAPPED))aSyscall[58].pCurrent) + +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, +#else + { "UnmapViewOfFile", (SYSCALL)0, 0 }, +#endif + +#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent) + + { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 }, + +#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \ + LPCSTR,LPBOOL))aSyscall[60].pCurrent) + + { "WriteFile", (SYSCALL)WriteFile, 0 }, + +#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \ + LPOVERLAPPED))aSyscall[61].pCurrent) + +#if SQLITE_OS_WINRT + { "CreateEventExW", (SYSCALL)CreateEventExW, 0 }, +#else + { "CreateEventExW", (SYSCALL)0, 0 }, +#endif + +#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \ + DWORD,DWORD))aSyscall[62].pCurrent) + +#if !SQLITE_OS_WINRT + { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 }, +#else + { "WaitForSingleObject", (SYSCALL)0, 0 }, +#endif + +#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ + DWORD))aSyscall[63].pCurrent) + +#if !SQLITE_OS_WINCE + { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 }, +#else + { "WaitForSingleObjectEx", (SYSCALL)0, 0 }, +#endif + +#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \ + BOOL))aSyscall[64].pCurrent) + +#if SQLITE_OS_WINRT + { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 }, +#else + { "SetFilePointerEx", (SYSCALL)0, 0 }, +#endif + +#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \ + PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent) + +#if SQLITE_OS_WINRT + { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 }, +#else + { "GetFileInformationByHandleEx", (SYSCALL)0, 0 }, +#endif + +#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ + FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) + +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) + { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, +#else + { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, +#endif + +#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \ + SIZE_T))aSyscall[67].pCurrent) + +#if SQLITE_OS_WINRT + { "CreateFile2", (SYSCALL)CreateFile2, 0 }, +#else + { "CreateFile2", (SYSCALL)0, 0 }, +#endif + +#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \ + LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent) + +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION) + { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 }, +#else + { "LoadPackagedLibrary", (SYSCALL)0, 0 }, +#endif + +#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \ + DWORD))aSyscall[69].pCurrent) + +#if SQLITE_OS_WINRT + { "GetTickCount64", (SYSCALL)GetTickCount64, 0 }, +#else + { "GetTickCount64", (SYSCALL)0, 0 }, +#endif + +#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent) + +#if SQLITE_OS_WINRT + { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 }, +#else + { "GetNativeSystemInfo", (SYSCALL)0, 0 }, +#endif + +#define osGetNativeSystemInfo ((VOID(WINAPI*)( \ + LPSYSTEM_INFO))aSyscall[71].pCurrent) + +#if defined(SQLITE_WIN32_HAS_ANSI) + { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 }, +#else + { "OutputDebugStringA", (SYSCALL)0, 0 }, +#endif + +#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent) + +#if defined(SQLITE_WIN32_HAS_WIDE) + { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 }, +#else + { "OutputDebugStringW", (SYSCALL)0, 0 }, +#endif + +#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent) + + { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, + +#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) + +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) + { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, +#else + { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, +#endif + +#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \ + LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent) + +/* +** NOTE: On some sub-platforms, the InterlockedCompareExchange "function" +** is really just a macro that uses a compiler intrinsic (e.g. x64). +** So do not try to make this is into a redefinable interface. +*/ +#if defined(InterlockedCompareExchange) + { "InterlockedCompareExchange", (SYSCALL)0, 0 }, + +#define osInterlockedCompareExchange InterlockedCompareExchange +#else + { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 }, + +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \ + SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent) +#endif /* defined(InterlockedCompareExchange) */ + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID + { "UuidCreate", (SYSCALL)UuidCreate, 0 }, +#else + { "UuidCreate", (SYSCALL)0, 0 }, +#endif + +#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent) + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID + { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 }, +#else + { "UuidCreateSequential", (SYSCALL)0, 0 }, +#endif + +#define osUuidCreateSequential \ + ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent) + +#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0 + { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 }, +#else + { "FlushViewOfFile", (SYSCALL)0, 0 }, +#endif + +#define osFlushViewOfFile \ + ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent) + +}; /* End of the overrideable system calls */ + +/* +** This is the xSetSystemCall() method of sqlite3_vfs for all of the +** "win32" VFSes. Return SQLITE_OK opon successfully updating the +** system call pointer, or SQLITE_NOTFOUND if there is no configurable +** system call named zName. +*/ +static int winSetSystemCall( + sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ + const char *zName, /* Name of system call to override */ + sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ +){ + unsigned int i; + int rc = SQLITE_NOTFOUND; + + UNUSED_PARAMETER(pNotUsed); + if( zName==0 ){ + /* If no zName is given, restore all system calls to their default + ** settings and return NULL + */ + rc = SQLITE_OK; + for(i=0; i0 ){ + memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); + memcpy(zDbgBuf, zBuf, nMin); + osOutputDebugStringA(zDbgBuf); + }else{ + osOutputDebugStringA(zBuf); + } +#elif defined(SQLITE_WIN32_HAS_WIDE) + memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); + if ( osMultiByteToWideChar( + osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf, + nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){ + return; + } + osOutputDebugStringW((LPCWSTR)zDbgBuf); +#else + if( nMin>0 ){ + memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); + memcpy(zDbgBuf, zBuf, nMin); + fprintf(stderr, "%s", zDbgBuf); + }else{ + fprintf(stderr, "%s", zBuf); + } +#endif +} + +/* +** The following routine suspends the current thread for at least ms +** milliseconds. This is equivalent to the Win32 Sleep() interface. +*/ +#if SQLITE_OS_WINRT +static HANDLE sleepObj = NULL; +#endif + +SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds){ +#if SQLITE_OS_WINRT + if ( sleepObj==NULL ){ + sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, + SYNCHRONIZE); + } + assert( sleepObj!=NULL ); + osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE); +#else + osSleep(milliseconds); +#endif +} + +#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ + SQLITE_THREADSAFE>0 +SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){ + DWORD rc; + while( (rc = osWaitForSingleObjectEx(hObject, INFINITE, + TRUE))==WAIT_IO_COMPLETION ){} + return rc; +} +#endif + +/* +** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, +** or WinCE. Return false (zero) for Win95, Win98, or WinME. +** +** Here is an interesting observation: Win95, Win98, and WinME lack +** the LockFileEx() API. But we can still statically link against that +** API as long as we don't call it when running Win95/98/ME. A call to +** this routine is used to determine if the host is Win95/98/ME or +** WinNT/2K/XP so that we will know whether or not we can safely call +** the LockFileEx() API. +*/ + +#if !SQLITE_WIN32_GETVERSIONEX +# define osIsNT() (1) +#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI) +# define osIsNT() (1) +#elif !defined(SQLITE_WIN32_HAS_WIDE) +# define osIsNT() (0) +#else +# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt()) +#endif + +/* +** This function determines if the machine is running a version of Windows +** based on the NT kernel. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void){ +#if SQLITE_OS_WINRT + /* + ** NOTE: The WinRT sub-platform is always assumed to be based on the NT + ** kernel. + */ + return 1; +#elif SQLITE_WIN32_GETVERSIONEX + if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){ +#if defined(SQLITE_WIN32_HAS_ANSI) + OSVERSIONINFOA sInfo; + sInfo.dwOSVersionInfoSize = sizeof(sInfo); + osGetVersionExA(&sInfo); + osInterlockedCompareExchange(&sqlite3_os_type, + (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); +#elif defined(SQLITE_WIN32_HAS_WIDE) + OSVERSIONINFOW sInfo; + sInfo.dwOSVersionInfoSize = sizeof(sInfo); + osGetVersionExW(&sInfo); + osInterlockedCompareExchange(&sqlite3_os_type, + (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); +#endif + } + return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; +#elif SQLITE_TEST + return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; +#else + /* + ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are + ** deprecated are always assumed to be based on the NT kernel. + */ + return 1; +#endif +} + +#ifdef SQLITE_WIN32_MALLOC +/* +** Allocate nBytes of memory. +*/ +static void *winMemMalloc(int nBytes){ + HANDLE hHeap; + void *p; + + winMemAssertMagic(); + hHeap = winMemGetHeap(); + assert( hHeap!=0 ); + assert( hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); +#endif + assert( nBytes>=0 ); + p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); + if( !p ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p", + nBytes, osGetLastError(), (void*)hHeap); + } + return p; +} + +/* +** Free memory. +*/ +static void winMemFree(void *pPrior){ + HANDLE hHeap; + + winMemAssertMagic(); + hHeap = winMemGetHeap(); + assert( hHeap!=0 ); + assert( hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); +#endif + if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */ + if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p", + pPrior, osGetLastError(), (void*)hHeap); + } +} + +/* +** Change the size of an existing memory allocation +*/ +static void *winMemRealloc(void *pPrior, int nBytes){ + HANDLE hHeap; + void *p; + + winMemAssertMagic(); + hHeap = winMemGetHeap(); + assert( hHeap!=0 ); + assert( hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); +#endif + assert( nBytes>=0 ); + if( !pPrior ){ + p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); + }else{ + p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes); + } + if( !p ){ + sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p", + pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(), + (void*)hHeap); + } + return p; +} + +/* +** Return the size of an outstanding allocation, in bytes. +*/ +static int winMemSize(void *p){ + HANDLE hHeap; + SIZE_T n; + + winMemAssertMagic(); + hHeap = winMemGetHeap(); + assert( hHeap!=0 ); + assert( hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) ); +#endif + if( !p ) return 0; + n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p); + if( n==(SIZE_T)-1 ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p", + p, osGetLastError(), (void*)hHeap); + return 0; + } + return (int)n; +} + +/* +** Round up a request size to the next valid allocation size. +*/ +static int winMemRoundup(int n){ + return n; +} + +/* +** Initialize this module. +*/ +static int winMemInit(void *pAppData){ + winMemData *pWinMemData = (winMemData *)pAppData; + + if( !pWinMemData ) return SQLITE_ERROR; + assert( pWinMemData->magic1==WINMEM_MAGIC1 ); + assert( pWinMemData->magic2==WINMEM_MAGIC2 ); + +#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE + if( !pWinMemData->hHeap ){ + DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE; + DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap; + if( dwMaximumSize==0 ){ + dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE; + }else if( dwInitialSize>dwMaximumSize ){ + dwInitialSize = dwMaximumSize; + } + pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS, + dwInitialSize, dwMaximumSize); + if( !pWinMemData->hHeap ){ + sqlite3_log(SQLITE_NOMEM, + "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu", + osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize, + dwMaximumSize); + return SQLITE_NOMEM_BKPT; + } + pWinMemData->bOwned = TRUE; + assert( pWinMemData->bOwned ); + } +#else + pWinMemData->hHeap = osGetProcessHeap(); + if( !pWinMemData->hHeap ){ + sqlite3_log(SQLITE_NOMEM, + "failed to GetProcessHeap (%lu)", osGetLastError()); + return SQLITE_NOMEM_BKPT; + } + pWinMemData->bOwned = FALSE; + assert( !pWinMemData->bOwned ); +#endif + assert( pWinMemData->hHeap!=0 ); + assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); +#endif + return SQLITE_OK; +} + +/* +** Deinitialize this module. +*/ +static void winMemShutdown(void *pAppData){ + winMemData *pWinMemData = (winMemData *)pAppData; + + if( !pWinMemData ) return; + assert( pWinMemData->magic1==WINMEM_MAGIC1 ); + assert( pWinMemData->magic2==WINMEM_MAGIC2 ); + + if( pWinMemData->hHeap ){ + assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) + assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); +#endif + if( pWinMemData->bOwned ){ + if( !osHeapDestroy(pWinMemData->hHeap) ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p", + osGetLastError(), (void*)pWinMemData->hHeap); + } + pWinMemData->bOwned = FALSE; + } + pWinMemData->hHeap = NULL; + } +} + +/* +** Populate the low-level memory allocation function pointers in +** sqlite3GlobalConfig.m with pointers to the routines in this file. The +** arguments specify the block of memory to manage. +** +** This routine is only called by sqlite3_config(), and therefore +** is not required to be threadsafe (it is not). +*/ +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){ + static const sqlite3_mem_methods winMemMethods = { + winMemMalloc, + winMemFree, + winMemRealloc, + winMemSize, + winMemRoundup, + winMemInit, + winMemShutdown, + &win_mem_data + }; + return &winMemMethods; +} + +SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32()); +} +#endif /* SQLITE_WIN32_MALLOC */ + +/* +** Convert a UTF-8 string to Microsoft Unicode. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static LPWSTR winUtf8ToUnicode(const char *zText){ + int nChar; + LPWSTR zWideText; + + nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); + if( nChar==0 ){ + return 0; + } + zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) ); + if( zWideText==0 ){ + return 0; + } + nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, + nChar); + if( nChar==0 ){ + sqlite3_free(zWideText); + zWideText = 0; + } + return zWideText; +} + +/* +** Convert a Microsoft Unicode string to UTF-8. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static char *winUnicodeToUtf8(LPCWSTR zWideText){ + int nByte; + char *zText; + + nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); + if( nByte == 0 ){ + return 0; + } + zText = sqlite3MallocZero( nByte ); + if( zText==0 ){ + return 0; + } + nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, + 0, 0); + if( nByte == 0 ){ + sqlite3_free(zText); + zText = 0; + } + return zText; +} + +/* +** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM +** code page. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){ + int nByte; + LPWSTR zMbcsText; + int codepage = useAnsi ? CP_ACP : CP_OEMCP; + + nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, + 0)*sizeof(WCHAR); + if( nByte==0 ){ + return 0; + } + zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) ); + if( zMbcsText==0 ){ + return 0; + } + nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText, + nByte); + if( nByte==0 ){ + sqlite3_free(zMbcsText); + zMbcsText = 0; + } + return zMbcsText; +} + +/* +** Convert a Microsoft Unicode string to a multi-byte character string, +** using the ANSI or OEM code page. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){ + int nByte; + char *zText; + int codepage = useAnsi ? CP_ACP : CP_OEMCP; + + nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0); + if( nByte == 0 ){ + return 0; + } + zText = sqlite3MallocZero( nByte ); + if( zText==0 ){ + return 0; + } + nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText, + nByte, 0, 0); + if( nByte == 0 ){ + sqlite3_free(zText); + zText = 0; + } + return zText; +} + +/* +** Convert a multi-byte character string to UTF-8. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static char *winMbcsToUtf8(const char *zText, int useAnsi){ + char *zTextUtf8; + LPWSTR zTmpWide; + + zTmpWide = winMbcsToUnicode(zText, useAnsi); + if( zTmpWide==0 ){ + return 0; + } + zTextUtf8 = winUnicodeToUtf8(zTmpWide); + sqlite3_free(zTmpWide); + return zTextUtf8; +} + +/* +** Convert a UTF-8 string to a multi-byte character string. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). +*/ +static char *winUtf8ToMbcs(const char *zText, int useAnsi){ + char *zTextMbcs; + LPWSTR zTmpWide; + + zTmpWide = winUtf8ToUnicode(zText); + if( zTmpWide==0 ){ + return 0; + } + zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi); + sqlite3_free(zTmpWide); + return zTextMbcs; +} + +/* +** This is a public wrapper for the winUtf8ToUnicode() function. +*/ +SQLITE_API LPWSTR SQLITE_STDCALL sqlite3_win32_utf8_to_unicode(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToUnicode(zText); +} + +/* +** This is a public wrapper for the winUnicodeToUtf8() function. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zWideText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUnicodeToUtf8(zWideText); +} + +/* +** This is a public wrapper for the winMbcsToUtf8() function. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winMbcsToUtf8(zText, osAreFileApisANSI()); +} + +/* +** This is a public wrapper for the winMbcsToUtf8() function. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winMbcsToUtf8(zText, useAnsi); +} + +/* +** This is a public wrapper for the winUtf8ToMbcs() function. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToMbcs(zText, osAreFileApisANSI()); +} + +/* +** This is a public wrapper for the winUtf8ToMbcs() function. +*/ +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToMbcs(zText, useAnsi); +} + +/* +** This function sets the data directory or the temporary directory based on +** the provided arguments. The type argument must be 1 in order to set the +** data directory or 2 in order to set the temporary directory. The zValue +** argument is the name of the directory to use. The return value will be +** SQLITE_OK if successful. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){ + char **ppDirectory = 0; +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){ + ppDirectory = &sqlite3_data_directory; + }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){ + ppDirectory = &sqlite3_temp_directory; + } + assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE + || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE + ); + assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); + if( ppDirectory ){ + char *zValueUtf8 = 0; + if( zValue && zValue[0] ){ + zValueUtf8 = winUnicodeToUtf8(zValue); + if ( zValueUtf8==0 ){ + return SQLITE_NOMEM_BKPT; + } + } + sqlite3_free(*ppDirectory); + *ppDirectory = zValueUtf8; + return SQLITE_OK; + } + return SQLITE_ERROR; +} + +/* +** The return value of winGetLastErrorMsg +** is zero if the error message fits in the buffer, or non-zero +** otherwise (if the message was truncated). +*/ +static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){ + /* FormatMessage returns 0 on failure. Otherwise it + ** returns the number of TCHARs written to the output + ** buffer, excluding the terminating null char. + */ + DWORD dwLen = 0; + char *zOut = 0; + + if( osIsNT() ){ +#if SQLITE_OS_WINRT + WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1]; + dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + lastErrno, + 0, + zTempWide, + SQLITE_WIN32_MAX_ERRMSG_CHARS, + 0); +#else + LPWSTR zTempWide = NULL; + dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + lastErrno, + 0, + (LPWSTR) &zTempWide, + 0, + 0); +#endif + if( dwLen > 0 ){ + /* allocate a buffer and convert to UTF8 */ + sqlite3BeginBenignMalloc(); + zOut = winUnicodeToUtf8(zTempWide); + sqlite3EndBenignMalloc(); +#if !SQLITE_OS_WINRT + /* free the system buffer allocated by FormatMessage */ + osLocalFree(zTempWide); +#endif + } + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + char *zTemp = NULL; + dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + lastErrno, + 0, + (LPSTR) &zTemp, + 0, + 0); + if( dwLen > 0 ){ + /* allocate a buffer and convert to UTF8 */ + sqlite3BeginBenignMalloc(); + zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); + sqlite3EndBenignMalloc(); + /* free the system buffer allocated by FormatMessage */ + osLocalFree(zTemp); + } + } +#endif + if( 0 == dwLen ){ + sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno); + }else{ + /* copy a maximum of nBuf chars to output buffer */ + sqlite3_snprintf(nBuf, zBuf, "%s", zOut); + /* free the UTF8 buffer */ + sqlite3_free(zOut); + } + return 0; +} + +/* +** +** This function - winLogErrorAtLine() - is only ever called via the macro +** winLogError(). +** +** This routine is invoked after an error occurs in an OS function. +** It logs a message using sqlite3_log() containing the current value of +** error code and, if possible, the human-readable equivalent from +** FormatMessage. +** +** The first argument passed to the macro should be the error code that +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). +** The two subsequent arguments should be the name of the OS function that +** failed and the associated file-system path, if any. +*/ +#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__) +static int winLogErrorAtLine( + int errcode, /* SQLite error code */ + DWORD lastErrno, /* Win32 last error */ + const char *zFunc, /* Name of OS function that failed */ + const char *zPath, /* File path associated with error */ + int iLine /* Source line number where error occurred */ +){ + char zMsg[500]; /* Human readable error text */ + int i; /* Loop counter */ + + zMsg[0] = 0; + winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg); + assert( errcode!=SQLITE_OK ); + if( zPath==0 ) zPath = ""; + for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){} + zMsg[i] = 0; + sqlite3_log(errcode, + "os_win.c:%d: (%lu) %s(%s) - %s", + iLine, lastErrno, zFunc, zPath, zMsg + ); + + return errcode; +} + +/* +** The number of times that a ReadFile(), WriteFile(), and DeleteFile() +** will be retried following a locking error - probably caused by +** antivirus software. Also the initial delay before the first retry. +** The delay increases linearly with each retry. +*/ +#ifndef SQLITE_WIN32_IOERR_RETRY +# define SQLITE_WIN32_IOERR_RETRY 10 +#endif +#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY +# define SQLITE_WIN32_IOERR_RETRY_DELAY 25 +#endif +static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY; +static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY; + +/* +** The "winIoerrCanRetry1" macro is used to determine if a particular I/O +** error code obtained via GetLastError() is eligible to be retried. It +** must accept the error code DWORD as its only argument and should return +** non-zero if the error code is transient in nature and the operation +** responsible for generating the original error might succeed upon being +** retried. The argument to this macro should be a variable. +** +** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it +** is defined, it will be consulted only when the macro "winIoerrCanRetry1" +** returns zero. The "winIoerrCanRetry2" macro is completely optional and +** may be used to include additional error codes in the set that should +** result in the failing I/O operation being retried by the caller. If +** defined, the "winIoerrCanRetry2" macro must exhibit external semantics +** identical to those of the "winIoerrCanRetry1" macro. +*/ +#if !defined(winIoerrCanRetry1) +#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \ + ((a)==ERROR_SHARING_VIOLATION) || \ + ((a)==ERROR_LOCK_VIOLATION) || \ + ((a)==ERROR_DEV_NOT_EXIST) || \ + ((a)==ERROR_NETNAME_DELETED) || \ + ((a)==ERROR_SEM_TIMEOUT) || \ + ((a)==ERROR_NETWORK_UNREACHABLE)) +#endif + +/* +** If a ReadFile() or WriteFile() error occurs, invoke this routine +** to see if it should be retried. Return TRUE to retry. Return FALSE +** to give up with an error. +*/ +static int winRetryIoerr(int *pnRetry, DWORD *pError){ + DWORD e = osGetLastError(); + if( *pnRetry>=winIoerrRetry ){ + if( pError ){ + *pError = e; + } + return 0; + } + if( winIoerrCanRetry1(e) ){ + sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); + ++*pnRetry; + return 1; + } +#if defined(winIoerrCanRetry2) + else if( winIoerrCanRetry2(e) ){ + sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); + ++*pnRetry; + return 1; + } +#endif + if( pError ){ + *pError = e; + } + return 0; +} + +/* +** Log a I/O error retry episode. +*/ +static void winLogIoerr(int nRetry, int lineno){ + if( nRetry ){ + sqlite3_log(SQLITE_NOTICE, + "delayed %dms for lock/sharing conflict at line %d", + winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno + ); + } +} + +/* +** This #if does not rely on the SQLITE_OS_WINCE define because the +** corresponding section in "date.c" cannot use it. +*/ +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ + (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) +/* +** The MSVC CRT on Windows CE may not have a localtime() function. +** So define a substitute. +*/ +/* # include */ +struct tm *__cdecl localtime(const time_t *t) +{ + static struct tm y; + FILETIME uTm, lTm; + SYSTEMTIME pTm; + sqlite3_int64 t64; + t64 = *t; + t64 = (t64 + 11644473600)*10000000; + uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF); + uTm.dwHighDateTime= (DWORD)(t64 >> 32); + osFileTimeToLocalFileTime(&uTm,&lTm); + osFileTimeToSystemTime(&lTm,&pTm); + y.tm_year = pTm.wYear - 1900; + y.tm_mon = pTm.wMonth - 1; + y.tm_wday = pTm.wDayOfWeek; + y.tm_mday = pTm.wDay; + y.tm_hour = pTm.wHour; + y.tm_min = pTm.wMinute; + y.tm_sec = pTm.wSecond; + return &y; +} +#endif + +#if SQLITE_OS_WINCE +/************************************************************************* +** This section contains code for WinCE only. +*/ +#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)] + +/* +** Acquire a lock on the handle h +*/ +static void winceMutexAcquire(HANDLE h){ + DWORD dwErr; + do { + dwErr = osWaitForSingleObject(h, INFINITE); + } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); +} +/* +** Release a lock acquired by winceMutexAcquire() +*/ +#define winceMutexRelease(h) ReleaseMutex(h) + +/* +** Create the mutex and shared memory used for locking in the file +** descriptor pFile +*/ +static int winceCreateLock(const char *zFilename, winFile *pFile){ + LPWSTR zTok; + LPWSTR zName; + DWORD lastErrno; + BOOL bLogged = FALSE; + BOOL bInit = TRUE; + + zName = winUtf8ToUnicode(zFilename); + if( zName==0 ){ + /* out of memory */ + return SQLITE_IOERR_NOMEM_BKPT; + } + + /* Initialize the local lockdata */ + memset(&pFile->local, 0, sizeof(pFile->local)); + + /* Replace the backslashes from the filename and lowercase it + ** to derive a mutex name. */ + zTok = osCharLowerW(zName); + for (;*zTok;zTok++){ + if (*zTok == '\\') *zTok = '_'; + } + + /* Create/open the named mutex */ + pFile->hMutex = osCreateMutexW(NULL, FALSE, zName); + if (!pFile->hMutex){ + pFile->lastErrno = osGetLastError(); + sqlite3_free(zName); + return winLogError(SQLITE_IOERR, pFile->lastErrno, + "winceCreateLock1", zFilename); + } + + /* Acquire the mutex before continuing */ + winceMutexAcquire(pFile->hMutex); + + /* Since the names of named mutexes, semaphores, file mappings etc are + ** case-sensitive, take advantage of that by uppercasing the mutex name + ** and using that as the shared filemapping name. + */ + osCharUpperW(zName); + pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, 0, sizeof(winceLock), + zName); + + /* Set a flag that indicates we're the first to create the memory so it + ** must be zero-initialized */ + lastErrno = osGetLastError(); + if (lastErrno == ERROR_ALREADY_EXISTS){ + bInit = FALSE; + } + + sqlite3_free(zName); + + /* If we succeeded in making the shared memory handle, map it. */ + if( pFile->hShared ){ + pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, + FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); + /* If mapping failed, close the shared memory handle and erase it */ + if( !pFile->shared ){ + pFile->lastErrno = osGetLastError(); + winLogError(SQLITE_IOERR, pFile->lastErrno, + "winceCreateLock2", zFilename); + bLogged = TRUE; + osCloseHandle(pFile->hShared); + pFile->hShared = NULL; + } + } + + /* If shared memory could not be created, then close the mutex and fail */ + if( pFile->hShared==NULL ){ + if( !bLogged ){ + pFile->lastErrno = lastErrno; + winLogError(SQLITE_IOERR, pFile->lastErrno, + "winceCreateLock3", zFilename); + bLogged = TRUE; + } + winceMutexRelease(pFile->hMutex); + osCloseHandle(pFile->hMutex); + pFile->hMutex = NULL; + return SQLITE_IOERR; + } + + /* Initialize the shared memory if we're supposed to */ + if( bInit ){ + memset(pFile->shared, 0, sizeof(winceLock)); + } + + winceMutexRelease(pFile->hMutex); + return SQLITE_OK; +} + +/* +** Destroy the part of winFile that deals with wince locks +*/ +static void winceDestroyLock(winFile *pFile){ + if (pFile->hMutex){ + /* Acquire the mutex */ + winceMutexAcquire(pFile->hMutex); + + /* The following blocks should probably assert in debug mode, but they + are to cleanup in case any locks remained open */ + if (pFile->local.nReaders){ + pFile->shared->nReaders --; + } + if (pFile->local.bReserved){ + pFile->shared->bReserved = FALSE; + } + if (pFile->local.bPending){ + pFile->shared->bPending = FALSE; + } + if (pFile->local.bExclusive){ + pFile->shared->bExclusive = FALSE; + } + + /* De-reference and close our copy of the shared memory handle */ + osUnmapViewOfFile(pFile->shared); + osCloseHandle(pFile->hShared); + + /* Done with the mutex */ + winceMutexRelease(pFile->hMutex); + osCloseHandle(pFile->hMutex); + pFile->hMutex = NULL; + } +} + +/* +** An implementation of the LockFile() API of Windows for CE +*/ +static BOOL winceLockFile( + LPHANDLE phFile, + DWORD dwFileOffsetLow, + DWORD dwFileOffsetHigh, + DWORD nNumberOfBytesToLockLow, + DWORD nNumberOfBytesToLockHigh +){ + winFile *pFile = HANDLE_TO_WINFILE(phFile); + BOOL bReturn = FALSE; + + UNUSED_PARAMETER(dwFileOffsetHigh); + UNUSED_PARAMETER(nNumberOfBytesToLockHigh); + + if (!pFile->hMutex) return TRUE; + winceMutexAcquire(pFile->hMutex); + + /* Wanting an exclusive lock? */ + if (dwFileOffsetLow == (DWORD)SHARED_FIRST + && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){ + if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ + pFile->shared->bExclusive = TRUE; + pFile->local.bExclusive = TRUE; + bReturn = TRUE; + } + } + + /* Want a read-only lock? */ + else if (dwFileOffsetLow == (DWORD)SHARED_FIRST && + nNumberOfBytesToLockLow == 1){ + if (pFile->shared->bExclusive == 0){ + pFile->local.nReaders ++; + if (pFile->local.nReaders == 1){ + pFile->shared->nReaders ++; + } + bReturn = TRUE; + } + } + + /* Want a pending lock? */ + else if (dwFileOffsetLow == (DWORD)PENDING_BYTE + && nNumberOfBytesToLockLow == 1){ + /* If no pending lock has been acquired, then acquire it */ + if (pFile->shared->bPending == 0) { + pFile->shared->bPending = TRUE; + pFile->local.bPending = TRUE; + bReturn = TRUE; + } + } + + /* Want a reserved lock? */ + else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE + && nNumberOfBytesToLockLow == 1){ + if (pFile->shared->bReserved == 0) { + pFile->shared->bReserved = TRUE; + pFile->local.bReserved = TRUE; + bReturn = TRUE; + } + } + + winceMutexRelease(pFile->hMutex); + return bReturn; +} + +/* +** An implementation of the UnlockFile API of Windows for CE +*/ +static BOOL winceUnlockFile( + LPHANDLE phFile, + DWORD dwFileOffsetLow, + DWORD dwFileOffsetHigh, + DWORD nNumberOfBytesToUnlockLow, + DWORD nNumberOfBytesToUnlockHigh +){ + winFile *pFile = HANDLE_TO_WINFILE(phFile); + BOOL bReturn = FALSE; + + UNUSED_PARAMETER(dwFileOffsetHigh); + UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh); + + if (!pFile->hMutex) return TRUE; + winceMutexAcquire(pFile->hMutex); + + /* Releasing a reader lock or an exclusive lock */ + if (dwFileOffsetLow == (DWORD)SHARED_FIRST){ + /* Did we have an exclusive lock? */ + if (pFile->local.bExclusive){ + assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE); + pFile->local.bExclusive = FALSE; + pFile->shared->bExclusive = FALSE; + bReturn = TRUE; + } + + /* Did we just have a reader lock? */ + else if (pFile->local.nReaders){ + assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE + || nNumberOfBytesToUnlockLow == 1); + pFile->local.nReaders --; + if (pFile->local.nReaders == 0) + { + pFile->shared->nReaders --; + } + bReturn = TRUE; + } + } + + /* Releasing a pending lock */ + else if (dwFileOffsetLow == (DWORD)PENDING_BYTE + && nNumberOfBytesToUnlockLow == 1){ + if (pFile->local.bPending){ + pFile->local.bPending = FALSE; + pFile->shared->bPending = FALSE; + bReturn = TRUE; + } + } + /* Releasing a reserved lock */ + else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE + && nNumberOfBytesToUnlockLow == 1){ + if (pFile->local.bReserved) { + pFile->local.bReserved = FALSE; + pFile->shared->bReserved = FALSE; + bReturn = TRUE; + } + } + + winceMutexRelease(pFile->hMutex); + return bReturn; +} +/* +** End of the special code for wince +*****************************************************************************/ +#endif /* SQLITE_OS_WINCE */ + +/* +** Lock a file region. +*/ +static BOOL winLockFile( + LPHANDLE phFile, + DWORD flags, + DWORD offsetLow, + DWORD offsetHigh, + DWORD numBytesLow, + DWORD numBytesHigh +){ +#if SQLITE_OS_WINCE + /* + ** NOTE: Windows CE is handled differently here due its lack of the Win32 + ** API LockFile. + */ + return winceLockFile(phFile, offsetLow, offsetHigh, + numBytesLow, numBytesHigh); +#else + if( osIsNT() ){ + OVERLAPPED ovlp; + memset(&ovlp, 0, sizeof(OVERLAPPED)); + ovlp.Offset = offsetLow; + ovlp.OffsetHigh = offsetHigh; + return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp); + }else{ + return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow, + numBytesHigh); + } +#endif +} + +/* +** Unlock a file region. + */ +static BOOL winUnlockFile( + LPHANDLE phFile, + DWORD offsetLow, + DWORD offsetHigh, + DWORD numBytesLow, + DWORD numBytesHigh +){ +#if SQLITE_OS_WINCE + /* + ** NOTE: Windows CE is handled differently here due its lack of the Win32 + ** API UnlockFile. + */ + return winceUnlockFile(phFile, offsetLow, offsetHigh, + numBytesLow, numBytesHigh); +#else + if( osIsNT() ){ + OVERLAPPED ovlp; + memset(&ovlp, 0, sizeof(OVERLAPPED)); + ovlp.Offset = offsetLow; + ovlp.OffsetHigh = offsetHigh; + return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp); + }else{ + return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow, + numBytesHigh); + } +#endif +} + +/***************************************************************************** +** The next group of routines implement the I/O methods specified +** by the sqlite3_io_methods object. +******************************************************************************/ + +/* +** Some Microsoft compilers lack this definition. +*/ +#ifndef INVALID_SET_FILE_POINTER +# define INVALID_SET_FILE_POINTER ((DWORD)-1) +#endif + +/* +** Move the current position of the file handle passed as the first +** argument to offset iOffset within the file. If successful, return 0. +** Otherwise, set pFile->lastErrno and return non-zero. +*/ +static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ +#if !SQLITE_OS_WINRT + LONG upperBits; /* Most sig. 32 bits of new offset */ + LONG lowerBits; /* Least sig. 32 bits of new offset */ + DWORD dwRet; /* Value returned by SetFilePointer() */ + DWORD lastErrno; /* Value returned by GetLastError() */ + + OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset)); + + upperBits = (LONG)((iOffset>>32) & 0x7fffffff); + lowerBits = (LONG)(iOffset & 0xffffffff); + + /* API oddity: If successful, SetFilePointer() returns a dword + ** containing the lower 32-bits of the new file-offset. Or, if it fails, + ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, + ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine + ** whether an error has actually occurred, it is also necessary to call + ** GetLastError(). + */ + dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); + + if( (dwRet==INVALID_SET_FILE_POINTER + && ((lastErrno = osGetLastError())!=NO_ERROR)) ){ + pFile->lastErrno = lastErrno; + winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, + "winSeekFile", pFile->zPath); + OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); + return 1; + } + + OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); + return 0; +#else + /* + ** Same as above, except that this implementation works for WinRT. + */ + + LARGE_INTEGER x; /* The new offset */ + BOOL bRet; /* Value returned by SetFilePointerEx() */ + + x.QuadPart = iOffset; + bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN); + + if(!bRet){ + pFile->lastErrno = osGetLastError(); + winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, + "winSeekFile", pFile->zPath); + OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); + return 1; + } + + OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); + return 0; +#endif +} + +#if SQLITE_MAX_MMAP_SIZE>0 +/* Forward references to VFS helper methods used for memory mapped files */ +static int winMapfile(winFile*, sqlite3_int64); +static int winUnmapfile(winFile*); +#endif + +/* +** Close a file. +** +** It is reported that an attempt to close a handle might sometimes +** fail. This is a very unreasonable result, but Windows is notorious +** for being unreasonable so I do not doubt that it might happen. If +** the close fails, we pause for 100 milliseconds and try again. As +** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before +** giving up and returning an error. +*/ +#define MX_CLOSE_ATTEMPT 3 +static int winClose(sqlite3_file *id){ + int rc, cnt = 0; + winFile *pFile = (winFile*)id; + + assert( id!=0 ); +#ifndef SQLITE_OMIT_WAL + assert( pFile->pShm==0 ); +#endif + assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE ); + OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n", + osGetCurrentProcessId(), pFile, pFile->h)); + +#if SQLITE_MAX_MMAP_SIZE>0 + winUnmapfile(pFile); +#endif + + do{ + rc = osCloseHandle(pFile->h); + /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */ + }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); +#if SQLITE_OS_WINCE +#define WINCE_DELETION_ATTEMPTS 3 + winceDestroyLock(pFile); + if( pFile->zDeleteOnClose ){ + int cnt = 0; + while( + osDeleteFileW(pFile->zDeleteOnClose)==0 + && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff + && cnt++ < WINCE_DELETION_ATTEMPTS + ){ + sqlite3_win32_sleep(100); /* Wait a little before trying again */ + } + sqlite3_free(pFile->zDeleteOnClose); + } +#endif + if( rc ){ + pFile->h = NULL; + } + OpenCounter(-1); + OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n", + osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed")); + return rc ? SQLITE_OK + : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(), + "winClose", pFile->zPath); +} + +/* +** Read data from a file into a buffer. Return SQLITE_OK if all +** bytes were read successfully and SQLITE_IOERR if anything goes +** wrong. +*/ +static int winRead( + sqlite3_file *id, /* File to read from */ + void *pBuf, /* Write content into this buffer */ + int amt, /* Number of bytes to read */ + sqlite3_int64 offset /* Begin reading at this offset */ +){ +#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) + OVERLAPPED overlapped; /* The offset for ReadFile. */ +#endif + winFile *pFile = (winFile*)id; /* file handle */ + DWORD nRead; /* Number of bytes actually read from file */ + int nRetry = 0; /* Number of retrys */ + + assert( id!=0 ); + assert( amt>0 ); + assert( offset>=0 ); + SimulateIOError(return SQLITE_IOERR_READ); + OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " + "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, + pFile->h, pBuf, amt, offset, pFile->locktype)); + +#if SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this read request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offsetmmapSize ){ + if( offset+amt <= pFile->mmapSize ){ + memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); + OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; + }else{ + int nCopy = (int)(pFile->mmapSize - offset); + memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); + pBuf = &((u8 *)pBuf)[nCopy]; + amt -= nCopy; + offset += nCopy; + } + } +#endif + +#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) + if( winSeekFile(pFile, offset) ){ + OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_FULL; + } + while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ +#else + memset(&overlapped, 0, sizeof(OVERLAPPED)); + overlapped.Offset = (LONG)(offset & 0xffffffff); + overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); + while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) && + osGetLastError()!=ERROR_HANDLE_EOF ){ +#endif + DWORD lastErrno; + if( winRetryIoerr(&nRetry, &lastErrno) ) continue; + pFile->lastErrno = lastErrno; + OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return winLogError(SQLITE_IOERR_READ, pFile->lastErrno, + "winRead", pFile->zPath); + } + winLogIoerr(nRetry, __LINE__); + if( nRead<(DWORD)amt ){ + /* Unread parts of the buffer must be zero-filled */ + memset(&((char*)pBuf)[nRead], 0, amt-nRead); + OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_IOERR_SHORT_READ; + } + + OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; +} + +/* +** Write data from a buffer into a file. Return SQLITE_OK on success +** or some other error code on failure. +*/ +static int winWrite( + sqlite3_file *id, /* File to write into */ + const void *pBuf, /* The bytes to be written */ + int amt, /* Number of bytes to write */ + sqlite3_int64 offset /* Offset into the file to begin writing at */ +){ + int rc = 0; /* True if error has occurred, else false */ + winFile *pFile = (winFile*)id; /* File handle */ + int nRetry = 0; /* Number of retries */ + + assert( amt>0 ); + assert( pFile ); + SimulateIOError(return SQLITE_IOERR_WRITE); + SimulateDiskfullError(return SQLITE_FULL); + + OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " + "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, + pFile->h, pBuf, amt, offset, pFile->locktype)); + +#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this write request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offsetmmapSize ){ + if( offset+amt <= pFile->mmapSize ){ + memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); + OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; + }else{ + int nCopy = (int)(pFile->mmapSize - offset); + memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); + pBuf = &((u8 *)pBuf)[nCopy]; + amt -= nCopy; + offset += nCopy; + } + } +#endif + +#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) + rc = winSeekFile(pFile, offset); + if( rc==0 ){ +#else + { +#endif +#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) + OVERLAPPED overlapped; /* The offset for WriteFile. */ +#endif + u8 *aRem = (u8 *)pBuf; /* Data yet to be written */ + int nRem = amt; /* Number of bytes yet to be written */ + DWORD nWrite; /* Bytes written by each WriteFile() call */ + DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */ + +#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) + memset(&overlapped, 0, sizeof(OVERLAPPED)); + overlapped.Offset = (LONG)(offset & 0xffffffff); + overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); +#endif + + while( nRem>0 ){ +#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) + if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){ +#else + if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){ +#endif + if( winRetryIoerr(&nRetry, &lastErrno) ) continue; + break; + } + assert( nWrite==0 || nWrite<=(DWORD)nRem ); + if( nWrite==0 || nWrite>(DWORD)nRem ){ + lastErrno = osGetLastError(); + break; + } +#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) + offset += nWrite; + overlapped.Offset = (LONG)(offset & 0xffffffff); + overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); +#endif + aRem += nWrite; + nRem -= nWrite; + } + if( nRem>0 ){ + pFile->lastErrno = lastErrno; + rc = 1; + } + } + + if( rc ){ + if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ) + || ( pFile->lastErrno==ERROR_DISK_FULL )){ + OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return winLogError(SQLITE_FULL, pFile->lastErrno, + "winWrite1", pFile->zPath); + } + OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno, + "winWrite2", pFile->zPath); + }else{ + winLogIoerr(nRetry, __LINE__); + } + OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; +} + +/* +** Truncate an open file to a specified size +*/ +static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ + winFile *pFile = (winFile*)id; /* File handle object */ + int rc = SQLITE_OK; /* Return code for this function */ + DWORD lastErrno; + + assert( pFile ); + SimulateIOError(return SQLITE_IOERR_TRUNCATE); + OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n", + osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype)); + + /* If the user has configured a chunk-size for this file, truncate the + ** file so that it consists of an integer number of chunks (i.e. the + ** actual file size after the operation may be larger than the requested + ** size). + */ + if( pFile->szChunk>0 ){ + nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; + } + + /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ + if( winSeekFile(pFile, nByte) ){ + rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, + "winTruncate1", pFile->zPath); + }else if( 0==osSetEndOfFile(pFile->h) && + ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){ + pFile->lastErrno = lastErrno; + rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, + "winTruncate2", pFile->zPath); + } + +#if SQLITE_MAX_MMAP_SIZE>0 + /* If the file was truncated to a size smaller than the currently + ** mapped region, reduce the effective mapping size as well. SQLite will + ** use read() and write() to access data beyond this point from now on. + */ + if( pFile->pMapRegion && nBytemmapSize ){ + pFile->mmapSize = nByte; + } +#endif + + OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n", + osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc))); + return rc; +} + +#ifdef SQLITE_TEST +/* +** Count the number of fullsyncs and normal syncs. This is used to test +** that syncs and fullsyncs are occuring at the right times. +*/ +SQLITE_API int sqlite3_sync_count = 0; +SQLITE_API int sqlite3_fullsync_count = 0; +#endif + +/* +** Make sure all writes to a particular file are committed to disk. +*/ +static int winSync(sqlite3_file *id, int flags){ +#ifndef SQLITE_NO_SYNC + /* + ** Used only when SQLITE_NO_SYNC is not defined. + */ + BOOL rc; +#endif +#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \ + defined(SQLITE_HAVE_OS_TRACE) + /* + ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or + ** OSTRACE() macros. + */ + winFile *pFile = (winFile*)id; +#else + UNUSED_PARAMETER(id); +#endif + + assert( pFile ); + /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ + assert((flags&0x0F)==SQLITE_SYNC_NORMAL + || (flags&0x0F)==SQLITE_SYNC_FULL + ); + + /* Unix cannot, but some systems may return SQLITE_FULL from here. This + ** line is to test that doing so does not cause any problems. + */ + SimulateDiskfullError( return SQLITE_FULL ); + + OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n", + osGetCurrentProcessId(), pFile, pFile->h, flags, + pFile->locktype)); + +#ifndef SQLITE_TEST + UNUSED_PARAMETER(flags); +#else + if( (flags&0x0F)==SQLITE_SYNC_FULL ){ + sqlite3_fullsync_count++; + } + sqlite3_sync_count++; +#endif + + /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a + ** no-op + */ +#ifdef SQLITE_NO_SYNC + OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; +#else +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFile->pMapRegion ){ + if( osFlushViewOfFile(pFile->pMapRegion, 0) ){ + OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " + "rc=SQLITE_OK\n", osGetCurrentProcessId(), + pFile, pFile->pMapRegion)); + }else{ + pFile->lastErrno = osGetLastError(); + OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " + "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), + pFile, pFile->pMapRegion)); + return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, + "winSync1", pFile->zPath); + } + } +#endif + rc = osFlushFileBuffers(pFile->h); + SimulateIOError( rc=FALSE ); + if( rc ){ + OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return SQLITE_OK; + }else{ + pFile->lastErrno = osGetLastError(); + OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n", + osGetCurrentProcessId(), pFile, pFile->h)); + return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno, + "winSync2", pFile->zPath); + } +#endif +} + +/* +** Determine the current size of a file in bytes +*/ +static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ + winFile *pFile = (winFile*)id; + int rc = SQLITE_OK; + + assert( id!=0 ); + assert( pSize!=0 ); + SimulateIOError(return SQLITE_IOERR_FSTAT); + OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize)); + +#if SQLITE_OS_WINRT + { + FILE_STANDARD_INFO info; + if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo, + &info, sizeof(info)) ){ + *pSize = info.EndOfFile.QuadPart; + }else{ + pFile->lastErrno = osGetLastError(); + rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, + "winFileSize", pFile->zPath); + } + } +#else + { + DWORD upperBits; + DWORD lowerBits; + DWORD lastErrno; + + lowerBits = osGetFileSize(pFile->h, &upperBits); + *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; + if( (lowerBits == INVALID_FILE_SIZE) + && ((lastErrno = osGetLastError())!=NO_ERROR) ){ + pFile->lastErrno = lastErrno; + rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, + "winFileSize", pFile->zPath); + } + } +#endif + OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n", + pFile->h, pSize, *pSize, sqlite3ErrName(rc))); + return rc; +} + +/* +** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. +*/ +#ifndef LOCKFILE_FAIL_IMMEDIATELY +# define LOCKFILE_FAIL_IMMEDIATELY 1 +#endif + +#ifndef LOCKFILE_EXCLUSIVE_LOCK +# define LOCKFILE_EXCLUSIVE_LOCK 2 +#endif + +/* +** Historically, SQLite has used both the LockFile and LockFileEx functions. +** When the LockFile function was used, it was always expected to fail +** immediately if the lock could not be obtained. Also, it always expected to +** obtain an exclusive lock. These flags are used with the LockFileEx function +** and reflect those expectations; therefore, they should not be changed. +*/ +#ifndef SQLITE_LOCKFILE_FLAGS +# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \ + LOCKFILE_EXCLUSIVE_LOCK) +#endif + +/* +** Currently, SQLite never calls the LockFileEx function without wanting the +** call to fail immediately if the lock cannot be obtained. +*/ +#ifndef SQLITE_LOCKFILEEX_FLAGS +# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY) +#endif + +/* +** Acquire a reader lock. +** Different API routines are called depending on whether or not this +** is Win9x or WinNT. +*/ +static int winGetReadLock(winFile *pFile){ + int res; + OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); + if( osIsNT() ){ +#if SQLITE_OS_WINCE + /* + ** NOTE: Windows CE is handled differently here due its lack of the Win32 + ** API LockFileEx. + */ + res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0); +#else + res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0, + SHARED_SIZE, 0); +#endif + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + int lk; + sqlite3_randomness(sizeof(lk), &lk); + pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1)); + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, + SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); + } +#endif + if( res == 0 ){ + pFile->lastErrno = osGetLastError(); + /* No need to log a failure to lock */ + } + OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res)); + return res; +} + +/* +** Undo a readlock +*/ +static int winUnlockReadLock(winFile *pFile){ + int res; + DWORD lastErrno; + OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); + if( osIsNT() ){ + res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); + } +#endif + if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){ + pFile->lastErrno = lastErrno; + winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno, + "winUnlockReadLock", pFile->zPath); + } + OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res)); + return res; +} + +/* +** Lock the file with the lock specified by parameter locktype - one +** of the following: +** +** (1) SHARED_LOCK +** (2) RESERVED_LOCK +** (3) PENDING_LOCK +** (4) EXCLUSIVE_LOCK +** +** Sometimes when requesting one lock state, additional lock states +** are inserted in between. The locking might fail on one of the later +** transitions leaving the lock state different from what it started but +** still short of its goal. The following chart shows the allowed +** transitions and the inserted intermediate states: +** +** UNLOCKED -> SHARED +** SHARED -> RESERVED +** SHARED -> (PENDING) -> EXCLUSIVE +** RESERVED -> (PENDING) -> EXCLUSIVE +** PENDING -> EXCLUSIVE +** +** This routine will only increase a lock. The winUnlock() routine +** erases all locks at once and returns us immediately to locking level 0. +** It is not possible to lower the locking level one step at a time. You +** must go straight to locking level 0. +*/ +static int winLock(sqlite3_file *id, int locktype){ + int rc = SQLITE_OK; /* Return code from subroutines */ + int res = 1; /* Result of a Windows lock call */ + int newLocktype; /* Set pFile->locktype to this value before exiting */ + int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ + winFile *pFile = (winFile*)id; + DWORD lastErrno = NO_ERROR; + + assert( id!=0 ); + OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n", + pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); + + /* If there is already a lock of this type or more restrictive on the + ** OsFile, do nothing. Don't use the end_lock: exit path, as + ** sqlite3OsEnterMutex() hasn't been called yet. + */ + if( pFile->locktype>=locktype ){ + OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + + /* Do not allow any kind of write-lock on a read-only database + */ + if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){ + return SQLITE_IOERR_LOCK; + } + + /* Make sure the locking sequence is correct + */ + assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); + assert( locktype!=PENDING_LOCK ); + assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); + + /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or + ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of + ** the PENDING_LOCK byte is temporary. + */ + newLocktype = pFile->locktype; + if( pFile->locktype==NO_LOCK + || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK) + ){ + int cnt = 3; + while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, + PENDING_BYTE, 0, 1, 0))==0 ){ + /* Try 3 times to get the pending lock. This is needed to work + ** around problems caused by indexing and/or anti-virus software on + ** Windows systems. + ** If you are using this code as a model for alternative VFSes, do not + ** copy this retry logic. It is a hack intended for Windows only. + */ + lastErrno = osGetLastError(); + OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", + pFile->h, cnt, res)); + if( lastErrno==ERROR_INVALID_HANDLE ){ + pFile->lastErrno = lastErrno; + rc = SQLITE_IOERR_LOCK; + OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", + pFile->h, cnt, sqlite3ErrName(rc))); + return rc; + } + if( cnt ) sqlite3_win32_sleep(1); + } + gotPendingLock = res; + if( !res ){ + lastErrno = osGetLastError(); + } + } + + /* Acquire a shared lock + */ + if( locktype==SHARED_LOCK && res ){ + assert( pFile->locktype==NO_LOCK ); + res = winGetReadLock(pFile); + if( res ){ + newLocktype = SHARED_LOCK; + }else{ + lastErrno = osGetLastError(); + } + } + + /* Acquire a RESERVED lock + */ + if( locktype==RESERVED_LOCK && res ){ + assert( pFile->locktype==SHARED_LOCK ); + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0); + if( res ){ + newLocktype = RESERVED_LOCK; + }else{ + lastErrno = osGetLastError(); + } + } + + /* Acquire a PENDING lock + */ + if( locktype==EXCLUSIVE_LOCK && res ){ + newLocktype = PENDING_LOCK; + gotPendingLock = 0; + } + + /* Acquire an EXCLUSIVE lock + */ + if( locktype==EXCLUSIVE_LOCK && res ){ + assert( pFile->locktype>=SHARED_LOCK ); + res = winUnlockReadLock(pFile); + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0, + SHARED_SIZE, 0); + if( res ){ + newLocktype = EXCLUSIVE_LOCK; + }else{ + lastErrno = osGetLastError(); + winGetReadLock(pFile); + } + } + + /* If we are holding a PENDING lock that ought to be released, then + ** release it now. + */ + if( gotPendingLock && locktype==SHARED_LOCK ){ + winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); + } + + /* Update the state of the lock has held in the file descriptor then + ** return the appropriate result code. + */ + if( res ){ + rc = SQLITE_OK; + }else{ + pFile->lastErrno = lastErrno; + rc = SQLITE_BUSY; + OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n", + pFile->h, locktype, newLocktype)); + } + pFile->locktype = (u8)newLocktype; + OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n", + pFile->h, pFile->locktype, sqlite3ErrName(rc))); + return rc; +} + +/* +** This routine checks if there is a RESERVED lock held on the specified +** file by this or any other process. If such a lock is held, return +** non-zero, otherwise zero. +*/ +static int winCheckReservedLock(sqlite3_file *id, int *pResOut){ + int res; + winFile *pFile = (winFile*)id; + + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut)); + + assert( id!=0 ); + if( pFile->locktype>=RESERVED_LOCK ){ + res = 1; + OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res)); + }else{ + res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0); + if( res ){ + winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); + } + res = !res; + OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res)); + } + *pResOut = res; + OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", + pFile->h, pResOut, *pResOut)); + return SQLITE_OK; +} + +/* +** Lower the locking level on file descriptor id to locktype. locktype +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +** +** It is not possible for this routine to fail if the second argument +** is NO_LOCK. If the second argument is SHARED_LOCK then this routine +** might return SQLITE_IOERR; +*/ +static int winUnlock(sqlite3_file *id, int locktype){ + int type; + winFile *pFile = (winFile*)id; + int rc = SQLITE_OK; + assert( pFile!=0 ); + assert( locktype<=SHARED_LOCK ); + OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n", + pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); + type = pFile->locktype; + if( type>=EXCLUSIVE_LOCK ){ + winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); + if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){ + /* This should never happen. We should always be able to + ** reacquire the read lock */ + rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(), + "winUnlock", pFile->zPath); + } + } + if( type>=RESERVED_LOCK ){ + winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); + } + if( locktype==NO_LOCK && type>=SHARED_LOCK ){ + winUnlockReadLock(pFile); + } + if( type>=PENDING_LOCK ){ + winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); + } + pFile->locktype = (u8)locktype; + OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n", + pFile->h, pFile->locktype, sqlite3ErrName(rc))); + return rc; +} + +/* +** If *pArg is initially negative then this is a query. Set *pArg to +** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. +** +** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. +*/ +static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){ + if( *pArg<0 ){ + *pArg = (pFile->ctrlFlags & mask)!=0; + }else if( (*pArg)==0 ){ + pFile->ctrlFlags &= ~mask; + }else{ + pFile->ctrlFlags |= mask; + } +} + +/* Forward references to VFS helper methods used for temporary files */ +static int winGetTempname(sqlite3_vfs *, char **); +static int winIsDir(const void *); +static BOOL winIsDriveLetterAndColon(const char *); + +/* +** Control and query of the open file handle. +*/ +static int winFileControl(sqlite3_file *id, int op, void *pArg){ + winFile *pFile = (winFile*)id; + OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg)); + switch( op ){ + case SQLITE_FCNTL_LOCKSTATE: { + *(int*)pArg = pFile->locktype; + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_LAST_ERRNO: { + *(int*)pArg = (int)pFile->lastErrno; + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_CHUNK_SIZE: { + pFile->szChunk = *(int *)pArg; + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_SIZE_HINT: { + if( pFile->szChunk>0 ){ + sqlite3_int64 oldSz; + int rc = winFileSize(id, &oldSz); + if( rc==SQLITE_OK ){ + sqlite3_int64 newSz = *(sqlite3_int64*)pArg; + if( newSz>oldSz ){ + SimulateIOErrorBenign(1); + rc = winTruncate(id, newSz); + SimulateIOErrorBenign(0); + } + } + OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); + return rc; + } + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_PERSIST_WAL: { + winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg); + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { + winModeBit(pFile, WINFILE_PSOW, (int*)pArg); + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_VFSNAME: { + *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } + case SQLITE_FCNTL_WIN32_AV_RETRY: { + int *a = (int*)pArg; + if( a[0]>0 ){ + winIoerrRetry = a[0]; + }else{ + a[0] = winIoerrRetry; + } + if( a[1]>0 ){ + winIoerrRetryDelay = a[1]; + }else{ + a[1] = winIoerrRetryDelay; + } + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } +#ifdef SQLITE_TEST + case SQLITE_FCNTL_WIN32_SET_HANDLE: { + LPHANDLE phFile = (LPHANDLE)pArg; + HANDLE hOldFile = pFile->h; + pFile->h = *phFile; + *phFile = hOldFile; + OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n", + hOldFile, pFile->h)); + return SQLITE_OK; + } +#endif + case SQLITE_FCNTL_TEMPFILENAME: { + char *zTFile = 0; + int rc = winGetTempname(pFile->pVfs, &zTFile); + if( rc==SQLITE_OK ){ + *(char**)pArg = zTFile; + } + OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); + return rc; + } +#if SQLITE_MAX_MMAP_SIZE>0 + case SQLITE_FCNTL_MMAP_SIZE: { + i64 newLimit = *(i64*)pArg; + int rc = SQLITE_OK; + if( newLimit>sqlite3GlobalConfig.mxMmap ){ + newLimit = sqlite3GlobalConfig.mxMmap; + } + *(i64*)pArg = pFile->mmapSizeMax; + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ + pFile->mmapSizeMax = newLimit; + if( pFile->mmapSize>0 ){ + winUnmapfile(pFile); + rc = winMapfile(pFile, -1); + } + } + OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); + return rc; + } +#endif + } + OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); + return SQLITE_NOTFOUND; +} + +/* +** Return the sector size in bytes of the underlying block device for +** the specified file. This is almost always 512 bytes, but may be +** larger for some devices. +** +** SQLite code assumes this function cannot fail. It also assumes that +** if two files are created in the same file-system directory (i.e. +** a database and its journal file) that the sector size will be the +** same for both. +*/ +static int winSectorSize(sqlite3_file *id){ + (void)id; + return SQLITE_DEFAULT_SECTOR_SIZE; +} + +/* +** Return a vector of device characteristics. +*/ +static int winDeviceCharacteristics(sqlite3_file *id){ + winFile *p = (winFile*)id; + return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | + ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); +} + +/* +** Windows will only let you create file view mappings +** on allocation size granularity boundaries. +** During sqlite3_os_init() we do a GetSystemInfo() +** to get the granularity size. +*/ +static SYSTEM_INFO winSysInfo; + +#ifndef SQLITE_OMIT_WAL + +/* +** Helper functions to obtain and relinquish the global mutex. The +** global mutex is used to protect the winLockInfo objects used by +** this file, all of which may be shared by multiple threads. +** +** Function winShmMutexHeld() is used to assert() that the global mutex +** is held when required. This function is only used as part of assert() +** statements. e.g. +** +** winShmEnterMutex() +** assert( winShmMutexHeld() ); +** winShmLeaveMutex() +*/ +static void winShmEnterMutex(void){ + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +static void winShmLeaveMutex(void){ + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +#ifndef NDEBUG +static int winShmMutexHeld(void) { + return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); +} +#endif + +/* +** Object used to represent a single file opened and mmapped to provide +** shared memory. When multiple threads all reference the same +** log-summary, each thread has its own winFile object, but they all +** point to a single instance of this object. In other words, each +** log-summary is opened only once per process. +** +** winShmMutexHeld() must be true when creating or destroying +** this object or while reading or writing the following fields: +** +** nRef +** pNext +** +** The following fields are read-only after the object is created: +** +** fid +** zFilename +** +** Either winShmNode.mutex must be held or winShmNode.nRef==0 and +** winShmMutexHeld() is true when reading or writing any other field +** in this structure. +** +*/ +struct winShmNode { + sqlite3_mutex *mutex; /* Mutex to access this object */ + char *zFilename; /* Name of the file */ + winFile hFile; /* File handle from winOpen */ + + int szRegion; /* Size of shared-memory regions */ + int nRegion; /* Size of array apRegion */ + struct ShmRegion { + HANDLE hMap; /* File handle from CreateFileMapping */ + void *pMap; + } *aRegion; + DWORD lastErrno; /* The Windows errno from the last I/O error */ + + int nRef; /* Number of winShm objects pointing to this */ + winShm *pFirst; /* All winShm objects pointing to this */ + winShmNode *pNext; /* Next in list of all winShmNode objects */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) + u8 nextShmId; /* Next available winShm.id value */ +#endif +}; + +/* +** A global array of all winShmNode objects. +** +** The winShmMutexHeld() must be true while reading or writing this list. +*/ +static winShmNode *winShmNodeList = 0; + +/* +** Structure used internally by this VFS to record the state of an +** open shared memory connection. +** +** The following fields are initialized when this object is created and +** are read-only thereafter: +** +** winShm.pShmNode +** winShm.id +** +** All other fields are read/write. The winShm.pShmNode->mutex must be held +** while accessing any read/write fields. +*/ +struct winShm { + winShmNode *pShmNode; /* The underlying winShmNode object */ + winShm *pNext; /* Next winShm with the same winShmNode */ + u8 hasMutex; /* True if holding the winShmNode mutex */ + u16 sharedMask; /* Mask of shared locks held */ + u16 exclMask; /* Mask of exclusive locks held */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) + u8 id; /* Id of this connection with its winShmNode */ +#endif +}; + +/* +** Constants used for locking +*/ +#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ +#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ + +/* +** Apply advisory locks for all n bytes beginning at ofst. +*/ +#define _SHM_UNLCK 1 +#define _SHM_RDLCK 2 +#define _SHM_WRLCK 3 +static int winShmSystemLock( + winShmNode *pFile, /* Apply locks to this open shared-memory segment */ + int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */ + int ofst, /* Offset to first byte to be locked/unlocked */ + int nByte /* Number of bytes to lock or unlock */ +){ + int rc = 0; /* Result code form Lock/UnlockFileEx() */ + + /* Access to the winShmNode object is serialized by the caller */ + assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 ); + + OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n", + pFile->hFile.h, lockType, ofst, nByte)); + + /* Release/Acquire the system-level lock */ + if( lockType==_SHM_UNLCK ){ + rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0); + }else{ + /* Initialize the locking parameters */ + DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; + if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; + rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0); + } + + if( rc!= 0 ){ + rc = SQLITE_OK; + }else{ + pFile->lastErrno = osGetLastError(); + rc = SQLITE_BUSY; + } + + OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n", + pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" : + "winLockFile", pFile->lastErrno, sqlite3ErrName(rc))); + + return rc; +} + +/* Forward references to VFS methods */ +static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*); +static int winDelete(sqlite3_vfs *,const char*,int); + +/* +** Purge the winShmNodeList list of all entries with winShmNode.nRef==0. +** +** This is not a VFS shared-memory method; it is a utility function called +** by VFS shared-memory methods. +*/ +static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ + winShmNode **pp; + winShmNode *p; + assert( winShmMutexHeld() ); + OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n", + osGetCurrentProcessId(), deleteFlag)); + pp = &winShmNodeList; + while( (p = *pp)!=0 ){ + if( p->nRef==0 ){ + int i; + if( p->mutex ){ sqlite3_mutex_free(p->mutex); } + for(i=0; inRegion; i++){ + BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap); + OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n", + osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); + UNUSED_VARIABLE_VALUE(bRc); + bRc = osCloseHandle(p->aRegion[i].hMap); + OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n", + osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); + UNUSED_VARIABLE_VALUE(bRc); + } + if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){ + SimulateIOErrorBenign(1); + winClose((sqlite3_file *)&p->hFile); + SimulateIOErrorBenign(0); + } + if( deleteFlag ){ + SimulateIOErrorBenign(1); + sqlite3BeginBenignMalloc(); + winDelete(pVfs, p->zFilename, 0); + sqlite3EndBenignMalloc(); + SimulateIOErrorBenign(0); + } + *pp = p->pNext; + sqlite3_free(p->aRegion); + sqlite3_free(p); + }else{ + pp = &p->pNext; + } + } +} + +/* +** Open the shared-memory area associated with database file pDbFd. +** +** When opening a new shared-memory file, if no other instances of that +** file are currently open, in this process or in other processes, then +** the file must be truncated to zero length or have its header cleared. +*/ +static int winOpenSharedMemory(winFile *pDbFd){ + struct winShm *p; /* The connection to be opened */ + struct winShmNode *pShmNode = 0; /* The underlying mmapped file */ + int rc; /* Result code */ + struct winShmNode *pNew; /* Newly allocated winShmNode */ + int nName; /* Size of zName in bytes */ + + assert( pDbFd->pShm==0 ); /* Not previously opened */ + + /* Allocate space for the new sqlite3_shm object. Also speculatively + ** allocate space for a new winShmNode and filename. + */ + p = sqlite3MallocZero( sizeof(*p) ); + if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT; + nName = sqlite3Strlen30(pDbFd->zPath); + pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 ); + if( pNew==0 ){ + sqlite3_free(p); + return SQLITE_IOERR_NOMEM_BKPT; + } + pNew->zFilename = (char*)&pNew[1]; + sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); + sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); + + /* Look to see if there is an existing winShmNode that can be used. + ** If no matching winShmNode currently exists, create a new one. + */ + winShmEnterMutex(); + for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){ + /* TBD need to come up with better match here. Perhaps + ** use FILE_ID_BOTH_DIR_INFO Structure. + */ + if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break; + } + if( pShmNode ){ + sqlite3_free(pNew); + }else{ + pShmNode = pNew; + pNew = 0; + ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE; + pShmNode->pNext = winShmNodeList; + winShmNodeList = pShmNode; + + if( sqlite3GlobalConfig.bCoreMutex ){ + pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pShmNode->mutex==0 ){ + rc = SQLITE_IOERR_NOMEM_BKPT; + goto shm_open_err; + } + } + + rc = winOpen(pDbFd->pVfs, + pShmNode->zFilename, /* Name of the file (UTF-8) */ + (sqlite3_file*)&pShmNode->hFile, /* File handle here */ + SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, + 0); + if( SQLITE_OK!=rc ){ + goto shm_open_err; + } + + /* Check to see if another process is holding the dead-man switch. + ** If not, truncate the file to zero length. + */ + if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){ + rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0); + if( rc!=SQLITE_OK ){ + rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), + "winOpenShm", pDbFd->zPath); + } + } + if( rc==SQLITE_OK ){ + winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); + rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1); + } + if( rc ) goto shm_open_err; + } + + /* Make the new connection a child of the winShmNode */ + p->pShmNode = pShmNode; +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) + p->id = pShmNode->nextShmId++; +#endif + pShmNode->nRef++; + pDbFd->pShm = p; + winShmLeaveMutex(); + + /* The reference count on pShmNode has already been incremented under + ** the cover of the winShmEnterMutex() mutex and the pointer from the + ** new (struct winShm) object to the pShmNode has been set. All that is + ** left to do is to link the new object into the linked list starting + ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex + ** mutex. + */ + sqlite3_mutex_enter(pShmNode->mutex); + p->pNext = pShmNode->pFirst; + pShmNode->pFirst = p; + sqlite3_mutex_leave(pShmNode->mutex); + return SQLITE_OK; + + /* Jump here on any error */ +shm_open_err: + winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); + winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */ + sqlite3_free(p); + sqlite3_free(pNew); + winShmLeaveMutex(); + return rc; +} + +/* +** Close a connection to shared-memory. Delete the underlying +** storage if deleteFlag is true. +*/ +static int winShmUnmap( + sqlite3_file *fd, /* Database holding shared memory */ + int deleteFlag /* Delete after closing if true */ +){ + winFile *pDbFd; /* Database holding shared-memory */ + winShm *p; /* The connection to be closed */ + winShmNode *pShmNode; /* The underlying shared-memory file */ + winShm **pp; /* For looping over sibling connections */ + + pDbFd = (winFile*)fd; + p = pDbFd->pShm; + if( p==0 ) return SQLITE_OK; + pShmNode = p->pShmNode; + + /* Remove connection p from the set of connections associated + ** with pShmNode */ + sqlite3_mutex_enter(pShmNode->mutex); + for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} + *pp = p->pNext; + + /* Free the connection p */ + sqlite3_free(p); + pDbFd->pShm = 0; + sqlite3_mutex_leave(pShmNode->mutex); + + /* If pShmNode->nRef has reached 0, then close the underlying + ** shared-memory file, too */ + winShmEnterMutex(); + assert( pShmNode->nRef>0 ); + pShmNode->nRef--; + if( pShmNode->nRef==0 ){ + winShmPurge(pDbFd->pVfs, deleteFlag); + } + winShmLeaveMutex(); + + return SQLITE_OK; +} + +/* +** Change the lock state for a shared-memory segment. +*/ +static int winShmLock( + sqlite3_file *fd, /* Database file holding the shared memory */ + int ofst, /* First lock to acquire or release */ + int n, /* Number of locks to acquire or release */ + int flags /* What to do with the lock */ +){ + winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ + winShm *p = pDbFd->pShm; /* The shared memory being locked */ + winShm *pX; /* For looping over all siblings */ + winShmNode *pShmNode = p->pShmNode; + int rc = SQLITE_OK; /* Result code */ + u16 mask; /* Mask of locks to take or release */ + + assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); + assert( n>=1 ); + assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); + assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); + + mask = (u16)((1U<<(ofst+n)) - (1U<1 || mask==(1<mutex); + if( flags & SQLITE_SHM_UNLOCK ){ + u16 allMask = 0; /* Mask of locks held by siblings */ + + /* See if any siblings hold this same lock */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( pX==p ) continue; + assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); + allMask |= pX->sharedMask; + } + + /* Unlock the system-level locks */ + if( (mask & allMask)==0 ){ + rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } + + /* Undo the local locks */ + if( rc==SQLITE_OK ){ + p->exclMask &= ~mask; + p->sharedMask &= ~mask; + } + }else if( flags & SQLITE_SHM_SHARED ){ + u16 allShared = 0; /* Union of locks held by connections other than "p" */ + + /* Find out which shared locks are already held by sibling connections. + ** If any sibling already holds an exclusive lock, go ahead and return + ** SQLITE_BUSY. + */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( (pX->exclMask & mask)!=0 ){ + rc = SQLITE_BUSY; + break; + } + allShared |= pX->sharedMask; + } + + /* Get shared locks at the system level, if necessary */ + if( rc==SQLITE_OK ){ + if( (allShared & mask)==0 ){ + rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } + } + + /* Get the local shared locks */ + if( rc==SQLITE_OK ){ + p->sharedMask |= mask; + } + }else{ + /* Make sure no sibling connections hold locks that will block this + ** lock. If any do, return SQLITE_BUSY right away. + */ + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ + rc = SQLITE_BUSY; + break; + } + } + + /* Get the exclusive locks at the system level. Then if successful + ** also mark the local connection as being locked. + */ + if( rc==SQLITE_OK ){ + rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n); + if( rc==SQLITE_OK ){ + assert( (p->sharedMask & mask)==0 ); + p->exclMask |= mask; + } + } + } + sqlite3_mutex_leave(pShmNode->mutex); + OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n", + osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask, + sqlite3ErrName(rc))); + return rc; +} + +/* +** Implement a memory barrier or memory fence on shared memory. +** +** All loads and stores begun before the barrier must complete before +** any load or store begun after the barrier. +*/ +static void winShmBarrier( + sqlite3_file *fd /* Database holding the shared memory */ +){ + UNUSED_PARAMETER(fd); + sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ + winShmEnterMutex(); /* Also mutex, for redundancy */ + winShmLeaveMutex(); +} + +/* +** This function is called to obtain a pointer to region iRegion of the +** shared-memory associated with the database file fd. Shared-memory regions +** are numbered starting from zero. Each shared-memory region is szRegion +** bytes in size. +** +** If an error occurs, an error code is returned and *pp is set to NULL. +** +** Otherwise, if the isWrite parameter is 0 and the requested shared-memory +** region has not been allocated (by any client, including one running in a +** separate process), then *pp is set to NULL and SQLITE_OK returned. If +** isWrite is non-zero and the requested shared-memory region has not yet +** been allocated, it is allocated by this function. +** +** If the shared-memory region has already been allocated or is allocated by +** this call as described above, then it is mapped into this processes +** address space (if it is not already), *pp is set to point to the mapped +** memory and SQLITE_OK returned. +*/ +static int winShmMap( + sqlite3_file *fd, /* Handle open on database file */ + int iRegion, /* Region to retrieve */ + int szRegion, /* Size of regions */ + int isWrite, /* True to extend file if necessary */ + void volatile **pp /* OUT: Mapped memory */ +){ + winFile *pDbFd = (winFile*)fd; + winShm *pShm = pDbFd->pShm; + winShmNode *pShmNode; + int rc = SQLITE_OK; + + if( !pShm ){ + rc = winOpenSharedMemory(pDbFd); + if( rc!=SQLITE_OK ) return rc; + pShm = pDbFd->pShm; + } + pShmNode = pShm->pShmNode; + + sqlite3_mutex_enter(pShmNode->mutex); + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); + + if( pShmNode->nRegion<=iRegion ){ + struct ShmRegion *apNew; /* New aRegion[] array */ + int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ + sqlite3_int64 sz; /* Current size of wal-index file */ + + pShmNode->szRegion = szRegion; + + /* The requested region is not mapped into this processes address space. + ** Check to see if it has been allocated (i.e. if the wal-index file is + ** large enough to contain the requested region). + */ + rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz); + if( rc!=SQLITE_OK ){ + rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), + "winShmMap1", pDbFd->zPath); + goto shmpage_out; + } + + if( szhFile, nByte); + if( rc!=SQLITE_OK ){ + rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), + "winShmMap2", pDbFd->zPath); + goto shmpage_out; + } + } + + /* Map the requested memory region into this processes address space. */ + apNew = (struct ShmRegion *)sqlite3_realloc64( + pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) + ); + if( !apNew ){ + rc = SQLITE_IOERR_NOMEM_BKPT; + goto shmpage_out; + } + pShmNode->aRegion = apNew; + + while( pShmNode->nRegion<=iRegion ){ + HANDLE hMap = NULL; /* file-mapping handle */ + void *pMap = 0; /* Mapped memory region */ + +#if SQLITE_OS_WINRT + hMap = osCreateFileMappingFromApp(pShmNode->hFile.h, + NULL, PAGE_READWRITE, nByte, NULL + ); +#elif defined(SQLITE_WIN32_HAS_WIDE) + hMap = osCreateFileMappingW(pShmNode->hFile.h, + NULL, PAGE_READWRITE, 0, nByte, NULL + ); +#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA + hMap = osCreateFileMappingA(pShmNode->hFile.h, + NULL, PAGE_READWRITE, 0, nByte, NULL + ); +#endif + OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n", + osGetCurrentProcessId(), pShmNode->nRegion, nByte, + hMap ? "ok" : "failed")); + if( hMap ){ + int iOffset = pShmNode->nRegion*szRegion; + int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; +#if SQLITE_OS_WINRT + pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ, + iOffset - iOffsetShift, szRegion + iOffsetShift + ); +#else + pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, + 0, iOffset - iOffsetShift, szRegion + iOffsetShift + ); +#endif + OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n", + osGetCurrentProcessId(), pShmNode->nRegion, iOffset, + szRegion, pMap ? "ok" : "failed")); + } + if( !pMap ){ + pShmNode->lastErrno = osGetLastError(); + rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno, + "winShmMap3", pDbFd->zPath); + if( hMap ) osCloseHandle(hMap); + goto shmpage_out; + } + + pShmNode->aRegion[pShmNode->nRegion].pMap = pMap; + pShmNode->aRegion[pShmNode->nRegion].hMap = hMap; + pShmNode->nRegion++; + } + } + +shmpage_out: + if( pShmNode->nRegion>iRegion ){ + int iOffset = iRegion*szRegion; + int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; + char *p = (char *)pShmNode->aRegion[iRegion].pMap; + *pp = (void *)&p[iOffsetShift]; + }else{ + *pp = 0; + } + sqlite3_mutex_leave(pShmNode->mutex); + return rc; +} + +#else +# define winShmMap 0 +# define winShmLock 0 +# define winShmBarrier 0 +# define winShmUnmap 0 +#endif /* #ifndef SQLITE_OMIT_WAL */ + +/* +** Cleans up the mapped region of the specified file, if any. +*/ +#if SQLITE_MAX_MMAP_SIZE>0 +static int winUnmapfile(winFile *pFile){ + assert( pFile!=0 ); + OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " + "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n", + osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, + pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax)); + if( pFile->pMapRegion ){ + if( !osUnmapViewOfFile(pFile->pMapRegion) ){ + pFile->lastErrno = osGetLastError(); + OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, " + "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile, + pFile->pMapRegion)); + return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, + "winUnmapfile1", pFile->zPath); + } + pFile->pMapRegion = 0; + pFile->mmapSize = 0; + pFile->mmapSizeActual = 0; + } + if( pFile->hMap!=NULL ){ + if( !osCloseHandle(pFile->hMap) ){ + pFile->lastErrno = osGetLastError(); + OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n", + osGetCurrentProcessId(), pFile, pFile->hMap)); + return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, + "winUnmapfile2", pFile->zPath); + } + pFile->hMap = NULL; + } + OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFile)); + return SQLITE_OK; +} + +/* +** Memory map or remap the file opened by file-descriptor pFd (if the file +** is already mapped, the existing mapping is replaced by the new). Or, if +** there already exists a mapping for this file, and there are still +** outstanding xFetch() references to it, this function is a no-op. +** +** If parameter nByte is non-negative, then it is the requested size of +** the mapping to create. Otherwise, if nByte is less than zero, then the +** requested size is the size of the file on disk. The actual size of the +** created mapping is either the requested size or the value configured +** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller. +** +** SQLITE_OK is returned if no error occurs (even if the mapping is not +** recreated as a result of outstanding references) or an SQLite error +** code otherwise. +*/ +static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ + sqlite3_int64 nMap = nByte; + int rc; + + assert( nMap>=0 || pFd->nFetchOut==0 ); + OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n", + osGetCurrentProcessId(), pFd, nByte)); + + if( pFd->nFetchOut>0 ) return SQLITE_OK; + + if( nMap<0 ){ + rc = winFileSize((sqlite3_file*)pFd, &nMap); + if( rc ){ + OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n", + osGetCurrentProcessId(), pFd)); + return SQLITE_IOERR_FSTAT; + } + } + if( nMap>pFd->mmapSizeMax ){ + nMap = pFd->mmapSizeMax; + } + nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1); + + if( nMap==0 && pFd->mmapSize>0 ){ + winUnmapfile(pFd); + } + if( nMap!=pFd->mmapSize ){ + void *pNew = 0; + DWORD protect = PAGE_READONLY; + DWORD flags = FILE_MAP_READ; + + winUnmapfile(pFd); +#ifdef SQLITE_MMAP_READWRITE + if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){ + protect = PAGE_READWRITE; + flags |= FILE_MAP_WRITE; + } +#endif +#if SQLITE_OS_WINRT + pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL); +#elif defined(SQLITE_WIN32_HAS_WIDE) + pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect, + (DWORD)((nMap>>32) & 0xffffffff), + (DWORD)(nMap & 0xffffffff), NULL); +#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA + pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect, + (DWORD)((nMap>>32) & 0xffffffff), + (DWORD)(nMap & 0xffffffff), NULL); +#endif + if( pFd->hMap==NULL ){ + pFd->lastErrno = osGetLastError(); + rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, + "winMapfile1", pFd->zPath); + /* Log the error, but continue normal operation using xRead/xWrite */ + OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n", + osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); + return SQLITE_OK; + } + assert( (nMap % winSysInfo.dwPageSize)==0 ); + assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff ); +#if SQLITE_OS_WINRT + pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap); +#else + pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap); +#endif + if( pNew==NULL ){ + osCloseHandle(pFd->hMap); + pFd->hMap = NULL; + pFd->lastErrno = osGetLastError(); + rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, + "winMapfile2", pFd->zPath); + /* Log the error, but continue normal operation using xRead/xWrite */ + OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n", + osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); + return SQLITE_OK; + } + pFd->pMapRegion = pNew; + pFd->mmapSize = nMap; + pFd->mmapSizeActual = nMap; + } + + OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), pFd)); + return SQLITE_OK; +} +#endif /* SQLITE_MAX_MMAP_SIZE>0 */ + +/* +** If possible, return a pointer to a mapping of file fd starting at offset +** iOff. The mapping must be valid for at least nAmt bytes. +** +** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. +** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. +** Finally, if an error does occur, return an SQLite error code. The final +** value of *pp is undefined in this case. +** +** If this function does return a pointer, the caller must eventually +** release the reference by calling winUnfetch(). +*/ +static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ +#if SQLITE_MAX_MMAP_SIZE>0 + winFile *pFd = (winFile*)fd; /* The underlying database file */ +#endif + *pp = 0; + + OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n", + osGetCurrentProcessId(), fd, iOff, nAmt, pp)); + +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFd->mmapSizeMax>0 ){ + if( pFd->pMapRegion==0 ){ + int rc = winMapfile(pFd, -1); + if( rc!=SQLITE_OK ){ + OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n", + osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); + return rc; + } + } + if( pFd->mmapSize >= iOff+nAmt ){ + *pp = &((u8 *)pFd->pMapRegion)[iOff]; + pFd->nFetchOut++; + } + } +#endif + + OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), fd, pp, *pp)); + return SQLITE_OK; +} + +/* +** If the third argument is non-NULL, then this function releases a +** reference obtained by an earlier call to winFetch(). The second +** argument passed to this function must be the same as the corresponding +** argument that was passed to the winFetch() invocation. +** +** Or, if the third argument is NULL, then this function is being called +** to inform the VFS layer that, according to POSIX, any existing mapping +** may now be invalid and should be unmapped. +*/ +static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){ +#if SQLITE_MAX_MMAP_SIZE>0 + winFile *pFd = (winFile*)fd; /* The underlying database file */ + + /* If p==0 (unmap the entire file) then there must be no outstanding + ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), + ** then there must be at least one outstanding. */ + assert( (p==0)==(pFd->nFetchOut==0) ); + + /* If p!=0, it must match the iOff value. */ + assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); + + OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n", + osGetCurrentProcessId(), pFd, iOff, p)); + + if( p ){ + pFd->nFetchOut--; + }else{ + /* FIXME: If Windows truly always prevents truncating or deleting a + ** file while a mapping is held, then the following winUnmapfile() call + ** is unnecessary can be omitted - potentially improving + ** performance. */ + winUnmapfile(pFd); + } + + assert( pFd->nFetchOut>=0 ); +#endif + + OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n", + osGetCurrentProcessId(), fd)); + return SQLITE_OK; +} + +/* +** Here ends the implementation of all sqlite3_file methods. +** +********************** End sqlite3_file Methods ******************************* +******************************************************************************/ + +/* +** This vector defines all the methods that can operate on an +** sqlite3_file for win32. +*/ +static const sqlite3_io_methods winIoMethod = { + 3, /* iVersion */ + winClose, /* xClose */ + winRead, /* xRead */ + winWrite, /* xWrite */ + winTruncate, /* xTruncate */ + winSync, /* xSync */ + winFileSize, /* xFileSize */ + winLock, /* xLock */ + winUnlock, /* xUnlock */ + winCheckReservedLock, /* xCheckReservedLock */ + winFileControl, /* xFileControl */ + winSectorSize, /* xSectorSize */ + winDeviceCharacteristics, /* xDeviceCharacteristics */ + winShmMap, /* xShmMap */ + winShmLock, /* xShmLock */ + winShmBarrier, /* xShmBarrier */ + winShmUnmap, /* xShmUnmap */ + winFetch, /* xFetch */ + winUnfetch /* xUnfetch */ +}; + +/**************************************************************************** +**************************** sqlite3_vfs methods **************************** +** +** This division contains the implementation of methods on the +** sqlite3_vfs object. +*/ + +#if defined(__CYGWIN__) +/* +** Convert a filename from whatever the underlying operating system +** supports for filenames into UTF-8. Space to hold the result is +** obtained from malloc and must be freed by the calling function. +*/ +static char *winConvertToUtf8Filename(const void *zFilename){ + char *zConverted = 0; + if( osIsNT() ){ + zConverted = winUnicodeToUtf8(zFilename); + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI()); + } +#endif + /* caller will handle out of memory */ + return zConverted; +} +#endif + +/* +** Convert a UTF-8 filename into whatever form the underlying +** operating system wants filenames in. Space to hold the result +** is obtained from malloc and must be freed by the calling +** function. +*/ +static void *winConvertFromUtf8Filename(const char *zFilename){ + void *zConverted = 0; + if( osIsNT() ){ + zConverted = winUtf8ToUnicode(zFilename); + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI()); + } +#endif + /* caller will handle out of memory */ + return zConverted; +} + +/* +** This function returns non-zero if the specified UTF-8 string buffer +** ends with a directory separator character or one was successfully +** added to it. +*/ +static int winMakeEndInDirSep(int nBuf, char *zBuf){ + if( zBuf ){ + int nLen = sqlite3Strlen30(zBuf); + if( nLen>0 ){ + if( winIsDirSep(zBuf[nLen-1]) ){ + return 1; + }else if( nLen+1mxPathname; nBuf = nMax + 2; + zBuf = sqlite3MallocZero( nBuf ); + if( !zBuf ){ + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); + return SQLITE_IOERR_NOMEM_BKPT; + } + + /* Figure out the effective temporary directory. First, check if one + ** has been explicitly set by the application; otherwise, use the one + ** configured by the operating system. + */ + nDir = nMax - (nPre + 15); + assert( nDir>0 ); + if( sqlite3_temp_directory ){ + int nDirLen = sqlite3Strlen30(sqlite3_temp_directory); + if( nDirLen>0 ){ + if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){ + nDirLen++; + } + if( nDirLen>nDir ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); + return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0); + } + sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory); + } + } +#if defined(__CYGWIN__) + else{ + static const char *azDirs[] = { + 0, /* getenv("SQLITE_TMPDIR") */ + 0, /* getenv("TMPDIR") */ + 0, /* getenv("TMP") */ + 0, /* getenv("TEMP") */ + 0, /* getenv("USERPROFILE") */ + "/var/tmp", + "/usr/tmp", + "/tmp", + ".", + 0 /* List terminator */ + }; + unsigned int i; + const char *zDir = 0; + + if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); + if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); + if( !azDirs[2] ) azDirs[2] = getenv("TMP"); + if( !azDirs[3] ) azDirs[3] = getenv("TEMP"); + if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE"); + for(i=0; i/etilqs_XXXXXXXXXXXXXXX\0\0" + ** + ** If not, return SQLITE_ERROR. The number 17 is used here in order to + ** account for the space used by the 15 character random suffix and the + ** two trailing NUL characters. The final directory separator character + ** has already added if it was not already present. + */ + nLen = sqlite3Strlen30(zBuf); + if( (nLen + nPre + 17) > nBuf ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); + return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0); + } + + sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX); + + j = sqlite3Strlen30(zBuf); + sqlite3_randomness(15, &zBuf[j]); + for(i=0; i<15; i++, j++){ + zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; + } + zBuf[j] = 0; + zBuf[j+1] = 0; + *pzBuf = zBuf; + + OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf)); + return SQLITE_OK; +} + +/* +** Return TRUE if the named file is really a directory. Return false if +** it is something other than a directory, or if there is any kind of memory +** allocation failure. +*/ +static int winIsDir(const void *zConverted){ + DWORD attr; + int rc = 0; + DWORD lastErrno; + + if( osIsNT() ){ + int cnt = 0; + WIN32_FILE_ATTRIBUTE_DATA sAttrData; + memset(&sAttrData, 0, sizeof(sAttrData)); + while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, + GetFileExInfoStandard, + &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} + if( !rc ){ + return 0; /* Invalid name? */ + } + attr = sAttrData.dwFileAttributes; +#if SQLITE_OS_WINCE==0 + }else{ + attr = osGetFileAttributesA((char*)zConverted); +#endif + } + return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY); +} + +/* +** Open a file. +*/ +static int winOpen( + sqlite3_vfs *pVfs, /* Used to get maximum path name length */ + const char *zName, /* Name of the file (UTF-8) */ + sqlite3_file *id, /* Write the SQLite file handle here */ + int flags, /* Open mode flags */ + int *pOutFlags /* Status return flags */ +){ + HANDLE h; + DWORD lastErrno = 0; + DWORD dwDesiredAccess; + DWORD dwShareMode; + DWORD dwCreationDisposition; + DWORD dwFlagsAndAttributes = 0; +#if SQLITE_OS_WINCE + int isTemp = 0; +#endif + winFile *pFile = (winFile*)id; + void *zConverted; /* Filename in OS encoding */ + const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ + int cnt = 0; + + /* If argument zPath is a NULL pointer, this function is required to open + ** a temporary file. Use this buffer to store the file name in. + */ + char *zTmpname = 0; /* For temporary filename, if necessary. */ + + int rc = SQLITE_OK; /* Function Return Code */ +#if !defined(NDEBUG) || SQLITE_OS_WINCE + int eType = flags&0xFFFFFF00; /* Type of file to open */ +#endif + + int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); + int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); + int isCreate = (flags & SQLITE_OPEN_CREATE); + int isReadonly = (flags & SQLITE_OPEN_READONLY); + int isReadWrite = (flags & SQLITE_OPEN_READWRITE); + +#ifndef NDEBUG + int isOpenJournal = (isCreate && ( + eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_MAIN_JOURNAL + || eType==SQLITE_OPEN_WAL + )); +#endif + + OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n", + zUtf8Name, id, flags, pOutFlags)); + + /* Check the following statements are true: + ** + ** (a) Exactly one of the READWRITE and READONLY flags must be set, and + ** (b) if CREATE is set, then READWRITE must also be set, and + ** (c) if EXCLUSIVE is set, then CREATE must also be set. + ** (d) if DELETEONCLOSE is set, then CREATE must also be set. + */ + assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); + assert(isCreate==0 || isReadWrite); + assert(isExclusive==0 || isCreate); + assert(isDelete==0 || isCreate); + + /* The main DB, main journal, WAL file and master journal are never + ** automatically deleted. Nor are they ever temporary files. */ + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); + + /* Assert that the upper layer has set one of the "file-type" flags. */ + assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB + || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL + || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL + ); + + assert( pFile!=0 ); + memset(pFile, 0, sizeof(winFile)); + pFile->h = INVALID_HANDLE_VALUE; + +#if SQLITE_OS_WINRT + if( !zUtf8Name && !sqlite3_temp_directory ){ + sqlite3_log(SQLITE_ERROR, + "sqlite3_temp_directory variable should be set for WinRT"); + } +#endif + + /* If the second argument to this function is NULL, generate a + ** temporary file name to use + */ + if( !zUtf8Name ){ + assert( isDelete && !isOpenJournal ); + rc = winGetTempname(pVfs, &zTmpname); + if( rc!=SQLITE_OK ){ + OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc))); + return rc; + } + zUtf8Name = zTmpname; + } + + /* Database filenames are double-zero terminated if they are not + ** URIs with parameters. Hence, they can always be passed into + ** sqlite3_uri_parameter(). + */ + assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) || + zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 ); + + /* Convert the filename to the system encoding. */ + zConverted = winConvertFromUtf8Filename(zUtf8Name); + if( zConverted==0 ){ + sqlite3_free(zTmpname); + OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name)); + return SQLITE_IOERR_NOMEM_BKPT; + } + + if( winIsDir(zConverted) ){ + sqlite3_free(zConverted); + sqlite3_free(zTmpname); + OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name)); + return SQLITE_CANTOPEN_ISDIR; + } + + if( isReadWrite ){ + dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; + }else{ + dwDesiredAccess = GENERIC_READ; + } + + /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is + ** created. SQLite doesn't use it to indicate "exclusive access" + ** as it is usually understood. + */ + if( isExclusive ){ + /* Creates a new file, only if it does not already exist. */ + /* If the file exists, it fails. */ + dwCreationDisposition = CREATE_NEW; + }else if( isCreate ){ + /* Open existing file, or create if it doesn't exist */ + dwCreationDisposition = OPEN_ALWAYS; + }else{ + /* Opens a file, only if it exists. */ + dwCreationDisposition = OPEN_EXISTING; + } + + dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; + + if( isDelete ){ +#if SQLITE_OS_WINCE + dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; + isTemp = 1; +#else + dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY + | FILE_ATTRIBUTE_HIDDEN + | FILE_FLAG_DELETE_ON_CLOSE; +#endif + }else{ + dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; + } + /* Reports from the internet are that performance is always + ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ +#if SQLITE_OS_WINCE + dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; +#endif + + if( osIsNT() ){ +#if SQLITE_OS_WINRT + CREATEFILE2_EXTENDED_PARAMETERS extendedParameters; + extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS); + extendedParameters.dwFileAttributes = + dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK; + extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK; + extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; + extendedParameters.lpSecurityAttributes = NULL; + extendedParameters.hTemplateFile = NULL; + while( (h = osCreateFile2((LPCWSTR)zConverted, + dwDesiredAccess, + dwShareMode, + dwCreationDisposition, + &extendedParameters))==INVALID_HANDLE_VALUE && + winRetryIoerr(&cnt, &lastErrno) ){ + /* Noop */ + } +#else + while( (h = osCreateFileW((LPCWSTR)zConverted, + dwDesiredAccess, + dwShareMode, NULL, + dwCreationDisposition, + dwFlagsAndAttributes, + NULL))==INVALID_HANDLE_VALUE && + winRetryIoerr(&cnt, &lastErrno) ){ + /* Noop */ + } +#endif + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + while( (h = osCreateFileA((LPCSTR)zConverted, + dwDesiredAccess, + dwShareMode, NULL, + dwCreationDisposition, + dwFlagsAndAttributes, + NULL))==INVALID_HANDLE_VALUE && + winRetryIoerr(&cnt, &lastErrno) ){ + /* Noop */ + } + } +#endif + winLogIoerr(cnt, __LINE__); + + OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name, + dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); + + if( h==INVALID_HANDLE_VALUE ){ + pFile->lastErrno = lastErrno; + winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); + sqlite3_free(zConverted); + sqlite3_free(zTmpname); + if( isReadWrite && !isExclusive ){ + return winOpen(pVfs, zName, id, + ((flags|SQLITE_OPEN_READONLY) & + ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), + pOutFlags); + }else{ + return SQLITE_CANTOPEN_BKPT; + } + } + + if( pOutFlags ){ + if( isReadWrite ){ + *pOutFlags = SQLITE_OPEN_READWRITE; + }else{ + *pOutFlags = SQLITE_OPEN_READONLY; + } + } + + OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, " + "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ? + *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); + +#if SQLITE_OS_WINCE + if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB + && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK + ){ + osCloseHandle(h); + sqlite3_free(zConverted); + sqlite3_free(zTmpname); + OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); + return rc; + } + if( isTemp ){ + pFile->zDeleteOnClose = zConverted; + }else +#endif + { + sqlite3_free(zConverted); + } + + sqlite3_free(zTmpname); + pFile->pMethod = &winIoMethod; + pFile->pVfs = pVfs; + pFile->h = h; + if( isReadonly ){ + pFile->ctrlFlags |= WINFILE_RDONLY; + } + if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ + pFile->ctrlFlags |= WINFILE_PSOW; + } + pFile->lastErrno = NO_ERROR; + pFile->zPath = zName; +#if SQLITE_MAX_MMAP_SIZE>0 + pFile->hMap = NULL; + pFile->pMapRegion = 0; + pFile->mmapSize = 0; + pFile->mmapSizeActual = 0; + pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; +#endif + + OpenCounter(+1); + return rc; +} + +/* +** Delete the named file. +** +** Note that Windows does not allow a file to be deleted if some other +** process has it open. Sometimes a virus scanner or indexing program +** will open a journal file shortly after it is created in order to do +** whatever it does. While this other process is holding the +** file open, we will be unable to delete it. To work around this +** problem, we delay 100 milliseconds and try to delete again. Up +** to MX_DELETION_ATTEMPTs deletion attempts are run before giving +** up and returning an error. +*/ +static int winDelete( + sqlite3_vfs *pVfs, /* Not used on win32 */ + const char *zFilename, /* Name of file to delete */ + int syncDir /* Not used on win32 */ +){ + int cnt = 0; + int rc; + DWORD attr; + DWORD lastErrno = 0; + void *zConverted; + UNUSED_PARAMETER(pVfs); + UNUSED_PARAMETER(syncDir); + + SimulateIOError(return SQLITE_IOERR_DELETE); + OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir)); + + zConverted = winConvertFromUtf8Filename(zFilename); + if( zConverted==0 ){ + OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); + return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + do { +#if SQLITE_OS_WINRT + WIN32_FILE_ATTRIBUTE_DATA sAttrData; + memset(&sAttrData, 0, sizeof(sAttrData)); + if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard, + &sAttrData) ){ + attr = sAttrData.dwFileAttributes; + }else{ + lastErrno = osGetLastError(); + if( lastErrno==ERROR_FILE_NOT_FOUND + || lastErrno==ERROR_PATH_NOT_FOUND ){ + rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ + }else{ + rc = SQLITE_ERROR; + } + break; + } +#else + attr = osGetFileAttributesW(zConverted); +#endif + if ( attr==INVALID_FILE_ATTRIBUTES ){ + lastErrno = osGetLastError(); + if( lastErrno==ERROR_FILE_NOT_FOUND + || lastErrno==ERROR_PATH_NOT_FOUND ){ + rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ + }else{ + rc = SQLITE_ERROR; + } + break; + } + if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ + rc = SQLITE_ERROR; /* Files only. */ + break; + } + if ( osDeleteFileW(zConverted) ){ + rc = SQLITE_OK; /* Deleted OK. */ + break; + } + if ( !winRetryIoerr(&cnt, &lastErrno) ){ + rc = SQLITE_ERROR; /* No more retries. */ + break; + } + } while(1); + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + do { + attr = osGetFileAttributesA(zConverted); + if ( attr==INVALID_FILE_ATTRIBUTES ){ + lastErrno = osGetLastError(); + if( lastErrno==ERROR_FILE_NOT_FOUND + || lastErrno==ERROR_PATH_NOT_FOUND ){ + rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ + }else{ + rc = SQLITE_ERROR; + } + break; + } + if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ + rc = SQLITE_ERROR; /* Files only. */ + break; + } + if ( osDeleteFileA(zConverted) ){ + rc = SQLITE_OK; /* Deleted OK. */ + break; + } + if ( !winRetryIoerr(&cnt, &lastErrno) ){ + rc = SQLITE_ERROR; /* No more retries. */ + break; + } + } while(1); + } +#endif + if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){ + rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename); + }else{ + winLogIoerr(cnt, __LINE__); + } + sqlite3_free(zConverted); + OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc))); + return rc; +} + +/* +** Check the existence and status of a file. +*/ +static int winAccess( + sqlite3_vfs *pVfs, /* Not used on win32 */ + const char *zFilename, /* Name of file to check */ + int flags, /* Type of test to make on this file */ + int *pResOut /* OUT: Result */ +){ + DWORD attr; + int rc = 0; + DWORD lastErrno = 0; + void *zConverted; + UNUSED_PARAMETER(pVfs); + + SimulateIOError( return SQLITE_IOERR_ACCESS; ); + OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n", + zFilename, flags, pResOut)); + + zConverted = winConvertFromUtf8Filename(zFilename); + if( zConverted==0 ){ + OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); + return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + int cnt = 0; + WIN32_FILE_ATTRIBUTE_DATA sAttrData; + memset(&sAttrData, 0, sizeof(sAttrData)); + while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, + GetFileExInfoStandard, + &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} + if( rc ){ + /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file + ** as if it does not exist. + */ + if( flags==SQLITE_ACCESS_EXISTS + && sAttrData.nFileSizeHigh==0 + && sAttrData.nFileSizeLow==0 ){ + attr = INVALID_FILE_ATTRIBUTES; + }else{ + attr = sAttrData.dwFileAttributes; + } + }else{ + winLogIoerr(cnt, __LINE__); + if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){ + sqlite3_free(zConverted); + return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", + zFilename); + }else{ + attr = INVALID_FILE_ATTRIBUTES; + } + } + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + attr = osGetFileAttributesA((char*)zConverted); + } +#endif + sqlite3_free(zConverted); + switch( flags ){ + case SQLITE_ACCESS_READ: + case SQLITE_ACCESS_EXISTS: + rc = attr!=INVALID_FILE_ATTRIBUTES; + break; + case SQLITE_ACCESS_READWRITE: + rc = attr!=INVALID_FILE_ATTRIBUTES && + (attr & FILE_ATTRIBUTE_READONLY)==0; + break; + default: + assert(!"Invalid flags argument"); + } + *pResOut = rc; + OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", + zFilename, pResOut, *pResOut)); + return SQLITE_OK; +} + +/* +** Returns non-zero if the specified path name starts with a drive letter +** followed by a colon character. +*/ +static BOOL winIsDriveLetterAndColon( + const char *zPathname +){ + return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ); +} + +/* +** Returns non-zero if the specified path name should be used verbatim. If +** non-zero is returned from this function, the calling function must simply +** use the provided path name verbatim -OR- resolve it into a full path name +** using the GetFullPathName Win32 API function (if available). +*/ +static BOOL winIsVerbatimPathname( + const char *zPathname +){ + /* + ** If the path name starts with a forward slash or a backslash, it is either + ** a legal UNC name, a volume relative path, or an absolute path name in the + ** "Unix" format on Windows. There is no easy way to differentiate between + ** the final two cases; therefore, we return the safer return value of TRUE + ** so that callers of this function will simply use it verbatim. + */ + if ( winIsDirSep(zPathname[0]) ){ + return TRUE; + } + + /* + ** If the path name starts with a letter and a colon it is either a volume + ** relative path or an absolute path. Callers of this function must not + ** attempt to treat it as a relative path name (i.e. they should simply use + ** it verbatim). + */ + if ( winIsDriveLetterAndColon(zPathname) ){ + return TRUE; + } + + /* + ** If we get to this point, the path name should almost certainly be a purely + ** relative one (i.e. not a UNC name, not absolute, and not volume relative). + */ + return FALSE; +} + +/* +** Turn a relative pathname into a full pathname. Write the full +** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname +** bytes in size. +*/ +static int winFullPathname( + sqlite3_vfs *pVfs, /* Pointer to vfs object */ + const char *zRelative, /* Possibly relative input path */ + int nFull, /* Size of output buffer in bytes */ + char *zFull /* Output buffer */ +){ + +#if defined(__CYGWIN__) + SimulateIOError( return SQLITE_ERROR ); + UNUSED_PARAMETER(nFull); + assert( nFull>=pVfs->mxPathname ); + if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ + /* + ** NOTE: We are dealing with a relative path name and the data + ** directory has been set. Therefore, use it as the basis + ** for converting the relative path name to an absolute + ** one by prepending the data directory and a slash. + */ + char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); + if( !zOut ){ + return SQLITE_IOERR_NOMEM_BKPT; + } + if( cygwin_conv_path( + (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) | + CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){ + sqlite3_free(zOut); + return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, + "winFullPathname1", zRelative); + }else{ + char *zUtf8 = winConvertToUtf8Filename(zOut); + if( !zUtf8 ){ + sqlite3_free(zOut); + return SQLITE_IOERR_NOMEM_BKPT; + } + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", + sqlite3_data_directory, winGetDirSep(), zUtf8); + sqlite3_free(zUtf8); + sqlite3_free(zOut); + } + }else{ + char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); + if( !zOut ){ + return SQLITE_IOERR_NOMEM_BKPT; + } + if( cygwin_conv_path( + (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A), + zRelative, zOut, pVfs->mxPathname+1)<0 ){ + sqlite3_free(zOut); + return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, + "winFullPathname2", zRelative); + }else{ + char *zUtf8 = winConvertToUtf8Filename(zOut); + if( !zUtf8 ){ + sqlite3_free(zOut); + return SQLITE_IOERR_NOMEM_BKPT; + } + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8); + sqlite3_free(zUtf8); + sqlite3_free(zOut); + } + } + return SQLITE_OK; +#endif + +#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__) + SimulateIOError( return SQLITE_ERROR ); + /* WinCE has no concept of a relative pathname, or so I am told. */ + /* WinRT has no way to convert a relative path to an absolute one. */ + if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ + /* + ** NOTE: We are dealing with a relative path name and the data + ** directory has been set. Therefore, use it as the basis + ** for converting the relative path name to an absolute + ** one by prepending the data directory and a backslash. + */ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", + sqlite3_data_directory, winGetDirSep(), zRelative); + }else{ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative); + } + return SQLITE_OK; +#endif + +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__) + DWORD nByte; + void *zConverted; + char *zOut; + + /* If this path name begins with "/X:", where "X" is any alphabetic + ** character, discard the initial "/" from the pathname. + */ + if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ + zRelative++; + } + + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this + ** function failing. This function could fail if, for example, the + ** current working directory has been unlinked. + */ + SimulateIOError( return SQLITE_ERROR ); + if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ + /* + ** NOTE: We are dealing with a relative path name and the data + ** directory has been set. Therefore, use it as the basis + ** for converting the relative path name to an absolute + ** one by prepending the data directory and a backslash. + */ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", + sqlite3_data_directory, winGetDirSep(), zRelative); + return SQLITE_OK; + } + zConverted = winConvertFromUtf8Filename(zRelative); + if( zConverted==0 ){ + return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + LPWSTR zTemp; + nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0); + if( nByte==0 ){ + sqlite3_free(zConverted); + return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), + "winFullPathname1", zRelative); + } + nByte += 3; + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); + if( zTemp==0 ){ + sqlite3_free(zConverted); + return SQLITE_IOERR_NOMEM_BKPT; + } + nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0); + if( nByte==0 ){ + sqlite3_free(zConverted); + sqlite3_free(zTemp); + return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), + "winFullPathname2", zRelative); + } + sqlite3_free(zConverted); + zOut = winUnicodeToUtf8(zTemp); + sqlite3_free(zTemp); + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + char *zTemp; + nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0); + if( nByte==0 ){ + sqlite3_free(zConverted); + return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), + "winFullPathname3", zRelative); + } + nByte += 3; + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); + if( zTemp==0 ){ + sqlite3_free(zConverted); + return SQLITE_IOERR_NOMEM_BKPT; + } + nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0); + if( nByte==0 ){ + sqlite3_free(zConverted); + sqlite3_free(zTemp); + return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), + "winFullPathname4", zRelative); + } + sqlite3_free(zConverted); + zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); + sqlite3_free(zTemp); + } +#endif + if( zOut ){ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); + sqlite3_free(zOut); + return SQLITE_OK; + }else{ + return SQLITE_IOERR_NOMEM_BKPT; + } +#endif +} + +#ifndef SQLITE_OMIT_LOAD_EXTENSION +/* +** Interfaces for opening a shared library, finding entry points +** within the shared library, and closing the shared library. +*/ +static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ + HANDLE h; +#if defined(__CYGWIN__) + int nFull = pVfs->mxPathname+1; + char *zFull = sqlite3MallocZero( nFull ); + void *zConverted = 0; + if( zFull==0 ){ + OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); + return 0; + } + if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){ + sqlite3_free(zFull); + OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); + return 0; + } + zConverted = winConvertFromUtf8Filename(zFull); + sqlite3_free(zFull); +#else + void *zConverted = winConvertFromUtf8Filename(zFilename); + UNUSED_PARAMETER(pVfs); +#endif + if( zConverted==0 ){ + OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); + return 0; + } + if( osIsNT() ){ +#if SQLITE_OS_WINRT + h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0); +#else + h = osLoadLibraryW((LPCWSTR)zConverted); +#endif + } +#ifdef SQLITE_WIN32_HAS_ANSI + else{ + h = osLoadLibraryA((char*)zConverted); + } +#endif + OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h)); + sqlite3_free(zConverted); + return (void*)h; +} +static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ + UNUSED_PARAMETER(pVfs); + winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut); +} +static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){ + FARPROC proc; + UNUSED_PARAMETER(pVfs); + proc = osGetProcAddressA((HANDLE)pH, zSym); + OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n", + (void*)pH, zSym, (void*)proc)); + return (void(*)(void))proc; +} +static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ + UNUSED_PARAMETER(pVfs); + osFreeLibrary((HANDLE)pHandle); + OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle)); +} +#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ + #define winDlOpen 0 + #define winDlError 0 + #define winDlSym 0 + #define winDlClose 0 +#endif + +/* State information for the randomness gatherer. */ +typedef struct EntropyGatherer EntropyGatherer; +struct EntropyGatherer { + unsigned char *a; /* Gather entropy into this buffer */ + int na; /* Size of a[] in bytes */ + int i; /* XOR next input into a[i] */ + int nXor; /* Number of XOR operations done */ +}; + +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) +/* Mix sz bytes of entropy into p. */ +static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){ + int j, k; + for(j=0, k=p->i; ja[k++] ^= x[j]; + if( k>=p->na ) k = 0; + } + p->i = k; + p->nXor += sz; +} +#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */ + +/* +** Write up to nBuf bytes of randomness into zBuf. +*/ +static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) + UNUSED_PARAMETER(pVfs); + memset(zBuf, 0, nBuf); + return nBuf; +#else + EntropyGatherer e; + UNUSED_PARAMETER(pVfs); + memset(zBuf, 0, nBuf); +#if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE + rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */ +#endif /* defined(_MSC_VER) && _MSC_VER>=1400 */ + e.a = (unsigned char*)zBuf; + e.na = nBuf; + e.nXor = 0; + e.i = 0; + { + SYSTEMTIME x; + osGetSystemTime(&x); + xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME)); + } + { + DWORD pid = osGetCurrentProcessId(); + xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD)); + } +#if SQLITE_OS_WINRT + { + ULONGLONG cnt = osGetTickCount64(); + xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG)); + } +#else + { + DWORD cnt = osGetTickCount(); + xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD)); + } +#endif /* SQLITE_OS_WINRT */ + { + LARGE_INTEGER i; + osQueryPerformanceCounter(&i); + xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER)); + } +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID + { + UUID id; + memset(&id, 0, sizeof(UUID)); + osUuidCreate(&id); + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); + memset(&id, 0, sizeof(UUID)); + osUuidCreateSequential(&id); + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); + } +#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */ + return e.nXor>nBuf ? nBuf : e.nXor; +#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */ +} + + +/* +** Sleep for a little while. Return the amount of time slept. +*/ +static int winSleep(sqlite3_vfs *pVfs, int microsec){ + sqlite3_win32_sleep((microsec+999)/1000); + UNUSED_PARAMETER(pVfs); + return ((microsec+999)/1000)*1000; +} + +/* +** The following variable, if set to a non-zero value, is interpreted as +** the number of seconds since 1970 and is used to set the result of +** sqlite3OsCurrentTime() during testing. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ +#endif + +/* +** Find the current time (in Universal Coordinated Time). Write into *piNow +** the current time and date as a Julian Day number times 86_400_000. In +** other words, write into *piNow the number of milliseconds since the Julian +** epoch of noon in Greenwich on November 24, 4714 B.C according to the +** proleptic Gregorian calendar. +** +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date +** cannot be found. +*/ +static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){ + /* FILETIME structure is a 64-bit value representing the number of + 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). + */ + FILETIME ft; + static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000; +#ifdef SQLITE_TEST + static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; +#endif + /* 2^32 - to avoid use of LL and warnings in gcc */ + static const sqlite3_int64 max32BitValue = + (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + + (sqlite3_int64)294967296; + +#if SQLITE_OS_WINCE + SYSTEMTIME time; + osGetSystemTime(&time); + /* if SystemTimeToFileTime() fails, it returns zero. */ + if (!osSystemTimeToFileTime(&time,&ft)){ + return SQLITE_ERROR; + } +#else + osGetSystemTimeAsFileTime( &ft ); +#endif + + *piNow = winFiletimeEpoch + + ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + + (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000; + +#ifdef SQLITE_TEST + if( sqlite3_current_time ){ + *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; + } +#endif + UNUSED_PARAMETER(pVfs); + return SQLITE_OK; +} + +/* +** Find the current time (in Universal Coordinated Time). Write the +** current time and date as a Julian Day number into *prNow and +** return 0. Return 1 if the time and date cannot be found. +*/ +static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ + int rc; + sqlite3_int64 i; + rc = winCurrentTimeInt64(pVfs, &i); + if( !rc ){ + *prNow = i/86400000.0; + } + return rc; +} + +/* +** The idea is that this function works like a combination of +** GetLastError() and FormatMessage() on Windows (or errno and +** strerror_r() on Unix). After an error is returned by an OS +** function, SQLite calls this function with zBuf pointing to +** a buffer of nBuf bytes. The OS layer should populate the +** buffer with a nul-terminated UTF-8 encoded error message +** describing the last IO error to have occurred within the calling +** thread. +** +** If the error message is too large for the supplied buffer, +** it should be truncated. The return value of xGetLastError +** is zero if the error message fits in the buffer, or non-zero +** otherwise (if the message was truncated). If non-zero is returned, +** then it is not necessary to include the nul-terminator character +** in the output buffer. +** +** Not supplying an error message will have no adverse effect +** on SQLite. It is fine to have an implementation that never +** returns an error message: +** +** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ +** assert(zBuf[0]=='\0'); +** return 0; +** } +** +** However if an error message is supplied, it will be incorporated +** by sqlite into the error message available to the user using +** sqlite3_errmsg(), possibly making IO errors easier to debug. +*/ +static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ + DWORD e = osGetLastError(); + UNUSED_PARAMETER(pVfs); + if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf); + return e; +} + +/* +** Initialize and deinitialize the operating system interface. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ + static sqlite3_vfs winVfs = { + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ + SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ + 0, /* pNext */ + "win32", /* zName */ + 0, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ + }; +#if defined(SQLITE_WIN32_HAS_WIDE) + static sqlite3_vfs winLongPathVfs = { + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ + SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ + 0, /* pNext */ + "win32-longpath", /* zName */ + 0, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ + }; +#endif + + /* Double-check that the aSyscall[] array has been constructed + ** correctly. See ticket [bb3a86e890c8e96ab] */ + assert( ArraySize(aSyscall)==80 ); + + /* get memory map allocation granularity */ + memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); +#if SQLITE_OS_WINRT + osGetNativeSystemInfo(&winSysInfo); +#else + osGetSystemInfo(&winSysInfo); +#endif + assert( winSysInfo.dwAllocationGranularity>0 ); + assert( winSysInfo.dwPageSize>0 ); + + sqlite3_vfs_register(&winVfs, 1); + +#if defined(SQLITE_WIN32_HAS_WIDE) + sqlite3_vfs_register(&winLongPathVfs, 0); +#endif + + return SQLITE_OK; +} + +SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ +#if SQLITE_OS_WINRT + if( sleepObj!=NULL ){ + osCloseHandle(sleepObj); + sleepObj = NULL; + } +#endif + return SQLITE_OK; +} + +#endif /* SQLITE_OS_WIN */ + +/************** End of os_win.c **********************************************/ +/************** Begin file bitvec.c ******************************************/ +/* +** 2008 February 16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements an object that represents a fixed-length +** bitmap. Bits are numbered starting with 1. +** +** A bitmap is used to record which pages of a database file have been +** journalled during a transaction, or which pages have the "dont-write" +** property. Usually only a few pages are meet either condition. +** So the bitmap is usually sparse and has low cardinality. +** But sometimes (for example when during a DROP of a large table) most +** or all of the pages in a database can get journalled. In those cases, +** the bitmap becomes dense with high cardinality. The algorithm needs +** to handle both cases well. +** +** The size of the bitmap is fixed when the object is created. +** +** All bits are clear when the bitmap is created. Individual bits +** may be set or cleared one at a time. +** +** Test operations are about 100 times more common that set operations. +** Clear operations are exceedingly rare. There are usually between +** 5 and 500 set operations per Bitvec object, though the number of sets can +** sometimes grow into tens of thousands or larger. The size of the +** Bitvec object is the number of pages in the database file at the +** start of a transaction, and is thus usually less than a few thousand, +** but can be as large as 2 billion for a really big database. +*/ +/* #include "sqliteInt.h" */ + +/* Size of the Bitvec structure in bytes. */ +#define BITVEC_SZ 512 + +/* Round the union size down to the nearest pointer boundary, since that's how +** it will be aligned within the Bitvec struct. */ +#define BITVEC_USIZE \ + (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) + +/* Type of the array "element" for the bitmap representation. +** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. +** Setting this to the "natural word" size of your CPU may improve +** performance. */ +#define BITVEC_TELEM u8 +/* Size, in bits, of the bitmap element. */ +#define BITVEC_SZELEM 8 +/* Number of elements in a bitmap array. */ +#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM)) +/* Number of bits in the bitmap array. */ +#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM) + +/* Number of u32 values in hash table. */ +#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32)) +/* Maximum number of entries in hash table before +** sub-dividing and re-hashing. */ +#define BITVEC_MXHASH (BITVEC_NINT/2) +/* Hashing function for the aHash representation. +** Empirical testing showed that the *37 multiplier +** (an arbitrary prime)in the hash function provided +** no fewer collisions than the no-op *1. */ +#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT) + +#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) + + +/* +** A bitmap is an instance of the following structure. +** +** This bitmap records the existence of zero or more bits +** with values between 1 and iSize, inclusive. +** +** There are three possible representations of the bitmap. +** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight +** bitmap. The least significant bit is bit 1. +** +** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is +** a hash table that will hold up to BITVEC_MXHASH distinct values. +** +** Otherwise, the value i is redirected into one of BITVEC_NPTR +** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap +** handles up to iDivisor separate values of i. apSub[0] holds +** values between 1 and iDivisor. apSub[1] holds values between +** iDivisor+1 and 2*iDivisor. apSub[N] holds values between +** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized +** to hold deal with values between 1 and iDivisor. +*/ +struct Bitvec { + u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */ + u32 nSet; /* Number of bits that are set - only valid for aHash + ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512, + ** this would be 125. */ + u32 iDivisor; /* Number of bits handled by each apSub[] entry. */ + /* Should >=0 for apSub element. */ + /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */ + /* For a BITVEC_SZ of 512, this would be 34,359,739. */ + union { + BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */ + u32 aHash[BITVEC_NINT]; /* Hash table representation */ + Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */ + } u; +}; + +/* +** Create a new bitmap object able to handle bits between 0 and iSize, +** inclusive. Return a pointer to the new object. Return NULL if +** malloc fails. +*/ +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){ + Bitvec *p; + assert( sizeof(*p)==BITVEC_SZ ); + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + p->iSize = iSize; + } + return p; +} + +/* +** Check to see if the i-th bit is set. Return true or false. +** If p is NULL (if the bitmap has not been created) or if +** i is out of range, then return false. +*/ +SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){ + assert( p!=0 ); + i--; + if( i>=p->iSize ) return 0; + while( p->iDivisor ){ + u32 bin = i/p->iDivisor; + i = i%p->iDivisor; + p = p->u.apSub[bin]; + if (!p) { + return 0; + } + } + if( p->iSize<=BITVEC_NBIT ){ + return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0; + } else{ + u32 h = BITVEC_HASH(i++); + while( p->u.aHash[h] ){ + if( p->u.aHash[h]==i ) return 1; + h = (h+1) % BITVEC_NINT; + } + return 0; + } +} +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ + return p!=0 && sqlite3BitvecTestNotNull(p,i); +} + +/* +** Set the i-th bit. Return 0 on success and an error code if +** anything goes wrong. +** +** This routine might cause sub-bitmaps to be allocated. Failing +** to get the memory needed to hold the sub-bitmap is the only +** that can go wrong with an insert, assuming p and i are valid. +** +** The calling function must ensure that p is a valid Bitvec object +** and that the value for "i" is within range of the Bitvec object. +** Otherwise the behavior is undefined. +*/ +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){ + u32 h; + if( p==0 ) return SQLITE_OK; + assert( i>0 ); + assert( i<=p->iSize ); + i--; + while((p->iSize > BITVEC_NBIT) && p->iDivisor) { + u32 bin = i/p->iDivisor; + i = i%p->iDivisor; + if( p->u.apSub[bin]==0 ){ + p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); + if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT; + } + p = p->u.apSub[bin]; + } + if( p->iSize<=BITVEC_NBIT ){ + p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1)); + return SQLITE_OK; + } + h = BITVEC_HASH(i++); + /* if there wasn't a hash collision, and this doesn't */ + /* completely fill the hash, then just add it without */ + /* worring about sub-dividing and re-hashing. */ + if( !p->u.aHash[h] ){ + if (p->nSet<(BITVEC_NINT-1)) { + goto bitvec_set_end; + } else { + goto bitvec_set_rehash; + } + } + /* there was a collision, check to see if it's already */ + /* in hash, if not, try to find a spot for it */ + do { + if( p->u.aHash[h]==i ) return SQLITE_OK; + h++; + if( h>=BITVEC_NINT ) h = 0; + } while( p->u.aHash[h] ); + /* we didn't find it in the hash. h points to the first */ + /* available free spot. check to see if this is going to */ + /* make our hash too "full". */ +bitvec_set_rehash: + if( p->nSet>=BITVEC_MXHASH ){ + unsigned int j; + int rc; + u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash)); + if( aiValues==0 ){ + return SQLITE_NOMEM_BKPT; + }else{ + memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); + memset(p->u.apSub, 0, sizeof(p->u.apSub)); + p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; + rc = sqlite3BitvecSet(p, i); + for(j=0; jnSet++; + p->u.aHash[h] = i; + return SQLITE_OK; +} + +/* +** Clear the i-th bit. +** +** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage +** that BitvecClear can use to rebuilt its hash table. +*/ +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){ + if( p==0 ) return; + assert( i>0 ); + i--; + while( p->iDivisor ){ + u32 bin = i/p->iDivisor; + i = i%p->iDivisor; + p = p->u.apSub[bin]; + if (!p) { + return; + } + } + if( p->iSize<=BITVEC_NBIT ){ + p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1))); + }else{ + unsigned int j; + u32 *aiValues = pBuf; + memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); + memset(p->u.aHash, 0, sizeof(p->u.aHash)); + p->nSet = 0; + for(j=0; jnSet++; + while( p->u.aHash[h] ){ + h++; + if( h>=BITVEC_NINT ) h = 0; + } + p->u.aHash[h] = aiValues[j]; + } + } + } +} + +/* +** Destroy a bitmap object. Reclaim all memory used. +*/ +SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){ + if( p==0 ) return; + if( p->iDivisor ){ + unsigned int i; + for(i=0; iu.apSub[i]); + } + } + sqlite3_free(p); +} + +/* +** Return the value of the iSize parameter specified when Bitvec *p +** was created. +*/ +SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){ + return p->iSize; +} + +#ifndef SQLITE_OMIT_BUILTIN_TEST +/* +** Let V[] be an array of unsigned characters sufficient to hold +** up to N bits. Let I be an integer between 0 and N. 0<=I>3] |= (1<<(I&7)) +#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7)) +#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0 + +/* +** This routine runs an extensive test of the Bitvec code. +** +** The input is an array of integers that acts as a program +** to test the Bitvec. The integers are opcodes followed +** by 0, 1, or 3 operands, depending on the opcode. Another +** opcode follows immediately after the last operand. +** +** There are 6 opcodes numbered from 0 through 5. 0 is the +** "halt" opcode and causes the test to end. +** +** 0 Halt and return the number of errors +** 1 N S X Set N bits beginning with S and incrementing by X +** 2 N S X Clear N bits beginning with S and incrementing by X +** 3 N Set N randomly chosen bits +** 4 N Clear N randomly chosen bits +** 5 N S X Set N bits from S increment X in array only, not in bitvec +** +** The opcodes 1 through 4 perform set and clear operations are performed +** on both a Bitvec object and on a linear array of bits obtained from malloc. +** Opcode 5 works on the linear array only, not on the Bitvec. +** Opcode 5 is used to deliberately induce a fault in order to +** confirm that error detection works. +** +** At the conclusion of the test the linear array is compared +** against the Bitvec object. If there are any differences, +** an error is returned. If they are the same, zero is returned. +** +** If a memory allocation error occurs, return -1. +*/ +SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){ + Bitvec *pBitvec = 0; + unsigned char *pV = 0; + int rc = -1; + int i, nx, pc, op; + void *pTmpSpace; + + /* Allocate the Bitvec to be tested and a linear array of + ** bits to act as the reference */ + pBitvec = sqlite3BitvecCreate( sz ); + pV = sqlite3MallocZero( (sz+7)/8 + 1 ); + pTmpSpace = sqlite3_malloc64(BITVEC_SZ); + if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; + + /* NULL pBitvec tests */ + sqlite3BitvecSet(0, 1); + sqlite3BitvecClear(0, 1, pTmpSpace); + + /* Run the program */ + pc = 0; + while( (op = aOp[pc])!=0 ){ + switch( op ){ + case 1: + case 2: + case 5: { + nx = 4; + i = aOp[pc+2] - 1; + aOp[pc+2] += aOp[pc+3]; + break; + } + case 3: + case 4: + default: { + nx = 2; + sqlite3_randomness(sizeof(i), &i); + break; + } + } + if( (--aOp[pc+1]) > 0 ) nx = 0; + pc += nx; + i = (i & 0x7fffffff)%sz; + if( (op & 1)!=0 ){ + SETBIT(pV, (i+1)); + if( op!=5 ){ + if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end; + } + }else{ + CLEARBIT(pV, (i+1)); + sqlite3BitvecClear(pBitvec, i+1, pTmpSpace); + } + } + + /* Test to make sure the linear array exactly matches the + ** Bitvec object. Start with the assumption that they do + ** match (rc==0). Change rc to non-zero if a discrepancy + ** is found. + */ + rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1) + + sqlite3BitvecTest(pBitvec, 0) + + (sqlite3BitvecSize(pBitvec) - sz); + for(i=1; i<=sz; i++){ + if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){ + rc = i; + break; + } + } + + /* Free allocated structure */ +bitvec_end: + sqlite3_free(pTmpSpace); + sqlite3_free(pV); + sqlite3BitvecDestroy(pBitvec); + return rc; +} +#endif /* SQLITE_OMIT_BUILTIN_TEST */ + +/************** End of bitvec.c **********************************************/ +/************** Begin file pcache.c ******************************************/ +/* +** 2008 August 05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements that page cache. +*/ +/* #include "sqliteInt.h" */ + +/* +** A complete page cache is an instance of this structure. Every +** entry in the cache holds a single page of the database file. The +** btree layer only operates on the cached copy of the database pages. +** +** A page cache entry is "clean" if it exactly matches what is currently +** on disk. A page is "dirty" if it has been modified and needs to be +** persisted to disk. +** +** pDirty, pDirtyTail, pSynced: +** All dirty pages are linked into the doubly linked list using +** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order +** such that p was added to the list more recently than p->pDirtyNext. +** PCache.pDirty points to the first (newest) element in the list and +** pDirtyTail to the last (oldest). +** +** The PCache.pSynced variable is used to optimize searching for a dirty +** page to eject from the cache mid-transaction. It is better to eject +** a page that does not require a journal sync than one that does. +** Therefore, pSynced is maintained to that it *almost* always points +** to either the oldest page in the pDirty/pDirtyTail list that has a +** clear PGHDR_NEED_SYNC flag or to a page that is older than this one +** (so that the right page to eject can be found by following pDirtyPrev +** pointers). +*/ +struct PCache { + PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ + PgHdr *pSynced; /* Last synced page in dirty page list */ + int nRefSum; /* Sum of ref counts over all pages */ + int szCache; /* Configured cache size */ + int szSpill; /* Size before spilling occurs */ + int szPage; /* Size of every page in this cache */ + int szExtra; /* Size of extra space for each page */ + u8 bPurgeable; /* True if pages are on backing store */ + u8 eCreate; /* eCreate value for for xFetch() */ + int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ + void *pStress; /* Argument to xStress */ + sqlite3_pcache *pCache; /* Pluggable cache module */ +}; + +/********************************** Test and Debug Logic **********************/ +/* +** Debug tracing macros. Enable by by changing the "0" to "1" and +** recompiling. +** +** When sqlite3PcacheTrace is 1, single line trace messages are issued. +** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries +** is displayed for many operations, resulting in a lot of output. +*/ +#if defined(SQLITE_DEBUG) && 0 + int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ + int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ +# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} + void pcacheDump(PCache *pCache){ + int N; + int i, j; + sqlite3_pcache_page *pLower; + PgHdr *pPg; + unsigned char *a; + + if( sqlite3PcacheTrace<2 ) return; + if( pCache->pCache==0 ) return; + N = sqlite3PcachePagecount(pCache); + if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; + for(i=1; i<=N; i++){ + pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); + if( pLower==0 ) continue; + pPg = (PgHdr*)pLower->pExtra; + printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); + a = (unsigned char *)pLower->pBuf; + for(j=0; j<12; j++) printf("%02x", a[j]); + printf("\n"); + if( pPg->pPage==0 ){ + sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); + } + } + } + #else +# define pcacheTrace(X) +# define pcacheDump(X) +#endif + +/* +** Check invariants on a PgHdr entry. Return true if everything is OK. +** Return false if any invariant is violated. +** +** This routine is for use inside of assert() statements only. For +** example: +** +** assert( sqlite3PcachePageSanity(pPg) ); +*/ +#if SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){ + PCache *pCache; + assert( pPg!=0 ); + assert( pPg->pgno>0 ); /* Page number is 1 or more */ + pCache = pPg->pCache; + assert( pCache!=0 ); /* Every page has an associated PCache */ + if( pPg->flags & PGHDR_CLEAN ){ + assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ + assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */ + assert( pCache->pDirtyTail!=pPg ); + } + /* WRITEABLE pages must also be DIRTY */ + if( pPg->flags & PGHDR_WRITEABLE ){ + assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ + } + /* NEED_SYNC can be set independently of WRITEABLE. This can happen, + ** for example, when using the sqlite3PagerDontWrite() optimization: + ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK. + ** (2) Page X moved to freelist, WRITEABLE is cleared + ** (3) Page X reused, WRITEABLE is set again + ** If NEED_SYNC had been cleared in step 2, then it would not be reset + ** in step 3, and page might be written into the database without first + ** syncing the rollback journal, which might cause corruption on a power + ** loss. + ** + ** Another example is when the database page size is smaller than the + ** disk sector size. When any page of a sector is journalled, all pages + ** in that sector are marked NEED_SYNC even if they are still CLEAN, just + ** in case they are later modified, since all pages in the same sector + ** must be journalled and synced before any of those pages can be safely + ** written. + */ + return 1; +} +#endif /* SQLITE_DEBUG */ + + +/********************************** Linked List Management ********************/ + +/* Allowed values for second argument to pcacheManageDirtyList() */ +#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */ +#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */ +#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */ + +/* +** Manage pPage's participation on the dirty list. Bits of the addRemove +** argument determines what operation to do. The 0x01 bit means first +** remove pPage from the dirty list. The 0x02 means add pPage back to +** the dirty list. Doing both moves pPage to the front of the dirty list. +*/ +static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ + PCache *p = pPage->pCache; + + pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, + addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT", + pPage->pgno)); + if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ + assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); + assert( pPage->pDirtyPrev || pPage==p->pDirty ); + + /* Update the PCache1.pSynced variable if necessary. */ + if( p->pSynced==pPage ){ + p->pSynced = pPage->pDirtyPrev; + } + + if( pPage->pDirtyNext ){ + pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; + }else{ + assert( pPage==p->pDirtyTail ); + p->pDirtyTail = pPage->pDirtyPrev; + } + if( pPage->pDirtyPrev ){ + pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; + }else{ + /* If there are now no dirty pages in the cache, set eCreate to 2. + ** This is an optimization that allows sqlite3PcacheFetch() to skip + ** searching for a dirty page to eject from the cache when it might + ** otherwise have to. */ + assert( pPage==p->pDirty ); + p->pDirty = pPage->pDirtyNext; + assert( p->bPurgeable || p->eCreate==2 ); + if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/ + assert( p->bPurgeable==0 || p->eCreate==1 ); + p->eCreate = 2; + } + } + pPage->pDirtyNext = 0; + pPage->pDirtyPrev = 0; + } + if( addRemove & PCACHE_DIRTYLIST_ADD ){ + assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); + + pPage->pDirtyNext = p->pDirty; + if( pPage->pDirtyNext ){ + assert( pPage->pDirtyNext->pDirtyPrev==0 ); + pPage->pDirtyNext->pDirtyPrev = pPage; + }else{ + p->pDirtyTail = pPage; + if( p->bPurgeable ){ + assert( p->eCreate==2 ); + p->eCreate = 1; + } + } + p->pDirty = pPage; + + /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set + ** pSynced to point to it. Checking the NEED_SYNC flag is an + ** optimization, as if pSynced points to a page with the NEED_SYNC + ** flag set sqlite3PcacheFetchStress() searches through all newer + ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ + if( !p->pSynced + && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/ + ){ + p->pSynced = pPage; + } + } + pcacheDump(p); +} + +/* +** Wrapper around the pluggable caches xUnpin method. If the cache is +** being used for an in-memory database, this function is a no-op. +*/ +static void pcacheUnpin(PgHdr *p){ + if( p->pCache->bPurgeable ){ + pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno)); + sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); + pcacheDump(p->pCache); + } +} + +/* +** Compute the number of pages of cache requested. p->szCache is the +** cache size requested by the "PRAGMA cache_size" statement. +*/ +static int numberOfCachePages(PCache *p){ + if( p->szCache>=0 ){ + /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the + ** suggested cache size is set to N. */ + return p->szCache; + }else{ + /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then + ** the number of cache pages is adjusted to use approximately abs(N*1024) + ** bytes of memory. */ + return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); + } +} + +/*************************************************** General Interfaces ****** +** +** Initialize and shutdown the page cache subsystem. Neither of these +** functions are threadsafe. +*/ +SQLITE_PRIVATE int sqlite3PcacheInitialize(void){ + if( sqlite3GlobalConfig.pcache2.xInit==0 ){ + /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the + ** built-in default page cache is used instead of the application defined + ** page cache. */ + sqlite3PCacheSetDefault(); + } + return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); +} +SQLITE_PRIVATE void sqlite3PcacheShutdown(void){ + if( sqlite3GlobalConfig.pcache2.xShutdown ){ + /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ + sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); + } +} + +/* +** Return the size in bytes of a PCache object. +*/ +SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); } + +/* +** Create a new PCache object. Storage space to hold the object +** has already been allocated and is passed in as the p pointer. +** The caller discovers how much space needs to be allocated by +** calling sqlite3PcacheSize(). +*/ +SQLITE_PRIVATE int sqlite3PcacheOpen( + int szPage, /* Size of every page */ + int szExtra, /* Extra space associated with each page */ + int bPurgeable, /* True if pages are on backing store */ + int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ + void *pStress, /* Argument to xStress */ + PCache *p /* Preallocated space for the PCache */ +){ + memset(p, 0, sizeof(PCache)); + p->szPage = 1; + p->szExtra = szExtra; + p->bPurgeable = bPurgeable; + p->eCreate = 2; + p->xStress = xStress; + p->pStress = pStress; + p->szCache = 100; + p->szSpill = 1; + pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable)); + return sqlite3PcacheSetPageSize(p, szPage); +} + +/* +** Change the page size for PCache object. The caller must ensure that there +** are no outstanding page references when this function is called. +*/ +SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ + assert( pCache->nRefSum==0 && pCache->pDirty==0 ); + if( pCache->szPage ){ + sqlite3_pcache *pNew; + pNew = sqlite3GlobalConfig.pcache2.xCreate( + szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), + pCache->bPurgeable + ); + if( pNew==0 ) return SQLITE_NOMEM_BKPT; + sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); + if( pCache->pCache ){ + sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); + } + pCache->pCache = pNew; + pCache->szPage = szPage; + pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage)); + } + return SQLITE_OK; +} + +/* +** Try to obtain a page from the cache. +** +** This routine returns a pointer to an sqlite3_pcache_page object if +** such an object is already in cache, or if a new one is created. +** This routine returns a NULL pointer if the object was not in cache +** and could not be created. +** +** The createFlags should be 0 to check for existing pages and should +** be 3 (not 1, but 3) to try to create a new page. +** +** If the createFlag is 0, then NULL is always returned if the page +** is not already in the cache. If createFlag is 1, then a new page +** is created only if that can be done without spilling dirty pages +** and without exceeding the cache size limit. +** +** The caller needs to invoke sqlite3PcacheFetchFinish() to properly +** initialize the sqlite3_pcache_page object and convert it into a +** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() +** routines are split this way for performance reasons. When separated +** they can both (usually) operate without having to push values to +** the stack on entry and pop them back off on exit, which saves a +** lot of pushing and popping. +*/ +SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch( + PCache *pCache, /* Obtain the page from this cache */ + Pgno pgno, /* Page number to obtain */ + int createFlag /* If true, create page if it does not exist already */ +){ + int eCreate; + sqlite3_pcache_page *pRes; + + assert( pCache!=0 ); + assert( pCache->pCache!=0 ); + assert( createFlag==3 || createFlag==0 ); + assert( pgno>0 ); + assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) ); + + /* eCreate defines what to do if the page does not exist. + ** 0 Do not allocate a new page. (createFlag==0) + ** 1 Allocate a new page if doing so is inexpensive. + ** (createFlag==1 AND bPurgeable AND pDirty) + ** 2 Allocate a new page even it doing so is difficult. + ** (createFlag==1 AND !(bPurgeable AND pDirty) + */ + eCreate = createFlag & pCache->eCreate; + assert( eCreate==0 || eCreate==1 || eCreate==2 ); + assert( createFlag==0 || pCache->eCreate==eCreate ); + assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); + pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); + pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno, + createFlag?" create":"",pRes)); + return pRes; +} + +/* +** If the sqlite3PcacheFetch() routine is unable to allocate a new +** page because no clean pages are available for reuse and the cache +** size limit has been reached, then this routine can be invoked to +** try harder to allocate a page. This routine might invoke the stress +** callback to spill dirty pages to the journal. It will then try to +** allocate the new page and will only fail to allocate a new page on +** an OOM error. +** +** This routine should be invoked only after sqlite3PcacheFetch() fails. +*/ +SQLITE_PRIVATE int sqlite3PcacheFetchStress( + PCache *pCache, /* Obtain the page from this cache */ + Pgno pgno, /* Page number to obtain */ + sqlite3_pcache_page **ppPage /* Write result here */ +){ + PgHdr *pPg; + if( pCache->eCreate==2 ) return 0; + + if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){ + /* Find a dirty page to write-out and recycle. First try to find a + ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC + ** cleared), but if that is not possible settle for any other + ** unreferenced dirty page. + ** + ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC + ** flag is currently referenced, then the following may leave pSynced + ** set incorrectly (pointing to other than the LRU page with NEED_SYNC + ** cleared). This is Ok, as pSynced is just an optimization. */ + for(pPg=pCache->pSynced; + pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); + pPg=pPg->pDirtyPrev + ); + pCache->pSynced = pPg; + if( !pPg ){ + for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); + } + if( pPg ){ + int rc; +#ifdef SQLITE_LOG_CACHE_SPILL + sqlite3_log(SQLITE_FULL, + "spill page %d making room for %d - cache used: %d/%d", + pPg->pgno, pgno, + sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), + numberOfCachePages(pCache)); +#endif + pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno)); + rc = pCache->xStress(pCache->pStress, pPg); + pcacheDump(pCache); + if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ + return rc; + } + } + } + *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); + return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; +} + +/* +** This is a helper routine for sqlite3PcacheFetchFinish() +** +** In the uncommon case where the page being fetched has not been +** initialized, this routine is invoked to do the initialization. +** This routine is broken out into a separate function since it +** requires extra stack manipulation that can be avoided in the common +** case. +*/ +static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( + PCache *pCache, /* Obtain the page from this cache */ + Pgno pgno, /* Page number obtained */ + sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ +){ + PgHdr *pPgHdr; + assert( pPage!=0 ); + pPgHdr = (PgHdr*)pPage->pExtra; + assert( pPgHdr->pPage==0 ); + memset(pPgHdr, 0, sizeof(PgHdr)); + pPgHdr->pPage = pPage; + pPgHdr->pData = pPage->pBuf; + pPgHdr->pExtra = (void *)&pPgHdr[1]; + memset(pPgHdr->pExtra, 0, pCache->szExtra); + pPgHdr->pCache = pCache; + pPgHdr->pgno = pgno; + pPgHdr->flags = PGHDR_CLEAN; + return sqlite3PcacheFetchFinish(pCache,pgno,pPage); +} + +/* +** This routine converts the sqlite3_pcache_page object returned by +** sqlite3PcacheFetch() into an initialized PgHdr object. This routine +** must be called after sqlite3PcacheFetch() in order to get a usable +** result. +*/ +SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish( + PCache *pCache, /* Obtain the page from this cache */ + Pgno pgno, /* Page number obtained */ + sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ +){ + PgHdr *pPgHdr; + + assert( pPage!=0 ); + pPgHdr = (PgHdr *)pPage->pExtra; + + if( !pPgHdr->pPage ){ + return pcacheFetchFinishWithInit(pCache, pgno, pPage); + } + pCache->nRefSum++; + pPgHdr->nRef++; + assert( sqlite3PcachePageSanity(pPgHdr) ); + return pPgHdr; +} + +/* +** Decrement the reference count on a page. If the page is clean and the +** reference count drops to 0, then it is made eligible for recycling. +*/ +SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ + assert( p->nRef>0 ); + p->pCache->nRefSum--; + if( (--p->nRef)==0 ){ + if( p->flags&PGHDR_CLEAN ){ + pcacheUnpin(p); + }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* Move the page to the head of the dirty list. If p->pDirtyPrev==0, + ** then page p is already at the head of the dirty list and the + ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE + ** tag above. */ + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); + } + } +} + +/* +** Increase the reference count of a supplied page by 1. +*/ +SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){ + assert(p->nRef>0); + assert( sqlite3PcachePageSanity(p) ); + p->nRef++; + p->pCache->nRefSum++; +} + +/* +** Drop a page from the cache. There must be exactly one reference to the +** page. This function deletes that reference, so after it returns the +** page pointed to by p is invalid. +*/ +SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){ + assert( p->nRef==1 ); + assert( sqlite3PcachePageSanity(p) ); + if( p->flags&PGHDR_DIRTY ){ + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); + } + p->pCache->nRefSum--; + sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); +} + +/* +** Make sure the page is marked as dirty. If it isn't dirty already, +** make it so. +*/ +SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ + assert( p->nRef>0 ); + assert( sqlite3PcachePageSanity(p) ); + if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ + p->flags &= ~PGHDR_DONT_WRITE; + if( p->flags & PGHDR_CLEAN ){ + p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); + pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); + assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); + } + assert( sqlite3PcachePageSanity(p) ); + } +} + +/* +** Make sure the page is marked as clean. If it isn't clean already, +** make it so. +*/ +SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ + assert( sqlite3PcachePageSanity(p) ); + if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){ + assert( (p->flags & PGHDR_CLEAN)==0 ); + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); + p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); + p->flags |= PGHDR_CLEAN; + pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno)); + assert( sqlite3PcachePageSanity(p) ); + if( p->nRef==0 ){ + pcacheUnpin(p); + } + } +} + +/* +** Make every page in the cache clean. +*/ +SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){ + PgHdr *p; + pcacheTrace(("%p.CLEAN-ALL\n",pCache)); + while( (p = pCache->pDirty)!=0 ){ + sqlite3PcacheMakeClean(p); + } +} + +/* +** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. +*/ +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){ + PgHdr *p; + pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache)); + for(p=pCache->pDirty; p; p=p->pDirtyNext){ + p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE); + } + pCache->pSynced = pCache->pDirtyTail; +} + +/* +** Clear the PGHDR_NEED_SYNC flag from all dirty pages. +*/ +SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){ + PgHdr *p; + for(p=pCache->pDirty; p; p=p->pDirtyNext){ + p->flags &= ~PGHDR_NEED_SYNC; + } + pCache->pSynced = pCache->pDirtyTail; +} + +/* +** Change the page number of page p to newPgno. +*/ +SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ + PCache *pCache = p->pCache; + assert( p->nRef>0 ); + assert( newPgno>0 ); + assert( sqlite3PcachePageSanity(p) ); + pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); + sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); + p->pgno = newPgno; + if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); + } +} + +/* +** Drop every cache entry whose page number is greater than "pgno". The +** caller must ensure that there are no outstanding references to any pages +** other than page 1 with a page number greater than pgno. +** +** If there is a reference to page 1 and the pgno parameter passed to this +** function is 0, then the data area associated with page 1 is zeroed, but +** the page object is not dropped. +*/ +SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ + if( pCache->pCache ){ + PgHdr *p; + PgHdr *pNext; + pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno)); + for(p=pCache->pDirty; p; p=pNext){ + pNext = p->pDirtyNext; + /* This routine never gets call with a positive pgno except right + ** after sqlite3PcacheCleanAll(). So if there are dirty pages, + ** it must be that pgno==0. + */ + assert( p->pgno>0 ); + if( p->pgno>pgno ){ + assert( p->flags&PGHDR_DIRTY ); + sqlite3PcacheMakeClean(p); + } + } + if( pgno==0 && pCache->nRefSum ){ + sqlite3_pcache_page *pPage1; + pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); + if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because + ** pCache->nRefSum>0 */ + memset(pPage1->pBuf, 0, pCache->szPage); + pgno = 1; + } + } + sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); + } +} + +/* +** Close a cache. +*/ +SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){ + assert( pCache->pCache!=0 ); + pcacheTrace(("%p.CLOSE\n",pCache)); + sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); +} + +/* +** Discard the contents of the cache. +*/ +SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){ + sqlite3PcacheTruncate(pCache, 0); +} + +/* +** Merge two lists of pages connected by pDirty and in pgno order. +** Do not both fixing the pDirtyPrev pointers. +*/ +static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ + PgHdr result, *pTail; + pTail = &result; + while( pA && pB ){ + if( pA->pgnopgno ){ + pTail->pDirty = pA; + pTail = pA; + pA = pA->pDirty; + }else{ + pTail->pDirty = pB; + pTail = pB; + pB = pB->pDirty; + } + } + if( pA ){ + pTail->pDirty = pA; + }else if( pB ){ + pTail->pDirty = pB; + }else{ + pTail->pDirty = 0; + } + return result.pDirty; +} + +/* +** Sort the list of pages in accending order by pgno. Pages are +** connected by pDirty pointers. The pDirtyPrev pointers are +** corrupted by this sort. +** +** Since there cannot be more than 2^31 distinct pages in a database, +** there cannot be more than 31 buckets required by the merge sorter. +** One extra bucket is added to catch overflow in case something +** ever changes to make the previous sentence incorrect. +*/ +#define N_SORT_BUCKET 32 +static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ + PgHdr *a[N_SORT_BUCKET], *p; + int i; + memset(a, 0, sizeof(a)); + while( pIn ){ + p = pIn; + pIn = p->pDirty; + p->pDirty = 0; + for(i=0; ALWAYS(ipDirty; p; p=p->pDirtyNext){ + p->pDirty = p->pDirtyNext; + } + return pcacheSortDirtyList(pCache->pDirty); +} + +/* +** Return the total number of references to all pages held by the cache. +** +** This is not the total number of pages referenced, but the sum of the +** reference count for all pages. +*/ +SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){ + return pCache->nRefSum; +} + +/* +** Return the number of references to the page supplied as an argument. +*/ +SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){ + return p->nRef; +} + +/* +** Return the total number of pages in the cache. +*/ +SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){ + assert( pCache->pCache!=0 ); + return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); +} + +#ifdef SQLITE_TEST +/* +** Get the suggested cache-size value. +*/ +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){ + return numberOfCachePages(pCache); +} +#endif + +/* +** Set the suggested cache-size value. +*/ +SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ + assert( pCache->pCache!=0 ); + pCache->szCache = mxPage; + sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, + numberOfCachePages(pCache)); +} + +/* +** Set the suggested cache-spill value. Make no changes if if the +** argument is zero. Return the effective cache-spill size, which will +** be the larger of the szSpill and szCache. +*/ +SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){ + int res; + assert( p->pCache!=0 ); + if( mxPage ){ + if( mxPage<0 ){ + mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra)); + } + p->szSpill = mxPage; + } + res = numberOfCachePages(p); + if( resszSpill ) res = p->szSpill; + return res; +} + +/* +** Free up as much memory as possible from the page cache. +*/ +SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ + assert( pCache->pCache!=0 ); + sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); +} + +/* +** Return the size of the header added by this middleware layer +** in the page-cache hierarchy. +*/ +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); } + +/* +** Return the number of dirty pages currently in the cache, as a percentage +** of the configured cache size. +*/ +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){ + PgHdr *pDirty; + int nDirty = 0; + int nCache = numberOfCachePages(pCache); + for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; + return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; +} + +#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) +/* +** For all dirty pages currently in the cache, invoke the specified +** callback. This is only used if the SQLITE_CHECK_PAGES macro is +** defined. +*/ +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){ + PgHdr *pDirty; + for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ + xIter(pDirty); + } +} +#endif + +/************** End of pcache.c **********************************************/ +/************** Begin file pcache1.c *****************************************/ +/* +** 2008 November 05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements the default page cache implementation (the +** sqlite3_pcache interface). It also contains part of the implementation +** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features. +** If the default page cache implementation is overridden, then neither of +** these two features are available. +** +** A Page cache line looks like this: +** +** ------------------------------------------------------------- +** | database page content | PgHdr1 | MemPage | PgHdr | +** ------------------------------------------------------------- +** +** The database page content is up front (so that buffer overreads tend to +** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage +** is the extension added by the btree.c module containing information such +** as the database page number and how that database page is used. PgHdr +** is added by the pcache.c layer and contains information used to keep track +** of which pages are "dirty". PgHdr1 is an extension added by this +** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page. +** PgHdr1 contains information needed to look up a page by its page number. +** The superclass sqlite3_pcache_page.pBuf points to the start of the +** database page content and sqlite3_pcache_page.pExtra points to PgHdr. +** +** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at +** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The +** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this +** size can vary according to architecture, compile-time options, and +** SQLite library version number. +** +** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained +** using a separate memory allocation from the database page content. This +** seeks to overcome the "clownshoe" problem (also called "internal +** fragmentation" in academic literature) of allocating a few bytes more +** than a power of two with the memory allocator rounding up to the next +** power of two, and leaving the rounded-up space unused. +** +** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates +** with this module. Information is passed back and forth as PgHdr1 pointers. +** +** The pcache.c and pager.c modules deal pointers to PgHdr objects. +** The btree.c module deals with pointers to MemPage objects. +** +** SOURCE OF PAGE CACHE MEMORY: +** +** Memory for a page might come from any of three sources: +** +** (1) The general-purpose memory allocator - sqlite3Malloc() +** (2) Global page-cache memory provided using sqlite3_config() with +** SQLITE_CONFIG_PAGECACHE. +** (3) PCache-local bulk allocation. +** +** The third case is a chunk of heap memory (defaulting to 100 pages worth) +** that is allocated when the page cache is created. The size of the local +** bulk allocation can be adjusted using +** +** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N). +** +** If N is positive, then N pages worth of memory are allocated using a single +** sqlite3Malloc() call and that memory is used for the first N pages allocated. +** Or if N is negative, then -1024*N bytes of memory are allocated and used +** for as many pages as can be accomodated. +** +** Only one of (2) or (3) can be used. Once the memory available to (2) or +** (3) is exhausted, subsequent allocations fail over to the general-purpose +** memory allocator (1). +** +** Earlier versions of SQLite used only methods (1) and (2). But experiments +** show that method (3) with N==100 provides about a 5% performance boost for +** common workloads. +*/ +/* #include "sqliteInt.h" */ + +typedef struct PCache1 PCache1; +typedef struct PgHdr1 PgHdr1; +typedef struct PgFreeslot PgFreeslot; +typedef struct PGroup PGroup; + +/* +** Each cache entry is represented by an instance of the following +** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of +** PgHdr1.pCache->szPage bytes is allocated directly before this structure +** in memory. +*/ +struct PgHdr1 { + sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ + unsigned int iKey; /* Key value (page number) */ + u8 isPinned; /* Page in use, not on the LRU list */ + u8 isBulkLocal; /* This page from bulk local storage */ + u8 isAnchor; /* This is the PGroup.lru element */ + PgHdr1 *pNext; /* Next in hash table chain */ + PCache1 *pCache; /* Cache that currently owns this page */ + PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ + PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ +}; + +/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set +** of one or more PCaches that are able to recycle each other's unpinned +** pages when they are under memory pressure. A PGroup is an instance of +** the following object. +** +** This page cache implementation works in one of two modes: +** +** (1) Every PCache is the sole member of its own PGroup. There is +** one PGroup per PCache. +** +** (2) There is a single global PGroup that all PCaches are a member +** of. +** +** Mode 1 uses more memory (since PCache instances are not able to rob +** unused pages from other PCaches) but it also operates without a mutex, +** and is therefore often faster. Mode 2 requires a mutex in order to be +** threadsafe, but recycles pages more efficiently. +** +** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single +** PGroup which is the pcache1.grp global variable and its mutex is +** SQLITE_MUTEX_STATIC_LRU. +*/ +struct PGroup { + sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */ + unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ + unsigned int nMinPage; /* Sum of nMin for purgeable caches */ + unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ + unsigned int nCurrentPage; /* Number of purgeable pages allocated */ + PgHdr1 lru; /* The beginning and end of the LRU list */ +}; + +/* Each page cache is an instance of the following object. Every +** open database file (including each in-memory database and each +** temporary or transient database) has a single page cache which +** is an instance of this object. +** +** Pointers to structures of this type are cast and returned as +** opaque sqlite3_pcache* handles. +*/ +struct PCache1 { + /* Cache configuration parameters. Page size (szPage) and the purgeable + ** flag (bPurgeable) are set when the cache is created. nMax may be + ** modified at any time by a call to the pcache1Cachesize() method. + ** The PGroup mutex must be held when accessing nMax. + */ + PGroup *pGroup; /* PGroup this cache belongs to */ + int szPage; /* Size of database content section */ + int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */ + int szAlloc; /* Total size of one pcache line */ + int bPurgeable; /* True if cache is purgeable */ + unsigned int nMin; /* Minimum number of pages reserved */ + unsigned int nMax; /* Configured "cache_size" value */ + unsigned int n90pct; /* nMax*9/10 */ + unsigned int iMaxKey; /* Largest key seen since xTruncate() */ + + /* Hash table of all pages. The following variables may only be accessed + ** when the accessor is holding the PGroup mutex. + */ + unsigned int nRecyclable; /* Number of pages in the LRU list */ + unsigned int nPage; /* Total number of pages in apHash */ + unsigned int nHash; /* Number of slots in apHash[] */ + PgHdr1 **apHash; /* Hash table for fast lookup by key */ + PgHdr1 *pFree; /* List of unused pcache-local pages */ + void *pBulk; /* Bulk memory used by pcache-local */ +}; + +/* +** Free slots in the allocator used to divide up the global page cache +** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism. +*/ +struct PgFreeslot { + PgFreeslot *pNext; /* Next free slot */ +}; + +/* +** Global data used by this cache. +*/ +static SQLITE_WSD struct PCacheGlobal { + PGroup grp; /* The global PGroup for mode (2) */ + + /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The + ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all + ** fixed at sqlite3_initialize() time and do not require mutex protection. + ** The nFreeSlot and pFree values do require mutex protection. + */ + int isInit; /* True if initialized */ + int separateCache; /* Use a new PGroup for each PCache */ + int nInitPage; /* Initial bulk allocation size */ + int szSlot; /* Size of each free slot */ + int nSlot; /* The number of pcache slots */ + int nReserve; /* Try to keep nFreeSlot above this */ + void *pStart, *pEnd; /* Bounds of global page cache memory */ + /* Above requires no mutex. Use mutex below for variable that follow. */ + sqlite3_mutex *mutex; /* Mutex for accessing the following: */ + PgFreeslot *pFree; /* Free page blocks */ + int nFreeSlot; /* Number of unused pcache slots */ + /* The following value requires a mutex to change. We skip the mutex on + ** reading because (1) most platforms read a 32-bit integer atomically and + ** (2) even if an incorrect value is read, no great harm is done since this + ** is really just an optimization. */ + int bUnderPressure; /* True if low on PAGECACHE memory */ +} pcache1_g; + +/* +** All code in this file should access the global structure above via the +** alias "pcache1". This ensures that the WSD emulation is used when +** compiling for systems that do not support real WSD. +*/ +#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g)) + +/* +** Macros to enter and leave the PCache LRU mutex. +*/ +#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 +# define pcache1EnterMutex(X) assert((X)->mutex==0) +# define pcache1LeaveMutex(X) assert((X)->mutex==0) +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 +#else +# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) +# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 +#endif + +/******************************************************************************/ +/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ + + +/* +** This function is called during initialization if a static buffer is +** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE +** verb to sqlite3_config(). Parameter pBuf points to an allocation large +** enough to contain 'n' buffers of 'sz' bytes each. +** +** This routine is called from sqlite3_initialize() and so it is guaranteed +** to be serialized already. There is no need for further mutexing. +*/ +SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ + if( pcache1.isInit ){ + PgFreeslot *p; + if( pBuf==0 ) sz = n = 0; + sz = ROUNDDOWN8(sz); + pcache1.szSlot = sz; + pcache1.nSlot = pcache1.nFreeSlot = n; + pcache1.nReserve = n>90 ? 10 : (n/10 + 1); + pcache1.pStart = pBuf; + pcache1.pFree = 0; + pcache1.bUnderPressure = 0; + while( n-- ){ + p = (PgFreeslot*)pBuf; + p->pNext = pcache1.pFree; + pcache1.pFree = p; + pBuf = (void*)&((char*)pBuf)[sz]; + } + pcache1.pEnd = pBuf; + } +} + +/* +** Try to initialize the pCache->pFree and pCache->pBulk fields. Return +** true if pCache->pFree ends up containing one or more free pages. +*/ +static int pcache1InitBulk(PCache1 *pCache){ + i64 szBulk; + char *zBulk; + if( pcache1.nInitPage==0 ) return 0; + /* Do not bother with a bulk allocation if the cache size very small */ + if( pCache->nMax<3 ) return 0; + sqlite3BeginBenignMalloc(); + if( pcache1.nInitPage>0 ){ + szBulk = pCache->szAlloc * (i64)pcache1.nInitPage; + }else{ + szBulk = -1024 * (i64)pcache1.nInitPage; + } + if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ + szBulk = pCache->szAlloc*pCache->nMax; + } + zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); + sqlite3EndBenignMalloc(); + if( zBulk ){ + int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; + int i; + for(i=0; iszPage]; + pX->page.pBuf = zBulk; + pX->page.pExtra = &pX[1]; + pX->isBulkLocal = 1; + pX->isAnchor = 0; + pX->pNext = pCache->pFree; + pCache->pFree = pX; + zBulk += pCache->szAlloc; + } + } + return pCache->pFree!=0; +} + +/* +** Malloc function used within this file to allocate space from the buffer +** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no +** such buffer exists or there is no space left in it, this function falls +** back to sqlite3Malloc(). +** +** Multiple threads can run this routine at the same time. Global variables +** in pcache1 need to be protected via mutex. +*/ +static void *pcache1Alloc(int nByte){ + void *p = 0; + assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); + if( nByte<=pcache1.szSlot ){ + sqlite3_mutex_enter(pcache1.mutex); + p = (PgHdr1 *)pcache1.pFree; + if( p ){ + pcache1.pFree = pcache1.pFree->pNext; + pcache1.nFreeSlot--; + pcache1.bUnderPressure = pcache1.nFreeSlot=0 ); + sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); + } + sqlite3_mutex_leave(pcache1.mutex); + } + if( p==0 ){ + /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get + ** it from sqlite3Malloc instead. + */ + p = sqlite3Malloc(nByte); +#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS + if( p ){ + int sz = sqlite3MallocSize(p); + sqlite3_mutex_enter(pcache1.mutex); + sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); + sqlite3_mutex_leave(pcache1.mutex); + } +#endif + sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); + } + return p; +} + +/* +** Free an allocated buffer obtained from pcache1Alloc(). +*/ +static void pcache1Free(void *p){ + if( p==0 ) return; + if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){ + PgFreeslot *pSlot; + sqlite3_mutex_enter(pcache1.mutex); + sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); + pSlot = (PgFreeslot*)p; + pSlot->pNext = pcache1.pFree; + pcache1.pFree = pSlot; + pcache1.nFreeSlot++; + pcache1.bUnderPressure = pcache1.nFreeSlot=pcache1.pStart && ppGroup->mutex) ); + if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){ + p = pCache->pFree; + pCache->pFree = p->pNext; + p->pNext = 0; + }else{ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + /* The group mutex must be released before pcache1Alloc() is called. This + ** is because it might call sqlite3_release_memory(), which assumes that + ** this mutex is not held. */ + assert( pcache1.separateCache==0 ); + assert( pCache->pGroup==&pcache1.grp ); + pcache1LeaveMutex(pCache->pGroup); +#endif + if( benignMalloc ){ sqlite3BeginBenignMalloc(); } +#ifdef SQLITE_PCACHE_SEPARATE_HEADER + pPg = pcache1Alloc(pCache->szPage); + p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); + if( !pPg || !p ){ + pcache1Free(pPg); + sqlite3_free(p); + pPg = 0; + } +#else + pPg = pcache1Alloc(pCache->szAlloc); + p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; +#endif + if( benignMalloc ){ sqlite3EndBenignMalloc(); } +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + pcache1EnterMutex(pCache->pGroup); +#endif + if( pPg==0 ) return 0; + p->page.pBuf = pPg; + p->page.pExtra = &p[1]; + p->isBulkLocal = 0; + p->isAnchor = 0; + } + if( pCache->bPurgeable ){ + pCache->pGroup->nCurrentPage++; + } + return p; +} + +/* +** Free a page object allocated by pcache1AllocPage(). +*/ +static void pcache1FreePage(PgHdr1 *p){ + PCache1 *pCache; + assert( p!=0 ); + pCache = p->pCache; + assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); + if( p->isBulkLocal ){ + p->pNext = pCache->pFree; + pCache->pFree = p; + }else{ + pcache1Free(p->page.pBuf); +#ifdef SQLITE_PCACHE_SEPARATE_HEADER + sqlite3_free(p); +#endif + } + if( pCache->bPurgeable ){ + pCache->pGroup->nCurrentPage--; + } +} + +/* +** Malloc function used by SQLite to obtain space from the buffer configured +** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer +** exists, this function falls back to sqlite3Malloc(). +*/ +SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){ + return pcache1Alloc(sz); +} + +/* +** Free an allocated buffer obtained from sqlite3PageMalloc(). +*/ +SQLITE_PRIVATE void sqlite3PageFree(void *p){ + pcache1Free(p); +} + + +/* +** Return true if it desirable to avoid allocating a new page cache +** entry. +** +** If memory was allocated specifically to the page cache using +** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then +** it is desirable to avoid allocating a new page cache entry because +** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient +** for all page cache needs and we should not need to spill the +** allocation onto the heap. +** +** Or, the heap is used for all page cache memory but the heap is +** under memory pressure, then again it is desirable to avoid +** allocating a new page cache entry in order to avoid stressing +** the heap even further. +*/ +static int pcache1UnderMemoryPressure(PCache1 *pCache){ + if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){ + return pcache1.bUnderPressure; + }else{ + return sqlite3HeapNearlyFull(); + } +} + +/******************************************************************************/ +/******** General Implementation Functions ************************************/ + +/* +** This function is used to resize the hash table used by the cache passed +** as the first argument. +** +** The PCache mutex must be held when this function is called. +*/ +static void pcache1ResizeHash(PCache1 *p){ + PgHdr1 **apNew; + unsigned int nNew; + unsigned int i; + + assert( sqlite3_mutex_held(p->pGroup->mutex) ); + + nNew = p->nHash*2; + if( nNew<256 ){ + nNew = 256; + } + + pcache1LeaveMutex(p->pGroup); + if( p->nHash ){ sqlite3BeginBenignMalloc(); } + apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew); + if( p->nHash ){ sqlite3EndBenignMalloc(); } + pcache1EnterMutex(p->pGroup); + if( apNew ){ + for(i=0; inHash; i++){ + PgHdr1 *pPage; + PgHdr1 *pNext = p->apHash[i]; + while( (pPage = pNext)!=0 ){ + unsigned int h = pPage->iKey % nNew; + pNext = pPage->pNext; + pPage->pNext = apNew[h]; + apNew[h] = pPage; + } + } + sqlite3_free(p->apHash); + p->apHash = apNew; + p->nHash = nNew; + } +} + +/* +** This function is used internally to remove the page pPage from the +** PGroup LRU list, if is part of it. If pPage is not part of the PGroup +** LRU list, then this function is a no-op. +** +** The PGroup mutex must be held when this function is called. +*/ +static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ + PCache1 *pCache; + + assert( pPage!=0 ); + assert( pPage->isPinned==0 ); + pCache = pPage->pCache; + assert( pPage->pLruNext ); + assert( pPage->pLruPrev ); + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); + pPage->pLruPrev->pLruNext = pPage->pLruNext; + pPage->pLruNext->pLruPrev = pPage->pLruPrev; + pPage->pLruNext = 0; + pPage->pLruPrev = 0; + pPage->isPinned = 1; + assert( pPage->isAnchor==0 ); + assert( pCache->pGroup->lru.isAnchor==1 ); + pCache->nRecyclable--; + return pPage; +} + + +/* +** Remove the page supplied as an argument from the hash table +** (PCache1.apHash structure) that it is currently stored in. +** Also free the page if freePage is true. +** +** The PGroup mutex must be held when this function is called. +*/ +static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ + unsigned int h; + PCache1 *pCache = pPage->pCache; + PgHdr1 **pp; + + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); + h = pPage->iKey % pCache->nHash; + for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); + *pp = (*pp)->pNext; + + pCache->nPage--; + if( freeFlag ) pcache1FreePage(pPage); +} + +/* +** If there are currently more than nMaxPage pages allocated, try +** to recycle pages to reduce the number allocated to nMaxPage. +*/ +static void pcache1EnforceMaxPage(PCache1 *pCache){ + PGroup *pGroup = pCache->pGroup; + PgHdr1 *p; + assert( sqlite3_mutex_held(pGroup->mutex) ); + while( pGroup->nCurrentPage>pGroup->nMaxPage + && (p=pGroup->lru.pLruPrev)->isAnchor==0 + ){ + assert( p->pCache->pGroup==pGroup ); + assert( p->isPinned==0 ); + pcache1PinPage(p); + pcache1RemoveFromHash(p, 1); + } + if( pCache->nPage==0 && pCache->pBulk ){ + sqlite3_free(pCache->pBulk); + pCache->pBulk = pCache->pFree = 0; + } +} + +/* +** Discard all pages from cache pCache with a page number (key value) +** greater than or equal to iLimit. Any pinned pages that meet this +** criteria are unpinned before they are discarded. +** +** The PCache mutex must be held when this function is called. +*/ +static void pcache1TruncateUnsafe( + PCache1 *pCache, /* The cache to truncate */ + unsigned int iLimit /* Drop pages with this pgno or larger */ +){ + TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ + unsigned int h; + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); + for(h=0; hnHash; h++){ + PgHdr1 **pp = &pCache->apHash[h]; + PgHdr1 *pPage; + while( (pPage = *pp)!=0 ){ + if( pPage->iKey>=iLimit ){ + pCache->nPage--; + *pp = pPage->pNext; + if( !pPage->isPinned ) pcache1PinPage(pPage); + pcache1FreePage(pPage); + }else{ + pp = &pPage->pNext; + TESTONLY( nPage++; ) + } + } + } + assert( pCache->nPage==nPage ); +} + +/******************************************************************************/ +/******** sqlite3_pcache Methods **********************************************/ + +/* +** Implementation of the sqlite3_pcache.xInit method. +*/ +static int pcache1Init(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + assert( pcache1.isInit==0 ); + memset(&pcache1, 0, sizeof(pcache1)); + + + /* + ** The pcache1.separateCache variable is true if each PCache has its own + ** private PGroup (mode-1). pcache1.separateCache is false if the single + ** PGroup in pcache1.grp is used for all page caches (mode-2). + ** + ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT + ** + ** * Use a unified cache in single-threaded applications that have + ** configured a start-time buffer for use as page-cache memory using + ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL + ** pBuf argument. + ** + ** * Otherwise use separate caches (mode-1) + */ +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) + pcache1.separateCache = 0; +#elif SQLITE_THREADSAFE + pcache1.separateCache = sqlite3GlobalConfig.pPage==0 + || sqlite3GlobalConfig.bCoreMutex>0; +#else + pcache1.separateCache = sqlite3GlobalConfig.pPage==0; +#endif + +#if SQLITE_THREADSAFE + if( sqlite3GlobalConfig.bCoreMutex ){ + pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU); + pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM); + } +#endif + if( pcache1.separateCache + && sqlite3GlobalConfig.nPage!=0 + && sqlite3GlobalConfig.pPage==0 + ){ + pcache1.nInitPage = sqlite3GlobalConfig.nPage; + }else{ + pcache1.nInitPage = 0; + } + pcache1.grp.mxPinned = 10; + pcache1.isInit = 1; + return SQLITE_OK; +} + +/* +** Implementation of the sqlite3_pcache.xShutdown method. +** Note that the static mutex allocated in xInit does +** not need to be freed. +*/ +static void pcache1Shutdown(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + assert( pcache1.isInit!=0 ); + memset(&pcache1, 0, sizeof(pcache1)); +} + +/* forward declaration */ +static void pcache1Destroy(sqlite3_pcache *p); + +/* +** Implementation of the sqlite3_pcache.xCreate method. +** +** Allocate a new cache. +*/ +static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ + PCache1 *pCache; /* The newly created page cache */ + PGroup *pGroup; /* The group the new page cache will belong to */ + int sz; /* Bytes of memory required to allocate the new cache */ + + assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); + assert( szExtra < 300 ); + + sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache; + pCache = (PCache1 *)sqlite3MallocZero(sz); + if( pCache ){ + if( pcache1.separateCache ){ + pGroup = (PGroup*)&pCache[1]; + pGroup->mxPinned = 10; + }else{ + pGroup = &pcache1.grp; + } + if( pGroup->lru.isAnchor==0 ){ + pGroup->lru.isAnchor = 1; + pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru; + } + pCache->pGroup = pGroup; + pCache->szPage = szPage; + pCache->szExtra = szExtra; + pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1)); + pCache->bPurgeable = (bPurgeable ? 1 : 0); + pcache1EnterMutex(pGroup); + pcache1ResizeHash(pCache); + if( bPurgeable ){ + pCache->nMin = 10; + pGroup->nMinPage += pCache->nMin; + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; + } + pcache1LeaveMutex(pGroup); + if( pCache->nHash==0 ){ + pcache1Destroy((sqlite3_pcache*)pCache); + pCache = 0; + } + } + return (sqlite3_pcache *)pCache; +} + +/* +** Implementation of the sqlite3_pcache.xCachesize method. +** +** Configure the cache_size limit for a cache. +*/ +static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ + PCache1 *pCache = (PCache1 *)p; + if( pCache->bPurgeable ){ + PGroup *pGroup = pCache->pGroup; + pcache1EnterMutex(pGroup); + pGroup->nMaxPage += (nMax - pCache->nMax); + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; + pCache->nMax = nMax; + pCache->n90pct = pCache->nMax*9/10; + pcache1EnforceMaxPage(pCache); + pcache1LeaveMutex(pGroup); + } +} + +/* +** Implementation of the sqlite3_pcache.xShrink method. +** +** Free up as much memory as possible. +*/ +static void pcache1Shrink(sqlite3_pcache *p){ + PCache1 *pCache = (PCache1*)p; + if( pCache->bPurgeable ){ + PGroup *pGroup = pCache->pGroup; + int savedMaxPage; + pcache1EnterMutex(pGroup); + savedMaxPage = pGroup->nMaxPage; + pGroup->nMaxPage = 0; + pcache1EnforceMaxPage(pCache); + pGroup->nMaxPage = savedMaxPage; + pcache1LeaveMutex(pGroup); + } +} + +/* +** Implementation of the sqlite3_pcache.xPagecount method. +*/ +static int pcache1Pagecount(sqlite3_pcache *p){ + int n; + PCache1 *pCache = (PCache1*)p; + pcache1EnterMutex(pCache->pGroup); + n = pCache->nPage; + pcache1LeaveMutex(pCache->pGroup); + return n; +} + + +/* +** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described +** in the header of the pcache1Fetch() procedure. +** +** This steps are broken out into a separate procedure because they are +** usually not needed, and by avoiding the stack initialization required +** for these steps, the main pcache1Fetch() procedure can run faster. +*/ +static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( + PCache1 *pCache, + unsigned int iKey, + int createFlag +){ + unsigned int nPinned; + PGroup *pGroup = pCache->pGroup; + PgHdr1 *pPage = 0; + + /* Step 3: Abort if createFlag is 1 but the cache is nearly full */ + assert( pCache->nPage >= pCache->nRecyclable ); + nPinned = pCache->nPage - pCache->nRecyclable; + assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); + assert( pCache->n90pct == pCache->nMax*9/10 ); + if( createFlag==1 && ( + nPinned>=pGroup->mxPinned + || nPinned>=pCache->n90pct + || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclablenPage>=pCache->nHash ) pcache1ResizeHash(pCache); + assert( pCache->nHash>0 && pCache->apHash ); + + /* Step 4. Try to recycle a page. */ + if( pCache->bPurgeable + && !pGroup->lru.pLruPrev->isAnchor + && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache)) + ){ + PCache1 *pOther; + pPage = pGroup->lru.pLruPrev; + assert( pPage->isPinned==0 ); + pcache1RemoveFromHash(pPage, 0); + pcache1PinPage(pPage); + pOther = pPage->pCache; + if( pOther->szAlloc != pCache->szAlloc ){ + pcache1FreePage(pPage); + pPage = 0; + }else{ + pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); + } + } + + /* Step 5. If a usable page buffer has still not been found, + ** attempt to allocate a new one. + */ + if( !pPage ){ + pPage = pcache1AllocPage(pCache, createFlag==1); + } + + if( pPage ){ + unsigned int h = iKey % pCache->nHash; + pCache->nPage++; + pPage->iKey = iKey; + pPage->pNext = pCache->apHash[h]; + pPage->pCache = pCache; + pPage->pLruPrev = 0; + pPage->pLruNext = 0; + pPage->isPinned = 1; + *(void **)pPage->page.pExtra = 0; + pCache->apHash[h] = pPage; + if( iKey>pCache->iMaxKey ){ + pCache->iMaxKey = iKey; + } + } + return pPage; +} + +/* +** Implementation of the sqlite3_pcache.xFetch method. +** +** Fetch a page by key value. +** +** Whether or not a new page may be allocated by this function depends on +** the value of the createFlag argument. 0 means do not allocate a new +** page. 1 means allocate a new page if space is easily available. 2 +** means to try really hard to allocate a new page. +** +** For a non-purgeable cache (a cache used as the storage for an in-memory +** database) there is really no difference between createFlag 1 and 2. So +** the calling function (pcache.c) will never have a createFlag of 1 on +** a non-purgeable cache. +** +** There are three different approaches to obtaining space for a page, +** depending on the value of parameter createFlag (which may be 0, 1 or 2). +** +** 1. Regardless of the value of createFlag, the cache is searched for a +** copy of the requested page. If one is found, it is returned. +** +** 2. If createFlag==0 and the page is not already in the cache, NULL is +** returned. +** +** 3. If createFlag is 1, and the page is not already in the cache, then +** return NULL (do not allocate a new page) if any of the following +** conditions are true: +** +** (a) the number of pages pinned by the cache is greater than +** PCache1.nMax, or +** +** (b) the number of pages pinned by the cache is greater than +** the sum of nMax for all purgeable caches, less the sum of +** nMin for all other purgeable caches, or +** +** 4. If none of the first three conditions apply and the cache is marked +** as purgeable, and if one of the following is true: +** +** (a) The number of pages allocated for the cache is already +** PCache1.nMax, or +** +** (b) The number of pages allocated for all purgeable caches is +** already equal to or greater than the sum of nMax for all +** purgeable caches, +** +** (c) The system is under memory pressure and wants to avoid +** unnecessary pages cache entry allocations +** +** then attempt to recycle a page from the LRU list. If it is the right +** size, return the recycled buffer. Otherwise, free the buffer and +** proceed to step 5. +** +** 5. Otherwise, allocate and return a new page buffer. +** +** There are two versions of this routine. pcache1FetchWithMutex() is +** the general case. pcache1FetchNoMutex() is a faster implementation for +** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper +** invokes the appropriate routine. +*/ +static PgHdr1 *pcache1FetchNoMutex( + sqlite3_pcache *p, + unsigned int iKey, + int createFlag +){ + PCache1 *pCache = (PCache1 *)p; + PgHdr1 *pPage = 0; + + /* Step 1: Search the hash table for an existing entry. */ + pPage = pCache->apHash[iKey % pCache->nHash]; + while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } + + /* Step 2: If the page was found in the hash table, then return it. + ** If the page was not in the hash table and createFlag is 0, abort. + ** Otherwise (page not in hash and createFlag!=0) continue with + ** subsequent steps to try to create the page. */ + if( pPage ){ + if( !pPage->isPinned ){ + return pcache1PinPage(pPage); + }else{ + return pPage; + } + }else if( createFlag ){ + /* Steps 3, 4, and 5 implemented by this subroutine */ + return pcache1FetchStage2(pCache, iKey, createFlag); + }else{ + return 0; + } +} +#if PCACHE1_MIGHT_USE_GROUP_MUTEX +static PgHdr1 *pcache1FetchWithMutex( + sqlite3_pcache *p, + unsigned int iKey, + int createFlag +){ + PCache1 *pCache = (PCache1 *)p; + PgHdr1 *pPage; + + pcache1EnterMutex(pCache->pGroup); + pPage = pcache1FetchNoMutex(p, iKey, createFlag); + assert( pPage==0 || pCache->iMaxKey>=iKey ); + pcache1LeaveMutex(pCache->pGroup); + return pPage; +} +#endif +static sqlite3_pcache_page *pcache1Fetch( + sqlite3_pcache *p, + unsigned int iKey, + int createFlag +){ +#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) + PCache1 *pCache = (PCache1 *)p; +#endif + + assert( offsetof(PgHdr1,page)==0 ); + assert( pCache->bPurgeable || createFlag!=1 ); + assert( pCache->bPurgeable || pCache->nMin==0 ); + assert( pCache->bPurgeable==0 || pCache->nMin==10 ); + assert( pCache->nMin==0 || pCache->bPurgeable ); + assert( pCache->nHash>0 ); +#if PCACHE1_MIGHT_USE_GROUP_MUTEX + if( pCache->pGroup->mutex ){ + return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); + }else +#endif + { + return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); + } +} + + +/* +** Implementation of the sqlite3_pcache.xUnpin method. +** +** Mark a page as unpinned (eligible for asynchronous recycling). +*/ +static void pcache1Unpin( + sqlite3_pcache *p, + sqlite3_pcache_page *pPg, + int reuseUnlikely +){ + PCache1 *pCache = (PCache1 *)p; + PgHdr1 *pPage = (PgHdr1 *)pPg; + PGroup *pGroup = pCache->pGroup; + + assert( pPage->pCache==pCache ); + pcache1EnterMutex(pGroup); + + /* It is an error to call this function if the page is already + ** part of the PGroup LRU list. + */ + assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); + assert( pPage->isPinned==1 ); + + if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ + pcache1RemoveFromHash(pPage, 1); + }else{ + /* Add the page to the PGroup LRU list. */ + PgHdr1 **ppFirst = &pGroup->lru.pLruNext; + pPage->pLruPrev = &pGroup->lru; + (pPage->pLruNext = *ppFirst)->pLruPrev = pPage; + *ppFirst = pPage; + pCache->nRecyclable++; + pPage->isPinned = 0; + } + + pcache1LeaveMutex(pCache->pGroup); +} + +/* +** Implementation of the sqlite3_pcache.xRekey method. +*/ +static void pcache1Rekey( + sqlite3_pcache *p, + sqlite3_pcache_page *pPg, + unsigned int iOld, + unsigned int iNew +){ + PCache1 *pCache = (PCache1 *)p; + PgHdr1 *pPage = (PgHdr1 *)pPg; + PgHdr1 **pp; + unsigned int h; + assert( pPage->iKey==iOld ); + assert( pPage->pCache==pCache ); + + pcache1EnterMutex(pCache->pGroup); + + h = iOld%pCache->nHash; + pp = &pCache->apHash[h]; + while( (*pp)!=pPage ){ + pp = &(*pp)->pNext; + } + *pp = pPage->pNext; + + h = iNew%pCache->nHash; + pPage->iKey = iNew; + pPage->pNext = pCache->apHash[h]; + pCache->apHash[h] = pPage; + if( iNew>pCache->iMaxKey ){ + pCache->iMaxKey = iNew; + } + + pcache1LeaveMutex(pCache->pGroup); +} + +/* +** Implementation of the sqlite3_pcache.xTruncate method. +** +** Discard all unpinned pages in the cache with a page number equal to +** or greater than parameter iLimit. Any pinned pages with a page number +** equal to or greater than iLimit are implicitly unpinned. +*/ +static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ + PCache1 *pCache = (PCache1 *)p; + pcache1EnterMutex(pCache->pGroup); + if( iLimit<=pCache->iMaxKey ){ + pcache1TruncateUnsafe(pCache, iLimit); + pCache->iMaxKey = iLimit-1; + } + pcache1LeaveMutex(pCache->pGroup); +} + +/* +** Implementation of the sqlite3_pcache.xDestroy method. +** +** Destroy a cache allocated using pcache1Create(). +*/ +static void pcache1Destroy(sqlite3_pcache *p){ + PCache1 *pCache = (PCache1 *)p; + PGroup *pGroup = pCache->pGroup; + assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); + pcache1EnterMutex(pGroup); + pcache1TruncateUnsafe(pCache, 0); + assert( pGroup->nMaxPage >= pCache->nMax ); + pGroup->nMaxPage -= pCache->nMax; + assert( pGroup->nMinPage >= pCache->nMin ); + pGroup->nMinPage -= pCache->nMin; + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; + pcache1EnforceMaxPage(pCache); + pcache1LeaveMutex(pGroup); + sqlite3_free(pCache->pBulk); + sqlite3_free(pCache->apHash); + sqlite3_free(pCache); +} + +/* +** This function is called during initialization (sqlite3_initialize()) to +** install the default pluggable cache module, assuming the user has not +** already provided an alternative. +*/ +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){ + static const sqlite3_pcache_methods2 defaultMethods = { + 1, /* iVersion */ + 0, /* pArg */ + pcache1Init, /* xInit */ + pcache1Shutdown, /* xShutdown */ + pcache1Create, /* xCreate */ + pcache1Cachesize, /* xCachesize */ + pcache1Pagecount, /* xPagecount */ + pcache1Fetch, /* xFetch */ + pcache1Unpin, /* xUnpin */ + pcache1Rekey, /* xRekey */ + pcache1Truncate, /* xTruncate */ + pcache1Destroy, /* xDestroy */ + pcache1Shrink /* xShrink */ + }; + sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); +} + +/* +** Return the size of the header on each page of this PCACHE implementation. +*/ +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); } + +/* +** Return the global mutex used by this PCACHE implementation. The +** sqlite3_status() routine needs access to this mutex. +*/ +SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){ + return pcache1.mutex; +} + +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +/* +** This function is called to free superfluous dynamically allocated memory +** held by the pager system. Memory in use by any SQLite pager allocated +** by the current thread may be sqlite3_free()ed. +** +** nReq is the number of bytes of memory required. Once this much has +** been released, the function returns. The return value is the total number +** of bytes of memory released. +*/ +SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ + int nFree = 0; + assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); + assert( sqlite3_mutex_notheld(pcache1.mutex) ); + if( sqlite3GlobalConfig.nPage==0 ){ + PgHdr1 *p; + pcache1EnterMutex(&pcache1.grp); + while( (nReq<0 || nFreeisAnchor==0 + ){ + nFree += pcache1MemSize(p->page.pBuf); +#ifdef SQLITE_PCACHE_SEPARATE_HEADER + nFree += sqlite3MemSize(p); +#endif + assert( p->isPinned==0 ); + pcache1PinPage(p); + pcache1RemoveFromHash(p, 1); + } + pcache1LeaveMutex(&pcache1.grp); + } + return nFree; +} +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ + +#ifdef SQLITE_TEST +/* +** This function is used by test procedures to inspect the internal state +** of the global cache. +*/ +SQLITE_PRIVATE void sqlite3PcacheStats( + int *pnCurrent, /* OUT: Total number of pages cached */ + int *pnMax, /* OUT: Global maximum cache size */ + int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */ + int *pnRecyclable /* OUT: Total number of pages available for recycling */ +){ + PgHdr1 *p; + int nRecyclable = 0; + for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){ + assert( p->isPinned==0 ); + nRecyclable++; + } + *pnCurrent = pcache1.grp.nCurrentPage; + *pnMax = (int)pcache1.grp.nMaxPage; + *pnMin = (int)pcache1.grp.nMinPage; + *pnRecyclable = nRecyclable; +} +#endif + +/************** End of pcache1.c *********************************************/ +/************** Begin file rowset.c ******************************************/ +/* +** 2008 December 3 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This module implements an object we call a "RowSet". +** +** The RowSet object is a collection of rowids. Rowids +** are inserted into the RowSet in an arbitrary order. Inserts +** can be intermixed with tests to see if a given rowid has been +** previously inserted into the RowSet. +** +** After all inserts are finished, it is possible to extract the +** elements of the RowSet in sorted order. Once this extraction +** process has started, no new elements may be inserted. +** +** Hence, the primitive operations for a RowSet are: +** +** CREATE +** INSERT +** TEST +** SMALLEST +** DESTROY +** +** The CREATE and DESTROY primitives are the constructor and destructor, +** obviously. The INSERT primitive adds a new element to the RowSet. +** TEST checks to see if an element is already in the RowSet. SMALLEST +** extracts the least value from the RowSet. +** +** The INSERT primitive might allocate additional memory. Memory is +** allocated in chunks so most INSERTs do no allocation. There is an +** upper bound on the size of allocated memory. No memory is freed +** until DESTROY. +** +** The TEST primitive includes a "batch" number. The TEST primitive +** will only see elements that were inserted before the last change +** in the batch number. In other words, if an INSERT occurs between +** two TESTs where the TESTs have the same batch nubmer, then the +** value added by the INSERT will not be visible to the second TEST. +** The initial batch number is zero, so if the very first TEST contains +** a non-zero batch number, it will see all prior INSERTs. +** +** No INSERTs may occurs after a SMALLEST. An assertion will fail if +** that is attempted. +** +** The cost of an INSERT is roughly constant. (Sometimes new memory +** has to be allocated on an INSERT.) The cost of a TEST with a new +** batch number is O(NlogN) where N is the number of elements in the RowSet. +** The cost of a TEST using the same batch number is O(logN). The cost +** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST +** primitives are constant time. The cost of DESTROY is O(N). +** +** TEST and SMALLEST may not be used by the same RowSet. This used to +** be possible, but the feature was not used, so it was removed in order +** to simplify the code. +*/ +/* #include "sqliteInt.h" */ + + +/* +** Target size for allocation chunks. +*/ +#define ROWSET_ALLOCATION_SIZE 1024 + +/* +** The number of rowset entries per allocation chunk. +*/ +#define ROWSET_ENTRY_PER_CHUNK \ + ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry)) + +/* +** Each entry in a RowSet is an instance of the following object. +** +** This same object is reused to store a linked list of trees of RowSetEntry +** objects. In that alternative use, pRight points to the next entry +** in the list, pLeft points to the tree, and v is unused. The +** RowSet.pForest value points to the head of this forest list. +*/ +struct RowSetEntry { + i64 v; /* ROWID value for this entry */ + struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */ + struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */ +}; + +/* +** RowSetEntry objects are allocated in large chunks (instances of the +** following structure) to reduce memory allocation overhead. The +** chunks are kept on a linked list so that they can be deallocated +** when the RowSet is destroyed. +*/ +struct RowSetChunk { + struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */ + struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */ +}; + +/* +** A RowSet in an instance of the following structure. +** +** A typedef of this structure if found in sqliteInt.h. +*/ +struct RowSet { + struct RowSetChunk *pChunk; /* List of all chunk allocations */ + sqlite3 *db; /* The database connection */ + struct RowSetEntry *pEntry; /* List of entries using pRight */ + struct RowSetEntry *pLast; /* Last entry on the pEntry list */ + struct RowSetEntry *pFresh; /* Source of new entry objects */ + struct RowSetEntry *pForest; /* List of binary trees of entries */ + u16 nFresh; /* Number of objects on pFresh */ + u16 rsFlags; /* Various flags */ + int iBatch; /* Current insert batch */ +}; + +/* +** Allowed values for RowSet.rsFlags +*/ +#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */ +#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */ + +/* +** Turn bulk memory into a RowSet object. N bytes of memory +** are available at pSpace. The db pointer is used as a memory context +** for any subsequent allocations that need to occur. +** Return a pointer to the new RowSet object. +** +** It must be the case that N is sufficient to make a Rowset. If not +** an assertion fault occurs. +** +** If N is larger than the minimum, use the surplus as an initial +** allocation of entries available to be filled. +*/ +SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){ + RowSet *p; + assert( N >= ROUND8(sizeof(*p)) ); + p = pSpace; + p->pChunk = 0; + p->db = db; + p->pEntry = 0; + p->pLast = 0; + p->pForest = 0; + p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); + p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); + p->rsFlags = ROWSET_SORTED; + p->iBatch = 0; + return p; +} + +/* +** Deallocate all chunks from a RowSet. This frees all memory that +** the RowSet has allocated over its lifetime. This routine is +** the destructor for the RowSet. +*/ +SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){ + struct RowSetChunk *pChunk, *pNextChunk; + for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){ + pNextChunk = pChunk->pNextChunk; + sqlite3DbFree(p->db, pChunk); + } + p->pChunk = 0; + p->nFresh = 0; + p->pEntry = 0; + p->pLast = 0; + p->pForest = 0; + p->rsFlags = ROWSET_SORTED; +} + +/* +** Allocate a new RowSetEntry object that is associated with the +** given RowSet. Return a pointer to the new and completely uninitialized +** objected. +** +** In an OOM situation, the RowSet.db->mallocFailed flag is set and this +** routine returns NULL. +*/ +static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){ + assert( p!=0 ); + if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* We could allocate a fresh RowSetEntry each time one is needed, but it + ** is more efficient to pull a preallocated entry from the pool */ + struct RowSetChunk *pNew; + pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew)); + if( pNew==0 ){ + return 0; + } + pNew->pNextChunk = p->pChunk; + p->pChunk = pNew; + p->pFresh = pNew->aEntry; + p->nFresh = ROWSET_ENTRY_PER_CHUNK; + } + p->nFresh--; + return p->pFresh++; +} + +/* +** Insert a new value into a RowSet. +** +** The mallocFailed flag of the database connection is set if a +** memory allocation fails. +*/ +SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){ + struct RowSetEntry *pEntry; /* The new entry */ + struct RowSetEntry *pLast; /* The last prior entry */ + + /* This routine is never called after sqlite3RowSetNext() */ + assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 ); + + pEntry = rowSetEntryAlloc(p); + if( pEntry==0 ) return; + pEntry->v = rowid; + pEntry->pRight = 0; + pLast = p->pLast; + if( pLast ){ + if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/ + /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags + ** where possible */ + p->rsFlags &= ~ROWSET_SORTED; + } + pLast->pRight = pEntry; + }else{ + p->pEntry = pEntry; + } + p->pLast = pEntry; +} + +/* +** Merge two lists of RowSetEntry objects. Remove duplicates. +** +** The input lists are connected via pRight pointers and are +** assumed to each already be in sorted order. +*/ +static struct RowSetEntry *rowSetEntryMerge( + struct RowSetEntry *pA, /* First sorted list to be merged */ + struct RowSetEntry *pB /* Second sorted list to be merged */ +){ + struct RowSetEntry head; + struct RowSetEntry *pTail; + + pTail = &head; + while( pA && pB ){ + assert( pA->pRight==0 || pA->v<=pA->pRight->v ); + assert( pB->pRight==0 || pB->v<=pB->pRight->v ); + if( pA->vv ){ + pTail->pRight = pA; + pA = pA->pRight; + pTail = pTail->pRight; + }else if( pB->vv ){ + pTail->pRight = pB; + pB = pB->pRight; + pTail = pTail->pRight; + }else{ + pA = pA->pRight; + } + } + if( pA ){ + assert( pA->pRight==0 || pA->v<=pA->pRight->v ); + pTail->pRight = pA; + }else{ + assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v ); + pTail->pRight = pB; + } + return head.pRight; +} + +/* +** Sort all elements on the list of RowSetEntry objects into order of +** increasing v. +*/ +static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){ + unsigned int i; + struct RowSetEntry *pNext, *aBucket[40]; + + memset(aBucket, 0, sizeof(aBucket)); + while( pIn ){ + pNext = pIn->pRight; + pIn->pRight = 0; + for(i=0; aBucket[i]; i++){ + pIn = rowSetEntryMerge(aBucket[i], pIn); + aBucket[i] = 0; + } + aBucket[i] = pIn; + pIn = pNext; + } + pIn = 0; + for(i=0; ipLeft ){ + struct RowSetEntry *p; + rowSetTreeToList(pIn->pLeft, ppFirst, &p); + p->pRight = pIn; + }else{ + *ppFirst = pIn; + } + if( pIn->pRight ){ + rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast); + }else{ + *ppLast = pIn; + } + assert( (*ppLast)->pRight==0 ); +} + + +/* +** Convert a sorted list of elements (connected by pRight) into a binary +** tree with depth of iDepth. A depth of 1 means the tree contains a single +** node taken from the head of *ppList. A depth of 2 means a tree with +** three nodes. And so forth. +** +** Use as many entries from the input list as required and update the +** *ppList to point to the unused elements of the list. If the input +** list contains too few elements, then construct an incomplete tree +** and leave *ppList set to NULL. +** +** Return a pointer to the root of the constructed binary tree. +*/ +static struct RowSetEntry *rowSetNDeepTree( + struct RowSetEntry **ppList, + int iDepth +){ + struct RowSetEntry *p; /* Root of the new tree */ + struct RowSetEntry *pLeft; /* Left subtree */ + if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/ + /* Prevent unnecessary deep recursion when we run out of entries */ + return 0; + } + if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/ + /* This branch causes a *balanced* tree to be generated. A valid tree + ** is still generated without this branch, but the tree is wildly + ** unbalanced and inefficient. */ + pLeft = rowSetNDeepTree(ppList, iDepth-1); + p = *ppList; + if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* It is safe to always return here, but the resulting tree + ** would be unbalanced */ + return pLeft; + } + p->pLeft = pLeft; + *ppList = p->pRight; + p->pRight = rowSetNDeepTree(ppList, iDepth-1); + }else{ + p = *ppList; + *ppList = p->pRight; + p->pLeft = p->pRight = 0; + } + return p; +} + +/* +** Convert a sorted list of elements into a binary tree. Make the tree +** as deep as it needs to be in order to contain the entire list. +*/ +static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){ + int iDepth; /* Depth of the tree so far */ + struct RowSetEntry *p; /* Current tree root */ + struct RowSetEntry *pLeft; /* Left subtree */ + + assert( pList!=0 ); + p = pList; + pList = p->pRight; + p->pLeft = p->pRight = 0; + for(iDepth=1; pList; iDepth++){ + pLeft = p; + p = pList; + pList = p->pRight; + p->pLeft = pLeft; + p->pRight = rowSetNDeepTree(&pList, iDepth); + } + return p; +} + +/* +** Extract the smallest element from the RowSet. +** Write the element into *pRowid. Return 1 on success. Return +** 0 if the RowSet is already empty. +** +** After this routine has been called, the sqlite3RowSetInsert() +** routine may not be called again. +** +** This routine may not be called after sqlite3RowSetTest() has +** been used. Older versions of RowSet allowed that, but as the +** capability was not used by the code generator, it was removed +** for code economy. +*/ +SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){ + assert( p!=0 ); + assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */ + + /* Merge the forest into a single sorted list on first call */ + if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + p->pEntry = rowSetEntrySort(p->pEntry); + } + p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT; + } + + /* Return the next entry on the list */ + if( p->pEntry ){ + *pRowid = p->pEntry->v; + p->pEntry = p->pEntry->pRight; + if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/ + /* Free memory immediately, rather than waiting on sqlite3_finalize() */ + sqlite3RowSetClear(p); + } + return 1; + }else{ + return 0; + } +} + +/* +** Check to see if element iRowid was inserted into the rowset as +** part of any insert batch prior to iBatch. Return 1 or 0. +** +** If this is the first test of a new batch and if there exist entries +** on pRowSet->pEntry, then sort those entries into the forest at +** pRowSet->pForest so that they can be tested. +*/ +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){ + struct RowSetEntry *p, *pTree; + + /* This routine is never called after sqlite3RowSetNext() */ + assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 ); + + /* Sort entries into the forest on the first test of a new batch. + ** To save unnecessary work, only do this when the batch number changes. + */ + if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/ + p = pRowSet->pEntry; + if( p ){ + struct RowSetEntry **ppPrevTree = &pRowSet->pForest; + if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* Only sort the current set of entiries if they need it */ + p = rowSetEntrySort(p); + } + for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ + ppPrevTree = &pTree->pRight; + if( pTree->pLeft==0 ){ + pTree->pLeft = rowSetListToTree(p); + break; + }else{ + struct RowSetEntry *pAux, *pTail; + rowSetTreeToList(pTree->pLeft, &pAux, &pTail); + pTree->pLeft = 0; + p = rowSetEntryMerge(pAux, p); + } + } + if( pTree==0 ){ + *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet); + if( pTree ){ + pTree->v = 0; + pTree->pRight = 0; + pTree->pLeft = rowSetListToTree(p); + } + } + pRowSet->pEntry = 0; + pRowSet->pLast = 0; + pRowSet->rsFlags |= ROWSET_SORTED; + } + pRowSet->iBatch = iBatch; + } + + /* Test to see if the iRowid value appears anywhere in the forest. + ** Return 1 if it does and 0 if not. + */ + for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ + p = pTree->pLeft; + while( p ){ + if( p->vpRight; + }else if( p->v>iRowid ){ + p = p->pLeft; + }else{ + return 1; + } + } + } + return 0; +} + +/************** End of rowset.c **********************************************/ +/************** Begin file pager.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the implementation of the page cache subsystem or "pager". +** +** The pager is used to access a database disk file. It implements +** atomic commit and rollback through the use of a journal file that +** is separate from the database file. The pager also implements file +** locking to prevent two processes from writing the same database +** file simultaneously, or one process from reading the database while +** another is writing. +*/ +#ifndef SQLITE_OMIT_DISKIO +/* #include "sqliteInt.h" */ +/************** Include wal.h in the middle of pager.c ***********************/ +/************** Begin file wal.h *********************************************/ +/* +** 2010 February 1 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface to the write-ahead logging +** system. Refer to the comments below and the header comment attached to +** the implementation of each function in log.c for further details. +*/ + +#ifndef _WAL_H_ +#define _WAL_H_ + +/* #include "sqliteInt.h" */ + +/* Additional values that can be added to the sync_flags argument of +** sqlite3WalFrames(): +*/ +#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */ +#define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */ + +#ifdef SQLITE_OMIT_WAL +# define sqlite3WalOpen(x,y,z) 0 +# define sqlite3WalLimit(x,y) +# define sqlite3WalClose(w,x,y,z) 0 +# define sqlite3WalBeginReadTransaction(y,z) 0 +# define sqlite3WalEndReadTransaction(z) +# define sqlite3WalDbsize(y) 0 +# define sqlite3WalBeginWriteTransaction(y) 0 +# define sqlite3WalEndWriteTransaction(x) 0 +# define sqlite3WalUndo(x,y,z) 0 +# define sqlite3WalSavepoint(y,z) +# define sqlite3WalSavepointUndo(y,z) 0 +# define sqlite3WalFrames(u,v,w,x,y,z) 0 +# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0 +# define sqlite3WalCallback(z) 0 +# define sqlite3WalExclusiveMode(y,z) 0 +# define sqlite3WalHeapMemory(z) 0 +# define sqlite3WalFramesize(z) 0 +# define sqlite3WalFindFrame(x,y,z) 0 +# define sqlite3WalFile(x) 0 +#else + +#define WAL_SAVEPOINT_NDATA 4 + +/* Connection to a write-ahead log (WAL) file. +** There is one object of this type for each pager. +*/ +typedef struct Wal Wal; + +/* Open and close a connection to a write-ahead log. */ +SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**); +SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *); + +/* Set the limiting size of a WAL file. */ +SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64); + +/* Used by readers to open (lock) and close (unlock) a snapshot. A +** snapshot is like a read-transaction. It is the state of the database +** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and +** preserves the current state even if the other threads or processes +** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the +** transaction and releases the lock. +*/ +SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *); +SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal); + +/* Read a page from the write-ahead log, if it is present. */ +SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *); +SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *); + +/* If the WAL is not empty, return the size of the database. */ +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal); + +/* Obtain or release the WRITER lock. */ +SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal); +SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal); + +/* Undo any frames written (but not committed) to the log */ +SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx); + +/* Return an integer that records the current (uncommitted) write +** position in the WAL */ +SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData); + +/* Move the write position of the WAL back to iFrame. Called in +** response to a ROLLBACK TO command. */ +SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData); + +/* Write a frame or frames to the log. */ +SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int); + +/* Copy pages from the log to the database file */ +SQLITE_PRIVATE int sqlite3WalCheckpoint( + Wal *pWal, /* Write-ahead log connection */ + int eMode, /* One of PASSIVE, FULL and RESTART */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ + int sync_flags, /* Flags to sync db file with (or 0) */ + int nBuf, /* Size of buffer nBuf */ + u8 *zBuf, /* Temporary buffer to use */ + int *pnLog, /* OUT: Number of frames in WAL */ + int *pnCkpt /* OUT: Number of backfilled frames in WAL */ +); + +/* Return the value to pass to a sqlite3_wal_hook callback, the +** number of frames in the WAL at the point of the last commit since +** sqlite3WalCallback() was called. If no commits have occurred since +** the last call, then return 0. +*/ +SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal); + +/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released) +** by the pager layer on the database file. +*/ +SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op); + +/* Return true if the argument is non-NULL and the WAL module is using +** heap-memory for the wal-index. Otherwise, if the argument is NULL or the +** WAL module is using shared-memory, return false. +*/ +SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); + +#ifdef SQLITE_ENABLE_SNAPSHOT +SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); +SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); +#endif + +#ifdef SQLITE_ENABLE_ZIPVFS +/* If the WAL file is not empty, return the number of bytes of content +** stored in each frame (i.e. the db page-size when the WAL was created). +*/ +SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal); +#endif + +/* Return the sqlite3_file object for the WAL file */ +SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal); + +#endif /* ifndef SQLITE_OMIT_WAL */ +#endif /* _WAL_H_ */ + +/************** End of wal.h *************************************************/ +/************** Continuing where we left off in pager.c **********************/ + + +/******************* NOTES ON THE DESIGN OF THE PAGER ************************ +** +** This comment block describes invariants that hold when using a rollback +** journal. These invariants do not apply for journal_mode=WAL, +** journal_mode=MEMORY, or journal_mode=OFF. +** +** Within this comment block, a page is deemed to have been synced +** automatically as soon as it is written when PRAGMA synchronous=OFF. +** Otherwise, the page is not synced until the xSync method of the VFS +** is called successfully on the file containing the page. +** +** Definition: A page of the database file is said to be "overwriteable" if +** one or more of the following are true about the page: +** +** (a) The original content of the page as it was at the beginning of +** the transaction has been written into the rollback journal and +** synced. +** +** (b) The page was a freelist leaf page at the start of the transaction. +** +** (c) The page number is greater than the largest page that existed in +** the database file at the start of the transaction. +** +** (1) A page of the database file is never overwritten unless one of the +** following are true: +** +** (a) The page and all other pages on the same sector are overwriteable. +** +** (b) The atomic page write optimization is enabled, and the entire +** transaction other than the update of the transaction sequence +** number consists of a single page change. +** +** (2) The content of a page written into the rollback journal exactly matches +** both the content in the database when the rollback journal was written +** and the content in the database at the beginning of the current +** transaction. +** +** (3) Writes to the database file are an integer multiple of the page size +** in length and are aligned on a page boundary. +** +** (4) Reads from the database file are either aligned on a page boundary and +** an integer multiple of the page size in length or are taken from the +** first 100 bytes of the database file. +** +** (5) All writes to the database file are synced prior to the rollback journal +** being deleted, truncated, or zeroed. +** +** (6) If a master journal file is used, then all writes to the database file +** are synced prior to the master journal being deleted. +** +** Definition: Two databases (or the same database at two points it time) +** are said to be "logically equivalent" if they give the same answer to +** all queries. Note in particular the content of freelist leaf +** pages can be changed arbitrarily without affecting the logical equivalence +** of the database. +** +** (7) At any time, if any subset, including the empty set and the total set, +** of the unsynced changes to a rollback journal are removed and the +** journal is rolled back, the resulting database file will be logically +** equivalent to the database file at the beginning of the transaction. +** +** (8) When a transaction is rolled back, the xTruncate method of the VFS +** is called to restore the database file to the same size it was at +** the beginning of the transaction. (In some VFSes, the xTruncate +** method is a no-op, but that does not change the fact the SQLite will +** invoke it.) +** +** (9) Whenever the database file is modified, at least one bit in the range +** of bytes from 24 through 39 inclusive will be changed prior to releasing +** the EXCLUSIVE lock, thus signaling other connections on the same +** database to flush their caches. +** +** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less +** than one billion transactions. +** +** (11) A database file is well-formed at the beginning and at the conclusion +** of every transaction. +** +** (12) An EXCLUSIVE lock is held on the database file when writing to +** the database file. +** +** (13) A SHARED lock is held on the database file while reading any +** content out of the database file. +** +******************************************************************************/ + +/* +** Macros for troubleshooting. Normally turned off +*/ +#if 0 +int sqlite3PagerTrace=1; /* True to enable tracing */ +#define sqlite3DebugPrintf printf +#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; } +#else +#define PAGERTRACE(X) +#endif + +/* +** The following two macros are used within the PAGERTRACE() macros above +** to print out file-descriptors. +** +** PAGERID() takes a pointer to a Pager struct as its argument. The +** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file +** struct as its argument. +*/ +#define PAGERID(p) ((int)(p->fd)) +#define FILEHANDLEID(fd) ((int)fd) + +/* +** The Pager.eState variable stores the current 'state' of a pager. A +** pager may be in any one of the seven states shown in the following +** state diagram. +** +** OPEN <------+------+ +** | | | +** V | | +** +---------> READER-------+ | +** | | | +** | V | +** |<-------WRITER_LOCKED------> ERROR +** | | ^ +** | V | +** |<------WRITER_CACHEMOD-------->| +** | | | +** | V | +** |<-------WRITER_DBMOD---------->| +** | | | +** | V | +** +<------WRITER_FINISHED-------->+ +** +** +** List of state transitions and the C [function] that performs each: +** +** OPEN -> READER [sqlite3PagerSharedLock] +** READER -> OPEN [pager_unlock] +** +** READER -> WRITER_LOCKED [sqlite3PagerBegin] +** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal] +** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal] +** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne] +** WRITER_*** -> READER [pager_end_transaction] +** +** WRITER_*** -> ERROR [pager_error] +** ERROR -> OPEN [pager_unlock] +** +** +** OPEN: +** +** The pager starts up in this state. Nothing is guaranteed in this +** state - the file may or may not be locked and the database size is +** unknown. The database may not be read or written. +** +** * No read or write transaction is active. +** * Any lock, or no lock at all, may be held on the database file. +** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted. +** +** READER: +** +** In this state all the requirements for reading the database in +** rollback (non-WAL) mode are met. Unless the pager is (or recently +** was) in exclusive-locking mode, a user-level read transaction is +** open. The database size is known in this state. +** +** A connection running with locking_mode=normal enters this state when +** it opens a read-transaction on the database and returns to state +** OPEN after the read-transaction is completed. However a connection +** running in locking_mode=exclusive (including temp databases) remains in +** this state even after the read-transaction is closed. The only way +** a locking_mode=exclusive connection can transition from READER to OPEN +** is via the ERROR state (see below). +** +** * A read transaction may be active (but a write-transaction cannot). +** * A SHARED or greater lock is held on the database file. +** * The dbSize variable may be trusted (even if a user-level read +** transaction is not active). The dbOrigSize and dbFileSize variables +** may not be trusted at this point. +** * If the database is a WAL database, then the WAL connection is open. +** * Even if a read-transaction is not open, it is guaranteed that +** there is no hot-journal in the file-system. +** +** WRITER_LOCKED: +** +** The pager moves to this state from READER when a write-transaction +** is first opened on the database. In WRITER_LOCKED state, all locks +** required to start a write-transaction are held, but no actual +** modifications to the cache or database have taken place. +** +** In rollback mode, a RESERVED or (if the transaction was opened with +** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when +** moving to this state, but the journal file is not written to or opened +** to in this state. If the transaction is committed or rolled back while +** in WRITER_LOCKED state, all that is required is to unlock the database +** file. +** +** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file. +** If the connection is running with locking_mode=exclusive, an attempt +** is made to obtain an EXCLUSIVE lock on the database file. +** +** * A write transaction is active. +** * If the connection is open in rollback-mode, a RESERVED or greater +** lock is held on the database file. +** * If the connection is open in WAL-mode, a WAL write transaction +** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully +** called). +** * The dbSize, dbOrigSize and dbFileSize variables are all valid. +** * The contents of the pager cache have not been modified. +** * The journal file may or may not be open. +** * Nothing (not even the first header) has been written to the journal. +** +** WRITER_CACHEMOD: +** +** A pager moves from WRITER_LOCKED state to this state when a page is +** first modified by the upper layer. In rollback mode the journal file +** is opened (if it is not already open) and a header written to the +** start of it. The database file on disk has not been modified. +** +** * A write transaction is active. +** * A RESERVED or greater lock is held on the database file. +** * The journal file is open and the first header has been written +** to it, but the header has not been synced to disk. +** * The contents of the page cache have been modified. +** +** WRITER_DBMOD: +** +** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state +** when it modifies the contents of the database file. WAL connections +** never enter this state (since they do not modify the database file, +** just the log file). +** +** * A write transaction is active. +** * An EXCLUSIVE or greater lock is held on the database file. +** * The journal file is open and the first header has been written +** and synced to disk. +** * The contents of the page cache have been modified (and possibly +** written to disk). +** +** WRITER_FINISHED: +** +** It is not possible for a WAL connection to enter this state. +** +** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD +** state after the entire transaction has been successfully written into the +** database file. In this state the transaction may be committed simply +** by finalizing the journal file. Once in WRITER_FINISHED state, it is +** not possible to modify the database further. At this point, the upper +** layer must either commit or rollback the transaction. +** +** * A write transaction is active. +** * An EXCLUSIVE or greater lock is held on the database file. +** * All writing and syncing of journal and database data has finished. +** If no error occurred, all that remains is to finalize the journal to +** commit the transaction. If an error did occur, the caller will need +** to rollback the transaction. +** +** ERROR: +** +** The ERROR state is entered when an IO or disk-full error (including +** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it +** difficult to be sure that the in-memory pager state (cache contents, +** db size etc.) are consistent with the contents of the file-system. +** +** Temporary pager files may enter the ERROR state, but in-memory pagers +** cannot. +** +** For example, if an IO error occurs while performing a rollback, +** the contents of the page-cache may be left in an inconsistent state. +** At this point it would be dangerous to change back to READER state +** (as usually happens after a rollback). Any subsequent readers might +** report database corruption (due to the inconsistent cache), and if +** they upgrade to writers, they may inadvertently corrupt the database +** file. To avoid this hazard, the pager switches into the ERROR state +** instead of READER following such an error. +** +** Once it has entered the ERROR state, any attempt to use the pager +** to read or write data returns an error. Eventually, once all +** outstanding transactions have been abandoned, the pager is able to +** transition back to OPEN state, discarding the contents of the +** page-cache and any other in-memory state at the same time. Everything +** is reloaded from disk (and, if necessary, hot-journal rollback peformed) +** when a read-transaction is next opened on the pager (transitioning +** the pager into READER state). At that point the system has recovered +** from the error. +** +** Specifically, the pager jumps into the ERROR state if: +** +** 1. An error occurs while attempting a rollback. This happens in +** function sqlite3PagerRollback(). +** +** 2. An error occurs while attempting to finalize a journal file +** following a commit in function sqlite3PagerCommitPhaseTwo(). +** +** 3. An error occurs while attempting to write to the journal or +** database file in function pagerStress() in order to free up +** memory. +** +** In other cases, the error is returned to the b-tree layer. The b-tree +** layer then attempts a rollback operation. If the error condition +** persists, the pager enters the ERROR state via condition (1) above. +** +** Condition (3) is necessary because it can be triggered by a read-only +** statement executed within a transaction. In this case, if the error +** code were simply returned to the user, the b-tree layer would not +** automatically attempt a rollback, as it assumes that an error in a +** read-only statement cannot leave the pager in an internally inconsistent +** state. +** +** * The Pager.errCode variable is set to something other than SQLITE_OK. +** * There are one or more outstanding references to pages (after the +** last reference is dropped the pager should move back to OPEN state). +** * The pager is not an in-memory pager. +** +** +** Notes: +** +** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the +** connection is open in WAL mode. A WAL connection is always in one +** of the first four states. +** +** * Normally, a connection open in exclusive mode is never in PAGER_OPEN +** state. There are two exceptions: immediately after exclusive-mode has +** been turned on (and before any read or write transactions are +** executed), and when the pager is leaving the "error state". +** +** * See also: assert_pager_state(). +*/ +#define PAGER_OPEN 0 +#define PAGER_READER 1 +#define PAGER_WRITER_LOCKED 2 +#define PAGER_WRITER_CACHEMOD 3 +#define PAGER_WRITER_DBMOD 4 +#define PAGER_WRITER_FINISHED 5 +#define PAGER_ERROR 6 + +/* +** The Pager.eLock variable is almost always set to one of the +** following locking-states, according to the lock currently held on +** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. +** This variable is kept up to date as locks are taken and released by +** the pagerLockDb() and pagerUnlockDb() wrappers. +** +** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY +** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not +** the operation was successful. In these circumstances pagerLockDb() and +** pagerUnlockDb() take a conservative approach - eLock is always updated +** when unlocking the file, and only updated when locking the file if the +** VFS call is successful. This way, the Pager.eLock variable may be set +** to a less exclusive (lower) value than the lock that is actually held +** at the system level, but it is never set to a more exclusive value. +** +** This is usually safe. If an xUnlock fails or appears to fail, there may +** be a few redundant xLock() calls or a lock may be held for longer than +** required, but nothing really goes wrong. +** +** The exception is when the database file is unlocked as the pager moves +** from ERROR to OPEN state. At this point there may be a hot-journal file +** in the file-system that needs to be rolled back (as part of an OPEN->SHARED +** transition, by the same pager or any other). If the call to xUnlock() +** fails at this point and the pager is left holding an EXCLUSIVE lock, this +** can confuse the call to xCheckReservedLock() call made later as part +** of hot-journal detection. +** +** xCheckReservedLock() is defined as returning true "if there is a RESERVED +** lock held by this process or any others". So xCheckReservedLock may +** return true because the caller itself is holding an EXCLUSIVE lock (but +** doesn't know it because of a previous error in xUnlock). If this happens +** a hot-journal may be mistaken for a journal being created by an active +** transaction in another process, causing SQLite to read from the database +** without rolling it back. +** +** To work around this, if a call to xUnlock() fails when unlocking the +** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It +** is only changed back to a real locking state after a successful call +** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition +** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK +** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE +** lock on the database file before attempting to roll it back. See function +** PagerSharedLock() for more detail. +** +** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in +** PAGER_OPEN state. +*/ +#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1) + +/* +** A macro used for invoking the codec if there is one +*/ +#ifdef SQLITE_HAS_CODEC +# define CODEC1(P,D,N,X,E) \ + if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; } +# define CODEC2(P,D,N,X,E,O) \ + if( P->xCodec==0 ){ O=(char*)D; }else \ + if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; } +#else +# define CODEC1(P,D,N,X,E) /* NO-OP */ +# define CODEC2(P,D,N,X,E,O) O=(char*)D +#endif + +/* +** The maximum allowed sector size. 64KiB. If the xSectorsize() method +** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. +** This could conceivably cause corruption following a power failure on +** such a system. This is currently an undocumented limit. +*/ +#define MAX_SECTOR_SIZE 0x10000 + + +/* +** An instance of the following structure is allocated for each active +** savepoint and statement transaction in the system. All such structures +** are stored in the Pager.aSavepoint[] array, which is allocated and +** resized using sqlite3Realloc(). +** +** When a savepoint is created, the PagerSavepoint.iHdrOffset field is +** set to 0. If a journal-header is written into the main journal while +** the savepoint is active, then iHdrOffset is set to the byte offset +** immediately following the last journal record written into the main +** journal before the journal-header. This is required during savepoint +** rollback (see pagerPlaybackSavepoint()). +*/ +typedef struct PagerSavepoint PagerSavepoint; +struct PagerSavepoint { + i64 iOffset; /* Starting offset in main journal */ + i64 iHdrOffset; /* See above */ + Bitvec *pInSavepoint; /* Set of pages in this savepoint */ + Pgno nOrig; /* Original number of pages in file */ + Pgno iSubRec; /* Index of first record in sub-journal */ +#ifndef SQLITE_OMIT_WAL + u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ +#endif +}; + +/* +** Bits of the Pager.doNotSpill flag. See further description below. +*/ +#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ +#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ +#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ + +/* +** An open page cache is an instance of struct Pager. A description of +** some of the more important member variables follows: +** +** eState +** +** The current 'state' of the pager object. See the comment and state +** diagram above for a description of the pager state. +** +** eLock +** +** For a real on-disk database, the current lock held on the database file - +** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. +** +** For a temporary or in-memory database (neither of which require any +** locks), this variable is always set to EXCLUSIVE_LOCK. Since such +** databases always have Pager.exclusiveMode==1, this tricks the pager +** logic into thinking that it already has all the locks it will ever +** need (and no reason to release them). +** +** In some (obscure) circumstances, this variable may also be set to +** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for +** details. +** +** changeCountDone +** +** This boolean variable is used to make sure that the change-counter +** (the 4-byte header field at byte offset 24 of the database file) is +** not updated more often than necessary. +** +** It is set to true when the change-counter field is updated, which +** can only happen if an exclusive lock is held on the database file. +** It is cleared (set to false) whenever an exclusive lock is +** relinquished on the database file. Each time a transaction is committed, +** The changeCountDone flag is inspected. If it is true, the work of +** updating the change-counter is omitted for the current transaction. +** +** This mechanism means that when running in exclusive mode, a connection +** need only update the change-counter once, for the first transaction +** committed. +** +** setMaster +** +** When PagerCommitPhaseOne() is called to commit a transaction, it may +** (or may not) specify a master-journal name to be written into the +** journal file before it is synced to disk. +** +** Whether or not a journal file contains a master-journal pointer affects +** the way in which the journal file is finalized after the transaction is +** committed or rolled back when running in "journal_mode=PERSIST" mode. +** If a journal file does not contain a master-journal pointer, it is +** finalized by overwriting the first journal header with zeroes. If +** it does contain a master-journal pointer the journal file is finalized +** by truncating it to zero bytes, just as if the connection were +** running in "journal_mode=truncate" mode. +** +** Journal files that contain master journal pointers cannot be finalized +** simply by overwriting the first journal-header with zeroes, as the +** master journal pointer could interfere with hot-journal rollback of any +** subsequently interrupted transaction that reuses the journal file. +** +** The flag is cleared as soon as the journal file is finalized (either +** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the +** journal file from being successfully finalized, the setMaster flag +** is cleared anyway (and the pager will move to ERROR state). +** +** doNotSpill +** +** This variables control the behavior of cache-spills (calls made by +** the pcache module to the pagerStress() routine to write cached data +** to the file-system in order to free up memory). +** +** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set, +** writing to the database from pagerStress() is disabled altogether. +** The SPILLFLAG_ROLLBACK case is done in a very obscure case that +** comes up during savepoint rollback that requires the pcache module +** to allocate a new page to prevent the journal file from being written +** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF +** case is a user preference. +** +** If the SPILLFLAG_NOSYNC bit is set, writing to the database from +** pagerStress() is permitted, but syncing the journal file is not. +** This flag is set by sqlite3PagerWrite() when the file-system sector-size +** is larger than the database page-size in order to prevent a journal sync +** from happening in between the journalling of two pages on the same sector. +** +** subjInMemory +** +** This is a boolean variable. If true, then any required sub-journal +** is opened as an in-memory journal file. If false, then in-memory +** sub-journals are only used for in-memory pager files. +** +** This variable is updated by the upper layer each time a new +** write-transaction is opened. +** +** dbSize, dbOrigSize, dbFileSize +** +** Variable dbSize is set to the number of pages in the database file. +** It is valid in PAGER_READER and higher states (all states except for +** OPEN and ERROR). +** +** dbSize is set based on the size of the database file, which may be +** larger than the size of the database (the value stored at offset +** 28 of the database header by the btree). If the size of the file +** is not an integer multiple of the page-size, the value stored in +** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2). +** Except, any file that is greater than 0 bytes in size is considered +** to have at least one page. (i.e. a 1KB file with 2K page-size leads +** to dbSize==1). +** +** During a write-transaction, if pages with page-numbers greater than +** dbSize are modified in the cache, dbSize is updated accordingly. +** Similarly, if the database is truncated using PagerTruncateImage(), +** dbSize is updated. +** +** Variables dbOrigSize and dbFileSize are valid in states +** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize +** variable at the start of the transaction. It is used during rollback, +** and to determine whether or not pages need to be journalled before +** being modified. +** +** Throughout a write-transaction, dbFileSize contains the size of +** the file on disk in pages. It is set to a copy of dbSize when the +** write-transaction is first opened, and updated when VFS calls are made +** to write or truncate the database file on disk. +** +** The only reason the dbFileSize variable is required is to suppress +** unnecessary calls to xTruncate() after committing a transaction. If, +** when a transaction is committed, the dbFileSize variable indicates +** that the database file is larger than the database image (Pager.dbSize), +** pager_truncate() is called. The pager_truncate() call uses xFilesize() +** to measure the database file on disk, and then truncates it if required. +** dbFileSize is not used when rolling back a transaction. In this case +** pager_truncate() is called unconditionally (which means there may be +** a call to xFilesize() that is not strictly required). In either case, +** pager_truncate() may cause the file to become smaller or larger. +** +** dbHintSize +** +** The dbHintSize variable is used to limit the number of calls made to +** the VFS xFileControl(FCNTL_SIZE_HINT) method. +** +** dbHintSize is set to a copy of the dbSize variable when a +** write-transaction is opened (at the same time as dbFileSize and +** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called, +** dbHintSize is increased to the number of pages that correspond to the +** size-hint passed to the method call. See pager_write_pagelist() for +** details. +** +** errCode +** +** The Pager.errCode variable is only ever used in PAGER_ERROR state. It +** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode +** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX +** sub-codes. +*/ +struct Pager { + sqlite3_vfs *pVfs; /* OS functions to use for IO */ + u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ + u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ + u8 useJournal; /* Use a rollback journal on this file */ + u8 noSync; /* Do not sync the journal if true */ + u8 fullSync; /* Do extra syncs of the journal for robustness */ + u8 extraSync; /* sync directory after journal delete */ + u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ + u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */ + u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ + u8 tempFile; /* zFilename is a temporary or immutable file */ + u8 noLock; /* Do not lock (except in WAL mode) */ + u8 readOnly; /* True for a read-only database */ + u8 memDb; /* True to inhibit all file I/O */ + + /************************************************************************** + ** The following block contains those class members that change during + ** routine operation. Class members not in this block are either fixed + ** when the pager is first created or else only change when there is a + ** significant mode change (such as changing the page_size, locking_mode, + ** or the journal_mode). From another view, these class members describe + ** the "state" of the pager, while other class members describe the + ** "configuration" of the pager. + */ + u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */ + u8 eLock; /* Current lock held on database file */ + u8 changeCountDone; /* Set after incrementing the change-counter */ + u8 setMaster; /* True if a m-j name has been written to jrnl */ + u8 doNotSpill; /* Do not spill the cache when non-zero */ + u8 subjInMemory; /* True to use in-memory sub-journals */ + u8 bUseFetch; /* True to use xFetch() */ + u8 hasHeldSharedLock; /* True if a shared lock has ever been held */ + Pgno dbSize; /* Number of pages in the database */ + Pgno dbOrigSize; /* dbSize before the current transaction */ + Pgno dbFileSize; /* Number of pages in the database file */ + Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ + int errCode; /* One of several kinds of errors */ + int nRec; /* Pages journalled since last j-header written */ + u32 cksumInit; /* Quasi-random value added to every checksum */ + u32 nSubRec; /* Number of records written to sub-journal */ + Bitvec *pInJournal; /* One bit for each page in the database file */ + sqlite3_file *fd; /* File descriptor for database */ + sqlite3_file *jfd; /* File descriptor for main journal */ + sqlite3_file *sjfd; /* File descriptor for sub-journal */ + i64 journalOff; /* Current write offset in the journal file */ + i64 journalHdr; /* Byte offset to previous journal header */ + sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ + PagerSavepoint *aSavepoint; /* Array of active savepoints */ + int nSavepoint; /* Number of elements in aSavepoint[] */ + u32 iDataVersion; /* Changes whenever database content changes */ + char dbFileVers[16]; /* Changes whenever database file changes */ + + int nMmapOut; /* Number of mmap pages currently outstanding */ + sqlite3_int64 szMmap; /* Desired maximum mmap size */ + PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */ + /* + ** End of the routinely-changing class members + ***************************************************************************/ + + u16 nExtra; /* Add this many bytes to each in-memory page */ + i16 nReserve; /* Number of unused bytes at end of each page */ + u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ + u32 sectorSize; /* Assumed sector size during rollback */ + int pageSize; /* Number of bytes in a page */ + Pgno mxPgno; /* Maximum allowed size of the database */ + i64 journalSizeLimit; /* Size limit for persistent journal files */ + char *zFilename; /* Name of the database file */ + char *zJournal; /* Name of the journal file */ + int (*xBusyHandler)(void*); /* Function to call when busy */ + void *pBusyHandlerArg; /* Context argument for xBusyHandler */ + int aStat[3]; /* Total cache hits, misses and writes */ +#ifdef SQLITE_TEST + int nRead; /* Database pages read */ +#endif + void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ +#ifdef SQLITE_HAS_CODEC + void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ + void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ + void (*xCodecFree)(void*); /* Destructor for the codec */ + void *pCodec; /* First argument to xCodec... methods */ +#endif + char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ + PCache *pPCache; /* Pointer to page cache object */ +#ifndef SQLITE_OMIT_WAL + Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ + char *zWal; /* File name for write-ahead log */ +#endif +}; + +/* +** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains +** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS +** or CACHE_WRITE to sqlite3_db_status(). +*/ +#define PAGER_STAT_HIT 0 +#define PAGER_STAT_MISS 1 +#define PAGER_STAT_WRITE 2 + +/* +** The following global variables hold counters used for +** testing purposes only. These variables do not exist in +** a non-testing build. These variables are not thread-safe. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ +SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ +SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ +# define PAGER_INCR(v) v++ +#else +# define PAGER_INCR(v) +#endif + + + +/* +** Journal files begin with the following magic string. The data +** was obtained from /dev/random. It is used only as a sanity check. +** +** Since version 2.8.0, the journal format contains additional sanity +** checking information. If the power fails while the journal is being +** written, semi-random garbage data might appear in the journal +** file after power is restored. If an attempt is then made +** to roll the journal back, the database could be corrupted. The additional +** sanity checking data is an attempt to discover the garbage in the +** journal and ignore it. +** +** The sanity checking information for the new journal format consists +** of a 32-bit checksum on each page of data. The checksum covers both +** the page number and the pPager->pageSize bytes of data for the page. +** This cksum is initialized to a 32-bit random value that appears in the +** journal file right after the header. The random initializer is important, +** because garbage data that appears at the end of a journal is likely +** data that was once in other files that have now been deleted. If the +** garbage data came from an obsolete journal file, the checksums might +** be correct. But by initializing the checksum to random value which +** is different for every journal, we minimize that risk. +*/ +static const unsigned char aJournalMagic[] = { + 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, +}; + +/* +** The size of the of each page record in the journal is given by +** the following macro. +*/ +#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) + +/* +** The journal header size for this pager. This is usually the same +** size as a single disk sector. See also setSectorSize(). +*/ +#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) + +/* +** The macro MEMDB is true if we are dealing with an in-memory database. +** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, +** the value of MEMDB will be a constant and the compiler will optimize +** out code that would never execute. +*/ +#ifdef SQLITE_OMIT_MEMORYDB +# define MEMDB 0 +#else +# define MEMDB pPager->memDb +#endif + +/* +** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch +** interfaces to access the database using memory-mapped I/O. +*/ +#if SQLITE_MAX_MMAP_SIZE>0 +# define USEFETCH(x) ((x)->bUseFetch) +#else +# define USEFETCH(x) 0 +#endif + +/* +** The maximum legal page number is (2^31 - 1). +*/ +#define PAGER_MAX_PGNO 2147483647 + +/* +** The argument to this macro is a file descriptor (type sqlite3_file*). +** Return 0 if it is not open, or non-zero (but not 1) if it is. +** +** This is so that expressions can be written as: +** +** if( isOpen(pPager->jfd) ){ ... +** +** instead of +** +** if( pPager->jfd->pMethods ){ ... +*/ +#define isOpen(pFd) ((pFd)->pMethods!=0) + +/* +** Return true if this pager uses a write-ahead log instead of the usual +** rollback journal. Otherwise false. +*/ +#ifndef SQLITE_OMIT_WAL +static int pagerUseWal(Pager *pPager){ + return (pPager->pWal!=0); +} +#else +# define pagerUseWal(x) 0 +# define pagerRollbackWal(x) 0 +# define pagerWalFrames(v,w,x,y) 0 +# define pagerOpenWalIfPresent(z) SQLITE_OK +# define pagerBeginReadTransaction(z) SQLITE_OK +#endif + +#ifndef NDEBUG +/* +** Usage: +** +** assert( assert_pager_state(pPager) ); +** +** This function runs many asserts to try to find inconsistencies in +** the internal state of the Pager object. +*/ +static int assert_pager_state(Pager *p){ + Pager *pPager = p; + + /* State must be valid. */ + assert( p->eState==PAGER_OPEN + || p->eState==PAGER_READER + || p->eState==PAGER_WRITER_LOCKED + || p->eState==PAGER_WRITER_CACHEMOD + || p->eState==PAGER_WRITER_DBMOD + || p->eState==PAGER_WRITER_FINISHED + || p->eState==PAGER_ERROR + ); + + /* Regardless of the current state, a temp-file connection always behaves + ** as if it has an exclusive lock on the database file. It never updates + ** the change-counter field, so the changeCountDone flag is always set. + */ + assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK ); + assert( p->tempFile==0 || pPager->changeCountDone ); + + /* If the useJournal flag is clear, the journal-mode must be "OFF". + ** And if the journal-mode is "OFF", the journal file must not be open. + */ + assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal ); + assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) ); + + /* Check that MEMDB implies noSync. And an in-memory journal. Since + ** this means an in-memory pager performs no IO at all, it cannot encounter + ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing + ** a journal file. (although the in-memory journal implementation may + ** return SQLITE_IOERR_NOMEM while the journal file is being written). It + ** is therefore not possible for an in-memory pager to enter the ERROR + ** state. + */ + if( MEMDB ){ + assert( !isOpen(p->fd) ); + assert( p->noSync ); + assert( p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_MEMORY + ); + assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); + assert( pagerUseWal(p)==0 ); + } + + /* If changeCountDone is set, a RESERVED lock or greater must be held + ** on the file. + */ + assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK ); + assert( p->eLock!=PENDING_LOCK ); + + switch( p->eState ){ + case PAGER_OPEN: + assert( !MEMDB ); + assert( pPager->errCode==SQLITE_OK ); + assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile ); + break; + + case PAGER_READER: + assert( pPager->errCode==SQLITE_OK ); + assert( p->eLock!=UNKNOWN_LOCK ); + assert( p->eLock>=SHARED_LOCK ); + break; + + case PAGER_WRITER_LOCKED: + assert( p->eLock!=UNKNOWN_LOCK ); + assert( pPager->errCode==SQLITE_OK ); + if( !pagerUseWal(pPager) ){ + assert( p->eLock>=RESERVED_LOCK ); + } + assert( pPager->dbSize==pPager->dbOrigSize ); + assert( pPager->dbOrigSize==pPager->dbFileSize ); + assert( pPager->dbOrigSize==pPager->dbHintSize ); + assert( pPager->setMaster==0 ); + break; + + case PAGER_WRITER_CACHEMOD: + assert( p->eLock!=UNKNOWN_LOCK ); + assert( pPager->errCode==SQLITE_OK ); + if( !pagerUseWal(pPager) ){ + /* It is possible that if journal_mode=wal here that neither the + ** journal file nor the WAL file are open. This happens during + ** a rollback transaction that switches from journal_mode=off + ** to journal_mode=wal. + */ + assert( p->eLock>=RESERVED_LOCK ); + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL + ); + } + assert( pPager->dbOrigSize==pPager->dbFileSize ); + assert( pPager->dbOrigSize==pPager->dbHintSize ); + break; + + case PAGER_WRITER_DBMOD: + assert( p->eLock==EXCLUSIVE_LOCK ); + assert( pPager->errCode==SQLITE_OK ); + assert( !pagerUseWal(pPager) ); + assert( p->eLock>=EXCLUSIVE_LOCK ); + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL + ); + assert( pPager->dbOrigSize<=pPager->dbHintSize ); + break; + + case PAGER_WRITER_FINISHED: + assert( p->eLock==EXCLUSIVE_LOCK ); + assert( pPager->errCode==SQLITE_OK ); + assert( !pagerUseWal(pPager) ); + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL + ); + break; + + case PAGER_ERROR: + /* There must be at least one outstanding reference to the pager if + ** in ERROR state. Otherwise the pager should have already dropped + ** back to OPEN state. + */ + assert( pPager->errCode!=SQLITE_OK ); + assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile ); + break; + } + + return 1; +} +#endif /* ifndef NDEBUG */ + +#ifdef SQLITE_DEBUG +/* +** Return a pointer to a human readable string in a static buffer +** containing the state of the Pager object passed as an argument. This +** is intended to be used within debuggers. For example, as an alternative +** to "print *pPager" in gdb: +** +** (gdb) printf "%s", print_pager_state(pPager) +*/ +static char *print_pager_state(Pager *p){ + static char zRet[1024]; + + sqlite3_snprintf(1024, zRet, + "Filename: %s\n" + "State: %s errCode=%d\n" + "Lock: %s\n" + "Locking mode: locking_mode=%s\n" + "Journal mode: journal_mode=%s\n" + "Backing store: tempFile=%d memDb=%d useJournal=%d\n" + "Journal: journalOff=%lld journalHdr=%lld\n" + "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n" + , p->zFilename + , p->eState==PAGER_OPEN ? "OPEN" : + p->eState==PAGER_READER ? "READER" : + p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" : + p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" : + p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" : + p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" : + p->eState==PAGER_ERROR ? "ERROR" : "?error?" + , (int)p->errCode + , p->eLock==NO_LOCK ? "NO_LOCK" : + p->eLock==RESERVED_LOCK ? "RESERVED" : + p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" : + p->eLock==SHARED_LOCK ? "SHARED" : + p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?" + , p->exclusiveMode ? "exclusive" : "normal" + , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" : + p->journalMode==PAGER_JOURNALMODE_OFF ? "off" : + p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" : + p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" : + p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" : + p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?" + , (int)p->tempFile, (int)p->memDb, (int)p->useJournal + , p->journalOff, p->journalHdr + , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize + ); + + return zRet; +} +#endif + +/* +** Return true if it is necessary to write page *pPg into the sub-journal. +** A page needs to be written into the sub-journal if there exists one +** or more open savepoints for which: +** +** * The page-number is less than or equal to PagerSavepoint.nOrig, and +** * The bit corresponding to the page-number is not set in +** PagerSavepoint.pInSavepoint. +*/ +static int subjRequiresPage(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + PagerSavepoint *p; + Pgno pgno = pPg->pgno; + int i; + for(i=0; inSavepoint; i++){ + p = &pPager->aSavepoint[i]; + if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){ + return 1; + } + } + return 0; +} + +#ifdef SQLITE_DEBUG +/* +** Return true if the page is already in the journal file. +*/ +static int pageInJournal(Pager *pPager, PgHdr *pPg){ + return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno); +} +#endif + +/* +** Read a 32-bit integer from the given file descriptor. Store the integer +** that is read in *pRes. Return SQLITE_OK if everything worked, or an +** error code is something goes wrong. +** +** All values are stored on disk as big-endian. +*/ +static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ + unsigned char ac[4]; + int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); + if( rc==SQLITE_OK ){ + *pRes = sqlite3Get4byte(ac); + } + return rc; +} + +/* +** Write a 32-bit integer into a string buffer in big-endian byte order. +*/ +#define put32bits(A,B) sqlite3Put4byte((u8*)A,B) + + +/* +** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK +** on success or an error code is something goes wrong. +*/ +static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ + char ac[4]; + put32bits(ac, val); + return sqlite3OsWrite(fd, ac, 4, offset); +} + +/* +** Unlock the database file to level eLock, which must be either NO_LOCK +** or SHARED_LOCK. Regardless of whether or not the call to xUnlock() +** succeeds, set the Pager.eLock variable to match the (attempted) new lock. +** +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is +** called, do not modify it. See the comment above the #define of +** UNKNOWN_LOCK for an explanation of this. +*/ +static int pagerUnlockDb(Pager *pPager, int eLock){ + int rc = SQLITE_OK; + + assert( !pPager->exclusiveMode || pPager->eLock==eLock ); + assert( eLock==NO_LOCK || eLock==SHARED_LOCK ); + assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 ); + if( isOpen(pPager->fd) ){ + assert( pPager->eLock>=eLock ); + rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock); + if( pPager->eLock!=UNKNOWN_LOCK ){ + pPager->eLock = (u8)eLock; + } + IOTRACE(("UNLOCK %p %d\n", pPager, eLock)) + } + return rc; +} + +/* +** Lock the database file to level eLock, which must be either SHARED_LOCK, +** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the +** Pager.eLock variable to the new locking state. +** +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is +** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. +** See the comment above the #define of UNKNOWN_LOCK for an explanation +** of this. +*/ +static int pagerLockDb(Pager *pPager, int eLock){ + int rc = SQLITE_OK; + + assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK ); + if( pPager->eLockeLock==UNKNOWN_LOCK ){ + rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock); + if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){ + pPager->eLock = (u8)eLock; + IOTRACE(("LOCK %p %d\n", pPager, eLock)) + } + } + return rc; +} + +/* +** This function determines whether or not the atomic-write optimization +** can be used with this pager. The optimization can be used if: +** +** (a) the value returned by OsDeviceCharacteristics() indicates that +** a database page may be written atomically, and +** (b) the value returned by OsSectorSize() is less than or equal +** to the page size. +** +** The optimization is also always enabled for temporary files. It is +** an error to call this function if pPager is opened on an in-memory +** database. +** +** If the optimization cannot be used, 0 is returned. If it can be used, +** then the value returned is the size of the journal file when it +** contains rollback data for exactly one page. +*/ +#ifdef SQLITE_ENABLE_ATOMIC_WRITE +static int jrnlBufferSize(Pager *pPager){ + assert( !MEMDB ); + if( !pPager->tempFile ){ + int dc; /* Device characteristics */ + int nSector; /* Sector size */ + int szPage; /* Page size */ + + assert( isOpen(pPager->fd) ); + dc = sqlite3OsDeviceCharacteristics(pPager->fd); + nSector = pPager->sectorSize; + szPage = pPager->pageSize; + + assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); + assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); + if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){ + return 0; + } + } + + return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); +} +#else +# define jrnlBufferSize(x) 0 +#endif + +/* +** If SQLITE_CHECK_PAGES is defined then we do some sanity checking +** on the cache using a hash function. This is used for testing +** and debugging only. +*/ +#ifdef SQLITE_CHECK_PAGES +/* +** Return a 32-bit hash of the page data for pPage. +*/ +static u32 pager_datahash(int nByte, unsigned char *pData){ + u32 hash = 0; + int i; + for(i=0; ipPager->pageSize, (unsigned char *)pPage->pData); +} +static void pager_set_pagehash(PgHdr *pPage){ + pPage->pageHash = pager_pagehash(pPage); +} + +/* +** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES +** is defined, and NDEBUG is not defined, an assert() statement checks +** that the page is either dirty or still matches the calculated page-hash. +*/ +#define CHECK_PAGE(x) checkPage(x) +static void checkPage(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + assert( pPager->eState!=PAGER_ERROR ); + assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); +} + +#else +#define pager_datahash(X,Y) 0 +#define pager_pagehash(X) 0 +#define pager_set_pagehash(X) +#define CHECK_PAGE(x) +#endif /* SQLITE_CHECK_PAGES */ + +/* +** When this is called the journal file for pager pPager must be open. +** This function attempts to read a master journal file name from the +** end of the file and, if successful, copies it into memory supplied +** by the caller. See comments above writeMasterJournal() for the format +** used to store a master journal file name at the end of a journal file. +** +** zMaster must point to a buffer of at least nMaster bytes allocated by +** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is +** enough space to write the master journal name). If the master journal +** name in the journal is longer than nMaster bytes (including a +** nul-terminator), then this is handled as if no master journal name +** were present in the journal. +** +** If a master journal file name is present at the end of the journal +** file, then it is copied into the buffer pointed to by zMaster. A +** nul-terminator byte is appended to the buffer following the master +** journal file name. +** +** If it is determined that no master journal file name is present +** zMaster[0] is set to 0 and SQLITE_OK returned. +** +** If an error occurs while reading from the journal file, an SQLite +** error code is returned. +*/ +static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ + int rc; /* Return code */ + u32 len; /* Length in bytes of master journal name */ + i64 szJ; /* Total size in bytes of journal file pJrnl */ + u32 cksum; /* MJ checksum value read from journal */ + u32 u; /* Unsigned loop counter */ + unsigned char aMagic[8]; /* A buffer to hold the magic header */ + zMaster[0] = '\0'; + + if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ)) + || szJ<16 + || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len)) + || len>=nMaster + || len==0 + || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum)) + || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8)) + || memcmp(aMagic, aJournalMagic, 8) + || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len)) + ){ + return rc; + } + + /* See if the checksum matches the master journal name */ + for(u=0; ujournalOff, assuming a sector +** size of pPager->sectorSize bytes. +** +** i.e for a sector size of 512: +** +** Pager.journalOff Return value +** --------------------------------------- +** 0 0 +** 512 512 +** 100 512 +** 2000 2048 +** +*/ +static i64 journalHdrOffset(Pager *pPager){ + i64 offset = 0; + i64 c = pPager->journalOff; + if( c ){ + offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); + } + assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); + assert( offset>=c ); + assert( (offset-c)jfd) ); + assert( !sqlite3JournalIsInMemory(pPager->jfd) ); + if( pPager->journalOff ){ + const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ + + IOTRACE(("JZEROHDR %p\n", pPager)) + if( doTruncate || iLimit==0 ){ + rc = sqlite3OsTruncate(pPager->jfd, 0); + }else{ + static const char zeroHdr[28] = {0}; + rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); + } + if( rc==SQLITE_OK && !pPager->noSync ){ + rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags); + } + + /* At this point the transaction is committed but the write lock + ** is still held on the file. If there is a size limit configured for + ** the persistent journal and the journal file currently consumes more + ** space than that limit allows for, truncate it now. There is no need + ** to sync the file following this operation. + */ + if( rc==SQLITE_OK && iLimit>0 ){ + i64 sz; + rc = sqlite3OsFileSize(pPager->jfd, &sz); + if( rc==SQLITE_OK && sz>iLimit ){ + rc = sqlite3OsTruncate(pPager->jfd, iLimit); + } + } + } + return rc; +} + +/* +** The journal file must be open when this routine is called. A journal +** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the +** current location. +** +** The format for the journal header is as follows: +** - 8 bytes: Magic identifying journal format. +** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. +** - 4 bytes: Random number used for page hash. +** - 4 bytes: Initial database page count. +** - 4 bytes: Sector size used by the process that wrote this journal. +** - 4 bytes: Database page size. +** +** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. +*/ +static int writeJournalHdr(Pager *pPager){ + int rc = SQLITE_OK; /* Return code */ + char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */ + u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */ + u32 nWrite; /* Bytes of header sector written */ + int ii; /* Loop counter */ + + assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ + + if( nHeader>JOURNAL_HDR_SZ(pPager) ){ + nHeader = JOURNAL_HDR_SZ(pPager); + } + + /* If there are active savepoints and any of them were created + ** since the most recent journal header was written, update the + ** PagerSavepoint.iHdrOffset fields now. + */ + for(ii=0; iinSavepoint; ii++){ + if( pPager->aSavepoint[ii].iHdrOffset==0 ){ + pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff; + } + } + + pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager); + + /* + ** Write the nRec Field - the number of page records that follow this + ** journal header. Normally, zero is written to this value at this time. + ** After the records are added to the journal (and the journal synced, + ** if in full-sync mode), the zero is overwritten with the true number + ** of records (see syncJournal()). + ** + ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When + ** reading the journal this value tells SQLite to assume that the + ** rest of the journal file contains valid page records. This assumption + ** is dangerous, as if a failure occurred whilst writing to the journal + ** file it may contain some garbage data. There are two scenarios + ** where this risk can be ignored: + ** + ** * When the pager is in no-sync mode. Corruption can follow a + ** power failure in this case anyway. + ** + ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees + ** that garbage data is never appended to the journal file. + */ + assert( isOpen(pPager->fd) || pPager->noSync ); + if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) + || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) + ){ + memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); + put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); + }else{ + memset(zHeader, 0, sizeof(aJournalMagic)+4); + } + + /* The random check-hash initializer */ + sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); + put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); + /* The initial database size */ + put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); + /* The assumed sector size for this process */ + put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); + + /* The page size */ + put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); + + /* Initializing the tail of the buffer is not necessary. Everything + ** works find if the following memset() is omitted. But initializing + ** the memory prevents valgrind from complaining, so we are willing to + ** take the performance hit. + */ + memset(&zHeader[sizeof(aJournalMagic)+20], 0, + nHeader-(sizeof(aJournalMagic)+20)); + + /* In theory, it is only necessary to write the 28 bytes that the + ** journal header consumes to the journal file here. Then increment the + ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next + ** record is written to the following sector (leaving a gap in the file + ** that will be implicitly filled in by the OS). + ** + ** However it has been discovered that on some systems this pattern can + ** be significantly slower than contiguously writing data to the file, + ** even if that means explicitly writing data to the block of + ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what + ** is done. + ** + ** The loop is required here in case the sector-size is larger than the + ** database page size. Since the zHeader buffer is only Pager.pageSize + ** bytes in size, more than one call to sqlite3OsWrite() may be required + ** to populate the entire journal header sector. + */ + for(nWrite=0; rc==SQLITE_OK&&nWritejournalHdr, nHeader)) + rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); + assert( pPager->journalHdr <= pPager->journalOff ); + pPager->journalOff += nHeader; + } + + return rc; +} + +/* +** The journal file must be open when this is called. A journal header file +** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal +** file. The current location in the journal file is given by +** pPager->journalOff. See comments above function writeJournalHdr() for +** a description of the journal header format. +** +** If the header is read successfully, *pNRec is set to the number of +** page records following this header and *pDbSize is set to the size of the +** database before the transaction began, in pages. Also, pPager->cksumInit +** is set to the value read from the journal header. SQLITE_OK is returned +** in this case. +** +** If the journal header file appears to be corrupted, SQLITE_DONE is +** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes +** cannot be read from the journal file an error code is returned. +*/ +static int readJournalHdr( + Pager *pPager, /* Pager object */ + int isHot, + i64 journalSize, /* Size of the open journal file in bytes */ + u32 *pNRec, /* OUT: Value read from the nRec field */ + u32 *pDbSize /* OUT: Value of original database size field */ +){ + int rc; /* Return code */ + unsigned char aMagic[8]; /* A buffer to hold the magic header */ + i64 iHdrOff; /* Offset of journal header being read */ + + assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ + + /* Advance Pager.journalOff to the start of the next sector. If the + ** journal file is too small for there to be a header stored at this + ** point, return SQLITE_DONE. + */ + pPager->journalOff = journalHdrOffset(pPager); + if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ + return SQLITE_DONE; + } + iHdrOff = pPager->journalOff; + + /* Read in the first 8 bytes of the journal header. If they do not match + ** the magic string found at the start of each journal header, return + ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise, + ** proceed. + */ + if( isHot || iHdrOff!=pPager->journalHdr ){ + rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff); + if( rc ){ + return rc; + } + if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ + return SQLITE_DONE; + } + } + + /* Read the first three 32-bit fields of the journal header: The nRec + ** field, the checksum-initializer and the database size at the start + ** of the transaction. Return an error code if anything goes wrong. + */ + if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec)) + || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit)) + || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize)) + ){ + return rc; + } + + if( pPager->journalOff==0 ){ + u32 iPageSize; /* Page-size field of journal header */ + u32 iSectorSize; /* Sector-size field of journal header */ + + /* Read the page-size and sector-size journal header fields. */ + if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize)) + || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize)) + ){ + return rc; + } + + /* Versions of SQLite prior to 3.5.8 set the page-size field of the + ** journal header to zero. In this case, assume that the Pager.pageSize + ** variable is already set to the correct page size. + */ + if( iPageSize==0 ){ + iPageSize = pPager->pageSize; + } + + /* Check that the values read from the page-size and sector-size fields + ** are within range. To be 'in range', both values need to be a power + ** of two greater than or equal to 512 or 32, and not greater than their + ** respective compile time maximum limits. + */ + if( iPageSize<512 || iSectorSize<32 + || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE + || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0 + ){ + /* If the either the page-size or sector-size in the journal-header is + ** invalid, then the process that wrote the journal-header must have + ** crashed before the header was synced. In this case stop reading + ** the journal file here. + */ + return SQLITE_DONE; + } + + /* Update the page-size to match the value read from the journal. + ** Use a testcase() macro to make sure that malloc failure within + ** PagerSetPagesize() is tested. + */ + rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1); + testcase( rc!=SQLITE_OK ); + + /* Update the assumed sector-size to match the value used by + ** the process that created this journal. If this journal was + ** created by a process other than this one, then this routine + ** is being called from within pager_playback(). The local value + ** of Pager.sectorSize is restored at the end of that routine. + */ + pPager->sectorSize = iSectorSize; + } + + pPager->journalOff += JOURNAL_HDR_SZ(pPager); + return rc; +} + + +/* +** Write the supplied master journal name into the journal file for pager +** pPager at the current location. The master journal name must be the last +** thing written to a journal file. If the pager is in full-sync mode, the +** journal file descriptor is advanced to the next sector boundary before +** anything is written. The format is: +** +** + 4 bytes: PAGER_MJ_PGNO. +** + N bytes: Master journal filename in utf-8. +** + 4 bytes: N (length of master journal name in bytes, no nul-terminator). +** + 4 bytes: Master journal name checksum. +** + 8 bytes: aJournalMagic[]. +** +** The master journal page checksum is the sum of the bytes in the master +** journal name, where each byte is interpreted as a signed 8-bit integer. +** +** If zMaster is a NULL pointer (occurs for a single database transaction), +** this call is a no-op. +*/ +static int writeMasterJournal(Pager *pPager, const char *zMaster){ + int rc; /* Return code */ + int nMaster; /* Length of string zMaster */ + i64 iHdrOff; /* Offset of header in journal file */ + i64 jrnlSize; /* Size of journal file on disk */ + u32 cksum = 0; /* Checksum of string zMaster */ + + assert( pPager->setMaster==0 ); + assert( !pagerUseWal(pPager) ); + + if( !zMaster + || pPager->journalMode==PAGER_JOURNALMODE_MEMORY + || !isOpen(pPager->jfd) + ){ + return SQLITE_OK; + } + pPager->setMaster = 1; + assert( pPager->journalHdr <= pPager->journalOff ); + + /* Calculate the length in bytes and the checksum of zMaster */ + for(nMaster=0; zMaster[nMaster]; nMaster++){ + cksum += zMaster[nMaster]; + } + + /* If in full-sync mode, advance to the next disk sector before writing + ** the master journal name. This is in case the previous page written to + ** the journal has already been synced. + */ + if( pPager->fullSync ){ + pPager->journalOff = journalHdrOffset(pPager); + } + iHdrOff = pPager->journalOff; + + /* Write the master journal data to the end of the journal file. If + ** an error occurs, return the error code to the caller. + */ + if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))) + || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))) + || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, + iHdrOff+4+nMaster+8))) + ){ + return rc; + } + pPager->journalOff += (nMaster+20); + + /* If the pager is in peristent-journal mode, then the physical + ** journal-file may extend past the end of the master-journal name + ** and 8 bytes of magic data just written to the file. This is + ** dangerous because the code to rollback a hot-journal file + ** will not be able to find the master-journal name to determine + ** whether or not the journal is hot. + ** + ** Easiest thing to do in this scenario is to truncate the journal + ** file to the required size. + */ + if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize)) + && jrnlSize>pPager->journalOff + ){ + rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff); + } + return rc; +} + +/* +** Discard the entire contents of the in-memory page-cache. +*/ +static void pager_reset(Pager *pPager){ + pPager->iDataVersion++; + sqlite3BackupRestart(pPager->pBackup); + sqlite3PcacheClear(pPager->pPCache); +} + +/* +** Return the pPager->iDataVersion value +*/ +SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){ + assert( pPager->eState>PAGER_OPEN ); + return pPager->iDataVersion; +} + +/* +** Free all structures in the Pager.aSavepoint[] array and set both +** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal +** if it is open and the pager is not in exclusive mode. +*/ +static void releaseAllSavepoints(Pager *pPager){ + int ii; /* Iterator for looping through Pager.aSavepoint */ + for(ii=0; iinSavepoint; ii++){ + sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); + } + if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){ + sqlite3OsClose(pPager->sjfd); + } + sqlite3_free(pPager->aSavepoint); + pPager->aSavepoint = 0; + pPager->nSavepoint = 0; + pPager->nSubRec = 0; +} + +/* +** Set the bit number pgno in the PagerSavepoint.pInSavepoint +** bitvecs of all open savepoints. Return SQLITE_OK if successful +** or SQLITE_NOMEM if a malloc failure occurs. +*/ +static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){ + int ii; /* Loop counter */ + int rc = SQLITE_OK; /* Result code */ + + for(ii=0; iinSavepoint; ii++){ + PagerSavepoint *p = &pPager->aSavepoint[ii]; + if( pgno<=p->nOrig ){ + rc |= sqlite3BitvecSet(p->pInSavepoint, pgno); + testcase( rc==SQLITE_NOMEM ); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + } + } + return rc; +} + +/* +** This function is a no-op if the pager is in exclusive mode and not +** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN +** state. +** +** If the pager is not in exclusive-access mode, the database file is +** completely unlocked. If the file is unlocked and the file-system does +** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is +** closed (if it is open). +** +** If the pager is in ERROR state when this function is called, the +** contents of the pager cache are discarded before switching back to +** the OPEN state. Regardless of whether the pager is in exclusive-mode +** or not, any journal file left in the file-system will be treated +** as a hot-journal and rolled back the next time a read-transaction +** is opened (by this or by any other connection). +*/ +static void pager_unlock(Pager *pPager){ + + assert( pPager->eState==PAGER_READER + || pPager->eState==PAGER_OPEN + || pPager->eState==PAGER_ERROR + ); + + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; + releaseAllSavepoints(pPager); + + if( pagerUseWal(pPager) ){ + assert( !isOpen(pPager->jfd) ); + sqlite3WalEndReadTransaction(pPager->pWal); + pPager->eState = PAGER_OPEN; + }else if( !pPager->exclusiveMode ){ + int rc; /* Error code returned by pagerUnlockDb() */ + int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0; + + /* If the operating system support deletion of open files, then + ** close the journal file when dropping the database lock. Otherwise + ** another connection with journal_mode=delete might delete the file + ** out from under us. + */ + assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 ); + assert( (PAGER_JOURNALMODE_OFF & 5)!=1 ); + assert( (PAGER_JOURNALMODE_WAL & 5)!=1 ); + assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 ); + assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); + assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); + if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN) + || 1!=(pPager->journalMode & 5) + ){ + sqlite3OsClose(pPager->jfd); + } + + /* If the pager is in the ERROR state and the call to unlock the database + ** file fails, set the current lock to UNKNOWN_LOCK. See the comment + ** above the #define for UNKNOWN_LOCK for an explanation of why this + ** is necessary. + */ + rc = pagerUnlockDb(pPager, NO_LOCK); + if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){ + pPager->eLock = UNKNOWN_LOCK; + } + + /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here + ** without clearing the error code. This is intentional - the error + ** code is cleared and the cache reset in the block below. + */ + assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); + pPager->changeCountDone = 0; + pPager->eState = PAGER_OPEN; + } + + /* If Pager.errCode is set, the contents of the pager cache cannot be + ** trusted. Now that there are no outstanding references to the pager, + ** it can safely move back to PAGER_OPEN state. This happens in both + ** normal and exclusive-locking mode. + */ + assert( pPager->errCode==SQLITE_OK || !MEMDB ); + if( pPager->errCode ){ + if( pPager->tempFile==0 ){ + pager_reset(pPager); + pPager->changeCountDone = 0; + pPager->eState = PAGER_OPEN; + }else{ + pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER); + } + if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); + pPager->errCode = SQLITE_OK; + } + + pPager->journalOff = 0; + pPager->journalHdr = 0; + pPager->setMaster = 0; +} + +/* +** This function is called whenever an IOERR or FULL error that requires +** the pager to transition into the ERROR state may ahve occurred. +** The first argument is a pointer to the pager structure, the second +** the error-code about to be returned by a pager API function. The +** value returned is a copy of the second argument to this function. +** +** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the +** IOERR sub-codes, the pager enters the ERROR state and the error code +** is stored in Pager.errCode. While the pager remains in the ERROR state, +** all major API calls on the Pager will immediately return Pager.errCode. +** +** The ERROR state indicates that the contents of the pager-cache +** cannot be trusted. This state can be cleared by completely discarding +** the contents of the pager-cache. If a transaction was active when +** the persistent error occurred, then the rollback journal may need +** to be replayed to restore the contents of the database file (as if +** it were a hot-journal). +*/ +static int pager_error(Pager *pPager, int rc){ + int rc2 = rc & 0xff; + assert( rc==SQLITE_OK || !MEMDB ); + assert( + pPager->errCode==SQLITE_FULL || + pPager->errCode==SQLITE_OK || + (pPager->errCode & 0xff)==SQLITE_IOERR + ); + if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ + pPager->errCode = rc; + pPager->eState = PAGER_ERROR; + } + return rc; +} + +static int pager_truncate(Pager *pPager, Pgno nPage); + +/* +** The write transaction open on pPager is being committed (bCommit==1) +** or rolled back (bCommit==0). +** +** Return TRUE if and only if all dirty pages should be flushed to disk. +** +** Rules: +** +** * For non-TEMP databases, always sync to disk. This is necessary +** for transactions to be durable. +** +** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing +** file has been created already (via a spill on pagerStress()) and +** when the number of dirty pages in memory exceeds 25% of the total +** cache size. +*/ +static int pagerFlushOnCommit(Pager *pPager, int bCommit){ + if( pPager->tempFile==0 ) return 1; + if( !bCommit ) return 0; + if( !isOpen(pPager->fd) ) return 0; + return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); +} + +/* +** This routine ends a transaction. A transaction is usually ended by +** either a COMMIT or a ROLLBACK operation. This routine may be called +** after rollback of a hot-journal, or if an error occurs while opening +** the journal file or writing the very first journal-header of a +** database transaction. +** +** This routine is never called in PAGER_ERROR state. If it is called +** in PAGER_NONE or PAGER_SHARED state and the lock held is less +** exclusive than a RESERVED lock, it is a no-op. +** +** Otherwise, any active savepoints are released. +** +** If the journal file is open, then it is "finalized". Once a journal +** file has been finalized it is not possible to use it to roll back a +** transaction. Nor will it be considered to be a hot-journal by this +** or any other database connection. Exactly how a journal is finalized +** depends on whether or not the pager is running in exclusive mode and +** the current journal-mode (Pager.journalMode value), as follows: +** +** journalMode==MEMORY +** Journal file descriptor is simply closed. This destroys an +** in-memory journal. +** +** journalMode==TRUNCATE +** Journal file is truncated to zero bytes in size. +** +** journalMode==PERSIST +** The first 28 bytes of the journal file are zeroed. This invalidates +** the first journal header in the file, and hence the entire journal +** file. An invalid journal file cannot be rolled back. +** +** journalMode==DELETE +** The journal file is closed and deleted using sqlite3OsDelete(). +** +** If the pager is running in exclusive mode, this method of finalizing +** the journal file is never used. Instead, if the journalMode is +** DELETE and the pager is in exclusive mode, the method described under +** journalMode==PERSIST is used instead. +** +** After the journal is finalized, the pager moves to PAGER_READER state. +** If running in non-exclusive rollback mode, the lock on the file is +** downgraded to a SHARED_LOCK. +** +** SQLITE_OK is returned if no error occurs. If an error occurs during +** any of the IO operations to finalize the journal file or unlock the +** database then the IO error code is returned to the user. If the +** operation to finalize the journal file fails, then the code still +** tries to unlock the database file if not in exclusive mode. If the +** unlock operation fails as well, then the first error code related +** to the first error encountered (the journal finalization one) is +** returned. +*/ +static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ + int rc = SQLITE_OK; /* Error code from journal finalization operation */ + int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ + + /* Do nothing if the pager does not have an open write transaction + ** or at least a RESERVED lock. This function may be called when there + ** is no write-transaction active but a RESERVED or greater lock is + ** held under two circumstances: + ** + ** 1. After a successful hot-journal rollback, it is called with + ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. + ** + ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE + ** lock switches back to locking_mode=normal and then executes a + ** read-transaction, this function is called with eState==PAGER_READER + ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed. + */ + assert( assert_pager_state(pPager) ); + assert( pPager->eState!=PAGER_ERROR ); + if( pPager->eStateeLockjfd) || pPager->pInJournal==0 ); + if( isOpen(pPager->jfd) ){ + assert( !pagerUseWal(pPager) ); + + /* Finalize the journal file. */ + if( sqlite3JournalIsInMemory(pPager->jfd) ){ + /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ + sqlite3OsClose(pPager->jfd); + }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ + if( pPager->journalOff==0 ){ + rc = SQLITE_OK; + }else{ + rc = sqlite3OsTruncate(pPager->jfd, 0); + if( rc==SQLITE_OK && pPager->fullSync ){ + /* Make sure the new file size is written into the inode right away. + ** Otherwise the journal might resurrect following a power loss and + ** cause the last transaction to roll back. See + ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773 + */ + rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); + } + } + pPager->journalOff = 0; + }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST + || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) + ){ + rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile); + pPager->journalOff = 0; + }else{ + /* This branch may be executed with Pager.journalMode==MEMORY if + ** a hot-journal was just rolled back. In this case the journal + ** file should be closed and deleted. If this connection writes to + ** the database file, it will do so using an in-memory journal. + */ + int bDelete = !pPager->tempFile; + assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); + assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE + || pPager->journalMode==PAGER_JOURNALMODE_MEMORY + || pPager->journalMode==PAGER_JOURNALMODE_WAL + ); + sqlite3OsClose(pPager->jfd); + if( bDelete ){ + rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync); + } + } + } + +#ifdef SQLITE_CHECK_PAGES + sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); + if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ + PgHdr *p = sqlite3PagerLookup(pPager, 1); + if( p ){ + p->pageHash = 0; + sqlite3PagerUnrefNotNull(p); + } + } +#endif + + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; + pPager->nRec = 0; + if( rc==SQLITE_OK ){ + if( pagerFlushOnCommit(pPager, bCommit) ){ + sqlite3PcacheCleanAll(pPager->pPCache); + }else{ + sqlite3PcacheClearWritable(pPager->pPCache); + } + sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); + } + + if( pagerUseWal(pPager) ){ + /* Drop the WAL write-lock, if any. Also, if the connection was in + ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE + ** lock held on the database file. + */ + rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); + assert( rc2==SQLITE_OK ); + }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ + /* This branch is taken when committing a transaction in rollback-journal + ** mode if the database file on disk is larger than the database image. + ** At this point the journal has been finalized and the transaction + ** successfully committed, but the EXCLUSIVE lock is still held on the + ** file. So it is safe to truncate the database file to its minimum + ** required size. */ + assert( pPager->eLock==EXCLUSIVE_LOCK ); + rc = pager_truncate(pPager, pPager->dbSize); + } + + if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){ + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0); + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + } + + if( !pPager->exclusiveMode + && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0)) + ){ + rc2 = pagerUnlockDb(pPager, SHARED_LOCK); + pPager->changeCountDone = 0; + } + pPager->eState = PAGER_READER; + pPager->setMaster = 0; + + return (rc==SQLITE_OK?rc2:rc); +} + +/* +** Execute a rollback if a transaction is active and unlock the +** database file. +** +** If the pager has already entered the ERROR state, do not attempt +** the rollback at this time. Instead, pager_unlock() is called. The +** call to pager_unlock() will discard all in-memory pages, unlock +** the database file and move the pager back to OPEN state. If this +** means that there is a hot-journal left in the file-system, the next +** connection to obtain a shared lock on the pager (which may be this one) +** will roll it back. +** +** If the pager has not already entered the ERROR state, but an IO or +** malloc error occurs during a rollback, then this will itself cause +** the pager to enter the ERROR state. Which will be cleared by the +** call to pager_unlock(), as described above. +*/ +static void pagerUnlockAndRollback(Pager *pPager){ + if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){ + assert( assert_pager_state(pPager) ); + if( pPager->eState>=PAGER_WRITER_LOCKED ){ + sqlite3BeginBenignMalloc(); + sqlite3PagerRollback(pPager); + sqlite3EndBenignMalloc(); + }else if( !pPager->exclusiveMode ){ + assert( pPager->eState==PAGER_READER ); + pager_end_transaction(pPager, 0, 0); + } + } + pager_unlock(pPager); +} + +/* +** Parameter aData must point to a buffer of pPager->pageSize bytes +** of data. Compute and return a checksum based ont the contents of the +** page of data and the current value of pPager->cksumInit. +** +** This is not a real checksum. It is really just the sum of the +** random initial value (pPager->cksumInit) and every 200th byte +** of the page data, starting with byte offset (pPager->pageSize%200). +** Each byte is interpreted as an 8-bit unsigned integer. +** +** Changing the formula used to compute this checksum results in an +** incompatible journal file format. +** +** If journal corruption occurs due to a power failure, the most likely +** scenario is that one end or the other of the record will be changed. +** It is much less likely that the two ends of the journal record will be +** correct and the middle be corrupt. Thus, this "checksum" scheme, +** though fast and simple, catches the mostly likely kind of corruption. +*/ +static u32 pager_cksum(Pager *pPager, const u8 *aData){ + u32 cksum = pPager->cksumInit; /* Checksum value to return */ + int i = pPager->pageSize-200; /* Loop counter */ + while( i>0 ){ + cksum += aData[i]; + i -= 200; + } + return cksum; +} + +/* +** Report the current page size and number of reserved bytes back +** to the codec. +*/ +#ifdef SQLITE_HAS_CODEC +static void pagerReportSize(Pager *pPager){ + if( pPager->xCodecSizeChng ){ + pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize, + (int)pPager->nReserve); + } +} +#else +# define pagerReportSize(X) /* No-op if we do not support a codec */ +#endif + +#ifdef SQLITE_HAS_CODEC +/* +** Make sure the number of reserved bits is the same in the destination +** pager as it is in the source. This comes up when a VACUUM changes the +** number of reserved bits to the "optimal" amount. +*/ +SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ + if( pDest->nReserve!=pSrc->nReserve ){ + pDest->nReserve = pSrc->nReserve; + pagerReportSize(pDest); + } +} +#endif + +/* +** Read a single page from either the journal file (if isMainJrnl==1) or +** from the sub-journal (if isMainJrnl==0) and playback that page. +** The page begins at offset *pOffset into the file. The *pOffset +** value is increased to the start of the next page in the journal. +** +** The main rollback journal uses checksums - the statement journal does +** not. +** +** If the page number of the page record read from the (sub-)journal file +** is greater than the current value of Pager.dbSize, then playback is +** skipped and SQLITE_OK is returned. +** +** If pDone is not NULL, then it is a record of pages that have already +** been played back. If the page at *pOffset has already been played back +** (if the corresponding pDone bit is set) then skip the playback. +** Make sure the pDone bit corresponding to the *pOffset page is set +** prior to returning. +** +** If the page record is successfully read from the (sub-)journal file +** and played back, then SQLITE_OK is returned. If an IO error occurs +** while reading the record from the (sub-)journal file or while writing +** to the database file, then the IO error code is returned. If data +** is successfully read from the (sub-)journal file but appears to be +** corrupted, SQLITE_DONE is returned. Data is considered corrupted in +** two circumstances: +** +** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or +** * If the record is being rolled back from the main journal file +** and the checksum field does not match the record content. +** +** Neither of these two scenarios are possible during a savepoint rollback. +** +** If this is a savepoint rollback, then memory may have to be dynamically +** allocated by this function. If this is the case and an allocation fails, +** SQLITE_NOMEM is returned. +*/ +static int pager_playback_one_page( + Pager *pPager, /* The pager being played back */ + i64 *pOffset, /* Offset of record to playback */ + Bitvec *pDone, /* Bitvec of pages already played back */ + int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ + int isSavepnt /* True for a savepoint rollback */ +){ + int rc; + PgHdr *pPg; /* An existing page in the cache */ + Pgno pgno; /* The page number of a page in journal */ + u32 cksum; /* Checksum used for sanity checking */ + char *aData; /* Temporary storage for the page */ + sqlite3_file *jfd; /* The file descriptor for the journal file */ + int isSynced; /* True if journal page is synced */ + + assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ + assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ + assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */ + assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ + + aData = pPager->pTmpSpace; + assert( aData ); /* Temp storage must have already been allocated */ + assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) ); + + /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction + ** or savepoint rollback done at the request of the caller) or this is + ** a hot-journal rollback. If it is a hot-journal rollback, the pager + ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback + ** only reads from the main journal, not the sub-journal. + */ + assert( pPager->eState>=PAGER_WRITER_CACHEMOD + || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK) + ); + assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl ); + + /* Read the page number and page data from the journal or sub-journal + ** file. Return an error code to the caller if an IO error occurs. + */ + jfd = isMainJrnl ? pPager->jfd : pPager->sjfd; + rc = read32bits(jfd, *pOffset, &pgno); + if( rc!=SQLITE_OK ) return rc; + rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4); + if( rc!=SQLITE_OK ) return rc; + *pOffset += pPager->pageSize + 4 + isMainJrnl*4; + + /* Sanity checking on the page. This is more important that I originally + ** thought. If a power failure occurs while the journal is being written, + ** it could cause invalid data to be written into the journal. We need to + ** detect this invalid data (with high probability) and ignore it. + */ + if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ + assert( !isSavepnt ); + return SQLITE_DONE; + } + if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){ + return SQLITE_OK; + } + if( isMainJrnl ){ + rc = read32bits(jfd, (*pOffset)-4, &cksum); + if( rc ) return rc; + if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){ + return SQLITE_DONE; + } + } + + /* If this page has already been played back before during the current + ** rollback, then don't bother to play it back again. + */ + if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ + return rc; + } + + /* When playing back page 1, restore the nReserve setting + */ + if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){ + pPager->nReserve = ((u8*)aData)[20]; + pagerReportSize(pPager); + } + + /* If the pager is in CACHEMOD state, then there must be a copy of this + ** page in the pager cache. In this case just update the pager cache, + ** not the database file. The page is left marked dirty in this case. + ** + ** An exception to the above rule: If the database is in no-sync mode + ** and a page is moved during an incremental vacuum then the page may + ** not be in the pager cache. Later: if a malloc() or IO error occurs + ** during a Movepage() call, then the page may not be in the cache + ** either. So the condition described in the above paragraph is not + ** assert()able. + ** + ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the + ** pager cache if it exists and the main file. The page is then marked + ** not dirty. Since this code is only executed in PAGER_OPEN state for + ** a hot-journal rollback, it is guaranteed that the page-cache is empty + ** if the pager is in OPEN state. + ** + ** Ticket #1171: The statement journal might contain page content that is + ** different from the page content at the start of the transaction. + ** This occurs when a page is changed prior to the start of a statement + ** then changed again within the statement. When rolling back such a + ** statement we must not write to the original database unless we know + ** for certain that original page contents are synced into the main rollback + ** journal. Otherwise, a power loss might leave modified data in the + ** database file without an entry in the rollback journal that can + ** restore the database to its original form. Two conditions must be + ** met before writing to the database files. (1) the database must be + ** locked. (2) we know that the original page content is fully synced + ** in the main journal either because the page is not in cache or else + ** the page is marked as needSync==0. + ** + ** 2008-04-14: When attempting to vacuum a corrupt database file, it + ** is possible to fail a statement on a database that does not yet exist. + ** Do not attempt to write if database file has never been opened. + */ + if( pagerUseWal(pPager) ){ + pPg = 0; + }else{ + pPg = sqlite3PagerLookup(pPager, pgno); + } + assert( pPg || !MEMDB ); + assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile ); + PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", + PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), + (isMainJrnl?"main-journal":"sub-journal") + )); + if( isMainJrnl ){ + isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); + }else{ + isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); + } + if( isOpen(pPager->fd) + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) + && isSynced + ){ + i64 ofst = (pgno-1)*(i64)pPager->pageSize; + testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); + assert( !pagerUseWal(pPager) ); + rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); + if( pgno>pPager->dbFileSize ){ + pPager->dbFileSize = pgno; + } + if( pPager->pBackup ){ + CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); + sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); + CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData); + } + }else if( !isMainJrnl && pPg==0 ){ + /* If this is a rollback of a savepoint and data was not written to + ** the database and the page is not in-memory, there is a potential + ** problem. When the page is next fetched by the b-tree layer, it + ** will be read from the database file, which may or may not be + ** current. + ** + ** There are a couple of different ways this can happen. All are quite + ** obscure. When running in synchronous mode, this can only happen + ** if the page is on the free-list at the start of the transaction, then + ** populated, then moved using sqlite3PagerMovepage(). + ** + ** The solution is to add an in-memory page to the cache containing + ** the data just read from the sub-journal. Mark the page as dirty + ** and if the pager requires a journal-sync, then mark the page as + ** requiring a journal-sync before it is written. + */ + assert( isSavepnt ); + assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 ); + pPager->doNotSpill |= SPILLFLAG_ROLLBACK; + rc = sqlite3PagerGet(pPager, pgno, &pPg, 1); + assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 ); + pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK; + if( rc!=SQLITE_OK ) return rc; + sqlite3PcacheMakeDirty(pPg); + } + if( pPg ){ + /* No page should ever be explicitly rolled back that is in use, except + ** for page 1 which is held in use in order to keep the lock on the + ** database active. However such a page may be rolled back as a result + ** of an internal error resulting in an automatic call to + ** sqlite3PagerRollback(). + */ + void *pData; + pData = pPg->pData; + memcpy(pData, (u8*)aData, pPager->pageSize); + pPager->xReiniter(pPg); + /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But + ** that call was dangerous and had no detectable benefit since the cache + ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so + ** has been removed. */ + pager_set_pagehash(pPg); + + /* If this was page 1, then restore the value of Pager.dbFileVers. + ** Do this before any decoding. */ + if( pgno==1 ){ + memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); + } + + /* Decode the page just read from disk */ + CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); + sqlite3PcacheRelease(pPg); + } + return rc; +} + +/* +** Parameter zMaster is the name of a master journal file. A single journal +** file that referred to the master journal file has just been rolled back. +** This routine checks if it is possible to delete the master journal file, +** and does so if it is. +** +** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not +** available for use within this function. +** +** When a master journal file is created, it is populated with the names +** of all of its child journals, one after another, formatted as utf-8 +** encoded text. The end of each child journal file is marked with a +** nul-terminator byte (0x00). i.e. the entire contents of a master journal +** file for a transaction involving two databases might be: +** +** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00" +** +** A master journal file may only be deleted once all of its child +** journals have been rolled back. +** +** This function reads the contents of the master-journal file into +** memory and loops through each of the child journal names. For +** each child journal, it checks if: +** +** * if the child journal exists, and if so +** * if the child journal contains a reference to master journal +** file zMaster +** +** If a child journal can be found that matches both of the criteria +** above, this function returns without doing anything. Otherwise, if +** no such child journal can be found, file zMaster is deleted from +** the file-system using sqlite3OsDelete(). +** +** If an IO error within this function, an error code is returned. This +** function allocates memory by calling sqlite3Malloc(). If an allocation +** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors +** occur, SQLITE_OK is returned. +** +** TODO: This function allocates a single block of memory to load +** the entire contents of the master journal file. This could be +** a couple of kilobytes or so - potentially larger than the page +** size. +*/ +static int pager_delmaster(Pager *pPager, const char *zMaster){ + sqlite3_vfs *pVfs = pPager->pVfs; + int rc; /* Return code */ + sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */ + sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */ + char *zMasterJournal = 0; /* Contents of master journal file */ + i64 nMasterJournal; /* Size of master journal file */ + char *zJournal; /* Pointer to one journal within MJ file */ + char *zMasterPtr; /* Space to hold MJ filename from a journal file */ + int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */ + + /* Allocate space for both the pJournal and pMaster file descriptors. + ** If successful, open the master journal file for reading. + */ + pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); + pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); + if( !pMaster ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); + rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); + } + if( rc!=SQLITE_OK ) goto delmaster_out; + + /* Load the entire master journal file into space obtained from + ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain + ** sufficient space (in zMasterPtr) to hold the names of master + ** journal files extracted from regular rollback-journals. + */ + rc = sqlite3OsFileSize(pMaster, &nMasterJournal); + if( rc!=SQLITE_OK ) goto delmaster_out; + nMasterPtr = pVfs->mxPathname+1; + zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1); + if( !zMasterJournal ){ + rc = SQLITE_NOMEM_BKPT; + goto delmaster_out; + } + zMasterPtr = &zMasterJournal[nMasterJournal+1]; + rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); + if( rc!=SQLITE_OK ) goto delmaster_out; + zMasterJournal[nMasterJournal] = 0; + + zJournal = zMasterJournal; + while( (zJournal-zMasterJournal)pageSize bytes). +** If the file on disk is currently larger than nPage pages, then use the VFS +** xTruncate() method to truncate it. +** +** Or, it might be the case that the file on disk is smaller than +** nPage pages. Some operating system implementations can get confused if +** you try to truncate a file to some size that is larger than it +** currently is, so detect this case and write a single zero byte to +** the end of the new file instead. +** +** If successful, return SQLITE_OK. If an IO error occurs while modifying +** the database file, return the error code to the caller. +*/ +static int pager_truncate(Pager *pPager, Pgno nPage){ + int rc = SQLITE_OK; + assert( pPager->eState!=PAGER_ERROR ); + assert( pPager->eState!=PAGER_READER ); + + if( isOpen(pPager->fd) + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) + ){ + i64 currentSize, newSize; + int szPage = pPager->pageSize; + assert( pPager->eLock==EXCLUSIVE_LOCK ); + /* TODO: Is it safe to use Pager.dbFileSize here? */ + rc = sqlite3OsFileSize(pPager->fd, ¤tSize); + newSize = szPage*(i64)nPage; + if( rc==SQLITE_OK && currentSize!=newSize ){ + if( currentSize>newSize ){ + rc = sqlite3OsTruncate(pPager->fd, newSize); + }else if( (currentSize+szPage)<=newSize ){ + char *pTmp = pPager->pTmpSpace; + memset(pTmp, 0, szPage); + testcase( (newSize-szPage) == currentSize ); + testcase( (newSize-szPage) > currentSize ); + rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage); + } + if( rc==SQLITE_OK ){ + pPager->dbFileSize = nPage; + } + } + } + return rc; +} + +/* +** Return a sanitized version of the sector-size of OS file pFile. The +** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE. +*/ +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){ + int iRet = sqlite3OsSectorSize(pFile); + if( iRet<32 ){ + iRet = 512; + }else if( iRet>MAX_SECTOR_SIZE ){ + assert( MAX_SECTOR_SIZE>=512 ); + iRet = MAX_SECTOR_SIZE; + } + return iRet; +} + +/* +** Set the value of the Pager.sectorSize variable for the given +** pager based on the value returned by the xSectorSize method +** of the open database file. The sector size will be used +** to determine the size and alignment of journal header and +** master journal pointers within created journal files. +** +** For temporary files the effective sector size is always 512 bytes. +** +** Otherwise, for non-temporary files, the effective sector size is +** the value returned by the xSectorSize() method rounded up to 32 if +** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it +** is greater than MAX_SECTOR_SIZE. +** +** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set +** the effective sector size to its minimum value (512). The purpose of +** pPager->sectorSize is to define the "blast radius" of bytes that +** might change if a crash occurs while writing to a single byte in +** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero +** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector +** size. For backwards compatibility of the rollback journal file format, +** we cannot reduce the effective sector size below 512. +*/ +static void setSectorSize(Pager *pPager){ + assert( isOpen(pPager->fd) || pPager->tempFile ); + + if( pPager->tempFile + || (sqlite3OsDeviceCharacteristics(pPager->fd) & + SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0 + ){ + /* Sector size doesn't matter for temporary files. Also, the file + ** may not have been opened yet, in which case the OsSectorSize() + ** call will segfault. */ + pPager->sectorSize = 512; + }else{ + pPager->sectorSize = sqlite3SectorSize(pPager->fd); + } +} + +/* +** Playback the journal and thus restore the database file to +** the state it was in before we started making changes. +** +** The journal file format is as follows: +** +** (1) 8 byte prefix. A copy of aJournalMagic[]. +** (2) 4 byte big-endian integer which is the number of valid page records +** in the journal. If this value is 0xffffffff, then compute the +** number of page records from the journal size. +** (3) 4 byte big-endian integer which is the initial value for the +** sanity checksum. +** (4) 4 byte integer which is the number of pages to truncate the +** database to during a rollback. +** (5) 4 byte big-endian integer which is the sector size. The header +** is this many bytes in size. +** (6) 4 byte big-endian integer which is the page size. +** (7) zero padding out to the next sector size. +** (8) Zero or more pages instances, each as follows: +** + 4 byte page number. +** + pPager->pageSize bytes of data. +** + 4 byte checksum +** +** When we speak of the journal header, we mean the first 7 items above. +** Each entry in the journal is an instance of the 8th item. +** +** Call the value from the second bullet "nRec". nRec is the number of +** valid page entries in the journal. In most cases, you can compute the +** value of nRec from the size of the journal file. But if a power +** failure occurred while the journal was being written, it could be the +** case that the size of the journal file had already been increased but +** the extra entries had not yet made it safely to disk. In such a case, +** the value of nRec computed from the file size would be too large. For +** that reason, we always use the nRec value in the header. +** +** If the nRec value is 0xffffffff it means that nRec should be computed +** from the file size. This value is used when the user selects the +** no-sync option for the journal. A power failure could lead to corruption +** in this case. But for things like temporary table (which will be +** deleted when the power is restored) we don't care. +** +** If the file opened as the journal file is not a well-formed +** journal file then all pages up to the first corrupted page are rolled +** back (or no pages if the journal header is corrupted). The journal file +** is then deleted and SQLITE_OK returned, just as if no corruption had +** been encountered. +** +** If an I/O or malloc() error occurs, the journal-file is not deleted +** and an error code is returned. +** +** The isHot parameter indicates that we are trying to rollback a journal +** that might be a hot journal. Or, it could be that the journal is +** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE. +** If the journal really is hot, reset the pager cache prior rolling +** back any content. If the journal is merely persistent, no reset is +** needed. +*/ +static int pager_playback(Pager *pPager, int isHot){ + sqlite3_vfs *pVfs = pPager->pVfs; + i64 szJ; /* Size of the journal file in bytes */ + u32 nRec; /* Number of Records in the journal */ + u32 u; /* Unsigned loop counter */ + Pgno mxPg = 0; /* Size of the original file in pages */ + int rc; /* Result code of a subroutine */ + int res = 1; /* Value returned by sqlite3OsAccess() */ + char *zMaster = 0; /* Name of master journal file if any */ + int needPagerReset; /* True to reset page prior to first page rollback */ + int nPlayback = 0; /* Total number of pages restored from journal */ + + /* Figure out how many records are in the journal. Abort early if + ** the journal is empty. + */ + assert( isOpen(pPager->jfd) ); + rc = sqlite3OsFileSize(pPager->jfd, &szJ); + if( rc!=SQLITE_OK ){ + goto end_playback; + } + + /* Read the master journal name from the journal, if it is present. + ** If a master journal file name is specified, but the file is not + ** present on disk, then the journal is not hot and does not need to be + ** played back. + ** + ** TODO: Technically the following is an error because it assumes that + ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that + ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, + ** mxPathname is 512, which is the same as the minimum allowable value + ** for pageSize. + */ + zMaster = pPager->pTmpSpace; + rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); + if( rc==SQLITE_OK && zMaster[0] ){ + rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); + } + zMaster = 0; + if( rc!=SQLITE_OK || !res ){ + goto end_playback; + } + pPager->journalOff = 0; + needPagerReset = isHot; + + /* This loop terminates either when a readJournalHdr() or + ** pager_playback_one_page() call returns SQLITE_DONE or an IO error + ** occurs. + */ + while( 1 ){ + /* Read the next journal header from the journal file. If there are + ** not enough bytes left in the journal file for a complete header, or + ** it is corrupted, then a process must have failed while writing it. + ** This indicates nothing more needs to be rolled back. + */ + rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + } + goto end_playback; + } + + /* If nRec is 0xffffffff, then this journal was created by a process + ** working in no-sync mode. This means that the rest of the journal + ** file consists of pages, there are no more journal headers. Compute + ** the value of nRec based on this assumption. + */ + if( nRec==0xffffffff ){ + assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ); + nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager)); + } + + /* If nRec is 0 and this rollback is of a transaction created by this + ** process and if this is the final header in the journal, then it means + ** that this part of the journal was being filled but has not yet been + ** synced to disk. Compute the number of pages based on the remaining + ** size of the file. + ** + ** The third term of the test was added to fix ticket #2565. + ** When rolling back a hot journal, nRec==0 always means that the next + ** chunk of the journal contains zero pages to be rolled back. But + ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in + ** the journal, it means that the journal might contain additional + ** pages that need to be rolled back and that the number of pages + ** should be computed based on the journal file size. + */ + if( nRec==0 && !isHot && + pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ + nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)); + } + + /* If this is the first header read from the journal, truncate the + ** database file back to its original size. + */ + if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){ + rc = pager_truncate(pPager, mxPg); + if( rc!=SQLITE_OK ){ + goto end_playback; + } + pPager->dbSize = mxPg; + } + + /* Copy original pages out of the journal and back into the + ** database file and/or page cache. + */ + for(u=0; ujournalOff,0,1,0); + if( rc==SQLITE_OK ){ + nPlayback++; + }else{ + if( rc==SQLITE_DONE ){ + pPager->journalOff = szJ; + break; + }else if( rc==SQLITE_IOERR_SHORT_READ ){ + /* If the journal has been truncated, simply stop reading and + ** processing the journal. This might happen if the journal was + ** not completely written and synced prior to a crash. In that + ** case, the database should have never been written in the + ** first place so it is OK to simply abandon the rollback. */ + rc = SQLITE_OK; + goto end_playback; + }else{ + /* If we are unable to rollback, quit and return the error + ** code. This will cause the pager to enter the error state + ** so that no further harm will be done. Perhaps the next + ** process to come along will be able to rollback the database. + */ + goto end_playback; + } + } + } + } + /*NOTREACHED*/ + assert( 0 ); + +end_playback: + /* Following a rollback, the database file should be back in its original + ** state prior to the start of the transaction, so invoke the + ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the + ** assertion that the transaction counter was modified. + */ +#ifdef SQLITE_DEBUG + if( pPager->fd->pMethods ){ + sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0); + } +#endif + + /* If this playback is happening automatically as a result of an IO or + ** malloc error that occurred after the change-counter was updated but + ** before the transaction was committed, then the change-counter + ** modification may just have been reverted. If this happens in exclusive + ** mode, then subsequent transactions performed by the connection will not + ** update the change-counter at all. This may lead to cache inconsistency + ** problems for other processes at some point in the future. So, just + ** in case this has happened, clear the changeCountDone flag now. + */ + pPager->changeCountDone = pPager->tempFile; + + if( rc==SQLITE_OK ){ + zMaster = pPager->pTmpSpace; + rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); + testcase( rc!=SQLITE_OK ); + } + if( rc==SQLITE_OK + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) + ){ + rc = sqlite3PagerSync(pPager, 0); + } + if( rc==SQLITE_OK ){ + rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0); + testcase( rc!=SQLITE_OK ); + } + if( rc==SQLITE_OK && zMaster[0] && res ){ + /* If there was a master journal and this routine will return success, + ** see if it is possible to delete the master journal. + */ + rc = pager_delmaster(pPager, zMaster); + testcase( rc!=SQLITE_OK ); + } + if( isHot && nPlayback ){ + sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s", + nPlayback, pPager->zJournal); + } + + /* The Pager.sectorSize variable may have been updated while rolling + ** back a journal created by a process with a different sector size + ** value. Reset it to the correct value for this process. + */ + setSectorSize(pPager); + return rc; +} + + +/* +** Read the content for page pPg out of the database file and into +** pPg->pData. A shared lock or greater must be held on the database +** file before this function is called. +** +** If page 1 is read, then the value of Pager.dbFileVers[] is set to +** the value read from the database file. +** +** If an IO error occurs, then the IO error is returned to the caller. +** Otherwise, SQLITE_OK is returned. +*/ +static int readDbPage(PgHdr *pPg, u32 iFrame){ + Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */ + Pgno pgno = pPg->pgno; /* Page number to read */ + int rc = SQLITE_OK; /* Return code */ + int pgsz = pPager->pageSize; /* Number of bytes to read */ + + assert( pPager->eState>=PAGER_READER && !MEMDB ); + assert( isOpen(pPager->fd) ); + +#ifndef SQLITE_OMIT_WAL + if( iFrame ){ + /* Try to pull the page from the write-ahead log. */ + rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData); + }else +#endif + { + i64 iOffset = (pgno-1)*(i64)pPager->pageSize; + rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset); + if( rc==SQLITE_IOERR_SHORT_READ ){ + rc = SQLITE_OK; + } + } + + if( pgno==1 ){ + if( rc ){ + /* If the read is unsuccessful, set the dbFileVers[] to something + ** that will never be a valid file version. dbFileVers[] is a copy + ** of bytes 24..39 of the database. Bytes 28..31 should always be + ** zero or the size of the database in page. Bytes 32..35 and 35..39 + ** should be page numbers which are never 0xffffffff. So filling + ** pPager->dbFileVers[] with all 0xff bytes should suffice. + ** + ** For an encrypted database, the situation is more complex: bytes + ** 24..39 of the database are white noise. But the probability of + ** white noise equaling 16 bytes of 0xff is vanishingly small so + ** we should still be ok. + */ + memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); + }else{ + u8 *dbFileVers = &((u8*)pPg->pData)[24]; + memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); + } + } + CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT); + + PAGER_INCR(sqlite3_pager_readdb_count); + PAGER_INCR(pPager->nRead); + IOTRACE(("PGIN %p %d\n", pPager, pgno)); + PAGERTRACE(("FETCH %d page %d hash(%08x)\n", + PAGERID(pPager), pgno, pager_pagehash(pPg))); + + return rc; +} + +/* +** Update the value of the change-counter at offsets 24 and 92 in +** the header and the sqlite version number at offset 96. +** +** This is an unconditional update. See also the pager_incr_changecounter() +** routine which only updates the change-counter if the update is actually +** needed, as determined by the pPager->changeCountDone state variable. +*/ +static void pager_write_changecounter(PgHdr *pPg){ + u32 change_counter; + + /* Increment the value just read and write it back to byte 24. */ + change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1; + put32bits(((char*)pPg->pData)+24, change_counter); + + /* Also store the SQLite version number in bytes 96..99 and in + ** bytes 92..95 store the change counter for which the version number + ** is valid. */ + put32bits(((char*)pPg->pData)+92, change_counter); + put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER); +} + +#ifndef SQLITE_OMIT_WAL +/* +** This function is invoked once for each page that has already been +** written into the log file when a WAL transaction is rolled back. +** Parameter iPg is the page number of said page. The pCtx argument +** is actually a pointer to the Pager structure. +** +** If page iPg is present in the cache, and has no outstanding references, +** it is discarded. Otherwise, if there are one or more outstanding +** references, the page content is reloaded from the database. If the +** attempt to reload content from the database is required and fails, +** return an SQLite error code. Otherwise, SQLITE_OK. +*/ +static int pagerUndoCallback(void *pCtx, Pgno iPg){ + int rc = SQLITE_OK; + Pager *pPager = (Pager *)pCtx; + PgHdr *pPg; + + assert( pagerUseWal(pPager) ); + pPg = sqlite3PagerLookup(pPager, iPg); + if( pPg ){ + if( sqlite3PcachePageRefcount(pPg)==1 ){ + sqlite3PcacheDrop(pPg); + }else{ + u32 iFrame = 0; + rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); + if( rc==SQLITE_OK ){ + rc = readDbPage(pPg, iFrame); + } + if( rc==SQLITE_OK ){ + pPager->xReiniter(pPg); + } + sqlite3PagerUnrefNotNull(pPg); + } + } + + /* Normally, if a transaction is rolled back, any backup processes are + ** updated as data is copied out of the rollback journal and into the + ** database. This is not generally possible with a WAL database, as + ** rollback involves simply truncating the log file. Therefore, if one + ** or more frames have already been written to the log (and therefore + ** also copied into the backup databases) as part of this transaction, + ** the backups must be restarted. + */ + sqlite3BackupRestart(pPager->pBackup); + + return rc; +} + +/* +** This function is called to rollback a transaction on a WAL database. +*/ +static int pagerRollbackWal(Pager *pPager){ + int rc; /* Return Code */ + PgHdr *pList; /* List of dirty pages to revert */ + + /* For all pages in the cache that are currently dirty or have already + ** been written (but not committed) to the log file, do one of the + ** following: + ** + ** + Discard the cached page (if refcount==0), or + ** + Reload page content from the database (if refcount>0). + */ + pPager->dbSize = pPager->dbOrigSize; + rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager); + pList = sqlite3PcacheDirtyList(pPager->pPCache); + while( pList && rc==SQLITE_OK ){ + PgHdr *pNext = pList->pDirty; + rc = pagerUndoCallback((void *)pPager, pList->pgno); + pList = pNext; + } + + return rc; +} + +/* +** This function is a wrapper around sqlite3WalFrames(). As well as logging +** the contents of the list of pages headed by pList (connected by pDirty), +** this function notifies any active backup processes that the pages have +** changed. +** +** The list of pages passed into this routine is always sorted by page number. +** Hence, if page 1 appears anywhere on the list, it will be the first page. +*/ +static int pagerWalFrames( + Pager *pPager, /* Pager object */ + PgHdr *pList, /* List of frames to log */ + Pgno nTruncate, /* Database size after this commit */ + int isCommit /* True if this is a commit */ +){ + int rc; /* Return code */ + int nList; /* Number of pages in pList */ + PgHdr *p; /* For looping over pages */ + + assert( pPager->pWal ); + assert( pList ); +#ifdef SQLITE_DEBUG + /* Verify that the page list is in accending order */ + for(p=pList; p && p->pDirty; p=p->pDirty){ + assert( p->pgno < p->pDirty->pgno ); + } +#endif + + assert( pList->pDirty==0 || isCommit ); + if( isCommit ){ + /* If a WAL transaction is being committed, there is no point in writing + ** any pages with page numbers greater than nTruncate into the WAL file. + ** They will never be read by any client. So remove them from the pDirty + ** list here. */ + PgHdr **ppNext = &pList; + nList = 0; + for(p=pList; (*ppNext = p)!=0; p=p->pDirty){ + if( p->pgno<=nTruncate ){ + ppNext = &p->pDirty; + nList++; + } + } + assert( pList ); + }else{ + nList = 1; + } + pPager->aStat[PAGER_STAT_WRITE] += nList; + + if( pList->pgno==1 ) pager_write_changecounter(pList); + rc = sqlite3WalFrames(pPager->pWal, + pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags + ); + if( rc==SQLITE_OK && pPager->pBackup ){ + for(p=pList; p; p=p->pDirty){ + sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData); + } + } + +#ifdef SQLITE_CHECK_PAGES + pList = sqlite3PcacheDirtyList(pPager->pPCache); + for(p=pList; p; p=p->pDirty){ + pager_set_pagehash(p); + } +#endif + + return rc; +} + +/* +** Begin a read transaction on the WAL. +** +** This routine used to be called "pagerOpenSnapshot()" because it essentially +** makes a snapshot of the database at the current point in time and preserves +** that snapshot for use by the reader in spite of concurrently changes by +** other writers or checkpointers. +*/ +static int pagerBeginReadTransaction(Pager *pPager){ + int rc; /* Return code */ + int changed = 0; /* True if cache must be reset */ + + assert( pagerUseWal(pPager) ); + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); + + /* sqlite3WalEndReadTransaction() was not called for the previous + ** transaction in locking_mode=EXCLUSIVE. So call it now. If we + ** are in locking_mode=NORMAL and EndRead() was previously called, + ** the duplicate call is harmless. + */ + sqlite3WalEndReadTransaction(pPager->pWal); + + rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed); + if( rc!=SQLITE_OK || changed ){ + pager_reset(pPager); + if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); + } + + return rc; +} +#endif + +/* +** This function is called as part of the transition from PAGER_OPEN +** to PAGER_READER state to determine the size of the database file +** in pages (assuming the page size currently stored in Pager.pageSize). +** +** If no error occurs, SQLITE_OK is returned and the size of the database +** in pages is stored in *pnPage. Otherwise, an error code (perhaps +** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified. +*/ +static int pagerPagecount(Pager *pPager, Pgno *pnPage){ + Pgno nPage; /* Value to return via *pnPage */ + + /* Query the WAL sub-system for the database size. The WalDbsize() + ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or + ** if the database size is not available. The database size is not + ** available from the WAL sub-system if the log file is empty or + ** contains no valid committed transactions. + */ + assert( pPager->eState==PAGER_OPEN ); + assert( pPager->eLock>=SHARED_LOCK ); + assert( isOpen(pPager->fd) ); + assert( pPager->tempFile==0 ); + nPage = sqlite3WalDbsize(pPager->pWal); + + /* If the number of pages in the database is not available from the + ** WAL sub-system, determine the page counte based on the size of + ** the database file. If the size of the database file is not an + ** integer multiple of the page-size, round up the result. + */ + if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){ + i64 n = 0; /* Size of db file in bytes */ + int rc = sqlite3OsFileSize(pPager->fd, &n); + if( rc!=SQLITE_OK ){ + return rc; + } + nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize); + } + + /* If the current number of pages in the file is greater than the + ** configured maximum pager number, increase the allowed limit so + ** that the file can be read. + */ + if( nPage>pPager->mxPgno ){ + pPager->mxPgno = (Pgno)nPage; + } + + *pnPage = nPage; + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_WAL +/* +** Check if the *-wal file that corresponds to the database opened by pPager +** exists if the database is not empy, or verify that the *-wal file does +** not exist (by deleting it) if the database file is empty. +** +** If the database is not empty and the *-wal file exists, open the pager +** in WAL mode. If the database is empty or if no *-wal file exists and +** if no error occurs, make sure Pager.journalMode is not set to +** PAGER_JOURNALMODE_WAL. +** +** Return SQLITE_OK or an error code. +** +** The caller must hold a SHARED lock on the database file to call this +** function. Because an EXCLUSIVE lock on the db file is required to delete +** a WAL on a none-empty database, this ensures there is no race condition +** between the xAccess() below and an xDelete() being executed by some +** other connection. +*/ +static int pagerOpenWalIfPresent(Pager *pPager){ + int rc = SQLITE_OK; + assert( pPager->eState==PAGER_OPEN ); + assert( pPager->eLock>=SHARED_LOCK ); + + if( !pPager->tempFile ){ + int isWal; /* True if WAL file exists */ + Pgno nPage; /* Size of the database file */ + + rc = pagerPagecount(pPager, &nPage); + if( rc ) return rc; + if( nPage==0 ){ + rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); + if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK; + isWal = 0; + }else{ + rc = sqlite3OsAccess( + pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal + ); + } + if( rc==SQLITE_OK ){ + if( isWal ){ + testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); + rc = sqlite3PagerOpenWal(pPager, 0); + }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ + pPager->journalMode = PAGER_JOURNALMODE_DELETE; + } + } + } + return rc; +} +#endif + +/* +** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback +** the entire master journal file. The case pSavepoint==NULL occurs when +** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction +** savepoint. +** +** When pSavepoint is not NULL (meaning a non-transaction savepoint is +** being rolled back), then the rollback consists of up to three stages, +** performed in the order specified: +** +** * Pages are played back from the main journal starting at byte +** offset PagerSavepoint.iOffset and continuing to +** PagerSavepoint.iHdrOffset, or to the end of the main journal +** file if PagerSavepoint.iHdrOffset is zero. +** +** * If PagerSavepoint.iHdrOffset is not zero, then pages are played +** back starting from the journal header immediately following +** PagerSavepoint.iHdrOffset to the end of the main journal file. +** +** * Pages are then played back from the sub-journal file, starting +** with the PagerSavepoint.iSubRec and continuing to the end of +** the journal file. +** +** Throughout the rollback process, each time a page is rolled back, the +** corresponding bit is set in a bitvec structure (variable pDone in the +** implementation below). This is used to ensure that a page is only +** rolled back the first time it is encountered in either journal. +** +** If pSavepoint is NULL, then pages are only played back from the main +** journal file. There is no need for a bitvec in this case. +** +** In either case, before playback commences the Pager.dbSize variable +** is reset to the value that it held at the start of the savepoint +** (or transaction). No page with a page-number greater than this value +** is played back. If one is encountered it is simply skipped. +*/ +static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ + i64 szJ; /* Effective size of the main journal */ + i64 iHdrOff; /* End of first segment of main-journal records */ + int rc = SQLITE_OK; /* Return code */ + Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ + + assert( pPager->eState!=PAGER_ERROR ); + assert( pPager->eState>=PAGER_WRITER_LOCKED ); + + /* Allocate a bitvec to use to store the set of pages rolled back */ + if( pSavepoint ){ + pDone = sqlite3BitvecCreate(pSavepoint->nOrig); + if( !pDone ){ + return SQLITE_NOMEM_BKPT; + } + } + + /* Set the database size back to the value it was before the savepoint + ** being reverted was opened. + */ + pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; + pPager->changeCountDone = pPager->tempFile; + + if( !pSavepoint && pagerUseWal(pPager) ){ + return pagerRollbackWal(pPager); + } + + /* Use pPager->journalOff as the effective size of the main rollback + ** journal. The actual file might be larger than this in + ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything + ** past pPager->journalOff is off-limits to us. + */ + szJ = pPager->journalOff; + assert( pagerUseWal(pPager)==0 || szJ==0 ); + + /* Begin by rolling back records from the main journal starting at + ** PagerSavepoint.iOffset and continuing to the next journal header. + ** There might be records in the main journal that have a page number + ** greater than the current database size (pPager->dbSize) but those + ** will be skipped automatically. Pages are added to pDone as they + ** are played back. + */ + if( pSavepoint && !pagerUseWal(pPager) ){ + iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ; + pPager->journalOff = pSavepoint->iOffset; + while( rc==SQLITE_OK && pPager->journalOffjournalOff, pDone, 1, 1); + } + assert( rc!=SQLITE_DONE ); + }else{ + pPager->journalOff = 0; + } + + /* Continue rolling back records out of the main journal starting at + ** the first journal header seen and continuing until the effective end + ** of the main journal file. Continue to skip out-of-range pages and + ** continue adding pages rolled back to pDone. + */ + while( rc==SQLITE_OK && pPager->journalOffjournalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff" + ** test is related to ticket #2565. See the discussion in the + ** pager_playback() function for additional information. + */ + if( nJRec==0 + && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff + ){ + nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)); + } + for(ii=0; rc==SQLITE_OK && iijournalOffjournalOff, pDone, 1, 1); + } + assert( rc!=SQLITE_DONE ); + } + assert( rc!=SQLITE_OK || pPager->journalOff>=szJ ); + + /* Finally, rollback pages from the sub-journal. Page that were + ** previously rolled back out of the main journal (and are hence in pDone) + ** will be skipped. Out-of-range pages are also skipped. + */ + if( pSavepoint ){ + u32 ii; /* Loop counter */ + i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize); + + if( pagerUseWal(pPager) ){ + rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData); + } + for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && iinSubRec; ii++){ + assert( offset==(i64)ii*(4+pPager->pageSize) ); + rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1); + } + assert( rc!=SQLITE_DONE ); + } + + sqlite3BitvecDestroy(pDone); + if( rc==SQLITE_OK ){ + pPager->journalOff = szJ; + } + + return rc; +} + +/* +** Change the maximum number of in-memory pages that are allowed +** before attempting to recycle clean and unused pages. +*/ +SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ + sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); +} + +/* +** Change the maximum number of in-memory pages that are allowed +** before attempting to spill pages to journal. +*/ +SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){ + return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage); +} + +/* +** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap. +*/ +static void pagerFixMaplimit(Pager *pPager){ +#if SQLITE_MAX_MMAP_SIZE>0 + sqlite3_file *fd = pPager->fd; + if( isOpen(fd) && fd->pMethods->iVersion>=3 ){ + sqlite3_int64 sz; + sz = pPager->szMmap; + pPager->bUseFetch = (sz>0); + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz); + } +#endif +} + +/* +** Change the maximum size of any memory mapping made of the database file. +*/ +SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){ + pPager->szMmap = szMmap; + pagerFixMaplimit(pPager); +} + +/* +** Free as much memory as possible from the pager. +*/ +SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){ + sqlite3PcacheShrink(pPager->pPCache); +} + +/* +** Adjust settings of the pager to those specified in the pgFlags parameter. +** +** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness +** of the database to damage due to OS crashes or power failures by +** changing the number of syncs()s when writing the journals. +** There are four levels: +** +** OFF sqlite3OsSync() is never called. This is the default +** for temporary and transient files. +** +** NORMAL The journal is synced once before writes begin on the +** database. This is normally adequate protection, but +** it is theoretically possible, though very unlikely, +** that an inopertune power failure could leave the journal +** in a state which would cause damage to the database +** when it is rolled back. +** +** FULL The journal is synced twice before writes begin on the +** database (with some additional information - the nRec field +** of the journal header - being written in between the two +** syncs). If we assume that writing a +** single disk sector is atomic, then this mode provides +** assurance that the journal will not be corrupted to the +** point of causing damage to the database during rollback. +** +** EXTRA This is like FULL except that is also syncs the directory +** that contains the rollback journal after the rollback +** journal is unlinked. +** +** The above is for a rollback-journal mode. For WAL mode, OFF continues +** to mean that no syncs ever occur. NORMAL means that the WAL is synced +** prior to the start of checkpoint and that the database file is synced +** at the conclusion of the checkpoint if the entire content of the WAL +** was written back into the database. But no sync operations occur for +** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL +** file is synced following each commit operation, in addition to the +** syncs associated with NORMAL. There is no difference between FULL +** and EXTRA for WAL mode. +** +** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The +** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync +** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an +** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL +** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the +** synchronous=FULL versus synchronous=NORMAL setting determines when +** the xSync primitive is called and is relevant to all platforms. +** +** Numeric values associated with these states are OFF==1, NORMAL=2, +** and FULL=3. +*/ +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +SQLITE_PRIVATE void sqlite3PagerSetFlags( + Pager *pPager, /* The pager to set safety level for */ + unsigned pgFlags /* Various flags */ +){ + unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK; + if( pPager->tempFile ){ + pPager->noSync = 1; + pPager->fullSync = 0; + pPager->extraSync = 0; + }else{ + pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0; + pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0; + pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0; + } + if( pPager->noSync ){ + pPager->syncFlags = 0; + pPager->ckptSyncFlags = 0; + }else if( pgFlags & PAGER_FULLFSYNC ){ + pPager->syncFlags = SQLITE_SYNC_FULL; + pPager->ckptSyncFlags = SQLITE_SYNC_FULL; + }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){ + pPager->syncFlags = SQLITE_SYNC_NORMAL; + pPager->ckptSyncFlags = SQLITE_SYNC_FULL; + }else{ + pPager->syncFlags = SQLITE_SYNC_NORMAL; + pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; + } + pPager->walSyncFlags = pPager->syncFlags; + if( pPager->fullSync ){ + pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS; + } + if( pgFlags & PAGER_CACHESPILL ){ + pPager->doNotSpill &= ~SPILLFLAG_OFF; + }else{ + pPager->doNotSpill |= SPILLFLAG_OFF; + } +} +#endif + +/* +** The following global variable is incremented whenever the library +** attempts to open a temporary file. This information is used for +** testing and analysis only. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_opentemp_count = 0; +#endif + +/* +** Open a temporary file. +** +** Write the file descriptor into *pFile. Return SQLITE_OK on success +** or some other error code if we fail. The OS will automatically +** delete the temporary file when it is closed. +** +** The flags passed to the VFS layer xOpen() call are those specified +** by parameter vfsFlags ORed with the following: +** +** SQLITE_OPEN_READWRITE +** SQLITE_OPEN_CREATE +** SQLITE_OPEN_EXCLUSIVE +** SQLITE_OPEN_DELETEONCLOSE +*/ +static int pagerOpentemp( + Pager *pPager, /* The pager object */ + sqlite3_file *pFile, /* Write the file descriptor here */ + int vfsFlags /* Flags passed through to the VFS */ +){ + int rc; /* Return code */ + +#ifdef SQLITE_TEST + sqlite3_opentemp_count++; /* Used for testing and analysis only */ +#endif + + vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; + rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0); + assert( rc!=SQLITE_OK || isOpen(pFile) ); + return rc; +} + +/* +** Set the busy handler function. +** +** The pager invokes the busy-handler if sqlite3OsLock() returns +** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock, +** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE +** lock. It does *not* invoke the busy handler when upgrading from +** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE +** (which occurs during hot-journal rollback). Summary: +** +** Transition | Invokes xBusyHandler +** -------------------------------------------------------- +** NO_LOCK -> SHARED_LOCK | Yes +** SHARED_LOCK -> RESERVED_LOCK | No +** SHARED_LOCK -> EXCLUSIVE_LOCK | No +** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes +** +** If the busy-handler callback returns non-zero, the lock is +** retried. If it returns zero, then the SQLITE_BUSY error is +** returned to the caller of the pager API function. +*/ +SQLITE_PRIVATE void sqlite3PagerSetBusyhandler( + Pager *pPager, /* Pager object */ + int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ + void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ +){ + pPager->xBusyHandler = xBusyHandler; + pPager->pBusyHandlerArg = pBusyHandlerArg; + + if( isOpen(pPager->fd) ){ + void **ap = (void **)&pPager->xBusyHandler; + assert( ((int(*)(void *))(ap[0]))==xBusyHandler ); + assert( ap[1]==pBusyHandlerArg ); + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap); + } +} + +/* +** Change the page size used by the Pager object. The new page size +** is passed in *pPageSize. +** +** If the pager is in the error state when this function is called, it +** is a no-op. The value returned is the error state error code (i.e. +** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL). +** +** Otherwise, if all of the following are true: +** +** * the new page size (value of *pPageSize) is valid (a power +** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and +** +** * there are no outstanding page references, and +** +** * the database is either not an in-memory database or it is +** an in-memory database that currently consists of zero pages. +** +** then the pager object page size is set to *pPageSize. +** +** If the page size is changed, then this function uses sqlite3PagerMalloc() +** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt +** fails, SQLITE_NOMEM is returned and the page size remains unchanged. +** In all other cases, SQLITE_OK is returned. +** +** If the page size is not changed, either because one of the enumerated +** conditions above is not true, the pager was in error state when this +** function was called, or because the memory allocation attempt failed, +** then *pPageSize is set to the old, retained page size before returning. +*/ +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ + int rc = SQLITE_OK; + + /* It is not possible to do a full assert_pager_state() here, as this + ** function may be called from within PagerOpen(), before the state + ** of the Pager object is internally consistent. + ** + ** At one point this function returned an error if the pager was in + ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that + ** there is at least one outstanding page reference, this function + ** is a no-op for that case anyhow. + */ + + u32 pageSize = *pPageSize; + assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); + if( (pPager->memDb==0 || pPager->dbSize==0) + && sqlite3PcacheRefCount(pPager->pPCache)==0 + && pageSize && pageSize!=(u32)pPager->pageSize + ){ + char *pNew = NULL; /* New temp space */ + i64 nByte = 0; + + if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){ + rc = sqlite3OsFileSize(pPager->fd, &nByte); + } + if( rc==SQLITE_OK ){ + pNew = (char *)sqlite3PageMalloc(pageSize); + if( !pNew ) rc = SQLITE_NOMEM_BKPT; + } + + if( rc==SQLITE_OK ){ + pager_reset(pPager); + rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); + } + if( rc==SQLITE_OK ){ + sqlite3PageFree(pPager->pTmpSpace); + pPager->pTmpSpace = pNew; + pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); + pPager->pageSize = pageSize; + }else{ + sqlite3PageFree(pNew); + } + } + + *pPageSize = pPager->pageSize; + if( rc==SQLITE_OK ){ + if( nReserve<0 ) nReserve = pPager->nReserve; + assert( nReserve>=0 && nReserve<1000 ); + pPager->nReserve = (i16)nReserve; + pagerReportSize(pPager); + pagerFixMaplimit(pPager); + } + return rc; +} + +/* +** Return a pointer to the "temporary page" buffer held internally +** by the pager. This is a buffer that is big enough to hold the +** entire content of a database page. This buffer is used internally +** during rollback and will be overwritten whenever a rollback +** occurs. But other modules are free to use it too, as long as +** no rollbacks are happening. +*/ +SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){ + return pPager->pTmpSpace; +} + +/* +** Attempt to set the maximum database page count if mxPage is positive. +** Make no changes if mxPage is zero or negative. And never reduce the +** maximum page count below the current size of the database. +** +** Regardless of mxPage, return the current maximum page count. +*/ +SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ + if( mxPage>0 ){ + pPager->mxPgno = mxPage; + } + assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */ + assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */ + return pPager->mxPgno; +} + +/* +** The following set of routines are used to disable the simulated +** I/O error mechanism. These routines are used to avoid simulated +** errors in places where we do not care about errors. +** +** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops +** and generate no code. +*/ +#ifdef SQLITE_TEST +SQLITE_API extern int sqlite3_io_error_pending; +SQLITE_API extern int sqlite3_io_error_hit; +static int saved_cnt; +void disable_simulated_io_errors(void){ + saved_cnt = sqlite3_io_error_pending; + sqlite3_io_error_pending = -1; +} +void enable_simulated_io_errors(void){ + sqlite3_io_error_pending = saved_cnt; +} +#else +# define disable_simulated_io_errors() +# define enable_simulated_io_errors() +#endif + +/* +** Read the first N bytes from the beginning of the file into memory +** that pDest points to. +** +** If the pager was opened on a transient file (zFilename==""), or +** opened on a file less than N bytes in size, the output buffer is +** zeroed and SQLITE_OK returned. The rationale for this is that this +** function is used to read database headers, and a new transient or +** zero sized database has a header than consists entirely of zeroes. +** +** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered, +** the error code is returned to the caller and the contents of the +** output buffer undefined. +*/ +SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ + int rc = SQLITE_OK; + memset(pDest, 0, N); + assert( isOpen(pPager->fd) || pPager->tempFile ); + + /* This routine is only called by btree immediately after creating + ** the Pager object. There has not been an opportunity to transition + ** to WAL mode yet. + */ + assert( !pagerUseWal(pPager) ); + + if( isOpen(pPager->fd) ){ + IOTRACE(("DBHDR %p 0 %d\n", pPager, N)) + rc = sqlite3OsRead(pPager->fd, pDest, N, 0); + if( rc==SQLITE_IOERR_SHORT_READ ){ + rc = SQLITE_OK; + } + } + return rc; +} + +/* +** This function may only be called when a read-transaction is open on +** the pager. It returns the total number of pages in the database. +** +** However, if the file is between 1 and bytes in size, then +** this is considered a 1 page file. +*/ +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){ + assert( pPager->eState>=PAGER_READER ); + assert( pPager->eState!=PAGER_WRITER_FINISHED ); + *pnPage = (int)pPager->dbSize; +} + + +/* +** Try to obtain a lock of type locktype on the database file. If +** a similar or greater lock is already held, this function is a no-op +** (returning SQLITE_OK immediately). +** +** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke +** the busy callback if the lock is currently not available. Repeat +** until the busy callback returns false or until the attempt to +** obtain the lock succeeds. +** +** Return SQLITE_OK on success and an error code if we cannot obtain +** the lock. If the lock is obtained successfully, set the Pager.state +** variable to locktype before returning. +*/ +static int pager_wait_on_lock(Pager *pPager, int locktype){ + int rc; /* Return code */ + + /* Check that this is either a no-op (because the requested lock is + ** already held), or one of the transitions that the busy-handler + ** may be invoked during, according to the comment above + ** sqlite3PagerSetBusyhandler(). + */ + assert( (pPager->eLock>=locktype) + || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK) + || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK) + ); + + do { + rc = pagerLockDb(pPager, locktype); + }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); + return rc; +} + +/* +** Function assertTruncateConstraint(pPager) checks that one of the +** following is true for all dirty pages currently in the page-cache: +** +** a) The page number is less than or equal to the size of the +** current database image, in pages, OR +** +** b) if the page content were written at this time, it would not +** be necessary to write the current content out to the sub-journal +** (as determined by function subjRequiresPage()). +** +** If the condition asserted by this function were not true, and the +** dirty page were to be discarded from the cache via the pagerStress() +** routine, pagerStress() would not write the current page content to +** the database file. If a savepoint transaction were rolled back after +** this happened, the correct behavior would be to restore the current +** content of the page. However, since this content is not present in either +** the database file or the portion of the rollback journal and +** sub-journal rolled back the content could not be restored and the +** database image would become corrupt. It is therefore fortunate that +** this circumstance cannot arise. +*/ +#if defined(SQLITE_DEBUG) +static void assertTruncateConstraintCb(PgHdr *pPg){ + assert( pPg->flags&PGHDR_DIRTY ); + assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize ); +} +static void assertTruncateConstraint(Pager *pPager){ + sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb); +} +#else +# define assertTruncateConstraint(pPager) +#endif + +/* +** Truncate the in-memory database file image to nPage pages. This +** function does not actually modify the database file on disk. It +** just sets the internal state of the pager object so that the +** truncation will be done when the current transaction is committed. +** +** This function is only called right before committing a transaction. +** Once this function has been called, the transaction must either be +** rolled back or committed. It is not safe to call this function and +** then continue writing to the database. +*/ +SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ + assert( pPager->dbSize>=nPage ); + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); + pPager->dbSize = nPage; + + /* At one point the code here called assertTruncateConstraint() to + ** ensure that all pages being truncated away by this operation are, + ** if one or more savepoints are open, present in the savepoint + ** journal so that they can be restored if the savepoint is rolled + ** back. This is no longer necessary as this function is now only + ** called right before committing a transaction. So although the + ** Pager object may still have open savepoints (Pager.nSavepoint!=0), + ** they cannot be rolled back. So the assertTruncateConstraint() call + ** is no longer correct. */ +} + + +/* +** This function is called before attempting a hot-journal rollback. It +** syncs the journal file to disk, then sets pPager->journalHdr to the +** size of the journal file so that the pager_playback() routine knows +** that the entire journal file has been synced. +** +** Syncing a hot-journal to disk before attempting to roll it back ensures +** that if a power-failure occurs during the rollback, the process that +** attempts rollback following system recovery sees the same journal +** content as this process. +** +** If everything goes as planned, SQLITE_OK is returned. Otherwise, +** an SQLite error code. +*/ +static int pagerSyncHotJournal(Pager *pPager){ + int rc = SQLITE_OK; + if( !pPager->noSync ){ + rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL); + } + if( rc==SQLITE_OK ){ + rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr); + } + return rc; +} + +/* +** Obtain a reference to a memory mapped page object for page number pgno. +** The new object will use the pointer pData, obtained from xFetch(). +** If successful, set *ppPage to point to the new page reference +** and return SQLITE_OK. Otherwise, return an SQLite error code and set +** *ppPage to zero. +** +** Page references obtained by calling this function should be released +** by calling pagerReleaseMapPage(). +*/ +static int pagerAcquireMapPage( + Pager *pPager, /* Pager object */ + Pgno pgno, /* Page number */ + void *pData, /* xFetch()'d data for this page */ + PgHdr **ppPage /* OUT: Acquired page object */ +){ + PgHdr *p; /* Memory mapped page to return */ + + if( pPager->pMmapFreelist ){ + *ppPage = p = pPager->pMmapFreelist; + pPager->pMmapFreelist = p->pDirty; + p->pDirty = 0; + memset(p->pExtra, 0, pPager->nExtra); + }else{ + *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra); + if( p==0 ){ + sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData); + return SQLITE_NOMEM_BKPT; + } + p->pExtra = (void *)&p[1]; + p->flags = PGHDR_MMAP; + p->nRef = 1; + p->pPager = pPager; + } + + assert( p->pExtra==(void *)&p[1] ); + assert( p->pPage==0 ); + assert( p->flags==PGHDR_MMAP ); + assert( p->pPager==pPager ); + assert( p->nRef==1 ); + + p->pgno = pgno; + p->pData = pData; + pPager->nMmapOut++; + + return SQLITE_OK; +} + +/* +** Release a reference to page pPg. pPg must have been returned by an +** earlier call to pagerAcquireMapPage(). +*/ +static void pagerReleaseMapPage(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + pPager->nMmapOut--; + pPg->pDirty = pPager->pMmapFreelist; + pPager->pMmapFreelist = pPg; + + assert( pPager->fd->pMethods->iVersion>=3 ); + sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData); +} + +/* +** Free all PgHdr objects stored in the Pager.pMmapFreelist list. +*/ +static void pagerFreeMapHdrs(Pager *pPager){ + PgHdr *p; + PgHdr *pNext; + for(p=pPager->pMmapFreelist; p; p=pNext){ + pNext = p->pDirty; + sqlite3_free(p); + } +} + + +/* +** Shutdown the page cache. Free all memory and close all files. +** +** If a transaction was in progress when this routine is called, that +** transaction is rolled back. All outstanding pages are invalidated +** and their memory is freed. Any attempt to use a page associated +** with this page cache after this function returns will likely +** result in a coredump. +** +** This function always succeeds. If a transaction is active an attempt +** is made to roll it back. If an error occurs during the rollback +** a hot journal may be left in the filesystem but no error is returned +** to the caller. +*/ +SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ + u8 *pTmp = (u8 *)pPager->pTmpSpace; + + assert( assert_pager_state(pPager) ); + disable_simulated_io_errors(); + sqlite3BeginBenignMalloc(); + pagerFreeMapHdrs(pPager); + /* pPager->errCode = 0; */ + pPager->exclusiveMode = 0; +#ifndef SQLITE_OMIT_WAL + sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp); + pPager->pWal = 0; +#endif + pager_reset(pPager); + if( MEMDB ){ + pager_unlock(pPager); + }else{ + /* If it is open, sync the journal file before calling UnlockAndRollback. + ** If this is not done, then an unsynced portion of the open journal + ** file may be played back into the database. If a power failure occurs + ** while this is happening, the database could become corrupt. + ** + ** If an error occurs while trying to sync the journal, shift the pager + ** into the ERROR state. This causes UnlockAndRollback to unlock the + ** database and close the journal file without attempting to roll it + ** back or finalize it. The next database user will have to do hot-journal + ** rollback before accessing the database file. + */ + if( isOpen(pPager->jfd) ){ + pager_error(pPager, pagerSyncHotJournal(pPager)); + } + pagerUnlockAndRollback(pPager); + } + sqlite3EndBenignMalloc(); + enable_simulated_io_errors(); + PAGERTRACE(("CLOSE %d\n", PAGERID(pPager))); + IOTRACE(("CLOSE %p\n", pPager)) + sqlite3OsClose(pPager->jfd); + sqlite3OsClose(pPager->fd); + sqlite3PageFree(pTmp); + sqlite3PcacheClose(pPager->pPCache); + +#ifdef SQLITE_HAS_CODEC + if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); +#endif + + assert( !pPager->aSavepoint && !pPager->pInJournal ); + assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ); + + sqlite3_free(pPager); + return SQLITE_OK; +} + +#if !defined(NDEBUG) || defined(SQLITE_TEST) +/* +** Return the page number for page pPg. +*/ +SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){ + return pPg->pgno; +} +#endif + +/* +** Increment the reference count for page pPg. +*/ +SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){ + sqlite3PcacheRef(pPg); +} + +/* +** Sync the journal. In other words, make sure all the pages that have +** been written to the journal have actually reached the surface of the +** disk and can be restored in the event of a hot-journal rollback. +** +** If the Pager.noSync flag is set, then this function is a no-op. +** Otherwise, the actions required depend on the journal-mode and the +** device characteristics of the file-system, as follows: +** +** * If the journal file is an in-memory journal file, no action need +** be taken. +** +** * Otherwise, if the device does not support the SAFE_APPEND property, +** then the nRec field of the most recently written journal header +** is updated to contain the number of journal records that have +** been written following it. If the pager is operating in full-sync +** mode, then the journal file is synced before this field is updated. +** +** * If the device does not support the SEQUENTIAL property, then +** journal file is synced. +** +** Or, in pseudo-code: +** +** if( NOT ){ +** if( NOT SAFE_APPEND ){ +** if( ) xSync(); +** +** } +** if( NOT SEQUENTIAL ) xSync(); +** } +** +** If successful, this routine clears the PGHDR_NEED_SYNC flag of every +** page currently held in memory before returning SQLITE_OK. If an IO +** error is encountered, then the IO error code is returned to the caller. +*/ +static int syncJournal(Pager *pPager, int newHdr){ + int rc; /* Return code */ + + assert( pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + ); + assert( assert_pager_state(pPager) ); + assert( !pagerUseWal(pPager) ); + + rc = sqlite3PagerExclusiveLock(pPager); + if( rc!=SQLITE_OK ) return rc; + + if( !pPager->noSync ){ + assert( !pPager->tempFile ); + if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ + const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); + assert( isOpen(pPager->jfd) ); + + if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ + /* This block deals with an obscure problem. If the last connection + ** that wrote to this database was operating in persistent-journal + ** mode, then the journal file may at this point actually be larger + ** than Pager.journalOff bytes. If the next thing in the journal + ** file happens to be a journal-header (written as part of the + ** previous connection's transaction), and a crash or power-failure + ** occurs after nRec is updated but before this connection writes + ** anything else to the journal file (or commits/rolls back its + ** transaction), then SQLite may become confused when doing the + ** hot-journal rollback following recovery. It may roll back all + ** of this connections data, then proceed to rolling back the old, + ** out-of-date data that follows it. Database corruption. + ** + ** To work around this, if the journal file does appear to contain + ** a valid header following Pager.journalOff, then write a 0x00 + ** byte to the start of it to prevent it from being recognized. + ** + ** Variable iNextHdrOffset is set to the offset at which this + ** problematic header will occur, if it exists. aMagic is used + ** as a temporary buffer to inspect the first couple of bytes of + ** the potential journal header. + */ + i64 iNextHdrOffset; + u8 aMagic[8]; + u8 zHeader[sizeof(aJournalMagic)+4]; + + memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); + put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec); + + iNextHdrOffset = journalHdrOffset(pPager); + rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset); + if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){ + static const u8 zerobyte = 0; + rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset); + } + if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ + return rc; + } + + /* Write the nRec value into the journal file header. If in + ** full-synchronous mode, sync the journal first. This ensures that + ** all data has really hit the disk before nRec is updated to mark + ** it as a candidate for rollback. + ** + ** This is not required if the persistent media supports the + ** SAFE_APPEND property. Because in this case it is not possible + ** for garbage data to be appended to the file, the nRec field + ** is populated with 0xFFFFFFFF when the journal header is written + ** and never needs to be updated. + */ + if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ + PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); + IOTRACE(("JSYNC %p\n", pPager)) + rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); + if( rc!=SQLITE_OK ) return rc; + } + IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr)); + rc = sqlite3OsWrite( + pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr + ); + if( rc!=SQLITE_OK ) return rc; + } + if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ + PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); + IOTRACE(("JSYNC %p\n", pPager)) + rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| + (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) + ); + if( rc!=SQLITE_OK ) return rc; + } + + pPager->journalHdr = pPager->journalOff; + if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ + pPager->nRec = 0; + rc = writeJournalHdr(pPager); + if( rc!=SQLITE_OK ) return rc; + } + }else{ + pPager->journalHdr = pPager->journalOff; + } + } + + /* Unless the pager is in noSync mode, the journal file was just + ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on + ** all pages. + */ + sqlite3PcacheClearSyncFlags(pPager->pPCache); + pPager->eState = PAGER_WRITER_DBMOD; + assert( assert_pager_state(pPager) ); + return SQLITE_OK; +} + +/* +** The argument is the first in a linked list of dirty pages connected +** by the PgHdr.pDirty pointer. This function writes each one of the +** in-memory pages in the list to the database file. The argument may +** be NULL, representing an empty list. In this case this function is +** a no-op. +** +** The pager must hold at least a RESERVED lock when this function +** is called. Before writing anything to the database file, this lock +** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained, +** SQLITE_BUSY is returned and no data is written to the database file. +** +** If the pager is a temp-file pager and the actual file-system file +** is not yet open, it is created and opened before any data is +** written out. +** +** Once the lock has been upgraded and, if necessary, the file opened, +** the pages are written out to the database file in list order. Writing +** a page is skipped if it meets either of the following criteria: +** +** * The page number is greater than Pager.dbSize, or +** * The PGHDR_DONT_WRITE flag is set on the page. +** +** If writing out a page causes the database file to grow, Pager.dbFileSize +** is updated accordingly. If page 1 is written out, then the value cached +** in Pager.dbFileVers[] is updated to match the new value stored in +** the database file. +** +** If everything is successful, SQLITE_OK is returned. If an IO error +** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot +** be obtained, SQLITE_BUSY is returned. +*/ +static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ + int rc = SQLITE_OK; /* Return code */ + + /* This function is only called for rollback pagers in WRITER_DBMOD state. */ + assert( !pagerUseWal(pPager) ); + assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD ); + assert( pPager->eLock==EXCLUSIVE_LOCK ); + assert( isOpen(pPager->fd) || pList->pDirty==0 ); + + /* If the file is a temp-file has not yet been opened, open it now. It + ** is not possible for rc to be other than SQLITE_OK if this branch + ** is taken, as pager_wait_on_lock() is a no-op for temp-files. + */ + if( !isOpen(pPager->fd) ){ + assert( pPager->tempFile && rc==SQLITE_OK ); + rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); + } + + /* Before the first write, give the VFS a hint of what the final + ** file size will be. + */ + assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); + if( rc==SQLITE_OK + && pPager->dbHintSizedbSize + && (pList->pDirty || pList->pgno>pPager->dbHintSize) + ){ + sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); + pPager->dbHintSize = pPager->dbSize; + } + + while( rc==SQLITE_OK && pList ){ + Pgno pgno = pList->pgno; + + /* If there are dirty pages in the page cache with page numbers greater + ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to + ** make the file smaller (presumably by auto-vacuum code). Do not write + ** any such pages to the file. + ** + ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag + ** set (set by sqlite3PagerDontWrite()). + */ + if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ + i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ + char *pData; /* Data to write */ + + assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); + if( pList->pgno==1 ) pager_write_changecounter(pList); + + /* Encode the database */ + CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData); + + /* Write out the page data. */ + rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); + + /* If page 1 was just written, update Pager.dbFileVers to match + ** the value now stored in the database file. If writing this + ** page caused the database file to grow, update dbFileSize. + */ + if( pgno==1 ){ + memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); + } + if( pgno>pPager->dbFileSize ){ + pPager->dbFileSize = pgno; + } + pPager->aStat[PAGER_STAT_WRITE]++; + + /* Update any backup objects copying the contents of this pager. */ + sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData); + + PAGERTRACE(("STORE %d page %d hash(%08x)\n", + PAGERID(pPager), pgno, pager_pagehash(pList))); + IOTRACE(("PGOUT %p %d\n", pPager, pgno)); + PAGER_INCR(sqlite3_pager_writedb_count); + }else{ + PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno)); + } + pager_set_pagehash(pList); + pList = pList->pDirty; + } + + return rc; +} + +/* +** Ensure that the sub-journal file is open. If it is already open, this +** function is a no-op. +** +** SQLITE_OK is returned if everything goes according to plan. An +** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() +** fails. +*/ +static int openSubJournal(Pager *pPager){ + int rc = SQLITE_OK; + if( !isOpen(pPager->sjfd) ){ + const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE + | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE + | SQLITE_OPEN_DELETEONCLOSE; + int nStmtSpill = sqlite3Config.nStmtSpill; + if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){ + nStmtSpill = -1; + } + rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill); + } + return rc; +} + +/* +** Append a record of the current state of page pPg to the sub-journal. +** +** If successful, set the bit corresponding to pPg->pgno in the bitvecs +** for all open savepoints before returning. +** +** This function returns SQLITE_OK if everything is successful, an IO +** error code if the attempt to write to the sub-journal fails, or +** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint +** bitvec. +*/ +static int subjournalPage(PgHdr *pPg){ + int rc = SQLITE_OK; + Pager *pPager = pPg->pPager; + if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ + + /* Open the sub-journal, if it has not already been opened */ + assert( pPager->useJournal ); + assert( isOpen(pPager->jfd) || pagerUseWal(pPager) ); + assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 ); + assert( pagerUseWal(pPager) + || pageInJournal(pPager, pPg) + || pPg->pgno>pPager->dbOrigSize + ); + rc = openSubJournal(pPager); + + /* If the sub-journal was opened successfully (or was already open), + ** write the journal record into the file. */ + if( rc==SQLITE_OK ){ + void *pData = pPg->pData; + i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); + char *pData2; + + CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); + PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); + rc = write32bits(pPager->sjfd, offset, pPg->pgno); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); + } + } + } + if( rc==SQLITE_OK ){ + pPager->nSubRec++; + assert( pPager->nSavepoint>0 ); + rc = addToSavepointBitvecs(pPager, pPg->pgno); + } + return rc; +} +static int subjournalPageIfRequired(PgHdr *pPg){ + if( subjRequiresPage(pPg) ){ + return subjournalPage(pPg); + }else{ + return SQLITE_OK; + } +} + +/* +** This function is called by the pcache layer when it has reached some +** soft memory limit. The first argument is a pointer to a Pager object +** (cast as a void*). The pager is always 'purgeable' (not an in-memory +** database). The second argument is a reference to a page that is +** currently dirty but has no outstanding references. The page +** is always associated with the Pager object passed as the first +** argument. +** +** The job of this function is to make pPg clean by writing its contents +** out to the database file, if possible. This may involve syncing the +** journal file. +** +** If successful, sqlite3PcacheMakeClean() is called on the page and +** SQLITE_OK returned. If an IO error occurs while trying to make the +** page clean, the IO error code is returned. If the page cannot be +** made clean for some other reason, but no error occurs, then SQLITE_OK +** is returned by sqlite3PcacheMakeClean() is not called. +*/ +static int pagerStress(void *p, PgHdr *pPg){ + Pager *pPager = (Pager *)p; + int rc = SQLITE_OK; + + assert( pPg->pPager==pPager ); + assert( pPg->flags&PGHDR_DIRTY ); + + /* The doNotSpill NOSYNC bit is set during times when doing a sync of + ** journal (and adding a new header) is not allowed. This occurs + ** during calls to sqlite3PagerWrite() while trying to journal multiple + ** pages belonging to the same sector. + ** + ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling + ** regardless of whether or not a sync is required. This is set during + ** a rollback or by user request, respectively. + ** + ** Spilling is also prohibited when in an error state since that could + ** lead to database corruption. In the current implementation it + ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3 + ** while in the error state, hence it is impossible for this routine to + ** be called in the error state. Nevertheless, we include a NEVER() + ** test for the error state as a safeguard against future changes. + */ + if( NEVER(pPager->errCode) ) return SQLITE_OK; + testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK ); + testcase( pPager->doNotSpill & SPILLFLAG_OFF ); + testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC ); + if( pPager->doNotSpill + && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0 + || (pPg->flags & PGHDR_NEED_SYNC)!=0) + ){ + return SQLITE_OK; + } + + pPg->pDirty = 0; + if( pagerUseWal(pPager) ){ + /* Write a single frame for this page to the log. */ + rc = subjournalPageIfRequired(pPg); + if( rc==SQLITE_OK ){ + rc = pagerWalFrames(pPager, pPg, 0, 0); + } + }else{ + + /* Sync the journal file if required. */ + if( pPg->flags&PGHDR_NEED_SYNC + || pPager->eState==PAGER_WRITER_CACHEMOD + ){ + rc = syncJournal(pPager, 1); + } + + /* Write the contents of the page out to the database file. */ + if( rc==SQLITE_OK ){ + assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); + rc = pager_write_pagelist(pPager, pPg); + } + } + + /* Mark the page as clean. */ + if( rc==SQLITE_OK ){ + PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno)); + sqlite3PcacheMakeClean(pPg); + } + + return pager_error(pPager, rc); +} + +/* +** Flush all unreferenced dirty pages to disk. +*/ +SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){ + int rc = pPager->errCode; + if( !MEMDB ){ + PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); + assert( assert_pager_state(pPager) ); + while( rc==SQLITE_OK && pList ){ + PgHdr *pNext = pList->pDirty; + if( pList->nRef==0 ){ + rc = pagerStress((void*)pPager, pList); + } + pList = pNext; + } + } + + return rc; +} + +/* +** Allocate and initialize a new Pager object and put a pointer to it +** in *ppPager. The pager should eventually be freed by passing it +** to sqlite3PagerClose(). +** +** The zFilename argument is the path to the database file to open. +** If zFilename is NULL then a randomly-named temporary file is created +** and used as the file to be cached. Temporary files are be deleted +** automatically when they are closed. If zFilename is ":memory:" then +** all information is held in cache. It is never written to disk. +** This can be used to implement an in-memory database. +** +** The nExtra parameter specifies the number of bytes of space allocated +** along with each page reference. This space is available to the user +** via the sqlite3PagerGetExtra() API. +** +** The flags argument is used to specify properties that affect the +** operation of the pager. It should be passed some bitwise combination +** of the PAGER_* flags. +** +** The vfsFlags parameter is a bitmask to pass to the flags parameter +** of the xOpen() method of the supplied VFS when opening files. +** +** If the pager object is allocated and the specified file opened +** successfully, SQLITE_OK is returned and *ppPager set to point to +** the new pager object. If an error occurs, *ppPager is set to NULL +** and error code returned. This function may return SQLITE_NOMEM +** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or +** various SQLITE_IO_XXX errors. +*/ +SQLITE_PRIVATE int sqlite3PagerOpen( + sqlite3_vfs *pVfs, /* The virtual file system to use */ + Pager **ppPager, /* OUT: Return the Pager structure here */ + const char *zFilename, /* Name of the database file to open */ + int nExtra, /* Extra bytes append to each in-memory page */ + int flags, /* flags controlling this file */ + int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */ + void (*xReinit)(DbPage*) /* Function to reinitialize pages */ +){ + u8 *pPtr; + Pager *pPager = 0; /* Pager object to allocate and return */ + int rc = SQLITE_OK; /* Return code */ + int tempFile = 0; /* True for temp files (incl. in-memory files) */ + int memDb = 0; /* True if this is an in-memory file */ + int readOnly = 0; /* True if this is a read-only file */ + int journalFileSize; /* Bytes to allocate for each journal fd */ + char *zPathname = 0; /* Full path to database file */ + int nPathname = 0; /* Number of bytes in zPathname */ + int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ + int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ + u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ + const char *zUri = 0; /* URI args to copy */ + int nUri = 0; /* Number of bytes of URI args at *zUri */ + + /* Figure out how much space is required for each journal file-handle + ** (there are two of them, the main journal and the sub-journal). */ + journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); + + /* Set the output variable to NULL in case an error occurs. */ + *ppPager = 0; + +#ifndef SQLITE_OMIT_MEMORYDB + if( flags & PAGER_MEMORY ){ + memDb = 1; + if( zFilename && zFilename[0] ){ + zPathname = sqlite3DbStrDup(0, zFilename); + if( zPathname==0 ) return SQLITE_NOMEM_BKPT; + nPathname = sqlite3Strlen30(zPathname); + zFilename = 0; + } + } +#endif + + /* Compute and store the full pathname in an allocated buffer pointed + ** to by zPathname, length nPathname. Or, if this is a temporary file, + ** leave both nPathname and zPathname set to 0. + */ + if( zFilename && zFilename[0] ){ + const char *z; + nPathname = pVfs->mxPathname+1; + zPathname = sqlite3DbMallocRaw(0, nPathname*2); + if( zPathname==0 ){ + return SQLITE_NOMEM_BKPT; + } + zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ + rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); + nPathname = sqlite3Strlen30(zPathname); + z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1]; + while( *z ){ + z += sqlite3Strlen30(z)+1; + z += sqlite3Strlen30(z)+1; + } + nUri = (int)(&z[1] - zUri); + assert( nUri>=0 ); + if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ + /* This branch is taken when the journal path required by + ** the database being opened will be more than pVfs->mxPathname + ** bytes in length. This means the database cannot be opened, + ** as it will not be possible to open the journal file or even + ** check for a hot-journal before reading. + */ + rc = SQLITE_CANTOPEN_BKPT; + } + if( rc!=SQLITE_OK ){ + sqlite3DbFree(0, zPathname); + return rc; + } + } + + /* Allocate memory for the Pager structure, PCache object, the + ** three file descriptors, the database file name and the journal + ** file name. The layout in memory is as follows: + ** + ** Pager object (sizeof(Pager) bytes) + ** PCache object (sqlite3PcacheSize() bytes) + ** Database file handle (pVfs->szOsFile bytes) + ** Sub-journal file handle (journalFileSize bytes) + ** Main journal file handle (journalFileSize bytes) + ** Database file name (nPathname+1 bytes) + ** Journal file name (nPathname+8+1 bytes) + */ + pPtr = (u8 *)sqlite3MallocZero( + ROUND8(sizeof(*pPager)) + /* Pager structure */ + ROUND8(pcacheSize) + /* PCache object */ + ROUND8(pVfs->szOsFile) + /* The main db file */ + journalFileSize * 2 + /* The two journal files */ + nPathname + 1 + nUri + /* zFilename */ + nPathname + 8 + 2 /* zJournal */ +#ifndef SQLITE_OMIT_WAL + + nPathname + 4 + 2 /* zWal */ +#endif + ); + assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); + if( !pPtr ){ + sqlite3DbFree(0, zPathname); + return SQLITE_NOMEM_BKPT; + } + pPager = (Pager*)(pPtr); + pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); + pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); + pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); + pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); + pPager->zFilename = (char*)(pPtr += journalFileSize); + assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); + + /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ + if( zPathname ){ + assert( nPathname>0 ); + pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri); + memcpy(pPager->zFilename, zPathname, nPathname); + if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri); + memcpy(pPager->zJournal, zPathname, nPathname); + memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2); + sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal); +#ifndef SQLITE_OMIT_WAL + pPager->zWal = &pPager->zJournal[nPathname+8+1]; + memcpy(pPager->zWal, zPathname, nPathname); + memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1); + sqlite3FileSuffix3(pPager->zFilename, pPager->zWal); +#endif + sqlite3DbFree(0, zPathname); + } + pPager->pVfs = pVfs; + pPager->vfsFlags = vfsFlags; + + /* Open the pager file. + */ + if( zFilename && zFilename[0] ){ + int fout = 0; /* VFS flags returned by xOpen() */ + rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); + assert( !memDb ); + readOnly = (fout&SQLITE_OPEN_READONLY); + + /* If the file was successfully opened for read/write access, + ** choose a default page size in case we have to create the + ** database file. The default page size is the maximum of: + ** + ** + SQLITE_DEFAULT_PAGE_SIZE, + ** + The value returned by sqlite3OsSectorSize() + ** + The largest page size that can be written atomically. + */ + if( rc==SQLITE_OK ){ + int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); + if( !readOnly ){ + setSectorSize(pPager); + assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE); + if( szPageDfltsectorSize ){ + if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ + szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; + }else{ + szPageDflt = (u32)pPager->sectorSize; + } + } +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + { + int ii; + assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); + assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); + assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536); + for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){ + if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){ + szPageDflt = ii; + } + } + } +#endif + } + pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0); + if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0 + || sqlite3_uri_boolean(zFilename, "immutable", 0) ){ + vfsFlags |= SQLITE_OPEN_READONLY; + goto act_like_temp_file; + } + } + }else{ + /* If a temporary file is requested, it is not opened immediately. + ** In this case we accept the default page size and delay actually + ** opening the file until the first call to OsWrite(). + ** + ** This branch is also run for an in-memory database. An in-memory + ** database is the same as a temp-file that is never written out to + ** disk and uses an in-memory rollback journal. + ** + ** This branch also runs for files marked as immutable. + */ +act_like_temp_file: + tempFile = 1; + pPager->eState = PAGER_READER; /* Pretend we already have a lock */ + pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */ + pPager->noLock = 1; /* Do no locking */ + readOnly = (vfsFlags&SQLITE_OPEN_READONLY); + } + + /* The following call to PagerSetPagesize() serves to set the value of + ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. + */ + if( rc==SQLITE_OK ){ + assert( pPager->memDb==0 ); + rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1); + testcase( rc!=SQLITE_OK ); + } + + /* Initialize the PCache object. */ + if( rc==SQLITE_OK ){ + assert( nExtra<1000 ); + nExtra = ROUND8(nExtra); + rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, + !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); + } + + /* If an error occurred above, free the Pager structure and close the file. + */ + if( rc!=SQLITE_OK ){ + sqlite3OsClose(pPager->fd); + sqlite3PageFree(pPager->pTmpSpace); + sqlite3_free(pPager); + return rc; + } + + PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename)); + IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) + + pPager->useJournal = (u8)useJournal; + /* pPager->stmtOpen = 0; */ + /* pPager->stmtInUse = 0; */ + /* pPager->nRef = 0; */ + /* pPager->stmtSize = 0; */ + /* pPager->stmtJSize = 0; */ + /* pPager->nPage = 0; */ + pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; + /* pPager->state = PAGER_UNLOCK; */ + /* pPager->errMask = 0; */ + pPager->tempFile = (u8)tempFile; + assert( tempFile==PAGER_LOCKINGMODE_NORMAL + || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); + assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); + pPager->exclusiveMode = (u8)tempFile; + pPager->changeCountDone = pPager->tempFile; + pPager->memDb = (u8)memDb; + pPager->readOnly = (u8)readOnly; + assert( useJournal || pPager->tempFile ); + pPager->noSync = pPager->tempFile; + if( pPager->noSync ){ + assert( pPager->fullSync==0 ); + assert( pPager->extraSync==0 ); + assert( pPager->syncFlags==0 ); + assert( pPager->walSyncFlags==0 ); + assert( pPager->ckptSyncFlags==0 ); + }else{ + pPager->fullSync = 1; + pPager->extraSync = 0; + pPager->syncFlags = SQLITE_SYNC_NORMAL; + pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS; + pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; + } + /* pPager->pFirst = 0; */ + /* pPager->pFirstSynced = 0; */ + /* pPager->pLast = 0; */ + pPager->nExtra = (u16)nExtra; + pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; + assert( isOpen(pPager->fd) || tempFile ); + setSectorSize(pPager); + if( !useJournal ){ + pPager->journalMode = PAGER_JOURNALMODE_OFF; + }else if( memDb ){ + pPager->journalMode = PAGER_JOURNALMODE_MEMORY; + } + /* pPager->xBusyHandler = 0; */ + /* pPager->pBusyHandlerArg = 0; */ + pPager->xReiniter = xReinit; + /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ + /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ + + *ppPager = pPager; + return SQLITE_OK; +} + + +/* Verify that the database file has not be deleted or renamed out from +** under the pager. Return SQLITE_OK if the database is still were it ought +** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error +** code from sqlite3OsAccess()) if the database has gone missing. +*/ +static int databaseIsUnmoved(Pager *pPager){ + int bHasMoved = 0; + int rc; + + if( pPager->tempFile ) return SQLITE_OK; + if( pPager->dbSize==0 ) return SQLITE_OK; + assert( pPager->zFilename && pPager->zFilename[0] ); + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved); + if( rc==SQLITE_NOTFOUND ){ + /* If the HAS_MOVED file-control is unimplemented, assume that the file + ** has not been moved. That is the historical behavior of SQLite: prior to + ** version 3.8.3, it never checked */ + rc = SQLITE_OK; + }else if( rc==SQLITE_OK && bHasMoved ){ + rc = SQLITE_READONLY_DBMOVED; + } + return rc; +} + + +/* +** This function is called after transitioning from PAGER_UNLOCK to +** PAGER_SHARED state. It tests if there is a hot journal present in +** the file-system for the given pager. A hot journal is one that +** needs to be played back. According to this function, a hot-journal +** file exists if the following criteria are met: +** +** * The journal file exists in the file system, and +** * No process holds a RESERVED or greater lock on the database file, and +** * The database file itself is greater than 0 bytes in size, and +** * The first byte of the journal file exists and is not 0x00. +** +** If the current size of the database file is 0 but a journal file +** exists, that is probably an old journal left over from a prior +** database with the same name. In this case the journal file is +** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK +** is returned. +** +** This routine does not check if there is a master journal filename +** at the end of the file. If there is, and that master journal file +** does not exist, then the journal file is not really hot. In this +** case this routine will return a false-positive. The pager_playback() +** routine will discover that the journal file is not really hot and +** will not roll it back. +** +** If a hot-journal file is found to exist, *pExists is set to 1 and +** SQLITE_OK returned. If no hot-journal file is present, *pExists is +** set to 0 and SQLITE_OK returned. If an IO error occurs while trying +** to determine whether or not a hot-journal file exists, the IO error +** code is returned and the value of *pExists is undefined. +*/ +static int hasHotJournal(Pager *pPager, int *pExists){ + sqlite3_vfs * const pVfs = pPager->pVfs; + int rc = SQLITE_OK; /* Return code */ + int exists = 1; /* True if a journal file is present */ + int jrnlOpen = !!isOpen(pPager->jfd); + + assert( pPager->useJournal ); + assert( isOpen(pPager->fd) ); + assert( pPager->eState==PAGER_OPEN ); + + assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) & + SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN + )); + + *pExists = 0; + if( !jrnlOpen ){ + rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); + } + if( rc==SQLITE_OK && exists ){ + int locked = 0; /* True if some process holds a RESERVED lock */ + + /* Race condition here: Another process might have been holding the + ** the RESERVED lock and have a journal open at the sqlite3OsAccess() + ** call above, but then delete the journal and drop the lock before + ** we get to the following sqlite3OsCheckReservedLock() call. If that + ** is the case, this routine might think there is a hot journal when + ** in fact there is none. This results in a false-positive which will + ** be dealt with by the playback routine. Ticket #3883. + */ + rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); + if( rc==SQLITE_OK && !locked ){ + Pgno nPage; /* Number of pages in database file */ + + assert( pPager->tempFile==0 ); + rc = pagerPagecount(pPager, &nPage); + if( rc==SQLITE_OK ){ + /* If the database is zero pages in size, that means that either (1) the + ** journal is a remnant from a prior database with the same name where + ** the database file but not the journal was deleted, or (2) the initial + ** transaction that populates a new database is being rolled back. + ** In either case, the journal file can be deleted. However, take care + ** not to delete the journal file if it is already open due to + ** journal_mode=PERSIST. + */ + if( nPage==0 && !jrnlOpen ){ + sqlite3BeginBenignMalloc(); + if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){ + sqlite3OsDelete(pVfs, pPager->zJournal, 0); + if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); + } + sqlite3EndBenignMalloc(); + }else{ + /* The journal file exists and no other connection has a reserved + ** or greater lock on the database file. Now check that there is + ** at least one non-zero bytes at the start of the journal file. + ** If there is, then we consider this journal to be hot. If not, + ** it can be ignored. + */ + if( !jrnlOpen ){ + int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL; + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f); + } + if( rc==SQLITE_OK ){ + u8 first = 0; + rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0); + if( rc==SQLITE_IOERR_SHORT_READ ){ + rc = SQLITE_OK; + } + if( !jrnlOpen ){ + sqlite3OsClose(pPager->jfd); + } + *pExists = (first!=0); + }else if( rc==SQLITE_CANTOPEN ){ + /* If we cannot open the rollback journal file in order to see if + ** it has a zero header, that might be due to an I/O error, or + ** it might be due to the race condition described above and in + ** ticket #3883. Either way, assume that the journal is hot. + ** This might be a false positive. But if it is, then the + ** automatic journal playback and recovery mechanism will deal + ** with it under an EXCLUSIVE lock where we do not need to + ** worry so much with race conditions. + */ + *pExists = 1; + rc = SQLITE_OK; + } + } + } + } + } + + return rc; +} + +/* +** This function is called to obtain a shared lock on the database file. +** It is illegal to call sqlite3PagerGet() until after this function +** has been successfully called. If a shared-lock is already held when +** this function is called, it is a no-op. +** +** The following operations are also performed by this function. +** +** 1) If the pager is currently in PAGER_OPEN state (no lock held +** on the database file), then an attempt is made to obtain a +** SHARED lock on the database file. Immediately after obtaining +** the SHARED lock, the file-system is checked for a hot-journal, +** which is played back if present. Following any hot-journal +** rollback, the contents of the cache are validated by checking +** the 'change-counter' field of the database file header and +** discarded if they are found to be invalid. +** +** 2) If the pager is running in exclusive-mode, and there are currently +** no outstanding references to any pages, and is in the error state, +** then an attempt is made to clear the error state by discarding +** the contents of the page cache and rolling back any open journal +** file. +** +** If everything is successful, SQLITE_OK is returned. If an IO error +** occurs while locking the database, checking for a hot-journal file or +** rolling back a journal file, the IO error code is returned. +*/ +SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + int rc = SQLITE_OK; /* Return code */ + + /* This routine is only called from b-tree and only when there are no + ** outstanding pages. This implies that the pager state should either + ** be OPEN or READER. READER is only possible if the pager is or was in + ** exclusive access mode. */ + assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); + assert( assert_pager_state(pPager) ); + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); + assert( pPager->errCode==SQLITE_OK ); + + if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ + int bHotJournal = 1; /* True if there exists a hot journal-file */ + + assert( !MEMDB ); + assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK ); + + rc = pager_wait_on_lock(pPager, SHARED_LOCK); + if( rc!=SQLITE_OK ){ + assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK ); + goto failed; + } + + /* If a journal file exists, and there is no RESERVED lock on the + ** database file, then it either needs to be played back or deleted. + */ + if( pPager->eLock<=SHARED_LOCK ){ + rc = hasHotJournal(pPager, &bHotJournal); + } + if( rc!=SQLITE_OK ){ + goto failed; + } + if( bHotJournal ){ + if( pPager->readOnly ){ + rc = SQLITE_READONLY_ROLLBACK; + goto failed; + } + + /* Get an EXCLUSIVE lock on the database file. At this point it is + ** important that a RESERVED lock is not obtained on the way to the + ** EXCLUSIVE lock. If it were, another process might open the + ** database file, detect the RESERVED lock, and conclude that the + ** database is safe to read while this process is still rolling the + ** hot-journal back. + ** + ** Because the intermediate RESERVED lock is not requested, any + ** other process attempting to access the database file will get to + ** this point in the code and fail to obtain its own EXCLUSIVE lock + ** on the database file. + ** + ** Unless the pager is in locking_mode=exclusive mode, the lock is + ** downgraded to SHARED_LOCK before this function returns. + */ + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); + if( rc!=SQLITE_OK ){ + goto failed; + } + + /* If it is not already open and the file exists on disk, open the + ** journal for read/write access. Write access is required because + ** in exclusive-access mode the file descriptor will be kept open + ** and possibly used for a transaction later on. Also, write-access + ** is usually required to finalize the journal in journal_mode=persist + ** mode (and also for journal_mode=truncate on some systems). + ** + ** If the journal does not exist, it usually means that some + ** other connection managed to get in and roll it back before + ** this connection obtained the exclusive lock above. Or, it + ** may mean that the pager was in the error-state when this + ** function was called and the journal file does not exist. + */ + if( !isOpen(pPager->jfd) ){ + sqlite3_vfs * const pVfs = pPager->pVfs; + int bExists; /* True if journal file exists */ + rc = sqlite3OsAccess( + pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists); + if( rc==SQLITE_OK && bExists ){ + int fout = 0; + int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; + assert( !pPager->tempFile ); + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); + assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); + if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){ + rc = SQLITE_CANTOPEN_BKPT; + sqlite3OsClose(pPager->jfd); + } + } + } + + /* Playback and delete the journal. Drop the database write + ** lock and reacquire the read lock. Purge the cache before + ** playing back the hot-journal so that we don't end up with + ** an inconsistent cache. Sync the hot journal before playing + ** it back since the process that crashed and left the hot journal + ** probably did not sync it and we are required to always sync + ** the journal before playing it back. + */ + if( isOpen(pPager->jfd) ){ + assert( rc==SQLITE_OK ); + rc = pagerSyncHotJournal(pPager); + if( rc==SQLITE_OK ){ + rc = pager_playback(pPager, !pPager->tempFile); + pPager->eState = PAGER_OPEN; + } + }else if( !pPager->exclusiveMode ){ + pagerUnlockDb(pPager, SHARED_LOCK); + } + + if( rc!=SQLITE_OK ){ + /* This branch is taken if an error occurs while trying to open + ** or roll back a hot-journal while holding an EXCLUSIVE lock. The + ** pager_unlock() routine will be called before returning to unlock + ** the file. If the unlock attempt fails, then Pager.eLock must be + ** set to UNKNOWN_LOCK (see the comment above the #define for + ** UNKNOWN_LOCK above for an explanation). + ** + ** In order to get pager_unlock() to do this, set Pager.eState to + ** PAGER_ERROR now. This is not actually counted as a transition + ** to ERROR state in the state diagram at the top of this file, + ** since we know that the same call to pager_unlock() will very + ** shortly transition the pager object to the OPEN state. Calling + ** assert_pager_state() would fail now, as it should not be possible + ** to be in ERROR state when there are zero outstanding page + ** references. + */ + pager_error(pPager, rc); + goto failed; + } + + assert( pPager->eState==PAGER_OPEN ); + assert( (pPager->eLock==SHARED_LOCK) + || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK) + ); + } + + if( !pPager->tempFile && pPager->hasHeldSharedLock ){ + /* The shared-lock has just been acquired then check to + ** see if the database has been modified. If the database has changed, + ** flush the cache. The hasHeldSharedLock flag prevents this from + ** occurring on the very first access to a file, in order to save a + ** single unnecessary sqlite3OsRead() call at the start-up. + ** + ** Database changes are detected by looking at 15 bytes beginning + ** at offset 24 into the file. The first 4 of these 16 bytes are + ** a 32-bit counter that is incremented with each change. The + ** other bytes change randomly with each file change when + ** a codec is in use. + ** + ** There is a vanishingly small chance that a change will not be + ** detected. The chance of an undetected change is so small that + ** it can be neglected. + */ + Pgno nPage = 0; + char dbFileVers[sizeof(pPager->dbFileVers)]; + + rc = pagerPagecount(pPager, &nPage); + if( rc ) goto failed; + + if( nPage>0 ){ + IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); + rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); + if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ + goto failed; + } + }else{ + memset(dbFileVers, 0, sizeof(dbFileVers)); + } + + if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ + pager_reset(pPager); + + /* Unmap the database file. It is possible that external processes + ** may have truncated the database file and then extended it back + ** to its original size while this process was not holding a lock. + ** In this case there may exist a Pager.pMap mapping that appears + ** to be the right size but is not actually valid. Avoid this + ** possibility by unmapping the db here. */ + if( USEFETCH(pPager) ){ + sqlite3OsUnfetch(pPager->fd, 0, 0); + } + } + } + + /* If there is a WAL file in the file-system, open this database in WAL + ** mode. Otherwise, the following function call is a no-op. + */ + rc = pagerOpenWalIfPresent(pPager); +#ifndef SQLITE_OMIT_WAL + assert( pPager->pWal==0 || rc==SQLITE_OK ); +#endif + } + + if( pagerUseWal(pPager) ){ + assert( rc==SQLITE_OK ); + rc = pagerBeginReadTransaction(pPager); + } + + if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ + rc = pagerPagecount(pPager, &pPager->dbSize); + } + + failed: + if( rc!=SQLITE_OK ){ + assert( !MEMDB ); + pager_unlock(pPager); + assert( pPager->eState==PAGER_OPEN ); + }else{ + pPager->eState = PAGER_READER; + pPager->hasHeldSharedLock = 1; + } + return rc; +} + +/* +** If the reference count has reached zero, rollback any active +** transaction and unlock the pager. +** +** Except, in locking_mode=EXCLUSIVE when there is nothing to in +** the rollback journal, the unlock is not performed and there is +** nothing to rollback, so this routine is a no-op. +*/ +static void pagerUnlockIfUnused(Pager *pPager){ + if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ + pagerUnlockAndRollback(pPager); + } +} + +/* +** Acquire a reference to page number pgno in pager pPager (a page +** reference has type DbPage*). If the requested reference is +** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. +** +** If the requested page is already in the cache, it is returned. +** Otherwise, a new page object is allocated and populated with data +** read from the database file. In some cases, the pcache module may +** choose not to allocate a new page object and may reuse an existing +** object with no outstanding references. +** +** The extra data appended to a page is always initialized to zeros the +** first time a page is loaded into memory. If the page requested is +** already in the cache when this function is called, then the extra +** data is left as it was when the page object was last used. +** +** If the database image is smaller than the requested page or if a +** non-zero value is passed as the noContent parameter and the +** requested page is not already stored in the cache, then no +** actual disk read occurs. In this case the memory image of the +** page is initialized to all zeros. +** +** If noContent is true, it means that we do not care about the contents +** of the page. This occurs in two scenarios: +** +** a) When reading a free-list leaf page from the database, and +** +** b) When a savepoint is being rolled back and we need to load +** a new page into the cache to be filled with the data read +** from the savepoint journal. +** +** If noContent is true, then the data returned is zeroed instead of +** being read from the database. Additionally, the bits corresponding +** to pgno in Pager.pInJournal (bitvec of pages already written to the +** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open +** savepoints are set. This means if the page is made writable at any +** point in the future, using a call to sqlite3PagerWrite(), its contents +** will not be journaled. This saves IO. +** +** The acquisition might fail for several reasons. In all cases, +** an appropriate error code is returned and *ppPage is set to NULL. +** +** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt +** to find a page in the in-memory cache first. If the page is not already +** in memory, this routine goes to disk to read it in whereas Lookup() +** just returns 0. This routine acquires a read-lock the first time it +** has to go to disk, and could also playback an old journal if necessary. +** Since Lookup() never goes to disk, it never has to deal with locks +** or journal files. +*/ +SQLITE_PRIVATE int sqlite3PagerGet( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int flags /* PAGER_GET_XXX flags */ +){ + int rc = SQLITE_OK; + PgHdr *pPg = 0; + u32 iFrame = 0; /* Frame to read from WAL file */ + const int noContent = (flags & PAGER_GET_NOCONTENT); + + /* It is acceptable to use a read-only (mmap) page for any page except + ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY + ** flag was specified by the caller. And so long as the db is not a + ** temporary or in-memory database. */ + const int bMmapOk = (pgno>1 && USEFETCH(pPager) + && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) +#ifdef SQLITE_HAS_CODEC + && pPager->xCodec==0 +#endif + ); + + /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here + ** allows the compiler optimizer to reuse the results of the "pgno>1" + ** test in the previous statement, and avoid testing pgno==0 in the + ** common case where pgno is large. */ + if( pgno<=1 && pgno==0 ){ + return SQLITE_CORRUPT_BKPT; + } + assert( pPager->eState>=PAGER_READER ); + assert( assert_pager_state(pPager) ); + assert( noContent==0 || bMmapOk==0 ); + + assert( pPager->hasHeldSharedLock==1 ); + + /* If the pager is in the error state, return an error immediately. + ** Otherwise, request the page from the PCache layer. */ + if( pPager->errCode!=SQLITE_OK ){ + rc = pPager->errCode; + }else{ + if( bMmapOk && pagerUseWal(pPager) ){ + rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); + if( rc!=SQLITE_OK ) goto pager_acquire_err; + } + + if( bMmapOk && iFrame==0 ){ + void *pData = 0; + + rc = sqlite3OsFetch(pPager->fd, + (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData + ); + + if( rc==SQLITE_OK && pData ){ + if( pPager->eState>PAGER_READER || pPager->tempFile ){ + pPg = sqlite3PagerLookup(pPager, pgno); + } + if( pPg==0 ){ + rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); + }else{ + sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); + } + if( pPg ){ + assert( rc==SQLITE_OK ); + *ppPage = pPg; + return SQLITE_OK; + } + } + if( rc!=SQLITE_OK ){ + goto pager_acquire_err; + } + } + + { + sqlite3_pcache_page *pBase; + pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); + if( pBase==0 ){ + rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); + if( rc!=SQLITE_OK ) goto pager_acquire_err; + if( pBase==0 ){ + pPg = *ppPage = 0; + rc = SQLITE_NOMEM_BKPT; + goto pager_acquire_err; + } + } + pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); + assert( pPg!=0 ); + } + } + + if( rc!=SQLITE_OK ){ + /* Either the call to sqlite3PcacheFetch() returned an error or the + ** pager was already in the error-state when this function was called. + ** Set pPg to 0 and jump to the exception handler. */ + pPg = 0; + goto pager_acquire_err; + } + assert( pPg==(*ppPage) ); + assert( pPg->pgno==pgno ); + assert( pPg->pPager==pPager || pPg->pPager==0 ); + + if( pPg->pPager && !noContent ){ + /* In this case the pcache already contains an initialized copy of + ** the page. Return without further ado. */ + assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); + pPager->aStat[PAGER_STAT_HIT]++; + return SQLITE_OK; + + }else{ + /* The pager cache has created a new page. Its content needs to + ** be initialized. */ + + pPg->pPager = pPager; + + /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page + ** number greater than this, or the unused locking-page, is requested. */ + if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ + rc = SQLITE_CORRUPT_BKPT; + goto pager_acquire_err; + } + + assert( !isOpen(pPager->fd) || !MEMDB ); + if( !isOpen(pPager->fd) || pPager->dbSizepPager->mxPgno ){ + rc = SQLITE_FULL; + goto pager_acquire_err; + } + if( noContent ){ + /* Failure to set the bits in the InJournal bit-vectors is benign. + ** It merely means that we might do some extra work to journal a + ** page that does not need to be journaled. Nevertheless, be sure + ** to test the case where a malloc error occurs while trying to set + ** a bit in a bit vector. + */ + sqlite3BeginBenignMalloc(); + if( pgno<=pPager->dbOrigSize ){ + TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno); + testcase( rc==SQLITE_NOMEM ); + } + TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno); + testcase( rc==SQLITE_NOMEM ); + sqlite3EndBenignMalloc(); + } + memset(pPg->pData, 0, pPager->pageSize); + IOTRACE(("ZERO %p %d\n", pPager, pgno)); + }else{ + if( pagerUseWal(pPager) && bMmapOk==0 ){ + rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); + if( rc!=SQLITE_OK ) goto pager_acquire_err; + } + assert( pPg->pPager==pPager ); + pPager->aStat[PAGER_STAT_MISS]++; + rc = readDbPage(pPg, iFrame); + if( rc!=SQLITE_OK ){ + goto pager_acquire_err; + } + } + pager_set_pagehash(pPg); + } + + return SQLITE_OK; + +pager_acquire_err: + assert( rc!=SQLITE_OK ); + if( pPg ){ + sqlite3PcacheDrop(pPg); + } + pagerUnlockIfUnused(pPager); + + *ppPage = 0; + return rc; +} + +/* +** Acquire a page if it is already in the in-memory cache. Do +** not read the page from disk. Return a pointer to the page, +** or 0 if the page is not in cache. +** +** See also sqlite3PagerGet(). The difference between this routine +** and sqlite3PagerGet() is that _get() will go to the disk and read +** in the page if the page is not already in cache. This routine +** returns NULL if the page is not in cache or if a disk I/O error +** has ever happened. +*/ +SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ + sqlite3_pcache_page *pPage; + assert( pPager!=0 ); + assert( pgno!=0 ); + assert( pPager->pPCache!=0 ); + pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0); + assert( pPage==0 || pPager->hasHeldSharedLock ); + if( pPage==0 ) return 0; + return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage); +} + +/* +** Release a page reference. +** +** If the number of references to the page drop to zero, then the +** page is added to the LRU list. When all references to all pages +** are released, a rollback occurs and the lock on the database is +** removed. +*/ +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){ + Pager *pPager; + assert( pPg!=0 ); + pPager = pPg->pPager; + if( pPg->flags & PGHDR_MMAP ){ + pagerReleaseMapPage(pPg); + }else{ + sqlite3PcacheRelease(pPg); + } + pagerUnlockIfUnused(pPager); +} +SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){ + if( pPg ) sqlite3PagerUnrefNotNull(pPg); +} + +/* +** This function is called at the start of every write transaction. +** There must already be a RESERVED or EXCLUSIVE lock on the database +** file when this routine is called. +** +** Open the journal file for pager pPager and write a journal header +** to the start of it. If there are active savepoints, open the sub-journal +** as well. This function is only used when the journal file is being +** opened to write a rollback log for a transaction. It is not used +** when opening a hot journal file to roll it back. +** +** If the journal file is already open (as it may be in exclusive mode), +** then this function just writes a journal header to the start of the +** already open file. +** +** Whether or not the journal file is opened by this function, the +** Pager.pInJournal bitvec structure is allocated. +** +** Return SQLITE_OK if everything is successful. Otherwise, return +** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or +** an IO error code if opening or writing the journal file fails. +*/ +static int pager_open_journal(Pager *pPager){ + int rc = SQLITE_OK; /* Return code */ + sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ + + assert( pPager->eState==PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); + assert( pPager->pInJournal==0 ); + + /* If already in the error state, this function is a no-op. But on + ** the other hand, this routine is never called if we are already in + ** an error state. */ + if( NEVER(pPager->errCode) ) return pPager->errCode; + + if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ + pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); + if( pPager->pInJournal==0 ){ + return SQLITE_NOMEM_BKPT; + } + + /* Open the journal file if it is not already open. */ + if( !isOpen(pPager->jfd) ){ + if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ + sqlite3MemJournalOpen(pPager->jfd); + }else{ + int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + int nSpill; + + if( pPager->tempFile ){ + flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); + nSpill = sqlite3Config.nStmtSpill; + }else{ + flags |= SQLITE_OPEN_MAIN_JOURNAL; + nSpill = jrnlBufferSize(pPager); + } + + /* Verify that the database still has the same name as it did when + ** it was originally opened. */ + rc = databaseIsUnmoved(pPager); + if( rc==SQLITE_OK ){ + rc = sqlite3JournalOpen ( + pVfs, pPager->zJournal, pPager->jfd, flags, nSpill + ); + } + } + assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); + } + + + /* Write the first journal header to the journal file and open + ** the sub-journal if necessary. + */ + if( rc==SQLITE_OK ){ + /* TODO: Check if all of these are really required. */ + pPager->nRec = 0; + pPager->journalOff = 0; + pPager->setMaster = 0; + pPager->journalHdr = 0; + rc = writeJournalHdr(pPager); + } + } + + if( rc!=SQLITE_OK ){ + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; + }else{ + assert( pPager->eState==PAGER_WRITER_LOCKED ); + pPager->eState = PAGER_WRITER_CACHEMOD; + } + + return rc; +} + +/* +** Begin a write-transaction on the specified pager object. If a +** write-transaction has already been opened, this function is a no-op. +** +** If the exFlag argument is false, then acquire at least a RESERVED +** lock on the database file. If exFlag is true, then acquire at least +** an EXCLUSIVE lock. If such a lock is already held, no locking +** functions need be called. +** +** If the subjInMemory argument is non-zero, then any sub-journal opened +** within this transaction will be opened as an in-memory file. This +** has no effect if the sub-journal is already opened (as it may be when +** running in exclusive mode) or if the transaction does not require a +** sub-journal. If the subjInMemory argument is zero, then any required +** sub-journal is implemented in-memory if pPager is an in-memory database, +** or using a temporary file otherwise. +*/ +SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ + int rc = SQLITE_OK; + + if( pPager->errCode ) return pPager->errCode; + assert( pPager->eState>=PAGER_READER && pPager->eStatesubjInMemory = (u8)subjInMemory; + + if( ALWAYS(pPager->eState==PAGER_READER) ){ + assert( pPager->pInJournal==0 ); + + if( pagerUseWal(pPager) ){ + /* If the pager is configured to use locking_mode=exclusive, and an + ** exclusive lock on the database is not already held, obtain it now. + */ + if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){ + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); + if( rc!=SQLITE_OK ){ + return rc; + } + (void)sqlite3WalExclusiveMode(pPager->pWal, 1); + } + + /* Grab the write lock on the log file. If successful, upgrade to + ** PAGER_RESERVED state. Otherwise, return an error code to the caller. + ** The busy-handler is not invoked if another connection already + ** holds the write-lock. If possible, the upper layer will call it. + */ + rc = sqlite3WalBeginWriteTransaction(pPager->pWal); + }else{ + /* Obtain a RESERVED lock on the database file. If the exFlag parameter + ** is true, then immediately upgrade this to an EXCLUSIVE lock. The + ** busy-handler callback can be used when upgrading to the EXCLUSIVE + ** lock, but not when obtaining the RESERVED lock. + */ + rc = pagerLockDb(pPager, RESERVED_LOCK); + if( rc==SQLITE_OK && exFlag ){ + rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); + } + } + + if( rc==SQLITE_OK ){ + /* Change to WRITER_LOCKED state. + ** + ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD + ** when it has an open transaction, but never to DBMOD or FINISHED. + ** This is because in those states the code to roll back savepoint + ** transactions may copy data from the sub-journal into the database + ** file as well as into the page cache. Which would be incorrect in + ** WAL mode. + */ + pPager->eState = PAGER_WRITER_LOCKED; + pPager->dbHintSize = pPager->dbSize; + pPager->dbFileSize = pPager->dbSize; + pPager->dbOrigSize = pPager->dbSize; + pPager->journalOff = 0; + } + + assert( rc==SQLITE_OK || pPager->eState==PAGER_READER ); + assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); + } + + PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); + return rc; +} + +/* +** Write page pPg onto the end of the rollback journal. +*/ +static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + int rc; + u32 cksum; + char *pData2; + i64 iOff = pPager->journalOff; + + /* We should never write to the journal file the page that + ** contains the database locks. The following assert verifies + ** that we do not. */ + assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); + + assert( pPager->journalHdr<=pPager->journalOff ); + CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); + cksum = pager_cksum(pPager, (u8*)pData2); + + /* Even if an IO or diskfull error occurs while journalling the + ** page in the block above, set the need-sync flag for the page. + ** Otherwise, when the transaction is rolled back, the logic in + ** playback_one_page() will think that the page needs to be restored + ** in the database file. And if an IO error occurs while doing so, + ** then corruption may follow. + */ + pPg->flags |= PGHDR_NEED_SYNC; + + rc = write32bits(pPager->jfd, iOff, pPg->pgno); + if( rc!=SQLITE_OK ) return rc; + rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); + if( rc!=SQLITE_OK ) return rc; + rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); + if( rc!=SQLITE_OK ) return rc; + + IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, + pPager->journalOff, pPager->pageSize)); + PAGER_INCR(sqlite3_pager_writej_count); + PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", + PAGERID(pPager), pPg->pgno, + ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); + + pPager->journalOff += 8 + pPager->pageSize; + pPager->nRec++; + assert( pPager->pInJournal!=0 ); + rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); + testcase( rc==SQLITE_NOMEM ); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + rc |= addToSavepointBitvecs(pPager, pPg->pgno); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + return rc; +} + +/* +** Mark a single data page as writeable. The page is written into the +** main journal or sub-journal as required. If the page is written into +** one of the journals, the corresponding bit is set in the +** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs +** of any open savepoints as appropriate. +*/ +static int pager_write(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + int rc = SQLITE_OK; + + /* This routine is not called unless a write-transaction has already + ** been started. The journal file may or may not be open at this point. + ** It is never called in the ERROR state. + */ + assert( pPager->eState==PAGER_WRITER_LOCKED + || pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + ); + assert( assert_pager_state(pPager) ); + assert( pPager->errCode==0 ); + assert( pPager->readOnly==0 ); + CHECK_PAGE(pPg); + + /* The journal file needs to be opened. Higher level routines have already + ** obtained the necessary locks to begin the write-transaction, but the + ** rollback journal might not yet be open. Open it now if this is the case. + ** + ** This is done before calling sqlite3PcacheMakeDirty() on the page. + ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then + ** an error might occur and the pager would end up in WRITER_LOCKED state + ** with pages marked as dirty in the cache. + */ + if( pPager->eState==PAGER_WRITER_LOCKED ){ + rc = pager_open_journal(pPager); + if( rc!=SQLITE_OK ) return rc; + } + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); + assert( assert_pager_state(pPager) ); + + /* Mark the page that is about to be modified as dirty. */ + sqlite3PcacheMakeDirty(pPg); + + /* If a rollback journal is in use, them make sure the page that is about + ** to change is in the rollback journal, or if the page is a new page off + ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC. + */ + assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) ); + if( pPager->pInJournal!=0 + && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0 + ){ + assert( pagerUseWal(pPager)==0 ); + if( pPg->pgno<=pPager->dbOrigSize ){ + rc = pagerAddPageToRollbackJournal(pPg); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + if( pPager->eState!=PAGER_WRITER_DBMOD ){ + pPg->flags |= PGHDR_NEED_SYNC; + } + PAGERTRACE(("APPEND %d page %d needSync=%d\n", + PAGERID(pPager), pPg->pgno, + ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); + } + } + + /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list + ** and before writing the page into the rollback journal. Wait until now, + ** after the page has been successfully journalled, before setting the + ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified. + */ + pPg->flags |= PGHDR_WRITEABLE; + + /* If the statement journal is open and the page is not in it, + ** then write the page into the statement journal. + */ + if( pPager->nSavepoint>0 ){ + rc = subjournalPageIfRequired(pPg); + } + + /* Update the database size and return. */ + if( pPager->dbSizepgno ){ + pPager->dbSize = pPg->pgno; + } + return rc; +} + +/* +** This is a variant of sqlite3PagerWrite() that runs when the sector size +** is larger than the page size. SQLite makes the (reasonable) assumption that +** all bytes of a sector are written together by hardware. Hence, all bytes of +** a sector need to be journalled in case of a power loss in the middle of +** a write. +** +** Usually, the sector size is less than or equal to the page size, in which +** case pages can be individually written. This routine only runs in the +** exceptional case where the page size is smaller than the sector size. +*/ +static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ + int rc = SQLITE_OK; /* Return code */ + Pgno nPageCount; /* Total number of pages in database file */ + Pgno pg1; /* First page of the sector pPg is located on. */ + int nPage = 0; /* Number of pages starting at pg1 to journal */ + int ii; /* Loop counter */ + int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ + Pager *pPager = pPg->pPager; /* The pager that owns pPg */ + Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); + + /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow + ** a journal header to be written between the pages journaled by + ** this function. + */ + assert( !MEMDB ); + assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 ); + pPager->doNotSpill |= SPILLFLAG_NOSYNC; + + /* This trick assumes that both the page-size and sector-size are + ** an integer power of 2. It sets variable pg1 to the identifier + ** of the first page of the sector pPg is located on. + */ + pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; + + nPageCount = pPager->dbSize; + if( pPg->pgno>nPageCount ){ + nPage = (pPg->pgno - pg1)+1; + }else if( (pg1+nPagePerSector-1)>nPageCount ){ + nPage = nPageCount+1-pg1; + }else{ + nPage = nPagePerSector; + } + assert(nPage>0); + assert(pg1<=pPg->pgno); + assert((pg1+nPage)>pPg->pgno); + + for(ii=0; iipgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ + if( pg!=PAGER_MJ_PGNO(pPager) ){ + rc = sqlite3PagerGet(pPager, pg, &pPage, 0); + if( rc==SQLITE_OK ){ + rc = pager_write(pPage); + if( pPage->flags&PGHDR_NEED_SYNC ){ + needSync = 1; + } + sqlite3PagerUnrefNotNull(pPage); + } + } + }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){ + if( pPage->flags&PGHDR_NEED_SYNC ){ + needSync = 1; + } + sqlite3PagerUnrefNotNull(pPage); + } + } + + /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages + ** starting at pg1, then it needs to be set for all of them. Because + ** writing to any of these nPage pages may damage the others, the + ** journal file must contain sync()ed copies of all of them + ** before any of them can be written out to the database file. + */ + if( rc==SQLITE_OK && needSync ){ + assert( !MEMDB ); + for(ii=0; iiflags |= PGHDR_NEED_SYNC; + sqlite3PagerUnrefNotNull(pPage); + } + } + } + + assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 ); + pPager->doNotSpill &= ~SPILLFLAG_NOSYNC; + return rc; +} + +/* +** Mark a data page as writeable. This routine must be called before +** making changes to a page. The caller must check the return value +** of this function and be careful not to change any page data unless +** this routine returns SQLITE_OK. +** +** The difference between this function and pager_write() is that this +** function also deals with the special case where 2 or more pages +** fit on a single disk sector. In this case all co-resident pages +** must have been written to the journal file before returning. +** +** If an error occurs, SQLITE_NOMEM or an IO error code is returned +** as appropriate. Otherwise, SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + assert( (pPg->flags & PGHDR_MMAP)==0 ); + assert( pPager->eState>=PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); + if( pPager->errCode ){ + return pPager->errCode; + }else if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ + if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); + return SQLITE_OK; + }else if( pPager->sectorSize > (u32)pPager->pageSize ){ + assert( pPager->tempFile==0 ); + return pagerWriteLargeSector(pPg); + }else{ + return pager_write(pPg); + } +} + +/* +** Return TRUE if the page given in the argument was previously passed +** to sqlite3PagerWrite(). In other words, return TRUE if it is ok +** to change the content of the page. +*/ +#ifndef NDEBUG +SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){ + return pPg->flags & PGHDR_WRITEABLE; +} +#endif + +/* +** A call to this routine tells the pager that it is not necessary to +** write the information on page pPg back to the disk, even though +** that page might be marked as dirty. This happens, for example, when +** the page has been added as a leaf of the freelist and so its +** content no longer matters. +** +** The overlying software layer calls this routine when all of the data +** on the given page is unused. The pager marks the page as clean so +** that it does not get written to disk. +** +** Tests show that this optimization can quadruple the speed of large +** DELETE operations. +** +** This optimization cannot be used with a temp-file, as the page may +** have been dirty at the start of the transaction. In that case, if +** memory pressure forces page pPg out of the cache, the data does need +** to be written out to disk so that it may be read back in if the +** current transaction is rolled back. +*/ +SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ + PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); + IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) + pPg->flags |= PGHDR_DONT_WRITE; + pPg->flags &= ~PGHDR_WRITEABLE; + testcase( pPg->flags & PGHDR_NEED_SYNC ); + pager_set_pagehash(pPg); + } +} + +/* +** This routine is called to increment the value of the database file +** change-counter, stored as a 4-byte big-endian integer starting at +** byte offset 24 of the pager file. The secondary change counter at +** 92 is also updated, as is the SQLite version number at offset 96. +** +** But this only happens if the pPager->changeCountDone flag is false. +** To avoid excess churning of page 1, the update only happens once. +** See also the pager_write_changecounter() routine that does an +** unconditional update of the change counters. +** +** If the isDirectMode flag is zero, then this is done by calling +** sqlite3PagerWrite() on page 1, then modifying the contents of the +** page data. In this case the file will be updated when the current +** transaction is committed. +** +** The isDirectMode flag may only be non-zero if the library was compiled +** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case, +** if isDirect is non-zero, then the database file is updated directly +** by writing an updated version of page 1 using a call to the +** sqlite3OsWrite() function. +*/ +static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ + int rc = SQLITE_OK; + + assert( pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + ); + assert( assert_pager_state(pPager) ); + + /* Declare and initialize constant integer 'isDirect'. If the + ** atomic-write optimization is enabled in this build, then isDirect + ** is initialized to the value passed as the isDirectMode parameter + ** to this function. Otherwise, it is always set to zero. + ** + ** The idea is that if the atomic-write optimization is not + ** enabled at compile time, the compiler can omit the tests of + ** 'isDirect' below, as well as the block enclosed in the + ** "if( isDirect )" condition. + */ +#ifndef SQLITE_ENABLE_ATOMIC_WRITE +# define DIRECT_MODE 0 + assert( isDirectMode==0 ); + UNUSED_PARAMETER(isDirectMode); +#else +# define DIRECT_MODE isDirectMode +#endif + + if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){ + PgHdr *pPgHdr; /* Reference to page 1 */ + + assert( !pPager->tempFile && isOpen(pPager->fd) ); + + /* Open page 1 of the file for writing. */ + rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0); + assert( pPgHdr==0 || rc==SQLITE_OK ); + + /* If page one was fetched successfully, and this function is not + ** operating in direct-mode, make page 1 writable. When not in + ** direct mode, page 1 is always held in cache and hence the PagerGet() + ** above is always successful - hence the ALWAYS on rc==SQLITE_OK. + */ + if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){ + rc = sqlite3PagerWrite(pPgHdr); + } + + if( rc==SQLITE_OK ){ + /* Actually do the update of the change counter */ + pager_write_changecounter(pPgHdr); + + /* If running in direct mode, write the contents of page 1 to the file. */ + if( DIRECT_MODE ){ + const void *zBuf; + assert( pPager->dbFileSize>0 ); + CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); + pPager->aStat[PAGER_STAT_WRITE]++; + } + if( rc==SQLITE_OK ){ + /* Update the pager's copy of the change-counter. Otherwise, the + ** next time a read transaction is opened the cache will be + ** flushed (as the change-counter values will not match). */ + const void *pCopy = (const void *)&((const char *)zBuf)[24]; + memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers)); + pPager->changeCountDone = 1; + } + }else{ + pPager->changeCountDone = 1; + } + } + + /* Release the page reference. */ + sqlite3PagerUnref(pPgHdr); + } + return rc; +} + +/* +** Sync the database file to disk. This is a no-op for in-memory databases +** or pages with the Pager.noSync flag set. +** +** If successful, or if called on a pager for which it is a no-op, this +** function returns SQLITE_OK. Otherwise, an IO error code is returned. +*/ +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){ + int rc = SQLITE_OK; + + if( isOpen(pPager->fd) ){ + void *pArg = (void*)zMaster; + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg); + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + } + if( rc==SQLITE_OK && !pPager->noSync ){ + assert( !MEMDB ); + rc = sqlite3OsSync(pPager->fd, pPager->syncFlags); + } + return rc; +} + +/* +** This function may only be called while a write-transaction is active in +** rollback. If the connection is in WAL mode, this call is a no-op. +** Otherwise, if the connection does not already have an EXCLUSIVE lock on +** the database file, an attempt is made to obtain one. +** +** If the EXCLUSIVE lock is already held or the attempt to obtain it is +** successful, or the connection is in WAL mode, SQLITE_OK is returned. +** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is +** returned. +*/ +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){ + int rc = pPager->errCode; + assert( assert_pager_state(pPager) ); + if( rc==SQLITE_OK ){ + assert( pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + || pPager->eState==PAGER_WRITER_LOCKED + ); + assert( assert_pager_state(pPager) ); + if( 0==pagerUseWal(pPager) ){ + rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); + } + } + return rc; +} + +/* +** Sync the database file for the pager pPager. zMaster points to the name +** of a master journal file that should be written into the individual +** journal file. zMaster may be NULL, which is interpreted as no master +** journal (a single database transaction). +** +** This routine ensures that: +** +** * The database file change-counter is updated, +** * the journal is synced (unless the atomic-write optimization is used), +** * all dirty pages are written to the database file, +** * the database file is truncated (if required), and +** * the database file synced. +** +** The only thing that remains to commit the transaction is to finalize +** (delete, truncate or zero the first part of) the journal file (or +** delete the master journal file if specified). +** +** Note that if zMaster==NULL, this does not overwrite a previous value +** passed to an sqlite3PagerCommitPhaseOne() call. +** +** If the final parameter - noSync - is true, then the database file itself +** is not synced. The caller must call sqlite3PagerSync() directly to +** sync the database file before calling CommitPhaseTwo() to delete the +** journal file in this case. +*/ +SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne( + Pager *pPager, /* Pager object */ + const char *zMaster, /* If not NULL, the master journal name */ + int noSync /* True to omit the xSync on the db file */ +){ + int rc = SQLITE_OK; /* Return code */ + + assert( pPager->eState==PAGER_WRITER_LOCKED + || pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + || pPager->eState==PAGER_ERROR + ); + assert( assert_pager_state(pPager) ); + + /* If a prior error occurred, report that error again. */ + if( NEVER(pPager->errCode) ) return pPager->errCode; + + /* Provide the ability to easily simulate an I/O error during testing */ + if( sqlite3FaultSim(400) ) return SQLITE_IOERR; + + PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", + pPager->zFilename, zMaster, pPager->dbSize)); + + /* If no database changes have been made, return early. */ + if( pPager->eStatetempFile ); + assert( isOpen(pPager->fd) || pPager->tempFile ); + if( 0==pagerFlushOnCommit(pPager, 1) ){ + /* If this is an in-memory db, or no pages have been written to, or this + ** function has already been called, it is mostly a no-op. However, any + ** backup in progress needs to be restarted. */ + sqlite3BackupRestart(pPager->pBackup); + }else{ + if( pagerUseWal(pPager) ){ + PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); + PgHdr *pPageOne = 0; + if( pList==0 ){ + /* Must have at least one page for the WAL commit flag. + ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ + rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0); + pList = pPageOne; + pList->pDirty = 0; + } + assert( rc==SQLITE_OK ); + if( ALWAYS(pList) ){ + rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1); + } + sqlite3PagerUnref(pPageOne); + if( rc==SQLITE_OK ){ + sqlite3PcacheCleanAll(pPager->pPCache); + } + }else{ + /* The following block updates the change-counter. Exactly how it + ** does this depends on whether or not the atomic-update optimization + ** was enabled at compile time, and if this transaction meets the + ** runtime criteria to use the operation: + ** + ** * The file-system supports the atomic-write property for + ** blocks of size page-size, and + ** * This commit is not part of a multi-file transaction, and + ** * Exactly one page has been modified and store in the journal file. + ** + ** If the optimization was not enabled at compile time, then the + ** pager_incr_changecounter() function is called to update the change + ** counter in 'indirect-mode'. If the optimization is compiled in but + ** is not applicable to this transaction, call sqlite3JournalCreate() + ** to make sure the journal file has actually been created, then call + ** pager_incr_changecounter() to update the change-counter in indirect + ** mode. + ** + ** Otherwise, if the optimization is both enabled and applicable, + ** then call pager_incr_changecounter() to update the change-counter + ** in 'direct' mode. In this case the journal file will never be + ** created for this transaction. + */ + #ifdef SQLITE_ENABLE_ATOMIC_WRITE + PgHdr *pPg; + assert( isOpen(pPager->jfd) + || pPager->journalMode==PAGER_JOURNALMODE_OFF + || pPager->journalMode==PAGER_JOURNALMODE_WAL + ); + if( !zMaster && isOpen(pPager->jfd) + && pPager->journalOff==jrnlBufferSize(pPager) + && pPager->dbSize>=pPager->dbOrigSize + && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) + ){ + /* Update the db file change counter via the direct-write method. The + ** following call will modify the in-memory representation of page 1 + ** to include the updated change counter and then write page 1 + ** directly to the database file. Because of the atomic-write + ** property of the host file-system, this is safe. + */ + rc = pager_incr_changecounter(pPager, 1); + }else{ + rc = sqlite3JournalCreate(pPager->jfd); + if( rc==SQLITE_OK ){ + rc = pager_incr_changecounter(pPager, 0); + } + } + #else + rc = pager_incr_changecounter(pPager, 0); + #endif + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + + /* Write the master journal name into the journal file. If a master + ** journal file name has already been written to the journal file, + ** or if zMaster is NULL (no master journal), then this call is a no-op. + */ + rc = writeMasterJournal(pPager, zMaster); + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + + /* Sync the journal file and write all dirty pages to the database. + ** If the atomic-update optimization is being used, this sync will not + ** create the journal file or perform any real IO. + ** + ** Because the change-counter page was just modified, unless the + ** atomic-update optimization is used it is almost certain that the + ** journal requires a sync here. However, in locking_mode=exclusive + ** on a system under memory pressure it is just possible that this is + ** not the case. In this case it is likely enough that the redundant + ** xSync() call will be changed to a no-op by the OS anyhow. + */ + rc = syncJournal(pPager, 0); + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + + rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); + if( rc!=SQLITE_OK ){ + assert( rc!=SQLITE_IOERR_BLOCKED ); + goto commit_phase_one_exit; + } + sqlite3PcacheCleanAll(pPager->pPCache); + + /* If the file on disk is smaller than the database image, use + ** pager_truncate to grow the file here. This can happen if the database + ** image was extended as part of the current transaction and then the + ** last page in the db image moved to the free-list. In this case the + ** last page is never written out to disk, leaving the database file + ** undersized. Fix this now if it is the case. */ + if( pPager->dbSize>pPager->dbFileSize ){ + Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); + assert( pPager->eState==PAGER_WRITER_DBMOD ); + rc = pager_truncate(pPager, nNew); + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + } + + /* Finally, sync the database file. */ + if( !noSync ){ + rc = sqlite3PagerSync(pPager, zMaster); + } + IOTRACE(("DBSYNC %p\n", pPager)) + } + } + +commit_phase_one_exit: + if( rc==SQLITE_OK && !pagerUseWal(pPager) ){ + pPager->eState = PAGER_WRITER_FINISHED; + } + return rc; +} + + +/* +** When this function is called, the database file has been completely +** updated to reflect the changes made by the current transaction and +** synced to disk. The journal file still exists in the file-system +** though, and if a failure occurs at this point it will eventually +** be used as a hot-journal and the current transaction rolled back. +** +** This function finalizes the journal file, either by deleting, +** truncating or partially zeroing it, so that it cannot be used +** for hot-journal rollback. Once this is done the transaction is +** irrevocably committed. +** +** If an error occurs, an IO error code is returned and the pager +** moves into the error state. Otherwise, SQLITE_OK is returned. +*/ +SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){ + int rc = SQLITE_OK; /* Return code */ + + /* This routine should not be called if a prior error has occurred. + ** But if (due to a coding error elsewhere in the system) it does get + ** called, just return the same error code without doing anything. */ + if( NEVER(pPager->errCode) ) return pPager->errCode; + + assert( pPager->eState==PAGER_WRITER_LOCKED + || pPager->eState==PAGER_WRITER_FINISHED + || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD) + ); + assert( assert_pager_state(pPager) ); + + /* An optimization. If the database was not actually modified during + ** this transaction, the pager is running in exclusive-mode and is + ** using persistent journals, then this function is a no-op. + ** + ** The start of the journal file currently contains a single journal + ** header with the nRec field set to 0. If such a journal is used as + ** a hot-journal during hot-journal rollback, 0 changes will be made + ** to the database file. So there is no need to zero the journal + ** header. Since the pager is in exclusive mode, there is no need + ** to drop any locks either. + */ + if( pPager->eState==PAGER_WRITER_LOCKED + && pPager->exclusiveMode + && pPager->journalMode==PAGER_JOURNALMODE_PERSIST + ){ + assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff ); + pPager->eState = PAGER_READER; + return SQLITE_OK; + } + + PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); + pPager->iDataVersion++; + rc = pager_end_transaction(pPager, pPager->setMaster, 1); + return pager_error(pPager, rc); +} + +/* +** If a write transaction is open, then all changes made within the +** transaction are reverted and the current write-transaction is closed. +** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR +** state if an error occurs. +** +** If the pager is already in PAGER_ERROR state when this function is called, +** it returns Pager.errCode immediately. No work is performed in this case. +** +** Otherwise, in rollback mode, this function performs two functions: +** +** 1) It rolls back the journal file, restoring all database file and +** in-memory cache pages to the state they were in when the transaction +** was opened, and +** +** 2) It finalizes the journal file, so that it is not used for hot +** rollback at any point in the future. +** +** Finalization of the journal file (task 2) is only performed if the +** rollback is successful. +** +** In WAL mode, all cache-entries containing data modified within the +** current transaction are either expelled from the cache or reverted to +** their pre-transaction state by re-reading data from the database or +** WAL files. The WAL transaction is then closed. +*/ +SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){ + int rc = SQLITE_OK; /* Return code */ + PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager))); + + /* PagerRollback() is a no-op if called in READER or OPEN state. If + ** the pager is already in the ERROR state, the rollback is not + ** attempted here. Instead, the error code is returned to the caller. + */ + assert( assert_pager_state(pPager) ); + if( pPager->eState==PAGER_ERROR ) return pPager->errCode; + if( pPager->eState<=PAGER_READER ) return SQLITE_OK; + + if( pagerUseWal(pPager) ){ + int rc2; + rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); + rc2 = pager_end_transaction(pPager, pPager->setMaster, 0); + if( rc==SQLITE_OK ) rc = rc2; + }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ + int eState = pPager->eState; + rc = pager_end_transaction(pPager, 0, 0); + if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ + /* This can happen using journal_mode=off. Move the pager to the error + ** state to indicate that the contents of the cache may not be trusted. + ** Any active readers will get SQLITE_ABORT. + */ + pPager->errCode = SQLITE_ABORT; + pPager->eState = PAGER_ERROR; + return rc; + } + }else{ + rc = pager_playback(pPager, 0); + } + + assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); + assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT + || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR + || rc==SQLITE_CANTOPEN + ); + + /* If an error occurs during a ROLLBACK, we can no longer trust the pager + ** cache. So call pager_error() on the way out to make any error persistent. + */ + return pager_error(pPager, rc); +} + +/* +** Return TRUE if the database file is opened read-only. Return FALSE +** if the database is (in theory) writable. +*/ +SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){ + return pPager->readOnly; +} + +#ifdef SQLITE_DEBUG +/* +** Return the sum of the reference counts for all pages held by pPager. +*/ +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){ + return sqlite3PcacheRefCount(pPager->pPCache); +} +#endif + +/* +** Return the approximate number of bytes of memory currently +** used by the pager and its associated cache. +*/ +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){ + int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr) + + 5*sizeof(void*); + return perPageSize*sqlite3PcachePagecount(pPager->pPCache) + + sqlite3MallocSize(pPager) + + pPager->pageSize; +} + +/* +** Return the number of references to the specified page. +*/ +SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){ + return sqlite3PcachePageRefcount(pPage); +} + +#ifdef SQLITE_TEST +/* +** This routine is used for testing and analysis only. +*/ +SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){ + static int a[11]; + a[0] = sqlite3PcacheRefCount(pPager->pPCache); + a[1] = sqlite3PcachePagecount(pPager->pPCache); + a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); + a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize; + a[4] = pPager->eState; + a[5] = pPager->errCode; + a[6] = pPager->aStat[PAGER_STAT_HIT]; + a[7] = pPager->aStat[PAGER_STAT_MISS]; + a[8] = 0; /* Used to be pPager->nOvfl */ + a[9] = pPager->nRead; + a[10] = pPager->aStat[PAGER_STAT_WRITE]; + return a; +} +#endif + +/* +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the +** current cache hit or miss count, according to the value of eStat. If the +** reset parameter is non-zero, the cache hit or miss count is zeroed before +** returning. +*/ +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){ + + assert( eStat==SQLITE_DBSTATUS_CACHE_HIT + || eStat==SQLITE_DBSTATUS_CACHE_MISS + || eStat==SQLITE_DBSTATUS_CACHE_WRITE + ); + + assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS ); + assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE ); + assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 ); + + *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT]; + if( reset ){ + pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0; + } +} + +/* +** Return true if this is an in-memory or temp-file backed pager. +*/ +SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ + return pPager->tempFile; +} + +/* +** Check that there are at least nSavepoint savepoints open. If there are +** currently less than nSavepoints open, then open one or more savepoints +** to make up the difference. If the number of savepoints is already +** equal to nSavepoint, then this function is a no-op. +** +** If a memory allocation fails, SQLITE_NOMEM is returned. If an error +** occurs while opening the sub-journal file, then an IO error code is +** returned. Otherwise, SQLITE_OK. +*/ +static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){ + int rc = SQLITE_OK; /* Return code */ + int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ + int ii; /* Iterator variable */ + PagerSavepoint *aNew; /* New Pager.aSavepoint array */ + + assert( pPager->eState>=PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); + assert( nSavepoint>nCurrent && pPager->useJournal ); + + /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM + ** if the allocation fails. Otherwise, zero the new portion in case a + ** malloc failure occurs while populating it in the for(...) loop below. + */ + aNew = (PagerSavepoint *)sqlite3Realloc( + pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint + ); + if( !aNew ){ + return SQLITE_NOMEM_BKPT; + } + memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); + pPager->aSavepoint = aNew; + + /* Populate the PagerSavepoint structures just allocated. */ + for(ii=nCurrent; iidbSize; + if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ + aNew[ii].iOffset = pPager->journalOff; + }else{ + aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); + } + aNew[ii].iSubRec = pPager->nSubRec; + aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); + if( !aNew[ii].pInSavepoint ){ + return SQLITE_NOMEM_BKPT; + } + if( pagerUseWal(pPager) ){ + sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); + } + pPager->nSavepoint = ii+1; + } + assert( pPager->nSavepoint==nSavepoint ); + assertTruncateConstraint(pPager); + return rc; +} +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ + assert( pPager->eState>=PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); + + if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){ + return pagerOpenSavepoint(pPager, nSavepoint); + }else{ + return SQLITE_OK; + } +} + + +/* +** This function is called to rollback or release (commit) a savepoint. +** The savepoint to release or rollback need not be the most recently +** created savepoint. +** +** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE. +** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with +** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes +** that have occurred since the specified savepoint was created. +** +** The savepoint to rollback or release is identified by parameter +** iSavepoint. A value of 0 means to operate on the outermost savepoint +** (the first created). A value of (Pager.nSavepoint-1) means operate +** on the most recently created savepoint. If iSavepoint is greater than +** (Pager.nSavepoint-1), then this function is a no-op. +** +** If a negative value is passed to this function, then the current +** transaction is rolled back. This is different to calling +** sqlite3PagerRollback() because this function does not terminate +** the transaction or unlock the database, it just restores the +** contents of the database to its original state. +** +** In any case, all savepoints with an index greater than iSavepoint +** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), +** then savepoint iSavepoint is also destroyed. +** +** This function may return SQLITE_NOMEM if a memory allocation fails, +** or an IO error code if an IO error occurs while rolling back a +** savepoint. If no errors occur, SQLITE_OK is returned. +*/ +SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ + int rc = pPager->errCode; /* Return code */ + + assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); + assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); + + if( rc==SQLITE_OK && iSavepointnSavepoint ){ + int ii; /* Iterator variable */ + int nNew; /* Number of remaining savepoints after this op. */ + + /* Figure out how many savepoints will still be active after this + ** operation. Store this value in nNew. Then free resources associated + ** with any savepoints that are destroyed by this operation. + */ + nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1); + for(ii=nNew; iinSavepoint; ii++){ + sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); + } + pPager->nSavepoint = nNew; + + /* If this is a release of the outermost savepoint, truncate + ** the sub-journal to zero bytes in size. */ + if( op==SAVEPOINT_RELEASE ){ + if( nNew==0 && isOpen(pPager->sjfd) ){ + /* Only truncate if it is an in-memory sub-journal. */ + if( sqlite3JournalIsInMemory(pPager->sjfd) ){ + rc = sqlite3OsTruncate(pPager->sjfd, 0); + assert( rc==SQLITE_OK ); + } + pPager->nSubRec = 0; + } + } + /* Else this is a rollback operation, playback the specified savepoint. + ** If this is a temp-file, it is possible that the journal file has + ** not yet been opened. In this case there have been no changes to + ** the database file, so the playback operation can be skipped. + */ + else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){ + PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; + rc = pagerPlaybackSavepoint(pPager, pSavepoint); + assert(rc!=SQLITE_DONE); + } + } + + return rc; +} + +/* +** Return the full pathname of the database file. +** +** Except, if the pager is in-memory only, then return an empty string if +** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when +** used to report the filename to the user, for compatibility with legacy +** behavior. But when the Btree needs to know the filename for matching to +** shared cache, it uses nullIfMemDb==0 so that in-memory databases can +** participate in shared-cache. +*/ +SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){ + return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename; +} + +/* +** Return the VFS structure for the pager. +*/ +SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ + return pPager->pVfs; +} + +/* +** Return the file handle for the database file associated +** with the pager. This might return NULL if the file has +** not yet been opened. +*/ +SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){ + return pPager->fd; +} + +/* +** Return the file handle for the journal file (if it exists). +** This will be either the rollback journal or the WAL file. +*/ +SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ +#if SQLITE_OMIT_WAL + return pPager->jfd; +#else + return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd; +#endif +} + +/* +** Return the full pathname of the journal file. +*/ +SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){ + return pPager->zJournal; +} + +#ifdef SQLITE_HAS_CODEC +/* +** Set or retrieve the codec for this pager +*/ +SQLITE_PRIVATE void sqlite3PagerSetCodec( + Pager *pPager, + void *(*xCodec)(void*,void*,Pgno,int), + void (*xCodecSizeChng)(void*,int,int), + void (*xCodecFree)(void*), + void *pCodec +){ + if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); + pPager->xCodec = pPager->memDb ? 0 : xCodec; + pPager->xCodecSizeChng = xCodecSizeChng; + pPager->xCodecFree = xCodecFree; + pPager->pCodec = pCodec; + pagerReportSize(pPager); +} +SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){ + return pPager->pCodec; +} + +/* +** This function is called by the wal module when writing page content +** into the log file. +** +** This function returns a pointer to a buffer containing the encrypted +** page content. If a malloc fails, this function may return NULL. +*/ +SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){ + void *aData = 0; + CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData); + return aData; +} + +/* +** Return the current pager state +*/ +SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){ + return pPager->eState; +} +#endif /* SQLITE_HAS_CODEC */ + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Move the page pPg to location pgno in the file. +** +** There must be no references to the page previously located at +** pgno (which we call pPgOld) though that page is allowed to be +** in cache. If the page previously located at pgno is not already +** in the rollback journal, it is not put there by by this routine. +** +** References to the page pPg remain valid. Updating any +** meta-data associated with pPg (i.e. data stored in the nExtra bytes +** allocated along with the page) is the responsibility of the caller. +** +** A transaction must be active when this routine is called. It used to be +** required that a statement transaction was not active, but this restriction +** has been removed (CREATE INDEX needs to move a page when a statement +** transaction is active). +** +** If the fourth argument, isCommit, is non-zero, then this page is being +** moved as part of a database reorganization just before the transaction +** is being committed. In this case, it is guaranteed that the database page +** pPg refers to will not be written to again within this transaction. +** +** This function may return SQLITE_NOMEM or an IO error code if an error +** occurs. Otherwise, it returns SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ + PgHdr *pPgOld; /* The page being overwritten. */ + Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */ + int rc; /* Return code */ + Pgno origPgno; /* The original page number */ + + assert( pPg->nRef>0 ); + assert( pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + ); + assert( assert_pager_state(pPager) ); + + /* In order to be able to rollback, an in-memory database must journal + ** the page we are moving from. + */ + assert( pPager->tempFile || !MEMDB ); + if( pPager->tempFile ){ + rc = sqlite3PagerWrite(pPg); + if( rc ) return rc; + } + + /* If the page being moved is dirty and has not been saved by the latest + ** savepoint, then save the current contents of the page into the + ** sub-journal now. This is required to handle the following scenario: + ** + ** BEGIN; + ** + ** SAVEPOINT one; + ** + ** ROLLBACK TO one; + ** + ** If page X were not written to the sub-journal here, it would not + ** be possible to restore its contents when the "ROLLBACK TO one" + ** statement were is processed. + ** + ** subjournalPage() may need to allocate space to store pPg->pgno into + ** one or more savepoint bitvecs. This is the reason this function + ** may return SQLITE_NOMEM. + */ + if( (pPg->flags & PGHDR_DIRTY)!=0 + && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg)) + ){ + return rc; + } + + PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", + PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno)); + IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) + + /* If the journal needs to be sync()ed before page pPg->pgno can + ** be written to, store pPg->pgno in local variable needSyncPgno. + ** + ** If the isCommit flag is set, there is no need to remember that + ** the journal needs to be sync()ed before database page pPg->pgno + ** can be written to. The caller has already promised not to write to it. + */ + if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ + needSyncPgno = pPg->pgno; + assert( pPager->journalMode==PAGER_JOURNALMODE_OFF || + pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize ); + assert( pPg->flags&PGHDR_DIRTY ); + } + + /* If the cache contains a page with page-number pgno, remove it + ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for + ** page pgno before the 'move' operation, it needs to be retained + ** for the page moved there. + */ + pPg->flags &= ~PGHDR_NEED_SYNC; + pPgOld = sqlite3PagerLookup(pPager, pgno); + assert( !pPgOld || pPgOld->nRef==1 ); + if( pPgOld ){ + pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); + if( pPager->tempFile ){ + /* Do not discard pages from an in-memory database since we might + ** need to rollback later. Just move the page out of the way. */ + sqlite3PcacheMove(pPgOld, pPager->dbSize+1); + }else{ + sqlite3PcacheDrop(pPgOld); + } + } + + origPgno = pPg->pgno; + sqlite3PcacheMove(pPg, pgno); + sqlite3PcacheMakeDirty(pPg); + + /* For an in-memory database, make sure the original page continues + ** to exist, in case the transaction needs to roll back. Use pPgOld + ** as the original page since it has already been allocated. + */ + if( pPager->tempFile && pPgOld ){ + sqlite3PcacheMove(pPgOld, origPgno); + sqlite3PagerUnrefNotNull(pPgOld); + } + + if( needSyncPgno ){ + /* If needSyncPgno is non-zero, then the journal file needs to be + ** sync()ed before any data is written to database file page needSyncPgno. + ** Currently, no such page exists in the page-cache and the + ** "is journaled" bitvec flag has been set. This needs to be remedied by + ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC + ** flag. + ** + ** If the attempt to load the page into the page-cache fails, (due + ** to a malloc() or IO failure), clear the bit in the pInJournal[] + ** array. Otherwise, if the page is loaded and written again in + ** this transaction, it may be written to the database file before + ** it is synced into the journal file. This way, it may end up in + ** the journal file twice, but that is not a problem. + */ + PgHdr *pPgHdr; + rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0); + if( rc!=SQLITE_OK ){ + if( needSyncPgno<=pPager->dbOrigSize ){ + assert( pPager->pTmpSpace!=0 ); + sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace); + } + return rc; + } + pPgHdr->flags |= PGHDR_NEED_SYNC; + sqlite3PcacheMakeDirty(pPgHdr); + sqlite3PagerUnrefNotNull(pPgHdr); + } + + return SQLITE_OK; +} +#endif + +/* +** The page handle passed as the first argument refers to a dirty page +** with a page number other than iNew. This function changes the page's +** page number to iNew and sets the value of the PgHdr.flags field to +** the value passed as the third parameter. +*/ +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ + assert( pPg->pgno!=iNew ); + pPg->flags = flags; + sqlite3PcacheMove(pPg, iNew); +} + +/* +** Return a pointer to the data for the specified page. +*/ +SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ + assert( pPg->nRef>0 || pPg->pPager->memDb ); + return pPg->pData; +} + +/* +** Return a pointer to the Pager.nExtra bytes of "extra" space +** allocated along with the specified page. +*/ +SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){ + return pPg->pExtra; +} + +/* +** Get/set the locking-mode for this pager. Parameter eMode must be one +** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or +** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then +** the locking-mode is set to the value specified. +** +** The returned value is either PAGER_LOCKINGMODE_NORMAL or +** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) +** locking-mode. +*/ +SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){ + assert( eMode==PAGER_LOCKINGMODE_QUERY + || eMode==PAGER_LOCKINGMODE_NORMAL + || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); + assert( PAGER_LOCKINGMODE_QUERY<0 ); + assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 ); + assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) ); + if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){ + pPager->exclusiveMode = (u8)eMode; + } + return (int)pPager->exclusiveMode; +} + +/* +** Set the journal-mode for this pager. Parameter eMode must be one of: +** +** PAGER_JOURNALMODE_DELETE +** PAGER_JOURNALMODE_TRUNCATE +** PAGER_JOURNALMODE_PERSIST +** PAGER_JOURNALMODE_OFF +** PAGER_JOURNALMODE_MEMORY +** PAGER_JOURNALMODE_WAL +** +** The journalmode is set to the value specified if the change is allowed. +** The change may be disallowed for the following reasons: +** +** * An in-memory database can only have its journal_mode set to _OFF +** or _MEMORY. +** +** * Temporary databases cannot have _WAL journalmode. +** +** The returned indicate the current (possibly updated) journal-mode. +*/ +SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ + u8 eOld = pPager->journalMode; /* Prior journalmode */ + +#ifdef SQLITE_DEBUG + /* The print_pager_state() routine is intended to be used by the debugger + ** only. We invoke it once here to suppress a compiler warning. */ + print_pager_state(pPager); +#endif + + + /* The eMode parameter is always valid */ + assert( eMode==PAGER_JOURNALMODE_DELETE + || eMode==PAGER_JOURNALMODE_TRUNCATE + || eMode==PAGER_JOURNALMODE_PERSIST + || eMode==PAGER_JOURNALMODE_OFF + || eMode==PAGER_JOURNALMODE_WAL + || eMode==PAGER_JOURNALMODE_MEMORY ); + + /* This routine is only called from the OP_JournalMode opcode, and + ** the logic there will never allow a temporary file to be changed + ** to WAL mode. + */ + assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL ); + + /* Do allow the journalmode of an in-memory database to be set to + ** anything other than MEMORY or OFF + */ + if( MEMDB ){ + assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF ); + if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){ + eMode = eOld; + } + } + + if( eMode!=eOld ){ + + /* Change the journal mode. */ + assert( pPager->eState!=PAGER_ERROR ); + pPager->journalMode = (u8)eMode; + + /* When transistioning from TRUNCATE or PERSIST to any other journal + ** mode except WAL, unless the pager is in locking_mode=exclusive mode, + ** delete the journal file. + */ + assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); + assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); + assert( (PAGER_JOURNALMODE_DELETE & 5)==0 ); + assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 ); + assert( (PAGER_JOURNALMODE_OFF & 5)==0 ); + assert( (PAGER_JOURNALMODE_WAL & 5)==5 ); + + assert( isOpen(pPager->fd) || pPager->exclusiveMode ); + if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){ + + /* In this case we would like to delete the journal file. If it is + ** not possible, then that is not a problem. Deleting the journal file + ** here is an optimization only. + ** + ** Before deleting the journal file, obtain a RESERVED lock on the + ** database file. This ensures that the journal file is not deleted + ** while it is in use by some other client. + */ + sqlite3OsClose(pPager->jfd); + if( pPager->eLock>=RESERVED_LOCK ){ + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); + }else{ + int rc = SQLITE_OK; + int state = pPager->eState; + assert( state==PAGER_OPEN || state==PAGER_READER ); + if( state==PAGER_OPEN ){ + rc = sqlite3PagerSharedLock(pPager); + } + if( pPager->eState==PAGER_READER ){ + assert( rc==SQLITE_OK ); + rc = pagerLockDb(pPager, RESERVED_LOCK); + } + if( rc==SQLITE_OK ){ + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); + } + if( rc==SQLITE_OK && state==PAGER_READER ){ + pagerUnlockDb(pPager, SHARED_LOCK); + }else if( state==PAGER_OPEN ){ + pager_unlock(pPager); + } + assert( state==pPager->eState ); + } + }else if( eMode==PAGER_JOURNALMODE_OFF ){ + sqlite3OsClose(pPager->jfd); + } + } + + /* Return the new journal mode */ + return (int)pPager->journalMode; +} + +/* +** Return the current journal mode. +*/ +SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){ + return (int)pPager->journalMode; +} + +/* +** Return TRUE if the pager is in a state where it is OK to change the +** journalmode. Journalmode changes can only happen when the database +** is unmodified. +*/ +SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){ + assert( assert_pager_state(pPager) ); + if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0; + if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0; + return 1; +} + +/* +** Get/set the size-limit used for persistent journal files. +** +** Setting the size limit to -1 means no limit is enforced. +** An attempt to set a limit smaller than -1 is a no-op. +*/ +SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ + if( iLimit>=-1 ){ + pPager->journalSizeLimit = iLimit; + sqlite3WalLimit(pPager->pWal, iLimit); + } + return pPager->journalSizeLimit; +} + +/* +** Return a pointer to the pPager->pBackup variable. The backup module +** in backup.c maintains the content of this variable. This module +** uses it opaquely as an argument to sqlite3BackupRestart() and +** sqlite3BackupUpdate() only. +*/ +SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ + return &pPager->pBackup; +} + +#ifndef SQLITE_OMIT_VACUUM +/* +** Unless this is an in-memory or temporary database, clear the pager cache. +*/ +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){ + assert( MEMDB==0 || pPager->tempFile ); + if( pPager->tempFile==0 ) pager_reset(pPager); +} +#endif + +#ifndef SQLITE_OMIT_WAL +/* +** This function is called when the user invokes "PRAGMA wal_checkpoint", +** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint() +** or wal_blocking_checkpoint() API functions. +** +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +*/ +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ + int rc = SQLITE_OK; + if( pPager->pWal ){ + rc = sqlite3WalCheckpoint(pPager->pWal, eMode, + (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), + pPager->pBusyHandlerArg, + pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, + pnLog, pnCkpt + ); + } + return rc; +} + +SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){ + return sqlite3WalCallback(pPager->pWal); +} + +/* +** Return true if the underlying VFS for the given pager supports the +** primitives necessary for write-ahead logging. +*/ +SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){ + const sqlite3_io_methods *pMethods = pPager->fd->pMethods; + if( pPager->noLock ) return 0; + return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap); +} + +/* +** Attempt to take an exclusive lock on the database file. If a PENDING lock +** is obtained instead, immediately release it. +*/ +static int pagerExclusiveLock(Pager *pPager){ + int rc; /* Return code */ + + assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK ); + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); + if( rc!=SQLITE_OK ){ + /* If the attempt to grab the exclusive lock failed, release the + ** pending lock that may have been obtained instead. */ + pagerUnlockDb(pPager, SHARED_LOCK); + } + + return rc; +} + +/* +** Call sqlite3WalOpen() to open the WAL handle. If the pager is in +** exclusive-locking mode when this function is called, take an EXCLUSIVE +** lock on the database file and use heap-memory to store the wal-index +** in. Otherwise, use the normal shared-memory. +*/ +static int pagerOpenWal(Pager *pPager){ + int rc = SQLITE_OK; + + assert( pPager->pWal==0 && pPager->tempFile==0 ); + assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK ); + + /* If the pager is already in exclusive-mode, the WAL module will use + ** heap-memory for the wal-index instead of the VFS shared-memory + ** implementation. Take the exclusive lock now, before opening the WAL + ** file, to make sure this is safe. + */ + if( pPager->exclusiveMode ){ + rc = pagerExclusiveLock(pPager); + } + + /* Open the connection to the log file. If this operation fails, + ** (e.g. due to malloc() failure), return an error code. + */ + if( rc==SQLITE_OK ){ + rc = sqlite3WalOpen(pPager->pVfs, + pPager->fd, pPager->zWal, pPager->exclusiveMode, + pPager->journalSizeLimit, &pPager->pWal + ); + } + pagerFixMaplimit(pPager); + + return rc; +} + + +/* +** The caller must be holding a SHARED lock on the database file to call +** this function. +** +** If the pager passed as the first argument is open on a real database +** file (not a temp file or an in-memory database), and the WAL file +** is not already open, make an attempt to open it now. If successful, +** return SQLITE_OK. If an error occurs or the VFS used by the pager does +** not support the xShmXXX() methods, return an error code. *pbOpen is +** not modified in either case. +** +** If the pager is open on a temp-file (or in-memory database), or if +** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK +** without doing anything. +*/ +SQLITE_PRIVATE int sqlite3PagerOpenWal( + Pager *pPager, /* Pager object */ + int *pbOpen /* OUT: Set to true if call is a no-op */ +){ + int rc = SQLITE_OK; /* Return code */ + + assert( assert_pager_state(pPager) ); + assert( pPager->eState==PAGER_OPEN || pbOpen ); + assert( pPager->eState==PAGER_READER || !pbOpen ); + assert( pbOpen==0 || *pbOpen==0 ); + assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) ); + + if( !pPager->tempFile && !pPager->pWal ){ + if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN; + + /* Close any rollback journal previously open */ + sqlite3OsClose(pPager->jfd); + + rc = pagerOpenWal(pPager); + if( rc==SQLITE_OK ){ + pPager->journalMode = PAGER_JOURNALMODE_WAL; + pPager->eState = PAGER_OPEN; + } + }else{ + *pbOpen = 1; + } + + return rc; +} + +/* +** This function is called to close the connection to the log file prior +** to switching from WAL to rollback mode. +** +** Before closing the log file, this function attempts to take an +** EXCLUSIVE lock on the database file. If this cannot be obtained, an +** error (SQLITE_BUSY) is returned and the log connection is not closed. +** If successful, the EXCLUSIVE lock is not released before returning. +*/ +SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){ + int rc = SQLITE_OK; + + assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); + + /* If the log file is not already open, but does exist in the file-system, + ** it may need to be checkpointed before the connection can switch to + ** rollback mode. Open it now so this can happen. + */ + if( !pPager->pWal ){ + int logexists = 0; + rc = pagerLockDb(pPager, SHARED_LOCK); + if( rc==SQLITE_OK ){ + rc = sqlite3OsAccess( + pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists + ); + } + if( rc==SQLITE_OK && logexists ){ + rc = pagerOpenWal(pPager); + } + } + + /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on + ** the database file, the log and log-summary files will be deleted. + */ + if( rc==SQLITE_OK && pPager->pWal ){ + rc = pagerExclusiveLock(pPager); + if( rc==SQLITE_OK ){ + rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, + pPager->pageSize, (u8*)pPager->pTmpSpace); + pPager->pWal = 0; + pagerFixMaplimit(pPager); + if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); + } + } + return rc; +} + +#ifdef SQLITE_ENABLE_SNAPSHOT +/* +** If this is a WAL database, obtain a snapshot handle for the snapshot +** currently open. Otherwise, return an error. +*/ +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ + int rc = SQLITE_ERROR; + if( pPager->pWal ){ + rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot); + } + return rc; +} + +/* +** If this is a WAL database, store a pointer to pSnapshot. Next time a +** read transaction is opened, attempt to read from the snapshot it +** identifies. If this is not a WAL database, return an error. +*/ +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ + int rc = SQLITE_OK; + if( pPager->pWal ){ + sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); + }else{ + rc = SQLITE_ERROR; + } + return rc; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ +#endif /* !SQLITE_OMIT_WAL */ + +#ifdef SQLITE_ENABLE_ZIPVFS +/* +** A read-lock must be held on the pager when this function is called. If +** the pager is in WAL mode and the WAL file currently contains one or more +** frames, return the size in bytes of the page images stored within the +** WAL frames. Otherwise, if this is not a WAL database or the WAL file +** is empty, return 0. +*/ +SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ + assert( pPager->eState>=PAGER_READER ); + return sqlite3WalFramesize(pPager->pWal); +} +#endif + + +#endif /* SQLITE_OMIT_DISKIO */ + +/************** End of pager.c ***********************************************/ +/************** Begin file wal.c *********************************************/ +/* +** 2010 February 1 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the implementation of a write-ahead log (WAL) used in +** "journal_mode=WAL" mode. +** +** WRITE-AHEAD LOG (WAL) FILE FORMAT +** +** A WAL file consists of a header followed by zero or more "frames". +** Each frame records the revised content of a single page from the +** database file. All changes to the database are recorded by writing +** frames into the WAL. Transactions commit when a frame is written that +** contains a commit marker. A single WAL can and usually does record +** multiple transactions. Periodically, the content of the WAL is +** transferred back into the database file in an operation called a +** "checkpoint". +** +** A single WAL file can be used multiple times. In other words, the +** WAL can fill up with frames and then be checkpointed and then new +** frames can overwrite the old ones. A WAL always grows from beginning +** toward the end. Checksums and counters attached to each frame are +** used to determine which frames within the WAL are valid and which +** are leftovers from prior checkpoints. +** +** The WAL header is 32 bytes in size and consists of the following eight +** big-endian 32-bit unsigned integer values: +** +** 0: Magic number. 0x377f0682 or 0x377f0683 +** 4: File format version. Currently 3007000 +** 8: Database page size. Example: 1024 +** 12: Checkpoint sequence number +** 16: Salt-1, random integer incremented with each checkpoint +** 20: Salt-2, a different random integer changing with each ckpt +** 24: Checksum-1 (first part of checksum for first 24 bytes of header). +** 28: Checksum-2 (second part of checksum for first 24 bytes of header). +** +** Immediately following the wal-header are zero or more frames. Each +** frame consists of a 24-byte frame-header followed by a bytes +** of page data. The frame-header is six big-endian 32-bit unsigned +** integer values, as follows: +** +** 0: Page number. +** 4: For commit records, the size of the database image in pages +** after the commit. For all other records, zero. +** 8: Salt-1 (copied from the header) +** 12: Salt-2 (copied from the header) +** 16: Checksum-1. +** 20: Checksum-2. +** +** A frame is considered valid if and only if the following conditions are +** true: +** +** (1) The salt-1 and salt-2 values in the frame-header match +** salt values in the wal-header +** +** (2) The checksum values in the final 8 bytes of the frame-header +** exactly match the checksum computed consecutively on the +** WAL header and the first 8 bytes and the content of all frames +** up to and including the current frame. +** +** The checksum is computed using 32-bit big-endian integers if the +** magic number in the first 4 bytes of the WAL is 0x377f0683 and it +** is computed using little-endian if the magic number is 0x377f0682. +** The checksum values are always stored in the frame header in a +** big-endian format regardless of which byte order is used to compute +** the checksum. The checksum is computed by interpreting the input as +** an even number of unsigned 32-bit integers: x[0] through x[N]. The +** algorithm used for the checksum is as follows: +** +** for i from 0 to n-1 step 2: +** s0 += x[i] + s1; +** s1 += x[i+1] + s0; +** endfor +** +** Note that s0 and s1 are both weighted checksums using fibonacci weights +** in reverse order (the largest fibonacci weight occurs on the first element +** of the sequence being summed.) The s1 value spans all 32-bit +** terms of the sequence whereas s0 omits the final term. +** +** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the +** WAL is transferred into the database, then the database is VFS.xSync-ed. +** The VFS.xSync operations serve as write barriers - all writes launched +** before the xSync must complete before any write that launches after the +** xSync begins. +** +** After each checkpoint, the salt-1 value is incremented and the salt-2 +** value is randomized. This prevents old and new frames in the WAL from +** being considered valid at the same time and being checkpointing together +** following a crash. +** +** READER ALGORITHM +** +** To read a page from the database (call it page number P), a reader +** first checks the WAL to see if it contains page P. If so, then the +** last valid instance of page P that is a followed by a commit frame +** or is a commit frame itself becomes the value read. If the WAL +** contains no copies of page P that are valid and which are a commit +** frame or are followed by a commit frame, then page P is read from +** the database file. +** +** To start a read transaction, the reader records the index of the last +** valid frame in the WAL. The reader uses this recorded "mxFrame" value +** for all subsequent read operations. New transactions can be appended +** to the WAL, but as long as the reader uses its original mxFrame value +** and ignores the newly appended content, it will see a consistent snapshot +** of the database from a single point in time. This technique allows +** multiple concurrent readers to view different versions of the database +** content simultaneously. +** +** The reader algorithm in the previous paragraphs works correctly, but +** because frames for page P can appear anywhere within the WAL, the +** reader has to scan the entire WAL looking for page P frames. If the +** WAL is large (multiple megabytes is typical) that scan can be slow, +** and read performance suffers. To overcome this problem, a separate +** data structure called the wal-index is maintained to expedite the +** search for frames of a particular page. +** +** WAL-INDEX FORMAT +** +** Conceptually, the wal-index is shared memory, though VFS implementations +** might choose to implement the wal-index using a mmapped file. Because +** the wal-index is shared memory, SQLite does not support journal_mode=WAL +** on a network filesystem. All users of the database must be able to +** share memory. +** +** The wal-index is transient. After a crash, the wal-index can (and should +** be) reconstructed from the original WAL file. In fact, the VFS is required +** to either truncate or zero the header of the wal-index when the last +** connection to it closes. Because the wal-index is transient, it can +** use an architecture-specific format; it does not have to be cross-platform. +** Hence, unlike the database and WAL file formats which store all values +** as big endian, the wal-index can store multi-byte values in the native +** byte order of the host computer. +** +** The purpose of the wal-index is to answer this question quickly: Given +** a page number P and a maximum frame index M, return the index of the +** last frame in the wal before frame M for page P in the WAL, or return +** NULL if there are no frames for page P in the WAL prior to M. +** +** The wal-index consists of a header region, followed by an one or +** more index blocks. +** +** The wal-index header contains the total number of frames within the WAL +** in the mxFrame field. +** +** Each index block except for the first contains information on +** HASHTABLE_NPAGE frames. The first index block contains information on +** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and +** HASHTABLE_NPAGE are selected so that together the wal-index header and +** first index block are the same size as all other index blocks in the +** wal-index. +** +** Each index block contains two sections, a page-mapping that contains the +** database page number associated with each wal frame, and a hash-table +** that allows readers to query an index block for a specific page number. +** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE +** for the first index block) 32-bit page numbers. The first entry in the +** first index-block contains the database page number corresponding to the +** first frame in the WAL file. The first entry in the second index block +** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in +** the log, and so on. +** +** The last index block in a wal-index usually contains less than the full +** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers, +** depending on the contents of the WAL file. This does not change the +** allocated size of the page-mapping array - the page-mapping array merely +** contains unused entries. +** +** Even without using the hash table, the last frame for page P +** can be found by scanning the page-mapping sections of each index block +** starting with the last index block and moving toward the first, and +** within each index block, starting at the end and moving toward the +** beginning. The first entry that equals P corresponds to the frame +** holding the content for that page. +** +** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers. +** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the +** hash table for each page number in the mapping section, so the hash +** table is never more than half full. The expected number of collisions +** prior to finding a match is 1. Each entry of the hash table is an +** 1-based index of an entry in the mapping section of the same +** index block. Let K be the 1-based index of the largest entry in +** the mapping section. (For index blocks other than the last, K will +** always be exactly HASHTABLE_NPAGE (4096) and for the last index block +** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table +** contain a value of 0. +** +** To look for page P in the hash table, first compute a hash iKey on +** P as follows: +** +** iKey = (P * 383) % HASHTABLE_NSLOT +** +** Then start scanning entries of the hash table, starting with iKey +** (wrapping around to the beginning when the end of the hash table is +** reached) until an unused hash slot is found. Let the first unused slot +** be at index iUnused. (iUnused might be less than iKey if there was +** wrap-around.) Because the hash table is never more than half full, +** the search is guaranteed to eventually hit an unused entry. Let +** iMax be the value between iKey and iUnused, closest to iUnused, +** where aHash[iMax]==P. If there is no iMax entry (if there exists +** no hash slot such that aHash[i]==p) then page P is not in the +** current index block. Otherwise the iMax-th mapping entry of the +** current index block corresponds to the last entry that references +** page P. +** +** A hash search begins with the last index block and moves toward the +** first index block, looking for entries corresponding to page P. On +** average, only two or three slots in each index block need to be +** examined in order to either find the last entry for page P, or to +** establish that no such entry exists in the block. Each index block +** holds over 4000 entries. So two or three index blocks are sufficient +** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10 +** comparisons (on average) suffice to either locate a frame in the +** WAL or to establish that the frame does not exist in the WAL. This +** is much faster than scanning the entire 10MB WAL. +** +** Note that entries are added in order of increasing K. Hence, one +** reader might be using some value K0 and a second reader that started +** at a later time (after additional transactions were added to the WAL +** and to the wal-index) might be using a different value K1, where K1>K0. +** Both readers can use the same hash table and mapping section to get +** the correct result. There may be entries in the hash table with +** K>K0 but to the first reader, those entries will appear to be unused +** slots in the hash table and so the first reader will get an answer as +** if no values greater than K0 had ever been inserted into the hash table +** in the first place - which is what reader one wants. Meanwhile, the +** second reader using K1 will see additional values that were inserted +** later, which is exactly what reader two wants. +** +** When a rollback occurs, the value of K is decreased. Hash table entries +** that correspond to frames greater than the new K value are removed +** from the hash table at this point. +*/ +#ifndef SQLITE_OMIT_WAL + +/* #include "wal.h" */ + +/* +** Trace output macros +*/ +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) +SQLITE_PRIVATE int sqlite3WalTrace = 0; +# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X +#else +# define WALTRACE(X) +#endif + +/* +** The maximum (and only) versions of the wal and wal-index formats +** that may be interpreted by this version of SQLite. +** +** If a client begins recovering a WAL file and finds that (a) the checksum +** values in the wal-header are correct and (b) the version field is not +** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN. +** +** Similarly, if a client successfully reads a wal-index header (i.e. the +** checksum test is successful) and finds that the version field is not +** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite +** returns SQLITE_CANTOPEN. +*/ +#define WAL_MAX_VERSION 3007000 +#define WALINDEX_MAX_VERSION 3007000 + +/* +** Indices of various locking bytes. WAL_NREADER is the number +** of available reader locks and should be at least 3. The default +** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. +*/ +#define WAL_WRITE_LOCK 0 +#define WAL_ALL_BUT_WRITE 1 +#define WAL_CKPT_LOCK 1 +#define WAL_RECOVER_LOCK 2 +#define WAL_READ_LOCK(I) (3+(I)) +#define WAL_NREADER (SQLITE_SHM_NLOCK-3) + + +/* Object declarations */ +typedef struct WalIndexHdr WalIndexHdr; +typedef struct WalIterator WalIterator; +typedef struct WalCkptInfo WalCkptInfo; + + +/* +** The following object holds a copy of the wal-index header content. +** +** The actual header in the wal-index consists of two copies of this +** object followed by one instance of the WalCkptInfo object. +** For all versions of SQLite through 3.10.0 and probably beyond, +** the locking bytes (WalCkptInfo.aLock) start at offset 120 and +** the total header size is 136 bytes. +** +** The szPage value can be any power of 2 between 512 and 32768, inclusive. +** Or it can be 1 to represent a 65536-byte page. The latter case was +** added in 3.7.1 when support for 64K pages was added. +*/ +struct WalIndexHdr { + u32 iVersion; /* Wal-index version */ + u32 unused; /* Unused (padding) field */ + u32 iChange; /* Counter incremented each transaction */ + u8 isInit; /* 1 when initialized */ + u8 bigEndCksum; /* True if checksums in WAL are big-endian */ + u16 szPage; /* Database page size in bytes. 1==64K */ + u32 mxFrame; /* Index of last valid frame in the WAL */ + u32 nPage; /* Size of database in pages */ + u32 aFrameCksum[2]; /* Checksum of last frame in log */ + u32 aSalt[2]; /* Two salt values copied from WAL header */ + u32 aCksum[2]; /* Checksum over all prior fields */ +}; + +/* +** A copy of the following object occurs in the wal-index immediately +** following the second copy of the WalIndexHdr. This object stores +** information used by checkpoint. +** +** nBackfill is the number of frames in the WAL that have been written +** back into the database. (We call the act of moving content from WAL to +** database "backfilling".) The nBackfill number is never greater than +** WalIndexHdr.mxFrame. nBackfill can only be increased by threads +** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). +** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from +** mxFrame back to zero when the WAL is reset. +** +** nBackfillAttempted is the largest value of nBackfill that a checkpoint +** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however +** the nBackfillAttempted is set before any backfilling is done and the +** nBackfill is only set after all backfilling completes. So if a checkpoint +** crashes, nBackfillAttempted might be larger than nBackfill. The +** WalIndexHdr.mxFrame must never be less than nBackfillAttempted. +** +** The aLock[] field is a set of bytes used for locking. These bytes should +** never be read or written. +** +** There is one entry in aReadMark[] for each reader lock. If a reader +** holds read-lock K, then the value in aReadMark[K] is no greater than +** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) +** for any aReadMark[] means that entry is unused. aReadMark[0] is +** a special case; its value is never used and it exists as a place-holder +** to avoid having to offset aReadMark[] indexs by one. Readers holding +** WAL_READ_LOCK(0) always ignore the entire WAL and read all content +** directly from the database. +** +** The value of aReadMark[K] may only be changed by a thread that +** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of +** aReadMark[K] cannot changed while there is a reader is using that mark +** since the reader will be holding a shared lock on WAL_READ_LOCK(K). +** +** The checkpointer may only transfer frames from WAL to database where +** the frame numbers are less than or equal to every aReadMark[] that is +** in use (that is, every aReadMark[j] for which there is a corresponding +** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the +** largest value and will increase an unused aReadMark[] to mxFrame if there +** is not already an aReadMark[] equal to mxFrame. The exception to the +** previous sentence is when nBackfill equals mxFrame (meaning that everything +** in the WAL has been backfilled into the database) then new readers +** will choose aReadMark[0] which has value 0 and hence such reader will +** get all their all content directly from the database file and ignore +** the WAL. +** +** Writers normally append new frames to the end of the WAL. However, +** if nBackfill equals mxFrame (meaning that all WAL content has been +** written back into the database) and if no readers are using the WAL +** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then +** the writer will first "reset" the WAL back to the beginning and start +** writing new content beginning at frame 1. +** +** We assume that 32-bit loads are atomic and so no locks are needed in +** order to read from any aReadMark[] entries. +*/ +struct WalCkptInfo { + u32 nBackfill; /* Number of WAL frames backfilled into DB */ + u32 aReadMark[WAL_NREADER]; /* Reader marks */ + u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */ + u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */ + u32 notUsed0; /* Available for future enhancements */ +}; +#define READMARK_NOT_USED 0xffffffff + + +/* A block of WALINDEX_LOCK_RESERVED bytes beginning at +** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems +** only support mandatory file-locks, we do not read or write data +** from the region of the file on which locks are applied. +*/ +#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)) +#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) + +/* Size of header before each frame in wal */ +#define WAL_FRAME_HDRSIZE 24 + +/* Size of write ahead log header, including checksum. */ +/* #define WAL_HDRSIZE 24 */ +#define WAL_HDRSIZE 32 + +/* WAL magic value. Either this value, or the same value with the least +** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit +** big-endian format in the first 4 bytes of a WAL file. +** +** If the LSB is set, then the checksums for each frame within the WAL +** file are calculated by treating all data as an array of 32-bit +** big-endian words. Otherwise, they are calculated by interpreting +** all data as 32-bit little-endian words. +*/ +#define WAL_MAGIC 0x377f0682 + +/* +** Return the offset of frame iFrame in the write-ahead log file, +** assuming a database page size of szPage bytes. The offset returned +** is to the start of the write-ahead log frame-header. +*/ +#define walFrameOffset(iFrame, szPage) ( \ + WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \ +) + +/* +** An open write-ahead log file is represented by an instance of the +** following object. +*/ +struct Wal { + sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */ + sqlite3_file *pDbFd; /* File handle for the database file */ + sqlite3_file *pWalFd; /* File handle for WAL file */ + u32 iCallback; /* Value to pass to log callback (or 0) */ + i64 mxWalSize; /* Truncate WAL to this size upon reset */ + int nWiData; /* Size of array apWiData */ + int szFirstBlock; /* Size of first block written to WAL file */ + volatile u32 **apWiData; /* Pointer to wal-index content in memory */ + u32 szPage; /* Database page size */ + i16 readLock; /* Which read lock is being held. -1 for none */ + u8 syncFlags; /* Flags to use to sync header writes */ + u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ + u8 writeLock; /* True if in a write transaction */ + u8 ckptLock; /* True if holding a checkpoint lock */ + u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */ + u8 truncateOnCommit; /* True to truncate WAL file on commit */ + u8 syncHeader; /* Fsync the WAL header if true */ + u8 padToSectorBoundary; /* Pad transactions out to the next sector */ + WalIndexHdr hdr; /* Wal-index header for current transaction */ + u32 minFrame; /* Ignore wal frames before this one */ + u32 iReCksum; /* On commit, recalculate checksums from here */ + const char *zWalName; /* Name of WAL file */ + u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ +#ifdef SQLITE_DEBUG + u8 lockError; /* True if a locking error has occurred */ +#endif +#ifdef SQLITE_ENABLE_SNAPSHOT + WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ +#endif +}; + +/* +** Candidate values for Wal.exclusiveMode. +*/ +#define WAL_NORMAL_MODE 0 +#define WAL_EXCLUSIVE_MODE 1 +#define WAL_HEAPMEMORY_MODE 2 + +/* +** Possible values for WAL.readOnly +*/ +#define WAL_RDWR 0 /* Normal read/write connection */ +#define WAL_RDONLY 1 /* The WAL file is readonly */ +#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */ + +/* +** Each page of the wal-index mapping contains a hash-table made up of +** an array of HASHTABLE_NSLOT elements of the following type. +*/ +typedef u16 ht_slot; + +/* +** This structure is used to implement an iterator that loops through +** all frames in the WAL in database page order. Where two or more frames +** correspond to the same database page, the iterator visits only the +** frame most recently written to the WAL (in other words, the frame with +** the largest index). +** +** The internals of this structure are only accessed by: +** +** walIteratorInit() - Create a new iterator, +** walIteratorNext() - Step an iterator, +** walIteratorFree() - Free an iterator. +** +** This functionality is used by the checkpoint code (see walCheckpoint()). +*/ +struct WalIterator { + int iPrior; /* Last result returned from the iterator */ + int nSegment; /* Number of entries in aSegment[] */ + struct WalSegment { + int iNext; /* Next slot in aIndex[] not yet returned */ + ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */ + u32 *aPgno; /* Array of page numbers. */ + int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */ + int iZero; /* Frame number associated with aPgno[0] */ + } aSegment[1]; /* One for every 32KB page in the wal-index */ +}; + +/* +** Define the parameters of the hash tables in the wal-index file. There +** is a hash-table following every HASHTABLE_NPAGE page numbers in the +** wal-index. +** +** Changing any of these constants will alter the wal-index format and +** create incompatibilities. +*/ +#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */ +#define HASHTABLE_HASH_1 383 /* Should be prime */ +#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */ + +/* +** The block of page numbers associated with the first hash-table in a +** wal-index is smaller than usual. This is so that there is a complete +** hash-table on each aligned 32KB page of the wal-index. +*/ +#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32))) + +/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */ +#define WALINDEX_PGSZ ( \ + sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \ +) + +/* +** Obtain a pointer to the iPage'th page of the wal-index. The wal-index +** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are +** numbered from zero. +** +** If this call is successful, *ppPage is set to point to the wal-index +** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, +** then an SQLite error code is returned and *ppPage is set to 0. +*/ +static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ + int rc = SQLITE_OK; + + /* Enlarge the pWal->apWiData[] array if required */ + if( pWal->nWiData<=iPage ){ + int nByte = sizeof(u32*)*(iPage+1); + volatile u32 **apNew; + apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); + if( !apNew ){ + *ppPage = 0; + return SQLITE_NOMEM_BKPT; + } + memset((void*)&apNew[pWal->nWiData], 0, + sizeof(u32*)*(iPage+1-pWal->nWiData)); + pWal->apWiData = apNew; + pWal->nWiData = iPage+1; + } + + /* Request a pointer to the required page from the VFS */ + if( pWal->apWiData[iPage]==0 ){ + if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ + pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ); + if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT; + }else{ + rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, + pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] + ); + if( rc==SQLITE_READONLY ){ + pWal->readOnly |= WAL_SHM_RDONLY; + rc = SQLITE_OK; + } + } + } + + *ppPage = pWal->apWiData[iPage]; + assert( iPage==0 || *ppPage || rc!=SQLITE_OK ); + return rc; +} + +/* +** Return a pointer to the WalCkptInfo structure in the wal-index. +*/ +static volatile WalCkptInfo *walCkptInfo(Wal *pWal){ + assert( pWal->nWiData>0 && pWal->apWiData[0] ); + return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]); +} + +/* +** Return a pointer to the WalIndexHdr structure in the wal-index. +*/ +static volatile WalIndexHdr *walIndexHdr(Wal *pWal){ + assert( pWal->nWiData>0 && pWal->apWiData[0] ); + return (volatile WalIndexHdr*)pWal->apWiData[0]; +} + +/* +** The argument to this macro must be of type u32. On a little-endian +** architecture, it returns the u32 value that results from interpreting +** the 4 bytes as a big-endian value. On a big-endian architecture, it +** returns the value that would be produced by interpreting the 4 bytes +** of the input value as a little-endian integer. +*/ +#define BYTESWAP32(x) ( \ + (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \ + + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \ +) + +/* +** Generate or extend an 8 byte checksum based on the data in +** array aByte[] and the initial values of aIn[0] and aIn[1] (or +** initial values of 0 and 0 if aIn==NULL). +** +** The checksum is written back into aOut[] before returning. +** +** nByte must be a positive multiple of 8. +*/ +static void walChecksumBytes( + int nativeCksum, /* True for native byte-order, false for non-native */ + u8 *a, /* Content to be checksummed */ + int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */ + const u32 *aIn, /* Initial checksum value input */ + u32 *aOut /* OUT: Final checksum value output */ +){ + u32 s1, s2; + u32 *aData = (u32 *)a; + u32 *aEnd = (u32 *)&a[nByte]; + + if( aIn ){ + s1 = aIn[0]; + s2 = aIn[1]; + }else{ + s1 = s2 = 0; + } + + assert( nByte>=8 ); + assert( (nByte&0x00000007)==0 ); + + if( nativeCksum ){ + do { + s1 += *aData++ + s2; + s2 += *aData++ + s1; + }while( aDataexclusiveMode!=WAL_HEAPMEMORY_MODE ){ + sqlite3OsShmBarrier(pWal->pDbFd); + } +} + +/* +** Write the header information in pWal->hdr into the wal-index. +** +** The checksum on pWal->hdr is updated before it is written. +*/ +static void walIndexWriteHdr(Wal *pWal){ + volatile WalIndexHdr *aHdr = walIndexHdr(pWal); + const int nCksum = offsetof(WalIndexHdr, aCksum); + + assert( pWal->writeLock ); + pWal->hdr.isInit = 1; + pWal->hdr.iVersion = WALINDEX_MAX_VERSION; + walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); + memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); + walShmBarrier(pWal); + memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); +} + +/* +** This function encodes a single frame header and writes it to a buffer +** supplied by the caller. A frame-header is made up of a series of +** 4-byte big-endian integers, as follows: +** +** 0: Page number. +** 4: For commit records, the size of the database image in pages +** after the commit. For all other records, zero. +** 8: Salt-1 (copied from the wal-header) +** 12: Salt-2 (copied from the wal-header) +** 16: Checksum-1. +** 20: Checksum-2. +*/ +static void walEncodeFrame( + Wal *pWal, /* The write-ahead log */ + u32 iPage, /* Database page number for frame */ + u32 nTruncate, /* New db size (or 0 for non-commit frames) */ + u8 *aData, /* Pointer to page data */ + u8 *aFrame /* OUT: Write encoded frame here */ +){ + int nativeCksum; /* True for native byte-order checksums */ + u32 *aCksum = pWal->hdr.aFrameCksum; + assert( WAL_FRAME_HDRSIZE==24 ); + sqlite3Put4byte(&aFrame[0], iPage); + sqlite3Put4byte(&aFrame[4], nTruncate); + if( pWal->iReCksum==0 ){ + memcpy(&aFrame[8], pWal->hdr.aSalt, 8); + + nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); + walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); + walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); + + sqlite3Put4byte(&aFrame[16], aCksum[0]); + sqlite3Put4byte(&aFrame[20], aCksum[1]); + }else{ + memset(&aFrame[8], 0, 16); + } +} + +/* +** Check to see if the frame with header in aFrame[] and content +** in aData[] is valid. If it is a valid frame, fill *piPage and +** *pnTruncate and return true. Return if the frame is not valid. +*/ +static int walDecodeFrame( + Wal *pWal, /* The write-ahead log */ + u32 *piPage, /* OUT: Database page number for frame */ + u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ + u8 *aData, /* Pointer to page data (for checksum) */ + u8 *aFrame /* Frame data */ +){ + int nativeCksum; /* True for native byte-order checksums */ + u32 *aCksum = pWal->hdr.aFrameCksum; + u32 pgno; /* Page number of the frame */ + assert( WAL_FRAME_HDRSIZE==24 ); + + /* A frame is only valid if the salt values in the frame-header + ** match the salt values in the wal-header. + */ + if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){ + return 0; + } + + /* A frame is only valid if the page number is creater than zero. + */ + pgno = sqlite3Get4byte(&aFrame[0]); + if( pgno==0 ){ + return 0; + } + + /* A frame is only valid if a checksum of the WAL header, + ** all prior frams, the first 16 bytes of this frame-header, + ** and the frame-data matches the checksum in the last 8 + ** bytes of this frame-header. + */ + nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); + walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); + walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); + if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) + || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) + ){ + /* Checksum failed. */ + return 0; + } + + /* If we reach this point, the frame is valid. Return the page number + ** and the new database size. + */ + *piPage = pgno; + *pnTruncate = sqlite3Get4byte(&aFrame[4]); + return 1; +} + + +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) +/* +** Names of locks. This routine is used to provide debugging output and is not +** a part of an ordinary build. +*/ +static const char *walLockName(int lockIdx){ + if( lockIdx==WAL_WRITE_LOCK ){ + return "WRITE-LOCK"; + }else if( lockIdx==WAL_CKPT_LOCK ){ + return "CKPT-LOCK"; + }else if( lockIdx==WAL_RECOVER_LOCK ){ + return "RECOVER-LOCK"; + }else{ + static char zName[15]; + sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]", + lockIdx-WAL_READ_LOCK(0)); + return zName; + } +} +#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */ + + +/* +** Set or release locks on the WAL. Locks are either shared or exclusive. +** A lock cannot be moved directly between shared and exclusive - it must go +** through the unlocked state first. +** +** In locking_mode=EXCLUSIVE, all of these routines become no-ops. +*/ +static int walLockShared(Wal *pWal, int lockIdx){ + int rc; + if( pWal->exclusiveMode ) return SQLITE_OK; + rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, + SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); + WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, + walLockName(lockIdx), rc ? "failed" : "ok")); + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) + return rc; +} +static void walUnlockShared(Wal *pWal, int lockIdx){ + if( pWal->exclusiveMode ) return; + (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, + SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); + WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); +} +static int walLockExclusive(Wal *pWal, int lockIdx, int n){ + int rc; + if( pWal->exclusiveMode ) return SQLITE_OK; + rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, + SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); + WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, + walLockName(lockIdx), n, rc ? "failed" : "ok")); + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) + return rc; +} +static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ + if( pWal->exclusiveMode ) return; + (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, + SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE); + WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal, + walLockName(lockIdx), n)); +} + +/* +** Compute a hash on a page number. The resulting hash value must land +** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances +** the hash to the next value in the event of a collision. +*/ +static int walHash(u32 iPage){ + assert( iPage>0 ); + assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 ); + return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1); +} +static int walNextHash(int iPriorHash){ + return (iPriorHash+1)&(HASHTABLE_NSLOT-1); +} + +/* +** Return pointers to the hash table and page number array stored on +** page iHash of the wal-index. The wal-index is broken into 32KB pages +** numbered starting from 0. +** +** Set output variable *paHash to point to the start of the hash table +** in the wal-index file. Set *piZero to one less than the frame +** number of the first frame indexed by this hash table. If a +** slot in the hash table is set to N, it refers to frame number +** (*piZero+N) in the log. +** +** Finally, set *paPgno so that *paPgno[1] is the page number of the +** first frame indexed by the hash table, frame (*piZero+1). +*/ +static int walHashGet( + Wal *pWal, /* WAL handle */ + int iHash, /* Find the iHash'th table */ + volatile ht_slot **paHash, /* OUT: Pointer to hash index */ + volatile u32 **paPgno, /* OUT: Pointer to page number array */ + u32 *piZero /* OUT: Frame associated with *paPgno[0] */ +){ + int rc; /* Return code */ + volatile u32 *aPgno; + + rc = walIndexPage(pWal, iHash, &aPgno); + assert( rc==SQLITE_OK || iHash>0 ); + + if( rc==SQLITE_OK ){ + u32 iZero; + volatile ht_slot *aHash; + + aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE]; + if( iHash==0 ){ + aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; + iZero = 0; + }else{ + iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; + } + + *paPgno = &aPgno[-1]; + *paHash = aHash; + *piZero = iZero; + } + return rc; +} + +/* +** Return the number of the wal-index page that contains the hash-table +** and page-number array that contain entries corresponding to WAL frame +** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages +** are numbered starting from 0. +*/ +static int walFramePage(u32 iFrame){ + int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE; + assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE) + && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE) + && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)) + && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE) + && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE)) + ); + return iHash; +} + +/* +** Return the page number associated with frame iFrame in this WAL. +*/ +static u32 walFramePgno(Wal *pWal, u32 iFrame){ + int iHash = walFramePage(iFrame); + if( iHash==0 ){ + return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1]; + } + return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE]; +} + +/* +** Remove entries from the hash table that point to WAL slots greater +** than pWal->hdr.mxFrame. +** +** This function is called whenever pWal->hdr.mxFrame is decreased due +** to a rollback or savepoint. +** +** At most only the hash table containing pWal->hdr.mxFrame needs to be +** updated. Any later hash tables will be automatically cleared when +** pWal->hdr.mxFrame advances to the point where those hash tables are +** actually needed. +*/ +static void walCleanupHash(Wal *pWal){ + volatile ht_slot *aHash = 0; /* Pointer to hash table to clear */ + volatile u32 *aPgno = 0; /* Page number array for hash table */ + u32 iZero = 0; /* frame == (aHash[x]+iZero) */ + int iLimit = 0; /* Zero values greater than this */ + int nByte; /* Number of bytes to zero in aPgno[] */ + int i; /* Used to iterate through aHash[] */ + + assert( pWal->writeLock ); + testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 ); + testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE ); + testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 ); + + if( pWal->hdr.mxFrame==0 ) return; + + /* Obtain pointers to the hash-table and page-number array containing + ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed + ** that the page said hash-table and array reside on is already mapped. + */ + assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); + assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); + walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero); + + /* Zero all hash-table entries that correspond to frame numbers greater + ** than pWal->hdr.mxFrame. + */ + iLimit = pWal->hdr.mxFrame - iZero; + assert( iLimit>0 ); + for(i=0; iiLimit ){ + aHash[i] = 0; + } + } + + /* Zero the entries in the aPgno array that correspond to frames with + ** frame numbers greater than pWal->hdr.mxFrame. + */ + nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]); + memset((void *)&aPgno[iLimit+1], 0, nByte); + +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT + /* Verify that the every entry in the mapping region is still reachable + ** via the hash table even after the cleanup. + */ + if( iLimit ){ + int j; /* Loop counter */ + int iKey; /* Hash key */ + for(j=1; j<=iLimit; j++){ + for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){ + if( aHash[iKey]==j ) break; + } + assert( aHash[iKey]==j ); + } + } +#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ +} + + +/* +** Set an entry in the wal-index that will map database page number +** pPage into WAL frame iFrame. +*/ +static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ + int rc; /* Return code */ + u32 iZero = 0; /* One less than frame number of aPgno[1] */ + volatile u32 *aPgno = 0; /* Page number array */ + volatile ht_slot *aHash = 0; /* Hash table */ + + rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero); + + /* Assuming the wal-index file was successfully mapped, populate the + ** page number array and hash table entry. + */ + if( rc==SQLITE_OK ){ + int iKey; /* Hash table key */ + int idx; /* Value to write to hash-table slot */ + int nCollide; /* Number of hash collisions */ + + idx = iFrame - iZero; + assert( idx <= HASHTABLE_NSLOT/2 + 1 ); + + /* If this is the first entry to be added to this hash-table, zero the + ** entire hash table and aPgno[] array before proceeding. + */ + if( idx==1 ){ + int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]); + memset((void*)&aPgno[1], 0, nByte); + } + + /* If the entry in aPgno[] is already set, then the previous writer + ** must have exited unexpectedly in the middle of a transaction (after + ** writing one or more dirty pages to the WAL to free up memory). + ** Remove the remnants of that writers uncommitted transaction from + ** the hash-table before writing any new entries. + */ + if( aPgno[idx] ){ + walCleanupHash(pWal); + assert( !aPgno[idx] ); + } + + /* Write the aPgno[] array entry and the hash-table slot. */ + nCollide = idx; + for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){ + if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT; + } + aPgno[idx] = iPage; + aHash[iKey] = (ht_slot)idx; + +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT + /* Verify that the number of entries in the hash table exactly equals + ** the number of entries in the mapping region. + */ + { + int i; /* Loop counter */ + int nEntry = 0; /* Number of entries in the hash table */ + for(i=0; ickptLock==1 || pWal->ckptLock==0 ); + assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 ); + assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE ); + assert( pWal->writeLock ); + iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; + nLock = SQLITE_SHM_NLOCK - iLock; + rc = walLockExclusive(pWal, iLock, nLock); + if( rc ){ + return rc; + } + WALTRACE(("WAL%p: recovery begin...\n", pWal)); + + memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); + + rc = sqlite3OsFileSize(pWal->pWalFd, &nSize); + if( rc!=SQLITE_OK ){ + goto recovery_error; + } + + if( nSize>WAL_HDRSIZE ){ + u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ + u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ + int szFrame; /* Number of bytes in buffer aFrame[] */ + u8 *aData; /* Pointer to data part of aFrame buffer */ + int iFrame; /* Index of last frame read */ + i64 iOffset; /* Next offset to read from log file */ + int szPage; /* Page size according to the log */ + u32 magic; /* Magic value read from WAL header */ + u32 version; /* Magic value read from WAL header */ + int isValid; /* True if this frame is valid */ + + /* Read in the WAL header. */ + rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); + if( rc!=SQLITE_OK ){ + goto recovery_error; + } + + /* If the database page size is not a power of two, or is greater than + ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid + ** data. Similarly, if the 'magic' value is invalid, ignore the whole + ** WAL file. + */ + magic = sqlite3Get4byte(&aBuf[0]); + szPage = sqlite3Get4byte(&aBuf[8]); + if( (magic&0xFFFFFFFE)!=WAL_MAGIC + || szPage&(szPage-1) + || szPage>SQLITE_MAX_PAGE_SIZE + || szPage<512 + ){ + goto finished; + } + pWal->hdr.bigEndCksum = (u8)(magic&0x00000001); + pWal->szPage = szPage; + pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); + memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); + + /* Verify that the WAL header checksum is correct */ + walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, + aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum + ); + if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24]) + || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28]) + ){ + goto finished; + } + + /* Verify that the version number on the WAL format is one that + ** are able to understand */ + version = sqlite3Get4byte(&aBuf[4]); + if( version!=WAL_MAX_VERSION ){ + rc = SQLITE_CANTOPEN_BKPT; + goto finished; + } + + /* Malloc a buffer to read frames into. */ + szFrame = szPage + WAL_FRAME_HDRSIZE; + aFrame = (u8 *)sqlite3_malloc64(szFrame); + if( !aFrame ){ + rc = SQLITE_NOMEM_BKPT; + goto recovery_error; + } + aData = &aFrame[WAL_FRAME_HDRSIZE]; + + /* Read all frames from the log file. */ + iFrame = 0; + for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){ + u32 pgno; /* Database page number for frame */ + u32 nTruncate; /* dbsize field from frame header */ + + /* Read and decode the next log frame. */ + iFrame++; + rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); + if( rc!=SQLITE_OK ) break; + isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); + if( !isValid ) break; + rc = walIndexAppend(pWal, iFrame, pgno); + if( rc!=SQLITE_OK ) break; + + /* If nTruncate is non-zero, this is a commit record. */ + if( nTruncate ){ + pWal->hdr.mxFrame = iFrame; + pWal->hdr.nPage = nTruncate; + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); + testcase( szPage<=32768 ); + testcase( szPage>=65536 ); + aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; + aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; + } + } + + sqlite3_free(aFrame); + } + +finished: + if( rc==SQLITE_OK ){ + volatile WalCkptInfo *pInfo; + int i; + pWal->hdr.aFrameCksum[0] = aFrameCksum[0]; + pWal->hdr.aFrameCksum[1] = aFrameCksum[1]; + walIndexWriteHdr(pWal); + + /* Reset the checkpoint-header. This is safe because this thread is + ** currently holding locks that exclude all other readers, writers and + ** checkpointers. + */ + pInfo = walCkptInfo(pWal); + pInfo->nBackfill = 0; + pInfo->nBackfillAttempted = pWal->hdr.mxFrame; + pInfo->aReadMark[0] = 0; + for(i=1; iaReadMark[i] = READMARK_NOT_USED; + if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; + + /* If more than one frame was recovered from the log file, report an + ** event via sqlite3_log(). This is to help with identifying performance + ** problems caused by applications routinely shutting down without + ** checkpointing the log file. + */ + if( pWal->hdr.nPage ){ + sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, + "recovered %d frames from WAL file %s", + pWal->hdr.mxFrame, pWal->zWalName + ); + } + } + +recovery_error: + WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); + walUnlockExclusive(pWal, iLock, nLock); + return rc; +} + +/* +** Close an open wal-index. +*/ +static void walIndexClose(Wal *pWal, int isDelete){ + if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ + int i; + for(i=0; inWiData; i++){ + sqlite3_free((void *)pWal->apWiData[i]); + pWal->apWiData[i] = 0; + } + }else{ + sqlite3OsShmUnmap(pWal->pDbFd, isDelete); + } +} + +/* +** Open a connection to the WAL file zWalName. The database file must +** already be opened on connection pDbFd. The buffer that zWalName points +** to must remain valid for the lifetime of the returned Wal* handle. +** +** A SHARED lock should be held on the database file when this function +** is called. The purpose of this SHARED lock is to prevent any other +** client from unlinking the WAL or wal-index file. If another process +** were to do this just after this client opened one of these files, the +** system would be badly broken. +** +** If the log file is successfully opened, SQLITE_OK is returned and +** *ppWal is set to point to a new WAL handle. If an error occurs, +** an SQLite error code is returned and *ppWal is left unmodified. +*/ +SQLITE_PRIVATE int sqlite3WalOpen( + sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */ + sqlite3_file *pDbFd, /* The open database file */ + const char *zWalName, /* Name of the WAL file */ + int bNoShm, /* True to run in heap-memory mode */ + i64 mxWalSize, /* Truncate WAL to this size on reset */ + Wal **ppWal /* OUT: Allocated Wal handle */ +){ + int rc; /* Return Code */ + Wal *pRet; /* Object to allocate and return */ + int flags; /* Flags passed to OsOpen() */ + + assert( zWalName && zWalName[0] ); + assert( pDbFd ); + + /* In the amalgamation, the os_unix.c and os_win.c source files come before + ** this source file. Verify that the #defines of the locking byte offsets + ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. + ** For that matter, if the lock offset ever changes from its initial design + ** value of 120, we need to know that so there is an assert() to check it. + */ + assert( 120==WALINDEX_LOCK_OFFSET ); + assert( 136==WALINDEX_HDR_SIZE ); +#ifdef WIN_SHM_BASE + assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); +#endif +#ifdef UNIX_SHM_BASE + assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); +#endif + + + /* Allocate an instance of struct Wal to return. */ + *ppWal = 0; + pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile); + if( !pRet ){ + return SQLITE_NOMEM_BKPT; + } + + pRet->pVfs = pVfs; + pRet->pWalFd = (sqlite3_file *)&pRet[1]; + pRet->pDbFd = pDbFd; + pRet->readLock = -1; + pRet->mxWalSize = mxWalSize; + pRet->zWalName = zWalName; + pRet->syncHeader = 1; + pRet->padToSectorBoundary = 1; + pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE); + + /* Open file handle on the write-ahead log file. */ + flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL); + rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags); + if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){ + pRet->readOnly = WAL_RDONLY; + } + + if( rc!=SQLITE_OK ){ + walIndexClose(pRet, 0); + sqlite3OsClose(pRet->pWalFd); + sqlite3_free(pRet); + }else{ + int iDC = sqlite3OsDeviceCharacteristics(pDbFd); + if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; } + if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){ + pRet->padToSectorBoundary = 0; + } + *ppWal = pRet; + WALTRACE(("WAL%d: opened\n", pRet)); + } + return rc; +} + +/* +** Change the size to which the WAL file is trucated on each reset. +*/ +SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){ + if( pWal ) pWal->mxWalSize = iLimit; +} + +/* +** Find the smallest page number out of all pages held in the WAL that +** has not been returned by any prior invocation of this method on the +** same WalIterator object. Write into *piFrame the frame index where +** that page was last written into the WAL. Write into *piPage the page +** number. +** +** Return 0 on success. If there are no pages in the WAL with a page +** number larger than *piPage, then return 1. +*/ +static int walIteratorNext( + WalIterator *p, /* Iterator */ + u32 *piPage, /* OUT: The page number of the next page */ + u32 *piFrame /* OUT: Wal frame index of next page */ +){ + u32 iMin; /* Result pgno must be greater than iMin */ + u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */ + int i; /* For looping through segments */ + + iMin = p->iPrior; + assert( iMin<0xffffffff ); + for(i=p->nSegment-1; i>=0; i--){ + struct WalSegment *pSegment = &p->aSegment[i]; + while( pSegment->iNextnEntry ){ + u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]]; + if( iPg>iMin ){ + if( iPgiZero + pSegment->aIndex[pSegment->iNext]; + } + break; + } + pSegment->iNext++; + } + } + + *piPage = p->iPrior = iRet; + return (iRet==0xFFFFFFFF); +} + +/* +** This function merges two sorted lists into a single sorted list. +** +** aLeft[] and aRight[] are arrays of indices. The sort key is +** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following +** is guaranteed for all J0 && nRight>0 ); + while( iRight=nRight || aContent[aLeft[iLeft]]=nLeft || aContent[aLeft[iLeft]]>dbpage ); + assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); + } + + *paRight = aLeft; + *pnRight = iOut; + memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut); +} + +/* +** Sort the elements in list aList using aContent[] as the sort key. +** Remove elements with duplicate keys, preferring to keep the +** larger aList[] values. +** +** The aList[] entries are indices into aContent[]. The values in +** aList[] are to be sorted so that for all J0 ); + assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) ); + + for(iList=0; iListaList && p->nList<=(1<aList==&aList[iList&~((2<aList, p->nList, &aMerge, &nMerge, aBuffer); + } + aSub[iSub].aList = aMerge; + aSub[iSub].nList = nMerge; + } + + for(iSub++; iSubnList<=(1<aList==&aList[nList&~((2<aList, p->nList, &aMerge, &nMerge, aBuffer); + } + } + assert( aMerge==aList ); + *pnList = nMerge; + +#ifdef SQLITE_DEBUG + { + int i; + for(i=1; i<*pnList; i++){ + assert( aContent[aList[i]] > aContent[aList[i-1]] ); + } + } +#endif +} + +/* +** Free an iterator allocated by walIteratorInit(). +*/ +static void walIteratorFree(WalIterator *p){ + sqlite3_free(p); +} + +/* +** Construct a WalInterator object that can be used to loop over all +** pages in the WAL in ascending order. The caller must hold the checkpoint +** lock. +** +** On success, make *pp point to the newly allocated WalInterator object +** return SQLITE_OK. Otherwise, return an error code. If this routine +** returns an error, the value of *pp is undefined. +** +** The calling routine should invoke walIteratorFree() to destroy the +** WalIterator object when it has finished with it. +*/ +static int walIteratorInit(Wal *pWal, WalIterator **pp){ + WalIterator *p; /* Return value */ + int nSegment; /* Number of segments to merge */ + u32 iLast; /* Last frame in log */ + int nByte; /* Number of bytes to allocate */ + int i; /* Iterator variable */ + ht_slot *aTmp; /* Temp space used by merge-sort */ + int rc = SQLITE_OK; /* Return Code */ + + /* This routine only runs while holding the checkpoint lock. And + ** it only runs if there is actually content in the log (mxFrame>0). + */ + assert( pWal->ckptLock && pWal->hdr.mxFrame>0 ); + iLast = pWal->hdr.mxFrame; + + /* Allocate space for the WalIterator object. */ + nSegment = walFramePage(iLast) + 1; + nByte = sizeof(WalIterator) + + (nSegment-1)*sizeof(struct WalSegment) + + iLast*sizeof(ht_slot); + p = (WalIterator *)sqlite3_malloc64(nByte); + if( !p ){ + return SQLITE_NOMEM_BKPT; + } + memset(p, 0, nByte); + p->nSegment = nSegment; + + /* Allocate temporary space used by the merge-sort routine. This block + ** of memory will be freed before this function returns. + */ + aTmp = (ht_slot *)sqlite3_malloc64( + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) + ); + if( !aTmp ){ + rc = SQLITE_NOMEM_BKPT; + } + + for(i=0; rc==SQLITE_OK && iaSegment[p->nSegment])[iZero]; + iZero++; + + for(j=0; jaSegment[i].iZero = iZero; + p->aSegment[i].nEntry = nEntry; + p->aSegment[i].aIndex = aIndex; + p->aSegment[i].aPgno = (u32 *)aPgno; + } + } + sqlite3_free(aTmp); + + if( rc!=SQLITE_OK ){ + walIteratorFree(p); + } + *pp = p; + return rc; +} + +/* +** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and +** n. If the attempt fails and parameter xBusy is not NULL, then it is a +** busy-handler function. Invoke it and retry the lock until either the +** lock is successfully obtained or the busy-handler returns 0. +*/ +static int walBusyLock( + Wal *pWal, /* WAL connection */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ + int lockIdx, /* Offset of first byte to lock */ + int n /* Number of bytes to lock */ +){ + int rc; + do { + rc = walLockExclusive(pWal, lockIdx, n); + }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); + return rc; +} + +/* +** The cache of the wal-index header must be valid to call this function. +** Return the page-size in bytes used by the database. +*/ +static int walPagesize(Wal *pWal){ + return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); +} + +/* +** The following is guaranteed when this function is called: +** +** a) the WRITER lock is held, +** b) the entire log file has been checkpointed, and +** c) any existing readers are reading exclusively from the database +** file - there are no readers that may attempt to read a frame from +** the log file. +** +** This function updates the shared-memory structures so that the next +** client to write to the database (which may be this one) does so by +** writing frames into the start of the log file. +** +** The value of parameter salt1 is used as the aSalt[1] value in the +** new wal-index header. It should be passed a pseudo-random value (i.e. +** one obtained from sqlite3_randomness()). +*/ +static void walRestartHdr(Wal *pWal, u32 salt1){ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + int i; /* Loop counter */ + u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */ + pWal->nCkpt++; + pWal->hdr.mxFrame = 0; + sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); + memcpy(&pWal->hdr.aSalt[1], &salt1, 4); + walIndexWriteHdr(pWal); + pInfo->nBackfill = 0; + pInfo->nBackfillAttempted = 0; + pInfo->aReadMark[1] = 0; + for(i=2; iaReadMark[i] = READMARK_NOT_USED; + assert( pInfo->aReadMark[0]==0 ); +} + +/* +** Copy as much content as we can from the WAL back into the database file +** in response to an sqlite3_wal_checkpoint() request or the equivalent. +** +** The amount of information copies from WAL to database might be limited +** by active readers. This routine will never overwrite a database page +** that a concurrent reader might be using. +** +** All I/O barrier operations (a.k.a fsyncs) occur in this routine when +** SQLite is in WAL-mode in synchronous=NORMAL. That means that if +** checkpoints are always run by a background thread or background +** process, foreground threads will never block on a lengthy fsync call. +** +** Fsync is called on the WAL before writing content out of the WAL and +** into the database. This ensures that if the new content is persistent +** in the WAL and can be recovered following a power-loss or hard reset. +** +** Fsync is also called on the database file if (and only if) the entire +** WAL content is copied into the database file. This second fsync makes +** it safe to delete the WAL since the new content will persist in the +** database file. +** +** This routine uses and updates the nBackfill field of the wal-index header. +** This is the only routine that will increase the value of nBackfill. +** (A WAL reset or recovery will revert nBackfill to zero, but not increase +** its value.) +** +** The caller must be holding sufficient locks to ensure that no other +** checkpoint is running (in any other thread or process) at the same +** time. +*/ +static int walCheckpoint( + Wal *pWal, /* Wal connection */ + int eMode, /* One of PASSIVE, FULL or RESTART */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ + int sync_flags, /* Flags for OsSync() (or 0) */ + u8 *zBuf /* Temporary buffer to use */ +){ + int rc = SQLITE_OK; /* Return code */ + int szPage; /* Database page-size */ + WalIterator *pIter = 0; /* Wal iterator context */ + u32 iDbpage = 0; /* Next database page to write */ + u32 iFrame = 0; /* Wal frame containing data for iDbpage */ + u32 mxSafeFrame; /* Max frame that can be backfilled */ + u32 mxPage; /* Max database page to write */ + int i; /* Loop counter */ + volatile WalCkptInfo *pInfo; /* The checkpoint status information */ + + szPage = walPagesize(pWal); + testcase( szPage<=32768 ); + testcase( szPage>=65536 ); + pInfo = walCkptInfo(pWal); + if( pInfo->nBackfillhdr.mxFrame ){ + + /* Allocate the iterator */ + rc = walIteratorInit(pWal, &pIter); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( pIter ); + + /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked + ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ + assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); + + /* Compute in mxSafeFrame the index of the last frame of the WAL that is + ** safe to write into the database. Frames beyond mxSafeFrame might + ** overwrite database pages that are in use by active readers and thus + ** cannot be backfilled from the WAL. + */ + mxSafeFrame = pWal->hdr.mxFrame; + mxPage = pWal->hdr.nPage; + for(i=1; iaReadMark[i]; + if( mxSafeFrame>y ){ + assert( y<=pWal->hdr.mxFrame ); + rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); + if( rc==SQLITE_OK ){ + pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + }else if( rc==SQLITE_BUSY ){ + mxSafeFrame = y; + xBusy = 0; + }else{ + goto walcheckpoint_out; + } + } + } + + if( pInfo->nBackfillnBackfill; + + pInfo->nBackfillAttempted = mxSafeFrame; + + /* Sync the WAL to disk */ + if( sync_flags ){ + rc = sqlite3OsSync(pWal->pWalFd, sync_flags); + } + + /* If the database may grow as a result of this checkpoint, hint + ** about the eventual size of the db file to the VFS layer. + */ + if( rc==SQLITE_OK ){ + i64 nReq = ((i64)mxPage * szPage); + rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); + if( rc==SQLITE_OK && nSizepDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); + } + } + + + /* Iterate through the contents of the WAL, copying data to the db file */ + while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ + i64 iOffset; + assert( walFramePgno(pWal, iFrame)==iDbpage ); + if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){ + continue; + } + iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE; + /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ + rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset); + if( rc!=SQLITE_OK ) break; + iOffset = (iDbpage-1)*(i64)szPage; + testcase( IS_BIG_INT(iOffset) ); + rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset); + if( rc!=SQLITE_OK ) break; + } + + /* If work was actually accomplished... */ + if( rc==SQLITE_OK ){ + if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ + i64 szDb = pWal->hdr.nPage*(i64)szPage; + testcase( IS_BIG_INT(szDb) ); + rc = sqlite3OsTruncate(pWal->pDbFd, szDb); + if( rc==SQLITE_OK && sync_flags ){ + rc = sqlite3OsSync(pWal->pDbFd, sync_flags); + } + } + if( rc==SQLITE_OK ){ + pInfo->nBackfill = mxSafeFrame; + } + } + + /* Release the reader lock held while backfilling */ + walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); + } + + if( rc==SQLITE_BUSY ){ + /* Reset the return code so as not to report a checkpoint failure + ** just because there are active readers. */ + rc = SQLITE_OK; + } + } + + /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the + ** entire wal file has been copied into the database file, then block + ** until all readers have finished using the wal file. This ensures that + ** the next process to write to the database restarts the wal file. + */ + if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){ + assert( pWal->writeLock ); + if( pInfo->nBackfillhdr.mxFrame ){ + rc = SQLITE_BUSY; + }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){ + u32 salt1; + sqlite3_randomness(4, &salt1); + assert( pInfo->nBackfill==pWal->hdr.mxFrame ); + rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1); + if( rc==SQLITE_OK ){ + if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){ + /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as + ** SQLITE_CHECKPOINT_RESTART with the addition that it also + ** truncates the log file to zero bytes just prior to a + ** successful return. + ** + ** In theory, it might be safe to do this without updating the + ** wal-index header in shared memory, as all subsequent reader or + ** writer clients should see that the entire log file has been + ** checkpointed and behave accordingly. This seems unsafe though, + ** as it would leave the system in a state where the contents of + ** the wal-index header do not match the contents of the + ** file-system. To avoid this, update the wal-index header to + ** indicate that the log file contains zero valid frames. */ + walRestartHdr(pWal, salt1); + rc = sqlite3OsTruncate(pWal->pWalFd, 0); + } + walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); + } + } + } + + walcheckpoint_out: + walIteratorFree(pIter); + return rc; +} + +/* +** If the WAL file is currently larger than nMax bytes in size, truncate +** it to exactly nMax bytes. If an error occurs while doing so, ignore it. +*/ +static void walLimitSize(Wal *pWal, i64 nMax){ + i64 sz; + int rx; + sqlite3BeginBenignMalloc(); + rx = sqlite3OsFileSize(pWal->pWalFd, &sz); + if( rx==SQLITE_OK && (sz > nMax ) ){ + rx = sqlite3OsTruncate(pWal->pWalFd, nMax); + } + sqlite3EndBenignMalloc(); + if( rx ){ + sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName); + } +} + +/* +** Close a connection to a log file. +*/ +SQLITE_PRIVATE int sqlite3WalClose( + Wal *pWal, /* Wal to close */ + int sync_flags, /* Flags to pass to OsSync() (or 0) */ + int nBuf, + u8 *zBuf /* Buffer of at least nBuf bytes */ +){ + int rc = SQLITE_OK; + if( pWal ){ + int isDelete = 0; /* True to unlink wal and wal-index files */ + + /* If an EXCLUSIVE lock can be obtained on the database file (using the + ** ordinary, rollback-mode locking methods, this guarantees that the + ** connection associated with this log file is the only connection to + ** the database. In this case checkpoint the database and unlink both + ** the wal and wal-index files. + ** + ** The EXCLUSIVE lock is not released before returning. + */ + rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE); + if( rc==SQLITE_OK ){ + if( pWal->exclusiveMode==WAL_NORMAL_MODE ){ + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; + } + rc = sqlite3WalCheckpoint( + pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 + ); + if( rc==SQLITE_OK ){ + int bPersist = -1; + sqlite3OsFileControlHint( + pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist + ); + if( bPersist!=1 ){ + /* Try to delete the WAL file if the checkpoint completed and + ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal + ** mode (!bPersist) */ + isDelete = 1; + }else if( pWal->mxWalSize>=0 ){ + /* Try to truncate the WAL file to zero bytes if the checkpoint + ** completed and fsynced (rc==SQLITE_OK) and we are in persistent + ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a + ** non-negative value (pWal->mxWalSize>=0). Note that we truncate + ** to zero bytes as truncating to the journal_size_limit might + ** leave a corrupt WAL file on disk. */ + walLimitSize(pWal, 0); + } + } + } + + walIndexClose(pWal, isDelete); + sqlite3OsClose(pWal->pWalFd); + if( isDelete ){ + sqlite3BeginBenignMalloc(); + sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0); + sqlite3EndBenignMalloc(); + } + WALTRACE(("WAL%p: closed\n", pWal)); + sqlite3_free((void *)pWal->apWiData); + sqlite3_free(pWal); + } + return rc; +} + +/* +** Try to read the wal-index header. Return 0 on success and 1 if +** there is a problem. +** +** The wal-index is in shared memory. Another thread or process might +** be writing the header at the same time this procedure is trying to +** read it, which might result in inconsistency. A dirty read is detected +** by verifying that both copies of the header are the same and also by +** a checksum on the header. +** +** If and only if the read is consistent and the header is different from +** pWal->hdr, then pWal->hdr is updated to the content of the new header +** and *pChanged is set to 1. +** +** If the checksum cannot be verified return non-zero. If the header +** is read successfully and the checksum verified, return zero. +*/ +static int walIndexTryHdr(Wal *pWal, int *pChanged){ + u32 aCksum[2]; /* Checksum on the header content */ + WalIndexHdr h1, h2; /* Two copies of the header content */ + WalIndexHdr volatile *aHdr; /* Header in shared memory */ + + /* The first page of the wal-index must be mapped at this point. */ + assert( pWal->nWiData>0 && pWal->apWiData[0] ); + + /* Read the header. This might happen concurrently with a write to the + ** same area of shared memory on a different CPU in a SMP, + ** meaning it is possible that an inconsistent snapshot is read + ** from the file. If this happens, return non-zero. + ** + ** There are two copies of the header at the beginning of the wal-index. + ** When reading, read [0] first then [1]. Writes are in the reverse order. + ** Memory barriers are used to prevent the compiler or the hardware from + ** reordering the reads and writes. + */ + aHdr = walIndexHdr(pWal); + memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); + walShmBarrier(pWal); + memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); + + if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ + return 1; /* Dirty read */ + } + if( h1.isInit==0 ){ + return 1; /* Malformed header - probably all zeros */ + } + walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum); + if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){ + return 1; /* Checksum does not match */ + } + + if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){ + *pChanged = 1; + memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr)); + pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); + testcase( pWal->szPage<=32768 ); + testcase( pWal->szPage>=65536 ); + } + + /* The header was successfully read. Return zero. */ + return 0; +} + +/* +** Read the wal-index header from the wal-index and into pWal->hdr. +** If the wal-header appears to be corrupt, try to reconstruct the +** wal-index from the WAL before returning. +** +** Set *pChanged to 1 if the wal-index header value in pWal->hdr is +** changed by this operation. If pWal->hdr is unchanged, set *pChanged +** to 0. +** +** If the wal-index header is successfully read, return SQLITE_OK. +** Otherwise an SQLite error code. +*/ +static int walIndexReadHdr(Wal *pWal, int *pChanged){ + int rc; /* Return code */ + int badHdr; /* True if a header read failed */ + volatile u32 *page0; /* Chunk of wal-index containing header */ + + /* Ensure that page 0 of the wal-index (the page that contains the + ** wal-index header) is mapped. Return early if an error occurs here. + */ + assert( pChanged ); + rc = walIndexPage(pWal, 0, &page0); + if( rc!=SQLITE_OK ){ + return rc; + }; + assert( page0 || pWal->writeLock==0 ); + + /* If the first page of the wal-index has been mapped, try to read the + ** wal-index header immediately, without holding any lock. This usually + ** works, but may fail if the wal-index header is corrupt or currently + ** being modified by another thread or process. + */ + badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); + + /* If the first attempt failed, it might have been due to a race + ** with a writer. So get a WRITE lock and try again. + */ + assert( badHdr==0 || pWal->writeLock==0 ); + if( badHdr ){ + if( pWal->readOnly & WAL_SHM_RDONLY ){ + if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ + walUnlockShared(pWal, WAL_WRITE_LOCK); + rc = SQLITE_READONLY_RECOVERY; + } + }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ + pWal->writeLock = 1; + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ + badHdr = walIndexTryHdr(pWal, pChanged); + if( badHdr ){ + /* If the wal-index header is still malformed even while holding + ** a WRITE lock, it can only mean that the header is corrupted and + ** needs to be reconstructed. So run recovery to do exactly that. + */ + rc = walIndexRecover(pWal); + *pChanged = 1; + } + } + pWal->writeLock = 0; + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + } + } + + /* If the header is read successfully, check the version number to make + ** sure the wal-index was not constructed with some future format that + ** this version of SQLite cannot understand. + */ + if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){ + rc = SQLITE_CANTOPEN_BKPT; + } + + return rc; +} + +/* +** This is the value that walTryBeginRead returns when it needs to +** be retried. +*/ +#define WAL_RETRY (-1) + +/* +** Attempt to start a read transaction. This might fail due to a race or +** other transient condition. When that happens, it returns WAL_RETRY to +** indicate to the caller that it is safe to retry immediately. +** +** On success return SQLITE_OK. On a permanent failure (such an +** I/O error or an SQLITE_BUSY because another process is running +** recovery) return a positive error code. +** +** The useWal parameter is true to force the use of the WAL and disable +** the case where the WAL is bypassed because it has been completely +** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() +** to make a copy of the wal-index header into pWal->hdr. If the +** wal-index header has changed, *pChanged is set to 1 (as an indication +** to the caller that the local paget cache is obsolete and needs to be +** flushed.) When useWal==1, the wal-index header is assumed to already +** be loaded and the pChanged parameter is unused. +** +** The caller must set the cnt parameter to the number of prior calls to +** this routine during the current read attempt that returned WAL_RETRY. +** This routine will start taking more aggressive measures to clear the +** race conditions after multiple WAL_RETRY returns, and after an excessive +** number of errors will ultimately return SQLITE_PROTOCOL. The +** SQLITE_PROTOCOL return indicates that some other process has gone rogue +** and is not honoring the locking protocol. There is a vanishingly small +** chance that SQLITE_PROTOCOL could be returned because of a run of really +** bad luck when there is lots of contention for the wal-index, but that +** possibility is so small that it can be safely neglected, we believe. +** +** On success, this routine obtains a read lock on +** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is +** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1) +** that means the Wal does not hold any read lock. The reader must not +** access any database page that is modified by a WAL frame up to and +** including frame number aReadMark[pWal->readLock]. The reader will +** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0 +** Or if pWal->readLock==0, then the reader will ignore the WAL +** completely and get all content directly from the database file. +** If the useWal parameter is 1 then the WAL will never be ignored and +** this routine will always set pWal->readLock>0 on success. +** When the read transaction is completed, the caller must release the +** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1. +** +** This routine uses the nBackfill and aReadMark[] fields of the header +** to select a particular WAL_READ_LOCK() that strives to let the +** checkpoint process do as much work as possible. This routine might +** update values of the aReadMark[] array in the header, but if it does +** so it takes care to hold an exclusive lock on the corresponding +** WAL_READ_LOCK() while changing values. +*/ +static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ + u32 mxReadMark; /* Largest aReadMark[] value */ + int mxI; /* Index of largest aReadMark[] value */ + int i; /* Loop counter */ + int rc = SQLITE_OK; /* Return code */ + u32 mxFrame; /* Wal frame to lock to */ + + assert( pWal->readLock<0 ); /* Not currently locked */ + + /* Take steps to avoid spinning forever if there is a protocol error. + ** + ** Circumstances that cause a RETRY should only last for the briefest + ** instances of time. No I/O or other system calls are done while the + ** locks are held, so the locks should not be held for very long. But + ** if we are unlucky, another process that is holding a lock might get + ** paged out or take a page-fault that is time-consuming to resolve, + ** during the few nanoseconds that it is holding the lock. In that case, + ** it might take longer than normal for the lock to free. + ** + ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few + ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this + ** is more of a scheduler yield than an actual delay. But on the 10th + ** an subsequent retries, the delays start becoming longer and longer, + ** so that on the 100th (and last) RETRY we delay for 323 milliseconds. + ** The total delay time before giving up is less than 10 seconds. + */ + if( cnt>5 ){ + int nDelay = 1; /* Pause time in microseconds */ + if( cnt>100 ){ + VVA_ONLY( pWal->lockError = 1; ) + return SQLITE_PROTOCOL; + } + if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; + sqlite3OsSleep(pWal->pVfs, nDelay); + } + + if( !useWal ){ + rc = walIndexReadHdr(pWal, pChanged); + if( rc==SQLITE_BUSY ){ + /* If there is not a recovery running in another thread or process + ** then convert BUSY errors to WAL_RETRY. If recovery is known to + ** be running, convert BUSY to BUSY_RECOVERY. There is a race here + ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY + ** would be technically correct. But the race is benign since with + ** WAL_RETRY this routine will be called again and will probably be + ** right on the second iteration. + */ + if( pWal->apWiData[0]==0 ){ + /* This branch is taken when the xShmMap() method returns SQLITE_BUSY. + ** We assume this is a transient condition, so return WAL_RETRY. The + ** xShmMap() implementation used by the default unix and win32 VFS + ** modules may return SQLITE_BUSY due to a race condition in the + ** code that determines whether or not the shared-memory region + ** must be zeroed before the requested page is returned. + */ + rc = WAL_RETRY; + }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){ + walUnlockShared(pWal, WAL_RECOVER_LOCK); + rc = WAL_RETRY; + }else if( rc==SQLITE_BUSY ){ + rc = SQLITE_BUSY_RECOVERY; + } + } + if( rc!=SQLITE_OK ){ + return rc; + } + } + + pInfo = walCkptInfo(pWal); + if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame +#ifdef SQLITE_ENABLE_SNAPSHOT + && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 + || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) +#endif + ){ + /* The WAL has been completely backfilled (or it is empty). + ** and can be safely ignored. + */ + rc = walLockShared(pWal, WAL_READ_LOCK(0)); + walShmBarrier(pWal); + if( rc==SQLITE_OK ){ + if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ + /* It is not safe to allow the reader to continue here if frames + ** may have been appended to the log before READ_LOCK(0) was obtained. + ** When holding READ_LOCK(0), the reader ignores the entire log file, + ** which implies that the database file contains a trustworthy + ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from + ** happening, this is usually correct. + ** + ** However, if frames have been appended to the log (or if the log + ** is wrapped and written for that matter) before the READ_LOCK(0) + ** is obtained, that is not necessarily true. A checkpointer may + ** have started to backfill the appended frames but crashed before + ** it finished. Leaving a corrupt image in the database file. + */ + walUnlockShared(pWal, WAL_READ_LOCK(0)); + return WAL_RETRY; + } + pWal->readLock = 0; + return SQLITE_OK; + }else if( rc!=SQLITE_BUSY ){ + return rc; + } + } + + /* If we get this far, it means that the reader will want to use + ** the WAL to get at content from recent commits. The job now is + ** to select one of the aReadMark[] entries that is closest to + ** but not exceeding pWal->hdr.mxFrame and lock that entry. + */ + mxReadMark = 0; + mxI = 0; + mxFrame = pWal->hdr.mxFrame; +#ifdef SQLITE_ENABLE_SNAPSHOT + if( pWal->pSnapshot && pWal->pSnapshot->mxFramepSnapshot->mxFrame; + } +#endif + for(i=1; iaReadMark[i]; + if( mxReadMark<=thisMark && thisMark<=mxFrame ){ + assert( thisMark!=READMARK_NOT_USED ); + mxReadMark = thisMark; + mxI = i; + } + } + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 + && (mxReadMarkaReadMark[i] = mxFrame; + mxI = i; + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + break; + }else if( rc!=SQLITE_BUSY ){ + return rc; + } + } + } + if( mxI==0 ){ + assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); + return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; + } + + rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); + if( rc ){ + return rc==SQLITE_BUSY ? WAL_RETRY : rc; + } + /* Now that the read-lock has been obtained, check that neither the + ** value in the aReadMark[] array or the contents of the wal-index + ** header have changed. + ** + ** It is necessary to check that the wal-index header did not change + ** between the time it was read and when the shared-lock was obtained + ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility + ** that the log file may have been wrapped by a writer, or that frames + ** that occur later in the log than pWal->hdr.mxFrame may have been + ** copied into the database by a checkpointer. If either of these things + ** happened, then reading the database with the current value of + ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry + ** instead. + ** + ** Before checking that the live wal-index header has not changed + ** since it was read, set Wal.minFrame to the first frame in the wal + ** file that has not yet been checkpointed. This client will not need + ** to read any frames earlier than minFrame from the wal file - they + ** can be safely read directly from the database file. + ** + ** Because a ShmBarrier() call is made between taking the copy of + ** nBackfill and checking that the wal-header in shared-memory still + ** matches the one cached in pWal->hdr, it is guaranteed that the + ** checkpointer that set nBackfill was not working with a wal-index + ** header newer than that cached in pWal->hdr. If it were, that could + ** cause a problem. The checkpointer could omit to checkpoint + ** a version of page X that lies before pWal->minFrame (call that version + ** A) on the basis that there is a newer version (version B) of the same + ** page later in the wal file. But if version B happens to like past + ** frame pWal->hdr.mxFrame - then the client would incorrectly assume + ** that it can read version A from the database file. However, since + ** we can guarantee that the checkpointer that set nBackfill could not + ** see any pages past pWal->hdr.mxFrame, this problem does not come up. + */ + pWal->minFrame = pInfo->nBackfill+1; + walShmBarrier(pWal); + if( pInfo->aReadMark[mxI]!=mxReadMark + || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) + ){ + walUnlockShared(pWal, WAL_READ_LOCK(mxI)); + return WAL_RETRY; + }else{ + assert( mxReadMark<=pWal->hdr.mxFrame ); + pWal->readLock = (i16)mxI; + } + return rc; +} + +/* +** Begin a read transaction on the database. +** +** This routine used to be called sqlite3OpenSnapshot() and with good reason: +** it takes a snapshot of the state of the WAL and wal-index for the current +** instant in time. The current thread will continue to use this snapshot. +** Other threads might append new content to the WAL and wal-index but +** that extra content is ignored by the current thread. +** +** If the database contents have changes since the previous read +** transaction, then *pChanged is set to 1 before returning. The +** Pager layer will use this to know that is cache is stale and +** needs to be flushed. +*/ +SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ + int rc; /* Return code */ + int cnt = 0; /* Number of TryBeginRead attempts */ + +#ifdef SQLITE_ENABLE_SNAPSHOT + int bChanged = 0; + WalIndexHdr *pSnapshot = pWal->pSnapshot; + if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ + bChanged = 1; + } +#endif + + do{ + rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); + }while( rc==WAL_RETRY ); + testcase( (rc&0xff)==SQLITE_BUSY ); + testcase( (rc&0xff)==SQLITE_IOERR ); + testcase( rc==SQLITE_PROTOCOL ); + testcase( rc==SQLITE_OK ); + +#ifdef SQLITE_ENABLE_SNAPSHOT + if( rc==SQLITE_OK ){ + if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ + /* At this point the client has a lock on an aReadMark[] slot holding + ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr + ** is populated with the wal-index header corresponding to the head + ** of the wal file. Verify that pSnapshot is still valid before + ** continuing. Reasons why pSnapshot might no longer be valid: + ** + ** (1) The WAL file has been reset since the snapshot was taken. + ** In this case, the salt will have changed. + ** + ** (2) A checkpoint as been attempted that wrote frames past + ** pSnapshot->mxFrame into the database file. Note that the + ** checkpoint need not have completed for this to cause problems. + */ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + + assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); + assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); + + /* It is possible that there is a checkpointer thread running + ** concurrent with this code. If this is the case, it may be that the + ** checkpointer has already determined that it will checkpoint + ** snapshot X, where X is later in the wal file than pSnapshot, but + ** has not yet set the pInfo->nBackfillAttempted variable to indicate + ** its intent. To avoid the race condition this leads to, ensure that + ** there is no checkpointer process by taking a shared CKPT lock + ** before checking pInfo->nBackfillAttempted. */ + rc = walLockShared(pWal, WAL_CKPT_LOCK); + + if( rc==SQLITE_OK ){ + /* Check that the wal file has not been wrapped. Assuming that it has + ** not, also check that no checkpointer has attempted to checkpoint any + ** frames beyond pSnapshot->mxFrame. If either of these conditions are + ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr + ** with *pSnapshot and set *pChanged as appropriate for opening the + ** snapshot. */ + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted + ){ + assert( pWal->readLock>0 ); + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); + *pChanged = bChanged; + }else{ + rc = SQLITE_BUSY_SNAPSHOT; + } + + /* Release the shared CKPT lock obtained above. */ + walUnlockShared(pWal, WAL_CKPT_LOCK); + } + + + if( rc!=SQLITE_OK ){ + sqlite3WalEndReadTransaction(pWal); + } + } + } +#endif + return rc; +} + +/* +** Finish with a read transaction. All this does is release the +** read-lock. +*/ +SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){ + sqlite3WalEndWriteTransaction(pWal); + if( pWal->readLock>=0 ){ + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + pWal->readLock = -1; + } +} + +/* +** Search the wal file for page pgno. If found, set *piRead to the frame that +** contains the page. Otherwise, if pgno is not in the wal file, set *piRead +** to zero. +** +** Return SQLITE_OK if successful, or an error code if an error occurs. If an +** error does occur, the final value of *piRead is undefined. +*/ +SQLITE_PRIVATE int sqlite3WalFindFrame( + Wal *pWal, /* WAL handle */ + Pgno pgno, /* Database page number to read data for */ + u32 *piRead /* OUT: Frame number (or zero) */ +){ + u32 iRead = 0; /* If !=0, WAL frame to return data from */ + u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */ + int iHash; /* Used to loop through N hash tables */ + int iMinHash; + + /* This routine is only be called from within a read transaction. */ + assert( pWal->readLock>=0 || pWal->lockError ); + + /* If the "last page" field of the wal-index header snapshot is 0, then + ** no data will be read from the wal under any circumstances. Return early + ** in this case as an optimization. Likewise, if pWal->readLock==0, + ** then the WAL is ignored by the reader so return early, as if the + ** WAL were empty. + */ + if( iLast==0 || pWal->readLock==0 ){ + *piRead = 0; + return SQLITE_OK; + } + + /* Search the hash table or tables for an entry matching page number + ** pgno. Each iteration of the following for() loop searches one + ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). + ** + ** This code might run concurrently to the code in walIndexAppend() + ** that adds entries to the wal-index (and possibly to this hash + ** table). This means the value just read from the hash + ** slot (aHash[iKey]) may have been added before or after the + ** current read transaction was opened. Values added after the + ** read transaction was opened may have been written incorrectly - + ** i.e. these slots may contain garbage data. However, we assume + ** that any slots written before the current read transaction was + ** opened remain unmodified. + ** + ** For the reasons above, the if(...) condition featured in the inner + ** loop of the following block is more stringent that would be required + ** if we had exclusive access to the hash-table: + ** + ** (aPgno[iFrame]==pgno): + ** This condition filters out normal hash-table collisions. + ** + ** (iFrame<=iLast): + ** This condition filters out entries that were added to the hash + ** table after the current read-transaction had started. + */ + iMinHash = walFramePage(pWal->minFrame); + for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){ + volatile ht_slot *aHash; /* Pointer to hash table */ + volatile u32 *aPgno; /* Pointer to array of page numbers */ + u32 iZero; /* Frame number corresponding to aPgno[0] */ + int iKey; /* Hash slot index */ + int nCollide; /* Number of hash collisions remaining */ + int rc; /* Error code */ + + rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); + if( rc!=SQLITE_OK ){ + return rc; + } + nCollide = HASHTABLE_NSLOT; + for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ + u32 iFrame = aHash[iKey] + iZero; + if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){ + assert( iFrame>iRead || CORRUPT_DB ); + iRead = iFrame; + } + if( (nCollide--)==0 ){ + return SQLITE_CORRUPT_BKPT; + } + } + } + +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT + /* If expensive assert() statements are available, do a linear search + ** of the wal-index file content. Make sure the results agree with the + ** result obtained using the hash indexes above. */ + { + u32 iRead2 = 0; + u32 iTest; + assert( pWal->minFrame>0 ); + for(iTest=iLast; iTest>=pWal->minFrame; iTest--){ + if( walFramePgno(pWal, iTest)==pgno ){ + iRead2 = iTest; + break; + } + } + assert( iRead==iRead2 ); + } +#endif + + *piRead = iRead; + return SQLITE_OK; +} + +/* +** Read the contents of frame iRead from the wal file into buffer pOut +** (which is nOut bytes in size). Return SQLITE_OK if successful, or an +** error code otherwise. +*/ +SQLITE_PRIVATE int sqlite3WalReadFrame( + Wal *pWal, /* WAL handle */ + u32 iRead, /* Frame to read */ + int nOut, /* Size of buffer pOut in bytes */ + u8 *pOut /* Buffer to write page data to */ +){ + int sz; + i64 iOffset; + sz = pWal->hdr.szPage; + sz = (sz&0xfe00) + ((sz&0x0001)<<16); + testcase( sz<=32768 ); + testcase( sz>=65536 ); + iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE; + /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ + return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset); +} + +/* +** Return the size of the database in pages (or zero, if unknown). +*/ +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){ + if( pWal && ALWAYS(pWal->readLock>=0) ){ + return pWal->hdr.nPage; + } + return 0; +} + + +/* +** This function starts a write transaction on the WAL. +** +** A read transaction must have already been started by a prior call +** to sqlite3WalBeginReadTransaction(). +** +** If another thread or process has written into the database since +** the read transaction was started, then it is not possible for this +** thread to write as doing so would cause a fork. So this routine +** returns SQLITE_BUSY in that case and no write transaction is started. +** +** There can only be a single writer active at a time. +*/ +SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ + int rc; + + /* Cannot start a write transaction without first holding a read + ** transaction. */ + assert( pWal->readLock>=0 ); + assert( pWal->writeLock==0 && pWal->iReCksum==0 ); + + if( pWal->readOnly ){ + return SQLITE_READONLY; + } + + /* Only one writer allowed at a time. Get the write lock. Return + ** SQLITE_BUSY if unable. + */ + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); + if( rc ){ + return rc; + } + pWal->writeLock = 1; + + /* If another connection has written to the database file since the + ** time the read transaction on this connection was started, then + ** the write is disallowed. + */ + if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){ + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + pWal->writeLock = 0; + rc = SQLITE_BUSY_SNAPSHOT; + } + + return rc; +} + +/* +** End a write transaction. The commit has already been done. This +** routine merely releases the lock. +*/ +SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){ + if( pWal->writeLock ){ + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + pWal->writeLock = 0; + pWal->iReCksum = 0; + pWal->truncateOnCommit = 0; + } + return SQLITE_OK; +} + +/* +** If any data has been written (but not committed) to the log file, this +** function moves the write-pointer back to the start of the transaction. +** +** Additionally, the callback function is invoked for each frame written +** to the WAL since the start of the transaction. If the callback returns +** other than SQLITE_OK, it is not invoked again and the error code is +** returned to the caller. +** +** Otherwise, if the callback function does not return an error, this +** function returns SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ + int rc = SQLITE_OK; + if( ALWAYS(pWal->writeLock) ){ + Pgno iMax = pWal->hdr.mxFrame; + Pgno iFrame; + + /* Restore the clients cache of the wal-index header to the state it + ** was in before the client began writing to the database. + */ + memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); + + for(iFrame=pWal->hdr.mxFrame+1; + ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; + iFrame++ + ){ + /* This call cannot fail. Unless the page for which the page number + ** is passed as the second argument is (a) in the cache and + ** (b) has an outstanding reference, then xUndo is either a no-op + ** (if (a) is false) or simply expels the page from the cache (if (b) + ** is false). + ** + ** If the upper layer is doing a rollback, it is guaranteed that there + ** are no outstanding references to any page other than page 1. And + ** page 1 is never written to the log until the transaction is + ** committed. As a result, the call to xUndo may not fail. + */ + assert( walFramePgno(pWal, iFrame)!=1 ); + rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); + } + if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); + } + return rc; +} + +/* +** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 +** values. This function populates the array with values required to +** "rollback" the write position of the WAL handle back to the current +** point in the event of a savepoint rollback (via WalSavepointUndo()). +*/ +SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ + assert( pWal->writeLock ); + aWalData[0] = pWal->hdr.mxFrame; + aWalData[1] = pWal->hdr.aFrameCksum[0]; + aWalData[2] = pWal->hdr.aFrameCksum[1]; + aWalData[3] = pWal->nCkpt; +} + +/* +** Move the write position of the WAL back to the point identified by +** the values in the aWalData[] array. aWalData must point to an array +** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated +** by a call to WalSavepoint(). +*/ +SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ + int rc = SQLITE_OK; + + assert( pWal->writeLock ); + assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame ); + + if( aWalData[3]!=pWal->nCkpt ){ + /* This savepoint was opened immediately after the write-transaction + ** was started. Right after that, the writer decided to wrap around + ** to the start of the log. Update the savepoint values to match. + */ + aWalData[0] = 0; + aWalData[3] = pWal->nCkpt; + } + + if( aWalData[0]hdr.mxFrame ){ + pWal->hdr.mxFrame = aWalData[0]; + pWal->hdr.aFrameCksum[0] = aWalData[1]; + pWal->hdr.aFrameCksum[1] = aWalData[2]; + walCleanupHash(pWal); + } + + return rc; +} + +/* +** This function is called just before writing a set of frames to the log +** file (see sqlite3WalFrames()). It checks to see if, instead of appending +** to the current log file, it is possible to overwrite the start of the +** existing log file with the new frames (i.e. "reset" the log). If so, +** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left +** unchanged. +** +** SQLITE_OK is returned if no error is encountered (regardless of whether +** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned +** if an error occurs. +*/ +static int walRestartLog(Wal *pWal){ + int rc = SQLITE_OK; + int cnt; + + if( pWal->readLock==0 ){ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + assert( pInfo->nBackfill==pWal->hdr.mxFrame ); + if( pInfo->nBackfill>0 ){ + u32 salt1; + sqlite3_randomness(4, &salt1); + rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); + if( rc==SQLITE_OK ){ + /* If all readers are using WAL_READ_LOCK(0) (in other words if no + ** readers are currently using the WAL), then the transactions + ** frames will overwrite the start of the existing log. Update the + ** wal-index header to reflect this. + ** + ** In theory it would be Ok to update the cache of the header only + ** at this point. But updating the actual wal-index header is also + ** safe and means there is no special case for sqlite3WalUndo() + ** to handle if this transaction is rolled back. */ + walRestartHdr(pWal, salt1); + walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); + }else if( rc!=SQLITE_BUSY ){ + return rc; + } + } + walUnlockShared(pWal, WAL_READ_LOCK(0)); + pWal->readLock = -1; + cnt = 0; + do{ + int notUsed; + rc = walTryBeginRead(pWal, ¬Used, 1, ++cnt); + }while( rc==WAL_RETRY ); + assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */ + testcase( (rc&0xff)==SQLITE_IOERR ); + testcase( rc==SQLITE_PROTOCOL ); + testcase( rc==SQLITE_OK ); + } + return rc; +} + +/* +** Information about the current state of the WAL file and where +** the next fsync should occur - passed from sqlite3WalFrames() into +** walWriteToLog(). +*/ +typedef struct WalWriter { + Wal *pWal; /* The complete WAL information */ + sqlite3_file *pFd; /* The WAL file to which we write */ + sqlite3_int64 iSyncPoint; /* Fsync at this offset */ + int syncFlags; /* Flags for the fsync */ + int szPage; /* Size of one page */ +} WalWriter; + +/* +** Write iAmt bytes of content into the WAL file beginning at iOffset. +** Do a sync when crossing the p->iSyncPoint boundary. +** +** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt, +** first write the part before iSyncPoint, then sync, then write the +** rest. +*/ +static int walWriteToLog( + WalWriter *p, /* WAL to write to */ + void *pContent, /* Content to be written */ + int iAmt, /* Number of bytes to write */ + sqlite3_int64 iOffset /* Start writing at this offset */ +){ + int rc; + if( iOffsetiSyncPoint && iOffset+iAmt>=p->iSyncPoint ){ + int iFirstAmt = (int)(p->iSyncPoint - iOffset); + rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset); + if( rc ) return rc; + iOffset += iFirstAmt; + iAmt -= iFirstAmt; + pContent = (void*)(iFirstAmt + (char*)pContent); + assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) ); + rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK); + if( iAmt==0 || rc ) return rc; + } + rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset); + return rc; +} + +/* +** Write out a single frame of the WAL +*/ +static int walWriteOneFrame( + WalWriter *p, /* Where to write the frame */ + PgHdr *pPage, /* The page of the frame to be written */ + int nTruncate, /* The commit flag. Usually 0. >0 for commit */ + sqlite3_int64 iOffset /* Byte offset at which to write */ +){ + int rc; /* Result code from subfunctions */ + void *pData; /* Data actually written */ + u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ +#if defined(SQLITE_HAS_CODEC) + if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; +#else + pData = pPage->pData; +#endif + walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame); + rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset); + if( rc ) return rc; + /* Write the page data */ + rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame)); + return rc; +} + +/* +** This function is called as part of committing a transaction within which +** one or more frames have been overwritten. It updates the checksums for +** all frames written to the wal file by the current transaction starting +** with the earliest to have been overwritten. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +static int walRewriteChecksums(Wal *pWal, u32 iLast){ + const int szPage = pWal->szPage;/* Database page size */ + int rc = SQLITE_OK; /* Return code */ + u8 *aBuf; /* Buffer to load data from wal file into */ + u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */ + u32 iRead; /* Next frame to read from wal file */ + i64 iCksumOff; + + aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE); + if( aBuf==0 ) return SQLITE_NOMEM_BKPT; + + /* Find the checksum values to use as input for the recalculating the + ** first checksum. If the first frame is frame 1 (implying that the current + ** transaction restarted the wal file), these values must be read from the + ** wal-file header. Otherwise, read them from the frame header of the + ** previous frame. */ + assert( pWal->iReCksum>0 ); + if( pWal->iReCksum==1 ){ + iCksumOff = 24; + }else{ + iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16; + } + rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff); + pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf); + pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]); + + iRead = pWal->iReCksum; + pWal->iReCksum = 0; + for(; rc==SQLITE_OK && iRead<=iLast; iRead++){ + i64 iOff = walFrameOffset(iRead, szPage); + rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); + if( rc==SQLITE_OK ){ + u32 iPgno, nDbSize; + iPgno = sqlite3Get4byte(aBuf); + nDbSize = sqlite3Get4byte(&aBuf[4]); + + walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame); + rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff); + } + } + + sqlite3_free(aBuf); + return rc; +} + +/* +** Write a set of frames to the log. The caller must hold the write-lock +** on the log file (obtained using sqlite3WalBeginWriteTransaction()). +*/ +SQLITE_PRIVATE int sqlite3WalFrames( + Wal *pWal, /* Wal handle to write to */ + int szPage, /* Database page-size in bytes */ + PgHdr *pList, /* List of dirty pages to write */ + Pgno nTruncate, /* Database size after this commit */ + int isCommit, /* True if this is a commit */ + int sync_flags /* Flags to pass to OsSync() (or 0) */ +){ + int rc; /* Used to catch return codes */ + u32 iFrame; /* Next frame address */ + PgHdr *p; /* Iterator to run through pList with. */ + PgHdr *pLast = 0; /* Last frame in list */ + int nExtra = 0; /* Number of extra copies of last page */ + int szFrame; /* The size of a single frame */ + i64 iOffset; /* Next byte to write in WAL file */ + WalWriter w; /* The writer */ + u32 iFirst = 0; /* First frame that may be overwritten */ + WalIndexHdr *pLive; /* Pointer to shared header */ + + assert( pList ); + assert( pWal->writeLock ); + + /* If this frame set completes a transaction, then nTruncate>0. If + ** nTruncate==0 then this frame set does not complete the transaction. */ + assert( (isCommit!=0)==(nTruncate!=0) ); + +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) + { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){} + WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n", + pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill")); + } +#endif + + pLive = (WalIndexHdr*)walIndexHdr(pWal); + if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){ + iFirst = pLive->mxFrame+1; + } + + /* See if it is possible to write these frames into the start of the + ** log file, instead of appending to it at pWal->hdr.mxFrame. + */ + if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){ + return rc; + } + + /* If this is the first frame written into the log, write the WAL + ** header to the start of the WAL file. See comments at the top of + ** this source file for a description of the WAL header format. + */ + iFrame = pWal->hdr.mxFrame; + if( iFrame==0 ){ + u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */ + u32 aCksum[2]; /* Checksum for wal-header */ + + sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN)); + sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION); + sqlite3Put4byte(&aWalHdr[8], szPage); + sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt); + if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt); + memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); + walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); + sqlite3Put4byte(&aWalHdr[24], aCksum[0]); + sqlite3Put4byte(&aWalHdr[28], aCksum[1]); + + pWal->szPage = szPage; + pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; + pWal->hdr.aFrameCksum[0] = aCksum[0]; + pWal->hdr.aFrameCksum[1] = aCksum[1]; + pWal->truncateOnCommit = 1; + + rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); + WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless + ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise + ** an out-of-order write following a WAL restart could result in + ** database corruption. See the ticket: + ** + ** http://localhost:591/sqlite/info/ff5be73dee + */ + if( pWal->syncHeader && sync_flags ){ + rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK); + if( rc ) return rc; + } + } + assert( (int)pWal->szPage==szPage ); + + /* Setup information needed to write frames into the WAL */ + w.pWal = pWal; + w.pFd = pWal->pWalFd; + w.iSyncPoint = 0; + w.syncFlags = sync_flags; + w.szPage = szPage; + iOffset = walFrameOffset(iFrame+1, szPage); + szFrame = szPage + WAL_FRAME_HDRSIZE; + + /* Write all frames into the log file exactly once */ + for(p=pList; p; p=p->pDirty){ + int nDbSize; /* 0 normally. Positive == commit flag */ + + /* Check if this page has already been written into the wal file by + ** the current transaction. If so, overwrite the existing frame and + ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that + ** checksums must be recomputed when the transaction is committed. */ + if( iFirst && (p->pDirty || isCommit==0) ){ + u32 iWrite = 0; + VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite); + assert( rc==SQLITE_OK || iWrite==0 ); + if( iWrite>=iFirst ){ + i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE; + void *pData; + if( pWal->iReCksum==0 || iWriteiReCksum ){ + pWal->iReCksum = iWrite; + } +#if defined(SQLITE_HAS_CODEC) + if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM; +#else + pData = p->pData; +#endif + rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff); + if( rc ) return rc; + p->flags &= ~PGHDR_WAL_APPEND; + continue; + } + } + + iFrame++; + assert( iOffset==walFrameOffset(iFrame, szPage) ); + nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0; + rc = walWriteOneFrame(&w, p, nDbSize, iOffset); + if( rc ) return rc; + pLast = p; + iOffset += szFrame; + p->flags |= PGHDR_WAL_APPEND; + } + + /* Recalculate checksums within the wal file if required. */ + if( isCommit && pWal->iReCksum ){ + rc = walRewriteChecksums(pWal, iFrame); + if( rc ) return rc; + } + + /* If this is the end of a transaction, then we might need to pad + ** the transaction and/or sync the WAL file. + ** + ** Padding and syncing only occur if this set of frames complete a + ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL + ** or synchronous==OFF, then no padding or syncing are needed. + ** + ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not + ** needed and only the sync is done. If padding is needed, then the + ** final frame is repeated (with its commit mark) until the next sector + ** boundary is crossed. Only the part of the WAL prior to the last + ** sector boundary is synced; the part of the last frame that extends + ** past the sector boundary is written after the sync. + */ + if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){ + if( pWal->padToSectorBoundary ){ + int sectorSize = sqlite3SectorSize(pWal->pWalFd); + w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize; + while( iOffsettruncateOnCommit && pWal->mxWalSize>=0 ){ + i64 sz = pWal->mxWalSize; + if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){ + sz = walFrameOffset(iFrame+nExtra+1, szPage); + } + walLimitSize(pWal, sz); + pWal->truncateOnCommit = 0; + } + + /* Append data to the wal-index. It is not necessary to lock the + ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index + ** guarantees that there are no other writers, and no data that may + ** be in use by existing readers is being overwritten. + */ + iFrame = pWal->hdr.mxFrame; + for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){ + if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue; + iFrame++; + rc = walIndexAppend(pWal, iFrame, p->pgno); + } + while( rc==SQLITE_OK && nExtra>0 ){ + iFrame++; + nExtra--; + rc = walIndexAppend(pWal, iFrame, pLast->pgno); + } + + if( rc==SQLITE_OK ){ + /* Update the private copy of the header. */ + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); + testcase( szPage<=32768 ); + testcase( szPage>=65536 ); + pWal->hdr.mxFrame = iFrame; + if( isCommit ){ + pWal->hdr.iChange++; + pWal->hdr.nPage = nTruncate; + } + /* If this is a commit, update the wal-index header too. */ + if( isCommit ){ + walIndexWriteHdr(pWal); + pWal->iCallback = iFrame; + } + } + + WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok")); + return rc; +} + +/* +** This routine is called to implement sqlite3_wal_checkpoint() and +** related interfaces. +** +** Obtain a CHECKPOINT lock and then backfill as much information as +** we can from WAL into the database. +** +** If parameter xBusy is not NULL, it is a pointer to a busy-handler +** callback. In this case this function runs a blocking checkpoint. +*/ +SQLITE_PRIVATE int sqlite3WalCheckpoint( + Wal *pWal, /* Wal connection */ + int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ + int sync_flags, /* Flags to sync db file with (or 0) */ + int nBuf, /* Size of temporary buffer */ + u8 *zBuf, /* Temporary buffer to use */ + int *pnLog, /* OUT: Number of frames in WAL */ + int *pnCkpt /* OUT: Number of backfilled frames in WAL */ +){ + int rc; /* Return code */ + int isChanged = 0; /* True if a new wal-index header is loaded */ + int eMode2 = eMode; /* Mode to pass to walCheckpoint() */ + int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */ + + assert( pWal->ckptLock==0 ); + assert( pWal->writeLock==0 ); + + /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked + ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ + assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); + + if( pWal->readOnly ) return SQLITE_READONLY; + WALTRACE(("WAL%p: checkpoint begins\n", pWal)); + + /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive + ** "checkpoint" lock on the database file. */ + rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); + if( rc ){ + /* EVIDENCE-OF: R-10421-19736 If any other process is running a + ** checkpoint operation at the same time, the lock cannot be obtained and + ** SQLITE_BUSY is returned. + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, + ** it will not be invoked in this case. + */ + testcase( rc==SQLITE_BUSY ); + testcase( xBusy!=0 ); + return rc; + } + pWal->ckptLock = 1; + + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and + ** TRUNCATE modes also obtain the exclusive "writer" lock on the database + ** file. + ** + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained + ** immediately, and a busy-handler is configured, it is invoked and the + ** writer lock retried until either the busy-handler returns 0 or the + ** lock is successfully obtained. + */ + if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ + rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1); + if( rc==SQLITE_OK ){ + pWal->writeLock = 1; + }else if( rc==SQLITE_BUSY ){ + eMode2 = SQLITE_CHECKPOINT_PASSIVE; + xBusy2 = 0; + rc = SQLITE_OK; + } + } + + /* Read the wal-index header. */ + if( rc==SQLITE_OK ){ + rc = walIndexReadHdr(pWal, &isChanged); + if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ + sqlite3OsUnfetch(pWal->pDbFd, 0, 0); + } + } + + /* Copy data from the log to the database file. */ + if( rc==SQLITE_OK ){ + + if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); + } + + /* If no error occurred, set the output variables. */ + if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ + if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame; + if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill); + } + } + + if( isChanged ){ + /* If a new wal-index header was loaded before the checkpoint was + ** performed, then the pager-cache associated with pWal is now + ** out of date. So zero the cached wal-index header to ensure that + ** next time the pager opens a snapshot on this database it knows that + ** the cache needs to be reset. + */ + memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); + } + + /* Release the locks. */ + sqlite3WalEndWriteTransaction(pWal); + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); + pWal->ckptLock = 0; + WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); + return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); +} + +/* Return the value to pass to a sqlite3_wal_hook callback, the +** number of frames in the WAL at the point of the last commit since +** sqlite3WalCallback() was called. If no commits have occurred since +** the last call, then return 0. +*/ +SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){ + u32 ret = 0; + if( pWal ){ + ret = pWal->iCallback; + pWal->iCallback = 0; + } + return (int)ret; +} + +/* +** This function is called to change the WAL subsystem into or out +** of locking_mode=EXCLUSIVE. +** +** If op is zero, then attempt to change from locking_mode=EXCLUSIVE +** into locking_mode=NORMAL. This means that we must acquire a lock +** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL +** or if the acquisition of the lock fails, then return 0. If the +** transition out of exclusive-mode is successful, return 1. This +** operation must occur while the pager is still holding the exclusive +** lock on the main database file. +** +** If op is one, then change from locking_mode=NORMAL into +** locking_mode=EXCLUSIVE. This means that the pWal->readLock must +** be released. Return 1 if the transition is made and 0 if the +** WAL is already in exclusive-locking mode - meaning that this +** routine is a no-op. The pager must already hold the exclusive lock +** on the main database file before invoking this operation. +** +** If op is negative, then do a dry-run of the op==1 case but do +** not actually change anything. The pager uses this to see if it +** should acquire the database exclusive lock prior to invoking +** the op==1 case. +*/ +SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){ + int rc; + assert( pWal->writeLock==0 ); + assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 ); + + /* pWal->readLock is usually set, but might be -1 if there was a + ** prior error while attempting to acquire are read-lock. This cannot + ** happen if the connection is actually in exclusive mode (as no xShmLock + ** locks are taken in this case). Nor should the pager attempt to + ** upgrade to exclusive-mode following such an error. + */ + assert( pWal->readLock>=0 || pWal->lockError ); + assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) ); + + if( op==0 ){ + if( pWal->exclusiveMode ){ + pWal->exclusiveMode = 0; + if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){ + pWal->exclusiveMode = 1; + } + rc = pWal->exclusiveMode==0; + }else{ + /* Already in locking_mode=NORMAL */ + rc = 0; + } + }else if( op>0 ){ + assert( pWal->exclusiveMode==0 ); + assert( pWal->readLock>=0 ); + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + pWal->exclusiveMode = 1; + rc = 1; + }else{ + rc = pWal->exclusiveMode==0; + } + return rc; +} + +/* +** Return true if the argument is non-NULL and the WAL module is using +** heap-memory for the wal-index. Otherwise, if the argument is NULL or the +** WAL module is using shared-memory, return false. +*/ +SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){ + return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); +} + +#ifdef SQLITE_ENABLE_SNAPSHOT +/* Create a snapshot object. The content of a snapshot is opaque to +** every other subsystem, so the WAL module can put whatever it needs +** in the object. +*/ +SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ + int rc = SQLITE_OK; + WalIndexHdr *pRet; + + assert( pWal->readLock>=0 && pWal->writeLock==0 ); + + pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr)); + if( pRet==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr)); + *ppSnapshot = (sqlite3_snapshot*)pRet; + } + + return rc; +} + +/* Try to open on pSnapshot when the next read-transaction starts +*/ +SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ + pWal->pSnapshot = (WalIndexHdr*)pSnapshot; +} + +/* +** Return a +ve value if snapshot p1 is newer than p2. A -ve value if +** p1 is older than p2 and zero if p1 and p2 are the same snapshot. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){ + WalIndexHdr *pHdr1 = (WalIndexHdr*)p1; + WalIndexHdr *pHdr2 = (WalIndexHdr*)p2; + + /* aSalt[0] is a copy of the value stored in the wal file header. It + ** is incremented each time the wal file is restarted. */ + if( pHdr1->aSalt[0]aSalt[0] ) return -1; + if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1; + if( pHdr1->mxFramemxFrame ) return -1; + if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1; + return 0; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + +#ifdef SQLITE_ENABLE_ZIPVFS +/* +** If the argument is not NULL, it points to a Wal object that holds a +** read-lock. This function returns the database page-size if it is known, +** or zero if it is not (or if pWal is NULL). +*/ +SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){ + assert( pWal==0 || pWal->readLock>=0 ); + return (pWal ? pWal->szPage : 0); +} +#endif + +/* Return the sqlite3_file object for the WAL file +*/ +SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){ + return pWal->pWalFd; +} + +#endif /* #ifndef SQLITE_OMIT_WAL */ + +/************** End of wal.c *************************************************/ +/************** Begin file btmutex.c *****************************************/ +/* +** 2007 August 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code used to implement mutexes on Btree objects. +** This code really belongs in btree.c. But btree.c is getting too +** big and we want to break it down some. This packaged seemed like +** a good breakout. +*/ +/************** Include btreeInt.h in the middle of btmutex.c ****************/ +/************** Begin file btreeInt.h ****************************************/ +/* +** 2004 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements an external (disk-based) database using BTrees. +** For a detailed discussion of BTrees, refer to +** +** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: +** "Sorting And Searching", pages 473-480. Addison-Wesley +** Publishing Company, Reading, Massachusetts. +** +** The basic idea is that each page of the file contains N database +** entries and N+1 pointers to subpages. +** +** ---------------------------------------------------------------- +** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | +** ---------------------------------------------------------------- +** +** All of the keys on the page that Ptr(0) points to have values less +** than Key(0). All of the keys on page Ptr(1) and its subpages have +** values greater than Key(0) and less than Key(1). All of the keys +** on Ptr(N) and its subpages have values greater than Key(N-1). And +** so forth. +** +** Finding a particular key requires reading O(log(M)) pages from the +** disk where M is the number of entries in the tree. +** +** In this implementation, a single file can hold one or more separate +** BTrees. Each BTree is identified by the index of its root page. The +** key and data for any entry are combined to form the "payload". A +** fixed amount of payload can be carried directly on the database +** page. If the payload is larger than the preset amount then surplus +** bytes are stored on overflow pages. The payload for an entry +** and the preceding pointer are combined to form a "Cell". Each +** page has a small header which contains the Ptr(N) pointer and other +** information such as the size of key and data. +** +** FORMAT DETAILS +** +** The file is divided into pages. The first page is called page 1, +** the second is page 2, and so forth. A page number of zero indicates +** "no such page". The page size can be any power of 2 between 512 and 65536. +** Each page can be either a btree page, a freelist page, an overflow +** page, or a pointer-map page. +** +** The first page is always a btree page. The first 100 bytes of the first +** page contain a special header (the "file header") that describes the file. +** The format of the file header is as follows: +** +** OFFSET SIZE DESCRIPTION +** 0 16 Header string: "SQLite format 3\000" +** 16 2 Page size in bytes. (1 means 65536) +** 18 1 File format write version +** 19 1 File format read version +** 20 1 Bytes of unused space at the end of each page +** 21 1 Max embedded payload fraction (must be 64) +** 22 1 Min embedded payload fraction (must be 32) +** 23 1 Min leaf payload fraction (must be 32) +** 24 4 File change counter +** 28 4 Reserved for future use +** 32 4 First freelist page +** 36 4 Number of freelist pages in the file +** 40 60 15 4-byte meta values passed to higher layers +** +** 40 4 Schema cookie +** 44 4 File format of schema layer +** 48 4 Size of page cache +** 52 4 Largest root-page (auto/incr_vacuum) +** 56 4 1=UTF-8 2=UTF16le 3=UTF16be +** 60 4 User version +** 64 4 Incremental vacuum mode +** 68 4 Application-ID +** 72 20 unused +** 92 4 The version-valid-for number +** 96 4 SQLITE_VERSION_NUMBER +** +** All of the integer values are big-endian (most significant byte first). +** +** The file change counter is incremented when the database is changed +** This counter allows other processes to know when the file has changed +** and thus when they need to flush their cache. +** +** The max embedded payload fraction is the amount of the total usable +** space in a page that can be consumed by a single cell for standard +** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default +** is to limit the maximum cell size so that at least 4 cells will fit +** on one page. Thus the default max embedded payload fraction is 64. +** +** If the payload for a cell is larger than the max payload, then extra +** payload is spilled to overflow pages. Once an overflow page is allocated, +** as many bytes as possible are moved into the overflow pages without letting +** the cell size drop below the min embedded payload fraction. +** +** The min leaf payload fraction is like the min embedded payload fraction +** except that it applies to leaf nodes in a LEAFDATA tree. The maximum +** payload fraction for a LEAFDATA tree is always 100% (or 255) and it +** not specified in the header. +** +** Each btree pages is divided into three sections: The header, the +** cell pointer array, and the cell content area. Page 1 also has a 100-byte +** file header that occurs before the page header. +** +** |----------------| +** | file header | 100 bytes. Page 1 only. +** |----------------| +** | page header | 8 bytes for leaves. 12 bytes for interior nodes +** |----------------| +** | cell pointer | | 2 bytes per cell. Sorted order. +** | array | | Grows downward +** | | v +** |----------------| +** | unallocated | +** | space | +** |----------------| ^ Grows upwards +** | cell content | | Arbitrary order interspersed with freeblocks. +** | area | | and free space fragments. +** |----------------| +** +** The page headers looks like this: +** +** OFFSET SIZE DESCRIPTION +** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf +** 1 2 byte offset to the first freeblock +** 3 2 number of cells on this page +** 5 2 first byte of the cell content area +** 7 1 number of fragmented free bytes +** 8 4 Right child (the Ptr(N) value). Omitted on leaves. +** +** The flags define the format of this btree page. The leaf flag means that +** this page has no children. The zerodata flag means that this page carries +** only keys and no data. The intkey flag means that the key is an integer +** which is stored in the key size entry of the cell header rather than in +** the payload area. +** +** The cell pointer array begins on the first byte after the page header. +** The cell pointer array contains zero or more 2-byte numbers which are +** offsets from the beginning of the page to the cell content in the cell +** content area. The cell pointers occur in sorted order. The system strives +** to keep free space after the last cell pointer so that new cells can +** be easily added without having to defragment the page. +** +** Cell content is stored at the very end of the page and grows toward the +** beginning of the page. +** +** Unused space within the cell content area is collected into a linked list of +** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset +** to the first freeblock is given in the header. Freeblocks occur in +** increasing order. Because a freeblock must be at least 4 bytes in size, +** any group of 3 or fewer unused bytes in the cell content area cannot +** exist on the freeblock chain. A group of 3 or fewer free bytes is called +** a fragment. The total number of bytes in all fragments is recorded. +** in the page header at offset 7. +** +** SIZE DESCRIPTION +** 2 Byte offset of the next freeblock +** 2 Bytes in this freeblock +** +** Cells are of variable length. Cells are stored in the cell content area at +** the end of the page. Pointers to the cells are in the cell pointer array +** that immediately follows the page header. Cells is not necessarily +** contiguous or in order, but cell pointers are contiguous and in order. +** +** Cell content makes use of variable length integers. A variable +** length integer is 1 to 9 bytes where the lower 7 bits of each +** byte are used. The integer consists of all bytes that have bit 8 set and +** the first byte with bit 8 clear. The most significant byte of the integer +** appears first. A variable-length integer may not be more than 9 bytes long. +** As a special case, all 8 bytes of the 9th byte are used as data. This +** allows a 64-bit integer to be encoded in 9 bytes. +** +** 0x00 becomes 0x00000000 +** 0x7f becomes 0x0000007f +** 0x81 0x00 becomes 0x00000080 +** 0x82 0x00 becomes 0x00000100 +** 0x80 0x7f becomes 0x0000007f +** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 +** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 +** +** Variable length integers are used for rowids and to hold the number of +** bytes of key and data in a btree cell. +** +** The content of a cell looks like this: +** +** SIZE DESCRIPTION +** 4 Page number of the left child. Omitted if leaf flag is set. +** var Number of bytes of data. Omitted if the zerodata flag is set. +** var Number of bytes of key. Or the key itself if intkey flag is set. +** * Payload +** 4 First page of the overflow chain. Omitted if no overflow +** +** Overflow pages form a linked list. Each page except the last is completely +** filled with data (pagesize - 4 bytes). The last page can have as little +** as 1 byte of data. +** +** SIZE DESCRIPTION +** 4 Page number of next overflow page +** * Data +** +** Freelist pages come in two subtypes: trunk pages and leaf pages. The +** file header points to the first in a linked list of trunk page. Each trunk +** page points to multiple leaf pages. The content of a leaf page is +** unspecified. A trunk page looks like this: +** +** SIZE DESCRIPTION +** 4 Page number of next trunk page +** 4 Number of leaf pointers on this page +** * zero or more pages numbers of leaves +*/ +/* #include "sqliteInt.h" */ + + +/* The following value is the maximum cell size assuming a maximum page +** size give above. +*/ +#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8)) + +/* The maximum number of cells on a single page of the database. This +** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself +** plus 2 bytes for the index to the cell in the page header). Such +** small cells will be rare, but they are possible. +*/ +#define MX_CELL(pBt) ((pBt->pageSize-8)/6) + +/* Forward declarations */ +typedef struct MemPage MemPage; +typedef struct BtLock BtLock; +typedef struct CellInfo CellInfo; + +/* +** This is a magic string that appears at the beginning of every +** SQLite database in order to identify the file as a real database. +** +** You can change this value at compile-time by specifying a +** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The +** header must be exactly 16 bytes including the zero-terminator so +** the string itself should be 15 characters long. If you change +** the header, then your custom library will not be able to read +** databases generated by the standard tools and the standard tools +** will not be able to read databases created by your custom library. +*/ +#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ +# define SQLITE_FILE_HEADER "SQLite format 3" +#endif + +/* +** Page type flags. An ORed combination of these flags appear as the +** first byte of on-disk image of every BTree page. +*/ +#define PTF_INTKEY 0x01 +#define PTF_ZERODATA 0x02 +#define PTF_LEAFDATA 0x04 +#define PTF_LEAF 0x08 + +/* +** As each page of the file is loaded into memory, an instance of the following +** structure is appended and initialized to zero. This structure stores +** information about the page that is decoded from the raw file page. +** +** The pParent field points back to the parent page. This allows us to +** walk up the BTree from any leaf to the root. Care must be taken to +** unref() the parent page pointer when this page is no longer referenced. +** The pageDestructor() routine handles that chore. +** +** Access to all fields of this structure is controlled by the mutex +** stored in MemPage.pBt->mutex. +*/ +struct MemPage { + u8 isInit; /* True if previously initialized. MUST BE FIRST! */ + u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ + u8 intKey; /* True if table b-trees. False for index b-trees */ + u8 intKeyLeaf; /* True if the leaf of an intKey table */ + u8 leaf; /* True if a leaf page */ + u8 hdrOffset; /* 100 for page 1. 0 otherwise */ + u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ + u8 max1bytePayload; /* min(maxLocal,127) */ + u8 bBusy; /* Prevent endless loops on corrupt database files */ + u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ + u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ + u16 cellOffset; /* Index in aData of first cell pointer */ + u16 nFree; /* Number of free bytes on the page */ + u16 nCell; /* Number of cells on this page, local and ovfl */ + u16 maskPage; /* Mask for page offset */ + u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th + ** non-overflow cell */ + u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ + BtShared *pBt; /* Pointer to BtShared that this page is part of */ + u8 *aData; /* Pointer to disk image of the page data */ + u8 *aDataEnd; /* One byte past the end of usable data */ + u8 *aCellIdx; /* The cell index area */ + u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ + DbPage *pDbPage; /* Pager page handle */ + u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ + void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ + Pgno pgno; /* Page number for this page */ +}; + +/* +** The in-memory image of a disk page has the auxiliary information appended +** to the end. EXTRA_SIZE is the number of bytes of space needed to hold +** that extra information. +*/ +#define EXTRA_SIZE sizeof(MemPage) + +/* +** A linked list of the following structures is stored at BtShared.pLock. +** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor +** is opened on the table with root page BtShared.iTable. Locks are removed +** from this list when a transaction is committed or rolled back, or when +** a btree handle is closed. +*/ +struct BtLock { + Btree *pBtree; /* Btree handle holding this lock */ + Pgno iTable; /* Root page of table */ + u8 eLock; /* READ_LOCK or WRITE_LOCK */ + BtLock *pNext; /* Next in BtShared.pLock list */ +}; + +/* Candidate values for BtLock.eLock */ +#define READ_LOCK 1 +#define WRITE_LOCK 2 + +/* A Btree handle +** +** A database connection contains a pointer to an instance of +** this object for every database file that it has open. This structure +** is opaque to the database connection. The database connection cannot +** see the internals of this structure and only deals with pointers to +** this structure. +** +** For some database files, the same underlying database cache might be +** shared between multiple connections. In that case, each connection +** has it own instance of this object. But each instance of this object +** points to the same BtShared object. The database cache and the +** schema associated with the database file are all contained within +** the BtShared object. +** +** All fields in this structure are accessed under sqlite3.mutex. +** The pBt pointer itself may not be changed while there exists cursors +** in the referenced BtShared that point back to this Btree since those +** cursors have to go through this Btree to find their BtShared and +** they often do so without holding sqlite3.mutex. +*/ +struct Btree { + sqlite3 *db; /* The database connection holding this btree */ + BtShared *pBt; /* Sharable content of this btree */ + u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ + u8 sharable; /* True if we can share pBt with another db */ + u8 locked; /* True if db currently has pBt locked */ + u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ + int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ + int nBackup; /* Number of backup operations reading this btree */ + u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */ + Btree *pNext; /* List of other sharable Btrees from the same db */ + Btree *pPrev; /* Back pointer of the same list */ +#ifndef SQLITE_OMIT_SHARED_CACHE + BtLock lock; /* Object used to lock page 1 */ +#endif +}; + +/* +** Btree.inTrans may take one of the following values. +** +** If the shared-data extension is enabled, there may be multiple users +** of the Btree structure. At most one of these may open a write transaction, +** but any number may have active read transactions. +*/ +#define TRANS_NONE 0 +#define TRANS_READ 1 +#define TRANS_WRITE 2 + +/* +** An instance of this object represents a single database file. +** +** A single database file can be in use at the same time by two +** or more database connections. When two or more connections are +** sharing the same database file, each connection has it own +** private Btree object for the file and each of those Btrees points +** to this one BtShared object. BtShared.nRef is the number of +** connections currently sharing this database file. +** +** Fields in this structure are accessed under the BtShared.mutex +** mutex, except for nRef and pNext which are accessed under the +** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field +** may not be modified once it is initially set as long as nRef>0. +** The pSchema field may be set once under BtShared.mutex and +** thereafter is unchanged as long as nRef>0. +** +** isPending: +** +** If a BtShared client fails to obtain a write-lock on a database +** table (because there exists one or more read-locks on the table), +** the shared-cache enters 'pending-lock' state and isPending is +** set to true. +** +** The shared-cache leaves the 'pending lock' state when either of +** the following occur: +** +** 1) The current writer (BtShared.pWriter) concludes its transaction, OR +** 2) The number of locks held by other connections drops to zero. +** +** while in the 'pending-lock' state, no connection may start a new +** transaction. +** +** This feature is included to help prevent writer-starvation. +*/ +struct BtShared { + Pager *pPager; /* The page cache */ + sqlite3 *db; /* Database connection currently using this Btree */ + BtCursor *pCursor; /* A list of all open cursors */ + MemPage *pPage1; /* First page of the database */ + u8 openFlags; /* Flags to sqlite3BtreeOpen() */ +#ifndef SQLITE_OMIT_AUTOVACUUM + u8 autoVacuum; /* True if auto-vacuum is enabled */ + u8 incrVacuum; /* True if incr-vacuum is enabled */ + u8 bDoTruncate; /* True to truncate db on commit */ +#endif + u8 inTransaction; /* Transaction state */ + u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ +#ifdef SQLITE_HAS_CODEC + u8 optimalReserve; /* Desired amount of reserved space per page */ +#endif + u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ + u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ + u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ + u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ + u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ + u32 pageSize; /* Total number of bytes on a page */ + u32 usableSize; /* Number of usable bytes on each page */ + int nTransaction; /* Number of open transactions (read + write) */ + u32 nPage; /* Number of pages in the database */ + void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ + void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ + sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */ + Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ +#ifndef SQLITE_OMIT_SHARED_CACHE + int nRef; /* Number of references to this structure */ + BtShared *pNext; /* Next on a list of sharable BtShared structs */ + BtLock *pLock; /* List of locks held on this shared-btree struct */ + Btree *pWriter; /* Btree with currently open write transaction */ +#endif + u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ +}; + +/* +** Allowed values for BtShared.btsFlags +*/ +#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ +#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ +#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ +#define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ +#define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ +#define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ +#define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ + +/* +** An instance of the following structure is used to hold information +** about a cell. The parseCellPtr() function fills in this structure +** based on information extract from the raw disk page. +*/ +struct CellInfo { + i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ + u8 *pPayload; /* Pointer to the start of payload */ + u32 nPayload; /* Bytes of payload */ + u16 nLocal; /* Amount of payload held locally, not on overflow */ + u16 nSize; /* Size of the cell content on the main b-tree page */ +}; + +/* +** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than +** this will be declared corrupt. This value is calculated based on a +** maximum database size of 2^31 pages a minimum fanout of 2 for a +** root-node and 3 for all other internal nodes. +** +** If a tree that appears to be taller than this is encountered, it is +** assumed that the database is corrupt. +*/ +#define BTCURSOR_MAX_DEPTH 20 + +/* +** A cursor is a pointer to a particular entry within a particular +** b-tree within a database file. +** +** The entry is identified by its MemPage and the index in +** MemPage.aCell[] of the entry. +** +** A single database file can be shared by two more database connections, +** but cursors cannot be shared. Each cursor is associated with a +** particular database connection identified BtCursor.pBtree.db. +** +** Fields in this structure are accessed under the BtShared.mutex +** found at self->pBt->mutex. +** +** skipNext meaning: +** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. +** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. +** eState==FAULT: Cursor fault with skipNext as error code. +*/ +struct BtCursor { + Btree *pBtree; /* The Btree to which this cursor belongs */ + BtShared *pBt; /* The BtShared this cursor points to */ + BtCursor *pNext; /* Forms a linked list of all cursors */ + Pgno *aOverflow; /* Cache of overflow page locations */ + CellInfo info; /* A parse of the cell we are pointing at */ + i64 nKey; /* Size of pKey, or last integer key */ + void *pKey; /* Saved key that was cursor last known position */ + Pgno pgnoRoot; /* The root page of this tree */ + int nOvflAlloc; /* Allocated size of aOverflow[] array */ + int skipNext; /* Prev() is noop if negative. Next() is noop if positive. + ** Error code if eState==CURSOR_FAULT */ + u8 curFlags; /* zero or more BTCF_* flags defined below */ + u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */ + u8 eState; /* One of the CURSOR_XXX constants (see below) */ + u8 hints; /* As configured by CursorSetHints() */ + /* All fields above are zeroed when the cursor is allocated. See + ** sqlite3BtreeCursorZero(). Fields that follow must be manually + ** initialized. */ + i8 iPage; /* Index of current page in apPage */ + u8 curIntKey; /* Value of apPage[0]->intKey */ + struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ + void *padding1; /* Make object size a multiple of 16 */ + u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ + MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ +}; + +/* +** Legal values for BtCursor.curFlags +*/ +#define BTCF_WriteFlag 0x01 /* True if a write cursor */ +#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ +#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ +#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ +#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ +#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */ + +/* +** Potential values for BtCursor.eState. +** +** CURSOR_INVALID: +** Cursor does not point to a valid entry. This can happen (for example) +** because the table is empty or because BtreeCursorFirst() has not been +** called. +** +** CURSOR_VALID: +** Cursor points to a valid entry. getPayload() etc. may be called. +** +** CURSOR_SKIPNEXT: +** Cursor is valid except that the Cursor.skipNext field is non-zero +** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious() +** operation should be a no-op. +** +** CURSOR_REQUIRESEEK: +** The table that this cursor was opened on still exists, but has been +** modified since the cursor was last used. The cursor position is saved +** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in +** this state, restoreCursorPosition() can be called to attempt to +** seek the cursor to the saved position. +** +** CURSOR_FAULT: +** An unrecoverable error (an I/O error or a malloc failure) has occurred +** on a different connection that shares the BtShared cache with this +** cursor. The error has left the cache in an inconsistent state. +** Do nothing else with this cursor. Any attempt to use the cursor +** should return the error code stored in BtCursor.skipNext +*/ +#define CURSOR_INVALID 0 +#define CURSOR_VALID 1 +#define CURSOR_SKIPNEXT 2 +#define CURSOR_REQUIRESEEK 3 +#define CURSOR_FAULT 4 + +/* +** The database page the PENDING_BYTE occupies. This page is never used. +*/ +# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) + +/* +** These macros define the location of the pointer-map entry for a +** database page. The first argument to each is the number of usable +** bytes on each page of the database (often 1024). The second is the +** page number to look up in the pointer map. +** +** PTRMAP_PAGENO returns the database page number of the pointer-map +** page that stores the required pointer. PTRMAP_PTROFFSET returns +** the offset of the requested map entry. +** +** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, +** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be +** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements +** this test. +*/ +#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) +#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1)) +#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) + +/* +** The pointer map is a lookup table that identifies the parent page for +** each child page in the database file. The parent page is the page that +** contains a pointer to the child. Every page in the database contains +** 0 or 1 parent pages. (In this context 'database page' refers +** to any page that is not part of the pointer map itself.) Each pointer map +** entry consists of a single byte 'type' and a 4 byte parent page number. +** The PTRMAP_XXX identifiers below are the valid types. +** +** The purpose of the pointer map is to facility moving pages from one +** position in the file to another as part of autovacuum. When a page +** is moved, the pointer in its parent must be updated to point to the +** new location. The pointer map is used to locate the parent page quickly. +** +** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not +** used in this case. +** +** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number +** is not used in this case. +** +** PTRMAP_OVERFLOW1: The database page is the first page in a list of +** overflow pages. The page number identifies the page that +** contains the cell with a pointer to this overflow page. +** +** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of +** overflow pages. The page-number identifies the previous +** page in the overflow page list. +** +** PTRMAP_BTREE: The database page is a non-root btree page. The page number +** identifies the parent page in the btree. +*/ +#define PTRMAP_ROOTPAGE 1 +#define PTRMAP_FREEPAGE 2 +#define PTRMAP_OVERFLOW1 3 +#define PTRMAP_OVERFLOW2 4 +#define PTRMAP_BTREE 5 + +/* A bunch of assert() statements to check the transaction state variables +** of handle p (type Btree*) are internally consistent. +*/ +#define btreeIntegrity(p) \ + assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ + assert( p->pBt->inTransaction>=p->inTrans ); + + +/* +** The ISAUTOVACUUM macro is used within balance_nonroot() to determine +** if the database supports auto-vacuum or not. Because it is used +** within an expression that is an argument to another macro +** (sqliteMallocRaw), it is not possible to use conditional compilation. +** So, this macro is defined instead. +*/ +#ifndef SQLITE_OMIT_AUTOVACUUM +#define ISAUTOVACUUM (pBt->autoVacuum) +#else +#define ISAUTOVACUUM 0 +#endif + + +/* +** This structure is passed around through all the sanity checking routines +** in order to keep track of some global state information. +** +** The aRef[] array is allocated so that there is 1 bit for each page in +** the database. As the integrity-check proceeds, for each page used in +** the database the corresponding bit is set. This allows integrity-check to +** detect pages that are used twice and orphaned pages (both of which +** indicate corruption). +*/ +typedef struct IntegrityCk IntegrityCk; +struct IntegrityCk { + BtShared *pBt; /* The tree being checked out */ + Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ + u8 *aPgRef; /* 1 bit per page in the db (see above) */ + Pgno nPage; /* Number of pages in the database */ + int mxErr; /* Stop accumulating errors when this reaches zero */ + int nErr; /* Number of messages written to zErrMsg so far */ + int mallocFailed; /* A memory allocation error has occurred */ + const char *zPfx; /* Error message prefix */ + int v1, v2; /* Values for up to two %d fields in zPfx */ + StrAccum errMsg; /* Accumulate the error message text here */ + u32 *heap; /* Min-heap used for analyzing cell coverage */ +}; + +/* +** Routines to read or write a two- and four-byte big-endian integer values. +*/ +#define get2byte(x) ((x)[0]<<8 | (x)[1]) +#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) +#define get4byte sqlite3Get4byte +#define put4byte sqlite3Put4byte + +/* +** get2byteAligned(), unlike get2byte(), requires that its argument point to a +** two-byte aligned address. get2bytea() is only used for accessing the +** cell addresses in a btree header. +*/ +#if SQLITE_BYTEORDER==4321 +# define get2byteAligned(x) (*(u16*)(x)) +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && GCC_VERSION>=4008000 +# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ + && defined(_MSC_VER) && _MSC_VER>=1300 +# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) +#else +# define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) +#endif + +/************** End of btreeInt.h ********************************************/ +/************** Continuing where we left off in btmutex.c ********************/ +#ifndef SQLITE_OMIT_SHARED_CACHE +#if SQLITE_THREADSAFE + +/* +** Obtain the BtShared mutex associated with B-Tree handle p. Also, +** set BtShared.db to the database handle associated with p and the +** p->locked boolean to true. +*/ +static void lockBtreeMutex(Btree *p){ + assert( p->locked==0 ); + assert( sqlite3_mutex_notheld(p->pBt->mutex) ); + assert( sqlite3_mutex_held(p->db->mutex) ); + + sqlite3_mutex_enter(p->pBt->mutex); + p->pBt->db = p->db; + p->locked = 1; +} + +/* +** Release the BtShared mutex associated with B-Tree handle p and +** clear the p->locked boolean. +*/ +static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){ + BtShared *pBt = p->pBt; + assert( p->locked==1 ); + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( sqlite3_mutex_held(p->db->mutex) ); + assert( p->db==pBt->db ); + + sqlite3_mutex_leave(pBt->mutex); + p->locked = 0; +} + +/* Forward reference */ +static void SQLITE_NOINLINE btreeLockCarefully(Btree *p); + +/* +** Enter a mutex on the given BTree object. +** +** If the object is not sharable, then no mutex is ever required +** and this routine is a no-op. The underlying mutex is non-recursive. +** But we keep a reference count in Btree.wantToLock so the behavior +** of this interface is recursive. +** +** To avoid deadlocks, multiple Btrees are locked in the same order +** by all database connections. The p->pNext is a list of other +** Btrees belonging to the same database connection as the p Btree +** which need to be locked after p. If we cannot get a lock on +** p, then first unlock all of the others on p->pNext, then wait +** for the lock to become available on p, then relock all of the +** subsequent Btrees that desire a lock. +*/ +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){ + /* Some basic sanity checking on the Btree. The list of Btrees + ** connected by pNext and pPrev should be in sorted order by + ** Btree.pBt value. All elements of the list should belong to + ** the same connection. Only shared Btrees are on the list. */ + assert( p->pNext==0 || p->pNext->pBt>p->pBt ); + assert( p->pPrev==0 || p->pPrev->pBtpBt ); + assert( p->pNext==0 || p->pNext->db==p->db ); + assert( p->pPrev==0 || p->pPrev->db==p->db ); + assert( p->sharable || (p->pNext==0 && p->pPrev==0) ); + + /* Check for locking consistency */ + assert( !p->locked || p->wantToLock>0 ); + assert( p->sharable || p->wantToLock==0 ); + + /* We should already hold a lock on the database connection */ + assert( sqlite3_mutex_held(p->db->mutex) ); + + /* Unless the database is sharable and unlocked, then BtShared.db + ** should already be set correctly. */ + assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db ); + + if( !p->sharable ) return; + p->wantToLock++; + if( p->locked ) return; + btreeLockCarefully(p); +} + +/* This is a helper function for sqlite3BtreeLock(). By moving +** complex, but seldom used logic, out of sqlite3BtreeLock() and +** into this routine, we avoid unnecessary stack pointer changes +** and thus help the sqlite3BtreeLock() routine to run much faster +** in the common case. +*/ +static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){ + Btree *pLater; + + /* In most cases, we should be able to acquire the lock we + ** want without having to go through the ascending lock + ** procedure that follows. Just be sure not to block. + */ + if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){ + p->pBt->db = p->db; + p->locked = 1; + return; + } + + /* To avoid deadlock, first release all locks with a larger + ** BtShared address. Then acquire our lock. Then reacquire + ** the other BtShared locks that we used to hold in ascending + ** order. + */ + for(pLater=p->pNext; pLater; pLater=pLater->pNext){ + assert( pLater->sharable ); + assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt ); + assert( !pLater->locked || pLater->wantToLock>0 ); + if( pLater->locked ){ + unlockBtreeMutex(pLater); + } + } + lockBtreeMutex(p); + for(pLater=p->pNext; pLater; pLater=pLater->pNext){ + if( pLater->wantToLock ){ + lockBtreeMutex(pLater); + } + } +} + + +/* +** Exit the recursive mutex on a Btree. +*/ +SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){ + assert( sqlite3_mutex_held(p->db->mutex) ); + if( p->sharable ){ + assert( p->wantToLock>0 ); + p->wantToLock--; + if( p->wantToLock==0 ){ + unlockBtreeMutex(p); + } + } +} + +#ifndef NDEBUG +/* +** Return true if the BtShared mutex is held on the btree, or if the +** B-Tree is not marked as sharable. +** +** This routine is used only from within assert() statements. +*/ +SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){ + assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 ); + assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db ); + assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) ); + assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) ); + + return (p->sharable==0 || p->locked); +} +#endif + + +/* +** Enter the mutex on every Btree associated with a database +** connection. This is needed (for example) prior to parsing +** a statement since we will be comparing table and column names +** against all schemas and we do not want those schemas being +** reset out from under us. +** +** There is a corresponding leave-all procedures. +** +** Enter the mutexes in accending order by BtShared pointer address +** to avoid the possibility of deadlock when two threads with +** two or more btrees in common both try to lock all their btrees +** at the same instant. +*/ +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ + int i; + Btree *p; + assert( sqlite3_mutex_held(db->mutex) ); + for(i=0; inDb; i++){ + p = db->aDb[i].pBt; + if( p ) sqlite3BtreeEnter(p); + } +} +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ + int i; + Btree *p; + assert( sqlite3_mutex_held(db->mutex) ); + for(i=0; inDb; i++){ + p = db->aDb[i].pBt; + if( p ) sqlite3BtreeLeave(p); + } +} + +#ifndef NDEBUG +/* +** Return true if the current thread holds the database connection +** mutex and all required BtShared mutexes. +** +** This routine is used inside assert() statements only. +*/ +SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){ + int i; + if( !sqlite3_mutex_held(db->mutex) ){ + return 0; + } + for(i=0; inDb; i++){ + Btree *p; + p = db->aDb[i].pBt; + if( p && p->sharable && + (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){ + return 0; + } + } + return 1; +} +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* +** Return true if the correct mutexes are held for accessing the +** db->aDb[iDb].pSchema structure. The mutexes required for schema +** access are: +** +** (1) The mutex on db +** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt. +** +** If pSchema is not NULL, then iDb is computed from pSchema and +** db using sqlite3SchemaToIndex(). +*/ +SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){ + Btree *p; + assert( db!=0 ); + if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema); + assert( iDb>=0 && iDbnDb ); + if( !sqlite3_mutex_held(db->mutex) ) return 0; + if( iDb==1 ) return 1; + p = db->aDb[iDb].pBt; + assert( p!=0 ); + return p->sharable==0 || p->locked==1; +} +#endif /* NDEBUG */ + +#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */ +/* +** The following are special cases for mutex enter routines for use +** in single threaded applications that use shared cache. Except for +** these two routines, all mutex operations are no-ops in that case and +** are null #defines in btree.h. +** +** If shared cache is disabled, then all btree mutex routines, including +** the ones below, are no-ops and are null #defines in btree.h. +*/ + +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){ + p->pBt->db = p->db; +} +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ + int i; + for(i=0; inDb; i++){ + Btree *p = db->aDb[i].pBt; + if( p ){ + p->pBt->db = p->db; + } + } +} +#endif /* if SQLITE_THREADSAFE */ + +#ifndef SQLITE_OMIT_INCRBLOB +/* +** Enter a mutex on a Btree given a cursor owned by that Btree. +** +** These entry points are used by incremental I/O only. Enter() is required +** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not +** the build is threadsafe. Leave() is only required by threadsafe builds. +*/ +SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){ + sqlite3BtreeEnter(pCur->pBtree); +} +# if SQLITE_THREADSAFE +SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){ + sqlite3BtreeLeave(pCur->pBtree); +} +# endif +#endif /* ifndef SQLITE_OMIT_INCRBLOB */ + +#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */ + +/************** End of btmutex.c *********************************************/ +/************** Begin file btree.c *******************************************/ +/* +** 2004 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements an external (disk-based) database using BTrees. +** See the header comment on "btreeInt.h" for additional information. +** Including a description of file format and an overview of operation. +*/ +/* #include "btreeInt.h" */ + +/* +** The header string that appears at the beginning of every +** SQLite database. +*/ +static const char zMagicHeader[] = SQLITE_FILE_HEADER; + +/* +** Set this global variable to 1 to enable tracing using the TRACE +** macro. +*/ +#if 0 +int sqlite3BtreeTrace=1; /* True to enable tracing */ +# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} +#else +# define TRACE(X) +#endif + +/* +** Extract a 2-byte big-endian integer from an array of unsigned bytes. +** But if the value is zero, make it 65536. +** +** This routine is used to extract the "offset to cell content area" value +** from the header of a btree page. If the page size is 65536 and the page +** is empty, the offset should be 65536, but the 2-byte value stores zero. +** This routine makes the necessary adjustment to 65536. +*/ +#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1) + +/* +** Values passed as the 5th argument to allocateBtreePage() +*/ +#define BTALLOC_ANY 0 /* Allocate any page */ +#define BTALLOC_EXACT 1 /* Allocate exact page if possible */ +#define BTALLOC_LE 2 /* Allocate any page <= the parameter */ + +/* +** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not +** defined, or 0 if it is. For example: +** +** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum); +*/ +#ifndef SQLITE_OMIT_AUTOVACUUM +#define IfNotOmitAV(expr) (expr) +#else +#define IfNotOmitAV(expr) 0 +#endif + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** A list of BtShared objects that are eligible for participation +** in shared cache. This variable has file scope during normal builds, +** but the test harness needs to access it so we make it global for +** test builds. +** +** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. +*/ +#ifdef SQLITE_TEST +SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; +#else +static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; +#endif +#endif /* SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Enable or disable the shared pager and schema features. +** +** This routine has no effect on existing database connections. +** The shared cache setting effects only future calls to +** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){ + sqlite3GlobalConfig.sharedCacheEnabled = enable; + return SQLITE_OK; +} +#endif + + + +#ifdef SQLITE_OMIT_SHARED_CACHE + /* + ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), + ** and clearAllSharedCacheTableLocks() + ** manipulate entries in the BtShared.pLock linked list used to store + ** shared-cache table level locks. If the library is compiled with the + ** shared-cache feature disabled, then there is only ever one user + ** of each BtShared structure and so this locking is not necessary. + ** So define the lock related functions as no-ops. + */ + #define querySharedCacheTableLock(a,b,c) SQLITE_OK + #define setSharedCacheTableLock(a,b,c) SQLITE_OK + #define clearAllSharedCacheTableLocks(a) + #define downgradeAllSharedCacheTableLocks(a) + #define hasSharedCacheTableLock(a,b,c,d) 1 + #define hasReadConflicts(a, b) 0 +#endif + +#ifndef SQLITE_OMIT_SHARED_CACHE + +#ifdef SQLITE_DEBUG +/* +**** This function is only used as part of an assert() statement. *** +** +** Check to see if pBtree holds the required locks to read or write to the +** table with root page iRoot. Return 1 if it does and 0 if not. +** +** For example, when writing to a table with root-page iRoot via +** Btree connection pBtree: +** +** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); +** +** When writing to an index that resides in a sharable database, the +** caller should have first obtained a lock specifying the root page of +** the corresponding table. This makes things a bit more complicated, +** as this module treats each table as a separate structure. To determine +** the table corresponding to the index being written, this +** function has to search through the database schema. +** +** Instead of a lock on the table/index rooted at page iRoot, the caller may +** hold a write-lock on the schema table (root page 1). This is also +** acceptable. +*/ +static int hasSharedCacheTableLock( + Btree *pBtree, /* Handle that must hold lock */ + Pgno iRoot, /* Root page of b-tree */ + int isIndex, /* True if iRoot is the root of an index b-tree */ + int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ +){ + Schema *pSchema = (Schema *)pBtree->pBt->pSchema; + Pgno iTab = 0; + BtLock *pLock; + + /* If this database is not shareable, or if the client is reading + ** and has the read-uncommitted flag set, then no lock is required. + ** Return true immediately. + */ + if( (pBtree->sharable==0) + || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) + ){ + return 1; + } + + /* If the client is reading or writing an index and the schema is + ** not loaded, then it is too difficult to actually check to see if + ** the correct locks are held. So do not bother - just return true. + ** This case does not come up very often anyhow. + */ + if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){ + return 1; + } + + /* Figure out the root-page that the lock should be held on. For table + ** b-trees, this is just the root page of the b-tree being read or + ** written. For index b-trees, it is the root page of the associated + ** table. */ + if( isIndex ){ + HashElem *p; + for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ + Index *pIdx = (Index *)sqliteHashData(p); + if( pIdx->tnum==(int)iRoot ){ + if( iTab ){ + /* Two or more indexes share the same root page. There must + ** be imposter tables. So just return true. The assert is not + ** useful in that case. */ + return 1; + } + iTab = pIdx->pTable->tnum; + } + } + }else{ + iTab = iRoot; + } + + /* Search for the required lock. Either a write-lock on root-page iTab, a + ** write-lock on the schema table, or (if the client is reading) a + ** read-lock on iTab will suffice. Return 1 if any of these are found. */ + for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ + if( pLock->pBtree==pBtree + && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) + && pLock->eLock>=eLockType + ){ + return 1; + } + } + + /* Failed to find the required lock. */ + return 0; +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* +**** This function may be used as part of assert() statements only. **** +** +** Return true if it would be illegal for pBtree to write into the +** table or index rooted at iRoot because other shared connections are +** simultaneously reading that same table or index. +** +** It is illegal for pBtree to write if some other Btree object that +** shares the same BtShared object is currently reading or writing +** the iRoot table. Except, if the other Btree object has the +** read-uncommitted flag set, then it is OK for the other object to +** have a read cursor. +** +** For example, before writing to any part of the table or index +** rooted at page iRoot, one should call: +** +** assert( !hasReadConflicts(pBtree, iRoot) ); +*/ +static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ + BtCursor *p; + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + if( p->pgnoRoot==iRoot + && p->pBtree!=pBtree + && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) + ){ + return 1; + } + } + return 0; +} +#endif /* #ifdef SQLITE_DEBUG */ + +/* +** Query to see if Btree handle p may obtain a lock of type eLock +** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return +** SQLITE_OK if the lock may be obtained (by calling +** setSharedCacheTableLock()), or SQLITE_LOCKED if not. +*/ +static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ + BtShared *pBt = p->pBt; + BtLock *pIter; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); + assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); + + /* If requesting a write-lock, then the Btree must have an open write + ** transaction on this file. And, obviously, for this to be so there + ** must be an open write transaction on the file itself. + */ + assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); + assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); + + /* This routine is a no-op if the shared-cache is not enabled */ + if( !p->sharable ){ + return SQLITE_OK; + } + + /* If some other connection is holding an exclusive lock, the + ** requested lock may not be obtained. + */ + if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){ + sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); + return SQLITE_LOCKED_SHAREDCACHE; + } + + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + /* The condition (pIter->eLock!=eLock) in the following if(...) + ** statement is a simplification of: + ** + ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) + ** + ** since we know that if eLock==WRITE_LOCK, then no other connection + ** may hold a WRITE_LOCK on any table in this file (since there can + ** only be a single writer). + */ + assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); + assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); + if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ + sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); + if( eLock==WRITE_LOCK ){ + assert( p==pBt->pWriter ); + pBt->btsFlags |= BTS_PENDING; + } + return SQLITE_LOCKED_SHAREDCACHE; + } + } + return SQLITE_OK; +} +#endif /* !SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Add a lock on the table with root-page iTable to the shared-btree used +** by Btree handle p. Parameter eLock must be either READ_LOCK or +** WRITE_LOCK. +** +** This function assumes the following: +** +** (a) The specified Btree object p is connected to a sharable +** database (one with the BtShared.sharable flag set), and +** +** (b) No other Btree objects hold a lock that conflicts +** with the requested lock (i.e. querySharedCacheTableLock() has +** already been called and returned SQLITE_OK). +** +** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM +** is returned if a malloc attempt fails. +*/ +static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ + BtShared *pBt = p->pBt; + BtLock *pLock = 0; + BtLock *pIter; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); + + /* A connection with the read-uncommitted flag set will never try to + ** obtain a read-lock using this function. The only read-lock obtained + ** by a connection in read-uncommitted mode is on the sqlite_master + ** table, and that lock is obtained in BtreeBeginTrans(). */ + assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); + + /* This function should only be called on a sharable b-tree after it + ** has been determined that no other b-tree holds a conflicting lock. */ + assert( p->sharable ); + assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); + + /* First search the list for an existing lock on this table. */ + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + if( pIter->iTable==iTable && pIter->pBtree==p ){ + pLock = pIter; + break; + } + } + + /* If the above search did not find a BtLock struct associating Btree p + ** with table iTable, allocate one and link it into the list. + */ + if( !pLock ){ + pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); + if( !pLock ){ + return SQLITE_NOMEM_BKPT; + } + pLock->iTable = iTable; + pLock->pBtree = p; + pLock->pNext = pBt->pLock; + pBt->pLock = pLock; + } + + /* Set the BtLock.eLock variable to the maximum of the current lock + ** and the requested lock. This means if a write-lock was already held + ** and a read-lock requested, we don't incorrectly downgrade the lock. + */ + assert( WRITE_LOCK>READ_LOCK ); + if( eLock>pLock->eLock ){ + pLock->eLock = eLock; + } + + return SQLITE_OK; +} +#endif /* !SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Release all the table locks (locks obtained via calls to +** the setSharedCacheTableLock() procedure) held by Btree object p. +** +** This function assumes that Btree p has an open read or write +** transaction. If it does not, then the BTS_PENDING flag +** may be incorrectly cleared. +*/ +static void clearAllSharedCacheTableLocks(Btree *p){ + BtShared *pBt = p->pBt; + BtLock **ppIter = &pBt->pLock; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( p->sharable || 0==*ppIter ); + assert( p->inTrans>0 ); + + while( *ppIter ){ + BtLock *pLock = *ppIter; + assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree ); + assert( pLock->pBtree->inTrans>=pLock->eLock ); + if( pLock->pBtree==p ){ + *ppIter = pLock->pNext; + assert( pLock->iTable!=1 || pLock==&p->lock ); + if( pLock->iTable!=1 ){ + sqlite3_free(pLock); + } + }else{ + ppIter = &pLock->pNext; + } + } + + assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter ); + if( pBt->pWriter==p ){ + pBt->pWriter = 0; + pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); + }else if( pBt->nTransaction==2 ){ + /* This function is called when Btree p is concluding its + ** transaction. If there currently exists a writer, and p is not + ** that writer, then the number of locks held by connections other + ** than the writer must be about to drop to zero. In this case + ** set the BTS_PENDING flag to 0. + ** + ** If there is not currently a writer, then BTS_PENDING must + ** be zero already. So this next line is harmless in that case. + */ + pBt->btsFlags &= ~BTS_PENDING; + } +} + +/* +** This function changes all write-locks held by Btree p into read-locks. +*/ +static void downgradeAllSharedCacheTableLocks(Btree *p){ + BtShared *pBt = p->pBt; + if( pBt->pWriter==p ){ + BtLock *pLock; + pBt->pWriter = 0; + pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); + for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ + assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); + pLock->eLock = READ_LOCK; + } + } +} + +#endif /* SQLITE_OMIT_SHARED_CACHE */ + +static void releasePage(MemPage *pPage); /* Forward reference */ + +/* +***** This routine is used inside of assert() only **** +** +** Verify that the cursor holds the mutex on its BtShared +*/ +#ifdef SQLITE_DEBUG +static int cursorHoldsMutex(BtCursor *p){ + return sqlite3_mutex_held(p->pBt->mutex); +} +static int cursorOwnsBtShared(BtCursor *p){ + assert( cursorHoldsMutex(p) ); + return (p->pBtree->db==p->pBt->db); +} +#endif + +/* +** Invalidate the overflow cache of the cursor passed as the first argument. +** on the shared btree structure pBt. +*/ +#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl) + +/* +** Invalidate the overflow page-list cache for all cursors opened +** on the shared btree structure pBt. +*/ +static void invalidateAllOverflowCache(BtShared *pBt){ + BtCursor *p; + assert( sqlite3_mutex_held(pBt->mutex) ); + for(p=pBt->pCursor; p; p=p->pNext){ + invalidateOverflowCache(p); + } +} + +#ifndef SQLITE_OMIT_INCRBLOB +/* +** This function is called before modifying the contents of a table +** to invalidate any incrblob cursors that are open on the +** row or one of the rows being modified. +** +** If argument isClearTable is true, then the entire contents of the +** table is about to be deleted. In this case invalidate all incrblob +** cursors open on any row within the table with root-page pgnoRoot. +** +** Otherwise, if argument isClearTable is false, then the row with +** rowid iRow is being replaced or deleted. In this case invalidate +** only those incrblob cursors open on that specific row. +*/ +static void invalidateIncrblobCursors( + Btree *pBtree, /* The database file to check */ + i64 iRow, /* The rowid that might be changing */ + int isClearTable /* True if all rows are being deleted */ +){ + BtCursor *p; + if( pBtree->hasIncrblobCur==0 ) return; + assert( sqlite3BtreeHoldsMutex(pBtree) ); + pBtree->hasIncrblobCur = 0; + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + if( (p->curFlags & BTCF_Incrblob)!=0 ){ + pBtree->hasIncrblobCur = 1; + if( isClearTable || p->info.nKey==iRow ){ + p->eState = CURSOR_INVALID; + } + } + } +} + +#else + /* Stub function when INCRBLOB is omitted */ + #define invalidateIncrblobCursors(x,y,z) +#endif /* SQLITE_OMIT_INCRBLOB */ + +/* +** Set bit pgno of the BtShared.pHasContent bitvec. This is called +** when a page that previously contained data becomes a free-list leaf +** page. +** +** The BtShared.pHasContent bitvec exists to work around an obscure +** bug caused by the interaction of two useful IO optimizations surrounding +** free-list leaf pages: +** +** 1) When all data is deleted from a page and the page becomes +** a free-list leaf page, the page is not written to the database +** (as free-list leaf pages contain no meaningful data). Sometimes +** such a page is not even journalled (as it will not be modified, +** why bother journalling it?). +** +** 2) When a free-list leaf page is reused, its content is not read +** from the database or written to the journal file (why should it +** be, if it is not at all meaningful?). +** +** By themselves, these optimizations work fine and provide a handy +** performance boost to bulk delete or insert operations. However, if +** a page is moved to the free-list and then reused within the same +** transaction, a problem comes up. If the page is not journalled when +** it is moved to the free-list and it is also not journalled when it +** is extracted from the free-list and reused, then the original data +** may be lost. In the event of a rollback, it may not be possible +** to restore the database to its original configuration. +** +** The solution is the BtShared.pHasContent bitvec. Whenever a page is +** moved to become a free-list leaf page, the corresponding bit is +** set in the bitvec. Whenever a leaf page is extracted from the free-list, +** optimization 2 above is omitted if the corresponding bit is already +** set in BtShared.pHasContent. The contents of the bitvec are cleared +** at the end of every transaction. +*/ +static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ + int rc = SQLITE_OK; + if( !pBt->pHasContent ){ + assert( pgno<=pBt->nPage ); + pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); + if( !pBt->pHasContent ){ + rc = SQLITE_NOMEM_BKPT; + } + } + if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ + rc = sqlite3BitvecSet(pBt->pHasContent, pgno); + } + return rc; +} + +/* +** Query the BtShared.pHasContent vector. +** +** This function is called when a free-list leaf page is removed from the +** free-list for reuse. It returns false if it is safe to retrieve the +** page from the pager layer with the 'no-content' flag set. True otherwise. +*/ +static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ + Bitvec *p = pBt->pHasContent; + return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); +} + +/* +** Clear (destroy) the BtShared.pHasContent bitvec. This should be +** invoked at the conclusion of each write-transaction. +*/ +static void btreeClearHasContent(BtShared *pBt){ + sqlite3BitvecDestroy(pBt->pHasContent); + pBt->pHasContent = 0; +} + +/* +** Release all of the apPage[] pages for a cursor. +*/ +static void btreeReleaseAllCursorPages(BtCursor *pCur){ + int i; + for(i=0; i<=pCur->iPage; i++){ + releasePage(pCur->apPage[i]); + pCur->apPage[i] = 0; + } + pCur->iPage = -1; +} + +/* +** The cursor passed as the only argument must point to a valid entry +** when this function is called (i.e. have eState==CURSOR_VALID). This +** function saves the current cursor key in variables pCur->nKey and +** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error +** code otherwise. +** +** If the cursor is open on an intkey table, then the integer key +** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to +** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is +** set to point to a malloced buffer pCur->nKey bytes in size containing +** the key. +*/ +static int saveCursorKey(BtCursor *pCur){ + int rc; + assert( CURSOR_VALID==pCur->eState ); + assert( 0==pCur->pKey ); + assert( cursorHoldsMutex(pCur) ); + + rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); + assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ + + /* If this is an intKey table, then the above call to BtreeKeySize() + ** stores the integer key in pCur->nKey. In this case this value is + ** all that is required. Otherwise, if pCur is not open on an intKey + ** table, then malloc space for and store the pCur->nKey bytes of key + ** data. */ + if( 0==pCur->curIntKey ){ + void *pKey = sqlite3Malloc( pCur->nKey ); + if( pKey ){ + rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); + if( rc==SQLITE_OK ){ + pCur->pKey = pKey; + }else{ + sqlite3_free(pKey); + } + }else{ + rc = SQLITE_NOMEM_BKPT; + } + } + assert( !pCur->curIntKey || !pCur->pKey ); + return rc; +} + +/* +** Save the current cursor position in the variables BtCursor.nKey +** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. +** +** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) +** prior to calling this routine. +*/ +static int saveCursorPosition(BtCursor *pCur){ + int rc; + + assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState ); + assert( 0==pCur->pKey ); + assert( cursorHoldsMutex(pCur) ); + + if( pCur->eState==CURSOR_SKIPNEXT ){ + pCur->eState = CURSOR_VALID; + }else{ + pCur->skipNext = 0; + } + + rc = saveCursorKey(pCur); + if( rc==SQLITE_OK ){ + btreeReleaseAllCursorPages(pCur); + pCur->eState = CURSOR_REQUIRESEEK; + } + + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast); + return rc; +} + +/* Forward reference */ +static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); + +/* +** Save the positions of all cursors (except pExcept) that are open on +** the table with root-page iRoot. "Saving the cursor position" means that +** the location in the btree is remembered in such a way that it can be +** moved back to the same spot after the btree has been modified. This +** routine is called just before cursor pExcept is used to modify the +** table, for example in BtreeDelete() or BtreeInsert(). +** +** If there are two or more cursors on the same btree, then all such +** cursors should have their BTCF_Multiple flag set. The btreeCursor() +** routine enforces that rule. This routine only needs to be called in +** the uncommon case when pExpect has the BTCF_Multiple flag set. +** +** If pExpect!=NULL and if no other cursors are found on the same root-page, +** then the BTCF_Multiple flag on pExpect is cleared, to avoid another +** pointless call to this routine. +** +** Implementation note: This routine merely checks to see if any cursors +** need to be saved. It calls out to saveCursorsOnList() in the (unusual) +** event that cursors are in need to being saved. +*/ +static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ + BtCursor *p; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pExcept==0 || pExcept->pBt==pBt ); + for(p=pBt->pCursor; p; p=p->pNext){ + if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; + } + if( p ) return saveCursorsOnList(p, iRoot, pExcept); + if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; + return SQLITE_OK; +} + +/* This helper routine to saveAllCursors does the actual work of saving +** the cursors if and when a cursor is found that actually requires saving. +** The common case is that no cursors need to be saved, so this routine is +** broken out from its caller to avoid unnecessary stack pointer movement. +*/ +static int SQLITE_NOINLINE saveCursorsOnList( + BtCursor *p, /* The first cursor that needs saving */ + Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ + BtCursor *pExcept /* Do not save this cursor */ +){ + do{ + if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ + if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ + int rc = saveCursorPosition(p); + if( SQLITE_OK!=rc ){ + return rc; + } + }else{ + testcase( p->iPage>0 ); + btreeReleaseAllCursorPages(p); + } + } + p = p->pNext; + }while( p ); + return SQLITE_OK; +} + +/* +** Clear the current cursor position. +*/ +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + sqlite3_free(pCur->pKey); + pCur->pKey = 0; + pCur->eState = CURSOR_INVALID; +} + +/* +** In this version of BtreeMoveto, pKey is a packed index record +** such as is generated by the OP_MakeRecord opcode. Unpack the +** record and then call BtreeMovetoUnpacked() to do the work. +*/ +static int btreeMoveto( + BtCursor *pCur, /* Cursor open on the btree to be searched */ + const void *pKey, /* Packed key if the btree is an index */ + i64 nKey, /* Integer key for tables. Size of pKey for indices */ + int bias, /* Bias search to the high end */ + int *pRes /* Write search results here */ +){ + int rc; /* Status code */ + UnpackedRecord *pIdxKey; /* Unpacked index key */ + char aSpace[200]; /* Temp space for pIdxKey - to avoid a malloc */ + char *pFree = 0; + + if( pKey ){ + assert( nKey==(i64)(int)nKey ); + pIdxKey = sqlite3VdbeAllocUnpackedRecord( + pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree + ); + if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; + sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); + if( pIdxKey->nField==0 ){ + sqlite3DbFree(pCur->pKeyInfo->db, pFree); + return SQLITE_CORRUPT_BKPT; + } + }else{ + pIdxKey = 0; + } + rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); + if( pFree ){ + sqlite3DbFree(pCur->pKeyInfo->db, pFree); + } + return rc; +} + +/* +** Restore the cursor to the position it was in (or as close to as possible) +** when saveCursorPosition() was called. Note that this call deletes the +** saved position info stored by saveCursorPosition(), so there can be +** at most one effective restoreCursorPosition() call after each +** saveCursorPosition(). +*/ +static int btreeRestoreCursorPosition(BtCursor *pCur){ + int rc; + int skipNext; + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState>=CURSOR_REQUIRESEEK ); + if( pCur->eState==CURSOR_FAULT ){ + return pCur->skipNext; + } + pCur->eState = CURSOR_INVALID; + rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); + if( rc==SQLITE_OK ){ + sqlite3_free(pCur->pKey); + pCur->pKey = 0; + assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); + pCur->skipNext |= skipNext; + if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ + pCur->eState = CURSOR_SKIPNEXT; + } + } + return rc; +} + +#define restoreCursorPosition(p) \ + (p->eState>=CURSOR_REQUIRESEEK ? \ + btreeRestoreCursorPosition(p) : \ + SQLITE_OK) + +/* +** Determine whether or not a cursor has moved from the position where +** it was last placed, or has been invalidated for any other reason. +** Cursors can move when the row they are pointing at is deleted out +** from under them, for example. Cursor might also move if a btree +** is rebalanced. +** +** Calling this routine with a NULL cursor pointer returns false. +** +** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor +** back to where it ought to be if this routine returns true. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ + return pCur->eState!=CURSOR_VALID; +} + +/* +** This routine restores a cursor back to its original position after it +** has been moved by some outside activity (such as a btree rebalance or +** a row having been deleted out from under the cursor). +** +** On success, the *pDifferentRow parameter is false if the cursor is left +** pointing at exactly the same row. *pDifferntRow is the row the cursor +** was pointing to has been deleted, forcing the cursor to point to some +** nearby row. +** +** This routine should only be called for a cursor that just returned +** TRUE from sqlite3BtreeCursorHasMoved(). +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ + int rc; + + assert( pCur!=0 ); + assert( pCur->eState!=CURSOR_VALID ); + rc = restoreCursorPosition(pCur); + if( rc ){ + *pDifferentRow = 1; + return rc; + } + if( pCur->eState!=CURSOR_VALID ){ + *pDifferentRow = 1; + }else{ + assert( pCur->skipNext==0 ); + *pDifferentRow = 0; + } + return SQLITE_OK; +} + +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* +** Provide hints to the cursor. The particular hint given (and the type +** and number of the varargs parameters) is determined by the eHintType +** parameter. See the definitions of the BTREE_HINT_* macros for details. +*/ +SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ + /* Used only by system that substitute their own storage engine */ +} +#endif + +/* +** Provide flag hints to the cursor. +*/ +SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ + assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); + pCur->hints = x; +} + + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Given a page number of a regular database page, return the page +** number for the pointer-map page that contains the entry for the +** input page number. +** +** Return 0 (not a valid page) for pgno==1 since there is +** no pointer map associated with page 1. The integrity_check logic +** requires that ptrmapPageno(*,1)!=1. +*/ +static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ + int nPagesPerMapPage; + Pgno iPtrMap, ret; + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pgno<2 ) return 0; + nPagesPerMapPage = (pBt->usableSize/5)+1; + iPtrMap = (pgno-2)/nPagesPerMapPage; + ret = (iPtrMap*nPagesPerMapPage) + 2; + if( ret==PENDING_BYTE_PAGE(pBt) ){ + ret++; + } + return ret; +} + +/* +** Write an entry into the pointer map. +** +** This routine updates the pointer map entry for page number 'key' +** so that it maps to type 'eType' and parent page number 'pgno'. +** +** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is +** a no-op. If an error occurs, the appropriate error code is written +** into *pRC. +*/ +static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ + DbPage *pDbPage; /* The pointer map page */ + u8 *pPtrmap; /* The pointer map data */ + Pgno iPtrmap; /* The pointer map page number */ + int offset; /* Offset in pointer map page */ + int rc; /* Return code from subfunctions */ + + if( *pRC ) return; + + assert( sqlite3_mutex_held(pBt->mutex) ); + /* The master-journal page number must never be used as a pointer map page */ + assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); + + assert( pBt->autoVacuum ); + if( key==0 ){ + *pRC = SQLITE_CORRUPT_BKPT; + return; + } + iPtrmap = PTRMAP_PAGENO(pBt, key); + rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + offset = PTRMAP_PTROFFSET(iPtrmap, key); + if( offset<0 ){ + *pRC = SQLITE_CORRUPT_BKPT; + goto ptrmap_exit; + } + assert( offset <= (int)pBt->usableSize-5 ); + pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); + + if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ + TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); + *pRC= rc = sqlite3PagerWrite(pDbPage); + if( rc==SQLITE_OK ){ + pPtrmap[offset] = eType; + put4byte(&pPtrmap[offset+1], parent); + } + } + +ptrmap_exit: + sqlite3PagerUnref(pDbPage); +} + +/* +** Read an entry from the pointer map. +** +** This routine retrieves the pointer map entry for page 'key', writing +** the type and parent page number to *pEType and *pPgno respectively. +** An error code is returned if something goes wrong, otherwise SQLITE_OK. +*/ +static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ + DbPage *pDbPage; /* The pointer map page */ + int iPtrmap; /* Pointer map page index */ + u8 *pPtrmap; /* Pointer map page data */ + int offset; /* Offset of entry in pointer map */ + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + + iPtrmap = PTRMAP_PAGENO(pBt, key); + rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); + if( rc!=0 ){ + return rc; + } + pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); + + offset = PTRMAP_PTROFFSET(iPtrmap, key); + if( offset<0 ){ + sqlite3PagerUnref(pDbPage); + return SQLITE_CORRUPT_BKPT; + } + assert( offset <= (int)pBt->usableSize-5 ); + assert( pEType!=0 ); + *pEType = pPtrmap[offset]; + if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); + + sqlite3PagerUnref(pDbPage); + if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; + return SQLITE_OK; +} + +#else /* if defined SQLITE_OMIT_AUTOVACUUM */ + #define ptrmapPut(w,x,y,z,rc) + #define ptrmapGet(w,x,y,z) SQLITE_OK + #define ptrmapPutOvflPtr(x, y, rc) +#endif + +/* +** Given a btree page and a cell index (0 means the first cell on +** the page, 1 means the second cell, and so forth) return a pointer +** to the cell content. +** +** findCellPastPtr() does the same except it skips past the initial +** 4-byte child pointer found on interior pages, if there is one. +** +** This routine works only for pages that do not contain overflow cells. +*/ +#define findCell(P,I) \ + ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) +#define findCellPastPtr(P,I) \ + ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) + + +/* +** This is common tail processing for btreeParseCellPtr() and +** btreeParseCellPtrIndex() for the case when the cell does not fit entirely +** on a single B-tree page. Make necessary adjustments to the CellInfo +** structure. +*/ +static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ + CellInfo *pInfo /* Fill in this structure */ +){ + /* If the payload will not fit completely on the local page, we have + ** to decide how much to store locally and how much to spill onto + ** overflow pages. The strategy is to minimize the amount of unused + ** space on overflow pages while keeping the amount of local storage + ** in between minLocal and maxLocal. + ** + ** Warning: changing the way overflow payload is distributed in any + ** way will result in an incompatible file format. + */ + int minLocal; /* Minimum amount of payload held locally */ + int maxLocal; /* Maximum amount of payload held locally */ + int surplus; /* Overflow payload available for local storage */ + + minLocal = pPage->minLocal; + maxLocal = pPage->maxLocal; + surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); + testcase( surplus==maxLocal ); + testcase( surplus==maxLocal+1 ); + if( surplus <= maxLocal ){ + pInfo->nLocal = (u16)surplus; + }else{ + pInfo->nLocal = (u16)minLocal; + } + pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; +} + +/* +** The following routines are implementations of the MemPage.xParseCell() +** method. +** +** Parse a cell content block and fill in the CellInfo structure. +** +** btreeParseCellPtr() => table btree leaf nodes +** btreeParseCellNoPayload() => table btree internal nodes +** btreeParseCellPtrIndex() => index btree nodes +** +** There is also a wrapper function btreeParseCell() that works for +** all MemPage types and that references the cell by index rather than +** by pointer. +*/ +static void btreeParseCellPtrNoPayload( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ + CellInfo *pInfo /* Fill in this structure */ +){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->leaf==0 ); + assert( pPage->childPtrSize==4 ); +#ifndef SQLITE_DEBUG + UNUSED_PARAMETER(pPage); +#endif + pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); + pInfo->nPayload = 0; + pInfo->nLocal = 0; + pInfo->pPayload = 0; + return; +} +static void btreeParseCellPtr( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ + CellInfo *pInfo /* Fill in this structure */ +){ + u8 *pIter; /* For scanning through pCell */ + u32 nPayload; /* Number of bytes of cell payload */ + u64 iKey; /* Extracted Key value */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->leaf==0 || pPage->leaf==1 ); + assert( pPage->intKeyLeaf ); + assert( pPage->childPtrSize==0 ); + pIter = pCell; + + /* The next block of code is equivalent to: + ** + ** pIter += getVarint32(pIter, nPayload); + ** + ** The code is inlined to avoid a function call. + */ + nPayload = *pIter; + if( nPayload>=0x80 ){ + u8 *pEnd = &pIter[8]; + nPayload &= 0x7f; + do{ + nPayload = (nPayload<<7) | (*++pIter & 0x7f); + }while( (*pIter)>=0x80 && pIternKey); + ** + ** The code is inlined to avoid a function call. + */ + iKey = *pIter; + if( iKey>=0x80 ){ + u8 *pEnd = &pIter[7]; + iKey &= 0x7f; + while(1){ + iKey = (iKey<<7) | (*++pIter & 0x7f); + if( (*pIter)<0x80 ) break; + if( pIter>=pEnd ){ + iKey = (iKey<<8) | *++pIter; + break; + } + } + } + pIter++; + + pInfo->nKey = *(i64*)&iKey; + pInfo->nPayload = nPayload; + pInfo->pPayload = pIter; + testcase( nPayload==pPage->maxLocal ); + testcase( nPayload==pPage->maxLocal+1 ); + if( nPayload<=pPage->maxLocal ){ + /* This is the (easy) common case where the entire payload fits + ** on the local page. No overflow is required. + */ + pInfo->nSize = nPayload + (u16)(pIter - pCell); + if( pInfo->nSize<4 ) pInfo->nSize = 4; + pInfo->nLocal = (u16)nPayload; + }else{ + btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); + } +} +static void btreeParseCellPtrIndex( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ + CellInfo *pInfo /* Fill in this structure */ +){ + u8 *pIter; /* For scanning through pCell */ + u32 nPayload; /* Number of bytes of cell payload */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->leaf==0 || pPage->leaf==1 ); + assert( pPage->intKeyLeaf==0 ); + pIter = pCell + pPage->childPtrSize; + nPayload = *pIter; + if( nPayload>=0x80 ){ + u8 *pEnd = &pIter[8]; + nPayload &= 0x7f; + do{ + nPayload = (nPayload<<7) | (*++pIter & 0x7f); + }while( *(pIter)>=0x80 && pIternKey = nPayload; + pInfo->nPayload = nPayload; + pInfo->pPayload = pIter; + testcase( nPayload==pPage->maxLocal ); + testcase( nPayload==pPage->maxLocal+1 ); + if( nPayload<=pPage->maxLocal ){ + /* This is the (easy) common case where the entire payload fits + ** on the local page. No overflow is required. + */ + pInfo->nSize = nPayload + (u16)(pIter - pCell); + if( pInfo->nSize<4 ) pInfo->nSize = 4; + pInfo->nLocal = (u16)nPayload; + }else{ + btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); + } +} +static void btreeParseCell( + MemPage *pPage, /* Page containing the cell */ + int iCell, /* The cell index. First cell is 0 */ + CellInfo *pInfo /* Fill in this structure */ +){ + pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); +} + +/* +** The following routines are implementations of the MemPage.xCellSize +** method. +** +** Compute the total number of bytes that a Cell needs in the cell +** data area of the btree-page. The return number includes the cell +** data header and the local payload, but not any overflow page or +** the space used by the cell pointer. +** +** cellSizePtrNoPayload() => table internal nodes +** cellSizePtr() => all index nodes & table leaf nodes +*/ +static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ + u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ + u8 *pEnd; /* End mark for a varint */ + u32 nSize; /* Size value to return */ + +#ifdef SQLITE_DEBUG + /* The value returned by this function should always be the same as + ** the (CellInfo.nSize) value found by doing a full parse of the + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; + pPage->xParseCell(pPage, pCell, &debuginfo); +#endif + + nSize = *pIter; + if( nSize>=0x80 ){ + pEnd = &pIter[8]; + nSize &= 0x7f; + do{ + nSize = (nSize<<7) | (*++pIter & 0x7f); + }while( *(pIter)>=0x80 && pIterintKey ){ + /* pIter now points at the 64-bit integer key value, a variable length + ** integer. The following block moves pIter to point at the first byte + ** past the end of the key value. */ + pEnd = &pIter[9]; + while( (*pIter++)&0x80 && pItermaxLocal ); + testcase( nSize==pPage->maxLocal+1 ); + if( nSize<=pPage->maxLocal ){ + nSize += (u32)(pIter - pCell); + if( nSize<4 ) nSize = 4; + }else{ + int minLocal = pPage->minLocal; + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); + testcase( nSize==pPage->maxLocal ); + testcase( nSize==pPage->maxLocal+1 ); + if( nSize>pPage->maxLocal ){ + nSize = minLocal; + } + nSize += 4 + (u16)(pIter - pCell); + } + assert( nSize==debuginfo.nSize || CORRUPT_DB ); + return (u16)nSize; +} +static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ + u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ + u8 *pEnd; /* End mark for a varint */ + +#ifdef SQLITE_DEBUG + /* The value returned by this function should always be the same as + ** the (CellInfo.nSize) value found by doing a full parse of the + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; + pPage->xParseCell(pPage, pCell, &debuginfo); +#else + UNUSED_PARAMETER(pPage); +#endif + + assert( pPage->childPtrSize==4 ); + pEnd = pIter + 9; + while( (*pIter++)&0x80 && pIterxCellSize(pPage, findCell(pPage, iCell)); +} +#endif + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** If the cell pCell, part of page pPage contains a pointer +** to an overflow page, insert an entry into the pointer-map +** for the overflow page. +*/ +static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ + CellInfo info; + if( *pRC ) return; + assert( pCell!=0 ); + pPage->xParseCell(pPage, pCell, &info); + if( info.nLocalpBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); + } +} +#endif + + +/* +** Defragment the page given. All Cells are moved to the +** end of the page and all free space is collected into one +** big FreeBlk that occurs in between the header and cell +** pointer array and the cell content area. +** +** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a +** b-tree page so that there are no freeblocks or fragment bytes, all +** unused bytes are contained in the unallocated space region, and all +** cells are packed tightly at the end of the page. +*/ +static int defragmentPage(MemPage *pPage){ + int i; /* Loop counter */ + int pc; /* Address of the i-th cell */ + int hdr; /* Offset to the page header */ + int size; /* Size of a cell */ + int usableSize; /* Number of usable bytes on a page */ + int cellOffset; /* Offset to the cell pointer array */ + int cbrk; /* Offset to the cell content area */ + int nCell; /* Number of cells on the page */ + unsigned char *data; /* The page data */ + unsigned char *temp; /* Temp area for cell content */ + unsigned char *src; /* Source of content */ + int iCellFirst; /* First allowable cell index */ + int iCellLast; /* Last possible cell index */ + + + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( pPage->pBt!=0 ); + assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); + assert( pPage->nOverflow==0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + temp = 0; + src = data = pPage->aData; + hdr = pPage->hdrOffset; + cellOffset = pPage->cellOffset; + nCell = pPage->nCell; + assert( nCell==get2byte(&data[hdr+3]) ); + usableSize = pPage->pBt->usableSize; + cbrk = usableSize; + iCellFirst = cellOffset + 2*nCell; + iCellLast = usableSize - 4; + for(i=0; iiCellLast ){ + return SQLITE_CORRUPT_BKPT; + } + assert( pc>=iCellFirst && pc<=iCellLast ); + size = pPage->xCellSize(pPage, &src[pc]); + cbrk -= size; + if( cbrkusableSize ){ + return SQLITE_CORRUPT_BKPT; + } + assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); + testcase( cbrk+size==usableSize ); + testcase( pc+size==usableSize ); + put2byte(pAddr, cbrk); + if( temp==0 ){ + int x; + if( cbrk==pc ) continue; + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); + x = get2byte(&data[hdr+5]); + memcpy(&temp[x], &data[x], (cbrk+size) - x); + src = temp; + } + memcpy(&data[cbrk], &src[pc], size); + } + assert( cbrk>=iCellFirst ); + put2byte(&data[hdr+5], cbrk); + data[hdr+1] = 0; + data[hdr+2] = 0; + data[hdr+7] = 0; + memset(&data[iCellFirst], 0, cbrk-iCellFirst); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + if( cbrk-iCellFirst!=pPage->nFree ){ + return SQLITE_CORRUPT_BKPT; + } + return SQLITE_OK; +} + +/* +** Search the free-list on page pPg for space to store a cell nByte bytes in +** size. If one can be found, return a pointer to the space and remove it +** from the free-list. +** +** If no suitable space can be found on the free-list, return NULL. +** +** This function may detect corruption within pPg. If corruption is +** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. +** +** Slots on the free list that are between 1 and 3 bytes larger than nByte +** will be ignored if adding the extra space to the fragmentation count +** causes the fragmentation count to exceed 60. +*/ +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ + const int hdr = pPg->hdrOffset; + u8 * const aData = pPg->aData; + int iAddr = hdr + 1; + int pc = get2byte(&aData[iAddr]); + int x; + int usableSize = pPg->pBt->usableSize; + + assert( pc>0 ); + do{ + int size; /* Size of the free slot */ + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of + ** increasing offset. */ + if( pc>usableSize-4 || pc=0 ){ + testcase( x==4 ); + testcase( x==3 ); + if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ + *pRc = SQLITE_CORRUPT_BKPT; + return 0; + }else if( x<4 ){ + /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total + ** number of bytes in fragments may not exceed 60. */ + if( aData[hdr+7]>57 ) return 0; + + /* Remove the slot from the free-list. Update the number of + ** fragmented bytes within the page. */ + memcpy(&aData[iAddr], &aData[pc], 2); + aData[hdr+7] += (u8)x; + }else{ + /* The slot remains on the free-list. Reduce its size to account + ** for the portion used by the new allocation. */ + put2byte(&aData[pc+2], x); + } + return &aData[pc + x]; + } + iAddr = pc; + pc = get2byte(&aData[pc]); + }while( pc ); + + return 0; +} + +/* +** Allocate nByte bytes of space from within the B-Tree page passed +** as the first argument. Write into *pIdx the index into pPage->aData[] +** of the first byte of allocated space. Return either SQLITE_OK or +** an error code (usually SQLITE_CORRUPT). +** +** The caller guarantees that there is sufficient space to make the +** allocation. This routine might need to defragment in order to bring +** all the space together, however. This routine will avoid using +** the first two bytes past the cell pointer area since presumably this +** allocation is being made in order to insert a new cell, so we will +** also end up needing a new cell pointer. +*/ +static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ + const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ + u8 * const data = pPage->aData; /* Local cache of pPage->aData */ + int top; /* First byte of cell content area */ + int rc = SQLITE_OK; /* Integer return code */ + int gap; /* First byte of gap between cell pointers and cell content */ + + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( pPage->pBt ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( nByte>=0 ); /* Minimum cell size is 4 */ + assert( pPage->nFree>=nByte ); + assert( pPage->nOverflow==0 ); + assert( nByte < (int)(pPage->pBt->usableSize-8) ); + + assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); + gap = pPage->cellOffset + 2*pPage->nCell; + assert( gap<=65536 ); + /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size + ** and the reserved space is zero (the usual value for reserved space) + ** then the cell content offset of an empty page wants to be 65536. + ** However, that integer is too large to be stored in a 2-byte unsigned + ** integer, so a value of 0 is used in its place. */ + top = get2byte(&data[hdr+5]); + assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ + if( gap>top ){ + if( top==0 && pPage->pBt->usableSize==65536 ){ + top = 65536; + }else{ + return SQLITE_CORRUPT_BKPT; + } + } + + /* If there is enough space between gap and top for one more cell pointer + ** array entry offset, and if the freelist is not empty, then search the + ** freelist looking for a free slot big enough to satisfy the request. + */ + testcase( gap+2==top ); + testcase( gap+1==top ); + testcase( gap==top ); + if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ + u8 *pSpace = pageFindSlot(pPage, nByte, &rc); + if( pSpace ){ + assert( pSpace>=data && (pSpace - data)<65536 ); + *pIdx = (int)(pSpace - data); + return SQLITE_OK; + }else if( rc ){ + return rc; + } + } + + /* The request could not be fulfilled using a freelist slot. Check + ** to see if defragmentation is necessary. + */ + testcase( gap+2+nByte==top ); + if( gap+2+nByte>top ){ + assert( pPage->nCell>0 || CORRUPT_DB ); + rc = defragmentPage(pPage); + if( rc ) return rc; + top = get2byteNotZero(&data[hdr+5]); + assert( gap+nByte<=top ); + } + + + /* Allocate memory from the gap in between the cell pointer array + ** and the cell content area. The btreeInitPage() call has already + ** validated the freelist. Given that the freelist is valid, there + ** is no way that the allocation can extend off the end of the page. + ** The assert() below verifies the previous sentence. + */ + top -= nByte; + put2byte(&data[hdr+5], top); + assert( top+nByte <= (int)pPage->pBt->usableSize ); + *pIdx = top; + return SQLITE_OK; +} + +/* +** Return a section of the pPage->aData to the freelist. +** The first byte of the new free block is pPage->aData[iStart] +** and the size of the block is iSize bytes. +** +** Adjacent freeblocks are coalesced. +** +** Note that even though the freeblock list was checked by btreeInitPage(), +** that routine will not detect overlap between cells or freeblocks. Nor +** does it detect cells or freeblocks that encrouch into the reserved bytes +** at the end of the page. So do additional corruption checks inside this +** routine and return SQLITE_CORRUPT if any problems are found. +*/ +static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ + u16 iPtr; /* Address of ptr to next freeblock */ + u16 iFreeBlk; /* Address of the next freeblock */ + u8 hdr; /* Page header size. 0 or 100 */ + u8 nFrag = 0; /* Reduction in fragmentation */ + u16 iOrigSize = iSize; /* Original value of iSize */ + u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ + u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ + unsigned char *data = pPage->aData; /* Page content */ + + assert( pPage->pBt!=0 ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); + assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( iSize>=4 ); /* Minimum cell size is 4 */ + assert( iStart<=iLast ); + + /* Overwrite deleted information with zeros when the secure_delete + ** option is enabled */ + if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ + memset(&data[iStart], 0, iSize); + } + + /* The list of freeblocks must be in ascending order. Find the + ** spot on the list where iStart should be inserted. + */ + hdr = pPage->hdrOffset; + iPtr = hdr + 1; + if( data[iPtr+1]==0 && data[iPtr]==0 ){ + iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ + }else{ + while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlkiLast ) return SQLITE_CORRUPT_BKPT; + assert( iFreeBlk>iPtr || iFreeBlk==0 ); + + /* At this point: + ** iFreeBlk: First freeblock after iStart, or zero if none + ** iPtr: The address of a pointer to iFreeBlk + ** + ** Check to see if iFreeBlk should be coalesced onto the end of iStart. + */ + if( iFreeBlk && iEnd+3>=iFreeBlk ){ + nFrag = iFreeBlk - iEnd; + if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; + iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); + if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT; + iSize = iEnd - iStart; + iFreeBlk = get2byte(&data[iFreeBlk]); + } + + /* If iPtr is another freeblock (that is, if iPtr is not the freelist + ** pointer in the page header) then check to see if iStart should be + ** coalesced onto the end of iPtr. + */ + if( iPtr>hdr+1 ){ + int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); + if( iPtrEnd+3>=iStart ){ + if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; + nFrag += iStart - iPtrEnd; + iSize = iEnd - iPtr; + iStart = iPtr; + } + } + if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT; + data[hdr+7] -= nFrag; + } + if( iStart==get2byte(&data[hdr+5]) ){ + /* The new freeblock is at the beginning of the cell content area, + ** so just extend the cell content area rather than create another + ** freelist entry */ + if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT; + put2byte(&data[hdr+1], iFreeBlk); + put2byte(&data[hdr+5], iEnd); + }else{ + /* Insert the new freeblock into the freelist */ + put2byte(&data[iPtr], iStart); + put2byte(&data[iStart], iFreeBlk); + put2byte(&data[iStart+2], iSize); + } + pPage->nFree += iOrigSize; + return SQLITE_OK; +} + +/* +** Decode the flags byte (the first byte of the header) for a page +** and initialize fields of the MemPage structure accordingly. +** +** Only the following combinations are supported. Anything different +** indicates a corrupt database files: +** +** PTF_ZERODATA +** PTF_ZERODATA | PTF_LEAF +** PTF_LEAFDATA | PTF_INTKEY +** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF +*/ +static int decodeFlags(MemPage *pPage, int flagByte){ + BtShared *pBt; /* A copy of pPage->pBt */ + + assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); + flagByte &= ~PTF_LEAF; + pPage->childPtrSize = 4-4*pPage->leaf; + pPage->xCellSize = cellSizePtr; + pBt = pPage->pBt; + if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ + /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an + ** interior table b-tree page. */ + assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); + /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a + ** leaf table b-tree page. */ + assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); + pPage->intKey = 1; + if( pPage->leaf ){ + pPage->intKeyLeaf = 1; + pPage->xParseCell = btreeParseCellPtr; + }else{ + pPage->intKeyLeaf = 0; + pPage->xCellSize = cellSizePtrNoPayload; + pPage->xParseCell = btreeParseCellPtrNoPayload; + } + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + }else if( flagByte==PTF_ZERODATA ){ + /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an + ** interior index b-tree page. */ + assert( (PTF_ZERODATA)==2 ); + /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a + ** leaf index b-tree page. */ + assert( (PTF_ZERODATA|PTF_LEAF)==10 ); + pPage->intKey = 0; + pPage->intKeyLeaf = 0; + pPage->xParseCell = btreeParseCellPtrIndex; + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; + }else{ + /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is + ** an error. */ + return SQLITE_CORRUPT_BKPT; + } + pPage->max1bytePayload = pBt->max1bytePayload; + return SQLITE_OK; +} + +/* +** Initialize the auxiliary information for a disk block. +** +** Return SQLITE_OK on success. If we see that the page does +** not contain a well-formed database page, then return +** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not +** guarantee that the page is well-formed. It only shows that +** we failed to detect any corruption. +*/ +static int btreeInitPage(MemPage *pPage){ + + assert( pPage->pBt!=0 ); + assert( pPage->pBt->db!=0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); + assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); + assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); + + if( !pPage->isInit ){ + u16 pc; /* Address of a freeblock within pPage->aData[] */ + u8 hdr; /* Offset to beginning of page header */ + u8 *data; /* Equal to pPage->aData */ + BtShared *pBt; /* The main btree structure */ + int usableSize; /* Amount of usable space on each page */ + u16 cellOffset; /* Offset from start of page to first cell pointer */ + int nFree; /* Number of unused bytes on the page */ + int top; /* First byte of the cell content area */ + int iCellFirst; /* First allowable cell or freeblock offset */ + int iCellLast; /* Last possible cell or freeblock offset */ + + pBt = pPage->pBt; + + hdr = pPage->hdrOffset; + data = pPage->aData; + /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating + ** the b-tree page type. */ + if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); + pPage->nOverflow = 0; + usableSize = pBt->usableSize; + pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; + pPage->aDataEnd = &data[usableSize]; + pPage->aCellIdx = &data[cellOffset]; + pPage->aDataOfst = &data[pPage->childPtrSize]; + /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates + ** the start of the cell content area. A zero value for this integer is + ** interpreted as 65536. */ + top = get2byteNotZero(&data[hdr+5]); + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the + ** number of cells on the page. */ + pPage->nCell = get2byte(&data[hdr+3]); + if( pPage->nCell>MX_CELL(pBt) ){ + /* To many cells for a single page. The page must be corrupt */ + return SQLITE_CORRUPT_BKPT; + } + testcase( pPage->nCell==MX_CELL(pBt) ); + /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only + ** possible for a root page of a table that contains no rows) then the + ** offset to the cell content area will equal the page size minus the + ** bytes of reserved space. */ + assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); + + /* A malformed database page might cause us to read past the end + ** of page when parsing a cell. + ** + ** The following block of code checks early to see if a cell extends + ** past the end of a page boundary and causes SQLITE_CORRUPT to be + ** returned if it does. + */ + iCellFirst = cellOffset + 2*pPage->nCell; + iCellLast = usableSize - 4; + if( pBt->db->flags & SQLITE_CellSizeCk ){ + int i; /* Index into the cell pointer array */ + int sz; /* Size of a cell */ + + if( !pPage->leaf ) iCellLast--; + for(i=0; inCell; i++){ + pc = get2byteAligned(&data[cellOffset+i*2]); + testcase( pc==iCellFirst ); + testcase( pc==iCellLast ); + if( pciCellLast ){ + return SQLITE_CORRUPT_BKPT; + } + sz = pPage->xCellSize(pPage, &data[pc]); + testcase( pc+sz==usableSize ); + if( pc+sz>usableSize ){ + return SQLITE_CORRUPT_BKPT; + } + } + if( !pPage->leaf ) iCellLast++; + } + + /* Compute the total free space on the page + ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the + ** start of the first freeblock on the page, or is zero if there are no + ** freeblocks. */ + pc = get2byte(&data[hdr+1]); + nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ + while( pc>0 ){ + u16 next, size; + if( pciCellLast ){ + /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will + ** always be at least one cell before the first freeblock. + ** + ** Or, the freeblock is off the end of the page + */ + return SQLITE_CORRUPT_BKPT; + } + next = get2byte(&data[pc]); + size = get2byte(&data[pc+2]); + if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ + /* Free blocks must be in ascending order. And the last byte of + ** the free-block must lie on the database page. */ + return SQLITE_CORRUPT_BKPT; + } + nFree = nFree + size; + pc = next; + } + + /* At this point, nFree contains the sum of the offset to the start + ** of the cell-content area plus the number of free bytes within + ** the cell-content area. If this is greater than the usable-size + ** of the page, then the page must be corrupted. This check also + ** serves to verify that the offset to the start of the cell-content + ** area, according to the page header, lies within the page. + */ + if( nFree>usableSize ){ + return SQLITE_CORRUPT_BKPT; + } + pPage->nFree = (u16)(nFree - iCellFirst); + pPage->isInit = 1; + } + return SQLITE_OK; +} + +/* +** Set up a raw page so that it looks like a database page holding +** no entries. +*/ +static void zeroPage(MemPage *pPage, int flags){ + unsigned char *data = pPage->aData; + BtShared *pBt = pPage->pBt; + u8 hdr = pPage->hdrOffset; + u16 first; + + assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); + assert( sqlite3PagerGetData(pPage->pDbPage) == data ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pBt->btsFlags & BTS_SECURE_DELETE ){ + memset(&data[hdr], 0, pBt->usableSize - hdr); + } + data[hdr] = (char)flags; + first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8); + memset(&data[hdr+1], 0, 4); + data[hdr+7] = 0; + put2byte(&data[hdr+5], pBt->usableSize); + pPage->nFree = (u16)(pBt->usableSize - first); + decodeFlags(pPage, flags); + pPage->cellOffset = first; + pPage->aDataEnd = &data[pBt->usableSize]; + pPage->aCellIdx = &data[first]; + pPage->aDataOfst = &data[pPage->childPtrSize]; + pPage->nOverflow = 0; + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); + pPage->nCell = 0; + pPage->isInit = 1; +} + + +/* +** Convert a DbPage obtained from the pager into a MemPage used by +** the btree layer. +*/ +static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ + MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); + if( pgno!=pPage->pgno ){ + pPage->aData = sqlite3PagerGetData(pDbPage); + pPage->pDbPage = pDbPage; + pPage->pBt = pBt; + pPage->pgno = pgno; + pPage->hdrOffset = pgno==1 ? 100 : 0; + } + assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); + return pPage; +} + +/* +** Get a page from the pager. Initialize the MemPage.pBt and +** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). +** +** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care +** about the content of the page at this time. So do not go to the disk +** to fetch the content. Just fill in the content with zeros for now. +** If in the future we call sqlite3PagerWrite() on this page, that +** means we have started to be concerned about content and the disk +** read should occur at that point. +*/ +static int btreeGetPage( + BtShared *pBt, /* The btree */ + Pgno pgno, /* Number of the page to fetch */ + MemPage **ppPage, /* Return the page in this parameter */ + int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ +){ + int rc; + DbPage *pDbPage; + + assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); + assert( sqlite3_mutex_held(pBt->mutex) ); + rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); + if( rc ) return rc; + *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); + return SQLITE_OK; +} + +/* +** Retrieve a page from the pager cache. If the requested page is not +** already in the pager cache return NULL. Initialize the MemPage.pBt and +** MemPage.aData elements if needed. +*/ +static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ + DbPage *pDbPage; + assert( sqlite3_mutex_held(pBt->mutex) ); + pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); + if( pDbPage ){ + return btreePageFromDbPage(pDbPage, pgno, pBt); + } + return 0; +} + +/* +** Return the size of the database file in pages. If there is any kind of +** error, return ((unsigned int)-1). +*/ +static Pgno btreePagecount(BtShared *pBt){ + return pBt->nPage; +} +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ + assert( sqlite3BtreeHoldsMutex(p) ); + assert( ((p->pBt->nPage)&0x8000000)==0 ); + return btreePagecount(p->pBt); +} + +/* +** Get a page from the pager and initialize it. +** +** If pCur!=0 then the page is being fetched as part of a moveToChild() +** call. Do additional sanity checking on the page in this case. +** And if the fetch fails, this routine must decrement pCur->iPage. +** +** The page is fetched as read-write unless pCur is not NULL and is +** a read-only cursor. +** +** If an error occurs, then *ppPage is undefined. It +** may remain unchanged, or it may be set to an invalid value. +*/ +static int getAndInitPage( + BtShared *pBt, /* The database file */ + Pgno pgno, /* Number of the page to get */ + MemPage **ppPage, /* Write the page pointer here */ + BtCursor *pCur, /* Cursor to receive the page, or NULL */ + int bReadOnly /* True for a read-only page */ +){ + int rc; + DbPage *pDbPage; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] ); + assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); + assert( pCur==0 || pCur->iPage>0 ); + + if( pgno>btreePagecount(pBt) ){ + rc = SQLITE_CORRUPT_BKPT; + goto getAndInitPage_error; + } + rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); + if( rc ){ + goto getAndInitPage_error; + } + *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); + if( (*ppPage)->isInit==0 ){ + btreePageFromDbPage(pDbPage, pgno, pBt); + rc = btreeInitPage(*ppPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + goto getAndInitPage_error; + } + } + assert( (*ppPage)->pgno==pgno ); + assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); + + /* If obtaining a child page for a cursor, we must verify that the page is + ** compatible with the root page. */ + if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ + rc = SQLITE_CORRUPT_BKPT; + releasePage(*ppPage); + goto getAndInitPage_error; + } + return SQLITE_OK; + +getAndInitPage_error: + if( pCur ) pCur->iPage--; + testcase( pgno==0 ); + assert( pgno!=0 || rc==SQLITE_CORRUPT ); + return rc; +} + +/* +** Release a MemPage. This should be called once for each prior +** call to btreeGetPage. +*/ +static void releasePageNotNull(MemPage *pPage){ + assert( pPage->aData ); + assert( pPage->pBt ); + assert( pPage->pDbPage!=0 ); + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); + assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + sqlite3PagerUnrefNotNull(pPage->pDbPage); +} +static void releasePage(MemPage *pPage){ + if( pPage ) releasePageNotNull(pPage); +} + +/* +** Get an unused page. +** +** This works just like btreeGetPage() with the addition: +** +** * If the page is already in use for some other purpose, immediately +** release it and return an SQLITE_CURRUPT error. +** * Make sure the isInit flag is clear +*/ +static int btreeGetUnusedPage( + BtShared *pBt, /* The btree */ + Pgno pgno, /* Number of the page to fetch */ + MemPage **ppPage, /* Return the page in this parameter */ + int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ +){ + int rc = btreeGetPage(pBt, pgno, ppPage, flags); + if( rc==SQLITE_OK ){ + if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ + releasePage(*ppPage); + *ppPage = 0; + return SQLITE_CORRUPT_BKPT; + } + (*ppPage)->isInit = 0; + }else{ + *ppPage = 0; + } + return rc; +} + + +/* +** During a rollback, when the pager reloads information into the cache +** so that the cache is restored to its original state at the start of +** the transaction, for each page restored this routine is called. +** +** This routine needs to reset the extra data section at the end of the +** page to agree with the restored data. +*/ +static void pageReinit(DbPage *pData){ + MemPage *pPage; + pPage = (MemPage *)sqlite3PagerGetExtra(pData); + assert( sqlite3PagerPageRefcount(pData)>0 ); + if( pPage->isInit ){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + pPage->isInit = 0; + if( sqlite3PagerPageRefcount(pData)>1 ){ + /* pPage might not be a btree page; it might be an overflow page + ** or ptrmap page or a free page. In those cases, the following + ** call to btreeInitPage() will likely return SQLITE_CORRUPT. + ** But no harm is done by this. And it is very important that + ** btreeInitPage() be called on every btree page so we make + ** the call for every page that comes in for re-initing. */ + btreeInitPage(pPage); + } + } +} + +/* +** Invoke the busy handler for a btree. +*/ +static int btreeInvokeBusyHandler(void *pArg){ + BtShared *pBt = (BtShared*)pArg; + assert( pBt->db ); + assert( sqlite3_mutex_held(pBt->db->mutex) ); + return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); +} + +/* +** Open a database file. +** +** zFilename is the name of the database file. If zFilename is NULL +** then an ephemeral database is created. The ephemeral database might +** be exclusively in memory, or it might use a disk-based memory cache. +** Either way, the ephemeral database will be automatically deleted +** when sqlite3BtreeClose() is called. +** +** If zFilename is ":memory:" then an in-memory database is created +** that is automatically destroyed when it is closed. +** +** The "flags" parameter is a bitmask that might contain bits like +** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. +** +** If the database is already opened in the same database connection +** and we are in shared cache mode, then the open will fail with an +** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared +** objects in the same database connection since doing so will lead +** to problems with locking. +*/ +SQLITE_PRIVATE int sqlite3BtreeOpen( + sqlite3_vfs *pVfs, /* VFS to use for this b-tree */ + const char *zFilename, /* Name of the file containing the BTree database */ + sqlite3 *db, /* Associated database handle */ + Btree **ppBtree, /* Pointer to new Btree object written here */ + int flags, /* Options */ + int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ +){ + BtShared *pBt = 0; /* Shared part of btree structure */ + Btree *p; /* Handle to return */ + sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ + int rc = SQLITE_OK; /* Result code from this function */ + u8 nReserve; /* Byte of unused space on each page */ + unsigned char zDbHeader[100]; /* Database header content */ + + /* True if opening an ephemeral, temporary database */ + const int isTempDb = zFilename==0 || zFilename[0]==0; + + /* Set the variable isMemdb to true for an in-memory database, or + ** false for a file-based database. + */ +#ifdef SQLITE_OMIT_MEMORYDB + const int isMemdb = 0; +#else + const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) + || (isTempDb && sqlite3TempInMemory(db)) + || (vfsFlags & SQLITE_OPEN_MEMORY)!=0; +#endif + + assert( db!=0 ); + assert( pVfs!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ + + /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ + assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); + + /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ + assert( (flags & BTREE_SINGLE)==0 || isTempDb ); + + if( isMemdb ){ + flags |= BTREE_MEMORY; + } + if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ + vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; + } + p = sqlite3MallocZero(sizeof(Btree)); + if( !p ){ + return SQLITE_NOMEM_BKPT; + } + p->inTrans = TRANS_NONE; + p->db = db; +#ifndef SQLITE_OMIT_SHARED_CACHE + p->lock.pBtree = p; + p->lock.iTable = 1; +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* + ** If this Btree is a candidate for shared cache, try to find an + ** existing BtShared object that we can share with + */ + if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ + if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ + int nFilename = sqlite3Strlen30(zFilename)+1; + int nFullPathname = pVfs->mxPathname+1; + char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)); + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) + + p->sharable = 1; + if( !zFullPathname ){ + sqlite3_free(p); + return SQLITE_NOMEM_BKPT; + } + if( isMemdb ){ + memcpy(zFullPathname, zFilename, nFilename); + }else{ + rc = sqlite3OsFullPathname(pVfs, zFilename, + nFullPathname, zFullPathname); + if( rc ){ + sqlite3_free(zFullPathname); + sqlite3_free(p); + return rc; + } + } +#if SQLITE_THREADSAFE + mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); + sqlite3_mutex_enter(mutexOpen); + mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex_enter(mutexShared); +#endif + for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ + assert( pBt->nRef>0 ); + if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0)) + && sqlite3PagerVfs(pBt->pPager)==pVfs ){ + int iDb; + for(iDb=db->nDb-1; iDb>=0; iDb--){ + Btree *pExisting = db->aDb[iDb].pBt; + if( pExisting && pExisting->pBt==pBt ){ + sqlite3_mutex_leave(mutexShared); + sqlite3_mutex_leave(mutexOpen); + sqlite3_free(zFullPathname); + sqlite3_free(p); + return SQLITE_CONSTRAINT; + } + } + p->pBt = pBt; + pBt->nRef++; + break; + } + } + sqlite3_mutex_leave(mutexShared); + sqlite3_free(zFullPathname); + } +#ifdef SQLITE_DEBUG + else{ + /* In debug mode, we mark all persistent databases as sharable + ** even when they are not. This exercises the locking code and + ** gives more opportunity for asserts(sqlite3_mutex_held()) + ** statements to find locking problems. + */ + p->sharable = 1; + } +#endif + } +#endif + if( pBt==0 ){ + /* + ** The following asserts make sure that structures used by the btree are + ** the right size. This is to guard against size changes that result + ** when compiling on a different architecture. + */ + assert( sizeof(i64)==8 ); + assert( sizeof(u64)==8 ); + assert( sizeof(u32)==4 ); + assert( sizeof(u16)==2 ); + assert( sizeof(Pgno)==4 ); + + pBt = sqlite3MallocZero( sizeof(*pBt) ); + if( pBt==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto btree_open_out; + } + rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, + EXTRA_SIZE, flags, vfsFlags, pageReinit); + if( rc==SQLITE_OK ){ + sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); + rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); + } + if( rc!=SQLITE_OK ){ + goto btree_open_out; + } + pBt->openFlags = (u8)flags; + pBt->db = db; + sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); + p->pBt = pBt; + + pBt->pCursor = 0; + pBt->pPage1 = 0; + if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; +#ifdef SQLITE_SECURE_DELETE + pBt->btsFlags |= BTS_SECURE_DELETE; +#endif + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is + ** determined by the 2-byte integer located at an offset of 16 bytes from + ** the beginning of the database file. */ + pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); + if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE + || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ + pBt->pageSize = 0; +#ifndef SQLITE_OMIT_AUTOVACUUM + /* If the magic name ":memory:" will create an in-memory database, then + ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if + ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if + ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a + ** regular file-name. In this case the auto-vacuum applies as per normal. + */ + if( zFilename && !isMemdb ){ + pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); + pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); + } +#endif + nReserve = 0; + }else{ + /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is + ** determined by the one-byte unsigned integer found at an offset of 20 + ** into the database file header. */ + nReserve = zDbHeader[20]; + pBt->btsFlags |= BTS_PAGESIZE_FIXED; +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); + pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); +#endif + } + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); + if( rc ) goto btree_open_out; + pBt->usableSize = pBt->pageSize - nReserve; + assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* Add the new BtShared object to the linked list sharable BtShareds. + */ + if( p->sharable ){ + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) + pBt->nRef = 1; + MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) + if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ + pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); + if( pBt->mutex==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto btree_open_out; + } + } + sqlite3_mutex_enter(mutexShared); + pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); + GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; + sqlite3_mutex_leave(mutexShared); + } +#endif + } + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* If the new Btree uses a sharable pBtShared, then link the new + ** Btree into the list of all sharable Btrees for the same connection. + ** The list is kept in ascending order by pBt address. + */ + if( p->sharable ){ + int i; + Btree *pSib; + for(i=0; inDb; i++){ + if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ + while( pSib->pPrev ){ pSib = pSib->pPrev; } + if( (uptr)p->pBt<(uptr)pSib->pBt ){ + p->pNext = pSib; + p->pPrev = 0; + pSib->pPrev = p; + }else{ + while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){ + pSib = pSib->pNext; + } + p->pNext = pSib->pNext; + p->pPrev = pSib; + if( p->pNext ){ + p->pNext->pPrev = p; + } + pSib->pNext = p; + } + break; + } + } + } +#endif + *ppBtree = p; + +btree_open_out: + if( rc!=SQLITE_OK ){ + if( pBt && pBt->pPager ){ + sqlite3PagerClose(pBt->pPager); + } + sqlite3_free(pBt); + sqlite3_free(p); + *ppBtree = 0; + }else{ + /* If the B-Tree was successfully opened, set the pager-cache size to the + ** default value. Except, when opening on an existing shared pager-cache, + ** do not change the pager-cache size. + */ + if( sqlite3BtreeSchema(p, 0, 0)==0 ){ + sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); + } + } + if( mutexOpen ){ + assert( sqlite3_mutex_held(mutexOpen) ); + sqlite3_mutex_leave(mutexOpen); + } + return rc; +} + +/* +** Decrement the BtShared.nRef counter. When it reaches zero, +** remove the BtShared structure from the sharing list. Return +** true if the BtShared.nRef counter reaches zero and return +** false if it is still positive. +*/ +static int removeFromSharingList(BtShared *pBt){ +#ifndef SQLITE_OMIT_SHARED_CACHE + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) + BtShared *pList; + int removed = 0; + + assert( sqlite3_mutex_notheld(pBt->mutex) ); + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) + sqlite3_mutex_enter(pMaster); + pBt->nRef--; + if( pBt->nRef<=0 ){ + if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ + GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; + }else{ + pList = GLOBAL(BtShared*,sqlite3SharedCacheList); + while( ALWAYS(pList) && pList->pNext!=pBt ){ + pList=pList->pNext; + } + if( ALWAYS(pList) ){ + pList->pNext = pBt->pNext; + } + } + if( SQLITE_THREADSAFE ){ + sqlite3_mutex_free(pBt->mutex); + } + removed = 1; + } + sqlite3_mutex_leave(pMaster); + return removed; +#else + return 1; +#endif +} + +/* +** Make sure pBt->pTmpSpace points to an allocation of +** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child +** pointer. +*/ +static void allocateTempSpace(BtShared *pBt){ + if( !pBt->pTmpSpace ){ + pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); + + /* One of the uses of pBt->pTmpSpace is to format cells before + ** inserting them into a leaf page (function fillInCell()). If + ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes + ** by the various routines that manipulate binary cells. Which + ** can mean that fillInCell() only initializes the first 2 or 3 + ** bytes of pTmpSpace, but that the first 4 bytes are copied from + ** it into a database page. This is not actually a problem, but it + ** does cause a valgrind error when the 1 or 2 bytes of unitialized + ** data is passed to system call write(). So to avoid this error, + ** zero the first 4 bytes of temp space here. + ** + ** Also: Provide four bytes of initialized space before the + ** beginning of pTmpSpace as an area available to prepend the + ** left-child pointer to the beginning of a cell. + */ + if( pBt->pTmpSpace ){ + memset(pBt->pTmpSpace, 0, 8); + pBt->pTmpSpace += 4; + } + } +} + +/* +** Free the pBt->pTmpSpace allocation +*/ +static void freeTempSpace(BtShared *pBt){ + if( pBt->pTmpSpace ){ + pBt->pTmpSpace -= 4; + sqlite3PageFree(pBt->pTmpSpace); + pBt->pTmpSpace = 0; + } +} + +/* +** Close an open database and invalidate all cursors. +*/ +SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){ + BtShared *pBt = p->pBt; + BtCursor *pCur; + + /* Close all cursors opened via this handle. */ + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + pCur = pBt->pCursor; + while( pCur ){ + BtCursor *pTmp = pCur; + pCur = pCur->pNext; + if( pTmp->pBtree==p ){ + sqlite3BtreeCloseCursor(pTmp); + } + } + + /* Rollback any active transaction and free the handle structure. + ** The call to sqlite3BtreeRollback() drops any table-locks held by + ** this handle. + */ + sqlite3BtreeRollback(p, SQLITE_OK, 0); + sqlite3BtreeLeave(p); + + /* If there are still other outstanding references to the shared-btree + ** structure, return now. The remainder of this procedure cleans + ** up the shared-btree. + */ + assert( p->wantToLock==0 && p->locked==0 ); + if( !p->sharable || removeFromSharingList(pBt) ){ + /* The pBt is no longer on the sharing list, so we can access + ** it without having to hold the mutex. + ** + ** Clean out and delete the BtShared object. + */ + assert( !pBt->pCursor ); + sqlite3PagerClose(pBt->pPager); + if( pBt->xFreeSchema && pBt->pSchema ){ + pBt->xFreeSchema(pBt->pSchema); + } + sqlite3DbFree(0, pBt->pSchema); + freeTempSpace(pBt); + sqlite3_free(pBt); + } + +#ifndef SQLITE_OMIT_SHARED_CACHE + assert( p->wantToLock==0 ); + assert( p->locked==0 ); + if( p->pPrev ) p->pPrev->pNext = p->pNext; + if( p->pNext ) p->pNext->pPrev = p->pPrev; +#endif + + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Change the "soft" limit on the number of pages in the cache. +** Unused and unmodified pages will be recycled when the number of +** pages in the cache exceeds this soft limit. But the size of the +** cache is allowed to grow larger than this limit if it contains +** dirty pages or pages still in active use. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ + BtShared *pBt = p->pBt; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + sqlite3PagerSetCachesize(pBt->pPager, mxPage); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} + +/* +** Change the "spill" limit on the number of pages in the cache. +** If the number of pages exceeds this limit during a write transaction, +** the pager might attempt to "spill" pages to the journal early in +** order to free up memory. +** +** The value returned is the current spill size. If zero is passed +** as an argument, no changes are made to the spill size setting, so +** using mxPage of 0 is a way to query the current spill size. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){ + BtShared *pBt = p->pBt; + int res; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage); + sqlite3BtreeLeave(p); + return res; +} + +#if SQLITE_MAX_MMAP_SIZE>0 +/* +** Change the limit on the amount of the database file that may be +** memory mapped. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){ + BtShared *pBt = p->pBt; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + sqlite3PagerSetMmapLimit(pBt->pPager, szMmap); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} +#endif /* SQLITE_MAX_MMAP_SIZE>0 */ + +/* +** Change the way data is synced to disk in order to increase or decrease +** how well the database resists damage due to OS crashes and power +** failures. Level 1 is the same as asynchronous (no syncs() occur and +** there is a high probability of damage) Level 2 is the default. There +** is a very low but non-zero probability of damage. Level 3 reduces the +** probability of damage to near zero but with a write performance reduction. +*/ +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags( + Btree *p, /* The btree to set the safety level on */ + unsigned pgFlags /* Various PAGER_* flags */ +){ + BtShared *pBt = p->pBt; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + sqlite3PagerSetFlags(pBt->pPager, pgFlags); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} +#endif + +/* +** Change the default pages size and the number of reserved bytes per page. +** Or, if the page size has already been fixed, return SQLITE_READONLY +** without changing anything. +** +** The page size must be a power of 2 between 512 and 65536. If the page +** size supplied does not meet this constraint then the page size is not +** changed. +** +** Page sizes are constrained to be a power of two so that the region +** of the database file used for locking (beginning at PENDING_BYTE, +** the first byte past the 1GB boundary, 0x40000000) needs to occur +** at the beginning of a page. +** +** If parameter nReserve is less than zero, then the number of reserved +** bytes per page is left unchanged. +** +** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size +** and autovacuum mode can no longer be changed. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ + int rc = SQLITE_OK; + BtShared *pBt = p->pBt; + assert( nReserve>=-1 && nReserve<=255 ); + sqlite3BtreeEnter(p); +#if SQLITE_HAS_CODEC + if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve; +#endif + if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ + sqlite3BtreeLeave(p); + return SQLITE_READONLY; + } + if( nReserve<0 ){ + nReserve = pBt->pageSize - pBt->usableSize; + } + assert( nReserve>=0 && nReserve<=255 ); + if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && + ((pageSize-1)&pageSize)==0 ){ + assert( (pageSize & 7)==0 ); + assert( !pBt->pCursor ); + pBt->pageSize = (u32)pageSize; + freeTempSpace(pBt); + } + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); + pBt->usableSize = pBt->pageSize - (u16)nReserve; + if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED; + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Return the currently defined page size +*/ +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){ + return p->pBt->pageSize; +} + +/* +** This function is similar to sqlite3BtreeGetReserve(), except that it +** may only be called if it is guaranteed that the b-tree mutex is already +** held. +** +** This is useful in one special case in the backup API code where it is +** known that the shared b-tree mutex is held, but the mutex on the +** database handle that owns *p is not. In this case if sqlite3BtreeEnter() +** were to be called, it might collide with some other operation on the +** database handle that owns *p, causing undefined behavior. +*/ +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){ + int n; + assert( sqlite3_mutex_held(p->pBt->mutex) ); + n = p->pBt->pageSize - p->pBt->usableSize; + return n; +} + +/* +** Return the number of bytes of space at the end of every page that +** are intentually left unused. This is the "reserved" space that is +** sometimes used by extensions. +** +** If SQLITE_HAS_MUTEX is defined then the number returned is the +** greater of the current reserved space and the maximum requested +** reserve space. +*/ +SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree *p){ + int n; + sqlite3BtreeEnter(p); + n = sqlite3BtreeGetReserveNoMutex(p); +#ifdef SQLITE_HAS_CODEC + if( npBt->optimalReserve ) n = p->pBt->optimalReserve; +#endif + sqlite3BtreeLeave(p); + return n; +} + + +/* +** Set the maximum page count for a database if mxPage is positive. +** No changes are made if mxPage is 0 or negative. +** Regardless of the value of mxPage, return the maximum page count. +*/ +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ + int n; + sqlite3BtreeEnter(p); + n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); + sqlite3BtreeLeave(p); + return n; +} + +/* +** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1, +** then make no changes. Always return the value of the BTS_SECURE_DELETE +** setting after the change. +*/ +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ + int b; + if( p==0 ) return 0; + sqlite3BtreeEnter(p); + if( newFlag>=0 ){ + p->pBt->btsFlags &= ~BTS_SECURE_DELETE; + if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE; + } + b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0; + sqlite3BtreeLeave(p); + return b; +} + +/* +** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' +** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it +** is disabled. The default value for the auto-vacuum property is +** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ +#ifdef SQLITE_OMIT_AUTOVACUUM + return SQLITE_READONLY; +#else + BtShared *pBt = p->pBt; + int rc = SQLITE_OK; + u8 av = (u8)autoVacuum; + + sqlite3BtreeEnter(p); + if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){ + rc = SQLITE_READONLY; + }else{ + pBt->autoVacuum = av ?1:0; + pBt->incrVacuum = av==2 ?1:0; + } + sqlite3BtreeLeave(p); + return rc; +#endif +} + +/* +** Return the value of the 'auto-vacuum' property. If auto-vacuum is +** enabled 1 is returned. Otherwise 0. +*/ +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){ +#ifdef SQLITE_OMIT_AUTOVACUUM + return BTREE_AUTOVACUUM_NONE; +#else + int rc; + sqlite3BtreeEnter(p); + rc = ( + (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: + (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: + BTREE_AUTOVACUUM_INCR + ); + sqlite3BtreeLeave(p); + return rc; +#endif +} + + +/* +** Get a reference to pPage1 of the database file. This will +** also acquire a readlock on that file. +** +** SQLITE_OK is returned on success. If the file is not a +** well-formed database file, then SQLITE_CORRUPT is returned. +** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM +** is returned if we run out of memory. +*/ +static int lockBtree(BtShared *pBt){ + int rc; /* Result code from subfunctions */ + MemPage *pPage1; /* Page 1 of the database file */ + int nPage; /* Number of pages in the database */ + int nPageFile = 0; /* Number of pages in the database file */ + int nPageHeader; /* Number of pages in the database according to hdr */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pBt->pPage1==0 ); + rc = sqlite3PagerSharedLock(pBt->pPager); + if( rc!=SQLITE_OK ) return rc; + rc = btreeGetPage(pBt, 1, &pPage1, 0); + if( rc!=SQLITE_OK ) return rc; + + /* Do some checking to help insure the file we opened really is + ** a valid database file. + */ + nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); + sqlite3PagerPagecount(pBt->pPager, &nPageFile); + if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ + nPage = nPageFile; + } + if( nPage>0 ){ + u32 pageSize; + u32 usableSize; + u8 *page1 = pPage1->aData; + rc = SQLITE_NOTADB; + /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins + ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d + ** 61 74 20 33 00. */ + if( memcmp(page1, zMagicHeader, 16)!=0 ){ + goto page1_init_failed; + } + +#ifdef SQLITE_OMIT_WAL + if( page1[18]>1 ){ + pBt->btsFlags |= BTS_READ_ONLY; + } + if( page1[19]>1 ){ + goto page1_init_failed; + } +#else + if( page1[18]>2 ){ + pBt->btsFlags |= BTS_READ_ONLY; + } + if( page1[19]>2 ){ + goto page1_init_failed; + } + + /* If the write version is set to 2, this database should be accessed + ** in WAL mode. If the log is not already open, open it now. Then + ** return SQLITE_OK and return without populating BtShared.pPage1. + ** The caller detects this and calls this function again. This is + ** required as the version of page 1 currently in the page1 buffer + ** may not be the latest version - there may be a newer one in the log + ** file. + */ + if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ + int isOpen = 0; + rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); + if( rc!=SQLITE_OK ){ + goto page1_init_failed; + }else{ +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS + sqlite3 *db; + Db *pDb; + if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ + while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } + if( pDb->bSyncSet==0 + && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1 + ){ + pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1; + sqlite3PagerSetFlags(pBt->pPager, + pDb->safety_level | (db->flags & PAGER_FLAGS_MASK)); + } + } +#endif + if( isOpen==0 ){ + releasePage(pPage1); + return SQLITE_OK; + } + } + rc = SQLITE_NOTADB; + } +#endif + + /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload + ** fractions and the leaf payload fraction values must be 64, 32, and 32. + ** + ** The original design allowed these amounts to vary, but as of + ** version 3.6.0, we require them to be fixed. + */ + if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ + goto page1_init_failed; + } + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is + ** determined by the 2-byte integer located at an offset of 16 bytes from + ** the beginning of the database file. */ + pageSize = (page1[16]<<8) | (page1[17]<<16); + /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two + ** between 512 and 65536 inclusive. */ + if( ((pageSize-1)&pageSize)!=0 + || pageSize>SQLITE_MAX_PAGE_SIZE + || pageSize<=256 + ){ + goto page1_init_failed; + } + assert( (pageSize & 7)==0 ); + /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte + ** integer at offset 20 is the number of bytes of space at the end of + ** each page to reserve for extensions. + ** + ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is + ** determined by the one-byte unsigned integer found at an offset of 20 + ** into the database file header. */ + usableSize = pageSize - page1[20]; + if( (u32)pageSize!=pBt->pageSize ){ + /* After reading the first page of the database assuming a page size + ** of BtShared.pageSize, we have discovered that the page-size is + ** actually pageSize. Unlock the database, leave pBt->pPage1 at + ** zero and return SQLITE_OK. The caller will call this function + ** again with the correct page-size. + */ + releasePage(pPage1); + pBt->usableSize = usableSize; + pBt->pageSize = pageSize; + freeTempSpace(pBt); + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, + pageSize-usableSize); + return rc; + } + if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ + rc = SQLITE_CORRUPT_BKPT; + goto page1_init_failed; + } + /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to + ** be less than 480. In other words, if the page size is 512, then the + ** reserved space size cannot exceed 32. */ + if( usableSize<480 ){ + goto page1_init_failed; + } + pBt->pageSize = pageSize; + pBt->usableSize = usableSize; +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); + pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); +#endif + } + + /* maxLocal is the maximum amount of payload to store locally for + ** a cell. Make sure it is small enough so that at least minFanout + ** cells can will fit on one page. We assume a 10-byte page header. + ** Besides the payload, the cell must store: + ** 2-byte pointer to the cell + ** 4-byte child pointer + ** 9-byte nKey value + ** 4-byte nData value + ** 4-byte overflow page pointer + ** So a cell consists of a 2-byte pointer, a header which is as much as + ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow + ** page pointer. + */ + pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); + pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); + pBt->maxLeaf = (u16)(pBt->usableSize - 35); + pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); + if( pBt->maxLocal>127 ){ + pBt->max1bytePayload = 127; + }else{ + pBt->max1bytePayload = (u8)pBt->maxLocal; + } + assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); + pBt->pPage1 = pPage1; + pBt->nPage = nPage; + return SQLITE_OK; + +page1_init_failed: + releasePage(pPage1); + pBt->pPage1 = 0; + return rc; +} + +#ifndef NDEBUG +/* +** Return the number of cursors open on pBt. This is for use +** in assert() expressions, so it is only compiled if NDEBUG is not +** defined. +** +** Only write cursors are counted if wrOnly is true. If wrOnly is +** false then all cursors are counted. +** +** For the purposes of this routine, a cursor is any cursor that +** is capable of reading or writing to the database. Cursors that +** have been tripped into the CURSOR_FAULT state are not counted. +*/ +static int countValidCursors(BtShared *pBt, int wrOnly){ + BtCursor *pCur; + int r = 0; + for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ + if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) + && pCur->eState!=CURSOR_FAULT ) r++; + } + return r; +} +#endif + +/* +** If there are no outstanding cursors and we are not in the middle +** of a transaction but there is a read lock on the database, then +** this routine unrefs the first page of the database file which +** has the effect of releasing the read lock. +** +** If there is a transaction in progress, this routine is a no-op. +*/ +static void unlockBtreeIfUnused(BtShared *pBt){ + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); + if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ + MemPage *pPage1 = pBt->pPage1; + assert( pPage1->aData ); + assert( sqlite3PagerRefcount(pBt->pPager)==1 ); + pBt->pPage1 = 0; + releasePageNotNull(pPage1); + } +} + +/* +** If pBt points to an empty file then convert that empty file +** into a new empty database by initializing the first page of +** the database. +*/ +static int newDatabase(BtShared *pBt){ + MemPage *pP1; + unsigned char *data; + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pBt->nPage>0 ){ + return SQLITE_OK; + } + pP1 = pBt->pPage1; + assert( pP1!=0 ); + data = pP1->aData; + rc = sqlite3PagerWrite(pP1->pDbPage); + if( rc ) return rc; + memcpy(data, zMagicHeader, sizeof(zMagicHeader)); + assert( sizeof(zMagicHeader)==16 ); + data[16] = (u8)((pBt->pageSize>>8)&0xff); + data[17] = (u8)((pBt->pageSize>>16)&0xff); + data[18] = 1; + data[19] = 1; + assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); + data[20] = (u8)(pBt->pageSize - pBt->usableSize); + data[21] = 64; + data[22] = 32; + data[23] = 32; + memset(&data[24], 0, 100-24); + zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); + pBt->btsFlags |= BTS_PAGESIZE_FIXED; +#ifndef SQLITE_OMIT_AUTOVACUUM + assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); + assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); + put4byte(&data[36 + 4*4], pBt->autoVacuum); + put4byte(&data[36 + 7*4], pBt->incrVacuum); +#endif + pBt->nPage = 1; + data[31] = 1; + return SQLITE_OK; +} + +/* +** Initialize the first page of the database file (creating a database +** consisting of a single page and no schema objects). Return SQLITE_OK +** if successful, or an SQLite error code otherwise. +*/ +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){ + int rc; + sqlite3BtreeEnter(p); + p->pBt->nPage = 0; + rc = newDatabase(p->pBt); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Attempt to start a new transaction. A write-transaction +** is started if the second argument is nonzero, otherwise a read- +** transaction. If the second argument is 2 or more and exclusive +** transaction is started, meaning that no other process is allowed +** to access the database. A preexisting transaction may not be +** upgraded to exclusive by calling this routine a second time - the +** exclusivity flag only works for a new transaction. +** +** A write-transaction must be started before attempting any +** changes to the database. None of the following routines +** will work unless a transaction is started first: +** +** sqlite3BtreeCreateTable() +** sqlite3BtreeCreateIndex() +** sqlite3BtreeClearTable() +** sqlite3BtreeDropTable() +** sqlite3BtreeInsert() +** sqlite3BtreeDelete() +** sqlite3BtreeUpdateMeta() +** +** If an initial attempt to acquire the lock fails because of lock contention +** and the database was previously unlocked, then invoke the busy handler +** if there is one. But if there was previously a read-lock, do not +** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is +** returned when there is already a read-lock in order to avoid a deadlock. +** +** Suppose there are two processes A and B. A has a read lock and B has +** a reserved lock. B tries to promote to exclusive but is blocked because +** of A's read lock. A tries to promote to reserved but is blocked by B. +** One or the other of the two processes must give way or there can be +** no progress. By returning SQLITE_BUSY and not invoking the busy callback +** when A already has a read lock, we encourage A to give up and let B +** proceed. +*/ +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + BtShared *pBt = p->pBt; + int rc = SQLITE_OK; + + sqlite3BtreeEnter(p); + btreeIntegrity(p); + + /* If the btree is already in a write-transaction, or it + ** is already in a read-transaction and a read-transaction + ** is requested, this is a no-op. + */ + if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ + goto trans_begun; + } + assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); + + /* Write transactions are not possible on a read-only database */ + if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ + rc = SQLITE_READONLY; + goto trans_begun; + } + +#ifndef SQLITE_OMIT_SHARED_CACHE + { + sqlite3 *pBlock = 0; + /* If another database handle has already opened a write transaction + ** on this shared-btree structure and a second write transaction is + ** requested, return SQLITE_LOCKED. + */ + if( (wrflag && pBt->inTransaction==TRANS_WRITE) + || (pBt->btsFlags & BTS_PENDING)!=0 + ){ + pBlock = pBt->pWriter->db; + }else if( wrflag>1 ){ + BtLock *pIter; + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + if( pIter->pBtree!=p ){ + pBlock = pIter->pBtree->db; + break; + } + } + } + if( pBlock ){ + sqlite3ConnectionBlocked(p->db, pBlock); + rc = SQLITE_LOCKED_SHAREDCACHE; + goto trans_begun; + } + } +#endif + + /* Any read-only or read-write transaction implies a read-lock on + ** page 1. So if some other shared-cache client already has a write-lock + ** on page 1, the transaction cannot be opened. */ + rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + if( SQLITE_OK!=rc ) goto trans_begun; + + pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; + if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; + do { + /* Call lockBtree() until either pBt->pPage1 is populated or + ** lockBtree() returns something other than SQLITE_OK. lockBtree() + ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after + ** reading page 1 it discovers that the page-size of the database + ** file is not pBt->pageSize. In this case lockBtree() will update + ** pBt->pageSize to the page-size of the file on disk. + */ + while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); + + if( rc==SQLITE_OK && wrflag ){ + if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ + rc = SQLITE_READONLY; + }else{ + rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); + if( rc==SQLITE_OK ){ + rc = newDatabase(pBt); + } + } + } + + if( rc!=SQLITE_OK ){ + unlockBtreeIfUnused(pBt); + } + }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && + btreeInvokeBusyHandler(pBt) ); + + if( rc==SQLITE_OK ){ + if( p->inTrans==TRANS_NONE ){ + pBt->nTransaction++; +#ifndef SQLITE_OMIT_SHARED_CACHE + if( p->sharable ){ + assert( p->lock.pBtree==p && p->lock.iTable==1 ); + p->lock.eLock = READ_LOCK; + p->lock.pNext = pBt->pLock; + pBt->pLock = &p->lock; + } +#endif + } + p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); + if( p->inTrans>pBt->inTransaction ){ + pBt->inTransaction = p->inTrans; + } + if( wrflag ){ + MemPage *pPage1 = pBt->pPage1; +#ifndef SQLITE_OMIT_SHARED_CACHE + assert( !pBt->pWriter ); + pBt->pWriter = p; + pBt->btsFlags &= ~BTS_EXCLUSIVE; + if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE; +#endif + + /* If the db-size header field is incorrect (as it may be if an old + ** client has been writing the database file), update it now. Doing + ** this sooner rather than later means the database size can safely + ** re-read the database size from page 1 if a savepoint or transaction + ** rollback occurs within the transaction. + */ + if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pPage1->aData[28], pBt->nPage); + } + } + } + } + + +trans_begun: + if( rc==SQLITE_OK && wrflag ){ + /* This call makes sure that the pager has the correct number of + ** open savepoints. If the second parameter is greater than 0 and + ** the sub-journal is not already open, then it will be opened here. + */ + rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); + } + + btreeIntegrity(p); + sqlite3BtreeLeave(p); + return rc; +} + +#ifndef SQLITE_OMIT_AUTOVACUUM + +/* +** Set the pointer-map entries for all children of page pPage. Also, if +** pPage contains cells that point to overflow pages, set the pointer +** map entries for the overflow pages as well. +*/ +static int setChildPtrmaps(MemPage *pPage){ + int i; /* Counter variable */ + int nCell; /* Number of cells in page pPage */ + int rc; /* Return code */ + BtShared *pBt = pPage->pBt; + u8 isInitOrig = pPage->isInit; + Pgno pgno = pPage->pgno; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + rc = btreeInitPage(pPage); + if( rc!=SQLITE_OK ){ + goto set_child_ptrmaps_out; + } + nCell = pPage->nCell; + + for(i=0; ileaf ){ + Pgno childPgno = get4byte(pCell); + ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + } + } + + if( !pPage->leaf ){ + Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); + ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + } + +set_child_ptrmaps_out: + pPage->isInit = isInitOrig; + return rc; +} + +/* +** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so +** that it points to iTo. Parameter eType describes the type of pointer to +** be modified, as follows: +** +** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child +** page of pPage. +** +** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow +** page pointed to by one of the cells on pPage. +** +** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next +** overflow page in the list. +*/ +static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + if( eType==PTRMAP_OVERFLOW2 ){ + /* The pointer is always the first 4 bytes of the page in this case. */ + if( get4byte(pPage->aData)!=iFrom ){ + return SQLITE_CORRUPT_BKPT; + } + put4byte(pPage->aData, iTo); + }else{ + u8 isInitOrig = pPage->isInit; + int i; + int nCell; + int rc; + + rc = btreeInitPage(pPage); + if( rc ) return rc; + nCell = pPage->nCell; + + for(i=0; ixParseCell(pPage, pCell, &info); + if( info.nLocalaData+pPage->maskPage + && iFrom==get4byte(pCell+info.nSize-4) + ){ + put4byte(pCell+info.nSize-4, iTo); + break; + } + }else{ + if( get4byte(pCell)==iFrom ){ + put4byte(pCell, iTo); + break; + } + } + } + + if( i==nCell ){ + if( eType!=PTRMAP_BTREE || + get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ + return SQLITE_CORRUPT_BKPT; + } + put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); + } + + pPage->isInit = isInitOrig; + } + return SQLITE_OK; +} + + +/* +** Move the open database page pDbPage to location iFreePage in the +** database. The pDbPage reference remains valid. +** +** The isCommit flag indicates that there is no need to remember that +** the journal needs to be sync()ed before database page pDbPage->pgno +** can be written to. The caller has already promised not to write to that +** page. +*/ +static int relocatePage( + BtShared *pBt, /* Btree */ + MemPage *pDbPage, /* Open page to move */ + u8 eType, /* Pointer map 'type' entry for pDbPage */ + Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ + Pgno iFreePage, /* The location to move pDbPage to */ + int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ +){ + MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ + Pgno iDbPage = pDbPage->pgno; + Pager *pPager = pBt->pPager; + int rc; + + assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || + eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pDbPage->pBt==pBt ); + + /* Move page iDbPage from its current location to page number iFreePage */ + TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", + iDbPage, iFreePage, iPtrPage, eType)); + rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); + if( rc!=SQLITE_OK ){ + return rc; + } + pDbPage->pgno = iFreePage; + + /* If pDbPage was a btree-page, then it may have child pages and/or cells + ** that point to overflow pages. The pointer map entries for all these + ** pages need to be changed. + ** + ** If pDbPage is an overflow page, then the first 4 bytes may store a + ** pointer to a subsequent overflow page. If this is the case, then + ** the pointer map needs to be updated for the subsequent overflow page. + */ + if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ + rc = setChildPtrmaps(pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + Pgno nextOvfl = get4byte(pDbPage->aData); + if( nextOvfl!=0 ){ + ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); + if( rc!=SQLITE_OK ){ + return rc; + } + } + } + + /* Fix the database pointer on page iPtrPage that pointed at iDbPage so + ** that it points at iFreePage. Also fix the pointer map entry for + ** iPtrPage. + */ + if( eType!=PTRMAP_ROOTPAGE ){ + rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = sqlite3PagerWrite(pPtrPage->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pPtrPage); + return rc; + } + rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); + releasePage(pPtrPage); + if( rc==SQLITE_OK ){ + ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); + } + } + return rc; +} + +/* Forward declaration required by incrVacuumStep(). */ +static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); + +/* +** Perform a single step of an incremental-vacuum. If successful, return +** SQLITE_OK. If there is no work to do (and therefore no point in +** calling this function again), return SQLITE_DONE. Or, if an error +** occurs, return some other error code. +** +** More specifically, this function attempts to re-organize the database so +** that the last page of the file currently in use is no longer in use. +** +** Parameter nFin is the number of pages that this database would contain +** were this function called until it returns SQLITE_DONE. +** +** If the bCommit parameter is non-zero, this function assumes that the +** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE +** or an error. bCommit is passed true for an auto-vacuum-on-commit +** operation, or false for an incremental vacuum. +*/ +static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ + Pgno nFreeList; /* Number of pages still on the free-list */ + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( iLastPg>nFin ); + + if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ + u8 eType; + Pgno iPtrPage; + + nFreeList = get4byte(&pBt->pPage1->aData[36]); + if( nFreeList==0 ){ + return SQLITE_DONE; + } + + rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); + if( rc!=SQLITE_OK ){ + return rc; + } + if( eType==PTRMAP_ROOTPAGE ){ + return SQLITE_CORRUPT_BKPT; + } + + if( eType==PTRMAP_FREEPAGE ){ + if( bCommit==0 ){ + /* Remove the page from the files free-list. This is not required + ** if bCommit is non-zero. In that case, the free-list will be + ** truncated to zero after this function returns, so it doesn't + ** matter if it still contains some garbage entries. + */ + Pgno iFreePg; + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( iFreePg==iLastPg ); + releasePage(pFreePg); + } + } else { + Pgno iFreePg; /* Index of free page to move pLastPg to */ + MemPage *pLastPg; + u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ + Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ + + rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* If bCommit is zero, this loop runs exactly once and page pLastPg + ** is swapped with the first free page pulled off the free list. + ** + ** On the other hand, if bCommit is greater than zero, then keep + ** looping until a free-page located within the first nFin pages + ** of the file is found. + */ + if( bCommit==0 ){ + eMode = BTALLOC_LE; + iNear = nFin; + } + do { + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); + if( rc!=SQLITE_OK ){ + releasePage(pLastPg); + return rc; + } + releasePage(pFreePg); + }while( bCommit && iFreePg>nFin ); + assert( iFreePgbDoTruncate = 1; + pBt->nPage = iLastPg; + } + return SQLITE_OK; +} + +/* +** The database opened by the first argument is an auto-vacuum database +** nOrig pages in size containing nFree free pages. Return the expected +** size of the database in pages following an auto-vacuum operation. +*/ +static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ + int nEntry; /* Number of entries on one ptrmap page */ + Pgno nPtrmap; /* Number of PtrMap pages to be freed */ + Pgno nFin; /* Return value */ + + nEntry = pBt->usableSize/5; + nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; + nFin = nOrig - nFree - nPtrmap; + if( nOrig>PENDING_BYTE_PAGE(pBt) && nFinpBt; + + sqlite3BtreeEnter(p); + assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); + if( !pBt->autoVacuum ){ + rc = SQLITE_DONE; + }else{ + Pgno nOrig = btreePagecount(pBt); + Pgno nFree = get4byte(&pBt->pPage1->aData[36]); + Pgno nFin = finalDbSize(pBt, nOrig, nFree); + + if( nOrig0 ){ + rc = saveAllCursors(pBt, 0, 0); + if( rc==SQLITE_OK ){ + invalidateAllOverflowCache(pBt); + rc = incrVacuumStep(pBt, nFin, nOrig, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + put4byte(&pBt->pPage1->aData[28], pBt->nPage); + } + }else{ + rc = SQLITE_DONE; + } + } + sqlite3BtreeLeave(p); + return rc; +} + +/* +** This routine is called prior to sqlite3PagerCommit when a transaction +** is committed for an auto-vacuum database. +** +** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages +** the database file should be truncated to during the commit process. +** i.e. the database has been reorganized so that only the first *pnTrunc +** pages are in use. +*/ +static int autoVacuumCommit(BtShared *pBt){ + int rc = SQLITE_OK; + Pager *pPager = pBt->pPager; + VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) + + assert( sqlite3_mutex_held(pBt->mutex) ); + invalidateAllOverflowCache(pBt); + assert(pBt->autoVacuum); + if( !pBt->incrVacuum ){ + Pgno nFin; /* Number of pages in database after autovacuuming */ + Pgno nFree; /* Number of pages on the freelist initially */ + Pgno iFree; /* The next page to be freed */ + Pgno nOrig; /* Database size before freeing */ + + nOrig = btreePagecount(pBt); + if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ + /* It is not possible to create a database for which the final page + ** is either a pointer-map page or the pending-byte page. If one + ** is encountered, this indicates corruption. + */ + return SQLITE_CORRUPT_BKPT; + } + + nFree = get4byte(&pBt->pPage1->aData[36]); + nFin = finalDbSize(pBt, nOrig, nFree); + if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; + if( nFinnFin && rc==SQLITE_OK; iFree--){ + rc = incrVacuumStep(pBt, nFin, iFree, 1); + } + if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + put4byte(&pBt->pPage1->aData[32], 0); + put4byte(&pBt->pPage1->aData[36], 0); + put4byte(&pBt->pPage1->aData[28], nFin); + pBt->bDoTruncate = 1; + pBt->nPage = nFin; + } + if( rc!=SQLITE_OK ){ + sqlite3PagerRollback(pPager); + } + } + + assert( nRef>=sqlite3PagerRefcount(pPager) ); + return rc; +} + +#else /* ifndef SQLITE_OMIT_AUTOVACUUM */ +# define setChildPtrmaps(x) SQLITE_OK +#endif + +/* +** This routine does the first phase of a two-phase commit. This routine +** causes a rollback journal to be created (if it does not already exist) +** and populated with enough information so that if a power loss occurs +** the database can be restored to its original state by playing back +** the journal. Then the contents of the journal are flushed out to +** the disk. After the journal is safely on oxide, the changes to the +** database are written into the database file and flushed to oxide. +** At the end of this call, the rollback journal still exists on the +** disk and we are still holding all locks, so the transaction has not +** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the +** commit process. +** +** This call is a no-op if no write-transaction is currently active on pBt. +** +** Otherwise, sync the database file for the btree pBt. zMaster points to +** the name of a master journal file that should be written into the +** individual journal file, or is NULL, indicating no master journal file +** (single database transaction). +** +** When this is called, the master journal should already have been +** created, populated with this journal pointer and synced to disk. +** +** Once this is routine has returned, the only thing required to commit +** the write-transaction for this database file is to delete the journal. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ + int rc = SQLITE_OK; + if( p->inTrans==TRANS_WRITE ){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + rc = autoVacuumCommit(pBt); + if( rc!=SQLITE_OK ){ + sqlite3BtreeLeave(p); + return rc; + } + } + if( pBt->bDoTruncate ){ + sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); + } +#endif + rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); + sqlite3BtreeLeave(p); + } + return rc; +} + +/* +** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() +** at the conclusion of a transaction. +*/ +static void btreeEndTransaction(Btree *p){ + BtShared *pBt = p->pBt; + sqlite3 *db = p->db; + assert( sqlite3BtreeHoldsMutex(p) ); + +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->bDoTruncate = 0; +#endif + if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){ + /* If there are other active statements that belong to this database + ** handle, downgrade to a read-only transaction. The other statements + ** may still be reading from the database. */ + downgradeAllSharedCacheTableLocks(p); + p->inTrans = TRANS_READ; + }else{ + /* If the handle had any kind of transaction open, decrement the + ** transaction count of the shared btree. If the transaction count + ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() + ** call below will unlock the pager. */ + if( p->inTrans!=TRANS_NONE ){ + clearAllSharedCacheTableLocks(p); + pBt->nTransaction--; + if( 0==pBt->nTransaction ){ + pBt->inTransaction = TRANS_NONE; + } + } + + /* Set the current transaction state to TRANS_NONE and unlock the + ** pager if this call closed the only read or write transaction. */ + p->inTrans = TRANS_NONE; + unlockBtreeIfUnused(pBt); + } + + btreeIntegrity(p); +} + +/* +** Commit the transaction currently in progress. +** +** This routine implements the second phase of a 2-phase commit. The +** sqlite3BtreeCommitPhaseOne() routine does the first phase and should +** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() +** routine did all the work of writing information out to disk and flushing the +** contents so that they are written onto the disk platter. All this +** routine has to do is delete or truncate or zero the header in the +** the rollback journal (which causes the transaction to commit) and +** drop locks. +** +** Normally, if an error occurs while the pager layer is attempting to +** finalize the underlying journal file, this function returns an error and +** the upper layer will attempt a rollback. However, if the second argument +** is non-zero then this b-tree transaction is part of a multi-file +** transaction. In this case, the transaction has already been committed +** (by deleting a master journal file) and the caller will ignore this +** functions return code. So, even if an error occurs in the pager layer, +** reset the b-tree objects internal state to indicate that the write +** transaction has been closed. This is quite safe, as the pager will have +** transitioned to the error state. +** +** This will release the write lock on the database file. If there +** are no active cursors, it also releases the read lock. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ + + if( p->inTrans==TRANS_NONE ) return SQLITE_OK; + sqlite3BtreeEnter(p); + btreeIntegrity(p); + + /* If the handle has a write-transaction open, commit the shared-btrees + ** transaction and set the shared state to TRANS_READ. + */ + if( p->inTrans==TRANS_WRITE ){ + int rc; + BtShared *pBt = p->pBt; + assert( pBt->inTransaction==TRANS_WRITE ); + assert( pBt->nTransaction>0 ); + rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); + if( rc!=SQLITE_OK && bCleanup==0 ){ + sqlite3BtreeLeave(p); + return rc; + } + p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */ + pBt->inTransaction = TRANS_READ; + btreeClearHasContent(pBt); + } + + btreeEndTransaction(p); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} + +/* +** Do both phases of a commit. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){ + int rc; + sqlite3BtreeEnter(p); + rc = sqlite3BtreeCommitPhaseOne(p, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeCommitPhaseTwo(p, 0); + } + sqlite3BtreeLeave(p); + return rc; +} + +/* +** This routine sets the state to CURSOR_FAULT and the error +** code to errCode for every cursor on any BtShared that pBtree +** references. Or if the writeOnly flag is set to 1, then only +** trip write cursors and leave read cursors unchanged. +** +** Every cursor is a candidate to be tripped, including cursors +** that belong to other database connections that happen to be +** sharing the cache with pBtree. +** +** This routine gets called when a rollback occurs. If the writeOnly +** flag is true, then only write-cursors need be tripped - read-only +** cursors save their current positions so that they may continue +** following the rollback. Or, if writeOnly is false, all cursors are +** tripped. In general, writeOnly is false if the transaction being +** rolled back modified the database schema. In this case b-tree root +** pages may be moved or deleted from the database altogether, making +** it unsafe for read cursors to continue. +** +** If the writeOnly flag is true and an error is encountered while +** saving the current position of a read-only cursor, all cursors, +** including all read-cursors are tripped. +** +** SQLITE_OK is returned if successful, or if an error occurs while +** saving a cursor position, an SQLite error code. +*/ +SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ + BtCursor *p; + int rc = SQLITE_OK; + + assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); + if( pBtree ){ + sqlite3BtreeEnter(pBtree); + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + int i; + if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ + if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ + rc = saveCursorPosition(p); + if( rc!=SQLITE_OK ){ + (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); + break; + } + } + }else{ + sqlite3BtreeClearCursor(p); + p->eState = CURSOR_FAULT; + p->skipNext = errCode; + } + for(i=0; i<=p->iPage; i++){ + releasePage(p->apPage[i]); + p->apPage[i] = 0; + } + } + sqlite3BtreeLeave(pBtree); + } + return rc; +} + +/* +** Rollback the transaction in progress. +** +** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). +** Only write cursors are tripped if writeOnly is true but all cursors are +** tripped if writeOnly is false. Any attempt to use +** a tripped cursor will result in an error. +** +** This will release the write lock on the database file. If there +** are no active cursors, it also releases the read lock. +*/ +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ + int rc; + BtShared *pBt = p->pBt; + MemPage *pPage1; + + assert( writeOnly==1 || writeOnly==0 ); + assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); + sqlite3BtreeEnter(p); + if( tripCode==SQLITE_OK ){ + rc = tripCode = saveAllCursors(pBt, 0, 0); + if( rc ) writeOnly = 0; + }else{ + rc = SQLITE_OK; + } + if( tripCode ){ + int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); + assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) ); + if( rc2!=SQLITE_OK ) rc = rc2; + } + btreeIntegrity(p); + + if( p->inTrans==TRANS_WRITE ){ + int rc2; + + assert( TRANS_WRITE==pBt->inTransaction ); + rc2 = sqlite3PagerRollback(pBt->pPager); + if( rc2!=SQLITE_OK ){ + rc = rc2; + } + + /* The rollback may have destroyed the pPage1->aData value. So + ** call btreeGetPage() on page 1 again to make + ** sure pPage1->aData is set correctly. */ + if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ + int nPage = get4byte(28+(u8*)pPage1->aData); + testcase( nPage==0 ); + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); + testcase( pBt->nPage!=nPage ); + pBt->nPage = nPage; + releasePage(pPage1); + } + assert( countValidCursors(pBt, 1)==0 ); + pBt->inTransaction = TRANS_READ; + btreeClearHasContent(pBt); + } + + btreeEndTransaction(p); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Start a statement subtransaction. The subtransaction can be rolled +** back independently of the main transaction. You must start a transaction +** before starting a subtransaction. The subtransaction is ended automatically +** if the main transaction commits or rolls back. +** +** Statement subtransactions are used around individual SQL statements +** that are contained within a BEGIN...COMMIT block. If a constraint +** error occurs within the statement, the effect of that one statement +** can be rolled back without having to rollback the entire transaction. +** +** A statement sub-transaction is implemented as an anonymous savepoint. The +** value passed as the second parameter is the total number of savepoints, +** including the new anonymous savepoint, open on the B-Tree. i.e. if there +** are no active savepoints and no other statement-transactions open, +** iStatement is 1. This anonymous savepoint can be released or rolled back +** using the sqlite3BtreeSavepoint() function. +*/ +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ + int rc; + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); + assert( iStatement>0 ); + assert( iStatement>p->db->nSavepoint ); + assert( pBt->inTransaction==TRANS_WRITE ); + /* At the pager level, a statement transaction is a savepoint with + ** an index greater than all savepoints created explicitly using + ** SQL statements. It is illegal to open, release or rollback any + ** such savepoints while the statement transaction savepoint is active. + */ + rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** The second argument to this function, op, is always SAVEPOINT_ROLLBACK +** or SAVEPOINT_RELEASE. This function either releases or rolls back the +** savepoint identified by parameter iSavepoint, depending on the value +** of op. +** +** Normally, iSavepoint is greater than or equal to zero. However, if op is +** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the +** contents of the entire transaction are rolled back. This is different +** from a normal transaction rollback, as no locks are released and the +** transaction remains open. +*/ +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ + int rc = SQLITE_OK; + if( p && p->inTrans==TRANS_WRITE ){ + BtShared *pBt = p->pBt; + assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); + assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); + sqlite3BtreeEnter(p); + rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); + if( rc==SQLITE_OK ){ + if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ + pBt->nPage = 0; + } + rc = newDatabase(pBt); + pBt->nPage = get4byte(28 + pBt->pPage1->aData); + + /* The database size was written into the offset 28 of the header + ** when the transaction started, so we know that the value at offset + ** 28 is nonzero. */ + assert( pBt->nPage>0 ); + } + sqlite3BtreeLeave(p); + } + return rc; +} + +/* +** Create a new cursor for the BTree whose root is on the page +** iTable. If a read-only cursor is requested, it is assumed that +** the caller already has at least a read-only transaction open +** on the database already. If a write-cursor is requested, then +** the caller is assumed to have an open write transaction. +** +** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only +** be used for reading. If the BTREE_WRCSR bit is set, then the cursor +** can be used for reading or for writing if other conditions for writing +** are also met. These are the conditions that must be met in order +** for writing to be allowed: +** +** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR +** +** 2: Other database connections that share the same pager cache +** but which are not in the READ_UNCOMMITTED state may not have +** cursors open with wrFlag==0 on the same table. Otherwise +** the changes made by this write cursor would be visible to +** the read cursors in the other database connection. +** +** 3: The database must be writable (not on read-only media) +** +** 4: There must be an active transaction. +** +** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR +** is set. If FORDELETE is set, that is a hint to the implementation that +** this cursor will only be used to seek to and delete entries of an index +** as part of a larger DELETE statement. The FORDELETE hint is not used by +** this implementation. But in a hypothetical alternative storage engine +** in which index entries are automatically deleted when corresponding table +** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE +** operations on this cursor can be no-ops and all READ operations can +** return a null row (2-bytes: 0x01 0x00). +** +** No checking is done to make sure that page iTable really is the +** root page of a b-tree. If it is not, then the cursor acquired +** will not work correctly. +** +** It is assumed that the sqlite3BtreeCursorZero() has been called +** on pCur to initialize the memory space prior to invoking this routine. +*/ +static int btreeCursor( + Btree *p, /* The btree */ + int iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to comparison function */ + BtCursor *pCur /* Space for new cursor */ +){ + BtShared *pBt = p->pBt; /* Shared b-tree handle */ + BtCursor *pX; /* Looping over other all cursors */ + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( wrFlag==0 + || wrFlag==BTREE_WRCSR + || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) + ); + + /* The following assert statements verify that if this is a sharable + ** b-tree database, the connection is holding the required table locks, + ** and that no other connection has any open cursor that conflicts with + ** this lock. */ + assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) ); + assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); + + /* Assert that the caller has opened the required transaction. */ + assert( p->inTrans>TRANS_NONE ); + assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); + assert( pBt->pPage1 && pBt->pPage1->aData ); + assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); + + if( wrFlag ){ + allocateTempSpace(pBt); + if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT; + } + if( iTable==1 && btreePagecount(pBt)==0 ){ + assert( wrFlag==0 ); + iTable = 0; + } + + /* Now that no other errors can occur, finish filling in the BtCursor + ** variables and link the cursor into the BtShared list. */ + pCur->pgnoRoot = (Pgno)iTable; + pCur->iPage = -1; + pCur->pKeyInfo = pKeyInfo; + pCur->pBtree = p; + pCur->pBt = pBt; + pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0; + pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; + /* If there are two or more cursors on the same btree, then all such + ** cursors *must* have the BTCF_Multiple flag set. */ + for(pX=pBt->pCursor; pX; pX=pX->pNext){ + if( pX->pgnoRoot==(Pgno)iTable ){ + pX->curFlags |= BTCF_Multiple; + pCur->curFlags |= BTCF_Multiple; + } + } + pCur->pNext = pBt->pCursor; + pBt->pCursor = pCur; + pCur->eState = CURSOR_INVALID; + return SQLITE_OK; +} +SQLITE_PRIVATE int sqlite3BtreeCursor( + Btree *p, /* The btree */ + int iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ + BtCursor *pCur /* Write new cursor here */ +){ + int rc; + if( iTable<1 ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + sqlite3BtreeEnter(p); + rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); + sqlite3BtreeLeave(p); + } + return rc; +} + +/* +** Return the size of a BtCursor object in bytes. +** +** This interfaces is needed so that users of cursors can preallocate +** sufficient storage to hold a cursor. The BtCursor object is opaque +** to users so they cannot do the sizeof() themselves - they must call +** this routine. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){ + return ROUND8(sizeof(BtCursor)); +} + +/* +** Initialize memory that will be converted into a BtCursor object. +** +** The simple approach here would be to memset() the entire object +** to zero. But it turns out that the apPage[] and aiIdx[] arrays +** do not need to be zeroed and they are large, so we can save a lot +** of run-time by skipping the initialization of those elements. +*/ +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){ + memset(p, 0, offsetof(BtCursor, iPage)); +} + +/* +** Close a cursor. The read lock on the database file is released +** when the last cursor is closed. +*/ +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){ + Btree *pBtree = pCur->pBtree; + if( pBtree ){ + int i; + BtShared *pBt = pCur->pBt; + sqlite3BtreeEnter(pBtree); + sqlite3BtreeClearCursor(pCur); + assert( pBt->pCursor!=0 ); + if( pBt->pCursor==pCur ){ + pBt->pCursor = pCur->pNext; + }else{ + BtCursor *pPrev = pBt->pCursor; + do{ + if( pPrev->pNext==pCur ){ + pPrev->pNext = pCur->pNext; + break; + } + pPrev = pPrev->pNext; + }while( ALWAYS(pPrev) ); + } + for(i=0; i<=pCur->iPage; i++){ + releasePage(pCur->apPage[i]); + } + unlockBtreeIfUnused(pBt); + sqlite3_free(pCur->aOverflow); + /* sqlite3_free(pCur); */ + sqlite3BtreeLeave(pBtree); + } + return SQLITE_OK; +} + +/* +** Make sure the BtCursor* given in the argument has a valid +** BtCursor.info structure. If it is not already valid, call +** btreeParseCell() to fill it in. +** +** BtCursor.info is a cache of the information in the current cell. +** Using this cache reduces the number of calls to btreeParseCell(). +*/ +#ifndef NDEBUG + static void assertCellInfo(BtCursor *pCur){ + CellInfo info; + int iPage = pCur->iPage; + memset(&info, 0, sizeof(info)); + btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); + assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); + } +#else + #define assertCellInfo(x) +#endif +static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ + if( pCur->info.nSize==0 ){ + int iPage = pCur->iPage; + pCur->curFlags |= BTCF_ValidNKey; + btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); + }else{ + assertCellInfo(pCur); + } +} + +#ifndef NDEBUG /* The next routine used only within assert() statements */ +/* +** Return true if the given BtCursor is valid. A valid cursor is one +** that is currently pointing to a row in a (non-empty) table. +** This is a verification routine is used only within assert() statements. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){ + return pCur && pCur->eState==CURSOR_VALID; +} +#endif /* NDEBUG */ + +/* +** Set *pSize to the size of the buffer needed to hold the value of +** the key for the current entry. If the cursor is not pointing +** to a valid entry, *pSize is set to 0. +** +** For a table with the INTKEY flag set, this routine returns the key +** itself, not the number of bytes in the key. +** +** The caller must position the cursor prior to invoking this routine. +** +** This routine cannot fail. It always returns SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + getCellInfo(pCur); + *pSize = pCur->info.nKey; + return SQLITE_OK; +} + +/* +** Set *pSize to the number of bytes of data in the entry the +** cursor currently points to. +** +** The caller must guarantee that the cursor is pointing to a non-NULL +** valid entry. In other words, the calling procedure must guarantee +** that the cursor has Cursor.eState==CURSOR_VALID. +** +** Failure is not possible. This function always returns SQLITE_OK. +** It might just as well be a procedure (returning void) but we continue +** to return an integer result code for historical reasons. +*/ +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>=0 ); + assert( pCur->iPageapPage[pCur->iPage]->intKeyLeaf==1 ); + getCellInfo(pCur); + *pSize = pCur->info.nPayload; + return SQLITE_OK; +} + +/* +** Given the page number of an overflow page in the database (parameter +** ovfl), this function finds the page number of the next page in the +** linked list of overflow pages. If possible, it uses the auto-vacuum +** pointer-map data instead of reading the content of page ovfl to do so. +** +** If an error occurs an SQLite error code is returned. Otherwise: +** +** The page number of the next overflow page in the linked list is +** written to *pPgnoNext. If page ovfl is the last page in its linked +** list, *pPgnoNext is set to zero. +** +** If ppPage is not NULL, and a reference to the MemPage object corresponding +** to page number pOvfl was obtained, then *ppPage is set to point to that +** reference. It is the responsibility of the caller to call releasePage() +** on *ppPage to free the reference. In no reference was obtained (because +** the pointer-map was used to obtain the value for *pPgnoNext), then +** *ppPage is set to zero. +*/ +static int getOverflowPage( + BtShared *pBt, /* The database file */ + Pgno ovfl, /* Current overflow page number */ + MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ + Pgno *pPgnoNext /* OUT: Next overflow page number */ +){ + Pgno next = 0; + MemPage *pPage = 0; + int rc = SQLITE_OK; + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert(pPgnoNext); + +#ifndef SQLITE_OMIT_AUTOVACUUM + /* Try to find the next page in the overflow list using the + ** autovacuum pointer-map pages. Guess that the next page in + ** the overflow list is page number (ovfl+1). If that guess turns + ** out to be wrong, fall back to loading the data of page + ** number ovfl to determine the next page number. + */ + if( pBt->autoVacuum ){ + Pgno pgno; + Pgno iGuess = ovfl+1; + u8 eType; + + while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ + iGuess++; + } + + if( iGuess<=btreePagecount(pBt) ){ + rc = ptrmapGet(pBt, iGuess, &eType, &pgno); + if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ + next = iGuess; + rc = SQLITE_DONE; + } + } + } +#endif + + assert( next==0 || rc==SQLITE_DONE ); + if( rc==SQLITE_OK ){ + rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0); + assert( rc==SQLITE_OK || pPage==0 ); + if( rc==SQLITE_OK ){ + next = get4byte(pPage->aData); + } + } + + *pPgnoNext = next; + if( ppPage ){ + *ppPage = pPage; + }else{ + releasePage(pPage); + } + return (rc==SQLITE_DONE ? SQLITE_OK : rc); +} + +/* +** Copy data from a buffer to a page, or from a page to a buffer. +** +** pPayload is a pointer to data stored on database page pDbPage. +** If argument eOp is false, then nByte bytes of data are copied +** from pPayload to the buffer pointed at by pBuf. If eOp is true, +** then sqlite3PagerWrite() is called on pDbPage and nByte bytes +** of data are copied from the buffer pBuf to pPayload. +** +** SQLITE_OK is returned on success, otherwise an error code. +*/ +static int copyPayload( + void *pPayload, /* Pointer to page data */ + void *pBuf, /* Pointer to buffer */ + int nByte, /* Number of bytes to copy */ + int eOp, /* 0 -> copy from page, 1 -> copy to page */ + DbPage *pDbPage /* Page containing pPayload */ +){ + if( eOp ){ + /* Copy data from buffer to page (a write operation) */ + int rc = sqlite3PagerWrite(pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + memcpy(pPayload, pBuf, nByte); + }else{ + /* Copy data from page to buffer (a read operation) */ + memcpy(pBuf, pPayload, nByte); + } + return SQLITE_OK; +} + +/* +** This function is used to read or overwrite payload information +** for the entry that the pCur cursor is pointing to. The eOp +** argument is interpreted as follows: +** +** 0: The operation is a read. Populate the overflow cache. +** 1: The operation is a write. Populate the overflow cache. +** 2: The operation is a read. Do not populate the overflow cache. +** +** A total of "amt" bytes are read or written beginning at "offset". +** Data is read to or from the buffer pBuf. +** +** The content being read or written might appear on the main page +** or be scattered out on multiple overflow pages. +** +** If the current cursor entry uses one or more overflow pages and the +** eOp argument is not 2, this function may allocate space for and lazily +** populates the overflow page-list cache array (BtCursor.aOverflow). +** Subsequent calls use this cache to make seeking to the supplied offset +** more efficient. +** +** Once an overflow page-list cache has been allocated, it may be +** invalidated if some other cursor writes to the same table, or if +** the cursor is moved to a different row. Additionally, in auto-vacuum +** mode, the following events may invalidate an overflow page-list cache. +** +** * An incremental vacuum, +** * A commit in auto_vacuum="full" mode, +** * Creating a table (may require moving an overflow page). +*/ +static int accessPayload( + BtCursor *pCur, /* Cursor pointing to entry to read from */ + u32 offset, /* Begin reading this far into payload */ + u32 amt, /* Read this many bytes */ + unsigned char *pBuf, /* Write the bytes into this buffer */ + int eOp /* zero to read. non-zero to write. */ +){ + unsigned char *aPayload; + int rc = SQLITE_OK; + int iIdx = 0; + MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ + BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ +#ifdef SQLITE_DIRECT_OVERFLOW_READ + unsigned char * const pBufStart = pBuf; + int bEnd; /* True if reading to end of data */ +#endif + + assert( pPage ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->aiIdx[pCur->iPage]nCell ); + assert( cursorHoldsMutex(pCur) ); + assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ + + getCellInfo(pCur); + aPayload = pCur->info.pPayload; +#ifdef SQLITE_DIRECT_OVERFLOW_READ + bEnd = offset+amt==pCur->info.nPayload; +#endif + assert( offset+amt <= pCur->info.nPayload ); + + assert( aPayload > pPage->aData ); + if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ + /* Trying to read or write past the end of the data is an error. The + ** conditional above is really: + ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] + ** but is recast into its current form to avoid integer overflow problems + */ + return SQLITE_CORRUPT_BKPT; + } + + /* Check if data must be read/written to/from the btree page itself. */ + if( offsetinfo.nLocal ){ + int a = amt; + if( a+offset>pCur->info.nLocal ){ + a = pCur->info.nLocal - offset; + } + rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); + offset = 0; + pBuf += a; + amt -= a; + }else{ + offset -= pCur->info.nLocal; + } + + + if( rc==SQLITE_OK && amt>0 ){ + const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ + Pgno nextPage; + + nextPage = get4byte(&aPayload[pCur->info.nLocal]); + + /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. + ** Except, do not allocate aOverflow[] for eOp==2. + ** + ** The aOverflow[] array is sized at one entry for each overflow page + ** in the overflow chain. The page number of the first overflow page is + ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array + ** means "not yet known" (the cache is lazily populated). + */ + if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ + int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; + if( nOvfl>pCur->nOvflAlloc ){ + Pgno *aNew = (Pgno*)sqlite3Realloc( + pCur->aOverflow, nOvfl*2*sizeof(Pgno) + ); + if( aNew==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + pCur->nOvflAlloc = nOvfl*2; + pCur->aOverflow = aNew; + } + } + if( rc==SQLITE_OK ){ + memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); + pCur->curFlags |= BTCF_ValidOvfl; + } + } + + /* If the overflow page-list cache has been allocated and the + ** entry for the first required overflow page is valid, skip + ** directly to it. + */ + if( (pCur->curFlags & BTCF_ValidOvfl)!=0 + && pCur->aOverflow[offset/ovflSize] + ){ + iIdx = (offset/ovflSize); + nextPage = pCur->aOverflow[iIdx]; + offset = (offset%ovflSize); + } + + for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ + + /* If required, populate the overflow page-list cache. */ + if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ + assert( pCur->aOverflow[iIdx]==0 + || pCur->aOverflow[iIdx]==nextPage + || CORRUPT_DB ); + pCur->aOverflow[iIdx] = nextPage; + } + + if( offset>=ovflSize ){ + /* The only reason to read this page is to obtain the page + ** number for the next page in the overflow chain. The page + ** data is not required. So first try to lookup the overflow + ** page-list cache, if any, then fall back to the getOverflowPage() + ** function. + ** + ** Note that the aOverflow[] array must be allocated because eOp!=2 + ** here. If eOp==2, then offset==0 and this branch is never taken. + */ + assert( eOp!=2 ); + assert( pCur->curFlags & BTCF_ValidOvfl ); + assert( pCur->pBtree->db==pBt->db ); + if( pCur->aOverflow[iIdx+1] ){ + nextPage = pCur->aOverflow[iIdx+1]; + }else{ + rc = getOverflowPage(pBt, nextPage, 0, &nextPage); + } + offset -= ovflSize; + }else{ + /* Need to read this page properly. It contains some of the + ** range of data that is being read (eOp==0) or written (eOp!=0). + */ +#ifdef SQLITE_DIRECT_OVERFLOW_READ + sqlite3_file *fd; +#endif + int a = amt; + if( a + offset > ovflSize ){ + a = ovflSize - offset; + } + +#ifdef SQLITE_DIRECT_OVERFLOW_READ + /* If all the following are true: + ** + ** 1) this is a read operation, and + ** 2) data is required from the start of this overflow page, and + ** 3) the database is file-backed, and + ** 4) there is no open write-transaction, and + ** 5) the database is not a WAL database, + ** 6) all data from the page is being read. + ** 7) at least 4 bytes have already been read into the output buffer + ** + ** then data can be read directly from the database file into the + ** output buffer, bypassing the page-cache altogether. This speeds + ** up loading large records that span many overflow pages. + */ + if( (eOp&0x01)==0 /* (1) */ + && offset==0 /* (2) */ + && (bEnd || a==ovflSize) /* (6) */ + && pBt->inTransaction==TRANS_READ /* (4) */ + && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ + && pBt->pPage1->aData[19]==0x01 /* (5) */ + && &pBuf[-4]>=pBufStart /* (7) */ + ){ + u8 aSave[4]; + u8 *aWrite = &pBuf[-4]; + assert( aWrite>=pBufStart ); /* hence (7) */ + memcpy(aSave, aWrite, 4); + rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); + nextPage = get4byte(aWrite); + memcpy(aWrite, aSave, 4); + }else +#endif + + { + DbPage *pDbPage; + rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, + ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) + ); + if( rc==SQLITE_OK ){ + aPayload = sqlite3PagerGetData(pDbPage); + nextPage = get4byte(aPayload); + rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); + sqlite3PagerUnref(pDbPage); + offset = 0; + } + } + amt -= a; + pBuf += a; + } + } + } + + if( rc==SQLITE_OK && amt>0 ){ + return SQLITE_CORRUPT_BKPT; + } + return rc; +} + +/* +** Read part of the key associated with cursor pCur. Exactly +** "amt" bytes will be transferred into pBuf[]. The transfer +** begins at "offset". +** +** The caller must ensure that pCur is pointing to a valid row +** in the table. +** +** Return SQLITE_OK on success or an error code if anything goes +** wrong. An error is returned if "offset+amt" is larger than +** the available payload. +*/ +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); +} + +/* +** Read part of the data associated with cursor pCur. Exactly +** "amt" bytes will be transfered into pBuf[]. The transfer +** begins at "offset". +** +** Return SQLITE_OK on success or an error code if anything goes +** wrong. An error is returned if "offset+amt" is larger than +** the available payload. +*/ +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + int rc; + +#ifndef SQLITE_OMIT_INCRBLOB + if ( pCur->eState==CURSOR_INVALID ){ + return SQLITE_ABORT; + } +#endif + + assert( cursorOwnsBtShared(pCur) ); + rc = restoreCursorPosition(pCur); + if( rc==SQLITE_OK ){ + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + rc = accessPayload(pCur, offset, amt, pBuf, 0); + } + return rc; +} + +/* +** Return a pointer to payload information from the entry that the +** pCur cursor is pointing to. The pointer is to the beginning of +** the key if index btrees (pPage->intKey==0) and is the data for +** table btrees (pPage->intKey==1). The number of bytes of available +** key/data is written into *pAmt. If *pAmt==0, then the value +** returned will not be a valid pointer. +** +** This routine is an optimization. It is common for the entire key +** and data to fit on the local page and for there to be no overflow +** pages. When that is so, this routine can be used to access the +** key and data without making a copy. If the key and/or data spills +** onto overflow pages, then accessPayload() must be used to reassemble +** the key/data and copy it into a preallocated buffer. +** +** The pointer returned by this routine looks directly into the cached +** page of the database. The data might change or move the next time +** any btree routine is called. +*/ +static const void *fetchPayload( + BtCursor *pCur, /* Cursor pointing to entry to read from */ + u32 *pAmt /* Write the number of available bytes here */ +){ + u32 amt; + assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); + assert( pCur->eState==CURSOR_VALID ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + assert( pCur->info.nSize>0 ); + assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB ); + assert( pCur->info.pPayloadapPage[pCur->iPage]->aDataEnd ||CORRUPT_DB); + amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload); + if( pCur->info.nLocalinfo.nLocal; + *pAmt = amt; + return (void*)pCur->info.pPayload; +} + + +/* +** For the entry that cursor pCur is point to, return as +** many bytes of the key or data as are available on the local +** b-tree page. Write the number of available bytes into *pAmt. +** +** The pointer returned is ephemeral. The key/data may move +** or be destroyed on the next call to any Btree routine, +** including calls from other threads against the same cache. +** Hence, a mutex on the BtShared should be held prior to calling +** this routine. +** +** These routines is used to get quick access to key and data +** in the common case where no overflow pages are used. +*/ +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){ + return fetchPayload(pCur, pAmt); +} +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ + return fetchPayload(pCur, pAmt); +} + + +/* +** Move the cursor down to a new child page. The newPgno argument is the +** page number of the child page to move to. +** +** This function returns SQLITE_CORRUPT if the page-header flags field of +** the new child page does not match the flags field of the parent (i.e. +** if an intkey page appears to be the parent of a non-intkey page, or +** vice-versa). +*/ +static int moveToChild(BtCursor *pCur, u32 newPgno){ + BtShared *pBt = pCur->pBt; + + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPageiPage>=0 ); + if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ + return SQLITE_CORRUPT_BKPT; + } + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + pCur->iPage++; + pCur->aiIdx[pCur->iPage] = 0; + return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage], + pCur, pCur->curPagerFlags); +} + +#if SQLITE_DEBUG +/* +** Page pParent is an internal (non-leaf) tree page. This function +** asserts that page number iChild is the left-child if the iIdx'th +** cell in page pParent. Or, if iIdx is equal to the total number of +** cells in pParent, that page number iChild is the right-child of +** the page. +*/ +static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ + if( CORRUPT_DB ) return; /* The conditions tested below might not be true + ** in a corrupt database */ + assert( iIdx<=pParent->nCell ); + if( iIdx==pParent->nCell ){ + assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); + }else{ + assert( get4byte(findCell(pParent, iIdx))==iChild ); + } +} +#else +# define assertParentIndex(x,y,z) +#endif + +/* +** Move the cursor up to the parent page. +** +** pCur->idx is set to the cell index that contains the pointer +** to the page we are coming from. If we are coming from the +** right-most child page then pCur->idx is set to one more than +** the largest cell index. +*/ +static void moveToParent(BtCursor *pCur){ + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>0 ); + assert( pCur->apPage[pCur->iPage] ); + assertParentIndex( + pCur->apPage[pCur->iPage-1], + pCur->aiIdx[pCur->iPage-1], + pCur->apPage[pCur->iPage]->pgno + ); + testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + releasePageNotNull(pCur->apPage[pCur->iPage--]); +} + +/* +** Move the cursor to point to the root page of its b-tree structure. +** +** If the table has a virtual root page, then the cursor is moved to point +** to the virtual root page instead of the actual root page. A table has a +** virtual root page when the actual root page contains no cells and a +** single child page. This can only happen with the table rooted at page 1. +** +** If the b-tree structure is empty, the cursor state is set to +** CURSOR_INVALID. Otherwise, the cursor is set to point to the first +** cell located on the root (or virtual root) page and the cursor state +** is set to CURSOR_VALID. +** +** If this function returns successfully, it may be assumed that the +** page-header flags indicate that the [virtual] root-page is the expected +** kind of b-tree page (i.e. if when opening the cursor the caller did not +** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, +** indicating a table b-tree, or if the caller did specify a KeyInfo +** structure the flags byte is set to 0x02 or 0x0A, indicating an index +** b-tree). +*/ +static int moveToRoot(BtCursor *pCur){ + MemPage *pRoot; + int rc = SQLITE_OK; + + assert( cursorOwnsBtShared(pCur) ); + assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); + if( pCur->eState>=CURSOR_REQUIRESEEK ){ + if( pCur->eState==CURSOR_FAULT ){ + assert( pCur->skipNext!=SQLITE_OK ); + return pCur->skipNext; + } + sqlite3BtreeClearCursor(pCur); + } + + if( pCur->iPage>=0 ){ + while( pCur->iPage ){ + assert( pCur->apPage[pCur->iPage]!=0 ); + releasePageNotNull(pCur->apPage[pCur->iPage--]); + } + }else if( pCur->pgnoRoot==0 ){ + pCur->eState = CURSOR_INVALID; + return SQLITE_OK; + }else{ + assert( pCur->iPage==(-1) ); + rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], + 0, pCur->curPagerFlags); + if( rc!=SQLITE_OK ){ + pCur->eState = CURSOR_INVALID; + return rc; + } + pCur->iPage = 0; + pCur->curIntKey = pCur->apPage[0]->intKey; + } + pRoot = pCur->apPage[0]; + assert( pRoot->pgno==pCur->pgnoRoot ); + + /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor + ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is + ** NULL, the caller expects a table b-tree. If this is not the case, + ** return an SQLITE_CORRUPT error. + ** + ** Earlier versions of SQLite assumed that this test could not fail + ** if the root page was already loaded when this function was called (i.e. + ** if pCur->iPage>=0). But this is not so if the database is corrupted + ** in such a way that page pRoot is linked into a second b-tree table + ** (or the freelist). */ + assert( pRoot->intKey==1 || pRoot->intKey==0 ); + if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ + return SQLITE_CORRUPT_BKPT; + } + + pCur->aiIdx[0] = 0; + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); + + if( pRoot->nCell>0 ){ + pCur->eState = CURSOR_VALID; + }else if( !pRoot->leaf ){ + Pgno subpage; + if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; + subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); + pCur->eState = CURSOR_VALID; + rc = moveToChild(pCur, subpage); + }else{ + pCur->eState = CURSOR_INVALID; + } + return rc; +} + +/* +** Move the cursor down to the left-most leaf entry beneath the +** entry to which it is currently pointing. +** +** The left-most leaf is the one with the smallest key - the first +** in ascending order. +*/ +static int moveToLeftmost(BtCursor *pCur){ + Pgno pgno; + int rc = SQLITE_OK; + MemPage *pPage; + + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ + assert( pCur->aiIdx[pCur->iPage]nCell ); + pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); + rc = moveToChild(pCur, pgno); + } + return rc; +} + +/* +** Move the cursor down to the right-most leaf entry beneath the +** page to which it is currently pointing. Notice the difference +** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() +** finds the left-most entry beneath the *entry* whereas moveToRightmost() +** finds the right-most entry beneath the *page*. +** +** The right-most entry is the one with the largest key - the last +** key in ascending order. +*/ +static int moveToRightmost(BtCursor *pCur){ + Pgno pgno; + int rc = SQLITE_OK; + MemPage *pPage = 0; + + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){ + pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); + pCur->aiIdx[pCur->iPage] = pPage->nCell; + rc = moveToChild(pCur, pgno); + if( rc ) return rc; + } + pCur->aiIdx[pCur->iPage] = pPage->nCell-1; + assert( pCur->info.nSize==0 ); + assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); + return SQLITE_OK; +} + +/* Move the cursor to the first entry in the table. Return SQLITE_OK +** on success. Set *pRes to 0 if the cursor actually points to something +** or set *pRes to 1 if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ + int rc; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ + if( pCur->eState==CURSOR_INVALID ){ + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); + *pRes = 1; + }else{ + assert( pCur->apPage[pCur->iPage]->nCell>0 ); + *pRes = 0; + rc = moveToLeftmost(pCur); + } + } + return rc; +} + +/* Move the cursor to the last entry in the table. Return SQLITE_OK +** on success. Set *pRes to 0 if the cursor actually points to something +** or set *pRes to 1 if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ + int rc; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + + /* If the cursor already points to the last entry, this is a no-op. */ + if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ +#ifdef SQLITE_DEBUG + /* This block serves to assert() that the cursor really does point + ** to the last entry in the b-tree. */ + int ii; + for(ii=0; iiiPage; ii++){ + assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); + } + assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); + assert( pCur->apPage[pCur->iPage]->leaf ); +#endif + return SQLITE_OK; + } + + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ + if( CURSOR_INVALID==pCur->eState ){ + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); + *pRes = 1; + }else{ + assert( pCur->eState==CURSOR_VALID ); + *pRes = 0; + rc = moveToRightmost(pCur); + if( rc==SQLITE_OK ){ + pCur->curFlags |= BTCF_AtLast; + }else{ + pCur->curFlags &= ~BTCF_AtLast; + } + + } + } + return rc; +} + +/* Move the cursor so that it points to an entry near the key +** specified by pIdxKey or intKey. Return a success code. +** +** For INTKEY tables, the intKey parameter is used. pIdxKey +** must be NULL. For index tables, pIdxKey is used and intKey +** is ignored. +** +** If an exact match is not found, then the cursor is always +** left pointing at a leaf page which would hold the entry if it +** were present. The cursor might point to an entry that comes +** before or after the key. +** +** An integer is written into *pRes which is the result of +** comparing the key with the entry to which the cursor is +** pointing. The meaning of the integer written into +** *pRes is as follows: +** +** *pRes<0 The cursor is left pointing at an entry that +** is smaller than intKey/pIdxKey or if the table is empty +** and the cursor is therefore left point to nothing. +** +** *pRes==0 The cursor is left pointing at an entry that +** exactly matches intKey/pIdxKey. +** +** *pRes>0 The cursor is left pointing at an entry that +** is larger than intKey/pIdxKey. +** +** For index tables, the pIdxKey->eqSeen field is set to 1 if there +** exists an entry in the table that exactly matches pIdxKey. +*/ +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + BtCursor *pCur, /* The cursor to be moved */ + UnpackedRecord *pIdxKey, /* Unpacked index key */ + i64 intKey, /* The table key */ + int biasRight, /* If true, bias the search to the high end */ + int *pRes /* Write search results here */ +){ + int rc; + RecordCompare xRecordCompare; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( pRes ); + assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); + + /* If the cursor is already positioned at the point we are trying + ** to move to, then just return without doing any work */ + if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 + && pCur->curIntKey + ){ + if( pCur->info.nKey==intKey ){ + *pRes = 0; + return SQLITE_OK; + } + if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKeyerrCode = 0; + assert( pIdxKey->default_rc==1 + || pIdxKey->default_rc==0 + || pIdxKey->default_rc==-1 + ); + }else{ + xRecordCompare = 0; /* All keys are integers */ + } + + rc = moveToRoot(pCur); + if( rc ){ + return rc; + } + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); + assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); + if( pCur->eState==CURSOR_INVALID ){ + *pRes = -1; + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); + return SQLITE_OK; + } + assert( pCur->apPage[0]->intKey==pCur->curIntKey ); + assert( pCur->curIntKey || pIdxKey ); + for(;;){ + int lwr, upr, idx, c; + Pgno chldPg; + MemPage *pPage = pCur->apPage[pCur->iPage]; + u8 *pCell; /* Pointer to current cell in pPage */ + + /* pPage->nCell must be greater than zero. If this is the root-page + ** the cursor would have been INVALID above and this for(;;) loop + ** not run. If this is not the root-page, then the moveToChild() routine + ** would have already detected db corruption. Similarly, pPage must + ** be the right kind (index or table) of b-tree page. Otherwise + ** a moveToChild() or moveToRoot() call would have detected corruption. */ + assert( pPage->nCell>0 ); + assert( pPage->intKey==(pIdxKey==0) ); + lwr = 0; + upr = pPage->nCell-1; + assert( biasRight==0 || biasRight==1 ); + idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ + pCur->aiIdx[pCur->iPage] = (u16)idx; + if( xRecordCompare==0 ){ + for(;;){ + i64 nCellKey; + pCell = findCellPastPtr(pPage, idx); + if( pPage->intKeyLeaf ){ + while( 0x80 <= *(pCell++) ){ + if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; + } + } + getVarint(pCell, (u64*)&nCellKey); + if( nCellKeyupr ){ c = -1; break; } + }else if( nCellKey>intKey ){ + upr = idx-1; + if( lwr>upr ){ c = +1; break; } + }else{ + assert( nCellKey==intKey ); + pCur->curFlags |= BTCF_ValidNKey; + pCur->info.nKey = nCellKey; + pCur->aiIdx[pCur->iPage] = (u16)idx; + if( !pPage->leaf ){ + lwr = idx; + goto moveto_next_layer; + }else{ + *pRes = 0; + rc = SQLITE_OK; + goto moveto_finish; + } + } + assert( lwr+upr>=0 ); + idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ + } + }else{ + for(;;){ + int nCell; /* Size of the pCell cell in bytes */ + pCell = findCellPastPtr(pPage, idx); + + /* The maximum supported page-size is 65536 bytes. This means that + ** the maximum number of record bytes stored on an index B-Tree + ** page is less than 16384 bytes and may be stored as a 2-byte + ** varint. This information is used to attempt to avoid parsing + ** the entire cell by checking for the cases where the record is + ** stored entirely within the b-tree page by inspecting the first + ** 2 bytes of the cell. + */ + nCell = pCell[0]; + if( nCell<=pPage->max1bytePayload ){ + /* This branch runs if the record-size field of the cell is a + ** single byte varint and the record fits entirely on the main + ** b-tree page. */ + testcase( pCell+nCell+1==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); + }else if( !(pCell[1] & 0x80) + && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal + ){ + /* The record-size field is a 2 byte varint and the record + ** fits entirely on the main b-tree page. */ + testcase( pCell+nCell+2==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); + }else{ + /* The record flows over onto one or more overflow pages. In + ** this case the whole cell needs to be parsed, a buffer allocated + ** and accessPayload() used to retrieve the record into the + ** buffer before VdbeRecordCompare() can be called. + ** + ** If the record is corrupt, the xRecordCompare routine may read + ** up to two varints past the end of the buffer. An extra 18 + ** bytes of padding is allocated at the end of the buffer in + ** case this happens. */ + void *pCellKey; + u8 * const pCellBody = pCell - pPage->childPtrSize; + pPage->xParseCell(pPage, pCellBody, &pCur->info); + nCell = (int)pCur->info.nKey; + testcase( nCell<0 ); /* True if key size is 2^32 or more */ + testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ + testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ + testcase( nCell==2 ); /* Minimum legal index key size */ + if( nCell<2 ){ + rc = SQLITE_CORRUPT_BKPT; + goto moveto_finish; + } + pCellKey = sqlite3Malloc( nCell+18 ); + if( pCellKey==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto moveto_finish; + } + pCur->aiIdx[pCur->iPage] = (u16)idx; + rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); + if( rc ){ + sqlite3_free(pCellKey); + goto moveto_finish; + } + c = xRecordCompare(nCell, pCellKey, pIdxKey); + sqlite3_free(pCellKey); + } + assert( + (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) + && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) + ); + if( c<0 ){ + lwr = idx+1; + }else if( c>0 ){ + upr = idx-1; + }else{ + assert( c==0 ); + *pRes = 0; + rc = SQLITE_OK; + pCur->aiIdx[pCur->iPage] = (u16)idx; + if( pIdxKey->errCode ) rc = SQLITE_CORRUPT; + goto moveto_finish; + } + if( lwr>upr ) break; + assert( lwr+upr>=0 ); + idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ + } + } + assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); + assert( pPage->isInit ); + if( pPage->leaf ){ + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + pCur->aiIdx[pCur->iPage] = (u16)idx; + *pRes = c; + rc = SQLITE_OK; + goto moveto_finish; + } +moveto_next_layer: + if( lwr>=pPage->nCell ){ + chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); + }else{ + chldPg = get4byte(findCell(pPage, lwr)); + } + pCur->aiIdx[pCur->iPage] = (u16)lwr; + rc = moveToChild(pCur, chldPg); + if( rc ) break; + } +moveto_finish: + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + return rc; +} + + +/* +** Return TRUE if the cursor is not pointing at an entry of the table. +** +** TRUE will be returned after a call to sqlite3BtreeNext() moves +** past the last entry in the table or sqlite3BtreePrev() moves past +** the first entry. TRUE is also returned if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){ + /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries + ** have been deleted? This API will need to change to return an error code + ** as well as the boolean result value. + */ + return (CURSOR_VALID!=pCur->eState); +} + +/* +** Advance the cursor to the next entry in the database. If +** successful then set *pRes=0. If the cursor +** was already pointing to the last entry in the database before +** this routine was called, then set *pRes=1. +** +** The main entry point is sqlite3BtreeNext(). That routine is optimized +** for the common case of merely incrementing the cell counter BtCursor.aiIdx +** to the next cell on the current page. The (slower) btreeNext() helper +** routine is called when it is necessary to move to a different page or +** to restore the cursor. +** +** The calling function will set *pRes to 0 or 1. The initial *pRes value +** will be 1 if the cursor being stepped corresponds to an SQL index and +** if this routine could have been skipped if that SQL index had been +** a unique index. Otherwise the caller will have set *pRes to zero. +** Zero is the common case. The btree implementation is free to use the +** initial *pRes value as a hint to improve performance, but the current +** SQLite btree implementation does not. (Note that the comdb2 btree +** implementation does use this hint, however.) +*/ +static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ + int rc; + int idx; + MemPage *pPage; + + assert( cursorOwnsBtShared(pCur) ); + assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); + assert( *pRes==0 ); + if( pCur->eState!=CURSOR_VALID ){ + assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); + rc = restoreCursorPosition(pCur); + if( rc!=SQLITE_OK ){ + return rc; + } + if( CURSOR_INVALID==pCur->eState ){ + *pRes = 1; + return SQLITE_OK; + } + if( pCur->skipNext ){ + assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); + pCur->eState = CURSOR_VALID; + if( pCur->skipNext>0 ){ + pCur->skipNext = 0; + return SQLITE_OK; + } + pCur->skipNext = 0; + } + } + + pPage = pCur->apPage[pCur->iPage]; + idx = ++pCur->aiIdx[pCur->iPage]; + assert( pPage->isInit ); + + /* If the database file is corrupt, it is possible for the value of idx + ** to be invalid here. This can only occur if a second cursor modifies + ** the page while cursor pCur is holding a reference to it. Which can + ** only happen if the database is corrupt in such a way as to link the + ** page into more than one b-tree structure. */ + testcase( idx>pPage->nCell ); + + if( idx>=pPage->nCell ){ + if( !pPage->leaf ){ + rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); + if( rc ) return rc; + return moveToLeftmost(pCur); + } + do{ + if( pCur->iPage==0 ){ + *pRes = 1; + pCur->eState = CURSOR_INVALID; + return SQLITE_OK; + } + moveToParent(pCur); + pPage = pCur->apPage[pCur->iPage]; + }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); + if( pPage->intKey ){ + return sqlite3BtreeNext(pCur, pRes); + }else{ + return SQLITE_OK; + } + } + if( pPage->leaf ){ + return SQLITE_OK; + }else{ + return moveToLeftmost(pCur); + } +} +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ + MemPage *pPage; + assert( cursorOwnsBtShared(pCur) ); + assert( pRes!=0 ); + assert( *pRes==0 || *pRes==1 ); + assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + *pRes = 0; + if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes); + pPage = pCur->apPage[pCur->iPage]; + if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){ + pCur->aiIdx[pCur->iPage]--; + return btreeNext(pCur, pRes); + } + if( pPage->leaf ){ + return SQLITE_OK; + }else{ + return moveToLeftmost(pCur); + } +} + +/* +** Step the cursor to the back to the previous entry in the database. If +** successful then set *pRes=0. If the cursor +** was already pointing to the first entry in the database before +** this routine was called, then set *pRes=1. +** +** The main entry point is sqlite3BtreePrevious(). That routine is optimized +** for the common case of merely decrementing the cell counter BtCursor.aiIdx +** to the previous cell on the current page. The (slower) btreePrevious() +** helper routine is called when it is necessary to move to a different page +** or to restore the cursor. +** +** The calling function will set *pRes to 0 or 1. The initial *pRes value +** will be 1 if the cursor being stepped corresponds to an SQL index and +** if this routine could have been skipped if that SQL index had been +** a unique index. Otherwise the caller will have set *pRes to zero. +** Zero is the common case. The btree implementation is free to use the +** initial *pRes value as a hint to improve performance, but the current +** SQLite btree implementation does not. (Note that the comdb2 btree +** implementation does use this hint, however.) +*/ +static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ + int rc; + MemPage *pPage; + + assert( cursorOwnsBtShared(pCur) ); + assert( pRes!=0 ); + assert( *pRes==0 ); + assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); + assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); + assert( pCur->info.nSize==0 ); + if( pCur->eState!=CURSOR_VALID ){ + rc = restoreCursorPosition(pCur); + if( rc!=SQLITE_OK ){ + return rc; + } + if( CURSOR_INVALID==pCur->eState ){ + *pRes = 1; + return SQLITE_OK; + } + if( pCur->skipNext ){ + assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); + pCur->eState = CURSOR_VALID; + if( pCur->skipNext<0 ){ + pCur->skipNext = 0; + return SQLITE_OK; + } + pCur->skipNext = 0; + } + } + + pPage = pCur->apPage[pCur->iPage]; + assert( pPage->isInit ); + if( !pPage->leaf ){ + int idx = pCur->aiIdx[pCur->iPage]; + rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); + if( rc ) return rc; + rc = moveToRightmost(pCur); + }else{ + while( pCur->aiIdx[pCur->iPage]==0 ){ + if( pCur->iPage==0 ){ + pCur->eState = CURSOR_INVALID; + *pRes = 1; + return SQLITE_OK; + } + moveToParent(pCur); + } + assert( pCur->info.nSize==0 ); + assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); + + pCur->aiIdx[pCur->iPage]--; + pPage = pCur->apPage[pCur->iPage]; + if( pPage->intKey && !pPage->leaf ){ + rc = sqlite3BtreePrevious(pCur, pRes); + }else{ + rc = SQLITE_OK; + } + } + return rc; +} +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ + assert( cursorOwnsBtShared(pCur) ); + assert( pRes!=0 ); + assert( *pRes==0 || *pRes==1 ); + assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); + *pRes = 0; + pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); + pCur->info.nSize = 0; + if( pCur->eState!=CURSOR_VALID + || pCur->aiIdx[pCur->iPage]==0 + || pCur->apPage[pCur->iPage]->leaf==0 + ){ + return btreePrevious(pCur, pRes); + } + pCur->aiIdx[pCur->iPage]--; + return SQLITE_OK; +} + +/* +** Allocate a new page from the database file. +** +** The new page is marked as dirty. (In other words, sqlite3PagerWrite() +** has already been called on the new page.) The new page has also +** been referenced and the calling routine is responsible for calling +** sqlite3PagerUnref() on the new page when it is done. +** +** SQLITE_OK is returned on success. Any other return value indicates +** an error. *ppPage is set to NULL in the event of an error. +** +** If the "nearby" parameter is not 0, then an effort is made to +** locate a page close to the page number "nearby". This can be used in an +** attempt to keep related pages close to each other in the database file, +** which in turn can make database access faster. +** +** If the eMode parameter is BTALLOC_EXACT and the nearby page exists +** anywhere on the free-list, then it is guaranteed to be returned. If +** eMode is BTALLOC_LT then the page returned will be less than or equal +** to nearby if any such page exists. If eMode is BTALLOC_ANY then there +** are no restrictions on which page is returned. +*/ +static int allocateBtreePage( + BtShared *pBt, /* The btree */ + MemPage **ppPage, /* Store pointer to the allocated page here */ + Pgno *pPgno, /* Store the page number here */ + Pgno nearby, /* Search for a page near this one */ + u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */ +){ + MemPage *pPage1; + int rc; + u32 n; /* Number of pages on the freelist */ + u32 k; /* Number of leaves on the trunk of the freelist */ + MemPage *pTrunk = 0; + MemPage *pPrevTrunk = 0; + Pgno mxPage; /* Total size of the database file */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); + pPage1 = pBt->pPage1; + mxPage = btreePagecount(pBt); + /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 + ** stores stores the total number of pages on the freelist. */ + n = get4byte(&pPage1->aData[36]); + testcase( n==mxPage-1 ); + if( n>=mxPage ){ + return SQLITE_CORRUPT_BKPT; + } + if( n>0 ){ + /* There are pages on the freelist. Reuse one of those pages. */ + Pgno iTrunk; + u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ + u32 nSearch = 0; /* Count of the number of search attempts */ + + /* If eMode==BTALLOC_EXACT and a query of the pointer-map + ** shows that the page 'nearby' is somewhere on the free-list, then + ** the entire-list will be searched for that page. + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( eMode==BTALLOC_EXACT ){ + if( nearby<=mxPage ){ + u8 eType; + assert( nearby>0 ); + assert( pBt->autoVacuum ); + rc = ptrmapGet(pBt, nearby, &eType, 0); + if( rc ) return rc; + if( eType==PTRMAP_FREEPAGE ){ + searchList = 1; + } + } + }else if( eMode==BTALLOC_LE ){ + searchList = 1; + } +#endif + + /* Decrement the free-list count by 1. Set iTrunk to the index of the + ** first free-list trunk page. iPrevTrunk is initially 1. + */ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc ) return rc; + put4byte(&pPage1->aData[36], n-1); + + /* The code within this loop is run only once if the 'searchList' variable + ** is not true. Otherwise, it runs once for each trunk-page on the + ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) + ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) + */ + do { + pPrevTrunk = pTrunk; + if( pPrevTrunk ){ + /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page + ** is the page number of the next freelist trunk page in the list or + ** zero if this is the last freelist trunk page. */ + iTrunk = get4byte(&pPrevTrunk->aData[0]); + }else{ + /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 + ** stores the page number of the first page of the freelist, or zero if + ** the freelist is empty. */ + iTrunk = get4byte(&pPage1->aData[32]); + } + testcase( iTrunk==mxPage ); + if( iTrunk>mxPage || nSearch++ > n ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); + } + if( rc ){ + pTrunk = 0; + goto end_allocate_page; + } + assert( pTrunk!=0 ); + assert( pTrunk->aData!=0 ); + /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page + ** is the number of leaf page pointers to follow. */ + k = get4byte(&pTrunk->aData[4]); + if( k==0 && !searchList ){ + /* The trunk has no leaves and the list is not being searched. + ** So extract the trunk page itself and use it as the newly + ** allocated page */ + assert( pPrevTrunk==0 ); + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + *pPgno = iTrunk; + memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); + *ppPage = pTrunk; + pTrunk = 0; + TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); + }else if( k>(u32)(pBt->usableSize/4 - 2) ){ + /* Value of k is out of range. Database corruption */ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; +#ifndef SQLITE_OMIT_AUTOVACUUM + }else if( searchList + && (nearby==iTrunk || (iTrunkpDbPage); + if( rc ){ + goto end_allocate_page; + } + if( k==0 ){ + if( !pPrevTrunk ){ + memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); + }else{ + rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); + } + }else{ + /* The trunk page is required by the caller but it contains + ** pointers to free-list leaves. The first leaf becomes a trunk + ** page in this case. + */ + MemPage *pNewTrunk; + Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); + if( iNewTrunk>mxPage ){ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; + } + testcase( iNewTrunk==mxPage ); + rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + rc = sqlite3PagerWrite(pNewTrunk->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pNewTrunk); + goto end_allocate_page; + } + memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); + put4byte(&pNewTrunk->aData[4], k-1); + memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); + releasePage(pNewTrunk); + if( !pPrevTrunk ){ + assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); + put4byte(&pPage1->aData[32], iNewTrunk); + }else{ + rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + put4byte(&pPrevTrunk->aData[0], iNewTrunk); + } + } + pTrunk = 0; + TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); +#endif + }else if( k>0 ){ + /* Extract a leaf from the trunk */ + u32 closest; + Pgno iPage; + unsigned char *aData = pTrunk->aData; + if( nearby>0 ){ + u32 i; + closest = 0; + if( eMode==BTALLOC_LE ){ + for(i=0; imxPage ){ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; + } + testcase( iPage==mxPage ); + if( !searchList + || (iPage==nearby || (iPagepgno, n-1)); + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc ) goto end_allocate_page; + if( closestpDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + *ppPage = 0; + } + } + searchList = 0; + } + } + releasePage(pPrevTrunk); + pPrevTrunk = 0; + }while( searchList ); + }else{ + /* There are no pages on the freelist, so append a new page to the + ** database image. + ** + ** Normally, new pages allocated by this block can be requested from the + ** pager layer with the 'no-content' flag set. This prevents the pager + ** from trying to read the pages content from disk. However, if the + ** current transaction has already run one or more incremental-vacuum + ** steps, then the page we are about to allocate may contain content + ** that is required in the event of a rollback. In this case, do + ** not set the no-content flag. This causes the pager to load and journal + ** the current page content before overwriting it. + ** + ** Note that the pager will not actually attempt to load or journal + ** content for any page that really does lie past the end of the database + ** file on disk. So the effects of disabling the no-content optimization + ** here are confined to those pages that lie between the end of the + ** database image and the end of the database file. + */ + int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; + + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc ) return rc; + pBt->nPage++; + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; + +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ + /* If *pPgno refers to a pointer-map page, allocate two new pages + ** at the end of the file instead of one. The first allocated page + ** becomes a new pointer-map page, the second is used by the caller. + */ + MemPage *pPg = 0; + TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); + assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); + rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pPg->pDbPage); + releasePage(pPg); + } + if( rc ) return rc; + pBt->nPage++; + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } + } +#endif + put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); + *pPgno = pBt->nPage; + + assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); + rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); + if( rc ) return rc; + rc = sqlite3PagerWrite((*ppPage)->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + *ppPage = 0; + } + TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); + } + + assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); + +end_allocate_page: + releasePage(pTrunk); + releasePage(pPrevTrunk); + assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); + assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 ); + return rc; +} + +/* +** This function is used to add page iPage to the database file free-list. +** It is assumed that the page is not already a part of the free-list. +** +** The value passed as the second argument to this function is optional. +** If the caller happens to have a pointer to the MemPage object +** corresponding to page iPage handy, it may pass it as the second value. +** Otherwise, it may pass NULL. +** +** If a pointer to a MemPage object is passed as the second argument, +** its reference count is not altered by this function. +*/ +static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ + MemPage *pTrunk = 0; /* Free-list trunk page */ + Pgno iTrunk = 0; /* Page number of free-list trunk page */ + MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ + MemPage *pPage; /* Page being freed. May be NULL. */ + int rc; /* Return Code */ + int nFree; /* Initial number of pages on free-list */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( CORRUPT_DB || iPage>1 ); + assert( !pMemPage || pMemPage->pgno==iPage ); + + if( iPage<2 ) return SQLITE_CORRUPT_BKPT; + if( pMemPage ){ + pPage = pMemPage; + sqlite3PagerRef(pPage->pDbPage); + }else{ + pPage = btreePageLookup(pBt, iPage); + } + + /* Increment the free page count on pPage1 */ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc ) goto freepage_out; + nFree = get4byte(&pPage1->aData[36]); + put4byte(&pPage1->aData[36], nFree+1); + + if( pBt->btsFlags & BTS_SECURE_DELETE ){ + /* If the secure_delete option is enabled, then + ** always fully overwrite deleted information with zeros. + */ + if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) + || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) + ){ + goto freepage_out; + } + memset(pPage->aData, 0, pPage->pBt->pageSize); + } + + /* If the database supports auto-vacuum, write an entry in the pointer-map + ** to indicate that the page is free. + */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); + if( rc ) goto freepage_out; + } + + /* Now manipulate the actual database free-list structure. There are two + ** possibilities. If the free-list is currently empty, or if the first + ** trunk page in the free-list is full, then this page will become a + ** new free-list trunk page. Otherwise, it will become a leaf of the + ** first trunk page in the current free-list. This block tests if it + ** is possible to add the page as a new free-list leaf. + */ + if( nFree!=0 ){ + u32 nLeaf; /* Initial number of leaf cells on trunk page */ + + iTrunk = get4byte(&pPage1->aData[32]); + rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); + if( rc!=SQLITE_OK ){ + goto freepage_out; + } + + nLeaf = get4byte(&pTrunk->aData[4]); + assert( pBt->usableSize>32 ); + if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ + rc = SQLITE_CORRUPT_BKPT; + goto freepage_out; + } + if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ + /* In this case there is room on the trunk page to insert the page + ** being freed as a new leaf. + ** + ** Note that the trunk page is not really full until it contains + ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have + ** coded. But due to a coding error in versions of SQLite prior to + ** 3.6.0, databases with freelist trunk pages holding more than + ** usableSize/4 - 8 entries will be reported as corrupt. In order + ** to maintain backwards compatibility with older versions of SQLite, + ** we will continue to restrict the number of entries to usableSize/4 - 8 + ** for now. At some point in the future (once everyone has upgraded + ** to 3.6.0 or later) we should consider fixing the conditional above + ** to read "usableSize/4-2" instead of "usableSize/4-8". + ** + ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still + ** avoid using the last six entries in the freelist trunk page array in + ** order that database files created by newer versions of SQLite can be + ** read by older versions of SQLite. + */ + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pTrunk->aData[4], nLeaf+1); + put4byte(&pTrunk->aData[8+nLeaf*4], iPage); + if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ + sqlite3PagerDontWrite(pPage->pDbPage); + } + rc = btreeSetHasContent(pBt, iPage); + } + TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); + goto freepage_out; + } + } + + /* If control flows to this point, then it was not possible to add the + ** the page being freed as a leaf page of the first trunk in the free-list. + ** Possibly because the free-list is empty, or possibly because the + ** first trunk in the free-list is full. Either way, the page being freed + ** will become the new first trunk page in the free-list. + */ + if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ + goto freepage_out; + } + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc!=SQLITE_OK ){ + goto freepage_out; + } + put4byte(pPage->aData, iTrunk); + put4byte(&pPage->aData[4], 0); + put4byte(&pPage1->aData[32], iPage); + TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); + +freepage_out: + if( pPage ){ + pPage->isInit = 0; + } + releasePage(pPage); + releasePage(pTrunk); + return rc; +} +static void freePage(MemPage *pPage, int *pRC){ + if( (*pRC)==SQLITE_OK ){ + *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); + } +} + +/* +** Free any overflow pages associated with the given Cell. Write the +** local Cell size (the number of bytes on the original page, omitting +** overflow) into *pnSize. +*/ +static int clearCell( + MemPage *pPage, /* The page that contains the Cell */ + unsigned char *pCell, /* First byte of the Cell */ + u16 *pnSize /* Write the size of the Cell here */ +){ + BtShared *pBt = pPage->pBt; + CellInfo info; + Pgno ovflPgno; + int rc; + int nOvfl; + u32 ovflPageSize; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + pPage->xParseCell(pPage, pCell, &info); + *pnSize = info.nSize; + if( info.nLocal==info.nPayload ){ + return SQLITE_OK; /* No overflow pages. Return without doing anything */ + } + if( pCell+info.nSize-1 > pPage->aData+pPage->maskPage ){ + return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ + } + ovflPgno = get4byte(pCell + info.nSize - 4); + assert( pBt->usableSize > 4 ); + ovflPageSize = pBt->usableSize - 4; + nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; + assert( nOvfl>0 || + (CORRUPT_DB && (info.nPayload + ovflPageSize)btreePagecount(pBt) ){ + /* 0 is not a legal page number and page 1 cannot be an + ** overflow page. Therefore if ovflPgno<2 or past the end of the + ** file the database must be corrupt. */ + return SQLITE_CORRUPT_BKPT; + } + if( nOvfl ){ + rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); + if( rc ) return rc; + } + + if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) + && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 + ){ + /* There is no reason any cursor should have an outstanding reference + ** to an overflow page belonging to a cell that is being deleted/updated. + ** So if there exists more than one reference to this page, then it + ** must not really be an overflow page and the database must be corrupt. + ** It is helpful to detect this before calling freePage2(), as + ** freePage2() may zero the page contents if secure-delete mode is + ** enabled. If this 'overflow' page happens to be a page that the + ** caller is iterating through or using in some other way, this + ** can be problematic. + */ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = freePage2(pBt, pOvfl, ovflPgno); + } + + if( pOvfl ){ + sqlite3PagerUnref(pOvfl->pDbPage); + } + if( rc ) return rc; + ovflPgno = iNext; + } + return SQLITE_OK; +} + +/* +** Create the byte sequence used to represent a cell on page pPage +** and write that byte sequence into pCell[]. Overflow pages are +** allocated and filled in as necessary. The calling procedure +** is responsible for making sure sufficient space has been allocated +** for pCell[]. +** +** Note that pCell does not necessary need to point to the pPage->aData +** area. pCell might point to some temporary storage. The cell will +** be constructed in this temporary area then copied into pPage->aData +** later. +*/ +static int fillInCell( + MemPage *pPage, /* The page that contains the cell */ + unsigned char *pCell, /* Complete text of the cell */ + const void *pKey, i64 nKey, /* The key */ + const void *pData,int nData, /* The data */ + int nZero, /* Extra zero bytes to append to pData */ + int *pnSize /* Write cell size here */ +){ + int nPayload; + const u8 *pSrc; + int nSrc, n, rc; + int spaceLeft; + MemPage *pOvfl = 0; + MemPage *pToRelease = 0; + unsigned char *pPrior; + unsigned char *pPayload; + BtShared *pBt = pPage->pBt; + Pgno pgnoOvfl = 0; + int nHeader; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + + /* pPage is not necessarily writeable since pCell might be auxiliary + ** buffer space that is separate from the pPage buffer area */ + assert( pCellaData || pCell>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + /* Fill in the header. */ + nHeader = pPage->childPtrSize; + nPayload = nData + nZero; + if( pPage->intKeyLeaf ){ + nHeader += putVarint32(&pCell[nHeader], nPayload); + }else{ + assert( nData==0 ); + assert( nZero==0 ); + } + nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); + + /* Fill in the payload size */ + if( pPage->intKey ){ + pSrc = pData; + nSrc = nData; + nData = 0; + }else{ + assert( nKey<=0x7fffffff && pKey!=0 ); + nPayload = (int)nKey; + pSrc = pKey; + nSrc = (int)nKey; + } + if( nPayload<=pPage->maxLocal ){ + n = nHeader + nPayload; + testcase( n==3 ); + testcase( n==4 ); + if( n<4 ) n = 4; + *pnSize = n; + spaceLeft = nPayload; + pPrior = pCell; + }else{ + int mn = pPage->minLocal; + n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); + testcase( n==pPage->maxLocal ); + testcase( n==pPage->maxLocal+1 ); + if( n > pPage->maxLocal ) n = mn; + spaceLeft = n; + *pnSize = n + nHeader + 4; + pPrior = &pCell[nHeader+n]; + } + pPayload = &pCell[nHeader]; + + /* At this point variables should be set as follows: + ** + ** nPayload Total payload size in bytes + ** pPayload Begin writing payload here + ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, + ** that means content must spill into overflow pages. + ** *pnSize Size of the local cell (not counting overflow pages) + ** pPrior Where to write the pgno of the first overflow page + ** + ** Use a call to btreeParseCellPtr() to verify that the values above + ** were computed correctly. + */ +#if SQLITE_DEBUG + { + CellInfo info; + pPage->xParseCell(pPage, pCell, &info); + assert( nHeader==(int)(info.pPayload - pCell) ); + assert( info.nKey==nKey ); + assert( *pnSize == info.nSize ); + assert( spaceLeft == info.nLocal ); + } +#endif + + /* Write the payload into the local Cell and any extra into overflow pages */ + while( nPayload>0 ){ + if( spaceLeft==0 ){ +#ifndef SQLITE_OMIT_AUTOVACUUM + Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ + if( pBt->autoVacuum ){ + do{ + pgnoOvfl++; + } while( + PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) + ); + } +#endif + rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); +#ifndef SQLITE_OMIT_AUTOVACUUM + /* If the database supports auto-vacuum, and the second or subsequent + ** overflow page is being allocated, add an entry to the pointer-map + ** for that page now. + ** + ** If this is the first overflow page, then write a partial entry + ** to the pointer-map. If we write nothing to this pointer-map slot, + ** then the optimistic overflow chain processing in clearCell() + ** may misinterpret the uninitialized values and delete the + ** wrong pages from the database. + */ + if( pBt->autoVacuum && rc==SQLITE_OK ){ + u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); + ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); + if( rc ){ + releasePage(pOvfl); + } + } +#endif + if( rc ){ + releasePage(pToRelease); + return rc; + } + + /* If pToRelease is not zero than pPrior points into the data area + ** of pToRelease. Make sure pToRelease is still writeable. */ + assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); + + /* If pPrior is part of the data area of pPage, then make sure pPage + ** is still writeable */ + assert( pPrioraData || pPrior>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + put4byte(pPrior, pgnoOvfl); + releasePage(pToRelease); + pToRelease = pOvfl; + pPrior = pOvfl->aData; + put4byte(pPrior, 0); + pPayload = &pOvfl->aData[4]; + spaceLeft = pBt->usableSize - 4; + } + n = nPayload; + if( n>spaceLeft ) n = spaceLeft; + + /* If pToRelease is not zero than pPayload points into the data area + ** of pToRelease. Make sure pToRelease is still writeable. */ + assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); + + /* If pPayload is part of the data area of pPage, then make sure pPage + ** is still writeable */ + assert( pPayloadaData || pPayload>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + if( nSrc>0 ){ + if( n>nSrc ) n = nSrc; + assert( pSrc ); + memcpy(pPayload, pSrc, n); + }else{ + memset(pPayload, 0, n); + } + nPayload -= n; + pPayload += n; + pSrc += n; + nSrc -= n; + spaceLeft -= n; + if( nSrc==0 ){ + nSrc = nData; + pSrc = pData; + } + } + releasePage(pToRelease); + return SQLITE_OK; +} + +/* +** Remove the i-th cell from pPage. This routine effects pPage only. +** The cell content is not freed or deallocated. It is assumed that +** the cell content has been copied someplace else. This routine just +** removes the reference to the cell from pPage. +** +** "sz" must be the number of bytes in the cell. +*/ +static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ + u32 pc; /* Offset to cell content of cell being deleted */ + u8 *data; /* pPage->aData */ + u8 *ptr; /* Used to move bytes around within data[] */ + int rc; /* The return code */ + int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ + + if( *pRC ) return; + + assert( idx>=0 && idxnCell ); + assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + data = pPage->aData; + ptr = &pPage->aCellIdx[2*idx]; + pc = get2byte(ptr); + hdr = pPage->hdrOffset; + testcase( pc==get2byte(&data[hdr+5]) ); + testcase( pc+sz==pPage->pBt->usableSize ); + if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ + *pRC = SQLITE_CORRUPT_BKPT; + return; + } + rc = freeSpace(pPage, pc, sz); + if( rc ){ + *pRC = rc; + return; + } + pPage->nCell--; + if( pPage->nCell==0 ){ + memset(&data[hdr+1], 0, 4); + data[hdr+7] = 0; + put2byte(&data[hdr+5], pPage->pBt->usableSize); + pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset + - pPage->childPtrSize - 8; + }else{ + memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); + put2byte(&data[hdr+3], pPage->nCell); + pPage->nFree += 2; + } +} + +/* +** Insert a new cell on pPage at cell index "i". pCell points to the +** content of the cell. +** +** If the cell content will fit on the page, then put it there. If it +** will not fit, then make a copy of the cell content into pTemp if +** pTemp is not null. Regardless of pTemp, allocate a new entry +** in pPage->apOvfl[] and make it point to the cell content (either +** in pTemp or the original pCell) and also record its index. +** Allocating a new entry in pPage->aCell[] implies that +** pPage->nOverflow is incremented. +*/ +static void insertCell( + MemPage *pPage, /* Page into which we are copying */ + int i, /* New cell becomes the i-th cell of the page */ + u8 *pCell, /* Content of the new cell */ + int sz, /* Bytes of content in pCell */ + u8 *pTemp, /* Temp storage space for pCell, if needed */ + Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ + int *pRC /* Read and write return code from here */ +){ + int idx = 0; /* Where to write new cell content in data[] */ + int j; /* Loop counter */ + u8 *data; /* The content of the whole page */ + u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ + + if( *pRC ) return; + + assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); + assert( MX_CELL(pPage->pBt)<=10921 ); + assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); + assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); + assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + /* The cell should normally be sized correctly. However, when moving a + ** malformed cell from a leaf page to an interior page, if the cell size + ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size + ** might be less than 8 (leaf-size + pointer) on the interior node. Hence + ** the term after the || in the following assert(). */ + assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); + if( pPage->nOverflow || sz+2>pPage->nFree ){ + if( pTemp ){ + memcpy(pTemp, pCell, sz); + pCell = pTemp; + } + if( iChild ){ + put4byte(pCell, iChild); + } + j = pPage->nOverflow++; + assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); + pPage->apOvfl[j] = pCell; + pPage->aiOvfl[j] = (u16)i; + + /* When multiple overflows occur, they are always sequential and in + ** sorted order. This invariants arise because multiple overflows can + ** only occur when inserting divider cells into the parent page during + ** balancing, and the dividers are adjacent and sorted. + */ + assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ + assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ + }else{ + int rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + data = pPage->aData; + assert( &data[pPage->cellOffset]==pPage->aCellIdx ); + rc = allocateSpace(pPage, sz, &idx); + if( rc ){ *pRC = rc; return; } + /* The allocateSpace() routine guarantees the following properties + ** if it returns successfully */ + assert( idx >= 0 ); + assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); + assert( idx+sz <= (int)pPage->pBt->usableSize ); + pPage->nFree -= (u16)(2 + sz); + memcpy(&data[idx], pCell, sz); + if( iChild ){ + put4byte(&data[idx], iChild); + } + pIns = pPage->aCellIdx + i*2; + memmove(pIns+2, pIns, 2*(pPage->nCell - i)); + put2byte(pIns, idx); + pPage->nCell++; + /* increment the cell count */ + if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; + assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell ); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pPage->pBt->autoVacuum ){ + /* The cell may contain a pointer to an overflow page. If so, write + ** the entry for the overflow page into the pointer map. + */ + ptrmapPutOvflPtr(pPage, pCell, pRC); + } +#endif + } +} + +/* +** A CellArray object contains a cache of pointers and sizes for a +** consecutive sequence of cells that might be held multiple pages. +*/ +typedef struct CellArray CellArray; +struct CellArray { + int nCell; /* Number of cells in apCell[] */ + MemPage *pRef; /* Reference page */ + u8 **apCell; /* All cells begin balanced */ + u16 *szCell; /* Local size of all cells in apCell[] */ +}; + +/* +** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been +** computed. +*/ +static void populateCellCache(CellArray *p, int idx, int N){ + assert( idx>=0 && idx+N<=p->nCell ); + while( N>0 ){ + assert( p->apCell[idx]!=0 ); + if( p->szCell[idx]==0 ){ + p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); + }else{ + assert( CORRUPT_DB || + p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); + } + idx++; + N--; + } +} + +/* +** Return the size of the Nth element of the cell array +*/ +static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ + assert( N>=0 && NnCell ); + assert( p->szCell[N]==0 ); + p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); + return p->szCell[N]; +} +static u16 cachedCellSize(CellArray *p, int N){ + assert( N>=0 && NnCell ); + if( p->szCell[N] ) return p->szCell[N]; + return computeCellSize(p, N); +} + +/* +** Array apCell[] contains pointers to nCell b-tree page cells. The +** szCell[] array contains the size in bytes of each cell. This function +** replaces the current contents of page pPg with the contents of the cell +** array. +** +** Some of the cells in apCell[] may currently be stored in pPg. This +** function works around problems caused by this by making a copy of any +** such cells before overwriting the page data. +** +** The MemPage.nFree field is invalidated by this function. It is the +** responsibility of the caller to set it correctly. +*/ +static int rebuildPage( + MemPage *pPg, /* Edit this page */ + int nCell, /* Final number of cells on page */ + u8 **apCell, /* Array of cells */ + u16 *szCell /* Array of cell sizes */ +){ + const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ + u8 * const aData = pPg->aData; /* Pointer to data for pPg */ + const int usableSize = pPg->pBt->usableSize; + u8 * const pEnd = &aData[usableSize]; + int i; + u8 *pCellptr = pPg->aCellIdx; + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); + u8 *pData; + + i = get2byte(&aData[hdr+5]); + memcpy(&pTmp[i], &aData[i], usableSize - i); + + pData = pEnd; + for(i=0; ixCellSize(pPg, pCell) || CORRUPT_DB ); + testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); + } + + /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ + pPg->nCell = nCell; + pPg->nOverflow = 0; + + put2byte(&aData[hdr+1], 0); + put2byte(&aData[hdr+3], pPg->nCell); + put2byte(&aData[hdr+5], pData - aData); + aData[hdr+7] = 0x00; + return SQLITE_OK; +} + +/* +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell +** contains the size in bytes of each such cell. This function attempts to +** add the cells stored in the array to page pPg. If it cannot (because +** the page needs to be defragmented before the cells will fit), non-zero +** is returned. Otherwise, if the cells are added successfully, zero is +** returned. +** +** Argument pCellptr points to the first entry in the cell-pointer array +** (part of page pPg) to populate. After cell apCell[0] is written to the +** page body, a 16-bit offset is written to pCellptr. And so on, for each +** cell in the array. It is the responsibility of the caller to ensure +** that it is safe to overwrite this part of the cell-pointer array. +** +** When this function is called, *ppData points to the start of the +** content area on page pPg. If the size of the content area is extended, +** *ppData is updated to point to the new start of the content area +** before returning. +** +** Finally, argument pBegin points to the byte immediately following the +** end of the space required by this page for the cell-pointer area (for +** all cells - not just those inserted by the current call). If the content +** area must be extended to before this point in order to accomodate all +** cells in apCell[], then the cells do not fit and non-zero is returned. +*/ +static int pageInsertArray( + MemPage *pPg, /* Page to add cells to */ + u8 *pBegin, /* End of cell-pointer array */ + u8 **ppData, /* IN/OUT: Page content -area pointer */ + u8 *pCellptr, /* Pointer to cell-pointer area */ + int iFirst, /* Index of first cell to add */ + int nCell, /* Number of cells to add to pPg */ + CellArray *pCArray /* Array of cells */ +){ + int i; + u8 *aData = pPg->aData; + u8 *pData = *ppData; + int iEnd = iFirst + nCell; + assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ + for(i=iFirst; iapCell[i] will never overlap on a well-formed + ** database. But they might for a corrupt database. Hence use memmove() + ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ + assert( (pSlot+sz)<=pCArray->apCell[i] + || pSlot>=(pCArray->apCell[i]+sz) + || CORRUPT_DB ); + memmove(pSlot, pCArray->apCell[i], sz); + put2byte(pCellptr, (pSlot - aData)); + pCellptr += 2; + } + *ppData = pData; + return 0; +} + +/* +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell +** contains the size in bytes of each such cell. This function adds the +** space associated with each cell in the array that is currently stored +** within the body of pPg to the pPg free-list. The cell-pointers and other +** fields of the page are not updated. +** +** This function returns the total number of cells added to the free-list. +*/ +static int pageFreeArray( + MemPage *pPg, /* Page to edit */ + int iFirst, /* First cell to delete */ + int nCell, /* Cells to delete */ + CellArray *pCArray /* Array of cells */ +){ + u8 * const aData = pPg->aData; + u8 * const pEnd = &aData[pPg->pBt->usableSize]; + u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; + int nRet = 0; + int i; + int iEnd = iFirst + nCell; + u8 *pFree = 0; + int szFree = 0; + + for(i=iFirst; iapCell[i]; + if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ + int sz; + /* No need to use cachedCellSize() here. The sizes of all cells that + ** are to be freed have already been computing while deciding which + ** cells need freeing */ + sz = pCArray->szCell[i]; assert( sz>0 ); + if( pFree!=(pCell + sz) ){ + if( pFree ){ + assert( pFree>aData && (pFree - aData)<65536 ); + freeSpace(pPg, (u16)(pFree - aData), szFree); + } + pFree = pCell; + szFree = sz; + if( pFree+sz>pEnd ) return 0; + }else{ + pFree = pCell; + szFree += sz; + } + nRet++; + } + } + if( pFree ){ + assert( pFree>aData && (pFree - aData)<65536 ); + freeSpace(pPg, (u16)(pFree - aData), szFree); + } + return nRet; +} + +/* +** apCell[] and szCell[] contains pointers to and sizes of all cells in the +** pages being balanced. The current page, pPg, has pPg->nCell cells starting +** with apCell[iOld]. After balancing, this page should hold nNew cells +** starting at apCell[iNew]. +** +** This routine makes the necessary adjustments to pPg so that it contains +** the correct cells after being balanced. +** +** The pPg->nFree field is invalid when this function returns. It is the +** responsibility of the caller to set it correctly. +*/ +static int editPage( + MemPage *pPg, /* Edit this page */ + int iOld, /* Index of first cell currently on page */ + int iNew, /* Index of new first cell on page */ + int nNew, /* Final number of cells on page */ + CellArray *pCArray /* Array of cells and sizes */ +){ + u8 * const aData = pPg->aData; + const int hdr = pPg->hdrOffset; + u8 *pBegin = &pPg->aCellIdx[nNew * 2]; + int nCell = pPg->nCell; /* Cells stored on pPg */ + u8 *pData; + u8 *pCellptr; + int i; + int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; + int iNewEnd = iNew + nNew; + +#ifdef SQLITE_DEBUG + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); + memcpy(pTmp, aData, pPg->pBt->usableSize); +#endif + + /* Remove cells from the start and end of the page */ + if( iOldaCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); + nCell -= nShift; + } + if( iNewEnd < iOldEnd ){ + nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); + } + + pData = &aData[get2byteNotZero(&aData[hdr+5])]; + if( pDataaCellIdx; + memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, + iNew, nAdd, pCArray + ) ) goto editpage_fail; + nCell += nAdd; + } + + /* Add any overflow cells */ + for(i=0; inOverflow; i++){ + int iCell = (iOld + pPg->aiOvfl[i]) - iNew; + if( iCell>=0 && iCellaCellIdx[iCell * 2]; + memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); + nCell++; + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, + iCell+iNew, 1, pCArray + ) ) goto editpage_fail; + } + } + + /* Append cells to the end of the page */ + pCellptr = &pPg->aCellIdx[nCell*2]; + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, + iNew+nCell, nNew-nCell, pCArray + ) ) goto editpage_fail; + + pPg->nCell = nNew; + pPg->nOverflow = 0; + + put2byte(&aData[hdr+3], pPg->nCell); + put2byte(&aData[hdr+5], pData - aData); + +#ifdef SQLITE_DEBUG + for(i=0; iapCell[i+iNew]; + int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); + if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){ + pCell = &pTmp[pCell - aData]; + } + assert( 0==memcmp(pCell, &aData[iOff], + pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); + } +#endif + + return SQLITE_OK; + editpage_fail: + /* Unable to edit this page. Rebuild it from scratch instead. */ + populateCellCache(pCArray, iNew, nNew); + return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); +} + +/* +** The following parameters determine how many adjacent pages get involved +** in a balancing operation. NN is the number of neighbors on either side +** of the page that participate in the balancing operation. NB is the +** total number of pages that participate, including the target page and +** NN neighbors on either side. +** +** The minimum value of NN is 1 (of course). Increasing NN above 1 +** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance +** in exchange for a larger degradation in INSERT and UPDATE performance. +** The value of NN appears to give the best results overall. +*/ +#define NN 1 /* Number of neighbors on either side of pPage */ +#define NB (NN*2+1) /* Total pages involved in the balance */ + + +#ifndef SQLITE_OMIT_QUICKBALANCE +/* +** This version of balance() handles the common special case where +** a new entry is being inserted on the extreme right-end of the +** tree, in other words, when the new entry will become the largest +** entry in the tree. +** +** Instead of trying to balance the 3 right-most leaf pages, just add +** a new page to the right-hand side and put the one new entry in +** that page. This leaves the right side of the tree somewhat +** unbalanced. But odds are that we will be inserting new entries +** at the end soon afterwards so the nearly empty page will quickly +** fill up. On average. +** +** pPage is the leaf page which is the right-most page in the tree. +** pParent is its parent. pPage must have a single overflow entry +** which is also the right-most entry on the page. +** +** The pSpace buffer is used to store a temporary copy of the divider +** cell that will be inserted into pParent. Such a cell consists of a 4 +** byte page number followed by a variable length integer. In other +** words, at most 13 bytes. Hence the pSpace buffer must be at +** least 13 bytes in size. +*/ +static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + BtShared *const pBt = pPage->pBt; /* B-Tree Database */ + MemPage *pNew; /* Newly allocated page */ + int rc; /* Return Code */ + Pgno pgnoNew; /* Page number of pNew */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + assert( pPage->nOverflow==1 ); + + /* This error condition is now caught prior to reaching this function */ + if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; + + /* Allocate a new page. This page will become the right-sibling of + ** pPage. Make the parent page writable, so that the new divider cell + ** may be inserted. If both these operations are successful, proceed. + */ + rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); + + if( rc==SQLITE_OK ){ + + u8 *pOut = &pSpace[4]; + u8 *pCell = pPage->apOvfl[0]; + u16 szCell = pPage->xCellSize(pPage, pCell); + u8 *pStop; + + assert( sqlite3PagerIswriteable(pNew->pDbPage) ); + assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); + zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); + rc = rebuildPage(pNew, 1, &pCell, &szCell); + if( NEVER(rc) ) return rc; + pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; + + /* If this is an auto-vacuum database, update the pointer map + ** with entries for the new page, and any pointer from the + ** cell on the page to an overflow page. If either of these + ** operations fails, the return code is set, but the contents + ** of the parent page are still manipulated by thh code below. + ** That is Ok, at this point the parent page is guaranteed to + ** be marked as dirty. Returning an error code will cause a + ** rollback, undoing any changes made to the parent page. + */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); + if( szCell>pNew->minLocal ){ + ptrmapPutOvflPtr(pNew, pCell, &rc); + } + } + + /* Create a divider cell to insert into pParent. The divider cell + ** consists of a 4-byte page number (the page number of pPage) and + ** a variable length key value (which must be the same value as the + ** largest key on pPage). + ** + ** To find the largest key value on pPage, first find the right-most + ** cell on pPage. The first two fields of this cell are the + ** record-length (a variable length integer at most 32-bits in size) + ** and the key value (a variable length integer, may have any value). + ** The first of the while(...) loops below skips over the record-length + ** field. The second while(...) loop copies the key value from the + ** cell on pPage into the pSpace buffer. + */ + pCell = findCell(pPage, pPage->nCell-1); + pStop = &pCell[9]; + while( (*(pCell++)&0x80) && pCellnCell, pSpace, (int)(pOut-pSpace), + 0, pPage->pgno, &rc); + + /* Set the right-child pointer of pParent to point to the new page. */ + put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); + + /* Release the reference to the new page. */ + releasePage(pNew); + } + + return rc; +} +#endif /* SQLITE_OMIT_QUICKBALANCE */ + +#if 0 +/* +** This function does not contribute anything to the operation of SQLite. +** it is sometimes activated temporarily while debugging code responsible +** for setting pointer-map entries. +*/ +static int ptrmapCheckPages(MemPage **apPage, int nPage){ + int i, j; + for(i=0; ipBt; + assert( pPage->isInit ); + + for(j=0; jnCell; j++){ + CellInfo info; + u8 *z; + + z = findCell(pPage, j); + pPage->xParseCell(pPage, z, &info); + if( info.nLocalpgno && e==PTRMAP_OVERFLOW1 ); + } + if( !pPage->leaf ){ + Pgno child = get4byte(z); + ptrmapGet(pBt, child, &e, &n); + assert( n==pPage->pgno && e==PTRMAP_BTREE ); + } + } + if( !pPage->leaf ){ + Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); + ptrmapGet(pBt, child, &e, &n); + assert( n==pPage->pgno && e==PTRMAP_BTREE ); + } + } + return 1; +} +#endif + +/* +** This function is used to copy the contents of the b-tree node stored +** on page pFrom to page pTo. If page pFrom was not a leaf page, then +** the pointer-map entries for each child page are updated so that the +** parent page stored in the pointer map is page pTo. If pFrom contained +** any cells with overflow page pointers, then the corresponding pointer +** map entries are also updated so that the parent page is page pTo. +** +** If pFrom is currently carrying any overflow cells (entries in the +** MemPage.apOvfl[] array), they are not copied to pTo. +** +** Before returning, page pTo is reinitialized using btreeInitPage(). +** +** The performance of this function is not critical. It is only used by +** the balance_shallower() and balance_deeper() procedures, neither of +** which are called often under normal circumstances. +*/ +static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ + if( (*pRC)==SQLITE_OK ){ + BtShared * const pBt = pFrom->pBt; + u8 * const aFrom = pFrom->aData; + u8 * const aTo = pTo->aData; + int const iFromHdr = pFrom->hdrOffset; + int const iToHdr = ((pTo->pgno==1) ? 100 : 0); + int rc; + int iData; + + + assert( pFrom->isInit ); + assert( pFrom->nFree>=iToHdr ); + assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); + + /* Copy the b-tree node content from page pFrom to page pTo. */ + iData = get2byte(&aFrom[iFromHdr+5]); + memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); + memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); + + /* Reinitialize page pTo so that the contents of the MemPage structure + ** match the new data. The initialization of pTo can actually fail under + ** fairly obscure circumstances, even though it is a copy of initialized + ** page pFrom. + */ + pTo->isInit = 0; + rc = btreeInitPage(pTo); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + + /* If this is an auto-vacuum database, update the pointer-map entries + ** for any b-tree or overflow pages that pTo now contains the pointers to. + */ + if( ISAUTOVACUUM ){ + *pRC = setChildPtrmaps(pTo); + } + } +} + +/* +** This routine redistributes cells on the iParentIdx'th child of pParent +** (hereafter "the page") and up to 2 siblings so that all pages have about the +** same amount of free space. Usually a single sibling on either side of the +** page are used in the balancing, though both siblings might come from one +** side if the page is the first or last child of its parent. If the page +** has fewer than 2 siblings (something which can only happen if the page +** is a root page or a child of a root page) then all available siblings +** participate in the balancing. +** +** The number of siblings of the page might be increased or decreased by +** one or two in an effort to keep pages nearly full but not over full. +** +** Note that when this routine is called, some of the cells on the page +** might not actually be stored in MemPage.aData[]. This can happen +** if the page is overfull. This routine ensures that all cells allocated +** to the page and its siblings fit into MemPage.aData[] before returning. +** +** In the course of balancing the page and its siblings, cells may be +** inserted into or removed from the parent page (pParent). Doing so +** may cause the parent page to become overfull or underfull. If this +** happens, it is the responsibility of the caller to invoke the correct +** balancing routine to fix this problem (see the balance() routine). +** +** If this routine fails for any reason, it might leave the database +** in a corrupted state. So if this routine fails, the database should +** be rolled back. +** +** The third argument to this function, aOvflSpace, is a pointer to a +** buffer big enough to hold one page. If while inserting cells into the parent +** page (pParent) the parent page becomes overfull, this buffer is +** used to store the parent's overflow cells. Because this function inserts +** a maximum of four divider cells into the parent page, and the maximum +** size of a cell stored within an internal node is always less than 1/4 +** of the page-size, the aOvflSpace[] buffer is guaranteed to be large +** enough for all overflow cells. +** +** If aOvflSpace is set to a null pointer, this function returns +** SQLITE_NOMEM. +*/ +static int balance_nonroot( + MemPage *pParent, /* Parent page of siblings being balanced */ + int iParentIdx, /* Index of "the page" in pParent */ + u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ + int isRoot, /* True if pParent is a root-page */ + int bBulk /* True if this call is part of a bulk load */ +){ + BtShared *pBt; /* The whole database */ + int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ + int nNew = 0; /* Number of pages in apNew[] */ + int nOld; /* Number of pages in apOld[] */ + int i, j, k; /* Loop counters */ + int nxDiv; /* Next divider slot in pParent->aCell[] */ + int rc = SQLITE_OK; /* The return code */ + u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ + int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ + int usableSpace; /* Bytes in pPage beyond the header */ + int pageFlags; /* Value of pPage->aData[0] */ + int iSpace1 = 0; /* First unused byte of aSpace1[] */ + int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ + int szScratch; /* Size of scratch memory requested */ + MemPage *apOld[NB]; /* pPage and up to two siblings */ + MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ + u8 *pRight; /* Location in parent of right-sibling pointer */ + u8 *apDiv[NB-1]; /* Divider cells in pParent */ + int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ + int cntOld[NB+2]; /* Old index in b.apCell[] */ + int szNew[NB+2]; /* Combined size of cells placed on i-th page */ + u8 *aSpace1; /* Space for copies of dividers cells */ + Pgno pgno; /* Temp var to store a page number in */ + u8 abDone[NB+2]; /* True after i'th new page is populated */ + Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ + Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ + u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ + CellArray b; /* Parsed information on cells being balanced */ + + memset(abDone, 0, sizeof(abDone)); + b.nCell = 0; + b.apCell = 0; + pBt = pParent->pBt; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + +#if 0 + TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); +#endif + + /* At this point pParent may have at most one overflow cell. And if + ** this overflow cell is present, it must be the cell with + ** index iParentIdx. This scenario comes about when this function + ** is called (indirectly) from sqlite3BtreeDelete(). + */ + assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); + assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); + + if( !aOvflSpace ){ + return SQLITE_NOMEM_BKPT; + } + + /* Find the sibling pages to balance. Also locate the cells in pParent + ** that divide the siblings. An attempt is made to find NN siblings on + ** either side of pPage. More siblings are taken from one side, however, + ** if there are fewer than NN siblings on the other side. If pParent + ** has NB or fewer children then all children of pParent are taken. + ** + ** This loop also drops the divider cells from the parent page. This + ** way, the remainder of the function does not have to deal with any + ** overflow cells in the parent page, since if any existed they will + ** have already been removed. + */ + i = pParent->nOverflow + pParent->nCell; + if( i<2 ){ + nxDiv = 0; + }else{ + assert( bBulk==0 || bBulk==1 ); + if( iParentIdx==0 ){ + nxDiv = 0; + }else if( iParentIdx==i ){ + nxDiv = i-2+bBulk; + }else{ + nxDiv = iParentIdx-1; + } + i = 2-bBulk; + } + nOld = i+1; + if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ + pRight = &pParent->aData[pParent->hdrOffset+8]; + }else{ + pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); + } + pgno = get4byte(pRight); + while( 1 ){ + rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); + if( rc ){ + memset(apOld, 0, (i+1)*sizeof(MemPage*)); + goto balance_cleanup; + } + nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; + if( (i--)==0 ) break; + + if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ + apDiv[i] = pParent->apOvfl[0]; + pgno = get4byte(apDiv[i]); + szNew[i] = pParent->xCellSize(pParent, apDiv[i]); + pParent->nOverflow = 0; + }else{ + apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); + pgno = get4byte(apDiv[i]); + szNew[i] = pParent->xCellSize(pParent, apDiv[i]); + + /* Drop the cell from the parent page. apDiv[i] still points to + ** the cell within the parent, even though it has been dropped. + ** This is safe because dropping a cell only overwrites the first + ** four bytes of it, and this function does not need the first + ** four bytes of the divider cell. So the pointer is safe to use + ** later on. + ** + ** But not if we are in secure-delete mode. In secure-delete mode, + ** the dropCell() routine will overwrite the entire cell with zeroes. + ** In this case, temporarily copy the cell into the aOvflSpace[] + ** buffer. It will be copied out again as soon as the aSpace[] buffer + ** is allocated. */ + if( pBt->btsFlags & BTS_SECURE_DELETE ){ + int iOff; + + iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); + if( (iOff+szNew[i])>(int)pBt->usableSize ){ + rc = SQLITE_CORRUPT_BKPT; + memset(apOld, 0, (i+1)*sizeof(MemPage*)); + goto balance_cleanup; + }else{ + memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); + apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; + } + } + dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); + } + } + + /* Make nMaxCells a multiple of 4 in order to preserve 8-byte + ** alignment */ + nMaxCells = (nMaxCells + 3)&~3; + + /* + ** Allocate space for memory structures + */ + szScratch = + nMaxCells*sizeof(u8*) /* b.apCell */ + + nMaxCells*sizeof(u16) /* b.szCell */ + + pBt->pageSize; /* aSpace1 */ + + /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer + ** that is more than 6 times the database page size. */ + assert( szScratch<=6*(int)pBt->pageSize ); + b.apCell = sqlite3ScratchMalloc( szScratch ); + if( b.apCell==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto balance_cleanup; + } + b.szCell = (u16*)&b.apCell[nMaxCells]; + aSpace1 = (u8*)&b.szCell[nMaxCells]; + assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); + + /* + ** Load pointers to all cells on sibling pages and the divider cells + ** into the local b.apCell[] array. Make copies of the divider cells + ** into space obtained from aSpace1[]. The divider cells have already + ** been removed from pParent. + ** + ** If the siblings are on leaf pages, then the child pointers of the + ** divider cells are stripped from the cells before they are copied + ** into aSpace1[]. In this way, all cells in b.apCell[] are without + ** child pointers. If siblings are not leaves, then all cell in + ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] + ** are alike. + ** + ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. + ** leafData: 1 if pPage holds key+data and pParent holds only keys. + */ + b.pRef = apOld[0]; + leafCorrection = b.pRef->leaf*4; + leafData = b.pRef->intKeyLeaf; + for(i=0; inCell; + u8 *aData = pOld->aData; + u16 maskPage = pOld->maskPage; + u8 *piCell = aData + pOld->cellOffset; + u8 *piEnd; + + /* Verify that all sibling pages are of the same "type" (table-leaf, + ** table-interior, index-leaf, or index-interior). + */ + if( pOld->aData[0]!=apOld[0]->aData[0] ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } + + /* Load b.apCell[] with pointers to all cells in pOld. If pOld + ** constains overflow cells, include them in the b.apCell[] array + ** in the correct spot. + ** + ** Note that when there are multiple overflow cells, it is always the + ** case that they are sequential and adjacent. This invariant arises + ** because multiple overflows can only occurs when inserting divider + ** cells into a parent on a prior balance, and divider cells are always + ** adjacent and are inserted in order. There is an assert() tagged + ** with "NOTE 1" in the overflow cell insertion loop to prove this + ** invariant. + ** + ** This must be done in advance. Once the balance starts, the cell + ** offset section of the btree page will be overwritten and we will no + ** long be able to find the cells if a pointer to each cell is not saved + ** first. + */ + memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); + if( pOld->nOverflow>0 ){ + limit = pOld->aiOvfl[0]; + for(j=0; jnOverflow; k++){ + assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ + b.apCell[b.nCell] = pOld->apOvfl[k]; + b.nCell++; + } + } + piEnd = aData + pOld->cellOffset + 2*pOld->nCell; + while( piCellmaxLocal+23 ); + assert( iSpace1 <= (int)pBt->pageSize ); + memcpy(pTemp, apDiv[i], sz); + b.apCell[b.nCell] = pTemp+leafCorrection; + assert( leafCorrection==0 || leafCorrection==4 ); + b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; + if( !pOld->leaf ){ + assert( leafCorrection==0 ); + assert( pOld->hdrOffset==0 ); + /* The right pointer of the child page pOld becomes the left + ** pointer of the divider cell */ + memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); + }else{ + assert( leafCorrection==4 ); + while( b.szCell[b.nCell]<4 ){ + /* Do not allow any cells smaller than 4 bytes. If a smaller cell + ** does exist, pad it with 0x00 bytes. */ + assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); + assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); + aSpace1[iSpace1++] = 0x00; + b.szCell[b.nCell]++; + } + } + b.nCell++; + } + } + + /* + ** Figure out the number of pages needed to hold all b.nCell cells. + ** Store this number in "k". Also compute szNew[] which is the total + ** size of all cells on the i-th page and cntNew[] which is the index + ** in b.apCell[] of the cell that divides page i from page i+1. + ** cntNew[k] should equal b.nCell. + ** + ** Values computed by this block: + ** + ** k: The total number of sibling pages + ** szNew[i]: Spaced used on the i-th sibling page. + ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to + ** the right of the i-th sibling page. + ** usableSpace: Number of bytes of space available on each sibling. + ** + */ + usableSpace = pBt->usableSize - 12 + leafCorrection; + for(i=0; inFree; + if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + for(j=0; jnOverflow; j++){ + szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); + } + cntNew[i] = cntOld[i]; + } + k = nOld; + for(i=0; iusableSpace ){ + if( i+1>=k ){ + k = i+2; + if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + szNew[k-1] = 0; + cntNew[k-1] = b.nCell; + } + sz = 2 + cachedCellSize(&b, cntNew[i]-1); + szNew[i] -= sz; + if( !leafData ){ + if( cntNew[i]usableSpace ) break; + szNew[i] += sz; + cntNew[i]++; + if( !leafData ){ + if( cntNew[i]=b.nCell ){ + k = i+1; + }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } + } + + /* + ** The packing computed by the previous block is biased toward the siblings + ** on the left side (siblings with smaller keys). The left siblings are + ** always nearly full, while the right-most sibling might be nearly empty. + ** The next block of code attempts to adjust the packing of siblings to + ** get a better balance. + ** + ** This adjustment is more than an optimization. The packing above might + ** be so out of balance as to be illegal. For example, the right-most + ** sibling might be completely empty. This adjustment is not optional. + */ + for(i=k-1; i>0; i--){ + int szRight = szNew[i]; /* Size of sibling on the right */ + int szLeft = szNew[i-1]; /* Size of sibling on the left */ + int r; /* Index of right-most cell in left sibling */ + int d; /* Index of first cell to the left of right sibling */ + + r = cntNew[i-1] - 1; + d = r + 1 - leafData; + (void)cachedCellSize(&b, d); + do{ + assert( d szLeft-(b.szCell[r]+2)) ){ + break; + } + szRight += b.szCell[d] + 2; + szLeft -= b.szCell[r] + 2; + cntNew[i-1] = r; + r--; + d--; + }while( r>=0 ); + szNew[i] = szRight; + szNew[i-1] = szLeft; + if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } + } + + /* Sanity check: For a non-corrupt database file one of the follwing + ** must be true: + ** (1) We found one or more cells (cntNew[0])>0), or + ** (2) pPage is a virtual root page. A virtual root page is when + ** the real root page is page 1 and we are the only child of + ** that page. + */ + assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); + TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", + apOld[0]->pgno, apOld[0]->nCell, + nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, + nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 + )); + + /* + ** Allocate k new pages. Reuse old pages where possible. + */ + pageFlags = apOld[0]->aData[0]; + for(i=0; ipDbPage); + nNew++; + if( rc ) goto balance_cleanup; + }else{ + assert( i>0 ); + rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); + if( rc ) goto balance_cleanup; + zeroPage(pNew, pageFlags); + apNew[i] = pNew; + nNew++; + cntOld[i] = b.nCell; + + /* Set the pointer-map entry for the new sibling page. */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); + if( rc!=SQLITE_OK ){ + goto balance_cleanup; + } + } + } + } + + /* + ** Reassign page numbers so that the new pages are in ascending order. + ** This helps to keep entries in the disk file in order so that a scan + ** of the table is closer to a linear scan through the file. That in turn + ** helps the operating system to deliver pages from the disk more rapidly. + ** + ** An O(n^2) insertion sort algorithm is used, but since n is never more + ** than (NB+2) (a small constant), that should not be a problem. + ** + ** When NB==3, this one optimization makes the database about 25% faster + ** for large insertions and deletions. + */ + for(i=0; ipgno; + aPgFlags[i] = apNew[i]->pDbPage->flags; + for(j=0; ji ){ + sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); + } + sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); + apNew[i]->pgno = pgno; + } + } + + TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " + "%d(%d nc=%d) %d(%d nc=%d)\n", + apNew[0]->pgno, szNew[0], cntNew[0], + nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, + nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, + nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, + nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, + nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, + nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, + nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, + nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 + )); + + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + put4byte(pRight, apNew[nNew-1]->pgno); + + /* If the sibling pages are not leaves, ensure that the right-child pointer + ** of the right-most new sibling page is set to the value that was + ** originally in the same field of the right-most old sibling page. */ + if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ + MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; + memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); + } + + /* Make any required updates to pointer map entries associated with + ** cells stored on sibling pages following the balance operation. Pointer + ** map entries associated with divider cells are set by the insertCell() + ** routine. The associated pointer map entries are: + ** + ** a) if the cell contains a reference to an overflow chain, the + ** entry associated with the first page in the overflow chain, and + ** + ** b) if the sibling pages are not leaves, the child page associated + ** with the cell. + ** + ** If the sibling pages are not leaves, then the pointer map entry + ** associated with the right-child of each sibling may also need to be + ** updated. This happens below, after the sibling pages have been + ** populated, not here. + */ + if( ISAUTOVACUUM ){ + MemPage *pNew = apNew[0]; + u8 *aOld = pNew->aData; + int cntOldNext = pNew->nCell + pNew->nOverflow; + int usableSize = pBt->usableSize; + int iNew = 0; + int iOld = 0; + + for(i=0; inCell + pOld->nOverflow + !leafData; + aOld = pOld->aData; + } + if( i==cntNew[iNew] ){ + pNew = apNew[++iNew]; + if( !leafData ) continue; + } + + /* Cell pCell is destined for new sibling page pNew. Originally, it + ** was either part of sibling page iOld (possibly an overflow cell), + ** or else the divider cell to the left of sibling page iOld. So, + ** if sibling page iOld had the same page number as pNew, and if + ** pCell really was a part of sibling page iOld (not a divider or + ** overflow cell), we can skip updating the pointer map entries. */ + if( iOld>=nNew + || pNew->pgno!=aPgno[iOld] + || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) + ){ + if( !leafCorrection ){ + ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); + } + if( cachedCellSize(&b,i)>pNew->minLocal ){ + ptrmapPutOvflPtr(pNew, pCell, &rc); + } + if( rc ) goto balance_cleanup; + } + } + } + + /* Insert new divider cells into pParent. */ + for(i=0; ileaf ){ + memcpy(&pNew->aData[8], pCell, 4); + }else if( leafData ){ + /* If the tree is a leaf-data tree, and the siblings are leaves, + ** then there is no divider cell in b.apCell[]. Instead, the divider + ** cell consists of the integer key for the right-most cell of + ** the sibling-page assembled above only. + */ + CellInfo info; + j--; + pNew->xParseCell(pNew, b.apCell[j], &info); + pCell = pTemp; + sz = 4 + putVarint(&pCell[4], info.nKey); + pTemp = 0; + }else{ + pCell -= 4; + /* Obscure case for non-leaf-data trees: If the cell at pCell was + ** previously stored on a leaf node, and its reported size was 4 + ** bytes, then it may actually be smaller than this + ** (see btreeParseCellPtr(), 4 bytes is the minimum size of + ** any cell). But it is important to pass the correct size to + ** insertCell(), so reparse the cell now. + ** + ** This can only happen for b-trees used to evaluate "IN (SELECT ...)" + ** and WITHOUT ROWID tables with exactly one column which is the + ** primary key. + */ + if( b.szCell[j]==4 ){ + assert(leafCorrection==4); + sz = pParent->xCellSize(pParent, pCell); + } + } + iOvflSpace += sz; + assert( sz<=pBt->maxLocal+23 ); + assert( iOvflSpace <= (int)pBt->pageSize ); + insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); + if( rc!=SQLITE_OK ) goto balance_cleanup; + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + } + + /* Now update the actual sibling pages. The order in which they are updated + ** is important, as this code needs to avoid disrupting any page from which + ** cells may still to be read. In practice, this means: + ** + ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) + ** then it is not safe to update page apNew[iPg] until after + ** the left-hand sibling apNew[iPg-1] has been updated. + ** + ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) + ** then it is not safe to update page apNew[iPg] until after + ** the right-hand sibling apNew[iPg+1] has been updated. + ** + ** If neither of the above apply, the page is safe to update. + ** + ** The iPg value in the following loop starts at nNew-1 goes down + ** to 0, then back up to nNew-1 again, thus making two passes over + ** the pages. On the initial downward pass, only condition (1) above + ** needs to be tested because (2) will always be true from the previous + ** step. On the upward pass, both conditions are always true, so the + ** upwards pass simply processes pages that were missed on the downward + ** pass. + */ + for(i=1-nNew; i=0 && iPg=0 /* On the upwards pass, or... */ + || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ + ){ + int iNew; + int iOld; + int nNewCell; + + /* Verify condition (1): If cells are moving left, update iPg + ** only after iPg-1 has already been updated. */ + assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); + + /* Verify condition (2): If cells are moving right, update iPg + ** only after iPg+1 has already been updated. */ + assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); + + if( iPg==0 ){ + iNew = iOld = 0; + nNewCell = cntNew[0]; + }else{ + iOld = iPgnFree = usableSpace-szNew[iPg]; + assert( apNew[iPg]->nOverflow==0 ); + assert( apNew[iPg]->nCell==nNewCell ); + } + } + + /* All pages have been processed exactly once */ + assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); + + assert( nOld>0 ); + assert( nNew>0 ); + + if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ + /* The root page of the b-tree now contains no cells. The only sibling + ** page is the right-child of the parent. Copy the contents of the + ** child page into the parent, decreasing the overall height of the + ** b-tree structure by one. This is described as the "balance-shallower" + ** sub-algorithm in some documentation. + ** + ** If this is an auto-vacuum database, the call to copyNodeContent() + ** sets all pointer-map entries corresponding to database image pages + ** for which the pointer is stored within the content being copied. + ** + ** It is critical that the child page be defragmented before being + ** copied into the parent, because if the parent is page 1 then it will + ** by smaller than the child due to the database header, and so all the + ** free space needs to be up front. + */ + assert( nNew==1 || CORRUPT_DB ); + rc = defragmentPage(apNew[0]); + testcase( rc!=SQLITE_OK ); + assert( apNew[0]->nFree == + (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) + || rc!=SQLITE_OK + ); + copyNodeContent(apNew[0], pParent, &rc); + freePage(apNew[0], &rc); + }else if( ISAUTOVACUUM && !leafCorrection ){ + /* Fix the pointer map entries associated with the right-child of each + ** sibling page. All other pointer map entries have already been taken + ** care of. */ + for(i=0; iaData[8]); + ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); + } + } + + assert( pParent->isInit ); + TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", + nOld, nNew, b.nCell)); + + /* Free any old pages that were not reused as new pages. + */ + for(i=nNew; iisInit ){ + /* The ptrmapCheckPages() contains assert() statements that verify that + ** all pointer map pages are set correctly. This is helpful while + ** debugging. This is usually disabled because a corrupt database may + ** cause an assert() statement to fail. */ + ptrmapCheckPages(apNew, nNew); + ptrmapCheckPages(&pParent, 1); + } +#endif + + /* + ** Cleanup before returning. + */ +balance_cleanup: + sqlite3ScratchFree(b.apCell); + for(i=0; ipBt; /* The BTree */ + + assert( pRoot->nOverflow>0 ); + assert( sqlite3_mutex_held(pBt->mutex) ); + + /* Make pRoot, the root page of the b-tree, writable. Allocate a new + ** page that will become the new right-child of pPage. Copy the contents + ** of the node stored on pRoot into the new child page. + */ + rc = sqlite3PagerWrite(pRoot->pDbPage); + if( rc==SQLITE_OK ){ + rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); + copyNodeContent(pRoot, pChild, &rc); + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); + } + } + if( rc ){ + *ppChild = 0; + releasePage(pChild); + return rc; + } + assert( sqlite3PagerIswriteable(pChild->pDbPage) ); + assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); + assert( pChild->nCell==pRoot->nCell ); + + TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); + + /* Copy the overflow cells from pRoot to pChild */ + memcpy(pChild->aiOvfl, pRoot->aiOvfl, + pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); + memcpy(pChild->apOvfl, pRoot->apOvfl, + pRoot->nOverflow*sizeof(pRoot->apOvfl[0])); + pChild->nOverflow = pRoot->nOverflow; + + /* Zero the contents of pRoot. Then install pChild as the right-child. */ + zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); + put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); + + *ppChild = pChild; + return SQLITE_OK; +} + +/* +** The page that pCur currently points to has just been modified in +** some way. This function figures out if this modification means the +** tree needs to be balanced, and if so calls the appropriate balancing +** routine. Balancing routines are: +** +** balance_quick() +** balance_deeper() +** balance_nonroot() +*/ +static int balance(BtCursor *pCur){ + int rc = SQLITE_OK; + const int nMin = pCur->pBt->usableSize * 2 / 3; + u8 aBalanceQuickSpace[13]; + u8 *pFree = 0; + + VVA_ONLY( int balance_quick_called = 0 ); + VVA_ONLY( int balance_deeper_called = 0 ); + + do { + int iPage = pCur->iPage; + MemPage *pPage = pCur->apPage[iPage]; + + if( iPage==0 ){ + if( pPage->nOverflow ){ + /* The root page of the b-tree is overfull. In this case call the + ** balance_deeper() function to create a new child for the root-page + ** and copy the current contents of the root-page to it. The + ** next iteration of the do-loop will balance the child page. + */ + assert( balance_deeper_called==0 ); + VVA_ONLY( balance_deeper_called++ ); + rc = balance_deeper(pPage, &pCur->apPage[1]); + if( rc==SQLITE_OK ){ + pCur->iPage = 1; + pCur->aiIdx[0] = 0; + pCur->aiIdx[1] = 0; + assert( pCur->apPage[1]->nOverflow ); + } + }else{ + break; + } + }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ + break; + }else{ + MemPage * const pParent = pCur->apPage[iPage-1]; + int const iIdx = pCur->aiIdx[iPage-1]; + + rc = sqlite3PagerWrite(pParent->pDbPage); + if( rc==SQLITE_OK ){ +#ifndef SQLITE_OMIT_QUICKBALANCE + if( pPage->intKeyLeaf + && pPage->nOverflow==1 + && pPage->aiOvfl[0]==pPage->nCell + && pParent->pgno!=1 + && pParent->nCell==iIdx + ){ + /* Call balance_quick() to create a new sibling of pPage on which + ** to store the overflow cell. balance_quick() inserts a new cell + ** into pParent, which may cause pParent overflow. If this + ** happens, the next iteration of the do-loop will balance pParent + ** use either balance_nonroot() or balance_deeper(). Until this + ** happens, the overflow cell is stored in the aBalanceQuickSpace[] + ** buffer. + ** + ** The purpose of the following assert() is to check that only a + ** single call to balance_quick() is made for each call to this + ** function. If this were not verified, a subtle bug involving reuse + ** of the aBalanceQuickSpace[] might sneak in. + */ + assert( balance_quick_called==0 ); + VVA_ONLY( balance_quick_called++ ); + rc = balance_quick(pParent, pPage, aBalanceQuickSpace); + }else +#endif + { + /* In this case, call balance_nonroot() to redistribute cells + ** between pPage and up to 2 of its sibling pages. This involves + ** modifying the contents of pParent, which may cause pParent to + ** become overfull or underfull. The next iteration of the do-loop + ** will balance the parent page to correct this. + ** + ** If the parent page becomes overfull, the overflow cell or cells + ** are stored in the pSpace buffer allocated immediately below. + ** A subsequent iteration of the do-loop will deal with this by + ** calling balance_nonroot() (balance_deeper() may be called first, + ** but it doesn't deal with overflow cells - just moves them to a + ** different page). Once this subsequent call to balance_nonroot() + ** has completed, it is safe to release the pSpace buffer used by + ** the previous call, as the overflow cell data will have been + ** copied either into the body of a database page or into the new + ** pSpace buffer passed to the latter call to balance_nonroot(). + */ + u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); + rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, + pCur->hints&BTREE_BULKLOAD); + if( pFree ){ + /* If pFree is not NULL, it points to the pSpace buffer used + ** by a previous call to balance_nonroot(). Its contents are + ** now stored either on real database pages or within the + ** new pSpace buffer, so it may be safely freed here. */ + sqlite3PageFree(pFree); + } + + /* The pSpace buffer will be freed after the next call to + ** balance_nonroot(), or just before this function returns, whichever + ** comes first. */ + pFree = pSpace; + } + } + + pPage->nOverflow = 0; + + /* The next iteration of the do-loop balances the parent page. */ + releasePage(pPage); + pCur->iPage--; + assert( pCur->iPage>=0 ); + } + }while( rc==SQLITE_OK ); + + if( pFree ){ + sqlite3PageFree(pFree); + } + return rc; +} + + +/* +** Insert a new record into the BTree. The key is given by (pKey,nKey) +** and the data is given by (pData,nData). The cursor is used only to +** define what table the record should be inserted into. The cursor +** is left pointing at a random location. +** +** For an INTKEY table, only the nKey value of the key is used. pKey is +** ignored. For a ZERODATA table, the pData and nData are both ignored. +** +** If the seekResult parameter is non-zero, then a successful call to +** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already +** been performed. seekResult is the search result returned (a negative +** number if pCur points at an entry that is smaller than (pKey, nKey), or +** a positive value if pCur points at an entry that is larger than +** (pKey, nKey)). +** +** If the seekResult parameter is non-zero, then the caller guarantees that +** cursor pCur is pointing at the existing copy of a row that is to be +** overwritten. If the seekResult parameter is 0, then cursor pCur may +** point to any entry or to no entry at all and so this function has to seek +** the cursor before the new key can be inserted. +*/ +SQLITE_PRIVATE int sqlite3BtreeInsert( + BtCursor *pCur, /* Insert data into the table of this cursor */ + const void *pKey, i64 nKey, /* The key of the new record */ + const void *pData, int nData, /* The data of the new record */ + int nZero, /* Number of extra 0 bytes to append to data */ + int appendBias, /* True if this is likely an append */ + int seekResult /* Result of prior MovetoUnpacked() call */ +){ + int rc; + int loc = seekResult; /* -1: before desired location +1: after */ + int szNew = 0; + int idx; + MemPage *pPage; + Btree *p = pCur->pBtree; + BtShared *pBt = p->pBt; + unsigned char *oldCell; + unsigned char *newCell = 0; + + if( pCur->eState==CURSOR_FAULT ){ + assert( pCur->skipNext!=SQLITE_OK ); + return pCur->skipNext; + } + + assert( cursorOwnsBtShared(pCur) ); + assert( (pCur->curFlags & BTCF_WriteFlag)!=0 + && pBt->inTransaction==TRANS_WRITE + && (pBt->btsFlags & BTS_READ_ONLY)==0 ); + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + + /* Assert that the caller has been consistent. If this cursor was opened + ** expecting an index b-tree, then the caller should be inserting blob + ** keys with no associated data. If the cursor was opened expecting an + ** intkey table, the caller should be inserting integer keys with a + ** blob of associated data. */ + assert( (pKey==0)==(pCur->pKeyInfo==0) ); + + /* Save the positions of any other cursors open on this table. + ** + ** In some cases, the call to btreeMoveto() below is a no-op. For + ** example, when inserting data into a table with auto-generated integer + ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the + ** integer key to use. It then calls this function to actually insert the + ** data into the intkey B-Tree. In this case btreeMoveto() recognizes + ** that the cursor is already where it needs to be and returns without + ** doing any work. To avoid thwarting these optimizations, it is important + ** not to clear the cursor here. + */ + if( pCur->curFlags & BTCF_Multiple ){ + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); + if( rc ) return rc; + } + + if( pCur->pKeyInfo==0 ){ + assert( pKey==0 ); + /* If this is an insert into a table b-tree, invalidate any incrblob + ** cursors open on the row being replaced */ + invalidateIncrblobCursors(p, nKey, 0); + + /* If the cursor is currently on the last row and we are appending a + ** new row onto the end, set the "loc" to avoid an unnecessary + ** btreeMoveto() call */ + if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 + && pCur->info.nKey==nKey-1 ){ + loc = -1; + }else if( loc==0 ){ + rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc); + if( rc ) return rc; + } + }else if( loc==0 ){ + rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); + if( rc ) return rc; + } + assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); + + pPage = pCur->apPage[pCur->iPage]; + assert( pPage->intKey || nKey>=0 ); + assert( pPage->leaf || !pPage->intKey ); + + TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", + pCur->pgnoRoot, nKey, nData, pPage->pgno, + loc==0 ? "overwrite" : "new entry")); + assert( pPage->isInit ); + newCell = pBt->pTmpSpace; + assert( newCell!=0 ); + rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); + if( rc ) goto end_insert; + assert( szNew==pPage->xCellSize(pPage, newCell) ); + assert( szNew <= MX_CELL_SIZE(pBt) ); + idx = pCur->aiIdx[pCur->iPage]; + if( loc==0 ){ + u16 szOld; + assert( idxnCell ); + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc ){ + goto end_insert; + } + oldCell = findCell(pPage, idx); + if( !pPage->leaf ){ + memcpy(newCell, oldCell, 4); + } + rc = clearCell(pPage, oldCell, &szOld); + dropCell(pPage, idx, szOld, &rc); + if( rc ) goto end_insert; + }else if( loc<0 && pPage->nCell>0 ){ + assert( pPage->leaf ); + idx = ++pCur->aiIdx[pCur->iPage]; + }else{ + assert( pPage->leaf ); + } + insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); + assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); + + /* If no error has occurred and pPage has an overflow cell, call balance() + ** to redistribute the cells within the tree. Since balance() may move + ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey + ** variables. + ** + ** Previous versions of SQLite called moveToRoot() to move the cursor + ** back to the root page as balance() used to invalidate the contents + ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, + ** set the cursor state to "invalid". This makes common insert operations + ** slightly faster. + ** + ** There is a subtle but important optimization here too. When inserting + ** multiple records into an intkey b-tree using a single cursor (as can + ** happen while processing an "INSERT INTO ... SELECT" statement), it + ** is advantageous to leave the cursor pointing to the last entry in + ** the b-tree if possible. If the cursor is left pointing to the last + ** entry in the table, and the next row inserted has an integer key + ** larger than the largest existing key, it is possible to insert the + ** row without seeking the cursor. This can be a big performance boost. + */ + pCur->info.nSize = 0; + if( rc==SQLITE_OK && pPage->nOverflow ){ + pCur->curFlags &= ~(BTCF_ValidNKey); + rc = balance(pCur); + + /* Must make sure nOverflow is reset to zero even if the balance() + ** fails. Internal data structure corruption will result otherwise. + ** Also, set the cursor state to invalid. This stops saveCursorPosition() + ** from trying to save the current position of the cursor. */ + pCur->apPage[pCur->iPage]->nOverflow = 0; + pCur->eState = CURSOR_INVALID; + } + assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); + +end_insert: + return rc; +} + +/* +** Delete the entry that the cursor is pointing to. +** +** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then +** the cursor is left pointing at an arbitrary location after the delete. +** But if that bit is set, then the cursor is left in a state such that +** the next call to BtreeNext() or BtreePrev() moves it to the same row +** as it would have been on if the call to BtreeDelete() had been omitted. +** +** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes +** associated with a single table entry and its indexes. Only one of those +** deletes is considered the "primary" delete. The primary delete occurs +** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete +** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag. +** The BTREE_AUXDELETE bit is a hint that is not used by this implementation, +** but which might be used by alternative storage engines. +*/ +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ + Btree *p = pCur->pBtree; + BtShared *pBt = p->pBt; + int rc; /* Return code */ + MemPage *pPage; /* Page to delete cell from */ + unsigned char *pCell; /* Pointer to cell to delete */ + int iCellIdx; /* Index of cell to delete */ + int iCellDepth; /* Depth of node containing pCell */ + u16 szCell; /* Size of the cell being deleted */ + int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */ + u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */ + + assert( cursorOwnsBtShared(pCur) ); + assert( pBt->inTransaction==TRANS_WRITE ); + assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); + assert( pCur->curFlags & BTCF_WriteFlag ); + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + assert( !hasReadConflicts(p, pCur->pgnoRoot) ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + assert( pCur->eState==CURSOR_VALID ); + assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); + + iCellDepth = pCur->iPage; + iCellIdx = pCur->aiIdx[iCellDepth]; + pPage = pCur->apPage[iCellDepth]; + pCell = findCell(pPage, iCellIdx); + + /* If the bPreserve flag is set to true, then the cursor position must + ** be preserved following this delete operation. If the current delete + ** will cause a b-tree rebalance, then this is done by saving the cursor + ** key and leaving the cursor in CURSOR_REQUIRESEEK state before + ** returning. + ** + ** Or, if the current delete will not cause a rebalance, then the cursor + ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately + ** before or after the deleted entry. In this case set bSkipnext to true. */ + if( bPreserve ){ + if( !pPage->leaf + || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) + ){ + /* A b-tree rebalance will be required after deleting this entry. + ** Save the cursor key. */ + rc = saveCursorKey(pCur); + if( rc ) return rc; + }else{ + bSkipnext = 1; + } + } + + /* If the page containing the entry to delete is not a leaf page, move + ** the cursor to the largest entry in the tree that is smaller than + ** the entry being deleted. This cell will replace the cell being deleted + ** from the internal node. The 'previous' entry is used for this instead + ** of the 'next' entry, as the previous entry is always a part of the + ** sub-tree headed by the child page of the cell being deleted. This makes + ** balancing the tree following the delete operation easier. */ + if( !pPage->leaf ){ + int notUsed = 0; + rc = sqlite3BtreePrevious(pCur, ¬Used); + if( rc ) return rc; + } + + /* Save the positions of any other cursors open on this table before + ** making any modifications. */ + if( pCur->curFlags & BTCF_Multiple ){ + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); + if( rc ) return rc; + } + + /* If this is a delete operation to remove a row from a table b-tree, + ** invalidate any incrblob cursors open on the row being deleted. */ + if( pCur->pKeyInfo==0 ){ + invalidateIncrblobCursors(p, pCur->info.nKey, 0); + } + + /* Make the page containing the entry to be deleted writable. Then free any + ** overflow pages associated with the entry and finally remove the cell + ** itself from within the page. */ + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc ) return rc; + rc = clearCell(pPage, pCell, &szCell); + dropCell(pPage, iCellIdx, szCell, &rc); + if( rc ) return rc; + + /* If the cell deleted was not located on a leaf page, then the cursor + ** is currently pointing to the largest entry in the sub-tree headed + ** by the child-page of the cell that was just deleted from an internal + ** node. The cell from the leaf node needs to be moved to the internal + ** node to replace the deleted cell. */ + if( !pPage->leaf ){ + MemPage *pLeaf = pCur->apPage[pCur->iPage]; + int nCell; + Pgno n = pCur->apPage[iCellDepth+1]->pgno; + unsigned char *pTmp; + + pCell = findCell(pLeaf, pLeaf->nCell-1); + if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; + nCell = pLeaf->xCellSize(pLeaf, pCell); + assert( MX_CELL_SIZE(pBt) >= nCell ); + pTmp = pBt->pTmpSpace; + assert( pTmp!=0 ); + rc = sqlite3PagerWrite(pLeaf->pDbPage); + insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); + dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); + if( rc ) return rc; + } + + /* Balance the tree. If the entry deleted was located on a leaf page, + ** then the cursor still points to that page. In this case the first + ** call to balance() repairs the tree, and the if(...) condition is + ** never true. + ** + ** Otherwise, if the entry deleted was on an internal node page, then + ** pCur is pointing to the leaf page from which a cell was removed to + ** replace the cell deleted from the internal node. This is slightly + ** tricky as the leaf node may be underfull, and the internal node may + ** be either under or overfull. In this case run the balancing algorithm + ** on the leaf node first. If the balance proceeds far enough up the + ** tree that we can be sure that any problem in the internal node has + ** been corrected, so be it. Otherwise, after balancing the leaf node, + ** walk the cursor up the tree to the internal node and balance it as + ** well. */ + rc = balance(pCur); + if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ + while( pCur->iPage>iCellDepth ){ + releasePage(pCur->apPage[pCur->iPage--]); + } + rc = balance(pCur); + } + + if( rc==SQLITE_OK ){ + if( bSkipnext ){ + assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) ); + assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB ); + assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell ); + pCur->eState = CURSOR_SKIPNEXT; + if( iCellIdx>=pPage->nCell ){ + pCur->skipNext = -1; + pCur->aiIdx[iCellDepth] = pPage->nCell-1; + }else{ + pCur->skipNext = 1; + } + }else{ + rc = moveToRoot(pCur); + if( bPreserve ){ + pCur->eState = CURSOR_REQUIRESEEK; + } + } + } + return rc; +} + +/* +** Create a new BTree table. Write into *piTable the page +** number for the root page of the new table. +** +** The type of type is determined by the flags parameter. Only the +** following values of flags are currently in use. Other values for +** flags might not work: +** +** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys +** BTREE_ZERODATA Used for SQL indices +*/ +static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ + BtShared *pBt = p->pBt; + MemPage *pRoot; + Pgno pgnoRoot; + int rc; + int ptfFlags; /* Page-type flage for the root page of new table */ + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( pBt->inTransaction==TRANS_WRITE ); + assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); + +#ifdef SQLITE_OMIT_AUTOVACUUM + rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); + if( rc ){ + return rc; + } +#else + if( pBt->autoVacuum ){ + Pgno pgnoMove; /* Move a page here to make room for the root-page */ + MemPage *pPageMove; /* The page to move to. */ + + /* Creating a new table may probably require moving an existing database + ** to make room for the new tables root page. In case this page turns + ** out to be an overflow page, delete all overflow page-map caches + ** held by open cursors. + */ + invalidateAllOverflowCache(pBt); + + /* Read the value of meta[3] from the database to determine where the + ** root page of the new table should go. meta[3] is the largest root-page + ** created so far, so the new root-page is (meta[3]+1). + */ + sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); + pgnoRoot++; + + /* The new root-page may not be allocated on a pointer-map page, or the + ** PENDING_BYTE page. + */ + while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || + pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ + pgnoRoot++; + } + assert( pgnoRoot>=3 || CORRUPT_DB ); + testcase( pgnoRoot<3 ); + + /* Allocate a page. The page that currently resides at pgnoRoot will + ** be moved to the allocated page (unless the allocated page happens + ** to reside at pgnoRoot). + */ + rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); + if( rc!=SQLITE_OK ){ + return rc; + } + + if( pgnoMove!=pgnoRoot ){ + /* pgnoRoot is the page that will be used for the root-page of + ** the new table (assuming an error did not occur). But we were + ** allocated pgnoMove. If required (i.e. if it was not allocated + ** by extending the file), the current page at position pgnoMove + ** is already journaled. + */ + u8 eType = 0; + Pgno iPtrPage = 0; + + /* Save the positions of any open cursors. This is required in + ** case they are holding a reference to an xFetch reference + ** corresponding to page pgnoRoot. */ + rc = saveAllCursors(pBt, 0, 0); + releasePage(pPageMove); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Move the page currently at pgnoRoot to pgnoMove. */ + rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); + if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ + rc = SQLITE_CORRUPT_BKPT; + } + if( rc!=SQLITE_OK ){ + releasePage(pRoot); + return rc; + } + assert( eType!=PTRMAP_ROOTPAGE ); + assert( eType!=PTRMAP_FREEPAGE ); + rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); + releasePage(pRoot); + + /* Obtain the page at pgnoRoot */ + if( rc!=SQLITE_OK ){ + return rc; + } + rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = sqlite3PagerWrite(pRoot->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pRoot); + return rc; + } + }else{ + pRoot = pPageMove; + } + + /* Update the pointer-map and meta-data with the new root-page number. */ + ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); + if( rc ){ + releasePage(pRoot); + return rc; + } + + /* When the new root page was allocated, page 1 was made writable in + ** order either to increase the database filesize, or to decrement the + ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. + */ + assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); + rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); + if( NEVER(rc) ){ + releasePage(pRoot); + return rc; + } + + }else{ + rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); + if( rc ) return rc; + } +#endif + assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); + if( createTabFlags & BTREE_INTKEY ){ + ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; + }else{ + ptfFlags = PTF_ZERODATA | PTF_LEAF; + } + zeroPage(pRoot, ptfFlags); + sqlite3PagerUnref(pRoot->pDbPage); + assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); + *piTable = (int)pgnoRoot; + return SQLITE_OK; +} +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeCreateTable(p, piTable, flags); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Erase the given database page and all its children. Return +** the page to the freelist. +*/ +static int clearDatabasePage( + BtShared *pBt, /* The BTree that contains the table */ + Pgno pgno, /* Page number to clear */ + int freePageFlag, /* Deallocate page if true */ + int *pnChange /* Add number of Cells freed to this counter */ +){ + MemPage *pPage; + int rc; + unsigned char *pCell; + int i; + int hdr; + u16 szCell; + + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pgno>btreePagecount(pBt) ){ + return SQLITE_CORRUPT_BKPT; + } + rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); + if( rc ) return rc; + if( pPage->bBusy ){ + rc = SQLITE_CORRUPT_BKPT; + goto cleardatabasepage_out; + } + pPage->bBusy = 1; + hdr = pPage->hdrOffset; + for(i=0; inCell; i++){ + pCell = findCell(pPage, i); + if( !pPage->leaf ){ + rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); + if( rc ) goto cleardatabasepage_out; + } + rc = clearCell(pPage, pCell, &szCell); + if( rc ) goto cleardatabasepage_out; + } + if( !pPage->leaf ){ + rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); + if( rc ) goto cleardatabasepage_out; + }else if( pnChange ){ + assert( pPage->intKey || CORRUPT_DB ); + testcase( !pPage->intKey ); + *pnChange += pPage->nCell; + } + if( freePageFlag ){ + freePage(pPage, &rc); + }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ + zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF); + } + +cleardatabasepage_out: + pPage->bBusy = 0; + releasePage(pPage); + return rc; +} + +/* +** Delete all information from a single table in the database. iTable is +** the page number of the root of the table. After this routine returns, +** the root page is empty, but still exists. +** +** This routine will fail with SQLITE_LOCKED if there are any open +** read cursors on the table. Open write cursors are moved to the +** root of the table. +** +** If pnChange is not NULL, then table iTable must be an intkey table. The +** integer value pointed to by pnChange is incremented by the number of +** entries in the table. +*/ +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ + int rc; + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + + rc = saveAllCursors(pBt, (Pgno)iTable, 0); + + if( SQLITE_OK==rc ){ + /* Invalidate all incrblob cursors open on table iTable (assuming iTable + ** is the root of a table b-tree - if it is not, the following call is + ** a no-op). */ + invalidateIncrblobCursors(p, 0, 1); + rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); + } + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Delete all information from the single table that pCur is open on. +** +** This routine only work for pCur on an ephemeral table. +*/ +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ + return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0); +} + +/* +** Erase all information in a table and add the root of the table to +** the freelist. Except, the root of the principle table (the one on +** page 1) is never added to the freelist. +** +** This routine will fail with SQLITE_LOCKED if there are any open +** cursors on the table. +** +** If AUTOVACUUM is enabled and the page at iTable is not the last +** root page in the database file, then the last root page +** in the database file is moved into the slot formerly occupied by +** iTable and that last slot formerly occupied by the last root page +** is added to the freelist instead of iTable. In this say, all +** root pages are kept at the beginning of the database file, which +** is necessary for AUTOVACUUM to work right. *piMoved is set to the +** page number that used to be the last root page in the file before +** the move. If no page gets moved, *piMoved is set to 0. +** The last root page is recorded in meta[3] and the value of +** meta[3] is updated by this procedure. +*/ +static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ + int rc; + MemPage *pPage = 0; + BtShared *pBt = p->pBt; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( p->inTrans==TRANS_WRITE ); + + /* It is illegal to drop a table if any cursors are open on the + ** database. This is because in auto-vacuum mode the backend may + ** need to move another root-page to fill a gap left by the deleted + ** root page. If an open cursor was using this page a problem would + ** occur. + ** + ** This error is caught long before control reaches this point. + */ + if( NEVER(pBt->pCursor) ){ + sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); + return SQLITE_LOCKED_SHAREDCACHE; + } + + /* + ** It is illegal to drop the sqlite_master table on page 1. But again, + ** this error is caught long before reaching this point. + */ + if( NEVER(iTable<2) ){ + return SQLITE_CORRUPT_BKPT; + } + + rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); + if( rc ) return rc; + rc = sqlite3BtreeClearTable(p, iTable, 0); + if( rc ){ + releasePage(pPage); + return rc; + } + + *piMoved = 0; + +#ifdef SQLITE_OMIT_AUTOVACUUM + freePage(pPage, &rc); + releasePage(pPage); +#else + if( pBt->autoVacuum ){ + Pgno maxRootPgno; + sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); + + if( iTable==maxRootPgno ){ + /* If the table being dropped is the table with the largest root-page + ** number in the database, put the root page on the free list. + */ + freePage(pPage, &rc); + releasePage(pPage); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + /* The table being dropped does not have the largest root-page + ** number in the database. So move the page that does into the + ** gap left by the deleted root-page. + */ + MemPage *pMove; + releasePage(pPage); + rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); + releasePage(pMove); + if( rc!=SQLITE_OK ){ + return rc; + } + pMove = 0; + rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); + freePage(pMove, &rc); + releasePage(pMove); + if( rc!=SQLITE_OK ){ + return rc; + } + *piMoved = maxRootPgno; + } + + /* Set the new 'max-root-page' value in the database header. This + ** is the old value less one, less one more if that happens to + ** be a root-page number, less one again if that is the + ** PENDING_BYTE_PAGE. + */ + maxRootPgno--; + while( maxRootPgno==PENDING_BYTE_PAGE(pBt) + || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ + maxRootPgno--; + } + assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); + + rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); + }else{ + freePage(pPage, &rc); + releasePage(pPage); + } +#endif + return rc; +} +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeDropTable(p, iTable, piMoved); + sqlite3BtreeLeave(p); + return rc; +} + + +/* +** This function may only be called if the b-tree connection already +** has a read or write transaction open on the database. +** +** Read the meta-information out of a database file. Meta[0] +** is the number of free pages currently in the database. Meta[1] +** through meta[15] are available for use by higher layers. Meta[0] +** is read-only, the others are read/write. +** +** The schema layer numbers meta values differently. At the schema +** layer (and the SetCookie and ReadCookie opcodes) the number of +** free pages is not visible. So Cookie[0] is the same as Meta[1]. +** +** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead +** of reading the value out of the header, it instead loads the "DataVersion" +** from the pager. The BTREE_DATA_VERSION value is not actually stored in the +** database file. It is a number computed by the pager. But its access +** pattern is the same as header meta values, and so it is convenient to +** read it from this routine. +*/ +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ + BtShared *pBt = p->pBt; + + sqlite3BtreeEnter(p); + assert( p->inTrans>TRANS_NONE ); + assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); + assert( pBt->pPage1 ); + assert( idx>=0 && idx<=15 ); + + if( idx==BTREE_DATA_VERSION ){ + *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion; + }else{ + *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); + } + + /* If auto-vacuum is disabled in this build and this is an auto-vacuum + ** database, mark the database as read-only. */ +#ifdef SQLITE_OMIT_AUTOVACUUM + if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){ + pBt->btsFlags |= BTS_READ_ONLY; + } +#endif + + sqlite3BtreeLeave(p); +} + +/* +** Write meta-information back into the database. Meta[0] is +** read-only and may not be written. +*/ +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ + BtShared *pBt = p->pBt; + unsigned char *pP1; + int rc; + assert( idx>=1 && idx<=15 ); + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + assert( pBt->pPage1!=0 ); + pP1 = pBt->pPage1->aData; + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pP1[36 + idx*4], iMeta); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( idx==BTREE_INCR_VACUUM ){ + assert( pBt->autoVacuum || iMeta==0 ); + assert( iMeta==0 || iMeta==1 ); + pBt->incrVacuum = (u8)iMeta; + } +#endif + } + sqlite3BtreeLeave(p); + return rc; +} + +#ifndef SQLITE_OMIT_BTREECOUNT +/* +** The first argument, pCur, is a cursor opened on some b-tree. Count the +** number of entries in the b-tree and write the result to *pnEntry. +** +** SQLITE_OK is returned if the operation is successfully executed. +** Otherwise, if an error is encountered (i.e. an IO error or database +** corruption) an SQLite error code is returned. +*/ +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ + i64 nEntry = 0; /* Value to return in *pnEntry */ + int rc; /* Return code */ + + if( pCur->pgnoRoot==0 ){ + *pnEntry = 0; + return SQLITE_OK; + } + rc = moveToRoot(pCur); + + /* Unless an error occurs, the following loop runs one iteration for each + ** page in the B-Tree structure (not including overflow pages). + */ + while( rc==SQLITE_OK ){ + int iIdx; /* Index of child node in parent */ + MemPage *pPage; /* Current page of the b-tree */ + + /* If this is a leaf page or the tree is not an int-key tree, then + ** this page contains countable entries. Increment the entry counter + ** accordingly. + */ + pPage = pCur->apPage[pCur->iPage]; + if( pPage->leaf || !pPage->intKey ){ + nEntry += pPage->nCell; + } + + /* pPage is a leaf node. This loop navigates the cursor so that it + ** points to the first interior cell that it points to the parent of + ** the next page in the tree that has not yet been visited. The + ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell + ** of the page, or to the number of cells in the page if the next page + ** to visit is the right-child of its parent. + ** + ** If all pages in the tree have been visited, return SQLITE_OK to the + ** caller. + */ + if( pPage->leaf ){ + do { + if( pCur->iPage==0 ){ + /* All pages of the b-tree have been visited. Return successfully. */ + *pnEntry = nEntry; + return moveToRoot(pCur); + } + moveToParent(pCur); + }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); + + pCur->aiIdx[pCur->iPage]++; + pPage = pCur->apPage[pCur->iPage]; + } + + /* Descend to the child node of the cell that the cursor currently + ** points at. This is the right-child if (iIdx==pPage->nCell). + */ + iIdx = pCur->aiIdx[pCur->iPage]; + if( iIdx==pPage->nCell ){ + rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); + }else{ + rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); + } + } + + /* An error has occurred. Return an error code. */ + return rc; +} +#endif + +/* +** Return the pager associated with a BTree. This routine is used for +** testing and debugging only. +*/ +SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){ + return p->pBt->pPager; +} + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** Append a message to the error message string. +*/ +static void checkAppendMsg( + IntegrityCk *pCheck, + const char *zFormat, + ... +){ + va_list ap; + if( !pCheck->mxErr ) return; + pCheck->mxErr--; + pCheck->nErr++; + va_start(ap, zFormat); + if( pCheck->errMsg.nChar ){ + sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); + } + if( pCheck->zPfx ){ + sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2); + } + sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap); + va_end(ap); + if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ + pCheck->mallocFailed = 1; + } +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK + +/* +** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that +** corresponds to page iPg is already set. +*/ +static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ + assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); + return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); +} + +/* +** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. +*/ +static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ + assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); + pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); +} + + +/* +** Add 1 to the reference count for page iPage. If this is the second +** reference to the page, add an error message to pCheck->zErrMsg. +** Return 1 if there are 2 or more references to the page and 0 if +** if this is the first reference to the page. +** +** Also check that the page number is in bounds. +*/ +static int checkRef(IntegrityCk *pCheck, Pgno iPage){ + if( iPage==0 ) return 1; + if( iPage>pCheck->nPage ){ + checkAppendMsg(pCheck, "invalid page number %d", iPage); + return 1; + } + if( getPageReferenced(pCheck, iPage) ){ + checkAppendMsg(pCheck, "2nd reference to page %d", iPage); + return 1; + } + setPageReferenced(pCheck, iPage); + return 0; +} + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Check that the entry in the pointer-map for page iChild maps to +** page iParent, pointer type ptrType. If not, append an error message +** to pCheck. +*/ +static void checkPtrmap( + IntegrityCk *pCheck, /* Integrity check context */ + Pgno iChild, /* Child page number */ + u8 eType, /* Expected pointer map type */ + Pgno iParent /* Expected pointer map parent page number */ +){ + int rc; + u8 ePtrmapType; + Pgno iPtrmapParent; + + rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; + checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); + return; + } + + if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ + checkAppendMsg(pCheck, + "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", + iChild, eType, iParent, ePtrmapType, iPtrmapParent); + } +} +#endif + +/* +** Check the integrity of the freelist or of an overflow page list. +** Verify that the number of pages on the list is N. +*/ +static void checkList( + IntegrityCk *pCheck, /* Integrity checking context */ + int isFreeList, /* True for a freelist. False for overflow page list */ + int iPage, /* Page number for first page in the list */ + int N /* Expected number of pages in the list */ +){ + int i; + int expected = N; + int iFirst = iPage; + while( N-- > 0 && pCheck->mxErr ){ + DbPage *pOvflPage; + unsigned char *pOvflData; + if( iPage<1 ){ + checkAppendMsg(pCheck, + "%d of %d pages missing from overflow list starting at %d", + N+1, expected, iFirst); + break; + } + if( checkRef(pCheck, iPage) ) break; + if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ + checkAppendMsg(pCheck, "failed to get page %d", iPage); + break; + } + pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); + if( isFreeList ){ + int n = get4byte(&pOvflData[4]); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pCheck->pBt->autoVacuum ){ + checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); + } +#endif + if( n>(int)pCheck->pBt->usableSize/4-2 ){ + checkAppendMsg(pCheck, + "freelist leaf count too big on page %d", iPage); + N--; + }else{ + for(i=0; ipBt->autoVacuum ){ + checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); + } +#endif + checkRef(pCheck, iFreePage); + } + N -= n; + } + } +#ifndef SQLITE_OMIT_AUTOVACUUM + else{ + /* If this database supports auto-vacuum and iPage is not the last + ** page in this overflow list, check that the pointer-map entry for + ** the following page matches iPage. + */ + if( pCheck->pBt->autoVacuum && N>0 ){ + i = get4byte(pOvflData); + checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); + } + } +#endif + iPage = get4byte(pOvflData); + sqlite3PagerUnref(pOvflPage); + + if( isFreeList && N<(iPage!=0) ){ + checkAppendMsg(pCheck, "free-page count in header is too small"); + } + } +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +/* +** An implementation of a min-heap. +** +** aHeap[0] is the number of elements on the heap. aHeap[1] is the +** root element. The daughter nodes of aHeap[N] are aHeap[N*2] +** and aHeap[N*2+1]. +** +** The heap property is this: Every node is less than or equal to both +** of its daughter nodes. A consequence of the heap property is that the +** root node aHeap[1] is always the minimum value currently in the heap. +** +** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto +** the heap, preserving the heap property. The btreeHeapPull() routine +** removes the root element from the heap (the minimum value in the heap) +** and then moves other nodes around as necessary to preserve the heap +** property. +** +** This heap is used for cell overlap and coverage testing. Each u32 +** entry represents the span of a cell or freeblock on a btree page. +** The upper 16 bits are the index of the first byte of a range and the +** lower 16 bits are the index of the last byte of that range. +*/ +static void btreeHeapInsert(u32 *aHeap, u32 x){ + u32 j, i = ++aHeap[0]; + aHeap[i] = x; + while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ + x = aHeap[j]; + aHeap[j] = aHeap[i]; + aHeap[i] = x; + i = j; + } +} +static int btreeHeapPull(u32 *aHeap, u32 *pOut){ + u32 j, i, x; + if( (x = aHeap[0])==0 ) return 0; + *pOut = aHeap[1]; + aHeap[1] = aHeap[x]; + aHeap[x] = 0xffffffff; + aHeap[0]--; + i = 1; + while( (j = i*2)<=aHeap[0] ){ + if( aHeap[j]>aHeap[j+1] ) j++; + if( aHeap[i]zPfx; + int saved_v1 = pCheck->v1; + int saved_v2 = pCheck->v2; + u8 savedIsInit = 0; + + /* Check that the page exists + */ + pBt = pCheck->pBt; + usableSize = pBt->usableSize; + if( iPage==0 ) return 0; + if( checkRef(pCheck, iPage) ) return 0; + pCheck->zPfx = "Page %d: "; + pCheck->v1 = iPage; + if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ + checkAppendMsg(pCheck, + "unable to get the page. error code=%d", rc); + goto end_of_check; + } + + /* Clear MemPage.isInit to make sure the corruption detection code in + ** btreeInitPage() is executed. */ + savedIsInit = pPage->isInit; + pPage->isInit = 0; + if( (rc = btreeInitPage(pPage))!=0 ){ + assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ + checkAppendMsg(pCheck, + "btreeInitPage() returns error code %d", rc); + goto end_of_check; + } + data = pPage->aData; + hdr = pPage->hdrOffset; + + /* Set up for cell analysis */ + pCheck->zPfx = "On tree page %d cell %d: "; + contentOffset = get2byteNotZero(&data[hdr+5]); + assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ + + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the + ** number of cells on the page. */ + nCell = get2byte(&data[hdr+3]); + assert( pPage->nCell==nCell ); + + /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page + ** immediately follows the b-tree page header. */ + cellStart = hdr + 12 - 4*pPage->leaf; + assert( pPage->aCellIdx==&data[cellStart] ); + pCellIdx = &data[cellStart + 2*(nCell-1)]; + + if( !pPage->leaf ){ + /* Analyze the right-child page of internal pages */ + pgno = get4byte(&data[hdr+8]); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + pCheck->zPfx = "On page %d at right child: "; + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); + } +#endif + depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); + keyCanBeEqual = 0; + }else{ + /* For leaf pages, the coverage check will occur in the same loop + ** as the other cell checks, so initialize the heap. */ + heap = pCheck->heap; + heap[0] = 0; + } + + /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte + ** integer offsets to the cell contents. */ + for(i=nCell-1; i>=0 && pCheck->mxErr; i--){ + CellInfo info; + + /* Check cell size */ + pCheck->v2 = i; + assert( pCellIdx==&data[cellStart + i*2] ); + pc = get2byteAligned(pCellIdx); + pCellIdx -= 2; + if( pcusableSize-4 ){ + checkAppendMsg(pCheck, "Offset %d out of range %d..%d", + pc, contentOffset, usableSize-4); + doCoverageCheck = 0; + continue; + } + pCell = &data[pc]; + pPage->xParseCell(pPage, pCell, &info); + if( pc+info.nSize>usableSize ){ + checkAppendMsg(pCheck, "Extends off end of page"); + doCoverageCheck = 0; + continue; + } + + /* Check for integer primary key out of range */ + if( pPage->intKey ){ + if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ + checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); + } + maxKey = info.nKey; + } + + /* Check the content overflow list */ + if( info.nPayload>info.nLocal ){ + int nPage; /* Number of pages on the overflow chain */ + Pgno pgnoOvfl; /* First page of the overflow chain */ + assert( pc + info.nSize - 4 <= usableSize ); + nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); + pgnoOvfl = get4byte(&pCell[info.nSize - 4]); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); + } +#endif + checkList(pCheck, 0, pgnoOvfl, nPage); + } + + if( !pPage->leaf ){ + /* Check sanity of left child page for internal pages */ + pgno = get4byte(pCell); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); + } +#endif + d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey); + keyCanBeEqual = 0; + if( d2!=depth ){ + checkAppendMsg(pCheck, "Child page depth differs"); + depth = d2; + } + }else{ + /* Populate the coverage-checking heap for leaf pages */ + btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1)); + } + } + *piMinKey = maxKey; + + /* Check for complete coverage of the page + */ + pCheck->zPfx = 0; + if( doCoverageCheck && pCheck->mxErr>0 ){ + /* For leaf pages, the min-heap has already been initialized and the + ** cells have already been inserted. But for internal pages, that has + ** not yet been done, so do it now */ + if( !pPage->leaf ){ + heap = pCheck->heap; + heap[0] = 0; + for(i=nCell-1; i>=0; i--){ + u32 size; + pc = get2byteAligned(&data[cellStart+i*2]); + size = pPage->xCellSize(pPage, &data[pc]); + btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); + } + } + /* Add the freeblocks to the min-heap + ** + ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header + ** is the offset of the first freeblock, or zero if there are no + ** freeblocks on the page. + */ + i = get2byte(&data[hdr+1]); + while( i>0 ){ + int size, j; + assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */ + size = get2byte(&data[i+2]); + assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */ + btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1)); + /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a + ** big-endian integer which is the offset in the b-tree page of the next + ** freeblock in the chain, or zero if the freeblock is the last on the + ** chain. */ + j = get2byte(&data[i]); + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of + ** increasing offset. */ + assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ + assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */ + i = j; + } + /* Analyze the min-heap looking for overlap between cells and/or + ** freeblocks, and counting the number of untracked bytes in nFrag. + ** + ** Each min-heap entry is of the form: (start_address<<16)|end_address. + ** There is an implied first entry the covers the page header, the cell + ** pointer index, and the gap between the cell pointer index and the start + ** of cell content. + ** + ** The loop below pulls entries from the min-heap in order and compares + ** the start_address against the previous end_address. If there is an + ** overlap, that means bytes are used multiple times. If there is a gap, + ** that gap is added to the fragmentation count. + */ + nFrag = 0; + prev = contentOffset - 1; /* Implied first min-heap entry */ + while( btreeHeapPull(heap,&x) ){ + if( (prev&0xffff)>=(x>>16) ){ + checkAppendMsg(pCheck, + "Multiple uses for byte %u of page %d", x>>16, iPage); + break; + }else{ + nFrag += (x>>16) - (prev&0xffff) - 1; + prev = x; + } + } + nFrag += usableSize - (prev&0xffff) - 1; + /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments + ** is stored in the fifth field of the b-tree page header. + ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the + ** number of fragmented free bytes within the cell content area. + */ + if( heap[0]==0 && nFrag!=data[hdr+7] ){ + checkAppendMsg(pCheck, + "Fragmentation of %d bytes reported as %d on page %d", + nFrag, data[hdr+7], iPage); + } + } + +end_of_check: + if( !doCoverageCheck ) pPage->isInit = savedIsInit; + releasePage(pPage); + pCheck->zPfx = saved_zPfx; + pCheck->v1 = saved_v1; + pCheck->v2 = saved_v2; + return depth+1; +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** This routine does a complete check of the given BTree file. aRoot[] is +** an array of pages numbers were each page number is the root page of +** a table. nRoot is the number of entries in aRoot. +** +** A read-only or read-write transaction must be opened before calling +** this function. +** +** Write the number of error seen in *pnErr. Except for some memory +** allocation errors, an error message held in memory obtained from +** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is +** returned. If a memory allocation error occurs, NULL is returned. +*/ +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck( + Btree *p, /* The btree to be checked */ + int *aRoot, /* An array of root pages numbers for individual trees */ + int nRoot, /* Number of entries in aRoot[] */ + int mxErr, /* Stop reporting errors after this many */ + int *pnErr /* Write number of errors seen to this variable */ +){ + Pgno i; + IntegrityCk sCheck; + BtShared *pBt = p->pBt; + int savedDbFlags = pBt->db->flags; + char zErr[100]; + VVA_ONLY( int nRef ); + + sqlite3BtreeEnter(p); + assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); + VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) ); + assert( nRef>=0 ); + sCheck.pBt = pBt; + sCheck.pPager = pBt->pPager; + sCheck.nPage = btreePagecount(sCheck.pBt); + sCheck.mxErr = mxErr; + sCheck.nErr = 0; + sCheck.mallocFailed = 0; + sCheck.zPfx = 0; + sCheck.v1 = 0; + sCheck.v2 = 0; + sCheck.aPgRef = 0; + sCheck.heap = 0; + sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); + sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL; + if( sCheck.nPage==0 ){ + goto integrity_ck_cleanup; + } + + sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); + if( !sCheck.aPgRef ){ + sCheck.mallocFailed = 1; + goto integrity_ck_cleanup; + } + sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); + if( sCheck.heap==0 ){ + sCheck.mallocFailed = 1; + goto integrity_ck_cleanup; + } + + i = PENDING_BYTE_PAGE(pBt); + if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); + + /* Check the integrity of the freelist + */ + sCheck.zPfx = "Main freelist: "; + checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), + get4byte(&pBt->pPage1->aData[36])); + sCheck.zPfx = 0; + + /* Check all the tables. + */ + testcase( pBt->db->flags & SQLITE_CellSizeCk ); + pBt->db->flags &= ~SQLITE_CellSizeCk; + for(i=0; (int)iautoVacuum && aRoot[i]>1 ){ + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); + } +#endif + checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); + } + pBt->db->flags = savedDbFlags; + + /* Make sure every page in the file is referenced + */ + for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ +#ifdef SQLITE_OMIT_AUTOVACUUM + if( getPageReferenced(&sCheck, i)==0 ){ + checkAppendMsg(&sCheck, "Page %d is never used", i); + } +#else + /* If the database supports auto-vacuum, make sure no tables contain + ** references to pointer-map pages. + */ + if( getPageReferenced(&sCheck, i)==0 && + (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ + checkAppendMsg(&sCheck, "Page %d is never used", i); + } + if( getPageReferenced(&sCheck, i)!=0 && + (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ + checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); + } +#endif + } + + /* Clean up and report errors. + */ +integrity_ck_cleanup: + sqlite3PageFree(sCheck.heap); + sqlite3_free(sCheck.aPgRef); + if( sCheck.mallocFailed ){ + sqlite3StrAccumReset(&sCheck.errMsg); + sCheck.nErr++; + } + *pnErr = sCheck.nErr; + if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); + /* Make sure this analysis did not leave any unref() pages. */ + assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); + sqlite3BtreeLeave(p); + return sqlite3StrAccumFinish(&sCheck.errMsg); +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +/* +** Return the full pathname of the underlying database file. Return +** an empty string if the database is in-memory or a TEMP database. +** +** The pager filename is invariant as long as the pager is +** open so it is safe to access without the BtShared mutex. +*/ +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){ + assert( p->pBt->pPager!=0 ); + return sqlite3PagerFilename(p->pBt->pPager, 1); +} + +/* +** Return the pathname of the journal file for this database. The return +** value of this routine is the same regardless of whether the journal file +** has been created or not. +** +** The pager journal filename is invariant as long as the pager is +** open so it is safe to access without the BtShared mutex. +*/ +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){ + assert( p->pBt->pPager!=0 ); + return sqlite3PagerJournalname(p->pBt->pPager); +} + +/* +** Return non-zero if a transaction is active. +*/ +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){ + assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); + return (p && (p->inTrans==TRANS_WRITE)); +} + +#ifndef SQLITE_OMIT_WAL +/* +** Run a checkpoint on the Btree passed as the first argument. +** +** Return SQLITE_LOCKED if this or any other connection has an open +** transaction on the shared-cache the argument Btree is connected to. +** +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +*/ +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ + int rc = SQLITE_OK; + if( p ){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + if( pBt->inTransaction!=TRANS_NONE ){ + rc = SQLITE_LOCKED; + }else{ + rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt); + } + sqlite3BtreeLeave(p); + } + return rc; +} +#endif + +/* +** Return non-zero if a read (or write) transaction is active. +*/ +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){ + assert( p ); + assert( sqlite3_mutex_held(p->db->mutex) ); + return p->inTrans!=TRANS_NONE; +} + +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){ + assert( p ); + assert( sqlite3_mutex_held(p->db->mutex) ); + return p->nBackup!=0; +} + +/* +** This function returns a pointer to a blob of memory associated with +** a single shared-btree. The memory is used by client code for its own +** purposes (for example, to store a high-level schema associated with +** the shared-btree). The btree layer manages reference counting issues. +** +** The first time this is called on a shared-btree, nBytes bytes of memory +** are allocated, zeroed, and returned to the caller. For each subsequent +** call the nBytes parameter is ignored and a pointer to the same blob +** of memory returned. +** +** If the nBytes parameter is 0 and the blob of memory has not yet been +** allocated, a null pointer is returned. If the blob has already been +** allocated, it is returned as normal. +** +** Just before the shared-btree is closed, the function passed as the +** xFree argument when the memory allocation was made is invoked on the +** blob of allocated memory. The xFree function should not call sqlite3_free() +** on the memory, the btree layer does that. +*/ +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + if( !pBt->pSchema && nBytes ){ + pBt->pSchema = sqlite3DbMallocZero(0, nBytes); + pBt->xFreeSchema = xFree; + } + sqlite3BtreeLeave(p); + return pBt->pSchema; +} + +/* +** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared +** btree as the argument handle holds an exclusive lock on the +** sqlite_master table. Otherwise SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){ + int rc; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); + sqlite3BtreeLeave(p); + return rc; +} + + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Obtain a lock on the table whose root page is iTab. The +** lock is a write lock if isWritelock is true or a read lock +** if it is false. +*/ +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ + int rc = SQLITE_OK; + assert( p->inTrans!=TRANS_NONE ); + if( p->sharable ){ + u8 lockType = READ_LOCK + isWriteLock; + assert( READ_LOCK+1==WRITE_LOCK ); + assert( isWriteLock==0 || isWriteLock==1 ); + + sqlite3BtreeEnter(p); + rc = querySharedCacheTableLock(p, iTab, lockType); + if( rc==SQLITE_OK ){ + rc = setSharedCacheTableLock(p, iTab, lockType); + } + sqlite3BtreeLeave(p); + } + return rc; +} +#endif + +#ifndef SQLITE_OMIT_INCRBLOB +/* +** Argument pCsr must be a cursor opened for writing on an +** INTKEY table currently pointing at a valid table entry. +** This function modifies the data stored as part of that entry. +** +** Only the data content may only be modified, it is not possible to +** change the length of the data stored. If this function is called with +** parameters that attempt to write past the end of the existing data, +** no modifications are made and SQLITE_CORRUPT is returned. +*/ +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ + int rc; + assert( cursorOwnsBtShared(pCsr) ); + assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); + assert( pCsr->curFlags & BTCF_Incrblob ); + + rc = restoreCursorPosition(pCsr); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( pCsr->eState!=CURSOR_REQUIRESEEK ); + if( pCsr->eState!=CURSOR_VALID ){ + return SQLITE_ABORT; + } + + /* Save the positions of all other cursors open on this table. This is + ** required in case any of them are holding references to an xFetch + ** version of the b-tree page modified by the accessPayload call below. + ** + ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() + ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence + ** saveAllCursors can only return SQLITE_OK. + */ + VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); + assert( rc==SQLITE_OK ); + + /* Check some assumptions: + ** (a) the cursor is open for writing, + ** (b) there is a read/write transaction open, + ** (c) the connection holds a write-lock on the table (if required), + ** (d) there are no conflicting read-locks, and + ** (e) the cursor points at a valid row of an intKey table. + */ + if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){ + return SQLITE_READONLY; + } + assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0 + && pCsr->pBt->inTransaction==TRANS_WRITE ); + assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); + assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); + assert( pCsr->apPage[pCsr->iPage]->intKey ); + + return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); +} + +/* +** Mark this cursor as an incremental blob cursor. +*/ +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ + pCur->curFlags |= BTCF_Incrblob; + pCur->pBtree->hasIncrblobCur = 1; +} +#endif + +/* +** Set both the "read version" (single byte at byte offset 18) and +** "write version" (single byte at byte offset 19) fields in the database +** header to iVersion. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ + BtShared *pBt = pBtree->pBt; + int rc; /* Return code */ + + assert( iVersion==1 || iVersion==2 ); + + /* If setting the version fields to 1, do not automatically open the + ** WAL connection, even if the version fields are currently set to 2. + */ + pBt->btsFlags &= ~BTS_NO_WAL; + if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; + + rc = sqlite3BtreeBeginTrans(pBtree, 0); + if( rc==SQLITE_OK ){ + u8 *aData = pBt->pPage1->aData; + if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ + rc = sqlite3BtreeBeginTrans(pBtree, 2); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc==SQLITE_OK ){ + aData[18] = (u8)iVersion; + aData[19] = (u8)iVersion; + } + } + } + } + + pBt->btsFlags &= ~BTS_NO_WAL; + return rc; +} + +/* +** Return true if the cursor has a hint specified. This routine is +** only used from within assert() statements +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){ + return (pCsr->hints & mask)!=0; +} + +/* +** Return true if the given Btree is read-only. +*/ +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ + return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; +} + +/* +** Return the size of the header added to each page by this module. +*/ +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } + +#if !defined(SQLITE_OMIT_SHARED_CACHE) +/* +** Return true if the Btree passed as the only argument is sharable. +*/ +SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){ + return p->sharable; +} +#endif + +/************** End of btree.c ***********************************************/ +/************** Begin file backup.c ******************************************/ +/* +** 2009 January 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the implementation of the sqlite3_backup_XXX() +** API functions and the related features. +*/ +/* #include "sqliteInt.h" */ +/* #include "btreeInt.h" */ + +/* +** Structure allocated for each backup operation. +*/ +struct sqlite3_backup { + sqlite3* pDestDb; /* Destination database handle */ + Btree *pDest; /* Destination b-tree file */ + u32 iDestSchema; /* Original schema cookie in destination */ + int bDestLocked; /* True once a write-transaction is open on pDest */ + + Pgno iNext; /* Page number of the next source page to copy */ + sqlite3* pSrcDb; /* Source database handle */ + Btree *pSrc; /* Source b-tree file */ + + int rc; /* Backup process error code */ + + /* These two variables are set by every call to backup_step(). They are + ** read by calls to backup_remaining() and backup_pagecount(). + */ + Pgno nRemaining; /* Number of pages left to copy */ + Pgno nPagecount; /* Total number of pages to copy */ + + int isAttached; /* True once backup has been registered with pager */ + sqlite3_backup *pNext; /* Next backup associated with source pager */ +}; + +/* +** THREAD SAFETY NOTES: +** +** Once it has been created using backup_init(), a single sqlite3_backup +** structure may be accessed via two groups of thread-safe entry points: +** +** * Via the sqlite3_backup_XXX() API function backup_step() and +** backup_finish(). Both these functions obtain the source database +** handle mutex and the mutex associated with the source BtShared +** structure, in that order. +** +** * Via the BackupUpdate() and BackupRestart() functions, which are +** invoked by the pager layer to report various state changes in +** the page cache associated with the source database. The mutex +** associated with the source database BtShared structure will always +** be held when either of these functions are invoked. +** +** The other sqlite3_backup_XXX() API functions, backup_remaining() and +** backup_pagecount() are not thread-safe functions. If they are called +** while some other thread is calling backup_step() or backup_finish(), +** the values returned may be invalid. There is no way for a call to +** BackupUpdate() or BackupRestart() to interfere with backup_remaining() +** or backup_pagecount(). +** +** Depending on the SQLite configuration, the database handles and/or +** the Btree objects may have their own mutexes that require locking. +** Non-sharable Btrees (in-memory databases for example), do not have +** associated mutexes. +*/ + +/* +** Return a pointer corresponding to database zDb (i.e. "main", "temp") +** in connection handle pDb. If such a database cannot be found, return +** a NULL pointer and write an error message to pErrorDb. +** +** If the "temp" database is requested, it may need to be opened by this +** function. If an error occurs while doing so, return 0 and write an +** error message to pErrorDb. +*/ +static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ + int i = sqlite3FindDbName(pDb, zDb); + + if( i==1 ){ + Parse *pParse; + int rc = 0; + pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse)); + if( pParse==0 ){ + sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory"); + rc = SQLITE_NOMEM_BKPT; + }else{ + pParse->db = pDb; + if( sqlite3OpenTempDatabase(pParse) ){ + sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); + rc = SQLITE_ERROR; + } + sqlite3DbFree(pErrorDb, pParse->zErrMsg); + sqlite3ParserReset(pParse); + sqlite3StackFree(pErrorDb, pParse); + } + if( rc ){ + return 0; + } + } + + if( i<0 ){ + sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb); + return 0; + } + + return pDb->aDb[i].pBt; +} + +/* +** Attempt to set the page size of the destination to match the page size +** of the source. +*/ +static int setDestPgsz(sqlite3_backup *p){ + int rc; + rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); + return rc; +} + +/* +** Check that there is no open read-transaction on the b-tree passed as the +** second argument. If there is not, return SQLITE_OK. Otherwise, if there +** is an open read-transaction, return SQLITE_ERROR and leave an error +** message in database handle db. +*/ +static int checkReadTransaction(sqlite3 *db, Btree *p){ + if( sqlite3BtreeIsInReadTrans(p) ){ + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +/* +** Create an sqlite3_backup process to copy the contents of zSrcDb from +** connection handle pSrcDb to zDestDb in pDestDb. If successful, return +** a pointer to the new sqlite3_backup object. +** +** If an error occurs, NULL is returned and an error code and error message +** stored in database handle pDestDb. +*/ +SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init( + sqlite3* pDestDb, /* Database to write to */ + const char *zDestDb, /* Name of database within pDestDb */ + sqlite3* pSrcDb, /* Database connection to read from */ + const char *zSrcDb /* Name of database within pSrcDb */ +){ + sqlite3_backup *p; /* Value to return */ + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + + /* Lock the source database handle. The destination database + ** handle is not locked in this routine, but it is locked in + ** sqlite3_backup_step(). The user is required to ensure that no + ** other thread accesses the destination handle for the duration + ** of the backup operation. Any attempt to use the destination + ** database connection while a backup is in progress may cause + ** a malfunction or a deadlock. + */ + sqlite3_mutex_enter(pSrcDb->mutex); + sqlite3_mutex_enter(pDestDb->mutex); + + if( pSrcDb==pDestDb ){ + sqlite3ErrorWithMsg( + pDestDb, SQLITE_ERROR, "source and destination must be distinct" + ); + p = 0; + }else { + /* Allocate space for a new sqlite3_backup object... + ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a + ** call to sqlite3_backup_init() and is destroyed by a call to + ** sqlite3_backup_finish(). */ + p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup)); + if( !p ){ + sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT); + } + } + + /* If the allocation succeeded, populate the new object. */ + if( p ){ + p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb); + p->pDest = findBtree(pDestDb, pDestDb, zDestDb); + p->pDestDb = pDestDb; + p->pSrcDb = pSrcDb; + p->iNext = 1; + p->isAttached = 0; + + if( 0==p->pSrc || 0==p->pDest + || setDestPgsz(p)==SQLITE_NOMEM + || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK + ){ + /* One (or both) of the named databases did not exist or an OOM + ** error was hit. Or there is a transaction open on the destination + ** database. The error has already been written into the pDestDb + ** handle. All that is left to do here is free the sqlite3_backup + ** structure. */ + sqlite3_free(p); + p = 0; + } + } + if( p ){ + p->pSrc->nBackup++; + } + + sqlite3_mutex_leave(pDestDb->mutex); + sqlite3_mutex_leave(pSrcDb->mutex); + return p; +} + +/* +** Argument rc is an SQLite error code. Return true if this error is +** considered fatal if encountered during a backup operation. All errors +** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED. +*/ +static int isFatalError(int rc){ + return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED)); +} + +/* +** Parameter zSrcData points to a buffer containing the data for +** page iSrcPg from the source database. Copy this data into the +** destination database. +*/ +static int backupOnePage( + sqlite3_backup *p, /* Backup handle */ + Pgno iSrcPg, /* Source database page to backup */ + const u8 *zSrcData, /* Source database page data */ + int bUpdate /* True for an update, false otherwise */ +){ + Pager * const pDestPager = sqlite3BtreePager(p->pDest); + const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc); + int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); + const int nCopy = MIN(nSrcPgsz, nDestPgsz); + const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; +#ifdef SQLITE_HAS_CODEC + /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is + ** guaranteed that the shared-mutex is held by this thread, handle + ** p->pSrc may not actually be the owner. */ + int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc); + int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest); +#endif + int rc = SQLITE_OK; + i64 iOff; + + assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 ); + assert( p->bDestLocked ); + assert( !isFatalError(p->rc) ); + assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); + assert( zSrcData ); + + /* Catch the case where the destination is an in-memory database and the + ** page sizes of the source and destination differ. + */ + if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ + rc = SQLITE_READONLY; + } + +#ifdef SQLITE_HAS_CODEC + /* Backup is not possible if the page size of the destination is changing + ** and a codec is in use. + */ + if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){ + rc = SQLITE_READONLY; + } + + /* Backup is not possible if the number of bytes of reserve space differ + ** between source and destination. If there is a difference, try to + ** fix the destination to agree with the source. If that is not possible, + ** then the backup cannot proceed. + */ + if( nSrcReserve!=nDestReserve ){ + u32 newPgsz = nSrcPgsz; + rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve); + if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY; + } +#endif + + /* This loop runs once for each destination page spanned by the source + ** page. For each iteration, variable iOff is set to the byte offset + ** of the destination page. + */ + for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOffpDest->pBt) ) continue; + if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0)) + && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg)) + ){ + const u8 *zIn = &zSrcData[iOff%nSrcPgsz]; + u8 *zDestData = sqlite3PagerGetData(pDestPg); + u8 *zOut = &zDestData[iOff%nDestPgsz]; + + /* Copy the data from the source page into the destination page. + ** Then clear the Btree layer MemPage.isInit flag. Both this module + ** and the pager code use this trick (clearing the first byte + ** of the page 'extra' space to invalidate the Btree layers + ** cached parse of the page). MemPage.isInit is marked + ** "MUST BE FIRST" for this purpose. + */ + memcpy(zOut, zIn, nCopy); + ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0; + if( iOff==0 && bUpdate==0 ){ + sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc)); + } + } + sqlite3PagerUnref(pDestPg); + } + + return rc; +} + +/* +** If pFile is currently larger than iSize bytes, then truncate it to +** exactly iSize bytes. If pFile is not larger than iSize bytes, then +** this function is a no-op. +** +** Return SQLITE_OK if everything is successful, or an SQLite error +** code if an error occurs. +*/ +static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){ + i64 iCurrent; + int rc = sqlite3OsFileSize(pFile, &iCurrent); + if( rc==SQLITE_OK && iCurrent>iSize ){ + rc = sqlite3OsTruncate(pFile, iSize); + } + return rc; +} + +/* +** Register this backup object with the associated source pager for +** callbacks when pages are changed or the cache invalidated. +*/ +static void attachBackupObject(sqlite3_backup *p){ + sqlite3_backup **pp; + assert( sqlite3BtreeHoldsMutex(p->pSrc) ); + pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); + p->pNext = *pp; + *pp = p; + p->isAttached = 1; +} + +/* +** Copy nPage pages from the source b-tree to the destination. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){ + int rc; + int destMode; /* Destination journal mode */ + int pgszSrc = 0; /* Source page size */ + int pgszDest = 0; /* Destination page size */ + +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(p->pSrcDb->mutex); + sqlite3BtreeEnter(p->pSrc); + if( p->pDestDb ){ + sqlite3_mutex_enter(p->pDestDb->mutex); + } + + rc = p->rc; + if( !isFatalError(rc) ){ + Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */ + Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */ + int ii; /* Iterator variable */ + int nSrcPage = -1; /* Size of source db in pages */ + int bCloseTrans = 0; /* True if src db requires unlocking */ + + /* If the source pager is currently in a write-transaction, return + ** SQLITE_BUSY immediately. + */ + if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){ + rc = SQLITE_BUSY; + }else{ + rc = SQLITE_OK; + } + + /* Lock the destination database, if it is not locked already. */ + if( SQLITE_OK==rc && p->bDestLocked==0 + && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) + ){ + p->bDestLocked = 1; + sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema); + } + + /* If there is no open read-transaction on the source database, open + ** one now. If a transaction is opened here, then it will be closed + ** before this function exits. + */ + if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){ + rc = sqlite3BtreeBeginTrans(p->pSrc, 0); + bCloseTrans = 1; + } + + /* Do not allow backup if the destination database is in WAL mode + ** and the page sizes are different between source and destination */ + pgszSrc = sqlite3BtreeGetPageSize(p->pSrc); + pgszDest = sqlite3BtreeGetPageSize(p->pDest); + destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest)); + if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){ + rc = SQLITE_READONLY; + } + + /* Now that there is a read-lock on the source database, query the + ** source pager for the number of pages in the database. + */ + nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); + assert( nSrcPage>=0 ); + for(ii=0; (nPage<0 || iiiNext<=(Pgno)nSrcPage && !rc; ii++){ + const Pgno iSrcPg = p->iNext; /* Source page number */ + if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ + DbPage *pSrcPg; /* Source page object */ + rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY); + if( rc==SQLITE_OK ){ + rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0); + sqlite3PagerUnref(pSrcPg); + } + } + p->iNext++; + } + if( rc==SQLITE_OK ){ + p->nPagecount = nSrcPage; + p->nRemaining = nSrcPage+1-p->iNext; + if( p->iNext>(Pgno)nSrcPage ){ + rc = SQLITE_DONE; + }else if( !p->isAttached ){ + attachBackupObject(p); + } + } + + /* Update the schema version field in the destination database. This + ** is to make sure that the schema-version really does change in + ** the case where the source and destination databases have the + ** same schema version. + */ + if( rc==SQLITE_DONE ){ + if( nSrcPage==0 ){ + rc = sqlite3BtreeNewDb(p->pDest); + nSrcPage = 1; + } + if( rc==SQLITE_OK || rc==SQLITE_DONE ){ + rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1); + } + if( rc==SQLITE_OK ){ + if( p->pDestDb ){ + sqlite3ResetAllSchemasOfConnection(p->pDestDb); + } + if( destMode==PAGER_JOURNALMODE_WAL ){ + rc = sqlite3BtreeSetVersion(p->pDest, 2); + } + } + if( rc==SQLITE_OK ){ + int nDestTruncate; + /* Set nDestTruncate to the final number of pages in the destination + ** database. The complication here is that the destination page + ** size may be different to the source page size. + ** + ** If the source page size is smaller than the destination page size, + ** round up. In this case the call to sqlite3OsTruncate() below will + ** fix the size of the file. However it is important to call + ** sqlite3PagerTruncateImage() here so that any pages in the + ** destination file that lie beyond the nDestTruncate page mark are + ** journalled by PagerCommitPhaseOne() before they are destroyed + ** by the file truncation. + */ + assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); + assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); + if( pgszSrcpDest->pBt) ){ + nDestTruncate--; + } + }else{ + nDestTruncate = nSrcPage * (pgszSrc/pgszDest); + } + assert( nDestTruncate>0 ); + + if( pgszSrc= iSize || ( + nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) + && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest + )); + + /* This block ensures that all data required to recreate the original + ** database has been stored in the journal for pDestPager and the + ** journal synced to disk. So at this point we may safely modify + ** the database file in any way, knowing that if a power failure + ** occurs, the original database will be reconstructed from the + ** journal file. */ + sqlite3PagerPagecount(pDestPager, &nDstPage); + for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){ + if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){ + DbPage *pPg; + rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pPg); + sqlite3PagerUnref(pPg); + } + } + } + if( rc==SQLITE_OK ){ + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); + } + + /* Write the extra pages and truncate the database file as required */ + iEnd = MIN(PENDING_BYTE + pgszDest, iSize); + for( + iOff=PENDING_BYTE+pgszSrc; + rc==SQLITE_OK && iOffpDest, 0)) + ){ + rc = SQLITE_DONE; + } + } + } + + /* If bCloseTrans is true, then this function opened a read transaction + ** on the source database. Close the read transaction here. There is + ** no need to check the return values of the btree methods here, as + ** "committing" a read-only transaction cannot fail. + */ + if( bCloseTrans ){ + TESTONLY( int rc2 ); + TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0); + TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0); + assert( rc2==SQLITE_OK ); + } + + if( rc==SQLITE_IOERR_NOMEM ){ + rc = SQLITE_NOMEM_BKPT; + } + p->rc = rc; + } + if( p->pDestDb ){ + sqlite3_mutex_leave(p->pDestDb->mutex); + } + sqlite3BtreeLeave(p->pSrc); + sqlite3_mutex_leave(p->pSrcDb->mutex); + return rc; +} + +/* +** Release all resources associated with an sqlite3_backup* handle. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p){ + sqlite3_backup **pp; /* Ptr to head of pagers backup list */ + sqlite3 *pSrcDb; /* Source database connection */ + int rc; /* Value to return */ + + /* Enter the mutexes */ + if( p==0 ) return SQLITE_OK; + pSrcDb = p->pSrcDb; + sqlite3_mutex_enter(pSrcDb->mutex); + sqlite3BtreeEnter(p->pSrc); + if( p->pDestDb ){ + sqlite3_mutex_enter(p->pDestDb->mutex); + } + + /* Detach this backup from the source pager. */ + if( p->pDestDb ){ + p->pSrc->nBackup--; + } + if( p->isAttached ){ + pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); + while( *pp!=p ){ + pp = &(*pp)->pNext; + } + *pp = p->pNext; + } + + /* If a transaction is still open on the Btree, roll it back. */ + sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0); + + /* Set the error code of the destination database handle. */ + rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; + if( p->pDestDb ){ + sqlite3Error(p->pDestDb, rc); + + /* Exit the mutexes and free the backup context structure. */ + sqlite3LeaveMutexAndCloseZombie(p->pDestDb); + } + sqlite3BtreeLeave(p->pSrc); + if( p->pDestDb ){ + /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a + ** call to sqlite3_backup_init() and is destroyed by a call to + ** sqlite3_backup_finish(). */ + sqlite3_free(p); + } + sqlite3LeaveMutexAndCloseZombie(pSrcDb); + return rc; +} + +/* +** Return the number of pages still to be backed up as of the most recent +** call to sqlite3_backup_step(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return p->nRemaining; +} + +/* +** Return the total number of pages in the source database as of the most +** recent call to sqlite3_backup_step(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return p->nPagecount; +} + +/* +** This function is called after the contents of page iPage of the +** source database have been modified. If page iPage has already been +** copied into the destination database, then the data written to the +** destination is now invalidated. The destination copy of iPage needs +** to be updated with the new data before the backup operation is +** complete. +** +** It is assumed that the mutex associated with the BtShared object +** corresponding to the source database is held when this function is +** called. +*/ +static SQLITE_NOINLINE void backupUpdate( + sqlite3_backup *p, + Pgno iPage, + const u8 *aData +){ + assert( p!=0 ); + do{ + assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); + if( !isFatalError(p->rc) && iPageiNext ){ + /* The backup process p has already copied page iPage. But now it + ** has been modified by a transaction on the source pager. Copy + ** the new data into the backup. + */ + int rc; + assert( p->pDestDb ); + sqlite3_mutex_enter(p->pDestDb->mutex); + rc = backupOnePage(p, iPage, aData, 1); + sqlite3_mutex_leave(p->pDestDb->mutex); + assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); + if( rc!=SQLITE_OK ){ + p->rc = rc; + } + } + }while( (p = p->pNext)!=0 ); +} +SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ + if( pBackup ) backupUpdate(pBackup, iPage, aData); +} + +/* +** Restart the backup process. This is called when the pager layer +** detects that the database has been modified by an external database +** connection. In this case there is no way of knowing which of the +** pages that have been copied into the destination database are still +** valid and which are not, so the entire process needs to be restarted. +** +** It is assumed that the mutex associated with the BtShared object +** corresponding to the source database is held when this function is +** called. +*/ +SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){ + sqlite3_backup *p; /* Iterator variable */ + for(p=pBackup; p; p=p->pNext){ + assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); + p->iNext = 1; + } +} + +#ifndef SQLITE_OMIT_VACUUM +/* +** Copy the complete content of pBtFrom into pBtTo. A transaction +** must be active for both files. +** +** The size of file pTo may be reduced by this operation. If anything +** goes wrong, the transaction on pTo is rolled back. If successful, the +** transaction is committed before returning. +*/ +SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ + int rc; + sqlite3_file *pFd; /* File descriptor for database pTo */ + sqlite3_backup b; + sqlite3BtreeEnter(pTo); + sqlite3BtreeEnter(pFrom); + + assert( sqlite3BtreeIsInTrans(pTo) ); + pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); + if( pFd->pMethods ){ + i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); + rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte); + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + if( rc ) goto copy_finished; + } + + /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set + ** to 0. This is used by the implementations of sqlite3_backup_step() + ** and sqlite3_backup_finish() to detect that they are being called + ** from this function, not directly by the user. + */ + memset(&b, 0, sizeof(b)); + b.pSrcDb = pFrom->db; + b.pSrc = pFrom; + b.pDest = pTo; + b.iNext = 1; + +#ifdef SQLITE_HAS_CODEC + sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom)); +#endif + + /* 0x7FFFFFFF is the hard limit for the number of pages in a database + ** file. By passing this as the number of pages to copy to + ** sqlite3_backup_step(), we can guarantee that the copy finishes + ** within a single call (unless an error occurs). The assert() statement + ** checks this assumption - (p->rc) should be set to either SQLITE_DONE + ** or an error code. + */ + sqlite3_backup_step(&b, 0x7FFFFFFF); + assert( b.rc!=SQLITE_OK ); + rc = sqlite3_backup_finish(&b); + if( rc==SQLITE_OK ){ + pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; + }else{ + sqlite3PagerClearCache(sqlite3BtreePager(b.pDest)); + } + + assert( sqlite3BtreeIsInTrans(pTo)==0 ); +copy_finished: + sqlite3BtreeLeave(pFrom); + sqlite3BtreeLeave(pTo); + return rc; +} +#endif /* SQLITE_OMIT_VACUUM */ + +/************** End of backup.c **********************************************/ +/************** Begin file vdbemem.c *****************************************/ +/* +** 2004 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code use to manipulate "Mem" structure. A "Mem" +** stores a single value in the VDBE. Mem is an opaque structure visible +** only within the VDBE. Interface routines refer to a Mem using the +** name sqlite_value +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +#ifdef SQLITE_DEBUG +/* +** Check invariants on a Mem object. +** +** This routine is intended for use inside of assert() statements, like +** this: assert( sqlite3VdbeCheckMemInvariants(pMem) ); +*/ +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){ + /* If MEM_Dyn is set then Mem.xDel!=0. + ** Mem.xDel is might not be initialized if MEM_Dyn is clear. + */ + assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); + + /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we + ** ensure that if Mem.szMalloc>0 then it is safe to do + ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. + ** That saves a few cycles in inner loops. */ + assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); + + /* Cannot be both MEM_Int and MEM_Real at the same time */ + assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); + + /* The szMalloc field holds the correct memory allocation size */ + assert( p->szMalloc==0 + || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) ); + + /* If p holds a string or blob, the Mem.z must point to exactly + ** one of the following: + ** + ** (1) Memory in Mem.zMalloc and managed by the Mem object + ** (2) Memory to be freed using Mem.xDel + ** (3) An ephemeral string or blob + ** (4) A static string or blob + */ + if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){ + assert( + ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) + + ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + + ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + + ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1 + ); + } + return 1; +} +#endif + + +/* +** If pMem is an object with a valid string representation, this routine +** ensures the internal encoding for the string representation is +** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. +** +** If pMem is not a string object, or the encoding of the string +** representation is already stored using the requested encoding, then this +** routine is a no-op. +** +** SQLITE_OK is returned if the conversion is successful (or not required). +** SQLITE_NOMEM may be returned if a malloc() fails during conversion +** between formats. +*/ +SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ +#ifndef SQLITE_OMIT_UTF16 + int rc; +#endif + assert( (pMem->flags&MEM_RowSet)==0 ); + assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE + || desiredEnc==SQLITE_UTF16BE ); + if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ + return SQLITE_OK; + } + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); +#ifdef SQLITE_OMIT_UTF16 + return SQLITE_ERROR; +#else + + /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, + ** then the encoding of the value may not have changed. + */ + rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc); + assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); + assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); + assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); + return rc; +#endif +} + +/* +** Make sure pMem->z points to a writable allocation of at least +** min(n,32) bytes. +** +** If the bPreserve argument is true, then copy of the content of +** pMem->z into the new allocation. pMem must be either a string or +** blob if bPreserve is true. If bPreserve is false, any prior content +** in pMem->z is discarded. +*/ +SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ + assert( sqlite3VdbeCheckMemInvariants(pMem) ); + assert( (pMem->flags&MEM_RowSet)==0 ); + testcase( pMem->db==0 ); + + /* If the bPreserve flag is set to true, then the memory cell must already + ** contain a valid string or blob value. */ + assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) ); + testcase( bPreserve && pMem->z==0 ); + + assert( pMem->szMalloc==0 + || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) ); + if( pMem->szMallocszMalloc>0 && pMem->z==pMem->zMalloc ){ + pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); + bPreserve = 0; + }else{ + if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc); + pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); + } + if( pMem->zMalloc==0 ){ + sqlite3VdbeMemSetNull(pMem); + pMem->z = 0; + pMem->szMalloc = 0; + return SQLITE_NOMEM_BKPT; + }else{ + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); + } + } + + if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){ + memcpy(pMem->zMalloc, pMem->z, pMem->n); + } + if( (pMem->flags&MEM_Dyn)!=0 ){ + assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); + pMem->xDel((void *)(pMem->z)); + } + + pMem->z = pMem->zMalloc; + pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static); + return SQLITE_OK; +} + +/* +** Change the pMem->zMalloc allocation to be at least szNew bytes. +** If pMem->zMalloc already meets or exceeds the requested size, this +** routine is a no-op. +** +** Any prior string or blob content in the pMem object may be discarded. +** The pMem->xDel destructor is called, if it exists. Though MEM_Str +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null +** values are preserved. +** +** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) +** if unable to complete the resizing. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ + assert( szNew>0 ); + assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); + if( pMem->szMallocflags & MEM_Dyn)==0 ); + pMem->z = pMem->zMalloc; + pMem->flags &= (MEM_Null|MEM_Int|MEM_Real); + return SQLITE_OK; +} + +/* +** Change pMem so that its MEM_Str or MEM_Blob value is stored in +** MEM.zMalloc, where it can be safely written. +** +** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){ + int f; + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( (pMem->flags&MEM_RowSet)==0 ); + ExpandBlob(pMem); + f = pMem->flags; + if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){ + if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ + return SQLITE_NOMEM_BKPT; + } + pMem->z[pMem->n] = 0; + pMem->z[pMem->n+1] = 0; + pMem->flags |= MEM_Term; + } + pMem->flags &= ~MEM_Ephem; +#ifdef SQLITE_DEBUG + pMem->pScopyFrom = 0; +#endif + + return SQLITE_OK; +} + +/* +** If the given Mem* has a zero-filled tail, turn it into an ordinary +** blob stored in dynamically allocated space. +*/ +#ifndef SQLITE_OMIT_INCRBLOB +SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){ + if( pMem->flags & MEM_Zero ){ + int nByte; + assert( pMem->flags&MEM_Blob ); + assert( (pMem->flags&MEM_RowSet)==0 ); + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + + /* Set nByte to the number of bytes required to store the expanded blob. */ + nByte = pMem->n + pMem->u.nZero; + if( nByte<=0 ){ + nByte = 1; + } + if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ + return SQLITE_NOMEM_BKPT; + } + + memset(&pMem->z[pMem->n], 0, pMem->u.nZero); + pMem->n += pMem->u.nZero; + pMem->flags &= ~(MEM_Zero|MEM_Term); + } + return SQLITE_OK; +} +#endif + +/* +** It is already known that pMem contains an unterminated string. +** Add the zero terminator. +*/ +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ + if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ + return SQLITE_NOMEM_BKPT; + } + pMem->z[pMem->n] = 0; + pMem->z[pMem->n+1] = 0; + pMem->flags |= MEM_Term; + return SQLITE_OK; +} + +/* +** Make sure the given Mem is \u0000 terminated. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){ + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) ); + testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 ); + if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){ + return SQLITE_OK; /* Nothing to do */ + }else{ + return vdbeMemAddTerminator(pMem); + } +} + +/* +** Add MEM_Str to the set of representations for the given Mem. Numbers +** are converted using sqlite3_snprintf(). Converting a BLOB to a string +** is a no-op. +** +** Existing representations MEM_Int and MEM_Real are invalidated if +** bForce is true but are retained if bForce is false. +** +** A MEM_Null value will never be passed to this function. This function is +** used for converting values to text for returning to the user (i.e. via +** sqlite3_value_text()), or for ensuring that values to be used as btree +** keys are strings. In the former case a NULL pointer is returned the +** user and the latter is an internal programming error. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ + int fg = pMem->flags; + const int nByte = 32; + + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( !(fg&MEM_Zero) ); + assert( !(fg&(MEM_Str|MEM_Blob)) ); + assert( fg&(MEM_Int|MEM_Real) ); + assert( (pMem->flags&MEM_RowSet)==0 ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + + + if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){ + return SQLITE_NOMEM_BKPT; + } + + /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 + ** string representation of the value. Then, if the required encoding + ** is UTF-16le or UTF-16be do a translation. + ** + ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. + */ + if( fg & MEM_Int ){ + sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); + }else{ + assert( fg & MEM_Real ); + sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); + } + pMem->n = sqlite3Strlen30(pMem->z); + pMem->enc = SQLITE_UTF8; + pMem->flags |= MEM_Str|MEM_Term; + if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); + sqlite3VdbeChangeEncoding(pMem, enc); + return SQLITE_OK; +} + +/* +** Memory cell pMem contains the context of an aggregate function. +** This routine calls the finalize method for that function. The +** result of the aggregate is stored back into pMem. +** +** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK +** otherwise. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ + int rc = SQLITE_OK; + if( ALWAYS(pFunc && pFunc->xFinalize) ){ + sqlite3_context ctx; + Mem t; + assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + memset(&ctx, 0, sizeof(ctx)); + memset(&t, 0, sizeof(t)); + t.flags = MEM_Null; + t.db = pMem->db; + ctx.pOut = &t; + ctx.pMem = pMem; + ctx.pFunc = pFunc; + pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ + assert( (pMem->flags & MEM_Dyn)==0 ); + if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc); + memcpy(pMem, &t, sizeof(t)); + rc = ctx.isError; + } + return rc; +} + +/* +** If the memory cell contains a value that must be freed by +** invoking the external callback in Mem.xDel, then this routine +** will free that value. It also sets Mem.flags to MEM_Null. +** +** This is a helper routine for sqlite3VdbeMemSetNull() and +** for sqlite3VdbeMemRelease(). Use those other routines as the +** entry point for releasing Mem resources. +*/ +static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){ + assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); + assert( VdbeMemDynamic(p) ); + if( p->flags&MEM_Agg ){ + sqlite3VdbeMemFinalize(p, p->u.pDef); + assert( (p->flags & MEM_Agg)==0 ); + testcase( p->flags & MEM_Dyn ); + } + if( p->flags&MEM_Dyn ){ + assert( (p->flags&MEM_RowSet)==0 ); + assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); + p->xDel((void *)p->z); + }else if( p->flags&MEM_RowSet ){ + sqlite3RowSetClear(p->u.pRowSet); + }else if( p->flags&MEM_Frame ){ + VdbeFrame *pFrame = p->u.pFrame; + pFrame->pParent = pFrame->v->pDelFrame; + pFrame->v->pDelFrame = pFrame; + } + p->flags = MEM_Null; +} + +/* +** Release memory held by the Mem p, both external memory cleared +** by p->xDel and memory in p->zMalloc. +** +** This is a helper routine invoked by sqlite3VdbeMemRelease() in +** the unusual case where there really is memory in p that needs +** to be freed. +*/ +static SQLITE_NOINLINE void vdbeMemClear(Mem *p){ + if( VdbeMemDynamic(p) ){ + vdbeMemClearExternAndSetNull(p); + } + if( p->szMalloc ){ + sqlite3DbFree(p->db, p->zMalloc); + p->szMalloc = 0; + } + p->z = 0; +} + +/* +** Release any memory resources held by the Mem. Both the memory that is +** free by Mem.xDel and the Mem.zMalloc allocation are freed. +** +** Use this routine prior to clean up prior to abandoning a Mem, or to +** reset a Mem back to its minimum memory utilization. +** +** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space +** prior to inserting new content into the Mem. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ + assert( sqlite3VdbeCheckMemInvariants(p) ); + if( VdbeMemDynamic(p) || p->szMalloc ){ + vdbeMemClear(p); + } +} + +/* +** Convert a 64-bit IEEE double into a 64-bit signed integer. +** If the double is out of range of a 64-bit signed integer then +** return the closest available 64-bit signed integer. +*/ +static i64 doubleToInt64(double r){ +#ifdef SQLITE_OMIT_FLOATING_POINT + /* When floating-point is omitted, double and int64 are the same thing */ + return r; +#else + /* + ** Many compilers we encounter do not define constants for the + ** minimum and maximum 64-bit integers, or they define them + ** inconsistently. And many do not understand the "LL" notation. + ** So we define our own static constants here using nothing + ** larger than a 32-bit integer constant. + */ + static const i64 maxInt = LARGEST_INT64; + static const i64 minInt = SMALLEST_INT64; + + if( r<=(double)minInt ){ + return minInt; + }else if( r>=(double)maxInt ){ + return maxInt; + }else{ + return (i64)r; + } +#endif +} + +/* +** Return some kind of integer value which is the best we can do +** at representing the value that *pMem describes as an integer. +** If pMem is an integer, then the value is exact. If pMem is +** a floating-point then the value returned is the integer part. +** If pMem is a string or blob, then we make an attempt to convert +** it into an integer and return that. If pMem represents an +** an SQL-NULL value, return 0. +** +** If pMem represents a string value, its encoding might be changed. +*/ +SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){ + int flags; + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + flags = pMem->flags; + if( flags & MEM_Int ){ + return pMem->u.i; + }else if( flags & MEM_Real ){ + return doubleToInt64(pMem->u.r); + }else if( flags & (MEM_Str|MEM_Blob) ){ + i64 value = 0; + assert( pMem->z || pMem->n==0 ); + sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); + return value; + }else{ + return 0; + } +} + +/* +** Return the best representation of pMem that we can get into a +** double. If pMem is already a double or an integer, return its +** value. If it is a string or blob, try to convert it to a double. +** If it is a NULL, return 0.0. +*/ +SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){ + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + if( pMem->flags & MEM_Real ){ + return pMem->u.r; + }else if( pMem->flags & MEM_Int ){ + return (double)pMem->u.i; + }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + double val = (double)0; + sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); + return val; + }else{ + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + return (double)0; + } +} + +/* +** The MEM structure is already a MEM_Real. Try to also make it a +** MEM_Int if we can. +*/ +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){ + i64 ix; + assert( pMem->flags & MEM_Real ); + assert( (pMem->flags & MEM_RowSet)==0 ); + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + + ix = doubleToInt64(pMem->u.r); + + /* Only mark the value as an integer if + ** + ** (1) the round-trip conversion real->int->real is a no-op, and + ** (2) The integer is neither the largest nor the smallest + ** possible integer (ticket #3922) + ** + ** The second and third terms in the following conditional enforces + ** the second condition under the assumption that addition overflow causes + ** values to wrap around. + */ + if( pMem->u.r==ix && ix>SMALLEST_INT64 && ixu.i = ix; + MemSetTypeFlag(pMem, MEM_Int); + } +} + +/* +** Convert pMem to type integer. Invalidate any prior representations. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){ + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( (pMem->flags & MEM_RowSet)==0 ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + + pMem->u.i = sqlite3VdbeIntValue(pMem); + MemSetTypeFlag(pMem, MEM_Int); + return SQLITE_OK; +} + +/* +** Convert pMem so that it is of type MEM_Real. +** Invalidate any prior representations. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){ + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + + pMem->u.r = sqlite3VdbeRealValue(pMem); + MemSetTypeFlag(pMem, MEM_Real); + return SQLITE_OK; +} + +/* +** Convert pMem so that it has types MEM_Real or MEM_Int or both. +** Invalidate any prior representations. +** +** Every effort is made to force the conversion, even if the input +** is a string that does not look completely like a number. Convert +** as much of the string as we can and ignore the rest. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){ + if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){ + assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){ + MemSetTypeFlag(pMem, MEM_Int); + }else{ + pMem->u.r = sqlite3VdbeRealValue(pMem); + MemSetTypeFlag(pMem, MEM_Real); + sqlite3VdbeIntegerAffinity(pMem); + } + } + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); + pMem->flags &= ~(MEM_Str|MEM_Blob); + return SQLITE_OK; +} + +/* +** Cast the datatype of the value in pMem according to the affinity +** "aff". Casting is different from applying affinity in that a cast +** is forced. In other words, the value is converted into the desired +** affinity even if that results in loss of data. This routine is +** used (for example) to implement the SQL "cast()" operator. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ + if( pMem->flags & MEM_Null ) return; + switch( aff ){ + case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */ + if( (pMem->flags & MEM_Blob)==0 ){ + sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); + assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); + MemSetTypeFlag(pMem, MEM_Blob); + }else{ + pMem->flags &= ~(MEM_TypeMask&~MEM_Blob); + } + break; + } + case SQLITE_AFF_NUMERIC: { + sqlite3VdbeMemNumerify(pMem); + break; + } + case SQLITE_AFF_INTEGER: { + sqlite3VdbeMemIntegerify(pMem); + break; + } + case SQLITE_AFF_REAL: { + sqlite3VdbeMemRealify(pMem); + break; + } + default: { + assert( aff==SQLITE_AFF_TEXT ); + assert( MEM_Str==(MEM_Blob>>3) ); + pMem->flags |= (pMem->flags&MEM_Blob)>>3; + sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); + assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); + pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); + break; + } + } +} + +/* +** Initialize bulk memory to be a consistent Mem object. +** +** The minimum amount of initialization feasible is performed. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){ + assert( (flags & ~MEM_TypeMask)==0 ); + pMem->flags = flags; + pMem->db = db; + pMem->szMalloc = 0; +} + + +/* +** Delete any previous value and set the value stored in *pMem to NULL. +** +** This routine calls the Mem.xDel destructor to dispose of values that +** require the destructor. But it preserves the Mem.zMalloc memory allocation. +** To free all resources, use sqlite3VdbeMemRelease(), which both calls this +** routine to invoke the destructor and deallocates Mem.zMalloc. +** +** Use this routine to reset the Mem prior to insert a new value. +** +** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){ + if( VdbeMemDynamic(pMem) ){ + vdbeMemClearExternAndSetNull(pMem); + }else{ + pMem->flags = MEM_Null; + } +} +SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){ + sqlite3VdbeMemSetNull((Mem*)p); +} + +/* +** Delete any previous value and set the value to be a BLOB of length +** n containing all zeros. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ + sqlite3VdbeMemRelease(pMem); + pMem->flags = MEM_Blob|MEM_Zero; + pMem->n = 0; + if( n<0 ) n = 0; + pMem->u.nZero = n; + pMem->enc = SQLITE_UTF8; + pMem->z = 0; +} + +/* +** The pMem is known to contain content that needs to be destroyed prior +** to a value change. So invoke the destructor, then set the value to +** a 64-bit integer. +*/ +static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){ + sqlite3VdbeMemSetNull(pMem); + pMem->u.i = val; + pMem->flags = MEM_Int; +} + +/* +** Delete any previous value and set the value stored in *pMem to val, +** manifest type INTEGER. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ + if( VdbeMemDynamic(pMem) ){ + vdbeReleaseAndSetInt64(pMem, val); + }else{ + pMem->u.i = val; + pMem->flags = MEM_Int; + } +} + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** Delete any previous value and set the value stored in *pMem to val, +** manifest type REAL. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ + sqlite3VdbeMemSetNull(pMem); + if( !sqlite3IsNaN(val) ){ + pMem->u.r = val; + pMem->flags = MEM_Real; + } +} +#endif + +/* +** Delete any previous value and set the value of pMem to be an +** empty boolean index. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){ + sqlite3 *db = pMem->db; + assert( db!=0 ); + assert( (pMem->flags & MEM_RowSet)==0 ); + sqlite3VdbeMemRelease(pMem); + pMem->zMalloc = sqlite3DbMallocRawNN(db, 64); + if( db->mallocFailed ){ + pMem->flags = MEM_Null; + pMem->szMalloc = 0; + }else{ + assert( pMem->zMalloc ); + pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc); + pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc); + assert( pMem->u.pRowSet!=0 ); + pMem->flags = MEM_RowSet; + } +} + +/* +** Return true if the Mem object contains a TEXT or BLOB that is +** too large - whose size exceeds SQLITE_MAX_LENGTH. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){ + assert( p->db!=0 ); + if( p->flags & (MEM_Str|MEM_Blob) ){ + int n = p->n; + if( p->flags & MEM_Zero ){ + n += p->u.nZero; + } + return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; + } + return 0; +} + +#ifdef SQLITE_DEBUG +/* +** This routine prepares a memory cell for modification by breaking +** its link to a shallow copy and by marking any current shallow +** copies of this cell as invalid. +** +** This is used for testing and debugging only - to make sure shallow +** copies are not misused. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){ + int i; + Mem *pX; + for(i=0, pX=pVdbe->aMem; inMem; i++, pX++){ + if( pX->pScopyFrom==pMem ){ + pX->flags |= MEM_Undefined; + pX->pScopyFrom = 0; + } + } + pMem->pScopyFrom = 0; +} +#endif /* SQLITE_DEBUG */ + + +/* +** Make an shallow copy of pFrom into pTo. Prior contents of +** pTo are freed. The pFrom->z field is not duplicated. If +** pFrom->z is used, then pTo->z points to the same thing as pFrom->z +** and flags gets srcType (either MEM_Ephem or MEM_Static). +*/ +static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){ + vdbeMemClearExternAndSetNull(pTo); + assert( !VdbeMemDynamic(pTo) ); + sqlite3VdbeMemShallowCopy(pTo, pFrom, eType); +} +SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ + assert( (pFrom->flags & MEM_RowSet)==0 ); + assert( pTo->db==pFrom->db ); + if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; } + memcpy(pTo, pFrom, MEMCELLSIZE); + if( (pFrom->flags&MEM_Static)==0 ){ + pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); + assert( srcType==MEM_Ephem || srcType==MEM_Static ); + pTo->flags |= srcType; + } +} + +/* +** Make a full copy of pFrom into pTo. Prior contents of pTo are +** freed before the copy is made. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ + int rc = SQLITE_OK; + + assert( (pFrom->flags & MEM_RowSet)==0 ); + if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); + memcpy(pTo, pFrom, MEMCELLSIZE); + pTo->flags &= ~MEM_Dyn; + if( pTo->flags&(MEM_Str|MEM_Blob) ){ + if( 0==(pFrom->flags&MEM_Static) ){ + pTo->flags |= MEM_Ephem; + rc = sqlite3VdbeMemMakeWriteable(pTo); + } + } + + return rc; +} + +/* +** Transfer the contents of pFrom to pTo. Any existing value in pTo is +** freed. If pFrom contains ephemeral data, a copy is made. +** +** pFrom contains an SQL NULL when this routine returns. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ + assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); + assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); + assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); + + sqlite3VdbeMemRelease(pTo); + memcpy(pTo, pFrom, sizeof(Mem)); + pFrom->flags = MEM_Null; + pFrom->szMalloc = 0; +} + +/* +** Change the value of a Mem to be a string or a BLOB. +** +** The memory management strategy depends on the value of the xDel +** parameter. If the value passed is SQLITE_TRANSIENT, then the +** string is copied into a (possibly existing) buffer managed by the +** Mem structure. Otherwise, any existing buffer is freed and the +** pointer copied. +** +** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH +** size limit) then no memory allocation occurs. If the string can be +** stored without allocating memory, then it is. If a memory allocation +** is required to store the string, then value of pMem is unchanged. In +** either case, SQLITE_TOOBIG is returned. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemSetStr( + Mem *pMem, /* Memory cell to set to string value */ + const char *z, /* String pointer */ + int n, /* Bytes in string, or negative */ + u8 enc, /* Encoding of z. 0 for BLOBs */ + void (*xDel)(void*) /* Destructor function */ +){ + int nByte = n; /* New value for pMem->n */ + int iLimit; /* Maximum allowed string or blob size */ + u16 flags = 0; /* New value for pMem->flags */ + + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); + assert( (pMem->flags & MEM_RowSet)==0 ); + + /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ + if( !z ){ + sqlite3VdbeMemSetNull(pMem); + return SQLITE_OK; + } + + if( pMem->db ){ + iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; + }else{ + iLimit = SQLITE_MAX_LENGTH; + } + flags = (enc==0?MEM_Blob:MEM_Str); + if( nByte<0 ){ + assert( enc!=0 ); + if( enc==SQLITE_UTF8 ){ + nByte = sqlite3Strlen30(z); + if( nByte>iLimit ) nByte = iLimit+1; + }else{ + for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} + } + flags |= MEM_Term; + } + + /* The following block sets the new values of Mem.z and Mem.xDel. It + ** also sets a flag in local variable "flags" to indicate the memory + ** management (one of MEM_Dyn or MEM_Static). + */ + if( xDel==SQLITE_TRANSIENT ){ + int nAlloc = nByte; + if( flags&MEM_Term ){ + nAlloc += (enc==SQLITE_UTF8?1:2); + } + if( nByte>iLimit ){ + return SQLITE_TOOBIG; + } + testcase( nAlloc==0 ); + testcase( nAlloc==31 ); + testcase( nAlloc==32 ); + if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){ + return SQLITE_NOMEM_BKPT; + } + memcpy(pMem->z, z, nAlloc); + }else if( xDel==SQLITE_DYNAMIC ){ + sqlite3VdbeMemRelease(pMem); + pMem->zMalloc = pMem->z = (char *)z; + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); + }else{ + sqlite3VdbeMemRelease(pMem); + pMem->z = (char *)z; + pMem->xDel = xDel; + flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); + } + + pMem->n = nByte; + pMem->flags = flags; + pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); + +#ifndef SQLITE_OMIT_UTF16 + if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ + return SQLITE_NOMEM_BKPT; + } +#endif + + if( nByte>iLimit ){ + return SQLITE_TOOBIG; + } + + return SQLITE_OK; +} + +/* +** Move data out of a btree key or data field and into a Mem structure. +** The data or key is taken from the entry that pCur is currently pointing +** to. offset and amt determine what portion of the data or key to retrieve. +** key is true to get the key or false to get data. The result is written +** into the pMem element. +** +** The pMem object must have been initialized. This routine will use +** pMem->zMalloc to hold the content from the btree, if possible. New +** pMem->zMalloc space will be allocated if necessary. The calling routine +** is responsible for making sure that the pMem object is eventually +** destroyed. +** +** If this routine fails for any reason (malloc returns NULL or unable +** to read from the disk) then the pMem is left in an inconsistent state. +*/ +static SQLITE_NOINLINE int vdbeMemFromBtreeResize( + BtCursor *pCur, /* Cursor pointing at record to retrieve. */ + u32 offset, /* Offset from the start of data to return bytes from. */ + u32 amt, /* Number of bytes to return. */ + int key, /* If true, retrieve from the btree key, not data. */ + Mem *pMem /* OUT: Return data in this Mem structure. */ +){ + int rc; + pMem->flags = MEM_Null; + if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){ + if( key ){ + rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); + }else{ + rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); + } + if( rc==SQLITE_OK ){ + pMem->z[amt] = 0; + pMem->z[amt+1] = 0; + pMem->flags = MEM_Blob|MEM_Term; + pMem->n = (int)amt; + }else{ + sqlite3VdbeMemRelease(pMem); + } + } + return rc; +} +SQLITE_PRIVATE int sqlite3VdbeMemFromBtree( + BtCursor *pCur, /* Cursor pointing at record to retrieve. */ + u32 offset, /* Offset from the start of data to return bytes from. */ + u32 amt, /* Number of bytes to return. */ + int key, /* If true, retrieve from the btree key, not data. */ + Mem *pMem /* OUT: Return data in this Mem structure. */ +){ + char *zData; /* Data from the btree layer */ + u32 available = 0; /* Number of bytes available on the local btree page */ + int rc = SQLITE_OK; /* Return code */ + + assert( sqlite3BtreeCursorIsValid(pCur) ); + assert( !VdbeMemDynamic(pMem) ); + + /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() + ** that both the BtShared and database handle mutexes are held. */ + assert( (pMem->flags & MEM_RowSet)==0 ); + if( key ){ + zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); + }else{ + zData = (char *)sqlite3BtreeDataFetch(pCur, &available); + } + assert( zData!=0 ); + + if( offset+amt<=available ){ + pMem->z = &zData[offset]; + pMem->flags = MEM_Blob|MEM_Ephem; + pMem->n = (int)amt; + }else{ + rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem); + } + + return rc; +} + +/* +** The pVal argument is known to be a value other than NULL. +** Convert it into a string with encoding enc and return a pointer +** to a zero-terminated version of that string. +*/ +static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ + assert( pVal!=0 ); + assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); + assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); + assert( (pVal->flags & MEM_RowSet)==0 ); + assert( (pVal->flags & (MEM_Null))==0 ); + if( pVal->flags & (MEM_Blob|MEM_Str) ){ + pVal->flags |= MEM_Str; + if( pVal->flags & MEM_Zero ){ + sqlite3VdbeMemExpandBlob(pVal); + } + if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){ + sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); + } + if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){ + assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); + if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ + return 0; + } + } + sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */ + }else{ + sqlite3VdbeMemStringify(pVal, enc, 0); + assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); + } + assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 + || pVal->db->mallocFailed ); + if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ + return pVal->z; + }else{ + return 0; + } +} + +/* This function is only available internally, it is not part of the +** external API. It works in a similar way to sqlite3_value_text(), +** except the data returned is in the encoding specified by the second +** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or +** SQLITE_UTF8. +** +** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. +** If that is the case, then the result must be aligned on an even byte +** boundary. +*/ +SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ + if( !pVal ) return 0; + assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); + assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); + assert( (pVal->flags & MEM_RowSet)==0 ); + if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){ + return pVal->z; + } + if( pVal->flags&MEM_Null ){ + return 0; + } + return valueToText(pVal, enc); +} + +/* +** Create a new sqlite3_value object. +*/ +SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){ + Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); + if( p ){ + p->flags = MEM_Null; + p->db = db; + } + return p; +} + +/* +** Context object passed by sqlite3Stat4ProbeSetValue() through to +** valueNew(). See comments above valueNew() for details. +*/ +struct ValueNewStat4Ctx { + Parse *pParse; + Index *pIdx; + UnpackedRecord **ppRec; + int iVal; +}; + +/* +** Allocate and return a pointer to a new sqlite3_value object. If +** the second argument to this function is NULL, the object is allocated +** by calling sqlite3ValueNew(). +** +** Otherwise, if the second argument is non-zero, then this function is +** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not +** already been allocated, allocate the UnpackedRecord structure that +** that function will return to its caller here. Then return a pointer to +** an sqlite3_value within the UnpackedRecord.a[] array. +*/ +static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( p ){ + UnpackedRecord *pRec = p->ppRec[0]; + + if( pRec==0 ){ + Index *pIdx = p->pIdx; /* Index being probed */ + int nByte; /* Bytes of space to allocate */ + int i; /* Counter variable */ + int nCol = pIdx->nColumn; /* Number of index columns including rowid */ + + nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord)); + pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte); + if( pRec ){ + pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); + if( pRec->pKeyInfo ){ + assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol ); + assert( pRec->pKeyInfo->enc==ENC(db) ); + pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); + for(i=0; iaMem[i].flags = MEM_Null; + pRec->aMem[i].db = db; + } + }else{ + sqlite3DbFree(db, pRec); + pRec = 0; + } + } + if( pRec==0 ) return 0; + p->ppRec[0] = pRec; + } + + pRec->nField = p->iVal+1; + return &pRec->aMem[p->iVal]; + } +#else + UNUSED_PARAMETER(p); +#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ + return sqlite3ValueNew(db); +} + +/* +** The expression object indicated by the second argument is guaranteed +** to be a scalar SQL function. If +** +** * all function arguments are SQL literals, +** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and +** * the SQLITE_FUNC_NEEDCOLL function flag is not set, +** +** then this routine attempts to invoke the SQL function. Assuming no +** error occurs, output parameter (*ppVal) is set to point to a value +** object containing the result before returning SQLITE_OK. +** +** Affinity aff is applied to the result of the function before returning. +** If the result is a text value, the sqlite3_value object uses encoding +** enc. +** +** If the conditions above are not met, this function returns SQLITE_OK +** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to +** NULL and an SQLite error code returned. +*/ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +static int valueFromFunction( + sqlite3 *db, /* The database connection */ + Expr *p, /* The expression to evaluate */ + u8 enc, /* Encoding to use */ + u8 aff, /* Affinity to use */ + sqlite3_value **ppVal, /* Write the new value here */ + struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ +){ + sqlite3_context ctx; /* Context object for function invocation */ + sqlite3_value **apVal = 0; /* Function arguments */ + int nVal = 0; /* Size of apVal[] array */ + FuncDef *pFunc = 0; /* Function definition */ + sqlite3_value *pVal = 0; /* New value */ + int rc = SQLITE_OK; /* Return code */ + ExprList *pList = 0; /* Function arguments */ + int i; /* Iterator variable */ + + assert( pCtx!=0 ); + assert( (p->flags & EP_TokenOnly)==0 ); + pList = p->x.pList; + if( pList ) nVal = pList->nExpr; + pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0); + assert( pFunc ); + if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 + || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) + ){ + return SQLITE_OK; + } + + if( pList ){ + apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal); + if( apVal==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto value_from_function_out; + } + for(i=0; ia[i].pExpr, enc, aff, &apVal[i]); + if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out; + } + } + + pVal = valueNew(db, pCtx); + if( pVal==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto value_from_function_out; + } + + assert( pCtx->pParse->rc==SQLITE_OK ); + memset(&ctx, 0, sizeof(ctx)); + ctx.pOut = pVal; + ctx.pFunc = pFunc; + pFunc->xSFunc(&ctx, nVal, apVal); + if( ctx.isError ){ + rc = ctx.isError; + sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); + }else{ + sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); + assert( rc==SQLITE_OK ); + rc = sqlite3VdbeChangeEncoding(pVal, enc); + if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ + rc = SQLITE_TOOBIG; + pCtx->pParse->nErr++; + } + } + pCtx->pParse->rc = rc; + + value_from_function_out: + if( rc!=SQLITE_OK ){ + pVal = 0; + } + if( apVal ){ + for(i=0; iop)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; + if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; + + /* Compressed expressions only appear when parsing the DEFAULT clause + ** on a table column definition, and hence only when pCtx==0. This + ** check ensures that an EP_TokenOnly expression is never passed down + ** into valueFromFunction(). */ + assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 ); + + if( op==TK_CAST ){ + u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); + rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); + testcase( rc!=SQLITE_OK ); + if( *ppVal ){ + sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8); + sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8); + } + return rc; + } + + /* Handle negative integers in a single step. This is needed in the + ** case when the value is -9223372036854775808. + */ + if( op==TK_UMINUS + && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){ + pExpr = pExpr->pLeft; + op = pExpr->op; + negInt = -1; + zNeg = "-"; + } + + if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ + pVal = valueNew(db, pCtx); + if( pVal==0 ) goto no_mem; + if( ExprHasProperty(pExpr, EP_IntValue) ){ + sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); + }else{ + zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); + if( zVal==0 ) goto no_mem; + sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); + } + if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ + sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); + }else{ + sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); + } + if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; + if( enc!=SQLITE_UTF8 ){ + rc = sqlite3VdbeChangeEncoding(pVal, enc); + } + }else if( op==TK_UMINUS ) { + /* This branch happens for multiple negative signs. Ex: -(-5) */ + if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) + && pVal!=0 + ){ + sqlite3VdbeMemNumerify(pVal); + if( pVal->flags & MEM_Real ){ + pVal->u.r = -pVal->u.r; + }else if( pVal->u.i==SMALLEST_INT64 ){ + pVal->u.r = -(double)SMALLEST_INT64; + MemSetTypeFlag(pVal, MEM_Real); + }else{ + pVal->u.i = -pVal->u.i; + } + sqlite3ValueApplyAffinity(pVal, affinity, enc); + } + }else if( op==TK_NULL ){ + pVal = valueNew(db, pCtx); + if( pVal==0 ) goto no_mem; + } +#ifndef SQLITE_OMIT_BLOB_LITERAL + else if( op==TK_BLOB ){ + int nVal; + assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); + assert( pExpr->u.zToken[1]=='\'' ); + pVal = valueNew(db, pCtx); + if( !pVal ) goto no_mem; + zVal = &pExpr->u.zToken[2]; + nVal = sqlite3Strlen30(zVal)-1; + assert( zVal[nVal]=='\'' ); + sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, + 0, SQLITE_DYNAMIC); + } +#endif + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + else if( op==TK_FUNCTION && pCtx!=0 ){ + rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); + } +#endif + + *ppVal = pVal; + return rc; + +no_mem: + sqlite3OomFault(db); + sqlite3DbFree(db, zVal); + assert( *ppVal==0 ); +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( pCtx==0 ) sqlite3ValueFree(pVal); +#else + assert( pCtx==0 ); sqlite3ValueFree(pVal); +#endif + return SQLITE_NOMEM_BKPT; +} + +/* +** Create a new sqlite3_value object, containing the value of pExpr. +** +** This only works for very simple expressions that consist of one constant +** token (i.e. "5", "5.1", "'a string'"). If the expression can +** be converted directly into a value, then the value is allocated and +** a pointer written to *ppVal. The caller is responsible for deallocating +** the value by passing it to sqlite3ValueFree() later on. If the expression +** cannot be converted to a value, then *ppVal is set to NULL. +*/ +SQLITE_PRIVATE int sqlite3ValueFromExpr( + sqlite3 *db, /* The database connection */ + Expr *pExpr, /* The expression to evaluate */ + u8 enc, /* Encoding to use */ + u8 affinity, /* Affinity to use */ + sqlite3_value **ppVal /* Write the new value here */ +){ + return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0); +} + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** The implementation of the sqlite_record() function. This function accepts +** a single argument of any type. The return value is a formatted database +** record (a blob) containing the argument value. +** +** This is used to convert the value stored in the 'sample' column of the +** sqlite_stat3 table to the record format SQLite uses internally. +*/ +static void recordFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const int file_format = 1; + u32 iSerial; /* Serial type */ + int nSerial; /* Bytes of space for iSerial as varint */ + u32 nVal; /* Bytes of space required for argv[0] */ + int nRet; + sqlite3 *db; + u8 *aRet; + + UNUSED_PARAMETER( argc ); + iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal); + nSerial = sqlite3VarintLen(iSerial); + db = sqlite3_context_db_handle(context); + + nRet = 1 + nSerial + nVal; + aRet = sqlite3DbMallocRawNN(db, nRet); + if( aRet==0 ){ + sqlite3_result_error_nomem(context); + }else{ + aRet[0] = nSerial+1; + putVarint32(&aRet[1], iSerial); + sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial); + sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT); + sqlite3DbFree(db, aRet); + } +} + +/* +** Register built-in functions used to help read ANALYZE data. +*/ +SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){ + static FuncDef aAnalyzeTableFuncs[] = { + FUNCTION(sqlite_record, 1, 0, 0, recordFunc), + }; + sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs)); +} + +/* +** Attempt to extract a value from pExpr and use it to construct *ppVal. +** +** If pAlloc is not NULL, then an UnpackedRecord object is created for +** pAlloc if one does not exist and the new value is added to the +** UnpackedRecord object. +** +** A value is extracted in the following cases: +** +** * (pExpr==0). In this case the value is assumed to be an SQL NULL, +** +** * The expression is a bound variable, and this is a reprepare, or +** +** * The expression is a literal value. +** +** On success, *ppVal is made to point to the extracted value. The caller +** is responsible for ensuring that the value is eventually freed. +*/ +static int stat4ValueFromExpr( + Parse *pParse, /* Parse context */ + Expr *pExpr, /* The expression to extract a value from */ + u8 affinity, /* Affinity to use */ + struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */ + sqlite3_value **ppVal /* OUT: New value object (or NULL) */ +){ + int rc = SQLITE_OK; + sqlite3_value *pVal = 0; + sqlite3 *db = pParse->db; + + /* Skip over any TK_COLLATE nodes */ + pExpr = sqlite3ExprSkipCollate(pExpr); + + if( !pExpr ){ + pVal = valueNew(db, pAlloc); + if( pVal ){ + sqlite3VdbeMemSetNull((Mem*)pVal); + } + }else if( pExpr->op==TK_VARIABLE + || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) + ){ + Vdbe *v; + int iBindVar = pExpr->iColumn; + sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar); + if( (v = pParse->pReprepare)!=0 ){ + pVal = valueNew(db, pAlloc); + if( pVal ){ + rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); + if( rc==SQLITE_OK ){ + sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); + } + pVal->db = pParse->db; + } + } + }else{ + rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc); + } + + assert( pVal==0 || pVal->db==db ); + *ppVal = pVal; + return rc; +} + +/* +** This function is used to allocate and populate UnpackedRecord +** structures intended to be compared against sample index keys stored +** in the sqlite_stat4 table. +** +** A single call to this function attempts to populates field iVal (leftmost +** is 0 etc.) of the unpacked record with a value extracted from expression +** pExpr. Extraction of values is possible if: +** +** * (pExpr==0). In this case the value is assumed to be an SQL NULL, +** +** * The expression is a bound variable, and this is a reprepare, or +** +** * The sqlite3ValueFromExpr() function is able to extract a value +** from the expression (i.e. the expression is a literal value). +** +** If a value can be extracted, the affinity passed as the 5th argument +** is applied to it before it is copied into the UnpackedRecord. Output +** parameter *pbOk is set to true if a value is extracted, or false +** otherwise. +** +** When this function is called, *ppRec must either point to an object +** allocated by an earlier call to this function, or must be NULL. If it +** is NULL and a value can be successfully extracted, a new UnpackedRecord +** is allocated (and *ppRec set to point to it) before returning. +** +** Unless an error is encountered, SQLITE_OK is returned. It is not an +** error if a value cannot be extracted from pExpr. If an error does +** occur, an SQLite error code is returned. +*/ +SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue( + Parse *pParse, /* Parse context */ + Index *pIdx, /* Index being probed */ + UnpackedRecord **ppRec, /* IN/OUT: Probe record */ + Expr *pExpr, /* The expression to extract a value from */ + u8 affinity, /* Affinity to use */ + int iVal, /* Array element to populate */ + int *pbOk /* OUT: True if value was extracted */ +){ + int rc; + sqlite3_value *pVal = 0; + struct ValueNewStat4Ctx alloc; + + alloc.pParse = pParse; + alloc.pIdx = pIdx; + alloc.ppRec = ppRec; + alloc.iVal = iVal; + + rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal); + assert( pVal==0 || pVal->db==pParse->db ); + *pbOk = (pVal!=0); + return rc; +} + +/* +** Attempt to extract a value from expression pExpr using the methods +** as described for sqlite3Stat4ProbeSetValue() above. +** +** If successful, set *ppVal to point to a new value object and return +** SQLITE_OK. If no value can be extracted, but no other error occurs +** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error +** does occur, return an SQLite error code. The final value of *ppVal +** is undefined in this case. +*/ +SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr( + Parse *pParse, /* Parse context */ + Expr *pExpr, /* The expression to extract a value from */ + u8 affinity, /* Affinity to use */ + sqlite3_value **ppVal /* OUT: New value object (or NULL) */ +){ + return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal); +} + +/* +** Extract the iCol-th column from the nRec-byte record in pRec. Write +** the column value into *ppVal. If *ppVal is initially NULL then a new +** sqlite3_value object is allocated. +** +** If *ppVal is initially NULL then the caller is responsible for +** ensuring that the value written into *ppVal is eventually freed. +*/ +SQLITE_PRIVATE int sqlite3Stat4Column( + sqlite3 *db, /* Database handle */ + const void *pRec, /* Pointer to buffer containing record */ + int nRec, /* Size of buffer pRec in bytes */ + int iCol, /* Column to extract */ + sqlite3_value **ppVal /* OUT: Extracted value */ +){ + u32 t; /* a column type code */ + int nHdr; /* Size of the header in the record */ + int iHdr; /* Next unread header byte */ + int iField; /* Next unread data byte */ + int szField; /* Size of the current data field */ + int i; /* Column index */ + u8 *a = (u8*)pRec; /* Typecast byte array */ + Mem *pMem = *ppVal; /* Write result into this Mem object */ + + assert( iCol>0 ); + iHdr = getVarint32(a, nHdr); + if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT; + iField = nHdr; + for(i=0; i<=iCol; i++){ + iHdr += getVarint32(&a[iHdr], t); + testcase( iHdr==nHdr ); + testcase( iHdr==nHdr+1 ); + if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT; + szField = sqlite3VdbeSerialTypeLen(t); + iField += szField; + } + testcase( iField==nRec ); + testcase( iField==nRec+1 ); + if( iField>nRec ) return SQLITE_CORRUPT_BKPT; + if( pMem==0 ){ + pMem = *ppVal = sqlite3ValueNew(db); + if( pMem==0 ) return SQLITE_NOMEM_BKPT; + } + sqlite3VdbeSerialGet(&a[iField-szField], t, pMem); + pMem->enc = ENC(db); + return SQLITE_OK; +} + +/* +** Unless it is NULL, the argument must be an UnpackedRecord object returned +** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes +** the object. +*/ +SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){ + if( pRec ){ + int i; + int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField; + Mem *aMem = pRec->aMem; + sqlite3 *db = aMem[0].db; + for(i=0; ipKeyInfo); + sqlite3DbFree(db, pRec); + } +} +#endif /* ifdef SQLITE_ENABLE_STAT4 */ + +/* +** Change the string value of an sqlite3_value object +*/ +SQLITE_PRIVATE void sqlite3ValueSetStr( + sqlite3_value *v, /* Value to be set */ + int n, /* Length of string z */ + const void *z, /* Text of the new string */ + u8 enc, /* Encoding to use */ + void (*xDel)(void*) /* Destructor for the string */ +){ + if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); +} + +/* +** Free an sqlite3_value object +*/ +SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){ + if( !v ) return; + sqlite3VdbeMemRelease((Mem *)v); + sqlite3DbFree(((Mem*)v)->db, v); +} + +/* +** The sqlite3ValueBytes() routine returns the number of bytes in the +** sqlite3_value object assuming that it uses the encoding "enc". +** The valueBytes() routine is a helper function. +*/ +static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){ + return valueToText(pVal, enc)!=0 ? pVal->n : 0; +} +SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ + Mem *p = (Mem*)pVal; + assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 ); + if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){ + return p->n; + } + if( (p->flags & MEM_Blob)!=0 ){ + if( p->flags & MEM_Zero ){ + return p->n + p->u.nZero; + }else{ + return p->n; + } + } + if( p->flags & MEM_Null ) return 0; + return valueBytes(pVal, enc); +} + +/************** End of vdbemem.c *********************************************/ +/************** Begin file vdbeaux.c *****************************************/ +/* +** 2003 September 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used for creating, destroying, and populating +** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +/* +** Create a new virtual database engine. +*/ +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){ + sqlite3 *db = pParse->db; + Vdbe *p; + p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); + if( p==0 ) return 0; + p->db = db; + if( db->pVdbe ){ + db->pVdbe->pPrev = p; + } + p->pNext = db->pVdbe; + p->pPrev = 0; + db->pVdbe = p; + p->magic = VDBE_MAGIC_INIT; + p->pParse = pParse; + assert( pParse->aLabel==0 ); + assert( pParse->nLabel==0 ); + assert( pParse->nOpAlloc==0 ); + assert( pParse->szOpAlloc==0 ); + return p; +} + +/* +** Change the error string stored in Vdbe.zErrMsg +*/ +SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){ + va_list ap; + sqlite3DbFree(p->db, p->zErrMsg); + va_start(ap, zFormat); + p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap); + va_end(ap); +} + +/* +** Remember the SQL string for a prepared statement. +*/ +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ + assert( isPrepareV2==1 || isPrepareV2==0 ); + if( p==0 ) return; +#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) + if( !isPrepareV2 ) return; +#endif + assert( p->zSql==0 ); + p->zSql = sqlite3DbStrNDup(p->db, z, n); + p->isPrepareV2 = (u8)isPrepareV2; +} + +/* +** Return the SQL associated with a prepared statement +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe *)pStmt; + return p ? p->zSql : 0; +} + +/* +** Swap all content between two VDBE structures. +*/ +SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ + Vdbe tmp, *pTmp; + char *zTmp; + assert( pA->db==pB->db ); + tmp = *pA; + *pA = *pB; + *pB = tmp; + pTmp = pA->pNext; + pA->pNext = pB->pNext; + pB->pNext = pTmp; + pTmp = pA->pPrev; + pA->pPrev = pB->pPrev; + pB->pPrev = pTmp; + zTmp = pA->zSql; + pA->zSql = pB->zSql; + pB->zSql = zTmp; + pB->isPrepareV2 = pA->isPrepareV2; +} + +/* +** Resize the Vdbe.aOp array so that it is at least nOp elements larger +** than its current size. nOp is guaranteed to be less than or equal +** to 1024/sizeof(Op). +** +** If an out-of-memory error occurs while resizing the array, return +** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain +** unchanged (this is so that any opcodes already allocated can be +** correctly deallocated along with the rest of the Vdbe). +*/ +static int growOpArray(Vdbe *v, int nOp){ + VdbeOp *pNew; + Parse *p = v->pParse; + + /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force + ** more frequent reallocs and hence provide more opportunities for + ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used + ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array + ** by the minimum* amount required until the size reaches 512. Normal + ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current + ** size of the op array or add 1KB of space, whichever is smaller. */ +#ifdef SQLITE_TEST_REALLOC_STRESS + int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); +#else + int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); + UNUSED_PARAMETER(nOp); +#endif + + assert( nOp<=(1024/sizeof(Op)) ); + assert( nNew>=(p->nOpAlloc+nOp) ); + pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); + if( pNew ){ + p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); + p->nOpAlloc = p->szOpAlloc/sizeof(Op); + v->aOp = pNew; + } + return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT); +} + +#ifdef SQLITE_DEBUG +/* This routine is just a convenient place to set a breakpoint that will +** fire after each opcode is inserted and displayed using +** "PRAGMA vdbe_addoptrace=on". +*/ +static void test_addop_breakpoint(void){ + static int n = 0; + n++; +} +#endif + +/* +** Add a new instruction to the list of instructions current in the +** VDBE. Return the address of the new instruction. +** +** Parameters: +** +** p Pointer to the VDBE +** +** op The opcode for this instruction +** +** p1, p2, p3 Operands +** +** Use the sqlite3VdbeResolveLabel() function to fix an address and +** the sqlite3VdbeChangeP4() function to change the value of the P4 +** operand. +*/ +static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){ + assert( p->pParse->nOpAlloc<=p->nOp ); + if( growOpArray(p, 1) ) return 1; + assert( p->pParse->nOpAlloc>p->nOp ); + return sqlite3VdbeAddOp3(p, op, p1, p2, p3); +} +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ + int i; + VdbeOp *pOp; + + i = p->nOp; + assert( p->magic==VDBE_MAGIC_INIT ); + assert( op>=0 && op<0xff ); + if( p->pParse->nOpAlloc<=i ){ + return growOp3(p, op, p1, p2, p3); + } + p->nOp++; + pOp = &p->aOp[i]; + pOp->opcode = (u8)op; + pOp->p5 = 0; + pOp->p1 = p1; + pOp->p2 = p2; + pOp->p3 = p3; + pOp->p4.p = 0; + pOp->p4type = P4_NOTUSED; +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + pOp->zComment = 0; +#endif +#ifdef SQLITE_DEBUG + if( p->db->flags & SQLITE_VdbeAddopTrace ){ + int jj, kk; + Parse *pParse = p->pParse; + for(jj=kk=0; jjaColCache + jj; + if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; + printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); + kk++; + } + if( kk ) printf("\n"); + sqlite3VdbePrintOp(0, i, &p->aOp[i]); + test_addop_breakpoint(); + } +#endif +#ifdef VDBE_PROFILE + pOp->cycles = 0; + pOp->cnt = 0; +#endif +#ifdef SQLITE_VDBE_COVERAGE + pOp->iSrcLine = 0; +#endif + return i; +} +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){ + return sqlite3VdbeAddOp3(p, op, 0, 0, 0); +} +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ + return sqlite3VdbeAddOp3(p, op, p1, 0, 0); +} +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ + return sqlite3VdbeAddOp3(p, op, p1, p2, 0); +} + +/* Generate code for an unconditional jump to instruction iDest +*/ +SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){ + return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0); +} + +/* Generate code to cause the string zStr to be loaded into +** register iDest +*/ +SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){ + return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0); +} + +/* +** Generate code that initializes multiple registers to string or integer +** constants. The registers begin with iDest and increase consecutively. +** One register is initialized for each characgter in zTypes[]. For each +** "s" character in zTypes[], the register is a string if the argument is +** not NULL, or OP_Null if the value is a null pointer. For each "i" character +** in zTypes[], the register is initialized to an integer. +*/ +SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ + va_list ap; + int i; + char c; + va_start(ap, zTypes); + for(i=0; (c = zTypes[i])!=0; i++){ + if( c=='s' ){ + const char *z = va_arg(ap, const char*); + sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0); + }else{ + assert( c=='i' ); + sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++); + } + } + va_end(ap); +} + +/* +** Add an opcode that includes the p4 value as a pointer. +*/ +SQLITE_PRIVATE int sqlite3VdbeAddOp4( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + const char *zP4, /* The P4 operand */ + int p4type /* P4 operand type */ +){ + int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); + sqlite3VdbeChangeP4(p, addr, zP4, p4type); + return addr; +} + +/* +** Add an opcode that includes the p4 value with a P4_INT64 or +** P4_REAL type. +*/ +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + const u8 *zP4, /* The P4 operand */ + int p4type /* P4 operand type */ +){ + char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8); + if( p4copy ) memcpy(p4copy, zP4, 8); + return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); +} + +/* +** Add an OP_ParseSchema opcode. This routine is broken out from +** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees +** as having been used. +** +** The zWhere string must have been obtained from sqlite3_malloc(). +** This routine will take ownership of the allocated memory. +*/ +SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ + int j; + sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); + for(j=0; jdb->nDb; j++) sqlite3VdbeUsesBtree(p, j); +} + +/* +** Add an opcode that includes the p4 value as an integer. +*/ +SQLITE_PRIVATE int sqlite3VdbeAddOp4Int( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + int p4 /* The P4 operand as an integer */ +){ + int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); + sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); + return addr; +} + +/* Insert the end of a co-routine +*/ +SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){ + sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); + + /* Clear the temporary register cache, thereby ensuring that each + ** co-routine has its own independent set of registers, because co-routines + ** might expect their registers to be preserved across an OP_Yield, and + ** that could cause problems if two or more co-routines are using the same + ** temporary register. + */ + v->pParse->nTempReg = 0; + v->pParse->nRangeReg = 0; +} + +/* +** Create a new symbolic label for an instruction that has yet to be +** coded. The symbolic label is really just a negative number. The +** label can be used as the P2 value of an operation. Later, when +** the label is resolved to a specific address, the VDBE will scan +** through its operation list and change all values of P2 which match +** the label into the resolved address. +** +** The VDBE knows that a P2 value is a label because labels are +** always negative and P2 values are suppose to be non-negative. +** Hence, a negative P2 value is a label that has yet to be resolved. +** +** Zero is returned if a malloc() fails. +*/ +SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){ + Parse *p = v->pParse; + int i = p->nLabel++; + assert( v->magic==VDBE_MAGIC_INIT ); + if( (i & (i-1))==0 ){ + p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, + (i*2+1)*sizeof(p->aLabel[0])); + } + if( p->aLabel ){ + p->aLabel[i] = -1; + } + return ADDR(i); +} + +/* +** Resolve label "x" to be the address of the next instruction to +** be inserted. The parameter "x" must have been obtained from +** a prior call to sqlite3VdbeMakeLabel(). +*/ +SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){ + Parse *p = v->pParse; + int j = ADDR(x); + assert( v->magic==VDBE_MAGIC_INIT ); + assert( jnLabel ); + assert( j>=0 ); + if( p->aLabel ){ + p->aLabel[j] = v->nOp; + } + p->iFixedOp = v->nOp - 1; +} + +/* +** Mark the VDBE as one that can only be run one time. +*/ +SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){ + p->runOnlyOnce = 1; +} + +/* +** Mark the VDBE as one that can only be run multiple times. +*/ +SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){ + p->runOnlyOnce = 0; +} + +#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ + +/* +** The following type and function are used to iterate through all opcodes +** in a Vdbe main program and each of the sub-programs (triggers) it may +** invoke directly or indirectly. It should be used as follows: +** +** Op *pOp; +** VdbeOpIter sIter; +** +** memset(&sIter, 0, sizeof(sIter)); +** sIter.v = v; // v is of type Vdbe* +** while( (pOp = opIterNext(&sIter)) ){ +** // Do something with pOp +** } +** sqlite3DbFree(v->db, sIter.apSub); +** +*/ +typedef struct VdbeOpIter VdbeOpIter; +struct VdbeOpIter { + Vdbe *v; /* Vdbe to iterate through the opcodes of */ + SubProgram **apSub; /* Array of subprograms */ + int nSub; /* Number of entries in apSub */ + int iAddr; /* Address of next instruction to return */ + int iSub; /* 0 = main program, 1 = first sub-program etc. */ +}; +static Op *opIterNext(VdbeOpIter *p){ + Vdbe *v = p->v; + Op *pRet = 0; + Op *aOp; + int nOp; + + if( p->iSub<=p->nSub ){ + + if( p->iSub==0 ){ + aOp = v->aOp; + nOp = v->nOp; + }else{ + aOp = p->apSub[p->iSub-1]->aOp; + nOp = p->apSub[p->iSub-1]->nOp; + } + assert( p->iAddriAddr]; + p->iAddr++; + if( p->iAddr==nOp ){ + p->iSub++; + p->iAddr = 0; + } + + if( pRet->p4type==P4_SUBPROGRAM ){ + int nByte = (p->nSub+1)*sizeof(SubProgram*); + int j; + for(j=0; jnSub; j++){ + if( p->apSub[j]==pRet->p4.pProgram ) break; + } + if( j==p->nSub ){ + p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte); + if( !p->apSub ){ + pRet = 0; + }else{ + p->apSub[p->nSub++] = pRet->p4.pProgram; + } + } + } + } + + return pRet; +} + +/* +** Check if the program stored in the VM associated with pParse may +** throw an ABORT exception (causing the statement, but not entire transaction +** to be rolled back). This condition is true if the main program or any +** sub-programs contains any of the following: +** +** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. +** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. +** * OP_Destroy +** * OP_VUpdate +** * OP_VRename +** * OP_FkCounter with P2==0 (immediate foreign key constraint) +** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...) +** +** Then check that the value of Parse.mayAbort is true if an +** ABORT may be thrown, or false otherwise. Return true if it does +** match, or false otherwise. This function is intended to be used as +** part of an assert statement in the compiler. Similar to: +** +** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); +*/ +SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ + int hasAbort = 0; + int hasFkCounter = 0; + int hasCreateTable = 0; + int hasInitCoroutine = 0; + Op *pOp; + VdbeOpIter sIter; + memset(&sIter, 0, sizeof(sIter)); + sIter.v = v; + + while( (pOp = opIterNext(&sIter))!=0 ){ + int opcode = pOp->opcode; + if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename + || ((opcode==OP_Halt || opcode==OP_HaltIfNull) + && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) + ){ + hasAbort = 1; + break; + } + if( opcode==OP_CreateTable ) hasCreateTable = 1; + if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1; +#ifndef SQLITE_OMIT_FOREIGN_KEY + if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){ + hasFkCounter = 1; + } +#endif + } + sqlite3DbFree(v->db, sIter.apSub); + + /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. + ** If malloc failed, then the while() loop above may not have iterated + ** through all opcodes and hasAbort may be set incorrectly. Return + ** true for this case to prevent the assert() in the callers frame + ** from failing. */ + return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter + || (hasCreateTable && hasInitCoroutine) ); +} +#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ + +/* +** This routine is called after all opcodes have been inserted. It loops +** through all the opcodes and fixes up some details. +** +** (1) For each jump instruction with a negative P2 value (a label) +** resolve the P2 value to an actual address. +** +** (2) Compute the maximum number of arguments used by any SQL function +** and store that value in *pMaxFuncArgs. +** +** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately +** indicate what the prepared statement actually does. +** +** (4) Initialize the p4.xAdvance pointer on opcodes that use it. +** +** (5) Reclaim the memory allocated for storing labels. +** +** This routine will only function correctly if the mkopcodeh.tcl generator +** script numbers the opcodes correctly. Changes to this routine must be +** coordinated with changes to mkopcodeh.tcl. +*/ +static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ + int nMaxArgs = *pMaxFuncArgs; + Op *pOp; + Parse *pParse = p->pParse; + int *aLabel = pParse->aLabel; + p->readOnly = 1; + p->bIsReader = 0; + pOp = &p->aOp[p->nOp-1]; + while(1){ + + /* Only JUMP opcodes and the short list of special opcodes in the switch + ** below need to be considered. The mkopcodeh.tcl generator script groups + ** all these opcodes together near the front of the opcode list. Skip + ** any opcode that does not need processing by virtual of the fact that + ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. + */ + if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){ + /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing + ** cases from this switch! */ + switch( pOp->opcode ){ + case OP_Transaction: { + if( pOp->p2!=0 ) p->readOnly = 0; + /* fall thru */ + } + case OP_AutoCommit: + case OP_Savepoint: { + p->bIsReader = 1; + break; + } +#ifndef SQLITE_OMIT_WAL + case OP_Checkpoint: +#endif + case OP_Vacuum: + case OP_JournalMode: { + p->readOnly = 0; + p->bIsReader = 1; + break; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + case OP_VUpdate: { + if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; + break; + } + case OP_VFilter: { + int n; + assert( (pOp - p->aOp) >= 3 ); + assert( pOp[-1].opcode==OP_Integer ); + n = pOp[-1].p1; + if( n>nMaxArgs ) nMaxArgs = n; + break; + } +#endif + case OP_Next: + case OP_NextIfOpen: + case OP_SorterNext: { + pOp->p4.xAdvance = sqlite3BtreeNext; + pOp->p4type = P4_ADVANCE; + break; + } + case OP_Prev: + case OP_PrevIfOpen: { + pOp->p4.xAdvance = sqlite3BtreePrevious; + pOp->p4type = P4_ADVANCE; + break; + } + } + if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){ + assert( ADDR(pOp->p2)nLabel ); + pOp->p2 = aLabel[ADDR(pOp->p2)]; + } + } + if( pOp==p->aOp ) break; + pOp--; + } + sqlite3DbFree(p->db, pParse->aLabel); + pParse->aLabel = 0; + pParse->nLabel = 0; + *pMaxFuncArgs = nMaxArgs; + assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); +} + +/* +** Return the address of the next instruction to be inserted. +*/ +SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){ + assert( p->magic==VDBE_MAGIC_INIT ); + return p->nOp; +} + +/* +** Verify that at least N opcode slots are available in p without +** having to malloc for more space (except when compiled using +** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing +** to verify that certain calls to sqlite3VdbeAddOpList() can never +** fail due to a OOM fault and hence that the return value from +** sqlite3VdbeAddOpList() will always be non-NULL. +*/ +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) +SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){ + assert( p->nOp + N <= p->pParse->nOpAlloc ); +} +#endif + +/* +** This function returns a pointer to the array of opcodes associated with +** the Vdbe passed as the first argument. It is the callers responsibility +** to arrange for the returned array to be eventually freed using the +** vdbeFreeOpArray() function. +** +** Before returning, *pnOp is set to the number of entries in the returned +** array. Also, *pnMaxArg is set to the larger of its current value and +** the number of entries in the Vdbe.apArg[] array required to execute the +** returned program. +*/ +SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ + VdbeOp *aOp = p->aOp; + assert( aOp && !p->db->mallocFailed ); + + /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ + assert( DbMaskAllZero(p->btreeMask) ); + + resolveP2Values(p, pnMaxArg); + *pnOp = p->nOp; + p->aOp = 0; + return aOp; +} + +/* +** Add a whole list of operations to the operation stack. Return a +** pointer to the first operation inserted. +** +** Non-zero P2 arguments to jump instructions are automatically adjusted +** so that the jump target is relative to the first operation inserted. +*/ +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList( + Vdbe *p, /* Add opcodes to the prepared statement */ + int nOp, /* Number of opcodes to add */ + VdbeOpList const *aOp, /* The opcodes to be added */ + int iLineno /* Source-file line number of first opcode */ +){ + int i; + VdbeOp *pOut, *pFirst; + assert( nOp>0 ); + assert( p->magic==VDBE_MAGIC_INIT ); + if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ + return 0; + } + pFirst = pOut = &p->aOp[p->nOp]; + for(i=0; iopcode = aOp->opcode; + pOut->p1 = aOp->p1; + pOut->p2 = aOp->p2; + assert( aOp->p2>=0 ); + if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){ + pOut->p2 += p->nOp; + } + pOut->p3 = aOp->p3; + pOut->p4type = P4_NOTUSED; + pOut->p4.p = 0; + pOut->p5 = 0; +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + pOut->zComment = 0; +#endif +#ifdef SQLITE_VDBE_COVERAGE + pOut->iSrcLine = iLineno+i; +#else + (void)iLineno; +#endif +#ifdef SQLITE_DEBUG + if( p->db->flags & SQLITE_VdbeAddopTrace ){ + sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]); + } +#endif + } + p->nOp += nOp; + return pFirst; +} + +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) +/* +** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). +*/ +SQLITE_PRIVATE void sqlite3VdbeScanStatus( + Vdbe *p, /* VM to add scanstatus() to */ + int addrExplain, /* Address of OP_Explain (or 0) */ + int addrLoop, /* Address of loop counter */ + int addrVisit, /* Address of rows visited counter */ + LogEst nEst, /* Estimated number of output rows */ + const char *zName /* Name of table or index being scanned */ +){ + int nByte = (p->nScan+1) * sizeof(ScanStatus); + ScanStatus *aNew; + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); + if( aNew ){ + ScanStatus *pNew = &aNew[p->nScan++]; + pNew->addrExplain = addrExplain; + pNew->addrLoop = addrLoop; + pNew->addrVisit = addrVisit; + pNew->nEst = nEst; + pNew->zName = sqlite3DbStrDup(p->db, zName); + p->aScan = aNew; + } +} +#endif + + +/* +** Change the value of the opcode, or P1, P2, P3, or P5 operands +** for a specific instruction. +*/ +SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){ + sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode; +} +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ + sqlite3VdbeGetOp(p,addr)->p1 = val; +} +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ + sqlite3VdbeGetOp(p,addr)->p2 = val; +} +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ + sqlite3VdbeGetOp(p,addr)->p3 = val; +} +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ + if( !p->db->mallocFailed ) p->aOp[p->nOp-1].p5 = p5; +} + +/* +** Change the P2 operand of instruction addr so that it points to +** the address of the next instruction to be coded. +*/ +SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){ + p->pParse->iFixedOp = p->nOp - 1; + sqlite3VdbeChangeP2(p, addr, p->nOp); +} + + +/* +** If the input FuncDef structure is ephemeral, then free it. If +** the FuncDef is not ephermal, then do nothing. +*/ +static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ + if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ + sqlite3DbFree(db, pDef); + } +} + +static void vdbeFreeOpArray(sqlite3 *, Op *, int); + +/* +** Delete a P4 value if necessary. +*/ +static void freeP4(sqlite3 *db, int p4type, void *p4){ + assert( db ); + switch( p4type ){ + case P4_FUNCCTX: { + freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc); + /* Fall through into the next case */ + } + case P4_REAL: + case P4_INT64: + case P4_DYNAMIC: + case P4_INTARRAY: { + sqlite3DbFree(db, p4); + break; + } + case P4_KEYINFO: { + if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); + break; + } +#ifdef SQLITE_ENABLE_CURSOR_HINTS + case P4_EXPR: { + sqlite3ExprDelete(db, (Expr*)p4); + break; + } +#endif + case P4_MPRINTF: { + if( db->pnBytesFreed==0 ) sqlite3_free(p4); + break; + } + case P4_FUNCDEF: { + freeEphemeralFunction(db, (FuncDef*)p4); + break; + } + case P4_MEM: { + if( db->pnBytesFreed==0 ){ + sqlite3ValueFree((sqlite3_value*)p4); + }else{ + Mem *p = (Mem*)p4; + if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); + sqlite3DbFree(db, p); + } + break; + } + case P4_VTAB : { + if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); + break; + } + } +} + +/* +** Free the space allocated for aOp and any p4 values allocated for the +** opcodes contained within. If aOp is not NULL it is assumed to contain +** nOp entries. +*/ +static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ + if( aOp ){ + Op *pOp; + for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ + if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p); +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + sqlite3DbFree(db, pOp->zComment); +#endif + } + } + sqlite3DbFree(db, aOp); +} + +/* +** Link the SubProgram object passed as the second argument into the linked +** list at Vdbe.pSubProgram. This list is used to delete all sub-program +** objects when the VM is no longer required. +*/ +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ + p->pNext = pVdbe->pProgram; + pVdbe->pProgram = p; +} + +/* +** Change the opcode at addr into OP_Noop +*/ +SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ + VdbeOp *pOp; + if( p->db->mallocFailed ) return 0; + assert( addr>=0 && addrnOp ); + pOp = &p->aOp[addr]; + freeP4(p->db, pOp->p4type, pOp->p4.p); + pOp->p4type = P4_NOTUSED; + pOp->p4.z = 0; + pOp->opcode = OP_Noop; + return 1; +} + +/* +** If the last opcode is "op" and it is not a jump destination, +** then remove it. Return true if and only if an opcode was removed. +*/ +SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ + if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ + return sqlite3VdbeChangeToNoop(p, p->nOp-1); + }else{ + return 0; + } +} + +/* +** Change the value of the P4 operand for a specific instruction. +** This routine is useful when a large program is loaded from a +** static array using sqlite3VdbeAddOpList but we want to make a +** few minor changes to the program. +** +** If n>=0 then the P4 operand is dynamic, meaning that a copy of +** the string is made into memory obtained from sqlite3_malloc(). +** A value of n==0 means copy bytes of zP4 up to and including the +** first null byte. If n>0 then copy n+1 bytes of zP4. +** +** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points +** to a string or structure that is guaranteed to exist for the lifetime of +** the Vdbe. In these cases we can just copy the pointer. +** +** If addr<0 then change P4 on the most recently inserted instruction. +*/ +static void SQLITE_NOINLINE vdbeChangeP4Full( + Vdbe *p, + Op *pOp, + const char *zP4, + int n +){ + if( pOp->p4type ){ + freeP4(p->db, pOp->p4type, pOp->p4.p); + pOp->p4type = 0; + pOp->p4.p = 0; + } + if( n<0 ){ + sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n); + }else{ + if( n==0 ) n = sqlite3Strlen30(zP4); + pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); + pOp->p4type = P4_DYNAMIC; + } +} +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ + Op *pOp; + sqlite3 *db; + assert( p!=0 ); + db = p->db; + assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->aOp!=0 || db->mallocFailed ); + if( db->mallocFailed ){ + if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4); + return; + } + assert( p->nOp>0 ); + assert( addrnOp ); + if( addr<0 ){ + addr = p->nOp - 1; + } + pOp = &p->aOp[addr]; + if( n>=0 || pOp->p4type ){ + vdbeChangeP4Full(p, pOp, zP4, n); + return; + } + if( n==P4_INT32 ){ + /* Note: this cast is safe, because the origin data point was an int + ** that was cast to a (const char *). */ + pOp->p4.i = SQLITE_PTR_TO_INT(zP4); + pOp->p4type = P4_INT32; + }else if( zP4!=0 ){ + assert( n<0 ); + pOp->p4.p = (void*)zP4; + pOp->p4type = (signed char)n; + if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4); + } +} + +/* +** Set the P4 on the most recently added opcode to the KeyInfo for the +** index given. +*/ +SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ + Vdbe *v = pParse->pVdbe; + assert( v!=0 ); + assert( pIdx!=0 ); + sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), + P4_KEYINFO); +} + +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS +/* +** Change the comment on the most recently coded instruction. Or +** insert a No-op and add the comment to that new instruction. This +** makes the code easier to read during debugging. None of this happens +** in a production build. +*/ +static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ + assert( p->nOp>0 || p->aOp==0 ); + assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); + if( p->nOp ){ + assert( p->aOp ); + sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); + p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); + } +} +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ + va_list ap; + if( p ){ + va_start(ap, zFormat); + vdbeVComment(p, zFormat, ap); + va_end(ap); + } +} +SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ + va_list ap; + if( p ){ + sqlite3VdbeAddOp0(p, OP_Noop); + va_start(ap, zFormat); + vdbeVComment(p, zFormat, ap); + va_end(ap); + } +} +#endif /* NDEBUG */ + +#ifdef SQLITE_VDBE_COVERAGE +/* +** Set the value if the iSrcLine field for the previously coded instruction. +*/ +SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ + sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; +} +#endif /* SQLITE_VDBE_COVERAGE */ + +/* +** Return the opcode for a given address. If the address is -1, then +** return the most recently inserted opcode. +** +** If a memory allocation error has occurred prior to the calling of this +** routine, then a pointer to a dummy VdbeOp will be returned. That opcode +** is readable but not writable, though it is cast to a writable value. +** The return of a dummy opcode allows the call to continue functioning +** after an OOM fault without having to check to see if the return from +** this routine is a valid pointer. But because the dummy.opcode is 0, +** dummy will never be written to. This is verified by code inspection and +** by running with Valgrind. +*/ +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ + /* C89 specifies that the constant "dummy" will be initialized to all + ** zeros, which is correct. MSVC generates a warning, nevertheless. */ + static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ + assert( p->magic==VDBE_MAGIC_INIT ); + if( addr<0 ){ + addr = p->nOp - 1; + } + assert( (addr>=0 && addrnOp) || p->db->mallocFailed ); + if( p->db->mallocFailed ){ + return (VdbeOp*)&dummy; + }else{ + return &p->aOp[addr]; + } +} + +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) +/* +** Return an integer value for one of the parameters to the opcode pOp +** determined by character c. +*/ +static int translateP(char c, const Op *pOp){ + if( c=='1' ) return pOp->p1; + if( c=='2' ) return pOp->p2; + if( c=='3' ) return pOp->p3; + if( c=='4' ) return pOp->p4.i; + return pOp->p5; +} + +/* +** Compute a string for the "comment" field of a VDBE opcode listing. +** +** The Synopsis: field in comments in the vdbe.c source file gets converted +** to an extra string that is appended to the sqlite3OpcodeName(). In the +** absence of other comments, this synopsis becomes the comment on the opcode. +** Some translation occurs: +** +** "PX" -> "r[X]" +** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 +** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 +** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x +*/ +static int displayComment( + const Op *pOp, /* The opcode to be commented */ + const char *zP4, /* Previously obtained value for P4 */ + char *zTemp, /* Write result here */ + int nTemp /* Space available in zTemp[] */ +){ + const char *zOpName; + const char *zSynopsis; + int nOpName; + int ii, jj; + zOpName = sqlite3OpcodeName(pOp->opcode); + nOpName = sqlite3Strlen30(zOpName); + if( zOpName[nOpName+1] ){ + int seenCom = 0; + char c; + zSynopsis = zOpName += nOpName + 1; + for(ii=jj=0; jjzComment); + seenCom = 1; + }else{ + int v1 = translateP(c, pOp); + int v2; + sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); + if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ + ii += 3; + jj += sqlite3Strlen30(zTemp+jj); + v2 = translateP(zSynopsis[ii], pOp); + if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ + ii += 2; + v2++; + } + if( v2>1 ){ + sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); + } + }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ + ii += 4; + } + } + jj += sqlite3Strlen30(zTemp+jj); + }else{ + zTemp[jj++] = c; + } + } + if( !seenCom && jjzComment ){ + sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); + jj += sqlite3Strlen30(zTemp+jj); + } + if( jjzComment ){ + sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); + jj = sqlite3Strlen30(zTemp); + }else{ + zTemp[0] = 0; + jj = 0; + } + return jj; +} +#endif /* SQLITE_DEBUG */ + +#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) +/* +** Translate the P4.pExpr value for an OP_CursorHint opcode into text +** that can be displayed in the P4 column of EXPLAIN output. +*/ +static void displayP4Expr(StrAccum *p, Expr *pExpr){ + const char *zOp = 0; + switch( pExpr->op ){ + case TK_STRING: + sqlite3XPrintf(p, "%Q", pExpr->u.zToken); + break; + case TK_INTEGER: + sqlite3XPrintf(p, "%d", pExpr->u.iValue); + break; + case TK_NULL: + sqlite3XPrintf(p, "NULL"); + break; + case TK_REGISTER: { + sqlite3XPrintf(p, "r[%d]", pExpr->iTable); + break; + } + case TK_COLUMN: { + if( pExpr->iColumn<0 ){ + sqlite3XPrintf(p, "rowid"); + }else{ + sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn); + } + break; + } + case TK_LT: zOp = "LT"; break; + case TK_LE: zOp = "LE"; break; + case TK_GT: zOp = "GT"; break; + case TK_GE: zOp = "GE"; break; + case TK_NE: zOp = "NE"; break; + case TK_EQ: zOp = "EQ"; break; + case TK_IS: zOp = "IS"; break; + case TK_ISNOT: zOp = "ISNOT"; break; + case TK_AND: zOp = "AND"; break; + case TK_OR: zOp = "OR"; break; + case TK_PLUS: zOp = "ADD"; break; + case TK_STAR: zOp = "MUL"; break; + case TK_MINUS: zOp = "SUB"; break; + case TK_REM: zOp = "REM"; break; + case TK_BITAND: zOp = "BITAND"; break; + case TK_BITOR: zOp = "BITOR"; break; + case TK_SLASH: zOp = "DIV"; break; + case TK_LSHIFT: zOp = "LSHIFT"; break; + case TK_RSHIFT: zOp = "RSHIFT"; break; + case TK_CONCAT: zOp = "CONCAT"; break; + case TK_UMINUS: zOp = "MINUS"; break; + case TK_UPLUS: zOp = "PLUS"; break; + case TK_BITNOT: zOp = "BITNOT"; break; + case TK_NOT: zOp = "NOT"; break; + case TK_ISNULL: zOp = "ISNULL"; break; + case TK_NOTNULL: zOp = "NOTNULL"; break; + + default: + sqlite3XPrintf(p, "%s", "expr"); + break; + } + + if( zOp ){ + sqlite3XPrintf(p, "%s(", zOp); + displayP4Expr(p, pExpr->pLeft); + if( pExpr->pRight ){ + sqlite3StrAccumAppend(p, ",", 1); + displayP4Expr(p, pExpr->pRight); + } + sqlite3StrAccumAppend(p, ")", 1); + } +} +#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ + + +#if VDBE_DISPLAY_P4 +/* +** Compute a string that describes the P4 parameter for an opcode. +** Use zTemp for any required temporary buffer space. +*/ +static char *displayP4(Op *pOp, char *zTemp, int nTemp){ + char *zP4 = zTemp; + StrAccum x; + assert( nTemp>=20 ); + sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); + switch( pOp->p4type ){ + case P4_KEYINFO: { + int j; + KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; + assert( pKeyInfo->aSortOrder!=0 ); + sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField); + for(j=0; jnField; j++){ + CollSeq *pColl = pKeyInfo->aColl[j]; + const char *zColl = pColl ? pColl->zName : ""; + if( strcmp(zColl, "BINARY")==0 ) zColl = "B"; + sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl); + } + sqlite3StrAccumAppend(&x, ")", 1); + break; + } +#ifdef SQLITE_ENABLE_CURSOR_HINTS + case P4_EXPR: { + displayP4Expr(&x, pOp->p4.pExpr); + break; + } +#endif + case P4_COLLSEQ: { + CollSeq *pColl = pOp->p4.pColl; + sqlite3XPrintf(&x, "(%.20s)", pColl->zName); + break; + } + case P4_FUNCDEF: { + FuncDef *pDef = pOp->p4.pFunc; + sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); + break; + } +#ifdef SQLITE_DEBUG + case P4_FUNCCTX: { + FuncDef *pDef = pOp->p4.pCtx->pFunc; + sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); + break; + } +#endif + case P4_INT64: { + sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64); + break; + } + case P4_INT32: { + sqlite3XPrintf(&x, "%d", pOp->p4.i); + break; + } + case P4_REAL: { + sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal); + break; + } + case P4_MEM: { + Mem *pMem = pOp->p4.pMem; + if( pMem->flags & MEM_Str ){ + zP4 = pMem->z; + }else if( pMem->flags & MEM_Int ){ + sqlite3XPrintf(&x, "%lld", pMem->u.i); + }else if( pMem->flags & MEM_Real ){ + sqlite3XPrintf(&x, "%.16g", pMem->u.r); + }else if( pMem->flags & MEM_Null ){ + zP4 = "NULL"; + }else{ + assert( pMem->flags & MEM_Blob ); + zP4 = "(blob)"; + } + break; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + case P4_VTAB: { + sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; + sqlite3XPrintf(&x, "vtab:%p", pVtab); + break; + } +#endif + case P4_INTARRAY: { + int i; + int *ai = pOp->p4.ai; + int n = ai[0]; /* The first element of an INTARRAY is always the + ** count of the number of elements to follow */ + for(i=1; ip4.pTab->zName); + break; + } + default: { + zP4 = pOp->p4.z; + if( zP4==0 ){ + zP4 = zTemp; + zTemp[0] = 0; + } + } + } + sqlite3StrAccumFinish(&x); + assert( zP4!=0 ); + return zP4; +} +#endif /* VDBE_DISPLAY_P4 */ + +/* +** Declare to the Vdbe that the BTree object at db->aDb[i] is used. +** +** The prepared statements need to know in advance the complete set of +** attached databases that will be use. A mask of these databases +** is maintained in p->btreeMask. The p->lockMask value is the subset of +** p->btreeMask of databases that will require a lock. +*/ +SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){ + assert( i>=0 && idb->nDb && i<(int)sizeof(yDbMask)*8 ); + assert( i<(int)sizeof(p->btreeMask)*8 ); + DbMaskSet(p->btreeMask, i); + if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ + DbMaskSet(p->lockMask, i); + } +} + +#if !defined(SQLITE_OMIT_SHARED_CACHE) +/* +** If SQLite is compiled to support shared-cache mode and to be threadsafe, +** this routine obtains the mutex associated with each BtShared structure +** that may be accessed by the VM passed as an argument. In doing so it also +** sets the BtShared.db member of each of the BtShared structures, ensuring +** that the correct busy-handler callback is invoked if required. +** +** If SQLite is not threadsafe but does support shared-cache mode, then +** sqlite3BtreeEnter() is invoked to set the BtShared.db variables +** of all of BtShared structures accessible via the database handle +** associated with the VM. +** +** If SQLite is not threadsafe and does not support shared-cache mode, this +** function is a no-op. +** +** The p->btreeMask field is a bitmask of all btrees that the prepared +** statement p will ever use. Let N be the number of bits in p->btreeMask +** corresponding to btrees that use shared cache. Then the runtime of +** this routine is N*N. But as N is rarely more than 1, this should not +** be a problem. +*/ +SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){ + int i; + sqlite3 *db; + Db *aDb; + int nDb; + if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ + db = p->db; + aDb = db->aDb; + nDb = db->nDb; + for(i=0; ilockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ + sqlite3BtreeEnter(aDb[i].pBt); + } + } +} +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 +/* +** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). +*/ +static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){ + int i; + sqlite3 *db; + Db *aDb; + int nDb; + db = p->db; + aDb = db->aDb; + nDb = db->nDb; + for(i=0; ilockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ + sqlite3BtreeLeave(aDb[i].pBt); + } + } +} +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){ + if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ + vdbeLeave(p); +} +#endif + +#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) +/* +** Print a single opcode. This routine is used for debugging only. +*/ +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ + char *zP4; + char zPtr[50]; + char zCom[100]; + static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; + if( pOut==0 ) pOut = stdout; + zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + displayComment(pOp, zP4, zCom, sizeof(zCom)); +#else + zCom[0] = 0; +#endif + /* NB: The sqlite3OpcodeName() function is implemented by code created + ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the + ** information from the vdbe.c source text */ + fprintf(pOut, zFormat1, pc, + sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, + zCom + ); + fflush(pOut); +} +#endif + +/* +** Release an array of N Mem elements +*/ +static void releaseMemArray(Mem *p, int N){ + if( p && N ){ + Mem *pEnd = &p[N]; + sqlite3 *db = p->db; + if( db->pnBytesFreed ){ + do{ + if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); + }while( (++p)flags & MEM_Agg ); + testcase( p->flags & MEM_Dyn ); + testcase( p->flags & MEM_Frame ); + testcase( p->flags & MEM_RowSet ); + if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ + sqlite3VdbeMemRelease(p); + }else if( p->szMalloc ){ + sqlite3DbFree(db, p->zMalloc); + p->szMalloc = 0; + } + + p->flags = MEM_Undefined; + }while( (++p)nChildMem]; + for(i=0; inChildCsr; i++){ + sqlite3VdbeFreeCursor(p->v, apCsr[i]); + } + releaseMemArray(aMem, p->nChildMem); + sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0); + sqlite3DbFree(p->v->db, p); +} + +#ifndef SQLITE_OMIT_EXPLAIN +/* +** Give a listing of the program in the virtual machine. +** +** The interface is the same as sqlite3VdbeExec(). But instead of +** running the code, it invokes the callback once for each instruction. +** This feature is used to implement "EXPLAIN". +** +** When p->explain==1, each instruction is listed. When +** p->explain==2, only OP_Explain instructions are listed and these +** are shown in a different format. p->explain==2 is used to implement +** EXPLAIN QUERY PLAN. +** +** When p->explain==1, first the main program is listed, then each of +** the trigger subprograms are listed one by one. +*/ +SQLITE_PRIVATE int sqlite3VdbeList( + Vdbe *p /* The VDBE */ +){ + int nRow; /* Stop when row count reaches this */ + int nSub = 0; /* Number of sub-vdbes seen so far */ + SubProgram **apSub = 0; /* Array of sub-vdbes */ + Mem *pSub = 0; /* Memory cell hold array of subprogs */ + sqlite3 *db = p->db; /* The database connection */ + int i; /* Loop counter */ + int rc = SQLITE_OK; /* Return code */ + Mem *pMem = &p->aMem[1]; /* First Mem of result set */ + + assert( p->explain ); + assert( p->magic==VDBE_MAGIC_RUN ); + assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); + + /* Even though this opcode does not use dynamic strings for + ** the result, result columns may become dynamic if the user calls + ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. + */ + releaseMemArray(pMem, 8); + p->pResultSet = 0; + + if( p->rc==SQLITE_NOMEM_BKPT ){ + /* This happens if a malloc() inside a call to sqlite3_column_text() or + ** sqlite3_column_text16() failed. */ + sqlite3OomFault(db); + return SQLITE_ERROR; + } + + /* When the number of output rows reaches nRow, that means the + ** listing has finished and sqlite3_step() should return SQLITE_DONE. + ** nRow is the sum of the number of rows in the main program, plus + ** the sum of the number of rows in all trigger subprograms encountered + ** so far. The nRow value will increase as new trigger subprograms are + ** encountered, but p->pc will eventually catch up to nRow. + */ + nRow = p->nOp; + if( p->explain==1 ){ + /* The first 8 memory cells are used for the result set. So we will + ** commandeer the 9th cell to use as storage for an array of pointers + ** to trigger subprograms. The VDBE is guaranteed to have at least 9 + ** cells. */ + assert( p->nMem>9 ); + pSub = &p->aMem[9]; + if( pSub->flags&MEM_Blob ){ + /* On the first call to sqlite3_step(), pSub will hold a NULL. It is + ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ + nSub = pSub->n/sizeof(Vdbe*); + apSub = (SubProgram **)pSub->z; + } + for(i=0; inOp; + } + } + + do{ + i = p->pc++; + }while( iexplain==2 && p->aOp[i].opcode!=OP_Explain ); + if( i>=nRow ){ + p->rc = SQLITE_OK; + rc = SQLITE_DONE; + }else if( db->u1.isInterrupted ){ + p->rc = SQLITE_INTERRUPT; + rc = SQLITE_ERROR; + sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); + }else{ + char *zP4; + Op *pOp; + if( inOp ){ + /* The output line number is small enough that we are still in the + ** main program. */ + pOp = &p->aOp[i]; + }else{ + /* We are currently listing subprograms. Figure out which one and + ** pick up the appropriate opcode. */ + int j; + i -= p->nOp; + for(j=0; i>=apSub[j]->nOp; j++){ + i -= apSub[j]->nOp; + } + pOp = &apSub[j]->aOp[i]; + } + if( p->explain==1 ){ + pMem->flags = MEM_Int; + pMem->u.i = i; /* Program counter */ + pMem++; + + pMem->flags = MEM_Static|MEM_Str|MEM_Term; + pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ + assert( pMem->z!=0 ); + pMem->n = sqlite3Strlen30(pMem->z); + pMem->enc = SQLITE_UTF8; + pMem++; + + /* When an OP_Program opcode is encounter (the only opcode that has + ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms + ** kept in p->aMem[9].z to hold the new program - assuming this subprogram + ** has not already been seen. + */ + if( pOp->p4type==P4_SUBPROGRAM ){ + int nByte = (nSub+1)*sizeof(SubProgram*); + int j; + for(j=0; jp4.pProgram ) break; + } + if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){ + apSub = (SubProgram **)pSub->z; + apSub[nSub++] = pOp->p4.pProgram; + pSub->flags |= MEM_Blob; + pSub->n = nSub*sizeof(SubProgram*); + } + } + } + + pMem->flags = MEM_Int; + pMem->u.i = pOp->p1; /* P1 */ + pMem++; + + pMem->flags = MEM_Int; + pMem->u.i = pOp->p2; /* P2 */ + pMem++; + + pMem->flags = MEM_Int; + pMem->u.i = pOp->p3; /* P3 */ + pMem++; + + if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ + assert( p->db->mallocFailed ); + return SQLITE_ERROR; + } + pMem->flags = MEM_Str|MEM_Term; + zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); + if( zP4!=pMem->z ){ + sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); + }else{ + assert( pMem->z!=0 ); + pMem->n = sqlite3Strlen30(pMem->z); + pMem->enc = SQLITE_UTF8; + } + pMem++; + + if( p->explain==1 ){ + if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ + assert( p->db->mallocFailed ); + return SQLITE_ERROR; + } + pMem->flags = MEM_Str|MEM_Term; + pMem->n = 2; + sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ + pMem->enc = SQLITE_UTF8; + pMem++; + +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ + assert( p->db->mallocFailed ); + return SQLITE_ERROR; + } + pMem->flags = MEM_Str|MEM_Term; + pMem->n = displayComment(pOp, zP4, pMem->z, 500); + pMem->enc = SQLITE_UTF8; +#else + pMem->flags = MEM_Null; /* Comment */ +#endif + } + + p->nResColumn = 8 - 4*(p->explain-1); + p->pResultSet = &p->aMem[1]; + p->rc = SQLITE_OK; + rc = SQLITE_ROW; + } + return rc; +} +#endif /* SQLITE_OMIT_EXPLAIN */ + +#ifdef SQLITE_DEBUG +/* +** Print the SQL that was used to generate a VDBE program. +*/ +SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){ + const char *z = 0; + if( p->zSql ){ + z = p->zSql; + }else if( p->nOp>=1 ){ + const VdbeOp *pOp = &p->aOp[0]; + if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ + z = pOp->p4.z; + while( sqlite3Isspace(*z) ) z++; + } + } + if( z ) printf("SQL: [%s]\n", z); +} +#endif + +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) +/* +** Print an IOTRACE message showing SQL content. +*/ +SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){ + int nOp = p->nOp; + VdbeOp *pOp; + if( sqlite3IoTrace==0 ) return; + if( nOp<1 ) return; + pOp = &p->aOp[0]; + if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ + int i, j; + char z[1000]; + sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); + for(i=0; sqlite3Isspace(z[i]); i++){} + for(j=0; z[i]; i++){ + if( sqlite3Isspace(z[i]) ){ + if( z[i-1]!=' ' ){ + z[j++] = ' '; + } + }else{ + z[j++] = z[i]; + } + } + z[j] = 0; + sqlite3IoTrace("SQL %s\n", z); + } +} +#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ + +/* An instance of this object describes bulk memory available for use +** by subcomponents of a prepared statement. Space is allocated out +** of a ReusableSpace object by the allocSpace() routine below. +*/ +struct ReusableSpace { + u8 *pSpace; /* Available memory */ + int nFree; /* Bytes of available memory */ + int nNeeded; /* Total bytes that could not be allocated */ +}; + +/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf +** from the ReusableSpace object. Return a pointer to the allocated +** memory on success. If insufficient memory is available in the +** ReusableSpace object, increase the ReusableSpace.nNeeded +** value by the amount needed and return NULL. +** +** If pBuf is not initially NULL, that means that the memory has already +** been allocated by a prior call to this routine, so just return a copy +** of pBuf and leave ReusableSpace unchanged. +** +** This allocator is employed to repurpose unused slots at the end of the +** opcode array of prepared state for other memory needs of the prepared +** statement. +*/ +static void *allocSpace( + struct ReusableSpace *p, /* Bulk memory available for allocation */ + void *pBuf, /* Pointer to a prior allocation */ + int nByte /* Bytes of memory needed */ +){ + assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) ); + if( pBuf==0 ){ + nByte = ROUND8(nByte); + if( nByte <= p->nFree ){ + p->nFree -= nByte; + pBuf = &p->pSpace[p->nFree]; + }else{ + p->nNeeded += nByte; + } + } + assert( EIGHT_BYTE_ALIGNMENT(pBuf) ); + return pBuf; +} + +/* +** Rewind the VDBE back to the beginning in preparation for +** running it. +*/ +SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){ +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) + int i; +#endif + assert( p!=0 ); + assert( p->magic==VDBE_MAGIC_INIT ); + + /* There should be at least one opcode. + */ + assert( p->nOp>0 ); + + /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ + p->magic = VDBE_MAGIC_RUN; + +#ifdef SQLITE_DEBUG + for(i=0; inMem; i++){ + assert( p->aMem[i].db==p->db ); + } +#endif + p->pc = -1; + p->rc = SQLITE_OK; + p->errorAction = OE_Abort; + p->nChange = 0; + p->cacheCtr = 1; + p->minWriteFileFormat = 255; + p->iStatement = 0; + p->nFkConstraint = 0; +#ifdef VDBE_PROFILE + for(i=0; inOp; i++){ + p->aOp[i].cnt = 0; + p->aOp[i].cycles = 0; + } +#endif +} + +/* +** Prepare a virtual machine for execution for the first time after +** creating the virtual machine. This involves things such +** as allocating registers and initializing the program counter. +** After the VDBE has be prepped, it can be executed by one or more +** calls to sqlite3VdbeExec(). +** +** This function may be called exactly once on each virtual machine. +** After this routine is called the VM has been "packaged" and is ready +** to run. After this routine is called, further calls to +** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects +** the Vdbe from the Parse object that helped generate it so that the +** the Vdbe becomes an independent entity and the Parse object can be +** destroyed. +** +** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back +** to its initial state after it has been run. +*/ +SQLITE_PRIVATE void sqlite3VdbeMakeReady( + Vdbe *p, /* The VDBE */ + Parse *pParse /* Parsing context */ +){ + sqlite3 *db; /* The database connection */ + int nVar; /* Number of parameters */ + int nMem; /* Number of VM memory registers */ + int nCursor; /* Number of cursors required */ + int nArg; /* Number of arguments in subprograms */ + int nOnce; /* Number of OP_Once instructions */ + int n; /* Loop counter */ + struct ReusableSpace x; /* Reusable bulk memory */ + + assert( p!=0 ); + assert( p->nOp>0 ); + assert( pParse!=0 ); + assert( p->magic==VDBE_MAGIC_INIT ); + assert( pParse==p->pParse ); + db = p->db; + assert( db->mallocFailed==0 ); + nVar = pParse->nVar; + nMem = pParse->nMem; + nCursor = pParse->nTab; + nArg = pParse->nMaxArg; + nOnce = pParse->nOnce; + if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ + + /* Each cursor uses a memory cell. The first cursor (cursor 0) can + ** use aMem[0] which is not otherwise used by the VDBE program. Allocate + ** space at the end of aMem[] for cursors 1 and greater. + ** See also: allocateCursor(). + */ + nMem += nCursor; + if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */ + + /* Figure out how much reusable memory is available at the end of the + ** opcode array. This extra memory will be reallocated for other elements + ** of the prepared statement. + */ + n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */ + x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */ + assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) ); + x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */ + assert( x.nFree>=0 ); + if( x.nFree>0 ){ + memset(x.pSpace, 0, x.nFree); + assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) ); + } + + resolveP2Values(p, &nArg); + p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); + if( pParse->explain && nMem<10 ){ + nMem = 10; + } + p->expired = 0; + + /* Memory for registers, parameters, cursor, etc, is allocated in one or two + ** passes. On the first pass, we try to reuse unused memory at the + ** end of the opcode array. If we are unable to satisfy all memory + ** requirements by reusing the opcode array tail, then the second + ** pass will fill in the remainder using a fresh memory allocation. + ** + ** This two-pass approach that reuses as much memory as possible from + ** the leftover memory at the end of the opcode array. This can significantly + ** reduce the amount of memory held by a prepared statement. + */ + do { + x.nNeeded = 0; + p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem)); + p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); + p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); + p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); + p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce); +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64)); +#endif + if( x.nNeeded==0 ) break; + x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded); + x.nFree = x.nNeeded; + }while( !db->mallocFailed ); + + p->nCursor = nCursor; + p->nOnceFlag = nOnce; + if( p->aVar ){ + p->nVar = (ynVar)nVar; + for(n=0; naVar[n].flags = MEM_Null; + p->aVar[n].db = db; + } + } + p->nzVar = pParse->nzVar; + p->azVar = pParse->azVar; + pParse->nzVar = 0; + pParse->azVar = 0; + if( p->aMem ){ + p->nMem = nMem; + for(n=0; naMem[n].flags = MEM_Undefined; + p->aMem[n].db = db; + } + } + p->explain = pParse->explain; + sqlite3VdbeRewind(p); +} + +/* +** Close a VDBE cursor and release all the resources that cursor +** happens to hold. +*/ +SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ + if( pCx==0 ){ + return; + } + assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE ); + switch( pCx->eCurType ){ + case CURTYPE_SORTER: { + sqlite3VdbeSorterClose(p->db, pCx); + break; + } + case CURTYPE_BTREE: { + if( pCx->pBt ){ + sqlite3BtreeClose(pCx->pBt); + /* The pCx->pCursor will be close automatically, if it exists, by + ** the call above. */ + }else{ + assert( pCx->uc.pCursor!=0 ); + sqlite3BtreeCloseCursor(pCx->uc.pCursor); + } + break; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + case CURTYPE_VTAB: { + sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur; + const sqlite3_module *pModule = pVCur->pVtab->pModule; + assert( pVCur->pVtab->nRef>0 ); + pVCur->pVtab->nRef--; + pModule->xClose(pVCur); + break; + } +#endif + } +} + +/* +** Close all cursors in the current frame. +*/ +static void closeCursorsInFrame(Vdbe *p){ + if( p->apCsr ){ + int i; + for(i=0; inCursor; i++){ + VdbeCursor *pC = p->apCsr[i]; + if( pC ){ + sqlite3VdbeFreeCursor(p, pC); + p->apCsr[i] = 0; + } + } + } +} + +/* +** Copy the values stored in the VdbeFrame structure to its Vdbe. This +** is used, for example, when a trigger sub-program is halted to restore +** control to the main program. +*/ +SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ + Vdbe *v = pFrame->v; + closeCursorsInFrame(v); +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + v->anExec = pFrame->anExec; +#endif + v->aOnceFlag = pFrame->aOnceFlag; + v->nOnceFlag = pFrame->nOnceFlag; + v->aOp = pFrame->aOp; + v->nOp = pFrame->nOp; + v->aMem = pFrame->aMem; + v->nMem = pFrame->nMem; + v->apCsr = pFrame->apCsr; + v->nCursor = pFrame->nCursor; + v->db->lastRowid = pFrame->lastRowid; + v->nChange = pFrame->nChange; + v->db->nChange = pFrame->nDbChange; + sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0); + v->pAuxData = pFrame->pAuxData; + pFrame->pAuxData = 0; + return pFrame->pc; +} + +/* +** Close all cursors. +** +** Also release any dynamic memory held by the VM in the Vdbe.aMem memory +** cell array. This is necessary as the memory cell array may contain +** pointers to VdbeFrame objects, which may in turn contain pointers to +** open cursors. +*/ +static void closeAllCursors(Vdbe *p){ + if( p->pFrame ){ + VdbeFrame *pFrame; + for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); + sqlite3VdbeFrameRestore(pFrame); + p->pFrame = 0; + p->nFrame = 0; + } + assert( p->nFrame==0 ); + closeCursorsInFrame(p); + if( p->aMem ){ + releaseMemArray(p->aMem, p->nMem); + } + while( p->pDelFrame ){ + VdbeFrame *pDel = p->pDelFrame; + p->pDelFrame = pDel->pParent; + sqlite3VdbeFrameDelete(pDel); + } + + /* Delete any auxdata allocations made by the VM */ + if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0); + assert( p->pAuxData==0 ); +} + +/* +** Clean up the VM after a single run. +*/ +static void Cleanup(Vdbe *p){ + sqlite3 *db = p->db; + +#ifdef SQLITE_DEBUG + /* Execute assert() statements to ensure that the Vdbe.apCsr[] and + ** Vdbe.aMem[] arrays have already been cleaned up. */ + int i; + if( p->apCsr ) for(i=0; inCursor; i++) assert( p->apCsr[i]==0 ); + if( p->aMem ){ + for(i=0; inMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); + } +#endif + + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = 0; + p->pResultSet = 0; +} + +/* +** Set the number of result columns that will be returned by this SQL +** statement. This is now set at compile time, rather than during +** execution of the vdbe program so that sqlite3_column_count() can +** be called on an SQL statement before sqlite3_step(). +*/ +SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ + Mem *pColName; + int n; + sqlite3 *db = p->db; + + releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); + sqlite3DbFree(db, p->aColName); + n = nResColumn*COLNAME_N; + p->nResColumn = (u16)nResColumn; + p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); + if( p->aColName==0 ) return; + while( n-- > 0 ){ + pColName->flags = MEM_Null; + pColName->db = p->db; + pColName++; + } +} + +/* +** Set the name of the idx'th column to be returned by the SQL statement. +** zName must be a pointer to a nul terminated string. +** +** This call must be made after a call to sqlite3VdbeSetNumCols(). +** +** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC +** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed +** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. +*/ +SQLITE_PRIVATE int sqlite3VdbeSetColName( + Vdbe *p, /* Vdbe being configured */ + int idx, /* Index of column zName applies to */ + int var, /* One of the COLNAME_* constants */ + const char *zName, /* Pointer to buffer containing name */ + void (*xDel)(void*) /* Memory management strategy for zName */ +){ + int rc; + Mem *pColName; + assert( idxnResColumn ); + assert( vardb->mallocFailed ){ + assert( !zName || xDel!=SQLITE_DYNAMIC ); + return SQLITE_NOMEM_BKPT; + } + assert( p->aColName!=0 ); + pColName = &(p->aColName[idx+var*p->nResColumn]); + rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); + assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); + return rc; +} + +/* +** A read or write transaction may or may not be active on database handle +** db. If a transaction is active, commit it. If there is a +** write-transaction spanning more than one database file, this routine +** takes care of the master journal trickery. +*/ +static int vdbeCommit(sqlite3 *db, Vdbe *p){ + int i; + int nTrans = 0; /* Number of databases with an active write-transaction + ** that are candidates for a two-phase commit using a + ** master-journal */ + int rc = SQLITE_OK; + int needXcommit = 0; + +#ifdef SQLITE_OMIT_VIRTUALTABLE + /* With this option, sqlite3VtabSync() is defined to be simply + ** SQLITE_OK so p is not used. + */ + UNUSED_PARAMETER(p); +#endif + + /* Before doing anything else, call the xSync() callback for any + ** virtual module tables written in this transaction. This has to + ** be done before determining whether a master journal file is + ** required, as an xSync() callback may add an attached database + ** to the transaction. + */ + rc = sqlite3VtabSync(db, p); + + /* This loop determines (a) if the commit hook should be invoked and + ** (b) how many database files have open write transactions, not + ** including the temp database. (b) is important because if more than + ** one database file has an open write transaction, a master journal + ** file is required for an atomic commit. + */ + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( sqlite3BtreeIsInTrans(pBt) ){ + /* Whether or not a database might need a master journal depends upon + ** its journal mode (among other things). This matrix determines which + ** journal modes use a master journal and which do not */ + static const u8 aMJNeeded[] = { + /* DELETE */ 1, + /* PERSIST */ 1, + /* OFF */ 0, + /* TRUNCATE */ 1, + /* MEMORY */ 0, + /* WAL */ 0 + }; + Pager *pPager; /* Pager associated with pBt */ + needXcommit = 1; + sqlite3BtreeEnter(pBt); + pPager = sqlite3BtreePager(pBt); + if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF + && aMJNeeded[sqlite3PagerGetJournalMode(pPager)] + ){ + assert( i!=1 ); + nTrans++; + } + rc = sqlite3PagerExclusiveLock(pPager); + sqlite3BtreeLeave(pBt); + } + } + if( rc!=SQLITE_OK ){ + return rc; + } + + /* If there are any write-transactions at all, invoke the commit hook */ + if( needXcommit && db->xCommitCallback ){ + rc = db->xCommitCallback(db->pCommitArg); + if( rc ){ + return SQLITE_CONSTRAINT_COMMITHOOK; + } + } + + /* The simple case - no more than one database file (not counting the + ** TEMP database) has a transaction active. There is no need for the + ** master-journal. + ** + ** If the return value of sqlite3BtreeGetFilename() is a zero length + ** string, it means the main database is :memory: or a temp file. In + ** that case we do not support atomic multi-file commits, so use the + ** simple case then too. + */ + if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) + || nTrans<=1 + ){ + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + rc = sqlite3BtreeCommitPhaseOne(pBt, 0); + } + } + + /* Do the commit only if all databases successfully complete phase 1. + ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an + ** IO error while deleting or truncating a journal file. It is unlikely, + ** but could happen. In this case abandon processing and return the error. + */ + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); + } + } + if( rc==SQLITE_OK ){ + sqlite3VtabCommit(db); + } + } + + /* The complex case - There is a multi-file write-transaction active. + ** This requires a master journal file to ensure the transaction is + ** committed atomically. + */ +#ifndef SQLITE_OMIT_DISKIO + else{ + sqlite3_vfs *pVfs = db->pVfs; + char *zMaster = 0; /* File-name for the master journal */ + char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); + sqlite3_file *pMaster = 0; + i64 offset = 0; + int res; + int retryCount = 0; + int nMainFile; + + /* Select a master journal file name */ + nMainFile = sqlite3Strlen30(zMainFile); + zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); + if( zMaster==0 ) return SQLITE_NOMEM_BKPT; + do { + u32 iRandom; + if( retryCount ){ + if( retryCount>100 ){ + sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); + sqlite3OsDelete(pVfs, zMaster, 0); + break; + }else if( retryCount==1 ){ + sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); + } + } + retryCount++; + sqlite3_randomness(sizeof(iRandom), &iRandom); + sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", + (iRandom>>8)&0xffffff, iRandom&0xff); + /* The antipenultimate character of the master journal name must + ** be "9" to avoid name collisions when using 8+3 filenames. */ + assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); + sqlite3FileSuffix3(zMainFile, zMaster); + rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); + }while( rc==SQLITE_OK && res ); + if( rc==SQLITE_OK ){ + /* Open the master journal. */ + rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, + SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| + SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 + ); + } + if( rc!=SQLITE_OK ){ + sqlite3DbFree(db, zMaster); + return rc; + } + + /* Write the name of each database file in the transaction into the new + ** master journal file. If an error occurs at this point close + ** and delete the master journal file. All the individual journal files + ** still have 'null' as the master journal pointer, so they will roll + ** back independently if a failure occurs. + */ + for(i=0; inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( sqlite3BtreeIsInTrans(pBt) ){ + char const *zFile = sqlite3BtreeGetJournalname(pBt); + if( zFile==0 ){ + continue; /* Ignore TEMP and :memory: databases */ + } + assert( zFile[0]!=0 ); + rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); + offset += sqlite3Strlen30(zFile)+1; + if( rc!=SQLITE_OK ){ + sqlite3OsCloseFree(pMaster); + sqlite3OsDelete(pVfs, zMaster, 0); + sqlite3DbFree(db, zMaster); + return rc; + } + } + } + + /* Sync the master journal file. If the IOCAP_SEQUENTIAL device + ** flag is set this is not required. + */ + if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) + && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) + ){ + sqlite3OsCloseFree(pMaster); + sqlite3OsDelete(pVfs, zMaster, 0); + sqlite3DbFree(db, zMaster); + return rc; + } + + /* Sync all the db files involved in the transaction. The same call + ** sets the master journal pointer in each individual journal. If + ** an error occurs here, do not delete the master journal file. + ** + ** If the error occurs during the first call to + ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the + ** master journal file will be orphaned. But we cannot delete it, + ** in case the master journal file name was written into the journal + ** file before the failure occurred. + */ + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); + } + } + sqlite3OsCloseFree(pMaster); + assert( rc!=SQLITE_BUSY ); + if( rc!=SQLITE_OK ){ + sqlite3DbFree(db, zMaster); + return rc; + } + + /* Delete the master journal file. This commits the transaction. After + ** doing this the directory is synced again before any individual + ** transaction files are deleted. + */ + rc = sqlite3OsDelete(pVfs, zMaster, 1); + sqlite3DbFree(db, zMaster); + zMaster = 0; + if( rc ){ + return rc; + } + + /* All files and directories have already been synced, so the following + ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and + ** deleting or truncating journals. If something goes wrong while + ** this is happening we don't really care. The integrity of the + ** transaction is already guaranteed, but some stray 'cold' journals + ** may be lying around. Returning an error code won't help matters. + */ + disable_simulated_io_errors(); + sqlite3BeginBenignMalloc(); + for(i=0; inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + sqlite3BtreeCommitPhaseTwo(pBt, 1); + } + } + sqlite3EndBenignMalloc(); + enable_simulated_io_errors(); + + sqlite3VtabCommit(db); + } +#endif + + return rc; +} + +/* +** This routine checks that the sqlite3.nVdbeActive count variable +** matches the number of vdbe's in the list sqlite3.pVdbe that are +** currently active. An assertion fails if the two counts do not match. +** This is an internal self-check only - it is not an essential processing +** step. +** +** This is a no-op if NDEBUG is defined. +*/ +#ifndef NDEBUG +static void checkActiveVdbeCnt(sqlite3 *db){ + Vdbe *p; + int cnt = 0; + int nWrite = 0; + int nRead = 0; + p = db->pVdbe; + while( p ){ + if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ + cnt++; + if( p->readOnly==0 ) nWrite++; + if( p->bIsReader ) nRead++; + } + p = p->pNext; + } + assert( cnt==db->nVdbeActive ); + assert( nWrite==db->nVdbeWrite ); + assert( nRead==db->nVdbeRead ); +} +#else +#define checkActiveVdbeCnt(x) +#endif + +/* +** If the Vdbe passed as the first argument opened a statement-transaction, +** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or +** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement +** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the +** statement transaction is committed. +** +** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. +** Otherwise SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ + sqlite3 *const db = p->db; + int rc = SQLITE_OK; + + /* If p->iStatement is greater than zero, then this Vdbe opened a + ** statement transaction that should be closed here. The only exception + ** is that an IO error may have occurred, causing an emergency rollback. + ** In this case (db->nStatement==0), and there is nothing to do. + */ + if( db->nStatement && p->iStatement ){ + int i; + const int iSavepoint = p->iStatement-1; + + assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); + assert( db->nStatement>0 ); + assert( p->iStatement==(db->nStatement+db->nSavepoint) ); + + for(i=0; inDb; i++){ + int rc2 = SQLITE_OK; + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + if( eOp==SAVEPOINT_ROLLBACK ){ + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); + } + if( rc2==SQLITE_OK ){ + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); + } + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + db->nStatement--; + p->iStatement = 0; + + if( rc==SQLITE_OK ){ + if( eOp==SAVEPOINT_ROLLBACK ){ + rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); + } + if( rc==SQLITE_OK ){ + rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); + } + } + + /* If the statement transaction is being rolled back, also restore the + ** database handles deferred constraint counter to the value it had when + ** the statement transaction was opened. */ + if( eOp==SAVEPOINT_ROLLBACK ){ + db->nDeferredCons = p->nStmtDefCons; + db->nDeferredImmCons = p->nStmtDefImmCons; + } + } + return rc; +} + +/* +** This function is called when a transaction opened by the database +** handle associated with the VM passed as an argument is about to be +** committed. If there are outstanding deferred foreign key constraint +** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. +** +** If there are outstanding FK violations and this function returns +** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY +** and write an error message to it. Then return SQLITE_ERROR. +*/ +#ifndef SQLITE_OMIT_FOREIGN_KEY +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ + sqlite3 *db = p->db; + if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) + || (!deferred && p->nFkConstraint>0) + ){ + p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; + p->errorAction = OE_Abort; + sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); + return SQLITE_ERROR; + } + return SQLITE_OK; +} +#endif + +/* +** This routine is called the when a VDBE tries to halt. If the VDBE +** has made changes and is in autocommit mode, then commit those +** changes. If a rollback is needed, then do the rollback. +** +** This routine is the only way to move the state of a VM from +** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to +** call this on a VM that is in the SQLITE_MAGIC_HALT state. +** +** Return an error code. If the commit could not complete because of +** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it +** means the close did not happen and needs to be repeated. +*/ +SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){ + int rc; /* Used to store transient return codes */ + sqlite3 *db = p->db; + + /* This function contains the logic that determines if a statement or + ** transaction will be committed or rolled back as a result of the + ** execution of this virtual machine. + ** + ** If any of the following errors occur: + ** + ** SQLITE_NOMEM + ** SQLITE_IOERR + ** SQLITE_FULL + ** SQLITE_INTERRUPT + ** + ** Then the internal cache might have been left in an inconsistent + ** state. We need to rollback the statement transaction, if there is + ** one, or the complete transaction if there is no statement transaction. + */ + + if( db->mallocFailed ){ + p->rc = SQLITE_NOMEM_BKPT; + } + if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); + closeAllCursors(p); + if( p->magic!=VDBE_MAGIC_RUN ){ + return SQLITE_OK; + } + checkActiveVdbeCnt(db); + + /* No commit or rollback needed if the program never started or if the + ** SQL statement does not read or write a database file. */ + if( p->pc>=0 && p->bIsReader ){ + int mrc; /* Primary error code from p->rc */ + int eStatementOp = 0; + int isSpecialError; /* Set to true if a 'special' error */ + + /* Lock all btrees used by the statement */ + sqlite3VdbeEnter(p); + + /* Check for one of the special errors */ + mrc = p->rc & 0xff; + isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR + || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; + if( isSpecialError ){ + /* If the query was read-only and the error code is SQLITE_INTERRUPT, + ** no rollback is necessary. Otherwise, at least a savepoint + ** transaction must be rolled back to restore the database to a + ** consistent state. + ** + ** Even if the statement is read-only, it is important to perform + ** a statement or transaction rollback operation. If the error + ** occurred while writing to the journal, sub-journal or database + ** file as part of an effort to free up cache space (see function + ** pagerStress() in pager.c), the rollback is required to restore + ** the pager to a consistent state. + */ + if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ + if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ + eStatementOp = SAVEPOINT_ROLLBACK; + }else{ + /* We are forced to roll back the active transaction. Before doing + ** so, abort any other statements this handle currently has active. + */ + sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); + sqlite3CloseSavepoints(db); + db->autoCommit = 1; + p->nChange = 0; + } + } + } + + /* Check for immediate foreign key violations. */ + if( p->rc==SQLITE_OK ){ + sqlite3VdbeCheckFk(p, 0); + } + + /* If the auto-commit flag is set and this is the only active writer + ** VM, then we do either a commit or rollback of the current transaction. + ** + ** Note: This block also runs if one of the special errors handled + ** above has occurred. + */ + if( !sqlite3VtabInSync(db) + && db->autoCommit + && db->nVdbeWrite==(p->readOnly==0) + ){ + if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ + rc = sqlite3VdbeCheckFk(p, 1); + if( rc!=SQLITE_OK ){ + if( NEVER(p->readOnly) ){ + sqlite3VdbeLeave(p); + return SQLITE_ERROR; + } + rc = SQLITE_CONSTRAINT_FOREIGNKEY; + }else{ + /* The auto-commit flag is true, the vdbe program was successful + ** or hit an 'OR FAIL' constraint and there are no deferred foreign + ** key constraints to hold up the transaction. This means a commit + ** is required. */ + rc = vdbeCommit(db, p); + } + if( rc==SQLITE_BUSY && p->readOnly ){ + sqlite3VdbeLeave(p); + return SQLITE_BUSY; + }else if( rc!=SQLITE_OK ){ + p->rc = rc; + sqlite3RollbackAll(db, SQLITE_OK); + p->nChange = 0; + }else{ + db->nDeferredCons = 0; + db->nDeferredImmCons = 0; + db->flags &= ~SQLITE_DeferFKs; + sqlite3CommitInternalChanges(db); + } + }else{ + sqlite3RollbackAll(db, SQLITE_OK); + p->nChange = 0; + } + db->nStatement = 0; + }else if( eStatementOp==0 ){ + if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ + eStatementOp = SAVEPOINT_RELEASE; + }else if( p->errorAction==OE_Abort ){ + eStatementOp = SAVEPOINT_ROLLBACK; + }else{ + sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); + sqlite3CloseSavepoints(db); + db->autoCommit = 1; + p->nChange = 0; + } + } + + /* If eStatementOp is non-zero, then a statement transaction needs to + ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to + ** do so. If this operation returns an error, and the current statement + ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the + ** current statement error code. + */ + if( eStatementOp ){ + rc = sqlite3VdbeCloseStatement(p, eStatementOp); + if( rc ){ + if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ + p->rc = rc; + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = 0; + } + sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); + sqlite3CloseSavepoints(db); + db->autoCommit = 1; + p->nChange = 0; + } + } + + /* If this was an INSERT, UPDATE or DELETE and no statement transaction + ** has been rolled back, update the database connection change-counter. + */ + if( p->changeCntOn ){ + if( eStatementOp!=SAVEPOINT_ROLLBACK ){ + sqlite3VdbeSetChanges(db, p->nChange); + }else{ + sqlite3VdbeSetChanges(db, 0); + } + p->nChange = 0; + } + + /* Release the locks */ + sqlite3VdbeLeave(p); + } + + /* We have successfully halted and closed the VM. Record this fact. */ + if( p->pc>=0 ){ + db->nVdbeActive--; + if( !p->readOnly ) db->nVdbeWrite--; + if( p->bIsReader ) db->nVdbeRead--; + assert( db->nVdbeActive>=db->nVdbeRead ); + assert( db->nVdbeRead>=db->nVdbeWrite ); + assert( db->nVdbeWrite>=0 ); + } + p->magic = VDBE_MAGIC_HALT; + checkActiveVdbeCnt(db); + if( db->mallocFailed ){ + p->rc = SQLITE_NOMEM_BKPT; + } + + /* If the auto-commit flag is set to true, then any locks that were held + ** by connection db have now been released. Call sqlite3ConnectionUnlocked() + ** to invoke any required unlock-notify callbacks. + */ + if( db->autoCommit ){ + sqlite3ConnectionUnlocked(db); + } + + assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); + return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); +} + + +/* +** Each VDBE holds the result of the most recent sqlite3_step() call +** in p->rc. This routine sets that result back to SQLITE_OK. +*/ +SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){ + p->rc = SQLITE_OK; +} + +/* +** Copy the error code and error message belonging to the VDBE passed +** as the first argument to its database handle (so that they will be +** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). +** +** This function does not clear the VDBE error code or message, just +** copies them to the database handle. +*/ +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){ + sqlite3 *db = p->db; + int rc = p->rc; + if( p->zErrMsg ){ + db->bBenignMalloc++; + sqlite3BeginBenignMalloc(); + if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); + sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); + sqlite3EndBenignMalloc(); + db->bBenignMalloc--; + db->errCode = rc; + }else{ + sqlite3Error(db, rc); + } + return rc; +} + +#ifdef SQLITE_ENABLE_SQLLOG +/* +** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, +** invoke it. +*/ +static void vdbeInvokeSqllog(Vdbe *v){ + if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){ + char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql); + assert( v->db->init.busy==0 ); + if( zExpanded ){ + sqlite3GlobalConfig.xSqllog( + sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1 + ); + sqlite3DbFree(v->db, zExpanded); + } + } +} +#else +# define vdbeInvokeSqllog(x) +#endif + +/* +** Clean up a VDBE after execution but do not delete the VDBE just yet. +** Write any error messages into *pzErrMsg. Return the result code. +** +** After this routine is run, the VDBE should be ready to be executed +** again. +** +** To look at it another way, this routine resets the state of the +** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to +** VDBE_MAGIC_INIT. +*/ +SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){ + sqlite3 *db; + db = p->db; + + /* If the VM did not run to completion or if it encountered an + ** error, then it might not have been halted properly. So halt + ** it now. + */ + sqlite3VdbeHalt(p); + + /* If the VDBE has be run even partially, then transfer the error code + ** and error message from the VDBE into the main database structure. But + ** if the VDBE has just been set to run but has not actually executed any + ** instructions yet, leave the main database error information unchanged. + */ + if( p->pc>=0 ){ + vdbeInvokeSqllog(p); + sqlite3VdbeTransferError(p); + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = 0; + if( p->runOnlyOnce ) p->expired = 1; + }else if( p->rc && p->expired ){ + /* The expired flag was set on the VDBE before the first call + ** to sqlite3_step(). For consistency (since sqlite3_step() was + ** called), set the database error in this case as well. + */ + sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = 0; + } + + /* Reclaim all memory used by the VDBE + */ + Cleanup(p); + + /* Save profiling information from this VDBE run. + */ +#ifdef VDBE_PROFILE + { + FILE *out = fopen("vdbe_profile.out", "a"); + if( out ){ + int i; + fprintf(out, "---- "); + for(i=0; inOp; i++){ + fprintf(out, "%02x", p->aOp[i].opcode); + } + fprintf(out, "\n"); + if( p->zSql ){ + char c, pc = 0; + fprintf(out, "-- "); + for(i=0; (c = p->zSql[i])!=0; i++){ + if( pc=='\n' ) fprintf(out, "-- "); + putc(c, out); + pc = c; + } + if( pc!='\n' ) fprintf(out, "\n"); + } + for(i=0; inOp; i++){ + char zHdr[100]; + sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", + p->aOp[i].cnt, + p->aOp[i].cycles, + p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 + ); + fprintf(out, "%s", zHdr); + sqlite3VdbePrintOp(out, i, &p->aOp[i]); + } + fclose(out); + } + } +#endif + p->iCurrentTime = 0; + p->magic = VDBE_MAGIC_INIT; + return p->rc & db->errMask; +} + +/* +** Clean up and delete a VDBE after execution. Return an integer which is +** the result code. Write any error message text into *pzErrMsg. +*/ +SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){ + int rc = SQLITE_OK; + if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ + rc = sqlite3VdbeReset(p); + assert( (rc & p->db->errMask)==rc ); + } + sqlite3VdbeDelete(p); + return rc; +} + +/* +** If parameter iOp is less than zero, then invoke the destructor for +** all auxiliary data pointers currently cached by the VM passed as +** the first argument. +** +** Or, if iOp is greater than or equal to zero, then the destructor is +** only invoked for those auxiliary data pointers created by the user +** function invoked by the OP_Function opcode at instruction iOp of +** VM pVdbe, and only then if: +** +** * the associated function parameter is the 32nd or later (counting +** from left to right), or +** +** * the corresponding bit in argument mask is clear (where the first +** function parameter corresponds to bit 0 etc.). +*/ +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){ + while( *pp ){ + AuxData *pAux = *pp; + if( (iOp<0) + || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) + ){ + testcase( pAux->iArg==31 ); + if( pAux->xDelete ){ + pAux->xDelete(pAux->pAux); + } + *pp = pAux->pNext; + sqlite3DbFree(db, pAux); + }else{ + pp= &pAux->pNext; + } + } +} + +/* +** Free all memory associated with the Vdbe passed as the second argument, +** except for object itself, which is preserved. +** +** The difference between this function and sqlite3VdbeDelete() is that +** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with +** the database connection and frees the object itself. +*/ +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ + SubProgram *pSub, *pNext; + int i; + assert( p->db==0 || p->db==db ); + releaseMemArray(p->aVar, p->nVar); + releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); + for(pSub=p->pProgram; pSub; pSub=pNext){ + pNext = pSub->pNext; + vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); + sqlite3DbFree(db, pSub); + } + for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); + sqlite3DbFree(db, p->azVar); + vdbeFreeOpArray(db, p->aOp, p->nOp); + sqlite3DbFree(db, p->aColName); + sqlite3DbFree(db, p->zSql); + sqlite3DbFree(db, p->pFree); +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + for(i=0; inScan; i++){ + sqlite3DbFree(db, p->aScan[i].zName); + } + sqlite3DbFree(db, p->aScan); +#endif +} + +/* +** Delete an entire VDBE. +*/ +SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){ + sqlite3 *db; + + if( NEVER(p==0) ) return; + db = p->db; + assert( sqlite3_mutex_held(db->mutex) ); + sqlite3VdbeClearObject(db, p); + if( p->pPrev ){ + p->pPrev->pNext = p->pNext; + }else{ + assert( db->pVdbe==p ); + db->pVdbe = p->pNext; + } + if( p->pNext ){ + p->pNext->pPrev = p->pPrev; + } + p->magic = VDBE_MAGIC_DEAD; + p->db = 0; + sqlite3DbFree(db, p); +} + +/* +** The cursor "p" has a pending seek operation that has not yet been +** carried out. Seek the cursor now. If an error occurs, return +** the appropriate error code. +*/ +static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ + int res, rc; +#ifdef SQLITE_TEST + extern int sqlite3_search_count; +#endif + assert( p->deferredMoveto ); + assert( p->isTable ); + assert( p->eCurType==CURTYPE_BTREE ); + rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res); + if( rc ) return rc; + if( res!=0 ) return SQLITE_CORRUPT_BKPT; +#ifdef SQLITE_TEST + sqlite3_search_count++; +#endif + p->deferredMoveto = 0; + p->cacheStatus = CACHE_STALE; + return SQLITE_OK; +} + +/* +** Something has moved cursor "p" out of place. Maybe the row it was +** pointed to was deleted out from under it. Or maybe the btree was +** rebalanced. Whatever the cause, try to restore "p" to the place it +** is supposed to be pointing. If the row was deleted out from under the +** cursor, set the cursor to point to a NULL row. +*/ +static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ + int isDifferentRow, rc; + assert( p->eCurType==CURTYPE_BTREE ); + assert( p->uc.pCursor!=0 ); + assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ); + rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow); + p->cacheStatus = CACHE_STALE; + if( isDifferentRow ) p->nullRow = 1; + return rc; +} + +/* +** Check to ensure that the cursor is valid. Restore the cursor +** if need be. Return any I/O error from the restore operation. +*/ +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){ + assert( p->eCurType==CURTYPE_BTREE ); + if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ + return handleMovedCursor(p); + } + return SQLITE_OK; +} + +/* +** Make sure the cursor p is ready to read or write the row to which it +** was last positioned. Return an error code if an OOM fault or I/O error +** prevents us from positioning the cursor to its correct position. +** +** If a MoveTo operation is pending on the given cursor, then do that +** MoveTo now. If no move is pending, check to see if the row has been +** deleted out from under the cursor and if it has, mark the row as +** a NULL row. +** +** If the cursor is already pointing to the correct row and that row has +** not been deleted out from under the cursor, then this routine is a no-op. +*/ +SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ + VdbeCursor *p = *pp; + if( p->eCurType==CURTYPE_BTREE ){ + if( p->deferredMoveto ){ + int iMap; + if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){ + *pp = p->pAltCursor; + *piCol = iMap - 1; + return SQLITE_OK; + } + return handleDeferredMoveto(p); + } + if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ + return handleMovedCursor(p); + } + } + return SQLITE_OK; +} + +/* +** The following functions: +** +** sqlite3VdbeSerialType() +** sqlite3VdbeSerialTypeLen() +** sqlite3VdbeSerialLen() +** sqlite3VdbeSerialPut() +** sqlite3VdbeSerialGet() +** +** encapsulate the code that serializes values for storage in SQLite +** data and index records. Each serialized value consists of a +** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned +** integer, stored as a varint. +** +** In an SQLite index record, the serial type is stored directly before +** the blob of data that it corresponds to. In a table record, all serial +** types are stored at the start of the record, and the blobs of data at +** the end. Hence these functions allow the caller to handle the +** serial-type and data blob separately. +** +** The following table describes the various storage classes for data: +** +** serial type bytes of data type +** -------------- --------------- --------------- +** 0 0 NULL +** 1 1 signed integer +** 2 2 signed integer +** 3 3 signed integer +** 4 4 signed integer +** 5 6 signed integer +** 6 8 signed integer +** 7 8 IEEE float +** 8 0 Integer constant 0 +** 9 0 Integer constant 1 +** 10,11 reserved for expansion +** N>=12 and even (N-12)/2 BLOB +** N>=13 and odd (N-13)/2 text +** +** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions +** of SQLite will not understand those serial types. +*/ + +/* +** Return the serial-type for the value stored in pMem. +*/ +SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ + int flags = pMem->flags; + u32 n; + + assert( pLen!=0 ); + if( flags&MEM_Null ){ + *pLen = 0; + return 0; + } + if( flags&MEM_Int ){ + /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ +# define MAX_6BYTE ((((i64)0x00008000)<<32)-1) + i64 i = pMem->u.i; + u64 u; + if( i<0 ){ + u = ~i; + }else{ + u = i; + } + if( u<=127 ){ + if( (i&1)==i && file_format>=4 ){ + *pLen = 0; + return 8+(u32)u; + }else{ + *pLen = 1; + return 1; + } + } + if( u<=32767 ){ *pLen = 2; return 2; } + if( u<=8388607 ){ *pLen = 3; return 3; } + if( u<=2147483647 ){ *pLen = 4; return 4; } + if( u<=MAX_6BYTE ){ *pLen = 6; return 5; } + *pLen = 8; + return 6; + } + if( flags&MEM_Real ){ + *pLen = 8; + return 7; + } + assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); + assert( pMem->n>=0 ); + n = (u32)pMem->n; + if( flags & MEM_Zero ){ + n += pMem->u.nZero; + } + *pLen = n; + return ((n*2) + 12 + ((flags&MEM_Str)!=0)); +} + +/* +** The sizes for serial types less than 128 +*/ +static const u8 sqlite3SmallTypeSizes[] = { + /* 0 1 2 3 4 5 6 7 8 9 */ +/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, +/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, +/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, +/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, +/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, +/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, +/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, +/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, +/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, +/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, +/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, +/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, +/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57 +}; + +/* +** Return the length of the data corresponding to the supplied serial-type. +*/ +SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ + if( serial_type>=128 ){ + return (serial_type-12)/2; + }else{ + assert( serial_type<12 + || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 ); + return sqlite3SmallTypeSizes[serial_type]; + } +} +SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ + assert( serial_type<128 ); + return sqlite3SmallTypeSizes[serial_type]; +} + +/* +** If we are on an architecture with mixed-endian floating +** points (ex: ARM7) then swap the lower 4 bytes with the +** upper 4 bytes. Return the result. +** +** For most architectures, this is a no-op. +** +** (later): It is reported to me that the mixed-endian problem +** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems +** that early versions of GCC stored the two words of a 64-bit +** float in the wrong order. And that error has been propagated +** ever since. The blame is not necessarily with GCC, though. +** GCC might have just copying the problem from a prior compiler. +** I am also told that newer versions of GCC that follow a different +** ABI get the byte order right. +** +** Developers using SQLite on an ARM7 should compile and run their +** application using -DSQLITE_DEBUG=1 at least once. With DEBUG +** enabled, some asserts below will ensure that the byte order of +** floating point values is correct. +** +** (2007-08-30) Frank van Vugt has studied this problem closely +** and has send his findings to the SQLite developers. Frank +** writes that some Linux kernels offer floating point hardware +** emulation that uses only 32-bit mantissas instead of a full +** 48-bits as required by the IEEE standard. (This is the +** CONFIG_FPE_FASTFPE option.) On such systems, floating point +** byte swapping becomes very complicated. To avoid problems, +** the necessary byte swapping is carried out using a 64-bit integer +** rather than a 64-bit float. Frank assures us that the code here +** works for him. We, the developers, have no way to independently +** verify this, but Frank seems to know what he is talking about +** so we trust him. +*/ +#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT +static u64 floatSwap(u64 in){ + union { + u64 r; + u32 i[2]; + } u; + u32 t; + + u.r = in; + t = u.i[0]; + u.i[0] = u.i[1]; + u.i[1] = t; + return u.r; +} +# define swapMixedEndianFloat(X) X = floatSwap(X) +#else +# define swapMixedEndianFloat(X) +#endif + +/* +** Write the serialized data blob for the value stored in pMem into +** buf. It is assumed that the caller has allocated sufficient space. +** Return the number of bytes written. +** +** nBuf is the amount of space left in buf[]. The caller is responsible +** for allocating enough space to buf[] to hold the entire field, exclusive +** of the pMem->u.nZero bytes for a MEM_Zero value. +** +** Return the number of bytes actually written into buf[]. The number +** of bytes in the zero-filled tail is included in the return value only +** if those bytes were zeroed in buf[]. +*/ +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ + u32 len; + + /* Integer and Real */ + if( serial_type<=7 && serial_type>0 ){ + u64 v; + u32 i; + if( serial_type==7 ){ + assert( sizeof(v)==sizeof(pMem->u.r) ); + memcpy(&v, &pMem->u.r, sizeof(v)); + swapMixedEndianFloat(v); + }else{ + v = pMem->u.i; + } + len = i = sqlite3SmallTypeSizes[serial_type]; + assert( i>0 ); + do{ + buf[--i] = (u8)(v&0xFF); + v >>= 8; + }while( i ); + return len; + } + + /* String or blob */ + if( serial_type>=12 ){ + assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) + == (int)sqlite3VdbeSerialTypeLen(serial_type) ); + len = pMem->n; + if( len>0 ) memcpy(buf, pMem->z, len); + return len; + } + + /* NULL or constants 0 or 1 */ + return 0; +} + +/* Input "x" is a sequence of unsigned characters that represent a +** big-endian integer. Return the equivalent native integer +*/ +#define ONE_BYTE_INT(x) ((i8)(x)[0]) +#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) +#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) +#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) +#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) + +/* +** Deserialize the data blob pointed to by buf as serial type serial_type +** and store the result in pMem. Return the number of bytes read. +** +** This function is implemented as two separate routines for performance. +** The few cases that require local variables are broken out into a separate +** routine so that in most cases the overhead of moving the stack pointer +** is avoided. +*/ +static u32 SQLITE_NOINLINE serialGet( + const unsigned char *buf, /* Buffer to deserialize from */ + u32 serial_type, /* Serial type to deserialize */ + Mem *pMem /* Memory cell to write value into */ +){ + u64 x = FOUR_BYTE_UINT(buf); + u32 y = FOUR_BYTE_UINT(buf+4); + x = (x<<32) + y; + if( serial_type==6 ){ + /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit + ** twos-complement integer. */ + pMem->u.i = *(i64*)&x; + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + }else{ + /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit + ** floating point number. */ +#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) + /* Verify that integers and floating point values use the same + ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is + ** defined that 64-bit floating point values really are mixed + ** endian. + */ + static const u64 t1 = ((u64)0x3ff00000)<<32; + static const double r1 = 1.0; + u64 t2 = t1; + swapMixedEndianFloat(t2); + assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); +#endif + assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); + swapMixedEndianFloat(x); + memcpy(&pMem->u.r, &x, sizeof(x)); + pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; + } + return 8; +} +SQLITE_PRIVATE u32 sqlite3VdbeSerialGet( + const unsigned char *buf, /* Buffer to deserialize from */ + u32 serial_type, /* Serial type to deserialize */ + Mem *pMem /* Memory cell to write value into */ +){ + switch( serial_type ){ + case 10: /* Reserved for future use */ + case 11: /* Reserved for future use */ + case 0: { /* Null */ + /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ + pMem->flags = MEM_Null; + break; + } + case 1: { + /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement + ** integer. */ + pMem->u.i = ONE_BYTE_INT(buf); + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + return 1; + } + case 2: { /* 2-byte signed integer */ + /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit + ** twos-complement integer. */ + pMem->u.i = TWO_BYTE_INT(buf); + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + return 2; + } + case 3: { /* 3-byte signed integer */ + /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit + ** twos-complement integer. */ + pMem->u.i = THREE_BYTE_INT(buf); + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + return 3; + } + case 4: { /* 4-byte signed integer */ + /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit + ** twos-complement integer. */ + pMem->u.i = FOUR_BYTE_INT(buf); +#ifdef __HP_cc + /* Work around a sign-extension bug in the HP compiler for HP/UX */ + if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL; +#endif + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + return 4; + } + case 5: { /* 6-byte signed integer */ + /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit + ** twos-complement integer. */ + pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); + pMem->flags = MEM_Int; + testcase( pMem->u.i<0 ); + return 6; + } + case 6: /* 8-byte signed integer */ + case 7: { /* IEEE floating point */ + /* These use local variables, so do them in a separate routine + ** to avoid having to move the frame pointer in the common case */ + return serialGet(buf,serial_type,pMem); + } + case 8: /* Integer 0 */ + case 9: { /* Integer 1 */ + /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ + /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ + pMem->u.i = serial_type-8; + pMem->flags = MEM_Int; + return 0; + } + default: { + /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in + ** length. + ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and + ** (N-13)/2 bytes in length. */ + static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; + pMem->z = (char *)buf; + pMem->n = (serial_type-12)/2; + pMem->flags = aFlag[serial_type&1]; + return pMem->n; + } + } + return 0; +} +/* +** This routine is used to allocate sufficient space for an UnpackedRecord +** structure large enough to be used with sqlite3VdbeRecordUnpack() if +** the first argument is a pointer to KeyInfo structure pKeyInfo. +** +** The space is either allocated using sqlite3DbMallocRaw() or from within +** the unaligned buffer passed via the second and third arguments (presumably +** stack space). If the former, then *ppFree is set to a pointer that should +** be eventually freed by the caller using sqlite3DbFree(). Or, if the +** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL +** before returning. +** +** If an OOM error occurs, NULL is returned. +*/ +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( + KeyInfo *pKeyInfo, /* Description of the record */ + char *pSpace, /* Unaligned space available */ + int szSpace, /* Size of pSpace[] in bytes */ + char **ppFree /* OUT: Caller should free this pointer */ +){ + UnpackedRecord *p; /* Unpacked record to return */ + int nOff; /* Increment pSpace by nOff to align it */ + int nByte; /* Number of bytes required for *p */ + + /* We want to shift the pointer pSpace up such that it is 8-byte aligned. + ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift + ** it by. If pSpace is already 8-byte aligned, nOff should be zero. + */ + nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; + nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); + if( nByte>szSpace+nOff ){ + p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); + *ppFree = (char *)p; + if( !p ) return 0; + }else{ + p = (UnpackedRecord*)&pSpace[nOff]; + *ppFree = 0; + } + + p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; + assert( pKeyInfo->aSortOrder!=0 ); + p->pKeyInfo = pKeyInfo; + p->nField = pKeyInfo->nField + 1; + return p; +} + +/* +** Given the nKey-byte encoding of a record in pKey[], populate the +** UnpackedRecord structure indicated by the fourth argument with the +** contents of the decoded record. +*/ +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack( + KeyInfo *pKeyInfo, /* Information about the record format */ + int nKey, /* Size of the binary record */ + const void *pKey, /* The binary record */ + UnpackedRecord *p /* Populate this structure before returning. */ +){ + const unsigned char *aKey = (const unsigned char *)pKey; + int d; + u32 idx; /* Offset in aKey[] to read from */ + u16 u; /* Unsigned loop counter */ + u32 szHdr; + Mem *pMem = p->aMem; + + p->default_rc = 0; + assert( EIGHT_BYTE_ALIGNMENT(pMem) ); + idx = getVarint32(aKey, szHdr); + d = szHdr; + u = 0; + while( idxenc = pKeyInfo->enc; + pMem->db = pKeyInfo->db; + /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ + pMem->szMalloc = 0; + pMem->z = 0; + d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); + pMem++; + if( (++u)>=p->nField ) break; + } + assert( u<=pKeyInfo->nField + 1 ); + p->nField = u; +} + +#if SQLITE_DEBUG +/* +** This function compares two index or table record keys in the same way +** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), +** this function deserializes and compares values using the +** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used +** in assert() statements to ensure that the optimized code in +** sqlite3VdbeRecordCompare() returns results with these two primitives. +** +** Return true if the result of comparison is equivalent to desiredResult. +** Return false if there is a disagreement. +*/ +static int vdbeRecordCompareDebug( + int nKey1, const void *pKey1, /* Left key */ + const UnpackedRecord *pPKey2, /* Right key */ + int desiredResult /* Correct answer */ +){ + u32 d1; /* Offset into aKey[] of next data element */ + u32 idx1; /* Offset into aKey[] of next header element */ + u32 szHdr1; /* Number of bytes in header */ + int i = 0; + int rc = 0; + const unsigned char *aKey1 = (const unsigned char *)pKey1; + KeyInfo *pKeyInfo; + Mem mem1; + + pKeyInfo = pPKey2->pKeyInfo; + if( pKeyInfo->db==0 ) return 1; + mem1.enc = pKeyInfo->enc; + mem1.db = pKeyInfo->db; + /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ + VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ + + /* Compilers may complain that mem1.u.i is potentially uninitialized. + ** We could initialize it, as shown here, to silence those complaints. + ** But in fact, mem1.u.i will never actually be used uninitialized, and doing + ** the unnecessary initialization has a measurable negative performance + ** impact, since this routine is a very high runner. And so, we choose + ** to ignore the compiler warnings and leave this variable uninitialized. + */ + /* mem1.u.i = 0; // not needed, here to silence compiler warning */ + + idx1 = getVarint32(aKey1, szHdr1); + if( szHdr1>98307 ) return SQLITE_CORRUPT; + d1 = szHdr1; + assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); + assert( pKeyInfo->aSortOrder!=0 ); + assert( pKeyInfo->nField>0 ); + assert( idx1<=szHdr1 || CORRUPT_DB ); + do{ + u32 serial_type1; + + /* Read the serial types for the next element in each key. */ + idx1 += getVarint32( aKey1+idx1, serial_type1 ); + + /* Verify that there is enough key space remaining to avoid + ** a buffer overread. The "d1+serial_type1+2" subexpression will + ** always be greater than or equal to the amount of required key space. + ** Use that approximation to avoid the more expensive call to + ** sqlite3VdbeSerialTypeLen() in the common case. + */ + if( d1+serial_type1+2>(u32)nKey1 + && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 + ){ + break; + } + + /* Extract the values to be compared. + */ + d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); + + /* Do the comparison + */ + rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); + if( rc!=0 ){ + assert( mem1.szMalloc==0 ); /* See comment below */ + if( pKeyInfo->aSortOrder[i] ){ + rc = -rc; /* Invert the result for DESC sort order. */ + } + goto debugCompareEnd; + } + i++; + }while( idx1nField ); + + /* No memory allocation is ever used on mem1. Prove this using + ** the following assert(). If the assert() fails, it indicates a + ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). + */ + assert( mem1.szMalloc==0 ); + + /* rc==0 here means that one of the keys ran out of fields and + ** all the fields up to that point were equal. Return the default_rc + ** value. */ + rc = pPKey2->default_rc; + +debugCompareEnd: + if( desiredResult==0 && rc==0 ) return 1; + if( desiredResult<0 && rc<0 ) return 1; + if( desiredResult>0 && rc>0 ) return 1; + if( CORRUPT_DB ) return 1; + if( pKeyInfo->db->mallocFailed ) return 1; + return 0; +} +#endif + +#if SQLITE_DEBUG +/* +** Count the number of fields (a.k.a. columns) in the record given by +** pKey,nKey. The verify that this count is less than or equal to the +** limit given by pKeyInfo->nField + pKeyInfo->nXField. +** +** If this constraint is not satisfied, it means that the high-speed +** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will +** not work correctly. If this assert() ever fires, it probably means +** that the KeyInfo.nField or KeyInfo.nXField values were computed +** incorrectly. +*/ +static void vdbeAssertFieldCountWithinLimits( + int nKey, const void *pKey, /* The record to verify */ + const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */ +){ + int nField = 0; + u32 szHdr; + u32 idx; + u32 notUsed; + const unsigned char *aKey = (const unsigned char*)pKey; + + if( CORRUPT_DB ) return; + idx = getVarint32(aKey, szHdr); + assert( nKey>=0 ); + assert( szHdr<=(u32)nKey ); + while( idxnField+pKeyInfo->nXField ); +} +#else +# define vdbeAssertFieldCountWithinLimits(A,B,C) +#endif + +/* +** Both *pMem1 and *pMem2 contain string values. Compare the two values +** using the collation sequence pColl. As usual, return a negative , zero +** or positive value if *pMem1 is less than, equal to or greater than +** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". +*/ +static int vdbeCompareMemString( + const Mem *pMem1, + const Mem *pMem2, + const CollSeq *pColl, + u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ +){ + if( pMem1->enc==pColl->enc ){ + /* The strings are already in the correct encoding. Call the + ** comparison function directly */ + return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); + }else{ + int rc; + const void *v1, *v2; + int n1, n2; + Mem c1; + Mem c2; + sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); + sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); + sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); + sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); + v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); + n1 = v1==0 ? 0 : c1.n; + v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); + n2 = v2==0 ? 0 : c2.n; + rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); + if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT; + sqlite3VdbeMemRelease(&c1); + sqlite3VdbeMemRelease(&c2); + return rc; + } +} + +/* +** Compare two blobs. Return negative, zero, or positive if the first +** is less than, equal to, or greater than the second, respectively. +** If one blob is a prefix of the other, then the shorter is the lessor. +*/ +static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ + int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); + if( c ) return c; + return pB1->n - pB2->n; +} + +/* +** Do a comparison between a 64-bit signed integer and a 64-bit floating-point +** number. Return negative, zero, or positive if the first (i64) is less than, +** equal to, or greater than the second (double). +*/ +static int sqlite3IntFloatCompare(i64 i, double r){ + if( sizeof(LONGDOUBLE_TYPE)>8 ){ + LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; + if( xr ) return +1; + return 0; + }else{ + i64 y; + double s; + if( r<-9223372036854775808.0 ) return +1; + if( r>9223372036854775807.0 ) return -1; + y = (i64)r; + if( iy ){ + if( y==SMALLEST_INT64 && r>0.0 ) return -1; + return +1; + } + s = (double)i; + if( sr ) return +1; + return 0; + } +} + +/* +** Compare the values contained by the two memory cells, returning +** negative, zero or positive if pMem1 is less than, equal to, or greater +** than pMem2. Sorting order is NULL's first, followed by numbers (integers +** and reals) sorted numerically, followed by text ordered by the collating +** sequence pColl and finally blob's ordered by memcmp(). +** +** Two NULL values are considered equal by this function. +*/ +SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ + int f1, f2; + int combined_flags; + + f1 = pMem1->flags; + f2 = pMem2->flags; + combined_flags = f1|f2; + assert( (combined_flags & MEM_RowSet)==0 ); + + /* If one value is NULL, it is less than the other. If both values + ** are NULL, return 0. + */ + if( combined_flags&MEM_Null ){ + return (f2&MEM_Null) - (f1&MEM_Null); + } + + /* At least one of the two values is a number + */ + if( combined_flags&(MEM_Int|MEM_Real) ){ + if( (f1 & f2 & MEM_Int)!=0 ){ + if( pMem1->u.i < pMem2->u.i ) return -1; + if( pMem1->u.i > pMem2->u.i ) return +1; + return 0; + } + if( (f1 & f2 & MEM_Real)!=0 ){ + if( pMem1->u.r < pMem2->u.r ) return -1; + if( pMem1->u.r > pMem2->u.r ) return +1; + return 0; + } + if( (f1&MEM_Int)!=0 ){ + if( (f2&MEM_Real)!=0 ){ + return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r); + }else{ + return -1; + } + } + if( (f1&MEM_Real)!=0 ){ + if( (f2&MEM_Int)!=0 ){ + return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r); + }else{ + return -1; + } + } + return +1; + } + + /* If one value is a string and the other is a blob, the string is less. + ** If both are strings, compare using the collating functions. + */ + if( combined_flags&MEM_Str ){ + if( (f1 & MEM_Str)==0 ){ + return 1; + } + if( (f2 & MEM_Str)==0 ){ + return -1; + } + + assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed ); + assert( pMem1->enc==SQLITE_UTF8 || + pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); + + /* The collation sequence must be defined at this point, even if + ** the user deletes the collation sequence after the vdbe program is + ** compiled (this was not always the case). + */ + assert( !pColl || pColl->xCmp ); + + if( pColl ){ + return vdbeCompareMemString(pMem1, pMem2, pColl, 0); + } + /* If a NULL pointer was passed as the collate function, fall through + ** to the blob case and use memcmp(). */ + } + + /* Both values must be blobs. Compare using memcmp(). */ + return sqlite3BlobCompare(pMem1, pMem2); +} + + +/* +** The first argument passed to this function is a serial-type that +** corresponds to an integer - all values between 1 and 9 inclusive +** except 7. The second points to a buffer containing an integer value +** serialized according to serial_type. This function deserializes +** and returns the value. +*/ +static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ + u32 y; + assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); + switch( serial_type ){ + case 0: + case 1: + testcase( aKey[0]&0x80 ); + return ONE_BYTE_INT(aKey); + case 2: + testcase( aKey[0]&0x80 ); + return TWO_BYTE_INT(aKey); + case 3: + testcase( aKey[0]&0x80 ); + return THREE_BYTE_INT(aKey); + case 4: { + testcase( aKey[0]&0x80 ); + y = FOUR_BYTE_UINT(aKey); + return (i64)*(int*)&y; + } + case 5: { + testcase( aKey[0]&0x80 ); + return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); + } + case 6: { + u64 x = FOUR_BYTE_UINT(aKey); + testcase( aKey[0]&0x80 ); + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); + return (i64)*(i64*)&x; + } + } + + return (serial_type - 8); +} + +/* +** This function compares the two table rows or index records +** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero +** or positive integer if key1 is less than, equal to or +** greater than key2. The {nKey1, pKey1} key must be a blob +** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 +** key must be a parsed key such as obtained from +** sqlite3VdbeParseRecord. +** +** If argument bSkip is non-zero, it is assumed that the caller has already +** determined that the first fields of the keys are equal. +** +** Key1 and Key2 do not have to contain the same number of fields. If all +** fields that appear in both keys are equal, then pPKey2->default_rc is +** returned. +** +** If database corruption is discovered, set pPKey2->errCode to +** SQLITE_CORRUPT and return 0. If an OOM error is encountered, +** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the +** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). +*/ +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip( + int nKey1, const void *pKey1, /* Left key */ + UnpackedRecord *pPKey2, /* Right key */ + int bSkip /* If true, skip the first field */ +){ + u32 d1; /* Offset into aKey[] of next data element */ + int i; /* Index of next field to compare */ + u32 szHdr1; /* Size of record header in bytes */ + u32 idx1; /* Offset of first type in header */ + int rc = 0; /* Return value */ + Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ + KeyInfo *pKeyInfo = pPKey2->pKeyInfo; + const unsigned char *aKey1 = (const unsigned char *)pKey1; + Mem mem1; + + /* If bSkip is true, then the caller has already determined that the first + ** two elements in the keys are equal. Fix the various stack variables so + ** that this routine begins comparing at the second field. */ + if( bSkip ){ + u32 s1; + idx1 = 1 + getVarint32(&aKey1[1], s1); + szHdr1 = aKey1[0]; + d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); + i = 1; + pRhs++; + }else{ + idx1 = getVarint32(aKey1, szHdr1); + d1 = szHdr1; + if( d1>(unsigned)nKey1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corruption */ + } + i = 0; + } + + VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ + assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField + || CORRUPT_DB ); + assert( pPKey2->pKeyInfo->aSortOrder!=0 ); + assert( pPKey2->pKeyInfo->nField>0 ); + assert( idx1<=szHdr1 || CORRUPT_DB ); + do{ + u32 serial_type; + + /* RHS is an integer */ + if( pRhs->flags & MEM_Int ){ + serial_type = aKey1[idx1]; + testcase( serial_type==12 ); + if( serial_type>=10 ){ + rc = +1; + }else if( serial_type==0 ){ + rc = -1; + }else if( serial_type==7 ){ + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); + rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r); + }else{ + i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); + i64 rhs = pRhs->u.i; + if( lhsrhs ){ + rc = +1; + } + } + } + + /* RHS is real */ + else if( pRhs->flags & MEM_Real ){ + serial_type = aKey1[idx1]; + if( serial_type>=10 ){ + /* Serial types 12 or greater are strings and blobs (greater than + ** numbers). Types 10 and 11 are currently "reserved for future + ** use", so it doesn't really matter what the results of comparing + ** them to numberic values are. */ + rc = +1; + }else if( serial_type==0 ){ + rc = -1; + }else{ + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); + if( serial_type==7 ){ + if( mem1.u.ru.r ){ + rc = -1; + }else if( mem1.u.r>pRhs->u.r ){ + rc = +1; + } + }else{ + rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r); + } + } + } + + /* RHS is a string */ + else if( pRhs->flags & MEM_Str ){ + getVarint32(&aKey1[idx1], serial_type); + testcase( serial_type==12 ); + if( serial_type<12 ){ + rc = -1; + }else if( !(serial_type & 0x01) ){ + rc = +1; + }else{ + mem1.n = (serial_type - 12) / 2; + testcase( (d1+mem1.n)==(unsigned)nKey1 ); + testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); + if( (d1+mem1.n) > (unsigned)nKey1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corruption */ + }else if( pKeyInfo->aColl[i] ){ + mem1.enc = pKeyInfo->enc; + mem1.db = pKeyInfo->db; + mem1.flags = MEM_Str; + mem1.z = (char*)&aKey1[d1]; + rc = vdbeCompareMemString( + &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode + ); + }else{ + int nCmp = MIN(mem1.n, pRhs->n); + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); + if( rc==0 ) rc = mem1.n - pRhs->n; + } + } + } + + /* RHS is a blob */ + else if( pRhs->flags & MEM_Blob ){ + getVarint32(&aKey1[idx1], serial_type); + testcase( serial_type==12 ); + if( serial_type<12 || (serial_type & 0x01) ){ + rc = -1; + }else{ + int nStr = (serial_type - 12) / 2; + testcase( (d1+nStr)==(unsigned)nKey1 ); + testcase( (d1+nStr+1)==(unsigned)nKey1 ); + if( (d1+nStr) > (unsigned)nKey1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corruption */ + }else{ + int nCmp = MIN(nStr, pRhs->n); + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); + if( rc==0 ) rc = nStr - pRhs->n; + } + } + } + + /* RHS is null */ + else{ + serial_type = aKey1[idx1]; + rc = (serial_type!=0); + } + + if( rc!=0 ){ + if( pKeyInfo->aSortOrder[i] ){ + rc = -rc; + } + assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); + assert( mem1.szMalloc==0 ); /* See comment below */ + return rc; + } + + i++; + pRhs++; + d1 += sqlite3VdbeSerialTypeLen(serial_type); + idx1 += sqlite3VarintLen(serial_type); + }while( idx1<(unsigned)szHdr1 && inField && d1<=(unsigned)nKey1 ); + + /* No memory allocation is ever used on mem1. Prove this using + ** the following assert(). If the assert() fails, it indicates a + ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ + assert( mem1.szMalloc==0 ); + + /* rc==0 here means that one or both of the keys ran out of fields and + ** all the fields up to that point were equal. Return the default_rc + ** value. */ + assert( CORRUPT_DB + || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) + || pKeyInfo->db->mallocFailed + ); + pPKey2->eqSeen = 1; + return pPKey2->default_rc; +} +SQLITE_PRIVATE int sqlite3VdbeRecordCompare( + int nKey1, const void *pKey1, /* Left key */ + UnpackedRecord *pPKey2 /* Right key */ +){ + return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); +} + + +/* +** This function is an optimized version of sqlite3VdbeRecordCompare() +** that (a) the first field of pPKey2 is an integer, and (b) the +** size-of-header varint at the start of (pKey1/nKey1) fits in a single +** byte (i.e. is less than 128). +** +** To avoid concerns about buffer overreads, this routine is only used +** on schemas where the maximum valid header size is 63 bytes or less. +*/ +static int vdbeRecordCompareInt( + int nKey1, const void *pKey1, /* Left key */ + UnpackedRecord *pPKey2 /* Right key */ +){ + const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; + int serial_type = ((const u8*)pKey1)[1]; + int res; + u32 y; + u64 x; + i64 v = pPKey2->aMem[0].u.i; + i64 lhs; + + vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); + assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); + switch( serial_type ){ + case 1: { /* 1-byte signed integer */ + lhs = ONE_BYTE_INT(aKey); + testcase( lhs<0 ); + break; + } + case 2: { /* 2-byte signed integer */ + lhs = TWO_BYTE_INT(aKey); + testcase( lhs<0 ); + break; + } + case 3: { /* 3-byte signed integer */ + lhs = THREE_BYTE_INT(aKey); + testcase( lhs<0 ); + break; + } + case 4: { /* 4-byte signed integer */ + y = FOUR_BYTE_UINT(aKey); + lhs = (i64)*(int*)&y; + testcase( lhs<0 ); + break; + } + case 5: { /* 6-byte signed integer */ + lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); + testcase( lhs<0 ); + break; + } + case 6: { /* 8-byte signed integer */ + x = FOUR_BYTE_UINT(aKey); + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); + lhs = *(i64*)&x; + testcase( lhs<0 ); + break; + } + case 8: + lhs = 0; + break; + case 9: + lhs = 1; + break; + + /* This case could be removed without changing the results of running + ** this code. Including it causes gcc to generate a faster switch + ** statement (since the range of switch targets now starts at zero and + ** is contiguous) but does not cause any duplicate code to be generated + ** (as gcc is clever enough to combine the two like cases). Other + ** compilers might be similar. */ + case 0: case 7: + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); + + default: + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); + } + + if( v>lhs ){ + res = pPKey2->r1; + }else if( vr2; + }else if( pPKey2->nField>1 ){ + /* The first fields of the two keys are equal. Compare the trailing + ** fields. */ + res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); + }else{ + /* The first fields of the two keys are equal and there are no trailing + ** fields. Return pPKey2->default_rc in this case. */ + res = pPKey2->default_rc; + pPKey2->eqSeen = 1; + } + + assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); + return res; +} + +/* +** This function is an optimized version of sqlite3VdbeRecordCompare() +** that (a) the first field of pPKey2 is a string, that (b) the first field +** uses the collation sequence BINARY and (c) that the size-of-header varint +** at the start of (pKey1/nKey1) fits in a single byte. +*/ +static int vdbeRecordCompareString( + int nKey1, const void *pKey1, /* Left key */ + UnpackedRecord *pPKey2 /* Right key */ +){ + const u8 *aKey1 = (const u8*)pKey1; + int serial_type; + int res; + + assert( pPKey2->aMem[0].flags & MEM_Str ); + vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); + getVarint32(&aKey1[1], serial_type); + if( serial_type<12 ){ + res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ + }else if( !(serial_type & 0x01) ){ + res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ + }else{ + int nCmp; + int nStr; + int szHdr = aKey1[0]; + + nStr = (serial_type-12) / 2; + if( (szHdr + nStr) > nKey1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corruption */ + } + nCmp = MIN( pPKey2->aMem[0].n, nStr ); + res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); + + if( res==0 ){ + res = nStr - pPKey2->aMem[0].n; + if( res==0 ){ + if( pPKey2->nField>1 ){ + res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); + }else{ + res = pPKey2->default_rc; + pPKey2->eqSeen = 1; + } + }else if( res>0 ){ + res = pPKey2->r2; + }else{ + res = pPKey2->r1; + } + }else if( res>0 ){ + res = pPKey2->r2; + }else{ + res = pPKey2->r1; + } + } + + assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) + || CORRUPT_DB + || pPKey2->pKeyInfo->db->mallocFailed + ); + return res; +} + +/* +** Return a pointer to an sqlite3VdbeRecordCompare() compatible function +** suitable for comparing serialized records to the unpacked record passed +** as the only argument. +*/ +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ + /* varintRecordCompareInt() and varintRecordCompareString() both assume + ** that the size-of-header varint that occurs at the start of each record + ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() + ** also assumes that it is safe to overread a buffer by at least the + ** maximum possible legal header size plus 8 bytes. Because there is + ** guaranteed to be at least 74 (but not 136) bytes of padding following each + ** buffer passed to varintRecordCompareInt() this makes it convenient to + ** limit the size of the header to 64 bytes in cases where the first field + ** is an integer. + ** + ** The easiest way to enforce this limit is to consider only records with + ** 13 fields or less. If the first field is an integer, the maximum legal + ** header size is (12*5 + 1 + 1) bytes. */ + if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ + int flags = p->aMem[0].flags; + if( p->pKeyInfo->aSortOrder[0] ){ + p->r1 = 1; + p->r2 = -1; + }else{ + p->r1 = -1; + p->r2 = 1; + } + if( (flags & MEM_Int) ){ + return vdbeRecordCompareInt; + } + testcase( flags & MEM_Real ); + testcase( flags & MEM_Null ); + testcase( flags & MEM_Blob ); + if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ + assert( flags & MEM_Str ); + return vdbeRecordCompareString; + } + } + + return sqlite3VdbeRecordCompare; +} + +/* +** pCur points at an index entry created using the OP_MakeRecord opcode. +** Read the rowid (the last field in the record) and store it in *rowid. +** Return SQLITE_OK if everything works, or an error code otherwise. +** +** pCur might be pointing to text obtained from a corrupt database file. +** So the content cannot be trusted. Do appropriate checks on the content. +*/ +SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ + i64 nCellKey = 0; + int rc; + u32 szHdr; /* Size of the header */ + u32 typeRowid; /* Serial type of the rowid */ + u32 lenRowid; /* Size of the rowid */ + Mem m, v; + + /* Get the size of the index entry. Only indices entries of less + ** than 2GiB are support - anything large must be database corruption. + ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so + ** this code can safely assume that nCellKey is 32-bits + */ + assert( sqlite3BtreeCursorIsValid(pCur) ); + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); + assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ + assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); + + /* Read in the complete content of the index entry */ + sqlite3VdbeMemInit(&m, db, 0); + rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); + if( rc ){ + return rc; + } + + /* The index entry must begin with a header size */ + (void)getVarint32((u8*)m.z, szHdr); + testcase( szHdr==3 ); + testcase( szHdr==m.n ); + if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ + goto idx_rowid_corruption; + } + + /* The last field of the index should be an integer - the ROWID. + ** Verify that the last entry really is an integer. */ + (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); + testcase( typeRowid==1 ); + testcase( typeRowid==2 ); + testcase( typeRowid==3 ); + testcase( typeRowid==4 ); + testcase( typeRowid==5 ); + testcase( typeRowid==6 ); + testcase( typeRowid==8 ); + testcase( typeRowid==9 ); + if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ + goto idx_rowid_corruption; + } + lenRowid = sqlite3SmallTypeSizes[typeRowid]; + testcase( (u32)m.n==szHdr+lenRowid ); + if( unlikely((u32)m.neCurType==CURTYPE_BTREE ); + pCur = pC->uc.pCursor; + assert( sqlite3BtreeCursorIsValid(pCur) ); + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); + assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ + /* nCellKey will always be between 0 and 0xffffffff because of the way + ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ + if( nCellKey<=0 || nCellKey>0x7fffffff ){ + *res = 0; + return SQLITE_CORRUPT_BKPT; + } + sqlite3VdbeMemInit(&m, db, 0); + rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); + if( rc ){ + return rc; + } + *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); + sqlite3VdbeMemRelease(&m); + return SQLITE_OK; +} + +/* +** This routine sets the value to be returned by subsequent calls to +** sqlite3_changes() on the database handle 'db'. +*/ +SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ + assert( sqlite3_mutex_held(db->mutex) ); + db->nChange = nChange; + db->nTotalChange += nChange; +} + +/* +** Set a flag in the vdbe to update the change counter when it is finalised +** or reset. +*/ +SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){ + v->changeCntOn = 1; +} + +/* +** Mark every prepared statement associated with a database connection +** as expired. +** +** An expired statement means that recompilation of the statement is +** recommend. Statements expire when things happen that make their +** programs obsolete. Removing user-defined functions or collating +** sequences, or changing an authorization function are the types of +** things that make prepared statements obsolete. +*/ +SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){ + Vdbe *p; + for(p = db->pVdbe; p; p=p->pNext){ + p->expired = 1; + } +} + +/* +** Return the database associated with the Vdbe. +*/ +SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){ + return v->db; +} + +/* +** Return a pointer to an sqlite3_value structure containing the value bound +** parameter iVar of VM v. Except, if the value is an SQL NULL, return +** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* +** constants) to the value before returning it. +** +** The returned value must be freed by the caller using sqlite3ValueFree(). +*/ +SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ + assert( iVar>0 ); + if( v ){ + Mem *pMem = &v->aVar[iVar-1]; + if( 0==(pMem->flags & MEM_Null) ){ + sqlite3_value *pRet = sqlite3ValueNew(v->db); + if( pRet ){ + sqlite3VdbeMemCopy((Mem *)pRet, pMem); + sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); + } + return pRet; + } + } + return 0; +} + +/* +** Configure SQL variable iVar so that binding a new value to it signals +** to sqlite3_reoptimize() that re-preparing the statement may result +** in a better query plan. +*/ +SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ + assert( iVar>0 ); + if( iVar>32 ){ + v->expmask = 0xffffffff; + }else{ + v->expmask |= ((u32)1 << (iVar-1)); + } +} + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored +** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored +** in memory obtained from sqlite3DbMalloc). +*/ +SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ + if( pVtab->zErrMsg ){ + sqlite3 *db = p->db; + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); + sqlite3_free(pVtab->zErrMsg); + pVtab->zErrMsg = 0; + } +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + +/* +** If the second argument is not NULL, release any allocations associated +** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord +** structure itself, using sqlite3DbFree(). +** +** This function is used to free UnpackedRecord structures allocated by +** the vdbeUnpackRecord() function found in vdbeapi.c. +*/ +static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){ + if( p ){ + int i; + for(i=0; inField; i++){ + Mem *pMem = &p->aMem[i]; + if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem); + } + sqlite3DbFree(db, p); + } +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call, +** then cursor passed as the second argument should point to the row about +** to be update or deleted. If the application calls sqlite3_preupdate_old(), +** the required value will be read from the row the cursor points to. +*/ +SQLITE_PRIVATE void sqlite3VdbePreUpdateHook( + Vdbe *v, /* Vdbe pre-update hook is invoked by */ + VdbeCursor *pCsr, /* Cursor to grab old.* values from */ + int op, /* SQLITE_INSERT, UPDATE or DELETE */ + const char *zDb, /* Database name */ + Table *pTab, /* Modified table */ + i64 iKey1, /* Initial key value */ + int iReg /* Register for new.* record */ +){ + sqlite3 *db = v->db; + i64 iKey2; + PreUpdate preupdate; + const char *zTbl = pTab->zName; + static const u8 fakeSortOrder = 0; + + assert( db->pPreUpdate==0 ); + memset(&preupdate, 0, sizeof(PreUpdate)); + if( op==SQLITE_UPDATE ){ + iKey2 = v->aMem[iReg].u.i; + }else{ + iKey2 = iKey1; + } + + assert( pCsr->nField==pTab->nCol + || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1) + ); + + preupdate.v = v; + preupdate.pCsr = pCsr; + preupdate.op = op; + preupdate.iNewReg = iReg; + preupdate.keyinfo.db = db; + preupdate.keyinfo.enc = ENC(db); + preupdate.keyinfo.nField = pTab->nCol; + preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder; + preupdate.iKey1 = iKey1; + preupdate.iKey2 = iKey2; + preupdate.iPKey = pTab->iPKey; + + db->pPreUpdate = &preupdate; + db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); + db->pPreUpdate = 0; + sqlite3DbFree(db, preupdate.aRecord); + vdbeFreeUnpacked(db, preupdate.pUnpacked); + vdbeFreeUnpacked(db, preupdate.pNewUnpacked); + if( preupdate.aNew ){ + int i; + for(i=0; inField; i++){ + sqlite3VdbeMemRelease(&preupdate.aNew[i]); + } + sqlite3DbFree(db, preupdate.aNew); + } +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +/************** End of vdbeaux.c *********************************************/ +/************** Begin file vdbeapi.c *****************************************/ +/* +** 2004 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code use to implement APIs that are part of the +** VDBE. +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** Return TRUE (non-zero) of the statement supplied as an argument needs +** to be recompiled. A statement needs to be recompiled whenever the +** execution environment changes in a way that would alter the program +** that sqlite3_prepare() generates. For example, if new functions or +** collating sequences are registered or if an authorizer function is +** added or changed. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe*)pStmt; + return p==0 || p->expired; +} +#endif + +/* +** Check on a Vdbe to make sure it has not been finalized. Log +** an error and return true if it has been finalized (or is otherwise +** invalid). Return false if it is ok. +*/ +static int vdbeSafety(Vdbe *p){ + if( p->db==0 ){ + sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); + return 1; + }else{ + return 0; + } +} +static int vdbeSafetyNotNull(Vdbe *p){ + if( p==0 ){ + sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); + return 1; + }else{ + return vdbeSafety(p); + } +} + +#ifndef SQLITE_OMIT_TRACE +/* +** Invoke the profile callback. This routine is only called if we already +** know that the profile callback is defined and needs to be invoked. +*/ +static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ + sqlite3_int64 iNow; + assert( p->startTime>0 ); + assert( db->xProfile!=0 ); + assert( db->init.busy==0 ); + assert( p->zSql!=0 ); + sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); + db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); + p->startTime = 0; +} +/* +** The checkProfileCallback(DB,P) macro checks to see if a profile callback +** is needed, and it invokes the callback if it is needed. +*/ +# define checkProfileCallback(DB,P) \ + if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } +#else +# define checkProfileCallback(DB,P) /*no-op*/ +#endif + +/* +** The following routine destroys a virtual machine that is created by +** the sqlite3_compile() routine. The integer returned is an SQLITE_ +** success/failure code that describes the result of executing the virtual +** machine. +** +** This routine sets the error code and string returned by +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){ + int rc; + if( pStmt==0 ){ + /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL + ** pointer is a harmless no-op. */ + rc = SQLITE_OK; + }else{ + Vdbe *v = (Vdbe*)pStmt; + sqlite3 *db = v->db; + if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; + sqlite3_mutex_enter(db->mutex); + checkProfileCallback(db, v); + rc = sqlite3VdbeFinalize(v); + rc = sqlite3ApiExit(db, rc); + sqlite3LeaveMutexAndCloseZombie(db); + } + return rc; +} + +/* +** Terminate the current execution of an SQL statement and reset it +** back to its starting state so that it can be reused. A success code from +** the prior execution is returned. +** +** This routine sets the error code and string returned by +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt){ + int rc; + if( pStmt==0 ){ + rc = SQLITE_OK; + }else{ + Vdbe *v = (Vdbe*)pStmt; + sqlite3 *db = v->db; + sqlite3_mutex_enter(db->mutex); + checkProfileCallback(db, v); + rc = sqlite3VdbeReset(v); + sqlite3VdbeRewind(v); + assert( (rc & (db->errMask))==rc ); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + } + return rc; +} + +/* +** Set all the parameters in the compiled SQL statement to NULL. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){ + int i; + int rc = SQLITE_OK; + Vdbe *p = (Vdbe*)pStmt; +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; +#endif + sqlite3_mutex_enter(mutex); + for(i=0; inVar; i++){ + sqlite3VdbeMemRelease(&p->aVar[i]); + p->aVar[i].flags = MEM_Null; + } + if( p->isPrepareV2 && p->expmask ){ + p->expired = 1; + } + sqlite3_mutex_leave(mutex); + return rc; +} + + +/**************************** sqlite3_value_ ******************************* +** The following routines extract information from a Mem or sqlite3_value +** structure. +*/ +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value *pVal){ + Mem *p = (Mem*)pVal; + if( p->flags & (MEM_Blob|MEM_Str) ){ + if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){ + assert( p->flags==MEM_Null && p->z==0 ); + return 0; + } + p->flags |= MEM_Blob; + return p->n ? p->z : 0; + }else{ + return sqlite3_value_text(pVal); + } +} +SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value *pVal){ + return sqlite3ValueBytes(pVal, SQLITE_UTF8); +} +SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value *pVal){ + return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); +} +SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value *pVal){ + return sqlite3VdbeRealValue((Mem*)pVal); +} +SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value *pVal){ + return (int)sqlite3VdbeIntValue((Mem*)pVal); +} +SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){ + return sqlite3VdbeIntValue((Mem*)pVal); +} +SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value *pVal){ + Mem *pMem = (Mem*)pVal; + return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); +} +SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){ + return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value* pVal){ + return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); +} +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value *pVal){ + return sqlite3ValueText(pVal, SQLITE_UTF16BE); +} +SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value *pVal){ + return sqlite3ValueText(pVal, SQLITE_UTF16LE); +} +#endif /* SQLITE_OMIT_UTF16 */ +/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five +** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating +** point number string BLOB NULL +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value* pVal){ + static const u8 aType[] = { + SQLITE_BLOB, /* 0x00 */ + SQLITE_NULL, /* 0x01 */ + SQLITE_TEXT, /* 0x02 */ + SQLITE_NULL, /* 0x03 */ + SQLITE_INTEGER, /* 0x04 */ + SQLITE_NULL, /* 0x05 */ + SQLITE_INTEGER, /* 0x06 */ + SQLITE_NULL, /* 0x07 */ + SQLITE_FLOAT, /* 0x08 */ + SQLITE_NULL, /* 0x09 */ + SQLITE_FLOAT, /* 0x0a */ + SQLITE_NULL, /* 0x0b */ + SQLITE_INTEGER, /* 0x0c */ + SQLITE_NULL, /* 0x0d */ + SQLITE_INTEGER, /* 0x0e */ + SQLITE_NULL, /* 0x0f */ + SQLITE_BLOB, /* 0x10 */ + SQLITE_NULL, /* 0x11 */ + SQLITE_TEXT, /* 0x12 */ + SQLITE_NULL, /* 0x13 */ + SQLITE_INTEGER, /* 0x14 */ + SQLITE_NULL, /* 0x15 */ + SQLITE_INTEGER, /* 0x16 */ + SQLITE_NULL, /* 0x17 */ + SQLITE_FLOAT, /* 0x18 */ + SQLITE_NULL, /* 0x19 */ + SQLITE_FLOAT, /* 0x1a */ + SQLITE_NULL, /* 0x1b */ + SQLITE_INTEGER, /* 0x1c */ + SQLITE_NULL, /* 0x1d */ + SQLITE_INTEGER, /* 0x1e */ + SQLITE_NULL, /* 0x1f */ + }; + return aType[pVal->flags&MEM_AffMask]; +} + +/* Make a copy of an sqlite3_value object +*/ +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value *pOrig){ + sqlite3_value *pNew; + if( pOrig==0 ) return 0; + pNew = sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return 0; + memset(pNew, 0, sizeof(*pNew)); + memcpy(pNew, pOrig, MEMCELLSIZE); + pNew->flags &= ~MEM_Dyn; + pNew->db = 0; + if( pNew->flags&(MEM_Str|MEM_Blob) ){ + pNew->flags &= ~(MEM_Static|MEM_Dyn); + pNew->flags |= MEM_Ephem; + if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ + sqlite3ValueFree(pNew); + pNew = 0; + } + } + return pNew; +} + +/* Destroy an sqlite3_value object previously obtained from +** sqlite3_value_dup(). +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value *pOld){ + sqlite3ValueFree(pOld); +} + + +/**************************** sqlite3_result_ ******************************* +** The following routines are used by user-defined functions to specify +** the function result. +** +** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the +** result as a string or blob but if the string or blob is too large, it +** then sets the error code to SQLITE_TOOBIG +** +** The invokeValueDestructor(P,X) routine invokes destructor function X() +** on value P is not going to be used and need to be destroyed. +*/ +static void setResultStrOrError( + sqlite3_context *pCtx, /* Function context */ + const char *z, /* String pointer */ + int n, /* Bytes in string, or negative */ + u8 enc, /* Encoding of z. 0 for BLOBs */ + void (*xDel)(void*) /* Destructor function */ +){ + if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ + sqlite3_result_error_toobig(pCtx); + } +} +static int invokeValueDestructor( + const void *p, /* Value to destroy */ + void (*xDel)(void*), /* The destructor */ + sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ +){ + assert( xDel!=SQLITE_DYNAMIC ); + if( xDel==0 ){ + /* noop */ + }else if( xDel==SQLITE_TRANSIENT ){ + /* noop */ + }else{ + xDel((void*)p); + } + if( pCtx ) sqlite3_result_error_toobig(pCtx); + return SQLITE_TOOBIG; +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_blob( + sqlite3_context *pCtx, + const void *z, + int n, + void (*xDel)(void *) +){ + assert( n>=0 ); + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + setResultStrOrError(pCtx, z, n, 0, xDel); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64( + sqlite3_context *pCtx, + const void *z, + sqlite3_uint64 n, + void (*xDel)(void *) +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + assert( xDel!=SQLITE_DYNAMIC ); + if( n>0x7fffffff ){ + (void)invokeValueDestructor(z, xDel, pCtx); + }else{ + setResultStrOrError(pCtx, z, (int)n, 0, xDel); + } +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context *pCtx, double rVal){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + pCtx->isError = SQLITE_ERROR; + pCtx->fErrorOrAux = 1; + sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + pCtx->isError = SQLITE_ERROR; + pCtx->fErrorOrAux = 1; + sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); +} +#endif +SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context *pCtx, int iVal){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetNull(pCtx->pOut); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ + Mem *pOut = pCtx->pOut; + assert( sqlite3_mutex_held(pOut->db->mutex) ); + pOut->eSubtype = eSubtype & 0xff; + pOut->flags |= MEM_Subtype; +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_text( + sqlite3_context *pCtx, + const char *z, + int n, + void (*xDel)(void *) +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_text64( + sqlite3_context *pCtx, + const char *z, + sqlite3_uint64 n, + void (*xDel)(void *), + unsigned char enc +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + assert( xDel!=SQLITE_DYNAMIC ); + if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; + if( n>0x7fffffff ){ + (void)invokeValueDestructor(z, xDel, pCtx); + }else{ + setResultStrOrError(pCtx, z, (int)n, enc, xDel); + } +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16( + sqlite3_context *pCtx, + const void *z, + int n, + void (*xDel)(void *) +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be( + sqlite3_context *pCtx, + const void *z, + int n, + void (*xDel)(void *) +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le( + sqlite3_context *pCtx, + const void *z, + int n, + void (*xDel)(void *) +){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); +} +#endif /* SQLITE_OMIT_UTF16 */ +SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemCopy(pCtx->pOut, pValue); +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); +} +SQLITE_API int SQLITE_STDCALL sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ + Mem *pOut = pCtx->pOut; + assert( sqlite3_mutex_held(pOut->db->mutex) ); + if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ + return SQLITE_TOOBIG; + } + sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); + return SQLITE_OK; +} +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ + pCtx->isError = errCode; + pCtx->fErrorOrAux = 1; +#ifdef SQLITE_DEBUG + if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; +#endif + if( pCtx->pOut->flags & MEM_Null ){ + sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, + SQLITE_UTF8, SQLITE_STATIC); + } +} + +/* Force an SQLITE_TOOBIG error. */ +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context *pCtx){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + pCtx->isError = SQLITE_TOOBIG; + pCtx->fErrorOrAux = 1; + sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, + SQLITE_UTF8, SQLITE_STATIC); +} + +/* An SQLITE_NOMEM error. */ +SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + sqlite3VdbeMemSetNull(pCtx->pOut); + pCtx->isError = SQLITE_NOMEM_BKPT; + pCtx->fErrorOrAux = 1; + sqlite3OomFault(pCtx->pOut->db); +} + +/* +** This function is called after a transaction has been committed. It +** invokes callbacks registered with sqlite3_wal_hook() as required. +*/ +static int doWalCallbacks(sqlite3 *db){ + int rc = SQLITE_OK; +#ifndef SQLITE_OMIT_WAL + int i; + for(i=0; inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + int nEntry; + sqlite3BtreeEnter(pBt); + nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); + sqlite3BtreeLeave(pBt); + if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ + rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); + } + } + } +#endif + return rc; +} + + +/* +** Execute the statement pStmt, either until a row of data is ready, the +** statement is completely executed or an error occurs. +** +** This routine implements the bulk of the logic behind the sqlite_step() +** API. The only thing omitted is the automatic recompile if a +** schema change has occurred. That detail is handled by the +** outer sqlite3_step() wrapper procedure. +*/ +static int sqlite3Step(Vdbe *p){ + sqlite3 *db; + int rc; + + assert(p); + if( p->magic!=VDBE_MAGIC_RUN ){ + /* We used to require that sqlite3_reset() be called before retrying + ** sqlite3_step() after any error or after SQLITE_DONE. But beginning + ** with version 3.7.0, we changed this so that sqlite3_reset() would + ** be called automatically instead of throwing the SQLITE_MISUSE error. + ** This "automatic-reset" change is not technically an incompatibility, + ** since any application that receives an SQLITE_MISUSE is broken by + ** definition. + ** + ** Nevertheless, some published applications that were originally written + ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE + ** returns, and those were broken by the automatic-reset change. As a + ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the + ** legacy behavior of returning SQLITE_MISUSE for cases where the + ** previous sqlite3_step() returned something other than a SQLITE_LOCKED + ** or SQLITE_BUSY error. + */ +#ifdef SQLITE_OMIT_AUTORESET + if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + sqlite3_reset((sqlite3_stmt*)p); + }else{ + return SQLITE_MISUSE_BKPT; + } +#else + sqlite3_reset((sqlite3_stmt*)p); +#endif + } + + /* Check that malloc() has not failed. If it has, return early. */ + db = p->db; + if( db->mallocFailed ){ + p->rc = SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; + } + + if( p->pc<=0 && p->expired ){ + p->rc = SQLITE_SCHEMA; + rc = SQLITE_ERROR; + goto end_of_step; + } + if( p->pc<0 ){ + /* If there are no other statements currently running, then + ** reset the interrupt flag. This prevents a call to sqlite3_interrupt + ** from interrupting a statement that has not yet started. + */ + if( db->nVdbeActive==0 ){ + db->u1.isInterrupted = 0; + } + + assert( db->nVdbeWrite>0 || db->autoCommit==0 + || (db->nDeferredCons==0 && db->nDeferredImmCons==0) + ); + +#ifndef SQLITE_OMIT_TRACE + if( db->xProfile && !db->init.busy && p->zSql ){ + sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); + }else{ + assert( p->startTime==0 ); + } +#endif + + db->nVdbeActive++; + if( p->readOnly==0 ) db->nVdbeWrite++; + if( p->bIsReader ) db->nVdbeRead++; + p->pc = 0; + } +#ifdef SQLITE_DEBUG + p->rcApp = SQLITE_OK; +#endif +#ifndef SQLITE_OMIT_EXPLAIN + if( p->explain ){ + rc = sqlite3VdbeList(p); + }else +#endif /* SQLITE_OMIT_EXPLAIN */ + { + db->nVdbeExec++; + rc = sqlite3VdbeExec(p); + db->nVdbeExec--; + } + +#ifndef SQLITE_OMIT_TRACE + /* If the statement completed successfully, invoke the profile callback */ + if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); +#endif + + if( rc==SQLITE_DONE ){ + assert( p->rc==SQLITE_OK ); + p->rc = doWalCallbacks(db); + if( p->rc!=SQLITE_OK ){ + rc = SQLITE_ERROR; + } + } + + db->errCode = rc; + if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ + p->rc = SQLITE_NOMEM_BKPT; + } +end_of_step: + /* At this point local variable rc holds the value that should be + ** returned if this statement was compiled using the legacy + ** sqlite3_prepare() interface. According to the docs, this can only + ** be one of the values in the first assert() below. Variable p->rc + ** contains the value that would be returned if sqlite3_finalize() + ** were called on statement p. + */ + assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR + || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE + ); + assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); + if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ + /* If this statement was prepared using sqlite3_prepare_v2(), and an + ** error has occurred, then return the error code in p->rc to the + ** caller. Set the error code in the database handle to the same value. + */ + rc = sqlite3VdbeTransferError(p); + } + return (rc&db->errMask); +} + +/* +** This is the top-level implementation of sqlite3_step(). Call +** sqlite3Step() to do most of the work. If a schema error occurs, +** call sqlite3Reprepare() and try again. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){ + int rc = SQLITE_OK; /* Result from sqlite3Step() */ + int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ + Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ + int cnt = 0; /* Counter to prevent infinite loop of reprepares */ + sqlite3 *db; /* The database connection */ + + if( vdbeSafetyNotNull(v) ){ + return SQLITE_MISUSE_BKPT; + } + db = v->db; + sqlite3_mutex_enter(db->mutex); + v->doingRerun = 0; + while( (rc = sqlite3Step(v))==SQLITE_SCHEMA + && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ + int savedPc = v->pc; + rc2 = rc = sqlite3Reprepare(v); + if( rc!=SQLITE_OK) break; + sqlite3_reset(pStmt); + if( savedPc>=0 ) v->doingRerun = 1; + assert( v->expired==0 ); + } + if( rc2!=SQLITE_OK ){ + /* This case occurs after failing to recompile an sql statement. + ** The error message from the SQL compiler has already been loaded + ** into the database handle. This block copies the error message + ** from the database handle into the statement and sets the statement + ** program counter to 0 to ensure that when the statement is + ** finalized or reset the parser error message is available via + ** sqlite3_errmsg() and sqlite3_errcode(). + */ + const char *zErr = (const char *)sqlite3_value_text(db->pErr); + sqlite3DbFree(db, v->zErrMsg); + if( !db->mallocFailed ){ + v->zErrMsg = sqlite3DbStrDup(db, zErr); + v->rc = rc2; + } else { + v->zErrMsg = 0; + v->rc = rc = SQLITE_NOMEM_BKPT; + } + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + + +/* +** Extract the user data from a sqlite3_context structure and return a +** pointer to it. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context *p){ + assert( p && p->pFunc ); + return p->pFunc->pUserData; +} + +/* +** Extract the user data from a sqlite3_context structure and return a +** pointer to it. +** +** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface +** returns a copy of the pointer to the database connection (the 1st +** parameter) of the sqlite3_create_function() and +** sqlite3_create_function16() routines that originally registered the +** application defined function. +*/ +SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context *p){ + assert( p && p->pOut ); + return p->pOut->db; +} + +/* +** Return the current time for a statement. If the current time +** is requested more than once within the same run of a single prepared +** statement, the exact same time is returned for each invocation regardless +** of the amount of time that elapses between invocations. In other words, +** the time returned is always the time of the first call. +*/ +SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ + int rc; +#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; + assert( p->pVdbe!=0 ); +#else + sqlite3_int64 iTime = 0; + sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; +#endif + if( *piTime==0 ){ + rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); + if( rc ) *piTime = 0; + } + return *piTime; +} + +/* +** The following is the implementation of an SQL function that always +** fails with an error message stating that the function is used in the +** wrong context. The sqlite3_overload_function() API might construct +** SQL function that use this routine so that the functions will exist +** for name resolution but are actually overloaded by the xFindFunction +** method of virtual tables. +*/ +SQLITE_PRIVATE void sqlite3InvalidFunction( + sqlite3_context *context, /* The function calling context */ + int NotUsed, /* Number of arguments to the function */ + sqlite3_value **NotUsed2 /* Value of each argument */ +){ + const char *zName = context->pFunc->zName; + char *zErr; + UNUSED_PARAMETER2(NotUsed, NotUsed2); + zErr = sqlite3_mprintf( + "unable to use function %s in the requested context", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); +} + +/* +** Create a new aggregate context for p and return a pointer to +** its pMem->z element. +*/ +static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ + Mem *pMem = p->pMem; + assert( (pMem->flags & MEM_Agg)==0 ); + if( nByte<=0 ){ + sqlite3VdbeMemSetNull(pMem); + pMem->z = 0; + }else{ + sqlite3VdbeMemClearAndResize(pMem, nByte); + pMem->flags = MEM_Agg; + pMem->u.pDef = p->pFunc; + if( pMem->z ){ + memset(pMem->z, 0, nByte); + } + } + return (void*)pMem->z; +} + +/* +** Allocate or return the aggregate context for a user function. A new +** context is allocated on the first call. Subsequent calls return the +** same context that was returned on prior calls. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, int nByte){ + assert( p && p->pFunc && p->pFunc->xFinalize ); + assert( sqlite3_mutex_held(p->pOut->db->mutex) ); + testcase( nByte<0 ); + if( (p->pMem->flags & MEM_Agg)==0 ){ + return createAggContext(p, nByte); + }else{ + return (void*)p->pMem->z; + } +} + +/* +** Return the auxiliary data pointer, if any, for the iArg'th argument to +** the user-function defined by pCtx. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ + AuxData *pAuxData; + + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); +#if SQLITE_ENABLE_STAT3_OR_STAT4 + if( pCtx->pVdbe==0 ) return 0; +#else + assert( pCtx->pVdbe!=0 ); +#endif + for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ + if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; + } + + return (pAuxData ? pAuxData->pAux : 0); +} + +/* +** Set the auxiliary data pointer and delete function, for the iArg'th +** argument to the user-function defined by pCtx. Any previous value is +** deleted by calling the delete function specified when it was set. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata( + sqlite3_context *pCtx, + int iArg, + void *pAux, + void (*xDelete)(void*) +){ + AuxData *pAuxData; + Vdbe *pVdbe = pCtx->pVdbe; + + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + if( iArg<0 ) goto failed; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( pVdbe==0 ) goto failed; +#else + assert( pVdbe!=0 ); +#endif + + for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ + if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; + } + if( pAuxData==0 ){ + pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); + if( !pAuxData ) goto failed; + pAuxData->iOp = pCtx->iOp; + pAuxData->iArg = iArg; + pAuxData->pNext = pVdbe->pAuxData; + pVdbe->pAuxData = pAuxData; + if( pCtx->fErrorOrAux==0 ){ + pCtx->isError = 0; + pCtx->fErrorOrAux = 1; + } + }else if( pAuxData->xDelete ){ + pAuxData->xDelete(pAuxData->pAux); + } + + pAuxData->pAux = pAux; + pAuxData->xDelete = xDelete; + return; + +failed: + if( xDelete ){ + xDelete(pAux); + } +} + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** Return the number of times the Step function of an aggregate has been +** called. +** +** This function is deprecated. Do not use it for new code. It is +** provide only to avoid breaking legacy code. New aggregate function +** implementations should keep their own counts within their aggregate +** context. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){ + assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); + return p->pMem->n; +} +#endif + +/* +** Return the number of columns in the result set for the statement pStmt. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt){ + Vdbe *pVm = (Vdbe *)pStmt; + return pVm ? pVm->nResColumn : 0; +} + +/* +** Return the number of values available from the current row of the +** currently executing statement pStmt. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt){ + Vdbe *pVm = (Vdbe *)pStmt; + if( pVm==0 || pVm->pResultSet==0 ) return 0; + return pVm->nResColumn; +} + +/* +** Return a pointer to static memory containing an SQL NULL value. +*/ +static const Mem *columnNullValue(void){ + /* Even though the Mem structure contains an element + ** of type i64, on certain architectures (x86) with certain compiler + ** switches (-Os), gcc may align this Mem object on a 4-byte boundary + ** instead of an 8-byte one. This all works fine, except that when + ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s + ** that a Mem structure is located on an 8-byte boundary. To prevent + ** these assert()s from failing, when building with SQLITE_DEBUG defined + ** using gcc, we force nullMem to be 8-byte aligned using the magical + ** __attribute__((aligned(8))) macro. */ + static const Mem nullMem +#if defined(SQLITE_DEBUG) && defined(__GNUC__) + __attribute__((aligned(8))) +#endif + = { + /* .u = */ {0}, + /* .flags = */ (u16)MEM_Null, + /* .enc = */ (u8)0, + /* .eSubtype = */ (u8)0, + /* .n = */ (int)0, + /* .z = */ (char*)0, + /* .zMalloc = */ (char*)0, + /* .szMalloc = */ (int)0, + /* .uTemp = */ (u32)0, + /* .db = */ (sqlite3*)0, + /* .xDel = */ (void(*)(void*))0, +#ifdef SQLITE_DEBUG + /* .pScopyFrom = */ (Mem*)0, + /* .pFiller = */ (void*)0, +#endif + }; + return &nullMem; +} + +/* +** Check to see if column iCol of the given statement is valid. If +** it is, return a pointer to the Mem for the value of that column. +** If iCol is not valid, return a pointer to a Mem which has a value +** of NULL. +*/ +static Mem *columnMem(sqlite3_stmt *pStmt, int i){ + Vdbe *pVm; + Mem *pOut; + + pVm = (Vdbe *)pStmt; + if( pVm && pVm->pResultSet!=0 && inResColumn && i>=0 ){ + sqlite3_mutex_enter(pVm->db->mutex); + pOut = &pVm->pResultSet[i]; + }else{ + if( pVm && ALWAYS(pVm->db) ){ + sqlite3_mutex_enter(pVm->db->mutex); + sqlite3Error(pVm->db, SQLITE_RANGE); + } + pOut = (Mem*)columnNullValue(); + } + return pOut; +} + +/* +** This function is called after invoking an sqlite3_value_XXX function on a +** column value (i.e. a value returned by evaluating an SQL expression in the +** select list of a SELECT statement) that may cause a malloc() failure. If +** malloc() has failed, the threads mallocFailed flag is cleared and the result +** code of statement pStmt set to SQLITE_NOMEM. +** +** Specifically, this is called from within: +** +** sqlite3_column_int() +** sqlite3_column_int64() +** sqlite3_column_text() +** sqlite3_column_text16() +** sqlite3_column_real() +** sqlite3_column_bytes() +** sqlite3_column_bytes16() +** sqiite3_column_blob() +*/ +static void columnMallocFailure(sqlite3_stmt *pStmt) +{ + /* If malloc() failed during an encoding conversion within an + ** sqlite3_column_XXX API, then set the return code of the statement to + ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR + ** and _finalize() will return NOMEM. + */ + Vdbe *p = (Vdbe *)pStmt; + if( p ){ + p->rc = sqlite3ApiExit(p->db, p->rc); + sqlite3_mutex_leave(p->db->mutex); + } +} + +/**************************** sqlite3_column_ ******************************* +** The following routines are used to access elements of the current row +** in the result set. +*/ +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ + const void *val; + val = sqlite3_value_blob( columnMem(pStmt,i) ); + /* Even though there is no encoding conversion, value_blob() might + ** need to call malloc() to expand the result of a zeroblob() + ** expression. + */ + columnMallocFailure(pStmt); + return val; +} +SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ + int val = sqlite3_value_bytes( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ + int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt *pStmt, int i){ + double val = sqlite3_value_double( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt *pStmt, int i){ + int val = sqlite3_value_int( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ + sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt *pStmt, int i){ + const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt *pStmt, int i){ + Mem *pOut = columnMem(pStmt, i); + if( pOut->flags&MEM_Static ){ + pOut->flags &= ~MEM_Static; + pOut->flags |= MEM_Ephem; + } + columnMallocFailure(pStmt); + return (sqlite3_value *)pOut; +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ + const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return val; +} +#endif /* SQLITE_OMIT_UTF16 */ +SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt *pStmt, int i){ + int iType = sqlite3_value_type( columnMem(pStmt,i) ); + columnMallocFailure(pStmt); + return iType; +} + +/* +** Convert the N-th element of pStmt->pColName[] into a string using +** xFunc() then return that string. If N is out of range, return 0. +** +** There are up to 5 names for each column. useType determines which +** name is returned. Here are the names: +** +** 0 The column name as it should be displayed for output +** 1 The datatype name for the column +** 2 The name of the database that the column derives from +** 3 The name of the table that the column derives from +** 4 The name of the table column that the result column derives from +** +** If the result is not a simple column reference (if it is an expression +** or a constant) then useTypes 2, 3, and 4 return NULL. +*/ +static const void *columnName( + sqlite3_stmt *pStmt, + int N, + const void *(*xFunc)(Mem*), + int useType +){ + const void *ret; + Vdbe *p; + int n; + sqlite3 *db; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pStmt==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + ret = 0; + p = (Vdbe *)pStmt; + db = p->db; + assert( db!=0 ); + n = sqlite3_column_count(pStmt); + if( N=0 ){ + N += useType*n; + sqlite3_mutex_enter(db->mutex); + assert( db->mallocFailed==0 ); + ret = xFunc(&p->aColName[N]); + /* A malloc may have failed inside of the xFunc() call. If this + ** is the case, clear the mallocFailed flag and return NULL. + */ + if( db->mallocFailed ){ + sqlite3OomClear(db); + ret = 0; + } + sqlite3_mutex_leave(db->mutex); + } + return ret; +} + +/* +** Return the name of the Nth column of the result set returned by SQL +** statement pStmt. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); +} +#endif + +/* +** Constraint: If you have ENABLE_COLUMN_METADATA then you must +** not define OMIT_DECLTYPE. +*/ +#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) +# error "Must not define both SQLITE_OMIT_DECLTYPE \ + and SQLITE_ENABLE_COLUMN_METADATA" +#endif + +#ifndef SQLITE_OMIT_DECLTYPE +/* +** Return the column declaration type (if applicable) of the 'i'th column +** of the result set of SQL statement pStmt. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); +} +#endif /* SQLITE_OMIT_UTF16 */ +#endif /* SQLITE_OMIT_DECLTYPE */ + +#ifdef SQLITE_ENABLE_COLUMN_METADATA +/* +** Return the name of the database from which a result column derives. +** NULL is returned if the result column is an expression or constant or +** anything else which is not an unambiguous reference to a database column. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Return the name of the table from which a result column derives. +** NULL is returned if the result column is an expression or constant or +** anything else which is not an unambiguous reference to a database column. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Return the name of the table column from which a result column derives. +** NULL is returned if the result column is an expression or constant or +** anything else which is not an unambiguous reference to a database column. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ + return columnName( + pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); +} +#endif /* SQLITE_OMIT_UTF16 */ +#endif /* SQLITE_ENABLE_COLUMN_METADATA */ + + +/******************************* sqlite3_bind_ *************************** +** +** Routines used to attach values to wildcards in a compiled SQL statement. +*/ +/* +** Unbind the value bound to variable i in virtual machine p. This is the +** the same as binding a NULL value to the column. If the "i" parameter is +** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. +** +** A successful evaluation of this routine acquires the mutex on p. +** the mutex is released if any kind of error occurs. +** +** The error code stored in database p->db is overwritten with the return +** value in any case. +*/ +static int vdbeUnbind(Vdbe *p, int i){ + Mem *pVar; + if( vdbeSafetyNotNull(p) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(p->db->mutex); + if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ + sqlite3Error(p->db, SQLITE_MISUSE); + sqlite3_mutex_leave(p->db->mutex); + sqlite3_log(SQLITE_MISUSE, + "bind on a busy prepared statement: [%s]", p->zSql); + return SQLITE_MISUSE_BKPT; + } + if( i<1 || i>p->nVar ){ + sqlite3Error(p->db, SQLITE_RANGE); + sqlite3_mutex_leave(p->db->mutex); + return SQLITE_RANGE; + } + i--; + pVar = &p->aVar[i]; + sqlite3VdbeMemRelease(pVar); + pVar->flags = MEM_Null; + sqlite3Error(p->db, SQLITE_OK); + + /* If the bit corresponding to this variable in Vdbe.expmask is set, then + ** binding a new value to this variable invalidates the current query plan. + ** + ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host + ** parameter in the WHERE clause might influence the choice of query plan + ** for a statement, then the statement will be automatically recompiled, + ** as if there had been a schema change, on the first sqlite3_step() call + ** following any change to the bindings of that parameter. + */ + if( p->isPrepareV2 && + ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) + ){ + p->expired = 1; + } + return SQLITE_OK; +} + +/* +** Bind a text or BLOB value. +*/ +static int bindText( + sqlite3_stmt *pStmt, /* The statement to bind against */ + int i, /* Index of the parameter to bind */ + const void *zData, /* Pointer to the data to be bound */ + int nData, /* Number of bytes of data to be bound */ + void (*xDel)(void*), /* Destructor for the data */ + u8 encoding /* Encoding for the data */ +){ + Vdbe *p = (Vdbe *)pStmt; + Mem *pVar; + int rc; + + rc = vdbeUnbind(p, i); + if( rc==SQLITE_OK ){ + if( zData!=0 ){ + pVar = &p->aVar[i-1]; + rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); + if( rc==SQLITE_OK && encoding!=0 ){ + rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); + } + sqlite3Error(p->db, rc); + rc = sqlite3ApiExit(p->db, rc); + } + sqlite3_mutex_leave(p->db->mutex); + }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ + xDel((void*)zData); + } + return rc; +} + + +/* +** Bind a blob value to an SQL statement variable. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob( + sqlite3_stmt *pStmt, + int i, + const void *zData, + int nData, + void (*xDel)(void*) +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( nData<0 ) return SQLITE_MISUSE_BKPT; +#endif + return bindText(pStmt, i, zData, nData, xDel, 0); +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64( + sqlite3_stmt *pStmt, + int i, + const void *zData, + sqlite3_uint64 nData, + void (*xDel)(void*) +){ + assert( xDel!=SQLITE_DYNAMIC ); + if( nData>0x7fffffff ){ + return invokeValueDestructor(zData, xDel, 0); + }else{ + return bindText(pStmt, i, zData, (int)nData, xDel, 0); + } +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ + int rc; + Vdbe *p = (Vdbe *)pStmt; + rc = vdbeUnbind(p, i); + if( rc==SQLITE_OK ){ + sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); + sqlite3_mutex_leave(p->db->mutex); + } + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ + return sqlite3_bind_int64(p, i, (i64)iValue); +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ + int rc; + Vdbe *p = (Vdbe *)pStmt; + rc = vdbeUnbind(p, i); + if( rc==SQLITE_OK ){ + sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); + sqlite3_mutex_leave(p->db->mutex); + } + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ + int rc; + Vdbe *p = (Vdbe*)pStmt; + rc = vdbeUnbind(p, i); + if( rc==SQLITE_OK ){ + sqlite3_mutex_leave(p->db->mutex); + } + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text( + sqlite3_stmt *pStmt, + int i, + const char *zData, + int nData, + void (*xDel)(void*) +){ + return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64( + sqlite3_stmt *pStmt, + int i, + const char *zData, + sqlite3_uint64 nData, + void (*xDel)(void*), + unsigned char enc +){ + assert( xDel!=SQLITE_DYNAMIC ); + if( nData>0x7fffffff ){ + return invokeValueDestructor(zData, xDel, 0); + }else{ + if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; + return bindText(pStmt, i, zData, (int)nData, xDel, enc); + } +} +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16( + sqlite3_stmt *pStmt, + int i, + const void *zData, + int nData, + void (*xDel)(void*) +){ + return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); +} +#endif /* SQLITE_OMIT_UTF16 */ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ + int rc; + switch( sqlite3_value_type((sqlite3_value*)pValue) ){ + case SQLITE_INTEGER: { + rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); + break; + } + case SQLITE_FLOAT: { + rc = sqlite3_bind_double(pStmt, i, pValue->u.r); + break; + } + case SQLITE_BLOB: { + if( pValue->flags & MEM_Zero ){ + rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); + }else{ + rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); + } + break; + } + case SQLITE_TEXT: { + rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, + pValue->enc); + break; + } + default: { + rc = sqlite3_bind_null(pStmt, i); + break; + } + } + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ + int rc; + Vdbe *p = (Vdbe *)pStmt; + rc = vdbeUnbind(p, i); + if( rc==SQLITE_OK ){ + sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); + sqlite3_mutex_leave(p->db->mutex); + } + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ + int rc; + Vdbe *p = (Vdbe *)pStmt; + sqlite3_mutex_enter(p->db->mutex); + if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ + rc = SQLITE_TOOBIG; + }else{ + assert( (n & 0x7FFFFFFF)==n ); + rc = sqlite3_bind_zeroblob(pStmt, i, n); + } + rc = sqlite3ApiExit(p->db, rc); + sqlite3_mutex_leave(p->db->mutex); + return rc; +} + +/* +** Return the number of wildcards that can be potentially bound to. +** This routine is added to support DBD::SQLite. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe*)pStmt; + return p ? p->nVar : 0; +} + +/* +** Return the name of a wildcard parameter. Return NULL if the index +** is out of range or if the wildcard is unnamed. +** +** The result is always UTF-8. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ + Vdbe *p = (Vdbe*)pStmt; + if( p==0 || i<1 || i>p->nzVar ){ + return 0; + } + return p->azVar[i-1]; +} + +/* +** Given a wildcard parameter name, return the index of the variable +** with that name. If there is no variable with the given name, +** return 0. +*/ +SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ + int i; + if( p==0 ){ + return 0; + } + if( zName ){ + for(i=0; inzVar; i++){ + const char *z = p->azVar[i]; + if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ + return i+1; + } + } + } + return 0; +} +SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ + return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); +} + +/* +** Transfer all bindings from the first statement over to the second. +*/ +SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ + Vdbe *pFrom = (Vdbe*)pFromStmt; + Vdbe *pTo = (Vdbe*)pToStmt; + int i; + assert( pTo->db==pFrom->db ); + assert( pTo->nVar==pFrom->nVar ); + sqlite3_mutex_enter(pTo->db->mutex); + for(i=0; inVar; i++){ + sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); + } + sqlite3_mutex_leave(pTo->db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** Deprecated external interface. Internal/core SQLite code +** should call sqlite3TransferBindings. +** +** It is misuse to call this routine with statements from different +** database connections. But as this is a deprecated interface, we +** will not bother to check for that condition. +** +** If the two statements contain a different number of bindings, then +** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise +** SQLITE_OK is returned. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ + Vdbe *pFrom = (Vdbe*)pFromStmt; + Vdbe *pTo = (Vdbe*)pToStmt; + if( pFrom->nVar!=pTo->nVar ){ + return SQLITE_ERROR; + } + if( pTo->isPrepareV2 && pTo->expmask ){ + pTo->expired = 1; + } + if( pFrom->isPrepareV2 && pFrom->expmask ){ + pFrom->expired = 1; + } + return sqlite3TransferBindings(pFromStmt, pToStmt); +} +#endif + +/* +** Return the sqlite3* database handle to which the prepared statement given +** in the argument belongs. This is the same database handle that was +** the first argument to the sqlite3_prepare() that was used to create +** the statement in the first place. +*/ +SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt *pStmt){ + return pStmt ? ((Vdbe*)pStmt)->db : 0; +} + +/* +** Return true if the prepared statement is guaranteed to not modify the +** database. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ + return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; +} + +/* +** Return true if the prepared statement is in need of being reset. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt *pStmt){ + Vdbe *v = (Vdbe*)pStmt; + return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN; +} + +/* +** Return a pointer to the next prepared statement after pStmt associated +** with database connection pDb. If pStmt is NULL, return the first +** prepared statement for the database connection. Return NULL if there +** are no more. +*/ +SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ + sqlite3_stmt *pNext; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(pDb) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(pDb->mutex); + if( pStmt==0 ){ + pNext = (sqlite3_stmt*)pDb->pVdbe; + }else{ + pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; + } + sqlite3_mutex_leave(pDb->mutex); + return pNext; +} + +/* +** Return the value of a status counter for a prepared statement +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ + Vdbe *pVdbe = (Vdbe*)pStmt; + u32 v; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !pStmt ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + v = pVdbe->aCounter[op]; + if( resetFlag ) pVdbe->aCounter[op] = 0; + return (int)v; +} + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Allocate and populate an UnpackedRecord structure based on the serialized +** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure +** if successful, or a NULL pointer if an OOM error is encountered. +*/ +static UnpackedRecord *vdbeUnpackRecord( + KeyInfo *pKeyInfo, + int nKey, + const void *pKey +){ + char *dummy; /* Dummy argument for AllocUnpackedRecord() */ + UnpackedRecord *pRet; /* Return value */ + + pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo, 0, 0, &dummy); + if( pRet ){ + memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1)); + sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); + } + return pRet; +} + +/* +** This function is called from within a pre-update callback to retrieve +** a field of the row currently being updated or deleted. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ + PreUpdate *p = db->pPreUpdate; + int rc = SQLITE_OK; + + /* Test that this call is being made from within an SQLITE_DELETE or + ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ + if( !p || p->op==SQLITE_INSERT ){ + rc = SQLITE_MISUSE_BKPT; + goto preupdate_old_out; + } + if( iIdx>=p->pCsr->nField || iIdx<0 ){ + rc = SQLITE_RANGE; + goto preupdate_old_out; + } + + /* If the old.* record has not yet been loaded into memory, do so now. */ + if( p->pUnpacked==0 ){ + u32 nRec; + u8 *aRec; + + rc = sqlite3BtreeDataSize(p->pCsr->uc.pCursor, &nRec); + if( rc!=SQLITE_OK ) goto preupdate_old_out; + aRec = sqlite3DbMallocRaw(db, nRec); + if( !aRec ) goto preupdate_old_out; + rc = sqlite3BtreeData(p->pCsr->uc.pCursor, 0, nRec, aRec); + if( rc==SQLITE_OK ){ + p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); + if( !p->pUnpacked ) rc = SQLITE_NOMEM; + } + if( rc!=SQLITE_OK ){ + sqlite3DbFree(db, aRec); + goto preupdate_old_out; + } + p->aRecord = aRec; + } + + if( iIdx>=p->pUnpacked->nField ){ + *ppValue = (sqlite3_value *)columnNullValue(); + }else{ + *ppValue = &p->pUnpacked->aMem[iIdx]; + if( iIdx==p->iPKey ){ + sqlite3VdbeMemSetInt64(*ppValue, p->iKey1); + } + } + + preupdate_old_out: + sqlite3Error(db, rc); + return sqlite3ApiExit(db, rc); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is called from within a pre-update callback to retrieve +** the number of columns in the row being updated, deleted or inserted. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_preupdate_count(sqlite3 *db){ + PreUpdate *p = db->pPreUpdate; + return (p ? p->keyinfo.nField : 0); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is designed to be called from within a pre-update callback +** only. It returns zero if the change that caused the callback was made +** immediately by a user SQL statement. Or, if the change was made by a +** trigger program, it returns the number of trigger programs currently +** on the stack (1 for a top-level trigger, 2 for a trigger fired by a +** top-level trigger etc.). +** +** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL +** or SET DEFAULT action is considered a trigger. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_preupdate_depth(sqlite3 *db){ + PreUpdate *p = db->pPreUpdate; + return (p ? p->v->nFrame : 0); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is called from within a pre-update callback to retrieve +** a field of the row currently being updated or inserted. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ + PreUpdate *p = db->pPreUpdate; + int rc = SQLITE_OK; + Mem *pMem; + + if( !p || p->op==SQLITE_DELETE ){ + rc = SQLITE_MISUSE_BKPT; + goto preupdate_new_out; + } + if( iIdx>=p->pCsr->nField || iIdx<0 ){ + rc = SQLITE_RANGE; + goto preupdate_new_out; + } + + if( p->op==SQLITE_INSERT ){ + /* For an INSERT, memory cell p->iNewReg contains the serialized record + ** that is being inserted. Deserialize it. */ + UnpackedRecord *pUnpack = p->pNewUnpacked; + if( !pUnpack ){ + Mem *pData = &p->v->aMem[p->iNewReg]; + rc = sqlite3VdbeMemExpandBlob(pData); + if( rc!=SQLITE_OK ) goto preupdate_new_out; + pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); + if( !pUnpack ){ + rc = SQLITE_NOMEM; + goto preupdate_new_out; + } + p->pNewUnpacked = pUnpack; + } + if( iIdx>=pUnpack->nField ){ + pMem = (sqlite3_value *)columnNullValue(); + }else{ + pMem = &pUnpack->aMem[iIdx]; + if( iIdx==p->iPKey ){ + sqlite3VdbeMemSetInt64(pMem, p->iKey2); + } + } + }else{ + /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required + ** value. Make a copy of the cell contents and return a pointer to it. + ** It is not safe to return a pointer to the memory cell itself as the + ** caller may modify the value text encoding. + */ + assert( p->op==SQLITE_UPDATE ); + if( !p->aNew ){ + p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); + if( !p->aNew ){ + rc = SQLITE_NOMEM; + goto preupdate_new_out; + } + } + assert( iIdx>=0 && iIdxpCsr->nField ); + pMem = &p->aNew[iIdx]; + if( pMem->flags==0 ){ + if( iIdx==p->iPKey ){ + sqlite3VdbeMemSetInt64(pMem, p->iKey2); + }else{ + rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); + if( rc!=SQLITE_OK ) goto preupdate_new_out; + } + } + } + *ppValue = pMem; + + preupdate_new_out: + sqlite3Error(db, rc); + return sqlite3ApiExit(db, rc); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +/* +** Return status data for a single loop within query pStmt. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus( + sqlite3_stmt *pStmt, /* Prepared statement being queried */ + int idx, /* Index of loop to report on */ + int iScanStatusOp, /* Which metric to return */ + void *pOut /* OUT: Write the answer here */ +){ + Vdbe *p = (Vdbe*)pStmt; + ScanStatus *pScan; + if( idx<0 || idx>=p->nScan ) return 1; + pScan = &p->aScan[idx]; + switch( iScanStatusOp ){ + case SQLITE_SCANSTAT_NLOOP: { + *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; + break; + } + case SQLITE_SCANSTAT_NVISIT: { + *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; + break; + } + case SQLITE_SCANSTAT_EST: { + double r = 1.0; + LogEst x = pScan->nEst; + while( x<100 ){ + x += 10; + r *= 0.5; + } + *(double*)pOut = r*sqlite3LogEstToInt(x); + break; + } + case SQLITE_SCANSTAT_NAME: { + *(const char**)pOut = pScan->zName; + break; + } + case SQLITE_SCANSTAT_EXPLAIN: { + if( pScan->addrExplain ){ + *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; + }else{ + *(const char**)pOut = 0; + } + break; + } + case SQLITE_SCANSTAT_SELECTID: { + if( pScan->addrExplain ){ + *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; + }else{ + *(int*)pOut = -1; + } + break; + } + default: { + return 1; + } + } + return 0; +} + +/* +** Zero all counters associated with the sqlite3_stmt_scanstatus() data. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe*)pStmt; + memset(p->anExec, 0, p->nOp * sizeof(i64)); +} +#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ + +/************** End of vdbeapi.c *********************************************/ +/************** Begin file vdbetrace.c ***************************************/ +/* +** 2009 November 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code used to insert the values of host parameters +** (aka "wildcards") into the SQL text output by sqlite3_trace(). +** +** The Vdbe parse-tree explainer is also found here. +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +#ifndef SQLITE_OMIT_TRACE + +/* +** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of +** bytes in this text up to but excluding the first character in +** a host parameter. If the text contains no host parameters, return +** the total number of bytes in the text. +*/ +static int findNextHostParameter(const char *zSql, int *pnToken){ + int tokenType; + int nTotal = 0; + int n; + + *pnToken = 0; + while( zSql[0] ){ + n = sqlite3GetToken((u8*)zSql, &tokenType); + assert( n>0 && tokenType!=TK_ILLEGAL ); + if( tokenType==TK_VARIABLE ){ + *pnToken = n; + break; + } + nTotal += n; + zSql += n; + } + return nTotal; +} + +/* +** This function returns a pointer to a nul-terminated string in memory +** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the +** string contains a copy of zRawSql but with host parameters expanded to +** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1, +** then the returned string holds a copy of zRawSql with "-- " prepended +** to each line of text. +** +** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then +** then long strings and blobs are truncated to that many bytes. This +** can be used to prevent unreasonably large trace strings when dealing +** with large (multi-megabyte) strings and blobs. +** +** The calling function is responsible for making sure the memory returned +** is eventually freed. +** +** ALGORITHM: Scan the input string looking for host parameters in any of +** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within +** string literals, quoted identifier names, and comments. For text forms, +** the host parameter index is found by scanning the prepared +** statement for the corresponding OP_Variable opcode. Once the host +** parameter index is known, locate the value in p->aVar[]. Then render +** the value as a literal in place of the host parameter name. +*/ +SQLITE_PRIVATE char *sqlite3VdbeExpandSql( + Vdbe *p, /* The prepared statement being evaluated */ + const char *zRawSql /* Raw text of the SQL statement */ +){ + sqlite3 *db; /* The database connection */ + int idx = 0; /* Index of a host parameter */ + int nextIndex = 1; /* Index of next ? host parameter */ + int n; /* Length of a token prefix */ + int nToken; /* Length of the parameter token */ + int i; /* Loop counter */ + Mem *pVar; /* Value of a host parameter */ + StrAccum out; /* Accumulate the output here */ + char zBase[100]; /* Initial working space */ + + db = p->db; + sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), + db->aLimit[SQLITE_LIMIT_LENGTH]); + if( db->nVdbeExec>1 ){ + while( *zRawSql ){ + const char *zStart = zRawSql; + while( *(zRawSql++)!='\n' && *zRawSql ); + sqlite3StrAccumAppend(&out, "-- ", 3); + assert( (zRawSql - zStart) > 0 ); + sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart)); + } + }else if( p->nVar==0 ){ + sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql)); + }else{ + while( zRawSql[0] ){ + n = findNextHostParameter(zRawSql, &nToken); + assert( n>0 ); + sqlite3StrAccumAppend(&out, zRawSql, n); + zRawSql += n; + assert( zRawSql[0] || nToken==0 ); + if( nToken==0 ) break; + if( zRawSql[0]=='?' ){ + if( nToken>1 ){ + assert( sqlite3Isdigit(zRawSql[1]) ); + sqlite3GetInt32(&zRawSql[1], &idx); + }else{ + idx = nextIndex; + } + }else{ + assert( zRawSql[0]==':' || zRawSql[0]=='$' || + zRawSql[0]=='@' || zRawSql[0]=='#' ); + testcase( zRawSql[0]==':' ); + testcase( zRawSql[0]=='$' ); + testcase( zRawSql[0]=='@' ); + testcase( zRawSql[0]=='#' ); + idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); + assert( idx>0 ); + } + zRawSql += nToken; + nextIndex = idx + 1; + assert( idx>0 && idx<=p->nVar ); + pVar = &p->aVar[idx-1]; + if( pVar->flags & MEM_Null ){ + sqlite3StrAccumAppend(&out, "NULL", 4); + }else if( pVar->flags & MEM_Int ){ + sqlite3XPrintf(&out, "%lld", pVar->u.i); + }else if( pVar->flags & MEM_Real ){ + sqlite3XPrintf(&out, "%!.15g", pVar->u.r); + }else if( pVar->flags & MEM_Str ){ + int nOut; /* Number of bytes of the string text to include in output */ +#ifndef SQLITE_OMIT_UTF16 + u8 enc = ENC(db); + Mem utf8; + if( enc!=SQLITE_UTF8 ){ + memset(&utf8, 0, sizeof(utf8)); + utf8.db = db; + sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC); + sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8); + pVar = &utf8; + } +#endif + nOut = pVar->n; +#ifdef SQLITE_TRACE_SIZE_LIMIT + if( nOut>SQLITE_TRACE_SIZE_LIMIT ){ + nOut = SQLITE_TRACE_SIZE_LIMIT; + while( nOutn && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } + } +#endif + sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z); +#ifdef SQLITE_TRACE_SIZE_LIMIT + if( nOutn ){ + sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); + } +#endif +#ifndef SQLITE_OMIT_UTF16 + if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); +#endif + }else if( pVar->flags & MEM_Zero ){ + sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero); + }else{ + int nOut; /* Number of bytes of the blob to include in output */ + assert( pVar->flags & MEM_Blob ); + sqlite3StrAccumAppend(&out, "x'", 2); + nOut = pVar->n; +#ifdef SQLITE_TRACE_SIZE_LIMIT + if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT; +#endif + for(i=0; iz[i]&0xff); + } + sqlite3StrAccumAppend(&out, "'", 1); +#ifdef SQLITE_TRACE_SIZE_LIMIT + if( nOutn ){ + sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); + } +#endif + } + } + } + return sqlite3StrAccumFinish(&out); +} + +#endif /* #ifndef SQLITE_OMIT_TRACE */ + +/************** End of vdbetrace.c *******************************************/ +/************** Begin file vdbe.c ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** The code in this file implements the function that runs the +** bytecode of a prepared statement. +** +** Various scripts scan this source file in order to generate HTML +** documentation, headers files, or other derived files. The formatting +** of the code in this file is, therefore, important. See other comments +** in this file for details. If in doubt, do not deviate from existing +** commenting and indentation practices when changing or adding code. +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +/* +** Invoke this macro on memory cells just prior to changing the +** value of the cell. This macro verifies that shallow copies are +** not misused. A shallow copy of a string or blob just copies a +** pointer to the string or blob, not the content. If the original +** is changed while the copy is still in use, the string or blob might +** be changed out from under the copy. This macro verifies that nothing +** like that ever happens. +*/ +#ifdef SQLITE_DEBUG +# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) +#else +# define memAboutToChange(P,M) +#endif + +/* +** The following global variable is incremented every time a cursor +** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test +** procedures use this information to make sure that indices are +** working correctly. This variable has no function other than to +** help verify the correct operation of the library. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_search_count = 0; +#endif + +/* +** When this global variable is positive, it gets decremented once before +** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted +** field of the sqlite3 structure is set in order to simulate an interrupt. +** +** This facility is used for testing purposes only. It does not function +** in an ordinary build. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_interrupt_count = 0; +#endif + +/* +** The next global variable is incremented each type the OP_Sort opcode +** is executed. The test procedures use this information to make sure that +** sorting is occurring or not occurring at appropriate times. This variable +** has no function other than to help verify the correct operation of the +** library. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_sort_count = 0; +#endif + +/* +** The next global variable records the size of the largest MEM_Blob +** or MEM_Str that has been used by a VDBE opcode. The test procedures +** use this information to make sure that the zero-blob functionality +** is working correctly. This variable has no function other than to +** help verify the correct operation of the library. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_max_blobsize = 0; +static void updateMaxBlobsize(Mem *p){ + if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ + sqlite3_max_blobsize = p->n; + } +} +#endif + +/* +** This macro evaluates to true if either the update hook or the preupdate +** hook are enabled for database connect DB. +*/ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) +#else +# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) +#endif + +/* +** The next global variable is incremented each time the OP_Found opcode +** is executed. This is used to test whether or not the foreign key +** operation implemented using OP_FkIsZero is working. This variable +** has no function other than to help verify the correct operation of the +** library. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_found_count = 0; +#endif + +/* +** Test a register to see if it exceeds the current maximum blob size. +** If it does, record the new maximum blob size. +*/ +#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) +# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) +#else +# define UPDATE_MAX_BLOBSIZE(P) +#endif + +/* +** Invoke the VDBE coverage callback, if that callback is defined. This +** feature is used for test suite validation only and does not appear an +** production builds. +** +** M is an integer, 2 or 3, that indices how many different ways the +** branch can go. It is usually 2. "I" is the direction the branch +** goes. 0 means falls through. 1 means branch is taken. 2 means the +** second alternative branch is taken. +** +** iSrcLine is the source code line (from the __LINE__ macro) that +** generated the VDBE instruction. This instrumentation assumes that all +** source code is in a single file (the amalgamation). Special values 1 +** and 2 for the iSrcLine parameter mean that this particular branch is +** always taken or never taken, respectively. +*/ +#if !defined(SQLITE_VDBE_COVERAGE) +# define VdbeBranchTaken(I,M) +#else +# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) + static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ + if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ + M = iSrcLine; + /* Assert the truth of VdbeCoverageAlwaysTaken() and + ** VdbeCoverageNeverTaken() */ + assert( (M & I)==I ); + }else{ + if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ + sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, + iSrcLine,I,M); + } + } +#endif + +/* +** Convert the given register into a string if it isn't one +** already. Return non-zero if a malloc() fails. +*/ +#define Stringify(P, enc) \ + if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ + { goto no_mem; } + +/* +** An ephemeral string value (signified by the MEM_Ephem flag) contains +** a pointer to a dynamically allocated string where some other entity +** is responsible for deallocating that string. Because the register +** does not control the string, it might be deleted without the register +** knowing it. +** +** This routine converts an ephemeral string into a dynamically allocated +** string that the register itself controls. In other words, it +** converts an MEM_Ephem string into a string with P.z==P.zMalloc. +*/ +#define Deephemeralize(P) \ + if( ((P)->flags&MEM_Ephem)!=0 \ + && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} + +/* Return true if the cursor was opened using the OP_OpenSorter opcode. */ +#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) + +/* +** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL +** if we run out of memory. +*/ +static VdbeCursor *allocateCursor( + Vdbe *p, /* The virtual machine */ + int iCur, /* Index of the new VdbeCursor */ + int nField, /* Number of fields in the table or index */ + int iDb, /* Database the cursor belongs to, or -1 */ + u8 eCurType /* Type of the new cursor */ +){ + /* Find the memory cell that will be used to store the blob of memory + ** required for this VdbeCursor structure. It is convenient to use a + ** vdbe memory cell to manage the memory allocation required for a + ** VdbeCursor structure for the following reasons: + ** + ** * Sometimes cursor numbers are used for a couple of different + ** purposes in a vdbe program. The different uses might require + ** different sized allocations. Memory cells provide growable + ** allocations. + ** + ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can + ** be freed lazily via the sqlite3_release_memory() API. This + ** minimizes the number of malloc calls made by the system. + ** + ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from + ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. + ** Cursor 2 is at Mem[p->nMem-2]. And so forth. + */ + Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; + + int nByte; + VdbeCursor *pCx = 0; + nByte = + ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + + (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); + + assert( iCur>=0 && iCurnCursor ); + if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ + sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); + p->apCsr[iCur] = 0; + } + if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ + p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; + memset(pCx, 0, sizeof(VdbeCursor)); + pCx->eCurType = eCurType; + pCx->iDb = iDb; + pCx->nField = nField; + pCx->aOffset = &pCx->aType[nField]; + if( eCurType==CURTYPE_BTREE ){ + pCx->uc.pCursor = (BtCursor*) + &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; + sqlite3BtreeCursorZero(pCx->uc.pCursor); + } + } + return pCx; +} + +/* +** Try to convert a value into a numeric representation if we can +** do so without loss of information. In other words, if the string +** looks like a number, convert it into a number. If it does not +** look like a number, leave it alone. +** +** If the bTryForInt flag is true, then extra effort is made to give +** an integer representation. Strings that look like floating point +** values but which have no fractional component (example: '48.00') +** will have a MEM_Int representation when bTryForInt is true. +** +** If bTryForInt is false, then if the input string contains a decimal +** point or exponential notation, the result is only MEM_Real, even +** if there is an exact integer representation of the quantity. +*/ +static void applyNumericAffinity(Mem *pRec, int bTryForInt){ + double rValue; + i64 iValue; + u8 enc = pRec->enc; + assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); + if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; + if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ + pRec->u.i = iValue; + pRec->flags |= MEM_Int; + }else{ + pRec->u.r = rValue; + pRec->flags |= MEM_Real; + if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); + } +} + +/* +** Processing is determine by the affinity parameter: +** +** SQLITE_AFF_INTEGER: +** SQLITE_AFF_REAL: +** SQLITE_AFF_NUMERIC: +** Try to convert pRec to an integer representation or a +** floating-point representation if an integer representation +** is not possible. Note that the integer representation is +** always preferred, even if the affinity is REAL, because +** an integer representation is more space efficient on disk. +** +** SQLITE_AFF_TEXT: +** Convert pRec to a text representation. +** +** SQLITE_AFF_BLOB: +** No-op. pRec is unchanged. +*/ +static void applyAffinity( + Mem *pRec, /* The value to apply affinity to */ + char affinity, /* The affinity to be applied */ + u8 enc /* Use this text encoding */ +){ + if( affinity>=SQLITE_AFF_NUMERIC ){ + assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL + || affinity==SQLITE_AFF_NUMERIC ); + if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + if( (pRec->flags & MEM_Real)==0 ){ + if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); + }else{ + sqlite3VdbeIntegerAffinity(pRec); + } + } + }else if( affinity==SQLITE_AFF_TEXT ){ + /* Only attempt the conversion to TEXT if there is an integer or real + ** representation (blob and NULL do not get converted) but no string + ** representation. It would be harmless to repeat the conversion if + ** there is already a string rep, but it is pointless to waste those + ** CPU cycles. */ + if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ + if( (pRec->flags&(MEM_Real|MEM_Int)) ){ + sqlite3VdbeMemStringify(pRec, enc, 1); + } + } + pRec->flags &= ~(MEM_Real|MEM_Int); + } +} + +/* +** Try to convert the type of a function argument or a result column +** into a numeric representation. Use either INTEGER or REAL whichever +** is appropriate. But only do the conversion if it is possible without +** loss of information and return the revised type of the argument. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value *pVal){ + int eType = sqlite3_value_type(pVal); + if( eType==SQLITE_TEXT ){ + Mem *pMem = (Mem*)pVal; + applyNumericAffinity(pMem, 0); + eType = sqlite3_value_type(pVal); + } + return eType; +} + +/* +** Exported version of applyAffinity(). This one works on sqlite3_value*, +** not the internal Mem* type. +*/ +SQLITE_PRIVATE void sqlite3ValueApplyAffinity( + sqlite3_value *pVal, + u8 affinity, + u8 enc +){ + applyAffinity((Mem *)pVal, affinity, enc); +} + +/* +** pMem currently only holds a string type (or maybe a BLOB that we can +** interpret as a string if we want to). Compute its corresponding +** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields +** accordingly. +*/ +static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ + assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); + assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); + if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ + return 0; + } + if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ + return MEM_Int; + } + return MEM_Real; +} + +/* +** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or +** none. +** +** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. +** But it does set pMem->u.r and pMem->u.i appropriately. +*/ +static u16 numericType(Mem *pMem){ + if( pMem->flags & (MEM_Int|MEM_Real) ){ + return pMem->flags & (MEM_Int|MEM_Real); + } + if( pMem->flags & (MEM_Str|MEM_Blob) ){ + return computeNumericType(pMem); + } + return 0; +} + +#ifdef SQLITE_DEBUG +/* +** Write a nice string representation of the contents of cell pMem +** into buffer zBuf, length nBuf. +*/ +SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ + char *zCsr = zBuf; + int f = pMem->flags; + + static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; + + if( f&MEM_Blob ){ + int i; + char c; + if( f & MEM_Dyn ){ + c = 'z'; + assert( (f & (MEM_Static|MEM_Ephem))==0 ); + }else if( f & MEM_Static ){ + c = 't'; + assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); + }else if( f & MEM_Ephem ){ + c = 'e'; + assert( (f & (MEM_Static|MEM_Dyn))==0 ); + }else{ + c = 's'; + } + + sqlite3_snprintf(100, zCsr, "%c", c); + zCsr += sqlite3Strlen30(zCsr); + sqlite3_snprintf(100, zCsr, "%d[", pMem->n); + zCsr += sqlite3Strlen30(zCsr); + for(i=0; i<16 && in; i++){ + sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); + zCsr += sqlite3Strlen30(zCsr); + } + for(i=0; i<16 && in; i++){ + char z = pMem->z[i]; + if( z<32 || z>126 ) *zCsr++ = '.'; + else *zCsr++ = z; + } + + sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); + zCsr += sqlite3Strlen30(zCsr); + if( f & MEM_Zero ){ + sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); + zCsr += sqlite3Strlen30(zCsr); + } + *zCsr = '\0'; + }else if( f & MEM_Str ){ + int j, k; + zBuf[0] = ' '; + if( f & MEM_Dyn ){ + zBuf[1] = 'z'; + assert( (f & (MEM_Static|MEM_Ephem))==0 ); + }else if( f & MEM_Static ){ + zBuf[1] = 't'; + assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); + }else if( f & MEM_Ephem ){ + zBuf[1] = 'e'; + assert( (f & (MEM_Static|MEM_Dyn))==0 ); + }else{ + zBuf[1] = 's'; + } + k = 2; + sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); + k += sqlite3Strlen30(&zBuf[k]); + zBuf[k++] = '['; + for(j=0; j<15 && jn; j++){ + u8 c = pMem->z[j]; + if( c>=0x20 && c<0x7f ){ + zBuf[k++] = c; + }else{ + zBuf[k++] = '.'; + } + } + zBuf[k++] = ']'; + sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); + k += sqlite3Strlen30(&zBuf[k]); + zBuf[k++] = 0; + } +} +#endif + +#ifdef SQLITE_DEBUG +/* +** Print the value of a register for tracing purposes: +*/ +static void memTracePrint(Mem *p){ + if( p->flags & MEM_Undefined ){ + printf(" undefined"); + }else if( p->flags & MEM_Null ){ + printf(" NULL"); + }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ + printf(" si:%lld", p->u.i); + }else if( p->flags & MEM_Int ){ + printf(" i:%lld", p->u.i); +#ifndef SQLITE_OMIT_FLOATING_POINT + }else if( p->flags & MEM_Real ){ + printf(" r:%g", p->u.r); +#endif + }else if( p->flags & MEM_RowSet ){ + printf(" (rowset)"); + }else{ + char zBuf[200]; + sqlite3VdbeMemPrettyPrint(p, zBuf); + printf(" %s", zBuf); + } + if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); +} +static void registerTrace(int iReg, Mem *p){ + printf("REG[%d] = ", iReg); + memTracePrint(p); + printf("\n"); +} +#endif + +#ifdef SQLITE_DEBUG +# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) +#else +# define REGISTER_TRACE(R,M) +#endif + + +#ifdef VDBE_PROFILE + +/* +** hwtime.h contains inline assembler code for implementing +** high-performance timing routines. +*/ +/************** Include hwtime.h in the middle of vdbe.c *********************/ +/************** Begin file hwtime.h ******************************************/ +/* +** 2008 May 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains inline asm code for retrieving "high-performance" +** counters for x86 class CPUs. +*/ +#ifndef _HWTIME_H_ +#define _HWTIME_H_ + +/* +** The following routine only works on pentium-class (or newer) processors. +** It uses the RDTSC opcode to read the cycle count value out of the +** processor and returns that value. This can be used for high-res +** profiling. +*/ +#if (defined(__GNUC__) || defined(_MSC_VER)) && \ + (defined(i386) || defined(__i386__) || defined(_M_IX86)) + + #if defined(__GNUC__) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned int lo, hi; + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return (sqlite_uint64)hi << 32 | lo; + } + + #elif defined(_MSC_VER) + + __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ + __asm { + rdtsc + ret ; return value at EDX:EAX + } + } + + #endif + +#elif (defined(__GNUC__) && defined(__x86_64__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long val; + __asm__ __volatile__ ("rdtsc" : "=A" (val)); + return val; + } + +#elif (defined(__GNUC__) && defined(__ppc__)) + + __inline__ sqlite_uint64 sqlite3Hwtime(void){ + unsigned long long retval; + unsigned long junk; + __asm__ __volatile__ ("\n\ + 1: mftbu %1\n\ + mftb %L0\n\ + mftbu %0\n\ + cmpw %0,%1\n\ + bne 1b" + : "=r" (retval), "=r" (junk)); + return retval; + } + +#else + + #error Need implementation of sqlite3Hwtime() for your platform. + + /* + ** To compile without implementing sqlite3Hwtime() for your platform, + ** you can remove the above #error and use the following + ** stub function. You will lose timing support for many + ** of the debugging and testing utilities, but it should at + ** least compile and run. + */ +SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + +#endif + +#endif /* !defined(_HWTIME_H_) */ + +/************** End of hwtime.h **********************************************/ +/************** Continuing where we left off in vdbe.c ***********************/ + +#endif + +#ifndef NDEBUG +/* +** This function is only called from within an assert() expression. It +** checks that the sqlite3.nTransaction variable is correctly set to +** the number of non-transaction savepoints currently in the +** linked list starting at sqlite3.pSavepoint. +** +** Usage: +** +** assert( checkSavepointCount(db) ); +*/ +static int checkSavepointCount(sqlite3 *db){ + int n = 0; + Savepoint *p; + for(p=db->pSavepoint; p; p=p->pNext) n++; + assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); + return 1; +} +#endif + +/* +** Return the register of pOp->p2 after first preparing it to be +** overwritten with an integer value. +*/ +static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ + sqlite3VdbeMemSetNull(pOut); + pOut->flags = MEM_Int; + return pOut; +} +static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ + Mem *pOut; + assert( pOp->p2>0 ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); + pOut = &p->aMem[pOp->p2]; + memAboutToChange(p, pOut); + if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ + return out2PrereleaseWithClear(pOut); + }else{ + pOut->flags = MEM_Int; + return pOut; + } +} + + +/* +** Execute as much of a VDBE program as we can. +** This is the core of sqlite3_step(). +*/ +SQLITE_PRIVATE int sqlite3VdbeExec( + Vdbe *p /* The VDBE */ +){ + Op *aOp = p->aOp; /* Copy of p->aOp */ + Op *pOp = aOp; /* Current operation */ +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) + Op *pOrigOp; /* Value of pOp at the top of the loop */ +#endif +#ifdef SQLITE_DEBUG + int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ +#endif + int rc = SQLITE_OK; /* Value to return */ + sqlite3 *db = p->db; /* The database */ + u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ + u8 encoding = ENC(db); /* The database encoding */ + int iCompare = 0; /* Result of last OP_Compare operation */ + unsigned nVmStep = 0; /* Number of virtual machine steps */ +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ +#endif + Mem *aMem = p->aMem; /* Copy of p->aMem */ + Mem *pIn1 = 0; /* 1st input operand */ + Mem *pIn2 = 0; /* 2nd input operand */ + Mem *pIn3 = 0; /* 3rd input operand */ + Mem *pOut = 0; /* Output operand */ + int *aPermute = 0; /* Permutation of columns for OP_Compare */ + i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ +#ifdef VDBE_PROFILE + u64 start; /* CPU clock count at start of opcode */ +#endif + /*** INSERT STACK UNION HERE ***/ + + assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ + sqlite3VdbeEnter(p); + if( p->rc==SQLITE_NOMEM ){ + /* This happens if a malloc() inside a call to sqlite3_column_text() or + ** sqlite3_column_text16() failed. */ + goto no_mem; + } + assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); + assert( p->bIsReader || p->readOnly!=0 ); + p->rc = SQLITE_OK; + p->iCurrentTime = 0; + assert( p->explain==0 ); + p->pResultSet = 0; + db->busyHandler.nBusy = 0; + if( db->u1.isInterrupted ) goto abort_due_to_interrupt; + sqlite3VdbeIOTraceSql(p); +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( db->xProgress ){ + u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; + assert( 0 < db->nProgressOps ); + nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); + } +#endif +#ifdef SQLITE_DEBUG + sqlite3BeginBenignMalloc(); + if( p->pc==0 + && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 + ){ + int i; + int once = 1; + sqlite3VdbePrintSql(p); + if( p->db->flags & SQLITE_VdbeListing ){ + printf("VDBE Program Listing:\n"); + for(i=0; inOp; i++){ + sqlite3VdbePrintOp(stdout, i, &aOp[i]); + } + } + if( p->db->flags & SQLITE_VdbeEQP ){ + for(i=0; inOp; i++){ + if( aOp[i].opcode==OP_Explain ){ + if( once ) printf("VDBE Query Plan:\n"); + printf("%s\n", aOp[i].p4.z); + once = 0; + } + } + } + if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); + } + sqlite3EndBenignMalloc(); +#endif + for(pOp=&aOp[p->pc]; 1; pOp++){ + /* Errors are detected by individual opcodes, with an immediate + ** jumps to abort_due_to_error. */ + assert( rc==SQLITE_OK ); + + assert( pOp>=aOp && pOp<&aOp[p->nOp]); +#ifdef VDBE_PROFILE + start = sqlite3Hwtime(); +#endif + nVmStep++; +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; +#endif + + /* Only allow tracing if SQLITE_DEBUG is defined. + */ +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_VdbeTrace ){ + sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); + } +#endif + + + /* Check to see if we need to simulate an interrupt. This only happens + ** if we have a special test build. + */ +#ifdef SQLITE_TEST + if( sqlite3_interrupt_count>0 ){ + sqlite3_interrupt_count--; + if( sqlite3_interrupt_count==0 ){ + sqlite3_interrupt(db); + } + } +#endif + + /* Sanity checking on other operands */ +#ifdef SQLITE_DEBUG + { + u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; + if( (opProperty & OPFLG_IN1)!=0 ){ + assert( pOp->p1>0 ); + assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p1]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); + REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); + } + if( (opProperty & OPFLG_IN2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p2]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); + REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); + } + if( (opProperty & OPFLG_IN3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p3]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); + REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); + } + if( (opProperty & OPFLG_OUT2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); + memAboutToChange(p, &aMem[pOp->p2]); + } + if( (opProperty & OPFLG_OUT3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + memAboutToChange(p, &aMem[pOp->p3]); + } + } +#endif +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) + pOrigOp = pOp; +#endif + + switch( pOp->opcode ){ + +/***************************************************************************** +** What follows is a massive switch statement where each case implements a +** separate instruction in the virtual machine. If we follow the usual +** indentation conventions, each case should be indented by 6 spaces. But +** that is a lot of wasted space on the left margin. So the code within +** the switch statement will break with convention and be flush-left. Another +** big comment (similar to this one) will mark the point in the code where +** we transition back to normal indentation. +** +** The formatting of each case is important. The makefile for SQLite +** generates two C files "opcodes.h" and "opcodes.c" by scanning this +** file looking for lines that begin with "case OP_". The opcodes.h files +** will be filled with #defines that give unique integer values to each +** opcode and the opcodes.c file is filled with an array of strings where +** each string is the symbolic name for the corresponding opcode. If the +** case statement is followed by a comment of the form "/# same as ... #/" +** that comment is used to determine the particular value of the opcode. +** +** Other keywords in the comment that follows each case are used to +** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. +** Keywords include: in1, in2, in3, out2, out3. See +** the mkopcodeh.awk script for additional information. +** +** Documentation about VDBE opcodes is generated by scanning this file +** for lines of that contain "Opcode:". That line and all subsequent +** comment lines are used in the generation of the opcode.html documentation +** file. +** +** SUMMARY: +** +** Formatting is important to scripts that scan this file. +** Do not deviate from the formatting style currently in use. +** +*****************************************************************************/ + +/* Opcode: Goto * P2 * * * +** +** An unconditional jump to address P2. +** The next instruction executed will be +** the one at index P2 from the beginning of +** the program. +** +** The P1 parameter is not actually used by this opcode. However, it +** is sometimes set to 1 instead of 0 as a hint to the command-line shell +** that this Goto is the bottom of a loop and that the lines from P2 down +** to the current line should be indented for EXPLAIN output. +*/ +case OP_Goto: { /* jump */ +jump_to_p2_and_check_for_interrupt: + pOp = &aOp[pOp->p2 - 1]; + + /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, + ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon + ** completion. Check to see if sqlite3_interrupt() has been called + ** or if the progress callback needs to be invoked. + ** + ** This code uses unstructured "goto" statements and does not look clean. + ** But that is not due to sloppy coding habits. The code is written this + ** way for performance, to avoid having to run the interrupt and progress + ** checks on every opcode. This helps sqlite3_step() to run about 1.5% + ** faster according to "valgrind --tool=cachegrind" */ +check_for_interrupt: + if( db->u1.isInterrupted ) goto abort_due_to_interrupt; +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + /* Call the progress callback if it is configured and the required number + ** of VDBE ops have been executed (either since this invocation of + ** sqlite3VdbeExec() or since last time the progress callback was called). + ** If the progress callback returns non-zero, exit the virtual machine with + ** a return code SQLITE_ABORT. + */ + if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ + assert( db->nProgressOps!=0 ); + nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); + if( db->xProgress(db->pProgressArg) ){ + rc = SQLITE_INTERRUPT; + goto abort_due_to_error; + } + } +#endif + + break; +} + +/* Opcode: Gosub P1 P2 * * * +** +** Write the current address onto register P1 +** and then jump to address P2. +*/ +case OP_Gosub: { /* jump */ + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + pIn1 = &aMem[pOp->p1]; + assert( VdbeMemDynamic(pIn1)==0 ); + memAboutToChange(p, pIn1); + pIn1->flags = MEM_Int; + pIn1->u.i = (int)(pOp-aOp); + REGISTER_TRACE(pOp->p1, pIn1); + + /* Most jump operations do a goto to this spot in order to update + ** the pOp pointer. */ +jump_to_p2: + pOp = &aOp[pOp->p2 - 1]; + break; +} + +/* Opcode: Return P1 * * * * +** +** Jump to the next instruction after the address in register P1. After +** the jump, register P1 becomes undefined. +*/ +case OP_Return: { /* in1 */ + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags==MEM_Int ); + pOp = &aOp[pIn1->u.i]; + pIn1->flags = MEM_Undefined; + break; +} + +/* Opcode: InitCoroutine P1 P2 P3 * * +** +** Set up register P1 so that it will Yield to the coroutine +** located at address P3. +** +** If P2!=0 then the coroutine implementation immediately follows +** this opcode. So jump over the coroutine implementation to +** address P2. +** +** See also: EndCoroutine +*/ +case OP_InitCoroutine: { /* jump */ + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + assert( pOp->p2>=0 && pOp->p2nOp ); + assert( pOp->p3>=0 && pOp->p3nOp ); + pOut = &aMem[pOp->p1]; + assert( !VdbeMemDynamic(pOut) ); + pOut->u.i = pOp->p3 - 1; + pOut->flags = MEM_Int; + if( pOp->p2 ) goto jump_to_p2; + break; +} + +/* Opcode: EndCoroutine P1 * * * * +** +** The instruction at the address in register P1 is a Yield. +** Jump to the P2 parameter of that Yield. +** After the jump, register P1 becomes undefined. +** +** See also: InitCoroutine +*/ +case OP_EndCoroutine: { /* in1 */ + VdbeOp *pCaller; + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags==MEM_Int ); + assert( pIn1->u.i>=0 && pIn1->u.inOp ); + pCaller = &aOp[pIn1->u.i]; + assert( pCaller->opcode==OP_Yield ); + assert( pCaller->p2>=0 && pCaller->p2nOp ); + pOp = &aOp[pCaller->p2 - 1]; + pIn1->flags = MEM_Undefined; + break; +} + +/* Opcode: Yield P1 P2 * * * +** +** Swap the program counter with the value in register P1. This +** has the effect of yielding to a coroutine. +** +** If the coroutine that is launched by this instruction ends with +** Yield or Return then continue to the next instruction. But if +** the coroutine launched by this instruction ends with +** EndCoroutine, then jump to P2 rather than continuing with the +** next instruction. +** +** See also: InitCoroutine +*/ +case OP_Yield: { /* in1, jump */ + int pcDest; + pIn1 = &aMem[pOp->p1]; + assert( VdbeMemDynamic(pIn1)==0 ); + pIn1->flags = MEM_Int; + pcDest = (int)pIn1->u.i; + pIn1->u.i = (int)(pOp - aOp); + REGISTER_TRACE(pOp->p1, pIn1); + pOp = &aOp[pcDest]; + break; +} + +/* Opcode: HaltIfNull P1 P2 P3 P4 P5 +** Synopsis: if r[P3]=null halt +** +** Check the value in register P3. If it is NULL then Halt using +** parameter P1, P2, and P4 as if this were a Halt instruction. If the +** value in register P3 is not NULL, then this routine is a no-op. +** The P5 parameter should be 1. +*/ +case OP_HaltIfNull: { /* in3 */ + pIn3 = &aMem[pOp->p3]; + if( (pIn3->flags & MEM_Null)==0 ) break; + /* Fall through into OP_Halt */ +} + +/* Opcode: Halt P1 P2 * P4 P5 +** +** Exit immediately. All open cursors, etc are closed +** automatically. +** +** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), +** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). +** For errors, it can be some other value. If P1!=0 then P2 will determine +** whether or not to rollback the current transaction. Do not rollback +** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, +** then back out all changes that have occurred during this execution of the +** VDBE, but do not rollback the transaction. +** +** If P4 is not null then it is an error message string. +** +** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. +** +** 0: (no change) +** 1: NOT NULL contraint failed: P4 +** 2: UNIQUE constraint failed: P4 +** 3: CHECK constraint failed: P4 +** 4: FOREIGN KEY constraint failed: P4 +** +** If P5 is not zero and P4 is NULL, then everything after the ":" is +** omitted. +** +** There is an implied "Halt 0 0 0" instruction inserted at the very end of +** every program. So a jump past the last instruction of the program +** is the same as executing Halt. +*/ +case OP_Halt: { + VdbeFrame *pFrame; + int pcx; + + pcx = (int)(pOp - aOp); + if( pOp->p1==SQLITE_OK && p->pFrame ){ + /* Halt the sub-program. Return control to the parent frame. */ + pFrame = p->pFrame; + p->pFrame = pFrame->pParent; + p->nFrame--; + sqlite3VdbeSetChanges(db, p->nChange); + pcx = sqlite3VdbeFrameRestore(pFrame); + lastRowid = db->lastRowid; + if( pOp->p2==OE_Ignore ){ + /* Instruction pcx is the OP_Program that invoked the sub-program + ** currently being halted. If the p2 instruction of this OP_Halt + ** instruction is set to OE_Ignore, then the sub-program is throwing + ** an IGNORE exception. In this case jump to the address specified + ** as the p2 of the calling OP_Program. */ + pcx = p->aOp[pcx].p2-1; + } + aOp = p->aOp; + aMem = p->aMem; + pOp = &aOp[pcx]; + break; + } + p->rc = pOp->p1; + p->errorAction = (u8)pOp->p2; + p->pc = pcx; + assert( pOp->p5>=0 && pOp->p5<=4 ); + if( p->rc ){ + if( pOp->p5 ){ + static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", + "FOREIGN KEY" }; + testcase( pOp->p5==1 ); + testcase( pOp->p5==2 ); + testcase( pOp->p5==3 ); + testcase( pOp->p5==4 ); + sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); + if( pOp->p4.z ){ + p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); + } + }else{ + sqlite3VdbeError(p, "%s", pOp->p4.z); + } + sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); + } + rc = sqlite3VdbeHalt(p); + assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); + if( rc==SQLITE_BUSY ){ + p->rc = SQLITE_BUSY; + }else{ + assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); + assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); + rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; + } + goto vdbe_return; +} + +/* Opcode: Integer P1 P2 * * * +** Synopsis: r[P2]=P1 +** +** The 32-bit integer value P1 is written into register P2. +*/ +case OP_Integer: { /* out2 */ + pOut = out2Prerelease(p, pOp); + pOut->u.i = pOp->p1; + break; +} + +/* Opcode: Int64 * P2 * P4 * +** Synopsis: r[P2]=P4 +** +** P4 is a pointer to a 64-bit integer value. +** Write that value into register P2. +*/ +case OP_Int64: { /* out2 */ + pOut = out2Prerelease(p, pOp); + assert( pOp->p4.pI64!=0 ); + pOut->u.i = *pOp->p4.pI64; + break; +} + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* Opcode: Real * P2 * P4 * +** Synopsis: r[P2]=P4 +** +** P4 is a pointer to a 64-bit floating point value. +** Write that value into register P2. +*/ +case OP_Real: { /* same as TK_FLOAT, out2 */ + pOut = out2Prerelease(p, pOp); + pOut->flags = MEM_Real; + assert( !sqlite3IsNaN(*pOp->p4.pReal) ); + pOut->u.r = *pOp->p4.pReal; + break; +} +#endif + +/* Opcode: String8 * P2 * P4 * +** Synopsis: r[P2]='P4' +** +** P4 points to a nul terminated UTF-8 string. This opcode is transformed +** into a String opcode before it is executed for the first time. During +** this transformation, the length of string P4 is computed and stored +** as the P1 parameter. +*/ +case OP_String8: { /* same as TK_STRING, out2 */ + assert( pOp->p4.z!=0 ); + pOut = out2Prerelease(p, pOp); + pOp->opcode = OP_String; + pOp->p1 = sqlite3Strlen30(pOp->p4.z); + +#ifndef SQLITE_OMIT_UTF16 + if( encoding!=SQLITE_UTF8 ){ + rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); + assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); + if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; + assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); + assert( VdbeMemDynamic(pOut)==0 ); + pOut->szMalloc = 0; + pOut->flags |= MEM_Static; + if( pOp->p4type==P4_DYNAMIC ){ + sqlite3DbFree(db, pOp->p4.z); + } + pOp->p4type = P4_DYNAMIC; + pOp->p4.z = pOut->z; + pOp->p1 = pOut->n; + } + testcase( rc==SQLITE_TOOBIG ); +#endif + if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + assert( rc==SQLITE_OK ); + /* Fall through to the next case, OP_String */ +} + +/* Opcode: String P1 P2 P3 P4 P5 +** Synopsis: r[P2]='P4' (len=P1) +** +** The string value P4 of length P1 (bytes) is stored in register P2. +** +** If P3 is not zero and the content of register P3 is equal to P5, then +** the datatype of the register P2 is converted to BLOB. The content is +** the same sequence of bytes, it is merely interpreted as a BLOB instead +** of a string, as if it had been CAST. In other words: +** +** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) +*/ +case OP_String: { /* out2 */ + assert( pOp->p4.z!=0 ); + pOut = out2Prerelease(p, pOp); + pOut->flags = MEM_Str|MEM_Static|MEM_Term; + pOut->z = pOp->p4.z; + pOut->n = pOp->p1; + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS + if( pOp->p3>0 ){ + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + pIn3 = &aMem[pOp->p3]; + assert( pIn3->flags & MEM_Int ); + if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; + } +#endif + break; +} + +/* Opcode: Null P1 P2 P3 * * +** Synopsis: r[P2..P3]=NULL +** +** Write a NULL into registers P2. If P3 greater than P2, then also write +** NULL into register P3 and every register in between P2 and P3. If P3 +** is less than P2 (typically P3 is zero) then only register P2 is +** set to NULL. +** +** If the P1 value is non-zero, then also set the MEM_Cleared flag so that +** NULL values will not compare equal even if SQLITE_NULLEQ is set on +** OP_Ne or OP_Eq. +*/ +case OP_Null: { /* out2 */ + int cnt; + u16 nullFlag; + pOut = out2Prerelease(p, pOp); + cnt = pOp->p3-pOp->p2; + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; + while( cnt>0 ){ + pOut++; + memAboutToChange(p, pOut); + sqlite3VdbeMemSetNull(pOut); + pOut->flags = nullFlag; + cnt--; + } + break; +} + +/* Opcode: SoftNull P1 * * * * +** Synopsis: r[P1]=NULL +** +** Set register P1 to have the value NULL as seen by the OP_MakeRecord +** instruction, but do not free any string or blob memory associated with +** the register, so that if the value was a string or blob that was +** previously copied using OP_SCopy, the copies will continue to be valid. +*/ +case OP_SoftNull: { + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + pOut = &aMem[pOp->p1]; + pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; + break; +} + +/* Opcode: Blob P1 P2 * P4 * +** Synopsis: r[P2]=P4 (len=P1) +** +** P4 points to a blob of data P1 bytes long. Store this +** blob in register P2. +*/ +case OP_Blob: { /* out2 */ + assert( pOp->p1 <= SQLITE_MAX_LENGTH ); + pOut = out2Prerelease(p, pOp); + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); + break; +} + +/* Opcode: Variable P1 P2 * P4 * +** Synopsis: r[P2]=parameter(P1,P4) +** +** Transfer the values of bound parameter P1 into register P2 +** +** If the parameter is named, then its name appears in P4. +** The P4 value is used by sqlite3_bind_parameter_name(). +*/ +case OP_Variable: { /* out2 */ + Mem *pVar; /* Value being transferred */ + + assert( pOp->p1>0 && pOp->p1<=p->nVar ); + assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); + pVar = &p->aVar[pOp->p1 - 1]; + if( sqlite3VdbeMemTooBig(pVar) ){ + goto too_big; + } + pOut = out2Prerelease(p, pOp); + sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); + UPDATE_MAX_BLOBSIZE(pOut); + break; +} + +/* Opcode: Move P1 P2 P3 * * +** Synopsis: r[P2@P3]=r[P1@P3] +** +** Move the P3 values in register P1..P1+P3-1 over into +** registers P2..P2+P3-1. Registers P1..P1+P3-1 are +** left holding a NULL. It is an error for register ranges +** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error +** for P3 to be less than 1. +*/ +case OP_Move: { + int n; /* Number of registers left to copy */ + int p1; /* Register to copy from */ + int p2; /* Register to copy to */ + + n = pOp->p3; + p1 = pOp->p1; + p2 = pOp->p2; + assert( n>0 && p1>0 && p2>0 ); + assert( p1+n<=p2 || p2+n<=p1 ); + + pIn1 = &aMem[p1]; + pOut = &aMem[p2]; + do{ + assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); + assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); + assert( memIsValid(pIn1) ); + memAboutToChange(p, pOut); + sqlite3VdbeMemMove(pOut, pIn1); +#ifdef SQLITE_DEBUG + if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrompScopyFrom += pOp->p2 - p1; + } +#endif + Deephemeralize(pOut); + REGISTER_TRACE(p2++, pOut); + pIn1++; + pOut++; + }while( --n ); + break; +} + +/* Opcode: Copy P1 P2 P3 * * +** Synopsis: r[P2@P3+1]=r[P1@P3+1] +** +** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. +** +** This instruction makes a deep copy of the value. A duplicate +** is made of any string or blob constant. See also OP_SCopy. +*/ +case OP_Copy: { + int n; + + n = pOp->p3; + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + assert( pOut!=pIn1 ); + while( 1 ){ + sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); + Deephemeralize(pOut); +#ifdef SQLITE_DEBUG + pOut->pScopyFrom = 0; +#endif + REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); + if( (n--)==0 ) break; + pOut++; + pIn1++; + } + break; +} + +/* Opcode: SCopy P1 P2 * * * +** Synopsis: r[P2]=r[P1] +** +** Make a shallow copy of register P1 into register P2. +** +** This instruction makes a shallow copy of the value. If the value +** is a string or blob, then the copy is only a pointer to the +** original and hence if the original changes so will the copy. +** Worse, if the original is deallocated, the copy becomes invalid. +** Thus the program must guarantee that the original will not change +** during the lifetime of the copy. Use OP_Copy to make a complete +** copy. +*/ +case OP_SCopy: { /* out2 */ + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + assert( pOut!=pIn1 ); + sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); +#ifdef SQLITE_DEBUG + if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; +#endif + break; +} + +/* Opcode: IntCopy P1 P2 * * * +** Synopsis: r[P2]=r[P1] +** +** Transfer the integer value held in register P1 into register P2. +** +** This is an optimized version of SCopy that works only for integer +** values. +*/ +case OP_IntCopy: { /* out2 */ + pIn1 = &aMem[pOp->p1]; + assert( (pIn1->flags & MEM_Int)!=0 ); + pOut = &aMem[pOp->p2]; + sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); + break; +} + +/* Opcode: ResultRow P1 P2 * * * +** Synopsis: output=r[P1@P2] +** +** The registers P1 through P1+P2-1 contain a single row of +** results. This opcode causes the sqlite3_step() call to terminate +** with an SQLITE_ROW return code and it sets up the sqlite3_stmt +** structure to provide access to the r(P1)..r(P1+P2-1) values as +** the result row. +*/ +case OP_ResultRow: { + Mem *pMem; + int i; + assert( p->nResColumn==pOp->p2 ); + assert( pOp->p1>0 ); + assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + /* Run the progress counter just before returning. + */ + if( db->xProgress!=0 + && nVmStep>=nProgressLimit + && db->xProgress(db->pProgressArg)!=0 + ){ + rc = SQLITE_INTERRUPT; + goto abort_due_to_error; + } +#endif + + /* If this statement has violated immediate foreign key constraints, do + ** not return the number of rows modified. And do not RELEASE the statement + ** transaction. It needs to be rolled back. */ + if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ + assert( db->flags&SQLITE_CountRows ); + assert( p->usesStmtJournal ); + goto abort_due_to_error; + } + + /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then + ** DML statements invoke this opcode to return the number of rows + ** modified to the user. This is the only way that a VM that + ** opens a statement transaction may invoke this opcode. + ** + ** In case this is such a statement, close any statement transaction + ** opened by this VM before returning control to the user. This is to + ** ensure that statement-transactions are always nested, not overlapping. + ** If the open statement-transaction is not closed here, then the user + ** may step another VM that opens its own statement transaction. This + ** may lead to overlapping statement transactions. + ** + ** The statement transaction is never a top-level transaction. Hence + ** the RELEASE call below can never fail. + */ + assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); + rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); + assert( rc==SQLITE_OK ); + + /* Invalidate all ephemeral cursor row caches */ + p->cacheCtr = (p->cacheCtr + 2)|1; + + /* Make sure the results of the current row are \000 terminated + ** and have an assigned type. The results are de-ephemeralized as + ** a side effect. + */ + pMem = p->pResultSet = &aMem[pOp->p1]; + for(i=0; ip2; i++){ + assert( memIsValid(&pMem[i]) ); + Deephemeralize(&pMem[i]); + assert( (pMem[i].flags & MEM_Ephem)==0 + || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); + sqlite3VdbeMemNulTerminate(&pMem[i]); + REGISTER_TRACE(pOp->p1+i, &pMem[i]); + } + if( db->mallocFailed ) goto no_mem; + + /* Return SQLITE_ROW + */ + p->pc = (int)(pOp - aOp) + 1; + rc = SQLITE_ROW; + goto vdbe_return; +} + +/* Opcode: Concat P1 P2 P3 * * +** Synopsis: r[P3]=r[P2]+r[P1] +** +** Add the text in register P1 onto the end of the text in +** register P2 and store the result in register P3. +** If either the P1 or P2 text are NULL then store NULL in P3. +** +** P3 = P2 || P1 +** +** It is illegal for P1 and P3 to be the same register. Sometimes, +** if P3 is the same register as P2, the implementation is able +** to avoid a memcpy(). +*/ +case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ + i64 nByte; + + pIn1 = &aMem[pOp->p1]; + pIn2 = &aMem[pOp->p2]; + pOut = &aMem[pOp->p3]; + assert( pIn1!=pOut ); + if( (pIn1->flags | pIn2->flags) & MEM_Null ){ + sqlite3VdbeMemSetNull(pOut); + break; + } + if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; + Stringify(pIn1, encoding); + Stringify(pIn2, encoding); + nByte = pIn1->n + pIn2->n; + if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ + goto no_mem; + } + MemSetTypeFlag(pOut, MEM_Str); + if( pOut!=pIn2 ){ + memcpy(pOut->z, pIn2->z, pIn2->n); + } + memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); + pOut->z[nByte]=0; + pOut->z[nByte+1] = 0; + pOut->flags |= MEM_Term; + pOut->n = (int)nByte; + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); + break; +} + +/* Opcode: Add P1 P2 P3 * * +** Synopsis: r[P3]=r[P1]+r[P2] +** +** Add the value in register P1 to the value in register P2 +** and store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +/* Opcode: Multiply P1 P2 P3 * * +** Synopsis: r[P3]=r[P1]*r[P2] +** +** +** Multiply the value in register P1 by the value in register P2 +** and store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +/* Opcode: Subtract P1 P2 P3 * * +** Synopsis: r[P3]=r[P2]-r[P1] +** +** Subtract the value in register P1 from the value in register P2 +** and store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +/* Opcode: Divide P1 P2 P3 * * +** Synopsis: r[P3]=r[P2]/r[P1] +** +** Divide the value in register P1 by the value in register P2 +** and store the result in register P3 (P3=P2/P1). If the value in +** register P1 is zero, then the result is NULL. If either input is +** NULL, the result is NULL. +*/ +/* Opcode: Remainder P1 P2 P3 * * +** Synopsis: r[P3]=r[P2]%r[P1] +** +** Compute the remainder after integer register P2 is divided by +** register P1 and store the result in register P3. +** If the value in register P1 is zero the result is NULL. +** If either operand is NULL, the result is NULL. +*/ +case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ +case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ +case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ +case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ +case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ + char bIntint; /* Started out as two integer operands */ + u16 flags; /* Combined MEM_* flags from both inputs */ + u16 type1; /* Numeric type of left operand */ + u16 type2; /* Numeric type of right operand */ + i64 iA; /* Integer value of left operand */ + i64 iB; /* Integer value of right operand */ + double rA; /* Real value of left operand */ + double rB; /* Real value of right operand */ + + pIn1 = &aMem[pOp->p1]; + type1 = numericType(pIn1); + pIn2 = &aMem[pOp->p2]; + type2 = numericType(pIn2); + pOut = &aMem[pOp->p3]; + flags = pIn1->flags | pIn2->flags; + if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; + if( (type1 & type2 & MEM_Int)!=0 ){ + iA = pIn1->u.i; + iB = pIn2->u.i; + bIntint = 1; + switch( pOp->opcode ){ + case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; + case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; + case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; + case OP_Divide: { + if( iA==0 ) goto arithmetic_result_is_null; + if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; + iB /= iA; + break; + } + default: { + if( iA==0 ) goto arithmetic_result_is_null; + if( iA==-1 ) iA = 1; + iB %= iA; + break; + } + } + pOut->u.i = iB; + MemSetTypeFlag(pOut, MEM_Int); + }else{ + bIntint = 0; +fp_math: + rA = sqlite3VdbeRealValue(pIn1); + rB = sqlite3VdbeRealValue(pIn2); + switch( pOp->opcode ){ + case OP_Add: rB += rA; break; + case OP_Subtract: rB -= rA; break; + case OP_Multiply: rB *= rA; break; + case OP_Divide: { + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + if( rA==(double)0 ) goto arithmetic_result_is_null; + rB /= rA; + break; + } + default: { + iA = (i64)rA; + iB = (i64)rB; + if( iA==0 ) goto arithmetic_result_is_null; + if( iA==-1 ) iA = 1; + rB = (double)(iB % iA); + break; + } + } +#ifdef SQLITE_OMIT_FLOATING_POINT + pOut->u.i = rB; + MemSetTypeFlag(pOut, MEM_Int); +#else + if( sqlite3IsNaN(rB) ){ + goto arithmetic_result_is_null; + } + pOut->u.r = rB; + MemSetTypeFlag(pOut, MEM_Real); + if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ + sqlite3VdbeIntegerAffinity(pOut); + } +#endif + } + break; + +arithmetic_result_is_null: + sqlite3VdbeMemSetNull(pOut); + break; +} + +/* Opcode: CollSeq P1 * * P4 +** +** P4 is a pointer to a CollSeq struct. If the next call to a user function +** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will +** be returned. This is used by the built-in min(), max() and nullif() +** functions. +** +** If P1 is not zero, then it is a register that a subsequent min() or +** max() aggregate will set to 1 if the current row is not the minimum or +** maximum. The P1 register is initialized to 0 by this instruction. +** +** The interface used by the implementation of the aforementioned functions +** to retrieve the collation sequence set by this opcode is not available +** publicly. Only built-in functions have access to this feature. +*/ +case OP_CollSeq: { + assert( pOp->p4type==P4_COLLSEQ ); + if( pOp->p1 ){ + sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); + } + break; +} + +/* Opcode: Function0 P1 P2 P3 P4 P5 +** Synopsis: r[P3]=func(r[P2@P5]) +** +** Invoke a user function (P4 is a pointer to a FuncDef object that +** defines the function) with P5 arguments taken from register P2 and +** successors. The result of the function is stored in register P3. +** Register P3 must not be one of the function inputs. +** +** P1 is a 32-bit bitmask indicating whether or not each argument to the +** function was determined to be constant at compile time. If the first +** argument was constant then bit 0 of P1 is set. This is used to determine +** whether meta data associated with a user function argument using the +** sqlite3_set_auxdata() API may be safely retained until the next +** invocation of this opcode. +** +** See also: Function, AggStep, AggFinal +*/ +/* Opcode: Function P1 P2 P3 P4 P5 +** Synopsis: r[P3]=func(r[P2@P5]) +** +** Invoke a user function (P4 is a pointer to an sqlite3_context object that +** contains a pointer to the function to be run) with P5 arguments taken +** from register P2 and successors. The result of the function is stored +** in register P3. Register P3 must not be one of the function inputs. +** +** P1 is a 32-bit bitmask indicating whether or not each argument to the +** function was determined to be constant at compile time. If the first +** argument was constant then bit 0 of P1 is set. This is used to determine +** whether meta data associated with a user function argument using the +** sqlite3_set_auxdata() API may be safely retained until the next +** invocation of this opcode. +** +** SQL functions are initially coded as OP_Function0 with P4 pointing +** to a FuncDef object. But on first evaluation, the P4 operand is +** automatically converted into an sqlite3_context object and the operation +** changed to this OP_Function opcode. In this way, the initialization of +** the sqlite3_context object occurs only once, rather than once for each +** evaluation of the function. +** +** See also: Function0, AggStep, AggFinal +*/ +case OP_Function0: { + int n; + sqlite3_context *pCtx; + + assert( pOp->p4type==P4_FUNCDEF ); + n = pOp->p5; + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); + assert( pOp->p3p2 || pOp->p3>=pOp->p2+n ); + pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); + if( pCtx==0 ) goto no_mem; + pCtx->pOut = 0; + pCtx->pFunc = pOp->p4.pFunc; + pCtx->iOp = (int)(pOp - aOp); + pCtx->pVdbe = p; + pCtx->argc = n; + pOp->p4type = P4_FUNCCTX; + pOp->p4.pCtx = pCtx; + pOp->opcode = OP_Function; + /* Fall through into OP_Function */ +} +case OP_Function: { + int i; + sqlite3_context *pCtx; + + assert( pOp->p4type==P4_FUNCCTX ); + pCtx = pOp->p4.pCtx; + + /* If this function is inside of a trigger, the register array in aMem[] + ** might change from one evaluation to the next. The next block of code + ** checks to see if the register array has changed, and if so it + ** reinitializes the relavant parts of the sqlite3_context object */ + pOut = &aMem[pOp->p3]; + if( pCtx->pOut != pOut ){ + pCtx->pOut = pOut; + for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; + } + + memAboutToChange(p, pCtx->pOut); +#ifdef SQLITE_DEBUG + for(i=0; iargc; i++){ + assert( memIsValid(pCtx->argv[i]) ); + REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); + } +#endif + MemSetTypeFlag(pCtx->pOut, MEM_Null); + pCtx->fErrorOrAux = 0; + db->lastRowid = lastRowid; + (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ + lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */ + + /* If the function returned an error, throw an exception */ + if( pCtx->fErrorOrAux ){ + if( pCtx->isError ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); + rc = pCtx->isError; + } + sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); + if( rc ) goto abort_due_to_error; + } + + /* Copy the result of the function into register P3 */ + if( pOut->flags & (MEM_Str|MEM_Blob) ){ + sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); + if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; + } + + REGISTER_TRACE(pOp->p3, pCtx->pOut); + UPDATE_MAX_BLOBSIZE(pCtx->pOut); + break; +} + +/* Opcode: BitAnd P1 P2 P3 * * +** Synopsis: r[P3]=r[P1]&r[P2] +** +** Take the bit-wise AND of the values in register P1 and P2 and +** store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +/* Opcode: BitOr P1 P2 P3 * * +** Synopsis: r[P3]=r[P1]|r[P2] +** +** Take the bit-wise OR of the values in register P1 and P2 and +** store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +/* Opcode: ShiftLeft P1 P2 P3 * * +** Synopsis: r[P3]=r[P2]<>r[P1] +** +** Shift the integer value in register P2 to the right by the +** number of bits specified by the integer in register P1. +** Store the result in register P3. +** If either input is NULL, the result is NULL. +*/ +case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ +case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ +case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ +case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ + i64 iA; + u64 uA; + i64 iB; + u8 op; + + pIn1 = &aMem[pOp->p1]; + pIn2 = &aMem[pOp->p2]; + pOut = &aMem[pOp->p3]; + if( (pIn1->flags | pIn2->flags) & MEM_Null ){ + sqlite3VdbeMemSetNull(pOut); + break; + } + iA = sqlite3VdbeIntValue(pIn2); + iB = sqlite3VdbeIntValue(pIn1); + op = pOp->opcode; + if( op==OP_BitAnd ){ + iA &= iB; + }else if( op==OP_BitOr ){ + iA |= iB; + }else if( iB!=0 ){ + assert( op==OP_ShiftRight || op==OP_ShiftLeft ); + + /* If shifting by a negative amount, shift in the other direction */ + if( iB<0 ){ + assert( OP_ShiftRight==OP_ShiftLeft+1 ); + op = 2*OP_ShiftLeft + 1 - op; + iB = iB>(-64) ? -iB : 64; + } + + if( iB>=64 ){ + iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; + }else{ + memcpy(&uA, &iA, sizeof(uA)); + if( op==OP_ShiftLeft ){ + uA <<= iB; + }else{ + uA >>= iB; + /* Sign-extend on a right shift of a negative number */ + if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); + } + memcpy(&iA, &uA, sizeof(iA)); + } + } + pOut->u.i = iA; + MemSetTypeFlag(pOut, MEM_Int); + break; +} + +/* Opcode: AddImm P1 P2 * * * +** Synopsis: r[P1]=r[P1]+P2 +** +** Add the constant P2 to the value in register P1. +** The result is always an integer. +** +** To force any register to be an integer, just add 0. +*/ +case OP_AddImm: { /* in1 */ + pIn1 = &aMem[pOp->p1]; + memAboutToChange(p, pIn1); + sqlite3VdbeMemIntegerify(pIn1); + pIn1->u.i += pOp->p2; + break; +} + +/* Opcode: MustBeInt P1 P2 * * * +** +** Force the value in register P1 to be an integer. If the value +** in P1 is not an integer and cannot be converted into an integer +** without data loss, then jump immediately to P2, or if P2==0 +** raise an SQLITE_MISMATCH exception. +*/ +case OP_MustBeInt: { /* jump, in1 */ + pIn1 = &aMem[pOp->p1]; + if( (pIn1->flags & MEM_Int)==0 ){ + applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); + VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); + if( (pIn1->flags & MEM_Int)==0 ){ + if( pOp->p2==0 ){ + rc = SQLITE_MISMATCH; + goto abort_due_to_error; + }else{ + goto jump_to_p2; + } + } + } + MemSetTypeFlag(pIn1, MEM_Int); + break; +} + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* Opcode: RealAffinity P1 * * * * +** +** If register P1 holds an integer convert it to a real value. +** +** This opcode is used when extracting information from a column that +** has REAL affinity. Such column values may still be stored as +** integers, for space efficiency, but after extraction we want them +** to have only a real value. +*/ +case OP_RealAffinity: { /* in1 */ + pIn1 = &aMem[pOp->p1]; + if( pIn1->flags & MEM_Int ){ + sqlite3VdbeMemRealify(pIn1); + } + break; +} +#endif + +#ifndef SQLITE_OMIT_CAST +/* Opcode: Cast P1 P2 * * * +** Synopsis: affinity(r[P1]) +** +** Force the value in register P1 to be the type defined by P2. +** +**
      +**
    • TEXT +**
    • BLOB +**
    • NUMERIC +**
    • INTEGER +**
    • REAL +**
    +** +** A NULL value is not changed by this routine. It remains NULL. +*/ +case OP_Cast: { /* in1 */ + assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); + testcase( pOp->p2==SQLITE_AFF_TEXT ); + testcase( pOp->p2==SQLITE_AFF_BLOB ); + testcase( pOp->p2==SQLITE_AFF_NUMERIC ); + testcase( pOp->p2==SQLITE_AFF_INTEGER ); + testcase( pOp->p2==SQLITE_AFF_REAL ); + pIn1 = &aMem[pOp->p1]; + memAboutToChange(p, pIn1); + rc = ExpandBlob(pIn1); + sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); + UPDATE_MAX_BLOBSIZE(pIn1); + if( rc ) goto abort_due_to_error; + break; +} +#endif /* SQLITE_OMIT_CAST */ + +/* Opcode: Lt P1 P2 P3 P4 P5 +** Synopsis: if r[P1]r[P3] goto P2 +** +** This works just like the Lt opcode except that the jump is taken if +** the content of register P3 is greater than the content of +** register P1. See the Lt opcode for additional information. +*/ +/* Opcode: Ge P1 P2 P3 P4 P5 +** Synopsis: if r[P1]>=r[P3] goto P2 +** +** This works just like the Lt opcode except that the jump is taken if +** the content of register P3 is greater than or equal to the content of +** register P1. See the Lt opcode for additional information. +*/ +case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ +case OP_Ne: /* same as TK_NE, jump, in1, in3 */ +case OP_Lt: /* same as TK_LT, jump, in1, in3 */ +case OP_Le: /* same as TK_LE, jump, in1, in3 */ +case OP_Gt: /* same as TK_GT, jump, in1, in3 */ +case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ + int res; /* Result of the comparison of pIn1 against pIn3 */ + char affinity; /* Affinity to use for comparison */ + u16 flags1; /* Copy of initial value of pIn1->flags */ + u16 flags3; /* Copy of initial value of pIn3->flags */ + + pIn1 = &aMem[pOp->p1]; + pIn3 = &aMem[pOp->p3]; + flags1 = pIn1->flags; + flags3 = pIn3->flags; + if( (flags1 | flags3)&MEM_Null ){ + /* One or both operands are NULL */ + if( pOp->p5 & SQLITE_NULLEQ ){ + /* If SQLITE_NULLEQ is set (which will only happen if the operator is + ** OP_Eq or OP_Ne) then take the jump or not depending on whether + ** or not both operands are null. + */ + assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); + assert( (flags1 & MEM_Cleared)==0 ); + assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); + if( (flags1&MEM_Null)!=0 + && (flags3&MEM_Null)!=0 + && (flags3&MEM_Cleared)==0 + ){ + res = 0; /* Results are equal */ + }else{ + res = 1; /* Results are not equal */ + } + }else{ + /* SQLITE_NULLEQ is clear and at least one operand is NULL, + ** then the result is always NULL. + ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. + */ + if( pOp->p5 & SQLITE_STOREP2 ){ + pOut = &aMem[pOp->p2]; + memAboutToChange(p, pOut); + MemSetTypeFlag(pOut, MEM_Null); + REGISTER_TRACE(pOp->p2, pOut); + }else{ + VdbeBranchTaken(2,3); + if( pOp->p5 & SQLITE_JUMPIFNULL ){ + goto jump_to_p2; + } + } + break; + } + }else{ + /* Neither operand is NULL. Do a comparison. */ + affinity = pOp->p5 & SQLITE_AFF_MASK; + if( affinity>=SQLITE_AFF_NUMERIC ){ + if( (flags1 | flags3)&MEM_Str ){ + if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ + applyNumericAffinity(pIn1,0); + } + if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ + applyNumericAffinity(pIn3,0); + } + } + }else if( affinity==SQLITE_AFF_TEXT ){ + if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ + testcase( pIn1->flags & MEM_Int ); + testcase( pIn1->flags & MEM_Real ); + sqlite3VdbeMemStringify(pIn1, encoding, 1); + testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); + flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); + } + if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ + testcase( pIn3->flags & MEM_Int ); + testcase( pIn3->flags & MEM_Real ); + sqlite3VdbeMemStringify(pIn3, encoding, 1); + testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); + flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); + } + } + assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); + if( flags1 & MEM_Zero ){ + sqlite3VdbeMemExpandBlob(pIn1); + flags1 &= ~MEM_Zero; + } + if( flags3 & MEM_Zero ){ + sqlite3VdbeMemExpandBlob(pIn3); + flags3 &= ~MEM_Zero; + } + res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); + } + switch( pOp->opcode ){ + case OP_Eq: res = res==0; break; + case OP_Ne: res = res!=0; break; + case OP_Lt: res = res<0; break; + case OP_Le: res = res<=0; break; + case OP_Gt: res = res>0; break; + default: res = res>=0; break; + } + + /* Undo any changes made by applyAffinity() to the input registers. */ + assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); + pIn1->flags = flags1; + assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); + pIn3->flags = flags3; + + if( pOp->p5 & SQLITE_STOREP2 ){ + pOut = &aMem[pOp->p2]; + memAboutToChange(p, pOut); + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = res; + REGISTER_TRACE(pOp->p2, pOut); + }else{ + VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); + if( res ){ + goto jump_to_p2; + } + } + break; +} + +/* Opcode: Permutation * * * P4 * +** +** Set the permutation used by the OP_Compare operator to be the array +** of integers in P4. +** +** The permutation is only valid until the next OP_Compare that has +** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should +** occur immediately prior to the OP_Compare. +** +** The first integer in the P4 integer array is the length of the array +** and does not become part of the permutation. +*/ +case OP_Permutation: { + assert( pOp->p4type==P4_INTARRAY ); + assert( pOp->p4.ai ); + aPermute = pOp->p4.ai + 1; + break; +} + +/* Opcode: Compare P1 P2 P3 P4 P5 +** Synopsis: r[P1@P3] <-> r[P2@P3] +** +** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this +** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of +** the comparison for use by the next OP_Jump instruct. +** +** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is +** determined by the most recent OP_Permutation operator. If the +** OPFLAG_PERMUTE bit is clear, then register are compared in sequential +** order. +** +** P4 is a KeyInfo structure that defines collating sequences and sort +** orders for the comparison. The permutation applies to registers +** only. The KeyInfo elements are used sequentially. +** +** The comparison is a sort comparison, so NULLs compare equal, +** NULLs are less than numbers, numbers are less than strings, +** and strings are less than blobs. +*/ +case OP_Compare: { + int n; + int i; + int p1; + int p2; + const KeyInfo *pKeyInfo; + int idx; + CollSeq *pColl; /* Collating sequence to use on this term */ + int bRev; /* True for DESCENDING sort order */ + + if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; + n = pOp->p3; + pKeyInfo = pOp->p4.pKeyInfo; + assert( n>0 ); + assert( pKeyInfo!=0 ); + p1 = pOp->p1; + p2 = pOp->p2; +#if SQLITE_DEBUG + if( aPermute ){ + int k, mx = 0; + for(k=0; kmx ) mx = aPermute[k]; + assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); + assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); + }else{ + assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); + assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); + } +#endif /* SQLITE_DEBUG */ + for(i=0; inField ); + pColl = pKeyInfo->aColl[i]; + bRev = pKeyInfo->aSortOrder[i]; + iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); + if( iCompare ){ + if( bRev ) iCompare = -iCompare; + break; + } + } + aPermute = 0; + break; +} + +/* Opcode: Jump P1 P2 P3 * * +** +** Jump to the instruction at address P1, P2, or P3 depending on whether +** in the most recent OP_Compare instruction the P1 vector was less than +** equal to, or greater than the P2 vector, respectively. +*/ +case OP_Jump: { /* jump */ + if( iCompare<0 ){ + VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; + }else if( iCompare==0 ){ + VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; + }else{ + VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; + } + break; +} + +/* Opcode: And P1 P2 P3 * * +** Synopsis: r[P3]=(r[P1] && r[P2]) +** +** Take the logical AND of the values in registers P1 and P2 and +** write the result into register P3. +** +** If either P1 or P2 is 0 (false) then the result is 0 even if +** the other input is NULL. A NULL and true or two NULLs give +** a NULL output. +*/ +/* Opcode: Or P1 P2 P3 * * +** Synopsis: r[P3]=(r[P1] || r[P2]) +** +** Take the logical OR of the values in register P1 and P2 and +** store the answer in register P3. +** +** If either P1 or P2 is nonzero (true) then the result is 1 (true) +** even if the other input is NULL. A NULL and false or two NULLs +** give a NULL output. +*/ +case OP_And: /* same as TK_AND, in1, in2, out3 */ +case OP_Or: { /* same as TK_OR, in1, in2, out3 */ + int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ + int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ + + pIn1 = &aMem[pOp->p1]; + if( pIn1->flags & MEM_Null ){ + v1 = 2; + }else{ + v1 = sqlite3VdbeIntValue(pIn1)!=0; + } + pIn2 = &aMem[pOp->p2]; + if( pIn2->flags & MEM_Null ){ + v2 = 2; + }else{ + v2 = sqlite3VdbeIntValue(pIn2)!=0; + } + if( pOp->opcode==OP_And ){ + static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; + v1 = and_logic[v1*3+v2]; + }else{ + static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; + v1 = or_logic[v1*3+v2]; + } + pOut = &aMem[pOp->p3]; + if( v1==2 ){ + MemSetTypeFlag(pOut, MEM_Null); + }else{ + pOut->u.i = v1; + MemSetTypeFlag(pOut, MEM_Int); + } + break; +} + +/* Opcode: Not P1 P2 * * * +** Synopsis: r[P2]= !r[P1] +** +** Interpret the value in register P1 as a boolean value. Store the +** boolean complement in register P2. If the value in register P1 is +** NULL, then a NULL is stored in P2. +*/ +case OP_Not: { /* same as TK_NOT, in1, out2 */ + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + sqlite3VdbeMemSetNull(pOut); + if( (pIn1->flags & MEM_Null)==0 ){ + pOut->flags = MEM_Int; + pOut->u.i = !sqlite3VdbeIntValue(pIn1); + } + break; +} + +/* Opcode: BitNot P1 P2 * * * +** Synopsis: r[P1]= ~r[P1] +** +** Interpret the content of register P1 as an integer. Store the +** ones-complement of the P1 value into register P2. If P1 holds +** a NULL then store a NULL in P2. +*/ +case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + sqlite3VdbeMemSetNull(pOut); + if( (pIn1->flags & MEM_Null)==0 ){ + pOut->flags = MEM_Int; + pOut->u.i = ~sqlite3VdbeIntValue(pIn1); + } + break; +} + +/* Opcode: Once P1 P2 * * * +** +** Check the "once" flag number P1. If it is set, jump to instruction P2. +** Otherwise, set the flag and fall through to the next instruction. +** In other words, this opcode causes all following opcodes up through P2 +** (but not including P2) to run just once and to be skipped on subsequent +** times through the loop. +** +** All "once" flags are initially cleared whenever a prepared statement +** first begins to run. +*/ +case OP_Once: { /* jump */ + assert( pOp->p1nOnceFlag ); + VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); + if( p->aOnceFlag[pOp->p1] ){ + goto jump_to_p2; + }else{ + p->aOnceFlag[pOp->p1] = 1; + } + break; +} + +/* Opcode: If P1 P2 P3 * * +** +** Jump to P2 if the value in register P1 is true. The value +** is considered true if it is numeric and non-zero. If the value +** in P1 is NULL then take the jump if and only if P3 is non-zero. +*/ +/* Opcode: IfNot P1 P2 P3 * * +** +** Jump to P2 if the value in register P1 is False. The value +** is considered false if it has a numeric value of zero. If the value +** in P1 is NULL then take the jump if and only if P3 is non-zero. +*/ +case OP_If: /* jump, in1 */ +case OP_IfNot: { /* jump, in1 */ + int c; + pIn1 = &aMem[pOp->p1]; + if( pIn1->flags & MEM_Null ){ + c = pOp->p3; + }else{ +#ifdef SQLITE_OMIT_FLOATING_POINT + c = sqlite3VdbeIntValue(pIn1)!=0; +#else + c = sqlite3VdbeRealValue(pIn1)!=0.0; +#endif + if( pOp->opcode==OP_IfNot ) c = !c; + } + VdbeBranchTaken(c!=0, 2); + if( c ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: IsNull P1 P2 * * * +** Synopsis: if r[P1]==NULL goto P2 +** +** Jump to P2 if the value in register P1 is NULL. +*/ +case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ + pIn1 = &aMem[pOp->p1]; + VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); + if( (pIn1->flags & MEM_Null)!=0 ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: NotNull P1 P2 * * * +** Synopsis: if r[P1]!=NULL goto P2 +** +** Jump to P2 if the value in register P1 is not NULL. +*/ +case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ + pIn1 = &aMem[pOp->p1]; + VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); + if( (pIn1->flags & MEM_Null)==0 ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: Column P1 P2 P3 P4 P5 +** Synopsis: r[P3]=PX +** +** Interpret the data that cursor P1 points to as a structure built using +** the MakeRecord instruction. (See the MakeRecord opcode for additional +** information about the format of the data.) Extract the P2-th column +** from this record. If there are less that (P2+1) +** values in the record, extract a NULL. +** +** The value extracted is stored in register P3. +** +** If the column contains fewer than P2 fields, then extract a NULL. Or, +** if the P4 argument is a P4_MEM use the value of the P4 argument as +** the result. +** +** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, +** then the cache of the cursor is reset prior to extracting the column. +** The first OP_Column against a pseudo-table after the value of the content +** register has changed should have this bit set. +** +** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when +** the result is guaranteed to only be used as the argument of a length() +** or typeof() function, respectively. The loading of large blobs can be +** skipped for length() and all content loading can be skipped for typeof(). +*/ +case OP_Column: { + i64 payloadSize64; /* Number of bytes in the record */ + int p2; /* column number to retrieve */ + VdbeCursor *pC; /* The VDBE cursor */ + BtCursor *pCrsr; /* The BTree cursor */ + u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ + int len; /* The length of the serialized data for the column */ + int i; /* Loop counter */ + Mem *pDest; /* Where to write the extracted value */ + Mem sMem; /* For storing the record being decoded */ + const u8 *zData; /* Part of the record being decoded */ + const u8 *zHdr; /* Next unparsed byte of the header */ + const u8 *zEndHdr; /* Pointer to first byte after the header */ + u32 offset; /* Offset into the data */ + u64 offset64; /* 64-bit offset */ + u32 avail; /* Number of bytes of available data */ + u32 t; /* A type code from the record header */ + Mem *pReg; /* PseudoTable input register */ + + pC = p->apCsr[pOp->p1]; + p2 = pOp->p2; + + /* If the cursor cache is stale, bring it up-to-date */ + rc = sqlite3VdbeCursorMoveto(&pC, &p2); + + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + pDest = &aMem[pOp->p3]; + memAboutToChange(p, pDest); + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pC!=0 ); + assert( p2nField ); + aOffset = pC->aOffset; + assert( pC->eCurType!=CURTYPE_VTAB ); + assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); + assert( pC->eCurType!=CURTYPE_SORTER ); + pCrsr = pC->uc.pCursor; + + if( rc ) goto abort_due_to_error; + if( pC->cacheStatus!=p->cacheCtr ){ + if( pC->nullRow ){ + if( pC->eCurType==CURTYPE_PSEUDO ){ + assert( pC->uc.pseudoTableReg>0 ); + pReg = &aMem[pC->uc.pseudoTableReg]; + assert( pReg->flags & MEM_Blob ); + assert( memIsValid(pReg) ); + pC->payloadSize = pC->szRow = avail = pReg->n; + pC->aRow = (u8*)pReg->z; + }else{ + sqlite3VdbeMemSetNull(pDest); + goto op_column_out; + } + }else{ + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pCrsr ); + if( pC->isTable==0 ){ + assert( sqlite3BtreeCursorIsValid(pCrsr) ); + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64); + assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ + /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the + ** payload size, so it is impossible for payloadSize64 to be + ** larger than 32 bits. */ + assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); + pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail); + pC->payloadSize = (u32)payloadSize64; + }else{ + assert( sqlite3BtreeCursorIsValid(pCrsr) ); + VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize); + assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ + pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail); + } + assert( avail<=65536 ); /* Maximum page size is 64KiB */ + if( pC->payloadSize <= (u32)avail ){ + pC->szRow = pC->payloadSize; + }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + }else{ + pC->szRow = avail; + } + } + pC->cacheStatus = p->cacheCtr; + pC->iHdrOffset = getVarint32(pC->aRow, offset); + pC->nHdrParsed = 0; + aOffset[0] = offset; + + + if( availaRow does not have to hold the entire row, but it does at least + ** need to cover the header of the record. If pC->aRow does not contain + ** the complete header, then set it to zero, forcing the header to be + ** dynamically allocated. */ + pC->aRow = 0; + pC->szRow = 0; + + /* Make sure a corrupt database has not given us an oversize header. + ** Do this now to avoid an oversize memory allocation. + ** + ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte + ** types use so much data space that there can only be 4096 and 32 of + ** them, respectively. So the maximum header length results from a + ** 3-byte type for each of the maximum of 32768 columns plus three + ** extra bytes for the header length itself. 32768*3 + 3 = 98307. + */ + if( offset > 98307 || offset > pC->payloadSize ){ + rc = SQLITE_CORRUPT_BKPT; + goto abort_due_to_error; + } + } + + /* The following goto is an optimization. It can be omitted and + ** everything will still work. But OP_Column is measurably faster + ** by skipping the subsequent conditional, which is always true. + */ + assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ + goto op_column_read_header; + } + + /* Make sure at least the first p2+1 entries of the header have been + ** parsed and valid information is in aOffset[] and pC->aType[]. + */ + if( pC->nHdrParsed<=p2 ){ + /* If there is more header available for parsing in the record, try + ** to extract additional fields up through the p2+1-th field + */ + op_column_read_header: + if( pC->iHdrOffsetaRow==0 ){ + memset(&sMem, 0, sizeof(sMem)); + rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + zData = (u8*)sMem.z; + }else{ + zData = pC->aRow; + } + + /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ + i = pC->nHdrParsed; + offset64 = aOffset[i]; + zHdr = zData + pC->iHdrOffset; + zEndHdr = zData + aOffset[0]; + assert( i<=p2 && zHdraType[i++] = t; + aOffset[i] = (u32)(offset64 & 0xffffffff); + }while( i<=p2 && zHdrnHdrParsed = i; + pC->iHdrOffset = (u32)(zHdr - zData); + + /* The record is corrupt if any of the following are true: + ** (1) the bytes of the header extend past the declared header size + ** (2) the entire header was used but not all data was used + ** (3) the end of the data extends beyond the end of the record. + */ + if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) + || (offset64 > pC->payloadSize) + ){ + if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); + rc = SQLITE_CORRUPT_BKPT; + goto abort_due_to_error; + } + if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); + + }else{ + t = 0; + } + + /* If after trying to extract new entries from the header, nHdrParsed is + ** still not up to p2, that means that the record has fewer than p2 + ** columns. So the result will be either the default value or a NULL. + */ + if( pC->nHdrParsed<=p2 ){ + if( pOp->p4type==P4_MEM ){ + sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); + }else{ + sqlite3VdbeMemSetNull(pDest); + } + goto op_column_out; + } + }else{ + t = pC->aType[p2]; + } + + /* Extract the content for the p2+1-th column. Control can only + ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are + ** all valid. + */ + assert( p2nHdrParsed ); + assert( rc==SQLITE_OK ); + assert( sqlite3VdbeCheckMemInvariants(pDest) ); + if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest); + assert( t==pC->aType[p2] ); + pDest->enc = encoding; + if( pC->szRow>=aOffset[p2+1] ){ + /* This is the common case where the desired content fits on the original + ** page - where the content is not on an overflow page */ + zData = pC->aRow + aOffset[p2]; + if( t<12 ){ + sqlite3VdbeSerialGet(zData, t, pDest); + }else{ + /* If the column value is a string, we need a persistent value, not + ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent + ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). + */ + static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; + pDest->n = len = (t-12)/2; + if( pDest->szMalloc < len+2 ){ + pDest->flags = MEM_Null; + if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; + }else{ + pDest->z = pDest->zMalloc; + } + memcpy(pDest->z, zData, len); + pDest->z[len] = 0; + pDest->z[len+1] = 0; + pDest->flags = aFlag[t&1]; + } + }else{ + /* This branch happens only when content is on overflow pages */ + if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 + && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) + || (len = sqlite3VdbeSerialTypeLen(t))==0 + ){ + /* Content is irrelevant for + ** 1. the typeof() function, + ** 2. the length(X) function if X is a blob, and + ** 3. if the content length is zero. + ** So we might as well use bogus content rather than reading + ** content from disk. */ + static u8 aZero[8]; /* This is the bogus content */ + sqlite3VdbeSerialGet(aZero, t, pDest); + }else{ + rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable, + pDest); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); + pDest->flags &= ~MEM_Ephem; + } + } + +op_column_out: + UPDATE_MAX_BLOBSIZE(pDest); + REGISTER_TRACE(pOp->p3, pDest); + break; +} + +/* Opcode: Affinity P1 P2 * P4 * +** Synopsis: affinity(r[P1@P2]) +** +** Apply affinities to a range of P2 registers starting with P1. +** +** P4 is a string that is P2 characters long. The nth character of the +** string indicates the column affinity that should be used for the nth +** memory cell in the range. +*/ +case OP_Affinity: { + const char *zAffinity; /* The affinity to be applied */ + char cAff; /* A single character of affinity */ + + zAffinity = pOp->p4.z; + assert( zAffinity!=0 ); + assert( zAffinity[pOp->p2]==0 ); + pIn1 = &aMem[pOp->p1]; + while( (cAff = *(zAffinity++))!=0 ){ + assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); + assert( memIsValid(pIn1) ); + applyAffinity(pIn1, cAff, encoding); + pIn1++; + } + break; +} + +/* Opcode: MakeRecord P1 P2 P3 P4 * +** Synopsis: r[P3]=mkrec(r[P1@P2]) +** +** Convert P2 registers beginning with P1 into the [record format] +** use as a data record in a database table or as a key +** in an index. The OP_Column opcode can decode the record later. +** +** P4 may be a string that is P2 characters long. The nth character of the +** string indicates the column affinity that should be used for the nth +** field of the index key. +** +** The mapping from character to affinity is given by the SQLITE_AFF_ +** macros defined in sqliteInt.h. +** +** If P4 is NULL then all index fields have the affinity BLOB. +*/ +case OP_MakeRecord: { + u8 *zNewRecord; /* A buffer to hold the data for the new record */ + Mem *pRec; /* The new record */ + u64 nData; /* Number of bytes of data space */ + int nHdr; /* Number of bytes of header space */ + i64 nByte; /* Data space required for this record */ + i64 nZero; /* Number of zero bytes at the end of the record */ + int nVarint; /* Number of bytes in a varint */ + u32 serial_type; /* Type field */ + Mem *pData0; /* First field to be combined into the record */ + Mem *pLast; /* Last field of the record */ + int nField; /* Number of fields in the record */ + char *zAffinity; /* The affinity string for the record */ + int file_format; /* File format to use for encoding */ + int i; /* Space used in zNewRecord[] header */ + int j; /* Space used in zNewRecord[] content */ + u32 len; /* Length of a field */ + + /* Assuming the record contains N fields, the record format looks + ** like this: + ** + ** ------------------------------------------------------------------------ + ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | + ** ------------------------------------------------------------------------ + ** + ** Data(0) is taken from register P1. Data(1) comes from register P1+1 + ** and so forth. + ** + ** Each type field is a varint representing the serial type of the + ** corresponding data element (see sqlite3VdbeSerialType()). The + ** hdr-size field is also a varint which is the offset from the beginning + ** of the record to data0. + */ + nData = 0; /* Number of bytes of data space */ + nHdr = 0; /* Number of bytes of header space */ + nZero = 0; /* Number of zero bytes at the end of the record */ + nField = pOp->p1; + zAffinity = pOp->p4.z; + assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); + pData0 = &aMem[nField]; + nField = pOp->p2; + pLast = &pData0[nField-1]; + file_format = p->minWriteFileFormat; + + /* Identify the output register */ + assert( pOp->p3p1 || pOp->p3>=pOp->p1+pOp->p2 ); + pOut = &aMem[pOp->p3]; + memAboutToChange(p, pOut); + + /* Apply the requested affinity to all inputs + */ + assert( pData0<=pLast ); + if( zAffinity ){ + pRec = pData0; + do{ + applyAffinity(pRec++, *(zAffinity++), encoding); + assert( zAffinity[0]==0 || pRec<=pLast ); + }while( zAffinity[0] ); + } + + /* Loop through the elements that will make up the record to figure + ** out how much space is required for the new record. + */ + pRec = pLast; + do{ + assert( memIsValid(pRec) ); + pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); + if( pRec->flags & MEM_Zero ){ + if( nData ){ + if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; + }else{ + nZero += pRec->u.nZero; + len -= pRec->u.nZero; + } + } + nData += len; + testcase( serial_type==127 ); + testcase( serial_type==128 ); + nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); + if( pRec==pData0 ) break; + pRec--; + }while(1); + + /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint + ** which determines the total number of bytes in the header. The varint + ** value is the size of the header in bytes including the size varint + ** itself. */ + testcase( nHdr==126 ); + testcase( nHdr==127 ); + if( nHdr<=126 ){ + /* The common case */ + nHdr += 1; + }else{ + /* Rare case of a really large header */ + nVarint = sqlite3VarintLen(nHdr); + nHdr += nVarint; + if( nVarintdb->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + + /* Make sure the output register has a buffer large enough to store + ** the new record. The output register (pOp->p3) is not allowed to + ** be one of the input registers (because the following call to + ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). + */ + if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ + goto no_mem; + } + zNewRecord = (u8 *)pOut->z; + + /* Write the record */ + i = putVarint32(zNewRecord, nHdr); + j = nHdr; + assert( pData0<=pLast ); + pRec = pData0; + do{ + serial_type = pRec->uTemp; + /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more + ** additional varints, one per column. */ + i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ + /* EVIDENCE-OF: R-64536-51728 The values for each column in the record + ** immediately follow the header. */ + j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ + }while( (++pRec)<=pLast ); + assert( i==nHdr ); + assert( j==nByte ); + + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + pOut->n = (int)nByte; + pOut->flags = MEM_Blob; + if( nZero ){ + pOut->u.nZero = nZero; + pOut->flags |= MEM_Zero; + } + pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ + REGISTER_TRACE(pOp->p3, pOut); + UPDATE_MAX_BLOBSIZE(pOut); + break; +} + +/* Opcode: Count P1 P2 * * * +** Synopsis: r[P2]=count() +** +** Store the number of entries (an integer value) in the table or index +** opened by cursor P1 in register P2 +*/ +#ifndef SQLITE_OMIT_BTREECOUNT +case OP_Count: { /* out2 */ + i64 nEntry; + BtCursor *pCrsr; + + assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); + pCrsr = p->apCsr[pOp->p1]->uc.pCursor; + assert( pCrsr ); + nEntry = 0; /* Not needed. Only used to silence a warning. */ + rc = sqlite3BtreeCount(pCrsr, &nEntry); + if( rc ) goto abort_due_to_error; + pOut = out2Prerelease(p, pOp); + pOut->u.i = nEntry; + break; +} +#endif + +/* Opcode: Savepoint P1 * * P4 * +** +** Open, release or rollback the savepoint named by parameter P4, depending +** on the value of P1. To open a new savepoint, P1==0. To release (commit) an +** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. +*/ +case OP_Savepoint: { + int p1; /* Value of P1 operand */ + char *zName; /* Name of savepoint */ + int nName; + Savepoint *pNew; + Savepoint *pSavepoint; + Savepoint *pTmp; + int iSavepoint; + int ii; + + p1 = pOp->p1; + zName = pOp->p4.z; + + /* Assert that the p1 parameter is valid. Also that if there is no open + ** transaction, then there cannot be any savepoints. + */ + assert( db->pSavepoint==0 || db->autoCommit==0 ); + assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); + assert( db->pSavepoint || db->isTransactionSavepoint==0 ); + assert( checkSavepointCount(db) ); + assert( p->bIsReader ); + + if( p1==SAVEPOINT_BEGIN ){ + if( db->nVdbeWrite>0 ){ + /* A new savepoint cannot be created if there are active write + ** statements (i.e. open read/write incremental blob handles). + */ + sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); + rc = SQLITE_BUSY; + }else{ + nName = sqlite3Strlen30(zName); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* This call is Ok even if this savepoint is actually a transaction + ** savepoint (and therefore should not prompt xSavepoint()) callbacks. + ** If this is a transaction savepoint being opened, it is guaranteed + ** that the db->aVTrans[] array is empty. */ + assert( db->autoCommit==0 || db->nVTrans==0 ); + rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, + db->nStatement+db->nSavepoint); + if( rc!=SQLITE_OK ) goto abort_due_to_error; +#endif + + /* Create a new savepoint structure. */ + pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); + if( pNew ){ + pNew->zName = (char *)&pNew[1]; + memcpy(pNew->zName, zName, nName+1); + + /* If there is no open transaction, then mark this as a special + ** "transaction savepoint". */ + if( db->autoCommit ){ + db->autoCommit = 0; + db->isTransactionSavepoint = 1; + }else{ + db->nSavepoint++; + } + + /* Link the new savepoint into the database handle's list. */ + pNew->pNext = db->pSavepoint; + db->pSavepoint = pNew; + pNew->nDeferredCons = db->nDeferredCons; + pNew->nDeferredImmCons = db->nDeferredImmCons; + } + } + }else{ + iSavepoint = 0; + + /* Find the named savepoint. If there is no such savepoint, then an + ** an error is returned to the user. */ + for( + pSavepoint = db->pSavepoint; + pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); + pSavepoint = pSavepoint->pNext + ){ + iSavepoint++; + } + if( !pSavepoint ){ + sqlite3VdbeError(p, "no such savepoint: %s", zName); + rc = SQLITE_ERROR; + }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ + /* It is not possible to release (commit) a savepoint if there are + ** active write statements. + */ + sqlite3VdbeError(p, "cannot release savepoint - " + "SQL statements in progress"); + rc = SQLITE_BUSY; + }else{ + + /* Determine whether or not this is a transaction savepoint. If so, + ** and this is a RELEASE command, then the current transaction + ** is committed. + */ + int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; + if( isTransaction && p1==SAVEPOINT_RELEASE ){ + if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ + goto vdbe_return; + } + db->autoCommit = 1; + if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ + p->pc = (int)(pOp - aOp); + db->autoCommit = 0; + p->rc = rc = SQLITE_BUSY; + goto vdbe_return; + } + db->isTransactionSavepoint = 0; + rc = p->rc; + }else{ + int isSchemaChange; + iSavepoint = db->nSavepoint - iSavepoint - 1; + if( p1==SAVEPOINT_ROLLBACK ){ + isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; + for(ii=0; iinDb; ii++){ + rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, + SQLITE_ABORT_ROLLBACK, + isSchemaChange==0); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + } + }else{ + isSchemaChange = 0; + } + for(ii=0; iinDb; ii++){ + rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + } + if( isSchemaChange ){ + sqlite3ExpirePreparedStatements(db); + sqlite3ResetAllSchemasOfConnection(db); + db->flags = (db->flags | SQLITE_InternChanges); + } + } + + /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all + ** savepoints nested inside of the savepoint being operated on. */ + while( db->pSavepoint!=pSavepoint ){ + pTmp = db->pSavepoint; + db->pSavepoint = pTmp->pNext; + sqlite3DbFree(db, pTmp); + db->nSavepoint--; + } + + /* If it is a RELEASE, then destroy the savepoint being operated on + ** too. If it is a ROLLBACK TO, then set the number of deferred + ** constraint violations present in the database to the value stored + ** when the savepoint was created. */ + if( p1==SAVEPOINT_RELEASE ){ + assert( pSavepoint==db->pSavepoint ); + db->pSavepoint = pSavepoint->pNext; + sqlite3DbFree(db, pSavepoint); + if( !isTransaction ){ + db->nSavepoint--; + } + }else{ + db->nDeferredCons = pSavepoint->nDeferredCons; + db->nDeferredImmCons = pSavepoint->nDeferredImmCons; + } + + if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ + rc = sqlite3VtabSavepoint(db, p1, iSavepoint); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + } + } + } + if( rc ) goto abort_due_to_error; + + break; +} + +/* Opcode: AutoCommit P1 P2 * * * +** +** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll +** back any currently active btree transactions. If there are any active +** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if +** there are active writing VMs or active VMs that use shared cache. +** +** This instruction causes the VM to halt. +*/ +case OP_AutoCommit: { + int desiredAutoCommit; + int iRollback; + + desiredAutoCommit = pOp->p1; + iRollback = pOp->p2; + assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); + assert( desiredAutoCommit==1 || iRollback==0 ); + assert( db->nVdbeActive>0 ); /* At least this one VM is active */ + assert( p->bIsReader ); + + if( desiredAutoCommit!=db->autoCommit ){ + if( iRollback ){ + assert( desiredAutoCommit==1 ); + sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); + db->autoCommit = 1; + }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ + /* If this instruction implements a COMMIT and other VMs are writing + ** return an error indicating that the other VMs must complete first. + */ + sqlite3VdbeError(p, "cannot commit transaction - " + "SQL statements in progress"); + rc = SQLITE_BUSY; + goto abort_due_to_error; + }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ + goto vdbe_return; + }else{ + db->autoCommit = (u8)desiredAutoCommit; + } + if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ + p->pc = (int)(pOp - aOp); + db->autoCommit = (u8)(1-desiredAutoCommit); + p->rc = rc = SQLITE_BUSY; + goto vdbe_return; + } + assert( db->nStatement==0 ); + sqlite3CloseSavepoints(db); + if( p->rc==SQLITE_OK ){ + rc = SQLITE_DONE; + }else{ + rc = SQLITE_ERROR; + } + goto vdbe_return; + }else{ + sqlite3VdbeError(p, + (!desiredAutoCommit)?"cannot start a transaction within a transaction":( + (iRollback)?"cannot rollback - no transaction is active": + "cannot commit - no transaction is active")); + + rc = SQLITE_ERROR; + goto abort_due_to_error; + } + break; +} + +/* Opcode: Transaction P1 P2 P3 P4 P5 +** +** Begin a transaction on database P1 if a transaction is not already +** active. +** If P2 is non-zero, then a write-transaction is started, or if a +** read-transaction is already active, it is upgraded to a write-transaction. +** If P2 is zero, then a read-transaction is started. +** +** P1 is the index of the database file on which the transaction is +** started. Index 0 is the main database file and index 1 is the +** file used for temporary tables. Indices of 2 or more are used for +** attached databases. +** +** If a write-transaction is started and the Vdbe.usesStmtJournal flag is +** true (this flag is set if the Vdbe may modify more than one row and may +** throw an ABORT exception), a statement transaction may also be opened. +** More specifically, a statement transaction is opened iff the database +** connection is currently not in autocommit mode, or if there are other +** active statements. A statement transaction allows the changes made by this +** VDBE to be rolled back after an error without having to roll back the +** entire transaction. If no error is encountered, the statement transaction +** will automatically commit when the VDBE halts. +** +** If P5!=0 then this opcode also checks the schema cookie against P3 +** and the schema generation counter against P4. +** The cookie changes its value whenever the database schema changes. +** This operation is used to detect when that the cookie has changed +** and that the current process needs to reread the schema. If the schema +** cookie in P3 differs from the schema cookie in the database header or +** if the schema generation counter in P4 differs from the current +** generation counter, then an SQLITE_SCHEMA error is raised and execution +** halts. The sqlite3_step() wrapper function might then reprepare the +** statement and rerun it from the beginning. +*/ +case OP_Transaction: { + Btree *pBt; + int iMeta; + int iGen; + + assert( p->bIsReader ); + assert( p->readOnly==0 || pOp->p2==0 ); + assert( pOp->p1>=0 && pOp->p1nDb ); + assert( DbMaskTest(p->btreeMask, pOp->p1) ); + if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ + rc = SQLITE_READONLY; + goto abort_due_to_error; + } + pBt = db->aDb[pOp->p1].pBt; + + if( pBt ){ + rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); + testcase( rc==SQLITE_BUSY_SNAPSHOT ); + testcase( rc==SQLITE_BUSY_RECOVERY ); + if( (rc&0xff)==SQLITE_BUSY ){ + p->pc = (int)(pOp - aOp); + p->rc = rc; + goto vdbe_return; + } + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + + if( pOp->p2 && p->usesStmtJournal + && (db->autoCommit==0 || db->nVdbeRead>1) + ){ + assert( sqlite3BtreeIsInTrans(pBt) ); + if( p->iStatement==0 ){ + assert( db->nStatement>=0 && db->nSavepoint>=0 ); + db->nStatement++; + p->iStatement = db->nSavepoint + db->nStatement; + } + + rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); + } + + /* Store the current value of the database handles deferred constraint + ** counter. If the statement transaction needs to be rolled back, + ** the value of this counter needs to be restored too. */ + p->nStmtDefCons = db->nDeferredCons; + p->nStmtDefImmCons = db->nDeferredImmCons; + } + + /* Gather the schema version number for checking: + ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite + ** each time a query is executed to ensure that the internal cache of the + ** schema used when compiling the SQL query matches the schema of the + ** database against which the compiled query is actually executed. + */ + sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); + iGen = db->aDb[pOp->p1].pSchema->iGeneration; + }else{ + iGen = iMeta = 0; + } + assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); + if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); + /* If the schema-cookie from the database file matches the cookie + ** stored with the in-memory representation of the schema, do + ** not reload the schema from the database file. + ** + ** If virtual-tables are in use, this is not just an optimization. + ** Often, v-tables store their data in other SQLite tables, which + ** are queried from within xNext() and other v-table methods using + ** prepared queries. If such a query is out-of-date, we do not want to + ** discard the database schema, as the user code implementing the + ** v-table would have to be ready for the sqlite3_vtab structure itself + ** to be invalidated whenever sqlite3_step() is called from within + ** a v-table method. + */ + if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ + sqlite3ResetOneSchema(db, pOp->p1); + } + p->expired = 1; + rc = SQLITE_SCHEMA; + } + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: ReadCookie P1 P2 P3 * * +** +** Read cookie number P3 from database P1 and write it into register P2. +** P3==1 is the schema version. P3==2 is the database format. +** P3==3 is the recommended pager cache size, and so forth. P1==0 is +** the main database file and P1==1 is the database file used to store +** temporary tables. +** +** There must be a read-lock on the database (either a transaction +** must be started or there must be an open cursor) before +** executing this instruction. +*/ +case OP_ReadCookie: { /* out2 */ + int iMeta; + int iDb; + int iCookie; + + assert( p->bIsReader ); + iDb = pOp->p1; + iCookie = pOp->p3; + assert( pOp->p3=0 && iDbnDb ); + assert( db->aDb[iDb].pBt!=0 ); + assert( DbMaskTest(p->btreeMask, iDb) ); + + sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); + pOut = out2Prerelease(p, pOp); + pOut->u.i = iMeta; + break; +} + +/* Opcode: SetCookie P1 P2 P3 * * +** +** Write the integer value P3 into cookie number P2 of database P1. +** P2==1 is the schema version. P2==2 is the database format. +** P2==3 is the recommended pager cache +** size, and so forth. P1==0 is the main database file and P1==1 is the +** database file used to store temporary tables. +** +** A transaction must be started before executing this opcode. +*/ +case OP_SetCookie: { + Db *pDb; + assert( pOp->p2p1>=0 && pOp->p1nDb ); + assert( DbMaskTest(p->btreeMask, pOp->p1) ); + assert( p->readOnly==0 ); + pDb = &db->aDb[pOp->p1]; + assert( pDb->pBt!=0 ); + assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); + /* See note about index shifting on OP_ReadCookie */ + rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); + if( pOp->p2==BTREE_SCHEMA_VERSION ){ + /* When the schema cookie changes, record the new cookie internally */ + pDb->pSchema->schema_cookie = pOp->p3; + db->flags |= SQLITE_InternChanges; + }else if( pOp->p2==BTREE_FILE_FORMAT ){ + /* Record changes in the file format */ + pDb->pSchema->file_format = pOp->p3; + } + if( pOp->p1==1 ){ + /* Invalidate all prepared statements whenever the TEMP database + ** schema is changed. Ticket #1644 */ + sqlite3ExpirePreparedStatements(db); + p->expired = 0; + } + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: OpenRead P1 P2 P3 P4 P5 +** Synopsis: root=P2 iDb=P3 +** +** Open a read-only cursor for the database table whose root page is +** P2 in a database file. The database file is determined by P3. +** P3==0 means the main database, P3==1 means the database used for +** temporary tables, and P3>1 means used the corresponding attached +** database. Give the new cursor an identifier of P1. The P1 +** values need not be contiguous but all P1 values should be small integers. +** It is an error for P1 to be negative. +** +** If P5!=0 then use the content of register P2 as the root page, not +** the value of P2 itself. +** +** There will be a read lock on the database whenever there is an +** open cursor. If the database was unlocked prior to this instruction +** then a read lock is acquired as part of this instruction. A read +** lock allows other processes to read the database but prohibits +** any other process from modifying the database. The read lock is +** released when all cursors are closed. If this instruction attempts +** to get a read lock but fails, the script terminates with an +** SQLITE_BUSY error code. +** +** The P4 value may be either an integer (P4_INT32) or a pointer to +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo +** structure, then said structure defines the content and collating +** sequence of the index being opened. Otherwise, if P4 is an integer +** value, it is set to the number of columns in the table. +** +** See also: OpenWrite, ReopenIdx +*/ +/* Opcode: ReopenIdx P1 P2 P3 P4 P5 +** Synopsis: root=P2 iDb=P3 +** +** The ReopenIdx opcode works exactly like ReadOpen except that it first +** checks to see if the cursor on P1 is already open with a root page +** number of P2 and if it is this opcode becomes a no-op. In other words, +** if the cursor is already open, do not reopen it. +** +** The ReopenIdx opcode may only be used with P5==0 and with P4 being +** a P4_KEYINFO object. Furthermore, the P3 value must be the same as +** every other ReopenIdx or OpenRead for the same cursor number. +** +** See the OpenRead opcode documentation for additional information. +*/ +/* Opcode: OpenWrite P1 P2 P3 P4 P5 +** Synopsis: root=P2 iDb=P3 +** +** Open a read/write cursor named P1 on the table or index whose root +** page is P2. Or if P5!=0 use the content of register P2 to find the +** root page. +** +** The P4 value may be either an integer (P4_INT32) or a pointer to +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo +** structure, then said structure defines the content and collating +** sequence of the index being opened. Otherwise, if P4 is an integer +** value, it is set to the number of columns in the table, or to the +** largest index of any column of the table that is actually used. +** +** This instruction works just like OpenRead except that it opens the cursor +** in read/write mode. For a given table, there can be one or more read-only +** cursors or a single read/write cursor but not both. +** +** See also OpenRead. +*/ +case OP_ReopenIdx: { + int nField; + KeyInfo *pKeyInfo; + int p2; + int iDb; + int wrFlag; + Btree *pX; + VdbeCursor *pCur; + Db *pDb; + + assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); + assert( pOp->p4type==P4_KEYINFO ); + pCur = p->apCsr[pOp->p1]; + if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ + assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ + goto open_cursor_set_hints; + } + /* If the cursor is not currently open or is open on a different + ** index, then fall through into OP_OpenRead to force a reopen */ +case OP_OpenRead: +case OP_OpenWrite: + + assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); + assert( p->bIsReader ); + assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx + || p->readOnly==0 ); + + if( p->expired ){ + rc = SQLITE_ABORT_ROLLBACK; + goto abort_due_to_error; + } + + nField = 0; + pKeyInfo = 0; + p2 = pOp->p2; + iDb = pOp->p3; + assert( iDb>=0 && iDbnDb ); + assert( DbMaskTest(p->btreeMask, iDb) ); + pDb = &db->aDb[iDb]; + pX = pDb->pBt; + assert( pX!=0 ); + if( pOp->opcode==OP_OpenWrite ){ + assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); + wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( pDb->pSchema->file_format < p->minWriteFileFormat ){ + p->minWriteFileFormat = pDb->pSchema->file_format; + } + }else{ + wrFlag = 0; + } + if( pOp->p5 & OPFLAG_P2ISREG ){ + assert( p2>0 ); + assert( p2<=(p->nMem+1 - p->nCursor) ); + pIn2 = &aMem[p2]; + assert( memIsValid(pIn2) ); + assert( (pIn2->flags & MEM_Int)!=0 ); + sqlite3VdbeMemIntegerify(pIn2); + p2 = (int)pIn2->u.i; + /* The p2 value always comes from a prior OP_CreateTable opcode and + ** that opcode will always set the p2 value to 2 or more or else fail. + ** If there were a failure, the prepared statement would have halted + ** before reaching this instruction. */ + assert( p2>=2 ); + } + if( pOp->p4type==P4_KEYINFO ){ + pKeyInfo = pOp->p4.pKeyInfo; + assert( pKeyInfo->enc==ENC(db) ); + assert( pKeyInfo->db==db ); + nField = pKeyInfo->nField+pKeyInfo->nXField; + }else if( pOp->p4type==P4_INT32 ){ + nField = pOp->p4.i; + } + assert( pOp->p1>=0 ); + assert( nField>=0 ); + testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ + pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); + if( pCur==0 ) goto no_mem; + pCur->nullRow = 1; + pCur->isOrdered = 1; + pCur->pgnoRoot = p2; +#ifdef SQLITE_DEBUG + pCur->wrFlag = wrFlag; +#endif + rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); + pCur->pKeyInfo = pKeyInfo; + /* Set the VdbeCursor.isTable variable. Previous versions of + ** SQLite used to check if the root-page flags were sane at this point + ** and report database corruption if they were not, but this check has + ** since moved into the btree layer. */ + pCur->isTable = pOp->p4type!=P4_KEYINFO; + +open_cursor_set_hints: + assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); + assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); + testcase( pOp->p5 & OPFLAG_BULKCSR ); +#ifdef SQLITE_ENABLE_CURSOR_HINTS + testcase( pOp->p2 & OPFLAG_SEEKEQ ); +#endif + sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, + (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: OpenEphemeral P1 P2 * P4 P5 +** Synopsis: nColumn=P2 +** +** Open a new cursor P1 to a transient table. +** The cursor is always opened read/write even if +** the main database is read-only. The ephemeral +** table is deleted automatically when the cursor is closed. +** +** P2 is the number of columns in the ephemeral table. +** The cursor points to a BTree table if P4==0 and to a BTree index +** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure +** that defines the format of keys in the index. +** +** The P5 parameter can be a mask of the BTREE_* flags defined +** in btree.h. These flags control aspects of the operation of +** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are +** added automatically. +*/ +/* Opcode: OpenAutoindex P1 P2 * P4 * +** Synopsis: nColumn=P2 +** +** This opcode works the same as OP_OpenEphemeral. It has a +** different name to distinguish its use. Tables created using +** by this opcode will be used for automatically created transient +** indices in joins. +*/ +case OP_OpenAutoindex: +case OP_OpenEphemeral: { + VdbeCursor *pCx; + KeyInfo *pKeyInfo; + + static const int vfsFlags = + SQLITE_OPEN_READWRITE | + SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | + SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_TRANSIENT_DB; + assert( pOp->p1>=0 ); + assert( pOp->p2>=0 ); + pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); + if( pCx==0 ) goto no_mem; + pCx->nullRow = 1; + pCx->isEphemeral = 1; + rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, + BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); + } + if( rc==SQLITE_OK ){ + /* If a transient index is required, create it by calling + ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before + ** opening it. If a transient table is required, just use the + ** automatically created table with root-page 1 (an BLOB_INTKEY table). + */ + if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ + int pgno; + assert( pOp->p4type==P4_KEYINFO ); + rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); + if( rc==SQLITE_OK ){ + assert( pgno==MASTER_ROOT+1 ); + assert( pKeyInfo->db==db ); + assert( pKeyInfo->enc==ENC(db) ); + pCx->pKeyInfo = pKeyInfo; + rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR, + pKeyInfo, pCx->uc.pCursor); + } + pCx->isTable = 0; + }else{ + rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, + 0, pCx->uc.pCursor); + pCx->isTable = 1; + } + } + if( rc ) goto abort_due_to_error; + pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); + break; +} + +/* Opcode: SorterOpen P1 P2 P3 P4 * +** +** This opcode works like OP_OpenEphemeral except that it opens +** a transient index that is specifically designed to sort large +** tables using an external merge-sort algorithm. +** +** If argument P3 is non-zero, then it indicates that the sorter may +** assume that a stable sort considering the first P3 fields of each +** key is sufficient to produce the required results. +*/ +case OP_SorterOpen: { + VdbeCursor *pCx; + + assert( pOp->p1>=0 ); + assert( pOp->p2>=0 ); + pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); + if( pCx==0 ) goto no_mem; + pCx->pKeyInfo = pOp->p4.pKeyInfo; + assert( pCx->pKeyInfo->db==db ); + assert( pCx->pKeyInfo->enc==ENC(db) ); + rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: SequenceTest P1 P2 * * * +** Synopsis: if( cursor[P1].ctr++ ) pc = P2 +** +** P1 is a sorter cursor. If the sequence counter is currently zero, jump +** to P2. Regardless of whether or not the jump is taken, increment the +** the sequence value. +*/ +case OP_SequenceTest: { + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( isSorter(pC) ); + if( (pC->seqCount++)==0 ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: OpenPseudo P1 P2 P3 * * +** Synopsis: P3 columns in r[P2] +** +** Open a new cursor that points to a fake table that contains a single +** row of data. The content of that one row is the content of memory +** register P2. In other words, cursor P1 becomes an alias for the +** MEM_Blob content contained in register P2. +** +** A pseudo-table created by this opcode is used to hold a single +** row output from the sorter so that the row can be decomposed into +** individual columns using the OP_Column opcode. The OP_Column opcode +** is the only cursor opcode that works with a pseudo-table. +** +** P3 is the number of fields in the records that will be stored by +** the pseudo-table. +*/ +case OP_OpenPseudo: { + VdbeCursor *pCx; + + assert( pOp->p1>=0 ); + assert( pOp->p3>=0 ); + pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); + if( pCx==0 ) goto no_mem; + pCx->nullRow = 1; + pCx->uc.pseudoTableReg = pOp->p2; + pCx->isTable = 1; + assert( pOp->p5==0 ); + break; +} + +/* Opcode: Close P1 * * * * +** +** Close a cursor previously opened as P1. If P1 is not +** currently open, this instruction is a no-op. +*/ +case OP_Close: { + assert( pOp->p1>=0 && pOp->p1nCursor ); + sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); + p->apCsr[pOp->p1] = 0; + break; +} + +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK +/* Opcode: ColumnsUsed P1 * * P4 * +** +** This opcode (which only exists if SQLite was compiled with +** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the +** table or index for cursor P1 are used. P4 is a 64-bit integer +** (P4_INT64) in which the first 63 bits are one for each of the +** first 63 columns of the table or index that are actually used +** by the cursor. The high-order bit is set if any column after +** the 64th is used. +*/ +case OP_ColumnsUsed: { + VdbeCursor *pC; + pC = p->apCsr[pOp->p1]; + assert( pC->eCurType==CURTYPE_BTREE ); + pC->maskUsed = *(u64*)pOp->p4.pI64; + break; +} +#endif + +/* Opcode: SeekGE P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as the key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. +** +** Reposition cursor P1 so that it points to the smallest entry that +** is greater than or equal to the key value. If there are no records +** greater than or equal to the key and P2 is not zero, then jump to P2. +** +** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this +** opcode will always land on a record that equally equals the key, or +** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this +** opcode must be followed by an IdxLE opcode with the same arguments. +** The IdxLE opcode will be skipped if this opcode succeeds, but the +** IdxLE opcode will be used on subsequent loop iterations. +** +** This opcode leaves the cursor configured to move in forward order, +** from the beginning toward the end. In other words, the cursor is +** configured to use Next, not Prev. +** +** See also: Found, NotFound, SeekLt, SeekGt, SeekLe +*/ +/* Opcode: SeekGT P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. +** +** Reposition cursor P1 so that it points to the smallest entry that +** is greater than the key value. If there are no records greater than +** the key and P2 is not zero, then jump to P2. +** +** This opcode leaves the cursor configured to move in forward order, +** from the beginning toward the end. In other words, the cursor is +** configured to use Next, not Prev. +** +** See also: Found, NotFound, SeekLt, SeekGe, SeekLe +*/ +/* Opcode: SeekLT P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. +** +** Reposition cursor P1 so that it points to the largest entry that +** is less than the key value. If there are no records less than +** the key and P2 is not zero, then jump to P2. +** +** This opcode leaves the cursor configured to move in reverse order, +** from the end toward the beginning. In other words, the cursor is +** configured to use Prev, not Next. +** +** See also: Found, NotFound, SeekGt, SeekGe, SeekLe +*/ +/* Opcode: SeekLE P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. +** +** Reposition cursor P1 so that it points to the largest entry that +** is less than or equal to the key value. If there are no records +** less than or equal to the key and P2 is not zero, then jump to P2. +** +** This opcode leaves the cursor configured to move in reverse order, +** from the end toward the beginning. In other words, the cursor is +** configured to use Prev, not Next. +** +** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this +** opcode will always land on a record that equally equals the key, or +** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this +** opcode must be followed by an IdxGE opcode with the same arguments. +** The IdxGE opcode will be skipped if this opcode succeeds, but the +** IdxGE opcode will be used on subsequent loop iterations. +** +** See also: Found, NotFound, SeekGt, SeekGe, SeekLt +*/ +case OP_SeekLT: /* jump, in3 */ +case OP_SeekLE: /* jump, in3 */ +case OP_SeekGE: /* jump, in3 */ +case OP_SeekGT: { /* jump, in3 */ + int res; /* Comparison result */ + int oc; /* Opcode */ + VdbeCursor *pC; /* The cursor to seek */ + UnpackedRecord r; /* The key to seek for */ + int nField; /* Number of columns or fields in the key */ + i64 iKey; /* The rowid we are to seek to */ + int eqOnly; /* Only interested in == results */ + + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pOp->p2!=0 ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( OP_SeekLE == OP_SeekLT+1 ); + assert( OP_SeekGE == OP_SeekLT+2 ); + assert( OP_SeekGT == OP_SeekLT+3 ); + assert( pC->isOrdered ); + assert( pC->uc.pCursor!=0 ); + oc = pOp->opcode; + eqOnly = 0; + pC->nullRow = 0; +#ifdef SQLITE_DEBUG + pC->seekOp = pOp->opcode; +#endif + + if( pC->isTable ){ + /* The BTREE_SEEK_EQ flag is only set on index cursors */ + assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 ); + + /* The input value in P3 might be of any type: integer, real, string, + ** blob, or NULL. But it needs to be an integer before we can do + ** the seek, so convert it. */ + pIn3 = &aMem[pOp->p3]; + if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ + applyNumericAffinity(pIn3, 0); + } + iKey = sqlite3VdbeIntValue(pIn3); + + /* If the P3 value could not be converted into an integer without + ** loss of information, then special processing is required... */ + if( (pIn3->flags & MEM_Int)==0 ){ + if( (pIn3->flags & MEM_Real)==0 ){ + /* If the P3 value cannot be converted into any kind of a number, + ** then the seek is not possible, so jump to P2 */ + VdbeBranchTaken(1,2); goto jump_to_p2; + break; + } + + /* If the approximation iKey is larger than the actual real search + ** term, substitute >= for > and < for <=. e.g. if the search term + ** is 4.9 and the integer approximation 5: + ** + ** (x > 4.9) -> (x >= 5) + ** (x <= 4.9) -> (x < 5) + */ + if( pIn3->u.r<(double)iKey ){ + assert( OP_SeekGE==(OP_SeekGT-1) ); + assert( OP_SeekLT==(OP_SeekLE-1) ); + assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); + if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; + } + + /* If the approximation iKey is smaller than the actual real search + ** term, substitute <= for < and > for >=. */ + else if( pIn3->u.r>(double)iKey ){ + assert( OP_SeekLE==(OP_SeekLT+1) ); + assert( OP_SeekGT==(OP_SeekGE+1) ); + assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); + if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; + } + } + rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); + pC->movetoTarget = iKey; /* Used by OP_Delete */ + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + }else{ + /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and + ** OP_SeekLE opcodes are allowed, and these must be immediately followed + ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. + */ + if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ + eqOnly = 1; + assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); + assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); + assert( pOp[1].p1==pOp[0].p1 ); + assert( pOp[1].p2==pOp[0].p2 ); + assert( pOp[1].p3==pOp[0].p3 ); + assert( pOp[1].p4.i==pOp[0].p4.i ); + } + + nField = pOp->p4.i; + assert( pOp->p4type==P4_INT32 ); + assert( nField>0 ); + r.pKeyInfo = pC->pKeyInfo; + r.nField = (u16)nField; + + /* The next line of code computes as follows, only faster: + ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ + ** r.default_rc = -1; + ** }else{ + ** r.default_rc = +1; + ** } + */ + r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); + assert( oc!=OP_SeekGT || r.default_rc==-1 ); + assert( oc!=OP_SeekLE || r.default_rc==-1 ); + assert( oc!=OP_SeekGE || r.default_rc==+1 ); + assert( oc!=OP_SeekLT || r.default_rc==+1 ); + + r.aMem = &aMem[pOp->p3]; +#ifdef SQLITE_DEBUG + { int i; for(i=0; iuc.pCursor, &r, 0, 0, &res); + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + if( eqOnly && r.eqSeen==0 ){ + assert( res!=0 ); + goto seek_not_found; + } + } + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; +#ifdef SQLITE_TEST + sqlite3_search_count++; +#endif + if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); + if( res<0 || (res==0 && oc==OP_SeekGT) ){ + res = 0; + rc = sqlite3BtreeNext(pC->uc.pCursor, &res); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + }else{ + res = 0; + } + }else{ + assert( oc==OP_SeekLT || oc==OP_SeekLE ); + if( res>0 || (res==0 && oc==OP_SeekLT) ){ + res = 0; + rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + }else{ + /* res might be negative because the table is empty. Check to + ** see if this is the case. + */ + res = sqlite3BtreeEof(pC->uc.pCursor); + } + } +seek_not_found: + assert( pOp->p2>0 ); + VdbeBranchTaken(res!=0,2); + if( res ){ + goto jump_to_p2; + }else if( eqOnly ){ + assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); + pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ + } + break; +} + + +/* Opcode: Found P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If P4==0 then register P3 holds a blob constructed by MakeRecord. If +** P4>0 then register P3 is the first of P4 registers that form an unpacked +** record. +** +** Cursor P1 is on an index btree. If the record identified by P3 and P4 +** is a prefix of any entry in P1 then a jump is made to P2 and +** P1 is left pointing at the matching entry. +** +** This operation leaves the cursor in a state where it can be +** advanced in the forward direction. The Next instruction will work, +** but not the Prev instruction. +** +** See also: NotFound, NoConflict, NotExists. SeekGe +*/ +/* Opcode: NotFound P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If P4==0 then register P3 holds a blob constructed by MakeRecord. If +** P4>0 then register P3 is the first of P4 registers that form an unpacked +** record. +** +** Cursor P1 is on an index btree. If the record identified by P3 and P4 +** is not the prefix of any entry in P1 then a jump is made to P2. If P1 +** does contain an entry whose prefix matches the P3/P4 record then control +** falls through to the next instruction and P1 is left pointing at the +** matching entry. +** +** This operation leaves the cursor in a state where it cannot be +** advanced in either direction. In other words, the Next and Prev +** opcodes do not work after this operation. +** +** See also: Found, NotExists, NoConflict +*/ +/* Opcode: NoConflict P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** If P4==0 then register P3 holds a blob constructed by MakeRecord. If +** P4>0 then register P3 is the first of P4 registers that form an unpacked +** record. +** +** Cursor P1 is on an index btree. If the record identified by P3 and P4 +** contains any NULL value, jump immediately to P2. If all terms of the +** record are not-NULL then a check is done to determine if any row in the +** P1 index btree has a matching key prefix. If there are no matches, jump +** immediately to P2. If there is a match, fall through and leave the P1 +** cursor pointing to the matching row. +** +** This opcode is similar to OP_NotFound with the exceptions that the +** branch is always taken if any part of the search key input is NULL. +** +** This operation leaves the cursor in a state where it cannot be +** advanced in either direction. In other words, the Next and Prev +** opcodes do not work after this operation. +** +** See also: NotFound, Found, NotExists +*/ +case OP_NoConflict: /* jump, in3 */ +case OP_NotFound: /* jump, in3 */ +case OP_Found: { /* jump, in3 */ + int alreadyExists; + int takeJump; + int ii; + VdbeCursor *pC; + int res; + char *pFree; + UnpackedRecord *pIdxKey; + UnpackedRecord r; + char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7]; + +#ifdef SQLITE_TEST + if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; +#endif + + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pOp->p4type==P4_INT32 ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); +#ifdef SQLITE_DEBUG + pC->seekOp = pOp->opcode; +#endif + pIn3 = &aMem[pOp->p3]; + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + assert( pC->isTable==0 ); + pFree = 0; + if( pOp->p4.i>0 ){ + r.pKeyInfo = pC->pKeyInfo; + r.nField = (u16)pOp->p4.i; + r.aMem = pIn3; + for(ii=0; iip3+ii, &r.aMem[ii]); +#endif + } + pIdxKey = &r; + }else{ + pIdxKey = sqlite3VdbeAllocUnpackedRecord( + pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree + ); + if( pIdxKey==0 ) goto no_mem; + assert( pIn3->flags & MEM_Blob ); + ExpandBlob(pIn3); + sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); + } + pIdxKey->default_rc = 0; + takeJump = 0; + if( pOp->opcode==OP_NoConflict ){ + /* For the OP_NoConflict opcode, take the jump if any of the + ** input fields are NULL, since any key with a NULL will not + ** conflict */ + for(ii=0; iinField; ii++){ + if( pIdxKey->aMem[ii].flags & MEM_Null ){ + takeJump = 1; + break; + } + } + } + rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); + sqlite3DbFree(db, pFree); + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + pC->seekResult = res; + alreadyExists = (res==0); + pC->nullRow = 1-alreadyExists; + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + if( pOp->opcode==OP_Found ){ + VdbeBranchTaken(alreadyExists!=0,2); + if( alreadyExists ) goto jump_to_p2; + }else{ + VdbeBranchTaken(takeJump||alreadyExists==0,2); + if( takeJump || !alreadyExists ) goto jump_to_p2; + } + break; +} + +/* Opcode: NotExists P1 P2 P3 * * +** Synopsis: intkey=r[P3] +** +** P1 is the index of a cursor open on an SQL table btree (with integer +** keys). P3 is an integer rowid. If P1 does not contain a record with +** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an +** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then +** leave the cursor pointing at that record and fall through to the next +** instruction. +** +** The OP_NotFound opcode performs the same operation on index btrees +** (with arbitrary multi-value keys). +** +** This opcode leaves the cursor in a state where it cannot be advanced +** in either direction. In other words, the Next and Prev opcodes will +** not work following this opcode. +** +** See also: Found, NotFound, NoConflict +*/ +case OP_NotExists: { /* jump, in3 */ + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + u64 iKey; + + pIn3 = &aMem[pOp->p3]; + assert( pIn3->flags & MEM_Int ); + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); +#ifdef SQLITE_DEBUG + pC->seekOp = 0; +#endif + assert( pC->isTable ); + assert( pC->eCurType==CURTYPE_BTREE ); + pCrsr = pC->uc.pCursor; + assert( pCrsr!=0 ); + res = 0; + iKey = pIn3->u.i; + rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); + assert( rc==SQLITE_OK || res==0 ); + pC->movetoTarget = iKey; /* Used by OP_Delete */ + pC->nullRow = 0; + pC->cacheStatus = CACHE_STALE; + pC->deferredMoveto = 0; + VdbeBranchTaken(res!=0,2); + pC->seekResult = res; + if( res!=0 ){ + assert( rc==SQLITE_OK ); + if( pOp->p2==0 ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + goto jump_to_p2; + } + } + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: Sequence P1 P2 * * * +** Synopsis: r[P2]=cursor[P1].ctr++ +** +** Find the next available sequence number for cursor P1. +** Write the sequence number into register P2. +** The sequence number on the cursor is incremented after this +** instruction. +*/ +case OP_Sequence: { /* out2 */ + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( p->apCsr[pOp->p1]!=0 ); + assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); + pOut = out2Prerelease(p, pOp); + pOut->u.i = p->apCsr[pOp->p1]->seqCount++; + break; +} + + +/* Opcode: NewRowid P1 P2 P3 * * +** Synopsis: r[P2]=rowid +** +** Get a new integer record number (a.k.a "rowid") used as the key to a table. +** The record number is not previously used as a key in the database +** table that cursor P1 points to. The new record number is written +** written to register P2. +** +** If P3>0 then P3 is a register in the root frame of this VDBE that holds +** the largest previously generated record number. No new record numbers are +** allowed to be less than this value. When this value reaches its maximum, +** an SQLITE_FULL error is generated. The P3 register is updated with the ' +** generated record number. This P3 mechanism is used to help implement the +** AUTOINCREMENT feature. +*/ +case OP_NewRowid: { /* out2 */ + i64 v; /* The new rowid */ + VdbeCursor *pC; /* Cursor of table to get the new rowid */ + int res; /* Result of an sqlite3BtreeLast() */ + int cnt; /* Counter to limit the number of searches */ + Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ + VdbeFrame *pFrame; /* Root frame of VDBE */ + + v = 0; + res = 0; + pOut = out2Prerelease(p, pOp); + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + { + /* The next rowid or record number (different terms for the same + ** thing) is obtained in a two-step algorithm. + ** + ** First we attempt to find the largest existing rowid and add one + ** to that. But if the largest existing rowid is already the maximum + ** positive integer, we have to fall through to the second + ** probabilistic algorithm + ** + ** The second algorithm is to select a rowid at random and see if + ** it already exists in the table. If it does not exist, we have + ** succeeded. If the random rowid does exist, we select a new one + ** and try again, up to 100 times. + */ + assert( pC->isTable ); + +#ifdef SQLITE_32BIT_ROWID +# define MAX_ROWID 0x7fffffff +#else + /* Some compilers complain about constants of the form 0x7fffffffffffffff. + ** Others complain about 0x7ffffffffffffffffLL. The following macro seems + ** to provide the constant while making all compilers happy. + */ +# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) +#endif + + if( !pC->useRandomRowid ){ + rc = sqlite3BtreeLast(pC->uc.pCursor, &res); + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + if( res ){ + v = 1; /* IMP: R-61914-48074 */ + }else{ + assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); + rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); + assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ + if( v>=MAX_ROWID ){ + pC->useRandomRowid = 1; + }else{ + v++; /* IMP: R-29538-34987 */ + } + } + } + +#ifndef SQLITE_OMIT_AUTOINCREMENT + if( pOp->p3 ){ + /* Assert that P3 is a valid memory cell. */ + assert( pOp->p3>0 ); + if( p->pFrame ){ + for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); + /* Assert that P3 is a valid memory cell. */ + assert( pOp->p3<=pFrame->nMem ); + pMem = &pFrame->aMem[pOp->p3]; + }else{ + /* Assert that P3 is a valid memory cell. */ + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + pMem = &aMem[pOp->p3]; + memAboutToChange(p, pMem); + } + assert( memIsValid(pMem) ); + + REGISTER_TRACE(pOp->p3, pMem); + sqlite3VdbeMemIntegerify(pMem); + assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ + if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ + rc = SQLITE_FULL; /* IMP: R-12275-61338 */ + goto abort_due_to_error; + } + if( vu.i+1 ){ + v = pMem->u.i + 1; + } + pMem->u.i = v; + } +#endif + if( pC->useRandomRowid ){ + /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the + ** largest possible integer (9223372036854775807) then the database + ** engine starts picking positive candidate ROWIDs at random until + ** it finds one that is not previously used. */ + assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is + ** an AUTOINCREMENT table. */ + cnt = 0; + do{ + sqlite3_randomness(sizeof(v), &v); + v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ + }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, + 0, &res))==SQLITE_OK) + && (res==0) + && (++cnt<100)); + if( rc ) goto abort_due_to_error; + if( res==0 ){ + rc = SQLITE_FULL; /* IMP: R-38219-53002 */ + goto abort_due_to_error; + } + assert( v>0 ); /* EV: R-40812-03570 */ + } + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + } + pOut->u.i = v; + break; +} + +/* Opcode: Insert P1 P2 P3 P4 P5 +** Synopsis: intkey=r[P3] data=r[P2] +** +** Write an entry into the table of cursor P1. A new entry is +** created if it doesn't already exist or the data for an existing +** entry is overwritten. The data is the value MEM_Blob stored in register +** number P2. The key is stored in register P3. The key must +** be a MEM_Int. +** +** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is +** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, +** then rowid is stored for subsequent return by the +** sqlite3_last_insert_rowid() function (otherwise it is unmodified). +** +** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of +** the last seek operation (OP_NotExists) was a success, then this +** operation will not attempt to find the appropriate row before doing +** the insert but will instead overwrite the row that the cursor is +** currently pointing to. Presumably, the prior OP_NotExists opcode +** has already positioned the cursor correctly. This is an optimization +** that boosts performance by avoiding redundant seeks. +** +** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an +** UPDATE operation. Otherwise (if the flag is clear) then this opcode +** is part of an INSERT operation. The difference is only important to +** the update hook. +** +** Parameter P4 may point to a Table structure, or may be NULL. If it is +** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked +** following a successful insert. +** +** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically +** allocated, then ownership of P2 is transferred to the pseudo-cursor +** and register P2 becomes ephemeral. If the cursor is changed, the +** value of register P2 will then change. Make sure this does not +** cause any problems.) +** +** This instruction only works on tables. The equivalent instruction +** for indices is OP_IdxInsert. +*/ +/* Opcode: InsertInt P1 P2 P3 P4 P5 +** Synopsis: intkey=P3 data=r[P2] +** +** This works exactly like OP_Insert except that the key is the +** integer value P3, not the value of the integer stored in register P3. +*/ +case OP_Insert: +case OP_InsertInt: { + Mem *pData; /* MEM cell holding data for the record to be inserted */ + Mem *pKey; /* MEM cell holding key for the record */ + i64 iKey; /* The integer ROWID or key for the record to be inserted */ + VdbeCursor *pC; /* Cursor to table into which insert is written */ + int nZero; /* Number of zero-bytes to append */ + int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ + const char *zDb; /* database name - used by the update hook */ + Table *pTab; /* Table structure - used by update and pre-update hooks */ + int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ + + op = 0; + pData = &aMem[pOp->p2]; + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( memIsValid(pData) ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + assert( pC->isTable ); + assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); + REGISTER_TRACE(pOp->p2, pData); + + if( pOp->opcode==OP_Insert ){ + pKey = &aMem[pOp->p3]; + assert( pKey->flags & MEM_Int ); + assert( memIsValid(pKey) ); + REGISTER_TRACE(pOp->p3, pKey); + iKey = pKey->u.i; + }else{ + assert( pOp->opcode==OP_InsertInt ); + iKey = pOp->p3; + } + + if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ + assert( pC->isTable ); + assert( pC->iDb>=0 ); + zDb = db->aDb[pC->iDb].zName; + pTab = pOp->p4.pTab; + assert( HasRowid(pTab) ); + op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); + }else{ + pTab = 0; /* Not needed. Silence a comiler warning. */ + zDb = 0; /* Not needed. Silence a compiler warning. */ + } + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + /* Invoke the pre-update hook, if any */ + if( db->xPreUpdateCallback + && pOp->p4type==P4_TABLE + && !(pOp->p5 & OPFLAG_ISUPDATE) + ){ + sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, iKey, pOp->p2); + } +#endif + + if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; + if( pData->flags & MEM_Null ){ + pData->z = 0; + pData->n = 0; + }else{ + assert( pData->flags & (MEM_Blob|MEM_Str) ); + } + seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); + if( pData->flags & MEM_Zero ){ + nZero = pData->u.nZero; + }else{ + nZero = 0; + } + rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey, + pData->z, pData->n, nZero, + (pOp->p5 & OPFLAG_APPEND)!=0, seekResult + ); + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + + /* Invoke the update-hook if required. */ + if( rc ) goto abort_due_to_error; + if( db->xUpdateCallback && op ){ + db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, iKey); + } + break; +} + +/* Opcode: Delete P1 P2 P3 P4 P5 +** +** Delete the record at which the P1 cursor is currently pointing. +** +** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then +** the cursor will be left pointing at either the next or the previous +** record in the table. If it is left pointing at the next record, then +** the next Next instruction will be a no-op. As a result, in this case +** it is ok to delete a record from within a Next loop. If +** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be +** left in an undefined state. +** +** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this +** delete one of several associated with deleting a table row and all its +** associated index entries. Exactly one of those deletes is the "primary" +** delete. The others are all on OPFLAG_FORDELETE cursors or else are +** marked with the AUXDELETE flag. +** +** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row +** change count is incremented (otherwise not). +** +** P1 must not be pseudo-table. It has to be a real table with +** multiple rows. +** +** If P4 is not NULL then it points to a Table struture. In this case either +** the update or pre-update hook, or both, may be invoked. The P1 cursor must +** have been positioned using OP_NotFound prior to invoking this opcode in +** this case. Specifically, if one is configured, the pre-update hook is +** invoked if P4 is not NULL. The update-hook is invoked if one is configured, +** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. +** +** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address +** of the memory cell that contains the value that the rowid of the row will +** be set to by the update. +*/ +case OP_Delete: { + VdbeCursor *pC; + const char *zDb; + Table *pTab; + int opflags; + + opflags = pOp->p2; + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + assert( pC->deferredMoveto==0 ); + +#ifdef SQLITE_DEBUG + if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ + /* If p5 is zero, the seek operation that positioned the cursor prior to + ** OP_Delete will have also set the pC->movetoTarget field to the rowid of + ** the row that is being deleted */ + i64 iKey = 0; + sqlite3BtreeKeySize(pC->uc.pCursor, &iKey); + assert( pC->movetoTarget==iKey ); + } +#endif + + /* If the update-hook or pre-update-hook will be invoked, set zDb to + ** the name of the db to pass as to it. Also set local pTab to a copy + ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was + ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set + ** VdbeCursor.movetoTarget to the current rowid. */ + if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ + assert( pC->iDb>=0 ); + assert( pOp->p4.pTab!=0 ); + zDb = db->aDb[pC->iDb].zName; + pTab = pOp->p4.pTab; + if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ + sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget); + } + }else{ + zDb = 0; /* Not needed. Silence a compiler warning. */ + pTab = 0; /* Not needed. Silence a compiler warning. */ + } + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + /* Invoke the pre-update-hook if required. */ + if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){ + assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) ); + sqlite3VdbePreUpdateHook(p, pC, + (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, + zDb, pTab, pC->movetoTarget, + pOp->p3 + ); + } + if( opflags & OPFLAG_ISNOOP ) break; +#endif + + /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ + assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); + assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); + assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); + +#ifdef SQLITE_DEBUG + if( p->pFrame==0 ){ + if( pC->isEphemeral==0 + && (pOp->p5 & OPFLAG_AUXDELETE)==0 + && (pC->wrFlag & OPFLAG_FORDELETE)==0 + ){ + nExtraDelete++; + } + if( pOp->p2 & OPFLAG_NCHANGE ){ + nExtraDelete--; + } + } +#endif + + rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); + pC->cacheStatus = CACHE_STALE; + if( rc ) goto abort_due_to_error; + + /* Invoke the update-hook if required. */ + if( opflags & OPFLAG_NCHANGE ){ + p->nChange++; + if( db->xUpdateCallback && HasRowid(pTab) ){ + db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, + pC->movetoTarget); + assert( pC->iDb>=0 ); + } + } + + break; +} +/* Opcode: ResetCount * * * * * +** +** The value of the change counter is copied to the database handle +** change counter (returned by subsequent calls to sqlite3_changes()). +** Then the VMs internal change counter resets to 0. +** This is used by trigger programs. +*/ +case OP_ResetCount: { + sqlite3VdbeSetChanges(db, p->nChange); + p->nChange = 0; + break; +} + +/* Opcode: SorterCompare P1 P2 P3 P4 +** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 +** +** P1 is a sorter cursor. This instruction compares a prefix of the +** record blob in register P3 against a prefix of the entry that +** the sorter cursor currently points to. Only the first P4 fields +** of r[P3] and the sorter record are compared. +** +** If either P3 or the sorter contains a NULL in one of their significant +** fields (not counting the P4 fields at the end which are ignored) then +** the comparison is assumed to be equal. +** +** Fall through to next instruction if the two records compare equal to +** each other. Jump to P2 if they are different. +*/ +case OP_SorterCompare: { + VdbeCursor *pC; + int res; + int nKeyCol; + + pC = p->apCsr[pOp->p1]; + assert( isSorter(pC) ); + assert( pOp->p4type==P4_INT32 ); + pIn3 = &aMem[pOp->p3]; + nKeyCol = pOp->p4.i; + res = 0; + rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); + VdbeBranchTaken(res!=0,2); + if( rc ) goto abort_due_to_error; + if( res ) goto jump_to_p2; + break; +}; + +/* Opcode: SorterData P1 P2 P3 * * +** Synopsis: r[P2]=data +** +** Write into register P2 the current sorter data for sorter cursor P1. +** Then clear the column header cache on cursor P3. +** +** This opcode is normally use to move a record out of the sorter and into +** a register that is the source for a pseudo-table cursor created using +** OpenPseudo. That pseudo-table cursor is the one that is identified by +** parameter P3. Clearing the P3 column cache as part of this opcode saves +** us from having to issue a separate NullRow instruction to clear that cache. +*/ +case OP_SorterData: { + VdbeCursor *pC; + + pOut = &aMem[pOp->p2]; + pC = p->apCsr[pOp->p1]; + assert( isSorter(pC) ); + rc = sqlite3VdbeSorterRowkey(pC, pOut); + assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); + assert( pOp->p1>=0 && pOp->p1nCursor ); + if( rc ) goto abort_due_to_error; + p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; + break; +} + +/* Opcode: RowData P1 P2 * * * +** Synopsis: r[P2]=data +** +** Write into register P2 the complete row data for cursor P1. +** There is no interpretation of the data. +** It is just copied onto the P2 register exactly as +** it is found in the database file. +** +** If the P1 cursor must be pointing to a valid row (not a NULL row) +** of a real table, not a pseudo-table. +*/ +/* Opcode: RowKey P1 P2 * * * +** Synopsis: r[P2]=key +** +** Write into register P2 the complete row key for cursor P1. +** There is no interpretation of the data. +** The key is copied onto the P2 register exactly as +** it is found in the database file. +** +** If the P1 cursor must be pointing to a valid row (not a NULL row) +** of a real table, not a pseudo-table. +*/ +case OP_RowKey: +case OP_RowData: { + VdbeCursor *pC; + BtCursor *pCrsr; + u32 n; + i64 n64; + + pOut = &aMem[pOp->p2]; + memAboutToChange(p, pOut); + + /* Note that RowKey and RowData are really exactly the same instruction */ + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( isSorter(pC)==0 ); + assert( pC->isTable || pOp->opcode!=OP_RowData ); + assert( pC->isTable==0 || pOp->opcode==OP_RowData ); + assert( pC->nullRow==0 ); + assert( pC->uc.pCursor!=0 ); + pCrsr = pC->uc.pCursor; + + /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or + ** OP_Rewind/Op_Next with no intervening instructions that might invalidate + ** the cursor. If this where not the case, on of the following assert()s + ** would fail. Should this ever change (because of changes in the code + ** generator) then the fix would be to insert a call to + ** sqlite3VdbeCursorMoveto(). + */ + assert( pC->deferredMoveto==0 ); + assert( sqlite3BtreeCursorIsValid(pCrsr) ); +#if 0 /* Not required due to the previous to assert() statements */ + rc = sqlite3VdbeCursorMoveto(pC); + if( rc!=SQLITE_OK ) goto abort_due_to_error; +#endif + + if( pC->isTable==0 ){ + assert( !pC->isTable ); + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); + assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ + if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + n = (u32)n64; + }else{ + VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n); + assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ + if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + } + testcase( n==0 ); + if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ + goto no_mem; + } + pOut->n = n; + MemSetTypeFlag(pOut, MEM_Blob); + if( pC->isTable==0 ){ + rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); + }else{ + rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); + } + if( rc ) goto abort_due_to_error; + pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ + UPDATE_MAX_BLOBSIZE(pOut); + REGISTER_TRACE(pOp->p2, pOut); + break; +} + +/* Opcode: Rowid P1 P2 * * * +** Synopsis: r[P2]=rowid +** +** Store in register P2 an integer which is the key of the table entry that +** P1 is currently point to. +** +** P1 can be either an ordinary table or a virtual table. There used to +** be a separate OP_VRowid opcode for use with virtual tables, but this +** one opcode now works for both table types. +*/ +case OP_Rowid: { /* out2 */ + VdbeCursor *pC; + i64 v; + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + + pOut = out2Prerelease(p, pOp); + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); + if( pC->nullRow ){ + pOut->flags = MEM_Null; + break; + }else if( pC->deferredMoveto ){ + v = pC->movetoTarget; +#ifndef SQLITE_OMIT_VIRTUALTABLE + }else if( pC->eCurType==CURTYPE_VTAB ){ + assert( pC->uc.pVCur!=0 ); + pVtab = pC->uc.pVCur->pVtab; + pModule = pVtab->pModule; + assert( pModule->xRowid ); + rc = pModule->xRowid(pC->uc.pVCur, &v); + sqlite3VtabImportErrmsg(p, pVtab); + if( rc ) goto abort_due_to_error; +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + }else{ + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + rc = sqlite3VdbeCursorRestore(pC); + if( rc ) goto abort_due_to_error; + if( pC->nullRow ){ + pOut->flags = MEM_Null; + break; + } + rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); + assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ + } + pOut->u.i = v; + break; +} + +/* Opcode: NullRow P1 * * * * +** +** Move the cursor P1 to a null row. Any OP_Column operations +** that occur while the cursor is on the null row will always +** write a NULL. +*/ +case OP_NullRow: { + VdbeCursor *pC; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + pC->nullRow = 1; + pC->cacheStatus = CACHE_STALE; + if( pC->eCurType==CURTYPE_BTREE ){ + assert( pC->uc.pCursor!=0 ); + sqlite3BtreeClearCursor(pC->uc.pCursor); + } + break; +} + +/* Opcode: Last P1 P2 P3 * * +** +** The next use of the Rowid or Column or Prev instruction for P1 +** will refer to the last entry in the database table or index. +** If the table or index is empty and P2>0, then jump immediately to P2. +** If P2 is 0 or if the table or index is not empty, fall through +** to the following instruction. +** +** This opcode leaves the cursor configured to move in reverse order, +** from the end toward the beginning. In other words, the cursor is +** configured to use Prev, not Next. +*/ +case OP_Last: { /* jump */ + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + pCrsr = pC->uc.pCursor; + res = 0; + assert( pCrsr!=0 ); + rc = sqlite3BtreeLast(pCrsr, &res); + pC->nullRow = (u8)res; + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + pC->seekResult = pOp->p3; +#ifdef SQLITE_DEBUG + pC->seekOp = OP_Last; +#endif + if( rc ) goto abort_due_to_error; + if( pOp->p2>0 ){ + VdbeBranchTaken(res!=0,2); + if( res ) goto jump_to_p2; + } + break; +} + + +/* Opcode: Sort P1 P2 * * * +** +** This opcode does exactly the same thing as OP_Rewind except that +** it increments an undocumented global variable used for testing. +** +** Sorting is accomplished by writing records into a sorting index, +** then rewinding that index and playing it back from beginning to +** end. We use the OP_Sort opcode instead of OP_Rewind to do the +** rewinding so that the global variable will be incremented and +** regression tests can determine whether or not the optimizer is +** correctly optimizing out sorts. +*/ +case OP_SorterSort: /* jump */ +case OP_Sort: { /* jump */ +#ifdef SQLITE_TEST + sqlite3_sort_count++; + sqlite3_search_count--; +#endif + p->aCounter[SQLITE_STMTSTATUS_SORT]++; + /* Fall through into OP_Rewind */ +} +/* Opcode: Rewind P1 P2 * * * +** +** The next use of the Rowid or Column or Next instruction for P1 +** will refer to the first entry in the database table or index. +** If the table or index is empty, jump immediately to P2. +** If the table or index is not empty, fall through to the following +** instruction. +** +** This opcode leaves the cursor configured to move in forward order, +** from the beginning toward the end. In other words, the cursor is +** configured to use Next, not Prev. +*/ +case OP_Rewind: { /* jump */ + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); + res = 1; +#ifdef SQLITE_DEBUG + pC->seekOp = OP_Rewind; +#endif + if( isSorter(pC) ){ + rc = sqlite3VdbeSorterRewind(pC, &res); + }else{ + assert( pC->eCurType==CURTYPE_BTREE ); + pCrsr = pC->uc.pCursor; + assert( pCrsr ); + rc = sqlite3BtreeFirst(pCrsr, &res); + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + } + if( rc ) goto abort_due_to_error; + pC->nullRow = (u8)res; + assert( pOp->p2>0 && pOp->p2nOp ); + VdbeBranchTaken(res!=0,2); + if( res ) goto jump_to_p2; + break; +} + +/* Opcode: Next P1 P2 P3 P4 P5 +** +** Advance cursor P1 so that it points to the next key/data pair in its +** table or index. If there are no more key/value pairs then fall through +** to the following instruction. But if the cursor advance was successful, +** jump immediately to P2. +** +** The Next opcode is only valid following an SeekGT, SeekGE, or +** OP_Rewind opcode used to position the cursor. Next is not allowed +** to follow SeekLT, SeekLE, or OP_Last. +** +** The P1 cursor must be for a real table, not a pseudo-table. P1 must have +** been opened prior to this opcode or the program will segfault. +** +** The P3 value is a hint to the btree implementation. If P3==1, that +** means P1 is an SQL index and that this instruction could have been +** omitted if that index had been unique. P3 is usually 0. P3 is +** always either 0 or 1. +** +** P4 is always of type P4_ADVANCE. The function pointer points to +** sqlite3BtreeNext(). +** +** If P5 is positive and the jump is taken, then event counter +** number P5-1 in the prepared statement is incremented. +** +** See also: Prev, NextIfOpen +*/ +/* Opcode: NextIfOpen P1 P2 P3 P4 P5 +** +** This opcode works just like Next except that if cursor P1 is not +** open it behaves a no-op. +*/ +/* Opcode: Prev P1 P2 P3 P4 P5 +** +** Back up cursor P1 so that it points to the previous key/data pair in its +** table or index. If there is no previous key/value pairs then fall through +** to the following instruction. But if the cursor backup was successful, +** jump immediately to P2. +** +** +** The Prev opcode is only valid following an SeekLT, SeekLE, or +** OP_Last opcode used to position the cursor. Prev is not allowed +** to follow SeekGT, SeekGE, or OP_Rewind. +** +** The P1 cursor must be for a real table, not a pseudo-table. If P1 is +** not open then the behavior is undefined. +** +** The P3 value is a hint to the btree implementation. If P3==1, that +** means P1 is an SQL index and that this instruction could have been +** omitted if that index had been unique. P3 is usually 0. P3 is +** always either 0 or 1. +** +** P4 is always of type P4_ADVANCE. The function pointer points to +** sqlite3BtreePrevious(). +** +** If P5 is positive and the jump is taken, then event counter +** number P5-1 in the prepared statement is incremented. +*/ +/* Opcode: PrevIfOpen P1 P2 P3 P4 P5 +** +** This opcode works just like Prev except that if cursor P1 is not +** open it behaves a no-op. +*/ +case OP_SorterNext: { /* jump */ + VdbeCursor *pC; + int res; + + pC = p->apCsr[pOp->p1]; + assert( isSorter(pC) ); + res = 0; + rc = sqlite3VdbeSorterNext(db, pC, &res); + goto next_tail; +case OP_PrevIfOpen: /* jump */ +case OP_NextIfOpen: /* jump */ + if( p->apCsr[pOp->p1]==0 ) break; + /* Fall through */ +case OP_Prev: /* jump */ +case OP_Next: /* jump */ + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pOp->p5aCounter) ); + pC = p->apCsr[pOp->p1]; + res = pOp->p3; + assert( pC!=0 ); + assert( pC->deferredMoveto==0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( res==0 || (res==1 && pC->isTable==0) ); + testcase( res==1 ); + assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); + assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); + assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); + assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); + + /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. + ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ + assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen + || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE + || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); + assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen + || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE + || pC->seekOp==OP_Last ); + + rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); +next_tail: + pC->cacheStatus = CACHE_STALE; + VdbeBranchTaken(res==0,2); + if( rc ) goto abort_due_to_error; + if( res==0 ){ + pC->nullRow = 0; + p->aCounter[pOp->p5]++; +#ifdef SQLITE_TEST + sqlite3_search_count++; +#endif + goto jump_to_p2_and_check_for_interrupt; + }else{ + pC->nullRow = 1; + } + goto check_for_interrupt; +} + +/* Opcode: IdxInsert P1 P2 P3 * P5 +** Synopsis: key=r[P2] +** +** Register P2 holds an SQL index key made using the +** MakeRecord instructions. This opcode writes that key +** into the index P1. Data for the entry is nil. +** +** P3 is a flag that provides a hint to the b-tree layer that this +** insert is likely to be an append. +** +** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is +** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, +** then the change counter is unchanged. +** +** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have +** just done a seek to the spot where the new entry is to be inserted. +** This flag avoids doing an extra seek. +** +** This instruction only works for indices. The equivalent instruction +** for tables is OP_Insert. +*/ +case OP_SorterInsert: /* in2 */ +case OP_IdxInsert: { /* in2 */ + VdbeCursor *pC; + int nKey; + const char *zKey; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); + pIn2 = &aMem[pOp->p2]; + assert( pIn2->flags & MEM_Blob ); + if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; + assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); + assert( pC->isTable==0 ); + rc = ExpandBlob(pIn2); + if( rc ) goto abort_due_to_error; + if( pOp->opcode==OP_SorterInsert ){ + rc = sqlite3VdbeSorterWrite(pC, pIn2); + }else{ + nKey = pIn2->n; + zKey = pIn2->z; + rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, + ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) + ); + assert( pC->deferredMoveto==0 ); + pC->cacheStatus = CACHE_STALE; + } + if( rc) goto abort_due_to_error; + break; +} + +/* Opcode: IdxDelete P1 P2 P3 * * +** Synopsis: key=r[P2@P3] +** +** The content of P3 registers starting at register P2 form +** an unpacked index key. This opcode removes that entry from the +** index opened by cursor P1. +*/ +case OP_IdxDelete: { + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + UnpackedRecord r; + + assert( pOp->p3>0 ); + assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + pCrsr = pC->uc.pCursor; + assert( pCrsr!=0 ); + assert( pOp->p5==0 ); + r.pKeyInfo = pC->pKeyInfo; + r.nField = (u16)pOp->p3; + r.default_rc = 0; + r.aMem = &aMem[pOp->p2]; + rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); + if( rc ) goto abort_due_to_error; + if( res==0 ){ + rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); + if( rc ) goto abort_due_to_error; + } + assert( pC->deferredMoveto==0 ); + pC->cacheStatus = CACHE_STALE; + break; +} + +/* Opcode: Seek P1 * P3 P4 * +** Synopsis: Move P3 to P1.rowid +** +** P1 is an open index cursor and P3 is a cursor on the corresponding +** table. This opcode does a deferred seek of the P3 table cursor +** to the row that corresponds to the current row of P1. +** +** This is a deferred seek. Nothing actually happens until +** the cursor is used to read a record. That way, if no reads +** occur, no unnecessary I/O happens. +** +** P4 may be an array of integers (type P4_INTARRAY) containing +** one entry for each column in the P3 table. If array entry a(i) +** is non-zero, then reading column a(i)-1 from cursor P3 is +** equivalent to performing the deferred seek and then reading column i +** from P1. This information is stored in P3 and used to redirect +** reads against P3 over to P1, thus possibly avoiding the need to +** seek and read cursor P3. +*/ +/* Opcode: IdxRowid P1 P2 * * * +** Synopsis: r[P2]=rowid +** +** Write into register P2 an integer which is the last entry in the record at +** the end of the index key pointed to by cursor P1. This integer should be +** the rowid of the table entry to which this index entry points. +** +** See also: Rowid, MakeRecord. +*/ +case OP_Seek: +case OP_IdxRowid: { /* out2 */ + VdbeCursor *pC; /* The P1 index cursor */ + VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ + i64 rowid; /* Rowid that P1 current points to */ + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0 ); + assert( pC->isTable==0 ); + assert( pC->deferredMoveto==0 ); + assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); + + /* The IdxRowid and Seek opcodes are combined because of the commonality + ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ + rc = sqlite3VdbeCursorRestore(pC); + + /* sqlite3VbeCursorRestore() can only fail if the record has been deleted + ** out from under the cursor. That will never happens for an IdxRowid + ** or Seek opcode */ + if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; + + if( !pC->nullRow ){ + rowid = 0; /* Not needed. Only used to silence a warning. */ + rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); + if( rc!=SQLITE_OK ){ + goto abort_due_to_error; + } + if( pOp->opcode==OP_Seek ){ + assert( pOp->p3>=0 && pOp->p3nCursor ); + pTabCur = p->apCsr[pOp->p3]; + assert( pTabCur!=0 ); + assert( pTabCur->eCurType==CURTYPE_BTREE ); + assert( pTabCur->uc.pCursor!=0 ); + assert( pTabCur->isTable ); + pTabCur->nullRow = 0; + pTabCur->movetoTarget = rowid; + pTabCur->deferredMoveto = 1; + assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); + pTabCur->aAltMap = pOp->p4.ai; + pTabCur->pAltCursor = pC; + }else{ + pOut = out2Prerelease(p, pOp); + pOut->u.i = rowid; + pOut->flags = MEM_Int; + } + }else{ + assert( pOp->opcode==OP_IdxRowid ); + sqlite3VdbeMemSetNull(&aMem[pOp->p2]); + } + break; +} + +/* Opcode: IdxGE P1 P2 P3 P4 P5 +** Synopsis: key=r[P3@P4] +** +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY. Compare this key value against the index +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID +** fields at the end. +** +** If the P1 index entry is greater than or equal to the key value +** then jump to P2. Otherwise fall through to the next instruction. +*/ +/* Opcode: IdxGT P1 P2 P3 P4 P5 +** Synopsis: key=r[P3@P4] +** +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY. Compare this key value against the index +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID +** fields at the end. +** +** If the P1 index entry is greater than the key value +** then jump to P2. Otherwise fall through to the next instruction. +*/ +/* Opcode: IdxLT P1 P2 P3 P4 P5 +** Synopsis: key=r[P3@P4] +** +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY or ROWID. Compare this key value against +** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or +** ROWID on the P1 index. +** +** If the P1 index entry is less than the key value then jump to P2. +** Otherwise fall through to the next instruction. +*/ +/* Opcode: IdxLE P1 P2 P3 P4 P5 +** Synopsis: key=r[P3@P4] +** +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY or ROWID. Compare this key value against +** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or +** ROWID on the P1 index. +** +** If the P1 index entry is less than or equal to the key value then jump +** to P2. Otherwise fall through to the next instruction. +*/ +case OP_IdxLE: /* jump */ +case OP_IdxGT: /* jump */ +case OP_IdxLT: /* jump */ +case OP_IdxGE: { /* jump */ + VdbeCursor *pC; + int res; + UnpackedRecord r; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->isOrdered ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->uc.pCursor!=0); + assert( pC->deferredMoveto==0 ); + assert( pOp->p5==0 || pOp->p5==1 ); + assert( pOp->p4type==P4_INT32 ); + r.pKeyInfo = pC->pKeyInfo; + r.nField = (u16)pOp->p4.i; + if( pOp->opcodeopcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); + r.default_rc = -1; + }else{ + assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); + r.default_rc = 0; + } + r.aMem = &aMem[pOp->p3]; +#ifdef SQLITE_DEBUG + { int i; for(i=0; iopcode&1)==(OP_IdxLT&1) ){ + assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); + res = -res; + }else{ + assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); + res++; + } + VdbeBranchTaken(res>0,2); + if( rc ) goto abort_due_to_error; + if( res>0 ) goto jump_to_p2; + break; +} + +/* Opcode: Destroy P1 P2 P3 * * +** +** Delete an entire database table or index whose root page in the database +** file is given by P1. +** +** The table being destroyed is in the main database file if P3==0. If +** P3==1 then the table to be clear is in the auxiliary database file +** that is used to store tables create using CREATE TEMPORARY TABLE. +** +** If AUTOVACUUM is enabled then it is possible that another root page +** might be moved into the newly deleted root page in order to keep all +** root pages contiguous at the beginning of the database. The former +** value of the root page that moved - its value before the move occurred - +** is stored in register P2. If no page +** movement was required (because the table being dropped was already +** the last one in the database) then a zero is stored in register P2. +** If AUTOVACUUM is disabled then a zero is stored in register P2. +** +** See also: Clear +*/ +case OP_Destroy: { /* out2 */ + int iMoved; + int iDb; + + assert( p->readOnly==0 ); + assert( pOp->p1>1 ); + pOut = out2Prerelease(p, pOp); + pOut->flags = MEM_Null; + if( db->nVdbeRead > db->nVDestroy+1 ){ + rc = SQLITE_LOCKED; + p->errorAction = OE_Abort; + goto abort_due_to_error; + }else{ + iDb = pOp->p3; + assert( DbMaskTest(p->btreeMask, iDb) ); + iMoved = 0; /* Not needed. Only to silence a warning. */ + rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); + pOut->flags = MEM_Int; + pOut->u.i = iMoved; + if( rc ) goto abort_due_to_error; +#ifndef SQLITE_OMIT_AUTOVACUUM + if( iMoved!=0 ){ + sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); + /* All OP_Destroy operations occur on the same btree */ + assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); + resetSchemaOnFault = iDb+1; + } +#endif + } + break; +} + +/* Opcode: Clear P1 P2 P3 +** +** Delete all contents of the database table or index whose root page +** in the database file is given by P1. But, unlike Destroy, do not +** remove the table or index from the database file. +** +** The table being clear is in the main database file if P2==0. If +** P2==1 then the table to be clear is in the auxiliary database file +** that is used to store tables create using CREATE TEMPORARY TABLE. +** +** If the P3 value is non-zero, then the table referred to must be an +** intkey table (an SQL table, not an index). In this case the row change +** count is incremented by the number of rows in the table being cleared. +** If P3 is greater than zero, then the value stored in register P3 is +** also incremented by the number of rows in the table being cleared. +** +** See also: Destroy +*/ +case OP_Clear: { + int nChange; + + nChange = 0; + assert( p->readOnly==0 ); + assert( DbMaskTest(p->btreeMask, pOp->p2) ); + rc = sqlite3BtreeClearTable( + db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) + ); + if( pOp->p3 ){ + p->nChange += nChange; + if( pOp->p3>0 ){ + assert( memIsValid(&aMem[pOp->p3]) ); + memAboutToChange(p, &aMem[pOp->p3]); + aMem[pOp->p3].u.i += nChange; + } + } + if( rc ) goto abort_due_to_error; + break; +} + +/* Opcode: ResetSorter P1 * * * * +** +** Delete all contents from the ephemeral table or sorter +** that is open on cursor P1. +** +** This opcode only works for cursors used for sorting and +** opened with OP_OpenEphemeral or OP_SorterOpen. +*/ +case OP_ResetSorter: { + VdbeCursor *pC; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + if( isSorter(pC) ){ + sqlite3VdbeSorterReset(db, pC->uc.pSorter); + }else{ + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->isEphemeral ); + rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); + if( rc ) goto abort_due_to_error; + } + break; +} + +/* Opcode: CreateTable P1 P2 * * * +** Synopsis: r[P2]=root iDb=P1 +** +** Allocate a new table in the main database file if P1==0 or in the +** auxiliary database file if P1==1 or in an attached database if +** P1>1. Write the root page number of the new table into +** register P2 +** +** The difference between a table and an index is this: A table must +** have a 4-byte integer key and can have arbitrary data. An index +** has an arbitrary key but no data. +** +** See also: CreateIndex +*/ +/* Opcode: CreateIndex P1 P2 * * * +** Synopsis: r[P2]=root iDb=P1 +** +** Allocate a new index in the main database file if P1==0 or in the +** auxiliary database file if P1==1 or in an attached database if +** P1>1. Write the root page number of the new table into +** register P2. +** +** See documentation on OP_CreateTable for additional information. +*/ +case OP_CreateIndex: /* out2 */ +case OP_CreateTable: { /* out2 */ + int pgno; + int flags; + Db *pDb; + + pOut = out2Prerelease(p, pOp); + pgno = 0; + assert( pOp->p1>=0 && pOp->p1nDb ); + assert( DbMaskTest(p->btreeMask, pOp->p1) ); + assert( p->readOnly==0 ); + pDb = &db->aDb[pOp->p1]; + assert( pDb->pBt!=0 ); + if( pOp->opcode==OP_CreateTable ){ + /* flags = BTREE_INTKEY; */ + flags = BTREE_INTKEY; + }else{ + flags = BTREE_BLOBKEY; + } + rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); + if( rc ) goto abort_due_to_error; + pOut->u.i = pgno; + break; +} + +/* Opcode: ParseSchema P1 * * P4 * +** +** Read and parse all entries from the SQLITE_MASTER table of database P1 +** that match the WHERE clause P4. +** +** This opcode invokes the parser to create a new virtual machine, +** then runs the new virtual machine. It is thus a re-entrant opcode. +*/ +case OP_ParseSchema: { + int iDb; + const char *zMaster; + char *zSql; + InitData initData; + + /* Any prepared statement that invokes this opcode will hold mutexes + ** on every btree. This is a prerequisite for invoking + ** sqlite3InitCallback(). + */ +#ifdef SQLITE_DEBUG + for(iDb=0; iDbnDb; iDb++){ + assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); + } +#endif + + iDb = pOp->p1; + assert( iDb>=0 && iDbnDb ); + assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); + /* Used to be a conditional */ { + zMaster = SCHEMA_TABLE(iDb); + initData.db = db; + initData.iDb = pOp->p1; + initData.pzErrMsg = &p->zErrMsg; + zSql = sqlite3MPrintf(db, + "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", + db->aDb[iDb].zName, zMaster, pOp->p4.z); + if( zSql==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + assert( db->init.busy==0 ); + db->init.busy = 1; + initData.rc = SQLITE_OK; + assert( !db->mallocFailed ); + rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); + if( rc==SQLITE_OK ) rc = initData.rc; + sqlite3DbFree(db, zSql); + db->init.busy = 0; + } + } + if( rc ){ + sqlite3ResetAllSchemasOfConnection(db); + if( rc==SQLITE_NOMEM ){ + goto no_mem; + } + goto abort_due_to_error; + } + break; +} + +#if !defined(SQLITE_OMIT_ANALYZE) +/* Opcode: LoadAnalysis P1 * * * * +** +** Read the sqlite_stat1 table for database P1 and load the content +** of that table into the internal index hash table. This will cause +** the analysis to be used when preparing all subsequent queries. +*/ +case OP_LoadAnalysis: { + assert( pOp->p1>=0 && pOp->p1nDb ); + rc = sqlite3AnalysisLoad(db, pOp->p1); + if( rc ) goto abort_due_to_error; + break; +} +#endif /* !defined(SQLITE_OMIT_ANALYZE) */ + +/* Opcode: DropTable P1 * * P4 * +** +** Remove the internal (in-memory) data structures that describe +** the table named P4 in database P1. This is called after a table +** is dropped from disk (using the Destroy opcode) in order to keep +** the internal representation of the +** schema consistent with what is on disk. +*/ +case OP_DropTable: { + sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); + break; +} + +/* Opcode: DropIndex P1 * * P4 * +** +** Remove the internal (in-memory) data structures that describe +** the index named P4 in database P1. This is called after an index +** is dropped from disk (using the Destroy opcode) +** in order to keep the internal representation of the +** schema consistent with what is on disk. +*/ +case OP_DropIndex: { + sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); + break; +} + +/* Opcode: DropTrigger P1 * * P4 * +** +** Remove the internal (in-memory) data structures that describe +** the trigger named P4 in database P1. This is called after a trigger +** is dropped from disk (using the Destroy opcode) in order to keep +** the internal representation of the +** schema consistent with what is on disk. +*/ +case OP_DropTrigger: { + sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); + break; +} + + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* Opcode: IntegrityCk P1 P2 P3 P4 P5 +** +** Do an analysis of the currently open database. Store in +** register P1 the text of an error message describing any problems. +** If no problems are found, store a NULL in register P1. +** +** The register P3 contains the maximum number of allowed errors. +** At most reg(P3) errors will be reported. +** In other words, the analysis stops as soon as reg(P1) errors are +** seen. Reg(P1) is updated with the number of errors remaining. +** +** The root page numbers of all tables in the database are integers +** stored in P4_INTARRAY argument. +** +** If P5 is not zero, the check is done on the auxiliary database +** file, not the main database file. +** +** This opcode is used to implement the integrity_check pragma. +*/ +case OP_IntegrityCk: { + int nRoot; /* Number of tables to check. (Number of root pages.) */ + int *aRoot; /* Array of rootpage numbers for tables to be checked */ + int nErr; /* Number of errors reported */ + char *z; /* Text of the error report */ + Mem *pnErr; /* Register keeping track of errors remaining */ + + assert( p->bIsReader ); + nRoot = pOp->p2; + aRoot = pOp->p4.ai; + assert( nRoot>0 ); + assert( aRoot[nRoot]==0 ); + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + pnErr = &aMem[pOp->p3]; + assert( (pnErr->flags & MEM_Int)!=0 ); + assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); + pIn1 = &aMem[pOp->p1]; + assert( pOp->p5nDb ); + assert( DbMaskTest(p->btreeMask, pOp->p5) ); + z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, + (int)pnErr->u.i, &nErr); + pnErr->u.i -= nErr; + sqlite3VdbeMemSetNull(pIn1); + if( nErr==0 ){ + assert( z==0 ); + }else if( z==0 ){ + goto no_mem; + }else{ + sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); + } + UPDATE_MAX_BLOBSIZE(pIn1); + sqlite3VdbeChangeEncoding(pIn1, encoding); + break; +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +/* Opcode: RowSetAdd P1 P2 * * * +** Synopsis: rowset(P1)=r[P2] +** +** Insert the integer value held by register P2 into a boolean index +** held in register P1. +** +** An assertion fails if P2 is not an integer. +*/ +case OP_RowSetAdd: { /* in1, in2 */ + pIn1 = &aMem[pOp->p1]; + pIn2 = &aMem[pOp->p2]; + assert( (pIn2->flags & MEM_Int)!=0 ); + if( (pIn1->flags & MEM_RowSet)==0 ){ + sqlite3VdbeMemSetRowSet(pIn1); + if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; + } + sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); + break; +} + +/* Opcode: RowSetRead P1 P2 P3 * * +** Synopsis: r[P3]=rowset(P1) +** +** Extract the smallest value from boolean index P1 and put that value into +** register P3. Or, if boolean index P1 is initially empty, leave P3 +** unchanged and jump to instruction P2. +*/ +case OP_RowSetRead: { /* jump, in1, out3 */ + i64 val; + + pIn1 = &aMem[pOp->p1]; + if( (pIn1->flags & MEM_RowSet)==0 + || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 + ){ + /* The boolean index is empty */ + sqlite3VdbeMemSetNull(pIn1); + VdbeBranchTaken(1,2); + goto jump_to_p2_and_check_for_interrupt; + }else{ + /* A value was pulled from the index */ + VdbeBranchTaken(0,2); + sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); + } + goto check_for_interrupt; +} + +/* Opcode: RowSetTest P1 P2 P3 P4 +** Synopsis: if r[P3] in rowset(P1) goto P2 +** +** Register P3 is assumed to hold a 64-bit integer value. If register P1 +** contains a RowSet object and that RowSet object contains +** the value held in P3, jump to register P2. Otherwise, insert the +** integer in P3 into the RowSet and continue on to the +** next opcode. +** +** The RowSet object is optimized for the case where successive sets +** of integers, where each set contains no duplicates. Each set +** of values is identified by a unique P4 value. The first set +** must have P4==0, the final set P4=-1. P4 must be either -1 or +** non-negative. For non-negative values of P4 only the lower 4 +** bits are significant. +** +** This allows optimizations: (a) when P4==0 there is no need to test +** the rowset object for P3, as it is guaranteed not to contain it, +** (b) when P4==-1 there is no need to insert the value, as it will +** never be tested for, and (c) when a value that is part of set X is +** inserted, there is no need to search to see if the same value was +** previously inserted as part of set X (only if it was previously +** inserted as part of some other set). +*/ +case OP_RowSetTest: { /* jump, in1, in3 */ + int iSet; + int exists; + + pIn1 = &aMem[pOp->p1]; + pIn3 = &aMem[pOp->p3]; + iSet = pOp->p4.i; + assert( pIn3->flags&MEM_Int ); + + /* If there is anything other than a rowset object in memory cell P1, + ** delete it now and initialize P1 with an empty rowset + */ + if( (pIn1->flags & MEM_RowSet)==0 ){ + sqlite3VdbeMemSetRowSet(pIn1); + if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; + } + + assert( pOp->p4type==P4_INT32 ); + assert( iSet==-1 || iSet>=0 ); + if( iSet ){ + exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); + VdbeBranchTaken(exists!=0,2); + if( exists ) goto jump_to_p2; + } + if( iSet>=0 ){ + sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); + } + break; +} + + +#ifndef SQLITE_OMIT_TRIGGER + +/* Opcode: Program P1 P2 P3 P4 P5 +** +** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). +** +** P1 contains the address of the memory cell that contains the first memory +** cell in an array of values used as arguments to the sub-program. P2 +** contains the address to jump to if the sub-program throws an IGNORE +** exception using the RAISE() function. Register P3 contains the address +** of a memory cell in this (the parent) VM that is used to allocate the +** memory required by the sub-vdbe at runtime. +** +** P4 is a pointer to the VM containing the trigger program. +** +** If P5 is non-zero, then recursive program invocation is enabled. +*/ +case OP_Program: { /* jump */ + int nMem; /* Number of memory registers for sub-program */ + int nByte; /* Bytes of runtime space required for sub-program */ + Mem *pRt; /* Register to allocate runtime space */ + Mem *pMem; /* Used to iterate through memory cells */ + Mem *pEnd; /* Last memory cell in new array */ + VdbeFrame *pFrame; /* New vdbe frame to execute in */ + SubProgram *pProgram; /* Sub-program to execute */ + void *t; /* Token identifying trigger */ + + pProgram = pOp->p4.pProgram; + pRt = &aMem[pOp->p3]; + assert( pProgram->nOp>0 ); + + /* If the p5 flag is clear, then recursive invocation of triggers is + ** disabled for backwards compatibility (p5 is set if this sub-program + ** is really a trigger, not a foreign key action, and the flag set + ** and cleared by the "PRAGMA recursive_triggers" command is clear). + ** + ** It is recursive invocation of triggers, at the SQL level, that is + ** disabled. In some cases a single trigger may generate more than one + ** SubProgram (if the trigger may be executed with more than one different + ** ON CONFLICT algorithm). SubProgram structures associated with a + ** single trigger all have the same value for the SubProgram.token + ** variable. */ + if( pOp->p5 ){ + t = pProgram->token; + for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); + if( pFrame ) break; + } + + if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ + rc = SQLITE_ERROR; + sqlite3VdbeError(p, "too many levels of trigger recursion"); + goto abort_due_to_error; + } + + /* Register pRt is used to store the memory required to save the state + ** of the current program, and the memory required at runtime to execute + ** the trigger program. If this trigger has been fired before, then pRt + ** is already allocated. Otherwise, it must be initialized. */ + if( (pRt->flags&MEM_Frame)==0 ){ + /* SubProgram.nMem is set to the number of memory cells used by the + ** program stored in SubProgram.aOp. As well as these, one memory + ** cell is required for each cursor used by the program. Set local + ** variable nMem (and later, VdbeFrame.nChildMem) to this value. + */ + nMem = pProgram->nMem + pProgram->nCsr; + assert( nMem>0 ); + if( pProgram->nCsr==0 ) nMem++; + nByte = ROUND8(sizeof(VdbeFrame)) + + nMem * sizeof(Mem) + + pProgram->nCsr * sizeof(VdbeCursor *) + + pProgram->nOnce * sizeof(u8); + pFrame = sqlite3DbMallocZero(db, nByte); + if( !pFrame ){ + goto no_mem; + } + sqlite3VdbeMemRelease(pRt); + pRt->flags = MEM_Frame; + pRt->u.pFrame = pFrame; + + pFrame->v = p; + pFrame->nChildMem = nMem; + pFrame->nChildCsr = pProgram->nCsr; + pFrame->pc = (int)(pOp - aOp); + pFrame->aMem = p->aMem; + pFrame->nMem = p->nMem; + pFrame->apCsr = p->apCsr; + pFrame->nCursor = p->nCursor; + pFrame->aOp = p->aOp; + pFrame->nOp = p->nOp; + pFrame->token = pProgram->token; + pFrame->aOnceFlag = p->aOnceFlag; + pFrame->nOnceFlag = p->nOnceFlag; +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + pFrame->anExec = p->anExec; +#endif + + pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; + for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ + pMem->flags = MEM_Undefined; + pMem->db = db; + } + }else{ + pFrame = pRt->u.pFrame; + assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem + || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); + assert( pProgram->nCsr==pFrame->nChildCsr ); + assert( (int)(pOp - aOp)==pFrame->pc ); + } + + p->nFrame++; + pFrame->pParent = p->pFrame; + pFrame->lastRowid = lastRowid; + pFrame->nChange = p->nChange; + pFrame->nDbChange = p->db->nChange; + assert( pFrame->pAuxData==0 ); + pFrame->pAuxData = p->pAuxData; + p->pAuxData = 0; + p->nChange = 0; + p->pFrame = pFrame; + p->aMem = aMem = VdbeFrameMem(pFrame); + p->nMem = pFrame->nChildMem; + p->nCursor = (u16)pFrame->nChildCsr; + p->apCsr = (VdbeCursor **)&aMem[p->nMem]; + p->aOp = aOp = pProgram->aOp; + p->nOp = pProgram->nOp; + p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; + p->nOnceFlag = pProgram->nOnce; +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + p->anExec = 0; +#endif + pOp = &aOp[-1]; + memset(p->aOnceFlag, 0, p->nOnceFlag); + + break; +} + +/* Opcode: Param P1 P2 * * * +** +** This opcode is only ever present in sub-programs called via the +** OP_Program instruction. Copy a value currently stored in a memory +** cell of the calling (parent) frame to cell P2 in the current frames +** address space. This is used by trigger programs to access the new.* +** and old.* values. +** +** The address of the cell in the parent frame is determined by adding +** the value of the P1 argument to the value of the P1 argument to the +** calling OP_Program instruction. +*/ +case OP_Param: { /* out2 */ + VdbeFrame *pFrame; + Mem *pIn; + pOut = out2Prerelease(p, pOp); + pFrame = p->pFrame; + pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; + sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); + break; +} + +#endif /* #ifndef SQLITE_OMIT_TRIGGER */ + +#ifndef SQLITE_OMIT_FOREIGN_KEY +/* Opcode: FkCounter P1 P2 * * * +** Synopsis: fkctr[P1]+=P2 +** +** Increment a "constraint counter" by P2 (P2 may be negative or positive). +** If P1 is non-zero, the database constraint counter is incremented +** (deferred foreign key constraints). Otherwise, if P1 is zero, the +** statement counter is incremented (immediate foreign key constraints). +*/ +case OP_FkCounter: { + if( db->flags & SQLITE_DeferFKs ){ + db->nDeferredImmCons += pOp->p2; + }else if( pOp->p1 ){ + db->nDeferredCons += pOp->p2; + }else{ + p->nFkConstraint += pOp->p2; + } + break; +} + +/* Opcode: FkIfZero P1 P2 * * * +** Synopsis: if fkctr[P1]==0 goto P2 +** +** This opcode tests if a foreign key constraint-counter is currently zero. +** If so, jump to instruction P2. Otherwise, fall through to the next +** instruction. +** +** If P1 is non-zero, then the jump is taken if the database constraint-counter +** is zero (the one that counts deferred constraint violations). If P1 is +** zero, the jump is taken if the statement constraint-counter is zero +** (immediate foreign key constraint violations). +*/ +case OP_FkIfZero: { /* jump */ + if( pOp->p1 ){ + VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); + if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; + }else{ + VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); + if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; + } + break; +} +#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ + +#ifndef SQLITE_OMIT_AUTOINCREMENT +/* Opcode: MemMax P1 P2 * * * +** Synopsis: r[P1]=max(r[P1],r[P2]) +** +** P1 is a register in the root frame of this VM (the root frame is +** different from the current frame if this instruction is being executed +** within a sub-program). Set the value of register P1 to the maximum of +** its current value and the value in register P2. +** +** This instruction throws an error if the memory cell is not initially +** an integer. +*/ +case OP_MemMax: { /* in2 */ + VdbeFrame *pFrame; + if( p->pFrame ){ + for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); + pIn1 = &pFrame->aMem[pOp->p1]; + }else{ + pIn1 = &aMem[pOp->p1]; + } + assert( memIsValid(pIn1) ); + sqlite3VdbeMemIntegerify(pIn1); + pIn2 = &aMem[pOp->p2]; + sqlite3VdbeMemIntegerify(pIn2); + if( pIn1->u.iu.i){ + pIn1->u.i = pIn2->u.i; + } + break; +} +#endif /* SQLITE_OMIT_AUTOINCREMENT */ + +/* Opcode: IfPos P1 P2 P3 * * +** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 +** +** Register P1 must contain an integer. +** If the value of register P1 is 1 or greater, subtract P3 from the +** value in P1 and jump to P2. +** +** If the initial value of register P1 is less than 1, then the +** value is unchanged and control passes through to the next instruction. +*/ +case OP_IfPos: { /* jump, in1 */ + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags&MEM_Int ); + VdbeBranchTaken( pIn1->u.i>0, 2); + if( pIn1->u.i>0 ){ + pIn1->u.i -= pOp->p3; + goto jump_to_p2; + } + break; +} + +/* Opcode: OffsetLimit P1 P2 P3 * * +** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) +** +** This opcode performs a commonly used computation associated with +** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] +** holds the offset counter. The opcode computes the combined value +** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] +** value computed is the total number of rows that will need to be +** visited in order to complete the query. +** +** If r[P3] is zero or negative, that means there is no OFFSET +** and r[P2] is set to be the value of the LIMIT, r[P1]. +** +** if r[P1] is zero or negative, that means there is no LIMIT +** and r[P2] is set to -1. +** +** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. +*/ +case OP_OffsetLimit: { /* in1, out2, in3 */ + pIn1 = &aMem[pOp->p1]; + pIn3 = &aMem[pOp->p3]; + pOut = out2Prerelease(p, pOp); + assert( pIn1->flags & MEM_Int ); + assert( pIn3->flags & MEM_Int ); + pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0); + break; +} + +/* Opcode: IfNotZero P1 P2 P3 * * +** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 +** +** Register P1 must contain an integer. If the content of register P1 is +** initially nonzero, then subtract P3 from the value in register P1 and +** jump to P2. If register P1 is initially zero, leave it unchanged +** and fall through. +*/ +case OP_IfNotZero: { /* jump, in1 */ + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags&MEM_Int ); + VdbeBranchTaken(pIn1->u.i<0, 2); + if( pIn1->u.i ){ + pIn1->u.i -= pOp->p3; + goto jump_to_p2; + } + break; +} + +/* Opcode: DecrJumpZero P1 P2 * * * +** Synopsis: if (--r[P1])==0 goto P2 +** +** Register P1 must hold an integer. Decrement the value in register P1 +** then jump to P2 if the new value is exactly zero. +*/ +case OP_DecrJumpZero: { /* jump, in1 */ + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags&MEM_Int ); + pIn1->u.i--; + VdbeBranchTaken(pIn1->u.i==0, 2); + if( pIn1->u.i==0 ) goto jump_to_p2; + break; +} + + +/* Opcode: AggStep0 * P2 P3 P4 P5 +** Synopsis: accum=r[P3] step(r[P2@P5]) +** +** Execute the step function for an aggregate. The +** function has P5 arguments. P4 is a pointer to the FuncDef +** structure that specifies the function. Register P3 is the +** accumulator. +** +** The P5 arguments are taken from register P2 and its +** successors. +*/ +/* Opcode: AggStep * P2 P3 P4 P5 +** Synopsis: accum=r[P3] step(r[P2@P5]) +** +** Execute the step function for an aggregate. The +** function has P5 arguments. P4 is a pointer to an sqlite3_context +** object that is used to run the function. Register P3 is +** as the accumulator. +** +** The P5 arguments are taken from register P2 and its +** successors. +** +** This opcode is initially coded as OP_AggStep0. On first evaluation, +** the FuncDef stored in P4 is converted into an sqlite3_context and +** the opcode is changed. In this way, the initialization of the +** sqlite3_context only happens once, instead of on each call to the +** step function. +*/ +case OP_AggStep0: { + int n; + sqlite3_context *pCtx; + + assert( pOp->p4type==P4_FUNCDEF ); + n = pOp->p5; + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); + assert( pOp->p3p2 || pOp->p3>=pOp->p2+n ); + pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); + if( pCtx==0 ) goto no_mem; + pCtx->pMem = 0; + pCtx->pFunc = pOp->p4.pFunc; + pCtx->iOp = (int)(pOp - aOp); + pCtx->pVdbe = p; + pCtx->argc = n; + pOp->p4type = P4_FUNCCTX; + pOp->p4.pCtx = pCtx; + pOp->opcode = OP_AggStep; + /* Fall through into OP_AggStep */ +} +case OP_AggStep: { + int i; + sqlite3_context *pCtx; + Mem *pMem; + Mem t; + + assert( pOp->p4type==P4_FUNCCTX ); + pCtx = pOp->p4.pCtx; + pMem = &aMem[pOp->p3]; + + /* If this function is inside of a trigger, the register array in aMem[] + ** might change from one evaluation to the next. The next block of code + ** checks to see if the register array has changed, and if so it + ** reinitializes the relavant parts of the sqlite3_context object */ + if( pCtx->pMem != pMem ){ + pCtx->pMem = pMem; + for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; + } + +#ifdef SQLITE_DEBUG + for(i=0; iargc; i++){ + assert( memIsValid(pCtx->argv[i]) ); + REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); + } +#endif + + pMem->n++; + sqlite3VdbeMemInit(&t, db, MEM_Null); + pCtx->pOut = &t; + pCtx->fErrorOrAux = 0; + pCtx->skipFlag = 0; + (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ + if( pCtx->fErrorOrAux ){ + if( pCtx->isError ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); + rc = pCtx->isError; + } + sqlite3VdbeMemRelease(&t); + if( rc ) goto abort_due_to_error; + }else{ + assert( t.flags==MEM_Null ); + } + if( pCtx->skipFlag ){ + assert( pOp[-1].opcode==OP_CollSeq ); + i = pOp[-1].p1; + if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); + } + break; +} + +/* Opcode: AggFinal P1 P2 * P4 * +** Synopsis: accum=r[P1] N=P2 +** +** Execute the finalizer function for an aggregate. P1 is +** the memory location that is the accumulator for the aggregate. +** +** P2 is the number of arguments that the step function takes and +** P4 is a pointer to the FuncDef for this function. The P2 +** argument is not used by this opcode. It is only there to disambiguate +** functions that can take varying numbers of arguments. The +** P4 argument is only needed for the degenerate case where +** the step function was not previously called. +*/ +case OP_AggFinal: { + Mem *pMem; + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + pMem = &aMem[pOp->p1]; + assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); + rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); + if( rc ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); + goto abort_due_to_error; + } + sqlite3VdbeChangeEncoding(pMem, encoding); + UPDATE_MAX_BLOBSIZE(pMem); + if( sqlite3VdbeMemTooBig(pMem) ){ + goto too_big; + } + break; +} + +#ifndef SQLITE_OMIT_WAL +/* Opcode: Checkpoint P1 P2 P3 * * +** +** Checkpoint database P1. This is a no-op if P1 is not currently in +** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, +** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns +** SQLITE_BUSY or not, respectively. Write the number of pages in the +** WAL after the checkpoint into mem[P3+1] and the number of pages +** in the WAL that have been checkpointed after the checkpoint +** completes into mem[P3+2]. However on an error, mem[P3+1] and +** mem[P3+2] are initialized to -1. +*/ +case OP_Checkpoint: { + int i; /* Loop counter */ + int aRes[3]; /* Results */ + Mem *pMem; /* Write results here */ + + assert( p->readOnly==0 ); + aRes[0] = 0; + aRes[1] = aRes[2] = -1; + assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE + || pOp->p2==SQLITE_CHECKPOINT_FULL + || pOp->p2==SQLITE_CHECKPOINT_RESTART + || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE + ); + rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); + if( rc ){ + if( rc!=SQLITE_BUSY ) goto abort_due_to_error; + rc = SQLITE_OK; + aRes[0] = 1; + } + for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ + sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); + } + break; +}; +#endif + +#ifndef SQLITE_OMIT_PRAGMA +/* Opcode: JournalMode P1 P2 P3 * * +** +** Change the journal mode of database P1 to P3. P3 must be one of the +** PAGER_JOURNALMODE_XXX values. If changing between the various rollback +** modes (delete, truncate, persist, off and memory), this is a simple +** operation. No IO is required. +** +** If changing into or out of WAL mode the procedure is more complicated. +** +** Write a string containing the final journal-mode to register P2. +*/ +case OP_JournalMode: { /* out2 */ + Btree *pBt; /* Btree to change journal mode of */ + Pager *pPager; /* Pager associated with pBt */ + int eNew; /* New journal mode */ + int eOld; /* The old journal mode */ +#ifndef SQLITE_OMIT_WAL + const char *zFilename; /* Name of database file for pPager */ +#endif + + pOut = out2Prerelease(p, pOp); + eNew = pOp->p3; + assert( eNew==PAGER_JOURNALMODE_DELETE + || eNew==PAGER_JOURNALMODE_TRUNCATE + || eNew==PAGER_JOURNALMODE_PERSIST + || eNew==PAGER_JOURNALMODE_OFF + || eNew==PAGER_JOURNALMODE_MEMORY + || eNew==PAGER_JOURNALMODE_WAL + || eNew==PAGER_JOURNALMODE_QUERY + ); + assert( pOp->p1>=0 && pOp->p1nDb ); + assert( p->readOnly==0 ); + + pBt = db->aDb[pOp->p1].pBt; + pPager = sqlite3BtreePager(pBt); + eOld = sqlite3PagerGetJournalMode(pPager); + if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; + if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; + +#ifndef SQLITE_OMIT_WAL + zFilename = sqlite3PagerFilename(pPager, 1); + + /* Do not allow a transition to journal_mode=WAL for a database + ** in temporary storage or if the VFS does not support shared memory + */ + if( eNew==PAGER_JOURNALMODE_WAL + && (sqlite3Strlen30(zFilename)==0 /* Temp file */ + || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ + ){ + eNew = eOld; + } + + if( (eNew!=eOld) + && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) + ){ + if( !db->autoCommit || db->nVdbeRead>1 ){ + rc = SQLITE_ERROR; + sqlite3VdbeError(p, + "cannot change %s wal mode from within a transaction", + (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") + ); + goto abort_due_to_error; + }else{ + + if( eOld==PAGER_JOURNALMODE_WAL ){ + /* If leaving WAL mode, close the log file. If successful, the call + ** to PagerCloseWal() checkpoints and deletes the write-ahead-log + ** file. An EXCLUSIVE lock may still be held on the database file + ** after a successful return. + */ + rc = sqlite3PagerCloseWal(pPager); + if( rc==SQLITE_OK ){ + sqlite3PagerSetJournalMode(pPager, eNew); + } + }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ + /* Cannot transition directly from MEMORY to WAL. Use mode OFF + ** as an intermediate */ + sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); + } + + /* Open a transaction on the database file. Regardless of the journal + ** mode, this transaction always uses a rollback journal. + */ + assert( sqlite3BtreeIsInTrans(pBt)==0 ); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); + } + } + } +#endif /* ifndef SQLITE_OMIT_WAL */ + + if( rc ) eNew = eOld; + eNew = sqlite3PagerSetJournalMode(pPager, eNew); + + pOut->flags = MEM_Str|MEM_Static|MEM_Term; + pOut->z = (char *)sqlite3JournalModename(eNew); + pOut->n = sqlite3Strlen30(pOut->z); + pOut->enc = SQLITE_UTF8; + sqlite3VdbeChangeEncoding(pOut, encoding); + if( rc ) goto abort_due_to_error; + break; +}; +#endif /* SQLITE_OMIT_PRAGMA */ + +#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) +/* Opcode: Vacuum * * * * * +** +** Vacuum the entire database. This opcode will cause other virtual +** machines to be created and run. It may not be called from within +** a transaction. +*/ +case OP_Vacuum: { + assert( p->readOnly==0 ); + rc = sqlite3RunVacuum(&p->zErrMsg, db); + if( rc ) goto abort_due_to_error; + break; +} +#endif + +#if !defined(SQLITE_OMIT_AUTOVACUUM) +/* Opcode: IncrVacuum P1 P2 * * * +** +** Perform a single step of the incremental vacuum procedure on +** the P1 database. If the vacuum has finished, jump to instruction +** P2. Otherwise, fall through to the next instruction. +*/ +case OP_IncrVacuum: { /* jump */ + Btree *pBt; + + assert( pOp->p1>=0 && pOp->p1nDb ); + assert( DbMaskTest(p->btreeMask, pOp->p1) ); + assert( p->readOnly==0 ); + pBt = db->aDb[pOp->p1].pBt; + rc = sqlite3BtreeIncrVacuum(pBt); + VdbeBranchTaken(rc==SQLITE_DONE,2); + if( rc ){ + if( rc!=SQLITE_DONE ) goto abort_due_to_error; + rc = SQLITE_OK; + goto jump_to_p2; + } + break; +} +#endif + +/* Opcode: Expire P1 * * * * +** +** Cause precompiled statements to expire. When an expired statement +** is executed using sqlite3_step() it will either automatically +** reprepare itself (if it was originally created using sqlite3_prepare_v2()) +** or it will fail with SQLITE_SCHEMA. +** +** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, +** then only the currently executing statement is expired. +*/ +case OP_Expire: { + if( !pOp->p1 ){ + sqlite3ExpirePreparedStatements(db); + }else{ + p->expired = 1; + } + break; +} + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* Opcode: TableLock P1 P2 P3 P4 * +** Synopsis: iDb=P1 root=P2 write=P3 +** +** Obtain a lock on a particular table. This instruction is only used when +** the shared-cache feature is enabled. +** +** P1 is the index of the database in sqlite3.aDb[] of the database +** on which the lock is acquired. A readlock is obtained if P3==0 or +** a write lock if P3==1. +** +** P2 contains the root-page of the table to lock. +** +** P4 contains a pointer to the name of the table being locked. This is only +** used to generate an error message if the lock cannot be obtained. +*/ +case OP_TableLock: { + u8 isWriteLock = (u8)pOp->p3; + if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ + int p1 = pOp->p1; + assert( p1>=0 && p1nDb ); + assert( DbMaskTest(p->btreeMask, p1) ); + assert( isWriteLock==0 || isWriteLock==1 ); + rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); + if( rc ){ + if( (rc&0xFF)==SQLITE_LOCKED ){ + const char *z = pOp->p4.z; + sqlite3VdbeError(p, "database table is locked: %s", z); + } + goto abort_due_to_error; + } + } + break; +} +#endif /* SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VBegin * * * P4 * +** +** P4 may be a pointer to an sqlite3_vtab structure. If so, call the +** xBegin method for that table. +** +** Also, whether or not P4 is set, check that this is not being called from +** within a callback to a virtual table xSync() method. If it is, the error +** code will be set to SQLITE_LOCKED. +*/ +case OP_VBegin: { + VTable *pVTab; + pVTab = pOp->p4.pVtab; + rc = sqlite3VtabBegin(db, pVTab); + if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); + if( rc ) goto abort_due_to_error; + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VCreate P1 P2 * * * +** +** P2 is a register that holds the name of a virtual table in database +** P1. Call the xCreate method for that table. +*/ +case OP_VCreate: { + Mem sMem; /* For storing the record being decoded */ + const char *zTab; /* Name of the virtual table */ + + memset(&sMem, 0, sizeof(sMem)); + sMem.db = db; + /* Because P2 is always a static string, it is impossible for the + ** sqlite3VdbeMemCopy() to fail */ + assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); + assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); + rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); + assert( rc==SQLITE_OK ); + zTab = (const char*)sqlite3_value_text(&sMem); + assert( zTab || db->mallocFailed ); + if( zTab ){ + rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); + } + sqlite3VdbeMemRelease(&sMem); + if( rc ) goto abort_due_to_error; + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VDestroy P1 * * P4 * +** +** P4 is the name of a virtual table in database P1. Call the xDestroy method +** of that table. +*/ +case OP_VDestroy: { + db->nVDestroy++; + rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); + db->nVDestroy--; + if( rc ) goto abort_due_to_error; + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VOpen P1 * * P4 * +** +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. +** P1 is a cursor number. This opcode opens a cursor to the virtual +** table and stores that cursor in P1. +*/ +case OP_VOpen: { + VdbeCursor *pCur; + sqlite3_vtab_cursor *pVCur; + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + + assert( p->bIsReader ); + pCur = 0; + pVCur = 0; + pVtab = pOp->p4.pVtab->pVtab; + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ + rc = SQLITE_LOCKED; + goto abort_due_to_error; + } + pModule = pVtab->pModule; + rc = pModule->xOpen(pVtab, &pVCur); + sqlite3VtabImportErrmsg(p, pVtab); + if( rc ) goto abort_due_to_error; + + /* Initialize sqlite3_vtab_cursor base class */ + pVCur->pVtab = pVtab; + + /* Initialize vdbe cursor object */ + pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); + if( pCur ){ + pCur->uc.pVCur = pVCur; + pVtab->nRef++; + }else{ + assert( db->mallocFailed ); + pModule->xClose(pVCur); + goto no_mem; + } + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VFilter P1 P2 P3 P4 * +** Synopsis: iplan=r[P3] zplan='P4' +** +** P1 is a cursor opened using VOpen. P2 is an address to jump to if +** the filtered result set is empty. +** +** P4 is either NULL or a string that was generated by the xBestIndex +** method of the module. The interpretation of the P4 string is left +** to the module implementation. +** +** This opcode invokes the xFilter method on the virtual table specified +** by P1. The integer query plan parameter to xFilter is stored in register +** P3. Register P3+1 stores the argc parameter to be passed to the +** xFilter method. Registers P3+2..P3+1+argc are the argc +** additional parameters which are passed to +** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. +** +** A jump is made to P2 if the result set after filtering would be empty. +*/ +case OP_VFilter: { /* jump */ + int nArg; + int iQuery; + const sqlite3_module *pModule; + Mem *pQuery; + Mem *pArgc; + sqlite3_vtab_cursor *pVCur; + sqlite3_vtab *pVtab; + VdbeCursor *pCur; + int res; + int i; + Mem **apArg; + + pQuery = &aMem[pOp->p3]; + pArgc = &pQuery[1]; + pCur = p->apCsr[pOp->p1]; + assert( memIsValid(pQuery) ); + REGISTER_TRACE(pOp->p3, pQuery); + assert( pCur->eCurType==CURTYPE_VTAB ); + pVCur = pCur->uc.pVCur; + pVtab = pVCur->pVtab; + pModule = pVtab->pModule; + + /* Grab the index number and argc parameters */ + assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); + nArg = (int)pArgc->u.i; + iQuery = (int)pQuery->u.i; + + /* Invoke the xFilter method */ + res = 0; + apArg = p->apArg; + for(i = 0; ixFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); + sqlite3VtabImportErrmsg(p, pVtab); + if( rc ) goto abort_due_to_error; + res = pModule->xEof(pVCur); + pCur->nullRow = 0; + VdbeBranchTaken(res!=0,2); + if( res ) goto jump_to_p2; + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VColumn P1 P2 P3 * * +** Synopsis: r[P3]=vcolumn(P2) +** +** Store the value of the P2-th column of +** the row of the virtual-table that the +** P1 cursor is pointing to into register P3. +*/ +case OP_VColumn: { + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + Mem *pDest; + sqlite3_context sContext; + + VdbeCursor *pCur = p->apCsr[pOp->p1]; + assert( pCur->eCurType==CURTYPE_VTAB ); + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + pDest = &aMem[pOp->p3]; + memAboutToChange(p, pDest); + if( pCur->nullRow ){ + sqlite3VdbeMemSetNull(pDest); + break; + } + pVtab = pCur->uc.pVCur->pVtab; + pModule = pVtab->pModule; + assert( pModule->xColumn ); + memset(&sContext, 0, sizeof(sContext)); + sContext.pOut = pDest; + MemSetTypeFlag(pDest, MEM_Null); + rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); + sqlite3VtabImportErrmsg(p, pVtab); + if( sContext.isError ){ + rc = sContext.isError; + } + sqlite3VdbeChangeEncoding(pDest, encoding); + REGISTER_TRACE(pOp->p3, pDest); + UPDATE_MAX_BLOBSIZE(pDest); + + if( sqlite3VdbeMemTooBig(pDest) ){ + goto too_big; + } + if( rc ) goto abort_due_to_error; + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VNext P1 P2 * * * +** +** Advance virtual table P1 to the next row in its result set and +** jump to instruction P2. Or, if the virtual table has reached +** the end of its result set, then fall through to the next instruction. +*/ +case OP_VNext: { /* jump */ + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + int res; + VdbeCursor *pCur; + + res = 0; + pCur = p->apCsr[pOp->p1]; + assert( pCur->eCurType==CURTYPE_VTAB ); + if( pCur->nullRow ){ + break; + } + pVtab = pCur->uc.pVCur->pVtab; + pModule = pVtab->pModule; + assert( pModule->xNext ); + + /* Invoke the xNext() method of the module. There is no way for the + ** underlying implementation to return an error if one occurs during + ** xNext(). Instead, if an error occurs, true is returned (indicating that + ** data is available) and the error code returned when xColumn or + ** some other method is next invoked on the save virtual table cursor. + */ + rc = pModule->xNext(pCur->uc.pVCur); + sqlite3VtabImportErrmsg(p, pVtab); + if( rc ) goto abort_due_to_error; + res = pModule->xEof(pCur->uc.pVCur); + VdbeBranchTaken(!res,2); + if( !res ){ + /* If there is data, jump to P2 */ + goto jump_to_p2_and_check_for_interrupt; + } + goto check_for_interrupt; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VRename P1 * * P4 * +** +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. +** This opcode invokes the corresponding xRename method. The value +** in register P1 is passed as the zName argument to the xRename method. +*/ +case OP_VRename: { + sqlite3_vtab *pVtab; + Mem *pName; + + pVtab = pOp->p4.pVtab->pVtab; + pName = &aMem[pOp->p1]; + assert( pVtab->pModule->xRename ); + assert( memIsValid(pName) ); + assert( p->readOnly==0 ); + REGISTER_TRACE(pOp->p1, pName); + assert( pName->flags & MEM_Str ); + testcase( pName->enc==SQLITE_UTF8 ); + testcase( pName->enc==SQLITE_UTF16BE ); + testcase( pName->enc==SQLITE_UTF16LE ); + rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); + if( rc ) goto abort_due_to_error; + rc = pVtab->pModule->xRename(pVtab, pName->z); + sqlite3VtabImportErrmsg(p, pVtab); + p->expired = 0; + if( rc ) goto abort_due_to_error; + break; +} +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VUpdate P1 P2 P3 P4 P5 +** Synopsis: data=r[P3@P2] +** +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. +** This opcode invokes the corresponding xUpdate method. P2 values +** are contiguous memory cells starting at P3 to pass to the xUpdate +** invocation. The value in register (P3+P2-1) corresponds to the +** p2th element of the argv array passed to xUpdate. +** +** The xUpdate method will do a DELETE or an INSERT or both. +** The argv[0] element (which corresponds to memory cell P3) +** is the rowid of a row to delete. If argv[0] is NULL then no +** deletion occurs. The argv[1] element is the rowid of the new +** row. This can be NULL to have the virtual table select the new +** rowid for itself. The subsequent elements in the array are +** the values of columns in the new row. +** +** If P2==1 then no insert is performed. argv[0] is the rowid of +** a row to delete. +** +** P1 is a boolean flag. If it is set to true and the xUpdate call +** is successful, then the value returned by sqlite3_last_insert_rowid() +** is set to the value of the rowid for the row just inserted. +** +** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to +** apply in the case of a constraint failure on an insert or update. +*/ +case OP_VUpdate: { + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + int nArg; + int i; + sqlite_int64 rowid; + Mem **apArg; + Mem *pX; + + assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback + || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace + ); + assert( p->readOnly==0 ); + pVtab = pOp->p4.pVtab->pVtab; + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ + rc = SQLITE_LOCKED; + goto abort_due_to_error; + } + pModule = pVtab->pModule; + nArg = pOp->p2; + assert( pOp->p4type==P4_VTAB ); + if( ALWAYS(pModule->xUpdate) ){ + u8 vtabOnConflict = db->vtabOnConflict; + apArg = p->apArg; + pX = &aMem[pOp->p3]; + for(i=0; ivtabOnConflict = pOp->p5; + rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); + db->vtabOnConflict = vtabOnConflict; + sqlite3VtabImportErrmsg(p, pVtab); + if( rc==SQLITE_OK && pOp->p1 ){ + assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); + db->lastRowid = lastRowid = rowid; + } + if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ + if( pOp->p5==OE_Ignore ){ + rc = SQLITE_OK; + }else{ + p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); + } + }else{ + p->nChange++; + } + if( rc ) goto abort_due_to_error; + } + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* Opcode: Pagecount P1 P2 * * * +** +** Write the current number of pages in database P1 to memory cell P2. +*/ +case OP_Pagecount: { /* out2 */ + pOut = out2Prerelease(p, pOp); + pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); + break; +} +#endif + + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* Opcode: MaxPgcnt P1 P2 P3 * * +** +** Try to set the maximum page count for database P1 to the value in P3. +** Do not let the maximum page count fall below the current page count and +** do not change the maximum page count value if P3==0. +** +** Store the maximum page count after the change in register P2. +*/ +case OP_MaxPgcnt: { /* out2 */ + unsigned int newMax; + Btree *pBt; + + pOut = out2Prerelease(p, pOp); + pBt = db->aDb[pOp->p1].pBt; + newMax = 0; + if( pOp->p3 ){ + newMax = sqlite3BtreeLastPage(pBt); + if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; + } + pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); + break; +} +#endif + + +/* Opcode: Init * P2 * P4 * +** Synopsis: Start at P2 +** +** Programs contain a single instance of this opcode as the very first +** opcode. +** +** If tracing is enabled (by the sqlite3_trace()) interface, then +** the UTF-8 string contained in P4 is emitted on the trace callback. +** Or if P4 is blank, use the string returned by sqlite3_sql(). +** +** If P2 is not zero, jump to instruction P2. +*/ +case OP_Init: { /* jump */ + char *zTrace; + char *z; + +#ifndef SQLITE_OMIT_TRACE + if( db->xTrace + && !p->doingRerun + && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 + ){ + z = sqlite3VdbeExpandSql(p, zTrace); + db->xTrace(db->pTraceArg, z); + sqlite3DbFree(db, z); + } +#ifdef SQLITE_USE_FCNTL_TRACE + zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); + if( zTrace ){ + int i; + for(i=0; inDb; i++){ + if( DbMaskTest(p->btreeMask, i)==0 ) continue; + sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); + } + } +#endif /* SQLITE_USE_FCNTL_TRACE */ +#ifdef SQLITE_DEBUG + if( (db->flags & SQLITE_SqlTrace)!=0 + && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 + ){ + sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); + } +#endif /* SQLITE_DEBUG */ +#endif /* SQLITE_OMIT_TRACE */ + if( pOp->p2 ) goto jump_to_p2; + break; +} + +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* Opcode: CursorHint P1 * * P4 * +** +** Provide a hint to cursor P1 that it only needs to return rows that +** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer +** to values currently held in registers. TK_COLUMN terms in the P4 +** expression refer to columns in the b-tree to which cursor P1 is pointing. +*/ +case OP_CursorHint: { + VdbeCursor *pC; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pOp->p4type==P4_EXPR ); + pC = p->apCsr[pOp->p1]; + if( pC ){ + assert( pC->eCurType==CURTYPE_BTREE ); + sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, + pOp->p4.pExpr, aMem); + } + break; +} +#endif /* SQLITE_ENABLE_CURSOR_HINTS */ + +/* Opcode: Noop * * * * * +** +** Do nothing. This instruction is often useful as a jump +** destination. +*/ +/* +** The magic Explain opcode are only inserted when explain==2 (which +** is to say when the EXPLAIN QUERY PLAN syntax is used.) +** This opcode records information from the optimizer. It is the +** the same as a no-op. This opcodesnever appears in a real VM program. +*/ +default: { /* This is really OP_Noop and OP_Explain */ + assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); + break; +} + +/***************************************************************************** +** The cases of the switch statement above this line should all be indented +** by 6 spaces. But the left-most 6 spaces have been removed to improve the +** readability. From this point on down, the normal indentation rules are +** restored. +*****************************************************************************/ + } + +#ifdef VDBE_PROFILE + { + u64 endTime = sqlite3Hwtime(); + if( endTime>start ) pOrigOp->cycles += endTime - start; + pOrigOp->cnt++; + } +#endif + + /* The following code adds nothing to the actual functionality + ** of the program. It is only here for testing and debugging. + ** On the other hand, it does burn CPU cycles every time through + ** the evaluator loop. So we can leave it out when NDEBUG is defined. + */ +#ifndef NDEBUG + assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); + +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_VdbeTrace ){ + u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; + if( rc!=0 ) printf("rc=%d\n",rc); + if( opProperty & (OPFLG_OUT2) ){ + registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); + } + if( opProperty & OPFLG_OUT3 ){ + registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); + } + } +#endif /* SQLITE_DEBUG */ +#endif /* NDEBUG */ + } /* The end of the for(;;) loop the loops through opcodes */ + + /* If we reach this point, it means that execution is finished with + ** an error of some kind. + */ +abort_due_to_error: + if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; + assert( rc ); + if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ + sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); + } + p->rc = rc; + sqlite3SystemError(db, rc); + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(rc, "statement aborts at %d: [%s] %s", + (int)(pOp - aOp), p->zSql, p->zErrMsg); + sqlite3VdbeHalt(p); + if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); + rc = SQLITE_ERROR; + if( resetSchemaOnFault>0 ){ + sqlite3ResetOneSchema(db, resetSchemaOnFault-1); + } + + /* This is the only way out of this procedure. We have to + ** release the mutexes on btrees that were acquired at the + ** top. */ +vdbe_return: + db->lastRowid = lastRowid; + testcase( nVmStep>0 ); + p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; + sqlite3VdbeLeave(p); + assert( rc!=SQLITE_OK || nExtraDelete==0 + || sqlite3_strlike("DELETE%",p->zSql,0)!=0 + ); + return rc; + + /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH + ** is encountered. + */ +too_big: + sqlite3VdbeError(p, "string or blob too big"); + rc = SQLITE_TOOBIG; + goto abort_due_to_error; + + /* Jump to here if a malloc() fails. + */ +no_mem: + sqlite3OomFault(db); + sqlite3VdbeError(p, "out of memory"); + rc = SQLITE_NOMEM_BKPT; + goto abort_due_to_error; + + /* Jump to here if the sqlite3_interrupt() API sets the interrupt + ** flag. + */ +abort_due_to_interrupt: + assert( db->u1.isInterrupted ); + rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; + p->rc = rc; + sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); + goto abort_due_to_error; +} + + +/************** End of vdbe.c ************************************************/ +/************** Begin file vdbeblob.c ****************************************/ +/* +** 2007 May 1 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code used to implement incremental BLOB I/O. +*/ + +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +#ifndef SQLITE_OMIT_INCRBLOB + +/* +** Valid sqlite3_blob* handles point to Incrblob structures. +*/ +typedef struct Incrblob Incrblob; +struct Incrblob { + int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ + int nByte; /* Size of open blob, in bytes */ + int iOffset; /* Byte offset of blob in cursor data */ + int iCol; /* Table column this handle is open on */ + BtCursor *pCsr; /* Cursor pointing at blob row */ + sqlite3_stmt *pStmt; /* Statement holding cursor open */ + sqlite3 *db; /* The associated database */ + char *zDb; /* Database name */ + Table *pTab; /* Table object */ +}; + + +/* +** This function is used by both blob_open() and blob_reopen(). It seeks +** the b-tree cursor associated with blob handle p to point to row iRow. +** If successful, SQLITE_OK is returned and subsequent calls to +** sqlite3_blob_read() or sqlite3_blob_write() access the specified row. +** +** If an error occurs, or if the specified row does not exist or does not +** contain a value of type TEXT or BLOB in the column nominated when the +** blob handle was opened, then an error code is returned and *pzErr may +** be set to point to a buffer containing an error message. It is the +** responsibility of the caller to free the error message buffer using +** sqlite3DbFree(). +** +** If an error does occur, then the b-tree cursor is closed. All subsequent +** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will +** immediately return SQLITE_ABORT. +*/ +static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ + int rc; /* Error code */ + char *zErr = 0; /* Error message */ + Vdbe *v = (Vdbe *)p->pStmt; + + /* Set the value of the SQL statements only variable to integer iRow. + ** This is done directly instead of using sqlite3_bind_int64() to avoid + ** triggering asserts related to mutexes. + */ + assert( v->aVar[0].flags&MEM_Int ); + v->aVar[0].u.i = iRow; + + rc = sqlite3_step(p->pStmt); + if( rc==SQLITE_ROW ){ + VdbeCursor *pC = v->apCsr[0]; + u32 type = pC->aType[p->iCol]; + if( type<12 ){ + zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", + type==0?"null": type==7?"real": "integer" + ); + rc = SQLITE_ERROR; + sqlite3_finalize(p->pStmt); + p->pStmt = 0; + }else{ + p->iOffset = pC->aType[p->iCol + pC->nField]; + p->nByte = sqlite3VdbeSerialTypeLen(type); + p->pCsr = pC->uc.pCursor; + sqlite3BtreeIncrblobCursor(p->pCsr); + } + } + + if( rc==SQLITE_ROW ){ + rc = SQLITE_OK; + }else if( p->pStmt ){ + rc = sqlite3_finalize(p->pStmt); + p->pStmt = 0; + if( rc==SQLITE_OK ){ + zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow); + rc = SQLITE_ERROR; + }else{ + zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db)); + } + } + + assert( rc!=SQLITE_OK || zErr==0 ); + assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE ); + + *pzErr = zErr; + return rc; +} + +/* +** Open a blob handle. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_open( + sqlite3* db, /* The database connection */ + const char *zDb, /* The attached database containing the blob */ + const char *zTable, /* The table containing the blob */ + const char *zColumn, /* The column containing the blob */ + sqlite_int64 iRow, /* The row containing the glob */ + int flags, /* True -> read/write access, false -> read-only */ + sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ +){ + int nAttempt = 0; + int iCol; /* Index of zColumn in row-record */ + int rc = SQLITE_OK; + char *zErr = 0; + Table *pTab; + Parse *pParse = 0; + Incrblob *pBlob = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( ppBlob==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + *ppBlob = 0; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + flags = !!flags; /* flags = (flags ? 1 : 0); */ + + sqlite3_mutex_enter(db->mutex); + + pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); + if( !pBlob ) goto blob_open_out; + pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); + if( !pParse ) goto blob_open_out; + + do { + memset(pParse, 0, sizeof(Parse)); + pParse->db = db; + sqlite3DbFree(db, zErr); + zErr = 0; + + sqlite3BtreeEnterAll(db); + pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); + if( pTab && IsVirtual(pTab) ){ + pTab = 0; + sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); + } + if( pTab && !HasRowid(pTab) ){ + pTab = 0; + sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable); + } +#ifndef SQLITE_OMIT_VIEW + if( pTab && pTab->pSelect ){ + pTab = 0; + sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); + } +#endif + if( !pTab ){ + if( pParse->zErrMsg ){ + sqlite3DbFree(db, zErr); + zErr = pParse->zErrMsg; + pParse->zErrMsg = 0; + } + rc = SQLITE_ERROR; + sqlite3BtreeLeaveAll(db); + goto blob_open_out; + } + pBlob->pTab = pTab; + pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zName; + + /* Now search pTab for the exact column. */ + for(iCol=0; iColnCol; iCol++) { + if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ + break; + } + } + if( iCol==pTab->nCol ){ + sqlite3DbFree(db, zErr); + zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); + rc = SQLITE_ERROR; + sqlite3BtreeLeaveAll(db); + goto blob_open_out; + } + + /* If the value is being opened for writing, check that the + ** column is not indexed, and that it is not part of a foreign key. + ** It is against the rules to open a column to which either of these + ** descriptions applies for writing. */ + if( flags ){ + const char *zFault = 0; + Index *pIdx; +#ifndef SQLITE_OMIT_FOREIGN_KEY + if( db->flags&SQLITE_ForeignKeys ){ + /* Check that the column is not part of an FK child key definition. It + ** is not necessary to check if it is part of a parent key, as parent + ** key columns must be indexed. The check below will pick up this + ** case. */ + FKey *pFKey; + for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + int j; + for(j=0; jnCol; j++){ + if( pFKey->aCol[j].iFrom==iCol ){ + zFault = "foreign key"; + } + } + } + } +#endif + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int j; + for(j=0; jnKeyCol; j++){ + /* FIXME: Be smarter about indexes that use expressions */ + if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){ + zFault = "indexed"; + } + } + } + if( zFault ){ + sqlite3DbFree(db, zErr); + zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); + rc = SQLITE_ERROR; + sqlite3BtreeLeaveAll(db); + goto blob_open_out; + } + } + + pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); + assert( pBlob->pStmt || db->mallocFailed ); + if( pBlob->pStmt ){ + + /* This VDBE program seeks a btree cursor to the identified + ** db/table/row entry. The reason for using a vdbe program instead + ** of writing code to use the b-tree layer directly is that the + ** vdbe program will take advantage of the various transaction, + ** locking and error handling infrastructure built into the vdbe. + ** + ** After seeking the cursor, the vdbe executes an OP_ResultRow. + ** Code external to the Vdbe then "borrows" the b-tree cursor and + ** uses it to implement the blob_read(), blob_write() and + ** blob_bytes() functions. + ** + ** The sqlite3_blob_close() function finalizes the vdbe program, + ** which closes the b-tree cursor and (possibly) commits the + ** transaction. + */ + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList openBlob[] = { + {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ + {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ + {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */ + {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */ + {OP_Column, 0, 0, 1}, /* 4 */ + {OP_ResultRow, 1, 0, 0}, /* 5 */ + {OP_Goto, 0, 2, 0}, /* 6 */ + {OP_Close, 0, 0, 0}, /* 7 */ + {OP_Halt, 0, 0, 0}, /* 8 */ + }; + Vdbe *v = (Vdbe *)pBlob->pStmt; + int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + VdbeOp *aOp; + + sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, + pTab->pSchema->schema_cookie, + pTab->pSchema->iGeneration); + sqlite3VdbeChangeP5(v, 1); + aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); + + /* Make sure a mutex is held on the table to be accessed */ + sqlite3VdbeUsesBtree(v, iDb); + + if( db->mallocFailed==0 ){ + assert( aOp!=0 ); + /* Configure the OP_TableLock instruction */ +#ifdef SQLITE_OMIT_SHARED_CACHE + aOp[0].opcode = OP_Noop; +#else + aOp[0].p1 = iDb; + aOp[0].p2 = pTab->tnum; + aOp[0].p3 = flags; + sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); + } + if( db->mallocFailed==0 ){ +#endif + + /* Remove either the OP_OpenWrite or OpenRead. Set the P2 + ** parameter of the other to pTab->tnum. */ + if( flags ) aOp[1].opcode = OP_OpenWrite; + aOp[1].p2 = pTab->tnum; + aOp[1].p3 = iDb; + + /* Configure the number of columns. Configure the cursor to + ** think that the table has one more column than it really + ** does. An OP_Column to retrieve this imaginary column will + ** always return an SQL NULL. This is useful because it means + ** we can invoke OP_Column to fill in the vdbe cursors type + ** and offset cache without causing any IO. + */ + aOp[1].p4type = P4_INT32; + aOp[1].p4.i = pTab->nCol+1; + aOp[4].p2 = pTab->nCol; + + pParse->nVar = 1; + pParse->nMem = 1; + pParse->nTab = 1; + sqlite3VdbeMakeReady(v, pParse); + } + } + + pBlob->flags = flags; + pBlob->iCol = iCol; + pBlob->db = db; + sqlite3BtreeLeaveAll(db); + if( db->mallocFailed ){ + goto blob_open_out; + } + sqlite3_bind_int64(pBlob->pStmt, 1, iRow); + rc = blobSeekToRow(pBlob, iRow, &zErr); + } while( (++nAttempt)mallocFailed==0 ){ + *ppBlob = (sqlite3_blob *)pBlob; + }else{ + if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); + sqlite3DbFree(db, pBlob); + } + sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); + sqlite3DbFree(db, zErr); + sqlite3ParserReset(pParse); + sqlite3StackFree(db, pParse); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Close a blob handle that was previously created using +** sqlite3_blob_open(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *pBlob){ + Incrblob *p = (Incrblob *)pBlob; + int rc; + sqlite3 *db; + + if( p ){ + db = p->db; + sqlite3_mutex_enter(db->mutex); + rc = sqlite3_finalize(p->pStmt); + sqlite3DbFree(db, p); + sqlite3_mutex_leave(db->mutex); + }else{ + rc = SQLITE_OK; + } + return rc; +} + +/* +** Perform a read or write operation on a blob +*/ +static int blobReadWrite( + sqlite3_blob *pBlob, + void *z, + int n, + int iOffset, + int (*xCall)(BtCursor*, u32, u32, void*) +){ + int rc; + Incrblob *p = (Incrblob *)pBlob; + Vdbe *v; + sqlite3 *db; + + if( p==0 ) return SQLITE_MISUSE_BKPT; + db = p->db; + sqlite3_mutex_enter(db->mutex); + v = (Vdbe*)p->pStmt; + + if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){ + /* Request is out of range. Return a transient error. */ + rc = SQLITE_ERROR; + }else if( v==0 ){ + /* If there is no statement handle, then the blob-handle has + ** already been invalidated. Return SQLITE_ABORT in this case. + */ + rc = SQLITE_ABORT; + }else{ + /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is + ** returned, clean-up the statement handle. + */ + assert( db == v->db ); + sqlite3BtreeEnterCursor(p->pCsr); + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){ + /* If a pre-update hook is registered and this is a write cursor, + ** invoke it here. + ** + ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this + ** operation should really be an SQLITE_UPDATE. This is probably + ** incorrect, but is convenient because at this point the new.* values + ** are not easily obtainable. And for the sessions module, an + ** SQLITE_UPDATE where the PK columns do not change is handled in the + ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually + ** slightly more efficient). Since you cannot write to a PK column + ** using the incremental-blob API, this works. For the sessions module + ** anyhow. + */ + sqlite3_int64 iKey; + sqlite3BtreeKeySize(p->pCsr, &iKey); + sqlite3VdbePreUpdateHook( + v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1 + ); + } +#endif + + rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); + sqlite3BtreeLeaveCursor(p->pCsr); + if( rc==SQLITE_ABORT ){ + sqlite3VdbeFinalize(v); + p->pStmt = 0; + }else{ + v->rc = rc; + } + } + sqlite3Error(db, rc); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Read data from a blob handle. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ + return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); +} + +/* +** Write data to a blob handle. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ + return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); +} + +/* +** Query a blob handle for the size of the data. +** +** The Incrblob.nByte field is fixed for the lifetime of the Incrblob +** so no mutex is required for access. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *pBlob){ + Incrblob *p = (Incrblob *)pBlob; + return (p && p->pStmt) ? p->nByte : 0; +} + +/* +** Move an existing blob handle to point to a different row of the same +** database table. +** +** If an error occurs, or if the specified row does not exist or does not +** contain a blob or text value, then an error code is returned and the +** database handle error code and message set. If this happens, then all +** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) +** immediately return SQLITE_ABORT. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){ + int rc; + Incrblob *p = (Incrblob *)pBlob; + sqlite3 *db; + + if( p==0 ) return SQLITE_MISUSE_BKPT; + db = p->db; + sqlite3_mutex_enter(db->mutex); + + if( p->pStmt==0 ){ + /* If there is no statement handle, then the blob-handle has + ** already been invalidated. Return SQLITE_ABORT in this case. + */ + rc = SQLITE_ABORT; + }else{ + char *zErr; + rc = blobSeekToRow(p, iRow, &zErr); + if( rc!=SQLITE_OK ){ + sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); + sqlite3DbFree(db, zErr); + } + assert( rc!=SQLITE_SCHEMA ); + } + + rc = sqlite3ApiExit(db, rc); + assert( rc==SQLITE_OK || p->pStmt==0 ); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#endif /* #ifndef SQLITE_OMIT_INCRBLOB */ + +/************** End of vdbeblob.c ********************************************/ +/************** Begin file vdbesort.c ****************************************/ +/* +** 2011-07-09 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code for the VdbeSorter object, used in concert with +** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements +** or by SELECT statements with ORDER BY clauses that cannot be satisfied +** using indexes and without LIMIT clauses. +** +** The VdbeSorter object implements a multi-threaded external merge sort +** algorithm that is efficient even if the number of elements being sorted +** exceeds the available memory. +** +** Here is the (internal, non-API) interface between this module and the +** rest of the SQLite system: +** +** sqlite3VdbeSorterInit() Create a new VdbeSorter object. +** +** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter +** object. The row is a binary blob in the +** OP_MakeRecord format that contains both +** the ORDER BY key columns and result columns +** in the case of a SELECT w/ ORDER BY, or +** the complete record for an index entry +** in the case of a CREATE INDEX. +** +** sqlite3VdbeSorterRewind() Sort all content previously added. +** Position the read cursor on the +** first sorted element. +** +** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted +** element. +** +** sqlite3VdbeSorterRowkey() Return the complete binary blob for the +** row currently under the read cursor. +** +** sqlite3VdbeSorterCompare() Compare the binary blob for the row +** currently under the read cursor against +** another binary blob X and report if +** X is strictly less than the read cursor. +** Used to enforce uniqueness in a +** CREATE UNIQUE INDEX statement. +** +** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim +** all resources. +** +** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This +** is like Close() followed by Init() only +** much faster. +** +** The interfaces above must be called in a particular order. Write() can +** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and +** Compare() can only occur in between Rewind() and Close()/Reset(). i.e. +** +** Init() +** for each record: Write() +** Rewind() +** Rowkey()/Compare() +** Next() +** Close() +** +** Algorithm: +** +** Records passed to the sorter via calls to Write() are initially held +** unsorted in main memory. Assuming the amount of memory used never exceeds +** a threshold, when Rewind() is called the set of records is sorted using +** an in-memory merge sort. In this case, no temporary files are required +** and subsequent calls to Rowkey(), Next() and Compare() read records +** directly from main memory. +** +** If the amount of space used to store records in main memory exceeds the +** threshold, then the set of records currently in memory are sorted and +** written to a temporary file in "Packed Memory Array" (PMA) format. +** A PMA created at this point is known as a "level-0 PMA". Higher levels +** of PMAs may be created by merging existing PMAs together - for example +** merging two or more level-0 PMAs together creates a level-1 PMA. +** +** The threshold for the amount of main memory to use before flushing +** records to a PMA is roughly the same as the limit configured for the +** page-cache of the main database. Specifically, the threshold is set to +** the value returned by "PRAGMA main.page_size" multipled by +** that returned by "PRAGMA main.cache_size", in bytes. +** +** If the sorter is running in single-threaded mode, then all PMAs generated +** are appended to a single temporary file. Or, if the sorter is running in +** multi-threaded mode then up to (N+1) temporary files may be opened, where +** N is the configured number of worker threads. In this case, instead of +** sorting the records and writing the PMA to a temporary file itself, the +** calling thread usually launches a worker thread to do so. Except, if +** there are already N worker threads running, the main thread does the work +** itself. +** +** The sorter is running in multi-threaded mode if (a) the library was built +** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater +** than zero, and (b) worker threads have been enabled at runtime by calling +** "PRAGMA threads=N" with some value of N greater than 0. +** +** When Rewind() is called, any data remaining in memory is flushed to a +** final PMA. So at this point the data is stored in some number of sorted +** PMAs within temporary files on disk. +** +** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the +** sorter is running in single-threaded mode, then these PMAs are merged +** incrementally as keys are retreived from the sorter by the VDBE. The +** MergeEngine object, described in further detail below, performs this +** merge. +** +** Or, if running in multi-threaded mode, then a background thread is +** launched to merge the existing PMAs. Once the background thread has +** merged T bytes of data into a single sorted PMA, the main thread +** begins reading keys from that PMA while the background thread proceeds +** with merging the next T bytes of data. And so on. +** +** Parameter T is set to half the value of the memory threshold used +** by Write() above to determine when to create a new PMA. +** +** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when +** Rewind() is called, then a hierarchy of incremental-merges is used. +** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on +** disk are merged together. Then T bytes of data from the second set, and +** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT +** PMAs at a time. This done is to improve locality. +** +** If running in multi-threaded mode and there are more than +** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more +** than one background thread may be created. Specifically, there may be +** one background thread for each temporary file on disk, and one background +** thread to merge the output of each of the others to a single PMA for +** the main thread to read from. +*/ +/* #include "sqliteInt.h" */ +/* #include "vdbeInt.h" */ + +/* +** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various +** messages to stderr that may be helpful in understanding the performance +** characteristics of the sorter in multi-threaded mode. +*/ +#if 0 +# define SQLITE_DEBUG_SORTER_THREADS 1 +#endif + +/* +** Hard-coded maximum amount of data to accumulate in memory before flushing +** to a level 0 PMA. The purpose of this limit is to prevent various integer +** overflows. 512MiB. +*/ +#define SQLITE_MAX_PMASZ (1<<29) + +/* +** Private objects used by the sorter +*/ +typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ +typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ +typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */ +typedef struct SorterRecord SorterRecord; /* A record being sorted */ +typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */ +typedef struct SorterFile SorterFile; /* Temporary file object wrapper */ +typedef struct SorterList SorterList; /* In-memory list of records */ +typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */ + +/* +** A container for a temp file handle and the current amount of data +** stored in the file. +*/ +struct SorterFile { + sqlite3_file *pFd; /* File handle */ + i64 iEof; /* Bytes of data stored in pFd */ +}; + +/* +** An in-memory list of objects to be sorted. +** +** If aMemory==0 then each object is allocated separately and the objects +** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects +** are stored in the aMemory[] bulk memory, one right after the other, and +** are connected using SorterRecord.u.iNext. +*/ +struct SorterList { + SorterRecord *pList; /* Linked list of records */ + u8 *aMemory; /* If non-NULL, bulk memory to hold pList */ + int szPMA; /* Size of pList as PMA in bytes */ +}; + +/* +** The MergeEngine object is used to combine two or more smaller PMAs into +** one big PMA using a merge operation. Separate PMAs all need to be +** combined into one big PMA in order to be able to step through the sorted +** records in order. +** +** The aReadr[] array contains a PmaReader object for each of the PMAs being +** merged. An aReadr[] object either points to a valid key or else is at EOF. +** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.) +** For the purposes of the paragraphs below, we assume that the array is +** actually N elements in size, where N is the smallest power of 2 greater +** to or equal to the number of PMAs being merged. The extra aReadr[] elements +** are treated as if they are empty (always at EOF). +** +** The aTree[] array is also N elements in size. The value of N is stored in +** the MergeEngine.nTree variable. +** +** The final (N/2) elements of aTree[] contain the results of comparing +** pairs of PMA keys together. Element i contains the result of +** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the +** aTree element is set to the index of it. +** +** For the purposes of this comparison, EOF is considered greater than any +** other key value. If the keys are equal (only possible with two EOF +** values), it doesn't matter which index is stored. +** +** The (N/4) elements of aTree[] that precede the final (N/2) described +** above contains the index of the smallest of each block of 4 PmaReaders +** And so on. So that aTree[1] contains the index of the PmaReader that +** currently points to the smallest key value. aTree[0] is unused. +** +** Example: +** +** aReadr[0] -> Banana +** aReadr[1] -> Feijoa +** aReadr[2] -> Elderberry +** aReadr[3] -> Currant +** aReadr[4] -> Grapefruit +** aReadr[5] -> Apple +** aReadr[6] -> Durian +** aReadr[7] -> EOF +** +** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } +** +** The current element is "Apple" (the value of the key indicated by +** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will +** be advanced to the next key in its segment. Say the next key is +** "Eggplant": +** +** aReadr[5] -> Eggplant +** +** The contents of aTree[] are updated first by comparing the new PmaReader +** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader +** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree. +** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader +** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Bananafile2. And instead of using a +** background thread to prepare data for the PmaReader, with a single +** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with +** keys from pMerger by the calling thread whenever the PmaReader runs out +** of data. +*/ +struct IncrMerger { + SortSubtask *pTask; /* Task that owns this merger */ + MergeEngine *pMerger; /* Merge engine thread reads data from */ + i64 iStartOff; /* Offset to start writing file at */ + int mxSz; /* Maximum bytes of data to store */ + int bEof; /* Set to true when merge is finished */ + int bUseThread; /* True to use a bg thread for this object */ + SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */ +}; + +/* +** An instance of this object is used for writing a PMA. +** +** The PMA is written one record at a time. Each record is of an arbitrary +** size. But I/O is more efficient if it occurs in page-sized blocks where +** each block is aligned on a page boundary. This object caches writes to +** the PMA so that aligned, page-size blocks are written. +*/ +struct PmaWriter { + int eFWErr; /* Non-zero if in an error state */ + u8 *aBuffer; /* Pointer to write buffer */ + int nBuffer; /* Size of write buffer in bytes */ + int iBufStart; /* First byte of buffer to write */ + int iBufEnd; /* Last byte of buffer to write */ + i64 iWriteOff; /* Offset of start of buffer in file */ + sqlite3_file *pFd; /* File handle to write to */ +}; + +/* +** This object is the header on a single record while that record is being +** held in memory and prior to being written out as part of a PMA. +** +** How the linked list is connected depends on how memory is being managed +** by this module. If using a separate allocation for each in-memory record +** (VdbeSorter.list.aMemory==0), then the list is always connected using the +** SorterRecord.u.pNext pointers. +** +** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0), +** then while records are being accumulated the list is linked using the +** SorterRecord.u.iNext offset. This is because the aMemory[] array may +** be sqlite3Realloc()ed while records are being accumulated. Once the VM +** has finished passing records to the sorter, or when the in-memory buffer +** is full, the list is sorted. As part of the sorting process, it is +** converted to use the SorterRecord.u.pNext pointers. See function +** vdbeSorterSort() for details. +*/ +struct SorterRecord { + int nVal; /* Size of the record in bytes */ + union { + SorterRecord *pNext; /* Pointer to next record in list */ + int iNext; /* Offset within aMemory of next record */ + } u; + /* The data for the record immediately follows this header */ +}; + +/* Return a pointer to the buffer containing the record data for SorterRecord +** object p. Should be used as if: +** +** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; } +*/ +#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1)) + + +/* Maximum number of PMAs that a single MergeEngine can merge */ +#define SORTER_MAX_MERGE_COUNT 16 + +static int vdbeIncrSwap(IncrMerger*); +static void vdbeIncrFree(IncrMerger *); + +/* +** Free all memory belonging to the PmaReader object passed as the +** argument. All structure fields are set to zero before returning. +*/ +static void vdbePmaReaderClear(PmaReader *pReadr){ + sqlite3_free(pReadr->aAlloc); + sqlite3_free(pReadr->aBuffer); + if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap); + vdbeIncrFree(pReadr->pIncr); + memset(pReadr, 0, sizeof(PmaReader)); +} + +/* +** Read the next nByte bytes of data from the PMA p. +** If successful, set *ppOut to point to a buffer containing the data +** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite +** error code. +** +** The buffer returned in *ppOut is only valid until the +** next call to this function. +*/ +static int vdbePmaReadBlob( + PmaReader *p, /* PmaReader from which to take the blob */ + int nByte, /* Bytes of data to read */ + u8 **ppOut /* OUT: Pointer to buffer containing data */ +){ + int iBuf; /* Offset within buffer to read from */ + int nAvail; /* Bytes of data available in buffer */ + + if( p->aMap ){ + *ppOut = &p->aMap[p->iReadOff]; + p->iReadOff += nByte; + return SQLITE_OK; + } + + assert( p->aBuffer ); + + /* If there is no more data to be read from the buffer, read the next + ** p->nBuffer bytes of data from the file into it. Or, if there are less + ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */ + iBuf = p->iReadOff % p->nBuffer; + if( iBuf==0 ){ + int nRead; /* Bytes to read from disk */ + int rc; /* sqlite3OsRead() return code */ + + /* Determine how many bytes of data to read. */ + if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){ + nRead = p->nBuffer; + }else{ + nRead = (int)(p->iEof - p->iReadOff); + } + assert( nRead>0 ); + + /* Readr data from the file. Return early if an error occurs. */ + rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff); + assert( rc!=SQLITE_IOERR_SHORT_READ ); + if( rc!=SQLITE_OK ) return rc; + } + nAvail = p->nBuffer - iBuf; + + if( nByte<=nAvail ){ + /* The requested data is available in the in-memory buffer. In this + ** case there is no need to make a copy of the data, just return a + ** pointer into the buffer to the caller. */ + *ppOut = &p->aBuffer[iBuf]; + p->iReadOff += nByte; + }else{ + /* The requested data is not all available in the in-memory buffer. + ** In this case, allocate space at p->aAlloc[] to copy the requested + ** range into. Then return a copy of pointer p->aAlloc to the caller. */ + int nRem; /* Bytes remaining to copy */ + + /* Extend the p->aAlloc[] allocation if required. */ + if( p->nAllocnAlloc*2); + while( nByte>nNew ) nNew = nNew*2; + aNew = sqlite3Realloc(p->aAlloc, nNew); + if( !aNew ) return SQLITE_NOMEM_BKPT; + p->nAlloc = nNew; + p->aAlloc = aNew; + } + + /* Copy as much data as is available in the buffer into the start of + ** p->aAlloc[]. */ + memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail); + p->iReadOff += nAvail; + nRem = nByte - nAvail; + + /* The following loop copies up to p->nBuffer bytes per iteration into + ** the p->aAlloc[] buffer. */ + while( nRem>0 ){ + int rc; /* vdbePmaReadBlob() return code */ + int nCopy; /* Number of bytes to copy */ + u8 *aNext; /* Pointer to buffer to copy data from */ + + nCopy = nRem; + if( nRem>p->nBuffer ) nCopy = p->nBuffer; + rc = vdbePmaReadBlob(p, nCopy, &aNext); + if( rc!=SQLITE_OK ) return rc; + assert( aNext!=p->aAlloc ); + memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy); + nRem -= nCopy; + } + + *ppOut = p->aAlloc; + } + + return SQLITE_OK; +} + +/* +** Read a varint from the stream of data accessed by p. Set *pnOut to +** the value read. +*/ +static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){ + int iBuf; + + if( p->aMap ){ + p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut); + }else{ + iBuf = p->iReadOff % p->nBuffer; + if( iBuf && (p->nBuffer-iBuf)>=9 ){ + p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut); + }else{ + u8 aVarint[16], *a; + int i = 0, rc; + do{ + rc = vdbePmaReadBlob(p, 1, &a); + if( rc ) return rc; + aVarint[(i++)&0xf] = a[0]; + }while( (a[0]&0x80)!=0 ); + sqlite3GetVarint(aVarint, pnOut); + } + } + + return SQLITE_OK; +} + +/* +** Attempt to memory map file pFile. If successful, set *pp to point to the +** new mapping and return SQLITE_OK. If the mapping is not attempted +** (because the file is too large or the VFS layer is configured not to use +** mmap), return SQLITE_OK and set *pp to NULL. +** +** Or, if an error occurs, return an SQLite error code. The final value of +** *pp is undefined in this case. +*/ +static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){ + int rc = SQLITE_OK; + if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){ + sqlite3_file *pFd = pFile->pFd; + if( pFd->pMethods->iVersion>=3 ){ + rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp); + testcase( rc!=SQLITE_OK ); + } + } + return rc; +} + +/* +** Attach PmaReader pReadr to file pFile (if it is not already attached to +** that file) and seek it to offset iOff within the file. Return SQLITE_OK +** if successful, or an SQLite error code if an error occurs. +*/ +static int vdbePmaReaderSeek( + SortSubtask *pTask, /* Task context */ + PmaReader *pReadr, /* Reader whose cursor is to be moved */ + SorterFile *pFile, /* Sorter file to read from */ + i64 iOff /* Offset in pFile */ +){ + int rc = SQLITE_OK; + + assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 ); + + if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ; + if( pReadr->aMap ){ + sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap); + pReadr->aMap = 0; + } + pReadr->iReadOff = iOff; + pReadr->iEof = pFile->iEof; + pReadr->pFd = pFile->pFd; + + rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap); + if( rc==SQLITE_OK && pReadr->aMap==0 ){ + int pgsz = pTask->pSorter->pgsz; + int iBuf = pReadr->iReadOff % pgsz; + if( pReadr->aBuffer==0 ){ + pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz); + if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT; + pReadr->nBuffer = pgsz; + } + if( rc==SQLITE_OK && iBuf ){ + int nRead = pgsz - iBuf; + if( (pReadr->iReadOff + nRead) > pReadr->iEof ){ + nRead = (int)(pReadr->iEof - pReadr->iReadOff); + } + rc = sqlite3OsRead( + pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff + ); + testcase( rc!=SQLITE_OK ); + } + } + + return rc; +} + +/* +** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if +** no error occurs, or an SQLite error code if one does. +*/ +static int vdbePmaReaderNext(PmaReader *pReadr){ + int rc = SQLITE_OK; /* Return Code */ + u64 nRec = 0; /* Size of record in bytes */ + + + if( pReadr->iReadOff>=pReadr->iEof ){ + IncrMerger *pIncr = pReadr->pIncr; + int bEof = 1; + if( pIncr ){ + rc = vdbeIncrSwap(pIncr); + if( rc==SQLITE_OK && pIncr->bEof==0 ){ + rc = vdbePmaReaderSeek( + pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff + ); + bEof = 0; + } + } + + if( bEof ){ + /* This is an EOF condition */ + vdbePmaReaderClear(pReadr); + testcase( rc!=SQLITE_OK ); + return rc; + } + } + + if( rc==SQLITE_OK ){ + rc = vdbePmaReadVarint(pReadr, &nRec); + } + if( rc==SQLITE_OK ){ + pReadr->nKey = (int)nRec; + rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey); + testcase( rc!=SQLITE_OK ); + } + + return rc; +} + +/* +** Initialize PmaReader pReadr to scan through the PMA stored in file pFile +** starting at offset iStart and ending at offset iEof-1. This function +** leaves the PmaReader pointing to the first key in the PMA (or EOF if the +** PMA is empty). +** +** If the pnByte parameter is NULL, then it is assumed that the file +** contains a single PMA, and that that PMA omits the initial length varint. +*/ +static int vdbePmaReaderInit( + SortSubtask *pTask, /* Task context */ + SorterFile *pFile, /* Sorter file to read from */ + i64 iStart, /* Start offset in pFile */ + PmaReader *pReadr, /* PmaReader to populate */ + i64 *pnByte /* IN/OUT: Increment this value by PMA size */ +){ + int rc; + + assert( pFile->iEof>iStart ); + assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 ); + assert( pReadr->aBuffer==0 ); + assert( pReadr->aMap==0 ); + + rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart); + if( rc==SQLITE_OK ){ + u64 nByte = 0; /* Size of PMA in bytes */ + rc = vdbePmaReadVarint(pReadr, &nByte); + pReadr->iEof = pReadr->iReadOff + nByte; + *pnByte += nByte; + } + + if( rc==SQLITE_OK ){ + rc = vdbePmaReaderNext(pReadr); + } + return rc; +} + +/* +** A version of vdbeSorterCompare() that assumes that it has already been +** determined that the first field of key1 is equal to the first field of +** key2. +*/ +static int vdbeSorterCompareTail( + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ + const void *pKey1, int nKey1, /* Left side of comparison */ + const void *pKey2, int nKey2 /* Right side of comparison */ +){ + UnpackedRecord *r2 = pTask->pUnpacked; + if( *pbKey2Cached==0 ){ + sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); + *pbKey2Cached = 1; + } + return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1); +} + +/* +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, +** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences +** used by the comparison. Return the result of the comparison. +** +** If IN/OUT parameter *pbKey2Cached is true when this function is called, +** it is assumed that (pTask->pUnpacked) contains the unpacked version +** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked +** version of key2 and *pbKey2Cached set to true before returning. +** +** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set +** to SQLITE_NOMEM. +*/ +static int vdbeSorterCompare( + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ + const void *pKey1, int nKey1, /* Left side of comparison */ + const void *pKey2, int nKey2 /* Right side of comparison */ +){ + UnpackedRecord *r2 = pTask->pUnpacked; + if( !*pbKey2Cached ){ + sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); + *pbKey2Cached = 1; + } + return sqlite3VdbeRecordCompare(nKey1, pKey1, r2); +} + +/* +** A specially optimized version of vdbeSorterCompare() that assumes that +** the first field of each key is a TEXT value and that the collation +** sequence to compare them with is BINARY. +*/ +static int vdbeSorterCompareText( + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ + const void *pKey1, int nKey1, /* Left side of comparison */ + const void *pKey2, int nKey2 /* Right side of comparison */ +){ + const u8 * const p1 = (const u8 * const)pKey1; + const u8 * const p2 = (const u8 * const)pKey2; + const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ + const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ + + int n1; + int n2; + int res; + + getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2; + getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2; + res = memcmp(v1, v2, MIN(n1, n2)); + if( res==0 ){ + res = n1 - n2; + } + + if( res==0 ){ + if( pTask->pSorter->pKeyInfo->nField>1 ){ + res = vdbeSorterCompareTail( + pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 + ); + } + }else{ + if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ + res = res * -1; + } + } + + return res; +} + +/* +** A specially optimized version of vdbeSorterCompare() that assumes that +** the first field of each key is an INTEGER value. +*/ +static int vdbeSorterCompareInt( + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ + const void *pKey1, int nKey1, /* Left side of comparison */ + const void *pKey2, int nKey2 /* Right side of comparison */ +){ + const u8 * const p1 = (const u8 * const)pKey1; + const u8 * const p2 = (const u8 * const)pKey2; + const int s1 = p1[1]; /* Left hand serial type */ + const int s2 = p2[1]; /* Right hand serial type */ + const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ + const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ + int res; /* Return value */ + + assert( (s1>0 && s1<7) || s1==8 || s1==9 ); + assert( (s2>0 && s2<7) || s2==8 || s2==9 ); + + if( s1>7 && s2>7 ){ + res = s1 - s2; + }else{ + if( s1==s2 ){ + if( (*v1 ^ *v2) & 0x80 ){ + /* The two values have different signs */ + res = (*v1 & 0x80) ? -1 : +1; + }else{ + /* The two values have the same sign. Compare using memcmp(). */ + static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 }; + int i; + res = 0; + for(i=0; i7 ){ + res = +1; + }else if( s1>7 ){ + res = -1; + }else{ + res = s1 - s2; + } + assert( res!=0 ); + + if( res>0 ){ + if( *v1 & 0x80 ) res = -1; + }else{ + if( *v2 & 0x80 ) res = +1; + } + } + } + + if( res==0 ){ + if( pTask->pSorter->pKeyInfo->nField>1 ){ + res = vdbeSorterCompareTail( + pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 + ); + } + }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ + res = res * -1; + } + + return res; +} + +/* +** Initialize the temporary index cursor just opened as a sorter cursor. +** +** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField) +** to determine the number of fields that should be compared from the +** records being sorted. However, if the value passed as argument nField +** is non-zero and the sorter is able to guarantee a stable sort, nField +** is used instead. This is used when sorting records for a CREATE INDEX +** statement. In this case, keys are always delivered to the sorter in +** order of the primary key, which happens to be make up the final part +** of the records being sorted. So if the sort is stable, there is never +** any reason to compare PK fields and they can be ignored for a small +** performance boost. +** +** The sorter can guarantee a stable sort when running in single-threaded +** mode, but not in multi-threaded mode. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterInit( + sqlite3 *db, /* Database connection (for malloc()) */ + int nField, /* Number of key fields in each record */ + VdbeCursor *pCsr /* Cursor that holds the new sorter */ +){ + int pgsz; /* Page size of main database */ + int i; /* Used to iterate through aTask[] */ + VdbeSorter *pSorter; /* The new sorter */ + KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */ + int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */ + int sz; /* Size of pSorter in bytes */ + int rc = SQLITE_OK; +#if SQLITE_MAX_WORKER_THREADS==0 +# define nWorker 0 +#else + int nWorker; +#endif + + /* Initialize the upper limit on the number of worker threads */ +#if SQLITE_MAX_WORKER_THREADS>0 + if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){ + nWorker = 0; + }else{ + nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS]; + } +#endif + + /* Do not allow the total number of threads (main thread + all workers) + ** to exceed the maximum merge count */ +#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT + if( nWorker>=SORTER_MAX_MERGE_COUNT ){ + nWorker = SORTER_MAX_MERGE_COUNT-1; + } +#endif + + assert( pCsr->pKeyInfo && pCsr->pBt==0 ); + assert( pCsr->eCurType==CURTYPE_SORTER ); + szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*); + sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask); + + pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo); + pCsr->uc.pSorter = pSorter; + if( pSorter==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz); + memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo); + pKeyInfo->db = 0; + if( nField && nWorker==0 ){ + pKeyInfo->nXField += (pKeyInfo->nField - nField); + pKeyInfo->nField = nField; + } + pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt); + pSorter->nTask = nWorker + 1; + pSorter->iPrev = (u8)(nWorker - 1); + pSorter->bUseThreads = (pSorter->nTask>1); + pSorter->db = db; + for(i=0; inTask; i++){ + SortSubtask *pTask = &pSorter->aTask[i]; + pTask->pSorter = pSorter; + } + + if( !sqlite3TempInMemory(db) ){ + i64 mxCache; /* Cache size in bytes*/ + u32 szPma = sqlite3GlobalConfig.szPma; + pSorter->mnPmaSize = szPma * pgsz; + + mxCache = db->aDb[0].pSchema->cache_size; + if( mxCache<0 ){ + /* A negative cache-size value C indicates that the cache is abs(C) + ** KiB in size. */ + mxCache = mxCache * -1024; + }else{ + mxCache = mxCache * pgsz; + } + mxCache = MIN(mxCache, SQLITE_MAX_PMASZ); + pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache); + + /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of + ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary + ** large heap allocations. + */ + if( sqlite3GlobalConfig.pScratch==0 ){ + assert( pSorter->iMemory==0 ); + pSorter->nMemory = pgsz; + pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); + if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT; + } + } + + if( (pKeyInfo->nField+pKeyInfo->nXField)<13 + && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl) + ){ + pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT; + } + } + + return rc; +} +#undef nWorker /* Defined at the top of this function */ + +/* +** Free the list of sorted records starting at pRecord. +*/ +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){ + SorterRecord *p; + SorterRecord *pNext; + for(p=pRecord; p; p=pNext){ + pNext = p->u.pNext; + sqlite3DbFree(db, p); + } +} + +/* +** Free all resources owned by the object indicated by argument pTask. All +** fields of *pTask are zeroed before returning. +*/ +static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){ + sqlite3DbFree(db, pTask->pUnpacked); +#if SQLITE_MAX_WORKER_THREADS>0 + /* pTask->list.aMemory can only be non-zero if it was handed memory + ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */ + if( pTask->list.aMemory ){ + sqlite3_free(pTask->list.aMemory); + }else +#endif + { + assert( pTask->list.aMemory==0 ); + vdbeSorterRecordFree(0, pTask->list.pList); + } + if( pTask->file.pFd ){ + sqlite3OsCloseFree(pTask->file.pFd); + } + if( pTask->file2.pFd ){ + sqlite3OsCloseFree(pTask->file2.pFd); + } + memset(pTask, 0, sizeof(SortSubtask)); +} + +#ifdef SQLITE_DEBUG_SORTER_THREADS +static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){ + i64 t; + int iTask = (pTask - pTask->pSorter->aTask); + sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); + fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent); +} +static void vdbeSorterRewindDebug(const char *zEvent){ + i64 t; + sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t); + fprintf(stderr, "%lld:X %s\n", t, zEvent); +} +static void vdbeSorterPopulateDebug( + SortSubtask *pTask, + const char *zEvent +){ + i64 t; + int iTask = (pTask - pTask->pSorter->aTask); + sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); + fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent); +} +static void vdbeSorterBlockDebug( + SortSubtask *pTask, + int bBlocked, + const char *zEvent +){ + if( bBlocked ){ + i64 t; + sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); + fprintf(stderr, "%lld:main %s\n", t, zEvent); + } +} +#else +# define vdbeSorterWorkDebug(x,y) +# define vdbeSorterRewindDebug(y) +# define vdbeSorterPopulateDebug(x,y) +# define vdbeSorterBlockDebug(x,y,z) +#endif + +#if SQLITE_MAX_WORKER_THREADS>0 +/* +** Join thread pTask->thread. +*/ +static int vdbeSorterJoinThread(SortSubtask *pTask){ + int rc = SQLITE_OK; + if( pTask->pThread ){ +#ifdef SQLITE_DEBUG_SORTER_THREADS + int bDone = pTask->bDone; +#endif + void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR); + vdbeSorterBlockDebug(pTask, !bDone, "enter"); + (void)sqlite3ThreadJoin(pTask->pThread, &pRet); + vdbeSorterBlockDebug(pTask, !bDone, "exit"); + rc = SQLITE_PTR_TO_INT(pRet); + assert( pTask->bDone==1 ); + pTask->bDone = 0; + pTask->pThread = 0; + } + return rc; +} + +/* +** Launch a background thread to run xTask(pIn). +*/ +static int vdbeSorterCreateThread( + SortSubtask *pTask, /* Thread will use this task object */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + assert( pTask->pThread==0 && pTask->bDone==0 ); + return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn); +} + +/* +** Join all outstanding threads launched by SorterWrite() to create +** level-0 PMAs. +*/ +static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){ + int rc = rcin; + int i; + + /* This function is always called by the main user thread. + ** + ** If this function is being called after SorterRewind() has been called, + ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread + ** is currently attempt to join one of the other threads. To avoid a race + ** condition where this thread also attempts to join the same object, join + ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */ + for(i=pSorter->nTask-1; i>=0; i--){ + SortSubtask *pTask = &pSorter->aTask[i]; + int rc2 = vdbeSorterJoinThread(pTask); + if( rc==SQLITE_OK ) rc = rc2; + } + return rc; +} +#else +# define vdbeSorterJoinAll(x,rcin) (rcin) +# define vdbeSorterJoinThread(pTask) SQLITE_OK +#endif + +/* +** Allocate a new MergeEngine object capable of handling up to +** nReader PmaReader inputs. +** +** nReader is automatically rounded up to the next power of two. +** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up. +*/ +static MergeEngine *vdbeMergeEngineNew(int nReader){ + int N = 2; /* Smallest power of two >= nReader */ + int nByte; /* Total bytes of space to allocate */ + MergeEngine *pNew; /* Pointer to allocated object to return */ + + assert( nReader<=SORTER_MAX_MERGE_COUNT ); + + while( NnTree = N; + pNew->pTask = 0; + pNew->aReadr = (PmaReader*)&pNew[1]; + pNew->aTree = (int*)&pNew->aReadr[N]; + } + return pNew; +} + +/* +** Free the MergeEngine object passed as the only argument. +*/ +static void vdbeMergeEngineFree(MergeEngine *pMerger){ + int i; + if( pMerger ){ + for(i=0; inTree; i++){ + vdbePmaReaderClear(&pMerger->aReadr[i]); + } + } + sqlite3_free(pMerger); +} + +/* +** Free all resources associated with the IncrMerger object indicated by +** the first argument. +*/ +static void vdbeIncrFree(IncrMerger *pIncr){ + if( pIncr ){ +#if SQLITE_MAX_WORKER_THREADS>0 + if( pIncr->bUseThread ){ + vdbeSorterJoinThread(pIncr->pTask); + if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd); + if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd); + } +#endif + vdbeMergeEngineFree(pIncr->pMerger); + sqlite3_free(pIncr); + } +} + +/* +** Reset a sorting cursor back to its original empty state. +*/ +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){ + int i; + (void)vdbeSorterJoinAll(pSorter, SQLITE_OK); + assert( pSorter->bUseThreads || pSorter->pReader==0 ); +#if SQLITE_MAX_WORKER_THREADS>0 + if( pSorter->pReader ){ + vdbePmaReaderClear(pSorter->pReader); + sqlite3DbFree(db, pSorter->pReader); + pSorter->pReader = 0; + } +#endif + vdbeMergeEngineFree(pSorter->pMerger); + pSorter->pMerger = 0; + for(i=0; inTask; i++){ + SortSubtask *pTask = &pSorter->aTask[i]; + vdbeSortSubtaskCleanup(db, pTask); + pTask->pSorter = pSorter; + } + if( pSorter->list.aMemory==0 ){ + vdbeSorterRecordFree(0, pSorter->list.pList); + } + pSorter->list.pList = 0; + pSorter->list.szPMA = 0; + pSorter->bUsePMA = 0; + pSorter->iMemory = 0; + pSorter->mxKeysize = 0; + sqlite3DbFree(db, pSorter->pUnpacked); + pSorter->pUnpacked = 0; +} + +/* +** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. +*/ +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ + VdbeSorter *pSorter; + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + if( pSorter ){ + sqlite3VdbeSorterReset(db, pSorter); + sqlite3_free(pSorter->list.aMemory); + sqlite3DbFree(db, pSorter); + pCsr->uc.pSorter = 0; + } +} + +#if SQLITE_MAX_MMAP_SIZE>0 +/* +** The first argument is a file-handle open on a temporary file. The file +** is guaranteed to be nByte bytes or smaller in size. This function +** attempts to extend the file to nByte bytes in size and to ensure that +** the VFS has memory mapped it. +** +** Whether or not the file does end up memory mapped of course depends on +** the specific VFS implementation. +*/ +static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){ + if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){ + void *p = 0; + int chunksize = 4*1024; + sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize); + sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte); + sqlite3OsFetch(pFd, 0, (int)nByte, &p); + sqlite3OsUnfetch(pFd, 0, p); + } +} +#else +# define vdbeSorterExtendFile(x,y,z) +#endif + +/* +** Allocate space for a file-handle and open a temporary file. If successful, +** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK. +** Otherwise, set *ppFd to 0 and return an SQLite error code. +*/ +static int vdbeSorterOpenTempFile( + sqlite3 *db, /* Database handle doing sort */ + i64 nExtend, /* Attempt to extend file to this size */ + sqlite3_file **ppFd +){ + int rc; + if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS; + rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd, + SQLITE_OPEN_TEMP_JOURNAL | + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc + ); + if( rc==SQLITE_OK ){ + i64 max = SQLITE_MAX_MMAP_SIZE; + sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max); + if( nExtend>0 ){ + vdbeSorterExtendFile(db, *ppFd, nExtend); + } + } + return rc; +} + +/* +** If it has not already been allocated, allocate the UnpackedRecord +** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or +** if no allocation was required), or SQLITE_NOMEM otherwise. +*/ +static int vdbeSortAllocUnpacked(SortSubtask *pTask){ + if( pTask->pUnpacked==0 ){ + char *pFree; + pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord( + pTask->pSorter->pKeyInfo, 0, 0, &pFree + ); + assert( pTask->pUnpacked==(UnpackedRecord*)pFree ); + if( pFree==0 ) return SQLITE_NOMEM_BKPT; + pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField; + pTask->pUnpacked->errCode = 0; + } + return SQLITE_OK; +} + + +/* +** Merge the two sorted lists p1 and p2 into a single list. +** Set *ppOut to the head of the new list. +*/ +static void vdbeSorterMerge( + SortSubtask *pTask, /* Calling thread context */ + SorterRecord *p1, /* First list to merge */ + SorterRecord *p2, /* Second list to merge */ + SorterRecord **ppOut /* OUT: Head of merged list */ +){ + SorterRecord *pFinal = 0; + SorterRecord **pp = &pFinal; + int bCached = 0; + + while( p1 && p2 ){ + int res; + res = pTask->xCompare( + pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal + ); + + if( res<=0 ){ + *pp = p1; + pp = &p1->u.pNext; + p1 = p1->u.pNext; + }else{ + *pp = p2; + pp = &p2->u.pNext; + p2 = p2->u.pNext; + bCached = 0; + } + } + *pp = p1 ? p1 : p2; + *ppOut = pFinal; +} + +/* +** Return the SorterCompare function to compare values collected by the +** sorter object passed as the only argument. +*/ +static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){ + if( p->typeMask==SORTER_TYPE_INTEGER ){ + return vdbeSorterCompareInt; + }else if( p->typeMask==SORTER_TYPE_TEXT ){ + return vdbeSorterCompareText; + } + return vdbeSorterCompare; +} + +/* +** Sort the linked list of records headed at pTask->pList. Return +** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if +** an error occurs. +*/ +static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ + int i; + SorterRecord **aSlot; + SorterRecord *p; + int rc; + + rc = vdbeSortAllocUnpacked(pTask); + if( rc!=SQLITE_OK ) return rc; + + p = pList->pList; + pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter); + + aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); + if( !aSlot ){ + return SQLITE_NOMEM_BKPT; + } + + while( p ){ + SorterRecord *pNext; + if( pList->aMemory ){ + if( (u8*)p==pList->aMemory ){ + pNext = 0; + }else{ + assert( p->u.iNextaMemory) ); + pNext = (SorterRecord*)&pList->aMemory[p->u.iNext]; + } + }else{ + pNext = p->u.pNext; + } + + p->u.pNext = 0; + for(i=0; aSlot[i]; i++){ + vdbeSorterMerge(pTask, p, aSlot[i], &p); + aSlot[i] = 0; + } + aSlot[i] = p; + p = pNext; + } + + p = 0; + for(i=0; i<64; i++){ + vdbeSorterMerge(pTask, p, aSlot[i], &p); + } + pList->pList = p; + + sqlite3_free(aSlot); + assert( pTask->pUnpacked->errCode==SQLITE_OK + || pTask->pUnpacked->errCode==SQLITE_NOMEM + ); + return pTask->pUnpacked->errCode; +} + +/* +** Initialize a PMA-writer object. +*/ +static void vdbePmaWriterInit( + sqlite3_file *pFd, /* File handle to write to */ + PmaWriter *p, /* Object to populate */ + int nBuf, /* Buffer size */ + i64 iStart /* Offset of pFd to begin writing at */ +){ + memset(p, 0, sizeof(PmaWriter)); + p->aBuffer = (u8*)sqlite3Malloc(nBuf); + if( !p->aBuffer ){ + p->eFWErr = SQLITE_NOMEM_BKPT; + }else{ + p->iBufEnd = p->iBufStart = (iStart % nBuf); + p->iWriteOff = iStart - p->iBufStart; + p->nBuffer = nBuf; + p->pFd = pFd; + } +} + +/* +** Write nData bytes of data to the PMA. Return SQLITE_OK +** if successful, or an SQLite error code if an error occurs. +*/ +static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){ + int nRem = nData; + while( nRem>0 && p->eFWErr==0 ){ + int nCopy = nRem; + if( nCopy>(p->nBuffer - p->iBufEnd) ){ + nCopy = p->nBuffer - p->iBufEnd; + } + + memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy); + p->iBufEnd += nCopy; + if( p->iBufEnd==p->nBuffer ){ + p->eFWErr = sqlite3OsWrite(p->pFd, + &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, + p->iWriteOff + p->iBufStart + ); + p->iBufStart = p->iBufEnd = 0; + p->iWriteOff += p->nBuffer; + } + assert( p->iBufEndnBuffer ); + + nRem -= nCopy; + } +} + +/* +** Flush any buffered data to disk and clean up the PMA-writer object. +** The results of using the PMA-writer after this call are undefined. +** Return SQLITE_OK if flushing the buffered data succeeds or is not +** required. Otherwise, return an SQLite error code. +** +** Before returning, set *piEof to the offset immediately following the +** last byte written to the file. +*/ +static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){ + int rc; + if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){ + p->eFWErr = sqlite3OsWrite(p->pFd, + &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, + p->iWriteOff + p->iBufStart + ); + } + *piEof = (p->iWriteOff + p->iBufEnd); + sqlite3_free(p->aBuffer); + rc = p->eFWErr; + memset(p, 0, sizeof(PmaWriter)); + return rc; +} + +/* +** Write value iVal encoded as a varint to the PMA. Return +** SQLITE_OK if successful, or an SQLite error code if an error occurs. +*/ +static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){ + int nByte; + u8 aByte[10]; + nByte = sqlite3PutVarint(aByte, iVal); + vdbePmaWriteBlob(p, aByte, nByte); +} + +/* +** Write the current contents of in-memory linked-list pList to a level-0 +** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if +** successful, or an SQLite error code otherwise. +** +** The format of a PMA is: +** +** * A varint. This varint contains the total number of bytes of content +** in the PMA (not including the varint itself). +** +** * One or more records packed end-to-end in order of ascending keys. +** Each record consists of a varint followed by a blob of data (the +** key). The varint is the number of bytes in the blob of data. +*/ +static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){ + sqlite3 *db = pTask->pSorter->db; + int rc = SQLITE_OK; /* Return code */ + PmaWriter writer; /* Object used to write to the file */ + +#ifdef SQLITE_DEBUG + /* Set iSz to the expected size of file pTask->file after writing the PMA. + ** This is used by an assert() statement at the end of this function. */ + i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof; +#endif + + vdbeSorterWorkDebug(pTask, "enter"); + memset(&writer, 0, sizeof(PmaWriter)); + assert( pList->szPMA>0 ); + + /* If the first temporary PMA file has not been opened, open it now. */ + if( pTask->file.pFd==0 ){ + rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd); + assert( rc!=SQLITE_OK || pTask->file.pFd ); + assert( pTask->file.iEof==0 ); + assert( pTask->nPMA==0 ); + } + + /* Try to get the file to memory map */ + if( rc==SQLITE_OK ){ + vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9); + } + + /* Sort the list */ + if( rc==SQLITE_OK ){ + rc = vdbeSorterSort(pTask, pList); + } + + if( rc==SQLITE_OK ){ + SorterRecord *p; + SorterRecord *pNext = 0; + + vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz, + pTask->file.iEof); + pTask->nPMA++; + vdbePmaWriteVarint(&writer, pList->szPMA); + for(p=pList->pList; p; p=pNext){ + pNext = p->u.pNext; + vdbePmaWriteVarint(&writer, p->nVal); + vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal); + if( pList->aMemory==0 ) sqlite3_free(p); + } + pList->pList = p; + rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof); + } + + vdbeSorterWorkDebug(pTask, "exit"); + assert( rc!=SQLITE_OK || pList->pList==0 ); + assert( rc!=SQLITE_OK || pTask->file.iEof==iSz ); + return rc; +} + +/* +** Advance the MergeEngine to its next entry. +** Set *pbEof to true there is no next entry because +** the MergeEngine has reached the end of all its inputs. +** +** Return SQLITE_OK if successful or an error code if an error occurs. +*/ +static int vdbeMergeEngineStep( + MergeEngine *pMerger, /* The merge engine to advance to the next row */ + int *pbEof /* Set TRUE at EOF. Set false for more content */ +){ + int rc; + int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */ + SortSubtask *pTask = pMerger->pTask; + + /* Advance the current PmaReader */ + rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]); + + /* Update contents of aTree[] */ + if( rc==SQLITE_OK ){ + int i; /* Index of aTree[] to recalculate */ + PmaReader *pReadr1; /* First PmaReader to compare */ + PmaReader *pReadr2; /* Second PmaReader to compare */ + int bCached = 0; + + /* Find the first two PmaReaders to compare. The one that was just + ** advanced (iPrev) and the one next to it in the array. */ + pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)]; + pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)]; + + for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){ + /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */ + int iRes; + if( pReadr1->pFd==0 ){ + iRes = +1; + }else if( pReadr2->pFd==0 ){ + iRes = -1; + }else{ + iRes = pTask->xCompare(pTask, &bCached, + pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey + ); + } + + /* If pReadr1 contained the smaller value, set aTree[i] to its index. + ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this + ** case there is no cache of pReadr2 in pTask->pUnpacked, so set + ** pKey2 to point to the record belonging to pReadr2. + ** + ** Alternatively, if pReadr2 contains the smaller of the two values, + ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare() + ** was actually called above, then pTask->pUnpacked now contains + ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent + ** vdbeSorterCompare() from decoding pReadr2 again. + ** + ** If the two values were equal, then the value from the oldest + ** PMA should be considered smaller. The VdbeSorter.aReadr[] array + ** is sorted from oldest to newest, so pReadr1 contains older values + ** than pReadr2 iff (pReadr1aTree[i] = (int)(pReadr1 - pMerger->aReadr); + pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; + bCached = 0; + }else{ + if( pReadr1->pFd ) bCached = 0; + pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr); + pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; + } + } + *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0); + } + + return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc); +} + +#if SQLITE_MAX_WORKER_THREADS>0 +/* +** The main routine for background threads that write level-0 PMAs. +*/ +static void *vdbeSorterFlushThread(void *pCtx){ + SortSubtask *pTask = (SortSubtask*)pCtx; + int rc; /* Return code */ + assert( pTask->bDone==0 ); + rc = vdbeSorterListToPMA(pTask, &pTask->list); + pTask->bDone = 1; + return SQLITE_INT_TO_PTR(rc); +} +#endif /* SQLITE_MAX_WORKER_THREADS>0 */ + +/* +** Flush the current contents of VdbeSorter.list to a new PMA, possibly +** using a background thread. +*/ +static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ +#if SQLITE_MAX_WORKER_THREADS==0 + pSorter->bUsePMA = 1; + return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list); +#else + int rc = SQLITE_OK; + int i; + SortSubtask *pTask = 0; /* Thread context used to create new PMA */ + int nWorker = (pSorter->nTask-1); + + /* Set the flag to indicate that at least one PMA has been written. + ** Or will be, anyhow. */ + pSorter->bUsePMA = 1; + + /* Select a sub-task to sort and flush the current list of in-memory + ** records to disk. If the sorter is running in multi-threaded mode, + ** round-robin between the first (pSorter->nTask-1) tasks. Except, if + ** the background thread from a sub-tasks previous turn is still running, + ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy, + ** fall back to using the final sub-task. The first (pSorter->nTask-1) + ** sub-tasks are prefered as they use background threads - the final + ** sub-task uses the main thread. */ + for(i=0; iiPrev + i + 1) % nWorker; + pTask = &pSorter->aTask[iTest]; + if( pTask->bDone ){ + rc = vdbeSorterJoinThread(pTask); + } + if( rc!=SQLITE_OK || pTask->pThread==0 ) break; + } + + if( rc==SQLITE_OK ){ + if( i==nWorker ){ + /* Use the foreground thread for this operation */ + rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list); + }else{ + /* Launch a background thread for this operation */ + u8 *aMem = pTask->list.aMemory; + void *pCtx = (void*)pTask; + + assert( pTask->pThread==0 && pTask->bDone==0 ); + assert( pTask->list.pList==0 ); + assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 ); + + pSorter->iPrev = (u8)(pTask - pSorter->aTask); + pTask->list = pSorter->list; + pSorter->list.pList = 0; + pSorter->list.szPMA = 0; + if( aMem ){ + pSorter->list.aMemory = aMem; + pSorter->nMemory = sqlite3MallocSize(aMem); + }else if( pSorter->list.aMemory ){ + pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory); + if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT; + } + + rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx); + } + } + + return rc; +#endif /* SQLITE_MAX_WORKER_THREADS!=0 */ +} + +/* +** Add a record to the sorter. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterWrite( + const VdbeCursor *pCsr, /* Sorter cursor */ + Mem *pVal /* Memory cell containing record */ +){ + VdbeSorter *pSorter; + int rc = SQLITE_OK; /* Return Code */ + SorterRecord *pNew; /* New list element */ + int bFlush; /* True to flush contents of memory to PMA */ + int nReq; /* Bytes of memory required */ + int nPMA; /* Bytes of PMA space required */ + int t; /* serial type of first record field */ + + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + getVarint32((const u8*)&pVal->z[1], t); + if( t>0 && t<10 && t!=7 ){ + pSorter->typeMask &= SORTER_TYPE_INTEGER; + }else if( t>10 && (t & 0x01) ){ + pSorter->typeMask &= SORTER_TYPE_TEXT; + }else{ + pSorter->typeMask = 0; + } + + assert( pSorter ); + + /* Figure out whether or not the current contents of memory should be + ** flushed to a PMA before continuing. If so, do so. + ** + ** If using the single large allocation mode (pSorter->aMemory!=0), then + ** flush the contents of memory to a new PMA if (a) at least one value is + ** already in memory and (b) the new value will not fit in memory. + ** + ** Or, if using separate allocations for each record, flush the contents + ** of memory to a PMA if either of the following are true: + ** + ** * The total memory allocated for the in-memory list is greater + ** than (page-size * cache-size), or + ** + ** * The total memory allocated for the in-memory list is greater + ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true. + */ + nReq = pVal->n + sizeof(SorterRecord); + nPMA = pVal->n + sqlite3VarintLen(pVal->n); + if( pSorter->mxPmaSize ){ + if( pSorter->list.aMemory ){ + bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize; + }else{ + bFlush = ( + (pSorter->list.szPMA > pSorter->mxPmaSize) + || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull()) + ); + } + if( bFlush ){ + rc = vdbeSorterFlushPMA(pSorter); + pSorter->list.szPMA = 0; + pSorter->iMemory = 0; + assert( rc!=SQLITE_OK || pSorter->list.pList==0 ); + } + } + + pSorter->list.szPMA += nPMA; + if( nPMA>pSorter->mxKeysize ){ + pSorter->mxKeysize = nPMA; + } + + if( pSorter->list.aMemory ){ + int nMin = pSorter->iMemory + nReq; + + if( nMin>pSorter->nMemory ){ + u8 *aNew; + int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory; + int nNew = pSorter->nMemory * 2; + while( nNew < nMin ) nNew = nNew*2; + if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize; + if( nNew < nMin ) nNew = nMin; + + aNew = sqlite3Realloc(pSorter->list.aMemory, nNew); + if( !aNew ) return SQLITE_NOMEM_BKPT; + pSorter->list.pList = (SorterRecord*)&aNew[iListOff]; + pSorter->list.aMemory = aNew; + pSorter->nMemory = nNew; + } + + pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory]; + pSorter->iMemory += ROUND8(nReq); + if( pSorter->list.pList ){ + pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory); + } + }else{ + pNew = (SorterRecord *)sqlite3Malloc(nReq); + if( pNew==0 ){ + return SQLITE_NOMEM_BKPT; + } + pNew->u.pNext = pSorter->list.pList; + } + + memcpy(SRVAL(pNew), pVal->z, pVal->n); + pNew->nVal = pVal->n; + pSorter->list.pList = pNew; + + return rc; +} + +/* +** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format +** of the data stored in aFile[1] is the same as that used by regular PMAs, +** except that the number-of-bytes varint is omitted from the start. +*/ +static int vdbeIncrPopulate(IncrMerger *pIncr){ + int rc = SQLITE_OK; + int rc2; + i64 iStart = pIncr->iStartOff; + SorterFile *pOut = &pIncr->aFile[1]; + SortSubtask *pTask = pIncr->pTask; + MergeEngine *pMerger = pIncr->pMerger; + PmaWriter writer; + assert( pIncr->bEof==0 ); + + vdbeSorterPopulateDebug(pTask, "enter"); + + vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart); + while( rc==SQLITE_OK ){ + int dummy; + PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ]; + int nKey = pReader->nKey; + i64 iEof = writer.iWriteOff + writer.iBufEnd; + + /* Check if the output file is full or if the input has been exhausted. + ** In either case exit the loop. */ + if( pReader->pFd==0 ) break; + if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break; + + /* Write the next key to the output. */ + vdbePmaWriteVarint(&writer, nKey); + vdbePmaWriteBlob(&writer, pReader->aKey, nKey); + assert( pIncr->pMerger->pTask==pTask ); + rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy); + } + + rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof); + if( rc==SQLITE_OK ) rc = rc2; + vdbeSorterPopulateDebug(pTask, "exit"); + return rc; +} + +#if SQLITE_MAX_WORKER_THREADS>0 +/* +** The main routine for background threads that populate aFile[1] of +** multi-threaded IncrMerger objects. +*/ +static void *vdbeIncrPopulateThread(void *pCtx){ + IncrMerger *pIncr = (IncrMerger*)pCtx; + void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) ); + pIncr->pTask->bDone = 1; + return pRet; +} + +/* +** Launch a background thread to populate aFile[1] of pIncr. +*/ +static int vdbeIncrBgPopulate(IncrMerger *pIncr){ + void *p = (void*)pIncr; + assert( pIncr->bUseThread ); + return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p); +} +#endif + +/* +** This function is called when the PmaReader corresponding to pIncr has +** finished reading the contents of aFile[0]. Its purpose is to "refill" +** aFile[0] such that the PmaReader should start rereading it from the +** beginning. +** +** For single-threaded objects, this is accomplished by literally reading +** keys from pIncr->pMerger and repopulating aFile[0]. +** +** For multi-threaded objects, all that is required is to wait until the +** background thread is finished (if it is not already) and then swap +** aFile[0] and aFile[1] in place. If the contents of pMerger have not +** been exhausted, this function also launches a new background thread +** to populate the new aFile[1]. +** +** SQLITE_OK is returned on success, or an SQLite error code otherwise. +*/ +static int vdbeIncrSwap(IncrMerger *pIncr){ + int rc = SQLITE_OK; + +#if SQLITE_MAX_WORKER_THREADS>0 + if( pIncr->bUseThread ){ + rc = vdbeSorterJoinThread(pIncr->pTask); + + if( rc==SQLITE_OK ){ + SorterFile f0 = pIncr->aFile[0]; + pIncr->aFile[0] = pIncr->aFile[1]; + pIncr->aFile[1] = f0; + } + + if( rc==SQLITE_OK ){ + if( pIncr->aFile[0].iEof==pIncr->iStartOff ){ + pIncr->bEof = 1; + }else{ + rc = vdbeIncrBgPopulate(pIncr); + } + } + }else +#endif + { + rc = vdbeIncrPopulate(pIncr); + pIncr->aFile[0] = pIncr->aFile[1]; + if( pIncr->aFile[0].iEof==pIncr->iStartOff ){ + pIncr->bEof = 1; + } + } + + return rc; +} + +/* +** Allocate and return a new IncrMerger object to read data from pMerger. +** +** If an OOM condition is encountered, return NULL. In this case free the +** pMerger argument before returning. +*/ +static int vdbeIncrMergerNew( + SortSubtask *pTask, /* The thread that will be using the new IncrMerger */ + MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */ + IncrMerger **ppOut /* Write the new IncrMerger here */ +){ + int rc = SQLITE_OK; + IncrMerger *pIncr = *ppOut = (IncrMerger*) + (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr))); + if( pIncr ){ + pIncr->pMerger = pMerger; + pIncr->pTask = pTask; + pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2); + pTask->file2.iEof += pIncr->mxSz; + }else{ + vdbeMergeEngineFree(pMerger); + rc = SQLITE_NOMEM_BKPT; + } + return rc; +} + +#if SQLITE_MAX_WORKER_THREADS>0 +/* +** Set the "use-threads" flag on object pIncr. +*/ +static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){ + pIncr->bUseThread = 1; + pIncr->pTask->file2.iEof -= pIncr->mxSz; +} +#endif /* SQLITE_MAX_WORKER_THREADS>0 */ + + + +/* +** Recompute pMerger->aTree[iOut] by comparing the next keys on the +** two PmaReaders that feed that entry. Neither of the PmaReaders +** are advanced. This routine merely does the comparison. +*/ +static void vdbeMergeEngineCompare( + MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */ + int iOut /* Store the result in pMerger->aTree[iOut] */ +){ + int i1; + int i2; + int iRes; + PmaReader *p1; + PmaReader *p2; + + assert( iOutnTree && iOut>0 ); + + if( iOut>=(pMerger->nTree/2) ){ + i1 = (iOut - pMerger->nTree/2) * 2; + i2 = i1 + 1; + }else{ + i1 = pMerger->aTree[iOut*2]; + i2 = pMerger->aTree[iOut*2+1]; + } + + p1 = &pMerger->aReadr[i1]; + p2 = &pMerger->aReadr[i2]; + + if( p1->pFd==0 ){ + iRes = i2; + }else if( p2->pFd==0 ){ + iRes = i1; + }else{ + SortSubtask *pTask = pMerger->pTask; + int bCached = 0; + int res; + assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */ + res = pTask->xCompare( + pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey + ); + if( res<=0 ){ + iRes = i1; + }else{ + iRes = i2; + } + } + + pMerger->aTree[iOut] = iRes; +} + +/* +** Allowed values for the eMode parameter to vdbeMergeEngineInit() +** and vdbePmaReaderIncrMergeInit(). +** +** Only INCRINIT_NORMAL is valid in single-threaded builds (when +** SQLITE_MAX_WORKER_THREADS==0). The other values are only used +** when there exists one or more separate worker threads. +*/ +#define INCRINIT_NORMAL 0 +#define INCRINIT_TASK 1 +#define INCRINIT_ROOT 2 + +/* +** Forward reference required as the vdbeIncrMergeInit() and +** vdbePmaReaderIncrInit() routines are called mutually recursively when +** building a merge tree. +*/ +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode); + +/* +** Initialize the MergeEngine object passed as the second argument. Once this +** function returns, the first key of merged data may be read from the +** MergeEngine object in the usual fashion. +** +** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge +** objects attached to the PmaReader objects that the merger reads from have +** already been populated, but that they have not yet populated aFile[0] and +** set the PmaReader objects up to read from it. In this case all that is +** required is to call vdbePmaReaderNext() on each PmaReader to point it at +** its first key. +** +** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use +** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data +** to pMerger. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +static int vdbeMergeEngineInit( + SortSubtask *pTask, /* Thread that will run pMerger */ + MergeEngine *pMerger, /* MergeEngine to initialize */ + int eMode /* One of the INCRINIT_XXX constants */ +){ + int rc = SQLITE_OK; /* Return code */ + int i; /* For looping over PmaReader objects */ + int nTree = pMerger->nTree; + + /* eMode is always INCRINIT_NORMAL in single-threaded mode */ + assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); + + /* Verify that the MergeEngine is assigned to a single thread */ + assert( pMerger->pTask==0 ); + pMerger->pTask = pTask; + + for(i=0; i0 && eMode==INCRINIT_ROOT ){ + /* PmaReaders should be normally initialized in order, as if they are + ** reading from the same temp file this makes for more linear file IO. + ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is + ** in use it will block the vdbePmaReaderNext() call while it uses + ** the main thread to fill its buffer. So calling PmaReaderNext() + ** on this PmaReader before any of the multi-threaded PmaReaders takes + ** better advantage of multi-processor hardware. */ + rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]); + }else{ + rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL); + } + if( rc!=SQLITE_OK ) return rc; + } + + for(i=pMerger->nTree-1; i>0; i--){ + vdbeMergeEngineCompare(pMerger, i); + } + return pTask->pUnpacked->errCode; +} + +/* +** The PmaReader passed as the first argument is guaranteed to be an +** incremental-reader (pReadr->pIncr!=0). This function serves to open +** and/or initialize the temp file related fields of the IncrMerge +** object at (pReadr->pIncr). +** +** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders +** in the sub-tree headed by pReadr are also initialized. Data is then +** loaded into the buffers belonging to pReadr and it is set to point to +** the first key in its range. +** +** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed +** to be a multi-threaded PmaReader and this function is being called in a +** background thread. In this case all PmaReaders in the sub-tree are +** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to +** pReadr is populated. However, pReadr itself is not set up to point +** to its first key. A call to vdbePmaReaderNext() is still required to do +** that. +** +** The reason this function does not call vdbePmaReaderNext() immediately +** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has +** to block on thread (pTask->thread) before accessing aFile[1]. But, since +** this entire function is being run by thread (pTask->thread), that will +** lead to the current background thread attempting to join itself. +** +** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed +** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all +** child-trees have already been initialized using IncrInit(INCRINIT_TASK). +** In this case vdbePmaReaderNext() is called on all child PmaReaders and +** the current PmaReader set to point to the first key in its range. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ + int rc = SQLITE_OK; + IncrMerger *pIncr = pReadr->pIncr; + SortSubtask *pTask = pIncr->pTask; + sqlite3 *db = pTask->pSorter->db; + + /* eMode is always INCRINIT_NORMAL in single-threaded mode */ + assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); + + rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode); + + /* Set up the required files for pIncr. A multi-theaded IncrMerge object + ** requires two temp files to itself, whereas a single-threaded object + ** only requires a region of pTask->file2. */ + if( rc==SQLITE_OK ){ + int mxSz = pIncr->mxSz; +#if SQLITE_MAX_WORKER_THREADS>0 + if( pIncr->bUseThread ){ + rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd); + if( rc==SQLITE_OK ){ + rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd); + } + }else +#endif + /*if( !pIncr->bUseThread )*/{ + if( pTask->file2.pFd==0 ){ + assert( pTask->file2.iEof>0 ); + rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd); + pTask->file2.iEof = 0; + } + if( rc==SQLITE_OK ){ + pIncr->aFile[1].pFd = pTask->file2.pFd; + pIncr->iStartOff = pTask->file2.iEof; + pTask->file2.iEof += mxSz; + } + } + } + +#if SQLITE_MAX_WORKER_THREADS>0 + if( rc==SQLITE_OK && pIncr->bUseThread ){ + /* Use the current thread to populate aFile[1], even though this + ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object, + ** then this function is already running in background thread + ** pIncr->pTask->thread. + ** + ** If this is the INCRINIT_ROOT object, then it is running in the + ** main VDBE thread. But that is Ok, as that thread cannot return + ** control to the VDBE or proceed with anything useful until the + ** first results are ready from this merger object anyway. + */ + assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK ); + rc = vdbeIncrPopulate(pIncr); + } +#endif + + if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){ + rc = vdbePmaReaderNext(pReadr); + } + + return rc; +} + +#if SQLITE_MAX_WORKER_THREADS>0 +/* +** The main routine for vdbePmaReaderIncrMergeInit() operations run in +** background threads. +*/ +static void *vdbePmaReaderBgIncrInit(void *pCtx){ + PmaReader *pReader = (PmaReader*)pCtx; + void *pRet = SQLITE_INT_TO_PTR( + vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK) + ); + pReader->pIncr->pTask->bDone = 1; + return pRet; +} +#endif + +/* +** If the PmaReader passed as the first argument is not an incremental-reader +** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes +** the vdbePmaReaderIncrMergeInit() function with the parameters passed to +** this routine to initialize the incremental merge. +** +** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), +** then a background thread is launched to call vdbePmaReaderIncrMergeInit(). +** Or, if the IncrMerger is single threaded, the same function is called +** using the current thread. +*/ +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){ + IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */ + int rc = SQLITE_OK; /* Return code */ + if( pIncr ){ +#if SQLITE_MAX_WORKER_THREADS>0 + assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK ); + if( pIncr->bUseThread ){ + void *pCtx = (void*)pReadr; + rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx); + }else +#endif + { + rc = vdbePmaReaderIncrMergeInit(pReadr, eMode); + } + } + return rc; +} + +/* +** Allocate a new MergeEngine object to merge the contents of nPMA level-0 +** PMAs from pTask->file. If no error occurs, set *ppOut to point to +** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut +** to NULL and return an SQLite error code. +** +** When this function is called, *piOffset is set to the offset of the +** first PMA to read from pTask->file. Assuming no error occurs, it is +** set to the offset immediately following the last byte of the last +** PMA before returning. If an error does occur, then the final value of +** *piOffset is undefined. +*/ +static int vdbeMergeEngineLevel0( + SortSubtask *pTask, /* Sorter task to read from */ + int nPMA, /* Number of PMAs to read */ + i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */ + MergeEngine **ppOut /* OUT: New merge-engine */ +){ + MergeEngine *pNew; /* Merge engine to return */ + i64 iOff = *piOffset; + int i; + int rc = SQLITE_OK; + + *ppOut = pNew = vdbeMergeEngineNew(nPMA); + if( pNew==0 ) rc = SQLITE_NOMEM_BKPT; + + for(i=0; iaReadr[i]; + rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy); + iOff = pReadr->iEof; + } + + if( rc!=SQLITE_OK ){ + vdbeMergeEngineFree(pNew); + *ppOut = 0; + } + *piOffset = iOff; + return rc; +} + +/* +** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of +** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes. +** +** i.e. +** +** nPMA<=16 -> TreeDepth() == 0 +** nPMA<=256 -> TreeDepth() == 1 +** nPMA<=65536 -> TreeDepth() == 2 +*/ +static int vdbeSorterTreeDepth(int nPMA){ + int nDepth = 0; + i64 nDiv = SORTER_MAX_MERGE_COUNT; + while( nDiv < (i64)nPMA ){ + nDiv = nDiv * SORTER_MAX_MERGE_COUNT; + nDepth++; + } + return nDepth; +} + +/* +** pRoot is the root of an incremental merge-tree with depth nDepth (according +** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the +** tree, counting from zero. This function adds pLeaf to the tree. +** +** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error +** code is returned and pLeaf is freed. +*/ +static int vdbeSorterAddToTree( + SortSubtask *pTask, /* Task context */ + int nDepth, /* Depth of tree according to TreeDepth() */ + int iSeq, /* Sequence number of leaf within tree */ + MergeEngine *pRoot, /* Root of tree */ + MergeEngine *pLeaf /* Leaf to add to tree */ +){ + int rc = SQLITE_OK; + int nDiv = 1; + int i; + MergeEngine *p = pRoot; + IncrMerger *pIncr; + + rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr); + + for(i=1; iaReadr[iIter]; + + if( pReadr->pIncr==0 ){ + MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); + if( pNew==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr); + } + } + if( rc==SQLITE_OK ){ + p = pReadr->pIncr->pMerger; + nDiv = nDiv / SORTER_MAX_MERGE_COUNT; + } + } + + if( rc==SQLITE_OK ){ + p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr; + }else{ + vdbeIncrFree(pIncr); + } + return rc; +} + +/* +** This function is called as part of a SorterRewind() operation on a sorter +** that has already written two or more level-0 PMAs to one or more temp +** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that +** can be used to incrementally merge all PMAs on disk. +** +** If successful, SQLITE_OK is returned and *ppOut set to point to the +** MergeEngine object at the root of the tree before returning. Or, if an +** error occurs, an SQLite error code is returned and the final value +** of *ppOut is undefined. +*/ +static int vdbeSorterMergeTreeBuild( + VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */ + MergeEngine **ppOut /* Write the MergeEngine here */ +){ + MergeEngine *pMain = 0; + int rc = SQLITE_OK; + int iTask; + +#if SQLITE_MAX_WORKER_THREADS>0 + /* If the sorter uses more than one task, then create the top-level + ** MergeEngine here. This MergeEngine will read data from exactly + ** one PmaReader per sub-task. */ + assert( pSorter->bUseThreads || pSorter->nTask==1 ); + if( pSorter->nTask>1 ){ + pMain = vdbeMergeEngineNew(pSorter->nTask); + if( pMain==0 ) rc = SQLITE_NOMEM_BKPT; + } +#endif + + for(iTask=0; rc==SQLITE_OK && iTasknTask; iTask++){ + SortSubtask *pTask = &pSorter->aTask[iTask]; + assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 ); + if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){ + MergeEngine *pRoot = 0; /* Root node of tree for this task */ + int nDepth = vdbeSorterTreeDepth(pTask->nPMA); + i64 iReadOff = 0; + + if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){ + rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot); + }else{ + int i; + int iSeq = 0; + pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); + if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT; + for(i=0; inPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){ + MergeEngine *pMerger = 0; /* New level-0 PMA merger */ + int nReader; /* Number of level-0 PMAs to merge */ + + nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT); + rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger); + if( rc==SQLITE_OK ){ + rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger); + } + } + } + + if( rc==SQLITE_OK ){ +#if SQLITE_MAX_WORKER_THREADS>0 + if( pMain!=0 ){ + rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr); + }else +#endif + { + assert( pMain==0 ); + pMain = pRoot; + } + }else{ + vdbeMergeEngineFree(pRoot); + } + } + } + + if( rc!=SQLITE_OK ){ + vdbeMergeEngineFree(pMain); + pMain = 0; + } + *ppOut = pMain; + return rc; +} + +/* +** This function is called as part of an sqlite3VdbeSorterRewind() operation +** on a sorter that has written two or more PMAs to temporary files. It sets +** up either VdbeSorter.pMerger (for single threaded sorters) or pReader +** (for multi-threaded sorters) so that it can be used to iterate through +** all records stored in the sorter. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +static int vdbeSorterSetupMerge(VdbeSorter *pSorter){ + int rc; /* Return code */ + SortSubtask *pTask0 = &pSorter->aTask[0]; + MergeEngine *pMain = 0; +#if SQLITE_MAX_WORKER_THREADS + sqlite3 *db = pTask0->pSorter->db; + int i; + SorterCompare xCompare = vdbeSorterGetCompare(pSorter); + for(i=0; inTask; i++){ + pSorter->aTask[i].xCompare = xCompare; + } +#endif + + rc = vdbeSorterMergeTreeBuild(pSorter, &pMain); + if( rc==SQLITE_OK ){ +#if SQLITE_MAX_WORKER_THREADS + assert( pSorter->bUseThreads==0 || pSorter->nTask>1 ); + if( pSorter->bUseThreads ){ + int iTask; + PmaReader *pReadr = 0; + SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1]; + rc = vdbeSortAllocUnpacked(pLast); + if( rc==SQLITE_OK ){ + pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader)); + pSorter->pReader = pReadr; + if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT; + } + if( rc==SQLITE_OK ){ + rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr); + if( rc==SQLITE_OK ){ + vdbeIncrMergerSetThreads(pReadr->pIncr); + for(iTask=0; iTask<(pSorter->nTask-1); iTask++){ + IncrMerger *pIncr; + if( (pIncr = pMain->aReadr[iTask].pIncr) ){ + vdbeIncrMergerSetThreads(pIncr); + assert( pIncr->pTask!=pLast ); + } + } + for(iTask=0; rc==SQLITE_OK && iTasknTask; iTask++){ + /* Check that: + ** + ** a) The incremental merge object is configured to use the + ** right task, and + ** b) If it is using task (nTask-1), it is configured to run + ** in single-threaded mode. This is important, as the + ** root merge (INCRINIT_ROOT) will be using the same task + ** object. + */ + PmaReader *p = &pMain->aReadr[iTask]; + assert( p->pIncr==0 || ( + (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */ + && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */ + )); + rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK); + } + } + pMain = 0; + } + if( rc==SQLITE_OK ){ + rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT); + } + }else +#endif + { + rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL); + pSorter->pMerger = pMain; + pMain = 0; + } + } + + if( rc!=SQLITE_OK ){ + vdbeMergeEngineFree(pMain); + } + return rc; +} + + +/* +** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite, +** this function is called to prepare for iterating through the records +** in sorted order. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){ + VdbeSorter *pSorter; + int rc = SQLITE_OK; /* Return code */ + + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + assert( pSorter ); + + /* If no data has been written to disk, then do not do so now. Instead, + ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly + ** from the in-memory list. */ + if( pSorter->bUsePMA==0 ){ + if( pSorter->list.pList ){ + *pbEof = 0; + rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list); + }else{ + *pbEof = 1; + } + return rc; + } + + /* Write the current in-memory list to a PMA. When the VdbeSorterWrite() + ** function flushes the contents of memory to disk, it immediately always + ** creates a new list consisting of a single key immediately afterwards. + ** So the list is never empty at this point. */ + assert( pSorter->list.pList ); + rc = vdbeSorterFlushPMA(pSorter); + + /* Join all threads */ + rc = vdbeSorterJoinAll(pSorter, rc); + + vdbeSorterRewindDebug("rewind"); + + /* Assuming no errors have occurred, set up a merger structure to + ** incrementally read and merge all remaining PMAs. */ + assert( pSorter->pReader==0 ); + if( rc==SQLITE_OK ){ + rc = vdbeSorterSetupMerge(pSorter); + *pbEof = 0; + } + + vdbeSorterRewindDebug("rewinddone"); + return rc; +} + +/* +** Advance to the next element in the sorter. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){ + VdbeSorter *pSorter; + int rc; /* Return code */ + + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) ); + if( pSorter->bUsePMA ){ + assert( pSorter->pReader==0 || pSorter->pMerger==0 ); + assert( pSorter->bUseThreads==0 || pSorter->pReader ); + assert( pSorter->bUseThreads==1 || pSorter->pMerger ); +#if SQLITE_MAX_WORKER_THREADS>0 + if( pSorter->bUseThreads ){ + rc = vdbePmaReaderNext(pSorter->pReader); + *pbEof = (pSorter->pReader->pFd==0); + }else +#endif + /*if( !pSorter->bUseThreads )*/ { + assert( pSorter->pMerger!=0 ); + assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) ); + rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof); + } + }else{ + SorterRecord *pFree = pSorter->list.pList; + pSorter->list.pList = pFree->u.pNext; + pFree->u.pNext = 0; + if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree); + *pbEof = !pSorter->list.pList; + rc = SQLITE_OK; + } + return rc; +} + +/* +** Return a pointer to a buffer owned by the sorter that contains the +** current key. +*/ +static void *vdbeSorterRowkey( + const VdbeSorter *pSorter, /* Sorter object */ + int *pnKey /* OUT: Size of current key in bytes */ +){ + void *pKey; + if( pSorter->bUsePMA ){ + PmaReader *pReader; +#if SQLITE_MAX_WORKER_THREADS>0 + if( pSorter->bUseThreads ){ + pReader = pSorter->pReader; + }else +#endif + /*if( !pSorter->bUseThreads )*/{ + pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]]; + } + *pnKey = pReader->nKey; + pKey = pReader->aKey; + }else{ + *pnKey = pSorter->list.pList->nVal; + pKey = SRVAL(pSorter->list.pList); + } + return pKey; +} + +/* +** Copy the current sorter key into the memory cell pOut. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){ + VdbeSorter *pSorter; + void *pKey; int nKey; /* Sorter key to copy into pOut */ + + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + pKey = vdbeSorterRowkey(pSorter, &nKey); + if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){ + return SQLITE_NOMEM_BKPT; + } + pOut->n = nKey; + MemSetTypeFlag(pOut, MEM_Blob); + memcpy(pOut->z, pKey, nKey); + + return SQLITE_OK; +} + +/* +** Compare the key in memory cell pVal with the key that the sorter cursor +** passed as the first argument currently points to. For the purposes of +** the comparison, ignore the rowid field at the end of each record. +** +** If the sorter cursor key contains any NULL values, consider it to be +** less than pVal. Even if pVal also contains NULL values. +** +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM). +** Otherwise, set *pRes to a negative, zero or positive value if the +** key in pVal is smaller than, equal to or larger than the current sorter +** key. +** +** This routine forms the core of the OP_SorterCompare opcode, which in +** turn is used to verify uniqueness when constructing a UNIQUE INDEX. +*/ +SQLITE_PRIVATE int sqlite3VdbeSorterCompare( + const VdbeCursor *pCsr, /* Sorter cursor */ + Mem *pVal, /* Value to compare to current sorter key */ + int nKeyCol, /* Compare this many columns */ + int *pRes /* OUT: Result of comparison */ +){ + VdbeSorter *pSorter; + UnpackedRecord *r2; + KeyInfo *pKeyInfo; + int i; + void *pKey; int nKey; /* Sorter key to compare pVal with */ + + assert( pCsr->eCurType==CURTYPE_SORTER ); + pSorter = pCsr->uc.pSorter; + r2 = pSorter->pUnpacked; + pKeyInfo = pCsr->pKeyInfo; + if( r2==0 ){ + char *p; + r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p); + assert( pSorter->pUnpacked==(UnpackedRecord*)p ); + if( r2==0 ) return SQLITE_NOMEM_BKPT; + r2->nField = nKeyCol; + } + assert( r2->nField==nKeyCol ); + + pKey = vdbeSorterRowkey(pSorter, &nKey); + sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2); + for(i=0; iaMem[i].flags & MEM_Null ){ + *pRes = -1; + return SQLITE_OK; + } + } + + *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2); + return SQLITE_OK; +} + +/************** End of vdbesort.c ********************************************/ +/************** Begin file memjournal.c **************************************/ +/* +** 2008 October 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code use to implement an in-memory rollback journal. +** The in-memory rollback journal is used to journal transactions for +** ":memory:" databases and when the journal_mode=MEMORY pragma is used. +** +** Update: The in-memory journal is also used to temporarily cache +** smaller journals that are not critical for power-loss recovery. +** For example, statement journals that are not too big will be held +** entirely in memory, thus reducing the number of file I/O calls, and +** more importantly, reducing temporary file creation events. If these +** journals become too large for memory, they are spilled to disk. But +** in the common case, they are usually small and no file I/O needs to +** occur. +*/ +/* #include "sqliteInt.h" */ + +/* Forward references to internal structures */ +typedef struct MemJournal MemJournal; +typedef struct FilePoint FilePoint; +typedef struct FileChunk FileChunk; + +/* +** The rollback journal is composed of a linked list of these structures. +** +** The zChunk array is always at least 8 bytes in size - usually much more. +** Its actual size is stored in the MemJournal.nChunkSize variable. +*/ +struct FileChunk { + FileChunk *pNext; /* Next chunk in the journal */ + u8 zChunk[8]; /* Content of this chunk */ +}; + +/* +** By default, allocate this many bytes of memory for each FileChunk object. +*/ +#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024 + +/* +** For chunk size nChunkSize, return the number of bytes that should +** be allocated for each FileChunk structure. +*/ +#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8)) + +/* +** An instance of this object serves as a cursor into the rollback journal. +** The cursor can be either for reading or writing. +*/ +struct FilePoint { + sqlite3_int64 iOffset; /* Offset from the beginning of the file */ + FileChunk *pChunk; /* Specific chunk into which cursor points */ +}; + +/* +** This structure is a subclass of sqlite3_file. Each open memory-journal +** is an instance of this class. +*/ +struct MemJournal { + const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ + int nChunkSize; /* In-memory chunk-size */ + + int nSpill; /* Bytes of data before flushing */ + int nSize; /* Bytes of data currently in memory */ + FileChunk *pFirst; /* Head of in-memory chunk-list */ + FilePoint endpoint; /* Pointer to the end of the file */ + FilePoint readpoint; /* Pointer to the end of the last xRead() */ + + int flags; /* xOpen flags */ + sqlite3_vfs *pVfs; /* The "real" underlying VFS */ + const char *zJournal; /* Name of the journal file */ +}; + +/* +** Read data from the in-memory journal file. This is the implementation +** of the sqlite3_vfs.xRead method. +*/ +static int memjrnlRead( + sqlite3_file *pJfd, /* The journal file from which to read */ + void *zBuf, /* Put the results here */ + int iAmt, /* Number of bytes to read */ + sqlite_int64 iOfst /* Begin reading at this offset */ +){ + MemJournal *p = (MemJournal *)pJfd; + u8 *zOut = zBuf; + int nRead = iAmt; + int iChunkOffset; + FileChunk *pChunk; + +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + if( (iAmt+iOfst)>p->endpoint.iOffset ){ + return SQLITE_IOERR_SHORT_READ; + } +#endif + + assert( (iAmt+iOfst)<=p->endpoint.iOffset ); + assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 ); + if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ + sqlite3_int64 iOff = 0; + for(pChunk=p->pFirst; + ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; + pChunk=pChunk->pNext + ){ + iOff += p->nChunkSize; + } + }else{ + pChunk = p->readpoint.pChunk; + assert( pChunk!=0 ); + } + + iChunkOffset = (int)(iOfst%p->nChunkSize); + do { + int iSpace = p->nChunkSize - iChunkOffset; + int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); + memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); + zOut += nCopy; + nRead -= iSpace; + iChunkOffset = 0; + } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); + p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0; + p->readpoint.pChunk = pChunk; + + return SQLITE_OK; +} + +/* +** Free the list of FileChunk structures headed at MemJournal.pFirst. +*/ +static void memjrnlFreeChunks(MemJournal *p){ + FileChunk *pIter; + FileChunk *pNext; + for(pIter=p->pFirst; pIter; pIter=pNext){ + pNext = pIter->pNext; + sqlite3_free(pIter); + } + p->pFirst = 0; +} + +/* +** Flush the contents of memory to a real file on disk. +*/ +static int memjrnlCreateFile(MemJournal *p){ + int rc; + sqlite3_file *pReal = (sqlite3_file*)p; + MemJournal copy = *p; + + memset(p, 0, sizeof(MemJournal)); + rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0); + if( rc==SQLITE_OK ){ + int nChunk = copy.nChunkSize; + i64 iOff = 0; + FileChunk *pIter; + for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){ + if( iOff + nChunk > copy.endpoint.iOffset ){ + nChunk = copy.endpoint.iOffset - iOff; + } + rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff); + if( rc ) break; + iOff += nChunk; + } + if( rc==SQLITE_OK ){ + /* No error has occurred. Free the in-memory buffers. */ + memjrnlFreeChunks(©); + } + } + if( rc!=SQLITE_OK ){ + /* If an error occurred while creating or writing to the file, restore + ** the original before returning. This way, SQLite uses the in-memory + ** journal data to roll back changes made to the internal page-cache + ** before this function was called. */ + sqlite3OsClose(pReal); + *p = copy; + } + return rc; +} + + +/* +** Write data to the file. +*/ +static int memjrnlWrite( + sqlite3_file *pJfd, /* The journal file into which to write */ + const void *zBuf, /* Take data to be written from here */ + int iAmt, /* Number of bytes to write */ + sqlite_int64 iOfst /* Begin writing at this offset into the file */ +){ + MemJournal *p = (MemJournal *)pJfd; + int nWrite = iAmt; + u8 *zWrite = (u8 *)zBuf; + + /* If the file should be created now, create it and write the new data + ** into the file on disk. */ + if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ + int rc = memjrnlCreateFile(p); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst); + } + return rc; + } + + /* If the contents of this write should be stored in memory */ + else{ + /* An in-memory journal file should only ever be appended to. Random + ** access writes are not required. The only exception to this is when + ** the in-memory journal is being used by a connection using the + ** atomic-write optimization. In this case the first 28 bytes of the + ** journal file may be written as part of committing the transaction. */ + assert( iOfst==p->endpoint.iOffset || iOfst==0 ); +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + if( iOfst==0 && p->pFirst ){ + assert( p->nChunkSize>iAmt ); + memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); + }else +#else + assert( iOfst>0 || p->pFirst==0 ); +#endif + { + while( nWrite>0 ){ + FileChunk *pChunk = p->endpoint.pChunk; + int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); + int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); + + if( iChunkOffset==0 ){ + /* New chunk is required to extend the file. */ + FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); + if( !pNew ){ + return SQLITE_IOERR_NOMEM_BKPT; + } + pNew->pNext = 0; + if( pChunk ){ + assert( p->pFirst ); + pChunk->pNext = pNew; + }else{ + assert( !p->pFirst ); + p->pFirst = pNew; + } + p->endpoint.pChunk = pNew; + } + + memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); + zWrite += iSpace; + nWrite -= iSpace; + p->endpoint.iOffset += iSpace; + } + p->nSize = iAmt + iOfst; + } + } + + return SQLITE_OK; +} + +/* +** Truncate the file. +** +** If the journal file is already on disk, truncate it there. Or, if it +** is still in main memory but is being truncated to zero bytes in size, +** ignore +*/ +static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ + MemJournal *p = (MemJournal *)pJfd; + if( ALWAYS(size==0) ){ + memjrnlFreeChunks(p); + p->nSize = 0; + p->endpoint.pChunk = 0; + p->endpoint.iOffset = 0; + p->readpoint.pChunk = 0; + p->readpoint.iOffset = 0; + } + return SQLITE_OK; +} + +/* +** Close the file. +*/ +static int memjrnlClose(sqlite3_file *pJfd){ + MemJournal *p = (MemJournal *)pJfd; + memjrnlFreeChunks(p); + return SQLITE_OK; +} + +/* +** Sync the file. +** +** If the real file has been created, call its xSync method. Otherwise, +** syncing an in-memory journal is a no-op. +*/ +static int memjrnlSync(sqlite3_file *pJfd, int flags){ + UNUSED_PARAMETER2(pJfd, flags); + return SQLITE_OK; +} + +/* +** Query the size of the file in bytes. +*/ +static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ + MemJournal *p = (MemJournal *)pJfd; + *pSize = (sqlite_int64) p->endpoint.iOffset; + return SQLITE_OK; +} + +/* +** Table of methods for MemJournal sqlite3_file object. +*/ +static const struct sqlite3_io_methods MemJournalMethods = { + 1, /* iVersion */ + memjrnlClose, /* xClose */ + memjrnlRead, /* xRead */ + memjrnlWrite, /* xWrite */ + memjrnlTruncate, /* xTruncate */ + memjrnlSync, /* xSync */ + memjrnlFileSize, /* xFileSize */ + 0, /* xLock */ + 0, /* xUnlock */ + 0, /* xCheckReservedLock */ + 0, /* xFileControl */ + 0, /* xSectorSize */ + 0, /* xDeviceCharacteristics */ + 0, /* xShmMap */ + 0, /* xShmLock */ + 0, /* xShmBarrier */ + 0, /* xShmUnmap */ + 0, /* xFetch */ + 0 /* xUnfetch */ +}; + +/* +** Open a journal file. +** +** The behaviour of the journal file depends on the value of parameter +** nSpill. If nSpill is 0, then the journal file is always create and +** accessed using the underlying VFS. If nSpill is less than zero, then +** all content is always stored in main-memory. Finally, if nSpill is a +** positive value, then the journal file is initially created in-memory +** but may be flushed to disk later on. In this case the journal file is +** flushed to disk either when it grows larger than nSpill bytes in size, +** or when sqlite3JournalCreate() is called. +*/ +SQLITE_PRIVATE int sqlite3JournalOpen( + sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ + const char *zName, /* Name of the journal file */ + sqlite3_file *pJfd, /* Preallocated, blank file handle */ + int flags, /* Opening flags */ + int nSpill /* Bytes buffered before opening the file */ +){ + MemJournal *p = (MemJournal*)pJfd; + + /* Zero the file-handle object. If nSpill was passed zero, initialize + ** it using the sqlite3OsOpen() function of the underlying VFS. In this + ** case none of the code in this module is executed as a result of calls + ** made on the journal file-handle. */ + memset(p, 0, sizeof(MemJournal)); + if( nSpill==0 ){ + return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); + } + + if( nSpill>0 ){ + p->nChunkSize = nSpill; + }else{ + p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); + assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); + } + + p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods; + p->nSpill = nSpill; + p->flags = flags; + p->zJournal = zName; + p->pVfs = pVfs; + return SQLITE_OK; +} + +/* +** Open an in-memory journal file. +*/ +SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){ + sqlite3JournalOpen(0, 0, pJfd, 0, -1); +} + +#ifdef SQLITE_ENABLE_ATOMIC_WRITE +/* +** If the argument p points to a MemJournal structure that is not an +** in-memory-only journal file (i.e. is one that was opened with a +ve +** nSpill parameter), and the underlying file has not yet been created, +** create it now. +*/ +SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){ + int rc = SQLITE_OK; + if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){ + rc = memjrnlCreateFile((MemJournal*)p); + } + return rc; +} +#endif + +/* +** The file-handle passed as the only argument is open on a journal file. +** Return true if this "journal file" is currently stored in heap memory, +** or false otherwise. +*/ +SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){ + return p->pMethods==&MemJournalMethods; +} + +/* +** Return the number of bytes required to store a JournalFile that uses vfs +** pVfs to create the underlying on-disk files. +*/ +SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){ + return MAX(pVfs->szOsFile, (int)sizeof(MemJournal)); +} + +/************** End of memjournal.c ******************************************/ +/************** Begin file walker.c ******************************************/ +/* +** 2008 August 16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains routines used for walking the parser tree for +** an SQL statement. +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include */ + + +/* +** Walk an expression tree. Invoke the callback once for each node +** of the expression, while descending. (In other words, the callback +** is invoked before visiting children.) +** +** The return value from the callback should be one of the WRC_* +** constants to specify how to proceed with the walk. +** +** WRC_Continue Continue descending down the tree. +** +** WRC_Prune Do not descend into child nodes. But allow +** the walk to continue with sibling nodes. +** +** WRC_Abort Do no more callbacks. Unwind the stack and +** return the top-level walk call. +** +** The return value from this routine is WRC_Abort to abandon the tree walk +** and WRC_Continue to continue. +*/ +static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ + int rc; + testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); + testcase( ExprHasProperty(pExpr, EP_Reduced) ); + rc = pWalker->xExprCallback(pWalker, pExpr); + if( rc==WRC_Continue + && !ExprHasProperty(pExpr,EP_TokenOnly) ){ + if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; + if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort; + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; + }else{ + if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; + } + } + return rc & WRC_Abort; +} +SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ + return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; +} + +/* +** Call sqlite3WalkExpr() for every expression in list p or until +** an abort request is seen. +*/ +SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ + int i; + struct ExprList_item *pItem; + if( p ){ + for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ + if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; + } + } + return WRC_Continue; +} + +/* +** Walk all expressions associated with SELECT statement p. Do +** not invoke the SELECT callback on p, but do (of course) invoke +** any expr callbacks and SELECT callbacks that come from subqueries. +** Return WRC_Abort or WRC_Continue. +*/ +SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ + if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; + if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; + if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; + if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; + if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; + if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; + if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort; + return WRC_Continue; +} + +/* +** Walk the parse trees associated with all subqueries in the +** FROM clause of SELECT statement p. Do not invoke the select +** callback on p, but do invoke it on each FROM clause subquery +** and on any subqueries further down in the tree. Return +** WRC_Abort or WRC_Continue; +*/ +SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ + SrcList *pSrc; + int i; + struct SrcList_item *pItem; + + pSrc = p->pSrc; + if( ALWAYS(pSrc) ){ + for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ + if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){ + return WRC_Abort; + } + if( pItem->fg.isTabFunc + && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) + ){ + return WRC_Abort; + } + } + } + return WRC_Continue; +} + +/* +** Call sqlite3WalkExpr() for every expression in Select statement p. +** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and +** on the compound select chain, p->pPrior. +** +** If it is not NULL, the xSelectCallback() callback is invoked before +** the walk of the expressions and FROM clause. The xSelectCallback2() +** method, if it is not NULL, is invoked following the walk of the +** expressions and FROM clause. +** +** Return WRC_Continue under normal conditions. Return WRC_Abort if +** there is an abort request. +** +** If the Walker does not have an xSelectCallback() then this routine +** is a no-op returning WRC_Continue. +*/ +SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){ + int rc; + if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){ + return WRC_Continue; + } + rc = WRC_Continue; + pWalker->walkerDepth++; + while( p ){ + if( pWalker->xSelectCallback ){ + rc = pWalker->xSelectCallback(pWalker, p); + if( rc ) break; + } + if( sqlite3WalkSelectExpr(pWalker, p) + || sqlite3WalkSelectFrom(pWalker, p) + ){ + pWalker->walkerDepth--; + return WRC_Abort; + } + if( pWalker->xSelectCallback2 ){ + pWalker->xSelectCallback2(pWalker, p); + } + p = p->pPrior; + } + pWalker->walkerDepth--; + return rc & WRC_Abort; +} + +/************** End of walker.c **********************************************/ +/************** Begin file resolve.c *****************************************/ +/* +** 2008 August 18 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains routines used for walking the parser tree and +** resolve all identifiers by associating them with a particular +** table and column. +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include */ + +/* +** Walk the expression tree pExpr and increase the aggregate function +** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. +** This needs to occur when copying a TK_AGG_FUNCTION node from an +** outer query into an inner subquery. +** +** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) +** is a helper function - a callback for the tree walker. +*/ +static int incrAggDepth(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; + return WRC_Continue; +} +static void incrAggFunctionDepth(Expr *pExpr, int N){ + if( N>0 ){ + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = incrAggDepth; + w.u.n = N; + sqlite3WalkExpr(&w, pExpr); + } +} + +/* +** Turn the pExpr expression into an alias for the iCol-th column of the +** result set in pEList. +** +** If the reference is followed by a COLLATE operator, then make sure +** the COLLATE operator is preserved. For example: +** +** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; +** +** Should be transformed into: +** +** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; +** +** The nSubquery parameter specifies how many levels of subquery the +** alias is removed from the original expression. The usual value is +** zero but it might be more if the alias is contained within a subquery +** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION +** structures must be increased by the nSubquery amount. +*/ +static void resolveAlias( + Parse *pParse, /* Parsing context */ + ExprList *pEList, /* A result set */ + int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ + Expr *pExpr, /* Transform this into an alias to the result set */ + const char *zType, /* "GROUP" or "ORDER" or "" */ + int nSubquery /* Number of subqueries that the label is moving */ +){ + Expr *pOrig; /* The iCol-th column of the result set */ + Expr *pDup; /* Copy of pOrig */ + sqlite3 *db; /* The database connection */ + + assert( iCol>=0 && iColnExpr ); + pOrig = pEList->a[iCol].pExpr; + assert( pOrig!=0 ); + db = pParse->db; + pDup = sqlite3ExprDup(db, pOrig, 0); + if( pDup==0 ) return; + if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery); + if( pExpr->op==TK_COLLATE ){ + pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); + } + ExprSetProperty(pDup, EP_Alias); + + /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This + ** prevents ExprDelete() from deleting the Expr structure itself, + ** allowing it to be repopulated by the memcpy() on the following line. + ** The pExpr->u.zToken might point into memory that will be freed by the + ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to + ** make a copy of the token before doing the sqlite3DbFree(). + */ + ExprSetProperty(pExpr, EP_Static); + sqlite3ExprDelete(db, pExpr); + memcpy(pExpr, pDup, sizeof(*pExpr)); + if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){ + assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 ); + pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken); + pExpr->flags |= EP_MemToken; + } + sqlite3DbFree(db, pDup); +} + + +/* +** Return TRUE if the name zCol occurs anywhere in the USING clause. +** +** Return FALSE if the USING clause is NULL or if it does not contain +** zCol. +*/ +static int nameInUsingClause(IdList *pUsing, const char *zCol){ + if( pUsing ){ + int k; + for(k=0; knId; k++){ + if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1; + } + } + return 0; +} + +/* +** Subqueries stores the original database, table and column names for their +** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN". +** Check to see if the zSpan given to this routine matches the zDb, zTab, +** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will +** match anything. +*/ +SQLITE_PRIVATE int sqlite3MatchSpanName( + const char *zSpan, + const char *zCol, + const char *zTab, + const char *zDb +){ + int n; + for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} + if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ + return 0; + } + zSpan += n+1; + for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} + if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){ + return 0; + } + zSpan += n+1; + if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ + return 0; + } + return 1; +} + +/* +** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up +** that name in the set of source tables in pSrcList and make the pExpr +** expression node refer back to that source column. The following changes +** are made to pExpr: +** +** pExpr->iDb Set the index in db->aDb[] of the database X +** (even if X is implied). +** pExpr->iTable Set to the cursor number for the table obtained +** from pSrcList. +** pExpr->pTab Points to the Table structure of X.Y (even if +** X and/or Y are implied.) +** pExpr->iColumn Set to the column number within the table. +** pExpr->op Set to TK_COLUMN. +** pExpr->pLeft Any expression this points to is deleted +** pExpr->pRight Any expression this points to is deleted. +** +** The zDb variable is the name of the database (the "X"). This value may be +** NULL meaning that name is of the form Y.Z or Z. Any available database +** can be used. The zTable variable is the name of the table (the "Y"). This +** value can be NULL if zDb is also NULL. If zTable is NULL it +** means that the form of the name is Z and that columns from any table +** can be used. +** +** If the name cannot be resolved unambiguously, leave an error message +** in pParse and return WRC_Abort. Return WRC_Prune on success. +*/ +static int lookupName( + Parse *pParse, /* The parsing context */ + const char *zDb, /* Name of the database containing table, or NULL */ + const char *zTab, /* Name of table containing column, or NULL */ + const char *zCol, /* Name of the column. */ + NameContext *pNC, /* The name context used to resolve the name */ + Expr *pExpr /* Make this EXPR node point to the selected column */ +){ + int i, j; /* Loop counters */ + int cnt = 0; /* Number of matching column names */ + int cntTab = 0; /* Number of matching table names */ + int nSubquery = 0; /* How many levels of subquery */ + sqlite3 *db = pParse->db; /* The database connection */ + struct SrcList_item *pItem; /* Use for looping over pSrcList items */ + struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ + NameContext *pTopNC = pNC; /* First namecontext in the list */ + Schema *pSchema = 0; /* Schema of the expression */ + int isTrigger = 0; /* True if resolved to a trigger column */ + Table *pTab = 0; /* Table hold the row */ + Column *pCol; /* A column of pTab */ + + assert( pNC ); /* the name context cannot be NULL. */ + assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ + assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + + /* Initialize the node to no-match */ + pExpr->iTable = -1; + pExpr->pTab = 0; + ExprSetVVAProperty(pExpr, EP_NoReduce); + + /* Translate the schema name in zDb into a pointer to the corresponding + ** schema. If not found, pSchema will remain NULL and nothing will match + ** resulting in an appropriate error message toward the end of this routine + */ + if( zDb ){ + testcase( pNC->ncFlags & NC_PartIdx ); + testcase( pNC->ncFlags & NC_IsCheck ); + if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){ + /* Silently ignore database qualifiers inside CHECK constraints and + ** partial indices. Do not raise errors because that might break + ** legacy and because it does not hurt anything to just ignore the + ** database name. */ + zDb = 0; + }else{ + for(i=0; inDb; i++){ + assert( db->aDb[i].zName ); + if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){ + pSchema = db->aDb[i].pSchema; + break; + } + } + } + } + + /* Start at the inner-most context and move outward until a match is found */ + while( pNC && cnt==0 ){ + ExprList *pEList; + SrcList *pSrcList = pNC->pSrcList; + + if( pSrcList ){ + for(i=0, pItem=pSrcList->a; inSrc; i++, pItem++){ + pTab = pItem->pTab; + assert( pTab!=0 && pTab->zName!=0 ); + assert( pTab->nCol>0 ); + if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){ + int hit = 0; + pEList = pItem->pSelect->pEList; + for(j=0; jnExpr; j++){ + if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){ + cnt++; + cntTab = 2; + pMatch = pItem; + pExpr->iColumn = j; + hit = 1; + } + } + if( hit || zTab==0 ) continue; + } + if( zDb && pTab->pSchema!=pSchema ){ + continue; + } + if( zTab ){ + const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; + assert( zTabName!=0 ); + if( sqlite3StrICmp(zTabName, zTab)!=0 ){ + continue; + } + } + if( 0==(cntTab++) ){ + pMatch = pItem; + } + for(j=0, pCol=pTab->aCol; jnCol; j++, pCol++){ + if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ + /* If there has been exactly one prior match and this match + ** is for the right-hand table of a NATURAL JOIN or is in a + ** USING clause, then skip this match. + */ + if( cnt==1 ){ + if( pItem->fg.jointype & JT_NATURAL ) continue; + if( nameInUsingClause(pItem->pUsing, zCol) ) continue; + } + cnt++; + pMatch = pItem; + /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ + pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; + break; + } + } + } + if( pMatch ){ + pExpr->iTable = pMatch->iCursor; + pExpr->pTab = pMatch->pTab; + /* RIGHT JOIN not (yet) supported */ + assert( (pMatch->fg.jointype & JT_RIGHT)==0 ); + if( (pMatch->fg.jointype & JT_LEFT)!=0 ){ + ExprSetProperty(pExpr, EP_CanBeNull); + } + pSchema = pExpr->pTab->pSchema; + } + } /* if( pSrcList ) */ + +#ifndef SQLITE_OMIT_TRIGGER + /* If we have not already resolved the name, then maybe + ** it is a new.* or old.* trigger argument reference + */ + if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){ + int op = pParse->eTriggerOp; + assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); + if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){ + pExpr->iTable = 1; + pTab = pParse->pTriggerTab; + }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ + pExpr->iTable = 0; + pTab = pParse->pTriggerTab; + }else{ + pTab = 0; + } + + if( pTab ){ + int iCol; + pSchema = pTab->pSchema; + cntTab++; + for(iCol=0, pCol=pTab->aCol; iColnCol; iCol++, pCol++){ + if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ + if( iCol==pTab->iPKey ){ + iCol = -1; + } + break; + } + } + if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ + /* IMP: R-51414-32910 */ + iCol = -1; + } + if( iColnCol ){ + cnt++; + if( iCol<0 ){ + pExpr->affinity = SQLITE_AFF_INTEGER; + }else if( pExpr->iTable==0 ){ + testcase( iCol==31 ); + testcase( iCol==32 ); + pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<iColumn = (i16)iCol; + pExpr->pTab = pTab; + isTrigger = 1; + } + } + } +#endif /* !defined(SQLITE_OMIT_TRIGGER) */ + + /* + ** Perhaps the name is a reference to the ROWID + */ + if( cnt==0 + && cntTab==1 + && pMatch + && (pNC->ncFlags & NC_IdxExpr)==0 + && sqlite3IsRowid(zCol) + && VisibleRowid(pMatch->pTab) + ){ + cnt = 1; + pExpr->iColumn = -1; + pExpr->affinity = SQLITE_AFF_INTEGER; + } + + /* + ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z + ** might refer to an result-set alias. This happens, for example, when + ** we are resolving names in the WHERE clause of the following command: + ** + ** SELECT a+b AS x FROM table WHERE x<10; + ** + ** In cases like this, replace pExpr with a copy of the expression that + ** forms the result set entry ("a+b" in the example) and return immediately. + ** Note that the expression in the result set should have already been + ** resolved by the time the WHERE clause is resolved. + ** + ** The ability to use an output result-set column in the WHERE, GROUP BY, + ** or HAVING clauses, or as part of a larger expression in the ORDER BY + ** clause is not standard SQL. This is a (goofy) SQLite extension, that + ** is supported for backwards compatibility only. Hence, we issue a warning + ** on sqlite3_log() whenever the capability is used. + */ + if( (pEList = pNC->pEList)!=0 + && zTab==0 + && cnt==0 + ){ + for(j=0; jnExpr; j++){ + char *zAs = pEList->a[j].zName; + if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ + Expr *pOrig; + assert( pExpr->pLeft==0 && pExpr->pRight==0 ); + assert( pExpr->x.pList==0 ); + assert( pExpr->x.pSelect==0 ); + pOrig = pEList->a[j].pExpr; + if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ + sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); + return WRC_Abort; + } + resolveAlias(pParse, pEList, j, pExpr, "", nSubquery); + cnt = 1; + pMatch = 0; + assert( zTab==0 && zDb==0 ); + goto lookupname_end; + } + } + } + + /* Advance to the next name context. The loop will exit when either + ** we have a match (cnt>0) or when we run out of name contexts. + */ + if( cnt==0 ){ + pNC = pNC->pNext; + nSubquery++; + } + } + + /* + ** If X and Y are NULL (in other words if only the column name Z is + ** supplied) and the value of Z is enclosed in double-quotes, then + ** Z is a string literal if it doesn't match any column names. In that + ** case, we need to return right away and not make any changes to + ** pExpr. + ** + ** Because no reference was made to outer contexts, the pNC->nRef + ** fields are not changed in any context. + */ + if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){ + pExpr->op = TK_STRING; + pExpr->pTab = 0; + return WRC_Prune; + } + + /* + ** cnt==0 means there was not match. cnt>1 means there were two or + ** more matches. Either way, we have an error. + */ + if( cnt!=1 ){ + const char *zErr; + zErr = cnt==0 ? "no such column" : "ambiguous column name"; + if( zDb ){ + sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); + }else if( zTab ){ + sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); + }else{ + sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); + } + pParse->checkSchema = 1; + pTopNC->nErr++; + } + + /* If a column from a table in pSrcList is referenced, then record + ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes + ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the + ** column number is greater than the number of bits in the bitmask + ** then set the high-order bit of the bitmask. + */ + if( pExpr->iColumn>=0 && pMatch!=0 ){ + int n = pExpr->iColumn; + testcase( n==BMS-1 ); + if( n>=BMS ){ + n = BMS-1; + } + assert( pMatch->iCursor==pExpr->iTable ); + pMatch->colUsed |= ((Bitmask)1)<pLeft); + pExpr->pLeft = 0; + sqlite3ExprDelete(db, pExpr->pRight); + pExpr->pRight = 0; + pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN); +lookupname_end: + if( cnt==1 ){ + assert( pNC!=0 ); + if( !ExprHasProperty(pExpr, EP_Alias) ){ + sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); + } + /* Increment the nRef value on all name contexts from TopNC up to + ** the point where the name matched. */ + for(;;){ + assert( pTopNC!=0 ); + pTopNC->nRef++; + if( pTopNC==pNC ) break; + pTopNC = pTopNC->pNext; + } + return WRC_Prune; + } else { + return WRC_Abort; + } +} + +/* +** Allocate and return a pointer to an expression to load the column iCol +** from datasource iSrc in SrcList pSrc. +*/ +SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ + Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); + if( p ){ + struct SrcList_item *pItem = &pSrc->a[iSrc]; + p->pTab = pItem->pTab; + p->iTable = pItem->iCursor; + if( p->pTab->iPKey==iCol ){ + p->iColumn = -1; + }else{ + p->iColumn = (ynVar)iCol; + testcase( iCol==BMS ); + testcase( iCol==BMS-1 ); + pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); + } + ExprSetProperty(p, EP_Resolved); + } + return p; +} + +/* +** Report an error that an expression is not valid for some set of +** pNC->ncFlags values determined by validMask. +*/ +static void notValid( + Parse *pParse, /* Leave error message here */ + NameContext *pNC, /* The name context */ + const char *zMsg, /* Type of error */ + int validMask /* Set of contexts for which prohibited */ +){ + assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 ); + if( (pNC->ncFlags & validMask)!=0 ){ + const char *zIn = "partial index WHERE clauses"; + if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; +#ifndef SQLITE_OMIT_CHECK + else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; +#endif + sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); + } +} + +/* +** Expression p should encode a floating point value between 1.0 and 0.0. +** Return 1024 times this value. Or return -1 if p is not a floating point +** value between 1.0 and 0.0. +*/ +static int exprProbability(Expr *p){ + double r = -1.0; + if( p->op!=TK_FLOAT ) return -1; + sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); + assert( r>=0.0 ); + if( r>1.0 ) return -1; + return (int)(r*134217728.0); +} + +/* +** This routine is callback for sqlite3WalkExpr(). +** +** Resolve symbolic names into TK_COLUMN operators for the current +** node in the expression tree. Return 0 to continue the search down +** the tree or 2 to abort the tree walk. +** +** This routine also does error checking and name resolution for +** function names. The operator for aggregate functions is changed +** to TK_AGG_FUNCTION. +*/ +static int resolveExprStep(Walker *pWalker, Expr *pExpr){ + NameContext *pNC; + Parse *pParse; + + pNC = pWalker->u.pNC; + assert( pNC!=0 ); + pParse = pNC->pParse; + assert( pParse==pWalker->pParse ); + + if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune; + ExprSetProperty(pExpr, EP_Resolved); +#ifndef NDEBUG + if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ + SrcList *pSrcList = pNC->pSrcList; + int i; + for(i=0; ipSrcList->nSrc; i++){ + assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursornTab); + } + } +#endif + switch( pExpr->op ){ + +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) + /* The special operator TK_ROW means use the rowid for the first + ** column in the FROM clause. This is used by the LIMIT and ORDER BY + ** clause processing on UPDATE and DELETE statements. + */ + case TK_ROW: { + SrcList *pSrcList = pNC->pSrcList; + struct SrcList_item *pItem; + assert( pSrcList && pSrcList->nSrc==1 ); + pItem = pSrcList->a; + pExpr->op = TK_COLUMN; + pExpr->pTab = pItem->pTab; + pExpr->iTable = pItem->iCursor; + pExpr->iColumn = -1; + pExpr->affinity = SQLITE_AFF_INTEGER; + break; + } +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) + && !defined(SQLITE_OMIT_SUBQUERY) */ + + /* A lone identifier is the name of a column. + */ + case TK_ID: { + return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); + } + + /* A table name and column name: ID.ID + ** Or a database, table and column: ID.ID.ID + */ + case TK_DOT: { + const char *zColumn; + const char *zTable; + const char *zDb; + Expr *pRight; + + /* if( pSrcList==0 ) break; */ + notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr); + /*notValid(pParse, pNC, "the \".\" operator", NC_PartIdx|NC_IsCheck, 1);*/ + pRight = pExpr->pRight; + if( pRight->op==TK_ID ){ + zDb = 0; + zTable = pExpr->pLeft->u.zToken; + zColumn = pRight->u.zToken; + }else{ + assert( pRight->op==TK_DOT ); + zDb = pExpr->pLeft->u.zToken; + zTable = pRight->pLeft->u.zToken; + zColumn = pRight->pRight->u.zToken; + } + return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); + } + + /* Resolve function names + */ + case TK_FUNCTION: { + ExprList *pList = pExpr->x.pList; /* The argument list */ + int n = pList ? pList->nExpr : 0; /* Number of arguments */ + int no_such_func = 0; /* True if no such function exists */ + int wrong_num_args = 0; /* True if wrong number of arguments */ + int is_agg = 0; /* True if is an aggregate function */ + int auth; /* Authorization to use the function */ + int nId; /* Number of characters in function name */ + const char *zId; /* The function name. */ + FuncDef *pDef; /* Information about the function */ + u8 enc = ENC(pParse->db); /* The database encoding */ + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + notValid(pParse, pNC, "functions", NC_PartIdx); + zId = pExpr->u.zToken; + nId = sqlite3Strlen30(zId); + pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0); + if( pDef==0 ){ + pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0); + if( pDef==0 ){ + no_such_func = 1; + }else{ + wrong_num_args = 1; + } + }else{ + is_agg = pDef->xFinalize!=0; + if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ + ExprSetProperty(pExpr, EP_Unlikely|EP_Skip); + if( n==2 ){ + pExpr->iTable = exprProbability(pList->a[1].pExpr); + if( pExpr->iTable<0 ){ + sqlite3ErrorMsg(pParse, + "second argument to likelihood() must be a " + "constant between 0.0 and 1.0"); + pNC->nErr++; + } + }else{ + /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is + ** equivalent to likelihood(X, 0.0625). + ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is + ** short-hand for likelihood(X,0.0625). + ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand + ** for likelihood(X,0.9375). + ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent + ** to likelihood(X,0.9375). */ + /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ + pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; + } + } +#ifndef SQLITE_OMIT_AUTHORIZATION + auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); + if( auth!=SQLITE_OK ){ + if( auth==SQLITE_DENY ){ + sqlite3ErrorMsg(pParse, "not authorized to use function: %s", + pDef->zName); + pNC->nErr++; + } + pExpr->op = TK_NULL; + return WRC_Prune; + } +#endif + if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){ + /* For the purposes of the EP_ConstFunc flag, date and time + ** functions and other functions that change slowly are considered + ** constant because they are constant for the duration of one query */ + ExprSetProperty(pExpr,EP_ConstFunc); + } + if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){ + /* Date/time functions that use 'now', and other functions like + ** sqlite_version() that might change over time cannot be used + ** in an index. */ + notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr); + } + } + if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ + sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); + pNC->nErr++; + is_agg = 0; + }else if( no_such_func && pParse->db->init.busy==0 ){ + sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); + pNC->nErr++; + }else if( wrong_num_args ){ + sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", + nId, zId); + pNC->nErr++; + } + if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg; + sqlite3WalkExprList(pWalker, pList); + if( is_agg ){ + NameContext *pNC2 = pNC; + pExpr->op = TK_AGG_FUNCTION; + pExpr->op2 = 0; + while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ + pExpr->op2++; + pNC2 = pNC2->pNext; + } + assert( pDef!=0 ); + if( pNC2 ){ + assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); + testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); + pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX); + + } + pNC->ncFlags |= NC_AllowAgg; + } + /* FIX ME: Compute pExpr->affinity based on the expected return + ** type of the function + */ + return WRC_Prune; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_SELECT: + case TK_EXISTS: testcase( pExpr->op==TK_EXISTS ); +#endif + case TK_IN: { + testcase( pExpr->op==TK_IN ); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + int nRef = pNC->nRef; + notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr); + sqlite3WalkSelect(pWalker, pExpr->x.pSelect); + assert( pNC->nRef>=nRef ); + if( nRef!=pNC->nRef ){ + ExprSetProperty(pExpr, EP_VarSelect); + pNC->ncFlags |= NC_VarSelect; + } + } + break; + } + case TK_VARIABLE: { + notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr); + break; + } + } + return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; +} + +/* +** pEList is a list of expressions which are really the result set of the +** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. +** This routine checks to see if pE is a simple identifier which corresponds +** to the AS-name of one of the terms of the expression list. If it is, +** this routine return an integer between 1 and N where N is the number of +** elements in pEList, corresponding to the matching entry. If there is +** no match, or if pE is not a simple identifier, then this routine +** return 0. +** +** pEList has been resolved. pE has not. +*/ +static int resolveAsName( + Parse *pParse, /* Parsing context for error messages */ + ExprList *pEList, /* List of expressions to scan */ + Expr *pE /* Expression we are trying to match */ +){ + int i; /* Loop counter */ + + UNUSED_PARAMETER(pParse); + + if( pE->op==TK_ID ){ + char *zCol = pE->u.zToken; + for(i=0; inExpr; i++){ + char *zAs = pEList->a[i].zName; + if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ + return i+1; + } + } + } + return 0; +} + +/* +** pE is a pointer to an expression which is a single term in the +** ORDER BY of a compound SELECT. The expression has not been +** name resolved. +** +** At the point this routine is called, we already know that the +** ORDER BY term is not an integer index into the result set. That +** case is handled by the calling routine. +** +** Attempt to match pE against result set columns in the left-most +** SELECT statement. Return the index i of the matching column, +** as an indication to the caller that it should sort by the i-th column. +** The left-most column is 1. In other words, the value returned is the +** same integer value that would be used in the SQL statement to indicate +** the column. +** +** If there is no match, return 0. Return -1 if an error occurs. +*/ +static int resolveOrderByTermToExprList( + Parse *pParse, /* Parsing context for error messages */ + Select *pSelect, /* The SELECT statement with the ORDER BY clause */ + Expr *pE /* The specific ORDER BY term */ +){ + int i; /* Loop counter */ + ExprList *pEList; /* The columns of the result set */ + NameContext nc; /* Name context for resolving pE */ + sqlite3 *db; /* Database connection */ + int rc; /* Return code from subprocedures */ + u8 savedSuppErr; /* Saved value of db->suppressErr */ + + assert( sqlite3ExprIsInteger(pE, &i)==0 ); + pEList = pSelect->pEList; + + /* Resolve all names in the ORDER BY term expression + */ + memset(&nc, 0, sizeof(nc)); + nc.pParse = pParse; + nc.pSrcList = pSelect->pSrc; + nc.pEList = pEList; + nc.ncFlags = NC_AllowAgg; + nc.nErr = 0; + db = pParse->db; + savedSuppErr = db->suppressErr; + db->suppressErr = 1; + rc = sqlite3ResolveExprNames(&nc, pE); + db->suppressErr = savedSuppErr; + if( rc ) return 0; + + /* Try to match the ORDER BY expression against an expression + ** in the result set. Return an 1-based index of the matching + ** result-set entry. + */ + for(i=0; inExpr; i++){ + if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){ + return i+1; + } + } + + /* If no match, return 0. */ + return 0; +} + +/* +** Generate an ORDER BY or GROUP BY term out-of-range error. +*/ +static void resolveOutOfRangeError( + Parse *pParse, /* The error context into which to write the error */ + const char *zType, /* "ORDER" or "GROUP" */ + int i, /* The index (1-based) of the term out of range */ + int mx /* Largest permissible value of i */ +){ + sqlite3ErrorMsg(pParse, + "%r %s BY term out of range - should be " + "between 1 and %d", i, zType, mx); +} + +/* +** Analyze the ORDER BY clause in a compound SELECT statement. Modify +** each term of the ORDER BY clause is a constant integer between 1 +** and N where N is the number of columns in the compound SELECT. +** +** ORDER BY terms that are already an integer between 1 and N are +** unmodified. ORDER BY terms that are integers outside the range of +** 1 through N generate an error. ORDER BY terms that are expressions +** are matched against result set expressions of compound SELECT +** beginning with the left-most SELECT and working toward the right. +** At the first match, the ORDER BY expression is transformed into +** the integer column number. +** +** Return the number of errors seen. +*/ +static int resolveCompoundOrderBy( + Parse *pParse, /* Parsing context. Leave error messages here */ + Select *pSelect /* The SELECT statement containing the ORDER BY */ +){ + int i; + ExprList *pOrderBy; + ExprList *pEList; + sqlite3 *db; + int moreToDo = 1; + + pOrderBy = pSelect->pOrderBy; + if( pOrderBy==0 ) return 0; + db = pParse->db; +#if SQLITE_MAX_COLUMN + if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); + return 1; + } +#endif + for(i=0; inExpr; i++){ + pOrderBy->a[i].done = 0; + } + pSelect->pNext = 0; + while( pSelect->pPrior ){ + pSelect->pPrior->pNext = pSelect; + pSelect = pSelect->pPrior; + } + while( pSelect && moreToDo ){ + struct ExprList_item *pItem; + moreToDo = 0; + pEList = pSelect->pEList; + assert( pEList!=0 ); + for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ + int iCol = -1; + Expr *pE, *pDup; + if( pItem->done ) continue; + pE = sqlite3ExprSkipCollate(pItem->pExpr); + if( sqlite3ExprIsInteger(pE, &iCol) ){ + if( iCol<=0 || iCol>pEList->nExpr ){ + resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); + return 1; + } + }else{ + iCol = resolveAsName(pParse, pEList, pE); + if( iCol==0 ){ + pDup = sqlite3ExprDup(db, pE, 0); + if( !db->mallocFailed ){ + assert(pDup); + iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); + } + sqlite3ExprDelete(db, pDup); + } + } + if( iCol>0 ){ + /* Convert the ORDER BY term into an integer column number iCol, + ** taking care to preserve the COLLATE clause if it exists */ + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); + if( pNew==0 ) return 1; + pNew->flags |= EP_IntValue; + pNew->u.iValue = iCol; + if( pItem->pExpr==pE ){ + pItem->pExpr = pNew; + }else{ + Expr *pParent = pItem->pExpr; + assert( pParent->op==TK_COLLATE ); + while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; + assert( pParent->pLeft==pE ); + pParent->pLeft = pNew; + } + sqlite3ExprDelete(db, pE); + pItem->u.x.iOrderByCol = (u16)iCol; + pItem->done = 1; + }else{ + moreToDo = 1; + } + } + pSelect = pSelect->pNext; + } + for(i=0; inExpr; i++){ + if( pOrderBy->a[i].done==0 ){ + sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " + "column in the result set", i+1); + return 1; + } + } + return 0; +} + +/* +** Check every term in the ORDER BY or GROUP BY clause pOrderBy of +** the SELECT statement pSelect. If any term is reference to a +** result set expression (as determined by the ExprList.a.u.x.iOrderByCol +** field) then convert that term into a copy of the corresponding result set +** column. +** +** If any errors are detected, add an error message to pParse and +** return non-zero. Return zero if no errors are seen. +*/ +SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy( + Parse *pParse, /* Parsing context. Leave error messages here */ + Select *pSelect, /* The SELECT statement containing the clause */ + ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ + const char *zType /* "ORDER" or "GROUP" */ +){ + int i; + sqlite3 *db = pParse->db; + ExprList *pEList; + struct ExprList_item *pItem; + + if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; +#if SQLITE_MAX_COLUMN + if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); + return 1; + } +#endif + pEList = pSelect->pEList; + assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ + for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ + if( pItem->u.x.iOrderByCol ){ + if( pItem->u.x.iOrderByCol>pEList->nExpr ){ + resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); + return 1; + } + resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, + zType,0); + } + } + return 0; +} + +/* +** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. +** The Name context of the SELECT statement is pNC. zType is either +** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. +** +** This routine resolves each term of the clause into an expression. +** If the order-by term is an integer I between 1 and N (where N is the +** number of columns in the result set of the SELECT) then the expression +** in the resolution is a copy of the I-th result-set expression. If +** the order-by term is an identifier that corresponds to the AS-name of +** a result-set expression, then the term resolves to a copy of the +** result-set expression. Otherwise, the expression is resolved in +** the usual way - using sqlite3ResolveExprNames(). +** +** This routine returns the number of errors. If errors occur, then +** an appropriate error message might be left in pParse. (OOM errors +** excepted.) +*/ +static int resolveOrderGroupBy( + NameContext *pNC, /* The name context of the SELECT statement */ + Select *pSelect, /* The SELECT statement holding pOrderBy */ + ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ + const char *zType /* Either "ORDER" or "GROUP", as appropriate */ +){ + int i, j; /* Loop counters */ + int iCol; /* Column number */ + struct ExprList_item *pItem; /* A term of the ORDER BY clause */ + Parse *pParse; /* Parsing context */ + int nResult; /* Number of terms in the result set */ + + if( pOrderBy==0 ) return 0; + nResult = pSelect->pEList->nExpr; + pParse = pNC->pParse; + for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ + Expr *pE = pItem->pExpr; + Expr *pE2 = sqlite3ExprSkipCollate(pE); + if( zType[0]!='G' ){ + iCol = resolveAsName(pParse, pSelect->pEList, pE2); + if( iCol>0 ){ + /* If an AS-name match is found, mark this ORDER BY column as being + ** a copy of the iCol-th result-set column. The subsequent call to + ** sqlite3ResolveOrderGroupBy() will convert the expression to a + ** copy of the iCol-th result-set expression. */ + pItem->u.x.iOrderByCol = (u16)iCol; + continue; + } + } + if( sqlite3ExprIsInteger(pE2, &iCol) ){ + /* The ORDER BY term is an integer constant. Again, set the column + ** number so that sqlite3ResolveOrderGroupBy() will convert the + ** order-by term to a copy of the result-set expression */ + if( iCol<1 || iCol>0xffff ){ + resolveOutOfRangeError(pParse, zType, i+1, nResult); + return 1; + } + pItem->u.x.iOrderByCol = (u16)iCol; + continue; + } + + /* Otherwise, treat the ORDER BY term as an ordinary expression */ + pItem->u.x.iOrderByCol = 0; + if( sqlite3ResolveExprNames(pNC, pE) ){ + return 1; + } + for(j=0; jpEList->nExpr; j++){ + if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ + pItem->u.x.iOrderByCol = j+1; + } + } + } + return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); +} + +/* +** Resolve names in the SELECT statement p and all of its descendants. +*/ +static int resolveSelectStep(Walker *pWalker, Select *p){ + NameContext *pOuterNC; /* Context that contains this SELECT */ + NameContext sNC; /* Name context of this SELECT */ + int isCompound; /* True if p is a compound select */ + int nCompound; /* Number of compound terms processed so far */ + Parse *pParse; /* Parsing context */ + int i; /* Loop counter */ + ExprList *pGroupBy; /* The GROUP BY clause */ + Select *pLeftmost; /* Left-most of SELECT of a compound */ + sqlite3 *db; /* Database connection */ + + + assert( p!=0 ); + if( p->selFlags & SF_Resolved ){ + return WRC_Prune; + } + pOuterNC = pWalker->u.pNC; + pParse = pWalker->pParse; + db = pParse->db; + + /* Normally sqlite3SelectExpand() will be called first and will have + ** already expanded this SELECT. However, if this is a subquery within + ** an expression, sqlite3ResolveExprNames() will be called without a + ** prior call to sqlite3SelectExpand(). When that happens, let + ** sqlite3SelectPrep() do all of the processing for this SELECT. + ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and + ** this routine in the correct order. + */ + if( (p->selFlags & SF_Expanded)==0 ){ + sqlite3SelectPrep(pParse, p, pOuterNC); + return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; + } + + isCompound = p->pPrior!=0; + nCompound = 0; + pLeftmost = p; + while( p ){ + assert( (p->selFlags & SF_Expanded)!=0 ); + assert( (p->selFlags & SF_Resolved)==0 ); + p->selFlags |= SF_Resolved; + + /* Resolve the expressions in the LIMIT and OFFSET clauses. These + ** are not allowed to refer to any names, so pass an empty NameContext. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + if( sqlite3ResolveExprNames(&sNC, p->pLimit) || + sqlite3ResolveExprNames(&sNC, p->pOffset) ){ + return WRC_Abort; + } + + /* If the SF_Converted flags is set, then this Select object was + ** was created by the convertCompoundSelectToSubquery() function. + ** In this case the ORDER BY clause (p->pOrderBy) should be resolved + ** as if it were part of the sub-query, not the parent. This block + ** moves the pOrderBy down to the sub-query. It will be moved back + ** after the names have been resolved. */ + if( p->selFlags & SF_Converted ){ + Select *pSub = p->pSrc->a[0].pSelect; + assert( p->pSrc->nSrc==1 && p->pOrderBy ); + assert( pSub->pPrior && pSub->pOrderBy==0 ); + pSub->pOrderBy = p->pOrderBy; + p->pOrderBy = 0; + } + + /* Recursively resolve names in all subqueries + */ + for(i=0; ipSrc->nSrc; i++){ + struct SrcList_item *pItem = &p->pSrc->a[i]; + if( pItem->pSelect ){ + NameContext *pNC; /* Used to iterate name contexts */ + int nRef = 0; /* Refcount for pOuterNC and outer contexts */ + const char *zSavedContext = pParse->zAuthContext; + + /* Count the total number of references to pOuterNC and all of its + ** parent contexts. After resolving references to expressions in + ** pItem->pSelect, check if this value has changed. If so, then + ** SELECT statement pItem->pSelect must be correlated. Set the + ** pItem->fg.isCorrelated flag if this is the case. */ + for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef; + + if( pItem->zName ) pParse->zAuthContext = pItem->zName; + sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); + pParse->zAuthContext = zSavedContext; + if( pParse->nErr || db->mallocFailed ) return WRC_Abort; + + for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef; + assert( pItem->fg.isCorrelated==0 && nRef<=0 ); + pItem->fg.isCorrelated = (nRef!=0); + } + } + + /* Set up the local name-context to pass to sqlite3ResolveExprNames() to + ** resolve the result-set expression list. + */ + sNC.ncFlags = NC_AllowAgg; + sNC.pSrcList = p->pSrc; + sNC.pNext = pOuterNC; + + /* Resolve names in the result set. */ + if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort; + + /* If there are no aggregate functions in the result-set, and no GROUP BY + ** expression, do not allow aggregates in any of the other expressions. + */ + assert( (p->selFlags & SF_Aggregate)==0 ); + pGroupBy = p->pGroupBy; + if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ + assert( NC_MinMaxAgg==SF_MinMaxAgg ); + p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg); + }else{ + sNC.ncFlags &= ~NC_AllowAgg; + } + + /* If a HAVING clause is present, then there must be a GROUP BY clause. + */ + if( p->pHaving && !pGroupBy ){ + sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); + return WRC_Abort; + } + + /* Add the output column list to the name-context before parsing the + ** other expressions in the SELECT statement. This is so that + ** expressions in the WHERE clause (etc.) can refer to expressions by + ** aliases in the result set. + ** + ** Minor point: If this is the case, then the expression will be + ** re-evaluated for each reference to it. + */ + sNC.pEList = p->pEList; + if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; + if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; + + /* Resolve names in table-valued-function arguments */ + for(i=0; ipSrc->nSrc; i++){ + struct SrcList_item *pItem = &p->pSrc->a[i]; + if( pItem->fg.isTabFunc + && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) + ){ + return WRC_Abort; + } + } + + /* The ORDER BY and GROUP BY clauses may not refer to terms in + ** outer queries + */ + sNC.pNext = 0; + sNC.ncFlags |= NC_AllowAgg; + + /* If this is a converted compound query, move the ORDER BY clause from + ** the sub-query back to the parent query. At this point each term + ** within the ORDER BY clause has been transformed to an integer value. + ** These integers will be replaced by copies of the corresponding result + ** set expressions by the call to resolveOrderGroupBy() below. */ + if( p->selFlags & SF_Converted ){ + Select *pSub = p->pSrc->a[0].pSelect; + p->pOrderBy = pSub->pOrderBy; + pSub->pOrderBy = 0; + } + + /* Process the ORDER BY clause for singleton SELECT statements. + ** The ORDER BY clause for compounds SELECT statements is handled + ** below, after all of the result-sets for all of the elements of + ** the compound have been resolved. + ** + ** If there is an ORDER BY clause on a term of a compound-select other + ** than the right-most term, then that is a syntax error. But the error + ** is not detected until much later, and so we need to go ahead and + ** resolve those symbols on the incorrect ORDER BY for consistency. + */ + if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ + && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") + ){ + return WRC_Abort; + } + if( db->mallocFailed ){ + return WRC_Abort; + } + + /* Resolve the GROUP BY clause. At the same time, make sure + ** the GROUP BY clause does not contain aggregate functions. + */ + if( pGroupBy ){ + struct ExprList_item *pItem; + + if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ + return WRC_Abort; + } + for(i=0, pItem=pGroupBy->a; inExpr; i++, pItem++){ + if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ + sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " + "the GROUP BY clause"); + return WRC_Abort; + } + } + } + + /* If this is part of a compound SELECT, check that it has the right + ** number of expressions in the select list. */ + if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ + sqlite3SelectWrongNumTermsError(pParse, p->pNext); + return WRC_Abort; + } + + /* Advance to the next term of the compound + */ + p = p->pPrior; + nCompound++; + } + + /* Resolve the ORDER BY on a compound SELECT after all terms of + ** the compound have been resolved. + */ + if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ + return WRC_Abort; + } + + return WRC_Prune; +} + +/* +** This routine walks an expression tree and resolves references to +** table columns and result-set columns. At the same time, do error +** checking on function usage and set a flag if any aggregate functions +** are seen. +** +** To resolve table columns references we look for nodes (or subtrees) of the +** form X.Y.Z or Y.Z or just Z where +** +** X: The name of a database. Ex: "main" or "temp" or +** the symbolic name assigned to an ATTACH-ed database. +** +** Y: The name of a table in a FROM clause. Or in a trigger +** one of the special names "old" or "new". +** +** Z: The name of a column in table Y. +** +** The node at the root of the subtree is modified as follows: +** +** Expr.op Changed to TK_COLUMN +** Expr.pTab Points to the Table object for X.Y +** Expr.iColumn The column index in X.Y. -1 for the rowid. +** Expr.iTable The VDBE cursor number for X.Y +** +** +** To resolve result-set references, look for expression nodes of the +** form Z (with no X and Y prefix) where the Z matches the right-hand +** size of an AS clause in the result-set of a SELECT. The Z expression +** is replaced by a copy of the left-hand side of the result-set expression. +** Table-name and function resolution occurs on the substituted expression +** tree. For example, in: +** +** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; +** +** The "x" term of the order by is replaced by "a+b" to render: +** +** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; +** +** Function calls are checked to make sure that the function is +** defined and that the correct number of arguments are specified. +** If the function is an aggregate function, then the NC_HasAgg flag is +** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. +** If an expression contains aggregate functions then the EP_Agg +** property on the expression is set. +** +** An error message is left in pParse if anything is amiss. The number +** if errors is returned. +*/ +SQLITE_PRIVATE int sqlite3ResolveExprNames( + NameContext *pNC, /* Namespace to resolve expressions in. */ + Expr *pExpr /* The expression to be analyzed. */ +){ + u16 savedHasAgg; + Walker w; + + if( pExpr==0 ) return 0; +#if SQLITE_MAX_EXPR_DEPTH>0 + { + Parse *pParse = pNC->pParse; + if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ + return 1; + } + pParse->nHeight += pExpr->nHeight; + } +#endif + savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg); + pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg); + w.pParse = pNC->pParse; + w.xExprCallback = resolveExprStep; + w.xSelectCallback = resolveSelectStep; + w.xSelectCallback2 = 0; + w.walkerDepth = 0; + w.eCode = 0; + w.u.pNC = pNC; + sqlite3WalkExpr(&w, pExpr); +#if SQLITE_MAX_EXPR_DEPTH>0 + pNC->pParse->nHeight -= pExpr->nHeight; +#endif + if( pNC->nErr>0 || w.pParse->nErr>0 ){ + ExprSetProperty(pExpr, EP_Error); + } + if( pNC->ncFlags & NC_HasAgg ){ + ExprSetProperty(pExpr, EP_Agg); + } + pNC->ncFlags |= savedHasAgg; + return ExprHasProperty(pExpr, EP_Error); +} + +/* +** Resolve all names for all expression in an expression list. This is +** just like sqlite3ResolveExprNames() except that it works for an expression +** list rather than a single expression. +*/ +SQLITE_PRIVATE int sqlite3ResolveExprListNames( + NameContext *pNC, /* Namespace to resolve expressions in. */ + ExprList *pList /* The expression list to be analyzed. */ +){ + int i; + if( pList ){ + for(i=0; inExpr; i++){ + if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort; + } + } + return WRC_Continue; +} + +/* +** Resolve all names in all expressions of a SELECT and in all +** decendents of the SELECT, including compounds off of p->pPrior, +** subqueries in expressions, and subqueries used as FROM clause +** terms. +** +** See sqlite3ResolveExprNames() for a description of the kinds of +** transformations that occur. +** +** All SELECT statements should have been expanded using +** sqlite3SelectExpand() prior to invoking this routine. +*/ +SQLITE_PRIVATE void sqlite3ResolveSelectNames( + Parse *pParse, /* The parser context */ + Select *p, /* The SELECT statement being coded. */ + NameContext *pOuterNC /* Name context for parent SELECT statement */ +){ + Walker w; + + assert( p!=0 ); + memset(&w, 0, sizeof(w)); + w.xExprCallback = resolveExprStep; + w.xSelectCallback = resolveSelectStep; + w.pParse = pParse; + w.u.pNC = pOuterNC; + sqlite3WalkSelect(&w, p); +} + +/* +** Resolve names in expressions that can only reference a single table: +** +** * CHECK constraints +** * WHERE clauses on partial indices +** +** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression +** is set to -1 and the Expr.iColumn value is set to the column number. +** +** Any errors cause an error message to be set in pParse. +*/ +SQLITE_PRIVATE void sqlite3ResolveSelfReference( + Parse *pParse, /* Parsing context */ + Table *pTab, /* The table being referenced */ + int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */ + Expr *pExpr, /* Expression to resolve. May be NULL. */ + ExprList *pList /* Expression list to resolve. May be NUL. */ +){ + SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ + NameContext sNC; /* Name context for pParse->pNewTable */ + + assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr ); + memset(&sNC, 0, sizeof(sNC)); + memset(&sSrc, 0, sizeof(sSrc)); + sSrc.nSrc = 1; + sSrc.a[0].zName = pTab->zName; + sSrc.a[0].pTab = pTab; + sSrc.a[0].iCursor = -1; + sNC.pParse = pParse; + sNC.pSrcList = &sSrc; + sNC.ncFlags = type; + if( sqlite3ResolveExprNames(&sNC, pExpr) ) return; + if( pList ) sqlite3ResolveExprListNames(&sNC, pList); +} + +/************** End of resolve.c *********************************************/ +/************** Begin file expr.c ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains routines used for analyzing expressions and +** for generating VDBE code that evaluates expressions in SQLite. +*/ +/* #include "sqliteInt.h" */ + +/* +** Return the 'affinity' of the expression pExpr if any. +** +** If pExpr is a column, a reference to a column via an 'AS' alias, +** or a sub-select with a column as the return value, then the +** affinity of that column is returned. Otherwise, 0x00 is returned, +** indicating no affinity for the expression. +** +** i.e. the WHERE clause expressions in the following statements all +** have an affinity: +** +** CREATE TABLE t1(a); +** SELECT * FROM t1 WHERE a; +** SELECT a AS b FROM t1 WHERE b; +** SELECT * FROM t1 WHERE (select a from t1); +*/ +SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){ + int op; + pExpr = sqlite3ExprSkipCollate(pExpr); + if( pExpr->flags & EP_Generic ) return 0; + op = pExpr->op; + if( op==TK_SELECT ){ + assert( pExpr->flags&EP_xIsSelect ); + return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); + } +#ifndef SQLITE_OMIT_CAST + if( op==TK_CAST ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + return sqlite3AffinityType(pExpr->u.zToken, 0); + } +#endif + if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) + && pExpr->pTab!=0 + ){ + /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally + ** a TK_COLUMN but was previously evaluated and cached in a register */ + int j = pExpr->iColumn; + if( j<0 ) return SQLITE_AFF_INTEGER; + assert( pExpr->pTab && jpTab->nCol ); + return pExpr->pTab->aCol[j].affinity; + } + return pExpr->affinity; +} + +/* +** Set the collating sequence for expression pExpr to be the collating +** sequence named by pToken. Return a pointer to a new Expr node that +** implements the COLLATE operator. +** +** If a memory allocation error occurs, that fact is recorded in pParse->db +** and the pExpr parameter is returned unchanged. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* Add the "COLLATE" clause to this expression */ + const Token *pCollName, /* Name of collating sequence */ + int dequote /* True to dequote pCollName */ +){ + if( pCollName->n>0 ){ + Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); + if( pNew ){ + pNew->pLeft = pExpr; + pNew->flags |= EP_Collate|EP_Skip; + pExpr = pNew; + } + } + return pExpr; +} +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ + Token s; + assert( zC!=0 ); + sqlite3TokenInit(&s, (char*)zC); + return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); +} + +/* +** Skip over any TK_COLLATE operators and any unlikely() +** or likelihood() function at the root of an expression. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){ + while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ + if( ExprHasProperty(pExpr, EP_Unlikely) ){ + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + assert( pExpr->x.pList->nExpr>0 ); + assert( pExpr->op==TK_FUNCTION ); + pExpr = pExpr->x.pList->a[0].pExpr; + }else{ + assert( pExpr->op==TK_COLLATE ); + pExpr = pExpr->pLeft; + } + } + return pExpr; +} + +/* +** Return the collation sequence for the expression pExpr. If +** there is no defined collating sequence, return NULL. +** +** The collating sequence might be determined by a COLLATE operator +** or by the presence of a column with a defined collating sequence. +** COLLATE operators take first precedence. Left operands take +** precedence over right operands. +*/ +SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ + sqlite3 *db = pParse->db; + CollSeq *pColl = 0; + Expr *p = pExpr; + while( p ){ + int op = p->op; + if( p->flags & EP_Generic ) break; + if( op==TK_CAST || op==TK_UPLUS ){ + p = p->pLeft; + continue; + } + if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ + pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); + break; + } + if( (op==TK_AGG_COLUMN || op==TK_COLUMN + || op==TK_REGISTER || op==TK_TRIGGER) + && p->pTab!=0 + ){ + /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally + ** a TK_COLUMN but was previously evaluated and cached in a register */ + int j = p->iColumn; + if( j>=0 ){ + const char *zColl = p->pTab->aCol[j].zColl; + pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); + } + break; + } + if( p->flags & EP_Collate ){ + if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ + p = p->pLeft; + }else{ + Expr *pNext = p->pRight; + /* The Expr.x union is never used at the same time as Expr.pRight */ + assert( p->x.pList==0 || p->pRight==0 ); + /* p->flags holds EP_Collate and p->pLeft->flags does not. And + ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at + ** least one EP_Collate. Thus the following two ALWAYS. */ + if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ + int i; + for(i=0; ALWAYS(ix.pList->nExpr); i++){ + if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ + pNext = p->x.pList->a[i].pExpr; + break; + } + } + } + p = pNext; + } + }else{ + break; + } + } + if( sqlite3CheckCollSeq(pParse, pColl) ){ + pColl = 0; + } + return pColl; +} + +/* +** pExpr is an operand of a comparison operator. aff2 is the +** type affinity of the other operand. This routine returns the +** type affinity that should be used for the comparison operator. +*/ +SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){ + char aff1 = sqlite3ExprAffinity(pExpr); + if( aff1 && aff2 ){ + /* Both sides of the comparison are columns. If one has numeric + ** affinity, use that. Otherwise use no affinity. + */ + if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ + return SQLITE_AFF_NUMERIC; + }else{ + return SQLITE_AFF_BLOB; + } + }else if( !aff1 && !aff2 ){ + /* Neither side of the comparison is a column. Compare the + ** results directly. + */ + return SQLITE_AFF_BLOB; + }else{ + /* One side is a column, the other is not. Use the columns affinity. */ + assert( aff1==0 || aff2==0 ); + return (aff1 + aff2); + } +} + +/* +** pExpr is a comparison operator. Return the type affinity that should +** be applied to both operands prior to doing the comparison. +*/ +static char comparisonAffinity(Expr *pExpr){ + char aff; + assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || + pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || + pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); + assert( pExpr->pLeft ); + aff = sqlite3ExprAffinity(pExpr->pLeft); + if( pExpr->pRight ){ + aff = sqlite3CompareAffinity(pExpr->pRight, aff); + }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); + }else if( !aff ){ + aff = SQLITE_AFF_BLOB; + } + return aff; +} + +/* +** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. +** idx_affinity is the affinity of an indexed column. Return true +** if the index with affinity idx_affinity may be used to implement +** the comparison in pExpr. +*/ +SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ + char aff = comparisonAffinity(pExpr); + switch( aff ){ + case SQLITE_AFF_BLOB: + return 1; + case SQLITE_AFF_TEXT: + return idx_affinity==SQLITE_AFF_TEXT; + default: + return sqlite3IsNumericAffinity(idx_affinity); + } +} + +/* +** Return the P5 value that should be used for a binary comparison +** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. +*/ +static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ + u8 aff = (char)sqlite3ExprAffinity(pExpr2); + aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; + return aff; +} + +/* +** Return a pointer to the collation sequence that should be used by +** a binary comparison operator comparing pLeft and pRight. +** +** If the left hand expression has a collating sequence type, then it is +** used. Otherwise the collation sequence for the right hand expression +** is used, or the default (BINARY) if neither expression has a collating +** type. +** +** Argument pRight (but not pLeft) may be a null pointer. In this case, +** it is not considered. +*/ +SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq( + Parse *pParse, + Expr *pLeft, + Expr *pRight +){ + CollSeq *pColl; + assert( pLeft ); + if( pLeft->flags & EP_Collate ){ + pColl = sqlite3ExprCollSeq(pParse, pLeft); + }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ + pColl = sqlite3ExprCollSeq(pParse, pRight); + }else{ + pColl = sqlite3ExprCollSeq(pParse, pLeft); + if( !pColl ){ + pColl = sqlite3ExprCollSeq(pParse, pRight); + } + } + return pColl; +} + +/* +** Generate code for a comparison operator. +*/ +static int codeCompare( + Parse *pParse, /* The parsing (and code generating) context */ + Expr *pLeft, /* The left operand */ + Expr *pRight, /* The right operand */ + int opcode, /* The comparison opcode */ + int in1, int in2, /* Register holding operands */ + int dest, /* Jump here if true. */ + int jumpIfNull /* If true, jump if either operand is NULL */ +){ + int p5; + int addr; + CollSeq *p4; + + p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); + p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); + addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, + (void*)p4, P4_COLLSEQ); + sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); + return addr; +} + +#if SQLITE_MAX_EXPR_DEPTH>0 +/* +** Check that argument nHeight is less than or equal to the maximum +** expression depth allowed. If it is not, leave an error message in +** pParse. +*/ +SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ + int rc = SQLITE_OK; + int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; + if( nHeight>mxHeight ){ + sqlite3ErrorMsg(pParse, + "Expression tree is too large (maximum depth %d)", mxHeight + ); + rc = SQLITE_ERROR; + } + return rc; +} + +/* The following three functions, heightOfExpr(), heightOfExprList() +** and heightOfSelect(), are used to determine the maximum height +** of any expression tree referenced by the structure passed as the +** first argument. +** +** If this maximum height is greater than the current value pointed +** to by pnHeight, the second parameter, then set *pnHeight to that +** value. +*/ +static void heightOfExpr(Expr *p, int *pnHeight){ + if( p ){ + if( p->nHeight>*pnHeight ){ + *pnHeight = p->nHeight; + } + } +} +static void heightOfExprList(ExprList *p, int *pnHeight){ + if( p ){ + int i; + for(i=0; inExpr; i++){ + heightOfExpr(p->a[i].pExpr, pnHeight); + } + } +} +static void heightOfSelect(Select *p, int *pnHeight){ + if( p ){ + heightOfExpr(p->pWhere, pnHeight); + heightOfExpr(p->pHaving, pnHeight); + heightOfExpr(p->pLimit, pnHeight); + heightOfExpr(p->pOffset, pnHeight); + heightOfExprList(p->pEList, pnHeight); + heightOfExprList(p->pGroupBy, pnHeight); + heightOfExprList(p->pOrderBy, pnHeight); + heightOfSelect(p->pPrior, pnHeight); + } +} + +/* +** Set the Expr.nHeight variable in the structure passed as an +** argument. An expression with no children, Expr.pList or +** Expr.pSelect member has a height of 1. Any other expression +** has a height equal to the maximum height of any other +** referenced Expr plus one. +** +** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, +** if appropriate. +*/ +static void exprSetHeight(Expr *p){ + int nHeight = 0; + heightOfExpr(p->pLeft, &nHeight); + heightOfExpr(p->pRight, &nHeight); + if( ExprHasProperty(p, EP_xIsSelect) ){ + heightOfSelect(p->x.pSelect, &nHeight); + }else if( p->x.pList ){ + heightOfExprList(p->x.pList, &nHeight); + p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); + } + p->nHeight = nHeight + 1; +} + +/* +** Set the Expr.nHeight variable using the exprSetHeight() function. If +** the height is greater than the maximum allowed expression depth, +** leave an error in pParse. +** +** Also propagate all EP_Propagate flags from the Expr.x.pList into +** Expr.flags. +*/ +SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ + if( pParse->nErr ) return; + exprSetHeight(p); + sqlite3ExprCheckHeight(pParse, p->nHeight); +} + +/* +** Return the maximum height of any expression tree referenced +** by the select statement passed as an argument. +*/ +SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){ + int nHeight = 0; + heightOfSelect(p, &nHeight); + return nHeight; +} +#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ +/* +** Propagate all EP_Propagate flags from the Expr.x.pList into +** Expr.flags. +*/ +SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ + if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ + p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); + } +} +#define exprSetHeight(y) +#endif /* SQLITE_MAX_EXPR_DEPTH>0 */ + +/* +** This routine is the core allocator for Expr nodes. +** +** Construct a new expression node and return a pointer to it. Memory +** for this node and for the pToken argument is a single allocation +** obtained from sqlite3DbMalloc(). The calling function +** is responsible for making sure the node eventually gets freed. +** +** If dequote is true, then the token (if it exists) is dequoted. +** If dequote is false, no dequoting is performed. The deQuote +** parameter is ignored if pToken is NULL or if the token does not +** appear to be quoted. If the quotes were of the form "..." (double-quotes) +** then the EP_DblQuoted flag is set on the expression node. +** +** Special case: If op==TK_INTEGER and pToken points to a string that +** can be translated into a 32-bit integer, then the token is not +** stored in u.zToken. Instead, the integer values is written +** into u.iValue and the EP_IntValue flag is set. No extra storage +** is allocated to hold the integer text and the dequote flag is ignored. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprAlloc( + sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ + int op, /* Expression opcode */ + const Token *pToken, /* Token argument. Might be NULL */ + int dequote /* True to dequote */ +){ + Expr *pNew; + int nExtra = 0; + int iValue = 0; + + assert( db!=0 ); + if( pToken ){ + if( op!=TK_INTEGER || pToken->z==0 + || sqlite3GetInt32(pToken->z, &iValue)==0 ){ + nExtra = pToken->n+1; + assert( iValue>=0 ); + } + } + pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); + if( pNew ){ + memset(pNew, 0, sizeof(Expr)); + pNew->op = (u8)op; + pNew->iAgg = -1; + if( pToken ){ + if( nExtra==0 ){ + pNew->flags |= EP_IntValue; + pNew->u.iValue = iValue; + }else{ + pNew->u.zToken = (char*)&pNew[1]; + assert( pToken->z!=0 || pToken->n==0 ); + if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); + pNew->u.zToken[pToken->n] = 0; + if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ + if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; + sqlite3Dequote(pNew->u.zToken); + } + } + } +#if SQLITE_MAX_EXPR_DEPTH>0 + pNew->nHeight = 1; +#endif + } + return pNew; +} + +/* +** Allocate a new expression node from a zero-terminated token that has +** already been dequoted. +*/ +SQLITE_PRIVATE Expr *sqlite3Expr( + sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ + int op, /* Expression opcode */ + const char *zToken /* Token argument. Might be NULL */ +){ + Token x; + x.z = zToken; + x.n = zToken ? sqlite3Strlen30(zToken) : 0; + return sqlite3ExprAlloc(db, op, &x, 0); +} + +/* +** Attach subtrees pLeft and pRight to the Expr node pRoot. +** +** If pRoot==NULL that means that a memory allocation error has occurred. +** In that case, delete the subtrees pLeft and pRight. +*/ +SQLITE_PRIVATE void sqlite3ExprAttachSubtrees( + sqlite3 *db, + Expr *pRoot, + Expr *pLeft, + Expr *pRight +){ + if( pRoot==0 ){ + assert( db->mallocFailed ); + sqlite3ExprDelete(db, pLeft); + sqlite3ExprDelete(db, pRight); + }else{ + if( pRight ){ + pRoot->pRight = pRight; + pRoot->flags |= EP_Propagate & pRight->flags; + } + if( pLeft ){ + pRoot->pLeft = pLeft; + pRoot->flags |= EP_Propagate & pLeft->flags; + } + exprSetHeight(pRoot); + } +} + +/* +** Allocate an Expr node which joins as many as two subtrees. +** +** One or both of the subtrees can be NULL. Return a pointer to the new +** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, +** free the subtrees and return NULL. +*/ +SQLITE_PRIVATE Expr *sqlite3PExpr( + Parse *pParse, /* Parsing context */ + int op, /* Expression opcode */ + Expr *pLeft, /* Left operand */ + Expr *pRight, /* Right operand */ + const Token *pToken /* Argument token */ +){ + Expr *p; + if( op==TK_AND && pParse->nErr==0 ){ + /* Take advantage of short-circuit false optimization for AND */ + p = sqlite3ExprAnd(pParse->db, pLeft, pRight); + }else{ + p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); + sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); + } + if( p ) { + sqlite3ExprCheckHeight(pParse, p->nHeight); + } + return p; +} + +/* +** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due +** do a memory allocation failure) then delete the pSelect object. +*/ +SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ + if( pExpr ){ + pExpr->x.pSelect = pSelect; + ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); + sqlite3ExprSetHeightAndFlags(pParse, pExpr); + }else{ + assert( pParse->db->mallocFailed ); + sqlite3SelectDelete(pParse->db, pSelect); + } +} + + +/* +** If the expression is always either TRUE or FALSE (respectively), +** then return 1. If one cannot determine the truth value of the +** expression at compile-time return 0. +** +** This is an optimization. If is OK to return 0 here even if +** the expression really is always false or false (a false negative). +** But it is a bug to return 1 if the expression might have different +** boolean values in different circumstances (a false positive.) +** +** Note that if the expression is part of conditional for a +** LEFT JOIN, then we cannot determine at compile-time whether or not +** is it true or false, so always return 0. +*/ +static int exprAlwaysTrue(Expr *p){ + int v = 0; + if( ExprHasProperty(p, EP_FromJoin) ) return 0; + if( !sqlite3ExprIsInteger(p, &v) ) return 0; + return v!=0; +} +static int exprAlwaysFalse(Expr *p){ + int v = 0; + if( ExprHasProperty(p, EP_FromJoin) ) return 0; + if( !sqlite3ExprIsInteger(p, &v) ) return 0; + return v==0; +} + +/* +** Join two expressions using an AND operator. If either expression is +** NULL, then just return the other expression. +** +** If one side or the other of the AND is known to be false, then instead +** of returning an AND expression, just return a constant expression with +** a value of false. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ + if( pLeft==0 ){ + return pRight; + }else if( pRight==0 ){ + return pLeft; + }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ + sqlite3ExprDelete(db, pLeft); + sqlite3ExprDelete(db, pRight); + return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); + }else{ + Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); + sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); + return pNew; + } +} + +/* +** Construct a new expression node for a function with multiple +** arguments. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ + Expr *pNew; + sqlite3 *db = pParse->db; + assert( pToken ); + pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); + if( pNew==0 ){ + sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ + return 0; + } + pNew->x.pList = pList; + assert( !ExprHasProperty(pNew, EP_xIsSelect) ); + sqlite3ExprSetHeightAndFlags(pParse, pNew); + return pNew; +} + +/* +** Assign a variable number to an expression that encodes a wildcard +** in the original SQL statement. +** +** Wildcards consisting of a single "?" are assigned the next sequential +** variable number. +** +** Wildcards of the form "?nnn" are assigned the number "nnn". We make +** sure "nnn" is not too be to avoid a denial of service attack when +** the SQL statement comes from an external source. +** +** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number +** as the previous instance of the same wildcard. Or if this is the first +** instance of the wildcard, the next sequential variable number is +** assigned. +*/ +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ + sqlite3 *db = pParse->db; + const char *z; + + if( pExpr==0 ) return; + assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); + z = pExpr->u.zToken; + assert( z!=0 ); + assert( z[0]!=0 ); + if( z[1]==0 ){ + /* Wildcard of the form "?". Assign the next variable number */ + assert( z[0]=='?' ); + pExpr->iColumn = (ynVar)(++pParse->nVar); + }else{ + ynVar x = 0; + u32 n = sqlite3Strlen30(z); + if( z[0]=='?' ){ + /* Wildcard of the form "?nnn". Convert "nnn" to an integer and + ** use it as the variable number */ + i64 i; + int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); + pExpr->iColumn = x = (ynVar)i; + testcase( i==0 ); + testcase( i==1 ); + testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); + testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); + if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ + sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", + db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); + x = 0; + } + if( i>pParse->nVar ){ + pParse->nVar = (int)i; + } + }else{ + /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable + ** number as the prior appearance of the same name, or if the name + ** has never appeared before, reuse the same variable number + */ + ynVar i; + for(i=0; inzVar; i++){ + if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ + pExpr->iColumn = x = (ynVar)i+1; + break; + } + } + if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); + } + if( x>0 ){ + if( x>pParse->nzVar ){ + char **a; + a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); + if( a==0 ){ + assert( db->mallocFailed ); /* Error reported through mallocFailed */ + return; + } + pParse->azVar = a; + memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); + pParse->nzVar = x; + } + if( z[0]!='?' || pParse->azVar[x-1]==0 ){ + sqlite3DbFree(db, pParse->azVar[x-1]); + pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); + } + } + } + if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ + sqlite3ErrorMsg(pParse, "too many SQL variables"); + } +} + +/* +** Recursively delete an expression tree. +*/ +static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ + assert( p!=0 ); + /* Sanity check: Assert that the IntValue is non-negative if it exists */ + assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); + if( !ExprHasProperty(p, EP_TokenOnly) ){ + /* The Expr.x union is never used at the same time as Expr.pRight */ + assert( p->x.pList==0 || p->pRight==0 ); + sqlite3ExprDelete(db, p->pLeft); + sqlite3ExprDelete(db, p->pRight); + if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); + if( ExprHasProperty(p, EP_xIsSelect) ){ + sqlite3SelectDelete(db, p->x.pSelect); + }else{ + sqlite3ExprListDelete(db, p->x.pList); + } + } + if( !ExprHasProperty(p, EP_Static) ){ + sqlite3DbFree(db, p); + } +} +SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){ + if( p ) sqlite3ExprDeleteNN(db, p); +} + +/* +** Return the number of bytes allocated for the expression structure +** passed as the first argument. This is always one of EXPR_FULLSIZE, +** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. +*/ +static int exprStructSize(Expr *p){ + if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; + if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; + return EXPR_FULLSIZE; +} + +/* +** The dupedExpr*Size() routines each return the number of bytes required +** to store a copy of an expression or expression tree. They differ in +** how much of the tree is measured. +** +** dupedExprStructSize() Size of only the Expr structure +** dupedExprNodeSize() Size of Expr + space for token +** dupedExprSize() Expr + token + subtree components +** +*************************************************************************** +** +** The dupedExprStructSize() function returns two values OR-ed together: +** (1) the space required for a copy of the Expr structure only and +** (2) the EP_xxx flags that indicate what the structure size should be. +** The return values is always one of: +** +** EXPR_FULLSIZE +** EXPR_REDUCEDSIZE | EP_Reduced +** EXPR_TOKENONLYSIZE | EP_TokenOnly +** +** The size of the structure can be found by masking the return value +** of this routine with 0xfff. The flags can be found by masking the +** return value with EP_Reduced|EP_TokenOnly. +** +** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size +** (unreduced) Expr objects as they or originally constructed by the parser. +** During expression analysis, extra information is computed and moved into +** later parts of teh Expr object and that extra information might get chopped +** off if the expression is reduced. Note also that it does not work to +** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal +** to reduce a pristine expression tree from the parser. The implementation +** of dupedExprStructSize() contain multiple assert() statements that attempt +** to enforce this constraint. +*/ +static int dupedExprStructSize(Expr *p, int flags){ + int nSize; + assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ + assert( EXPR_FULLSIZE<=0xfff ); + assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); + if( 0==flags ){ + nSize = EXPR_FULLSIZE; + }else{ + assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); + assert( !ExprHasProperty(p, EP_FromJoin) ); + assert( !ExprHasProperty(p, EP_MemToken) ); + assert( !ExprHasProperty(p, EP_NoReduce) ); + if( p->pLeft || p->x.pList ){ + nSize = EXPR_REDUCEDSIZE | EP_Reduced; + }else{ + assert( p->pRight==0 ); + nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; + } + } + return nSize; +} + +/* +** This function returns the space in bytes required to store the copy +** of the Expr structure and a copy of the Expr.u.zToken string (if that +** string is defined.) +*/ +static int dupedExprNodeSize(Expr *p, int flags){ + int nByte = dupedExprStructSize(p, flags) & 0xfff; + if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ + nByte += sqlite3Strlen30(p->u.zToken)+1; + } + return ROUND8(nByte); +} + +/* +** Return the number of bytes required to create a duplicate of the +** expression passed as the first argument. The second argument is a +** mask containing EXPRDUP_XXX flags. +** +** The value returned includes space to create a copy of the Expr struct +** itself and the buffer referred to by Expr.u.zToken, if any. +** +** If the EXPRDUP_REDUCE flag is set, then the return value includes +** space to duplicate all Expr nodes in the tree formed by Expr.pLeft +** and Expr.pRight variables (but not for any structures pointed to or +** descended from the Expr.x.pList or Expr.x.pSelect variables). +*/ +static int dupedExprSize(Expr *p, int flags){ + int nByte = 0; + if( p ){ + nByte = dupedExprNodeSize(p, flags); + if( flags&EXPRDUP_REDUCE ){ + nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); + } + } + return nByte; +} + +/* +** This function is similar to sqlite3ExprDup(), except that if pzBuffer +** is not NULL then *pzBuffer is assumed to point to a buffer large enough +** to store the copy of expression p, the copies of p->u.zToken +** (if applicable), and the copies of the p->pLeft and p->pRight expressions, +** if any. Before returning, *pzBuffer is set to the first byte past the +** portion of the buffer copied into by this function. +*/ +static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ + Expr *pNew; /* Value to return */ + u8 *zAlloc; /* Memory space from which to build Expr object */ + u32 staticFlag; /* EP_Static if space not obtained from malloc */ + + assert( db!=0 ); + assert( p ); + assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); + assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); + + /* Figure out where to write the new Expr structure. */ + if( pzBuffer ){ + zAlloc = *pzBuffer; + staticFlag = EP_Static; + }else{ + zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); + staticFlag = 0; + } + pNew = (Expr *)zAlloc; + + if( pNew ){ + /* Set nNewSize to the size allocated for the structure pointed to + ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or + ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed + ** by the copy of the p->u.zToken string (if any). + */ + const unsigned nStructSize = dupedExprStructSize(p, dupFlags); + const int nNewSize = nStructSize & 0xfff; + int nToken; + if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ + nToken = sqlite3Strlen30(p->u.zToken) + 1; + }else{ + nToken = 0; + } + if( dupFlags ){ + assert( ExprHasProperty(p, EP_Reduced)==0 ); + memcpy(zAlloc, p, nNewSize); + }else{ + u32 nSize = (u32)exprStructSize(p); + memcpy(zAlloc, p, nSize); + if( nSizeflags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); + pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); + pNew->flags |= staticFlag; + + /* Copy the p->u.zToken string, if any. */ + if( nToken ){ + char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; + memcpy(zToken, p->u.zToken, nToken); + } + + if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ + /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ + if( ExprHasProperty(p, EP_xIsSelect) ){ + pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); + }else{ + pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); + } + } + + /* Fill in pNew->pLeft and pNew->pRight. */ + if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ + zAlloc += dupedExprNodeSize(p, dupFlags); + if( ExprHasProperty(pNew, EP_Reduced) ){ + pNew->pLeft = p->pLeft ? + exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; + pNew->pRight = p->pRight ? + exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; + } + if( pzBuffer ){ + *pzBuffer = zAlloc; + } + }else{ + if( !ExprHasProperty(p, EP_TokenOnly) ){ + pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); + pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); + } + } + } + return pNew; +} + +/* +** Create and return a deep copy of the object passed as the second +** argument. If an OOM condition is encountered, NULL is returned +** and the db->mallocFailed flag set. +*/ +#ifndef SQLITE_OMIT_CTE +static With *withDup(sqlite3 *db, With *p){ + With *pRet = 0; + if( p ){ + int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); + pRet = sqlite3DbMallocZero(db, nByte); + if( pRet ){ + int i; + pRet->nCte = p->nCte; + for(i=0; inCte; i++){ + pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); + pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); + pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); + } + } + } + return pRet; +} +#else +# define withDup(x,y) 0 +#endif + +/* +** The following group of routines make deep copies of expressions, +** expression lists, ID lists, and select statements. The copies can +** be deleted (by being passed to their respective ...Delete() routines) +** without effecting the originals. +** +** The expression list, ID, and source lists return by sqlite3ExprListDup(), +** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded +** by subsequent calls to sqlite*ListAppend() routines. +** +** Any tables that the SrcList might point to are not duplicated. +** +** The flags parameter contains a combination of the EXPRDUP_XXX flags. +** If the EXPRDUP_REDUCE flag is set, then the structure returned is a +** truncated version of the usual Expr structure that will be stored as +** part of the in-memory representation of the database schema. +*/ +SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ + assert( flags==0 || flags==EXPRDUP_REDUCE ); + return p ? exprDup(db, p, flags, 0) : 0; +} +SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ + ExprList *pNew; + struct ExprList_item *pItem, *pOldItem; + int i; + assert( db!=0 ); + if( p==0 ) return 0; + pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + if( pNew==0 ) return 0; + pNew->nExpr = i = p->nExpr; + if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; inExpr; i+=i){} + pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); + if( pItem==0 ){ + sqlite3DbFree(db, pNew); + return 0; + } + pOldItem = p->a; + for(i=0; inExpr; i++, pItem++, pOldItem++){ + Expr *pOldExpr = pOldItem->pExpr; + pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); + pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); + pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); + pItem->sortOrder = pOldItem->sortOrder; + pItem->done = 0; + pItem->bSpanIsTab = pOldItem->bSpanIsTab; + pItem->u = pOldItem->u; + } + return pNew; +} + +/* +** If cursors, triggers, views and subqueries are all omitted from +** the build, then none of the following routines, except for +** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes +** called with a NULL argument. +*/ +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ + || !defined(SQLITE_OMIT_SUBQUERY) +SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ + SrcList *pNew; + int i; + int nByte; + assert( db!=0 ); + if( p==0 ) return 0; + nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); + pNew = sqlite3DbMallocRawNN(db, nByte ); + if( pNew==0 ) return 0; + pNew->nSrc = pNew->nAlloc = p->nSrc; + for(i=0; inSrc; i++){ + struct SrcList_item *pNewItem = &pNew->a[i]; + struct SrcList_item *pOldItem = &p->a[i]; + Table *pTab; + pNewItem->pSchema = pOldItem->pSchema; + pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); + pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); + pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); + pNewItem->fg = pOldItem->fg; + pNewItem->iCursor = pOldItem->iCursor; + pNewItem->addrFillSub = pOldItem->addrFillSub; + pNewItem->regReturn = pOldItem->regReturn; + if( pNewItem->fg.isIndexedBy ){ + pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); + } + pNewItem->pIBIndex = pOldItem->pIBIndex; + if( pNewItem->fg.isTabFunc ){ + pNewItem->u1.pFuncArg = + sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); + } + pTab = pNewItem->pTab = pOldItem->pTab; + if( pTab ){ + pTab->nRef++; + } + pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); + pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); + pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); + pNewItem->colUsed = pOldItem->colUsed; + } + return pNew; +} +SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ + IdList *pNew; + int i; + assert( db!=0 ); + if( p==0 ) return 0; + pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + if( pNew==0 ) return 0; + pNew->nId = p->nId; + pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); + if( pNew->a==0 ){ + sqlite3DbFree(db, pNew); + return 0; + } + /* Note that because the size of the allocation for p->a[] is not + ** necessarily a power of two, sqlite3IdListAppend() may not be called + ** on the duplicate created by this function. */ + for(i=0; inId; i++){ + struct IdList_item *pNewItem = &pNew->a[i]; + struct IdList_item *pOldItem = &p->a[i]; + pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); + pNewItem->idx = pOldItem->idx; + } + return pNew; +} +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ + Select *pNew, *pPrior; + assert( db!=0 ); + if( p==0 ) return 0; + pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); + if( pNew==0 ) return 0; + pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); + pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); + pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); + pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); + pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); + pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); + pNew->op = p->op; + pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); + if( pPrior ) pPrior->pNext = pNew; + pNew->pNext = 0; + pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); + pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); + pNew->iLimit = 0; + pNew->iOffset = 0; + pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; + pNew->addrOpenEphm[0] = -1; + pNew->addrOpenEphm[1] = -1; + pNew->nSelectRow = p->nSelectRow; + pNew->pWith = withDup(db, p->pWith); + sqlite3SelectSetName(pNew, p->zSelName); + return pNew; +} +#else +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ + assert( p==0 ); + return 0; +} +#endif + + +/* +** Add a new element to the end of an expression list. If pList is +** initially NULL, then create a new expression list. +** +** If a memory allocation error occurs, the entire list is freed and +** NULL is returned. If non-NULL is returned, then it is guaranteed +** that the new entry was successfully appended. +*/ +SQLITE_PRIVATE ExprList *sqlite3ExprListAppend( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to append. Might be NULL */ + Expr *pExpr /* Expression to be appended. Might be NULL */ +){ + sqlite3 *db = pParse->db; + assert( db!=0 ); + if( pList==0 ){ + pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); + if( pList==0 ){ + goto no_mem; + } + pList->nExpr = 0; + pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); + if( pList->a==0 ) goto no_mem; + }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ + struct ExprList_item *a; + assert( pList->nExpr>0 ); + a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); + if( a==0 ){ + goto no_mem; + } + pList->a = a; + } + assert( pList->a!=0 ); + if( 1 ){ + struct ExprList_item *pItem = &pList->a[pList->nExpr++]; + memset(pItem, 0, sizeof(*pItem)); + pItem->pExpr = pExpr; + } + return pList; + +no_mem: + /* Avoid leaking memory if malloc has failed. */ + sqlite3ExprDelete(db, pExpr); + sqlite3ExprListDelete(db, pList); + return 0; +} + +/* +** Set the sort order for the last element on the given ExprList. +*/ +SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ + if( p==0 ) return; + assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); + assert( p->nExpr>0 ); + if( iSortOrder<0 ){ + assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); + return; + } + p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; +} + +/* +** Set the ExprList.a[].zName element of the most recently added item +** on the expression list. +** +** pList might be NULL following an OOM error. But pName should never be +** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag +** is set. +*/ +SQLITE_PRIVATE void sqlite3ExprListSetName( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to add the span. */ + Token *pName, /* Name to be added */ + int dequote /* True to cause the name to be dequoted */ +){ + assert( pList!=0 || pParse->db->mallocFailed!=0 ); + if( pList ){ + struct ExprList_item *pItem; + assert( pList->nExpr>0 ); + pItem = &pList->a[pList->nExpr-1]; + assert( pItem->zName==0 ); + pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); + if( dequote ) sqlite3Dequote(pItem->zName); + } +} + +/* +** Set the ExprList.a[].zSpan element of the most recently added item +** on the expression list. +** +** pList might be NULL following an OOM error. But pSpan should never be +** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag +** is set. +*/ +SQLITE_PRIVATE void sqlite3ExprListSetSpan( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to add the span. */ + ExprSpan *pSpan /* The span to be added */ +){ + sqlite3 *db = pParse->db; + assert( pList!=0 || db->mallocFailed!=0 ); + if( pList ){ + struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; + assert( pList->nExpr>0 ); + assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); + sqlite3DbFree(db, pItem->zSpan); + pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, + (int)(pSpan->zEnd - pSpan->zStart)); + } +} + +/* +** If the expression list pEList contains more than iLimit elements, +** leave an error message in pParse. +*/ +SQLITE_PRIVATE void sqlite3ExprListCheckLength( + Parse *pParse, + ExprList *pEList, + const char *zObject +){ + int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; + testcase( pEList && pEList->nExpr==mx ); + testcase( pEList && pEList->nExpr==mx+1 ); + if( pEList && pEList->nExpr>mx ){ + sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); + } +} + +/* +** Delete an entire expression list. +*/ +static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ + int i; + struct ExprList_item *pItem; + assert( pList->a!=0 || pList->nExpr==0 ); + for(pItem=pList->a, i=0; inExpr; i++, pItem++){ + sqlite3ExprDelete(db, pItem->pExpr); + sqlite3DbFree(db, pItem->zName); + sqlite3DbFree(db, pItem->zSpan); + } + sqlite3DbFree(db, pList->a); + sqlite3DbFree(db, pList); +} +SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ + if( pList ) exprListDeleteNN(db, pList); +} + +/* +** Return the bitwise-OR of all Expr.flags fields in the given +** ExprList. +*/ +SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){ + int i; + u32 m = 0; + if( pList ){ + for(i=0; inExpr; i++){ + Expr *pExpr = pList->a[i].pExpr; + assert( pExpr!=0 ); + m |= pExpr->flags; + } + } + return m; +} + +/* +** These routines are Walker callbacks used to check expressions to +** see if they are "constant" for some definition of constant. The +** Walker.eCode value determines the type of "constant" we are looking +** for. +** +** These callback routines are used to implement the following: +** +** sqlite3ExprIsConstant() pWalker->eCode==1 +** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 +** sqlite3ExprIsTableConstant() pWalker->eCode==3 +** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 +** +** In all cases, the callbacks set Walker.eCode=0 and abort if the expression +** is found to not be a constant. +** +** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions +** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing +** an existing schema and 4 when processing a new statement. A bound +** parameter raises an error for new statements, but is silently converted +** to NULL for existing schemas. This allows sqlite_master tables that +** contain a bound parameter because they were generated by older versions +** of SQLite to be parsed by newer versions of SQLite without raising a +** malformed schema error. +*/ +static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ + + /* If pWalker->eCode is 2 then any term of the expression that comes from + ** the ON or USING clauses of a left join disqualifies the expression + ** from being considered constant. */ + if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ + pWalker->eCode = 0; + return WRC_Abort; + } + + switch( pExpr->op ){ + /* Consider functions to be constant if all their arguments are constant + ** and either pWalker->eCode==4 or 5 or the function has the + ** SQLITE_FUNC_CONST flag. */ + case TK_FUNCTION: + if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ + return WRC_Continue; + }else{ + pWalker->eCode = 0; + return WRC_Abort; + } + case TK_ID: + case TK_COLUMN: + case TK_AGG_FUNCTION: + case TK_AGG_COLUMN: + testcase( pExpr->op==TK_ID ); + testcase( pExpr->op==TK_COLUMN ); + testcase( pExpr->op==TK_AGG_FUNCTION ); + testcase( pExpr->op==TK_AGG_COLUMN ); + if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ + return WRC_Continue; + }else{ + pWalker->eCode = 0; + return WRC_Abort; + } + case TK_VARIABLE: + if( pWalker->eCode==5 ){ + /* Silently convert bound parameters that appear inside of CREATE + ** statements into a NULL when parsing the CREATE statement text out + ** of the sqlite_master table */ + pExpr->op = TK_NULL; + }else if( pWalker->eCode==4 ){ + /* A bound parameter in a CREATE statement that originates from + ** sqlite3_prepare() causes an error */ + pWalker->eCode = 0; + return WRC_Abort; + } + /* Fall through */ + default: + testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ + testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ + return WRC_Continue; + } +} +static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ + UNUSED_PARAMETER(NotUsed); + pWalker->eCode = 0; + return WRC_Abort; +} +static int exprIsConst(Expr *p, int initFlag, int iCur){ + Walker w; + memset(&w, 0, sizeof(w)); + w.eCode = initFlag; + w.xExprCallback = exprNodeIsConstant; + w.xSelectCallback = selectNodeIsConstant; + w.u.iCur = iCur; + sqlite3WalkExpr(&w, p); + return w.eCode; +} + +/* +** Walk an expression tree. Return non-zero if the expression is constant +** and 0 if it involves variables or function calls. +** +** For the purposes of this function, a double-quoted string (ex: "abc") +** is considered a variable but a single-quoted string (ex: 'abc') is +** a constant. +*/ +SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ + return exprIsConst(p, 1, 0); +} + +/* +** Walk an expression tree. Return non-zero if the expression is constant +** that does no originate from the ON or USING clauses of a join. +** Return 0 if it involves variables or function calls or terms from +** an ON or USING clause. +*/ +SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ + return exprIsConst(p, 2, 0); +} + +/* +** Walk an expression tree. Return non-zero if the expression is constant +** for any single row of the table with cursor iCur. In other words, the +** expression must not refer to any non-deterministic function nor any +** table other than iCur. +*/ +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ + return exprIsConst(p, 3, iCur); +} + +/* +** Walk an expression tree. Return non-zero if the expression is constant +** or a function call with constant arguments. Return and 0 if there +** are any variables. +** +** For the purposes of this function, a double-quoted string (ex: "abc") +** is considered a variable but a single-quoted string (ex: 'abc') is +** a constant. +*/ +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ + assert( isInit==0 || isInit==1 ); + return exprIsConst(p, 4+isInit, 0); +} + +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* +** Walk an expression tree. Return 1 if the expression contains a +** subquery of some kind. Return 0 if there are no subqueries. +*/ +SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){ + Walker w; + memset(&w, 0, sizeof(w)); + w.eCode = 1; + w.xExprCallback = sqlite3ExprWalkNoop; + w.xSelectCallback = selectNodeIsConstant; + sqlite3WalkExpr(&w, p); + return w.eCode==0; +} +#endif + +/* +** If the expression p codes a constant integer that is small enough +** to fit in a 32-bit integer, return 1 and put the value of the integer +** in *pValue. If the expression is not an integer or if it is too big +** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. +*/ +SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){ + int rc = 0; + + /* If an expression is an integer literal that fits in a signed 32-bit + ** integer, then the EP_IntValue flag will have already been set */ + assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 + || sqlite3GetInt32(p->u.zToken, &rc)==0 ); + + if( p->flags & EP_IntValue ){ + *pValue = p->u.iValue; + return 1; + } + switch( p->op ){ + case TK_UPLUS: { + rc = sqlite3ExprIsInteger(p->pLeft, pValue); + break; + } + case TK_UMINUS: { + int v; + if( sqlite3ExprIsInteger(p->pLeft, &v) ){ + assert( v!=(-2147483647-1) ); + *pValue = -v; + rc = 1; + } + break; + } + default: break; + } + return rc; +} + +/* +** Return FALSE if there is no chance that the expression can be NULL. +** +** If the expression might be NULL or if the expression is too complex +** to tell return TRUE. +** +** This routine is used as an optimization, to skip OP_IsNull opcodes +** when we know that a value cannot be NULL. Hence, a false positive +** (returning TRUE when in fact the expression can never be NULL) might +** be a small performance hit but is otherwise harmless. On the other +** hand, a false negative (returning FALSE when the result could be NULL) +** will likely result in an incorrect answer. So when in doubt, return +** TRUE. +*/ +SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){ + u8 op; + while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } + op = p->op; + if( op==TK_REGISTER ) op = p->op2; + switch( op ){ + case TK_INTEGER: + case TK_STRING: + case TK_FLOAT: + case TK_BLOB: + return 0; + case TK_COLUMN: + assert( p->pTab!=0 ); + return ExprHasProperty(p, EP_CanBeNull) || + (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); + default: + return 1; + } +} + +/* +** Return TRUE if the given expression is a constant which would be +** unchanged by OP_Affinity with the affinity given in the second +** argument. +** +** This routine is used to determine if the OP_Affinity operation +** can be omitted. When in doubt return FALSE. A false negative +** is harmless. A false positive, however, can result in the wrong +** answer. +*/ +SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ + u8 op; + if( aff==SQLITE_AFF_BLOB ) return 1; + while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } + op = p->op; + if( op==TK_REGISTER ) op = p->op2; + switch( op ){ + case TK_INTEGER: { + return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; + } + case TK_FLOAT: { + return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; + } + case TK_STRING: { + return aff==SQLITE_AFF_TEXT; + } + case TK_BLOB: { + return 1; + } + case TK_COLUMN: { + assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ + return p->iColumn<0 + && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); + } + default: { + return 0; + } + } +} + +/* +** Return TRUE if the given string is a row-id column name. +*/ +SQLITE_PRIVATE int sqlite3IsRowid(const char *z){ + if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; + if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; + if( sqlite3StrICmp(z, "OID")==0 ) return 1; + return 0; +} + +/* +** pX is the RHS of an IN operator. If pX is a SELECT statement +** that can be simplified to a direct table access, then return +** a pointer to the SELECT statement. If pX is not a SELECT statement, +** or if the SELECT statement needs to be manifested into a transient +** table, then return NULL. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +static Select *isCandidateForInOpt(Expr *pX){ + Select *p; + SrcList *pSrc; + ExprList *pEList; + Expr *pRes; + Table *pTab; + if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ + if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ + p = pX->x.pSelect; + if( p->pPrior ) return 0; /* Not a compound SELECT */ + if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ + testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); + testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); + return 0; /* No DISTINCT keyword and no aggregate functions */ + } + assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ + if( p->pLimit ) return 0; /* Has no LIMIT clause */ + assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ + if( p->pWhere ) return 0; /* Has no WHERE clause */ + pSrc = p->pSrc; + assert( pSrc!=0 ); + if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ + if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ + pTab = pSrc->a[0].pTab; + assert( pTab!=0 ); + assert( pTab->pSelect==0 ); /* FROM clause is not a view */ + if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ + pEList = p->pEList; + if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ + pRes = pEList->a[0].pExpr; + if( pRes->op!=TK_COLUMN ) return 0; /* Result is a column */ + assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ + return p; +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +/* +** Code an OP_Once instruction and allocate space for its flag. Return the +** address of the new instruction. +*/ +SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){ + Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ + return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); +} + +/* +** Generate code that checks the left-most column of index table iCur to see if +** it contains any NULL entries. Cause the register at regHasNull to be set +** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull +** to be set to NULL if iCur contains one or more NULL values. +*/ +static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ + int addr1; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); + addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); + sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); + VdbeComment((v, "first_entry_in(%d)", iCur)); + sqlite3VdbeJumpHere(v, addr1); +} + + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** The argument is an IN operator with a list (not a subquery) on the +** right-hand side. Return TRUE if that list is constant. +*/ +static int sqlite3InRhsIsConstant(Expr *pIn){ + Expr *pLHS; + int res; + assert( !ExprHasProperty(pIn, EP_xIsSelect) ); + pLHS = pIn->pLeft; + pIn->pLeft = 0; + res = sqlite3ExprIsConstant(pIn); + pIn->pLeft = pLHS; + return res; +} +#endif + +/* +** This function is used by the implementation of the IN (...) operator. +** The pX parameter is the expression on the RHS of the IN operator, which +** might be either a list of expressions or a subquery. +** +** The job of this routine is to find or create a b-tree object that can +** be used either to test for membership in the RHS set or to iterate through +** all members of the RHS set, skipping duplicates. +** +** A cursor is opened on the b-tree object that is the RHS of the IN operator +** and pX->iTable is set to the index of that cursor. +** +** The returned value of this function indicates the b-tree type, as follows: +** +** IN_INDEX_ROWID - The cursor was opened on a database table. +** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. +** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. +** IN_INDEX_EPH - The cursor was opened on a specially created and +** populated epheremal table. +** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be +** implemented as a sequence of comparisons. +** +** An existing b-tree might be used if the RHS expression pX is a simple +** subquery such as: +** +** SELECT FROM +** +** If the RHS of the IN operator is a list or a more complex subquery, then +** an ephemeral table might need to be generated from the RHS and then +** pX->iTable made to point to the ephemeral table instead of an +** existing table. +** +** The inFlags parameter must contain exactly one of the bits +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a +** fast membership test. When the IN_INDEX_LOOP bit is set, the +** IN index will be used to loop over all values of the RHS of the +** IN operator. +** +** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate +** through the set members) then the b-tree must not contain duplicates. +** An epheremal table must be used unless the selected is guaranteed +** to be unique - either because it is an INTEGER PRIMARY KEY or it +** has a UNIQUE constraint or UNIQUE index. +** +** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used +** for fast set membership tests) then an epheremal table must +** be used unless is an INTEGER PRIMARY KEY or an index can +** be found with as its left-most column. +** +** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and +** if the RHS of the IN operator is a list (not a subquery) then this +** routine might decide that creating an ephemeral b-tree for membership +** testing is too expensive and return IN_INDEX_NOOP. In that case, the +** calling routine should implement the IN operator using a sequence +** of Eq or Ne comparison operations. +** +** When the b-tree is being used for membership tests, the calling function +** might need to know whether or not the RHS side of the IN operator +** contains a NULL. If prRhsHasNull is not a NULL pointer and +** if there is any chance that the (...) might contain a NULL value at +** runtime, then a register is allocated and the register number written +** to *prRhsHasNull. If there is no chance that the (...) contains a +** NULL value, then *prRhsHasNull is left unchanged. +** +** If a register is allocated and its location stored in *prRhsHasNull, then +** the value in that register will be NULL if the b-tree contains one or more +** NULL values, and it will be some non-NULL value if the b-tree contains no +** NULL values. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ + Select *p; /* SELECT to the right of IN operator */ + int eType = 0; /* Type of RHS table. IN_INDEX_* */ + int iTab = pParse->nTab++; /* Cursor of the RHS table */ + int mustBeUnique; /* True if RHS must be unique */ + Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ + + assert( pX->op==TK_IN ); + mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; + + /* Check to see if an existing table or index can be used to + ** satisfy the query. This is preferable to generating a new + ** ephemeral table. + */ + if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ + sqlite3 *db = pParse->db; /* Database connection */ + Table *pTab; /* Table
    . */ + Expr *pExpr; /* Expression */ + i16 iCol; /* Index of column */ + i16 iDb; /* Database idx for pTab */ + + assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ + assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ + assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ + pTab = p->pSrc->a[0].pTab; + pExpr = p->pEList->a[0].pExpr; + iCol = (i16)pExpr->iColumn; + + /* Code an OP_Transaction and OP_TableLock for
    . */ + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + sqlite3CodeVerifySchema(pParse, iDb); + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + + /* This function is only called from two places. In both cases the vdbe + ** has already been allocated. So assume sqlite3GetVdbe() is always + ** successful here. + */ + assert(v); + if( iCol<0 ){ + int iAddr = sqlite3CodeOnce(pParse); + VdbeCoverage(v); + + sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); + eType = IN_INDEX_ROWID; + + sqlite3VdbeJumpHere(v, iAddr); + }else{ + Index *pIdx; /* Iterator variable */ + + /* The collation sequence used by the comparison. If an index is to + ** be used in place of a temp-table, it must be ordered according + ** to this collation sequence. */ + CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); + + /* Check that the affinity that will be used to perform the + ** comparison is the same as the affinity of the column. If + ** it is not, it is not possible to use any index. + */ + int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity); + + for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ + if( (pIdx->aiColumn[0]==iCol) + && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq + && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx))) + ){ + int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + VdbeComment((v, "%s", pIdx->zName)); + assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); + eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; + + if( prRhsHasNull && !pTab->aCol[iCol].notNull ){ + *prRhsHasNull = ++pParse->nMem; + sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); + } + sqlite3VdbeJumpHere(v, iAddr); + } + } + } + } + + /* If no preexisting index is available for the IN clause + ** and IN_INDEX_NOOP is an allowed reply + ** and the RHS of the IN operator is a list, not a subquery + ** and the RHS is not contant or has two or fewer terms, + ** then it is not worth creating an ephemeral table to evaluate + ** the IN operator so return IN_INDEX_NOOP. + */ + if( eType==0 + && (inFlags & IN_INDEX_NOOP_OK) + && !ExprHasProperty(pX, EP_xIsSelect) + && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) + ){ + eType = IN_INDEX_NOOP; + } + + + if( eType==0 ){ + /* Could not find an existing table or index to use as the RHS b-tree. + ** We will have to generate an ephemeral table to do the job. + */ + u32 savedNQueryLoop = pParse->nQueryLoop; + int rMayHaveNull = 0; + eType = IN_INDEX_EPH; + if( inFlags & IN_INDEX_LOOP ){ + pParse->nQueryLoop = 0; + if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ + eType = IN_INDEX_ROWID; + } + }else if( prRhsHasNull ){ + *prRhsHasNull = rMayHaveNull = ++pParse->nMem; + } + sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); + pParse->nQueryLoop = savedNQueryLoop; + }else{ + pX->iTable = iTab; + } + return eType; +} +#endif + +/* +** Generate code for scalar subqueries used as a subquery expression, EXISTS, +** or IN operators. Examples: +** +** (SELECT a FROM b) -- subquery +** EXISTS (SELECT a FROM b) -- EXISTS subquery +** x IN (4,5,11) -- IN operator with list on right-hand side +** x IN (SELECT a FROM b) -- IN operator with subquery on the right +** +** The pExpr parameter describes the expression that contains the IN +** operator or subquery. +** +** If parameter isRowid is non-zero, then expression pExpr is guaranteed +** to be of the form " IN (?, ?, ?)", where is a reference +** to some integer key column of a table B-Tree. In this case, use an +** intkey B-Tree to store the set of IN(...) values instead of the usual +** (slower) variable length keys B-Tree. +** +** If rMayHaveNull is non-zero, that means that the operation is an IN +** (not a SELECT or EXISTS) and that the RHS might contains NULLs. +** All this routine does is initialize the register given by rMayHaveNull +** to NULL. Calling routines will take care of changing this register +** value to non-NULL if the RHS is NULL-free. +** +** For a SELECT or EXISTS operator, return the register that holds the +** result. For IN operators or if an error occurs, the return value is 0. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +SQLITE_PRIVATE int sqlite3CodeSubselect( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ + int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ + int isRowid /* If true, LHS of IN operator is a rowid */ +){ + int jmpIfDynamic = -1; /* One-time test address */ + int rReg = 0; /* Register storing resulting */ + Vdbe *v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return 0; + sqlite3ExprCachePush(pParse); + + /* This code must be run in its entirety every time it is encountered + ** if any of the following is true: + ** + ** * The right-hand side is a correlated subquery + ** * The right-hand side is an expression list containing variables + ** * We are inside a trigger + ** + ** If all of the above are false, then we can run this code just once + ** save the results, and reuse the same result on subsequent invocations. + */ + if( !ExprHasProperty(pExpr, EP_VarSelect) ){ + jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); + } + +#ifndef SQLITE_OMIT_EXPLAIN + if( pParse->explain==2 ){ + char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", + jmpIfDynamic>=0?"":"CORRELATED ", + pExpr->op==TK_IN?"LIST":"SCALAR", + pParse->iNextSelectId + ); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +#endif + + switch( pExpr->op ){ + case TK_IN: { + char affinity; /* Affinity of the LHS of the IN */ + int addr; /* Address of OP_OpenEphemeral instruction */ + Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ + KeyInfo *pKeyInfo = 0; /* Key information */ + + affinity = sqlite3ExprAffinity(pLeft); + + /* Whether this is an 'x IN(SELECT...)' or an 'x IN()' + ** expression it is handled the same way. An ephemeral table is + ** filled with single-field index keys representing the results + ** from the SELECT or the . + ** + ** If the 'x' expression is a column value, or the SELECT... + ** statement returns a column value, then the affinity of that + ** column is used to build the index keys. If both 'x' and the + ** SELECT... statement are columns, then numeric affinity is used + ** if either column has NUMERIC or INTEGER affinity. If neither + ** 'x' nor the SELECT... statement are columns, then numeric affinity + ** is used. + */ + pExpr->iTable = pParse->nTab++; + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); + pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1); + + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + /* Case 1: expr IN (SELECT ...) + ** + ** Generate code to write the results of the select into the temporary + ** table allocated and opened above. + */ + Select *pSelect = pExpr->x.pSelect; + SelectDest dest; + ExprList *pEList; + + assert( !isRowid ); + sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); + dest.affSdst = (u8)affinity; + assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); + pSelect->iLimit = 0; + testcase( pSelect->selFlags & SF_Distinct ); + testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ + if( sqlite3Select(pParse, pSelect, &dest) ){ + sqlite3KeyInfoUnref(pKeyInfo); + return 0; + } + pEList = pSelect->pEList; + assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ + assert( pEList!=0 ); + assert( pEList->nExpr>0 ); + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); + pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, + pEList->a[0].pExpr); + }else if( ALWAYS(pExpr->x.pList!=0) ){ + /* Case 2: expr IN (exprlist) + ** + ** For each expression, build an index key from the evaluation and + ** store it in the temporary table. If is a column, then use + ** that columns affinity when building index keys. If is not + ** a column, use numeric affinity. + */ + int i; + ExprList *pList = pExpr->x.pList; + struct ExprList_item *pItem; + int r1, r2, r3; + + if( !affinity ){ + affinity = SQLITE_AFF_BLOB; + } + if( pKeyInfo ){ + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); + pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); + } + + /* Loop through each expression in . */ + r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3GetTempReg(pParse); + if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); + for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ + Expr *pE2 = pItem->pExpr; + int iValToIns; + + /* If the expression is not constant then we will need to + ** disable the test that was generated above that makes sure + ** this code only executes once. Because for a non-constant + ** expression we need to rerun this code each time. + */ + if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ + sqlite3VdbeChangeToNoop(v, jmpIfDynamic); + jmpIfDynamic = -1; + } + + /* Evaluate the expression and insert it into the temp table */ + if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ + sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); + }else{ + r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); + if( isRowid ){ + sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, + sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); + }else{ + sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); + sqlite3ExprCacheAffinityChange(pParse, r3, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); + } + } + } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); + } + if( pKeyInfo ){ + sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); + } + break; + } + + case TK_EXISTS: + case TK_SELECT: + default: { + /* If this has to be a scalar SELECT. Generate code to put the + ** value of this select in a memory cell and record the number + ** of the memory cell in iColumn. If this is an EXISTS, write + ** an integer 0 (not exists) or 1 (exists) into a memory cell + ** and record that memory cell in iColumn. + */ + Select *pSel; /* SELECT statement to encode */ + SelectDest dest; /* How to deal with SELECt result */ + + testcase( pExpr->op==TK_EXISTS ); + testcase( pExpr->op==TK_SELECT ); + assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); + + assert( ExprHasProperty(pExpr, EP_xIsSelect) ); + pSel = pExpr->x.pSelect; + sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); + if( pExpr->op==TK_SELECT ){ + dest.eDest = SRT_Mem; + dest.iSdst = dest.iSDParm; + sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm); + VdbeComment((v, "Init subquery result")); + }else{ + dest.eDest = SRT_Exists; + sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); + VdbeComment((v, "Init EXISTS result")); + } + sqlite3ExprDelete(pParse->db, pSel->pLimit); + pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, + &sqlite3IntTokens[1]); + pSel->iLimit = 0; + pSel->selFlags &= ~SF_MultiValue; + if( sqlite3Select(pParse, pSel, &dest) ){ + return 0; + } + rReg = dest.iSDParm; + ExprSetVVAProperty(pExpr, EP_NoReduce); + break; + } + } + + if( rHasNullFlag ){ + sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); + } + + if( jmpIfDynamic>=0 ){ + sqlite3VdbeJumpHere(v, jmpIfDynamic); + } + sqlite3ExprCachePop(pParse); + + return rReg; +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Generate code for an IN expression. +** +** x IN (SELECT ...) +** x IN (value, value, ...) +** +** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) +** is an array of zero or more values. The expression is true if the LHS is +** contained within the RHS. The value of the expression is unknown (NULL) +** if the LHS is NULL or if the LHS is not contained within the RHS and the +** RHS contains one or more NULL values. +** +** This routine generates code that jumps to destIfFalse if the LHS is not +** contained within the RHS. If due to NULLs we cannot determine if the LHS +** is contained in the RHS then jump to destIfNull. If the LHS is contained +** within the RHS then fall through. +*/ +static void sqlite3ExprCodeIN( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* The IN expression */ + int destIfFalse, /* Jump here if LHS is not contained in the RHS */ + int destIfNull /* Jump here if the results are unknown due to NULLs */ +){ + int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ + char affinity; /* Comparison affinity to use */ + int eType; /* Type of the RHS */ + int r1; /* Temporary use register */ + Vdbe *v; /* Statement under construction */ + + /* Compute the RHS. After this step, the table with cursor + ** pExpr->iTable will contains the values that make up the RHS. + */ + v = pParse->pVdbe; + assert( v!=0 ); /* OOM detected prior to this routine */ + VdbeNoopComment((v, "begin IN expr")); + eType = sqlite3FindInIndex(pParse, pExpr, + IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, + destIfFalse==destIfNull ? 0 : &rRhsHasNull); + + /* Figure out the affinity to use to create a key from the results + ** of the expression. affinityStr stores a static string suitable for + ** P4 of OP_MakeRecord. + */ + affinity = comparisonAffinity(pExpr); + + /* Code the LHS, the from " IN (...)". + */ + sqlite3ExprCachePush(pParse); + r1 = sqlite3GetTempReg(pParse); + sqlite3ExprCode(pParse, pExpr->pLeft, r1); + + /* If sqlite3FindInIndex() did not find or create an index that is + ** suitable for evaluating the IN operator, then evaluate using a + ** sequence of comparisons. + */ + if( eType==IN_INDEX_NOOP ){ + ExprList *pList = pExpr->x.pList; + CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); + int labelOk = sqlite3VdbeMakeLabel(v); + int r2, regToFree; + int regCkNull = 0; + int ii; + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + if( destIfNull!=destIfFalse ){ + regCkNull = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); + } + for(ii=0; iinExpr; ii++){ + r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); + if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ + sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); + } + if( iinExpr-1 || destIfNull!=destIfFalse ){ + sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, + (void*)pColl, P4_COLLSEQ); + VdbeCoverageIf(v, iinExpr-1); + VdbeCoverageIf(v, ii==pList->nExpr-1); + sqlite3VdbeChangeP5(v, affinity); + }else{ + assert( destIfNull==destIfFalse ); + sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, + (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); + sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); + } + sqlite3ReleaseTempReg(pParse, regToFree); + } + if( regCkNull ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); + sqlite3VdbeGoto(v, destIfFalse); + } + sqlite3VdbeResolveLabel(v, labelOk); + sqlite3ReleaseTempReg(pParse, regCkNull); + }else{ + + /* If the LHS is NULL, then the result is either false or NULL depending + ** on whether the RHS is empty or not, respectively. + */ + if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ + if( destIfNull==destIfFalse ){ + /* Shortcut for the common case where the false and NULL outcomes are + ** the same. */ + sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); + }else{ + int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); + VdbeCoverage(v); + sqlite3VdbeGoto(v, destIfNull); + sqlite3VdbeJumpHere(v, addr1); + } + } + + if( eType==IN_INDEX_ROWID ){ + /* In this case, the RHS is the ROWID of table b-tree + */ + sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); + VdbeCoverage(v); + }else{ + /* In this case, the RHS is an index b-tree. + */ + sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); + + /* If the set membership test fails, then the result of the + ** "x IN (...)" expression must be either 0 or NULL. If the set + ** contains no NULL values, then the result is 0. If the set + ** contains one or more NULL values, then the result of the + ** expression is also NULL. + */ + assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); + if( rRhsHasNull==0 ){ + /* This branch runs if it is known at compile time that the RHS + ** cannot contain NULL values. This happens as the result + ** of a "NOT NULL" constraint in the database schema. + ** + ** Also run this branch if NULL is equivalent to FALSE + ** for this particular IN operator. + */ + sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); + VdbeCoverage(v); + }else{ + /* In this branch, the RHS of the IN might contain a NULL and + ** the presence of a NULL on the RHS makes a difference in the + ** outcome. + */ + int addr1; + + /* First check to see if the LHS is contained in the RHS. If so, + ** then the answer is TRUE the presence of NULLs in the RHS does + ** not matter. If the LHS is not contained in the RHS, then the + ** answer is NULL if the RHS contains NULLs and the answer is + ** FALSE if the RHS is NULL-free. + */ + addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); + VdbeCoverage(v); + sqlite3VdbeGoto(v, destIfFalse); + sqlite3VdbeJumpHere(v, addr1); + } + } + } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ExprCachePop(pParse); + VdbeComment((v, "end IN expr")); +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** Generate an instruction that will put the floating point +** value described by z[0..n-1] into register iMem. +** +** The z[] string will probably not be zero-terminated. But the +** z[n] character is guaranteed to be something that does not look +** like the continuation of the number. +*/ +static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ + if( ALWAYS(z!=0) ){ + double value; + sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); + assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ + if( negateFlag ) value = -value; + sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); + } +} +#endif + + +/* +** Generate an instruction that will put the integer describe by +** text z[0..n-1] into register iMem. +** +** Expr.u.zToken is always UTF8 and zero-terminated. +*/ +static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ + Vdbe *v = pParse->pVdbe; + if( pExpr->flags & EP_IntValue ){ + int i = pExpr->u.iValue; + assert( i>=0 ); + if( negFlag ) i = -i; + sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); + }else{ + int c; + i64 value; + const char *z = pExpr->u.zToken; + assert( z!=0 ); + c = sqlite3DecOrHexToI64(z, &value); + if( c==0 || (c==2 && negFlag) ){ + if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } + sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); + }else{ +#ifdef SQLITE_OMIT_FLOATING_POINT + sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); +#else +#ifndef SQLITE_OMIT_HEX_INTEGER + if( sqlite3_strnicmp(z,"0x",2)==0 ){ + sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); + }else +#endif + { + codeReal(v, z, negFlag, iMem); + } +#endif + } + } +} + +#if defined(SQLITE_DEBUG) +/* +** Verify the consistency of the column cache +*/ +static int cacheIsValid(Parse *pParse){ + int i, n; + for(i=n=0; iaColCache[i].iReg>0 ) n++; + } + return n==pParse->nColCache; +} +#endif + +/* +** Clear a cache entry. +*/ +static void cacheEntryClear(Parse *pParse, struct yColCache *p){ + if( p->tempReg ){ + if( pParse->nTempRegaTempReg) ){ + pParse->aTempReg[pParse->nTempReg++] = p->iReg; + } + p->tempReg = 0; + } + p->iReg = 0; + pParse->nColCache--; + assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); +} + + +/* +** Record in the column cache that a particular column from a +** particular table is stored in a particular register. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ + int i; + int minLru; + int idxLru; + struct yColCache *p; + + /* Unless an error has occurred, register numbers are always positive. */ + assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); + assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ + + /* The SQLITE_ColumnCache flag disables the column cache. This is used + ** for testing only - to verify that SQLite always gets the same answer + ** with and without the column cache. + */ + if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; + + /* First replace any existing entry. + ** + ** Actually, the way the column cache is currently used, we are guaranteed + ** that the object will never already be in cache. Verify this guarantee. + */ +#ifndef NDEBUG + for(i=0, p=pParse->aColCache; iiReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); + } +#endif + + /* Find an empty slot and replace it */ + for(i=0, p=pParse->aColCache; iiReg==0 ){ + p->iLevel = pParse->iCacheLevel; + p->iTable = iTab; + p->iColumn = iCol; + p->iReg = iReg; + p->tempReg = 0; + p->lru = pParse->iCacheCnt++; + pParse->nColCache++; + assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); + return; + } + } + + /* Replace the last recently used */ + minLru = 0x7fffffff; + idxLru = -1; + for(i=0, p=pParse->aColCache; ilrulru; + } + } + if( ALWAYS(idxLru>=0) ){ + p = &pParse->aColCache[idxLru]; + p->iLevel = pParse->iCacheLevel; + p->iTable = iTab; + p->iColumn = iCol; + p->iReg = iReg; + p->tempReg = 0; + p->lru = pParse->iCacheCnt++; + assert( cacheIsValid(pParse) ); + return; + } +} + +/* +** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. +** Purge the range of registers from the column cache. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ + struct yColCache *p; + if( iReg<=0 || pParse->nColCache==0 ) return; + p = &pParse->aColCache[SQLITE_N_COLCACHE-1]; + while(1){ + if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p); + if( p==pParse->aColCache ) break; + p--; + } +} + +/* +** Remember the current column cache context. Any new entries added +** added to the column cache after this call are removed when the +** corresponding pop occurs. +*/ +SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){ + pParse->iCacheLevel++; +#ifdef SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("PUSH to %d\n", pParse->iCacheLevel); + } +#endif +} + +/* +** Remove from the column cache any entries that were added since the +** the previous sqlite3ExprCachePush operation. In other words, restore +** the cache to the state it was in prior the most recent Push. +*/ +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){ + int i; + struct yColCache *p; + assert( pParse->iCacheLevel>=1 ); + pParse->iCacheLevel--; +#ifdef SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("POP to %d\n", pParse->iCacheLevel); + } +#endif + for(i=0, p=pParse->aColCache; iiReg && p->iLevel>pParse->iCacheLevel ){ + cacheEntryClear(pParse, p); + } + } +} + +/* +** When a cached column is reused, make sure that its register is +** no longer available as a temp register. ticket #3879: that same +** register might be in the cache in multiple places, so be sure to +** get them all. +*/ +static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg==iReg ){ + p->tempReg = 0; + } + } +} + +/* Generate code that will load into register regOut a value that is +** appropriate for the iIdxCol-th column of index pIdx. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn( + Parse *pParse, /* The parsing context */ + Index *pIdx, /* The index whose column is to be loaded */ + int iTabCur, /* Cursor pointing to a table row */ + int iIdxCol, /* The column of the index to be loaded */ + int regOut /* Store the index column value in this register */ +){ + i16 iTabCol = pIdx->aiColumn[iIdxCol]; + if( iTabCol==XN_EXPR ){ + assert( pIdx->aColExpr ); + assert( pIdx->aColExpr->nExpr>iIdxCol ); + pParse->iSelfTab = iTabCur; + sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); + }else{ + sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, + iTabCol, regOut); + } +} + +/* +** Generate code to extract the value of the iCol-th column of a table. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable( + Vdbe *v, /* The VDBE under construction */ + Table *pTab, /* The table containing the value */ + int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ + int iCol, /* Index of the column to extract */ + int regOut /* Extract the value into this register */ +){ + if( iCol<0 || iCol==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); + }else{ + int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; + int x = iCol; + if( !HasRowid(pTab) ){ + x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); + } + sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); + } + if( iCol>=0 ){ + sqlite3ColumnDefault(v, pTab, iCol, regOut); + } +} + +/* +** Generate code that will extract the iColumn-th column from +** table pTab and store the column value in a register. +** +** An effort is made to store the column value in register iReg. This +** is not garanteeed for GetColumn() - the result can be stored in +** any register. But the result is guaranteed to land in register iReg +** for GetColumnToReg(). +** +** There must be an open cursor to pTab in iTable when this routine +** is called. If iColumn<0 then code is generated that extracts the rowid. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* Description of the table we are reading from */ + int iColumn, /* Index of the table column */ + int iTable, /* The cursor pointing to the table */ + int iReg, /* Store results here */ + u8 p5 /* P5 value for OP_Column + FLAGS */ +){ + Vdbe *v = pParse->pVdbe; + int i; + struct yColCache *p; + + for(i=0, p=pParse->aColCache; iiReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ + p->lru = pParse->iCacheCnt++; + sqlite3ExprCachePinRegister(pParse, p->iReg); + return p->iReg; + } + } + assert( v!=0 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); + if( p5 ){ + sqlite3VdbeChangeP5(v, p5); + }else{ + sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); + } + return iReg; +} +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnToReg( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* Description of the table we are reading from */ + int iColumn, /* Index of the table column */ + int iTable, /* The cursor pointing to the table */ + int iReg /* Store results here */ +){ + int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); + if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); +} + + +/* +** Clear all column cache entries. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){ + int i; + struct yColCache *p; + +#if SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("CLEAR\n"); + } +#endif + for(i=0, p=pParse->aColCache; iiReg ){ + cacheEntryClear(pParse, p); + } + } +} + +/* +** Record the fact that an affinity change has occurred on iCount +** registers starting with iStart. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ + sqlite3ExprCacheRemove(pParse, iStart, iCount); +} + +/* +** Generate code to move content from registers iFrom...iFrom+nReg-1 +** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ + assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); + sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); + sqlite3ExprCacheRemove(pParse, iFrom, nReg); +} + +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) +/* +** Return true if any register in the range iFrom..iTo (inclusive) +** is used as part of the column cache. +** +** This routine is used within assert() and testcase() macros only +** and does not appear in a normal build. +*/ +static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg; + if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ + } + return 0; +} +#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ + + +/* +** Convert an expression node to a TK_REGISTER +*/ +static void exprToRegister(Expr *p, int iReg){ + p->op2 = p->op; + p->op = TK_REGISTER; + p->iTable = iReg; + ExprClearProperty(p, EP_Skip); +} + +/* +** Generate code into the current Vdbe to evaluate the given +** expression. Attempt to store the results in register "target". +** Return the register where results are stored. +** +** With this routine, there is no guarantee that results will +** be stored in target. The result might be stored in some other +** register if it is convenient to do so. The calling function +** must check the return code and move the results to the desired +** register. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ + Vdbe *v = pParse->pVdbe; /* The VM under construction */ + int op; /* The opcode being coded */ + int inReg = target; /* Results stored in register inReg */ + int regFree1 = 0; /* If non-zero free this temporary register */ + int regFree2 = 0; /* If non-zero free this temporary register */ + int r1, r2, r3, r4; /* Various register numbers */ + sqlite3 *db = pParse->db; /* The database connection */ + Expr tempX; /* Temporary expression node */ + + assert( target>0 && target<=pParse->nMem ); + if( v==0 ){ + assert( pParse->db->mallocFailed ); + return 0; + } + + if( pExpr==0 ){ + op = TK_NULL; + }else{ + op = pExpr->op; + } + switch( op ){ + case TK_AGG_COLUMN: { + AggInfo *pAggInfo = pExpr->pAggInfo; + struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; + if( !pAggInfo->directMode ){ + assert( pCol->iMem>0 ); + inReg = pCol->iMem; + break; + }else if( pAggInfo->useSortingIdx ){ + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, + pCol->iSorterColumn, target); + break; + } + /* Otherwise, fall thru into the TK_COLUMN case */ + } + case TK_COLUMN: { + int iTab = pExpr->iTable; + if( iTab<0 ){ + if( pParse->ckBase>0 ){ + /* Generating CHECK constraints or inserting into partial index */ + inReg = pExpr->iColumn + pParse->ckBase; + break; + }else{ + /* Coding an expression that is part of an index where column names + ** in the index refer to the table to which the index belongs */ + iTab = pParse->iSelfTab; + } + } + inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, + pExpr->iColumn, iTab, target, + pExpr->op2); + break; + } + case TK_INTEGER: { + codeInteger(pParse, pExpr, 0, target); + break; + } +#ifndef SQLITE_OMIT_FLOATING_POINT + case TK_FLOAT: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + codeReal(v, pExpr->u.zToken, 0, target); + break; + } +#endif + case TK_STRING: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3VdbeLoadString(v, target, pExpr->u.zToken); + break; + } + case TK_NULL: { + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + break; + } +#ifndef SQLITE_OMIT_BLOB_LITERAL + case TK_BLOB: { + int n; + const char *z; + char *zBlob; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); + assert( pExpr->u.zToken[1]=='\'' ); + z = &pExpr->u.zToken[2]; + n = sqlite3Strlen30(z) - 1; + assert( z[n]=='\'' ); + zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); + sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); + break; + } +#endif + case TK_VARIABLE: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( pExpr->u.zToken!=0 ); + assert( pExpr->u.zToken[0]!=0 ); + sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); + if( pExpr->u.zToken[1]!=0 ){ + assert( pExpr->u.zToken[0]=='?' + || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); + sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); + } + break; + } + case TK_REGISTER: { + inReg = pExpr->iTable; + break; + } +#ifndef SQLITE_OMIT_CAST + case TK_CAST: { + /* Expressions of the form: CAST(pLeft AS token) */ + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + if( inReg!=target ){ + sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); + inReg = target; + } + sqlite3VdbeAddOp2(v, OP_Cast, target, + sqlite3AffinityType(pExpr->u.zToken, 0)); + testcase( usedAsColumnCache(pParse, inReg, inReg) ); + sqlite3ExprCacheAffinityChange(pParse, inReg, 1); + break; + } +#endif /* SQLITE_OMIT_CAST */ + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, inReg, SQLITE_STOREP2); + assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); + assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); + assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); + assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_IS: + case TK_ISNOT: { + testcase( op==TK_IS ); + testcase( op==TK_ISNOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + op = (op==TK_IS) ? TK_EQ : TK_NE; + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); + VdbeCoverageIf(v, op==TK_EQ); + VdbeCoverageIf(v, op==TK_NE); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_AND: + case TK_OR: + case TK_PLUS: + case TK_STAR: + case TK_MINUS: + case TK_REM: + case TK_BITAND: + case TK_BITOR: + case TK_SLASH: + case TK_LSHIFT: + case TK_RSHIFT: + case TK_CONCAT: { + assert( TK_AND==OP_And ); testcase( op==TK_AND ); + assert( TK_OR==OP_Or ); testcase( op==TK_OR ); + assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); + assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); + assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); + assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); + assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); + assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); + assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); + assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); + assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + sqlite3VdbeAddOp3(v, op, r2, r1, target); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_UMINUS: { + Expr *pLeft = pExpr->pLeft; + assert( pLeft ); + if( pLeft->op==TK_INTEGER ){ + codeInteger(pParse, pLeft, 1, target); +#ifndef SQLITE_OMIT_FLOATING_POINT + }else if( pLeft->op==TK_FLOAT ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + codeReal(v, pLeft->u.zToken, 1, target); +#endif + }else{ + tempX.op = TK_INTEGER; + tempX.flags = EP_IntValue|EP_TokenOnly; + tempX.u.iValue = 0; + r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); + sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); + testcase( regFree2==0 ); + } + inReg = target; + break; + } + case TK_BITNOT: + case TK_NOT: { + assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); + assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + testcase( regFree1==0 ); + inReg = target; + sqlite3VdbeAddOp2(v, op, r1, inReg); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + int addr; + assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); + assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + testcase( regFree1==0 ); + addr = sqlite3VdbeAddOp1(v, op, r1); + VdbeCoverageIf(v, op==TK_ISNULL); + VdbeCoverageIf(v, op==TK_NOTNULL); + sqlite3VdbeAddOp2(v, OP_Integer, 0, target); + sqlite3VdbeJumpHere(v, addr); + break; + } + case TK_AGG_FUNCTION: { + AggInfo *pInfo = pExpr->pAggInfo; + if( pInfo==0 ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); + }else{ + inReg = pInfo->aFunc[pExpr->iAgg].iMem; + } + break; + } + case TK_FUNCTION: { + ExprList *pFarg; /* List of function arguments */ + int nFarg; /* Number of function arguments */ + FuncDef *pDef; /* The function definition object */ + const char *zId; /* The function name */ + u32 constMask = 0; /* Mask of function arguments that are constant */ + int i; /* Loop counter */ + u8 enc = ENC(db); /* The text encoding used by this database */ + CollSeq *pColl = 0; /* A collating sequence */ + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + if( ExprHasProperty(pExpr, EP_TokenOnly) ){ + pFarg = 0; + }else{ + pFarg = pExpr->x.pList; + } + nFarg = pFarg ? pFarg->nExpr : 0; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + zId = pExpr->u.zToken; + pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); + if( pDef==0 || pDef->xFinalize!=0 ){ + sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); + break; + } + + /* Attempt a direct implementation of the built-in COALESCE() and + ** IFNULL() functions. This avoids unnecessary evaluation of + ** arguments past the first non-NULL argument. + */ + if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ + int endCoalesce = sqlite3VdbeMakeLabel(v); + assert( nFarg>=2 ); + sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); + for(i=1; ia[i].pExpr, target); + sqlite3ExprCachePop(pParse); + } + sqlite3VdbeResolveLabel(v, endCoalesce); + break; + } + + /* The UNLIKELY() function is a no-op. The result is the value + ** of the first argument. + */ + if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ + assert( nFarg>=1 ); + inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); + break; + } + + for(i=0; ia[i].pExpr) ){ + testcase( i==31 ); + constMask |= MASKBIT32(i); + } + if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ + pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); + } + } + if( pFarg ){ + if( constMask ){ + r1 = pParse->nMem+1; + pParse->nMem += nFarg; + }else{ + r1 = sqlite3GetTempRange(pParse, nFarg); + } + + /* For length() and typeof() functions with a column argument, + ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG + ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data + ** loading. + */ + if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ + u8 exprOp; + assert( nFarg==1 ); + assert( pFarg->a[0].pExpr!=0 ); + exprOp = pFarg->a[0].pExpr->op; + if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ + assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); + assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); + testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); + pFarg->a[0].pExpr->op2 = + pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); + } + } + + sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ + sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, + SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); + sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ + }else{ + r1 = 0; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Possibly overload the function if the first argument is + ** a virtual table column. + ** + ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the + ** second argument, not the first, as the argument to test to + ** see if it is a column in a virtual table. This is done because + ** the left operand of infix functions (the operand we want to + ** control overloading) ends up as the second argument to the + ** function. The expression "A glob B" is equivalent to + ** "glob(B,A). We want to use the A in "A glob B" to test + ** for function overloading. But we use the B term in "glob(B,A)". + */ + if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ + pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); + }else if( nFarg>0 ){ + pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); + } +#endif + if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ + if( !pColl ) pColl = db->pDfltColl; + sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, + (char*)pDef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u8)nFarg); + if( nFarg && constMask==0 ){ + sqlite3ReleaseTempRange(pParse, r1, nFarg); + } + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_EXISTS: + case TK_SELECT: { + testcase( op==TK_EXISTS ); + testcase( op==TK_SELECT ); + inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); + break; + } + case TK_IN: { + int destIfFalse = sqlite3VdbeMakeLabel(v); + int destIfNull = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); + sqlite3VdbeResolveLabel(v, destIfFalse); + sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); + sqlite3VdbeResolveLabel(v, destIfNull); + break; + } +#endif /* SQLITE_OMIT_SUBQUERY */ + + + /* + ** x BETWEEN y AND z + ** + ** This is equivalent to + ** + ** x>=y AND x<=z + ** + ** X is stored in pExpr->pLeft. + ** Y is stored in pExpr->pList->a[0].pExpr. + ** Z is stored in pExpr->pList->a[1].pExpr. + */ + case TK_BETWEEN: { + Expr *pLeft = pExpr->pLeft; + struct ExprList_item *pLItem = pExpr->x.pList->a; + Expr *pRight = pLItem->pExpr; + + r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + r3 = sqlite3GetTempReg(pParse); + r4 = sqlite3GetTempReg(pParse); + codeCompare(pParse, pLeft, pRight, OP_Ge, + r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); + pLItem++; + pRight = pLItem->pExpr; + sqlite3ReleaseTempReg(pParse, regFree2); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + testcase( regFree2==0 ); + codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); + sqlite3ReleaseTempReg(pParse, r3); + sqlite3ReleaseTempReg(pParse, r4); + break; + } + case TK_SPAN: + case TK_COLLATE: + case TK_UPLUS: { + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + break; + } + + case TK_TRIGGER: { + /* If the opcode is TK_TRIGGER, then the expression is a reference + ** to a column in the new.* or old.* pseudo-tables available to + ** trigger programs. In this case Expr.iTable is set to 1 for the + ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn + ** is set to the column of the pseudo-table to read, or to -1 to + ** read the rowid field. + ** + ** The expression is implemented using an OP_Param opcode. The p1 + ** parameter is set to 0 for an old.rowid reference, or to (i+1) + ** to reference another column of the old.* pseudo-table, where + ** i is the index of the column. For a new.rowid reference, p1 is + ** set to (n+1), where n is the number of columns in each pseudo-table. + ** For a reference to any other column in the new.* pseudo-table, p1 + ** is set to (n+2+i), where n and i are as defined previously. For + ** example, if the table on which triggers are being fired is + ** declared as: + ** + ** CREATE TABLE t1(a, b); + ** + ** Then p1 is interpreted as follows: + ** + ** p1==0 -> old.rowid p1==3 -> new.rowid + ** p1==1 -> old.a p1==4 -> new.a + ** p1==2 -> old.b p1==5 -> new.b + */ + Table *pTab = pExpr->pTab; + int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; + + assert( pExpr->iTable==0 || pExpr->iTable==1 ); + assert( pExpr->iColumn>=-1 && pExpr->iColumnnCol ); + assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); + assert( p1>=0 && p1<(pTab->nCol*2+2) ); + + sqlite3VdbeAddOp2(v, OP_Param, p1, target); + VdbeComment((v, "%s.%s -> $%d", + (pExpr->iTable ? "new" : "old"), + (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), + target + )); + +#ifndef SQLITE_OMIT_FLOATING_POINT + /* If the column has REAL affinity, it may currently be stored as an + ** integer. Use OP_RealAffinity to make sure it is really real. + ** + ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to + ** floating point when extracting it from the record. */ + if( pExpr->iColumn>=0 + && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL + ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, target); + } +#endif + break; + } + + + /* + ** Form A: + ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form B: + ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form A is can be transformed into the equivalent form B as follows: + ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... + ** WHEN x=eN THEN rN ELSE y END + ** + ** X (if it exists) is in pExpr->pLeft. + ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is + ** odd. The Y is also optional. If the number of elements in x.pList + ** is even, then Y is omitted and the "otherwise" result is NULL. + ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. + ** + ** The result of the expression is the Ri for the first matching Ei, + ** or if there is no matching Ei, the ELSE term Y, or if there is + ** no ELSE term, NULL. + */ + default: assert( op==TK_CASE ); { + int endLabel; /* GOTO label for end of CASE stmt */ + int nextCase; /* GOTO label for next WHEN clause */ + int nExpr; /* 2x number of WHEN terms */ + int i; /* Loop counter */ + ExprList *pEList; /* List of WHEN terms */ + struct ExprList_item *aListelem; /* Array of WHEN terms */ + Expr opCompare; /* The X==Ei expression */ + Expr *pX; /* The X expression */ + Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ + VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); + assert(pExpr->x.pList->nExpr > 0); + pEList = pExpr->x.pList; + aListelem = pEList->a; + nExpr = pEList->nExpr; + endLabel = sqlite3VdbeMakeLabel(v); + if( (pX = pExpr->pLeft)!=0 ){ + tempX = *pX; + testcase( pX->op==TK_COLUMN ); + exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); + testcase( regFree1==0 ); + opCompare.op = TK_EQ; + opCompare.pLeft = &tempX; + pTest = &opCompare; + /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: + ** The value in regFree1 might get SCopy-ed into the file result. + ** So make sure that the regFree1 register is not reused for other + ** purposes and possibly overwritten. */ + regFree1 = 0; + } + for(i=0; iop==TK_COLUMN ); + sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); + testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); + sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); + sqlite3VdbeGoto(v, endLabel); + sqlite3ExprCachePop(pParse); + sqlite3VdbeResolveLabel(v, nextCase); + } + if( (nExpr&1)!=0 ){ + sqlite3ExprCachePush(pParse); + sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); + sqlite3ExprCachePop(pParse); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + } + assert( db->mallocFailed || pParse->nErr>0 + || pParse->iCacheLevel==iCacheLevel ); + sqlite3VdbeResolveLabel(v, endLabel); + break; + } +#ifndef SQLITE_OMIT_TRIGGER + case TK_RAISE: { + assert( pExpr->affinity==OE_Rollback + || pExpr->affinity==OE_Abort + || pExpr->affinity==OE_Fail + || pExpr->affinity==OE_Ignore + ); + if( !pParse->pTriggerTab ){ + sqlite3ErrorMsg(pParse, + "RAISE() may only be used within a trigger-program"); + return 0; + } + if( pExpr->affinity==OE_Abort ){ + sqlite3MayAbort(pParse); + } + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + if( pExpr->affinity==OE_Ignore ){ + sqlite3VdbeAddOp4( + v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); + VdbeCoverage(v); + }else{ + sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, + pExpr->affinity, pExpr->u.zToken, 0, 0); + } + + break; + } +#endif + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); + return inReg; +} + +/* +** Factor out the code of the given expression to initialization time. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeAtInit( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The expression to code when the VDBE initializes */ + int regDest, /* Store the value in this register */ + u8 reusable /* True if this expression is reusable */ +){ + ExprList *p; + assert( ConstFactorOk(pParse) ); + p = pParse->pConstExpr; + pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); + p = sqlite3ExprListAppend(pParse, p, pExpr); + if( p ){ + struct ExprList_item *pItem = &p->a[p->nExpr-1]; + pItem->u.iConstExprReg = regDest; + pItem->reusable = reusable; + } + pParse->pConstExpr = p; +} + +/* +** Generate code to evaluate an expression and store the results +** into a register. Return the register number where the results +** are stored. +** +** If the register is a temporary register that can be deallocated, +** then write its number into *pReg. If the result register is not +** a temporary, then set *pReg to zero. +** +** If pExpr is a constant, then this routine might generate this +** code to fill the register in the initialization section of the +** VDBE program, in order to factor it out of the evaluation loop. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ + int r2; + pExpr = sqlite3ExprSkipCollate(pExpr); + if( ConstFactorOk(pParse) + && pExpr->op!=TK_REGISTER + && sqlite3ExprIsConstantNotJoin(pExpr) + ){ + ExprList *p = pParse->pConstExpr; + int i; + *pReg = 0; + if( p ){ + struct ExprList_item *pItem; + for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ + if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ + return pItem->u.iConstExprReg; + } + } + } + r2 = ++pParse->nMem; + sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); + }else{ + int r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); + if( r2==r1 ){ + *pReg = r1; + }else{ + sqlite3ReleaseTempReg(pParse, r1); + *pReg = 0; + } + } + return r2; +} + +/* +** Generate code that will evaluate expression pExpr and store the +** results in register target. The results are guaranteed to appear +** in register target. +*/ +SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ + int inReg; + + assert( target>0 && target<=pParse->nMem ); + if( pExpr && pExpr->op==TK_REGISTER ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); + }else{ + inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); + assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); + if( inReg!=target && pParse->pVdbe ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); + } + } +} + +/* +** Make a transient copy of expression pExpr and then code it using +** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() +** except that the input expression is guaranteed to be unchanged. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ + sqlite3 *db = pParse->db; + pExpr = sqlite3ExprDup(db, pExpr, 0); + if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); + sqlite3ExprDelete(db, pExpr); +} + +/* +** Generate code that will evaluate expression pExpr and store the +** results in register target. The results are guaranteed to appear +** in register target. If the expression is constant, then this routine +** might choose to code the expression at initialization time. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ + if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ + sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); + }else{ + sqlite3ExprCode(pParse, pExpr, target); + } +} + +/* +** Generate code that evaluates the given expression and puts the result +** in register target. +** +** Also make a copy of the expression results into another "cache" register +** and modify the expression so that the next time it is evaluated, +** the result is a copy of the cache register. +** +** This routine is used for expressions that are used multiple +** times. They are evaluated once and the results of the expression +** are reused. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ + Vdbe *v = pParse->pVdbe; + int iMem; + + assert( target>0 ); + assert( pExpr->op!=TK_REGISTER ); + sqlite3ExprCode(pParse, pExpr, target); + iMem = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); + exprToRegister(pExpr, iMem); +} + +/* +** Generate code that pushes the value of every element of the given +** expression list into a sequence of registers beginning at target. +** +** Return the number of elements evaluated. +** +** The SQLITE_ECEL_DUP flag prevents the arguments from being +** filled using OP_SCopy. OP_Copy must be used instead. +** +** The SQLITE_ECEL_FACTOR argument allows constant arguments to be +** factored out into initialization code. +** +** The SQLITE_ECEL_REF flag means that expressions in the list with +** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored +** in registers at srcReg, and so the value can be copied from there. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeExprList( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* The expression list to be coded */ + int target, /* Where to write results */ + int srcReg, /* Source registers if SQLITE_ECEL_REF */ + u8 flags /* SQLITE_ECEL_* flags */ +){ + struct ExprList_item *pItem; + int i, j, n; + u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; + Vdbe *v = pParse->pVdbe; + assert( pList!=0 ); + assert( target>0 ); + assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ + n = pList->nExpr; + if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; + for(pItem=pList->a, i=0; ipExpr; + if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ + sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); + }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ + sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); + }else{ + int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); + if( inReg!=target+i ){ + VdbeOp *pOp; + if( copyOp==OP_Copy + && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy + && pOp->p1+pOp->p3+1==inReg + && pOp->p2+pOp->p3+1==target+i + ){ + pOp->p3++; + }else{ + sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); + } + } + } + } + return n; +} + +/* +** Generate code for a BETWEEN operator. +** +** x BETWEEN y AND z +** +** The above is equivalent to +** +** x>=y AND x<=z +** +** Code it as such, taking care to do the common subexpression +** elimination of x. +*/ +static void exprCodeBetween( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* The BETWEEN expression */ + int dest, /* Jump here if the jump is taken */ + int jumpIfTrue, /* Take the jump if the BETWEEN is true */ + int jumpIfNull /* Take the jump if the BETWEEN is NULL */ +){ + Expr exprAnd; /* The AND operator in x>=y AND x<=z */ + Expr compLeft; /* The x>=y term */ + Expr compRight; /* The x<=z term */ + Expr exprX; /* The x subexpression */ + int regFree1 = 0; /* Temporary use register */ + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + exprX = *pExpr->pLeft; + exprAnd.op = TK_AND; + exprAnd.pLeft = &compLeft; + exprAnd.pRight = &compRight; + compLeft.op = TK_GE; + compLeft.pLeft = &exprX; + compLeft.pRight = pExpr->x.pList->a[0].pExpr; + compRight.op = TK_LE; + compRight.pLeft = &exprX; + compRight.pRight = pExpr->x.pList->a[1].pExpr; + exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); + if( jumpIfTrue ){ + sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); + }else{ + sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); + } + sqlite3ReleaseTempReg(pParse, regFree1); + + /* Ensure adequate test coverage */ + testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); + testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); + testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); + testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); + testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); + testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); + testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); + testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); +} + +/* +** Generate code for a boolean expression such that a jump is made +** to the label "dest" if the expression is true but execution +** continues straight thru if the expression is false. +** +** If the expression evaluates to NULL (neither true nor false), then +** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. +** +** This code depends on the fact that certain token values (ex: TK_EQ) +** are the same as opcode values (ex: OP_Eq) that implement the corresponding +** operation. Special comments in vdbe.c and the mkopcodeh.awk script in +** the make process cause these values to align. Assert()s in the code +** below verify that the numbers are aligned correctly. +*/ +SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ + Vdbe *v = pParse->pVdbe; + int op = 0; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); + if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ + if( NEVER(pExpr==0) ) return; /* No way this can happen */ + op = pExpr->op; + switch( op ){ + case TK_AND: { + int d2 = sqlite3VdbeMakeLabel(v); + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + sqlite3ExprCachePop(pParse); + break; + } + case TK_OR: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3ExprCachePop(pParse); + break; + } + case TK_NOT: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); + break; + } + case TK_IS: + case TK_ISNOT: + testcase( op==TK_IS ); + testcase( op==TK_ISNOT ); + op = (op==TK_IS) ? TK_EQ : TK_NE; + jumpIfNull = SQLITE_NULLEQ; + /* Fall thru */ + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + testcase( jumpIfNull==0 ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); + assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); + assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); + assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); + assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); + assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); + VdbeCoverageIf(v, op==TK_ISNULL); + VdbeCoverageIf(v, op==TK_NOTNULL); + testcase( regFree1==0 ); + break; + } + case TK_BETWEEN: { + testcase( jumpIfNull==0 ); + exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_IN: { + int destIfFalse = sqlite3VdbeMakeLabel(v); + int destIfNull = jumpIfNull ? dest : destIfFalse; + sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); + sqlite3VdbeGoto(v, dest); + sqlite3VdbeResolveLabel(v, destIfFalse); + break; + } +#endif + default: { + if( exprAlwaysTrue(pExpr) ){ + sqlite3VdbeGoto(v, dest); + }else if( exprAlwaysFalse(pExpr) ){ + /* No-op */ + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); + VdbeCoverage(v); + testcase( regFree1==0 ); + testcase( jumpIfNull==0 ); + } + break; + } + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); +} + +/* +** Generate code for a boolean expression such that a jump is made +** to the label "dest" if the expression is false but execution +** continues straight thru if the expression is true. +** +** If the expression evaluates to NULL (neither true nor false) then +** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull +** is 0. +*/ +SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ + Vdbe *v = pParse->pVdbe; + int op = 0; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); + if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ + if( pExpr==0 ) return; + + /* The value of pExpr->op and op are related as follows: + ** + ** pExpr->op op + ** --------- ---------- + ** TK_ISNULL OP_NotNull + ** TK_NOTNULL OP_IsNull + ** TK_NE OP_Eq + ** TK_EQ OP_Ne + ** TK_GT OP_Le + ** TK_LE OP_Gt + ** TK_GE OP_Lt + ** TK_LT OP_Ge + ** + ** For other values of pExpr->op, op is undefined and unused. + ** The value of TK_ and OP_ constants are arranged such that we + ** can compute the mapping above using the following expression. + ** Assert()s verify that the computation is correct. + */ + op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); + + /* Verify correct alignment of TK_ and OP_ constants + */ + assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); + assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); + assert( pExpr->op!=TK_NE || op==OP_Eq ); + assert( pExpr->op!=TK_EQ || op==OP_Ne ); + assert( pExpr->op!=TK_LT || op==OP_Ge ); + assert( pExpr->op!=TK_LE || op==OP_Gt ); + assert( pExpr->op!=TK_GT || op==OP_Le ); + assert( pExpr->op!=TK_GE || op==OP_Lt ); + + switch( pExpr->op ){ + case TK_AND: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3ExprCachePop(pParse); + break; + } + case TK_OR: { + int d2 = sqlite3VdbeMakeLabel(v); + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + sqlite3ExprCachePop(pParse); + break; + } + case TK_NOT: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); + break; + } + case TK_IS: + case TK_ISNOT: + testcase( pExpr->op==TK_IS ); + testcase( pExpr->op==TK_ISNOT ); + op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; + jumpIfNull = SQLITE_NULLEQ; + /* Fall thru */ + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + testcase( jumpIfNull==0 ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); + assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); + assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); + assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); + assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); + testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); + testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); + testcase( regFree1==0 ); + break; + } + case TK_BETWEEN: { + testcase( jumpIfNull==0 ); + exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_IN: { + if( jumpIfNull ){ + sqlite3ExprCodeIN(pParse, pExpr, dest, dest); + }else{ + int destIfNull = sqlite3VdbeMakeLabel(v); + sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); + sqlite3VdbeResolveLabel(v, destIfNull); + } + break; + } +#endif + default: { + if( exprAlwaysFalse(pExpr) ){ + sqlite3VdbeGoto(v, dest); + }else if( exprAlwaysTrue(pExpr) ){ + /* no-op */ + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); + VdbeCoverage(v); + testcase( regFree1==0 ); + testcase( jumpIfNull==0 ); + } + break; + } + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); +} + +/* +** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before +** code generation, and that copy is deleted after code generation. This +** ensures that the original pExpr is unchanged. +*/ +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ + sqlite3 *db = pParse->db; + Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); + if( db->mallocFailed==0 ){ + sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); + } + sqlite3ExprDelete(db, pCopy); +} + + +/* +** Do a deep comparison of two expression trees. Return 0 if the two +** expressions are completely identical. Return 1 if they differ only +** by a COLLATE operator at the top level. Return 2 if there are differences +** other than the top-level COLLATE operator. +** +** If any subelement of pB has Expr.iTable==(-1) then it is allowed +** to compare equal to an equivalent element in pA with Expr.iTable==iTab. +** +** The pA side might be using TK_REGISTER. If that is the case and pB is +** not using TK_REGISTER but is otherwise equivalent, then still return 0. +** +** Sometimes this routine will return 2 even if the two expressions +** really are equivalent. If we cannot prove that the expressions are +** identical, we return 2 just to be safe. So if this routine +** returns 2, then you do not really know for certain if the two +** expressions are the same. But if you get a 0 or 1 return, then you +** can be sure the expressions are the same. In the places where +** this routine is used, it does not hurt to get an extra 2 - that +** just might result in some slightly slower code. But returning +** an incorrect 0 or 1 could lead to a malfunction. +*/ +SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ + u32 combinedFlags; + if( pA==0 || pB==0 ){ + return pB==pA ? 0 : 2; + } + combinedFlags = pA->flags | pB->flags; + if( combinedFlags & EP_IntValue ){ + if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ + return 0; + } + return 2; + } + if( pA->op!=pB->op ){ + if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ + return 1; + } + if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ + return 1; + } + return 2; + } + if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ + if( pA->op==TK_FUNCTION ){ + if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; + }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ + return pA->op==TK_COLLATE ? 1 : 2; + } + } + if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; + if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ + if( combinedFlags & EP_xIsSelect ) return 2; + if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; + if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; + if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; + if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ + if( pA->iColumn!=pB->iColumn ) return 2; + if( pA->iTable!=pB->iTable + && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; + } + } + return 0; +} + +/* +** Compare two ExprList objects. Return 0 if they are identical and +** non-zero if they differ in any way. +** +** If any subelement of pB has Expr.iTable==(-1) then it is allowed +** to compare equal to an equivalent element in pA with Expr.iTable==iTab. +** +** This routine might return non-zero for equivalent ExprLists. The +** only consequence will be disabled optimizations. But this routine +** must never return 0 if the two ExprList objects are different, or +** a malfunction will result. +** +** Two NULL pointers are considered to be the same. But a NULL pointer +** always differs from a non-NULL pointer. +*/ +SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ + int i; + if( pA==0 && pB==0 ) return 0; + if( pA==0 || pB==0 ) return 1; + if( pA->nExpr!=pB->nExpr ) return 1; + for(i=0; inExpr; i++){ + Expr *pExprA = pA->a[i].pExpr; + Expr *pExprB = pB->a[i].pExpr; + if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; + if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; + } + return 0; +} + +/* +** Return true if we can prove the pE2 will always be true if pE1 is +** true. Return false if we cannot complete the proof or if pE2 might +** be false. Examples: +** +** pE1: x==5 pE2: x==5 Result: true +** pE1: x>0 pE2: x==5 Result: false +** pE1: x=21 pE2: x=21 OR y=43 Result: true +** pE1: x!=123 pE2: x IS NOT NULL Result: true +** pE1: x!=?1 pE2: x IS NOT NULL Result: true +** pE1: x IS NULL pE2: x IS NOT NULL Result: false +** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false +** +** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has +** Expr.iTable<0 then assume a table number given by iTab. +** +** When in doubt, return false. Returning true might give a performance +** improvement. Returning false might cause a performance reduction, but +** it will always give the correct answer and is hence always safe. +*/ +SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ + if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ + return 1; + } + if( pE2->op==TK_OR + && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) + || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) + ){ + return 1; + } + if( pE2->op==TK_NOTNULL + && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 + && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) + ){ + return 1; + } + return 0; +} + +/* +** An instance of the following structure is used by the tree walker +** to count references to table columns in the arguments of an +** aggregate function, in order to implement the +** sqlite3FunctionThisSrc() routine. +*/ +struct SrcCount { + SrcList *pSrc; /* One particular FROM clause in a nested query */ + int nThis; /* Number of references to columns in pSrcList */ + int nOther; /* Number of references to columns in other FROM clauses */ +}; + +/* +** Count the number of references to columns. +*/ +static int exprSrcCount(Walker *pWalker, Expr *pExpr){ + /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() + ** is always called before sqlite3ExprAnalyzeAggregates() and so the + ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If + ** sqlite3FunctionUsesThisSrc() is used differently in the future, the + ** NEVER() will need to be removed. */ + if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ + int i; + struct SrcCount *p = pWalker->u.pSrcCount; + SrcList *pSrc = p->pSrc; + int nSrc = pSrc ? pSrc->nSrc : 0; + for(i=0; iiTable==pSrc->a[i].iCursor ) break; + } + if( inThis++; + }else{ + p->nOther++; + } + } + return WRC_Continue; +} + +/* +** Determine if any of the arguments to the pExpr Function reference +** pSrcList. Return true if they do. Also return true if the function +** has no arguments or has only constant arguments. Return false if pExpr +** references columns but not columns of tables found in pSrcList. +*/ +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ + Walker w; + struct SrcCount cnt; + assert( pExpr->op==TK_AGG_FUNCTION ); + memset(&w, 0, sizeof(w)); + w.xExprCallback = exprSrcCount; + w.u.pSrcCount = &cnt; + cnt.pSrc = pSrcList; + cnt.nThis = 0; + cnt.nOther = 0; + sqlite3WalkExprList(&w, pExpr->x.pList); + return cnt.nThis>0 || cnt.nOther==0; +} + +/* +** Add a new element to the pAggInfo->aCol[] array. Return the index of +** the new element. Return a negative number if malloc fails. +*/ +static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ + int i; + pInfo->aCol = sqlite3ArrayAllocate( + db, + pInfo->aCol, + sizeof(pInfo->aCol[0]), + &pInfo->nColumn, + &i + ); + return i; +} + +/* +** Add a new element to the pAggInfo->aFunc[] array. Return the index of +** the new element. Return a negative number if malloc fails. +*/ +static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ + int i; + pInfo->aFunc = sqlite3ArrayAllocate( + db, + pInfo->aFunc, + sizeof(pInfo->aFunc[0]), + &pInfo->nFunc, + &i + ); + return i; +} + +/* +** This is the xExprCallback for a tree walker. It is used to +** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates +** for additional information. +*/ +static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ + int i; + NameContext *pNC = pWalker->u.pNC; + Parse *pParse = pNC->pParse; + SrcList *pSrcList = pNC->pSrcList; + AggInfo *pAggInfo = pNC->pAggInfo; + + switch( pExpr->op ){ + case TK_AGG_COLUMN: + case TK_COLUMN: { + testcase( pExpr->op==TK_AGG_COLUMN ); + testcase( pExpr->op==TK_COLUMN ); + /* Check to see if the column is in one of the tables in the FROM + ** clause of the aggregate query */ + if( ALWAYS(pSrcList!=0) ){ + struct SrcList_item *pItem = pSrcList->a; + for(i=0; inSrc; i++, pItem++){ + struct AggInfo_col *pCol; + assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + if( pExpr->iTable==pItem->iCursor ){ + /* If we reach this point, it means that pExpr refers to a table + ** that is in the FROM clause of the aggregate query. + ** + ** Make an entry for the column in pAggInfo->aCol[] if there + ** is not an entry there already. + */ + int k; + pCol = pAggInfo->aCol; + for(k=0; knColumn; k++, pCol++){ + if( pCol->iTable==pExpr->iTable && + pCol->iColumn==pExpr->iColumn ){ + break; + } + } + if( (k>=pAggInfo->nColumn) + && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 + ){ + pCol = &pAggInfo->aCol[k]; + pCol->pTab = pExpr->pTab; + pCol->iTable = pExpr->iTable; + pCol->iColumn = pExpr->iColumn; + pCol->iMem = ++pParse->nMem; + pCol->iSorterColumn = -1; + pCol->pExpr = pExpr; + if( pAggInfo->pGroupBy ){ + int j, n; + ExprList *pGB = pAggInfo->pGroupBy; + struct ExprList_item *pTerm = pGB->a; + n = pGB->nExpr; + for(j=0; jpExpr; + if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && + pE->iColumn==pExpr->iColumn ){ + pCol->iSorterColumn = j; + break; + } + } + } + if( pCol->iSorterColumn<0 ){ + pCol->iSorterColumn = pAggInfo->nSortingColumn++; + } + } + /* There is now an entry for pExpr in pAggInfo->aCol[] (either + ** because it was there before or because we just created it). + ** Convert the pExpr to be a TK_AGG_COLUMN referring to that + ** pAggInfo->aCol[] entry. + */ + ExprSetVVAProperty(pExpr, EP_NoReduce); + pExpr->pAggInfo = pAggInfo; + pExpr->op = TK_AGG_COLUMN; + pExpr->iAgg = (i16)k; + break; + } /* endif pExpr->iTable==pItem->iCursor */ + } /* end loop over pSrcList */ + } + return WRC_Prune; + } + case TK_AGG_FUNCTION: { + if( (pNC->ncFlags & NC_InAggFunc)==0 + && pWalker->walkerDepth==pExpr->op2 + ){ + /* Check to see if pExpr is a duplicate of another aggregate + ** function that is already in the pAggInfo structure + */ + struct AggInfo_func *pItem = pAggInfo->aFunc; + for(i=0; inFunc; i++, pItem++){ + if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ + break; + } + } + if( i>=pAggInfo->nFunc ){ + /* pExpr is original. Make a new entry in pAggInfo->aFunc[] + */ + u8 enc = ENC(pParse->db); + i = addAggInfoFunc(pParse->db, pAggInfo); + if( i>=0 ){ + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + pItem = &pAggInfo->aFunc[i]; + pItem->pExpr = pExpr; + pItem->iMem = ++pParse->nMem; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pItem->pFunc = sqlite3FindFunction(pParse->db, + pExpr->u.zToken, + pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); + if( pExpr->flags & EP_Distinct ){ + pItem->iDistinct = pParse->nTab++; + }else{ + pItem->iDistinct = -1; + } + } + } + /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry + */ + assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + ExprSetVVAProperty(pExpr, EP_NoReduce); + pExpr->iAgg = (i16)i; + pExpr->pAggInfo = pAggInfo; + return WRC_Prune; + }else{ + return WRC_Continue; + } + } + } + return WRC_Continue; +} +static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ + UNUSED_PARAMETER(pWalker); + UNUSED_PARAMETER(pSelect); + return WRC_Continue; +} + +/* +** Analyze the pExpr expression looking for aggregate functions and +** for variables that need to be added to AggInfo object that pNC->pAggInfo +** points to. Additional entries are made on the AggInfo object as +** necessary. +** +** This routine should only be called after the expression has been +** analyzed by sqlite3ResolveExprNames(). +*/ +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = analyzeAggregate; + w.xSelectCallback = analyzeAggregatesInSelect; + w.u.pNC = pNC; + assert( pNC->pSrcList!=0 ); + sqlite3WalkExpr(&w, pExpr); +} + +/* +** Call sqlite3ExprAnalyzeAggregates() for every expression in an +** expression list. Return the number of errors. +** +** If an error is found, the analysis is cut short. +*/ +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ + struct ExprList_item *pItem; + int i; + if( pList ){ + for(pItem=pList->a, i=0; inExpr; i++, pItem++){ + sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); + } + } +} + +/* +** Allocate a single new register for use to hold some intermediate result. +*/ +SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){ + if( pParse->nTempReg==0 ){ + return ++pParse->nMem; + } + return pParse->aTempReg[--pParse->nTempReg]; +} + +/* +** Deallocate a register, making available for reuse for some other +** purpose. +** +** If a register is currently being used by the column cache, then +** the deallocation is deferred until the column cache line that uses +** the register becomes stale. +*/ +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ + if( iReg && pParse->nTempRegaTempReg) ){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg==iReg ){ + p->tempReg = 1; + return; + } + } + pParse->aTempReg[pParse->nTempReg++] = iReg; + } +} + +/* +** Allocate or deallocate a block of nReg consecutive registers +*/ +SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){ + int i, n; + i = pParse->iRangeReg; + n = pParse->nRangeReg; + if( nReg<=n ){ + assert( !usedAsColumnCache(pParse, i, i+n-1) ); + pParse->iRangeReg += nReg; + pParse->nRangeReg -= nReg; + }else{ + i = pParse->nMem+1; + pParse->nMem += nReg; + } + return i; +} +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ + sqlite3ExprCacheRemove(pParse, iReg, nReg); + if( nReg>pParse->nRangeReg ){ + pParse->nRangeReg = nReg; + pParse->iRangeReg = iReg; + } +} + +/* +** Mark all temporary registers as being unavailable for reuse. +*/ +SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){ + pParse->nTempReg = 0; + pParse->nRangeReg = 0; +} + +/* +** Validate that no temporary register falls within the range of +** iFirst..iLast, inclusive. This routine is only call from within assert() +** statements. +*/ +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ + int i; + if( pParse->nRangeReg>0 + && pParse->iRangeReg+pParse->nRangeRegiRangeReg>=iFirst + ){ + return 0; + } + for(i=0; inTempReg; i++){ + if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ + return 0; + } + } + return 1; +} +#endif /* SQLITE_DEBUG */ + +/************** End of expr.c ************************************************/ +/************** Begin file alter.c *******************************************/ +/* +** 2005 February 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that used to generate VDBE code +** that implements the ALTER TABLE command. +*/ +/* #include "sqliteInt.h" */ + +/* +** The code in this file only exists if we are not omitting the +** ALTER TABLE logic from the build. +*/ +#ifndef SQLITE_OMIT_ALTERTABLE + + +/* +** This function is used by SQL generated to implement the +** ALTER TABLE command. The first argument is the text of a CREATE TABLE or +** CREATE INDEX command. The second is a table name. The table name in +** the CREATE TABLE or CREATE INDEX statement is replaced with the third +** argument and the result returned. Examples: +** +** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') +** -> 'CREATE TABLE def(a, b, c)' +** +** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') +** -> 'CREATE INDEX i ON def(a, b, c)' +*/ +static void renameTableFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + unsigned char const *zSql = sqlite3_value_text(argv[0]); + unsigned char const *zTableName = sqlite3_value_text(argv[1]); + + int token; + Token tname; + unsigned char const *zCsr = zSql; + int len = 0; + char *zRet; + + sqlite3 *db = sqlite3_context_db_handle(context); + + UNUSED_PARAMETER(NotUsed); + + /* The principle used to locate the table name in the CREATE TABLE + ** statement is that the table name is the first non-space token that + ** is immediately followed by a TK_LP or TK_USING token. + */ + if( zSql ){ + do { + if( !*zCsr ){ + /* Ran out of input before finding an opening bracket. Return NULL. */ + return; + } + + /* Store the token that zCsr points to in tname. */ + tname.z = (char*)zCsr; + tname.n = len; + + /* Advance zCsr to the next token. Store that token type in 'token', + ** and its length in 'len' (to be used next iteration of this loop). + */ + do { + zCsr += len; + len = sqlite3GetToken(zCsr, &token); + } while( token==TK_SPACE ); + assert( len>0 ); + } while( token!=TK_LP && token!=TK_USING ); + + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), + zSql, zTableName, tname.z+tname.n); + sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); + } +} + +/* +** This C function implements an SQL user function that is used by SQL code +** generated by the ALTER TABLE ... RENAME command to modify the definition +** of any foreign key constraints that use the table being renamed as the +** parent table. It is passed three arguments: +** +** 1) The complete text of the CREATE TABLE statement being modified, +** 2) The old name of the table being renamed, and +** 3) The new name of the table being renamed. +** +** It returns the new CREATE TABLE statement. For example: +** +** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3') +** -> 'CREATE TABLE t1(a REFERENCES t3)' +*/ +#ifndef SQLITE_OMIT_FOREIGN_KEY +static void renameParentFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + char *zOutput = 0; + char *zResult; + unsigned char const *zInput = sqlite3_value_text(argv[0]); + unsigned char const *zOld = sqlite3_value_text(argv[1]); + unsigned char const *zNew = sqlite3_value_text(argv[2]); + + unsigned const char *z; /* Pointer to token */ + int n; /* Length of token z */ + int token; /* Type of token */ + + UNUSED_PARAMETER(NotUsed); + if( zInput==0 || zOld==0 ) return; + for(z=zInput; *z; z=z+n){ + n = sqlite3GetToken(z, &token); + if( token==TK_REFERENCES ){ + char *zParent; + do { + z += n; + n = sqlite3GetToken(z, &token); + }while( token==TK_SPACE ); + + if( token==TK_ILLEGAL ) break; + zParent = sqlite3DbStrNDup(db, (const char *)z, n); + if( zParent==0 ) break; + sqlite3Dequote(zParent); + if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ + char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", + (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew + ); + sqlite3DbFree(db, zOutput); + zOutput = zOut; + zInput = &z[n]; + } + sqlite3DbFree(db, zParent); + } + } + + zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), + sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); + sqlite3DbFree(db, zOutput); +} +#endif + +#ifndef SQLITE_OMIT_TRIGGER +/* This function is used by SQL generated to implement the +** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER +** statement. The second is a table name. The table name in the CREATE +** TRIGGER statement is replaced with the third argument and the result +** returned. This is analagous to renameTableFunc() above, except for CREATE +** TRIGGER, not CREATE INDEX and CREATE TABLE. +*/ +static void renameTriggerFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + unsigned char const *zSql = sqlite3_value_text(argv[0]); + unsigned char const *zTableName = sqlite3_value_text(argv[1]); + + int token; + Token tname; + int dist = 3; + unsigned char const *zCsr = zSql; + int len = 0; + char *zRet; + sqlite3 *db = sqlite3_context_db_handle(context); + + UNUSED_PARAMETER(NotUsed); + + /* The principle used to locate the table name in the CREATE TRIGGER + ** statement is that the table name is the first token that is immediately + ** preceded by either TK_ON or TK_DOT and immediately followed by one + ** of TK_WHEN, TK_BEGIN or TK_FOR. + */ + if( zSql ){ + do { + + if( !*zCsr ){ + /* Ran out of input before finding the table name. Return NULL. */ + return; + } + + /* Store the token that zCsr points to in tname. */ + tname.z = (char*)zCsr; + tname.n = len; + + /* Advance zCsr to the next token. Store that token type in 'token', + ** and its length in 'len' (to be used next iteration of this loop). + */ + do { + zCsr += len; + len = sqlite3GetToken(zCsr, &token); + }while( token==TK_SPACE ); + assert( len>0 ); + + /* Variable 'dist' stores the number of tokens read since the most + ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN + ** token is read and 'dist' equals 2, the condition stated above + ** to be met. + ** + ** Note that ON cannot be a database, table or column name, so + ** there is no need to worry about syntax like + ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. + */ + dist++; + if( token==TK_DOT || token==TK_ON ){ + dist = 0; + } + } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); + + /* Variable tname now contains the token that is the old table-name + ** in the CREATE TRIGGER statement. + */ + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), + zSql, zTableName, tname.z+tname.n); + sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); + } +} +#endif /* !SQLITE_OMIT_TRIGGER */ + +/* +** Register built-in functions used to help implement ALTER TABLE +*/ +SQLITE_PRIVATE void sqlite3AlterFunctions(void){ + static FuncDef aAlterTableFuncs[] = { + FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), +#ifndef SQLITE_OMIT_TRIGGER + FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), +#endif +#ifndef SQLITE_OMIT_FOREIGN_KEY + FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), +#endif + }; + sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); +} + +/* +** This function is used to create the text of expressions of the form: +** +** name= OR name= OR ... +** +** If argument zWhere is NULL, then a pointer string containing the text +** "name=" is returned, where is the quoted version +** of the string passed as argument zConstant. The returned buffer is +** allocated using sqlite3DbMalloc(). It is the responsibility of the +** caller to ensure that it is eventually freed. +** +** If argument zWhere is not NULL, then the string returned is +** " OR name=", where is the contents of zWhere. +** In this case zWhere is passed to sqlite3DbFree() before returning. +** +*/ +static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ + char *zNew; + if( !zWhere ){ + zNew = sqlite3MPrintf(db, "name=%Q", zConstant); + }else{ + zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); + sqlite3DbFree(db, zWhere); + } + return zNew; +} + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) +/* +** Generate the text of a WHERE expression which can be used to select all +** tables that have foreign key constraints that refer to table pTab (i.e. +** constraints for which pTab is the parent table) from the sqlite_master +** table. +*/ +static char *whereForeignKeys(Parse *pParse, Table *pTab){ + FKey *p; + char *zWhere = 0; + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); + } + return zWhere; +} +#endif + +/* +** Generate the text of a WHERE expression which can be used to select all +** temporary triggers on table pTab from the sqlite_temp_master table. If +** table pTab has no temporary triggers, or is itself stored in the +** temporary database, NULL is returned. +*/ +static char *whereTempTriggers(Parse *pParse, Table *pTab){ + Trigger *pTrig; + char *zWhere = 0; + const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ + + /* If the table is not located in the temp-db (in which case NULL is + ** returned, loop through the tables list of triggers. For each trigger + ** that is not part of the temp-db schema, add a clause to the WHERE + ** expression being built up in zWhere. + */ + if( pTab->pSchema!=pTempSchema ){ + sqlite3 *db = pParse->db; + for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ + if( pTrig->pSchema==pTempSchema ){ + zWhere = whereOrName(db, zWhere, pTrig->zName); + } + } + } + if( zWhere ){ + char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); + sqlite3DbFree(pParse->db, zWhere); + zWhere = zNew; + } + return zWhere; +} + +/* +** Generate code to drop and reload the internal representation of table +** pTab from the database, including triggers and temporary triggers. +** Argument zName is the name of the table in the database schema at +** the time the generated code is executed. This can be different from +** pTab->zName if this function is being called to code part of an +** "ALTER TABLE RENAME TO" statement. +*/ +static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ + Vdbe *v; + char *zWhere; + int iDb; /* Index of database containing pTab */ +#ifndef SQLITE_OMIT_TRIGGER + Trigger *pTrig; +#endif + + v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return; + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + assert( iDb>=0 ); + +#ifndef SQLITE_OMIT_TRIGGER + /* Drop any table triggers from the internal schema. */ + for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ + int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); + assert( iTrigDb==iDb || iTrigDb==1 ); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); + } +#endif + + /* Drop the table and index from the internal schema. */ + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + + /* Reload the table, index and permanent trigger schemas. */ + zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); + if( !zWhere ) return; + sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); + +#ifndef SQLITE_OMIT_TRIGGER + /* Now, if the table is not stored in the temp database, reload any temp + ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. + */ + if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ + sqlite3VdbeAddParseSchemaOp(v, 1, zWhere); + } +#endif +} + +/* +** Parameter zName is the name of a table that is about to be altered +** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). +** If the table is a system table, this function leaves an error message +** in pParse->zErr (system tables may not be altered) and returns non-zero. +** +** Or, if zName is not a system table, zero is returned. +*/ +static int isSystemTable(Parse *pParse, const char *zName){ + if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ + sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); + return 1; + } + return 0; +} + +/* +** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" +** command. +*/ +SQLITE_PRIVATE void sqlite3AlterRenameTable( + Parse *pParse, /* Parser context. */ + SrcList *pSrc, /* The table to rename. */ + Token *pName /* The new table name. */ +){ + int iDb; /* Database that contains the table */ + char *zDb; /* Name of database iDb */ + Table *pTab; /* Table being renamed */ + char *zName = 0; /* NULL-terminated version of pName */ + sqlite3 *db = pParse->db; /* Database connection */ + int nTabName; /* Number of UTF-8 characters in zTabName */ + const char *zTabName; /* Original name of the table */ + Vdbe *v; +#ifndef SQLITE_OMIT_TRIGGER + char *zWhere = 0; /* Where clause to locate temp triggers */ +#endif + VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ + int savedDbFlags; /* Saved value of db->flags */ + + savedDbFlags = db->flags; + if( NEVER(db->mallocFailed) ) goto exit_rename_table; + assert( pSrc->nSrc==1 ); + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + + pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); + if( !pTab ) goto exit_rename_table; + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + zDb = db->aDb[iDb].zName; + db->flags |= SQLITE_PreferBuiltin; + + /* Get a NULL terminated version of the new table name. */ + zName = sqlite3NameFromToken(db, pName); + if( !zName ) goto exit_rename_table; + + /* Check that a table or index named 'zName' does not already exist + ** in database iDb. If so, this is an error. + */ + if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ + sqlite3ErrorMsg(pParse, + "there is already another table or index with this name: %s", zName); + goto exit_rename_table; + } + + /* Make sure it is not a system table being altered, or a reserved name + ** that the table is being renamed to. + */ + if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ + goto exit_rename_table; + } + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto + exit_rename_table; + } + +#ifndef SQLITE_OMIT_VIEW + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); + goto exit_rename_table; + } +#endif + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ + goto exit_rename_table; + } +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto exit_rename_table; + } + if( IsVirtual(pTab) ){ + pVTab = sqlite3GetVTable(db, pTab); + if( pVTab->pVtab->pModule->xRename==0 ){ + pVTab = 0; + } + } +#endif + + /* Begin a transaction for database iDb. + ** Then modify the schema cookie (since the ALTER TABLE modifies the + ** schema). Open a statement transaction if the table is a virtual + ** table. + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ){ + goto exit_rename_table; + } + sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); + sqlite3ChangeCookie(pParse, iDb); + + /* If this is a virtual table, invoke the xRename() function if + ** one is defined. The xRename() callback will modify the names + ** of any resources used by the v-table implementation (including other + ** SQLite tables) that are identified by the name of the virtual table. + */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pVTab ){ + int i = ++pParse->nMem; + sqlite3VdbeLoadString(v, i, zName); + sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); + sqlite3MayAbort(pParse); + } +#endif + + /* figure out how many UTF-8 characters are in zName */ + zTabName = pTab->zName; + nTabName = sqlite3Utf8CharLen(zTabName, -1); + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + if( db->flags&SQLITE_ForeignKeys ){ + /* If foreign-key support is enabled, rewrite the CREATE TABLE + ** statements corresponding to all child tables of foreign key constraints + ** for which the renamed table is the parent table. */ + if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ + sqlite3NestedParse(pParse, + "UPDATE \"%w\".%s SET " + "sql = sqlite_rename_parent(sql, %Q, %Q) " + "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere); + sqlite3DbFree(db, zWhere); + } + } +#endif + + /* Modify the sqlite_master table to use the new table name. */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s SET " +#ifdef SQLITE_OMIT_TRIGGER + "sql = sqlite_rename_table(sql, %Q), " +#else + "sql = CASE " + "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" + "ELSE sqlite_rename_table(sql, %Q) END, " +#endif + "tbl_name = %Q, " + "name = CASE " + "WHEN type='table' THEN %Q " + "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " + "'sqlite_autoindex_' || %Q || substr(name,%d+18) " + "ELSE name END " + "WHERE tbl_name=%Q COLLATE nocase AND " + "(type='table' OR type='index' OR type='trigger');", + zDb, SCHEMA_TABLE(iDb), zName, zName, zName, +#ifndef SQLITE_OMIT_TRIGGER + zName, +#endif + zName, nTabName, zTabName + ); + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* If the sqlite_sequence table exists in this database, then update + ** it with the new table name. + */ + if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ + sqlite3NestedParse(pParse, + "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", + zDb, zName, pTab->zName); + } +#endif + +#ifndef SQLITE_OMIT_TRIGGER + /* If there are TEMP triggers on this table, modify the sqlite_temp_master + ** table. Don't do this if the table being ALTERed is itself located in + ** the temp database. + */ + if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ + sqlite3NestedParse(pParse, + "UPDATE sqlite_temp_master SET " + "sql = sqlite_rename_trigger(sql, %Q), " + "tbl_name = %Q " + "WHERE %s;", zName, zName, zWhere); + sqlite3DbFree(db, zWhere); + } +#endif + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + if( db->flags&SQLITE_ForeignKeys ){ + FKey *p; + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + Table *pFrom = p->pFrom; + if( pFrom!=pTab ){ + reloadTableSchema(pParse, p->pFrom, pFrom->zName); + } + } + } +#endif + + /* Drop and reload the internal table schema. */ + reloadTableSchema(pParse, pTab, zName); + +exit_rename_table: + sqlite3SrcListDelete(db, pSrc); + sqlite3DbFree(db, zName); + db->flags = savedDbFlags; +} + +/* +** This function is called after an "ALTER TABLE ... ADD" statement +** has been parsed. Argument pColDef contains the text of the new +** column definition. +** +** The Table structure pParse->pNewTable was extended to include +** the new column during parsing. +*/ +SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ + Table *pNew; /* Copy of pParse->pNewTable */ + Table *pTab; /* Table being altered */ + int iDb; /* Database number */ + const char *zDb; /* Database name */ + const char *zTab; /* Table name */ + char *zCol; /* Null-terminated column definition */ + Column *pCol; /* The new column */ + Expr *pDflt; /* Default value for the new column */ + sqlite3 *db; /* The database connection; */ + Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ + + db = pParse->db; + if( pParse->nErr || db->mallocFailed ) return; + assert( v!=0 ); + pNew = pParse->pNewTable; + assert( pNew ); + + assert( sqlite3BtreeHoldsAllMutexes(db) ); + iDb = sqlite3SchemaToIndex(db, pNew->pSchema); + zDb = db->aDb[iDb].zName; + zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ + pCol = &pNew->aCol[pNew->nCol-1]; + pDflt = pCol->pDflt; + pTab = sqlite3FindTable(db, zTab, zDb); + assert( pTab ); + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ + return; + } +#endif + + /* If the default value for the new column was specified with a + ** literal NULL, then set pDflt to 0. This simplifies checking + ** for an SQL NULL default below. + */ + assert( pDflt==0 || pDflt->op==TK_SPAN ); + if( pDflt && pDflt->pLeft->op==TK_NULL ){ + pDflt = 0; + } + + /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. + ** If there is a NOT NULL constraint, then the default value for the + ** column must not be NULL. + */ + if( pCol->colFlags & COLFLAG_PRIMKEY ){ + sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); + return; + } + if( pNew->pIndex ){ + sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); + return; + } + if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ + sqlite3ErrorMsg(pParse, + "Cannot add a REFERENCES column with non-NULL default value"); + return; + } + if( pCol->notNull && !pDflt ){ + sqlite3ErrorMsg(pParse, + "Cannot add a NOT NULL column with default value NULL"); + return; + } + + /* Ensure the default expression is something that sqlite3ValueFromExpr() + ** can handle (i.e. not CURRENT_TIME etc.) + */ + if( pDflt ){ + sqlite3_value *pVal = 0; + int rc; + rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + if( rc!=SQLITE_OK ){ + assert( db->mallocFailed == 1 ); + return; + } + if( !pVal ){ + sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); + return; + } + sqlite3ValueFree(pVal); + } + + /* Modify the CREATE TABLE statement. */ + zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); + if( zCol ){ + char *zEnd = &zCol[pColDef->n-1]; + int savedDbFlags = db->flags; + while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ + *zEnd-- = '\0'; + } + db->flags |= SQLITE_PreferBuiltin; + sqlite3NestedParse(pParse, + "UPDATE \"%w\".%s SET " + "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " + "WHERE type = 'table' AND name = %Q", + zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, + zTab + ); + sqlite3DbFree(db, zCol); + db->flags = savedDbFlags; + } + + /* If the default value of the new column is NULL, then the file + ** format to 2. If the default value of the new column is not NULL, + ** the file format be 3. Back when this feature was first added + ** in 2006, we went to the trouble to upgrade the file format to the + ** minimum support values. But 10-years on, we can assume that all + ** extent versions of SQLite support file-format 4, so we always and + ** unconditionally upgrade to 4. + */ + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, + SQLITE_MAX_FILE_FORMAT); + + /* Reload the schema of the modified table. */ + reloadTableSchema(pParse, pTab, pTab->zName); +} + +/* +** This function is called by the parser after the table-name in +** an "ALTER TABLE ADD" statement is parsed. Argument +** pSrc is the full-name of the table being altered. +** +** This routine makes a (partial) copy of the Table structure +** for the table being altered and sets Parse.pNewTable to point +** to it. Routines called by the parser as the column definition +** is parsed (i.e. sqlite3AddColumn()) add the new Column data to +** the copy. The copy of the Table structure is deleted by tokenize.c +** after parsing is finished. +** +** Routine sqlite3AlterFinishAddColumn() will be called to complete +** coding the "ALTER TABLE ... ADD" statement. +*/ +SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ + Table *pNew; + Table *pTab; + Vdbe *v; + int iDb; + int i; + int nAlloc; + sqlite3 *db = pParse->db; + + /* Look up the table being altered. */ + assert( pParse->pNewTable==0 ); + assert( sqlite3BtreeHoldsAllMutexes(db) ); + if( db->mallocFailed ) goto exit_begin_add_column; + pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); + if( !pTab ) goto exit_begin_add_column; + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); + goto exit_begin_add_column; + } +#endif + + /* Make sure this is not an attempt to ALTER a view. */ + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); + goto exit_begin_add_column; + } + if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ + goto exit_begin_add_column; + } + + assert( pTab->addColOffset>0 ); + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + + /* Put a copy of the Table struct in Parse.pNewTable for the + ** sqlite3AddColumn() function and friends to modify. But modify + ** the name by adding an "sqlite_altertab_" prefix. By adding this + ** prefix, we insure that the name will not collide with an existing + ** table because user table are not allowed to have the "sqlite_" + ** prefix on their name. + */ + pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); + if( !pNew ) goto exit_begin_add_column; + pParse->pNewTable = pNew; + pNew->nRef = 1; + pNew->nCol = pTab->nCol; + assert( pNew->nCol>0 ); + nAlloc = (((pNew->nCol-1)/8)*8)+8; + assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); + pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); + pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); + if( !pNew->aCol || !pNew->zName ){ + assert( db->mallocFailed ); + goto exit_begin_add_column; + } + memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); + for(i=0; inCol; i++){ + Column *pCol = &pNew->aCol[i]; + pCol->zName = sqlite3DbStrDup(db, pCol->zName); + pCol->zColl = 0; + pCol->pDflt = 0; + } + pNew->pSchema = db->aDb[iDb].pSchema; + pNew->addColOffset = pTab->addColOffset; + pNew->nRef = 1; + + /* Begin a transaction and increment the schema cookie. */ + sqlite3BeginWriteOperation(pParse, 0, iDb); + v = sqlite3GetVdbe(pParse); + if( !v ) goto exit_begin_add_column; + sqlite3ChangeCookie(pParse, iDb); + +exit_begin_add_column: + sqlite3SrcListDelete(db, pSrc); + return; +} +#endif /* SQLITE_ALTER_TABLE */ + +/************** End of alter.c ***********************************************/ +/************** Begin file analyze.c *****************************************/ +/* +** 2005-07-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code associated with the ANALYZE command. +** +** The ANALYZE command gather statistics about the content of tables +** and indices. These statistics are made available to the query planner +** to help it make better decisions about how to perform queries. +** +** The following system tables are or have been supported: +** +** CREATE TABLE sqlite_stat1(tbl, idx, stat); +** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); +** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); +** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample); +** +** Additional tables might be added in future releases of SQLite. +** The sqlite_stat2 table is not created or used unless the SQLite version +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled +** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. +** The sqlite_stat2 table is superseded by sqlite_stat3, which is only +** created and used by SQLite versions 3.7.9 and later and with +** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3 +** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced +** version of sqlite_stat3 and is only available when compiled with +** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is +** not possible to enable both STAT3 and STAT4 at the same time. If they +** are both enabled, then STAT4 takes precedence. +** +** For most applications, sqlite_stat1 provides all the statistics required +** for the query planner to make good choices. +** +** Format of sqlite_stat1: +** +** There is normally one row per index, with the index identified by the +** name in the idx column. The tbl column is the name of the table to +** which the index belongs. In each such row, the stat column will be +** a string consisting of a list of integers. The first integer in this +** list is the number of rows in the index. (This is the same as the +** number of rows in the table, except for partial indices.) The second +** integer is the average number of rows in the index that have the same +** value in the first column of the index. The third integer is the average +** number of rows in the index that have the same value for the first two +** columns. The N-th integer (for N>1) is the average number of rows in +** the index which have the same value for the first N-1 columns. For +** a K-column index, there will be K+1 integers in the stat column. If +** the index is unique, then the last integer will be 1. +** +** The list of integers in the stat column can optionally be followed +** by the keyword "unordered". The "unordered" keyword, if it is present, +** must be separated from the last integer by a single space. If the +** "unordered" keyword is present, then the query planner assumes that +** the index is unordered and will not use the index for a range query. +** +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat +** column contains a single integer which is the (estimated) number of +** rows in the table identified by sqlite_stat1.tbl. +** +** Format of sqlite_stat2: +** +** The sqlite_stat2 is only created and is only used if SQLite is compiled +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between +** 3.6.18 and 3.7.8. The "stat2" table contains additional information +** about the distribution of keys within an index. The index is identified by +** the "idx" column and the "tbl" column is the name of the table to which +** the index belongs. There are usually 10 rows in the sqlite_stat2 +** table for each index. +** +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 +** inclusive are samples of the left-most key value in the index taken at +** evenly spaced points along the index. Let the number of samples be S +** (10 in the standard build) and let C be the number of rows in the index. +** Then the sampled rows are given by: +** +** rownumber = (i*C*2 + C)/(S*2) +** +** For i between 0 and S-1. Conceptually, the index space is divided into +** S uniform buckets and the samples are the middle row from each bucket. +** +** The format for sqlite_stat2 is recorded here for legacy reference. This +** version of SQLite does not support sqlite_stat2. It neither reads nor +** writes the sqlite_stat2 table. This version of SQLite only supports +** sqlite_stat3. +** +** Format for sqlite_stat3: +** +** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the +** sqlite_stat4 format will be described first. Further information +** about sqlite_stat3 follows the sqlite_stat4 description. +** +** Format for sqlite_stat4: +** +** As with sqlite_stat2, the sqlite_stat4 table contains histogram data +** to aid the query planner in choosing good indices based on the values +** that indexed columns are compared against in the WHERE clauses of +** queries. +** +** The sqlite_stat4 table contains multiple entries for each index. +** The idx column names the index and the tbl column is the table of the +** index. If the idx and tbl columns are the same, then the sample is +** of the INTEGER PRIMARY KEY. The sample column is a blob which is the +** binary encoding of a key from the index. The nEq column is a +** list of integers. The first integer is the approximate number +** of entries in the index whose left-most column exactly matches +** the left-most column of the sample. The second integer in nEq +** is the approximate number of entries in the index where the +** first two columns match the first two columns of the sample. +** And so forth. nLt is another list of integers that show the approximate +** number of entries that are strictly less than the sample. The first +** integer in nLt contains the number of entries in the index where the +** left-most column is less than the left-most column of the sample. +** The K-th integer in the nLt entry is the number of index entries +** where the first K columns are less than the first K columns of the +** sample. The nDLt column is like nLt except that it contains the +** number of distinct entries in the index that are less than the +** sample. +** +** There can be an arbitrary number of sqlite_stat4 entries per index. +** The ANALYZE command will typically generate sqlite_stat4 tables +** that contain between 10 and 40 samples which are distributed across +** the key space, though not uniformly, and which include samples with +** large nEq values. +** +** Format for sqlite_stat3 redux: +** +** The sqlite_stat3 table is like sqlite_stat4 except that it only +** looks at the left-most column of the index. The sqlite_stat3.sample +** column contains the actual value of the left-most column instead +** of a blob encoding of the complete index key as is found in +** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3 +** all contain just a single integer which is the same as the first +** integer in the equivalent columns in sqlite_stat4. +*/ +#ifndef SQLITE_OMIT_ANALYZE +/* #include "sqliteInt.h" */ + +#if defined(SQLITE_ENABLE_STAT4) +# define IsStat4 1 +# define IsStat3 0 +#elif defined(SQLITE_ENABLE_STAT3) +# define IsStat4 0 +# define IsStat3 1 +#else +# define IsStat4 0 +# define IsStat3 0 +# undef SQLITE_STAT4_SAMPLES +# define SQLITE_STAT4_SAMPLES 1 +#endif +#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */ + +/* +** This routine generates code that opens the sqlite_statN tables. +** The sqlite_stat1 table is always relevant. sqlite_stat2 is now +** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when +** appropriate compile-time options are provided. +** +** If the sqlite_statN tables do not previously exist, it is created. +** +** Argument zWhere may be a pointer to a buffer containing a table name, +** or it may be a NULL pointer. If it is not NULL, then all entries in +** the sqlite_statN tables associated with the named table are deleted. +** If zWhere==0, then code is generated to delete all stat table entries. +*/ +static void openStatTable( + Parse *pParse, /* Parsing context */ + int iDb, /* The database we are looking in */ + int iStatCur, /* Open the sqlite_stat1 table on this cursor */ + const char *zWhere, /* Delete entries for this table or index */ + const char *zWhereType /* Either "tbl" or "idx" */ +){ + static const struct { + const char *zName; + const char *zCols; + } aTable[] = { + { "sqlite_stat1", "tbl,idx,stat" }, +#if defined(SQLITE_ENABLE_STAT4) + { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" }, + { "sqlite_stat3", 0 }, +#elif defined(SQLITE_ENABLE_STAT3) + { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, + { "sqlite_stat4", 0 }, +#else + { "sqlite_stat3", 0 }, + { "sqlite_stat4", 0 }, +#endif + }; + int i; + sqlite3 *db = pParse->db; + Db *pDb; + Vdbe *v = sqlite3GetVdbe(pParse); + int aRoot[ArraySize(aTable)]; + u8 aCreateTbl[ArraySize(aTable)]; + + if( v==0 ) return; + assert( sqlite3BtreeHoldsAllMutexes(db) ); + assert( sqlite3VdbeDb(v)==db ); + pDb = &db->aDb[iDb]; + + /* Create new statistic tables if they do not exist, or clear them + ** if they do already exist. + */ + for(i=0; izName))==0 ){ + if( aTable[i].zCols ){ + /* The sqlite_statN table does not exist. Create it. Note that a + ** side-effect of the CREATE TABLE statement is to leave the rootpage + ** of the new table in register pParse->regRoot. This is important + ** because the OpenWrite opcode below will be needing it. */ + sqlite3NestedParse(pParse, + "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols + ); + aRoot[i] = pParse->regRoot; + aCreateTbl[i] = OPFLAG_P2ISREG; + } + }else{ + /* The table already exists. If zWhere is not NULL, delete all entries + ** associated with the table zWhere. If zWhere is NULL, delete the + ** entire contents of the table. */ + aRoot[i] = pStat->tnum; + aCreateTbl[i] = 0; + sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); + if( zWhere ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE %s=%Q", + pDb->zName, zTab, zWhereType, zWhere + ); + }else{ + /* The sqlite_stat[134] table already exists. Delete all rows. */ + sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); + } + } + } + + /* Open the sqlite_stat[134] tables for writing. */ + for(i=0; aTable[i].zCols; i++){ + assert( inRowid ){ + sqlite3DbFree(db, p->u.aRowid); + p->nRowid = 0; + } +} +#endif + +/* Initialize the BLOB value of a ROWID +*/ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){ + assert( db!=0 ); + if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); + p->u.aRowid = sqlite3DbMallocRawNN(db, n); + if( p->u.aRowid ){ + p->nRowid = n; + memcpy(p->u.aRowid, pData, n); + }else{ + p->nRowid = 0; + } +} +#endif + +/* Initialize the INTEGER value of a ROWID. +*/ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){ + assert( db!=0 ); + if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); + p->nRowid = 0; + p->u.iRowid = iRowid; +} +#endif + + +/* +** Copy the contents of object (*pFrom) into (*pTo). +*/ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){ + pTo->isPSample = pFrom->isPSample; + pTo->iCol = pFrom->iCol; + pTo->iHash = pFrom->iHash; + memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol); + memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol); + memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol); + if( pFrom->nRowid ){ + sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid); + }else{ + sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid); + } +} +#endif + +/* +** Reclaim all memory of a Stat4Accum structure. +*/ +static void stat4Destructor(void *pOld){ + Stat4Accum *p = (Stat4Accum*)pOld; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int i; + for(i=0; inCol; i++) sampleClear(p->db, p->aBest+i); + for(i=0; imxSample; i++) sampleClear(p->db, p->a+i); + sampleClear(p->db, &p->current); +#endif + sqlite3DbFree(p->db, p); +} + +/* +** Implementation of the stat_init(N,K,C) SQL function. The three parameters +** are: +** N: The number of columns in the index including the rowid/pk (note 1) +** K: The number of columns in the index excluding the rowid/pk. +** C: The number of rows in the index (note 2) +** +** Note 1: In the special case of the covering index that implements a +** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the +** total number of columns in the table. +** +** Note 2: C is only used for STAT3 and STAT4. +** +** For indexes on ordinary rowid tables, N==K+1. But for indexes on +** WITHOUT ROWID tables, N=K+P where P is the number of columns in the +** PRIMARY KEY of the table. The covering index that implements the +** original WITHOUT ROWID table as N==K as a special case. +** +** This routine allocates the Stat4Accum object in heap memory. The return +** value is a pointer to the Stat4Accum object. The datatype of the +** return value is BLOB, but it is really just a pointer to the Stat4Accum +** object. +*/ +static void statInit( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Stat4Accum *p; + int nCol; /* Number of columns in index being sampled */ + int nKeyCol; /* Number of key columns */ + int nColUp; /* nCol rounded up for alignment */ + int n; /* Bytes of space to allocate */ + sqlite3 *db; /* Database connection */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int mxSample = SQLITE_STAT4_SAMPLES; +#endif + + /* Decode the three function arguments */ + UNUSED_PARAMETER(argc); + nCol = sqlite3_value_int(argv[0]); + assert( nCol>0 ); + nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol; + nKeyCol = sqlite3_value_int(argv[1]); + assert( nKeyCol<=nCol ); + assert( nKeyCol>0 ); + + /* Allocate the space required for the Stat4Accum object */ + n = sizeof(*p) + + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */ + + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */ + + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */ + + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample) +#endif + ; + db = sqlite3_context_db_handle(context); + p = sqlite3DbMallocZero(db, n); + if( p==0 ){ + sqlite3_result_error_nomem(context); + return; + } + + p->db = db; + p->nRow = 0; + p->nCol = nCol; + p->nKeyCol = nKeyCol; + p->current.anDLt = (tRowcnt*)&p[1]; + p->current.anEq = &p->current.anDLt[nColUp]; + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + { + u8 *pSpace; /* Allocated space not yet assigned */ + int i; /* Used to iterate through p->aSample[] */ + + p->iGet = -1; + p->mxSample = mxSample; + p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); + p->current.anLt = &p->current.anEq[nColUp]; + p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); + + /* Set up the Stat4Accum.a[] and aBest[] arrays */ + p->a = (struct Stat4Sample*)&p->current.anLt[nColUp]; + p->aBest = &p->a[mxSample]; + pSpace = (u8*)(&p->a[mxSample+nCol]); + for(i=0; i<(mxSample+nCol); i++){ + p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); + p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); + p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); + } + assert( (pSpace - (u8*)p)==n ); + + for(i=0; iaBest[i].iCol = i; + } + } +#endif + + /* Return a pointer to the allocated object to the caller. Note that + ** only the pointer (the 2nd parameter) matters. The size of the object + ** (given by the 3rd parameter) is never used and can be any positive + ** value. */ + sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor); +} +static const FuncDef statInitFuncdef = { + 2+IsStat34, /* nArg */ + SQLITE_UTF8, /* funcFlags */ + 0, /* pUserData */ + 0, /* pNext */ + statInit, /* xSFunc */ + 0, /* xFinalize */ + "stat_init", /* zName */ + {0} +}; + +#ifdef SQLITE_ENABLE_STAT4 +/* +** pNew and pOld are both candidate non-periodic samples selected for +** the same column (pNew->iCol==pOld->iCol). Ignoring this column and +** considering only any trailing columns and the sample hash value, this +** function returns true if sample pNew is to be preferred over pOld. +** In other words, if we assume that the cardinalities of the selected +** column for pNew and pOld are equal, is pNew to be preferred over pOld. +** +** This function assumes that for each argument sample, the contents of +** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. +*/ +static int sampleIsBetterPost( + Stat4Accum *pAccum, + Stat4Sample *pNew, + Stat4Sample *pOld +){ + int nCol = pAccum->nCol; + int i; + assert( pNew->iCol==pOld->iCol ); + for(i=pNew->iCol+1; ianEq[i]>pOld->anEq[i] ) return 1; + if( pNew->anEq[i]anEq[i] ) return 0; + } + if( pNew->iHash>pOld->iHash ) return 1; + return 0; +} +#endif + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Return true if pNew is to be preferred over pOld. +** +** This function assumes that for each argument sample, the contents of +** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. +*/ +static int sampleIsBetter( + Stat4Accum *pAccum, + Stat4Sample *pNew, + Stat4Sample *pOld +){ + tRowcnt nEqNew = pNew->anEq[pNew->iCol]; + tRowcnt nEqOld = pOld->anEq[pOld->iCol]; + + assert( pOld->isPSample==0 && pNew->isPSample==0 ); + assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) ); + + if( (nEqNew>nEqOld) ) return 1; +#ifdef SQLITE_ENABLE_STAT4 + if( nEqNew==nEqOld ){ + if( pNew->iColiCol ) return 1; + return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld)); + } + return 0; +#else + return (nEqNew==nEqOld && pNew->iHash>pOld->iHash); +#endif +} + +/* +** Copy the contents of sample *pNew into the p->a[] array. If necessary, +** remove the least desirable sample from p->a[] to make room. +*/ +static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ + Stat4Sample *pSample = 0; + int i; + + assert( IsStat4 || nEqZero==0 ); + +#ifdef SQLITE_ENABLE_STAT4 + if( pNew->isPSample==0 ){ + Stat4Sample *pUpgrade = 0; + assert( pNew->anEq[pNew->iCol]>0 ); + + /* This sample is being added because the prefix that ends in column + ** iCol occurs many times in the table. However, if we have already + ** added a sample that shares this prefix, there is no need to add + ** this one. Instead, upgrade the priority of the highest priority + ** existing sample that shares this prefix. */ + for(i=p->nSample-1; i>=0; i--){ + Stat4Sample *pOld = &p->a[i]; + if( pOld->anEq[pNew->iCol]==0 ){ + if( pOld->isPSample ) return; + assert( pOld->iCol>pNew->iCol ); + assert( sampleIsBetter(p, pNew, pOld) ); + if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){ + pUpgrade = pOld; + } + } + } + if( pUpgrade ){ + pUpgrade->iCol = pNew->iCol; + pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol]; + goto find_new_min; + } + } +#endif + + /* If necessary, remove sample iMin to make room for the new sample. */ + if( p->nSample>=p->mxSample ){ + Stat4Sample *pMin = &p->a[p->iMin]; + tRowcnt *anEq = pMin->anEq; + tRowcnt *anLt = pMin->anLt; + tRowcnt *anDLt = pMin->anDLt; + sampleClear(p->db, pMin); + memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1)); + pSample = &p->a[p->nSample-1]; + pSample->nRowid = 0; + pSample->anEq = anEq; + pSample->anDLt = anDLt; + pSample->anLt = anLt; + p->nSample = p->mxSample-1; + } + + /* The "rows less-than" for the rowid column must be greater than that + ** for the last sample in the p->a[] array. Otherwise, the samples would + ** be out of order. */ +#ifdef SQLITE_ENABLE_STAT4 + assert( p->nSample==0 + || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] ); +#endif + + /* Insert the new sample */ + pSample = &p->a[p->nSample]; + sampleCopy(p, pSample, pNew); + p->nSample++; + + /* Zero the first nEqZero entries in the anEq[] array. */ + memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero); + +#ifdef SQLITE_ENABLE_STAT4 + find_new_min: +#endif + if( p->nSample>=p->mxSample ){ + int iMin = -1; + for(i=0; imxSample; i++){ + if( p->a[i].isPSample ) continue; + if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){ + iMin = i; + } + } + assert( iMin>=0 ); + p->iMin = iMin; + } +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +/* +** Field iChng of the index being scanned has changed. So at this point +** p->current contains a sample that reflects the previous row of the +** index. The value of anEq[iChng] and subsequent anEq[] elements are +** correct at this point. +*/ +static void samplePushPrevious(Stat4Accum *p, int iChng){ +#ifdef SQLITE_ENABLE_STAT4 + int i; + + /* Check if any samples from the aBest[] array should be pushed + ** into IndexSample.a[] at this point. */ + for(i=(p->nCol-2); i>=iChng; i--){ + Stat4Sample *pBest = &p->aBest[i]; + pBest->anEq[i] = p->current.anEq[i]; + if( p->nSamplemxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){ + sampleInsert(p, pBest, i); + } + } + + /* Update the anEq[] fields of any samples already collected. */ + for(i=p->nSample-1; i>=0; i--){ + int j; + for(j=iChng; jnCol; j++){ + if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j]; + } + } +#endif + +#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4) + if( iChng==0 ){ + tRowcnt nLt = p->current.anLt[0]; + tRowcnt nEq = p->current.anEq[0]; + + /* Check if this is to be a periodic sample. If so, add it. */ + if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){ + p->current.isPSample = 1; + sampleInsert(p, &p->current, 0); + p->current.isPSample = 0; + }else + + /* Or if it is a non-periodic sample. Add it in this case too. */ + if( p->nSamplemxSample + || sampleIsBetter(p, &p->current, &p->a[p->iMin]) + ){ + sampleInsert(p, &p->current, 0); + } + } +#endif + +#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 + UNUSED_PARAMETER( p ); + UNUSED_PARAMETER( iChng ); +#endif +} + +/* +** Implementation of the stat_push SQL function: stat_push(P,C,R) +** Arguments: +** +** P Pointer to the Stat4Accum object created by stat_init() +** C Index of left-most column to differ from previous row +** R Rowid for the current row. Might be a key record for +** WITHOUT ROWID tables. +** +** This SQL function always returns NULL. It's purpose it to accumulate +** statistical data and/or samples in the Stat4Accum object about the +** index being analyzed. The stat_get() SQL function will later be used to +** extract relevant information for constructing the sqlite_statN tables. +** +** The R parameter is only used for STAT3 and STAT4 +*/ +static void statPush( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i; + + /* The three function arguments */ + Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); + int iChng = sqlite3_value_int(argv[1]); + + UNUSED_PARAMETER( argc ); + UNUSED_PARAMETER( context ); + assert( p->nCol>0 ); + assert( iChngnCol ); + + if( p->nRow==0 ){ + /* This is the first call to this function. Do initialization. */ + for(i=0; inCol; i++) p->current.anEq[i] = 1; + }else{ + /* Second and subsequent calls get processed here */ + samplePushPrevious(p, iChng); + + /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply + ** to the current row of the index. */ + for(i=0; icurrent.anEq[i]++; + } + for(i=iChng; inCol; i++){ + p->current.anDLt[i]++; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + p->current.anLt[i] += p->current.anEq[i]; +#endif + p->current.anEq[i] = 1; + } + } + p->nRow++; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ + sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); + }else{ + sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), + sqlite3_value_blob(argv[2])); + } + p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; +#endif + +#ifdef SQLITE_ENABLE_STAT4 + { + tRowcnt nLt = p->current.anLt[p->nCol-1]; + + /* Check if this is to be a periodic sample. If so, add it. */ + if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ + p->current.isPSample = 1; + p->current.iCol = 0; + sampleInsert(p, &p->current, p->nCol-1); + p->current.isPSample = 0; + } + + /* Update the aBest[] array. */ + for(i=0; i<(p->nCol-1); i++){ + p->current.iCol = i; + if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){ + sampleCopy(p, &p->aBest[i], &p->current); + } + } + } +#endif +} +static const FuncDef statPushFuncdef = { + 2+IsStat34, /* nArg */ + SQLITE_UTF8, /* funcFlags */ + 0, /* pUserData */ + 0, /* pNext */ + statPush, /* xSFunc */ + 0, /* xFinalize */ + "stat_push", /* zName */ + {0} +}; + +#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */ +#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */ +#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */ +#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */ +#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */ + +/* +** Implementation of the stat_get(P,J) SQL function. This routine is +** used to query statistical information that has been gathered into +** the Stat4Accum object by prior calls to stat_push(). The P parameter +** has type BLOB but it is really just a pointer to the Stat4Accum object. +** The content to returned is determined by the parameter J +** which is one of the STAT_GET_xxxx values defined above. +** +** If neither STAT3 nor STAT4 are enabled, then J is always +** STAT_GET_STAT1 and is hence omitted and this routine becomes +** a one-parameter function, stat_get(P), that always returns the +** stat1 table entry information. +*/ +static void statGet( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + /* STAT3 and STAT4 have a parameter on this routine. */ + int eCall = sqlite3_value_int(argv[1]); + assert( argc==2 ); + assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ + || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT + || eCall==STAT_GET_NDLT + ); + if( eCall==STAT_GET_STAT1 ) +#else + assert( argc==1 ); +#endif + { + /* Return the value to store in the "stat" column of the sqlite_stat1 + ** table for this index. + ** + ** The value is a string composed of a list of integers describing + ** the index. The first integer in the list is the total number of + ** entries in the index. There is one additional integer in the list + ** for each indexed column. This additional integer is an estimate of + ** the number of rows matched by a stabbing query on the index using + ** a key with the corresponding number of fields. In other words, + ** if the index is on columns (a,b) and the sqlite_stat1 value is + ** "100 10 2", then SQLite estimates that: + ** + ** * the index contains 100 rows, + ** * "WHERE a=?" matches 10 rows, and + ** * "WHERE a=? AND b=?" matches 2 rows. + ** + ** If D is the count of distinct values and K is the total number of + ** rows, then each estimate is computed as: + ** + ** I = (K+D-1)/D + */ + char *z; + int i; + + char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 ); + if( zRet==0 ){ + sqlite3_result_error_nomem(context); + return; + } + + sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); + z = zRet + sqlite3Strlen30(zRet); + for(i=0; inKeyCol; i++){ + u64 nDistinct = p->current.anDLt[i] + 1; + u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; + sqlite3_snprintf(24, z, " %llu", iVal); + z += sqlite3Strlen30(z); + assert( p->current.anEq[i] ); + } + assert( z[0]=='\0' && z>zRet ); + + sqlite3_result_text(context, zRet, -1, sqlite3_free); + } +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + else if( eCall==STAT_GET_ROWID ){ + if( p->iGet<0 ){ + samplePushPrevious(p, 0); + p->iGet = 0; + } + if( p->iGetnSample ){ + Stat4Sample *pS = p->a + p->iGet; + if( pS->nRowid==0 ){ + sqlite3_result_int64(context, pS->u.iRowid); + }else{ + sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid, + SQLITE_TRANSIENT); + } + } + }else{ + tRowcnt *aCnt = 0; + + assert( p->iGetnSample ); + switch( eCall ){ + case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break; + case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break; + default: { + aCnt = p->a[p->iGet].anDLt; + p->iGet++; + break; + } + } + + if( IsStat3 ){ + sqlite3_result_int64(context, (i64)aCnt[0]); + }else{ + char *zRet = sqlite3MallocZero(p->nCol * 25); + if( zRet==0 ){ + sqlite3_result_error_nomem(context); + }else{ + int i; + char *z = zRet; + for(i=0; inCol; i++){ + sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]); + z += sqlite3Strlen30(z); + } + assert( z[0]=='\0' && z>zRet ); + z[-1] = '\0'; + sqlite3_result_text(context, zRet, -1, sqlite3_free); + } + } + } +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#ifndef SQLITE_DEBUG + UNUSED_PARAMETER( argc ); +#endif +} +static const FuncDef statGetFuncdef = { + 1+IsStat34, /* nArg */ + SQLITE_UTF8, /* funcFlags */ + 0, /* pUserData */ + 0, /* pNext */ + statGet, /* xSFunc */ + 0, /* xFinalize */ + "stat_get", /* zName */ + {0} +}; + +static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){ + assert( regOut!=regStat4 && regOut!=regStat4+1 ); +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1); +#elif SQLITE_DEBUG + assert( iParam==STAT_GET_STAT1 ); +#else + UNUSED_PARAMETER( iParam ); +#endif + sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut, + (char*)&statGetFuncdef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, 1 + IsStat34); +} + +/* +** Generate code to do an analysis of all indices associated with +** a single table. +*/ +static void analyzeOneTable( + Parse *pParse, /* Parser context */ + Table *pTab, /* Table whose indices are to be analyzed */ + Index *pOnlyIdx, /* If not NULL, only analyze this one index */ + int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ + int iMem, /* Available memory locations begin here */ + int iTab /* Next available cursor */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + Index *pIdx; /* An index to being analyzed */ + int iIdxCur; /* Cursor open on index being analyzed */ + int iTabCur; /* Table cursor */ + Vdbe *v; /* The virtual machine being built up */ + int i; /* Loop counter */ + int jZeroRows = -1; /* Jump from here if number of rows is zero */ + int iDb; /* Index of database containing pTab */ + u8 needTableCnt = 1; /* True to count the table */ + int regNewRowid = iMem++; /* Rowid for the inserted record */ + int regStat4 = iMem++; /* Register to hold Stat4Accum object */ + int regChng = iMem++; /* Index of changed index field */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int regRowid = iMem++; /* Rowid argument passed to stat_push() */ +#endif + int regTemp = iMem++; /* Temporary use register */ + int regTabname = iMem++; /* Register containing table name */ + int regIdxname = iMem++; /* Register containing index name */ + int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ + int regPrev = iMem; /* MUST BE LAST (see below) */ + + pParse->nMem = MAX(pParse->nMem, iMem); + v = sqlite3GetVdbe(pParse); + if( v==0 || NEVER(pTab==0) ){ + return; + } + if( pTab->tnum==0 ){ + /* Do not gather statistics on views or virtual tables */ + return; + } + if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){ + /* Do not gather statistics on system tables */ + return; + } + assert( sqlite3BtreeHoldsAllMutexes(db) ); + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 ); + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); +#ifndef SQLITE_OMIT_AUTHORIZATION + if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, + db->aDb[iDb].zName ) ){ + return; + } +#endif + + /* Establish a read-lock on the table at the shared-cache level. + ** Open a read-only cursor on the table. Also allocate a cursor number + ** to use for scanning indexes (iIdxCur). No index cursor is opened at + ** this time though. */ + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + iTabCur = iTab++; + iIdxCur = iTab++; + pParse->nTab = MAX(pParse->nTab, iTab); + sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); + sqlite3VdbeLoadString(v, regTabname, pTab->zName); + + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int nCol; /* Number of columns in pIdx. "N" */ + int addrRewind; /* Address of "OP_Rewind iIdxCur" */ + int addrNextRow; /* Address of "next_row:" */ + const char *zIdxName; /* Name of the index */ + int nColTest; /* Number of columns to test for changes */ + + if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; + if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0; + if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){ + nCol = pIdx->nKeyCol; + zIdxName = pTab->zName; + nColTest = nCol - 1; + }else{ + nCol = pIdx->nColumn; + zIdxName = pIdx->zName; + nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1; + } + + /* Populate the register containing the index name. */ + sqlite3VdbeLoadString(v, regIdxname, zIdxName); + VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName)); + + /* + ** Pseudo-code for loop that calls stat_push(): + ** + ** Rewind csr + ** if eof(csr) goto end_of_scan; + ** regChng = 0 + ** goto chng_addr_0; + ** + ** next_row: + ** regChng = 0 + ** if( idx(0) != regPrev(0) ) goto chng_addr_0 + ** regChng = 1 + ** if( idx(1) != regPrev(1) ) goto chng_addr_1 + ** ... + ** regChng = N + ** goto chng_addr_N + ** + ** chng_addr_0: + ** regPrev(0) = idx(0) + ** chng_addr_1: + ** regPrev(1) = idx(1) + ** ... + ** + ** endDistinctTest: + ** regRowid = idx(rowid) + ** stat_push(P, regChng, regRowid) + ** Next csr + ** if !eof(csr) goto next_row; + ** + ** end_of_scan: + */ + + /* Make sure there are enough memory cells allocated to accommodate + ** the regPrev array and a trailing rowid (the rowid slot is required + ** when building a record to insert into the sample column of + ** the sqlite_stat4 table. */ + pParse->nMem = MAX(pParse->nMem, regPrev+nColTest); + + /* Open a read-only cursor on the index being analyzed. */ + assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); + sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + VdbeComment((v, "%s", pIdx->zName)); + + /* Invoke the stat_init() function. The arguments are: + ** + ** (1) the number of columns in the index including the rowid + ** (or for a WITHOUT ROWID table, the number of PK columns), + ** (2) the number of columns in the key without the rowid/pk + ** (3) the number of rows in the index, + ** + ** + ** The third argument is only used for STAT3 and STAT4 + */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); +#endif + sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); + sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4, + (char*)&statInitFuncdef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, 2+IsStat34); + + /* Implementation of the following: + ** + ** Rewind csr + ** if eof(csr) goto end_of_scan; + ** regChng = 0 + ** goto next_push_0; + ** + */ + addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); + addrNextRow = sqlite3VdbeCurrentAddr(v); + + if( nColTest>0 ){ + int endDistinctTest = sqlite3VdbeMakeLabel(v); + int *aGotoChng; /* Array of jump instruction addresses */ + aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest); + if( aGotoChng==0 ) continue; + + /* + ** next_row: + ** regChng = 0 + ** if( idx(0) != regPrev(0) ) goto chng_addr_0 + ** regChng = 1 + ** if( idx(1) != regPrev(1) ) goto chng_addr_1 + ** ... + ** regChng = N + ** goto endDistinctTest + */ + sqlite3VdbeAddOp0(v, OP_Goto); + addrNextRow = sqlite3VdbeCurrentAddr(v); + if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){ + /* For a single-column UNIQUE index, once we have found a non-NULL + ** row, we know that all the rest will be distinct, so skip + ** subsequent distinctness tests. */ + sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest); + VdbeCoverage(v); + } + for(i=0; iazColl[i]); + sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); + aGotoChng[i] = + sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); + sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); + VdbeCoverage(v); + } + sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng); + sqlite3VdbeGoto(v, endDistinctTest); + + + /* + ** chng_addr_0: + ** regPrev(0) = idx(0) + ** chng_addr_1: + ** regPrev(1) = idx(1) + ** ... + */ + sqlite3VdbeJumpHere(v, addrNextRow-1); + for(i=0; ipTable); + int j, k, regKey; + regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); + for(j=0; jnKeyCol; j++){ + k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); + assert( k>=0 && knCol ); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); + VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName)); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); + sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); + } +#endif + assert( regChng==(regStat4+1) ); + sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp, + (char*)&statPushFuncdef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, 2+IsStat34); + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); + + /* Add the entry to the stat1 table. */ + callStatGet(v, regStat4, STAT_GET_STAT1, regStat1); + assert( "BBB"[0]==SQLITE_AFF_TEXT ); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + + /* Add the entries to the stat3 or stat4 table. */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + { + int regEq = regStat1; + int regLt = regStat1+1; + int regDLt = regStat1+2; + int regSample = regStat1+3; + int regCol = regStat1+4; + int regSampleRowid = regCol + nCol; + int addrNext; + int addrIsNull; + u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; + + pParse->nMem = MAX(pParse->nMem, regCol+nCol); + + addrNext = sqlite3VdbeCurrentAddr(v); + callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid); + addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); + VdbeCoverage(v); + callStatGet(v, regStat4, STAT_GET_NEQ, regEq); + callStatGet(v, regStat4, STAT_GET_NLT, regLt); + callStatGet(v, regStat4, STAT_GET_NDLT, regDLt); + sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); + /* We know that the regSampleRowid row exists because it was read by + ** the previous loop. Thus the not-found jump of seekOp will never + ** be taken */ + VdbeCoverageNeverTaken(v); +#ifdef SQLITE_ENABLE_STAT3 + sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample); +#else + for(i=0; izName)); + sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1); + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); + assert( "BBB"[0]==SQLITE_AFF_TEXT ); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeJumpHere(v, jZeroRows); + } +} + + +/* +** Generate code that will cause the most recent index analysis to +** be loaded into internal hash tables where is can be used. +*/ +static void loadAnalysis(Parse *pParse, int iDb){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); + } +} + +/* +** Generate code that will do an analysis of an entire database +*/ +static void analyzeDatabase(Parse *pParse, int iDb){ + sqlite3 *db = pParse->db; + Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ + HashElem *k; + int iStatCur; + int iMem; + int iTab; + + sqlite3BeginWriteOperation(pParse, 0, iDb); + iStatCur = pParse->nTab; + pParse->nTab += 3; + openStatTable(pParse, iDb, iStatCur, 0, 0); + iMem = pParse->nMem+1; + iTab = pParse->nTab; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ + Table *pTab = (Table*)sqliteHashData(k); + analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab); + } + loadAnalysis(pParse, iDb); +} + +/* +** Generate code that will do an analysis of a single table in +** a database. If pOnlyIdx is not NULL then it is a single index +** in pTab that should be analyzed. +*/ +static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){ + int iDb; + int iStatCur; + + assert( pTab!=0 ); + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3BeginWriteOperation(pParse, 0, iDb); + iStatCur = pParse->nTab; + pParse->nTab += 3; + if( pOnlyIdx ){ + openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); + }else{ + openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); + } + analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab); + loadAnalysis(pParse, iDb); +} + +/* +** Generate code for the ANALYZE command. The parser calls this routine +** when it recognizes an ANALYZE command. +** +** ANALYZE -- 1 +** ANALYZE -- 2 +** ANALYZE ?.? -- 3 +** +** Form 1 causes all indices in all attached databases to be analyzed. +** Form 2 analyzes all indices the single database named. +** Form 3 analyzes all indices associated with the named table. +*/ +SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ + sqlite3 *db = pParse->db; + int iDb; + int i; + char *z, *zDb; + Table *pTab; + Index *pIdx; + Token *pTableName; + Vdbe *v; + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return; + } + + assert( pName2!=0 || pName1==0 ); + if( pName1==0 ){ + /* Form 1: Analyze everything */ + for(i=0; inDb; i++){ + if( i==1 ) continue; /* Do not analyze the TEMP database */ + analyzeDatabase(pParse, i); + } + }else if( pName2->n==0 ){ + /* Form 2: Analyze the database or table named */ + iDb = sqlite3FindDb(db, pName1); + if( iDb>=0 ){ + analyzeDatabase(pParse, iDb); + }else{ + z = sqlite3NameFromToken(db, pName1); + if( z ){ + if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ + analyzeTable(pParse, pIdx->pTable, pIdx); + }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ + analyzeTable(pParse, pTab, 0); + } + sqlite3DbFree(db, z); + } + } + }else{ + /* Form 3: Analyze the fully qualified table name */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); + if( iDb>=0 ){ + zDb = db->aDb[iDb].zName; + z = sqlite3NameFromToken(db, pTableName); + if( z ){ + if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ + analyzeTable(pParse, pIdx->pTable, pIdx); + }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){ + analyzeTable(pParse, pTab, 0); + } + sqlite3DbFree(db, z); + } + } + } + v = sqlite3GetVdbe(pParse); + if( v ) sqlite3VdbeAddOp0(v, OP_Expire); +} + +/* +** Used to pass information from the analyzer reader through to the +** callback routine. +*/ +typedef struct analysisInfo analysisInfo; +struct analysisInfo { + sqlite3 *db; + const char *zDatabase; +}; + +/* +** The first argument points to a nul-terminated string containing a +** list of space separated integers. Read the first nOut of these into +** the array aOut[]. +*/ +static void decodeIntArray( + char *zIntArray, /* String containing int array to decode */ + int nOut, /* Number of slots in aOut[] */ + tRowcnt *aOut, /* Store integers here */ + LogEst *aLog, /* Or, if aOut==0, here */ + Index *pIndex /* Handle extra flags for this index, if not NULL */ +){ + char *z = zIntArray; + int c; + int i; + tRowcnt v; + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( z==0 ) z = ""; +#else + assert( z!=0 ); +#endif + for(i=0; *z && i='0' && c<='9' ){ + v = v*10 + c - '0'; + z++; + } +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( aOut ) aOut[i] = v; + if( aLog ) aLog[i] = sqlite3LogEst(v); +#else + assert( aOut==0 ); + UNUSED_PARAMETER(aOut); + assert( aLog!=0 ); + aLog[i] = sqlite3LogEst(v); +#endif + if( *z==' ' ) z++; + } +#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 + assert( pIndex!=0 ); { +#else + if( pIndex ){ +#endif + pIndex->bUnordered = 0; + pIndex->noSkipScan = 0; + while( z[0] ){ + if( sqlite3_strglob("unordered*", z)==0 ){ + pIndex->bUnordered = 1; + }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ + pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); + }else if( sqlite3_strglob("noskipscan*", z)==0 ){ + pIndex->noSkipScan = 1; + } +#ifdef SQLITE_ENABLE_COSTMULT + else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ + pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); + } +#endif + while( z[0]!=0 && z[0]!=' ' ) z++; + while( z[0]==' ' ) z++; + } + } +} + +/* +** This callback is invoked once for each index when reading the +** sqlite_stat1 table. +** +** argv[0] = name of the table +** argv[1] = name of the index (might be NULL) +** argv[2] = results of analysis - on integer for each column +** +** Entries for which argv[1]==NULL simply record the number of rows in +** the table. +*/ +static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ + analysisInfo *pInfo = (analysisInfo*)pData; + Index *pIndex; + Table *pTable; + const char *z; + + assert( argc==3 ); + UNUSED_PARAMETER2(NotUsed, argc); + + if( argv==0 || argv[0]==0 || argv[2]==0 ){ + return 0; + } + pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); + if( pTable==0 ){ + return 0; + } + if( argv[1]==0 ){ + pIndex = 0; + }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){ + pIndex = sqlite3PrimaryKeyIndex(pTable); + }else{ + pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); + } + z = argv[2]; + + if( pIndex ){ + tRowcnt *aiRowEst = 0; + int nCol = pIndex->nKeyCol+1; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + /* Index.aiRowEst may already be set here if there are duplicate + ** sqlite_stat1 entries for this index. In that case just clobber + ** the old data with the new instead of allocating a new array. */ + if( pIndex->aiRowEst==0 ){ + pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol); + if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db); + } + aiRowEst = pIndex->aiRowEst; +#endif + pIndex->bUnordered = 0; + decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); + if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; + }else{ + Index fakeIdx; + fakeIdx.szIdxRow = pTable->szTabRow; +#ifdef SQLITE_ENABLE_COSTMULT + fakeIdx.pTable = pTable; +#endif + decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx); + pTable->szTabRow = fakeIdx.szIdxRow; + } + + return 0; +} + +/* +** If the Index.aSample variable is not NULL, delete the aSample[] array +** and its contents. +*/ +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( pIdx->aSample ){ + int j; + for(j=0; jnSample; j++){ + IndexSample *p = &pIdx->aSample[j]; + sqlite3DbFree(db, p->p); + } + sqlite3DbFree(db, pIdx->aSample); + } + if( db && db->pnBytesFreed==0 ){ + pIdx->nSample = 0; + pIdx->aSample = 0; + } +#else + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(pIdx); +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +} + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Populate the pIdx->aAvgEq[] array based on the samples currently +** stored in pIdx->aSample[]. +*/ +static void initAvgEq(Index *pIdx){ + if( pIdx ){ + IndexSample *aSample = pIdx->aSample; + IndexSample *pFinal = &aSample[pIdx->nSample-1]; + int iCol; + int nCol = 1; + if( pIdx->nSampleCol>1 ){ + /* If this is stat4 data, then calculate aAvgEq[] values for all + ** sample columns except the last. The last is always set to 1, as + ** once the trailing PK fields are considered all index keys are + ** unique. */ + nCol = pIdx->nSampleCol-1; + pIdx->aAvgEq[nCol] = 1; + } + for(iCol=0; iColnSample; + int i; /* Used to iterate through samples */ + tRowcnt sumEq = 0; /* Sum of the nEq values */ + tRowcnt avgEq = 0; + tRowcnt nRow; /* Number of rows in index */ + i64 nSum100 = 0; /* Number of terms contributing to sumEq */ + i64 nDist100; /* Number of distinct values in index */ + + if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){ + nRow = pFinal->anLt[iCol]; + nDist100 = (i64)100 * pFinal->anDLt[iCol]; + nSample--; + }else{ + nRow = pIdx->aiRowEst[0]; + nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; + } + pIdx->nRowEst0 = nRow; + + /* Set nSum to the number of distinct (iCol+1) field prefixes that + ** occur in the stat4 table for this index. Set sumEq to the sum of + ** the nEq values for column iCol for the same set (adding the value + ** only once where there exist duplicate prefixes). */ + for(i=0; inSample-1) + || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] + ){ + sumEq += aSample[i].anEq[iCol]; + nSum100 += 100; + } + } + + if( nDist100>nSum100 ){ + avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); + } + if( avgEq==0 ) avgEq = 1; + pIdx->aAvgEq[iCol] = avgEq; + } + } +} + +/* +** Look up an index by name. Or, if the name of a WITHOUT ROWID table +** is supplied instead, find the PRIMARY KEY index for that table. +*/ +static Index *findIndexOrPrimaryKey( + sqlite3 *db, + const char *zName, + const char *zDb +){ + Index *pIdx = sqlite3FindIndex(db, zName, zDb); + if( pIdx==0 ){ + Table *pTab = sqlite3FindTable(db, zName, zDb); + if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab); + } + return pIdx; +} + +/* +** Load the content from either the sqlite_stat4 or sqlite_stat3 table +** into the relevant Index.aSample[] arrays. +** +** Arguments zSql1 and zSql2 must point to SQL statements that return +** data equivalent to the following (statements are different for stat3, +** see the caller of this function for details): +** +** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx +** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4 +** +** where %Q is replaced with the database name before the SQL is executed. +*/ +static int loadStatTbl( + sqlite3 *db, /* Database handle */ + int bStat3, /* Assume single column records only */ + const char *zSql1, /* SQL statement 1 (see above) */ + const char *zSql2, /* SQL statement 2 (see above) */ + const char *zDb /* Database name (e.g. "main") */ +){ + int rc; /* Result codes from subroutines */ + sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ + char *zSql; /* Text of the SQL statement */ + Index *pPrevIdx = 0; /* Previous index in the loop */ + IndexSample *pSample; /* A slot in pIdx->aSample[] */ + + assert( db->lookaside.bDisable ); + zSql = sqlite3MPrintf(db, zSql1, zDb); + if( !zSql ){ + return SQLITE_NOMEM_BKPT; + } + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + sqlite3DbFree(db, zSql); + if( rc ) return rc; + + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + int nIdxCol = 1; /* Number of columns in stat4 records */ + + char *zIndex; /* Index name */ + Index *pIdx; /* Pointer to the index object */ + int nSample; /* Number of samples */ + int nByte; /* Bytes of space required */ + int i; /* Bytes of space required */ + tRowcnt *pSpace; + + zIndex = (char *)sqlite3_column_text(pStmt, 0); + if( zIndex==0 ) continue; + nSample = sqlite3_column_int(pStmt, 1); + pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); + assert( pIdx==0 || bStat3 || pIdx->nSample==0 ); + /* Index.nSample is non-zero at this point if data has already been + ** loaded from the stat4 table. In this case ignore stat3 data. */ + if( pIdx==0 || pIdx->nSample ) continue; + if( bStat3==0 ){ + assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 ); + if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ + nIdxCol = pIdx->nKeyCol; + }else{ + nIdxCol = pIdx->nColumn; + } + } + pIdx->nSampleCol = nIdxCol; + nByte = sizeof(IndexSample) * nSample; + nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample; + nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */ + + pIdx->aSample = sqlite3DbMallocZero(db, nByte); + if( pIdx->aSample==0 ){ + sqlite3_finalize(pStmt); + return SQLITE_NOMEM_BKPT; + } + pSpace = (tRowcnt*)&pIdx->aSample[nSample]; + pIdx->aAvgEq = pSpace; pSpace += nIdxCol; + for(i=0; iaSample[i].anEq = pSpace; pSpace += nIdxCol; + pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol; + pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol; + } + assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) ); + } + rc = sqlite3_finalize(pStmt); + if( rc ) return rc; + + zSql = sqlite3MPrintf(db, zSql2, zDb); + if( !zSql ){ + return SQLITE_NOMEM_BKPT; + } + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + sqlite3DbFree(db, zSql); + if( rc ) return rc; + + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + char *zIndex; /* Index name */ + Index *pIdx; /* Pointer to the index object */ + int nCol = 1; /* Number of columns in index */ + + zIndex = (char *)sqlite3_column_text(pStmt, 0); + if( zIndex==0 ) continue; + pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); + if( pIdx==0 ) continue; + /* This next condition is true if data has already been loaded from + ** the sqlite_stat4 table. In this case ignore stat3 data. */ + nCol = pIdx->nSampleCol; + if( bStat3 && nCol>1 ) continue; + if( pIdx!=pPrevIdx ){ + initAvgEq(pPrevIdx); + pPrevIdx = pIdx; + } + pSample = &pIdx->aSample[pIdx->nSample]; + decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0); + decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0); + decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0); + + /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer. + ** This is in case the sample record is corrupted. In that case, the + ** sqlite3VdbeRecordCompare() may read up to two varints past the + ** end of the allocated buffer before it realizes it is dealing with + ** a corrupt record. Adding the two 0x00 bytes prevents this from causing + ** a buffer overread. */ + pSample->n = sqlite3_column_bytes(pStmt, 4); + pSample->p = sqlite3DbMallocZero(db, pSample->n + 2); + if( pSample->p==0 ){ + sqlite3_finalize(pStmt); + return SQLITE_NOMEM_BKPT; + } + memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n); + pIdx->nSample++; + } + rc = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ) initAvgEq(pPrevIdx); + return rc; +} + +/* +** Load content from the sqlite_stat4 and sqlite_stat3 tables into +** the Index.aSample[] arrays of all indices. +*/ +static int loadStat4(sqlite3 *db, const char *zDb){ + int rc = SQLITE_OK; /* Result codes from subroutines */ + + assert( db->lookaside.bDisable ); + if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){ + rc = loadStatTbl(db, 0, + "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", + "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4", + zDb + ); + } + + if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){ + rc = loadStatTbl(db, 1, + "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", + "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3", + zDb + ); + } + + return rc; +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +/* +** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The +** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] +** arrays. The contents of sqlite_stat3/4 are used to populate the +** Index.aSample[] arrays. +** +** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR +** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined +** during compilation and the sqlite_stat3/4 table is present, no data is +** read from it. +** +** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the +** sqlite_stat4 table is not present in the database, SQLITE_ERROR is +** returned. However, in this case, data is read from the sqlite_stat1 +** table (if it is present) before returning. +** +** If an OOM error occurs, this function always sets db->mallocFailed. +** This means if the caller does not care about other errors, the return +** code may be ignored. +*/ +SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ + analysisInfo sInfo; + HashElem *i; + char *zSql; + int rc = SQLITE_OK; + + assert( iDb>=0 && iDbnDb ); + assert( db->aDb[iDb].pBt!=0 ); + + /* Clear any prior statistics */ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + Index *pIdx = sqliteHashData(i); + pIdx->aiRowLogEst[0] = 0; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3DeleteIndexSamples(db, pIdx); + pIdx->aSample = 0; +#endif + } + + /* Load new statistics out of the sqlite_stat1 table */ + sInfo.db = db; + sInfo.zDatabase = db->aDb[iDb].zName; + if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){ + zSql = sqlite3MPrintf(db, + "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); + if( zSql==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); + sqlite3DbFree(db, zSql); + } + } + + /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + Index *pIdx = sqliteHashData(i); + if( pIdx->aiRowLogEst[0]==0 ) sqlite3DefaultRowEst(pIdx); + } + + /* Load the statistics from the sqlite_stat4 table. */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ + db->lookaside.bDisable++; + rc = loadStat4(db, sInfo.zDatabase); + db->lookaside.bDisable--; + } + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + Index *pIdx = sqliteHashData(i); + sqlite3_free(pIdx->aiRowEst); + pIdx->aiRowEst = 0; + } +#endif + + if( rc==SQLITE_NOMEM ){ + sqlite3OomFault(db); + } + return rc; +} + + +#endif /* SQLITE_OMIT_ANALYZE */ + +/************** End of analyze.c *********************************************/ +/************** Begin file attach.c ******************************************/ +/* +** 2003 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the ATTACH and DETACH commands. +*/ +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_OMIT_ATTACH +/* +** Resolve an expression that was part of an ATTACH or DETACH statement. This +** is slightly different from resolving a normal SQL expression, because simple +** identifiers are treated as strings, not possible column names or aliases. +** +** i.e. if the parser sees: +** +** ATTACH DATABASE abc AS def +** +** it treats the two expressions as literal strings 'abc' and 'def' instead of +** looking for columns of the same name. +** +** This only applies to the root node of pExpr, so the statement: +** +** ATTACH DATABASE abc||def AS 'db2' +** +** will fail because neither abc or def can be resolved. +*/ +static int resolveAttachExpr(NameContext *pName, Expr *pExpr) +{ + int rc = SQLITE_OK; + if( pExpr ){ + if( pExpr->op!=TK_ID ){ + rc = sqlite3ResolveExprNames(pName, pExpr); + }else{ + pExpr->op = TK_STRING; + } + } + return rc; +} + +/* +** An SQL user-function registered to do the work of an ATTACH statement. The +** three arguments to the function come directly from an attach statement: +** +** ATTACH DATABASE x AS y KEY z +** +** SELECT sqlite_attach(x, y, z) +** +** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the +** third argument. +*/ +static void attachFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + int i; + int rc = 0; + sqlite3 *db = sqlite3_context_db_handle(context); + const char *zName; + const char *zFile; + char *zPath = 0; + char *zErr = 0; + unsigned int flags; + Db *aNew; + char *zErrDyn = 0; + sqlite3_vfs *pVfs; + + UNUSED_PARAMETER(NotUsed); + + zFile = (const char *)sqlite3_value_text(argv[0]); + zName = (const char *)sqlite3_value_text(argv[1]); + if( zFile==0 ) zFile = ""; + if( zName==0 ) zName = ""; + + /* Check for the following errors: + ** + ** * Too many attached databases, + ** * Transaction currently open + ** * Specified database name already being used. + */ + if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ + zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", + db->aLimit[SQLITE_LIMIT_ATTACHED] + ); + goto attach_error; + } + if( !db->autoCommit ){ + zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction"); + goto attach_error; + } + for(i=0; inDb; i++){ + char *z = db->aDb[i].zName; + assert( z && zName ); + if( sqlite3StrICmp(z, zName)==0 ){ + zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); + goto attach_error; + } + } + + /* Allocate the new entry in the db->aDb[] array and initialize the schema + ** hash tables. + */ + if( db->aDb==db->aDbStatic ){ + aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 ); + if( aNew==0 ) return; + memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); + }else{ + aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); + if( aNew==0 ) return; + } + db->aDb = aNew; + aNew = &db->aDb[db->nDb]; + memset(aNew, 0, sizeof(*aNew)); + + /* Open the database file. If the btree is successfully opened, use + ** it to obtain the database schema. At this point the schema may + ** or may not be initialized. + */ + flags = db->openFlags; + rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + assert( pVfs ); + flags |= SQLITE_OPEN_MAIN_DB; + rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags); + sqlite3_free( zPath ); + db->nDb++; + if( rc==SQLITE_CONSTRAINT ){ + rc = SQLITE_ERROR; + zErrDyn = sqlite3MPrintf(db, "database is already attached"); + }else if( rc==SQLITE_OK ){ + Pager *pPager; + aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); + if( !aNew->pSchema ){ + rc = SQLITE_NOMEM_BKPT; + }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ + zErrDyn = sqlite3MPrintf(db, + "attached databases must use the same text encoding as main database"); + rc = SQLITE_ERROR; + } + sqlite3BtreeEnter(aNew->pBt); + pPager = sqlite3BtreePager(aNew->pBt); + sqlite3PagerLockingMode(pPager, db->dfltLockMode); + sqlite3BtreeSecureDelete(aNew->pBt, + sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) ); +#ifndef SQLITE_OMIT_PAGER_PRAGMAS + sqlite3BtreeSetPagerFlags(aNew->pBt, + PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK)); +#endif + sqlite3BtreeLeave(aNew->pBt); + } + aNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; + aNew->zName = sqlite3DbStrDup(db, zName); + if( rc==SQLITE_OK && aNew->zName==0 ){ + rc = SQLITE_NOMEM_BKPT; + } + + +#ifdef SQLITE_HAS_CODEC + if( rc==SQLITE_OK ){ + extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); + extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + int nKey; + char *zKey; + int t = sqlite3_value_type(argv[2]); + switch( t ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: + zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); + rc = SQLITE_ERROR; + break; + + case SQLITE_TEXT: + case SQLITE_BLOB: + nKey = sqlite3_value_bytes(argv[2]); + zKey = (char *)sqlite3_value_blob(argv[2]); + rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + break; + + case SQLITE_NULL: + /* No key specified. Use the key from the main database */ + sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); + if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){ + rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + } + break; + } + } +#endif + + /* If the file was opened successfully, read the schema for the new database. + ** If this fails, or if opening the file failed, then close the file and + ** remove the entry from the db->aDb[] array. i.e. put everything back the way + ** we found it. + */ + if( rc==SQLITE_OK ){ + sqlite3BtreeEnterAll(db); + rc = sqlite3Init(db, &zErrDyn); + sqlite3BtreeLeaveAll(db); + } +#ifdef SQLITE_USER_AUTHENTICATION + if( rc==SQLITE_OK ){ + u8 newAuth = 0; + rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth); + if( newAuthauth.authLevel ){ + rc = SQLITE_AUTH_USER; + } + } +#endif + if( rc ){ + int iDb = db->nDb - 1; + assert( iDb>=2 ); + if( db->aDb[iDb].pBt ){ + sqlite3BtreeClose(db->aDb[iDb].pBt); + db->aDb[iDb].pBt = 0; + db->aDb[iDb].pSchema = 0; + } + sqlite3ResetAllSchemasOfConnection(db); + db->nDb = iDb; + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomFault(db); + sqlite3DbFree(db, zErrDyn); + zErrDyn = sqlite3MPrintf(db, "out of memory"); + }else if( zErrDyn==0 ){ + zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); + } + goto attach_error; + } + + return; + +attach_error: + /* Return an error if we get here */ + if( zErrDyn ){ + sqlite3_result_error(context, zErrDyn, -1); + sqlite3DbFree(db, zErrDyn); + } + if( rc ) sqlite3_result_error_code(context, rc); +} + +/* +** An SQL user-function registered to do the work of an DETACH statement. The +** three arguments to the function come directly from a detach statement: +** +** DETACH DATABASE x +** +** SELECT sqlite_detach(x) +*/ +static void detachFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + const char *zName = (const char *)sqlite3_value_text(argv[0]); + sqlite3 *db = sqlite3_context_db_handle(context); + int i; + Db *pDb = 0; + char zErr[128]; + + UNUSED_PARAMETER(NotUsed); + + if( zName==0 ) zName = ""; + for(i=0; inDb; i++){ + pDb = &db->aDb[i]; + if( pDb->pBt==0 ) continue; + if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; + } + + if( i>=db->nDb ){ + sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); + goto detach_error; + } + if( i<2 ){ + sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); + goto detach_error; + } + if( !db->autoCommit ){ + sqlite3_snprintf(sizeof(zErr), zErr, + "cannot DETACH database within transaction"); + goto detach_error; + } + if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){ + sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); + goto detach_error; + } + + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + pDb->pSchema = 0; + sqlite3CollapseDatabaseArray(db); + return; + +detach_error: + sqlite3_result_error(context, zErr, -1); +} + +/* +** This procedure generates VDBE code for a single invocation of either the +** sqlite_detach() or sqlite_attach() SQL user functions. +*/ +static void codeAttach( + Parse *pParse, /* The parser context */ + int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ + FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */ + Expr *pAuthArg, /* Expression to pass to authorization callback */ + Expr *pFilename, /* Name of database file */ + Expr *pDbname, /* Name of the database to use internally */ + Expr *pKey /* Database key for encryption extension */ +){ + int rc; + NameContext sName; + Vdbe *v; + sqlite3* db = pParse->db; + int regArgs; + + memset(&sName, 0, sizeof(NameContext)); + sName.pParse = pParse; + + if( + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) + ){ + goto attach_end; + } + +#ifndef SQLITE_OMIT_AUTHORIZATION + if( pAuthArg ){ + char *zAuthArg; + if( pAuthArg->op==TK_STRING ){ + zAuthArg = pAuthArg->u.zToken; + }else{ + zAuthArg = 0; + } + rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); + if(rc!=SQLITE_OK ){ + goto attach_end; + } + } +#endif /* SQLITE_OMIT_AUTHORIZATION */ + + + v = sqlite3GetVdbe(pParse); + regArgs = sqlite3GetTempRange(pParse, 4); + sqlite3ExprCode(pParse, pFilename, regArgs); + sqlite3ExprCode(pParse, pDbname, regArgs+1); + sqlite3ExprCode(pParse, pKey, regArgs+2); + + assert( v || db->mallocFailed ); + if( v ){ + sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3, + (char *)pFunc, P4_FUNCDEF); + assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg ); + sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg)); + + /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this + ** statement only). For DETACH, set it to false (expire all existing + ** statements). + */ + sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); + } + +attach_end: + sqlite3ExprDelete(db, pFilename); + sqlite3ExprDelete(db, pDbname); + sqlite3ExprDelete(db, pKey); +} + +/* +** Called by the parser to compile a DETACH statement. +** +** DETACH pDbname +*/ +SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){ + static const FuncDef detach_func = { + 1, /* nArg */ + SQLITE_UTF8, /* funcFlags */ + 0, /* pUserData */ + 0, /* pNext */ + detachFunc, /* xSFunc */ + 0, /* xFinalize */ + "sqlite_detach", /* zName */ + {0} + }; + codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); +} + +/* +** Called by the parser to compile an ATTACH statement. +** +** ATTACH p AS pDbname KEY pKey +*/ +SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ + static const FuncDef attach_func = { + 3, /* nArg */ + SQLITE_UTF8, /* funcFlags */ + 0, /* pUserData */ + 0, /* pNext */ + attachFunc, /* xSFunc */ + 0, /* xFinalize */ + "sqlite_attach", /* zName */ + {0} + }; + codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); +} +#endif /* SQLITE_OMIT_ATTACH */ + +/* +** Initialize a DbFixer structure. This routine must be called prior +** to passing the structure to one of the sqliteFixAAAA() routines below. +*/ +SQLITE_PRIVATE void sqlite3FixInit( + DbFixer *pFix, /* The fixer to be initialized */ + Parse *pParse, /* Error messages will be written here */ + int iDb, /* This is the database that must be used */ + const char *zType, /* "view", "trigger", or "index" */ + const Token *pName /* Name of the view, trigger, or index */ +){ + sqlite3 *db; + + db = pParse->db; + assert( db->nDb>iDb ); + pFix->pParse = pParse; + pFix->zDb = db->aDb[iDb].zName; + pFix->pSchema = db->aDb[iDb].pSchema; + pFix->zType = zType; + pFix->pName = pName; + pFix->bVarOnly = (iDb==1); +} + +/* +** The following set of routines walk through the parse tree and assign +** a specific database to all table references where the database name +** was left unspecified in the original SQL statement. The pFix structure +** must have been initialized by a prior call to sqlite3FixInit(). +** +** These routines are used to make sure that an index, trigger, or +** view in one database does not refer to objects in a different database. +** (Exception: indices, triggers, and views in the TEMP database are +** allowed to refer to anything.) If a reference is explicitly made +** to an object in a different database, an error message is added to +** pParse->zErrMsg and these routines return non-zero. If everything +** checks out, these routines return 0. +*/ +SQLITE_PRIVATE int sqlite3FixSrcList( + DbFixer *pFix, /* Context of the fixation */ + SrcList *pList /* The Source list to check and modify */ +){ + int i; + const char *zDb; + struct SrcList_item *pItem; + + if( NEVER(pList==0) ) return 0; + zDb = pFix->zDb; + for(i=0, pItem=pList->a; inSrc; i++, pItem++){ + if( pFix->bVarOnly==0 ){ + if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){ + sqlite3ErrorMsg(pFix->pParse, + "%s %T cannot reference objects in database %s", + pFix->zType, pFix->pName, pItem->zDatabase); + return 1; + } + sqlite3DbFree(pFix->pParse->db, pItem->zDatabase); + pItem->zDatabase = 0; + pItem->pSchema = pFix->pSchema; + } +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) + if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; + if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; +#endif + } + return 0; +} +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) +SQLITE_PRIVATE int sqlite3FixSelect( + DbFixer *pFix, /* Context of the fixation */ + Select *pSelect /* The SELECT statement to be fixed to one database */ +){ + while( pSelect ){ + if( sqlite3FixExprList(pFix, pSelect->pEList) ){ + return 1; + } + if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ + return 1; + } + if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ + return 1; + } + if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pLimit) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pOffset) ){ + return 1; + } + pSelect = pSelect->pPrior; + } + return 0; +} +SQLITE_PRIVATE int sqlite3FixExpr( + DbFixer *pFix, /* Context of the fixation */ + Expr *pExpr /* The expression to be fixed to one database */ +){ + while( pExpr ){ + if( pExpr->op==TK_VARIABLE ){ + if( pFix->pParse->db->init.busy ){ + pExpr->op = TK_NULL; + }else{ + sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType); + return 1; + } + } + if( ExprHasProperty(pExpr, EP_TokenOnly) ) break; + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1; + }else{ + if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1; + } + if( sqlite3FixExpr(pFix, pExpr->pRight) ){ + return 1; + } + pExpr = pExpr->pLeft; + } + return 0; +} +SQLITE_PRIVATE int sqlite3FixExprList( + DbFixer *pFix, /* Context of the fixation */ + ExprList *pList /* The expression to be fixed to one database */ +){ + int i; + struct ExprList_item *pItem; + if( pList==0 ) return 0; + for(i=0, pItem=pList->a; inExpr; i++, pItem++){ + if( sqlite3FixExpr(pFix, pItem->pExpr) ){ + return 1; + } + } + return 0; +} +#endif + +#ifndef SQLITE_OMIT_TRIGGER +SQLITE_PRIVATE int sqlite3FixTriggerStep( + DbFixer *pFix, /* Context of the fixation */ + TriggerStep *pStep /* The trigger step be fixed to one database */ +){ + while( pStep ){ + if( sqlite3FixSelect(pFix, pStep->pSelect) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pStep->pWhere) ){ + return 1; + } + if( sqlite3FixExprList(pFix, pStep->pExprList) ){ + return 1; + } + pStep = pStep->pNext; + } + return 0; +} +#endif + +/************** End of attach.c **********************************************/ +/************** Begin file auth.c ********************************************/ +/* +** 2003 January 11 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the sqlite3_set_authorizer() +** API. This facility is an optional feature of the library. Embedded +** systems that do not need this facility may omit it by recompiling +** the library with -DSQLITE_OMIT_AUTHORIZATION=1 +*/ +/* #include "sqliteInt.h" */ + +/* +** All of the code in this file may be omitted by defining a single +** macro. +*/ +#ifndef SQLITE_OMIT_AUTHORIZATION + +/* +** Set or clear the access authorization function. +** +** The access authorization function is be called during the compilation +** phase to verify that the user has read and/or write access permission on +** various fields of the database. The first argument to the auth function +** is a copy of the 3rd argument to this routine. The second argument +** to the auth function is one of these constants: +** +** SQLITE_CREATE_INDEX +** SQLITE_CREATE_TABLE +** SQLITE_CREATE_TEMP_INDEX +** SQLITE_CREATE_TEMP_TABLE +** SQLITE_CREATE_TEMP_TRIGGER +** SQLITE_CREATE_TEMP_VIEW +** SQLITE_CREATE_TRIGGER +** SQLITE_CREATE_VIEW +** SQLITE_DELETE +** SQLITE_DROP_INDEX +** SQLITE_DROP_TABLE +** SQLITE_DROP_TEMP_INDEX +** SQLITE_DROP_TEMP_TABLE +** SQLITE_DROP_TEMP_TRIGGER +** SQLITE_DROP_TEMP_VIEW +** SQLITE_DROP_TRIGGER +** SQLITE_DROP_VIEW +** SQLITE_INSERT +** SQLITE_PRAGMA +** SQLITE_READ +** SQLITE_SELECT +** SQLITE_TRANSACTION +** SQLITE_UPDATE +** +** The third and fourth arguments to the auth function are the name of +** the table and the column that are being accessed. The auth function +** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If +** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY +** means that the SQL statement will never-run - the sqlite3_exec() call +** will return with an error. SQLITE_IGNORE means that the SQL statement +** should run but attempts to read the specified column will return NULL +** and attempts to write the column will be ignored. +** +** Setting the auth function to NULL disables this hook. The default +** setting of the auth function is NULL. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer( + sqlite3 *db, + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), + void *pArg +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + db->xAuth = (sqlite3_xauth)xAuth; + db->pAuthArg = pArg; + sqlite3ExpirePreparedStatements(db); + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +/* +** Write an error message into pParse->zErrMsg that explains that the +** user-supplied authorization function returned an illegal value. +*/ +static void sqliteAuthBadReturnCode(Parse *pParse){ + sqlite3ErrorMsg(pParse, "authorizer malfunction"); + pParse->rc = SQLITE_ERROR; +} + +/* +** Invoke the authorization callback for permission to read column zCol from +** table zTab in database zDb. This function assumes that an authorization +** callback has been registered (i.e. that sqlite3.xAuth is not NULL). +** +** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed +** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE +** is treated as SQLITE_DENY. In this case an error is left in pParse. +*/ +SQLITE_PRIVATE int sqlite3AuthReadCol( + Parse *pParse, /* The parser context */ + const char *zTab, /* Table name */ + const char *zCol, /* Column name */ + int iDb /* Index of containing database. */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + char *zDb = db->aDb[iDb].zName; /* Name of attached database */ + int rc; /* Auth callback return code */ + + rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext +#ifdef SQLITE_USER_AUTHENTICATION + ,db->auth.zAuthUser +#endif + ); + if( rc==SQLITE_DENY ){ + if( db->nDb>2 || iDb!=0 ){ + sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol); + }else{ + sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol); + } + pParse->rc = SQLITE_AUTH; + }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){ + sqliteAuthBadReturnCode(pParse); + } + return rc; +} + +/* +** The pExpr should be a TK_COLUMN expression. The table referred to +** is in pTabList or else it is the NEW or OLD table of a trigger. +** Check to see if it is OK to read this particular column. +** +** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN +** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, +** then generate an error. +*/ +SQLITE_PRIVATE void sqlite3AuthRead( + Parse *pParse, /* The parser context */ + Expr *pExpr, /* The expression to check authorization on */ + Schema *pSchema, /* The schema of the expression */ + SrcList *pTabList /* All table that pExpr might refer to */ +){ + sqlite3 *db = pParse->db; + Table *pTab = 0; /* The table being read */ + const char *zCol; /* Name of the column of the table */ + int iSrc; /* Index in pTabList->a[] of table being read */ + int iDb; /* The index of the database the expression refers to */ + int iCol; /* Index of column in table */ + + if( db->xAuth==0 ) return; + iDb = sqlite3SchemaToIndex(pParse->db, pSchema); + if( iDb<0 ){ + /* An attempt to read a column out of a subquery or other + ** temporary table. */ + return; + } + + assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); + if( pExpr->op==TK_TRIGGER ){ + pTab = pParse->pTriggerTab; + }else{ + assert( pTabList ); + for(iSrc=0; ALWAYS(iSrcnSrc); iSrc++){ + if( pExpr->iTable==pTabList->a[iSrc].iCursor ){ + pTab = pTabList->a[iSrc].pTab; + break; + } + } + } + iCol = pExpr->iColumn; + if( NEVER(pTab==0) ) return; + + if( iCol>=0 ){ + assert( iColnCol ); + zCol = pTab->aCol[iCol].zName; + }else if( pTab->iPKey>=0 ){ + assert( pTab->iPKeynCol ); + zCol = pTab->aCol[pTab->iPKey].zName; + }else{ + zCol = "ROWID"; + } + assert( iDb>=0 && iDbnDb ); + if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){ + pExpr->op = TK_NULL; + } +} + +/* +** Do an authorization check using the code and arguments given. Return +** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY +** is returned, then the error count and error message in pParse are +** modified appropriately. +*/ +SQLITE_PRIVATE int sqlite3AuthCheck( + Parse *pParse, + int code, + const char *zArg1, + const char *zArg2, + const char *zArg3 +){ + sqlite3 *db = pParse->db; + int rc; + + /* Don't do any authorization checks if the database is initialising + ** or if the parser is being invoked from within sqlite3_declare_vtab. + */ + if( db->init.busy || IN_DECLARE_VTAB ){ + return SQLITE_OK; + } + + if( db->xAuth==0 ){ + return SQLITE_OK; + } + rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext +#ifdef SQLITE_USER_AUTHENTICATION + ,db->auth.zAuthUser +#endif + ); + if( rc==SQLITE_DENY ){ + sqlite3ErrorMsg(pParse, "not authorized"); + pParse->rc = SQLITE_AUTH; + }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ + rc = SQLITE_DENY; + sqliteAuthBadReturnCode(pParse); + } + return rc; +} + +/* +** Push an authorization context. After this routine is called, the +** zArg3 argument to authorization callbacks will be zContext until +** popped. Or if pParse==0, this routine is a no-op. +*/ +SQLITE_PRIVATE void sqlite3AuthContextPush( + Parse *pParse, + AuthContext *pContext, + const char *zContext +){ + assert( pParse ); + pContext->pParse = pParse; + pContext->zAuthContext = pParse->zAuthContext; + pParse->zAuthContext = zContext; +} + +/* +** Pop an authorization context that was previously pushed +** by sqlite3AuthContextPush +*/ +SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){ + if( pContext->pParse ){ + pContext->pParse->zAuthContext = pContext->zAuthContext; + pContext->pParse = 0; + } +} + +#endif /* SQLITE_OMIT_AUTHORIZATION */ + +/************** End of auth.c ************************************************/ +/************** Begin file build.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the SQLite parser +** when syntax rules are reduced. The routines in this file handle the +** following kinds of SQL syntax: +** +** CREATE TABLE +** DROP TABLE +** CREATE INDEX +** DROP INDEX +** creating ID lists +** BEGIN TRANSACTION +** COMMIT +** ROLLBACK +*/ +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** The TableLock structure is only used by the sqlite3TableLock() and +** codeTableLocks() functions. +*/ +struct TableLock { + int iDb; /* The database containing the table to be locked */ + int iTab; /* The root page of the table to be locked */ + u8 isWriteLock; /* True for write lock. False for a read lock */ + const char *zName; /* Name of the table */ +}; + +/* +** Record the fact that we want to lock a table at run-time. +** +** The table to be locked has root page iTab and is found in database iDb. +** A read or a write lock can be taken depending on isWritelock. +** +** This routine just records the fact that the lock is desired. The +** code to make the lock occur is generated by a later call to +** codeTableLocks() which occurs during sqlite3FinishCoding(). +*/ +SQLITE_PRIVATE void sqlite3TableLock( + Parse *pParse, /* Parsing context */ + int iDb, /* Index of the database containing the table to lock */ + int iTab, /* Root page number of the table to be locked */ + u8 isWriteLock, /* True for a write lock */ + const char *zName /* Name of the table to be locked */ +){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + int i; + int nBytes; + TableLock *p; + assert( iDb>=0 ); + + for(i=0; inTableLock; i++){ + p = &pToplevel->aTableLock[i]; + if( p->iDb==iDb && p->iTab==iTab ){ + p->isWriteLock = (p->isWriteLock || isWriteLock); + return; + } + } + + nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); + pToplevel->aTableLock = + sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); + if( pToplevel->aTableLock ){ + p = &pToplevel->aTableLock[pToplevel->nTableLock++]; + p->iDb = iDb; + p->iTab = iTab; + p->isWriteLock = isWriteLock; + p->zName = zName; + }else{ + pToplevel->nTableLock = 0; + sqlite3OomFault(pToplevel->db); + } +} + +/* +** Code an OP_TableLock instruction for each table locked by the +** statement (configured by calls to sqlite3TableLock()). +*/ +static void codeTableLocks(Parse *pParse){ + int i; + Vdbe *pVdbe; + + pVdbe = sqlite3GetVdbe(pParse); + assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ + + for(i=0; inTableLock; i++){ + TableLock *p = &pParse->aTableLock[i]; + int p1 = p->iDb; + sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, + p->zName, P4_STATIC); + } +} +#else + #define codeTableLocks(x) +#endif + +/* +** Return TRUE if the given yDbMask object is empty - if it contains no +** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() +** macros when SQLITE_MAX_ATTACHED is greater than 30. +*/ +#if SQLITE_MAX_ATTACHED>30 +SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){ + int i; + for(i=0; ipToplevel==0 ); + db = pParse->db; + if( pParse->nested ) return; + if( db->mallocFailed || pParse->nErr ){ + if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; + return; + } + + /* Begin by generating some termination code at the end of the + ** vdbe program + */ + v = sqlite3GetVdbe(pParse); + assert( !pParse->isMultiWrite + || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); + if( v ){ + while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){} + sqlite3VdbeAddOp0(v, OP_Halt); + +#if SQLITE_USER_AUTHENTICATION + if( pParse->nTableLock>0 && db->init.busy==0 ){ + sqlite3UserAuthInit(db); + if( db->auth.authLevelrc = SQLITE_AUTH_USER; + sqlite3ErrorMsg(pParse, "user not authenticated"); + return; + } + } +#endif + + /* The cookie mask contains one bit for each database file open. + ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are + ** set for each database that is used. Generate code to start a + ** transaction on each used database and to verify the schema cookie + ** on each used database. + */ + if( db->mallocFailed==0 + && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) + ){ + int iDb, i; + assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); + sqlite3VdbeJumpHere(v, 0); + for(iDb=0; iDbnDb; iDb++){ + if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; + sqlite3VdbeUsesBtree(v, iDb); + sqlite3VdbeAddOp4Int(v, + OP_Transaction, /* Opcode */ + iDb, /* P1 */ + DbMaskTest(pParse->writeMask,iDb), /* P2 */ + pParse->cookieValue[iDb], /* P3 */ + db->aDb[iDb].pSchema->iGeneration /* P4 */ + ); + if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); + VdbeComment((v, + "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + for(i=0; inVtabLock; i++){ + char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); + } + pParse->nVtabLock = 0; +#endif + + /* Once all the cookies have been verified and transactions opened, + ** obtain the required table-locks. This is a no-op unless the + ** shared-cache feature is enabled. + */ + codeTableLocks(pParse); + + /* Initialize any AUTOINCREMENT data structures required. + */ + sqlite3AutoincrementBegin(pParse); + + /* Code constant expressions that where factored out of inner loops */ + if( pParse->pConstExpr ){ + ExprList *pEL = pParse->pConstExpr; + pParse->okConstFactor = 0; + for(i=0; inExpr; i++){ + sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); + } + } + + /* Finally, jump back to the beginning of the executable code. */ + sqlite3VdbeGoto(v, 1); + } + } + + + /* Get the VDBE program ready for execution + */ + if( v && pParse->nErr==0 && !db->mallocFailed ){ + assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ + /* A minimum of one cursor is required if autoincrement is used + * See ticket [a696379c1f08866] */ + if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; + sqlite3VdbeMakeReady(v, pParse); + pParse->rc = SQLITE_DONE; + }else{ + pParse->rc = SQLITE_ERROR; + } + + /* We are done with this Parse object. There is no need to de-initialize it */ +#if 0 + pParse->colNamesSet = 0; + pParse->nTab = 0; + pParse->nMem = 0; + pParse->nSet = 0; + pParse->nVar = 0; + DbMaskZero(pParse->cookieMask); +#endif +} + +/* +** Run the parser and code generator recursively in order to generate +** code for the SQL statement given onto the end of the pParse context +** currently under construction. When the parser is run recursively +** this way, the final OP_Halt is not appended and other initialization +** and finalization steps are omitted because those are handling by the +** outermost parser. +** +** Not everything is nestable. This facility is designed to permit +** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use +** care if you decide to try to use this routine for some other purposes. +*/ +SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ + va_list ap; + char *zSql; + char *zErrMsg = 0; + sqlite3 *db = pParse->db; +# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) + char saveBuf[SAVE_SZ]; + + if( pParse->nErr ) return; + assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ + va_start(ap, zFormat); + zSql = sqlite3VMPrintf(db, zFormat, ap); + va_end(ap); + if( zSql==0 ){ + return; /* A malloc must have failed */ + } + pParse->nested++; + memcpy(saveBuf, &pParse->nVar, SAVE_SZ); + memset(&pParse->nVar, 0, SAVE_SZ); + sqlite3RunParser(pParse, zSql, &zErrMsg); + sqlite3DbFree(db, zErrMsg); + sqlite3DbFree(db, zSql); + memcpy(&pParse->nVar, saveBuf, SAVE_SZ); + pParse->nested--; +} + +#if SQLITE_USER_AUTHENTICATION +/* +** Return TRUE if zTable is the name of the system table that stores the +** list of users and their access credentials. +*/ +SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){ + return sqlite3_stricmp(zTable, "sqlite_user")==0; +} +#endif + +/* +** Locate the in-memory structure that describes a particular database +** table given the name of that table and (optionally) the name of the +** database containing the table. Return NULL if not found. +** +** If zDatabase is 0, all databases are searched for the table and the +** first matching table is returned. (No checking for duplicate table +** names is done.) The search order is TEMP first, then MAIN, then any +** auxiliary databases added using the ATTACH command. +** +** See also sqlite3LocateTable(). +*/ +SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ + Table *p = 0; + int i; + + /* All mutexes are required for schema access. Make sure we hold them. */ + assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); +#if SQLITE_USER_AUTHENTICATION + /* Only the admin user is allowed to know that the sqlite_user table + ** exists */ + if( db->auth.authLevelnDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; + assert( sqlite3SchemaMutexHeld(db, j, 0) ); + p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); + if( p ) break; + } + return p; +} + +/* +** Locate the in-memory structure that describes a particular database +** table given the name of that table and (optionally) the name of the +** database containing the table. Return NULL if not found. Also leave an +** error message in pParse->zErrMsg. +** +** The difference between this routine and sqlite3FindTable() is that this +** routine leaves an error message in pParse->zErrMsg where +** sqlite3FindTable() does not. +*/ +SQLITE_PRIVATE Table *sqlite3LocateTable( + Parse *pParse, /* context in which to report errors */ + int isView, /* True if looking for a VIEW rather than a TABLE */ + const char *zName, /* Name of the table we are looking for */ + const char *zDbase /* Name of the database. Might be NULL */ +){ + Table *p; + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return 0; + } + + p = sqlite3FindTable(pParse->db, zName, zDbase); + if( p==0 ){ + const char *zMsg = isView ? "no such view" : "no such table"; +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ + /* If zName is the not the name of a table in the schema created using + ** CREATE, then check to see if it is the name of an virtual table that + ** can be an eponymous virtual table. */ + Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); + if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ + return pMod->pEpoTab; + } + } +#endif + if( zDbase ){ + sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); + }else{ + sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); + } + pParse->checkSchema = 1; + } + + return p; +} + +/* +** Locate the table identified by *p. +** +** This is a wrapper around sqlite3LocateTable(). The difference between +** sqlite3LocateTable() and this function is that this function restricts +** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be +** non-NULL if it is part of a view or trigger program definition. See +** sqlite3FixSrcList() for details. +*/ +SQLITE_PRIVATE Table *sqlite3LocateTableItem( + Parse *pParse, + int isView, + struct SrcList_item *p +){ + const char *zDb; + assert( p->pSchema==0 || p->zDatabase==0 ); + if( p->pSchema ){ + int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); + zDb = pParse->db->aDb[iDb].zName; + }else{ + zDb = p->zDatabase; + } + return sqlite3LocateTable(pParse, isView, p->zName, zDb); +} + +/* +** Locate the in-memory structure that describes +** a particular index given the name of that index +** and the name of the database that contains the index. +** Return NULL if not found. +** +** If zDatabase is 0, all databases are searched for the +** table and the first matching index is returned. (No checking +** for duplicate index names is done.) The search order is +** TEMP first, then MAIN, then any auxiliary databases added +** using the ATTACH command. +*/ +SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ + Index *p = 0; + int i; + /* All mutexes are required for schema access. Make sure we hold them. */ + assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); + for(i=OMIT_TEMPDB; inDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + Schema *pSchema = db->aDb[j].pSchema; + assert( pSchema ); + if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; + assert( sqlite3SchemaMutexHeld(db, j, 0) ); + p = sqlite3HashFind(&pSchema->idxHash, zName); + if( p ) break; + } + return p; +} + +/* +** Reclaim the memory used by an index +*/ +static void freeIndex(sqlite3 *db, Index *p){ +#ifndef SQLITE_OMIT_ANALYZE + sqlite3DeleteIndexSamples(db, p); +#endif + sqlite3ExprDelete(db, p->pPartIdxWhere); + sqlite3ExprListDelete(db, p->aColExpr); + sqlite3DbFree(db, p->zColAff); + if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3_free(p->aiRowEst); +#endif + sqlite3DbFree(db, p); +} + +/* +** For the index called zIdxName which is found in the database iDb, +** unlike that index from its Table then remove the index from +** the index hash table and free all memory structures associated +** with the index. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ + Index *pIndex; + Hash *pHash; + + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pHash = &db->aDb[iDb].pSchema->idxHash; + pIndex = sqlite3HashInsert(pHash, zIdxName, 0); + if( ALWAYS(pIndex) ){ + if( pIndex->pTable->pIndex==pIndex ){ + pIndex->pTable->pIndex = pIndex->pNext; + }else{ + Index *p; + /* Justification of ALWAYS(); The index must be on the list of + ** indices. */ + p = pIndex->pTable->pIndex; + while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } + if( ALWAYS(p && p->pNext==pIndex) ){ + p->pNext = pIndex->pNext; + } + } + freeIndex(db, pIndex); + } + db->flags |= SQLITE_InternChanges; +} + +/* +** Look through the list of open database files in db->aDb[] and if +** any have been closed, remove them from the list. Reallocate the +** db->aDb[] structure to a smaller size, if possible. +** +** Entry 0 (the "main" database) and entry 1 (the "temp" database) +** are never candidates for being collapsed. +*/ +SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){ + int i, j; + for(i=j=2; inDb; i++){ + struct Db *pDb = &db->aDb[i]; + if( pDb->pBt==0 ){ + sqlite3DbFree(db, pDb->zName); + pDb->zName = 0; + continue; + } + if( jaDb[j] = db->aDb[i]; + } + j++; + } + db->nDb = j; + if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ + memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); + sqlite3DbFree(db, db->aDb); + db->aDb = db->aDbStatic; + } +} + +/* +** Reset the schema for the database at index iDb. Also reset the +** TEMP schema. +*/ +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ + Db *pDb; + assert( iDbnDb ); + + /* Case 1: Reset the single schema identified by iDb */ + pDb = &db->aDb[iDb]; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + assert( pDb->pSchema!=0 ); + sqlite3SchemaClear(pDb->pSchema); + + /* If any database other than TEMP is reset, then also reset TEMP + ** since TEMP might be holding triggers that reference tables in the + ** other database. + */ + if( iDb!=1 ){ + pDb = &db->aDb[1]; + assert( pDb->pSchema!=0 ); + sqlite3SchemaClear(pDb->pSchema); + } + return; +} + +/* +** Erase all schema information from all attached databases (including +** "main" and "temp") for a single database connection. +*/ +SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ + int i; + sqlite3BtreeEnterAll(db); + for(i=0; inDb; i++){ + Db *pDb = &db->aDb[i]; + if( pDb->pSchema ){ + sqlite3SchemaClear(pDb->pSchema); + } + } + db->flags &= ~SQLITE_InternChanges; + sqlite3VtabUnlockList(db); + sqlite3BtreeLeaveAll(db); + sqlite3CollapseDatabaseArray(db); +} + +/* +** This routine is called when a commit occurs. +*/ +SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){ + db->flags &= ~SQLITE_InternChanges; +} + +/* +** Delete memory allocated for the column names of a table or view (the +** Table.aCol[] array). +*/ +SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ + int i; + Column *pCol; + assert( pTable!=0 ); + if( (pCol = pTable->aCol)!=0 ){ + for(i=0; inCol; i++, pCol++){ + sqlite3DbFree(db, pCol->zName); + sqlite3ExprDelete(db, pCol->pDflt); + sqlite3DbFree(db, pCol->zColl); + } + sqlite3DbFree(db, pTable->aCol); + } +} + +/* +** Remove the memory data structures associated with the given +** Table. No changes are made to disk by this routine. +** +** This routine just deletes the data structure. It does not unlink +** the table data structure from the hash table. But it does destroy +** memory structures of the indices and foreign keys associated with +** the table. +** +** The db parameter is optional. It is needed if the Table object +** contains lookaside memory. (Table objects in the schema do not use +** lookaside memory, but some ephemeral Table objects do.) Or the +** db parameter can be used with db->pnBytesFreed to measure the memory +** used by the Table object. +*/ +static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ + Index *pIndex, *pNext; + TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */ + + /* Record the number of outstanding lookaside allocations in schema Tables + ** prior to doing any free() operations. Since schema Tables do not use + ** lookaside, this number should not change. */ + TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ? + db->lookaside.nOut : 0 ); + + /* Delete all indices associated with this table. */ + for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ + pNext = pIndex->pNext; + assert( pIndex->pSchema==pTable->pSchema ); + if( !db || db->pnBytesFreed==0 ){ + char *zName = pIndex->zName; + TESTONLY ( Index *pOld = ) sqlite3HashInsert( + &pIndex->pSchema->idxHash, zName, 0 + ); + assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); + assert( pOld==pIndex || pOld==0 ); + } + freeIndex(db, pIndex); + } + + /* Delete any foreign keys attached to this table. */ + sqlite3FkDelete(db, pTable); + + /* Delete the Table structure itself. + */ + sqlite3DeleteColumnNames(db, pTable); + sqlite3DbFree(db, pTable->zName); + sqlite3DbFree(db, pTable->zColAff); + sqlite3SelectDelete(db, pTable->pSelect); + sqlite3ExprListDelete(db, pTable->pCheck); +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3VtabClear(db, pTable); +#endif + sqlite3DbFree(db, pTable); + + /* Verify that no lookaside memory was used by schema tables */ + assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); +} +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ + /* Do not delete the table until the reference count reaches zero. */ + if( !pTable ) return; + if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; + deleteTable(db, pTable); +} + + +/* +** Unlink the given table from the hash tables and the delete the +** table structure with all its indices and foreign keys. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ + Table *p; + Db *pDb; + + assert( db!=0 ); + assert( iDb>=0 && iDbnDb ); + assert( zTabName ); + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ + pDb = &db->aDb[iDb]; + p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); + sqlite3DeleteTable(db, p); + db->flags |= SQLITE_InternChanges; +} + +/* +** Given a token, return a string that consists of the text of that +** token. Space to hold the returned string +** is obtained from sqliteMalloc() and must be freed by the calling +** function. +** +** Any quotation marks (ex: "name", 'name', [name], or `name`) that +** surround the body of the token are removed. +** +** Tokens are often just pointers into the original SQL text and so +** are not \000 terminated and are not persistent. The returned string +** is \000 terminated and is persistent. +*/ +SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ + char *zName; + if( pName ){ + zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); + sqlite3Dequote(zName); + }else{ + zName = 0; + } + return zName; +} + +/* +** Open the sqlite_master table stored in database number iDb for +** writing. The table is opened using cursor 0. +*/ +SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){ + Vdbe *v = sqlite3GetVdbe(p); + sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); + sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); + if( p->nTab==0 ){ + p->nTab = 1; + } +} + +/* +** Parameter zName points to a nul-terminated buffer containing the name +** of a database ("main", "temp" or the name of an attached db). This +** function returns the index of the named database in db->aDb[], or +** -1 if the named db cannot be found. +*/ +SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){ + int i = -1; /* Database number */ + if( zName ){ + Db *pDb; + for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ + if( 0==sqlite3StrICmp(pDb->zName, zName) ) break; + } + } + return i; +} + +/* +** The token *pName contains the name of a database (either "main" or +** "temp" or the name of an attached db). This routine returns the +** index of the named database in db->aDb[], or -1 if the named db +** does not exist. +*/ +SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){ + int i; /* Database number */ + char *zName; /* Name we are searching for */ + zName = sqlite3NameFromToken(db, pName); + i = sqlite3FindDbName(db, zName); + sqlite3DbFree(db, zName); + return i; +} + +/* The table or view or trigger name is passed to this routine via tokens +** pName1 and pName2. If the table name was fully qualified, for example: +** +** CREATE TABLE xxx.yyy (...); +** +** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if +** the table name is not fully qualified, i.e.: +** +** CREATE TABLE yyy(...); +** +** Then pName1 is set to "yyy" and pName2 is "". +** +** This routine sets the *ppUnqual pointer to point at the token (pName1 or +** pName2) that stores the unqualified table name. The index of the +** database "xxx" is returned. +*/ +SQLITE_PRIVATE int sqlite3TwoPartName( + Parse *pParse, /* Parsing and code generating context */ + Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ + Token *pName2, /* The "yyy" in the name "xxx.yyy" */ + Token **pUnqual /* Write the unqualified object name here */ +){ + int iDb; /* Database holding the object */ + sqlite3 *db = pParse->db; + + assert( pName2!=0 ); + if( pName2->n>0 ){ + if( db->init.busy ) { + sqlite3ErrorMsg(pParse, "corrupt database"); + return -1; + } + *pUnqual = pName2; + iDb = sqlite3FindDb(db, pName1); + if( iDb<0 ){ + sqlite3ErrorMsg(pParse, "unknown database %T", pName1); + return -1; + } + }else{ + assert( db->init.iDb==0 || db->init.busy ); + iDb = db->init.iDb; + *pUnqual = pName1; + } + return iDb; +} + +/* +** This routine is used to check if the UTF-8 string zName is a legal +** unqualified name for a new schema object (table, index, view or +** trigger). All names are legal except those that begin with the string +** "sqlite_" (in upper, lower or mixed case). This portion of the namespace +** is reserved for internal use. +*/ +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){ + if( !pParse->db->init.busy && pParse->nested==0 + && (pParse->db->flags & SQLITE_WriteSchema)==0 + && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ + sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +/* +** Return the PRIMARY KEY index of a table +*/ +SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){ + Index *p; + for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} + return p; +} + +/* +** Return the column of index pIdx that corresponds to table +** column iCol. Return -1 if not found. +*/ +SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){ + int i; + for(i=0; inColumn; i++){ + if( iCol==pIdx->aiColumn[i] ) return i; + } + return -1; +} + +/* +** Begin constructing a new table representation in memory. This is +** the first of several action routines that get called in response +** to a CREATE TABLE statement. In particular, this routine is called +** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp +** flag is true if the table should be stored in the auxiliary database +** file instead of in the main database file. This is normally the case +** when the "TEMP" or "TEMPORARY" keyword occurs in between +** CREATE and TABLE. +** +** The new table record is initialized and put in pParse->pNewTable. +** As more of the CREATE TABLE statement is parsed, additional action +** routines will be called to add more information to this record. +** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine +** is called to complete the construction of the new table record. +*/ +SQLITE_PRIVATE void sqlite3StartTable( + Parse *pParse, /* Parser context */ + Token *pName1, /* First part of the name of the table or view */ + Token *pName2, /* Second part of the name of the table or view */ + int isTemp, /* True if this is a TEMP table */ + int isView, /* True if this is a VIEW */ + int isVirtual, /* True if this is a VIRTUAL table */ + int noErr /* Do nothing if table already exists */ +){ + Table *pTable; + char *zName = 0; /* The name of the new table */ + sqlite3 *db = pParse->db; + Vdbe *v; + int iDb; /* Database number to create the table in */ + Token *pName; /* Unqualified name of the table to create */ + + if( db->init.busy && db->init.newTnum==1 ){ + /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ + iDb = db->init.iDb; + zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); + pName = pName1; + }else{ + /* The common case */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ) return; + if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ + /* If creating a temp table, the name may not be qualified. Unless + ** the database name is "temp" anyway. */ + sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); + return; + } + if( !OMIT_TEMPDB && isTemp ) iDb = 1; + zName = sqlite3NameFromToken(db, pName); + } + pParse->sNameToken = *pName; + if( zName==0 ) return; + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto begin_table_error; + } + if( db->init.iDb==1 ) isTemp = 1; +#ifndef SQLITE_OMIT_AUTHORIZATION + assert( isTemp==0 || isTemp==1 ); + assert( isView==0 || isView==1 ); + { + static const u8 aCode[] = { + SQLITE_CREATE_TABLE, + SQLITE_CREATE_TEMP_TABLE, + SQLITE_CREATE_VIEW, + SQLITE_CREATE_TEMP_VIEW + }; + char *zDb = db->aDb[iDb].zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ + goto begin_table_error; + } + if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], + zName, 0, zDb) ){ + goto begin_table_error; + } + } +#endif + + /* Make sure the new table name does not collide with an existing + ** index or table name in the same database. Issue an error message if + ** it does. The exception is if the statement being parsed was passed + ** to an sqlite3_declare_vtab() call. In that case only the column names + ** and types will be used, so there is no need to test for namespace + ** collisions. + */ + if( !IN_DECLARE_VTAB ){ + char *zDb = db->aDb[iDb].zName; + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto begin_table_error; + } + pTable = sqlite3FindTable(db, zName, zDb); + if( pTable ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "table %T already exists", pName); + }else{ + assert( !db->init.busy || CORRUPT_DB ); + sqlite3CodeVerifySchema(pParse, iDb); + } + goto begin_table_error; + } + if( sqlite3FindIndex(db, zName, zDb)!=0 ){ + sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); + goto begin_table_error; + } + } + + pTable = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTable==0 ){ + assert( db->mallocFailed ); + pParse->rc = SQLITE_NOMEM_BKPT; + pParse->nErr++; + goto begin_table_error; + } + pTable->zName = zName; + pTable->iPKey = -1; + pTable->pSchema = db->aDb[iDb].pSchema; + pTable->nRef = 1; + pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); + assert( pParse->pNewTable==0 ); + pParse->pNewTable = pTable; + + /* If this is the magic sqlite_sequence table used by autoincrement, + ** then record a pointer to this table in the main database structure + ** so that INSERT can find the table easily. + */ +#ifndef SQLITE_OMIT_AUTOINCREMENT + if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pTable->pSchema->pSeqTab = pTable; + } +#endif + + /* Begin generating the code that will insert the table record into + ** the SQLITE_MASTER table. Note in particular that we must go ahead + ** and allocate the record number for the table entry now. Before any + ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause + ** indices to be created and the table record must come before the + ** indices. Hence, the record number for the table must be allocated + ** now. + */ + if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ + int addr1; + int fileFormat; + int reg1, reg2, reg3; + /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ + static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; + sqlite3BeginWriteOperation(pParse, 1, iDb); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( isVirtual ){ + sqlite3VdbeAddOp0(v, OP_VBegin); + } +#endif + + /* If the file format and encoding in the database have not been set, + ** set them now. + */ + reg1 = pParse->regRowid = ++pParse->nMem; + reg2 = pParse->regRoot = ++pParse->nMem; + reg3 = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); + sqlite3VdbeUsesBtree(v, iDb); + addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); + fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? + 1 : SQLITE_MAX_FILE_FORMAT; + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); + sqlite3VdbeJumpHere(v, addr1); + + /* This just creates a place-holder record in the sqlite_master table. + ** The record created does not contain anything yet. It will be replaced + ** by the real entry in code generated at sqlite3EndTable(). + ** + ** The rowid for the new entry is left in register pParse->regRowid. + ** The root page number of the new table is left in reg pParse->regRoot. + ** The rowid and root page number values are needed by the code that + ** sqlite3EndTable will generate. + */ +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) + if( isView || isVirtual ){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); + }else +#endif + { + pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); + } + sqlite3OpenMasterTable(pParse, iDb); + sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); + sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); + sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeAddOp0(v, OP_Close); + } + + /* Normal (non-error) return. */ + return; + + /* If an error occurs, we jump here */ +begin_table_error: + sqlite3DbFree(db, zName); + return; +} + +/* Set properties of a table column based on the (magical) +** name of the column. +*/ +#if SQLITE_ENABLE_HIDDEN_COLUMNS +SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ + if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ + pCol->colFlags |= COLFLAG_HIDDEN; + }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ + pTab->tabFlags |= TF_OOOHidden; + } +} +#endif + + +/* +** Add a new column to the table currently being constructed. +** +** The parser calls this routine once for each column declaration +** in a CREATE TABLE statement. sqlite3StartTable() gets called +** first to get things going. Then this routine is called for each +** column. +*/ +SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ + Table *p; + int i; + char *z; + char *zType; + Column *pCol; + sqlite3 *db = pParse->db; + if( (p = pParse->pNewTable)==0 ) return; +#if SQLITE_MAX_COLUMN + if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); + return; + } +#endif + z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); + if( z==0 ) return; + memcpy(z, pName->z, pName->n); + z[pName->n] = 0; + sqlite3Dequote(z); + for(i=0; inCol; i++){ + if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ + sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); + sqlite3DbFree(db, z); + return; + } + } + if( (p->nCol & 0x7)==0 ){ + Column *aNew; + aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); + if( aNew==0 ){ + sqlite3DbFree(db, z); + return; + } + p->aCol = aNew; + } + pCol = &p->aCol[p->nCol]; + memset(pCol, 0, sizeof(p->aCol[0])); + pCol->zName = z; + sqlite3ColumnPropertiesFromName(p, pCol); + + if( pType->n==0 ){ + /* If there is no type specified, columns have the default affinity + ** 'BLOB'. */ + pCol->affinity = SQLITE_AFF_BLOB; + pCol->szEst = 1; + }else{ + zType = z + sqlite3Strlen30(z) + 1; + memcpy(zType, pType->z, pType->n); + zType[pType->n] = 0; + sqlite3Dequote(zType); + pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst); + pCol->colFlags |= COLFLAG_HASTYPE; + } + p->nCol++; + pParse->constraintName.n = 0; +} + +/* +** This routine is called by the parser while in the middle of +** parsing a CREATE TABLE statement. A "NOT NULL" constraint has +** been seen on a column. This routine sets the notNull flag on +** the column currently under construction. +*/ +SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){ + Table *p; + p = pParse->pNewTable; + if( p==0 || NEVER(p->nCol<1) ) return; + p->aCol[p->nCol-1].notNull = (u8)onError; +} + +/* +** Scan the column type name zType (length nType) and return the +** associated affinity type. +** +** This routine does a case-independent search of zType for the +** substrings in the following table. If one of the substrings is +** found, the corresponding affinity is returned. If zType contains +** more than one of the substrings, entries toward the top of +** the table take priority. For example, if zType is 'BLOBINT', +** SQLITE_AFF_INTEGER is returned. +** +** Substring | Affinity +** -------------------------------- +** 'INT' | SQLITE_AFF_INTEGER +** 'CHAR' | SQLITE_AFF_TEXT +** 'CLOB' | SQLITE_AFF_TEXT +** 'TEXT' | SQLITE_AFF_TEXT +** 'BLOB' | SQLITE_AFF_BLOB +** 'REAL' | SQLITE_AFF_REAL +** 'FLOA' | SQLITE_AFF_REAL +** 'DOUB' | SQLITE_AFF_REAL +** +** If none of the substrings in the above table are found, +** SQLITE_AFF_NUMERIC is returned. +*/ +SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){ + u32 h = 0; + char aff = SQLITE_AFF_NUMERIC; + const char *zChar = 0; + + assert( zIn!=0 ); + while( zIn[0] ){ + h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; + zIn++; + if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ + aff = SQLITE_AFF_TEXT; + zChar = zIn; + }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ + aff = SQLITE_AFF_TEXT; + }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ + aff = SQLITE_AFF_TEXT; + }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ + && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ + aff = SQLITE_AFF_BLOB; + if( zIn[0]=='(' ) zChar = zIn; +#ifndef SQLITE_OMIT_FLOATING_POINT + }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; + }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; + }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; +#endif + }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ + aff = SQLITE_AFF_INTEGER; + break; + } + } + + /* If pszEst is not NULL, store an estimate of the field size. The + ** estimate is scaled so that the size of an integer is 1. */ + if( pszEst ){ + *pszEst = 1; /* default size is approx 4 bytes */ + if( aff255 ) v = 255; + *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ + break; + } + zChar++; + } + }else{ + *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ + } + } + } + return aff; +} + +/* +** The expression is the default value for the most recently added column +** of the table currently under construction. +** +** Default value expressions must be constant. Raise an exception if this +** is not the case. +** +** This routine is called by the parser while in the middle of +** parsing a CREATE TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ + Table *p; + Column *pCol; + sqlite3 *db = pParse->db; + p = pParse->pNewTable; + if( p!=0 ){ + pCol = &(p->aCol[p->nCol-1]); + if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ + sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", + pCol->zName); + }else{ + /* A copy of pExpr is used instead of the original, as pExpr contains + ** tokens that point to volatile memory. The 'span' of the expression + ** is required by pragma table_info. + */ + Expr x; + sqlite3ExprDelete(db, pCol->pDflt); + memset(&x, 0, sizeof(x)); + x.op = TK_SPAN; + x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart, + (int)(pSpan->zEnd - pSpan->zStart)); + x.pLeft = pSpan->pExpr; + x.flags = EP_Skip; + pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); + sqlite3DbFree(db, x.u.zToken); + } + } + sqlite3ExprDelete(db, pSpan->pExpr); +} + +/* +** Backwards Compatibility Hack: +** +** Historical versions of SQLite accepted strings as column names in +** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: +** +** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) +** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); +** +** This is goofy. But to preserve backwards compatibility we continue to +** accept it. This routine does the necessary conversion. It converts +** the expression given in its argument from a TK_STRING into a TK_ID +** if the expression is just a TK_STRING with an optional COLLATE clause. +** If the epxression is anything other than TK_STRING, the expression is +** unchanged. +*/ +static void sqlite3StringToId(Expr *p){ + if( p->op==TK_STRING ){ + p->op = TK_ID; + }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ + p->pLeft->op = TK_ID; + } +} + +/* +** Designate the PRIMARY KEY for the table. pList is a list of names +** of columns that form the primary key. If pList is NULL, then the +** most recently added column of the table is the primary key. +** +** A table can have at most one primary key. If the table already has +** a primary key (and this is the second primary key) then create an +** error. +** +** If the PRIMARY KEY is on a single column whose datatype is INTEGER, +** then we will try to use that column as the rowid. Set the Table.iPKey +** field of the table under construction to be the index of the +** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is +** no INTEGER PRIMARY KEY. +** +** If the key is not an INTEGER PRIMARY KEY, then create a unique +** index for the key. No index is created for INTEGER PRIMARY KEYs. +*/ +SQLITE_PRIVATE void sqlite3AddPrimaryKey( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List of field names to be indexed */ + int onError, /* What to do with a uniqueness conflict */ + int autoInc, /* True if the AUTOINCREMENT keyword is present */ + int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ +){ + Table *pTab = pParse->pNewTable; + Column *pCol = 0; + int iCol = -1, i; + int nTerm; + if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit; + if( pTab->tabFlags & TF_HasPrimaryKey ){ + sqlite3ErrorMsg(pParse, + "table \"%s\" has more than one primary key", pTab->zName); + goto primary_key_exit; + } + pTab->tabFlags |= TF_HasPrimaryKey; + if( pList==0 ){ + iCol = pTab->nCol - 1; + pCol = &pTab->aCol[iCol]; + pCol->colFlags |= COLFLAG_PRIMKEY; + nTerm = 1; + }else{ + nTerm = pList->nExpr; + for(i=0; ia[i].pExpr); + assert( pCExpr!=0 ); + sqlite3StringToId(pCExpr); + if( pCExpr->op==TK_ID ){ + const char *zCName = pCExpr->u.zToken; + for(iCol=0; iColnCol; iCol++){ + if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ + pCol = &pTab->aCol[iCol]; + pCol->colFlags |= COLFLAG_PRIMKEY; + break; + } + } + } + } + } + if( nTerm==1 + && pCol + && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 + && sortOrder!=SQLITE_SO_DESC + ){ + pTab->iPKey = iCol; + pTab->keyConf = (u8)onError; + assert( autoInc==0 || autoInc==1 ); + pTab->tabFlags |= autoInc*TF_Autoincrement; + if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; + }else if( autoInc ){ +#ifndef SQLITE_OMIT_AUTOINCREMENT + sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " + "INTEGER PRIMARY KEY"); +#endif + }else{ + Index *p; + p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, + 0, sortOrder, 0); + if( p ){ + p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; + } + pList = 0; + } + +primary_key_exit: + sqlite3ExprListDelete(pParse->db, pList); + return; +} + +/* +** Add a new CHECK constraint to the table currently under construction. +*/ +SQLITE_PRIVATE void sqlite3AddCheckConstraint( + Parse *pParse, /* Parsing context */ + Expr *pCheckExpr /* The check expression */ +){ +#ifndef SQLITE_OMIT_CHECK + Table *pTab = pParse->pNewTable; + sqlite3 *db = pParse->db; + if( pTab && !IN_DECLARE_VTAB + && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) + ){ + pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); + if( pParse->constraintName.n ){ + sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); + } + }else +#endif + { + sqlite3ExprDelete(pParse->db, pCheckExpr); + } +} + +/* +** Set the collation function of the most recently parsed table column +** to the CollSeq given. +*/ +SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){ + Table *p; + int i; + char *zColl; /* Dequoted name of collation sequence */ + sqlite3 *db; + + if( (p = pParse->pNewTable)==0 ) return; + i = p->nCol-1; + db = pParse->db; + zColl = sqlite3NameFromToken(db, pToken); + if( !zColl ) return; + + if( sqlite3LocateCollSeq(pParse, zColl) ){ + Index *pIdx; + sqlite3DbFree(db, p->aCol[i].zColl); + p->aCol[i].zColl = zColl; + + /* If the column is declared as " PRIMARY KEY COLLATE ", + ** then an index may have been created on this column before the + ** collation type was added. Correct this if it is the case. + */ + for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ + assert( pIdx->nKeyCol==1 ); + if( pIdx->aiColumn[0]==i ){ + pIdx->azColl[0] = p->aCol[i].zColl; + } + } + }else{ + sqlite3DbFree(db, zColl); + } +} + +/* +** This function returns the collation sequence for database native text +** encoding identified by the string zName, length nName. +** +** If the requested collation sequence is not available, or not available +** in the database native encoding, the collation factory is invoked to +** request it. If the collation factory does not supply such a sequence, +** and the sequence is available in another text encoding, then that is +** returned instead. +** +** If no versions of the requested collations sequence are available, or +** another error occurs, NULL is returned and an error message written into +** pParse. +** +** This routine is a wrapper around sqlite3FindCollSeq(). This routine +** invokes the collation factory if the named collation cannot be found +** and generates an error message. +** +** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ + sqlite3 *db = pParse->db; + u8 enc = ENC(db); + u8 initbusy = db->init.busy; + CollSeq *pColl; + + pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); + if( !initbusy && (!pColl || !pColl->xCmp) ){ + pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName); + } + + return pColl; +} + + +/* +** Generate code that will increment the schema cookie. +** +** The schema cookie is used to determine when the schema for the +** database changes. After each schema change, the cookie value +** changes. When a process first reads the schema it records the +** cookie. Thereafter, whenever it goes to access the database, +** it checks the cookie to make sure the schema has not changed +** since it was last read. +** +** This plan is not completely bullet-proof. It is possible for +** the schema to change multiple times and for the cookie to be +** set back to prior value. But schema changes are infrequent +** and the probability of hitting the same cookie value is only +** 1 chance in 2^32. So we're safe enough. +*/ +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){ + sqlite3 *db = pParse->db; + Vdbe *v = pParse->pVdbe; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, + db->aDb[iDb].pSchema->schema_cookie+1); +} + +/* +** Measure the number of characters needed to output the given +** identifier. The number returned includes any quotes used +** but does not include the null terminator. +** +** The estimate is conservative. It might be larger that what is +** really needed. +*/ +static int identLength(const char *z){ + int n; + for(n=0; *z; n++, z++){ + if( *z=='"' ){ n++; } + } + return n + 2; +} + +/* +** The first parameter is a pointer to an output buffer. The second +** parameter is a pointer to an integer that contains the offset at +** which to write into the output buffer. This function copies the +** nul-terminated string pointed to by the third parameter, zSignedIdent, +** to the specified offset in the buffer and updates *pIdx to refer +** to the first byte after the last byte written before returning. +** +** If the string zSignedIdent consists entirely of alpha-numeric +** characters, does not begin with a digit and is not an SQL keyword, +** then it is copied to the output buffer exactly as it is. Otherwise, +** it is quoted using double-quotes. +*/ +static void identPut(char *z, int *pIdx, char *zSignedIdent){ + unsigned char *zIdent = (unsigned char*)zSignedIdent; + int i, j, needQuote; + i = *pIdx; + + for(j=0; zIdent[j]; j++){ + if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; + } + needQuote = sqlite3Isdigit(zIdent[0]) + || sqlite3KeywordCode(zIdent, j)!=TK_ID + || zIdent[j]!=0 + || j==0; + + if( needQuote ) z[i++] = '"'; + for(j=0; zIdent[j]; j++){ + z[i++] = zIdent[j]; + if( zIdent[j]=='"' ) z[i++] = '"'; + } + if( needQuote ) z[i++] = '"'; + z[i] = 0; + *pIdx = i; +} + +/* +** Generate a CREATE TABLE statement appropriate for the given +** table. Memory to hold the text of the statement is obtained +** from sqliteMalloc() and must be freed by the calling function. +*/ +static char *createTableStmt(sqlite3 *db, Table *p){ + int i, k, n; + char *zStmt; + char *zSep, *zSep2, *zEnd; + Column *pCol; + n = 0; + for(pCol = p->aCol, i=0; inCol; i++, pCol++){ + n += identLength(pCol->zName) + 5; + } + n += identLength(p->zName); + if( n<50 ){ + zSep = ""; + zSep2 = ","; + zEnd = ")"; + }else{ + zSep = "\n "; + zSep2 = ",\n "; + zEnd = "\n)"; + } + n += 35 + 6*p->nCol; + zStmt = sqlite3DbMallocRaw(0, n); + if( zStmt==0 ){ + sqlite3OomFault(db); + return 0; + } + sqlite3_snprintf(n, zStmt, "CREATE TABLE "); + k = sqlite3Strlen30(zStmt); + identPut(zStmt, &k, p->zName); + zStmt[k++] = '('; + for(pCol=p->aCol, i=0; inCol; i++, pCol++){ + static const char * const azType[] = { + /* SQLITE_AFF_BLOB */ "", + /* SQLITE_AFF_TEXT */ " TEXT", + /* SQLITE_AFF_NUMERIC */ " NUM", + /* SQLITE_AFF_INTEGER */ " INT", + /* SQLITE_AFF_REAL */ " REAL" + }; + int len; + const char *zType; + + sqlite3_snprintf(n-k, &zStmt[k], zSep); + k += sqlite3Strlen30(&zStmt[k]); + zSep = zSep2; + identPut(zStmt, &k, pCol->zName); + assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); + assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); + testcase( pCol->affinity==SQLITE_AFF_BLOB ); + testcase( pCol->affinity==SQLITE_AFF_TEXT ); + testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); + testcase( pCol->affinity==SQLITE_AFF_INTEGER ); + testcase( pCol->affinity==SQLITE_AFF_REAL ); + + zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; + len = sqlite3Strlen30(zType); + assert( pCol->affinity==SQLITE_AFF_BLOB + || pCol->affinity==sqlite3AffinityType(zType, 0) ); + memcpy(&zStmt[k], zType, len); + k += len; + assert( k<=n ); + } + sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); + return zStmt; +} + +/* +** Resize an Index object to hold N columns total. Return SQLITE_OK +** on success and SQLITE_NOMEM on an OOM error. +*/ +static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ + char *zExtra; + int nByte; + if( pIdx->nColumn>=N ) return SQLITE_OK; + assert( pIdx->isResized==0 ); + nByte = (sizeof(char*) + sizeof(i16) + 1)*N; + zExtra = sqlite3DbMallocZero(db, nByte); + if( zExtra==0 ) return SQLITE_NOMEM_BKPT; + memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); + pIdx->azColl = (const char**)zExtra; + zExtra += sizeof(char*)*N; + memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); + pIdx->aiColumn = (i16*)zExtra; + zExtra += sizeof(i16)*N; + memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); + pIdx->aSortOrder = (u8*)zExtra; + pIdx->nColumn = N; + pIdx->isResized = 1; + return SQLITE_OK; +} + +/* +** Estimate the total row width for a table. +*/ +static void estimateTableWidth(Table *pTab){ + unsigned wTable = 0; + const Column *pTabCol; + int i; + for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ + wTable += pTabCol->szEst; + } + if( pTab->iPKey<0 ) wTable++; + pTab->szTabRow = sqlite3LogEst(wTable*4); +} + +/* +** Estimate the average size of a row for an index. +*/ +static void estimateIndexWidth(Index *pIdx){ + unsigned wIndex = 0; + int i; + const Column *aCol = pIdx->pTable->aCol; + for(i=0; inColumn; i++){ + i16 x = pIdx->aiColumn[i]; + assert( xpTable->nCol ); + wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; + } + pIdx->szIdxRow = sqlite3LogEst(wIndex*4); +} + +/* Return true if value x is found any of the first nCol entries of aiCol[] +*/ +static int hasColumn(const i16 *aiCol, int nCol, int x){ + while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1; + return 0; +} + +/* +** This routine runs at the end of parsing a CREATE TABLE statement that +** has a WITHOUT ROWID clause. The job of this routine is to convert both +** internal schema data structures and the generated VDBE code so that they +** are appropriate for a WITHOUT ROWID table instead of a rowid table. +** Changes include: +** +** (1) Convert the OP_CreateTable into an OP_CreateIndex. There is +** no rowid btree for a WITHOUT ROWID. Instead, the canonical +** data storage is a covering index btree. +** (2) Bypass the creation of the sqlite_master table entry +** for the PRIMARY KEY as the primary key index is now +** identified by the sqlite_master table entry of the table itself. +** (3) Set the Index.tnum of the PRIMARY KEY Index object in the +** schema to the rootpage from the main table. +** (4) Set all columns of the PRIMARY KEY schema object to be NOT NULL. +** (5) Add all table columns to the PRIMARY KEY Index object +** so that the PRIMARY KEY is a covering index. The surplus +** columns are part of KeyInfo.nXField and are not used for +** sorting or lookup or uniqueness checks. +** (6) Replace the rowid tail on all automatically generated UNIQUE +** indices with the PRIMARY KEY columns. +*/ +static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ + Index *pIdx; + Index *pPk; + int nPk; + int i, j; + sqlite3 *db = pParse->db; + Vdbe *v = pParse->pVdbe; + + /* Convert the OP_CreateTable opcode that would normally create the + ** root-page for the table into an OP_CreateIndex opcode. The index + ** created will become the PRIMARY KEY index. + */ + if( pParse->addrCrTab ){ + assert( v ); + sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); + } + + /* Locate the PRIMARY KEY index. Or, if this table was originally + ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. + */ + if( pTab->iPKey>=0 ){ + ExprList *pList; + Token ipkToken; + sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); + pList = sqlite3ExprListAppend(pParse, 0, + sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); + if( pList==0 ) return; + pList->a[0].sortOrder = pParse->iPkSortOrder; + assert( pParse->pNewTable==pTab ); + pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); + if( pPk==0 ) return; + pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; + pTab->iPKey = -1; + }else{ + pPk = sqlite3PrimaryKeyIndex(pTab); + + /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master + ** table entry. This is only required if currently generating VDBE + ** code for a CREATE TABLE (not when parsing one as part of reading + ** a database schema). */ + if( v ){ + assert( db->init.busy==0 ); + sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); + } + + /* + ** Remove all redundant columns from the PRIMARY KEY. For example, change + ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later + ** code assumes the PRIMARY KEY contains no repeated columns. + */ + for(i=j=1; inKeyCol; i++){ + if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ + pPk->nColumn--; + }else{ + pPk->aiColumn[j++] = pPk->aiColumn[i]; + } + } + pPk->nKeyCol = j; + } + pPk->isCovering = 1; + assert( pPk!=0 ); + nPk = pPk->nKeyCol; + + /* Make sure every column of the PRIMARY KEY is NOT NULL. (Except, + ** do not enforce this for imposter tables.) */ + if( !db->init.imposterTable ){ + for(i=0; iaCol[pPk->aiColumn[i]].notNull = OE_Abort; + } + pPk->uniqNotNull = 1; + } + + /* The root page of the PRIMARY KEY is the table root page */ + pPk->tnum = pTab->tnum; + + /* Update the in-memory representation of all UNIQUE indices by converting + ** the final rowid column into one or more columns of the PRIMARY KEY. + */ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int n; + if( IsPrimaryKeyIndex(pIdx) ) continue; + for(i=n=0; iaiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++; + } + if( n==0 ){ + /* This index is a superset of the primary key */ + pIdx->nColumn = pIdx->nKeyCol; + continue; + } + if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; + for(i=0, j=pIdx->nKeyCol; iaiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){ + pIdx->aiColumn[j] = pPk->aiColumn[i]; + pIdx->azColl[j] = pPk->azColl[i]; + j++; + } + } + assert( pIdx->nColumn>=pIdx->nKeyCol+n ); + assert( pIdx->nColumn>=j ); + } + + /* Add all table columns to the PRIMARY KEY index + */ + if( nPknCol ){ + if( resizeIndexObject(db, pPk, pTab->nCol) ) return; + for(i=0, j=nPk; inCol; i++){ + if( !hasColumn(pPk->aiColumn, j, i) ){ + assert( jnColumn ); + pPk->aiColumn[j] = i; + pPk->azColl[j] = sqlite3StrBINARY; + j++; + } + } + assert( pPk->nColumn==j ); + assert( pTab->nCol==j ); + }else{ + pPk->nColumn = pTab->nCol; + } +} + +/* +** This routine is called to report the final ")" that terminates +** a CREATE TABLE statement. +** +** The table structure that other action routines have been building +** is added to the internal hash tables, assuming no errors have +** occurred. +** +** An entry for the table is made in the master table on disk, unless +** this is a temporary table or db->init.busy==1. When db->init.busy==1 +** it means we are reading the sqlite_master table because we just +** connected to the database or because the sqlite_master table has +** recently changed, so the entry for this table already exists in +** the sqlite_master table. We do not want to create it again. +** +** If the pSelect argument is not NULL, it means that this routine +** was called to create a table generated from a +** "CREATE TABLE ... AS SELECT ..." statement. The column names of +** the new table will match the result set of the SELECT. +*/ +SQLITE_PRIVATE void sqlite3EndTable( + Parse *pParse, /* Parse context */ + Token *pCons, /* The ',' token after the last column defn. */ + Token *pEnd, /* The ')' before options in the CREATE TABLE */ + u8 tabOpts, /* Extra table options. Usually 0. */ + Select *pSelect /* Select from a "CREATE ... AS SELECT" */ +){ + Table *p; /* The new table */ + sqlite3 *db = pParse->db; /* The database connection */ + int iDb; /* Database in which the table lives */ + Index *pIdx; /* An implied index of the table */ + + if( pEnd==0 && pSelect==0 ){ + return; + } + assert( !db->mallocFailed ); + p = pParse->pNewTable; + if( p==0 ) return; + + assert( !db->init.busy || !pSelect ); + + /* If the db->init.busy is 1 it means we are reading the SQL off the + ** "sqlite_master" or "sqlite_temp_master" table on the disk. + ** So do not write to the disk again. Extract the root page number + ** for the table from the db->init.newTnum field. (The page number + ** should have been put there by the sqliteOpenCb routine.) + ** + ** If the root page number is 1, that means this is the sqlite_master + ** table itself. So mark it read-only. + */ + if( db->init.busy ){ + p->tnum = db->init.newTnum; + if( p->tnum==1 ) p->tabFlags |= TF_Readonly; + } + + /* Special processing for WITHOUT ROWID Tables */ + if( tabOpts & TF_WithoutRowid ){ + if( (p->tabFlags & TF_Autoincrement) ){ + sqlite3ErrorMsg(pParse, + "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); + return; + } + if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ + sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); + }else{ + p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; + convertToWithoutRowidTable(pParse, p); + } + } + + iDb = sqlite3SchemaToIndex(db, p->pSchema); + +#ifndef SQLITE_OMIT_CHECK + /* Resolve names in all CHECK constraint expressions. + */ + if( p->pCheck ){ + sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); + } +#endif /* !defined(SQLITE_OMIT_CHECK) */ + + /* Estimate the average row size for the table and for all implied indices */ + estimateTableWidth(p); + for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ + estimateIndexWidth(pIdx); + } + + /* If not initializing, then create a record for the new table + ** in the SQLITE_MASTER table of the database. + ** + ** If this is a TEMPORARY table, write the entry into the auxiliary + ** file instead of into the main database file. + */ + if( !db->init.busy ){ + int n; + Vdbe *v; + char *zType; /* "view" or "table" */ + char *zType2; /* "VIEW" or "TABLE" */ + char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ + + v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return; + + sqlite3VdbeAddOp1(v, OP_Close, 0); + + /* + ** Initialize zType for the new view or table. + */ + if( p->pSelect==0 ){ + /* A regular table */ + zType = "table"; + zType2 = "TABLE"; +#ifndef SQLITE_OMIT_VIEW + }else{ + /* A view */ + zType = "view"; + zType2 = "VIEW"; +#endif + } + + /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT + ** statement to populate the new table. The root-page number for the + ** new table is in register pParse->regRoot. + ** + ** Once the SELECT has been coded by sqlite3Select(), it is in a + ** suitable state to query for the column names and types to be used + ** by the new table. + ** + ** A shared-cache write-lock is not required to write to the new table, + ** as a schema-lock must have already been obtained to create it. Since + ** a schema-lock excludes all other database users, the write-lock would + ** be redundant. + */ + if( pSelect ){ + SelectDest dest; /* Where the SELECT should store results */ + int regYield; /* Register holding co-routine entry-point */ + int addrTop; /* Top of the co-routine */ + int regRec; /* A record to be insert into the new table */ + int regRowid; /* Rowid of the next row to insert */ + int addrInsLoop; /* Top of the loop for inserting rows */ + Table *pSelTab; /* A table that describes the SELECT results */ + + regYield = ++pParse->nMem; + regRec = ++pParse->nMem; + regRowid = ++pParse->nMem; + assert(pParse->nTab==1); + sqlite3MayAbort(pParse); + sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); + sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); + pParse->nTab = 2; + addrTop = sqlite3VdbeCurrentAddr(v) + 1; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); + sqlite3Select(pParse, pSelect, &dest); + sqlite3VdbeEndCoroutine(v, regYield); + sqlite3VdbeJumpHere(v, addrTop - 1); + if( pParse->nErr ) return; + pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); + if( pSelTab==0 ) return; + assert( p->aCol==0 ); + p->nCol = pSelTab->nCol; + p->aCol = pSelTab->aCol; + pSelTab->nCol = 0; + pSelTab->aCol = 0; + sqlite3DeleteTable(db, pSelTab); + addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); + sqlite3TableAffinity(v, p, 0); + sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); + sqlite3VdbeGoto(v, addrInsLoop); + sqlite3VdbeJumpHere(v, addrInsLoop); + sqlite3VdbeAddOp1(v, OP_Close, 1); + } + + /* Compute the complete text of the CREATE statement */ + if( pSelect ){ + zStmt = createTableStmt(db, p); + }else{ + Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; + n = (int)(pEnd2->z - pParse->sNameToken.z); + if( pEnd2->z[0]!=';' ) n += pEnd2->n; + zStmt = sqlite3MPrintf(db, + "CREATE %s %.*s", zType2, n, pParse->sNameToken.z + ); + } + + /* A slot for the record has already been allocated in the + ** SQLITE_MASTER table. We just need to update that slot with all + ** the information we've collected. + */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s " + "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " + "WHERE rowid=#%d", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + zType, + p->zName, + p->zName, + pParse->regRoot, + zStmt, + pParse->regRowid + ); + sqlite3DbFree(db, zStmt); + sqlite3ChangeCookie(pParse, iDb); + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* Check to see if we need to create an sqlite_sequence table for + ** keeping track of autoincrement keys. + */ + if( p->tabFlags & TF_Autoincrement ){ + Db *pDb = &db->aDb[iDb]; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( pDb->pSchema->pSeqTab==0 ){ + sqlite3NestedParse(pParse, + "CREATE TABLE %Q.sqlite_sequence(name,seq)", + pDb->zName + ); + } + } +#endif + + /* Reparse everything to update our internal data structures */ + sqlite3VdbeAddParseSchemaOp(v, iDb, + sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); + } + + + /* Add the table to the in-memory representation of the database. + */ + if( db->init.busy ){ + Table *pOld; + Schema *pSchema = p->pSchema; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); + if( pOld ){ + assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ + sqlite3OomFault(db); + return; + } + pParse->pNewTable = 0; + db->flags |= SQLITE_InternChanges; + +#ifndef SQLITE_OMIT_ALTERTABLE + if( !p->pSelect ){ + const char *zName = (const char *)pParse->sNameToken.z; + int nName; + assert( !pSelect && pCons && pEnd ); + if( pCons->z==0 ){ + pCons = pEnd; + } + nName = (int)((const char *)pCons->z - zName); + p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); + } +#endif + } +} + +#ifndef SQLITE_OMIT_VIEW +/* +** The parser calls this routine in order to create a new VIEW +*/ +SQLITE_PRIVATE void sqlite3CreateView( + Parse *pParse, /* The parsing context */ + Token *pBegin, /* The CREATE token that begins the statement */ + Token *pName1, /* The token that holds the name of the view */ + Token *pName2, /* The token that holds the name of the view */ + ExprList *pCNames, /* Optional list of view column names */ + Select *pSelect, /* A SELECT statement that will become the new view */ + int isTemp, /* TRUE for a TEMPORARY view */ + int noErr /* Suppress error messages if VIEW already exists */ +){ + Table *p; + int n; + const char *z; + Token sEnd; + DbFixer sFix; + Token *pName = 0; + int iDb; + sqlite3 *db = pParse->db; + + if( pParse->nVar>0 ){ + sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); + goto create_view_fail; + } + sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); + p = pParse->pNewTable; + if( p==0 || pParse->nErr ) goto create_view_fail; + sqlite3TwoPartName(pParse, pName1, pName2, &pName); + iDb = sqlite3SchemaToIndex(db, p->pSchema); + sqlite3FixInit(&sFix, pParse, iDb, "view", pName); + if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; + + /* Make a copy of the entire SELECT statement that defines the view. + ** This will force all the Expr.token.z values to be dynamically + ** allocated rather than point to the input string - which means that + ** they will persist after the current sqlite3_exec() call returns. + */ + p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); + if( db->mallocFailed ) goto create_view_fail; + + /* Locate the end of the CREATE VIEW statement. Make sEnd point to + ** the end. + */ + sEnd = pParse->sLastToken; + assert( sEnd.z[0]!=0 ); + if( sEnd.z[0]!=';' ){ + sEnd.z += sEnd.n; + } + sEnd.n = 0; + n = (int)(sEnd.z - pBegin->z); + assert( n>0 ); + z = pBegin->z; + while( sqlite3Isspace(z[n-1]) ){ n--; } + sEnd.z = &z[n-1]; + sEnd.n = 1; + + /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ + sqlite3EndTable(pParse, 0, &sEnd, 0, 0); + +create_view_fail: + sqlite3SelectDelete(db, pSelect); + sqlite3ExprListDelete(db, pCNames); + return; +} +#endif /* SQLITE_OMIT_VIEW */ + +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) +/* +** The Table structure pTable is really a VIEW. Fill in the names of +** the columns of the view in the pTable structure. Return the number +** of errors. If an error is seen leave an error message in pParse->zErrMsg. +*/ +SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ + Table *pSelTab; /* A fake table from which we get the result set */ + Select *pSel; /* Copy of the SELECT that implements the view */ + int nErr = 0; /* Number of errors encountered */ + int n; /* Temporarily holds the number of cursors assigned */ + sqlite3 *db = pParse->db; /* Database connection for malloc errors */ + sqlite3_xauth xAuth; /* Saved xAuth pointer */ + + assert( pTable ); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3VtabCallConnect(pParse, pTable) ){ + return SQLITE_ERROR; + } + if( IsVirtual(pTable) ) return 0; +#endif + +#ifndef SQLITE_OMIT_VIEW + /* A positive nCol means the columns names for this view are + ** already known. + */ + if( pTable->nCol>0 ) return 0; + + /* A negative nCol is a special marker meaning that we are currently + ** trying to compute the column names. If we enter this routine with + ** a negative nCol, it means two or more views form a loop, like this: + ** + ** CREATE VIEW one AS SELECT * FROM two; + ** CREATE VIEW two AS SELECT * FROM one; + ** + ** Actually, the error above is now caught prior to reaching this point. + ** But the following test is still important as it does come up + ** in the following: + ** + ** CREATE TABLE main.ex1(a); + ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; + ** SELECT * FROM temp.ex1; + */ + if( pTable->nCol<0 ){ + sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); + return 1; + } + assert( pTable->nCol>=0 ); + + /* If we get this far, it means we need to compute the table names. + ** Note that the call to sqlite3ResultSetOfSelect() will expand any + ** "*" elements in the results set of the view and will assign cursors + ** to the elements of the FROM clause. But we do not want these changes + ** to be permanent. So the computation is done on a copy of the SELECT + ** statement that defines the view. + */ + assert( pTable->pSelect ); + pSel = sqlite3SelectDup(db, pTable->pSelect, 0); + if( pSel ){ + n = pParse->nTab; + sqlite3SrcListAssignCursors(pParse, pSel->pSrc); + pTable->nCol = -1; + db->lookaside.bDisable++; +#ifndef SQLITE_OMIT_AUTHORIZATION + xAuth = db->xAuth; + db->xAuth = 0; + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); + db->xAuth = xAuth; +#else + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); +#endif + pParse->nTab = n; + if( pTable->pCheck ){ + /* CREATE VIEW name(arglist) AS ... + ** The names of the columns in the table are taken from + ** arglist which is stored in pTable->pCheck. The pCheck field + ** normally holds CHECK constraints on an ordinary table, but for + ** a VIEW it holds the list of column names. + */ + sqlite3ColumnsFromExprList(pParse, pTable->pCheck, + &pTable->nCol, &pTable->aCol); + if( db->mallocFailed==0 + && pParse->nErr==0 + && pTable->nCol==pSel->pEList->nExpr + ){ + sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel); + } + }else if( pSelTab ){ + /* CREATE VIEW name AS... without an argument list. Construct + ** the column names from the SELECT statement that defines the view. + */ + assert( pTable->aCol==0 ); + pTable->nCol = pSelTab->nCol; + pTable->aCol = pSelTab->aCol; + pSelTab->nCol = 0; + pSelTab->aCol = 0; + assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); + }else{ + pTable->nCol = 0; + nErr++; + } + sqlite3DeleteTable(db, pSelTab); + sqlite3SelectDelete(db, pSel); + db->lookaside.bDisable--; + } else { + nErr++; + } + pTable->pSchema->schemaFlags |= DB_UnresetViews; +#endif /* SQLITE_OMIT_VIEW */ + return nErr; +} +#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ + +#ifndef SQLITE_OMIT_VIEW +/* +** Clear the column names from every VIEW in database idx. +*/ +static void sqliteViewResetAll(sqlite3 *db, int idx){ + HashElem *i; + assert( sqlite3SchemaMutexHeld(db, idx, 0) ); + if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; + for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ + Table *pTab = sqliteHashData(i); + if( pTab->pSelect ){ + sqlite3DeleteColumnNames(db, pTab); + pTab->aCol = 0; + pTab->nCol = 0; + } + } + DbClearProperty(db, idx, DB_UnresetViews); +} +#else +# define sqliteViewResetAll(A,B) +#endif /* SQLITE_OMIT_VIEW */ + +/* +** This function is called by the VDBE to adjust the internal schema +** used by SQLite when the btree layer moves a table root page. The +** root-page of a table or index in database iDb has changed from iFrom +** to iTo. +** +** Ticket #1728: The symbol table might still contain information +** on tables and/or indices that are the process of being deleted. +** If you are unlucky, one of those deleted indices or tables might +** have the same rootpage number as the real table or index that is +** being moved. So we cannot stop searching after the first match +** because the first match might be for one of the deleted indices +** or tables and not the table/index that is actually being moved. +** We must continue looping until all tables and indices with +** rootpage==iFrom have been converted to have a rootpage of iTo +** in order to be certain that we got the right one. +*/ +#ifndef SQLITE_OMIT_AUTOVACUUM +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ + HashElem *pElem; + Hash *pHash; + Db *pDb; + + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pDb = &db->aDb[iDb]; + pHash = &pDb->pSchema->tblHash; + for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ + Table *pTab = sqliteHashData(pElem); + if( pTab->tnum==iFrom ){ + pTab->tnum = iTo; + } + } + pHash = &pDb->pSchema->idxHash; + for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ + Index *pIdx = sqliteHashData(pElem); + if( pIdx->tnum==iFrom ){ + pIdx->tnum = iTo; + } + } +} +#endif + +/* +** Write code to erase the table with root-page iTable from database iDb. +** Also write code to modify the sqlite_master table and internal schema +** if a root-page of another table is moved by the btree-layer whilst +** erasing iTable (this can happen with an auto-vacuum database). +*/ +static void destroyRootPage(Parse *pParse, int iTable, int iDb){ + Vdbe *v = sqlite3GetVdbe(pParse); + int r1 = sqlite3GetTempReg(pParse); + assert( iTable>1 ); + sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); + sqlite3MayAbort(pParse); +#ifndef SQLITE_OMIT_AUTOVACUUM + /* OP_Destroy stores an in integer r1. If this integer + ** is non-zero, then it is the root page number of a table moved to + ** location iTable. The following code modifies the sqlite_master table to + ** reflect this. + ** + ** The "#NNN" in the SQL is a special constant that means whatever value + ** is in register NNN. See grammar rules associated with the TK_REGISTER + ** token for additional information. + */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", + pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1); +#endif + sqlite3ReleaseTempReg(pParse, r1); +} + +/* +** Write VDBE code to erase table pTab and all associated indices on disk. +** Code to update the sqlite_master tables and internal schema definitions +** in case a root-page belonging to another table is moved by the btree layer +** is also added (this can happen with an auto-vacuum database). +*/ +static void destroyTable(Parse *pParse, Table *pTab){ +#ifdef SQLITE_OMIT_AUTOVACUUM + Index *pIdx; + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + destroyRootPage(pParse, pTab->tnum, iDb); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + destroyRootPage(pParse, pIdx->tnum, iDb); + } +#else + /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM + ** is not defined), then it is important to call OP_Destroy on the + ** table and index root-pages in order, starting with the numerically + ** largest root-page number. This guarantees that none of the root-pages + ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the + ** following were coded: + ** + ** OP_Destroy 4 0 + ** ... + ** OP_Destroy 5 0 + ** + ** and root page 5 happened to be the largest root-page number in the + ** database, then root page 5 would be moved to page 4 by the + ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit + ** a free-list page. + */ + int iTab = pTab->tnum; + int iDestroyed = 0; + + while( 1 ){ + Index *pIdx; + int iLargest = 0; + + if( iDestroyed==0 || iTabpIndex; pIdx; pIdx=pIdx->pNext){ + int iIdx = pIdx->tnum; + assert( pIdx->pSchema==pTab->pSchema ); + if( (iDestroyed==0 || (iIdxiLargest ){ + iLargest = iIdx; + } + } + if( iLargest==0 ){ + return; + }else{ + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + assert( iDb>=0 && iDbdb->nDb ); + destroyRootPage(pParse, iLargest, iDb); + iDestroyed = iLargest; + } + } +#endif +} + +/* +** Remove entries from the sqlite_statN tables (for N in (1,2,3)) +** after a DROP INDEX or DROP TABLE command. +*/ +static void sqlite3ClearStatTables( + Parse *pParse, /* The parsing context */ + int iDb, /* The database number */ + const char *zType, /* "idx" or "tbl" */ + const char *zName /* Name of index or table */ +){ + int i; + const char *zDbName = pParse->db->aDb[iDb].zName; + for(i=1; i<=4; i++){ + char zTab[24]; + sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); + if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE %s=%Q", + zDbName, zTab, zType, zName + ); + } + } +} + +/* +** Generate code to drop a table. +*/ +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ + Vdbe *v; + sqlite3 *db = pParse->db; + Trigger *pTrigger; + Db *pDb = &db->aDb[iDb]; + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + sqlite3BeginWriteOperation(pParse, 1, iDb); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp0(v, OP_VBegin); + } +#endif + + /* Drop all triggers associated with the table being dropped. Code + ** is generated to remove entries from sqlite_master and/or + ** sqlite_temp_master if required. + */ + pTrigger = sqlite3TriggerList(pParse, pTab); + while( pTrigger ){ + assert( pTrigger->pSchema==pTab->pSchema || + pTrigger->pSchema==db->aDb[1].pSchema ); + sqlite3DropTriggerPtr(pParse, pTrigger); + pTrigger = pTrigger->pNext; + } + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* Remove any entries of the sqlite_sequence table associated with + ** the table being dropped. This is done before the table is dropped + ** at the btree level, in case the sqlite_sequence table needs to + ** move as a result of the drop (can happen in auto-vacuum mode). + */ + if( pTab->tabFlags & TF_Autoincrement ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", + pDb->zName, pTab->zName + ); + } +#endif + + /* Drop all SQLITE_MASTER table and index entries that refer to the + ** table. The program name loops through the master table and deletes + ** every row that refers to a table of the same name as the one being + ** dropped. Triggers are handled separately because a trigger can be + ** created in the temp database that refers to a table in another + ** database. + */ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); + if( !isView && !IsVirtual(pTab) ){ + destroyTable(pParse, pTab); + } + + /* Remove the table entry from SQLite's internal schema and modify + ** the schema cookie. + */ + if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); + } + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + sqlite3ChangeCookie(pParse, iDb); + sqliteViewResetAll(db, iDb); +} + +/* +** This routine is called to do the work of a DROP TABLE statement. +** pName is the name of the table to be dropped. +*/ +SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ + Table *pTab; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + if( db->mallocFailed ){ + goto exit_drop_table; + } + assert( pParse->nErr==0 ); + assert( pName->nSrc==1 ); + if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; + if( noErr ) db->suppressErr++; + pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); + if( noErr ) db->suppressErr--; + + if( pTab==0 ){ + if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); + goto exit_drop_table; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 && iDbnDb ); + + /* If pTab is a virtual table, call ViewGetColumnNames() to ensure + ** it is initialized. + */ + if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto exit_drop_table; + } +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code; + const char *zTab = SCHEMA_TABLE(iDb); + const char *zDb = db->aDb[iDb].zName; + const char *zArg2 = 0; + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ + goto exit_drop_table; + } + if( isView ){ + if( !OMIT_TEMPDB && iDb==1 ){ + code = SQLITE_DROP_TEMP_VIEW; + }else{ + code = SQLITE_DROP_VIEW; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + }else if( IsVirtual(pTab) ){ + code = SQLITE_DROP_VTABLE; + zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; +#endif + }else{ + if( !OMIT_TEMPDB && iDb==1 ){ + code = SQLITE_DROP_TEMP_TABLE; + }else{ + code = SQLITE_DROP_TABLE; + } + } + if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ + goto exit_drop_table; + } + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ + goto exit_drop_table; + } + } +#endif + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 + && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ + sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); + goto exit_drop_table; + } + +#ifndef SQLITE_OMIT_VIEW + /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used + ** on a table. + */ + if( isView && pTab->pSelect==0 ){ + sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); + goto exit_drop_table; + } + if( !isView && pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); + goto exit_drop_table; + } +#endif + + /* Generate code to remove the table from the master table + ** on disk. + */ + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); + sqlite3FkDropTable(pParse, pName, pTab); + sqlite3CodeDropTable(pParse, pTab, iDb, isView); + } + +exit_drop_table: + sqlite3SrcListDelete(db, pName); +} + +/* +** This routine is called to create a new foreign key on the table +** currently under construction. pFromCol determines which columns +** in the current table point to the foreign key. If pFromCol==0 then +** connect the key to the last column inserted. pTo is the name of +** the table referred to (a.k.a the "parent" table). pToCol is a list +** of tables in the parent pTo table. flags contains all +** information about the conflict resolution algorithms specified +** in the ON DELETE, ON UPDATE and ON INSERT clauses. +** +** An FKey structure is created and added to the table currently +** under construction in the pParse->pNewTable field. +** +** The foreign key is set for IMMEDIATE processing. A subsequent call +** to sqlite3DeferForeignKey() might change this to DEFERRED. +*/ +SQLITE_PRIVATE void sqlite3CreateForeignKey( + Parse *pParse, /* Parsing context */ + ExprList *pFromCol, /* Columns in this table that point to other table */ + Token *pTo, /* Name of the other table */ + ExprList *pToCol, /* Columns in the other table */ + int flags /* Conflict resolution algorithms. */ +){ + sqlite3 *db = pParse->db; +#ifndef SQLITE_OMIT_FOREIGN_KEY + FKey *pFKey = 0; + FKey *pNextTo; + Table *p = pParse->pNewTable; + int nByte; + int i; + int nCol; + char *z; + + assert( pTo!=0 ); + if( p==0 || IN_DECLARE_VTAB ) goto fk_end; + if( pFromCol==0 ){ + int iCol = p->nCol-1; + if( NEVER(iCol<0) ) goto fk_end; + if( pToCol && pToCol->nExpr!=1 ){ + sqlite3ErrorMsg(pParse, "foreign key on %s" + " should reference only one column of table %T", + p->aCol[iCol].zName, pTo); + goto fk_end; + } + nCol = 1; + }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ + sqlite3ErrorMsg(pParse, + "number of columns in foreign key does not match the number of " + "columns in the referenced table"); + goto fk_end; + }else{ + nCol = pFromCol->nExpr; + } + nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; + if( pToCol ){ + for(i=0; inExpr; i++){ + nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; + } + } + pFKey = sqlite3DbMallocZero(db, nByte ); + if( pFKey==0 ){ + goto fk_end; + } + pFKey->pFrom = p; + pFKey->pNextFrom = p->pFKey; + z = (char*)&pFKey->aCol[nCol]; + pFKey->zTo = z; + memcpy(z, pTo->z, pTo->n); + z[pTo->n] = 0; + sqlite3Dequote(z); + z += pTo->n+1; + pFKey->nCol = nCol; + if( pFromCol==0 ){ + pFKey->aCol[0].iFrom = p->nCol-1; + }else{ + for(i=0; inCol; j++){ + if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ + pFKey->aCol[i].iFrom = j; + break; + } + } + if( j>=p->nCol ){ + sqlite3ErrorMsg(pParse, + "unknown column \"%s\" in foreign key definition", + pFromCol->a[i].zName); + goto fk_end; + } + } + } + if( pToCol ){ + for(i=0; ia[i].zName); + pFKey->aCol[i].zCol = z; + memcpy(z, pToCol->a[i].zName, n); + z[n] = 0; + z += n+1; + } + } + pFKey->isDeferred = 0; + pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ + pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ + + assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); + pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, + pFKey->zTo, (void *)pFKey + ); + if( pNextTo==pFKey ){ + sqlite3OomFault(db); + goto fk_end; + } + if( pNextTo ){ + assert( pNextTo->pPrevTo==0 ); + pFKey->pNextTo = pNextTo; + pNextTo->pPrevTo = pFKey; + } + + /* Link the foreign key to the table as the last step. + */ + p->pFKey = pFKey; + pFKey = 0; + +fk_end: + sqlite3DbFree(db, pFKey); +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ + sqlite3ExprListDelete(db, pFromCol); + sqlite3ExprListDelete(db, pToCol); +} + +/* +** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED +** clause is seen as part of a foreign key definition. The isDeferred +** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. +** The behavior of the most recently created foreign key is adjusted +** accordingly. +*/ +SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ +#ifndef SQLITE_OMIT_FOREIGN_KEY + Table *pTab; + FKey *pFKey; + if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; + assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ + pFKey->isDeferred = (u8)isDeferred; +#endif +} + +/* +** Generate code that will erase and refill index *pIdx. This is +** used to initialize a newly created index or to recompute the +** content of an index in response to a REINDEX command. +** +** if memRootPage is not negative, it means that the index is newly +** created. The register specified by memRootPage contains the +** root page number of the index. If memRootPage is negative, then +** the index already exists and must be cleared before being refilled and +** the root page number of the index is taken from pIndex->tnum. +*/ +static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ + Table *pTab = pIndex->pTable; /* The table that is indexed */ + int iTab = pParse->nTab++; /* Btree cursor used for pTab */ + int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ + int iSorter; /* Cursor opened by OpenSorter (if in use) */ + int addr1; /* Address of top of loop */ + int addr2; /* Address to jump to for next iteration */ + int tnum; /* Root page of index */ + int iPartIdxLabel; /* Jump to this label to skip a row */ + Vdbe *v; /* Generate code into this virtual machine */ + KeyInfo *pKey; /* KeyInfo for index */ + int regRecord; /* Register holding assembled index record */ + sqlite3 *db = pParse->db; /* The database connection */ + int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); + +#ifndef SQLITE_OMIT_AUTHORIZATION + if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, + db->aDb[iDb].zName ) ){ + return; + } +#endif + + /* Require a write-lock on the table to perform this operation */ + sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); + + v = sqlite3GetVdbe(pParse); + if( v==0 ) return; + if( memRootPage>=0 ){ + tnum = memRootPage; + }else{ + tnum = pIndex->tnum; + } + pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); + assert( pKey!=0 || db->mallocFailed || pParse->nErr ); + + /* Open the sorter cursor if we are to use one. */ + iSorter = pParse->nTab++; + sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) + sqlite3KeyInfoRef(pKey), P4_KEYINFO); + + /* Open the table. Loop through all rows of the table, inserting index + ** records into the sorter. */ + sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); + regRecord = sqlite3GetTempReg(pParse); + + sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); + sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); + sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr1); + if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, + (char *)pKey, P4_KEYINFO); + sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); + + addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); + if( IsUniqueIndex(pIndex) ){ + int j2 = sqlite3VdbeCurrentAddr(v) + 3; + sqlite3VdbeGoto(v, j2); + addr2 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, + pIndex->nKeyCol); VdbeCoverage(v); + sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); + }else{ + addr2 = sqlite3VdbeCurrentAddr(v); + } + sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); + sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); + sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr1); + + sqlite3VdbeAddOp1(v, OP_Close, iTab); + sqlite3VdbeAddOp1(v, OP_Close, iIdx); + sqlite3VdbeAddOp1(v, OP_Close, iSorter); +} + +/* +** Allocate heap space to hold an Index object with nCol columns. +** +** Increase the allocation size to provide an extra nExtra bytes +** of 8-byte aligned space after the Index object and return a +** pointer to this extra space in *ppExtra. +*/ +SQLITE_PRIVATE Index *sqlite3AllocateIndexObject( + sqlite3 *db, /* Database connection */ + i16 nCol, /* Total number of columns in the index */ + int nExtra, /* Number of bytes of extra space to alloc */ + char **ppExtra /* Pointer to the "extra" space */ +){ + Index *p; /* Allocated index object */ + int nByte; /* Bytes of space for Index object + arrays */ + + nByte = ROUND8(sizeof(Index)) + /* Index structure */ + ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ + ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ + sizeof(i16)*nCol + /* Index.aiColumn */ + sizeof(u8)*nCol); /* Index.aSortOrder */ + p = sqlite3DbMallocZero(db, nByte + nExtra); + if( p ){ + char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); + p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); + p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); + p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; + p->aSortOrder = (u8*)pExtra; + p->nColumn = nCol; + p->nKeyCol = nCol - 1; + *ppExtra = ((char*)p) + nByte; + } + return p; +} + +/* +** Create a new index for an SQL table. pName1.pName2 is the name of the index +** and pTblList is the name of the table that is to be indexed. Both will +** be NULL for a primary key or an index that is created to satisfy a +** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable +** as the table to be indexed. pParse->pNewTable is a table that is +** currently being constructed by a CREATE TABLE statement. +** +** pList is a list of columns to be indexed. pList will be NULL if this +** is a primary key or unique-constraint on the most recent column added +** to the table currently under construction. +** +** If the index is created successfully, return a pointer to the new Index +** structure. This is used by sqlite3AddPrimaryKey() to mark the index +** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY) +*/ +SQLITE_PRIVATE Index *sqlite3CreateIndex( + Parse *pParse, /* All information about this parse */ + Token *pName1, /* First part of index name. May be NULL */ + Token *pName2, /* Second part of index name. May be NULL */ + SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ + ExprList *pList, /* A list of columns to be indexed */ + int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ + Token *pStart, /* The CREATE token that begins this statement */ + Expr *pPIWhere, /* WHERE clause for partial indices */ + int sortOrder, /* Sort order of primary key when pList==NULL */ + int ifNotExist /* Omit error if index already exists */ +){ + Index *pRet = 0; /* Pointer to return */ + Table *pTab = 0; /* Table to be indexed */ + Index *pIndex = 0; /* The index to be created */ + char *zName = 0; /* Name of the index */ + int nName; /* Number of characters in zName */ + int i, j; + DbFixer sFix; /* For assigning database names to pTable */ + int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ + sqlite3 *db = pParse->db; + Db *pDb; /* The specific table containing the indexed database */ + int iDb; /* Index of the database that is being written */ + Token *pName = 0; /* Unqualified name of the index to create */ + struct ExprList_item *pListItem; /* For looping over pList */ + int nExtra = 0; /* Space allocated for zExtra[] */ + int nExtraCol; /* Number of extra columns needed */ + char *zExtra = 0; /* Extra space after the Index object */ + Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ + + if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){ + goto exit_create_index; + } + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto exit_create_index; + } + + /* + ** Find the table that is to be indexed. Return early if not found. + */ + if( pTblName!=0 ){ + + /* Use the two-part index name to determine the database + ** to search for the table. 'Fix' the table name to this db + ** before looking up the table. + */ + assert( pName1 && pName2 ); + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ) goto exit_create_index; + assert( pName && pName->z ); + +#ifndef SQLITE_OMIT_TEMPDB + /* If the index name was unqualified, check if the table + ** is a temp table. If so, set the database to 1. Do not do this + ** if initialising a database schema. + */ + if( !db->init.busy ){ + pTab = sqlite3SrcListLookup(pParse, pTblName); + if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ + iDb = 1; + } + } +#endif + + sqlite3FixInit(&sFix, pParse, iDb, "index", pName); + if( sqlite3FixSrcList(&sFix, pTblName) ){ + /* Because the parser constructs pTblName from a single identifier, + ** sqlite3FixSrcList can never fail. */ + assert(0); + } + pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); + assert( db->mallocFailed==0 || pTab==0 ); + if( pTab==0 ) goto exit_create_index; + if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ + sqlite3ErrorMsg(pParse, + "cannot create a TEMP index on non-TEMP table \"%s\"", + pTab->zName); + goto exit_create_index; + } + if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); + }else{ + assert( pName==0 ); + assert( pStart==0 ); + pTab = pParse->pNewTable; + if( !pTab ) goto exit_create_index; + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + } + pDb = &db->aDb[iDb]; + + assert( pTab!=0 ); + assert( pParse->nErr==0 ); + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 + && db->init.busy==0 +#if SQLITE_USER_AUTHENTICATION + && sqlite3UserAuthTable(pTab->zName)==0 +#endif + && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){ + sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); + goto exit_create_index; + } +#ifndef SQLITE_OMIT_VIEW + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "views may not be indexed"); + goto exit_create_index; + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); + goto exit_create_index; + } +#endif + + /* + ** Find the name of the index. Make sure there is not already another + ** index or table with the same name. + ** + ** Exception: If we are reading the names of permanent indices from the + ** sqlite_master table (because some other process changed the schema) and + ** one of the index names collides with the name of a temporary table or + ** index, then we will continue to process this index. + ** + ** If pName==0 it means that we are + ** dealing with a primary key or UNIQUE constraint. We have to invent our + ** own name. + */ + if( pName ){ + zName = sqlite3NameFromToken(db, pName); + if( zName==0 ) goto exit_create_index; + assert( pName->z!=0 ); + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto exit_create_index; + } + if( !db->init.busy ){ + if( sqlite3FindTable(db, zName, 0)!=0 ){ + sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); + goto exit_create_index; + } + } + if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ + if( !ifNotExist ){ + sqlite3ErrorMsg(pParse, "index %s already exists", zName); + }else{ + assert( !db->init.busy ); + sqlite3CodeVerifySchema(pParse, iDb); + } + goto exit_create_index; + } + }else{ + int n; + Index *pLoop; + for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} + zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); + if( zName==0 ){ + goto exit_create_index; + } + } + + /* Check for authorization to create an index. + */ +#ifndef SQLITE_OMIT_AUTHORIZATION + { + const char *zDb = pDb->zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ + goto exit_create_index; + } + i = SQLITE_CREATE_INDEX; + if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; + if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ + goto exit_create_index; + } + } +#endif + + /* If pList==0, it means this routine was called to make a primary + ** key out of the last column added to the table under construction. + ** So create a fake list to simulate this. + */ + if( pList==0 ){ + Token prevCol; + sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName); + pList = sqlite3ExprListAppend(pParse, 0, + sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); + if( pList==0 ) goto exit_create_index; + assert( pList->nExpr==1 ); + sqlite3ExprListSetSortOrder(pList, sortOrder); + }else{ + sqlite3ExprListCheckLength(pParse, pList, "index"); + } + + /* Figure out how many bytes of space are required to store explicitly + ** specified collation sequence names. + */ + for(i=0; inExpr; i++){ + Expr *pExpr = pList->a[i].pExpr; + assert( pExpr!=0 ); + if( pExpr->op==TK_COLLATE ){ + nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); + } + } + + /* + ** Allocate the index structure. + */ + nName = sqlite3Strlen30(zName); + nExtraCol = pPk ? pPk->nKeyCol : 1; + pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, + nName + nExtra + 1, &zExtra); + if( db->mallocFailed ){ + goto exit_create_index; + } + assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); + assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); + pIndex->zName = zExtra; + zExtra += nName + 1; + memcpy(pIndex->zName, zName, nName+1); + pIndex->pTable = pTab; + pIndex->onError = (u8)onError; + pIndex->uniqNotNull = onError!=OE_None; + pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE; + pIndex->pSchema = db->aDb[iDb].pSchema; + pIndex->nKeyCol = pList->nExpr; + if( pPIWhere ){ + sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); + pIndex->pPartIdxWhere = pPIWhere; + pPIWhere = 0; + } + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + + /* Check to see if we should honor DESC requests on index columns + */ + if( pDb->pSchema->file_format>=4 ){ + sortOrderMask = -1; /* Honor DESC */ + }else{ + sortOrderMask = 0; /* Ignore DESC */ + } + + /* Analyze the list of expressions that form the terms of the index and + ** report any errors. In the common case where the expression is exactly + ** a table column, store that column in aiColumn[]. For general expressions, + ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. + ** + ** TODO: Issue a warning if two or more columns of the index are identical. + ** TODO: Issue a warning if the table primary key is used as part of the + ** index key. + */ + for(i=0, pListItem=pList->a; inExpr; i++, pListItem++){ + Expr *pCExpr; /* The i-th index expression */ + int requestedSortOrder; /* ASC or DESC on the i-th expression */ + const char *zColl; /* Collation sequence name */ + + sqlite3StringToId(pListItem->pExpr); + sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); + if( pParse->nErr ) goto exit_create_index; + pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); + if( pCExpr->op!=TK_COLUMN ){ + if( pTab==pParse->pNewTable ){ + sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " + "UNIQUE constraints"); + goto exit_create_index; + } + if( pIndex->aColExpr==0 ){ + ExprList *pCopy = sqlite3ExprListDup(db, pList, 0); + pIndex->aColExpr = pCopy; + if( !db->mallocFailed ){ + assert( pCopy!=0 ); + pListItem = &pCopy->a[i]; + } + } + j = XN_EXPR; + pIndex->aiColumn[i] = XN_EXPR; + pIndex->uniqNotNull = 0; + }else{ + j = pCExpr->iColumn; + assert( j<=0x7fff ); + if( j<0 ){ + j = pTab->iPKey; + }else if( pTab->aCol[j].notNull==0 ){ + pIndex->uniqNotNull = 0; + } + pIndex->aiColumn[i] = (i16)j; + } + zColl = 0; + if( pListItem->pExpr->op==TK_COLLATE ){ + int nColl; + zColl = pListItem->pExpr->u.zToken; + nColl = sqlite3Strlen30(zColl) + 1; + assert( nExtra>=nColl ); + memcpy(zExtra, zColl, nColl); + zColl = zExtra; + zExtra += nColl; + nExtra -= nColl; + }else if( j>=0 ){ + zColl = pTab->aCol[j].zColl; + } + if( !zColl ) zColl = sqlite3StrBINARY; + if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ + goto exit_create_index; + } + pIndex->azColl[i] = zColl; + requestedSortOrder = pListItem->sortOrder & sortOrderMask; + pIndex->aSortOrder[i] = (u8)requestedSortOrder; + } + + /* Append the table key to the end of the index. For WITHOUT ROWID + ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For + ** normal tables (when pPk==0) this will be the rowid. + */ + if( pPk ){ + for(j=0; jnKeyCol; j++){ + int x = pPk->aiColumn[j]; + assert( x>=0 ); + if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ + pIndex->nColumn--; + }else{ + pIndex->aiColumn[i] = x; + pIndex->azColl[i] = pPk->azColl[j]; + pIndex->aSortOrder[i] = pPk->aSortOrder[j]; + i++; + } + } + assert( i==pIndex->nColumn ); + }else{ + pIndex->aiColumn[i] = XN_ROWID; + pIndex->azColl[i] = sqlite3StrBINARY; + } + sqlite3DefaultRowEst(pIndex); + if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); + + /* If this index contains every column of its table, then mark + ** it as a covering index */ + assert( HasRowid(pTab) + || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 ); + if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ + pIndex->isCovering = 1; + for(j=0; jnCol; j++){ + if( j==pTab->iPKey ) continue; + if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue; + pIndex->isCovering = 0; + break; + } + } + + if( pTab==pParse->pNewTable ){ + /* This routine has been called to create an automatic index as a + ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or + ** a PRIMARY KEY or UNIQUE clause following the column definitions. + ** i.e. one of: + ** + ** CREATE TABLE t(x PRIMARY KEY, y); + ** CREATE TABLE t(x, y, UNIQUE(x, y)); + ** + ** Either way, check to see if the table already has such an index. If + ** so, don't bother creating this one. This only applies to + ** automatically created indices. Users can do as they wish with + ** explicit indices. + ** + ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent + ** (and thus suppressing the second one) even if they have different + ** sort orders. + ** + ** If there are different collating sequences or if the columns of + ** the constraint occur in different orders, then the constraints are + ** considered distinct and both result in separate indices. + */ + Index *pIdx; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int k; + assert( IsUniqueIndex(pIdx) ); + assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); + assert( IsUniqueIndex(pIndex) ); + + if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; + for(k=0; knKeyCol; k++){ + const char *z1; + const char *z2; + assert( pIdx->aiColumn[k]>=0 ); + if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; + z1 = pIdx->azColl[k]; + z2 = pIndex->azColl[k]; + if( sqlite3StrICmp(z1, z2) ) break; + } + if( k==pIdx->nKeyCol ){ + if( pIdx->onError!=pIndex->onError ){ + /* This constraint creates the same index as a previous + ** constraint specified somewhere in the CREATE TABLE statement. + ** However the ON CONFLICT clauses are different. If both this + ** constraint and the previous equivalent constraint have explicit + ** ON CONFLICT clauses this is an error. Otherwise, use the + ** explicitly specified behavior for the index. + */ + if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ + sqlite3ErrorMsg(pParse, + "conflicting ON CONFLICT clauses specified", 0); + } + if( pIdx->onError==OE_Default ){ + pIdx->onError = pIndex->onError; + } + } + pRet = pIdx; + goto exit_create_index; + } + } + } + + /* Link the new Index structure to its table and to the other + ** in-memory database structures. + */ + assert( pParse->nErr==0 ); + if( db->init.busy ){ + Index *p; + assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); + p = sqlite3HashInsert(&pIndex->pSchema->idxHash, + pIndex->zName, pIndex); + if( p ){ + assert( p==pIndex ); /* Malloc must have failed */ + sqlite3OomFault(db); + goto exit_create_index; + } + db->flags |= SQLITE_InternChanges; + if( pTblName!=0 ){ + pIndex->tnum = db->init.newTnum; + } + } + + /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the + ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then + ** emit code to allocate the index rootpage on disk and make an entry for + ** the index in the sqlite_master table and populate the index with + ** content. But, do not do this if we are simply reading the sqlite_master + ** table to parse the schema, or if this index is the PRIMARY KEY index + ** of a WITHOUT ROWID table. + ** + ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY + ** or UNIQUE index in a CREATE TABLE statement. Since the table + ** has just been created, it contains no data and the index initialization + ** step can be skipped. + */ + else if( HasRowid(pTab) || pTblName!=0 ){ + Vdbe *v; + char *zStmt; + int iMem = ++pParse->nMem; + + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto exit_create_index; + + sqlite3BeginWriteOperation(pParse, 1, iDb); + + /* Create the rootpage for the index using CreateIndex. But before + ** doing so, code a Noop instruction and store its address in + ** Index.tnum. This is required in case this index is actually a + ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In + ** that case the convertToWithoutRowidTable() routine will replace + ** the Noop with a Goto to jump over the VDBE code generated below. */ + pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); + sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); + + /* Gather the complete text of the CREATE INDEX statement into + ** the zStmt variable + */ + if( pStart ){ + int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; + if( pName->z[n-1]==';' ) n--; + /* A named index with an explicit CREATE INDEX statement */ + zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", + onError==OE_None ? "" : " UNIQUE", n, pName->z); + }else{ + /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ + /* zStmt = sqlite3MPrintf(""); */ + zStmt = 0; + } + + /* Add an entry in sqlite_master for this index + */ + sqlite3NestedParse(pParse, + "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + pIndex->zName, + pTab->zName, + iMem, + zStmt + ); + sqlite3DbFree(db, zStmt); + + /* Fill the index with data and reparse the schema. Code an OP_Expire + ** to invalidate all pre-compiled statements. + */ + if( pTblName ){ + sqlite3RefillIndex(pParse, pIndex, iMem); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddParseSchemaOp(v, iDb, + sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); + sqlite3VdbeAddOp1(v, OP_Expire, 0); + } + + sqlite3VdbeJumpHere(v, pIndex->tnum); + } + + /* When adding an index to the list of indices for a table, make + ** sure all indices labeled OE_Replace come after all those labeled + ** OE_Ignore. This is necessary for the correct constraint check + ** processing (in sqlite3GenerateConstraintChecks()) as part of + ** UPDATE and INSERT statements. + */ + if( db->init.busy || pTblName==0 ){ + if( onError!=OE_Replace || pTab->pIndex==0 + || pTab->pIndex->onError==OE_Replace){ + pIndex->pNext = pTab->pIndex; + pTab->pIndex = pIndex; + }else{ + Index *pOther = pTab->pIndex; + while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ + pOther = pOther->pNext; + } + pIndex->pNext = pOther->pNext; + pOther->pNext = pIndex; + } + pRet = pIndex; + pIndex = 0; + } + + /* Clean up before exiting */ +exit_create_index: + if( pIndex ) freeIndex(db, pIndex); + sqlite3ExprDelete(db, pPIWhere); + sqlite3ExprListDelete(db, pList); + sqlite3SrcListDelete(db, pTblName); + sqlite3DbFree(db, zName); + return pRet; +} + +/* +** Fill the Index.aiRowEst[] array with default information - information +** to be used when we have not run the ANALYZE command. +** +** aiRowEst[0] is supposed to contain the number of elements in the index. +** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the +** number of rows in the table that match any particular value of the +** first column of the index. aiRowEst[2] is an estimate of the number +** of rows that match any particular combination of the first 2 columns +** of the index. And so forth. It must always be the case that +* +** aiRowEst[N]<=aiRowEst[N-1] +** aiRowEst[N]>=1 +** +** Apart from that, we have little to go on besides intuition as to +** how aiRowEst[] should be initialized. The numbers generated here +** are based on typical values found in actual indices. +*/ +SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ + /* 10, 9, 8, 7, 6 */ + LogEst aVal[] = { 33, 32, 30, 28, 26 }; + LogEst *a = pIdx->aiRowLogEst; + int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); + int i; + + /* Set the first entry (number of rows in the index) to the estimated + ** number of rows in the table. Or 10, if the estimated number of rows + ** in the table is less than that. */ + a[0] = pIdx->pTable->nRowLogEst; + if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); + + /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is + ** 6 and each subsequent value (if any) is 5. */ + memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); + for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ + a[i] = 23; assert( 23==sqlite3LogEst(5) ); + } + + assert( 0==sqlite3LogEst(1) ); + if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; +} + +/* +** This routine will drop an existing named index. This routine +** implements the DROP INDEX statement. +*/ +SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ + Index *pIndex; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + assert( pParse->nErr==0 ); /* Never called with prior errors */ + if( db->mallocFailed ){ + goto exit_drop_index; + } + assert( pName->nSrc==1 ); + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto exit_drop_index; + } + pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); + if( pIndex==0 ){ + if( !ifExists ){ + sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); + }else{ + sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); + } + pParse->checkSchema = 1; + goto exit_drop_index; + } + if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ + sqlite3ErrorMsg(pParse, "index associated with UNIQUE " + "or PRIMARY KEY constraint cannot be dropped", 0); + goto exit_drop_index; + } + iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_DROP_INDEX; + Table *pTab = pIndex->pTable; + const char *zDb = db->aDb[iDb].zName; + const char *zTab = SCHEMA_TABLE(iDb); + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ + goto exit_drop_index; + } + if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; + if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ + goto exit_drop_index; + } + } +#endif + + /* Generate code to remove the index and from the master table */ + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName + ); + sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); + sqlite3ChangeCookie(pParse, iDb); + destroyRootPage(pParse, pIndex->tnum, iDb); + sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); + } + +exit_drop_index: + sqlite3SrcListDelete(db, pName); +} + +/* +** pArray is a pointer to an array of objects. Each object in the +** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() +** to extend the array so that there is space for a new object at the end. +** +** When this function is called, *pnEntry contains the current size of +** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes +** in total). +** +** If the realloc() is successful (i.e. if no OOM condition occurs), the +** space allocated for the new object is zeroed, *pnEntry updated to +** reflect the new size of the array and a pointer to the new allocation +** returned. *pIdx is set to the index of the new array entry in this case. +** +** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains +** unchanged and a copy of pArray returned. +*/ +SQLITE_PRIVATE void *sqlite3ArrayAllocate( + sqlite3 *db, /* Connection to notify of malloc failures */ + void *pArray, /* Array of objects. Might be reallocated */ + int szEntry, /* Size of each object in the array */ + int *pnEntry, /* Number of objects currently in use */ + int *pIdx /* Write the index of a new slot here */ +){ + char *z; + int n = *pnEntry; + if( (n & (n-1))==0 ){ + int sz = (n==0) ? 1 : 2*n; + void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); + if( pNew==0 ){ + *pIdx = -1; + return pArray; + } + pArray = pNew; + } + z = (char*)pArray; + memset(&z[n * szEntry], 0, szEntry); + *pIdx = n; + ++*pnEntry; + return pArray; +} + +/* +** Append a new element to the given IdList. Create a new IdList if +** need be. +** +** A new IdList is returned, or NULL if malloc() fails. +*/ +SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ + int i; + if( pList==0 ){ + pList = sqlite3DbMallocZero(db, sizeof(IdList) ); + if( pList==0 ) return 0; + } + pList->a = sqlite3ArrayAllocate( + db, + pList->a, + sizeof(pList->a[0]), + &pList->nId, + &i + ); + if( i<0 ){ + sqlite3IdListDelete(db, pList); + return 0; + } + pList->a[i].zName = sqlite3NameFromToken(db, pToken); + return pList; +} + +/* +** Delete an IdList. +*/ +SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ + int i; + if( pList==0 ) return; + for(i=0; inId; i++){ + sqlite3DbFree(db, pList->a[i].zName); + } + sqlite3DbFree(db, pList->a); + sqlite3DbFree(db, pList); +} + +/* +** Return the index in pList of the identifier named zId. Return -1 +** if not found. +*/ +SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){ + int i; + if( pList==0 ) return -1; + for(i=0; inId; i++){ + if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; + } + return -1; +} + +/* +** Expand the space allocated for the given SrcList object by +** creating nExtra new slots beginning at iStart. iStart is zero based. +** New slots are zeroed. +** +** For example, suppose a SrcList initially contains two entries: A,B. +** To append 3 new entries onto the end, do this: +** +** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); +** +** After the call above it would contain: A, B, nil, nil, nil. +** If the iStart argument had been 1 instead of 2, then the result +** would have been: A, nil, nil, nil, B. To prepend the new slots, +** the iStart value would be 0. The result then would +** be: nil, nil, nil, A, B. +** +** If a memory allocation fails the SrcList is unchanged. The +** db->mallocFailed flag will be set to true. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge( + sqlite3 *db, /* Database connection to notify of OOM errors */ + SrcList *pSrc, /* The SrcList to be enlarged */ + int nExtra, /* Number of new slots to add to pSrc->a[] */ + int iStart /* Index in pSrc->a[] of first new slot */ +){ + int i; + + /* Sanity checking on calling parameters */ + assert( iStart>=0 ); + assert( nExtra>=1 ); + assert( pSrc!=0 ); + assert( iStart<=pSrc->nSrc ); + + /* Allocate additional space if needed */ + if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ + SrcList *pNew; + int nAlloc = pSrc->nSrc+nExtra; + int nGot; + pNew = sqlite3DbRealloc(db, pSrc, + sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); + if( pNew==0 ){ + assert( db->mallocFailed ); + return pSrc; + } + pSrc = pNew; + nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; + pSrc->nAlloc = nGot; + } + + /* Move existing slots that come after the newly inserted slots + ** out of the way */ + for(i=pSrc->nSrc-1; i>=iStart; i--){ + pSrc->a[i+nExtra] = pSrc->a[i]; + } + pSrc->nSrc += nExtra; + + /* Zero the newly allocated slots */ + memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); + for(i=iStart; ia[i].iCursor = -1; + } + + /* Return a pointer to the enlarged SrcList */ + return pSrc; +} + + +/* +** Append a new table name to the given SrcList. Create a new SrcList if +** need be. A new entry is created in the SrcList even if pTable is NULL. +** +** A SrcList is returned, or NULL if there is an OOM error. The returned +** SrcList might be the same as the SrcList that was input or it might be +** a new one. If an OOM error does occurs, then the prior value of pList +** that is input to this routine is automatically freed. +** +** If pDatabase is not null, it means that the table has an optional +** database name prefix. Like this: "database.table". The pDatabase +** points to the table name and the pTable points to the database name. +** The SrcList.a[].zName field is filled with the table name which might +** come from pTable (if pDatabase is NULL) or from pDatabase. +** SrcList.a[].zDatabase is filled with the database name from pTable, +** or with NULL if no database is specified. +** +** In other words, if call like this: +** +** sqlite3SrcListAppend(D,A,B,0); +** +** Then B is a table name and the database name is unspecified. If called +** like this: +** +** sqlite3SrcListAppend(D,A,B,C); +** +** Then C is the table name and B is the database name. If C is defined +** then so is B. In other words, we never have a case where: +** +** sqlite3SrcListAppend(D,A,0,C); +** +** Both pTable and pDatabase are assumed to be quoted. They are dequoted +** before being added to the SrcList. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListAppend( + sqlite3 *db, /* Connection to notify of malloc failures */ + SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ + Token *pTable, /* Table to append */ + Token *pDatabase /* Database of the table */ +){ + struct SrcList_item *pItem; + assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ + assert( db!=0 ); + if( pList==0 ){ + pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) ); + if( pList==0 ) return 0; + pList->nAlloc = 1; + pList->nSrc = 0; + } + pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); + if( db->mallocFailed ){ + sqlite3SrcListDelete(db, pList); + return 0; + } + pItem = &pList->a[pList->nSrc-1]; + if( pDatabase && pDatabase->z==0 ){ + pDatabase = 0; + } + if( pDatabase ){ + Token *pTemp = pDatabase; + pDatabase = pTable; + pTable = pTemp; + } + pItem->zName = sqlite3NameFromToken(db, pTable); + pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); + return pList; +} + +/* +** Assign VdbeCursor index numbers to all tables in a SrcList +*/ +SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ + int i; + struct SrcList_item *pItem; + assert(pList || pParse->db->mallocFailed ); + if( pList ){ + for(i=0, pItem=pList->a; inSrc; i++, pItem++){ + if( pItem->iCursor>=0 ) break; + pItem->iCursor = pParse->nTab++; + if( pItem->pSelect ){ + sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); + } + } + } +} + +/* +** Delete an entire SrcList including all its substructure. +*/ +SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ + int i; + struct SrcList_item *pItem; + if( pList==0 ) return; + for(pItem=pList->a, i=0; inSrc; i++, pItem++){ + sqlite3DbFree(db, pItem->zDatabase); + sqlite3DbFree(db, pItem->zName); + sqlite3DbFree(db, pItem->zAlias); + if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); + if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); + sqlite3DeleteTable(db, pItem->pTab); + sqlite3SelectDelete(db, pItem->pSelect); + sqlite3ExprDelete(db, pItem->pOn); + sqlite3IdListDelete(db, pItem->pUsing); + } + sqlite3DbFree(db, pList); +} + +/* +** This routine is called by the parser to add a new term to the +** end of a growing FROM clause. The "p" parameter is the part of +** the FROM clause that has already been constructed. "p" is NULL +** if this is the first term of the FROM clause. pTable and pDatabase +** are the name of the table and database named in the FROM clause term. +** pDatabase is NULL if the database name qualifier is missing - the +** usual case. If the term has an alias, then pAlias points to the +** alias token. If the term is a subquery, then pSubquery is the +** SELECT statement that the subquery encodes. The pTable and +** pDatabase parameters are NULL for subqueries. The pOn and pUsing +** parameters are the content of the ON and USING clauses. +** +** Return a new SrcList which encodes is the FROM with the new +** term added. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm( + Parse *pParse, /* Parsing context */ + SrcList *p, /* The left part of the FROM clause already seen */ + Token *pTable, /* Name of the table to add to the FROM clause */ + Token *pDatabase, /* Name of the database containing pTable */ + Token *pAlias, /* The right-hand side of the AS subexpression */ + Select *pSubquery, /* A subquery used in place of a table name */ + Expr *pOn, /* The ON clause of a join */ + IdList *pUsing /* The USING clause of a join */ +){ + struct SrcList_item *pItem; + sqlite3 *db = pParse->db; + if( !p && (pOn || pUsing) ){ + sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", + (pOn ? "ON" : "USING") + ); + goto append_from_error; + } + p = sqlite3SrcListAppend(db, p, pTable, pDatabase); + if( p==0 || NEVER(p->nSrc==0) ){ + goto append_from_error; + } + pItem = &p->a[p->nSrc-1]; + assert( pAlias!=0 ); + if( pAlias->n ){ + pItem->zAlias = sqlite3NameFromToken(db, pAlias); + } + pItem->pSelect = pSubquery; + pItem->pOn = pOn; + pItem->pUsing = pUsing; + return p; + + append_from_error: + assert( p==0 ); + sqlite3ExprDelete(db, pOn); + sqlite3IdListDelete(db, pUsing); + sqlite3SelectDelete(db, pSubquery); + return 0; +} + +/* +** Add an INDEXED BY or NOT INDEXED clause to the most recently added +** element of the source-list passed as the second argument. +*/ +SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ + assert( pIndexedBy!=0 ); + if( p && ALWAYS(p->nSrc>0) ){ + struct SrcList_item *pItem = &p->a[p->nSrc-1]; + assert( pItem->fg.notIndexed==0 ); + assert( pItem->fg.isIndexedBy==0 ); + assert( pItem->fg.isTabFunc==0 ); + if( pIndexedBy->n==1 && !pIndexedBy->z ){ + /* A "NOT INDEXED" clause was supplied. See parse.y + ** construct "indexed_opt" for details. */ + pItem->fg.notIndexed = 1; + }else{ + pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); + pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0); + } + } +} + +/* +** Add the list of function arguments to the SrcList entry for a +** table-valued-function. +*/ +SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ + if( p ){ + struct SrcList_item *pItem = &p->a[p->nSrc-1]; + assert( pItem->fg.notIndexed==0 ); + assert( pItem->fg.isIndexedBy==0 ); + assert( pItem->fg.isTabFunc==0 ); + pItem->u1.pFuncArg = pList; + pItem->fg.isTabFunc = 1; + }else{ + sqlite3ExprListDelete(pParse->db, pList); + } +} + +/* +** When building up a FROM clause in the parser, the join operator +** is initially attached to the left operand. But the code generator +** expects the join operator to be on the right operand. This routine +** Shifts all join operators from left to right for an entire FROM +** clause. +** +** Example: Suppose the join is like this: +** +** A natural cross join B +** +** The operator is "natural cross join". The A and B operands are stored +** in p->a[0] and p->a[1], respectively. The parser initially stores the +** operator with A. This routine shifts that operator over to B. +*/ +SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ + if( p ){ + int i; + for(i=p->nSrc-1; i>0; i--){ + p->a[i].fg.jointype = p->a[i-1].fg.jointype; + } + p->a[0].fg.jointype = 0; + } +} + +/* +** Generate VDBE code for a BEGIN statement. +*/ +SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){ + sqlite3 *db; + Vdbe *v; + int i; + + assert( pParse!=0 ); + db = pParse->db; + assert( db!=0 ); + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( !v ) return; + if( type!=TK_DEFERRED ){ + for(i=0; inDb; i++){ + sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); + sqlite3VdbeUsesBtree(v, i); + } + } + sqlite3VdbeAddOp0(v, OP_AutoCommit); +} + +/* +** Generate VDBE code for a COMMIT statement. +*/ +SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ + Vdbe *v; + + assert( pParse!=0 ); + assert( pParse->db!=0 ); + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp1(v, OP_AutoCommit, 1); + } +} + +/* +** Generate VDBE code for a ROLLBACK statement. +*/ +SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ + Vdbe *v; + + assert( pParse!=0 ); + assert( pParse->db!=0 ); + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1); + } +} + +/* +** This function is called by the parser when it parses a command to create, +** release or rollback an SQL savepoint. +*/ +SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ + char *zName = sqlite3NameFromToken(pParse->db, pName); + if( zName ){ + Vdbe *v = sqlite3GetVdbe(pParse); +#ifndef SQLITE_OMIT_AUTHORIZATION + static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; + assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); +#endif + if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ + sqlite3DbFree(pParse->db, zName); + return; + } + sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); + } +} + +/* +** Make sure the TEMP database is open and available for use. Return +** the number of errors. Leave any error messages in the pParse structure. +*/ +SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){ + sqlite3 *db = pParse->db; + if( db->aDb[1].pBt==0 && !pParse->explain ){ + int rc; + Btree *pBt; + static const int flags = + SQLITE_OPEN_READWRITE | + SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | + SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_TEMP_DB; + + rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); + if( rc!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "unable to open a temporary database " + "file for storing temporary tables"); + pParse->rc = rc; + return 1; + } + db->aDb[1].pBt = pBt; + assert( db->aDb[1].pSchema ); + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ + sqlite3OomFault(db); + return 1; + } + } + return 0; +} + +/* +** Record the fact that the schema cookie will need to be verified +** for database iDb. The code to actually verify the schema cookie +** will occur at the end of the top-level VDBE and will be generated +** later, by sqlite3FinishCoding(). +*/ +SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + sqlite3 *db = pToplevel->db; + + assert( iDb>=0 && iDbnDb ); + assert( db->aDb[iDb].pBt!=0 || iDb==1 ); + assert( iDbcookieMask, iDb)==0 ){ + DbMaskSet(pToplevel->cookieMask, iDb); + pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; + if( !OMIT_TEMPDB && iDb==1 ){ + sqlite3OpenTempDatabase(pToplevel); + } + } +} + +/* +** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each +** attached database. Otherwise, invoke it for the database named zDb only. +*/ +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ + sqlite3 *db = pParse->db; + int i; + for(i=0; inDb; i++){ + Db *pDb = &db->aDb[i]; + if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){ + sqlite3CodeVerifySchema(pParse, i); + } + } +} + +/* +** Generate VDBE code that prepares for doing an operation that +** might change the database. +** +** This routine starts a new transaction if we are not already within +** a transaction. If we are already within a transaction, then a checkpoint +** is set if the setStatement parameter is true. A checkpoint should +** be set for operations that might fail (due to a constraint) part of +** the way through and which will need to undo some writes without having to +** rollback the whole transaction. For operations where all constraints +** can be checked before any changes are made to the database, it is never +** necessary to undo a write and the checkpoint should not be set. +*/ +SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + sqlite3CodeVerifySchema(pParse, iDb); + DbMaskSet(pToplevel->writeMask, iDb); + pToplevel->isMultiWrite |= setStatement; +} + +/* +** Indicate that the statement currently under construction might write +** more than one entry (example: deleting one row then inserting another, +** inserting multiple rows in a table, or inserting a row and index entries.) +** If an abort occurs after some of these writes have completed, then it will +** be necessary to undo the completed writes. +*/ +SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pToplevel->isMultiWrite = 1; +} + +/* +** The code generator calls this routine if is discovers that it is +** possible to abort a statement prior to completion. In order to +** perform this abort without corrupting the database, we need to make +** sure that the statement is protected by a statement transaction. +** +** Technically, we only need to set the mayAbort flag if the +** isMultiWrite flag was previously set. There is a time dependency +** such that the abort must occur after the multiwrite. This makes +** some statements involving the REPLACE conflict resolution algorithm +** go a little faster. But taking advantage of this time dependency +** makes it more difficult to prove that the code is correct (in +** particular, it prevents us from writing an effective +** implementation of sqlite3AssertMayAbort()) and so we have chosen +** to take the safe route and skip the optimization. +*/ +SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pToplevel->mayAbort = 1; +} + +/* +** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT +** error. The onError parameter determines which (if any) of the statement +** and/or current transaction is rolled back. +*/ +SQLITE_PRIVATE void sqlite3HaltConstraint( + Parse *pParse, /* Parsing context */ + int errCode, /* extended error code */ + int onError, /* Constraint type */ + char *p4, /* Error message */ + i8 p4type, /* P4_STATIC or P4_TRANSIENT */ + u8 p5Errmsg /* P5_ErrMsg type */ +){ + Vdbe *v = sqlite3GetVdbe(pParse); + assert( (errCode&0xff)==SQLITE_CONSTRAINT ); + if( onError==OE_Abort ){ + sqlite3MayAbort(pParse); + } + sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); + sqlite3VdbeChangeP5(v, p5Errmsg); +} + +/* +** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. +*/ +SQLITE_PRIVATE void sqlite3UniqueConstraint( + Parse *pParse, /* Parsing context */ + int onError, /* Constraint type */ + Index *pIdx /* The index that triggers the constraint */ +){ + char *zErr; + int j; + StrAccum errMsg; + Table *pTab = pIdx->pTable; + + sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); + if( pIdx->aColExpr ){ + sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName); + }else{ + for(j=0; jnKeyCol; j++){ + char *zCol; + assert( pIdx->aiColumn[j]>=0 ); + zCol = pTab->aCol[pIdx->aiColumn[j]].zName; + if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); + sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol); + } + } + zErr = sqlite3StrAccumFinish(&errMsg); + sqlite3HaltConstraint(pParse, + IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY + : SQLITE_CONSTRAINT_UNIQUE, + onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); +} + + +/* +** Code an OP_Halt due to non-unique rowid. +*/ +SQLITE_PRIVATE void sqlite3RowidConstraint( + Parse *pParse, /* Parsing context */ + int onError, /* Conflict resolution algorithm */ + Table *pTab /* The table with the non-unique rowid */ +){ + char *zMsg; + int rc; + if( pTab->iPKey>=0 ){ + zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, + pTab->aCol[pTab->iPKey].zName); + rc = SQLITE_CONSTRAINT_PRIMARYKEY; + }else{ + zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); + rc = SQLITE_CONSTRAINT_ROWID; + } + sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, + P5_ConstraintUnique); +} + +/* +** Check to see if pIndex uses the collating sequence pColl. Return +** true if it does and false if it does not. +*/ +#ifndef SQLITE_OMIT_REINDEX +static int collationMatch(const char *zColl, Index *pIndex){ + int i; + assert( zColl!=0 ); + for(i=0; inColumn; i++){ + const char *z = pIndex->azColl[i]; + assert( z!=0 || pIndex->aiColumn[i]<0 ); + if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ + return 1; + } + } + return 0; +} +#endif + +/* +** Recompute all indices of pTab that use the collating sequence pColl. +** If pColl==0 then recompute all indices of pTab. +*/ +#ifndef SQLITE_OMIT_REINDEX +static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ + Index *pIndex; /* An index associated with pTab */ + + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( zColl==0 || collationMatch(zColl, pIndex) ){ + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3RefillIndex(pParse, pIndex, -1); + } + } +} +#endif + +/* +** Recompute all indices of all tables in all databases where the +** indices use the collating sequence pColl. If pColl==0 then recompute +** all indices everywhere. +*/ +#ifndef SQLITE_OMIT_REINDEX +static void reindexDatabases(Parse *pParse, char const *zColl){ + Db *pDb; /* A single database */ + int iDb; /* The database index number */ + sqlite3 *db = pParse->db; /* The database connection */ + HashElem *k; /* For looping over tables in pDb */ + Table *pTab; /* A table in the database */ + + assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ + for(iDb=0, pDb=db->aDb; iDbnDb; iDb++, pDb++){ + assert( pDb!=0 ); + for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ + pTab = (Table*)sqliteHashData(k); + reindexTable(pParse, pTab, zColl); + } + } +} +#endif + +/* +** Generate code for the REINDEX command. +** +** REINDEX -- 1 +** REINDEX -- 2 +** REINDEX ?.? -- 3 +** REINDEX ?.? -- 4 +** +** Form 1 causes all indices in all attached databases to be rebuilt. +** Form 2 rebuilds all indices in all databases that use the named +** collating function. Forms 3 and 4 rebuild the named index or all +** indices associated with the named table. +*/ +#ifndef SQLITE_OMIT_REINDEX +SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ + CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ + char *z; /* Name of a table or index */ + const char *zDb; /* Name of the database */ + Table *pTab; /* A table in the database */ + Index *pIndex; /* An index associated with pTab */ + int iDb; /* The database index number */ + sqlite3 *db = pParse->db; /* The database connection */ + Token *pObjName; /* Name of the table or index to be reindexed */ + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return; + } + + if( pName1==0 ){ + reindexDatabases(pParse, 0); + return; + }else if( NEVER(pName2==0) || pName2->z==0 ){ + char *zColl; + assert( pName1->z ); + zColl = sqlite3NameFromToken(pParse->db, pName1); + if( !zColl ) return; + pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); + if( pColl ){ + reindexDatabases(pParse, zColl); + sqlite3DbFree(db, zColl); + return; + } + sqlite3DbFree(db, zColl); + } + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); + if( iDb<0 ) return; + z = sqlite3NameFromToken(db, pObjName); + if( z==0 ) return; + zDb = db->aDb[iDb].zName; + pTab = sqlite3FindTable(db, z, zDb); + if( pTab ){ + reindexTable(pParse, pTab, 0); + sqlite3DbFree(db, z); + return; + } + pIndex = sqlite3FindIndex(db, z, zDb); + sqlite3DbFree(db, z); + if( pIndex ){ + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3RefillIndex(pParse, pIndex, -1); + return; + } + sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); +} +#endif + +/* +** Return a KeyInfo structure that is appropriate for the given Index. +** +** The KeyInfo structure for an index is cached in the Index object. +** So there might be multiple references to the returned pointer. The +** caller should not try to modify the KeyInfo object. +** +** The caller should invoke sqlite3KeyInfoUnref() on the returned object +** when it has finished using it. +*/ +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ + int i; + int nCol = pIdx->nColumn; + int nKey = pIdx->nKeyCol; + KeyInfo *pKey; + if( pParse->nErr ) return 0; + if( pIdx->uniqNotNull ){ + pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); + }else{ + pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); + } + if( pKey ){ + assert( sqlite3KeyInfoIsWriteable(pKey) ); + for(i=0; iazColl[i]; + pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : + sqlite3LocateCollSeq(pParse, zColl); + pKey->aSortOrder[i] = pIdx->aSortOrder[i]; + } + if( pParse->nErr ){ + sqlite3KeyInfoUnref(pKey); + pKey = 0; + } + } + return pKey; +} + +#ifndef SQLITE_OMIT_CTE +/* +** This routine is invoked once per CTE by the parser while parsing a +** WITH clause. +*/ +SQLITE_PRIVATE With *sqlite3WithAdd( + Parse *pParse, /* Parsing context */ + With *pWith, /* Existing WITH clause, or NULL */ + Token *pName, /* Name of the common-table */ + ExprList *pArglist, /* Optional column name list for the table */ + Select *pQuery /* Query used to initialize the table */ +){ + sqlite3 *db = pParse->db; + With *pNew; + char *zName; + + /* Check that the CTE name is unique within this WITH clause. If + ** not, store an error in the Parse structure. */ + zName = sqlite3NameFromToken(pParse->db, pName); + if( zName && pWith ){ + int i; + for(i=0; inCte; i++){ + if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ + sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); + } + } + } + + if( pWith ){ + int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); + pNew = sqlite3DbRealloc(db, pWith, nByte); + }else{ + pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); + } + assert( (pNew!=0 && zName!=0) || db->mallocFailed ); + + if( db->mallocFailed ){ + sqlite3ExprListDelete(db, pArglist); + sqlite3SelectDelete(db, pQuery); + sqlite3DbFree(db, zName); + pNew = pWith; + }else{ + pNew->a[pNew->nCte].pSelect = pQuery; + pNew->a[pNew->nCte].pCols = pArglist; + pNew->a[pNew->nCte].zName = zName; + pNew->a[pNew->nCte].zCteErr = 0; + pNew->nCte++; + } + + return pNew; +} + +/* +** Free the contents of the With object passed as the second argument. +*/ +SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){ + if( pWith ){ + int i; + for(i=0; inCte; i++){ + struct Cte *pCte = &pWith->a[i]; + sqlite3ExprListDelete(db, pCte->pCols); + sqlite3SelectDelete(db, pCte->pSelect); + sqlite3DbFree(db, pCte->zName); + } + sqlite3DbFree(db, pWith); + } +} +#endif /* !defined(SQLITE_OMIT_CTE) */ + +/************** End of build.c ***********************************************/ +/************** Begin file callback.c ****************************************/ +/* +** 2005 May 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains functions used to access the internal hash tables +** of user defined functions and collation sequences. +*/ + +/* #include "sqliteInt.h" */ + +/* +** Invoke the 'collation needed' callback to request a collation sequence +** in the encoding enc of name zName, length nName. +*/ +static void callCollNeeded(sqlite3 *db, int enc, const char *zName){ + assert( !db->xCollNeeded || !db->xCollNeeded16 ); + if( db->xCollNeeded ){ + char *zExternal = sqlite3DbStrDup(db, zName); + if( !zExternal ) return; + db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal); + sqlite3DbFree(db, zExternal); + } +#ifndef SQLITE_OMIT_UTF16 + if( db->xCollNeeded16 ){ + char const *zExternal; + sqlite3_value *pTmp = sqlite3ValueNew(db); + sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC); + zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE); + if( zExternal ){ + db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal); + } + sqlite3ValueFree(pTmp); + } +#endif +} + +/* +** This routine is called if the collation factory fails to deliver a +** collation function in the best encoding but there may be other versions +** of this collation function (for other text encodings) available. Use one +** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if +** possible. +*/ +static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ + CollSeq *pColl2; + char *z = pColl->zName; + int i; + static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 }; + for(i=0; i<3; i++){ + pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0); + if( pColl2->xCmp!=0 ){ + memcpy(pColl, pColl2, sizeof(CollSeq)); + pColl->xDel = 0; /* Do not copy the destructor */ + return SQLITE_OK; + } + } + return SQLITE_ERROR; +} + +/* +** This function is responsible for invoking the collation factory callback +** or substituting a collation sequence of a different encoding when the +** requested collation sequence is not available in the desired encoding. +** +** If it is not NULL, then pColl must point to the database native encoding +** collation sequence with name zName, length nName. +** +** The return value is either the collation sequence to be used in database +** db for collation type name zName, length nName, or NULL, if no collation +** sequence can be found. If no collation is found, leave an error message. +** +** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq( + Parse *pParse, /* Parsing context */ + u8 enc, /* The desired encoding for the collating sequence */ + CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ + const char *zName /* Collating sequence name */ +){ + CollSeq *p; + sqlite3 *db = pParse->db; + + p = pColl; + if( !p ){ + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( !p || !p->xCmp ){ + /* No collation sequence of this type for this encoding is registered. + ** Call the collation factory to see if it can supply us with one. + */ + callCollNeeded(db, enc, zName); + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( p && !p->xCmp && synthCollSeq(db, p) ){ + p = 0; + } + assert( !p || p->xCmp ); + if( p==0 ){ + sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); + } + return p; +} + +/* +** This routine is called on a collation sequence before it is used to +** check that it is defined. An undefined collation sequence exists when +** a database is loaded that contains references to collation sequences +** that have not been defined by sqlite3_create_collation() etc. +** +** If required, this routine calls the 'collation needed' callback to +** request a definition of the collating sequence. If this doesn't work, +** an equivalent collating sequence that uses a text encoding different +** from the main database is substituted, if one is available. +*/ +SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ + if( pColl ){ + const char *zName = pColl->zName; + sqlite3 *db = pParse->db; + CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName); + if( !p ){ + return SQLITE_ERROR; + } + assert( p==pColl ); + } + return SQLITE_OK; +} + + + +/* +** Locate and return an entry from the db.aCollSeq hash table. If the entry +** specified by zName and nName is not found and parameter 'create' is +** true, then create a new entry. Otherwise return NULL. +** +** Each pointer stored in the sqlite3.aCollSeq hash table contains an +** array of three CollSeq structures. The first is the collation sequence +** preferred for UTF-8, the second UTF-16le, and the third UTF-16be. +** +** Stored immediately after the three collation sequences is a copy of +** the collation sequence name. A pointer to this string is stored in +** each collation sequence structure. +*/ +static CollSeq *findCollSeqEntry( + sqlite3 *db, /* Database connection */ + const char *zName, /* Name of the collating sequence */ + int create /* Create a new entry if true */ +){ + CollSeq *pColl; + pColl = sqlite3HashFind(&db->aCollSeq, zName); + + if( 0==pColl && create ){ + int nName = sqlite3Strlen30(zName); + pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1); + if( pColl ){ + CollSeq *pDel = 0; + pColl[0].zName = (char*)&pColl[3]; + pColl[0].enc = SQLITE_UTF8; + pColl[1].zName = (char*)&pColl[3]; + pColl[1].enc = SQLITE_UTF16LE; + pColl[2].zName = (char*)&pColl[3]; + pColl[2].enc = SQLITE_UTF16BE; + memcpy(pColl[0].zName, zName, nName); + pColl[0].zName[nName] = 0; + pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl); + + /* If a malloc() failure occurred in sqlite3HashInsert(), it will + ** return the pColl pointer to be deleted (because it wasn't added + ** to the hash table). + */ + assert( pDel==0 || pDel==pColl ); + if( pDel!=0 ){ + sqlite3OomFault(db); + sqlite3DbFree(db, pDel); + pColl = 0; + } + } + } + return pColl; +} + +/* +** Parameter zName points to a UTF-8 encoded string nName bytes long. +** Return the CollSeq* pointer for the collation sequence named zName +** for the encoding 'enc' from the database 'db'. +** +** If the entry specified is not found and 'create' is true, then create a +** new entry. Otherwise return NULL. +** +** A separate function sqlite3LocateCollSeq() is a wrapper around +** this routine. sqlite3LocateCollSeq() invokes the collation factory +** if necessary and generates an error message if the collating sequence +** cannot be found. +** +** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq( + sqlite3 *db, + u8 enc, + const char *zName, + int create +){ + CollSeq *pColl; + if( zName ){ + pColl = findCollSeqEntry(db, zName, create); + }else{ + pColl = db->pDfltColl; + } + assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); + if( pColl ) pColl += enc-1; + return pColl; +} + +/* During the search for the best function definition, this procedure +** is called to test how well the function passed as the first argument +** matches the request for a function with nArg arguments in a system +** that uses encoding enc. The value returned indicates how well the +** request is matched. A higher value indicates a better match. +** +** If nArg is -1 that means to only return a match (non-zero) if p->nArg +** is also -1. In other words, we are searching for a function that +** takes a variable number of arguments. +** +** If nArg is -2 that means that we are searching for any function +** regardless of the number of arguments it uses, so return a positive +** match score for any +** +** The returned value is always between 0 and 6, as follows: +** +** 0: Not a match. +** 1: UTF8/16 conversion required and function takes any number of arguments. +** 2: UTF16 byte order change required and function takes any number of args. +** 3: encoding matches and function takes any number of arguments +** 4: UTF8/16 conversion required - argument count matches exactly +** 5: UTF16 byte order conversion required - argument count matches exactly +** 6: Perfect match: encoding and argument count match exactly. +** +** If nArg==(-2) then any function with a non-null xSFunc is +** a perfect match and any function with xSFunc NULL is +** a non-match. +*/ +#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */ +static int matchQuality( + FuncDef *p, /* The function we are evaluating for match quality */ + int nArg, /* Desired number of arguments. (-1)==any */ + u8 enc /* Desired text encoding */ +){ + int match; + + /* nArg of -2 is a special case */ + if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH; + + /* Wrong number of arguments means "no match" */ + if( p->nArg!=nArg && p->nArg>=0 ) return 0; + + /* Give a better score to a function with a specific number of arguments + ** than to function that accepts any number of arguments. */ + if( p->nArg==nArg ){ + match = 4; + }else{ + match = 1; + } + + /* Bonus points if the text encoding matches */ + if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){ + match += 2; /* Exact encoding match */ + }else if( (enc & p->funcFlags & 2)!=0 ){ + match += 1; /* Both are UTF16, but with different byte orders */ + } + + return match; +} + +/* +** Search a FuncDefHash for a function with the given name. Return +** a pointer to the matching FuncDef if found, or 0 if there is no match. +*/ +static FuncDef *functionSearch( + int h, /* Hash of the name */ + const char *zFunc /* Name of function */ +){ + FuncDef *p; + for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){ + if( sqlite3StrICmp(p->zName, zFunc)==0 ){ + return p; + } + } + return 0; +} + +/* +** Insert a new FuncDef into a FuncDefHash hash table. +*/ +SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs( + FuncDef *aDef, /* List of global functions to be inserted */ + int nDef /* Length of the apDef[] list */ +){ + int i; + for(i=0; ipNext!=&aDef[i] ); + aDef[i].pNext = pOther->pNext; + pOther->pNext = &aDef[i]; + }else{ + aDef[i].pNext = 0; + aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h]; + sqlite3BuiltinFunctions.a[h] = &aDef[i]; + } + } +} + + + +/* +** Locate a user function given a name, a number of arguments and a flag +** indicating whether the function prefers UTF-16 over UTF-8. Return a +** pointer to the FuncDef structure that defines that function, or return +** NULL if the function does not exist. +** +** If the createFlag argument is true, then a new (blank) FuncDef +** structure is created and liked into the "db" structure if a +** no matching function previously existed. +** +** If nArg is -2, then the first valid function found is returned. A +** function is valid if xSFunc is non-zero. The nArg==(-2) +** case is used to see if zName is a valid function name for some number +** of arguments. If nArg is -2, then createFlag must be 0. +** +** If createFlag is false, then a function with the required name and +** number of arguments may be returned even if the eTextRep flag does not +** match that requested. +*/ +SQLITE_PRIVATE FuncDef *sqlite3FindFunction( + sqlite3 *db, /* An open database */ + const char *zName, /* Name of the function. zero-terminated */ + int nArg, /* Number of arguments. -1 means any number */ + u8 enc, /* Preferred text encoding */ + u8 createFlag /* Create new entry if true and does not otherwise exist */ +){ + FuncDef *p; /* Iterator variable */ + FuncDef *pBest = 0; /* Best match found so far */ + int bestScore = 0; /* Score of best match */ + int h; /* Hash value */ + int nName; /* Length of the name */ + + assert( nArg>=(-2) ); + assert( nArg>=(-1) || createFlag==0 ); + nName = sqlite3Strlen30(zName); + + /* First search for a match amongst the application-defined functions. + */ + p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName); + while( p ){ + int score = matchQuality(p, nArg, enc); + if( score>bestScore ){ + pBest = p; + bestScore = score; + } + p = p->pNext; + } + + /* If no match is found, search the built-in functions. + ** + ** If the SQLITE_PreferBuiltin flag is set, then search the built-in + ** functions even if a prior app-defined function was found. And give + ** priority to built-in functions. + ** + ** Except, if createFlag is true, that means that we are trying to + ** install a new function. Whatever FuncDef structure is returned it will + ** have fields overwritten with new information appropriate for the + ** new function. But the FuncDefs for built-in functions are read-only. + ** So we must not search for built-ins when creating a new function. + */ + if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ + bestScore = 0; + h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ; + p = functionSearch(h, zName); + while( p ){ + int score = matchQuality(p, nArg, enc); + if( score>bestScore ){ + pBest = p; + bestScore = score; + } + p = p->pNext; + } + } + + /* If the createFlag parameter is true and the search did not reveal an + ** exact match for the name, number of arguments and encoding, then add a + ** new entry to the hash table and return it. + */ + if( createFlag && bestScorezName = (const char*)&pBest[1]; + pBest->nArg = (u16)nArg; + pBest->funcFlags = enc; + memcpy((char*)&pBest[1], zName, nName+1); + pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest); + if( pOther==pBest ){ + sqlite3DbFree(db, pBest); + sqlite3OomFault(db); + return 0; + }else{ + pBest->pNext = pOther; + } + } + + if( pBest && (pBest->xSFunc || createFlag) ){ + return pBest; + } + return 0; +} + +/* +** Free all resources held by the schema structure. The void* argument points +** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the +** pointer itself, it just cleans up subsidiary resources (i.e. the contents +** of the schema hash tables). +** +** The Schema.cache_size variable is not cleared. +*/ +SQLITE_PRIVATE void sqlite3SchemaClear(void *p){ + Hash temp1; + Hash temp2; + HashElem *pElem; + Schema *pSchema = (Schema *)p; + + temp1 = pSchema->tblHash; + temp2 = pSchema->trigHash; + sqlite3HashInit(&pSchema->trigHash); + sqlite3HashClear(&pSchema->idxHash); + for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ + sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); + } + sqlite3HashClear(&temp2); + sqlite3HashInit(&pSchema->tblHash); + for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ + Table *pTab = sqliteHashData(pElem); + sqlite3DeleteTable(0, pTab); + } + sqlite3HashClear(&temp1); + sqlite3HashClear(&pSchema->fkeyHash); + pSchema->pSeqTab = 0; + if( pSchema->schemaFlags & DB_SchemaLoaded ){ + pSchema->iGeneration++; + pSchema->schemaFlags &= ~DB_SchemaLoaded; + } +} + +/* +** Find and return the schema associated with a BTree. Create +** a new one if necessary. +*/ +SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ + Schema * p; + if( pBt ){ + p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear); + }else{ + p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); + } + if( !p ){ + sqlite3OomFault(db); + }else if ( 0==p->file_format ){ + sqlite3HashInit(&p->tblHash); + sqlite3HashInit(&p->idxHash); + sqlite3HashInit(&p->trigHash); + sqlite3HashInit(&p->fkeyHash); + p->enc = SQLITE_UTF8; + } + return p; +} + +/************** End of callback.c ********************************************/ +/************** Begin file delete.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** in order to generate code for DELETE FROM statements. +*/ +/* #include "sqliteInt.h" */ + +/* +** While a SrcList can in general represent multiple tables and subqueries +** (as in the FROM clause of a SELECT statement) in this case it contains +** the name of a single table, as one might find in an INSERT, DELETE, +** or UPDATE statement. Look up that table in the symbol table and +** return a pointer. Set an error message and return NULL if the table +** name is not found or if any other error occurs. +** +** The following fields are initialized appropriate in pSrc: +** +** pSrc->a[0].pTab Pointer to the Table object +** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one +** +*/ +SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ + struct SrcList_item *pItem = pSrc->a; + Table *pTab; + assert( pItem && pSrc->nSrc==1 ); + pTab = sqlite3LocateTableItem(pParse, 0, pItem); + sqlite3DeleteTable(pParse->db, pItem->pTab); + pItem->pTab = pTab; + if( pTab ){ + pTab->nRef++; + } + if( sqlite3IndexedByLookup(pParse, pItem) ){ + pTab = 0; + } + return pTab; +} + +/* +** Check to make sure the given table is writable. If it is not +** writable, generate an error message and return 1. If it is +** writable return 0; +*/ +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ + /* A table is not writable under the following circumstances: + ** + ** 1) It is a virtual table and no implementation of the xUpdate method + ** has been provided, or + ** 2) It is a system table (i.e. sqlite_master), this call is not + ** part of a nested parse and writable_schema pragma has not + ** been specified. + ** + ** In either case leave an error message in pParse and return non-zero. + */ + if( ( IsVirtual(pTab) + && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ) + || ( (pTab->tabFlags & TF_Readonly)!=0 + && (pParse->db->flags & SQLITE_WriteSchema)==0 + && pParse->nested==0 ) + ){ + sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); + return 1; + } + +#ifndef SQLITE_OMIT_VIEW + if( !viewOk && pTab->pSelect ){ + sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); + return 1; + } +#endif + return 0; +} + + +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) +/* +** Evaluate a view and store its result in an ephemeral table. The +** pWhere argument is an optional WHERE clause that restricts the +** set of rows in the view that are to be added to the ephemeral table. +*/ +SQLITE_PRIVATE void sqlite3MaterializeView( + Parse *pParse, /* Parsing context */ + Table *pView, /* View definition */ + Expr *pWhere, /* Optional WHERE clause to be added */ + int iCur /* Cursor number for ephemeral table */ +){ + SelectDest dest; + Select *pSel; + SrcList *pFrom; + sqlite3 *db = pParse->db; + int iDb = sqlite3SchemaToIndex(db, pView->pSchema); + pWhere = sqlite3ExprDup(db, pWhere, 0); + pFrom = sqlite3SrcListAppend(db, 0, 0, 0); + if( pFrom ){ + assert( pFrom->nSrc==1 ); + pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName); + pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); + assert( pFrom->a[0].pOn==0 ); + assert( pFrom->a[0].pUsing==0 ); + } + pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, + SF_IncludeHidden, 0, 0); + sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); + sqlite3Select(pParse, pSel, &dest); + sqlite3SelectDelete(db, pSel); +} +#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ + +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) +/* +** Generate an expression tree to implement the WHERE, ORDER BY, +** and LIMIT/OFFSET portion of DELETE and UPDATE statements. +** +** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1; +** \__________________________/ +** pLimitWhere (pInClause) +*/ +SQLITE_PRIVATE Expr *sqlite3LimitWhere( + Parse *pParse, /* The parser context */ + SrcList *pSrc, /* the FROM clause -- which tables to scan */ + Expr *pWhere, /* The WHERE clause. May be null */ + ExprList *pOrderBy, /* The ORDER BY clause. May be null */ + Expr *pLimit, /* The LIMIT clause. May be null */ + Expr *pOffset, /* The OFFSET clause. May be null */ + char *zStmtType /* Either DELETE or UPDATE. For err msgs. */ +){ + Expr *pWhereRowid = NULL; /* WHERE rowid .. */ + Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */ + Expr *pSelectRowid = NULL; /* SELECT rowid ... */ + ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */ + SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */ + Select *pSelect = NULL; /* Complete SELECT tree */ + + /* Check that there isn't an ORDER BY without a LIMIT clause. + */ + if( pOrderBy && (pLimit == 0) ) { + sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType); + goto limit_where_cleanup; + } + + /* We only need to generate a select expression if there + ** is a limit/offset term to enforce. + */ + if( pLimit == 0 ) { + /* if pLimit is null, pOffset will always be null as well. */ + assert( pOffset == 0 ); + return pWhere; + } + + /* Generate a select expression tree to enforce the limit/offset + ** term for the DELETE or UPDATE statement. For example: + ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 + ** becomes: + ** DELETE FROM table_a WHERE rowid IN ( + ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 + ** ); + */ + + pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); + if( pSelectRowid == 0 ) goto limit_where_cleanup; + pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid); + if( pEList == 0 ) goto limit_where_cleanup; + + /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree + ** and the SELECT subtree. */ + pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0); + if( pSelectSrc == 0 ) { + sqlite3ExprListDelete(pParse->db, pEList); + goto limit_where_cleanup; + } + + /* generate the SELECT expression tree. */ + pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0, + pOrderBy,0,pLimit,pOffset); + if( pSelect == 0 ) return 0; + + /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */ + pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); + pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0) : 0; + sqlite3PExprAddSelect(pParse, pInClause, pSelect); + return pInClause; + +limit_where_cleanup: + sqlite3ExprDelete(pParse->db, pWhere); + sqlite3ExprListDelete(pParse->db, pOrderBy); + sqlite3ExprDelete(pParse->db, pLimit); + sqlite3ExprDelete(pParse->db, pOffset); + return 0; +} +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */ + /* && !defined(SQLITE_OMIT_SUBQUERY) */ + +/* +** Generate code for a DELETE FROM statement. +** +** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; +** \________/ \________________/ +** pTabList pWhere +*/ +SQLITE_PRIVATE void sqlite3DeleteFrom( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* The table from which we should delete things */ + Expr *pWhere /* The WHERE clause. May be null */ +){ + Vdbe *v; /* The virtual database engine */ + Table *pTab; /* The table from which records will be deleted */ + const char *zDb; /* Name of database holding pTab */ + int i; /* Loop counter */ + WhereInfo *pWInfo; /* Information about the WHERE clause */ + Index *pIdx; /* For looping over indices of the table */ + int iTabCur; /* Cursor number for the table */ + int iDataCur = 0; /* VDBE cursor for the canonical data source */ + int iIdxCur = 0; /* Cursor number of the first index */ + int nIdx; /* Number of indices */ + sqlite3 *db; /* Main database structure */ + AuthContext sContext; /* Authorization context */ + NameContext sNC; /* Name context to resolve expressions in */ + int iDb; /* Database number */ + int memCnt = -1; /* Memory cell used for change counting */ + int rcauth; /* Value returned by authorization callback */ + int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */ + int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ + u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */ + Index *pPk; /* The PRIMARY KEY index on the table */ + int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */ + i16 nPk = 1; /* Number of columns in the PRIMARY KEY */ + int iKey; /* Memory cell holding key of row to be deleted */ + i16 nKey; /* Number of memory cells in the row key */ + int iEphCur = 0; /* Ephemeral table holding all primary key values */ + int iRowSet = 0; /* Register for rowset of rows to delete */ + int addrBypass = 0; /* Address of jump over the delete logic */ + int addrLoop = 0; /* Top of the delete loop */ + int addrEphOpen = 0; /* Instruction to open the Ephemeral table */ + int bComplex; /* True if there are triggers or FKs or + ** subqueries in the WHERE clause */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True if attempting to delete from a view */ + Trigger *pTrigger; /* List of table triggers, if required */ +#endif + + memset(&sContext, 0, sizeof(sContext)); + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ + goto delete_from_cleanup; + } + assert( pTabList->nSrc==1 ); + + /* Locate the table which we want to delete. This table has to be + ** put in an SrcList structure because some of the subroutines we + ** will be calling are designed to work with multiple tables and expect + ** an SrcList* parameter instead of just a Table* parameter. + */ + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ) goto delete_from_cleanup; + + /* Figure out if we have any triggers and if the table being + ** deleted from is a view + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + isView = pTab->pSelect!=0; + bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0); +#else +# define pTrigger 0 +# define isView 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + + /* If pTab is really a view, make sure it has been initialized. + */ + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto delete_from_cleanup; + } + + if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ + goto delete_from_cleanup; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDbnDb ); + zDb = db->aDb[iDb].zName; + rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb); + assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE ); + if( rcauth==SQLITE_DENY ){ + goto delete_from_cleanup; + } + assert(!isView || pTrigger); + + /* Assign cursor numbers to the table and all its indices. + */ + assert( pTabList->nSrc==1 ); + iTabCur = pTabList->a[0].iCursor = pParse->nTab++; + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ + pParse->nTab++; + } + + /* Start the view context + */ + if( isView ){ + sqlite3AuthContextPush(pParse, &sContext, pTab->zName); + } + + /* Begin generating code. + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ){ + goto delete_from_cleanup; + } + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, 1, iDb); + + /* If we are trying to delete from a view, realize that view into + ** an ephemeral table. + */ +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) + if( isView ){ + sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur); + iDataCur = iIdxCur = iTabCur; + } +#endif + + /* Resolve the column names in the WHERE clause. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + if( sqlite3ResolveExprNames(&sNC, pWhere) ){ + goto delete_from_cleanup; + } + + /* Initialize the counter of the number of rows deleted, if + ** we are counting rows. + */ + if( db->flags & SQLITE_CountRows ){ + memCnt = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); + } + +#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION + /* Special case: A DELETE without a WHERE clause deletes everything. + ** It is easier just to erase the whole table. Prior to version 3.6.5, + ** this optimization caused the row change count (the value returned by + ** API function sqlite3_count_changes) to be set incorrectly. */ + if( rcauth==SQLITE_OK + && pWhere==0 + && !bComplex + && !IsVirtual(pTab) +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + && db->xPreUpdateCallback==0 +#endif + ){ + assert( !isView ); + sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt, + pTab->zName, P4_STATIC); + } + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + assert( pIdx->pSchema==pTab->pSchema ); + sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); + } + }else +#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ + { + u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE; + if( sNC.ncFlags & NC_VarSelect ) bComplex = 1; + wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW); + if( HasRowid(pTab) ){ + /* For a rowid table, initialize the RowSet to an empty set */ + pPk = 0; + nPk = 1; + iRowSet = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); + }else{ + /* For a WITHOUT ROWID table, create an ephemeral table used to + ** hold all primary keys for rows to be deleted. */ + pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + nPk = pPk->nKeyCol; + iPk = pParse->nMem+1; + pParse->nMem += nPk; + iEphCur = pParse->nTab++; + addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + } + + /* Construct a query to find the rowid or primary key for every row + ** to be deleted, based on the WHERE clause. Set variable eOnePass + ** to indicate the strategy used to implement this delete: + ** + ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values. + ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. + ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. + */ + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1); + if( pWInfo==0 ) goto delete_from_cleanup; + eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); + assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI ); + assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF ); + + /* Keep track of the number of rows to be deleted */ + if( db->flags & SQLITE_CountRows ){ + sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); + } + + /* Extract the rowid or primary key for the current row */ + if( pPk ){ + for(i=0; iaiColumn[i]>=0 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, + pPk->aiColumn[i], iPk+i); + } + iKey = iPk; + }else{ + iKey = pParse->nMem + 1; + iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0); + if( iKey>pParse->nMem ) pParse->nMem = iKey; + } + + if( eOnePass!=ONEPASS_OFF ){ + /* For ONEPASS, no need to store the rowid/primary-key. There is only + ** one, so just keep it in its register(s) and fall through to the + ** delete code. */ + nKey = nPk; /* OP_Found will use an unpacked key */ + aToOpen = sqlite3DbMallocRawNN(db, nIdx+2); + if( aToOpen==0 ){ + sqlite3WhereEnd(pWInfo); + goto delete_from_cleanup; + } + memset(aToOpen, 1, nIdx+1); + aToOpen[nIdx+1] = 0; + if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0; + if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0; + if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen); + }else{ + if( pPk ){ + /* Add the PK key for this row to the temporary table */ + iKey = ++pParse->nMem; + nKey = 0; /* Zero tells OP_Found to use a composite key */ + sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, + sqlite3IndexAffinityStr(pParse->db, pPk), nPk); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey); + }else{ + /* Add the rowid of the row to be deleted to the RowSet */ + nKey = 1; /* OP_Seek always uses a single rowid */ + sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey); + } + } + + /* If this DELETE cannot use the ONEPASS strategy, this is the + ** end of the WHERE loop */ + if( eOnePass!=ONEPASS_OFF ){ + addrBypass = sqlite3VdbeMakeLabel(v); + }else{ + sqlite3WhereEnd(pWInfo); + } + + /* Unless this is a view, open cursors for the table we are + ** deleting from and all its indices. If this is a view, then the + ** only effect this statement has is to fire the INSTEAD OF + ** triggers. + */ + if( !isView ){ + int iAddrOnce = 0; + if( eOnePass==ONEPASS_MULTI ){ + iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v); + } + testcase( IsVirtual(pTab) ); + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE, + iTabCur, aToOpen, &iDataCur, &iIdxCur); + assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur ); + assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 ); + if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce); + } + + /* Set up a loop over the rowids/primary-keys that were found in the + ** where-clause loop above. + */ + if( eOnePass!=ONEPASS_OFF ){ + assert( nKey==nPk ); /* OP_Found will use an unpacked key */ + if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){ + assert( pPk!=0 || pTab->pSelect!=0 ); + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey); + VdbeCoverage(v); + } + }else if( pPk ){ + addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey); + assert( nKey==0 ); /* OP_Found will use a composite key */ + }else{ + addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey); + VdbeCoverage(v); + assert( nKey==1 ); + } + + /* Delete the row */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + sqlite3VtabMakeWritable(pParse, pTab); + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); + sqlite3VdbeChangeP5(v, OE_Abort); + assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE ); + sqlite3MayAbort(pParse); + if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){ + pParse->isMultiWrite = 0; + } + }else +#endif + { + int count = (pParse->nested==0); /* True to count changes */ + int iIdxNoSeek = -1; + if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){ + iIdxNoSeek = aiCurOnePass[1]; + } + sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, + iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek); + } + + /* End of the loop over all rowids/primary-keys. */ + if( eOnePass!=ONEPASS_OFF ){ + sqlite3VdbeResolveLabel(v, addrBypass); + sqlite3WhereEnd(pWInfo); + }else if( pPk ){ + sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrLoop); + }else{ + sqlite3VdbeGoto(v, addrLoop); + sqlite3VdbeJumpHere(v, addrLoop); + } + + /* Close the cursors open on the table and its indexes. */ + if( !isView && !IsVirtual(pTab) ){ + if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); + for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ + sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i); + } + } + } /* End non-truncate path */ + + /* Update the sqlite_sequence table by storing the content of the + ** maximum rowid counter values recorded while inserting into + ** autoincrement tables. + */ + if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* Return the number of rows that were deleted. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC); + } + +delete_from_cleanup: + sqlite3AuthContextPop(&sContext); + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprDelete(db, pWhere); + sqlite3DbFree(db, aToOpen); + return; +} +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** they may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif + +/* +** This routine generates VDBE code that causes a single row of a +** single table to be deleted. Both the original table entry and +** all indices are removed. +** +** Preconditions: +** +** 1. iDataCur is an open cursor on the btree that is the canonical data +** store for the table. (This will be either the table itself, +** in the case of a rowid table, or the PRIMARY KEY index in the case +** of a WITHOUT ROWID table.) +** +** 2. Read/write cursors for all indices of pTab must be open as +** cursor number iIdxCur+i for the i-th index. +** +** 3. The primary key for the row to be deleted must be stored in a +** sequence of nPk memory cells starting at iPk. If nPk==0 that means +** that a search record formed from OP_MakeRecord is contained in the +** single memory location iPk. +** +** eMode: +** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or +** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor +** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF +** then this function must seek iDataCur to the entry identified by iPk +** and nPk before reading from it. +** +** If eMode is ONEPASS_MULTI, then this call is being made as part +** of a ONEPASS delete that affects multiple rows. In this case, if +** iIdxNoSeek is a valid cursor number (>=0), then its position should +** be preserved following the delete operation. Or, if iIdxNoSeek is not +** a valid cursor number, the position of iDataCur should be preserved +** instead. +** +** iIdxNoSeek: +** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an +** index cursor (from within array of cursors starting at iIdxCur) that +** already points to the index entry to be deleted. +*/ +SQLITE_PRIVATE void sqlite3GenerateRowDelete( + Parse *pParse, /* Parsing context */ + Table *pTab, /* Table containing the row to be deleted */ + Trigger *pTrigger, /* List of triggers to (potentially) fire */ + int iDataCur, /* Cursor from which column data is extracted */ + int iIdxCur, /* First index cursor */ + int iPk, /* First memory cell containing the PRIMARY KEY */ + i16 nPk, /* Number of PRIMARY KEY memory cells */ + u8 count, /* If non-zero, increment the row change counter */ + u8 onconf, /* Default ON CONFLICT policy for triggers */ + u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */ + int iIdxNoSeek /* Cursor number of cursor that does not need seeking */ +){ + Vdbe *v = pParse->pVdbe; /* Vdbe */ + int iOld = 0; /* First register in OLD.* array */ + int iLabel; /* Label resolved to end of generated code */ + u8 opSeek; /* Seek opcode */ + + /* Vdbe is guaranteed to have been allocated by this stage. */ + assert( v ); + VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)", + iDataCur, iIdxCur, iPk, (int)nPk)); + + /* Seek cursor iCur to the row to delete. If this row no longer exists + ** (this can happen if a trigger program has already deleted it), do + ** not attempt to delete it or fire any DELETE triggers. */ + iLabel = sqlite3VdbeMakeLabel(v); + opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound; + if( eMode==ONEPASS_OFF ){ + sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk); + VdbeCoverageIf(v, opSeek==OP_NotExists); + VdbeCoverageIf(v, opSeek==OP_NotFound); + } + + /* If there are any triggers to fire, allocate a range of registers to + ** use for the old.* references in the triggers. */ + if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ + u32 mask; /* Mask of OLD.* columns in use */ + int iCol; /* Iterator used while populating OLD.* */ + int addrStart; /* Start of BEFORE trigger programs */ + + /* TODO: Could use temporary registers here. Also could attempt to + ** avoid copying the contents of the rowid register. */ + mask = sqlite3TriggerColmask( + pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf + ); + mask |= sqlite3FkOldmask(pParse, pTab); + iOld = pParse->nMem+1; + pParse->nMem += (1 + pTab->nCol); + + /* Populate the OLD.* pseudo-table register array. These values will be + ** used by any BEFORE and AFTER triggers that exist. */ + sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld); + for(iCol=0; iColnCol; iCol++){ + testcase( mask!=0xffffffff && iCol==31 ); + testcase( mask!=0xffffffff && iCol==32 ); + if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1); + } + } + + /* Invoke BEFORE DELETE trigger programs. */ + addrStart = sqlite3VdbeCurrentAddr(v); + sqlite3CodeRowTrigger(pParse, pTrigger, + TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel + ); + + /* If any BEFORE triggers were coded, then seek the cursor to the + ** row to be deleted again. It may be that the BEFORE triggers moved + ** the cursor or of already deleted the row that the cursor was + ** pointing to. + */ + if( addrStartpSelect==0 ){ + u8 p5 = 0; + sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek); + sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0)); + sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE); + if( eMode!=ONEPASS_OFF ){ + sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE); + } + if( iIdxNoSeek>=0 ){ + sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek); + } + if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION; + sqlite3VdbeChangeP5(v, p5); + } + + /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to + ** handle rows (possibly in other tables) that refer via a foreign key + ** to the row just deleted. */ + sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0); + + /* Invoke AFTER DELETE trigger programs. */ + sqlite3CodeRowTrigger(pParse, pTrigger, + TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel + ); + + /* Jump here if the row had already been deleted before any BEFORE + ** trigger programs were invoked. Or if a trigger program throws a + ** RAISE(IGNORE) exception. */ + sqlite3VdbeResolveLabel(v, iLabel); + VdbeModuleComment((v, "END: GenRowDel()")); +} + +/* +** This routine generates VDBE code that causes the deletion of all +** index entries associated with a single row of a single table, pTab +** +** Preconditions: +** +** 1. A read/write cursor "iDataCur" must be open on the canonical storage +** btree for the table pTab. (This will be either the table itself +** for rowid tables or to the primary key index for WITHOUT ROWID +** tables.) +** +** 2. Read/write cursors for all indices of pTab must be open as +** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex +** index is the 0-th index.) +** +** 3. The "iDataCur" cursor must be already be positioned on the row +** that is to be deleted. +*/ +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* Table containing the row to be deleted */ + int iDataCur, /* Cursor of table holding data. */ + int iIdxCur, /* First index cursor */ + int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ + int iIdxNoSeek /* Do not delete from this cursor */ +){ + int i; /* Index loop counter */ + int r1 = -1; /* Register holding an index key */ + int iPartIdxLabel; /* Jump destination for skipping partial index entries */ + Index *pIdx; /* Current index */ + Index *pPrior = 0; /* Prior index */ + Vdbe *v; /* The prepared statement under construction */ + Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */ + + v = pParse->pVdbe; + pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); + for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ + assert( iIdxCur+i!=iDataCur || pPk==pIdx ); + if( aRegIdx!=0 && aRegIdx[i]==0 ) continue; + if( pIdx==pPk ) continue; + if( iIdxCur+i==iIdxNoSeek ) continue; + VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName)); + r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, + &iPartIdxLabel, pPrior, r1); + sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1, + pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn); + sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); + pPrior = pIdx; + } +} + +/* +** Generate code that will assemble an index key and stores it in register +** regOut. The key with be for index pIdx which is an index on pTab. +** iCur is the index of a cursor open on the pTab table and pointing to +** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then +** iCur must be the cursor of the PRIMARY KEY index. +** +** Return a register number which is the first in a block of +** registers that holds the elements of the index key. The +** block of registers has already been deallocated by the time +** this routine returns. +** +** If *piPartIdxLabel is not NULL, fill it in with a label and jump +** to that label if pIdx is a partial index that should be skipped. +** The label should be resolved using sqlite3ResolvePartIdxLabel(). +** A partial index should be skipped if its WHERE clause evaluates +** to false or null. If pIdx is not a partial index, *piPartIdxLabel +** will be set to zero which is an empty label that is ignored by +** sqlite3ResolvePartIdxLabel(). +** +** The pPrior and regPrior parameters are used to implement a cache to +** avoid unnecessary register loads. If pPrior is not NULL, then it is +** a pointer to a different index for which an index key has just been +** computed into register regPrior. If the current pIdx index is generating +** its key into the same sequence of registers and if pPrior and pIdx share +** a column in common, then the register corresponding to that column already +** holds the correct value and the loading of that register is skipped. +** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK +** on a table with multiple indices, and especially with the ROWID or +** PRIMARY KEY columns of the index. +*/ +SQLITE_PRIVATE int sqlite3GenerateIndexKey( + Parse *pParse, /* Parsing context */ + Index *pIdx, /* The index for which to generate a key */ + int iDataCur, /* Cursor number from which to take column data */ + int regOut, /* Put the new key into this register if not 0 */ + int prefixOnly, /* Compute only a unique prefix of the key */ + int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */ + Index *pPrior, /* Previously generated index key */ + int regPrior /* Register holding previous generated key */ +){ + Vdbe *v = pParse->pVdbe; + int j; + int regBase; + int nCol; + + if( piPartIdxLabel ){ + if( pIdx->pPartIdxWhere ){ + *piPartIdxLabel = sqlite3VdbeMakeLabel(v); + pParse->iSelfTab = iDataCur; + sqlite3ExprCachePush(pParse); + sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, + SQLITE_JUMPIFNULL); + }else{ + *piPartIdxLabel = 0; + } + } + nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn; + regBase = sqlite3GetTempRange(pParse, nCol); + if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0; + for(j=0; jaiColumn[j]==pIdx->aiColumn[j] + && pPrior->aiColumn[j]!=XN_EXPR + ){ + /* This column was already computed by the previous index */ + continue; + } + sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j); + /* If the column affinity is REAL but the number is an integer, then it + ** might be stored in the table as an integer (using a compact + ** representation) then converted to REAL by an OP_RealAffinity opcode. + ** But we are getting ready to store this value back into an index, where + ** it should be converted by to INTEGER again. So omit the OP_RealAffinity + ** opcode if it is present */ + sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); + } + if( regOut ){ + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut); + } + sqlite3ReleaseTempRange(pParse, regBase, nCol); + return regBase; +} + +/* +** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label +** because it was a partial index, then this routine should be called to +** resolve that label. +*/ +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){ + if( iLabel ){ + sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel); + sqlite3ExprCachePop(pParse); + } +} + +/************** End of delete.c **********************************************/ +/************** Begin file func.c ********************************************/ +/* +** 2002 February 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C-language implementations for many of the SQL +** functions of SQLite. (Some function, and in particular the date and +** time functions, are implemented separately.) +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include */ +/* #include "vdbeInt.h" */ + +/* +** Return the collating function associated with a function. +*/ +static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ + VdbeOp *pOp; + assert( context->pVdbe!=0 ); + pOp = &context->pVdbe->aOp[context->iOp-1]; + assert( pOp->opcode==OP_CollSeq ); + assert( pOp->p4type==P4_COLLSEQ ); + return pOp->p4.pColl; +} + +/* +** Indicate that the accumulator load should be skipped on this +** iteration of the aggregate loop. +*/ +static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ + context->skipFlag = 1; +} + +/* +** Implementation of the non-aggregate min() and max() functions +*/ +static void minmaxFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i; + int mask; /* 0 for min() or 0xffffffff for max() */ + int iBest; + CollSeq *pColl; + + assert( argc>1 ); + mask = sqlite3_user_data(context)==0 ? 0 : -1; + pColl = sqlite3GetFuncCollSeq(context); + assert( pColl ); + assert( mask==-1 || mask==0 ); + iBest = 0; + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + for(i=1; i=0 ){ + testcase( mask==0 ); + iBest = i; + } + } + sqlite3_result_value(context, argv[iBest]); +} + +/* +** Return the type of the argument. +*/ +static void typeofFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + const char *z = 0; + UNUSED_PARAMETER(NotUsed); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_INTEGER: z = "integer"; break; + case SQLITE_TEXT: z = "text"; break; + case SQLITE_FLOAT: z = "real"; break; + case SQLITE_BLOB: z = "blob"; break; + default: z = "null"; break; + } + sqlite3_result_text(context, z, -1, SQLITE_STATIC); +} + + +/* +** Implementation of the length() function +*/ +static void lengthFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int len; + + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_BLOB: + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); + break; + } + case SQLITE_TEXT: { + const unsigned char *z = sqlite3_value_text(argv[0]); + if( z==0 ) return; + len = 0; + while( *z ){ + len++; + SQLITE_SKIP_UTF8(z); + } + sqlite3_result_int(context, len); + break; + } + default: { + sqlite3_result_null(context); + break; + } + } +} + +/* +** Implementation of the abs() function. +** +** IMP: R-23979-26855 The abs(X) function returns the absolute value of +** the numeric argument X. +*/ +static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_INTEGER: { + i64 iVal = sqlite3_value_int64(argv[0]); + if( iVal<0 ){ + if( iVal==SMALLEST_INT64 ){ + /* IMP: R-31676-45509 If X is the integer -9223372036854775808 + ** then abs(X) throws an integer overflow error since there is no + ** equivalent positive 64-bit two complement value. */ + sqlite3_result_error(context, "integer overflow", -1); + return; + } + iVal = -iVal; + } + sqlite3_result_int64(context, iVal); + break; + } + case SQLITE_NULL: { + /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ + sqlite3_result_null(context); + break; + } + default: { + /* Because sqlite3_value_double() returns 0.0 if the argument is not + ** something that can be converted into a number, we have: + ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob + ** that cannot be converted to a numeric value. + */ + double rVal = sqlite3_value_double(argv[0]); + if( rVal<0 ) rVal = -rVal; + sqlite3_result_double(context, rVal); + break; + } + } +} + +/* +** Implementation of the instr() function. +** +** instr(haystack,needle) finds the first occurrence of needle +** in haystack and returns the number of previous characters plus 1, +** or 0 if needle does not occur within haystack. +** +** If both haystack and needle are BLOBs, then the result is one more than +** the number of bytes in haystack prior to the first occurrence of needle, +** or 0 if needle never occurs in haystack. +*/ +static void instrFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zHaystack; + const unsigned char *zNeedle; + int nHaystack; + int nNeedle; + int typeHaystack, typeNeedle; + int N = 1; + int isText; + + UNUSED_PARAMETER(argc); + typeHaystack = sqlite3_value_type(argv[0]); + typeNeedle = sqlite3_value_type(argv[1]); + if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; + nHaystack = sqlite3_value_bytes(argv[0]); + nNeedle = sqlite3_value_bytes(argv[1]); + if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ + zHaystack = sqlite3_value_blob(argv[0]); + zNeedle = sqlite3_value_blob(argv[1]); + isText = 0; + }else{ + zHaystack = sqlite3_value_text(argv[0]); + zNeedle = sqlite3_value_text(argv[1]); + isText = 1; + } + while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ + N++; + do{ + nHaystack--; + zHaystack++; + }while( isText && (zHaystack[0]&0xc0)==0x80 ); + } + if( nNeedle>nHaystack ) N = 0; + sqlite3_result_int(context, N); +} + +/* +** Implementation of the printf() function. +*/ +static void printfFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + PrintfArguments x; + StrAccum str; + const char *zFormat; + int n; + sqlite3 *db = sqlite3_context_db_handle(context); + + if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ + x.nArg = argc-1; + x.nUsed = 0; + x.apArg = argv+1; + sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); + str.printfFlags = SQLITE_PRINTF_SQLFUNC; + sqlite3XPrintf(&str, zFormat, &x); + n = str.nChar; + sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, + SQLITE_DYNAMIC); + } +} + +/* +** Implementation of the substr() function. +** +** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. +** p1 is 1-indexed. So substr(x,1,1) returns the first character +** of x. If x is text, then we actually count UTF-8 characters. +** If x is a blob, then we count bytes. +** +** If p1 is negative, then we begin abs(p1) from the end of x[]. +** +** If p2 is negative, return the p2 characters preceding p1. +*/ +static void substrFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *z; + const unsigned char *z2; + int len; + int p0type; + i64 p1, p2; + int negP2 = 0; + + assert( argc==3 || argc==2 ); + if( sqlite3_value_type(argv[1])==SQLITE_NULL + || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) + ){ + return; + } + p0type = sqlite3_value_type(argv[0]); + p1 = sqlite3_value_int(argv[1]); + if( p0type==SQLITE_BLOB ){ + len = sqlite3_value_bytes(argv[0]); + z = sqlite3_value_blob(argv[0]); + if( z==0 ) return; + assert( len==sqlite3_value_bytes(argv[0]) ); + }else{ + z = sqlite3_value_text(argv[0]); + if( z==0 ) return; + len = 0; + if( p1<0 ){ + for(z2=z; *z2; len++){ + SQLITE_SKIP_UTF8(z2); + } + } + } +#ifdef SQLITE_SUBSTR_COMPATIBILITY + /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as + ** as substr(X,1,N) - it returns the first N characters of X. This + ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] + ** from 2009-02-02 for compatibility of applications that exploited the + ** old buggy behavior. */ + if( p1==0 ) p1 = 1; /* */ +#endif + if( argc==3 ){ + p2 = sqlite3_value_int(argv[2]); + if( p2<0 ){ + p2 = -p2; + negP2 = 1; + } + }else{ + p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; + } + if( p1<0 ){ + p1 += len; + if( p1<0 ){ + p2 += p1; + if( p2<0 ) p2 = 0; + p1 = 0; + } + }else if( p1>0 ){ + p1--; + }else if( p2>0 ){ + p2--; + } + if( negP2 ){ + p1 -= p2; + if( p1<0 ){ + p2 += p1; + p1 = 0; + } + } + assert( p1>=0 && p2>=0 ); + if( p0type!=SQLITE_BLOB ){ + while( *z && p1 ){ + SQLITE_SKIP_UTF8(z); + p1--; + } + for(z2=z; *z2 && p2; p2--){ + SQLITE_SKIP_UTF8(z2); + } + sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, + SQLITE_UTF8); + }else{ + if( p1+p2>len ){ + p2 = len-p1; + if( p2<0 ) p2 = 0; + } + sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); + } +} + +/* +** Implementation of the round() function +*/ +#ifndef SQLITE_OMIT_FLOATING_POINT +static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + int n = 0; + double r; + char *zBuf; + assert( argc==1 || argc==2 ); + if( argc==2 ){ + if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; + n = sqlite3_value_int(argv[1]); + if( n>30 ) n = 30; + if( n<0 ) n = 0; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + r = sqlite3_value_double(argv[0]); + /* If Y==0 and X will fit in a 64-bit int, + ** handle the rounding directly, + ** otherwise use printf. + */ + if( n==0 && r>=0 && r0 ); + testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); + testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); + if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + z = 0; + }else{ + z = sqlite3Malloc(nByte); + if( !z ){ + sqlite3_result_error_nomem(context); + } + } + return z; +} + +/* +** Implementation of the upper() and lower() SQL functions. +*/ +static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + char *z1; + const char *z2; + int i, n; + UNUSED_PARAMETER(argc); + z2 = (char*)sqlite3_value_text(argv[0]); + n = sqlite3_value_bytes(argv[0]); + /* Verify that the call to _bytes() does not invalidate the _text() pointer */ + assert( z2==(char*)sqlite3_value_text(argv[0]) ); + if( z2 ){ + z1 = contextMalloc(context, ((i64)n)+1); + if( z1 ){ + for(i=0; imatchOne; /* "?" or "_" */ + u32 matchAll = pInfo->matchAll; /* "*" or "%" */ + u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ + const u8 *zEscaped = 0; /* One past the last escaped input char */ + + while( (c = Utf8Read(zPattern))!=0 ){ + if( c==matchAll ){ /* Match "*" */ + /* Skip over multiple "*" characters in the pattern. If there + ** are also "?" characters, skip those as well, but consume a + ** single character of the input string for each "?" skipped */ + while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ + if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ + return 0; + } + } + if( c==0 ){ + return 1; /* "*" at the end of the pattern matches */ + }else if( c==matchOther ){ + if( pInfo->matchSet==0 ){ + c = sqlite3Utf8Read(&zPattern); + if( c==0 ) return 0; + }else{ + /* "[...]" immediately follows the "*". We have to do a slow + ** recursive search in this case, but it is an unusual case. */ + assert( matchOther<0x80 ); /* '[' is a single-byte character */ + while( *zString + && patternCompare(&zPattern[-1],zString,pInfo,matchOther)==0 ){ + SQLITE_SKIP_UTF8(zString); + } + return *zString!=0; + } + } + + /* At this point variable c contains the first character of the + ** pattern string past the "*". Search in the input string for the + ** first matching character and recursively contine the match from + ** that point. + ** + ** For a case-insensitive search, set variable cx to be the same as + ** c but in the other case and search the input string for either + ** c or cx. + */ + if( c<=0x80 ){ + u32 cx; + if( noCase ){ + cx = sqlite3Toupper(c); + c = sqlite3Tolower(c); + }else{ + cx = c; + } + while( (c2 = *(zString++))!=0 ){ + if( c2!=c && c2!=cx ) continue; + if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1; + } + }else{ + while( (c2 = Utf8Read(zString))!=0 ){ + if( c2!=c ) continue; + if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1; + } + } + return 0; + } + if( c==matchOther ){ + if( pInfo->matchSet==0 ){ + c = sqlite3Utf8Read(&zPattern); + if( c==0 ) return 0; + zEscaped = zPattern; + }else{ + u32 prior_c = 0; + int seen = 0; + int invert = 0; + c = sqlite3Utf8Read(&zString); + if( c==0 ) return 0; + c2 = sqlite3Utf8Read(&zPattern); + if( c2=='^' ){ + invert = 1; + c2 = sqlite3Utf8Read(&zPattern); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = sqlite3Utf8Read(&zPattern); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ + c2 = sqlite3Utf8Read(&zPattern); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = sqlite3Utf8Read(&zPattern); + } + if( c2==0 || (seen ^ invert)==0 ){ + return 0; + } + continue; + } + } + c2 = Utf8Read(zString); + if( c==c2 ) continue; + if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ + continue; + } + if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; + return 0; + } + return *zString==0; +} + +/* +** The sqlite3_strglob() interface. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlobPattern, const char *zString){ + return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[')==0; +} + +/* +** The sqlite3_strlike() interface. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ + return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc)==0; +} + +/* +** Count the number of times that the LIKE operator (or GLOB which is +** just a variation of LIKE) gets called. This is used for testing +** only. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_like_count = 0; +#endif + + +/* +** Implementation of the like() SQL function. This function implements +** the build-in LIKE operator. The first argument to the function is the +** pattern and the second argument is the string. So, the SQL statements: +** +** A LIKE B +** +** is implemented as like(B,A). +** +** This same function (with a different compareInfo structure) computes +** the GLOB operator. +*/ +static void likeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zA, *zB; + u32 escape; + int nPat; + sqlite3 *db = sqlite3_context_db_handle(context); + struct compareInfo *pInfo = sqlite3_user_data(context); + +#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS + if( sqlite3_value_type(argv[0])==SQLITE_BLOB + || sqlite3_value_type(argv[1])==SQLITE_BLOB + ){ +#ifdef SQLITE_TEST + sqlite3_like_count++; +#endif + sqlite3_result_int(context, 0); + return; + } +#endif + zB = sqlite3_value_text(argv[0]); + zA = sqlite3_value_text(argv[1]); + + /* Limit the length of the LIKE or GLOB pattern to avoid problems + ** of deep recursion and N*N behavior in patternCompare(). + */ + nPat = sqlite3_value_bytes(argv[0]); + testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); + testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); + if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); + return; + } + assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ + + if( argc==3 ){ + /* The escape character string must consist of a single UTF-8 character. + ** Otherwise, return an error. + */ + const unsigned char *zEsc = sqlite3_value_text(argv[2]); + if( zEsc==0 ) return; + if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ + sqlite3_result_error(context, + "ESCAPE expression must be a single character", -1); + return; + } + escape = sqlite3Utf8Read(&zEsc); + }else{ + escape = pInfo->matchSet; + } + if( zA && zB ){ +#ifdef SQLITE_TEST + sqlite3_like_count++; +#endif + sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); + } +} + +/* +** Implementation of the NULLIF(x,y) function. The result is the first +** argument if the arguments are different. The result is NULL if the +** arguments are equal to each other. +*/ +static void nullifFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + CollSeq *pColl = sqlite3GetFuncCollSeq(context); + UNUSED_PARAMETER(NotUsed); + if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ + sqlite3_result_value(context, argv[0]); + } +} + +/* +** Implementation of the sqlite_version() function. The result is the version +** of the SQLite library that is running. +*/ +static void versionFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + /* IMP: R-48699-48617 This function is an SQL wrapper around the + ** sqlite3_libversion() C-interface. */ + sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); +} + +/* +** Implementation of the sqlite_source_id() function. The result is a string +** that identifies the particular version of the source code used to build +** SQLite. +*/ +static void sourceidFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + /* IMP: R-24470-31136 This function is an SQL wrapper around the + ** sqlite3_sourceid() C interface. */ + sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); +} + +/* +** Implementation of the sqlite_log() function. This is a wrapper around +** sqlite3_log(). The return value is NULL. The function exists purely for +** its side-effects. +*/ +static void errlogFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(context); + sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); +} + +/* +** Implementation of the sqlite_compileoption_used() function. +** The result is an integer that identifies if the compiler option +** was used to build SQLite. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +static void compileoptionusedFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zOptName; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL + ** function is a wrapper around the sqlite3_compileoption_used() C/C++ + ** function. + */ + if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ + sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); + } +} +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +/* +** Implementation of the sqlite_compileoption_get() function. +** The result is a string that identifies the compiler options +** used to build SQLite. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +static void compileoptiongetFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int n; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function + ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. + */ + n = sqlite3_value_int(argv[0]); + sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); +} +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +/* Array for converting from half-bytes (nybbles) into ASCII hex +** digits. */ +static const char hexdigits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +}; + +/* +** Implementation of the QUOTE() function. This function takes a single +** argument. If the argument is numeric, the return value is the same as +** the argument. If the argument is NULL, the return value is the string +** "NULL". Otherwise, the argument is enclosed in single quotes with +** single-quote escapes. +*/ +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_FLOAT: { + double r1, r2; + char zBuf[50]; + r1 = sqlite3_value_double(argv[0]); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); + sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); + if( r1!=r2 ){ + sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); + } + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); + break; + } + case SQLITE_INTEGER: { + sqlite3_result_value(context, argv[0]); + break; + } + case SQLITE_BLOB: { + char *zText = 0; + char const *zBlob = sqlite3_value_blob(argv[0]); + int nBlob = sqlite3_value_bytes(argv[0]); + assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ + zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); + if( zText ){ + int i; + for(i=0; i>4)&0x0F]; + zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; + } + zText[(nBlob*2)+2] = '\''; + zText[(nBlob*2)+3] = '\0'; + zText[0] = 'X'; + zText[1] = '\''; + sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); + sqlite3_free(zText); + } + break; + } + case SQLITE_TEXT: { + int i,j; + u64 n; + const unsigned char *zArg = sqlite3_value_text(argv[0]); + char *z; + + if( zArg==0 ) return; + for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } + z = contextMalloc(context, ((i64)i)+((i64)n)+3); + if( z ){ + z[0] = '\''; + for(i=0, j=1; zArg[i]; i++){ + z[j++] = zArg[i]; + if( zArg[i]=='\'' ){ + z[j++] = '\''; + } + } + z[j++] = '\''; + z[j] = 0; + sqlite3_result_text(context, z, j, sqlite3_free); + } + break; + } + default: { + assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); + sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); + break; + } + } +} + +/* +** The unicode() function. Return the integer unicode code-point value +** for the first character of the input string. +*/ +static void unicodeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *z = sqlite3_value_text(argv[0]); + (void)argc; + if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); +} + +/* +** The char() function takes zero or more arguments, each of which is +** an integer. It constructs a string where each character of the string +** is the unicode character for the corresponding integer argument. +*/ +static void charFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + unsigned char *z, *zOut; + int i; + zOut = z = sqlite3_malloc64( argc*4+1 ); + if( z==0 ){ + sqlite3_result_error_nomem(context); + return; + } + for(i=0; i0x10ffff ) x = 0xfffd; + c = (unsigned)(x & 0x1fffff); + if( c<0x00080 ){ + *zOut++ = (u8)(c&0xFF); + }else if( c<0x00800 ){ + *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); + *zOut++ = 0x80 + (u8)(c & 0x3F); + }else if( c<0x10000 ){ + *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); + *zOut++ = 0x80 + (u8)(c & 0x3F); + }else{ + *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); + *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); + *zOut++ = 0x80 + (u8)(c & 0x3F); + } \ + } + sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); +} + +/* +** The hex() function. Interpret the argument as a blob. Return +** a hexadecimal rendering as text. +*/ +static void hexFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i, n; + const unsigned char *pBlob; + char *zHex, *z; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + pBlob = sqlite3_value_blob(argv[0]); + n = sqlite3_value_bytes(argv[0]); + assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ + z = zHex = contextMalloc(context, ((i64)n)*2 + 1); + if( zHex ){ + for(i=0; i>4)&0xf]; + *(z++) = hexdigits[c&0xf]; + } + *z = 0; + sqlite3_result_text(context, zHex, n*2, sqlite3_free); + } +} + +/* +** The zeroblob(N) function returns a zero-filled blob of size N bytes. +*/ +static void zeroblobFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + i64 n; + int rc; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + n = sqlite3_value_int64(argv[0]); + if( n<0 ) n = 0; + rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ + if( rc ){ + sqlite3_result_error_code(context, rc); + } +} + +/* +** The replace() function. Three arguments are all strings: call +** them A, B, and C. The result is also a string which is derived +** from A by replacing every occurrence of B with C. The match +** must be exact. Collating sequences are not used. +*/ +static void replaceFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zStr; /* The input string A */ + const unsigned char *zPattern; /* The pattern string B */ + const unsigned char *zRep; /* The replacement string C */ + unsigned char *zOut; /* The output */ + int nStr; /* Size of zStr */ + int nPattern; /* Size of zPattern */ + int nRep; /* Size of zRep */ + i64 nOut; /* Maximum size of zOut */ + int loopLimit; /* Last zStr[] that might match zPattern[] */ + int i, j; /* Loop counters */ + + assert( argc==3 ); + UNUSED_PARAMETER(argc); + zStr = sqlite3_value_text(argv[0]); + if( zStr==0 ) return; + nStr = sqlite3_value_bytes(argv[0]); + assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ + zPattern = sqlite3_value_text(argv[1]); + if( zPattern==0 ){ + assert( sqlite3_value_type(argv[1])==SQLITE_NULL + || sqlite3_context_db_handle(context)->mallocFailed ); + return; + } + if( zPattern[0]==0 ){ + assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); + sqlite3_result_value(context, argv[0]); + return; + } + nPattern = sqlite3_value_bytes(argv[1]); + assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ + zRep = sqlite3_value_text(argv[2]); + if( zRep==0 ) return; + nRep = sqlite3_value_bytes(argv[2]); + assert( zRep==sqlite3_value_text(argv[2]) ); + nOut = nStr + 1; + assert( nOutaLimit[SQLITE_LIMIT_LENGTH] ); + testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); + if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + sqlite3_free(zOut); + return; + } + zOld = zOut; + zOut = sqlite3_realloc64(zOut, (int)nOut); + if( zOut==0 ){ + sqlite3_result_error_nomem(context); + sqlite3_free(zOld); + return; + } + memcpy(&zOut[j], zRep, nRep); + j += nRep; + i += nPattern-1; + } + } + assert( j+nStr-i+1==nOut ); + memcpy(&zOut[j], &zStr[i], nStr-i); + j += nStr - i; + assert( j<=nOut ); + zOut[j] = 0; + sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); +} + +/* +** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. +** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. +*/ +static void trimFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zIn; /* Input string */ + const unsigned char *zCharSet; /* Set of characters to trim */ + int nIn; /* Number of bytes in input */ + int flags; /* 1: trimleft 2: trimright 3: trim */ + int i; /* Loop counter */ + unsigned char *aLen = 0; /* Length of each character in zCharSet */ + unsigned char **azChar = 0; /* Individual characters in zCharSet */ + int nChar; /* Number of characters in zCharSet */ + + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + return; + } + zIn = sqlite3_value_text(argv[0]); + if( zIn==0 ) return; + nIn = sqlite3_value_bytes(argv[0]); + assert( zIn==sqlite3_value_text(argv[0]) ); + if( argc==1 ){ + static const unsigned char lenOne[] = { 1 }; + static unsigned char * const azOne[] = { (u8*)" " }; + nChar = 1; + aLen = (u8*)lenOne; + azChar = (unsigned char **)azOne; + zCharSet = 0; + }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ + return; + }else{ + const unsigned char *z; + for(z=zCharSet, nChar=0; *z; nChar++){ + SQLITE_SKIP_UTF8(z); + } + if( nChar>0 ){ + azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); + if( azChar==0 ){ + return; + } + aLen = (unsigned char*)&azChar[nChar]; + for(z=zCharSet, nChar=0; *z; nChar++){ + azChar[nChar] = (unsigned char *)z; + SQLITE_SKIP_UTF8(z); + aLen[nChar] = (u8)(z - azChar[nChar]); + } + } + } + if( nChar>0 ){ + flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); + if( flags & 1 ){ + while( nIn>0 ){ + int len = 0; + for(i=0; i=nChar ) break; + zIn += len; + nIn -= len; + } + } + if( flags & 2 ){ + while( nIn>0 ){ + int len = 0; + for(i=0; i=nChar ) break; + nIn -= len; + } + } + if( zCharSet ){ + sqlite3_free(azChar); + } + } + sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); +} + + +/* IMP: R-25361-16150 This function is omitted from SQLite by default. It +** is only available if the SQLITE_SOUNDEX compile-time option is used +** when SQLite is built. +*/ +#ifdef SQLITE_SOUNDEX +/* +** Compute the soundex encoding of a word. +** +** IMP: R-59782-00072 The soundex(X) function returns a string that is the +** soundex encoding of the string X. +*/ +static void soundexFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + char zResult[8]; + const u8 *zIn; + int i, j; + static const unsigned char iCode[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, + }; + assert( argc==1 ); + zIn = (u8*)sqlite3_value_text(argv[0]); + if( zIn==0 ) zIn = (u8*)""; + for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} + if( zIn[i] ){ + u8 prevcode = iCode[zIn[i]&0x7f]; + zResult[0] = sqlite3Toupper(zIn[i]); + for(j=1; j<4 && zIn[i]; i++){ + int code = iCode[zIn[i]&0x7f]; + if( code>0 ){ + if( code!=prevcode ){ + prevcode = code; + zResult[j++] = code + '0'; + } + }else{ + prevcode = 0; + } + } + while( j<4 ){ + zResult[j++] = '0'; + } + zResult[j] = 0; + sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); + }else{ + /* IMP: R-64894-50321 The string "?000" is returned if the argument + ** is NULL or contains no ASCII alphabetic characters. */ + sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); + } +} +#endif /* SQLITE_SOUNDEX */ + +#ifndef SQLITE_OMIT_LOAD_EXTENSION +/* +** A function that loads a shared-library extension then returns NULL. +*/ +static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ + const char *zFile = (const char *)sqlite3_value_text(argv[0]); + const char *zProc; + sqlite3 *db = sqlite3_context_db_handle(context); + char *zErrMsg = 0; + + /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc + ** flag is set. See the sqlite3_enable_load_extension() API. + */ + if( (db->flags & SQLITE_LoadExtFunc)==0 ){ + sqlite3_result_error(context, "not authorized", -1); + return; + } + + if( argc==2 ){ + zProc = (const char *)sqlite3_value_text(argv[1]); + }else{ + zProc = 0; + } + if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ + sqlite3_result_error(context, zErrMsg, -1); + sqlite3_free(zErrMsg); + } +} +#endif + + +/* +** An instance of the following structure holds the context of a +** sum() or avg() aggregate computation. +*/ +typedef struct SumCtx SumCtx; +struct SumCtx { + double rSum; /* Floating point sum */ + i64 iSum; /* Integer sum */ + i64 cnt; /* Number of elements summed */ + u8 overflow; /* True if integer overflow seen */ + u8 approx; /* True if non-integer value was input to the sum */ +}; + +/* +** Routines used to compute the sum, average, and total. +** +** The SUM() function follows the (broken) SQL standard which means +** that it returns NULL if it sums over no inputs. TOTAL returns +** 0.0 in that case. In addition, TOTAL always returns a float where +** SUM might return an integer if it never encounters a floating point +** value. TOTAL never fails, but SUM might through an exception if +** it overflows an integer. +*/ +static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ + SumCtx *p; + int type; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + p = sqlite3_aggregate_context(context, sizeof(*p)); + type = sqlite3_value_numeric_type(argv[0]); + if( p && type!=SQLITE_NULL ){ + p->cnt++; + if( type==SQLITE_INTEGER ){ + i64 v = sqlite3_value_int64(argv[0]); + p->rSum += v; + if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ + p->overflow = 1; + } + }else{ + p->rSum += sqlite3_value_double(argv[0]); + p->approx = 1; + } + } +} +static void sumFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + if( p && p->cnt>0 ){ + if( p->overflow ){ + sqlite3_result_error(context,"integer overflow",-1); + }else if( p->approx ){ + sqlite3_result_double(context, p->rSum); + }else{ + sqlite3_result_int64(context, p->iSum); + } + } +} +static void avgFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + if( p && p->cnt>0 ){ + sqlite3_result_double(context, p->rSum/(double)p->cnt); + } +} +static void totalFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + sqlite3_result_double(context, p ? p->rSum : (double)0); +} + +/* +** The following structure keeps track of state information for the +** count() aggregate function. +*/ +typedef struct CountCtx CountCtx; +struct CountCtx { + i64 n; +}; + +/* +** Routines to implement the count() aggregate function. +*/ +static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ + CountCtx *p; + p = sqlite3_aggregate_context(context, sizeof(*p)); + if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ + p->n++; + } + +#ifndef SQLITE_OMIT_DEPRECATED + /* The sqlite3_aggregate_count() function is deprecated. But just to make + ** sure it still operates correctly, verify that its count agrees with our + ** internal count when using count(*) and when the total count can be + ** expressed as a 32-bit integer. */ + assert( argc==1 || p==0 || p->n>0x7fffffff + || p->n==sqlite3_aggregate_count(context) ); +#endif +} +static void countFinalize(sqlite3_context *context){ + CountCtx *p; + p = sqlite3_aggregate_context(context, 0); + sqlite3_result_int64(context, p ? p->n : 0); +} + +/* +** Routines to implement min() and max() aggregate functions. +*/ +static void minmaxStep( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + Mem *pArg = (Mem *)argv[0]; + Mem *pBest; + UNUSED_PARAMETER(NotUsed); + + pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); + if( !pBest ) return; + + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); + }else if( pBest->flags ){ + int max; + int cmp; + CollSeq *pColl = sqlite3GetFuncCollSeq(context); + /* This step function is used for both the min() and max() aggregates, + ** the only difference between the two being that the sense of the + ** comparison is inverted. For the max() aggregate, the + ** sqlite3_user_data() function returns (void *)-1. For min() it + ** returns (void *)db, where db is the sqlite3* database pointer. + ** Therefore the next statement sets variable 'max' to 1 for the max() + ** aggregate, or 0 for min(). + */ + max = sqlite3_user_data(context)!=0; + cmp = sqlite3MemCompare(pBest, pArg, pColl); + if( (max && cmp<0) || (!max && cmp>0) ){ + sqlite3VdbeMemCopy(pBest, pArg); + }else{ + sqlite3SkipAccumulatorLoad(context); + } + }else{ + pBest->db = sqlite3_context_db_handle(context); + sqlite3VdbeMemCopy(pBest, pArg); + } +} +static void minMaxFinalize(sqlite3_context *context){ + sqlite3_value *pRes; + pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); + if( pRes ){ + if( pRes->flags ){ + sqlite3_result_value(context, pRes); + } + sqlite3VdbeMemRelease(pRes); + } +} + +/* +** group_concat(EXPR, ?SEPARATOR?) +*/ +static void groupConcatStep( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zVal; + StrAccum *pAccum; + const char *zSep; + int nVal, nSep; + assert( argc==1 || argc==2 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); + + if( pAccum ){ + sqlite3 *db = sqlite3_context_db_handle(context); + int firstTerm = pAccum->mxAlloc==0; + pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; + if( !firstTerm ){ + if( argc==2 ){ + zSep = (char*)sqlite3_value_text(argv[1]); + nSep = sqlite3_value_bytes(argv[1]); + }else{ + zSep = ","; + nSep = 1; + } + if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); + } + zVal = (char*)sqlite3_value_text(argv[0]); + nVal = sqlite3_value_bytes(argv[0]); + if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal); + } +} +static void groupConcatFinalize(sqlite3_context *context){ + StrAccum *pAccum; + pAccum = sqlite3_aggregate_context(context, 0); + if( pAccum ){ + if( pAccum->accError==STRACCUM_TOOBIG ){ + sqlite3_result_error_toobig(context); + }else if( pAccum->accError==STRACCUM_NOMEM ){ + sqlite3_result_error_nomem(context); + }else{ + sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, + sqlite3_free); + } + } +} + +/* +** This routine does per-connection function registration. Most +** of the built-in functions above are part of the global function set. +** This routine only deals with those that are not global. +*/ +SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ + int rc = sqlite3_overload_function(db, "MATCH", 2); + assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); + if( rc==SQLITE_NOMEM ){ + sqlite3OomFault(db); + } +} + +/* +** Set the LIKEOPT flag on the 2-argument function with the given name. +*/ +static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ + FuncDef *pDef; + pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0); + if( ALWAYS(pDef) ){ + pDef->funcFlags |= flagVal; + } +} + +/* +** Register the built-in LIKE and GLOB functions. The caseSensitive +** parameter determines whether or not the LIKE operator is case +** sensitive. GLOB is always case sensitive. +*/ +SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ + struct compareInfo *pInfo; + if( caseSensitive ){ + pInfo = (struct compareInfo*)&likeInfoAlt; + }else{ + pInfo = (struct compareInfo*)&likeInfoNorm; + } + sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); + sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); + sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, + (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); + setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); + setLikeOptFlag(db, "like", + caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); +} + +/* +** pExpr points to an expression which implements a function. If +** it is appropriate to apply the LIKE optimization to that function +** then set aWc[0] through aWc[2] to the wildcard characters and +** return TRUE. If the function is not a LIKE-style function then +** return FALSE. +** +** *pIsNocase is set to true if uppercase and lowercase are equivalent for +** the function (default for LIKE). If the function makes the distinction +** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to +** false. +*/ +SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ + FuncDef *pDef; + if( pExpr->op!=TK_FUNCTION + || !pExpr->x.pList + || pExpr->x.pList->nExpr!=2 + ){ + return 0; + } + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + pDef = sqlite3FindFunction(db, pExpr->u.zToken, 2, SQLITE_UTF8, 0); + if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ + return 0; + } + + /* The memcpy() statement assumes that the wildcard characters are + ** the first three statements in the compareInfo structure. The + ** asserts() that follow verify that assumption + */ + memcpy(aWc, pDef->pUserData, 3); + assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); + assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); + assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); + *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; + return 1; +} + +/* +** All of the FuncDef structures in the aBuiltinFunc[] array above +** to the global function hash table. This occurs at start-time (as +** a consequence of calling sqlite3_initialize()). +** +** After this routine runs +*/ +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){ + /* + ** The following array holds FuncDef structures for all of the functions + ** defined in this file. + ** + ** The array cannot be constant since changes are made to the + ** FuncDef.pHash elements at start-time. The elements of this array + ** are read-only after initialization is complete. + ** + ** For peak efficiency, put the most frequently used function last. + */ + static FuncDef aBuiltinFunc[] = { +#ifdef SQLITE_SOUNDEX + FUNCTION(soundex, 1, 0, 0, soundexFunc ), +#endif +#ifndef SQLITE_OMIT_LOAD_EXTENSION + VFUNCTION(load_extension, 1, 0, 0, loadExt ), + VFUNCTION(load_extension, 2, 0, 0, loadExt ), +#endif +#if SQLITE_USER_AUTHENTICATION + FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), +#endif +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), + DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), + FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), + FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), + FUNCTION(ltrim, 1, 1, 0, trimFunc ), + FUNCTION(ltrim, 2, 1, 0, trimFunc ), + FUNCTION(rtrim, 1, 2, 0, trimFunc ), + FUNCTION(rtrim, 2, 2, 0, trimFunc ), + FUNCTION(trim, 1, 3, 0, trimFunc ), + FUNCTION(trim, 2, 3, 0, trimFunc ), + FUNCTION(min, -1, 0, 1, minmaxFunc ), + FUNCTION(min, 0, 0, 1, 0 ), + AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize, + SQLITE_FUNC_MINMAX ), + FUNCTION(max, -1, 1, 1, minmaxFunc ), + FUNCTION(max, 0, 1, 1, 0 ), + AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize, + SQLITE_FUNC_MINMAX ), + FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), + FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), + FUNCTION(instr, 2, 0, 0, instrFunc ), + FUNCTION(printf, -1, 0, 0, printfFunc ), + FUNCTION(unicode, 1, 0, 0, unicodeFunc ), + FUNCTION(char, -1, 0, 0, charFunc ), + FUNCTION(abs, 1, 0, 0, absFunc ), +#ifndef SQLITE_OMIT_FLOATING_POINT + FUNCTION(round, 1, 0, 0, roundFunc ), + FUNCTION(round, 2, 0, 0, roundFunc ), +#endif + FUNCTION(upper, 1, 0, 0, upperFunc ), + FUNCTION(lower, 1, 0, 0, lowerFunc ), + FUNCTION(hex, 1, 0, 0, hexFunc ), + FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), + VFUNCTION(random, 0, 0, 0, randomFunc ), + VFUNCTION(randomblob, 1, 0, 0, randomBlob ), + FUNCTION(nullif, 2, 0, 1, nullifFunc ), + DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), + DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), + FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), + FUNCTION(quote, 1, 0, 0, quoteFunc ), + VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), + VFUNCTION(changes, 0, 0, 0, changes ), + VFUNCTION(total_changes, 0, 0, 0, total_changes ), + FUNCTION(replace, 3, 0, 0, replaceFunc ), + FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), + FUNCTION(substr, 2, 0, 0, substrFunc ), + FUNCTION(substr, 3, 0, 0, substrFunc ), + AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), + AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), + AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), + AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, + SQLITE_FUNC_COUNT ), + AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), + AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), + AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), + + LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + #ifdef SQLITE_CASE_SENSITIVE_LIKE + LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + #else + LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), + LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), + #endif + FUNCTION(coalesce, 1, 0, 0, 0 ), + FUNCTION(coalesce, 0, 0, 0, 0 ), + FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), + }; +#ifndef SQLITE_OMIT_ALTERTABLE + sqlite3AlterFunctions(); +#endif +#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) + sqlite3AnalyzeFunctions(); +#endif + sqlite3RegisterDateTimeFunctions(); + sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); + +#if 0 /* Enable to print out how the built-in functions are hashed */ + { + int i; + FuncDef *p; + for(i=0; iu.pHash){ + int n = sqlite3Strlen30(p->zName); + int h = p->zName[0] + n; + printf(" %s(%d)", p->zName, h); + } + printf("\n"); + } + } +#endif +} + +/************** End of func.c ************************************************/ +/************** Begin file fkey.c ********************************************/ +/* +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used by the compiler to add foreign key +** support to compiled SQL statements. +*/ +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_OMIT_FOREIGN_KEY +#ifndef SQLITE_OMIT_TRIGGER + +/* +** Deferred and Immediate FKs +** -------------------------- +** +** Foreign keys in SQLite come in two flavours: deferred and immediate. +** If an immediate foreign key constraint is violated, +** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current +** statement transaction rolled back. If a +** deferred foreign key constraint is violated, no action is taken +** immediately. However if the application attempts to commit the +** transaction before fixing the constraint violation, the attempt fails. +** +** Deferred constraints are implemented using a simple counter associated +** with the database handle. The counter is set to zero each time a +** database transaction is opened. Each time a statement is executed +** that causes a foreign key violation, the counter is incremented. Each +** time a statement is executed that removes an existing violation from +** the database, the counter is decremented. When the transaction is +** committed, the commit fails if the current value of the counter is +** greater than zero. This scheme has two big drawbacks: +** +** * When a commit fails due to a deferred foreign key constraint, +** there is no way to tell which foreign constraint is not satisfied, +** or which row it is not satisfied for. +** +** * If the database contains foreign key violations when the +** transaction is opened, this may cause the mechanism to malfunction. +** +** Despite these problems, this approach is adopted as it seems simpler +** than the alternatives. +** +** INSERT operations: +** +** I.1) For each FK for which the table is the child table, search +** the parent table for a match. If none is found increment the +** constraint counter. +** +** I.2) For each FK for which the table is the parent table, +** search the child table for rows that correspond to the new +** row in the parent table. Decrement the counter for each row +** found (as the constraint is now satisfied). +** +** DELETE operations: +** +** D.1) For each FK for which the table is the child table, +** search the parent table for a row that corresponds to the +** deleted row in the child table. If such a row is not found, +** decrement the counter. +** +** D.2) For each FK for which the table is the parent table, search +** the child table for rows that correspond to the deleted row +** in the parent table. For each found increment the counter. +** +** UPDATE operations: +** +** An UPDATE command requires that all 4 steps above are taken, but only +** for FK constraints for which the affected columns are actually +** modified (values must be compared at runtime). +** +** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2. +** This simplifies the implementation a bit. +** +** For the purposes of immediate FK constraints, the OR REPLACE conflict +** resolution is considered to delete rows before the new row is inserted. +** If a delete caused by OR REPLACE violates an FK constraint, an exception +** is thrown, even if the FK constraint would be satisfied after the new +** row is inserted. +** +** Immediate constraints are usually handled similarly. The only difference +** is that the counter used is stored as part of each individual statement +** object (struct Vdbe). If, after the statement has run, its immediate +** constraint counter is greater than zero, +** it returns SQLITE_CONSTRAINT_FOREIGNKEY +** and the statement transaction is rolled back. An exception is an INSERT +** statement that inserts a single row only (no triggers). In this case, +** instead of using a counter, an exception is thrown immediately if the +** INSERT violates a foreign key constraint. This is necessary as such +** an INSERT does not open a statement transaction. +** +** TODO: How should dropping a table be handled? How should renaming a +** table be handled? +** +** +** Query API Notes +** --------------- +** +** Before coding an UPDATE or DELETE row operation, the code-generator +** for those two operations needs to know whether or not the operation +** requires any FK processing and, if so, which columns of the original +** row are required by the FK processing VDBE code (i.e. if FKs were +** implemented using triggers, which of the old.* columns would be +** accessed). No information is required by the code-generator before +** coding an INSERT operation. The functions used by the UPDATE/DELETE +** generation code to query for this information are: +** +** sqlite3FkRequired() - Test to see if FK processing is required. +** sqlite3FkOldmask() - Query for the set of required old.* columns. +** +** +** Externally accessible module functions +** -------------------------------------- +** +** sqlite3FkCheck() - Check for foreign key violations. +** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions. +** sqlite3FkDelete() - Delete an FKey structure. +*/ + +/* +** VDBE Calling Convention +** ----------------------- +** +** Example: +** +** For the following INSERT statement: +** +** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c); +** INSERT INTO t1 VALUES(1, 2, 3.1); +** +** Register (x): 2 (type integer) +** Register (x+1): 1 (type integer) +** Register (x+2): NULL (type NULL) +** Register (x+3): 3.1 (type real) +*/ + +/* +** A foreign key constraint requires that the key columns in the parent +** table are collectively subject to a UNIQUE or PRIMARY KEY constraint. +** Given that pParent is the parent table for foreign key constraint pFKey, +** search the schema for a unique index on the parent key columns. +** +** If successful, zero is returned. If the parent key is an INTEGER PRIMARY +** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx +** is set to point to the unique index. +** +** If the parent key consists of a single column (the foreign key constraint +** is not a composite foreign key), output variable *paiCol is set to NULL. +** Otherwise, it is set to point to an allocated array of size N, where +** N is the number of columns in the parent key. The first element of the +** array is the index of the child table column that is mapped by the FK +** constraint to the parent table column stored in the left-most column +** of index *ppIdx. The second element of the array is the index of the +** child table column that corresponds to the second left-most column of +** *ppIdx, and so on. +** +** If the required index cannot be found, either because: +** +** 1) The named parent key columns do not exist, or +** +** 2) The named parent key columns do exist, but are not subject to a +** UNIQUE or PRIMARY KEY constraint, or +** +** 3) No parent key columns were provided explicitly as part of the +** foreign key definition, and the parent table does not have a +** PRIMARY KEY, or +** +** 4) No parent key columns were provided explicitly as part of the +** foreign key definition, and the PRIMARY KEY of the parent table +** consists of a different number of columns to the child key in +** the child table. +** +** then non-zero is returned, and a "foreign key mismatch" error loaded +** into pParse. If an OOM error occurs, non-zero is returned and the +** pParse->db->mallocFailed flag is set. +*/ +SQLITE_PRIVATE int sqlite3FkLocateIndex( + Parse *pParse, /* Parse context to store any error in */ + Table *pParent, /* Parent table of FK constraint pFKey */ + FKey *pFKey, /* Foreign key to find index for */ + Index **ppIdx, /* OUT: Unique index on parent table */ + int **paiCol /* OUT: Map of index columns in pFKey */ +){ + Index *pIdx = 0; /* Value to return via *ppIdx */ + int *aiCol = 0; /* Value to return via *paiCol */ + int nCol = pFKey->nCol; /* Number of columns in parent key */ + char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */ + + /* The caller is responsible for zeroing output parameters. */ + assert( ppIdx && *ppIdx==0 ); + assert( !paiCol || *paiCol==0 ); + assert( pParse ); + + /* If this is a non-composite (single column) foreign key, check if it + ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx + ** and *paiCol set to zero and return early. + ** + ** Otherwise, for a composite foreign key (more than one column), allocate + ** space for the aiCol array (returned via output parameter *paiCol). + ** Non-composite foreign keys do not require the aiCol array. + */ + if( nCol==1 ){ + /* The FK maps to the IPK if any of the following are true: + ** + ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly + ** mapped to the primary key of table pParent, or + ** 2) The FK is explicitly mapped to a column declared as INTEGER + ** PRIMARY KEY. + */ + if( pParent->iPKey>=0 ){ + if( !zKey ) return 0; + if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0; + } + }else if( paiCol ){ + assert( nCol>1 ); + aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int)); + if( !aiCol ) return 1; + *paiCol = aiCol; + } + + for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ + /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number + ** of columns. If each indexed column corresponds to a foreign key + ** column of pFKey, then this index is a winner. */ + + if( zKey==0 ){ + /* If zKey is NULL, then this foreign key is implicitly mapped to + ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be + ** identified by the test. */ + if( IsPrimaryKeyIndex(pIdx) ){ + if( aiCol ){ + int i; + for(i=0; iaCol[i].iFrom; + } + break; + } + }else{ + /* If zKey is non-NULL, then this foreign key was declared to + ** map to an explicit list of columns in table pParent. Check if this + ** index matches those columns. Also, check that the index uses + ** the default collation sequences for each column. */ + int i, j; + for(i=0; iaiColumn[i]; /* Index of column in parent tbl */ + const char *zDfltColl; /* Def. collation for column */ + char *zIdxCol; /* Name of indexed column */ + + if( iCol<0 ) break; /* No foreign keys against expression indexes */ + + /* If the index uses a collation sequence that is different from + ** the default collation sequence for the column, this index is + ** unusable. Bail out early in this case. */ + zDfltColl = pParent->aCol[iCol].zColl; + if( !zDfltColl ) zDfltColl = sqlite3StrBINARY; + if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break; + + zIdxCol = pParent->aCol[iCol].zName; + for(j=0; jaCol[j].zCol, zIdxCol)==0 ){ + if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom; + break; + } + } + if( j==nCol ) break; + } + if( i==nCol ) break; /* pIdx is usable */ + } + } + } + + if( !pIdx ){ + if( !pParse->disableTriggers ){ + sqlite3ErrorMsg(pParse, + "foreign key mismatch - \"%w\" referencing \"%w\"", + pFKey->pFrom->zName, pFKey->zTo); + } + sqlite3DbFree(pParse->db, aiCol); + return 1; + } + + *ppIdx = pIdx; + return 0; +} + +/* +** This function is called when a row is inserted into or deleted from the +** child table of foreign key constraint pFKey. If an SQL UPDATE is executed +** on the child table of pFKey, this function is invoked twice for each row +** affected - once to "delete" the old row, and then again to "insert" the +** new row. +** +** Each time it is called, this function generates VDBE code to locate the +** row in the parent table that corresponds to the row being inserted into +** or deleted from the child table. If the parent row can be found, no +** special action is taken. Otherwise, if the parent row can *not* be +** found in the parent table: +** +** Operation | FK type | Action taken +** -------------------------------------------------------------------------- +** INSERT immediate Increment the "immediate constraint counter". +** +** DELETE immediate Decrement the "immediate constraint counter". +** +** INSERT deferred Increment the "deferred constraint counter". +** +** DELETE deferred Decrement the "deferred constraint counter". +** +** These operations are identified in the comment at the top of this file +** (fkey.c) as "I.1" and "D.1". +*/ +static void fkLookupParent( + Parse *pParse, /* Parse context */ + int iDb, /* Index of database housing pTab */ + Table *pTab, /* Parent table of FK pFKey */ + Index *pIdx, /* Unique index on parent key columns in pTab */ + FKey *pFKey, /* Foreign key constraint */ + int *aiCol, /* Map from parent key columns to child table columns */ + int regData, /* Address of array containing child table row */ + int nIncr, /* Increment constraint counter by this */ + int isIgnore /* If true, pretend pTab contains all NULL values */ +){ + int i; /* Iterator variable */ + Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */ + int iCur = pParse->nTab - 1; /* Cursor number to use */ + int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */ + + /* If nIncr is less than zero, then check at runtime if there are any + ** outstanding constraints to resolve. If there are not, there is no need + ** to check if deleting this row resolves any outstanding violations. + ** + ** Check if any of the key columns in the child table row are NULL. If + ** any are, then the constraint is considered satisfied. No need to + ** search for a matching row in the parent table. */ + if( nIncr<0 ){ + sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk); + VdbeCoverage(v); + } + for(i=0; inCol; i++){ + int iReg = aiCol[i] + regData + 1; + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v); + } + + if( isIgnore==0 ){ + if( pIdx==0 ){ + /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY + ** column of the parent table (table pTab). */ + int iMustBeInt; /* Address of MustBeInt instruction */ + int regTemp = sqlite3GetTempReg(pParse); + + /* Invoke MustBeInt to coerce the child key value to an integer (i.e. + ** apply the affinity of the parent key). If this fails, then there + ** is no matching parent key. Before using MustBeInt, make a copy of + ** the value. Otherwise, the value inserted into the child key column + ** will have INTEGER affinity applied to it, which may not be correct. */ + sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp); + iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0); + VdbeCoverage(v); + + /* If the parent table is the same as the child table, and we are about + ** to increment the constraint-counter (i.e. this is an INSERT operation), + ** then check if the row being inserted matches itself. If so, do not + ** increment the constraint-counter. */ + if( pTab==pFKey->pFrom && nIncr==1 ){ + sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + } + + sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v); + sqlite3VdbeGoto(v, iOk); + sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + sqlite3VdbeJumpHere(v, iMustBeInt); + sqlite3ReleaseTempReg(pParse, regTemp); + }else{ + int nCol = pFKey->nCol; + int regTemp = sqlite3GetTempRange(pParse, nCol); + int regRec = sqlite3GetTempReg(pParse); + + sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + for(i=0; ipFrom && nIncr==1 ){ + int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1; + for(i=0; iaiColumn[i]+1+regData; + assert( pIdx->aiColumn[i]>=0 ); + assert( aiCol[i]!=pTab->iPKey ); + if( pIdx->aiColumn[i]==pTab->iPKey ){ + /* The parent key is a composite key that includes the IPK column */ + iParent = regData; + } + sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v); + sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); + } + sqlite3VdbeGoto(v, iOk); + } + + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec, + sqlite3IndexAffinityStr(pParse->db,pIdx), nCol); + sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v); + + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempRange(pParse, regTemp, nCol); + } + } + + if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs) + && !pParse->pToplevel + && !pParse->isMultiWrite + ){ + /* Special case: If this is an INSERT statement that will insert exactly + ** one row into the table, raise a constraint immediately instead of + ** incrementing a counter. This is necessary as the VM code is being + ** generated for will not open a statement transaction. */ + assert( nIncr==1 ); + sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY, + OE_Abort, 0, P4_STATIC, P5_ConstraintFK); + }else{ + if( nIncr>0 && pFKey->isDeferred==0 ){ + sqlite3MayAbort(pParse); + } + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); + } + + sqlite3VdbeResolveLabel(v, iOk); + sqlite3VdbeAddOp1(v, OP_Close, iCur); +} + + +/* +** Return an Expr object that refers to a memory register corresponding +** to column iCol of table pTab. +** +** regBase is the first of an array of register that contains the data +** for pTab. regBase itself holds the rowid. regBase+1 holds the first +** column. regBase+2 holds the second column, and so forth. +*/ +static Expr *exprTableRegister( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* The table whose content is at r[regBase]... */ + int regBase, /* Contents of table pTab */ + i16 iCol /* Which column of pTab is desired */ +){ + Expr *pExpr; + Column *pCol; + const char *zColl; + sqlite3 *db = pParse->db; + + pExpr = sqlite3Expr(db, TK_REGISTER, 0); + if( pExpr ){ + if( iCol>=0 && iCol!=pTab->iPKey ){ + pCol = &pTab->aCol[iCol]; + pExpr->iTable = regBase + iCol + 1; + pExpr->affinity = pCol->affinity; + zColl = pCol->zColl; + if( zColl==0 ) zColl = db->pDfltColl->zName; + pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl); + }else{ + pExpr->iTable = regBase; + pExpr->affinity = SQLITE_AFF_INTEGER; + } + } + return pExpr; +} + +/* +** Return an Expr object that refers to column iCol of table pTab which +** has cursor iCur. +*/ +static Expr *exprTableColumn( + sqlite3 *db, /* The database connection */ + Table *pTab, /* The table whose column is desired */ + int iCursor, /* The open cursor on the table */ + i16 iCol /* The column that is wanted */ +){ + Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0); + if( pExpr ){ + pExpr->pTab = pTab; + pExpr->iTable = iCursor; + pExpr->iColumn = iCol; + } + return pExpr; +} + +/* +** This function is called to generate code executed when a row is deleted +** from the parent table of foreign key constraint pFKey and, if pFKey is +** deferred, when a row is inserted into the same table. When generating +** code for an SQL UPDATE operation, this function may be called twice - +** once to "delete" the old row and once to "insert" the new row. +** +** Parameter nIncr is passed -1 when inserting a row (as this may decrease +** the number of FK violations in the db) or +1 when deleting one (as this +** may increase the number of FK constraint problems). +** +** The code generated by this function scans through the rows in the child +** table that correspond to the parent table row being deleted or inserted. +** For each child row found, one of the following actions is taken: +** +** Operation | FK type | Action taken +** -------------------------------------------------------------------------- +** DELETE immediate Increment the "immediate constraint counter". +** Or, if the ON (UPDATE|DELETE) action is RESTRICT, +** throw a "FOREIGN KEY constraint failed" exception. +** +** INSERT immediate Decrement the "immediate constraint counter". +** +** DELETE deferred Increment the "deferred constraint counter". +** Or, if the ON (UPDATE|DELETE) action is RESTRICT, +** throw a "FOREIGN KEY constraint failed" exception. +** +** INSERT deferred Decrement the "deferred constraint counter". +** +** These operations are identified in the comment at the top of this file +** (fkey.c) as "I.2" and "D.2". +*/ +static void fkScanChildren( + Parse *pParse, /* Parse context */ + SrcList *pSrc, /* The child table to be scanned */ + Table *pTab, /* The parent table */ + Index *pIdx, /* Index on parent covering the foreign key */ + FKey *pFKey, /* The foreign key linking pSrc to pTab */ + int *aiCol, /* Map from pIdx cols to child table cols */ + int regData, /* Parent row data starts here */ + int nIncr /* Amount to increment deferred counter by */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + int i; /* Iterator variable */ + Expr *pWhere = 0; /* WHERE clause to scan with */ + NameContext sNameContext; /* Context used to resolve WHERE clause */ + WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */ + int iFkIfZero = 0; /* Address of OP_FkIfZero */ + Vdbe *v = sqlite3GetVdbe(pParse); + + assert( pIdx==0 || pIdx->pTable==pTab ); + assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol ); + assert( pIdx!=0 || pFKey->nCol==1 ); + assert( pIdx!=0 || HasRowid(pTab) ); + + if( nIncr<0 ){ + iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0); + VdbeCoverage(v); + } + + /* Create an Expr object representing an SQL expression like: + ** + ** = AND = ... + ** + ** The collation sequence used for the comparison should be that of + ** the parent key columns. The affinity of the parent key column should + ** be applied to each child key value before the comparison takes place. + */ + for(i=0; inCol; i++){ + Expr *pLeft; /* Value from parent table row */ + Expr *pRight; /* Column ref to child table */ + Expr *pEq; /* Expression (pLeft = pRight) */ + i16 iCol; /* Index of column in child table */ + const char *zCol; /* Name of column in child table */ + + iCol = pIdx ? pIdx->aiColumn[i] : -1; + pLeft = exprTableRegister(pParse, pTab, regData, iCol); + iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; + assert( iCol>=0 ); + zCol = pFKey->pFrom->aCol[iCol].zName; + pRight = sqlite3Expr(db, TK_ID, zCol); + pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); + pWhere = sqlite3ExprAnd(db, pWhere, pEq); + } + + /* If the child table is the same as the parent table, then add terms + ** to the WHERE clause that prevent this entry from being scanned. + ** The added WHERE clause terms are like this: + ** + ** $current_rowid!=rowid + ** NOT( $current_a==a AND $current_b==b AND ... ) + ** + ** The first form is used for rowid tables. The second form is used + ** for WITHOUT ROWID tables. In the second form, the primary key is + ** (a,b,...) + */ + if( pTab==pFKey->pFrom && nIncr>0 ){ + Expr *pNe; /* Expression (pLeft != pRight) */ + Expr *pLeft; /* Value from parent table row */ + Expr *pRight; /* Column ref to child table */ + if( HasRowid(pTab) ){ + pLeft = exprTableRegister(pParse, pTab, regData, -1); + pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1); + pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0); + }else{ + Expr *pEq, *pAll = 0; + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pIdx!=0 ); + for(i=0; inKeyCol; i++){ + i16 iCol = pIdx->aiColumn[i]; + assert( iCol>=0 ); + pLeft = exprTableRegister(pParse, pTab, regData, iCol); + pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol); + pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); + pAll = sqlite3ExprAnd(db, pAll, pEq); + } + pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0); + } + pWhere = sqlite3ExprAnd(db, pWhere, pNe); + } + + /* Resolve the references in the WHERE clause. */ + memset(&sNameContext, 0, sizeof(NameContext)); + sNameContext.pSrcList = pSrc; + sNameContext.pParse = pParse; + sqlite3ResolveExprNames(&sNameContext, pWhere); + + /* Create VDBE to loop through the entries in pSrc that match the WHERE + ** clause. For each row found, increment either the deferred or immediate + ** foreign key constraint counter. */ + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0); + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); + if( pWInfo ){ + sqlite3WhereEnd(pWInfo); + } + + /* Clean up the WHERE clause constructed above. */ + sqlite3ExprDelete(db, pWhere); + if( iFkIfZero ){ + sqlite3VdbeJumpHere(v, iFkIfZero); + } +} + +/* +** This function returns a linked list of FKey objects (connected by +** FKey.pNextTo) holding all children of table pTab. For example, +** given the following schema: +** +** CREATE TABLE t1(a PRIMARY KEY); +** CREATE TABLE t2(b REFERENCES t1(a); +** +** Calling this function with table "t1" as an argument returns a pointer +** to the FKey structure representing the foreign key constraint on table +** "t2". Calling this function with "t2" as the argument would return a +** NULL pointer (as there are no FK constraints for which t2 is the parent +** table). +*/ +SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){ + return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName); +} + +/* +** The second argument is a Trigger structure allocated by the +** fkActionTrigger() routine. This function deletes the Trigger structure +** and all of its sub-components. +** +** The Trigger structure or any of its sub-components may be allocated from +** the lookaside buffer belonging to database handle dbMem. +*/ +static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){ + if( p ){ + TriggerStep *pStep = p->step_list; + sqlite3ExprDelete(dbMem, pStep->pWhere); + sqlite3ExprListDelete(dbMem, pStep->pExprList); + sqlite3SelectDelete(dbMem, pStep->pSelect); + sqlite3ExprDelete(dbMem, p->pWhen); + sqlite3DbFree(dbMem, p); + } +} + +/* +** This function is called to generate code that runs when table pTab is +** being dropped from the database. The SrcList passed as the second argument +** to this function contains a single entry guaranteed to resolve to +** table pTab. +** +** Normally, no code is required. However, if either +** +** (a) The table is the parent table of a FK constraint, or +** (b) The table is the child table of a deferred FK constraint and it is +** determined at runtime that there are outstanding deferred FK +** constraint violations in the database, +** +** then the equivalent of "DELETE FROM " is executed before dropping +** the table from the database. Triggers are disabled while running this +** DELETE, but foreign key actions are not. +*/ +SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ + sqlite3 *db = pParse->db; + if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){ + int iSkip = 0; + Vdbe *v = sqlite3GetVdbe(pParse); + + assert( v ); /* VDBE has already been allocated */ + if( sqlite3FkReferences(pTab)==0 ){ + /* Search for a deferred foreign key constraint for which this table + ** is the child table. If one cannot be found, return without + ** generating any VDBE code. If one can be found, then jump over + ** the entire DELETE if there are no outstanding deferred constraints + ** when this statement is run. */ + FKey *p; + for(p=pTab->pFKey; p; p=p->pNextFrom){ + if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break; + } + if( !p ) return; + iSkip = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v); + } + + pParse->disableTriggers = 1; + sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0); + pParse->disableTriggers = 0; + + /* If the DELETE has generated immediate foreign key constraint + ** violations, halt the VDBE and return an error at this point, before + ** any modifications to the schema are made. This is because statement + ** transactions are not able to rollback schema changes. + ** + ** If the SQLITE_DeferFKs flag is set, then this is not required, as + ** the statement transaction will not be rolled back even if FK + ** constraints are violated. + */ + if( (db->flags & SQLITE_DeferFKs)==0 ){ + sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY, + OE_Abort, 0, P4_STATIC, P5_ConstraintFK); + } + + if( iSkip ){ + sqlite3VdbeResolveLabel(v, iSkip); + } + } +} + + +/* +** The second argument points to an FKey object representing a foreign key +** for which pTab is the child table. An UPDATE statement against pTab +** is currently being processed. For each column of the table that is +** actually updated, the corresponding element in the aChange[] array +** is zero or greater (if a column is unmodified the corresponding element +** is set to -1). If the rowid column is modified by the UPDATE statement +** the bChngRowid argument is non-zero. +** +** This function returns true if any of the columns that are part of the +** child key for FK constraint *p are modified. +*/ +static int fkChildIsModified( + Table *pTab, /* Table being updated */ + FKey *p, /* Foreign key for which pTab is the child */ + int *aChange, /* Array indicating modified columns */ + int bChngRowid /* True if rowid is modified by this update */ +){ + int i; + for(i=0; inCol; i++){ + int iChildKey = p->aCol[i].iFrom; + if( aChange[iChildKey]>=0 ) return 1; + if( iChildKey==pTab->iPKey && bChngRowid ) return 1; + } + return 0; +} + +/* +** The second argument points to an FKey object representing a foreign key +** for which pTab is the parent table. An UPDATE statement against pTab +** is currently being processed. For each column of the table that is +** actually updated, the corresponding element in the aChange[] array +** is zero or greater (if a column is unmodified the corresponding element +** is set to -1). If the rowid column is modified by the UPDATE statement +** the bChngRowid argument is non-zero. +** +** This function returns true if any of the columns that are part of the +** parent key for FK constraint *p are modified. +*/ +static int fkParentIsModified( + Table *pTab, + FKey *p, + int *aChange, + int bChngRowid +){ + int i; + for(i=0; inCol; i++){ + char *zKey = p->aCol[i].zCol; + int iKey; + for(iKey=0; iKeynCol; iKey++){ + if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){ + Column *pCol = &pTab->aCol[iKey]; + if( zKey ){ + if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1; + }else if( pCol->colFlags & COLFLAG_PRIMKEY ){ + return 1; + } + } + } + } + return 0; +} + +/* +** Return true if the parser passed as the first argument is being +** used to code a trigger that is really a "SET NULL" action belonging +** to trigger pFKey. +*/ +static int isSetNullAction(Parse *pParse, FKey *pFKey){ + Parse *pTop = sqlite3ParseToplevel(pParse); + if( pTop->pTriggerPrg ){ + Trigger *p = pTop->pTriggerPrg->pTrigger; + if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull) + || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull) + ){ + return 1; + } + } + return 0; +} + +/* +** This function is called when inserting, deleting or updating a row of +** table pTab to generate VDBE code to perform foreign key constraint +** processing for the operation. +** +** For a DELETE operation, parameter regOld is passed the index of the +** first register in an array of (pTab->nCol+1) registers containing the +** rowid of the row being deleted, followed by each of the column values +** of the row being deleted, from left to right. Parameter regNew is passed +** zero in this case. +** +** For an INSERT operation, regOld is passed zero and regNew is passed the +** first register of an array of (pTab->nCol+1) registers containing the new +** row data. +** +** For an UPDATE operation, this function is called twice. Once before +** the original record is deleted from the table using the calling convention +** described for DELETE. Then again after the original record is deleted +** but before the new record is inserted using the INSERT convention. +*/ +SQLITE_PRIVATE void sqlite3FkCheck( + Parse *pParse, /* Parse context */ + Table *pTab, /* Row is being deleted from this table */ + int regOld, /* Previous row data is stored here */ + int regNew, /* New row data is stored here */ + int *aChange, /* Array indicating UPDATEd columns (or 0) */ + int bChngRowid /* True if rowid is UPDATEd */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + FKey *pFKey; /* Used to iterate through FKs */ + int iDb; /* Index of database containing pTab */ + const char *zDb; /* Name of database containing pTab */ + int isIgnoreErrors = pParse->disableTriggers; + + /* Exactly one of regOld and regNew should be non-zero. */ + assert( (regOld==0)!=(regNew==0) ); + + /* If foreign-keys are disabled, this function is a no-op. */ + if( (db->flags&SQLITE_ForeignKeys)==0 ) return; + + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + zDb = db->aDb[iDb].zName; + + /* Loop through all the foreign key constraints for which pTab is the + ** child table (the table that the foreign key definition is part of). */ + for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + Table *pTo; /* Parent table of foreign key pFKey */ + Index *pIdx = 0; /* Index on key columns in pTo */ + int *aiFree = 0; + int *aiCol; + int iCol; + int i; + int bIgnore = 0; + + if( aChange + && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0 + && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0 + ){ + continue; + } + + /* Find the parent table of this foreign key. Also find a unique index + ** on the parent key columns in the parent table. If either of these + ** schema items cannot be located, set an error in pParse and return + ** early. */ + if( pParse->disableTriggers ){ + pTo = sqlite3FindTable(db, pFKey->zTo, zDb); + }else{ + pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); + } + if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ + assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) ); + if( !isIgnoreErrors || db->mallocFailed ) return; + if( pTo==0 ){ + /* If isIgnoreErrors is true, then a table is being dropped. In this + ** case SQLite runs a "DELETE FROM xxx" on the table being dropped + ** before actually dropping it in order to check FK constraints. + ** If the parent table of an FK constraint on the current table is + ** missing, behave as if it is empty. i.e. decrement the relevant + ** FK counter for each row of the current table with non-NULL keys. + */ + Vdbe *v = sqlite3GetVdbe(pParse); + int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; + for(i=0; inCol; i++){ + int iReg = pFKey->aCol[i].iFrom + regOld + 1; + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v); + } + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); + } + continue; + } + assert( pFKey->nCol==1 || (aiFree && pIdx) ); + + if( aiFree ){ + aiCol = aiFree; + }else{ + iCol = pFKey->aCol[0].iFrom; + aiCol = &iCol; + } + for(i=0; inCol; i++){ + if( aiCol[i]==pTab->iPKey ){ + aiCol[i] = -1; + } + assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Request permission to read the parent key columns. If the + ** authorization callback returns SQLITE_IGNORE, behave as if any + ** values read from the parent table are NULL. */ + if( db->xAuth ){ + int rcauth; + char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName; + rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb); + bIgnore = (rcauth==SQLITE_IGNORE); + } +#endif + } + + /* Take a shared-cache advisory read-lock on the parent table. Allocate + ** a cursor to use to search the unique index on the parent key columns + ** in the parent table. */ + sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName); + pParse->nTab++; + + if( regOld!=0 ){ + /* A row is being removed from the child table. Search for the parent. + ** If the parent does not exist, removing the child row resolves an + ** outstanding foreign key constraint violation. */ + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore); + } + if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){ + /* A row is being added to the child table. If a parent row cannot + ** be found, adding the child row has violated the FK constraint. + ** + ** If this operation is being performed as part of a trigger program + ** that is actually a "SET NULL" action belonging to this very + ** foreign key, then omit this scan altogether. As all child key + ** values are guaranteed to be NULL, it is not possible for adding + ** this row to cause an FK violation. */ + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore); + } + + sqlite3DbFree(db, aiFree); + } + + /* Loop through all the foreign key constraints that refer to this table. + ** (the "child" constraints) */ + for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ + Index *pIdx = 0; /* Foreign key index for pFKey */ + SrcList *pSrc; + int *aiCol = 0; + + if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){ + continue; + } + + if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs) + && !pParse->pToplevel && !pParse->isMultiWrite + ){ + assert( regOld==0 && regNew!=0 ); + /* Inserting a single row into a parent table cannot cause (or fix) + ** an immediate foreign key violation. So do nothing in this case. */ + continue; + } + + if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){ + if( !isIgnoreErrors || db->mallocFailed ) return; + continue; + } + assert( aiCol || pFKey->nCol==1 ); + + /* Create a SrcList structure containing the child table. We need the + ** child table as a SrcList for sqlite3WhereBegin() */ + pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + if( pSrc ){ + struct SrcList_item *pItem = pSrc->a; + pItem->pTab = pFKey->pFrom; + pItem->zName = pFKey->pFrom->zName; + pItem->pTab->nRef++; + pItem->iCursor = pParse->nTab++; + + if( regNew!=0 ){ + fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1); + } + if( regOld!=0 ){ + int eAction = pFKey->aAction[aChange!=0]; + fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1); + /* If this is a deferred FK constraint, or a CASCADE or SET NULL + ** action applies, then any foreign key violations caused by + ** removing the parent key will be rectified by the action trigger. + ** So do not set the "may-abort" flag in this case. + ** + ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the + ** may-abort flag will eventually be set on this statement anyway + ** (when this function is called as part of processing the UPDATE + ** within the action trigger). + ** + ** Note 2: At first glance it may seem like SQLite could simply omit + ** all OP_FkCounter related scans when either CASCADE or SET NULL + ** applies. The trouble starts if the CASCADE or SET NULL action + ** trigger causes other triggers or action rules attached to the + ** child table to fire. In these cases the fk constraint counters + ** might be set incorrectly if any OP_FkCounter related scans are + ** omitted. */ + if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){ + sqlite3MayAbort(pParse); + } + } + pItem->zName = 0; + sqlite3SrcListDelete(db, pSrc); + } + sqlite3DbFree(db, aiCol); + } +} + +#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x))) + +/* +** This function is called before generating code to update or delete a +** row contained in table pTab. +*/ +SQLITE_PRIVATE u32 sqlite3FkOldmask( + Parse *pParse, /* Parse context */ + Table *pTab /* Table being modified */ +){ + u32 mask = 0; + if( pParse->db->flags&SQLITE_ForeignKeys ){ + FKey *p; + int i; + for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(i=0; inCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom); + } + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + Index *pIdx = 0; + sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0); + if( pIdx ){ + for(i=0; inKeyCol; i++){ + assert( pIdx->aiColumn[i]>=0 ); + mask |= COLUMN_MASK(pIdx->aiColumn[i]); + } + } + } + } + return mask; +} + + +/* +** This function is called before generating code to update or delete a +** row contained in table pTab. If the operation is a DELETE, then +** parameter aChange is passed a NULL value. For an UPDATE, aChange points +** to an array of size N, where N is the number of columns in table pTab. +** If the i'th column is not modified by the UPDATE, then the corresponding +** entry in the aChange[] array is set to -1. If the column is modified, +** the value is 0 or greater. Parameter chngRowid is set to true if the +** UPDATE statement modifies the rowid fields of the table. +** +** If any foreign key processing will be required, this function returns +** true. If there is no foreign key related processing, this function +** returns false. +*/ +SQLITE_PRIVATE int sqlite3FkRequired( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being modified */ + int *aChange, /* Non-NULL for UPDATE operations */ + int chngRowid /* True for UPDATE that affects rowid */ +){ + if( pParse->db->flags&SQLITE_ForeignKeys ){ + if( !aChange ){ + /* A DELETE operation. Foreign key processing is required if the + ** table in question is either the child or parent table for any + ** foreign key constraint. */ + return (sqlite3FkReferences(pTab) || pTab->pFKey); + }else{ + /* This is an UPDATE. Foreign key processing is only required if the + ** operation modifies one or more child or parent key columns. */ + FKey *p; + + /* Check if any child key columns are being modified. */ + for(p=pTab->pFKey; p; p=p->pNextFrom){ + if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1; + } + + /* Check if any parent key columns are being modified. */ + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1; + } + } + } + return 0; +} + +/* +** This function is called when an UPDATE or DELETE operation is being +** compiled on table pTab, which is the parent table of foreign-key pFKey. +** If the current operation is an UPDATE, then the pChanges parameter is +** passed a pointer to the list of columns being modified. If it is a +** DELETE, pChanges is passed a NULL pointer. +** +** It returns a pointer to a Trigger structure containing a trigger +** equivalent to the ON UPDATE or ON DELETE action specified by pFKey. +** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is +** returned (these actions require no special handling by the triggers +** sub-system, code for them is created by fkScanChildren()). +** +** For example, if pFKey is the foreign key and pTab is table "p" in +** the following schema: +** +** CREATE TABLE p(pk PRIMARY KEY); +** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE); +** +** then the returned trigger structure is equivalent to: +** +** CREATE TRIGGER ... DELETE ON p BEGIN +** DELETE FROM c WHERE ck = old.pk; +** END; +** +** The returned pointer is cached as part of the foreign key object. It +** is eventually freed along with the rest of the foreign key object by +** sqlite3FkDelete(). +*/ +static Trigger *fkActionTrigger( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being updated or deleted from */ + FKey *pFKey, /* Foreign key to get action for */ + ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + int action; /* One of OE_None, OE_Cascade etc. */ + Trigger *pTrigger; /* Trigger definition to return */ + int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */ + + action = pFKey->aAction[iAction]; + if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){ + return 0; + } + pTrigger = pFKey->apTrigger[iAction]; + + if( action!=OE_None && !pTrigger ){ + char const *zFrom; /* Name of child table */ + int nFrom; /* Length in bytes of zFrom */ + Index *pIdx = 0; /* Parent key index for this FK */ + int *aiCol = 0; /* child table cols -> parent key cols */ + TriggerStep *pStep = 0; /* First (only) step of trigger program */ + Expr *pWhere = 0; /* WHERE clause of trigger step */ + ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */ + Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */ + int i; /* Iterator variable */ + Expr *pWhen = 0; /* WHEN clause for the trigger */ + + if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0; + assert( aiCol || pFKey->nCol==1 ); + + for(i=0; inCol; i++){ + Token tOld = { "old", 3 }; /* Literal "old" token */ + Token tNew = { "new", 3 }; /* Literal "new" token */ + Token tFromCol; /* Name of column in child table */ + Token tToCol; /* Name of column in parent table */ + int iFromCol; /* Idx of column in child table */ + Expr *pEq; /* tFromCol = OLD.tToCol */ + + iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; + assert( iFromCol>=0 ); + assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKeynCol) ); + assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); + sqlite3TokenInit(&tToCol, + pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName); + sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName); + + /* Create the expression "OLD.zToCol = zFromCol". It is important + ** that the "OLD.zToCol" term is on the LHS of the = operator, so + ** that the affinity and collation sequence associated with the + ** parent table are used for the comparison. */ + pEq = sqlite3PExpr(pParse, TK_EQ, + sqlite3PExpr(pParse, TK_DOT, + sqlite3ExprAlloc(db, TK_ID, &tOld, 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) + , 0), + sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0) + , 0); + pWhere = sqlite3ExprAnd(db, pWhere, pEq); + + /* For ON UPDATE, construct the next term of the WHEN clause. + ** The final WHEN clause will be like this: + ** + ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN) + */ + if( pChanges ){ + pEq = sqlite3PExpr(pParse, TK_IS, + sqlite3PExpr(pParse, TK_DOT, + sqlite3ExprAlloc(db, TK_ID, &tOld, 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), + 0), + sqlite3PExpr(pParse, TK_DOT, + sqlite3ExprAlloc(db, TK_ID, &tNew, 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), + 0), + 0); + pWhen = sqlite3ExprAnd(db, pWhen, pEq); + } + + if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){ + Expr *pNew; + if( action==OE_Cascade ){ + pNew = sqlite3PExpr(pParse, TK_DOT, + sqlite3ExprAlloc(db, TK_ID, &tNew, 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) + , 0); + }else if( action==OE_SetDflt ){ + Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt; + if( pDflt ){ + pNew = sqlite3ExprDup(db, pDflt, 0); + }else{ + pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + } + }else{ + pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + } + pList = sqlite3ExprListAppend(pParse, pList, pNew); + sqlite3ExprListSetName(pParse, pList, &tFromCol, 0); + } + } + sqlite3DbFree(db, aiCol); + + zFrom = pFKey->pFrom->zName; + nFrom = sqlite3Strlen30(zFrom); + + if( action==OE_Restrict ){ + Token tFrom; + Expr *pRaise; + + tFrom.z = zFrom; + tFrom.n = nFrom; + pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed"); + if( pRaise ){ + pRaise->affinity = OE_Abort; + } + pSelect = sqlite3SelectNew(pParse, + sqlite3ExprListAppend(pParse, 0, pRaise), + sqlite3SrcListAppend(db, 0, &tFrom, 0), + pWhere, + 0, 0, 0, 0, 0, 0 + ); + pWhere = 0; + } + + /* Disable lookaside memory allocation */ + db->lookaside.bDisable++; + + pTrigger = (Trigger *)sqlite3DbMallocZero(db, + sizeof(Trigger) + /* struct Trigger */ + sizeof(TriggerStep) + /* Single step in trigger program */ + nFrom + 1 /* Space for pStep->zTarget */ + ); + if( pTrigger ){ + pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1]; + pStep->zTarget = (char *)&pStep[1]; + memcpy((char *)pStep->zTarget, zFrom, nFrom); + + pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE); + pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + if( pWhen ){ + pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0); + pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); + } + } + + /* Re-enable the lookaside buffer, if it was disabled earlier. */ + db->lookaside.bDisable--; + + sqlite3ExprDelete(db, pWhere); + sqlite3ExprDelete(db, pWhen); + sqlite3ExprListDelete(db, pList); + sqlite3SelectDelete(db, pSelect); + if( db->mallocFailed==1 ){ + fkTriggerDelete(db, pTrigger); + return 0; + } + assert( pStep!=0 ); + + switch( action ){ + case OE_Restrict: + pStep->op = TK_SELECT; + break; + case OE_Cascade: + if( !pChanges ){ + pStep->op = TK_DELETE; + break; + } + default: + pStep->op = TK_UPDATE; + } + pStep->pTrig = pTrigger; + pTrigger->pSchema = pTab->pSchema; + pTrigger->pTabSchema = pTab->pSchema; + pFKey->apTrigger[iAction] = pTrigger; + pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE); + } + + return pTrigger; +} + +/* +** This function is called when deleting or updating a row to implement +** any required CASCADE, SET NULL or SET DEFAULT actions. +*/ +SQLITE_PRIVATE void sqlite3FkActions( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being updated or deleted from */ + ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */ + int regOld, /* Address of array containing old row */ + int *aChange, /* Array indicating UPDATEd columns (or 0) */ + int bChngRowid /* True if rowid is UPDATEd */ +){ + /* If foreign-key support is enabled, iterate through all FKs that + ** refer to table pTab. If there is an action associated with the FK + ** for this operation (either update or delete), invoke the associated + ** trigger sub-program. */ + if( pParse->db->flags&SQLITE_ForeignKeys ){ + FKey *pFKey; /* Iterator variable */ + for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ + if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){ + Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges); + if( pAct ){ + sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0); + } + } + } + } +} + +#endif /* ifndef SQLITE_OMIT_TRIGGER */ + +/* +** Free all memory associated with foreign key definitions attached to +** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash +** hash table. +*/ +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){ + FKey *pFKey; /* Iterator variable */ + FKey *pNext; /* Copy of pFKey->pNextFrom */ + + assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) ); + for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){ + + /* Remove the FK from the fkeyHash hash table. */ + if( !db || db->pnBytesFreed==0 ){ + if( pFKey->pPrevTo ){ + pFKey->pPrevTo->pNextTo = pFKey->pNextTo; + }else{ + void *p = (void *)pFKey->pNextTo; + const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo); + sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p); + } + if( pFKey->pNextTo ){ + pFKey->pNextTo->pPrevTo = pFKey->pPrevTo; + } + } + + /* EV: R-30323-21917 Each foreign key constraint in SQLite is + ** classified as either immediate or deferred. + */ + assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 ); + + /* Delete any triggers created to implement actions for this FK. */ +#ifndef SQLITE_OMIT_TRIGGER + fkTriggerDelete(db, pFKey->apTrigger[0]); + fkTriggerDelete(db, pFKey->apTrigger[1]); +#endif + + pNext = pFKey->pNextFrom; + sqlite3DbFree(db, pFKey); + } +} +#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */ + +/************** End of fkey.c ************************************************/ +/************** Begin file insert.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle INSERT statements in SQLite. +*/ +/* #include "sqliteInt.h" */ + +/* +** Generate code that will +** +** (1) acquire a lock for table pTab then +** (2) open pTab as cursor iCur. +** +** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index +** for that table that is actually opened. +*/ +SQLITE_PRIVATE void sqlite3OpenTable( + Parse *pParse, /* Generate code into this VDBE */ + int iCur, /* The cursor number of the table */ + int iDb, /* The database index in sqlite3.aDb[] */ + Table *pTab, /* The table to be opened */ + int opcode /* OP_OpenRead or OP_OpenWrite */ +){ + Vdbe *v; + assert( !IsVirtual(pTab) ); + v = sqlite3GetVdbe(pParse); + assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); + sqlite3TableLock(pParse, iDb, pTab->tnum, + (opcode==OP_OpenWrite)?1:0, pTab->zName); + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); + VdbeComment((v, "%s", pTab->zName)); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + assert( pPk->tnum==pTab->tnum ); + sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + VdbeComment((v, "%s", pTab->zName)); + } +} + +/* +** Return a pointer to the column affinity string associated with index +** pIdx. A column affinity string has one character for each column in +** the table, according to the affinity of the column: +** +** Character Column affinity +** ------------------------------ +** 'A' BLOB +** 'B' TEXT +** 'C' NUMERIC +** 'D' INTEGER +** 'F' REAL +** +** An extra 'D' is appended to the end of the string to cover the +** rowid that appears as the last column in every index. +** +** Memory for the buffer containing the column index affinity string +** is managed along with the rest of the Index structure. It will be +** released when sqlite3DeleteIndex() is called. +*/ +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ + if( !pIdx->zColAff ){ + /* The first time a column affinity string for a particular index is + ** required, it is allocated and populated here. It is then stored as + ** a member of the Index structure for subsequent use. + ** + ** The column affinity string will eventually be deleted by + ** sqliteDeleteIndex() when the Index structure itself is cleaned + ** up. + */ + int n; + Table *pTab = pIdx->pTable; + pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); + if( !pIdx->zColAff ){ + sqlite3OomFault(db); + return 0; + } + for(n=0; nnColumn; n++){ + i16 x = pIdx->aiColumn[n]; + if( x>=0 ){ + pIdx->zColAff[n] = pTab->aCol[x].affinity; + }else if( x==XN_ROWID ){ + pIdx->zColAff[n] = SQLITE_AFF_INTEGER; + }else{ + char aff; + assert( x==XN_EXPR ); + assert( pIdx->aColExpr!=0 ); + aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); + if( aff==0 ) aff = SQLITE_AFF_BLOB; + pIdx->zColAff[n] = aff; + } + } + pIdx->zColAff[n] = 0; + } + + return pIdx->zColAff; +} + +/* +** Compute the affinity string for table pTab, if it has not already been +** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. +** +** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and +** if iReg>0 then code an OP_Affinity opcode that will set the affinities +** for register iReg and following. Or if affinities exists and iReg==0, +** then just set the P4 operand of the previous opcode (which should be +** an OP_MakeRecord) to the affinity string. +** +** A column affinity string has one character per column: +** +** Character Column affinity +** ------------------------------ +** 'A' BLOB +** 'B' TEXT +** 'C' NUMERIC +** 'D' INTEGER +** 'E' REAL +*/ +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ + int i; + char *zColAff = pTab->zColAff; + if( zColAff==0 ){ + sqlite3 *db = sqlite3VdbeDb(v); + zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); + if( !zColAff ){ + sqlite3OomFault(db); + return; + } + + for(i=0; inCol; i++){ + zColAff[i] = pTab->aCol[i].affinity; + } + do{ + zColAff[i--] = 0; + }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); + pTab->zColAff = zColAff; + } + i = sqlite3Strlen30(zColAff); + if( i ){ + if( iReg ){ + sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); + }else{ + sqlite3VdbeChangeP4(v, -1, zColAff, i); + } + } +} + +/* +** Return non-zero if the table pTab in database iDb or any of its indices +** have been opened at any point in the VDBE program. This is used to see if +** a statement of the form "INSERT INTO SELECT ..." can +** run without using a temporary table for the results of the SELECT. +*/ +static int readsTable(Parse *p, int iDb, Table *pTab){ + Vdbe *v = sqlite3GetVdbe(p); + int i; + int iEnd = sqlite3VdbeCurrentAddr(v); +#ifndef SQLITE_OMIT_VIRTUALTABLE + VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; +#endif + + for(i=1; iopcode==OP_OpenRead && pOp->p3==iDb ){ + Index *pIndex; + int tnum = pOp->p2; + if( tnum==pTab->tnum ){ + return 1; + } + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( tnum==pIndex->tnum ){ + return 1; + } + } + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ + assert( pOp->p4.pVtab!=0 ); + assert( pOp->p4type==P4_VTAB ); + return 1; + } +#endif + } + return 0; +} + +#ifndef SQLITE_OMIT_AUTOINCREMENT +/* +** Locate or create an AutoincInfo structure associated with table pTab +** which is in database iDb. Return the register number for the register +** that holds the maximum rowid. +** +** There is at most one AutoincInfo structure per table even if the +** same table is autoincremented multiple times due to inserts within +** triggers. A new AutoincInfo structure is created if this is the +** first use of table pTab. On 2nd and subsequent uses, the original +** AutoincInfo structure is used. +** +** Three memory locations are allocated: +** +** (1) Register to hold the name of the pTab table. +** (2) Register to hold the maximum ROWID of pTab. +** (3) Register to hold the rowid in sqlite_sequence of pTab +** +** The 2nd register is the one that is returned. That is all the +** insert routine needs to know about. +*/ +static int autoIncBegin( + Parse *pParse, /* Parsing context */ + int iDb, /* Index of the database holding pTab */ + Table *pTab /* The table we are writing to */ +){ + int memId = 0; /* Register holding maximum rowid */ + if( pTab->tabFlags & TF_Autoincrement ){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + AutoincInfo *pInfo; + + pInfo = pToplevel->pAinc; + while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } + if( pInfo==0 ){ + pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); + if( pInfo==0 ) return 0; + pInfo->pNext = pToplevel->pAinc; + pToplevel->pAinc = pInfo; + pInfo->pTab = pTab; + pInfo->iDb = iDb; + pToplevel->nMem++; /* Register to hold name of table */ + pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ + pToplevel->nMem++; /* Rowid in sqlite_sequence */ + } + memId = pInfo->regCtr; + } + return memId; +} + +/* +** This routine generates code that will initialize all of the +** register used by the autoincrement tracker. +*/ +SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){ + AutoincInfo *p; /* Information about an AUTOINCREMENT */ + sqlite3 *db = pParse->db; /* The database connection */ + Db *pDb; /* Database only autoinc table */ + int memId; /* Register holding max rowid */ + Vdbe *v = pParse->pVdbe; /* VDBE under construction */ + + /* This routine is never called during trigger-generation. It is + ** only called from the top-level */ + assert( pParse->pTriggerTab==0 ); + assert( sqlite3IsToplevel(pParse) ); + + assert( v ); /* We failed long ago if this is not so */ + for(p = pParse->pAinc; p; p = p->pNext){ + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList autoInc[] = { + /* 0 */ {OP_Null, 0, 0, 0}, + /* 1 */ {OP_Rewind, 0, 9, 0}, + /* 2 */ {OP_Column, 0, 0, 0}, + /* 3 */ {OP_Ne, 0, 7, 0}, + /* 4 */ {OP_Rowid, 0, 0, 0}, + /* 5 */ {OP_Column, 0, 1, 0}, + /* 6 */ {OP_Goto, 0, 9, 0}, + /* 7 */ {OP_Next, 0, 2, 0}, + /* 8 */ {OP_Integer, 0, 0, 0}, + /* 9 */ {OP_Close, 0, 0, 0} + }; + VdbeOp *aOp; + pDb = &db->aDb[p->iDb]; + memId = p->regCtr; + assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); + sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); + sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); + aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn); + if( aOp==0 ) break; + aOp[0].p2 = memId; + aOp[0].p3 = memId+1; + aOp[2].p3 = memId; + aOp[3].p1 = memId-1; + aOp[3].p3 = memId; + aOp[3].p5 = SQLITE_JUMPIFNULL; + aOp[4].p2 = memId+1; + aOp[5].p3 = memId; + aOp[8].p2 = memId; + } +} + +/* +** Update the maximum rowid for an autoincrement calculation. +** +** This routine should be called when the regRowid register holds a +** new rowid that is about to be inserted. If that new rowid is +** larger than the maximum rowid in the memId memory cell, then the +** memory cell is updated. +*/ +static void autoIncStep(Parse *pParse, int memId, int regRowid){ + if( memId>0 ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); + } +} + +/* +** This routine generates the code needed to write autoincrement +** maximum rowid values back into the sqlite_sequence register. +** Every statement that might do an INSERT into an autoincrement +** table (either directly or through triggers) needs to call this +** routine just before the "exit" code. +*/ +static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){ + AutoincInfo *p; + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + + assert( v ); + for(p = pParse->pAinc; p; p = p->pNext){ + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList autoIncEnd[] = { + /* 0 */ {OP_NotNull, 0, 2, 0}, + /* 1 */ {OP_NewRowid, 0, 0, 0}, + /* 2 */ {OP_MakeRecord, 0, 2, 0}, + /* 3 */ {OP_Insert, 0, 0, 0}, + /* 4 */ {OP_Close, 0, 0, 0} + }; + VdbeOp *aOp; + Db *pDb = &db->aDb[p->iDb]; + int iRec; + int memId = p->regCtr; + + iRec = sqlite3GetTempReg(pParse); + assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); + sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); + aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn); + if( aOp==0 ) break; + aOp[0].p1 = memId+1; + aOp[1].p2 = memId+1; + aOp[2].p1 = memId-1; + aOp[2].p3 = iRec; + aOp[3].p2 = iRec; + aOp[3].p3 = memId+1; + aOp[3].p5 = OPFLAG_APPEND; + sqlite3ReleaseTempReg(pParse, iRec); + } +} +SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){ + if( pParse->pAinc ) autoIncrementEnd(pParse); +} +#else +/* +** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines +** above are all no-ops +*/ +# define autoIncBegin(A,B,C) (0) +# define autoIncStep(A,B,C) +#endif /* SQLITE_OMIT_AUTOINCREMENT */ + + +/* Forward declaration */ +static int xferOptimization( + Parse *pParse, /* Parser context */ + Table *pDest, /* The table we are inserting into */ + Select *pSelect, /* A SELECT statement to use as the data source */ + int onError, /* How to handle constraint errors */ + int iDbDest /* The database of pDest */ +); + +/* +** This routine is called to handle SQL of the following forms: +** +** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... +** insert into TABLE (IDLIST) select +** insert into TABLE (IDLIST) default values +** +** The IDLIST following the table name is always optional. If omitted, +** then a list of all (non-hidden) columns for the table is substituted. +** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST +** is omitted. +** +** For the pSelect parameter holds the values to be inserted for the +** first two forms shown above. A VALUES clause is really just short-hand +** for a SELECT statement that omits the FROM clause and everything else +** that follows. If the pSelect parameter is NULL, that means that the +** DEFAULT VALUES form of the INSERT statement is intended. +** +** The code generated follows one of four templates. For a simple +** insert with data coming from a single-row VALUES clause, the code executes +** once straight down through. Pseudo-code follows (we call this +** the "1st template"): +** +** open write cursor to
    and its indices +** put VALUES clause expressions into registers +** write the resulting record into
    +** cleanup +** +** The three remaining templates assume the statement is of the form +** +** INSERT INTO
    SELECT ... +** +** If the SELECT clause is of the restricted form "SELECT * FROM " - +** in other words if the SELECT pulls all columns from a single table +** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and +** if and are distinct tables but have identical +** schemas, including all the same indices, then a special optimization +** is invoked that copies raw records from over to . +** See the xferOptimization() function for the implementation of this +** template. This is the 2nd template. +** +** open a write cursor to
    +** open read cursor on +** transfer all records in over to
    +** close cursors +** foreach index on
    +** open a write cursor on the
    index +** open a read cursor on the corresponding index +** transfer all records from the read to the write cursors +** close cursors +** end foreach +** +** The 3rd template is for when the second template does not apply +** and the SELECT clause does not read from
    at any time. +** The generated code follows this template: +** +** X <- A +** goto B +** A: setup for the SELECT +** loop over the rows in the SELECT +** load values into registers R..R+n +** yield X +** end loop +** cleanup after the SELECT +** end-coroutine X +** B: open write cursor to
    and its indices +** C: yield X, at EOF goto D +** insert the select result into
    from R..R+n +** goto C +** D: cleanup +** +** The 4th template is used if the insert statement takes its +** values from a SELECT but the data is being inserted into a table +** that is also read as part of the SELECT. In the third form, +** we have to use an intermediate table to store the results of +** the select. The template is like this: +** +** X <- A +** goto B +** A: setup for the SELECT +** loop over the tables in the SELECT +** load value into register R..R+n +** yield X +** end loop +** cleanup after the SELECT +** end co-routine R +** B: open temp table +** L: yield X, at EOF goto M +** insert row from R..R+n into temp table +** goto L +** M: open write cursor to
    and its indices +** rewind temp table +** C: loop over rows of intermediate table +** transfer values form intermediate table into
    +** end loop +** D: cleanup +*/ +SQLITE_PRIVATE void sqlite3Insert( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* Name of table into which we are inserting */ + Select *pSelect, /* A SELECT statement to use as the data source */ + IdList *pColumn, /* Column names corresponding to IDLIST. */ + int onError /* How to handle constraint errors */ +){ + sqlite3 *db; /* The main database structure */ + Table *pTab; /* The table to insert into. aka TABLE */ + char *zTab; /* Name of the table into which we are inserting */ + const char *zDb; /* Name of the database holding this table */ + int i, j, idx; /* Loop counters */ + Vdbe *v; /* Generate code into this virtual machine */ + Index *pIdx; /* For looping over indices of the table */ + int nColumn; /* Number of columns in the data */ + int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ + int iDataCur = 0; /* VDBE cursor that is the main data repository */ + int iIdxCur = 0; /* First index cursor */ + int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ + int endOfLoop; /* Label for the end of the insertion loop */ + int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ + int addrInsTop = 0; /* Jump to label "D" */ + int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ + SelectDest dest; /* Destination for SELECT on rhs of INSERT */ + int iDb; /* Index of database holding TABLE */ + Db *pDb; /* The database containing table being inserted into */ + u8 useTempTable = 0; /* Store SELECT results in intermediate table */ + u8 appendFlag = 0; /* True if the insert is likely to be an append */ + u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ + u8 bIdListInOrder; /* True if IDLIST is in table order */ + ExprList *pList = 0; /* List of VALUES() to be inserted */ + + /* Register allocations */ + int regFromSelect = 0;/* Base register for data coming from SELECT */ + int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ + int regRowCount = 0; /* Memory cell used for the row counter */ + int regIns; /* Block of regs holding rowid+data being inserted */ + int regRowid; /* registers holding insert rowid */ + int regData; /* register holding first column to insert */ + int *aRegIdx = 0; /* One register allocated to each index */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True if attempting to insert into a view */ + Trigger *pTrigger; /* List of triggers on pTab, if required */ + int tmask; /* Mask of trigger times */ +#endif + + db = pParse->db; + memset(&dest, 0, sizeof(dest)); + if( pParse->nErr || db->mallocFailed ){ + goto insert_cleanup; + } + + /* If the Select object is really just a simple VALUES() list with a + ** single row (the common case) then keep that one row of values + ** and discard the other (unused) parts of the pSelect object + */ + if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ + pList = pSelect->pEList; + pSelect->pEList = 0; + sqlite3SelectDelete(db, pSelect); + pSelect = 0; + } + + /* Locate the table into which we will be inserting new information. + */ + assert( pTabList->nSrc==1 ); + zTab = pTabList->a[0].zName; + if( NEVER(zTab==0) ) goto insert_cleanup; + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ){ + goto insert_cleanup; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDbnDb ); + pDb = &db->aDb[iDb]; + zDb = pDb->zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ + goto insert_cleanup; + } + withoutRowid = !HasRowid(pTab); + + /* Figure out if we have any triggers and if the table being + ** inserted into is a view + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); + isView = pTab->pSelect!=0; +#else +# define pTrigger 0 +# define tmask 0 +# define isView 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); + + /* If pTab is really a view, make sure it has been initialized. + ** ViewGetColumnNames() is a no-op if pTab is not a view. + */ + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto insert_cleanup; + } + + /* Cannot insert into a read-only table. + */ + if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + goto insert_cleanup; + } + + /* Allocate a VDBE + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto insert_cleanup; + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); + +#ifndef SQLITE_OMIT_XFER_OPT + /* If the statement is of the form + ** + ** INSERT INTO SELECT * FROM ; + ** + ** Then special optimizations can be applied that make the transfer + ** very fast and which reduce fragmentation of indices. + ** + ** This is the 2nd template. + */ + if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ + assert( !pTrigger ); + assert( pList==0 ); + goto insert_end; + } +#endif /* SQLITE_OMIT_XFER_OPT */ + + /* If this is an AUTOINCREMENT table, look up the sequence number in the + ** sqlite_sequence table and store it in memory cell regAutoinc. + */ + regAutoinc = autoIncBegin(pParse, iDb, pTab); + + /* Allocate registers for holding the rowid of the new row, + ** the content of the new row, and the assembled row record. + */ + regRowid = regIns = pParse->nMem+1; + pParse->nMem += pTab->nCol + 1; + if( IsVirtual(pTab) ){ + regRowid++; + pParse->nMem++; + } + regData = regRowid+1; + + /* If the INSERT statement included an IDLIST term, then make sure + ** all elements of the IDLIST really are columns of the table and + ** remember the column indices. + ** + ** If the table has an INTEGER PRIMARY KEY column and that column + ** is named in the IDLIST, then record in the ipkColumn variable + ** the index into IDLIST of the primary key column. ipkColumn is + ** the index of the primary key as it appears in IDLIST, not as + ** is appears in the original table. (The index of the INTEGER + ** PRIMARY KEY in the original table is pTab->iPKey.) + */ + bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0; + if( pColumn ){ + for(i=0; inId; i++){ + pColumn->a[i].idx = -1; + } + for(i=0; inId; i++){ + for(j=0; jnCol; j++){ + if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ + pColumn->a[i].idx = j; + if( i!=j ) bIdListInOrder = 0; + if( j==pTab->iPKey ){ + ipkColumn = i; assert( !withoutRowid ); + } + break; + } + } + if( j>=pTab->nCol ){ + if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){ + ipkColumn = i; + bIdListInOrder = 0; + }else{ + sqlite3ErrorMsg(pParse, "table %S has no column named %s", + pTabList, 0, pColumn->a[i].zName); + pParse->checkSchema = 1; + goto insert_cleanup; + } + } + } + } + + /* Figure out how many columns of data are supplied. If the data + ** is coming from a SELECT statement, then generate a co-routine that + ** produces a single row of the SELECT on each invocation. The + ** co-routine is the common header to the 3rd and 4th templates. + */ + if( pSelect ){ + /* Data is coming from a SELECT or from a multi-row VALUES clause. + ** Generate a co-routine to run the SELECT. */ + int regYield; /* Register holding co-routine entry-point */ + int addrTop; /* Top of the co-routine */ + int rc; /* Result code */ + + regYield = ++pParse->nMem; + addrTop = sqlite3VdbeCurrentAddr(v) + 1; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); + dest.iSdst = bIdListInOrder ? regData : 0; + dest.nSdst = pTab->nCol; + rc = sqlite3Select(pParse, pSelect, &dest); + regFromSelect = dest.iSdst; + if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; + sqlite3VdbeEndCoroutine(v, regYield); + sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ + assert( pSelect->pEList ); + nColumn = pSelect->pEList->nExpr; + + /* Set useTempTable to TRUE if the result of the SELECT statement + ** should be written into a temporary table (template 4). Set to + ** FALSE if each output row of the SELECT can be written directly into + ** the destination table (template 3). + ** + ** A temp table must be used if the table being updated is also one + ** of the tables being read by the SELECT statement. Also use a + ** temp table in the case of row triggers. + */ + if( pTrigger || readsTable(pParse, iDb, pTab) ){ + useTempTable = 1; + } + + if( useTempTable ){ + /* Invoke the coroutine to extract information from the SELECT + ** and add it to a transient table srcTab. The code generated + ** here is from the 4th template: + ** + ** B: open temp table + ** L: yield X, goto M at EOF + ** insert row from R..R+n into temp table + ** goto L + ** M: ... + */ + int regRec; /* Register to hold packed record */ + int regTempRowid; /* Register to hold temp table ROWID */ + int addrL; /* Label "L" */ + + srcTab = pParse->nTab++; + regRec = sqlite3GetTempReg(pParse); + regTempRowid = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); + addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); + sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); + sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); + sqlite3VdbeGoto(v, addrL); + sqlite3VdbeJumpHere(v, addrL); + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempReg(pParse, regTempRowid); + } + }else{ + /* This is the case if the data for the INSERT is coming from a + ** single-row VALUES clause + */ + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + srcTab = -1; + assert( useTempTable==0 ); + if( pList ){ + nColumn = pList->nExpr; + if( sqlite3ResolveExprListNames(&sNC, pList) ){ + goto insert_cleanup; + } + }else{ + nColumn = 0; + } + } + + /* If there is no IDLIST term but the table has an integer primary + ** key, the set the ipkColumn variable to the integer primary key + ** column index in the original table definition. + */ + if( pColumn==0 && nColumn>0 ){ + ipkColumn = pTab->iPKey; + } + + /* Make sure the number of columns in the source data matches the number + ** of columns to be inserted into the table. + */ + for(i=0; inCol; i++){ + nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); + } + if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ + sqlite3ErrorMsg(pParse, + "table %S has %d columns but %d values were supplied", + pTabList, 0, pTab->nCol-nHidden, nColumn); + goto insert_cleanup; + } + if( pColumn!=0 && nColumn!=pColumn->nId ){ + sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); + goto insert_cleanup; + } + + /* Initialize the count of rows to be inserted + */ + if( db->flags & SQLITE_CountRows ){ + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); + } + + /* If this is not a view, open the table and and all indices */ + if( !isView ){ + int nIdx; + nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, + &iDataCur, &iIdxCur); + aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1)); + if( aRegIdx==0 ){ + goto insert_cleanup; + } + for(i=0; inMem; + } + } + + /* This is the top of the main insertion loop */ + if( useTempTable ){ + /* This block codes the top of loop only. The complete loop is the + ** following pseudocode (template 4): + ** + ** rewind temp table, if empty goto D + ** C: loop over rows of intermediate table + ** transfer values form intermediate table into
    + ** end loop + ** D: ... + */ + addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v); + addrCont = sqlite3VdbeCurrentAddr(v); + }else if( pSelect ){ + /* This block codes the top of loop only. The complete loop is the + ** following pseudocode (template 3): + ** + ** C: yield X, at EOF goto D + ** insert the select result into
    from R..R+n + ** goto C + ** D: ... + */ + addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); + VdbeCoverage(v); + } + + /* Run the BEFORE and INSTEAD OF triggers, if there are any + */ + endOfLoop = sqlite3VdbeMakeLabel(v); + if( tmask & TRIGGER_BEFORE ){ + int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); + + /* build the NEW.* reference row. Note that if there is an INTEGER + ** PRIMARY KEY into which a NULL is being inserted, that NULL will be + ** translated into a unique ID for the row. But on a BEFORE trigger, + ** we do not know what the unique ID will be (because the insert has + ** not happened yet) so we substitute a rowid of -1 + */ + if( ipkColumn<0 ){ + sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); + }else{ + int addr1; + assert( !withoutRowid ); + if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); + }else{ + assert( pSelect==0 ); /* Otherwise useTempTable is true */ + sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); + } + addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); + } + + /* Cannot have triggers on a virtual table. If it were possible, + ** this block would have to account for hidden column. + */ + assert( !IsVirtual(pTab) ); + + /* Create the new column data + */ + for(i=j=0; inCol; i++){ + if( pColumn ){ + for(j=0; jnId; j++){ + if( pColumn->a[j].idx==i ) break; + } + } + if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) + || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){ + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); + }else if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); + }else{ + assert( pSelect==0 ); /* Otherwise useTempTable is true */ + sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); + } + if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++; + } + + /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, + ** do not attempt any conversions before assembling the record. + ** If this is a real table, attempt conversions as required by the + ** table column affinities. + */ + if( !isView ){ + sqlite3TableAffinity(v, pTab, regCols+1); + } + + /* Fire BEFORE or INSTEAD OF triggers */ + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, + pTab, regCols-pTab->nCol-1, onError, endOfLoop); + + sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); + } + + /* Compute the content of the next row to insert into a range of + ** registers beginning at regIns. + */ + if( !isView ){ + if( IsVirtual(pTab) ){ + /* The row that the VUpdate opcode will delete: none */ + sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); + } + if( ipkColumn>=0 ){ + if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid); + }else if( pSelect ){ + sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid); + }else{ + VdbeOp *pOp; + sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); + pOp = sqlite3VdbeGetOp(v, -1); + if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ + appendFlag = 1; + pOp->opcode = OP_NewRowid; + pOp->p1 = iDataCur; + pOp->p2 = regRowid; + pOp->p3 = regAutoinc; + } + } + /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid + ** to generate a unique primary key value. + */ + if( !appendFlag ){ + int addr1; + if( !IsVirtual(pTab) ){ + addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); + sqlite3VdbeJumpHere(v, addr1); + }else{ + addr1 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v); + } + sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); + } + }else if( IsVirtual(pTab) || withoutRowid ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); + }else{ + sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); + appendFlag = 1; + } + autoIncStep(pParse, regAutoinc, regRowid); + + /* Compute data for all columns of the new entry, beginning + ** with the first column. + */ + nHidden = 0; + for(i=0; inCol; i++){ + int iRegStore = regRowid+1+i; + if( i==pTab->iPKey ){ + /* The value of the INTEGER PRIMARY KEY column is always a NULL. + ** Whenever this column is read, the rowid will be substituted + ** in its place. Hence, fill this column with a NULL to avoid + ** taking up data space with information that will never be used. + ** As there may be shallow copies of this value, make it a soft-NULL */ + sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); + continue; + } + if( pColumn==0 ){ + if( IsHiddenColumn(&pTab->aCol[i]) ){ + j = -1; + nHidden++; + }else{ + j = i - nHidden; + } + }else{ + for(j=0; jnId; j++){ + if( pColumn->a[j].idx==i ) break; + } + } + if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ + sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore); + }else if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); + }else if( pSelect ){ + if( regFromSelect!=regData ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); + } + }else{ + sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); + } + } + + /* Generate code to check constraints and generate index keys and + ** do the insertion. + */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + sqlite3VtabMakeWritable(pParse, pTab); + sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); + sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); + sqlite3MayAbort(pParse); + }else +#endif + { + int isReplace; /* Set to true if constraints may cause a replace */ + sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, + regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0 + ); + sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); + sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, + regIns, aRegIdx, 0, appendFlag, isReplace==0); + } + } + + /* Update the count of rows that are inserted + */ + if( (db->flags & SQLITE_CountRows)!=0 ){ + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); + } + + if( pTrigger ){ + /* Code AFTER triggers */ + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, + pTab, regData-2-pTab->nCol, onError, endOfLoop); + } + + /* The bottom of the main insertion loop, if the data source + ** is a SELECT statement. + */ + sqlite3VdbeResolveLabel(v, endOfLoop); + if( useTempTable ){ + sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrInsTop); + sqlite3VdbeAddOp1(v, OP_Close, srcTab); + }else if( pSelect ){ + sqlite3VdbeGoto(v, addrCont); + sqlite3VdbeJumpHere(v, addrInsTop); + } + + if( !IsVirtual(pTab) && !isView ){ + /* Close all tables opened */ + if( iDataCurpIndex; pIdx; pIdx=pIdx->pNext, idx++){ + sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur); + } + } + +insert_end: + /* Update the sqlite_sequence table by storing the content of the + ** maximum rowid counter values recorded while inserting into + ** autoincrement tables. + */ + if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* + ** Return the number of rows inserted. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); + } + +insert_cleanup: + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprListDelete(db, pList); + sqlite3SelectDelete(db, pSelect); + sqlite3IdListDelete(db, pColumn); + sqlite3DbFree(db, aRegIdx); +} + +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** they may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif +#ifdef tmask + #undef tmask +#endif + +/* +** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged() +*/ +#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ +#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ + +/* This is the Walker callback from checkConstraintUnchanged(). Set +** bit 0x01 of pWalker->eCode if +** pWalker->eCode to 0 if this expression node references any of the +** columns that are being modifed by an UPDATE statement. +*/ +static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_COLUMN ){ + assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 ); + if( pExpr->iColumn>=0 ){ + if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){ + pWalker->eCode |= CKCNSTRNT_COLUMN; + } + }else{ + pWalker->eCode |= CKCNSTRNT_ROWID; + } + } + return WRC_Continue; +} + +/* +** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The +** only columns that are modified by the UPDATE are those for which +** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. +** +** Return true if CHECK constraint pExpr does not use any of the +** changing columns (or the rowid if it is changing). In other words, +** return true if this CHECK constraint can be skipped when validating +** the new row in the UPDATE statement. +*/ +static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ + Walker w; + memset(&w, 0, sizeof(w)); + w.eCode = 0; + w.xExprCallback = checkConstraintExprNode; + w.u.aiCol = aiChng; + sqlite3WalkExpr(&w, pExpr); + if( !chngRowid ){ + testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 ); + w.eCode &= ~CKCNSTRNT_ROWID; + } + testcase( w.eCode==0 ); + testcase( w.eCode==CKCNSTRNT_COLUMN ); + testcase( w.eCode==CKCNSTRNT_ROWID ); + testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) ); + return !w.eCode; +} + +/* +** Generate code to do constraint checks prior to an INSERT or an UPDATE +** on table pTab. +** +** The regNewData parameter is the first register in a range that contains +** the data to be inserted or the data after the update. There will be +** pTab->nCol+1 registers in this range. The first register (the one +** that regNewData points to) will contain the new rowid, or NULL in the +** case of a WITHOUT ROWID table. The second register in the range will +** contain the content of the first table column. The third register will +** contain the content of the second table column. And so forth. +** +** The regOldData parameter is similar to regNewData except that it contains +** the data prior to an UPDATE rather than afterwards. regOldData is zero +** for an INSERT. This routine can distinguish between UPDATE and INSERT by +** checking regOldData for zero. +** +** For an UPDATE, the pkChng boolean is true if the true primary key (the +** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table) +** might be modified by the UPDATE. If pkChng is false, then the key of +** the iDataCur content table is guaranteed to be unchanged by the UPDATE. +** +** For an INSERT, the pkChng boolean indicates whether or not the rowid +** was explicitly specified as part of the INSERT statement. If pkChng +** is zero, it means that the either rowid is computed automatically or +** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT, +** pkChng will only be true if the INSERT statement provides an integer +** value for either the rowid column or its INTEGER PRIMARY KEY alias. +** +** The code generated by this routine will store new index entries into +** registers identified by aRegIdx[]. No index entry is created for +** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is +** the same as the order of indices on the linked list of indices +** at pTab->pIndex. +** +** The caller must have already opened writeable cursors on the main +** table and all applicable indices (that is to say, all indices for which +** aRegIdx[] is not zero). iDataCur is the cursor for the main table when +** inserting or updating a rowid table, or the cursor for the PRIMARY KEY +** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor +** for the first index in the pTab->pIndex list. Cursors for other indices +** are at iIdxCur+N for the N-th element of the pTab->pIndex list. +** +** This routine also generates code to check constraints. NOT NULL, +** CHECK, and UNIQUE constraints are all checked. If a constraint fails, +** then the appropriate action is performed. There are five possible +** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. +** +** Constraint type Action What Happens +** --------------- ---------- ---------------------------------------- +** any ROLLBACK The current transaction is rolled back and +** sqlite3_step() returns immediately with a +** return code of SQLITE_CONSTRAINT. +** +** any ABORT Back out changes from the current command +** only (do not do a complete rollback) then +** cause sqlite3_step() to return immediately +** with SQLITE_CONSTRAINT. +** +** any FAIL Sqlite3_step() returns immediately with a +** return code of SQLITE_CONSTRAINT. The +** transaction is not rolled back and any +** changes to prior rows are retained. +** +** any IGNORE The attempt in insert or update the current +** row is skipped, without throwing an error. +** Processing continues with the next row. +** (There is an immediate jump to ignoreDest.) +** +** NOT NULL REPLACE The NULL value is replace by the default +** value for that column. If the default value +** is NULL, the action is the same as ABORT. +** +** UNIQUE REPLACE The other row that conflicts with the row +** being inserted is removed. +** +** CHECK REPLACE Illegal. The results in an exception. +** +** Which action to take is determined by the overrideError parameter. +** Or if overrideError==OE_Default, then the pParse->onError parameter +** is used. Or if pParse->onError==OE_Default then the onError value +** for the constraint is used. +*/ +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( + Parse *pParse, /* The parser context */ + Table *pTab, /* The table being inserted or updated */ + int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */ + int iDataCur, /* Canonical data cursor (main table or PK index) */ + int iIdxCur, /* First index cursor */ + int regNewData, /* First register in a range holding values to insert */ + int regOldData, /* Previous content. 0 for INSERTs */ + u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */ + u8 overrideError, /* Override onError to this if not OE_Default */ + int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ + int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */ + int *aiChng /* column i is unchanged if aiChng[i]<0 */ +){ + Vdbe *v; /* VDBE under constrution */ + Index *pIdx; /* Pointer to one of the indices */ + Index *pPk = 0; /* The PRIMARY KEY index */ + sqlite3 *db; /* Database connection */ + int i; /* loop counter */ + int ix; /* Index loop counter */ + int nCol; /* Number of columns */ + int onError; /* Conflict resolution strategy */ + int addr1; /* Address of jump instruction */ + int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ + int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ + int ipkTop = 0; /* Top of the rowid change constraint check */ + int ipkBottom = 0; /* Bottom of the rowid change constraint check */ + u8 isUpdate; /* True if this is an UPDATE operation */ + u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ + int regRowid = -1; /* Register holding ROWID value */ + + isUpdate = regOldData!=0; + db = pParse->db; + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + nCol = pTab->nCol; + + /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for + ** normal rowid tables. nPkField is the number of key fields in the + ** pPk index or 1 for a rowid table. In other words, nPkField is the + ** number of fields in the true primary key of the table. */ + if( HasRowid(pTab) ){ + pPk = 0; + nPkField = 1; + }else{ + pPk = sqlite3PrimaryKeyIndex(pTab); + nPkField = pPk->nKeyCol; + } + + /* Record that this module has started */ + VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)", + iDataCur, iIdxCur, regNewData, regOldData, pkChng)); + + /* Test all NOT NULL constraints. + */ + for(i=0; iiPKey ){ + continue; /* ROWID is never NULL */ + } + if( aiChng && aiChng[i]<0 ){ + /* Don't bother checking for NOT NULL on columns that do not change */ + continue; + } + onError = pTab->aCol[i].notNull; + if( onError==OE_None ) continue; /* This column is allowed to be NULL */ + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ + onError = OE_Abort; + } + assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail + || onError==OE_Ignore || onError==OE_Replace ); + switch( onError ){ + case OE_Abort: + sqlite3MayAbort(pParse); + /* Fall through */ + case OE_Rollback: + case OE_Fail: { + char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, + pTab->aCol[i].zName); + sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError, + regNewData+1+i, zMsg, P4_DYNAMIC); + sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); + VdbeCoverage(v); + break; + } + case OE_Ignore: { + sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); + VdbeCoverage(v); + break; + } + default: { + assert( onError==OE_Replace ); + addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); + VdbeCoverage(v); + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); + sqlite3VdbeJumpHere(v, addr1); + break; + } + } + } + + /* Test all CHECK constraints + */ +#ifndef SQLITE_OMIT_CHECK + if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ + ExprList *pCheck = pTab->pCheck; + pParse->ckBase = regNewData+1; + onError = overrideError!=OE_Default ? overrideError : OE_Abort; + for(i=0; inExpr; i++){ + int allOk; + Expr *pExpr = pCheck->a[i].pExpr; + if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue; + allOk = sqlite3VdbeMakeLabel(v); + sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL); + if( onError==OE_Ignore ){ + sqlite3VdbeGoto(v, ignoreDest); + }else{ + char *zName = pCheck->a[i].zName; + if( zName==0 ) zName = pTab->zName; + if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ + sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, + onError, zName, P4_TRANSIENT, + P5_ConstraintCheck); + } + sqlite3VdbeResolveLabel(v, allOk); + } + } +#endif /* !defined(SQLITE_OMIT_CHECK) */ + + /* If rowid is changing, make sure the new rowid does not previously + ** exist in the table. + */ + if( pkChng && pPk==0 ){ + int addrRowidOk = sqlite3VdbeMakeLabel(v); + + /* Figure out what action to take in case of a rowid collision */ + onError = pTab->keyConf; + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + + if( isUpdate ){ + /* pkChng!=0 does not mean that the rowid has change, only that + ** it might have changed. Skip the conflict logic below if the rowid + ** is unchanged. */ + sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + VdbeCoverage(v); + } + + /* If the response to a rowid conflict is REPLACE but the response + ** to some other UNIQUE constraint is FAIL or IGNORE, then we need + ** to defer the running of the rowid conflict checking until after + ** the UNIQUE constraints have run. + */ + if( onError==OE_Replace && overrideError!=OE_Replace ){ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){ + ipkTop = sqlite3VdbeAddOp0(v, OP_Goto); + break; + } + } + } + + /* Check to see if the new rowid already exists in the table. Skip + ** the following conflict logic if it does not. */ + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); + VdbeCoverage(v); + + /* Generate code that deals with a rowid collision */ + switch( onError ){ + default: { + onError = OE_Abort; + /* Fall thru into the next case */ + } + case OE_Rollback: + case OE_Abort: + case OE_Fail: { + sqlite3RowidConstraint(pParse, onError, pTab); + break; + } + case OE_Replace: { + /* If there are DELETE triggers on this table and the + ** recursive-triggers flag is set, call GenerateRowDelete() to + ** remove the conflicting row from the table. This will fire + ** the triggers and remove both the table and index b-tree entries. + ** + ** Otherwise, if there are no triggers or the recursive-triggers + ** flag is not set, but the table has one or more indexes, call + ** GenerateRowIndexDelete(). This removes the index b-tree entries + ** only. The table b-tree entry will be replaced by the new entry + ** when it is inserted. + ** + ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, + ** also invoke MultiWrite() to indicate that this VDBE may require + ** statement rollback (if the statement is aborted after the delete + ** takes place). Earlier versions called sqlite3MultiWrite() regardless, + ** but being more selective here allows statements like: + ** + ** REPLACE INTO t(rowid) VALUES($newrowid) + ** + ** to run without a statement journal if there are no indexes on the + ** table. + */ + Trigger *pTrigger = 0; + if( db->flags&SQLITE_RecTriggers ){ + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + } + if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ + sqlite3MultiWrite(pParse); + sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, + regNewData, 1, 0, OE_Replace, 1, -1); + }else{ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( HasRowid(pTab) ){ + /* This OP_Delete opcode fires the pre-update-hook only. It does + ** not modify the b-tree. It is more efficient to let the coming + ** OP_Insert replace the existing entry than it is to delete the + ** existing entry and then insert a new one. */ + sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP); + sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE); + } +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + if( pTab->pIndex ){ + sqlite3MultiWrite(pParse); + sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); + } + } + seenReplace = 1; + break; + } + case OE_Ignore: { + /*assert( seenReplace==0 );*/ + sqlite3VdbeGoto(v, ignoreDest); + break; + } + } + sqlite3VdbeResolveLabel(v, addrRowidOk); + if( ipkTop ){ + ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, ipkTop); + } + } + + /* Test all UNIQUE constraints by creating entries for each UNIQUE + ** index and making sure that duplicate entries do not already exist. + ** Compute the revised record entries for indices as we go. + ** + ** This loop also handles the case of the PRIMARY KEY index for a + ** WITHOUT ROWID table. + */ + for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){ + int regIdx; /* Range of registers hold conent for pIdx */ + int regR; /* Range of registers holding conflicting PK */ + int iThisCur; /* Cursor for this UNIQUE index */ + int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */ + + if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ + if( bAffinityDone==0 ){ + sqlite3TableAffinity(v, pTab, regNewData+1); + bAffinityDone = 1; + } + iThisCur = iIdxCur+ix; + addrUniqueOk = sqlite3VdbeMakeLabel(v); + + /* Skip partial indices for which the WHERE clause is not true */ + if( pIdx->pPartIdxWhere ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); + pParse->ckBase = regNewData+1; + sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, + SQLITE_JUMPIFNULL); + pParse->ckBase = 0; + } + + /* Create a record for this index entry as it should appear after + ** the insert or update. Store that record in the aRegIdx[ix] register + */ + regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); + for(i=0; inColumn; i++){ + int iField = pIdx->aiColumn[i]; + int x; + if( iField==XN_EXPR ){ + pParse->ckBase = regNewData+1; + sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); + pParse->ckBase = 0; + VdbeComment((v, "%s column %d", pIdx->zName, i)); + }else{ + if( iField==XN_ROWID || iField==pTab->iPKey ){ + if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */ + x = regNewData; + regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i; + }else{ + x = iField + regNewData + 1; + } + sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); + VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); + } + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); + VdbeComment((v, "for %s", pIdx->zName)); + sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn); + + /* In an UPDATE operation, if this index is the PRIMARY KEY index + ** of a WITHOUT ROWID table and there has been no change the + ** primary key, then no collision is possible. The collision detection + ** logic below can all be skipped. */ + if( isUpdate && pPk==pIdx && pkChng==0 ){ + sqlite3VdbeResolveLabel(v, addrUniqueOk); + continue; + } + + /* Find out what action to take in case there is a uniqueness conflict */ + onError = pIdx->onError; + if( onError==OE_None ){ + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); + sqlite3VdbeResolveLabel(v, addrUniqueOk); + continue; /* pIdx is not a UNIQUE index */ + } + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + + /* Check to see if the new index entry will be unique */ + sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, + regIdx, pIdx->nKeyCol); VdbeCoverage(v); + + /* Generate code to handle collisions */ + regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField); + if( isUpdate || onError==OE_Replace ){ + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR); + /* Conflict only if the rowid of the existing index entry + ** is different from old-rowid */ + if( isUpdate ){ + sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + VdbeCoverage(v); + } + }else{ + int x; + /* Extract the PRIMARY KEY from the end of the index entry and + ** store it in registers regR..regR+nPk-1 */ + if( pIdx!=pPk ){ + for(i=0; inKeyCol; i++){ + assert( pPk->aiColumn[i]>=0 ); + x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); + sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); + VdbeComment((v, "%s.%s", pTab->zName, + pTab->aCol[pPk->aiColumn[i]].zName)); + } + } + if( isUpdate ){ + /* If currently processing the PRIMARY KEY of a WITHOUT ROWID + ** table, only conflict if the new PRIMARY KEY values are actually + ** different from the old. + ** + ** For a UNIQUE index, only conflict if the PRIMARY KEY values + ** of the matched index row are different from the original PRIMARY + ** KEY values of this row before the update. */ + int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; + int op = OP_Ne; + int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); + + for(i=0; inKeyCol; i++){ + char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); + x = pPk->aiColumn[i]; + assert( x>=0 ); + if( i==(pPk->nKeyCol-1) ){ + addrJump = addrUniqueOk; + op = OP_Eq; + } + sqlite3VdbeAddOp4(v, op, + regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ + ); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + VdbeCoverageIf(v, op==OP_Eq); + VdbeCoverageIf(v, op==OP_Ne); + } + } + } + } + + /* Generate code that executes if the new index entry is not unique */ + assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail + || onError==OE_Ignore || onError==OE_Replace ); + switch( onError ){ + case OE_Rollback: + case OE_Abort: + case OE_Fail: { + sqlite3UniqueConstraint(pParse, onError, pIdx); + break; + } + case OE_Ignore: { + sqlite3VdbeGoto(v, ignoreDest); + break; + } + default: { + Trigger *pTrigger = 0; + assert( onError==OE_Replace ); + sqlite3MultiWrite(pParse); + if( db->flags&SQLITE_RecTriggers ){ + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + } + sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, + regR, nPkField, 0, OE_Replace, + (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1); + seenReplace = 1; + break; + } + } + sqlite3VdbeResolveLabel(v, addrUniqueOk); + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); + if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); + } + if( ipkTop ){ + sqlite3VdbeGoto(v, ipkTop+1); + sqlite3VdbeJumpHere(v, ipkBottom); + } + + *pbMayReplace = seenReplace; + VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); +} + +/* +** This routine generates code to finish the INSERT or UPDATE operation +** that was started by a prior call to sqlite3GenerateConstraintChecks. +** A consecutive range of registers starting at regNewData contains the +** rowid and the content to be inserted. +** +** The arguments to this routine should be the same as the first six +** arguments to sqlite3GenerateConstraintChecks. +*/ +SQLITE_PRIVATE void sqlite3CompleteInsertion( + Parse *pParse, /* The parser context */ + Table *pTab, /* the table into which we are inserting */ + int iDataCur, /* Cursor of the canonical data source */ + int iIdxCur, /* First index cursor */ + int regNewData, /* Range of content */ + int *aRegIdx, /* Register used by each index. 0 for unused indices */ + int isUpdate, /* True for UPDATE, False for INSERT */ + int appendBias, /* True if this is likely to be an append */ + int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ +){ + Vdbe *v; /* Prepared statements under construction */ + Index *pIdx; /* An index being inserted or updated */ + u8 pik_flags; /* flag values passed to the btree insert */ + int regData; /* Content registers (after the rowid) */ + int regRec; /* Register holding assembled record for the table */ + int i; /* Loop counter */ + u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + if( aRegIdx[i]==0 ) continue; + bAffinityDone = 1; + if( pIdx->pPartIdxWhere ){ + sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + } + sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]); + pik_flags = 0; + if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT; + if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ + assert( pParse->nested==0 ); + pik_flags |= OPFLAG_NCHANGE; + } + sqlite3VdbeChangeP5(v, pik_flags); + } + if( !HasRowid(pTab) ) return; + regData = regNewData + 1; + regRec = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); + if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0); + sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); + if( pParse->nested ){ + pik_flags = 0; + }else{ + pik_flags = OPFLAG_NCHANGE; + pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); + } + if( appendBias ){ + pik_flags |= OPFLAG_APPEND; + } + if( useSeekResult ){ + pik_flags |= OPFLAG_USESEEKRESULT; + } + sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData); + if( !pParse->nested ){ + sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE); + } + sqlite3VdbeChangeP5(v, pik_flags); +} + +/* +** Allocate cursors for the pTab table and all its indices and generate +** code to open and initialized those cursors. +** +** The cursor for the object that contains the complete data (normally +** the table itself, but the PRIMARY KEY index in the case of a WITHOUT +** ROWID table) is returned in *piDataCur. The first index cursor is +** returned in *piIdxCur. The number of indices is returned. +** +** Use iBase as the first cursor (either the *piDataCur for rowid tables +** or the first index for WITHOUT ROWID tables) if it is non-negative. +** If iBase is negative, then allocate the next available cursor. +** +** For a rowid table, *piDataCur will be exactly one less than *piIdxCur. +** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range +** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the +** pTab->pIndex list. +** +** If pTab is a virtual table, then this routine is a no-op and the +** *piDataCur and *piIdxCur values are left uninitialized. +*/ +SQLITE_PRIVATE int sqlite3OpenTableAndIndices( + Parse *pParse, /* Parsing context */ + Table *pTab, /* Table to be opened */ + int op, /* OP_OpenRead or OP_OpenWrite */ + u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */ + int iBase, /* Use this for the table cursor, if there is one */ + u8 *aToOpen, /* If not NULL: boolean for each table and index */ + int *piDataCur, /* Write the database source cursor number here */ + int *piIdxCur /* Write the first index cursor number here */ +){ + int i; + int iDb; + int iDataCur; + Index *pIdx; + Vdbe *v; + + assert( op==OP_OpenRead || op==OP_OpenWrite ); + assert( op==OP_OpenWrite || p5==0 ); + if( IsVirtual(pTab) ){ + /* This routine is a no-op for virtual tables. Leave the output + ** variables *piDataCur and *piIdxCur uninitialized so that valgrind + ** can detect if they are used by mistake in the caller. */ + return 0; + } + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + if( iBase<0 ) iBase = pParse->nTab; + iDataCur = iBase++; + if( piDataCur ) *piDataCur = iDataCur; + if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ + sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); + }else{ + sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); + } + if( piIdxCur ) *piIdxCur = iBase; + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + int iIdxCur = iBase++; + assert( pIdx->pSchema==pTab->pSchema ); + if( aToOpen==0 || aToOpen[i+1] ){ + sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + VdbeComment((v, "%s", pIdx->zName)); + } + if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ + if( piDataCur ) *piDataCur = iIdxCur; + }else{ + sqlite3VdbeChangeP5(v, p5); + } + } + if( iBase>pParse->nTab ) pParse->nTab = iBase; + return i; +} + + +#ifdef SQLITE_TEST +/* +** The following global variable is incremented whenever the +** transfer optimization is used. This is used for testing +** purposes only - to make sure the transfer optimization really +** is happening when it is supposed to. +*/ +SQLITE_API int sqlite3_xferopt_count; +#endif /* SQLITE_TEST */ + + +#ifndef SQLITE_OMIT_XFER_OPT +/* +** Check to see if index pSrc is compatible as a source of data +** for index pDest in an insert transfer optimization. The rules +** for a compatible index: +** +** * The index is over the same set of columns +** * The same DESC and ASC markings occurs on all columns +** * The same onError processing (OE_Abort, OE_Ignore, etc) +** * The same collating sequence on each column +** * The index has the exact same WHERE clause +*/ +static int xferCompatibleIndex(Index *pDest, Index *pSrc){ + int i; + assert( pDest && pSrc ); + assert( pDest->pTable!=pSrc->pTable ); + if( pDest->nKeyCol!=pSrc->nKeyCol ){ + return 0; /* Different number of columns */ + } + if( pDest->onError!=pSrc->onError ){ + return 0; /* Different conflict resolution strategies */ + } + for(i=0; inKeyCol; i++){ + if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ + return 0; /* Different columns indexed */ + } + if( pSrc->aiColumn[i]==XN_EXPR ){ + assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); + if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr, + pDest->aColExpr->a[i].pExpr, -1)!=0 ){ + return 0; /* Different expressions in the index */ + } + } + if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ + return 0; /* Different sort orders */ + } + if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){ + return 0; /* Different collating sequences */ + } + } + if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ + return 0; /* Different WHERE clauses */ + } + + /* If no test above fails then the indices must be compatible */ + return 1; +} + +/* +** Attempt the transfer optimization on INSERTs of the form +** +** INSERT INTO tab1 SELECT * FROM tab2; +** +** The xfer optimization transfers raw records from tab2 over to tab1. +** Columns are not decoded and reassembled, which greatly improves +** performance. Raw index records are transferred in the same way. +** +** The xfer optimization is only attempted if tab1 and tab2 are compatible. +** There are lots of rules for determining compatibility - see comments +** embedded in the code for details. +** +** This routine returns TRUE if the optimization is guaranteed to be used. +** Sometimes the xfer optimization will only work if the destination table +** is empty - a factor that can only be determined at run-time. In that +** case, this routine generates code for the xfer optimization but also +** does a test to see if the destination table is empty and jumps over the +** xfer optimization code if the test fails. In that case, this routine +** returns FALSE so that the caller will know to go ahead and generate +** an unoptimized transfer. This routine also returns FALSE if there +** is no chance that the xfer optimization can be applied. +** +** This optimization is particularly useful at making VACUUM run faster. +*/ +static int xferOptimization( + Parse *pParse, /* Parser context */ + Table *pDest, /* The table we are inserting into */ + Select *pSelect, /* A SELECT statement to use as the data source */ + int onError, /* How to handle constraint errors */ + int iDbDest /* The database of pDest */ +){ + sqlite3 *db = pParse->db; + ExprList *pEList; /* The result set of the SELECT */ + Table *pSrc; /* The table in the FROM clause of SELECT */ + Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ + struct SrcList_item *pItem; /* An element of pSelect->pSrc */ + int i; /* Loop counter */ + int iDbSrc; /* The database of pSrc */ + int iSrc, iDest; /* Cursors from source and destination */ + int addr1, addr2; /* Loop addresses */ + int emptyDestTest = 0; /* Address of test for empty pDest */ + int emptySrcTest = 0; /* Address of test for empty pSrc */ + Vdbe *v; /* The VDBE we are building */ + int regAutoinc; /* Memory register used by AUTOINC */ + int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ + int regData, regRowid; /* Registers holding data and rowid */ + + if( pSelect==0 ){ + return 0; /* Must be of the form INSERT INTO ... SELECT ... */ + } + if( pParse->pWith || pSelect->pWith ){ + /* Do not attempt to process this query if there are an WITH clauses + ** attached to it. Proceeding may generate a false "no such table: xxx" + ** error if pSelect reads from a CTE named "xxx". */ + return 0; + } + if( sqlite3TriggerList(pParse, pDest) ){ + return 0; /* tab1 must not have triggers */ + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pDest->tabFlags & TF_Virtual ){ + return 0; /* tab1 must not be a virtual table */ + } +#endif + if( onError==OE_Default ){ + if( pDest->iPKey>=0 ) onError = pDest->keyConf; + if( onError==OE_Default ) onError = OE_Abort; + } + assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ + if( pSelect->pSrc->nSrc!=1 ){ + return 0; /* FROM clause must have exactly one term */ + } + if( pSelect->pSrc->a[0].pSelect ){ + return 0; /* FROM clause cannot contain a subquery */ + } + if( pSelect->pWhere ){ + return 0; /* SELECT may not have a WHERE clause */ + } + if( pSelect->pOrderBy ){ + return 0; /* SELECT may not have an ORDER BY clause */ + } + /* Do not need to test for a HAVING clause. If HAVING is present but + ** there is no ORDER BY, we will get an error. */ + if( pSelect->pGroupBy ){ + return 0; /* SELECT may not have a GROUP BY clause */ + } + if( pSelect->pLimit ){ + return 0; /* SELECT may not have a LIMIT clause */ + } + assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ + if( pSelect->pPrior ){ + return 0; /* SELECT may not be a compound query */ + } + if( pSelect->selFlags & SF_Distinct ){ + return 0; /* SELECT may not be DISTINCT */ + } + pEList = pSelect->pEList; + assert( pEList!=0 ); + if( pEList->nExpr!=1 ){ + return 0; /* The result set must have exactly one column */ + } + assert( pEList->a[0].pExpr ); + if( pEList->a[0].pExpr->op!=TK_ASTERISK ){ + return 0; /* The result set must be the special operator "*" */ + } + + /* At this point we have established that the statement is of the + ** correct syntactic form to participate in this optimization. Now + ** we have to check the semantics. + */ + pItem = pSelect->pSrc->a; + pSrc = sqlite3LocateTableItem(pParse, 0, pItem); + if( pSrc==0 ){ + return 0; /* FROM clause does not contain a real table */ + } + if( pSrc==pDest ){ + return 0; /* tab1 and tab2 may not be the same table */ + } + if( HasRowid(pDest)!=HasRowid(pSrc) ){ + return 0; /* source and destination must both be WITHOUT ROWID or not */ + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pSrc->tabFlags & TF_Virtual ){ + return 0; /* tab2 must not be a virtual table */ + } +#endif + if( pSrc->pSelect ){ + return 0; /* tab2 may not be a view */ + } + if( pDest->nCol!=pSrc->nCol ){ + return 0; /* Number of columns must be the same in tab1 and tab2 */ + } + if( pDest->iPKey!=pSrc->iPKey ){ + return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ + } + for(i=0; inCol; i++){ + Column *pDestCol = &pDest->aCol[i]; + Column *pSrcCol = &pSrc->aCol[i]; +#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS + if( (db->flags & SQLITE_Vacuum)==0 + && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN + ){ + return 0; /* Neither table may have __hidden__ columns */ + } +#endif + if( pDestCol->affinity!=pSrcCol->affinity ){ + return 0; /* Affinity must be the same on all columns */ + } + if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){ + return 0; /* Collating sequence must be the same on all columns */ + } + if( pDestCol->notNull && !pSrcCol->notNull ){ + return 0; /* tab2 must be NOT NULL if tab1 is */ + } + /* Default values for second and subsequent columns need to match. */ + if( i>0 ){ + assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN ); + assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN ); + if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0) + || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken, + pSrcCol->pDflt->u.zToken)!=0) + ){ + return 0; /* Default values must be the same for all columns */ + } + } + } + for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ + if( IsUniqueIndex(pDestIdx) ){ + destHasUniqueIdx = 1; + } + for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ + if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; + } + if( pSrcIdx==0 ){ + return 0; /* pDestIdx has no corresponding index in pSrc */ + } + } +#ifndef SQLITE_OMIT_CHECK + if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){ + return 0; /* Tables have different CHECK constraints. Ticket #2252 */ + } +#endif +#ifndef SQLITE_OMIT_FOREIGN_KEY + /* Disallow the transfer optimization if the destination table constains + ** any foreign key constraints. This is more restrictive than necessary. + ** But the main beneficiary of the transfer optimization is the VACUUM + ** command, and the VACUUM command disables foreign key constraints. So + ** the extra complication to make this rule less restrictive is probably + ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] + */ + if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ + return 0; + } +#endif + if( (db->flags & SQLITE_CountRows)!=0 ){ + return 0; /* xfer opt does not play well with PRAGMA count_changes */ + } + + /* If we get this far, it means that the xfer optimization is at + ** least a possibility, though it might only work if the destination + ** table (tab1) is initially empty. + */ +#ifdef SQLITE_TEST + sqlite3_xferopt_count++; +#endif + iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); + v = sqlite3GetVdbe(pParse); + sqlite3CodeVerifySchema(pParse, iDbSrc); + iSrc = pParse->nTab++; + iDest = pParse->nTab++; + regAutoinc = autoIncBegin(pParse, iDbDest, pDest); + regData = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); + sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); + assert( HasRowid(pDest) || destHasUniqueIdx ); + if( (db->flags & SQLITE_Vacuum)==0 && ( + (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ + || destHasUniqueIdx /* (2) */ + || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ + )){ + /* In some circumstances, we are able to run the xfer optimization + ** only if the destination table is initially empty. Unless the + ** SQLITE_Vacuum flag is set, this block generates code to make + ** that determination. If SQLITE_Vacuum is set, then the destination + ** table is always empty. + ** + ** Conditions under which the destination must be empty: + ** + ** (1) There is no INTEGER PRIMARY KEY but there are indices. + ** (If the destination is not initially empty, the rowid fields + ** of index entries might need to change.) + ** + ** (2) The destination has a unique index. (The xfer optimization + ** is unable to test uniqueness.) + ** + ** (3) onError is something other than OE_Abort and OE_Rollback. + */ + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); + emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, addr1); + } + if( HasRowid(pSrc) ){ + sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); + emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); + if( pDest->iPKey>=0 ){ + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); + addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); + VdbeCoverage(v); + sqlite3RowidConstraint(pParse, onError, pDest); + sqlite3VdbeJumpHere(v, addr2); + autoIncStep(pParse, regAutoinc, regRowid); + }else if( pDest->pIndex==0 ){ + addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); + }else{ + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); + assert( (pDest->tabFlags & TF_Autoincrement)==0 ); + } + sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); + sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid, + (char*)pDest, P4_TABLE); + sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + }else{ + sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); + sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); + } + for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ + u8 idxInsFlags = 0; + for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ + if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; + } + assert( pSrcIdx ); + sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); + sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); + VdbeComment((v, "%s", pSrcIdx->zName)); + sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); + sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); + sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); + VdbeComment((v, "%s", pDestIdx->zName)); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); + if( db->flags & SQLITE_Vacuum ){ + /* This INSERT command is part of a VACUUM operation, which guarantees + ** that the destination table is empty. If all indexed columns use + ** collation sequence BINARY, then it can also be assumed that the + ** index will be populated by inserting keys in strictly sorted + ** order. In this case, instead of seeking within the b-tree as part + ** of every OP_IdxInsert opcode, an OP_Last is added before the + ** OP_IdxInsert to seek to the point within the b-tree where each key + ** should be inserted. This is faster. + ** + ** If any of the indexed columns use a collation sequence other than + ** BINARY, this optimization is disabled. This is because the user + ** might change the definition of a collation sequence and then run + ** a VACUUM command. In that case keys may not be written in strictly + ** sorted order. */ + for(i=0; inColumn; i++){ + const char *zColl = pSrcIdx->azColl[i]; + assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0 + || sqlite3StrBINARY==zColl ); + if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; + } + if( i==pSrcIdx->nColumn ){ + idxInsFlags = OPFLAG_USESEEKRESULT; + sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); + } + } + if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ + idxInsFlags |= OPFLAG_NCHANGE; + } + sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); + sqlite3VdbeChangeP5(v, idxInsFlags); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + } + if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempReg(pParse, regData); + if( emptyDestTest ){ + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); + sqlite3VdbeJumpHere(v, emptyDestTest); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + return 0; + }else{ + return 1; + } +} +#endif /* SQLITE_OMIT_XFER_OPT */ + +/************** End of insert.c **********************************************/ +/************** Begin file legacy.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Main file for the SQLite library. The routines in this file +** implement the programmer interface to the library. Routines in +** other files are for internal use by SQLite and should not be +** accessed by users of the library. +*/ + +/* #include "sqliteInt.h" */ + +/* +** Execute SQL code. Return one of the SQLITE_ success/failure +** codes. Also write an error message into memory obtained from +** malloc() and make *pzErrMsg point to that message. +** +** If the SQL is a query, then for each row in the query result +** the xCallback() function is called. pArg becomes the first +** argument to xCallback(). If xCallback=NULL then no callback +** is invoked, even for queries. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_exec( + sqlite3 *db, /* The database on which the SQL executes */ + const char *zSql, /* The SQL to be executed */ + sqlite3_callback xCallback, /* Invoke this callback routine */ + void *pArg, /* First argument to xCallback() */ + char **pzErrMsg /* Write error messages here */ +){ + int rc = SQLITE_OK; /* Return code */ + const char *zLeftover; /* Tail of unprocessed SQL */ + sqlite3_stmt *pStmt = 0; /* The current SQL statement */ + char **azCols = 0; /* Names of result columns */ + int callbackIsInit; /* True if callback data is initialized */ + + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; + if( zSql==0 ) zSql = ""; + + sqlite3_mutex_enter(db->mutex); + sqlite3Error(db, SQLITE_OK); + while( rc==SQLITE_OK && zSql[0] ){ + int nCol; + char **azVals = 0; + + pStmt = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); + assert( rc==SQLITE_OK || pStmt==0 ); + if( rc!=SQLITE_OK ){ + continue; + } + if( !pStmt ){ + /* this happens for a comment or white-space */ + zSql = zLeftover; + continue; + } + + callbackIsInit = 0; + nCol = sqlite3_column_count(pStmt); + + while( 1 ){ + int i; + rc = sqlite3_step(pStmt); + + /* Invoke the callback function if required */ + if( xCallback && (SQLITE_ROW==rc || + (SQLITE_DONE==rc && !callbackIsInit + && db->flags&SQLITE_NullCallback)) ){ + if( !callbackIsInit ){ + azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); + if( azCols==0 ){ + goto exec_out; + } + for(i=0; ierrMask)==rc ); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/************** End of legacy.c **********************************************/ +/************** Begin file loadext.c *****************************************/ +/* +** 2006 June 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to dynamically load extensions into +** the SQLite library. +*/ + +#ifndef SQLITE_CORE + #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ +#endif +/************** Include sqlite3ext.h in the middle of loadext.c **************/ +/************** Begin file sqlite3ext.h **************************************/ +/* +** 2006 June 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the SQLite interface for use by +** shared libraries that want to be imported as extensions into +** an SQLite instance. Shared libraries that intend to be loaded +** as extensions by SQLite should #include this file instead of +** sqlite3.h. +*/ +#ifndef _SQLITE3EXT_H_ +#define _SQLITE3EXT_H_ +/* #include "sqlite3.h" */ + +typedef struct sqlite3_api_routines sqlite3_api_routines; + +/* +** The following structure holds pointers to all of the SQLite API +** routines. +** +** WARNING: In order to maintain backwards compatibility, add new +** interfaces to the end of this structure only. If you insert new +** interfaces in the middle of this structure, then older different +** versions of SQLite will not be able to load each other's shared +** libraries! +*/ +struct sqlite3_api_routines { + void * (*aggregate_context)(sqlite3_context*,int nBytes); + int (*aggregate_count)(sqlite3_context*); + int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); + int (*bind_double)(sqlite3_stmt*,int,double); + int (*bind_int)(sqlite3_stmt*,int,int); + int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); + int (*bind_null)(sqlite3_stmt*,int); + int (*bind_parameter_count)(sqlite3_stmt*); + int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); + const char * (*bind_parameter_name)(sqlite3_stmt*,int); + int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); + int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); + int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); + int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); + int (*busy_timeout)(sqlite3*,int ms); + int (*changes)(sqlite3*); + int (*close)(sqlite3*); + int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*, + int eTextRep,const char*)); + int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*, + int eTextRep,const void*)); + const void * (*column_blob)(sqlite3_stmt*,int iCol); + int (*column_bytes)(sqlite3_stmt*,int iCol); + int (*column_bytes16)(sqlite3_stmt*,int iCol); + int (*column_count)(sqlite3_stmt*pStmt); + const char * (*column_database_name)(sqlite3_stmt*,int); + const void * (*column_database_name16)(sqlite3_stmt*,int); + const char * (*column_decltype)(sqlite3_stmt*,int i); + const void * (*column_decltype16)(sqlite3_stmt*,int); + double (*column_double)(sqlite3_stmt*,int iCol); + int (*column_int)(sqlite3_stmt*,int iCol); + sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); + const char * (*column_name)(sqlite3_stmt*,int); + const void * (*column_name16)(sqlite3_stmt*,int); + const char * (*column_origin_name)(sqlite3_stmt*,int); + const void * (*column_origin_name16)(sqlite3_stmt*,int); + const char * (*column_table_name)(sqlite3_stmt*,int); + const void * (*column_table_name16)(sqlite3_stmt*,int); + const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); + const void * (*column_text16)(sqlite3_stmt*,int iCol); + int (*column_type)(sqlite3_stmt*,int iCol); + sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); + void * (*commit_hook)(sqlite3*,int(*)(void*),void*); + int (*complete)(const char*sql); + int (*complete16)(const void*sql); + int (*create_collation)(sqlite3*,const char*,int,void*, + int(*)(void*,int,const void*,int,const void*)); + int (*create_collation16)(sqlite3*,const void*,int,void*, + int(*)(void*,int,const void*,int,const void*)); + int (*create_function)(sqlite3*,const char*,int,int,void*, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*)); + int (*create_function16)(sqlite3*,const void*,int,int,void*, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*)); + int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); + int (*data_count)(sqlite3_stmt*pStmt); + sqlite3 * (*db_handle)(sqlite3_stmt*); + int (*declare_vtab)(sqlite3*,const char*); + int (*enable_shared_cache)(int); + int (*errcode)(sqlite3*db); + const char * (*errmsg)(sqlite3*); + const void * (*errmsg16)(sqlite3*); + int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); + int (*expired)(sqlite3_stmt*); + int (*finalize)(sqlite3_stmt*pStmt); + void (*free)(void*); + void (*free_table)(char**result); + int (*get_autocommit)(sqlite3*); + void * (*get_auxdata)(sqlite3_context*,int); + int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); + int (*global_recover)(void); + void (*interruptx)(sqlite3*); + sqlite_int64 (*last_insert_rowid)(sqlite3*); + const char * (*libversion)(void); + int (*libversion_number)(void); + void *(*malloc)(int); + char * (*mprintf)(const char*,...); + int (*open)(const char*,sqlite3**); + int (*open16)(const void*,sqlite3**); + int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); + void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); + void *(*realloc)(void*,int); + int (*reset)(sqlite3_stmt*pStmt); + void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_double)(sqlite3_context*,double); + void (*result_error)(sqlite3_context*,const char*,int); + void (*result_error16)(sqlite3_context*,const void*,int); + void (*result_int)(sqlite3_context*,int); + void (*result_int64)(sqlite3_context*,sqlite_int64); + void (*result_null)(sqlite3_context*); + void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); + void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_value)(sqlite3_context*,sqlite3_value*); + void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); + int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, + const char*,const char*),void*); + void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); + char * (*snprintf)(int,char*,const char*,...); + int (*step)(sqlite3_stmt*); + int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, + char const**,char const**,int*,int*,int*); + void (*thread_cleanup)(void); + int (*total_changes)(sqlite3*); + void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); + int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); + void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*, + sqlite_int64),void*); + void * (*user_data)(sqlite3_context*); + const void * (*value_blob)(sqlite3_value*); + int (*value_bytes)(sqlite3_value*); + int (*value_bytes16)(sqlite3_value*); + double (*value_double)(sqlite3_value*); + int (*value_int)(sqlite3_value*); + sqlite_int64 (*value_int64)(sqlite3_value*); + int (*value_numeric_type)(sqlite3_value*); + const unsigned char * (*value_text)(sqlite3_value*); + const void * (*value_text16)(sqlite3_value*); + const void * (*value_text16be)(sqlite3_value*); + const void * (*value_text16le)(sqlite3_value*); + int (*value_type)(sqlite3_value*); + char *(*vmprintf)(const char*,va_list); + /* Added ??? */ + int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); + /* Added by 3.3.13 */ + int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + int (*clear_bindings)(sqlite3_stmt*); + /* Added by 3.4.1 */ + int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*, + void (*xDestroy)(void *)); + /* Added by 3.5.0 */ + int (*bind_zeroblob)(sqlite3_stmt*,int,int); + int (*blob_bytes)(sqlite3_blob*); + int (*blob_close)(sqlite3_blob*); + int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64, + int,sqlite3_blob**); + int (*blob_read)(sqlite3_blob*,void*,int,int); + int (*blob_write)(sqlite3_blob*,const void*,int,int); + int (*create_collation_v2)(sqlite3*,const char*,int,void*, + int(*)(void*,int,const void*,int,const void*), + void(*)(void*)); + int (*file_control)(sqlite3*,const char*,int,void*); + sqlite3_int64 (*memory_highwater)(int); + sqlite3_int64 (*memory_used)(void); + sqlite3_mutex *(*mutex_alloc)(int); + void (*mutex_enter)(sqlite3_mutex*); + void (*mutex_free)(sqlite3_mutex*); + void (*mutex_leave)(sqlite3_mutex*); + int (*mutex_try)(sqlite3_mutex*); + int (*open_v2)(const char*,sqlite3**,int,const char*); + int (*release_memory)(int); + void (*result_error_nomem)(sqlite3_context*); + void (*result_error_toobig)(sqlite3_context*); + int (*sleep)(int); + void (*soft_heap_limit)(int); + sqlite3_vfs *(*vfs_find)(const char*); + int (*vfs_register)(sqlite3_vfs*,int); + int (*vfs_unregister)(sqlite3_vfs*); + int (*xthreadsafe)(void); + void (*result_zeroblob)(sqlite3_context*,int); + void (*result_error_code)(sqlite3_context*,int); + int (*test_control)(int, ...); + void (*randomness)(int,void*); + sqlite3 *(*context_db_handle)(sqlite3_context*); + int (*extended_result_codes)(sqlite3*,int); + int (*limit)(sqlite3*,int,int); + sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); + const char *(*sql)(sqlite3_stmt*); + int (*status)(int,int*,int*,int); + int (*backup_finish)(sqlite3_backup*); + sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); + int (*backup_pagecount)(sqlite3_backup*); + int (*backup_remaining)(sqlite3_backup*); + int (*backup_step)(sqlite3_backup*,int); + const char *(*compileoption_get)(int); + int (*compileoption_used)(const char*); + int (*create_function_v2)(sqlite3*,const char*,int,int,void*, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*), + void(*xDestroy)(void*)); + int (*db_config)(sqlite3*,int,...); + sqlite3_mutex *(*db_mutex)(sqlite3*); + int (*db_status)(sqlite3*,int,int*,int*,int); + int (*extended_errcode)(sqlite3*); + void (*log)(int,const char*,...); + sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); + const char *(*sourceid)(void); + int (*stmt_status)(sqlite3_stmt*,int,int); + int (*strnicmp)(const char*,const char*,int); + int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); + int (*wal_autocheckpoint)(sqlite3*,int); + int (*wal_checkpoint)(sqlite3*,const char*); + void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); + int (*blob_reopen)(sqlite3_blob*,sqlite3_int64); + int (*vtab_config)(sqlite3*,int op,...); + int (*vtab_on_conflict)(sqlite3*); + /* Version 3.7.16 and later */ + int (*close_v2)(sqlite3*); + const char *(*db_filename)(sqlite3*,const char*); + int (*db_readonly)(sqlite3*,const char*); + int (*db_release_memory)(sqlite3*); + const char *(*errstr)(int); + int (*stmt_busy)(sqlite3_stmt*); + int (*stmt_readonly)(sqlite3_stmt*); + int (*stricmp)(const char*,const char*); + int (*uri_boolean)(const char*,const char*,int); + sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); + const char *(*uri_parameter)(const char*,const char*); + char *(*vsnprintf)(int,char*,const char*,va_list); + int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); + /* Version 3.8.7 and later */ + int (*auto_extension)(void(*)(void)); + int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64, + void(*)(void*)); + int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64, + void(*)(void*),unsigned char); + int (*cancel_auto_extension)(void(*)(void)); + int (*load_extension)(sqlite3*,const char*,const char*,char**); + void *(*malloc64)(sqlite3_uint64); + sqlite3_uint64 (*msize)(void*); + void *(*realloc64)(void*,sqlite3_uint64); + void (*reset_auto_extension)(void); + void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, + void(*)(void*)); + void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, + void(*)(void*), unsigned char); + int (*strglob)(const char*,const char*); + /* Version 3.8.11 and later */ + sqlite3_value *(*value_dup)(const sqlite3_value*); + void (*value_free)(sqlite3_value*); + int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64); + int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64); + /* Version 3.9.0 and later */ + unsigned int (*value_subtype)(sqlite3_value*); + void (*result_subtype)(sqlite3_context*,unsigned int); + /* Version 3.10.0 and later */ + int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); + int (*strlike)(const char*,const char*,unsigned int); + int (*db_cacheflush)(sqlite3*); + /* Version 3.12.0 and later */ + int (*system_errno)(sqlite3*); +}; + +/* +** The following macros redefine the API routines so that they are +** redirected through the global sqlite3_api structure. +** +** This header file is also used by the loadext.c source file +** (part of the main SQLite library - not an extension) so that +** it can get access to the sqlite3_api_routines structure +** definition. But the main library does not want to redefine +** the API. So the redefinition macros are only valid if the +** SQLITE_CORE macros is undefined. +*/ +#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) +#define sqlite3_aggregate_context sqlite3_api->aggregate_context +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_aggregate_count sqlite3_api->aggregate_count +#endif +#define sqlite3_bind_blob sqlite3_api->bind_blob +#define sqlite3_bind_double sqlite3_api->bind_double +#define sqlite3_bind_int sqlite3_api->bind_int +#define sqlite3_bind_int64 sqlite3_api->bind_int64 +#define sqlite3_bind_null sqlite3_api->bind_null +#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count +#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index +#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name +#define sqlite3_bind_text sqlite3_api->bind_text +#define sqlite3_bind_text16 sqlite3_api->bind_text16 +#define sqlite3_bind_value sqlite3_api->bind_value +#define sqlite3_busy_handler sqlite3_api->busy_handler +#define sqlite3_busy_timeout sqlite3_api->busy_timeout +#define sqlite3_changes sqlite3_api->changes +#define sqlite3_close sqlite3_api->close +#define sqlite3_collation_needed sqlite3_api->collation_needed +#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 +#define sqlite3_column_blob sqlite3_api->column_blob +#define sqlite3_column_bytes sqlite3_api->column_bytes +#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 +#define sqlite3_column_count sqlite3_api->column_count +#define sqlite3_column_database_name sqlite3_api->column_database_name +#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 +#define sqlite3_column_decltype sqlite3_api->column_decltype +#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 +#define sqlite3_column_double sqlite3_api->column_double +#define sqlite3_column_int sqlite3_api->column_int +#define sqlite3_column_int64 sqlite3_api->column_int64 +#define sqlite3_column_name sqlite3_api->column_name +#define sqlite3_column_name16 sqlite3_api->column_name16 +#define sqlite3_column_origin_name sqlite3_api->column_origin_name +#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 +#define sqlite3_column_table_name sqlite3_api->column_table_name +#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 +#define sqlite3_column_text sqlite3_api->column_text +#define sqlite3_column_text16 sqlite3_api->column_text16 +#define sqlite3_column_type sqlite3_api->column_type +#define sqlite3_column_value sqlite3_api->column_value +#define sqlite3_commit_hook sqlite3_api->commit_hook +#define sqlite3_complete sqlite3_api->complete +#define sqlite3_complete16 sqlite3_api->complete16 +#define sqlite3_create_collation sqlite3_api->create_collation +#define sqlite3_create_collation16 sqlite3_api->create_collation16 +#define sqlite3_create_function sqlite3_api->create_function +#define sqlite3_create_function16 sqlite3_api->create_function16 +#define sqlite3_create_module sqlite3_api->create_module +#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 +#define sqlite3_data_count sqlite3_api->data_count +#define sqlite3_db_handle sqlite3_api->db_handle +#define sqlite3_declare_vtab sqlite3_api->declare_vtab +#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache +#define sqlite3_errcode sqlite3_api->errcode +#define sqlite3_errmsg sqlite3_api->errmsg +#define sqlite3_errmsg16 sqlite3_api->errmsg16 +#define sqlite3_exec sqlite3_api->exec +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_expired sqlite3_api->expired +#endif +#define sqlite3_finalize sqlite3_api->finalize +#define sqlite3_free sqlite3_api->free +#define sqlite3_free_table sqlite3_api->free_table +#define sqlite3_get_autocommit sqlite3_api->get_autocommit +#define sqlite3_get_auxdata sqlite3_api->get_auxdata +#define sqlite3_get_table sqlite3_api->get_table +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_global_recover sqlite3_api->global_recover +#endif +#define sqlite3_interrupt sqlite3_api->interruptx +#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid +#define sqlite3_libversion sqlite3_api->libversion +#define sqlite3_libversion_number sqlite3_api->libversion_number +#define sqlite3_malloc sqlite3_api->malloc +#define sqlite3_mprintf sqlite3_api->mprintf +#define sqlite3_open sqlite3_api->open +#define sqlite3_open16 sqlite3_api->open16 +#define sqlite3_prepare sqlite3_api->prepare +#define sqlite3_prepare16 sqlite3_api->prepare16 +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_profile sqlite3_api->profile +#define sqlite3_progress_handler sqlite3_api->progress_handler +#define sqlite3_realloc sqlite3_api->realloc +#define sqlite3_reset sqlite3_api->reset +#define sqlite3_result_blob sqlite3_api->result_blob +#define sqlite3_result_double sqlite3_api->result_double +#define sqlite3_result_error sqlite3_api->result_error +#define sqlite3_result_error16 sqlite3_api->result_error16 +#define sqlite3_result_int sqlite3_api->result_int +#define sqlite3_result_int64 sqlite3_api->result_int64 +#define sqlite3_result_null sqlite3_api->result_null +#define sqlite3_result_text sqlite3_api->result_text +#define sqlite3_result_text16 sqlite3_api->result_text16 +#define sqlite3_result_text16be sqlite3_api->result_text16be +#define sqlite3_result_text16le sqlite3_api->result_text16le +#define sqlite3_result_value sqlite3_api->result_value +#define sqlite3_rollback_hook sqlite3_api->rollback_hook +#define sqlite3_set_authorizer sqlite3_api->set_authorizer +#define sqlite3_set_auxdata sqlite3_api->set_auxdata +#define sqlite3_snprintf sqlite3_api->snprintf +#define sqlite3_step sqlite3_api->step +#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata +#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup +#define sqlite3_total_changes sqlite3_api->total_changes +#define sqlite3_trace sqlite3_api->trace +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings +#endif +#define sqlite3_update_hook sqlite3_api->update_hook +#define sqlite3_user_data sqlite3_api->user_data +#define sqlite3_value_blob sqlite3_api->value_blob +#define sqlite3_value_bytes sqlite3_api->value_bytes +#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 +#define sqlite3_value_double sqlite3_api->value_double +#define sqlite3_value_int sqlite3_api->value_int +#define sqlite3_value_int64 sqlite3_api->value_int64 +#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type +#define sqlite3_value_text sqlite3_api->value_text +#define sqlite3_value_text16 sqlite3_api->value_text16 +#define sqlite3_value_text16be sqlite3_api->value_text16be +#define sqlite3_value_text16le sqlite3_api->value_text16le +#define sqlite3_value_type sqlite3_api->value_type +#define sqlite3_vmprintf sqlite3_api->vmprintf +#define sqlite3_vsnprintf sqlite3_api->vsnprintf +#define sqlite3_overload_function sqlite3_api->overload_function +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_clear_bindings sqlite3_api->clear_bindings +#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob +#define sqlite3_blob_bytes sqlite3_api->blob_bytes +#define sqlite3_blob_close sqlite3_api->blob_close +#define sqlite3_blob_open sqlite3_api->blob_open +#define sqlite3_blob_read sqlite3_api->blob_read +#define sqlite3_blob_write sqlite3_api->blob_write +#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 +#define sqlite3_file_control sqlite3_api->file_control +#define sqlite3_memory_highwater sqlite3_api->memory_highwater +#define sqlite3_memory_used sqlite3_api->memory_used +#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc +#define sqlite3_mutex_enter sqlite3_api->mutex_enter +#define sqlite3_mutex_free sqlite3_api->mutex_free +#define sqlite3_mutex_leave sqlite3_api->mutex_leave +#define sqlite3_mutex_try sqlite3_api->mutex_try +#define sqlite3_open_v2 sqlite3_api->open_v2 +#define sqlite3_release_memory sqlite3_api->release_memory +#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem +#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig +#define sqlite3_sleep sqlite3_api->sleep +#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit +#define sqlite3_vfs_find sqlite3_api->vfs_find +#define sqlite3_vfs_register sqlite3_api->vfs_register +#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister +#define sqlite3_threadsafe sqlite3_api->xthreadsafe +#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob +#define sqlite3_result_error_code sqlite3_api->result_error_code +#define sqlite3_test_control sqlite3_api->test_control +#define sqlite3_randomness sqlite3_api->randomness +#define sqlite3_context_db_handle sqlite3_api->context_db_handle +#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes +#define sqlite3_limit sqlite3_api->limit +#define sqlite3_next_stmt sqlite3_api->next_stmt +#define sqlite3_sql sqlite3_api->sql +#define sqlite3_status sqlite3_api->status +#define sqlite3_backup_finish sqlite3_api->backup_finish +#define sqlite3_backup_init sqlite3_api->backup_init +#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount +#define sqlite3_backup_remaining sqlite3_api->backup_remaining +#define sqlite3_backup_step sqlite3_api->backup_step +#define sqlite3_compileoption_get sqlite3_api->compileoption_get +#define sqlite3_compileoption_used sqlite3_api->compileoption_used +#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 +#define sqlite3_db_config sqlite3_api->db_config +#define sqlite3_db_mutex sqlite3_api->db_mutex +#define sqlite3_db_status sqlite3_api->db_status +#define sqlite3_extended_errcode sqlite3_api->extended_errcode +#define sqlite3_log sqlite3_api->log +#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 +#define sqlite3_sourceid sqlite3_api->sourceid +#define sqlite3_stmt_status sqlite3_api->stmt_status +#define sqlite3_strnicmp sqlite3_api->strnicmp +#define sqlite3_unlock_notify sqlite3_api->unlock_notify +#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint +#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint +#define sqlite3_wal_hook sqlite3_api->wal_hook +#define sqlite3_blob_reopen sqlite3_api->blob_reopen +#define sqlite3_vtab_config sqlite3_api->vtab_config +#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict +/* Version 3.7.16 and later */ +#define sqlite3_close_v2 sqlite3_api->close_v2 +#define sqlite3_db_filename sqlite3_api->db_filename +#define sqlite3_db_readonly sqlite3_api->db_readonly +#define sqlite3_db_release_memory sqlite3_api->db_release_memory +#define sqlite3_errstr sqlite3_api->errstr +#define sqlite3_stmt_busy sqlite3_api->stmt_busy +#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly +#define sqlite3_stricmp sqlite3_api->stricmp +#define sqlite3_uri_boolean sqlite3_api->uri_boolean +#define sqlite3_uri_int64 sqlite3_api->uri_int64 +#define sqlite3_uri_parameter sqlite3_api->uri_parameter +#define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf +#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 +/* Version 3.8.7 and later */ +#define sqlite3_auto_extension sqlite3_api->auto_extension +#define sqlite3_bind_blob64 sqlite3_api->bind_blob64 +#define sqlite3_bind_text64 sqlite3_api->bind_text64 +#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension +#define sqlite3_load_extension sqlite3_api->load_extension +#define sqlite3_malloc64 sqlite3_api->malloc64 +#define sqlite3_msize sqlite3_api->msize +#define sqlite3_realloc64 sqlite3_api->realloc64 +#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension +#define sqlite3_result_blob64 sqlite3_api->result_blob64 +#define sqlite3_result_text64 sqlite3_api->result_text64 +#define sqlite3_strglob sqlite3_api->strglob +/* Version 3.8.11 and later */ +#define sqlite3_value_dup sqlite3_api->value_dup +#define sqlite3_value_free sqlite3_api->value_free +#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64 +#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64 +/* Version 3.9.0 and later */ +#define sqlite3_value_subtype sqlite3_api->value_subtype +#define sqlite3_result_subtype sqlite3_api->result_subtype +/* Version 3.10.0 and later */ +#define sqlite3_status64 sqlite3_api->status64 +#define sqlite3_strlike sqlite3_api->strlike +#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush +/* Version 3.12.0 and later */ +#define sqlite3_system_errno sqlite3_api->system_errno +#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ + +#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) + /* This case when the file really is being compiled as a loadable + ** extension */ +# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; +# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; +# define SQLITE_EXTENSION_INIT3 \ + extern const sqlite3_api_routines *sqlite3_api; +#else + /* This case when the file is being statically linked into the + ** application */ +# define SQLITE_EXTENSION_INIT1 /*no-op*/ +# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ +# define SQLITE_EXTENSION_INIT3 /*no-op*/ +#endif + +#endif /* _SQLITE3EXT_H_ */ + +/************** End of sqlite3ext.h ******************************************/ +/************** Continuing where we left off in loadext.c ********************/ +/* #include "sqliteInt.h" */ +/* #include */ + +#ifndef SQLITE_OMIT_LOAD_EXTENSION + +/* +** Some API routines are omitted when various features are +** excluded from a build of SQLite. Substitute a NULL pointer +** for any missing APIs. +*/ +#ifndef SQLITE_ENABLE_COLUMN_METADATA +# define sqlite3_column_database_name 0 +# define sqlite3_column_database_name16 0 +# define sqlite3_column_table_name 0 +# define sqlite3_column_table_name16 0 +# define sqlite3_column_origin_name 0 +# define sqlite3_column_origin_name16 0 +#endif + +#ifdef SQLITE_OMIT_AUTHORIZATION +# define sqlite3_set_authorizer 0 +#endif + +#ifdef SQLITE_OMIT_UTF16 +# define sqlite3_bind_text16 0 +# define sqlite3_collation_needed16 0 +# define sqlite3_column_decltype16 0 +# define sqlite3_column_name16 0 +# define sqlite3_column_text16 0 +# define sqlite3_complete16 0 +# define sqlite3_create_collation16 0 +# define sqlite3_create_function16 0 +# define sqlite3_errmsg16 0 +# define sqlite3_open16 0 +# define sqlite3_prepare16 0 +# define sqlite3_prepare16_v2 0 +# define sqlite3_result_error16 0 +# define sqlite3_result_text16 0 +# define sqlite3_result_text16be 0 +# define sqlite3_result_text16le 0 +# define sqlite3_value_text16 0 +# define sqlite3_value_text16be 0 +# define sqlite3_value_text16le 0 +# define sqlite3_column_database_name16 0 +# define sqlite3_column_table_name16 0 +# define sqlite3_column_origin_name16 0 +#endif + +#ifdef SQLITE_OMIT_COMPLETE +# define sqlite3_complete 0 +# define sqlite3_complete16 0 +#endif + +#ifdef SQLITE_OMIT_DECLTYPE +# define sqlite3_column_decltype16 0 +# define sqlite3_column_decltype 0 +#endif + +#ifdef SQLITE_OMIT_PROGRESS_CALLBACK +# define sqlite3_progress_handler 0 +#endif + +#ifdef SQLITE_OMIT_VIRTUALTABLE +# define sqlite3_create_module 0 +# define sqlite3_create_module_v2 0 +# define sqlite3_declare_vtab 0 +# define sqlite3_vtab_config 0 +# define sqlite3_vtab_on_conflict 0 +#endif + +#ifdef SQLITE_OMIT_SHARED_CACHE +# define sqlite3_enable_shared_cache 0 +#endif + +#ifdef SQLITE_OMIT_TRACE +# define sqlite3_profile 0 +# define sqlite3_trace 0 +#endif + +#ifdef SQLITE_OMIT_GET_TABLE +# define sqlite3_free_table 0 +# define sqlite3_get_table 0 +#endif + +#ifdef SQLITE_OMIT_INCRBLOB +#define sqlite3_bind_zeroblob 0 +#define sqlite3_blob_bytes 0 +#define sqlite3_blob_close 0 +#define sqlite3_blob_open 0 +#define sqlite3_blob_read 0 +#define sqlite3_blob_write 0 +#define sqlite3_blob_reopen 0 +#endif + +/* +** The following structure contains pointers to all SQLite API routines. +** A pointer to this structure is passed into extensions when they are +** loaded so that the extension can make calls back into the SQLite +** library. +** +** When adding new APIs, add them to the bottom of this structure +** in order to preserve backwards compatibility. +** +** Extensions that use newer APIs should first call the +** sqlite3_libversion_number() to make sure that the API they +** intend to use is supported by the library. Extensions should +** also check to make sure that the pointer to the function is +** not NULL before calling it. +*/ +static const sqlite3_api_routines sqlite3Apis = { + sqlite3_aggregate_context, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_aggregate_count, +#else + 0, +#endif + sqlite3_bind_blob, + sqlite3_bind_double, + sqlite3_bind_int, + sqlite3_bind_int64, + sqlite3_bind_null, + sqlite3_bind_parameter_count, + sqlite3_bind_parameter_index, + sqlite3_bind_parameter_name, + sqlite3_bind_text, + sqlite3_bind_text16, + sqlite3_bind_value, + sqlite3_busy_handler, + sqlite3_busy_timeout, + sqlite3_changes, + sqlite3_close, + sqlite3_collation_needed, + sqlite3_collation_needed16, + sqlite3_column_blob, + sqlite3_column_bytes, + sqlite3_column_bytes16, + sqlite3_column_count, + sqlite3_column_database_name, + sqlite3_column_database_name16, + sqlite3_column_decltype, + sqlite3_column_decltype16, + sqlite3_column_double, + sqlite3_column_int, + sqlite3_column_int64, + sqlite3_column_name, + sqlite3_column_name16, + sqlite3_column_origin_name, + sqlite3_column_origin_name16, + sqlite3_column_table_name, + sqlite3_column_table_name16, + sqlite3_column_text, + sqlite3_column_text16, + sqlite3_column_type, + sqlite3_column_value, + sqlite3_commit_hook, + sqlite3_complete, + sqlite3_complete16, + sqlite3_create_collation, + sqlite3_create_collation16, + sqlite3_create_function, + sqlite3_create_function16, + sqlite3_create_module, + sqlite3_data_count, + sqlite3_db_handle, + sqlite3_declare_vtab, + sqlite3_enable_shared_cache, + sqlite3_errcode, + sqlite3_errmsg, + sqlite3_errmsg16, + sqlite3_exec, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_expired, +#else + 0, +#endif + sqlite3_finalize, + sqlite3_free, + sqlite3_free_table, + sqlite3_get_autocommit, + sqlite3_get_auxdata, + sqlite3_get_table, + 0, /* Was sqlite3_global_recover(), but that function is deprecated */ + sqlite3_interrupt, + sqlite3_last_insert_rowid, + sqlite3_libversion, + sqlite3_libversion_number, + sqlite3_malloc, + sqlite3_mprintf, + sqlite3_open, + sqlite3_open16, + sqlite3_prepare, + sqlite3_prepare16, + sqlite3_profile, + sqlite3_progress_handler, + sqlite3_realloc, + sqlite3_reset, + sqlite3_result_blob, + sqlite3_result_double, + sqlite3_result_error, + sqlite3_result_error16, + sqlite3_result_int, + sqlite3_result_int64, + sqlite3_result_null, + sqlite3_result_text, + sqlite3_result_text16, + sqlite3_result_text16be, + sqlite3_result_text16le, + sqlite3_result_value, + sqlite3_rollback_hook, + sqlite3_set_authorizer, + sqlite3_set_auxdata, + sqlite3_snprintf, + sqlite3_step, + sqlite3_table_column_metadata, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_thread_cleanup, +#else + 0, +#endif + sqlite3_total_changes, + sqlite3_trace, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_transfer_bindings, +#else + 0, +#endif + sqlite3_update_hook, + sqlite3_user_data, + sqlite3_value_blob, + sqlite3_value_bytes, + sqlite3_value_bytes16, + sqlite3_value_double, + sqlite3_value_int, + sqlite3_value_int64, + sqlite3_value_numeric_type, + sqlite3_value_text, + sqlite3_value_text16, + sqlite3_value_text16be, + sqlite3_value_text16le, + sqlite3_value_type, + sqlite3_vmprintf, + /* + ** The original API set ends here. All extensions can call any + ** of the APIs above provided that the pointer is not NULL. But + ** before calling APIs that follow, extension should check the + ** sqlite3_libversion_number() to make sure they are dealing with + ** a library that is new enough to support that API. + ************************************************************************* + */ + sqlite3_overload_function, + + /* + ** Added after 3.3.13 + */ + sqlite3_prepare_v2, + sqlite3_prepare16_v2, + sqlite3_clear_bindings, + + /* + ** Added for 3.4.1 + */ + sqlite3_create_module_v2, + + /* + ** Added for 3.5.0 + */ + sqlite3_bind_zeroblob, + sqlite3_blob_bytes, + sqlite3_blob_close, + sqlite3_blob_open, + sqlite3_blob_read, + sqlite3_blob_write, + sqlite3_create_collation_v2, + sqlite3_file_control, + sqlite3_memory_highwater, + sqlite3_memory_used, +#ifdef SQLITE_MUTEX_OMIT + 0, + 0, + 0, + 0, + 0, +#else + sqlite3_mutex_alloc, + sqlite3_mutex_enter, + sqlite3_mutex_free, + sqlite3_mutex_leave, + sqlite3_mutex_try, +#endif + sqlite3_open_v2, + sqlite3_release_memory, + sqlite3_result_error_nomem, + sqlite3_result_error_toobig, + sqlite3_sleep, + sqlite3_soft_heap_limit, + sqlite3_vfs_find, + sqlite3_vfs_register, + sqlite3_vfs_unregister, + + /* + ** Added for 3.5.8 + */ + sqlite3_threadsafe, + sqlite3_result_zeroblob, + sqlite3_result_error_code, + sqlite3_test_control, + sqlite3_randomness, + sqlite3_context_db_handle, + + /* + ** Added for 3.6.0 + */ + sqlite3_extended_result_codes, + sqlite3_limit, + sqlite3_next_stmt, + sqlite3_sql, + sqlite3_status, + + /* + ** Added for 3.7.4 + */ + sqlite3_backup_finish, + sqlite3_backup_init, + sqlite3_backup_pagecount, + sqlite3_backup_remaining, + sqlite3_backup_step, +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + sqlite3_compileoption_get, + sqlite3_compileoption_used, +#else + 0, + 0, +#endif + sqlite3_create_function_v2, + sqlite3_db_config, + sqlite3_db_mutex, + sqlite3_db_status, + sqlite3_extended_errcode, + sqlite3_log, + sqlite3_soft_heap_limit64, + sqlite3_sourceid, + sqlite3_stmt_status, + sqlite3_strnicmp, +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + sqlite3_unlock_notify, +#else + 0, +#endif +#ifndef SQLITE_OMIT_WAL + sqlite3_wal_autocheckpoint, + sqlite3_wal_checkpoint, + sqlite3_wal_hook, +#else + 0, + 0, + 0, +#endif + sqlite3_blob_reopen, + sqlite3_vtab_config, + sqlite3_vtab_on_conflict, + sqlite3_close_v2, + sqlite3_db_filename, + sqlite3_db_readonly, + sqlite3_db_release_memory, + sqlite3_errstr, + sqlite3_stmt_busy, + sqlite3_stmt_readonly, + sqlite3_stricmp, + sqlite3_uri_boolean, + sqlite3_uri_int64, + sqlite3_uri_parameter, + sqlite3_vsnprintf, + sqlite3_wal_checkpoint_v2, + /* Version 3.8.7 and later */ + sqlite3_auto_extension, + sqlite3_bind_blob64, + sqlite3_bind_text64, + sqlite3_cancel_auto_extension, + sqlite3_load_extension, + sqlite3_malloc64, + sqlite3_msize, + sqlite3_realloc64, + sqlite3_reset_auto_extension, + sqlite3_result_blob64, + sqlite3_result_text64, + sqlite3_strglob, + /* Version 3.8.11 and later */ + (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, + sqlite3_value_free, + sqlite3_result_zeroblob64, + sqlite3_bind_zeroblob64, + /* Version 3.9.0 and later */ + sqlite3_value_subtype, + sqlite3_result_subtype, + /* Version 3.10.0 and later */ + sqlite3_status64, + sqlite3_strlike, + sqlite3_db_cacheflush, + /* Version 3.12.0 and later */ + sqlite3_system_errno +}; + +/* +** Attempt to load an SQLite extension library contained in the file +** zFile. The entry point is zProc. zProc may be 0 in which case a +** default entry point name (sqlite3_extension_init) is used. Use +** of the default name is recommended. +** +** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. +** +** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with +** error message text. The calling function should free this memory +** by calling sqlite3DbFree(db, ). +*/ +static int sqlite3LoadExtension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +){ + sqlite3_vfs *pVfs = db->pVfs; + void *handle; + int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); + char *zErrmsg = 0; + const char *zEntry; + char *zAltEntry = 0; + void **aHandle; + u64 nMsg = 300 + sqlite3Strlen30(zFile); + int ii; + + /* Shared library endings to try if zFile cannot be loaded as written */ + static const char *azEndings[] = { +#if SQLITE_OS_WIN + "dll" +#elif defined(__APPLE__) + "dylib" +#else + "so" +#endif + }; + + + if( pzErrMsg ) *pzErrMsg = 0; + + /* Ticket #1863. To avoid a creating security problems for older + ** applications that relink against newer versions of SQLite, the + ** ability to run load_extension is turned off by default. One + ** must call either sqlite3_enable_load_extension(db) or + ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) + ** to turn on extension loading. + */ + if( (db->flags & SQLITE_LoadExtension)==0 ){ + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("not authorized"); + } + return SQLITE_ERROR; + } + + zEntry = zProc ? zProc : "sqlite3_extension_init"; + + handle = sqlite3OsDlOpen(pVfs, zFile); +#if SQLITE_OS_UNIX || SQLITE_OS_WIN + for(ii=0; ii sqlite3_example_init + ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init + */ + if( xInit==0 && zProc==0 ){ + int iFile, iEntry, c; + int ncFile = sqlite3Strlen30(zFile); + zAltEntry = sqlite3_malloc64(ncFile+30); + if( zAltEntry==0 ){ + sqlite3OsDlClose(pVfs, handle); + return SQLITE_NOMEM_BKPT; + } + memcpy(zAltEntry, "sqlite3_", 8); + for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} + iFile++; + if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; + for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ + if( sqlite3Isalpha(c) ){ + zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; + } + } + memcpy(zAltEntry+iEntry, "_init", 6); + zEntry = zAltEntry; + xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) + sqlite3OsDlSym(pVfs, handle, zEntry); + } + if( xInit==0 ){ + if( pzErrMsg ){ + nMsg += sqlite3Strlen30(zEntry); + *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); + if( zErrmsg ){ + sqlite3_snprintf(nMsg, zErrmsg, + "no entry point [%s] in shared library [%s]", zEntry, zFile); + sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); + } + } + sqlite3OsDlClose(pVfs, handle); + sqlite3_free(zAltEntry); + return SQLITE_ERROR; + } + sqlite3_free(zAltEntry); + if( xInit(db, &zErrmsg, &sqlite3Apis) ){ + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); + } + sqlite3_free(zErrmsg); + sqlite3OsDlClose(pVfs, handle); + return SQLITE_ERROR; + } + + /* Append the new shared library handle to the db->aExtension array. */ + aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); + if( aHandle==0 ){ + return SQLITE_NOMEM_BKPT; + } + if( db->nExtension>0 ){ + memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); + } + sqlite3DbFree(db, db->aExtension); + db->aExtension = aHandle; + + db->aExtension[db->nExtension++] = handle; + return SQLITE_OK; +} +SQLITE_API int SQLITE_STDCALL sqlite3_load_extension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +){ + int rc; + sqlite3_mutex_enter(db->mutex); + rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Call this routine when the database connection is closing in order +** to clean up loaded extensions +*/ +SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){ + int i; + assert( sqlite3_mutex_held(db->mutex) ); + for(i=0; inExtension; i++){ + sqlite3OsDlClose(db->pVfs, db->aExtension[i]); + } + sqlite3DbFree(db, db->aExtension); +} + +/* +** Enable or disable extension loading. Extension loading is disabled by +** default so as not to open security holes in older applications. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff){ + sqlite3_mutex_enter(db->mutex); + if( onoff ){ + db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; + }else{ + db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc); + } + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ + +/* +** The auto-extension code added regardless of whether or not extension +** loading is supported. We need a dummy sqlite3Apis pointer for that +** code if regular extension loading is not available. This is that +** dummy pointer. +*/ +#ifdef SQLITE_OMIT_LOAD_EXTENSION +static const sqlite3_api_routines sqlite3Apis = { 0 }; +#endif + + +/* +** The following object holds the list of automatically loaded +** extensions. +** +** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER +** mutex must be held while accessing this list. +*/ +typedef struct sqlite3AutoExtList sqlite3AutoExtList; +static SQLITE_WSD struct sqlite3AutoExtList { + u32 nExt; /* Number of entries in aExt[] */ + void (**aExt)(void); /* Pointers to the extension init functions */ +} sqlite3Autoext = { 0, 0 }; + +/* The "wsdAutoext" macro will resolve to the autoextension +** state vector. If writable static data is unsupported on the target, +** we have to locate the state vector at run-time. In the more common +** case where writable static data is supported, wsdStat can refer directly +** to the "sqlite3Autoext" state vector declared above. +*/ +#ifdef SQLITE_OMIT_WSD +# define wsdAutoextInit \ + sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) +# define wsdAutoext x[0] +#else +# define wsdAutoextInit +# define wsdAutoext sqlite3Autoext +#endif + + +/* +** Register a statically linked extension that is automatically +** loaded by every new database connection. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){ + int rc = SQLITE_OK; +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ){ + return rc; + }else +#endif + { + u32 i; +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + wsdAutoextInit; + sqlite3_mutex_enter(mutex); + for(i=0; i=0; i--){ + if( wsdAutoext.aExt[i]==xInit ){ + wsdAutoext.nExt--; + wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; + n++; + break; + } + } + sqlite3_mutex_leave(mutex); + return n; +} + +/* +** Reset the automatic extension loading mechanism. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void){ +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize()==SQLITE_OK ) +#endif + { +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + wsdAutoextInit; + sqlite3_mutex_enter(mutex); + sqlite3_free(wsdAutoext.aExt); + wsdAutoext.aExt = 0; + wsdAutoext.nExt = 0; + sqlite3_mutex_leave(mutex); + } +} + +/* +** Load all automatic extensions. +** +** If anything goes wrong, set an error in the database connection. +*/ +SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){ + u32 i; + int go = 1; + int rc; + int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); + + wsdAutoextInit; + if( wsdAutoext.nExt==0 ){ + /* Common case: early out without every having to acquire a mutex */ + return; + } + for(i=0; go; i++){ + char *zErrmsg; +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + sqlite3_mutex_enter(mutex); + if( i>=wsdAutoext.nExt ){ + xInit = 0; + go = 0; + }else{ + xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) + wsdAutoext.aExt[i]; + } + sqlite3_mutex_leave(mutex); + zErrmsg = 0; + if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){ + sqlite3ErrorWithMsg(db, rc, + "automatic extension loading failed: %s", zErrmsg); + go = 0; + } + sqlite3_free(zErrmsg); + } +} + +/************** End of loadext.c *********************************************/ +/************** Begin file pragma.c ******************************************/ +/* +** 2003 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the PRAGMA command. +*/ +/* #include "sqliteInt.h" */ + +#if !defined(SQLITE_ENABLE_LOCKING_STYLE) +# if defined(__APPLE__) +# define SQLITE_ENABLE_LOCKING_STYLE 1 +# else +# define SQLITE_ENABLE_LOCKING_STYLE 0 +# endif +#endif + +/*************************************************************************** +** The "pragma.h" include file is an automatically generated file that +** that includes the PragType_XXXX macro definitions and the aPragmaName[] +** object. This ensures that the aPragmaName[] table is arranged in +** lexicographical order to facility a binary search of the pragma name. +** Do not edit pragma.h directly. Edit and rerun the script in at +** ../tool/mkpragmatab.tcl. */ +/************** Include pragma.h in the middle of pragma.c *******************/ +/************** Begin file pragma.h ******************************************/ +/* DO NOT EDIT! +** This file is automatically generated by the script at +** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit +** that script and rerun it. +*/ +#define PragTyp_HEADER_VALUE 0 +#define PragTyp_AUTO_VACUUM 1 +#define PragTyp_FLAG 2 +#define PragTyp_BUSY_TIMEOUT 3 +#define PragTyp_CACHE_SIZE 4 +#define PragTyp_CACHE_SPILL 5 +#define PragTyp_CASE_SENSITIVE_LIKE 6 +#define PragTyp_COLLATION_LIST 7 +#define PragTyp_COMPILE_OPTIONS 8 +#define PragTyp_DATA_STORE_DIRECTORY 9 +#define PragTyp_DATABASE_LIST 10 +#define PragTyp_DEFAULT_CACHE_SIZE 11 +#define PragTyp_ENCODING 12 +#define PragTyp_FOREIGN_KEY_CHECK 13 +#define PragTyp_FOREIGN_KEY_LIST 14 +#define PragTyp_INCREMENTAL_VACUUM 15 +#define PragTyp_INDEX_INFO 16 +#define PragTyp_INDEX_LIST 17 +#define PragTyp_INTEGRITY_CHECK 18 +#define PragTyp_JOURNAL_MODE 19 +#define PragTyp_JOURNAL_SIZE_LIMIT 20 +#define PragTyp_LOCK_PROXY_FILE 21 +#define PragTyp_LOCKING_MODE 22 +#define PragTyp_PAGE_COUNT 23 +#define PragTyp_MMAP_SIZE 24 +#define PragTyp_PAGE_SIZE 25 +#define PragTyp_SECURE_DELETE 26 +#define PragTyp_SHRINK_MEMORY 27 +#define PragTyp_SOFT_HEAP_LIMIT 28 +#define PragTyp_STATS 29 +#define PragTyp_SYNCHRONOUS 30 +#define PragTyp_TABLE_INFO 31 +#define PragTyp_TEMP_STORE 32 +#define PragTyp_TEMP_STORE_DIRECTORY 33 +#define PragTyp_THREADS 34 +#define PragTyp_WAL_AUTOCHECKPOINT 35 +#define PragTyp_WAL_CHECKPOINT 36 +#define PragTyp_ACTIVATE_EXTENSIONS 37 +#define PragTyp_HEXKEY 38 +#define PragTyp_KEY 39 +#define PragTyp_REKEY 40 +#define PragTyp_LOCK_STATUS 41 +#define PragTyp_PARSER_TRACE 42 +#define PragFlag_NeedSchema 0x01 +#define PragFlag_ReadOnly 0x02 +static const struct sPragmaNames { + const char *const zName; /* Name of pragma */ + u8 ePragTyp; /* PragTyp_XXX value */ + u8 mPragFlag; /* Zero or more PragFlag_XXX values */ + u32 iArg; /* Extra argument */ +} aPragmaNames[] = { +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) + { /* zName: */ "activate_extensions", + /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) + { /* zName: */ "application_id", + /* ePragTyp: */ PragTyp_HEADER_VALUE, + /* ePragFlag: */ 0, + /* iArg: */ BTREE_APPLICATION_ID }, +#endif +#if !defined(SQLITE_OMIT_AUTOVACUUM) + { /* zName: */ "auto_vacuum", + /* ePragTyp: */ PragTyp_AUTO_VACUUM, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX) + { /* zName: */ "automatic_index", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_AutoIndex }, +#endif +#endif + { /* zName: */ "busy_timeout", + /* ePragTyp: */ PragTyp_BUSY_TIMEOUT, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "cache_size", + /* ePragTyp: */ PragTyp_CACHE_SIZE, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "cache_spill", + /* ePragTyp: */ PragTyp_CACHE_SPILL, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif + { /* zName: */ "case_sensitive_like", + /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "cell_size_check", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_CellSizeCk }, +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "checkpoint_fullfsync", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_CkptFullFSync }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + { /* zName: */ "collation_list", + /* ePragTyp: */ PragTyp_COLLATION_LIST, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS) + { /* zName: */ "compile_options", + /* ePragTyp: */ PragTyp_COMPILE_OPTIONS, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "count_changes", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_CountRows }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN + { /* zName: */ "data_store_directory", + /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) + { /* zName: */ "data_version", + /* ePragTyp: */ PragTyp_HEADER_VALUE, + /* ePragFlag: */ PragFlag_ReadOnly, + /* iArg: */ BTREE_DATA_VERSION }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + { /* zName: */ "database_list", + /* ePragTyp: */ PragTyp_DATABASE_LIST, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) + { /* zName: */ "default_cache_size", + /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + { /* zName: */ "defer_foreign_keys", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_DeferFKs }, +#endif +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "empty_result_callbacks", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_NullCallback }, +#endif +#if !defined(SQLITE_OMIT_UTF16) + { /* zName: */ "encoding", + /* ePragTyp: */ PragTyp_ENCODING, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + { /* zName: */ "foreign_key_check", + /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FOREIGN_KEY) + { /* zName: */ "foreign_key_list", + /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + { /* zName: */ "foreign_keys", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_ForeignKeys }, +#endif +#endif +#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) + { /* zName: */ "freelist_count", + /* ePragTyp: */ PragTyp_HEADER_VALUE, + /* ePragFlag: */ PragFlag_ReadOnly, + /* iArg: */ BTREE_FREE_PAGE_COUNT }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "full_column_names", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_FullColNames }, + { /* zName: */ "fullfsync", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_FullFSync }, +#endif +#if defined(SQLITE_HAS_CODEC) + { /* zName: */ "hexkey", + /* ePragTyp: */ PragTyp_HEXKEY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "hexrekey", + /* ePragTyp: */ PragTyp_HEXKEY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if !defined(SQLITE_OMIT_CHECK) + { /* zName: */ "ignore_check_constraints", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_IgnoreChecks }, +#endif +#endif +#if !defined(SQLITE_OMIT_AUTOVACUUM) + { /* zName: */ "incremental_vacuum", + /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + { /* zName: */ "index_info", + /* ePragTyp: */ PragTyp_INDEX_INFO, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, + { /* zName: */ "index_list", + /* ePragTyp: */ PragTyp_INDEX_LIST, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, + { /* zName: */ "index_xinfo", + /* ePragTyp: */ PragTyp_INDEX_INFO, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 1 }, +#endif +#if !defined(SQLITE_OMIT_INTEGRITY_CHECK) + { /* zName: */ "integrity_check", + /* ePragTyp: */ PragTyp_INTEGRITY_CHECK, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "journal_mode", + /* ePragTyp: */ PragTyp_JOURNAL_MODE, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, + { /* zName: */ "journal_size_limit", + /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if defined(SQLITE_HAS_CODEC) + { /* zName: */ "key", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "legacy_file_format", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_LegacyFileFmt }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE + { /* zName: */ "lock_proxy_file", + /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + { /* zName: */ "lock_status", + /* ePragTyp: */ PragTyp_LOCK_STATUS, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "locking_mode", + /* ePragTyp: */ PragTyp_LOCKING_MODE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "max_page_count", + /* ePragTyp: */ PragTyp_PAGE_COUNT, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, + { /* zName: */ "mmap_size", + /* ePragTyp: */ PragTyp_MMAP_SIZE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "page_count", + /* ePragTyp: */ PragTyp_PAGE_COUNT, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, + { /* zName: */ "page_size", + /* ePragTyp: */ PragTyp_PAGE_SIZE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE) + { /* zName: */ "parser_trace", + /* ePragTyp: */ PragTyp_PARSER_TRACE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "query_only", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_QueryOnly }, +#endif +#if !defined(SQLITE_OMIT_INTEGRITY_CHECK) + { /* zName: */ "quick_check", + /* ePragTyp: */ PragTyp_INTEGRITY_CHECK, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "read_uncommitted", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_ReadUncommitted }, + { /* zName: */ "recursive_triggers", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_RecTriggers }, +#endif +#if defined(SQLITE_HAS_CODEC) + { /* zName: */ "rekey", + /* ePragTyp: */ PragTyp_REKEY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "reverse_unordered_selects", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_ReverseOrder }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) + { /* zName: */ "schema_version", + /* ePragTyp: */ PragTyp_HEADER_VALUE, + /* ePragFlag: */ 0, + /* iArg: */ BTREE_SCHEMA_VERSION }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "secure_delete", + /* ePragTyp: */ PragTyp_SECURE_DELETE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "short_column_names", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_ShortColNames }, +#endif + { /* zName: */ "shrink_memory", + /* ePragTyp: */ PragTyp_SHRINK_MEMORY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "soft_heap_limit", + /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if defined(SQLITE_DEBUG) + { /* zName: */ "sql_trace", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_SqlTrace }, +#endif +#endif +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + { /* zName: */ "stats", + /* ePragTyp: */ PragTyp_STATS, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "synchronous", + /* ePragTyp: */ PragTyp_SYNCHRONOUS, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + { /* zName: */ "table_info", + /* ePragTyp: */ PragTyp_TABLE_INFO, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + { /* zName: */ "temp_store", + /* ePragTyp: */ PragTyp_TEMP_STORE, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "temp_store_directory", + /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#endif + { /* zName: */ "threads", + /* ePragTyp: */ PragTyp_THREADS, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, +#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) + { /* zName: */ "user_version", + /* ePragTyp: */ PragTyp_HEADER_VALUE, + /* ePragFlag: */ 0, + /* iArg: */ BTREE_USER_VERSION }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) +#if defined(SQLITE_DEBUG) + { /* zName: */ "vdbe_addoptrace", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_VdbeAddopTrace }, + { /* zName: */ "vdbe_debug", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace }, + { /* zName: */ "vdbe_eqp", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_VdbeEQP }, + { /* zName: */ "vdbe_listing", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_VdbeListing }, + { /* zName: */ "vdbe_trace", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_VdbeTrace }, +#endif +#endif +#if !defined(SQLITE_OMIT_WAL) + { /* zName: */ "wal_autocheckpoint", + /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT, + /* ePragFlag: */ 0, + /* iArg: */ 0 }, + { /* zName: */ "wal_checkpoint", + /* ePragTyp: */ PragTyp_WAL_CHECKPOINT, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, +#endif +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) + { /* zName: */ "writable_schema", + /* ePragTyp: */ PragTyp_FLAG, + /* ePragFlag: */ 0, + /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode }, +#endif +}; +/* Number of pragmas: 60 on by default, 73 total. */ + +/************** End of pragma.h **********************************************/ +/************** Continuing where we left off in pragma.c *********************/ + +/* +** Interpret the given string as a safety level. Return 0 for OFF, +** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or +** unrecognized string argument. The FULL and EXTRA option is disallowed +** if the omitFull parameter it 1. +** +** Note that the values returned are one less that the values that +** should be passed into sqlite3BtreeSetSafetyLevel(). The is done +** to support legacy SQL code. The safety level used to be boolean +** and older scripts may have used numbers 0 for OFF and 1 for ON. +*/ +static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){ + /* 123456789 123456789 123 */ + static const char zText[] = "onoffalseyestruextrafull"; + static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20}; + static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4}; + static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2}; + /* on no off false yes true extra full */ + int i, n; + if( sqlite3Isdigit(*z) ){ + return (u8)sqlite3Atoi(z); + } + n = sqlite3Strlen30(z); + for(i=0; i=0&&i<=2)?i:0); +} +#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** Interpret the given string as a temp db location. Return 1 for file +** backed temporary databases, 2 for the Red-Black tree in memory database +** and 0 to use the compile-time default. +*/ +static int getTempStore(const char *z){ + if( z[0]>='0' && z[0]<='2' ){ + return z[0] - '0'; + }else if( sqlite3StrICmp(z, "file")==0 ){ + return 1; + }else if( sqlite3StrICmp(z, "memory")==0 ){ + return 2; + }else{ + return 0; + } +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** Invalidate temp storage, either when the temp storage is changed +** from default, or when 'file' and the temp_store_directory has changed +*/ +static int invalidateTempStorage(Parse *pParse){ + sqlite3 *db = pParse->db; + if( db->aDb[1].pBt!=0 ){ + if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ + sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " + "from within a transaction"); + return SQLITE_ERROR; + } + sqlite3BtreeClose(db->aDb[1].pBt); + db->aDb[1].pBt = 0; + sqlite3ResetAllSchemasOfConnection(db); + } + return SQLITE_OK; +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** If the TEMP database is open, close it and mark the database schema +** as needing reloading. This must be done when using the SQLITE_TEMP_STORE +** or DEFAULT_TEMP_STORE pragmas. +*/ +static int changeTempStorage(Parse *pParse, const char *zStorageType){ + int ts = getTempStore(zStorageType); + sqlite3 *db = pParse->db; + if( db->temp_store==ts ) return SQLITE_OK; + if( invalidateTempStorage( pParse ) != SQLITE_OK ){ + return SQLITE_ERROR; + } + db->temp_store = (u8)ts; + return SQLITE_OK; +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +/* +** Set the names of the first N columns to the values in azCol[] +*/ +static void setAllColumnNames( + Vdbe *v, /* The query under construction */ + int N, /* Number of columns */ + const char **azCol /* Names of columns */ +){ + int i; + sqlite3VdbeSetNumCols(v, N); + for(i=0; iautoCommit ){ + Db *pDb = db->aDb; + int n = db->nDb; + assert( SQLITE_FullFSync==PAGER_FULLFSYNC ); + assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC ); + assert( SQLITE_CacheSpill==PAGER_CACHESPILL ); + assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL) + == PAGER_FLAGS_MASK ); + assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level ); + while( (n--) > 0 ){ + if( pDb->pBt ){ + sqlite3BtreeSetPagerFlags(pDb->pBt, + pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) ); + } + pDb++; + } + } +} +#else +# define setAllPagerFlags(X) /* no-op */ +#endif + + +/* +** Return a human-readable name for a constraint resolution action. +*/ +#ifndef SQLITE_OMIT_FOREIGN_KEY +static const char *actionName(u8 action){ + const char *zName; + switch( action ){ + case OE_SetNull: zName = "SET NULL"; break; + case OE_SetDflt: zName = "SET DEFAULT"; break; + case OE_Cascade: zName = "CASCADE"; break; + case OE_Restrict: zName = "RESTRICT"; break; + default: zName = "NO ACTION"; + assert( action==OE_None ); break; + } + return zName; +} +#endif + + +/* +** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants +** defined in pager.h. This function returns the associated lowercase +** journal-mode name. +*/ +SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){ + static char * const azModeName[] = { + "delete", "persist", "off", "truncate", "memory" +#ifndef SQLITE_OMIT_WAL + , "wal" +#endif + }; + assert( PAGER_JOURNALMODE_DELETE==0 ); + assert( PAGER_JOURNALMODE_PERSIST==1 ); + assert( PAGER_JOURNALMODE_OFF==2 ); + assert( PAGER_JOURNALMODE_TRUNCATE==3 ); + assert( PAGER_JOURNALMODE_MEMORY==4 ); + assert( PAGER_JOURNALMODE_WAL==5 ); + assert( eMode>=0 && eMode<=ArraySize(azModeName) ); + + if( eMode==ArraySize(azModeName) ) return 0; + return azModeName[eMode]; +} + +/* +** Process a pragma statement. +** +** Pragmas are of this form: +** +** PRAGMA [schema.]id [= value] +** +** The identifier might also be a string. The value is a string, and +** identifier, or a number. If minusFlag is true, then the value is +** a number that was preceded by a minus sign. +** +** If the left side is "database.id" then pId1 is the database name +** and pId2 is the id. If the left side is just "id" then pId1 is the +** id and pId2 is any empty string. +*/ +SQLITE_PRIVATE void sqlite3Pragma( + Parse *pParse, + Token *pId1, /* First part of [schema.]id field */ + Token *pId2, /* Second part of [schema.]id field, or NULL */ + Token *pValue, /* Token for , or NULL */ + int minusFlag /* True if a '-' sign preceded */ +){ + char *zLeft = 0; /* Nul-terminated UTF-8 string */ + char *zRight = 0; /* Nul-terminated UTF-8 string , or NULL */ + const char *zDb = 0; /* The database name */ + Token *pId; /* Pointer to token */ + char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ + int iDb; /* Database index for */ + int lwr, upr, mid = 0; /* Binary search bounds */ + int rc; /* return value form SQLITE_FCNTL_PRAGMA */ + sqlite3 *db = pParse->db; /* The database connection */ + Db *pDb; /* The specific database being pragmaed */ + Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ + const struct sPragmaNames *pPragma; + + if( v==0 ) return; + sqlite3VdbeRunOnlyOnce(v); + pParse->nMem = 2; + + /* Interpret the [schema.] part of the pragma statement. iDb is the + ** index of the database this pragma is being applied to in db.aDb[]. */ + iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); + if( iDb<0 ) return; + pDb = &db->aDb[iDb]; + + /* If the temp database has been explicitly named as part of the + ** pragma, make sure it is open. + */ + if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ + return; + } + + zLeft = sqlite3NameFromToken(db, pId); + if( !zLeft ) return; + if( minusFlag ){ + zRight = sqlite3MPrintf(db, "-%T", pValue); + }else{ + zRight = sqlite3NameFromToken(db, pValue); + } + + assert( pId2 ); + zDb = pId2->n>0 ? pDb->zName : 0; + if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ + goto pragma_out; + } + + /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS + ** connection. If it returns SQLITE_OK, then assume that the VFS + ** handled the pragma and generate a no-op prepared statement. + ** + ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, + ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file + ** object corresponding to the database file to which the pragma + ** statement refers. + ** + ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA + ** file control is an array of pointers to strings (char**) in which the + ** second element of the array is the name of the pragma and the third + ** element is the argument to the pragma or NULL if the pragma has no + ** argument. + */ + aFcntl[0] = 0; + aFcntl[1] = zLeft; + aFcntl[2] = zRight; + aFcntl[3] = 0; + db->busyHandler.nBusy = 0; + rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); + if( rc==SQLITE_OK ){ + returnSingleText(v, "result", aFcntl[0]); + sqlite3_free(aFcntl[0]); + goto pragma_out; + } + if( rc!=SQLITE_NOTFOUND ){ + if( aFcntl[0] ){ + sqlite3ErrorMsg(pParse, "%s", aFcntl[0]); + sqlite3_free(aFcntl[0]); + } + pParse->nErr++; + pParse->rc = rc; + goto pragma_out; + } + + /* Locate the pragma in the lookup table */ + lwr = 0; + upr = ArraySize(aPragmaNames)-1; + while( lwr<=upr ){ + mid = (lwr+upr)/2; + rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName); + if( rc==0 ) break; + if( rc<0 ){ + upr = mid - 1; + }else{ + lwr = mid + 1; + } + } + if( lwr>upr ) goto pragma_out; + pPragma = &aPragmaNames[mid]; + + /* Make sure the database schema is loaded if the pragma requires that */ + if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + } + + /* Jump to the appropriate pragma handler */ + switch( pPragma->ePragTyp ){ + +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) + /* + ** PRAGMA [schema.]default_cache_size + ** PRAGMA [schema.]default_cache_size=N + ** + ** The first form reports the current persistent setting for the + ** page cache size. The value returned is the maximum number of + ** pages in the page cache. The second form sets both the current + ** page cache size value and the persistent page cache size value + ** stored in the database file. + ** + ** Older versions of SQLite would set the default cache size to a + ** negative number to indicate synchronous=OFF. These days, synchronous + ** is always on by default regardless of the sign of the default cache + ** size. But continue to take the absolute value of the default cache + ** size of historical compatibility. + */ + case PragTyp_DEFAULT_CACHE_SIZE: { + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList getCacheSize[] = { + { OP_Transaction, 0, 0, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ + { OP_IfPos, 1, 8, 0}, + { OP_Integer, 0, 2, 0}, + { OP_Subtract, 1, 2, 1}, + { OP_IfPos, 1, 8, 0}, + { OP_Integer, 0, 1, 0}, /* 6 */ + { OP_Noop, 0, 0, 0}, + { OP_ResultRow, 1, 1, 0}, + }; + VdbeOp *aOp; + sqlite3VdbeUsesBtree(v, iDb); + if( !zRight ){ + setOneColumnName(v, "cache_size"); + pParse->nMem += 2; + sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); + aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); + if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; + aOp[0].p1 = iDb; + aOp[1].p1 = iDb; + aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE; + }else{ + int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size); + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pDb->pSchema->cache_size = size; + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + break; + } +#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */ + +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) + /* + ** PRAGMA [schema.]page_size + ** PRAGMA [schema.]page_size=N + ** + ** The first form reports the current setting for the + ** database page size in bytes. The second form sets the + ** database page size value. The value can only be set if + ** the database has not yet been created. + */ + case PragTyp_PAGE_SIZE: { + Btree *pBt = pDb->pBt; + assert( pBt!=0 ); + if( !zRight ){ + int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; + returnSingleInt(v, "page_size", size); + }else{ + /* Malloc may fail when setting the page-size, as there is an internal + ** buffer that the pager module resizes using sqlite3_realloc(). + */ + db->nextPagesize = sqlite3Atoi(zRight); + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ + sqlite3OomFault(db); + } + } + break; + } + + /* + ** PRAGMA [schema.]secure_delete + ** PRAGMA [schema.]secure_delete=ON/OFF + ** + ** The first form reports the current setting for the + ** secure_delete flag. The second form changes the secure_delete + ** flag setting and reports thenew value. + */ + case PragTyp_SECURE_DELETE: { + Btree *pBt = pDb->pBt; + int b = -1; + assert( pBt!=0 ); + if( zRight ){ + b = sqlite3GetBoolean(zRight, 0); + } + if( pId2->n==0 && b>=0 ){ + int ii; + for(ii=0; iinDb; ii++){ + sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); + } + } + b = sqlite3BtreeSecureDelete(pBt, b); + returnSingleInt(v, "secure_delete", b); + break; + } + + /* + ** PRAGMA [schema.]max_page_count + ** PRAGMA [schema.]max_page_count=N + ** + ** The first form reports the current setting for the + ** maximum number of pages in the database file. The + ** second form attempts to change this setting. Both + ** forms return the current setting. + ** + ** The absolute value of N is used. This is undocumented and might + ** change. The only purpose is to provide an easy way to test + ** the sqlite3AbsInt32() function. + ** + ** PRAGMA [schema.]page_count + ** + ** Return the number of pages in the specified database. + */ + case PragTyp_PAGE_COUNT: { + int iReg; + sqlite3CodeVerifySchema(pParse, iDb); + iReg = ++pParse->nMem; + if( sqlite3Tolower(zLeft[0])=='p' ){ + sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); + }else{ + sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, + sqlite3AbsInt32(sqlite3Atoi(zRight))); + } + sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); + break; + } + + /* + ** PRAGMA [schema.]locking_mode + ** PRAGMA [schema.]locking_mode = (normal|exclusive) + */ + case PragTyp_LOCKING_MODE: { + const char *zRet = "normal"; + int eMode = getLockingMode(zRight); + + if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ + /* Simple "PRAGMA locking_mode;" statement. This is a query for + ** the current default locking mode (which may be different to + ** the locking-mode of the main database). + */ + eMode = db->dfltLockMode; + }else{ + Pager *pPager; + if( pId2->n==0 ){ + /* This indicates that no database name was specified as part + ** of the PRAGMA command. In this case the locking-mode must be + ** set on all attached databases, as well as the main db file. + ** + ** Also, the sqlite3.dfltLockMode variable is set so that + ** any subsequently attached databases also use the specified + ** locking mode. + */ + int ii; + assert(pDb==&db->aDb[0]); + for(ii=2; iinDb; ii++){ + pPager = sqlite3BtreePager(db->aDb[ii].pBt); + sqlite3PagerLockingMode(pPager, eMode); + } + db->dfltLockMode = (u8)eMode; + } + pPager = sqlite3BtreePager(pDb->pBt); + eMode = sqlite3PagerLockingMode(pPager, eMode); + } + + assert( eMode==PAGER_LOCKINGMODE_NORMAL + || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); + if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ + zRet = "exclusive"; + } + returnSingleText(v, "locking_mode", zRet); + break; + } + + /* + ** PRAGMA [schema.]journal_mode + ** PRAGMA [schema.]journal_mode = + ** (delete|persist|off|truncate|memory|wal|off) + */ + case PragTyp_JOURNAL_MODE: { + int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ + int ii; /* Loop counter */ + + setOneColumnName(v, "journal_mode"); + if( zRight==0 ){ + /* If there is no "=MODE" part of the pragma, do a query for the + ** current mode */ + eMode = PAGER_JOURNALMODE_QUERY; + }else{ + const char *zMode; + int n = sqlite3Strlen30(zRight); + for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ + if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; + } + if( !zMode ){ + /* If the "=MODE" part does not match any known journal mode, + ** then do a query */ + eMode = PAGER_JOURNALMODE_QUERY; + } + } + if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ + /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ + iDb = 0; + pId2->n = 1; + } + for(ii=db->nDb-1; ii>=0; ii--){ + if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ + sqlite3VdbeUsesBtree(v, ii); + sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); + } + } + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + break; + } + + /* + ** PRAGMA [schema.]journal_size_limit + ** PRAGMA [schema.]journal_size_limit=N + ** + ** Get or set the size limit on rollback journal files. + */ + case PragTyp_JOURNAL_SIZE_LIMIT: { + Pager *pPager = sqlite3BtreePager(pDb->pBt); + i64 iLimit = -2; + if( zRight ){ + sqlite3DecOrHexToI64(zRight, &iLimit); + if( iLimit<-1 ) iLimit = -1; + } + iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); + returnSingleInt(v, "journal_size_limit", iLimit); + break; + } + +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */ + + /* + ** PRAGMA [schema.]auto_vacuum + ** PRAGMA [schema.]auto_vacuum=N + ** + ** Get or set the value of the database 'auto-vacuum' parameter. + ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + case PragTyp_AUTO_VACUUM: { + Btree *pBt = pDb->pBt; + assert( pBt!=0 ); + if( !zRight ){ + returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt)); + }else{ + int eAuto = getAutoVacuum(zRight); + assert( eAuto>=0 && eAuto<=2 ); + db->nextAutovac = (u8)eAuto; + /* Call SetAutoVacuum() to set initialize the internal auto and + ** incr-vacuum flags. This is required in case this connection + ** creates the database file. It is important that it is created + ** as an auto-vacuum capable db. + */ + rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); + if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ + /* When setting the auto_vacuum mode to either "full" or + ** "incremental", write the value of meta[6] in the database + ** file. Before writing to meta[6], check that meta[3] indicates + ** that this really is an auto-vacuum capable database. + */ + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList setMeta6[] = { + { OP_Transaction, 0, 1, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, + { OP_If, 1, 0, 0}, /* 2 */ + { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ + { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */ + }; + VdbeOp *aOp; + int iAddr = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6)); + aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn); + if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; + aOp[0].p1 = iDb; + aOp[1].p1 = iDb; + aOp[2].p2 = iAddr+4; + aOp[4].p1 = iDb; + aOp[4].p3 = eAuto - 1; + sqlite3VdbeUsesBtree(v, iDb); + } + } + break; + } +#endif + + /* + ** PRAGMA [schema.]incremental_vacuum(N) + ** + ** Do N steps of incremental vacuuming on a database. + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + case PragTyp_INCREMENTAL_VACUUM: { + int iLimit, addr; + if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ + iLimit = 0x7fffffff; + } + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); + addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v); + sqlite3VdbeAddOp1(v, OP_ResultRow, 1); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); + sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr); + break; + } +#endif + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS + /* + ** PRAGMA [schema.]cache_size + ** PRAGMA [schema.]cache_size=N + ** + ** The first form reports the current local setting for the + ** page cache size. The second form sets the local + ** page cache size value. If N is positive then that is the + ** number of pages in the cache. If N is negative, then the + ** number of pages is adjusted so that the cache uses -N kibibytes + ** of memory. + */ + case PragTyp_CACHE_SIZE: { + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( !zRight ){ + returnSingleInt(v, "cache_size", pDb->pSchema->cache_size); + }else{ + int size = sqlite3Atoi(zRight); + pDb->pSchema->cache_size = size; + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + break; + } + + /* + ** PRAGMA [schema.]cache_spill + ** PRAGMA cache_spill=BOOLEAN + ** PRAGMA [schema.]cache_spill=N + ** + ** The first form reports the current local setting for the + ** page cache spill size. The second form turns cache spill on + ** or off. When turnning cache spill on, the size is set to the + ** current cache_size. The third form sets a spill size that + ** may be different form the cache size. + ** If N is positive then that is the + ** number of pages in the cache. If N is negative, then the + ** number of pages is adjusted so that the cache uses -N kibibytes + ** of memory. + ** + ** If the number of cache_spill pages is less then the number of + ** cache_size pages, no spilling occurs until the page count exceeds + ** the number of cache_size pages. + ** + ** The cache_spill=BOOLEAN setting applies to all attached schemas, + ** not just the schema specified. + */ + case PragTyp_CACHE_SPILL: { + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( !zRight ){ + returnSingleInt(v, "cache_spill", + (db->flags & SQLITE_CacheSpill)==0 ? 0 : + sqlite3BtreeSetSpillSize(pDb->pBt,0)); + }else{ + int size = 1; + if( sqlite3GetInt32(zRight, &size) ){ + sqlite3BtreeSetSpillSize(pDb->pBt, size); + } + if( sqlite3GetBoolean(zRight, size!=0) ){ + db->flags |= SQLITE_CacheSpill; + }else{ + db->flags &= ~SQLITE_CacheSpill; + } + setAllPagerFlags(db); + } + break; + } + + /* + ** PRAGMA [schema.]mmap_size(N) + ** + ** Used to set mapping size limit. The mapping size limit is + ** used to limit the aggregate size of all memory mapped regions of the + ** database file. If this parameter is set to zero, then memory mapping + ** is not used at all. If N is negative, then the default memory map + ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. + ** The parameter N is measured in bytes. + ** + ** This value is advisory. The underlying VFS is free to memory map + ** as little or as much as it wants. Except, if N is set to 0 then the + ** upper layers will never invoke the xFetch interfaces to the VFS. + */ + case PragTyp_MMAP_SIZE: { + sqlite3_int64 sz; +#if SQLITE_MAX_MMAP_SIZE>0 + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( zRight ){ + int ii; + sqlite3DecOrHexToI64(zRight, &sz); + if( sz<0 ) sz = sqlite3GlobalConfig.szMmap; + if( pId2->n==0 ) db->szMmap = sz; + for(ii=db->nDb-1; ii>=0; ii--){ + if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ + sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); + } + } + } + sz = -1; + rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); +#else + sz = 0; + rc = SQLITE_OK; +#endif + if( rc==SQLITE_OK ){ + returnSingleInt(v, "mmap_size", sz); + }else if( rc!=SQLITE_NOTFOUND ){ + pParse->nErr++; + pParse->rc = rc; + } + break; + } + + /* + ** PRAGMA temp_store + ** PRAGMA temp_store = "default"|"memory"|"file" + ** + ** Return or set the local value of the temp_store flag. Changing + ** the local value does not make changes to the disk file and the default + ** value will be restored the next time the database is opened. + ** + ** Note that it is possible for the library compile-time options to + ** override this setting + */ + case PragTyp_TEMP_STORE: { + if( !zRight ){ + returnSingleInt(v, "temp_store", db->temp_store); + }else{ + changeTempStorage(pParse, zRight); + } + break; + } + + /* + ** PRAGMA temp_store_directory + ** PRAGMA temp_store_directory = ""|"directory_name" + ** + ** Return or set the local value of the temp_store_directory flag. Changing + ** the value sets a specific directory to be used for temporary files. + ** Setting to a null string reverts to the default temporary directory search. + ** If temporary directory is changed, then invalidateTempStorage. + ** + */ + case PragTyp_TEMP_STORE_DIRECTORY: { + if( !zRight ){ + returnSingleText(v, "temp_store_directory", sqlite3_temp_directory); + }else{ +#ifndef SQLITE_OMIT_WSD + if( zRight[0] ){ + int res; + rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); + if( rc!=SQLITE_OK || res==0 ){ + sqlite3ErrorMsg(pParse, "not a writable directory"); + goto pragma_out; + } + } + if( SQLITE_TEMP_STORE==0 + || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) + || (SQLITE_TEMP_STORE==2 && db->temp_store==1) + ){ + invalidateTempStorage(pParse); + } + sqlite3_free(sqlite3_temp_directory); + if( zRight[0] ){ + sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); + }else{ + sqlite3_temp_directory = 0; + } +#endif /* SQLITE_OMIT_WSD */ + } + break; + } + +#if SQLITE_OS_WIN + /* + ** PRAGMA data_store_directory + ** PRAGMA data_store_directory = ""|"directory_name" + ** + ** Return or set the local value of the data_store_directory flag. Changing + ** the value sets a specific directory to be used for database files that + ** were specified with a relative pathname. Setting to a null string reverts + ** to the default database directory, which for database files specified with + ** a relative path will probably be based on the current directory for the + ** process. Database file specified with an absolute path are not impacted + ** by this setting, regardless of its value. + ** + */ + case PragTyp_DATA_STORE_DIRECTORY: { + if( !zRight ){ + returnSingleText(v, "data_store_directory", sqlite3_data_directory); + }else{ +#ifndef SQLITE_OMIT_WSD + if( zRight[0] ){ + int res; + rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); + if( rc!=SQLITE_OK || res==0 ){ + sqlite3ErrorMsg(pParse, "not a writable directory"); + goto pragma_out; + } + } + sqlite3_free(sqlite3_data_directory); + if( zRight[0] ){ + sqlite3_data_directory = sqlite3_mprintf("%s", zRight); + }else{ + sqlite3_data_directory = 0; + } +#endif /* SQLITE_OMIT_WSD */ + } + break; + } +#endif + +#if SQLITE_ENABLE_LOCKING_STYLE + /* + ** PRAGMA [schema.]lock_proxy_file + ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" + ** + ** Return or set the value of the lock_proxy_file flag. Changing + ** the value sets a specific file to be used for database access locks. + ** + */ + case PragTyp_LOCK_PROXY_FILE: { + if( !zRight ){ + Pager *pPager = sqlite3BtreePager(pDb->pBt); + char *proxy_file_path = NULL; + sqlite3_file *pFile = sqlite3PagerFile(pPager); + sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, + &proxy_file_path); + returnSingleText(v, "lock_proxy_file", proxy_file_path); + }else{ + Pager *pPager = sqlite3BtreePager(pDb->pBt); + sqlite3_file *pFile = sqlite3PagerFile(pPager); + int res; + if( zRight[0] ){ + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + zRight); + } else { + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + NULL); + } + if( res!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); + goto pragma_out; + } + } + break; + } +#endif /* SQLITE_ENABLE_LOCKING_STYLE */ + + /* + ** PRAGMA [schema.]synchronous + ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA + ** + ** Return or set the local value of the synchronous flag. Changing + ** the local value does not make changes to the disk file and the + ** default value will be restored the next time the database is + ** opened. + */ + case PragTyp_SYNCHRONOUS: { + if( !zRight ){ + returnSingleInt(v, "synchronous", pDb->safety_level-1); + }else{ + if( !db->autoCommit ){ + sqlite3ErrorMsg(pParse, + "Safety level may not be changed inside a transaction"); + }else{ + int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; + if( iLevel==0 ) iLevel = 1; + pDb->safety_level = iLevel; + pDb->bSyncSet = 1; + setAllPagerFlags(db); + } + } + break; + } +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_FLAG_PRAGMAS + case PragTyp_FLAG: { + if( zRight==0 ){ + returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 ); + }else{ + int mask = pPragma->iArg; /* Mask of bits to set or clear. */ + if( db->autoCommit==0 ){ + /* Foreign key support may not be enabled or disabled while not + ** in auto-commit mode. */ + mask &= ~(SQLITE_ForeignKeys); + } +#if SQLITE_USER_AUTHENTICATION + if( db->auth.authLevel==UAUTH_User ){ + /* Do not allow non-admin users to modify the schema arbitrarily */ + mask &= ~(SQLITE_WriteSchema); + } +#endif + + if( sqlite3GetBoolean(zRight, 0) ){ + db->flags |= mask; + }else{ + db->flags &= ~mask; + if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; + } + + /* Many of the flag-pragmas modify the code generated by the SQL + ** compiler (eg. count_changes). So add an opcode to expire all + ** compiled SQL statements after modifying a pragma value. + */ + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); + setAllPagerFlags(db); + } + break; + } +#endif /* SQLITE_OMIT_FLAG_PRAGMAS */ + +#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS + /* + ** PRAGMA table_info(
    ) + ** + ** Return a single row for each column of the named table. The columns of + ** the returned data set are: + ** + ** cid: Column id (numbered from left to right, starting at 0) + ** name: Column name + ** type: Column declaration type. + ** notnull: True if 'NOT NULL' is part of column declaration + ** dflt_value: The default value for the column, if any. + */ + case PragTyp_TABLE_INFO: if( zRight ){ + Table *pTab; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + static const char *azCol[] = { + "cid", "name", "type", "notnull", "dflt_value", "pk" + }; + int i, k; + int nHidden = 0; + Column *pCol; + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + pParse->nMem = 6; + sqlite3CodeVerifySchema(pParse, iDb); + setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) ); + sqlite3ViewGetColumnNames(pParse, pTab); + for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ + if( IsHiddenColumn(pCol) ){ + nHidden++; + continue; + } + if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ + k = 0; + }else if( pPk==0 ){ + k = 1; + }else{ + for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} + } + assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN ); + sqlite3VdbeMultiLoad(v, 1, "issisi", + i-nHidden, + pCol->zName, + sqlite3ColumnType(pCol,""), + pCol->notNull ? 1 : 0, + pCol->pDflt ? pCol->pDflt->u.zToken : 0, + k); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); + } + } + } + break; + + case PragTyp_STATS: { + static const char *azCol[] = { "table", "index", "width", "height" }; + Index *pIdx; + HashElem *i; + v = sqlite3GetVdbe(pParse); + pParse->nMem = 4; + sqlite3CodeVerifySchema(pParse, iDb); + setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); + for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ + Table *pTab = sqliteHashData(i); + sqlite3VdbeMultiLoad(v, 1, "ssii", + pTab->zName, + 0, + pTab->szTabRow, + pTab->nRowLogEst); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + sqlite3VdbeMultiLoad(v, 2, "sii", + pIdx->zName, + pIdx->szIdxRow, + pIdx->aiRowLogEst[0]); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); + } + } + } + break; + + case PragTyp_INDEX_INFO: if( zRight ){ + Index *pIdx; + Table *pTab; + pIdx = sqlite3FindIndex(db, zRight, zDb); + if( pIdx ){ + static const char *azCol[] = { + "seqno", "cid", "name", "desc", "coll", "key" + }; + int i; + int mx; + if( pPragma->iArg ){ + /* PRAGMA index_xinfo (newer version with more rows and columns) */ + mx = pIdx->nColumn; + pParse->nMem = 6; + }else{ + /* PRAGMA index_info (legacy version) */ + mx = pIdx->nKeyCol; + pParse->nMem = 3; + } + pTab = pIdx->pTable; + sqlite3CodeVerifySchema(pParse, iDb); + assert( pParse->nMem<=ArraySize(azCol) ); + setAllColumnNames(v, pParse->nMem, azCol); + for(i=0; iaiColumn[i]; + sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum, + cnum<0 ? 0 : pTab->aCol[cnum].zName); + if( pPragma->iArg ){ + sqlite3VdbeMultiLoad(v, 4, "isi", + pIdx->aSortOrder[i], + pIdx->azColl[i], + inKeyCol); + } + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); + } + } + } + break; + + case PragTyp_INDEX_LIST: if( zRight ){ + Index *pIdx; + Table *pTab; + int i; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + static const char *azCol[] = { + "seq", "name", "unique", "origin", "partial" + }; + v = sqlite3GetVdbe(pParse); + pParse->nMem = 5; + sqlite3CodeVerifySchema(pParse, iDb); + setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) ); + for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ + const char *azOrigin[] = { "c", "u", "pk" }; + sqlite3VdbeMultiLoad(v, 1, "isisi", + i, + pIdx->zName, + IsUniqueIndex(pIdx), + azOrigin[pIdx->idxType], + pIdx->pPartIdxWhere!=0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); + } + } + } + break; + + case PragTyp_DATABASE_LIST: { + static const char *azCol[] = { "seq", "name", "file" }; + int i; + pParse->nMem = 3; + setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); + for(i=0; inDb; i++){ + if( db->aDb[i].pBt==0 ) continue; + assert( db->aDb[i].zName!=0 ); + sqlite3VdbeMultiLoad(v, 1, "iss", + i, + db->aDb[i].zName, + sqlite3BtreeGetFilename(db->aDb[i].pBt)); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + } + } + break; + + case PragTyp_COLLATION_LIST: { + static const char *azCol[] = { "seq", "name" }; + int i = 0; + HashElem *p; + pParse->nMem = 2; + setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); + for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ + CollSeq *pColl = (CollSeq *)sqliteHashData(p); + sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); + } + } + break; +#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ + +#ifndef SQLITE_OMIT_FOREIGN_KEY + case PragTyp_FOREIGN_KEY_LIST: if( zRight ){ + FKey *pFK; + Table *pTab; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + v = sqlite3GetVdbe(pParse); + pFK = pTab->pFKey; + if( pFK ){ + static const char *azCol[] = { + "id", "seq", "table", "from", "to", "on_update", "on_delete", + "match" + }; + int i = 0; + pParse->nMem = 8; + sqlite3CodeVerifySchema(pParse, iDb); + setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) ); + while(pFK){ + int j; + for(j=0; jnCol; j++){ + sqlite3VdbeMultiLoad(v, 1, "iissssss", + i, + j, + pFK->zTo, + pTab->aCol[pFK->aCol[j].iFrom].zName, + pFK->aCol[j].zCol, + actionName(pFK->aAction[1]), /* ON UPDATE */ + actionName(pFK->aAction[0]), /* ON DELETE */ + "NONE"); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); + } + ++i; + pFK = pFK->pNextFrom; + } + } + } + } + break; +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ + +#ifndef SQLITE_OMIT_FOREIGN_KEY +#ifndef SQLITE_OMIT_TRIGGER + case PragTyp_FOREIGN_KEY_CHECK: { + FKey *pFK; /* A foreign key constraint */ + Table *pTab; /* Child table contain "REFERENCES" keyword */ + Table *pParent; /* Parent table that child points to */ + Index *pIdx; /* Index in the parent table */ + int i; /* Loop counter: Foreign key number for pTab */ + int j; /* Loop counter: Field of the foreign key */ + HashElem *k; /* Loop counter: Next table in schema */ + int x; /* result variable */ + int regResult; /* 3 registers to hold a result row */ + int regKey; /* Register to hold key for checking the FK */ + int regRow; /* Registers to hold a row from pTab */ + int addrTop; /* Top of a loop checking foreign keys */ + int addrOk; /* Jump here if the key is OK */ + int *aiCols; /* child to parent column mapping */ + static const char *azCol[] = { "table", "rowid", "parent", "fkid" }; + + regResult = pParse->nMem+1; + pParse->nMem += 4; + regKey = ++pParse->nMem; + regRow = ++pParse->nMem; + v = sqlite3GetVdbe(pParse); + setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); + sqlite3CodeVerifySchema(pParse, iDb); + k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); + while( k ){ + if( zRight ){ + pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); + k = 0; + }else{ + pTab = (Table*)sqliteHashData(k); + k = sqliteHashNext(k); + } + if( pTab==0 || pTab->pFKey==0 ) continue; + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; + sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); + sqlite3VdbeLoadString(v, regResult, pTab->zName); + for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ + pParent = sqlite3FindTable(db, pFK->zTo, zDb); + if( pParent==0 ) continue; + pIdx = 0; + sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); + x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); + if( x==0 ){ + if( pIdx==0 ){ + sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); + }else{ + sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + } + }else{ + k = 0; + break; + } + } + assert( pParse->nErr>0 || pFK==0 ); + if( pFK ) break; + if( pParse->nTabnTab = i; + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); + for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ + pParent = sqlite3FindTable(db, pFK->zTo, zDb); + pIdx = 0; + aiCols = 0; + if( pParent ){ + x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); + assert( x==0 ); + } + addrOk = sqlite3VdbeMakeLabel(v); + if( pParent && pIdx==0 ){ + int iKey = pFK->aCol[0].iFrom; + assert( iKey>=0 && iKeynCol ); + if( iKey!=pTab->iPKey ){ + sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow); + sqlite3ColumnDefault(v, pTab, iKey, regRow); + sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, + sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow); + } + sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v); + sqlite3VdbeGoto(v, addrOk); + sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + }else{ + for(j=0; jnCol; j++){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, + aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j); + sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); + } + if( pParent ){ + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey, + sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); + sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0); + VdbeCoverage(v); + } + } + sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); + sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1); + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); + sqlite3VdbeResolveLabel(v, addrOk); + sqlite3DbFree(db, aiCols); + } + sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrTop); + } + } + break; +#endif /* !defined(SQLITE_OMIT_TRIGGER) */ +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ + +#ifndef NDEBUG + case PragTyp_PARSER_TRACE: { + if( zRight ){ + if( sqlite3GetBoolean(zRight, 0) ){ + sqlite3ParserTrace(stdout, "parser: "); + }else{ + sqlite3ParserTrace(0, 0); + } + } + } + break; +#endif + + /* Reinstall the LIKE and GLOB functions. The variant of LIKE + ** used will be case sensitive or not depending on the RHS. + */ + case PragTyp_CASE_SENSITIVE_LIKE: { + if( zRight ){ + sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); + } + } + break; + +#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX +# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 +#endif + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK + /* Pragma "quick_check" is reduced version of + ** integrity_check designed to detect most database corruption + ** without most of the overhead of a full integrity-check. + */ + case PragTyp_INTEGRITY_CHECK: { + int i, j, addr, mxErr; + + int isQuick = (sqlite3Tolower(zLeft[0])=='q'); + + /* If the PRAGMA command was of the form "PRAGMA .integrity_check", + ** then iDb is set to the index of the database identified by . + ** In this case, the integrity of database iDb only is verified by + ** the VDBE created below. + ** + ** Otherwise, if the command was simply "PRAGMA integrity_check" (or + ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb + ** to -1 here, to indicate that the VDBE should verify the integrity + ** of all attached databases. */ + assert( iDb>=0 ); + assert( iDb==0 || pId2->z ); + if( pId2->z==0 ) iDb = -1; + + /* Initialize the VDBE program */ + pParse->nMem = 6; + setOneColumnName(v, "integrity_check"); + + /* Set the maximum error count */ + mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + if( zRight ){ + sqlite3GetInt32(zRight, &mxErr); + if( mxErr<=0 ){ + mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + } + } + sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ + + /* Do an integrity check on each database file */ + for(i=0; inDb; i++){ + HashElem *x; + Hash *pTbls; + int *aRoot; + int cnt = 0; + int mxIdx = 0; + int nIdx; + + if( OMIT_TEMPDB && i==1 ) continue; + if( iDb>=0 && i!=iDb ) continue; + + sqlite3CodeVerifySchema(pParse, i); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeJumpHere(v, addr); + + /* Do an integrity check of the B-Tree + ** + ** Begin by finding the root pages numbers + ** for all tables and indices in the database. + */ + assert( sqlite3SchemaMutexHeld(db, i, 0) ); + pTbls = &db->aDb[i].pSchema->tblHash; + for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + Index *pIdx; + if( HasRowid(pTab) ) cnt++; + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; } + if( nIdx>mxIdx ) mxIdx = nIdx; + } + aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1)); + if( aRoot==0 ) break; + for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + Index *pIdx; + if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + aRoot[cnt++] = pIdx->tnum; + } + } + aRoot[cnt] = 0; + + /* Make sure sufficient number of registers have been allocated */ + pParse->nMem = MAX( pParse->nMem, 8+mxIdx ); + + /* Do the b-tree integrity checks */ + sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY); + sqlite3VdbeChangeP5(v, (u8)i); + addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, + sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), + P4_DYNAMIC); + sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); + sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); + sqlite3VdbeJumpHere(v, addr); + + /* Make sure all the indices are constructed correctly. + */ + for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + Index *pIdx, *pPk; + Index *pPrior = 0; + int loopTop; + int iDataCur, iIdxCur; + int r1 = -1; + + if( pTab->pIndex==0 ) continue; + pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeJumpHere(v, addr); + sqlite3ExprCacheClear(pParse); + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, + 1, 0, &iDataCur, &iIdxCur); + sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ + } + assert( pParse->nMem>=8+j ); + assert( sqlite3NoTempsInRange(pParse,1,7+j) ); + sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); + loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); + /* Verify that all NOT NULL columns really are NOT NULL */ + for(j=0; jnCol; j++){ + char *zErr; + int jmp2, jmp3; + if( j==pTab->iPKey ) continue; + if( pTab->aCol[j].notNull==0 ) continue; + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); + sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); + jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ + zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, + pTab->aCol[j].zName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); + jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); + sqlite3VdbeAddOp0(v, OP_Halt); + sqlite3VdbeJumpHere(v, jmp2); + sqlite3VdbeJumpHere(v, jmp3); + } + /* Validate index entries for the current row */ + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int jmp2, jmp3, jmp4, jmp5; + int ckUniq = sqlite3VdbeMakeLabel(v); + if( pPk==pIdx ) continue; + r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, + pPrior, r1); + pPrior = pIdx; + sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */ + /* Verify that an index entry exists for the current table row */ + jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, + pIdx->nColumn); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ + sqlite3VdbeLoadString(v, 3, "row "); + sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); + sqlite3VdbeLoadString(v, 4, " missing from index "); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); + jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); + sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); + jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); + sqlite3VdbeAddOp0(v, OP_Halt); + sqlite3VdbeJumpHere(v, jmp2); + /* For UNIQUE indexes, verify that only one entry exists with the + ** current key. The entry is unique if (1) any column is NULL + ** or (2) the next entry has a different key */ + if( IsUniqueIndex(pIdx) ){ + int uniqOk = sqlite3VdbeMakeLabel(v); + int jmp6; + int kk; + for(kk=0; kknKeyCol; kk++){ + int iCol = pIdx->aiColumn[kk]; + assert( iCol!=XN_ROWID && iColnCol ); + if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; + sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); + VdbeCoverage(v); + } + jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); + sqlite3VdbeGoto(v, uniqOk); + sqlite3VdbeJumpHere(v, jmp6); + sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, + pIdx->nKeyCol); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ + sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); + sqlite3VdbeGoto(v, jmp5); + sqlite3VdbeResolveLabel(v, uniqOk); + } + sqlite3VdbeJumpHere(v, jmp4); + sqlite3ResolvePartIdxLabel(pParse, jmp3); + } + sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, loopTop-1); +#ifndef SQLITE_OMIT_BTREECOUNT + sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + if( pPk==pIdx ) continue; + addr = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); + sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); + sqlite3VdbeLoadString(v, 3, pIdx->zName); + sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7); + sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1); + } +#endif /* SQLITE_OMIT_BTREECOUNT */ + } + } + { + static const int iLn = VDBE_OFFSET_LINENO(2); + static const VdbeOpList endCode[] = { + { OP_AddImm, 1, 0, 0}, /* 0 */ + { OP_If, 1, 4, 0}, /* 1 */ + { OP_String8, 0, 3, 0}, /* 2 */ + { OP_ResultRow, 3, 1, 0}, /* 3 */ + }; + VdbeOp *aOp; + + aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); + if( aOp ){ + aOp[0].p2 = -mxErr; + aOp[2].p4type = P4_STATIC; + aOp[2].p4.z = "ok"; + } + } + } + break; +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_UTF16 + /* + ** PRAGMA encoding + ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" + ** + ** In its first form, this pragma returns the encoding of the main + ** database. If the database is not initialized, it is initialized now. + ** + ** The second form of this pragma is a no-op if the main database file + ** has not already been initialized. In this case it sets the default + ** encoding that will be used for the main database file if a new file + ** is created. If an existing main database file is opened, then the + ** default text encoding for the existing database is used. + ** + ** In all cases new databases created using the ATTACH command are + ** created to use the same default text encoding as the main database. If + ** the main database has not been initialized and/or created when ATTACH + ** is executed, this is done before the ATTACH operation. + ** + ** In the second form this pragma sets the text encoding to be used in + ** new database files created using this database handle. It is only + ** useful if invoked immediately after the main database i + */ + case PragTyp_ENCODING: { + static const struct EncName { + char *zName; + u8 enc; + } encnames[] = { + { "UTF8", SQLITE_UTF8 }, + { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ + { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ + { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ + { "UTF16le", SQLITE_UTF16LE }, + { "UTF16be", SQLITE_UTF16BE }, + { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ + { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ + { 0, 0 } + }; + const struct EncName *pEnc; + if( !zRight ){ /* "PRAGMA encoding" */ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); + assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); + assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); + returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName); + }else{ /* "PRAGMA encoding = XXX" */ + /* Only change the value of sqlite.enc if the database handle is not + ** initialized. If the main database exists, the new sqlite.enc value + ** will be overwritten when the schema is next loaded. If it does not + ** already exists, it will be created to use the new encoding value. + */ + if( + !(DbHasProperty(db, 0, DB_SchemaLoaded)) || + DbHasProperty(db, 0, DB_Empty) + ){ + for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ + if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ + SCHEMA_ENC(db) = ENC(db) = + pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; + break; + } + } + if( !pEnc->zName ){ + sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); + } + } + } + } + break; +#endif /* SQLITE_OMIT_UTF16 */ + +#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS + /* + ** PRAGMA [schema.]schema_version + ** PRAGMA [schema.]schema_version = + ** + ** PRAGMA [schema.]user_version + ** PRAGMA [schema.]user_version = + ** + ** PRAGMA [schema.]freelist_count + ** + ** PRAGMA [schema.]data_version + ** + ** PRAGMA [schema.]application_id + ** PRAGMA [schema.]application_id = + ** + ** The pragma's schema_version and user_version are used to set or get + ** the value of the schema-version and user-version, respectively. Both + ** the schema-version and the user-version are 32-bit signed integers + ** stored in the database header. + ** + ** The schema-cookie is usually only manipulated internally by SQLite. It + ** is incremented by SQLite whenever the database schema is modified (by + ** creating or dropping a table or index). The schema version is used by + ** SQLite each time a query is executed to ensure that the internal cache + ** of the schema used when compiling the SQL query matches the schema of + ** the database against which the compiled query is actually executed. + ** Subverting this mechanism by using "PRAGMA schema_version" to modify + ** the schema-version is potentially dangerous and may lead to program + ** crashes or database corruption. Use with caution! + ** + ** The user-version is not used internally by SQLite. It may be used by + ** applications for any purpose. + */ + case PragTyp_HEADER_VALUE: { + int iCookie = pPragma->iArg; /* Which cookie to read or write */ + sqlite3VdbeUsesBtree(v, iDb); + if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){ + /* Write the specified cookie value */ + static const VdbeOpList setCookie[] = { + { OP_Transaction, 0, 1, 0}, /* 0 */ + { OP_SetCookie, 0, 0, 0}, /* 1 */ + }; + VdbeOp *aOp; + sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie)); + aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0); + if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; + aOp[0].p1 = iDb; + aOp[1].p1 = iDb; + aOp[1].p2 = iCookie; + aOp[1].p3 = sqlite3Atoi(zRight); + }else{ + /* Read the specified cookie value */ + static const VdbeOpList readCookie[] = { + { OP_Transaction, 0, 0, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, 0}, /* 1 */ + { OP_ResultRow, 1, 1, 0} + }; + VdbeOp *aOp; + sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie)); + aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0); + if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; + aOp[0].p1 = iDb; + aOp[1].p1 = iDb; + aOp[1].p3 = iCookie; + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); + sqlite3VdbeReusable(v); + } + } + break; +#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ + +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + /* + ** PRAGMA compile_options + ** + ** Return the names of all compile-time options used in this build, + ** one option per row. + */ + case PragTyp_COMPILE_OPTIONS: { + int i = 0; + const char *zOpt; + pParse->nMem = 1; + setOneColumnName(v, "compile_option"); + while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ + sqlite3VdbeLoadString(v, 1, zOpt); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + sqlite3VdbeReusable(v); + } + break; +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +#ifndef SQLITE_OMIT_WAL + /* + ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate + ** + ** Checkpoint the database. + */ + case PragTyp_WAL_CHECKPOINT: { + static const char *azCol[] = { "busy", "log", "checkpointed" }; + int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); + int eMode = SQLITE_CHECKPOINT_PASSIVE; + if( zRight ){ + if( sqlite3StrICmp(zRight, "full")==0 ){ + eMode = SQLITE_CHECKPOINT_FULL; + }else if( sqlite3StrICmp(zRight, "restart")==0 ){ + eMode = SQLITE_CHECKPOINT_RESTART; + }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ + eMode = SQLITE_CHECKPOINT_TRUNCATE; + } + } + setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); + pParse->nMem = 3; + sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + } + break; + + /* + ** PRAGMA wal_autocheckpoint + ** PRAGMA wal_autocheckpoint = N + ** + ** Configure a database connection to automatically checkpoint a database + ** after accumulating N frames in the log. Or query for the current value + ** of N. + */ + case PragTyp_WAL_AUTOCHECKPOINT: { + if( zRight ){ + sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); + } + returnSingleInt(v, "wal_autocheckpoint", + db->xWalCallback==sqlite3WalDefaultHook ? + SQLITE_PTR_TO_INT(db->pWalArg) : 0); + } + break; +#endif + + /* + ** PRAGMA shrink_memory + ** + ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database + ** connection on which it is invoked to free up as much memory as it + ** can, by calling sqlite3_db_release_memory(). + */ + case PragTyp_SHRINK_MEMORY: { + sqlite3_db_release_memory(db); + break; + } + + /* + ** PRAGMA busy_timeout + ** PRAGMA busy_timeout = N + ** + ** Call sqlite3_busy_timeout(db, N). Return the current timeout value + ** if one is set. If no busy handler or a different busy handler is set + ** then 0 is returned. Setting the busy_timeout to 0 or negative + ** disables the timeout. + */ + /*case PragTyp_BUSY_TIMEOUT*/ default: { + assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT ); + if( zRight ){ + sqlite3_busy_timeout(db, sqlite3Atoi(zRight)); + } + returnSingleInt(v, "timeout", db->busyTimeout); + break; + } + + /* + ** PRAGMA soft_heap_limit + ** PRAGMA soft_heap_limit = N + ** + ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the + ** sqlite3_soft_heap_limit64() interface with the argument N, if N is + ** specified and is a non-negative integer. + ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always + ** returns the same integer that would be returned by the + ** sqlite3_soft_heap_limit64(-1) C-language function. + */ + case PragTyp_SOFT_HEAP_LIMIT: { + sqlite3_int64 N; + if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ + sqlite3_soft_heap_limit64(N); + } + returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1)); + break; + } + + /* + ** PRAGMA threads + ** PRAGMA threads = N + ** + ** Configure the maximum number of worker threads. Return the new + ** maximum, which might be less than requested. + */ + case PragTyp_THREADS: { + sqlite3_int64 N; + if( zRight + && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK + && N>=0 + ){ + sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); + } + returnSingleInt(v, "threads", + sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); + break; + } + +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* + ** Report the current state of file logs for all databases + */ + case PragTyp_LOCK_STATUS: { + static const char *const azLockName[] = { + "unlocked", "shared", "reserved", "pending", "exclusive" + }; + static const char *azCol[] = { "database", "status" }; + int i; + setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); + pParse->nMem = 2; + for(i=0; inDb; i++){ + Btree *pBt; + const char *zState = "unknown"; + int j; + if( db->aDb[i].zName==0 ) continue; + pBt = db->aDb[i].pBt; + if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ + zState = "closed"; + }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, + SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ + zState = azLockName[j]; + } + sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); + } + break; + } +#endif + +#ifdef SQLITE_HAS_CODEC + case PragTyp_KEY: { + if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); + break; + } + case PragTyp_REKEY: { + if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); + break; + } + case PragTyp_HEXKEY: { + if( zRight ){ + u8 iByte; + int i; + char zKey[40]; + for(i=0, iByte=0; idb; + if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ + char *z; + if( zObj==0 ) zObj = "?"; + z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj); + if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); + sqlite3DbFree(db, *pData->pzErrMsg); + *pData->pzErrMsg = z; + } + pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT; +} + +/* +** This is the callback routine for the code that initializes the +** database. See sqlite3Init() below for additional information. +** This routine is also called from the OP_ParseSchema opcode of the VDBE. +** +** Each callback contains the following information: +** +** argv[0] = name of thing being created +** argv[1] = root page number for table or index. 0 for trigger or view. +** argv[2] = SQL text for the CREATE statement. +** +*/ +SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ + InitData *pData = (InitData*)pInit; + sqlite3 *db = pData->db; + int iDb = pData->iDb; + + assert( argc==3 ); + UNUSED_PARAMETER2(NotUsed, argc); + assert( sqlite3_mutex_held(db->mutex) ); + DbClearProperty(db, iDb, DB_Empty); + if( db->mallocFailed ){ + corruptSchema(pData, argv[0], 0); + return 1; + } + + assert( iDb>=0 && iDbnDb ); + if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ + if( argv[1]==0 ){ + corruptSchema(pData, argv[0], 0); + }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){ + /* Call the parser to process a CREATE TABLE, INDEX or VIEW. + ** But because db->init.busy is set to 1, no VDBE code is generated + ** or executed. All the parser does is build the internal data + ** structures that describe the table, index, or view. + */ + int rc; + sqlite3_stmt *pStmt; + TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ + + assert( db->init.busy ); + db->init.iDb = iDb; + db->init.newTnum = sqlite3Atoi(argv[1]); + db->init.orphanTrigger = 0; + TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0); + rc = db->errCode; + assert( (rc&0xFF)==(rcp&0xFF) ); + db->init.iDb = 0; + if( SQLITE_OK!=rc ){ + if( db->init.orphanTrigger ){ + assert( iDb==1 ); + }else{ + pData->rc = rc; + if( rc==SQLITE_NOMEM ){ + sqlite3OomFault(db); + }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ + corruptSchema(pData, argv[0], sqlite3_errmsg(db)); + } + } + } + sqlite3_finalize(pStmt); + }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){ + corruptSchema(pData, argv[0], 0); + }else{ + /* If the SQL column is blank it means this is an index that + ** was created to be the PRIMARY KEY or to fulfill a UNIQUE + ** constraint for a CREATE TABLE. The index should have already + ** been created when we processed the CREATE TABLE. All we have + ** to do here is record the root page number for that index. + */ + Index *pIndex; + pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); + if( pIndex==0 ){ + /* This can occur if there exists an index on a TEMP table which + ** has the same name as another index on a permanent index. Since + ** the permanent table is hidden by the TEMP table, we can also + ** safely ignore the index on the permanent table. + */ + /* Do Nothing */; + }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){ + corruptSchema(pData, argv[0], "invalid rootpage"); + } + } + return 0; +} + +/* +** Attempt to read the database schema and initialize internal +** data structures for a single database file. The index of the +** database file is given by iDb. iDb==0 is used for the main +** database. iDb==1 should never be used. iDb>=2 is used for +** auxiliary databases. Return one of the SQLITE_ error codes to +** indicate success or failure. +*/ +static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ + int rc; + int i; +#ifndef SQLITE_OMIT_DEPRECATED + int size; +#endif + Db *pDb; + char const *azArg[4]; + int meta[5]; + InitData initData; + const char *zMasterName; + int openedTransaction = 0; + + assert( iDb>=0 && iDbnDb ); + assert( db->aDb[iDb].pSchema ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); + + /* Construct the in-memory representation schema tables (sqlite_master or + ** sqlite_temp_master) by invoking the parser directly. The appropriate + ** table name will be inserted automatically by the parser so we can just + ** use the abbreviation "x" here. The parser will also automatically tag + ** the schema table as read-only. */ + azArg[0] = zMasterName = SCHEMA_TABLE(iDb); + azArg[1] = "1"; + azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text," + "rootpage integer,sql text)"; + azArg[3] = 0; + initData.db = db; + initData.iDb = iDb; + initData.rc = SQLITE_OK; + initData.pzErrMsg = pzErrMsg; + sqlite3InitCallback(&initData, 3, (char **)azArg, 0); + if( initData.rc ){ + rc = initData.rc; + goto error_out; + } + + /* Create a cursor to hold the database open + */ + pDb = &db->aDb[iDb]; + if( pDb->pBt==0 ){ + if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){ + DbSetProperty(db, 1, DB_SchemaLoaded); + } + return SQLITE_OK; + } + + /* If there is not already a read-only (or read-write) transaction opened + ** on the b-tree database, open one now. If a transaction is opened, it + ** will be closed before this function returns. */ + sqlite3BtreeEnter(pDb->pBt); + if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ + rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); + if( rc!=SQLITE_OK ){ + sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); + goto initone_error_out; + } + openedTransaction = 1; + } + + /* Get the database meta information. + ** + ** Meta values are as follows: + ** meta[0] Schema cookie. Changes with each schema change. + ** meta[1] File format of schema layer. + ** meta[2] Size of the page cache. + ** meta[3] Largest rootpage (auto/incr_vacuum mode) + ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE + ** meta[5] User version + ** meta[6] Incremental vacuum mode + ** meta[7] unused + ** meta[8] unused + ** meta[9] unused + ** + ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to + ** the possible values of meta[4]. + */ + for(i=0; ipBt, i+1, (u32 *)&meta[i]); + } + pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; + + /* If opening a non-empty database, check the text encoding. For the + ** main database, set sqlite3.enc to the encoding of the main database. + ** For an attached db, it is an error if the encoding is not the same + ** as sqlite3.enc. + */ + if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ + if( iDb==0 ){ +#ifndef SQLITE_OMIT_UTF16 + u8 encoding; + /* If opening the main database, set ENC(db). */ + encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; + if( encoding==0 ) encoding = SQLITE_UTF8; + ENC(db) = encoding; +#else + ENC(db) = SQLITE_UTF8; +#endif + }else{ + /* If opening an attached database, the encoding much match ENC(db) */ + if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ + sqlite3SetString(pzErrMsg, db, "attached databases must use the same" + " text encoding as main database"); + rc = SQLITE_ERROR; + goto initone_error_out; + } + } + }else{ + DbSetProperty(db, iDb, DB_Empty); + } + pDb->pSchema->enc = ENC(db); + + if( pDb->pSchema->cache_size==0 ){ +#ifndef SQLITE_OMIT_DEPRECATED + size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); + if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } + pDb->pSchema->cache_size = size; +#else + pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE; +#endif + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + + /* + ** file_format==1 Version 3.0.0. + ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN + ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults + ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants + */ + pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; + if( pDb->pSchema->file_format==0 ){ + pDb->pSchema->file_format = 1; + } + if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ + sqlite3SetString(pzErrMsg, db, "unsupported file format"); + rc = SQLITE_ERROR; + goto initone_error_out; + } + + /* Ticket #2804: When we open a database in the newer file format, + ** clear the legacy_file_format pragma flag so that a VACUUM will + ** not downgrade the database and thus invalidate any descending + ** indices that the user might have created. + */ + if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ + db->flags &= ~SQLITE_LegacyFileFmt; + } + + /* Read the schema information out of the schema tables + */ + assert( db->init.busy ); + { + char *zSql; + zSql = sqlite3MPrintf(db, + "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid", + db->aDb[iDb].zName, zMasterName); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + sqlite3_xauth xAuth; + xAuth = db->xAuth; + db->xAuth = 0; +#endif + rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; + } +#endif + if( rc==SQLITE_OK ) rc = initData.rc; + sqlite3DbFree(db, zSql); +#ifndef SQLITE_OMIT_ANALYZE + if( rc==SQLITE_OK ){ + sqlite3AnalysisLoad(db, iDb); + } +#endif + } + if( db->mallocFailed ){ + rc = SQLITE_NOMEM_BKPT; + sqlite3ResetAllSchemasOfConnection(db); + } + if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ + /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider + ** the schema loaded, even if errors occurred. In this situation the + ** current sqlite3_prepare() operation will fail, but the following one + ** will attempt to compile the supplied statement against whatever subset + ** of the schema was loaded before the error occurred. The primary + ** purpose of this is to allow access to the sqlite_master table + ** even when its contents have been corrupted. + */ + DbSetProperty(db, iDb, DB_SchemaLoaded); + rc = SQLITE_OK; + } + + /* Jump here for an error that occurs after successfully allocating + ** curMain and calling sqlite3BtreeEnter(). For an error that occurs + ** before that point, jump to error_out. + */ +initone_error_out: + if( openedTransaction ){ + sqlite3BtreeCommit(pDb->pBt); + } + sqlite3BtreeLeave(pDb->pBt); + +error_out: + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomFault(db); + } + return rc; +} + +/* +** Initialize all database files - the main database file, the file +** used to store temporary tables, and any additional database files +** created using ATTACH statements. Return a success code. If an +** error occurs, write an error message into *pzErrMsg. +** +** After a database is initialized, the DB_SchemaLoaded bit is set +** bit is set in the flags field of the Db structure. If the database +** file was of zero-length, then the DB_Empty flag is also set. +*/ +SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){ + int i, rc; + int commit_internal = !(db->flags&SQLITE_InternChanges); + + assert( sqlite3_mutex_held(db->mutex) ); + assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) ); + assert( db->init.busy==0 ); + rc = SQLITE_OK; + db->init.busy = 1; + ENC(db) = SCHEMA_ENC(db); + for(i=0; rc==SQLITE_OK && inDb; i++){ + if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; + rc = sqlite3InitOne(db, i, pzErrMsg); + if( rc ){ + sqlite3ResetOneSchema(db, i); + } + } + + /* Once all the other databases have been initialized, load the schema + ** for the TEMP database. This is loaded last, as the TEMP database + ** schema may contain references to objects in other databases. + */ +#ifndef SQLITE_OMIT_TEMPDB + assert( db->nDb>1 ); + if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ + rc = sqlite3InitOne(db, 1, pzErrMsg); + if( rc ){ + sqlite3ResetOneSchema(db, 1); + } + } +#endif + + db->init.busy = 0; + if( rc==SQLITE_OK && commit_internal ){ + sqlite3CommitInternalChanges(db); + } + + return rc; +} + +/* +** This routine is a no-op if the database schema is already initialized. +** Otherwise, the schema is loaded. An error code is returned. +*/ +SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){ + int rc = SQLITE_OK; + sqlite3 *db = pParse->db; + assert( sqlite3_mutex_held(db->mutex) ); + if( !db->init.busy ){ + rc = sqlite3Init(db, &pParse->zErrMsg); + } + if( rc!=SQLITE_OK ){ + pParse->rc = rc; + pParse->nErr++; + } + return rc; +} + + +/* +** Check schema cookies in all databases. If any cookie is out +** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies +** make no changes to pParse->rc. +*/ +static void schemaIsValid(Parse *pParse){ + sqlite3 *db = pParse->db; + int iDb; + int rc; + int cookie; + + assert( pParse->checkSchema ); + assert( sqlite3_mutex_held(db->mutex) ); + for(iDb=0; iDbnDb; iDb++){ + int openedTransaction = 0; /* True if a transaction is opened */ + Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ + if( pBt==0 ) continue; + + /* If there is not already a read-only (or read-write) transaction opened + ** on the b-tree database, open one now. If a transaction is opened, it + ** will be closed immediately after reading the meta-value. */ + if( !sqlite3BtreeIsInReadTrans(pBt) ){ + rc = sqlite3BtreeBeginTrans(pBt, 0); + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomFault(db); + } + if( rc!=SQLITE_OK ) return; + openedTransaction = 1; + } + + /* Read the schema cookie from the database. If it does not match the + ** value stored as part of the in-memory schema representation, + ** set Parse.rc to SQLITE_SCHEMA. */ + sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ + sqlite3ResetOneSchema(db, iDb); + pParse->rc = SQLITE_SCHEMA; + } + + /* Close the transaction, if one was opened. */ + if( openedTransaction ){ + sqlite3BtreeCommit(pBt); + } + } +} + +/* +** Convert a schema pointer into the iDb index that indicates +** which database file in db->aDb[] the schema refers to. +** +** If the same database is attached more than once, the first +** attached database is returned. +*/ +SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ + int i = -1000000; + + /* If pSchema is NULL, then return -1000000. This happens when code in + ** expr.c is trying to resolve a reference to a transient table (i.e. one + ** created by a sub-select). In this case the return value of this + ** function should never be used. + ** + ** We return -1000000 instead of the more usual -1 simply because using + ** -1000000 as the incorrect index into db->aDb[] is much + ** more likely to cause a segfault than -1 (of course there are assert() + ** statements too, but it never hurts to play the odds). + */ + assert( sqlite3_mutex_held(db->mutex) ); + if( pSchema ){ + for(i=0; ALWAYS(inDb); i++){ + if( db->aDb[i].pSchema==pSchema ){ + break; + } + } + assert( i>=0 && inDb ); + } + return i; +} + +/* +** Free all memory allocations in the pParse object +*/ +SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){ + if( pParse ){ + sqlite3 *db = pParse->db; + sqlite3DbFree(db, pParse->aLabel); + sqlite3ExprListDelete(db, pParse->pConstExpr); + if( db ){ + assert( db->lookaside.bDisable >= pParse->disableLookaside ); + db->lookaside.bDisable -= pParse->disableLookaside; + } + pParse->disableLookaside = 0; + } +} + +/* +** Compile the UTF-8 encoded SQL statement zSql into a statement handle. +*/ +static int sqlite3Prepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + Vdbe *pReprepare, /* VM being reprepared */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + Parse *pParse; /* Parsing context */ + char *zErrMsg = 0; /* Error message */ + int rc = SQLITE_OK; /* Result code */ + int i; /* Loop counter */ + + /* Allocate the parsing context */ + pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); + if( pParse==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto end_prepare; + } + pParse->pReprepare = pReprepare; + assert( ppStmt && *ppStmt==0 ); + /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */ + assert( sqlite3_mutex_held(db->mutex) ); + + /* Check to verify that it is possible to get a read lock on all + ** database schemas. The inability to get a read lock indicates that + ** some other database connection is holding a write-lock, which in + ** turn means that the other connection has made uncommitted changes + ** to the schema. + ** + ** Were we to proceed and prepare the statement against the uncommitted + ** schema changes and if those schema changes are subsequently rolled + ** back and different changes are made in their place, then when this + ** prepared statement goes to run the schema cookie would fail to detect + ** the schema change. Disaster would follow. + ** + ** This thread is currently holding mutexes on all Btrees (because + ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it + ** is not possible for another thread to start a new schema change + ** while this routine is running. Hence, we do not need to hold + ** locks on the schema, we just need to make sure nobody else is + ** holding them. + ** + ** Note that setting READ_UNCOMMITTED overrides most lock detection, + ** but it does *not* override schema lock detection, so this all still + ** works even if READ_UNCOMMITTED is set. + */ + for(i=0; inDb; i++) { + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + assert( sqlite3BtreeHoldsMutex(pBt) ); + rc = sqlite3BtreeSchemaLocked(pBt); + if( rc ){ + const char *zDb = db->aDb[i].zName; + sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); + testcase( db->flags & SQLITE_ReadUncommitted ); + goto end_prepare; + } + } + } + + sqlite3VtabUnlockList(db); + + pParse->db = db; + pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */ + if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ + char *zSqlCopy; + int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; + testcase( nBytes==mxLen ); + testcase( nBytes==mxLen+1 ); + if( nBytes>mxLen ){ + sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long"); + rc = sqlite3ApiExit(db, SQLITE_TOOBIG); + goto end_prepare; + } + zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); + if( zSqlCopy ){ + sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); + pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; + sqlite3DbFree(db, zSqlCopy); + }else{ + pParse->zTail = &zSql[nBytes]; + } + }else{ + sqlite3RunParser(pParse, zSql, &zErrMsg); + } + assert( 0==pParse->nQueryLoop ); + + if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; + if( pParse->checkSchema ){ + schemaIsValid(pParse); + } + if( db->mallocFailed ){ + pParse->rc = SQLITE_NOMEM_BKPT; + } + if( pzTail ){ + *pzTail = pParse->zTail; + } + rc = pParse->rc; + +#ifndef SQLITE_OMIT_EXPLAIN + if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){ + static const char * const azColName[] = { + "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", + "selectid", "order", "from", "detail" + }; + int iFirst, mx; + if( pParse->explain==2 ){ + sqlite3VdbeSetNumCols(pParse->pVdbe, 4); + iFirst = 8; + mx = 12; + }else{ + sqlite3VdbeSetNumCols(pParse->pVdbe, 8); + iFirst = 0; + mx = 8; + } + for(i=iFirst; ipVdbe, i-iFirst, COLNAME_NAME, + azColName[i], SQLITE_STATIC); + } + } +#endif + + if( db->init.busy==0 ){ + Vdbe *pVdbe = pParse->pVdbe; + sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); + } + if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ + sqlite3VdbeFinalize(pParse->pVdbe); + assert(!(*ppStmt)); + }else{ + *ppStmt = (sqlite3_stmt*)pParse->pVdbe; + } + + if( zErrMsg ){ + sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg); + sqlite3DbFree(db, zErrMsg); + }else{ + sqlite3Error(db, rc); + } + + /* Delete any TriggerPrg structures allocated while parsing this statement. */ + while( pParse->pTriggerPrg ){ + TriggerPrg *pT = pParse->pTriggerPrg; + pParse->pTriggerPrg = pT->pNext; + sqlite3DbFree(db, pT); + } + +end_prepare: + + sqlite3ParserReset(pParse); + sqlite3StackFree(db, pParse); + rc = sqlite3ApiExit(db, rc); + assert( (rc&db->errMask)==rc ); + return rc; +} +static int sqlite3LockAndPrepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + Vdbe *pOld, /* VM being reprepared */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; +#endif + *ppStmt = 0; + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); + if( rc==SQLITE_SCHEMA ){ + sqlite3_finalize(*ppStmt); + rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); + } + sqlite3BtreeLeaveAll(db); + sqlite3_mutex_leave(db->mutex); + assert( rc==SQLITE_OK || *ppStmt==0 ); + return rc; +} + +/* +** Rerun the compilation of a statement after a schema change. +** +** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, +** if the statement cannot be recompiled because another connection has +** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error +** occurs, return SQLITE_SCHEMA. +*/ +SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){ + int rc; + sqlite3_stmt *pNew; + const char *zSql; + sqlite3 *db; + + assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); + zSql = sqlite3_sql((sqlite3_stmt *)p); + assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ + db = sqlite3VdbeDb(p); + assert( sqlite3_mutex_held(db->mutex) ); + rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0); + if( rc ){ + if( rc==SQLITE_NOMEM ){ + sqlite3OomFault(db); + } + assert( pNew==0 ); + return rc; + }else{ + assert( pNew!=0 ); + } + sqlite3VdbeSwap((Vdbe*)pNew, p); + sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); + sqlite3VdbeResetStepResult((Vdbe*)pNew); + sqlite3VdbeFinalize((Vdbe*)pNew); + return SQLITE_OK; +} + + +/* +** Two versions of the official API. Legacy and new use. In the legacy +** version, the original SQL text is not saved in the prepared statement +** and so if a schema change occurs, SQLITE_SCHEMA is returned by +** sqlite3_step(). In the new version, the original SQL text is retained +** and the statement is automatically recompiled if an schema change +** occurs. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_prepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} + + +#ifndef SQLITE_OMIT_UTF16 +/* +** Compile the UTF-16 encoded SQL statement zSql into a statement handle. +*/ +static int sqlite3Prepare16( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + /* This function currently works by first transforming the UTF-16 + ** encoded string to UTF-8, then invoking sqlite3_prepare(). The + ** tricky bit is figuring out the pointer to return in *pzTail. + */ + char *zSql8; + const char *zTail8 = 0; + int rc = SQLITE_OK; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; +#endif + *ppStmt = 0; + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ + return SQLITE_MISUSE_BKPT; + } + if( nBytes>=0 ){ + int sz; + const char *z = (const char*)zSql; + for(sz=0; szmutex); + zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); + if( zSql8 ){ + rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); + } + + if( zTail8 && pzTail ){ + /* If sqlite3_prepare returns a tail pointer, we calculate the + ** equivalent pointer into the UTF-16 string by counting the unicode + ** characters between zSql8 and zTail8, and then returning a pointer + ** the same number of characters into the UTF-16 string. + */ + int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); + *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); + } + sqlite3DbFree(db, zSql8); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Two versions of the official API. Legacy and new use. In the legacy +** version, the original SQL text is not saved in the prepared statement +** and so if a schema change occurs, SQLITE_SCHEMA is returned by +** sqlite3_step(). In the new version, the original SQL text is retained +** and the statement is automatically recompiled if an schema change +** occurs. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_prepare16( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} +SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} + +#endif /* SQLITE_OMIT_UTF16 */ + +/************** End of prepare.c *********************************************/ +/************** Begin file select.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle SELECT statements in SQLite. +*/ +/* #include "sqliteInt.h" */ + +/* +** Trace output macros +*/ +#if SELECTTRACE_ENABLED +/***/ int sqlite3SelectTrace = 0; +# define SELECTTRACE(K,P,S,X) \ + if(sqlite3SelectTrace&(K)) \ + sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\ + (S)->zSelName,(S)),\ + sqlite3DebugPrintf X +#else +# define SELECTTRACE(K,P,S,X) +#endif + + +/* +** An instance of the following object is used to record information about +** how to process the DISTINCT keyword, to simplify passing that information +** into the selectInnerLoop() routine. +*/ +typedef struct DistinctCtx DistinctCtx; +struct DistinctCtx { + u8 isTnct; /* True if the DISTINCT keyword is present */ + u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */ + int tabTnct; /* Ephemeral table used for DISTINCT processing */ + int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */ +}; + +/* +** An instance of the following object is used to record information about +** the ORDER BY (or GROUP BY) clause of query is being coded. +*/ +typedef struct SortCtx SortCtx; +struct SortCtx { + ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */ + int nOBSat; /* Number of ORDER BY terms satisfied by indices */ + int iECursor; /* Cursor number for the sorter */ + int regReturn; /* Register holding block-output return address */ + int labelBkOut; /* Start label for the block-output subroutine */ + int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */ + int labelDone; /* Jump here when done, ex: LIMIT reached */ + u8 sortFlags; /* Zero or more SORTFLAG_* bits */ +}; +#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ + +/* +** Delete all the content of a Select structure. Deallocate the structure +** itself only if bFree is true. +*/ +static void clearSelect(sqlite3 *db, Select *p, int bFree){ + while( p ){ + Select *pPrior = p->pPrior; + sqlite3ExprListDelete(db, p->pEList); + sqlite3SrcListDelete(db, p->pSrc); + sqlite3ExprDelete(db, p->pWhere); + sqlite3ExprListDelete(db, p->pGroupBy); + sqlite3ExprDelete(db, p->pHaving); + sqlite3ExprListDelete(db, p->pOrderBy); + sqlite3ExprDelete(db, p->pLimit); + sqlite3ExprDelete(db, p->pOffset); + if( p->pWith ) sqlite3WithDelete(db, p->pWith); + if( bFree ) sqlite3DbFree(db, p); + p = pPrior; + bFree = 1; + } +} + +/* +** Initialize a SelectDest structure. +*/ +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ + pDest->eDest = (u8)eDest; + pDest->iSDParm = iParm; + pDest->affSdst = 0; + pDest->iSdst = 0; + pDest->nSdst = 0; +} + + +/* +** Allocate a new Select structure and return a pointer to that +** structure. +*/ +SQLITE_PRIVATE Select *sqlite3SelectNew( + Parse *pParse, /* Parsing context */ + ExprList *pEList, /* which columns to include in the result */ + SrcList *pSrc, /* the FROM clause -- which tables to scan */ + Expr *pWhere, /* the WHERE clause */ + ExprList *pGroupBy, /* the GROUP BY clause */ + Expr *pHaving, /* the HAVING clause */ + ExprList *pOrderBy, /* the ORDER BY clause */ + u32 selFlags, /* Flag parameters, such as SF_Distinct */ + Expr *pLimit, /* LIMIT value. NULL means not used */ + Expr *pOffset /* OFFSET value. NULL means no offset */ +){ + Select *pNew; + Select standin; + sqlite3 *db = pParse->db; + pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + if( pNew==0 ){ + assert( db->mallocFailed ); + pNew = &standin; + } + if( pEList==0 ){ + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0)); + } + pNew->pEList = pEList; + pNew->op = TK_SELECT; + pNew->selFlags = selFlags; + pNew->iLimit = 0; + pNew->iOffset = 0; +#if SELECTTRACE_ENABLED + pNew->zSelName[0] = 0; +#endif + pNew->addrOpenEphm[0] = -1; + pNew->addrOpenEphm[1] = -1; + pNew->nSelectRow = 0; + if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc)); + pNew->pSrc = pSrc; + pNew->pWhere = pWhere; + pNew->pGroupBy = pGroupBy; + pNew->pHaving = pHaving; + pNew->pOrderBy = pOrderBy; + pNew->pPrior = 0; + pNew->pNext = 0; + pNew->pLimit = pLimit; + pNew->pOffset = pOffset; + pNew->pWith = 0; + assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 ); + if( db->mallocFailed ) { + clearSelect(db, pNew, pNew!=&standin); + pNew = 0; + }else{ + assert( pNew->pSrc!=0 || pParse->nErr>0 ); + } + assert( pNew!=&standin ); + return pNew; +} + +#if SELECTTRACE_ENABLED +/* +** Set the name of a Select object +*/ +SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){ + if( p && zName ){ + sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); + } +} +#endif + + +/* +** Delete the given Select structure and all of its substructures. +*/ +SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ + if( p ) clearSelect(db, p, 1); +} + +/* +** Return a pointer to the right-most SELECT statement in a compound. +*/ +static Select *findRightmost(Select *p){ + while( p->pNext ) p = p->pNext; + return p; +} + +/* +** Given 1 to 3 identifiers preceding the JOIN keyword, determine the +** type of join. Return an integer constant that expresses that type +** in terms of the following bit values: +** +** JT_INNER +** JT_CROSS +** JT_OUTER +** JT_NATURAL +** JT_LEFT +** JT_RIGHT +** +** A full outer join is the combination of JT_LEFT and JT_RIGHT. +** +** If an illegal or unsupported join type is seen, then still return +** a join type, but put an error in the pParse structure. +*/ +SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ + int jointype = 0; + Token *apAll[3]; + Token *p; + /* 0123456789 123456789 123456789 123 */ + static const char zKeyText[] = "naturaleftouterightfullinnercross"; + static const struct { + u8 i; /* Beginning of keyword text in zKeyText[] */ + u8 nChar; /* Length of the keyword in characters */ + u8 code; /* Join type mask */ + } aKeyword[] = { + /* natural */ { 0, 7, JT_NATURAL }, + /* left */ { 6, 4, JT_LEFT|JT_OUTER }, + /* outer */ { 10, 5, JT_OUTER }, + /* right */ { 14, 5, JT_RIGHT|JT_OUTER }, + /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER }, + /* inner */ { 23, 5, JT_INNER }, + /* cross */ { 28, 5, JT_INNER|JT_CROSS }, + }; + int i, j; + apAll[0] = pA; + apAll[1] = pB; + apAll[2] = pC; + for(i=0; i<3 && apAll[i]; i++){ + p = apAll[i]; + for(j=0; jn==aKeyword[j].nChar + && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){ + jointype |= aKeyword[j].code; + break; + } + } + testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 ); + if( j>=ArraySize(aKeyword) ){ + jointype |= JT_ERROR; + break; + } + } + if( + (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || + (jointype & JT_ERROR)!=0 + ){ + const char *zSp = " "; + assert( pB!=0 ); + if( pC==0 ){ zSp++; } + sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " + "%T %T%s%T", pA, pB, zSp, pC); + jointype = JT_INNER; + }else if( (jointype & JT_OUTER)!=0 + && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){ + sqlite3ErrorMsg(pParse, + "RIGHT and FULL OUTER JOINs are not currently supported"); + jointype = JT_INNER; + } + return jointype; +} + +/* +** Return the index of a column in a table. Return -1 if the column +** is not contained in the table. +*/ +static int columnIndex(Table *pTab, const char *zCol){ + int i; + for(i=0; inCol; i++){ + if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; + } + return -1; +} + +/* +** Search the first N tables in pSrc, from left to right, looking for a +** table that has a column named zCol. +** +** When found, set *piTab and *piCol to the table index and column index +** of the matching column and return TRUE. +** +** If not found, return FALSE. +*/ +static int tableAndColumnIndex( + SrcList *pSrc, /* Array of tables to search */ + int N, /* Number of tables in pSrc->a[] to search */ + const char *zCol, /* Name of the column we are looking for */ + int *piTab, /* Write index of pSrc->a[] here */ + int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */ +){ + int i; /* For looping over tables in pSrc */ + int iCol; /* Index of column matching zCol */ + + assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */ + for(i=0; ia[i].pTab, zCol); + if( iCol>=0 ){ + if( piTab ){ + *piTab = i; + *piCol = iCol; + } + return 1; + } + } + return 0; +} + +/* +** This function is used to add terms implied by JOIN syntax to the +** WHERE clause expression of a SELECT statement. The new term, which +** is ANDed with the existing WHERE clause, is of the form: +** +** (tab1.col1 = tab2.col2) +** +** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the +** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is +** column iColRight of tab2. +*/ +static void addWhereTerm( + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* List of tables in FROM clause */ + int iLeft, /* Index of first table to join in pSrc */ + int iColLeft, /* Index of column in first table */ + int iRight, /* Index of second table in pSrc */ + int iColRight, /* Index of column in second table */ + int isOuterJoin, /* True if this is an OUTER join */ + Expr **ppWhere /* IN/OUT: The WHERE clause to add to */ +){ + sqlite3 *db = pParse->db; + Expr *pE1; + Expr *pE2; + Expr *pEq; + + assert( iLeftnSrc>iRight ); + assert( pSrc->a[iLeft].pTab ); + assert( pSrc->a[iRight].pTab ); + + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft); + pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight); + + pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0); + if( pEq && isOuterJoin ){ + ExprSetProperty(pEq, EP_FromJoin); + assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); + ExprSetVVAProperty(pEq, EP_NoReduce); + pEq->iRightJoinTable = (i16)pE2->iTable; + } + *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); +} + +/* +** Set the EP_FromJoin property on all terms of the given expression. +** And set the Expr.iRightJoinTable to iTable for every term in the +** expression. +** +** The EP_FromJoin property is used on terms of an expression to tell +** the LEFT OUTER JOIN processing logic that this term is part of the +** join restriction specified in the ON or USING clause and not a part +** of the more general WHERE clause. These terms are moved over to the +** WHERE clause during join processing but we need to remember that they +** originated in the ON or USING clause. +** +** The Expr.iRightJoinTable tells the WHERE clause processing that the +** expression depends on table iRightJoinTable even if that table is not +** explicitly mentioned in the expression. That information is needed +** for cases like this: +** +** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 +** +** The where clause needs to defer the handling of the t1.x=5 +** term until after the t2 loop of the join. In that way, a +** NULL t2 row will be inserted whenever t1.x!=5. If we do not +** defer the handling of t1.x=5, it will be processed immediately +** after the t1 loop and rows with t1.x!=5 will never appear in +** the output, which is incorrect. +*/ +static void setJoinExpr(Expr *p, int iTable){ + while( p ){ + ExprSetProperty(p, EP_FromJoin); + assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); + ExprSetVVAProperty(p, EP_NoReduce); + p->iRightJoinTable = (i16)iTable; + if( p->op==TK_FUNCTION && p->x.pList ){ + int i; + for(i=0; ix.pList->nExpr; i++){ + setJoinExpr(p->x.pList->a[i].pExpr, iTable); + } + } + setJoinExpr(p->pLeft, iTable); + p = p->pRight; + } +} + +/* +** This routine processes the join information for a SELECT statement. +** ON and USING clauses are converted into extra terms of the WHERE clause. +** NATURAL joins also create extra WHERE clause terms. +** +** The terms of a FROM clause are contained in the Select.pSrc structure. +** The left most table is the first entry in Select.pSrc. The right-most +** table is the last entry. The join operator is held in the entry to +** the left. Thus entry 0 contains the join operator for the join between +** entries 0 and 1. Any ON or USING clauses associated with the join are +** also attached to the left entry. +** +** This routine returns the number of errors encountered. +*/ +static int sqliteProcessJoin(Parse *pParse, Select *p){ + SrcList *pSrc; /* All tables in the FROM clause */ + int i, j; /* Loop counters */ + struct SrcList_item *pLeft; /* Left table being joined */ + struct SrcList_item *pRight; /* Right table being joined */ + + pSrc = p->pSrc; + pLeft = &pSrc->a[0]; + pRight = &pLeft[1]; + for(i=0; inSrc-1; i++, pRight++, pLeft++){ + Table *pLeftTab = pLeft->pTab; + Table *pRightTab = pRight->pTab; + int isOuter; + + if( NEVER(pLeftTab==0 || pRightTab==0) ) continue; + isOuter = (pRight->fg.jointype & JT_OUTER)!=0; + + /* When the NATURAL keyword is present, add WHERE clause terms for + ** every column that the two tables have in common. + */ + if( pRight->fg.jointype & JT_NATURAL ){ + if( pRight->pOn || pRight->pUsing ){ + sqlite3ErrorMsg(pParse, "a NATURAL join may not have " + "an ON or USING clause", 0); + return 1; + } + for(j=0; jnCol; j++){ + char *zName; /* Name of column in the right table */ + int iLeft; /* Matching left table */ + int iLeftCol; /* Matching column in the left table */ + + zName = pRightTab->aCol[j].zName; + if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){ + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j, + isOuter, &p->pWhere); + } + } + } + + /* Disallow both ON and USING clauses in the same join + */ + if( pRight->pOn && pRight->pUsing ){ + sqlite3ErrorMsg(pParse, "cannot have both ON and USING " + "clauses in the same join"); + return 1; + } + + /* Add the ON clause to the end of the WHERE clause, connected by + ** an AND operator. + */ + if( pRight->pOn ){ + if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor); + p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn); + pRight->pOn = 0; + } + + /* Create extra terms on the WHERE clause for each column named + ** in the USING clause. Example: If the two tables to be joined are + ** A and B and the USING clause names X, Y, and Z, then add this + ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z + ** Report an error if any column mentioned in the USING clause is + ** not contained in both tables to be joined. + */ + if( pRight->pUsing ){ + IdList *pList = pRight->pUsing; + for(j=0; jnId; j++){ + char *zName; /* Name of the term in the USING clause */ + int iLeft; /* Table on the left with matching column name */ + int iLeftCol; /* Column number of matching column on the left */ + int iRightCol; /* Column number of matching column on the right */ + + zName = pList->a[j].zName; + iRightCol = columnIndex(pRightTab, zName); + if( iRightCol<0 + || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) + ){ + sqlite3ErrorMsg(pParse, "cannot join using column %s - column " + "not present in both tables", zName); + return 1; + } + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol, + isOuter, &p->pWhere); + } + } + } + return 0; +} + +/* Forward reference */ +static KeyInfo *keyInfoFromExprList( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* Form the KeyInfo object from this ExprList */ + int iStart, /* Begin with this column of pList */ + int nExtra /* Add this many extra columns to the end */ +); + +/* +** Generate code that will push the record in registers regData +** through regData+nData-1 onto the sorter. +*/ +static void pushOntoSorter( + Parse *pParse, /* Parser context */ + SortCtx *pSort, /* Information about the ORDER BY clause */ + Select *pSelect, /* The whole SELECT statement */ + int regData, /* First register holding data to be sorted */ + int regOrigData, /* First register holding data before packing */ + int nData, /* Number of elements in the data array */ + int nPrefixReg /* No. of reg prior to regData available for use */ +){ + Vdbe *v = pParse->pVdbe; /* Stmt under construction */ + int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0); + int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ + int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ + int regBase; /* Regs for sorter record */ + int regRecord = ++pParse->nMem; /* Assembled sorter record */ + int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ + int op; /* Opcode to add sorter record to sorter */ + int iLimit; /* LIMIT counter */ + + assert( bSeq==0 || bSeq==1 ); + assert( nData==1 || regData==regOrigData ); + if( nPrefixReg ){ + assert( nPrefixReg==nExpr+bSeq ); + regBase = regData - nExpr - bSeq; + }else{ + regBase = pParse->nMem + 1; + pParse->nMem += nBase; + } + assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); + iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; + pSort->labelDone = sqlite3VdbeMakeLabel(v); + sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, + SQLITE_ECEL_DUP|SQLITE_ECEL_REF); + if( bSeq ){ + sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); + } + if( nPrefixReg==0 ){ + sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); + if( nOBSat>0 ){ + int regPrevKey; /* The first nOBSat columns of the previous row */ + int addrFirst; /* Address of the OP_IfNot opcode */ + int addrJmp; /* Address of the OP_Jump opcode */ + VdbeOp *pOp; /* Opcode that opens the sorter */ + int nKey; /* Number of sorting key columns, including OP_Sequence */ + KeyInfo *pKI; /* Original KeyInfo on the sorter table */ + + regPrevKey = pParse->nMem+1; + pParse->nMem += pSort->nOBSat; + nKey = nExpr - pSort->nOBSat + bSeq; + if( bSeq ){ + addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); + }else{ + addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor); + } + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat); + pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex); + if( pParse->db->mallocFailed ) return; + pOp->p2 = nKey + nData; + pKI = pOp->p4.pKeyInfo; + memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */ + sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO); + testcase( pKI->nXField>2 ); + pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat, + pKI->nXField-1); + addrJmp = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v); + pSort->labelBkOut = sqlite3VdbeMakeLabel(v); + pSort->regReturn = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); + sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor); + if( iLimit ){ + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone); + VdbeCoverage(v); + } + sqlite3VdbeJumpHere(v, addrFirst); + sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat); + sqlite3VdbeJumpHere(v, addrJmp); + } + if( pSort->sortFlags & SORTFLAG_UseSorter ){ + op = OP_SorterInsert; + }else{ + op = OP_IdxInsert; + } + sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord); + if( iLimit ){ + int addr; + addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, 1); VdbeCoverage(v); + sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor); + sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor); + sqlite3VdbeJumpHere(v, addr); + } +} + +/* +** Add code to implement the OFFSET +*/ +static void codeOffset( + Vdbe *v, /* Generate code into this VM */ + int iOffset, /* Register holding the offset counter */ + int iContinue /* Jump here to skip the current record */ +){ + if( iOffset>0 ){ + sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v); + VdbeComment((v, "OFFSET")); + } +} + +/* +** Add code that will check to make sure the N registers starting at iMem +** form a distinct entry. iTab is a sorting index that holds previously +** seen combinations of the N values. A new entry is made in iTab +** if the current N values are new. +** +** A jump to addrRepeat is made and the N+1 values are popped from the +** stack if the top N elements are not distinct. +*/ +static void codeDistinct( + Parse *pParse, /* Parsing and code generating context */ + int iTab, /* A sorting index used to test for distinctness */ + int addrRepeat, /* Jump to here if not distinct */ + int N, /* Number of elements */ + int iMem /* First element */ +){ + Vdbe *v; + int r1; + + v = pParse->pVdbe; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); + sqlite3ReleaseTempReg(pParse, r1); +} + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Generate an error message when a SELECT is used within a subexpression +** (example: "a IN (SELECT * FROM table)") but it has more than 1 result +** column. We do this in a subroutine because the error used to occur +** in multiple places. (The error only occurs in one place now, but we +** retain the subroutine to minimize code disruption.) +*/ +static int checkForMultiColumnSelectError( + Parse *pParse, /* Parse context. */ + SelectDest *pDest, /* Destination of SELECT results */ + int nExpr /* Number of result columns returned by SELECT */ +){ + int eDest = pDest->eDest; + if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ + sqlite3ErrorMsg(pParse, "only a single result allowed for " + "a SELECT that is part of an expression"); + return 1; + }else{ + return 0; + } +} +#endif + +/* +** This routine generates the code for the inside of the inner loop +** of a SELECT. +** +** If srcTab is negative, then the pEList expressions +** are evaluated in order to get the data for this row. If srcTab is +** zero or more, then data is pulled from srcTab and pEList is used only +** to get number columns and the datatype for each column. +*/ +static void selectInnerLoop( + Parse *pParse, /* The parser context */ + Select *p, /* The complete select statement being coded */ + ExprList *pEList, /* List of values being extracted */ + int srcTab, /* Pull data from this table */ + SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */ + DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */ + SelectDest *pDest, /* How to dispose of the results */ + int iContinue, /* Jump here to continue with next row */ + int iBreak /* Jump here to break out of the inner loop */ +){ + Vdbe *v = pParse->pVdbe; + int i; + int hasDistinct; /* True if the DISTINCT keyword is present */ + int regResult; /* Start of memory holding result set */ + int eDest = pDest->eDest; /* How to dispose of results */ + int iParm = pDest->iSDParm; /* First argument to disposal method */ + int nResultCol; /* Number of result columns */ + int nPrefixReg = 0; /* Number of extra registers before regResult */ + + assert( v ); + assert( pEList!=0 ); + hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; + if( pSort && pSort->pOrderBy==0 ) pSort = 0; + if( pSort==0 && !hasDistinct ){ + assert( iContinue!=0 ); + codeOffset(v, p->iOffset, iContinue); + } + + /* Pull the requested columns. + */ + nResultCol = pEList->nExpr; + + if( pDest->iSdst==0 ){ + if( pSort ){ + nPrefixReg = pSort->pOrderBy->nExpr; + if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++; + pParse->nMem += nPrefixReg; + } + pDest->iSdst = pParse->nMem+1; + pParse->nMem += nResultCol; + }else if( pDest->iSdst+nResultCol > pParse->nMem ){ + /* This is an error condition that can result, for example, when a SELECT + ** on the right-hand side of an INSERT contains more result columns than + ** there are columns in the table on the left. The error will be caught + ** and reported later. But we need to make sure enough memory is allocated + ** to avoid other spurious errors in the meantime. */ + pParse->nMem += nResultCol; + } + pDest->nSdst = nResultCol; + regResult = pDest->iSdst; + if( srcTab>=0 ){ + for(i=0; ia[i].zName)); + } + }else if( eDest!=SRT_Exists ){ + /* If the destination is an EXISTS(...) expression, the actual + ** values returned by the SELECT are not required. + */ + u8 ecelFlags; + if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ + ecelFlags = SQLITE_ECEL_DUP; + }else{ + ecelFlags = 0; + } + sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags); + } + + /* If the DISTINCT keyword was present on the SELECT statement + ** and this row has been seen before, then do not make this row + ** part of the result. + */ + if( hasDistinct ){ + switch( pDistinct->eTnctType ){ + case WHERE_DISTINCT_ORDERED: { + VdbeOp *pOp; /* No longer required OpenEphemeral instr. */ + int iJump; /* Jump destination */ + int regPrev; /* Previous row content */ + + /* Allocate space for the previous row */ + regPrev = pParse->nMem+1; + pParse->nMem += nResultCol; + + /* Change the OP_OpenEphemeral coded earlier to an OP_Null + ** sets the MEM_Cleared bit on the first register of the + ** previous value. This will cause the OP_Ne below to always + ** fail on the first iteration of the loop even if the first + ** row is all NULLs. + */ + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct); + pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct); + pOp->opcode = OP_Null; + pOp->p1 = 1; + pOp->p2 = regPrev; + + iJump = sqlite3VdbeCurrentAddr(v) + nResultCol; + for(i=0; ia[i].pExpr); + if( idb->mallocFailed ); + sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1); + break; + } + + case WHERE_DISTINCT_UNIQUE: { + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct); + break; + } + + default: { + assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED ); + codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, + regResult); + break; + } + } + if( pSort==0 ){ + codeOffset(v, p->iOffset, iContinue); + } + } + + switch( eDest ){ + /* In this mode, write each query result to the key of the temporary + ** table iParm. + */ +#ifndef SQLITE_OMIT_COMPOUND_SELECT + case SRT_Union: { + int r1; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + + /* Construct a record from the query result, but instead of + ** saving that record, use it as a key to delete elements from + ** the temporary table iParm. + */ + case SRT_Except: { + sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol); + break; + } +#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + + /* Store the result as data using a unique key. + */ + case SRT_Fifo: + case SRT_DistFifo: + case SRT_Table: + case SRT_EphemTab: { + int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1); + testcase( eDest==SRT_Table ); + testcase( eDest==SRT_EphemTab ); + testcase( eDest==SRT_Fifo ); + testcase( eDest==SRT_DistFifo ); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg); +#ifndef SQLITE_OMIT_CTE + if( eDest==SRT_DistFifo ){ + /* If the destination is DistFifo, then cursor (iParm+1) is open + ** on an ephemeral index. If the current row is already present + ** in the index, do not write it to the output. If not, add the + ** current row to the index and proceed with writing it to the + ** output table as well. */ + int addr = sqlite3VdbeCurrentAddr(v) + 4; + sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1); + assert( pSort==0 ); + } +#endif + if( pSort ){ + pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); + }else{ + int r2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3ReleaseTempReg(pParse, r2); + } + sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1); + break; + } + +#ifndef SQLITE_OMIT_SUBQUERY + /* If we are creating a set for an "expr IN (SELECT ...)" construct, + ** then there should be a single item on the stack. Write this + ** item into the set table with bogus data. + */ + case SRT_Set: { + assert( nResultCol==1 ); + pDest->affSdst = + sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst); + if( pSort ){ + /* At first glance you would think we could optimize out the + ** ORDER BY in this case since the order of entries in the set + ** does not matter. But there might be a LIMIT clause, in which + ** case the order does matter */ + pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); + }else{ + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1); + sqlite3ExprCacheAffinityChange(pParse, regResult, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + } + break; + } + + /* If any row exist in the result set, record that fact and abort. + */ + case SRT_Exists: { + sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); + /* The LIMIT clause will terminate the loop for us */ + break; + } + + /* If this is a scalar select that is part of an expression, then + ** store the results in the appropriate memory cell and break out + ** of the scan loop. + */ + case SRT_Mem: { + assert( nResultCol==1 ); + if( pSort ){ + pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); + }else{ + assert( regResult==iParm ); + /* The LIMIT clause will jump out of the loop for us */ + } + break; + } +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */ + + case SRT_Coroutine: /* Send data to a co-routine */ + case SRT_Output: { /* Return the results */ + testcase( eDest==SRT_Coroutine ); + testcase( eDest==SRT_Output ); + if( pSort ){ + pushOntoSorter(pParse, pSort, p, regResult, regResult, nResultCol, + nPrefixReg); + }else if( eDest==SRT_Coroutine ){ + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); + }else{ + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); + sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); + } + break; + } + +#ifndef SQLITE_OMIT_CTE + /* Write the results into a priority queue that is order according to + ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an + ** index with pSO->nExpr+2 columns. Build a key using pSO for the first + ** pSO->nExpr columns, then make sure all keys are unique by adding a + ** final OP_Sequence column. The last column is the record as a blob. + */ + case SRT_DistQueue: + case SRT_Queue: { + int nKey; + int r1, r2, r3; + int addrTest = 0; + ExprList *pSO; + pSO = pDest->pOrderBy; + assert( pSO ); + nKey = pSO->nExpr; + r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3GetTempRange(pParse, nKey+2); + r3 = r2+nKey+1; + if( eDest==SRT_DistQueue ){ + /* If the destination is DistQueue, then cursor (iParm+1) is open + ** on a second ephemeral index that holds all values every previously + ** added to the queue. */ + addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, + regResult, nResultCol); + VdbeCoverage(v); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3); + if( eDest==SRT_DistQueue ){ + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + } + for(i=0; ia[i].u.x.iOrderByCol - 1, + r2+i); + } + sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); + sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempRange(pParse, r2, nKey+2); + break; + } +#endif /* SQLITE_OMIT_CTE */ + + + +#if !defined(SQLITE_OMIT_TRIGGER) + /* Discard the results. This is used for SELECT statements inside + ** the body of a TRIGGER. The purpose of such selects is to call + ** user-defined functions that have side effects. We do not care + ** about the actual results of the select. + */ + default: { + assert( eDest==SRT_Discard ); + break; + } +#endif + } + + /* Jump to the end of the loop if the LIMIT is reached. Except, if + ** there is a sorter, in which case the sorter has already limited + ** the output for us. + */ + if( pSort==0 && p->iLimit ){ + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); + } +} + +/* +** Allocate a KeyInfo object sufficient for an index of N key columns and +** X extra columns. +*/ +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ + int nExtra = (N+X)*(sizeof(CollSeq*)+1); + KeyInfo *p = sqlite3Malloc(sizeof(KeyInfo) + nExtra); + if( p ){ + p->aSortOrder = (u8*)&p->aColl[N+X]; + p->nField = (u16)N; + p->nXField = (u16)X; + p->enc = ENC(db); + p->db = db; + p->nRef = 1; + memset(&p[1], 0, nExtra); + }else{ + sqlite3OomFault(db); + } + return p; +} + +/* +** Deallocate a KeyInfo object +*/ +SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){ + if( p ){ + assert( p->nRef>0 ); + p->nRef--; + if( p->nRef==0 ) sqlite3DbFree(0, p); + } +} + +/* +** Make a new pointer to a KeyInfo object +*/ +SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){ + if( p ){ + assert( p->nRef>0 ); + p->nRef++; + } + return p; +} + +#ifdef SQLITE_DEBUG +/* +** Return TRUE if a KeyInfo object can be change. The KeyInfo object +** can only be changed if this is just a single reference to the object. +** +** This routine is used only inside of assert() statements. +*/ +SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; } +#endif /* SQLITE_DEBUG */ + +/* +** Given an expression list, generate a KeyInfo structure that records +** the collating sequence for each expression in that expression list. +** +** If the ExprList is an ORDER BY or GROUP BY clause then the resulting +** KeyInfo structure is appropriate for initializing a virtual index to +** implement that clause. If the ExprList is the result set of a SELECT +** then the KeyInfo structure is appropriate for initializing a virtual +** index to implement a DISTINCT test. +** +** Space to hold the KeyInfo structure is obtained from malloc. The calling +** function is responsible for seeing that this structure is eventually +** freed. +*/ +static KeyInfo *keyInfoFromExprList( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* Form the KeyInfo object from this ExprList */ + int iStart, /* Begin with this column of pList */ + int nExtra /* Add this many extra columns to the end */ +){ + int nExpr; + KeyInfo *pInfo; + struct ExprList_item *pItem; + sqlite3 *db = pParse->db; + int i; + + nExpr = pList->nExpr; + pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1); + if( pInfo ){ + assert( sqlite3KeyInfoIsWriteable(pInfo) ); + for(i=iStart, pItem=pList->a+iStart; ipExpr); + if( !pColl ) pColl = db->pDfltColl; + pInfo->aColl[i-iStart] = pColl; + pInfo->aSortOrder[i-iStart] = pItem->sortOrder; + } + } + return pInfo; +} + +/* +** Name of the connection operator, used for error messages. +*/ +static const char *selectOpName(int id){ + char *z; + switch( id ){ + case TK_ALL: z = "UNION ALL"; break; + case TK_INTERSECT: z = "INTERSECT"; break; + case TK_EXCEPT: z = "EXCEPT"; break; + default: z = "UNION"; break; + } + return z; +} + +#ifndef SQLITE_OMIT_EXPLAIN +/* +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function +** is a no-op. Otherwise, it adds a single row of output to the EQP result, +** where the caption is of the form: +** +** "USE TEMP B-TREE FOR xxx" +** +** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which +** is determined by the zUsage argument. +*/ +static void explainTempTable(Parse *pParse, const char *zUsage){ + if( pParse->explain==2 ){ + Vdbe *v = pParse->pVdbe; + char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +} + +/* +** Assign expression b to lvalue a. A second, no-op, version of this macro +** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code +** in sqlite3Select() to assign values to structure member variables that +** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the +** code with #ifndef directives. +*/ +# define explainSetInteger(a, b) a = b + +#else +/* No-op versions of the explainXXX() functions and macros. */ +# define explainTempTable(y,z) +# define explainSetInteger(y,z) +#endif + +#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT) +/* +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function +** is a no-op. Otherwise, it adds a single row of output to the EQP result, +** where the caption is of one of the two forms: +** +** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)" +** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)" +** +** where iSub1 and iSub2 are the integers passed as the corresponding +** function parameters, and op is the text representation of the parameter +** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT, +** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is +** false, or the second form if it is true. +*/ +static void explainComposite( + Parse *pParse, /* Parse context */ + int op, /* One of TK_UNION, TK_EXCEPT etc. */ + int iSub1, /* Subquery id 1 */ + int iSub2, /* Subquery id 2 */ + int bUseTmp /* True if a temp table was used */ +){ + assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL ); + if( pParse->explain==2 ){ + Vdbe *v = pParse->pVdbe; + char *zMsg = sqlite3MPrintf( + pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2, + bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op) + ); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +} +#else +/* No-op versions of the explainXXX() functions and macros. */ +# define explainComposite(v,w,x,y,z) +#endif + +/* +** If the inner loop was generated using a non-null pOrderBy argument, +** then the results were placed in a sorter. After the loop is terminated +** we need to run the sorter and output the results. The following +** routine generates the code needed to do that. +*/ +static void generateSortTail( + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement */ + SortCtx *pSort, /* Information on the ORDER BY clause */ + int nColumn, /* Number of columns of data */ + SelectDest *pDest /* Write the sorted results here */ +){ + Vdbe *v = pParse->pVdbe; /* The prepared statement */ + int addrBreak = pSort->labelDone; /* Jump here to exit loop */ + int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */ + int addr; + int addrOnce = 0; + int iTab; + ExprList *pOrderBy = pSort->pOrderBy; + int eDest = pDest->eDest; + int iParm = pDest->iSDParm; + int regRow; + int regRowid; + int nKey; + int iSortTab; /* Sorter cursor to read from */ + int nSortData; /* Trailing values to read from sorter */ + int i; + int bSeq; /* True if sorter record includes seq. no. */ +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + struct ExprList_item *aOutEx = p->pEList->a; +#endif + + assert( addrBreak<0 ); + if( pSort->labelBkOut ){ + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); + sqlite3VdbeGoto(v, addrBreak); + sqlite3VdbeResolveLabel(v, pSort->labelBkOut); + } + iTab = pSort->iECursor; + if( eDest==SRT_Output || eDest==SRT_Coroutine ){ + regRowid = 0; + regRow = pDest->iSdst; + nSortData = nColumn; + }else{ + regRowid = sqlite3GetTempReg(pParse); + regRow = sqlite3GetTempReg(pParse); + nSortData = 1; + } + nKey = pOrderBy->nExpr - pSort->nOBSat; + if( pSort->sortFlags & SORTFLAG_UseSorter ){ + int regSortOut = ++pParse->nMem; + iSortTab = pParse->nTab++; + if( pSort->labelBkOut ){ + addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v); + } + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); + if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); + addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); + VdbeCoverage(v); + codeOffset(v, p->iOffset, addrContinue); + sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); + bSeq = 0; + }else{ + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); + codeOffset(v, p->iOffset, addrContinue); + iSortTab = iTab; + bSeq = 1; + } + for(i=0; iaffSdst, 1); + sqlite3ExprCacheAffinityChange(pParse, regRow, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); + break; + } + case SRT_Mem: { + assert( nColumn==1 ); + sqlite3ExprCodeMove(pParse, regRow, iParm, 1); + /* The LIMIT clause will terminate the loop for us */ + break; + } +#endif + default: { + assert( eDest==SRT_Output || eDest==SRT_Coroutine ); + testcase( eDest==SRT_Output ); + testcase( eDest==SRT_Coroutine ); + if( eDest==SRT_Output ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn); + sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn); + }else{ + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); + } + break; + } + } + if( regRowid ){ + sqlite3ReleaseTempReg(pParse, regRow); + sqlite3ReleaseTempReg(pParse, regRowid); + } + /* The bottom of the loop + */ + sqlite3VdbeResolveLabel(v, addrContinue); + if( pSort->sortFlags & SORTFLAG_UseSorter ){ + sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v); + } + if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn); + sqlite3VdbeResolveLabel(v, addrBreak); +} + +/* +** Return a pointer to a string containing the 'declaration type' of the +** expression pExpr. The string may be treated as static by the caller. +** +** Also try to estimate the size of the returned value and return that +** result in *pEstWidth. +** +** The declaration type is the exact datatype definition extracted from the +** original CREATE TABLE statement if the expression is a column. The +** declaration type for a ROWID field is INTEGER. Exactly when an expression +** is considered a column can be complex in the presence of subqueries. The +** result-set expression in all of the following SELECT statements is +** considered a column by this function. +** +** SELECT col FROM tbl; +** SELECT (SELECT col FROM tbl; +** SELECT (SELECT col FROM tbl); +** SELECT abc FROM (SELECT col AS abc FROM tbl); +** +** The declaration type for any expression other than a column is NULL. +** +** This routine has either 3 or 6 parameters depending on whether or not +** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. +*/ +#ifdef SQLITE_ENABLE_COLUMN_METADATA +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F) +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F) +#endif +static const char *columnTypeImpl( + NameContext *pNC, + Expr *pExpr, +#ifdef SQLITE_ENABLE_COLUMN_METADATA + const char **pzOrigDb, + const char **pzOrigTab, + const char **pzOrigCol, +#endif + u8 *pEstWidth +){ + char const *zType = 0; + int j; + u8 estWidth = 1; +#ifdef SQLITE_ENABLE_COLUMN_METADATA + char const *zOrigDb = 0; + char const *zOrigTab = 0; + char const *zOrigCol = 0; +#endif + + assert( pExpr!=0 ); + assert( pNC->pSrcList!=0 ); + switch( pExpr->op ){ + case TK_AGG_COLUMN: + case TK_COLUMN: { + /* The expression is a column. Locate the table the column is being + ** extracted from in NameContext.pSrcList. This table may be real + ** database table or a subquery. + */ + Table *pTab = 0; /* Table structure column is extracted from */ + Select *pS = 0; /* Select the column is extracted from */ + int iCol = pExpr->iColumn; /* Index of column in pTab */ + testcase( pExpr->op==TK_AGG_COLUMN ); + testcase( pExpr->op==TK_COLUMN ); + while( pNC && !pTab ){ + SrcList *pTabList = pNC->pSrcList; + for(j=0;jnSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); + if( jnSrc ){ + pTab = pTabList->a[j].pTab; + pS = pTabList->a[j].pSelect; + }else{ + pNC = pNC->pNext; + } + } + + if( pTab==0 ){ + /* At one time, code such as "SELECT new.x" within a trigger would + ** cause this condition to run. Since then, we have restructured how + ** trigger code is generated and so this condition is no longer + ** possible. However, it can still be true for statements like + ** the following: + ** + ** CREATE TABLE t1(col INTEGER); + ** SELECT (SELECT t1.col) FROM FROM t1; + ** + ** when columnType() is called on the expression "t1.col" in the + ** sub-select. In this case, set the column type to NULL, even + ** though it should really be "INTEGER". + ** + ** This is not a problem, as the column type of "t1.col" is never + ** used. When columnType() is called on the expression + ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT + ** branch below. */ + break; + } + + assert( pTab && pExpr->pTab==pTab ); + if( pS ){ + /* The "table" is actually a sub-select or a view in the FROM clause + ** of the SELECT statement. Return the declaration type and origin + ** data for the result-set column of the sub-select. + */ + if( iCol>=0 && ALWAYS(iColpEList->nExpr) ){ + /* If iCol is less than zero, then the expression requests the + ** rowid of the sub-select or view. This expression is legal (see + ** test case misc2.2.2) - it always evaluates to NULL. + ** + ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been + ** caught already by name resolution. + */ + NameContext sNC; + Expr *p = pS->pEList->a[iCol].pExpr; + sNC.pSrcList = pS->pSrc; + sNC.pNext = pNC; + sNC.pParse = pNC->pParse; + zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); + } + }else if( pTab->pSchema ){ + /* A real table */ + assert( !pS ); + if( iCol<0 ) iCol = pTab->iPKey; + assert( iCol==-1 || (iCol>=0 && iColnCol) ); +#ifdef SQLITE_ENABLE_COLUMN_METADATA + if( iCol<0 ){ + zType = "INTEGER"; + zOrigCol = "rowid"; + }else{ + zOrigCol = pTab->aCol[iCol].zName; + zType = sqlite3ColumnType(&pTab->aCol[iCol],0); + estWidth = pTab->aCol[iCol].szEst; + } + zOrigTab = pTab->zName; + if( pNC->pParse ){ + int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); + zOrigDb = pNC->pParse->db->aDb[iDb].zName; + } +#else + if( iCol<0 ){ + zType = "INTEGER"; + }else{ + zType = sqlite3ColumnType(&pTab->aCol[iCol],0); + estWidth = pTab->aCol[iCol].szEst; + } +#endif + } + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_SELECT: { + /* The expression is a sub-select. Return the declaration type and + ** origin info for the single column in the result set of the SELECT + ** statement. + */ + NameContext sNC; + Select *pS = pExpr->x.pSelect; + Expr *p = pS->pEList->a[0].pExpr; + assert( ExprHasProperty(pExpr, EP_xIsSelect) ); + sNC.pSrcList = pS->pSrc; + sNC.pNext = pNC; + sNC.pParse = pNC->pParse; + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); + break; + } +#endif + } + +#ifdef SQLITE_ENABLE_COLUMN_METADATA + if( pzOrigDb ){ + assert( pzOrigTab && pzOrigCol ); + *pzOrigDb = zOrigDb; + *pzOrigTab = zOrigTab; + *pzOrigCol = zOrigCol; + } +#endif + if( pEstWidth ) *pEstWidth = estWidth; + return zType; +} + +/* +** Generate code that will tell the VDBE the declaration types of columns +** in the result set. +*/ +static void generateColumnTypes( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* List of tables */ + ExprList *pEList /* Expressions defining the result set */ +){ +#ifndef SQLITE_OMIT_DECLTYPE + Vdbe *v = pParse->pVdbe; + int i; + NameContext sNC; + sNC.pSrcList = pTabList; + sNC.pParse = pParse; + for(i=0; inExpr; i++){ + Expr *p = pEList->a[i].pExpr; + const char *zType; +#ifdef SQLITE_ENABLE_COLUMN_METADATA + const char *zOrigDb = 0; + const char *zOrigTab = 0; + const char *zOrigCol = 0; + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0); + + /* The vdbe must make its own copy of the column-type and other + ** column specific strings, in case the schema is reset before this + ** virtual machine is deleted. + */ + sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT); +#else + zType = columnType(&sNC, p, 0, 0, 0, 0); +#endif + sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT); + } +#endif /* !defined(SQLITE_OMIT_DECLTYPE) */ +} + +/* +** Generate code that will tell the VDBE the names of columns +** in the result set. This information is used to provide the +** azCol[] values in the callback. +*/ +static void generateColumnNames( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* List of tables */ + ExprList *pEList /* Expressions defining the result set */ +){ + Vdbe *v = pParse->pVdbe; + int i, j; + sqlite3 *db = pParse->db; + int fullNames, shortNames; + +#ifndef SQLITE_OMIT_EXPLAIN + /* If this is an EXPLAIN, skip this step */ + if( pParse->explain ){ + return; + } +#endif + + if( pParse->colNamesSet || db->mallocFailed ) return; + assert( v!=0 ); + assert( pTabList!=0 ); + pParse->colNamesSet = 1; + fullNames = (db->flags & SQLITE_FullColNames)!=0; + shortNames = (db->flags & SQLITE_ShortColNames)!=0; + sqlite3VdbeSetNumCols(v, pEList->nExpr); + for(i=0; inExpr; i++){ + Expr *p; + p = pEList->a[i].pExpr; + if( NEVER(p==0) ) continue; + if( pEList->a[i].zName ){ + char *zName = pEList->a[i].zName; + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT); + }else if( p->op==TK_COLUMN || p->op==TK_AGG_COLUMN ){ + Table *pTab; + char *zCol; + int iCol = p->iColumn; + for(j=0; ALWAYS(jnSrc); j++){ + if( pTabList->a[j].iCursor==p->iTable ) break; + } + assert( jnSrc ); + pTab = pTabList->a[j].pTab; + if( iCol<0 ) iCol = pTab->iPKey; + assert( iCol==-1 || (iCol>=0 && iColnCol) ); + if( iCol<0 ){ + zCol = "rowid"; + }else{ + zCol = pTab->aCol[iCol].zName; + } + if( !shortNames && !fullNames ){ + sqlite3VdbeSetColName(v, i, COLNAME_NAME, + sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC); + }else if( fullNames ){ + char *zName = 0; + zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol); + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC); + }else{ + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT); + } + }else{ + const char *z = pEList->a[i].zSpan; + z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z); + sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC); + } + } + generateColumnTypes(pParse, pTabList, pEList); +} + +/* +** Given an expression list (which is really the list of expressions +** that form the result set of a SELECT statement) compute appropriate +** column names for a table that would hold the expression list. +** +** All column names will be unique. +** +** Only the column names are computed. Column.zType, Column.zColl, +** and other fields of Column are zeroed. +** +** Return SQLITE_OK on success. If a memory allocation error occurs, +** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM. +*/ +SQLITE_PRIVATE int sqlite3ColumnsFromExprList( + Parse *pParse, /* Parsing context */ + ExprList *pEList, /* Expr list from which to derive column names */ + i16 *pnCol, /* Write the number of columns here */ + Column **paCol /* Write the new column list here */ +){ + sqlite3 *db = pParse->db; /* Database connection */ + int i, j; /* Loop counters */ + u32 cnt; /* Index added to make the name unique */ + Column *aCol, *pCol; /* For looping over result columns */ + int nCol; /* Number of columns in the result set */ + Expr *p; /* Expression for a single result column */ + char *zName; /* Column name */ + int nName; /* Size of name in zName[] */ + Hash ht; /* Hash table of column names */ + + sqlite3HashInit(&ht); + if( pEList ){ + nCol = pEList->nExpr; + aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); + testcase( aCol==0 ); + }else{ + nCol = 0; + aCol = 0; + } + assert( nCol==(i16)nCol ); + *pnCol = nCol; + *paCol = aCol; + + for(i=0, pCol=aCol; imallocFailed; i++, pCol++){ + /* Get an appropriate name for the column + */ + p = sqlite3ExprSkipCollate(pEList->a[i].pExpr); + if( (zName = pEList->a[i].zName)!=0 ){ + /* If the column contains an "AS " phrase, use as the name */ + }else{ + Expr *pColExpr = p; /* The expression that is the result column name */ + Table *pTab; /* Table associated with this expression */ + while( pColExpr->op==TK_DOT ){ + pColExpr = pColExpr->pRight; + assert( pColExpr!=0 ); + } + if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){ + /* For columns use the column name name */ + int iCol = pColExpr->iColumn; + pTab = pColExpr->pTab; + if( iCol<0 ) iCol = pTab->iPKey; + zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid"; + }else if( pColExpr->op==TK_ID ){ + assert( !ExprHasProperty(pColExpr, EP_IntValue) ); + zName = pColExpr->u.zToken; + }else{ + /* Use the original text of the column expression as its name */ + zName = pEList->a[i].zSpan; + } + } + zName = sqlite3MPrintf(db, "%s", zName); + + /* Make sure the column name is unique. If the name is not unique, + ** append an integer to the name so that it becomes unique. + */ + cnt = 0; + while( zName && sqlite3HashFind(&ht, zName)!=0 ){ + nName = sqlite3Strlen30(zName); + if( nName>0 ){ + for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} + if( zName[j]==':' ) nName = j; + } + zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); + if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); + } + pCol->zName = zName; + sqlite3ColumnPropertiesFromName(0, pCol); + if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ + sqlite3OomFault(db); + } + } + sqlite3HashClear(&ht); + if( db->mallocFailed ){ + for(j=0; jdb; + NameContext sNC; + Column *pCol; + CollSeq *pColl; + int i; + Expr *p; + struct ExprList_item *a; + u64 szAll = 0; + + assert( pSelect!=0 ); + assert( (pSelect->selFlags & SF_Resolved)!=0 ); + assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); + if( db->mallocFailed ) return; + memset(&sNC, 0, sizeof(sNC)); + sNC.pSrcList = pSelect->pSrc; + a = pSelect->pEList->a; + for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ + const char *zType; + int n, m; + p = a[i].pExpr; + zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst); + szAll += pCol->szEst; + pCol->affinity = sqlite3ExprAffinity(p); + if( zType && (m = sqlite3Strlen30(zType))>0 ){ + n = sqlite3Strlen30(pCol->zName); + pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2); + if( pCol->zName ){ + memcpy(&pCol->zName[n+1], zType, m+1); + pCol->colFlags |= COLFLAG_HASTYPE; + } + } + if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB; + pColl = sqlite3ExprCollSeq(pParse, p); + if( pColl && pCol->zColl==0 ){ + pCol->zColl = sqlite3DbStrDup(db, pColl->zName); + } + } + pTab->szTabRow = sqlite3LogEst(szAll*4); +} + +/* +** Given a SELECT statement, generate a Table structure that describes +** the result set of that SELECT. +*/ +SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){ + Table *pTab; + sqlite3 *db = pParse->db; + int savedFlags; + + savedFlags = db->flags; + db->flags &= ~SQLITE_FullColNames; + db->flags |= SQLITE_ShortColNames; + sqlite3SelectPrep(pParse, pSelect, 0); + if( pParse->nErr ) return 0; + while( pSelect->pPrior ) pSelect = pSelect->pPrior; + db->flags = savedFlags; + pTab = sqlite3DbMallocZero(db, sizeof(Table) ); + if( pTab==0 ){ + return 0; + } + /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside + ** is disabled */ + assert( db->lookaside.bDisable ); + pTab->nRef = 1; + pTab->zName = 0; + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); + sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect); + pTab->iPKey = -1; + if( db->mallocFailed ){ + sqlite3DeleteTable(db, pTab); + return 0; + } + return pTab; +} + +/* +** Get a VDBE for the given parser context. Create a new one if necessary. +** If an error occurs, return NULL and leave a message in pParse. +*/ +static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){ + Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse); + if( v ) sqlite3VdbeAddOp0(v, OP_Init); + if( pParse->pToplevel==0 + && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) + ){ + pParse->okConstFactor = 1; + } + return v; +} +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){ + Vdbe *v = pParse->pVdbe; + return v ? v : allocVdbe(pParse); +} + + +/* +** Compute the iLimit and iOffset fields of the SELECT based on the +** pLimit and pOffset expressions. pLimit and pOffset hold the expressions +** that appear in the original SQL statement after the LIMIT and OFFSET +** keywords. Or NULL if those keywords are omitted. iLimit and iOffset +** are the integer memory register numbers for counters used to compute +** the limit and offset. If there is no limit and/or offset, then +** iLimit and iOffset are negative. +** +** This routine changes the values of iLimit and iOffset only if +** a limit or offset is defined by pLimit and pOffset. iLimit and +** iOffset should have been preset to appropriate default values (zero) +** prior to calling this routine. +** +** The iOffset register (if it exists) is initialized to the value +** of the OFFSET. The iLimit register is initialized to LIMIT. Register +** iOffset+1 is initialized to LIMIT+OFFSET. +** +** Only if pLimit!=0 or pOffset!=0 do the limit registers get +** redefined. The UNION ALL operator uses this property to force +** the reuse of the same limit and offset registers across multiple +** SELECT statements. +*/ +static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ + Vdbe *v = 0; + int iLimit = 0; + int iOffset; + int n; + if( p->iLimit ) return; + + /* + ** "LIMIT -1" always shows all rows. There is some + ** controversy about what the correct behavior should be. + ** The current implementation interprets "LIMIT 0" to mean + ** no rows. + */ + sqlite3ExprCacheClear(pParse); + assert( p->pOffset==0 || p->pLimit!=0 ); + if( p->pLimit ){ + p->iLimit = iLimit = ++pParse->nMem; + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + if( sqlite3ExprIsInteger(p->pLimit, &n) ){ + sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); + VdbeComment((v, "LIMIT counter")); + if( n==0 ){ + sqlite3VdbeGoto(v, iBreak); + }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){ + p->nSelectRow = sqlite3LogEst((u64)n); + p->selFlags |= SF_FixedLimit; + } + }else{ + sqlite3ExprCode(pParse, p->pLimit, iLimit); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); + VdbeComment((v, "LIMIT counter")); + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); + } + if( p->pOffset ){ + p->iOffset = iOffset = ++pParse->nMem; + pParse->nMem++; /* Allocate an extra register for limit+offset */ + sqlite3ExprCode(pParse, p->pOffset, iOffset); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v); + VdbeComment((v, "OFFSET counter")); + sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset); + VdbeComment((v, "LIMIT+OFFSET")); + } + } +} + +#ifndef SQLITE_OMIT_COMPOUND_SELECT +/* +** Return the appropriate collating sequence for the iCol-th column of +** the result set for the compound-select statement "p". Return NULL if +** the column has no default collating sequence. +** +** The collating sequence for the compound select is taken from the +** left-most term of the select that has a collating sequence. +*/ +static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ + CollSeq *pRet; + if( p->pPrior ){ + pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); + }else{ + pRet = 0; + } + assert( iCol>=0 ); + /* iCol must be less than p->pEList->nExpr. Otherwise an error would + ** have been thrown during name resolution and we would not have gotten + ** this far */ + if( pRet==0 && ALWAYS(iColpEList->nExpr) ){ + pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); + } + return pRet; +} + +/* +** The select statement passed as the second parameter is a compound SELECT +** with an ORDER BY clause. This function allocates and returns a KeyInfo +** structure suitable for implementing the ORDER BY. +** +** Space to hold the KeyInfo structure is obtained from malloc. The calling +** function is responsible for ensuring that this structure is eventually +** freed. +*/ +static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ + ExprList *pOrderBy = p->pOrderBy; + int nOrderBy = p->pOrderBy->nExpr; + sqlite3 *db = pParse->db; + KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1); + if( pRet ){ + int i; + for(i=0; ia[i]; + Expr *pTerm = pItem->pExpr; + CollSeq *pColl; + + if( pTerm->flags & EP_Collate ){ + pColl = sqlite3ExprCollSeq(pParse, pTerm); + }else{ + pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1); + if( pColl==0 ) pColl = db->pDfltColl; + pOrderBy->a[i].pExpr = + sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName); + } + assert( sqlite3KeyInfoIsWriteable(pRet) ); + pRet->aColl[i] = pColl; + pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder; + } + } + + return pRet; +} + +#ifndef SQLITE_OMIT_CTE +/* +** This routine generates VDBE code to compute the content of a WITH RECURSIVE +** query of the form: +** +** AS ( UNION [ALL] ) +** \___________/ \_______________/ +** p->pPrior p +** +** +** There is exactly one reference to the recursive-table in the FROM clause +** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag. +** +** The setup-query runs once to generate an initial set of rows that go +** into a Queue table. Rows are extracted from the Queue table one by +** one. Each row extracted from Queue is output to pDest. Then the single +** extracted row (now in the iCurrent table) becomes the content of the +** recursive-table for a recursive-query run. The output of the recursive-query +** is added back into the Queue table. Then another row is extracted from Queue +** and the iteration continues until the Queue table is empty. +** +** If the compound query operator is UNION then no duplicate rows are ever +** inserted into the Queue table. The iDistinct table keeps a copy of all rows +** that have ever been inserted into Queue and causes duplicates to be +** discarded. If the operator is UNION ALL, then duplicates are allowed. +** +** If the query has an ORDER BY, then entries in the Queue table are kept in +** ORDER BY order and the first entry is extracted for each cycle. Without +** an ORDER BY, the Queue table is just a FIFO. +** +** If a LIMIT clause is provided, then the iteration stops after LIMIT rows +** have been output to pDest. A LIMIT of zero means to output no rows and a +** negative LIMIT means to output all rows. If there is also an OFFSET clause +** with a positive value, then the first OFFSET outputs are discarded rather +** than being sent to pDest. The LIMIT count does not begin until after OFFSET +** rows have been skipped. +*/ +static void generateWithRecursiveQuery( + Parse *pParse, /* Parsing context */ + Select *p, /* The recursive SELECT to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */ + int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */ + Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ + Select *pSetup = p->pPrior; /* The setup query */ + int addrTop; /* Top of the loop */ + int addrCont, addrBreak; /* CONTINUE and BREAK addresses */ + int iCurrent = 0; /* The Current table */ + int regCurrent; /* Register holding Current table */ + int iQueue; /* The Queue table */ + int iDistinct = 0; /* To ensure unique results if UNION */ + int eDest = SRT_Fifo; /* How to write to Queue */ + SelectDest destQueue; /* SelectDest targetting the Queue table */ + int i; /* Loop counter */ + int rc; /* Result code */ + ExprList *pOrderBy; /* The ORDER BY clause */ + Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */ + int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ + + /* Obtain authorization to do a recursive query */ + if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; + + /* Process the LIMIT and OFFSET clauses, if they exist */ + addrBreak = sqlite3VdbeMakeLabel(v); + computeLimitRegisters(pParse, p, addrBreak); + pLimit = p->pLimit; + pOffset = p->pOffset; + regLimit = p->iLimit; + regOffset = p->iOffset; + p->pLimit = p->pOffset = 0; + p->iLimit = p->iOffset = 0; + pOrderBy = p->pOrderBy; + + /* Locate the cursor number of the Current table */ + for(i=0; ALWAYS(inSrc); i++){ + if( pSrc->a[i].fg.isRecursive ){ + iCurrent = pSrc->a[i].iCursor; + break; + } + } + + /* Allocate cursors numbers for Queue and Distinct. The cursor number for + ** the Distinct table must be exactly one greater than Queue in order + ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */ + iQueue = pParse->nTab++; + if( p->op==TK_UNION ){ + eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo; + iDistinct = pParse->nTab++; + }else{ + eDest = pOrderBy ? SRT_Queue : SRT_Fifo; + } + sqlite3SelectDestInit(&destQueue, eDest, iQueue); + + /* Allocate cursors for Current, Queue, and Distinct. */ + regCurrent = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol); + if( pOrderBy ){ + KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0, + (char*)pKeyInfo, P4_KEYINFO); + destQueue.pOrderBy = pOrderBy; + }else{ + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol); + } + VdbeComment((v, "Queue table")); + if( iDistinct ){ + p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0); + p->selFlags |= SF_UsesEphemeral; + } + + /* Detach the ORDER BY clause from the compound SELECT */ + p->pOrderBy = 0; + + /* Store the results of the setup-query in Queue. */ + pSetup->pNext = 0; + rc = sqlite3Select(pParse, pSetup, &destQueue); + pSetup->pNext = p; + if( rc ) goto end_of_recursive_query; + + /* Find the next row in the Queue and output that row */ + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v); + + /* Transfer the next row in Queue over to Current */ + sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */ + if( pOrderBy ){ + sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent); + }else{ + sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent); + } + sqlite3VdbeAddOp1(v, OP_Delete, iQueue); + + /* Output the single row in Current */ + addrCont = sqlite3VdbeMakeLabel(v); + codeOffset(v, regOffset, addrCont); + selectInnerLoop(pParse, p, p->pEList, iCurrent, + 0, 0, pDest, addrCont, addrBreak); + if( regLimit ){ + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak); + VdbeCoverage(v); + } + sqlite3VdbeResolveLabel(v, addrCont); + + /* Execute the recursive SELECT taking the single row in Current as + ** the value for the recursive-table. Store the results in the Queue. + */ + if( p->selFlags & SF_Aggregate ){ + sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported"); + }else{ + p->pPrior = 0; + sqlite3Select(pParse, p, &destQueue); + assert( p->pPrior==0 ); + p->pPrior = pSetup; + } + + /* Keep running the loop until the Queue is empty */ + sqlite3VdbeGoto(v, addrTop); + sqlite3VdbeResolveLabel(v, addrBreak); + +end_of_recursive_query: + sqlite3ExprListDelete(pParse->db, p->pOrderBy); + p->pOrderBy = pOrderBy; + p->pLimit = pLimit; + p->pOffset = pOffset; + return; +} +#endif /* SQLITE_OMIT_CTE */ + +/* Forward references */ +static int multiSelectOrderBy( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +); + +/* +** Handle the special case of a compound-select that originates from a +** VALUES clause. By handling this as a special case, we avoid deep +** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT +** on a VALUES clause. +** +** Because the Select object originates from a VALUES clause: +** (1) It has no LIMIT or OFFSET +** (2) All terms are UNION ALL +** (3) There is no ORDER BY clause +*/ +static int multiSelectValues( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + Select *pPrior; + int nRow = 1; + int rc = 0; + assert( p->selFlags & SF_MultiValue ); + do{ + assert( p->selFlags & SF_Values ); + assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); + assert( p->pLimit==0 ); + assert( p->pOffset==0 ); + assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr ); + if( p->pPrior==0 ) break; + assert( p->pPrior->pNext==p ); + p = p->pPrior; + nRow++; + }while(1); + while( p ){ + pPrior = p->pPrior; + p->pPrior = 0; + rc = sqlite3Select(pParse, p, pDest); + p->pPrior = pPrior; + if( rc ) break; + p->nSelectRow = nRow; + p = p->pNext; + } + return rc; +} + +/* +** This routine is called to process a compound query form from +** two or more separate queries using UNION, UNION ALL, EXCEPT, or +** INTERSECT +** +** "p" points to the right-most of the two queries. the query on the +** left is p->pPrior. The left query could also be a compound query +** in which case this routine will be called recursively. +** +** The results of the total query are to be written into a destination +** of type eDest with parameter iParm. +** +** Example 1: Consider a three-way compound SQL statement. +** +** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 +** +** This statement is parsed up as follows: +** +** SELECT c FROM t3 +** | +** `-----> SELECT b FROM t2 +** | +** `------> SELECT a FROM t1 +** +** The arrows in the diagram above represent the Select.pPrior pointer. +** So if this routine is called with p equal to the t3 query, then +** pPrior will be the t2 query. p->op will be TK_UNION in this case. +** +** Notice that because of the way SQLite parses compound SELECTs, the +** individual selects always group from left to right. +*/ +static int multiSelect( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + int rc = SQLITE_OK; /* Success code from a subroutine */ + Select *pPrior; /* Another SELECT immediately to our left */ + Vdbe *v; /* Generate code to this VDBE */ + SelectDest dest; /* Alternative data destination */ + Select *pDelete = 0; /* Chain of simple selects to delete */ + sqlite3 *db; /* Database connection */ +#ifndef SQLITE_OMIT_EXPLAIN + int iSub1 = 0; /* EQP id of left-hand query */ + int iSub2 = 0; /* EQP id of right-hand query */ +#endif + + /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only + ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. + */ + assert( p && p->pPrior ); /* Calling function guarantees this much */ + assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION ); + db = pParse->db; + pPrior = p->pPrior; + dest = *pDest; + if( pPrior->pOrderBy ){ + sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", + selectOpName(p->op)); + rc = 1; + goto multi_select_end; + } + if( pPrior->pLimit ){ + sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", + selectOpName(p->op)); + rc = 1; + goto multi_select_end; + } + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); /* The VDBE already created by calling function */ + + /* Create the destination temporary table if necessary + */ + if( dest.eDest==SRT_EphemTab ){ + assert( p->pEList ); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr); + dest.eDest = SRT_Table; + } + + /* Special handling for a compound-select that originates as a VALUES clause. + */ + if( p->selFlags & SF_MultiValue ){ + rc = multiSelectValues(pParse, p, &dest); + goto multi_select_end; + } + + /* Make sure all SELECTs in the statement have the same number of elements + ** in their result sets. + */ + assert( p->pEList && pPrior->pEList ); + assert( p->pEList->nExpr==pPrior->pEList->nExpr ); + +#ifndef SQLITE_OMIT_CTE + if( p->selFlags & SF_Recursive ){ + generateWithRecursiveQuery(pParse, p, &dest); + }else +#endif + + /* Compound SELECTs that have an ORDER BY clause are handled separately. + */ + if( p->pOrderBy ){ + return multiSelectOrderBy(pParse, p, pDest); + }else + + /* Generate code for the left and right SELECT statements. + */ + switch( p->op ){ + case TK_ALL: { + int addr = 0; + int nLimit; + assert( !pPrior->pLimit ); + pPrior->iLimit = p->iLimit; + pPrior->iOffset = p->iOffset; + pPrior->pLimit = p->pLimit; + pPrior->pOffset = p->pOffset; + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &dest); + p->pLimit = 0; + p->pOffset = 0; + if( rc ){ + goto multi_select_end; + } + p->pPrior = 0; + p->iLimit = pPrior->iLimit; + p->iOffset = pPrior->iOffset; + if( p->iLimit ){ + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); + VdbeComment((v, "Jump ahead if LIMIT reached")); + if( p->iOffset ){ + sqlite3VdbeAddOp3(v, OP_OffsetLimit, + p->iLimit, p->iOffset+1, p->iOffset); + } + } + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &dest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); + if( pPrior->pLimit + && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) + && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) + ){ + p->nSelectRow = sqlite3LogEst((u64)nLimit); + } + if( addr ){ + sqlite3VdbeJumpHere(v, addr); + } + break; + } + case TK_EXCEPT: + case TK_UNION: { + int unionTab; /* Cursor number of the temporary table holding result */ + u8 op = 0; /* One of the SRT_ operations to apply to self */ + int priorOp; /* The SRT_ operation to apply to prior selects */ + Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ + int addr; + SelectDest uniondest; + + testcase( p->op==TK_EXCEPT ); + testcase( p->op==TK_UNION ); + priorOp = SRT_Union; + if( dest.eDest==priorOp ){ + /* We can reuse a temporary table generated by a SELECT to our + ** right. + */ + assert( p->pLimit==0 ); /* Not allowed on leftward elements */ + assert( p->pOffset==0 ); /* Not allowed on leftward elements */ + unionTab = dest.iSDParm; + }else{ + /* We will need to create our own temporary table to hold the + ** intermediate results. + */ + unionTab = pParse->nTab++; + assert( p->pOrderBy==0 ); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); + assert( p->addrOpenEphm[0] == -1 ); + p->addrOpenEphm[0] = addr; + findRightmost(p)->selFlags |= SF_UsesEphemeral; + assert( p->pEList ); + } + + /* Code the SELECT statements to our left + */ + assert( !pPrior->pOrderBy ); + sqlite3SelectDestInit(&uniondest, priorOp, unionTab); + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &uniondest); + if( rc ){ + goto multi_select_end; + } + + /* Code the current SELECT statement + */ + if( p->op==TK_EXCEPT ){ + op = SRT_Except; + }else{ + assert( p->op==TK_UNION ); + op = SRT_Union; + } + p->pPrior = 0; + pLimit = p->pLimit; + p->pLimit = 0; + pOffset = p->pOffset; + p->pOffset = 0; + uniondest.eDest = op; + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &uniondest); + testcase( rc!=SQLITE_OK ); + /* Query flattening in sqlite3Select() might refill p->pOrderBy. + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ + sqlite3ExprListDelete(db, p->pOrderBy); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->pOrderBy = 0; + if( p->op==TK_UNION ){ + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); + } + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = pLimit; + p->pOffset = pOffset; + p->iLimit = 0; + p->iOffset = 0; + + /* Convert the data in the temporary table into whatever form + ** it is that we currently need. + */ + assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); + if( dest.eDest!=priorOp ){ + int iCont, iBreak, iStart; + assert( p->pEList ); + if( dest.eDest==SRT_Output ){ + Select *pFirst = p; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); + } + iBreak = sqlite3VdbeMakeLabel(v); + iCont = sqlite3VdbeMakeLabel(v); + computeLimitRegisters(pParse, p, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); + iStart = sqlite3VdbeCurrentAddr(v); + selectInnerLoop(pParse, p, p->pEList, unionTab, + 0, 0, &dest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); + sqlite3VdbeResolveLabel(v, iBreak); + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); + } + break; + } + default: assert( p->op==TK_INTERSECT ); { + int tab1, tab2; + int iCont, iBreak, iStart; + Expr *pLimit, *pOffset; + int addr; + SelectDest intersectdest; + int r1; + + /* INTERSECT is different from the others since it requires + ** two temporary tables. Hence it has its own case. Begin + ** by allocating the tables we will need. + */ + tab1 = pParse->nTab++; + tab2 = pParse->nTab++; + assert( p->pOrderBy==0 ); + + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); + assert( p->addrOpenEphm[0] == -1 ); + p->addrOpenEphm[0] = addr; + findRightmost(p)->selFlags |= SF_UsesEphemeral; + assert( p->pEList ); + + /* Code the SELECTs to our left into temporary table "tab1". + */ + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &intersectdest); + if( rc ){ + goto multi_select_end; + } + + /* Code the current SELECT into temporary table "tab2" + */ + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); + assert( p->addrOpenEphm[1] == -1 ); + p->addrOpenEphm[1] = addr; + p->pPrior = 0; + pLimit = p->pLimit; + p->pLimit = 0; + pOffset = p->pOffset; + p->pOffset = 0; + intersectdest.iSDParm = tab2; + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &intersectdest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = pLimit; + p->pOffset = pOffset; + + /* Generate code to take the intersection of the two temporary + ** tables. + */ + assert( p->pEList ); + if( dest.eDest==SRT_Output ){ + Select *pFirst = p; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); + } + iBreak = sqlite3VdbeMakeLabel(v); + iCont = sqlite3VdbeMakeLabel(v); + computeLimitRegisters(pParse, p, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); + r1 = sqlite3GetTempReg(pParse); + iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1); + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v); + sqlite3ReleaseTempReg(pParse, r1); + selectInnerLoop(pParse, p, p->pEList, tab1, + 0, 0, &dest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); + sqlite3VdbeResolveLabel(v, iBreak); + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); + break; + } + } + + explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL); + + /* Compute collating sequences used by + ** temporary tables needed to implement the compound select. + ** Attach the KeyInfo structure to all temporary tables. + ** + ** This section is run by the right-most SELECT statement only. + ** SELECT statements to the left always skip this part. The right-most + ** SELECT might also skip this part if it has no ORDER BY clause and + ** no temp tables are required. + */ + if( p->selFlags & SF_UsesEphemeral ){ + int i; /* Loop counter */ + KeyInfo *pKeyInfo; /* Collating sequence for the result set */ + Select *pLoop; /* For looping through SELECT statements */ + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ + int nCol; /* Number of columns in result set */ + + assert( p->pNext==0 ); + nCol = p->pEList->nExpr; + pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); + if( !pKeyInfo ){ + rc = SQLITE_NOMEM_BKPT; + goto multi_select_end; + } + for(i=0, apColl=pKeyInfo->aColl; ipDfltColl; + } + } + + for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ + for(i=0; i<2; i++){ + int addr = pLoop->addrOpenEphm[i]; + if( addr<0 ){ + /* If [0] is unused then [1] is also unused. So we can + ** always safely abort as soon as the first unused slot is found */ + assert( pLoop->addrOpenEphm[1]<0 ); + break; + } + sqlite3VdbeChangeP2(v, addr, nCol); + sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo), + P4_KEYINFO); + pLoop->addrOpenEphm[i] = -1; + } + } + sqlite3KeyInfoUnref(pKeyInfo); + } + +multi_select_end: + pDest->iSdst = dest.iSdst; + pDest->nSdst = dest.nSdst; + sqlite3SelectDelete(db, pDelete); + return rc; +} +#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + +/* +** Error message for when two or more terms of a compound select have different +** size result sets. +*/ +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){ + if( p->selFlags & SF_Values ){ + sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms"); + }else{ + sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" + " do not have the same number of result columns", selectOpName(p->op)); + } +} + +/* +** Code an output subroutine for a coroutine implementation of a +** SELECT statment. +** +** The data to be output is contained in pIn->iSdst. There are +** pIn->nSdst columns to be output. pDest is where the output should +** be sent. +** +** regReturn is the number of the register holding the subroutine +** return address. +** +** If regPrev>0 then it is the first register in a vector that +** records the previous output. mem[regPrev] is a flag that is false +** if there has been no previous output. If regPrev>0 then code is +** generated to suppress duplicates. pKeyInfo is used for comparing +** keys. +** +** If the LIMIT found in p->iLimit is reached, jump immediately to +** iBreak. +*/ +static int generateOutputSubroutine( + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement */ + SelectDest *pIn, /* Coroutine supplying data */ + SelectDest *pDest, /* Where to send the data */ + int regReturn, /* The return address register */ + int regPrev, /* Previous result register. No uniqueness if 0 */ + KeyInfo *pKeyInfo, /* For comparing with previous entry */ + int iBreak /* Jump here if we hit the LIMIT */ +){ + Vdbe *v = pParse->pVdbe; + int iContinue; + int addr; + + addr = sqlite3VdbeCurrentAddr(v); + iContinue = sqlite3VdbeMakeLabel(v); + + /* Suppress duplicates for UNION, EXCEPT, and INTERSECT + */ + if( regPrev ){ + int addr1, addr2; + addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v); + addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst, + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO); + sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1); + sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev); + } + if( pParse->db->mallocFailed ) return 0; + + /* Suppress the first OFFSET entries if there is an OFFSET clause + */ + codeOffset(v, p->iOffset, iContinue); + + assert( pDest->eDest!=SRT_Exists ); + assert( pDest->eDest!=SRT_Table ); + switch( pDest->eDest ){ + /* Store the result as data using a unique key. + */ + case SRT_EphemTab: { + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); + sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2); + sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3ReleaseTempReg(pParse, r2); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + +#ifndef SQLITE_OMIT_SUBQUERY + /* If we are creating a set for an "expr IN (SELECT ...)" construct, + ** then there should be a single item on the stack. Write this + ** item into the set table with bogus data. + */ + case SRT_Set: { + int r1; + assert( pIn->nSdst==1 || pParse->nErr>0 ); + pDest->affSdst = + sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst); + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1); + sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + + /* If this is a scalar select that is part of an expression, then + ** store the results in the appropriate memory cell and break out + ** of the scan loop. + */ + case SRT_Mem: { + assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 ); + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1); + /* The LIMIT clause will jump out of the loop for us */ + break; + } +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */ + + /* The results are stored in a sequence of registers + ** starting at pDest->iSdst. Then the co-routine yields. + */ + case SRT_Coroutine: { + if( pDest->iSdst==0 ){ + pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst); + pDest->nSdst = pIn->nSdst; + } + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst); + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); + break; + } + + /* If none of the above, then the result destination must be + ** SRT_Output. This routine is never called with any other + ** destination other than the ones handled above or SRT_Output. + ** + ** For SRT_Output, results are stored in a sequence of registers. + ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to + ** return the next row of result. + */ + default: { + assert( pDest->eDest==SRT_Output ); + sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst); + sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst); + break; + } + } + + /* Jump to the end of the loop if the LIMIT is reached. + */ + if( p->iLimit ){ + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); + } + + /* Generate the subroutine return + */ + sqlite3VdbeResolveLabel(v, iContinue); + sqlite3VdbeAddOp1(v, OP_Return, regReturn); + + return addr; +} + +/* +** Alternative compound select code generator for cases when there +** is an ORDER BY clause. +** +** We assume a query of the following form: +** +** ORDER BY +** +** is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea +** is to code both and with the ORDER BY clause as +** co-routines. Then run the co-routines in parallel and merge the results +** into the output. In addition to the two coroutines (called selectA and +** selectB) there are 7 subroutines: +** +** outA: Move the output of the selectA coroutine into the output +** of the compound query. +** +** outB: Move the output of the selectB coroutine into the output +** of the compound query. (Only generated for UNION and +** UNION ALL. EXCEPT and INSERTSECT never output a row that +** appears only in B.) +** +** AltB: Called when there is data from both coroutines and AB. +** +** EofA: Called when data is exhausted from selectA. +** +** EofB: Called when data is exhausted from selectB. +** +** The implementation of the latter five subroutines depend on which +** is used: +** +** +** UNION ALL UNION EXCEPT INTERSECT +** ------------- ----------------- -------------- ----------------- +** AltB: outA, nextA outA, nextA outA, nextA nextA +** +** AeqB: outA, nextA nextA nextA outA, nextA +** +** AgtB: outB, nextB outB, nextB nextB nextB +** +** EofA: outB, nextB outB, nextB halt halt +** +** EofB: outA, nextA outA, nextA outA, nextA halt +** +** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA +** causes an immediate jump to EofA and an EOF on B following nextB causes +** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or +** following nextX causes a jump to the end of the select processing. +** +** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled +** within the output subroutine. The regPrev register set holds the previously +** output value. A comparison is made against this value and the output +** is skipped if the next results would be the same as the previous. +** +** The implementation plan is to implement the two coroutines and seven +** subroutines first, then put the control logic at the bottom. Like this: +** +** goto Init +** coA: coroutine for left query (A) +** coB: coroutine for right query (B) +** outA: output one row of A +** outB: output one row of B (UNION and UNION ALL only) +** EofA: ... +** EofB: ... +** AltB: ... +** AeqB: ... +** AgtB: ... +** Init: initialize coroutine registers +** yield coA +** if eof(A) goto EofA +** yield coB +** if eof(B) goto EofB +** Cmpr: Compare A, B +** Jump AltB, AeqB, AgtB +** End: ... +** +** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not +** actually called using Gosub and they do not Return. EofA and EofB loop +** until all data is exhausted then jump to the "end" labe. AltB, AeqB, +** and AgtB jump to either L2 or to one of EofA or EofB. +*/ +#ifndef SQLITE_OMIT_COMPOUND_SELECT +static int multiSelectOrderBy( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + int i, j; /* Loop counters */ + Select *pPrior; /* Another SELECT immediately to our left */ + Vdbe *v; /* Generate code to this VDBE */ + SelectDest destA; /* Destination for coroutine A */ + SelectDest destB; /* Destination for coroutine B */ + int regAddrA; /* Address register for select-A coroutine */ + int regAddrB; /* Address register for select-B coroutine */ + int addrSelectA; /* Address of the select-A coroutine */ + int addrSelectB; /* Address of the select-B coroutine */ + int regOutA; /* Address register for the output-A subroutine */ + int regOutB; /* Address register for the output-B subroutine */ + int addrOutA; /* Address of the output-A subroutine */ + int addrOutB = 0; /* Address of the output-B subroutine */ + int addrEofA; /* Address of the select-A-exhausted subroutine */ + int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */ + int addrEofB; /* Address of the select-B-exhausted subroutine */ + int addrAltB; /* Address of the AB subroutine */ + int regLimitA; /* Limit register for select-A */ + int regLimitB; /* Limit register for select-A */ + int regPrev; /* A range of registers to hold previous output */ + int savedLimit; /* Saved value of p->iLimit */ + int savedOffset; /* Saved value of p->iOffset */ + int labelCmpr; /* Label for the start of the merge algorithm */ + int labelEnd; /* Label for the end of the overall SELECT stmt */ + int addr1; /* Jump instructions that get retargetted */ + int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */ + KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */ + KeyInfo *pKeyMerge; /* Comparison information for merging rows */ + sqlite3 *db; /* Database connection */ + ExprList *pOrderBy; /* The ORDER BY clause */ + int nOrderBy; /* Number of terms in the ORDER BY clause */ + int *aPermute; /* Mapping from ORDER BY terms to result set columns */ +#ifndef SQLITE_OMIT_EXPLAIN + int iSub1; /* EQP id of left-hand query */ + int iSub2; /* EQP id of right-hand query */ +#endif + + assert( p->pOrderBy!=0 ); + assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */ + db = pParse->db; + v = pParse->pVdbe; + assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */ + labelEnd = sqlite3VdbeMakeLabel(v); + labelCmpr = sqlite3VdbeMakeLabel(v); + + + /* Patch up the ORDER BY clause + */ + op = p->op; + pPrior = p->pPrior; + assert( pPrior->pOrderBy==0 ); + pOrderBy = p->pOrderBy; + assert( pOrderBy ); + nOrderBy = pOrderBy->nExpr; + + /* For operators other than UNION ALL we have to make sure that + ** the ORDER BY clause covers every term of the result set. Add + ** terms to the ORDER BY clause as necessary. + */ + if( op!=TK_ALL ){ + for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ + struct ExprList_item *pItem; + for(j=0, pItem=pOrderBy->a; ju.x.iOrderByCol>0 ); + if( pItem->u.x.iOrderByCol==i ) break; + } + if( j==nOrderBy ){ + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); + if( pNew==0 ) return SQLITE_NOMEM_BKPT; + pNew->flags |= EP_IntValue; + pNew->u.iValue = i; + pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); + if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; + } + } + } + + /* Compute the comparison permutation and keyinfo that is used with + ** the permutation used to determine if the next + ** row of results comes from selectA or selectB. Also add explicit + ** collations to the ORDER BY clause terms so that when the subqueries + ** to the right and the left are evaluated, they use the correct + ** collation. + */ + aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1)); + if( aPermute ){ + struct ExprList_item *pItem; + aPermute[0] = nOrderBy; + for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){ + assert( pItem->u.x.iOrderByCol>0 ); + assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); + aPermute[i] = pItem->u.x.iOrderByCol - 1; + } + pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); + }else{ + pKeyMerge = 0; + } + + /* Reattach the ORDER BY clause to the query. + */ + p->pOrderBy = pOrderBy; + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); + + /* Allocate a range of temporary registers and the KeyInfo needed + ** for the logic that removes duplicate result rows when the + ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). + */ + if( op==TK_ALL ){ + regPrev = 0; + }else{ + int nExpr = p->pEList->nExpr; + assert( nOrderBy>=nExpr || db->mallocFailed ); + regPrev = pParse->nMem+1; + pParse->nMem += nExpr+1; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev); + pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1); + if( pKeyDup ){ + assert( sqlite3KeyInfoIsWriteable(pKeyDup) ); + for(i=0; iaColl[i] = multiSelectCollSeq(pParse, p, i); + pKeyDup->aSortOrder[i] = 0; + } + } + } + + /* Separate the left and the right query from one another + */ + p->pPrior = 0; + pPrior->pNext = 0; + sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); + if( pPrior->pPrior==0 ){ + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); + } + + /* Compute the limit registers */ + computeLimitRegisters(pParse, p, labelEnd); + if( p->iLimit && op==TK_ALL ){ + regLimitA = ++pParse->nMem; + regLimitB = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit, + regLimitA); + sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB); + }else{ + regLimitA = regLimitB = 0; + } + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = 0; + sqlite3ExprDelete(db, p->pOffset); + p->pOffset = 0; + + regAddrA = ++pParse->nMem; + regAddrB = ++pParse->nMem; + regOutA = ++pParse->nMem; + regOutB = ++pParse->nMem; + sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA); + sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB); + + /* Generate a coroutine to evaluate the SELECT statement to the + ** left of the compound operator - the "A" select. + */ + addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); + VdbeComment((v, "left SELECT")); + pPrior->iLimit = regLimitA; + explainSetInteger(iSub1, pParse->iNextSelectId); + sqlite3Select(pParse, pPrior, &destA); + sqlite3VdbeEndCoroutine(v, regAddrA); + sqlite3VdbeJumpHere(v, addr1); + + /* Generate a coroutine to evaluate the SELECT statement on + ** the right - the "B" select + */ + addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB); + VdbeComment((v, "right SELECT")); + savedLimit = p->iLimit; + savedOffset = p->iOffset; + p->iLimit = regLimitB; + p->iOffset = 0; + explainSetInteger(iSub2, pParse->iNextSelectId); + sqlite3Select(pParse, p, &destB); + p->iLimit = savedLimit; + p->iOffset = savedOffset; + sqlite3VdbeEndCoroutine(v, regAddrB); + + /* Generate a subroutine that outputs the current row of the A + ** select as the next output row of the compound select. + */ + VdbeNoopComment((v, "Output routine for A")); + addrOutA = generateOutputSubroutine(pParse, + p, &destA, pDest, regOutA, + regPrev, pKeyDup, labelEnd); + + /* Generate a subroutine that outputs the current row of the B + ** select as the next output row of the compound select. + */ + if( op==TK_ALL || op==TK_UNION ){ + VdbeNoopComment((v, "Output routine for B")); + addrOutB = generateOutputSubroutine(pParse, + p, &destB, pDest, regOutB, + regPrev, pKeyDup, labelEnd); + } + sqlite3KeyInfoUnref(pKeyDup); + + /* Generate a subroutine to run when the results from select A + ** are exhausted and only data in select B remains. + */ + if( op==TK_EXCEPT || op==TK_INTERSECT ){ + addrEofA_noB = addrEofA = labelEnd; + }else{ + VdbeNoopComment((v, "eof-A subroutine")); + addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); + addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); + VdbeCoverage(v); + sqlite3VdbeGoto(v, addrEofA); + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); + } + + /* Generate a subroutine to run when the results from select B + ** are exhausted and only data in select A remains. + */ + if( op==TK_INTERSECT ){ + addrEofB = addrEofA; + if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; + }else{ + VdbeNoopComment((v, "eof-B subroutine")); + addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v); + sqlite3VdbeGoto(v, addrEofB); + } + + /* Generate code to handle the case of AB + */ + VdbeNoopComment((v, "A-gt-B subroutine")); + addrAgtB = sqlite3VdbeCurrentAddr(v); + if( op==TK_ALL || op==TK_UNION ){ + sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); + } + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); + sqlite3VdbeGoto(v, labelCmpr); + + /* This code runs once to initialize everything. + */ + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); + + /* Implement the main merge loop + */ + sqlite3VdbeResolveLabel(v, labelCmpr); + sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY); + sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy, + (char*)pKeyMerge, P4_KEYINFO); + sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE); + sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v); + + /* Jump to the this point in order to terminate the query. + */ + sqlite3VdbeResolveLabel(v, labelEnd); + + /* Set the number of output columns + */ + if( pDest->eDest==SRT_Output ){ + Select *pFirst = pPrior; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); + } + + /* Reassembly the compound query so that it will be freed correctly + ** by the calling function */ + if( p->pPrior ){ + sqlite3SelectDelete(db, p->pPrior); + } + p->pPrior = pPrior; + pPrior->pNext = p; + + /*** TBD: Insert subroutine calls to close cursors on incomplete + **** subqueries ****/ + explainComposite(pParse, p->op, iSub1, iSub2, 0); + return pParse->nErr!=0; +} +#endif + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* Forward Declarations */ +static void substExprList(sqlite3*, ExprList*, int, ExprList*); +static void substSelect(sqlite3*, Select *, int, ExprList*, int); + +/* +** Scan through the expression pExpr. Replace every reference to +** a column in table number iTable with a copy of the iColumn-th +** entry in pEList. (But leave references to the ROWID column +** unchanged.) +** +** This routine is part of the flattening procedure. A subquery +** whose result set is defined by pEList appears as entry in the +** FROM clause of a SELECT such that the VDBE cursor assigned to that +** FORM clause entry is iTable. This routine make the necessary +** changes to pExpr so that it refers directly to the source table +** of the subquery rather the result set of the subquery. +*/ +static Expr *substExpr( + sqlite3 *db, /* Report malloc errors to this connection */ + Expr *pExpr, /* Expr in which substitution occurs */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute expressions */ +){ + if( pExpr==0 ) return 0; + if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ + if( pExpr->iColumn<0 ){ + pExpr->op = TK_NULL; + }else{ + Expr *pNew; + assert( pEList!=0 && pExpr->iColumnnExpr ); + assert( pExpr->pLeft==0 && pExpr->pRight==0 ); + pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0); + sqlite3ExprDelete(db, pExpr); + pExpr = pNew; + } + }else{ + pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList); + pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + substSelect(db, pExpr->x.pSelect, iTable, pEList, 1); + }else{ + substExprList(db, pExpr->x.pList, iTable, pEList); + } + } + return pExpr; +} +static void substExprList( + sqlite3 *db, /* Report malloc errors here */ + ExprList *pList, /* List to scan and in which to make substitutes */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute values */ +){ + int i; + if( pList==0 ) return; + for(i=0; inExpr; i++){ + pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList); + } +} +static void substSelect( + sqlite3 *db, /* Report malloc errors here */ + Select *p, /* SELECT statement in which to make substitutions */ + int iTable, /* Table to be replaced */ + ExprList *pEList, /* Substitute values */ + int doPrior /* Do substitutes on p->pPrior too */ +){ + SrcList *pSrc; + struct SrcList_item *pItem; + int i; + if( !p ) return; + do{ + substExprList(db, p->pEList, iTable, pEList); + substExprList(db, p->pGroupBy, iTable, pEList); + substExprList(db, p->pOrderBy, iTable, pEList); + p->pHaving = substExpr(db, p->pHaving, iTable, pEList); + p->pWhere = substExpr(db, p->pWhere, iTable, pEList); + pSrc = p->pSrc; + assert( pSrc!=0 ); + for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ + substSelect(db, pItem->pSelect, iTable, pEList, 1); + if( pItem->fg.isTabFunc ){ + substExprList(db, pItem->u1.pFuncArg, iTable, pEList); + } + } + }while( doPrior && (p = p->pPrior)!=0 ); +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** This routine attempts to flatten subqueries as a performance optimization. +** This routine returns 1 if it makes changes and 0 if no flattening occurs. +** +** To understand the concept of flattening, consider the following +** query: +** +** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 +** +** The default way of implementing this query is to execute the +** subquery first and store the results in a temporary table, then +** run the outer query on that temporary table. This requires two +** passes over the data. Furthermore, because the temporary table +** has no indices, the WHERE clause on the outer query cannot be +** optimized. +** +** This routine attempts to rewrite queries such as the above into +** a single flat select, like this: +** +** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 +** +** The code generated for this simplification gives the same result +** but only has to scan the data once. And because indices might +** exist on the table t1, a complete scan of the data might be +** avoided. +** +** Flattening is only attempted if all of the following are true: +** +** (1) The subquery and the outer query do not both use aggregates. +** +** (2) The subquery is not an aggregate or (2a) the outer query is not a join +** and (2b) the outer query does not use subqueries other than the one +** FROM-clause subquery that is a candidate for flattening. (2b is +** due to ticket [2f7170d73bf9abf80] from 2015-02-09.) +** +** (3) The subquery is not the right operand of a left outer join +** (Originally ticket #306. Strengthened by ticket #3300) +** +** (4) The subquery is not DISTINCT. +** +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT +** sub-queries that were excluded from this optimization. Restriction +** (4) has since been expanded to exclude all DISTINCT subqueries. +** +** (6) The subquery does not use aggregates or the outer query is not +** DISTINCT. +** +** (7) The subquery has a FROM clause. TODO: For subqueries without +** A FROM clause, consider adding a FROM close with the special +** table sqlite_once that consists of a single row containing a +** single NULL. +** +** (8) The subquery does not use LIMIT or the outer query is not a join. +** +** (9) The subquery does not use LIMIT or the outer query does not use +** aggregates. +** +** (**) Restriction (10) was removed from the code on 2005-02-05 but we +** accidently carried the comment forward until 2014-09-15. Original +** text: "The subquery does not use aggregates or the outer query +** does not use LIMIT." +** +** (11) The subquery and the outer query do not both have ORDER BY clauses. +** +** (**) Not implemented. Subsumed into restriction (3). Was previously +** a separate restriction deriving from ticket #350. +** +** (13) The subquery and outer query do not both use LIMIT. +** +** (14) The subquery does not use OFFSET. +** +** (15) The outer query is not part of a compound select or the +** subquery does not have a LIMIT clause. +** (See ticket #2339 and ticket [02a8e81d44]). +** +** (16) The outer query is not an aggregate or the subquery does +** not contain ORDER BY. (Ticket #2942) This used to not matter +** until we introduced the group_concat() function. +** +** (17) The sub-query is not a compound select, or it is a UNION ALL +** compound clause made up entirely of non-aggregate queries, and +** the parent query: +** +** * is not itself part of a compound select, +** * is not an aggregate or DISTINCT query, and +** * is not a join +** +** The parent and sub-query may contain WHERE clauses. Subject to +** rules (11), (13) and (14), they may also contain ORDER BY, +** LIMIT and OFFSET clauses. The subquery cannot use any compound +** operator other than UNION ALL because all the other compound +** operators have an implied DISTINCT which is disallowed by +** restriction (4). +** +** Also, each component of the sub-query must return the same number +** of result columns. This is actually a requirement for any compound +** SELECT statement, but all the code here does is make sure that no +** such (illegal) sub-query is flattened. The caller will detect the +** syntax error and return a detailed message. +** +** (18) If the sub-query is a compound select, then all terms of the +** ORDER by clause of the parent must be simple references to +** columns of the sub-query. +** +** (19) The subquery does not use LIMIT or the outer query does not +** have a WHERE clause. +** +** (20) If the sub-query is a compound select, then it must not use +** an ORDER BY clause. Ticket #3773. We could relax this constraint +** somewhat by saying that the terms of the ORDER BY clause must +** appear as unmodified result columns in the outer query. But we +** have other optimizations in mind to deal with that case. +** +** (21) The subquery does not use LIMIT or the outer query is not +** DISTINCT. (See ticket [752e1646fc]). +** +** (22) The subquery is not a recursive CTE. +** +** (23) The parent is not a recursive CTE, or the sub-query is not a +** compound query. This restriction is because transforming the +** parent to a compound query confuses the code that handles +** recursive queries in multiSelect(). +** +** (24) The subquery is not an aggregate that uses the built-in min() or +** or max() functions. (Without this restriction, a query like: +** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily +** return the value X for which Y was maximal.) +** +** +** In this routine, the "p" parameter is a pointer to the outer query. +** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query +** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. +** +** If flattening is not attempted, this routine is a no-op and returns 0. +** If flattening is attempted this routine returns 1. +** +** All of the expression analysis must occur on both the outer query and +** the subquery before this routine runs. +*/ +static int flattenSubquery( + Parse *pParse, /* Parsing context */ + Select *p, /* The parent or outer SELECT statement */ + int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ + int isAgg, /* True if outer SELECT uses aggregate functions */ + int subqueryIsAgg /* True if the subquery uses aggregate functions */ +){ + const char *zSavedAuthContext = pParse->zAuthContext; + Select *pParent; /* Current UNION ALL term of the other query */ + Select *pSub; /* The inner query or "subquery" */ + Select *pSub1; /* Pointer to the rightmost select in sub-query */ + SrcList *pSrc; /* The FROM clause of the outer query */ + SrcList *pSubSrc; /* The FROM clause of the subquery */ + ExprList *pList; /* The result set of the outer query */ + int iParent; /* VDBE cursor number of the pSub result set temp table */ + int i; /* Loop counter */ + Expr *pWhere; /* The WHERE clause */ + struct SrcList_item *pSubitem; /* The subquery */ + sqlite3 *db = pParse->db; + + /* Check to see if flattening is permitted. Return 0 if not. + */ + assert( p!=0 ); + assert( p->pPrior==0 ); /* Unable to flatten compound queries */ + if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0; + pSrc = p->pSrc; + assert( pSrc && iFrom>=0 && iFromnSrc ); + pSubitem = &pSrc->a[iFrom]; + iParent = pSubitem->iCursor; + pSub = pSubitem->pSelect; + assert( pSub!=0 ); + if( subqueryIsAgg ){ + if( isAgg ) return 0; /* Restriction (1) */ + if( pSrc->nSrc>1 ) return 0; /* Restriction (2a) */ + if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery)) + || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0 + || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0 + ){ + return 0; /* Restriction (2b) */ + } + } + + pSubSrc = pSub->pSrc; + assert( pSubSrc ); + /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, + ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET + ** because they could be computed at compile-time. But when LIMIT and OFFSET + ** became arbitrary expressions, we were forced to add restrictions (13) + ** and (14). */ + if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ + if( pSub->pOffset ) return 0; /* Restriction (14) */ + if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){ + return 0; /* Restriction (15) */ + } + if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */ + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ + return 0; /* Restrictions (8)(9) */ + } + if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ + return 0; /* Restriction (6) */ + } + if( p->pOrderBy && pSub->pOrderBy ){ + return 0; /* Restriction (11) */ + } + if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */ + if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */ + if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){ + return 0; /* Restriction (21) */ + } + testcase( pSub->selFlags & SF_Recursive ); + testcase( pSub->selFlags & SF_MinMaxAgg ); + if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){ + return 0; /* Restrictions (22) and (24) */ + } + if( (p->selFlags & SF_Recursive) && pSub->pPrior ){ + return 0; /* Restriction (23) */ + } + + /* OBSOLETE COMMENT 1: + ** Restriction 3: If the subquery is a join, make sure the subquery is + ** not used as the right operand of an outer join. Examples of why this + ** is not allowed: + ** + ** t1 LEFT OUTER JOIN (t2 JOIN t3) + ** + ** If we flatten the above, we would get + ** + ** (t1 LEFT OUTER JOIN t2) JOIN t3 + ** + ** which is not at all the same thing. + ** + ** OBSOLETE COMMENT 2: + ** Restriction 12: If the subquery is the right operand of a left outer + ** join, make sure the subquery has no WHERE clause. + ** An examples of why this is not allowed: + ** + ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) + ** + ** If we flatten the above, we would get + ** + ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 + ** + ** But the t2.x>0 test will always fail on a NULL row of t2, which + ** effectively converts the OUTER JOIN into an INNER JOIN. + ** + ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE: + ** Ticket #3300 shows that flattening the right term of a LEFT JOIN + ** is fraught with danger. Best to avoid the whole thing. If the + ** subquery is the right term of a LEFT JOIN, then do not flatten. + */ + if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){ + return 0; + } + + /* Restriction 17: If the sub-query is a compound SELECT, then it must + ** use only the UNION ALL operator. And none of the simple select queries + ** that make up the compound SELECT are allowed to be aggregate or distinct + ** queries. + */ + if( pSub->pPrior ){ + if( pSub->pOrderBy ){ + return 0; /* Restriction 20 */ + } + if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){ + return 0; + } + for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); + assert( pSub->pSrc!=0 ); + assert( pSub->pEList->nExpr==pSub1->pEList->nExpr ); + if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 + || (pSub1->pPrior && pSub1->op!=TK_ALL) + || pSub1->pSrc->nSrc<1 + ){ + return 0; + } + testcase( pSub1->pSrc->nSrc>1 ); + } + + /* Restriction 18. */ + if( p->pOrderBy ){ + int ii; + for(ii=0; iipOrderBy->nExpr; ii++){ + if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; + } + } + } + + /***** If we reach this point, flattening is permitted. *****/ + SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n", + pSub->zSelName, pSub, iFrom)); + + /* Authorize the subquery */ + pParse->zAuthContext = pSubitem->zName; + TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); + testcase( i==SQLITE_DENY ); + pParse->zAuthContext = zSavedAuthContext; + + /* If the sub-query is a compound SELECT statement, then (by restrictions + ** 17 and 18 above) it must be a UNION ALL and the parent query must + ** be of the form: + ** + ** SELECT FROM () + ** + ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block + ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or + ** OFFSET clauses and joins them to the left-hand-side of the original + ** using UNION ALL operators. In this case N is the number of simple + ** select statements in the compound sub-query. + ** + ** Example: + ** + ** SELECT a+1 FROM ( + ** SELECT x FROM tab + ** UNION ALL + ** SELECT y FROM tab + ** UNION ALL + ** SELECT abs(z*2) FROM tab2 + ** ) WHERE a!=5 ORDER BY 1 + ** + ** Transformed into: + ** + ** SELECT x+1 FROM tab WHERE x+1!=5 + ** UNION ALL + ** SELECT y+1 FROM tab WHERE y+1!=5 + ** UNION ALL + ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5 + ** ORDER BY 1 + ** + ** We call this the "compound-subquery flattening". + */ + for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){ + Select *pNew; + ExprList *pOrderBy = p->pOrderBy; + Expr *pLimit = p->pLimit; + Expr *pOffset = p->pOffset; + Select *pPrior = p->pPrior; + p->pOrderBy = 0; + p->pSrc = 0; + p->pPrior = 0; + p->pLimit = 0; + p->pOffset = 0; + pNew = sqlite3SelectDup(db, p, 0); + sqlite3SelectSetName(pNew, pSub->zSelName); + p->pOffset = pOffset; + p->pLimit = pLimit; + p->pOrderBy = pOrderBy; + p->pSrc = pSrc; + p->op = TK_ALL; + if( pNew==0 ){ + p->pPrior = pPrior; + }else{ + pNew->pPrior = pPrior; + if( pPrior ) pPrior->pNext = pNew; + pNew->pNext = p; + p->pPrior = pNew; + SELECTTRACE(2,pParse,p, + ("compound-subquery flattener creates %s.%p as peer\n", + pNew->zSelName, pNew)); + } + if( db->mallocFailed ) return 1; + } + + /* Begin flattening the iFrom-th entry of the FROM clause + ** in the outer query. + */ + pSub = pSub1 = pSubitem->pSelect; + + /* Delete the transient table structure associated with the + ** subquery + */ + sqlite3DbFree(db, pSubitem->zDatabase); + sqlite3DbFree(db, pSubitem->zName); + sqlite3DbFree(db, pSubitem->zAlias); + pSubitem->zDatabase = 0; + pSubitem->zName = 0; + pSubitem->zAlias = 0; + pSubitem->pSelect = 0; + + /* Defer deleting the Table object associated with the + ** subquery until code generation is + ** complete, since there may still exist Expr.pTab entries that + ** refer to the subquery even after flattening. Ticket #3346. + ** + ** pSubitem->pTab is always non-NULL by test restrictions and tests above. + */ + if( ALWAYS(pSubitem->pTab!=0) ){ + Table *pTabToDel = pSubitem->pTab; + if( pTabToDel->nRef==1 ){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pTabToDel->pNextZombie = pToplevel->pZombieTab; + pToplevel->pZombieTab = pTabToDel; + }else{ + pTabToDel->nRef--; + } + pSubitem->pTab = 0; + } + + /* The following loop runs once for each term in a compound-subquery + ** flattening (as described above). If we are doing a different kind + ** of flattening - a flattening other than a compound-subquery flattening - + ** then this loop only runs once. + ** + ** This loop moves all of the FROM elements of the subquery into the + ** the FROM clause of the outer query. Before doing this, remember + ** the cursor number for the original outer query FROM element in + ** iParent. The iParent cursor will never be used. Subsequent code + ** will scan expressions looking for iParent references and replace + ** those references with expressions that resolve to the subquery FROM + ** elements we are now copying in. + */ + for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ + int nSubSrc; + u8 jointype = 0; + pSubSrc = pSub->pSrc; /* FROM clause of subquery */ + nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */ + pSrc = pParent->pSrc; /* FROM clause of the outer query */ + + if( pSrc ){ + assert( pParent==p ); /* First time through the loop */ + jointype = pSubitem->fg.jointype; + }else{ + assert( pParent!=p ); /* 2nd and subsequent times through the loop */ + pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + if( pSrc==0 ){ + assert( db->mallocFailed ); + break; + } + } + + /* The subquery uses a single slot of the FROM clause of the outer + ** query. If the subquery has more than one element in its FROM clause, + ** then expand the outer query to make space for it to hold all elements + ** of the subquery. + ** + ** Example: + ** + ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB; + ** + ** The outer query has 3 slots in its FROM clause. One slot of the + ** outer query (the middle slot) is used by the subquery. The next + ** block of code will expand the outer query FROM clause to 4 slots. + ** The middle slot is expanded to two slots in order to make space + ** for the two elements in the FROM clause of the subquery. + */ + if( nSubSrc>1 ){ + pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1); + if( db->mallocFailed ){ + break; + } + } + + /* Transfer the FROM clause terms from the subquery into the + ** outer query. + */ + for(i=0; ia[i+iFrom].pUsing); + assert( pSrc->a[i+iFrom].fg.isTabFunc==0 ); + pSrc->a[i+iFrom] = pSubSrc->a[i]; + memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); + } + pSrc->a[iFrom].fg.jointype = jointype; + + /* Now begin substituting subquery result set expressions for + ** references to the iParent in the outer query. + ** + ** Example: + ** + ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; + ** \ \_____________ subquery __________/ / + ** \_____________________ outer query ______________________________/ + ** + ** We look at every expression in the outer query and every place we see + ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". + */ + pList = pParent->pEList; + for(i=0; inExpr; i++){ + if( pList->a[i].zName==0 ){ + char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan); + sqlite3Dequote(zName); + pList->a[i].zName = zName; + } + } + if( pSub->pOrderBy ){ + /* At this point, any non-zero iOrderByCol values indicate that the + ** ORDER BY column expression is identical to the iOrderByCol'th + ** expression returned by SELECT statement pSub. Since these values + ** do not necessarily correspond to columns in SELECT statement pParent, + ** zero them before transfering the ORDER BY clause. + ** + ** Not doing this may cause an error if a subsequent call to this + ** function attempts to flatten a compound sub-query into pParent + ** (the only way this can happen is if the compound sub-query is + ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */ + ExprList *pOrderBy = pSub->pOrderBy; + for(i=0; inExpr; i++){ + pOrderBy->a[i].u.x.iOrderByCol = 0; + } + assert( pParent->pOrderBy==0 ); + assert( pSub->pPrior==0 ); + pParent->pOrderBy = pOrderBy; + pSub->pOrderBy = 0; + } + pWhere = sqlite3ExprDup(db, pSub->pWhere, 0); + if( subqueryIsAgg ){ + assert( pParent->pHaving==0 ); + pParent->pHaving = pParent->pWhere; + pParent->pWhere = pWhere; + pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, + sqlite3ExprDup(db, pSub->pHaving, 0)); + assert( pParent->pGroupBy==0 ); + pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0); + }else{ + pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere); + } + substSelect(db, pParent, iParent, pSub->pEList, 0); + + /* The flattened query is distinct if either the inner or the + ** outer query is distinct. + */ + pParent->selFlags |= pSub->selFlags & SF_Distinct; + + /* + ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; + ** + ** One is tempted to try to add a and b to combine the limits. But this + ** does not work if either limit is negative. + */ + if( pSub->pLimit ){ + pParent->pLimit = pSub->pLimit; + pSub->pLimit = 0; + } + } + + /* Finially, delete what is left of the subquery and return + ** success. + */ + sqlite3SelectDelete(db, pSub1); + +#if SELECTTRACE_ENABLED + if( sqlite3SelectTrace & 0x100 ){ + SELECTTRACE(0x100,pParse,p,("After flattening:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + + return 1; +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + + + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** Make copies of relevant WHERE clause terms of the outer query into +** the WHERE clause of subquery. Example: +** +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; +** +** Transformed into: +** +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) +** WHERE x=5 AND y=10; +** +** The hope is that the terms added to the inner query will make it more +** efficient. +** +** Do not attempt this optimization if: +** +** (1) The inner query is an aggregate. (In that case, we'd really want +** to copy the outer WHERE-clause terms onto the HAVING clause of the +** inner query. But they probably won't help there so do not bother.) +** +** (2) The inner query is the recursive part of a common table expression. +** +** (3) The inner query has a LIMIT clause (since the changes to the WHERE +** close would change the meaning of the LIMIT). +** +** (4) The inner query is the right operand of a LEFT JOIN. (The caller +** enforces this restriction since this routine does not have enough +** information to know.) +** +** (5) The WHERE clause expression originates in the ON or USING clause +** of a LEFT JOIN. +** +** Return 0 if no changes are made and non-zero if one or more WHERE clause +** terms are duplicated into the subquery. +*/ +static int pushDownWhereTerms( + sqlite3 *db, /* The database connection (for malloc()) */ + Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ + Expr *pWhere, /* The WHERE clause of the outer query */ + int iCursor /* Cursor number of the subquery */ +){ + Expr *pNew; + int nChng = 0; + Select *pX; /* For looping over compound SELECTs in pSubq */ + if( pWhere==0 ) return 0; + for(pX=pSubq; pX; pX=pX->pPrior){ + if( (pX->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){ + testcase( pX->selFlags & SF_Aggregate ); + testcase( pX->selFlags & SF_Recursive ); + testcase( pX!=pSubq ); + return 0; /* restrictions (1) and (2) */ + } + } + if( pSubq->pLimit!=0 ){ + return 0; /* restriction (3) */ + } + while( pWhere->op==TK_AND ){ + nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor); + pWhere = pWhere->pLeft; + } + if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */ + if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ + nChng++; + while( pSubq ){ + pNew = sqlite3ExprDup(db, pWhere, 0); + pNew = substExpr(db, pNew, iCursor, pSubq->pEList); + pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew); + pSubq = pSubq->pPrior; + } + } + return nChng; +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +/* +** Based on the contents of the AggInfo structure indicated by the first +** argument, this function checks if the following are true: +** +** * the query contains just a single aggregate function, +** * the aggregate function is either min() or max(), and +** * the argument to the aggregate function is a column value. +** +** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX +** is returned as appropriate. Also, *ppMinMax is set to point to the +** list of arguments passed to the aggregate before returning. +** +** Or, if the conditions above are not met, *ppMinMax is set to 0 and +** WHERE_ORDERBY_NORMAL is returned. +*/ +static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){ + int eRet = WHERE_ORDERBY_NORMAL; /* Return value */ + + *ppMinMax = 0; + if( pAggInfo->nFunc==1 ){ + Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */ + ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */ + + assert( pExpr->op==TK_AGG_FUNCTION ); + if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){ + const char *zFunc = pExpr->u.zToken; + if( sqlite3StrICmp(zFunc, "min")==0 ){ + eRet = WHERE_ORDERBY_MIN; + *ppMinMax = pEList; + }else if( sqlite3StrICmp(zFunc, "max")==0 ){ + eRet = WHERE_ORDERBY_MAX; + *ppMinMax = pEList; + } + } + } + + assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 ); + return eRet; +} + +/* +** The select statement passed as the first argument is an aggregate query. +** The second argument is the associated aggregate-info object. This +** function tests if the SELECT is of the form: +** +** SELECT count(*) FROM +** +** where table is a database table, not a sub-select or view. If the query +** does match this pattern, then a pointer to the Table object representing +** is returned. Otherwise, 0 is returned. +*/ +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ + Table *pTab; + Expr *pExpr; + + assert( !p->pGroupBy ); + + if( p->pWhere || p->pEList->nExpr!=1 + || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect + ){ + return 0; + } + pTab = p->pSrc->a[0].pTab; + pExpr = p->pEList->a[0].pExpr; + assert( pTab && !pTab->pSelect && pExpr ); + + if( IsVirtual(pTab) ) return 0; + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; + if( NEVER(pAggInfo->nFunc==0) ) return 0; + if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; + if( pExpr->flags&EP_Distinct ) return 0; + + return pTab; +} + +/* +** If the source-list item passed as an argument was augmented with an +** INDEXED BY clause, then try to locate the specified index. If there +** was such a clause and the named index cannot be found, return +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate +** pFrom->pIndex and return SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){ + if( pFrom->pTab && pFrom->fg.isIndexedBy ){ + Table *pTab = pFrom->pTab; + char *zIndexedBy = pFrom->u1.zIndexedBy; + Index *pIdx; + for(pIdx=pTab->pIndex; + pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); + pIdx=pIdx->pNext + ); + if( !pIdx ){ + sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0); + pParse->checkSchema = 1; + return SQLITE_ERROR; + } + pFrom->pIBIndex = pIdx; + } + return SQLITE_OK; +} +/* +** Detect compound SELECT statements that use an ORDER BY clause with +** an alternative collating sequence. +** +** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ... +** +** These are rewritten as a subquery: +** +** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2) +** ORDER BY ... COLLATE ... +** +** This transformation is necessary because the multiSelectOrderBy() routine +** above that generates the code for a compound SELECT with an ORDER BY clause +** uses a merge algorithm that requires the same collating sequence on the +** result columns as on the ORDER BY clause. See ticket +** http://www.sqlite.org/src/info/6709574d2a +** +** This transformation is only needed for EXCEPT, INTERSECT, and UNION. +** The UNION ALL operator works fine with multiSelectOrderBy() even when +** there are COLLATE terms in the ORDER BY. +*/ +static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ + int i; + Select *pNew; + Select *pX; + sqlite3 *db; + struct ExprList_item *a; + SrcList *pNewSrc; + Parse *pParse; + Token dummy; + + if( p->pPrior==0 ) return WRC_Continue; + if( p->pOrderBy==0 ) return WRC_Continue; + for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){} + if( pX==0 ) return WRC_Continue; + a = p->pOrderBy->a; + for(i=p->pOrderBy->nExpr-1; i>=0; i--){ + if( a[i].pExpr->flags & EP_Collate ) break; + } + if( i<0 ) return WRC_Continue; + + /* If we reach this point, that means the transformation is required. */ + + pParse = pWalker->pParse; + db = pParse->db; + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); + if( pNew==0 ) return WRC_Abort; + memset(&dummy, 0, sizeof(dummy)); + pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0); + if( pNewSrc==0 ) return WRC_Abort; + *pNew = *p; + p->pSrc = pNewSrc; + p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0)); + p->op = TK_SELECT; + p->pWhere = 0; + pNew->pGroupBy = 0; + pNew->pHaving = 0; + pNew->pOrderBy = 0; + p->pPrior = 0; + p->pNext = 0; + p->pWith = 0; + p->selFlags &= ~SF_Compound; + assert( (p->selFlags & SF_Converted)==0 ); + p->selFlags |= SF_Converted; + assert( pNew->pPrior!=0 ); + pNew->pPrior->pNext = pNew; + pNew->pLimit = 0; + pNew->pOffset = 0; + return WRC_Continue; +} + +/* +** Check to see if the FROM clause term pFrom has table-valued function +** arguments. If it does, leave an error message in pParse and return +** non-zero, since pFrom is not allowed to be a table-valued function. +*/ +static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){ + if( pFrom->fg.isTabFunc ){ + sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName); + return 1; + } + return 0; +} + +#ifndef SQLITE_OMIT_CTE +/* +** Argument pWith (which may be NULL) points to a linked list of nested +** WITH contexts, from inner to outermost. If the table identified by +** FROM clause element pItem is really a common-table-expression (CTE) +** then return a pointer to the CTE definition for that table. Otherwise +** return NULL. +** +** If a non-NULL value is returned, set *ppContext to point to the With +** object that the returned CTE belongs to. +*/ +static struct Cte *searchWith( + With *pWith, /* Current innermost WITH clause */ + struct SrcList_item *pItem, /* FROM clause element to resolve */ + With **ppContext /* OUT: WITH clause return value belongs to */ +){ + const char *zName; + if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){ + With *p; + for(p=pWith; p; p=p->pOuter){ + int i; + for(i=0; inCte; i++){ + if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){ + *ppContext = p; + return &p->a[i]; + } + } + } + } + return 0; +} + +/* The code generator maintains a stack of active WITH clauses +** with the inner-most WITH clause being at the top of the stack. +** +** This routine pushes the WITH clause passed as the second argument +** onto the top of the stack. If argument bFree is true, then this +** WITH clause will never be popped from the stack. In this case it +** should be freed along with the Parse object. In other cases, when +** bFree==0, the With object will be freed along with the SELECT +** statement with which it is associated. +*/ +SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){ + assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) ); + if( pWith ){ + assert( pParse->pWith!=pWith ); + pWith->pOuter = pParse->pWith; + pParse->pWith = pWith; + if( bFree ) pParse->pWithToFree = pWith; + } +} + +/* +** This function checks if argument pFrom refers to a CTE declared by +** a WITH clause on the stack currently maintained by the parser. And, +** if currently processing a CTE expression, if it is a recursive +** reference to the current CTE. +** +** If pFrom falls into either of the two categories above, pFrom->pTab +** and other fields are populated accordingly. The caller should check +** (pFrom->pTab!=0) to determine whether or not a successful match +** was found. +** +** Whether or not a match is found, SQLITE_OK is returned if no error +** occurs. If an error does occur, an error message is stored in the +** parser and some error code other than SQLITE_OK returned. +*/ +static int withExpand( + Walker *pWalker, + struct SrcList_item *pFrom +){ + Parse *pParse = pWalker->pParse; + sqlite3 *db = pParse->db; + struct Cte *pCte; /* Matched CTE (or NULL if no match) */ + With *pWith; /* WITH clause that pCte belongs to */ + + assert( pFrom->pTab==0 ); + + pCte = searchWith(pParse->pWith, pFrom, &pWith); + if( pCte ){ + Table *pTab; + ExprList *pEList; + Select *pSel; + Select *pLeft; /* Left-most SELECT statement */ + int bMayRecursive; /* True if compound joined by UNION [ALL] */ + With *pSavedWith; /* Initial value of pParse->pWith */ + + /* If pCte->zCteErr is non-NULL at this point, then this is an illegal + ** recursive reference to CTE pCte. Leave an error in pParse and return + ** early. If pCte->zCteErr is NULL, then this is not a recursive reference. + ** In this case, proceed. */ + if( pCte->zCteErr ){ + sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); + return SQLITE_ERROR; + } + if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR; + + assert( pFrom->pTab==0 ); + pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTab==0 ) return WRC_Abort; + pTab->nRef = 1; + pTab->zName = sqlite3DbStrDup(db, pCte->zName); + pTab->iPKey = -1; + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); + pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; + pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); + if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; + assert( pFrom->pSelect ); + + /* Check if this is a recursive CTE. */ + pSel = pFrom->pSelect; + bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION ); + if( bMayRecursive ){ + int i; + SrcList *pSrc = pFrom->pSelect->pSrc; + for(i=0; inSrc; i++){ + struct SrcList_item *pItem = &pSrc->a[i]; + if( pItem->zDatabase==0 + && pItem->zName!=0 + && 0==sqlite3StrICmp(pItem->zName, pCte->zName) + ){ + pItem->pTab = pTab; + pItem->fg.isRecursive = 1; + pTab->nRef++; + pSel->selFlags |= SF_Recursive; + } + } + } + + /* Only one recursive reference is permitted. */ + if( pTab->nRef>2 ){ + sqlite3ErrorMsg( + pParse, "multiple references to recursive table: %s", pCte->zName + ); + return SQLITE_ERROR; + } + assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 )); + + pCte->zCteErr = "circular reference: %s"; + pSavedWith = pParse->pWith; + pParse->pWith = pWith; + sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); + pParse->pWith = pWith; + + for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); + pEList = pLeft->pEList; + if( pCte->pCols ){ + if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ + sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", + pCte->zName, pEList->nExpr, pCte->pCols->nExpr + ); + pParse->pWith = pSavedWith; + return SQLITE_ERROR; + } + pEList = pCte->pCols; + } + + sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol); + if( bMayRecursive ){ + if( pSel->selFlags & SF_Recursive ){ + pCte->zCteErr = "multiple recursive references: %s"; + }else{ + pCte->zCteErr = "recursive reference in a subquery: %s"; + } + sqlite3WalkSelect(pWalker, pSel); + } + pCte->zCteErr = 0; + pParse->pWith = pSavedWith; + } + + return SQLITE_OK; +} +#endif + +#ifndef SQLITE_OMIT_CTE +/* +** If the SELECT passed as the second argument has an associated WITH +** clause, pop it from the stack stored as part of the Parse object. +** +** This function is used as the xSelectCallback2() callback by +** sqlite3SelectExpand() when walking a SELECT tree to resolve table +** names and other FROM clause elements. +*/ +static void selectPopWith(Walker *pWalker, Select *p){ + Parse *pParse = pWalker->pParse; + With *pWith = findRightmost(p)->pWith; + if( pWith!=0 ){ + assert( pParse->pWith==pWith ); + pParse->pWith = pWith->pOuter; + } +} +#else +#define selectPopWith 0 +#endif + +/* +** This routine is a Walker callback for "expanding" a SELECT statement. +** "Expanding" means to do the following: +** +** (1) Make sure VDBE cursor numbers have been assigned to every +** element of the FROM clause. +** +** (2) Fill in the pTabList->a[].pTab fields in the SrcList that +** defines FROM clause. When views appear in the FROM clause, +** fill pTabList->a[].pSelect with a copy of the SELECT statement +** that implements the view. A copy is made of the view's SELECT +** statement so that we can freely modify or delete that statement +** without worrying about messing up the persistent representation +** of the view. +** +** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword +** on joins and the ON and USING clause of joins. +** +** (4) Scan the list of columns in the result set (pEList) looking +** for instances of the "*" operator or the TABLE.* operator. +** If found, expand each "*" to be every column in every table +** and TABLE.* to be every column in TABLE. +** +*/ +static int selectExpander(Walker *pWalker, Select *p){ + Parse *pParse = pWalker->pParse; + int i, j, k; + SrcList *pTabList; + ExprList *pEList; + struct SrcList_item *pFrom; + sqlite3 *db = pParse->db; + Expr *pE, *pRight, *pExpr; + u16 selFlags = p->selFlags; + + p->selFlags |= SF_Expanded; + if( db->mallocFailed ){ + return WRC_Abort; + } + if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ + return WRC_Prune; + } + pTabList = p->pSrc; + pEList = p->pEList; + if( pWalker->xSelectCallback2==selectPopWith ){ + sqlite3WithPush(pParse, findRightmost(p)->pWith, 0); + } + + /* Make sure cursor numbers have been assigned to all entries in + ** the FROM clause of the SELECT statement. + */ + sqlite3SrcListAssignCursors(pParse, pTabList); + + /* Look up every table named in the FROM clause of the select. If + ** an entry of the FROM clause is a subquery instead of a table or view, + ** then create a transient table structure to describe the subquery. + */ + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab; + assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); + if( pFrom->fg.isRecursive ) continue; + assert( pFrom->pTab==0 ); +#ifndef SQLITE_OMIT_CTE + if( withExpand(pWalker, pFrom) ) return WRC_Abort; + if( pFrom->pTab ) {} else +#endif + if( pFrom->zName==0 ){ +#ifndef SQLITE_OMIT_SUBQUERY + Select *pSel = pFrom->pSelect; + /* A sub-query in the FROM clause of a SELECT */ + assert( pSel!=0 ); + assert( pFrom->pTab==0 ); + if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; + pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTab==0 ) return WRC_Abort; + pTab->nRef = 1; + pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab); + while( pSel->pPrior ){ pSel = pSel->pPrior; } + sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); + pTab->iPKey = -1; + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); + pTab->tabFlags |= TF_Ephemeral; +#endif + }else{ + /* An ordinary table or view name in the FROM clause */ + assert( pFrom->pTab==0 ); + pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); + if( pTab==0 ) return WRC_Abort; + if( pTab->nRef==0xffff ){ + sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535", + pTab->zName); + pFrom->pTab = 0; + return WRC_Abort; + } + pTab->nRef++; + if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){ + return WRC_Abort; + } +#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) + if( IsVirtual(pTab) || pTab->pSelect ){ + i16 nCol; + if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; + assert( pFrom->pSelect==0 ); + pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); + sqlite3SelectSetName(pFrom->pSelect, pTab->zName); + nCol = pTab->nCol; + pTab->nCol = -1; + sqlite3WalkSelect(pWalker, pFrom->pSelect); + pTab->nCol = nCol; + } +#endif + } + + /* Locate the index named by the INDEXED BY clause, if any. */ + if( sqlite3IndexedByLookup(pParse, pFrom) ){ + return WRC_Abort; + } + } + + /* Process NATURAL keywords, and ON and USING clauses of joins. + */ + if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){ + return WRC_Abort; + } + + /* For every "*" that occurs in the column list, insert the names of + ** all columns in all tables. And for every TABLE.* insert the names + ** of all columns in TABLE. The parser inserted a special expression + ** with the TK_ASTERISK operator for each "*" that it found in the column + ** list. The following code just has to locate the TK_ASTERISK + ** expressions and expand each one to the list of all columns in + ** all tables. + ** + ** The first loop just checks to see if there are any "*" operators + ** that need expanding. + */ + for(k=0; knExpr; k++){ + pE = pEList->a[k].pExpr; + if( pE->op==TK_ASTERISK ) break; + assert( pE->op!=TK_DOT || pE->pRight!=0 ); + assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) ); + if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break; + } + if( knExpr ){ + /* + ** If we get here it means the result set contains one or more "*" + ** operators that need to be expanded. Loop through each expression + ** in the result set and expand them one by one. + */ + struct ExprList_item *a = pEList->a; + ExprList *pNew = 0; + int flags = pParse->db->flags; + int longNames = (flags & SQLITE_FullColNames)!=0 + && (flags & SQLITE_ShortColNames)==0; + + for(k=0; knExpr; k++){ + pE = a[k].pExpr; + pRight = pE->pRight; + assert( pE->op!=TK_DOT || pRight!=0 ); + if( pE->op!=TK_ASTERISK + && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK) + ){ + /* This particular expression does not need to be expanded. + */ + pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr); + if( pNew ){ + pNew->a[pNew->nExpr-1].zName = a[k].zName; + pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan; + a[k].zName = 0; + a[k].zSpan = 0; + } + a[k].pExpr = 0; + }else{ + /* This expression is a "*" or a "TABLE.*" and needs to be + ** expanded. */ + int tableSeen = 0; /* Set to 1 when TABLE matches */ + char *zTName = 0; /* text of name of TABLE */ + if( pE->op==TK_DOT ){ + assert( pE->pLeft!=0 ); + assert( !ExprHasProperty(pE->pLeft, EP_IntValue) ); + zTName = pE->pLeft->u.zToken; + } + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab = pFrom->pTab; + Select *pSub = pFrom->pSelect; + char *zTabName = pFrom->zAlias; + const char *zSchemaName = 0; + int iDb; + if( zTabName==0 ){ + zTabName = pTab->zName; + } + if( db->mallocFailed ) break; + if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){ + pSub = 0; + if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ + continue; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*"; + } + for(j=0; jnCol; j++){ + char *zName = pTab->aCol[j].zName; + char *zColname; /* The computed column name */ + char *zToFree; /* Malloced string that needs to be freed */ + Token sColname; /* Computed column name as a token */ + + assert( zName ); + if( zTName && pSub + && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 + ){ + continue; + } + + /* If a column is marked as 'hidden', omit it from the expanded + ** result-set list unless the SELECT has the SF_IncludeHidden + ** bit set. + */ + if( (p->selFlags & SF_IncludeHidden)==0 + && IsHiddenColumn(&pTab->aCol[j]) + ){ + continue; + } + tableSeen = 1; + + if( i>0 && zTName==0 ){ + if( (pFrom->fg.jointype & JT_NATURAL)!=0 + && tableAndColumnIndex(pTabList, i, zName, 0, 0) + ){ + /* In a NATURAL join, omit the join columns from the + ** table to the right of the join */ + continue; + } + if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){ + /* In a join with a USING clause, omit columns in the + ** using clause from the table on the right. */ + continue; + } + } + pRight = sqlite3Expr(db, TK_ID, zName); + zColname = zName; + zToFree = 0; + if( longNames || pTabList->nSrc>1 ){ + Expr *pLeft; + pLeft = sqlite3Expr(db, TK_ID, zTabName); + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + if( zSchemaName ){ + pLeft = sqlite3Expr(db, TK_ID, zSchemaName); + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0); + } + if( longNames ){ + zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); + zToFree = zColname; + } + }else{ + pExpr = pRight; + } + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); + sqlite3TokenInit(&sColname, zColname); + sqlite3ExprListSetName(pParse, pNew, &sColname, 0); + if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){ + struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; + if( pSub ){ + pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan); + testcase( pX->zSpan==0 ); + }else{ + pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s", + zSchemaName, zTabName, zColname); + testcase( pX->zSpan==0 ); + } + pX->bSpanIsTab = 1; + } + sqlite3DbFree(db, zToFree); + } + } + if( !tableSeen ){ + if( zTName ){ + sqlite3ErrorMsg(pParse, "no such table: %s", zTName); + }else{ + sqlite3ErrorMsg(pParse, "no tables specified"); + } + } + } + } + sqlite3ExprListDelete(db, pEList); + p->pEList = pNew; + } +#if SQLITE_MAX_COLUMN + if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns in result set"); + return WRC_Abort; + } +#endif + return WRC_Continue; +} + +/* +** No-op routine for the parse-tree walker. +** +** When this routine is the Walker.xExprCallback then expression trees +** are walked without any actions being taken at each node. Presumably, +** when this routine is used for Walker.xExprCallback then +** Walker.xSelectCallback is set to do something useful for every +** subquery in the parser tree. +*/ +SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return WRC_Continue; +} + +/* +** This routine "expands" a SELECT statement and all of its subqueries. +** For additional information on what it means to "expand" a SELECT +** statement, see the comment on the selectExpand worker callback above. +** +** Expanding a SELECT statement is the first step in processing a +** SELECT statement. The SELECT statement must be expanded before +** name resolution is performed. +** +** If anything goes wrong, an error message is written into pParse. +** The calling function can detect the problem by looking at pParse->nErr +** and/or pParse->db->mallocFailed. +*/ +static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = sqlite3ExprWalkNoop; + w.pParse = pParse; + if( pParse->hasCompound ){ + w.xSelectCallback = convertCompoundSelectToSubquery; + sqlite3WalkSelect(&w, pSelect); + } + w.xSelectCallback = selectExpander; + if( (pSelect->selFlags & SF_MultiValue)==0 ){ + w.xSelectCallback2 = selectPopWith; + } + sqlite3WalkSelect(&w, pSelect); +} + + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() +** interface. +** +** For each FROM-clause subquery, add Column.zType and Column.zColl +** information to the Table structure that represents the result set +** of that subquery. +** +** The Table structure that represents the result set was constructed +** by selectExpander() but the type and collation information was omitted +** at that point because identifiers had not yet been resolved. This +** routine is called after identifier resolution. +*/ +static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){ + Parse *pParse; + int i; + SrcList *pTabList; + struct SrcList_item *pFrom; + + assert( p->selFlags & SF_Resolved ); + assert( (p->selFlags & SF_HasTypeInfo)==0 ); + p->selFlags |= SF_HasTypeInfo; + pParse = pWalker->pParse; + pTabList = p->pSrc; + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab = pFrom->pTab; + assert( pTab!=0 ); + if( (pTab->tabFlags & TF_Ephemeral)!=0 ){ + /* A sub-query in the FROM clause of a SELECT */ + Select *pSel = pFrom->pSelect; + if( pSel ){ + while( pSel->pPrior ) pSel = pSel->pPrior; + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel); + } + } + } +} +#endif + + +/* +** This routine adds datatype and collating sequence information to +** the Table structures of all FROM-clause subqueries in a +** SELECT statement. +** +** Use this routine after name resolution. +*/ +static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){ +#ifndef SQLITE_OMIT_SUBQUERY + Walker w; + memset(&w, 0, sizeof(w)); + w.xSelectCallback2 = selectAddSubqueryTypeInfo; + w.xExprCallback = sqlite3ExprWalkNoop; + w.pParse = pParse; + sqlite3WalkSelect(&w, pSelect); +#endif +} + + +/* +** This routine sets up a SELECT statement for processing. The +** following is accomplished: +** +** * VDBE Cursor numbers are assigned to all FROM-clause terms. +** * Ephemeral Table objects are created for all FROM-clause subqueries. +** * ON and USING clauses are shifted into WHERE statements +** * Wildcards "*" and "TABLE.*" in result sets are expanded. +** * Identifiers in expression are matched to tables. +** +** This routine acts recursively on all subqueries within the SELECT. +*/ +SQLITE_PRIVATE void sqlite3SelectPrep( + Parse *pParse, /* The parser context */ + Select *p, /* The SELECT statement being coded. */ + NameContext *pOuterNC /* Name context for container */ +){ + sqlite3 *db; + if( NEVER(p==0) ) return; + db = pParse->db; + if( db->mallocFailed ) return; + if( p->selFlags & SF_HasTypeInfo ) return; + sqlite3SelectExpand(pParse, p); + if( pParse->nErr || db->mallocFailed ) return; + sqlite3ResolveSelectNames(pParse, p, pOuterNC); + if( pParse->nErr || db->mallocFailed ) return; + sqlite3SelectAddTypeInfo(pParse, p); +} + +/* +** Reset the aggregate accumulator. +** +** The aggregate accumulator is a set of memory cells that hold +** intermediate results while calculating an aggregate. This +** routine generates code that stores NULLs in all of those memory +** cells. +*/ +static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + struct AggInfo_func *pFunc; + int nReg = pAggInfo->nFunc + pAggInfo->nColumn; + if( nReg==0 ) return; +#ifdef SQLITE_DEBUG + /* Verify that all AggInfo registers are within the range specified by + ** AggInfo.mnReg..AggInfo.mxReg */ + assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 ); + for(i=0; inColumn; i++){ + assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg + && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg ); + } + for(i=0; inFunc; i++){ + assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg + && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg ); + } +#endif + sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg); + for(pFunc=pAggInfo->aFunc, i=0; inFunc; i++, pFunc++){ + if( pFunc->iDistinct>=0 ){ + Expr *pE = pFunc->pExpr; + assert( !ExprHasProperty(pE, EP_xIsSelect) ); + if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){ + sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one " + "argument"); + pFunc->iDistinct = -1; + }else{ + KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0, + (char*)pKeyInfo, P4_KEYINFO); + } + } + } +} + +/* +** Invoke the OP_AggFinalize opcode for every aggregate function +** in the AggInfo structure. +*/ +static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + struct AggInfo_func *pF; + for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ + ExprList *pList = pF->pExpr->x.pList; + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); + sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0, + (void*)pF->pFunc, P4_FUNCDEF); + } +} + +/* +** Update the accumulator memory cells for an aggregate based on +** the current cursor position. +*/ +static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + int regHit = 0; + int addrHitTest = 0; + struct AggInfo_func *pF; + struct AggInfo_col *pC; + + pAggInfo->directMode = 1; + for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ + int nArg; + int addrNext = 0; + int regAgg; + ExprList *pList = pF->pExpr->x.pList; + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); + if( pList ){ + nArg = pList->nExpr; + regAgg = sqlite3GetTempRange(pParse, nArg); + sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP); + }else{ + nArg = 0; + regAgg = 0; + } + if( pF->iDistinct>=0 ){ + addrNext = sqlite3VdbeMakeLabel(v); + testcase( nArg==0 ); /* Error condition */ + testcase( nArg>1 ); /* Also an error */ + codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); + } + if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ + CollSeq *pColl = 0; + struct ExprList_item *pItem; + int j; + assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ + for(j=0, pItem=pList->a; !pColl && jpExpr); + } + if( !pColl ){ + pColl = pParse->db->pDfltColl; + } + if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; + sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem, + (void*)pF->pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u8)nArg); + sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); + sqlite3ReleaseTempRange(pParse, regAgg, nArg); + if( addrNext ){ + sqlite3VdbeResolveLabel(v, addrNext); + sqlite3ExprCacheClear(pParse); + } + } + + /* Before populating the accumulator registers, clear the column cache. + ** Otherwise, if any of the required column values are already present + ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value + ** to pC->iMem. But by the time the value is used, the original register + ** may have been used, invalidating the underlying buffer holding the + ** text or blob value. See ticket [883034dcb5]. + ** + ** Another solution would be to change the OP_SCopy used to copy cached + ** values to an OP_Copy. + */ + if( regHit ){ + addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v); + } + sqlite3ExprCacheClear(pParse); + for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ + sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); + } + pAggInfo->directMode = 0; + sqlite3ExprCacheClear(pParse); + if( addrHitTest ){ + sqlite3VdbeJumpHere(v, addrHitTest); + } +} + +/* +** Add a single OP_Explain instruction to the VDBE to explain a simple +** count(*) query ("SELECT count(*) FROM pTab"). +*/ +#ifndef SQLITE_OMIT_EXPLAIN +static void explainSimpleCount( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being queried */ + Index *pIdx /* Index used to optimize scan, or NULL */ +){ + if( pParse->explain==2 ){ + int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx))); + char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s", + pTab->zName, + bCover ? " USING COVERING INDEX " : "", + bCover ? pIdx->zName : "" + ); + sqlite3VdbeAddOp4( + pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC + ); + } +} +#else +# define explainSimpleCount(a,b,c) +#endif + +/* +** Generate code for the SELECT statement given in the p argument. +** +** The results are returned according to the SelectDest structure. +** See comments in sqliteInt.h for further information. +** +** This routine returns the number of errors. If any errors are +** encountered, then an appropriate error message is left in +** pParse->zErrMsg. +** +** This routine does NOT free the Select structure passed in. The +** calling function needs to do that. +*/ +SQLITE_PRIVATE int sqlite3Select( + Parse *pParse, /* The parser context */ + Select *p, /* The SELECT statement being coded. */ + SelectDest *pDest /* What to do with the query results */ +){ + int i, j; /* Loop counters */ + WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ + Vdbe *v; /* The virtual machine under construction */ + int isAgg; /* True for select lists like "count(*)" */ + ExprList *pEList = 0; /* List of columns to extract. */ + SrcList *pTabList; /* List of tables to select from */ + Expr *pWhere; /* The WHERE clause. May be NULL */ + ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ + Expr *pHaving; /* The HAVING clause. May be NULL */ + int rc = 1; /* Value to return from this function */ + DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */ + SortCtx sSort; /* Info on how to code the ORDER BY clause */ + AggInfo sAggInfo; /* Information used by aggregate queries */ + int iEnd; /* Address of the end of the query */ + sqlite3 *db; /* The database connection */ + +#ifndef SQLITE_OMIT_EXPLAIN + int iRestoreSelectId = pParse->iSelectId; + pParse->iSelectId = pParse->iNextSelectId++; +#endif + + db = pParse->db; + if( p==0 || db->mallocFailed || pParse->nErr ){ + return 1; + } + if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; + memset(&sAggInfo, 0, sizeof(sAggInfo)); +#if SELECTTRACE_ENABLED + pParse->nSelectIndent++; + SELECTTRACE(1,pParse,p, ("begin processing:\n")); + if( sqlite3SelectTrace & 0x100 ){ + sqlite3TreeViewSelect(0, p, 0); + } +#endif + + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo ); + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo ); + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue ); + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue ); + if( IgnorableOrderby(pDest) ){ + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard || + pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo || + pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo); + /* If ORDER BY makes no difference in the output then neither does + ** DISTINCT so it can be removed too. */ + sqlite3ExprListDelete(db, p->pOrderBy); + p->pOrderBy = 0; + p->selFlags &= ~SF_Distinct; + } + sqlite3SelectPrep(pParse, p, 0); + memset(&sSort, 0, sizeof(sSort)); + sSort.pOrderBy = p->pOrderBy; + pTabList = p->pSrc; + if( pParse->nErr || db->mallocFailed ){ + goto select_end; + } + assert( p->pEList!=0 ); + isAgg = (p->selFlags & SF_Aggregate)!=0; +#if SELECTTRACE_ENABLED + if( sqlite3SelectTrace & 0x100 ){ + SELECTTRACE(0x100,pParse,p, ("after name resolution:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + + + /* If writing to memory or generating a set + ** only a single column may be output. + */ +#ifndef SQLITE_OMIT_SUBQUERY + if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){ + goto select_end; + } +#endif + + /* Try to flatten subqueries in the FROM clause up into the main query + */ +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) + for(i=0; !p->pPrior && inSrc; i++){ + struct SrcList_item *pItem = &pTabList->a[i]; + Select *pSub = pItem->pSelect; + int isAggSub; + Table *pTab = pItem->pTab; + if( pSub==0 ) continue; + + /* Catch mismatch in the declared columns of a view and the number of + ** columns in the SELECT on the RHS */ + if( pTab->nCol!=pSub->pEList->nExpr ){ + sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d", + pTab->nCol, pTab->zName, pSub->pEList->nExpr); + goto select_end; + } + + isAggSub = (pSub->selFlags & SF_Aggregate)!=0; + if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){ + /* This subquery can be absorbed into its parent. */ + if( isAggSub ){ + isAgg = 1; + p->selFlags |= SF_Aggregate; + } + i = -1; + } + pTabList = p->pSrc; + if( db->mallocFailed ) goto select_end; + if( !IgnorableOrderby(pDest) ){ + sSort.pOrderBy = p->pOrderBy; + } + } +#endif + + /* Get a pointer the VDBE under construction, allocating a new VDBE if one + ** does not already exist */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto select_end; + +#ifndef SQLITE_OMIT_COMPOUND_SELECT + /* Handle compound SELECT statements using the separate multiSelect() + ** procedure. + */ + if( p->pPrior ){ + rc = multiSelect(pParse, p, pDest); + explainSetInteger(pParse->iSelectId, iRestoreSelectId); +#if SELECTTRACE_ENABLED + SELECTTRACE(1,pParse,p,("end compound-select processing\n")); + pParse->nSelectIndent--; +#endif + return rc; + } +#endif + + /* Generate code for all sub-queries in the FROM clause + */ +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) + for(i=0; inSrc; i++){ + struct SrcList_item *pItem = &pTabList->a[i]; + SelectDest dest; + Select *pSub = pItem->pSelect; + if( pSub==0 ) continue; + + /* Sometimes the code for a subquery will be generated more than + ** once, if the subquery is part of the WHERE clause in a LEFT JOIN, + ** for example. In that case, do not regenerate the code to manifest + ** a view or the co-routine to implement a view. The first instance + ** is sufficient, though the subroutine to manifest the view does need + ** to be invoked again. */ + if( pItem->addrFillSub ){ + if( pItem->fg.viaCoroutine==0 ){ + sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub); + } + continue; + } + + /* Increment Parse.nHeight by the height of the largest expression + ** tree referred to by this, the parent select. The child select + ** may contain expression trees of at most + ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit + ** more conservative than necessary, but much easier than enforcing + ** an exact limit. + */ + pParse->nHeight += sqlite3SelectExprHeight(p); + + /* Make copies of constant WHERE-clause terms in the outer query down + ** inside the subquery. This can help the subquery to run more efficiently. + */ + if( (pItem->fg.jointype & JT_OUTER)==0 + && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor) + ){ +#if SELECTTRACE_ENABLED + if( sqlite3SelectTrace & 0x100 ){ + SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + } + + /* Generate code to implement the subquery + ** + ** The subquery is implemented as a co-routine if all of these are true: + ** (1) The subquery is guaranteed to be the outer loop (so that it + ** does not need to be computed more than once) + ** (2) The ALL keyword after SELECT is omitted. (Applications are + ** allowed to say "SELECT ALL" instead of just "SELECT" to disable + ** the use of co-routines.) + ** (3) Co-routines are not disabled using sqlite3_test_control() + ** with SQLITE_TESTCTRL_OPTIMIZATIONS. + ** + ** TODO: Are there other reasons beside (1) to use a co-routine + ** implementation? + */ + if( i==0 + && (pTabList->nSrc==1 + || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */ + && (p->selFlags & SF_All)==0 /* (2) */ + && OptimizationEnabled(db, SQLITE_SubqCoroutine) /* (3) */ + ){ + /* Implement a co-routine that will return a single row of the result + ** set on each invocation. + */ + int addrTop = sqlite3VdbeCurrentAddr(v)+1; + pItem->regReturn = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); + VdbeComment((v, "%s", pItem->pTab->zName)); + pItem->addrFillSub = addrTop; + sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); + explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + sqlite3Select(pParse, pSub, &dest); + pItem->pTab->nRowLogEst = pSub->nSelectRow; + pItem->fg.viaCoroutine = 1; + pItem->regResult = dest.iSdst; + sqlite3VdbeEndCoroutine(v, pItem->regReturn); + sqlite3VdbeJumpHere(v, addrTop-1); + sqlite3ClearTempRegCache(pParse); + }else{ + /* Generate a subroutine that will fill an ephemeral table with + ** the content of this subquery. pItem->addrFillSub will point + ** to the address of the generated subroutine. pItem->regReturn + ** is a register allocated to hold the subroutine return address + */ + int topAddr; + int onceAddr = 0; + int retAddr; + assert( pItem->addrFillSub==0 ); + pItem->regReturn = ++pParse->nMem; + topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn); + pItem->addrFillSub = topAddr+1; + if( pItem->fg.isCorrelated==0 ){ + /* If the subquery is not correlated and if we are not inside of + ** a trigger, then we only need to compute the value of the subquery + ** once. */ + onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); + VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName)); + }else{ + VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); + } + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); + explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + sqlite3Select(pParse, pSub, &dest); + pItem->pTab->nRowLogEst = pSub->nSelectRow; + if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); + retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); + VdbeComment((v, "end %s", pItem->pTab->zName)); + sqlite3VdbeChangeP1(v, topAddr, retAddr); + sqlite3ClearTempRegCache(pParse); + } + if( db->mallocFailed ) goto select_end; + pParse->nHeight -= sqlite3SelectExprHeight(p); + } +#endif + + /* Various elements of the SELECT copied into local variables for + ** convenience */ + pEList = p->pEList; + pWhere = p->pWhere; + pGroupBy = p->pGroupBy; + pHaving = p->pHaving; + sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0; + +#if SELECTTRACE_ENABLED + if( sqlite3SelectTrace & 0x400 ){ + SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + + /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and + ** if the select-list is the same as the ORDER BY list, then this query + ** can be rewritten as a GROUP BY. In other words, this: + ** + ** SELECT DISTINCT xyz FROM ... ORDER BY xyz + ** + ** is transformed to: + ** + ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz + ** + ** The second form is preferred as a single index (or temp-table) may be + ** used for both the ORDER BY and DISTINCT processing. As originally + ** written the query must use a temp-table for at least one of the ORDER + ** BY and DISTINCT, and an index or separate temp-table for the other. + */ + if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct + && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 + ){ + p->selFlags &= ~SF_Distinct; + pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); + /* Notice that even thought SF_Distinct has been cleared from p->selFlags, + ** the sDistinct.isTnct is still set. Hence, isTnct represents the + ** original setting of the SF_Distinct flag, not the current setting */ + assert( sDistinct.isTnct ); + +#if SELECTTRACE_ENABLED + if( sqlite3SelectTrace & 0x400 ){ + SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + } + + /* If there is an ORDER BY clause, then create an ephemeral index to + ** do the sorting. But this sorting ephemeral index might end up + ** being unused if the data can be extracted in pre-sorted order. + ** If that is the case, then the OP_OpenEphemeral instruction will be + ** changed to an OP_Noop once we figure out that the sorting index is + ** not needed. The sSort.addrSortIndex variable is used to facilitate + ** that change. + */ + if( sSort.pOrderBy ){ + KeyInfo *pKeyInfo; + pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr); + sSort.iECursor = pParse->nTab++; + sSort.addrSortIndex = + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0, + (char*)pKeyInfo, P4_KEYINFO + ); + }else{ + sSort.addrSortIndex = -1; + } + + /* If the output is destined for a temporary table, open that table. + */ + if( pDest->eDest==SRT_EphemTab ){ + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr); + } + + /* Set the limiter. + */ + iEnd = sqlite3VdbeMakeLabel(v); + p->nSelectRow = 320; /* 4 billion rows */ + computeLimitRegisters(pParse, p, iEnd); + if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ + sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); + sSort.sortFlags |= SORTFLAG_UseSorter; + } + + /* Open an ephemeral index to use for the distinct set. + */ + if( p->selFlags & SF_Distinct ){ + sDistinct.tabTnct = pParse->nTab++; + sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + sDistinct.tabTnct, 0, 0, + (char*)keyInfoFromExprList(pParse, p->pEList,0,0), + P4_KEYINFO); + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); + sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; + }else{ + sDistinct.eTnctType = WHERE_DISTINCT_NOOP; + } + + if( !isAgg && pGroupBy==0 ){ + /* No aggregate functions and no GROUP BY clause */ + u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0); + assert( WHERE_USE_LIMIT==SF_FixedLimit ); + wctrlFlags |= p->selFlags & SF_FixedLimit; + + /* Begin the database scan. */ + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, + p->pEList, wctrlFlags, p->nSelectRow); + if( pWInfo==0 ) goto select_end; + if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ + p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); + } + if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ + sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); + } + if( sSort.pOrderBy ){ + sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo); + if( sSort.nOBSat==sSort.pOrderBy->nExpr ){ + sSort.pOrderBy = 0; + } + } + + /* If sorting index that was created by a prior OP_OpenEphemeral + ** instruction ended up not being needed, then change the OP_OpenEphemeral + ** into an OP_Noop. + */ + if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){ + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex); + } + + /* Use the standard inner loop. */ + selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest, + sqlite3WhereContinueLabel(pWInfo), + sqlite3WhereBreakLabel(pWInfo)); + + /* End the database scan loop. + */ + sqlite3WhereEnd(pWInfo); + }else{ + /* This case when there exist aggregate functions or a GROUP BY clause + ** or both */ + NameContext sNC; /* Name context for processing aggregate information */ + int iAMem; /* First Mem address for storing current GROUP BY */ + int iBMem; /* First Mem address for previous GROUP BY */ + int iUseFlag; /* Mem address holding flag indicating that at least + ** one row of the input to the aggregator has been + ** processed */ + int iAbortFlag; /* Mem address which causes query abort if positive */ + int groupBySort; /* Rows come from source in GROUP BY order */ + int addrEnd; /* End of processing for this SELECT */ + int sortPTab = 0; /* Pseudotable used to decode sorting results */ + int sortOut = 0; /* Output register from the sorter */ + int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */ + + /* Remove any and all aliases between the result set and the + ** GROUP BY clause. + */ + if( pGroupBy ){ + int k; /* Loop counter */ + struct ExprList_item *pItem; /* For looping over expression in a list */ + + for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ + pItem->u.x.iAlias = 0; + } + for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ + pItem->u.x.iAlias = 0; + } + assert( 66==sqlite3LogEst(100) ); + if( p->nSelectRow>66 ) p->nSelectRow = 66; + }else{ + assert( 0==sqlite3LogEst(1) ); + p->nSelectRow = 0; + } + + /* If there is both a GROUP BY and an ORDER BY clause and they are + ** identical, then it may be possible to disable the ORDER BY clause + ** on the grounds that the GROUP BY will cause elements to come out + ** in the correct order. It also may not - the GROUP BY might use a + ** database index that causes rows to be grouped together as required + ** but not actually sorted. Either way, record the fact that the + ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp + ** variable. */ + if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){ + orderByGrp = 1; + } + + /* Create a label to jump to when we want to abort the query */ + addrEnd = sqlite3VdbeMakeLabel(v); + + /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in + ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the + ** SELECT statement. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + sNC.pAggInfo = &sAggInfo; + sAggInfo.mnReg = pParse->nMem+1; + sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0; + sAggInfo.pGroupBy = pGroupBy; + sqlite3ExprAnalyzeAggList(&sNC, pEList); + sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy); + if( pHaving ){ + sqlite3ExprAnalyzeAggregates(&sNC, pHaving); + } + sAggInfo.nAccumulator = sAggInfo.nColumn; + for(i=0; ix.pList); + sNC.ncFlags &= ~NC_InAggFunc; + } + sAggInfo.mxReg = pParse->nMem; + if( db->mallocFailed ) goto select_end; + + /* Processing for aggregates with GROUP BY is very different and + ** much more complex than aggregates without a GROUP BY. + */ + if( pGroupBy ){ + KeyInfo *pKeyInfo; /* Keying information for the group by clause */ + int addr1; /* A-vs-B comparision jump */ + int addrOutputRow; /* Start of subroutine that outputs a result row */ + int regOutputRow; /* Return address register for output subroutine */ + int addrSetAbort; /* Set the abort flag and return */ + int addrTopOfLoop; /* Top of the input loop */ + int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */ + int addrReset; /* Subroutine for resetting the accumulator */ + int regReset; /* Return address register for reset subroutine */ + + /* If there is a GROUP BY clause we might need a sorting index to + ** implement it. Allocate that sorting index now. If it turns out + ** that we do not need it after all, the OP_SorterOpen instruction + ** will be converted into a Noop. + */ + sAggInfo.sortingIdx = pParse->nTab++; + pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn); + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, + sAggInfo.sortingIdx, sAggInfo.nSortingColumn, + 0, (char*)pKeyInfo, P4_KEYINFO); + + /* Initialize memory locations used by GROUP BY aggregate processing + */ + iUseFlag = ++pParse->nMem; + iAbortFlag = ++pParse->nMem; + regOutputRow = ++pParse->nMem; + addrOutputRow = sqlite3VdbeMakeLabel(v); + regReset = ++pParse->nMem; + addrReset = sqlite3VdbeMakeLabel(v); + iAMem = pParse->nMem + 1; + pParse->nMem += pGroupBy->nExpr; + iBMem = pParse->nMem + 1; + pParse->nMem += pGroupBy->nExpr; + sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag); + VdbeComment((v, "clear abort flag")); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); + VdbeComment((v, "indicate accumulator empty")); + sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1); + + /* Begin a loop that will extract all source rows in GROUP BY order. + ** This might involve two separate loops with an OP_Sort in between, or + ** it might be a single loop that uses an index to extract information + ** in the right order to begin with. + */ + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, + WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0 + ); + if( pWInfo==0 ) goto select_end; + if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){ + /* The optimizer is able to deliver rows in group by order so + ** we do not have to sort. The OP_OpenEphemeral table will be + ** cancelled later because we still need to use the pKeyInfo + */ + groupBySort = 0; + }else{ + /* Rows are coming out in undetermined order. We have to push + ** each row into a sorting index, terminate the first loop, + ** then loop over the sorting index in order to get the output + ** in sorted order + */ + int regBase; + int regRecord; + int nCol; + int nGroupBy; + + explainTempTable(pParse, + (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ? + "DISTINCT" : "GROUP BY"); + + groupBySort = 1; + nGroupBy = pGroupBy->nExpr; + nCol = nGroupBy; + j = nGroupBy; + for(i=0; i=j ){ + nCol++; + j++; + } + } + regBase = sqlite3GetTempRange(pParse, nCol); + sqlite3ExprCacheClear(pParse); + sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0); + j = nGroupBy; + for(i=0; iiSorterColumn>=j ){ + int r1 = j + regBase; + sqlite3ExprCodeGetColumnToReg(pParse, + pCol->pTab, pCol->iColumn, pCol->iTable, r1); + j++; + } + } + regRecord = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); + sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ReleaseTempRange(pParse, regBase, nCol); + sqlite3WhereEnd(pWInfo); + sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++; + sortOut = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol); + sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd); + VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v); + sAggInfo.useSortingIdx = 1; + sqlite3ExprCacheClear(pParse); + + } + + /* If the index or temporary table used by the GROUP BY sort + ** will naturally deliver rows in the order required by the ORDER BY + ** clause, cancel the ephemeral table open coded earlier. + ** + ** This is an optimization - the correct answer should result regardless. + ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to + ** disable this optimization for testing purposes. */ + if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder) + && (groupBySort || sqlite3WhereIsSorted(pWInfo)) + ){ + sSort.pOrderBy = 0; + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex); + } + + /* Evaluate the current GROUP BY terms and store in b0, b1, b2... + ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) + ** Then compare the current GROUP BY terms against the GROUP BY terms + ** from the previous row currently stored in a0, a1, a2... + */ + addrTopOfLoop = sqlite3VdbeCurrentAddr(v); + sqlite3ExprCacheClear(pParse); + if( groupBySort ){ + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, + sortOut, sortPTab); + } + for(j=0; jnExpr; j++){ + if( groupBySort ){ + sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); + }else{ + sAggInfo.directMode = 1; + sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); + } + } + sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr, + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO); + addr1 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v); + + /* Generate code that runs whenever the GROUP BY changes. + ** Changes in the GROUP BY are detected by the previous code + ** block. If there were no changes, this block is skipped. + ** + ** This code copies current group by terms in b0,b1,b2,... + ** over to a0,a1,a2. It then calls the output subroutine + ** and resets the aggregate accumulator registers in preparation + ** for the next GROUP BY batch. + */ + sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr); + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); + VdbeComment((v, "output one row")); + sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v); + VdbeComment((v, "check abort flag")); + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); + VdbeComment((v, "reset accumulator")); + + /* Update the aggregate accumulators based on the content of + ** the current row + */ + sqlite3VdbeJumpHere(v, addr1); + updateAccumulator(pParse, &sAggInfo); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag); + VdbeComment((v, "indicate data in accumulator")); + + /* End of the loop + */ + if( groupBySort ){ + sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop); + VdbeCoverage(v); + }else{ + sqlite3WhereEnd(pWInfo); + sqlite3VdbeChangeToNoop(v, addrSortingIdx); + } + + /* Output the final row of result + */ + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); + VdbeComment((v, "output final row")); + + /* Jump over the subroutines + */ + sqlite3VdbeGoto(v, addrEnd); + + /* Generate a subroutine that outputs a single row of the result + ** set. This subroutine first looks at the iUseFlag. If iUseFlag + ** is less than or equal to zero, the subroutine is a no-op. If + ** the processing calls for the query to abort, this subroutine + ** increments the iAbortFlag memory location before returning in + ** order to signal the caller to abort. + */ + addrSetAbort = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag); + VdbeComment((v, "set abort flag")); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + sqlite3VdbeResolveLabel(v, addrOutputRow); + addrOutputRow = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); + VdbeCoverage(v); + VdbeComment((v, "Groupby result generator entry point")); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + finalizeAggFunctions(pParse, &sAggInfo); + sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); + selectInnerLoop(pParse, p, p->pEList, -1, &sSort, + &sDistinct, pDest, + addrOutputRow+1, addrSetAbort); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + VdbeComment((v, "end groupby result generator")); + + /* Generate a subroutine that will reset the group-by accumulator + */ + sqlite3VdbeResolveLabel(v, addrReset); + resetAccumulator(pParse, &sAggInfo); + sqlite3VdbeAddOp1(v, OP_Return, regReset); + + } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ + else { + ExprList *pDel = 0; +#ifndef SQLITE_OMIT_BTREECOUNT + Table *pTab; + if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){ + /* If isSimpleCount() returns a pointer to a Table structure, then + ** the SQL statement is of the form: + ** + ** SELECT count(*) FROM + ** + ** where the Table structure returned represents table . + ** + ** This statement is so common that it is optimized specially. The + ** OP_Count instruction is executed either on the intkey table that + ** contains the data for table or on one of its indexes. It + ** is better to execute the op on an index, as indexes are almost + ** always spread across less pages than their corresponding tables. + */ + const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */ + Index *pIdx; /* Iterator variable */ + KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */ + Index *pBest = 0; /* Best index found so far */ + int iRoot = pTab->tnum; /* Root page of scanned b-tree */ + + sqlite3CodeVerifySchema(pParse, iDb); + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + + /* Search for the index that has the lowest scan cost. + ** + ** (2011-04-15) Do not do a full scan of an unordered index. + ** + ** (2013-10-03) Do not count the entries in a partial index. + ** + ** In practice the KeyInfo structure will not be used. It is only + ** passed to keep OP_OpenRead happy. + */ + if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->bUnordered==0 + && pIdx->szIdxRowszTabRow + && pIdx->pPartIdxWhere==0 + && (!pBest || pIdx->szIdxRowszIdxRow) + ){ + pBest = pIdx; + } + } + if( pBest ){ + iRoot = pBest->tnum; + pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest); + } + + /* Open a read-only cursor, execute the OP_Count, close the cursor. */ + sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1); + if( pKeyInfo ){ + sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO); + } + sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); + sqlite3VdbeAddOp1(v, OP_Close, iCsr); + explainSimpleCount(pParse, pTab, pBest); + }else +#endif /* SQLITE_OMIT_BTREECOUNT */ + { + /* Check if the query is of one of the following forms: + ** + ** SELECT min(x) FROM ... + ** SELECT max(x) FROM ... + ** + ** If it is, then ask the code in where.c to attempt to sort results + ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. + ** If where.c is able to produce results sorted in this order, then + ** add vdbe code to break out of the processing loop after the + ** first iteration (since the first iteration of the loop is + ** guaranteed to operate on the row with the minimum or maximum + ** value of x, the only row required). + ** + ** A special flag must be passed to sqlite3WhereBegin() to slightly + ** modify behavior as follows: + ** + ** + If the query is a "SELECT min(x)", then the loop coded by + ** where.c should not iterate over any values with a NULL value + ** for x. + ** + ** + The optimizer code in where.c (the thing that decides which + ** index or indices to use) should place a different priority on + ** satisfying the 'ORDER BY' clause than it does in other cases. + ** Refer to code and comments in where.c for details. + */ + ExprList *pMinMax = 0; + u8 flag = WHERE_ORDERBY_NORMAL; + + assert( p->pGroupBy==0 ); + assert( flag==0 ); + if( p->pHaving==0 ){ + flag = minMaxQuery(&sAggInfo, &pMinMax); + } + assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) ); + + if( flag ){ + pMinMax = sqlite3ExprListDup(db, pMinMax, 0); + pDel = pMinMax; + assert( db->mallocFailed || pMinMax!=0 ); + if( !db->mallocFailed ){ + pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0; + pMinMax->a[0].pExpr->op = TK_COLUMN; + } + } + + /* This case runs if the aggregate has no GROUP BY clause. The + ** processing is much simpler since there is only a single row + ** of output. + */ + resetAccumulator(pParse, &sAggInfo); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0); + if( pWInfo==0 ){ + sqlite3ExprListDelete(db, pDel); + goto select_end; + } + updateAccumulator(pParse, &sAggInfo); + assert( pMinMax==0 || pMinMax->nExpr==1 ); + if( sqlite3WhereIsOrdered(pWInfo)>0 ){ + sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo)); + VdbeComment((v, "%s() by index", + (flag==WHERE_ORDERBY_MIN?"min":"max"))); + } + sqlite3WhereEnd(pWInfo); + finalizeAggFunctions(pParse, &sAggInfo); + } + + sSort.pOrderBy = 0; + sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL); + selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, + pDest, addrEnd, addrEnd); + sqlite3ExprListDelete(db, pDel); + } + sqlite3VdbeResolveLabel(v, addrEnd); + + } /* endif aggregate query */ + + if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){ + explainTempTable(pParse, "DISTINCT"); + } + + /* If there is an ORDER BY clause, then we need to sort the results + ** and send them to the callback one by one. + */ + if( sSort.pOrderBy ){ + explainTempTable(pParse, + sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY"); + generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest); + } + + /* Jump here to skip this query + */ + sqlite3VdbeResolveLabel(v, iEnd); + + /* The SELECT has been coded. If there is an error in the Parse structure, + ** set the return code to 1. Otherwise 0. */ + rc = (pParse->nErr>0); + + /* Control jumps to here if an error is encountered above, or upon + ** successful coding of the SELECT. + */ +select_end: + explainSetInteger(pParse->iSelectId, iRestoreSelectId); + + /* Identify column names if results of the SELECT are to be output. + */ + if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){ + generateColumnNames(pParse, pTabList, pEList); + } + + sqlite3DbFree(db, sAggInfo.aCol); + sqlite3DbFree(db, sAggInfo.aFunc); +#if SELECTTRACE_ENABLED + SELECTTRACE(1,pParse,p,("end processing\n")); + pParse->nSelectIndent--; +#endif + return rc; +} + +/************** End of select.c **********************************************/ +/************** Begin file table.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the sqlite3_get_table() and sqlite3_free_table() +** interface routines. These are just wrappers around the main +** interface routine of sqlite3_exec(). +** +** These routines are in a separate files so that they will not be linked +** if they are not used. +*/ +/* #include "sqliteInt.h" */ +/* #include */ +/* #include */ + +#ifndef SQLITE_OMIT_GET_TABLE + +/* +** This structure is used to pass data from sqlite3_get_table() through +** to the callback function is uses to build the result. +*/ +typedef struct TabResult { + char **azResult; /* Accumulated output */ + char *zErrMsg; /* Error message text, if an error occurs */ + u32 nAlloc; /* Slots allocated for azResult[] */ + u32 nRow; /* Number of rows in the result */ + u32 nColumn; /* Number of columns in the result */ + u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ + int rc; /* Return code from sqlite3_exec() */ +} TabResult; + +/* +** This routine is called once for each row in the result table. Its job +** is to fill in the TabResult structure appropriately, allocating new +** memory as necessary. +*/ +static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ + TabResult *p = (TabResult*)pArg; /* Result accumulator */ + int need; /* Slots needed in p->azResult[] */ + int i; /* Loop counter */ + char *z; /* A single column of result */ + + /* Make sure there is enough space in p->azResult to hold everything + ** we need to remember from this invocation of the callback. + */ + if( p->nRow==0 && argv!=0 ){ + need = nCol*2; + }else{ + need = nCol; + } + if( p->nData + need > p->nAlloc ){ + char **azNew; + p->nAlloc = p->nAlloc*2 + need; + azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); + if( azNew==0 ) goto malloc_failed; + p->azResult = azNew; + } + + /* If this is the first row, then generate an extra row containing + ** the names of all columns. + */ + if( p->nRow==0 ){ + p->nColumn = nCol; + for(i=0; iazResult[p->nData++] = z; + } + }else if( (int)p->nColumn!=nCol ){ + sqlite3_free(p->zErrMsg); + p->zErrMsg = sqlite3_mprintf( + "sqlite3_get_table() called with two or more incompatible queries" + ); + p->rc = SQLITE_ERROR; + return 1; + } + + /* Copy over the row data + */ + if( argv!=0 ){ + for(i=0; iazResult[p->nData++] = z; + } + p->nRow++; + } + return 0; + +malloc_failed: + p->rc = SQLITE_NOMEM_BKPT; + return 1; +} + +/* +** Query the database. But instead of invoking a callback for each row, +** malloc() for space to hold the result and return the entire results +** at the conclusion of the call. +** +** The result that is written to ***pazResult is held in memory obtained +** from malloc(). But the caller cannot free this memory directly. +** Instead, the entire table should be passed to sqlite3_free_table() when +** the calling procedure is finished using it. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_get_table( + sqlite3 *db, /* The database on which the SQL executes */ + const char *zSql, /* The SQL to be executed */ + char ***pazResult, /* Write the result table here */ + int *pnRow, /* Write the number of rows in the result here */ + int *pnColumn, /* Write the number of columns of result here */ + char **pzErrMsg /* Write error messages here */ +){ + int rc; + TabResult res; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT; +#endif + *pazResult = 0; + if( pnColumn ) *pnColumn = 0; + if( pnRow ) *pnRow = 0; + if( pzErrMsg ) *pzErrMsg = 0; + res.zErrMsg = 0; + res.nRow = 0; + res.nColumn = 0; + res.nData = 1; + res.nAlloc = 20; + res.rc = SQLITE_OK; + res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); + if( res.azResult==0 ){ + db->errCode = SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; + } + res.azResult[0] = 0; + rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); + assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); + res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); + if( (rc&0xff)==SQLITE_ABORT ){ + sqlite3_free_table(&res.azResult[1]); + if( res.zErrMsg ){ + if( pzErrMsg ){ + sqlite3_free(*pzErrMsg); + *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); + } + sqlite3_free(res.zErrMsg); + } + db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ + return res.rc; + } + sqlite3_free(res.zErrMsg); + if( rc!=SQLITE_OK ){ + sqlite3_free_table(&res.azResult[1]); + return rc; + } + if( res.nAlloc>res.nData ){ + char **azNew; + azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); + if( azNew==0 ){ + sqlite3_free_table(&res.azResult[1]); + db->errCode = SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; + } + res.azResult = azNew; + } + *pazResult = &res.azResult[1]; + if( pnColumn ) *pnColumn = res.nColumn; + if( pnRow ) *pnRow = res.nRow; + return rc; +} + +/* +** This routine frees the space the sqlite3_get_table() malloced. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_free_table( + char **azResult /* Result returned from sqlite3_get_table() */ +){ + if( azResult ){ + int i, n; + azResult--; + assert( azResult!=0 ); + n = SQLITE_PTR_TO_INT(azResult[0]); + for(i=1; ipNext; + + sqlite3ExprDelete(db, pTmp->pWhere); + sqlite3ExprListDelete(db, pTmp->pExprList); + sqlite3SelectDelete(db, pTmp->pSelect); + sqlite3IdListDelete(db, pTmp->pIdList); + + sqlite3DbFree(db, pTmp); + } +} + +/* +** Given table pTab, return a list of all the triggers attached to +** the table. The list is connected by Trigger.pNext pointers. +** +** All of the triggers on pTab that are in the same database as pTab +** are already attached to pTab->pTrigger. But there might be additional +** triggers on pTab in the TEMP schema. This routine prepends all +** TEMP triggers on pTab to the beginning of the pTab->pTrigger list +** and returns the combined list. +** +** To state it another way: This routine returns a list of all triggers +** that fire off of pTab. The list will include any TEMP triggers on +** pTab as well as the triggers lised in pTab->pTrigger. +*/ +SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ + Schema * const pTmpSchema = pParse->db->aDb[1].pSchema; + Trigger *pList = 0; /* List of triggers to return */ + + if( pParse->disableTriggers ){ + return 0; + } + + if( pTmpSchema!=pTab->pSchema ){ + HashElem *p; + assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) ); + for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){ + Trigger *pTrig = (Trigger *)sqliteHashData(p); + if( pTrig->pTabSchema==pTab->pSchema + && 0==sqlite3StrICmp(pTrig->table, pTab->zName) + ){ + pTrig->pNext = (pList ? pList : pTab->pTrigger); + pList = pTrig; + } + } + } + + return (pList ? pList : pTab->pTrigger); +} + +/* +** This is called by the parser when it sees a CREATE TRIGGER statement +** up to the point of the BEGIN before the trigger actions. A Trigger +** structure is generated based on the information available and stored +** in pParse->pNewTrigger. After the trigger actions have been parsed, the +** sqlite3FinishTrigger() function is called to complete the trigger +** construction process. +*/ +SQLITE_PRIVATE void sqlite3BeginTrigger( + Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ + Token *pName1, /* The name of the trigger */ + Token *pName2, /* The name of the trigger */ + int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ + int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ + IdList *pColumns, /* column list if this is an UPDATE OF trigger */ + SrcList *pTableName,/* The name of the table/view the trigger applies to */ + Expr *pWhen, /* WHEN clause */ + int isTemp, /* True if the TEMPORARY keyword is present */ + int noErr /* Suppress errors if the trigger already exists */ +){ + Trigger *pTrigger = 0; /* The new trigger */ + Table *pTab; /* Table that the trigger fires off of */ + char *zName = 0; /* Name of the trigger */ + sqlite3 *db = pParse->db; /* The database connection */ + int iDb; /* The database to store the trigger in */ + Token *pName; /* The unqualified db name */ + DbFixer sFix; /* State vector for the DB fixer */ + int iTabDb; /* Index of the database holding pTab */ + + assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */ + assert( pName2!=0 ); + assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE ); + assert( op>0 && op<0xff ); + if( isTemp ){ + /* If TEMP was specified, then the trigger name may not be qualified. */ + if( pName2->n>0 ){ + sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); + goto trigger_cleanup; + } + iDb = 1; + pName = pName1; + }else{ + /* Figure out the db that the trigger will be created in */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ){ + goto trigger_cleanup; + } + } + if( !pTableName || db->mallocFailed ){ + goto trigger_cleanup; + } + + /* A long-standing parser bug is that this syntax was allowed: + ** + ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab .... + ** ^^^^^^^^ + ** + ** To maintain backwards compatibility, ignore the database + ** name on pTableName if we are reparsing out of SQLITE_MASTER. + */ + if( db->init.busy && iDb!=1 ){ + sqlite3DbFree(db, pTableName->a[0].zDatabase); + pTableName->a[0].zDatabase = 0; + } + + /* If the trigger name was unqualified, and the table is a temp table, + ** then set iDb to 1 to create the trigger in the temporary database. + ** If sqlite3SrcListLookup() returns 0, indicating the table does not + ** exist, the error is caught by the block below. + */ + pTab = sqlite3SrcListLookup(pParse, pTableName); + if( db->init.busy==0 && pName2->n==0 && pTab + && pTab->pSchema==db->aDb[1].pSchema ){ + iDb = 1; + } + + /* Ensure the table name matches database name and that the table exists */ + if( db->mallocFailed ) goto trigger_cleanup; + assert( pTableName->nSrc==1 ); + sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName); + if( sqlite3FixSrcList(&sFix, pTableName) ){ + goto trigger_cleanup; + } + pTab = sqlite3SrcListLookup(pParse, pTableName); + if( !pTab ){ + /* The table does not exist. */ + if( db->init.iDb==1 ){ + /* Ticket #3810. + ** Normally, whenever a table is dropped, all associated triggers are + ** dropped too. But if a TEMP trigger is created on a non-TEMP table + ** and the table is dropped by a different database connection, the + ** trigger is not visible to the database connection that does the + ** drop so the trigger cannot be dropped. This results in an + ** "orphaned trigger" - a trigger whose associated table is missing. + */ + db->init.orphanTrigger = 1; + } + goto trigger_cleanup; + } + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables"); + goto trigger_cleanup; + } + + /* Check that the trigger name is not reserved and that no trigger of the + ** specified name exists */ + zName = sqlite3NameFromToken(db, pName); + if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto trigger_cleanup; + } + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); + }else{ + assert( !db->init.busy ); + sqlite3CodeVerifySchema(pParse, iDb); + } + goto trigger_cleanup; + } + + /* Do not create a trigger on a system table */ + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ + sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); + goto trigger_cleanup; + } + + /* INSTEAD of triggers are only for views and views only support INSTEAD + ** of triggers. + */ + if( pTab->pSelect && tr_tm!=TK_INSTEAD ){ + sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", + (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); + goto trigger_cleanup; + } + if( !pTab->pSelect && tr_tm==TK_INSTEAD ){ + sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" + " trigger on table: %S", pTableName, 0); + goto trigger_cleanup; + } + iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); + +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_CREATE_TRIGGER; + const char *zDb = db->aDb[iTabDb].zName; + const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; + if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; + if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ + goto trigger_cleanup; + } + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){ + goto trigger_cleanup; + } + } +#endif + + /* INSTEAD OF triggers can only appear on views and BEFORE triggers + ** cannot appear on views. So we might as well translate every + ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code + ** elsewhere. + */ + if (tr_tm == TK_INSTEAD){ + tr_tm = TK_BEFORE; + } + + /* Build the Trigger object */ + pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger)); + if( pTrigger==0 ) goto trigger_cleanup; + pTrigger->zName = zName; + zName = 0; + pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName); + pTrigger->pSchema = db->aDb[iDb].pSchema; + pTrigger->pTabSchema = pTab->pSchema; + pTrigger->op = (u8)op; + pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; + pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); + pTrigger->pColumns = sqlite3IdListDup(db, pColumns); + assert( pParse->pNewTrigger==0 ); + pParse->pNewTrigger = pTrigger; + +trigger_cleanup: + sqlite3DbFree(db, zName); + sqlite3SrcListDelete(db, pTableName); + sqlite3IdListDelete(db, pColumns); + sqlite3ExprDelete(db, pWhen); + if( !pParse->pNewTrigger ){ + sqlite3DeleteTrigger(db, pTrigger); + }else{ + assert( pParse->pNewTrigger==pTrigger ); + } +} + +/* +** This routine is called after all of the trigger actions have been parsed +** in order to complete the process of building the trigger. +*/ +SQLITE_PRIVATE void sqlite3FinishTrigger( + Parse *pParse, /* Parser context */ + TriggerStep *pStepList, /* The triggered program */ + Token *pAll /* Token that describes the complete CREATE TRIGGER */ +){ + Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */ + char *zName; /* Name of trigger */ + sqlite3 *db = pParse->db; /* The database */ + DbFixer sFix; /* Fixer object */ + int iDb; /* Database containing the trigger */ + Token nameToken; /* Trigger name for error reporting */ + + pParse->pNewTrigger = 0; + if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; + zName = pTrig->zName; + iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); + pTrig->step_list = pStepList; + while( pStepList ){ + pStepList->pTrig = pTrig; + pStepList = pStepList->pNext; + } + sqlite3TokenInit(&nameToken, pTrig->zName); + sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken); + if( sqlite3FixTriggerStep(&sFix, pTrig->step_list) + || sqlite3FixExpr(&sFix, pTrig->pWhen) + ){ + goto triggerfinish_cleanup; + } + + /* if we are not initializing, + ** build the sqlite_master entry + */ + if( !db->init.busy ){ + Vdbe *v; + char *z; + + /* Make an entry in the sqlite_master table */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto triggerfinish_cleanup; + sqlite3BeginWriteOperation(pParse, 0, iDb); + z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); + sqlite3NestedParse(pParse, + "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, + pTrig->table, z); + sqlite3DbFree(db, z); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddParseSchemaOp(v, iDb, + sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName)); + } + + if( db->init.busy ){ + Trigger *pLink = pTrig; + Hash *pHash = &db->aDb[iDb].pSchema->trigHash; + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pTrig = sqlite3HashInsert(pHash, zName, pTrig); + if( pTrig ){ + sqlite3OomFault(db); + }else if( pLink->pSchema==pLink->pTabSchema ){ + Table *pTab; + pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table); + assert( pTab!=0 ); + pLink->pNext = pTab->pTrigger; + pTab->pTrigger = pLink; + } + } + +triggerfinish_cleanup: + sqlite3DeleteTrigger(db, pTrig); + assert( !pParse->pNewTrigger ); + sqlite3DeleteTriggerStep(db, pStepList); +} + +/* +** Turn a SELECT statement (that the pSelect parameter points to) into +** a trigger step. Return a pointer to a TriggerStep structure. +** +** The parser calls this routine when it finds a SELECT statement in +** body of a TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); + if( pTriggerStep==0 ) { + sqlite3SelectDelete(db, pSelect); + return 0; + } + pTriggerStep->op = TK_SELECT; + pTriggerStep->pSelect = pSelect; + pTriggerStep->orconf = OE_Default; + return pTriggerStep; +} + +/* +** Allocate space to hold a new trigger step. The allocated space +** holds both the TriggerStep object and the TriggerStep.target.z string. +** +** If an OOM error occurs, NULL is returned and db->mallocFailed is set. +*/ +static TriggerStep *triggerStepAllocate( + sqlite3 *db, /* Database connection */ + u8 op, /* Trigger opcode */ + Token *pName /* The target name */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1); + if( pTriggerStep ){ + char *z = (char*)&pTriggerStep[1]; + memcpy(z, pName->z, pName->n); + sqlite3Dequote(z); + pTriggerStep->zTarget = z; + pTriggerStep->op = op; + } + return pTriggerStep; +} + +/* +** Build a trigger step out of an INSERT statement. Return a pointer +** to the new trigger step. +** +** The parser calls this routine when it sees an INSERT inside the +** body of a trigger. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep( + sqlite3 *db, /* The database connection */ + Token *pTableName, /* Name of the table into which we insert */ + IdList *pColumn, /* List of columns in pTableName to insert into */ + Select *pSelect, /* A SELECT statement that supplies values */ + u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ +){ + TriggerStep *pTriggerStep; + + assert(pSelect != 0 || db->mallocFailed); + + pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); + if( pTriggerStep ){ + pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + pTriggerStep->pIdList = pColumn; + pTriggerStep->orconf = orconf; + }else{ + sqlite3IdListDelete(db, pColumn); + } + sqlite3SelectDelete(db, pSelect); + + return pTriggerStep; +} + +/* +** Construct a trigger step that implements an UPDATE statement and return +** a pointer to that trigger step. The parser calls this routine when it +** sees an UPDATE statement inside the body of a CREATE TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep( + sqlite3 *db, /* The database connection */ + Token *pTableName, /* Name of the table to be updated */ + ExprList *pEList, /* The SET clause: list of column and new values */ + Expr *pWhere, /* The WHERE clause */ + u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName); + if( pTriggerStep ){ + pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pTriggerStep->orconf = orconf; + } + sqlite3ExprListDelete(db, pEList); + sqlite3ExprDelete(db, pWhere); + return pTriggerStep; +} + +/* +** Construct a trigger step that implements a DELETE statement and return +** a pointer to that trigger step. The parser calls this routine when it +** sees a DELETE statement inside the body of a CREATE TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep( + sqlite3 *db, /* Database connection */ + Token *pTableName, /* The table from which rows are deleted */ + Expr *pWhere /* The WHERE clause */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName); + if( pTriggerStep ){ + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pTriggerStep->orconf = OE_Default; + } + sqlite3ExprDelete(db, pWhere); + return pTriggerStep; +} + +/* +** Recursively delete a Trigger structure +*/ +SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){ + if( pTrigger==0 ) return; + sqlite3DeleteTriggerStep(db, pTrigger->step_list); + sqlite3DbFree(db, pTrigger->zName); + sqlite3DbFree(db, pTrigger->table); + sqlite3ExprDelete(db, pTrigger->pWhen); + sqlite3IdListDelete(db, pTrigger->pColumns); + sqlite3DbFree(db, pTrigger); +} + +/* +** This function is called to drop a trigger from the database schema. +** +** This may be called directly from the parser and therefore identifies +** the trigger by name. The sqlite3DropTriggerPtr() routine does the +** same job as this routine except it takes a pointer to the trigger +** instead of the trigger name. +**/ +SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ + Trigger *pTrigger = 0; + int i; + const char *zDb; + const char *zName; + sqlite3 *db = pParse->db; + + if( db->mallocFailed ) goto drop_trigger_cleanup; + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto drop_trigger_cleanup; + } + + assert( pName->nSrc==1 ); + zDb = pName->a[0].zDatabase; + zName = pName->a[0].zName; + assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); + for(i=OMIT_TEMPDB; inDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; + assert( sqlite3SchemaMutexHeld(db, j, 0) ); + pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName); + if( pTrigger ) break; + } + if( !pTrigger ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); + }else{ + sqlite3CodeVerifyNamedSchema(pParse, zDb); + } + pParse->checkSchema = 1; + goto drop_trigger_cleanup; + } + sqlite3DropTriggerPtr(pParse, pTrigger); + +drop_trigger_cleanup: + sqlite3SrcListDelete(db, pName); +} + +/* +** Return a pointer to the Table structure for the table that a trigger +** is set on. +*/ +static Table *tableOfTrigger(Trigger *pTrigger){ + return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table); +} + + +/* +** Drop a trigger given a pointer to that trigger. +*/ +SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ + Table *pTable; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); + assert( iDb>=0 && iDbnDb ); + pTable = tableOfTrigger(pTrigger); + assert( pTable ); + assert( pTable->pSchema==pTrigger->pSchema || iDb==1 ); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_DROP_TRIGGER; + const char *zDb = db->aDb[iDb].zName; + const char *zTab = SCHEMA_TABLE(iDb); + if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; + if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || + sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ + return; + } + } +#endif + + /* Generate code to destroy the database record of the trigger. + */ + assert( pTable!=0 ); + if( (v = sqlite3GetVdbe(pParse))!=0 ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName + ); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); + } +} + +/* +** Remove a trigger from the hash tables of the sqlite* pointer. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ + Trigger *pTrigger; + Hash *pHash; + + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + pHash = &(db->aDb[iDb].pSchema->trigHash); + pTrigger = sqlite3HashInsert(pHash, zName, 0); + if( ALWAYS(pTrigger) ){ + if( pTrigger->pSchema==pTrigger->pTabSchema ){ + Table *pTab = tableOfTrigger(pTrigger); + Trigger **pp; + for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); + *pp = (*pp)->pNext; + } + sqlite3DeleteTrigger(db, pTrigger); + db->flags |= SQLITE_InternChanges; + } +} + +/* +** pEList is the SET clause of an UPDATE statement. Each entry +** in pEList is of the format =. If any of the entries +** in pEList have an which matches an identifier in pIdList, +** then return TRUE. If pIdList==NULL, then it is considered a +** wildcard that matches anything. Likewise if pEList==NULL then +** it matches anything so always return true. Return false only +** if there is no match. +*/ +static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ + int e; + if( pIdList==0 || NEVER(pEList==0) ) return 1; + for(e=0; enExpr; e++){ + if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; + } + return 0; +} + +/* +** Return a list of all triggers on table pTab if there exists at least +** one trigger that must be fired when an operation of type 'op' is +** performed on the table, and, if that operation is an UPDATE, if at +** least one of the columns in pChanges is being modified. +*/ +SQLITE_PRIVATE Trigger *sqlite3TriggersExist( + Parse *pParse, /* Parse context */ + Table *pTab, /* The table the contains the triggers */ + int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ + ExprList *pChanges, /* Columns that change in an UPDATE statement */ + int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ +){ + int mask = 0; + Trigger *pList = 0; + Trigger *p; + + if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){ + pList = sqlite3TriggerList(pParse, pTab); + } + assert( pList==0 || IsVirtual(pTab)==0 ); + for(p=pList; p; p=p->pNext){ + if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ + mask |= p->tr_tm; + } + } + if( pMask ){ + *pMask = mask; + } + return (mask ? pList : 0); +} + +/* +** Convert the pStep->zTarget string into a SrcList and return a pointer +** to that SrcList. +** +** This routine adds a specific database name, if needed, to the target when +** forming the SrcList. This prevents a trigger in one database from +** referring to a target in another database. An exception is when the +** trigger is in TEMP in which case it can refer to any other database it +** wants. +*/ +static SrcList *targetSrcList( + Parse *pParse, /* The parsing context */ + TriggerStep *pStep /* The trigger containing the target token */ +){ + sqlite3 *db = pParse->db; + int iDb; /* Index of the database to use */ + SrcList *pSrc; /* SrcList to be returned */ + + pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + if( pSrc ){ + assert( pSrc->nSrc>0 ); + pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget); + iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema); + if( iDb==0 || iDb>=2 ){ + assert( iDbnDb ); + pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); + } + } + return pSrc; +} + +/* +** Generate VDBE code for the statements inside the body of a single +** trigger. +*/ +static int codeTriggerProgram( + Parse *pParse, /* The parser context */ + TriggerStep *pStepList, /* List of statements inside the trigger body */ + int orconf /* Conflict algorithm. (OE_Abort, etc) */ +){ + TriggerStep *pStep; + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + + assert( pParse->pTriggerTab && pParse->pToplevel ); + assert( pStepList ); + assert( v!=0 ); + for(pStep=pStepList; pStep; pStep=pStep->pNext){ + /* Figure out the ON CONFLICT policy that will be used for this step + ** of the trigger program. If the statement that caused this trigger + ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use + ** the ON CONFLICT policy that was specified as part of the trigger + ** step statement. Example: + ** + ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; + ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); + ** END; + ** + ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy + ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy + */ + pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; + assert( pParse->okConstFactor==0 ); + + switch( pStep->op ){ + case TK_UPDATE: { + sqlite3Update(pParse, + targetSrcList(pParse, pStep), + sqlite3ExprListDup(db, pStep->pExprList, 0), + sqlite3ExprDup(db, pStep->pWhere, 0), + pParse->eOrconf + ); + break; + } + case TK_INSERT: { + sqlite3Insert(pParse, + targetSrcList(pParse, pStep), + sqlite3SelectDup(db, pStep->pSelect, 0), + sqlite3IdListDup(db, pStep->pIdList), + pParse->eOrconf + ); + break; + } + case TK_DELETE: { + sqlite3DeleteFrom(pParse, + targetSrcList(pParse, pStep), + sqlite3ExprDup(db, pStep->pWhere, 0) + ); + break; + } + default: assert( pStep->op==TK_SELECT ); { + SelectDest sDest; + Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0); + sqlite3SelectDestInit(&sDest, SRT_Discard, 0); + sqlite3Select(pParse, pSelect, &sDest); + sqlite3SelectDelete(db, pSelect); + break; + } + } + if( pStep->op!=TK_SELECT ){ + sqlite3VdbeAddOp0(v, OP_ResetCount); + } + } + + return 0; +} + +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS +/* +** This function is used to add VdbeComment() annotations to a VDBE +** program. It is not used in production code, only for debugging. +*/ +static const char *onErrorText(int onError){ + switch( onError ){ + case OE_Abort: return "abort"; + case OE_Rollback: return "rollback"; + case OE_Fail: return "fail"; + case OE_Replace: return "replace"; + case OE_Ignore: return "ignore"; + case OE_Default: return "default"; + } + return "n/a"; +} +#endif + +/* +** Parse context structure pFrom has just been used to create a sub-vdbe +** (trigger program). If an error has occurred, transfer error information +** from pFrom to pTo. +*/ +static void transferParseError(Parse *pTo, Parse *pFrom){ + assert( pFrom->zErrMsg==0 || pFrom->nErr ); + assert( pTo->zErrMsg==0 || pTo->nErr ); + if( pTo->nErr==0 ){ + pTo->zErrMsg = pFrom->zErrMsg; + pTo->nErr = pFrom->nErr; + pTo->rc = pFrom->rc; + }else{ + sqlite3DbFree(pFrom->db, pFrom->zErrMsg); + } +} + +/* +** Create and populate a new TriggerPrg object with a sub-program +** implementing trigger pTrigger with ON CONFLICT policy orconf. +*/ +static TriggerPrg *codeRowTrigger( + Parse *pParse, /* Current parse context */ + Trigger *pTrigger, /* Trigger to code */ + Table *pTab, /* The table pTrigger is attached to */ + int orconf /* ON CONFLICT policy to code trigger program with */ +){ + Parse *pTop = sqlite3ParseToplevel(pParse); + sqlite3 *db = pParse->db; /* Database handle */ + TriggerPrg *pPrg; /* Value to return */ + Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ + Vdbe *v; /* Temporary VM */ + NameContext sNC; /* Name context for sub-vdbe */ + SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ + Parse *pSubParse; /* Parse context for sub-vdbe */ + int iEndTrigger = 0; /* Label to jump to if WHEN is false */ + + assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); + assert( pTop->pVdbe ); + + /* Allocate the TriggerPrg and SubProgram objects. To ensure that they + ** are freed if an error occurs, link them into the Parse.pTriggerPrg + ** list of the top-level Parse object sooner rather than later. */ + pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); + if( !pPrg ) return 0; + pPrg->pNext = pTop->pTriggerPrg; + pTop->pTriggerPrg = pPrg; + pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); + if( !pProgram ) return 0; + sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram); + pPrg->pTrigger = pTrigger; + pPrg->orconf = orconf; + pPrg->aColmask[0] = 0xffffffff; + pPrg->aColmask[1] = 0xffffffff; + + /* Allocate and populate a new Parse context to use for coding the + ** trigger sub-program. */ + pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); + if( !pSubParse ) return 0; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pSubParse; + pSubParse->db = db; + pSubParse->pTriggerTab = pTab; + pSubParse->pToplevel = pTop; + pSubParse->zAuthContext = pTrigger->zName; + pSubParse->eTriggerOp = pTrigger->op; + pSubParse->nQueryLoop = pParse->nQueryLoop; + + v = sqlite3GetVdbe(pSubParse); + if( v ){ + VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", + pTrigger->zName, onErrorText(orconf), + (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), + (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), + (pTrigger->op==TK_INSERT ? "INSERT" : ""), + (pTrigger->op==TK_DELETE ? "DELETE" : ""), + pTab->zName + )); +#ifndef SQLITE_OMIT_TRACE + sqlite3VdbeChangeP4(v, -1, + sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC + ); +#endif + + /* If one was specified, code the WHEN clause. If it evaluates to false + ** (or NULL) the sub-vdbe is immediately halted by jumping to the + ** OP_Halt inserted at the end of the program. */ + if( pTrigger->pWhen ){ + pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); + if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) + && db->mallocFailed==0 + ){ + iEndTrigger = sqlite3VdbeMakeLabel(v); + sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); + } + sqlite3ExprDelete(db, pWhen); + } + + /* Code the trigger program into the sub-vdbe. */ + codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); + + /* Insert an OP_Halt at the end of the sub-program. */ + if( iEndTrigger ){ + sqlite3VdbeResolveLabel(v, iEndTrigger); + } + sqlite3VdbeAddOp0(v, OP_Halt); + VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); + + transferParseError(pParse, pSubParse); + if( db->mallocFailed==0 ){ + pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); + } + pProgram->nMem = pSubParse->nMem; + pProgram->nCsr = pSubParse->nTab; + pProgram->nOnce = pSubParse->nOnce; + pProgram->token = (void *)pTrigger; + pPrg->aColmask[0] = pSubParse->oldmask; + pPrg->aColmask[1] = pSubParse->newmask; + sqlite3VdbeDelete(v); + } + + assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); + assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); + sqlite3ParserReset(pSubParse); + sqlite3StackFree(db, pSubParse); + + return pPrg; +} + +/* +** Return a pointer to a TriggerPrg object containing the sub-program for +** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such +** TriggerPrg object exists, a new object is allocated and populated before +** being returned. +*/ +static TriggerPrg *getRowTrigger( + Parse *pParse, /* Current parse context */ + Trigger *pTrigger, /* Trigger to code */ + Table *pTab, /* The table trigger pTrigger is attached to */ + int orconf /* ON CONFLICT algorithm. */ +){ + Parse *pRoot = sqlite3ParseToplevel(pParse); + TriggerPrg *pPrg; + + assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); + + /* It may be that this trigger has already been coded (or is in the + ** process of being coded). If this is the case, then an entry with + ** a matching TriggerPrg.pTrigger field will be present somewhere + ** in the Parse.pTriggerPrg list. Search for such an entry. */ + for(pPrg=pRoot->pTriggerPrg; + pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); + pPrg=pPrg->pNext + ); + + /* If an existing TriggerPrg could not be located, create a new one. */ + if( !pPrg ){ + pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); + } + + return pPrg; +} + +/* +** Generate code for the trigger program associated with trigger p on +** table pTab. The reg, orconf and ignoreJump parameters passed to this +** function are the same as those described in the header function for +** sqlite3CodeRowTrigger() +*/ +SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect( + Parse *pParse, /* Parse context */ + Trigger *p, /* Trigger to code */ + Table *pTab, /* The table to code triggers from */ + int reg, /* Reg array containing OLD.* and NEW.* values */ + int orconf, /* ON CONFLICT policy */ + int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ +){ + Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ + TriggerPrg *pPrg; + pPrg = getRowTrigger(pParse, p, pTab, orconf); + assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); + + /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program + ** is a pointer to the sub-vdbe containing the trigger program. */ + if( pPrg ){ + int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); + + sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem, + (const char *)pPrg->pProgram, P4_SUBPROGRAM); + VdbeComment( + (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); + + /* Set the P5 operand of the OP_Program instruction to non-zero if + ** recursive invocation of this trigger program is disallowed. Recursive + ** invocation is disallowed if (a) the sub-program is really a trigger, + ** not a foreign key action, and (b) the flag to enable recursive triggers + ** is clear. */ + sqlite3VdbeChangeP5(v, (u8)bRecursive); + } +} + +/* +** This is called to code the required FOR EACH ROW triggers for an operation +** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) +** is given by the op parameter. The tr_tm parameter determines whether the +** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then +** parameter pChanges is passed the list of columns being modified. +** +** If there are no triggers that fire at the specified time for the specified +** operation on pTab, this function is a no-op. +** +** The reg argument is the address of the first in an array of registers +** that contain the values substituted for the new.* and old.* references +** in the trigger program. If N is the number of columns in table pTab +** (a copy of pTab->nCol), then registers are populated as follows: +** +** Register Contains +** ------------------------------------------------------ +** reg+0 OLD.rowid +** reg+1 OLD.* value of left-most column of pTab +** ... ... +** reg+N OLD.* value of right-most column of pTab +** reg+N+1 NEW.rowid +** reg+N+2 OLD.* value of left-most column of pTab +** ... ... +** reg+N+N+1 NEW.* value of right-most column of pTab +** +** For ON DELETE triggers, the registers containing the NEW.* values will +** never be accessed by the trigger program, so they are not allocated or +** populated by the caller (there is no data to populate them with anyway). +** Similarly, for ON INSERT triggers the values stored in the OLD.* registers +** are never accessed, and so are not allocated by the caller. So, for an +** ON INSERT trigger, the value passed to this function as parameter reg +** is not a readable register, although registers (reg+N) through +** (reg+N+N+1) are. +** +** Parameter orconf is the default conflict resolution algorithm for the +** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump +** is the instruction that control should jump to if a trigger program +** raises an IGNORE exception. +*/ +SQLITE_PRIVATE void sqlite3CodeRowTrigger( + Parse *pParse, /* Parse context */ + Trigger *pTrigger, /* List of triggers on table pTab */ + int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ + ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ + int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ + Table *pTab, /* The table to code triggers from */ + int reg, /* The first in an array of registers (see above) */ + int orconf, /* ON CONFLICT policy */ + int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ +){ + Trigger *p; /* Used to iterate through pTrigger list */ + + assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE ); + assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER ); + assert( (op==TK_UPDATE)==(pChanges!=0) ); + + for(p=pTrigger; p; p=p->pNext){ + + /* Sanity checking: The schema for the trigger and for the table are + ** always defined. The trigger must be in the same schema as the table + ** or else it must be a TEMP trigger. */ + assert( p->pSchema!=0 ); + assert( p->pTabSchema!=0 ); + assert( p->pSchema==p->pTabSchema + || p->pSchema==pParse->db->aDb[1].pSchema ); + + /* Determine whether we should code this trigger */ + if( p->op==op + && p->tr_tm==tr_tm + && checkColumnOverlap(p->pColumns, pChanges) + ){ + sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); + } + } +} + +/* +** Triggers may access values stored in the old.* or new.* pseudo-table. +** This function returns a 32-bit bitmask indicating which columns of the +** old.* or new.* tables actually are used by triggers. This information +** may be used by the caller, for example, to avoid having to load the entire +** old.* record into memory when executing an UPDATE or DELETE command. +** +** Bit 0 of the returned mask is set if the left-most column of the +** table may be accessed using an [old|new].reference. Bit 1 is set if +** the second leftmost column value is required, and so on. If there +** are more than 32 columns in the table, and at least one of the columns +** with an index greater than 32 may be accessed, 0xffffffff is returned. +** +** It is not possible to determine if the old.rowid or new.rowid column is +** accessed by triggers. The caller must always assume that it is. +** +** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned +** applies to the old.* table. If 1, the new.* table. +** +** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE +** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only +** included in the returned mask if the TRIGGER_BEFORE bit is set in the +** tr_tm parameter. Similarly, values accessed by AFTER triggers are only +** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. +*/ +SQLITE_PRIVATE u32 sqlite3TriggerColmask( + Parse *pParse, /* Parse context */ + Trigger *pTrigger, /* List of triggers on table pTab */ + ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ + int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */ + int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ + Table *pTab, /* The table to code triggers from */ + int orconf /* Default ON CONFLICT policy for trigger steps */ +){ + const int op = pChanges ? TK_UPDATE : TK_DELETE; + u32 mask = 0; + Trigger *p; + + assert( isNew==1 || isNew==0 ); + for(p=pTrigger; p; p=p->pNext){ + if( p->op==op && (tr_tm&p->tr_tm) + && checkColumnOverlap(p->pColumns,pChanges) + ){ + TriggerPrg *pPrg; + pPrg = getRowTrigger(pParse, p, pTab, orconf); + if( pPrg ){ + mask |= pPrg->aColmask[isNew]; + } + } + } + + return mask; +} + +#endif /* !defined(SQLITE_OMIT_TRIGGER) */ + +/************** End of trigger.c *********************************************/ +/************** Begin file update.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle UPDATE statements. +*/ +/* #include "sqliteInt.h" */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Forward declaration */ +static void updateVirtualTable( + Parse *pParse, /* The parsing context */ + SrcList *pSrc, /* The virtual table to be modified */ + Table *pTab, /* The virtual table */ + ExprList *pChanges, /* The columns to change in the UPDATE statement */ + Expr *pRowidExpr, /* Expression used to recompute the rowid */ + int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ + Expr *pWhere, /* WHERE clause of the UPDATE statement */ + int onError /* ON CONFLICT strategy */ +); +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** The most recently coded instruction was an OP_Column to retrieve the +** i-th column of table pTab. This routine sets the P4 parameter of the +** OP_Column to the default value, if any. +** +** The default value of a column is specified by a DEFAULT clause in the +** column definition. This was either supplied by the user when the table +** was created, or added later to the table definition by an ALTER TABLE +** command. If the latter, then the row-records in the table btree on disk +** may not contain a value for the column and the default value, taken +** from the P4 parameter of the OP_Column instruction, is returned instead. +** If the former, then all row-records are guaranteed to include a value +** for the column and the P4 value is not required. +** +** Column definitions created by an ALTER TABLE command may only have +** literal default values specified: a number, null or a string. (If a more +** complicated default expression value was provided, it is evaluated +** when the ALTER TABLE is executed and one of the literal values written +** into the sqlite_master table.) +** +** Therefore, the P4 parameter is only required if the default value for +** the column is a literal number, string or null. The sqlite3ValueFromExpr() +** function is capable of transforming these types of expressions into +** sqlite3_value objects. +** +** If parameter iReg is not negative, code an OP_RealAffinity instruction +** on register iReg. This is used when an equivalent integer value is +** stored in place of an 8-byte floating point value in order to save +** space. +*/ +SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ + assert( pTab!=0 ); + if( !pTab->pSelect ){ + sqlite3_value *pValue = 0; + u8 enc = ENC(sqlite3VdbeDb(v)); + Column *pCol = &pTab->aCol[i]; + VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); + assert( inCol ); + sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, + pCol->affinity, &pValue); + if( pValue ){ + sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); + } +#ifndef SQLITE_OMIT_FLOATING_POINT + if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); + } +#endif + } +} + +/* +** Process an UPDATE statement. +** +** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; +** \_______/ \________/ \______/ \________________/ +* onError pTabList pChanges pWhere +*/ +SQLITE_PRIVATE void sqlite3Update( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* The table in which we should change things */ + ExprList *pChanges, /* Things to be changed */ + Expr *pWhere, /* The WHERE clause. May be null */ + int onError /* How to handle constraint errors */ +){ + int i, j; /* Loop counters */ + Table *pTab; /* The table to be updated */ + int addrTop = 0; /* VDBE instruction address of the start of the loop */ + WhereInfo *pWInfo; /* Information about the WHERE clause */ + Vdbe *v; /* The virtual database engine */ + Index *pIdx; /* For looping over indices */ + Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ + int nIdx; /* Number of indices that need updating */ + int iBaseCur; /* Base cursor number */ + int iDataCur; /* Cursor for the canonical data btree */ + int iIdxCur; /* Cursor for the first index */ + sqlite3 *db; /* The database structure */ + int *aRegIdx = 0; /* One register assigned to each index to be updated */ + int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the + ** an expression for the i-th column of the table. + ** aXRef[i]==-1 if the i-th column is not changed. */ + u8 *aToOpen; /* 1 for tables and indices to be opened */ + u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ + u8 chngRowid; /* Rowid changed in a normal table */ + u8 chngKey; /* Either chngPk or chngRowid */ + Expr *pRowidExpr = 0; /* Expression defining the new record number */ + AuthContext sContext; /* The authorization context */ + NameContext sNC; /* The name-context to resolve expressions in */ + int iDb; /* Database containing the table being updated */ + int okOnePass; /* True for one-pass algorithm without the FIFO */ + int hasFK; /* True if foreign key processing is required */ + int labelBreak; /* Jump here to break out of UPDATE loop */ + int labelContinue; /* Jump here to continue next step of UPDATE loop */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True when updating a view (INSTEAD OF trigger) */ + Trigger *pTrigger; /* List of triggers on pTab, if required */ + int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ +#endif + int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ + int iEph = 0; /* Ephemeral table holding all primary key values */ + int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ + int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ + + /* Register Allocations */ + int regRowCount = 0; /* A count of rows changed */ + int regOldRowid = 0; /* The old rowid */ + int regNewRowid = 0; /* The new rowid */ + int regNew = 0; /* Content of the NEW.* table in triggers */ + int regOld = 0; /* Content of OLD.* table in triggers */ + int regRowSet = 0; /* Rowset of rows to be updated */ + int regKey = 0; /* composite PRIMARY KEY value */ + + memset(&sContext, 0, sizeof(sContext)); + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ + goto update_cleanup; + } + assert( pTabList->nSrc==1 ); + + /* Locate the table which we want to update. + */ + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ) goto update_cleanup; + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + + /* Figure out if we have any triggers and if the table being + ** updated is a view. + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask); + isView = pTab->pSelect!=0; + assert( pTrigger || tmask==0 ); +#else +# define pTrigger 0 +# define isView 0 +# define tmask 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto update_cleanup; + } + if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + goto update_cleanup; + } + + /* Allocate a cursors for the main database table and for all indices. + ** The index cursors might not be used, but if they are used they + ** need to occur right after the database cursor. So go ahead and + ** allocate enough space, just in case. + */ + pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++; + iIdxCur = iDataCur+1; + pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ + if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){ + iDataCur = pParse->nTab; + pTabList->a[0].iCursor = iDataCur; + } + pParse->nTab++; + } + + /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. + ** Initialize aXRef[] and aToOpen[] to their default values. + */ + aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 ); + if( aXRef==0 ) goto update_cleanup; + aRegIdx = aXRef+pTab->nCol; + aToOpen = (u8*)(aRegIdx+nIdx); + memset(aToOpen, 1, nIdx+1); + aToOpen[nIdx+1] = 0; + for(i=0; inCol; i++) aXRef[i] = -1; + + /* Initialize the name-context */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + + /* Resolve the column names in all the expressions of the + ** of the UPDATE statement. Also find the column index + ** for each column to be updated in the pChanges array. For each + ** column to be updated, make sure we have authorization to change + ** that column. + */ + chngRowid = chngPk = 0; + for(i=0; inExpr; i++){ + if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ + goto update_cleanup; + } + for(j=0; jnCol; j++){ + if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ + if( j==pTab->iPKey ){ + chngRowid = 1; + pRowidExpr = pChanges->a[i].pExpr; + }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){ + chngPk = 1; + } + aXRef[j] = i; + break; + } + } + if( j>=pTab->nCol ){ + if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){ + j = -1; + chngRowid = 1; + pRowidExpr = pChanges->a[i].pExpr; + }else{ + sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); + pParse->checkSchema = 1; + goto update_cleanup; + } + } +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int rc; + rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, + j<0 ? "ROWID" : pTab->aCol[j].zName, + db->aDb[iDb].zName); + if( rc==SQLITE_DENY ){ + goto update_cleanup; + }else if( rc==SQLITE_IGNORE ){ + aXRef[j] = -1; + } + } +#endif + } + assert( (chngRowid & chngPk)==0 ); + assert( chngRowid==0 || chngRowid==1 ); + assert( chngPk==0 || chngPk==1 ); + chngKey = chngRowid + chngPk; + + /* The SET expressions are not actually used inside the WHERE loop. + ** So reset the colUsed mask. Unless this is a virtual table. In that + ** case, set all bits of the colUsed mask (to ensure that the virtual + ** table implementation makes all columns available). + */ + pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0; + + hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); + + /* There is one entry in the aRegIdx[] array for each index on the table + ** being updated. Fill in aRegIdx[] with a register number that will hold + ** the key for accessing each index. + ** + ** FIXME: Be smarter about omitting indexes that use expressions. + */ + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int reg; + if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ + reg = ++pParse->nMem; + }else{ + reg = 0; + for(i=0; inKeyCol; i++){ + i16 iIdxCol = pIdx->aiColumn[i]; + if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ + reg = ++pParse->nMem; + break; + } + } + } + if( reg==0 ) aToOpen[j+1] = 0; + aRegIdx[j] = reg; + } + + /* Begin generating code. */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto update_cleanup; + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, 1, iDb); + + /* Allocate required registers. */ + if( !IsVirtual(pTab) ){ + regRowSet = ++pParse->nMem; + regOldRowid = regNewRowid = ++pParse->nMem; + if( chngPk || pTrigger || hasFK ){ + regOld = pParse->nMem + 1; + pParse->nMem += pTab->nCol; + } + if( chngKey || pTrigger || hasFK ){ + regNewRowid = ++pParse->nMem; + } + regNew = pParse->nMem + 1; + pParse->nMem += pTab->nCol; + } + + /* Start the view context. */ + if( isView ){ + sqlite3AuthContextPush(pParse, &sContext, pTab->zName); + } + + /* If we are trying to update a view, realize that view into + ** an ephemeral table. + */ +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) + if( isView ){ + sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur); + } +#endif + + /* Resolve the column names in all the expressions in the + ** WHERE clause. + */ + if( sqlite3ResolveExprNames(&sNC, pWhere) ){ + goto update_cleanup; + } + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Virtual tables must be handled separately */ + if( IsVirtual(pTab) ){ + updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, + pWhere, onError); + goto update_cleanup; + } +#endif + + /* Begin the database scan + */ + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); + pWInfo = sqlite3WhereBegin( + pParse, pTabList, pWhere, 0, 0, + WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE, iIdxCur + ); + if( pWInfo==0 ) goto update_cleanup; + okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); + + /* Remember the rowid of every item to be updated. + */ + sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); + if( !okOnePass ){ + sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); + } + + /* End the database scan loop. + */ + sqlite3WhereEnd(pWInfo); + }else{ + int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ + i16 nPk; /* Number of components of the PRIMARY KEY */ + int addrOpen; /* Address of the OpenEphemeral instruction */ + + assert( pPk!=0 ); + nPk = pPk->nKeyCol; + iPk = pParse->nMem+1; + pParse->nMem += nPk; + regKey = ++pParse->nMem; + iEph = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); + addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, + WHERE_ONEPASS_DESIRED, iIdxCur); + if( pWInfo==0 ) goto update_cleanup; + okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); + for(i=0; iaiColumn[i]>=0 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], + iPk+i); + } + if( okOnePass ){ + sqlite3VdbeChangeToNoop(v, addrOpen); + nKey = nPk; + regKey = iPk; + }else{ + sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, + sqlite3IndexAffinityStr(db, pPk), nPk); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); + } + sqlite3WhereEnd(pWInfo); + } + + /* Initialize the count of updated rows + */ + if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); + } + + labelBreak = sqlite3VdbeMakeLabel(v); + if( !isView ){ + /* + ** Open every index that needs updating. Note that if any + ** index could potentially invoke a REPLACE conflict resolution + ** action, then we need to open all indices because we might need + ** to be deleting some records. + */ + if( onError==OE_Replace ){ + memset(aToOpen, 1, nIdx+1); + }else{ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->onError==OE_Replace ){ + memset(aToOpen, 1, nIdx+1); + break; + } + } + } + if( okOnePass ){ + if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; + if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; + } + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen, + 0, 0); + } + + /* Top of the update loop */ + if( okOnePass ){ + if( aToOpen[iDataCur-iBaseCur] && !isView ){ + assert( pPk ); + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); + VdbeCoverageNeverTaken(v); + } + labelContinue = labelBreak; + sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); + VdbeCoverageIf(v, pPk==0); + VdbeCoverageIf(v, pPk!=0); + }else if( pPk ){ + labelContinue = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v); + addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey); + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0); + VdbeCoverage(v); + }else{ + labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak, + regOldRowid); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); + VdbeCoverage(v); + } + + /* If the record number will change, set register regNewRowid to + ** contain the new value. If the record number is not being modified, + ** then regNewRowid is the same register as regOldRowid, which is + ** already populated. */ + assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid ); + if( chngRowid ){ + sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v); + } + + /* Compute the old pre-UPDATE content of the row being changed, if that + ** information is needed */ + if( chngPk || hasFK || pTrigger ){ + u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); + oldmask |= sqlite3TriggerColmask(pParse, + pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError + ); + for(i=0; inCol; i++){ + if( oldmask==0xffffffff + || (i<32 && (oldmask & MASKBIT32(i))!=0) + || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 + ){ + testcase( oldmask!=0xffffffff && i==31 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i); + } + } + if( chngRowid==0 && pPk==0 ){ + sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid); + } + } + + /* Populate the array of registers beginning at regNew with the new + ** row data. This array is used to check constants, create the new + ** table and index records, and as the values for any new.* references + ** made by triggers. + ** + ** If there are one or more BEFORE triggers, then do not populate the + ** registers associated with columns that are (a) not modified by + ** this UPDATE statement and (b) not accessed by new.* references. The + ** values for registers not modified by the UPDATE must be reloaded from + ** the database after the BEFORE triggers are fired anyway (as the trigger + ** may have modified them). So not loading those that are not going to + ** be used eliminates some redundant opcodes. + */ + newmask = sqlite3TriggerColmask( + pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError + ); + for(i=0; inCol; i++){ + if( i==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); + }else{ + j = aXRef[i]; + if( j>=0 ){ + sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); + }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){ + /* This branch loads the value of a column that will not be changed + ** into a register. This is done if there are no BEFORE triggers, or + ** if there are one or more BEFORE triggers that use this value via + ** a new.* reference in a trigger program. + */ + testcase( i==31 ); + testcase( i==32 ); + sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); + } + } + } + + /* Fire any BEFORE UPDATE triggers. This happens before constraints are + ** verified. One could argue that this is wrong. + */ + if( tmask&TRIGGER_BEFORE ){ + sqlite3TableAffinity(v, pTab, regNew); + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue); + + /* The row-trigger may have deleted the row being updated. In this + ** case, jump to the next row. No updates or AFTER triggers are + ** required. This behavior - what happens when the row being updated + ** is deleted or renamed by a BEFORE trigger - is left undefined in the + ** documentation. + */ + if( pPk ){ + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey); + VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); + VdbeCoverage(v); + } + + /* If it did not delete it, the row-trigger may still have modified + ** some of the columns of the row being updated. Load the values for + ** all columns not modified by the update statement into their + ** registers in case this has happened. + */ + for(i=0; inCol; i++){ + if( aXRef[i]<0 && i!=pTab->iPKey ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); + } + } + } + + if( !isView ){ + int addr1 = 0; /* Address of jump instruction */ + int bReplace = 0; /* True if REPLACE conflict resolution might happen */ + + /* Do constraint checks. */ + assert( regOldRowid>0 ); + sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, + regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace, + aXRef); + + /* Do FK constraint checks. */ + if( hasFK ){ + sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey); + } + + /* Delete the index entries associated with the current record. */ + if( bReplace || chngKey ){ + if( pPk ){ + addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey); + }else{ + addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid); + } + VdbeCoverageNeverTaken(v); + } + sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1); + + /* If changing the rowid value, or if there are foreign key constraints + ** to process, delete the old record. Otherwise, add a noop OP_Delete + ** to invoke the pre-update hook. + ** + ** That (regNew==regnewRowid+1) is true is also important for the + ** pre-update hook. If the caller invokes preupdate_new(), the returned + ** value is copied from memory cell (regNewRowid+1+iCol), where iCol + ** is the column index supplied by the user. + */ + assert( regNew==regNewRowid+1 ); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + sqlite3VdbeAddOp3(v, OP_Delete, iDataCur, + OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP), + regNewRowid + ); + if( !pParse->nested ){ + sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE); + } +#else + if( hasFK || chngKey || pPk!=0 ){ + sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); + } +#endif + if( bReplace || chngKey ){ + sqlite3VdbeJumpHere(v, addr1); + } + + if( hasFK ){ + sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); + } + + /* Insert the new index entries and the new record. */ + sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, + regNewRowid, aRegIdx, 1, 0, 0); + + /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to + ** handle rows (possibly in other tables) that refer via a foreign key + ** to the row just updated. */ + if( hasFK ){ + sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey); + } + } + + /* Increment the row counter + */ + if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){ + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); + } + + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue); + + /* Repeat the above with the next record to be updated, until + ** all record selected by the WHERE clause have been updated. + */ + if( okOnePass ){ + /* Nothing to do at end-of-loop for a single-pass */ + }else if( pPk ){ + sqlite3VdbeResolveLabel(v, labelContinue); + sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); + }else{ + sqlite3VdbeGoto(v, labelContinue); + } + sqlite3VdbeResolveLabel(v, labelBreak); + + /* Close all tables */ + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + assert( aRegIdx ); + if( aToOpen[i+1] ){ + sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); + } + } + if( iDataCurnested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* + ** Return the number of rows that were changed. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); + } + +update_cleanup: + sqlite3AuthContextPop(&sContext); + sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */ + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprListDelete(db, pChanges); + sqlite3ExprDelete(db, pWhere); + return; +} +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** they may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Generate code for an UPDATE of a virtual table. +** +** There are two possible strategies - the default and the special +** "onepass" strategy. Onepass is only used if the virtual table +** implementation indicates that pWhere may match at most one row. +** +** The default strategy is to create an ephemeral table that contains +** for each row to be changed: +** +** (A) The original rowid of that row. +** (B) The revised rowid for the row. +** (C) The content of every column in the row. +** +** Then loop through the contents of this ephemeral table executing a +** VUpdate for each row. When finished, drop the ephemeral table. +** +** The "onepass" strategy does not use an ephemeral table. Instead, it +** stores the same values (A, B and C above) in a register array and +** makes a single invocation of VUpdate. +*/ +static void updateVirtualTable( + Parse *pParse, /* The parsing context */ + SrcList *pSrc, /* The virtual table to be modified */ + Table *pTab, /* The virtual table */ + ExprList *pChanges, /* The columns to change in the UPDATE statement */ + Expr *pRowid, /* Expression used to recompute the rowid */ + int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ + Expr *pWhere, /* WHERE clause of the UPDATE statement */ + int onError /* ON CONFLICT strategy */ +){ + Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ + int ephemTab; /* Table holding the result of the SELECT */ + int i; /* Loop counter */ + sqlite3 *db = pParse->db; /* Database connection */ + const char *pVTab = (const char*)sqlite3GetVTable(db, pTab); + WhereInfo *pWInfo; + int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */ + int regArg; /* First register in VUpdate arg array */ + int regRec; /* Register in which to assemble record */ + int regRowid; /* Register for ephem table rowid */ + int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */ + int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */ + int bOnePass; /* True to use onepass strategy */ + int addr; /* Address of OP_OpenEphemeral */ + + /* Allocate nArg registers to martial the arguments to VUpdate. Then + ** create and open the ephemeral table in which the records created from + ** these arguments will be temporarily stored. */ + assert( v ); + ephemTab = pParse->nTab++; + addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg); + regArg = pParse->nMem + 1; + pParse->nMem += nArg; + regRec = ++pParse->nMem; + regRowid = ++pParse->nMem; + + /* Start scanning the virtual table */ + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0); + if( pWInfo==0 ) return; + + /* Populate the argument registers. */ + sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg); + if( pRowid ){ + sqlite3ExprCode(pParse, pRowid, regArg+1); + }else{ + sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1); + } + for(i=0; inCol; i++){ + if( aXRef[i]>=0 ){ + sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i); + }else{ + sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i); + } + } + + bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); + + if( bOnePass ){ + /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded + ** above. Also, if this is a top-level parse (not a trigger), clear the + ** multi-write flag so that the VM does not open a statement journal */ + sqlite3VdbeChangeToNoop(v, addr); + if( sqlite3IsToplevel(pParse) ){ + pParse->isMultiWrite = 0; + } + }else{ + /* Create a record from the argument register contents and insert it into + ** the ephemeral table. */ + sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec); + sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid); + } + + + if( bOnePass==0 ){ + /* End the virtual table scan */ + sqlite3WhereEnd(pWInfo); + + /* Begin scannning through the ephemeral table. */ + addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v); + + /* Extract arguments from the current row of the ephemeral table and + ** invoke the VUpdate method. */ + for(i=0; iflags&SQLITE_CountRows) ); + return vacuumFinalize(db, pStmt, pzErrMsg); +} + +/* +** Execute zSql on database db. The statement returns exactly +** one column. Execute this as SQL on the same database. +*/ +static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + int rc; + + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); + if( rc!=SQLITE_OK ){ + vacuumFinalize(db, pStmt, pzErrMsg); + return rc; + } + } + + return vacuumFinalize(db, pStmt, pzErrMsg); +} + +/* +** The VACUUM command is used to clean up the database, +** collapse free space, etc. It is modelled after the VACUUM command +** in PostgreSQL. The VACUUM command works as follows: +** +** (1) Create a new transient database file +** (2) Copy all content from the database being vacuumed into +** the new transient database file +** (3) Copy content from the transient database back into the +** original database. +** +** The transient database requires temporary disk space approximately +** equal to the size of the original database. The copy operation of +** step (3) requires additional temporary disk space approximately equal +** to the size of the original database for the rollback journal. +** Hence, temporary disk space that is approximately 2x the size of the +** original database is required. Every page of the database is written +** approximately 3 times: Once for step (2) and twice for step (3). +** Two writes per page are required in step (3) because the original +** database content must be written into the rollback journal prior to +** overwriting the database with the vacuumed content. +** +** Only 1x temporary space and only 1x writes would be required if +** the copy of step (3) were replaced by deleting the original database +** and renaming the transient database as the original. But that will +** not work if other processes are attached to the original database. +** And a power loss in between deleting the original and renaming the +** transient would cause the database file to appear to be deleted +** following reboot. +*/ +SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); + sqlite3VdbeUsesBtree(v, 0); + } + return; +} + +/* +** This routine implements the OP_Vacuum opcode of the VDBE. +*/ +SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ + int rc = SQLITE_OK; /* Return code from service routines */ + Btree *pMain; /* The database being vacuumed */ + Btree *pTemp; /* The temporary database we vacuum into */ + char *zSql = 0; /* SQL statements */ + int saved_flags; /* Saved value of the db->flags */ + int saved_nChange; /* Saved value of db->nChange */ + int saved_nTotalChange; /* Saved value of db->nTotalChange */ + void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ + Db *pDb = 0; /* Database to detach at end of vacuum */ + int isMemDb; /* True if vacuuming a :memory: database */ + int nRes; /* Bytes of reserved space at the end of each page */ + int nDb; /* Number of attached databases */ + + if( !db->autoCommit ){ + sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); + return SQLITE_ERROR; + } + if( db->nVdbeActive>1 ){ + sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); + return SQLITE_ERROR; + } + + /* Save the current value of the database flags so that it can be + ** restored before returning. Then set the writable-schema flag, and + ** disable CHECK and foreign key constraints. */ + saved_flags = db->flags; + saved_nChange = db->nChange; + saved_nTotalChange = db->nTotalChange; + saved_xTrace = db->xTrace; + db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; + db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); + db->xTrace = 0; + + pMain = db->aDb[0].pBt; + isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain)); + + /* Attach the temporary database as 'vacuum_db'. The synchronous pragma + ** can be set to 'off' for this file, as it is not recovered if a crash + ** occurs anyway. The integrity of the database is maintained by a + ** (possibly synchronous) transaction opened on the main database before + ** sqlite3BtreeCopyFile() is called. + ** + ** An optimisation would be to use a non-journaled pager. + ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but + ** that actually made the VACUUM run slower. Very little journalling + ** actually occurs when doing a vacuum since the vacuum_db is initially + ** empty. Only the journal header is written. Apparently it takes more + ** time to parse and run the PRAGMA to turn journalling off than it does + ** to write the journal header file. + */ + nDb = db->nDb; + if( sqlite3TempInMemory(db) ){ + zSql = "ATTACH ':memory:' AS vacuum_db;"; + }else{ + zSql = "ATTACH '' AS vacuum_db;"; + } + rc = execSql(db, pzErrMsg, zSql); + if( db->nDb>nDb ){ + pDb = &db->aDb[db->nDb-1]; + assert( strcmp(pDb->zName,"vacuum_db")==0 ); + } + if( rc!=SQLITE_OK ) goto end_of_vacuum; + pTemp = db->aDb[db->nDb-1].pBt; + + /* The call to execSql() to attach the temp database has left the file + ** locked (as there was more than one active statement when the transaction + ** to read the schema was concluded. Unlock it here so that this doesn't + ** cause problems for the call to BtreeSetPageSize() below. */ + sqlite3BtreeCommit(pTemp); + + nRes = sqlite3BtreeGetOptimalReserve(pMain); + + /* A VACUUM cannot change the pagesize of an encrypted database. */ +#ifdef SQLITE_HAS_CODEC + if( db->nextPagesize ){ + extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + int nKey; + char *zKey; + sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); + if( nKey ) db->nextPagesize = 0; + } +#endif + + rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF"); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Begin a transaction and take an exclusive lock on the main database + ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below, + ** to ensure that we do not try to change the page-size on a WAL database. + */ + rc = execSql(db, pzErrMsg, "BEGIN;"); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = sqlite3BtreeBeginTrans(pMain, 2); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Do not attempt to change the page size for a WAL database */ + if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) + ==PAGER_JOURNALMODE_WAL ){ + db->nextPagesize = 0; + } + + if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) + || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) + || NEVER(db->mallocFailed) + ){ + rc = SQLITE_NOMEM_BKPT; + goto end_of_vacuum; + } + +#ifndef SQLITE_OMIT_AUTOVACUUM + sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac : + sqlite3BtreeGetAutoVacuum(pMain)); +#endif + + /* Query the schema of the main database. Create a mirror schema + ** in the temporary database. + */ + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " + " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" + " AND coalesce(rootpage,1)>0" + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" + " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' "); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " + " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'"); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Loop through the tables in the main database. For each, do + ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy + ** the contents to the temporary database. + */ + assert( (db->flags & SQLITE_Vacuum)==0 ); + db->flags |= SQLITE_Vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'INSERT INTO vacuum_db.' || quote(name) " + "|| ' SELECT * FROM main.' || quote(name) || ';'" + "FROM main.sqlite_master " + "WHERE type = 'table' AND name!='sqlite_sequence' " + " AND coalesce(rootpage,1)>0" + ); + assert( (db->flags & SQLITE_Vacuum)!=0 ); + db->flags &= ~SQLITE_Vacuum; + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Copy over the sequence table + */ + rc = execExecSql(db, pzErrMsg, + "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' " + "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' " + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'INSERT INTO vacuum_db.' || quote(name) " + "|| ' SELECT * FROM main.' || quote(name) || ';' " + "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';" + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + + /* Copy the triggers, views, and virtual tables from the main database + ** over to the temporary database. None of these objects has any + ** associated storage, so all we have to do is copy their entries + ** from the SQLITE_MASTER table. + */ + rc = execSql(db, pzErrMsg, + "INSERT INTO vacuum_db.sqlite_master " + " SELECT type, name, tbl_name, rootpage, sql" + " FROM main.sqlite_master" + " WHERE type='view' OR type='trigger'" + " OR (type='table' AND rootpage=0)" + ); + if( rc ) goto end_of_vacuum; + + /* At this point, there is a write transaction open on both the + ** vacuum database and the main database. Assuming no error occurs, + ** both transactions are closed by this block - the main database + ** transaction by sqlite3BtreeCopyFile() and the other by an explicit + ** call to sqlite3BtreeCommit(). + */ + { + u32 meta; + int i; + + /* This array determines which meta meta values are preserved in the + ** vacuum. Even entries are the meta value number and odd entries + ** are an increment to apply to the meta value after the vacuum. + ** The increment is used to increase the schema cookie so that other + ** connections to the same database will know to reread the schema. + */ + static const unsigned char aCopy[] = { + BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */ + BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */ + BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */ + BTREE_USER_VERSION, 0, /* Preserve the user version */ + BTREE_APPLICATION_ID, 0, /* Preserve the application id */ + }; + + assert( 1==sqlite3BtreeIsInTrans(pTemp) ); + assert( 1==sqlite3BtreeIsInTrans(pMain) ); + + /* Copy Btree meta values */ + for(i=0; iflags */ + db->flags = saved_flags; + db->nChange = saved_nChange; + db->nTotalChange = saved_nTotalChange; + db->xTrace = saved_xTrace; + sqlite3BtreeSetPageSize(pMain, -1, -1, 1); + + /* Currently there is an SQL level transaction open on the vacuum + ** database. No locks are held on any other files (since the main file + ** was committed at the btree level). So it safe to end the transaction + ** by manually setting the autoCommit flag to true and detaching the + ** vacuum database. The vacuum_db journal file is deleted when the pager + ** is closed by the DETACH. + */ + db->autoCommit = 1; + + if( pDb ){ + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + pDb->pSchema = 0; + } + + /* This both clears the schemas and reduces the size of the db->aDb[] + ** array. */ + sqlite3ResetAllSchemasOfConnection(db); + + return rc; +} + +#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */ + +/************** End of vacuum.c **********************************************/ +/************** Begin file vtab.c ********************************************/ +/* +** 2006 June 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to help implement virtual tables. +*/ +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* #include "sqliteInt.h" */ + +/* +** Before a virtual table xCreate() or xConnect() method is invoked, the +** sqlite3.pVtabCtx member variable is set to point to an instance of +** this struct allocated on the stack. It is used by the implementation of +** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which +** are invoked only from within xCreate and xConnect methods. +*/ +struct VtabCtx { + VTable *pVTable; /* The virtual table being constructed */ + Table *pTab; /* The Table object to which the virtual table belongs */ + VtabCtx *pPrior; /* Parent context (if any) */ + int bDeclared; /* True after sqlite3_declare_vtab() is called */ +}; + +/* +** The actual function that does the work of creating a new module. +** This function implements the sqlite3_create_module() and +** sqlite3_create_module_v2() interfaces. +*/ +static int createModule( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux, /* Context pointer for xCreate/xConnect */ + void (*xDestroy)(void *) /* Module destructor function */ +){ + int rc = SQLITE_OK; + int nName; + + sqlite3_mutex_enter(db->mutex); + nName = sqlite3Strlen30(zName); + if( sqlite3HashFind(&db->aModule, zName) ){ + rc = SQLITE_MISUSE_BKPT; + }else{ + Module *pMod; + pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1); + if( pMod ){ + Module *pDel; + char *zCopy = (char *)(&pMod[1]); + memcpy(zCopy, zName, nName+1); + pMod->zName = zCopy; + pMod->pModule = pModule; + pMod->pAux = pAux; + pMod->xDestroy = xDestroy; + pMod->pEpoTab = 0; + pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); + assert( pDel==0 || pDel==pMod ); + if( pDel ){ + sqlite3OomFault(db); + sqlite3DbFree(db, pDel); + } + } + } + rc = sqlite3ApiExit(db, rc); + if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); + + sqlite3_mutex_leave(db->mutex); + return rc; +} + + +/* +** External API function used to create a new virtual-table module. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_module( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux /* Context pointer for xCreate/xConnect */ +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; +#endif + return createModule(db, zName, pModule, pAux, 0); +} + +/* +** External API function used to create a new virtual-table module. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux, /* Context pointer for xCreate/xConnect */ + void (*xDestroy)(void *) /* Module destructor function */ +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; +#endif + return createModule(db, zName, pModule, pAux, xDestroy); +} + +/* +** Lock the virtual table so that it cannot be disconnected. +** Locks nest. Every lock should have a corresponding unlock. +** If an unlock is omitted, resources leaks will occur. +** +** If a disconnect is attempted while a virtual table is locked, +** the disconnect is deferred until all locks have been removed. +*/ +SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){ + pVTab->nRef++; +} + + +/* +** pTab is a pointer to a Table structure representing a virtual-table. +** Return a pointer to the VTable object used by connection db to access +** this virtual-table, if one has been created, or NULL otherwise. +*/ +SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){ + VTable *pVtab; + assert( IsVirtual(pTab) ); + for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); + return pVtab; +} + +/* +** Decrement the ref-count on a virtual table object. When the ref-count +** reaches zero, call the xDisconnect() method to delete the object. +*/ +SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){ + sqlite3 *db = pVTab->db; + + assert( db ); + assert( pVTab->nRef>0 ); + assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE ); + + pVTab->nRef--; + if( pVTab->nRef==0 ){ + sqlite3_vtab *p = pVTab->pVtab; + if( p ){ + p->pModule->xDisconnect(p); + } + sqlite3DbFree(db, pVTab); + } +} + +/* +** Table p is a virtual table. This function moves all elements in the +** p->pVTable list to the sqlite3.pDisconnect lists of their associated +** database connections to be disconnected at the next opportunity. +** Except, if argument db is not NULL, then the entry associated with +** connection db is left in the p->pVTable list. +*/ +static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ + VTable *pRet = 0; + VTable *pVTable = p->pVTable; + p->pVTable = 0; + + /* Assert that the mutex (if any) associated with the BtShared database + ** that contains table p is held by the caller. See header comments + ** above function sqlite3VtabUnlockList() for an explanation of why + ** this makes it safe to access the sqlite3.pDisconnect list of any + ** database connection that may have an entry in the p->pVTable list. + */ + assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); + + while( pVTable ){ + sqlite3 *db2 = pVTable->db; + VTable *pNext = pVTable->pNext; + assert( db2 ); + if( db2==db ){ + pRet = pVTable; + p->pVTable = pRet; + pRet->pNext = 0; + }else{ + pVTable->pNext = db2->pDisconnect; + db2->pDisconnect = pVTable; + } + pVTable = pNext; + } + + assert( !db || pRet ); + return pRet; +} + +/* +** Table *p is a virtual table. This function removes the VTable object +** for table *p associated with database connection db from the linked +** list in p->pVTab. It also decrements the VTable ref count. This is +** used when closing database connection db to free all of its VTable +** objects without disturbing the rest of the Schema object (which may +** be being used by other shared-cache connections). +*/ +SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ + VTable **ppVTab; + + assert( IsVirtual(p) ); + assert( sqlite3BtreeHoldsAllMutexes(db) ); + assert( sqlite3_mutex_held(db->mutex) ); + + for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){ + if( (*ppVTab)->db==db ){ + VTable *pVTab = *ppVTab; + *ppVTab = pVTab->pNext; + sqlite3VtabUnlock(pVTab); + break; + } + } +} + + +/* +** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. +** +** This function may only be called when the mutexes associated with all +** shared b-tree databases opened using connection db are held by the +** caller. This is done to protect the sqlite3.pDisconnect list. The +** sqlite3.pDisconnect list is accessed only as follows: +** +** 1) By this function. In this case, all BtShared mutexes and the mutex +** associated with the database handle itself must be held. +** +** 2) By function vtabDisconnectAll(), when it adds a VTable entry to +** the sqlite3.pDisconnect list. In this case either the BtShared mutex +** associated with the database the virtual table is stored in is held +** or, if the virtual table is stored in a non-sharable database, then +** the database handle mutex is held. +** +** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously +** by multiple threads. It is thread-safe. +*/ +SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){ + VTable *p = db->pDisconnect; + db->pDisconnect = 0; + + assert( sqlite3BtreeHoldsAllMutexes(db) ); + assert( sqlite3_mutex_held(db->mutex) ); + + if( p ){ + sqlite3ExpirePreparedStatements(db); + do { + VTable *pNext = p->pNext; + sqlite3VtabUnlock(p); + p = pNext; + }while( p ); + } +} + +/* +** Clear any and all virtual-table information from the Table record. +** This routine is called, for example, just before deleting the Table +** record. +** +** Since it is a virtual-table, the Table structure contains a pointer +** to the head of a linked list of VTable structures. Each VTable +** structure is associated with a single sqlite3* user of the schema. +** The reference count of the VTable structure associated with database +** connection db is decremented immediately (which may lead to the +** structure being xDisconnected and free). Any other VTable structures +** in the list are moved to the sqlite3.pDisconnect list of the associated +** database connection. +*/ +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){ + if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); + if( p->azModuleArg ){ + int i; + for(i=0; inModuleArg; i++){ + if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]); + } + sqlite3DbFree(db, p->azModuleArg); + } +} + +/* +** Add a new module argument to pTable->azModuleArg[]. +** The string is not copied - the pointer is stored. The +** string will be freed automatically when the table is +** deleted. +*/ +static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ + int nBytes = sizeof(char *)*(2+pTable->nModuleArg); + char **azModuleArg; + azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); + if( azModuleArg==0 ){ + sqlite3DbFree(db, zArg); + }else{ + int i = pTable->nModuleArg++; + azModuleArg[i] = zArg; + azModuleArg[i+1] = 0; + pTable->azModuleArg = azModuleArg; + } +} + +/* +** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE +** statement. The module name has been parsed, but the optional list +** of parameters that follow the module name are still pending. +*/ +SQLITE_PRIVATE void sqlite3VtabBeginParse( + Parse *pParse, /* Parsing context */ + Token *pName1, /* Name of new table, or database name */ + Token *pName2, /* Name of new table or NULL */ + Token *pModuleName, /* Name of the module for the virtual table */ + int ifNotExists /* No error if the table already exists */ +){ + int iDb; /* The database the table is being created in */ + Table *pTable; /* The new virtual table */ + sqlite3 *db; /* Database connection */ + + sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists); + pTable = pParse->pNewTable; + if( pTable==0 ) return; + assert( 0==pTable->pIndex ); + + db = pParse->db; + iDb = sqlite3SchemaToIndex(db, pTable->pSchema); + assert( iDb>=0 ); + + pTable->tabFlags |= TF_Virtual; + pTable->nModuleArg = 0; + addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); + addModuleArgument(db, pTable, 0); + addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); + assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) + || (pParse->sNameToken.z==pName1->z && pName2->z==0) + ); + pParse->sNameToken.n = (int)( + &pModuleName->z[pModuleName->n] - pParse->sNameToken.z + ); + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Creating a virtual table invokes the authorization callback twice. + ** The first invocation, to obtain permission to INSERT a row into the + ** sqlite_master table, has already been made by sqlite3StartTable(). + ** The second call, to obtain permission to create the table, is made now. + */ + if( pTable->azModuleArg ){ + sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, + pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); + } +#endif +} + +/* +** This routine takes the module argument that has been accumulating +** in pParse->zArg[] and appends it to the list of arguments on the +** virtual table currently under construction in pParse->pTable. +*/ +static void addArgumentToVtab(Parse *pParse){ + if( pParse->sArg.z && pParse->pNewTable ){ + const char *z = (const char*)pParse->sArg.z; + int n = pParse->sArg.n; + sqlite3 *db = pParse->db; + addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); + } +} + +/* +** The parser calls this routine after the CREATE VIRTUAL TABLE statement +** has been completely parsed. +*/ +SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ + Table *pTab = pParse->pNewTable; /* The table being constructed */ + sqlite3 *db = pParse->db; /* The database connection */ + + if( pTab==0 ) return; + addArgumentToVtab(pParse); + pParse->sArg.z = 0; + if( pTab->nModuleArg<1 ) return; + + /* If the CREATE VIRTUAL TABLE statement is being entered for the + ** first time (in other words if the virtual table is actually being + ** created now instead of just being read out of sqlite_master) then + ** do additional initialization work and store the statement text + ** in the sqlite_master table. + */ + if( !db->init.busy ){ + char *zStmt; + char *zWhere; + int iDb; + int iReg; + Vdbe *v; + + /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ + if( pEnd ){ + pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; + } + zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); + + /* A slot for the record has already been allocated in the + ** SQLITE_MASTER table. We just need to update that slot with all + ** the information we've collected. + ** + ** The VM register number pParse->regRowid holds the rowid of an + ** entry in the sqlite_master table tht was created for this vtab + ** by sqlite3StartTable(). + */ + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + sqlite3NestedParse(pParse, + "UPDATE %Q.%s " + "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " + "WHERE rowid=#%d", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + pTab->zName, + pTab->zName, + zStmt, + pParse->regRowid + ); + sqlite3DbFree(db, zStmt); + v = sqlite3GetVdbe(pParse); + sqlite3ChangeCookie(pParse, iDb); + + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); + zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); + sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); + + iReg = ++pParse->nMem; + sqlite3VdbeLoadString(v, iReg, pTab->zName); + sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); + } + + /* If we are rereading the sqlite_master table create the in-memory + ** record of the table. The xConnect() method is not called until + ** the first time the virtual table is used in an SQL statement. This + ** allows a schema that contains virtual tables to be loaded before + ** the required virtual table implementations are registered. */ + else { + Table *pOld; + Schema *pSchema = pTab->pSchema; + const char *zName = pTab->zName; + assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); + pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab); + if( pOld ){ + sqlite3OomFault(db); + assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ + return; + } + pParse->pNewTable = 0; + } +} + +/* +** The parser calls this routine when it sees the first token +** of an argument to the module name in a CREATE VIRTUAL TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){ + addArgumentToVtab(pParse); + pParse->sArg.z = 0; + pParse->sArg.n = 0; +} + +/* +** The parser calls this routine for each token after the first token +** in an argument to the module name in a CREATE VIRTUAL TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){ + Token *pArg = &pParse->sArg; + if( pArg->z==0 ){ + pArg->z = p->z; + pArg->n = p->n; + }else{ + assert(pArg->z <= p->z); + pArg->n = (int)(&p->z[p->n] - pArg->z); + } +} + +/* +** Invoke a virtual table constructor (either xCreate or xConnect). The +** pointer to the function to invoke is passed as the fourth parameter +** to this procedure. +*/ +static int vtabCallConstructor( + sqlite3 *db, + Table *pTab, + Module *pMod, + int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), + char **pzErr +){ + VtabCtx sCtx; + VTable *pVTable; + int rc; + const char *const*azArg = (const char *const*)pTab->azModuleArg; + int nArg = pTab->nModuleArg; + char *zErr = 0; + char *zModuleName; + int iDb; + VtabCtx *pCtx; + + /* Check that the virtual-table is not already being initialized */ + for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){ + if( pCtx->pTab==pTab ){ + *pzErr = sqlite3MPrintf(db, + "vtable constructor called recursively: %s", pTab->zName + ); + return SQLITE_LOCKED; + } + } + + zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); + if( !zModuleName ){ + return SQLITE_NOMEM_BKPT; + } + + pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); + if( !pVTable ){ + sqlite3DbFree(db, zModuleName); + return SQLITE_NOMEM_BKPT; + } + pVTable->db = db; + pVTable->pMod = pMod; + + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + pTab->azModuleArg[1] = db->aDb[iDb].zName; + + /* Invoke the virtual table constructor */ + assert( &db->pVtabCtx ); + assert( xConstruct ); + sCtx.pTab = pTab; + sCtx.pVTable = pVTable; + sCtx.pPrior = db->pVtabCtx; + sCtx.bDeclared = 0; + db->pVtabCtx = &sCtx; + rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); + db->pVtabCtx = sCtx.pPrior; + if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); + assert( sCtx.pTab==pTab ); + + if( SQLITE_OK!=rc ){ + if( zErr==0 ){ + *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); + }else { + *pzErr = sqlite3MPrintf(db, "%s", zErr); + sqlite3_free(zErr); + } + sqlite3DbFree(db, pVTable); + }else if( ALWAYS(pVTable->pVtab) ){ + /* Justification of ALWAYS(): A correct vtab constructor must allocate + ** the sqlite3_vtab object if successful. */ + memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); + pVTable->pVtab->pModule = pMod->pModule; + pVTable->nRef = 1; + if( sCtx.bDeclared==0 ){ + const char *zFormat = "vtable constructor did not declare schema: %s"; + *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); + sqlite3VtabUnlock(pVTable); + rc = SQLITE_ERROR; + }else{ + int iCol; + u8 oooHidden = 0; + /* If everything went according to plan, link the new VTable structure + ** into the linked list headed by pTab->pVTable. Then loop through the + ** columns of the table to see if any of them contain the token "hidden". + ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from + ** the type string. */ + pVTable->pNext = pTab->pVTable; + pTab->pVTable = pVTable; + + for(iCol=0; iColnCol; iCol++){ + char *zType = sqlite3ColumnType(&pTab->aCol[iCol], ""); + int nType; + int i = 0; + nType = sqlite3Strlen30(zType); + for(i=0; i0 ){ + assert(zType[i-1]==' '); + zType[i-1] = '\0'; + } + pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; + oooHidden = TF_OOOHidden; + }else{ + pTab->tabFlags |= oooHidden; + } + } + } + } + + sqlite3DbFree(db, zModuleName); + return rc; +} + +/* +** This function is invoked by the parser to call the xConnect() method +** of the virtual table pTab. If an error occurs, an error code is returned +** and an error left in pParse. +** +** This call is a no-op if table pTab is not a virtual table. +*/ +SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ + sqlite3 *db = pParse->db; + const char *zMod; + Module *pMod; + int rc; + + assert( pTab ); + if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ + return SQLITE_OK; + } + + /* Locate the required virtual table module */ + zMod = pTab->azModuleArg[0]; + pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); + + if( !pMod ){ + const char *zModule = pTab->azModuleArg[0]; + sqlite3ErrorMsg(pParse, "no such module: %s", zModule); + rc = SQLITE_ERROR; + }else{ + char *zErr = 0; + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); + if( rc!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "%s", zErr); + } + sqlite3DbFree(db, zErr); + } + + return rc; +} +/* +** Grow the db->aVTrans[] array so that there is room for at least one +** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise. +*/ +static int growVTrans(sqlite3 *db){ + const int ARRAY_INCR = 5; + + /* Grow the sqlite3.aVTrans array if required */ + if( (db->nVTrans%ARRAY_INCR)==0 ){ + VTable **aVTrans; + int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); + aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); + if( !aVTrans ){ + return SQLITE_NOMEM_BKPT; + } + memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); + db->aVTrans = aVTrans; + } + + return SQLITE_OK; +} + +/* +** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should +** have already been reserved using growVTrans(). +*/ +static void addToVTrans(sqlite3 *db, VTable *pVTab){ + /* Add pVtab to the end of sqlite3.aVTrans */ + db->aVTrans[db->nVTrans++] = pVTab; + sqlite3VtabLock(pVTab); +} + +/* +** This function is invoked by the vdbe to call the xCreate method +** of the virtual table named zTab in database iDb. +** +** If an error occurs, *pzErr is set to point an an English language +** description of the error and an SQLITE_XXX error code is returned. +** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. +*/ +SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ + int rc = SQLITE_OK; + Table *pTab; + Module *pMod; + const char *zMod; + + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); + assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); + + /* Locate the required virtual table module */ + zMod = pTab->azModuleArg[0]; + pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); + + /* If the module has been registered and includes a Create method, + ** invoke it now. If the module has not been registered, return an + ** error. Otherwise, do nothing. + */ + if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){ + *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); + rc = SQLITE_ERROR; + }else{ + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); + } + + /* Justification of ALWAYS(): The xConstructor method is required to + ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ + if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ + rc = growVTrans(db); + if( rc==SQLITE_OK ){ + addToVTrans(db, sqlite3GetVTable(db, pTab)); + } + } + + return rc; +} + +/* +** This function is used to set the schema of a virtual table. It is only +** valid to call this function from within the xCreate() or xConnect() of a +** virtual table module. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ + VtabCtx *pCtx; + Parse *pParse; + int rc = SQLITE_OK; + Table *pTab; + char *zErr = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + pCtx = db->pVtabCtx; + if( !pCtx || pCtx->bDeclared ){ + sqlite3Error(db, SQLITE_MISUSE); + sqlite3_mutex_leave(db->mutex); + return SQLITE_MISUSE_BKPT; + } + pTab = pCtx->pTab; + assert( (pTab->tabFlags & TF_Virtual)!=0 ); + + pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); + if( pParse==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + pParse->declareVtab = 1; + pParse->db = db; + pParse->nQueryLoop = 1; + + if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) + && pParse->pNewTable + && !db->mallocFailed + && !pParse->pNewTable->pSelect + && (pParse->pNewTable->tabFlags & TF_Virtual)==0 + ){ + if( !pTab->aCol ){ + pTab->aCol = pParse->pNewTable->aCol; + pTab->nCol = pParse->pNewTable->nCol; + pParse->pNewTable->nCol = 0; + pParse->pNewTable->aCol = 0; + } + pCtx->bDeclared = 1; + }else{ + sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); + sqlite3DbFree(db, zErr); + rc = SQLITE_ERROR; + } + pParse->declareVtab = 0; + + if( pParse->pVdbe ){ + sqlite3VdbeFinalize(pParse->pVdbe); + } + sqlite3DeleteTable(db, pParse->pNewTable); + sqlite3ParserReset(pParse); + sqlite3StackFree(db, pParse); + } + + assert( (rc&0xff)==rc ); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** This function is invoked by the vdbe to call the xDestroy method +** of the virtual table named zTab in database iDb. This occurs +** when a DROP TABLE is mentioned. +** +** This call is a no-op if zTab is not a virtual table. +*/ +SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ + int rc = SQLITE_OK; + Table *pTab; + + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); + if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ + VTable *p; + int (*xDestroy)(sqlite3_vtab *); + for(p=pTab->pVTable; p; p=p->pNext){ + assert( p->pVtab ); + if( p->pVtab->nRef>0 ){ + return SQLITE_LOCKED; + } + } + p = vtabDisconnectAll(db, pTab); + xDestroy = p->pMod->pModule->xDestroy; + assert( xDestroy!=0 ); /* Checked before the virtual table is created */ + rc = xDestroy(p->pVtab); + /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ + if( rc==SQLITE_OK ){ + assert( pTab->pVTable==p && p->pNext==0 ); + p->pVtab = 0; + pTab->pVTable = 0; + sqlite3VtabUnlock(p); + } + } + + return rc; +} + +/* +** This function invokes either the xRollback or xCommit method +** of each of the virtual tables in the sqlite3.aVTrans array. The method +** called is identified by the second argument, "offset", which is +** the offset of the method to call in the sqlite3_module structure. +** +** The array is cleared after invoking the callbacks. +*/ +static void callFinaliser(sqlite3 *db, int offset){ + int i; + if( db->aVTrans ){ + VTable **aVTrans = db->aVTrans; + db->aVTrans = 0; + for(i=0; inVTrans; i++){ + VTable *pVTab = aVTrans[i]; + sqlite3_vtab *p = pVTab->pVtab; + if( p ){ + int (*x)(sqlite3_vtab *); + x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); + if( x ) x(p); + } + pVTab->iSavepoint = 0; + sqlite3VtabUnlock(pVTab); + } + sqlite3DbFree(db, aVTrans); + db->nVTrans = 0; + } +} + +/* +** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans +** array. Return the error code for the first error that occurs, or +** SQLITE_OK if all xSync operations are successful. +** +** If an error message is available, leave it in p->zErrMsg. +*/ +SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){ + int i; + int rc = SQLITE_OK; + VTable **aVTrans = db->aVTrans; + + db->aVTrans = 0; + for(i=0; rc==SQLITE_OK && inVTrans; i++){ + int (*x)(sqlite3_vtab *); + sqlite3_vtab *pVtab = aVTrans[i]->pVtab; + if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ + rc = x(pVtab); + sqlite3VtabImportErrmsg(p, pVtab); + } + } + db->aVTrans = aVTrans; + return rc; +} + +/* +** Invoke the xRollback method of all virtual tables in the +** sqlite3.aVTrans array. Then clear the array itself. +*/ +SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){ + callFinaliser(db, offsetof(sqlite3_module,xRollback)); + return SQLITE_OK; +} + +/* +** Invoke the xCommit method of all virtual tables in the +** sqlite3.aVTrans array. Then clear the array itself. +*/ +SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){ + callFinaliser(db, offsetof(sqlite3_module,xCommit)); + return SQLITE_OK; +} + +/* +** If the virtual table pVtab supports the transaction interface +** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is +** not currently open, invoke the xBegin method now. +** +** If the xBegin call is successful, place the sqlite3_vtab pointer +** in the sqlite3.aVTrans array. +*/ +SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ + int rc = SQLITE_OK; + const sqlite3_module *pModule; + + /* Special case: If db->aVTrans is NULL and db->nVTrans is greater + ** than zero, then this function is being called from within a + ** virtual module xSync() callback. It is illegal to write to + ** virtual module tables in this case, so return SQLITE_LOCKED. + */ + if( sqlite3VtabInSync(db) ){ + return SQLITE_LOCKED; + } + if( !pVTab ){ + return SQLITE_OK; + } + pModule = pVTab->pVtab->pModule; + + if( pModule->xBegin ){ + int i; + + /* If pVtab is already in the aVTrans array, return early */ + for(i=0; inVTrans; i++){ + if( db->aVTrans[i]==pVTab ){ + return SQLITE_OK; + } + } + + /* Invoke the xBegin method. If successful, add the vtab to the + ** sqlite3.aVTrans[] array. */ + rc = growVTrans(db); + if( rc==SQLITE_OK ){ + rc = pModule->xBegin(pVTab->pVtab); + if( rc==SQLITE_OK ){ + int iSvpt = db->nStatement + db->nSavepoint; + addToVTrans(db, pVTab); + if( iSvpt ) rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, iSvpt-1); + } + } + } + return rc; +} + +/* +** Invoke either the xSavepoint, xRollbackTo or xRelease method of all +** virtual tables that currently have an open transaction. Pass iSavepoint +** as the second argument to the virtual table method invoked. +** +** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is +** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is +** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with +** an open transaction is invoked. +** +** If any virtual table method returns an error code other than SQLITE_OK, +** processing is abandoned and the error returned to the caller of this +** function immediately. If all calls to virtual table methods are successful, +** SQLITE_OK is returned. +*/ +SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ + int rc = SQLITE_OK; + + assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); + assert( iSavepoint>=-1 ); + if( db->aVTrans ){ + int i; + for(i=0; rc==SQLITE_OK && inVTrans; i++){ + VTable *pVTab = db->aVTrans[i]; + const sqlite3_module *pMod = pVTab->pMod->pModule; + if( pVTab->pVtab && pMod->iVersion>=2 ){ + int (*xMethod)(sqlite3_vtab *, int); + switch( op ){ + case SAVEPOINT_BEGIN: + xMethod = pMod->xSavepoint; + pVTab->iSavepoint = iSavepoint+1; + break; + case SAVEPOINT_ROLLBACK: + xMethod = pMod->xRollbackTo; + break; + default: + xMethod = pMod->xRelease; + break; + } + if( xMethod && pVTab->iSavepoint>iSavepoint ){ + rc = xMethod(pVTab->pVtab, iSavepoint); + } + } + } + } + return rc; +} + +/* +** The first parameter (pDef) is a function implementation. The +** second parameter (pExpr) is the first argument to this function. +** If pExpr is a column in a virtual table, then let the virtual +** table implementation have an opportunity to overload the function. +** +** This routine is used to allow virtual table implementations to +** overload MATCH, LIKE, GLOB, and REGEXP operators. +** +** Return either the pDef argument (indicating no change) or a +** new FuncDef structure that is marked as ephemeral using the +** SQLITE_FUNC_EPHEM flag. +*/ +SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction( + sqlite3 *db, /* Database connection for reporting malloc problems */ + FuncDef *pDef, /* Function to possibly overload */ + int nArg, /* Number of arguments to the function */ + Expr *pExpr /* First argument to the function */ +){ + Table *pTab; + sqlite3_vtab *pVtab; + sqlite3_module *pMod; + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0; + void *pArg = 0; + FuncDef *pNew; + int rc = 0; + char *zLowerName; + unsigned char *z; + + + /* Check to see the left operand is a column in a virtual table */ + if( NEVER(pExpr==0) ) return pDef; + if( pExpr->op!=TK_COLUMN ) return pDef; + pTab = pExpr->pTab; + if( NEVER(pTab==0) ) return pDef; + if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef; + pVtab = sqlite3GetVTable(db, pTab)->pVtab; + assert( pVtab!=0 ); + assert( pVtab->pModule!=0 ); + pMod = (sqlite3_module *)pVtab->pModule; + if( pMod->xFindFunction==0 ) return pDef; + + /* Call the xFindFunction method on the virtual table implementation + ** to see if the implementation wants to overload this function + */ + zLowerName = sqlite3DbStrDup(db, pDef->zName); + if( zLowerName ){ + for(z=(unsigned char*)zLowerName; *z; z++){ + *z = sqlite3UpperToLower[*z]; + } + rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg); + sqlite3DbFree(db, zLowerName); + } + if( rc==0 ){ + return pDef; + } + + /* Create a new ephemeral function definition for the overloaded + ** function */ + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + + sqlite3Strlen30(pDef->zName) + 1); + if( pNew==0 ){ + return pDef; + } + *pNew = *pDef; + pNew->zName = (const char*)&pNew[1]; + memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1); + pNew->xSFunc = xSFunc; + pNew->pUserData = pArg; + pNew->funcFlags |= SQLITE_FUNC_EPHEM; + return pNew; +} + +/* +** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] +** array so that an OP_VBegin will get generated for it. Add pTab to the +** array if it is missing. If pTab is already in the array, this routine +** is a no-op. +*/ +SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + int i, n; + Table **apVtabLock; + + assert( IsVirtual(pTab) ); + for(i=0; inVtabLock; i++){ + if( pTab==pToplevel->apVtabLock[i] ) return; + } + n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); + apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); + if( apVtabLock ){ + pToplevel->apVtabLock = apVtabLock; + pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; + }else{ + sqlite3OomFault(pToplevel->db); + } +} + +/* +** Check to see if virtual tale module pMod can be have an eponymous +** virtual table instance. If it can, create one if one does not already +** exist. Return non-zero if the eponymous virtual table instance exists +** when this routine returns, and return zero if it does not exist. +** +** An eponymous virtual table instance is one that is named after its +** module, and more importantly, does not require a CREATE VIRTUAL TABLE +** statement in order to come into existance. Eponymous virtual table +** instances always exist. They cannot be DROP-ed. +** +** Any virtual table module for which xConnect and xCreate are the same +** method can have an eponymous virtual table instance. +*/ +SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){ + const sqlite3_module *pModule = pMod->pModule; + Table *pTab; + char *zErr = 0; + int nName; + int rc; + sqlite3 *db = pParse->db; + if( pMod->pEpoTab ) return 1; + if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0; + nName = sqlite3Strlen30(pMod->zName) + 1; + pTab = sqlite3DbMallocZero(db, sizeof(Table) + nName); + if( pTab==0 ) return 0; + pMod->pEpoTab = pTab; + pTab->zName = (char*)&pTab[1]; + memcpy(pTab->zName, pMod->zName, nName); + pTab->nRef = 1; + pTab->pSchema = db->aDb[0].pSchema; + pTab->tabFlags |= TF_Virtual; + pTab->nModuleArg = 0; + pTab->iPKey = -1; + addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); + addModuleArgument(db, pTab, 0); + addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); + rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr); + if( rc ){ + sqlite3ErrorMsg(pParse, "%s", zErr); + sqlite3DbFree(db, zErr); + sqlite3VtabEponymousTableClear(db, pMod); + return 0; + } + return 1; +} + +/* +** Erase the eponymous virtual table instance associated with +** virtual table module pMod, if it exists. +*/ +SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){ + Table *pTab = pMod->pEpoTab; + if( pTab!=0 ){ + sqlite3DeleteColumnNames(db, pTab); + sqlite3VtabClear(db, pTab); + sqlite3DbFree(db, pTab); + pMod->pEpoTab = 0; + } +} + +/* +** Return the ON CONFLICT resolution mode in effect for the virtual +** table update operation currently in progress. +** +** The results of this routine are undefined unless it is called from +** within an xUpdate method. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *db){ + static const unsigned char aMap[] = { + SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE + }; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); + assert( OE_Ignore==4 && OE_Replace==5 ); + assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); + return (int)aMap[db->vtabOnConflict-1]; +} + +/* +** Call from within the xCreate() or xConnect() methods to provide +** the SQLite core with additional information about the behavior +** of the virtual table being implemented. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){ + va_list ap; + int rc = SQLITE_OK; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + va_start(ap, op); + switch( op ){ + case SQLITE_VTAB_CONSTRAINT_SUPPORT: { + VtabCtx *p = db->pVtabCtx; + if( !p ){ + rc = SQLITE_MISUSE_BKPT; + }else{ + assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); + p->pVTable->bConstraint = (u8)va_arg(ap, int); + } + break; + } + default: + rc = SQLITE_MISUSE_BKPT; + break; + } + va_end(ap); + + if( rc!=SQLITE_OK ) sqlite3Error(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/************** End of vtab.c ************************************************/ +/************** Begin file wherecode.c ***************************************/ +/* +** 2015-06-06 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This module contains C code that generates VDBE code used to process +** the WHERE clause of SQL statements. +** +** This file was split off from where.c on 2015-06-06 in order to reduce the +** size of where.c and make it easier to edit. This file contains the routines +** that actually generate the bulk of the WHERE loop code. The original where.c +** file retains the code that does query planning and analysis. +*/ +/* #include "sqliteInt.h" */ +/************** Include whereInt.h in the middle of wherecode.c **************/ +/************** Begin file whereInt.h ****************************************/ +/* +** 2013-11-12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains structure and macro definitions for the query +** planner logic in "where.c". These definitions are broken out into +** a separate source file for easier editing. +*/ + +/* +** Trace output macros +*/ +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) +/***/ int sqlite3WhereTrace; +#endif +#if defined(SQLITE_DEBUG) \ + && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) +# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X +# define WHERETRACE_ENABLED 1 +#else +# define WHERETRACE(K,X) +#endif + +/* Forward references +*/ +typedef struct WhereClause WhereClause; +typedef struct WhereMaskSet WhereMaskSet; +typedef struct WhereOrInfo WhereOrInfo; +typedef struct WhereAndInfo WhereAndInfo; +typedef struct WhereLevel WhereLevel; +typedef struct WhereLoop WhereLoop; +typedef struct WherePath WherePath; +typedef struct WhereTerm WhereTerm; +typedef struct WhereLoopBuilder WhereLoopBuilder; +typedef struct WhereScan WhereScan; +typedef struct WhereOrCost WhereOrCost; +typedef struct WhereOrSet WhereOrSet; + +/* +** This object contains information needed to implement a single nested +** loop in WHERE clause. +** +** Contrast this object with WhereLoop. This object describes the +** implementation of the loop. WhereLoop describes the algorithm. +** This object contains a pointer to the WhereLoop algorithm as one of +** its elements. +** +** The WhereInfo object contains a single instance of this object for +** each term in the FROM clause (which is to say, for each of the +** nested loops as implemented). The order of WhereLevel objects determines +** the loop nested order, with WhereInfo.a[0] being the outer loop and +** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. +*/ +struct WhereLevel { + int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ + int iTabCur; /* The VDBE cursor used to access the table */ + int iIdxCur; /* The VDBE cursor used to access pIdx */ + int addrBrk; /* Jump here to break out of the loop */ + int addrNxt; /* Jump here to start the next IN combination */ + int addrSkip; /* Jump here for next iteration of skip-scan */ + int addrCont; /* Jump here to continue with the next loop cycle */ + int addrFirst; /* First instruction of interior of the loop */ + int addrBody; /* Beginning of the body of this loop */ +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS + u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */ + int addrLikeRep; /* LIKE range processing address */ +#endif + u8 iFrom; /* Which entry in the FROM clause */ + u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ + int p1, p2; /* Operands of the opcode used to ends the loop */ + union { /* Information that depends on pWLoop->wsFlags */ + struct { + int nIn; /* Number of entries in aInLoop[] */ + struct InLoop { + int iCur; /* The VDBE cursor used by this IN operator */ + int addrInTop; /* Top of the IN loop */ + u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ + } *aInLoop; /* Information about each nested IN operator */ + } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ + Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ + } u; + struct WhereLoop *pWLoop; /* The selected WhereLoop object */ + Bitmask notReady; /* FROM entries not usable at this level */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrVisit; /* Address at which row is visited */ +#endif +}; + +/* +** Each instance of this object represents an algorithm for evaluating one +** term of a join. Every term of the FROM clause will have at least +** one corresponding WhereLoop object (unless INDEXED BY constraints +** prevent a query solution - which is an error) and many terms of the +** FROM clause will have multiple WhereLoop objects, each describing a +** potential way of implementing that FROM-clause term, together with +** dependencies and cost estimates for using the chosen algorithm. +** +** Query planning consists of building up a collection of these WhereLoop +** objects, then computing a particular sequence of WhereLoop objects, with +** one WhereLoop object per FROM clause term, that satisfy all dependencies +** and that minimize the overall cost. +*/ +struct WhereLoop { + Bitmask prereq; /* Bitmask of other loops that must run first */ + Bitmask maskSelf; /* Bitmask identifying table iTab */ +#ifdef SQLITE_DEBUG + char cId; /* Symbolic ID of this loop for debugging use */ +#endif + u8 iTab; /* Position in FROM clause of table for this loop */ + u8 iSortIdx; /* Sorting index number. 0==None */ + LogEst rSetup; /* One-time setup cost (ex: create transient index) */ + LogEst rRun; /* Cost of running each loop */ + LogEst nOut; /* Estimated number of output rows */ + union { + struct { /* Information for internal btree tables */ + u16 nEq; /* Number of equality constraints */ + Index *pIndex; /* Index used, or NULL */ + } btree; + struct { /* Information for virtual tables */ + int idxNum; /* Index number */ + u8 needFree; /* True if sqlite3_free(idxStr) is needed */ + i8 isOrdered; /* True if satisfies ORDER BY */ + u16 omitMask; /* Terms that may be omitted */ + char *idxStr; /* Index identifier string */ + } vtab; + } u; + u32 wsFlags; /* WHERE_* flags describing the plan */ + u16 nLTerm; /* Number of entries in aLTerm[] */ + u16 nSkip; /* Number of NULL aLTerm[] entries */ + /**** whereLoopXfer() copies fields above ***********************/ +# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) + u16 nLSlot; /* Number of slots allocated for aLTerm[] */ + WhereTerm **aLTerm; /* WhereTerms used */ + WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ + WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ +}; + +/* This object holds the prerequisites and the cost of running a +** subquery on one operand of an OR operator in the WHERE clause. +** See WhereOrSet for additional information +*/ +struct WhereOrCost { + Bitmask prereq; /* Prerequisites */ + LogEst rRun; /* Cost of running this subquery */ + LogEst nOut; /* Number of outputs for this subquery */ +}; + +/* The WhereOrSet object holds a set of possible WhereOrCosts that +** correspond to the subquery(s) of OR-clause processing. Only the +** best N_OR_COST elements are retained. +*/ +#define N_OR_COST 3 +struct WhereOrSet { + u16 n; /* Number of valid a[] entries */ + WhereOrCost a[N_OR_COST]; /* Set of best costs */ +}; + +/* +** Each instance of this object holds a sequence of WhereLoop objects +** that implement some or all of a query plan. +** +** Think of each WhereLoop object as a node in a graph with arcs +** showing dependencies and costs for travelling between nodes. (That is +** not a completely accurate description because WhereLoop costs are a +** vector, not a scalar, and because dependencies are many-to-one, not +** one-to-one as are graph nodes. But it is a useful visualization aid.) +** Then a WherePath object is a path through the graph that visits some +** or all of the WhereLoop objects once. +** +** The "solver" works by creating the N best WherePath objects of length +** 1. Then using those as a basis to compute the N best WherePath objects +** of length 2. And so forth until the length of WherePaths equals the +** number of nodes in the FROM clause. The best (lowest cost) WherePath +** at the end is the chosen query plan. +*/ +struct WherePath { + Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ + Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ + LogEst nRow; /* Estimated number of rows generated by this path */ + LogEst rCost; /* Total cost of this path */ + LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */ + i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */ + WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ +}; + +/* +** The query generator uses an array of instances of this structure to +** help it analyze the subexpressions of the WHERE clause. Each WHERE +** clause subexpression is separated from the others by AND operators, +** usually, or sometimes subexpressions separated by OR. +** +** All WhereTerms are collected into a single WhereClause structure. +** The following identity holds: +** +** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm +** +** When a term is of the form: +** +** X +** +** where X is a column name and is one of certain operators, +** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the +** cursor number and column number for X. WhereTerm.eOperator records +** the using a bitmask encoding defined by WO_xxx below. The +** use of a bitmask encoding for the operator allows us to search +** quickly for terms that match any of several different operators. +** +** A WhereTerm might also be two or more subterms connected by OR: +** +** (t1.X ) OR (t1.Y ) OR .... +** +** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR +** and the WhereTerm.u.pOrInfo field points to auxiliary information that +** is collected about the OR clause. +** +** If a term in the WHERE clause does not match either of the two previous +** categories, then eOperator==0. The WhereTerm.pExpr field is still set +** to the original subexpression content and wtFlags is set up appropriately +** but no other fields in the WhereTerm object are meaningful. +** +** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, +** but they do so indirectly. A single WhereMaskSet structure translates +** cursor number into bits and the translated bit is stored in the prereq +** fields. The translation is used in order to maximize the number of +** bits that will fit in a Bitmask. The VDBE cursor numbers might be +** spread out over the non-negative integers. For example, the cursor +** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet +** translates these sparse cursor numbers into consecutive integers +** beginning with 0 in order to make the best possible use of the available +** bits in the Bitmask. So, in the example above, the cursor numbers +** would be mapped into integers 0 through 7. +** +** The number of terms in a join is limited by the number of bits +** in prereqRight and prereqAll. The default is 64 bits, hence SQLite +** is only able to process joins with 64 or fewer tables. +*/ +struct WhereTerm { + Expr *pExpr; /* Pointer to the subexpression that is this term */ + int iParent; /* Disable pWC->a[iParent] when this term disabled */ + int leftCursor; /* Cursor number of X in "X " */ + union { + int leftColumn; /* Column number of X in "X " */ + WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ + WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ + } u; + LogEst truthProb; /* Probability of truth for this expression */ + u16 eOperator; /* A WO_xx value describing */ + u16 wtFlags; /* TERM_xxx bit flags. See below */ + u8 nChild; /* Number of children that must disable us */ + u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ + WhereClause *pWC; /* The clause this term is part of */ + Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ + Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ +}; + +/* +** Allowed values of WhereTerm.wtFlags +*/ +#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ +#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ +#define TERM_CODED 0x04 /* This term is already coded */ +#define TERM_COPIED 0x08 /* Has a child */ +#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ +#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ +#define TERM_OR_OK 0x40 /* Used during OR-clause processing */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ +#else +# define TERM_VNULL 0x00 /* Disabled if not using stat3 */ +#endif +#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ +#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ +#define TERM_LIKE 0x400 /* The original LIKE operator */ +#define TERM_IS 0x800 /* Term.pExpr is an IS operator */ + +/* +** An instance of the WhereScan object is used as an iterator for locating +** terms in the WHERE clause that are useful to the query planner. +*/ +struct WhereScan { + WhereClause *pOrigWC; /* Original, innermost WhereClause */ + WhereClause *pWC; /* WhereClause currently being scanned */ + const char *zCollName; /* Required collating sequence, if not NULL */ + Expr *pIdxExpr; /* Search for this index expression */ + char idxaff; /* Must match this affinity, if zCollName!=NULL */ + unsigned char nEquiv; /* Number of entries in aEquiv[] */ + unsigned char iEquiv; /* Next unused slot in aEquiv[] */ + u32 opMask; /* Acceptable operators */ + int k; /* Resume scanning at this->pWC->a[this->k] */ + int aiCur[11]; /* Cursors in the equivalence class */ + i16 aiColumn[11]; /* Corresponding column number in the eq-class */ +}; + +/* +** An instance of the following structure holds all information about a +** WHERE clause. Mostly this is a container for one or more WhereTerms. +** +** Explanation of pOuter: For a WHERE clause of the form +** +** a AND ((b AND c) OR (d AND e)) AND f +** +** There are separate WhereClause objects for the whole clause and for +** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the +** subclauses points to the WhereClause object for the whole clause. +*/ +struct WhereClause { + WhereInfo *pWInfo; /* WHERE clause processing context */ + WhereClause *pOuter; /* Outer conjunction */ + u8 op; /* Split operator. TK_AND or TK_OR */ + int nTerm; /* Number of terms */ + int nSlot; /* Number of entries in a[] */ + WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ +#if defined(SQLITE_SMALL_STACK) + WhereTerm aStatic[1]; /* Initial static space for a[] */ +#else + WhereTerm aStatic[8]; /* Initial static space for a[] */ +#endif +}; + +/* +** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to +** a dynamically allocated instance of the following structure. +*/ +struct WhereOrInfo { + WhereClause wc; /* Decomposition into subterms */ + Bitmask indexable; /* Bitmask of all indexable tables in the clause */ +}; + +/* +** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to +** a dynamically allocated instance of the following structure. +*/ +struct WhereAndInfo { + WhereClause wc; /* The subexpression broken out */ +}; + +/* +** An instance of the following structure keeps track of a mapping +** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. +** +** The VDBE cursor numbers are small integers contained in +** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE +** clause, the cursor numbers might not begin with 0 and they might +** contain gaps in the numbering sequence. But we want to make maximum +** use of the bits in our bitmasks. This structure provides a mapping +** from the sparse cursor numbers into consecutive integers beginning +** with 0. +** +** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask +** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<3, 5->1, 8->2, 29->0, +** 57->5, 73->4. Or one of 719 other combinations might be used. It +** does not really matter. What is important is that sparse cursor +** numbers all get mapped into bit numbers that begin with 0 and contain +** no gaps. +*/ +struct WhereMaskSet { + int n; /* Number of assigned cursor values */ + int ix[BMS]; /* Cursor assigned to each bit */ +}; + +/* +** Initialize a WhereMaskSet object +*/ +#define initMaskSet(P) (P)->n=0 + +/* +** This object is a convenience wrapper holding all information needed +** to construct WhereLoop objects for a particular query. +*/ +struct WhereLoopBuilder { + WhereInfo *pWInfo; /* Information about this WHERE */ + WhereClause *pWC; /* WHERE clause terms */ + ExprList *pOrderBy; /* ORDER BY clause */ + WhereLoop *pNew; /* Template WhereLoop */ + WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + UnpackedRecord *pRec; /* Probe for stat4 (if required) */ + int nRecValid; /* Number of valid fields currently in pRec */ +#endif +}; + +/* +** The WHERE clause processing routine has two halves. The +** first part does the start of the WHERE loop and the second +** half does the tail of the WHERE loop. An instance of +** this structure is returned by the first half and passed +** into the second half to give some continuity. +** +** An instance of this object holds the complete state of the query +** planner. +*/ +struct WhereInfo { + Parse *pParse; /* Parsing and code generating context */ + SrcList *pTabList; /* List of tables in the join */ + ExprList *pOrderBy; /* The ORDER BY clause or NULL */ + ExprList *pDistinctSet; /* DISTINCT over all these values */ + WhereLoop *pLoops; /* List of all WhereLoop objects */ + Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ + LogEst nRowOut; /* Estimated number of output rows */ + LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */ + u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ + i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ + u8 sorted; /* True if really sorted (not just grouped) */ + u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ + u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ + u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ + u8 nLevel; /* Number of nested loop */ + int iTop; /* The very beginning of the WHERE loop */ + int iContinue; /* Jump here to continue with next record */ + int iBreak; /* Jump here to break out of the loop */ + int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ + int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ + WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ + WhereClause sWC; /* Decomposition of the WHERE clause */ + WhereLevel a[1]; /* Information about each nest loop in WHERE */ +}; + +/* +** Private interfaces - callable only by other where.c routines. +** +** where.c: +*/ +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( + WhereClause *pWC, /* The WHERE clause to be searched */ + int iCur, /* Cursor number of LHS */ + int iColumn, /* Column number of LHS */ + Bitmask notReady, /* RHS must not overlap with this mask */ + u32 op, /* Mask of WO_xx values describing operator */ + Index *pIdx /* Must be compatible with this index, if not NULL */ +); + +/* wherecode.c: */ +#ifndef SQLITE_OMIT_EXPLAIN +SQLITE_PRIVATE int sqlite3WhereExplainOneScan( + Parse *pParse, /* Parse context */ + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ + int iLevel, /* Value for "level" column of output */ + int iFrom, /* Value for "from" column of output */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ +); +#else +# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0 +#endif /* SQLITE_OMIT_EXPLAIN */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +SQLITE_PRIVATE void sqlite3WhereAddScanStatus( + Vdbe *v, /* Vdbe to add scanstatus entry to */ + SrcList *pSrclist, /* FROM clause pLvl reads data from */ + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ + int addrExplain /* Address of OP_Explain (or 0) */ +); +#else +# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) +#endif +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ + int iLevel, /* Which level of pWInfo->a[] should be coded */ + Bitmask notReady /* Which tables are currently available */ +); + +/* whereexpr.c: */ +SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); +SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); +SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); + + + + + +/* +** Bitmasks for the operators on WhereTerm objects. These are all +** operators that are of interest to the query planner. An +** OR-ed combination of these values can be used when searching for +** particular WhereTerms within a WhereClause. +** +** Value constraints: +** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ +** WO_LT == SQLITE_INDEX_CONSTRAINT_LT +** WO_LE == SQLITE_INDEX_CONSTRAINT_LE +** WO_GT == SQLITE_INDEX_CONSTRAINT_GT +** WO_GE == SQLITE_INDEX_CONSTRAINT_GE +** WO_MATCH == SQLITE_INDEX_CONSTRAINT_MATCH +*/ +#define WO_IN 0x0001 +#define WO_EQ 0x0002 +#define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) +#define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) +#define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) +#define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) +#define WO_MATCH 0x0040 +#define WO_IS 0x0080 +#define WO_ISNULL 0x0100 +#define WO_OR 0x0200 /* Two or more OR-connected terms */ +#define WO_AND 0x0400 /* Two or more AND-connected terms */ +#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ +#define WO_NOOP 0x1000 /* This term does not restrict search space */ + +#define WO_ALL 0x1fff /* Mask of all possible WO_* values */ +#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ + +/* +** These are definitions of bits in the WhereLoop.wsFlags field. +** The particular combination of bits in each WhereLoop help to +** determine the algorithm that WhereLoop represents. +*/ +#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ +#define WHERE_COLUMN_RANGE 0x00000002 /* xEXPR */ +#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ +#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ +#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ +#define WHERE_TOP_LIMIT 0x00000010 /* xEXPR or x>=EXPR constraint */ +#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and xaiColumn[i]; + if( i==XN_EXPR ) return ""; + if( i==XN_ROWID ) return "rowid"; + return pIdx->pTable->aCol[i].zName; +} + +/* +** Argument pLevel describes a strategy for scanning table pTab. This +** function appends text to pStr that describes the subset of table +** rows scanned by the strategy in the form of an SQL expression. +** +** For example, if the query: +** +** SELECT * FROM t1 WHERE a=1 AND b>2; +** +** is run and there is an index on (a, b), then this function returns a +** string similar to: +** +** "a=? AND b>?" +*/ +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){ + Index *pIndex = pLoop->u.btree.pIndex; + u16 nEq = pLoop->u.btree.nEq; + u16 nSkip = pLoop->nSkip; + int i, j; + + if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; + sqlite3StrAccumAppend(pStr, " (", 2); + for(i=0; i=nSkip ? "%s=?" : "ANY(%s)", z); + } + + j = i; + if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ + const char *z = explainIndexColumnName(pIndex, i); + explainAppendTerm(pStr, i++, z, ">"); + } + if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ + const char *z = explainIndexColumnName(pIndex, j); + explainAppendTerm(pStr, i, z, "<"); + } + sqlite3StrAccumAppend(pStr, ")", 1); +} + +/* +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode +** is added to the output to describe the table scan strategy in pLevel. +** +** If an OP_Explain opcode is added to the VM, its address is returned. +** Otherwise, if no OP_Explain is coded, zero is returned. +*/ +SQLITE_PRIVATE int sqlite3WhereExplainOneScan( + Parse *pParse, /* Parse context */ + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ + int iLevel, /* Value for "level" column of output */ + int iFrom, /* Value for "from" column of output */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ +){ + int ret = 0; +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) + if( pParse->explain==2 ) +#endif + { + struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; + Vdbe *v = pParse->pVdbe; /* VM being constructed */ + sqlite3 *db = pParse->db; /* Database handle */ + int iId = pParse->iSelectId; /* Select id (left-most output column) */ + int isSearch; /* True for a SEARCH. False for SCAN. */ + WhereLoop *pLoop; /* The controlling WhereLoop object */ + u32 flags; /* Flags that describe this loop */ + char *zMsg; /* Text to add to EQP output */ + StrAccum str; /* EQP output string */ + char zBuf[100]; /* Initial space for EQP output string */ + + pLoop = pLevel->pWLoop; + flags = pLoop->wsFlags; + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; + + isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 + || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); + + sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); + sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); + if( pItem->pSelect ){ + sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId); + }else{ + sqlite3XPrintf(&str, " TABLE %s", pItem->zName); + } + + if( pItem->zAlias ){ + sqlite3XPrintf(&str, " AS %s", pItem->zAlias); + } + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ + const char *zFmt = 0; + Index *pIdx; + + assert( pLoop->u.btree.pIndex!=0 ); + pIdx = pLoop->u.btree.pIndex; + assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); + if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ + if( isSearch ){ + zFmt = "PRIMARY KEY"; + } + }else if( flags & WHERE_PARTIALIDX ){ + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; + }else if( flags & WHERE_AUTO_INDEX ){ + zFmt = "AUTOMATIC COVERING INDEX"; + }else if( flags & WHERE_IDX_ONLY ){ + zFmt = "COVERING INDEX %s"; + }else{ + zFmt = "INDEX %s"; + } + if( zFmt ){ + sqlite3StrAccumAppend(&str, " USING ", 7); + sqlite3XPrintf(&str, zFmt, pIdx->zName); + explainIndexRange(&str, pLoop); + } + }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ + const char *zRangeOp; + if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ + zRangeOp = "="; + }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ + zRangeOp = ">? AND rowid<"; + }else if( flags&WHERE_BTM_LIMIT ){ + zRangeOp = ">"; + }else{ + assert( flags&WHERE_TOP_LIMIT); + zRangeOp = "<"; + } + sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp); + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ + sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s", + pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); + } +#endif +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS + if( pLoop->nOut>=10 ){ + sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); + }else{ + sqlite3StrAccumAppend(&str, " (~1 row)", 9); + } +#endif + zMsg = sqlite3StrAccumFinish(&str); + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); + } + return ret; +} +#endif /* SQLITE_OMIT_EXPLAIN */ + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +/* +** Configure the VM passed as the first argument with an +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to +** implement level pLvl. Argument pSrclist is a pointer to the FROM +** clause that the scan reads data from. +** +** If argument addrExplain is not 0, it must be the address of an +** OP_Explain instruction that describes the same loop. +*/ +SQLITE_PRIVATE void sqlite3WhereAddScanStatus( + Vdbe *v, /* Vdbe to add scanstatus entry to */ + SrcList *pSrclist, /* FROM clause pLvl reads data from */ + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ + int addrExplain /* Address of OP_Explain (or 0) */ +){ + const char *zObj = 0; + WhereLoop *pLoop = pLvl->pWLoop; + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ + zObj = pLoop->u.btree.pIndex->zName; + }else{ + zObj = pSrclist->a[pLvl->iFrom].zName; + } + sqlite3VdbeScanStatus( + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj + ); +} +#endif + + +/* +** Disable a term in the WHERE clause. Except, do not disable the term +** if it controls a LEFT OUTER JOIN and it did not originate in the ON +** or USING clause of that join. +** +** Consider the term t2.z='ok' in the following queries: +** +** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' +** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' +** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' +** +** The t2.z='ok' is disabled in the in (2) because it originates +** in the ON clause. The term is disabled in (3) because it is not part +** of a LEFT OUTER JOIN. In (1), the term is not disabled. +** +** Disabling a term causes that term to not be tested in the inner loop +** of the join. Disabling is an optimization. When terms are satisfied +** by indices, we disable them to prevent redundant tests in the inner +** loop. We would get the correct results if nothing were ever disabled, +** but joins might run a little slower. The trick is to disable as much +** as we can without disabling too much. If we disabled in (1), we'd get +** the wrong answer. See ticket #813. +** +** If all the children of a term are disabled, then that term is also +** automatically disabled. In this way, terms get disabled if derived +** virtual terms are tested first. For example: +** +** x GLOB 'abc*' AND x>='abc' AND x<'acd' +** \___________/ \______/ \_____/ +** parent child1 child2 +** +** Only the parent term was in the original WHERE clause. The child1 +** and child2 terms were added by the LIKE optimization. If both of +** the virtual child terms are valid, then testing of the parent can be +** skipped. +** +** Usually the parent term is marked as TERM_CODED. But if the parent +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. +** The TERM_LIKECOND marking indicates that the term should be coded inside +** a conditional such that is only evaluated on the second pass of a +** LIKE-optimization loop, when scanning BLOBs instead of strings. +*/ +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ + int nLoop = 0; + while( pTerm + && (pTerm->wtFlags & TERM_CODED)==0 + && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + && (pLevel->notReady & pTerm->prereqAll)==0 + ){ + if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ + pTerm->wtFlags |= TERM_LIKECOND; + }else{ + pTerm->wtFlags |= TERM_CODED; + } + if( pTerm->iParent<0 ) break; + pTerm = &pTerm->pWC->a[pTerm->iParent]; + pTerm->nChild--; + if( pTerm->nChild!=0 ) break; + nLoop++; + } +} + +/* +** Code an OP_Affinity opcode to apply the column affinity string zAff +** to the n registers starting at base. +** +** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the +** beginning and end of zAff are ignored. If all entries in zAff are +** SQLITE_AFF_BLOB, then no code gets generated. +** +** This routine makes its own copy of zAff so that the caller is free +** to modify zAff after this routine returns. +*/ +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ + Vdbe *v = pParse->pVdbe; + if( zAff==0 ){ + assert( pParse->db->mallocFailed ); + return; + } + assert( v!=0 ); + + /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning + ** and end of the affinity string. + */ + while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){ + n--; + base++; + zAff++; + } + while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){ + n--; + } + + /* Code the OP_Affinity opcode if there is anything left to do. */ + if( n>0 ){ + sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n); + sqlite3ExprCacheAffinityChange(pParse, base, n); + } +} + + +/* +** Generate code for a single equality term of the WHERE clause. An equality +** term can be either X=expr or X IN (...). pTerm is the term to be +** coded. +** +** The current value for the constraint is left in register iReg. +** +** For a constraint of the form X=expr, the expression is evaluated and its +** result is left on the stack. For constraints of the form X IN (...) +** this routine sets up a loop that will iterate over all values of X. +*/ +static int codeEqualityTerm( + Parse *pParse, /* The parsing context */ + WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ + WhereLevel *pLevel, /* The level of the FROM clause we are working on */ + int iEq, /* Index of the equality term within this level */ + int bRev, /* True for reverse-order IN operations */ + int iTarget /* Attempt to leave results in this register */ +){ + Expr *pX = pTerm->pExpr; + Vdbe *v = pParse->pVdbe; + int iReg; /* Register holding results */ + + assert( iTarget>0 ); + if( pX->op==TK_EQ || pX->op==TK_IS ){ + iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); + }else if( pX->op==TK_ISNULL ){ + iReg = iTarget; + sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); +#ifndef SQLITE_OMIT_SUBQUERY + }else{ + int eType; + int iTab; + struct InLoop *pIn; + WhereLoop *pLoop = pLevel->pWLoop; + + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 + && pLoop->u.btree.pIndex!=0 + && pLoop->u.btree.pIndex->aSortOrder[iEq] + ){ + testcase( iEq==0 ); + testcase( bRev ); + bRev = !bRev; + } + assert( pX->op==TK_IN ); + iReg = iTarget; + eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); + if( eType==IN_INDEX_INDEX_DESC ){ + testcase( bRev ); + bRev = !bRev; + } + iTab = pX->iTable; + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); + VdbeCoverageIf(v, bRev); + VdbeCoverageIf(v, !bRev); + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); + pLoop->wsFlags |= WHERE_IN_ABLE; + if( pLevel->u.in.nIn==0 ){ + pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + } + pLevel->u.in.nIn++; + pLevel->u.in.aInLoop = + sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, + sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); + pIn = pLevel->u.in.aInLoop; + if( pIn ){ + pIn += pLevel->u.in.nIn - 1; + pIn->iCur = iTab; + if( eType==IN_INDEX_ROWID ){ + pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); + }else{ + pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); + } + pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; + sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); + }else{ + pLevel->u.in.nIn = 0; + } +#endif + } + disableTerm(pLevel, pTerm); + return iReg; +} + +/* +** Generate code that will evaluate all == and IN constraints for an +** index scan. +** +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). +** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 +** The index has as many as three equality constraints, but in this +** example, the third "c" value is an inequality. So only two +** constraints are coded. This routine will generate code to evaluate +** a==5 and b IN (1,2,3). The current values for a and b will be stored +** in consecutive registers and the index of the first register is returned. +** +** In the example above nEq==2. But this subroutine works for any value +** of nEq including 0. If nEq==0, this routine is nearly a no-op. +** The only thing it does is allocate the pLevel->iMem memory cell and +** compute the affinity string. +** +** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints +** are == or IN and are covered by the nEq. nExtraReg is 1 if there is +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that +** occurs after the nEq quality constraints. +** +** This routine allocates a range of nEq+nExtraReg memory cells and returns +** the index of the first memory cell in that range. The code that +** calls this routine will use that memory range to store keys for +** start and termination conditions of the loop. +** key value of the loop. If one or more IN operators appear, then +** this routine allocates an additional nEq memory cells for internal +** use. +** +** Before returning, *pzAff is set to point to a buffer containing a +** copy of the column affinity string of the index allocated using +** sqlite3DbMalloc(). Except, entries in the copy of the string associated +** with equality constraints that use BLOB or NONE affinity are set to +** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: +** +** CREATE TABLE t1(a TEXT PRIMARY KEY, b); +** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; +** +** In the example above, the index on t1(a) has TEXT affinity. But since +** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, +** no conversion should be attempted before using a t2.b value as part of +** a key to search the index. Hence the first byte in the returned affinity +** string in this example would be set to SQLITE_AFF_BLOB. +*/ +static int codeAllEqualityTerms( + Parse *pParse, /* Parsing context */ + WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ + int bRev, /* Reverse the order of IN operators */ + int nExtraReg, /* Number of extra registers to allocate */ + char **pzAff /* OUT: Set to point to affinity string */ +){ + u16 nEq; /* The number of == or IN constraints to code */ + u16 nSkip; /* Number of left-most columns to skip */ + Vdbe *v = pParse->pVdbe; /* The vm under construction */ + Index *pIdx; /* The index being used for this loop */ + WhereTerm *pTerm; /* A single constraint term */ + WhereLoop *pLoop; /* The WhereLoop object */ + int j; /* Loop counter */ + int regBase; /* Base register */ + int nReg; /* Number of registers to allocate */ + char *zAff; /* Affinity string to return */ + + /* This module is only called on query plans that use an index. */ + pLoop = pLevel->pWLoop; + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); + nEq = pLoop->u.btree.nEq; + nSkip = pLoop->nSkip; + pIdx = pLoop->u.btree.pIndex; + assert( pIdx!=0 ); + + /* Figure out how many memory cells we will need then allocate them. + */ + regBase = pParse->nMem + 1; + nReg = pLoop->u.btree.nEq + nExtraReg; + pParse->nMem += nReg; + + zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx)); + assert( zAff!=0 || pParse->db->mallocFailed ); + + if( nSkip ){ + int iIdxCur = pLevel->iIdxCur; + sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); + j = sqlite3VdbeAddOp0(v, OP_Goto); + pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), + iIdxCur, 0, regBase, nSkip); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + sqlite3VdbeJumpHere(v, j); + for(j=0; jaiColumn[j]==XN_EXPR ); + VdbeComment((v, "%s", explainIndexColumnName(pIdx, j))); + } + } + + /* Evaluate the equality constraints + */ + assert( zAff==0 || (int)strlen(zAff)>=nEq ); + for(j=nSkip; jaLTerm[j]; + assert( pTerm!=0 ); + /* The following testcase is true for indices with redundant columns. + ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ + testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); + if( r1!=regBase+j ){ + if( nReg==1 ){ + sqlite3ReleaseTempReg(pParse, regBase); + regBase = r1; + }else{ + sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); + } + } + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_IN ); + if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ + Expr *pRight = pTerm->pExpr->pRight; + if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); + VdbeCoverage(v); + } + if( zAff ){ + if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ + zAff[j] = SQLITE_AFF_BLOB; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ + zAff[j] = SQLITE_AFF_BLOB; + } + } + } + } + *pzAff = zAff; + return regBase; +} + +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS +/* +** If the most recently coded instruction is a constant range constraint +** (a string literal) that originated from the LIKE optimization, then +** set P3 and P5 on the OP_String opcode so that the string will be cast +** to a BLOB at appropriate times. +** +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range +** expression: "x>='ABC' AND x<'abd'". But this requires that the range +** scan loop run twice, once for strings and a second time for BLOBs. +** The OP_String opcodes on the second pass convert the upper and lower +** bound string contants to blobs. This routine makes the necessary changes +** to the OP_String opcodes for that to happen. +** +** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then +** only the one pass through the string space is required, so this routine +** becomes a no-op. +*/ +static void whereLikeOptimizationStringFixup( + Vdbe *v, /* prepared statement under construction */ + WhereLevel *pLevel, /* The loop that contains the LIKE operator */ + WhereTerm *pTerm /* The upper or lower bound just coded */ +){ + if( pTerm->wtFlags & TERM_LIKEOPT ){ + VdbeOp *pOp; + assert( pLevel->iLikeRepCntr>0 ); + pOp = sqlite3VdbeGetOp(v, -1); + assert( pOp!=0 ); + assert( pOp->opcode==OP_String8 + || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); + pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */ + pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */ + } +} +#else +# define whereLikeOptimizationStringFixup(A,B,C) +#endif + +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* +** Information is passed from codeCursorHint() down to individual nodes of +** the expression tree (by sqlite3WalkExpr()) using an instance of this +** structure. +*/ +struct CCurHint { + int iTabCur; /* Cursor for the main table */ + int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */ + Index *pIdx; /* The index used to access the table */ +}; + +/* +** This function is called for every node of an expression that is a candidate +** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference +** the table CCurHint.iTabCur, verify that the same column can be +** accessed through the index. If it cannot, then set pWalker->eCode to 1. +*/ +static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){ + struct CCurHint *pHint = pWalker->u.pCCurHint; + assert( pHint->pIdx!=0 ); + if( pExpr->op==TK_COLUMN + && pExpr->iTable==pHint->iTabCur + && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0 + ){ + pWalker->eCode = 1; + } + return WRC_Continue; +} + + +/* +** This function is called on every node of an expression tree used as an +** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN +** that accesses any table other than the one identified by +** CCurHint.iTabCur, then do the following: +** +** 1) allocate a register and code an OP_Column instruction to read +** the specified column into the new register, and +** +** 2) transform the expression node to a TK_REGISTER node that reads +** from the newly populated register. +** +** Also, if the node is a TK_COLUMN that does access the table idenified +** by pCCurHint.iTabCur, and an index is being used (which we will +** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into +** an access of the index rather than the original table. +*/ +static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ + int rc = WRC_Continue; + struct CCurHint *pHint = pWalker->u.pCCurHint; + if( pExpr->op==TK_COLUMN ){ + if( pExpr->iTable!=pHint->iTabCur ){ + Vdbe *v = pWalker->pParse->pVdbe; + int reg = ++pWalker->pParse->nMem; /* Register for column value */ + sqlite3ExprCodeGetColumnOfTable( + v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg + ); + pExpr->op = TK_REGISTER; + pExpr->iTable = reg; + }else if( pHint->pIdx!=0 ){ + pExpr->iTable = pHint->iIdxCur; + pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn); + assert( pExpr->iColumn>=0 ); + } + }else if( pExpr->op==TK_AGG_FUNCTION ){ + /* An aggregate function in the WHERE clause of a query means this must + ** be a correlated sub-query, and expression pExpr is an aggregate from + ** the parent context. Do not walk the function arguments in this case. + ** + ** todo: It should be possible to replace this node with a TK_REGISTER + ** expression, as the result of the expression must be stored in a + ** register at this point. The same holds for TK_AGG_COLUMN nodes. */ + rc = WRC_Prune; + } + return rc; +} + +/* +** Insert an OP_CursorHint instruction if it is appropriate to do so. +*/ +static void codeCursorHint( + WhereInfo *pWInfo, /* The where clause */ + WhereLevel *pLevel, /* Which loop to provide hints for */ + WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */ +){ + Parse *pParse = pWInfo->pParse; + sqlite3 *db = pParse->db; + Vdbe *v = pParse->pVdbe; + Expr *pExpr = 0; + WhereLoop *pLoop = pLevel->pWLoop; + int iCur; + WhereClause *pWC; + WhereTerm *pTerm; + int i, j; + struct CCurHint sHint; + Walker sWalker; + + if( OptimizationDisabled(db, SQLITE_CursorHints) ) return; + iCur = pLevel->iTabCur; + assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor ); + sHint.iTabCur = iCur; + sHint.iIdxCur = pLevel->iIdxCur; + sHint.pIdx = pLoop->u.btree.pIndex; + memset(&sWalker, 0, sizeof(sWalker)); + sWalker.pParse = pParse; + sWalker.u.pCCurHint = &sHint; + pWC = &pWInfo->sWC; + for(i=0; inTerm; i++){ + pTerm = &pWC->a[i]; + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( pTerm->prereqAll & pLevel->notReady ) continue; + if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue; + + /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize + ** the cursor. These terms are not needed as hints for a pure range + ** scan (that has no == terms) so omit them. */ + if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){ + for(j=0; jnLTerm && pLoop->aLTerm[j]!=pTerm; j++){} + if( jnLTerm ) continue; + } + + /* No subqueries or non-deterministic functions allowed */ + if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue; + + /* For an index scan, make sure referenced columns are actually in + ** the index. */ + if( sHint.pIdx!=0 ){ + sWalker.eCode = 0; + sWalker.xExprCallback = codeCursorHintCheckExpr; + sqlite3WalkExpr(&sWalker, pTerm->pExpr); + if( sWalker.eCode ) continue; + } + + /* If we survive all prior tests, that means this term is worth hinting */ + pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); + } + if( pExpr!=0 ){ + sWalker.xExprCallback = codeCursorHintFixExpr; + sqlite3WalkExpr(&sWalker, pExpr); + sqlite3VdbeAddOp4(v, OP_CursorHint, + (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0, + (const char*)pExpr, P4_EXPR); + } +} +#else +# define codeCursorHint(A,B,C) /* No-op */ +#endif /* SQLITE_ENABLE_CURSOR_HINTS */ + +/* +** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains +** a rowid value just read from cursor iIdxCur, open on index pIdx. This +** function generates code to do a deferred seek of cursor iCur to the +** rowid stored in register iRowid. +** +** Normally, this is just: +** +** OP_Seek $iCur $iRowid +** +** However, if the scan currently being coded is a branch of an OR-loop and +** the statement currently being coded is a SELECT, then P3 of the OP_Seek +** is set to iIdxCur and P4 is set to point to an array of integers +** containing one entry for each column of the table cursor iCur is open +** on. For each table column, if the column is the i'th column of the +** index, then the corresponding array entry is set to (i+1). If the column +** does not appear in the index at all, the array entry is set to 0. +*/ +static void codeDeferredSeek( + WhereInfo *pWInfo, /* Where clause context */ + Index *pIdx, /* Index scan is using */ + int iCur, /* Cursor for IPK b-tree */ + int iIdxCur /* Index cursor */ +){ + Parse *pParse = pWInfo->pParse; /* Parse context */ + Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */ + + assert( iIdxCur>0 ); + assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 ); + + sqlite3VdbeAddOp3(v, OP_Seek, iIdxCur, 0, iCur); + if( (pWInfo->wctrlFlags & WHERE_FORCE_TABLE) + && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask) + ){ + int i; + Table *pTab = pIdx->pTable; + int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1)); + if( ai ){ + ai[0] = pTab->nCol; + for(i=0; inColumn-1; i++){ + assert( pIdx->aiColumn[i]nCol ); + if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1; + } + sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY); + } + } +} + +/* +** Generate code for the start of the iLevel-th loop in the WHERE clause +** implementation described by pWInfo. +*/ +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ + int iLevel, /* Which level of pWInfo->a[] should be coded */ + Bitmask notReady /* Which tables are currently available */ +){ + int j, k; /* Loop counters */ + int iCur; /* The VDBE cursor for the table */ + int addrNxt; /* Where to jump to continue with the next IN case */ + int omitTable; /* True if we use the index only */ + int bRev; /* True if we need to scan in reverse order */ + WhereLevel *pLevel; /* The where level to be coded */ + WhereLoop *pLoop; /* The WhereLoop object being coded */ + WhereClause *pWC; /* Decomposition of the entire WHERE clause */ + WhereTerm *pTerm; /* A WHERE clause term */ + Parse *pParse; /* Parsing context */ + sqlite3 *db; /* Database connection */ + Vdbe *v; /* The prepared stmt under constructions */ + struct SrcList_item *pTabItem; /* FROM clause term being coded */ + int addrBrk; /* Jump here to break out of the loop */ + int addrCont; /* Jump here to continue with next cycle */ + int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ + int iReleaseReg = 0; /* Temp register to free before returning */ + + pParse = pWInfo->pParse; + v = pParse->pVdbe; + pWC = &pWInfo->sWC; + db = pParse->db; + pLevel = &pWInfo->a[iLevel]; + pLoop = pLevel->pWLoop; + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; + iCur = pTabItem->iCursor; + pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); + bRev = (pWInfo->revMask>>iLevel)&1; + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 + && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; + VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); + + /* Create labels for the "break" and "continue" instructions + ** for the current loop. Jump to addrBrk to break out of a loop. + ** Jump to cont to go immediately to the next iteration of the + ** loop. + ** + ** When there is an IN operator, we also have a "addrNxt" label that + ** means to continue with the next IN value combination. When + ** there are no IN operators in the constraints, the "addrNxt" label + ** is the same as "addrBrk". + */ + addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); + + /* If this is the right table of a LEFT OUTER JOIN, allocate and + ** initialize a memory cell that records if this table matches any + ** row of the left table of the join. + */ + if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){ + pLevel->iLeftJoin = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); + VdbeComment((v, "init LEFT JOIN no-match flag")); + } + + /* Special case of a FROM clause subquery implemented as a co-routine */ + if( pTabItem->fg.viaCoroutine ){ + int regYield = pTabItem->regReturn; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); + VdbeCoverage(v); + VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + pLevel->op = OP_Goto; + }else + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + /* Case 1: The table is a virtual-table. Use the VFilter and VNext + ** to access the data. + */ + int iReg; /* P3 Value for OP_VFilter */ + int addrNotFound; + int nConstraint = pLoop->nLTerm; + int iIn; /* Counter for IN constraints */ + + sqlite3ExprCachePush(pParse); + iReg = sqlite3GetTempRange(pParse, nConstraint+2); + addrNotFound = pLevel->addrBrk; + for(j=0; jaLTerm[j]; + if( NEVER(pTerm==0) ) continue; + if( pTerm->eOperator & WO_IN ){ + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); + addrNotFound = pLevel->addrNxt; + }else{ + sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); + } + } + sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); + sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, + pLoop->u.vtab.idxStr, + pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); + VdbeCoverage(v); + pLoop->u.vtab.needFree = 0; + pLevel->p1 = iCur; + pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + iIn = pLevel->u.in.nIn; + for(j=nConstraint-1; j>=0; j--){ + pTerm = pLoop->aLTerm[j]; + if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ + disableTerm(pLevel, pTerm); + }else if( (pTerm->eOperator & WO_IN)!=0 ){ + Expr *pCompare; /* The comparison operator */ + Expr *pRight; /* RHS of the comparison */ + VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ + + /* Reload the constraint value into reg[iReg+j+2]. The same value + ** was loaded into the same register prior to the OP_VFilter, but + ** the xFilter implementation might have changed the datatype or + ** encoding of the value in the register, so it *must* be reloaded. */ + assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed ); + if( !db->mallocFailed ){ + assert( iIn>0 ); + pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop); + assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid ); + assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 ); + assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 ); + testcase( pOp->opcode==OP_Rowid ); + sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); + } + + /* Generate code that will continue to the next row if + ** the IN constraint is not satisfied */ + pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0, 0); + assert( pCompare!=0 || db->mallocFailed ); + if( pCompare ){ + pCompare->pLeft = pTerm->pExpr->pLeft; + pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); + if( pRight ){ + pRight->iTable = iReg+j+2; + sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0); + } + pCompare->pLeft = 0; + sqlite3ExprDelete(db, pCompare); + } + } + } + /* These registers need to be preserved in case there is an IN operator + ** loop. So we could deallocate the registers here (and potentially + ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems + ** simpler and safer to simply not reuse the registers. + ** + ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); + */ + sqlite3ExprCachePop(pParse); + }else +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + + if( (pLoop->wsFlags & WHERE_IPK)!=0 + && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 + ){ + /* Case 2: We can directly reference a single row using an + ** equality comparison against the ROWID field. Or + ** we reference multiple rows using a "rowid IN (...)" + ** construct. + */ + assert( pLoop->u.btree.nEq==1 ); + pTerm = pLoop->aLTerm[0]; + assert( pTerm!=0 ); + assert( pTerm->pExpr!=0 ); + assert( omitTable==0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + iReleaseReg = ++pParse->nMem; + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); + if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); + addrNxt = pLevel->addrNxt; + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); + VdbeCoverage(v); + sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + VdbeComment((v, "pk")); + pLevel->op = OP_Noop; + }else if( (pLoop->wsFlags & WHERE_IPK)!=0 + && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 + ){ + /* Case 3: We have an inequality comparison against the ROWID field. + */ + int testOp = OP_Noop; + int start; + int memEndValue = 0; + WhereTerm *pStart, *pEnd; + + assert( omitTable==0 ); + j = 0; + pStart = pEnd = 0; + if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; + if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; + assert( pStart!=0 || pEnd!=0 ); + if( bRev ){ + pTerm = pStart; + pStart = pEnd; + pEnd = pTerm; + } + codeCursorHint(pWInfo, pLevel, pEnd); + if( pStart ){ + Expr *pX; /* The expression that defines the start bound */ + int r1, rTemp; /* Registers for holding the start boundary */ + + /* The following constant maps TK_xx codes into corresponding + ** seek opcodes. It depends on a particular ordering of TK_xx + */ + const u8 aMoveOp[] = { + /* TK_GT */ OP_SeekGT, + /* TK_LE */ OP_SeekLE, + /* TK_LT */ OP_SeekLT, + /* TK_GE */ OP_SeekGE + }; + assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ + assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ + assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ + + assert( (pStart->wtFlags & TERM_VNULL)==0 ); + testcase( pStart->wtFlags & TERM_VIRTUAL ); + pX = pStart->pExpr; + assert( pX!=0 ); + testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); + sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); + VdbeComment((v, "pk")); + VdbeCoverageIf(v, pX->op==TK_GT); + VdbeCoverageIf(v, pX->op==TK_LE); + VdbeCoverageIf(v, pX->op==TK_LT); + VdbeCoverageIf(v, pX->op==TK_GE); + sqlite3ExprCacheAffinityChange(pParse, r1, 1); + sqlite3ReleaseTempReg(pParse, rTemp); + disableTerm(pLevel, pStart); + }else{ + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + } + if( pEnd ){ + Expr *pX; + pX = pEnd->pExpr; + assert( pX!=0 ); + assert( (pEnd->wtFlags & TERM_VNULL)==0 ); + testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ + testcase( pEnd->wtFlags & TERM_VIRTUAL ); + memEndValue = ++pParse->nMem; + sqlite3ExprCode(pParse, pX->pRight, memEndValue); + if( pX->op==TK_LT || pX->op==TK_GT ){ + testOp = bRev ? OP_Le : OP_Ge; + }else{ + testOp = bRev ? OP_Lt : OP_Gt; + } + disableTerm(pLevel, pEnd); + } + start = sqlite3VdbeCurrentAddr(v); + pLevel->op = bRev ? OP_Prev : OP_Next; + pLevel->p1 = iCur; + pLevel->p2 = start; + assert( pLevel->p5==0 ); + if( testOp!=OP_Noop ){ + iRowidReg = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); + VdbeCoverageIf(v, testOp==OP_Le); + VdbeCoverageIf(v, testOp==OP_Lt); + VdbeCoverageIf(v, testOp==OP_Ge); + VdbeCoverageIf(v, testOp==OP_Gt); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); + } + }else if( pLoop->wsFlags & WHERE_INDEXED ){ + /* Case 4: A scan using an index. + ** + ** The WHERE clause may contain zero or more equality + ** terms ("==" or "IN" operators) that refer to the N + ** left-most columns of the index. It may also contain + ** inequality constraints (>, <, >= or <=) on the indexed + ** column that immediately follows the N equalities. Only + ** the right-most column can be an inequality - the rest must + ** use the "==" and "IN" operators. For example, if the + ** index is on (x,y,z), then the following clauses are all + ** optimized: + ** + ** x=5 + ** x=5 AND y=10 + ** x=5 AND y<10 + ** x=5 AND y>5 AND y<10 + ** x=5 AND y=5 AND z<=10 + ** + ** The z<10 term of the following cannot be used, only + ** the x=5 term: + ** + ** x=5 AND z<10 + ** + ** N may be zero if there are inequality constraints. + ** If there are no inequality constraints, then N is at + ** least one. + ** + ** This case is also used when there are no WHERE clause + ** constraints but an index is selected anyway, in order + ** to force the output order to conform to an ORDER BY. + */ + static const u8 aStartOp[] = { + 0, + 0, + OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ + OP_Last, /* 3: (!start_constraints && startEq && bRev) */ + OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ + OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ + OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ + OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ + }; + static const u8 aEndOp[] = { + OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ + OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ + OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ + OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ + }; + u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ + int regBase; /* Base register holding constraint values */ + WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ + WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ + int startEq; /* True if range start uses ==, >= or <= */ + int endEq; /* True if range end uses ==, >= or <= */ + int start_constraints; /* Start of range is constrained */ + int nConstraint; /* Number of constraint terms */ + Index *pIdx; /* The index we will be using */ + int iIdxCur; /* The VDBE cursor for the index */ + int nExtraReg = 0; /* Number of extra registers needed */ + int op; /* Instruction opcode */ + char *zStartAff; /* Affinity for start of range constraint */ + char cEndAff = 0; /* Affinity for end of range constraint */ + u8 bSeekPastNull = 0; /* True to seek past initial nulls */ + u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ + + pIdx = pLoop->u.btree.pIndex; + iIdxCur = pLevel->iIdxCur; + assert( nEq>=pLoop->nSkip ); + + /* If this loop satisfies a sort order (pOrderBy) request that + ** was passed to this function to implement a "SELECT min(x) ..." + ** query, then the caller will only allow the loop to run for + ** a single iteration. This means that the first row returned + ** should not have a NULL value stored in 'x'. If column 'x' is + ** the first one after the nEq equality constraints in the index, + ** this requires some special handling. + */ + assert( pWInfo->pOrderBy==0 + || pWInfo->pOrderBy->nExpr==1 + || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); + if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 + && pWInfo->nOBSat>0 + && (pIdx->nKeyCol>nEq) + ){ + assert( pLoop->nSkip==0 ); + bSeekPastNull = 1; + nExtraReg = 1; + } + + /* Find any inequality constraint terms for the start and end + ** of the range. + */ + j = nEq; + if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ + pRangeStart = pLoop->aLTerm[j++]; + nExtraReg = 1; + /* Like optimization range constraints always occur in pairs */ + assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || + (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); + } + if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ + pRangeEnd = pLoop->aLTerm[j++]; + nExtraReg = 1; +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS + if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ + assert( pRangeStart!=0 ); /* LIKE opt constraints */ + assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ + pLevel->iLikeRepCntr = (u32)++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr); + VdbeComment((v, "LIKE loop counter")); + pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); + /* iLikeRepCntr actually stores 2x the counter register number. The + ** bottom bit indicates whether the search order is ASC or DESC. */ + testcase( bRev ); + testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); + assert( (bRev & ~1)==0 ); + pLevel->iLikeRepCntr <<=1; + pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC); + } +#endif + if( pRangeStart==0 + && (j = pIdx->aiColumn[nEq])>=0 + && pIdx->pTable->aCol[j].notNull==0 + ){ + bSeekPastNull = 1; + } + } + assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); + + /* If we are doing a reverse order scan on an ascending index, or + ** a forward order scan on a descending index, interchange the + ** start and end terms (pRangeStart and pRangeEnd). + */ + if( (nEqnKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) + || (bRev && pIdx->nKeyCol==nEq) + ){ + SWAP(WhereTerm *, pRangeEnd, pRangeStart); + SWAP(u8, bSeekPastNull, bStopAtNull); + } + + /* Generate code to evaluate all constraint terms using == or IN + ** and store the values of those terms in an array of registers + ** starting at regBase. + */ + codeCursorHint(pWInfo, pLevel, pRangeEnd); + regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); + assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); + if( zStartAff ) cEndAff = zStartAff[nEq]; + addrNxt = pLevel->addrNxt; + + testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); + testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); + startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); + endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); + start_constraints = pRangeStart || nEq>0; + + /* Seek the index cursor to the start of the range. */ + nConstraint = nEq; + if( pRangeStart ){ + Expr *pRight = pRangeStart->pExpr->pRight; + sqlite3ExprCode(pParse, pRight, regBase+nEq); + whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); + if( (pRangeStart->wtFlags & TERM_VNULL)==0 + && sqlite3ExprCanBeNull(pRight) + ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); + VdbeCoverage(v); + } + if( zStartAff ){ + if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){ + /* Since the comparison is to be performed with no conversions + ** applied to the operands, set the affinity to apply to pRight to + ** SQLITE_AFF_BLOB. */ + zStartAff[nEq] = SQLITE_AFF_BLOB; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ + zStartAff[nEq] = SQLITE_AFF_BLOB; + } + } + nConstraint++; + testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); + }else if( bSeekPastNull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + nConstraint++; + startEq = 0; + start_constraints = 1; + } + codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); + if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){ + /* The skip-scan logic inside the call to codeAllEqualityConstraints() + ** above has already left the cursor sitting on the correct row, + ** so no further seeking is needed */ + }else{ + op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; + assert( op!=0 ); + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + VdbeCoverage(v); + VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); + VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); + VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); + VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); + VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); + VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); + } + + /* Load the value for the inequality constraint at the end of the + ** range (if any). + */ + nConstraint = nEq; + if( pRangeEnd ){ + Expr *pRight = pRangeEnd->pExpr->pRight; + sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); + sqlite3ExprCode(pParse, pRight, regBase+nEq); + whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); + if( (pRangeEnd->wtFlags & TERM_VNULL)==0 + && sqlite3ExprCanBeNull(pRight) + ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); + VdbeCoverage(v); + } + if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB + && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) + ){ + codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); + } + nConstraint++; + testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); + }else if( bStopAtNull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + endEq = 0; + nConstraint++; + } + sqlite3DbFree(db, zStartAff); + + /* Top of the loop body */ + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + + /* Check if the index cursor is past the end of the range. */ + if( nConstraint ){ + op = aEndOp[bRev*2 + endEq]; + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); + testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); + testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); + testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); + } + + /* Seek the table cursor, if required */ + disableTerm(pLevel, pRangeStart); + disableTerm(pLevel, pRangeEnd); + if( omitTable ){ + /* pIdx is a covering index. No need to access the main table. */ + }else if( HasRowid(pIdx->pTable) ){ + if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){ + iRowidReg = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); + VdbeCoverage(v); + }else{ + codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur); + } + }else if( iCur!=iIdxCur ){ + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); + iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); + for(j=0; jnKeyCol; j++){ + k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); + } + sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, + iRowidReg, pPk->nKeyCol); VdbeCoverage(v); + } + + /* Record the instruction used to terminate the loop. Disable + ** WHERE clause terms made redundant by the index range scan. + */ + if( pLoop->wsFlags & WHERE_ONEROW ){ + pLevel->op = OP_Noop; + }else if( bRev ){ + pLevel->op = OP_Prev; + }else{ + pLevel->op = OP_Next; + } + pLevel->p1 = iIdxCur; + pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; + if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + }else{ + assert( pLevel->p5==0 ); + } + }else + +#ifndef SQLITE_OMIT_OR_OPTIMIZATION + if( pLoop->wsFlags & WHERE_MULTI_OR ){ + /* Case 5: Two or more separately indexed terms connected by OR + ** + ** Example: + ** + ** CREATE TABLE t1(a,b,c,d); + ** CREATE INDEX i1 ON t1(a); + ** CREATE INDEX i2 ON t1(b); + ** CREATE INDEX i3 ON t1(c); + ** + ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) + ** + ** In the example, there are three indexed terms connected by OR. + ** The top of the loop looks like this: + ** + ** Null 1 # Zero the rowset in reg 1 + ** + ** Then, for each indexed term, the following. The arguments to + ** RowSetTest are such that the rowid of the current row is inserted + ** into the RowSet. If it is already present, control skips the + ** Gosub opcode and jumps straight to the code generated by WhereEnd(). + ** + ** sqlite3WhereBegin() + ** RowSetTest # Insert rowid into rowset + ** Gosub 2 A + ** sqlite3WhereEnd() + ** + ** Following the above, code to terminate the loop. Label A, the target + ** of the Gosub above, jumps to the instruction right after the Goto. + ** + ** Null 1 # Zero the rowset in reg 1 + ** Goto B # The loop is finished. + ** + ** A: # Return data, whatever. + ** + ** Return 2 # Jump back to the Gosub + ** + ** B: + ** + ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then + ** use an ephemeral index instead of a RowSet to record the primary + ** keys of the rows we have already seen. + ** + */ + WhereClause *pOrWc; /* The OR-clause broken out into subterms */ + SrcList *pOrTab; /* Shortened table list or OR-clause generation */ + Index *pCov = 0; /* Potential covering index (or NULL) */ + int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ + + int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ + int regRowset = 0; /* Register for RowSet object */ + int regRowid = 0; /* Register holding rowid */ + int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ + int iRetInit; /* Address of regReturn init */ + int untestedTerms = 0; /* Some terms not completely tested */ + int ii; /* Loop counter */ + u16 wctrlFlags; /* Flags for sub-WHERE clause */ + Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ + Table *pTab = pTabItem->pTab; + + pTerm = pLoop->aLTerm[0]; + assert( pTerm!=0 ); + assert( pTerm->eOperator & WO_OR ); + assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); + pOrWc = &pTerm->u.pOrInfo->wc; + pLevel->op = OP_Return; + pLevel->p1 = regReturn; + + /* Set up a new SrcList in pOrTab containing the table being scanned + ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. + ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). + */ + if( pWInfo->nLevel>1 ){ + int nNotReady; /* The number of notReady tables */ + struct SrcList_item *origSrc; /* Original list of tables */ + nNotReady = pWInfo->nLevel - iLevel - 1; + pOrTab = sqlite3StackAllocRaw(db, + sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); + if( pOrTab==0 ) return notReady; + pOrTab->nAlloc = (u8)(nNotReady + 1); + pOrTab->nSrc = pOrTab->nAlloc; + memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); + origSrc = pWInfo->pTabList->a; + for(k=1; k<=nNotReady; k++){ + memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); + } + }else{ + pOrTab = pWInfo->pTabList; + } + + /* Initialize the rowset register to contain NULL. An SQL NULL is + ** equivalent to an empty rowset. Or, create an ephemeral index + ** capable of holding primary keys in the case of a WITHOUT ROWID. + ** + ** Also initialize regReturn to contain the address of the instruction + ** immediately following the OP_Return at the bottom of the loop. This + ** is required in a few obscure LEFT JOIN cases where control jumps + ** over the top of the loop into the body of it. In this case the + ** correct response for the end-of-loop code (the OP_Return) is to + ** fall through to the next instruction, just as an OP_Next does if + ** called on an uninitialized cursor. + */ + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + if( HasRowid(pTab) ){ + regRowset = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + regRowset = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + } + regRowid = ++pParse->nMem; + } + iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); + + /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y + ** Then for every term xN, evaluate as the subexpression: xN AND z + ** That way, terms in y that are factored into the disjunction will + ** be picked up by the recursive calls to sqlite3WhereBegin() below. + ** + ** Actually, each subexpression is converted to "xN AND w" where w is + ** the "interesting" terms of z - terms that did not originate in the + ** ON or USING clause of a LEFT JOIN, and terms that are usable as + ** indices. + ** + ** This optimization also only applies if the (x1 OR x2 OR ...) term + ** is not contained in the ON clause of a LEFT JOIN. + ** See ticket http://www.sqlite.org/src/info/f2369304e4 + */ + if( pWC->nTerm>1 ){ + int iTerm; + for(iTerm=0; iTermnTerm; iTerm++){ + Expr *pExpr = pWC->a[iTerm].pExpr; + if( &pWC->a[iTerm] == pTerm ) continue; + if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; + testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); + testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); + if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; + if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; + testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); + pExpr = sqlite3ExprDup(db, pExpr, 0); + pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); + } + if( pAndExpr ){ + pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr, 0); + } + } + + /* Run a separate WHERE clause for each term of the OR clause. After + ** eliminating duplicates from other WHERE clauses, the action for each + ** sub-WHERE clause is to to invoke the main loop body as a subroutine. + */ + wctrlFlags = WHERE_OMIT_OPEN_CLOSE + | WHERE_FORCE_TABLE + | WHERE_ONETABLE_ONLY + | WHERE_NO_AUTOINDEX + | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE); + for(ii=0; iinTerm; ii++){ + WhereTerm *pOrTerm = &pOrWc->a[ii]; + if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ + WhereInfo *pSubWInfo; /* Info for single OR-term scan */ + Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ + int jmp1 = 0; /* Address of jump operation */ + if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ + pAndExpr->pLeft = pOrExpr; + pOrExpr = pAndExpr; + } + /* Loop through table entries that match term pOrTerm. */ + WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, + wctrlFlags, iCovCur); + assert( pSubWInfo || pParse->nErr || db->mallocFailed ); + if( pSubWInfo ){ + WhereLoop *pSubLoop; + int addrExplain = sqlite3WhereExplainOneScan( + pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 + ); + sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); + + /* This is the sub-WHERE clause body. First skip over + ** duplicate rows from prior sub-WHERE clauses, and record the + ** rowid (or PRIMARY KEY) for the current row so that the same + ** row will be skipped in subsequent sub-WHERE clauses. + */ + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + int r; + int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); + if( HasRowid(pTab) ){ + r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); + jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, + r,iSet); + VdbeCoverage(v); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + int nPk = pPk->nKeyCol; + int iPk; + + /* Read the PK into an array of temp registers. */ + r = sqlite3GetTempRange(pParse, nPk); + for(iPk=0; iPkaiColumn[iPk]; + sqlite3ExprCodeGetColumnToReg(pParse, pTab, iCol, iCur, r+iPk); + } + + /* Check if the temp table already contains this key. If so, + ** the row has already been included in the result set and + ** can be ignored (by jumping past the Gosub below). Otherwise, + ** insert the key into the temp table and proceed with processing + ** the row. + ** + ** Use some of the same optimizations as OP_RowSetTest: If iSet + ** is zero, assume that the key cannot already be present in + ** the temp table. And if iSet is -1, assume that there is no + ** need to insert the key into the temp table, as it will never + ** be tested for. */ + if( iSet ){ + jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); + VdbeCoverage(v); + } + if( iSet>=0 ){ + sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); + sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); + if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + } + + /* Release the array of temp registers */ + sqlite3ReleaseTempRange(pParse, r, nPk); + } + } + + /* Invoke the main loop body as a subroutine */ + sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); + + /* Jump here (skipping the main loop body subroutine) if the + ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ + if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1); + + /* The pSubWInfo->untestedTerms flag means that this OR term + ** contained one or more AND term from a notReady table. The + ** terms from the notReady table could not be tested and will + ** need to be tested later. + */ + if( pSubWInfo->untestedTerms ) untestedTerms = 1; + + /* If all of the OR-connected terms are optimized using the same + ** index, and the index is opened using the same cursor number + ** by each call to sqlite3WhereBegin() made by this loop, it may + ** be possible to use that index as a covering index. + ** + ** If the call to sqlite3WhereBegin() above resulted in a scan that + ** uses an index, and this is either the first OR-connected term + ** processed or the index is the same as that used by all previous + ** terms, set pCov to the candidate covering index. Otherwise, set + ** pCov to NULL to indicate that no candidate covering index will + ** be available. + */ + pSubLoop = pSubWInfo->a[0].pWLoop; + assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); + if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 + && (ii==0 || pSubLoop->u.btree.pIndex==pCov) + && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) + ){ + assert( pSubWInfo->a[0].iIdxCur==iCovCur ); + pCov = pSubLoop->u.btree.pIndex; + wctrlFlags |= WHERE_REOPEN_IDX; + }else{ + pCov = 0; + } + + /* Finish the loop through table entries that match term pOrTerm. */ + sqlite3WhereEnd(pSubWInfo); + } + } + } + pLevel->u.pCovidx = pCov; + if( pCov ) pLevel->iIdxCur = iCovCur; + if( pAndExpr ){ + pAndExpr->pLeft = 0; + sqlite3ExprDelete(db, pAndExpr); + } + sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); + sqlite3VdbeGoto(v, pLevel->addrBrk); + sqlite3VdbeResolveLabel(v, iLoopBody); + + if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); + if( !untestedTerms ) disableTerm(pLevel, pTerm); + }else +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + + { + /* Case 6: There is no usable index. We must do a complete + ** scan of the entire table. + */ + static const u8 aStep[] = { OP_Next, OP_Prev }; + static const u8 aStart[] = { OP_Rewind, OP_Last }; + assert( bRev==0 || bRev==1 ); + if( pTabItem->fg.isRecursive ){ + /* Tables marked isRecursive have only a single row that is stored in + ** a pseudo-cursor. No need to Rewind or Next such cursors. */ + pLevel->op = OP_Noop; + }else{ + codeCursorHint(pWInfo, pLevel, 0); + pLevel->op = aStep[bRev]; + pLevel->p1 = iCur; + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + } + } + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); +#endif + + /* Insert code to test every subexpression that can be completely + ** computed using the current set of tables. + */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE; + int skipLikeAddr = 0; + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ + testcase( pWInfo->untestedTerms==0 + && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); + pWInfo->untestedTerms = 1; + continue; + } + pE = pTerm->pExpr; + assert( pE!=0 ); + if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ + continue; + } + if( pTerm->wtFlags & TERM_LIKECOND ){ + /* If the TERM_LIKECOND flag is set, that means that the range search + ** is sufficient to guarantee that the LIKE operator is true, so we + ** can skip the call to the like(A,B) function. But this only works + ** for strings. So do not skip the call to the function on the pass + ** that compares BLOBs. */ +#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS + continue; +#else + u32 x = pLevel->iLikeRepCntr; + assert( x>0 ); + skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)? OP_IfNot : OP_If, (int)(x>>1)); + VdbeCoverage(v); +#endif + } + sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); + if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); + pTerm->wtFlags |= TERM_CODED; + } + + /* Insert code to test for implied constraints based on transitivity + ** of the "==" operator. + ** + ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" + ** and we are coding the t1 loop and the t2 loop has not yet coded, + ** then we cannot use the "t1.a=t2.b" constraint, but we can code + ** the implied "t1.a=123" constraint. + */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE, *pEAlt; + WhereTerm *pAlt; + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; + if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; + if( pTerm->leftCursor!=iCur ) continue; + if( pLevel->iLeftJoin ) continue; + pE = pTerm->pExpr; + assert( !ExprHasProperty(pE, EP_FromJoin) ); + assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); + pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady, + WO_EQ|WO_IN|WO_IS, 0); + if( pAlt==0 ) continue; + if( pAlt->wtFlags & (TERM_CODED) ) continue; + testcase( pAlt->eOperator & WO_EQ ); + testcase( pAlt->eOperator & WO_IS ); + testcase( pAlt->eOperator & WO_IN ); + VdbeModuleComment((v, "begin transitive constraint")); + pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); + if( pEAlt ){ + *pEAlt = *pAlt->pExpr; + pEAlt->pLeft = pE->pLeft; + sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); + sqlite3StackFree(db, pEAlt); + } + } + + /* For a LEFT OUTER JOIN, generate code that will record the fact that + ** at least one row of the right table has matched the left table. + */ + if( pLevel->iLeftJoin ){ + pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); + VdbeComment((v, "record LEFT JOIN hit")); + sqlite3ExprCacheClear(pParse); + for(pTerm=pWC->a, j=0; jnTerm; j++, pTerm++){ + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ + assert( pWInfo->untestedTerms ); + continue; + } + assert( pTerm->pExpr ); + sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); + pTerm->wtFlags |= TERM_CODED; + } + } + + return pLevel->notReady; +} + +/************** End of wherecode.c *******************************************/ +/************** Begin file whereexpr.c ***************************************/ +/* +** 2015-06-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This module contains C code that generates VDBE code used to process +** the WHERE clause of SQL statements. +** +** This file was originally part of where.c but was split out to improve +** readability and editabiliity. This file contains utility routines for +** analyzing Expr objects in the WHERE clause. +*/ +/* #include "sqliteInt.h" */ +/* #include "whereInt.h" */ + +/* Forward declarations */ +static void exprAnalyze(SrcList*, WhereClause*, int); + +/* +** Deallocate all memory associated with a WhereOrInfo object. +*/ +static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ + sqlite3WhereClauseClear(&p->wc); + sqlite3DbFree(db, p); +} + +/* +** Deallocate all memory associated with a WhereAndInfo object. +*/ +static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ + sqlite3WhereClauseClear(&p->wc); + sqlite3DbFree(db, p); +} + +/* +** Add a single new WhereTerm entry to the WhereClause object pWC. +** The new WhereTerm object is constructed from Expr p and with wtFlags. +** The index in pWC->a[] of the new WhereTerm is returned on success. +** 0 is returned if the new WhereTerm could not be added due to a memory +** allocation error. The memory allocation failure will be recorded in +** the db->mallocFailed flag so that higher-level functions can detect it. +** +** This routine will increase the size of the pWC->a[] array as necessary. +** +** If the wtFlags argument includes TERM_DYNAMIC, then responsibility +** for freeing the expression p is assumed by the WhereClause object pWC. +** This is true even if this routine fails to allocate a new WhereTerm. +** +** WARNING: This routine might reallocate the space used to store +** WhereTerms. All pointers to WhereTerms should be invalidated after +** calling this routine. Such pointers may be reinitialized by referencing +** the pWC->a[] array. +*/ +static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ + WhereTerm *pTerm; + int idx; + testcase( wtFlags & TERM_VIRTUAL ); + if( pWC->nTerm>=pWC->nSlot ){ + WhereTerm *pOld = pWC->a; + sqlite3 *db = pWC->pWInfo->pParse->db; + pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); + if( pWC->a==0 ){ + if( wtFlags & TERM_DYNAMIC ){ + sqlite3ExprDelete(db, p); + } + pWC->a = pOld; + return 0; + } + memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); + if( pOld!=pWC->aStatic ){ + sqlite3DbFree(db, pOld); + } + pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); + memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm)); + } + pTerm = &pWC->a[idx = pWC->nTerm++]; + if( p && ExprHasProperty(p, EP_Unlikely) ){ + pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; + }else{ + pTerm->truthProb = 1; + } + pTerm->pExpr = sqlite3ExprSkipCollate(p); + pTerm->wtFlags = wtFlags; + pTerm->pWC = pWC; + pTerm->iParent = -1; + return idx; +} + +/* +** Return TRUE if the given operator is one of the operators that is +** allowed for an indexable WHERE clause term. The allowed operators are +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" +*/ +static int allowedOp(int op){ + assert( TK_GT>TK_EQ && TK_GTTK_EQ && TK_LTTK_EQ && TK_LE=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS; +} + +/* +** Commute a comparison operator. Expressions of the form "X op Y" +** are converted into "Y op X". +** +** If left/right precedence rules come into play when determining the +** collating sequence, then COLLATE operators are adjusted to ensure +** that the collating sequence does not change. For example: +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on +** the left hand side of a comparison overrides any collation sequence +** attached to the right. For the same reason the EP_Collate flag +** is not commuted. +*/ +static void exprCommute(Parse *pParse, Expr *pExpr){ + u16 expRight = (pExpr->pRight->flags & EP_Collate); + u16 expLeft = (pExpr->pLeft->flags & EP_Collate); + assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); + if( expRight==expLeft ){ + /* Either X and Y both have COLLATE operator or neither do */ + if( expRight ){ + /* Both X and Y have COLLATE operators. Make sure X is always + ** used by clearing the EP_Collate flag from Y. */ + pExpr->pRight->flags &= ~EP_Collate; + }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ + /* Neither X nor Y have COLLATE operators, but X has a non-default + ** collating sequence. So add the EP_Collate marker on X to cause + ** it to be searched first. */ + pExpr->pLeft->flags |= EP_Collate; + } + } + SWAP(Expr*,pExpr->pRight,pExpr->pLeft); + if( pExpr->op>=TK_GT ){ + assert( TK_LT==TK_GT+2 ); + assert( TK_GE==TK_LE+2 ); + assert( TK_GT>TK_EQ ); + assert( TK_GTop>=TK_GT && pExpr->op<=TK_GE ); + pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; + } +} + +/* +** Translate from TK_xx operator to WO_xx bitmask. +*/ +static u16 operatorMask(int op){ + u16 c; + assert( allowedOp(op) ); + if( op==TK_IN ){ + c = WO_IN; + }else if( op==TK_ISNULL ){ + c = WO_ISNULL; + }else if( op==TK_IS ){ + c = WO_IS; + }else{ + assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); + c = (u16)(WO_EQ<<(op-TK_EQ)); + } + assert( op!=TK_ISNULL || c==WO_ISNULL ); + assert( op!=TK_IN || c==WO_IN ); + assert( op!=TK_EQ || c==WO_EQ ); + assert( op!=TK_LT || c==WO_LT ); + assert( op!=TK_LE || c==WO_LE ); + assert( op!=TK_GT || c==WO_GT ); + assert( op!=TK_GE || c==WO_GE ); + assert( op!=TK_IS || c==WO_IS ); + return c; +} + + +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION +/* +** Check to see if the given expression is a LIKE or GLOB operator that +** can be optimized using inequality constraints. Return TRUE if it is +** so and false if not. +** +** In order for the operator to be optimizible, the RHS must be a string +** literal that does not begin with a wildcard. The LHS must be a column +** that may only be NULL, a string, or a BLOB, never a number. (This means +** that virtual tables cannot participate in the LIKE optimization.) The +** collating sequence for the column on the LHS must be appropriate for +** the operator. +*/ +static int isLikeOrGlob( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* Test this expression */ + Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ + int *pisComplete, /* True if the only wildcard is % in the last character */ + int *pnoCase /* True if uppercase is equivalent to lowercase */ +){ + const char *z = 0; /* String on RHS of LIKE operator */ + Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ + ExprList *pList; /* List of operands to the LIKE operator */ + int c; /* One character in z[] */ + int cnt; /* Number of non-wildcard prefix characters */ + char wc[3]; /* Wildcard characters */ + sqlite3 *db = pParse->db; /* Database connection */ + sqlite3_value *pVal = 0; + int op; /* Opcode of pRight */ + int rc; /* Result code to return */ + + if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ + return 0; + } +#ifdef SQLITE_EBCDIC + if( *pnoCase ) return 0; +#endif + pList = pExpr->x.pList; + pLeft = pList->a[1].pExpr; + if( pLeft->op!=TK_COLUMN + || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT + || IsVirtual(pLeft->pTab) /* Value might be numeric */ + ){ + /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must + ** be the name of an indexed column with TEXT affinity. */ + return 0; + } + assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ + + pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); + op = pRight->op; + if( op==TK_VARIABLE ){ + Vdbe *pReprepare = pParse->pReprepare; + int iCol = pRight->iColumn; + pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); + if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ + z = (char *)sqlite3_value_text(pVal); + } + sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); + assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); + }else if( op==TK_STRING ){ + z = pRight->u.zToken; + } + if( z ){ + cnt = 0; + while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ + cnt++; + } + if( cnt!=0 && 255!=(u8)z[cnt-1] ){ + Expr *pPrefix; + *pisComplete = c==wc[0] && z[cnt+1]==0; + pPrefix = sqlite3Expr(db, TK_STRING, z); + if( pPrefix ) pPrefix->u.zToken[cnt] = 0; + *ppPrefix = pPrefix; + if( op==TK_VARIABLE ){ + Vdbe *v = pParse->pVdbe; + sqlite3VdbeSetVarmask(v, pRight->iColumn); + if( *pisComplete && pRight->u.zToken[1] ){ + /* If the rhs of the LIKE expression is a variable, and the current + ** value of the variable means there is no need to invoke the LIKE + ** function, then no OP_Variable will be added to the program. + ** This causes problems for the sqlite3_bind_parameter_name() + ** API. To work around them, add a dummy OP_Variable here. + */ + int r1 = sqlite3GetTempReg(pParse); + sqlite3ExprCodeTarget(pParse, pRight, r1); + sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); + sqlite3ReleaseTempReg(pParse, r1); + } + } + }else{ + z = 0; + } + } + + rc = (z!=0); + sqlite3ValueFree(pVal); + return rc; +} +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ + + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Check to see if the given expression is of the form +** +** column OP expr +** +** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a +** column of a virtual table. +** +** If it is then return TRUE. If not, return FALSE. +*/ +static int isMatchOfColumn( + Expr *pExpr, /* Test this expression */ + unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */ +){ + struct Op2 { + const char *zOp; + unsigned char eOp2; + } aOp[] = { + { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, + { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, + { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, + { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } + }; + ExprList *pList; + Expr *pCol; /* Column reference */ + int i; + + if( pExpr->op!=TK_FUNCTION ){ + return 0; + } + pList = pExpr->x.pList; + if( pList==0 || pList->nExpr!=2 ){ + return 0; + } + pCol = pList->a[1].pExpr; + if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){ + return 0; + } + for(i=0; iu.zToken, aOp[i].zOp)==0 ){ + *peOp2 = aOp[i].eOp2; + return 1; + } + } + return 0; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** If the pBase expression originated in the ON or USING clause of +** a join, then transfer the appropriate markings over to derived. +*/ +static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ + if( pDerived ){ + pDerived->flags |= pBase->flags & EP_FromJoin; + pDerived->iRightJoinTable = pBase->iRightJoinTable; + } +} + +/* +** Mark term iChild as being a child of term iParent +*/ +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ + pWC->a[iChild].iParent = iParent; + pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; + pWC->a[iParent].nChild++; +} + +/* +** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not +** a conjunction, then return just pTerm when N==0. If N is exceeds +** the number of available subterms, return NULL. +*/ +static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ + if( pTerm->eOperator!=WO_AND ){ + return N==0 ? pTerm : 0; + } + if( Nu.pAndInfo->wc.nTerm ){ + return &pTerm->u.pAndInfo->wc.a[N]; + } + return 0; +} + +/* +** Subterms pOne and pTwo are contained within WHERE clause pWC. The +** two subterms are in disjunction - they are OR-ed together. +** +** If these two terms are both of the form: "A op B" with the same +** A and B values but different operators and if the operators are +** compatible (if one is = and the other is <, for example) then +** add a new virtual AND term to pWC that is the combination of the +** two. +** +** Some examples: +** +** x x<=y +** x=y OR x=y --> x=y +** x<=y OR x x<=y +** +** The following is NOT generated: +** +** xy --> x!=y +*/ +static void whereCombineDisjuncts( + SrcList *pSrc, /* the FROM clause */ + WhereClause *pWC, /* The complete WHERE clause */ + WhereTerm *pOne, /* First disjunct */ + WhereTerm *pTwo /* Second disjunct */ +){ + u16 eOp = pOne->eOperator | pTwo->eOperator; + sqlite3 *db; /* Database connection (for malloc) */ + Expr *pNew; /* New virtual expression */ + int op; /* Operator for the combined expression */ + int idxNew; /* Index in pWC of the next virtual term */ + + if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; + if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; + if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp + && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; + assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); + assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); + if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; + if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; + /* If we reach this point, it means the two subterms can be combined */ + if( (eOp & (eOp-1))!=0 ){ + if( eOp & (WO_LT|WO_LE) ){ + eOp = WO_LE; + }else{ + assert( eOp & (WO_GT|WO_GE) ); + eOp = WO_GE; + } + } + db = pWC->pWInfo->pParse->db; + pNew = sqlite3ExprDup(db, pOne->pExpr, 0); + if( pNew==0 ) return; + for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( opop = op; + idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); + exprAnalyze(pSrc, pWC, idxNew); +} + +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) +/* +** Analyze a term that consists of two or more OR-connected +** subterms. So in: +** +** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) +** ^^^^^^^^^^^^^^^^^^^^ +** +** This routine analyzes terms such as the middle term in the above example. +** A WhereOrTerm object is computed and attached to the term under +** analysis, regardless of the outcome of the analysis. Hence: +** +** WhereTerm.wtFlags |= TERM_ORINFO +** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object +** +** The term being analyzed must have two or more of OR-connected subterms. +** A single subterm might be a set of AND-connected sub-subterms. +** Examples of terms under analysis: +** +** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 +** (B) x=expr1 OR expr2=x OR x=expr3 +** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) +** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') +** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) +** (F) x>A OR (x=A AND y>=B) +** +** CASE 1: +** +** If all subterms are of the form T.C=expr for some single column of C and +** a single table T (as shown in example B above) then create a new virtual +** term that is an equivalent IN expression. In other words, if the term +** being analyzed is: +** +** x = expr1 OR expr2 = x OR x = expr3 +** +** then create a new virtual term like this: +** +** x IN (expr1,expr2,expr3) +** +** CASE 2: +** +** If there are exactly two disjuncts and one side has x>A and the other side +** has x=A (for the same x and A) then add a new virtual conjunct term to the +** WHERE clause of the form "x>=A". Example: +** +** x>A OR (x=A AND y>B) adds: x>=A +** +** The added conjunct can sometimes be helpful in query planning. +** +** CASE 3: +** +** If all subterms are indexable by a single table T, then set +** +** WhereTerm.eOperator = WO_OR +** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T +** +** A subterm is "indexable" if it is of the form +** "T.C " where C is any column of table T and +** is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". +** A subterm is also indexable if it is an AND of two or more +** subsubterms at least one of which is indexable. Indexable AND +** subterms have their eOperator set to WO_AND and they have +** u.pAndInfo set to a dynamically allocated WhereAndTerm object. +** +** From another point of view, "indexable" means that the subterm could +** potentially be used with an index if an appropriate index exists. +** This analysis does not consider whether or not the index exists; that +** is decided elsewhere. This analysis only looks at whether subterms +** appropriate for indexing exist. +** +** All examples A through E above satisfy case 3. But if a term +** also satisfies case 1 (such as B) we know that the optimizer will +** always prefer case 1, so in that case we pretend that case 3 is not +** satisfied. +** +** It might be the case that multiple tables are indexable. For example, +** (E) above is indexable on tables P, Q, and R. +** +** Terms that satisfy case 3 are candidates for lookup by using +** separate indices to find rowids for each subterm and composing +** the union of all rowids using a RowSet object. This is similar +** to "bitmap indices" in other database engines. +** +** OTHERWISE: +** +** If none of cases 1, 2, or 3 apply, then leave the eOperator set to +** zero. This term is not useful for search. +*/ +static void exprAnalyzeOrTerm( + SrcList *pSrc, /* the FROM clause */ + WhereClause *pWC, /* the complete WHERE clause */ + int idxTerm /* Index of the OR-term to be analyzed */ +){ + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ + Parse *pParse = pWInfo->pParse; /* Parser context */ + sqlite3 *db = pParse->db; /* Database connection */ + WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ + Expr *pExpr = pTerm->pExpr; /* The expression of the term */ + int i; /* Loop counters */ + WhereClause *pOrWc; /* Breakup of pTerm into subterms */ + WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ + WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ + Bitmask chngToIN; /* Tables that might satisfy case 1 */ + Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ + + /* + ** Break the OR clause into its separate subterms. The subterms are + ** stored in a WhereClause structure containing within the WhereOrInfo + ** object that is attached to the original OR clause term. + */ + assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); + assert( pExpr->op==TK_OR ); + pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); + if( pOrInfo==0 ) return; + pTerm->wtFlags |= TERM_ORINFO; + pOrWc = &pOrInfo->wc; + memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic)); + sqlite3WhereClauseInit(pOrWc, pWInfo); + sqlite3WhereSplit(pOrWc, pExpr, TK_OR); + sqlite3WhereExprAnalyze(pSrc, pOrWc); + if( db->mallocFailed ) return; + assert( pOrWc->nTerm>=2 ); + + /* + ** Compute the set of tables that might satisfy cases 1 or 3. + */ + indexable = ~(Bitmask)0; + chngToIN = ~(Bitmask)0; + for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ + if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ + WhereAndInfo *pAndInfo; + assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); + chngToIN = 0; + pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo)); + if( pAndInfo ){ + WhereClause *pAndWC; + WhereTerm *pAndTerm; + int j; + Bitmask b = 0; + pOrTerm->u.pAndInfo = pAndInfo; + pOrTerm->wtFlags |= TERM_ANDINFO; + pOrTerm->eOperator = WO_AND; + pAndWC = &pAndInfo->wc; + memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic)); + sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); + sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); + sqlite3WhereExprAnalyze(pSrc, pAndWC); + pAndWC->pOuter = pWC; + if( !db->mallocFailed ){ + for(j=0, pAndTerm=pAndWC->a; jnTerm; j++, pAndTerm++){ + assert( pAndTerm->pExpr ); + if( allowedOp(pAndTerm->pExpr->op) ){ + b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); + } + } + } + indexable &= b; + } + }else if( pOrTerm->wtFlags & TERM_COPIED ){ + /* Skip this term for now. We revisit it when we process the + ** corresponding TERM_VIRTUAL term */ + }else{ + Bitmask b; + b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); + if( pOrTerm->wtFlags & TERM_VIRTUAL ){ + WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; + b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); + } + indexable &= b; + if( (pOrTerm->eOperator & WO_EQ)==0 ){ + chngToIN = 0; + }else{ + chngToIN &= b; + } + } + } + + /* + ** Record the set of tables that satisfy case 3. The set might be + ** empty. + */ + pOrInfo->indexable = indexable; + pTerm->eOperator = indexable==0 ? 0 : WO_OR; + + /* For a two-way OR, attempt to implementation case 2. + */ + if( indexable && pOrWc->nTerm==2 ){ + int iOne = 0; + WhereTerm *pOne; + while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ + int iTwo = 0; + WhereTerm *pTwo; + while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ + whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); + } + } + } + + /* + ** chngToIN holds a set of tables that *might* satisfy case 1. But + ** we have to do some additional checking to see if case 1 really + ** is satisfied. + ** + ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means + ** that there is no possibility of transforming the OR clause into an + ** IN operator because one or more terms in the OR clause contain + ** something other than == on a column in the single table. The 1-bit + ** case means that every term of the OR clause is of the form + ** "table.column=expr" for some single table. The one bit that is set + ** will correspond to the common table. We still need to check to make + ** sure the same column is used on all terms. The 2-bit case is when + ** the all terms are of the form "table1.column=table2.column". It + ** might be possible to form an IN operator with either table1.column + ** or table2.column as the LHS if either is common to every term of + ** the OR clause. + ** + ** Note that terms of the form "table.column1=table.column2" (the + ** same table on both sizes of the ==) cannot be optimized. + */ + if( chngToIN ){ + int okToChngToIN = 0; /* True if the conversion to IN is valid */ + int iColumn = -1; /* Column index on lhs of IN operator */ + int iCursor = -1; /* Table cursor common to all terms */ + int j = 0; /* Loop counter */ + + /* Search for a table and column that appears on one side or the + ** other of the == operator in every subterm. That table and column + ** will be recorded in iCursor and iColumn. There might not be any + ** such table and column. Set okToChngToIN if an appropriate table + ** and column is found but leave okToChngToIN false if not found. + */ + for(j=0; j<2 && !okToChngToIN; j++){ + pOrTerm = pOrWc->a; + for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ + assert( pOrTerm->eOperator & WO_EQ ); + pOrTerm->wtFlags &= ~TERM_OR_OK; + if( pOrTerm->leftCursor==iCursor ){ + /* This is the 2-bit case and we are on the second iteration and + ** current term is from the first iteration. So skip this term. */ + assert( j==1 ); + continue; + } + if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, + pOrTerm->leftCursor))==0 ){ + /* This term must be of the form t1.a==t2.b where t2 is in the + ** chngToIN set but t1 is not. This term will be either preceded + ** or follwed by an inverted copy (t2.b==t1.a). Skip this term + ** and use its inversion. */ + testcase( pOrTerm->wtFlags & TERM_COPIED ); + testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); + assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); + continue; + } + iColumn = pOrTerm->u.leftColumn; + iCursor = pOrTerm->leftCursor; + break; + } + if( i<0 ){ + /* No candidate table+column was found. This can only occur + ** on the second iteration */ + assert( j==1 ); + assert( IsPowerOfTwo(chngToIN) ); + assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); + break; + } + testcase( j==1 ); + + /* We have found a candidate table and column. Check to see if that + ** table and column is common to every term in the OR clause */ + okToChngToIN = 1; + for(; i>=0 && okToChngToIN; i--, pOrTerm++){ + assert( pOrTerm->eOperator & WO_EQ ); + if( pOrTerm->leftCursor!=iCursor ){ + pOrTerm->wtFlags &= ~TERM_OR_OK; + }else if( pOrTerm->u.leftColumn!=iColumn ){ + okToChngToIN = 0; + }else{ + int affLeft, affRight; + /* If the right-hand side is also a column, then the affinities + ** of both right and left sides must be such that no type + ** conversions are required on the right. (Ticket #2249) + */ + affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); + affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); + if( affRight!=0 && affRight!=affLeft ){ + okToChngToIN = 0; + }else{ + pOrTerm->wtFlags |= TERM_OR_OK; + } + } + } + } + + /* At this point, okToChngToIN is true if original pTerm satisfies + ** case 1. In that case, construct a new virtual term that is + ** pTerm converted into an IN operator. + */ + if( okToChngToIN ){ + Expr *pDup; /* A transient duplicate expression */ + ExprList *pList = 0; /* The RHS of the IN operator */ + Expr *pLeft = 0; /* The LHS of the IN operator */ + Expr *pNew; /* The complete IN operator */ + + for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ + if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; + assert( pOrTerm->eOperator & WO_EQ ); + assert( pOrTerm->leftCursor==iCursor ); + assert( pOrTerm->u.leftColumn==iColumn ); + pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); + pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); + pLeft = pOrTerm->pExpr->pLeft; + } + assert( pLeft!=0 ); + pDup = sqlite3ExprDup(db, pLeft, 0); + pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); + if( pNew ){ + int idxNew; + transferJoinMarkings(pNew, pExpr); + assert( !ExprHasProperty(pNew, EP_xIsSelect) ); + pNew->x.pList = pList; + idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + exprAnalyze(pSrc, pWC, idxNew); + pTerm = &pWC->a[idxTerm]; + markTermAsChild(pWC, idxNew, idxTerm); + }else{ + sqlite3ExprListDelete(db, pList); + } + pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ + } + } +} +#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ + +/* +** We already know that pExpr is a binary operator where both operands are +** column references. This routine checks to see if pExpr is an equivalence +** relation: +** 1. The SQLITE_Transitive optimization must be enabled +** 2. Must be either an == or an IS operator +** 3. Not originating in the ON clause of an OUTER JOIN +** 4. The affinities of A and B must be compatible +** 5a. Both operands use the same collating sequence OR +** 5b. The overall collating sequence is BINARY +** If this routine returns TRUE, that means that the RHS can be substituted +** for the LHS anyplace else in the WHERE clause where the LHS column occurs. +** This is an optimization. No harm comes from returning 0. But if 1 is +** returned when it should not be, then incorrect answers might result. +*/ +static int termIsEquivalence(Parse *pParse, Expr *pExpr){ + char aff1, aff2; + CollSeq *pColl; + const char *zColl1, *zColl2; + if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; + if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; + if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; + aff1 = sqlite3ExprAffinity(pExpr->pLeft); + aff2 = sqlite3ExprAffinity(pExpr->pRight); + if( aff1!=aff2 + && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) + ){ + return 0; + } + pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); + if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; + pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); + /* Since pLeft and pRight are both a column references, their collating + ** sequence should always be defined. */ + zColl1 = ALWAYS(pColl) ? pColl->zName : 0; + pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); + zColl2 = ALWAYS(pColl) ? pColl->zName : 0; + return sqlite3StrICmp(zColl1, zColl2)==0; +} + +/* +** Recursively walk the expressions of a SELECT statement and generate +** a bitmask indicating which tables are used in that expression +** tree. +*/ +static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ + Bitmask mask = 0; + while( pS ){ + SrcList *pSrc = pS->pSrc; + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList); + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy); + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy); + mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere); + mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving); + if( ALWAYS(pSrc!=0) ){ + int i; + for(i=0; inSrc; i++){ + mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect); + mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn); + } + } + pS = pS->pPrior; + } + return mask; +} + +/* +** Expression pExpr is one operand of a comparison operator that might +** be useful for indexing. This routine checks to see if pExpr appears +** in any index. Return TRUE (1) if pExpr is an indexed term and return +** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor +** number of the table that is indexed and *piColumn to the column number +** of the column that is indexed, or -2 if an expression is being indexed. +** +** If pExpr is a TK_COLUMN column reference, then this routine always returns +** true even if that particular column is not indexed, because the column +** might be added to an automatic index later. +*/ +static int exprMightBeIndexed( + SrcList *pFrom, /* The FROM clause */ + Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */ + Expr *pExpr, /* An operand of a comparison operator */ + int *piCur, /* Write the referenced table cursor number here */ + int *piColumn /* Write the referenced table column number here */ +){ + Index *pIdx; + int i; + int iCur; + if( pExpr->op==TK_COLUMN ){ + *piCur = pExpr->iTable; + *piColumn = pExpr->iColumn; + return 1; + } + if( mPrereq==0 ) return 0; /* No table references */ + if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */ + for(i=0; mPrereq>1; i++, mPrereq>>=1){} + iCur = pFrom->a[i].iCursor; + for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->aColExpr==0 ) continue; + for(i=0; inKeyCol; i++){ + if( pIdx->aiColumn[i]!=(-2) ) continue; + if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){ + *piCur = iCur; + *piColumn = -2; + return 1; + } + } + } + return 0; +} + +/* +** The input to this routine is an WhereTerm structure with only the +** "pExpr" field filled in. The job of this routine is to analyze the +** subexpression and populate all the other fields of the WhereTerm +** structure. +** +** If the expression is of the form " X" it gets commuted +** to the standard form of "X ". +** +** If the expression is of the form "X Y" where both X and Y are +** columns, then the original expression is unchanged and a new virtual +** term of the form "Y X" is added to the WHERE clause and +** analyzed separately. The original term is marked with TERM_COPIED +** and the new term is marked with TERM_DYNAMIC (because it's pExpr +** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it +** is a commuted copy of a prior term.) The original term has nChild=1 +** and the copy has idxParent set to the index of the original term. +*/ +static void exprAnalyze( + SrcList *pSrc, /* the FROM clause */ + WhereClause *pWC, /* the WHERE clause */ + int idxTerm /* Index of the term to be analyzed */ +){ + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ + WhereTerm *pTerm; /* The term to be analyzed */ + WhereMaskSet *pMaskSet; /* Set of table index masks */ + Expr *pExpr; /* The expression to be analyzed */ + Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ + Bitmask prereqAll; /* Prerequesites of pExpr */ + Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ + Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ + int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ + int noCase = 0; /* uppercase equivalent to lowercase */ + int op; /* Top-level operator. pExpr->op */ + Parse *pParse = pWInfo->pParse; /* Parsing context */ + sqlite3 *db = pParse->db; /* Database connection */ + unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */ + + if( db->mallocFailed ){ + return; + } + pTerm = &pWC->a[idxTerm]; + pMaskSet = &pWInfo->sMaskSet; + pExpr = pTerm->pExpr; + assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); + prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); + op = pExpr->op; + if( op==TK_IN ){ + assert( pExpr->pRight==0 ); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); + }else{ + pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); + } + }else if( op==TK_ISNULL ){ + pTerm->prereqRight = 0; + }else{ + pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); + } + prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr); + if( ExprHasProperty(pExpr, EP_FromJoin) ){ + Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); + prereqAll |= x; + extraRight = x-1; /* ON clause terms may not be used with an index + ** on left table of a LEFT JOIN. Ticket #3015 */ + } + pTerm->prereqAll = prereqAll; + pTerm->leftCursor = -1; + pTerm->iParent = -1; + pTerm->eOperator = 0; + if( allowedOp(op) ){ + int iCur, iColumn; + Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); + Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); + u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; + if( exprMightBeIndexed(pSrc, prereqLeft, pLeft, &iCur, &iColumn) ){ + pTerm->leftCursor = iCur; + pTerm->u.leftColumn = iColumn; + pTerm->eOperator = operatorMask(op) & opMask; + } + if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; + if( pRight + && exprMightBeIndexed(pSrc, pTerm->prereqRight, pRight, &iCur, &iColumn) + ){ + WhereTerm *pNew; + Expr *pDup; + u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ + if( pTerm->leftCursor>=0 ){ + int idxNew; + pDup = sqlite3ExprDup(db, pExpr, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDup); + return; + } + idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); + if( idxNew==0 ) return; + pNew = &pWC->a[idxNew]; + markTermAsChild(pWC, idxNew, idxTerm); + if( op==TK_IS ) pNew->wtFlags |= TERM_IS; + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_COPIED; + + if( termIsEquivalence(pParse, pDup) ){ + pTerm->eOperator |= WO_EQUIV; + eExtraOp = WO_EQUIV; + } + }else{ + pDup = pExpr; + pNew = pTerm; + } + exprCommute(pParse, pDup); + pNew->leftCursor = iCur; + pNew->u.leftColumn = iColumn; + testcase( (prereqLeft | extraRight) != prereqLeft ); + pNew->prereqRight = prereqLeft | extraRight; + pNew->prereqAll = prereqAll; + pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; + } + } + +#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION + /* If a term is the BETWEEN operator, create two new virtual terms + ** that define the range that the BETWEEN implements. For example: + ** + ** a BETWEEN b AND c + ** + ** is converted into: + ** + ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) + ** + ** The two new terms are added onto the end of the WhereClause object. + ** The new terms are "dynamic" and are children of the original BETWEEN + ** term. That means that if the BETWEEN term is coded, the children are + ** skipped. Or, if the children are satisfied by an index, the original + ** BETWEEN term is skipped. + */ + else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ + ExprList *pList = pExpr->x.pList; + int i; + static const u8 ops[] = {TK_GE, TK_LE}; + assert( pList!=0 ); + assert( pList->nExpr==2 ); + for(i=0; i<2; i++){ + Expr *pNewExpr; + int idxNew; + pNewExpr = sqlite3PExpr(pParse, ops[i], + sqlite3ExprDup(db, pExpr->pLeft, 0), + sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); + transferJoinMarkings(pNewExpr, pExpr); + idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + exprAnalyze(pSrc, pWC, idxNew); + pTerm = &pWC->a[idxTerm]; + markTermAsChild(pWC, idxNew, idxTerm); + } + } +#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ + +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) + /* Analyze a term that is composed of two or more subterms connected by + ** an OR operator. + */ + else if( pExpr->op==TK_OR ){ + assert( pWC->op==TK_AND ); + exprAnalyzeOrTerm(pSrc, pWC, idxTerm); + pTerm = &pWC->a[idxTerm]; + } +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION + /* Add constraints to reduce the search space on a LIKE or GLOB + ** operator. + ** + ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints + ** + ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' + ** + ** The last character of the prefix "abc" is incremented to form the + ** termination condition "abd". If case is not significant (the default + ** for LIKE) then the lower-bound is made all uppercase and the upper- + ** bound is made all lowercase so that the bounds also work when comparing + ** BLOBs. + */ + if( pWC->op==TK_AND + && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) + ){ + Expr *pLeft; /* LHS of LIKE/GLOB operator */ + Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ + Expr *pNewExpr1; + Expr *pNewExpr2; + int idxNew1; + int idxNew2; + const char *zCollSeqName; /* Name of collating sequence */ + const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; + + pLeft = pExpr->x.pList->a[1].pExpr; + pStr2 = sqlite3ExprDup(db, pStr1, 0); + + /* Convert the lower bound to upper-case and the upper bound to + ** lower-case (upper-case is less than lower-case in ASCII) so that + ** the range constraints also work for BLOBs + */ + if( noCase && !pParse->db->mallocFailed ){ + int i; + char c; + pTerm->wtFlags |= TERM_LIKE; + for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ + pStr1->u.zToken[i] = sqlite3Toupper(c); + pStr2->u.zToken[i] = sqlite3Tolower(c); + } + } + + if( !db->mallocFailed ){ + u8 c, *pC; /* Last character before the first wildcard */ + pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; + c = *pC; + if( noCase ){ + /* The point is to increment the last character before the first + ** wildcard. But if we increment '@', that will push it into the + ** alphabetic range where case conversions will mess up the + ** inequality. To avoid this, make sure to also run the full + ** LIKE on all candidate expressions by clearing the isComplete flag + */ + if( c=='A'-1 ) isComplete = 0; + c = sqlite3UpperToLower[c]; + } + *pC = c + 1; + } + zCollSeqName = noCase ? "NOCASE" : "BINARY"; + pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); + pNewExpr1 = sqlite3PExpr(pParse, TK_GE, + sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), + pStr1, 0); + transferJoinMarkings(pNewExpr1, pExpr); + idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); + testcase( idxNew1==0 ); + exprAnalyze(pSrc, pWC, idxNew1); + pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); + pNewExpr2 = sqlite3PExpr(pParse, TK_LT, + sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), + pStr2, 0); + transferJoinMarkings(pNewExpr2, pExpr); + idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); + testcase( idxNew2==0 ); + exprAnalyze(pSrc, pWC, idxNew2); + pTerm = &pWC->a[idxTerm]; + if( isComplete ){ + markTermAsChild(pWC, idxNew1, idxTerm); + markTermAsChild(pWC, idxNew2, idxTerm); + } + } +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Add a WO_MATCH auxiliary term to the constraint set if the + ** current expression is of the form: column MATCH expr. + ** This information is used by the xBestIndex methods of + ** virtual tables. The native query optimizer does not attempt + ** to do anything with MATCH functions. + */ + if( isMatchOfColumn(pExpr, &eOp2) ){ + int idxNew; + Expr *pRight, *pLeft; + WhereTerm *pNewTerm; + Bitmask prereqColumn, prereqExpr; + + pRight = pExpr->x.pList->a[0].pExpr; + pLeft = pExpr->x.pList->a[1].pExpr; + prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); + prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); + if( (prereqExpr & prereqColumn)==0 ){ + Expr *pNewExpr; + pNewExpr = sqlite3PExpr(pParse, TK_MATCH, + 0, sqlite3ExprDup(db, pRight, 0), 0); + idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = prereqExpr; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_MATCH; + pNewTerm->eMatchOp = eOp2; + markTermAsChild(pWC, idxNew, idxTerm); + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + } +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + /* When sqlite_stat3 histogram data is available an operator of the + ** form "x IS NOT NULL" can sometimes be evaluated more efficiently + ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a + ** virtual term of that form. + ** + ** Note that the virtual term must be tagged with TERM_VNULL. + */ + if( pExpr->op==TK_NOTNULL + && pExpr->pLeft->op==TK_COLUMN + && pExpr->pLeft->iColumn>=0 + && OptimizationEnabled(db, SQLITE_Stat34) + ){ + Expr *pNewExpr; + Expr *pLeft = pExpr->pLeft; + int idxNew; + WhereTerm *pNewTerm; + + pNewExpr = sqlite3PExpr(pParse, TK_GT, + sqlite3ExprDup(db, pLeft, 0), + sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); + + idxNew = whereClauseInsert(pWC, pNewExpr, + TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); + if( idxNew ){ + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = 0; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_GT; + markTermAsChild(pWC, idxNew, idxTerm); + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + } +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + + /* Prevent ON clause terms of a LEFT JOIN from being used to drive + ** an index for tables to the left of the join. + */ + pTerm->prereqRight |= extraRight; +} + +/*************************************************************************** +** Routines with file scope above. Interface to the rest of the where.c +** subsystem follows. +***************************************************************************/ + +/* +** This routine identifies subexpressions in the WHERE clause where +** each subexpression is separated by the AND operator or some other +** operator specified in the op parameter. The WhereClause structure +** is filled with pointers to subexpressions. For example: +** +** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) +** \________/ \_______________/ \________________/ +** slot[0] slot[1] slot[2] +** +** The original WHERE clause in pExpr is unaltered. All this routine +** does is make slot[] entries point to substructure within pExpr. +** +** In the previous sentence and in the diagram, "slot[]" refers to +** the WhereClause.a[] array. The slot[] array grows as needed to contain +** all terms of the WHERE clause. +*/ +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ + Expr *pE2 = sqlite3ExprSkipCollate(pExpr); + pWC->op = op; + if( pE2==0 ) return; + if( pE2->op!=op ){ + whereClauseInsert(pWC, pExpr, 0); + }else{ + sqlite3WhereSplit(pWC, pE2->pLeft, op); + sqlite3WhereSplit(pWC, pE2->pRight, op); + } +} + +/* +** Initialize a preallocated WhereClause structure. +*/ +SQLITE_PRIVATE void sqlite3WhereClauseInit( + WhereClause *pWC, /* The WhereClause to be initialized */ + WhereInfo *pWInfo /* The WHERE processing context */ +){ + pWC->pWInfo = pWInfo; + pWC->pOuter = 0; + pWC->nTerm = 0; + pWC->nSlot = ArraySize(pWC->aStatic); + pWC->a = pWC->aStatic; +} + +/* +** Deallocate a WhereClause structure. The WhereClause structure +** itself is not freed. This routine is the inverse of +** sqlite3WhereClauseInit(). +*/ +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){ + int i; + WhereTerm *a; + sqlite3 *db = pWC->pWInfo->pParse->db; + for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ + if( a->wtFlags & TERM_DYNAMIC ){ + sqlite3ExprDelete(db, a->pExpr); + } + if( a->wtFlags & TERM_ORINFO ){ + whereOrInfoDelete(db, a->u.pOrInfo); + }else if( a->wtFlags & TERM_ANDINFO ){ + whereAndInfoDelete(db, a->u.pAndInfo); + } + } + if( pWC->a!=pWC->aStatic ){ + sqlite3DbFree(db, pWC->a); + } +} + + +/* +** These routines walk (recursively) an expression tree and generate +** a bitmask indicating which tables are used in that expression +** tree. +*/ +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ + Bitmask mask = 0; + if( p==0 ) return 0; + if( p->op==TK_COLUMN ){ + mask = sqlite3WhereGetMask(pMaskSet, p->iTable); + return mask; + } + mask = sqlite3WhereExprUsage(pMaskSet, p->pRight); + if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft); + if( ExprHasProperty(p, EP_xIsSelect) ){ + mask |= exprSelectUsage(pMaskSet, p->x.pSelect); + }else if( p->x.pList ){ + mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); + } + return mask; +} +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ + int i; + Bitmask mask = 0; + if( pList ){ + for(i=0; inExpr; i++){ + mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr); + } + } + return mask; +} + + +/* +** Call exprAnalyze on all terms in a WHERE clause. +** +** Note that exprAnalyze() might add new virtual terms onto the +** end of the WHERE clause. We do not want to analyze these new +** virtual terms, so start analyzing at the end and work forward +** so that the added virtual terms are never processed. +*/ +SQLITE_PRIVATE void sqlite3WhereExprAnalyze( + SrcList *pTabList, /* the FROM clause */ + WhereClause *pWC /* the WHERE clause to be analyzed */ +){ + int i; + for(i=pWC->nTerm-1; i>=0; i--){ + exprAnalyze(pTabList, pWC, i); + } +} + +/* +** For table-valued-functions, transform the function arguments into +** new WHERE clause terms. +** +** Each function argument translates into an equality constraint against +** a HIDDEN column in the table. +*/ +SQLITE_PRIVATE void sqlite3WhereTabFuncArgs( + Parse *pParse, /* Parsing context */ + struct SrcList_item *pItem, /* The FROM clause term to process */ + WhereClause *pWC /* Xfer function arguments to here */ +){ + Table *pTab; + int j, k; + ExprList *pArgs; + Expr *pColRef; + Expr *pTerm; + if( pItem->fg.isTabFunc==0 ) return; + pTab = pItem->pTab; + assert( pTab!=0 ); + pArgs = pItem->u1.pFuncArg; + if( pArgs==0 ) return; + for(j=k=0; jnExpr; j++){ + while( knCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} + if( k>=pTab->nCol ){ + sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", + pTab->zName, j); + return; + } + pColRef = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); + if( pColRef==0 ) return; + pColRef->iTable = pItem->iCursor; + pColRef->iColumn = k++; + pColRef->pTab = pTab; + pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, + sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); + whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); + } +} + +/************** End of whereexpr.c *******************************************/ +/************** Begin file where.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This module contains C code that generates VDBE code used to process +** the WHERE clause of SQL statements. This module is responsible for +** generating the code that loops through a table looking for applicable +** rows. Indices are selected and used to speed the search when doing +** so is applicable. Because this module is responsible for selecting +** indices, you might also think of this module as the "query optimizer". +*/ +/* #include "sqliteInt.h" */ +/* #include "whereInt.h" */ + +/* Forward declaration of methods */ +static int whereLoopResize(sqlite3*, WhereLoop*, int); + +/* Test variable that can be set to enable WHERE tracing */ +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) +/***/ int sqlite3WhereTrace = 0; +#endif + + +/* +** Return the estimated number of output rows from a WHERE clause +*/ +SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ + return pWInfo->nRowOut; +} + +/* +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this +** WHERE clause returns outputs for DISTINCT processing. +*/ +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ + return pWInfo->eDistinct; +} + +/* +** Return TRUE if the WHERE clause returns rows in ORDER BY order. +** Return FALSE if the output needs to be sorted. +*/ +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ + return pWInfo->nOBSat; +} + +/* +** Return the VDBE address or label to jump to in order to continue +** immediately with the next row of a WHERE clause. +*/ +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ + assert( pWInfo->iContinue!=0 ); + return pWInfo->iContinue; +} + +/* +** Return the VDBE address or label to jump to in order to break +** out of a WHERE loop. +*/ +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ + return pWInfo->iBreak; +} + +/* +** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to +** operate directly on the rowis returned by a WHERE clause. Return +** ONEPASS_SINGLE (1) if the statement can operation directly because only +** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass +** optimization can be used on multiple +** +** If the ONEPASS optimization is used (if this routine returns true) +** then also write the indices of open cursors used by ONEPASS +** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data +** table and iaCur[1] gets the cursor used by an auxiliary index. +** Either value may be -1, indicating that cursor is not used. +** Any cursors returned will have been opened for writing. +** +** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is +** unable to use the ONEPASS optimization. +*/ +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ + memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){ + sqlite3DebugPrintf("%s cursors: %d %d\n", + pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI", + aiCur[0], aiCur[1]); + } +#endif + return pWInfo->eOnePass; +} + +/* +** Move the content of pSrc into pDest +*/ +static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ + pDest->n = pSrc->n; + memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); +} + +/* +** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. +** +** The new entry might overwrite an existing entry, or it might be +** appended, or it might be discarded. Do whatever is the right thing +** so that pSet keeps the N_OR_COST best entries seen so far. +*/ +static int whereOrInsert( + WhereOrSet *pSet, /* The WhereOrSet to be updated */ + Bitmask prereq, /* Prerequisites of the new entry */ + LogEst rRun, /* Run-cost of the new entry */ + LogEst nOut /* Number of outputs for the new entry */ +){ + u16 i; + WhereOrCost *p; + for(i=pSet->n, p=pSet->a; i>0; i--, p++){ + if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ + goto whereOrInsert_done; + } + if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ + return 0; + } + } + if( pSet->na[pSet->n++]; + p->nOut = nOut; + }else{ + p = pSet->a; + for(i=1; in; i++){ + if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; + } + if( p->rRun<=rRun ) return 0; + } +whereOrInsert_done: + p->prereq = prereq; + p->rRun = rRun; + if( p->nOut>nOut ) p->nOut = nOut; + return 1; +} + +/* +** Return the bitmask for the given cursor number. Return 0 if +** iCursor is not in the set. +*/ +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ + int i; + assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); + for(i=0; in; i++){ + if( pMaskSet->ix[i]==iCursor ){ + return MASKBIT(i); + } + } + return 0; +} + +/* +** Create a new mask for cursor iCursor. +** +** There is one cursor per table in the FROM clause. The number of +** tables in the FROM clause is limited by a test early in the +** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] +** array will never overflow. +*/ +static void createMask(WhereMaskSet *pMaskSet, int iCursor){ + assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); + pMaskSet->ix[pMaskSet->n++] = iCursor; +} + +/* +** Advance to the next WhereTerm that matches according to the criteria +** established when the pScan object was initialized by whereScanInit(). +** Return NULL if there are no more matching WhereTerms. +*/ +static WhereTerm *whereScanNext(WhereScan *pScan){ + int iCur; /* The cursor on the LHS of the term */ + i16 iColumn; /* The column on the LHS of the term. -1 for IPK */ + Expr *pX; /* An expression being tested */ + WhereClause *pWC; /* Shorthand for pScan->pWC */ + WhereTerm *pTerm; /* The term being tested */ + int k = pScan->k; /* Where to start scanning */ + + while( pScan->iEquiv<=pScan->nEquiv ){ + iCur = pScan->aiCur[pScan->iEquiv-1]; + iColumn = pScan->aiColumn[pScan->iEquiv-1]; + if( iColumn==XN_EXPR && pScan->pIdxExpr==0 ) return 0; + while( (pWC = pScan->pWC)!=0 ){ + for(pTerm=pWC->a+k; knTerm; k++, pTerm++){ + if( pTerm->leftCursor==iCur + && pTerm->u.leftColumn==iColumn + && (iColumn!=XN_EXPR + || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0) + && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + ){ + if( (pTerm->eOperator & WO_EQUIV)!=0 + && pScan->nEquivaiCur) + && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN + ){ + int j; + for(j=0; jnEquiv; j++){ + if( pScan->aiCur[j]==pX->iTable + && pScan->aiColumn[j]==pX->iColumn ){ + break; + } + } + if( j==pScan->nEquiv ){ + pScan->aiCur[j] = pX->iTable; + pScan->aiColumn[j] = pX->iColumn; + pScan->nEquiv++; + } + } + if( (pTerm->eOperator & pScan->opMask)!=0 ){ + /* Verify the affinity and collating sequence match */ + if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ + CollSeq *pColl; + Parse *pParse = pWC->pWInfo->pParse; + pX = pTerm->pExpr; + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ + continue; + } + assert(pX->pLeft); + pColl = sqlite3BinaryCompareCollSeq(pParse, + pX->pLeft, pX->pRight); + if( pColl==0 ) pColl = pParse->db->pDfltColl; + if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ + continue; + } + } + if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 + && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN + && pX->iTable==pScan->aiCur[0] + && pX->iColumn==pScan->aiColumn[0] + ){ + testcase( pTerm->eOperator & WO_IS ); + continue; + } + pScan->k = k+1; + return pTerm; + } + } + } + pScan->pWC = pScan->pWC->pOuter; + k = 0; + } + pScan->pWC = pScan->pOrigWC; + k = 0; + pScan->iEquiv++; + } + return 0; +} + +/* +** Initialize a WHERE clause scanner object. Return a pointer to the +** first match. Return NULL if there are no matches. +** +** The scanner will be searching the WHERE clause pWC. It will look +** for terms of the form "X " where X is column iColumn of table +** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx +** must be one of the indexes of table iCur. +** +** The must be one of the operators described by opMask. +** +** If the search is for X and the WHERE clause contains terms of the +** form X=Y then this routine might also return terms of the form +** "Y ". The number of levels of transitivity is limited, +** but is enough to handle most commonly occurring SQL statements. +** +** If X is not the INTEGER PRIMARY KEY then X must be compatible with +** index pIdx. +*/ +static WhereTerm *whereScanInit( + WhereScan *pScan, /* The WhereScan object being initialized */ + WhereClause *pWC, /* The WHERE clause to be scanned */ + int iCur, /* Cursor to scan for */ + int iColumn, /* Column to scan for */ + u32 opMask, /* Operator(s) to scan for */ + Index *pIdx /* Must be compatible with this index */ +){ + int j = 0; + + /* memset(pScan, 0, sizeof(*pScan)); */ + pScan->pOrigWC = pWC; + pScan->pWC = pWC; + pScan->pIdxExpr = 0; + if( pIdx ){ + j = iColumn; + iColumn = pIdx->aiColumn[j]; + if( iColumn==XN_EXPR ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; + if( iColumn==pIdx->pTable->iPKey ) iColumn = XN_ROWID; + } + if( pIdx && iColumn>=0 ){ + pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; + pScan->zCollName = pIdx->azColl[j]; + }else{ + pScan->idxaff = 0; + pScan->zCollName = 0; + } + pScan->opMask = opMask; + pScan->k = 0; + pScan->aiCur[0] = iCur; + pScan->aiColumn[0] = iColumn; + pScan->nEquiv = 1; + pScan->iEquiv = 1; + return whereScanNext(pScan); +} + +/* +** Search for a term in the WHERE clause that is of the form "X " +** where X is a reference to the iColumn of table iCur or of index pIdx +** if pIdx!=0 and is one of the WO_xx operator codes specified by +** the op parameter. Return a pointer to the term. Return 0 if not found. +** +** If pIdx!=0 then it must be one of the indexes of table iCur. +** Search for terms matching the iColumn-th column of pIdx +** rather than the iColumn-th column of table iCur. +** +** The term returned might by Y= if there is another constraint in +** the WHERE clause that specifies that X=Y. Any such constraints will be +** identified by the WO_EQUIV bit in the pTerm->eOperator field. The +** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 +** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 +** other equivalent values. Hence a search for X will return if X=A1 +** and A1=A2 and A2=A3 and ... and A9=A10 and A10=. +** +** If there are multiple terms in the WHERE clause of the form "X " +** then try for the one with no dependencies on - in other words where +** is a constant expression of some kind. Only return entries of +** the form "X Y" where Y is a column in another table if no terms of +** the form "X " exist. If no terms with a constant RHS +** exist, try to return a term that does not use WO_EQUIV. +*/ +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( + WhereClause *pWC, /* The WHERE clause to be searched */ + int iCur, /* Cursor number of LHS */ + int iColumn, /* Column number of LHS */ + Bitmask notReady, /* RHS must not overlap with this mask */ + u32 op, /* Mask of WO_xx values describing operator */ + Index *pIdx /* Must be compatible with this index, if not NULL */ +){ + WhereTerm *pResult = 0; + WhereTerm *p; + WhereScan scan; + + p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); + op &= WO_EQ|WO_IS; + while( p ){ + if( (p->prereqRight & notReady)==0 ){ + if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ + testcase( p->eOperator & WO_IS ); + return p; + } + if( pResult==0 ) pResult = p; + } + p = whereScanNext(&scan); + } + return pResult; +} + +/* +** This function searches pList for an entry that matches the iCol-th column +** of index pIdx. +** +** If such an expression is found, its index in pList->a[] is returned. If +** no expression is found, -1 is returned. +*/ +static int findIndexCol( + Parse *pParse, /* Parse context */ + ExprList *pList, /* Expression list to search */ + int iBase, /* Cursor for table associated with pIdx */ + Index *pIdx, /* Index to match column of */ + int iCol /* Column of index to match */ +){ + int i; + const char *zColl = pIdx->azColl[iCol]; + + for(i=0; inExpr; i++){ + Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); + if( p->op==TK_COLUMN + && p->iColumn==pIdx->aiColumn[iCol] + && p->iTable==iBase + ){ + CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); + if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ + return i; + } + } + } + + return -1; +} + +/* +** Return TRUE if the iCol-th column of index pIdx is NOT NULL +*/ +static int indexColumnNotNull(Index *pIdx, int iCol){ + int j; + assert( pIdx!=0 ); + assert( iCol>=0 && iColnColumn ); + j = pIdx->aiColumn[iCol]; + if( j>=0 ){ + return pIdx->pTable->aCol[j].notNull; + }else if( j==(-1) ){ + return 1; + }else{ + assert( j==(-2) ); + return 0; /* Assume an indexed expression can always yield a NULL */ + + } +} + +/* +** Return true if the DISTINCT expression-list passed as the third argument +** is redundant. +** +** A DISTINCT list is redundant if any subset of the columns in the +** DISTINCT list are collectively unique and individually non-null. +*/ +static int isDistinctRedundant( + Parse *pParse, /* Parsing context */ + SrcList *pTabList, /* The FROM clause */ + WhereClause *pWC, /* The WHERE clause */ + ExprList *pDistinct /* The result set that needs to be DISTINCT */ +){ + Table *pTab; + Index *pIdx; + int i; + int iBase; + + /* If there is more than one table or sub-select in the FROM clause of + ** this query, then it will not be possible to show that the DISTINCT + ** clause is redundant. */ + if( pTabList->nSrc!=1 ) return 0; + iBase = pTabList->a[0].iCursor; + pTab = pTabList->a[0].pTab; + + /* If any of the expressions is an IPK column on table iBase, then return + ** true. Note: The (p->iTable==iBase) part of this test may be false if the + ** current SELECT is a correlated sub-query. + */ + for(i=0; inExpr; i++){ + Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); + if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; + } + + /* Loop through all indices on the table, checking each to see if it makes + ** the DISTINCT qualifier redundant. It does so if: + ** + ** 1. The index is itself UNIQUE, and + ** + ** 2. All of the columns in the index are either part of the pDistinct + ** list, or else the WHERE clause contains a term of the form "col=X", + ** where X is a constant value. The collation sequences of the + ** comparison and select-list expressions must match those of the index. + ** + ** 3. All of those index columns for which the WHERE clause does not + ** contain a "col=X" term are subject to a NOT NULL constraint. + */ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( !IsUniqueIndex(pIdx) ) continue; + for(i=0; inKeyCol; i++){ + if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){ + if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break; + if( indexColumnNotNull(pIdx, i)==0 ) break; + } + } + if( i==pIdx->nKeyCol ){ + /* This index implies that the DISTINCT qualifier is redundant. */ + return 1; + } + } + + return 0; +} + + +/* +** Estimate the logarithm of the input value to base 2. +*/ +static LogEst estLog(LogEst N){ + return N<=10 ? 0 : sqlite3LogEst(N) - 33; +} + +/* +** Convert OP_Column opcodes to OP_Copy in previously generated code. +** +** This routine runs over generated VDBE code and translates OP_Column +** opcodes into OP_Copy when the table is being accessed via co-routine +** instead of via table lookup. +** +** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on +** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero, +** then each OP_Rowid is transformed into an instruction to increment the +** value stored in its output register. +*/ +static void translateColumnToCopy( + Vdbe *v, /* The VDBE containing code to translate */ + int iStart, /* Translate from this opcode to the end */ + int iTabCur, /* OP_Column/OP_Rowid references to this table */ + int iRegister, /* The first column is in this register */ + int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */ +){ + VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); + int iEnd = sqlite3VdbeCurrentAddr(v); + for(; iStartp1!=iTabCur ) continue; + if( pOp->opcode==OP_Column ){ + pOp->opcode = OP_Copy; + pOp->p1 = pOp->p2 + iRegister; + pOp->p2 = pOp->p3; + pOp->p3 = 0; + }else if( pOp->opcode==OP_Rowid ){ + if( bIncrRowid ){ + /* Increment the value stored in the P2 operand of the OP_Rowid. */ + pOp->opcode = OP_AddImm; + pOp->p1 = pOp->p2; + pOp->p2 = 1; + }else{ + pOp->opcode = OP_Null; + pOp->p1 = 0; + pOp->p3 = 0; + } + } + } +} + +/* +** Two routines for printing the content of an sqlite3_index_info +** structure. Used for testing and debugging only. If neither +** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines +** are no-ops. +*/ +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) +static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ + int i; + if( !sqlite3WhereTrace ) return; + for(i=0; inConstraint; i++){ + sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", + i, + p->aConstraint[i].iColumn, + p->aConstraint[i].iTermOffset, + p->aConstraint[i].op, + p->aConstraint[i].usable); + } + for(i=0; inOrderBy; i++){ + sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", + i, + p->aOrderBy[i].iColumn, + p->aOrderBy[i].desc); + } +} +static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ + int i; + if( !sqlite3WhereTrace ) return; + for(i=0; inConstraint; i++){ + sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", + i, + p->aConstraintUsage[i].argvIndex, + p->aConstraintUsage[i].omit); + } + sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); + sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); + sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); + sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); + sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); +} +#else +#define TRACE_IDX_INPUTS(A) +#define TRACE_IDX_OUTPUTS(A) +#endif + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** Return TRUE if the WHERE clause term pTerm is of a form where it +** could be used with an index to access pSrc, assuming an appropriate +** index existed. +*/ +static int termCanDriveIndex( + WhereTerm *pTerm, /* WHERE clause term to check */ + struct SrcList_item *pSrc, /* Table we are trying to access */ + Bitmask notReady /* Tables in outer loops of the join */ +){ + char aff; + if( pTerm->leftCursor!=pSrc->iCursor ) return 0; + if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; + if( (pTerm->prereqRight & notReady)!=0 ) return 0; + if( pTerm->u.leftColumn<0 ) return 0; + aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; + if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; + testcase( pTerm->pExpr->op==TK_IS ); + return 1; +} +#endif + + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** Generate code to construct the Index object for an automatic index +** and to set up the WhereLevel object pLevel so that the code generator +** makes use of the automatic index. +*/ +static void constructAutomaticIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ + Bitmask notReady, /* Mask of cursors that are not available */ + WhereLevel *pLevel /* Write new index here */ +){ + int nKeyCol; /* Number of columns in the constructed index */ + WhereTerm *pTerm; /* A single term of the WHERE clause */ + WhereTerm *pWCEnd; /* End of pWC->a[] */ + Index *pIdx; /* Object describing the transient index */ + Vdbe *v; /* Prepared statement under construction */ + int addrInit; /* Address of the initialization bypass jump */ + Table *pTable; /* The table being indexed */ + int addrTop; /* Top of the index fill loop */ + int regRecord; /* Register holding an index record */ + int n; /* Column counter */ + int i; /* Loop counter */ + int mxBitCol; /* Maximum column in pSrc->colUsed */ + CollSeq *pColl; /* Collating sequence to on a column */ + WhereLoop *pLoop; /* The Loop object */ + char *zNotUsed; /* Extra space on the end of pIdx */ + Bitmask idxCols; /* Bitmap of columns used for indexing */ + Bitmask extraCols; /* Bitmap of additional columns */ + u8 sentWarning = 0; /* True if a warnning has been issued */ + Expr *pPartial = 0; /* Partial Index Expression */ + int iContinue = 0; /* Jump here to skip excluded rows */ + struct SrcList_item *pTabItem; /* FROM clause term being indexed */ + int addrCounter = 0; /* Address where integer counter is initialized */ + int regBase; /* Array of registers where record is assembled */ + + /* Generate code to skip over the creation and initialization of the + ** transient index on 2nd and subsequent iterations of the loop. */ + v = pParse->pVdbe; + assert( v!=0 ); + addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); + + /* Count the number of columns that will be added to the index + ** and used to match WHERE clause constraints */ + nKeyCol = 0; + pTable = pSrc->pTab; + pWCEnd = &pWC->a[pWC->nTerm]; + pLoop = pLevel->pWLoop; + idxCols = 0; + for(pTerm=pWC->a; pTermpExpr; + assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ + || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ + || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ + if( pLoop->prereq==0 + && (pTerm->wtFlags & TERM_VIRTUAL)==0 + && !ExprHasProperty(pExpr, EP_FromJoin) + && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ + pPartial = sqlite3ExprAnd(pParse->db, pPartial, + sqlite3ExprDup(pParse->db, pExpr, 0)); + } + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ + int iCol = pTerm->u.leftColumn; + Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); + testcase( iCol==BMS ); + testcase( iCol==BMS-1 ); + if( !sentWarning ){ + sqlite3_log(SQLITE_WARNING_AUTOINDEX, + "automatic index on %s(%s)", pTable->zName, + pTable->aCol[iCol].zName); + sentWarning = 1; + } + if( (idxCols & cMask)==0 ){ + if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ + goto end_auto_index_create; + } + pLoop->aLTerm[nKeyCol++] = pTerm; + idxCols |= cMask; + } + } + } + assert( nKeyCol>0 ); + pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; + pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED + | WHERE_AUTO_INDEX; + + /* Count the number of additional columns needed to create a + ** covering index. A "covering index" is an index that contains all + ** columns that are needed by the query. With a covering index, the + ** original table never needs to be accessed. Automatic indices must + ** be a covering index because the index will not be updated if the + ** original table changes and the index and table cannot both be used + ** if they go out of sync. + */ + extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); + mxBitCol = MIN(BMS-1,pTable->nCol); + testcase( pTable->nCol==BMS-1 ); + testcase( pTable->nCol==BMS-2 ); + for(i=0; icolUsed & MASKBIT(BMS-1) ){ + nKeyCol += pTable->nCol - BMS + 1; + } + + /* Construct the Index object to describe this index */ + pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); + if( pIdx==0 ) goto end_auto_index_create; + pLoop->u.btree.pIndex = pIdx; + pIdx->zName = "auto-index"; + pIdx->pTable = pTable; + n = 0; + idxCols = 0; + for(pTerm=pWC->a; pTermu.leftColumn; + Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); + testcase( iCol==BMS-1 ); + testcase( iCol==BMS ); + if( (idxCols & cMask)==0 ){ + Expr *pX = pTerm->pExpr; + idxCols |= cMask; + pIdx->aiColumn[n] = pTerm->u.leftColumn; + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); + pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY; + n++; + } + } + } + assert( (u32)n==pLoop->u.btree.nEq ); + + /* Add additional columns needed to make the automatic index into + ** a covering index */ + for(i=0; iaiColumn[n] = i; + pIdx->azColl[n] = sqlite3StrBINARY; + n++; + } + } + if( pSrc->colUsed & MASKBIT(BMS-1) ){ + for(i=BMS-1; inCol; i++){ + pIdx->aiColumn[n] = i; + pIdx->azColl[n] = sqlite3StrBINARY; + n++; + } + } + assert( n==nKeyCol ); + pIdx->aiColumn[n] = XN_ROWID; + pIdx->azColl[n] = sqlite3StrBINARY; + + /* Create the automatic index */ + assert( pLevel->iIdxCur>=0 ); + pLevel->iIdxCur = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + VdbeComment((v, "for %s", pTable->zName)); + + /* Fill the automatic index with content */ + sqlite3ExprCachePush(pParse); + pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; + if( pTabItem->fg.viaCoroutine ){ + int regYield = pTabItem->regReturn; + addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0); + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); + VdbeCoverage(v); + VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + }else{ + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); + } + if( pPartial ){ + iContinue = sqlite3VdbeMakeLabel(v); + sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); + pLoop->wsFlags |= WHERE_PARTIALIDX; + } + regRecord = sqlite3GetTempReg(pParse); + regBase = sqlite3GenerateIndexKey( + pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0 + ); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); + if( pTabItem->fg.viaCoroutine ){ + sqlite3VdbeChangeP2(v, addrCounter, regBase+n); + translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult, 1); + sqlite3VdbeGoto(v, addrTop); + pTabItem->fg.viaCoroutine = 0; + }else{ + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); + } + sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); + sqlite3VdbeJumpHere(v, addrTop); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ExprCachePop(pParse); + + /* Jump here when skipping the initialization */ + sqlite3VdbeJumpHere(v, addrInit); + +end_auto_index_create: + sqlite3ExprDelete(pParse->db, pPartial); +} +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Allocate and populate an sqlite3_index_info structure. It is the +** responsibility of the caller to eventually release the structure +** by passing the pointer returned by this function to sqlite3_free(). +*/ +static sqlite3_index_info *allocateIndexInfo( + Parse *pParse, + WhereClause *pWC, + Bitmask mUnusable, /* Ignore terms with these prereqs */ + struct SrcList_item *pSrc, + ExprList *pOrderBy +){ + int i, j; + int nTerm; + struct sqlite3_index_constraint *pIdxCons; + struct sqlite3_index_orderby *pIdxOrderBy; + struct sqlite3_index_constraint_usage *pUsage; + WhereTerm *pTerm; + int nOrderBy; + sqlite3_index_info *pIdxInfo; + + /* Count the number of possible WHERE clause constraints referring + ** to this virtual table */ + for(i=nTerm=0, pTerm=pWC->a; inTerm; i++, pTerm++){ + if( pTerm->leftCursor != pSrc->iCursor ) continue; + if( pTerm->prereqRight & mUnusable ) continue; + assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); + testcase( pTerm->eOperator & WO_IN ); + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_IS ); + testcase( pTerm->eOperator & WO_ALL ); + if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; + if( pTerm->wtFlags & TERM_VNULL ) continue; + assert( pTerm->u.leftColumn>=(-1) ); + nTerm++; + } + + /* If the ORDER BY clause contains only columns in the current + ** virtual table then allocate space for the aOrderBy part of + ** the sqlite3_index_info structure. + */ + nOrderBy = 0; + if( pOrderBy ){ + int n = pOrderBy->nExpr; + for(i=0; ia[i].pExpr; + if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; + } + if( i==n){ + nOrderBy = n; + } + } + + /* Allocate the sqlite3_index_info structure + */ + pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) + + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm + + sizeof(*pIdxOrderBy)*nOrderBy ); + if( pIdxInfo==0 ){ + sqlite3ErrorMsg(pParse, "out of memory"); + return 0; + } + + /* Initialize the structure. The sqlite3_index_info structure contains + ** many fields that are declared "const" to prevent xBestIndex from + ** changing them. We have to do some funky casting in order to + ** initialize those fields. + */ + pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; + pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; + pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; + *(int*)&pIdxInfo->nConstraint = nTerm; + *(int*)&pIdxInfo->nOrderBy = nOrderBy; + *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; + *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; + *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = + pUsage; + + for(i=j=0, pTerm=pWC->a; inTerm; i++, pTerm++){ + u8 op; + if( pTerm->leftCursor != pSrc->iCursor ) continue; + if( pTerm->prereqRight & mUnusable ) continue; + assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); + testcase( pTerm->eOperator & WO_IN ); + testcase( pTerm->eOperator & WO_IS ); + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_ALL ); + if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; + if( pTerm->wtFlags & TERM_VNULL ) continue; + assert( pTerm->u.leftColumn>=(-1) ); + pIdxCons[j].iColumn = pTerm->u.leftColumn; + pIdxCons[j].iTermOffset = i; + op = (u8)pTerm->eOperator & WO_ALL; + if( op==WO_IN ) op = WO_EQ; + if( op==WO_MATCH ){ + op = pTerm->eMatchOp; + } + pIdxCons[j].op = op; + /* The direct assignment in the previous line is possible only because + ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The + ** following asserts verify this fact. */ + assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); + assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); + assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); + assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); + assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); + assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); + assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); + j++; + } + for(i=0; ia[i].pExpr; + pIdxOrderBy[i].iColumn = pExpr->iColumn; + pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; + } + + return pIdxInfo; +} + +/* +** The table object reference passed as the second argument to this function +** must represent a virtual table. This function invokes the xBestIndex() +** method of the virtual table with the sqlite3_index_info object that +** comes in as the 3rd argument to this function. +** +** If an error occurs, pParse is populated with an error message and a +** non-zero value is returned. Otherwise, 0 is returned and the output +** part of the sqlite3_index_info structure is left populated. +** +** Whether or not an error is returned, it is the responsibility of the +** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates +** that this is required. +*/ +static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ + sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; + int rc; + + TRACE_IDX_INPUTS(p); + rc = pVtab->pModule->xBestIndex(pVtab, p); + TRACE_IDX_OUTPUTS(p); + + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ){ + sqlite3OomFault(pParse->db); + }else if( !pVtab->zErrMsg ){ + sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); + }else{ + sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); + } + } + sqlite3_free(pVtab->zErrMsg); + pVtab->zErrMsg = 0; + +#if 0 + /* This error is now caught by the caller. + ** Search for "xBestIndex malfunction" below */ + for(i=0; inConstraint; i++){ + if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ + sqlite3ErrorMsg(pParse, + "table %s: xBestIndex returned an invalid plan", pTab->zName); + } + } +#endif + + return pParse->nErr; +} +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Estimate the location of a particular key among all keys in an +** index. Store the results in aStat as follows: +** +** aStat[0] Est. number of rows less than pRec +** aStat[1] Est. number of rows equal to pRec +** +** Return the index of the sample that is the smallest sample that +** is greater than or equal to pRec. Note that this index is not an index +** into the aSample[] array - it is an index into a virtual set of samples +** based on the contents of aSample[] and the number of fields in record +** pRec. +*/ +static int whereKeyStats( + Parse *pParse, /* Database connection */ + Index *pIdx, /* Index to consider domain of */ + UnpackedRecord *pRec, /* Vector of values to consider */ + int roundUp, /* Round up if true. Round down if false */ + tRowcnt *aStat /* OUT: stats written here */ +){ + IndexSample *aSample = pIdx->aSample; + int iCol; /* Index of required stats in anEq[] etc. */ + int i; /* Index of first sample >= pRec */ + int iSample; /* Smallest sample larger than or equal to pRec */ + int iMin = 0; /* Smallest sample not yet tested */ + int iTest; /* Next sample to test */ + int res; /* Result of comparison operation */ + int nField; /* Number of fields in pRec */ + tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ + +#ifndef SQLITE_DEBUG + UNUSED_PARAMETER( pParse ); +#endif + assert( pRec!=0 ); + assert( pIdx->nSample>0 ); + assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); + + /* Do a binary search to find the first sample greater than or equal + ** to pRec. If pRec contains a single field, the set of samples to search + ** is simply the aSample[] array. If the samples in aSample[] contain more + ** than one fields, all fields following the first are ignored. + ** + ** If pRec contains N fields, where N is more than one, then as well as the + ** samples in aSample[] (truncated to N fields), the search also has to + ** consider prefixes of those samples. For example, if the set of samples + ** in aSample is: + ** + ** aSample[0] = (a, 5) + ** aSample[1] = (a, 10) + ** aSample[2] = (b, 5) + ** aSample[3] = (c, 100) + ** aSample[4] = (c, 105) + ** + ** Then the search space should ideally be the samples above and the + ** unique prefixes [a], [b] and [c]. But since that is hard to organize, + ** the code actually searches this set: + ** + ** 0: (a) + ** 1: (a, 5) + ** 2: (a, 10) + ** 3: (a, 10) + ** 4: (b) + ** 5: (b, 5) + ** 6: (c) + ** 7: (c, 100) + ** 8: (c, 105) + ** 9: (c, 105) + ** + ** For each sample in the aSample[] array, N samples are present in the + ** effective sample array. In the above, samples 0 and 1 are based on + ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. + ** + ** Often, sample i of each block of N effective samples has (i+1) fields. + ** Except, each sample may be extended to ensure that it is greater than or + ** equal to the previous sample in the array. For example, in the above, + ** sample 2 is the first sample of a block of N samples, so at first it + ** appears that it should be 1 field in size. However, that would make it + ** smaller than sample 1, so the binary search would not work. As a result, + ** it is extended to two fields. The duplicates that this creates do not + ** cause any problems. + */ + nField = pRec->nField; + iCol = 0; + iSample = pIdx->nSample * nField; + do{ + int iSamp; /* Index in aSample[] of test sample */ + int n; /* Number of fields in test sample */ + + iTest = (iMin+iSample)/2; + iSamp = iTest / nField; + if( iSamp>0 ){ + /* The proposed effective sample is a prefix of sample aSample[iSamp]. + ** Specifically, the shortest prefix of at least (1 + iTest%nField) + ** fields that is greater than the previous effective sample. */ + for(n=(iTest % nField) + 1; nnField = n; + res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); + if( res<0 ){ + iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; + iMin = iTest+1; + }else if( res==0 && ndb->mallocFailed==0 ){ + if( res==0 ){ + /* If (res==0) is true, then pRec must be equal to sample i. */ + assert( inSample ); + assert( iCol==nField-1 ); + pRec->nField = nField; + assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) + || pParse->db->mallocFailed + ); + }else{ + /* Unless i==pIdx->nSample, indicating that pRec is larger than + ** all samples in the aSample[] array, pRec must be smaller than the + ** (iCol+1) field prefix of sample i. */ + assert( i<=pIdx->nSample && i>=0 ); + pRec->nField = iCol+1; + assert( i==pIdx->nSample + || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 + || pParse->db->mallocFailed ); + + /* if i==0 and iCol==0, then record pRec is smaller than all samples + ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must + ** be greater than or equal to the (iCol) field prefix of sample i. + ** If (i>0), then pRec must also be greater than sample (i-1). */ + if( iCol>0 ){ + pRec->nField = iCol; + assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 + || pParse->db->mallocFailed ); + } + if( i>0 ){ + pRec->nField = nField; + assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 + || pParse->db->mallocFailed ); + } + } + } +#endif /* ifdef SQLITE_DEBUG */ + + if( res==0 ){ + /* Record pRec is equal to sample i */ + assert( iCol==nField-1 ); + aStat[0] = aSample[i].anLt[iCol]; + aStat[1] = aSample[i].anEq[iCol]; + }else{ + /* At this point, the (iCol+1) field prefix of aSample[i] is the first + ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec + ** is larger than all samples in the array. */ + tRowcnt iUpper, iGap; + if( i>=pIdx->nSample ){ + iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); + }else{ + iUpper = aSample[i].anLt[iCol]; + } + + if( iLower>=iUpper ){ + iGap = 0; + }else{ + iGap = iUpper - iLower; + } + if( roundUp ){ + iGap = (iGap*2)/3; + }else{ + iGap = iGap/3; + } + aStat[0] = iLower + iGap; + aStat[1] = pIdx->aAvgEq[iCol]; + } + + /* Restore the pRec->nField value before returning. */ + pRec->nField = nField; + return i; +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +/* +** If it is not NULL, pTerm is a term that provides an upper or lower +** bound on a range scan. Without considering pTerm, it is estimated +** that the scan will visit nNew rows. This function returns the number +** estimated to be visited after taking pTerm into account. +** +** If the user explicitly specified a likelihood() value for this term, +** then the return value is the likelihood multiplied by the number of +** input rows. Otherwise, this function assumes that an "IS NOT NULL" term +** has a likelihood of 0.50, and any other term a likelihood of 0.25. +*/ +static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ + LogEst nRet = nNew; + if( pTerm ){ + if( pTerm->truthProb<=0 ){ + nRet += pTerm->truthProb; + }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ + nRet -= 20; assert( 20==sqlite3LogEst(4) ); + } + } + return nRet; +} + + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Return the affinity for a single column of an index. +*/ +static char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ + assert( iCol>=0 && iColnColumn ); + if( !pIdx->zColAff ){ + if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; + } + return pIdx->zColAff[iCol]; +} +#endif + + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** This function is called to estimate the number of rows visited by a +** range-scan on a skip-scan index. For example: +** +** CREATE INDEX i1 ON t1(a, b, c); +** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; +** +** Value pLoop->nOut is currently set to the estimated number of rows +** visited for scanning (a=? AND b=?). This function reduces that estimate +** by some factor to account for the (c BETWEEN ? AND ?) expression based +** on the stat4 data for the index. this scan will be peformed multiple +** times (once for each (a,b) combination that matches a=?) is dealt with +** by the caller. +** +** It does this by scanning through all stat4 samples, comparing values +** extracted from pLower and pUpper with the corresponding column in each +** sample. If L and U are the number of samples found to be less than or +** equal to the values extracted from pLower and pUpper respectively, and +** N is the total number of samples, the pLoop->nOut value is adjusted +** as follows: +** +** nOut = nOut * ( min(U - L, 1) / N ) +** +** If pLower is NULL, or a value cannot be extracted from the term, L is +** set to zero. If pUpper is NULL, or a value cannot be extracted from it, +** U is set to N. +** +** Normally, this function sets *pbDone to 1 before returning. However, +** if no value can be extracted from either pLower or pUpper (and so the +** estimate of the number of rows delivered remains unchanged), *pbDone +** is left as is. +** +** If an error occurs, an SQLite error code is returned. Otherwise, +** SQLITE_OK. +*/ +static int whereRangeSkipScanEst( + Parse *pParse, /* Parsing & code generating context */ + WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ + WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ + WhereLoop *pLoop, /* Update the .nOut value of this loop */ + int *pbDone /* Set to true if at least one expr. value extracted */ +){ + Index *p = pLoop->u.btree.pIndex; + int nEq = pLoop->u.btree.nEq; + sqlite3 *db = pParse->db; + int nLower = -1; + int nUpper = p->nSample+1; + int rc = SQLITE_OK; + u8 aff = sqlite3IndexColumnAffinity(db, p, nEq); + CollSeq *pColl; + + sqlite3_value *p1 = 0; /* Value extracted from pLower */ + sqlite3_value *p2 = 0; /* Value extracted from pUpper */ + sqlite3_value *pVal = 0; /* Value extracted from record */ + + pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); + if( pLower ){ + rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); + nLower = 0; + } + if( pUpper && rc==SQLITE_OK ){ + rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); + nUpper = p2 ? 0 : p->nSample; + } + + if( p1 || p2 ){ + int i; + int nDiff; + for(i=0; rc==SQLITE_OK && inSample; i++){ + rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); + if( rc==SQLITE_OK && p1 ){ + int res = sqlite3MemCompare(p1, pVal, pColl); + if( res>=0 ) nLower++; + } + if( rc==SQLITE_OK && p2 ){ + int res = sqlite3MemCompare(p2, pVal, pColl); + if( res>=0 ) nUpper++; + } + } + nDiff = (nUpper - nLower); + if( nDiff<=0 ) nDiff = 1; + + /* If there is both an upper and lower bound specified, and the + ** comparisons indicate that they are close together, use the fallback + ** method (assume that the scan visits 1/64 of the rows) for estimating + ** the number of rows visited. Otherwise, estimate the number of rows + ** using the method described in the header comment for this function. */ + if( nDiff!=1 || pUpper==0 || pLower==0 ){ + int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); + pLoop->nOut -= nAdjust; + *pbDone = 1; + WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", + nLower, nUpper, nAdjust*-1, pLoop->nOut)); + } + + }else{ + assert( *pbDone==0 ); + } + + sqlite3ValueFree(p1); + sqlite3ValueFree(p2); + sqlite3ValueFree(pVal); + + return rc; +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +/* +** This function is used to estimate the number of rows that will be visited +** by scanning an index for a range of values. The range may have an upper +** bound, a lower bound, or both. The WHERE clause terms that set the upper +** and lower bounds are represented by pLower and pUpper respectively. For +** example, assuming that index p is on t1(a): +** +** ... FROM t1 WHERE a > ? AND a < ? ... +** |_____| |_____| +** | | +** pLower pUpper +** +** If either of the upper or lower bound is not present, then NULL is passed in +** place of the corresponding WhereTerm. +** +** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index +** column subject to the range constraint. Or, equivalently, the number of +** equality constraints optimized by the proposed index scan. For example, +** assuming index p is on t1(a, b), and the SQL query is: +** +** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... +** +** then nEq is set to 1 (as the range restricted column, b, is the second +** left-most column of the index). Or, if the query is: +** +** ... FROM t1 WHERE a > ? AND a < ? ... +** +** then nEq is set to 0. +** +** When this function is called, *pnOut is set to the sqlite3LogEst() of the +** number of rows that the index scan is expected to visit without +** considering the range constraints. If nEq is 0, then *pnOut is the number of +** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) +** to account for the range constraints pLower and pUpper. +** +** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be +** used, a single range inequality reduces the search space by a factor of 4. +** and a pair of constraints (x>? AND x123" Might be NULL */ + WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ + WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ +){ + int rc = SQLITE_OK; + int nOut = pLoop->nOut; + LogEst nNew; + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + Index *p = pLoop->u.btree.pIndex; + int nEq = pLoop->u.btree.nEq; + + if( p->nSample>0 && nEqnSampleCol ){ + if( nEq==pBuilder->nRecValid ){ + UnpackedRecord *pRec = pBuilder->pRec; + tRowcnt a[2]; + u8 aff; + + /* Variable iLower will be set to the estimate of the number of rows in + ** the index that are less than the lower bound of the range query. The + ** lower bound being the concatenation of $P and $L, where $P is the + ** key-prefix formed by the nEq values matched against the nEq left-most + ** columns of the index, and $L is the value in pLower. + ** + ** Or, if pLower is NULL or $L cannot be extracted from it (because it + ** is not a simple variable or literal value), the lower bound of the + ** range is $P. Due to a quirk in the way whereKeyStats() works, even + ** if $L is available, whereKeyStats() is called for both ($P) and + ** ($P:$L) and the larger of the two returned values is used. + ** + ** Similarly, iUpper is to be set to the estimate of the number of rows + ** less than the upper bound of the range query. Where the upper bound + ** is either ($P) or ($P:$U). Again, even if $U is available, both values + ** of iUpper are requested of whereKeyStats() and the smaller used. + ** + ** The number of rows between the two bounds is then just iUpper-iLower. + */ + tRowcnt iLower; /* Rows less than the lower bound */ + tRowcnt iUpper; /* Rows less than the upper bound */ + int iLwrIdx = -2; /* aSample[] for the lower bound */ + int iUprIdx = -1; /* aSample[] for the upper bound */ + + if( pRec ){ + testcase( pRec->nField!=pBuilder->nRecValid ); + pRec->nField = pBuilder->nRecValid; + } + aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq); + assert( nEq!=p->nKeyCol || aff==SQLITE_AFF_INTEGER ); + /* Determine iLower and iUpper using ($P) only. */ + if( nEq==0 ){ + iLower = 0; + iUpper = p->nRowEst0; + }else{ + /* Note: this call could be optimized away - since the same values must + ** have been requested when testing key $P in whereEqualScanEst(). */ + whereKeyStats(pParse, p, pRec, 0, a); + iLower = a[0]; + iUpper = a[0] + a[1]; + } + + assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); + assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); + assert( p->aSortOrder!=0 ); + if( p->aSortOrder[nEq] ){ + /* The roles of pLower and pUpper are swapped for a DESC index */ + SWAP(WhereTerm*, pLower, pUpper); + } + + /* If possible, improve on the iLower estimate using ($P:$L). */ + if( pLower ){ + int bOk; /* True if value is extracted from pExpr */ + Expr *pExpr = pLower->pExpr->pRight; + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); + if( rc==SQLITE_OK && bOk ){ + tRowcnt iNew; + iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); + iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); + if( iNew>iLower ) iLower = iNew; + nOut--; + pLower = 0; + } + } + + /* If possible, improve on the iUpper estimate using ($P:$U). */ + if( pUpper ){ + int bOk; /* True if value is extracted from pExpr */ + Expr *pExpr = pUpper->pExpr->pRight; + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); + if( rc==SQLITE_OK && bOk ){ + tRowcnt iNew; + iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); + iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); + if( iNewpRec = pRec; + if( rc==SQLITE_OK ){ + if( iUpper>iLower ){ + nNew = sqlite3LogEst(iUpper - iLower); + /* TUNING: If both iUpper and iLower are derived from the same + ** sample, then assume they are 4x more selective. This brings + ** the estimated selectivity more in line with what it would be + ** if estimated without the use of STAT3/4 tables. */ + if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); + }else{ + nNew = 10; assert( 10==sqlite3LogEst(2) ); + } + if( nNewwtFlags & TERM_VNULL)==0 ); + nNew = whereRangeAdjust(pLower, nOut); + nNew = whereRangeAdjust(pUpper, nNew); + + /* TUNING: If there is both an upper and lower limit and neither limit + ** has an application-defined likelihood(), assume the range is + ** reduced by an additional 75%. This means that, by default, an open-ended + ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the + ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to + ** match 1/64 of the index. */ + if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ + nNew -= 20; + } + + nOut -= (pLower!=0) + (pUpper!=0); + if( nNew<10 ) nNew = 10; + if( nNewnOut>nOut ){ + WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", + pLoop->nOut, nOut)); + } +#endif + pLoop->nOut = (LogEst)nOut; + return rc; +} + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Estimate the number of rows that will be returned based on +** an equality constraint x=VALUE and where that VALUE occurs in +** the histogram data. This only works when x is the left-most +** column of an index and sqlite_stat3 histogram data is available +** for that index. When pExpr==NULL that means the constraint is +** "x IS NULL" instead of "x=VALUE". +** +** Write the estimated row count into *pnRow and return SQLITE_OK. +** If unable to make an estimate, leave *pnRow unchanged and return +** non-zero. +** +** This routine can fail if it is unable to load a collating sequence +** required for string comparison, or if unable to allocate memory +** for a UTF conversion required for comparison. The error is stored +** in the pParse structure. +*/ +static int whereEqualScanEst( + Parse *pParse, /* Parsing & code generating context */ + WhereLoopBuilder *pBuilder, + Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ + tRowcnt *pnRow /* Write the revised row estimate here */ +){ + Index *p = pBuilder->pNew->u.btree.pIndex; + int nEq = pBuilder->pNew->u.btree.nEq; + UnpackedRecord *pRec = pBuilder->pRec; + u8 aff; /* Column affinity */ + int rc; /* Subfunction return code */ + tRowcnt a[2]; /* Statistics */ + int bOk; + + assert( nEq>=1 ); + assert( nEq<=p->nColumn ); + assert( p->aSample!=0 ); + assert( p->nSample>0 ); + assert( pBuilder->nRecValidnRecValid<(nEq-1) ){ + return SQLITE_NOTFOUND; + } + + /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() + ** below would return the same value. */ + if( nEq>=p->nColumn ){ + *pnRow = 1; + return SQLITE_OK; + } + + aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq-1); + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); + pBuilder->pRec = pRec; + if( rc!=SQLITE_OK ) return rc; + if( bOk==0 ) return SQLITE_NOTFOUND; + pBuilder->nRecValid = nEq; + + whereKeyStats(pParse, p, pRec, 0, a); + WHERETRACE(0x10,("equality scan regions %s(%d): %d\n", + p->zName, nEq-1, (int)a[1])); + *pnRow = a[1]; + + return rc; +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +/* +** Estimate the number of rows that will be returned based on +** an IN constraint where the right-hand side of the IN operator +** is a list of values. Example: +** +** WHERE x IN (1,2,3,4) +** +** Write the estimated row count into *pnRow and return SQLITE_OK. +** If unable to make an estimate, leave *pnRow unchanged and return +** non-zero. +** +** This routine can fail if it is unable to load a collating sequence +** required for string comparison, or if unable to allocate memory +** for a UTF conversion required for comparison. The error is stored +** in the pParse structure. +*/ +static int whereInScanEst( + Parse *pParse, /* Parsing & code generating context */ + WhereLoopBuilder *pBuilder, + ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ + tRowcnt *pnRow /* Write the revised row estimate here */ +){ + Index *p = pBuilder->pNew->u.btree.pIndex; + i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); + int nRecValid = pBuilder->nRecValid; + int rc = SQLITE_OK; /* Subfunction return code */ + tRowcnt nEst; /* Number of rows for a single term */ + tRowcnt nRowEst = 0; /* New estimate of the number of rows */ + int i; /* Loop counter */ + + assert( p->aSample!=0 ); + for(i=0; rc==SQLITE_OK && inExpr; i++){ + nEst = nRow0; + rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); + nRowEst += nEst; + pBuilder->nRecValid = nRecValid; + } + + if( rc==SQLITE_OK ){ + if( nRowEst > nRow0 ) nRowEst = nRow0; + *pnRow = nRowEst; + WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); + } + assert( pBuilder->nRecValid==nRecValid ); + return rc; +} +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + + +#ifdef WHERETRACE_ENABLED +/* +** Print the content of a WhereTerm object +*/ +static void whereTermPrint(WhereTerm *pTerm, int iTerm){ + if( pTerm==0 ){ + sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); + }else{ + char zType[4]; + memcpy(zType, "...", 4); + if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; + if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; + if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; + sqlite3DebugPrintf( + "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n", + iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, + pTerm->eOperator, pTerm->wtFlags); + sqlite3TreeViewExpr(0, pTerm->pExpr, 0); + } +} +#endif + +#ifdef WHERETRACE_ENABLED +/* +** Print a WhereLoop object for debugging purposes +*/ +static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ + WhereInfo *pWInfo = pWC->pWInfo; + int nb = 1+(pWInfo->pTabList->nSrc+3)/4; + struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; + Table *pTab = pItem->pTab; + Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; + sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, + p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); + sqlite3DebugPrintf(" %12s", + pItem->zAlias ? pItem->zAlias : pTab->zName); + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ + const char *zName; + if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ + if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ + int i = sqlite3Strlen30(zName) - 1; + while( zName[i]!='_' ) i--; + zName += i; + } + sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); + }else{ + sqlite3DebugPrintf("%20s",""); + } + }else{ + char *z; + if( p->u.vtab.idxStr ){ + z = sqlite3_mprintf("(%d,\"%s\",%x)", + p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); + }else{ + z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); + } + sqlite3DebugPrintf(" %-19s", z); + sqlite3_free(z); + } + if( p->wsFlags & WHERE_SKIPSCAN ){ + sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); + }else{ + sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); + } + sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); + if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ + int i; + for(i=0; inLTerm; i++){ + whereTermPrint(p->aLTerm[i], i); + } + } +} +#endif + +/* +** Convert bulk memory into a valid WhereLoop that can be passed +** to whereLoopClear harmlessly. +*/ +static void whereLoopInit(WhereLoop *p){ + p->aLTerm = p->aLTermSpace; + p->nLTerm = 0; + p->nLSlot = ArraySize(p->aLTermSpace); + p->wsFlags = 0; +} + +/* +** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. +*/ +static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ + if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ + if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ + sqlite3_free(p->u.vtab.idxStr); + p->u.vtab.needFree = 0; + p->u.vtab.idxStr = 0; + }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ + sqlite3DbFree(db, p->u.btree.pIndex->zColAff); + sqlite3DbFree(db, p->u.btree.pIndex); + p->u.btree.pIndex = 0; + } + } +} + +/* +** Deallocate internal memory used by a WhereLoop object +*/ +static void whereLoopClear(sqlite3 *db, WhereLoop *p){ + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); + whereLoopClearUnion(db, p); + whereLoopInit(p); +} + +/* +** Increase the memory allocation for pLoop->aLTerm[] to be at least n. +*/ +static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ + WhereTerm **paNew; + if( p->nLSlot>=n ) return SQLITE_OK; + n = (n+7)&~7; + paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n); + if( paNew==0 ) return SQLITE_NOMEM_BKPT; + memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); + p->aLTerm = paNew; + p->nLSlot = n; + return SQLITE_OK; +} + +/* +** Transfer content from the second pLoop into the first. +*/ +static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ + whereLoopClearUnion(db, pTo); + if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ + memset(&pTo->u, 0, sizeof(pTo->u)); + return SQLITE_NOMEM_BKPT; + } + memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); + memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); + if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ + pFrom->u.vtab.needFree = 0; + }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ + pFrom->u.btree.pIndex = 0; + } + return SQLITE_OK; +} + +/* +** Delete a WhereLoop object +*/ +static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ + whereLoopClear(db, p); + sqlite3DbFree(db, p); +} + +/* +** Free a WhereInfo structure +*/ +static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ + if( ALWAYS(pWInfo) ){ + int i; + for(i=0; inLevel; i++){ + WhereLevel *pLevel = &pWInfo->a[i]; + if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ + sqlite3DbFree(db, pLevel->u.in.aInLoop); + } + } + sqlite3WhereClauseClear(&pWInfo->sWC); + while( pWInfo->pLoops ){ + WhereLoop *p = pWInfo->pLoops; + pWInfo->pLoops = p->pNextLoop; + whereLoopDelete(db, p); + } + sqlite3DbFree(db, pWInfo); + } +} + +/* +** Return TRUE if all of the following are true: +** +** (1) X has the same or lower cost that Y +** (2) X is a proper subset of Y +** (3) X skips at least as many columns as Y +** +** By "proper subset" we mean that X uses fewer WHERE clause terms +** than Y and that every WHERE clause term used by X is also used +** by Y. +** +** If X is a proper subset of Y then Y is a better choice and ought +** to have a lower cost. This routine returns TRUE when that cost +** relationship is inverted and needs to be adjusted. The third rule +** was added because if X uses skip-scan less than Y it still might +** deserve a lower cost even if it is a proper subset of Y. +*/ +static int whereLoopCheaperProperSubset( + const WhereLoop *pX, /* First WhereLoop to compare */ + const WhereLoop *pY /* Compare against this WhereLoop */ +){ + int i, j; + if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ + return 0; /* X is not a subset of Y */ + } + if( pY->nSkip > pX->nSkip ) return 0; + if( pX->rRun >= pY->rRun ){ + if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ + if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ + } + for(i=pX->nLTerm-1; i>=0; i--){ + if( pX->aLTerm[i]==0 ) continue; + for(j=pY->nLTerm-1; j>=0; j--){ + if( pY->aLTerm[j]==pX->aLTerm[i] ) break; + } + if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ + } + return 1; /* All conditions meet */ +} + +/* +** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so +** that: +** +** (1) pTemplate costs less than any other WhereLoops that are a proper +** subset of pTemplate +** +** (2) pTemplate costs more than any other WhereLoops for which pTemplate +** is a proper subset. +** +** To say "WhereLoop X is a proper subset of Y" means that X uses fewer +** WHERE clause terms than Y and that every WHERE clause term used by X is +** also used by Y. +*/ +static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ + if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; + for(; p; p=p->pNextLoop){ + if( p->iTab!=pTemplate->iTab ) continue; + if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; + if( whereLoopCheaperProperSubset(p, pTemplate) ){ + /* Adjust pTemplate cost downward so that it is cheaper than its + ** subset p. */ + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); + pTemplate->rRun = p->rRun; + pTemplate->nOut = p->nOut - 1; + }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ + /* Adjust pTemplate cost upward so that it is costlier than p since + ** pTemplate is a proper subset of p */ + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); + pTemplate->rRun = p->rRun; + pTemplate->nOut = p->nOut + 1; + } + } +} + +/* +** Search the list of WhereLoops in *ppPrev looking for one that can be +** supplanted by pTemplate. +** +** Return NULL if the WhereLoop list contains an entry that can supplant +** pTemplate, in other words if pTemplate does not belong on the list. +** +** If pX is a WhereLoop that pTemplate can supplant, then return the +** link that points to pX. +** +** If pTemplate cannot supplant any existing element of the list but needs +** to be added to the list, then return a pointer to the tail of the list. +*/ +static WhereLoop **whereLoopFindLesser( + WhereLoop **ppPrev, + const WhereLoop *pTemplate +){ + WhereLoop *p; + for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ + if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ + /* If either the iTab or iSortIdx values for two WhereLoop are different + ** then those WhereLoops need to be considered separately. Neither is + ** a candidate to replace the other. */ + continue; + } + /* In the current implementation, the rSetup value is either zero + ** or the cost of building an automatic index (NlogN) and the NlogN + ** is the same for compatible WhereLoops. */ + assert( p->rSetup==0 || pTemplate->rSetup==0 + || p->rSetup==pTemplate->rSetup ); + + /* whereLoopAddBtree() always generates and inserts the automatic index + ** case first. Hence compatible candidate WhereLoops never have a larger + ** rSetup. Call this SETUP-INVARIANT */ + assert( p->rSetup>=pTemplate->rSetup ); + + /* Any loop using an appliation-defined index (or PRIMARY KEY or + ** UNIQUE constraint) with one or more == constraints is better + ** than an automatic index. Unless it is a skip-scan. */ + if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 + && (pTemplate->nSkip)==0 + && (pTemplate->wsFlags & WHERE_INDEXED)!=0 + && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 + && (p->prereq & pTemplate->prereq)==pTemplate->prereq + ){ + break; + } + + /* If existing WhereLoop p is better than pTemplate, pTemplate can be + ** discarded. WhereLoop p is better if: + ** (1) p has no more dependencies than pTemplate, and + ** (2) p has an equal or lower cost than pTemplate + */ + if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ + && p->rSetup<=pTemplate->rSetup /* (2a) */ + && p->rRun<=pTemplate->rRun /* (2b) */ + && p->nOut<=pTemplate->nOut /* (2c) */ + ){ + return 0; /* Discard pTemplate */ + } + + /* If pTemplate is always better than p, then cause p to be overwritten + ** with pTemplate. pTemplate is better than p if: + ** (1) pTemplate has no more dependences than p, and + ** (2) pTemplate has an equal or lower cost than p. + */ + if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ + && p->rRun>=pTemplate->rRun /* (2a) */ + && p->nOut>=pTemplate->nOut /* (2b) */ + ){ + assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ + break; /* Cause p to be overwritten by pTemplate */ + } + } + return ppPrev; +} + +/* +** Insert or replace a WhereLoop entry using the template supplied. +** +** An existing WhereLoop entry might be overwritten if the new template +** is better and has fewer dependencies. Or the template will be ignored +** and no insert will occur if an existing WhereLoop is faster and has +** fewer dependencies than the template. Otherwise a new WhereLoop is +** added based on the template. +** +** If pBuilder->pOrSet is not NULL then we care about only the +** prerequisites and rRun and nOut costs of the N best loops. That +** information is gathered in the pBuilder->pOrSet object. This special +** processing mode is used only for OR clause processing. +** +** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we +** still might overwrite similar loops with the new template if the +** new template is better. Loops may be overwritten if the following +** conditions are met: +** +** (1) They have the same iTab. +** (2) They have the same iSortIdx. +** (3) The template has same or fewer dependencies than the current loop +** (4) The template has the same or lower cost than the current loop +*/ +static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ + WhereLoop **ppPrev, *p; + WhereInfo *pWInfo = pBuilder->pWInfo; + sqlite3 *db = pWInfo->pParse->db; + int rc; + + /* If pBuilder->pOrSet is defined, then only keep track of the costs + ** and prereqs. + */ + if( pBuilder->pOrSet!=0 ){ + if( pTemplate->nLTerm ){ +#if WHERETRACE_ENABLED + u16 n = pBuilder->pOrSet->n; + int x = +#endif + whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, + pTemplate->nOut); +#if WHERETRACE_ENABLED /* 0x8 */ + if( sqlite3WhereTrace & 0x8 ){ + sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); + whereLoopPrint(pTemplate, pBuilder->pWC); + } +#endif + } + return SQLITE_OK; + } + + /* Look for an existing WhereLoop to replace with pTemplate + */ + whereLoopAdjustCost(pWInfo->pLoops, pTemplate); + ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); + + if( ppPrev==0 ){ + /* There already exists a WhereLoop on the list that is better + ** than pTemplate, so just ignore pTemplate */ +#if WHERETRACE_ENABLED /* 0x8 */ + if( sqlite3WhereTrace & 0x8 ){ + sqlite3DebugPrintf(" skip: "); + whereLoopPrint(pTemplate, pBuilder->pWC); + } +#endif + return SQLITE_OK; + }else{ + p = *ppPrev; + } + + /* If we reach this point it means that either p[] should be overwritten + ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new + ** WhereLoop and insert it. + */ +#if WHERETRACE_ENABLED /* 0x8 */ + if( sqlite3WhereTrace & 0x8 ){ + if( p!=0 ){ + sqlite3DebugPrintf("replace: "); + whereLoopPrint(p, pBuilder->pWC); + } + sqlite3DebugPrintf(" add: "); + whereLoopPrint(pTemplate, pBuilder->pWC); + } +#endif + if( p==0 ){ + /* Allocate a new WhereLoop to add to the end of the list */ + *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop)); + if( p==0 ) return SQLITE_NOMEM_BKPT; + whereLoopInit(p); + p->pNextLoop = 0; + }else{ + /* We will be overwriting WhereLoop p[]. But before we do, first + ** go through the rest of the list and delete any other entries besides + ** p[] that are also supplated by pTemplate */ + WhereLoop **ppTail = &p->pNextLoop; + WhereLoop *pToDel; + while( *ppTail ){ + ppTail = whereLoopFindLesser(ppTail, pTemplate); + if( ppTail==0 ) break; + pToDel = *ppTail; + if( pToDel==0 ) break; + *ppTail = pToDel->pNextLoop; +#if WHERETRACE_ENABLED /* 0x8 */ + if( sqlite3WhereTrace & 0x8 ){ + sqlite3DebugPrintf(" delete: "); + whereLoopPrint(pToDel, pBuilder->pWC); + } +#endif + whereLoopDelete(db, pToDel); + } + } + rc = whereLoopXfer(db, p, pTemplate); + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ + Index *pIndex = p->u.btree.pIndex; + if( pIndex && pIndex->tnum==0 ){ + p->u.btree.pIndex = 0; + } + } + return rc; +} + +/* +** Adjust the WhereLoop.nOut value downward to account for terms of the +** WHERE clause that reference the loop but which are not used by an +** index. +* +** For every WHERE clause term that is not used by the index +** and which has a truth probability assigned by one of the likelihood(), +** likely(), or unlikely() SQL functions, reduce the estimated number +** of output rows by the probability specified. +** +** TUNING: For every WHERE clause term that is not used by the index +** and which does not have an assigned truth probability, heuristics +** described below are used to try to estimate the truth probability. +** TODO --> Perhaps this is something that could be improved by better +** table statistics. +** +** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% +** value corresponds to -1 in LogEst notation, so this means decrement +** the WhereLoop.nOut field for every such WHERE clause term. +** +** Heuristic 2: If there exists one or more WHERE clause terms of the +** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the +** final output row estimate is no greater than 1/4 of the total number +** of rows in the table. In other words, assume that x==EXPR will filter +** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the +** "x" column is boolean or else -1 or 0 or 1 is a common default value +** on the "x" column and so in that case only cap the output row estimate +** at 1/2 instead of 1/4. +*/ +static void whereLoopOutputAdjust( + WhereClause *pWC, /* The WHERE clause */ + WhereLoop *pLoop, /* The loop to adjust downward */ + LogEst nRow /* Number of rows in the entire table */ +){ + WhereTerm *pTerm, *pX; + Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); + int i, j, k; + LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ + + assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); + for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ + if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; + if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; + if( (pTerm->prereqAll & notAllowed)!=0 ) continue; + for(j=pLoop->nLTerm-1; j>=0; j--){ + pX = pLoop->aLTerm[j]; + if( pX==0 ) continue; + if( pX==pTerm ) break; + if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; + } + if( j<0 ){ + if( pTerm->truthProb<=0 ){ + /* If a truth probability is specified using the likelihood() hints, + ** then use the probability provided by the application. */ + pLoop->nOut += pTerm->truthProb; + }else{ + /* In the absence of explicit truth probabilities, use heuristics to + ** guess a reasonable truth probability. */ + pLoop->nOut--; + if( pTerm->eOperator&(WO_EQ|WO_IS) ){ + Expr *pRight = pTerm->pExpr->pRight; + testcase( pTerm->pExpr->op==TK_IS ); + if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ + k = 10; + }else{ + k = 20; + } + if( iReducenOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; +} + +/* +** Adjust the cost C by the costMult facter T. This only occurs if +** compiled with -DSQLITE_ENABLE_COSTMULT +*/ +#ifdef SQLITE_ENABLE_COSTMULT +# define ApplyCostMultiplier(C,T) C += T +#else +# define ApplyCostMultiplier(C,T) +#endif + +/* +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the +** index pIndex. Try to match one more. +** +** When this function is called, pBuilder->pNew->nOut contains the +** number of rows expected to be visited by filtering using the nEq +** terms only. If it is modified, this value is restored before this +** function returns. +** +** If pProbe->tnum==0, that means pIndex is a fake index used for the +** INTEGER PRIMARY KEY. +*/ +static int whereLoopAddBtreeIndex( + WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ + struct SrcList_item *pSrc, /* FROM clause term being analyzed */ + Index *pProbe, /* An index on pSrc */ + LogEst nInMul /* log(Number of iterations due to IN) */ +){ + WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ + Parse *pParse = pWInfo->pParse; /* Parsing context */ + sqlite3 *db = pParse->db; /* Database connection malloc context */ + WhereLoop *pNew; /* Template WhereLoop under construction */ + WhereTerm *pTerm; /* A WhereTerm under consideration */ + int opMask; /* Valid operators for constraints */ + WhereScan scan; /* Iterator for WHERE terms */ + Bitmask saved_prereq; /* Original value of pNew->prereq */ + u16 saved_nLTerm; /* Original value of pNew->nLTerm */ + u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ + u16 saved_nSkip; /* Original value of pNew->nSkip */ + u32 saved_wsFlags; /* Original value of pNew->wsFlags */ + LogEst saved_nOut; /* Original value of pNew->nOut */ + int rc = SQLITE_OK; /* Return code */ + LogEst rSize; /* Number of rows in the table */ + LogEst rLogSize; /* Logarithm of table size */ + WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ + + pNew = pBuilder->pNew; + if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; + + assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); + assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); + if( pNew->wsFlags & WHERE_BTM_LIMIT ){ + opMask = WO_LT|WO_LE; + }else{ + opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; + } + if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); + + assert( pNew->u.btree.nEqnColumn ); + + saved_nEq = pNew->u.btree.nEq; + saved_nSkip = pNew->nSkip; + saved_nLTerm = pNew->nLTerm; + saved_wsFlags = pNew->wsFlags; + saved_prereq = pNew->prereq; + saved_nOut = pNew->nOut; + pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq, + opMask, pProbe); + pNew->rSetup = 0; + rSize = pProbe->aiRowLogEst[0]; + rLogSize = estLog(rSize); + for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ + u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ + LogEst rCostIdx; + LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ + int nIn = 0; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int nRecValid = pBuilder->nRecValid; +#endif + if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) + && indexColumnNotNull(pProbe, saved_nEq) + ){ + continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ + } + if( pTerm->prereqRight & pNew->maskSelf ) continue; + + /* Do not allow the upper bound of a LIKE optimization range constraint + ** to mix with a lower range bound from some other source */ + if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; + + /* Do not allow IS constraints from the WHERE clause to be used by the + ** right table of a LEFT JOIN. Only constraints in the ON clause are + ** allowed */ + if( (pSrc->fg.jointype & JT_LEFT)!=0 + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) + && (eOp & (WO_IS|WO_ISNULL))!=0 + ){ + testcase( eOp & WO_IS ); + testcase( eOp & WO_ISNULL ); + continue; + } + + pNew->wsFlags = saved_wsFlags; + pNew->u.btree.nEq = saved_nEq; + pNew->nLTerm = saved_nLTerm; + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ + pNew->aLTerm[pNew->nLTerm++] = pTerm; + pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; + + assert( nInMul==0 + || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 + || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 + || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 + ); + + if( eOp & WO_IN ){ + Expr *pExpr = pTerm->pExpr; + pNew->wsFlags |= WHERE_COLUMN_IN; + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ + nIn = 46; assert( 46==sqlite3LogEst(25) ); + }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ + /* "x IN (value, value, ...)" */ + nIn = sqlite3LogEst(pExpr->x.pList->nExpr); + } + assert( nIn>0 ); /* RHS always has 2 or more terms... The parser + ** changes "x IN (?)" into "x=?". */ + + }else if( eOp & (WO_EQ|WO_IS) ){ + int iCol = pProbe->aiColumn[saved_nEq]; + pNew->wsFlags |= WHERE_COLUMN_EQ; + assert( saved_nEq==pNew->u.btree.nEq ); + if( iCol==XN_ROWID + || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) + ){ + if( iCol>=0 && pProbe->uniqNotNull==0 ){ + pNew->wsFlags |= WHERE_UNQ_WANTED; + }else{ + pNew->wsFlags |= WHERE_ONEROW; + } + } + }else if( eOp & WO_ISNULL ){ + pNew->wsFlags |= WHERE_COLUMN_NULL; + }else if( eOp & (WO_GT|WO_GE) ){ + testcase( eOp & WO_GT ); + testcase( eOp & WO_GE ); + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; + pBtm = pTerm; + pTop = 0; + if( pTerm->wtFlags & TERM_LIKEOPT ){ + /* Range contraints that come from the LIKE optimization are + ** always used in pairs. */ + pTop = &pTerm[1]; + assert( (pTop-(pTerm->pWC->a))pWC->nTerm ); + assert( pTop->wtFlags & TERM_LIKEOPT ); + assert( pTop->eOperator==WO_LT ); + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ + pNew->aLTerm[pNew->nLTerm++] = pTop; + pNew->wsFlags |= WHERE_TOP_LIMIT; + } + }else{ + assert( eOp & (WO_LT|WO_LE) ); + testcase( eOp & WO_LT ); + testcase( eOp & WO_LE ); + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; + pTop = pTerm; + pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? + pNew->aLTerm[pNew->nLTerm-2] : 0; + } + + /* At this point pNew->nOut is set to the number of rows expected to + ** be visited by the index scan before considering term pTerm, or the + ** values of nIn and nInMul. In other words, assuming that all + ** "x IN(...)" terms are replaced with "x = ?". This block updates + ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ + assert( pNew->nOut==saved_nOut ); + if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ + /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 + ** data, using some other estimate. */ + whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); + }else{ + int nEq = ++pNew->u.btree.nEq; + assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); + + assert( pNew->nOut==saved_nOut ); + if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){ + assert( (eOp & WO_IN) || nIn==0 ); + testcase( eOp & WO_IN ); + pNew->nOut += pTerm->truthProb; + pNew->nOut -= nIn; + }else{ +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + tRowcnt nOut = 0; + if( nInMul==0 + && pProbe->nSample + && pNew->u.btree.nEq<=pProbe->nSampleCol + && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) + ){ + Expr *pExpr = pTerm->pExpr; + if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ + testcase( eOp & WO_EQ ); + testcase( eOp & WO_IS ); + testcase( eOp & WO_ISNULL ); + rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); + }else{ + rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); + } + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ + if( nOut ){ + pNew->nOut = sqlite3LogEst(nOut); + if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; + pNew->nOut -= nIn; + } + } + if( nOut==0 ) +#endif + { + pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); + if( eOp & WO_ISNULL ){ + /* TUNING: If there is no likelihood() value, assume that a + ** "col IS NULL" expression matches twice as many rows + ** as (col=?). */ + pNew->nOut += 10; + } + } + } + } + + /* Set rCostIdx to the cost of visiting selected rows in index. Add + ** it to pNew->rRun, which is currently set to the cost of the index + ** seek only. Then, if this is a non-covering index, add the cost of + ** visiting the rows in the main table. */ + rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; + pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); + if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ + pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); + } + ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); + + nOutUnadjusted = pNew->nOut; + pNew->rRun += nInMul + nIn; + pNew->nOut += nInMul + nIn; + whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); + rc = whereLoopInsert(pBuilder, pNew); + + if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ + pNew->nOut = saved_nOut; + }else{ + pNew->nOut = nOutUnadjusted; + } + + if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 + && pNew->u.btree.nEqnColumn + ){ + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); + } + pNew->nOut = saved_nOut; +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + pBuilder->nRecValid = nRecValid; +#endif + } + pNew->prereq = saved_prereq; + pNew->u.btree.nEq = saved_nEq; + pNew->nSkip = saved_nSkip; + pNew->wsFlags = saved_wsFlags; + pNew->nOut = saved_nOut; + pNew->nLTerm = saved_nLTerm; + + /* Consider using a skip-scan if there are no WHERE clause constraints + ** available for the left-most terms of the index, and if the average + ** number of repeats in the left-most terms is at least 18. + ** + ** The magic number 18 is selected on the basis that scanning 17 rows + ** is almost always quicker than an index seek (even though if the index + ** contains fewer than 2^17 rows we assume otherwise in other parts of + ** the code). And, even if it is not, it should not be too much slower. + ** On the other hand, the extra seeks could end up being significantly + ** more expensive. */ + assert( 42==sqlite3LogEst(18) ); + if( saved_nEq==saved_nSkip + && saved_nEq+1nKeyCol + && pProbe->noSkipScan==0 + && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ + && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK + ){ + LogEst nIter; + pNew->u.btree.nEq++; + pNew->nSkip++; + pNew->aLTerm[pNew->nLTerm++] = 0; + pNew->wsFlags |= WHERE_SKIPSCAN; + nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; + pNew->nOut -= nIter; + /* TUNING: Because uncertainties in the estimates for skip-scan queries, + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ + nIter += 5; + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); + pNew->nOut = saved_nOut; + pNew->u.btree.nEq = saved_nEq; + pNew->nSkip = saved_nSkip; + pNew->wsFlags = saved_wsFlags; + } + + return rc; +} + +/* +** Return True if it is possible that pIndex might be useful in +** implementing the ORDER BY clause in pBuilder. +** +** Return False if pBuilder does not contain an ORDER BY clause or +** if there is no way for pIndex to be useful in implementing that +** ORDER BY clause. +*/ +static int indexMightHelpWithOrderBy( + WhereLoopBuilder *pBuilder, + Index *pIndex, + int iCursor +){ + ExprList *pOB; + ExprList *aColExpr; + int ii, jj; + + if( pIndex->bUnordered ) return 0; + if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; + for(ii=0; iinExpr; ii++){ + Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); + if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){ + if( pExpr->iColumn<0 ) return 1; + for(jj=0; jjnKeyCol; jj++){ + if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; + } + }else if( (aColExpr = pIndex->aColExpr)!=0 ){ + for(jj=0; jjnKeyCol; jj++){ + if( pIndex->aiColumn[jj]!=XN_EXPR ) continue; + if( sqlite3ExprCompare(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){ + return 1; + } + } + } + } + return 0; +} + +/* +** Return a bitmask where 1s indicate that the corresponding column of +** the table is used by an index. Only the first 63 columns are considered. +*/ +static Bitmask columnsInIndex(Index *pIdx){ + Bitmask m = 0; + int j; + for(j=pIdx->nColumn-1; j>=0; j--){ + int x = pIdx->aiColumn[j]; + if( x>=0 ){ + testcase( x==BMS-1 ); + testcase( x==BMS-2 ); + if( xop==TK_AND ){ + if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0; + pWhere = pWhere->pRight; + } + for(i=0, pTerm=pWC->a; inTerm; i++, pTerm++){ + Expr *pExpr = pTerm->pExpr; + if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) + && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) + ){ + return 1; + } + } + return 0; +} + +/* +** Add all WhereLoop objects for a single table of the join where the table +** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be +** a b-tree table, not a virtual table. +** +** The costs (WhereLoop.rRun) of the b-tree loops added by this function +** are calculated as follows: +** +** For a full scan, assuming the table (or index) contains nRow rows: +** +** cost = nRow * 3.0 // full-table scan +** cost = nRow * K // scan of covering index +** cost = nRow * (K+3.0) // scan of non-covering index +** +** where K is a value between 1.1 and 3.0 set based on the relative +** estimated average size of the index and table records. +** +** For an index scan, where nVisit is the number of index rows visited +** by the scan, and nSeek is the number of seek operations required on +** the index b-tree: +** +** cost = nSeek * (log(nRow) + K * nVisit) // covering index +** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index +** +** Normally, nSeek is 1. nSeek values greater than 1 come about if the +** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when +** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. +** +** The estimated values (nRow, nVisit, nSeek) often contain a large amount +** of uncertainty. For this reason, scoring is designed to pick plans that +** "do the least harm" if the estimates are inaccurate. For example, a +** log(nRow) factor is omitted from a non-covering index scan in order to +** bias the scoring in favor of using an index, since the worst-case +** performance of using an index is far better than the worst-case performance +** of a full table scan. +*/ +static int whereLoopAddBtree( + WhereLoopBuilder *pBuilder, /* WHERE clause information */ + Bitmask mPrereq /* Extra prerequesites for using this table */ +){ + WhereInfo *pWInfo; /* WHERE analysis context */ + Index *pProbe; /* An index we are evaluating */ + Index sPk; /* A fake index object for the primary key */ + LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ + i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ + SrcList *pTabList; /* The FROM clause */ + struct SrcList_item *pSrc; /* The FROM clause btree term to add */ + WhereLoop *pNew; /* Template WhereLoop object */ + int rc = SQLITE_OK; /* Return code */ + int iSortIdx = 1; /* Index number */ + int b; /* A boolean value */ + LogEst rSize; /* number of rows in the table */ + LogEst rLogSize; /* Logarithm of the number of rows in the table */ + WhereClause *pWC; /* The parsed WHERE clause */ + Table *pTab; /* Table being queried */ + + pNew = pBuilder->pNew; + pWInfo = pBuilder->pWInfo; + pTabList = pWInfo->pTabList; + pSrc = pTabList->a + pNew->iTab; + pTab = pSrc->pTab; + pWC = pBuilder->pWC; + assert( !IsVirtual(pSrc->pTab) ); + + if( pSrc->pIBIndex ){ + /* An INDEXED BY clause specifies a particular index to use */ + pProbe = pSrc->pIBIndex; + }else if( !HasRowid(pTab) ){ + pProbe = pTab->pIndex; + }else{ + /* There is no INDEXED BY clause. Create a fake Index object in local + ** variable sPk to represent the rowid primary key index. Make this + ** fake index the first in a chain of Index objects with all of the real + ** indices to follow */ + Index *pFirst; /* First of real indices on the table */ + memset(&sPk, 0, sizeof(Index)); + sPk.nKeyCol = 1; + sPk.nColumn = 1; + sPk.aiColumn = &aiColumnPk; + sPk.aiRowLogEst = aiRowEstPk; + sPk.onError = OE_Replace; + sPk.pTable = pTab; + sPk.szIdxRow = pTab->szTabRow; + aiRowEstPk[0] = pTab->nRowLogEst; + aiRowEstPk[1] = 0; + pFirst = pSrc->pTab->pIndex; + if( pSrc->fg.notIndexed==0 ){ + /* The real indices of the table are only considered if the + ** NOT INDEXED qualifier is omitted from the FROM clause */ + sPk.pNext = pFirst; + } + pProbe = &sPk; + } + rSize = pTab->nRowLogEst; + rLogSize = estLog(rSize); + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX + /* Automatic indexes */ + if( !pBuilder->pOrSet /* Not part of an OR optimization */ + && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 + && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 + && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */ + && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */ + && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */ + && !pSrc->fg.isCorrelated /* Not a correlated subquery */ + && !pSrc->fg.isRecursive /* Not a recursive common table expression. */ + ){ + /* Generate auto-index WhereLoops */ + WhereTerm *pTerm; + WhereTerm *pWCEnd = pWC->a + pWC->nTerm; + for(pTerm=pWC->a; rc==SQLITE_OK && pTermprereqRight & pNew->maskSelf ) continue; + if( termCanDriveIndex(pTerm, pSrc, 0) ){ + pNew->u.btree.nEq = 1; + pNew->nSkip = 0; + pNew->u.btree.pIndex = 0; + pNew->nLTerm = 1; + pNew->aLTerm[0] = pTerm; + /* TUNING: One-time cost for computing the automatic index is + ** estimated to be X*N*log2(N) where N is the number of rows in + ** the table being indexed and where X is 7 (LogEst=28) for normal + ** tables or 1.375 (LogEst=4) for views and subqueries. The value + ** of X is smaller for views and subqueries so that the query planner + ** will be more aggressive about generating automatic indexes for + ** those objects, since there is no opportunity to add schema + ** indexes on subqueries and views. */ + pNew->rSetup = rLogSize + rSize + 4; + if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ + pNew->rSetup += 24; + } + ApplyCostMultiplier(pNew->rSetup, pTab->costMult); + /* TUNING: Each index lookup yields 20 rows in the table. This + ** is more than the usual guess of 10 rows, since we have no way + ** of knowing how selective the index will ultimately be. It would + ** not be unreasonable to make this value much larger. */ + pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); + pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); + pNew->wsFlags = WHERE_AUTO_INDEX; + pNew->prereq = mPrereq | pTerm->prereqRight; + rc = whereLoopInsert(pBuilder, pNew); + } + } + } +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ + + /* Loop over all indices + */ + for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ + if( pProbe->pPartIdxWhere!=0 + && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ + testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ + continue; /* Partial index inappropriate for this query */ + } + rSize = pProbe->aiRowLogEst[0]; + pNew->u.btree.nEq = 0; + pNew->nSkip = 0; + pNew->nLTerm = 0; + pNew->iSortIdx = 0; + pNew->rSetup = 0; + pNew->prereq = mPrereq; + pNew->nOut = rSize; + pNew->u.btree.pIndex = pProbe; + b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); + /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ + assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); + if( pProbe->tnum<=0 ){ + /* Integer primary key index */ + pNew->wsFlags = WHERE_IPK; + + /* Full table scan */ + pNew->iSortIdx = b ? iSortIdx : 0; + /* TUNING: Cost of full table scan is (N*3.0). */ + pNew->rRun = rSize + 16; + ApplyCostMultiplier(pNew->rRun, pTab->costMult); + whereLoopOutputAdjust(pWC, pNew, rSize); + rc = whereLoopInsert(pBuilder, pNew); + pNew->nOut = rSize; + if( rc ) break; + }else{ + Bitmask m; + if( pProbe->isCovering ){ + pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; + m = 0; + }else{ + m = pSrc->colUsed & ~columnsInIndex(pProbe); + pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; + } + + /* Full scan via index */ + if( b + || !HasRowid(pTab) + || ( m==0 + && pProbe->bUnordered==0 + && (pProbe->szIdxRowszTabRow) + && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 + && sqlite3GlobalConfig.bUseCis + && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) + ) + ){ + pNew->iSortIdx = b ? iSortIdx : 0; + + /* The cost of visiting the index rows is N*K, where K is + ** between 1.1 and 3.0, depending on the relative sizes of the + ** index and table rows. If this is a non-covering index scan, + ** also add the cost of visiting table rows (N*3.0). */ + pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; + if( m!=0 ){ + pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); + } + ApplyCostMultiplier(pNew->rRun, pTab->costMult); + whereLoopOutputAdjust(pWC, pNew, rSize); + rc = whereLoopInsert(pBuilder, pNew); + pNew->nOut = rSize; + if( rc ) break; + } + } + + rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + sqlite3Stat4ProbeFree(pBuilder->pRec); + pBuilder->nRecValid = 0; + pBuilder->pRec = 0; +#endif + + /* If there was an INDEXED BY clause, then only that one index is + ** considered. */ + if( pSrc->pIBIndex ) break; + } + return rc; +} + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** Argument pIdxInfo is already populated with all constraints that may +** be used by the virtual table identified by pBuilder->pNew->iTab. This +** function marks a subset of those constraints usable, invokes the +** xBestIndex method and adds the returned plan to pBuilder. +** +** A constraint is marked usable if: +** +** * Argument mUsable indicates that its prerequisites are available, and +** +** * It is not one of the operators specified in the mExclude mask passed +** as the fourth argument (which in practice is either WO_IN or 0). +** +** Argument mPrereq is a mask of tables that must be scanned before the +** virtual table in question. These are added to the plans prerequisites +** before it is added to pBuilder. +** +** Output parameter *pbIn is set to true if the plan added to pBuilder +** uses one or more WO_IN terms, or false otherwise. +*/ +static int whereLoopAddVirtualOne( + WhereLoopBuilder *pBuilder, + Bitmask mPrereq, /* Mask of tables that must be used. */ + Bitmask mUsable, /* Mask of usable tables */ + u16 mExclude, /* Exclude terms using these operators */ + sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ + int *pbIn /* OUT: True if plan uses an IN(...) op */ +){ + WhereClause *pWC = pBuilder->pWC; + struct sqlite3_index_constraint *pIdxCons; + struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; + int i; + int mxTerm; + int rc = SQLITE_OK; + WhereLoop *pNew = pBuilder->pNew; + Parse *pParse = pBuilder->pWInfo->pParse; + struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab]; + int nConstraint = pIdxInfo->nConstraint; + + assert( (mUsable & mPrereq)==mPrereq ); + *pbIn = 0; + pNew->prereq = mPrereq; + + /* Set the usable flag on the subset of constraints identified by + ** arguments mUsable and mExclude. */ + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + for(i=0; ia[pIdxCons->iTermOffset]; + pIdxCons->usable = 0; + if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight + && (pTerm->eOperator & mExclude)==0 + ){ + pIdxCons->usable = 1; + } + } + + /* Initialize the output fields of the sqlite3_index_info structure */ + memset(pUsage, 0, sizeof(pUsage[0])*nConstraint); + assert( pIdxInfo->needToFreeIdxStr==0 ); + pIdxInfo->idxStr = 0; + pIdxInfo->idxNum = 0; + pIdxInfo->orderByConsumed = 0; + pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; + pIdxInfo->estimatedRows = 25; + pIdxInfo->idxFlags = 0; + pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; + + /* Invoke the virtual table xBestIndex() method */ + rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); + if( rc ) return rc; + + mxTerm = -1; + assert( pNew->nLSlot>=nConstraint ); + for(i=0; iaLTerm[i] = 0; + pNew->u.vtab.omitMask = 0; + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + for(i=0; i=0 ){ + WhereTerm *pTerm; + int j = pIdxCons->iTermOffset; + if( iTerm>=nConstraint + || j<0 + || j>=pWC->nTerm + || pNew->aLTerm[iTerm]!=0 + || pIdxCons->usable==0 + ){ + rc = SQLITE_ERROR; + sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); + return rc; + } + testcase( iTerm==nConstraint-1 ); + testcase( j==0 ); + testcase( j==pWC->nTerm-1 ); + pTerm = &pWC->a[j]; + pNew->prereq |= pTerm->prereqRight; + assert( iTermnLSlot ); + pNew->aLTerm[iTerm] = pTerm; + if( iTerm>mxTerm ) mxTerm = iTerm; + testcase( iTerm==15 ); + testcase( iTerm==16 ); + if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<eOperator & WO_IN)!=0 ){ + /* A virtual table that is constrained by an IN clause may not + ** consume the ORDER BY clause because (1) the order of IN terms + ** is not necessarily related to the order of output terms and + ** (2) Multiple outputs from a single IN value will not merge + ** together. */ + pIdxInfo->orderByConsumed = 0; + pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; + *pbIn = 1; assert( (mExclude & WO_IN)==0 ); + } + } + } + + pNew->nLTerm = mxTerm+1; + assert( pNew->nLTerm<=pNew->nLSlot ); + pNew->u.vtab.idxNum = pIdxInfo->idxNum; + pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; + pIdxInfo->needToFreeIdxStr = 0; + pNew->u.vtab.idxStr = pIdxInfo->idxStr; + pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? + pIdxInfo->nOrderBy : 0); + pNew->rSetup = 0; + pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); + pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); + + /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated + ** that the scan will visit at most one row. Clear it otherwise. */ + if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ + pNew->wsFlags |= WHERE_ONEROW; + }else{ + pNew->wsFlags &= ~WHERE_ONEROW; + } + rc = whereLoopInsert(pBuilder, pNew); + if( pNew->u.vtab.needFree ){ + sqlite3_free(pNew->u.vtab.idxStr); + pNew->u.vtab.needFree = 0; + } + WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n", + *pbIn, (sqlite3_uint64)mPrereq, + (sqlite3_uint64)(pNew->prereq & ~mPrereq))); + + return rc; +} + + +/* +** Add all WhereLoop objects for a table of the join identified by +** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. +** +** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and +** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause +** entries that occur before the virtual table in the FROM clause and are +** separated from it by at least one LEFT or CROSS JOIN. Similarly, the +** mUnusable mask contains all FROM clause entries that occur after the +** virtual table and are separated from it by at least one LEFT or +** CROSS JOIN. +** +** For example, if the query were: +** +** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; +** +** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). +** +** All the tables in mPrereq must be scanned before the current virtual +** table. So any terms for which all prerequisites are satisfied by +** mPrereq may be specified as "usable" in all calls to xBestIndex. +** Conversely, all tables in mUnusable must be scanned after the current +** virtual table, so any terms for which the prerequisites overlap with +** mUnusable should always be configured as "not-usable" for xBestIndex. +*/ +static int whereLoopAddVirtual( + WhereLoopBuilder *pBuilder, /* WHERE clause information */ + Bitmask mPrereq, /* Tables that must be scanned before this one */ + Bitmask mUnusable /* Tables that must be scanned after this one */ +){ + int rc = SQLITE_OK; /* Return code */ + WhereInfo *pWInfo; /* WHERE analysis context */ + Parse *pParse; /* The parsing context */ + WhereClause *pWC; /* The WHERE clause */ + struct SrcList_item *pSrc; /* The FROM clause term to search */ + sqlite3_index_info *p; /* Object to pass to xBestIndex() */ + int nConstraint; /* Number of constraints in p */ + int bIn; /* True if plan uses IN(...) operator */ + WhereLoop *pNew; + Bitmask mBest; /* Tables used by best possible plan */ + + assert( (mPrereq & mUnusable)==0 ); + pWInfo = pBuilder->pWInfo; + pParse = pWInfo->pParse; + pWC = pBuilder->pWC; + pNew = pBuilder->pNew; + pSrc = &pWInfo->pTabList->a[pNew->iTab]; + assert( IsVirtual(pSrc->pTab) ); + p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy); + if( p==0 ) return SQLITE_NOMEM_BKPT; + pNew->rSetup = 0; + pNew->wsFlags = WHERE_VIRTUALTABLE; + pNew->nLTerm = 0; + pNew->u.vtab.needFree = 0; + nConstraint = p->nConstraint; + if( whereLoopResize(pParse->db, pNew, nConstraint) ){ + sqlite3DbFree(pParse->db, p); + return SQLITE_NOMEM_BKPT; + } + + /* First call xBestIndex() with all constraints usable. */ + WHERETRACE(0x40, (" VirtualOne: all usable\n")); + rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, &bIn); + + /* If the call to xBestIndex() with all terms enabled produced a plan + ** that does not require any source tables (IOW: a plan with mBest==0), + ** then there is no point in making any further calls to xBestIndex() + ** since they will all return the same result (if the xBestIndex() + ** implementation is sane). */ + if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){ + int seenZero = 0; /* True if a plan with no prereqs seen */ + int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */ + Bitmask mPrev = 0; + Bitmask mBestNoIn = 0; + + /* If the plan produced by the earlier call uses an IN(...) term, call + ** xBestIndex again, this time with IN(...) terms disabled. */ + if( bIn ){ + WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); + rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, WO_IN, p, &bIn); + assert( bIn==0 ); + mBestNoIn = pNew->prereq & ~mPrereq; + if( mBestNoIn==0 ){ + seenZero = 1; + seenZeroNoIN = 1; + } + } + + /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) + ** in the set of terms that apply to the current virtual table. */ + while( rc==SQLITE_OK ){ + int i; + Bitmask mNext = ALLBITS; + assert( mNext>0 ); + for(i=0; ia[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq + ); + if( mThis>mPrev && mThisprereq==mPrereq ){ + seenZero = 1; + if( bIn==0 ) seenZeroNoIN = 1; + } + } + + /* If the calls to xBestIndex() in the above loop did not find a plan + ** that requires no source tables at all (i.e. one guaranteed to be + ** usable), make a call here with all source tables disabled */ + if( rc==SQLITE_OK && seenZero==0 ){ + WHERETRACE(0x40, (" VirtualOne: all disabled\n")); + rc = whereLoopAddVirtualOne(pBuilder, mPrereq, mPrereq, 0, p, &bIn); + if( bIn==0 ) seenZeroNoIN = 1; + } + + /* If the calls to xBestIndex() have so far failed to find a plan + ** that requires no source tables at all and does not use an IN(...) + ** operator, make a final call to obtain one here. */ + if( rc==SQLITE_OK && seenZeroNoIN==0 ){ + WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); + rc = whereLoopAddVirtualOne(pBuilder, mPrereq, mPrereq, WO_IN, p, &bIn); + } + } + + if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); + sqlite3DbFree(pParse->db, p); + return rc; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** Add WhereLoop entries to handle OR terms. This works for either +** btrees or virtual tables. +*/ +static int whereLoopAddOr( + WhereLoopBuilder *pBuilder, + Bitmask mPrereq, + Bitmask mUnusable +){ + WhereInfo *pWInfo = pBuilder->pWInfo; + WhereClause *pWC; + WhereLoop *pNew; + WhereTerm *pTerm, *pWCEnd; + int rc = SQLITE_OK; + int iCur; + WhereClause tempWC; + WhereLoopBuilder sSubBuild; + WhereOrSet sSum, sCur; + struct SrcList_item *pItem; + + pWC = pBuilder->pWC; + pWCEnd = pWC->a + pWC->nTerm; + pNew = pBuilder->pNew; + memset(&sSum, 0, sizeof(sSum)); + pItem = pWInfo->pTabList->a + pNew->iTab; + iCur = pItem->iCursor; + + for(pTerm=pWC->a; pTermeOperator & WO_OR)!=0 + && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 + ){ + WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; + WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; + WhereTerm *pOrTerm; + int once = 1; + int i, j; + + sSubBuild = *pBuilder; + sSubBuild.pOrderBy = 0; + sSubBuild.pOrSet = &sCur; + + WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); + for(pOrTerm=pOrWC->a; pOrTermeOperator & WO_AND)!=0 ){ + sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; + }else if( pOrTerm->leftCursor==iCur ){ + tempWC.pWInfo = pWC->pWInfo; + tempWC.pOuter = pWC; + tempWC.op = TK_AND; + tempWC.nTerm = 1; + tempWC.a = pOrTerm; + sSubBuild.pWC = &tempWC; + }else{ + continue; + } + sCur.n = 0; +#ifdef WHERETRACE_ENABLED + WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", + (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); + if( sqlite3WhereTrace & 0x400 ){ + for(i=0; inTerm; i++){ + whereTermPrint(&sSubBuild.pWC->a[i], i); + } + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pItem->pTab) ){ + rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable); + }else +#endif + { + rc = whereLoopAddBtree(&sSubBuild, mPrereq); + } + if( rc==SQLITE_OK ){ + rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable); + } + assert( rc==SQLITE_OK || sCur.n==0 ); + if( sCur.n==0 ){ + sSum.n = 0; + break; + }else if( once ){ + whereOrMove(&sSum, &sCur); + once = 0; + }else{ + WhereOrSet sPrev; + whereOrMove(&sPrev, &sSum); + sSum.n = 0; + for(i=0; inLTerm = 1; + pNew->aLTerm[0] = pTerm; + pNew->wsFlags = WHERE_MULTI_OR; + pNew->rSetup = 0; + pNew->iSortIdx = 0; + memset(&pNew->u, 0, sizeof(pNew->u)); + for(i=0; rc==SQLITE_OK && irRun = sSum.a[i].rRun + 1; + pNew->nOut = sSum.a[i].nOut; + pNew->prereq = sSum.a[i].prereq; + rc = whereLoopInsert(pBuilder, pNew); + } + WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); + } + } + return rc; +} + +/* +** Add all WhereLoop objects for all tables +*/ +static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ + WhereInfo *pWInfo = pBuilder->pWInfo; + Bitmask mPrereq = 0; + Bitmask mPrior = 0; + int iTab; + SrcList *pTabList = pWInfo->pTabList; + struct SrcList_item *pItem; + struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; + sqlite3 *db = pWInfo->pParse->db; + int rc = SQLITE_OK; + WhereLoop *pNew; + u8 priorJointype = 0; + + /* Loop over the tables in the join, from left to right */ + pNew = pBuilder->pNew; + whereLoopInit(pNew); + for(iTab=0, pItem=pTabList->a; pItemiTab = iTab; + pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); + if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ + /* This condition is true when pItem is the FROM clause term on the + ** right-hand-side of a LEFT or CROSS JOIN. */ + mPrereq = mPrior; + } + priorJointype = pItem->fg.jointype; + if( IsVirtual(pItem->pTab) ){ + struct SrcList_item *p; + for(p=&pItem[1]; pfg.jointype & (JT_LEFT|JT_CROSS)) ){ + mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); + } + } + rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); + }else{ + rc = whereLoopAddBtree(pBuilder, mPrereq); + } + if( rc==SQLITE_OK ){ + rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); + } + mPrior |= pNew->maskSelf; + if( rc || db->mallocFailed ) break; + } + + whereLoopClear(db, pNew); + return rc; +} + +/* +** Examine a WherePath (with the addition of the extra WhereLoop of the 5th +** parameters) to see if it outputs rows in the requested ORDER BY +** (or GROUP BY) without requiring a separate sort operation. Return N: +** +** N>0: N terms of the ORDER BY clause are satisfied +** N==0: No terms of the ORDER BY clause are satisfied +** N<0: Unknown yet how many terms of ORDER BY might be satisfied. +** +** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as +** strict. With GROUP BY and DISTINCT the only requirement is that +** equivalent rows appear immediately adjacent to one another. GROUP BY +** and DISTINCT do not require rows to appear in any particular order as long +** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT +** the pOrderBy terms can be matched in any order. With ORDER BY, the +** pOrderBy terms must be matched in strict left-to-right order. +*/ +static i8 wherePathSatisfiesOrderBy( + WhereInfo *pWInfo, /* The WHERE clause */ + ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ + WherePath *pPath, /* The WherePath to check */ + u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ + u16 nLoop, /* Number of entries in pPath->aLoop[] */ + WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ + Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ +){ + u8 revSet; /* True if rev is known */ + u8 rev; /* Composite sort order */ + u8 revIdx; /* Index sort order */ + u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ + u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ + u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ + u16 nKeyCol; /* Number of key columns in pIndex */ + u16 nColumn; /* Total number of ordered columns in the index */ + u16 nOrderBy; /* Number terms in the ORDER BY clause */ + int iLoop; /* Index of WhereLoop in pPath being processed */ + int i, j; /* Loop counters */ + int iCur; /* Cursor number for current WhereLoop */ + int iColumn; /* A column number within table iCur */ + WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ + WhereTerm *pTerm; /* A single term of the WHERE clause */ + Expr *pOBExpr; /* An expression from the ORDER BY clause */ + CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ + Index *pIndex; /* The index associated with pLoop */ + sqlite3 *db = pWInfo->pParse->db; /* Database connection */ + Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ + Bitmask obDone; /* Mask of all ORDER BY terms */ + Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ + Bitmask ready; /* Mask of inner loops */ + + /* + ** We say the WhereLoop is "one-row" if it generates no more than one + ** row of output. A WhereLoop is one-row if all of the following are true: + ** (a) All index columns match with WHERE_COLUMN_EQ. + ** (b) The index is unique + ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. + ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. + ** + ** We say the WhereLoop is "order-distinct" if the set of columns from + ** that WhereLoop that are in the ORDER BY clause are different for every + ** row of the WhereLoop. Every one-row WhereLoop is automatically + ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause + ** is not order-distinct. To be order-distinct is not quite the same as being + ** UNIQUE since a UNIQUE column or index can have multiple rows that + ** are NULL and NULL values are equivalent for the purpose of order-distinct. + ** To be order-distinct, the columns must be UNIQUE and NOT NULL. + ** + ** The rowid for a table is always UNIQUE and NOT NULL so whenever the + ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is + ** automatically order-distinct. + */ + + assert( pOrderBy!=0 ); + if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; + + nOrderBy = pOrderBy->nExpr; + testcase( nOrderBy==BMS-1 ); + if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ + isOrderDistinct = 1; + obDone = MASKBIT(nOrderBy)-1; + orderDistinctMask = 0; + ready = 0; + for(iLoop=0; isOrderDistinct && obSat0 ) ready |= pLoop->maskSelf; + pLoop = iLoopaLoop[iLoop] : pLast; + if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ + if( pLoop->u.vtab.isOrdered ) obSat = obDone; + break; + } + iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; + + /* Mark off any ORDER BY term X that is a column in the table of + ** the current loop for which there is term in the WHERE + ** clause of the form X IS NULL or X=? that reference only outer + ** loops. + */ + for(i=0; ia[i].pExpr); + if( pOBExpr->op!=TK_COLUMN ) continue; + if( pOBExpr->iTable!=iCur ) continue; + pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, + ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); + if( pTerm==0 ) continue; + if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ + const char *z1, *z2; + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); + if( !pColl ) pColl = db->pDfltColl; + z1 = pColl->zName; + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); + if( !pColl ) pColl = db->pDfltColl; + z2 = pColl->zName; + if( sqlite3StrICmp(z1, z2)!=0 ) continue; + testcase( pTerm->pExpr->op==TK_IS ); + } + obSat |= MASKBIT(i); + } + + if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ + if( pLoop->wsFlags & WHERE_IPK ){ + pIndex = 0; + nKeyCol = 0; + nColumn = 1; + }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ + return 0; + }else{ + nKeyCol = pIndex->nKeyCol; + nColumn = pIndex->nColumn; + assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); + assert( pIndex->aiColumn[nColumn-1]==XN_ROWID + || !HasRowid(pIndex->pTable)); + isOrderDistinct = IsUniqueIndex(pIndex); + } + + /* Loop through all columns of the index and deal with the ones + ** that are not constrained by == or IN. + */ + rev = revSet = 0; + distinctColumns = 0; + for(j=0; ju.btree.nEq + && pLoop->nSkip==0 + && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 + ){ + if( i & WO_ISNULL ){ + testcase( isOrderDistinct ); + isOrderDistinct = 0; + } + continue; + } + + /* Get the column number in the table (iColumn) and sort order + ** (revIdx) for the j-th column of the index. + */ + if( pIndex ){ + iColumn = pIndex->aiColumn[j]; + revIdx = pIndex->aSortOrder[j]; + if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; + }else{ + iColumn = XN_ROWID; + revIdx = 0; + } + + /* An unconstrained column that might be NULL means that this + ** WhereLoop is not well-ordered + */ + if( isOrderDistinct + && iColumn>=0 + && j>=pLoop->u.btree.nEq + && pIndex->pTable->aCol[iColumn].notNull==0 + ){ + isOrderDistinct = 0; + } + + /* Find the ORDER BY term that corresponds to the j-th column + ** of the index and mark that ORDER BY term off + */ + bOnce = 1; + isMatch = 0; + for(i=0; bOnce && ia[i].pExpr); + testcase( wctrlFlags & WHERE_GROUPBY ); + testcase( wctrlFlags & WHERE_DISTINCTBY ); + if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; + if( iColumn>=(-1) ){ + if( pOBExpr->op!=TK_COLUMN ) continue; + if( pOBExpr->iTable!=iCur ) continue; + if( pOBExpr->iColumn!=iColumn ) continue; + }else{ + if( sqlite3ExprCompare(pOBExpr,pIndex->aColExpr->a[j].pExpr,iCur) ){ + continue; + } + } + if( iColumn>=0 ){ + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); + if( !pColl ) pColl = db->pDfltColl; + if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; + } + isMatch = 1; + break; + } + if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ + /* Make sure the sort order is compatible in an ORDER BY clause. + ** Sort order is irrelevant for a GROUP BY clause. */ + if( revSet ){ + if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; + }else{ + rev = revIdx ^ pOrderBy->a[i].sortOrder; + if( rev ) *pRevMask |= MASKBIT(iLoop); + revSet = 1; + } + } + if( isMatch ){ + if( iColumn<0 ){ + testcase( distinctColumns==0 ); + distinctColumns = 1; + } + obSat |= MASKBIT(i); + }else{ + /* No match found */ + if( j==0 || jmaskSelf; + for(i=0; ia[i].pExpr; + mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p); + if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; + if( (mTerm&~orderDistinctMask)==0 ){ + obSat |= MASKBIT(i); + } + } + } + } /* End the loop over all WhereLoops from outer-most down to inner-most */ + if( obSat==obDone ) return (i8)nOrderBy; + if( !isOrderDistinct ){ + for(i=nOrderBy-1; i>0; i--){ + Bitmask m = MASKBIT(i) - 1; + if( (obSat&m)==m ) return i; + } + return 0; + } + return -1; +} + + +/* +** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), +** the planner assumes that the specified pOrderBy list is actually a GROUP +** BY clause - and so any order that groups rows as required satisfies the +** request. +** +** Normally, in this case it is not possible for the caller to determine +** whether or not the rows are really being delivered in sorted order, or +** just in some other order that provides the required grouping. However, +** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then +** this function may be called on the returned WhereInfo object. It returns +** true if the rows really will be sorted in the specified order, or false +** otherwise. +** +** For example, assuming: +** +** CREATE INDEX i1 ON t1(x, Y); +** +** then +** +** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 +** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 +*/ +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){ + assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); + assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); + return pWInfo->sorted; +} + +#ifdef WHERETRACE_ENABLED +/* For debugging use only: */ +static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ + static char zName[65]; + int i; + for(i=0; iaLoop[i]->cId; } + if( pLast ) zName[i++] = pLast->cId; + zName[i] = 0; + return zName; +} +#endif + +/* +** Return the cost of sorting nRow rows, assuming that the keys have +** nOrderby columns and that the first nSorted columns are already in +** order. +*/ +static LogEst whereSortingCost( + WhereInfo *pWInfo, + LogEst nRow, + int nOrderBy, + int nSorted +){ + /* TUNING: Estimated cost of a full external sort, where N is + ** the number of rows to sort is: + ** + ** cost = (3.0 * N * log(N)). + ** + ** Or, if the order-by clause has X terms but only the last Y + ** terms are out of order, then block-sorting will reduce the + ** sorting cost to: + ** + ** cost = (3.0 * N * log(N)) * (Y/X) + ** + ** The (Y/X) term is implemented using stack variable rScale + ** below. */ + LogEst rScale, rSortCost; + assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); + rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; + rSortCost = nRow + rScale + 16; + + /* Multiple by log(M) where M is the number of output rows. + ** Use the LIMIT for M if it is smaller */ + if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimitiLimit; + } + rSortCost += estLog(nRow); + return rSortCost; +} + +/* +** Given the list of WhereLoop objects at pWInfo->pLoops, this routine +** attempts to find the lowest cost path that visits each WhereLoop +** once. This path is then loaded into the pWInfo->a[].pWLoop fields. +** +** Assume that the total number of output rows that will need to be sorted +** will be nRowEst (in the 10*log2 representation). Or, ignore sorting +** costs if nRowEst==0. +** +** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation +** error occurs. +*/ +static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ + int mxChoice; /* Maximum number of simultaneous paths tracked */ + int nLoop; /* Number of terms in the join */ + Parse *pParse; /* Parsing context */ + sqlite3 *db; /* The database connection */ + int iLoop; /* Loop counter over the terms of the join */ + int ii, jj; /* Loop counters */ + int mxI = 0; /* Index of next entry to replace */ + int nOrderBy; /* Number of ORDER BY clause terms */ + LogEst mxCost = 0; /* Maximum cost of a set of paths */ + LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ + int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ + WherePath *aFrom; /* All nFrom paths at the previous level */ + WherePath *aTo; /* The nTo best paths at the current level */ + WherePath *pFrom; /* An element of aFrom[] that we are working on */ + WherePath *pTo; /* An element of aTo[] that we are working on */ + WhereLoop *pWLoop; /* One of the WhereLoop objects */ + WhereLoop **pX; /* Used to divy up the pSpace memory */ + LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ + char *pSpace; /* Temporary memory used by this routine */ + int nSpace; /* Bytes of space allocated at pSpace */ + + pParse = pWInfo->pParse; + db = pParse->db; + nLoop = pWInfo->nLevel; + /* TUNING: For simple queries, only the best path is tracked. + ** For 2-way joins, the 5 best paths are followed. + ** For joins of 3 or more tables, track the 10 best paths */ + mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); + assert( nLoop<=pWInfo->pTabList->nSrc ); + WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); + + /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this + ** case the purpose of this call is to estimate the number of rows returned + ** by the overall query. Once this estimate has been obtained, the caller + ** will invoke this function a second time, passing the estimate as the + ** nRowEst parameter. */ + if( pWInfo->pOrderBy==0 || nRowEst==0 ){ + nOrderBy = 0; + }else{ + nOrderBy = pWInfo->pOrderBy->nExpr; + } + + /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ + nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; + nSpace += sizeof(LogEst) * nOrderBy; + pSpace = sqlite3DbMallocRawNN(db, nSpace); + if( pSpace==0 ) return SQLITE_NOMEM_BKPT; + aTo = (WherePath*)pSpace; + aFrom = aTo+mxChoice; + memset(aFrom, 0, sizeof(aFrom[0])); + pX = (WhereLoop**)(aFrom+mxChoice); + for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ + pFrom->aLoop = pX; + } + if( nOrderBy ){ + /* If there is an ORDER BY clause and it is not being ignored, set up + ** space for the aSortCost[] array. Each element of the aSortCost array + ** is either zero - meaning it has not yet been initialized - or the + ** cost of sorting nRowEst rows of data where the first X terms of + ** the ORDER BY clause are already in order, where X is the array + ** index. */ + aSortCost = (LogEst*)pX; + memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); + } + assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); + assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); + + /* Seed the search with a single WherePath containing zero WhereLoops. + ** + ** TUNING: Do not let the number of iterations go above 28. If the cost + ** of computing an automatic index is not paid back within the first 28 + ** rows, then do not use the automatic index. */ + aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); + nFrom = 1; + assert( aFrom[0].isOrdered==0 ); + if( nOrderBy ){ + /* If nLoop is zero, then there are no FROM terms in the query. Since + ** in this case the query may return a maximum of one row, the results + ** are already in the requested order. Set isOrdered to nOrderBy to + ** indicate this. Or, if nLoop is greater than zero, set isOrdered to + ** -1, indicating that the result set may or may not be ordered, + ** depending on the loops added to the current plan. */ + aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; + } + + /* Compute successively longer WherePaths using the previous generation + ** of WherePaths as the basis for the next. Keep track of the mxChoice + ** best paths at each generation */ + for(iLoop=0; iLooppLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ + LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ + LogEst rCost; /* Cost of path (pFrom+pWLoop) */ + LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ + i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ + Bitmask maskNew; /* Mask of src visited by (..) */ + Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ + + if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; + if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; + if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){ + /* Do not use an automatic index if the this loop is expected + ** to run less than 2 times. */ + assert( 10==sqlite3LogEst(2) ); + continue; + } + /* At this point, pWLoop is a candidate to be the next loop. + ** Compute its cost */ + rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); + rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); + nOut = pFrom->nRow + pWLoop->nOut; + maskNew = pFrom->maskLoop | pWLoop->maskSelf; + if( isOrdered<0 ){ + isOrdered = wherePathSatisfiesOrderBy(pWInfo, + pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, + iLoop, pWLoop, &revMask); + }else{ + revMask = pFrom->revLoop; + } + if( isOrdered>=0 && isOrderedisOrdered^isOrdered)&0x80)==0" is equivalent + ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range + ** of legal values for isOrdered, -1..64. + */ + for(jj=0, pTo=aTo; jjmaskLoop==maskNew + && ((pTo->isOrdered^isOrdered)&0x80)==0 + ){ + testcase( jj==nTo-1 ); + break; + } + } + if( jj>=nTo ){ + /* None of the existing best-so-far paths match the candidate. */ + if( nTo>=mxChoice + && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) + ){ + /* The current candidate is no better than any of the mxChoice + ** paths currently in the best-so-far buffer. So discard + ** this candidate as not viable. */ +#ifdef WHERETRACE_ENABLED /* 0x4 */ + if( sqlite3WhereTrace&0x4 ){ + sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + isOrdered>=0 ? isOrdered+'0' : '?'); + } +#endif + continue; + } + /* If we reach this points it means that the new candidate path + ** needs to be added to the set of best-so-far paths. */ + if( nTo=0 ? isOrdered+'0' : '?'); + } +#endif + }else{ + /* Control reaches here if best-so-far path pTo=aTo[jj] covers the + ** same set of loops and has the sam isOrdered setting as the + ** candidate path. Check to see if the candidate should replace + ** pTo or if the candidate should be skipped */ + if( pTo->rCostrCost==rCost && pTo->nRow<=nOut) ){ +#ifdef WHERETRACE_ENABLED /* 0x4 */ + if( sqlite3WhereTrace&0x4 ){ + sqlite3DebugPrintf( + "Skip %s cost=%-3d,%3d order=%c", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + isOrdered>=0 ? isOrdered+'0' : '?'); + sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", + wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, + pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); + } +#endif + /* Discard the candidate path from further consideration */ + testcase( pTo->rCost==rCost ); + continue; + } + testcase( pTo->rCost==rCost+1 ); + /* Control reaches here if the candidate path is better than the + ** pTo path. Replace pTo with the candidate. */ +#ifdef WHERETRACE_ENABLED /* 0x4 */ + if( sqlite3WhereTrace&0x4 ){ + sqlite3DebugPrintf( + "Update %s cost=%-3d,%3d order=%c", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + isOrdered>=0 ? isOrdered+'0' : '?'); + sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", + wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, + pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); + } +#endif + } + /* pWLoop is a winner. Add it to the set of best so far */ + pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; + pTo->revLoop = revMask; + pTo->nRow = nOut; + pTo->rCost = rCost; + pTo->rUnsorted = rUnsorted; + pTo->isOrdered = isOrdered; + memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); + pTo->aLoop[iLoop] = pWLoop; + if( nTo>=mxChoice ){ + mxI = 0; + mxCost = aTo[0].rCost; + mxUnsorted = aTo[0].nRow; + for(jj=1, pTo=&aTo[1]; jjrCost>mxCost + || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) + ){ + mxCost = pTo->rCost; + mxUnsorted = pTo->rUnsorted; + mxI = jj; + } + } + } + } + } + +#ifdef WHERETRACE_ENABLED /* >=2 */ + if( sqlite3WhereTrace & 0x02 ){ + sqlite3DebugPrintf("---- after round %d ----\n", iLoop); + for(ii=0, pTo=aTo; iirCost, pTo->nRow, + pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); + if( pTo->isOrdered>0 ){ + sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); + }else{ + sqlite3DebugPrintf("\n"); + } + } + } +#endif + + /* Swap the roles of aFrom and aTo for the next generation */ + pFrom = aTo; + aTo = aFrom; + aFrom = pFrom; + nFrom = nTo; + } + + if( nFrom==0 ){ + sqlite3ErrorMsg(pParse, "no query solution"); + sqlite3DbFree(db, pSpace); + return SQLITE_ERROR; + } + + /* Find the lowest cost path. pFrom will be left pointing to that path */ + pFrom = aFrom; + for(ii=1; iirCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; + } + assert( pWInfo->nLevel==nLoop ); + /* Load the lowest cost path into pWInfo */ + for(iLoop=0; iLoopa + iLoop; + pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; + pLevel->iFrom = pWLoop->iTab; + pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; + } + if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 + && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 + && pWInfo->eDistinct==WHERE_DISTINCT_NOOP + && nRowEst + ){ + Bitmask notUsed; + int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinctSet, pFrom, + WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); + if( rc==pWInfo->pDistinctSet->nExpr ){ + pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; + } + } + if( pWInfo->pOrderBy ){ + if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ + if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ + pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; + } + }else{ + pWInfo->nOBSat = pFrom->isOrdered; + if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; + pWInfo->revMask = pFrom->revLoop; + } + if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) + && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 + ){ + Bitmask revMask = 0; + int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, + pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask + ); + assert( pWInfo->sorted==0 ); + if( nOrder==pWInfo->pOrderBy->nExpr ){ + pWInfo->sorted = 1; + pWInfo->revMask = revMask; + } + } + } + + + pWInfo->nRowOut = pFrom->nRow; + + /* Free temporary memory and return success */ + sqlite3DbFree(db, pSpace); + return SQLITE_OK; +} + +/* +** Most queries use only a single table (they are not joins) and have +** simple == constraints against indexed fields. This routine attempts +** to plan those simple cases using much less ceremony than the +** general-purpose query planner, and thereby yield faster sqlite3_prepare() +** times for the common case. +** +** Return non-zero on success, if this query can be handled by this +** no-frills query planner. Return zero if this query needs the +** general-purpose query planner. +*/ +static int whereShortCut(WhereLoopBuilder *pBuilder){ + WhereInfo *pWInfo; + struct SrcList_item *pItem; + WhereClause *pWC; + WhereTerm *pTerm; + WhereLoop *pLoop; + int iCur; + int j; + Table *pTab; + Index *pIdx; + + pWInfo = pBuilder->pWInfo; + if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; + assert( pWInfo->pTabList->nSrc>=1 ); + pItem = pWInfo->pTabList->a; + pTab = pItem->pTab; + if( IsVirtual(pTab) ) return 0; + if( pItem->fg.isIndexedBy ) return 0; + iCur = pItem->iCursor; + pWC = &pWInfo->sWC; + pLoop = pBuilder->pNew; + pLoop->wsFlags = 0; + pLoop->nSkip = 0; + pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); + if( pTerm ){ + testcase( pTerm->eOperator & WO_IS ); + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; + pLoop->aLTerm[0] = pTerm; + pLoop->nLTerm = 1; + pLoop->u.btree.nEq = 1; + /* TUNING: Cost of a rowid lookup is 10 */ + pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ + }else{ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int opMask; + assert( pLoop->aLTermSpace==pLoop->aLTerm ); + if( !IsUniqueIndex(pIdx) + || pIdx->pPartIdxWhere!=0 + || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) + ) continue; + opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; + for(j=0; jnKeyCol; j++){ + pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx); + if( pTerm==0 ) break; + testcase( pTerm->eOperator & WO_IS ); + pLoop->aLTerm[j] = pTerm; + } + if( j!=pIdx->nKeyCol ) continue; + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; + if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ + pLoop->wsFlags |= WHERE_IDX_ONLY; + } + pLoop->nLTerm = j; + pLoop->u.btree.nEq = j; + pLoop->u.btree.pIndex = pIdx; + /* TUNING: Cost of a unique index lookup is 15 */ + pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ + break; + } + } + if( pLoop->wsFlags ){ + pLoop->nOut = (LogEst)1; + pWInfo->a[0].pWLoop = pLoop; + pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); + pWInfo->a[0].iTabCur = iCur; + pWInfo->nRowOut = 1; + if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; + if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; + } +#ifdef SQLITE_DEBUG + pLoop->cId = '0'; +#endif + return 1; + } + return 0; +} + +/* +** Generate the beginning of the loop used for WHERE clause processing. +** The return value is a pointer to an opaque structure that contains +** information needed to terminate the loop. Later, the calling routine +** should invoke sqlite3WhereEnd() with the return value of this function +** in order to complete the WHERE clause processing. +** +** If an error occurs, this routine returns NULL. +** +** The basic idea is to do a nested loop, one loop for each table in +** the FROM clause of a select. (INSERT and UPDATE statements are the +** same as a SELECT with only a single table in the FROM clause.) For +** example, if the SQL is this: +** +** SELECT * FROM t1, t2, t3 WHERE ...; +** +** Then the code generated is conceptually like the following: +** +** foreach row1 in t1 do \ Code generated +** foreach row2 in t2 do |-- by sqlite3WhereBegin() +** foreach row3 in t3 do / +** ... +** end \ Code generated +** end |-- by sqlite3WhereEnd() +** end / +** +** Note that the loops might not be nested in the order in which they +** appear in the FROM clause if a different order is better able to make +** use of indices. Note also that when the IN operator appears in +** the WHERE clause, it might result in additional nested loops for +** scanning through all values on the right-hand side of the IN. +** +** There are Btree cursors associated with each table. t1 uses cursor +** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. +** And so forth. This routine generates code to open those VDBE cursors +** and sqlite3WhereEnd() generates the code to close them. +** +** The code that sqlite3WhereBegin() generates leaves the cursors named +** in pTabList pointing at their appropriate entries. The [...] code +** can use OP_Column and OP_Rowid opcodes on these cursors to extract +** data from the various tables of the loop. +** +** If the WHERE clause is empty, the foreach loops must each scan their +** entire tables. Thus a three-way join is an O(N^3) operation. But if +** the tables have indices and there are terms in the WHERE clause that +** refer to those indices, a complete table scan can be avoided and the +** code will run much faster. Most of the work of this routine is checking +** to see if there are indices that can be used to speed up the loop. +** +** Terms of the WHERE clause are also used to limit which rows actually +** make it to the "..." in the middle of the loop. After each "foreach", +** terms of the WHERE clause that use only terms in that loop and outer +** loops are evaluated and if false a jump is made around all subsequent +** inner loops (or around the "..." if the test occurs within the inner- +** most loop) +** +** OUTER JOINS +** +** An outer join of tables t1 and t2 is conceptally coded as follows: +** +** foreach row1 in t1 do +** flag = 0 +** foreach row2 in t2 do +** start: +** ... +** flag = 1 +** end +** if flag==0 then +** move the row2 cursor to a null row +** goto start +** fi +** end +** +** ORDER BY CLAUSE PROCESSING +** +** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause +** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement +** if there is one. If there is no ORDER BY clause or if this routine +** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. +** +** The iIdxCur parameter is the cursor number of an index. If +** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index +** to use for OR clause processing. The WHERE clause should use this +** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is +** the first cursor in an array of cursors for all indices. iIdxCur should +** be used to compute the appropriate cursor depending on which index is +** used. +*/ +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ + Expr *pWhere, /* The WHERE clause */ + ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ + ExprList *pDistinctSet, /* Try not to output two rows that duplicate these */ + u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ + int iAuxArg /* If WHERE_ONETABLE_ONLY is set, index cursor number + ** If WHERE_USE_LIMIT, then the limit amount */ +){ + int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ + int nTabList; /* Number of elements in pTabList */ + WhereInfo *pWInfo; /* Will become the return value of this function */ + Vdbe *v = pParse->pVdbe; /* The virtual database engine */ + Bitmask notReady; /* Cursors that are not yet positioned */ + WhereLoopBuilder sWLB; /* The WhereLoop builder */ + WhereMaskSet *pMaskSet; /* The expression mask set */ + WhereLevel *pLevel; /* A single level in pWInfo->a[] */ + WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ + int ii; /* Loop counter */ + sqlite3 *db; /* Database connection */ + int rc; /* Return code */ + u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */ + + assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( + (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 + && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 + )); + + /* Only one of WHERE_ONETABLE_ONLY or WHERE_USE_LIMIT */ + assert( (wctrlFlags & WHERE_ONETABLE_ONLY)==0 + || (wctrlFlags & WHERE_USE_LIMIT)==0 ); + + /* Variable initialization */ + db = pParse->db; + memset(&sWLB, 0, sizeof(sWLB)); + + /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ + testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); + if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; + sWLB.pOrderBy = pOrderBy; + + /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via + ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ + if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ + wctrlFlags &= ~WHERE_WANT_DISTINCT; + } + + /* The number of tables in the FROM clause is limited by the number of + ** bits in a Bitmask + */ + testcase( pTabList->nSrc==BMS ); + if( pTabList->nSrc>BMS ){ + sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); + return 0; + } + + /* This function normally generates a nested loop for all tables in + ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should + ** only generate code for the first table in pTabList and assume that + ** any cursors associated with subsequent tables are uninitialized. + */ + nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; + + /* Allocate and initialize the WhereInfo structure that will become the + ** return value. A single allocation is used to store the WhereInfo + ** struct, the contents of WhereInfo.a[], the WhereClause structure + ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte + ** field (type Bitmask) it must be aligned on an 8-byte boundary on + ** some architectures. Hence the ROUND8() below. + */ + nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); + pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); + if( db->mallocFailed ){ + sqlite3DbFree(db, pWInfo); + pWInfo = 0; + goto whereBeginError; + } + pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; + pWInfo->nLevel = nTabList; + pWInfo->pParse = pParse; + pWInfo->pTabList = pTabList; + pWInfo->pOrderBy = pOrderBy; + pWInfo->pDistinctSet = pDistinctSet; + pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); + pWInfo->wctrlFlags = wctrlFlags; + pWInfo->iLimit = iAuxArg; + pWInfo->savedNQueryLoop = pParse->nQueryLoop; + assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ + pMaskSet = &pWInfo->sMaskSet; + sWLB.pWInfo = pWInfo; + sWLB.pWC = &pWInfo->sWC; + sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); + assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); + whereLoopInit(sWLB.pNew); +#ifdef SQLITE_DEBUG + sWLB.pNew->cId = '*'; +#endif + + /* Split the WHERE clause into separate subexpressions where each + ** subexpression is separated by an AND operator. + */ + initMaskSet(pMaskSet); + sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo); + sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND); + + /* Special case: a WHERE clause that is constant. Evaluate the + ** expression and either jump over all of the code or fall thru. + */ + for(ii=0; iinTerm; ii++){ + if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ + sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, + SQLITE_JUMPIFNULL); + sWLB.pWC->a[ii].wtFlags |= TERM_CODED; + } + } + + /* Special case: No FROM clause + */ + if( nTabList==0 ){ + if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; + if( wctrlFlags & WHERE_WANT_DISTINCT ){ + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; + } + } + + /* Assign a bit from the bitmask to every term in the FROM clause. + ** + ** The N-th term of the FROM clause is assigned a bitmask of 1<nSrc tables in + ** pTabList, not just the first nTabList tables. nTabList is normally + ** equal to pTabList->nSrc but might be shortened to 1 if the + ** WHERE_ONETABLE_ONLY flag is set. + */ + for(ii=0; iinSrc; ii++){ + createMask(pMaskSet, pTabList->a[ii].iCursor); + sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); + } +#ifdef SQLITE_DEBUG + for(ii=0; iinSrc; ii++){ + Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); + assert( m==MASKBIT(ii) ); + } +#endif + + /* Analyze all of the subexpressions. */ + sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); + if( db->mallocFailed ) goto whereBeginError; + + if( wctrlFlags & WHERE_WANT_DISTINCT ){ + if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pDistinctSet) ){ + /* The DISTINCT marking is pointless. Ignore it. */ + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; + }else if( pOrderBy==0 ){ + /* Try to ORDER BY the result set to make distinct processing easier */ + pWInfo->wctrlFlags |= WHERE_DISTINCTBY; + pWInfo->pOrderBy = pDistinctSet; + } + } + + /* Construct the WhereLoop objects */ +#if defined(WHERETRACE_ENABLED) + if( sqlite3WhereTrace & 0xffff ){ + sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags); + if( wctrlFlags & WHERE_USE_LIMIT ){ + sqlite3DebugPrintf(", limit: %d", iAuxArg); + } + sqlite3DebugPrintf(")\n"); + } + if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ + int i; + for(i=0; inTerm; i++){ + whereTermPrint(&sWLB.pWC->a[i], i); + } + } +#endif + + if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ + rc = whereLoopAddAll(&sWLB); + if( rc ) goto whereBeginError; + +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ + WhereLoop *p; + int i; + static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" + "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; + for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ + p->cId = zLabel[i%sizeof(zLabel)]; + whereLoopPrint(p, sWLB.pWC); + } + } +#endif + + wherePathSolver(pWInfo, 0); + if( db->mallocFailed ) goto whereBeginError; + if( pWInfo->pOrderBy ){ + wherePathSolver(pWInfo, pWInfo->nRowOut+1); + if( db->mallocFailed ) goto whereBeginError; + } + } + if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ + pWInfo->revMask = ALLBITS; + } + if( pParse->nErr || NEVER(db->mallocFailed) ){ + goto whereBeginError; + } +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace ){ + sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); + if( pWInfo->nOBSat>0 ){ + sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); + } + switch( pWInfo->eDistinct ){ + case WHERE_DISTINCT_UNIQUE: { + sqlite3DebugPrintf(" DISTINCT=unique"); + break; + } + case WHERE_DISTINCT_ORDERED: { + sqlite3DebugPrintf(" DISTINCT=ordered"); + break; + } + case WHERE_DISTINCT_UNORDERED: { + sqlite3DebugPrintf(" DISTINCT=unordered"); + break; + } + } + sqlite3DebugPrintf("\n"); + for(ii=0; iinLevel; ii++){ + whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); + } + } +#endif + /* Attempt to omit tables from the join that do not effect the result */ + if( pWInfo->nLevel>=2 + && pDistinctSet!=0 + && OptimizationEnabled(db, SQLITE_OmitNoopJoin) + ){ + Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pDistinctSet); + if( sWLB.pOrderBy ){ + tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); + } + while( pWInfo->nLevel>=2 ){ + WhereTerm *pTerm, *pEnd; + pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; + if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break; + if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 + && (pLoop->wsFlags & WHERE_ONEROW)==0 + ){ + break; + } + if( (tabUsed & pLoop->maskSelf)!=0 ) break; + pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; + for(pTerm=sWLB.pWC->a; pTermprereqAll & pLoop->maskSelf)!=0 + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) + ){ + break; + } + } + if( pTerm drop loop %c not used\n", pLoop->cId)); + pWInfo->nLevel--; + nTabList--; + } + } + WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); + pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; + + /* If the caller is an UPDATE or DELETE statement that is requesting + ** to use a one-pass algorithm, determine if this is appropriate. + */ + assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); + if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){ + int wsFlags = pWInfo->a[0].pWLoop->wsFlags; + int bOnerow = (wsFlags & WHERE_ONEROW)!=0; + if( bOnerow + || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0 + && 0==(wsFlags & WHERE_VIRTUALTABLE)) + ){ + pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI; + if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){ + if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){ + bFordelete = OPFLAG_FORDELETE; + } + pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY); + } + } + } + + /* Open all tables in the pTabList and any indices selected for + ** searching those tables. + */ + for(ii=0, pLevel=pWInfo->a; iia[pLevel->iFrom]; + pTab = pTabItem->pTab; + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + pLoop = pLevel->pWLoop; + if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ + /* Do nothing */ + }else +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + int iCur = pTabItem->iCursor; + sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); + }else if( IsVirtual(pTab) ){ + /* noop */ + }else +#endif + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 + && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ + int op = OP_OpenRead; + if( pWInfo->eOnePass!=ONEPASS_OFF ){ + op = OP_OpenWrite; + pWInfo->aiCurOnePass[0] = pTabItem->iCursor; + }; + sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); + assert( pTabItem->iCursor==pLevel->iTabCur ); + testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); + testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); + if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nColcolUsed; + int n = 0; + for(; b; b=b>>1, n++){} + sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32); + assert( n<=pTab->nCol ); + } +#ifdef SQLITE_ENABLE_CURSOR_HINTS + if( pLoop->u.btree.pIndex!=0 ){ + sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); + }else +#endif + { + sqlite3VdbeChangeP5(v, bFordelete); + } +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK + sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, + (const u8*)&pTabItem->colUsed, P4_INT64); +#endif + }else{ + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + } + if( pLoop->wsFlags & WHERE_INDEXED ){ + Index *pIx = pLoop->u.btree.pIndex; + int iIndexCur; + int op = OP_OpenRead; + /* iAuxArg is always set if to a positive value if ONEPASS is possible */ + assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); + if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) + && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 + ){ + /* This is one term of an OR-optimization using the PRIMARY KEY of a + ** WITHOUT ROWID table. No need for a separate index */ + iIndexCur = pLevel->iTabCur; + op = 0; + }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ + Index *pJ = pTabItem->pTab->pIndex; + iIndexCur = iAuxArg; + assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); + while( ALWAYS(pJ) && pJ!=pIx ){ + iIndexCur++; + pJ = pJ->pNext; + } + op = OP_OpenWrite; + pWInfo->aiCurOnePass[1] = iIndexCur; + }else if( iAuxArg && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ + iIndexCur = iAuxArg; + if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; + }else{ + iIndexCur = pParse->nTab++; + } + pLevel->iIdxCur = iIndexCur; + assert( pIx->pSchema==pTab->pSchema ); + assert( iIndexCur>=0 ); + if( op ){ + sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIx); + if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 + && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 + && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 + ){ + sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ + } + VdbeComment((v, "%s", pIx->zName)); +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK + { + u64 colUsed = 0; + int ii, jj; + for(ii=0; iinColumn; ii++){ + jj = pIx->aiColumn[ii]; + if( jj<0 ) continue; + if( jj>63 ) jj = 63; + if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue; + colUsed |= ((u64)1)<<(ii<63 ? ii : 63); + } + sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0, + (u8*)&colUsed, P4_INT64); + } +#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */ + } + } + if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); + } + pWInfo->iTop = sqlite3VdbeCurrentAddr(v); + if( db->mallocFailed ) goto whereBeginError; + + /* Generate the code to do the search. Each iteration of the for + ** loop below generates code for a single nested loop of the VM + ** program. + */ + notReady = ~(Bitmask)0; + for(ii=0; iia[ii]; + wsFlags = pLevel->pWLoop->wsFlags; +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX + if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ + constructAutomaticIndex(pParse, &pWInfo->sWC, + &pTabList->a[pLevel->iFrom], notReady, pLevel); + if( db->mallocFailed ) goto whereBeginError; + } +#endif + addrExplain = sqlite3WhereExplainOneScan( + pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags + ); + pLevel->addrBody = sqlite3VdbeCurrentAddr(v); + notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); + pWInfo->iContinue = pLevel->addrCont; + if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ + sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); + } + } + + /* Done. */ + VdbeModuleComment((v, "Begin WHERE-core")); + return pWInfo; + + /* Jump here if malloc fails */ +whereBeginError: + if( pWInfo ){ + pParse->nQueryLoop = pWInfo->savedNQueryLoop; + whereInfoFree(db, pWInfo); + } + return 0; +} + +/* +** Generate the end of the WHERE loop. See comments on +** sqlite3WhereBegin() for additional information. +*/ +SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ + Parse *pParse = pWInfo->pParse; + Vdbe *v = pParse->pVdbe; + int i; + WhereLevel *pLevel; + WhereLoop *pLoop; + SrcList *pTabList = pWInfo->pTabList; + sqlite3 *db = pParse->db; + + /* Generate loop termination code. + */ + VdbeModuleComment((v, "End WHERE-core")); + sqlite3ExprCacheClear(pParse); + for(i=pWInfo->nLevel-1; i>=0; i--){ + int addr; + pLevel = &pWInfo->a[i]; + pLoop = pLevel->pWLoop; + sqlite3VdbeResolveLabel(v, pLevel->addrCont); + if( pLevel->op!=OP_Noop ){ + sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); + sqlite3VdbeChangeP5(v, pLevel->p5); + VdbeCoverage(v); + VdbeCoverageIf(v, pLevel->op==OP_Next); + VdbeCoverageIf(v, pLevel->op==OP_Prev); + VdbeCoverageIf(v, pLevel->op==OP_VNext); + } + if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ + struct InLoop *pIn; + int j; + sqlite3VdbeResolveLabel(v, pLevel->addrNxt); + for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ + sqlite3VdbeJumpHere(v, pIn->addrInTop+1); + sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); + VdbeCoverage(v); + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); + sqlite3VdbeJumpHere(v, pIn->addrInTop-1); + } + } + sqlite3VdbeResolveLabel(v, pLevel->addrBrk); + if( pLevel->addrSkip ){ + sqlite3VdbeGoto(v, pLevel->addrSkip); + VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); + sqlite3VdbeJumpHere(v, pLevel->addrSkip); + sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); + } +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS + if( pLevel->addrLikeRep ){ + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1), + pLevel->addrLikeRep); + VdbeCoverage(v); + } +#endif + if( pLevel->iLeftJoin ){ + addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 + || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ + sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); + } + if( pLoop->wsFlags & WHERE_INDEXED ){ + sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); + } + if( pLevel->op==OP_Return ){ + sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); + }else{ + sqlite3VdbeGoto(v, pLevel->addrFirst); + } + sqlite3VdbeJumpHere(v, addr); + } + VdbeModuleComment((v, "End WHERE-loop%d: %s", i, + pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); + } + + /* The "break" point is here, just past the end of the outer loop. + ** Set it. + */ + sqlite3VdbeResolveLabel(v, pWInfo->iBreak); + + assert( pWInfo->nLevel<=pTabList->nSrc ); + for(i=0, pLevel=pWInfo->a; inLevel; i++, pLevel++){ + int k, last; + VdbeOp *pOp; + Index *pIdx = 0; + struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; + Table *pTab = pTabItem->pTab; + assert( pTab!=0 ); + pLoop = pLevel->pWLoop; + + /* For a co-routine, change all OP_Column references to the table of + ** the co-routine into OP_Copy of result contained in a register. + ** OP_Rowid becomes OP_Null. + */ + if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){ + translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, + pTabItem->regResult, 0); + continue; + } + + /* Close all of the cursors that were opened by sqlite3WhereBegin. + ** Except, do not close cursors that will be reused by the OR optimization + ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors + ** created for the ONEPASS optimization. + */ + if( (pTab->tabFlags & TF_Ephemeral)==0 + && pTab->pSelect==0 + && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 + ){ + int ws = pLoop->wsFlags; + if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){ + sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); + } + if( (ws & WHERE_INDEXED)!=0 + && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 + && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] + ){ + sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); + } + } + + /* If this scan uses an index, make VDBE code substitutions to read data + ** from the index instead of from the table where possible. In some cases + ** this optimization prevents the table from ever being read, which can + ** yield a significant performance boost. + ** + ** Calls to the code generator in between sqlite3WhereBegin and + ** sqlite3WhereEnd will have created code that references the table + ** directly. This loop scans all that code looking for opcodes + ** that reference the table and converts them into opcodes that + ** reference the index. + */ + if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ + pIdx = pLoop->u.btree.pIndex; + }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ + pIdx = pLevel->u.pCovidx; + } + if( pIdx + && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable)) + && !db->mallocFailed + ){ + last = sqlite3VdbeCurrentAddr(v); + k = pLevel->addrBody; + pOp = sqlite3VdbeGetOp(v, k); + for(; kp1!=pLevel->iTabCur ) continue; + if( pOp->opcode==OP_Column ){ + int x = pOp->p2; + assert( pIdx->pTable==pTab ); + if( !HasRowid(pTab) ){ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + x = pPk->aiColumn[x]; + assert( x>=0 ); + } + x = sqlite3ColumnOfIndex(pIdx, x); + if( x>=0 ){ + pOp->p2 = x; + pOp->p1 = pLevel->iIdxCur; + } + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); + }else if( pOp->opcode==OP_Rowid ){ + pOp->p1 = pLevel->iIdxCur; + pOp->opcode = OP_IdxRowid; + } + } + } + } + + /* Final cleanup + */ + pParse->nQueryLoop = pWInfo->savedNQueryLoop; + whereInfoFree(db, pWInfo); + return; +} + +/************** End of where.c ***********************************************/ +/************** Begin file parse.c *******************************************/ +/* +** 2000-05-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Driver template for the LEMON parser generator. +** +** The "lemon" program processes an LALR(1) input grammar file, then uses +** this template to construct a parser. The "lemon" program inserts text +** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the +** interstitial "-" characters) contained in this template is changed into +** the value of the %name directive from the grammar. Otherwise, the content +** of this template is copied straight through into the generate parser +** source file. +** +** The following is the concatenation of all %include directives from the +** input grammar file: +*/ +/* #include */ +/************ Begin %include sections from the grammar ************************/ + +/* #include "sqliteInt.h" */ + +/* +** Disable all error recovery processing in the parser push-down +** automaton. +*/ +#define YYNOERRORRECOVERY 1 + +/* +** Make yytestcase() the same as testcase() +*/ +#define yytestcase(X) testcase(X) + +/* +** Indicate that sqlite3ParserFree() will never be called with a null +** pointer. +*/ +#define YYPARSEFREENEVERNULL 1 + +/* +** Alternative datatype for the argument to the malloc() routine passed +** into sqlite3ParserAlloc(). The default is size_t. +*/ +#define YYMALLOCARGTYPE u64 + +/* +** An instance of this structure holds information about the +** LIMIT clause of a SELECT statement. +*/ +struct LimitVal { + Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ + Expr *pOffset; /* The OFFSET expression. NULL if there is none */ +}; + +/* +** An instance of this structure is used to store the LIKE, +** GLOB, NOT LIKE, and NOT GLOB operators. +*/ +struct LikeOp { + Token eOperator; /* "like" or "glob" or "regexp" */ + int bNot; /* True if the NOT keyword is present */ +}; + +/* +** An instance of the following structure describes the event of a +** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, +** TK_DELETE, or TK_INSTEAD. If the event is of the form +** +** UPDATE ON (a,b,c) +** +** Then the "b" IdList records the list "a,b,c". +*/ +struct TrigEvent { int a; IdList * b; }; + +/* +** An instance of this structure holds the ATTACH key and the key type. +*/ +struct AttachKey { int type; Token key; }; + +/* +** Disable lookaside memory allocation for objects that might be +** shared across database connections. +*/ +static void disableLookaside(Parse *pParse){ + pParse->disableLookaside++; + pParse->db->lookaside.bDisable++; +} + + + /* + ** For a compound SELECT statement, make sure p->pPrior->pNext==p for + ** all elements in the list. And make sure list length does not exceed + ** SQLITE_LIMIT_COMPOUND_SELECT. + */ + static void parserDoubleLinkSelect(Parse *pParse, Select *p){ + if( p->pPrior ){ + Select *pNext = 0, *pLoop; + int mxSelect, cnt = 0; + for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ + pLoop->pNext = pNext; + pLoop->selFlags |= SF_Compound; + } + if( (p->selFlags & SF_MultiValue)==0 && + (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && + cnt>mxSelect + ){ + sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); + } + } + } + + /* This is a utility routine used to set the ExprSpan.zStart and + ** ExprSpan.zEnd values of pOut so that the span covers the complete + ** range of text beginning with pStart and going to the end of pEnd. + */ + static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){ + pOut->zStart = pStart->z; + pOut->zEnd = &pEnd->z[pEnd->n]; + } + + /* Construct a new Expr object from a single identifier. Use the + ** new Expr to populate pOut. Set the span of pOut to be the identifier + ** that created the expression. + */ + static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){ + pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, &t); + pOut->zStart = t.z; + pOut->zEnd = &t.z[t.n]; + } + + /* This routine constructs a binary expression node out of two ExprSpan + ** objects and uses the result to populate a new ExprSpan object. + */ + static void spanBinaryExpr( + Parse *pParse, /* The parsing context. Errors accumulate here */ + int op, /* The binary operation */ + ExprSpan *pLeft, /* The left operand, and output */ + ExprSpan *pRight /* The right operand */ + ){ + pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0); + pLeft->zEnd = pRight->zEnd; + } + + /* If doNot is true, then add a TK_NOT Expr-node wrapper around the + ** outside of *ppExpr. + */ + static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){ + if( doNot ){ + pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0, 0); + } + } + + /* Construct an expression node for a unary postfix operator + */ + static void spanUnaryPostfix( + Parse *pParse, /* Parsing context to record errors */ + int op, /* The operator */ + ExprSpan *pOperand, /* The operand, and output */ + Token *pPostOp /* The operand token for setting the span */ + ){ + pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); + pOperand->zEnd = &pPostOp->z[pPostOp->n]; + } + + /* A routine to convert a binary TK_IS or TK_ISNOT expression into a + ** unary TK_ISNULL or TK_NOTNULL expression. */ + static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ + sqlite3 *db = pParse->db; + if( pA && pY && pY->op==TK_NULL ){ + pA->op = (u8)op; + sqlite3ExprDelete(db, pA->pRight); + pA->pRight = 0; + } + } + + /* Construct an expression node for a unary prefix operator + */ + static void spanUnaryPrefix( + ExprSpan *pOut, /* Write the new expression node here */ + Parse *pParse, /* Parsing context to record errors */ + int op, /* The operator */ + ExprSpan *pOperand, /* The operand */ + Token *pPreOp /* The operand token for setting the span */ + ){ + pOut->zStart = pPreOp->z; + pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); + pOut->zEnd = pOperand->zEnd; + } + + /* Add a single new term to an ExprList that is used to store a + ** list of identifiers. Report an error if the ID list contains + ** a COLLATE clause or an ASC or DESC keyword, except ignore the + ** error while parsing a legacy schema. + */ + static ExprList *parserAddExprIdListTerm( + Parse *pParse, + ExprList *pPrior, + Token *pIdToken, + int hasCollate, + int sortOrder + ){ + ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); + if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) + && pParse->db->init.busy==0 + ){ + sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", + pIdToken->n, pIdToken->z); + } + sqlite3ExprListSetName(pParse, p, pIdToken, 1); + return p; + } +/**************** End of %include directives **********************************/ +/* These constants specify the various numeric values for terminal symbols +** in a format understandable to "makeheaders". This section is blank unless +** "lemon" is run with the "-m" command-line option. +***************** Begin makeheaders token definitions *************************/ +/**************** End makeheaders token definitions ***************************/ + +/* The next sections is a series of control #defines. +** various aspects of the generated parser. +** YYCODETYPE is the data type used to store the integer codes +** that represent terminal and non-terminal symbols. +** "unsigned char" is used if there are fewer than +** 256 symbols. Larger types otherwise. +** YYNOCODE is a number of type YYCODETYPE that is not used for +** any terminal or nonterminal symbol. +** YYFALLBACK If defined, this indicates that one or more tokens +** (also known as: "terminal symbols") have fall-back +** values which should be used if the original symbol +** would not parse. This permits keywords to sometimes +** be used as identifiers, for example. +** YYACTIONTYPE is the data type used for "action codes" - numbers +** that indicate what to do in response to the next +** token. +** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal +** symbols. Background: A "minor type" is a semantic +** value associated with a terminal or non-terminal +** symbols. For example, for an "ID" terminal symbol, +** the minor type might be the name of the identifier. +** Each non-terminal can have a different minor type. +** Terminal symbols all have the same minor type, though. +** This macros defines the minor type for terminal +** symbols. +** YYMINORTYPE is the data type used for all minor types. +** This is typically a union of many types, one of +** which is sqlite3ParserTOKENTYPE. The entry in the union +** for terminal symbols is called "yy0". +** YYSTACKDEPTH is the maximum depth of the parser's stack. If +** zero the stack is dynamically sized using realloc() +** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument +** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument +** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser +** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser +** YYERRORSYMBOL is the code number of the error symbol. If not +** defined, then do no error processing. +** YYNSTATE the combined number of states. +** YYNRULE the number of rules in the grammar +** YY_MAX_SHIFT Maximum value for shift actions +** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions +** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions +** YY_MIN_REDUCE Maximum value for reduce actions +** YY_ERROR_ACTION The yy_action[] code for syntax error +** YY_ACCEPT_ACTION The yy_action[] code for accept +** YY_NO_ACTION The yy_action[] code for no-op +*/ +#ifndef INTERFACE +# define INTERFACE 1 +#endif +/************* Begin control #defines *****************************************/ +#define YYCODETYPE unsigned char +#define YYNOCODE 251 +#define YYACTIONTYPE unsigned short int +#define YYWILDCARD 96 +#define sqlite3ParserTOKENTYPE Token +typedef union { + int yyinit; + sqlite3ParserTOKENTYPE yy0; + struct LimitVal yy64; + Expr* yy122; + Select* yy159; + IdList* yy180; + struct {int value; int mask;} yy207; + struct LikeOp yy318; + TriggerStep* yy327; + With* yy331; + ExprSpan yy342; + SrcList* yy347; + int yy392; + struct TrigEvent yy410; + ExprList* yy442; +} YYMINORTYPE; +#ifndef YYSTACKDEPTH +#define YYSTACKDEPTH 100 +#endif +#define sqlite3ParserARG_SDECL Parse *pParse; +#define sqlite3ParserARG_PDECL ,Parse *pParse +#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse +#define sqlite3ParserARG_STORE yypParser->pParse = pParse +#define YYFALLBACK 1 +#define YYNSTATE 440 +#define YYNRULE 326 +#define YY_MAX_SHIFT 439 +#define YY_MIN_SHIFTREDUCE 649 +#define YY_MAX_SHIFTREDUCE 974 +#define YY_MIN_REDUCE 975 +#define YY_MAX_REDUCE 1300 +#define YY_ERROR_ACTION 1301 +#define YY_ACCEPT_ACTION 1302 +#define YY_NO_ACTION 1303 +/************* End control #defines *******************************************/ + +/* Define the yytestcase() macro to be a no-op if is not already defined +** otherwise. +** +** Applications can choose to define yytestcase() in the %include section +** to a macro that can assist in verifying code coverage. For production +** code the yytestcase() macro should be turned off. But it is useful +** for testing. +*/ +#ifndef yytestcase +# define yytestcase(X) +#endif + + +/* Next are the tables used to determine what action to take based on the +** current state and lookahead token. These tables are used to implement +** functions that take a state number and lookahead value and return an +** action integer. +** +** Suppose the action integer is N. Then the action is determined as +** follows +** +** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead +** token onto the stack and goto state N. +** +** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then +** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. +** +** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE +** and YY_MAX_REDUCE + +** N == YY_ERROR_ACTION A syntax error has occurred. +** +** N == YY_ACCEPT_ACTION The parser accepts its input. +** +** N == YY_NO_ACTION No such action. Denotes unused +** slots in the yy_action[] table. +** +** The action table is constructed as a single large table named yy_action[]. +** Given state S and lookahead X, the action is computed as +** +** yy_action[ yy_shift_ofst[S] + X ] +** +** If the index value yy_shift_ofst[S]+X is out of range or if the value +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] +** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table +** and that yy_default[S] should be used instead. +** +** The formula above is for computing the action when the lookahead is +** a terminal symbol. If the lookahead is a non-terminal (as occurs after +** a reduce action) then the yy_reduce_ofst[] array is used in place of +** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of +** YY_SHIFT_USE_DFLT. +** +** The following are the tables generated in this section: +** +** yy_action[] A single table containing all actions. +** yy_lookahead[] A table containing the lookahead for each entry in +** yy_action. Used to detect hash collisions. +** yy_shift_ofst[] For each state, the offset into yy_action for +** shifting terminals. +** yy_reduce_ofst[] For each state, the offset into yy_action for +** shifting non-terminals after a reduce. +** yy_default[] Default action for each state. +** +*********** Begin parsing tables **********************************************/ +#define YY_ACTTAB_COUNT (1501) +static const YYACTIONTYPE yy_action[] = { + /* 0 */ 315, 810, 339, 804, 5, 194, 194, 798, 92, 93, + /* 10 */ 83, 819, 819, 831, 834, 823, 823, 90, 90, 91, + /* 20 */ 91, 91, 91, 290, 89, 89, 89, 89, 88, 88, + /* 30 */ 87, 87, 87, 86, 339, 315, 952, 952, 803, 803, + /* 40 */ 803, 922, 342, 92, 93, 83, 819, 819, 831, 834, + /* 50 */ 823, 823, 90, 90, 91, 91, 91, 91, 123, 89, + /* 60 */ 89, 89, 89, 88, 88, 87, 87, 87, 86, 339, + /* 70 */ 88, 88, 87, 87, 87, 86, 339, 772, 952, 952, + /* 80 */ 315, 87, 87, 87, 86, 339, 773, 68, 92, 93, + /* 90 */ 83, 819, 819, 831, 834, 823, 823, 90, 90, 91, + /* 100 */ 91, 91, 91, 434, 89, 89, 89, 89, 88, 88, + /* 110 */ 87, 87, 87, 86, 339, 1302, 146, 921, 2, 315, + /* 120 */ 427, 24, 679, 953, 48, 86, 339, 92, 93, 83, + /* 130 */ 819, 819, 831, 834, 823, 823, 90, 90, 91, 91, + /* 140 */ 91, 91, 94, 89, 89, 89, 89, 88, 88, 87, + /* 150 */ 87, 87, 86, 339, 933, 933, 315, 259, 412, 398, + /* 160 */ 396, 57, 733, 733, 92, 93, 83, 819, 819, 831, + /* 170 */ 834, 823, 823, 90, 90, 91, 91, 91, 91, 56, + /* 180 */ 89, 89, 89, 89, 88, 88, 87, 87, 87, 86, + /* 190 */ 339, 315, 1245, 922, 342, 268, 934, 935, 241, 92, + /* 200 */ 93, 83, 819, 819, 831, 834, 823, 823, 90, 90, + /* 210 */ 91, 91, 91, 91, 291, 89, 89, 89, 89, 88, + /* 220 */ 88, 87, 87, 87, 86, 339, 315, 913, 1295, 682, + /* 230 */ 687, 1295, 233, 397, 92, 93, 83, 819, 819, 831, + /* 240 */ 834, 823, 823, 90, 90, 91, 91, 91, 91, 326, + /* 250 */ 89, 89, 89, 89, 88, 88, 87, 87, 87, 86, + /* 260 */ 339, 315, 85, 82, 168, 680, 431, 938, 939, 92, + /* 270 */ 93, 83, 819, 819, 831, 834, 823, 823, 90, 90, + /* 280 */ 91, 91, 91, 91, 291, 89, 89, 89, 89, 88, + /* 290 */ 88, 87, 87, 87, 86, 339, 315, 319, 913, 1296, + /* 300 */ 797, 911, 1296, 681, 92, 93, 83, 819, 819, 831, + /* 310 */ 834, 823, 823, 90, 90, 91, 91, 91, 91, 335, + /* 320 */ 89, 89, 89, 89, 88, 88, 87, 87, 87, 86, + /* 330 */ 339, 315, 876, 876, 373, 85, 82, 168, 944, 92, + /* 340 */ 93, 83, 819, 819, 831, 834, 823, 823, 90, 90, + /* 350 */ 91, 91, 91, 91, 896, 89, 89, 89, 89, 88, + /* 360 */ 88, 87, 87, 87, 86, 339, 315, 370, 307, 973, + /* 370 */ 367, 1, 911, 433, 92, 93, 83, 819, 819, 831, + /* 380 */ 834, 823, 823, 90, 90, 91, 91, 91, 91, 189, + /* 390 */ 89, 89, 89, 89, 88, 88, 87, 87, 87, 86, + /* 400 */ 339, 315, 720, 948, 933, 933, 149, 718, 948, 92, + /* 410 */ 93, 83, 819, 819, 831, 834, 823, 823, 90, 90, + /* 420 */ 91, 91, 91, 91, 434, 89, 89, 89, 89, 88, + /* 430 */ 88, 87, 87, 87, 86, 339, 338, 938, 939, 947, + /* 440 */ 694, 940, 974, 315, 953, 48, 934, 935, 715, 689, + /* 450 */ 71, 92, 93, 83, 819, 819, 831, 834, 823, 823, + /* 460 */ 90, 90, 91, 91, 91, 91, 320, 89, 89, 89, + /* 470 */ 89, 88, 88, 87, 87, 87, 86, 339, 315, 412, + /* 480 */ 403, 820, 820, 832, 835, 74, 92, 81, 83, 819, + /* 490 */ 819, 831, 834, 823, 823, 90, 90, 91, 91, 91, + /* 500 */ 91, 698, 89, 89, 89, 89, 88, 88, 87, 87, + /* 510 */ 87, 86, 339, 315, 259, 654, 655, 656, 393, 111, + /* 520 */ 331, 153, 93, 83, 819, 819, 831, 834, 823, 823, + /* 530 */ 90, 90, 91, 91, 91, 91, 434, 89, 89, 89, + /* 540 */ 89, 88, 88, 87, 87, 87, 86, 339, 315, 188, + /* 550 */ 187, 186, 824, 937, 328, 219, 953, 48, 83, 819, + /* 560 */ 819, 831, 834, 823, 823, 90, 90, 91, 91, 91, + /* 570 */ 91, 956, 89, 89, 89, 89, 88, 88, 87, 87, + /* 580 */ 87, 86, 339, 79, 429, 738, 3, 1174, 955, 348, + /* 590 */ 737, 332, 792, 933, 933, 937, 79, 429, 730, 3, + /* 600 */ 203, 160, 278, 391, 273, 390, 190, 892, 434, 400, + /* 610 */ 741, 76, 77, 271, 287, 253, 353, 242, 78, 340, + /* 620 */ 340, 85, 82, 168, 76, 77, 233, 397, 953, 48, + /* 630 */ 432, 78, 340, 340, 277, 934, 935, 185, 439, 651, + /* 640 */ 388, 385, 384, 432, 234, 276, 107, 418, 349, 337, + /* 650 */ 336, 383, 893, 728, 215, 949, 123, 971, 308, 810, + /* 660 */ 418, 436, 435, 412, 394, 798, 400, 873, 894, 123, + /* 670 */ 721, 872, 810, 889, 436, 435, 215, 949, 798, 351, + /* 680 */ 722, 697, 380, 434, 771, 371, 22, 434, 400, 79, + /* 690 */ 429, 232, 3, 189, 413, 870, 803, 803, 803, 805, + /* 700 */ 18, 54, 148, 953, 48, 956, 113, 953, 9, 803, + /* 710 */ 803, 803, 805, 18, 310, 123, 748, 76, 77, 742, + /* 720 */ 123, 325, 955, 866, 78, 340, 340, 113, 350, 359, + /* 730 */ 85, 82, 168, 343, 960, 960, 432, 770, 412, 414, + /* 740 */ 407, 23, 1240, 1240, 79, 429, 357, 3, 166, 91, + /* 750 */ 91, 91, 91, 418, 89, 89, 89, 89, 88, 88, + /* 760 */ 87, 87, 87, 86, 339, 810, 434, 436, 435, 792, + /* 770 */ 320, 798, 76, 77, 789, 271, 123, 434, 360, 78, + /* 780 */ 340, 340, 864, 85, 82, 168, 953, 9, 395, 743, + /* 790 */ 360, 432, 253, 358, 252, 933, 933, 953, 30, 889, + /* 800 */ 327, 216, 803, 803, 803, 805, 18, 113, 418, 89, + /* 810 */ 89, 89, 89, 88, 88, 87, 87, 87, 86, 339, + /* 820 */ 810, 113, 436, 435, 792, 185, 798, 288, 388, 385, + /* 830 */ 384, 123, 113, 920, 2, 796, 696, 934, 935, 383, + /* 840 */ 69, 429, 434, 3, 218, 110, 738, 253, 358, 252, + /* 850 */ 434, 737, 933, 933, 892, 359, 222, 803, 803, 803, + /* 860 */ 805, 18, 953, 47, 933, 933, 933, 933, 76, 77, + /* 870 */ 953, 9, 366, 904, 217, 78, 340, 340, 677, 305, + /* 880 */ 304, 303, 206, 301, 224, 259, 664, 432, 337, 336, + /* 890 */ 434, 228, 247, 144, 934, 935, 933, 933, 667, 893, + /* 900 */ 324, 1259, 96, 434, 418, 796, 934, 935, 934, 935, + /* 910 */ 953, 48, 401, 148, 289, 894, 810, 417, 436, 435, + /* 920 */ 677, 759, 798, 953, 9, 314, 220, 162, 161, 170, + /* 930 */ 402, 239, 953, 8, 194, 683, 683, 410, 934, 935, + /* 940 */ 238, 959, 933, 933, 225, 408, 945, 365, 957, 212, + /* 950 */ 958, 172, 757, 803, 803, 803, 805, 18, 173, 365, + /* 960 */ 176, 123, 171, 113, 244, 952, 246, 434, 356, 796, + /* 970 */ 372, 365, 236, 960, 960, 810, 290, 804, 191, 165, + /* 980 */ 852, 798, 259, 316, 934, 935, 237, 953, 34, 404, + /* 990 */ 91, 91, 91, 91, 84, 89, 89, 89, 89, 88, + /* 1000 */ 88, 87, 87, 87, 86, 339, 701, 952, 434, 240, + /* 1010 */ 347, 758, 803, 803, 803, 434, 245, 1179, 434, 389, + /* 1020 */ 434, 376, 434, 895, 167, 434, 405, 702, 953, 35, + /* 1030 */ 673, 321, 221, 434, 333, 953, 11, 434, 953, 26, + /* 1040 */ 953, 36, 953, 37, 251, 953, 38, 434, 259, 434, + /* 1050 */ 757, 434, 329, 953, 27, 434, 223, 953, 28, 434, + /* 1060 */ 690, 434, 67, 434, 65, 434, 862, 953, 39, 953, + /* 1070 */ 40, 953, 41, 423, 434, 953, 10, 434, 772, 953, + /* 1080 */ 42, 953, 98, 953, 43, 953, 44, 773, 434, 346, + /* 1090 */ 434, 75, 434, 73, 953, 31, 434, 953, 45, 434, + /* 1100 */ 259, 434, 690, 434, 757, 434, 887, 434, 953, 46, + /* 1110 */ 953, 32, 953, 115, 434, 266, 953, 116, 951, 953, + /* 1120 */ 117, 953, 52, 953, 33, 953, 99, 953, 49, 726, + /* 1130 */ 434, 909, 434, 19, 953, 100, 434, 344, 434, 113, + /* 1140 */ 434, 258, 692, 434, 259, 434, 670, 434, 20, 434, + /* 1150 */ 953, 101, 953, 97, 434, 259, 953, 114, 953, 112, + /* 1160 */ 953, 105, 113, 953, 104, 953, 102, 953, 103, 953, + /* 1170 */ 51, 434, 148, 434, 953, 53, 167, 434, 259, 113, + /* 1180 */ 300, 307, 912, 363, 311, 860, 248, 261, 209, 264, + /* 1190 */ 416, 953, 50, 953, 25, 420, 727, 953, 29, 430, + /* 1200 */ 321, 424, 757, 428, 322, 124, 1269, 214, 165, 710, + /* 1210 */ 859, 908, 806, 794, 309, 158, 193, 361, 254, 723, + /* 1220 */ 364, 67, 381, 269, 735, 199, 67, 70, 113, 700, + /* 1230 */ 699, 707, 708, 884, 113, 766, 113, 855, 193, 883, + /* 1240 */ 199, 869, 869, 675, 868, 868, 109, 368, 255, 260, + /* 1250 */ 263, 280, 859, 265, 806, 974, 267, 711, 695, 272, + /* 1260 */ 764, 282, 795, 284, 150, 744, 755, 415, 292, 293, + /* 1270 */ 802, 678, 672, 661, 660, 662, 927, 6, 306, 386, + /* 1280 */ 352, 786, 243, 250, 886, 362, 163, 286, 419, 298, + /* 1290 */ 930, 159, 968, 196, 126, 903, 901, 965, 55, 58, + /* 1300 */ 323, 275, 857, 136, 147, 694, 856, 121, 65, 354, + /* 1310 */ 355, 379, 175, 61, 151, 369, 180, 871, 375, 129, + /* 1320 */ 257, 756, 210, 181, 145, 131, 132, 377, 262, 663, + /* 1330 */ 133, 134, 139, 783, 791, 182, 392, 183, 312, 330, + /* 1340 */ 714, 888, 713, 851, 692, 195, 712, 406, 686, 705, + /* 1350 */ 313, 685, 64, 839, 274, 72, 684, 334, 942, 95, + /* 1360 */ 752, 279, 281, 704, 753, 751, 422, 283, 411, 750, + /* 1370 */ 426, 66, 204, 409, 21, 285, 928, 669, 437, 205, + /* 1380 */ 207, 208, 438, 658, 657, 652, 118, 108, 119, 226, + /* 1390 */ 650, 341, 157, 235, 169, 345, 106, 734, 790, 296, + /* 1400 */ 294, 295, 120, 297, 867, 865, 127, 128, 130, 724, + /* 1410 */ 229, 174, 249, 882, 137, 230, 138, 135, 885, 231, + /* 1420 */ 59, 60, 177, 881, 7, 178, 12, 179, 256, 874, + /* 1430 */ 140, 193, 962, 374, 141, 152, 666, 378, 276, 184, + /* 1440 */ 270, 122, 142, 382, 387, 62, 13, 14, 703, 63, + /* 1450 */ 125, 317, 318, 227, 809, 808, 837, 732, 15, 164, + /* 1460 */ 736, 4, 765, 211, 399, 213, 192, 143, 760, 70, + /* 1470 */ 67, 16, 17, 838, 836, 891, 841, 890, 198, 197, + /* 1480 */ 917, 154, 421, 923, 918, 155, 200, 977, 425, 840, + /* 1490 */ 156, 201, 807, 676, 80, 302, 299, 977, 202, 1261, + /* 1500 */ 1260, +}; +static const YYCODETYPE yy_lookahead[] = { + /* 0 */ 19, 95, 53, 97, 22, 24, 24, 101, 27, 28, + /* 10 */ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, + /* 20 */ 39, 40, 41, 152, 43, 44, 45, 46, 47, 48, + /* 30 */ 49, 50, 51, 52, 53, 19, 55, 55, 132, 133, + /* 40 */ 134, 1, 2, 27, 28, 29, 30, 31, 32, 33, + /* 50 */ 34, 35, 36, 37, 38, 39, 40, 41, 92, 43, + /* 60 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + /* 70 */ 47, 48, 49, 50, 51, 52, 53, 61, 97, 97, + /* 80 */ 19, 49, 50, 51, 52, 53, 70, 26, 27, 28, + /* 90 */ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, + /* 100 */ 39, 40, 41, 152, 43, 44, 45, 46, 47, 48, + /* 110 */ 49, 50, 51, 52, 53, 144, 145, 146, 147, 19, + /* 120 */ 249, 22, 172, 172, 173, 52, 53, 27, 28, 29, + /* 130 */ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + /* 140 */ 40, 41, 81, 43, 44, 45, 46, 47, 48, 49, + /* 150 */ 50, 51, 52, 53, 55, 56, 19, 152, 207, 208, + /* 160 */ 115, 24, 117, 118, 27, 28, 29, 30, 31, 32, + /* 170 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 79, + /* 180 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + /* 190 */ 53, 19, 0, 1, 2, 23, 97, 98, 193, 27, + /* 200 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, + /* 210 */ 38, 39, 40, 41, 152, 43, 44, 45, 46, 47, + /* 220 */ 48, 49, 50, 51, 52, 53, 19, 22, 23, 172, + /* 230 */ 23, 26, 119, 120, 27, 28, 29, 30, 31, 32, + /* 240 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 187, + /* 250 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + /* 260 */ 53, 19, 221, 222, 223, 23, 168, 169, 170, 27, + /* 270 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, + /* 280 */ 38, 39, 40, 41, 152, 43, 44, 45, 46, 47, + /* 290 */ 48, 49, 50, 51, 52, 53, 19, 157, 22, 23, + /* 300 */ 23, 96, 26, 172, 27, 28, 29, 30, 31, 32, + /* 310 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 187, + /* 320 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + /* 330 */ 53, 19, 108, 109, 110, 221, 222, 223, 185, 27, + /* 340 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, + /* 350 */ 38, 39, 40, 41, 240, 43, 44, 45, 46, 47, + /* 360 */ 48, 49, 50, 51, 52, 53, 19, 227, 22, 23, + /* 370 */ 230, 22, 96, 152, 27, 28, 29, 30, 31, 32, + /* 380 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 30, + /* 390 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + /* 400 */ 53, 19, 190, 191, 55, 56, 24, 190, 191, 27, + /* 410 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, + /* 420 */ 38, 39, 40, 41, 152, 43, 44, 45, 46, 47, + /* 430 */ 48, 49, 50, 51, 52, 53, 168, 169, 170, 179, + /* 440 */ 180, 171, 96, 19, 172, 173, 97, 98, 188, 179, + /* 450 */ 138, 27, 28, 29, 30, 31, 32, 33, 34, 35, + /* 460 */ 36, 37, 38, 39, 40, 41, 107, 43, 44, 45, + /* 470 */ 46, 47, 48, 49, 50, 51, 52, 53, 19, 207, + /* 480 */ 208, 30, 31, 32, 33, 138, 27, 28, 29, 30, + /* 490 */ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + /* 500 */ 41, 181, 43, 44, 45, 46, 47, 48, 49, 50, + /* 510 */ 51, 52, 53, 19, 152, 7, 8, 9, 49, 22, + /* 520 */ 19, 24, 28, 29, 30, 31, 32, 33, 34, 35, + /* 530 */ 36, 37, 38, 39, 40, 41, 152, 43, 44, 45, + /* 540 */ 46, 47, 48, 49, 50, 51, 52, 53, 19, 108, + /* 550 */ 109, 110, 101, 55, 53, 193, 172, 173, 29, 30, + /* 560 */ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + /* 570 */ 41, 152, 43, 44, 45, 46, 47, 48, 49, 50, + /* 580 */ 51, 52, 53, 19, 20, 116, 22, 23, 169, 170, + /* 590 */ 121, 207, 85, 55, 56, 97, 19, 20, 195, 22, + /* 600 */ 99, 100, 101, 102, 103, 104, 105, 12, 152, 206, + /* 610 */ 210, 47, 48, 112, 152, 108, 109, 110, 54, 55, + /* 620 */ 56, 221, 222, 223, 47, 48, 119, 120, 172, 173, + /* 630 */ 66, 54, 55, 56, 101, 97, 98, 99, 148, 149, + /* 640 */ 102, 103, 104, 66, 154, 112, 156, 83, 229, 47, + /* 650 */ 48, 113, 57, 163, 194, 195, 92, 246, 247, 95, + /* 660 */ 83, 97, 98, 207, 208, 101, 206, 59, 73, 92, + /* 670 */ 75, 63, 95, 163, 97, 98, 194, 195, 101, 219, + /* 680 */ 85, 181, 19, 152, 175, 77, 196, 152, 206, 19, + /* 690 */ 20, 199, 22, 30, 163, 11, 132, 133, 134, 135, + /* 700 */ 136, 209, 152, 172, 173, 152, 196, 172, 173, 132, + /* 710 */ 133, 134, 135, 136, 164, 92, 213, 47, 48, 49, + /* 720 */ 92, 186, 169, 170, 54, 55, 56, 196, 100, 219, + /* 730 */ 221, 222, 223, 243, 132, 133, 66, 175, 207, 208, + /* 740 */ 152, 231, 119, 120, 19, 20, 236, 22, 152, 38, + /* 750 */ 39, 40, 41, 83, 43, 44, 45, 46, 47, 48, + /* 760 */ 49, 50, 51, 52, 53, 95, 152, 97, 98, 85, + /* 770 */ 107, 101, 47, 48, 163, 112, 92, 152, 152, 54, + /* 780 */ 55, 56, 229, 221, 222, 223, 172, 173, 163, 49, + /* 790 */ 152, 66, 108, 109, 110, 55, 56, 172, 173, 163, + /* 800 */ 186, 22, 132, 133, 134, 135, 136, 196, 83, 43, + /* 810 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + /* 820 */ 95, 196, 97, 98, 85, 99, 101, 152, 102, 103, + /* 830 */ 104, 92, 196, 146, 147, 152, 181, 97, 98, 113, + /* 840 */ 19, 20, 152, 22, 218, 22, 116, 108, 109, 110, + /* 850 */ 152, 121, 55, 56, 12, 219, 218, 132, 133, 134, + /* 860 */ 135, 136, 172, 173, 55, 56, 55, 56, 47, 48, + /* 870 */ 172, 173, 236, 152, 5, 54, 55, 56, 55, 10, + /* 880 */ 11, 12, 13, 14, 186, 152, 17, 66, 47, 48, + /* 890 */ 152, 210, 16, 84, 97, 98, 55, 56, 21, 57, + /* 900 */ 217, 122, 22, 152, 83, 152, 97, 98, 97, 98, + /* 910 */ 172, 173, 152, 152, 224, 73, 95, 75, 97, 98, + /* 920 */ 97, 124, 101, 172, 173, 164, 193, 47, 48, 60, + /* 930 */ 163, 62, 172, 173, 24, 55, 56, 186, 97, 98, + /* 940 */ 71, 100, 55, 56, 183, 207, 185, 152, 107, 23, + /* 950 */ 109, 82, 26, 132, 133, 134, 135, 136, 89, 152, + /* 960 */ 26, 92, 93, 196, 88, 55, 90, 152, 91, 152, + /* 970 */ 217, 152, 152, 132, 133, 95, 152, 97, 211, 212, + /* 980 */ 103, 101, 152, 114, 97, 98, 152, 172, 173, 19, + /* 990 */ 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + /* 1000 */ 48, 49, 50, 51, 52, 53, 65, 97, 152, 152, + /* 1010 */ 141, 124, 132, 133, 134, 152, 140, 140, 152, 78, + /* 1020 */ 152, 233, 152, 193, 98, 152, 56, 86, 172, 173, + /* 1030 */ 166, 167, 237, 152, 217, 172, 173, 152, 172, 173, + /* 1040 */ 172, 173, 172, 173, 237, 172, 173, 152, 152, 152, + /* 1050 */ 124, 152, 111, 172, 173, 152, 237, 172, 173, 152, + /* 1060 */ 55, 152, 26, 152, 130, 152, 152, 172, 173, 172, + /* 1070 */ 173, 172, 173, 249, 152, 172, 173, 152, 61, 172, + /* 1080 */ 173, 172, 173, 172, 173, 172, 173, 70, 152, 193, + /* 1090 */ 152, 137, 152, 139, 172, 173, 152, 172, 173, 152, + /* 1100 */ 152, 152, 97, 152, 26, 152, 163, 152, 172, 173, + /* 1110 */ 172, 173, 172, 173, 152, 16, 172, 173, 26, 172, + /* 1120 */ 173, 172, 173, 172, 173, 172, 173, 172, 173, 163, + /* 1130 */ 152, 152, 152, 22, 172, 173, 152, 241, 152, 196, + /* 1140 */ 152, 193, 106, 152, 152, 152, 163, 152, 37, 152, + /* 1150 */ 172, 173, 172, 173, 152, 152, 172, 173, 172, 173, + /* 1160 */ 172, 173, 196, 172, 173, 172, 173, 172, 173, 172, + /* 1170 */ 173, 152, 152, 152, 172, 173, 98, 152, 152, 196, + /* 1180 */ 160, 22, 23, 19, 164, 193, 152, 88, 232, 90, + /* 1190 */ 191, 172, 173, 172, 173, 163, 193, 172, 173, 166, + /* 1200 */ 167, 163, 124, 163, 244, 245, 23, 211, 212, 26, + /* 1210 */ 55, 23, 55, 23, 26, 123, 26, 152, 23, 193, + /* 1220 */ 56, 26, 23, 23, 23, 26, 26, 26, 196, 100, + /* 1230 */ 101, 7, 8, 152, 196, 23, 196, 23, 26, 152, + /* 1240 */ 26, 132, 133, 23, 132, 133, 26, 152, 152, 152, + /* 1250 */ 152, 210, 97, 152, 97, 96, 152, 152, 152, 152, + /* 1260 */ 152, 210, 152, 210, 197, 152, 152, 152, 152, 152, + /* 1270 */ 152, 152, 152, 152, 152, 152, 152, 198, 150, 176, + /* 1280 */ 214, 201, 214, 238, 201, 238, 184, 214, 226, 200, + /* 1290 */ 155, 198, 67, 122, 242, 159, 159, 69, 239, 239, + /* 1300 */ 159, 175, 175, 22, 220, 180, 175, 27, 130, 18, + /* 1310 */ 159, 18, 158, 137, 220, 159, 158, 235, 74, 189, + /* 1320 */ 234, 159, 159, 158, 22, 192, 192, 177, 159, 159, + /* 1330 */ 192, 192, 189, 201, 189, 158, 107, 158, 177, 76, + /* 1340 */ 174, 201, 174, 201, 106, 159, 174, 125, 174, 182, + /* 1350 */ 177, 176, 107, 159, 174, 137, 174, 53, 174, 129, + /* 1360 */ 216, 215, 215, 182, 216, 216, 177, 215, 126, 216, + /* 1370 */ 177, 128, 25, 127, 26, 215, 13, 162, 161, 153, + /* 1380 */ 153, 6, 151, 151, 151, 151, 165, 178, 165, 178, + /* 1390 */ 4, 3, 22, 142, 15, 94, 16, 205, 120, 202, + /* 1400 */ 204, 203, 165, 201, 23, 23, 131, 111, 123, 20, + /* 1410 */ 225, 125, 16, 1, 131, 228, 111, 123, 56, 228, + /* 1420 */ 37, 37, 64, 1, 5, 122, 22, 107, 140, 80, + /* 1430 */ 80, 26, 87, 72, 107, 24, 20, 19, 112, 105, + /* 1440 */ 23, 68, 22, 79, 79, 22, 22, 22, 58, 22, + /* 1450 */ 245, 248, 248, 79, 23, 23, 23, 116, 22, 122, + /* 1460 */ 23, 22, 56, 23, 26, 23, 64, 22, 124, 26, + /* 1470 */ 26, 64, 64, 23, 23, 23, 11, 23, 22, 26, + /* 1480 */ 23, 22, 24, 1, 23, 22, 26, 250, 24, 23, + /* 1490 */ 22, 122, 23, 23, 22, 15, 23, 250, 122, 122, + /* 1500 */ 122, +}; +#define YY_SHIFT_USE_DFLT (-95) +#define YY_SHIFT_COUNT (439) +#define YY_SHIFT_MIN (-94) +#define YY_SHIFT_MAX (1482) +static const short yy_shift_ofst[] = { + /* 0 */ 40, 564, 869, 577, 725, 725, 725, 739, -19, 16, + /* 10 */ 16, 100, 725, 725, 725, 725, 725, 725, 725, 841, + /* 20 */ 841, 538, 507, 684, 623, 61, 137, 172, 207, 242, + /* 30 */ 277, 312, 347, 382, 424, 424, 424, 424, 424, 424, + /* 40 */ 424, 424, 424, 424, 424, 424, 424, 424, 424, 459, + /* 50 */ 424, 494, 529, 529, 670, 725, 725, 725, 725, 725, + /* 60 */ 725, 725, 725, 725, 725, 725, 725, 725, 725, 725, + /* 70 */ 725, 725, 725, 725, 725, 725, 725, 725, 725, 725, + /* 80 */ 725, 725, 725, 821, 725, 725, 725, 725, 725, 725, + /* 90 */ 725, 725, 725, 725, 725, 725, 725, 952, 711, 711, + /* 100 */ 711, 711, 711, 766, 23, 32, 811, 877, 663, 602, + /* 110 */ 602, 811, 73, 113, -51, -95, -95, -95, 501, 501, + /* 120 */ 501, 595, 595, 809, 205, 276, 811, 811, 811, 811, + /* 130 */ 811, 811, 811, 811, 811, 811, 811, 811, 811, 811, + /* 140 */ 811, 811, 811, 811, 811, 811, 192, 628, 498, 498, + /* 150 */ 113, -34, -34, -34, -34, -34, -34, -95, -95, -95, + /* 160 */ 880, -94, -94, 726, 740, 99, 797, 887, 349, 811, + /* 170 */ 811, 811, 811, 811, 811, 811, 811, 811, 811, 811, + /* 180 */ 811, 811, 811, 811, 811, 811, 941, 941, 941, 811, + /* 190 */ 811, 926, 811, 811, 811, -18, 811, 811, 842, 811, + /* 200 */ 811, 811, 811, 811, 811, 811, 811, 811, 811, 224, + /* 210 */ 608, 910, 910, 910, 1078, 45, 469, 508, 934, 970, + /* 220 */ 970, 1164, 934, 1164, 1036, 1183, 359, 1017, 970, 954, + /* 230 */ 1017, 1017, 1092, 730, 497, 1225, 1171, 1171, 1228, 1228, + /* 240 */ 1171, 1281, 1280, 1178, 1291, 1291, 1291, 1291, 1171, 1293, + /* 250 */ 1178, 1281, 1280, 1280, 1178, 1171, 1293, 1176, 1244, 1171, + /* 260 */ 1171, 1293, 1302, 1171, 1293, 1171, 1293, 1302, 1229, 1229, + /* 270 */ 1229, 1263, 1302, 1229, 1238, 1229, 1263, 1229, 1229, 1222, + /* 280 */ 1245, 1222, 1245, 1222, 1245, 1222, 1245, 1171, 1171, 1218, + /* 290 */ 1302, 1304, 1304, 1302, 1230, 1242, 1243, 1246, 1178, 1347, + /* 300 */ 1348, 1363, 1363, 1375, 1375, 1375, 1375, -95, -95, -95, + /* 310 */ -95, -95, -95, -95, -95, 451, 876, 346, 1159, 1099, + /* 320 */ 441, 823, 1188, 1111, 1190, 1195, 1199, 1200, 1005, 1129, + /* 330 */ 1224, 533, 1201, 1212, 1155, 1214, 1109, 1112, 1220, 1157, + /* 340 */ 779, 1386, 1388, 1370, 1251, 1379, 1301, 1380, 1381, 1382, + /* 350 */ 1278, 1275, 1296, 1285, 1389, 1286, 1396, 1412, 1294, 1283, + /* 360 */ 1383, 1384, 1305, 1362, 1358, 1303, 1422, 1419, 1404, 1320, + /* 370 */ 1288, 1349, 1405, 1350, 1345, 1361, 1327, 1411, 1416, 1418, + /* 380 */ 1326, 1334, 1420, 1364, 1423, 1424, 1417, 1425, 1365, 1390, + /* 390 */ 1427, 1374, 1373, 1431, 1432, 1433, 1341, 1436, 1437, 1439, + /* 400 */ 1438, 1337, 1440, 1442, 1406, 1402, 1445, 1344, 1443, 1407, + /* 410 */ 1444, 1408, 1443, 1450, 1451, 1452, 1453, 1454, 1456, 1465, + /* 420 */ 1457, 1459, 1458, 1460, 1461, 1463, 1464, 1460, 1466, 1468, + /* 430 */ 1469, 1470, 1472, 1369, 1376, 1377, 1378, 1473, 1480, 1482, +}; +#define YY_REDUCE_USE_DFLT (-130) +#define YY_REDUCE_COUNT (314) +#define YY_REDUCE_MIN (-129) +#define YY_REDUCE_MAX (1237) +static const short yy_reduce_ofst[] = { + /* 0 */ -29, 531, 490, 625, -49, 272, 456, 510, 400, 509, + /* 10 */ 562, 114, 535, 614, 698, 384, 738, 751, 690, 419, + /* 20 */ 553, 761, 460, 636, 767, 41, 41, 41, 41, 41, + /* 30 */ 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + /* 40 */ 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + /* 50 */ 41, 41, 41, 41, 760, 815, 856, 863, 866, 868, + /* 60 */ 870, 873, 881, 885, 895, 897, 899, 903, 907, 909, + /* 70 */ 911, 913, 922, 925, 936, 938, 940, 944, 947, 949, + /* 80 */ 951, 953, 955, 962, 978, 980, 984, 986, 988, 991, + /* 90 */ 993, 995, 997, 1002, 1019, 1021, 1025, 41, 41, 41, + /* 100 */ 41, 41, 41, 41, 41, 41, 896, 140, 260, 98, + /* 110 */ 268, 1020, 41, 482, 41, 41, 41, 41, 270, 270, + /* 120 */ 270, 212, 217, -129, 411, 411, 550, 5, 626, 362, + /* 130 */ 733, 830, 992, 1003, 1026, 795, 683, 807, 638, 819, + /* 140 */ 753, 948, 62, 817, 824, 132, 687, 611, 864, 1033, + /* 150 */ 403, 943, 966, 983, 1032, 1038, 1040, 960, 996, 492, + /* 160 */ -50, 57, 131, 153, 221, 462, 588, 596, 675, 721, + /* 170 */ 820, 834, 857, 914, 979, 1034, 1065, 1081, 1087, 1095, + /* 180 */ 1096, 1097, 1098, 1101, 1104, 1105, 320, 500, 655, 1106, + /* 190 */ 1107, 503, 1108, 1110, 1113, 681, 1114, 1115, 999, 1116, + /* 200 */ 1117, 1118, 221, 1119, 1120, 1121, 1122, 1123, 1124, 788, + /* 210 */ 956, 1041, 1051, 1053, 503, 1067, 1079, 1128, 1080, 1066, + /* 220 */ 1068, 1045, 1083, 1047, 1103, 1102, 1125, 1126, 1073, 1062, + /* 230 */ 1127, 1131, 1089, 1093, 1135, 1052, 1136, 1137, 1059, 1060, + /* 240 */ 1141, 1084, 1130, 1132, 1133, 1134, 1138, 1139, 1151, 1154, + /* 250 */ 1140, 1094, 1143, 1145, 1142, 1156, 1158, 1082, 1086, 1162, + /* 260 */ 1163, 1165, 1150, 1169, 1177, 1170, 1179, 1161, 1166, 1168, + /* 270 */ 1172, 1167, 1173, 1174, 1175, 1180, 1181, 1182, 1184, 1144, + /* 280 */ 1146, 1148, 1147, 1149, 1152, 1153, 1160, 1186, 1194, 1185, + /* 290 */ 1189, 1187, 1191, 1193, 1192, 1196, 1198, 1197, 1202, 1215, + /* 300 */ 1217, 1226, 1227, 1231, 1232, 1233, 1234, 1203, 1204, 1205, + /* 310 */ 1221, 1223, 1209, 1211, 1237, +}; +static const YYACTIONTYPE yy_default[] = { + /* 0 */ 1250, 1240, 1240, 1240, 1174, 1174, 1174, 1240, 1071, 1100, + /* 10 */ 1100, 1224, 1301, 1301, 1301, 1301, 1301, 1301, 1173, 1301, + /* 20 */ 1301, 1301, 1301, 1240, 1075, 1106, 1301, 1301, 1301, 1301, + /* 30 */ 1301, 1301, 1301, 1301, 1223, 1225, 1114, 1113, 1206, 1087, + /* 40 */ 1111, 1104, 1108, 1175, 1169, 1170, 1168, 1172, 1176, 1301, + /* 50 */ 1107, 1138, 1153, 1137, 1301, 1301, 1301, 1301, 1301, 1301, + /* 60 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 70 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 80 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 90 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1147, 1152, 1159, + /* 100 */ 1151, 1148, 1140, 1139, 1141, 1142, 1301, 994, 1042, 1301, + /* 110 */ 1301, 1301, 1143, 1301, 1144, 1156, 1155, 1154, 1231, 1258, + /* 120 */ 1257, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 130 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 140 */ 1301, 1301, 1301, 1301, 1301, 1301, 1250, 1240, 1000, 1000, + /* 150 */ 1301, 1240, 1240, 1240, 1240, 1240, 1240, 1236, 1075, 1066, + /* 160 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 170 */ 1228, 1226, 1301, 1187, 1301, 1301, 1301, 1301, 1301, 1301, + /* 180 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 190 */ 1301, 1301, 1301, 1301, 1301, 1071, 1301, 1301, 1301, 1301, + /* 200 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1252, 1301, + /* 210 */ 1201, 1071, 1071, 1071, 1073, 1055, 1065, 979, 1110, 1089, + /* 220 */ 1089, 1290, 1110, 1290, 1017, 1272, 1014, 1100, 1089, 1171, + /* 230 */ 1100, 1100, 1072, 1065, 1301, 1293, 1080, 1080, 1292, 1292, + /* 240 */ 1080, 1119, 1045, 1110, 1051, 1051, 1051, 1051, 1080, 991, + /* 250 */ 1110, 1119, 1045, 1045, 1110, 1080, 991, 1205, 1287, 1080, + /* 260 */ 1080, 991, 1180, 1080, 991, 1080, 991, 1180, 1043, 1043, + /* 270 */ 1043, 1032, 1180, 1043, 1017, 1043, 1032, 1043, 1043, 1093, + /* 280 */ 1088, 1093, 1088, 1093, 1088, 1093, 1088, 1080, 1080, 1301, + /* 290 */ 1180, 1184, 1184, 1180, 1105, 1094, 1103, 1101, 1110, 997, + /* 300 */ 1035, 1255, 1255, 1251, 1251, 1251, 1251, 1298, 1298, 1236, + /* 310 */ 1267, 1267, 1019, 1019, 1267, 1301, 1301, 1301, 1301, 1301, + /* 320 */ 1301, 1262, 1301, 1189, 1301, 1301, 1301, 1301, 1301, 1301, + /* 330 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 340 */ 1125, 1301, 975, 1233, 1301, 1301, 1232, 1301, 1301, 1301, + /* 350 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 360 */ 1301, 1301, 1301, 1301, 1301, 1289, 1301, 1301, 1301, 1301, + /* 370 */ 1301, 1301, 1204, 1203, 1301, 1301, 1301, 1301, 1301, 1301, + /* 380 */ 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 390 */ 1301, 1301, 1301, 1301, 1301, 1301, 1057, 1301, 1301, 1301, + /* 400 */ 1276, 1301, 1301, 1301, 1301, 1301, 1301, 1301, 1102, 1301, + /* 410 */ 1095, 1301, 1280, 1301, 1301, 1301, 1301, 1301, 1301, 1301, + /* 420 */ 1301, 1301, 1301, 1242, 1301, 1301, 1301, 1241, 1301, 1301, + /* 430 */ 1301, 1301, 1301, 1127, 1301, 1126, 1130, 1301, 985, 1301, +}; +/********** End of lemon-generated parsing tables *****************************/ + +/* The next table maps tokens (terminal symbols) into fallback tokens. +** If a construct like the following: +** +** %fallback ID X Y Z. +** +** appears in the grammar, then ID becomes a fallback token for X, Y, +** and Z. Whenever one of the tokens X, Y, or Z is input to the parser +** but it does not parse, the type of the token is changed to ID and +** the parse is retried before an error is thrown. +** +** This feature can be used, for example, to cause some keywords in a language +** to revert to identifiers if they keyword does not apply in the context where +** it appears. +*/ +#ifdef YYFALLBACK +static const YYCODETYPE yyFallback[] = { + 0, /* $ => nothing */ + 0, /* SEMI => nothing */ + 55, /* EXPLAIN => ID */ + 55, /* QUERY => ID */ + 55, /* PLAN => ID */ + 55, /* BEGIN => ID */ + 0, /* TRANSACTION => nothing */ + 55, /* DEFERRED => ID */ + 55, /* IMMEDIATE => ID */ + 55, /* EXCLUSIVE => ID */ + 0, /* COMMIT => nothing */ + 55, /* END => ID */ + 55, /* ROLLBACK => ID */ + 55, /* SAVEPOINT => ID */ + 55, /* RELEASE => ID */ + 0, /* TO => nothing */ + 0, /* TABLE => nothing */ + 0, /* CREATE => nothing */ + 55, /* IF => ID */ + 0, /* NOT => nothing */ + 0, /* EXISTS => nothing */ + 55, /* TEMP => ID */ + 0, /* LP => nothing */ + 0, /* RP => nothing */ + 0, /* AS => nothing */ + 55, /* WITHOUT => ID */ + 0, /* COMMA => nothing */ + 0, /* OR => nothing */ + 0, /* AND => nothing */ + 0, /* IS => nothing */ + 55, /* MATCH => ID */ + 55, /* LIKE_KW => ID */ + 0, /* BETWEEN => nothing */ + 0, /* IN => nothing */ + 0, /* ISNULL => nothing */ + 0, /* NOTNULL => nothing */ + 0, /* NE => nothing */ + 0, /* EQ => nothing */ + 0, /* GT => nothing */ + 0, /* LE => nothing */ + 0, /* LT => nothing */ + 0, /* GE => nothing */ + 0, /* ESCAPE => nothing */ + 0, /* BITAND => nothing */ + 0, /* BITOR => nothing */ + 0, /* LSHIFT => nothing */ + 0, /* RSHIFT => nothing */ + 0, /* PLUS => nothing */ + 0, /* MINUS => nothing */ + 0, /* STAR => nothing */ + 0, /* SLASH => nothing */ + 0, /* REM => nothing */ + 0, /* CONCAT => nothing */ + 0, /* COLLATE => nothing */ + 0, /* BITNOT => nothing */ + 0, /* ID => nothing */ + 0, /* INDEXED => nothing */ + 55, /* ABORT => ID */ + 55, /* ACTION => ID */ + 55, /* AFTER => ID */ + 55, /* ANALYZE => ID */ + 55, /* ASC => ID */ + 55, /* ATTACH => ID */ + 55, /* BEFORE => ID */ + 55, /* BY => ID */ + 55, /* CASCADE => ID */ + 55, /* CAST => ID */ + 55, /* COLUMNKW => ID */ + 55, /* CONFLICT => ID */ + 55, /* DATABASE => ID */ + 55, /* DESC => ID */ + 55, /* DETACH => ID */ + 55, /* EACH => ID */ + 55, /* FAIL => ID */ + 55, /* FOR => ID */ + 55, /* IGNORE => ID */ + 55, /* INITIALLY => ID */ + 55, /* INSTEAD => ID */ + 55, /* NO => ID */ + 55, /* KEY => ID */ + 55, /* OF => ID */ + 55, /* OFFSET => ID */ + 55, /* PRAGMA => ID */ + 55, /* RAISE => ID */ + 55, /* RECURSIVE => ID */ + 55, /* REPLACE => ID */ + 55, /* RESTRICT => ID */ + 55, /* ROW => ID */ + 55, /* TRIGGER => ID */ + 55, /* VACUUM => ID */ + 55, /* VIEW => ID */ + 55, /* VIRTUAL => ID */ + 55, /* WITH => ID */ + 55, /* REINDEX => ID */ + 55, /* RENAME => ID */ + 55, /* CTIME_KW => ID */ +}; +#endif /* YYFALLBACK */ + +/* The following structure represents a single element of the +** parser's stack. Information stored includes: +** +** + The state number for the parser at this level of the stack. +** +** + The value of the token stored at this level of the stack. +** (In other words, the "major" token.) +** +** + The semantic value stored at this level of the stack. This is +** the information used by the action routines in the grammar. +** It is sometimes called the "minor" token. +** +** After the "shift" half of a SHIFTREDUCE action, the stateno field +** actually contains the reduce action for the second half of the +** SHIFTREDUCE. +*/ +struct yyStackEntry { + YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */ + YYCODETYPE major; /* The major token value. This is the code + ** number for the token at this stack level */ + YYMINORTYPE minor; /* The user-supplied minor token value. This + ** is the value of the token */ +}; +typedef struct yyStackEntry yyStackEntry; + +/* The state of the parser is completely contained in an instance of +** the following structure */ +struct yyParser { + int yyidx; /* Index of top element in stack */ +#ifdef YYTRACKMAXSTACKDEPTH + int yyidxMax; /* Maximum value of yyidx */ +#endif +#ifndef YYNOERRORRECOVERY + int yyerrcnt; /* Shifts left before out of the error */ +#endif + sqlite3ParserARG_SDECL /* A place to hold %extra_argument */ +#if YYSTACKDEPTH<=0 + int yystksz; /* Current side of the stack */ + yyStackEntry *yystack; /* The parser's stack */ +#else + yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ +#endif +}; +typedef struct yyParser yyParser; + +#ifndef NDEBUG +/* #include */ +static FILE *yyTraceFILE = 0; +static char *yyTracePrompt = 0; +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* +** Turn parser tracing on by giving a stream to which to write the trace +** and a prompt to preface each trace message. Tracing is turned off +** by making either argument NULL +** +** Inputs: +**
      +**
    • A FILE* to which trace output should be written. +** If NULL, then tracing is turned off. +**
    • A prefix string written at the beginning of every +** line of trace output. If NULL, then tracing is +** turned off. +**
    +** +** Outputs: +** None. +*/ +SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){ + yyTraceFILE = TraceFILE; + yyTracePrompt = zTracePrompt; + if( yyTraceFILE==0 ) yyTracePrompt = 0; + else if( yyTracePrompt==0 ) yyTraceFILE = 0; +} +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* For tracing shifts, the names of all terminals and nonterminals +** are required. The following table supplies these names */ +static const char *const yyTokenName[] = { + "$", "SEMI", "EXPLAIN", "QUERY", + "PLAN", "BEGIN", "TRANSACTION", "DEFERRED", + "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END", + "ROLLBACK", "SAVEPOINT", "RELEASE", "TO", + "TABLE", "CREATE", "IF", "NOT", + "EXISTS", "TEMP", "LP", "RP", + "AS", "WITHOUT", "COMMA", "OR", + "AND", "IS", "MATCH", "LIKE_KW", + "BETWEEN", "IN", "ISNULL", "NOTNULL", + "NE", "EQ", "GT", "LE", + "LT", "GE", "ESCAPE", "BITAND", + "BITOR", "LSHIFT", "RSHIFT", "PLUS", + "MINUS", "STAR", "SLASH", "REM", + "CONCAT", "COLLATE", "BITNOT", "ID", + "INDEXED", "ABORT", "ACTION", "AFTER", + "ANALYZE", "ASC", "ATTACH", "BEFORE", + "BY", "CASCADE", "CAST", "COLUMNKW", + "CONFLICT", "DATABASE", "DESC", "DETACH", + "EACH", "FAIL", "FOR", "IGNORE", + "INITIALLY", "INSTEAD", "NO", "KEY", + "OF", "OFFSET", "PRAGMA", "RAISE", + "RECURSIVE", "REPLACE", "RESTRICT", "ROW", + "TRIGGER", "VACUUM", "VIEW", "VIRTUAL", + "WITH", "REINDEX", "RENAME", "CTIME_KW", + "ANY", "STRING", "JOIN_KW", "CONSTRAINT", + "DEFAULT", "NULL", "PRIMARY", "UNIQUE", + "CHECK", "REFERENCES", "AUTOINCR", "ON", + "INSERT", "DELETE", "UPDATE", "SET", + "DEFERRABLE", "FOREIGN", "DROP", "UNION", + "ALL", "EXCEPT", "INTERSECT", "SELECT", + "VALUES", "DISTINCT", "DOT", "FROM", + "JOIN", "USING", "ORDER", "GROUP", + "HAVING", "LIMIT", "WHERE", "INTO", + "INTEGER", "FLOAT", "BLOB", "VARIABLE", + "CASE", "WHEN", "THEN", "ELSE", + "INDEX", "ALTER", "ADD", "error", + "input", "cmdlist", "ecmd", "explain", + "cmdx", "cmd", "transtype", "trans_opt", + "nm", "savepoint_opt", "create_table", "create_table_args", + "createkw", "temp", "ifnotexists", "dbnm", + "columnlist", "conslist_opt", "table_options", "select", + "columnname", "carglist", "typetoken", "typename", + "signed", "plus_num", "minus_num", "ccons", + "term", "expr", "onconf", "sortorder", + "autoinc", "eidlist_opt", "refargs", "defer_subclause", + "refarg", "refact", "init_deferred_pred_opt", "conslist", + "tconscomma", "tcons", "sortlist", "eidlist", + "defer_subclause_opt", "orconf", "resolvetype", "raisetype", + "ifexists", "fullname", "selectnowith", "oneselect", + "with", "multiselect_op", "distinct", "selcollist", + "from", "where_opt", "groupby_opt", "having_opt", + "orderby_opt", "limit_opt", "values", "nexprlist", + "exprlist", "sclp", "as", "seltablist", + "stl_prefix", "joinop", "indexed_opt", "on_opt", + "using_opt", "idlist", "setlist", "insert_cmd", + "idlist_opt", "likeop", "between_op", "in_op", + "case_operand", "case_exprlist", "case_else", "uniqueflag", + "collate", "nmnum", "trigger_decl", "trigger_cmd_list", + "trigger_time", "trigger_event", "foreach_clause", "when_clause", + "trigger_cmd", "trnm", "tridxby", "database_kw_opt", + "key_opt", "add_column_fullname", "kwcolumn_opt", "create_vtab", + "vtabarglist", "vtabarg", "vtabargtoken", "lp", + "anylist", "wqlist", +}; +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* For tracing reduce actions, the names of all rules are required. +*/ +static const char *const yyRuleName[] = { + /* 0 */ "explain ::= EXPLAIN", + /* 1 */ "explain ::= EXPLAIN QUERY PLAN", + /* 2 */ "cmdx ::= cmd", + /* 3 */ "cmd ::= BEGIN transtype trans_opt", + /* 4 */ "transtype ::=", + /* 5 */ "transtype ::= DEFERRED", + /* 6 */ "transtype ::= IMMEDIATE", + /* 7 */ "transtype ::= EXCLUSIVE", + /* 8 */ "cmd ::= COMMIT trans_opt", + /* 9 */ "cmd ::= END trans_opt", + /* 10 */ "cmd ::= ROLLBACK trans_opt", + /* 11 */ "cmd ::= SAVEPOINT nm", + /* 12 */ "cmd ::= RELEASE savepoint_opt nm", + /* 13 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm", + /* 14 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm", + /* 15 */ "createkw ::= CREATE", + /* 16 */ "ifnotexists ::=", + /* 17 */ "ifnotexists ::= IF NOT EXISTS", + /* 18 */ "temp ::= TEMP", + /* 19 */ "temp ::=", + /* 20 */ "create_table_args ::= LP columnlist conslist_opt RP table_options", + /* 21 */ "create_table_args ::= AS select", + /* 22 */ "table_options ::=", + /* 23 */ "table_options ::= WITHOUT nm", + /* 24 */ "columnname ::= nm typetoken", + /* 25 */ "typetoken ::=", + /* 26 */ "typetoken ::= typename LP signed RP", + /* 27 */ "typetoken ::= typename LP signed COMMA signed RP", + /* 28 */ "typename ::= typename ID|STRING", + /* 29 */ "ccons ::= CONSTRAINT nm", + /* 30 */ "ccons ::= DEFAULT term", + /* 31 */ "ccons ::= DEFAULT LP expr RP", + /* 32 */ "ccons ::= DEFAULT PLUS term", + /* 33 */ "ccons ::= DEFAULT MINUS term", + /* 34 */ "ccons ::= DEFAULT ID|INDEXED", + /* 35 */ "ccons ::= NOT NULL onconf", + /* 36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc", + /* 37 */ "ccons ::= UNIQUE onconf", + /* 38 */ "ccons ::= CHECK LP expr RP", + /* 39 */ "ccons ::= REFERENCES nm eidlist_opt refargs", + /* 40 */ "ccons ::= defer_subclause", + /* 41 */ "ccons ::= COLLATE ID|STRING", + /* 42 */ "autoinc ::=", + /* 43 */ "autoinc ::= AUTOINCR", + /* 44 */ "refargs ::=", + /* 45 */ "refargs ::= refargs refarg", + /* 46 */ "refarg ::= MATCH nm", + /* 47 */ "refarg ::= ON INSERT refact", + /* 48 */ "refarg ::= ON DELETE refact", + /* 49 */ "refarg ::= ON UPDATE refact", + /* 50 */ "refact ::= SET NULL", + /* 51 */ "refact ::= SET DEFAULT", + /* 52 */ "refact ::= CASCADE", + /* 53 */ "refact ::= RESTRICT", + /* 54 */ "refact ::= NO ACTION", + /* 55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt", + /* 56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt", + /* 57 */ "init_deferred_pred_opt ::=", + /* 58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED", + /* 59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE", + /* 60 */ "conslist_opt ::=", + /* 61 */ "tconscomma ::= COMMA", + /* 62 */ "tcons ::= CONSTRAINT nm", + /* 63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf", + /* 64 */ "tcons ::= UNIQUE LP sortlist RP onconf", + /* 65 */ "tcons ::= CHECK LP expr RP onconf", + /* 66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt", + /* 67 */ "defer_subclause_opt ::=", + /* 68 */ "onconf ::=", + /* 69 */ "onconf ::= ON CONFLICT resolvetype", + /* 70 */ "orconf ::=", + /* 71 */ "orconf ::= OR resolvetype", + /* 72 */ "resolvetype ::= IGNORE", + /* 73 */ "resolvetype ::= REPLACE", + /* 74 */ "cmd ::= DROP TABLE ifexists fullname", + /* 75 */ "ifexists ::= IF EXISTS", + /* 76 */ "ifexists ::=", + /* 77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select", + /* 78 */ "cmd ::= DROP VIEW ifexists fullname", + /* 79 */ "cmd ::= select", + /* 80 */ "select ::= with selectnowith", + /* 81 */ "selectnowith ::= selectnowith multiselect_op oneselect", + /* 82 */ "multiselect_op ::= UNION", + /* 83 */ "multiselect_op ::= UNION ALL", + /* 84 */ "multiselect_op ::= EXCEPT|INTERSECT", + /* 85 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt", + /* 86 */ "values ::= VALUES LP nexprlist RP", + /* 87 */ "values ::= values COMMA LP exprlist RP", + /* 88 */ "distinct ::= DISTINCT", + /* 89 */ "distinct ::= ALL", + /* 90 */ "distinct ::=", + /* 91 */ "sclp ::=", + /* 92 */ "selcollist ::= sclp expr as", + /* 93 */ "selcollist ::= sclp STAR", + /* 94 */ "selcollist ::= sclp nm DOT STAR", + /* 95 */ "as ::= AS nm", + /* 96 */ "as ::=", + /* 97 */ "from ::=", + /* 98 */ "from ::= FROM seltablist", + /* 99 */ "stl_prefix ::= seltablist joinop", + /* 100 */ "stl_prefix ::=", + /* 101 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt", + /* 102 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt", + /* 103 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt", + /* 104 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt", + /* 105 */ "dbnm ::=", + /* 106 */ "dbnm ::= DOT nm", + /* 107 */ "fullname ::= nm dbnm", + /* 108 */ "joinop ::= COMMA|JOIN", + /* 109 */ "joinop ::= JOIN_KW JOIN", + /* 110 */ "joinop ::= JOIN_KW nm JOIN", + /* 111 */ "joinop ::= JOIN_KW nm nm JOIN", + /* 112 */ "on_opt ::= ON expr", + /* 113 */ "on_opt ::=", + /* 114 */ "indexed_opt ::=", + /* 115 */ "indexed_opt ::= INDEXED BY nm", + /* 116 */ "indexed_opt ::= NOT INDEXED", + /* 117 */ "using_opt ::= USING LP idlist RP", + /* 118 */ "using_opt ::=", + /* 119 */ "orderby_opt ::=", + /* 120 */ "orderby_opt ::= ORDER BY sortlist", + /* 121 */ "sortlist ::= sortlist COMMA expr sortorder", + /* 122 */ "sortlist ::= expr sortorder", + /* 123 */ "sortorder ::= ASC", + /* 124 */ "sortorder ::= DESC", + /* 125 */ "sortorder ::=", + /* 126 */ "groupby_opt ::=", + /* 127 */ "groupby_opt ::= GROUP BY nexprlist", + /* 128 */ "having_opt ::=", + /* 129 */ "having_opt ::= HAVING expr", + /* 130 */ "limit_opt ::=", + /* 131 */ "limit_opt ::= LIMIT expr", + /* 132 */ "limit_opt ::= LIMIT expr OFFSET expr", + /* 133 */ "limit_opt ::= LIMIT expr COMMA expr", + /* 134 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt", + /* 135 */ "where_opt ::=", + /* 136 */ "where_opt ::= WHERE expr", + /* 137 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt", + /* 138 */ "setlist ::= setlist COMMA nm EQ expr", + /* 139 */ "setlist ::= nm EQ expr", + /* 140 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select", + /* 141 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES", + /* 142 */ "insert_cmd ::= INSERT orconf", + /* 143 */ "insert_cmd ::= REPLACE", + /* 144 */ "idlist_opt ::=", + /* 145 */ "idlist_opt ::= LP idlist RP", + /* 146 */ "idlist ::= idlist COMMA nm", + /* 147 */ "idlist ::= nm", + /* 148 */ "expr ::= LP expr RP", + /* 149 */ "term ::= NULL", + /* 150 */ "expr ::= ID|INDEXED", + /* 151 */ "expr ::= JOIN_KW", + /* 152 */ "expr ::= nm DOT nm", + /* 153 */ "expr ::= nm DOT nm DOT nm", + /* 154 */ "term ::= INTEGER|FLOAT|BLOB", + /* 155 */ "term ::= STRING", + /* 156 */ "expr ::= VARIABLE", + /* 157 */ "expr ::= expr COLLATE ID|STRING", + /* 158 */ "expr ::= CAST LP expr AS typetoken RP", + /* 159 */ "expr ::= ID|INDEXED LP distinct exprlist RP", + /* 160 */ "expr ::= ID|INDEXED LP STAR RP", + /* 161 */ "term ::= CTIME_KW", + /* 162 */ "expr ::= expr AND expr", + /* 163 */ "expr ::= expr OR expr", + /* 164 */ "expr ::= expr LT|GT|GE|LE expr", + /* 165 */ "expr ::= expr EQ|NE expr", + /* 166 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr", + /* 167 */ "expr ::= expr PLUS|MINUS expr", + /* 168 */ "expr ::= expr STAR|SLASH|REM expr", + /* 169 */ "expr ::= expr CONCAT expr", + /* 170 */ "likeop ::= LIKE_KW|MATCH", + /* 171 */ "likeop ::= NOT LIKE_KW|MATCH", + /* 172 */ "expr ::= expr likeop expr", + /* 173 */ "expr ::= expr likeop expr ESCAPE expr", + /* 174 */ "expr ::= expr ISNULL|NOTNULL", + /* 175 */ "expr ::= expr NOT NULL", + /* 176 */ "expr ::= expr IS expr", + /* 177 */ "expr ::= expr IS NOT expr", + /* 178 */ "expr ::= NOT expr", + /* 179 */ "expr ::= BITNOT expr", + /* 180 */ "expr ::= MINUS expr", + /* 181 */ "expr ::= PLUS expr", + /* 182 */ "between_op ::= BETWEEN", + /* 183 */ "between_op ::= NOT BETWEEN", + /* 184 */ "expr ::= expr between_op expr AND expr", + /* 185 */ "in_op ::= IN", + /* 186 */ "in_op ::= NOT IN", + /* 187 */ "expr ::= expr in_op LP exprlist RP", + /* 188 */ "expr ::= LP select RP", + /* 189 */ "expr ::= expr in_op LP select RP", + /* 190 */ "expr ::= expr in_op nm dbnm", + /* 191 */ "expr ::= EXISTS LP select RP", + /* 192 */ "expr ::= CASE case_operand case_exprlist case_else END", + /* 193 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr", + /* 194 */ "case_exprlist ::= WHEN expr THEN expr", + /* 195 */ "case_else ::= ELSE expr", + /* 196 */ "case_else ::=", + /* 197 */ "case_operand ::= expr", + /* 198 */ "case_operand ::=", + /* 199 */ "exprlist ::=", + /* 200 */ "nexprlist ::= nexprlist COMMA expr", + /* 201 */ "nexprlist ::= expr", + /* 202 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt", + /* 203 */ "uniqueflag ::= UNIQUE", + /* 204 */ "uniqueflag ::=", + /* 205 */ "eidlist_opt ::=", + /* 206 */ "eidlist_opt ::= LP eidlist RP", + /* 207 */ "eidlist ::= eidlist COMMA nm collate sortorder", + /* 208 */ "eidlist ::= nm collate sortorder", + /* 209 */ "collate ::=", + /* 210 */ "collate ::= COLLATE ID|STRING", + /* 211 */ "cmd ::= DROP INDEX ifexists fullname", + /* 212 */ "cmd ::= VACUUM", + /* 213 */ "cmd ::= VACUUM nm", + /* 214 */ "cmd ::= PRAGMA nm dbnm", + /* 215 */ "cmd ::= PRAGMA nm dbnm EQ nmnum", + /* 216 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP", + /* 217 */ "cmd ::= PRAGMA nm dbnm EQ minus_num", + /* 218 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP", + /* 219 */ "plus_num ::= PLUS INTEGER|FLOAT", + /* 220 */ "minus_num ::= MINUS INTEGER|FLOAT", + /* 221 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END", + /* 222 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause", + /* 223 */ "trigger_time ::= BEFORE", + /* 224 */ "trigger_time ::= AFTER", + /* 225 */ "trigger_time ::= INSTEAD OF", + /* 226 */ "trigger_time ::=", + /* 227 */ "trigger_event ::= DELETE|INSERT", + /* 228 */ "trigger_event ::= UPDATE", + /* 229 */ "trigger_event ::= UPDATE OF idlist", + /* 230 */ "when_clause ::=", + /* 231 */ "when_clause ::= WHEN expr", + /* 232 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI", + /* 233 */ "trigger_cmd_list ::= trigger_cmd SEMI", + /* 234 */ "trnm ::= nm DOT nm", + /* 235 */ "tridxby ::= INDEXED BY nm", + /* 236 */ "tridxby ::= NOT INDEXED", + /* 237 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt", + /* 238 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select", + /* 239 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt", + /* 240 */ "trigger_cmd ::= select", + /* 241 */ "expr ::= RAISE LP IGNORE RP", + /* 242 */ "expr ::= RAISE LP raisetype COMMA nm RP", + /* 243 */ "raisetype ::= ROLLBACK", + /* 244 */ "raisetype ::= ABORT", + /* 245 */ "raisetype ::= FAIL", + /* 246 */ "cmd ::= DROP TRIGGER ifexists fullname", + /* 247 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt", + /* 248 */ "cmd ::= DETACH database_kw_opt expr", + /* 249 */ "key_opt ::=", + /* 250 */ "key_opt ::= KEY expr", + /* 251 */ "cmd ::= REINDEX", + /* 252 */ "cmd ::= REINDEX nm dbnm", + /* 253 */ "cmd ::= ANALYZE", + /* 254 */ "cmd ::= ANALYZE nm dbnm", + /* 255 */ "cmd ::= ALTER TABLE fullname RENAME TO nm", + /* 256 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist", + /* 257 */ "add_column_fullname ::= fullname", + /* 258 */ "cmd ::= create_vtab", + /* 259 */ "cmd ::= create_vtab LP vtabarglist RP", + /* 260 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm", + /* 261 */ "vtabarg ::=", + /* 262 */ "vtabargtoken ::= ANY", + /* 263 */ "vtabargtoken ::= lp anylist RP", + /* 264 */ "lp ::= LP", + /* 265 */ "with ::=", + /* 266 */ "with ::= WITH wqlist", + /* 267 */ "with ::= WITH RECURSIVE wqlist", + /* 268 */ "wqlist ::= nm eidlist_opt AS LP select RP", + /* 269 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP", + /* 270 */ "input ::= cmdlist", + /* 271 */ "cmdlist ::= cmdlist ecmd", + /* 272 */ "cmdlist ::= ecmd", + /* 273 */ "ecmd ::= SEMI", + /* 274 */ "ecmd ::= explain cmdx SEMI", + /* 275 */ "explain ::=", + /* 276 */ "trans_opt ::=", + /* 277 */ "trans_opt ::= TRANSACTION", + /* 278 */ "trans_opt ::= TRANSACTION nm", + /* 279 */ "savepoint_opt ::= SAVEPOINT", + /* 280 */ "savepoint_opt ::=", + /* 281 */ "cmd ::= create_table create_table_args", + /* 282 */ "columnlist ::= columnlist COMMA columnname carglist", + /* 283 */ "columnlist ::= columnname carglist", + /* 284 */ "nm ::= ID|INDEXED", + /* 285 */ "nm ::= STRING", + /* 286 */ "nm ::= JOIN_KW", + /* 287 */ "typetoken ::= typename", + /* 288 */ "typename ::= ID|STRING", + /* 289 */ "signed ::= plus_num", + /* 290 */ "signed ::= minus_num", + /* 291 */ "carglist ::= carglist ccons", + /* 292 */ "carglist ::=", + /* 293 */ "ccons ::= NULL onconf", + /* 294 */ "conslist_opt ::= COMMA conslist", + /* 295 */ "conslist ::= conslist tconscomma tcons", + /* 296 */ "conslist ::= tcons", + /* 297 */ "tconscomma ::=", + /* 298 */ "defer_subclause_opt ::= defer_subclause", + /* 299 */ "resolvetype ::= raisetype", + /* 300 */ "selectnowith ::= oneselect", + /* 301 */ "oneselect ::= values", + /* 302 */ "sclp ::= selcollist COMMA", + /* 303 */ "as ::= ID|STRING", + /* 304 */ "expr ::= term", + /* 305 */ "exprlist ::= nexprlist", + /* 306 */ "nmnum ::= plus_num", + /* 307 */ "nmnum ::= nm", + /* 308 */ "nmnum ::= ON", + /* 309 */ "nmnum ::= DELETE", + /* 310 */ "nmnum ::= DEFAULT", + /* 311 */ "plus_num ::= INTEGER|FLOAT", + /* 312 */ "foreach_clause ::=", + /* 313 */ "foreach_clause ::= FOR EACH ROW", + /* 314 */ "trnm ::= nm", + /* 315 */ "tridxby ::=", + /* 316 */ "database_kw_opt ::= DATABASE", + /* 317 */ "database_kw_opt ::=", + /* 318 */ "kwcolumn_opt ::=", + /* 319 */ "kwcolumn_opt ::= COLUMNKW", + /* 320 */ "vtabarglist ::= vtabarg", + /* 321 */ "vtabarglist ::= vtabarglist COMMA vtabarg", + /* 322 */ "vtabarg ::= vtabarg vtabargtoken", + /* 323 */ "anylist ::=", + /* 324 */ "anylist ::= anylist LP anylist RP", + /* 325 */ "anylist ::= anylist ANY", +}; +#endif /* NDEBUG */ + + +#if YYSTACKDEPTH<=0 +/* +** Try to increase the size of the parser stack. +*/ +static void yyGrowStack(yyParser *p){ + int newSize; + yyStackEntry *pNew; + + newSize = p->yystksz*2 + 100; + pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); + if( pNew ){ + p->yystack = pNew; + p->yystksz = newSize; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", + yyTracePrompt, p->yystksz); + } +#endif + } +} +#endif + +/* Datatype of the argument to the memory allocated passed as the +** second argument to sqlite3ParserAlloc() below. This can be changed by +** putting an appropriate #define in the %include section of the input +** grammar. +*/ +#ifndef YYMALLOCARGTYPE +# define YYMALLOCARGTYPE size_t +#endif + +/* +** This function allocates a new parser. +** The only argument is a pointer to a function which works like +** malloc. +** +** Inputs: +** A pointer to the function used to allocate memory. +** +** Outputs: +** A pointer to a parser. This pointer is used in subsequent calls +** to sqlite3Parser and sqlite3ParserFree. +*/ +SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ + yyParser *pParser; + pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); + if( pParser ){ + pParser->yyidx = -1; +#ifdef YYTRACKMAXSTACKDEPTH + pParser->yyidxMax = 0; +#endif +#if YYSTACKDEPTH<=0 + pParser->yystack = NULL; + pParser->yystksz = 0; + yyGrowStack(pParser); +#endif + } + return pParser; +} + +/* The following function deletes the "minor type" or semantic value +** associated with a symbol. The symbol can be either a terminal +** or nonterminal. "yymajor" is the symbol code, and "yypminor" is +** a pointer to the value to be deleted. The code used to do the +** deletions is derived from the %destructor and/or %token_destructor +** directives of the input grammar. +*/ +static void yy_destructor( + yyParser *yypParser, /* The parser */ + YYCODETYPE yymajor, /* Type code for object to destroy */ + YYMINORTYPE *yypminor /* The object to be destroyed */ +){ + sqlite3ParserARG_FETCH; + switch( yymajor ){ + /* Here is inserted the actions which take place when a + ** terminal or non-terminal is destroyed. This can happen + ** when the symbol is popped from the stack during a + ** reduce or during error processing or when a parser is + ** being destroyed before it is finished parsing. + ** + ** Note: during a reduce, the only symbols destroyed are those + ** which appear on the RHS of the rule, but which are *not* used + ** inside the C code. + */ +/********* Begin destructor definitions ***************************************/ + case 163: /* select */ + case 194: /* selectnowith */ + case 195: /* oneselect */ + case 206: /* values */ +{ +sqlite3SelectDelete(pParse->db, (yypminor->yy159)); +} + break; + case 172: /* term */ + case 173: /* expr */ +{ +sqlite3ExprDelete(pParse->db, (yypminor->yy342).pExpr); +} + break; + case 177: /* eidlist_opt */ + case 186: /* sortlist */ + case 187: /* eidlist */ + case 199: /* selcollist */ + case 202: /* groupby_opt */ + case 204: /* orderby_opt */ + case 207: /* nexprlist */ + case 208: /* exprlist */ + case 209: /* sclp */ + case 218: /* setlist */ + case 225: /* case_exprlist */ +{ +sqlite3ExprListDelete(pParse->db, (yypminor->yy442)); +} + break; + case 193: /* fullname */ + case 200: /* from */ + case 211: /* seltablist */ + case 212: /* stl_prefix */ +{ +sqlite3SrcListDelete(pParse->db, (yypminor->yy347)); +} + break; + case 196: /* with */ + case 249: /* wqlist */ +{ +sqlite3WithDelete(pParse->db, (yypminor->yy331)); +} + break; + case 201: /* where_opt */ + case 203: /* having_opt */ + case 215: /* on_opt */ + case 224: /* case_operand */ + case 226: /* case_else */ + case 235: /* when_clause */ + case 240: /* key_opt */ +{ +sqlite3ExprDelete(pParse->db, (yypminor->yy122)); +} + break; + case 216: /* using_opt */ + case 217: /* idlist */ + case 220: /* idlist_opt */ +{ +sqlite3IdListDelete(pParse->db, (yypminor->yy180)); +} + break; + case 231: /* trigger_cmd_list */ + case 236: /* trigger_cmd */ +{ +sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy327)); +} + break; + case 233: /* trigger_event */ +{ +sqlite3IdListDelete(pParse->db, (yypminor->yy410).b); +} + break; +/********* End destructor definitions *****************************************/ + default: break; /* If no destructor action specified: do nothing */ + } +} + +/* +** Pop the parser's stack once. +** +** If there is a destructor routine associated with the token which +** is popped from the stack, then call it. +*/ +static void yy_pop_parser_stack(yyParser *pParser){ + yyStackEntry *yytos; + assert( pParser->yyidx>=0 ); + yytos = &pParser->yystack[pParser->yyidx--]; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sPopping %s\n", + yyTracePrompt, + yyTokenName[yytos->major]); + } +#endif + yy_destructor(pParser, yytos->major, &yytos->minor); +} + +/* +** Deallocate and destroy a parser. Destructors are called for +** all stack elements before shutting the parser down. +** +** If the YYPARSEFREENEVERNULL macro exists (for example because it +** is defined in a %include section of the input grammar) then it is +** assumed that the input pointer is never NULL. +*/ +SQLITE_PRIVATE void sqlite3ParserFree( + void *p, /* The parser to be deleted */ + void (*freeProc)(void*) /* Function used to reclaim memory */ +){ + yyParser *pParser = (yyParser*)p; +#ifndef YYPARSEFREENEVERNULL + if( pParser==0 ) return; +#endif + while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); +#if YYSTACKDEPTH<=0 + free(pParser->yystack); +#endif + (*freeProc)((void*)pParser); +} + +/* +** Return the peak depth of the stack for a parser. +*/ +#ifdef YYTRACKMAXSTACKDEPTH +SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){ + yyParser *pParser = (yyParser*)p; + return pParser->yyidxMax; +} +#endif + +/* +** Find the appropriate action for a parser given the terminal +** look-ahead token iLookAhead. +*/ +static unsigned int yy_find_shift_action( + yyParser *pParser, /* The parser */ + YYCODETYPE iLookAhead /* The look-ahead token */ +){ + int i; + int stateno = pParser->yystack[pParser->yyidx].stateno; + + if( stateno>=YY_MIN_REDUCE ) return stateno; + assert( stateno <= YY_SHIFT_COUNT ); + do{ + i = yy_shift_ofst[stateno]; + if( i==YY_SHIFT_USE_DFLT ) return yy_default[stateno]; + assert( iLookAhead!=YYNOCODE ); + i += iLookAhead; + if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ + if( iLookAhead>0 ){ +#ifdef YYFALLBACK + YYCODETYPE iFallback; /* Fallback token */ + if( iLookAhead %s\n", + yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); + } +#endif + assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ + iLookAhead = iFallback; + continue; + } +#endif +#ifdef YYWILDCARD + { + int j = i - iLookAhead + YYWILDCARD; + if( +#if YY_SHIFT_MIN+YYWILDCARD<0 + j>=0 && +#endif +#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT + j %s\n", + yyTracePrompt, yyTokenName[iLookAhead], + yyTokenName[YYWILDCARD]); + } +#endif /* NDEBUG */ + return yy_action[j]; + } + } +#endif /* YYWILDCARD */ + } + return yy_default[stateno]; + }else{ + return yy_action[i]; + } + }while(1); +} + +/* +** Find the appropriate action for a parser given the non-terminal +** look-ahead token iLookAhead. +*/ +static int yy_find_reduce_action( + int stateno, /* Current state number */ + YYCODETYPE iLookAhead /* The look-ahead token */ +){ + int i; +#ifdef YYERRORSYMBOL + if( stateno>YY_REDUCE_COUNT ){ + return yy_default[stateno]; + } +#else + assert( stateno<=YY_REDUCE_COUNT ); +#endif + i = yy_reduce_ofst[stateno]; + assert( i!=YY_REDUCE_USE_DFLT ); + assert( iLookAhead!=YYNOCODE ); + i += iLookAhead; +#ifdef YYERRORSYMBOL + if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ + return yy_default[stateno]; + } +#else + assert( i>=0 && iyyidx--; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will execute if the parser + ** stack every overflows */ +/******** Begin %stack_overflow code ******************************************/ + + sqlite3ErrorMsg(pParse, "parser stack overflow"); +/******** End %stack_overflow code ********************************************/ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */ +} + +/* +** Print tracing information for a SHIFT action +*/ +#ifndef NDEBUG +static void yyTraceShift(yyParser *yypParser, int yyNewState){ + if( yyTraceFILE ){ + if( yyNewStateyystack[yypParser->yyidx].major], + yyNewState); + }else{ + fprintf(yyTraceFILE,"%sShift '%s'\n", + yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major]); + } + } +} +#else +# define yyTraceShift(X,Y) +#endif + +/* +** Perform a shift action. +*/ +static void yy_shift( + yyParser *yypParser, /* The parser to be shifted */ + int yyNewState, /* The new state to shift in */ + int yyMajor, /* The major token to shift in */ + sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */ +){ + yyStackEntry *yytos; + yypParser->yyidx++; +#ifdef YYTRACKMAXSTACKDEPTH + if( yypParser->yyidx>yypParser->yyidxMax ){ + yypParser->yyidxMax = yypParser->yyidx; + } +#endif +#if YYSTACKDEPTH>0 + if( yypParser->yyidx>=YYSTACKDEPTH ){ + yyStackOverflow(yypParser); + return; + } +#else + if( yypParser->yyidx>=yypParser->yystksz ){ + yyGrowStack(yypParser); + if( yypParser->yyidx>=yypParser->yystksz ){ + yyStackOverflow(yypParser); + return; + } + } +#endif + yytos = &yypParser->yystack[yypParser->yyidx]; + yytos->stateno = (YYACTIONTYPE)yyNewState; + yytos->major = (YYCODETYPE)yyMajor; + yytos->minor.yy0 = yyMinor; + yyTraceShift(yypParser, yyNewState); +} + +/* The following table contains information about every rule that +** is used during the reduce. +*/ +static const struct { + YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ + unsigned char nrhs; /* Number of right-hand side symbols in the rule */ +} yyRuleInfo[] = { + { 147, 1 }, + { 147, 3 }, + { 148, 1 }, + { 149, 3 }, + { 150, 0 }, + { 150, 1 }, + { 150, 1 }, + { 150, 1 }, + { 149, 2 }, + { 149, 2 }, + { 149, 2 }, + { 149, 2 }, + { 149, 3 }, + { 149, 5 }, + { 154, 6 }, + { 156, 1 }, + { 158, 0 }, + { 158, 3 }, + { 157, 1 }, + { 157, 0 }, + { 155, 5 }, + { 155, 2 }, + { 162, 0 }, + { 162, 2 }, + { 164, 2 }, + { 166, 0 }, + { 166, 4 }, + { 166, 6 }, + { 167, 2 }, + { 171, 2 }, + { 171, 2 }, + { 171, 4 }, + { 171, 3 }, + { 171, 3 }, + { 171, 2 }, + { 171, 3 }, + { 171, 5 }, + { 171, 2 }, + { 171, 4 }, + { 171, 4 }, + { 171, 1 }, + { 171, 2 }, + { 176, 0 }, + { 176, 1 }, + { 178, 0 }, + { 178, 2 }, + { 180, 2 }, + { 180, 3 }, + { 180, 3 }, + { 180, 3 }, + { 181, 2 }, + { 181, 2 }, + { 181, 1 }, + { 181, 1 }, + { 181, 2 }, + { 179, 3 }, + { 179, 2 }, + { 182, 0 }, + { 182, 2 }, + { 182, 2 }, + { 161, 0 }, + { 184, 1 }, + { 185, 2 }, + { 185, 7 }, + { 185, 5 }, + { 185, 5 }, + { 185, 10 }, + { 188, 0 }, + { 174, 0 }, + { 174, 3 }, + { 189, 0 }, + { 189, 2 }, + { 190, 1 }, + { 190, 1 }, + { 149, 4 }, + { 192, 2 }, + { 192, 0 }, + { 149, 9 }, + { 149, 4 }, + { 149, 1 }, + { 163, 2 }, + { 194, 3 }, + { 197, 1 }, + { 197, 2 }, + { 197, 1 }, + { 195, 9 }, + { 206, 4 }, + { 206, 5 }, + { 198, 1 }, + { 198, 1 }, + { 198, 0 }, + { 209, 0 }, + { 199, 3 }, + { 199, 2 }, + { 199, 4 }, + { 210, 2 }, + { 210, 0 }, + { 200, 0 }, + { 200, 2 }, + { 212, 2 }, + { 212, 0 }, + { 211, 7 }, + { 211, 9 }, + { 211, 7 }, + { 211, 7 }, + { 159, 0 }, + { 159, 2 }, + { 193, 2 }, + { 213, 1 }, + { 213, 2 }, + { 213, 3 }, + { 213, 4 }, + { 215, 2 }, + { 215, 0 }, + { 214, 0 }, + { 214, 3 }, + { 214, 2 }, + { 216, 4 }, + { 216, 0 }, + { 204, 0 }, + { 204, 3 }, + { 186, 4 }, + { 186, 2 }, + { 175, 1 }, + { 175, 1 }, + { 175, 0 }, + { 202, 0 }, + { 202, 3 }, + { 203, 0 }, + { 203, 2 }, + { 205, 0 }, + { 205, 2 }, + { 205, 4 }, + { 205, 4 }, + { 149, 6 }, + { 201, 0 }, + { 201, 2 }, + { 149, 8 }, + { 218, 5 }, + { 218, 3 }, + { 149, 6 }, + { 149, 7 }, + { 219, 2 }, + { 219, 1 }, + { 220, 0 }, + { 220, 3 }, + { 217, 3 }, + { 217, 1 }, + { 173, 3 }, + { 172, 1 }, + { 173, 1 }, + { 173, 1 }, + { 173, 3 }, + { 173, 5 }, + { 172, 1 }, + { 172, 1 }, + { 173, 1 }, + { 173, 3 }, + { 173, 6 }, + { 173, 5 }, + { 173, 4 }, + { 172, 1 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 173, 3 }, + { 221, 1 }, + { 221, 2 }, + { 173, 3 }, + { 173, 5 }, + { 173, 2 }, + { 173, 3 }, + { 173, 3 }, + { 173, 4 }, + { 173, 2 }, + { 173, 2 }, + { 173, 2 }, + { 173, 2 }, + { 222, 1 }, + { 222, 2 }, + { 173, 5 }, + { 223, 1 }, + { 223, 2 }, + { 173, 5 }, + { 173, 3 }, + { 173, 5 }, + { 173, 4 }, + { 173, 4 }, + { 173, 5 }, + { 225, 5 }, + { 225, 4 }, + { 226, 2 }, + { 226, 0 }, + { 224, 1 }, + { 224, 0 }, + { 208, 0 }, + { 207, 3 }, + { 207, 1 }, + { 149, 12 }, + { 227, 1 }, + { 227, 0 }, + { 177, 0 }, + { 177, 3 }, + { 187, 5 }, + { 187, 3 }, + { 228, 0 }, + { 228, 2 }, + { 149, 4 }, + { 149, 1 }, + { 149, 2 }, + { 149, 3 }, + { 149, 5 }, + { 149, 6 }, + { 149, 5 }, + { 149, 6 }, + { 169, 2 }, + { 170, 2 }, + { 149, 5 }, + { 230, 11 }, + { 232, 1 }, + { 232, 1 }, + { 232, 2 }, + { 232, 0 }, + { 233, 1 }, + { 233, 1 }, + { 233, 3 }, + { 235, 0 }, + { 235, 2 }, + { 231, 3 }, + { 231, 2 }, + { 237, 3 }, + { 238, 3 }, + { 238, 2 }, + { 236, 7 }, + { 236, 5 }, + { 236, 5 }, + { 236, 1 }, + { 173, 4 }, + { 173, 6 }, + { 191, 1 }, + { 191, 1 }, + { 191, 1 }, + { 149, 4 }, + { 149, 6 }, + { 149, 3 }, + { 240, 0 }, + { 240, 2 }, + { 149, 1 }, + { 149, 3 }, + { 149, 1 }, + { 149, 3 }, + { 149, 6 }, + { 149, 7 }, + { 241, 1 }, + { 149, 1 }, + { 149, 4 }, + { 243, 8 }, + { 245, 0 }, + { 246, 1 }, + { 246, 3 }, + { 247, 1 }, + { 196, 0 }, + { 196, 2 }, + { 196, 3 }, + { 249, 6 }, + { 249, 8 }, + { 144, 1 }, + { 145, 2 }, + { 145, 1 }, + { 146, 1 }, + { 146, 3 }, + { 147, 0 }, + { 151, 0 }, + { 151, 1 }, + { 151, 2 }, + { 153, 1 }, + { 153, 0 }, + { 149, 2 }, + { 160, 4 }, + { 160, 2 }, + { 152, 1 }, + { 152, 1 }, + { 152, 1 }, + { 166, 1 }, + { 167, 1 }, + { 168, 1 }, + { 168, 1 }, + { 165, 2 }, + { 165, 0 }, + { 171, 2 }, + { 161, 2 }, + { 183, 3 }, + { 183, 1 }, + { 184, 0 }, + { 188, 1 }, + { 190, 1 }, + { 194, 1 }, + { 195, 1 }, + { 209, 2 }, + { 210, 1 }, + { 173, 1 }, + { 208, 1 }, + { 229, 1 }, + { 229, 1 }, + { 229, 1 }, + { 229, 1 }, + { 229, 1 }, + { 169, 1 }, + { 234, 0 }, + { 234, 3 }, + { 237, 1 }, + { 238, 0 }, + { 239, 1 }, + { 239, 0 }, + { 242, 0 }, + { 242, 1 }, + { 244, 1 }, + { 244, 3 }, + { 245, 2 }, + { 248, 0 }, + { 248, 4 }, + { 248, 2 }, +}; + +static void yy_accept(yyParser*); /* Forward Declaration */ + +/* +** Perform a reduce action and the shift that must immediately +** follow the reduce. +*/ +static void yy_reduce( + yyParser *yypParser, /* The parser */ + unsigned int yyruleno /* Number of the rule by which to reduce */ +){ + int yygoto; /* The next state */ + int yyact; /* The next action */ + yyStackEntry *yymsp; /* The top of the parser's stack */ + int yysize; /* Amount to pop the stack */ + sqlite3ParserARG_FETCH; + yymsp = &yypParser->yystack[yypParser->yyidx]; +#ifndef NDEBUG + if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ + yysize = yyRuleInfo[yyruleno].nrhs; + fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt, + yyRuleName[yyruleno], yymsp[-yysize].stateno); + } +#endif /* NDEBUG */ + + /* Check that the stack is large enough to grow by a single entry + ** if the RHS of the rule is empty. This ensures that there is room + ** enough on the stack to push the LHS value */ + if( yyRuleInfo[yyruleno].nrhs==0 ){ +#ifdef YYTRACKMAXSTACKDEPTH + if( yypParser->yyidx>yypParser->yyidxMax ){ + yypParser->yyidxMax = yypParser->yyidx; + } +#endif +#if YYSTACKDEPTH>0 + if( yypParser->yyidx>=YYSTACKDEPTH-1 ){ + yyStackOverflow(yypParser); + return; + } +#else + if( yypParser->yyidx>=yypParser->yystksz-1 ){ + yyGrowStack(yypParser); + if( yypParser->yyidx>=yypParser->yystksz-1 ){ + yyStackOverflow(yypParser); + return; + } + } +#endif + } + + switch( yyruleno ){ + /* Beginning here are the reduction cases. A typical example + ** follows: + ** case 0: + ** #line + ** { ... } // User supplied code + ** #line + ** break; + */ +/********** Begin reduce actions **********************************************/ + YYMINORTYPE yylhsminor; + case 0: /* explain ::= EXPLAIN */ +{ pParse->explain = 1; } + break; + case 1: /* explain ::= EXPLAIN QUERY PLAN */ +{ pParse->explain = 2; } + break; + case 2: /* cmdx ::= cmd */ +{ sqlite3FinishCoding(pParse); } + break; + case 3: /* cmd ::= BEGIN transtype trans_opt */ +{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy392);} + break; + case 4: /* transtype ::= */ +{yymsp[1].minor.yy392 = TK_DEFERRED;} + break; + case 5: /* transtype ::= DEFERRED */ + case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6); + case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7); +{yymsp[0].minor.yy392 = yymsp[0].major; /*A-overwrites-X*/} + break; + case 8: /* cmd ::= COMMIT trans_opt */ + case 9: /* cmd ::= END trans_opt */ yytestcase(yyruleno==9); +{sqlite3CommitTransaction(pParse);} + break; + case 10: /* cmd ::= ROLLBACK trans_opt */ +{sqlite3RollbackTransaction(pParse);} + break; + case 11: /* cmd ::= SAVEPOINT nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0); +} + break; + case 12: /* cmd ::= RELEASE savepoint_opt nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0); +} + break; + case 13: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0); +} + break; + case 14: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */ +{ + sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy392,0,0,yymsp[-2].minor.yy392); +} + break; + case 15: /* createkw ::= CREATE */ +{disableLookaside(pParse);} + break; + case 16: /* ifnotexists ::= */ + case 19: /* temp ::= */ yytestcase(yyruleno==19); + case 22: /* table_options ::= */ yytestcase(yyruleno==22); + case 42: /* autoinc ::= */ yytestcase(yyruleno==42); + case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57); + case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67); + case 76: /* ifexists ::= */ yytestcase(yyruleno==76); + case 90: /* distinct ::= */ yytestcase(yyruleno==90); + case 209: /* collate ::= */ yytestcase(yyruleno==209); +{yymsp[1].minor.yy392 = 0;} + break; + case 17: /* ifnotexists ::= IF NOT EXISTS */ +{yymsp[-2].minor.yy392 = 1;} + break; + case 18: /* temp ::= TEMP */ + case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43); +{yymsp[0].minor.yy392 = 1;} + break; + case 20: /* create_table_args ::= LP columnlist conslist_opt RP table_options */ +{ + sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy392,0); +} + break; + case 21: /* create_table_args ::= AS select */ +{ + sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy159); + sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159); +} + break; + case 23: /* table_options ::= WITHOUT nm */ +{ + if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){ + yymsp[-1].minor.yy392 = TF_WithoutRowid | TF_NoVisibleRowid; + }else{ + yymsp[-1].minor.yy392 = 0; + sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z); + } +} + break; + case 24: /* columnname ::= nm typetoken */ +{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);} + break; + case 25: /* typetoken ::= */ + case 60: /* conslist_opt ::= */ yytestcase(yyruleno==60); + case 96: /* as ::= */ yytestcase(yyruleno==96); +{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;} + break; + case 26: /* typetoken ::= typename LP signed RP */ +{ + yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z); +} + break; + case 27: /* typetoken ::= typename LP signed COMMA signed RP */ +{ + yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z); +} + break; + case 28: /* typename ::= typename ID|STRING */ +{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);} + break; + case 29: /* ccons ::= CONSTRAINT nm */ + case 62: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==62); +{pParse->constraintName = yymsp[0].minor.yy0;} + break; + case 30: /* ccons ::= DEFAULT term */ + case 32: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==32); +{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy342);} + break; + case 31: /* ccons ::= DEFAULT LP expr RP */ +{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy342);} + break; + case 33: /* ccons ::= DEFAULT MINUS term */ +{ + ExprSpan v; + v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy342.pExpr, 0, 0); + v.zStart = yymsp[-1].minor.yy0.z; + v.zEnd = yymsp[0].minor.yy342.zEnd; + sqlite3AddDefaultValue(pParse,&v); +} + break; + case 34: /* ccons ::= DEFAULT ID|INDEXED */ +{ + ExprSpan v; + spanExpr(&v, pParse, TK_STRING, yymsp[0].minor.yy0); + sqlite3AddDefaultValue(pParse,&v); +} + break; + case 35: /* ccons ::= NOT NULL onconf */ +{sqlite3AddNotNull(pParse, yymsp[0].minor.yy392);} + break; + case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ +{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy392,yymsp[0].minor.yy392,yymsp[-2].minor.yy392);} + break; + case 37: /* ccons ::= UNIQUE onconf */ +{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy392,0,0,0,0);} + break; + case 38: /* ccons ::= CHECK LP expr RP */ +{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy342.pExpr);} + break; + case 39: /* ccons ::= REFERENCES nm eidlist_opt refargs */ +{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy442,yymsp[0].minor.yy392);} + break; + case 40: /* ccons ::= defer_subclause */ +{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy392);} + break; + case 41: /* ccons ::= COLLATE ID|STRING */ +{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);} + break; + case 44: /* refargs ::= */ +{ yymsp[1].minor.yy392 = OE_None*0x0101; /* EV: R-19803-45884 */} + break; + case 45: /* refargs ::= refargs refarg */ +{ yymsp[-1].minor.yy392 = (yymsp[-1].minor.yy392 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; } + break; + case 46: /* refarg ::= MATCH nm */ +{ yymsp[-1].minor.yy207.value = 0; yymsp[-1].minor.yy207.mask = 0x000000; } + break; + case 47: /* refarg ::= ON INSERT refact */ +{ yymsp[-2].minor.yy207.value = 0; yymsp[-2].minor.yy207.mask = 0x000000; } + break; + case 48: /* refarg ::= ON DELETE refact */ +{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy392; yymsp[-2].minor.yy207.mask = 0x0000ff; } + break; + case 49: /* refarg ::= ON UPDATE refact */ +{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy392<<8; yymsp[-2].minor.yy207.mask = 0x00ff00; } + break; + case 50: /* refact ::= SET NULL */ +{ yymsp[-1].minor.yy392 = OE_SetNull; /* EV: R-33326-45252 */} + break; + case 51: /* refact ::= SET DEFAULT */ +{ yymsp[-1].minor.yy392 = OE_SetDflt; /* EV: R-33326-45252 */} + break; + case 52: /* refact ::= CASCADE */ +{ yymsp[0].minor.yy392 = OE_Cascade; /* EV: R-33326-45252 */} + break; + case 53: /* refact ::= RESTRICT */ +{ yymsp[0].minor.yy392 = OE_Restrict; /* EV: R-33326-45252 */} + break; + case 54: /* refact ::= NO ACTION */ +{ yymsp[-1].minor.yy392 = OE_None; /* EV: R-33326-45252 */} + break; + case 55: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ +{yymsp[-2].minor.yy392 = 0;} + break; + case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ + case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71); + case 142: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==142); +{yymsp[-1].minor.yy392 = yymsp[0].minor.yy392;} + break; + case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ + case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75); + case 183: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==183); + case 186: /* in_op ::= NOT IN */ yytestcase(yyruleno==186); + case 210: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==210); +{yymsp[-1].minor.yy392 = 1;} + break; + case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ +{yymsp[-1].minor.yy392 = 0;} + break; + case 61: /* tconscomma ::= COMMA */ +{pParse->constraintName.n = 0;} + break; + case 63: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */ +{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy442,yymsp[0].minor.yy392,yymsp[-2].minor.yy392,0);} + break; + case 64: /* tcons ::= UNIQUE LP sortlist RP onconf */ +{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy442,yymsp[0].minor.yy392,0,0,0,0);} + break; + case 65: /* tcons ::= CHECK LP expr RP onconf */ +{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy342.pExpr);} + break; + case 66: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */ +{ + sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy442, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy442, yymsp[-1].minor.yy392); + sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy392); +} + break; + case 68: /* onconf ::= */ + case 70: /* orconf ::= */ yytestcase(yyruleno==70); +{yymsp[1].minor.yy392 = OE_Default;} + break; + case 69: /* onconf ::= ON CONFLICT resolvetype */ +{yymsp[-2].minor.yy392 = yymsp[0].minor.yy392;} + break; + case 72: /* resolvetype ::= IGNORE */ +{yymsp[0].minor.yy392 = OE_Ignore;} + break; + case 73: /* resolvetype ::= REPLACE */ + case 143: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==143); +{yymsp[0].minor.yy392 = OE_Replace;} + break; + case 74: /* cmd ::= DROP TABLE ifexists fullname */ +{ + sqlite3DropTable(pParse, yymsp[0].minor.yy347, 0, yymsp[-1].minor.yy392); +} + break; + case 77: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */ +{ + sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy442, yymsp[0].minor.yy159, yymsp[-7].minor.yy392, yymsp[-5].minor.yy392); +} + break; + case 78: /* cmd ::= DROP VIEW ifexists fullname */ +{ + sqlite3DropTable(pParse, yymsp[0].minor.yy347, 1, yymsp[-1].minor.yy392); +} + break; + case 79: /* cmd ::= select */ +{ + SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; + sqlite3Select(pParse, yymsp[0].minor.yy159, &dest); + sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159); +} + break; + case 80: /* select ::= with selectnowith */ +{ + Select *p = yymsp[0].minor.yy159; + if( p ){ + p->pWith = yymsp[-1].minor.yy331; + parserDoubleLinkSelect(pParse, p); + }else{ + sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy331); + } + yymsp[-1].minor.yy159 = p; /*A-overwrites-W*/ +} + break; + case 81: /* selectnowith ::= selectnowith multiselect_op oneselect */ +{ + Select *pRhs = yymsp[0].minor.yy159; + Select *pLhs = yymsp[-2].minor.yy159; + if( pRhs && pRhs->pPrior ){ + SrcList *pFrom; + Token x; + x.n = 0; + parserDoubleLinkSelect(pParse, pRhs); + pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); + pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); + } + if( pRhs ){ + pRhs->op = (u8)yymsp[-1].minor.yy392; + pRhs->pPrior = pLhs; + if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; + pRhs->selFlags &= ~SF_MultiValue; + if( yymsp[-1].minor.yy392!=TK_ALL ) pParse->hasCompound = 1; + }else{ + sqlite3SelectDelete(pParse->db, pLhs); + } + yymsp[-2].minor.yy159 = pRhs; +} + break; + case 82: /* multiselect_op ::= UNION */ + case 84: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==84); +{yymsp[0].minor.yy392 = yymsp[0].major; /*A-overwrites-OP*/} + break; + case 83: /* multiselect_op ::= UNION ALL */ +{yymsp[-1].minor.yy392 = TK_ALL;} + break; + case 85: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ +{ +#if SELECTTRACE_ENABLED + Token s = yymsp[-8].minor.yy0; /*A-overwrites-S*/ +#endif + yymsp[-8].minor.yy159 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy442,yymsp[-5].minor.yy347,yymsp[-4].minor.yy122,yymsp[-3].minor.yy442,yymsp[-2].minor.yy122,yymsp[-1].minor.yy442,yymsp[-7].minor.yy392,yymsp[0].minor.yy64.pLimit,yymsp[0].minor.yy64.pOffset); +#if SELECTTRACE_ENABLED + /* Populate the Select.zSelName[] string that is used to help with + ** query planner debugging, to differentiate between multiple Select + ** objects in a complex query. + ** + ** If the SELECT keyword is immediately followed by a C-style comment + ** then extract the first few alphanumeric characters from within that + ** comment to be the zSelName value. Otherwise, the label is #N where + ** is an integer that is incremented with each SELECT statement seen. + */ + if( yymsp[-8].minor.yy159!=0 ){ + const char *z = s.z+6; + int i; + sqlite3_snprintf(sizeof(yymsp[-8].minor.yy159->zSelName), yymsp[-8].minor.yy159->zSelName, "#%d", + ++pParse->nSelect); + while( z[0]==' ' ) z++; + if( z[0]=='/' && z[1]=='*' ){ + z += 2; + while( z[0]==' ' ) z++; + for(i=0; sqlite3Isalnum(z[i]); i++){} + sqlite3_snprintf(sizeof(yymsp[-8].minor.yy159->zSelName), yymsp[-8].minor.yy159->zSelName, "%.*s", i, z); + } + } +#endif /* SELECTRACE_ENABLED */ +} + break; + case 86: /* values ::= VALUES LP nexprlist RP */ +{ + yymsp[-3].minor.yy159 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy442,0,0,0,0,0,SF_Values,0,0); +} + break; + case 87: /* values ::= values COMMA LP exprlist RP */ +{ + Select *pRight, *pLeft = yymsp[-4].minor.yy159; + pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy442,0,0,0,0,0,SF_Values|SF_MultiValue,0,0); + if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; + if( pRight ){ + pRight->op = TK_ALL; + pRight->pPrior = pLeft; + yymsp[-4].minor.yy159 = pRight; + }else{ + yymsp[-4].minor.yy159 = pLeft; + } +} + break; + case 88: /* distinct ::= DISTINCT */ +{yymsp[0].minor.yy392 = SF_Distinct;} + break; + case 89: /* distinct ::= ALL */ +{yymsp[0].minor.yy392 = SF_All;} + break; + case 91: /* sclp ::= */ + case 119: /* orderby_opt ::= */ yytestcase(yyruleno==119); + case 126: /* groupby_opt ::= */ yytestcase(yyruleno==126); + case 199: /* exprlist ::= */ yytestcase(yyruleno==199); + case 205: /* eidlist_opt ::= */ yytestcase(yyruleno==205); +{yymsp[1].minor.yy442 = 0;} + break; + case 92: /* selcollist ::= sclp expr as */ +{ + yymsp[-2].minor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy442, yymsp[-1].minor.yy342.pExpr); + if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-2].minor.yy442, &yymsp[0].minor.yy0, 1); + sqlite3ExprListSetSpan(pParse,yymsp[-2].minor.yy442,&yymsp[-1].minor.yy342); +} + break; + case 93: /* selcollist ::= sclp STAR */ +{ + Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); + yymsp[-1].minor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy442, p); +} + break; + case 94: /* selcollist ::= sclp nm DOT STAR */ +{ + Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, &yymsp[0].minor.yy0); + Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + yymsp[-3].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442, pDot); +} + break; + case 95: /* as ::= AS nm */ + case 106: /* dbnm ::= DOT nm */ yytestcase(yyruleno==106); + case 219: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==219); + case 220: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==220); +{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;} + break; + case 97: /* from ::= */ +{yymsp[1].minor.yy347 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy347));} + break; + case 98: /* from ::= FROM seltablist */ +{ + yymsp[-1].minor.yy347 = yymsp[0].minor.yy347; + sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy347); +} + break; + case 99: /* stl_prefix ::= seltablist joinop */ +{ + if( ALWAYS(yymsp[-1].minor.yy347 && yymsp[-1].minor.yy347->nSrc>0) ) yymsp[-1].minor.yy347->a[yymsp[-1].minor.yy347->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy392; +} + break; + case 100: /* stl_prefix ::= */ +{yymsp[1].minor.yy347 = 0;} + break; + case 101: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */ +{ + yymsp[-6].minor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy122,yymsp[0].minor.yy180); + sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy347, &yymsp[-2].minor.yy0); +} + break; + case 102: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */ +{ + yymsp[-8].minor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy347,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy122,yymsp[0].minor.yy180); + sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy347, yymsp[-4].minor.yy442); +} + break; + case 103: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */ +{ + yymsp[-6].minor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy159,yymsp[-1].minor.yy122,yymsp[0].minor.yy180); + } + break; + case 104: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */ +{ + if( yymsp[-6].minor.yy347==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy122==0 && yymsp[0].minor.yy180==0 ){ + yymsp[-6].minor.yy347 = yymsp[-4].minor.yy347; + }else if( yymsp[-4].minor.yy347->nSrc==1 ){ + yymsp[-6].minor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy122,yymsp[0].minor.yy180); + if( yymsp[-6].minor.yy347 ){ + struct SrcList_item *pNew = &yymsp[-6].minor.yy347->a[yymsp[-6].minor.yy347->nSrc-1]; + struct SrcList_item *pOld = yymsp[-4].minor.yy347->a; + pNew->zName = pOld->zName; + pNew->zDatabase = pOld->zDatabase; + pNew->pSelect = pOld->pSelect; + pOld->zName = pOld->zDatabase = 0; + pOld->pSelect = 0; + } + sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy347); + }else{ + Select *pSubquery; + sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy347); + pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy347,0,0,0,0,SF_NestedFrom,0,0); + yymsp[-6].minor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy122,yymsp[0].minor.yy180); + } + } + break; + case 105: /* dbnm ::= */ + case 114: /* indexed_opt ::= */ yytestcase(yyruleno==114); +{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;} + break; + case 107: /* fullname ::= nm dbnm */ +{yymsp[-1].minor.yy347 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/} + break; + case 108: /* joinop ::= COMMA|JOIN */ +{ yymsp[0].minor.yy392 = JT_INNER; } + break; + case 109: /* joinop ::= JOIN_KW JOIN */ +{yymsp[-1].minor.yy392 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/} + break; + case 110: /* joinop ::= JOIN_KW nm JOIN */ +{yymsp[-2].minor.yy392 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/} + break; + case 111: /* joinop ::= JOIN_KW nm nm JOIN */ +{yymsp[-3].minor.yy392 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/} + break; + case 112: /* on_opt ::= ON expr */ + case 129: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==129); + case 136: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==136); + case 195: /* case_else ::= ELSE expr */ yytestcase(yyruleno==195); +{yymsp[-1].minor.yy122 = yymsp[0].minor.yy342.pExpr;} + break; + case 113: /* on_opt ::= */ + case 128: /* having_opt ::= */ yytestcase(yyruleno==128); + case 135: /* where_opt ::= */ yytestcase(yyruleno==135); + case 196: /* case_else ::= */ yytestcase(yyruleno==196); + case 198: /* case_operand ::= */ yytestcase(yyruleno==198); +{yymsp[1].minor.yy122 = 0;} + break; + case 115: /* indexed_opt ::= INDEXED BY nm */ +{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;} + break; + case 116: /* indexed_opt ::= NOT INDEXED */ +{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;} + break; + case 117: /* using_opt ::= USING LP idlist RP */ +{yymsp[-3].minor.yy180 = yymsp[-1].minor.yy180;} + break; + case 118: /* using_opt ::= */ + case 144: /* idlist_opt ::= */ yytestcase(yyruleno==144); +{yymsp[1].minor.yy180 = 0;} + break; + case 120: /* orderby_opt ::= ORDER BY sortlist */ + case 127: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==127); +{yymsp[-2].minor.yy442 = yymsp[0].minor.yy442;} + break; + case 121: /* sortlist ::= sortlist COMMA expr sortorder */ +{ + yymsp[-3].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442,yymsp[-1].minor.yy342.pExpr); + sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy442,yymsp[0].minor.yy392); +} + break; + case 122: /* sortlist ::= expr sortorder */ +{ + yymsp[-1].minor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy342.pExpr); /*A-overwrites-Y*/ + sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy442,yymsp[0].minor.yy392); +} + break; + case 123: /* sortorder ::= ASC */ +{yymsp[0].minor.yy392 = SQLITE_SO_ASC;} + break; + case 124: /* sortorder ::= DESC */ +{yymsp[0].minor.yy392 = SQLITE_SO_DESC;} + break; + case 125: /* sortorder ::= */ +{yymsp[1].minor.yy392 = SQLITE_SO_UNDEFINED;} + break; + case 130: /* limit_opt ::= */ +{yymsp[1].minor.yy64.pLimit = 0; yymsp[1].minor.yy64.pOffset = 0;} + break; + case 131: /* limit_opt ::= LIMIT expr */ +{yymsp[-1].minor.yy64.pLimit = yymsp[0].minor.yy342.pExpr; yymsp[-1].minor.yy64.pOffset = 0;} + break; + case 132: /* limit_opt ::= LIMIT expr OFFSET expr */ +{yymsp[-3].minor.yy64.pLimit = yymsp[-2].minor.yy342.pExpr; yymsp[-3].minor.yy64.pOffset = yymsp[0].minor.yy342.pExpr;} + break; + case 133: /* limit_opt ::= LIMIT expr COMMA expr */ +{yymsp[-3].minor.yy64.pOffset = yymsp[-2].minor.yy342.pExpr; yymsp[-3].minor.yy64.pLimit = yymsp[0].minor.yy342.pExpr;} + break; + case 134: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */ +{ + sqlite3WithPush(pParse, yymsp[-5].minor.yy331, 1); + sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy347, &yymsp[-1].minor.yy0); + sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy347,yymsp[0].minor.yy122); +} + break; + case 137: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */ +{ + sqlite3WithPush(pParse, yymsp[-7].minor.yy331, 1); + sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy347, &yymsp[-3].minor.yy0); + sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy442,"set list"); + sqlite3Update(pParse,yymsp[-4].minor.yy347,yymsp[-1].minor.yy442,yymsp[0].minor.yy122,yymsp[-5].minor.yy392); +} + break; + case 138: /* setlist ::= setlist COMMA nm EQ expr */ +{ + yymsp[-4].minor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy442, yymsp[0].minor.yy342.pExpr); + sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy442, &yymsp[-2].minor.yy0, 1); +} + break; + case 139: /* setlist ::= nm EQ expr */ +{ + yylhsminor.yy442 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy342.pExpr); + sqlite3ExprListSetName(pParse, yylhsminor.yy442, &yymsp[-2].minor.yy0, 1); +} + yymsp[-2].minor.yy442 = yylhsminor.yy442; + break; + case 140: /* cmd ::= with insert_cmd INTO fullname idlist_opt select */ +{ + sqlite3WithPush(pParse, yymsp[-5].minor.yy331, 1); + sqlite3Insert(pParse, yymsp[-2].minor.yy347, yymsp[0].minor.yy159, yymsp[-1].minor.yy180, yymsp[-4].minor.yy392); +} + break; + case 141: /* cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES */ +{ + sqlite3WithPush(pParse, yymsp[-6].minor.yy331, 1); + sqlite3Insert(pParse, yymsp[-3].minor.yy347, 0, yymsp[-2].minor.yy180, yymsp[-5].minor.yy392); +} + break; + case 145: /* idlist_opt ::= LP idlist RP */ +{yymsp[-2].minor.yy180 = yymsp[-1].minor.yy180;} + break; + case 146: /* idlist ::= idlist COMMA nm */ +{yymsp[-2].minor.yy180 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy180,&yymsp[0].minor.yy0);} + break; + case 147: /* idlist ::= nm */ +{yymsp[0].minor.yy180 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/} + break; + case 148: /* expr ::= LP expr RP */ +{spanSet(&yymsp[-2].minor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/ yymsp[-2].minor.yy342.pExpr = yymsp[-1].minor.yy342.pExpr;} + break; + case 149: /* term ::= NULL */ + case 154: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==154); + case 155: /* term ::= STRING */ yytestcase(yyruleno==155); +{spanExpr(&yymsp[0].minor.yy342,pParse,yymsp[0].major,yymsp[0].minor.yy0);/*A-overwrites-X*/} + break; + case 150: /* expr ::= ID|INDEXED */ + case 151: /* expr ::= JOIN_KW */ yytestcase(yyruleno==151); +{spanExpr(&yymsp[0].minor.yy342,pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/} + break; + case 152: /* expr ::= nm DOT nm */ +{ + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0); + spanSet(&yymsp[-2].minor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/ + yymsp[-2].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); +} + break; + case 153: /* expr ::= nm DOT nm DOT nm */ +{ + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0); + Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0); + spanSet(&yymsp[-4].minor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/ + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); +} + break; + case 156: /* expr ::= VARIABLE */ +{ + if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){ + spanExpr(&yymsp[0].minor.yy342, pParse, TK_VARIABLE, yymsp[0].minor.yy0); + sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy342.pExpr); + }else{ + /* When doing a nested parse, one can include terms in an expression + ** that look like this: #1 #2 ... These terms refer to registers + ** in the virtual machine. #N is the N-th register. */ + Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/ + assert( t.n>=2 ); + spanSet(&yymsp[0].minor.yy342, &t, &t); + if( pParse->nested==0 ){ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); + yymsp[0].minor.yy342.pExpr = 0; + }else{ + yymsp[0].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &t); + if( yymsp[0].minor.yy342.pExpr ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy342.pExpr->iTable); + } + } +} + break; + case 157: /* expr ::= expr COLLATE ID|STRING */ +{ + yymsp[-2].minor.yy342.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0, 1); + yymsp[-2].minor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; +} + break; + case 158: /* expr ::= CAST LP expr AS typetoken RP */ +{ + spanSet(&yymsp[-5].minor.yy342,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/ + yymsp[-5].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy342.pExpr, 0, &yymsp[-1].minor.yy0); +} + break; + case 159: /* expr ::= ID|INDEXED LP distinct exprlist RP */ +{ + if( yymsp[-1].minor.yy442 && yymsp[-1].minor.yy442->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ + sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0); + } + yylhsminor.yy342.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy442, &yymsp[-4].minor.yy0); + spanSet(&yylhsminor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); + if( yymsp[-2].minor.yy392==SF_Distinct && yylhsminor.yy342.pExpr ){ + yylhsminor.yy342.pExpr->flags |= EP_Distinct; + } +} + yymsp[-4].minor.yy342 = yylhsminor.yy342; + break; + case 160: /* expr ::= ID|INDEXED LP STAR RP */ +{ + yylhsminor.yy342.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0); + spanSet(&yylhsminor.yy342,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); +} + yymsp[-3].minor.yy342 = yylhsminor.yy342; + break; + case 161: /* term ::= CTIME_KW */ +{ + yylhsminor.yy342.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0); + spanSet(&yylhsminor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); +} + yymsp[0].minor.yy342 = yylhsminor.yy342; + break; + case 162: /* expr ::= expr AND expr */ + case 163: /* expr ::= expr OR expr */ yytestcase(yyruleno==163); + case 164: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==164); + case 165: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==165); + case 166: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==166); + case 167: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==167); + case 168: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==168); + case 169: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==169); +{spanBinaryExpr(pParse,yymsp[-1].major,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);} + break; + case 170: /* likeop ::= LIKE_KW|MATCH */ +{yymsp[0].minor.yy318.eOperator = yymsp[0].minor.yy0; yymsp[0].minor.yy318.bNot = 0;/*A-overwrites-X*/} + break; + case 171: /* likeop ::= NOT LIKE_KW|MATCH */ +{yymsp[-1].minor.yy318.eOperator = yymsp[0].minor.yy0; yymsp[-1].minor.yy318.bNot = 1;} + break; + case 172: /* expr ::= expr likeop expr */ +{ + ExprList *pList; + pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy342.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy342.pExpr); + yymsp[-2].minor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy318.eOperator); + exprNot(pParse, yymsp[-1].minor.yy318.bNot, &yymsp[-2].minor.yy342); + yymsp[-2].minor.yy342.zEnd = yymsp[0].minor.yy342.zEnd; + if( yymsp[-2].minor.yy342.pExpr ) yymsp[-2].minor.yy342.pExpr->flags |= EP_InfixFunc; +} + break; + case 173: /* expr ::= expr likeop expr ESCAPE expr */ +{ + ExprList *pList; + pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy342.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr); + yymsp[-4].minor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy318.eOperator); + exprNot(pParse, yymsp[-3].minor.yy318.bNot, &yymsp[-4].minor.yy342); + yymsp[-4].minor.yy342.zEnd = yymsp[0].minor.yy342.zEnd; + if( yymsp[-4].minor.yy342.pExpr ) yymsp[-4].minor.yy342.pExpr->flags |= EP_InfixFunc; +} + break; + case 174: /* expr ::= expr ISNULL|NOTNULL */ +{spanUnaryPostfix(pParse,yymsp[0].major,&yymsp[-1].minor.yy342,&yymsp[0].minor.yy0);} + break; + case 175: /* expr ::= expr NOT NULL */ +{spanUnaryPostfix(pParse,TK_NOTNULL,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy0);} + break; + case 176: /* expr ::= expr IS expr */ +{ + spanBinaryExpr(pParse,TK_IS,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342); + binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yymsp[-2].minor.yy342.pExpr, TK_ISNULL); +} + break; + case 177: /* expr ::= expr IS NOT expr */ +{ + spanBinaryExpr(pParse,TK_ISNOT,&yymsp[-3].minor.yy342,&yymsp[0].minor.yy342); + binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yymsp[-3].minor.yy342.pExpr, TK_NOTNULL); +} + break; + case 178: /* expr ::= NOT expr */ + case 179: /* expr ::= BITNOT expr */ yytestcase(yyruleno==179); +{spanUnaryPrefix(&yymsp[-1].minor.yy342,pParse,yymsp[-1].major,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);/*A-overwrites-B*/} + break; + case 180: /* expr ::= MINUS expr */ +{spanUnaryPrefix(&yymsp[-1].minor.yy342,pParse,TK_UMINUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);/*A-overwrites-B*/} + break; + case 181: /* expr ::= PLUS expr */ +{spanUnaryPrefix(&yymsp[-1].minor.yy342,pParse,TK_UPLUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);/*A-overwrites-B*/} + break; + case 182: /* between_op ::= BETWEEN */ + case 185: /* in_op ::= IN */ yytestcase(yyruleno==185); +{yymsp[0].minor.yy392 = 0;} + break; + case 184: /* expr ::= expr between_op expr AND expr */ +{ + ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr); + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy342.pExpr, 0, 0); + if( yymsp[-4].minor.yy342.pExpr ){ + yymsp[-4].minor.yy342.pExpr->x.pList = pList; + }else{ + sqlite3ExprListDelete(pParse->db, pList); + } + exprNot(pParse, yymsp[-3].minor.yy392, &yymsp[-4].minor.yy342); + yymsp[-4].minor.yy342.zEnd = yymsp[0].minor.yy342.zEnd; +} + break; + case 187: /* expr ::= expr in_op LP exprlist RP */ +{ + if( yymsp[-1].minor.yy442==0 ){ + /* Expressions of the form + ** + ** expr1 IN () + ** expr1 NOT IN () + ** + ** simplify to constants 0 (false) and 1 (true), respectively, + ** regardless of the value of expr1. + */ + sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy342.pExpr); + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy392]); + }else if( yymsp[-1].minor.yy442->nExpr==1 ){ + /* Expressions of the form: + ** + ** expr1 IN (?1) + ** expr1 NOT IN (?2) + ** + ** with exactly one value on the RHS can be simplified to something + ** like this: + ** + ** expr1 == ?1 + ** expr1 <> ?2 + ** + ** But, the RHS of the == or <> is marked with the EP_Generic flag + ** so that it may not contribute to the computation of comparison + ** affinity or the collating sequence to use for comparison. Otherwise, + ** the semantics would be subtly different from IN or NOT IN. + */ + Expr *pRHS = yymsp[-1].minor.yy442->a[0].pExpr; + yymsp[-1].minor.yy442->a[0].pExpr = 0; + sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy442); + /* pRHS cannot be NULL because a malloc error would have been detected + ** before now and control would have never reached this point */ + if( ALWAYS(pRHS) ){ + pRHS->flags &= ~EP_Collate; + pRHS->flags |= EP_Generic; + } + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy392 ? TK_NE : TK_EQ, yymsp[-4].minor.yy342.pExpr, pRHS, 0); + }else{ + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0); + if( yymsp[-4].minor.yy342.pExpr ){ + yymsp[-4].minor.yy342.pExpr->x.pList = yymsp[-1].minor.yy442; + sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy342.pExpr); + }else{ + sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy442); + } + exprNot(pParse, yymsp[-3].minor.yy392, &yymsp[-4].minor.yy342); + } + yymsp[-4].minor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 188: /* expr ::= LP select RP */ +{ + spanSet(&yymsp[-2].minor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/ + yymsp[-2].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); + sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy342.pExpr, yymsp[-1].minor.yy159); + } + break; + case 189: /* expr ::= expr in_op LP select RP */ +{ + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0); + sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy342.pExpr, yymsp[-1].minor.yy159); + exprNot(pParse, yymsp[-3].minor.yy392, &yymsp[-4].minor.yy342); + yymsp[-4].minor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 190: /* expr ::= expr in_op nm dbnm */ +{ + SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0); + Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); + yymsp[-3].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy342.pExpr, 0, 0); + sqlite3PExprAddSelect(pParse, yymsp[-3].minor.yy342.pExpr, pSelect); + exprNot(pParse, yymsp[-2].minor.yy392, &yymsp[-3].minor.yy342); + yymsp[-3].minor.yy342.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]; + } + break; + case 191: /* expr ::= EXISTS LP select RP */ +{ + Expr *p; + spanSet(&yymsp[-3].minor.yy342,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/ + p = yymsp[-3].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); + sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy159); + } + break; + case 192: /* expr ::= CASE case_operand case_exprlist case_else END */ +{ + spanSet(&yymsp[-4].minor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-C*/ + yymsp[-4].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy122, 0, 0); + if( yymsp[-4].minor.yy342.pExpr ){ + yymsp[-4].minor.yy342.pExpr->x.pList = yymsp[-1].minor.yy122 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy442,yymsp[-1].minor.yy122) : yymsp[-2].minor.yy442; + sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy342.pExpr); + }else{ + sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy442); + sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy122); + } +} + break; + case 193: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ +{ + yymsp[-4].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, yymsp[-2].minor.yy342.pExpr); + yymsp[-4].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, yymsp[0].minor.yy342.pExpr); +} + break; + case 194: /* case_exprlist ::= WHEN expr THEN expr */ +{ + yymsp[-3].minor.yy442 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr); + yymsp[-3].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442, yymsp[0].minor.yy342.pExpr); +} + break; + case 197: /* case_operand ::= expr */ +{yymsp[0].minor.yy122 = yymsp[0].minor.yy342.pExpr; /*A-overwrites-X*/} + break; + case 200: /* nexprlist ::= nexprlist COMMA expr */ +{yymsp[-2].minor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy442,yymsp[0].minor.yy342.pExpr);} + break; + case 201: /* nexprlist ::= expr */ +{yymsp[0].minor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy342.pExpr); /*A-overwrites-Y*/} + break; + case 202: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */ +{ + sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, + sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy442, yymsp[-10].minor.yy392, + &yymsp[-11].minor.yy0, yymsp[0].minor.yy122, SQLITE_SO_ASC, yymsp[-8].minor.yy392); +} + break; + case 203: /* uniqueflag ::= UNIQUE */ + case 244: /* raisetype ::= ABORT */ yytestcase(yyruleno==244); +{yymsp[0].minor.yy392 = OE_Abort;} + break; + case 204: /* uniqueflag ::= */ +{yymsp[1].minor.yy392 = OE_None;} + break; + case 206: /* eidlist_opt ::= LP eidlist RP */ +{yymsp[-2].minor.yy442 = yymsp[-1].minor.yy442;} + break; + case 207: /* eidlist ::= eidlist COMMA nm collate sortorder */ +{ + yymsp[-4].minor.yy442 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy442, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy392, yymsp[0].minor.yy392); +} + break; + case 208: /* eidlist ::= nm collate sortorder */ +{ + yymsp[-2].minor.yy442 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy392, yymsp[0].minor.yy392); /*A-overwrites-Y*/ +} + break; + case 211: /* cmd ::= DROP INDEX ifexists fullname */ +{sqlite3DropIndex(pParse, yymsp[0].minor.yy347, yymsp[-1].minor.yy392);} + break; + case 212: /* cmd ::= VACUUM */ + case 213: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==213); +{sqlite3Vacuum(pParse);} + break; + case 214: /* cmd ::= PRAGMA nm dbnm */ +{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);} + break; + case 215: /* cmd ::= PRAGMA nm dbnm EQ nmnum */ +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);} + break; + case 216: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);} + break; + case 217: /* cmd ::= PRAGMA nm dbnm EQ minus_num */ +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);} + break; + case 218: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */ +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);} + break; + case 221: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */ +{ + Token all; + all.z = yymsp[-3].minor.yy0.z; + all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n; + sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy327, &all); +} + break; + case 222: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */ +{ + sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy392, yymsp[-4].minor.yy410.a, yymsp[-4].minor.yy410.b, yymsp[-2].minor.yy347, yymsp[0].minor.yy122, yymsp[-10].minor.yy392, yymsp[-8].minor.yy392); + yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/ +} + break; + case 223: /* trigger_time ::= BEFORE */ +{ yymsp[0].minor.yy392 = TK_BEFORE; } + break; + case 224: /* trigger_time ::= AFTER */ +{ yymsp[0].minor.yy392 = TK_AFTER; } + break; + case 225: /* trigger_time ::= INSTEAD OF */ +{ yymsp[-1].minor.yy392 = TK_INSTEAD;} + break; + case 226: /* trigger_time ::= */ +{ yymsp[1].minor.yy392 = TK_BEFORE; } + break; + case 227: /* trigger_event ::= DELETE|INSERT */ + case 228: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==228); +{yymsp[0].minor.yy410.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy410.b = 0;} + break; + case 229: /* trigger_event ::= UPDATE OF idlist */ +{yymsp[-2].minor.yy410.a = TK_UPDATE; yymsp[-2].minor.yy410.b = yymsp[0].minor.yy180;} + break; + case 230: /* when_clause ::= */ + case 249: /* key_opt ::= */ yytestcase(yyruleno==249); +{ yymsp[1].minor.yy122 = 0; } + break; + case 231: /* when_clause ::= WHEN expr */ + case 250: /* key_opt ::= KEY expr */ yytestcase(yyruleno==250); +{ yymsp[-1].minor.yy122 = yymsp[0].minor.yy342.pExpr; } + break; + case 232: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ +{ + assert( yymsp[-2].minor.yy327!=0 ); + yymsp[-2].minor.yy327->pLast->pNext = yymsp[-1].minor.yy327; + yymsp[-2].minor.yy327->pLast = yymsp[-1].minor.yy327; +} + break; + case 233: /* trigger_cmd_list ::= trigger_cmd SEMI */ +{ + assert( yymsp[-1].minor.yy327!=0 ); + yymsp[-1].minor.yy327->pLast = yymsp[-1].minor.yy327; +} + break; + case 234: /* trnm ::= nm DOT nm */ +{ + yymsp[-2].minor.yy0 = yymsp[0].minor.yy0; + sqlite3ErrorMsg(pParse, + "qualified table names are not allowed on INSERT, UPDATE, and DELETE " + "statements within triggers"); +} + break; + case 235: /* tridxby ::= INDEXED BY nm */ +{ + sqlite3ErrorMsg(pParse, + "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " + "within triggers"); +} + break; + case 236: /* tridxby ::= NOT INDEXED */ +{ + sqlite3ErrorMsg(pParse, + "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " + "within triggers"); +} + break; + case 237: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */ +{yymsp[-6].minor.yy327 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy442, yymsp[0].minor.yy122, yymsp[-5].minor.yy392);} + break; + case 238: /* trigger_cmd ::= insert_cmd INTO trnm idlist_opt select */ +{yymsp[-4].minor.yy327 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy180, yymsp[0].minor.yy159, yymsp[-4].minor.yy392);/*A-overwrites-R*/} + break; + case 239: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */ +{yymsp[-4].minor.yy327 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy122);} + break; + case 240: /* trigger_cmd ::= select */ +{yymsp[0].minor.yy327 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy159); /*A-overwrites-X*/} + break; + case 241: /* expr ::= RAISE LP IGNORE RP */ +{ + spanSet(&yymsp[-3].minor.yy342,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/ + yymsp[-3].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); + if( yymsp[-3].minor.yy342.pExpr ){ + yymsp[-3].minor.yy342.pExpr->affinity = OE_Ignore; + } +} + break; + case 242: /* expr ::= RAISE LP raisetype COMMA nm RP */ +{ + spanSet(&yymsp[-5].minor.yy342,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/ + yymsp[-5].minor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); + if( yymsp[-5].minor.yy342.pExpr ) { + yymsp[-5].minor.yy342.pExpr->affinity = (char)yymsp[-3].minor.yy392; + } +} + break; + case 243: /* raisetype ::= ROLLBACK */ +{yymsp[0].minor.yy392 = OE_Rollback;} + break; + case 245: /* raisetype ::= FAIL */ +{yymsp[0].minor.yy392 = OE_Fail;} + break; + case 246: /* cmd ::= DROP TRIGGER ifexists fullname */ +{ + sqlite3DropTrigger(pParse,yymsp[0].minor.yy347,yymsp[-1].minor.yy392); +} + break; + case 247: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ +{ + sqlite3Attach(pParse, yymsp[-3].minor.yy342.pExpr, yymsp[-1].minor.yy342.pExpr, yymsp[0].minor.yy122); +} + break; + case 248: /* cmd ::= DETACH database_kw_opt expr */ +{ + sqlite3Detach(pParse, yymsp[0].minor.yy342.pExpr); +} + break; + case 251: /* cmd ::= REINDEX */ +{sqlite3Reindex(pParse, 0, 0);} + break; + case 252: /* cmd ::= REINDEX nm dbnm */ +{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} + break; + case 253: /* cmd ::= ANALYZE */ +{sqlite3Analyze(pParse, 0, 0);} + break; + case 254: /* cmd ::= ANALYZE nm dbnm */ +{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} + break; + case 255: /* cmd ::= ALTER TABLE fullname RENAME TO nm */ +{ + sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy347,&yymsp[0].minor.yy0); +} + break; + case 256: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */ +{ + yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n; + sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0); +} + break; + case 257: /* add_column_fullname ::= fullname */ +{ + disableLookaside(pParse); + sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy347); +} + break; + case 258: /* cmd ::= create_vtab */ +{sqlite3VtabFinishParse(pParse,0);} + break; + case 259: /* cmd ::= create_vtab LP vtabarglist RP */ +{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);} + break; + case 260: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */ +{ + sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy392); +} + break; + case 261: /* vtabarg ::= */ +{sqlite3VtabArgInit(pParse);} + break; + case 262: /* vtabargtoken ::= ANY */ + case 263: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==263); + case 264: /* lp ::= LP */ yytestcase(yyruleno==264); +{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);} + break; + case 265: /* with ::= */ +{yymsp[1].minor.yy331 = 0;} + break; + case 266: /* with ::= WITH wqlist */ +{ yymsp[-1].minor.yy331 = yymsp[0].minor.yy331; } + break; + case 267: /* with ::= WITH RECURSIVE wqlist */ +{ yymsp[-2].minor.yy331 = yymsp[0].minor.yy331; } + break; + case 268: /* wqlist ::= nm eidlist_opt AS LP select RP */ +{ + yymsp[-5].minor.yy331 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy442, yymsp[-1].minor.yy159); /*A-overwrites-X*/ +} + break; + case 269: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */ +{ + yymsp[-7].minor.yy331 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy331, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy442, yymsp[-1].minor.yy159); +} + break; + default: + /* (270) input ::= cmdlist */ yytestcase(yyruleno==270); + /* (271) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==271); + /* (272) cmdlist ::= ecmd */ yytestcase(yyruleno==272); + /* (273) ecmd ::= SEMI */ yytestcase(yyruleno==273); + /* (274) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==274); + /* (275) explain ::= */ yytestcase(yyruleno==275); + /* (276) trans_opt ::= */ yytestcase(yyruleno==276); + /* (277) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==277); + /* (278) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==278); + /* (279) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==279); + /* (280) savepoint_opt ::= */ yytestcase(yyruleno==280); + /* (281) cmd ::= create_table create_table_args */ yytestcase(yyruleno==281); + /* (282) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==282); + /* (283) columnlist ::= columnname carglist */ yytestcase(yyruleno==283); + /* (284) nm ::= ID|INDEXED */ yytestcase(yyruleno==284); + /* (285) nm ::= STRING */ yytestcase(yyruleno==285); + /* (286) nm ::= JOIN_KW */ yytestcase(yyruleno==286); + /* (287) typetoken ::= typename */ yytestcase(yyruleno==287); + /* (288) typename ::= ID|STRING */ yytestcase(yyruleno==288); + /* (289) signed ::= plus_num */ yytestcase(yyruleno==289); + /* (290) signed ::= minus_num */ yytestcase(yyruleno==290); + /* (291) carglist ::= carglist ccons */ yytestcase(yyruleno==291); + /* (292) carglist ::= */ yytestcase(yyruleno==292); + /* (293) ccons ::= NULL onconf */ yytestcase(yyruleno==293); + /* (294) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==294); + /* (295) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==295); + /* (296) conslist ::= tcons */ yytestcase(yyruleno==296); + /* (297) tconscomma ::= */ yytestcase(yyruleno==297); + /* (298) defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==298); + /* (299) resolvetype ::= raisetype */ yytestcase(yyruleno==299); + /* (300) selectnowith ::= oneselect */ yytestcase(yyruleno==300); + /* (301) oneselect ::= values */ yytestcase(yyruleno==301); + /* (302) sclp ::= selcollist COMMA */ yytestcase(yyruleno==302); + /* (303) as ::= ID|STRING */ yytestcase(yyruleno==303); + /* (304) expr ::= term */ yytestcase(yyruleno==304); + /* (305) exprlist ::= nexprlist */ yytestcase(yyruleno==305); + /* (306) nmnum ::= plus_num */ yytestcase(yyruleno==306); + /* (307) nmnum ::= nm */ yytestcase(yyruleno==307); + /* (308) nmnum ::= ON */ yytestcase(yyruleno==308); + /* (309) nmnum ::= DELETE */ yytestcase(yyruleno==309); + /* (310) nmnum ::= DEFAULT */ yytestcase(yyruleno==310); + /* (311) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==311); + /* (312) foreach_clause ::= */ yytestcase(yyruleno==312); + /* (313) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==313); + /* (314) trnm ::= nm */ yytestcase(yyruleno==314); + /* (315) tridxby ::= */ yytestcase(yyruleno==315); + /* (316) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==316); + /* (317) database_kw_opt ::= */ yytestcase(yyruleno==317); + /* (318) kwcolumn_opt ::= */ yytestcase(yyruleno==318); + /* (319) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==319); + /* (320) vtabarglist ::= vtabarg */ yytestcase(yyruleno==320); + /* (321) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==321); + /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322); + /* (323) anylist ::= */ yytestcase(yyruleno==323); + /* (324) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==324); + /* (325) anylist ::= anylist ANY */ yytestcase(yyruleno==325); + break; +/********** End reduce actions ************************************************/ + }; + assert( yyrulenoYY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; + yypParser->yyidx -= yysize - 1; + yymsp -= yysize-1; + yymsp->stateno = (YYACTIONTYPE)yyact; + yymsp->major = (YYCODETYPE)yygoto; + yyTraceShift(yypParser, yyact); + }else{ + assert( yyact == YY_ACCEPT_ACTION ); + yypParser->yyidx -= yysize; + yy_accept(yypParser); + } +} + +/* +** The following code executes when the parse fails +*/ +#ifndef YYNOERRORRECOVERY +static void yy_parse_failed( + yyParser *yypParser /* The parser */ +){ + sqlite3ParserARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will be executed whenever the + ** parser fails */ +/************ Begin %parse_failure code ***************************************/ +/************ End %parse_failure code *****************************************/ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} +#endif /* YYNOERRORRECOVERY */ + +/* +** The following code executes when a syntax error first occurs. +*/ +static void yy_syntax_error( + yyParser *yypParser, /* The parser */ + int yymajor, /* The major type of the error token */ + sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */ +){ + sqlite3ParserARG_FETCH; +#define TOKEN yyminor +/************ Begin %syntax_error code ****************************************/ + + UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ + assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); +/************ End %syntax_error code ******************************************/ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* +** The following is executed when the parser accepts +*/ +static void yy_accept( + yyParser *yypParser /* The parser */ +){ + sqlite3ParserARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will be executed whenever the + ** parser accepts */ +/*********** Begin %parse_accept code *****************************************/ +/*********** End %parse_accept code *******************************************/ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* The main parser program. +** The first argument is a pointer to a structure obtained from +** "sqlite3ParserAlloc" which describes the current state of the parser. +** The second argument is the major token number. The third is +** the minor token. The fourth optional argument is whatever the +** user wants (and specified in the grammar) and is available for +** use by the action routines. +** +** Inputs: +**
      +**
    • A pointer to the parser (an opaque structure.) +**
    • The major token number. +**
    • The minor token number. +**
    • An option argument of a grammar-specified type. +**
    +** +** Outputs: +** None. +*/ +SQLITE_PRIVATE void sqlite3Parser( + void *yyp, /* The parser */ + int yymajor, /* The major token code number */ + sqlite3ParserTOKENTYPE yyminor /* The value for the token */ + sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */ +){ + YYMINORTYPE yyminorunion; + unsigned int yyact; /* The parser action. */ +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) + int yyendofinput; /* True if we are at the end of input */ +#endif +#ifdef YYERRORSYMBOL + int yyerrorhit = 0; /* True if yymajor has invoked an error */ +#endif + yyParser *yypParser; /* The parser */ + + /* (re)initialize the parser, if necessary */ + yypParser = (yyParser*)yyp; + if( yypParser->yyidx<0 ){ +#if YYSTACKDEPTH<=0 + if( yypParser->yystksz <=0 ){ + yyStackOverflow(yypParser); + return; + } +#endif + yypParser->yyidx = 0; +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + yypParser->yystack[0].stateno = 0; + yypParser->yystack[0].major = 0; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sInitialize. Empty stack. State 0\n", + yyTracePrompt); + } +#endif + } +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) + yyendofinput = (yymajor==0); +#endif + sqlite3ParserARG_STORE; + +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]); + } +#endif + + do{ + yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); + if( yyact <= YY_MAX_SHIFTREDUCE ){ + if( yyact > YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; + yy_shift(yypParser,yyact,yymajor,yyminor); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt--; +#endif + yymajor = YYNOCODE; + }else if( yyact <= YY_MAX_REDUCE ){ + yy_reduce(yypParser,yyact-YY_MIN_REDUCE); + }else{ + assert( yyact == YY_ERROR_ACTION ); + yyminorunion.yy0 = yyminor; +#ifdef YYERRORSYMBOL + int yymx; +#endif +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); + } +#endif +#ifdef YYERRORSYMBOL + /* A syntax error has occurred. + ** The response to an error depends upon whether or not the + ** grammar defines an error token "ERROR". + ** + ** This is what we do if the grammar does define ERROR: + ** + ** * Call the %syntax_error function. + ** + ** * Begin popping the stack until we enter a state where + ** it is legal to shift the error symbol, then shift + ** the error symbol. + ** + ** * Set the error count to three. + ** + ** * Begin accepting and shifting new tokens. No new error + ** processing will occur until three tokens have been + ** shifted successfully. + ** + */ + if( yypParser->yyerrcnt<0 ){ + yy_syntax_error(yypParser,yymajor,yyminor); + } + yymx = yypParser->yystack[yypParser->yyidx].major; + if( yymx==YYERRORSYMBOL || yyerrorhit ){ +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sDiscard input token %s\n", + yyTracePrompt,yyTokenName[yymajor]); + } +#endif + yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); + yymajor = YYNOCODE; + }else{ + while( + yypParser->yyidx >= 0 && + yymx != YYERRORSYMBOL && + (yyact = yy_find_reduce_action( + yypParser->yystack[yypParser->yyidx].stateno, + YYERRORSYMBOL)) >= YY_MIN_REDUCE + ){ + yy_pop_parser_stack(yypParser); + } + if( yypParser->yyidx < 0 || yymajor==0 ){ + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yy_parse_failed(yypParser); + yymajor = YYNOCODE; + }else if( yymx!=YYERRORSYMBOL ){ + yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor); + } + } + yypParser->yyerrcnt = 3; + yyerrorhit = 1; +#elif defined(YYNOERRORRECOVERY) + /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to + ** do any kind of error recovery. Instead, simply invoke the syntax + ** error routine and continue going as if nothing had happened. + ** + ** Applications can set this macro (for example inside %include) if + ** they intend to abandon the parse upon the first syntax error seen. + */ + yy_syntax_error(yypParser,yymajor, yyminor); + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yymajor = YYNOCODE; + +#else /* YYERRORSYMBOL is not defined */ + /* This is what we do if the grammar does not define ERROR: + ** + ** * Report an error message, and throw away the input token. + ** + ** * If the input token is $, then fail the parse. + ** + ** As before, subsequent error messages are suppressed until + ** three input tokens have been successfully shifted. + */ + if( yypParser->yyerrcnt<=0 ){ + yy_syntax_error(yypParser,yymajor, yyminor); + } + yypParser->yyerrcnt = 3; + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + if( yyendofinput ){ + yy_parse_failed(yypParser); + } + yymajor = YYNOCODE; +#endif + } + }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); +#ifndef NDEBUG + if( yyTraceFILE ){ + int i; + fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); + for(i=1; i<=yypParser->yyidx; i++) + fprintf(yyTraceFILE,"%c%s", i==1 ? '[' : ' ', + yyTokenName[yypParser->yystack[i].major]); + fprintf(yyTraceFILE,"]\n"); + } +#endif + return; +} + +/************** End of parse.c ***********************************************/ +/************** Begin file tokenize.c ****************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** An tokenizer for SQL +** +** This file contains C code that splits an SQL input string up into +** individual tokens and sends those tokens one-by-one over to the +** parser for analysis. +*/ +/* #include "sqliteInt.h" */ +/* #include */ + +/* Character classes for tokenizing +** +** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented +** using a lookup table, whereas a switch() directly on c uses a binary search. +** The lookup table is much faster. To maximize speed, and to ensure that +** a lookup table is used, all of the classes need to be small integers and +** all of them need to be used within the switch. +*/ +#define CC_X 0 /* The letter 'x', or start of BLOB literal */ +#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ +#define CC_ID 2 /* unicode characters usable in IDs */ +#define CC_DIGIT 3 /* Digits */ +#define CC_DOLLAR 4 /* '$' */ +#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ +#define CC_VARNUM 6 /* '?'. Numeric SQL variables */ +#define CC_SPACE 7 /* Space characters */ +#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ +#define CC_QUOTE2 9 /* '['. [...] style quoted ids */ +#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ +#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ +#define CC_LT 12 /* '<'. Part of < or <= or <> */ +#define CC_GT 13 /* '>'. Part of > or >= */ +#define CC_EQ 14 /* '='. Part of = or == */ +#define CC_BANG 15 /* '!'. Part of != */ +#define CC_SLASH 16 /* '/'. / or c-style comment */ +#define CC_LP 17 /* '(' */ +#define CC_RP 18 /* ')' */ +#define CC_SEMI 19 /* ';' */ +#define CC_PLUS 20 /* '+' */ +#define CC_STAR 21 /* '*' */ +#define CC_PERCENT 22 /* '%' */ +#define CC_COMMA 23 /* ',' */ +#define CC_AND 24 /* '&' */ +#define CC_TILDA 25 /* '~' */ +#define CC_DOT 26 /* '.' */ +#define CC_ILLEGAL 27 /* Illegal character */ + +static const unsigned char aiClass[] = { +#ifdef SQLITE_ASCII +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ +/* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, +/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, +/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, +/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, +/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, +/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 +#endif +#ifdef SQLITE_EBCDIC +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ +/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, +/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10, +/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, +/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7, +/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, +/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, +/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, +/* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, +/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, +/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, +/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, +/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, +/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, +#endif +}; + +/* +** The charMap() macro maps alphabetic characters (only) into their +** lower-case ASCII equivalent. On ASCII machines, this is just +** an upper-to-lower case map. On EBCDIC machines we also need +** to adjust the encoding. The mapping is only valid for alphabetics +** which are the only characters for which this feature is used. +** +** Used by keywordhash.h +*/ +#ifdef SQLITE_ASCII +# define charMap(X) sqlite3UpperToLower[(unsigned char)X] +#endif +#ifdef SQLITE_EBCDIC +# define charMap(X) ebcdicToAscii[(unsigned char)X] +const unsigned char ebcdicToAscii[] = { +/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ + 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ + 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ + 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ + 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ + 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ + 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ +}; +#endif + +/* +** The sqlite3KeywordCode function looks up an identifier to determine if +** it is a keyword. If it is a keyword, the token code of that keyword is +** returned. If the input is not a keyword, TK_ID is returned. +** +** The implementation of this routine was generated by a program, +** mkkeywordhash.c, located in the tool subdirectory of the distribution. +** The output of the mkkeywordhash.c program is written into a file +** named keywordhash.h and then included into this source file by +** the #include below. +*/ +/************** Include keywordhash.h in the middle of tokenize.c ************/ +/************** Begin file keywordhash.h *************************************/ +/***** This file contains automatically generated code ****** +** +** The code in this file has been automatically generated by +** +** sqlite/tool/mkkeywordhash.c +** +** The code in this file implements a function that determines whether +** or not a given identifier is really an SQL keyword. The same thing +** might be implemented more directly using a hand-written hash table. +** But by using this automatically generated code, the size of the code +** is substantially reduced. This is important for embedded applications +** on platforms with limited memory. +*/ +/* Hash score: 182 */ +static int keywordCode(const char *z, int n, int *pType){ + /* zText[] encodes 834 bytes of keywords in 554 bytes */ + /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */ + /* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */ + /* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */ + /* UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE */ + /* BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH */ + /* IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN */ + /* WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT */ + /* CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL */ + /* FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING */ + /* VACUUMVIEWINITIALLY */ + static const char zText[553] = { + 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H', + 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G', + 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A', + 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F', + 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N', + 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I', + 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E', + 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E', + 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T', + 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q', + 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S', + 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A', + 'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E', + 'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A', + 'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A', + 'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A', + 'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J', + 'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L', + 'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E', + 'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H', + 'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E', + 'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E', + 'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M', + 'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R', + 'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A', + 'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D', + 'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O', + 'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T', + 'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R', + 'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M', + 'V','I','E','W','I','N','I','T','I','A','L','L','Y', + }; + static const unsigned char aHash[127] = { + 76, 105, 117, 74, 0, 45, 0, 0, 82, 0, 77, 0, 0, + 42, 12, 78, 15, 0, 116, 85, 54, 112, 0, 19, 0, 0, + 121, 0, 119, 115, 0, 22, 93, 0, 9, 0, 0, 70, 71, + 0, 69, 6, 0, 48, 90, 102, 0, 118, 101, 0, 0, 44, + 0, 103, 24, 0, 17, 0, 122, 53, 23, 0, 5, 110, 25, + 96, 0, 0, 124, 106, 60, 123, 57, 28, 55, 0, 91, 0, + 100, 26, 0, 99, 0, 0, 0, 95, 92, 97, 88, 109, 14, + 39, 108, 0, 81, 0, 18, 89, 111, 32, 0, 120, 80, 113, + 62, 46, 84, 0, 0, 94, 40, 59, 114, 0, 36, 0, 0, + 29, 0, 86, 63, 64, 0, 20, 61, 0, 56, + }; + static const unsigned char aNext[124] = { + 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, + 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 0, 0, 50, + 0, 43, 3, 47, 0, 0, 0, 0, 30, 0, 58, 0, 38, + 0, 0, 0, 1, 66, 0, 0, 67, 0, 41, 0, 0, 0, + 0, 0, 0, 49, 65, 0, 0, 0, 0, 31, 52, 16, 34, + 10, 0, 0, 0, 0, 0, 0, 0, 11, 72, 79, 0, 8, + 0, 104, 98, 0, 107, 0, 87, 0, 75, 51, 0, 27, 37, + 73, 83, 0, 35, 68, 0, 0, + }; + static const unsigned char aLen[124] = { + 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6, + 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6, + 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10, + 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7, + 6, 6, 5, 6, 5, 5, 9, 7, 7, 3, 2, 4, 4, + 7, 3, 6, 4, 7, 6, 12, 6, 9, 4, 6, 5, 4, + 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7, + 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8, + 2, 4, 4, 4, 4, 4, 2, 2, 6, 5, 8, 5, 8, + 3, 5, 5, 6, 4, 9, 3, + }; + static const unsigned short int aOffset[124] = { + 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33, + 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81, + 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152, + 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192, + 199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246, + 250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318, + 320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380, + 387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459, + 460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513, + 521, 524, 529, 534, 540, 544, 549, + }; + static const unsigned char aCode[124] = { + TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE, + TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN, + TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD, + TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE, + TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE, + TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW, + TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT, + TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO, + TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP, + TK_OR, TK_UNIQUE, TK_QUERY, TK_WITHOUT, TK_WITH, + TK_JOIN_KW, TK_RELEASE, TK_ATTACH, TK_HAVING, TK_GROUP, + TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RECURSIVE, TK_BETWEEN, + TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW, + TK_CASCADE, TK_ASC, TK_DELETE, TK_CASE, TK_COLLATE, + TK_CREATE, TK_CTIME_KW, TK_DETACH, TK_IMMEDIATE, TK_JOIN, + TK_INSERT, TK_MATCH, TK_PLAN, TK_ANALYZE, TK_PRAGMA, + TK_ABORT, TK_VALUES, TK_VIRTUAL, TK_LIMIT, TK_WHEN, + TK_WHERE, TK_RENAME, TK_AFTER, TK_REPLACE, TK_AND, + TK_DEFAULT, TK_AUTOINCR, TK_TO, TK_IN, TK_CAST, + TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, + TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, TK_DISTINCT, TK_IS, + TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW, + TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT, + TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING, + TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL, + }; + int i, j; + const char *zKW; + if( n>=2 ){ + i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127; + for(i=((int)aHash[i])-1; i>=0; i=((int)aNext[i])-1){ + if( aLen[i]!=n ) continue; + j = 0; + zKW = &zText[aOffset[i]]; +#ifdef SQLITE_ASCII + while( j=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) +#endif + +/* Make the IdChar function accessible from ctime.c */ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); } +#endif + + +/* +** Return the length (in bytes) of the token that begins at z[0]. +** Store the token type in *tokenType before returning. +*/ +SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){ + int i, c; + switch( aiClass[*z] ){ /* Switch on the character-class of the first byte + ** of the token. See the comment on the CC_ defines + ** above. */ + case CC_SPACE: { + testcase( z[0]==' ' ); + testcase( z[0]=='\t' ); + testcase( z[0]=='\n' ); + testcase( z[0]=='\f' ); + testcase( z[0]=='\r' ); + for(i=1; sqlite3Isspace(z[i]); i++){} + *tokenType = TK_SPACE; + return i; + } + case CC_MINUS: { + if( z[1]=='-' ){ + for(i=2; (c=z[i])!=0 && c!='\n'; i++){} + *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + return i; + } + *tokenType = TK_MINUS; + return 1; + } + case CC_LP: { + *tokenType = TK_LP; + return 1; + } + case CC_RP: { + *tokenType = TK_RP; + return 1; + } + case CC_SEMI: { + *tokenType = TK_SEMI; + return 1; + } + case CC_PLUS: { + *tokenType = TK_PLUS; + return 1; + } + case CC_STAR: { + *tokenType = TK_STAR; + return 1; + } + case CC_SLASH: { + if( z[1]!='*' || z[2]==0 ){ + *tokenType = TK_SLASH; + return 1; + } + for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} + if( c ) i++; + *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + return i; + } + case CC_PERCENT: { + *tokenType = TK_REM; + return 1; + } + case CC_EQ: { + *tokenType = TK_EQ; + return 1 + (z[1]=='='); + } + case CC_LT: { + if( (c=z[1])=='=' ){ + *tokenType = TK_LE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_NE; + return 2; + }else if( c=='<' ){ + *tokenType = TK_LSHIFT; + return 2; + }else{ + *tokenType = TK_LT; + return 1; + } + } + case CC_GT: { + if( (c=z[1])=='=' ){ + *tokenType = TK_GE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_RSHIFT; + return 2; + }else{ + *tokenType = TK_GT; + return 1; + } + } + case CC_BANG: { + if( z[1]!='=' ){ + *tokenType = TK_ILLEGAL; + return 1; + }else{ + *tokenType = TK_NE; + return 2; + } + } + case CC_PIPE: { + if( z[1]!='|' ){ + *tokenType = TK_BITOR; + return 1; + }else{ + *tokenType = TK_CONCAT; + return 2; + } + } + case CC_COMMA: { + *tokenType = TK_COMMA; + return 1; + } + case CC_AND: { + *tokenType = TK_BITAND; + return 1; + } + case CC_TILDA: { + *tokenType = TK_BITNOT; + return 1; + } + case CC_QUOTE: { + int delim = z[0]; + testcase( delim=='`' ); + testcase( delim=='\'' ); + testcase( delim=='"' ); + for(i=1; (c=z[i])!=0; i++){ + if( c==delim ){ + if( z[i+1]==delim ){ + i++; + }else{ + break; + } + } + } + if( c=='\'' ){ + *tokenType = TK_STRING; + return i+1; + }else if( c!=0 ){ + *tokenType = TK_ID; + return i+1; + }else{ + *tokenType = TK_ILLEGAL; + return i; + } + } + case CC_DOT: { +#ifndef SQLITE_OMIT_FLOATING_POINT + if( !sqlite3Isdigit(z[1]) ) +#endif + { + *tokenType = TK_DOT; + return 1; + } + /* If the next character is a digit, this is a floating point + ** number that begins with ".". Fall thru into the next case */ + } + case CC_DIGIT: { + testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); + testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); + testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); + testcase( z[0]=='9' ); + *tokenType = TK_INTEGER; +#ifndef SQLITE_OMIT_HEX_INTEGER + if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ + for(i=3; sqlite3Isxdigit(z[i]); i++){} + return i; + } +#endif + for(i=0; sqlite3Isdigit(z[i]); i++){} +#ifndef SQLITE_OMIT_FLOATING_POINT + if( z[i]=='.' ){ + i++; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } + if( (z[i]=='e' || z[i]=='E') && + ( sqlite3Isdigit(z[i+1]) + || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) + ) + ){ + i += 2; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } +#endif + while( IdChar(z[i]) ){ + *tokenType = TK_ILLEGAL; + i++; + } + return i; + } + case CC_QUOTE2: { + for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} + *tokenType = c==']' ? TK_ID : TK_ILLEGAL; + return i; + } + case CC_VARNUM: { + *tokenType = TK_VARIABLE; + for(i=1; sqlite3Isdigit(z[i]); i++){} + return i; + } + case CC_DOLLAR: + case CC_VARALPHA: { + int n = 0; + testcase( z[0]=='$' ); testcase( z[0]=='@' ); + testcase( z[0]==':' ); testcase( z[0]=='#' ); + *tokenType = TK_VARIABLE; + for(i=1; (c=z[i])!=0; i++){ + if( IdChar(c) ){ + n++; +#ifndef SQLITE_OMIT_TCL_VARIABLE + }else if( c=='(' && n>0 ){ + do{ + i++; + }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); + if( c==')' ){ + i++; + }else{ + *tokenType = TK_ILLEGAL; + } + break; + }else if( c==':' && z[i+1]==':' ){ + i++; +#endif + }else{ + break; + } + } + if( n==0 ) *tokenType = TK_ILLEGAL; + return i; + } + case CC_KYWD: { + for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} + if( IdChar(z[i]) ){ + /* This token started out using characters that can appear in keywords, + ** but z[i] is a character not allowed within keywords, so this must + ** be an identifier instead */ + i++; + break; + } + *tokenType = TK_ID; + return keywordCode((char*)z, i, tokenType); + } + case CC_X: { +#ifndef SQLITE_OMIT_BLOB_LITERAL + testcase( z[0]=='x' ); testcase( z[0]=='X' ); + if( z[1]=='\'' ){ + *tokenType = TK_BLOB; + for(i=2; sqlite3Isxdigit(z[i]); i++){} + if( z[i]!='\'' || i%2 ){ + *tokenType = TK_ILLEGAL; + while( z[i] && z[i]!='\'' ){ i++; } + } + if( z[i] ) i++; + return i; + } +#endif + /* If it is not a BLOB literal, then it must be an ID, since no + ** SQL keywords start with the letter 'x'. Fall through */ + } + case CC_ID: { + i = 1; + break; + } + default: { + *tokenType = TK_ILLEGAL; + return 1; + } + } + while( IdChar(z[i]) ){ i++; } + *tokenType = TK_ID; + return i; +} + +/* +** Run the parser on the given SQL string. The parser structure is +** passed in. An SQLITE_ status code is returned. If an error occurs +** then an and attempt is made to write an error message into +** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that +** error message. +*/ +SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ + int nErr = 0; /* Number of errors encountered */ + int i; /* Loop counter */ + void *pEngine; /* The LEMON-generated LALR(1) parser */ + int tokenType; /* type of the next token */ + int lastTokenParsed = -1; /* type of the previous token */ + sqlite3 *db = pParse->db; /* The database connection */ + int mxSqlLen; /* Max length of an SQL string */ + + assert( zSql!=0 ); + mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; + if( db->nVdbeActive==0 ){ + db->u1.isInterrupted = 0; + } + pParse->rc = SQLITE_OK; + pParse->zTail = zSql; + i = 0; + assert( pzErrMsg!=0 ); + /* sqlite3ParserTrace(stdout, "parser: "); */ + pEngine = sqlite3ParserAlloc(sqlite3Malloc); + if( pEngine==0 ){ + sqlite3OomFault(db); + return SQLITE_NOMEM_BKPT; + } + assert( pParse->pNewTable==0 ); + assert( pParse->pNewTrigger==0 ); + assert( pParse->nVar==0 ); + assert( pParse->nzVar==0 ); + assert( pParse->azVar==0 ); + while( zSql[i]!=0 ){ + assert( i>=0 ); + pParse->sLastToken.z = &zSql[i]; + pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); + i += pParse->sLastToken.n; + if( i>mxSqlLen ){ + pParse->rc = SQLITE_TOOBIG; + break; + } + if( tokenType>=TK_SPACE ){ + assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); + if( db->u1.isInterrupted ){ + pParse->rc = SQLITE_INTERRUPT; + break; + } + if( tokenType==TK_ILLEGAL ){ + sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", + &pParse->sLastToken); + break; + } + }else{ + sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); + lastTokenParsed = tokenType; + if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; + } + } + assert( nErr==0 ); + pParse->zTail = &zSql[i]; + if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ + assert( zSql[i]==0 ); + if( lastTokenParsed!=TK_SEMI ){ + sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); + } + if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ + sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); + } + } +#ifdef YYTRACKMAXSTACKDEPTH + sqlite3_mutex_enter(sqlite3MallocMutex()); + sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, + sqlite3ParserStackPeak(pEngine) + ); + sqlite3_mutex_leave(sqlite3MallocMutex()); +#endif /* YYDEBUG */ + sqlite3ParserFree(pEngine, sqlite3_free); + if( db->mallocFailed ){ + pParse->rc = SQLITE_NOMEM_BKPT; + } + if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ + pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); + } + assert( pzErrMsg!=0 ); + if( pParse->zErrMsg ){ + *pzErrMsg = pParse->zErrMsg; + sqlite3_log(pParse->rc, "%s", *pzErrMsg); + pParse->zErrMsg = 0; + nErr++; + } + if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ + sqlite3VdbeDelete(pParse->pVdbe); + pParse->pVdbe = 0; + } +#ifndef SQLITE_OMIT_SHARED_CACHE + if( pParse->nested==0 ){ + sqlite3DbFree(db, pParse->aTableLock); + pParse->aTableLock = 0; + pParse->nTableLock = 0; + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3_free(pParse->apVtabLock); +#endif + + if( !IN_DECLARE_VTAB ){ + /* If the pParse->declareVtab flag is set, do not delete any table + ** structure built up in pParse->pNewTable. The calling code (see vtab.c) + ** will take responsibility for freeing the Table structure. + */ + sqlite3DeleteTable(db, pParse->pNewTable); + } + + if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree); + sqlite3DeleteTrigger(db, pParse->pNewTrigger); + for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); + sqlite3DbFree(db, pParse->azVar); + while( pParse->pAinc ){ + AutoincInfo *p = pParse->pAinc; + pParse->pAinc = p->pNext; + sqlite3DbFree(db, p); + } + while( pParse->pZombieTab ){ + Table *p = pParse->pZombieTab; + pParse->pZombieTab = p->pNextZombie; + sqlite3DeleteTable(db, p); + } + assert( nErr==0 || pParse->rc!=SQLITE_OK ); + return nErr; +} + +/************** End of tokenize.c ********************************************/ +/************** Begin file complete.c ****************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** An tokenizer for SQL +** +** This file contains C code that implements the sqlite3_complete() API. +** This code used to be part of the tokenizer.c source file. But by +** separating it out, the code will be automatically omitted from +** static links that do not use it. +*/ +/* #include "sqliteInt.h" */ +#ifndef SQLITE_OMIT_COMPLETE + +/* +** This is defined in tokenize.c. We just have to import the definition. +*/ +#ifndef SQLITE_AMALGAMATION +#ifdef SQLITE_ASCII +#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) +#endif +#ifdef SQLITE_EBCDIC +SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[]; +#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) +#endif +#endif /* SQLITE_AMALGAMATION */ + + +/* +** Token types used by the sqlite3_complete() routine. See the header +** comments on that procedure for additional information. +*/ +#define tkSEMI 0 +#define tkWS 1 +#define tkOTHER 2 +#ifndef SQLITE_OMIT_TRIGGER +#define tkEXPLAIN 3 +#define tkCREATE 4 +#define tkTEMP 5 +#define tkTRIGGER 6 +#define tkEND 7 +#endif + +/* +** Return TRUE if the given SQL string ends in a semicolon. +** +** Special handling is require for CREATE TRIGGER statements. +** Whenever the CREATE TRIGGER keywords are seen, the statement +** must end with ";END;". +** +** This implementation uses a state machine with 8 states: +** +** (0) INVALID We have not yet seen a non-whitespace character. +** +** (1) START At the beginning or end of an SQL statement. This routine +** returns 1 if it ends in the START state and 0 if it ends +** in any other state. +** +** (2) NORMAL We are in the middle of statement which ends with a single +** semicolon. +** +** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of +** a statement. +** +** (4) CREATE The keyword CREATE has been seen at the beginning of a +** statement, possibly preceded by EXPLAIN and/or followed by +** TEMP or TEMPORARY +** +** (5) TRIGGER We are in the middle of a trigger definition that must be +** ended by a semicolon, the keyword END, and another semicolon. +** +** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at +** the end of a trigger definition. +** +** (7) END We've seen the ";END" of the ";END;" that occurs at the end +** of a trigger definition. +** +** Transitions between states above are determined by tokens extracted +** from the input. The following tokens are significant: +** +** (0) tkSEMI A semicolon. +** (1) tkWS Whitespace. +** (2) tkOTHER Any other SQL token. +** (3) tkEXPLAIN The "explain" keyword. +** (4) tkCREATE The "create" keyword. +** (5) tkTEMP The "temp" or "temporary" keyword. +** (6) tkTRIGGER The "trigger" keyword. +** (7) tkEND The "end" keyword. +** +** Whitespace never causes a state transition and is always ignored. +** This means that a SQL string of all whitespace is invalid. +** +** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed +** to recognize the end of a trigger can be omitted. All we have to do +** is look for a semicolon that is not part of an string or comment. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *zSql){ + u8 state = 0; /* Current state, using numbers defined in header comment */ + u8 token; /* Value of the next token */ + +#ifndef SQLITE_OMIT_TRIGGER + /* A complex statement machine used to detect the end of a CREATE TRIGGER + ** statement. This is the normal case. + */ + static const u8 trans[8][8] = { + /* Token: */ + /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ + /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, }, + /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, }, + /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, }, + /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, }, + /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, }, + /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, }, + /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, }, + /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, }, + }; +#else + /* If triggers are not supported by this compile then the statement machine + ** used to detect the end of a statement is much simpler + */ + static const u8 trans[3][3] = { + /* Token: */ + /* State: ** SEMI WS OTHER */ + /* 0 INVALID: */ { 1, 0, 2, }, + /* 1 START: */ { 1, 1, 2, }, + /* 2 NORMAL: */ { 1, 2, 2, }, + }; +#endif /* SQLITE_OMIT_TRIGGER */ + +#ifdef SQLITE_ENABLE_API_ARMOR + if( zSql==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + + while( *zSql ){ + switch( *zSql ){ + case ';': { /* A semicolon */ + token = tkSEMI; + break; + } + case ' ': + case '\r': + case '\t': + case '\n': + case '\f': { /* White space is ignored */ + token = tkWS; + break; + } + case '/': { /* C-style comments */ + if( zSql[1]!='*' ){ + token = tkOTHER; + break; + } + zSql += 2; + while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } + if( zSql[0]==0 ) return 0; + zSql++; + token = tkWS; + break; + } + case '-': { /* SQL-style comments from "--" to end of line */ + if( zSql[1]!='-' ){ + token = tkOTHER; + break; + } + while( *zSql && *zSql!='\n' ){ zSql++; } + if( *zSql==0 ) return state==1; + token = tkWS; + break; + } + case '[': { /* Microsoft-style identifiers in [...] */ + zSql++; + while( *zSql && *zSql!=']' ){ zSql++; } + if( *zSql==0 ) return 0; + token = tkOTHER; + break; + } + case '`': /* Grave-accent quoted symbols used by MySQL */ + case '"': /* single- and double-quoted strings */ + case '\'': { + int c = *zSql; + zSql++; + while( *zSql && *zSql!=c ){ zSql++; } + if( *zSql==0 ) return 0; + token = tkOTHER; + break; + } + default: { +#ifdef SQLITE_EBCDIC + unsigned char c; +#endif + if( IdChar((u8)*zSql) ){ + /* Keywords and unquoted identifiers */ + int nId; + for(nId=1; IdChar(zSql[nId]); nId++){} +#ifdef SQLITE_OMIT_TRIGGER + token = tkOTHER; +#else + switch( *zSql ){ + case 'c': case 'C': { + if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ + token = tkCREATE; + }else{ + token = tkOTHER; + } + break; + } + case 't': case 'T': { + if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ + token = tkTRIGGER; + }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ + token = tkTEMP; + }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ + token = tkTEMP; + }else{ + token = tkOTHER; + } + break; + } + case 'e': case 'E': { + if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ + token = tkEND; + }else +#ifndef SQLITE_OMIT_EXPLAIN + if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ + token = tkEXPLAIN; + }else +#endif + { + token = tkOTHER; + } + break; + } + default: { + token = tkOTHER; + break; + } + } +#endif /* SQLITE_OMIT_TRIGGER */ + zSql += nId-1; + }else{ + /* Operators and special symbols */ + token = tkOTHER; + } + break; + } + } + state = trans[state][token]; + zSql++; + } + return state==1; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** This routine is the same as the sqlite3_complete() routine described +** above, except that the parameter is required to be UTF-16 encoded, not +** UTF-8. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){ + sqlite3_value *pVal; + char const *zSql8; + int rc; + +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + pVal = sqlite3ValueNew(0); + sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); + zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); + if( zSql8 ){ + rc = sqlite3_complete(zSql8); + }else{ + rc = SQLITE_NOMEM_BKPT; + } + sqlite3ValueFree(pVal); + return rc & 0xff; +} +#endif /* SQLITE_OMIT_UTF16 */ +#endif /* SQLITE_OMIT_COMPLETE */ + +/************** End of complete.c ********************************************/ +/************** Begin file main.c ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Main file for the SQLite library. The routines in this file +** implement the programmer interface to the library. Routines in +** other files are for internal use by SQLite and should not be +** accessed by users of the library. +*/ +/* #include "sqliteInt.h" */ + +#ifdef SQLITE_ENABLE_FTS3 +/************** Include fts3.h in the middle of main.c ***********************/ +/************** Begin file fts3.h ********************************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** FTS3 library. All it does is declare the sqlite3Fts3Init() interface. +*/ +/* #include "sqlite3.h" */ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + +/************** End of fts3.h ************************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif +#ifdef SQLITE_ENABLE_RTREE +/************** Include rtree.h in the middle of main.c **********************/ +/************** Begin file rtree.h *******************************************/ +/* +** 2008 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** RTREE library. All it does is declare the sqlite3RtreeInit() interface. +*/ +/* #include "sqlite3.h" */ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + +/************** End of rtree.h ***********************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif +#ifdef SQLITE_ENABLE_ICU +/************** Include sqliteicu.h in the middle of main.c ******************/ +/************** Begin file sqliteicu.h ***************************************/ +/* +** 2008 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** ICU extension. All it does is declare the sqlite3IcuInit() interface. +*/ +/* #include "sqlite3.h" */ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + + +/************** End of sqliteicu.h *******************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif +#ifdef SQLITE_ENABLE_JSON1 +SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*); +#endif +#ifdef SQLITE_ENABLE_FTS5 +SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); +#endif + +#ifndef SQLITE_AMALGAMATION +/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant +** contains the text of SQLITE_VERSION macro. +*/ +SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; +#endif + +/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns +** a pointer to the to the sqlite3_version[] string constant. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void){ return sqlite3_version; } + +/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a +** pointer to a string constant whose value is the same as the +** SQLITE_SOURCE_ID C preprocessor macro. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } + +/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function +** returns an integer equal to SQLITE_VERSION_NUMBER. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } + +/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns +** zero if and only if SQLite was compiled with mutexing code omitted due to +** the SQLITE_THREADSAFE compile-time option being set to 0. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } + +/* +** When compiling the test fixture or with debugging enabled (on Win32), +** this variable being set to non-zero will cause OSTRACE macros to emit +** extra diagnostic information. +*/ +#ifdef SQLITE_HAVE_OS_TRACE +# ifndef SQLITE_DEBUG_OS_TRACE +# define SQLITE_DEBUG_OS_TRACE 0 +# endif + int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; +#endif + +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) +/* +** If the following function pointer is not NULL and if +** SQLITE_ENABLE_IOTRACE is enabled, then messages describing +** I/O active are written using this function. These messages +** are intended for debugging activity only. +*/ +SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0; +#endif + +/* +** If the following global variable points to a string which is the +** name of a directory, then that directory will be used to store +** temporary files. +** +** See also the "PRAGMA temp_store_directory" SQL command. +*/ +SQLITE_API char *sqlite3_temp_directory = 0; + +/* +** If the following global variable points to a string which is the +** name of a directory, then that directory will be used to store +** all database files specified with a relative pathname. +** +** See also the "PRAGMA data_store_directory" SQL command. +*/ +SQLITE_API char *sqlite3_data_directory = 0; + +/* +** Initialize SQLite. +** +** This routine must be called to initialize the memory allocation, +** VFS, and mutex subsystems prior to doing any serious work with +** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT +** this routine will be called automatically by key routines such as +** sqlite3_open(). +** +** This routine is a no-op except on its very first call for the process, +** or for the first call after a call to sqlite3_shutdown. +** +** The first thread to call this routine runs the initialization to +** completion. If subsequent threads call this routine before the first +** thread has finished the initialization process, then the subsequent +** threads must block until the first thread finishes with the initialization. +** +** The first thread might call this routine recursively. Recursive +** calls to this routine should not block, of course. Otherwise the +** initialization process would never complete. +** +** Let X be the first thread to enter this routine. Let Y be some other +** thread. Then while the initial invocation of this routine by X is +** incomplete, it is required that: +** +** * Calls to this routine from Y must block until the outer-most +** call by X completes. +** +** * Recursive calls to this routine from thread X return immediately +** without blocking. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){ + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ + int rc; /* Result code */ +#ifdef SQLITE_EXTRA_INIT + int bRunExtraInit = 0; /* Extra initialization needed */ +#endif + +#ifdef SQLITE_OMIT_WSD + rc = sqlite3_wsd_init(4096, 24); + if( rc!=SQLITE_OK ){ + return rc; + } +#endif + + /* If the following assert() fails on some obscure processor/compiler + ** combination, the work-around is to set the correct pointer + ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ + assert( SQLITE_PTRSIZE==sizeof(char*) ); + + /* If SQLite is already completely initialized, then this call + ** to sqlite3_initialize() should be a no-op. But the initialization + ** must be complete. So isInit must not be set until the very end + ** of this routine. + */ + if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; + + /* Make sure the mutex subsystem is initialized. If unable to + ** initialize the mutex subsystem, return early with the error. + ** If the system is so sick that we are unable to allocate a mutex, + ** there is not much SQLite is going to be able to do. + ** + ** The mutex subsystem must take care of serializing its own + ** initialization. + */ + rc = sqlite3MutexInit(); + if( rc ) return rc; + + /* Initialize the malloc() system and the recursive pInitMutex mutex. + ** This operation is protected by the STATIC_MASTER mutex. Note that + ** MutexAlloc() is called for a static mutex prior to initializing the + ** malloc subsystem - this implies that the allocation of a static + ** mutex must not require support from the malloc subsystem. + */ + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) + sqlite3_mutex_enter(pMaster); + sqlite3GlobalConfig.isMutexInit = 1; + if( !sqlite3GlobalConfig.isMallocInit ){ + rc = sqlite3MallocInit(); + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.isMallocInit = 1; + if( !sqlite3GlobalConfig.pInitMutex ){ + sqlite3GlobalConfig.pInitMutex = + sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); + if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ + rc = SQLITE_NOMEM_BKPT; + } + } + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.nRefInitMutex++; + } + sqlite3_mutex_leave(pMaster); + + /* If rc is not SQLITE_OK at this point, then either the malloc + ** subsystem could not be initialized or the system failed to allocate + ** the pInitMutex mutex. Return an error in either case. */ + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Do the rest of the initialization under the recursive mutex so + ** that we will be able to handle recursive calls into + ** sqlite3_initialize(). The recursive calls normally come through + ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other + ** recursive calls might also be possible. + ** + ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls + ** to the xInit method, so the xInit method need not be threadsafe. + ** + ** The following mutex is what serializes access to the appdef pcache xInit + ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the + ** call to sqlite3PcacheInitialize(). + */ + sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); + if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ + sqlite3GlobalConfig.inProgress = 1; +#ifdef SQLITE_ENABLE_SQLLOG + { + extern void sqlite3_init_sqllog(void); + sqlite3_init_sqllog(); + } +#endif + memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions)); + sqlite3RegisterBuiltinFunctions(); + if( sqlite3GlobalConfig.isPCacheInit==0 ){ + rc = sqlite3PcacheInitialize(); + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.isPCacheInit = 1; + rc = sqlite3OsInit(); + } + if( rc==SQLITE_OK ){ + sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, + sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); + sqlite3GlobalConfig.isInit = 1; +#ifdef SQLITE_EXTRA_INIT + bRunExtraInit = 1; +#endif + } + sqlite3GlobalConfig.inProgress = 0; + } + sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); + + /* Go back under the static mutex and clean up the recursive + ** mutex to prevent a resource leak. + */ + sqlite3_mutex_enter(pMaster); + sqlite3GlobalConfig.nRefInitMutex--; + if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ + assert( sqlite3GlobalConfig.nRefInitMutex==0 ); + sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); + sqlite3GlobalConfig.pInitMutex = 0; + } + sqlite3_mutex_leave(pMaster); + + /* The following is just a sanity check to make sure SQLite has + ** been compiled correctly. It is important to run this code, but + ** we don't want to run it too often and soak up CPU cycles for no + ** reason. So we run it once during initialization. + */ +#ifndef NDEBUG +#ifndef SQLITE_OMIT_FLOATING_POINT + /* This section of code's only "output" is via assert() statements. */ + if ( rc==SQLITE_OK ){ + u64 x = (((u64)1)<<63)-1; + double y; + assert(sizeof(x)==8); + assert(sizeof(x)==sizeof(y)); + memcpy(&y, &x, 8); + assert( sqlite3IsNaN(y) ); + } +#endif +#endif + + /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT + ** compile-time option. + */ +#ifdef SQLITE_EXTRA_INIT + if( bRunExtraInit ){ + int SQLITE_EXTRA_INIT(const char*); + rc = SQLITE_EXTRA_INIT(0); + } +#endif + + return rc; +} + +/* +** Undo the effects of sqlite3_initialize(). Must not be called while +** there are outstanding database connections or memory allocations or +** while any part of SQLite is otherwise in use in any thread. This +** routine is not threadsafe. But it is safe to invoke this routine +** on when SQLite is already shut down. If SQLite is already shut down +** when this routine is invoked, then this routine is a harmless no-op. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void){ +#ifdef SQLITE_OMIT_WSD + int rc = sqlite3_wsd_init(4096, 24); + if( rc!=SQLITE_OK ){ + return rc; + } +#endif + + if( sqlite3GlobalConfig.isInit ){ +#ifdef SQLITE_EXTRA_SHUTDOWN + void SQLITE_EXTRA_SHUTDOWN(void); + SQLITE_EXTRA_SHUTDOWN(); +#endif + sqlite3_os_end(); + sqlite3_reset_auto_extension(); + sqlite3GlobalConfig.isInit = 0; + } + if( sqlite3GlobalConfig.isPCacheInit ){ + sqlite3PcacheShutdown(); + sqlite3GlobalConfig.isPCacheInit = 0; + } + if( sqlite3GlobalConfig.isMallocInit ){ + sqlite3MallocEnd(); + sqlite3GlobalConfig.isMallocInit = 0; + +#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES + /* The heap subsystem has now been shutdown and these values are supposed + ** to be NULL or point to memory that was obtained from sqlite3_malloc(), + ** which would rely on that heap subsystem; therefore, make sure these + ** values cannot refer to heap memory that was just invalidated when the + ** heap subsystem was shutdown. This is only done if the current call to + ** this function resulted in the heap subsystem actually being shutdown. + */ + sqlite3_data_directory = 0; + sqlite3_temp_directory = 0; +#endif + } + if( sqlite3GlobalConfig.isMutexInit ){ + sqlite3MutexEnd(); + sqlite3GlobalConfig.isMutexInit = 0; + } + + return SQLITE_OK; +} + +/* +** This API allows applications to modify the global configuration of +** the SQLite library at run-time. +** +** This routine should only be called when there are no outstanding +** database connections or memory allocations. This routine is not +** threadsafe. Failure to heed these warnings can lead to unpredictable +** behavior. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){ + va_list ap; + int rc = SQLITE_OK; + + /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while + ** the SQLite library is in use. */ + if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; + + va_start(ap, op); + switch( op ){ + + /* Mutex configuration options are only available in a threadsafe + ** compile. + */ +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ + case SQLITE_CONFIG_SINGLETHREAD: { + /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to + ** Single-thread. */ + sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */ + sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ + break; + } +#endif +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ + case SQLITE_CONFIG_MULTITHREAD: { + /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to + ** Multi-thread. */ + sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ + sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ + break; + } +#endif +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ + case SQLITE_CONFIG_SERIALIZED: { + /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to + ** Serialized. */ + sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ + sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */ + break; + } +#endif +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ + case SQLITE_CONFIG_MUTEX: { + /* Specify an alternative mutex implementation */ + sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); + break; + } +#endif +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ + case SQLITE_CONFIG_GETMUTEX: { + /* Retrieve the current mutex implementation */ + *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; + break; + } +#endif + + case SQLITE_CONFIG_MALLOC: { + /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a + ** single argument which is a pointer to an instance of the + ** sqlite3_mem_methods structure. The argument specifies alternative + ** low-level memory allocation routines to be used in place of the memory + ** allocation routines built into SQLite. */ + sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); + break; + } + case SQLITE_CONFIG_GETMALLOC: { + /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a + ** single argument which is a pointer to an instance of the + ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is + ** filled with the currently defined memory allocation routines. */ + if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); + *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; + break; + } + case SQLITE_CONFIG_MEMSTATUS: { + /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes + ** single argument of type int, interpreted as a boolean, which enables + ** or disables the collection of memory allocation statistics. */ + sqlite3GlobalConfig.bMemstat = va_arg(ap, int); + break; + } + case SQLITE_CONFIG_SCRATCH: { + /* EVIDENCE-OF: R-08404-60887 There are three arguments to + ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from + ** which the scratch allocations will be drawn, the size of each scratch + ** allocation (sz), and the maximum number of scratch allocations (N). */ + sqlite3GlobalConfig.pScratch = va_arg(ap, void*); + sqlite3GlobalConfig.szScratch = va_arg(ap, int); + sqlite3GlobalConfig.nScratch = va_arg(ap, int); + break; + } + case SQLITE_CONFIG_PAGECACHE: { + /* EVIDENCE-OF: R-18761-36601 There are three arguments to + ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem), + ** the size of each page cache line (sz), and the number of cache lines + ** (N). */ + sqlite3GlobalConfig.pPage = va_arg(ap, void*); + sqlite3GlobalConfig.szPage = va_arg(ap, int); + sqlite3GlobalConfig.nPage = va_arg(ap, int); + break; + } + case SQLITE_CONFIG_PCACHE_HDRSZ: { + /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes + ** a single parameter which is a pointer to an integer and writes into + ** that integer the number of extra bytes per page required for each page + ** in SQLITE_CONFIG_PAGECACHE. */ + *va_arg(ap, int*) = + sqlite3HeaderSizeBtree() + + sqlite3HeaderSizePcache() + + sqlite3HeaderSizePcache1(); + break; + } + + case SQLITE_CONFIG_PCACHE: { + /* no-op */ + break; + } + case SQLITE_CONFIG_GETPCACHE: { + /* now an error */ + rc = SQLITE_ERROR; + break; + } + + case SQLITE_CONFIG_PCACHE2: { + /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a + ** single argument which is a pointer to an sqlite3_pcache_methods2 + ** object. This object specifies the interface to a custom page cache + ** implementation. */ + sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); + break; + } + case SQLITE_CONFIG_GETPCACHE2: { + /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a + ** single argument which is a pointer to an sqlite3_pcache_methods2 + ** object. SQLite copies of the current page cache implementation into + ** that object. */ + if( sqlite3GlobalConfig.pcache2.xInit==0 ){ + sqlite3PCacheSetDefault(); + } + *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; + break; + } + +/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only +** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or +** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + case SQLITE_CONFIG_HEAP: { + /* EVIDENCE-OF: R-19854-42126 There are three arguments to + ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the + ** number of bytes in the memory buffer, and the minimum allocation size. + */ + sqlite3GlobalConfig.pHeap = va_arg(ap, void*); + sqlite3GlobalConfig.nHeap = va_arg(ap, int); + sqlite3GlobalConfig.mnReq = va_arg(ap, int); + + if( sqlite3GlobalConfig.mnReq<1 ){ + sqlite3GlobalConfig.mnReq = 1; + }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ + /* cap min request size at 2^12 */ + sqlite3GlobalConfig.mnReq = (1<<12); + } + + if( sqlite3GlobalConfig.pHeap==0 ){ + /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) + ** is NULL, then SQLite reverts to using its default memory allocator + ** (the system malloc() implementation), undoing any prior invocation of + ** SQLITE_CONFIG_MALLOC. + ** + ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to + ** revert to its default implementation when sqlite3_initialize() is run + */ + memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); + }else{ + /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the + ** alternative memory allocator is engaged to handle all of SQLites + ** memory allocation needs. */ +#ifdef SQLITE_ENABLE_MEMSYS3 + sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); +#endif +#ifdef SQLITE_ENABLE_MEMSYS5 + sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); +#endif + } + break; + } +#endif + + case SQLITE_CONFIG_LOOKASIDE: { + sqlite3GlobalConfig.szLookaside = va_arg(ap, int); + sqlite3GlobalConfig.nLookaside = va_arg(ap, int); + break; + } + + /* Record a pointer to the logger function and its first argument. + ** The default is NULL. Logging is disabled if the function pointer is + ** NULL. + */ + case SQLITE_CONFIG_LOG: { + /* MSVC is picky about pulling func ptrs from va lists. + ** http://support.microsoft.com/kb/47961 + ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); + */ + typedef void(*LOGFUNC_t)(void*,int,const char*); + sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); + sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); + break; + } + + /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames + ** can be changed at start-time using the + ** sqlite3_config(SQLITE_CONFIG_URI,1) or + ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. + */ + case SQLITE_CONFIG_URI: { + /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single + ** argument of type int. If non-zero, then URI handling is globally + ** enabled. If the parameter is zero, then URI handling is globally + ** disabled. */ + sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); + break; + } + + case SQLITE_CONFIG_COVERING_INDEX_SCAN: { + /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN + ** option takes a single integer argument which is interpreted as a + ** boolean in order to enable or disable the use of covering indices for + ** full table scans in the query optimizer. */ + sqlite3GlobalConfig.bUseCis = va_arg(ap, int); + break; + } + +#ifdef SQLITE_ENABLE_SQLLOG + case SQLITE_CONFIG_SQLLOG: { + typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int); + sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t); + sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *); + break; + } +#endif + + case SQLITE_CONFIG_MMAP_SIZE: { + /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit + ** integer (sqlite3_int64) values that are the default mmap size limit + ** (the default setting for PRAGMA mmap_size) and the maximum allowed + ** mmap size limit. */ + sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); + sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); + /* EVIDENCE-OF: R-53367-43190 If either argument to this option is + ** negative, then that argument is changed to its compile-time default. + ** + ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be + ** silently truncated if necessary so that it does not exceed the + ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE + ** compile-time option. + */ + if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ + mxMmap = SQLITE_MAX_MMAP_SIZE; + } + if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; + if( szMmap>mxMmap) szMmap = mxMmap; + sqlite3GlobalConfig.mxMmap = mxMmap; + sqlite3GlobalConfig.szMmap = szMmap; + break; + } + +#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ + case SQLITE_CONFIG_WIN32_HEAPSIZE: { + /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit + ** unsigned integer value that specifies the maximum size of the created + ** heap. */ + sqlite3GlobalConfig.nHeap = va_arg(ap, int); + break; + } +#endif + + case SQLITE_CONFIG_PMASZ: { + sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int); + break; + } + + case SQLITE_CONFIG_STMTJRNL_SPILL: { + sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int); + break; + } + + default: { + rc = SQLITE_ERROR; + break; + } + } + va_end(ap); + return rc; +} + +/* +** Set up the lookaside buffers for a database connection. +** Return SQLITE_OK on success. +** If lookaside is already active, return SQLITE_BUSY. +** +** The sz parameter is the number of bytes in each lookaside slot. +** The cnt parameter is the number of slots. If pStart is NULL the +** space for the lookaside memory is obtained from sqlite3_malloc(). +** If pStart is not NULL then it is sz*cnt bytes of memory to use for +** the lookaside memory. +*/ +static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ +#ifndef SQLITE_OMIT_LOOKASIDE + void *pStart; + if( db->lookaside.nOut ){ + return SQLITE_BUSY; + } + /* Free any existing lookaside buffer for this handle before + ** allocating a new one so we don't have to have space for + ** both at the same time. + */ + if( db->lookaside.bMalloced ){ + sqlite3_free(db->lookaside.pStart); + } + /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger + ** than a pointer to be useful. + */ + sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ + if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; + if( cnt<0 ) cnt = 0; + if( sz==0 || cnt==0 ){ + sz = 0; + pStart = 0; + }else if( pBuf==0 ){ + sqlite3BeginBenignMalloc(); + pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ + sqlite3EndBenignMalloc(); + if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; + }else{ + pStart = pBuf; + } + db->lookaside.pStart = pStart; + db->lookaside.pFree = 0; + db->lookaside.sz = (u16)sz; + if( pStart ){ + int i; + LookasideSlot *p; + assert( sz > (int)sizeof(LookasideSlot*) ); + p = (LookasideSlot*)pStart; + for(i=cnt-1; i>=0; i--){ + p->pNext = db->lookaside.pFree; + db->lookaside.pFree = p; + p = (LookasideSlot*)&((u8*)p)[sz]; + } + db->lookaside.pEnd = p; + db->lookaside.bDisable = 0; + db->lookaside.bMalloced = pBuf==0 ?1:0; + }else{ + db->lookaside.pStart = db; + db->lookaside.pEnd = db; + db->lookaside.bDisable = 1; + db->lookaside.bMalloced = 0; + } +#endif /* SQLITE_OMIT_LOOKASIDE */ + return SQLITE_OK; +} + +/* +** Return the mutex associated with a database connection. +*/ +SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return db->mutex; +} + +/* +** Free up as much memory as we can from the given database +** connection. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3 *db){ + int i; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + for(i=0; inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + Pager *pPager = sqlite3BtreePager(pBt); + sqlite3PagerShrink(pPager); + } + } + sqlite3BtreeLeaveAll(db); + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +/* +** Flush any dirty pages in the pager-cache for any attached database +** to disk. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3 *db){ + int i; + int rc = SQLITE_OK; + int bSeenBusy = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt && sqlite3BtreeIsInTrans(pBt) ){ + Pager *pPager = sqlite3BtreePager(pBt); + rc = sqlite3PagerFlush(pPager); + if( rc==SQLITE_BUSY ){ + bSeenBusy = 1; + rc = SQLITE_OK; + } + } + } + sqlite3BtreeLeaveAll(db); + sqlite3_mutex_leave(db->mutex); + return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc); +} + +/* +** Configuration settings for an individual database connection +*/ +SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){ + va_list ap; + int rc; + va_start(ap, op); + switch( op ){ + case SQLITE_DBCONFIG_LOOKASIDE: { + void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ + int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ + int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ + rc = setupLookaside(db, pBuf, sz, cnt); + break; + } + default: { + static const struct { + int op; /* The opcode */ + u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */ + } aFlagOp[] = { + { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, + { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, + { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer }, + { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, + }; + unsigned int i; + rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ + for(i=0; iflags; + if( onoff>0 ){ + db->flags |= aFlagOp[i].mask; + }else if( onoff==0 ){ + db->flags &= ~aFlagOp[i].mask; + } + if( oldFlags!=db->flags ){ + sqlite3ExpirePreparedStatements(db); + } + if( pRes ){ + *pRes = (db->flags & aFlagOp[i].mask)!=0; + } + rc = SQLITE_OK; + break; + } + } + break; + } + } + va_end(ap); + return rc; +} + + +/* +** Return true if the buffer z[0..n-1] contains all spaces. +*/ +static int allSpaces(const char *z, int n){ + while( n>0 && z[n-1]==' ' ){ n--; } + return n==0; +} + +/* +** This is the default collating function named "BINARY" which is always +** available. +** +** If the padFlag argument is not NULL then space padding at the end +** of strings is ignored. This implements the RTRIM collation. +*/ +static int binCollFunc( + void *padFlag, + int nKey1, const void *pKey1, + int nKey2, const void *pKey2 +){ + int rc, n; + n = nKey1lastRowid; +} + +/* +** Return the number of changes in the most recent call to sqlite3_exec(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return db->nChange; +} + +/* +** Return the number of changes since the database handle was opened. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return db->nTotalChange; +} + +/* +** Close all open savepoints. This function only manipulates fields of the +** database handle object, it does not close any savepoints that may be open +** at the b-tree/pager level. +*/ +SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){ + while( db->pSavepoint ){ + Savepoint *pTmp = db->pSavepoint; + db->pSavepoint = pTmp->pNext; + sqlite3DbFree(db, pTmp); + } + db->nSavepoint = 0; + db->nStatement = 0; + db->isTransactionSavepoint = 0; +} + +/* +** Invoke the destructor function associated with FuncDef p, if any. Except, +** if this is not the last copy of the function, do not invoke it. Multiple +** copies of a single function are created when create_function() is called +** with SQLITE_ANY as the encoding. +*/ +static void functionDestroy(sqlite3 *db, FuncDef *p){ + FuncDestructor *pDestructor = p->u.pDestructor; + if( pDestructor ){ + pDestructor->nRef--; + if( pDestructor->nRef==0 ){ + pDestructor->xDestroy(pDestructor->pUserData); + sqlite3DbFree(db, pDestructor); + } + } +} + +/* +** Disconnect all sqlite3_vtab objects that belong to database connection +** db. This is called when db is being closed. +*/ +static void disconnectAllVtab(sqlite3 *db){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + int i; + HashElem *p; + sqlite3BtreeEnterAll(db); + for(i=0; inDb; i++){ + Schema *pSchema = db->aDb[i].pSchema; + if( db->aDb[i].pSchema ){ + for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ + Table *pTab = (Table *)sqliteHashData(p); + if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); + } + } + } + for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){ + Module *pMod = (Module *)sqliteHashData(p); + if( pMod->pEpoTab ){ + sqlite3VtabDisconnect(db, pMod->pEpoTab); + } + } + sqlite3VtabUnlockList(db); + sqlite3BtreeLeaveAll(db); +#else + UNUSED_PARAMETER(db); +#endif +} + +/* +** Return TRUE if database connection db has unfinalized prepared +** statements or unfinished sqlite3_backup objects. +*/ +static int connectionIsBusy(sqlite3 *db){ + int j; + assert( sqlite3_mutex_held(db->mutex) ); + if( db->pVdbe ) return 1; + for(j=0; jnDb; j++){ + Btree *pBt = db->aDb[j].pBt; + if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1; + } + return 0; +} + +/* +** Close an existing SQLite database +*/ +static int sqlite3Close(sqlite3 *db, int forceZombie){ + if( !db ){ + /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or + ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */ + return SQLITE_OK; + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + + /* Force xDisconnect calls on all virtual tables */ + disconnectAllVtab(db); + + /* If a transaction is open, the disconnectAllVtab() call above + ** will not have called the xDisconnect() method on any virtual + ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() + ** call will do so. We need to do this before the check for active + ** SQL statements below, as the v-table implementation may be storing + ** some prepared statements internally. + */ + sqlite3VtabRollback(db); + + /* Legacy behavior (sqlite3_close() behavior) is to return + ** SQLITE_BUSY if the connection can not be closed immediately. + */ + if( !forceZombie && connectionIsBusy(db) ){ + sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized " + "statements or unfinished backups"); + sqlite3_mutex_leave(db->mutex); + return SQLITE_BUSY; + } + +#ifdef SQLITE_ENABLE_SQLLOG + if( sqlite3GlobalConfig.xSqllog ){ + /* Closing the handle. Fourth parameter is passed the value 2. */ + sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); + } +#endif + + /* Convert the connection into a zombie and then close it. + */ + db->magic = SQLITE_MAGIC_ZOMBIE; + sqlite3LeaveMutexAndCloseZombie(db); + return SQLITE_OK; +} + +/* +** Two variations on the public interface for closing a database +** connection. The sqlite3_close() version returns SQLITE_BUSY and +** leaves the connection option if there are unfinalized prepared +** statements or unfinished sqlite3_backups. The sqlite3_close_v2() +** version forces the connection to become a zombie if there are +** unclosed resources, and arranges for deallocation when the last +** prepare statement or sqlite3_backup closes. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); } +SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); } + + +/* +** Close the mutex on database connection db. +** +** Furthermore, if database connection db is a zombie (meaning that there +** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and +** every sqlite3_stmt has now been finalized and every sqlite3_backup has +** finished, then free all resources. +*/ +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ + HashElem *i; /* Hash table iterator */ + int j; + + /* If there are outstanding sqlite3_stmt or sqlite3_backup objects + ** or if the connection has not yet been closed by sqlite3_close_v2(), + ** then just leave the mutex and return. + */ + if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ + sqlite3_mutex_leave(db->mutex); + return; + } + + /* If we reach this point, it means that the database connection has + ** closed all sqlite3_stmt and sqlite3_backup objects and has been + ** passed to sqlite3_close (meaning that it is a zombie). Therefore, + ** go ahead and free all resources. + */ + + /* If a transaction is open, roll it back. This also ensures that if + ** any database schemas have been modified by an uncommitted transaction + ** they are reset. And that the required b-tree mutex is held to make + ** the pager rollback and schema reset an atomic operation. */ + sqlite3RollbackAll(db, SQLITE_OK); + + /* Free any outstanding Savepoint structures. */ + sqlite3CloseSavepoints(db); + + /* Close all database connections */ + for(j=0; jnDb; j++){ + struct Db *pDb = &db->aDb[j]; + if( pDb->pBt ){ + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + if( j!=1 ){ + pDb->pSchema = 0; + } + } + } + /* Clear the TEMP schema separately and last */ + if( db->aDb[1].pSchema ){ + sqlite3SchemaClear(db->aDb[1].pSchema); + } + sqlite3VtabUnlockList(db); + + /* Free up the array of auxiliary databases */ + sqlite3CollapseDatabaseArray(db); + assert( db->nDb<=2 ); + assert( db->aDb==db->aDbStatic ); + + /* Tell the code in notify.c that the connection no longer holds any + ** locks and does not require any further unlock-notify callbacks. + */ + sqlite3ConnectionClosed(db); + + for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ + FuncDef *pNext, *p; + p = sqliteHashData(i); + do{ + functionDestroy(db, p); + pNext = p->pNext; + sqlite3DbFree(db, p); + p = pNext; + }while( p ); + } + sqlite3HashClear(&db->aFunc); + for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ + CollSeq *pColl = (CollSeq *)sqliteHashData(i); + /* Invoke any destructors registered for collation sequence user data. */ + for(j=0; j<3; j++){ + if( pColl[j].xDel ){ + pColl[j].xDel(pColl[j].pUser); + } + } + sqlite3DbFree(db, pColl); + } + sqlite3HashClear(&db->aCollSeq); +#ifndef SQLITE_OMIT_VIRTUALTABLE + for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ + Module *pMod = (Module *)sqliteHashData(i); + if( pMod->xDestroy ){ + pMod->xDestroy(pMod->pAux); + } + sqlite3VtabEponymousTableClear(db, pMod); + sqlite3DbFree(db, pMod); + } + sqlite3HashClear(&db->aModule); +#endif + + sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ + sqlite3ValueFree(db->pErr); + sqlite3CloseExtensions(db); +#if SQLITE_USER_AUTHENTICATION + sqlite3_free(db->auth.zAuthUser); + sqlite3_free(db->auth.zAuthPW); +#endif + + db->magic = SQLITE_MAGIC_ERROR; + + /* The temp-database schema is allocated differently from the other schema + ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). + ** So it needs to be freed here. Todo: Why not roll the temp schema into + ** the same sqliteMalloc() as the one that allocates the database + ** structure? + */ + sqlite3DbFree(db, db->aDb[1].pSchema); + sqlite3_mutex_leave(db->mutex); + db->magic = SQLITE_MAGIC_CLOSED; + sqlite3_mutex_free(db->mutex); + assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ + if( db->lookaside.bMalloced ){ + sqlite3_free(db->lookaside.pStart); + } + sqlite3_free(db); +} + +/* +** Rollback all database files. If tripCode is not SQLITE_OK, then +** any write cursors are invalidated ("tripped" - as in "tripping a circuit +** breaker") and made to return tripCode if there are any further +** attempts to use that cursor. Read cursors remain open and valid +** but are "saved" in case the table pages are moved around. +*/ +SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ + int i; + int inTrans = 0; + int schemaChange; + assert( sqlite3_mutex_held(db->mutex) ); + sqlite3BeginBenignMalloc(); + + /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). + ** This is important in case the transaction being rolled back has + ** modified the database schema. If the b-tree mutexes are not taken + ** here, then another shared-cache connection might sneak in between + ** the database rollback and schema reset, which can cause false + ** corruption reports in some cases. */ + sqlite3BtreeEnterAll(db); + schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; + + for(i=0; inDb; i++){ + Btree *p = db->aDb[i].pBt; + if( p ){ + if( sqlite3BtreeIsInTrans(p) ){ + inTrans = 1; + } + sqlite3BtreeRollback(p, tripCode, !schemaChange); + } + } + sqlite3VtabRollback(db); + sqlite3EndBenignMalloc(); + + if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){ + sqlite3ExpirePreparedStatements(db); + sqlite3ResetAllSchemasOfConnection(db); + } + sqlite3BtreeLeaveAll(db); + + /* Any deferred constraint violations have now been resolved. */ + db->nDeferredCons = 0; + db->nDeferredImmCons = 0; + db->flags &= ~SQLITE_DeferFKs; + + /* If one has been configured, invoke the rollback-hook callback */ + if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ + db->xRollbackCallback(db->pRollbackArg); + } +} + +/* +** Return a static string containing the name corresponding to the error code +** specified in the argument. +*/ +#if defined(SQLITE_NEED_ERR_NAME) +SQLITE_PRIVATE const char *sqlite3ErrName(int rc){ + const char *zName = 0; + int i, origRc = rc; + for(i=0; i<2 && zName==0; i++, rc &= 0xff){ + switch( rc ){ + case SQLITE_OK: zName = "SQLITE_OK"; break; + case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; + case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; + case SQLITE_PERM: zName = "SQLITE_PERM"; break; + case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; + case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break; + case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; + case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break; + case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break; + case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; + case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break; + case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; + case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; + case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; + case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break; + case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break; + case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break; + case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; + case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; + case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; + case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break; + case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break; + case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break; + case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break; + case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break; + case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break; + case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break; + case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break; + case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break; + case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break; + case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break; + case SQLITE_IOERR_CHECKRESERVEDLOCK: + zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; + case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break; + case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break; + case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break; + case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break; + case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break; + case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break; + case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break; + case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break; + case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break; + case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break; + case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break; + case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break; + case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; + case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break; + case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; + case SQLITE_FULL: zName = "SQLITE_FULL"; break; + case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; + case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break; + case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break; + case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break; + case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break; + case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; + case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; + case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; + case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; + case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; + case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break; + case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break; + case SQLITE_CONSTRAINT_FOREIGNKEY: + zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break; + case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break; + case SQLITE_CONSTRAINT_PRIMARYKEY: + zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break; + case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break; + case SQLITE_CONSTRAINT_COMMITHOOK: + zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break; + case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break; + case SQLITE_CONSTRAINT_FUNCTION: + zName = "SQLITE_CONSTRAINT_FUNCTION"; break; + case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break; + case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; + case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; + case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; + case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; + case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; + case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; + case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; + case SQLITE_ROW: zName = "SQLITE_ROW"; break; + case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break; + case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break; + case SQLITE_NOTICE_RECOVER_ROLLBACK: + zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break; + case SQLITE_WARNING: zName = "SQLITE_WARNING"; break; + case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break; + case SQLITE_DONE: zName = "SQLITE_DONE"; break; + } + } + if( zName==0 ){ + static char zBuf[50]; + sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc); + zName = zBuf; + } + return zName; +} +#endif + +/* +** Return a static string that describes the kind of error specified in the +** argument. +*/ +SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){ + static const char* const aMsg[] = { + /* SQLITE_OK */ "not an error", + /* SQLITE_ERROR */ "SQL logic error or missing database", + /* SQLITE_INTERNAL */ 0, + /* SQLITE_PERM */ "access permission denied", + /* SQLITE_ABORT */ "callback requested query abort", + /* SQLITE_BUSY */ "database is locked", + /* SQLITE_LOCKED */ "database table is locked", + /* SQLITE_NOMEM */ "out of memory", + /* SQLITE_READONLY */ "attempt to write a readonly database", + /* SQLITE_INTERRUPT */ "interrupted", + /* SQLITE_IOERR */ "disk I/O error", + /* SQLITE_CORRUPT */ "database disk image is malformed", + /* SQLITE_NOTFOUND */ "unknown operation", + /* SQLITE_FULL */ "database or disk is full", + /* SQLITE_CANTOPEN */ "unable to open database file", + /* SQLITE_PROTOCOL */ "locking protocol", + /* SQLITE_EMPTY */ "table contains no data", + /* SQLITE_SCHEMA */ "database schema has changed", + /* SQLITE_TOOBIG */ "string or blob too big", + /* SQLITE_CONSTRAINT */ "constraint failed", + /* SQLITE_MISMATCH */ "datatype mismatch", + /* SQLITE_MISUSE */ "library routine called out of sequence", + /* SQLITE_NOLFS */ "large file support is disabled", + /* SQLITE_AUTH */ "authorization denied", + /* SQLITE_FORMAT */ "auxiliary database format error", + /* SQLITE_RANGE */ "bind or column index out of range", + /* SQLITE_NOTADB */ "file is encrypted or is not a database", + }; + const char *zErr = "unknown error"; + switch( rc ){ + case SQLITE_ABORT_ROLLBACK: { + zErr = "abort due to ROLLBACK"; + break; + } + default: { + rc &= 0xff; + if( ALWAYS(rc>=0) && rcbusyTimeout; + int delay, prior; + + assert( count>=0 ); + if( count < NDELAY ){ + delay = delays[count]; + prior = totals[count]; + }else{ + delay = delays[NDELAY-1]; + prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); + } + if( prior + delay > timeout ){ + delay = timeout - prior; + if( delay<=0 ) return 0; + } + sqlite3OsSleep(db->pVfs, delay*1000); + return 1; +#else + sqlite3 *db = (sqlite3 *)ptr; + int timeout = ((sqlite3 *)ptr)->busyTimeout; + if( (count+1)*1000 > timeout ){ + return 0; + } + sqlite3OsSleep(db->pVfs, 1000000); + return 1; +#endif +} + +/* +** Invoke the given busy handler. +** +** This routine is called when an operation failed with a lock. +** If this routine returns non-zero, the lock is retried. If it +** returns 0, the operation aborts with an SQLITE_BUSY error. +*/ +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ + int rc; + if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; + rc = p->xFunc(p->pArg, p->nBusy); + if( rc==0 ){ + p->nBusy = -1; + }else{ + p->nBusy++; + } + return rc; +} + +/* +** This routine sets the busy callback for an Sqlite database to the +** given callback function with the given argument. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler( + sqlite3 *db, + int (*xBusy)(void*,int), + void *pArg +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + db->busyHandler.xFunc = xBusy; + db->busyHandler.pArg = pArg; + db->busyHandler.nBusy = 0; + db->busyTimeout = 0; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK +/* +** This routine sets the progress callback for an Sqlite database to the +** given callback function with the given argument. The progress callback will +** be invoked every nOps opcodes. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler( + sqlite3 *db, + int nOps, + int (*xProgress)(void*), + void *pArg +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( nOps>0 ){ + db->xProgress = xProgress; + db->nProgressOps = (unsigned)nOps; + db->pProgressArg = pArg; + }else{ + db->xProgress = 0; + db->nProgressOps = 0; + db->pProgressArg = 0; + } + sqlite3_mutex_leave(db->mutex); +} +#endif + + +/* +** This routine installs a default busy handler that waits for the +** specified number of milliseconds before returning 0. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3 *db, int ms){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + if( ms>0 ){ + sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); + db->busyTimeout = ms; + }else{ + sqlite3_busy_handler(db, 0, 0); + } + return SQLITE_OK; +} + +/* +** Cause any pending operation to stop at its earliest opportunity. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return; + } +#endif + db->u1.isInterrupted = 1; +} + + +/* +** This function is exactly the same as sqlite3_create_function(), except +** that it is designed to be called by internal code. The difference is +** that if a malloc() fails in sqlite3_create_function(), an error code +** is returned and the mallocFailed flag cleared. +*/ +SQLITE_PRIVATE int sqlite3CreateFunc( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int enc, + void *pUserData, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + FuncDestructor *pDestructor +){ + FuncDef *p; + int nName; + int extraFlags; + + assert( sqlite3_mutex_held(db->mutex) ); + if( zFunctionName==0 || + (xSFunc && (xFinal || xStep)) || + (!xSFunc && (xFinal && !xStep)) || + (!xSFunc && (!xFinal && xStep)) || + (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || + (255<(nName = sqlite3Strlen30( zFunctionName))) ){ + return SQLITE_MISUSE_BKPT; + } + + assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC ); + extraFlags = enc & SQLITE_DETERMINISTIC; + enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY); + +#ifndef SQLITE_OMIT_UTF16 + /* If SQLITE_UTF16 is specified as the encoding type, transform this + ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the + ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. + ** + ** If SQLITE_ANY is specified, add three versions of the function + ** to the hash table. + */ + if( enc==SQLITE_UTF16 ){ + enc = SQLITE_UTF16NATIVE; + }else if( enc==SQLITE_ANY ){ + int rc; + rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags, + pUserData, xSFunc, xStep, xFinal, pDestructor); + if( rc==SQLITE_OK ){ + rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags, + pUserData, xSFunc, xStep, xFinal, pDestructor); + } + if( rc!=SQLITE_OK ){ + return rc; + } + enc = SQLITE_UTF16BE; + } +#else + enc = SQLITE_UTF8; +#endif + + /* Check if an existing function is being overridden or deleted. If so, + ** and there are active VMs, then return SQLITE_BUSY. If a function + ** is being overridden/deleted but there are no active VMs, allow the + ** operation to continue but invalidate all precompiled statements. + */ + p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0); + if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){ + if( db->nVdbeActive ){ + sqlite3ErrorWithMsg(db, SQLITE_BUSY, + "unable to delete/modify user-function due to active statements"); + assert( !db->mallocFailed ); + return SQLITE_BUSY; + }else{ + sqlite3ExpirePreparedStatements(db); + } + } + + p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1); + assert(p || db->mallocFailed); + if( !p ){ + return SQLITE_NOMEM_BKPT; + } + + /* If an older version of the function with a configured destructor is + ** being replaced invoke the destructor function here. */ + functionDestroy(db, p); + + if( pDestructor ){ + pDestructor->nRef++; + } + p->u.pDestructor = pDestructor; + p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags; + testcase( p->funcFlags & SQLITE_DETERMINISTIC ); + p->xSFunc = xSFunc ? xSFunc : xStep; + p->xFinalize = xFinal; + p->pUserData = pUserData; + p->nArg = (u16)nArg; + return SQLITE_OK; +} + +/* +** Create new user functions. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_function( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*) +){ + return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xSFunc, xStep, + xFinal, 0); +} + +SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + void (*xDestroy)(void *) +){ + int rc = SQLITE_ERROR; + FuncDestructor *pArg = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( xDestroy ){ + pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); + if( !pArg ){ + xDestroy(p); + goto out; + } + pArg->xDestroy = xDestroy; + pArg->pUserData = p; + } + rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg); + if( pArg && pArg->nRef==0 ){ + assert( rc!=SQLITE_OK ); + xDestroy(p); + sqlite3DbFree(db, pArg); + } + + out: + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API int SQLITE_STDCALL sqlite3_create_function16( + sqlite3 *db, + const void *zFunctionName, + int nArg, + int eTextRep, + void *p, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +){ + int rc; + char *zFunc8; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); + rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0); + sqlite3DbFree(db, zFunc8); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} +#endif + + +/* +** Declare that a function has been overloaded by a virtual table. +** +** If the function already exists as a regular global function, then +** this routine is a no-op. If the function does not exist, then create +** a new one that always throws a run-time error. +** +** When virtual tables intend to provide an overloaded function, they +** should call this routine to make sure the global function exists. +** A global function must exist in order for name resolution to work +** properly. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_overload_function( + sqlite3 *db, + const char *zName, + int nArg +){ + int rc = SQLITE_OK; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){ + rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, + 0, sqlite3InvalidFunction, 0, 0, 0); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_TRACE +/* +** Register a trace function. The pArg from the previously registered trace +** is returned. +** +** A NULL trace function means that no tracing is executes. A non-NULL +** trace is a pointer to a function that is invoked at the start of each +** SQL statement. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ + void *pOld; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pOld = db->pTraceArg; + db->xTrace = xTrace; + db->pTraceArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} +/* +** Register a profile function. The pArg from the previously registered +** profile function is returned. +** +** A NULL profile function means that no profiling is executes. A non-NULL +** profile is a pointer to a function that is invoked at the conclusion of +** each SQL statement that is run. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_profile( + sqlite3 *db, + void (*xProfile)(void*,const char*,sqlite_uint64), + void *pArg +){ + void *pOld; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pOld = db->pProfileArg; + db->xProfile = xProfile; + db->pProfileArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} +#endif /* SQLITE_OMIT_TRACE */ + +/* +** Register a function to be invoked when a transaction commits. +** If the invoked function returns non-zero, then the commit becomes a +** rollback. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook( + sqlite3 *db, /* Attach the hook to this database */ + int (*xCallback)(void*), /* Function to invoke on each commit */ + void *pArg /* Argument to the function */ +){ + void *pOld; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pOld = db->pCommitArg; + db->xCommitCallback = xCallback; + db->pCommitArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} + +/* +** Register a callback to be invoked each time a row is updated, +** inserted or deleted using this database connection. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook( + sqlite3 *db, /* Attach the hook to this database */ + void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), + void *pArg /* Argument to the function */ +){ + void *pRet; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pRet = db->pUpdateArg; + db->xUpdateCallback = xCallback; + db->pUpdateArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} + +/* +** Register a callback to be invoked each time a transaction is rolled +** back by this database connection. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook( + sqlite3 *db, /* Attach the hook to this database */ + void (*xCallback)(void*), /* Callback function */ + void *pArg /* Argument to the function */ +){ + void *pRet; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pRet = db->pRollbackArg; + db->xRollbackCallback = xCallback; + db->pRollbackArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Register a callback to be invoked each time a row is updated, +** inserted or deleted using this database connection. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_preupdate_hook( + sqlite3 *db, /* Attach the hook to this database */ + void(*xCallback)( /* Callback function */ + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64), + void *pArg /* First callback argument */ +){ + void *pRet; + sqlite3_mutex_enter(db->mutex); + pRet = db->pPreUpdateArg; + db->xPreUpdateCallback = xCallback; + db->pPreUpdateArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifndef SQLITE_OMIT_WAL +/* +** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). +** Invoke sqlite3_wal_checkpoint if the number of frames in the log file +** is greater than sqlite3.pWalArg cast to an integer (the value configured by +** wal_autocheckpoint()). +*/ +SQLITE_PRIVATE int sqlite3WalDefaultHook( + void *pClientData, /* Argument */ + sqlite3 *db, /* Connection */ + const char *zDb, /* Database */ + int nFrame /* Size of WAL */ +){ + if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){ + sqlite3BeginBenignMalloc(); + sqlite3_wal_checkpoint(db, zDb); + sqlite3EndBenignMalloc(); + } + return SQLITE_OK; +} +#endif /* SQLITE_OMIT_WAL */ + +/* +** Configure an sqlite3_wal_hook() callback to automatically checkpoint +** a database after committing a transaction if there are nFrame or +** more frames in the log file. Passing zero or a negative value as the +** nFrame parameter disables automatic checkpoints entirely. +** +** The callback registered by this function replaces any existing callback +** registered using sqlite3_wal_hook(). Likewise, registering a callback +** using sqlite3_wal_hook() disables the automatic checkpoint mechanism +** configured by this function. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ +#ifdef SQLITE_OMIT_WAL + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(nFrame); +#else +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + if( nFrame>0 ){ + sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); + }else{ + sqlite3_wal_hook(db, 0, 0); + } +#endif + return SQLITE_OK; +} + +/* +** Register a callback to be invoked each time a transaction is written +** into the write-ahead-log by this database connection. +*/ +SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook( + sqlite3 *db, /* Attach the hook to this db handle */ + int(*xCallback)(void *, sqlite3*, const char*, int), + void *pArg /* First argument passed to xCallback() */ +){ +#ifndef SQLITE_OMIT_WAL + void *pRet; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pRet = db->pWalArg; + db->xWalCallback = xCallback; + db->pWalArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +#else + return 0; +#endif +} + +/* +** Checkpoint database zDb. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of attached database (or NULL) */ + int eMode, /* SQLITE_CHECKPOINT_* value */ + int *pnLog, /* OUT: Size of WAL log in frames */ + int *pnCkpt /* OUT: Total number of frames checkpointed */ +){ +#ifdef SQLITE_OMIT_WAL + return SQLITE_OK; +#else + int rc; /* Return code */ + int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + + /* Initialize the output variables to -1 in case an error occurs. */ + if( pnLog ) *pnLog = -1; + if( pnCkpt ) *pnCkpt = -1; + + assert( SQLITE_CHECKPOINT_PASSIVE==0 ); + assert( SQLITE_CHECKPOINT_FULL==1 ); + assert( SQLITE_CHECKPOINT_RESTART==2 ); + assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); + if( eModeSQLITE_CHECKPOINT_TRUNCATE ){ + /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint + ** mode: */ + return SQLITE_MISUSE; + } + + sqlite3_mutex_enter(db->mutex); + if( zDb && zDb[0] ){ + iDb = sqlite3FindDbName(db, zDb); + } + if( iDb<0 ){ + rc = SQLITE_ERROR; + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb); + }else{ + db->busyHandler.nBusy = 0; + rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); + sqlite3Error(db, rc); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +#endif +} + + +/* +** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points +** to contains a zero-length string, all attached databases are +** checkpointed. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ + /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to + ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */ + return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0); +} + +#ifndef SQLITE_OMIT_WAL +/* +** Run a checkpoint on database iDb. This is a no-op if database iDb is +** not currently open in WAL mode. +** +** If a transaction is open on the database being checkpointed, this +** function returns SQLITE_LOCKED and a checkpoint is not attempted. If +** an error occurs while running the checkpoint, an SQLite error code is +** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. +** +** The mutex on database handle db should be held by the caller. The mutex +** associated with the specific b-tree being checkpointed is taken by +** this function while the checkpoint is running. +** +** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are +** checkpointed. If an error is encountered it is returned immediately - +** no attempt is made to checkpoint any remaining databases. +** +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +*/ +SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ + int rc = SQLITE_OK; /* Return code */ + int i; /* Used to iterate through attached dbs */ + int bBusy = 0; /* True if SQLITE_BUSY has been encountered */ + + assert( sqlite3_mutex_held(db->mutex) ); + assert( !pnLog || *pnLog==-1 ); + assert( !pnCkpt || *pnCkpt==-1 ); + + for(i=0; inDb && rc==SQLITE_OK; i++){ + if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ + rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); + pnLog = 0; + pnCkpt = 0; + if( rc==SQLITE_BUSY ){ + bBusy = 1; + rc = SQLITE_OK; + } + } + } + + return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc; +} +#endif /* SQLITE_OMIT_WAL */ + +/* +** This function returns true if main-memory should be used instead of +** a temporary file for transient pager files and statement journals. +** The value returned depends on the value of db->temp_store (runtime +** parameter) and the compile time value of SQLITE_TEMP_STORE. The +** following table describes the relationship between these two values +** and this functions return value. +** +** SQLITE_TEMP_STORE db->temp_store Location of temporary database +** ----------------- -------------- ------------------------------ +** 0 any file (return 0) +** 1 1 file (return 0) +** 1 2 memory (return 1) +** 1 0 file (return 0) +** 2 1 file (return 0) +** 2 2 memory (return 1) +** 2 0 memory (return 1) +** 3 any memory (return 1) +*/ +SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){ +#if SQLITE_TEMP_STORE==1 + return ( db->temp_store==2 ); +#endif +#if SQLITE_TEMP_STORE==2 + return ( db->temp_store!=1 ); +#endif +#if SQLITE_TEMP_STORE==3 + UNUSED_PARAMETER(db); + return 1; +#endif +#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 + UNUSED_PARAMETER(db); + return 0; +#endif +} + +/* +** Return UTF-8 encoded English language explanation of the most recent +** error. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3 *db){ + const char *z; + if( !db ){ + return sqlite3ErrStr(SQLITE_NOMEM_BKPT); + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return sqlite3ErrStr(SQLITE_MISUSE_BKPT); + } + sqlite3_mutex_enter(db->mutex); + if( db->mallocFailed ){ + z = sqlite3ErrStr(SQLITE_NOMEM_BKPT); + }else{ + testcase( db->pErr==0 ); + z = (char*)sqlite3_value_text(db->pErr); + assert( !db->mallocFailed ); + if( z==0 ){ + z = sqlite3ErrStr(db->errCode); + } + } + sqlite3_mutex_leave(db->mutex); + return z; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Return UTF-16 encoded English language explanation of the most recent +** error. +*/ +SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){ + static const u16 outOfMem[] = { + 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 + }; + static const u16 misuse[] = { + 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', + 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', + 'c', 'a', 'l', 'l', 'e', 'd', ' ', + 'o', 'u', 't', ' ', + 'o', 'f', ' ', + 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 + }; + + const void *z; + if( !db ){ + return (void *)outOfMem; + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return (void *)misuse; + } + sqlite3_mutex_enter(db->mutex); + if( db->mallocFailed ){ + z = (void *)outOfMem; + }else{ + z = sqlite3_value_text16(db->pErr); + if( z==0 ){ + sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode)); + z = sqlite3_value_text16(db->pErr); + } + /* A malloc() may have failed within the call to sqlite3_value_text16() + ** above. If this is the case, then the db->mallocFailed flag needs to + ** be cleared before returning. Do this directly, instead of via + ** sqlite3ApiExit(), to avoid setting the database handle error message. + */ + sqlite3OomClear(db); + } + sqlite3_mutex_leave(db->mutex); + return z; +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Return the most recent error code generated by an SQLite routine. If NULL is +** passed to this function, we assume a malloc() failed during sqlite3_open(). +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db){ + if( db && !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + if( !db || db->mallocFailed ){ + return SQLITE_NOMEM_BKPT; + } + return db->errCode & db->errMask; +} +SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db){ + if( db && !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + if( !db || db->mallocFailed ){ + return SQLITE_NOMEM_BKPT; + } + return db->errCode; +} +SQLITE_API int SQLITE_STDCALL sqlite3_system_errno(sqlite3 *db){ + return db ? db->iSysErrno : 0; +} + +/* +** Return a string that describes the kind of error specified in the +** argument. For now, this simply calls the internal sqlite3ErrStr() +** function. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int rc){ + return sqlite3ErrStr(rc); +} + +/* +** Create a new collating function for database "db". The name is zName +** and the encoding is enc. +*/ +static int createCollation( + sqlite3* db, + const char *zName, + u8 enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDel)(void*) +){ + CollSeq *pColl; + int enc2; + + assert( sqlite3_mutex_held(db->mutex) ); + + /* If SQLITE_UTF16 is specified as the encoding type, transform this + ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the + ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. + */ + enc2 = enc; + testcase( enc2==SQLITE_UTF16 ); + testcase( enc2==SQLITE_UTF16_ALIGNED ); + if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ + enc2 = SQLITE_UTF16NATIVE; + } + if( enc2SQLITE_UTF16BE ){ + return SQLITE_MISUSE_BKPT; + } + + /* Check if this call is removing or replacing an existing collation + ** sequence. If so, and there are active VMs, return busy. If there + ** are no active VMs, invalidate any pre-compiled statements. + */ + pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); + if( pColl && pColl->xCmp ){ + if( db->nVdbeActive ){ + sqlite3ErrorWithMsg(db, SQLITE_BUSY, + "unable to delete/modify collation sequence due to active statements"); + return SQLITE_BUSY; + } + sqlite3ExpirePreparedStatements(db); + + /* If collation sequence pColl was created directly by a call to + ** sqlite3_create_collation, and not generated by synthCollSeq(), + ** then any copies made by synthCollSeq() need to be invalidated. + ** Also, collation destructor - CollSeq.xDel() - function may need + ** to be called. + */ + if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ + CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); + int j; + for(j=0; j<3; j++){ + CollSeq *p = &aColl[j]; + if( p->enc==pColl->enc ){ + if( p->xDel ){ + p->xDel(p->pUser); + } + p->xCmp = 0; + } + } + } + } + + pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); + if( pColl==0 ) return SQLITE_NOMEM_BKPT; + pColl->xCmp = xCompare; + pColl->pUser = pCtx; + pColl->xDel = xDel; + pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); + sqlite3Error(db, SQLITE_OK); + return SQLITE_OK; +} + + +/* +** This array defines hard upper bounds on limit values. The +** initializer must be kept in sync with the SQLITE_LIMIT_* +** #defines in sqlite3.h. +*/ +static const int aHardLimit[] = { + SQLITE_MAX_LENGTH, + SQLITE_MAX_SQL_LENGTH, + SQLITE_MAX_COLUMN, + SQLITE_MAX_EXPR_DEPTH, + SQLITE_MAX_COMPOUND_SELECT, + SQLITE_MAX_VDBE_OP, + SQLITE_MAX_FUNCTION_ARG, + SQLITE_MAX_ATTACHED, + SQLITE_MAX_LIKE_PATTERN_LENGTH, + SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */ + SQLITE_MAX_TRIGGER_DEPTH, + SQLITE_MAX_WORKER_THREADS, +}; + +/* +** Make sure the hard limits are set to reasonable values +*/ +#if SQLITE_MAX_LENGTH<100 +# error SQLITE_MAX_LENGTH must be at least 100 +#endif +#if SQLITE_MAX_SQL_LENGTH<100 +# error SQLITE_MAX_SQL_LENGTH must be at least 100 +#endif +#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH +# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH +#endif +#if SQLITE_MAX_COMPOUND_SELECT<2 +# error SQLITE_MAX_COMPOUND_SELECT must be at least 2 +#endif +#if SQLITE_MAX_VDBE_OP<40 +# error SQLITE_MAX_VDBE_OP must be at least 40 +#endif +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127 +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127 +#endif +#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 +# error SQLITE_MAX_ATTACHED must be between 0 and 125 +#endif +#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 +# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 +#endif +#if SQLITE_MAX_COLUMN>32767 +# error SQLITE_MAX_COLUMN must not exceed 32767 +#endif +#if SQLITE_MAX_TRIGGER_DEPTH<1 +# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 +#endif +#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50 +# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50 +#endif + + +/* +** Change the value of a limit. Report the old value. +** If an invalid limit index is supplied, report -1. +** Make no changes but still report the old value if the +** new limit is negative. +** +** A new lower limit does not shrink existing constructs. +** It merely prevents new constructs that exceed the limit +** from forming. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ + int oldLimit; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return -1; + } +#endif + + /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME + ** there is a hard upper bound set at compile-time by a C preprocessor + ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to + ** "_MAX_".) + */ + assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); + assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); + assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); + assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); + assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); + assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); + assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== + SQLITE_MAX_LIKE_PATTERN_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); + assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); + assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); + assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) ); + + + if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ + return -1; + } + oldLimit = db->aLimit[limitId]; + if( newLimit>=0 ){ /* IMP: R-52476-28732 */ + if( newLimit>aHardLimit[limitId] ){ + newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ + } + db->aLimit[limitId] = newLimit; + } + return oldLimit; /* IMP: R-53341-35419 */ +} + +/* +** This function is used to parse both URIs and non-URI filenames passed by the +** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database +** URIs specified as part of ATTACH statements. +** +** The first argument to this function is the name of the VFS to use (or +** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" +** query parameter. The second argument contains the URI (or non-URI filename) +** itself. When this function is called the *pFlags variable should contain +** the default flags to open the database handle with. The value stored in +** *pFlags may be updated before returning if the URI filename contains +** "cache=xxx" or "mode=xxx" query parameters. +** +** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to +** the VFS that should be used to open the database file. *pzFile is set to +** point to a buffer containing the name of the file to open. It is the +** responsibility of the caller to eventually call sqlite3_free() to release +** this buffer. +** +** If an error occurs, then an SQLite error code is returned and *pzErrMsg +** may be set to point to a buffer containing an English language error +** message. It is the responsibility of the caller to eventually release +** this buffer by calling sqlite3_free(). +*/ +SQLITE_PRIVATE int sqlite3ParseUri( + const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ + const char *zUri, /* Nul-terminated URI to parse */ + unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ + sqlite3_vfs **ppVfs, /* OUT: VFS to use */ + char **pzFile, /* OUT: Filename component of URI */ + char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ +){ + int rc = SQLITE_OK; + unsigned int flags = *pFlags; + const char *zVfs = zDefaultVfs; + char *zFile; + char c; + int nUri = sqlite3Strlen30(zUri); + + assert( *pzErrMsg==0 ); + + if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ + || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ + && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ + ){ + char *zOpt; + int eState; /* Parser state when parsing URI */ + int iIn; /* Input character index */ + int iOut = 0; /* Output character index */ + u64 nByte = nUri+2; /* Bytes of space to allocate */ + + /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen + ** method that there may be extra parameters following the file-name. */ + flags |= SQLITE_OPEN_URI; + + for(iIn=0; iIn=0 && octet<256 ); + if( octet==0 ){ + /* This branch is taken when "%00" appears within the URI. In this + ** case we ignore all text in the remainder of the path, name or + ** value currently being parsed. So ignore the current character + ** and skip to the next "?", "=" or "&", as appropriate. */ + while( (c = zUri[iIn])!=0 && c!='#' + && (eState!=0 || c!='?') + && (eState!=1 || (c!='=' && c!='&')) + && (eState!=2 || c!='&') + ){ + iIn++; + } + continue; + } + c = octet; + }else if( eState==1 && (c=='&' || c=='=') ){ + if( zFile[iOut-1]==0 ){ + /* An empty option name. Ignore this option altogether. */ + while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++; + continue; + } + if( c=='&' ){ + zFile[iOut++] = '\0'; + }else{ + eState = 2; + } + c = 0; + }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){ + c = 0; + eState = 1; + } + zFile[iOut++] = c; + } + if( eState==1 ) zFile[iOut++] = '\0'; + zFile[iOut++] = '\0'; + zFile[iOut++] = '\0'; + + /* Check if there were any options specified that should be interpreted + ** here. Options that are interpreted here include "vfs" and those that + ** correspond to flags that may be passed to the sqlite3_open_v2() + ** method. */ + zOpt = &zFile[sqlite3Strlen30(zFile)+1]; + while( zOpt[0] ){ + int nOpt = sqlite3Strlen30(zOpt); + char *zVal = &zOpt[nOpt+1]; + int nVal = sqlite3Strlen30(zVal); + + if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){ + zVfs = zVal; + }else{ + struct OpenMode { + const char *z; + int mode; + } *aMode = 0; + char *zModeType = 0; + int mask = 0; + int limit = 0; + + if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){ + static struct OpenMode aCacheMode[] = { + { "shared", SQLITE_OPEN_SHAREDCACHE }, + { "private", SQLITE_OPEN_PRIVATECACHE }, + { 0, 0 } + }; + + mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE; + aMode = aCacheMode; + limit = mask; + zModeType = "cache"; + } + if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ + static struct OpenMode aOpenMode[] = { + { "ro", SQLITE_OPEN_READONLY }, + { "rw", SQLITE_OPEN_READWRITE }, + { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, + { "memory", SQLITE_OPEN_MEMORY }, + { 0, 0 } + }; + + mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE + | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY; + aMode = aOpenMode; + limit = mask & flags; + zModeType = "access"; + } + + if( aMode ){ + int i; + int mode = 0; + for(i=0; aMode[i].z; i++){ + const char *z = aMode[i].z; + if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){ + mode = aMode[i].mode; + break; + } + } + if( mode==0 ){ + *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal); + rc = SQLITE_ERROR; + goto parse_uri_out; + } + if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){ + *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s", + zModeType, zVal); + rc = SQLITE_PERM; + goto parse_uri_out; + } + flags = (flags & ~mask) | mode; + } + } + + zOpt = &zVal[nVal+1]; + } + + }else{ + zFile = sqlite3_malloc64(nUri+2); + if( !zFile ) return SQLITE_NOMEM_BKPT; + memcpy(zFile, zUri, nUri); + zFile[nUri] = '\0'; + zFile[nUri+1] = '\0'; + flags &= ~SQLITE_OPEN_URI; + } + + *ppVfs = sqlite3_vfs_find(zVfs); + if( *ppVfs==0 ){ + *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); + rc = SQLITE_ERROR; + } + parse_uri_out: + if( rc!=SQLITE_OK ){ + sqlite3_free(zFile); + zFile = 0; + } + *pFlags = flags; + *pzFile = zFile; + return rc; +} + + +/* +** This routine does the work of opening a database on behalf of +** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" +** is UTF-8 encoded. +*/ +static int openDatabase( + const char *zFilename, /* Database filename UTF-8 encoded */ + sqlite3 **ppDb, /* OUT: Returned database handle */ + unsigned int flags, /* Operational flags */ + const char *zVfs /* Name of the VFS to use */ +){ + sqlite3 *db; /* Store allocated handle here */ + int rc; /* Return code */ + int isThreadsafe; /* True for threadsafe connections */ + char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ + char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ + +#ifdef SQLITE_ENABLE_API_ARMOR + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; +#endif + *ppDb = 0; +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + + /* Only allow sensible combinations of bits in the flags argument. + ** Throw an error if any non-sense combination is used. If we + ** do not block illegal combinations here, it could trigger + ** assert() statements in deeper layers. Sensible combinations + ** are: + ** + ** 1: SQLITE_OPEN_READONLY + ** 2: SQLITE_OPEN_READWRITE + ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + */ + assert( SQLITE_OPEN_READONLY == 0x01 ); + assert( SQLITE_OPEN_READWRITE == 0x02 ); + assert( SQLITE_OPEN_CREATE == 0x04 ); + testcase( (1<<(flags&7))==0x02 ); /* READONLY */ + testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ + testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ + if( ((1<<(flags&7)) & 0x46)==0 ){ + return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ + } + + if( sqlite3GlobalConfig.bCoreMutex==0 ){ + isThreadsafe = 0; + }else if( flags & SQLITE_OPEN_NOMUTEX ){ + isThreadsafe = 0; + }else if( flags & SQLITE_OPEN_FULLMUTEX ){ + isThreadsafe = 1; + }else{ + isThreadsafe = sqlite3GlobalConfig.bFullMutex; + } + if( flags & SQLITE_OPEN_PRIVATECACHE ){ + flags &= ~SQLITE_OPEN_SHAREDCACHE; + }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ + flags |= SQLITE_OPEN_SHAREDCACHE; + } + + /* Remove harmful bits from the flags parameter + ** + ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were + ** dealt with in the previous code block. Besides these, the only + ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, + ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, + ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask + ** off all other flags. + */ + flags &= ~( SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_EXCLUSIVE | + SQLITE_OPEN_MAIN_DB | + SQLITE_OPEN_TEMP_DB | + SQLITE_OPEN_TRANSIENT_DB | + SQLITE_OPEN_MAIN_JOURNAL | + SQLITE_OPEN_TEMP_JOURNAL | + SQLITE_OPEN_SUBJOURNAL | + SQLITE_OPEN_MASTER_JOURNAL | + SQLITE_OPEN_NOMUTEX | + SQLITE_OPEN_FULLMUTEX | + SQLITE_OPEN_WAL + ); + + /* Allocate the sqlite data structure */ + db = sqlite3MallocZero( sizeof(sqlite3) ); + if( db==0 ) goto opendb_out; + if( isThreadsafe ){ + db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); + if( db->mutex==0 ){ + sqlite3_free(db); + db = 0; + goto opendb_out; + } + } + sqlite3_mutex_enter(db->mutex); + db->errMask = 0xff; + db->nDb = 2; + db->magic = SQLITE_MAGIC_BUSY; + db->aDb = db->aDbStatic; + + assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); + memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); + db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS; + db->autoCommit = 1; + db->nextAutovac = -1; + db->szMmap = sqlite3GlobalConfig.szMmap; + db->nextPagesize = 0; + db->nMaxSorterMmap = 0x7FFFFFFF; + db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill +#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX + | SQLITE_AutoIndex +#endif +#if SQLITE_DEFAULT_CKPTFULLFSYNC + | SQLITE_CkptFullFSync +#endif +#if SQLITE_DEFAULT_FILE_FORMAT<4 + | SQLITE_LegacyFileFmt +#endif +#ifdef SQLITE_ENABLE_LOAD_EXTENSION + | SQLITE_LoadExtension +#endif +#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS + | SQLITE_RecTriggers +#endif +#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS + | SQLITE_ForeignKeys +#endif +#if defined(SQLITE_REVERSE_UNORDERED_SELECTS) + | SQLITE_ReverseOrder +#endif +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) + | SQLITE_CellSizeCk +#endif +#if defined(SQLITE_ENABLE_FTS3_TOKENIZER) + | SQLITE_Fts3Tokenizer +#endif + ; + sqlite3HashInit(&db->aCollSeq); +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3HashInit(&db->aModule); +#endif + + /* Add the default collation sequence BINARY. BINARY works for both UTF-8 + ** and UTF-16, so add a version for each to avoid any unnecessary + ** conversions. The only error that can occur here is a malloc() failure. + ** + ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating + ** functions: + */ + createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0); + createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0); + createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0); + createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); + createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); + if( db->mallocFailed ){ + goto opendb_out; + } + /* EVIDENCE-OF: R-08308-17224 The default collating function for all + ** strings is BINARY. + */ + db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0); + assert( db->pDfltColl!=0 ); + + /* Parse the filename/URI argument. */ + db->openFlags = flags; + rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); + sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); + sqlite3_free(zErrMsg); + goto opendb_out; + } + + /* Open the backend database driver */ + rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, + flags | SQLITE_OPEN_MAIN_DB); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_IOERR_NOMEM ){ + rc = SQLITE_NOMEM_BKPT; + } + sqlite3Error(db, rc); + goto opendb_out; + } + sqlite3BtreeEnter(db->aDb[0].pBt); + db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); + if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db); + sqlite3BtreeLeave(db->aDb[0].pBt); + db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); + + /* The default safety_level for the main database is FULL; for the temp + ** database it is OFF. This matches the pager layer defaults. + */ + db->aDb[0].zName = "main"; + db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; + db->aDb[1].zName = "temp"; + db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF; + + db->magic = SQLITE_MAGIC_OPEN; + if( db->mallocFailed ){ + goto opendb_out; + } + + /* Register all built-in functions, but do not attempt to read the + ** database schema yet. This is delayed until the first time the database + ** is accessed. + */ + sqlite3Error(db, SQLITE_OK); + sqlite3RegisterPerConnectionBuiltinFunctions(db); + + /* Load automatic extensions - extensions that have been registered + ** using the sqlite3_automatic_extension() API. + */ + rc = sqlite3_errcode(db); + if( rc==SQLITE_OK ){ + sqlite3AutoLoadExtensions(db); + rc = sqlite3_errcode(db); + if( rc!=SQLITE_OK ){ + goto opendb_out; + } + } + +#ifdef SQLITE_ENABLE_FTS1 + if( !db->mallocFailed ){ + extern int sqlite3Fts1Init(sqlite3*); + rc = sqlite3Fts1Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_FTS2 + if( !db->mallocFailed && rc==SQLITE_OK ){ + extern int sqlite3Fts2Init(sqlite3*); + rc = sqlite3Fts2Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */ + if( !db->mallocFailed && rc==SQLITE_OK ){ + rc = sqlite3Fts3Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_FTS5 + if( !db->mallocFailed && rc==SQLITE_OK ){ + rc = sqlite3Fts5Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_ICU + if( !db->mallocFailed && rc==SQLITE_OK ){ + rc = sqlite3IcuInit(db); + } +#endif + +#ifdef SQLITE_ENABLE_RTREE + if( !db->mallocFailed && rc==SQLITE_OK){ + rc = sqlite3RtreeInit(db); + } +#endif + +#ifdef SQLITE_ENABLE_DBSTAT_VTAB + if( !db->mallocFailed && rc==SQLITE_OK){ + rc = sqlite3DbstatRegister(db); + } +#endif + +#ifdef SQLITE_ENABLE_JSON1 + if( !db->mallocFailed && rc==SQLITE_OK){ + rc = sqlite3Json1Init(db); + } +#endif + + /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking + ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking + ** mode. Doing nothing at all also makes NORMAL the default. + */ +#ifdef SQLITE_DEFAULT_LOCKING_MODE + db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; + sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), + SQLITE_DEFAULT_LOCKING_MODE); +#endif + + if( rc ) sqlite3Error(db, rc); + + /* Enable the lookaside-malloc subsystem */ + setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, + sqlite3GlobalConfig.nLookaside); + + sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); + +opendb_out: + if( db ){ + assert( db->mutex!=0 || isThreadsafe==0 + || sqlite3GlobalConfig.bFullMutex==0 ); + sqlite3_mutex_leave(db->mutex); + } + rc = sqlite3_errcode(db); + assert( db!=0 || rc==SQLITE_NOMEM ); + if( rc==SQLITE_NOMEM ){ + sqlite3_close(db); + db = 0; + }else if( rc!=SQLITE_OK ){ + db->magic = SQLITE_MAGIC_SICK; + } + *ppDb = db; +#ifdef SQLITE_ENABLE_SQLLOG + if( sqlite3GlobalConfig.xSqllog ){ + /* Opening a db handle. Fourth parameter is passed 0. */ + void *pArg = sqlite3GlobalConfig.pSqllogArg; + sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); + } +#endif +#if defined(SQLITE_HAS_CODEC) + if( rc==SQLITE_OK ){ + const char *zHexKey = sqlite3_uri_parameter(zOpen, "hexkey"); + if( zHexKey && zHexKey[0] ){ + u8 iByte; + int i; + char zKey[40]; + for(i=0, iByte=0; imutex); + assert( !db->mallocFailed ); + rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Register a new collation sequence with the database handle db. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16( + sqlite3* db, + const void *zName, + int enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*) +){ + int rc = SQLITE_OK; + char *zName8; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); + if( zName8 ){ + rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); + sqlite3DbFree(db, zName8); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Register a collation sequence factory callback with the database handle +** db. Replace any previously installed collation sequence factory. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed( + sqlite3 *db, + void *pCollNeededArg, + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + db->xCollNeeded = xCollNeeded; + db->xCollNeeded16 = 0; + db->pCollNeededArg = pCollNeededArg; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Register a collation sequence factory callback with the database handle +** db. Replace any previously installed collation sequence factory. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16( + sqlite3 *db, + void *pCollNeededArg, + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + db->xCollNeeded = 0; + db->xCollNeeded16 = xCollNeeded16; + db->pCollNeededArg = pCollNeededArg; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} +#endif /* SQLITE_OMIT_UTF16 */ + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** This function is now an anachronism. It used to be used to recover from a +** malloc() failure, but SQLite now does this automatically. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_global_recover(void){ + return SQLITE_OK; +} +#endif + +/* +** Test to see whether or not the database connection is in autocommit +** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on +** by default. Autocommit is disabled by a BEGIN statement and reenabled +** by the next COMMIT or ROLLBACK. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return db->autoCommit; +} + +/* +** The following routines are substitutes for constants SQLITE_CORRUPT, +** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error +** constants. They serve two purposes: +** +** 1. Serve as a convenient place to set a breakpoint in a debugger +** to detect when version error conditions occurs. +** +** 2. Invoke sqlite3_log() to provide the source code location where +** a low-level error is first detected. +*/ +static int reportError(int iErr, int lineno, const char *zType){ + sqlite3_log(iErr, "%s at line %d of [%.10s]", + zType, lineno, 20+sqlite3_sourceid()); + return iErr; +} +SQLITE_PRIVATE int sqlite3CorruptError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return reportError(SQLITE_CORRUPT, lineno, "database corruption"); +} +SQLITE_PRIVATE int sqlite3MisuseError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return reportError(SQLITE_MISUSE, lineno, "misuse"); +} +SQLITE_PRIVATE int sqlite3CantopenError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return reportError(SQLITE_CANTOPEN, lineno, "cannot open file"); +} +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3NomemError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return reportError(SQLITE_NOMEM, lineno, "OOM"); +} +SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return reportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error"); +} +#endif + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** This is a convenience routine that makes sure that all thread-specific +** data for this thread has been deallocated. +** +** SQLite no longer uses thread-specific data so this routine is now a +** no-op. It is retained for historical compatibility. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_thread_cleanup(void){ +} +#endif + +/* +** Return meta information about a specific column of a database table. +** See comment in sqlite3.h (sqlite.h.in) for details. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata( + sqlite3 *db, /* Connection handle */ + const char *zDbName, /* Database name or NULL */ + const char *zTableName, /* Table name */ + const char *zColumnName, /* Column name */ + char const **pzDataType, /* OUTPUT: Declared data type */ + char const **pzCollSeq, /* OUTPUT: Collation sequence name */ + int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ + int *pPrimaryKey, /* OUTPUT: True if column part of PK */ + int *pAutoinc /* OUTPUT: True if column is auto-increment */ +){ + int rc; + char *zErrMsg = 0; + Table *pTab = 0; + Column *pCol = 0; + int iCol = 0; + char const *zDataType = 0; + char const *zCollSeq = 0; + int notnull = 0; + int primarykey = 0; + int autoinc = 0; + + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + + /* Ensure the database schema has been loaded */ + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + rc = sqlite3Init(db, &zErrMsg); + if( SQLITE_OK!=rc ){ + goto error_out; + } + + /* Locate the table in question */ + pTab = sqlite3FindTable(db, zTableName, zDbName); + if( !pTab || pTab->pSelect ){ + pTab = 0; + goto error_out; + } + + /* Find the column for which info is requested */ + if( zColumnName==0 ){ + /* Query for existance of table only */ + }else{ + for(iCol=0; iColnCol; iCol++){ + pCol = &pTab->aCol[iCol]; + if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ + break; + } + } + if( iCol==pTab->nCol ){ + if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){ + iCol = pTab->iPKey; + pCol = iCol>=0 ? &pTab->aCol[iCol] : 0; + }else{ + pTab = 0; + goto error_out; + } + } + } + + /* The following block stores the meta information that will be returned + ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey + ** and autoinc. At this point there are two possibilities: + ** + ** 1. The specified column name was rowid", "oid" or "_rowid_" + ** and there is no explicitly declared IPK column. + ** + ** 2. The table is not a view and the column name identified an + ** explicitly declared column. Copy meta information from *pCol. + */ + if( pCol ){ + zDataType = sqlite3ColumnType(pCol,0); + zCollSeq = pCol->zColl; + notnull = pCol->notNull!=0; + primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; + autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; + }else{ + zDataType = "INTEGER"; + primarykey = 1; + } + if( !zCollSeq ){ + zCollSeq = sqlite3StrBINARY; + } + +error_out: + sqlite3BtreeLeaveAll(db); + + /* Whether the function call succeeded or failed, set the output parameters + ** to whatever their local counterparts contain. If an error did occur, + ** this has the effect of zeroing all output parameters. + */ + if( pzDataType ) *pzDataType = zDataType; + if( pzCollSeq ) *pzCollSeq = zCollSeq; + if( pNotNull ) *pNotNull = notnull; + if( pPrimaryKey ) *pPrimaryKey = primarykey; + if( pAutoinc ) *pAutoinc = autoinc; + + if( SQLITE_OK==rc && !pTab ){ + sqlite3DbFree(db, zErrMsg); + zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, + zColumnName); + rc = SQLITE_ERROR; + } + sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg); + sqlite3DbFree(db, zErrMsg); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Sleep for a little while. Return the amount of time slept. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int ms){ + sqlite3_vfs *pVfs; + int rc; + pVfs = sqlite3_vfs_find(0); + if( pVfs==0 ) return 0; + + /* This function works in milliseconds, but the underlying OsSleep() + ** API uses microseconds. Hence the 1000's. + */ + rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); + return rc; +} + +/* +** Enable or disable the extended result codes. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3 *db, int onoff){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + db->errMask = onoff ? 0xffffffff : 0xff; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +/* +** Invoke the xFileControl method on a particular database. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ + int rc = SQLITE_ERROR; + Btree *pBtree; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); + pBtree = sqlite3DbNameToBtree(db, zDbName); + if( pBtree ){ + Pager *pPager; + sqlite3_file *fd; + sqlite3BtreeEnter(pBtree); + pPager = sqlite3BtreePager(pBtree); + assert( pPager!=0 ); + fd = sqlite3PagerFile(pPager); + assert( fd!=0 ); + if( op==SQLITE_FCNTL_FILE_POINTER ){ + *(sqlite3_file**)pArg = fd; + rc = SQLITE_OK; + }else if( op==SQLITE_FCNTL_VFS_POINTER ){ + *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager); + rc = SQLITE_OK; + }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){ + *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager); + rc = SQLITE_OK; + }else if( fd->pMethods ){ + rc = sqlite3OsFileControl(fd, op, pArg); + }else{ + rc = SQLITE_NOTFOUND; + } + sqlite3BtreeLeave(pBtree); + } + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Interface to the testing logic. +*/ +SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){ + int rc = 0; +#ifdef SQLITE_OMIT_BUILTIN_TEST + UNUSED_PARAMETER(op); +#else + va_list ap; + va_start(ap, op); + switch( op ){ + + /* + ** Save the current state of the PRNG. + */ + case SQLITE_TESTCTRL_PRNG_SAVE: { + sqlite3PrngSaveState(); + break; + } + + /* + ** Restore the state of the PRNG to the last state saved using + ** PRNG_SAVE. If PRNG_SAVE has never before been called, then + ** this verb acts like PRNG_RESET. + */ + case SQLITE_TESTCTRL_PRNG_RESTORE: { + sqlite3PrngRestoreState(); + break; + } + + /* + ** Reset the PRNG back to its uninitialized state. The next call + ** to sqlite3_randomness() will reseed the PRNG using a single call + ** to the xRandomness method of the default VFS. + */ + case SQLITE_TESTCTRL_PRNG_RESET: { + sqlite3_randomness(0,0); + break; + } + + /* + ** sqlite3_test_control(BITVEC_TEST, size, program) + ** + ** Run a test against a Bitvec object of size. The program argument + ** is an array of integers that defines the test. Return -1 on a + ** memory allocation error, 0 on success, or non-zero for an error. + ** See the sqlite3BitvecBuiltinTest() for additional information. + */ + case SQLITE_TESTCTRL_BITVEC_TEST: { + int sz = va_arg(ap, int); + int *aProg = va_arg(ap, int*); + rc = sqlite3BitvecBuiltinTest(sz, aProg); + break; + } + + /* + ** sqlite3_test_control(FAULT_INSTALL, xCallback) + ** + ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called, + ** if xCallback is not NULL. + ** + ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0) + ** is called immediately after installing the new callback and the return + ** value from sqlite3FaultSim(0) becomes the return from + ** sqlite3_test_control(). + */ + case SQLITE_TESTCTRL_FAULT_INSTALL: { + /* MSVC is picky about pulling func ptrs from va lists. + ** http://support.microsoft.com/kb/47961 + ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); + */ + typedef int(*TESTCALLBACKFUNC_t)(int); + sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); + rc = sqlite3FaultSim(0); + break; + } + + /* + ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) + ** + ** Register hooks to call to indicate which malloc() failures + ** are benign. + */ + case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { + typedef void (*void_function)(void); + void_function xBenignBegin; + void_function xBenignEnd; + xBenignBegin = va_arg(ap, void_function); + xBenignEnd = va_arg(ap, void_function); + sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); + break; + } + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) + ** + ** Set the PENDING byte to the value in the argument, if X>0. + ** Make no changes if X==0. Return the value of the pending byte + ** as it existing before this routine was called. + ** + ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in + ** an incompatible database file format. Changing the PENDING byte + ** while any database connection is open results in undefined and + ** deleterious behavior. + */ + case SQLITE_TESTCTRL_PENDING_BYTE: { + rc = PENDING_BYTE; +#ifndef SQLITE_OMIT_WSD + { + unsigned int newVal = va_arg(ap, unsigned int); + if( newVal ) sqlite3PendingByte = newVal; + } +#endif + break; + } + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) + ** + ** This action provides a run-time test to see whether or not + ** assert() was enabled at compile-time. If X is true and assert() + ** is enabled, then the return value is true. If X is true and + ** assert() is disabled, then the return value is zero. If X is + ** false and assert() is enabled, then the assertion fires and the + ** process aborts. If X is false and assert() is disabled, then the + ** return value is zero. + */ + case SQLITE_TESTCTRL_ASSERT: { + volatile int x = 0; + assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 ); + rc = x; + break; + } + + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) + ** + ** This action provides a run-time test to see how the ALWAYS and + ** NEVER macros were defined at compile-time. + ** + ** The return value is ALWAYS(X). + ** + ** The recommended test is X==2. If the return value is 2, that means + ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the + ** default setting. If the return value is 1, then ALWAYS() is either + ** hard-coded to true or else it asserts if its argument is false. + ** The first behavior (hard-coded to true) is the case if + ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second + ** behavior (assert if the argument to ALWAYS() is false) is the case if + ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. + ** + ** The run-time test procedure might look something like this: + ** + ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ + ** // ALWAYS() and NEVER() are no-op pass-through macros + ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ + ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. + ** }else{ + ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. + ** } + */ + case SQLITE_TESTCTRL_ALWAYS: { + int x = va_arg(ap,int); + rc = ALWAYS(x); + break; + } + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); + ** + ** The integer returned reveals the byte-order of the computer on which + ** SQLite is running: + ** + ** 1 big-endian, determined at run-time + ** 10 little-endian, determined at run-time + ** 432101 big-endian, determined at compile-time + ** 123410 little-endian, determined at compile-time + */ + case SQLITE_TESTCTRL_BYTEORDER: { + rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) + ** + ** Set the nReserve size to N for the main database on the database + ** connection db. + */ + case SQLITE_TESTCTRL_RESERVE: { + sqlite3 *db = va_arg(ap, sqlite3*); + int x = va_arg(ap,int); + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); + sqlite3_mutex_leave(db->mutex); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) + ** + ** Enable or disable various optimizations for testing purposes. The + ** argument N is a bitmask of optimizations to be disabled. For normal + ** operation N should be 0. The idea is that a test program (like the + ** SQL Logic Test or SLT test module) can run the same SQL multiple times + ** with various optimizations disabled to verify that the same answer + ** is obtained in every case. + */ + case SQLITE_TESTCTRL_OPTIMIZATIONS: { + sqlite3 *db = va_arg(ap, sqlite3*); + db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); + break; + } + +#ifdef SQLITE_N_KEYWORD + /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) + ** + ** If zWord is a keyword recognized by the parser, then return the + ** number of keywords. Or if zWord is not a keyword, return 0. + ** + ** This test feature is only available in the amalgamation since + ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite + ** is built using separate source files. + */ + case SQLITE_TESTCTRL_ISKEYWORD: { + const char *zWord = va_arg(ap, const char*); + int n = sqlite3Strlen30(zWord); + rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; + break; + } +#endif + + /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); + ** + ** Pass pFree into sqlite3ScratchFree(). + ** If sz>0 then allocate a scratch buffer into pNew. + */ + case SQLITE_TESTCTRL_SCRATCHMALLOC: { + void *pFree, **ppNew; + int sz; + sz = va_arg(ap, int); + ppNew = va_arg(ap, void**); + pFree = va_arg(ap, void*); + if( sz ) *ppNew = sqlite3ScratchMalloc(sz); + sqlite3ScratchFree(pFree); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); + ** + ** If parameter onoff is non-zero, configure the wrappers so that all + ** subsequent calls to localtime() and variants fail. If onoff is zero, + ** undo this setting. + */ + case SQLITE_TESTCTRL_LOCALTIME_FAULT: { + sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); + ** + ** Set or clear a flag that indicates that the database file is always well- + ** formed and never corrupt. This flag is clear by default, indicating that + ** database files might have arbitrary corruption. Setting the flag during + ** testing causes certain assert() statements in the code to be activated + ** that demonstrat invariants on well-formed database files. + */ + case SQLITE_TESTCTRL_NEVER_CORRUPT: { + sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); + break; + } + + + /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); + ** + ** Set the VDBE coverage callback function to xCallback with context + ** pointer ptr. + */ + case SQLITE_TESTCTRL_VDBE_COVERAGE: { +#ifdef SQLITE_VDBE_COVERAGE + typedef void (*branch_callback)(void*,int,u8,u8); + sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback); + sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*); +#endif + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */ + case SQLITE_TESTCTRL_SORTER_MMAP: { + sqlite3 *db = va_arg(ap, sqlite3*); + db->nMaxSorterMmap = va_arg(ap, int); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); + ** + ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if + ** not. + */ + case SQLITE_TESTCTRL_ISINIT: { + if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); + ** + ** This test control is used to create imposter tables. "db" is a pointer + ** to the database connection. dbName is the database name (ex: "main" or + ** "temp") which will receive the imposter. "onOff" turns imposter mode on + ** or off. "tnum" is the root page of the b-tree to which the imposter + ** table should connect. + ** + ** Enable imposter mode only when the schema has already been parsed. Then + ** run a single CREATE TABLE statement to construct the imposter table in + ** the parsed schema. Then turn imposter mode back off again. + ** + ** If onOff==0 and tnum>0 then reset the schema for all databases, causing + ** the schema to be reparsed the next time it is needed. This has the + ** effect of erasing all imposter tables. + */ + case SQLITE_TESTCTRL_IMPOSTER: { + sqlite3 *db = va_arg(ap, sqlite3*); + sqlite3_mutex_enter(db->mutex); + db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*)); + db->init.busy = db->init.imposterTable = va_arg(ap,int); + db->init.newTnum = va_arg(ap,int); + if( db->init.busy==0 && db->init.newTnum>0 ){ + sqlite3ResetAllSchemasOfConnection(db); + } + sqlite3_mutex_leave(db->mutex); + break; + } + } + va_end(ap); +#endif /* SQLITE_OMIT_BUILTIN_TEST */ + return rc; +} + +/* +** This is a utility routine, useful to VFS implementations, that checks +** to see if a database file was a URI that contained a specific query +** parameter, and if so obtains the value of the query parameter. +** +** The zFilename argument is the filename pointer passed into the xOpen() +** method of a VFS implementation. The zParam argument is the name of the +** query parameter we seek. This routine returns the value of the zParam +** parameter if it exists. If the parameter does not exist, this routine +** returns a NULL pointer. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam){ + if( zFilename==0 || zParam==0 ) return 0; + zFilename += sqlite3Strlen30(zFilename) + 1; + while( zFilename[0] ){ + int x = strcmp(zFilename, zParam); + zFilename += sqlite3Strlen30(zFilename) + 1; + if( x==0 ) return zFilename; + zFilename += sqlite3Strlen30(zFilename) + 1; + } + return 0; +} + +/* +** Return a boolean value for a query parameter. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){ + const char *z = sqlite3_uri_parameter(zFilename, zParam); + bDflt = bDflt!=0; + return z ? sqlite3GetBoolean(z, bDflt) : bDflt; +} + +/* +** Return a 64-bit integer value for a query parameter. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64( + const char *zFilename, /* Filename as passed to xOpen */ + const char *zParam, /* URI parameter sought */ + sqlite3_int64 bDflt /* return if parameter is missing */ +){ + const char *z = sqlite3_uri_parameter(zFilename, zParam); + sqlite3_int64 v; + if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){ + bDflt = v; + } + return bDflt; +} + +/* +** Return the Btree pointer identified by zDbName. Return NULL if not found. +*/ +SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ + int i; + for(i=0; inDb; i++){ + if( db->aDb[i].pBt + && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0) + ){ + return db->aDb[i].pBt; + } + } + return 0; +} + +/* +** Return the filename of the database associated with a database +** connection. +*/ +SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName){ + Btree *pBt; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + pBt = sqlite3DbNameToBtree(db, zDbName); + return pBt ? sqlite3BtreeGetFilename(pBt) : 0; +} + +/* +** Return 1 if database is read-only or 0 if read/write. Return -1 if +** no such database exists. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ + Btree *pBt; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return -1; + } +#endif + pBt = sqlite3DbNameToBtree(db, zDbName); + return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; +} + +#ifdef SQLITE_ENABLE_SNAPSHOT +/* +** Obtain a snapshot handle for the snapshot of database zDb currently +** being read by handle db. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_get( + sqlite3 *db, + const char *zDb, + sqlite3_snapshot **ppSnapshot +){ + int rc = SQLITE_ERROR; +#ifndef SQLITE_OMIT_WAL + int iDb; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + + iDb = sqlite3FindDbName(db, zDb); + if( iDb==0 || iDb>1 ){ + Btree *pBt = db->aDb[iDb].pBt; + if( 0==sqlite3BtreeIsInTrans(pBt) ){ + rc = sqlite3BtreeBeginTrans(pBt, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); + } + } + } + + sqlite3_mutex_leave(db->mutex); +#endif /* SQLITE_OMIT_WAL */ + return rc; +} + +/* +** Open a read-transaction on the snapshot idendified by pSnapshot. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_open( + sqlite3 *db, + const char *zDb, + sqlite3_snapshot *pSnapshot +){ + int rc = SQLITE_ERROR; +#ifndef SQLITE_OMIT_WAL + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( db->autoCommit==0 ){ + int iDb; + iDb = sqlite3FindDbName(db, zDb); + if( iDb==0 || iDb>1 ){ + Btree *pBt = db->aDb[iDb].pBt; + if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ + rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeBeginTrans(pBt, 0); + sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0); + } + } + } + } + + sqlite3_mutex_leave(db->mutex); +#endif /* SQLITE_OMIT_WAL */ + return rc; +} + +/* +** Free a snapshot handle obtained from sqlite3_snapshot_get(). +*/ +SQLITE_API void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ + sqlite3_free(pSnapshot); +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + +/************** End of main.c ************************************************/ +/************** Begin file notify.c ******************************************/ +/* +** 2009 March 3 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the implementation of the sqlite3_unlock_notify() +** API method and its associated functionality. +*/ +/* #include "sqliteInt.h" */ +/* #include "btreeInt.h" */ + +/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */ +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + +/* +** Public interfaces: +** +** sqlite3ConnectionBlocked() +** sqlite3ConnectionUnlocked() +** sqlite3ConnectionClosed() +** sqlite3_unlock_notify() +*/ + +#define assertMutexHeld() \ + assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ) + +/* +** Head of a linked list of all sqlite3 objects created by this process +** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection +** is not NULL. This variable may only accessed while the STATIC_MASTER +** mutex is held. +*/ +static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0; + +#ifndef NDEBUG +/* +** This function is a complex assert() that verifies the following +** properties of the blocked connections list: +** +** 1) Each entry in the list has a non-NULL value for either +** pUnlockConnection or pBlockingConnection, or both. +** +** 2) All entries in the list that share a common value for +** xUnlockNotify are grouped together. +** +** 3) If the argument db is not NULL, then none of the entries in the +** blocked connections list have pUnlockConnection or pBlockingConnection +** set to db. This is used when closing connection db. +*/ +static void checkListProperties(sqlite3 *db){ + sqlite3 *p; + for(p=sqlite3BlockedList; p; p=p->pNextBlocked){ + int seen = 0; + sqlite3 *p2; + + /* Verify property (1) */ + assert( p->pUnlockConnection || p->pBlockingConnection ); + + /* Verify property (2) */ + for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){ + if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1; + assert( p2->xUnlockNotify==p->xUnlockNotify || !seen ); + assert( db==0 || p->pUnlockConnection!=db ); + assert( db==0 || p->pBlockingConnection!=db ); + } + } +} +#else +# define checkListProperties(x) +#endif + +/* +** Remove connection db from the blocked connections list. If connection +** db is not currently a part of the list, this function is a no-op. +*/ +static void removeFromBlockedList(sqlite3 *db){ + sqlite3 **pp; + assertMutexHeld(); + for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){ + if( *pp==db ){ + *pp = (*pp)->pNextBlocked; + break; + } + } +} + +/* +** Add connection db to the blocked connections list. It is assumed +** that it is not already a part of the list. +*/ +static void addToBlockedList(sqlite3 *db){ + sqlite3 **pp; + assertMutexHeld(); + for( + pp=&sqlite3BlockedList; + *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; + pp=&(*pp)->pNextBlocked + ); + db->pNextBlocked = *pp; + *pp = db; +} + +/* +** Obtain the STATIC_MASTER mutex. +*/ +static void enterMutex(void){ + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + checkListProperties(0); +} + +/* +** Release the STATIC_MASTER mutex. +*/ +static void leaveMutex(void){ + assertMutexHeld(); + checkListProperties(0); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); +} + +/* +** Register an unlock-notify callback. +** +** This is called after connection "db" has attempted some operation +** but has received an SQLITE_LOCKED error because another connection +** (call it pOther) in the same process was busy using the same shared +** cache. pOther is found by looking at db->pBlockingConnection. +** +** If there is no blocking connection, the callback is invoked immediately, +** before this routine returns. +** +** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate +** a deadlock. +** +** Otherwise, make arrangements to invoke xNotify when pOther drops +** its locks. +** +** Each call to this routine overrides any prior callbacks registered +** on the same "db". If xNotify==0 then any prior callbacks are immediately +** cancelled. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify( + sqlite3 *db, + void (*xNotify)(void **, int), + void *pArg +){ + int rc = SQLITE_OK; + + sqlite3_mutex_enter(db->mutex); + enterMutex(); + + if( xNotify==0 ){ + removeFromBlockedList(db); + db->pBlockingConnection = 0; + db->pUnlockConnection = 0; + db->xUnlockNotify = 0; + db->pUnlockArg = 0; + }else if( 0==db->pBlockingConnection ){ + /* The blocking transaction has been concluded. Or there never was a + ** blocking transaction. In either case, invoke the notify callback + ** immediately. + */ + xNotify(&pArg, 1); + }else{ + sqlite3 *p; + + for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){} + if( p ){ + rc = SQLITE_LOCKED; /* Deadlock detected. */ + }else{ + db->pUnlockConnection = db->pBlockingConnection; + db->xUnlockNotify = xNotify; + db->pUnlockArg = pArg; + removeFromBlockedList(db); + addToBlockedList(db); + } + } + + leaveMutex(); + assert( !db->mallocFailed ); + sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0)); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** This function is called while stepping or preparing a statement +** associated with connection db. The operation will return SQLITE_LOCKED +** to the user because it requires a lock that will not be available +** until connection pBlocker concludes its current transaction. +*/ +SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){ + enterMutex(); + if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){ + addToBlockedList(db); + } + db->pBlockingConnection = pBlocker; + leaveMutex(); +} + +/* +** This function is called when +** the transaction opened by database db has just finished. Locks held +** by database connection db have been released. +** +** This function loops through each entry in the blocked connections +** list and does the following: +** +** 1) If the sqlite3.pBlockingConnection member of a list entry is +** set to db, then set pBlockingConnection=0. +** +** 2) If the sqlite3.pUnlockConnection member of a list entry is +** set to db, then invoke the configured unlock-notify callback and +** set pUnlockConnection=0. +** +** 3) If the two steps above mean that pBlockingConnection==0 and +** pUnlockConnection==0, remove the entry from the blocked connections +** list. +*/ +SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){ + void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */ + int nArg = 0; /* Number of entries in aArg[] */ + sqlite3 **pp; /* Iterator variable */ + void **aArg; /* Arguments to the unlock callback */ + void **aDyn = 0; /* Dynamically allocated space for aArg[] */ + void *aStatic[16]; /* Starter space for aArg[]. No malloc required */ + + aArg = aStatic; + enterMutex(); /* Enter STATIC_MASTER mutex */ + + /* This loop runs once for each entry in the blocked-connections list. */ + for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){ + sqlite3 *p = *pp; + + /* Step 1. */ + if( p->pBlockingConnection==db ){ + p->pBlockingConnection = 0; + } + + /* Step 2. */ + if( p->pUnlockConnection==db ){ + assert( p->xUnlockNotify ); + if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){ + xUnlockNotify(aArg, nArg); + nArg = 0; + } + + sqlite3BeginBenignMalloc(); + assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) ); + assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn ); + if( (!aDyn && nArg==(int)ArraySize(aStatic)) + || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*))) + ){ + /* The aArg[] array needs to grow. */ + void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2); + if( pNew ){ + memcpy(pNew, aArg, nArg*sizeof(void *)); + sqlite3_free(aDyn); + aDyn = aArg = pNew; + }else{ + /* This occurs when the array of context pointers that need to + ** be passed to the unlock-notify callback is larger than the + ** aStatic[] array allocated on the stack and the attempt to + ** allocate a larger array from the heap has failed. + ** + ** This is a difficult situation to handle. Returning an error + ** code to the caller is insufficient, as even if an error code + ** is returned the transaction on connection db will still be + ** closed and the unlock-notify callbacks on blocked connections + ** will go unissued. This might cause the application to wait + ** indefinitely for an unlock-notify callback that will never + ** arrive. + ** + ** Instead, invoke the unlock-notify callback with the context + ** array already accumulated. We can then clear the array and + ** begin accumulating any further context pointers without + ** requiring any dynamic allocation. This is sub-optimal because + ** it means that instead of one callback with a large array of + ** context pointers the application will receive two or more + ** callbacks with smaller arrays of context pointers, which will + ** reduce the applications ability to prioritize multiple + ** connections. But it is the best that can be done under the + ** circumstances. + */ + xUnlockNotify(aArg, nArg); + nArg = 0; + } + } + sqlite3EndBenignMalloc(); + + aArg[nArg++] = p->pUnlockArg; + xUnlockNotify = p->xUnlockNotify; + p->pUnlockConnection = 0; + p->xUnlockNotify = 0; + p->pUnlockArg = 0; + } + + /* Step 3. */ + if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){ + /* Remove connection p from the blocked connections list. */ + *pp = p->pNextBlocked; + p->pNextBlocked = 0; + }else{ + pp = &p->pNextBlocked; + } + } + + if( nArg!=0 ){ + xUnlockNotify(aArg, nArg); + } + sqlite3_free(aDyn); + leaveMutex(); /* Leave STATIC_MASTER mutex */ +} + +/* +** This is called when the database connection passed as an argument is +** being closed. The connection is removed from the blocked list. +*/ +SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){ + sqlite3ConnectionUnlocked(db); + enterMutex(); + removeFromBlockedList(db); + checkListProperties(db); + leaveMutex(); +} +#endif + +/************** End of notify.c **********************************************/ +/************** Begin file fts3.c ********************************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This is an SQLite module implementing full-text search. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ + +/* The full-text index is stored in a series of b+tree (-like) +** structures called segments which map terms to doclists. The +** structures are like b+trees in layout, but are constructed from the +** bottom up in optimal fashion and are not updatable. Since trees +** are built from the bottom up, things will be described from the +** bottom up. +** +** +**** Varints **** +** The basic unit of encoding is a variable-length integer called a +** varint. We encode variable-length integers in little-endian order +** using seven bits * per byte as follows: +** +** KEY: +** A = 0xxxxxxx 7 bits of data and one flag bit +** B = 1xxxxxxx 7 bits of data and one flag bit +** +** 7 bits - A +** 14 bits - BA +** 21 bits - BBA +** and so on. +** +** This is similar in concept to how sqlite encodes "varints" but +** the encoding is not the same. SQLite varints are big-endian +** are are limited to 9 bytes in length whereas FTS3 varints are +** little-endian and can be up to 10 bytes in length (in theory). +** +** Example encodings: +** +** 1: 0x01 +** 127: 0x7f +** 128: 0x81 0x00 +** +** +**** Document lists **** +** A doclist (document list) holds a docid-sorted list of hits for a +** given term. Doclists hold docids and associated token positions. +** A docid is the unique integer identifier for a single document. +** A position is the index of a word within the document. The first +** word of the document has a position of 0. +** +** FTS3 used to optionally store character offsets using a compile-time +** option. But that functionality is no longer supported. +** +** A doclist is stored like this: +** +** array { +** varint docid; (delta from previous doclist) +** array { (position list for column 0) +** varint position; (2 more than the delta from previous position) +** } +** array { +** varint POS_COLUMN; (marks start of position list for new column) +** varint column; (index of new column) +** array { +** varint position; (2 more than the delta from previous position) +** } +** } +** varint POS_END; (marks end of positions for this document. +** } +** +** Here, array { X } means zero or more occurrences of X, adjacent in +** memory. A "position" is an index of a token in the token stream +** generated by the tokenizer. Note that POS_END and POS_COLUMN occur +** in the same logical place as the position element, and act as sentinals +** ending a position list array. POS_END is 0. POS_COLUMN is 1. +** The positions numbers are not stored literally but rather as two more +** than the difference from the prior position, or the just the position plus +** 2 for the first position. Example: +** +** label: A B C D E F G H I J K +** value: 123 5 9 1 1 14 35 0 234 72 0 +** +** The 123 value is the first docid. For column zero in this document +** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1 +** at D signals the start of a new column; the 1 at E indicates that the +** new column is column number 1. There are two positions at 12 and 45 +** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The +** 234 at I is the delta to next docid (357). It has one position 70 +** (72-2) and then terminates with the 0 at K. +** +** A "position-list" is the list of positions for multiple columns for +** a single docid. A "column-list" is the set of positions for a single +** column. Hence, a position-list consists of one or more column-lists, +** a document record consists of a docid followed by a position-list and +** a doclist consists of one or more document records. +** +** A bare doclist omits the position information, becoming an +** array of varint-encoded docids. +** +**** Segment leaf nodes **** +** Segment leaf nodes store terms and doclists, ordered by term. Leaf +** nodes are written using LeafWriter, and read using LeafReader (to +** iterate through a single leaf node's data) and LeavesReader (to +** iterate through a segment's entire leaf layer). Leaf nodes have +** the format: +** +** varint iHeight; (height from leaf level, always 0) +** varint nTerm; (length of first term) +** char pTerm[nTerm]; (content of first term) +** varint nDoclist; (length of term's associated doclist) +** char pDoclist[nDoclist]; (content of doclist) +** array { +** (further terms are delta-encoded) +** varint nPrefix; (length of prefix shared with previous term) +** varint nSuffix; (length of unshared suffix) +** char pTermSuffix[nSuffix];(unshared suffix of next term) +** varint nDoclist; (length of term's associated doclist) +** char pDoclist[nDoclist]; (content of doclist) +** } +** +** Here, array { X } means zero or more occurrences of X, adjacent in +** memory. +** +** Leaf nodes are broken into blocks which are stored contiguously in +** the %_segments table in sorted order. This means that when the end +** of a node is reached, the next term is in the node with the next +** greater node id. +** +** New data is spilled to a new leaf node when the current node +** exceeds LEAF_MAX bytes (default 2048). New data which itself is +** larger than STANDALONE_MIN (default 1024) is placed in a standalone +** node (a leaf node with a single term and doclist). The goal of +** these settings is to pack together groups of small doclists while +** making it efficient to directly access large doclists. The +** assumption is that large doclists represent terms which are more +** likely to be query targets. +** +** TODO(shess) It may be useful for blocking decisions to be more +** dynamic. For instance, it may make more sense to have a 2.5k leaf +** node rather than splitting into 2k and .5k nodes. My intuition is +** that this might extend through 2x or 4x the pagesize. +** +** +**** Segment interior nodes **** +** Segment interior nodes store blockids for subtree nodes and terms +** to describe what data is stored by the each subtree. Interior +** nodes are written using InteriorWriter, and read using +** InteriorReader. InteriorWriters are created as needed when +** SegmentWriter creates new leaf nodes, or when an interior node +** itself grows too big and must be split. The format of interior +** nodes: +** +** varint iHeight; (height from leaf level, always >0) +** varint iBlockid; (block id of node's leftmost subtree) +** optional { +** varint nTerm; (length of first term) +** char pTerm[nTerm]; (content of first term) +** array { +** (further terms are delta-encoded) +** varint nPrefix; (length of shared prefix with previous term) +** varint nSuffix; (length of unshared suffix) +** char pTermSuffix[nSuffix]; (unshared suffix of next term) +** } +** } +** +** Here, optional { X } means an optional element, while array { X } +** means zero or more occurrences of X, adjacent in memory. +** +** An interior node encodes n terms separating n+1 subtrees. The +** subtree blocks are contiguous, so only the first subtree's blockid +** is encoded. The subtree at iBlockid will contain all terms less +** than the first term encoded (or all terms if no term is encoded). +** Otherwise, for terms greater than or equal to pTerm[i] but less +** than pTerm[i+1], the subtree for that term will be rooted at +** iBlockid+i. Interior nodes only store enough term data to +** distinguish adjacent children (if the rightmost term of the left +** child is "something", and the leftmost term of the right child is +** "wicked", only "w" is stored). +** +** New data is spilled to a new interior node at the same height when +** the current node exceeds INTERIOR_MAX bytes (default 2048). +** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing +** interior nodes and making the tree too skinny. The interior nodes +** at a given height are naturally tracked by interior nodes at +** height+1, and so on. +** +** +**** Segment directory **** +** The segment directory in table %_segdir stores meta-information for +** merging and deleting segments, and also the root node of the +** segment's tree. +** +** The root node is the top node of the segment's tree after encoding +** the entire segment, restricted to ROOT_MAX bytes (default 1024). +** This could be either a leaf node or an interior node. If the top +** node requires more than ROOT_MAX bytes, it is flushed to %_segments +** and a new root interior node is generated (which should always fit +** within ROOT_MAX because it only needs space for 2 varints, the +** height and the blockid of the previous root). +** +** The meta-information in the segment directory is: +** level - segment level (see below) +** idx - index within level +** - (level,idx uniquely identify a segment) +** start_block - first leaf node +** leaves_end_block - last leaf node +** end_block - last block (including interior nodes) +** root - contents of root node +** +** If the root node is a leaf node, then start_block, +** leaves_end_block, and end_block are all 0. +** +** +**** Segment merging **** +** To amortize update costs, segments are grouped into levels and +** merged in batches. Each increase in level represents exponentially +** more documents. +** +** New documents (actually, document updates) are tokenized and +** written individually (using LeafWriter) to a level 0 segment, with +** incrementing idx. When idx reaches MERGE_COUNT (default 16), all +** level 0 segments are merged into a single level 1 segment. Level 1 +** is populated like level 0, and eventually MERGE_COUNT level 1 +** segments are merged to a single level 2 segment (representing +** MERGE_COUNT^2 updates), and so on. +** +** A segment merge traverses all segments at a given level in +** parallel, performing a straightforward sorted merge. Since segment +** leaf nodes are written in to the %_segments table in order, this +** merge traverses the underlying sqlite disk structures efficiently. +** After the merge, all segment blocks from the merged level are +** deleted. +** +** MERGE_COUNT controls how often we merge segments. 16 seems to be +** somewhat of a sweet spot for insertion performance. 32 and 64 show +** very similar performance numbers to 16 on insertion, though they're +** a tiny bit slower (perhaps due to more overhead in merge-time +** sorting). 8 is about 20% slower than 16, 4 about 50% slower than +** 16, 2 about 66% slower than 16. +** +** At query time, high MERGE_COUNT increases the number of segments +** which need to be scanned and merged. For instance, with 100k docs +** inserted: +** +** MERGE_COUNT segments +** 16 25 +** 8 12 +** 4 10 +** 2 6 +** +** This appears to have only a moderate impact on queries for very +** frequent terms (which are somewhat dominated by segment merge +** costs), and infrequent and non-existent terms still seem to be fast +** even with many segments. +** +** TODO(shess) That said, it would be nice to have a better query-side +** argument for MERGE_COUNT of 16. Also, it is possible/likely that +** optimizations to things like doclist merging will swing the sweet +** spot around. +** +** +** +**** Handling of deletions and updates **** +** Since we're using a segmented structure, with no docid-oriented +** index into the term index, we clearly cannot simply update the term +** index when a document is deleted or updated. For deletions, we +** write an empty doclist (varint(docid) varint(POS_END)), for updates +** we simply write the new doclist. Segment merges overwrite older +** data for a particular docid with newer data, so deletes or updates +** will eventually overtake the earlier data and knock it out. The +** query logic likewise merges doclists so that newer data knocks out +** older data. +*/ + +/************** Include fts3Int.h in the middle of fts3.c ********************/ +/************** Begin file fts3Int.h *****************************************/ +/* +** 2009 Nov 12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ +#ifndef _FTSINT_H +#define _FTSINT_H + +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif + +/* FTS3/FTS4 require virtual tables */ +#ifdef SQLITE_OMIT_VIRTUALTABLE +# undef SQLITE_ENABLE_FTS3 +# undef SQLITE_ENABLE_FTS4 +#endif + +/* +** FTS4 is really an extension for FTS3. It is enabled using the +** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all +** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3. +*/ +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3) +# define SQLITE_ENABLE_FTS3 +#endif + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* If not building as part of the core, include sqlite3ext.h. */ +#ifndef SQLITE_CORE +/* # include "sqlite3ext.h" */ +SQLITE_EXTENSION_INIT3 +#endif + +/* #include "sqlite3.h" */ +/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/ +/************** Begin file fts3_tokenizer.h **********************************/ +/* +** 2006 July 10 +** +** The author disclaims copyright to this source code. +** +************************************************************************* +** Defines the interface to tokenizers used by fulltext-search. There +** are three basic components: +** +** sqlite3_tokenizer_module is a singleton defining the tokenizer +** interface functions. This is essentially the class structure for +** tokenizers. +** +** sqlite3_tokenizer is used to define a particular tokenizer, perhaps +** including customization information defined at creation time. +** +** sqlite3_tokenizer_cursor is generated by a tokenizer to generate +** tokens from a particular input. +*/ +#ifndef _FTS3_TOKENIZER_H_ +#define _FTS3_TOKENIZER_H_ + +/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. +** If tokenizers are to be allowed to call sqlite3_*() functions, then +** we will need a way to register the API consistently. +*/ +/* #include "sqlite3.h" */ + +/* +** Structures used by the tokenizer interface. When a new tokenizer +** implementation is registered, the caller provides a pointer to +** an sqlite3_tokenizer_module containing pointers to the callback +** functions that make up an implementation. +** +** When an fts3 table is created, it passes any arguments passed to +** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the +** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer +** implementation. The xCreate() function in turn returns an +** sqlite3_tokenizer structure representing the specific tokenizer to +** be used for the fts3 table (customized by the tokenizer clause arguments). +** +** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() +** method is called. It returns an sqlite3_tokenizer_cursor object +** that may be used to tokenize a specific input buffer based on +** the tokenization rules supplied by a specific sqlite3_tokenizer +** object. +*/ +typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; +typedef struct sqlite3_tokenizer sqlite3_tokenizer; +typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; + +struct sqlite3_tokenizer_module { + + /* + ** Structure version. Should always be set to 0 or 1. + */ + int iVersion; + + /* + ** Create a new tokenizer. The values in the argv[] array are the + ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL + ** TABLE statement that created the fts3 table. For example, if + ** the following SQL is executed: + ** + ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) + ** + ** then argc is set to 2, and the argv[] array contains pointers + ** to the strings "arg1" and "arg2". + ** + ** This method should return either SQLITE_OK (0), or an SQLite error + ** code. If SQLITE_OK is returned, then *ppTokenizer should be set + ** to point at the newly created tokenizer structure. The generic + ** sqlite3_tokenizer.pModule variable should not be initialized by + ** this callback. The caller will do so. + */ + int (*xCreate)( + int argc, /* Size of argv array */ + const char *const*argv, /* Tokenizer argument strings */ + sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ + ); + + /* + ** Destroy an existing tokenizer. The fts3 module calls this method + ** exactly once for each successful call to xCreate(). + */ + int (*xDestroy)(sqlite3_tokenizer *pTokenizer); + + /* + ** Create a tokenizer cursor to tokenize an input buffer. The caller + ** is responsible for ensuring that the input buffer remains valid + ** until the cursor is closed (using the xClose() method). + */ + int (*xOpen)( + sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ + const char *pInput, int nBytes, /* Input buffer */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ + ); + + /* + ** Destroy an existing tokenizer cursor. The fts3 module calls this + ** method exactly once for each successful call to xOpen(). + */ + int (*xClose)(sqlite3_tokenizer_cursor *pCursor); + + /* + ** Retrieve the next token from the tokenizer cursor pCursor. This + ** method should either return SQLITE_OK and set the values of the + ** "OUT" variables identified below, or SQLITE_DONE to indicate that + ** the end of the buffer has been reached, or an SQLite error code. + ** + ** *ppToken should be set to point at a buffer containing the + ** normalized version of the token (i.e. after any case-folding and/or + ** stemming has been performed). *pnBytes should be set to the length + ** of this buffer in bytes. The input text that generated the token is + ** identified by the byte offsets returned in *piStartOffset and + ** *piEndOffset. *piStartOffset should be set to the index of the first + ** byte of the token in the input buffer. *piEndOffset should be set + ** to the index of the first byte just past the end of the token in + ** the input buffer. + ** + ** The buffer *ppToken is set to point at is managed by the tokenizer + ** implementation. It is only required to be valid until the next call + ** to xNext() or xClose(). + */ + /* TODO(shess) current implementation requires pInput to be + ** nul-terminated. This should either be fixed, or pInput/nBytes + ** should be converted to zInput. + */ + int (*xNext)( + sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ + const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ + int *piStartOffset, /* OUT: Byte offset of token in input buffer */ + int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ + int *piPosition /* OUT: Number of tokens returned before this one */ + ); + + /*********************************************************************** + ** Methods below this point are only available if iVersion>=1. + */ + + /* + ** Configure the language id of a tokenizer cursor. + */ + int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid); +}; + +struct sqlite3_tokenizer { + const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ + /* Tokenizer implementations will typically add additional fields */ +}; + +struct sqlite3_tokenizer_cursor { + sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ + /* Tokenizer implementations will typically add additional fields */ +}; + +int fts3_global_term_cnt(int iTerm, int iCol); +int fts3_term_cnt(int iTerm, int iCol); + + +#endif /* _FTS3_TOKENIZER_H_ */ + +/************** End of fts3_tokenizer.h **************************************/ +/************** Continuing where we left off in fts3Int.h ********************/ +/************** Include fts3_hash.h in the middle of fts3Int.h ***************/ +/************** Begin file fts3_hash.h ***************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the header file for the generic hash-table implementation +** used in SQLite. We've modified it slightly to serve as a standalone +** hash table implementation for the full-text indexing module. +** +*/ +#ifndef _FTS3_HASH_H_ +#define _FTS3_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct Fts3Hash Fts3Hash; +typedef struct Fts3HashElem Fts3HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, many of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +*/ +struct Fts3Hash { + char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ + char copyKey; /* True if copy of key made on insert */ + int count; /* Number of entries in this table */ + Fts3HashElem *first; /* The first element of the array */ + int htsize; /* Number of buckets in the hash table */ + struct _fts3ht { /* the hash table */ + int count; /* Number of entries with this hash */ + Fts3HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. +** +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct Fts3HashElem { + Fts3HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + void *pKey; int nKey; /* Key associated with this element */ +}; + +/* +** There are 2 different modes of operation for a hash table: +** +** FTS3_HASH_STRING pKey points to a string that is nKey bytes long +** (including the null-terminator, if any). Case +** is respected in comparisons. +** +** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. +** memcmp() is used to compare keys. +** +** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. +*/ +#define FTS3_HASH_STRING 1 +#define FTS3_HASH_BINARY 2 + +/* +** Access routines. To delete, insert a NULL pointer. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey); +SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData); +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey); +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*); +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int); + +/* +** Shorthand for the functions above +*/ +#define fts3HashInit sqlite3Fts3HashInit +#define fts3HashInsert sqlite3Fts3HashInsert +#define fts3HashFind sqlite3Fts3HashFind +#define fts3HashClear sqlite3Fts3HashClear +#define fts3HashFindElem sqlite3Fts3HashFindElem + +/* +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** Fts3Hash h; +** Fts3HashElem *p; +** ... +** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ +** SomeStructure *pData = fts3HashData(p); +** // do something with pData +** } +*/ +#define fts3HashFirst(H) ((H)->first) +#define fts3HashNext(E) ((E)->next) +#define fts3HashData(E) ((E)->data) +#define fts3HashKey(E) ((E)->pKey) +#define fts3HashKeysize(E) ((E)->nKey) + +/* +** Number of entries in a hash table +*/ +#define fts3HashCount(H) ((H)->count) + +#endif /* _FTS3_HASH_H_ */ + +/************** End of fts3_hash.h *******************************************/ +/************** Continuing where we left off in fts3Int.h ********************/ + +/* +** This constant determines the maximum depth of an FTS expression tree +** that the library will create and use. FTS uses recursion to perform +** various operations on the query tree, so the disadvantage of a large +** limit is that it may allow very large queries to use large amounts +** of stack space (perhaps causing a stack overflow). +*/ +#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH +# define SQLITE_FTS3_MAX_EXPR_DEPTH 12 +#endif + + +/* +** This constant controls how often segments are merged. Once there are +** FTS3_MERGE_COUNT segments of level N, they are merged into a single +** segment of level N+1. +*/ +#define FTS3_MERGE_COUNT 16 + +/* +** This is the maximum amount of data (in bytes) to store in the +** Fts3Table.pendingTerms hash table. Normally, the hash table is +** populated as documents are inserted/updated/deleted in a transaction +** and used to create a new segment when the transaction is committed. +** However if this limit is reached midway through a transaction, a new +** segment is created and the hash table cleared immediately. +*/ +#define FTS3_MAX_PENDING_DATA (1*1024*1024) + +/* +** Macro to return the number of elements in an array. SQLite has a +** similar macro called ArraySize(). Use a different name to avoid +** a collision when building an amalgamation with built-in FTS3. +*/ +#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0]))) + + +#ifndef MIN +# define MIN(x,y) ((x)<(y)?(x):(y)) +#endif +#ifndef MAX +# define MAX(x,y) ((x)>(y)?(x):(y)) +#endif + +/* +** Maximum length of a varint encoded integer. The varint format is different +** from that used by SQLite, so the maximum length is 10, not 9. +*/ +#define FTS3_VARINT_MAX 10 + +/* +** FTS4 virtual tables may maintain multiple indexes - one index of all terms +** in the document set and zero or more prefix indexes. All indexes are stored +** as one or more b+-trees in the %_segments and %_segdir tables. +** +** It is possible to determine which index a b+-tree belongs to based on the +** value stored in the "%_segdir.level" column. Given this value L, the index +** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with +** level values between 0 and 1023 (inclusive) belong to index 0, all levels +** between 1024 and 2047 to index 1, and so on. +** +** It is considered impossible for an index to use more than 1024 levels. In +** theory though this may happen, but only after at least +** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables. +*/ +#define FTS3_SEGDIR_MAXLEVEL 1024 +#define FTS3_SEGDIR_MAXLEVEL_STR "1024" + +/* +** The testcase() macro is only used by the amalgamation. If undefined, +** make it a no-op. +*/ +#ifndef testcase +# define testcase(X) +#endif + +/* +** Terminator values for position-lists and column-lists. +*/ +#define POS_COLUMN (1) /* Column-list terminator */ +#define POS_END (0) /* Position-list terminator */ + +/* +** This section provides definitions to allow the +** FTS3 extension to be compiled outside of the +** amalgamation. +*/ +#ifndef SQLITE_AMALGAMATION +/* +** Macros indicating that conditional expressions are always true or +** false. +*/ +#ifdef SQLITE_COVERAGE_TEST +# define ALWAYS(x) (1) +# define NEVER(X) (0) +#elif defined(SQLITE_DEBUG) +# define ALWAYS(x) sqlite3Fts3Always((x)!=0) +# define NEVER(x) sqlite3Fts3Never((x)!=0) +SQLITE_PRIVATE int sqlite3Fts3Always(int b); +SQLITE_PRIVATE int sqlite3Fts3Never(int b); +#else +# define ALWAYS(x) (x) +# define NEVER(x) (x) +#endif + +/* +** Internal types used by SQLite. +*/ +typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */ +typedef short int i16; /* 2-byte (or larger) signed integer */ +typedef unsigned int u32; /* 4-byte unsigned integer */ +typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */ +typedef sqlite3_int64 i64; /* 8-byte signed integer */ + +/* +** Macro used to suppress compiler warnings for unused parameters. +*/ +#define UNUSED_PARAMETER(x) (void)(x) + +/* +** Activate assert() only if SQLITE_TEST is enabled. +*/ +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif + +/* +** The TESTONLY macro is used to enclose variable declarations or +** other bits of code that are needed to support the arguments +** within testcase() and assert() macros. +*/ +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) +# define TESTONLY(X) X +#else +# define TESTONLY(X) +#endif + +#endif /* SQLITE_AMALGAMATION */ + +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3Fts3Corrupt(void); +# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt() +#else +# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB +#endif + +typedef struct Fts3Table Fts3Table; +typedef struct Fts3Cursor Fts3Cursor; +typedef struct Fts3Expr Fts3Expr; +typedef struct Fts3Phrase Fts3Phrase; +typedef struct Fts3PhraseToken Fts3PhraseToken; + +typedef struct Fts3Doclist Fts3Doclist; +typedef struct Fts3SegFilter Fts3SegFilter; +typedef struct Fts3DeferredToken Fts3DeferredToken; +typedef struct Fts3SegReader Fts3SegReader; +typedef struct Fts3MultiSegReader Fts3MultiSegReader; + +typedef struct MatchinfoBuffer MatchinfoBuffer; + +/* +** A connection to a fulltext index is an instance of the following +** structure. The xCreate and xConnect methods create an instance +** of this structure and xDestroy and xDisconnect free that instance. +** All other methods receive a pointer to the structure as one of their +** arguments. +*/ +struct Fts3Table { + sqlite3_vtab base; /* Base class used by SQLite core */ + sqlite3 *db; /* The database connection */ + const char *zDb; /* logical database name */ + const char *zName; /* virtual table name */ + int nColumn; /* number of named columns in virtual table */ + char **azColumn; /* column names. malloced */ + u8 *abNotindexed; /* True for 'notindexed' columns */ + sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ + char *zContentTbl; /* content=xxx option, or NULL */ + char *zLanguageid; /* languageid=xxx option, or NULL */ + int nAutoincrmerge; /* Value configured by 'automerge' */ + u32 nLeafAdd; /* Number of leaf blocks added this trans */ + + /* Precompiled statements used by the implementation. Each of these + ** statements is run and reset within a single virtual table API call. + */ + sqlite3_stmt *aStmt[40]; + + char *zReadExprlist; + char *zWriteExprlist; + + int nNodeSize; /* Soft limit for node size */ + u8 bFts4; /* True for FTS4, false for FTS3 */ + u8 bHasStat; /* True if %_stat table exists (2==unknown) */ + u8 bHasDocsize; /* True if %_docsize table exists */ + u8 bDescIdx; /* True if doclists are in reverse order */ + u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */ + int nPgsz; /* Page size for host database */ + char *zSegmentsTbl; /* Name of %_segments table */ + sqlite3_blob *pSegments; /* Blob handle open on %_segments table */ + + /* + ** The following array of hash tables is used to buffer pending index + ** updates during transactions. All pending updates buffered at any one + ** time must share a common language-id (see the FTS4 langid= feature). + ** The current language id is stored in variable iPrevLangid. + ** + ** A single FTS4 table may have multiple full-text indexes. For each index + ** there is an entry in the aIndex[] array. Index 0 is an index of all the + ** terms that appear in the document set. Each subsequent index in aIndex[] + ** is an index of prefixes of a specific length. + ** + ** Variable nPendingData contains an estimate the memory consumed by the + ** pending data structures, including hash table overhead, but not including + ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash + ** tables are flushed to disk. Variable iPrevDocid is the docid of the most + ** recently inserted record. + */ + int nIndex; /* Size of aIndex[] */ + struct Fts3Index { + int nPrefix; /* Prefix length (0 for main terms index) */ + Fts3Hash hPending; /* Pending terms table for this index */ + } *aIndex; + int nMaxPendingData; /* Max pending data before flush to disk */ + int nPendingData; /* Current bytes of pending data */ + sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */ + int iPrevLangid; /* Langid of recently inserted document */ + int bPrevDelete; /* True if last operation was a delete */ + +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) + /* State variables used for validating that the transaction control + ** methods of the virtual table are called at appropriate times. These + ** values do not contribute to FTS functionality; they are used for + ** verifying the operation of the SQLite core. + */ + int inTransaction; /* True after xBegin but before xCommit/xRollback */ + int mxSavepoint; /* Largest valid xSavepoint integer */ +#endif + +#ifdef SQLITE_TEST + /* True to disable the incremental doclist optimization. This is controled + ** by special insert command 'test-no-incr-doclist'. */ + int bNoIncrDoclist; +#endif +}; + +/* +** When the core wants to read from the virtual table, it creates a +** virtual table cursor (an instance of the following structure) using +** the xOpen method. Cursors are destroyed using the xClose method. +*/ +struct Fts3Cursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + i16 eSearch; /* Search strategy (see below) */ + u8 isEof; /* True if at End Of Results */ + u8 isRequireSeek; /* True if must seek pStmt to %_content row */ + sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ + Fts3Expr *pExpr; /* Parsed MATCH query string */ + int iLangid; /* Language being queried for */ + int nPhrase; /* Number of matchable phrases in query */ + Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */ + sqlite3_int64 iPrevId; /* Previous id read from aDoclist */ + char *pNextId; /* Pointer into the body of aDoclist */ + char *aDoclist; /* List of docids for full-text queries */ + int nDoclist; /* Size of buffer at aDoclist */ + u8 bDesc; /* True to sort in descending order */ + int eEvalmode; /* An FTS3_EVAL_XX constant */ + int nRowAvg; /* Average size of database rows, in pages */ + sqlite3_int64 nDoc; /* Documents in table */ + i64 iMinDocid; /* Minimum docid to return */ + i64 iMaxDocid; /* Maximum docid to return */ + int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */ + MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */ +}; + +#define FTS3_EVAL_FILTER 0 +#define FTS3_EVAL_NEXT 1 +#define FTS3_EVAL_MATCHINFO 2 + +/* +** The Fts3Cursor.eSearch member is always set to one of the following. +** Actualy, Fts3Cursor.eSearch can be greater than or equal to +** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index +** of the column to be searched. For example, in +** +** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d); +** SELECT docid FROM ex1 WHERE b MATCH 'one two three'; +** +** Because the LHS of the MATCH operator is 2nd column "b", +** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a, +** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1" +** indicating that all columns should be searched, +** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4. +*/ +#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */ +#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */ +#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */ + +/* +** The lower 16-bits of the sqlite3_index_info.idxNum value set by +** the xBestIndex() method contains the Fts3Cursor.eSearch value described +** above. The upper 16-bits contain a combination of the following +** bits, used to describe extra constraints on full-text searches. +*/ +#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */ +#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */ +#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */ + +struct Fts3Doclist { + char *aAll; /* Array containing doclist (or NULL) */ + int nAll; /* Size of a[] in bytes */ + char *pNextDocid; /* Pointer to next docid */ + + sqlite3_int64 iDocid; /* Current docid (if pList!=0) */ + int bFreeList; /* True if pList should be sqlite3_free()d */ + char *pList; /* Pointer to position list following iDocid */ + int nList; /* Length of position list */ +}; + +/* +** A "phrase" is a sequence of one or more tokens that must match in +** sequence. A single token is the base case and the most common case. +** For a sequence of tokens contained in double-quotes (i.e. "one two three") +** nToken will be the number of tokens in the string. +*/ +struct Fts3PhraseToken { + char *z; /* Text of the token */ + int n; /* Number of bytes in buffer z */ + int isPrefix; /* True if token ends with a "*" character */ + int bFirst; /* True if token must appear at position 0 */ + + /* Variables above this point are populated when the expression is + ** parsed (by code in fts3_expr.c). Below this point the variables are + ** used when evaluating the expression. */ + Fts3DeferredToken *pDeferred; /* Deferred token object for this token */ + Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */ +}; + +struct Fts3Phrase { + /* Cache of doclist for this phrase. */ + Fts3Doclist doclist; + int bIncr; /* True if doclist is loaded incrementally */ + int iDoclistToken; + + /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an + ** OR condition. */ + char *pOrPoslist; + i64 iOrDocid; + + /* Variables below this point are populated by fts3_expr.c when parsing + ** a MATCH expression. Everything above is part of the evaluation phase. + */ + int nToken; /* Number of tokens in the phrase */ + int iColumn; /* Index of column this phrase must match */ + Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */ +}; + +/* +** A tree of these objects forms the RHS of a MATCH operator. +** +** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist +** points to a malloced buffer, size nDoclist bytes, containing the results +** of this phrase query in FTS3 doclist format. As usual, the initial +** "Length" field found in doclists stored on disk is omitted from this +** buffer. +** +** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global +** matchinfo data. If it is not NULL, it points to an array of size nCol*3, +** where nCol is the number of columns in the queried FTS table. The array +** is populated as follows: +** +** aMI[iCol*3 + 0] = Undefined +** aMI[iCol*3 + 1] = Number of occurrences +** aMI[iCol*3 + 2] = Number of rows containing at least one instance +** +** The aMI array is allocated using sqlite3_malloc(). It should be freed +** when the expression node is. +*/ +struct Fts3Expr { + int eType; /* One of the FTSQUERY_XXX values defined below */ + int nNear; /* Valid if eType==FTSQUERY_NEAR */ + Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */ + Fts3Expr *pLeft; /* Left operand */ + Fts3Expr *pRight; /* Right operand */ + Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */ + + /* The following are used by the fts3_eval.c module. */ + sqlite3_int64 iDocid; /* Current docid */ + u8 bEof; /* True this expression is at EOF already */ + u8 bStart; /* True if iDocid is valid */ + u8 bDeferred; /* True if this expression is entirely deferred */ + + /* The following are used by the fts3_snippet.c module. */ + int iPhrase; /* Index of this phrase in matchinfo() results */ + u32 *aMI; /* See above */ +}; + +/* +** Candidate values for Fts3Query.eType. Note that the order of the first +** four values is in order of precedence when parsing expressions. For +** example, the following: +** +** "a OR b AND c NOT d NEAR e" +** +** is equivalent to: +** +** "a OR (b AND (c NOT (d NEAR e)))" +*/ +#define FTSQUERY_NEAR 1 +#define FTSQUERY_NOT 2 +#define FTSQUERY_AND 3 +#define FTSQUERY_OR 4 +#define FTSQUERY_PHRASE 5 + + +/* fts3_write.c */ +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*); +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *); +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64, + sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**); +SQLITE_PRIVATE int sqlite3Fts3SegReaderPending( + Fts3Table*,int,const char*,int,int,Fts3SegReader**); +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *); +SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **); +SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*); + +SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **); +SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **); + +#ifndef SQLITE_DISABLE_FTS4_DEFERRED +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *); +SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int); +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *); +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *); +SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *); +#else +# define sqlite3Fts3FreeDeferredTokens(x) +# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK +# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK +# define sqlite3Fts3FreeDeferredDoclists(x) +# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK +#endif + +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *); + +/* Special values interpreted by sqlite3SegReaderCursor() */ +#define FTS3_SEGCURSOR_PENDING -1 +#define FTS3_SEGCURSOR_ALL -2 + +SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*); +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *); +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *); + +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *, + int, int, int, const char *, int, int, int, Fts3MultiSegReader *); + +/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */ +#define FTS3_SEGMENT_REQUIRE_POS 0x00000001 +#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 +#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 +#define FTS3_SEGMENT_PREFIX 0x00000008 +#define FTS3_SEGMENT_SCAN 0x00000010 +#define FTS3_SEGMENT_FIRST 0x00000020 + +/* Type passed as 4th argument to SegmentReaderIterate() */ +struct Fts3SegFilter { + const char *zTerm; + int nTerm; + int iCol; + int flags; +}; + +struct Fts3MultiSegReader { + /* Used internally by sqlite3Fts3SegReaderXXX() calls */ + Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */ + int nSegment; /* Size of apSegment array */ + int nAdvance; /* How many seg-readers to advance */ + Fts3SegFilter *pFilter; /* Pointer to filter object */ + char *aBuffer; /* Buffer to merge doclists in */ + int nBuffer; /* Allocated size of aBuffer[] in bytes */ + + int iColFilter; /* If >=0, filter for this column */ + int bRestart; + + /* Used by fts3.c only. */ + int nCost; /* Cost of running iterator */ + int bLookup; /* True if a lookup of a single entry. */ + + /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */ + char *zTerm; /* Pointer to term buffer */ + int nTerm; /* Size of zTerm in bytes */ + char *aDoclist; /* Pointer to doclist buffer */ + int nDoclist; /* Size of aDoclist[] in bytes */ +}; + +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int); + +#define fts3GetVarint32(p, piVal) ( \ + (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \ +) + +/* fts3.c */ +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...); +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64); +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); +SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *); +SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64); +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *); +SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*); +SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*); +SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); + +/* fts3_tokenizer.c */ +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); +SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, + sqlite3_tokenizer **, char ** +); +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char); + +/* fts3_snippet.c */ +SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*); +SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *, + const char *, const char *, int, int +); +SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *); +SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p); + +/* fts3_expr.c */ +SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int, + char **, int, int, int, const char *, int, Fts3Expr **, char ** +); +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *); +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db); +SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db); +#endif + +SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int, + sqlite3_tokenizer_cursor ** +); + +/* fts3_aux.c */ +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db); + +SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *); + +SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart( + Fts3Table*, Fts3MultiSegReader*, int, const char*, int); +SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext( + Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *); +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); +SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *); +SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr); + +/* fts3_tokenize_vtab.c */ +SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *); + +/* fts3_unicode2.c (functions generated by parsing unicode text files) */ +#ifndef SQLITE_DISABLE_FTS3_UNICODE +SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int); +SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int); +SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int); +#endif + +#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */ +#endif /* _FTSINT_H */ + +/************** End of fts3Int.h *********************************************/ +/************** Continuing where we left off in fts3.c ***********************/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) +# define SQLITE_CORE 1 +#endif + +/* #include */ +/* #include */ +/* #include */ +/* #include */ +/* #include */ +/* #include */ + +/* #include "fts3.h" */ +#ifndef SQLITE_CORE +/* # include "sqlite3ext.h" */ + SQLITE_EXTENSION_INIT1 +#endif + +static int fts3EvalNext(Fts3Cursor *pCsr); +static int fts3EvalStart(Fts3Cursor *pCsr); +static int fts3TermSegReaderCursor( + Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **); + +#ifndef SQLITE_AMALGAMATION +# if defined(SQLITE_DEBUG) +SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; } +SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; } +# endif +#endif + +/* +** Write a 64-bit variable-length integer to memory starting at p[0]. +** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. +** The number of bytes written is returned. +*/ +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){ + unsigned char *q = (unsigned char *) p; + sqlite_uint64 vu = v; + do{ + *q++ = (unsigned char) ((vu & 0x7f) | 0x80); + vu >>= 7; + }while( vu!=0 ); + q[-1] &= 0x7f; /* turn off high bit in final byte */ + assert( q - (unsigned char *)p <= FTS3_VARINT_MAX ); + return (int) (q - (unsigned char *)p); +} + +#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \ + v = (v & mask1) | ( (*ptr++) << shift ); \ + if( (v & mask2)==0 ){ var = v; return ret; } +#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \ + v = (*ptr++); \ + if( (v & mask2)==0 ){ var = v; return ret; } + +/* +** Read a 64-bit variable-length integer from memory starting at p[0]. +** Return the number of bytes read, or 0 on error. +** The value is stored in *v. +*/ +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ + const char *pStart = p; + u32 a; + u64 b; + int shift; + + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1); + GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2); + GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3); + GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4); + b = (a & 0x0FFFFFFF ); + + for(shift=28; shift<=63; shift+=7){ + u64 c = *p++; + b += (c&0x7F) << shift; + if( (c & 0x80)==0 ) break; + } + *v = b; + return (int)(p - pStart); +} + +/* +** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a +** 32-bit integer before it is returned. +*/ +SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){ + u32 a; + +#ifndef fts3GetVarint32 + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1); +#else + a = (*p++); + assert( a & 0x80 ); +#endif + + GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *pi, 2); + GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *pi, 3); + GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4); + a = (a & 0x0FFFFFFF ); + *pi = (int)(a | ((u32)(*p & 0x0F) << 28)); + return 5; +} + +/* +** Return the number of bytes required to encode v as a varint +*/ +SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){ + int i = 0; + do{ + i++; + v >>= 7; + }while( v!=0 ); + return i; +} + +/* +** Convert an SQL-style quoted string into a normal string by removing +** the quote characters. The conversion is done in-place. If the +** input does not begin with a quote character, then this routine +** is a no-op. +** +** Examples: +** +** "abc" becomes abc +** 'xyz' becomes xyz +** [pqr] becomes pqr +** `mno` becomes mno +** +*/ +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){ + char quote; /* Quote character (if any ) */ + + quote = z[0]; + if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){ + int iIn = 1; /* Index of next byte to read from input */ + int iOut = 0; /* Index of next byte to write to output */ + + /* If the first byte was a '[', then the close-quote character is a ']' */ + if( quote=='[' ) quote = ']'; + + while( z[iIn] ){ + if( z[iIn]==quote ){ + if( z[iIn+1]!=quote ) break; + z[iOut++] = quote; + iIn += 2; + }else{ + z[iOut++] = z[iIn++]; + } + } + z[iOut] = '\0'; + } +} + +/* +** Read a single varint from the doclist at *pp and advance *pp to point +** to the first byte past the end of the varint. Add the value of the varint +** to *pVal. +*/ +static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ + sqlite3_int64 iVal; + *pp += sqlite3Fts3GetVarint(*pp, &iVal); + *pVal += iVal; +} + +/* +** When this function is called, *pp points to the first byte following a +** varint that is part of a doclist (or position-list, or any other list +** of varints). This function moves *pp to point to the start of that varint, +** and sets *pVal by the varint value. +** +** Argument pStart points to the first byte of the doclist that the +** varint is part of. +*/ +static void fts3GetReverseVarint( + char **pp, + char *pStart, + sqlite3_int64 *pVal +){ + sqlite3_int64 iVal; + char *p; + + /* Pointer p now points at the first byte past the varint we are + ** interested in. So, unless the doclist is corrupt, the 0x80 bit is + ** clear on character p[-1]. */ + for(p = (*pp)-2; p>=pStart && *p&0x80; p--); + p++; + *pp = p; + + sqlite3Fts3GetVarint(p, &iVal); + *pVal = iVal; +} + +/* +** The xDisconnect() virtual table method. +*/ +static int fts3DisconnectMethod(sqlite3_vtab *pVtab){ + Fts3Table *p = (Fts3Table *)pVtab; + int i; + + assert( p->nPendingData==0 ); + assert( p->pSegments==0 ); + + /* Free any prepared statements held */ + for(i=0; iaStmt); i++){ + sqlite3_finalize(p->aStmt[i]); + } + sqlite3_free(p->zSegmentsTbl); + sqlite3_free(p->zReadExprlist); + sqlite3_free(p->zWriteExprlist); + sqlite3_free(p->zContentTbl); + sqlite3_free(p->zLanguageid); + + /* Invoke the tokenizer destructor to free the tokenizer. */ + p->pTokenizer->pModule->xDestroy(p->pTokenizer); + + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Write an error message into *pzErr +*/ +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){ + va_list ap; + sqlite3_free(*pzErr); + va_start(ap, zFormat); + *pzErr = sqlite3_vmprintf(zFormat, ap); + va_end(ap); +} + +/* +** Construct one or more SQL statements from the format string given +** and then evaluate those statements. The success code is written +** into *pRc. +** +** If *pRc is initially non-zero then this routine is a no-op. +*/ +static void fts3DbExec( + int *pRc, /* Success code */ + sqlite3 *db, /* Database in which to run SQL */ + const char *zFormat, /* Format string for SQL */ + ... /* Arguments to the format string */ +){ + va_list ap; + char *zSql; + if( *pRc ) return; + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( zSql==0 ){ + *pRc = SQLITE_NOMEM; + }else{ + *pRc = sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + } +} + +/* +** The xDestroy() virtual table method. +*/ +static int fts3DestroyMethod(sqlite3_vtab *pVtab){ + Fts3Table *p = (Fts3Table *)pVtab; + int rc = SQLITE_OK; /* Return code */ + const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */ + sqlite3 *db = p->db; /* Database handle */ + + /* Drop the shadow tables */ + if( p->zContentTbl==0 ){ + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName); + } + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName); + + /* If everything has worked, invoke fts3DisconnectMethod() to free the + ** memory associated with the Fts3Table structure and return SQLITE_OK. + ** Otherwise, return an SQLite error code. + */ + return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc); +} + + +/* +** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table +** passed as the first argument. This is done as part of the xConnect() +** and xCreate() methods. +** +** If *pRc is non-zero when this function is called, it is a no-op. +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc +** before returning. +*/ +static void fts3DeclareVtab(int *pRc, Fts3Table *p){ + if( *pRc==SQLITE_OK ){ + int i; /* Iterator variable */ + int rc; /* Return code */ + char *zSql; /* SQL statement passed to declare_vtab() */ + char *zCols; /* List of user defined columns */ + const char *zLanguageid; + + zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid"); + sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); + + /* Create a list of user columns for the virtual table */ + zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]); + for(i=1; zCols && inColumn; i++){ + zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]); + } + + /* Create the whole "CREATE TABLE" statement to pass to SQLite */ + zSql = sqlite3_mprintf( + "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)", + zCols, p->zName, zLanguageid + ); + if( !zCols || !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_declare_vtab(p->db, zSql); + } + + sqlite3_free(zSql); + sqlite3_free(zCols); + *pRc = rc; + } +} + +/* +** Create the %_stat table if it does not already exist. +*/ +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){ + fts3DbExec(pRc, p->db, + "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'" + "(id INTEGER PRIMARY KEY, value BLOB);", + p->zDb, p->zName + ); + if( (*pRc)==SQLITE_OK ) p->bHasStat = 1; +} + +/* +** Create the backing store tables (%_content, %_segments and %_segdir) +** required by the FTS3 table passed as the only argument. This is done +** as part of the vtab xCreate() method. +** +** If the p->bHasDocsize boolean is true (indicating that this is an +** FTS4 table, not an FTS3 table) then also create the %_docsize and +** %_stat tables required by FTS4. +*/ +static int fts3CreateTables(Fts3Table *p){ + int rc = SQLITE_OK; /* Return code */ + int i; /* Iterator variable */ + sqlite3 *db = p->db; /* The database connection */ + + if( p->zContentTbl==0 ){ + const char *zLanguageid = p->zLanguageid; + char *zContentCols; /* Columns of %_content table */ + + /* Create a list of user columns for the content table */ + zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); + for(i=0; zContentCols && inColumn; i++){ + char *z = p->azColumn[i]; + zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); + } + if( zLanguageid && zContentCols ){ + zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid); + } + if( zContentCols==0 ) rc = SQLITE_NOMEM; + + /* Create the content table */ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_content'(%s)", + p->zDb, p->zName, zContentCols + ); + sqlite3_free(zContentCols); + } + + /* Create other tables */ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);", + p->zDb, p->zName + ); + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_segdir'(" + "level INTEGER," + "idx INTEGER," + "start_block INTEGER," + "leaves_end_block INTEGER," + "end_block INTEGER," + "root BLOB," + "PRIMARY KEY(level, idx)" + ");", + p->zDb, p->zName + ); + if( p->bHasDocsize ){ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);", + p->zDb, p->zName + ); + } + assert( p->bHasStat==p->bFts4 ); + if( p->bHasStat ){ + sqlite3Fts3CreateStatTable(&rc, p); + } + return rc; +} + +/* +** Store the current database page-size in bytes in p->nPgsz. +** +** If *pRc is non-zero when this function is called, it is a no-op. +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc +** before returning. +*/ +static void fts3DatabasePageSize(int *pRc, Fts3Table *p){ + if( *pRc==SQLITE_OK ){ + int rc; /* Return code */ + char *zSql; /* SQL text "PRAGMA %Q.page_size" */ + sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */ + + zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_step(pStmt); + p->nPgsz = sqlite3_column_int(pStmt, 0); + rc = sqlite3_finalize(pStmt); + }else if( rc==SQLITE_AUTH ){ + p->nPgsz = 1024; + rc = SQLITE_OK; + } + } + assert( p->nPgsz>0 || rc!=SQLITE_OK ); + sqlite3_free(zSql); + *pRc = rc; + } +} + +/* +** "Special" FTS4 arguments are column specifications of the following form: +** +** = +** +** There may not be whitespace surrounding the "=" character. The +** term may be quoted, but the may not. +*/ +static int fts3IsSpecialColumn( + const char *z, + int *pnKey, + char **pzValue +){ + char *zValue; + const char *zCsr = z; + + while( *zCsr!='=' ){ + if( *zCsr=='\0' ) return 0; + zCsr++; + } + + *pnKey = (int)(zCsr-z); + zValue = sqlite3_mprintf("%s", &zCsr[1]); + if( zValue ){ + sqlite3Fts3Dequote(zValue); + } + *pzValue = zValue; + return 1; +} + +/* +** Append the output of a printf() style formatting to an existing string. +*/ +static void fts3Appendf( + int *pRc, /* IN/OUT: Error code */ + char **pz, /* IN/OUT: Pointer to string buffer */ + const char *zFormat, /* Printf format string to append */ + ... /* Arguments for printf format string */ +){ + if( *pRc==SQLITE_OK ){ + va_list ap; + char *z; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( z && *pz ){ + char *z2 = sqlite3_mprintf("%s%s", *pz, z); + sqlite3_free(z); + z = z2; + } + if( z==0 ) *pRc = SQLITE_NOMEM; + sqlite3_free(*pz); + *pz = z; + } +} + +/* +** Return a copy of input string zInput enclosed in double-quotes (") and +** with all double quote characters escaped. For example: +** +** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\"" +** +** The pointer returned points to memory obtained from sqlite3_malloc(). It +** is the callers responsibility to call sqlite3_free() to release this +** memory. +*/ +static char *fts3QuoteId(char const *zInput){ + int nRet; + char *zRet; + nRet = 2 + (int)strlen(zInput)*2 + 1; + zRet = sqlite3_malloc(nRet); + if( zRet ){ + int i; + char *z = zRet; + *(z++) = '"'; + for(i=0; zInput[i]; i++){ + if( zInput[i]=='"' ) *(z++) = '"'; + *(z++) = zInput[i]; + } + *(z++) = '"'; + *(z++) = '\0'; + } + return zRet; +} + +/* +** Return a list of comma separated SQL expressions and a FROM clause that +** could be used in a SELECT statement such as the following: +** +** SELECT FROM %_content AS x ... +** +** to return the docid, followed by each column of text data in order +** from left to write. If parameter zFunc is not NULL, then instead of +** being returned directly each column of text data is passed to an SQL +** function named zFunc first. For example, if zFunc is "unzip" and the +** table has the three user-defined columns "a", "b", and "c", the following +** string is returned: +** +** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x" +** +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It +** is the responsibility of the caller to eventually free it. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and +** a NULL pointer is returned). Otherwise, if an OOM error is encountered +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If +** no error occurs, *pRc is left unmodified. +*/ +static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){ + char *zRet = 0; + char *zFree = 0; + char *zFunction; + int i; + + if( p->zContentTbl==0 ){ + if( !zFunc ){ + zFunction = ""; + }else{ + zFree = zFunction = fts3QuoteId(zFunc); + } + fts3Appendf(pRc, &zRet, "docid"); + for(i=0; inColumn; i++){ + fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); + } + if( p->zLanguageid ){ + fts3Appendf(pRc, &zRet, ", x.%Q", "langid"); + } + sqlite3_free(zFree); + }else{ + fts3Appendf(pRc, &zRet, "rowid"); + for(i=0; inColumn; i++){ + fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]); + } + if( p->zLanguageid ){ + fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid); + } + } + fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x", + p->zDb, + (p->zContentTbl ? p->zContentTbl : p->zName), + (p->zContentTbl ? "" : "_content") + ); + return zRet; +} + +/* +** Return a list of N comma separated question marks, where N is the number +** of columns in the %_content table (one for the docid plus one for each +** user-defined text column). +** +** If argument zFunc is not NULL, then all but the first question mark +** is preceded by zFunc and an open bracket, and followed by a closed +** bracket. For example, if zFunc is "zip" and the FTS3 table has three +** user-defined text columns, the following string is returned: +** +** "?, zip(?), zip(?), zip(?)" +** +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It +** is the responsibility of the caller to eventually free it. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and +** a NULL pointer is returned). Otherwise, if an OOM error is encountered +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If +** no error occurs, *pRc is left unmodified. +*/ +static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){ + char *zRet = 0; + char *zFree = 0; + char *zFunction; + int i; + + if( !zFunc ){ + zFunction = ""; + }else{ + zFree = zFunction = fts3QuoteId(zFunc); + } + fts3Appendf(pRc, &zRet, "?"); + for(i=0; inColumn; i++){ + fts3Appendf(pRc, &zRet, ",%s(?)", zFunction); + } + if( p->zLanguageid ){ + fts3Appendf(pRc, &zRet, ", ?"); + } + sqlite3_free(zFree); + return zRet; +} + +/* +** This function interprets the string at (*pp) as a non-negative integer +** value. It reads the integer and sets *pnOut to the value read, then +** sets *pp to point to the byte immediately following the last byte of +** the integer value. +** +** Only decimal digits ('0'..'9') may be part of an integer value. +** +** If *pp does not being with a decimal digit SQLITE_ERROR is returned and +** the output value undefined. Otherwise SQLITE_OK is returned. +** +** This function is used when parsing the "prefix=" FTS4 parameter. +*/ +static int fts3GobbleInt(const char **pp, int *pnOut){ + const int MAX_NPREFIX = 10000000; + const char *p; /* Iterator pointer */ + int nInt = 0; /* Output value */ + + for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ + nInt = nInt * 10 + (p[0] - '0'); + if( nInt>MAX_NPREFIX ){ + nInt = 0; + break; + } + } + if( p==*pp ) return SQLITE_ERROR; + *pnOut = nInt; + *pp = p; + return SQLITE_OK; +} + +/* +** This function is called to allocate an array of Fts3Index structures +** representing the indexes maintained by the current FTS table. FTS tables +** always maintain the main "terms" index, but may also maintain one or +** more "prefix" indexes, depending on the value of the "prefix=" parameter +** (if any) specified as part of the CREATE VIRTUAL TABLE statement. +** +** Argument zParam is passed the value of the "prefix=" option if one was +** specified, or NULL otherwise. +** +** If no error occurs, SQLITE_OK is returned and *apIndex set to point to +** the allocated array. *pnIndex is set to the number of elements in the +** array. If an error does occur, an SQLite error code is returned. +** +** Regardless of whether or not an error is returned, it is the responsibility +** of the caller to call sqlite3_free() on the output array to free it. +*/ +static int fts3PrefixParameter( + const char *zParam, /* ABC in prefix=ABC parameter to parse */ + int *pnIndex, /* OUT: size of *apIndex[] array */ + struct Fts3Index **apIndex /* OUT: Array of indexes for this table */ +){ + struct Fts3Index *aIndex; /* Allocated array */ + int nIndex = 1; /* Number of entries in array */ + + if( zParam && zParam[0] ){ + const char *p; + nIndex++; + for(p=zParam; *p; p++){ + if( *p==',' ) nIndex++; + } + } + + aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex); + *apIndex = aIndex; + if( !aIndex ){ + return SQLITE_NOMEM; + } + + memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex); + if( zParam ){ + const char *p = zParam; + int i; + for(i=1; i=0 ); + if( nPrefix==0 ){ + nIndex--; + i--; + }else{ + aIndex[i].nPrefix = nPrefix; + } + p++; + } + } + + *pnIndex = nIndex; + return SQLITE_OK; +} + +/* +** This function is called when initializing an FTS4 table that uses the +** content=xxx option. It determines the number of and names of the columns +** of the new FTS4 table. +** +** The third argument passed to this function is the value passed to the +** config=xxx option (i.e. "xxx"). This function queries the database for +** a table of that name. If found, the output variables are populated +** as follows: +** +** *pnCol: Set to the number of columns table xxx has, +** +** *pnStr: Set to the total amount of space required to store a copy +** of each columns name, including the nul-terminator. +** +** *pazCol: Set to point to an array of *pnCol strings. Each string is +** the name of the corresponding column in table xxx. The array +** and its contents are allocated using a single allocation. It +** is the responsibility of the caller to free this allocation +** by eventually passing the *pazCol value to sqlite3_free(). +** +** If the table cannot be found, an error code is returned and the output +** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is +** returned (and the output variables are undefined). +*/ +static int fts3ContentColumns( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */ + const char *zTbl, /* Name of content table */ + const char ***pazCol, /* OUT: Malloc'd array of column names */ + int *pnCol, /* OUT: Size of array *pazCol */ + int *pnStr, /* OUT: Bytes of string content */ + char **pzErr /* OUT: error message */ +){ + int rc = SQLITE_OK; /* Return code */ + char *zSql; /* "SELECT *" statement on zTbl */ + sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */ + + zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db)); + } + } + sqlite3_free(zSql); + + if( rc==SQLITE_OK ){ + const char **azCol; /* Output array */ + int nStr = 0; /* Size of all column names (incl. 0x00) */ + int nCol; /* Number of table columns */ + int i; /* Used to iterate through columns */ + + /* Loop through the returned columns. Set nStr to the number of bytes of + ** space required to store a copy of each column name, including the + ** nul-terminator byte. */ + nCol = sqlite3_column_count(pStmt); + for(i=0; i module name ("fts3" or "fts4") +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> "column name" and other module argument fields. +*/ +static int fts3InitVtab( + int isCreate, /* True for xCreate, false for xConnect */ + sqlite3 *db, /* The SQLite database connection */ + void *pAux, /* Hash table containing tokenizers */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */ + char **pzErr /* Write any error message here */ +){ + Fts3Hash *pHash = (Fts3Hash *)pAux; + Fts3Table *p = 0; /* Pointer to allocated vtab */ + int rc = SQLITE_OK; /* Return code */ + int i; /* Iterator variable */ + int nByte; /* Size of allocation used for *p */ + int iCol; /* Column index */ + int nString = 0; /* Bytes required to hold all column names */ + int nCol = 0; /* Number of columns in the FTS table */ + char *zCsr; /* Space for holding column names */ + int nDb; /* Bytes required to hold database name */ + int nName; /* Bytes required to hold table name */ + int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */ + const char **aCol; /* Array of column names */ + sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ + + int nIndex = 0; /* Size of aIndex[] array */ + struct Fts3Index *aIndex = 0; /* Array of indexes for this table */ + + /* The results of parsing supported FTS4 key=value options: */ + int bNoDocsize = 0; /* True to omit %_docsize table */ + int bDescIdx = 0; /* True to store descending indexes */ + char *zPrefix = 0; /* Prefix parameter value (or NULL) */ + char *zCompress = 0; /* compress=? parameter (or NULL) */ + char *zUncompress = 0; /* uncompress=? parameter (or NULL) */ + char *zContent = 0; /* content=? parameter (or NULL) */ + char *zLanguageid = 0; /* languageid=? parameter (or NULL) */ + char **azNotindexed = 0; /* The set of notindexed= columns */ + int nNotindexed = 0; /* Size of azNotindexed[] array */ + + assert( strlen(argv[0])==4 ); + assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4) + || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4) + ); + + nDb = (int)strlen(argv[1]) + 1; + nName = (int)strlen(argv[2]) + 1; + + nByte = sizeof(const char *) * (argc-2); + aCol = (const char **)sqlite3_malloc(nByte); + if( aCol ){ + memset((void*)aCol, 0, nByte); + azNotindexed = (char **)sqlite3_malloc(nByte); + } + if( azNotindexed ){ + memset(azNotindexed, 0, nByte); + } + if( !aCol || !azNotindexed ){ + rc = SQLITE_NOMEM; + goto fts3_init_out; + } + + /* Loop through all of the arguments passed by the user to the FTS3/4 + ** module (i.e. all the column names and special arguments). This loop + ** does the following: + ** + ** + Figures out the number of columns the FTSX table will have, and + ** the number of bytes of space that must be allocated to store copies + ** of the column names. + ** + ** + If there is a tokenizer specification included in the arguments, + ** initializes the tokenizer pTokenizer. + */ + for(i=3; rc==SQLITE_OK && i8 + && 0==sqlite3_strnicmp(z, "tokenize", 8) + && 0==sqlite3Fts3IsIdChar(z[8]) + ){ + rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr); + } + + /* Check if it is an FTS4 special argument. */ + else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){ + struct Fts4Option { + const char *zOpt; + int nOpt; + } aFts4Opt[] = { + { "matchinfo", 9 }, /* 0 -> MATCHINFO */ + { "prefix", 6 }, /* 1 -> PREFIX */ + { "compress", 8 }, /* 2 -> COMPRESS */ + { "uncompress", 10 }, /* 3 -> UNCOMPRESS */ + { "order", 5 }, /* 4 -> ORDER */ + { "content", 7 }, /* 5 -> CONTENT */ + { "languageid", 10 }, /* 6 -> LANGUAGEID */ + { "notindexed", 10 } /* 7 -> NOTINDEXED */ + }; + + int iOpt; + if( !zVal ){ + rc = SQLITE_NOMEM; + }else{ + for(iOpt=0; iOptnOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){ + break; + } + } + if( iOpt==SizeofArray(aFts4Opt) ){ + sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z); + rc = SQLITE_ERROR; + }else{ + switch( iOpt ){ + case 0: /* MATCHINFO */ + if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){ + sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal); + rc = SQLITE_ERROR; + } + bNoDocsize = 1; + break; + + case 1: /* PREFIX */ + sqlite3_free(zPrefix); + zPrefix = zVal; + zVal = 0; + break; + + case 2: /* COMPRESS */ + sqlite3_free(zCompress); + zCompress = zVal; + zVal = 0; + break; + + case 3: /* UNCOMPRESS */ + sqlite3_free(zUncompress); + zUncompress = zVal; + zVal = 0; + break; + + case 4: /* ORDER */ + if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) + && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) + ){ + sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal); + rc = SQLITE_ERROR; + } + bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); + break; + + case 5: /* CONTENT */ + sqlite3_free(zContent); + zContent = zVal; + zVal = 0; + break; + + case 6: /* LANGUAGEID */ + assert( iOpt==6 ); + sqlite3_free(zLanguageid); + zLanguageid = zVal; + zVal = 0; + break; + + case 7: /* NOTINDEXED */ + azNotindexed[nNotindexed++] = zVal; + zVal = 0; + break; + } + } + sqlite3_free(zVal); + } + } + + /* Otherwise, the argument is a column name. */ + else { + nString += (int)(strlen(z) + 1); + aCol[nCol++] = z; + } + } + + /* If a content=xxx option was specified, the following: + ** + ** 1. Ignore any compress= and uncompress= options. + ** + ** 2. If no column names were specified as part of the CREATE VIRTUAL + ** TABLE statement, use all columns from the content table. + */ + if( rc==SQLITE_OK && zContent ){ + sqlite3_free(zCompress); + sqlite3_free(zUncompress); + zCompress = 0; + zUncompress = 0; + if( nCol==0 ){ + sqlite3_free((void*)aCol); + aCol = 0; + rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr); + + /* If a languageid= option was specified, remove the language id + ** column from the aCol[] array. */ + if( rc==SQLITE_OK && zLanguageid ){ + int j; + for(j=0; jdb = db; + p->nColumn = nCol; + p->nPendingData = 0; + p->azColumn = (char **)&p[1]; + p->pTokenizer = pTokenizer; + p->nMaxPendingData = FTS3_MAX_PENDING_DATA; + p->bHasDocsize = (isFts4 && bNoDocsize==0); + p->bHasStat = isFts4; + p->bFts4 = isFts4; + p->bDescIdx = bDescIdx; + p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */ + p->zContentTbl = zContent; + p->zLanguageid = zLanguageid; + zContent = 0; + zLanguageid = 0; + TESTONLY( p->inTransaction = -1 ); + TESTONLY( p->mxSavepoint = -1 ); + + p->aIndex = (struct Fts3Index *)&p->azColumn[nCol]; + memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex); + p->nIndex = nIndex; + for(i=0; iaIndex[i].hPending, FTS3_HASH_STRING, 1); + } + p->abNotindexed = (u8 *)&p->aIndex[nIndex]; + + /* Fill in the zName and zDb fields of the vtab structure. */ + zCsr = (char *)&p->abNotindexed[nCol]; + p->zName = zCsr; + memcpy(zCsr, argv[2], nName); + zCsr += nName; + p->zDb = zCsr; + memcpy(zCsr, argv[1], nDb); + zCsr += nDb; + + /* Fill in the azColumn array */ + for(iCol=0; iColazColumn[iCol] = zCsr; + zCsr += n+1; + assert( zCsr <= &((char *)p)[nByte] ); + } + + /* Fill in the abNotindexed array */ + for(iCol=0; iColazColumn[iCol]); + for(i=0; iazColumn[iCol], zNot, n) + ){ + p->abNotindexed[iCol] = 1; + sqlite3_free(zNot); + azNotindexed[i] = 0; + } + } + } + for(i=0; izReadExprlist = fts3ReadExprList(p, zUncompress, &rc); + p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc); + if( rc!=SQLITE_OK ) goto fts3_init_out; + + /* If this is an xCreate call, create the underlying tables in the + ** database. TODO: For xConnect(), it could verify that said tables exist. + */ + if( isCreate ){ + rc = fts3CreateTables(p); + } + + /* Check to see if a legacy fts3 table has been "upgraded" by the + ** addition of a %_stat table so that it can use incremental merge. + */ + if( !isFts4 && !isCreate ){ + p->bHasStat = 2; + } + + /* Figure out the page-size for the database. This is required in order to + ** estimate the cost of loading large doclists from the database. */ + fts3DatabasePageSize(&rc, p); + p->nNodeSize = p->nPgsz-35; + + /* Declare the table schema to SQLite. */ + fts3DeclareVtab(&rc, p); + +fts3_init_out: + sqlite3_free(zPrefix); + sqlite3_free(aIndex); + sqlite3_free(zCompress); + sqlite3_free(zUncompress); + sqlite3_free(zContent); + sqlite3_free(zLanguageid); + for(i=0; ipModule->xDestroy(pTokenizer); + } + }else{ + assert( p->pSegments==0 ); + *ppVTab = &p->base; + } + return rc; +} + +/* +** The xConnect() and xCreate() methods for the virtual table. All the +** work is done in function fts3InitVtab(). +*/ +static int fts3ConnectMethod( + sqlite3 *db, /* Database connection */ + void *pAux, /* Pointer to tokenizer hash table */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr); +} +static int fts3CreateMethod( + sqlite3 *db, /* Database connection */ + void *pAux, /* Pointer to tokenizer hash table */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr); +} + +/* +** Set the pIdxInfo->estimatedRows variable to nRow. Unless this +** extension is currently being used by a version of SQLite too old to +** support estimatedRows. In that case this function is a no-op. +*/ +static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){ +#if SQLITE_VERSION_NUMBER>=3008002 + if( sqlite3_libversion_number()>=3008002 ){ + pIdxInfo->estimatedRows = nRow; + } +#endif +} + +/* +** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this +** extension is currently being used by a version of SQLite too old to +** support index-info flags. In that case this function is a no-op. +*/ +static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){ +#if SQLITE_VERSION_NUMBER>=3008012 + if( sqlite3_libversion_number()>=3008012 ){ + pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE; + } +#endif +} + +/* +** Implementation of the xBestIndex method for FTS3 tables. There +** are three possible strategies, in order of preference: +** +** 1. Direct lookup by rowid or docid. +** 2. Full-text search using a MATCH operator on a non-docid column. +** 3. Linear scan of %_content table. +*/ +static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ + Fts3Table *p = (Fts3Table *)pVTab; + int i; /* Iterator variable */ + int iCons = -1; /* Index of constraint to use */ + + int iLangidCons = -1; /* Index of langid=x constraint, if present */ + int iDocidGe = -1; /* Index of docid>=x constraint, if present */ + int iDocidLe = -1; /* Index of docid<=x constraint, if present */ + int iIdx; + + /* By default use a full table scan. This is an expensive option, + ** so search through the constraints to see if a more efficient + ** strategy is possible. + */ + pInfo->idxNum = FTS3_FULLSCAN_SEARCH; + pInfo->estimatedCost = 5000000; + for(i=0; inConstraint; i++){ + int bDocid; /* True if this constraint is on docid */ + struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; + if( pCons->usable==0 ){ + if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ + /* There exists an unusable MATCH constraint. This means that if + ** the planner does elect to use the results of this call as part + ** of the overall query plan the user will see an "unable to use + ** function MATCH in the requested context" error. To discourage + ** this, return a very high cost here. */ + pInfo->idxNum = FTS3_FULLSCAN_SEARCH; + pInfo->estimatedCost = 1e50; + fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50); + return SQLITE_OK; + } + continue; + } + + bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1); + + /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ + if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){ + pInfo->idxNum = FTS3_DOCID_SEARCH; + pInfo->estimatedCost = 1.0; + iCons = i; + } + + /* A MATCH constraint. Use a full-text search. + ** + ** If there is more than one MATCH constraint available, use the first + ** one encountered. If there is both a MATCH constraint and a direct + ** rowid/docid lookup, prefer the MATCH strategy. This is done even + ** though the rowid/docid lookup is faster than a MATCH query, selecting + ** it would lead to an "unable to use function MATCH in the requested + ** context" error. + */ + if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH + && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn + ){ + pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn; + pInfo->estimatedCost = 2.0; + iCons = i; + } + + /* Equality constraint on the langid column */ + if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ + && pCons->iColumn==p->nColumn + 2 + ){ + iLangidCons = i; + } + + if( bDocid ){ + switch( pCons->op ){ + case SQLITE_INDEX_CONSTRAINT_GE: + case SQLITE_INDEX_CONSTRAINT_GT: + iDocidGe = i; + break; + + case SQLITE_INDEX_CONSTRAINT_LE: + case SQLITE_INDEX_CONSTRAINT_LT: + iDocidLe = i; + break; + } + } + } + + /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */ + if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo); + + iIdx = 1; + if( iCons>=0 ){ + pInfo->aConstraintUsage[iCons].argvIndex = iIdx++; + pInfo->aConstraintUsage[iCons].omit = 1; + } + if( iLangidCons>=0 ){ + pInfo->idxNum |= FTS3_HAVE_LANGID; + pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++; + } + if( iDocidGe>=0 ){ + pInfo->idxNum |= FTS3_HAVE_DOCID_GE; + pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++; + } + if( iDocidLe>=0 ){ + pInfo->idxNum |= FTS3_HAVE_DOCID_LE; + pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++; + } + + /* Regardless of the strategy selected, FTS can deliver rows in rowid (or + ** docid) order. Both ascending and descending are possible. + */ + if( pInfo->nOrderBy==1 ){ + struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0]; + if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){ + if( pOrder->desc ){ + pInfo->idxStr = "DESC"; + }else{ + pInfo->idxStr = "ASC"; + } + pInfo->orderByConsumed = 1; + } + } + + assert( p->pSegments==0 ); + return SQLITE_OK; +} + +/* +** Implementation of xOpen method. +*/ +static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ + sqlite3_vtab_cursor *pCsr; /* Allocated cursor */ + + UNUSED_PARAMETER(pVTab); + + /* Allocate a buffer large enough for an Fts3Cursor structure. If the + ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, + ** if the allocation fails, return SQLITE_NOMEM. + */ + *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor)); + if( !pCsr ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(Fts3Cursor)); + return SQLITE_OK; +} + +/* +** Close the cursor. For additional information see the documentation +** on the xClose method of the virtual table interface. +*/ +static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ + Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; + assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); + sqlite3_finalize(pCsr->pStmt); + sqlite3Fts3ExprFree(pCsr->pExpr); + sqlite3Fts3FreeDeferredTokens(pCsr); + sqlite3_free(pCsr->aDoclist); + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); + assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then +** compose and prepare an SQL statement of the form: +** +** "SELECT FROM %_content WHERE rowid = ?" +** +** (or the equivalent for a content=xxx table) and set pCsr->pStmt to +** it. If an error occurs, return an SQLite error code. +** +** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK. +*/ +static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ + int rc = SQLITE_OK; + if( pCsr->pStmt==0 ){ + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + char *zSql; + zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); + if( !zSql ) return SQLITE_NOMEM; + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); + sqlite3_free(zSql); + } + *ppStmt = pCsr->pStmt; + return rc; +} + +/* +** Position the pCsr->pStmt statement so that it is on the row +** of the %_content table that contains the last match. Return +** SQLITE_OK on success. +*/ +static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ + int rc = SQLITE_OK; + if( pCsr->isRequireSeek ){ + sqlite3_stmt *pStmt = 0; + + rc = fts3CursorSeekStmt(pCsr, &pStmt); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); + pCsr->isRequireSeek = 0; + if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ + return SQLITE_OK; + }else{ + rc = sqlite3_reset(pCsr->pStmt); + if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){ + /* If no row was found and no error has occurred, then the %_content + ** table is missing a row that is present in the full-text index. + ** The data structures are corrupt. */ + rc = FTS_CORRUPT_VTAB; + pCsr->isEof = 1; + } + } + } + } + + if( rc!=SQLITE_OK && pContext ){ + sqlite3_result_error_code(pContext, rc); + } + return rc; +} + +/* +** This function is used to process a single interior node when searching +** a b-tree for a term or term prefix. The node data is passed to this +** function via the zNode/nNode parameters. The term to search for is +** passed in zTerm/nTerm. +** +** If piFirst is not NULL, then this function sets *piFirst to the blockid +** of the child node that heads the sub-tree that may contain the term. +** +** If piLast is not NULL, then *piLast is set to the right-most child node +** that heads a sub-tree that may contain a term for which zTerm/nTerm is +** a prefix. +** +** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. +*/ +static int fts3ScanInteriorNode( + const char *zTerm, /* Term to select leaves for */ + int nTerm, /* Size of term zTerm in bytes */ + const char *zNode, /* Buffer containing segment interior node */ + int nNode, /* Size of buffer at zNode */ + sqlite3_int64 *piFirst, /* OUT: Selected child node */ + sqlite3_int64 *piLast /* OUT: Selected child node */ +){ + int rc = SQLITE_OK; /* Return code */ + const char *zCsr = zNode; /* Cursor to iterate through node */ + const char *zEnd = &zCsr[nNode];/* End of interior node buffer */ + char *zBuffer = 0; /* Buffer to load terms into */ + int nAlloc = 0; /* Size of allocated buffer */ + int isFirstTerm = 1; /* True when processing first term on page */ + sqlite3_int64 iChild; /* Block id of child node to descend to */ + + /* Skip over the 'height' varint that occurs at the start of every + ** interior node. Then load the blockid of the left-child of the b-tree + ** node into variable iChild. + ** + ** Even if the data structure on disk is corrupted, this (reading two + ** varints from the buffer) does not risk an overread. If zNode is a + ** root node, then the buffer comes from a SELECT statement. SQLite does + ** not make this guarantee explicitly, but in practice there are always + ** either more than 20 bytes of allocated space following the nNode bytes of + ** contents, or two zero bytes. Or, if the node is read from the %_segments + ** table, then there are always 20 bytes of zeroed padding following the + ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). + */ + zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); + zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); + if( zCsr>zEnd ){ + return FTS_CORRUPT_VTAB; + } + + while( zCsrzEnd ){ + rc = FTS_CORRUPT_VTAB; + goto finish_scan; + } + if( nPrefix+nSuffix>nAlloc ){ + char *zNew; + nAlloc = (nPrefix+nSuffix) * 2; + zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); + if( !zNew ){ + rc = SQLITE_NOMEM; + goto finish_scan; + } + zBuffer = zNew; + } + assert( zBuffer ); + memcpy(&zBuffer[nPrefix], zCsr, nSuffix); + nBuffer = nPrefix + nSuffix; + zCsr += nSuffix; + + /* Compare the term we are searching for with the term just loaded from + ** the interior node. If the specified term is greater than or equal + ** to the term from the interior node, then all terms on the sub-tree + ** headed by node iChild are smaller than zTerm. No need to search + ** iChild. + ** + ** If the interior node term is larger than the specified term, then + ** the tree headed by iChild may contain the specified term. + */ + cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer)); + if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){ + *piFirst = iChild; + piFirst = 0; + } + + if( piLast && cmp<0 ){ + *piLast = iChild; + piLast = 0; + } + + iChild++; + }; + + if( piFirst ) *piFirst = iChild; + if( piLast ) *piLast = iChild; + + finish_scan: + sqlite3_free(zBuffer); + return rc; +} + + +/* +** The buffer pointed to by argument zNode (size nNode bytes) contains an +** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes) +** contains a term. This function searches the sub-tree headed by the zNode +** node for the range of leaf nodes that may contain the specified term +** or terms for which the specified term is a prefix. +** +** If piLeaf is not NULL, then *piLeaf is set to the blockid of the +** left-most leaf node in the tree that may contain the specified term. +** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the +** right-most leaf node that may contain a term for which the specified +** term is a prefix. +** +** It is possible that the range of returned leaf nodes does not contain +** the specified term or any terms for which it is a prefix. However, if the +** segment does contain any such terms, they are stored within the identified +** range. Because this function only inspects interior segment nodes (and +** never loads leaf nodes into memory), it is not possible to be sure. +** +** If an error occurs, an error code other than SQLITE_OK is returned. +*/ +static int fts3SelectLeaf( + Fts3Table *p, /* Virtual table handle */ + const char *zTerm, /* Term to select leaves for */ + int nTerm, /* Size of term zTerm in bytes */ + const char *zNode, /* Buffer containing segment interior node */ + int nNode, /* Size of buffer at zNode */ + sqlite3_int64 *piLeaf, /* Selected leaf node */ + sqlite3_int64 *piLeaf2 /* Selected leaf node */ +){ + int rc = SQLITE_OK; /* Return code */ + int iHeight; /* Height of this node in tree */ + + assert( piLeaf || piLeaf2 ); + + fts3GetVarint32(zNode, &iHeight); + rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); + assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); + + if( rc==SQLITE_OK && iHeight>1 ){ + char *zBlob = 0; /* Blob read from %_segments table */ + int nBlob = 0; /* Size of zBlob in bytes */ + + if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){ + rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0); + if( rc==SQLITE_OK ){ + rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0); + } + sqlite3_free(zBlob); + piLeaf = 0; + zBlob = 0; + } + + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0); + } + if( rc==SQLITE_OK ){ + rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); + } + sqlite3_free(zBlob); + } + + return rc; +} + +/* +** This function is used to create delta-encoded serialized lists of FTS3 +** varints. Each call to this function appends a single varint to a list. +*/ +static void fts3PutDeltaVarint( + char **pp, /* IN/OUT: Output pointer */ + sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ + sqlite3_int64 iVal /* Write this value to the list */ +){ + assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) ); + *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev); + *piPrev = iVal; +} + +/* +** When this function is called, *ppPoslist is assumed to point to the +** start of a position-list. After it returns, *ppPoslist points to the +** first byte after the position-list. +** +** A position list is list of positions (delta encoded) and columns for +** a single document record of a doclist. So, in other words, this +** routine advances *ppPoslist so that it points to the next docid in +** the doclist, or to the first byte past the end of the doclist. +** +** If pp is not NULL, then the contents of the position list are copied +** to *pp. *pp is set to point to the first byte past the last byte copied +** before this function returns. +*/ +static void fts3PoslistCopy(char **pp, char **ppPoslist){ + char *pEnd = *ppPoslist; + char c = 0; + + /* The end of a position list is marked by a zero encoded as an FTS3 + ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by + ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail + ** of some other, multi-byte, value. + ** + ** The following while-loop moves pEnd to point to the first byte that is not + ** immediately preceded by a byte with the 0x80 bit set. Then increments + ** pEnd once more so that it points to the byte immediately following the + ** last byte in the position-list. + */ + while( *pEnd | c ){ + c = *pEnd++ & 0x80; + testcase( c!=0 && (*pEnd)==0 ); + } + pEnd++; /* Advance past the POS_END terminator byte */ + + if( pp ){ + int n = (int)(pEnd - *ppPoslist); + char *p = *pp; + memcpy(p, *ppPoslist, n); + p += n; + *pp = p; + } + *ppPoslist = pEnd; +} + +/* +** When this function is called, *ppPoslist is assumed to point to the +** start of a column-list. After it returns, *ppPoslist points to the +** to the terminator (POS_COLUMN or POS_END) byte of the column-list. +** +** A column-list is list of delta-encoded positions for a single column +** within a single document within a doclist. +** +** The column-list is terminated either by a POS_COLUMN varint (1) or +** a POS_END varint (0). This routine leaves *ppPoslist pointing to +** the POS_COLUMN or POS_END that terminates the column-list. +** +** If pp is not NULL, then the contents of the column-list are copied +** to *pp. *pp is set to point to the first byte past the last byte copied +** before this function returns. The POS_COLUMN or POS_END terminator +** is not copied into *pp. +*/ +static void fts3ColumnlistCopy(char **pp, char **ppPoslist){ + char *pEnd = *ppPoslist; + char c = 0; + + /* A column-list is terminated by either a 0x01 or 0x00 byte that is + ** not part of a multi-byte varint. + */ + while( 0xFE & (*pEnd | c) ){ + c = *pEnd++ & 0x80; + testcase( c!=0 && ((*pEnd)&0xfe)==0 ); + } + if( pp ){ + int n = (int)(pEnd - *ppPoslist); + char *p = *pp; + memcpy(p, *ppPoslist, n); + p += n; + *pp = p; + } + *ppPoslist = pEnd; +} + +/* +** Value used to signify the end of an position-list. This is safe because +** it is not possible to have a document with 2^31 terms. +*/ +#define POSITION_LIST_END 0x7fffffff + +/* +** This function is used to help parse position-lists. When this function is +** called, *pp may point to the start of the next varint in the position-list +** being parsed, or it may point to 1 byte past the end of the position-list +** (in which case **pp will be a terminator bytes POS_END (0) or +** (1)). +** +** If *pp points past the end of the current position-list, set *pi to +** POSITION_LIST_END and return. Otherwise, read the next varint from *pp, +** increment the current value of *pi by the value read, and set *pp to +** point to the next value before returning. +** +** Before calling this routine *pi must be initialized to the value of +** the previous position, or zero if we are reading the first position +** in the position-list. Because positions are delta-encoded, the value +** of the previous position is needed in order to compute the value of +** the next position. +*/ +static void fts3ReadNextPos( + char **pp, /* IN/OUT: Pointer into position-list buffer */ + sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ +){ + if( (**pp)&0xFE ){ + fts3GetDeltaVarint(pp, pi); + *pi -= 2; + }else{ + *pi = POSITION_LIST_END; + } +} + +/* +** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by +** the value of iCol encoded as a varint to *pp. This will start a new +** column list. +** +** Set *pp to point to the byte just after the last byte written before +** returning (do not modify it if iCol==0). Return the total number of bytes +** written (0 if iCol==0). +*/ +static int fts3PutColNumber(char **pp, int iCol){ + int n = 0; /* Number of bytes written */ + if( iCol ){ + char *p = *pp; /* Output pointer */ + n = 1 + sqlite3Fts3PutVarint(&p[1], iCol); + *p = 0x01; + *pp = &p[n]; + } + return n; +} + +/* +** Compute the union of two position lists. The output written +** into *pp contains all positions of both *pp1 and *pp2 in sorted +** order and with any duplicates removed. All pointers are +** updated appropriately. The caller is responsible for insuring +** that there is enough space in *pp to hold the complete output. +*/ +static void fts3PoslistMerge( + char **pp, /* Output buffer */ + char **pp1, /* Left input list */ + char **pp2 /* Right input list */ +){ + char *p = *pp; + char *p1 = *pp1; + char *p2 = *pp2; + + while( *p1 || *p2 ){ + int iCol1; /* The current column index in pp1 */ + int iCol2; /* The current column index in pp2 */ + + if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1); + else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; + else iCol1 = 0; + + if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2); + else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; + else iCol2 = 0; + + if( iCol1==iCol2 ){ + sqlite3_int64 i1 = 0; /* Last position from pp1 */ + sqlite3_int64 i2 = 0; /* Last position from pp2 */ + sqlite3_int64 iPrev = 0; + int n = fts3PutColNumber(&p, iCol1); + p1 += n; + p2 += n; + + /* At this point, both p1 and p2 point to the start of column-lists + ** for the same column (the column with index iCol1 and iCol2). + ** A column-list is a list of non-negative delta-encoded varints, each + ** incremented by 2 before being stored. Each list is terminated by a + ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists + ** and writes the results to buffer p. p is left pointing to the byte + ** after the list written. No terminator (POS_END or POS_COLUMN) is + ** written to the output. + */ + fts3GetDeltaVarint(&p1, &i1); + fts3GetDeltaVarint(&p2, &i2); + do { + fts3PutDeltaVarint(&p, &iPrev, (i1pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e. +** when the *pp1 token appears before the *pp2 token, but not more than nToken +** slots before it. +** +** e.g. nToken==1 searches for adjacent positions. +*/ +static int fts3PoslistPhraseMerge( + char **pp, /* IN/OUT: Preallocated output buffer */ + int nToken, /* Maximum difference in token positions */ + int isSaveLeft, /* Save the left position */ + int isExact, /* If *pp1 is exactly nTokens before *pp2 */ + char **pp1, /* IN/OUT: Left input list */ + char **pp2 /* IN/OUT: Right input list */ +){ + char *p = *pp; + char *p1 = *pp1; + char *p2 = *pp2; + int iCol1 = 0; + int iCol2 = 0; + + /* Never set both isSaveLeft and isExact for the same invocation. */ + assert( isSaveLeft==0 || isExact==0 ); + + assert( p!=0 && *p1!=0 && *p2!=0 ); + if( *p1==POS_COLUMN ){ + p1++; + p1 += fts3GetVarint32(p1, &iCol1); + } + if( *p2==POS_COLUMN ){ + p2++; + p2 += fts3GetVarint32(p2, &iCol2); + } + + while( 1 ){ + if( iCol1==iCol2 ){ + char *pSave = p; + sqlite3_int64 iPrev = 0; + sqlite3_int64 iPos1 = 0; + sqlite3_int64 iPos2 = 0; + + if( iCol1 ){ + *p++ = POS_COLUMN; + p += sqlite3Fts3PutVarint(p, iCol1); + } + + assert( *p1!=POS_END && *p1!=POS_COLUMN ); + assert( *p2!=POS_END && *p2!=POS_COLUMN ); + fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; + fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; + + while( 1 ){ + if( iPos2==iPos1+nToken + || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) + ){ + sqlite3_int64 iSave; + iSave = isSaveLeft ? iPos1 : iPos2; + fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2; + pSave = 0; + assert( p ); + } + if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){ + if( (*p2&0xFE)==0 ) break; + fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; + }else{ + if( (*p1&0xFE)==0 ) break; + fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; + } + } + + if( pSave ){ + assert( pp && p ); + p = pSave; + } + + fts3ColumnlistCopy(0, &p1); + fts3ColumnlistCopy(0, &p2); + assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 ); + if( 0==*p1 || 0==*p2 ) break; + + p1++; + p1 += fts3GetVarint32(p1, &iCol1); + p2++; + p2 += fts3GetVarint32(p2, &iCol2); + } + + /* Advance pointer p1 or p2 (whichever corresponds to the smaller of + ** iCol1 and iCol2) so that it points to either the 0x00 that marks the + ** end of the position list, or the 0x01 that precedes the next + ** column-number in the position list. + */ + else if( iCol1=pEnd ){ + *pp = 0; + }else{ + sqlite3_int64 iVal; + *pp += sqlite3Fts3GetVarint(*pp, &iVal); + if( bDescIdx ){ + *pVal -= iVal; + }else{ + *pVal += iVal; + } + } +} + +/* +** This function is used to write a single varint to a buffer. The varint +** is written to *pp. Before returning, *pp is set to point 1 byte past the +** end of the value written. +** +** If *pbFirst is zero when this function is called, the value written to +** the buffer is that of parameter iVal. +** +** If *pbFirst is non-zero when this function is called, then the value +** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal) +** (if bDescIdx is non-zero). +** +** Before returning, this function always sets *pbFirst to 1 and *piPrev +** to the value of parameter iVal. +*/ +static void fts3PutDeltaVarint3( + char **pp, /* IN/OUT: Output pointer */ + int bDescIdx, /* True for descending docids */ + sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ + int *pbFirst, /* IN/OUT: True after first int written */ + sqlite3_int64 iVal /* Write this value to the list */ +){ + sqlite3_int64 iWrite; + if( bDescIdx==0 || *pbFirst==0 ){ + iWrite = iVal - *piPrev; + }else{ + iWrite = *piPrev - iVal; + } + assert( *pbFirst || *piPrev==0 ); + assert( *pbFirst==0 || iWrite>0 ); + *pp += sqlite3Fts3PutVarint(*pp, iWrite); + *piPrev = iVal; + *pbFirst = 1; +} + + +/* +** This macro is used by various functions that merge doclists. The two +** arguments are 64-bit docid values. If the value of the stack variable +** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). +** Otherwise, (i2-i1). +** +** Using this makes it easier to write code that can merge doclists that are +** sorted in either ascending or descending order. +*/ +#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2)) + +/* +** This function does an "OR" merge of two doclists (output contains all +** positions contained in either argument doclist). If the docids in the +** input doclists are sorted in ascending order, parameter bDescDoclist +** should be false. If they are sorted in ascending order, it should be +** passed a non-zero value. +** +** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer +** containing the output doclist and SQLITE_OK is returned. In this case +** *pnOut is set to the number of bytes in the output doclist. +** +** If an error occurs, an SQLite error code is returned. The output values +** are undefined in this case. +*/ +static int fts3DoclistOrMerge( + int bDescDoclist, /* True if arguments are desc */ + char *a1, int n1, /* First doclist */ + char *a2, int n2, /* Second doclist */ + char **paOut, int *pnOut /* OUT: Malloc'd doclist */ +){ + sqlite3_int64 i1 = 0; + sqlite3_int64 i2 = 0; + sqlite3_int64 iPrev = 0; + char *pEnd1 = &a1[n1]; + char *pEnd2 = &a2[n2]; + char *p1 = a1; + char *p2 = a2; + char *p; + char *aOut; + int bFirstOut = 0; + + *paOut = 0; + *pnOut = 0; + + /* Allocate space for the output. Both the input and output doclists + ** are delta encoded. If they are in ascending order (bDescDoclist==0), + ** then the first docid in each list is simply encoded as a varint. For + ** each subsequent docid, the varint stored is the difference between the + ** current and previous docid (a positive number - since the list is in + ** ascending order). + ** + ** The first docid written to the output is therefore encoded using the + ** same number of bytes as it is in whichever of the input lists it is + ** read from. And each subsequent docid read from the same input list + ** consumes either the same or less bytes as it did in the input (since + ** the difference between it and the previous value in the output must + ** be a positive value less than or equal to the delta value read from + ** the input list). The same argument applies to all but the first docid + ** read from the 'other' list. And to the contents of all position lists + ** that will be copied and merged from the input to the output. + ** + ** However, if the first docid copied to the output is a negative number, + ** then the encoding of the first docid from the 'other' input list may + ** be larger in the output than it was in the input (since the delta value + ** may be a larger positive integer than the actual docid). + ** + ** The space required to store the output is therefore the sum of the + ** sizes of the two inputs, plus enough space for exactly one of the input + ** docids to grow. + ** + ** A symetric argument may be made if the doclists are in descending + ** order. + */ + aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1); + if( !aOut ) return SQLITE_NOMEM; + + p = aOut; + fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); + fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); + while( p1 || p2 ){ + sqlite3_int64 iDiff = DOCID_CMP(i1, i2); + + if( p2 && p1 && iDiff==0 ){ + fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); + fts3PoslistMerge(&p, &p1, &p2); + fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); + fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); + }else if( !p2 || (p1 && iDiff<0) ){ + fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); + fts3PoslistCopy(&p, &p1); + fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); + }else{ + fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2); + fts3PoslistCopy(&p, &p2); + fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); + } + } + + *paOut = aOut; + *pnOut = (int)(p-aOut); + assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 ); + return SQLITE_OK; +} + +/* +** This function does a "phrase" merge of two doclists. In a phrase merge, +** the output contains a copy of each position from the right-hand input +** doclist for which there is a position in the left-hand input doclist +** exactly nDist tokens before it. +** +** If the docids in the input doclists are sorted in ascending order, +** parameter bDescDoclist should be false. If they are sorted in ascending +** order, it should be passed a non-zero value. +** +** The right-hand input doclist is overwritten by this function. +*/ +static int fts3DoclistPhraseMerge( + int bDescDoclist, /* True if arguments are desc */ + int nDist, /* Distance from left to right (1=adjacent) */ + char *aLeft, int nLeft, /* Left doclist */ + char **paRight, int *pnRight /* IN/OUT: Right/output doclist */ +){ + sqlite3_int64 i1 = 0; + sqlite3_int64 i2 = 0; + sqlite3_int64 iPrev = 0; + char *aRight = *paRight; + char *pEnd1 = &aLeft[nLeft]; + char *pEnd2 = &aRight[*pnRight]; + char *p1 = aLeft; + char *p2 = aRight; + char *p; + int bFirstOut = 0; + char *aOut; + + assert( nDist>0 ); + if( bDescDoclist ){ + aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX); + if( aOut==0 ) return SQLITE_NOMEM; + }else{ + aOut = aRight; + } + p = aOut; + + fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); + fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); + + while( p1 && p2 ){ + sqlite3_int64 iDiff = DOCID_CMP(i1, i2); + if( iDiff==0 ){ + char *pSave = p; + sqlite3_int64 iPrevSave = iPrev; + int bFirstOutSave = bFirstOut; + + fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); + if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){ + p = pSave; + iPrev = iPrevSave; + bFirstOut = bFirstOutSave; + } + fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); + fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); + }else if( iDiff<0 ){ + fts3PoslistCopy(0, &p1); + fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); + }else{ + fts3PoslistCopy(0, &p2); + fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); + } + } + + *pnRight = (int)(p - aOut); + if( bDescDoclist ){ + sqlite3_free(aRight); + *paRight = aOut; + } + + return SQLITE_OK; +} + +/* +** Argument pList points to a position list nList bytes in size. This +** function checks to see if the position list contains any entries for +** a token in position 0 (of any column). If so, it writes argument iDelta +** to the output buffer pOut, followed by a position list consisting only +** of the entries from pList at position 0, and terminated by an 0x00 byte. +** The value returned is the number of bytes written to pOut (if any). +*/ +SQLITE_PRIVATE int sqlite3Fts3FirstFilter( + sqlite3_int64 iDelta, /* Varint that may be written to pOut */ + char *pList, /* Position list (no 0x00 term) */ + int nList, /* Size of pList in bytes */ + char *pOut /* Write output here */ +){ + int nOut = 0; + int bWritten = 0; /* True once iDelta has been written */ + char *p = pList; + char *pEnd = &pList[nList]; + + if( *p!=0x01 ){ + if( *p==0x02 ){ + nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta); + pOut[nOut++] = 0x02; + bWritten = 1; + } + fts3ColumnlistCopy(0, &p); + } + + while( paaOutput); i++){ + if( pTS->aaOutput[i] ){ + if( !aOut ){ + aOut = pTS->aaOutput[i]; + nOut = pTS->anOutput[i]; + pTS->aaOutput[i] = 0; + }else{ + int nNew; + char *aNew; + + int rc = fts3DoclistOrMerge(p->bDescIdx, + pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew + ); + if( rc!=SQLITE_OK ){ + sqlite3_free(aOut); + return rc; + } + + sqlite3_free(pTS->aaOutput[i]); + sqlite3_free(aOut); + pTS->aaOutput[i] = 0; + aOut = aNew; + nOut = nNew; + } + } + } + + pTS->aaOutput[0] = aOut; + pTS->anOutput[0] = nOut; + return SQLITE_OK; +} + +/* +** Merge the doclist aDoclist/nDoclist into the TermSelect object passed +** as the first argument. The merge is an "OR" merge (see function +** fts3DoclistOrMerge() for details). +** +** This function is called with the doclist for each term that matches +** a queried prefix. It merges all these doclists into one, the doclist +** for the specified prefix. Since there can be a very large number of +** doclists to merge, the merging is done pair-wise using the TermSelect +** object. +** +** This function returns SQLITE_OK if the merge is successful, or an +** SQLite error code (SQLITE_NOMEM) if an error occurs. +*/ +static int fts3TermSelectMerge( + Fts3Table *p, /* FTS table handle */ + TermSelect *pTS, /* TermSelect object to merge into */ + char *aDoclist, /* Pointer to doclist */ + int nDoclist /* Size of aDoclist in bytes */ +){ + if( pTS->aaOutput[0]==0 ){ + /* If this is the first term selected, copy the doclist to the output + ** buffer using memcpy(). + ** + ** Add FTS3_VARINT_MAX bytes of unused space to the end of the + ** allocation. This is so as to ensure that the buffer is big enough + ** to hold the current doclist AND'd with any other doclist. If the + ** doclists are stored in order=ASC order, this padding would not be + ** required (since the size of [doclistA AND doclistB] is always less + ** than or equal to the size of [doclistA] in that case). But this is + ** not true for order=DESC. For example, a doclist containing (1, -1) + ** may be smaller than (-1), as in the first example the -1 may be stored + ** as a single-byte delta, whereas in the second it must be stored as a + ** FTS3_VARINT_MAX byte varint. + ** + ** Similar padding is added in the fts3DoclistOrMerge() function. + */ + pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1); + pTS->anOutput[0] = nDoclist; + if( pTS->aaOutput[0] ){ + memcpy(pTS->aaOutput[0], aDoclist, nDoclist); + }else{ + return SQLITE_NOMEM; + } + }else{ + char *aMerge = aDoclist; + int nMerge = nDoclist; + int iOut; + + for(iOut=0; iOutaaOutput); iOut++){ + if( pTS->aaOutput[iOut]==0 ){ + assert( iOut>0 ); + pTS->aaOutput[iOut] = aMerge; + pTS->anOutput[iOut] = nMerge; + break; + }else{ + char *aNew; + int nNew; + + int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, + pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew + ); + if( rc!=SQLITE_OK ){ + if( aMerge!=aDoclist ) sqlite3_free(aMerge); + return rc; + } + + if( aMerge!=aDoclist ) sqlite3_free(aMerge); + sqlite3_free(pTS->aaOutput[iOut]); + pTS->aaOutput[iOut] = 0; + + aMerge = aNew; + nMerge = nNew; + if( (iOut+1)==SizeofArray(pTS->aaOutput) ){ + pTS->aaOutput[iOut] = aMerge; + pTS->anOutput[iOut] = nMerge; + } + } + } + } + return SQLITE_OK; +} + +/* +** Append SegReader object pNew to the end of the pCsr->apSegment[] array. +*/ +static int fts3SegReaderCursorAppend( + Fts3MultiSegReader *pCsr, + Fts3SegReader *pNew +){ + if( (pCsr->nSegment%16)==0 ){ + Fts3SegReader **apNew; + int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); + apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte); + if( !apNew ){ + sqlite3Fts3SegReaderFree(pNew); + return SQLITE_NOMEM; + } + pCsr->apSegment = apNew; + } + pCsr->apSegment[pCsr->nSegment++] = pNew; + return SQLITE_OK; +} + +/* +** Add seg-reader objects to the Fts3MultiSegReader object passed as the +** 8th argument. +** +** This function returns SQLITE_OK if successful, or an SQLite error code +** otherwise. +*/ +static int fts3SegReaderCursor( + Fts3Table *p, /* FTS3 table handle */ + int iLangid, /* Language id */ + int iIndex, /* Index to search (from 0 to p->nIndex-1) */ + int iLevel, /* Level of segments to scan */ + const char *zTerm, /* Term to query for */ + int nTerm, /* Size of zTerm in bytes */ + int isPrefix, /* True for a prefix search */ + int isScan, /* True to scan from zTerm to EOF */ + Fts3MultiSegReader *pCsr /* Cursor object to populate */ +){ + int rc = SQLITE_OK; /* Error code */ + sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */ + int rc2; /* Result of sqlite3_reset() */ + + /* If iLevel is less than 0 and this is not a scan, include a seg-reader + ** for the pending-terms. If this is a scan, then this call must be being + ** made by an fts4aux module, not an FTS table. In this case calling + ** Fts3SegReaderPending might segfault, as the data structures used by + ** fts4aux are not completely populated. So it's easiest to filter these + ** calls out here. */ + if( iLevel<0 && p->aIndex ){ + Fts3SegReader *pSeg = 0; + rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg); + if( rc==SQLITE_OK && pSeg ){ + rc = fts3SegReaderCursorAppend(pCsr, pSeg); + } + } + + if( iLevel!=FTS3_SEGCURSOR_PENDING ){ + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt); + } + + while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ + Fts3SegReader *pSeg = 0; + + /* Read the values returned by the SELECT into local variables. */ + sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1); + sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2); + sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3); + int nRoot = sqlite3_column_bytes(pStmt, 4); + char const *zRoot = sqlite3_column_blob(pStmt, 4); + + /* If zTerm is not NULL, and this segment is not stored entirely on its + ** root node, the range of leaves scanned can be reduced. Do this. */ + if( iStartBlock && zTerm ){ + sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0); + rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi); + if( rc!=SQLITE_OK ) goto finished; + if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock; + } + + rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, + (isPrefix==0 && isScan==0), + iStartBlock, iLeavesEndBlock, + iEndBlock, zRoot, nRoot, &pSeg + ); + if( rc!=SQLITE_OK ) goto finished; + rc = fts3SegReaderCursorAppend(pCsr, pSeg); + } + } + + finished: + rc2 = sqlite3_reset(pStmt); + if( rc==SQLITE_DONE ) rc = rc2; + + return rc; +} + +/* +** Set up a cursor object for iterating through a full-text index or a +** single level therein. +*/ +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor( + Fts3Table *p, /* FTS3 table handle */ + int iLangid, /* Language-id to search */ + int iIndex, /* Index to search (from 0 to p->nIndex-1) */ + int iLevel, /* Level of segments to scan */ + const char *zTerm, /* Term to query for */ + int nTerm, /* Size of zTerm in bytes */ + int isPrefix, /* True for a prefix search */ + int isScan, /* True to scan from zTerm to EOF */ + Fts3MultiSegReader *pCsr /* Cursor object to populate */ +){ + assert( iIndex>=0 && iIndexnIndex ); + assert( iLevel==FTS3_SEGCURSOR_ALL + || iLevel==FTS3_SEGCURSOR_PENDING + || iLevel>=0 + ); + assert( iLevelbase.pVtab; + + if( isPrefix ){ + for(i=1; bFound==0 && inIndex; i++){ + if( p->aIndex[i].nPrefix==nTerm ){ + bFound = 1; + rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, + i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr + ); + pSegcsr->bLookup = 1; + } + } + + for(i=1; bFound==0 && inIndex; i++){ + if( p->aIndex[i].nPrefix==nTerm+1 ){ + bFound = 1; + rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, + i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr + ); + if( rc==SQLITE_OK ){ + rc = fts3SegReaderCursorAddZero( + p, pCsr->iLangid, zTerm, nTerm, pSegcsr + ); + } + } + } + } + + if( bFound==0 ){ + rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, + 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr + ); + pSegcsr->bLookup = !isPrefix; + } + } + + *ppSegcsr = pSegcsr; + return rc; +} + +/* +** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor(). +*/ +static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){ + sqlite3Fts3SegReaderFinish(pSegcsr); + sqlite3_free(pSegcsr); +} + +/* +** This function retrieves the doclist for the specified term (or term +** prefix) from the database. +*/ +static int fts3TermSelect( + Fts3Table *p, /* Virtual table handle */ + Fts3PhraseToken *pTok, /* Token to query for */ + int iColumn, /* Column to query (or -ve for all columns) */ + int *pnOut, /* OUT: Size of buffer at *ppOut */ + char **ppOut /* OUT: Malloced result buffer */ +){ + int rc; /* Return code */ + Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */ + TermSelect tsc; /* Object for pair-wise doclist merging */ + Fts3SegFilter filter; /* Segment term filter configuration */ + + pSegcsr = pTok->pSegcsr; + memset(&tsc, 0, sizeof(TermSelect)); + + filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS + | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0) + | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0) + | (iColumnnColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0); + filter.iCol = iColumn; + filter.zTerm = pTok->z; + filter.nTerm = pTok->n; + + rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter); + while( SQLITE_OK==rc + && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) + ){ + rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist); + } + + if( rc==SQLITE_OK ){ + rc = fts3TermSelectFinishMerge(p, &tsc); + } + if( rc==SQLITE_OK ){ + *ppOut = tsc.aaOutput[0]; + *pnOut = tsc.anOutput[0]; + }else{ + int i; + for(i=0; ipSegcsr = 0; + return rc; +} + +/* +** This function counts the total number of docids in the doclist stored +** in buffer aList[], size nList bytes. +** +** If the isPoslist argument is true, then it is assumed that the doclist +** contains a position-list following each docid. Otherwise, it is assumed +** that the doclist is simply a list of docids stored as delta encoded +** varints. +*/ +static int fts3DoclistCountDocids(char *aList, int nList){ + int nDoc = 0; /* Return value */ + if( aList ){ + char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */ + char *p = aList; /* Cursor */ + while( peSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){ + if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){ + pCsr->isEof = 1; + rc = sqlite3_reset(pCsr->pStmt); + }else{ + pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0); + rc = SQLITE_OK; + } + }else{ + rc = fts3EvalNext((Fts3Cursor *)pCursor); + } + assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); + return rc; +} + +/* +** The following are copied from sqliteInt.h. +** +** Constants for the largest and smallest possible 64-bit signed integers. +** These macros are designed to work correctly on both 32-bit and 64-bit +** compilers. +*/ +#ifndef SQLITE_AMALGAMATION +# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) +# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) +#endif + +/* +** If the numeric type of argument pVal is "integer", then return it +** converted to a 64-bit signed integer. Otherwise, return a copy of +** the second parameter, iDefault. +*/ +static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){ + if( pVal ){ + int eType = sqlite3_value_numeric_type(pVal); + if( eType==SQLITE_INTEGER ){ + return sqlite3_value_int64(pVal); + } + } + return iDefault; +} + +/* +** This is the xFilter interface for the virtual table. See +** the virtual table xFilter method documentation for additional +** information. +** +** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against +** the %_content table. +** +** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry +** in the %_content table. +** +** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The +** column on the left-hand side of the MATCH operator is column +** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand +** side of the MATCH operator. +*/ +static int fts3FilterMethod( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, /* Strategy index */ + const char *idxStr, /* Unused */ + int nVal, /* Number of elements in apVal */ + sqlite3_value **apVal /* Arguments for the indexing scheme */ +){ + int rc = SQLITE_OK; + char *zSql; /* SQL statement used to access %_content */ + int eSearch; + Fts3Table *p = (Fts3Table *)pCursor->pVtab; + Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; + + sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */ + sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */ + sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */ + sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */ + int iIdx; + + UNUSED_PARAMETER(idxStr); + UNUSED_PARAMETER(nVal); + + eSearch = (idxNum & 0x0000FFFF); + assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) ); + assert( p->pSegments==0 ); + + /* Collect arguments into local variables */ + iIdx = 0; + if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++]; + if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++]; + if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++]; + if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++]; + assert( iIdx==nVal ); + + /* In case the cursor has been used before, clear it now. */ + sqlite3_finalize(pCsr->pStmt); + sqlite3_free(pCsr->aDoclist); + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); + sqlite3Fts3ExprFree(pCsr->pExpr); + memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); + + /* Set the lower and upper bounds on docids to return */ + pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); + pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64); + + if( idxStr ){ + pCsr->bDesc = (idxStr[0]=='D'); + }else{ + pCsr->bDesc = p->bDescIdx; + } + pCsr->eSearch = (i16)eSearch; + + if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){ + int iCol = eSearch-FTS3_FULLTEXT_SEARCH; + const char *zQuery = (const char *)sqlite3_value_text(pCons); + + if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){ + return SQLITE_NOMEM; + } + + pCsr->iLangid = 0; + if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid); + + assert( p->base.zErrMsg==0 ); + rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid, + p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr, + &p->base.zErrMsg + ); + if( rc!=SQLITE_OK ){ + return rc; + } + + rc = fts3EvalStart(pCsr); + sqlite3Fts3SegmentsClose(p); + if( rc!=SQLITE_OK ) return rc; + pCsr->pNextId = pCsr->aDoclist; + pCsr->iPrevId = 0; + } + + /* Compile a SELECT statement for this cursor. For a full-table-scan, the + ** statement loops through all rows of the %_content table. For a + ** full-text query or docid lookup, the statement retrieves a single + ** row by docid. + */ + if( eSearch==FTS3_FULLSCAN_SEARCH ){ + if( pDocidGe || pDocidLe ){ + zSql = sqlite3_mprintf( + "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s", + p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid, + (pCsr->bDesc ? "DESC" : "ASC") + ); + }else{ + zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s", + p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC") + ); + } + if( zSql ){ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); + sqlite3_free(zSql); + }else{ + rc = SQLITE_NOMEM; + } + }else if( eSearch==FTS3_DOCID_SEARCH ){ + rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt); + if( rc==SQLITE_OK ){ + rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons); + } + } + if( rc!=SQLITE_OK ) return rc; + + return fts3NextMethod(pCursor); +} + +/* +** This is the xEof method of the virtual table. SQLite calls this +** routine to find out if it has reached the end of a result set. +*/ +static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){ + return ((Fts3Cursor *)pCursor)->isEof; +} + +/* +** This is the xRowid method. The SQLite core calls this routine to +** retrieve the rowid for the current row of the result set. fts3 +** exposes %_content.docid as the rowid for the virtual table. The +** rowid should be written to *pRowid. +*/ +static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ + Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; + *pRowid = pCsr->iPrevId; + return SQLITE_OK; +} + +/* +** This is the xColumn method, called by SQLite to request a value from +** the row that the supplied cursor currently points to. +** +** If: +** +** (iCol < p->nColumn) -> The value of the iCol'th user column. +** (iCol == p->nColumn) -> Magic column with the same name as the table. +** (iCol == p->nColumn+1) -> Docid column +** (iCol == p->nColumn+2) -> Langid column +*/ +static int fts3ColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + int rc = SQLITE_OK; /* Return Code */ + Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; + Fts3Table *p = (Fts3Table *)pCursor->pVtab; + + /* The column value supplied by SQLite must be in range. */ + assert( iCol>=0 && iCol<=p->nColumn+2 ); + + if( iCol==p->nColumn+1 ){ + /* This call is a request for the "docid" column. Since "docid" is an + ** alias for "rowid", use the xRowid() method to obtain the value. + */ + sqlite3_result_int64(pCtx, pCsr->iPrevId); + }else if( iCol==p->nColumn ){ + /* The extra column whose name is the same as the table. + ** Return a blob which is a pointer to the cursor. */ + sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); + }else if( iCol==p->nColumn+2 && pCsr->pExpr ){ + sqlite3_result_int64(pCtx, pCsr->iLangid); + }else{ + /* The requested column is either a user column (one that contains + ** indexed data), or the language-id column. */ + rc = fts3CursorSeek(0, pCsr); + + if( rc==SQLITE_OK ){ + if( iCol==p->nColumn+2 ){ + int iLangid = 0; + if( p->zLanguageid ){ + iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1); + } + sqlite3_result_int(pCtx, iLangid); + }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){ + sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); + } + } + } + + assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); + return rc; +} + +/* +** This function is the implementation of the xUpdate callback used by +** FTS3 virtual tables. It is invoked by SQLite each time a row is to be +** inserted, updated or deleted. +*/ +static int fts3UpdateMethod( + sqlite3_vtab *pVtab, /* Virtual table handle */ + int nArg, /* Size of argument array */ + sqlite3_value **apVal, /* Array of arguments */ + sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ +){ + return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid); +} + +/* +** Implementation of xSync() method. Flush the contents of the pending-terms +** hash-table to the database. +*/ +static int fts3SyncMethod(sqlite3_vtab *pVtab){ + + /* Following an incremental-merge operation, assuming that the input + ** segments are not completely consumed (the usual case), they are updated + ** in place to remove the entries that have already been merged. This + ** involves updating the leaf block that contains the smallest unmerged + ** entry and each block (if any) between the leaf and the root node. So + ** if the height of the input segment b-trees is N, and input segments + ** are merged eight at a time, updating the input segments at the end + ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually + ** small - often between 0 and 2. So the overhead of the incremental + ** merge is somewhere between 8 and 24 blocks. To avoid this overhead + ** dwarfing the actual productive work accomplished, the incremental merge + ** is only attempted if it will write at least 64 leaf blocks. Hence + ** nMinMerge. + ** + ** Of course, updating the input segments also involves deleting a bunch + ** of blocks from the segments table. But this is not considered overhead + ** as it would also be required by a crisis-merge that used the same input + ** segments. + */ + const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */ + + Fts3Table *p = (Fts3Table*)pVtab; + int rc = sqlite3Fts3PendingTermsFlush(p); + + if( rc==SQLITE_OK + && p->nLeafAdd>(nMinMerge/16) + && p->nAutoincrmerge && p->nAutoincrmerge!=0xff + ){ + int mxLevel = 0; /* Maximum relative level value in db */ + int A; /* Incr-merge parameter A */ + + rc = sqlite3Fts3MaxLevel(p, &mxLevel); + assert( rc==SQLITE_OK || mxLevel==0 ); + A = p->nLeafAdd * mxLevel; + A += (A/2); + if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge); + } + sqlite3Fts3SegmentsClose(p); + return rc; +} + +/* +** If it is currently unknown whether or not the FTS table has an %_stat +** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat +** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code +** if an error occurs. +*/ +static int fts3SetHasStat(Fts3Table *p){ + int rc = SQLITE_OK; + if( p->bHasStat==2 ){ + const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'"; + char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName); + if( zSql ){ + sqlite3_stmt *pStmt = 0; + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW); + rc = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ) p->bHasStat = bHasStat; + } + sqlite3_free(zSql); + }else{ + rc = SQLITE_NOMEM; + } + } + return rc; +} + +/* +** Implementation of xBegin() method. +*/ +static int fts3BeginMethod(sqlite3_vtab *pVtab){ + Fts3Table *p = (Fts3Table*)pVtab; + UNUSED_PARAMETER(pVtab); + assert( p->pSegments==0 ); + assert( p->nPendingData==0 ); + assert( p->inTransaction!=1 ); + TESTONLY( p->inTransaction = 1 ); + TESTONLY( p->mxSavepoint = -1; ); + p->nLeafAdd = 0; + return fts3SetHasStat(p); +} + +/* +** Implementation of xCommit() method. This is a no-op. The contents of +** the pending-terms hash-table have already been flushed into the database +** by fts3SyncMethod(). +*/ +static int fts3CommitMethod(sqlite3_vtab *pVtab){ + TESTONLY( Fts3Table *p = (Fts3Table*)pVtab ); + UNUSED_PARAMETER(pVtab); + assert( p->nPendingData==0 ); + assert( p->inTransaction!=0 ); + assert( p->pSegments==0 ); + TESTONLY( p->inTransaction = 0 ); + TESTONLY( p->mxSavepoint = -1; ); + return SQLITE_OK; +} + +/* +** Implementation of xRollback(). Discard the contents of the pending-terms +** hash-table. Any changes made to the database are reverted by SQLite. +*/ +static int fts3RollbackMethod(sqlite3_vtab *pVtab){ + Fts3Table *p = (Fts3Table*)pVtab; + sqlite3Fts3PendingTermsClear(p); + assert( p->inTransaction!=0 ); + TESTONLY( p->inTransaction = 0 ); + TESTONLY( p->mxSavepoint = -1; ); + return SQLITE_OK; +} + +/* +** When called, *ppPoslist must point to the byte immediately following the +** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function +** moves *ppPoslist so that it instead points to the first byte of the +** same position list. +*/ +static void fts3ReversePoslist(char *pStart, char **ppPoslist){ + char *p = &(*ppPoslist)[-2]; + char c = 0; + + /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */ + while( p>pStart && (c=*p--)==0 ); + + /* Search backwards for a varint with value zero (the end of the previous + ** poslist). This is an 0x00 byte preceded by some byte that does not + ** have the 0x80 bit set. */ + while( p>pStart && (*p & 0x80) | c ){ + c = *p--; + } + assert( p==pStart || c==0 ); + + /* At this point p points to that preceding byte without the 0x80 bit + ** set. So to find the start of the poslist, skip forward 2 bytes then + ** over a varint. + ** + ** Normally. The other case is that p==pStart and the poslist to return + ** is the first in the doclist. In this case do not skip forward 2 bytes. + ** The second part of the if condition (c==0 && *ppPoslist>&p[2]) + ** is required for cases where the first byte of a doclist and the + ** doclist is empty. For example, if the first docid is 10, a doclist + ** that begins with: + ** + ** 0x0A 0x00 + */ + if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; } + while( *p++&0x80 ); + *ppPoslist = p; +} + +/* +** Helper function used by the implementation of the overloaded snippet(), +** offsets() and optimize() SQL functions. +** +** If the value passed as the third argument is a blob of size +** sizeof(Fts3Cursor*), then the blob contents are copied to the +** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error +** message is written to context pContext and SQLITE_ERROR returned. The +** string passed via zFunc is used as part of the error message. +*/ +static int fts3FunctionArg( + sqlite3_context *pContext, /* SQL function call context */ + const char *zFunc, /* Function name */ + sqlite3_value *pVal, /* argv[0] passed to function */ + Fts3Cursor **ppCsr /* OUT: Store cursor handle here */ +){ + Fts3Cursor *pRet; + if( sqlite3_value_type(pVal)!=SQLITE_BLOB + || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *) + ){ + char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc); + sqlite3_result_error(pContext, zErr, -1); + sqlite3_free(zErr); + return SQLITE_ERROR; + } + memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *)); + *ppCsr = pRet; + return SQLITE_OK; +} + +/* +** Implementation of the snippet() function for FTS3 +*/ +static void fts3SnippetFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of apVal[] array */ + sqlite3_value **apVal /* Array of arguments */ +){ + Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ + const char *zStart = ""; + const char *zEnd = ""; + const char *zEllipsis = "..."; + int iCol = -1; + int nToken = 15; /* Default number of tokens in snippet */ + + /* There must be at least one argument passed to this function (otherwise + ** the non-overloaded version would have been called instead of this one). + */ + assert( nVal>=1 ); + + if( nVal>6 ){ + sqlite3_result_error(pContext, + "wrong number of arguments to function snippet()", -1); + return; + } + if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return; + + switch( nVal ){ + case 6: nToken = sqlite3_value_int(apVal[5]); + case 5: iCol = sqlite3_value_int(apVal[4]); + case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]); + case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]); + case 2: zStart = (const char*)sqlite3_value_text(apVal[1]); + } + if( !zEllipsis || !zEnd || !zStart ){ + sqlite3_result_error_nomem(pContext); + }else if( nToken==0 ){ + sqlite3_result_text(pContext, "", -1, SQLITE_STATIC); + }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){ + sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken); + } +} + +/* +** Implementation of the offsets() function for FTS3 +*/ +static void fts3OffsetsFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ + + UNUSED_PARAMETER(nVal); + + assert( nVal==1 ); + if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return; + assert( pCsr ); + if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){ + sqlite3Fts3Offsets(pContext, pCsr); + } +} + +/* +** Implementation of the special optimize() function for FTS3. This +** function merges all segments in the database to a single segment. +** Example usage is: +** +** SELECT optimize(t) FROM t LIMIT 1; +** +** where 't' is the name of an FTS3 table. +*/ +static void fts3OptimizeFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + int rc; /* Return code */ + Fts3Table *p; /* Virtual table handle */ + Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */ + + UNUSED_PARAMETER(nVal); + + assert( nVal==1 ); + if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return; + p = (Fts3Table *)pCursor->base.pVtab; + assert( p ); + + rc = sqlite3Fts3Optimize(p); + + switch( rc ){ + case SQLITE_OK: + sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC); + break; + case SQLITE_DONE: + sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC); + break; + default: + sqlite3_result_error_code(pContext, rc); + break; + } +} + +/* +** Implementation of the matchinfo() function for FTS3 +*/ +static void fts3MatchinfoFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ + assert( nVal==1 || nVal==2 ); + if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){ + const char *zArg = 0; + if( nVal>1 ){ + zArg = (const char *)sqlite3_value_text(apVal[1]); + } + sqlite3Fts3Matchinfo(pContext, pCsr, zArg); + } +} + +/* +** This routine implements the xFindFunction method for the FTS3 +** virtual table. +*/ +static int fts3FindFunctionMethod( + sqlite3_vtab *pVtab, /* Virtual table handle */ + int nArg, /* Number of SQL function arguments */ + const char *zName, /* Name of SQL function */ + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */ + void **ppArg /* Unused */ +){ + struct Overloaded { + const char *zName; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } aOverload[] = { + { "snippet", fts3SnippetFunc }, + { "offsets", fts3OffsetsFunc }, + { "optimize", fts3OptimizeFunc }, + { "matchinfo", fts3MatchinfoFunc }, + }; + int i; /* Iterator variable */ + + UNUSED_PARAMETER(pVtab); + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(ppArg); + + for(i=0; idb; /* Database connection */ + int rc; /* Return Code */ + + /* At this point it must be known if the %_stat table exists or not. + ** So bHasStat may not be 2. */ + rc = fts3SetHasStat(p); + + /* As it happens, the pending terms table is always empty here. This is + ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction + ** always opens a savepoint transaction. And the xSavepoint() method + ** flushes the pending terms table. But leave the (no-op) call to + ** PendingTermsFlush() in in case that changes. + */ + assert( p->nPendingData==0 ); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3PendingTermsFlush(p); + } + + if( p->zContentTbl==0 ){ + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", + p->zDb, p->zName, zName + ); + } + + if( p->bHasDocsize ){ + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';", + p->zDb, p->zName, zName + ); + } + if( p->bHasStat ){ + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';", + p->zDb, p->zName, zName + ); + } + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';", + p->zDb, p->zName, zName + ); + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';", + p->zDb, p->zName, zName + ); + return rc; +} + +/* +** The xSavepoint() method. +** +** Flush the contents of the pending-terms table to disk. +*/ +static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ + int rc = SQLITE_OK; + UNUSED_PARAMETER(iSavepoint); + assert( ((Fts3Table *)pVtab)->inTransaction ); + assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint ); + TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint ); + if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){ + rc = fts3SyncMethod(pVtab); + } + return rc; +} + +/* +** The xRelease() method. +** +** This is a no-op. +*/ +static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ + TESTONLY( Fts3Table *p = (Fts3Table*)pVtab ); + UNUSED_PARAMETER(iSavepoint); + UNUSED_PARAMETER(pVtab); + assert( p->inTransaction ); + assert( p->mxSavepoint >= iSavepoint ); + TESTONLY( p->mxSavepoint = iSavepoint-1 ); + return SQLITE_OK; +} + +/* +** The xRollbackTo() method. +** +** Discard the contents of the pending terms table. +*/ +static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ + Fts3Table *p = (Fts3Table*)pVtab; + UNUSED_PARAMETER(iSavepoint); + assert( p->inTransaction ); + assert( p->mxSavepoint >= iSavepoint ); + TESTONLY( p->mxSavepoint = iSavepoint ); + sqlite3Fts3PendingTermsClear(p); + return SQLITE_OK; +} + +static const sqlite3_module fts3Module = { + /* iVersion */ 2, + /* xCreate */ fts3CreateMethod, + /* xConnect */ fts3ConnectMethod, + /* xBestIndex */ fts3BestIndexMethod, + /* xDisconnect */ fts3DisconnectMethod, + /* xDestroy */ fts3DestroyMethod, + /* xOpen */ fts3OpenMethod, + /* xClose */ fts3CloseMethod, + /* xFilter */ fts3FilterMethod, + /* xNext */ fts3NextMethod, + /* xEof */ fts3EofMethod, + /* xColumn */ fts3ColumnMethod, + /* xRowid */ fts3RowidMethod, + /* xUpdate */ fts3UpdateMethod, + /* xBegin */ fts3BeginMethod, + /* xSync */ fts3SyncMethod, + /* xCommit */ fts3CommitMethod, + /* xRollback */ fts3RollbackMethod, + /* xFindFunction */ fts3FindFunctionMethod, + /* xRename */ fts3RenameMethod, + /* xSavepoint */ fts3SavepointMethod, + /* xRelease */ fts3ReleaseMethod, + /* xRollbackTo */ fts3RollbackToMethod, +}; + +/* +** This function is registered as the module destructor (called when an +** FTS3 enabled database connection is closed). It frees the memory +** allocated for the tokenizer hash table. +*/ +static void hashDestroy(void *p){ + Fts3Hash *pHash = (Fts3Hash *)p; + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); +} + +/* +** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are +** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c +** respectively. The following three forward declarations are for functions +** declared in these files used to retrieve the respective implementations. +** +** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed +** to by the argument to point to the "simple" tokenizer implementation. +** And so on. +*/ +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); +#ifndef SQLITE_DISABLE_FTS3_UNICODE +SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule); +#endif +#ifdef SQLITE_ENABLE_ICU +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); +#endif + +/* +** Initialize the fts3 extension. If this extension is built as part +** of the sqlite library, then this function is called directly by +** SQLite. If fts3 is built as a dynamically loadable extension, this +** function is called by the sqlite3_extension_init() entry point. +*/ +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){ + int rc = SQLITE_OK; + Fts3Hash *pHash = 0; + const sqlite3_tokenizer_module *pSimple = 0; + const sqlite3_tokenizer_module *pPorter = 0; +#ifndef SQLITE_DISABLE_FTS3_UNICODE + const sqlite3_tokenizer_module *pUnicode = 0; +#endif + +#ifdef SQLITE_ENABLE_ICU + const sqlite3_tokenizer_module *pIcu = 0; + sqlite3Fts3IcuTokenizerModule(&pIcu); +#endif + +#ifndef SQLITE_DISABLE_FTS3_UNICODE + sqlite3Fts3UnicodeTokenizer(&pUnicode); +#endif + +#ifdef SQLITE_TEST + rc = sqlite3Fts3InitTerm(db); + if( rc!=SQLITE_OK ) return rc; +#endif + + rc = sqlite3Fts3InitAux(db); + if( rc!=SQLITE_OK ) return rc; + + sqlite3Fts3SimpleTokenizerModule(&pSimple); + sqlite3Fts3PorterTokenizerModule(&pPorter); + + /* Allocate and initialize the hash-table used to store tokenizers. */ + pHash = sqlite3_malloc(sizeof(Fts3Hash)); + if( !pHash ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); + } + + /* Load the built-in tokenizers into the hash table */ + if( rc==SQLITE_OK ){ + if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) + || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) + +#ifndef SQLITE_DISABLE_FTS3_UNICODE + || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) +#endif +#ifdef SQLITE_ENABLE_ICU + || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) +#endif + ){ + rc = SQLITE_NOMEM; + } + } + +#ifdef SQLITE_TEST + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3ExprInitTestInterface(db); + } +#endif + + /* Create the virtual table wrapper around the hash-table and overload + ** the two scalar functions. If this is successful, register the + ** module with sqlite. + */ + if( SQLITE_OK==rc + && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) + ){ + rc = sqlite3_create_module_v2( + db, "fts3", &fts3Module, (void *)pHash, hashDestroy + ); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_module_v2( + db, "fts4", &fts3Module, (void *)pHash, 0 + ); + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3InitTok(db, (void *)pHash); + } + return rc; + } + + + /* An error has occurred. Delete the hash table and return the error code. */ + assert( rc!=SQLITE_OK ); + if( pHash ){ + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); + } + return rc; +} + +/* +** Allocate an Fts3MultiSegReader for each token in the expression headed +** by pExpr. +** +** An Fts3SegReader object is a cursor that can seek or scan a range of +** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple +** Fts3SegReader objects internally to provide an interface to seek or scan +** within the union of all segments of a b-tree. Hence the name. +** +** If the allocated Fts3MultiSegReader just seeks to a single entry in a +** segment b-tree (if the term is not a prefix or it is a prefix for which +** there exists prefix b-tree of the right length) then it may be traversed +** and merged incrementally. Otherwise, it has to be merged into an in-memory +** doclist and then traversed. +*/ +static void fts3EvalAllocateReaders( + Fts3Cursor *pCsr, /* FTS cursor handle */ + Fts3Expr *pExpr, /* Allocate readers for this expression */ + int *pnToken, /* OUT: Total number of tokens in phrase. */ + int *pnOr, /* OUT: Total number of OR nodes in expr. */ + int *pRc /* IN/OUT: Error code */ +){ + if( pExpr && SQLITE_OK==*pRc ){ + if( pExpr->eType==FTSQUERY_PHRASE ){ + int i; + int nToken = pExpr->pPhrase->nToken; + *pnToken += nToken; + for(i=0; ipPhrase->aToken[i]; + int rc = fts3TermSegReaderCursor(pCsr, + pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr + ); + if( rc!=SQLITE_OK ){ + *pRc = rc; + return; + } + } + assert( pExpr->pPhrase->iDoclistToken==0 ); + pExpr->pPhrase->iDoclistToken = -1; + }else{ + *pnOr += (pExpr->eType==FTSQUERY_OR); + fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc); + fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc); + } + } +} + +/* +** Arguments pList/nList contain the doclist for token iToken of phrase p. +** It is merged into the main doclist stored in p->doclist.aAll/nAll. +** +** This function assumes that pList points to a buffer allocated using +** sqlite3_malloc(). This function takes responsibility for eventually +** freeing the buffer. +** +** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs. +*/ +static int fts3EvalPhraseMergeToken( + Fts3Table *pTab, /* FTS Table pointer */ + Fts3Phrase *p, /* Phrase to merge pList/nList into */ + int iToken, /* Token pList/nList corresponds to */ + char *pList, /* Pointer to doclist */ + int nList /* Number of bytes in pList */ +){ + int rc = SQLITE_OK; + assert( iToken!=p->iDoclistToken ); + + if( pList==0 ){ + sqlite3_free(p->doclist.aAll); + p->doclist.aAll = 0; + p->doclist.nAll = 0; + } + + else if( p->iDoclistToken<0 ){ + p->doclist.aAll = pList; + p->doclist.nAll = nList; + } + + else if( p->doclist.aAll==0 ){ + sqlite3_free(pList); + } + + else { + char *pLeft; + char *pRight; + int nLeft; + int nRight; + int nDiff; + + if( p->iDoclistTokendoclist.aAll; + nLeft = p->doclist.nAll; + pRight = pList; + nRight = nList; + nDiff = iToken - p->iDoclistToken; + }else{ + pRight = p->doclist.aAll; + nRight = p->doclist.nAll; + pLeft = pList; + nLeft = nList; + nDiff = p->iDoclistToken - iToken; + } + + rc = fts3DoclistPhraseMerge( + pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight + ); + sqlite3_free(pLeft); + p->doclist.aAll = pRight; + p->doclist.nAll = nRight; + } + + if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken; + return rc; +} + +/* +** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist +** does not take deferred tokens into account. +** +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. +*/ +static int fts3EvalPhraseLoad( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Phrase *p /* Phrase object */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int iToken; + int rc = SQLITE_OK; + + for(iToken=0; rc==SQLITE_OK && iTokennToken; iToken++){ + Fts3PhraseToken *pToken = &p->aToken[iToken]; + assert( pToken->pDeferred==0 || pToken->pSegcsr==0 ); + + if( pToken->pSegcsr ){ + int nThis = 0; + char *pThis = 0; + rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis); + if( rc==SQLITE_OK ){ + rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis); + } + } + assert( pToken->pSegcsr==0 ); + } + + return rc; +} + +/* +** This function is called on each phrase after the position lists for +** any deferred tokens have been loaded into memory. It updates the phrases +** current position list to include only those positions that are really +** instances of the phrase (after considering deferred tokens). If this +** means that the phrase does not appear in the current row, doclist.pList +** and doclist.nList are both zeroed. +** +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. +*/ +static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ + int iToken; /* Used to iterate through phrase tokens */ + char *aPoslist = 0; /* Position list for deferred tokens */ + int nPoslist = 0; /* Number of bytes in aPoslist */ + int iPrev = -1; /* Token number of previous deferred token */ + + assert( pPhrase->doclist.bFreeList==0 ); + + for(iToken=0; iTokennToken; iToken++){ + Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; + Fts3DeferredToken *pDeferred = pToken->pDeferred; + + if( pDeferred ){ + char *pList; + int nList; + int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList); + if( rc!=SQLITE_OK ) return rc; + + if( pList==0 ){ + sqlite3_free(aPoslist); + pPhrase->doclist.pList = 0; + pPhrase->doclist.nList = 0; + return SQLITE_OK; + + }else if( aPoslist==0 ){ + aPoslist = pList; + nPoslist = nList; + + }else{ + char *aOut = pList; + char *p1 = aPoslist; + char *p2 = aOut; + + assert( iPrev>=0 ); + fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2); + sqlite3_free(aPoslist); + aPoslist = pList; + nPoslist = (int)(aOut - aPoslist); + if( nPoslist==0 ){ + sqlite3_free(aPoslist); + pPhrase->doclist.pList = 0; + pPhrase->doclist.nList = 0; + return SQLITE_OK; + } + } + iPrev = iToken; + } + } + + if( iPrev>=0 ){ + int nMaxUndeferred = pPhrase->iDoclistToken; + if( nMaxUndeferred<0 ){ + pPhrase->doclist.pList = aPoslist; + pPhrase->doclist.nList = nPoslist; + pPhrase->doclist.iDocid = pCsr->iPrevId; + pPhrase->doclist.bFreeList = 1; + }else{ + int nDistance; + char *p1; + char *p2; + char *aOut; + + if( nMaxUndeferred>iPrev ){ + p1 = aPoslist; + p2 = pPhrase->doclist.pList; + nDistance = nMaxUndeferred - iPrev; + }else{ + p1 = pPhrase->doclist.pList; + p2 = aPoslist; + nDistance = iPrev - nMaxUndeferred; + } + + aOut = (char *)sqlite3_malloc(nPoslist+8); + if( !aOut ){ + sqlite3_free(aPoslist); + return SQLITE_NOMEM; + } + + pPhrase->doclist.pList = aOut; + if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){ + pPhrase->doclist.bFreeList = 1; + pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList); + }else{ + sqlite3_free(aOut); + pPhrase->doclist.pList = 0; + pPhrase->doclist.nList = 0; + } + sqlite3_free(aPoslist); + } + } + + return SQLITE_OK; +} + +/* +** Maximum number of tokens a phrase may have to be considered for the +** incremental doclists strategy. +*/ +#define MAX_INCR_PHRASE_TOKENS 4 + +/* +** This function is called for each Fts3Phrase in a full-text query +** expression to initialize the mechanism for returning rows. Once this +** function has been called successfully on an Fts3Phrase, it may be +** used with fts3EvalPhraseNext() to iterate through the matching docids. +** +** If parameter bOptOk is true, then the phrase may (or may not) use the +** incremental loading strategy. Otherwise, the entire doclist is loaded into +** memory within this call. +** +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. +*/ +static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; /* Error code */ + int i; + + /* Determine if doclists may be loaded from disk incrementally. This is + ** possible if the bOptOk argument is true, the FTS doclists will be + ** scanned in forward order, and the phrase consists of + ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first" + ** tokens or prefix tokens that cannot use a prefix-index. */ + int bHaveIncr = 0; + int bIncrOk = (bOptOk + && pCsr->bDesc==pTab->bDescIdx + && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0 +#ifdef SQLITE_TEST + && pTab->bNoIncrDoclist==0 +#endif + ); + for(i=0; bIncrOk==1 && inToken; i++){ + Fts3PhraseToken *pToken = &p->aToken[i]; + if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){ + bIncrOk = 0; + } + if( pToken->pSegcsr ) bHaveIncr = 1; + } + + if( bIncrOk && bHaveIncr ){ + /* Use the incremental approach. */ + int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn); + for(i=0; rc==SQLITE_OK && inToken; i++){ + Fts3PhraseToken *pToken = &p->aToken[i]; + Fts3MultiSegReader *pSegcsr = pToken->pSegcsr; + if( pSegcsr ){ + rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n); + } + } + p->bIncr = 1; + }else{ + /* Load the full doclist for the phrase into memory. */ + rc = fts3EvalPhraseLoad(pCsr, p); + p->bIncr = 0; + } + + assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr ); + return rc; +} + +/* +** This function is used to iterate backwards (from the end to start) +** through doclists. It is used by this module to iterate through phrase +** doclists in reverse and by the fts3_write.c module to iterate through +** pending-terms lists when writing to databases with "order=desc". +** +** The doclist may be sorted in ascending (parameter bDescIdx==0) or +** descending (parameter bDescIdx==1) order of docid. Regardless, this +** function iterates from the end of the doclist to the beginning. +*/ +SQLITE_PRIVATE void sqlite3Fts3DoclistPrev( + int bDescIdx, /* True if the doclist is desc */ + char *aDoclist, /* Pointer to entire doclist */ + int nDoclist, /* Length of aDoclist in bytes */ + char **ppIter, /* IN/OUT: Iterator pointer */ + sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */ + int *pnList, /* OUT: List length pointer */ + u8 *pbEof /* OUT: End-of-file flag */ +){ + char *p = *ppIter; + + assert( nDoclist>0 ); + assert( *pbEof==0 ); + assert( p || *piDocid==0 ); + assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) ); + + if( p==0 ){ + sqlite3_int64 iDocid = 0; + char *pNext = 0; + char *pDocid = aDoclist; + char *pEnd = &aDoclist[nDoclist]; + int iMul = 1; + + while( pDocid0 ); + assert( *pbEof==0 ); + assert( p || *piDocid==0 ); + assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) ); + + if( p==0 ){ + p = aDoclist; + p += sqlite3Fts3GetVarint(p, piDocid); + }else{ + fts3PoslistCopy(0, &p); + while( p<&aDoclist[nDoclist] && *p==0 ) p++; + if( p>=&aDoclist[nDoclist] ){ + *pbEof = 1; + }else{ + sqlite3_int64 iVar; + p += sqlite3Fts3GetVarint(p, &iVar); + *piDocid += ((bDescIdx ? -1 : 1) * iVar); + } + } + + *ppIter = p; +} + +/* +** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof +** to true if EOF is reached. +*/ +static void fts3EvalDlPhraseNext( + Fts3Table *pTab, + Fts3Doclist *pDL, + u8 *pbEof +){ + char *pIter; /* Used to iterate through aAll */ + char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */ + + if( pDL->pNextDocid ){ + pIter = pDL->pNextDocid; + }else{ + pIter = pDL->aAll; + } + + if( pIter>=pEnd ){ + /* We have already reached the end of this doclist. EOF. */ + *pbEof = 1; + }else{ + sqlite3_int64 iDelta; + pIter += sqlite3Fts3GetVarint(pIter, &iDelta); + if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){ + pDL->iDocid += iDelta; + }else{ + pDL->iDocid -= iDelta; + } + pDL->pList = pIter; + fts3PoslistCopy(0, &pIter); + pDL->nList = (int)(pIter - pDL->pList); + + /* pIter now points just past the 0x00 that terminates the position- + ** list for document pDL->iDocid. However, if this position-list was + ** edited in place by fts3EvalNearTrim(), then pIter may not actually + ** point to the start of the next docid value. The following line deals + ** with this case by advancing pIter past the zero-padding added by + ** fts3EvalNearTrim(). */ + while( pIterpNextDocid = pIter; + assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter ); + *pbEof = 0; + } +} + +/* +** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext(). +*/ +typedef struct TokenDoclist TokenDoclist; +struct TokenDoclist { + int bIgnore; + sqlite3_int64 iDocid; + char *pList; + int nList; +}; + +/* +** Token pToken is an incrementally loaded token that is part of a +** multi-token phrase. Advance it to the next matching document in the +** database and populate output variable *p with the details of the new +** entry. Or, if the iterator has reached EOF, set *pbEof to true. +** +** If an error occurs, return an SQLite error code. Otherwise, return +** SQLITE_OK. +*/ +static int incrPhraseTokenNext( + Fts3Table *pTab, /* Virtual table handle */ + Fts3Phrase *pPhrase, /* Phrase to advance token of */ + int iToken, /* Specific token to advance */ + TokenDoclist *p, /* OUT: Docid and doclist for new entry */ + u8 *pbEof /* OUT: True if iterator is at EOF */ +){ + int rc = SQLITE_OK; + + if( pPhrase->iDoclistToken==iToken ){ + assert( p->bIgnore==0 ); + assert( pPhrase->aToken[iToken].pSegcsr==0 ); + fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof); + p->pList = pPhrase->doclist.pList; + p->nList = pPhrase->doclist.nList; + p->iDocid = pPhrase->doclist.iDocid; + }else{ + Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; + assert( pToken->pDeferred==0 ); + assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 ); + if( pToken->pSegcsr ){ + assert( p->bIgnore==0 ); + rc = sqlite3Fts3MsrIncrNext( + pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList + ); + if( p->pList==0 ) *pbEof = 1; + }else{ + p->bIgnore = 1; + } + } + + return rc; +} + + +/* +** The phrase iterator passed as the second argument: +** +** * features at least one token that uses an incremental doclist, and +** +** * does not contain any deferred tokens. +** +** Advance it to the next matching documnent in the database and populate +** the Fts3Doclist.pList and nList fields. +** +** If there is no "next" entry and no error occurs, then *pbEof is set to +** 1 before returning. Otherwise, if no error occurs and the iterator is +** successfully advanced, *pbEof is set to 0. +** +** If an error occurs, return an SQLite error code. Otherwise, return +** SQLITE_OK. +*/ +static int fts3EvalIncrPhraseNext( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Phrase *p, /* Phrase object to advance to next docid */ + u8 *pbEof /* OUT: Set to 1 if EOF */ +){ + int rc = SQLITE_OK; + Fts3Doclist *pDL = &p->doclist; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + u8 bEof = 0; + + /* This is only called if it is guaranteed that the phrase has at least + ** one incremental token. In which case the bIncr flag is set. */ + assert( p->bIncr==1 ); + + if( p->nToken==1 && p->bIncr ){ + rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, + &pDL->iDocid, &pDL->pList, &pDL->nList + ); + if( pDL->pList==0 ) bEof = 1; + }else{ + int bDescDoclist = pCsr->bDesc; + struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS]; + + memset(a, 0, sizeof(a)); + assert( p->nToken<=MAX_INCR_PHRASE_TOKENS ); + assert( p->iDoclistTokennToken && bEof==0; i++){ + rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof); + if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){ + iMax = a[i].iDocid; + bMaxSet = 1; + } + } + assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) ); + assert( rc!=SQLITE_OK || bMaxSet ); + + /* Keep advancing iterators until they all point to the same document */ + for(i=0; inToken; i++){ + while( rc==SQLITE_OK && bEof==0 + && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0 + ){ + rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof); + if( DOCID_CMP(a[i].iDocid, iMax)>0 ){ + iMax = a[i].iDocid; + i = 0; + } + } + } + + /* Check if the current entries really are a phrase match */ + if( bEof==0 ){ + int nList = 0; + int nByte = a[p->nToken-1].nList; + char *aDoclist = sqlite3_malloc(nByte+1); + if( !aDoclist ) return SQLITE_NOMEM; + memcpy(aDoclist, a[p->nToken-1].pList, nByte+1); + + for(i=0; i<(p->nToken-1); i++){ + if( a[i].bIgnore==0 ){ + char *pL = a[i].pList; + char *pR = aDoclist; + char *pOut = aDoclist; + int nDist = p->nToken-1-i; + int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR); + if( res==0 ) break; + nList = (int)(pOut - aDoclist); + } + } + if( i==(p->nToken-1) ){ + pDL->iDocid = iMax; + pDL->pList = aDoclist; + pDL->nList = nList; + pDL->bFreeList = 1; + break; + } + sqlite3_free(aDoclist); + } + } + } + + *pbEof = bEof; + return rc; +} + +/* +** Attempt to move the phrase iterator to point to the next matching docid. +** If an error occurs, return an SQLite error code. Otherwise, return +** SQLITE_OK. +** +** If there is no "next" entry and no error occurs, then *pbEof is set to +** 1 before returning. Otherwise, if no error occurs and the iterator is +** successfully advanced, *pbEof is set to 0. +*/ +static int fts3EvalPhraseNext( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Phrase *p, /* Phrase object to advance to next docid */ + u8 *pbEof /* OUT: Set to 1 if EOF */ +){ + int rc = SQLITE_OK; + Fts3Doclist *pDL = &p->doclist; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + + if( p->bIncr ){ + rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof); + }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){ + sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, + &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof + ); + pDL->pList = pDL->pNextDocid; + }else{ + fts3EvalDlPhraseNext(pTab, pDL, pbEof); + } + + return rc; +} + +/* +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op. +** Otherwise, fts3EvalPhraseStart() is called on all phrases within the +** expression. Also the Fts3Expr.bDeferred variable is set to true for any +** expressions for which all descendent tokens are deferred. +** +** If parameter bOptOk is zero, then it is guaranteed that the +** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for +** each phrase in the expression (subject to deferred token processing). +** Or, if bOptOk is non-zero, then one or more tokens within the expression +** may be loaded incrementally, meaning doclist.aAll/nAll is not available. +** +** If an error occurs within this function, *pRc is set to an SQLite error +** code before returning. +*/ +static void fts3EvalStartReaders( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Expr *pExpr, /* Expression to initialize phrases in */ + int *pRc /* IN/OUT: Error code */ +){ + if( pExpr && SQLITE_OK==*pRc ){ + if( pExpr->eType==FTSQUERY_PHRASE ){ + int nToken = pExpr->pPhrase->nToken; + if( nToken ){ + int i; + for(i=0; ipPhrase->aToken[i].pDeferred==0 ) break; + } + pExpr->bDeferred = (i==nToken); + } + *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase); + }else{ + fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc); + fts3EvalStartReaders(pCsr, pExpr->pRight, pRc); + pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred); + } + } +} + +/* +** An array of the following structures is assembled as part of the process +** of selecting tokens to defer before the query starts executing (as part +** of the xFilter() method). There is one element in the array for each +** token in the FTS expression. +** +** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong +** to phrases that are connected only by AND and NEAR operators (not OR or +** NOT). When determining tokens to defer, each AND/NEAR cluster is considered +** separately. The root of a tokens AND/NEAR cluster is stored in +** Fts3TokenAndCost.pRoot. +*/ +typedef struct Fts3TokenAndCost Fts3TokenAndCost; +struct Fts3TokenAndCost { + Fts3Phrase *pPhrase; /* The phrase the token belongs to */ + int iToken; /* Position of token in phrase */ + Fts3PhraseToken *pToken; /* The token itself */ + Fts3Expr *pRoot; /* Root of NEAR/AND cluster */ + int nOvfl; /* Number of overflow pages to load doclist */ + int iCol; /* The column the token must match */ +}; + +/* +** This function is used to populate an allocated Fts3TokenAndCost array. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op. +** Otherwise, if an error occurs during execution, *pRc is set to an +** SQLite error code. +*/ +static void fts3EvalTokenCosts( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */ + Fts3Expr *pExpr, /* Expression to consider */ + Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */ + Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */ + int *pRc /* IN/OUT: Error code */ +){ + if( *pRc==SQLITE_OK ){ + if( pExpr->eType==FTSQUERY_PHRASE ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + int i; + for(i=0; *pRc==SQLITE_OK && inToken; i++){ + Fts3TokenAndCost *pTC = (*ppTC)++; + pTC->pPhrase = pPhrase; + pTC->iToken = i; + pTC->pRoot = pRoot; + pTC->pToken = &pPhrase->aToken[i]; + pTC->iCol = pPhrase->iColumn; + *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl); + } + }else if( pExpr->eType!=FTSQUERY_NOT ){ + assert( pExpr->eType==FTSQUERY_OR + || pExpr->eType==FTSQUERY_AND + || pExpr->eType==FTSQUERY_NEAR + ); + assert( pExpr->pLeft && pExpr->pRight ); + if( pExpr->eType==FTSQUERY_OR ){ + pRoot = pExpr->pLeft; + **ppOr = pRoot; + (*ppOr)++; + } + fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc); + if( pExpr->eType==FTSQUERY_OR ){ + pRoot = pExpr->pRight; + **ppOr = pRoot; + (*ppOr)++; + } + fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc); + } + } +} + +/* +** Determine the average document (row) size in pages. If successful, +** write this value to *pnPage and return SQLITE_OK. Otherwise, return +** an SQLite error code. +** +** The average document size in pages is calculated by first calculating +** determining the average size in bytes, B. If B is less than the amount +** of data that will fit on a single leaf page of an intkey table in +** this database, then the average docsize is 1. Otherwise, it is 1 plus +** the number of overflow pages consumed by a record B bytes in size. +*/ +static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){ + if( pCsr->nRowAvg==0 ){ + /* The average document size, which is required to calculate the cost + ** of each doclist, has not yet been determined. Read the required + ** data from the %_stat table to calculate it. + ** + ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 + ** varints, where nCol is the number of columns in the FTS3 table. + ** The first varint is the number of documents currently stored in + ** the table. The following nCol varints contain the total amount of + ** data stored in all rows of each column of the table, from left + ** to right. + */ + int rc; + Fts3Table *p = (Fts3Table*)pCsr->base.pVtab; + sqlite3_stmt *pStmt; + sqlite3_int64 nDoc = 0; + sqlite3_int64 nByte = 0; + const char *pEnd; + const char *a; + + rc = sqlite3Fts3SelectDoctotal(p, &pStmt); + if( rc!=SQLITE_OK ) return rc; + a = sqlite3_column_blob(pStmt, 0); + assert( a ); + + pEnd = &a[sqlite3_column_bytes(pStmt, 0)]; + a += sqlite3Fts3GetVarint(a, &nDoc); + while( anDoc = nDoc; + pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz); + assert( pCsr->nRowAvg>0 ); + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ) return rc; + } + + *pnPage = pCsr->nRowAvg; + return SQLITE_OK; +} + +/* +** This function is called to select the tokens (if any) that will be +** deferred. The array aTC[] has already been populated when this is +** called. +** +** This function is called once for each AND/NEAR cluster in the +** expression. Each invocation determines which tokens to defer within +** the cluster with root node pRoot. See comments above the definition +** of struct Fts3TokenAndCost for more details. +** +** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken() +** called on each token to defer. Otherwise, an SQLite error code is +** returned. +*/ +static int fts3EvalSelectDeferred( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Expr *pRoot, /* Consider tokens with this root node */ + Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */ + int nTC /* Number of entries in aTC[] */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int nDocSize = 0; /* Number of pages per doc loaded */ + int rc = SQLITE_OK; /* Return code */ + int ii; /* Iterator variable for various purposes */ + int nOvfl = 0; /* Total overflow pages used by doclists */ + int nToken = 0; /* Total number of tokens in cluster */ + + int nMinEst = 0; /* The minimum count for any phrase so far. */ + int nLoad4 = 1; /* (Phrases that will be loaded)^4. */ + + /* Tokens are never deferred for FTS tables created using the content=xxx + ** option. The reason being that it is not guaranteed that the content + ** table actually contains the same data as the index. To prevent this from + ** causing any problems, the deferred token optimization is completely + ** disabled for content=xxx tables. */ + if( pTab->zContentTbl ){ + return SQLITE_OK; + } + + /* Count the tokens in this AND/NEAR cluster. If none of the doclists + ** associated with the tokens spill onto overflow pages, or if there is + ** only 1 token, exit early. No tokens to defer in this case. */ + for(ii=0; ii0 ); + + + /* Iterate through all tokens in this AND/NEAR cluster, in ascending order + ** of the number of overflow pages that will be loaded by the pager layer + ** to retrieve the entire doclist for the token from the full-text index. + ** Load the doclists for tokens that are either: + ** + ** a. The cheapest token in the entire query (i.e. the one visited by the + ** first iteration of this loop), or + ** + ** b. Part of a multi-token phrase. + ** + ** After each token doclist is loaded, merge it with the others from the + ** same phrase and count the number of documents that the merged doclist + ** contains. Set variable "nMinEst" to the smallest number of documents in + ** any phrase doclist for which 1 or more token doclists have been loaded. + ** Let nOther be the number of other phrases for which it is certain that + ** one or more tokens will not be deferred. + ** + ** Then, for each token, defer it if loading the doclist would result in + ** loading N or more overflow pages into memory, where N is computed as: + ** + ** (nMinEst + 4^nOther - 1) / (4^nOther) + */ + for(ii=0; iinOvfl) + ){ + pTC = &aTC[iTC]; + } + } + assert( pTC ); + + if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){ + /* The number of overflow pages to load for this (and therefore all + ** subsequent) tokens is greater than the estimated number of pages + ** that will be loaded if all subsequent tokens are deferred. + */ + Fts3PhraseToken *pToken = pTC->pToken; + rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol); + fts3SegReaderCursorFree(pToken->pSegcsr); + pToken->pSegcsr = 0; + }else{ + /* Set nLoad4 to the value of (4^nOther) for the next iteration of the + ** for-loop. Except, limit the value to 2^24 to prevent it from + ** overflowing the 32-bit integer it is stored in. */ + if( ii<12 ) nLoad4 = nLoad4*4; + + if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){ + /* Either this is the cheapest token in the entire query, or it is + ** part of a multi-token phrase. Either way, the entire doclist will + ** (eventually) be loaded into memory. It may as well be now. */ + Fts3PhraseToken *pToken = pTC->pToken; + int nList = 0; + char *pList = 0; + rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList); + assert( rc==SQLITE_OK || pList==0 ); + if( rc==SQLITE_OK ){ + rc = fts3EvalPhraseMergeToken( + pTab, pTC->pPhrase, pTC->iToken,pList,nList + ); + } + if( rc==SQLITE_OK ){ + int nCount; + nCount = fts3DoclistCountDocids( + pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll + ); + if( ii==0 || nCountpToken = 0; + } + + return rc; +} + +/* +** This function is called from within the xFilter method. It initializes +** the full-text query currently stored in pCsr->pExpr. To iterate through +** the results of a query, the caller does: +** +** fts3EvalStart(pCsr); +** while( 1 ){ +** fts3EvalNext(pCsr); +** if( pCsr->bEof ) break; +** ... return row pCsr->iPrevId to the caller ... +** } +*/ +static int fts3EvalStart(Fts3Cursor *pCsr){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int nToken = 0; + int nOr = 0; + + /* Allocate a MultiSegReader for each token in the expression. */ + fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc); + + /* Determine which, if any, tokens in the expression should be deferred. */ +#ifndef SQLITE_DISABLE_FTS4_DEFERRED + if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){ + Fts3TokenAndCost *aTC; + Fts3Expr **apOr; + aTC = (Fts3TokenAndCost *)sqlite3_malloc( + sizeof(Fts3TokenAndCost) * nToken + + sizeof(Fts3Expr *) * nOr * 2 + ); + apOr = (Fts3Expr **)&aTC[nToken]; + + if( !aTC ){ + rc = SQLITE_NOMEM; + }else{ + int ii; + Fts3TokenAndCost *pTC = aTC; + Fts3Expr **ppOr = apOr; + + fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc); + nToken = (int)(pTC-aTC); + nOr = (int)(ppOr-apOr); + + if( rc==SQLITE_OK ){ + rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken); + for(ii=0; rc==SQLITE_OK && iipExpr, &rc); + return rc; +} + +/* +** Invalidate the current position list for phrase pPhrase. +*/ +static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){ + if( pPhrase->doclist.bFreeList ){ + sqlite3_free(pPhrase->doclist.pList); + } + pPhrase->doclist.pList = 0; + pPhrase->doclist.nList = 0; + pPhrase->doclist.bFreeList = 0; +} + +/* +** This function is called to edit the position list associated with +** the phrase object passed as the fifth argument according to a NEAR +** condition. For example: +** +** abc NEAR/5 "def ghi" +** +** Parameter nNear is passed the NEAR distance of the expression (5 in +** the example above). When this function is called, *paPoslist points to +** the position list, and *pnToken is the number of phrase tokens in, the +** phrase on the other side of the NEAR operator to pPhrase. For example, +** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to +** the position list associated with phrase "abc". +** +** All positions in the pPhrase position list that are not sufficiently +** close to a position in the *paPoslist position list are removed. If this +** leaves 0 positions, zero is returned. Otherwise, non-zero. +** +** Before returning, *paPoslist is set to point to the position lsit +** associated with pPhrase. And *pnToken is set to the number of tokens in +** pPhrase. +*/ +static int fts3EvalNearTrim( + int nNear, /* NEAR distance. As in "NEAR/nNear". */ + char *aTmp, /* Temporary space to use */ + char **paPoslist, /* IN/OUT: Position list */ + int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */ + Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */ +){ + int nParam1 = nNear + pPhrase->nToken; + int nParam2 = nNear + *pnToken; + int nNew; + char *p2; + char *pOut; + int res; + + assert( pPhrase->doclist.pList ); + + p2 = pOut = pPhrase->doclist.pList; + res = fts3PoslistNearMerge( + &pOut, aTmp, nParam1, nParam2, paPoslist, &p2 + ); + if( res ){ + nNew = (int)(pOut - pPhrase->doclist.pList) - 1; + assert( pPhrase->doclist.pList[nNew]=='\0' ); + assert( nNew<=pPhrase->doclist.nList && nNew>0 ); + memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew); + pPhrase->doclist.nList = nNew; + *paPoslist = pPhrase->doclist.pList; + *pnToken = pPhrase->nToken; + } + + return res; +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is called. +** Otherwise, it advances the expression passed as the second argument to +** point to the next matching row in the database. Expressions iterate through +** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero, +** or descending if it is non-zero. +** +** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if +** successful, the following variables in pExpr are set: +** +** Fts3Expr.bEof (non-zero if EOF - there is no next row) +** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row) +** +** If the expression is of type FTSQUERY_PHRASE, and the expression is not +** at EOF, then the following variables are populated with the position list +** for the phrase for the visited row: +** +** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes) +** FTs3Expr.pPhrase->doclist.pList (pointer to position list) +** +** It says above that this function advances the expression to the next +** matching row. This is usually true, but there are the following exceptions: +** +** 1. Deferred tokens are not taken into account. If a phrase consists +** entirely of deferred tokens, it is assumed to match every row in +** the db. In this case the position-list is not populated at all. +** +** Or, if a phrase contains one or more deferred tokens and one or +** more non-deferred tokens, then the expression is advanced to the +** next possible match, considering only non-deferred tokens. In other +** words, if the phrase is "A B C", and "B" is deferred, the expression +** is advanced to the next row that contains an instance of "A * C", +** where "*" may match any single token. The position list in this case +** is populated as for "A * C" before returning. +** +** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is +** advanced to point to the next row that matches "x AND y". +** +** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is +** really a match, taking into account deferred tokens and NEAR operators. +*/ +static void fts3EvalNextRow( + Fts3Cursor *pCsr, /* FTS Cursor handle */ + Fts3Expr *pExpr, /* Expr. to advance to next matching row */ + int *pRc /* IN/OUT: Error code */ +){ + if( *pRc==SQLITE_OK ){ + int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */ + assert( pExpr->bEof==0 ); + pExpr->bStart = 1; + + switch( pExpr->eType ){ + case FTSQUERY_NEAR: + case FTSQUERY_AND: { + Fts3Expr *pLeft = pExpr->pLeft; + Fts3Expr *pRight = pExpr->pRight; + assert( !pLeft->bDeferred || !pRight->bDeferred ); + + if( pLeft->bDeferred ){ + /* LHS is entirely deferred. So we assume it matches every row. + ** Advance the RHS iterator to find the next row visited. */ + fts3EvalNextRow(pCsr, pRight, pRc); + pExpr->iDocid = pRight->iDocid; + pExpr->bEof = pRight->bEof; + }else if( pRight->bDeferred ){ + /* RHS is entirely deferred. So we assume it matches every row. + ** Advance the LHS iterator to find the next row visited. */ + fts3EvalNextRow(pCsr, pLeft, pRc); + pExpr->iDocid = pLeft->iDocid; + pExpr->bEof = pLeft->bEof; + }else{ + /* Neither the RHS or LHS are deferred. */ + fts3EvalNextRow(pCsr, pLeft, pRc); + fts3EvalNextRow(pCsr, pRight, pRc); + while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){ + sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid); + if( iDiff==0 ) break; + if( iDiff<0 ){ + fts3EvalNextRow(pCsr, pLeft, pRc); + }else{ + fts3EvalNextRow(pCsr, pRight, pRc); + } + } + pExpr->iDocid = pLeft->iDocid; + pExpr->bEof = (pLeft->bEof || pRight->bEof); + if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){ + if( pRight->pPhrase && pRight->pPhrase->doclist.aAll ){ + Fts3Doclist *pDl = &pRight->pPhrase->doclist; + while( *pRc==SQLITE_OK && pRight->bEof==0 ){ + memset(pDl->pList, 0, pDl->nList); + fts3EvalNextRow(pCsr, pRight, pRc); + } + } + if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){ + Fts3Doclist *pDl = &pLeft->pPhrase->doclist; + while( *pRc==SQLITE_OK && pLeft->bEof==0 ){ + memset(pDl->pList, 0, pDl->nList); + fts3EvalNextRow(pCsr, pLeft, pRc); + } + } + } + } + break; + } + + case FTSQUERY_OR: { + Fts3Expr *pLeft = pExpr->pLeft; + Fts3Expr *pRight = pExpr->pRight; + sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid); + + assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid ); + assert( pRight->bStart || pLeft->iDocid==pRight->iDocid ); + + if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){ + fts3EvalNextRow(pCsr, pLeft, pRc); + }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){ + fts3EvalNextRow(pCsr, pRight, pRc); + }else{ + fts3EvalNextRow(pCsr, pLeft, pRc); + fts3EvalNextRow(pCsr, pRight, pRc); + } + + pExpr->bEof = (pLeft->bEof && pRight->bEof); + iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid); + if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){ + pExpr->iDocid = pLeft->iDocid; + }else{ + pExpr->iDocid = pRight->iDocid; + } + + break; + } + + case FTSQUERY_NOT: { + Fts3Expr *pLeft = pExpr->pLeft; + Fts3Expr *pRight = pExpr->pRight; + + if( pRight->bStart==0 ){ + fts3EvalNextRow(pCsr, pRight, pRc); + assert( *pRc!=SQLITE_OK || pRight->bStart ); + } + + fts3EvalNextRow(pCsr, pLeft, pRc); + if( pLeft->bEof==0 ){ + while( !*pRc + && !pRight->bEof + && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 + ){ + fts3EvalNextRow(pCsr, pRight, pRc); + } + } + pExpr->iDocid = pLeft->iDocid; + pExpr->bEof = pLeft->bEof; + break; + } + + default: { + Fts3Phrase *pPhrase = pExpr->pPhrase; + fts3EvalInvalidatePoslist(pPhrase); + *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof); + pExpr->iDocid = pPhrase->doclist.iDocid; + break; + } + } + } +} + +/* +** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR +** cluster, then this function returns 1 immediately. +** +** Otherwise, it checks if the current row really does match the NEAR +** expression, using the data currently stored in the position lists +** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. +** +** If the current row is a match, the position list associated with each +** phrase in the NEAR expression is edited in place to contain only those +** phrase instances sufficiently close to their peers to satisfy all NEAR +** constraints. In this case it returns 1. If the NEAR expression does not +** match the current row, 0 is returned. The position lists may or may not +** be edited if 0 is returned. +*/ +static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){ + int res = 1; + + /* The following block runs if pExpr is the root of a NEAR query. + ** For example, the query: + ** + ** "w" NEAR "x" NEAR "y" NEAR "z" + ** + ** which is represented in tree form as: + ** + ** | + ** +--NEAR--+ <-- root of NEAR query + ** | | + ** +--NEAR--+ "z" + ** | | + ** +--NEAR--+ "y" + ** | | + ** "w" "x" + ** + ** The right-hand child of a NEAR node is always a phrase. The + ** left-hand child may be either a phrase or a NEAR node. There are + ** no exceptions to this - it's the way the parser in fts3_expr.c works. + */ + if( *pRc==SQLITE_OK + && pExpr->eType==FTSQUERY_NEAR + && pExpr->bEof==0 + && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR) + ){ + Fts3Expr *p; + int nTmp = 0; /* Bytes of temp space */ + char *aTmp; /* Temp space for PoslistNearMerge() */ + + /* Allocate temporary working space. */ + for(p=pExpr; p->pLeft; p=p->pLeft){ + nTmp += p->pRight->pPhrase->doclist.nList; + } + nTmp += p->pPhrase->doclist.nList; + if( nTmp==0 ){ + res = 0; + }else{ + aTmp = sqlite3_malloc(nTmp*2); + if( !aTmp ){ + *pRc = SQLITE_NOMEM; + res = 0; + }else{ + char *aPoslist = p->pPhrase->doclist.pList; + int nToken = p->pPhrase->nToken; + + for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){ + Fts3Phrase *pPhrase = p->pRight->pPhrase; + int nNear = p->nNear; + res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); + } + + aPoslist = pExpr->pRight->pPhrase->doclist.pList; + nToken = pExpr->pRight->pPhrase->nToken; + for(p=pExpr->pLeft; p && res; p=p->pLeft){ + int nNear; + Fts3Phrase *pPhrase; + assert( p->pParent && p->pParent->pLeft==p ); + nNear = p->pParent->nNear; + pPhrase = ( + p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase + ); + res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); + } + } + + sqlite3_free(aTmp); + } + } + + return res; +} + +/* +** This function is a helper function for sqlite3Fts3EvalTestDeferred(). +** Assuming no error occurs or has occurred, It returns non-zero if the +** expression passed as the second argument matches the row that pCsr +** currently points to, or zero if it does not. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op. +** If an error occurs during execution of this function, *pRc is set to +** the appropriate SQLite error code. In this case the returned value is +** undefined. +*/ +static int fts3EvalTestExpr( + Fts3Cursor *pCsr, /* FTS cursor handle */ + Fts3Expr *pExpr, /* Expr to test. May or may not be root. */ + int *pRc /* IN/OUT: Error code */ +){ + int bHit = 1; /* Return value */ + if( *pRc==SQLITE_OK ){ + switch( pExpr->eType ){ + case FTSQUERY_NEAR: + case FTSQUERY_AND: + bHit = ( + fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc) + && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc) + && fts3EvalNearTest(pExpr, pRc) + ); + + /* If the NEAR expression does not match any rows, zero the doclist for + ** all phrases involved in the NEAR. This is because the snippet(), + ** offsets() and matchinfo() functions are not supposed to recognize + ** any instances of phrases that are part of unmatched NEAR queries. + ** For example if this expression: + ** + ** ... MATCH 'a OR (b NEAR c)' + ** + ** is matched against a row containing: + ** + ** 'a b d e' + ** + ** then any snippet() should ony highlight the "a" term, not the "b" + ** (as "b" is part of a non-matching NEAR clause). + */ + if( bHit==0 + && pExpr->eType==FTSQUERY_NEAR + && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR) + ){ + Fts3Expr *p; + for(p=pExpr; p->pPhrase==0; p=p->pLeft){ + if( p->pRight->iDocid==pCsr->iPrevId ){ + fts3EvalInvalidatePoslist(p->pRight->pPhrase); + } + } + if( p->iDocid==pCsr->iPrevId ){ + fts3EvalInvalidatePoslist(p->pPhrase); + } + } + + break; + + case FTSQUERY_OR: { + int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc); + int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc); + bHit = bHit1 || bHit2; + break; + } + + case FTSQUERY_NOT: + bHit = ( + fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc) + && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc) + ); + break; + + default: { +#ifndef SQLITE_DISABLE_FTS4_DEFERRED + if( pCsr->pDeferred + && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred) + ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 ); + if( pExpr->bDeferred ){ + fts3EvalInvalidatePoslist(pPhrase); + } + *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase); + bHit = (pPhrase->doclist.pList!=0); + pExpr->iDocid = pCsr->iPrevId; + }else +#endif + { + bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId); + } + break; + } + } + } + return bHit; +} + +/* +** This function is called as the second part of each xNext operation when +** iterating through the results of a full-text query. At this point the +** cursor points to a row that matches the query expression, with the +** following caveats: +** +** * Up until this point, "NEAR" operators in the expression have been +** treated as "AND". +** +** * Deferred tokens have not yet been considered. +** +** If *pRc is not SQLITE_OK when this function is called, it immediately +** returns 0. Otherwise, it tests whether or not after considering NEAR +** operators and deferred tokens the current row is still a match for the +** expression. It returns 1 if both of the following are true: +** +** 1. *pRc is SQLITE_OK when this function returns, and +** +** 2. After scanning the current FTS table row for the deferred tokens, +** it is determined that the row does *not* match the query. +** +** Or, if no error occurs and it seems the current row does match the FTS +** query, return 0. +*/ +SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){ + int rc = *pRc; + int bMiss = 0; + if( rc==SQLITE_OK ){ + + /* If there are one or more deferred tokens, load the current row into + ** memory and scan it to determine the position list for each deferred + ** token. Then, see if this row is really a match, considering deferred + ** tokens and NEAR operators (neither of which were taken into account + ** earlier, by fts3EvalNextRow()). + */ + if( pCsr->pDeferred ){ + rc = fts3CursorSeek(0, pCsr); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3CacheDeferredDoclists(pCsr); + } + } + bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc)); + + /* Free the position-lists accumulated for each deferred token above. */ + sqlite3Fts3FreeDeferredDoclists(pCsr); + *pRc = rc; + } + return (rc==SQLITE_OK && bMiss); +} + +/* +** Advance to the next document that matches the FTS expression in +** Fts3Cursor.pExpr. +*/ +static int fts3EvalNext(Fts3Cursor *pCsr){ + int rc = SQLITE_OK; /* Return Code */ + Fts3Expr *pExpr = pCsr->pExpr; + assert( pCsr->isEof==0 ); + if( pExpr==0 ){ + pCsr->isEof = 1; + }else{ + do { + if( pCsr->isRequireSeek==0 ){ + sqlite3_reset(pCsr->pStmt); + } + assert( sqlite3_data_count(pCsr->pStmt)==0 ); + fts3EvalNextRow(pCsr, pExpr, &rc); + pCsr->isEof = pExpr->bEof; + pCsr->isRequireSeek = 1; + pCsr->isMatchinfoNeeded = 1; + pCsr->iPrevId = pExpr->iDocid; + }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) ); + } + + /* Check if the cursor is past the end of the docid range specified + ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */ + if( rc==SQLITE_OK && ( + (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid) + || (pCsr->bDesc!=0 && pCsr->iPrevIdiMinDocid) + )){ + pCsr->isEof = 1; + } + + return rc; +} + +/* +** Restart interation for expression pExpr so that the next call to +** fts3EvalNext() visits the first row. Do not allow incremental +** loading or merging of phrase doclists for this iteration. +** +** If *pRc is other than SQLITE_OK when this function is called, it is +** a no-op. If an error occurs within this function, *pRc is set to an +** SQLite error code before returning. +*/ +static void fts3EvalRestart( + Fts3Cursor *pCsr, + Fts3Expr *pExpr, + int *pRc +){ + if( pExpr && *pRc==SQLITE_OK ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + + if( pPhrase ){ + fts3EvalInvalidatePoslist(pPhrase); + if( pPhrase->bIncr ){ + int i; + for(i=0; inToken; i++){ + Fts3PhraseToken *pToken = &pPhrase->aToken[i]; + assert( pToken->pDeferred==0 ); + if( pToken->pSegcsr ){ + sqlite3Fts3MsrIncrRestart(pToken->pSegcsr); + } + } + *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase); + } + pPhrase->doclist.pNextDocid = 0; + pPhrase->doclist.iDocid = 0; + pPhrase->pOrPoslist = 0; + } + + pExpr->iDocid = 0; + pExpr->bEof = 0; + pExpr->bStart = 0; + + fts3EvalRestart(pCsr, pExpr->pLeft, pRc); + fts3EvalRestart(pCsr, pExpr->pRight, pRc); + } +} + +/* +** After allocating the Fts3Expr.aMI[] array for each phrase in the +** expression rooted at pExpr, the cursor iterates through all rows matched +** by pExpr, calling this function for each row. This function increments +** the values in Fts3Expr.aMI[] according to the position-list currently +** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase +** expression nodes. +*/ +static void fts3EvalUpdateCounts(Fts3Expr *pExpr){ + if( pExpr ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + if( pPhrase && pPhrase->doclist.pList ){ + int iCol = 0; + char *p = pPhrase->doclist.pList; + + assert( *p ); + while( 1 ){ + u8 c = 0; + int iCnt = 0; + while( 0xFE & (*p | c) ){ + if( (c&0x80)==0 ) iCnt++; + c = *p++ & 0x80; + } + + /* aMI[iCol*3 + 1] = Number of occurrences + ** aMI[iCol*3 + 2] = Number of rows containing at least one instance + */ + pExpr->aMI[iCol*3 + 1] += iCnt; + pExpr->aMI[iCol*3 + 2] += (iCnt>0); + if( *p==0x00 ) break; + p++; + p += fts3GetVarint32(p, &iCol); + } + } + + fts3EvalUpdateCounts(pExpr->pLeft); + fts3EvalUpdateCounts(pExpr->pRight); + } +} + +/* +** Expression pExpr must be of type FTSQUERY_PHRASE. +** +** If it is not already allocated and populated, this function allocates and +** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part +** of a NEAR expression, then it also allocates and populates the same array +** for all other phrases that are part of the NEAR expression. +** +** SQLITE_OK is returned if the aMI[] array is successfully allocated and +** populated. Otherwise, if an error occurs, an SQLite error code is returned. +*/ +static int fts3EvalGatherStats( + Fts3Cursor *pCsr, /* Cursor object */ + Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */ +){ + int rc = SQLITE_OK; /* Return code */ + + assert( pExpr->eType==FTSQUERY_PHRASE ); + if( pExpr->aMI==0 ){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + Fts3Expr *pRoot; /* Root of NEAR expression */ + Fts3Expr *p; /* Iterator used for several purposes */ + + sqlite3_int64 iPrevId = pCsr->iPrevId; + sqlite3_int64 iDocid; + u8 bEof; + + /* Find the root of the NEAR expression */ + pRoot = pExpr; + while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){ + pRoot = pRoot->pParent; + } + iDocid = pRoot->iDocid; + bEof = pRoot->bEof; + assert( pRoot->bStart ); + + /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */ + for(p=pRoot; p; p=p->pLeft){ + Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight); + assert( pE->aMI==0 ); + pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32)); + if( !pE->aMI ) return SQLITE_NOMEM; + memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32)); + } + + fts3EvalRestart(pCsr, pRoot, &rc); + + while( pCsr->isEof==0 && rc==SQLITE_OK ){ + + do { + /* Ensure the %_content statement is reset. */ + if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt); + assert( sqlite3_data_count(pCsr->pStmt)==0 ); + + /* Advance to the next document */ + fts3EvalNextRow(pCsr, pRoot, &rc); + pCsr->isEof = pRoot->bEof; + pCsr->isRequireSeek = 1; + pCsr->isMatchinfoNeeded = 1; + pCsr->iPrevId = pRoot->iDocid; + }while( pCsr->isEof==0 + && pRoot->eType==FTSQUERY_NEAR + && sqlite3Fts3EvalTestDeferred(pCsr, &rc) + ); + + if( rc==SQLITE_OK && pCsr->isEof==0 ){ + fts3EvalUpdateCounts(pRoot); + } + } + + pCsr->isEof = 0; + pCsr->iPrevId = iPrevId; + + if( bEof ){ + pRoot->bEof = bEof; + }else{ + /* Caution: pRoot may iterate through docids in ascending or descending + ** order. For this reason, even though it seems more defensive, the + ** do loop can not be written: + ** + ** do {...} while( pRoot->iDocidbEof==0 ); + }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK ); + } + } + return rc; +} + +/* +** This function is used by the matchinfo() module to query a phrase +** expression node for the following information: +** +** 1. The total number of occurrences of the phrase in each column of +** the FTS table (considering all rows), and +** +** 2. For each column, the number of rows in the table for which the +** column contains at least one instance of the phrase. +** +** If no error occurs, SQLITE_OK is returned and the values for each column +** written into the array aiOut as follows: +** +** aiOut[iCol*3 + 1] = Number of occurrences +** aiOut[iCol*3 + 2] = Number of rows containing at least one instance +** +** Caveats: +** +** * If a phrase consists entirely of deferred tokens, then all output +** values are set to the number of documents in the table. In other +** words we assume that very common tokens occur exactly once in each +** column of each row of the table. +** +** * If a phrase contains some deferred tokens (and some non-deferred +** tokens), count the potential occurrence identified by considering +** the non-deferred tokens instead of actual phrase occurrences. +** +** * If the phrase is part of a NEAR expression, then only phrase instances +** that meet the NEAR constraint are included in the counts. +*/ +SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats( + Fts3Cursor *pCsr, /* FTS cursor handle */ + Fts3Expr *pExpr, /* Phrase expression */ + u32 *aiOut /* Array to write results into (see above) */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int iCol; + + if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){ + assert( pCsr->nDoc>0 ); + for(iCol=0; iColnColumn; iCol++){ + aiOut[iCol*3 + 1] = (u32)pCsr->nDoc; + aiOut[iCol*3 + 2] = (u32)pCsr->nDoc; + } + }else{ + rc = fts3EvalGatherStats(pCsr, pExpr); + if( rc==SQLITE_OK ){ + assert( pExpr->aMI ); + for(iCol=0; iColnColumn; iCol++){ + aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1]; + aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2]; + } + } + } + + return rc; +} + +/* +** The expression pExpr passed as the second argument to this function +** must be of type FTSQUERY_PHRASE. +** +** The returned value is either NULL or a pointer to a buffer containing +** a position-list indicating the occurrences of the phrase in column iCol +** of the current row. +** +** More specifically, the returned buffer contains 1 varint for each +** occurrence of the phrase in the column, stored using the normal (delta+2) +** compression and is terminated by either an 0x01 or 0x00 byte. For example, +** if the requested column contains "a b X c d X X" and the position-list +** for 'X' is requested, the buffer returned may contain: +** +** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00 +** +** This function works regardless of whether or not the phrase is deferred, +** incremental, or neither. +*/ +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist( + Fts3Cursor *pCsr, /* FTS3 cursor object */ + Fts3Expr *pExpr, /* Phrase to return doclist for */ + int iCol, /* Column to return position list for */ + char **ppOut /* OUT: Pointer to position list */ +){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + char *pIter; + int iThis; + sqlite3_int64 iDocid; + + /* If this phrase is applies specifically to some column other than + ** column iCol, return a NULL pointer. */ + *ppOut = 0; + assert( iCol>=0 && iColnColumn ); + if( (pPhrase->iColumnnColumn && pPhrase->iColumn!=iCol) ){ + return SQLITE_OK; + } + + iDocid = pExpr->iDocid; + pIter = pPhrase->doclist.pList; + if( iDocid!=pCsr->iPrevId || pExpr->bEof ){ + int rc = SQLITE_OK; + int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */ + int bOr = 0; + u8 bTreeEof = 0; + Fts3Expr *p; /* Used to iterate from pExpr to root */ + Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */ + int bMatch; + + /* Check if this phrase descends from an OR expression node. If not, + ** return NULL. Otherwise, the entry that corresponds to docid + ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the + ** tree that the node is part of has been marked as EOF, but the node + ** itself is not EOF, then it may point to an earlier entry. */ + pNear = pExpr; + for(p=pExpr->pParent; p; p=p->pParent){ + if( p->eType==FTSQUERY_OR ) bOr = 1; + if( p->eType==FTSQUERY_NEAR ) pNear = p; + if( p->bEof ) bTreeEof = 1; + } + if( bOr==0 ) return SQLITE_OK; + + /* This is the descendent of an OR node. In this case we cannot use + ** an incremental phrase. Load the entire doclist for the phrase + ** into memory in this case. */ + if( pPhrase->bIncr ){ + int bEofSave = pNear->bEof; + fts3EvalRestart(pCsr, pNear, &rc); + while( rc==SQLITE_OK && !pNear->bEof ){ + fts3EvalNextRow(pCsr, pNear, &rc); + if( bEofSave==0 && pNear->iDocid==iDocid ) break; + } + assert( rc!=SQLITE_OK || pPhrase->bIncr==0 ); + } + if( bTreeEof ){ + while( rc==SQLITE_OK && !pNear->bEof ){ + fts3EvalNextRow(pCsr, pNear, &rc); + } + } + if( rc!=SQLITE_OK ) return rc; + + bMatch = 1; + for(p=pNear; p; p=p->pLeft){ + u8 bEof = 0; + Fts3Expr *pTest = p; + Fts3Phrase *pPh; + assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE ); + if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight; + assert( pTest->eType==FTSQUERY_PHRASE ); + pPh = pTest->pPhrase; + + pIter = pPh->pOrPoslist; + iDocid = pPh->iOrDocid; + if( pCsr->bDesc==bDescDoclist ){ + bEof = !pPh->doclist.nAll || + (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll)); + while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){ + sqlite3Fts3DoclistNext( + bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll, + &pIter, &iDocid, &bEof + ); + } + }else{ + bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll); + while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){ + int dummy; + sqlite3Fts3DoclistPrev( + bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll, + &pIter, &iDocid, &dummy, &bEof + ); + } + } + pPh->pOrPoslist = pIter; + pPh->iOrDocid = iDocid; + if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0; + } + + if( bMatch ){ + pIter = pPhrase->pOrPoslist; + }else{ + pIter = 0; + } + } + if( pIter==0 ) return SQLITE_OK; + + if( *pIter==0x01 ){ + pIter++; + pIter += fts3GetVarint32(pIter, &iThis); + }else{ + iThis = 0; + } + while( iThisdoclist, and +** * any Fts3MultiSegReader objects held by phrase tokens. +*/ +SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){ + if( pPhrase ){ + int i; + sqlite3_free(pPhrase->doclist.aAll); + fts3EvalInvalidatePoslist(pPhrase); + memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist)); + for(i=0; inToken; i++){ + fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr); + pPhrase->aToken[i].pSegcsr = 0; + } + } +} + + +/* +** Return SQLITE_CORRUPT_VTAB. +*/ +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3Fts3Corrupt(){ + return SQLITE_CORRUPT_VTAB; +} +#endif + +#if !SQLITE_CORE +/* +** Initialize API pointer table, if required. +*/ +#ifdef _WIN32 +__declspec(dllexport) +#endif +SQLITE_API int SQLITE_STDCALL sqlite3_fts3_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3Fts3Init(db); +} +#endif + +#endif + +/************** End of fts3.c ************************************************/ +/************** Begin file fts3_aux.c ****************************************/ +/* +** 2011 Jan 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ + +typedef struct Fts3auxTable Fts3auxTable; +typedef struct Fts3auxCursor Fts3auxCursor; + +struct Fts3auxTable { + sqlite3_vtab base; /* Base class used by SQLite core */ + Fts3Table *pFts3Tab; +}; + +struct Fts3auxCursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + Fts3MultiSegReader csr; /* Must be right after "base" */ + Fts3SegFilter filter; + char *zStop; + int nStop; /* Byte-length of string zStop */ + int iLangid; /* Language id to query */ + int isEof; /* True if cursor is at EOF */ + sqlite3_int64 iRowid; /* Current rowid */ + + int iCol; /* Current value of 'col' column */ + int nStat; /* Size of aStat[] array */ + struct Fts3auxColstats { + sqlite3_int64 nDoc; /* 'documents' values for current csr row */ + sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */ + } *aStat; +}; + +/* +** Schema of the terms table. +*/ +#define FTS3_AUX_SCHEMA \ + "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)" + +/* +** This function does all the work for both the xConnect and xCreate methods. +** These tables have no persistent representation of their own, so xConnect +** and xCreate are identical operations. +*/ +static int fts3auxConnectMethod( + sqlite3 *db, /* Database connection */ + void *pUnused, /* Unused */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + char const *zDb; /* Name of database (e.g. "main") */ + char const *zFts3; /* Name of fts3 table */ + int nDb; /* Result of strlen(zDb) */ + int nFts3; /* Result of strlen(zFts3) */ + int nByte; /* Bytes of space to allocate here */ + int rc; /* value returned by declare_vtab() */ + Fts3auxTable *p; /* Virtual table object to return */ + + UNUSED_PARAMETER(pUnused); + + /* The user should invoke this in one of two forms: + ** + ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table); + ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table); + */ + if( argc!=4 && argc!=5 ) goto bad_args; + + zDb = argv[1]; + nDb = (int)strlen(zDb); + if( argc==5 ){ + if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){ + zDb = argv[3]; + nDb = (int)strlen(zDb); + zFts3 = argv[4]; + }else{ + goto bad_args; + } + }else{ + zFts3 = argv[3]; + } + nFts3 = (int)strlen(zFts3); + + rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA); + if( rc!=SQLITE_OK ) return rc; + + nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2; + p = (Fts3auxTable *)sqlite3_malloc(nByte); + if( !p ) return SQLITE_NOMEM; + memset(p, 0, nByte); + + p->pFts3Tab = (Fts3Table *)&p[1]; + p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1]; + p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1]; + p->pFts3Tab->db = db; + p->pFts3Tab->nIndex = 1; + + memcpy((char *)p->pFts3Tab->zDb, zDb, nDb); + memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3); + sqlite3Fts3Dequote((char *)p->pFts3Tab->zName); + + *ppVtab = (sqlite3_vtab *)p; + return SQLITE_OK; + + bad_args: + sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor"); + return SQLITE_ERROR; +} + +/* +** This function does the work for both the xDisconnect and xDestroy methods. +** These tables have no persistent representation of their own, so xDisconnect +** and xDestroy are identical operations. +*/ +static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){ + Fts3auxTable *p = (Fts3auxTable *)pVtab; + Fts3Table *pFts3 = p->pFts3Tab; + int i; + + /* Free any prepared statements held */ + for(i=0; iaStmt); i++){ + sqlite3_finalize(pFts3->aStmt[i]); + } + sqlite3_free(pFts3->zSegmentsTbl); + sqlite3_free(p); + return SQLITE_OK; +} + +#define FTS4AUX_EQ_CONSTRAINT 1 +#define FTS4AUX_GE_CONSTRAINT 2 +#define FTS4AUX_LE_CONSTRAINT 4 + +/* +** xBestIndex - Analyze a WHERE and ORDER BY clause. +*/ +static int fts3auxBestIndexMethod( + sqlite3_vtab *pVTab, + sqlite3_index_info *pInfo +){ + int i; + int iEq = -1; + int iGe = -1; + int iLe = -1; + int iLangid = -1; + int iNext = 1; /* Next free argvIndex value */ + + UNUSED_PARAMETER(pVTab); + + /* This vtab delivers always results in "ORDER BY term ASC" order. */ + if( pInfo->nOrderBy==1 + && pInfo->aOrderBy[0].iColumn==0 + && pInfo->aOrderBy[0].desc==0 + ){ + pInfo->orderByConsumed = 1; + } + + /* Search for equality and range constraints on the "term" column. + ** And equality constraints on the hidden "languageid" column. */ + for(i=0; inConstraint; i++){ + if( pInfo->aConstraint[i].usable ){ + int op = pInfo->aConstraint[i].op; + int iCol = pInfo->aConstraint[i].iColumn; + + if( iCol==0 ){ + if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i; + if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i; + if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i; + if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i; + if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i; + } + if( iCol==4 ){ + if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i; + } + } + } + + if( iEq>=0 ){ + pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT; + pInfo->aConstraintUsage[iEq].argvIndex = iNext++; + pInfo->estimatedCost = 5; + }else{ + pInfo->idxNum = 0; + pInfo->estimatedCost = 20000; + if( iGe>=0 ){ + pInfo->idxNum += FTS4AUX_GE_CONSTRAINT; + pInfo->aConstraintUsage[iGe].argvIndex = iNext++; + pInfo->estimatedCost /= 2; + } + if( iLe>=0 ){ + pInfo->idxNum += FTS4AUX_LE_CONSTRAINT; + pInfo->aConstraintUsage[iLe].argvIndex = iNext++; + pInfo->estimatedCost /= 2; + } + } + if( iLangid>=0 ){ + pInfo->aConstraintUsage[iLangid].argvIndex = iNext++; + pInfo->estimatedCost--; + } + + return SQLITE_OK; +} + +/* +** xOpen - Open a cursor. +*/ +static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ + Fts3auxCursor *pCsr; /* Pointer to cursor object to return */ + + UNUSED_PARAMETER(pVTab); + + pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor)); + if( !pCsr ) return SQLITE_NOMEM; + memset(pCsr, 0, sizeof(Fts3auxCursor)); + + *ppCsr = (sqlite3_vtab_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** xClose - Close a cursor. +*/ +static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){ + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + + sqlite3Fts3SegmentsClose(pFts3); + sqlite3Fts3SegReaderFinish(&pCsr->csr); + sqlite3_free((void *)pCsr->filter.zTerm); + sqlite3_free(pCsr->zStop); + sqlite3_free(pCsr->aStat); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){ + if( nSize>pCsr->nStat ){ + struct Fts3auxColstats *aNew; + aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, + sizeof(struct Fts3auxColstats) * nSize + ); + if( aNew==0 ) return SQLITE_NOMEM; + memset(&aNew[pCsr->nStat], 0, + sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat) + ); + pCsr->aStat = aNew; + pCsr->nStat = nSize; + } + return SQLITE_OK; +} + +/* +** xNext - Advance the cursor to the next row, if any. +*/ +static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + int rc; + + /* Increment our pretend rowid value. */ + pCsr->iRowid++; + + for(pCsr->iCol++; pCsr->iColnStat; pCsr->iCol++){ + if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK; + } + + rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr); + if( rc==SQLITE_ROW ){ + int i = 0; + int nDoclist = pCsr->csr.nDoclist; + char *aDoclist = pCsr->csr.aDoclist; + int iCol; + + int eState = 0; + + if( pCsr->zStop ){ + int n = (pCsr->nStopcsr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm; + int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n); + if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){ + pCsr->isEof = 1; + return SQLITE_OK; + } + } + + if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM; + memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat); + iCol = 0; + + while( iaStat[0].nDoc++; + eState = 1; + iCol = 0; + break; + + /* State 1. In this state we are expecting either a 1, indicating + ** that the following integer will be a column number, or the + ** start of a position list for column 0. + ** + ** The only difference between state 1 and state 2 is that if the + ** integer encountered in state 1 is not 0 or 1, then we need to + ** increment the column 0 "nDoc" count for this term. + */ + case 1: + assert( iCol==0 ); + if( v>1 ){ + pCsr->aStat[1].nDoc++; + } + eState = 2; + /* fall through */ + + case 2: + if( v==0 ){ /* 0x00. Next integer will be a docid. */ + eState = 0; + }else if( v==1 ){ /* 0x01. Next integer will be a column number. */ + eState = 3; + }else{ /* 2 or greater. A position. */ + pCsr->aStat[iCol+1].nOcc++; + pCsr->aStat[0].nOcc++; + } + break; + + /* State 3. The integer just read is a column number. */ + default: assert( eState==3 ); + iCol = (int)v; + if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM; + pCsr->aStat[iCol+1].nDoc++; + eState = 2; + break; + } + } + + pCsr->iCol = 0; + rc = SQLITE_OK; + }else{ + pCsr->isEof = 1; + } + return rc; +} + +/* +** xFilter - Initialize a cursor to point at the start of its data. +*/ +static int fts3auxFilterMethod( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, /* Strategy index */ + const char *idxStr, /* Unused */ + int nVal, /* Number of elements in apVal */ + sqlite3_value **apVal /* Arguments for the indexing scheme */ +){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + int rc; + int isScan = 0; + int iLangVal = 0; /* Language id to query */ + + int iEq = -1; /* Index of term=? value in apVal */ + int iGe = -1; /* Index of term>=? value in apVal */ + int iLe = -1; /* Index of term<=? value in apVal */ + int iLangid = -1; /* Index of languageid=? value in apVal */ + int iNext = 0; + + UNUSED_PARAMETER(nVal); + UNUSED_PARAMETER(idxStr); + + assert( idxStr==0 ); + assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0 + || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT + || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) + ); + + if( idxNum==FTS4AUX_EQ_CONSTRAINT ){ + iEq = iNext++; + }else{ + isScan = 1; + if( idxNum & FTS4AUX_GE_CONSTRAINT ){ + iGe = iNext++; + } + if( idxNum & FTS4AUX_LE_CONSTRAINT ){ + iLe = iNext++; + } + } + if( iNextfilter.zTerm); + sqlite3Fts3SegReaderFinish(&pCsr->csr); + sqlite3_free((void *)pCsr->filter.zTerm); + sqlite3_free(pCsr->aStat); + memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr); + + pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; + if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN; + + if( iEq>=0 || iGe>=0 ){ + const unsigned char *zStr = sqlite3_value_text(apVal[0]); + assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) ); + if( zStr ){ + pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr); + pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]); + if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; + } + } + + if( iLe>=0 ){ + pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe])); + pCsr->nStop = sqlite3_value_bytes(apVal[iLe]); + if( pCsr->zStop==0 ) return SQLITE_NOMEM; + } + + if( iLangid>=0 ){ + iLangVal = sqlite3_value_int(apVal[iLangid]); + + /* If the user specified a negative value for the languageid, use zero + ** instead. This works, as the "languageid=?" constraint will also + ** be tested by the VDBE layer. The test will always be false (since + ** this module will not return a row with a negative languageid), and + ** so the overall query will return zero rows. */ + if( iLangVal<0 ) iLangVal = 0; + } + pCsr->iLangid = iLangVal; + + rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL, + pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr + ); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter); + } + + if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor); + return rc; +} + +/* +** xEof - Return true if the cursor is at EOF, or false otherwise. +*/ +static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + return pCsr->isEof; +} + +/* +** xColumn - Return a column value. +*/ +static int fts3auxColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + Fts3auxCursor *p = (Fts3auxCursor *)pCursor; + + assert( p->isEof==0 ); + switch( iCol ){ + case 0: /* term */ + sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT); + break; + + case 1: /* col */ + if( p->iCol ){ + sqlite3_result_int(pCtx, p->iCol-1); + }else{ + sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC); + } + break; + + case 2: /* documents */ + sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc); + break; + + case 3: /* occurrences */ + sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc); + break; + + default: /* languageid */ + assert( iCol==4 ); + sqlite3_result_int(pCtx, p->iLangid); + break; + } + + return SQLITE_OK; +} + +/* +** xRowid - Return the current rowid for the cursor. +*/ +static int fts3auxRowidMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite_int64 *pRowid /* OUT: Rowid value */ +){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + *pRowid = pCsr->iRowid; + return SQLITE_OK; +} + +/* +** Register the fts3aux module with database connection db. Return SQLITE_OK +** if successful or an error code if sqlite3_create_module() fails. +*/ +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){ + static const sqlite3_module fts3aux_module = { + 0, /* iVersion */ + fts3auxConnectMethod, /* xCreate */ + fts3auxConnectMethod, /* xConnect */ + fts3auxBestIndexMethod, /* xBestIndex */ + fts3auxDisconnectMethod, /* xDisconnect */ + fts3auxDisconnectMethod, /* xDestroy */ + fts3auxOpenMethod, /* xOpen */ + fts3auxCloseMethod, /* xClose */ + fts3auxFilterMethod, /* xFilter */ + fts3auxNextMethod, /* xNext */ + fts3auxEofMethod, /* xEof */ + fts3auxColumnMethod, /* xColumn */ + fts3auxRowidMethod, /* xRowid */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindFunction */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0 /* xRollbackTo */ + }; + int rc; /* Return code */ + + rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0); + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_aux.c ********************************************/ +/************** Begin file fts3_expr.c ***************************************/ +/* +** 2008 Nov 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This module contains code that implements a parser for fts3 query strings +** (the right-hand argument to the MATCH operator). Because the supported +** syntax is relatively simple, the whole tokenizer/parser system is +** hand-coded. +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* +** By default, this module parses the legacy syntax that has been +** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS +** is defined, then it uses the new syntax. The differences between +** the new and the old syntaxes are: +** +** a) The new syntax supports parenthesis. The old does not. +** +** b) The new syntax supports the AND and NOT operators. The old does not. +** +** c) The old syntax supports the "-" token qualifier. This is not +** supported by the new syntax (it is replaced by the NOT operator). +** +** d) When using the old syntax, the OR operator has a greater precedence +** than an implicit AND. When using the new, both implicity and explicit +** AND operators have a higher precedence than OR. +** +** If compiled with SQLITE_TEST defined, then this module exports the +** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable +** to zero causes the module to use the old syntax. If it is set to +** non-zero the new syntax is activated. This is so both syntaxes can +** be tested using a single build of testfixture. +** +** The following describes the syntax supported by the fts3 MATCH +** operator in a similar format to that used by the lemon parser +** generator. This module does not use actually lemon, it uses a +** custom parser. +** +** query ::= andexpr (OR andexpr)*. +** +** andexpr ::= notexpr (AND? notexpr)*. +** +** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*. +** notexpr ::= LP query RP. +** +** nearexpr ::= phrase (NEAR distance_opt nearexpr)*. +** +** distance_opt ::= . +** distance_opt ::= / INTEGER. +** +** phrase ::= TOKEN. +** phrase ::= COLUMN:TOKEN. +** phrase ::= "TOKEN TOKEN TOKEN...". +*/ + +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_fts3_enable_parentheses = 0; +#else +# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS +# define sqlite3_fts3_enable_parentheses 1 +# else +# define sqlite3_fts3_enable_parentheses 0 +# endif +#endif + +/* +** Default span for NEAR operators. +*/ +#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 + +/* #include */ +/* #include */ + +/* +** isNot: +** This variable is used by function getNextNode(). When getNextNode() is +** called, it sets ParseContext.isNot to true if the 'next node' is a +** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the +** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to +** zero. +*/ +typedef struct ParseContext ParseContext; +struct ParseContext { + sqlite3_tokenizer *pTokenizer; /* Tokenizer module */ + int iLangid; /* Language id used with tokenizer */ + const char **azCol; /* Array of column names for fts3 table */ + int bFts4; /* True to allow FTS4-only syntax */ + int nCol; /* Number of entries in azCol[] */ + int iDefaultCol; /* Default column to query */ + int isNot; /* True if getNextNode() sees a unary - */ + sqlite3_context *pCtx; /* Write error message here */ + int nNest; /* Number of nested brackets */ +}; + +/* +** This function is equivalent to the standard isspace() function. +** +** The standard isspace() can be awkward to use safely, because although it +** is defined to accept an argument of type int, its behavior when passed +** an integer that falls outside of the range of the unsigned char type +** is undefined (and sometimes, "undefined" means segfault). This wrapper +** is defined to accept an argument of type char, and always returns 0 for +** any values that fall outside of the range of the unsigned char type (i.e. +** negative values). +*/ +static int fts3isspace(char c){ + return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; +} + +/* +** Allocate nByte bytes of memory using sqlite3_malloc(). If successful, +** zero the memory before returning a pointer to it. If unsuccessful, +** return NULL. +*/ +static void *fts3MallocZero(int nByte){ + void *pRet = sqlite3_malloc(nByte); + if( pRet ) memset(pRet, 0, nByte); + return pRet; +} + +SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer( + sqlite3_tokenizer *pTokenizer, + int iLangid, + const char *z, + int n, + sqlite3_tokenizer_cursor **ppCsr +){ + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + sqlite3_tokenizer_cursor *pCsr = 0; + int rc; + + rc = pModule->xOpen(pTokenizer, z, n, &pCsr); + assert( rc==SQLITE_OK || pCsr==0 ); + if( rc==SQLITE_OK ){ + pCsr->pTokenizer = pTokenizer; + if( pModule->iVersion>=1 ){ + rc = pModule->xLanguageid(pCsr, iLangid); + if( rc!=SQLITE_OK ){ + pModule->xClose(pCsr); + pCsr = 0; + } + } + } + *ppCsr = pCsr; + return rc; +} + +/* +** Function getNextNode(), which is called by fts3ExprParse(), may itself +** call fts3ExprParse(). So this forward declaration is required. +*/ +static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *); + +/* +** Extract the next token from buffer z (length n) using the tokenizer +** and other information (column names etc.) in pParse. Create an Fts3Expr +** structure of type FTSQUERY_PHRASE containing a phrase consisting of this +** single token and set *ppExpr to point to it. If the end of the buffer is +** reached before a token is found, set *ppExpr to zero. It is the +** responsibility of the caller to eventually deallocate the allocated +** Fts3Expr structure (if any) by passing it to sqlite3_free(). +** +** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation +** fails. +*/ +static int getNextToken( + ParseContext *pParse, /* fts3 query parse context */ + int iCol, /* Value for Fts3Phrase.iColumn */ + const char *z, int n, /* Input string */ + Fts3Expr **ppExpr, /* OUT: expression */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + int rc; + sqlite3_tokenizer_cursor *pCursor; + Fts3Expr *pRet = 0; + int i = 0; + + /* Set variable i to the maximum number of bytes of input to tokenize. */ + for(i=0; iiLangid, z, i, &pCursor); + if( rc==SQLITE_OK ){ + const char *zToken; + int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0; + int nByte; /* total space to allocate */ + + rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition); + if( rc==SQLITE_OK ){ + nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken; + pRet = (Fts3Expr *)fts3MallocZero(nByte); + if( !pRet ){ + rc = SQLITE_NOMEM; + }else{ + pRet->eType = FTSQUERY_PHRASE; + pRet->pPhrase = (Fts3Phrase *)&pRet[1]; + pRet->pPhrase->nToken = 1; + pRet->pPhrase->iColumn = iCol; + pRet->pPhrase->aToken[0].n = nToken; + pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1]; + memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken); + + if( iEndpPhrase->aToken[0].isPrefix = 1; + iEnd++; + } + + while( 1 ){ + if( !sqlite3_fts3_enable_parentheses + && iStart>0 && z[iStart-1]=='-' + ){ + pParse->isNot = 1; + iStart--; + }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){ + pRet->pPhrase->aToken[0].bFirst = 1; + iStart--; + }else{ + break; + } + } + + } + *pnConsumed = iEnd; + }else if( i && rc==SQLITE_DONE ){ + rc = SQLITE_OK; + } + + pModule->xClose(pCursor); + } + + *ppExpr = pRet; + return rc; +} + + +/* +** Enlarge a memory allocation. If an out-of-memory allocation occurs, +** then free the old allocation. +*/ +static void *fts3ReallocOrFree(void *pOrig, int nNew){ + void *pRet = sqlite3_realloc(pOrig, nNew); + if( !pRet ){ + sqlite3_free(pOrig); + } + return pRet; +} + +/* +** Buffer zInput, length nInput, contains the contents of a quoted string +** that appeared as part of an fts3 query expression. Neither quote character +** is included in the buffer. This function attempts to tokenize the entire +** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE +** containing the results. +** +** If successful, SQLITE_OK is returned and *ppExpr set to point at the +** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory +** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set +** to 0. +*/ +static int getNextString( + ParseContext *pParse, /* fts3 query parse context */ + const char *zInput, int nInput, /* Input string */ + Fts3Expr **ppExpr /* OUT: expression */ +){ + sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + int rc; + Fts3Expr *p = 0; + sqlite3_tokenizer_cursor *pCursor = 0; + char *zTemp = 0; + int nTemp = 0; + + const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase); + int nToken = 0; + + /* The final Fts3Expr data structure, including the Fts3Phrase, + ** Fts3PhraseToken structures token buffers are all stored as a single + ** allocation so that the expression can be freed with a single call to + ** sqlite3_free(). Setting this up requires a two pass approach. + ** + ** The first pass, in the block below, uses a tokenizer cursor to iterate + ** through the tokens in the expression. This pass uses fts3ReallocOrFree() + ** to assemble data in two dynamic buffers: + ** + ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase + ** structure, followed by the array of Fts3PhraseToken + ** structures. This pass only populates the Fts3PhraseToken array. + ** + ** Buffer zTemp: Contains copies of all tokens. + ** + ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below, + ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase + ** structures. + */ + rc = sqlite3Fts3OpenTokenizer( + pTokenizer, pParse->iLangid, zInput, nInput, &pCursor); + if( rc==SQLITE_OK ){ + int ii; + for(ii=0; rc==SQLITE_OK; ii++){ + const char *zByte; + int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0; + rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos); + if( rc==SQLITE_OK ){ + Fts3PhraseToken *pToken; + + p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken)); + if( !p ) goto no_mem; + + zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte); + if( !zTemp ) goto no_mem; + + assert( nToken==ii ); + pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii]; + memset(pToken, 0, sizeof(Fts3PhraseToken)); + + memcpy(&zTemp[nTemp], zByte, nByte); + nTemp += nByte; + + pToken->n = nByte; + pToken->isPrefix = (iEndbFirst = (iBegin>0 && zInput[iBegin-1]=='^'); + nToken = ii+1; + } + } + + pModule->xClose(pCursor); + pCursor = 0; + } + + if( rc==SQLITE_DONE ){ + int jj; + char *zBuf = 0; + + p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp); + if( !p ) goto no_mem; + memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p); + p->eType = FTSQUERY_PHRASE; + p->pPhrase = (Fts3Phrase *)&p[1]; + p->pPhrase->iColumn = pParse->iDefaultCol; + p->pPhrase->nToken = nToken; + + zBuf = (char *)&p->pPhrase->aToken[nToken]; + if( zTemp ){ + memcpy(zBuf, zTemp, nTemp); + sqlite3_free(zTemp); + }else{ + assert( nTemp==0 ); + } + + for(jj=0; jjpPhrase->nToken; jj++){ + p->pPhrase->aToken[jj].z = zBuf; + zBuf += p->pPhrase->aToken[jj].n; + } + rc = SQLITE_OK; + } + + *ppExpr = p; + return rc; +no_mem: + + if( pCursor ){ + pModule->xClose(pCursor); + } + sqlite3_free(zTemp); + sqlite3_free(p); + *ppExpr = 0; + return SQLITE_NOMEM; +} + +/* +** The output variable *ppExpr is populated with an allocated Fts3Expr +** structure, or set to 0 if the end of the input buffer is reached. +** +** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM +** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered. +** If SQLITE_ERROR is returned, pContext is populated with an error message. +*/ +static int getNextNode( + ParseContext *pParse, /* fts3 query parse context */ + const char *z, int n, /* Input string */ + Fts3Expr **ppExpr, /* OUT: expression */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + static const struct Fts3Keyword { + char *z; /* Keyword text */ + unsigned char n; /* Length of the keyword */ + unsigned char parenOnly; /* Only valid in paren mode */ + unsigned char eType; /* Keyword code */ + } aKeyword[] = { + { "OR" , 2, 0, FTSQUERY_OR }, + { "AND", 3, 1, FTSQUERY_AND }, + { "NOT", 3, 1, FTSQUERY_NOT }, + { "NEAR", 4, 0, FTSQUERY_NEAR } + }; + int ii; + int iCol; + int iColLen; + int rc; + Fts3Expr *pRet = 0; + + const char *zInput = z; + int nInput = n; + + pParse->isNot = 0; + + /* Skip over any whitespace before checking for a keyword, an open or + ** close bracket, or a quoted string. + */ + while( nInput>0 && fts3isspace(*zInput) ){ + nInput--; + zInput++; + } + if( nInput==0 ){ + return SQLITE_DONE; + } + + /* See if we are dealing with a keyword. */ + for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){ + const struct Fts3Keyword *pKey = &aKeyword[ii]; + + if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){ + continue; + } + + if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){ + int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM; + int nKey = pKey->n; + char cNext; + + /* If this is a "NEAR" keyword, check for an explicit nearness. */ + if( pKey->eType==FTSQUERY_NEAR ){ + assert( nKey==4 ); + if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){ + nNear = 0; + for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){ + nNear = nNear * 10 + (zInput[nKey] - '0'); + } + } + } + + /* At this point this is probably a keyword. But for that to be true, + ** the next byte must contain either whitespace, an open or close + ** parenthesis, a quote character, or EOF. + */ + cNext = zInput[nKey]; + if( fts3isspace(cNext) + || cNext=='"' || cNext=='(' || cNext==')' || cNext==0 + ){ + pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr)); + if( !pRet ){ + return SQLITE_NOMEM; + } + pRet->eType = pKey->eType; + pRet->nNear = nNear; + *ppExpr = pRet; + *pnConsumed = (int)((zInput - z) + nKey); + return SQLITE_OK; + } + + /* Turns out that wasn't a keyword after all. This happens if the + ** user has supplied a token such as "ORacle". Continue. + */ + } + } + + /* See if we are dealing with a quoted phrase. If this is the case, then + ** search for the closing quote and pass the whole string to getNextString() + ** for processing. This is easy to do, as fts3 has no syntax for escaping + ** a quote character embedded in a string. + */ + if( *zInput=='"' ){ + for(ii=1; iinNest++; + rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed); + if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; } + *pnConsumed = (int)(zInput - z) + 1 + nConsumed; + return rc; + }else if( *zInput==')' ){ + pParse->nNest--; + *pnConsumed = (int)((zInput - z) + 1); + *ppExpr = 0; + return SQLITE_DONE; + } + } + + /* If control flows to this point, this must be a regular token, or + ** the end of the input. Read a regular token using the sqlite3_tokenizer + ** interface. Before doing so, figure out if there is an explicit + ** column specifier for the token. + ** + ** TODO: Strangely, it is not possible to associate a column specifier + ** with a quoted phrase, only with a single token. Not sure if this was + ** an implementation artifact or an intentional decision when fts3 was + ** first implemented. Whichever it was, this module duplicates the + ** limitation. + */ + iCol = pParse->iDefaultCol; + iColLen = 0; + for(ii=0; iinCol; ii++){ + const char *zStr = pParse->azCol[ii]; + int nStr = (int)strlen(zStr); + if( nInput>nStr && zInput[nStr]==':' + && sqlite3_strnicmp(zStr, zInput, nStr)==0 + ){ + iCol = ii; + iColLen = (int)((zInput - z) + nStr + 1); + break; + } + } + rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed); + *pnConsumed += iColLen; + return rc; +} + +/* +** The argument is an Fts3Expr structure for a binary operator (any type +** except an FTSQUERY_PHRASE). Return an integer value representing the +** precedence of the operator. Lower values have a higher precedence (i.e. +** group more tightly). For example, in the C language, the == operator +** groups more tightly than ||, and would therefore have a higher precedence. +** +** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS +** is defined), the order of the operators in precedence from highest to +** lowest is: +** +** NEAR +** NOT +** AND (including implicit ANDs) +** OR +** +** Note that when using the old query syntax, the OR operator has a higher +** precedence than the AND operator. +*/ +static int opPrecedence(Fts3Expr *p){ + assert( p->eType!=FTSQUERY_PHRASE ); + if( sqlite3_fts3_enable_parentheses ){ + return p->eType; + }else if( p->eType==FTSQUERY_NEAR ){ + return 1; + }else if( p->eType==FTSQUERY_OR ){ + return 2; + } + assert( p->eType==FTSQUERY_AND ); + return 3; +} + +/* +** Argument ppHead contains a pointer to the current head of a query +** expression tree being parsed. pPrev is the expression node most recently +** inserted into the tree. This function adds pNew, which is always a binary +** operator node, into the expression tree based on the relative precedence +** of pNew and the existing nodes of the tree. This may result in the head +** of the tree changing, in which case *ppHead is set to the new root node. +*/ +static void insertBinaryOperator( + Fts3Expr **ppHead, /* Pointer to the root node of a tree */ + Fts3Expr *pPrev, /* Node most recently inserted into the tree */ + Fts3Expr *pNew /* New binary node to insert into expression tree */ +){ + Fts3Expr *pSplit = pPrev; + while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){ + pSplit = pSplit->pParent; + } + + if( pSplit->pParent ){ + assert( pSplit->pParent->pRight==pSplit ); + pSplit->pParent->pRight = pNew; + pNew->pParent = pSplit->pParent; + }else{ + *ppHead = pNew; + } + pNew->pLeft = pSplit; + pSplit->pParent = pNew; +} + +/* +** Parse the fts3 query expression found in buffer z, length n. This function +** returns either when the end of the buffer is reached or an unmatched +** closing bracket - ')' - is encountered. +** +** If successful, SQLITE_OK is returned, *ppExpr is set to point to the +** parsed form of the expression and *pnConsumed is set to the number of +** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM +** (out of memory error) or SQLITE_ERROR (parse error) is returned. +*/ +static int fts3ExprParse( + ParseContext *pParse, /* fts3 query parse context */ + const char *z, int n, /* Text of MATCH query */ + Fts3Expr **ppExpr, /* OUT: Parsed query structure */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + Fts3Expr *pRet = 0; + Fts3Expr *pPrev = 0; + Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */ + int nIn = n; + const char *zIn = z; + int rc = SQLITE_OK; + int isRequirePhrase = 1; + + while( rc==SQLITE_OK ){ + Fts3Expr *p = 0; + int nByte = 0; + + rc = getNextNode(pParse, zIn, nIn, &p, &nByte); + assert( nByte>0 || (rc!=SQLITE_OK && p==0) ); + if( rc==SQLITE_OK ){ + if( p ){ + int isPhrase; + + if( !sqlite3_fts3_enable_parentheses + && p->eType==FTSQUERY_PHRASE && pParse->isNot + ){ + /* Create an implicit NOT operator. */ + Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr)); + if( !pNot ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_NOMEM; + goto exprparse_out; + } + pNot->eType = FTSQUERY_NOT; + pNot->pRight = p; + p->pParent = pNot; + if( pNotBranch ){ + pNot->pLeft = pNotBranch; + pNotBranch->pParent = pNot; + } + pNotBranch = pNot; + p = pPrev; + }else{ + int eType = p->eType; + isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft); + + /* The isRequirePhrase variable is set to true if a phrase or + ** an expression contained in parenthesis is required. If a + ** binary operator (AND, OR, NOT or NEAR) is encounted when + ** isRequirePhrase is set, this is a syntax error. + */ + if( !isPhrase && isRequirePhrase ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_ERROR; + goto exprparse_out; + } + + if( isPhrase && !isRequirePhrase ){ + /* Insert an implicit AND operator. */ + Fts3Expr *pAnd; + assert( pRet && pPrev ); + pAnd = fts3MallocZero(sizeof(Fts3Expr)); + if( !pAnd ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_NOMEM; + goto exprparse_out; + } + pAnd->eType = FTSQUERY_AND; + insertBinaryOperator(&pRet, pPrev, pAnd); + pPrev = pAnd; + } + + /* This test catches attempts to make either operand of a NEAR + ** operator something other than a phrase. For example, either of + ** the following: + ** + ** (bracketed expression) NEAR phrase + ** phrase NEAR (bracketed expression) + ** + ** Return an error in either case. + */ + if( pPrev && ( + (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE) + || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR) + )){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_ERROR; + goto exprparse_out; + } + + if( isPhrase ){ + if( pRet ){ + assert( pPrev && pPrev->pLeft && pPrev->pRight==0 ); + pPrev->pRight = p; + p->pParent = pPrev; + }else{ + pRet = p; + } + }else{ + insertBinaryOperator(&pRet, pPrev, p); + } + isRequirePhrase = !isPhrase; + } + pPrev = p; + } + assert( nByte>0 ); + } + assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) ); + nIn -= nByte; + zIn += nByte; + } + + if( rc==SQLITE_DONE && pRet && isRequirePhrase ){ + rc = SQLITE_ERROR; + } + + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + if( !sqlite3_fts3_enable_parentheses && pNotBranch ){ + if( !pRet ){ + rc = SQLITE_ERROR; + }else{ + Fts3Expr *pIter = pNotBranch; + while( pIter->pLeft ){ + pIter = pIter->pLeft; + } + pIter->pLeft = pRet; + pRet->pParent = pIter; + pRet = pNotBranch; + } + } + } + *pnConsumed = n - nIn; + +exprparse_out: + if( rc!=SQLITE_OK ){ + sqlite3Fts3ExprFree(pRet); + sqlite3Fts3ExprFree(pNotBranch); + pRet = 0; + } + *ppExpr = pRet; + return rc; +} + +/* +** Return SQLITE_ERROR if the maximum depth of the expression tree passed +** as the only argument is more than nMaxDepth. +*/ +static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){ + int rc = SQLITE_OK; + if( p ){ + if( nMaxDepth<0 ){ + rc = SQLITE_TOOBIG; + }else{ + rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1); + if( rc==SQLITE_OK ){ + rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1); + } + } + } + return rc; +} + +/* +** This function attempts to transform the expression tree at (*pp) to +** an equivalent but more balanced form. The tree is modified in place. +** If successful, SQLITE_OK is returned and (*pp) set to point to the +** new root expression node. +** +** nMaxDepth is the maximum allowable depth of the balanced sub-tree. +** +** Otherwise, if an error occurs, an SQLite error code is returned and +** expression (*pp) freed. +*/ +static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){ + int rc = SQLITE_OK; /* Return code */ + Fts3Expr *pRoot = *pp; /* Initial root node */ + Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */ + int eType = pRoot->eType; /* Type of node in this tree */ + + if( nMaxDepth==0 ){ + rc = SQLITE_ERROR; + } + + if( rc==SQLITE_OK ){ + if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){ + Fts3Expr **apLeaf; + apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth); + if( 0==apLeaf ){ + rc = SQLITE_NOMEM; + }else{ + memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth); + } + + if( rc==SQLITE_OK ){ + int i; + Fts3Expr *p; + + /* Set $p to point to the left-most leaf in the tree of eType nodes. */ + for(p=pRoot; p->eType==eType; p=p->pLeft){ + assert( p->pParent==0 || p->pParent->pLeft==p ); + assert( p->pLeft && p->pRight ); + } + + /* This loop runs once for each leaf in the tree of eType nodes. */ + while( 1 ){ + int iLvl; + Fts3Expr *pParent = p->pParent; /* Current parent of p */ + + assert( pParent==0 || pParent->pLeft==p ); + p->pParent = 0; + if( pParent ){ + pParent->pLeft = 0; + }else{ + pRoot = 0; + } + rc = fts3ExprBalance(&p, nMaxDepth-1); + if( rc!=SQLITE_OK ) break; + + for(iLvl=0; p && iLvlpLeft = apLeaf[iLvl]; + pFree->pRight = p; + pFree->pLeft->pParent = pFree; + pFree->pRight->pParent = pFree; + + p = pFree; + pFree = pFree->pParent; + p->pParent = 0; + apLeaf[iLvl] = 0; + } + } + if( p ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_TOOBIG; + break; + } + + /* If that was the last leaf node, break out of the loop */ + if( pParent==0 ) break; + + /* Set $p to point to the next leaf in the tree of eType nodes */ + for(p=pParent->pRight; p->eType==eType; p=p->pLeft); + + /* Remove pParent from the original tree. */ + assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent ); + pParent->pRight->pParent = pParent->pParent; + if( pParent->pParent ){ + pParent->pParent->pLeft = pParent->pRight; + }else{ + assert( pParent==pRoot ); + pRoot = pParent->pRight; + } + + /* Link pParent into the free node list. It will be used as an + ** internal node of the new tree. */ + pParent->pParent = pFree; + pFree = pParent; + } + + if( rc==SQLITE_OK ){ + p = 0; + for(i=0; ipParent = 0; + }else{ + assert( pFree!=0 ); + pFree->pRight = p; + pFree->pLeft = apLeaf[i]; + pFree->pLeft->pParent = pFree; + pFree->pRight->pParent = pFree; + + p = pFree; + pFree = pFree->pParent; + p->pParent = 0; + } + } + } + pRoot = p; + }else{ + /* An error occurred. Delete the contents of the apLeaf[] array + ** and pFree list. Everything else is cleaned up by the call to + ** sqlite3Fts3ExprFree(pRoot) below. */ + Fts3Expr *pDel; + for(i=0; ipParent; + sqlite3_free(pDel); + } + } + + assert( pFree==0 ); + sqlite3_free( apLeaf ); + } + }else if( eType==FTSQUERY_NOT ){ + Fts3Expr *pLeft = pRoot->pLeft; + Fts3Expr *pRight = pRoot->pRight; + + pRoot->pLeft = 0; + pRoot->pRight = 0; + pLeft->pParent = 0; + pRight->pParent = 0; + + rc = fts3ExprBalance(&pLeft, nMaxDepth-1); + if( rc==SQLITE_OK ){ + rc = fts3ExprBalance(&pRight, nMaxDepth-1); + } + + if( rc!=SQLITE_OK ){ + sqlite3Fts3ExprFree(pRight); + sqlite3Fts3ExprFree(pLeft); + }else{ + assert( pLeft && pRight ); + pRoot->pLeft = pLeft; + pLeft->pParent = pRoot; + pRoot->pRight = pRight; + pRight->pParent = pRoot; + } + } + } + + if( rc!=SQLITE_OK ){ + sqlite3Fts3ExprFree(pRoot); + pRoot = 0; + } + *pp = pRoot; + return rc; +} + +/* +** This function is similar to sqlite3Fts3ExprParse(), with the following +** differences: +** +** 1. It does not do expression rebalancing. +** 2. It does not check that the expression does not exceed the +** maximum allowable depth. +** 3. Even if it fails, *ppExpr may still be set to point to an +** expression tree. It should be deleted using sqlite3Fts3ExprFree() +** in this case. +*/ +static int fts3ExprParseUnbalanced( + sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ + int iLangid, /* Language id for tokenizer */ + char **azCol, /* Array of column names for fts3 table */ + int bFts4, /* True to allow FTS4-only syntax */ + int nCol, /* Number of entries in azCol[] */ + int iDefaultCol, /* Default column to query */ + const char *z, int n, /* Text of MATCH query */ + Fts3Expr **ppExpr /* OUT: Parsed query structure */ +){ + int nParsed; + int rc; + ParseContext sParse; + + memset(&sParse, 0, sizeof(ParseContext)); + sParse.pTokenizer = pTokenizer; + sParse.iLangid = iLangid; + sParse.azCol = (const char **)azCol; + sParse.nCol = nCol; + sParse.iDefaultCol = iDefaultCol; + sParse.bFts4 = bFts4; + if( z==0 ){ + *ppExpr = 0; + return SQLITE_OK; + } + if( n<0 ){ + n = (int)strlen(z); + } + rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed); + assert( rc==SQLITE_OK || *ppExpr==0 ); + + /* Check for mismatched parenthesis */ + if( rc==SQLITE_OK && sParse.nNest ){ + rc = SQLITE_ERROR; + } + + return rc; +} + +/* +** Parameters z and n contain a pointer to and length of a buffer containing +** an fts3 query expression, respectively. This function attempts to parse the +** query expression and create a tree of Fts3Expr structures representing the +** parsed expression. If successful, *ppExpr is set to point to the head +** of the parsed expression tree and SQLITE_OK is returned. If an error +** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse +** error) is returned and *ppExpr is set to 0. +** +** If parameter n is a negative number, then z is assumed to point to a +** nul-terminated string and the length is determined using strlen(). +** +** The first parameter, pTokenizer, is passed the fts3 tokenizer module to +** use to normalize query tokens while parsing the expression. The azCol[] +** array, which is assumed to contain nCol entries, should contain the names +** of each column in the target fts3 table, in order from left to right. +** Column names must be nul-terminated strings. +** +** The iDefaultCol parameter should be passed the index of the table column +** that appears on the left-hand-side of the MATCH operator (the default +** column to match against for tokens for which a column name is not explicitly +** specified as part of the query string), or -1 if tokens may by default +** match any table column. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprParse( + sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ + int iLangid, /* Language id for tokenizer */ + char **azCol, /* Array of column names for fts3 table */ + int bFts4, /* True to allow FTS4-only syntax */ + int nCol, /* Number of entries in azCol[] */ + int iDefaultCol, /* Default column to query */ + const char *z, int n, /* Text of MATCH query */ + Fts3Expr **ppExpr, /* OUT: Parsed query structure */ + char **pzErr /* OUT: Error message (sqlite3_malloc) */ +){ + int rc = fts3ExprParseUnbalanced( + pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr + ); + + /* Rebalance the expression. And check that its depth does not exceed + ** SQLITE_FTS3_MAX_EXPR_DEPTH. */ + if( rc==SQLITE_OK && *ppExpr ){ + rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); + if( rc==SQLITE_OK ){ + rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); + } + } + + if( rc!=SQLITE_OK ){ + sqlite3Fts3ExprFree(*ppExpr); + *ppExpr = 0; + if( rc==SQLITE_TOOBIG ){ + sqlite3Fts3ErrMsg(pzErr, + "FTS expression tree is too large (maximum depth %d)", + SQLITE_FTS3_MAX_EXPR_DEPTH + ); + rc = SQLITE_ERROR; + }else if( rc==SQLITE_ERROR ){ + sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z); + } + } + + return rc; +} + +/* +** Free a single node of an expression tree. +*/ +static void fts3FreeExprNode(Fts3Expr *p){ + assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 ); + sqlite3Fts3EvalPhraseCleanup(p->pPhrase); + sqlite3_free(p->aMI); + sqlite3_free(p); +} + +/* +** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse(). +** +** This function would be simpler if it recursively called itself. But +** that would mean passing a sufficiently large expression to ExprParse() +** could cause a stack overflow. +*/ +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){ + Fts3Expr *p; + assert( pDel==0 || pDel->pParent==0 ); + for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){ + assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft ); + } + while( p ){ + Fts3Expr *pParent = p->pParent; + fts3FreeExprNode(p); + if( pParent && p==pParent->pLeft && pParent->pRight ){ + p = pParent->pRight; + while( p && (p->pLeft || p->pRight) ){ + assert( p==p->pParent->pRight || p==p->pParent->pLeft ); + p = (p->pLeft ? p->pLeft : p->pRight); + } + }else{ + p = pParent; + } + } +} + +/**************************************************************************** +***************************************************************************** +** Everything after this point is just test code. +*/ + +#ifdef SQLITE_TEST + +/* #include */ + +/* +** Function to query the hash-table of tokenizers (see README.tokenizers). +*/ +static int queryTestTokenizer( + sqlite3 *db, + const char *zName, + const sqlite3_tokenizer_module **pp +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?)"; + + *pp = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); + } + } + + return sqlite3_finalize(pStmt); +} + +/* +** Return a pointer to a buffer containing a text representation of the +** expression passed as the first argument. The buffer is obtained from +** sqlite3_malloc(). It is the responsibility of the caller to use +** sqlite3_free() to release the memory. If an OOM condition is encountered, +** NULL is returned. +** +** If the second argument is not NULL, then its contents are prepended to +** the returned expression text and then freed using sqlite3_free(). +*/ +static char *exprToString(Fts3Expr *pExpr, char *zBuf){ + if( pExpr==0 ){ + return sqlite3_mprintf(""); + } + switch( pExpr->eType ){ + case FTSQUERY_PHRASE: { + Fts3Phrase *pPhrase = pExpr->pPhrase; + int i; + zBuf = sqlite3_mprintf( + "%zPHRASE %d 0", zBuf, pPhrase->iColumn); + for(i=0; zBuf && inToken; i++){ + zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, + pPhrase->aToken[i].n, pPhrase->aToken[i].z, + (pPhrase->aToken[i].isPrefix?"+":"") + ); + } + return zBuf; + } + + case FTSQUERY_NEAR: + zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear); + break; + case FTSQUERY_NOT: + zBuf = sqlite3_mprintf("%zNOT ", zBuf); + break; + case FTSQUERY_AND: + zBuf = sqlite3_mprintf("%zAND ", zBuf); + break; + case FTSQUERY_OR: + zBuf = sqlite3_mprintf("%zOR ", zBuf); + break; + } + + if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf); + if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf); + + if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf); + + return zBuf; +} + +/* +** This is the implementation of a scalar SQL function used to test the +** expression parser. It should be called as follows: +** +** fts3_exprtest(, , , ...); +** +** The first argument, , is the name of the fts3 tokenizer used +** to parse the query expression (see README.tokenizers). The second argument +** is the query expression to parse. Each subsequent argument is the name +** of a column of the fts3 table that the query expression may refer to. +** For example: +** +** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2'); +*/ +static void fts3ExprTest( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_tokenizer_module const *pModule = 0; + sqlite3_tokenizer *pTokenizer = 0; + int rc; + char **azCol = 0; + const char *zExpr; + int nExpr; + int nCol; + int ii; + Fts3Expr *pExpr; + char *zBuf = 0; + sqlite3 *db = sqlite3_context_db_handle(context); + + if( argc<3 ){ + sqlite3_result_error(context, + "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1 + ); + return; + } + + rc = queryTestTokenizer(db, + (const char *)sqlite3_value_text(argv[0]), &pModule); + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + }else if( !pModule ){ + sqlite3_result_error(context, "No such tokenizer module", -1); + goto exprtest_out; + } + + rc = pModule->xCreate(0, 0, &pTokenizer); + assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + } + pTokenizer->pModule = pModule; + + zExpr = (const char *)sqlite3_value_text(argv[1]); + nExpr = sqlite3_value_bytes(argv[1]); + nCol = argc-2; + azCol = (char **)sqlite3_malloc(nCol*sizeof(char *)); + if( !azCol ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + } + for(ii=0; iixDestroy(pTokenizer); + } + sqlite3_free(azCol); +} + +/* +** Register the query expression parser test function fts3_exprtest() +** with database connection db. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){ + int rc = sqlite3_create_function( + db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0 + ); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", + -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0 + ); + } + return rc; +} + +#endif +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_expr.c *******************************************/ +/************** Begin file fts3_hash.c ***************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the implementation of generic hash-tables used in SQLite. +** We've modified it slightly to serve as a standalone hash table +** implementation for the full-text indexing module. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ +/* #include */ + +/* #include "fts3_hash.h" */ + +/* +** Malloc and Free functions +*/ +static void *fts3HashMalloc(int n){ + void *p = sqlite3_malloc(n); + if( p ){ + memset(p, 0, n); + } + return p; +} +static void fts3HashFree(void *p){ + sqlite3_free(p); +} + +/* Turn bulk memory into a hash table object by initializing the +** fields of the Hash structure. +** +** "pNew" is a pointer to the hash table that is to be initialized. +** keyClass is one of the constants +** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass +** determines what kind of key the hash table will use. "copyKey" is +** true if the hash table should make its own private copy of keys and +** false if it should just use the supplied pointer. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){ + assert( pNew!=0 ); + assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); + pNew->keyClass = keyClass; + pNew->copyKey = copyKey; + pNew->first = 0; + pNew->count = 0; + pNew->htsize = 0; + pNew->ht = 0; +} + +/* Remove all entries from a hash table. Reclaim all memory. +** Call this routine to delete a hash table or to reset a hash table +** to the empty state. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){ + Fts3HashElem *elem; /* For looping over all elements of the table */ + + assert( pH!=0 ); + elem = pH->first; + pH->first = 0; + fts3HashFree(pH->ht); + pH->ht = 0; + pH->htsize = 0; + while( elem ){ + Fts3HashElem *next_elem = elem->next; + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree(elem); + elem = next_elem; + } + pH->count = 0; +} + +/* +** Hash and comparison functions when the mode is FTS3_HASH_STRING +*/ +static int fts3StrHash(const void *pKey, int nKey){ + const char *z = (const char *)pKey; + unsigned h = 0; + if( nKey<=0 ) nKey = (int) strlen(z); + while( nKey > 0 ){ + h = (h<<3) ^ h ^ *z++; + nKey--; + } + return (int)(h & 0x7fffffff); +} +static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return strncmp((const char*)pKey1,(const char*)pKey2,n1); +} + +/* +** Hash and comparison functions when the mode is FTS3_HASH_BINARY +*/ +static int fts3BinHash(const void *pKey, int nKey){ + int h = 0; + const char *z = (const char *)pKey; + while( nKey-- > 0 ){ + h = (h<<3) ^ h ^ *(z++); + } + return h & 0x7fffffff; +} +static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return memcmp(pKey1,pKey2,n1); +} + +/* +** Return a pointer to the appropriate hash function given the key class. +** +** The C syntax in this function definition may be unfamilar to some +** programmers, so we provide the following additional explanation: +** +** The name of the function is "ftsHashFunction". The function takes a +** single parameter "keyClass". The return value of ftsHashFunction() +** is a pointer to another function. Specifically, the return value +** of ftsHashFunction() is a pointer to a function that takes two parameters +** with types "const void*" and "int" and returns an "int". +*/ +static int (*ftsHashFunction(int keyClass))(const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrHash; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinHash; + } +} + +/* +** Return a pointer to the appropriate hash function given the key class. +** +** For help in interpreted the obscure C code in the function definition, +** see the header comment on the previous function. +*/ +static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrCompare; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinCompare; + } +} + +/* Link an element into the hash table +*/ +static void fts3HashInsertElement( + Fts3Hash *pH, /* The complete hash table */ + struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ + Fts3HashElem *pNew /* The element to be inserted */ +){ + Fts3HashElem *pHead; /* First element already in pEntry */ + pHead = pEntry->chain; + if( pHead ){ + pNew->next = pHead; + pNew->prev = pHead->prev; + if( pHead->prev ){ pHead->prev->next = pNew; } + else { pH->first = pNew; } + pHead->prev = pNew; + }else{ + pNew->next = pH->first; + if( pH->first ){ pH->first->prev = pNew; } + pNew->prev = 0; + pH->first = pNew; + } + pEntry->count++; + pEntry->chain = pNew; +} + + +/* Resize the hash table so that it cantains "new_size" buckets. +** "new_size" must be a power of 2. The hash table might fail +** to resize if sqliteMalloc() fails. +** +** Return non-zero if a memory allocation error occurs. +*/ +static int fts3Rehash(Fts3Hash *pH, int new_size){ + struct _fts3ht *new_ht; /* The new hash table */ + Fts3HashElem *elem, *next_elem; /* For looping over existing elements */ + int (*xHash)(const void*,int); /* The hash function */ + + assert( (new_size & (new_size-1))==0 ); + new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); + if( new_ht==0 ) return 1; + fts3HashFree(pH->ht); + pH->ht = new_ht; + pH->htsize = new_size; + xHash = ftsHashFunction(pH->keyClass); + for(elem=pH->first, pH->first=0; elem; elem = next_elem){ + int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); + next_elem = elem->next; + fts3HashInsertElement(pH, &new_ht[h], elem); + } + return 0; +} + +/* This function (for internal use only) locates an element in an +** hash table that matches the given key. The hash for this key has +** already been computed and is passed as the 4th parameter. +*/ +static Fts3HashElem *fts3FindElementByHash( + const Fts3Hash *pH, /* The pH to be searched */ + const void *pKey, /* The key we are searching for */ + int nKey, + int h /* The hash for this key. */ +){ + Fts3HashElem *elem; /* Used to loop thru the element list */ + int count; /* Number of elements left to test */ + int (*xCompare)(const void*,int,const void*,int); /* comparison function */ + + if( pH->ht ){ + struct _fts3ht *pEntry = &pH->ht[h]; + elem = pEntry->chain; + count = pEntry->count; + xCompare = ftsCompareFunction(pH->keyClass); + while( count-- && elem ){ + if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ + return elem; + } + elem = elem->next; + } + } + return 0; +} + +/* Remove a single entry from the hash table given a pointer to that +** element and a hash on the element's key. +*/ +static void fts3RemoveElementByHash( + Fts3Hash *pH, /* The pH containing "elem" */ + Fts3HashElem* elem, /* The element to be removed from the pH */ + int h /* Hash value for the element */ +){ + struct _fts3ht *pEntry; + if( elem->prev ){ + elem->prev->next = elem->next; + }else{ + pH->first = elem->next; + } + if( elem->next ){ + elem->next->prev = elem->prev; + } + pEntry = &pH->ht[h]; + if( pEntry->chain==elem ){ + pEntry->chain = elem->next; + } + pEntry->count--; + if( pEntry->count<=0 ){ + pEntry->chain = 0; + } + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree( elem ); + pH->count--; + if( pH->count<=0 ){ + assert( pH->first==0 ); + assert( pH->count==0 ); + fts3HashClear(pH); + } +} + +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem( + const Fts3Hash *pH, + const void *pKey, + int nKey +){ + int h; /* A hash on key */ + int (*xHash)(const void*,int); /* The hash function */ + + if( pH==0 || pH->ht==0 ) return 0; + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + h = (*xHash)(pKey,nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); +} + +/* +** Attempt to locate an element of the hash table pH with a key +** that matches pKey,nKey. Return the data for this element if it is +** found, or NULL if there is no match. +*/ +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){ + Fts3HashElem *pElem; /* The element that matches key (if any) */ + + pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey); + return pElem ? pElem->data : 0; +} + +/* Insert an element into the hash table pH. The key is pKey,nKey +** and the data is "data". +** +** If no element exists with a matching key, then a new +** element is created. A copy of the key is made if the copyKey +** flag is set. NULL is returned. +** +** If another element already exists with the same key, then the +** new data replaces the old data and the old data is returned. +** The key is not copied in this instance. If a malloc fails, then +** the new data is returned and the hash table is unchanged. +** +** If the "data" parameter to this function is NULL, then the +** element corresponding to "key" is removed from the hash table. +*/ +SQLITE_PRIVATE void *sqlite3Fts3HashInsert( + Fts3Hash *pH, /* The hash table to insert into */ + const void *pKey, /* The key */ + int nKey, /* Number of bytes in the key */ + void *data /* The data */ +){ + int hraw; /* Raw hash value of the key */ + int h; /* the hash of the key modulo hash table size */ + Fts3HashElem *elem; /* Used to loop thru the element list */ + Fts3HashElem *new_elem; /* New element added to the pH */ + int (*xHash)(const void*,int); /* The hash function */ + + assert( pH!=0 ); + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + hraw = (*xHash)(pKey, nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + elem = fts3FindElementByHash(pH,pKey,nKey,h); + if( elem ){ + void *old_data = elem->data; + if( data==0 ){ + fts3RemoveElementByHash(pH,elem,h); + }else{ + elem->data = data; + } + return old_data; + } + if( data==0 ) return 0; + if( (pH->htsize==0 && fts3Rehash(pH,8)) + || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2)) + ){ + pH->count = 0; + return data; + } + assert( pH->htsize>0 ); + new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) ); + if( new_elem==0 ) return data; + if( pH->copyKey && pKey!=0 ){ + new_elem->pKey = fts3HashMalloc( nKey ); + if( new_elem->pKey==0 ){ + fts3HashFree(new_elem); + return data; + } + memcpy((void*)new_elem->pKey, pKey, nKey); + }else{ + new_elem->pKey = (void*)pKey; + } + new_elem->nKey = nKey; + pH->count++; + assert( pH->htsize>0 ); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + fts3HashInsertElement(pH, &pH->ht[h], new_elem); + new_elem->data = data; + return 0; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_hash.c *******************************************/ +/************** Begin file fts3_porter.c *************************************/ +/* +** 2006 September 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Implementation of the full-text-search tokenizer that implements +** a Porter stemmer. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ +/* #include */ +/* #include */ + +/* #include "fts3_tokenizer.h" */ + +/* +** Class derived from sqlite3_tokenizer +*/ +typedef struct porter_tokenizer { + sqlite3_tokenizer base; /* Base class */ +} porter_tokenizer; + +/* +** Class derived from sqlite3_tokenizer_cursor +*/ +typedef struct porter_tokenizer_cursor { + sqlite3_tokenizer_cursor base; + const char *zInput; /* input we are tokenizing */ + int nInput; /* size of the input */ + int iOffset; /* current position in zInput */ + int iToken; /* index of next token to be returned */ + char *zToken; /* storage for current token */ + int nAllocated; /* space allocated to zToken buffer */ +} porter_tokenizer_cursor; + + +/* +** Create a new tokenizer instance. +*/ +static int porterCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer +){ + porter_tokenizer *t; + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + + t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); + *ppTokenizer = &t->base; + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int porterDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is zInput[0..nInput-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int porterOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *zInput, int nInput, /* String to be tokenized */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + porter_tokenizer_cursor *c; + + UNUSED_PARAMETER(pTokenizer); + + c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + + c->zInput = zInput; + if( zInput==0 ){ + c->nInput = 0; + }else if( nInput<0 ){ + c->nInput = (int)strlen(zInput); + }else{ + c->nInput = nInput; + } + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->zToken = NULL; /* no space allocated, yet. */ + c->nAllocated = 0; + + *ppCursor = &c->base; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to +** porterOpen() above. +*/ +static int porterClose(sqlite3_tokenizer_cursor *pCursor){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + sqlite3_free(c->zToken); + sqlite3_free(c); + return SQLITE_OK; +} +/* +** Vowel or consonant +*/ +static const char cType[] = { + 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 2, 1 +}; + +/* +** isConsonant() and isVowel() determine if their first character in +** the string they point to is a consonant or a vowel, according +** to Porter ruls. +** +** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. +** 'Y' is a consonant unless it follows another consonant, +** in which case it is a vowel. +** +** In these routine, the letters are in reverse order. So the 'y' rule +** is that 'y' is a consonant unless it is followed by another +** consonent. +*/ +static int isVowel(const char*); +static int isConsonant(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return j; + return z[1]==0 || isVowel(z + 1); +} +static int isVowel(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return 1-j; + return isConsonant(z + 1); +} + +/* +** Let any sequence of one or more vowels be represented by V and let +** C be sequence of one or more consonants. Then every word can be +** represented as: +** +** [C] (VC){m} [V] +** +** In prose: A word is an optional consonant followed by zero or +** vowel-consonant pairs followed by an optional vowel. "m" is the +** number of vowel consonant pairs. This routine computes the value +** of m for the first i bytes of a word. +** +** Return true if the m-value for z is 1 or more. In other words, +** return true if z contains at least one vowel that is followed +** by a consonant. +** +** In this routine z[] is in reverse order. So we are really looking +** for an instance of a consonant followed by a vowel. +*/ +static int m_gt_0(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* Like mgt0 above except we are looking for a value of m which is +** exactly 1 +*/ +static int m_eq_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 1; + while( isConsonant(z) ){ z++; } + return *z==0; +} + +/* Like mgt0 above except we are looking for a value of m>1 instead +** or m>0 +*/ +static int m_gt_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* +** Return TRUE if there is a vowel anywhere within z[0..n-1] +*/ +static int hasVowel(const char *z){ + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* +** Return TRUE if the word ends in a double consonant. +** +** The text is reversed here. So we are really looking at +** the first two characters of z[]. +*/ +static int doubleConsonant(const char *z){ + return isConsonant(z) && z[0]==z[1]; +} + +/* +** Return TRUE if the word ends with three letters which +** are consonant-vowel-consonent and where the final consonant +** is not 'w', 'x', or 'y'. +** +** The word is reversed here. So we are really checking the +** first three letters and the first one cannot be in [wxy]. +*/ +static int star_oh(const char *z){ + return + isConsonant(z) && + z[0]!='w' && z[0]!='x' && z[0]!='y' && + isVowel(z+1) && + isConsonant(z+2); +} + +/* +** If the word ends with zFrom and xCond() is true for the stem +** of the word that preceeds the zFrom ending, then change the +** ending to zTo. +** +** The input word *pz and zFrom are both in reverse order. zTo +** is in normal order. +** +** Return TRUE if zFrom matches. Return FALSE if zFrom does not +** match. Not that TRUE is returned even if xCond() fails and +** no substitution occurs. +*/ +static int stem( + char **pz, /* The word being stemmed (Reversed) */ + const char *zFrom, /* If the ending matches this... (Reversed) */ + const char *zTo, /* ... change the ending to this (not reversed) */ + int (*xCond)(const char*) /* Condition that must be true */ +){ + char *z = *pz; + while( *zFrom && *zFrom==*z ){ z++; zFrom++; } + if( *zFrom!=0 ) return 0; + if( xCond && !xCond(z) ) return 1; + while( *zTo ){ + *(--z) = *(zTo++); + } + *pz = z; + return 1; +} + +/* +** This is the fallback stemmer used when the porter stemmer is +** inappropriate. The input word is copied into the output with +** US-ASCII case folding. If the input word is too long (more +** than 20 bytes if it contains no digits or more than 6 bytes if +** it contains digits) then word is truncated to 20 or 6 bytes +** by taking 10 or 3 bytes from the beginning and end. +*/ +static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ + int i, mx, j; + int hasDigit = 0; + for(i=0; i='A' && c<='Z' ){ + zOut[i] = c - 'A' + 'a'; + }else{ + if( c>='0' && c<='9' ) hasDigit = 1; + zOut[i] = c; + } + } + mx = hasDigit ? 3 : 10; + if( nIn>mx*2 ){ + for(j=mx, i=nIn-mx; i=(int)sizeof(zReverse)-7 ){ + /* The word is too big or too small for the porter stemmer. + ** Fallback to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ + zReverse[j] = c + 'a' - 'A'; + }else if( c>='a' && c<='z' ){ + zReverse[j] = c; + }else{ + /* The use of a character not in [a-zA-Z] means that we fallback + ** to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + } + memset(&zReverse[sizeof(zReverse)-5], 0, 5); + z = &zReverse[j+1]; + + + /* Step 1a */ + if( z[0]=='s' ){ + if( + !stem(&z, "sess", "ss", 0) && + !stem(&z, "sei", "i", 0) && + !stem(&z, "ss", "ss", 0) + ){ + z++; + } + } + + /* Step 1b */ + z2 = z; + if( stem(&z, "dee", "ee", m_gt_0) ){ + /* Do nothing. The work was all in the test */ + }else if( + (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) + && z!=z2 + ){ + if( stem(&z, "ta", "ate", 0) || + stem(&z, "lb", "ble", 0) || + stem(&z, "zi", "ize", 0) ){ + /* Do nothing. The work was all in the test */ + }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ + z++; + }else if( m_eq_1(z) && star_oh(z) ){ + *(--z) = 'e'; + } + } + + /* Step 1c */ + if( z[0]=='y' && hasVowel(z+1) ){ + z[0] = 'i'; + } + + /* Step 2 */ + switch( z[1] ){ + case 'a': + if( !stem(&z, "lanoita", "ate", m_gt_0) ){ + stem(&z, "lanoit", "tion", m_gt_0); + } + break; + case 'c': + if( !stem(&z, "icne", "ence", m_gt_0) ){ + stem(&z, "icna", "ance", m_gt_0); + } + break; + case 'e': + stem(&z, "rezi", "ize", m_gt_0); + break; + case 'g': + stem(&z, "igol", "log", m_gt_0); + break; + case 'l': + if( !stem(&z, "ilb", "ble", m_gt_0) + && !stem(&z, "illa", "al", m_gt_0) + && !stem(&z, "iltne", "ent", m_gt_0) + && !stem(&z, "ile", "e", m_gt_0) + ){ + stem(&z, "ilsuo", "ous", m_gt_0); + } + break; + case 'o': + if( !stem(&z, "noitazi", "ize", m_gt_0) + && !stem(&z, "noita", "ate", m_gt_0) + ){ + stem(&z, "rota", "ate", m_gt_0); + } + break; + case 's': + if( !stem(&z, "msila", "al", m_gt_0) + && !stem(&z, "ssenevi", "ive", m_gt_0) + && !stem(&z, "ssenluf", "ful", m_gt_0) + ){ + stem(&z, "ssensuo", "ous", m_gt_0); + } + break; + case 't': + if( !stem(&z, "itila", "al", m_gt_0) + && !stem(&z, "itivi", "ive", m_gt_0) + ){ + stem(&z, "itilib", "ble", m_gt_0); + } + break; + } + + /* Step 3 */ + switch( z[0] ){ + case 'e': + if( !stem(&z, "etaci", "ic", m_gt_0) + && !stem(&z, "evita", "", m_gt_0) + ){ + stem(&z, "ezila", "al", m_gt_0); + } + break; + case 'i': + stem(&z, "itici", "ic", m_gt_0); + break; + case 'l': + if( !stem(&z, "laci", "ic", m_gt_0) ){ + stem(&z, "luf", "", m_gt_0); + } + break; + case 's': + stem(&z, "ssen", "", m_gt_0); + break; + } + + /* Step 4 */ + switch( z[1] ){ + case 'a': + if( z[0]=='l' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'c': + if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'e': + if( z[0]=='r' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'i': + if( z[0]=='c' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'l': + if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'n': + if( z[0]=='t' ){ + if( z[2]=='a' ){ + if( m_gt_1(z+3) ){ + z += 3; + } + }else if( z[2]=='e' ){ + if( !stem(&z, "tneme", "", m_gt_1) + && !stem(&z, "tnem", "", m_gt_1) + ){ + stem(&z, "tne", "", m_gt_1); + } + } + } + break; + case 'o': + if( z[0]=='u' ){ + if( m_gt_1(z+2) ){ + z += 2; + } + }else if( z[3]=='s' || z[3]=='t' ){ + stem(&z, "noi", "", m_gt_1); + } + break; + case 's': + if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 't': + if( !stem(&z, "eta", "", m_gt_1) ){ + stem(&z, "iti", "", m_gt_1); + } + break; + case 'u': + if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 'v': + case 'z': + if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + } + + /* Step 5a */ + if( z[0]=='e' ){ + if( m_gt_1(z+1) ){ + z++; + }else if( m_eq_1(z+1) && !star_oh(z+1) ){ + z++; + } + } + + /* Step 5b */ + if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ + z++; + } + + /* z[] is now the stemmed word in reverse order. Flip it back + ** around into forward order and return. + */ + *pnOut = i = (int)strlen(z); + zOut[i] = 0; + while( *z ){ + zOut[--i] = *(z++); + } +} + +/* +** Characters that can be part of a token. We assume any character +** whose value is greater than 0x80 (any UTF character) can be +** part of a token. In other words, delimiters all must have +** values of 0x7f or lower. +*/ +static const char porterIdChar[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ +}; +#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30])) + +/* +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to porterOpen(). +*/ +static int porterNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ + const char **pzToken, /* OUT: *pzToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + const char *z = c->zInput; + + while( c->iOffsetnInput ){ + int iStartOffset, ch; + + /* Scan past delimiter characters */ + while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + if( c->iOffset>iStartOffset ){ + int n = c->iOffset-iStartOffset; + if( n>c->nAllocated ){ + char *pNew; + c->nAllocated = n+20; + pNew = sqlite3_realloc(c->zToken, c->nAllocated); + if( !pNew ) return SQLITE_NOMEM; + c->zToken = pNew; + } + porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); + *pzToken = c->zToken; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; + return SQLITE_OK; + } + } + return SQLITE_DONE; +} + +/* +** The set of routines that implement the porter-stemmer tokenizer +*/ +static const sqlite3_tokenizer_module porterTokenizerModule = { + 0, + porterCreate, + porterDestroy, + porterOpen, + porterClose, + porterNext, + 0 +}; + +/* +** Allocate a new porter tokenizer. Return a pointer to the new +** tokenizer in *ppModule +*/ +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &porterTokenizerModule; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_porter.c *****************************************/ +/************** Begin file fts3_tokenizer.c **********************************/ +/* +** 2007 June 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This is part of an SQLite module implementing full-text search. +** This particular file implements the generic tokenizer interface. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ + +/* +** Return true if the two-argument version of fts3_tokenizer() +** has been activated via a prior call to sqlite3_db_config(db, +** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0); +*/ +static int fts3TokenizerEnabled(sqlite3_context *context){ + sqlite3 *db = sqlite3_context_db_handle(context); + int isEnabled = 0; + sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled); + return isEnabled; +} + +/* +** Implementation of the SQL scalar function for accessing the underlying +** hash table. This function may be called as follows: +** +** SELECT (); +** SELECT (, ); +** +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). +** +** If the argument is specified, it must be a blob value +** containing a pointer to be stored as the hash data corresponding +** to the string . If is not specified, then +** the string must already exist in the has table. Otherwise, +** an error is returned. +** +** Whether or not the argument is specified, the value returned +** is a blob containing the pointer stored as the hash data corresponding +** to string (after the hash-table is updated, if applicable). +*/ +static void fts3TokenizerFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Fts3Hash *pHash; + void *pPtr = 0; + const unsigned char *zName; + int nName; + + assert( argc==1 || argc==2 ); + + pHash = (Fts3Hash *)sqlite3_user_data(context); + + zName = sqlite3_value_text(argv[0]); + nName = sqlite3_value_bytes(argv[0])+1; + + if( argc==2 ){ + if( fts3TokenizerEnabled(context) ){ + void *pOld; + int n = sqlite3_value_bytes(argv[1]); + if( zName==0 || n!=sizeof(pPtr) ){ + sqlite3_result_error(context, "argument type mismatch", -1); + return; + } + pPtr = *(void **)sqlite3_value_blob(argv[1]); + pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); + if( pOld==pPtr ){ + sqlite3_result_error(context, "out of memory", -1); + } + }else{ + sqlite3_result_error(context, "fts3tokenize disabled", -1); + return; + } + }else{ + if( zName ){ + pPtr = sqlite3Fts3HashFind(pHash, zName, nName); + } + if( !pPtr ){ + char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + } + sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); +} + +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){ + static const char isFtsIdChar[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ + }; + return (c&0x80 || isFtsIdChar[(int)(c)]); +} + +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){ + const char *z1; + const char *z2 = 0; + + /* Find the start of the next token. */ + z1 = zStr; + while( z2==0 ){ + char c = *z1; + switch( c ){ + case '\0': return 0; /* No more tokens here */ + case '\'': + case '"': + case '`': { + z2 = z1; + while( *++z2 && (*z2!=c || *++z2==c) ); + break; + } + case '[': + z2 = &z1[1]; + while( *z2 && z2[0]!=']' ) z2++; + if( *z2 ) z2++; + break; + + default: + if( sqlite3Fts3IsIdChar(*z1) ){ + z2 = &z1[1]; + while( sqlite3Fts3IsIdChar(*z2) ) z2++; + }else{ + z1++; + } + } + } + + *pn = (int)(z2-z1); + return z1; +} + +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer( + Fts3Hash *pHash, /* Tokenizer hash table */ + const char *zArg, /* Tokenizer name */ + sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */ + char **pzErr /* OUT: Set to malloced error message */ +){ + int rc; + char *z = (char *)zArg; + int n = 0; + char *zCopy; + char *zEnd; /* Pointer to nul-term of zCopy */ + sqlite3_tokenizer_module *m; + + zCopy = sqlite3_mprintf("%s", zArg); + if( !zCopy ) return SQLITE_NOMEM; + zEnd = &zCopy[strlen(zCopy)]; + + z = (char *)sqlite3Fts3NextToken(zCopy, &n); + if( z==0 ){ + assert( n==0 ); + z = zCopy; + } + z[n] = '\0'; + sqlite3Fts3Dequote(z); + + m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1); + if( !m ){ + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z); + rc = SQLITE_ERROR; + }else{ + char const **aArg = 0; + int iArg = 0; + z = &z[n+1]; + while( zxCreate(iArg, aArg, ppTok); + assert( rc!=SQLITE_OK || *ppTok ); + if( rc!=SQLITE_OK ){ + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer"); + }else{ + (*ppTok)->pModule = m; + } + sqlite3_free((void *)aArg); + } + + sqlite3_free(zCopy); + return rc; +} + + +#ifdef SQLITE_TEST + +#include +/* #include */ + +/* +** Implementation of a special SQL scalar function for testing tokenizers +** designed to be used in concert with the Tcl testing framework. This +** function must be called with two or more arguments: +** +** SELECT (, ..., ); +** +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') +** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). +** +** The return value is a string that may be interpreted as a Tcl +** list. For each token in the , three elements are +** added to the returned list. The first is the token position, the +** second is the token text (folded, stemmed, etc.) and the third is the +** substring of associated with the token. For example, +** using the built-in "simple" tokenizer: +** +** SELECT fts_tokenizer_test('simple', 'I don't see how'); +** +** will return the string: +** +** "{0 i I 1 dont don't 2 see see 3 how how}" +** +*/ +static void testFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Fts3Hash *pHash; + sqlite3_tokenizer_module *p; + sqlite3_tokenizer *pTokenizer = 0; + sqlite3_tokenizer_cursor *pCsr = 0; + + const char *zErr = 0; + + const char *zName; + int nName; + const char *zInput; + int nInput; + + const char *azArg[64]; + + const char *zToken; + int nToken = 0; + int iStart = 0; + int iEnd = 0; + int iPos = 0; + int i; + + Tcl_Obj *pRet; + + if( argc<2 ){ + sqlite3_result_error(context, "insufficient arguments", -1); + return; + } + + nName = sqlite3_value_bytes(argv[0]); + zName = (const char *)sqlite3_value_text(argv[0]); + nInput = sqlite3_value_bytes(argv[argc-1]); + zInput = (const char *)sqlite3_value_text(argv[argc-1]); + + pHash = (Fts3Hash *)sqlite3_user_data(context); + p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); + + if( !p ){ + char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr2, -1); + sqlite3_free(zErr2); + return; + } + + pRet = Tcl_NewObj(); + Tcl_IncrRefCount(pRet); + + for(i=1; ixCreate(argc-2, azArg, &pTokenizer) ){ + zErr = "error in xCreate()"; + goto finish; + } + pTokenizer->pModule = p; + if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){ + zErr = "error in xOpen()"; + goto finish; + } + + while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + zToken = &zInput[iStart]; + nToken = iEnd-iStart; + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + } + + if( SQLITE_OK!=p->xClose(pCsr) ){ + zErr = "error in xClose()"; + goto finish; + } + if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ + zErr = "error in xDestroy()"; + goto finish; + } + +finish: + if( zErr ){ + sqlite3_result_error(context, zErr, -1); + }else{ + sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); + } + Tcl_DecrRefCount(pRet); +} + +static +int registerTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module *p +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; + + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); + sqlite3_step(pStmt); + + return sqlite3_finalize(pStmt); +} + + +static +int queryTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module **pp +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?)"; + + *pp = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); + } + } + + return sqlite3_finalize(pStmt); +} + +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); + +/* +** Implementation of the scalar function fts3_tokenizer_internal_test(). +** This function is used for testing only, it is not included in the +** build unless SQLITE_TEST is defined. +** +** The purpose of this is to test that the fts3_tokenizer() function +** can be used as designed by the C-code in the queryTokenizer and +** registerTokenizer() functions above. These two functions are repeated +** in the README.tokenizer file as an example, so it is important to +** test them. +** +** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar +** function with no arguments. An assert() will fail if a problem is +** detected. i.e.: +** +** SELECT fts3_tokenizer_internal_test(); +** +*/ +static void intTestFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int rc; + const sqlite3_tokenizer_module *p1; + const sqlite3_tokenizer_module *p2; + sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + + /* Test the query function */ + sqlite3Fts3SimpleTokenizerModule(&p1); + rc = queryTokenizer(db, "simple", &p2); + assert( rc==SQLITE_OK ); + assert( p1==p2 ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_ERROR ); + assert( p2==0 ); + assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); + + /* Test the storage function */ + if( fts3TokenizerEnabled(context) ){ + rc = registerTokenizer(db, "nosuchtokenizer", p1); + assert( rc==SQLITE_OK ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_OK ); + assert( p2==p1 ); + } + + sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); +} + +#endif + +/* +** Set up SQL objects in database db used to access the contents of +** the hash table pointed to by argument pHash. The hash table must +** been initialized to use string keys, and to take a private copy +** of the key when a value is inserted. i.e. by a call similar to: +** +** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); +** +** This function adds a scalar function (see header comment above +** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is +** defined at compilation time, a temporary virtual table (see header +** comment above struct HashTableVtab) to the database schema. Both +** provide read/write access to the contents of *pHash. +** +** The third argument to this function, zName, is used as the name +** of both the scalar and, if created, the virtual table. +*/ +SQLITE_PRIVATE int sqlite3Fts3InitHashTable( + sqlite3 *db, + Fts3Hash *pHash, + const char *zName +){ + int rc = SQLITE_OK; + void *p = (void *)pHash; + const int any = SQLITE_ANY; + +#ifdef SQLITE_TEST + char *zTest = 0; + char *zTest2 = 0; + void *pdb = (void *)db; + zTest = sqlite3_mprintf("%s_test", zName); + zTest2 = sqlite3_mprintf("%s_internal_test", zName); + if( !zTest || !zTest2 ){ + rc = SQLITE_NOMEM; + } +#endif + + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0); + } +#ifdef SQLITE_TEST + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); + } +#endif + +#ifdef SQLITE_TEST + sqlite3_free(zTest); + sqlite3_free(zTest2); +#endif + + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_tokenizer.c **************************************/ +/************** Begin file fts3_tokenizer1.c *********************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Implementation of the "simple" full-text-search tokenizer. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ +/* #include */ +/* #include */ + +/* #include "fts3_tokenizer.h" */ + +typedef struct simple_tokenizer { + sqlite3_tokenizer base; + char delim[128]; /* flag ASCII delimiters */ +} simple_tokenizer; + +typedef struct simple_tokenizer_cursor { + sqlite3_tokenizer_cursor base; + const char *pInput; /* input we are tokenizing */ + int nBytes; /* size of the input */ + int iOffset; /* current position in pInput */ + int iToken; /* index of next token to be returned */ + char *pToken; /* storage for current token */ + int nTokenAllocated; /* space allocated to zToken buffer */ +} simple_tokenizer_cursor; + + +static int simpleDelim(simple_tokenizer *t, unsigned char c){ + return c<0x80 && t->delim[c]; +} +static int fts3_isalnum(int x){ + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); +} + +/* +** Create a new tokenizer instance. +*/ +static int simpleCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer +){ + simple_tokenizer *t; + + t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); + + /* TODO(shess) Delimiters need to remain the same from run to run, + ** else we need to reindex. One solution would be a meta-table to + ** track such information in the database, then we'd only want this + ** information on the initial create. + */ + if( argc>1 ){ + int i, n = (int)strlen(argv[1]); + for(i=0; i=0x80 ){ + sqlite3_free(t); + return SQLITE_ERROR; + } + t->delim[ch] = 1; + } + } else { + /* Mark non-alphanumeric ASCII characters as delimiters */ + int i; + for(i=1; i<0x80; i++){ + t->delim[i] = !fts3_isalnum(i) ? -1 : 0; + } + } + + *ppTokenizer = &t->base; + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is pInput[0..nBytes-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int simpleOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *pInput, int nBytes, /* String to be tokenized */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + simple_tokenizer_cursor *c; + + UNUSED_PARAMETER(pTokenizer); + + c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + + c->pInput = pInput; + if( pInput==0 ){ + c->nBytes = 0; + }else if( nBytes<0 ){ + c->nBytes = (int)strlen(pInput); + }else{ + c->nBytes = nBytes; + } + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->pToken = NULL; /* no space allocated, yet. */ + c->nTokenAllocated = 0; + + *ppCursor = &c->base; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to +** simpleOpen() above. +*/ +static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + sqlite3_free(c->pToken); + sqlite3_free(c); + return SQLITE_OK; +} + +/* +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to simpleOpen(). +*/ +static int simpleNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ + const char **ppToken, /* OUT: *ppToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; + unsigned char *p = (unsigned char *)c->pInput; + + while( c->iOffsetnBytes ){ + int iStartOffset; + + /* Scan past delimiter characters */ + while( c->iOffsetnBytes && simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; + } + + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnBytes && !simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; + } + + if( c->iOffset>iStartOffset ){ + int i, n = c->iOffset-iStartOffset; + if( n>c->nTokenAllocated ){ + char *pNew; + c->nTokenAllocated = n+20; + pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated); + if( !pNew ) return SQLITE_NOMEM; + c->pToken = pNew; + } + for(i=0; ipToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch); + } + *ppToken = c->pToken; + *pnBytes = n; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; + + return SQLITE_OK; + } + } + return SQLITE_DONE; +} + +/* +** The set of routines that implement the simple tokenizer +*/ +static const sqlite3_tokenizer_module simpleTokenizerModule = { + 0, + simpleCreate, + simpleDestroy, + simpleOpen, + simpleClose, + simpleNext, + 0, +}; + +/* +** Allocate a new simple tokenizer. Return a pointer to the new +** tokenizer in *ppModule +*/ +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &simpleTokenizerModule; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_tokenizer1.c *************************************/ +/************** Begin file fts3_tokenize_vtab.c ******************************/ +/* +** 2013 Apr 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code for the "fts3tokenize" virtual table module. +** An fts3tokenize virtual table is created as follows: +** +** CREATE VIRTUAL TABLE USING fts3tokenize( +** , , ... +** ); +** +** The table created has the following schema: +** +** CREATE TABLE (input, token, start, end, position) +** +** When queried, the query must include a WHERE clause of type: +** +** input = +** +** The virtual table module tokenizes this , using the FTS3 +** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE +** statement and returns one row for each token in the result. With +** fields set as follows: +** +** input: Always set to a copy of +** token: A token from the input. +** start: Byte offset of the token within the input . +** end: Byte offset of the byte immediately following the end of the +** token within the input string. +** pos: Token offset of token within input. +** +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ + +typedef struct Fts3tokTable Fts3tokTable; +typedef struct Fts3tokCursor Fts3tokCursor; + +/* +** Virtual table structure. +*/ +struct Fts3tokTable { + sqlite3_vtab base; /* Base class used by SQLite core */ + const sqlite3_tokenizer_module *pMod; + sqlite3_tokenizer *pTok; +}; + +/* +** Virtual table cursor structure. +*/ +struct Fts3tokCursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + char *zInput; /* Input string */ + sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */ + int iRowid; /* Current 'rowid' value */ + const char *zToken; /* Current 'token' value */ + int nToken; /* Size of zToken in bytes */ + int iStart; /* Current 'start' value */ + int iEnd; /* Current 'end' value */ + int iPos; /* Current 'pos' value */ +}; + +/* +** Query FTS for the tokenizer implementation named zName. +*/ +static int fts3tokQueryTokenizer( + Fts3Hash *pHash, + const char *zName, + const sqlite3_tokenizer_module **pp, + char **pzErr +){ + sqlite3_tokenizer_module *p; + int nName = (int)strlen(zName); + + p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); + if( !p ){ + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName); + return SQLITE_ERROR; + } + + *pp = p; + return SQLITE_OK; +} + +/* +** The second argument, argv[], is an array of pointers to nul-terminated +** strings. This function makes a copy of the array and strings into a +** single block of memory. It then dequotes any of the strings that appear +** to be quoted. +** +** If successful, output parameter *pazDequote is set to point at the +** array of dequoted strings and SQLITE_OK is returned. The caller is +** responsible for eventually calling sqlite3_free() to free the array +** in this case. Or, if an error occurs, an SQLite error code is returned. +** The final value of *pazDequote is undefined in this case. +*/ +static int fts3tokDequoteArray( + int argc, /* Number of elements in argv[] */ + const char * const *argv, /* Input array */ + char ***pazDequote /* Output array */ +){ + int rc = SQLITE_OK; /* Return code */ + if( argc==0 ){ + *pazDequote = 0; + }else{ + int i; + int nByte = 0; + char **azDequote; + + for(i=0; ixCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok); + } + + if( rc==SQLITE_OK ){ + pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable)); + if( pTab==0 ){ + rc = SQLITE_NOMEM; + } + } + + if( rc==SQLITE_OK ){ + memset(pTab, 0, sizeof(Fts3tokTable)); + pTab->pMod = pMod; + pTab->pTok = pTok; + *ppVtab = &pTab->base; + }else{ + if( pTok ){ + pMod->xDestroy(pTok); + } + } + + sqlite3_free(azDequote); + return rc; +} + +/* +** This function does the work for both the xDisconnect and xDestroy methods. +** These tables have no persistent representation of their own, so xDisconnect +** and xDestroy are identical operations. +*/ +static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){ + Fts3tokTable *pTab = (Fts3tokTable *)pVtab; + + pTab->pMod->xDestroy(pTab->pTok); + sqlite3_free(pTab); + return SQLITE_OK; +} + +/* +** xBestIndex - Analyze a WHERE and ORDER BY clause. +*/ +static int fts3tokBestIndexMethod( + sqlite3_vtab *pVTab, + sqlite3_index_info *pInfo +){ + int i; + UNUSED_PARAMETER(pVTab); + + for(i=0; inConstraint; i++){ + if( pInfo->aConstraint[i].usable + && pInfo->aConstraint[i].iColumn==0 + && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ + ){ + pInfo->idxNum = 1; + pInfo->aConstraintUsage[i].argvIndex = 1; + pInfo->aConstraintUsage[i].omit = 1; + pInfo->estimatedCost = 1; + return SQLITE_OK; + } + } + + pInfo->idxNum = 0; + assert( pInfo->estimatedCost>1000000.0 ); + + return SQLITE_OK; +} + +/* +** xOpen - Open a cursor. +*/ +static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ + Fts3tokCursor *pCsr; + UNUSED_PARAMETER(pVTab); + + pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor)); + if( pCsr==0 ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(Fts3tokCursor)); + + *ppCsr = (sqlite3_vtab_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** Reset the tokenizer cursor passed as the only argument. As if it had +** just been returned by fts3tokOpenMethod(). +*/ +static void fts3tokResetCursor(Fts3tokCursor *pCsr){ + if( pCsr->pCsr ){ + Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab); + pTab->pMod->xClose(pCsr->pCsr); + pCsr->pCsr = 0; + } + sqlite3_free(pCsr->zInput); + pCsr->zInput = 0; + pCsr->zToken = 0; + pCsr->nToken = 0; + pCsr->iStart = 0; + pCsr->iEnd = 0; + pCsr->iPos = 0; + pCsr->iRowid = 0; +} + +/* +** xClose - Close a cursor. +*/ +static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){ + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + + fts3tokResetCursor(pCsr); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** xNext - Advance the cursor to the next row, if any. +*/ +static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){ + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab); + int rc; /* Return code */ + + pCsr->iRowid++; + rc = pTab->pMod->xNext(pCsr->pCsr, + &pCsr->zToken, &pCsr->nToken, + &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos + ); + + if( rc!=SQLITE_OK ){ + fts3tokResetCursor(pCsr); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + + return rc; +} + +/* +** xFilter - Initialize a cursor to point at the start of its data. +*/ +static int fts3tokFilterMethod( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, /* Strategy index */ + const char *idxStr, /* Unused */ + int nVal, /* Number of elements in apVal */ + sqlite3_value **apVal /* Arguments for the indexing scheme */ +){ + int rc = SQLITE_ERROR; + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab); + UNUSED_PARAMETER(idxStr); + UNUSED_PARAMETER(nVal); + + fts3tokResetCursor(pCsr); + if( idxNum==1 ){ + const char *zByte = (const char *)sqlite3_value_text(apVal[0]); + int nByte = sqlite3_value_bytes(apVal[0]); + pCsr->zInput = sqlite3_malloc(nByte+1); + if( pCsr->zInput==0 ){ + rc = SQLITE_NOMEM; + }else{ + memcpy(pCsr->zInput, zByte, nByte); + pCsr->zInput[nByte] = 0; + rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr); + if( rc==SQLITE_OK ){ + pCsr->pCsr->pTokenizer = pTab->pTok; + } + } + } + + if( rc!=SQLITE_OK ) return rc; + return fts3tokNextMethod(pCursor); +} + +/* +** xEof - Return true if the cursor is at EOF, or false otherwise. +*/ +static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){ + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + return (pCsr->zToken==0); +} + +/* +** xColumn - Return a column value. +*/ +static int fts3tokColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + + /* CREATE TABLE x(input, token, start, end, position) */ + switch( iCol ){ + case 0: + sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT); + break; + case 1: + sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT); + break; + case 2: + sqlite3_result_int(pCtx, pCsr->iStart); + break; + case 3: + sqlite3_result_int(pCtx, pCsr->iEnd); + break; + default: + assert( iCol==4 ); + sqlite3_result_int(pCtx, pCsr->iPos); + break; + } + return SQLITE_OK; +} + +/* +** xRowid - Return the current rowid for the cursor. +*/ +static int fts3tokRowidMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite_int64 *pRowid /* OUT: Rowid value */ +){ + Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor; + *pRowid = (sqlite3_int64)pCsr->iRowid; + return SQLITE_OK; +} + +/* +** Register the fts3tok module with database connection db. Return SQLITE_OK +** if successful or an error code if sqlite3_create_module() fails. +*/ +SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){ + static const sqlite3_module fts3tok_module = { + 0, /* iVersion */ + fts3tokConnectMethod, /* xCreate */ + fts3tokConnectMethod, /* xConnect */ + fts3tokBestIndexMethod, /* xBestIndex */ + fts3tokDisconnectMethod, /* xDisconnect */ + fts3tokDisconnectMethod, /* xDestroy */ + fts3tokOpenMethod, /* xOpen */ + fts3tokCloseMethod, /* xClose */ + fts3tokFilterMethod, /* xFilter */ + fts3tokNextMethod, /* xNext */ + fts3tokEofMethod, /* xEof */ + fts3tokColumnMethod, /* xColumn */ + fts3tokRowidMethod, /* xRowid */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindFunction */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0 /* xRollbackTo */ + }; + int rc; /* Return code */ + + rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash); + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_tokenize_vtab.c **********************************/ +/************** Begin file fts3_write.c **************************************/ +/* +** 2009 Oct 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file is part of the SQLite FTS3 extension module. Specifically, +** this file contains code to insert, update and delete rows from FTS3 +** tables. It also contains code to merge FTS3 b-tree segments. Some +** of the sub-routines used to merge segments are also used by the query +** code in fts3.c. +*/ + +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ +/* #include */ + + +#define FTS_MAX_APPENDABLE_HEIGHT 16 + +/* +** When full-text index nodes are loaded from disk, the buffer that they +** are loaded into has the following number of bytes of padding at the end +** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer +** of 920 bytes is allocated for it. +** +** This means that if we have a pointer into a buffer containing node data, +** it is always safe to read up to two varints from it without risking an +** overread, even if the node data is corrupted. +*/ +#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2) + +/* +** Under certain circumstances, b-tree nodes (doclists) can be loaded into +** memory incrementally instead of all at once. This can be a big performance +** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext() +** method before retrieving all query results (as may happen, for example, +** if a query has a LIMIT clause). +** +** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD +** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes. +** The code is written so that the hard lower-limit for each of these values +** is 1. Clearly such small values would be inefficient, but can be useful +** for testing purposes. +** +** If this module is built with SQLITE_TEST defined, these constants may +** be overridden at runtime for testing purposes. File fts3_test.c contains +** a Tcl interface to read and write the values. +*/ +#ifdef SQLITE_TEST +int test_fts3_node_chunksize = (4*1024); +int test_fts3_node_chunk_threshold = (4*1024)*4; +# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize +# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold +#else +# define FTS3_NODE_CHUNKSIZE (4*1024) +# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4) +#endif + +/* +** The two values that may be meaningfully bound to the :1 parameter in +** statements SQL_REPLACE_STAT and SQL_SELECT_STAT. +*/ +#define FTS_STAT_DOCTOTAL 0 +#define FTS_STAT_INCRMERGEHINT 1 +#define FTS_STAT_AUTOINCRMERGE 2 + +/* +** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic +** and incremental merge operation that takes place. This is used for +** debugging FTS only, it should not usually be turned on in production +** systems. +*/ +#ifdef FTS3_LOG_MERGES +static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){ + sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel); +} +#else +#define fts3LogMerge(x, y) +#endif + + +typedef struct PendingList PendingList; +typedef struct SegmentNode SegmentNode; +typedef struct SegmentWriter SegmentWriter; + +/* +** An instance of the following data structure is used to build doclists +** incrementally. See function fts3PendingListAppend() for details. +*/ +struct PendingList { + int nData; + char *aData; + int nSpace; + sqlite3_int64 iLastDocid; + sqlite3_int64 iLastCol; + sqlite3_int64 iLastPos; +}; + + +/* +** Each cursor has a (possibly empty) linked list of the following objects. +*/ +struct Fts3DeferredToken { + Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */ + int iCol; /* Column token must occur in */ + Fts3DeferredToken *pNext; /* Next in list of deferred tokens */ + PendingList *pList; /* Doclist is assembled here */ +}; + +/* +** An instance of this structure is used to iterate through the terms on +** a contiguous set of segment b-tree leaf nodes. Although the details of +** this structure are only manipulated by code in this file, opaque handles +** of type Fts3SegReader* are also used by code in fts3.c to iterate through +** terms when querying the full-text index. See functions: +** +** sqlite3Fts3SegReaderNew() +** sqlite3Fts3SegReaderFree() +** sqlite3Fts3SegReaderIterate() +** +** Methods used to manipulate Fts3SegReader structures: +** +** fts3SegReaderNext() +** fts3SegReaderFirstDocid() +** fts3SegReaderNextDocid() +*/ +struct Fts3SegReader { + int iIdx; /* Index within level, or 0x7FFFFFFF for PT */ + u8 bLookup; /* True for a lookup only */ + u8 rootOnly; /* True for a root-only reader */ + + sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */ + sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */ + sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */ + sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */ + + char *aNode; /* Pointer to node data (or NULL) */ + int nNode; /* Size of buffer at aNode (or 0) */ + int nPopulate; /* If >0, bytes of buffer aNode[] loaded */ + sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */ + + Fts3HashElem **ppNextElem; + + /* Variables set by fts3SegReaderNext(). These may be read directly + ** by the caller. They are valid from the time SegmentReaderNew() returns + ** until SegmentReaderNext() returns something other than SQLITE_OK + ** (i.e. SQLITE_DONE). + */ + int nTerm; /* Number of bytes in current term */ + char *zTerm; /* Pointer to current term */ + int nTermAlloc; /* Allocated size of zTerm buffer */ + char *aDoclist; /* Pointer to doclist of current entry */ + int nDoclist; /* Size of doclist in current entry */ + + /* The following variables are used by fts3SegReaderNextDocid() to iterate + ** through the current doclist (aDoclist/nDoclist). + */ + char *pOffsetList; + int nOffsetList; /* For descending pending seg-readers only */ + sqlite3_int64 iDocid; +}; + +#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0) +#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0) + +/* +** An instance of this structure is used to create a segment b-tree in the +** database. The internal details of this type are only accessed by the +** following functions: +** +** fts3SegWriterAdd() +** fts3SegWriterFlush() +** fts3SegWriterFree() +*/ +struct SegmentWriter { + SegmentNode *pTree; /* Pointer to interior tree structure */ + sqlite3_int64 iFirst; /* First slot in %_segments written */ + sqlite3_int64 iFree; /* Next free slot in %_segments */ + char *zTerm; /* Pointer to previous term buffer */ + int nTerm; /* Number of bytes in zTerm */ + int nMalloc; /* Size of malloc'd buffer at zMalloc */ + char *zMalloc; /* Malloc'd space (possibly) used for zTerm */ + int nSize; /* Size of allocation at aData */ + int nData; /* Bytes of data in aData */ + char *aData; /* Pointer to block from malloc() */ + i64 nLeafData; /* Number of bytes of leaf data written */ +}; + +/* +** Type SegmentNode is used by the following three functions to create +** the interior part of the segment b+-tree structures (everything except +** the leaf nodes). These functions and type are only ever used by code +** within the fts3SegWriterXXX() family of functions described above. +** +** fts3NodeAddTerm() +** fts3NodeWrite() +** fts3NodeFree() +** +** When a b+tree is written to the database (either as a result of a merge +** or the pending-terms table being flushed), leaves are written into the +** database file as soon as they are completely populated. The interior of +** the tree is assembled in memory and written out only once all leaves have +** been populated and stored. This is Ok, as the b+-tree fanout is usually +** very large, meaning that the interior of the tree consumes relatively +** little memory. +*/ +struct SegmentNode { + SegmentNode *pParent; /* Parent node (or NULL for root node) */ + SegmentNode *pRight; /* Pointer to right-sibling */ + SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */ + int nEntry; /* Number of terms written to node so far */ + char *zTerm; /* Pointer to previous term buffer */ + int nTerm; /* Number of bytes in zTerm */ + int nMalloc; /* Size of malloc'd buffer at zMalloc */ + char *zMalloc; /* Malloc'd space (possibly) used for zTerm */ + int nData; /* Bytes of valid data so far */ + char *aData; /* Node data */ +}; + +/* +** Valid values for the second argument to fts3SqlStmt(). +*/ +#define SQL_DELETE_CONTENT 0 +#define SQL_IS_EMPTY 1 +#define SQL_DELETE_ALL_CONTENT 2 +#define SQL_DELETE_ALL_SEGMENTS 3 +#define SQL_DELETE_ALL_SEGDIR 4 +#define SQL_DELETE_ALL_DOCSIZE 5 +#define SQL_DELETE_ALL_STAT 6 +#define SQL_SELECT_CONTENT_BY_ROWID 7 +#define SQL_NEXT_SEGMENT_INDEX 8 +#define SQL_INSERT_SEGMENTS 9 +#define SQL_NEXT_SEGMENTS_ID 10 +#define SQL_INSERT_SEGDIR 11 +#define SQL_SELECT_LEVEL 12 +#define SQL_SELECT_LEVEL_RANGE 13 +#define SQL_SELECT_LEVEL_COUNT 14 +#define SQL_SELECT_SEGDIR_MAX_LEVEL 15 +#define SQL_DELETE_SEGDIR_LEVEL 16 +#define SQL_DELETE_SEGMENTS_RANGE 17 +#define SQL_CONTENT_INSERT 18 +#define SQL_DELETE_DOCSIZE 19 +#define SQL_REPLACE_DOCSIZE 20 +#define SQL_SELECT_DOCSIZE 21 +#define SQL_SELECT_STAT 22 +#define SQL_REPLACE_STAT 23 + +#define SQL_SELECT_ALL_PREFIX_LEVEL 24 +#define SQL_DELETE_ALL_TERMS_SEGDIR 25 +#define SQL_DELETE_SEGDIR_RANGE 26 +#define SQL_SELECT_ALL_LANGID 27 +#define SQL_FIND_MERGE_LEVEL 28 +#define SQL_MAX_LEAF_NODE_ESTIMATE 29 +#define SQL_DELETE_SEGDIR_ENTRY 30 +#define SQL_SHIFT_SEGDIR_ENTRY 31 +#define SQL_SELECT_SEGDIR 32 +#define SQL_CHOMP_SEGDIR 33 +#define SQL_SEGMENT_IS_APPENDABLE 34 +#define SQL_SELECT_INDEXES 35 +#define SQL_SELECT_MXLEVEL 36 + +#define SQL_SELECT_LEVEL_RANGE2 37 +#define SQL_UPDATE_LEVEL_IDX 38 +#define SQL_UPDATE_LEVEL 39 + +/* +** This function is used to obtain an SQLite prepared statement handle +** for the statement identified by the second argument. If successful, +** *pp is set to the requested statement handle and SQLITE_OK returned. +** Otherwise, an SQLite error code is returned and *pp is set to 0. +** +** If argument apVal is not NULL, then it must point to an array with +** at least as many entries as the requested statement has bound +** parameters. The values are bound to the statements parameters before +** returning. +*/ +static int fts3SqlStmt( + Fts3Table *p, /* Virtual table handle */ + int eStmt, /* One of the SQL_XXX constants above */ + sqlite3_stmt **pp, /* OUT: Statement handle */ + sqlite3_value **apVal /* Values to bind to statement */ +){ + const char *azSql[] = { +/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?", +/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)", +/* 2 */ "DELETE FROM %Q.'%q_content'", +/* 3 */ "DELETE FROM %Q.'%q_segments'", +/* 4 */ "DELETE FROM %Q.'%q_segdir'", +/* 5 */ "DELETE FROM %Q.'%q_docsize'", +/* 6 */ "DELETE FROM %Q.'%q_stat'", +/* 7 */ "SELECT %s WHERE rowid=?", +/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1", +/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)", +/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)", +/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)", + + /* Return segments in order from oldest to newest.*/ +/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root " + "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC", +/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root " + "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?" + "ORDER BY level DESC, idx ASC", + +/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?", +/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?", + +/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?", +/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?", +/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)", +/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?", +/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", +/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?", +/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?", +/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)", +/* 24 */ "", +/* 25 */ "", + +/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?", +/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'", + +/* This statement is used to determine which level to read the input from +** when performing an incremental merge. It returns the absolute level number +** of the oldest level in the db that contains at least ? segments. Or, +** if no level in the FTS index contains more than ? segments, the statement +** returns zero rows. */ +/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' " + " GROUP BY level HAVING cnt>=?" + " ORDER BY (level %% 1024) ASC LIMIT 1", + +/* Estimate the upper limit on the number of leaf nodes in a new segment +** created by merging the oldest :2 segments from absolute level :1. See +** function sqlite3Fts3Incrmerge() for details. */ +/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) " + " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?", + +/* SQL_DELETE_SEGDIR_ENTRY +** Delete the %_segdir entry on absolute level :1 with index :2. */ +/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?", + +/* SQL_SHIFT_SEGDIR_ENTRY +** Modify the idx value for the segment with idx=:3 on absolute level :2 +** to :1. */ +/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?", + +/* SQL_SELECT_SEGDIR +** Read a single entry from the %_segdir table. The entry from absolute +** level :1 with index value :2. */ +/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root " + "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?", + +/* SQL_CHOMP_SEGDIR +** Update the start_block (:1) and root (:2) fields of the %_segdir +** entry located on absolute level :3 with index :4. */ +/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?" + "WHERE level = ? AND idx = ?", + +/* SQL_SEGMENT_IS_APPENDABLE +** Return a single row if the segment with end_block=? is appendable. Or +** no rows otherwise. */ +/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL", + +/* SQL_SELECT_INDEXES +** Return the list of valid segment indexes for absolute level ? */ +/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC", + +/* SQL_SELECT_MXLEVEL +** Return the largest relative level in the FTS index or indexes. */ +/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'", + + /* Return segments in order from oldest to newest.*/ +/* 37 */ "SELECT level, idx, end_block " + "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? " + "ORDER BY level DESC, idx ASC", + + /* Update statements used while promoting segments */ +/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? " + "WHERE level=? AND idx=?", +/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1" + + }; + int rc = SQLITE_OK; + sqlite3_stmt *pStmt; + + assert( SizeofArray(azSql)==SizeofArray(p->aStmt) ); + assert( eStmt=0 ); + + pStmt = p->aStmt[eStmt]; + if( !pStmt ){ + char *zSql; + if( eStmt==SQL_CONTENT_INSERT ){ + zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist); + }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){ + zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist); + }else{ + zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName); + } + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL); + sqlite3_free(zSql); + assert( rc==SQLITE_OK || pStmt==0 ); + p->aStmt[eStmt] = pStmt; + } + } + if( apVal ){ + int i; + int nParam = sqlite3_bind_parameter_count(pStmt); + for(i=0; rc==SQLITE_OK && inPendingData==0 ){ + sqlite3_stmt *pStmt; + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_null(pStmt, 1); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + } + } + + return rc; +} + +/* +** FTS maintains a separate indexes for each language-id (a 32-bit integer). +** Within each language id, a separate index is maintained to store the +** document terms, and each configured prefix size (configured the FTS +** "prefix=" option). And each index consists of multiple levels ("relative +** levels"). +** +** All three of these values (the language id, the specific index and the +** level within the index) are encoded in 64-bit integer values stored +** in the %_segdir table on disk. This function is used to convert three +** separate component values into the single 64-bit integer value that +** can be used to query the %_segdir table. +** +** Specifically, each language-id/index combination is allocated 1024 +** 64-bit integer level values ("absolute levels"). The main terms index +** for language-id 0 is allocate values 0-1023. The first prefix index +** (if any) for language-id 0 is allocated values 1024-2047. And so on. +** Language 1 indexes are allocated immediately following language 0. +** +** So, for a system with nPrefix prefix indexes configured, the block of +** absolute levels that corresponds to language-id iLangid and index +** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024). +*/ +static sqlite3_int64 getAbsoluteLevel( + Fts3Table *p, /* FTS3 table handle */ + int iLangid, /* Language id */ + int iIndex, /* Index in p->aIndex[] */ + int iLevel /* Level of segments */ +){ + sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */ + assert( iLangid>=0 ); + assert( p->nIndex>0 ); + assert( iIndex>=0 && iIndexnIndex ); + + iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL; + return iBase + iLevel; +} + +/* +** Set *ppStmt to a statement handle that may be used to iterate through +** all rows in the %_segdir table, from oldest to newest. If successful, +** return SQLITE_OK. If an error occurs while preparing the statement, +** return an SQLite error code. +** +** There is only ever one instance of this SQL statement compiled for +** each FTS3 table. +** +** The statement returns the following columns from the %_segdir table: +** +** 0: idx +** 1: start_block +** 2: leaves_end_block +** 3: end_block +** 4: root +*/ +SQLITE_PRIVATE int sqlite3Fts3AllSegdirs( + Fts3Table *p, /* FTS3 table */ + int iLangid, /* Language being queried */ + int iIndex, /* Index for p->aIndex[] */ + int iLevel, /* Level to select (relative level) */ + sqlite3_stmt **ppStmt /* OUT: Compiled statement */ +){ + int rc; + sqlite3_stmt *pStmt = 0; + + assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 ); + assert( iLevel=0 && iIndexnIndex ); + + if( iLevel<0 ){ + /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */ + rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0)); + sqlite3_bind_int64(pStmt, 2, + getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1) + ); + } + }else{ + /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */ + rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel)); + } + } + *ppStmt = pStmt; + return rc; +} + + +/* +** Append a single varint to a PendingList buffer. SQLITE_OK is returned +** if successful, or an SQLite error code otherwise. +** +** This function also serves to allocate the PendingList structure itself. +** For example, to create a new PendingList structure containing two +** varints: +** +** PendingList *p = 0; +** fts3PendingListAppendVarint(&p, 1); +** fts3PendingListAppendVarint(&p, 2); +*/ +static int fts3PendingListAppendVarint( + PendingList **pp, /* IN/OUT: Pointer to PendingList struct */ + sqlite3_int64 i /* Value to append to data */ +){ + PendingList *p = *pp; + + /* Allocate or grow the PendingList as required. */ + if( !p ){ + p = sqlite3_malloc(sizeof(*p) + 100); + if( !p ){ + return SQLITE_NOMEM; + } + p->nSpace = 100; + p->aData = (char *)&p[1]; + p->nData = 0; + } + else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){ + int nNew = p->nSpace * 2; + p = sqlite3_realloc(p, sizeof(*p) + nNew); + if( !p ){ + sqlite3_free(*pp); + *pp = 0; + return SQLITE_NOMEM; + } + p->nSpace = nNew; + p->aData = (char *)&p[1]; + } + + /* Append the new serialized varint to the end of the list. */ + p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i); + p->aData[p->nData] = '\0'; + *pp = p; + return SQLITE_OK; +} + +/* +** Add a docid/column/position entry to a PendingList structure. Non-zero +** is returned if the structure is sqlite3_realloced as part of adding +** the entry. Otherwise, zero. +** +** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning. +** Zero is always returned in this case. Otherwise, if no OOM error occurs, +** it is set to SQLITE_OK. +*/ +static int fts3PendingListAppend( + PendingList **pp, /* IN/OUT: PendingList structure */ + sqlite3_int64 iDocid, /* Docid for entry to add */ + sqlite3_int64 iCol, /* Column for entry to add */ + sqlite3_int64 iPos, /* Position of term for entry to add */ + int *pRc /* OUT: Return code */ +){ + PendingList *p = *pp; + int rc = SQLITE_OK; + + assert( !p || p->iLastDocid<=iDocid ); + + if( !p || p->iLastDocid!=iDocid ){ + sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0); + if( p ){ + assert( p->nDatanSpace ); + assert( p->aData[p->nData]==0 ); + p->nData++; + } + if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){ + goto pendinglistappend_out; + } + p->iLastCol = -1; + p->iLastPos = 0; + p->iLastDocid = iDocid; + } + if( iCol>0 && p->iLastCol!=iCol ){ + if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1)) + || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol)) + ){ + goto pendinglistappend_out; + } + p->iLastCol = iCol; + p->iLastPos = 0; + } + if( iCol>=0 ){ + assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) ); + rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos); + if( rc==SQLITE_OK ){ + p->iLastPos = iPos; + } + } + + pendinglistappend_out: + *pRc = rc; + if( p!=*pp ){ + *pp = p; + return 1; + } + return 0; +} + +/* +** Free a PendingList object allocated by fts3PendingListAppend(). +*/ +static void fts3PendingListDelete(PendingList *pList){ + sqlite3_free(pList); +} + +/* +** Add an entry to one of the pending-terms hash tables. +*/ +static int fts3PendingTermsAddOne( + Fts3Table *p, + int iCol, + int iPos, + Fts3Hash *pHash, /* Pending terms hash table to add entry to */ + const char *zToken, + int nToken +){ + PendingList *pList; + int rc = SQLITE_OK; + + pList = (PendingList *)fts3HashFind(pHash, zToken, nToken); + if( pList ){ + p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem)); + } + if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){ + if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){ + /* Malloc failed while inserting the new entry. This can only + ** happen if there was no previous entry for this token. + */ + assert( 0==fts3HashFind(pHash, zToken, nToken) ); + sqlite3_free(pList); + rc = SQLITE_NOMEM; + } + } + if( rc==SQLITE_OK ){ + p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem)); + } + return rc; +} + +/* +** Tokenize the nul-terminated string zText and add all tokens to the +** pending-terms hash-table. The docid used is that currently stored in +** p->iPrevDocid, and the column is specified by argument iCol. +** +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. +*/ +static int fts3PendingTermsAdd( + Fts3Table *p, /* Table into which text will be inserted */ + int iLangid, /* Language id to use */ + const char *zText, /* Text of document to be inserted */ + int iCol, /* Column into which text is being inserted */ + u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */ +){ + int rc; + int iStart = 0; + int iEnd = 0; + int iPos = 0; + int nWord = 0; + + char const *zToken; + int nToken = 0; + + sqlite3_tokenizer *pTokenizer = p->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + sqlite3_tokenizer_cursor *pCsr; + int (*xNext)(sqlite3_tokenizer_cursor *pCursor, + const char**,int*,int*,int*,int*); + + assert( pTokenizer && pModule ); + + /* If the user has inserted a NULL value, this function may be called with + ** zText==0. In this case, add zero token entries to the hash table and + ** return early. */ + if( zText==0 ){ + *pnWord = 0; + return SQLITE_OK; + } + + rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr); + if( rc!=SQLITE_OK ){ + return rc; + } + + xNext = pModule->xNext; + while( SQLITE_OK==rc + && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos)) + ){ + int i; + if( iPos>=nWord ) nWord = iPos+1; + + /* Positions cannot be negative; we use -1 as a terminator internally. + ** Tokens must have a non-zero length. + */ + if( iPos<0 || !zToken || nToken<=0 ){ + rc = SQLITE_ERROR; + break; + } + + /* Add the term to the terms index */ + rc = fts3PendingTermsAddOne( + p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken + ); + + /* Add the term to each of the prefix indexes that it is not too + ** short for. */ + for(i=1; rc==SQLITE_OK && inIndex; i++){ + struct Fts3Index *pIndex = &p->aIndex[i]; + if( nTokennPrefix ) continue; + rc = fts3PendingTermsAddOne( + p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix + ); + } + } + + pModule->xClose(pCsr); + *pnWord += nWord; + return (rc==SQLITE_DONE ? SQLITE_OK : rc); +} + +/* +** Calling this function indicates that subsequent calls to +** fts3PendingTermsAdd() are to add term/position-list pairs for the +** contents of the document with docid iDocid. +*/ +static int fts3PendingTermsDocid( + Fts3Table *p, /* Full-text table handle */ + int bDelete, /* True if this op is a delete */ + int iLangid, /* Language id of row being written */ + sqlite_int64 iDocid /* Docid of row being written */ +){ + assert( iLangid>=0 ); + assert( bDelete==1 || bDelete==0 ); + + /* TODO(shess) Explore whether partially flushing the buffer on + ** forced-flush would provide better performance. I suspect that if + ** we ordered the doclists by size and flushed the largest until the + ** buffer was half empty, that would let the less frequent terms + ** generate longer doclists. + */ + if( iDocidiPrevDocid + || (iDocid==p->iPrevDocid && p->bPrevDelete==0) + || p->iPrevLangid!=iLangid + || p->nPendingData>p->nMaxPendingData + ){ + int rc = sqlite3Fts3PendingTermsFlush(p); + if( rc!=SQLITE_OK ) return rc; + } + p->iPrevDocid = iDocid; + p->iPrevLangid = iLangid; + p->bPrevDelete = bDelete; + return SQLITE_OK; +} + +/* +** Discard the contents of the pending-terms hash tables. +*/ +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){ + int i; + for(i=0; inIndex; i++){ + Fts3HashElem *pElem; + Fts3Hash *pHash = &p->aIndex[i].hPending; + for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){ + PendingList *pList = (PendingList *)fts3HashData(pElem); + fts3PendingListDelete(pList); + } + fts3HashClear(pHash); + } + p->nPendingData = 0; +} + +/* +** This function is called by the xUpdate() method as part of an INSERT +** operation. It adds entries for each term in the new record to the +** pendingTerms hash table. +** +** Argument apVal is the same as the similarly named argument passed to +** fts3InsertData(). Parameter iDocid is the docid of the new row. +*/ +static int fts3InsertTerms( + Fts3Table *p, + int iLangid, + sqlite3_value **apVal, + u32 *aSz +){ + int i; /* Iterator variable */ + for(i=2; inColumn+2; i++){ + int iCol = i-2; + if( p->abNotindexed[iCol]==0 ){ + const char *zText = (const char *)sqlite3_value_text(apVal[i]); + int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]); + if( rc!=SQLITE_OK ){ + return rc; + } + aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]); + } + } + return SQLITE_OK; +} + +/* +** This function is called by the xUpdate() method for an INSERT operation. +** The apVal parameter is passed a copy of the apVal argument passed by +** SQLite to the xUpdate() method. i.e: +** +** apVal[0] Not used for INSERT. +** apVal[1] rowid +** apVal[2] Left-most user-defined column +** ... +** apVal[p->nColumn+1] Right-most user-defined column +** apVal[p->nColumn+2] Hidden column with same name as table +** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid) +** apVal[p->nColumn+4] Hidden languageid column +*/ +static int fts3InsertData( + Fts3Table *p, /* Full-text table */ + sqlite3_value **apVal, /* Array of values to insert */ + sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */ +){ + int rc; /* Return code */ + sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */ + + if( p->zContentTbl ){ + sqlite3_value *pRowid = apVal[p->nColumn+3]; + if( sqlite3_value_type(pRowid)==SQLITE_NULL ){ + pRowid = apVal[1]; + } + if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){ + return SQLITE_CONSTRAINT; + } + *piDocid = sqlite3_value_int64(pRowid); + return SQLITE_OK; + } + + /* Locate the statement handle used to insert data into the %_content + ** table. The SQL for this statement is: + ** + ** INSERT INTO %_content VALUES(?, ?, ?, ...) + ** + ** The statement features N '?' variables, where N is the number of user + ** defined columns in the FTS3 table, plus one for the docid field. + */ + rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]); + if( rc==SQLITE_OK && p->zLanguageid ){ + rc = sqlite3_bind_int( + pContentInsert, p->nColumn+2, + sqlite3_value_int(apVal[p->nColumn+4]) + ); + } + if( rc!=SQLITE_OK ) return rc; + + /* There is a quirk here. The users INSERT statement may have specified + ** a value for the "rowid" field, for the "docid" field, or for both. + ** Which is a problem, since "rowid" and "docid" are aliases for the + ** same value. For example: + ** + ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2); + ** + ** In FTS3, this is an error. It is an error to specify non-NULL values + ** for both docid and some other rowid alias. + */ + if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){ + if( SQLITE_NULL==sqlite3_value_type(apVal[0]) + && SQLITE_NULL!=sqlite3_value_type(apVal[1]) + ){ + /* A rowid/docid conflict. */ + return SQLITE_ERROR; + } + rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]); + if( rc!=SQLITE_OK ) return rc; + } + + /* Execute the statement to insert the record. Set *piDocid to the + ** new docid value. + */ + sqlite3_step(pContentInsert); + rc = sqlite3_reset(pContentInsert); + + *piDocid = sqlite3_last_insert_rowid(p->db); + return rc; +} + + + +/* +** Remove all data from the FTS3 table. Clear the hash table containing +** pending terms. +*/ +static int fts3DeleteAll(Fts3Table *p, int bContent){ + int rc = SQLITE_OK; /* Return code */ + + /* Discard the contents of the pending-terms hash table. */ + sqlite3Fts3PendingTermsClear(p); + + /* Delete everything from the shadow tables. Except, leave %_content as + ** is if bContent is false. */ + assert( p->zContentTbl==0 || bContent==0 ); + if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0); + fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0); + fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0); + if( p->bHasDocsize ){ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0); + } + if( p->bHasStat ){ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0); + } + return rc; +} + +/* +** +*/ +static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){ + int iLangid = 0; + if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1); + return iLangid; +} + +/* +** The first element in the apVal[] array is assumed to contain the docid +** (an integer) of a row about to be deleted. Remove all terms from the +** full-text index. +*/ +static void fts3DeleteTerms( + int *pRC, /* Result code */ + Fts3Table *p, /* The FTS table to delete from */ + sqlite3_value *pRowid, /* The docid to be deleted */ + u32 *aSz, /* Sizes of deleted document written here */ + int *pbFound /* OUT: Set to true if row really does exist */ +){ + int rc; + sqlite3_stmt *pSelect; + + assert( *pbFound==0 ); + if( *pRC ) return; + rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pSelect) ){ + int i; + int iLangid = langidFromSelect(p, pSelect); + i64 iDocid = sqlite3_column_int64(pSelect, 0); + rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid); + for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){ + int iCol = i-1; + if( p->abNotindexed[iCol]==0 ){ + const char *zText = (const char *)sqlite3_column_text(pSelect, i); + rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]); + aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i); + } + } + if( rc!=SQLITE_OK ){ + sqlite3_reset(pSelect); + *pRC = rc; + return; + } + *pbFound = 1; + } + rc = sqlite3_reset(pSelect); + }else{ + sqlite3_reset(pSelect); + } + *pRC = rc; +} + +/* +** Forward declaration to account for the circular dependency between +** functions fts3SegmentMerge() and fts3AllocateSegdirIdx(). +*/ +static int fts3SegmentMerge(Fts3Table *, int, int, int); + +/* +** This function allocates a new level iLevel index in the segdir table. +** Usually, indexes are allocated within a level sequentially starting +** with 0, so the allocated index is one greater than the value returned +** by: +** +** SELECT max(idx) FROM %_segdir WHERE level = :iLevel +** +** However, if there are already FTS3_MERGE_COUNT indexes at the requested +** level, they are merged into a single level (iLevel+1) segment and the +** allocated index is 0. +** +** If successful, *piIdx is set to the allocated index slot and SQLITE_OK +** returned. Otherwise, an SQLite error code is returned. +*/ +static int fts3AllocateSegdirIdx( + Fts3Table *p, + int iLangid, /* Language id */ + int iIndex, /* Index for p->aIndex */ + int iLevel, + int *piIdx +){ + int rc; /* Return Code */ + sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */ + int iNext = 0; /* Result of query pNextIdx */ + + assert( iLangid>=0 ); + assert( p->nIndex>=1 ); + + /* Set variable iNext to the next available segdir index at level iLevel. */ + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64( + pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel) + ); + if( SQLITE_ROW==sqlite3_step(pNextIdx) ){ + iNext = sqlite3_column_int(pNextIdx, 0); + } + rc = sqlite3_reset(pNextIdx); + } + + if( rc==SQLITE_OK ){ + /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already + ** full, merge all segments in level iLevel into a single iLevel+1 + ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise, + ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext. + */ + if( iNext>=FTS3_MERGE_COUNT ){ + fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel)); + rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel); + *piIdx = 0; + }else{ + *piIdx = iNext; + } + } + + return rc; +} + +/* +** The %_segments table is declared as follows: +** +** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB) +** +** This function reads data from a single row of the %_segments table. The +** specific row is identified by the iBlockid parameter. If paBlob is not +** NULL, then a buffer is allocated using sqlite3_malloc() and populated +** with the contents of the blob stored in the "block" column of the +** identified table row is. Whether or not paBlob is NULL, *pnBlob is set +** to the size of the blob in bytes before returning. +** +** If an error occurs, or the table does not contain the specified row, +** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If +** paBlob is non-NULL, then it is the responsibility of the caller to +** eventually free the returned buffer. +** +** This function may leave an open sqlite3_blob* handle in the +** Fts3Table.pSegments variable. This handle is reused by subsequent calls +** to this function. The handle may be closed by calling the +** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy +** performance improvement, but the blob handle should always be closed +** before control is returned to the user (to prevent a lock being held +** on the database file for longer than necessary). Thus, any virtual table +** method (xFilter etc.) that may directly or indirectly call this function +** must call sqlite3Fts3SegmentsClose() before returning. +*/ +SQLITE_PRIVATE int sqlite3Fts3ReadBlock( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */ + char **paBlob, /* OUT: Blob data in malloc'd buffer */ + int *pnBlob, /* OUT: Size of blob data */ + int *pnLoad /* OUT: Bytes actually loaded */ +){ + int rc; /* Return code */ + + /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */ + assert( pnBlob ); + + if( p->pSegments ){ + rc = sqlite3_blob_reopen(p->pSegments, iBlockid); + }else{ + if( 0==p->zSegmentsTbl ){ + p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName); + if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM; + } + rc = sqlite3_blob_open( + p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments + ); + } + + if( rc==SQLITE_OK ){ + int nByte = sqlite3_blob_bytes(p->pSegments); + *pnBlob = nByte; + if( paBlob ){ + char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING); + if( !aByte ){ + rc = SQLITE_NOMEM; + }else{ + if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){ + nByte = FTS3_NODE_CHUNKSIZE; + *pnLoad = nByte; + } + rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0); + memset(&aByte[nByte], 0, FTS3_NODE_PADDING); + if( rc!=SQLITE_OK ){ + sqlite3_free(aByte); + aByte = 0; + } + } + *paBlob = aByte; + } + } + + return rc; +} + +/* +** Close the blob handle at p->pSegments, if it is open. See comments above +** the sqlite3Fts3ReadBlock() function for details. +*/ +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){ + sqlite3_blob_close(p->pSegments); + p->pSegments = 0; +} + +static int fts3SegReaderIncrRead(Fts3SegReader *pReader){ + int nRead; /* Number of bytes to read */ + int rc; /* Return code */ + + nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE); + rc = sqlite3_blob_read( + pReader->pBlob, + &pReader->aNode[pReader->nPopulate], + nRead, + pReader->nPopulate + ); + + if( rc==SQLITE_OK ){ + pReader->nPopulate += nRead; + memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING); + if( pReader->nPopulate==pReader->nNode ){ + sqlite3_blob_close(pReader->pBlob); + pReader->pBlob = 0; + pReader->nPopulate = 0; + } + } + return rc; +} + +static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){ + int rc = SQLITE_OK; + assert( !pReader->pBlob + || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode]) + ); + while( pReader->pBlob && rc==SQLITE_OK + && (pFrom - pReader->aNode + nByte)>pReader->nPopulate + ){ + rc = fts3SegReaderIncrRead(pReader); + } + return rc; +} + +/* +** Set an Fts3SegReader cursor to point at EOF. +*/ +static void fts3SegReaderSetEof(Fts3SegReader *pSeg){ + if( !fts3SegReaderIsRootOnly(pSeg) ){ + sqlite3_free(pSeg->aNode); + sqlite3_blob_close(pSeg->pBlob); + pSeg->pBlob = 0; + } + pSeg->aNode = 0; +} + +/* +** Move the iterator passed as the first argument to the next term in the +** segment. If successful, SQLITE_OK is returned. If there is no next term, +** SQLITE_DONE. Otherwise, an SQLite error code. +*/ +static int fts3SegReaderNext( + Fts3Table *p, + Fts3SegReader *pReader, + int bIncr +){ + int rc; /* Return code of various sub-routines */ + char *pNext; /* Cursor variable */ + int nPrefix; /* Number of bytes in term prefix */ + int nSuffix; /* Number of bytes in term suffix */ + + if( !pReader->aDoclist ){ + pNext = pReader->aNode; + }else{ + pNext = &pReader->aDoclist[pReader->nDoclist]; + } + + if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){ + + if( fts3SegReaderIsPending(pReader) ){ + Fts3HashElem *pElem = *(pReader->ppNextElem); + sqlite3_free(pReader->aNode); + pReader->aNode = 0; + if( pElem ){ + char *aCopy; + PendingList *pList = (PendingList *)fts3HashData(pElem); + int nCopy = pList->nData+1; + pReader->zTerm = (char *)fts3HashKey(pElem); + pReader->nTerm = fts3HashKeysize(pElem); + aCopy = (char*)sqlite3_malloc(nCopy); + if( !aCopy ) return SQLITE_NOMEM; + memcpy(aCopy, pList->aData, nCopy); + pReader->nNode = pReader->nDoclist = nCopy; + pReader->aNode = pReader->aDoclist = aCopy; + pReader->ppNextElem++; + assert( pReader->aNode ); + } + return SQLITE_OK; + } + + fts3SegReaderSetEof(pReader); + + /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf + ** blocks have already been traversed. */ + assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock ); + if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){ + return SQLITE_OK; + } + + rc = sqlite3Fts3ReadBlock( + p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, + (bIncr ? &pReader->nPopulate : 0) + ); + if( rc!=SQLITE_OK ) return rc; + assert( pReader->pBlob==0 ); + if( bIncr && pReader->nPopulatenNode ){ + pReader->pBlob = p->pSegments; + p->pSegments = 0; + } + pNext = pReader->aNode; + } + + assert( !fts3SegReaderIsPending(pReader) ); + + rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2); + if( rc!=SQLITE_OK ) return rc; + + /* Because of the FTS3_NODE_PADDING bytes of padding, the following is + ** safe (no risk of overread) even if the node data is corrupted. */ + pNext += fts3GetVarint32(pNext, &nPrefix); + pNext += fts3GetVarint32(pNext, &nSuffix); + if( nPrefix<0 || nSuffix<=0 + || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] + ){ + return FTS_CORRUPT_VTAB; + } + + if( nPrefix+nSuffix>pReader->nTermAlloc ){ + int nNew = (nPrefix+nSuffix)*2; + char *zNew = sqlite3_realloc(pReader->zTerm, nNew); + if( !zNew ){ + return SQLITE_NOMEM; + } + pReader->zTerm = zNew; + pReader->nTermAlloc = nNew; + } + + rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX); + if( rc!=SQLITE_OK ) return rc; + + memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix); + pReader->nTerm = nPrefix+nSuffix; + pNext += nSuffix; + pNext += fts3GetVarint32(pNext, &pReader->nDoclist); + pReader->aDoclist = pNext; + pReader->pOffsetList = 0; + + /* Check that the doclist does not appear to extend past the end of the + ** b-tree node. And that the final byte of the doclist is 0x00. If either + ** of these statements is untrue, then the data structure is corrupt. + */ + if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] + || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1]) + ){ + return FTS_CORRUPT_VTAB; + } + return SQLITE_OK; +} + +/* +** Set the SegReader to point to the first docid in the doclist associated +** with the current term. +*/ +static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){ + int rc = SQLITE_OK; + assert( pReader->aDoclist ); + assert( !pReader->pOffsetList ); + if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){ + u8 bEof = 0; + pReader->iDocid = 0; + pReader->nOffsetList = 0; + sqlite3Fts3DoclistPrev(0, + pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, + &pReader->iDocid, &pReader->nOffsetList, &bEof + ); + }else{ + rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX); + if( rc==SQLITE_OK ){ + int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid); + pReader->pOffsetList = &pReader->aDoclist[n]; + } + } + return rc; +} + +/* +** Advance the SegReader to point to the next docid in the doclist +** associated with the current term. +** +** If arguments ppOffsetList and pnOffsetList are not NULL, then +** *ppOffsetList is set to point to the first column-offset list +** in the doclist entry (i.e. immediately past the docid varint). +** *pnOffsetList is set to the length of the set of column-offset +** lists, not including the nul-terminator byte. For example: +*/ +static int fts3SegReaderNextDocid( + Fts3Table *pTab, + Fts3SegReader *pReader, /* Reader to advance to next docid */ + char **ppOffsetList, /* OUT: Pointer to current position-list */ + int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */ +){ + int rc = SQLITE_OK; + char *p = pReader->pOffsetList; + char c = 0; + + assert( p ); + + if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){ + /* A pending-terms seg-reader for an FTS4 table that uses order=desc. + ** Pending-terms doclists are always built up in ascending order, so + ** we have to iterate through them backwards here. */ + u8 bEof = 0; + if( ppOffsetList ){ + *ppOffsetList = pReader->pOffsetList; + *pnOffsetList = pReader->nOffsetList - 1; + } + sqlite3Fts3DoclistPrev(0, + pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid, + &pReader->nOffsetList, &bEof + ); + if( bEof ){ + pReader->pOffsetList = 0; + }else{ + pReader->pOffsetList = p; + } + }else{ + char *pEnd = &pReader->aDoclist[pReader->nDoclist]; + + /* Pointer p currently points at the first byte of an offset list. The + ** following block advances it to point one byte past the end of + ** the same offset list. */ + while( 1 ){ + + /* The following line of code (and the "p++" below the while() loop) is + ** normally all that is required to move pointer p to the desired + ** position. The exception is if this node is being loaded from disk + ** incrementally and pointer "p" now points to the first byte past + ** the populated part of pReader->aNode[]. + */ + while( *p | c ) c = *p++ & 0x80; + assert( *p==0 ); + + if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break; + rc = fts3SegReaderIncrRead(pReader); + if( rc!=SQLITE_OK ) return rc; + } + p++; + + /* If required, populate the output variables with a pointer to and the + ** size of the previous offset-list. + */ + if( ppOffsetList ){ + *ppOffsetList = pReader->pOffsetList; + *pnOffsetList = (int)(p - pReader->pOffsetList - 1); + } + + /* List may have been edited in place by fts3EvalNearTrim() */ + while( p=pEnd ){ + pReader->pOffsetList = 0; + }else{ + rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX); + if( rc==SQLITE_OK ){ + sqlite3_int64 iDelta; + pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta); + if( pTab->bDescIdx ){ + pReader->iDocid -= iDelta; + }else{ + pReader->iDocid += iDelta; + } + } + } + } + + return SQLITE_OK; +} + + +SQLITE_PRIVATE int sqlite3Fts3MsrOvfl( + Fts3Cursor *pCsr, + Fts3MultiSegReader *pMsr, + int *pnOvfl +){ + Fts3Table *p = (Fts3Table*)pCsr->base.pVtab; + int nOvfl = 0; + int ii; + int rc = SQLITE_OK; + int pgsz = p->nPgsz; + + assert( p->bFts4 ); + assert( pgsz>0 ); + + for(ii=0; rc==SQLITE_OK && iinSegment; ii++){ + Fts3SegReader *pReader = pMsr->apSegment[ii]; + if( !fts3SegReaderIsPending(pReader) + && !fts3SegReaderIsRootOnly(pReader) + ){ + sqlite3_int64 jj; + for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){ + int nBlob; + rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0); + if( rc!=SQLITE_OK ) break; + if( (nBlob+35)>pgsz ){ + nOvfl += (nBlob + 34)/pgsz; + } + } + } + } + *pnOvfl = nOvfl; + return rc; +} + +/* +** Free all allocations associated with the iterator passed as the +** second argument. +*/ +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){ + if( pReader ){ + if( !fts3SegReaderIsPending(pReader) ){ + sqlite3_free(pReader->zTerm); + } + if( !fts3SegReaderIsRootOnly(pReader) ){ + sqlite3_free(pReader->aNode); + } + sqlite3_blob_close(pReader->pBlob); + } + sqlite3_free(pReader); +} + +/* +** Allocate a new SegReader object. +*/ +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew( + int iAge, /* Segment "age". */ + int bLookup, /* True for a lookup only */ + sqlite3_int64 iStartLeaf, /* First leaf to traverse */ + sqlite3_int64 iEndLeaf, /* Final leaf to traverse */ + sqlite3_int64 iEndBlock, /* Final block of segment */ + const char *zRoot, /* Buffer containing root node */ + int nRoot, /* Size of buffer containing root node */ + Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */ +){ + Fts3SegReader *pReader; /* Newly allocated SegReader object */ + int nExtra = 0; /* Bytes to allocate segment root node */ + + assert( iStartLeaf<=iEndLeaf ); + if( iStartLeaf==0 ){ + nExtra = nRoot + FTS3_NODE_PADDING; + } + + pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra); + if( !pReader ){ + return SQLITE_NOMEM; + } + memset(pReader, 0, sizeof(Fts3SegReader)); + pReader->iIdx = iAge; + pReader->bLookup = bLookup!=0; + pReader->iStartBlock = iStartLeaf; + pReader->iLeafEndBlock = iEndLeaf; + pReader->iEndBlock = iEndBlock; + + if( nExtra ){ + /* The entire segment is stored in the root node. */ + pReader->aNode = (char *)&pReader[1]; + pReader->rootOnly = 1; + pReader->nNode = nRoot; + memcpy(pReader->aNode, zRoot, nRoot); + memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING); + }else{ + pReader->iCurrentBlock = iStartLeaf-1; + } + *ppReader = pReader; + return SQLITE_OK; +} + +/* +** This is a comparison function used as a qsort() callback when sorting +** an array of pending terms by term. This occurs as part of flushing +** the contents of the pending-terms hash table to the database. +*/ +static int SQLITE_CDECL fts3CompareElemByTerm( + const void *lhs, + const void *rhs +){ + char *z1 = fts3HashKey(*(Fts3HashElem **)lhs); + char *z2 = fts3HashKey(*(Fts3HashElem **)rhs); + int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs); + int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs); + + int n = (n1aIndex */ + const char *zTerm, /* Term to search for */ + int nTerm, /* Size of buffer zTerm */ + int bPrefix, /* True for a prefix iterator */ + Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */ +){ + Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */ + Fts3HashElem *pE; /* Iterator variable */ + Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */ + int nElem = 0; /* Size of array at aElem */ + int rc = SQLITE_OK; /* Return Code */ + Fts3Hash *pHash; + + pHash = &p->aIndex[iIndex].hPending; + if( bPrefix ){ + int nAlloc = 0; /* Size of allocated array at aElem */ + + for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){ + char *zKey = (char *)fts3HashKey(pE); + int nKey = fts3HashKeysize(pE); + if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){ + if( nElem==nAlloc ){ + Fts3HashElem **aElem2; + nAlloc += 16; + aElem2 = (Fts3HashElem **)sqlite3_realloc( + aElem, nAlloc*sizeof(Fts3HashElem *) + ); + if( !aElem2 ){ + rc = SQLITE_NOMEM; + nElem = 0; + break; + } + aElem = aElem2; + } + + aElem[nElem++] = pE; + } + } + + /* If more than one term matches the prefix, sort the Fts3HashElem + ** objects in term order using qsort(). This uses the same comparison + ** callback as is used when flushing terms to disk. + */ + if( nElem>1 ){ + qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm); + } + + }else{ + /* The query is a simple term lookup that matches at most one term in + ** the index. All that is required is a straight hash-lookup. + ** + ** Because the stack address of pE may be accessed via the aElem pointer + ** below, the "Fts3HashElem *pE" must be declared so that it is valid + ** within this entire function, not just this "else{...}" block. + */ + pE = fts3HashFindElem(pHash, zTerm, nTerm); + if( pE ){ + aElem = &pE; + nElem = 1; + } + } + + if( nElem>0 ){ + int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *); + pReader = (Fts3SegReader *)sqlite3_malloc(nByte); + if( !pReader ){ + rc = SQLITE_NOMEM; + }else{ + memset(pReader, 0, nByte); + pReader->iIdx = 0x7FFFFFFF; + pReader->ppNextElem = (Fts3HashElem **)&pReader[1]; + memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *)); + } + } + + if( bPrefix ){ + sqlite3_free(aElem); + } + *ppReader = pReader; + return rc; +} + +/* +** Compare the entries pointed to by two Fts3SegReader structures. +** Comparison is as follows: +** +** 1) EOF is greater than not EOF. +** +** 2) The current terms (if any) are compared using memcmp(). If one +** term is a prefix of another, the longer term is considered the +** larger. +** +** 3) By segment age. An older segment is considered larger. +*/ +static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ + int rc; + if( pLhs->aNode && pRhs->aNode ){ + int rc2 = pLhs->nTerm - pRhs->nTerm; + if( rc2<0 ){ + rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm); + }else{ + rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm); + } + if( rc==0 ){ + rc = rc2; + } + }else{ + rc = (pLhs->aNode==0) - (pRhs->aNode==0); + } + if( rc==0 ){ + rc = pRhs->iIdx - pLhs->iIdx; + } + assert( rc!=0 ); + return rc; +} + +/* +** A different comparison function for SegReader structures. In this +** version, it is assumed that each SegReader points to an entry in +** a doclist for identical terms. Comparison is made as follows: +** +** 1) EOF (end of doclist in this case) is greater than not EOF. +** +** 2) By current docid. +** +** 3) By segment age. An older segment is considered larger. +*/ +static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ + int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0); + if( rc==0 ){ + if( pLhs->iDocid==pRhs->iDocid ){ + rc = pRhs->iIdx - pLhs->iIdx; + }else{ + rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1; + } + } + assert( pLhs->aNode && pRhs->aNode ); + return rc; +} +static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ + int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0); + if( rc==0 ){ + if( pLhs->iDocid==pRhs->iDocid ){ + rc = pRhs->iIdx - pLhs->iIdx; + }else{ + rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1; + } + } + assert( pLhs->aNode && pRhs->aNode ); + return rc; +} + +/* +** Compare the term that the Fts3SegReader object passed as the first argument +** points to with the term specified by arguments zTerm and nTerm. +** +** If the pSeg iterator is already at EOF, return 0. Otherwise, return +** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are +** equal, or +ve if the pSeg term is greater than zTerm/nTerm. +*/ +static int fts3SegReaderTermCmp( + Fts3SegReader *pSeg, /* Segment reader object */ + const char *zTerm, /* Term to compare to */ + int nTerm /* Size of term zTerm in bytes */ +){ + int res = 0; + if( pSeg->aNode ){ + if( pSeg->nTerm>nTerm ){ + res = memcmp(pSeg->zTerm, zTerm, nTerm); + }else{ + res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm); + } + if( res==0 ){ + res = pSeg->nTerm-nTerm; + } + } + return res; +} + +/* +** Argument apSegment is an array of nSegment elements. It is known that +** the final (nSegment-nSuspect) members are already in sorted order +** (according to the comparison function provided). This function shuffles +** the array around until all entries are in sorted order. +*/ +static void fts3SegReaderSort( + Fts3SegReader **apSegment, /* Array to sort entries of */ + int nSegment, /* Size of apSegment array */ + int nSuspect, /* Unsorted entry count */ + int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */ +){ + int i; /* Iterator variable */ + + assert( nSuspect<=nSegment ); + + if( nSuspect==nSegment ) nSuspect--; + for(i=nSuspect-1; i>=0; i--){ + int j; + for(j=i; j<(nSegment-1); j++){ + Fts3SegReader *pTmp; + if( xCmp(apSegment[j], apSegment[j+1])<0 ) break; + pTmp = apSegment[j+1]; + apSegment[j+1] = apSegment[j]; + apSegment[j] = pTmp; + } + } + +#ifndef NDEBUG + /* Check that the list really is sorted now. */ + for(i=0; i<(nSuspect-1); i++){ + assert( xCmp(apSegment[i], apSegment[i+1])<0 ); + } +#endif +} + +/* +** Insert a record into the %_segments table. +*/ +static int fts3WriteSegment( + Fts3Table *p, /* Virtual table handle */ + sqlite3_int64 iBlock, /* Block id for new block */ + char *z, /* Pointer to buffer containing block data */ + int n /* Size of buffer z in bytes */ +){ + sqlite3_stmt *pStmt; + int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pStmt, 1, iBlock); + sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + } + return rc; +} + +/* +** Find the largest relative level number in the table. If successful, set +** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs, +** set *pnMax to zero and return an SQLite error code. +*/ +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){ + int rc; + int mxLevel = 0; + sqlite3_stmt *pStmt = 0; + + rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + mxLevel = sqlite3_column_int(pStmt, 0); + } + rc = sqlite3_reset(pStmt); + } + *pnMax = mxLevel; + return rc; +} + +/* +** Insert a record into the %_segdir table. +*/ +static int fts3WriteSegdir( + Fts3Table *p, /* Virtual table handle */ + sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */ + int iIdx, /* Value for "idx" field */ + sqlite3_int64 iStartBlock, /* Value for "start_block" field */ + sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */ + sqlite3_int64 iEndBlock, /* Value for "end_block" field */ + sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */ + char *zRoot, /* Blob value for "root" field */ + int nRoot /* Number of bytes in buffer zRoot */ +){ + sqlite3_stmt *pStmt; + int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pStmt, 1, iLevel); + sqlite3_bind_int(pStmt, 2, iIdx); + sqlite3_bind_int64(pStmt, 3, iStartBlock); + sqlite3_bind_int64(pStmt, 4, iLeafEndBlock); + if( nLeafData==0 ){ + sqlite3_bind_int64(pStmt, 5, iEndBlock); + }else{ + char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData); + if( !zEnd ) return SQLITE_NOMEM; + sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free); + } + sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + } + return rc; +} + +/* +** Return the size of the common prefix (if any) shared by zPrev and +** zNext, in bytes. For example, +** +** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3 +** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2 +** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0 +*/ +static int fts3PrefixCompress( + const char *zPrev, /* Buffer containing previous term */ + int nPrev, /* Size of buffer zPrev in bytes */ + const char *zNext, /* Buffer containing next term */ + int nNext /* Size of buffer zNext in bytes */ +){ + int n; + UNUSED_PARAMETER(nNext); + for(n=0; nnData; /* Current size of node in bytes */ + int nReq = nData; /* Required space after adding zTerm */ + int nPrefix; /* Number of bytes of prefix compression */ + int nSuffix; /* Suffix length */ + + nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm); + nSuffix = nTerm-nPrefix; + + nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix; + if( nReq<=p->nNodeSize || !pTree->zTerm ){ + + if( nReq>p->nNodeSize ){ + /* An unusual case: this is the first term to be added to the node + ** and the static node buffer (p->nNodeSize bytes) is not large + ** enough. Use a separately malloced buffer instead This wastes + ** p->nNodeSize bytes, but since this scenario only comes about when + ** the database contain two terms that share a prefix of almost 2KB, + ** this is not expected to be a serious problem. + */ + assert( pTree->aData==(char *)&pTree[1] ); + pTree->aData = (char *)sqlite3_malloc(nReq); + if( !pTree->aData ){ + return SQLITE_NOMEM; + } + } + + if( pTree->zTerm ){ + /* There is no prefix-length field for first term in a node */ + nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix); + } + + nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix); + memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix); + pTree->nData = nData + nSuffix; + pTree->nEntry++; + + if( isCopyTerm ){ + if( pTree->nMalloczMalloc, nTerm*2); + if( !zNew ){ + return SQLITE_NOMEM; + } + pTree->nMalloc = nTerm*2; + pTree->zMalloc = zNew; + } + pTree->zTerm = pTree->zMalloc; + memcpy(pTree->zTerm, zTerm, nTerm); + pTree->nTerm = nTerm; + }else{ + pTree->zTerm = (char *)zTerm; + pTree->nTerm = nTerm; + } + return SQLITE_OK; + } + } + + /* If control flows to here, it was not possible to append zTerm to the + ** current node. Create a new node (a right-sibling of the current node). + ** If this is the first node in the tree, the term is added to it. + ** + ** Otherwise, the term is not added to the new node, it is left empty for + ** now. Instead, the term is inserted into the parent of pTree. If pTree + ** has no parent, one is created here. + */ + pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize); + if( !pNew ){ + return SQLITE_NOMEM; + } + memset(pNew, 0, sizeof(SegmentNode)); + pNew->nData = 1 + FTS3_VARINT_MAX; + pNew->aData = (char *)&pNew[1]; + + if( pTree ){ + SegmentNode *pParent = pTree->pParent; + rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm); + if( pTree->pParent==0 ){ + pTree->pParent = pParent; + } + pTree->pRight = pNew; + pNew->pLeftmost = pTree->pLeftmost; + pNew->pParent = pParent; + pNew->zMalloc = pTree->zMalloc; + pNew->nMalloc = pTree->nMalloc; + pTree->zMalloc = 0; + }else{ + pNew->pLeftmost = pNew; + rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); + } + + *ppTree = pNew; + return rc; +} + +/* +** Helper function for fts3NodeWrite(). +*/ +static int fts3TreeFinishNode( + SegmentNode *pTree, + int iHeight, + sqlite3_int64 iLeftChild +){ + int nStart; + assert( iHeight>=1 && iHeight<128 ); + nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild); + pTree->aData[nStart] = (char)iHeight; + sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild); + return nStart; +} + +/* +** Write the buffer for the segment node pTree and all of its peers to the +** database. Then call this function recursively to write the parent of +** pTree and its peers to the database. +** +** Except, if pTree is a root node, do not write it to the database. Instead, +** set output variables *paRoot and *pnRoot to contain the root node. +** +** If successful, SQLITE_OK is returned and output variable *piLast is +** set to the largest blockid written to the database (or zero if no +** blocks were written to the db). Otherwise, an SQLite error code is +** returned. +*/ +static int fts3NodeWrite( + Fts3Table *p, /* Virtual table handle */ + SegmentNode *pTree, /* SegmentNode handle */ + int iHeight, /* Height of this node in tree */ + sqlite3_int64 iLeaf, /* Block id of first leaf node */ + sqlite3_int64 iFree, /* Block id of next free slot in %_segments */ + sqlite3_int64 *piLast, /* OUT: Block id of last entry written */ + char **paRoot, /* OUT: Data for root node */ + int *pnRoot /* OUT: Size of root node in bytes */ +){ + int rc = SQLITE_OK; + + if( !pTree->pParent ){ + /* Root node of the tree. */ + int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf); + *piLast = iFree-1; + *pnRoot = pTree->nData - nStart; + *paRoot = &pTree->aData[nStart]; + }else{ + SegmentNode *pIter; + sqlite3_int64 iNextFree = iFree; + sqlite3_int64 iNextLeaf = iLeaf; + for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){ + int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf); + int nWrite = pIter->nData - nStart; + + rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite); + iNextFree++; + iNextLeaf += (pIter->nEntry+1); + } + if( rc==SQLITE_OK ){ + assert( iNextLeaf==iFree ); + rc = fts3NodeWrite( + p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot + ); + } + } + + return rc; +} + +/* +** Free all memory allocations associated with the tree pTree. +*/ +static void fts3NodeFree(SegmentNode *pTree){ + if( pTree ){ + SegmentNode *p = pTree->pLeftmost; + fts3NodeFree(p->pParent); + while( p ){ + SegmentNode *pRight = p->pRight; + if( p->aData!=(char *)&p[1] ){ + sqlite3_free(p->aData); + } + assert( pRight==0 || p->zMalloc==0 ); + sqlite3_free(p->zMalloc); + sqlite3_free(p); + p = pRight; + } + } +} + +/* +** Add a term to the segment being constructed by the SegmentWriter object +** *ppWriter. When adding the first term to a segment, *ppWriter should +** be passed NULL. This function will allocate a new SegmentWriter object +** and return it via the input/output variable *ppWriter in this case. +** +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. +*/ +static int fts3SegWriterAdd( + Fts3Table *p, /* Virtual table handle */ + SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */ + int isCopyTerm, /* True if buffer zTerm must be copied */ + const char *zTerm, /* Pointer to buffer containing term */ + int nTerm, /* Size of term in bytes */ + const char *aDoclist, /* Pointer to buffer containing doclist */ + int nDoclist /* Size of doclist in bytes */ +){ + int nPrefix; /* Size of term prefix in bytes */ + int nSuffix; /* Size of term suffix in bytes */ + int nReq; /* Number of bytes required on leaf page */ + int nData; + SegmentWriter *pWriter = *ppWriter; + + if( !pWriter ){ + int rc; + sqlite3_stmt *pStmt; + + /* Allocate the SegmentWriter structure */ + pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter)); + if( !pWriter ) return SQLITE_NOMEM; + memset(pWriter, 0, sizeof(SegmentWriter)); + *ppWriter = pWriter; + + /* Allocate a buffer in which to accumulate data */ + pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize); + if( !pWriter->aData ) return SQLITE_NOMEM; + pWriter->nSize = p->nNodeSize; + + /* Find the next free blockid in the %_segments table */ + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + pWriter->iFree = sqlite3_column_int64(pStmt, 0); + pWriter->iFirst = pWriter->iFree; + } + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ) return rc; + } + nData = pWriter->nData; + + nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm); + nSuffix = nTerm-nPrefix; + + /* Figure out how many bytes are required by this new entry */ + nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */ + sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */ + nSuffix + /* Term suffix */ + sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */ + nDoclist; /* Doclist data */ + + if( nData>0 && nData+nReq>p->nNodeSize ){ + int rc; + + /* The current leaf node is full. Write it out to the database. */ + rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData); + if( rc!=SQLITE_OK ) return rc; + p->nLeafAdd++; + + /* Add the current term to the interior node tree. The term added to + ** the interior tree must: + ** + ** a) be greater than the largest term on the leaf node just written + ** to the database (still available in pWriter->zTerm), and + ** + ** b) be less than or equal to the term about to be added to the new + ** leaf node (zTerm/nTerm). + ** + ** In other words, it must be the prefix of zTerm 1 byte longer than + ** the common prefix (if any) of zTerm and pWriter->zTerm. + */ + assert( nPrefixpTree, isCopyTerm, zTerm, nPrefix+1); + if( rc!=SQLITE_OK ) return rc; + + nData = 0; + pWriter->nTerm = 0; + + nPrefix = 0; + nSuffix = nTerm; + nReq = 1 + /* varint containing prefix size */ + sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */ + nTerm + /* Term suffix */ + sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */ + nDoclist; /* Doclist data */ + } + + /* Increase the total number of bytes written to account for the new entry. */ + pWriter->nLeafData += nReq; + + /* If the buffer currently allocated is too small for this entry, realloc + ** the buffer to make it large enough. + */ + if( nReq>pWriter->nSize ){ + char *aNew = sqlite3_realloc(pWriter->aData, nReq); + if( !aNew ) return SQLITE_NOMEM; + pWriter->aData = aNew; + pWriter->nSize = nReq; + } + assert( nData+nReq<=pWriter->nSize ); + + /* Append the prefix-compressed term and doclist to the buffer. */ + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix); + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix); + memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix); + nData += nSuffix; + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist); + memcpy(&pWriter->aData[nData], aDoclist, nDoclist); + pWriter->nData = nData + nDoclist; + + /* Save the current term so that it can be used to prefix-compress the next. + ** If the isCopyTerm parameter is true, then the buffer pointed to by + ** zTerm is transient, so take a copy of the term data. Otherwise, just + ** store a copy of the pointer. + */ + if( isCopyTerm ){ + if( nTerm>pWriter->nMalloc ){ + char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2); + if( !zNew ){ + return SQLITE_NOMEM; + } + pWriter->nMalloc = nTerm*2; + pWriter->zMalloc = zNew; + pWriter->zTerm = zNew; + } + assert( pWriter->zTerm==pWriter->zMalloc ); + memcpy(pWriter->zTerm, zTerm, nTerm); + }else{ + pWriter->zTerm = (char *)zTerm; + } + pWriter->nTerm = nTerm; + + return SQLITE_OK; +} + +/* +** Flush all data associated with the SegmentWriter object pWriter to the +** database. This function must be called after all terms have been added +** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is +** returned. Otherwise, an SQLite error code. +*/ +static int fts3SegWriterFlush( + Fts3Table *p, /* Virtual table handle */ + SegmentWriter *pWriter, /* SegmentWriter to flush to the db */ + sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */ + int iIdx /* Value for 'idx' column of %_segdir */ +){ + int rc; /* Return code */ + if( pWriter->pTree ){ + sqlite3_int64 iLast = 0; /* Largest block id written to database */ + sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */ + char *zRoot = NULL; /* Pointer to buffer containing root node */ + int nRoot = 0; /* Size of buffer zRoot */ + + iLastLeaf = pWriter->iFree; + rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData); + if( rc==SQLITE_OK ){ + rc = fts3NodeWrite(p, pWriter->pTree, 1, + pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot); + } + if( rc==SQLITE_OK ){ + rc = fts3WriteSegdir(p, iLevel, iIdx, + pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot); + } + }else{ + /* The entire tree fits on the root node. Write it to the segdir table. */ + rc = fts3WriteSegdir(p, iLevel, iIdx, + 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData); + } + p->nLeafAdd++; + return rc; +} + +/* +** Release all memory held by the SegmentWriter object passed as the +** first argument. +*/ +static void fts3SegWriterFree(SegmentWriter *pWriter){ + if( pWriter ){ + sqlite3_free(pWriter->aData); + sqlite3_free(pWriter->zMalloc); + fts3NodeFree(pWriter->pTree); + sqlite3_free(pWriter); + } +} + +/* +** The first value in the apVal[] array is assumed to contain an integer. +** This function tests if there exist any documents with docid values that +** are different from that integer. i.e. if deleting the document with docid +** pRowid would mean the FTS3 table were empty. +** +** If successful, *pisEmpty is set to true if the table is empty except for +** document pRowid, or false otherwise, and SQLITE_OK is returned. If an +** error occurs, an SQLite error code is returned. +*/ +static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){ + sqlite3_stmt *pStmt; + int rc; + if( p->zContentTbl ){ + /* If using the content=xxx option, assume the table is never empty */ + *pisEmpty = 0; + rc = SQLITE_OK; + }else{ + rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + *pisEmpty = sqlite3_column_int(pStmt, 0); + } + rc = sqlite3_reset(pStmt); + } + } + return rc; +} + +/* +** Set *pnMax to the largest segment level in the database for the index +** iIndex. +** +** Segment levels are stored in the 'level' column of the %_segdir table. +** +** Return SQLITE_OK if successful, or an SQLite error code if not. +*/ +static int fts3SegmentMaxLevel( + Fts3Table *p, + int iLangid, + int iIndex, + sqlite3_int64 *pnMax +){ + sqlite3_stmt *pStmt; + int rc; + assert( iIndex>=0 && iIndexnIndex ); + + /* Set pStmt to the compiled version of: + ** + ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? + ** + ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR). + */ + rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0)); + sqlite3_bind_int64(pStmt, 2, + getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1) + ); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + *pnMax = sqlite3_column_int64(pStmt, 0); + } + return sqlite3_reset(pStmt); +} + +/* +** iAbsLevel is an absolute level that may be assumed to exist within +** the database. This function checks if it is the largest level number +** within its index. Assuming no error occurs, *pbMax is set to 1 if +** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK +** is returned. If an error occurs, an error code is returned and the +** final value of *pbMax is undefined. +*/ +static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){ + + /* Set pStmt to the compiled version of: + ** + ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? + ** + ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR). + */ + sqlite3_stmt *pStmt; + int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + sqlite3_bind_int64(pStmt, 1, iAbsLevel+1); + sqlite3_bind_int64(pStmt, 2, + ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL + ); + + *pbMax = 0; + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL; + } + return sqlite3_reset(pStmt); +} + +/* +** Delete all entries in the %_segments table associated with the segment +** opened with seg-reader pSeg. This function does not affect the contents +** of the %_segdir table. +*/ +static int fts3DeleteSegment( + Fts3Table *p, /* FTS table handle */ + Fts3SegReader *pSeg /* Segment to delete */ +){ + int rc = SQLITE_OK; /* Return code */ + if( pSeg->iStartBlock ){ + sqlite3_stmt *pDelete; /* SQL statement to delete rows */ + rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock); + sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock); + sqlite3_step(pDelete); + rc = sqlite3_reset(pDelete); + } + } + return rc; +} + +/* +** This function is used after merging multiple segments into a single large +** segment to delete the old, now redundant, segment b-trees. Specifically, +** it: +** +** 1) Deletes all %_segments entries for the segments associated with +** each of the SegReader objects in the array passed as the third +** argument, and +** +** 2) deletes all %_segdir entries with level iLevel, or all %_segdir +** entries regardless of level if (iLevel<0). +** +** SQLITE_OK is returned if successful, otherwise an SQLite error code. +*/ +static int fts3DeleteSegdir( + Fts3Table *p, /* Virtual table handle */ + int iLangid, /* Language id */ + int iIndex, /* Index for p->aIndex */ + int iLevel, /* Level of %_segdir entries to delete */ + Fts3SegReader **apSegment, /* Array of SegReader objects */ + int nReader /* Size of array apSegment */ +){ + int rc = SQLITE_OK; /* Return Code */ + int i; /* Iterator variable */ + sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */ + + for(i=0; rc==SQLITE_OK && i=0 || iLevel==FTS3_SEGCURSOR_ALL ); + if( iLevel==FTS3_SEGCURSOR_ALL ){ + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0)); + sqlite3_bind_int64(pDelete, 2, + getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1) + ); + } + }else{ + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64( + pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel) + ); + } + } + + if( rc==SQLITE_OK ){ + sqlite3_step(pDelete); + rc = sqlite3_reset(pDelete); + } + + return rc; +} + +/* +** When this function is called, buffer *ppList (size *pnList bytes) contains +** a position list that may (or may not) feature multiple columns. This +** function adjusts the pointer *ppList and the length *pnList so that they +** identify the subset of the position list that corresponds to column iCol. +** +** If there are no entries in the input position list for column iCol, then +** *pnList is set to zero before returning. +** +** If parameter bZero is non-zero, then any part of the input list following +** the end of the output list is zeroed before returning. +*/ +static void fts3ColumnFilter( + int iCol, /* Column to filter on */ + int bZero, /* Zero out anything following *ppList */ + char **ppList, /* IN/OUT: Pointer to position list */ + int *pnList /* IN/OUT: Size of buffer *ppList in bytes */ +){ + char *pList = *ppList; + int nList = *pnList; + char *pEnd = &pList[nList]; + int iCurrent = 0; + char *p = pList; + + assert( iCol>=0 ); + while( 1 ){ + char c = 0; + while( ppMsr->nBuffer ){ + char *pNew; + pMsr->nBuffer = nList*2; + pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer); + if( !pNew ) return SQLITE_NOMEM; + pMsr->aBuffer = pNew; + } + + memcpy(pMsr->aBuffer, pList, nList); + return SQLITE_OK; +} + +SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext( + Fts3Table *p, /* Virtual table handle */ + Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */ + sqlite3_int64 *piDocid, /* OUT: Docid value */ + char **paPoslist, /* OUT: Pointer to position list */ + int *pnPoslist /* OUT: Size of position list in bytes */ +){ + int nMerge = pMsr->nAdvance; + Fts3SegReader **apSegment = pMsr->apSegment; + int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = ( + p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp + ); + + if( nMerge==0 ){ + *paPoslist = 0; + return SQLITE_OK; + } + + while( 1 ){ + Fts3SegReader *pSeg; + pSeg = pMsr->apSegment[0]; + + if( pSeg->pOffsetList==0 ){ + *paPoslist = 0; + break; + }else{ + int rc; + char *pList; + int nList; + int j; + sqlite3_int64 iDocid = apSegment[0]->iDocid; + + rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList); + j = 1; + while( rc==SQLITE_OK + && jpOffsetList + && apSegment[j]->iDocid==iDocid + ){ + rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0); + j++; + } + if( rc!=SQLITE_OK ) return rc; + fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp); + + if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){ + rc = fts3MsrBufferData(pMsr, pList, nList+1); + if( rc!=SQLITE_OK ) return rc; + assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 ); + pList = pMsr->aBuffer; + } + + if( pMsr->iColFilter>=0 ){ + fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList); + } + + if( nList>0 ){ + *paPoslist = pList; + *piDocid = iDocid; + *pnPoslist = nList; + break; + } + } + } + + return SQLITE_OK; +} + +static int fts3SegReaderStart( + Fts3Table *p, /* Virtual table handle */ + Fts3MultiSegReader *pCsr, /* Cursor object */ + const char *zTerm, /* Term searched for (or NULL) */ + int nTerm /* Length of zTerm in bytes */ +){ + int i; + int nSeg = pCsr->nSegment; + + /* If the Fts3SegFilter defines a specific term (or term prefix) to search + ** for, then advance each segment iterator until it points to a term of + ** equal or greater value than the specified term. This prevents many + ** unnecessary merge/sort operations for the case where single segment + ** b-tree leaf nodes contain more than one term. + */ + for(i=0; pCsr->bRestart==0 && inSegment; i++){ + int res = 0; + Fts3SegReader *pSeg = pCsr->apSegment[i]; + do { + int rc = fts3SegReaderNext(p, pSeg, 0); + if( rc!=SQLITE_OK ) return rc; + }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 ); + + if( pSeg->bLookup && res!=0 ){ + fts3SegReaderSetEof(pSeg); + } + } + fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp); + + return SQLITE_OK; +} + +SQLITE_PRIVATE int sqlite3Fts3SegReaderStart( + Fts3Table *p, /* Virtual table handle */ + Fts3MultiSegReader *pCsr, /* Cursor object */ + Fts3SegFilter *pFilter /* Restrictions on range of iteration */ +){ + pCsr->pFilter = pFilter; + return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm); +} + +SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart( + Fts3Table *p, /* Virtual table handle */ + Fts3MultiSegReader *pCsr, /* Cursor object */ + int iCol, /* Column to match on. */ + const char *zTerm, /* Term to iterate through a doclist for */ + int nTerm /* Number of bytes in zTerm */ +){ + int i; + int rc; + int nSegment = pCsr->nSegment; + int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = ( + p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp + ); + + assert( pCsr->pFilter==0 ); + assert( zTerm && nTerm>0 ); + + /* Advance each segment iterator until it points to the term zTerm/nTerm. */ + rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm); + if( rc!=SQLITE_OK ) return rc; + + /* Determine how many of the segments actually point to zTerm/nTerm. */ + for(i=0; iapSegment[i]; + if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){ + break; + } + } + pCsr->nAdvance = i; + + /* Advance each of the segments to point to the first docid. */ + for(i=0; inAdvance; i++){ + rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]); + if( rc!=SQLITE_OK ) return rc; + } + fts3SegReaderSort(pCsr->apSegment, i, i, xCmp); + + assert( iCol<0 || iColnColumn ); + pCsr->iColFilter = iCol; + + return SQLITE_OK; +} + +/* +** This function is called on a MultiSegReader that has been started using +** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also +** have been made. Calling this function puts the MultiSegReader in such +** a state that if the next two calls are: +** +** sqlite3Fts3SegReaderStart() +** sqlite3Fts3SegReaderStep() +** +** then the entire doclist for the term is available in +** MultiSegReader.aDoclist/nDoclist. +*/ +SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){ + int i; /* Used to iterate through segment-readers */ + + assert( pCsr->zTerm==0 ); + assert( pCsr->nTerm==0 ); + assert( pCsr->aDoclist==0 ); + assert( pCsr->nDoclist==0 ); + + pCsr->nAdvance = 0; + pCsr->bRestart = 1; + for(i=0; inSegment; i++){ + pCsr->apSegment[i]->pOffsetList = 0; + pCsr->apSegment[i]->nOffsetList = 0; + pCsr->apSegment[i]->iDocid = 0; + } + + return SQLITE_OK; +} + + +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep( + Fts3Table *p, /* Virtual table handle */ + Fts3MultiSegReader *pCsr /* Cursor object */ +){ + int rc = SQLITE_OK; + + int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY); + int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS); + int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER); + int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX); + int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN); + int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST); + + Fts3SegReader **apSegment = pCsr->apSegment; + int nSegment = pCsr->nSegment; + Fts3SegFilter *pFilter = pCsr->pFilter; + int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = ( + p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp + ); + + if( pCsr->nSegment==0 ) return SQLITE_OK; + + do { + int nMerge; + int i; + + /* Advance the first pCsr->nAdvance entries in the apSegment[] array + ** forward. Then sort the list in order of current term again. + */ + for(i=0; inAdvance; i++){ + Fts3SegReader *pSeg = apSegment[i]; + if( pSeg->bLookup ){ + fts3SegReaderSetEof(pSeg); + }else{ + rc = fts3SegReaderNext(p, pSeg, 0); + } + if( rc!=SQLITE_OK ) return rc; + } + fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp); + pCsr->nAdvance = 0; + + /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */ + assert( rc==SQLITE_OK ); + if( apSegment[0]->aNode==0 ) break; + + pCsr->nTerm = apSegment[0]->nTerm; + pCsr->zTerm = apSegment[0]->zTerm; + + /* If this is a prefix-search, and if the term that apSegment[0] points + ** to does not share a suffix with pFilter->zTerm/nTerm, then all + ** required callbacks have been made. In this case exit early. + ** + ** Similarly, if this is a search for an exact match, and the first term + ** of segment apSegment[0] is not a match, exit early. + */ + if( pFilter->zTerm && !isScan ){ + if( pCsr->nTermnTerm + || (!isPrefix && pCsr->nTerm>pFilter->nTerm) + || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) + ){ + break; + } + } + + nMerge = 1; + while( nMergeaNode + && apSegment[nMerge]->nTerm==pCsr->nTerm + && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm) + ){ + nMerge++; + } + + assert( isIgnoreEmpty || (isRequirePos && !isColFilter) ); + if( nMerge==1 + && !isIgnoreEmpty + && !isFirst + && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0) + ){ + pCsr->nDoclist = apSegment[0]->nDoclist; + if( fts3SegReaderIsPending(apSegment[0]) ){ + rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist); + pCsr->aDoclist = pCsr->aBuffer; + }else{ + pCsr->aDoclist = apSegment[0]->aDoclist; + } + if( rc==SQLITE_OK ) rc = SQLITE_ROW; + }else{ + int nDoclist = 0; /* Size of doclist */ + sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */ + + /* The current term of the first nMerge entries in the array + ** of Fts3SegReader objects is the same. The doclists must be merged + ** and a single term returned with the merged doclist. + */ + for(i=0; ipOffsetList ){ + int j; /* Number of segments that share a docid */ + char *pList = 0; + int nList = 0; + int nByte; + sqlite3_int64 iDocid = apSegment[0]->iDocid; + fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList); + j = 1; + while( jpOffsetList + && apSegment[j]->iDocid==iDocid + ){ + fts3SegReaderNextDocid(p, apSegment[j], 0, 0); + j++; + } + + if( isColFilter ){ + fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList); + } + + if( !isIgnoreEmpty || nList>0 ){ + + /* Calculate the 'docid' delta value to write into the merged + ** doclist. */ + sqlite3_int64 iDelta; + if( p->bDescIdx && nDoclist>0 ){ + iDelta = iPrev - iDocid; + }else{ + iDelta = iDocid - iPrev; + } + assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) ); + assert( nDoclist>0 || iDelta==iDocid ); + + nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0); + if( nDoclist+nByte>pCsr->nBuffer ){ + char *aNew; + pCsr->nBuffer = (nDoclist+nByte)*2; + aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer); + if( !aNew ){ + return SQLITE_NOMEM; + } + pCsr->aBuffer = aNew; + } + + if( isFirst ){ + char *a = &pCsr->aBuffer[nDoclist]; + int nWrite; + + nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a); + if( nWrite ){ + iPrev = iDocid; + nDoclist += nWrite; + } + }else{ + nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta); + iPrev = iDocid; + if( isRequirePos ){ + memcpy(&pCsr->aBuffer[nDoclist], pList, nList); + nDoclist += nList; + pCsr->aBuffer[nDoclist++] = '\0'; + } + } + } + + fts3SegReaderSort(apSegment, nMerge, j, xCmp); + } + if( nDoclist>0 ){ + pCsr->aDoclist = pCsr->aBuffer; + pCsr->nDoclist = nDoclist; + rc = SQLITE_ROW; + } + } + pCsr->nAdvance = nMerge; + }while( rc==SQLITE_OK ); + + return rc; +} + + +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish( + Fts3MultiSegReader *pCsr /* Cursor object */ +){ + if( pCsr ){ + int i; + for(i=0; inSegment; i++){ + sqlite3Fts3SegReaderFree(pCsr->apSegment[i]); + } + sqlite3_free(pCsr->apSegment); + sqlite3_free(pCsr->aBuffer); + + pCsr->nSegment = 0; + pCsr->apSegment = 0; + pCsr->aBuffer = 0; + } +} + +/* +** Decode the "end_block" field, selected by column iCol of the SELECT +** statement passed as the first argument. +** +** The "end_block" field may contain either an integer, or a text field +** containing the text representation of two non-negative integers separated +** by one or more space (0x20) characters. In the first case, set *piEndBlock +** to the integer value and *pnByte to zero before returning. In the second, +** set *piEndBlock to the first value and *pnByte to the second. +*/ +static void fts3ReadEndBlockField( + sqlite3_stmt *pStmt, + int iCol, + i64 *piEndBlock, + i64 *pnByte +){ + const unsigned char *zText = sqlite3_column_text(pStmt, iCol); + if( zText ){ + int i; + int iMul = 1; + i64 iVal = 0; + for(i=0; zText[i]>='0' && zText[i]<='9'; i++){ + iVal = iVal*10 + (zText[i] - '0'); + } + *piEndBlock = iVal; + while( zText[i]==' ' ) i++; + iVal = 0; + if( zText[i]=='-' ){ + i++; + iMul = -1; + } + for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){ + iVal = iVal*10 + (zText[i] - '0'); + } + *pnByte = (iVal * (i64)iMul); + } +} + + +/* +** A segment of size nByte bytes has just been written to absolute level +** iAbsLevel. Promote any segments that should be promoted as a result. +*/ +static int fts3PromoteSegments( + Fts3Table *p, /* FTS table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level just updated */ + sqlite3_int64 nByte /* Size of new segment at iAbsLevel */ +){ + int rc = SQLITE_OK; + sqlite3_stmt *pRange; + + rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0); + + if( rc==SQLITE_OK ){ + int bOk = 0; + i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1; + i64 nLimit = (nByte*3)/2; + + /* Loop through all entries in the %_segdir table corresponding to + ** segments in this index on levels greater than iAbsLevel. If there is + ** at least one such segment, and it is possible to determine that all + ** such segments are smaller than nLimit bytes in size, they will be + ** promoted to level iAbsLevel. */ + sqlite3_bind_int64(pRange, 1, iAbsLevel+1); + sqlite3_bind_int64(pRange, 2, iLast); + while( SQLITE_ROW==sqlite3_step(pRange) ){ + i64 nSize = 0, dummy; + fts3ReadEndBlockField(pRange, 2, &dummy, &nSize); + if( nSize<=0 || nSize>nLimit ){ + /* If nSize==0, then the %_segdir.end_block field does not not + ** contain a size value. This happens if it was written by an + ** old version of FTS. In this case it is not possible to determine + ** the size of the segment, and so segment promotion does not + ** take place. */ + bOk = 0; + break; + } + bOk = 1; + } + rc = sqlite3_reset(pRange); + + if( bOk ){ + int iIdx = 0; + sqlite3_stmt *pUpdate1 = 0; + sqlite3_stmt *pUpdate2 = 0; + + if( rc==SQLITE_OK ){ + rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0); + } + if( rc==SQLITE_OK ){ + rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0); + } + + if( rc==SQLITE_OK ){ + + /* Loop through all %_segdir entries for segments in this index with + ** levels equal to or greater than iAbsLevel. As each entry is visited, + ** updated it to set (level = -1) and (idx = N), where N is 0 for the + ** oldest segment in the range, 1 for the next oldest, and so on. + ** + ** In other words, move all segments being promoted to level -1, + ** setting the "idx" fields as appropriate to keep them in the same + ** order. The contents of level -1 (which is never used, except + ** transiently here), will be moved back to level iAbsLevel below. */ + sqlite3_bind_int64(pRange, 1, iAbsLevel); + while( SQLITE_ROW==sqlite3_step(pRange) ){ + sqlite3_bind_int(pUpdate1, 1, iIdx++); + sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0)); + sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1)); + sqlite3_step(pUpdate1); + rc = sqlite3_reset(pUpdate1); + if( rc!=SQLITE_OK ){ + sqlite3_reset(pRange); + break; + } + } + } + if( rc==SQLITE_OK ){ + rc = sqlite3_reset(pRange); + } + + /* Move level -1 to level iAbsLevel */ + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pUpdate2, 1, iAbsLevel); + sqlite3_step(pUpdate2); + rc = sqlite3_reset(pUpdate2); + } + } + } + + + return rc; +} + +/* +** Merge all level iLevel segments in the database into a single +** iLevel+1 segment. Or, if iLevel<0, merge all segments into a +** single segment with a level equal to the numerically largest level +** currently present in the database. +** +** If this function is called with iLevel<0, but there is only one +** segment in the database, SQLITE_DONE is returned immediately. +** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, +** an SQLite error code is returned. +*/ +static int fts3SegmentMerge( + Fts3Table *p, + int iLangid, /* Language id to merge */ + int iIndex, /* Index in p->aIndex[] to merge */ + int iLevel /* Level to merge */ +){ + int rc; /* Return code */ + int iIdx = 0; /* Index of new segment */ + sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */ + SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */ + Fts3SegFilter filter; /* Segment term filter condition */ + Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */ + int bIgnoreEmpty = 0; /* True to ignore empty segments */ + i64 iMaxLevel = 0; /* Max level number for this index/langid */ + + assert( iLevel==FTS3_SEGCURSOR_ALL + || iLevel==FTS3_SEGCURSOR_PENDING + || iLevel>=0 + ); + assert( iLevel=0 && iIndexnIndex ); + + rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr); + if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished; + + if( iLevel!=FTS3_SEGCURSOR_PENDING ){ + rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel); + if( rc!=SQLITE_OK ) goto finished; + } + + if( iLevel==FTS3_SEGCURSOR_ALL ){ + /* This call is to merge all segments in the database to a single + ** segment. The level of the new segment is equal to the numerically + ** greatest segment level currently present in the database for this + ** index. The idx of the new segment is always 0. */ + if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){ + rc = SQLITE_DONE; + goto finished; + } + iNewLevel = iMaxLevel; + bIgnoreEmpty = 1; + + }else{ + /* This call is to merge all segments at level iLevel. find the next + ** available segment index at level iLevel+1. The call to + ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to + ** a single iLevel+2 segment if necessary. */ + assert( FTS3_SEGCURSOR_PENDING==-1 ); + iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1); + rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx); + bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel); + } + if( rc!=SQLITE_OK ) goto finished; + + assert( csr.nSegment>0 ); + assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) ); + assert( iNewLevelnLeafData); + } + } + } + + finished: + fts3SegWriterFree(pWriter); + sqlite3Fts3SegReaderFinish(&csr); + return rc; +} + + +/* +** Flush the contents of pendingTerms to level 0 segments. +*/ +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){ + int rc = SQLITE_OK; + int i; + + for(i=0; rc==SQLITE_OK && inIndex; i++){ + rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + sqlite3Fts3PendingTermsClear(p); + + /* Determine the auto-incr-merge setting if unknown. If enabled, + ** estimate the number of leaf blocks of content to be written + */ + if( rc==SQLITE_OK && p->bHasStat + && p->nAutoincrmerge==0xff && p->nLeafAdd>0 + ){ + sqlite3_stmt *pStmt = 0; + rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE); + rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + p->nAutoincrmerge = sqlite3_column_int(pStmt, 0); + if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8; + }else if( rc==SQLITE_DONE ){ + p->nAutoincrmerge = 0; + } + rc = sqlite3_reset(pStmt); + } + } + return rc; +} + +/* +** Encode N integers as varints into a blob. +*/ +static void fts3EncodeIntArray( + int N, /* The number of integers to encode */ + u32 *a, /* The integer values */ + char *zBuf, /* Write the BLOB here */ + int *pNBuf /* Write number of bytes if zBuf[] used here */ +){ + int i, j; + for(i=j=0; iiPrevDocid. The sizes are encoded as +** a blob of varints. +*/ +static void fts3InsertDocsize( + int *pRC, /* Result code */ + Fts3Table *p, /* Table into which to insert */ + u32 *aSz /* Sizes of each column, in tokens */ +){ + char *pBlob; /* The BLOB encoding of the document size */ + int nBlob; /* Number of bytes in the BLOB */ + sqlite3_stmt *pStmt; /* Statement used to insert the encoding */ + int rc; /* Result code from subfunctions */ + + if( *pRC ) return; + pBlob = sqlite3_malloc( 10*p->nColumn ); + if( pBlob==0 ){ + *pRC = SQLITE_NOMEM; + return; + } + fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob); + rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0); + if( rc ){ + sqlite3_free(pBlob); + *pRC = rc; + return; + } + sqlite3_bind_int64(pStmt, 1, p->iPrevDocid); + sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free); + sqlite3_step(pStmt); + *pRC = sqlite3_reset(pStmt); +} + +/* +** Record 0 of the %_stat table contains a blob consisting of N varints, +** where N is the number of user defined columns in the fts3 table plus +** two. If nCol is the number of user defined columns, then values of the +** varints are set as follows: +** +** Varint 0: Total number of rows in the table. +** +** Varint 1..nCol: For each column, the total number of tokens stored in +** the column for all rows of the table. +** +** Varint 1+nCol: The total size, in bytes, of all text values in all +** columns of all rows of the table. +** +*/ +static void fts3UpdateDocTotals( + int *pRC, /* The result code */ + Fts3Table *p, /* Table being updated */ + u32 *aSzIns, /* Size increases */ + u32 *aSzDel, /* Size decreases */ + int nChng /* Change in the number of documents */ +){ + char *pBlob; /* Storage for BLOB written into %_stat */ + int nBlob; /* Size of BLOB written into %_stat */ + u32 *a; /* Array of integers that becomes the BLOB */ + sqlite3_stmt *pStmt; /* Statement for reading and writing */ + int i; /* Loop counter */ + int rc; /* Result code from subfunctions */ + + const int nStat = p->nColumn+2; + + if( *pRC ) return; + a = sqlite3_malloc( (sizeof(u32)+10)*nStat ); + if( a==0 ){ + *pRC = SQLITE_NOMEM; + return; + } + pBlob = (char*)&a[nStat]; + rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0); + if( rc ){ + sqlite3_free(a); + *pRC = rc; + return; + } + sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + fts3DecodeIntArray(nStat, a, + sqlite3_column_blob(pStmt, 0), + sqlite3_column_bytes(pStmt, 0)); + }else{ + memset(a, 0, sizeof(u32)*(nStat) ); + } + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ){ + sqlite3_free(a); + *pRC = rc; + return; + } + if( nChng<0 && a[0]<(u32)(-nChng) ){ + a[0] = 0; + }else{ + a[0] += nChng; + } + for(i=0; inColumn+1; i++){ + u32 x = a[i+1]; + if( x+aSzIns[i] < aSzDel[i] ){ + x = 0; + }else{ + x = x + aSzIns[i] - aSzDel[i]; + } + a[i+1] = x; + } + fts3EncodeIntArray(nStat, a, pBlob, &nBlob); + rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0); + if( rc ){ + sqlite3_free(a); + *pRC = rc; + return; + } + sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL); + sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC); + sqlite3_step(pStmt); + *pRC = sqlite3_reset(pStmt); + sqlite3_free(a); +} + +/* +** Merge the entire database so that there is one segment for each +** iIndex/iLangid combination. +*/ +static int fts3DoOptimize(Fts3Table *p, int bReturnDone){ + int bSeenDone = 0; + int rc; + sqlite3_stmt *pAllLangid = 0; + + rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); + if( rc==SQLITE_OK ){ + int rc2; + sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid); + sqlite3_bind_int(pAllLangid, 2, p->nIndex); + while( sqlite3_step(pAllLangid)==SQLITE_ROW ){ + int i; + int iLangid = sqlite3_column_int(pAllLangid, 0); + for(i=0; rc==SQLITE_OK && inIndex; i++){ + rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL); + if( rc==SQLITE_DONE ){ + bSeenDone = 1; + rc = SQLITE_OK; + } + } + } + rc2 = sqlite3_reset(pAllLangid); + if( rc==SQLITE_OK ) rc = rc2; + } + + sqlite3Fts3SegmentsClose(p); + sqlite3Fts3PendingTermsClear(p); + + return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc; +} + +/* +** This function is called when the user executes the following statement: +** +** INSERT INTO () VALUES('rebuild'); +** +** The entire FTS index is discarded and rebuilt. If the table is one +** created using the content=xxx option, then the new index is based on +** the current contents of the xxx table. Otherwise, it is rebuilt based +** on the contents of the %_content table. +*/ +static int fts3DoRebuild(Fts3Table *p){ + int rc; /* Return Code */ + + rc = fts3DeleteAll(p, 0); + if( rc==SQLITE_OK ){ + u32 *aSz = 0; + u32 *aSzIns = 0; + u32 *aSzDel = 0; + sqlite3_stmt *pStmt = 0; + int nEntry = 0; + + /* Compose and prepare an SQL statement to loop through the content table */ + char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + } + + if( rc==SQLITE_OK ){ + int nByte = sizeof(u32) * (p->nColumn+1)*3; + aSz = (u32 *)sqlite3_malloc(nByte); + if( aSz==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(aSz, 0, nByte); + aSzIns = &aSz[p->nColumn+1]; + aSzDel = &aSzIns[p->nColumn+1]; + } + } + + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + int iCol; + int iLangid = langidFromSelect(p, pStmt); + rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0)); + memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1)); + for(iCol=0; rc==SQLITE_OK && iColnColumn; iCol++){ + if( p->abNotindexed[iCol]==0 ){ + const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1); + rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]); + aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1); + } + } + if( p->bHasDocsize ){ + fts3InsertDocsize(&rc, p, aSz); + } + if( rc!=SQLITE_OK ){ + sqlite3_finalize(pStmt); + pStmt = 0; + }else{ + nEntry++; + for(iCol=0; iCol<=p->nColumn; iCol++){ + aSzIns[iCol] += aSz[iCol]; + } + } + } + if( p->bFts4 ){ + fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry); + } + sqlite3_free(aSz); + + if( pStmt ){ + int rc2 = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + + return rc; +} + + +/* +** This function opens a cursor used to read the input data for an +** incremental merge operation. Specifically, it opens a cursor to scan +** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute +** level iAbsLevel. +*/ +static int fts3IncrmergeCsr( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level to open */ + int nSeg, /* Number of segments to merge */ + Fts3MultiSegReader *pCsr /* Cursor object to populate */ +){ + int rc; /* Return Code */ + sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */ + int nByte; /* Bytes allocated at pCsr->apSegment[] */ + + /* Allocate space for the Fts3MultiSegReader.aCsr[] array */ + memset(pCsr, 0, sizeof(*pCsr)); + nByte = sizeof(Fts3SegReader *) * nSeg; + pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte); + + if( pCsr->apSegment==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pCsr->apSegment, 0, nByte); + rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0); + } + if( rc==SQLITE_OK ){ + int i; + int rc2; + sqlite3_bind_int64(pStmt, 1, iAbsLevel); + assert( pCsr->nSegment==0 ); + for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && iapSegment[i] + ); + pCsr->nSegment++; + } + rc2 = sqlite3_reset(pStmt); + if( rc==SQLITE_OK ) rc = rc2; + } + + return rc; +} + +typedef struct IncrmergeWriter IncrmergeWriter; +typedef struct NodeWriter NodeWriter; +typedef struct Blob Blob; +typedef struct NodeReader NodeReader; + +/* +** An instance of the following structure is used as a dynamic buffer +** to build up nodes or other blobs of data in. +** +** The function blobGrowBuffer() is used to extend the allocation. +*/ +struct Blob { + char *a; /* Pointer to allocation */ + int n; /* Number of valid bytes of data in a[] */ + int nAlloc; /* Allocated size of a[] (nAlloc>=n) */ +}; + +/* +** This structure is used to build up buffers containing segment b-tree +** nodes (blocks). +*/ +struct NodeWriter { + sqlite3_int64 iBlock; /* Current block id */ + Blob key; /* Last key written to the current block */ + Blob block; /* Current block image */ +}; + +/* +** An object of this type contains the state required to create or append +** to an appendable b-tree segment. +*/ +struct IncrmergeWriter { + int nLeafEst; /* Space allocated for leaf blocks */ + int nWork; /* Number of leaf pages flushed */ + sqlite3_int64 iAbsLevel; /* Absolute level of input segments */ + int iIdx; /* Index of *output* segment in iAbsLevel+1 */ + sqlite3_int64 iStart; /* Block number of first allocated block */ + sqlite3_int64 iEnd; /* Block number of last allocated block */ + sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */ + u8 bNoLeafData; /* If true, store 0 for segment size */ + NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT]; +}; + +/* +** An object of the following type is used to read data from a single +** FTS segment node. See the following functions: +** +** nodeReaderInit() +** nodeReaderNext() +** nodeReaderRelease() +*/ +struct NodeReader { + const char *aNode; + int nNode; + int iOff; /* Current offset within aNode[] */ + + /* Output variables. Containing the current node entry. */ + sqlite3_int64 iChild; /* Pointer to child node */ + Blob term; /* Current term */ + const char *aDoclist; /* Pointer to doclist */ + int nDoclist; /* Size of doclist in bytes */ +}; + +/* +** If *pRc is not SQLITE_OK when this function is called, it is a no-op. +** Otherwise, if the allocation at pBlob->a is not already at least nMin +** bytes in size, extend (realloc) it to be so. +** +** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a +** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc +** to reflect the new size of the pBlob->a[] buffer. +*/ +static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){ + if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){ + int nAlloc = nMin; + char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc); + if( a ){ + pBlob->nAlloc = nAlloc; + pBlob->a = a; + }else{ + *pRc = SQLITE_NOMEM; + } + } +} + +/* +** Attempt to advance the node-reader object passed as the first argument to +** the next entry on the node. +** +** Return an error code if an error occurs (SQLITE_NOMEM is possible). +** Otherwise return SQLITE_OK. If there is no next entry on the node +** (e.g. because the current entry is the last) set NodeReader->aNode to +** NULL to indicate EOF. Otherwise, populate the NodeReader structure output +** variables for the new entry. +*/ +static int nodeReaderNext(NodeReader *p){ + int bFirst = (p->term.n==0); /* True for first term on the node */ + int nPrefix = 0; /* Bytes to copy from previous term */ + int nSuffix = 0; /* Bytes to append to the prefix */ + int rc = SQLITE_OK; /* Return code */ + + assert( p->aNode ); + if( p->iChild && bFirst==0 ) p->iChild++; + if( p->iOff>=p->nNode ){ + /* EOF */ + p->aNode = 0; + }else{ + if( bFirst==0 ){ + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix); + } + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); + + blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc); + if( rc==SQLITE_OK ){ + memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix); + p->term.n = nPrefix+nSuffix; + p->iOff += nSuffix; + if( p->iChild==0 ){ + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist); + p->aDoclist = &p->aNode[p->iOff]; + p->iOff += p->nDoclist; + } + } + } + + assert( p->iOff<=p->nNode ); + + return rc; +} + +/* +** Release all dynamic resources held by node-reader object *p. +*/ +static void nodeReaderRelease(NodeReader *p){ + sqlite3_free(p->term.a); +} + +/* +** Initialize a node-reader object to read the node in buffer aNode/nNode. +** +** If successful, SQLITE_OK is returned and the NodeReader object set to +** point to the first entry on the node (if any). Otherwise, an SQLite +** error code is returned. +*/ +static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){ + memset(p, 0, sizeof(NodeReader)); + p->aNode = aNode; + p->nNode = nNode; + + /* Figure out if this is a leaf or an internal node. */ + if( p->aNode[0] ){ + /* An internal node. */ + p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild); + }else{ + p->iOff = 1; + } + + return nodeReaderNext(p); +} + +/* +** This function is called while writing an FTS segment each time a leaf o +** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed +** to be greater than the largest key on the node just written, but smaller +** than or equal to the first key that will be written to the next leaf +** node. +** +** The block id of the leaf node just written to disk may be found in +** (pWriter->aNodeWriter[0].iBlock) when this function is called. +*/ +static int fts3IncrmergePush( + Fts3Table *p, /* Fts3 table handle */ + IncrmergeWriter *pWriter, /* Writer object */ + const char *zTerm, /* Term to write to internal node */ + int nTerm /* Bytes at zTerm */ +){ + sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock; + int iLayer; + + assert( nTerm>0 ); + for(iLayer=1; ALWAYS(iLayeraNodeWriter[iLayer]; + int rc = SQLITE_OK; + int nPrefix; + int nSuffix; + int nSpace; + + /* Figure out how much space the key will consume if it is written to + ** the current node of layer iLayer. Due to the prefix compression, + ** the space required changes depending on which node the key is to + ** be added to. */ + nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm); + nSuffix = nTerm - nPrefix; + nSpace = sqlite3Fts3VarintLen(nPrefix); + nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; + + if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ + /* If the current node of layer iLayer contains zero keys, or if adding + ** the key to it will not cause it to grow to larger than nNodeSize + ** bytes in size, write the key here. */ + + Blob *pBlk = &pNode->block; + if( pBlk->n==0 ){ + blobGrowBuffer(pBlk, p->nNodeSize, &rc); + if( rc==SQLITE_OK ){ + pBlk->a[0] = (char)iLayer; + pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr); + } + } + blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc); + blobGrowBuffer(&pNode->key, nTerm, &rc); + + if( rc==SQLITE_OK ){ + if( pNode->key.n ){ + pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix); + } + pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix); + memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix); + pBlk->n += nSuffix; + + memcpy(pNode->key.a, zTerm, nTerm); + pNode->key.n = nTerm; + } + }else{ + /* Otherwise, flush the current node of layer iLayer to disk. + ** Then allocate a new, empty sibling node. The key will be written + ** into the parent of this node. */ + rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n); + + assert( pNode->block.nAlloc>=p->nNodeSize ); + pNode->block.a[0] = (char)iLayer; + pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1); + + iNextPtr = pNode->iBlock; + pNode->iBlock++; + pNode->key.n = 0; + } + + if( rc!=SQLITE_OK || iNextPtr==0 ) return rc; + iPtr = iNextPtr; + } + + assert( 0 ); + return 0; +} + +/* +** Append a term and (optionally) doclist to the FTS segment node currently +** stored in blob *pNode. The node need not contain any terms, but the +** header must be written before this function is called. +** +** A node header is a single 0x00 byte for a leaf node, or a height varint +** followed by the left-hand-child varint for an internal node. +** +** The term to be appended is passed via arguments zTerm/nTerm. For a +** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal +** node, both aDoclist and nDoclist must be passed 0. +** +** If the size of the value in blob pPrev is zero, then this is the first +** term written to the node. Otherwise, pPrev contains a copy of the +** previous term. Before this function returns, it is updated to contain a +** copy of zTerm/nTerm. +** +** It is assumed that the buffer associated with pNode is already large +** enough to accommodate the new entry. The buffer associated with pPrev +** is extended by this function if requrired. +** +** If an error (i.e. OOM condition) occurs, an SQLite error code is +** returned. Otherwise, SQLITE_OK. +*/ +static int fts3AppendToNode( + Blob *pNode, /* Current node image to append to */ + Blob *pPrev, /* Buffer containing previous term written */ + const char *zTerm, /* New term to write */ + int nTerm, /* Size of zTerm in bytes */ + const char *aDoclist, /* Doclist (or NULL) to write */ + int nDoclist /* Size of aDoclist in bytes */ +){ + int rc = SQLITE_OK; /* Return code */ + int bFirst = (pPrev->n==0); /* True if this is the first term written */ + int nPrefix; /* Size of term prefix in bytes */ + int nSuffix; /* Size of term suffix in bytes */ + + /* Node must have already been started. There must be a doclist for a + ** leaf node, and there must not be a doclist for an internal node. */ + assert( pNode->n>0 ); + assert( (pNode->a[0]=='\0')==(aDoclist!=0) ); + + blobGrowBuffer(pPrev, nTerm, &rc); + if( rc!=SQLITE_OK ) return rc; + + nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm); + nSuffix = nTerm - nPrefix; + memcpy(pPrev->a, zTerm, nTerm); + pPrev->n = nTerm; + + if( bFirst==0 ){ + pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix); + } + pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix); + memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix); + pNode->n += nSuffix; + + if( aDoclist ){ + pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist); + memcpy(&pNode->a[pNode->n], aDoclist, nDoclist); + pNode->n += nDoclist; + } + + assert( pNode->n<=pNode->nAlloc ); + + return SQLITE_OK; +} + +/* +** Append the current term and doclist pointed to by cursor pCsr to the +** appendable b-tree segment opened for writing by pWriter. +** +** Return SQLITE_OK if successful, or an SQLite error code otherwise. +*/ +static int fts3IncrmergeAppend( + Fts3Table *p, /* Fts3 table handle */ + IncrmergeWriter *pWriter, /* Writer object */ + Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */ +){ + const char *zTerm = pCsr->zTerm; + int nTerm = pCsr->nTerm; + const char *aDoclist = pCsr->aDoclist; + int nDoclist = pCsr->nDoclist; + int rc = SQLITE_OK; /* Return code */ + int nSpace; /* Total space in bytes required on leaf */ + int nPrefix; /* Size of prefix shared with previous term */ + int nSuffix; /* Size of suffix (nTerm - nPrefix) */ + NodeWriter *pLeaf; /* Object used to write leaf nodes */ + + pLeaf = &pWriter->aNodeWriter[0]; + nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm); + nSuffix = nTerm - nPrefix; + + nSpace = sqlite3Fts3VarintLen(nPrefix); + nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; + nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist; + + /* If the current block is not empty, and if adding this term/doclist + ** to the current block would make it larger than Fts3Table.nNodeSize + ** bytes, write this block out to the database. */ + if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){ + rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n); + pWriter->nWork++; + + /* Add the current term to the parent node. The term added to the + ** parent must: + ** + ** a) be greater than the largest term on the leaf node just written + ** to the database (still available in pLeaf->key), and + ** + ** b) be less than or equal to the term about to be added to the new + ** leaf node (zTerm/nTerm). + ** + ** In other words, it must be the prefix of zTerm 1 byte longer than + ** the common prefix (if any) of zTerm and pWriter->zTerm. + */ + if( rc==SQLITE_OK ){ + rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1); + } + + /* Advance to the next output block */ + pLeaf->iBlock++; + pLeaf->key.n = 0; + pLeaf->block.n = 0; + + nSuffix = nTerm; + nSpace = 1; + nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; + nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist; + } + + pWriter->nLeafData += nSpace; + blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc); + if( rc==SQLITE_OK ){ + if( pLeaf->block.n==0 ){ + pLeaf->block.n = 1; + pLeaf->block.a[0] = '\0'; + } + rc = fts3AppendToNode( + &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist + ); + } + + return rc; +} + +/* +** This function is called to release all dynamic resources held by the +** merge-writer object pWriter, and if no error has occurred, to flush +** all outstanding node buffers held by pWriter to disk. +** +** If *pRc is not SQLITE_OK when this function is called, then no attempt +** is made to write any data to disk. Instead, this function serves only +** to release outstanding resources. +** +** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while +** flushing buffers to disk, *pRc is set to an SQLite error code before +** returning. +*/ +static void fts3IncrmergeRelease( + Fts3Table *p, /* FTS3 table handle */ + IncrmergeWriter *pWriter, /* Merge-writer object */ + int *pRc /* IN/OUT: Error code */ +){ + int i; /* Used to iterate through non-root layers */ + int iRoot; /* Index of root in pWriter->aNodeWriter */ + NodeWriter *pRoot; /* NodeWriter for root node */ + int rc = *pRc; /* Error code */ + + /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment + ** root node. If the segment fits entirely on a single leaf node, iRoot + ** will be set to 0. If the root node is the parent of the leaves, iRoot + ** will be 1. And so on. */ + for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){ + NodeWriter *pNode = &pWriter->aNodeWriter[iRoot]; + if( pNode->block.n>0 ) break; + assert( *pRc || pNode->block.nAlloc==0 ); + assert( *pRc || pNode->key.nAlloc==0 ); + sqlite3_free(pNode->block.a); + sqlite3_free(pNode->key.a); + } + + /* Empty output segment. This is a no-op. */ + if( iRoot<0 ) return; + + /* The entire output segment fits on a single node. Normally, this means + ** the node would be stored as a blob in the "root" column of the %_segdir + ** table. However, this is not permitted in this case. The problem is that + ** space has already been reserved in the %_segments table, and so the + ** start_block and end_block fields of the %_segdir table must be populated. + ** And, by design or by accident, released versions of FTS cannot handle + ** segments that fit entirely on the root node with start_block!=0. + ** + ** Instead, create a synthetic root node that contains nothing but a + ** pointer to the single content node. So that the segment consists of a + ** single leaf and a single interior (root) node. + ** + ** Todo: Better might be to defer allocating space in the %_segments + ** table until we are sure it is needed. + */ + if( iRoot==0 ){ + Blob *pBlock = &pWriter->aNodeWriter[1].block; + blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc); + if( rc==SQLITE_OK ){ + pBlock->a[0] = 0x01; + pBlock->n = 1 + sqlite3Fts3PutVarint( + &pBlock->a[1], pWriter->aNodeWriter[0].iBlock + ); + } + iRoot = 1; + } + pRoot = &pWriter->aNodeWriter[iRoot]; + + /* Flush all currently outstanding nodes to disk. */ + for(i=0; iaNodeWriter[i]; + if( pNode->block.n>0 && rc==SQLITE_OK ){ + rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n); + } + sqlite3_free(pNode->block.a); + sqlite3_free(pNode->key.a); + } + + /* Write the %_segdir record. */ + if( rc==SQLITE_OK ){ + rc = fts3WriteSegdir(p, + pWriter->iAbsLevel+1, /* level */ + pWriter->iIdx, /* idx */ + pWriter->iStart, /* start_block */ + pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */ + pWriter->iEnd, /* end_block */ + (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */ + pRoot->block.a, pRoot->block.n /* root */ + ); + } + sqlite3_free(pRoot->block.a); + sqlite3_free(pRoot->key.a); + + *pRc = rc; +} + +/* +** Compare the term in buffer zLhs (size in bytes nLhs) with that in +** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of +** the other, it is considered to be smaller than the other. +** +** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve +** if it is greater. +*/ +static int fts3TermCmp( + const char *zLhs, int nLhs, /* LHS of comparison */ + const char *zRhs, int nRhs /* RHS of comparison */ +){ + int nCmp = MIN(nLhs, nRhs); + int res; + + res = memcmp(zLhs, zRhs, nCmp); + if( res==0 ) res = nLhs - nRhs; + + return res; +} + + +/* +** Query to see if the entry in the %_segments table with blockid iEnd is +** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before +** returning. Otherwise, set *pbRes to 0. +** +** Or, if an error occurs while querying the database, return an SQLite +** error code. The final value of *pbRes is undefined in this case. +** +** This is used to test if a segment is an "appendable" segment. If it +** is, then a NULL entry has been inserted into the %_segments table +** with blockid %_segdir.end_block. +*/ +static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){ + int bRes = 0; /* Result to set *pbRes to */ + sqlite3_stmt *pCheck = 0; /* Statement to query database with */ + int rc; /* Return code */ + + rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pCheck, 1, iEnd); + if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1; + rc = sqlite3_reset(pCheck); + } + + *pbRes = bRes; + return rc; +} + +/* +** This function is called when initializing an incremental-merge operation. +** It checks if the existing segment with index value iIdx at absolute level +** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the +** merge-writer object *pWriter is initialized to write to it. +** +** An existing segment can be appended to by an incremental merge if: +** +** * It was initially created as an appendable segment (with all required +** space pre-allocated), and +** +** * The first key read from the input (arguments zKey and nKey) is +** greater than the largest key currently stored in the potential +** output segment. +*/ +static int fts3IncrmergeLoad( + Fts3Table *p, /* Fts3 table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level of input segments */ + int iIdx, /* Index of candidate output segment */ + const char *zKey, /* First key to write */ + int nKey, /* Number of bytes in nKey */ + IncrmergeWriter *pWriter /* Populate this object */ +){ + int rc; /* Return code */ + sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */ + + rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0); + if( rc==SQLITE_OK ){ + sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */ + sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */ + sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */ + const char *aRoot = 0; /* Pointer to %_segdir.root buffer */ + int nRoot = 0; /* Size of aRoot[] in bytes */ + int rc2; /* Return code from sqlite3_reset() */ + int bAppendable = 0; /* Set to true if segment is appendable */ + + /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */ + sqlite3_bind_int64(pSelect, 1, iAbsLevel+1); + sqlite3_bind_int(pSelect, 2, iIdx); + if( sqlite3_step(pSelect)==SQLITE_ROW ){ + iStart = sqlite3_column_int64(pSelect, 1); + iLeafEnd = sqlite3_column_int64(pSelect, 2); + fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData); + if( pWriter->nLeafData<0 ){ + pWriter->nLeafData = pWriter->nLeafData * -1; + } + pWriter->bNoLeafData = (pWriter->nLeafData==0); + nRoot = sqlite3_column_bytes(pSelect, 4); + aRoot = sqlite3_column_blob(pSelect, 4); + }else{ + return sqlite3_reset(pSelect); + } + + /* Check for the zero-length marker in the %_segments table */ + rc = fts3IsAppendable(p, iEnd, &bAppendable); + + /* Check that zKey/nKey is larger than the largest key the candidate */ + if( rc==SQLITE_OK && bAppendable ){ + char *aLeaf = 0; + int nLeaf = 0; + + rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0); + if( rc==SQLITE_OK ){ + NodeReader reader; + for(rc = nodeReaderInit(&reader, aLeaf, nLeaf); + rc==SQLITE_OK && reader.aNode; + rc = nodeReaderNext(&reader) + ){ + assert( reader.aNode ); + } + if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){ + bAppendable = 0; + } + nodeReaderRelease(&reader); + } + sqlite3_free(aLeaf); + } + + if( rc==SQLITE_OK && bAppendable ){ + /* It is possible to append to this segment. Set up the IncrmergeWriter + ** object to do so. */ + int i; + int nHeight = (int)aRoot[0]; + NodeWriter *pNode; + + pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT; + pWriter->iStart = iStart; + pWriter->iEnd = iEnd; + pWriter->iAbsLevel = iAbsLevel; + pWriter->iIdx = iIdx; + + for(i=nHeight+1; iaNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst; + } + + pNode = &pWriter->aNodeWriter[nHeight]; + pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight; + blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc); + if( rc==SQLITE_OK ){ + memcpy(pNode->block.a, aRoot, nRoot); + pNode->block.n = nRoot; + } + + for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){ + NodeReader reader; + pNode = &pWriter->aNodeWriter[i]; + + rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n); + while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader); + blobGrowBuffer(&pNode->key, reader.term.n, &rc); + if( rc==SQLITE_OK ){ + memcpy(pNode->key.a, reader.term.a, reader.term.n); + pNode->key.n = reader.term.n; + if( i>0 ){ + char *aBlock = 0; + int nBlock = 0; + pNode = &pWriter->aNodeWriter[i-1]; + pNode->iBlock = reader.iChild; + rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0); + blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc); + if( rc==SQLITE_OK ){ + memcpy(pNode->block.a, aBlock, nBlock); + pNode->block.n = nBlock; + } + sqlite3_free(aBlock); + } + } + nodeReaderRelease(&reader); + } + } + + rc2 = sqlite3_reset(pSelect); + if( rc==SQLITE_OK ) rc = rc2; + } + + return rc; +} + +/* +** Determine the largest segment index value that exists within absolute +** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus +** one before returning SQLITE_OK. Or, if there are no segments at all +** within level iAbsLevel, set *piIdx to zero. +** +** If an error occurs, return an SQLite error code. The final value of +** *piIdx is undefined in this case. +*/ +static int fts3IncrmergeOutputIdx( + Fts3Table *p, /* FTS Table handle */ + sqlite3_int64 iAbsLevel, /* Absolute index of input segments */ + int *piIdx /* OUT: Next free index at iAbsLevel+1 */ +){ + int rc; + sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */ + + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1); + sqlite3_step(pOutputIdx); + *piIdx = sqlite3_column_int(pOutputIdx, 0); + rc = sqlite3_reset(pOutputIdx); + } + + return rc; +} + +/* +** Allocate an appendable output segment on absolute level iAbsLevel+1 +** with idx value iIdx. +** +** In the %_segdir table, a segment is defined by the values in three +** columns: +** +** start_block +** leaves_end_block +** end_block +** +** When an appendable segment is allocated, it is estimated that the +** maximum number of leaf blocks that may be required is the sum of the +** number of leaf blocks consumed by the input segments, plus the number +** of input segments, multiplied by two. This value is stored in stack +** variable nLeafEst. +** +** A total of 16*nLeafEst blocks are allocated when an appendable segment +** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous +** array of leaf nodes starts at the first block allocated. The array +** of interior nodes that are parents of the leaf nodes start at block +** (start_block + (1 + end_block - start_block) / 16). And so on. +** +** In the actual code below, the value "16" is replaced with the +** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT. +*/ +static int fts3IncrmergeWriter( + Fts3Table *p, /* Fts3 table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level of input segments */ + int iIdx, /* Index of new output segment */ + Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */ + IncrmergeWriter *pWriter /* Populate this object */ +){ + int rc; /* Return Code */ + int i; /* Iterator variable */ + int nLeafEst = 0; /* Blocks allocated for leaf nodes */ + sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */ + sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */ + + /* Calculate nLeafEst. */ + rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pLeafEst, 1, iAbsLevel); + sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment); + if( SQLITE_ROW==sqlite3_step(pLeafEst) ){ + nLeafEst = sqlite3_column_int(pLeafEst, 0); + } + rc = sqlite3_reset(pLeafEst); + } + if( rc!=SQLITE_OK ) return rc; + + /* Calculate the first block to use in the output segment */ + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){ + pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0); + pWriter->iEnd = pWriter->iStart - 1; + pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT; + } + rc = sqlite3_reset(pFirstBlock); + } + if( rc!=SQLITE_OK ) return rc; + + /* Insert the marker in the %_segments table to make sure nobody tries + ** to steal the space just allocated. This is also used to identify + ** appendable segments. */ + rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0); + if( rc!=SQLITE_OK ) return rc; + + pWriter->iAbsLevel = iAbsLevel; + pWriter->nLeafEst = nLeafEst; + pWriter->iIdx = iIdx; + + /* Set up the array of NodeWriter objects */ + for(i=0; iaNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst; + } + return SQLITE_OK; +} + +/* +** Remove an entry from the %_segdir table. This involves running the +** following two statements: +** +** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx +** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx +** +** The DELETE statement removes the specific %_segdir level. The UPDATE +** statement ensures that the remaining segments have contiguously allocated +** idx values. +*/ +static int fts3RemoveSegdirEntry( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level to delete from */ + int iIdx /* Index of %_segdir entry to delete */ +){ + int rc; /* Return code */ + sqlite3_stmt *pDelete = 0; /* DELETE statement */ + + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pDelete, 1, iAbsLevel); + sqlite3_bind_int(pDelete, 2, iIdx); + sqlite3_step(pDelete); + rc = sqlite3_reset(pDelete); + } + + return rc; +} + +/* +** One or more segments have just been removed from absolute level iAbsLevel. +** Update the 'idx' values of the remaining segments in the level so that +** the idx values are a contiguous sequence starting from 0. +*/ +static int fts3RepackSegdirLevel( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iAbsLevel /* Absolute level to repack */ +){ + int rc; /* Return code */ + int *aIdx = 0; /* Array of remaining idx values */ + int nIdx = 0; /* Valid entries in aIdx[] */ + int nAlloc = 0; /* Allocated size of aIdx[] */ + int i; /* Iterator variable */ + sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */ + sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */ + + rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0); + if( rc==SQLITE_OK ){ + int rc2; + sqlite3_bind_int64(pSelect, 1, iAbsLevel); + while( SQLITE_ROW==sqlite3_step(pSelect) ){ + if( nIdx>=nAlloc ){ + int *aNew; + nAlloc += 16; + aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int)); + if( !aNew ){ + rc = SQLITE_NOMEM; + break; + } + aIdx = aNew; + } + aIdx[nIdx++] = sqlite3_column_int(pSelect, 0); + } + rc2 = sqlite3_reset(pSelect); + if( rc==SQLITE_OK ) rc = rc2; + } + + if( rc==SQLITE_OK ){ + rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0); + } + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pUpdate, 2, iAbsLevel); + } + + assert( p->bIgnoreSavepoint==0 ); + p->bIgnoreSavepoint = 1; + for(i=0; rc==SQLITE_OK && ibIgnoreSavepoint = 0; + + sqlite3_free(aIdx); + return rc; +} + +static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){ + pNode->a[0] = (char)iHeight; + if( iChild ){ + assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) ); + pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild); + }else{ + assert( pNode->nAlloc>=1 ); + pNode->n = 1; + } +} + +/* +** The first two arguments are a pointer to and the size of a segment b-tree +** node. The node may be a leaf or an internal node. +** +** This function creates a new node image in blob object *pNew by copying +** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes) +** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode. +*/ +static int fts3TruncateNode( + const char *aNode, /* Current node image */ + int nNode, /* Size of aNode in bytes */ + Blob *pNew, /* OUT: Write new node image here */ + const char *zTerm, /* Omit all terms smaller than this */ + int nTerm, /* Size of zTerm in bytes */ + sqlite3_int64 *piBlock /* OUT: Block number in next layer down */ +){ + NodeReader reader; /* Reader object */ + Blob prev = {0, 0, 0}; /* Previous term written to new node */ + int rc = SQLITE_OK; /* Return code */ + int bLeaf = aNode[0]=='\0'; /* True for a leaf node */ + + /* Allocate required output space */ + blobGrowBuffer(pNew, nNode, &rc); + if( rc!=SQLITE_OK ) return rc; + pNew->n = 0; + + /* Populate new node buffer */ + for(rc = nodeReaderInit(&reader, aNode, nNode); + rc==SQLITE_OK && reader.aNode; + rc = nodeReaderNext(&reader) + ){ + if( pNew->n==0 ){ + int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm); + if( res<0 || (bLeaf==0 && res==0) ) continue; + fts3StartNode(pNew, (int)aNode[0], reader.iChild); + *piBlock = reader.iChild; + } + rc = fts3AppendToNode( + pNew, &prev, reader.term.a, reader.term.n, + reader.aDoclist, reader.nDoclist + ); + if( rc!=SQLITE_OK ) break; + } + if( pNew->n==0 ){ + fts3StartNode(pNew, (int)aNode[0], reader.iChild); + *piBlock = reader.iChild; + } + assert( pNew->n<=pNew->nAlloc ); + + nodeReaderRelease(&reader); + sqlite3_free(prev.a); + return rc; +} + +/* +** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute +** level iAbsLevel. This may involve deleting entries from the %_segments +** table, and modifying existing entries in both the %_segments and %_segdir +** tables. +** +** SQLITE_OK is returned if the segment is updated successfully. Or an +** SQLite error code otherwise. +*/ +static int fts3TruncateSegment( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */ + int iIdx, /* Index within level of segment to modify */ + const char *zTerm, /* Remove terms smaller than this */ + int nTerm /* Number of bytes in buffer zTerm */ +){ + int rc = SQLITE_OK; /* Return code */ + Blob root = {0,0,0}; /* New root page image */ + Blob block = {0,0,0}; /* Buffer used for any other block */ + sqlite3_int64 iBlock = 0; /* Block id */ + sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */ + sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */ + sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */ + + rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0); + if( rc==SQLITE_OK ){ + int rc2; /* sqlite3_reset() return code */ + sqlite3_bind_int64(pFetch, 1, iAbsLevel); + sqlite3_bind_int(pFetch, 2, iIdx); + if( SQLITE_ROW==sqlite3_step(pFetch) ){ + const char *aRoot = sqlite3_column_blob(pFetch, 4); + int nRoot = sqlite3_column_bytes(pFetch, 4); + iOldStart = sqlite3_column_int64(pFetch, 1); + rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock); + } + rc2 = sqlite3_reset(pFetch); + if( rc==SQLITE_OK ) rc = rc2; + } + + while( rc==SQLITE_OK && iBlock ){ + char *aBlock = 0; + int nBlock = 0; + iNewStart = iBlock; + + rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0); + if( rc==SQLITE_OK ){ + rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock); + } + if( rc==SQLITE_OK ){ + rc = fts3WriteSegment(p, iNewStart, block.a, block.n); + } + sqlite3_free(aBlock); + } + + /* Variable iNewStart now contains the first valid leaf node. */ + if( rc==SQLITE_OK && iNewStart ){ + sqlite3_stmt *pDel = 0; + rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pDel, 1, iOldStart); + sqlite3_bind_int64(pDel, 2, iNewStart-1); + sqlite3_step(pDel); + rc = sqlite3_reset(pDel); + } + } + + if( rc==SQLITE_OK ){ + sqlite3_stmt *pChomp = 0; + rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pChomp, 1, iNewStart); + sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC); + sqlite3_bind_int64(pChomp, 3, iAbsLevel); + sqlite3_bind_int(pChomp, 4, iIdx); + sqlite3_step(pChomp); + rc = sqlite3_reset(pChomp); + } + } + + sqlite3_free(root.a); + sqlite3_free(block.a); + return rc; +} + +/* +** This function is called after an incrmental-merge operation has run to +** merge (or partially merge) two or more segments from absolute level +** iAbsLevel. +** +** Each input segment is either removed from the db completely (if all of +** its data was copied to the output segment by the incrmerge operation) +** or modified in place so that it no longer contains those entries that +** have been duplicated in the output segment. +*/ +static int fts3IncrmergeChomp( + Fts3Table *p, /* FTS table handle */ + sqlite3_int64 iAbsLevel, /* Absolute level containing segments */ + Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */ + int *pnRem /* Number of segments not deleted */ +){ + int i; + int nRem = 0; + int rc = SQLITE_OK; + + for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){ + Fts3SegReader *pSeg = 0; + int j; + + /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding + ** somewhere in the pCsr->apSegment[] array. */ + for(j=0; ALWAYS(jnSegment); j++){ + pSeg = pCsr->apSegment[j]; + if( pSeg->iIdx==i ) break; + } + assert( jnSegment && pSeg->iIdx==i ); + + if( pSeg->aNode==0 ){ + /* Seg-reader is at EOF. Remove the entire input segment. */ + rc = fts3DeleteSegment(p, pSeg); + if( rc==SQLITE_OK ){ + rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx); + } + *pnRem = 0; + }else{ + /* The incremental merge did not copy all the data from this + ** segment to the upper level. The segment is modified in place + ** so that it contains no keys smaller than zTerm/nTerm. */ + const char *zTerm = pSeg->zTerm; + int nTerm = pSeg->nTerm; + rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm); + nRem++; + } + } + + if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){ + rc = fts3RepackSegdirLevel(p, iAbsLevel); + } + + *pnRem = nRem; + return rc; +} + +/* +** Store an incr-merge hint in the database. +*/ +static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){ + sqlite3_stmt *pReplace = 0; + int rc; /* Return code */ + + rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT); + sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC); + sqlite3_step(pReplace); + rc = sqlite3_reset(pReplace); + } + + return rc; +} + +/* +** Load an incr-merge hint from the database. The incr-merge hint, if one +** exists, is stored in the rowid==1 row of the %_stat table. +** +** If successful, populate blob *pHint with the value read from the %_stat +** table and return SQLITE_OK. Otherwise, if an error occurs, return an +** SQLite error code. +*/ +static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){ + sqlite3_stmt *pSelect = 0; + int rc; + + pHint->n = 0; + rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0); + if( rc==SQLITE_OK ){ + int rc2; + sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT); + if( SQLITE_ROW==sqlite3_step(pSelect) ){ + const char *aHint = sqlite3_column_blob(pSelect, 0); + int nHint = sqlite3_column_bytes(pSelect, 0); + if( aHint ){ + blobGrowBuffer(pHint, nHint, &rc); + if( rc==SQLITE_OK ){ + memcpy(pHint->a, aHint, nHint); + pHint->n = nHint; + } + } + } + rc2 = sqlite3_reset(pSelect); + if( rc==SQLITE_OK ) rc = rc2; + } + + return rc; +} + +/* +** If *pRc is not SQLITE_OK when this function is called, it is a no-op. +** Otherwise, append an entry to the hint stored in blob *pHint. Each entry +** consists of two varints, the absolute level number of the input segments +** and the number of input segments. +** +** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs, +** set *pRc to an SQLite error code before returning. +*/ +static void fts3IncrmergeHintPush( + Blob *pHint, /* Hint blob to append to */ + i64 iAbsLevel, /* First varint to store in hint */ + int nInput, /* Second varint to store in hint */ + int *pRc /* IN/OUT: Error code */ +){ + blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc); + if( *pRc==SQLITE_OK ){ + pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel); + pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput); + } +} + +/* +** Read the last entry (most recently pushed) from the hint blob *pHint +** and then remove the entry. Write the two values read to *piAbsLevel and +** *pnInput before returning. +** +** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does +** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB. +*/ +static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){ + const int nHint = pHint->n; + int i; + + i = pHint->n-2; + while( i>0 && (pHint->a[i-1] & 0x80) ) i--; + while( i>0 && (pHint->a[i-1] & 0x80) ) i--; + + pHint->n = i; + i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel); + i += fts3GetVarint32(&pHint->a[i], pnInput); + if( i!=nHint ) return FTS_CORRUPT_VTAB; + + return SQLITE_OK; +} + + +/* +** Attempt an incremental merge that writes nMerge leaf blocks. +** +** Incremental merges happen nMin segments at a time. The segments +** to be merged are the nMin oldest segments (the ones with the smallest +** values for the _segdir.idx field) in the highest level that contains +** at least nMin segments. Multiple merges might occur in an attempt to +** write the quota of nMerge leaf blocks. +*/ +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ + int rc; /* Return code */ + int nRem = nMerge; /* Number of leaf pages yet to be written */ + Fts3MultiSegReader *pCsr; /* Cursor used to read input data */ + Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */ + IncrmergeWriter *pWriter; /* Writer object */ + int nSeg = 0; /* Number of input segments */ + sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */ + Blob hint = {0, 0, 0}; /* Hint read from %_stat table */ + int bDirtyHint = 0; /* True if blob 'hint' has been modified */ + + /* Allocate space for the cursor, filter and writer objects */ + const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter); + pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc); + if( !pWriter ) return SQLITE_NOMEM; + pFilter = (Fts3SegFilter *)&pWriter[1]; + pCsr = (Fts3MultiSegReader *)&pFilter[1]; + + rc = fts3IncrmergeHintLoad(p, &hint); + while( rc==SQLITE_OK && nRem>0 ){ + const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex; + sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */ + int bUseHint = 0; /* True if attempting to append */ + int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */ + + /* Search the %_segdir table for the absolute level with the smallest + ** relative level number that contains at least nMin segments, if any. + ** If one is found, set iAbsLevel to the absolute level number and + ** nSeg to nMin. If no level with at least nMin segments can be found, + ** set nSeg to -1. + */ + rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0); + sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)); + if( sqlite3_step(pFindLevel)==SQLITE_ROW ){ + iAbsLevel = sqlite3_column_int64(pFindLevel, 0); + nSeg = sqlite3_column_int(pFindLevel, 1); + assert( nSeg>=2 ); + }else{ + nSeg = -1; + } + rc = sqlite3_reset(pFindLevel); + + /* If the hint read from the %_stat table is not empty, check if the + ** last entry in it specifies a relative level smaller than or equal + ** to the level identified by the block above (if any). If so, this + ** iteration of the loop will work on merging at the hinted level. + */ + if( rc==SQLITE_OK && hint.n ){ + int nHint = hint.n; + sqlite3_int64 iHintAbsLevel = 0; /* Hint level */ + int nHintSeg = 0; /* Hint number of segments */ + + rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg); + if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){ + iAbsLevel = iHintAbsLevel; + nSeg = nHintSeg; + bUseHint = 1; + bDirtyHint = 1; + }else{ + /* This undoes the effect of the HintPop() above - so that no entry + ** is removed from the hint blob. */ + hint.n = nHint; + } + } + + /* If nSeg is less that zero, then there is no level with at least + ** nMin segments and no hint in the %_stat table. No work to do. + ** Exit early in this case. */ + if( nSeg<0 ) break; + + /* Open a cursor to iterate through the contents of the oldest nSeg + ** indexes of absolute level iAbsLevel. If this cursor is opened using + ** the 'hint' parameters, it is possible that there are less than nSeg + ** segments available in level iAbsLevel. In this case, no work is + ** done on iAbsLevel - fall through to the next iteration of the loop + ** to start work on some other level. */ + memset(pWriter, 0, nAlloc); + pFilter->flags = FTS3_SEGMENT_REQUIRE_POS; + + if( rc==SQLITE_OK ){ + rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx); + assert( bUseHint==1 || bUseHint==0 ); + if( iIdx==0 || (bUseHint && iIdx==1) ){ + int bIgnore = 0; + rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore); + if( bIgnore ){ + pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY; + } + } + } + + if( rc==SQLITE_OK ){ + rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr); + } + if( SQLITE_OK==rc && pCsr->nSegment==nSeg + && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter)) + && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr)) + ){ + if( bUseHint && iIdx>0 ){ + const char *zKey = pCsr->zTerm; + int nKey = pCsr->nTerm; + rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter); + }else{ + rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter); + } + + if( rc==SQLITE_OK && pWriter->nLeafEst ){ + fts3LogMerge(nSeg, iAbsLevel); + do { + rc = fts3IncrmergeAppend(p, pWriter, pCsr); + if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr); + if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK; + }while( rc==SQLITE_ROW ); + + /* Update or delete the input segments */ + if( rc==SQLITE_OK ){ + nRem -= (1 + pWriter->nWork); + rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg); + if( nSeg!=0 ){ + bDirtyHint = 1; + fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc); + } + } + } + + if( nSeg!=0 ){ + pWriter->nLeafData = pWriter->nLeafData * -1; + } + fts3IncrmergeRelease(p, pWriter, &rc); + if( nSeg==0 && pWriter->bNoLeafData==0 ){ + fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData); + } + } + + sqlite3Fts3SegReaderFinish(pCsr); + } + + /* Write the hint values into the %_stat table for the next incr-merger */ + if( bDirtyHint && rc==SQLITE_OK ){ + rc = fts3IncrmergeHintStore(p, &hint); + } + + sqlite3_free(pWriter); + sqlite3_free(hint.a); + return rc; +} + +/* +** Convert the text beginning at *pz into an integer and return +** its value. Advance *pz to point to the first character past +** the integer. +*/ +static int fts3Getint(const char **pz){ + const char *z = *pz; + int i = 0; + while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0'; + *pz = z; + return i; +} + +/* +** Process statements of the form: +** +** INSERT INTO table(table) VALUES('merge=A,B'); +** +** A and B are integers that decode to be the number of leaf pages +** written for the merge, and the minimum number of segments on a level +** before it will be selected for a merge, respectively. +*/ +static int fts3DoIncrmerge( + Fts3Table *p, /* FTS3 table handle */ + const char *zParam /* Nul-terminated string containing "A,B" */ +){ + int rc; + int nMin = (FTS3_MERGE_COUNT / 2); + int nMerge = 0; + const char *z = zParam; + + /* Read the first integer value */ + nMerge = fts3Getint(&z); + + /* If the first integer value is followed by a ',', read the second + ** integer value. */ + if( z[0]==',' && z[1]!='\0' ){ + z++; + nMin = fts3Getint(&z); + } + + if( z[0]!='\0' || nMin<2 ){ + rc = SQLITE_ERROR; + }else{ + rc = SQLITE_OK; + if( !p->bHasStat ){ + assert( p->bFts4==0 ); + sqlite3Fts3CreateStatTable(&rc, p); + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3Incrmerge(p, nMerge, nMin); + } + sqlite3Fts3SegmentsClose(p); + } + return rc; +} + +/* +** Process statements of the form: +** +** INSERT INTO table(table) VALUES('automerge=X'); +** +** where X is an integer. X==0 means to turn automerge off. X!=0 means +** turn it on. The setting is persistent. +*/ +static int fts3DoAutoincrmerge( + Fts3Table *p, /* FTS3 table handle */ + const char *zParam /* Nul-terminated string containing boolean */ +){ + int rc = SQLITE_OK; + sqlite3_stmt *pStmt = 0; + p->nAutoincrmerge = fts3Getint(&zParam); + if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){ + p->nAutoincrmerge = 8; + } + if( !p->bHasStat ){ + assert( p->bFts4==0 ); + sqlite3Fts3CreateStatTable(&rc, p); + if( rc ) return rc; + } + rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0); + if( rc ) return rc; + sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE); + sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + return rc; +} + +/* +** Return a 64-bit checksum for the FTS index entry specified by the +** arguments to this function. +*/ +static u64 fts3ChecksumEntry( + const char *zTerm, /* Pointer to buffer containing term */ + int nTerm, /* Size of zTerm in bytes */ + int iLangid, /* Language id for current row */ + int iIndex, /* Index (0..Fts3Table.nIndex-1) */ + i64 iDocid, /* Docid for current row. */ + int iCol, /* Column number */ + int iPos /* Position */ +){ + int i; + u64 ret = (u64)iDocid; + + ret += (ret<<3) + iLangid; + ret += (ret<<3) + iIndex; + ret += (ret<<3) + iCol; + ret += (ret<<3) + iPos; + for(i=0; inIndex-1) */ + int *pRc /* OUT: Return code */ +){ + Fts3SegFilter filter; + Fts3MultiSegReader csr; + int rc; + u64 cksum = 0; + + assert( *pRc==SQLITE_OK ); + + memset(&filter, 0, sizeof(filter)); + memset(&csr, 0, sizeof(csr)); + filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; + filter.flags |= FTS3_SEGMENT_SCAN; + + rc = sqlite3Fts3SegReaderCursor( + p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr + ); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3SegReaderStart(p, &csr, &filter); + } + + if( rc==SQLITE_OK ){ + while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){ + char *pCsr = csr.aDoclist; + char *pEnd = &pCsr[csr.nDoclist]; + + i64 iDocid = 0; + i64 iCol = 0; + i64 iPos = 0; + + pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid); + while( pCsriPrevLangid); + sqlite3_bind_int(pAllLangid, 2, p->nIndex); + while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){ + int iLangid = sqlite3_column_int(pAllLangid, 0); + int i; + for(i=0; inIndex; i++){ + cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc); + } + } + rc2 = sqlite3_reset(pAllLangid); + if( rc==SQLITE_OK ) rc = rc2; + } + + /* This block calculates the checksum according to the %_content table */ + if( rc==SQLITE_OK ){ + sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule; + sqlite3_stmt *pStmt = 0; + char *zSql; + + zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + } + + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + i64 iDocid = sqlite3_column_int64(pStmt, 0); + int iLang = langidFromSelect(p, pStmt); + int iCol; + + for(iCol=0; rc==SQLITE_OK && iColnColumn; iCol++){ + if( p->abNotindexed[iCol]==0 ){ + const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1); + int nText = sqlite3_column_bytes(pStmt, iCol+1); + sqlite3_tokenizer_cursor *pT = 0; + + rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT); + while( rc==SQLITE_OK ){ + char const *zToken; /* Buffer containing token */ + int nToken = 0; /* Number of bytes in token */ + int iDum1 = 0, iDum2 = 0; /* Dummy variables */ + int iPos = 0; /* Position of token in zText */ + + rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos); + if( rc==SQLITE_OK ){ + int i; + cksum2 = cksum2 ^ fts3ChecksumEntry( + zToken, nToken, iLang, 0, iDocid, iCol, iPos + ); + for(i=1; inIndex; i++){ + if( p->aIndex[i].nPrefix<=nToken ){ + cksum2 = cksum2 ^ fts3ChecksumEntry( + zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos + ); + } + } + } + } + if( pT ) pModule->xClose(pT); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + } + } + + sqlite3_finalize(pStmt); + } + + *pbOk = (cksum1==cksum2); + return rc; +} + +/* +** Run the integrity-check. If no error occurs and the current contents of +** the FTS index are correct, return SQLITE_OK. Or, if the contents of the +** FTS index are incorrect, return SQLITE_CORRUPT_VTAB. +** +** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite +** error code. +** +** The integrity-check works as follows. For each token and indexed token +** prefix in the document set, a 64-bit checksum is calculated (by code +** in fts3ChecksumEntry()) based on the following: +** +** + The index number (0 for the main index, 1 for the first prefix +** index etc.), +** + The token (or token prefix) text itself, +** + The language-id of the row it appears in, +** + The docid of the row it appears in, +** + The column it appears in, and +** + The tokens position within that column. +** +** The checksums for all entries in the index are XORed together to create +** a single checksum for the entire index. +** +** The integrity-check code calculates the same checksum in two ways: +** +** 1. By scanning the contents of the FTS index, and +** 2. By scanning and tokenizing the content table. +** +** If the two checksums are identical, the integrity-check is deemed to have +** passed. +*/ +static int fts3DoIntegrityCheck( + Fts3Table *p /* FTS3 table handle */ +){ + int rc; + int bOk = 0; + rc = fts3IntegrityCheck(p, &bOk); + if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB; + return rc; +} + +/* +** Handle a 'special' INSERT of the form: +** +** "INSERT INTO tbl(tbl) VALUES()" +** +** Argument pVal contains the result of . Currently the only +** meaningful value to insert is the text 'optimize'. +*/ +static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){ + int rc; /* Return Code */ + const char *zVal = (const char *)sqlite3_value_text(pVal); + int nVal = sqlite3_value_bytes(pVal); + + if( !zVal ){ + return SQLITE_NOMEM; + }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){ + rc = fts3DoOptimize(p, 0); + }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){ + rc = fts3DoRebuild(p); + }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){ + rc = fts3DoIntegrityCheck(p); + }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){ + rc = fts3DoIncrmerge(p, &zVal[6]); + }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){ + rc = fts3DoAutoincrmerge(p, &zVal[10]); +#ifdef SQLITE_TEST + }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){ + p->nNodeSize = atoi(&zVal[9]); + rc = SQLITE_OK; + }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){ + p->nMaxPendingData = atoi(&zVal[11]); + rc = SQLITE_OK; + }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){ + p->bNoIncrDoclist = atoi(&zVal[21]); + rc = SQLITE_OK; +#endif + }else{ + rc = SQLITE_ERROR; + } + + return rc; +} + +#ifndef SQLITE_DISABLE_FTS4_DEFERRED +/* +** Delete all cached deferred doclists. Deferred doclists are cached +** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){ + Fts3DeferredToken *pDef; + for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){ + fts3PendingListDelete(pDef->pList); + pDef->pList = 0; + } +} + +/* +** Free all entries in the pCsr->pDeffered list. Entries are added to +** this list using sqlite3Fts3DeferToken(). +*/ +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){ + Fts3DeferredToken *pDef; + Fts3DeferredToken *pNext; + for(pDef=pCsr->pDeferred; pDef; pDef=pNext){ + pNext = pDef->pNext; + fts3PendingListDelete(pDef->pList); + sqlite3_free(pDef); + } + pCsr->pDeferred = 0; +} + +/* +** Generate deferred-doclists for all tokens in the pCsr->pDeferred list +** based on the row that pCsr currently points to. +** +** A deferred-doclist is like any other doclist with position information +** included, except that it only contains entries for a single row of the +** table, not for all rows. +*/ +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){ + int rc = SQLITE_OK; /* Return code */ + if( pCsr->pDeferred ){ + int i; /* Used to iterate through table columns */ + sqlite3_int64 iDocid; /* Docid of the row pCsr points to */ + Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */ + + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + sqlite3_tokenizer *pT = p->pTokenizer; + sqlite3_tokenizer_module const *pModule = pT->pModule; + + assert( pCsr->isRequireSeek==0 ); + iDocid = sqlite3_column_int64(pCsr->pStmt, 0); + + for(i=0; inColumn && rc==SQLITE_OK; i++){ + if( p->abNotindexed[i]==0 ){ + const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1); + sqlite3_tokenizer_cursor *pTC = 0; + + rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC); + while( rc==SQLITE_OK ){ + char const *zToken; /* Buffer containing token */ + int nToken = 0; /* Number of bytes in token */ + int iDum1 = 0, iDum2 = 0; /* Dummy variables */ + int iPos = 0; /* Position of token in zText */ + + rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos); + for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){ + Fts3PhraseToken *pPT = pDef->pToken; + if( (pDef->iCol>=p->nColumn || pDef->iCol==i) + && (pPT->bFirst==0 || iPos==0) + && (pPT->n==nToken || (pPT->isPrefix && pPT->nz, pPT->n)) + ){ + fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc); + } + } + } + if( pTC ) pModule->xClose(pTC); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + } + + for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){ + if( pDef->pList ){ + rc = fts3PendingListAppendVarint(&pDef->pList, 0); + } + } + } + + return rc; +} + +SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList( + Fts3DeferredToken *p, + char **ppData, + int *pnData +){ + char *pRet; + int nSkip; + sqlite3_int64 dummy; + + *ppData = 0; + *pnData = 0; + + if( p->pList==0 ){ + return SQLITE_OK; + } + + pRet = (char *)sqlite3_malloc(p->pList->nData); + if( !pRet ) return SQLITE_NOMEM; + + nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy); + *pnData = p->pList->nData - nSkip; + *ppData = pRet; + + memcpy(pRet, &p->pList->aData[nSkip], *pnData); + return SQLITE_OK; +} + +/* +** Add an entry for token pToken to the pCsr->pDeferred list. +*/ +SQLITE_PRIVATE int sqlite3Fts3DeferToken( + Fts3Cursor *pCsr, /* Fts3 table cursor */ + Fts3PhraseToken *pToken, /* Token to defer */ + int iCol /* Column that token must appear in (or -1) */ +){ + Fts3DeferredToken *pDeferred; + pDeferred = sqlite3_malloc(sizeof(*pDeferred)); + if( !pDeferred ){ + return SQLITE_NOMEM; + } + memset(pDeferred, 0, sizeof(*pDeferred)); + pDeferred->pToken = pToken; + pDeferred->pNext = pCsr->pDeferred; + pDeferred->iCol = iCol; + pCsr->pDeferred = pDeferred; + + assert( pToken->pDeferred==0 ); + pToken->pDeferred = pDeferred; + + return SQLITE_OK; +} +#endif + +/* +** SQLite value pRowid contains the rowid of a row that may or may not be +** present in the FTS3 table. If it is, delete it and adjust the contents +** of subsiduary data structures accordingly. +*/ +static int fts3DeleteByRowid( + Fts3Table *p, + sqlite3_value *pRowid, + int *pnChng, /* IN/OUT: Decrement if row is deleted */ + u32 *aSzDel +){ + int rc = SQLITE_OK; /* Return code */ + int bFound = 0; /* True if *pRowid really is in the table */ + + fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound); + if( bFound && rc==SQLITE_OK ){ + int isEmpty = 0; /* Deleting *pRowid leaves the table empty */ + rc = fts3IsEmpty(p, pRowid, &isEmpty); + if( rc==SQLITE_OK ){ + if( isEmpty ){ + /* Deleting this row means the whole table is empty. In this case + ** delete the contents of all three tables and throw away any + ** data in the pendingTerms hash table. */ + rc = fts3DeleteAll(p, 1); + *pnChng = 0; + memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2); + }else{ + *pnChng = *pnChng - 1; + if( p->zContentTbl==0 ){ + fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid); + } + if( p->bHasDocsize ){ + fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid); + } + } + } + } + + return rc; +} + +/* +** This function does the work for the xUpdate method of FTS3 virtual +** tables. The schema of the virtual table being: +** +** CREATE TABLE
    ( +** , +**
    HIDDEN, +** docid HIDDEN, +** HIDDEN +** ); +** +** +*/ +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod( + sqlite3_vtab *pVtab, /* FTS3 vtab object */ + int nArg, /* Size of argument array */ + sqlite3_value **apVal, /* Array of arguments */ + sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ +){ + Fts3Table *p = (Fts3Table *)pVtab; + int rc = SQLITE_OK; /* Return Code */ + int isRemove = 0; /* True for an UPDATE or DELETE */ + u32 *aSzIns = 0; /* Sizes of inserted documents */ + u32 *aSzDel = 0; /* Sizes of deleted documents */ + int nChng = 0; /* Net change in number of documents */ + int bInsertDone = 0; + + /* At this point it must be known if the %_stat table exists or not. + ** So bHasStat may not be 2. */ + assert( p->bHasStat==0 || p->bHasStat==1 ); + + assert( p->pSegments==0 ); + assert( + nArg==1 /* DELETE operations */ + || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */ + ); + + /* Check for a "special" INSERT operation. One of the form: + ** + ** INSERT INTO xyz(xyz) VALUES('command'); + */ + if( nArg>1 + && sqlite3_value_type(apVal[0])==SQLITE_NULL + && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL + ){ + rc = fts3SpecialInsert(p, apVal[p->nColumn+2]); + goto update_out; + } + + if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){ + rc = SQLITE_CONSTRAINT; + goto update_out; + } + + /* Allocate space to hold the change in document sizes */ + aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 ); + if( aSzDel==0 ){ + rc = SQLITE_NOMEM; + goto update_out; + } + aSzIns = &aSzDel[p->nColumn+1]; + memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2); + + rc = fts3Writelock(p); + if( rc!=SQLITE_OK ) goto update_out; + + /* If this is an INSERT operation, or an UPDATE that modifies the rowid + ** value, then this operation requires constraint handling. + ** + ** If the on-conflict mode is REPLACE, this means that the existing row + ** should be deleted from the database before inserting the new row. Or, + ** if the on-conflict mode is other than REPLACE, then this method must + ** detect the conflict and return SQLITE_CONSTRAINT before beginning to + ** modify the database file. + */ + if( nArg>1 && p->zContentTbl==0 ){ + /* Find the value object that holds the new rowid value. */ + sqlite3_value *pNewRowid = apVal[3+p->nColumn]; + if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){ + pNewRowid = apVal[1]; + } + + if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( + sqlite3_value_type(apVal[0])==SQLITE_NULL + || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid) + )){ + /* The new rowid is not NULL (in this case the rowid will be + ** automatically assigned and there is no chance of a conflict), and + ** the statement is either an INSERT or an UPDATE that modifies the + ** rowid column. So if the conflict mode is REPLACE, then delete any + ** existing row with rowid=pNewRowid. + ** + ** Or, if the conflict mode is not REPLACE, insert the new record into + ** the %_content table. If we hit the duplicate rowid constraint (or any + ** other error) while doing so, return immediately. + ** + ** This branch may also run if pNewRowid contains a value that cannot + ** be losslessly converted to an integer. In this case, the eventual + ** call to fts3InsertData() (either just below or further on in this + ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is + ** invoked, it will delete zero rows (since no row will have + ** docid=$pNewRowid if $pNewRowid is not an integer value). + */ + if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){ + rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel); + }else{ + rc = fts3InsertData(p, apVal, pRowid); + bInsertDone = 1; + } + } + } + if( rc!=SQLITE_OK ){ + goto update_out; + } + + /* If this is a DELETE or UPDATE operation, remove the old record. */ + if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ + assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER ); + rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel); + isRemove = 1; + } + + /* If this is an INSERT or UPDATE operation, insert the new record. */ + if( nArg>1 && rc==SQLITE_OK ){ + int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]); + if( bInsertDone==0 ){ + rc = fts3InsertData(p, apVal, pRowid); + if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){ + rc = FTS_CORRUPT_VTAB; + } + } + if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){ + rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid); + } + if( rc==SQLITE_OK ){ + assert( p->iPrevDocid==*pRowid ); + rc = fts3InsertTerms(p, iLangid, apVal, aSzIns); + } + if( p->bHasDocsize ){ + fts3InsertDocsize(&rc, p, aSzIns); + } + nChng++; + } + + if( p->bFts4 ){ + fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng); + } + + update_out: + sqlite3_free(aSzDel); + sqlite3Fts3SegmentsClose(p); + return rc; +} + +/* +** Flush any data in the pending-terms hash table to disk. If successful, +** merge all segments in the database (including the new segment, if +** there was any data to flush) into a single segment. +*/ +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){ + int rc; + rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0); + if( rc==SQLITE_OK ){ + rc = fts3DoOptimize(p, 1); + if( rc==SQLITE_OK || rc==SQLITE_DONE ){ + int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0); + if( rc2!=SQLITE_OK ) rc = rc2; + }else{ + sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0); + sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0); + } + } + sqlite3Fts3SegmentsClose(p); + return rc; +} + +#endif + +/************** End of fts3_write.c ******************************************/ +/************** Begin file fts3_snippet.c ************************************/ +/* +** 2009 Oct 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +*/ + +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ + +/* +** Characters that may appear in the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ +#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */ +#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ +#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ +#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ +#define FTS3_MATCHINFO_LCS 's' /* nCol values */ +#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ +#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */ +#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */ + +/* +** The default value for the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_DEFAULT "pcx" + + +/* +** Used as an fts3ExprIterate() context when loading phrase doclists to +** Fts3Expr.aDoclist[]/nDoclist. +*/ +typedef struct LoadDoclistCtx LoadDoclistCtx; +struct LoadDoclistCtx { + Fts3Cursor *pCsr; /* FTS3 Cursor */ + int nPhrase; /* Number of phrases seen so far */ + int nToken; /* Number of tokens seen so far */ +}; + +/* +** The following types are used as part of the implementation of the +** fts3BestSnippet() routine. +*/ +typedef struct SnippetIter SnippetIter; +typedef struct SnippetPhrase SnippetPhrase; +typedef struct SnippetFragment SnippetFragment; + +struct SnippetIter { + Fts3Cursor *pCsr; /* Cursor snippet is being generated from */ + int iCol; /* Extract snippet from this column */ + int nSnippet; /* Requested snippet length (in tokens) */ + int nPhrase; /* Number of phrases in query */ + SnippetPhrase *aPhrase; /* Array of size nPhrase */ + int iCurrent; /* First token of current snippet */ +}; + +struct SnippetPhrase { + int nToken; /* Number of tokens in phrase */ + char *pList; /* Pointer to start of phrase position list */ + int iHead; /* Next value in position list */ + char *pHead; /* Position list data following iHead */ + int iTail; /* Next value in trailing position list */ + char *pTail; /* Position list data following iTail */ +}; + +struct SnippetFragment { + int iCol; /* Column snippet is extracted from */ + int iPos; /* Index of first token in snippet */ + u64 covered; /* Mask of query phrases covered */ + u64 hlmask; /* Mask of snippet terms to highlight */ +}; + +/* +** This type is used as an fts3ExprIterate() context object while +** accumulating the data returned by the matchinfo() function. +*/ +typedef struct MatchInfo MatchInfo; +struct MatchInfo { + Fts3Cursor *pCursor; /* FTS3 Cursor */ + int nCol; /* Number of columns in table */ + int nPhrase; /* Number of matchable phrases in query */ + sqlite3_int64 nDoc; /* Number of docs in database */ + char flag; + u32 *aMatchinfo; /* Pre-allocated buffer */ +}; + +/* +** An instance of this structure is used to manage a pair of buffers, each +** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below +** for details. +*/ +struct MatchinfoBuffer { + u8 aRef[3]; + int nElem; + int bGlobal; /* Set if global data is loaded */ + char *zMatchinfo; + u32 aMatchinfo[1]; +}; + + +/* +** The snippet() and offsets() functions both return text values. An instance +** of the following structure is used to accumulate those values while the +** functions are running. See fts3StringAppend() for details. +*/ +typedef struct StrBuffer StrBuffer; +struct StrBuffer { + char *z; /* Pointer to buffer containing string */ + int n; /* Length of z in bytes (excl. nul-term) */ + int nAlloc; /* Allocated size of buffer z in bytes */ +}; + + +/************************************************************************* +** Start of MatchinfoBuffer code. +*/ + +/* +** Allocate a two-slot MatchinfoBuffer object. +*/ +static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){ + MatchinfoBuffer *pRet; + int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer); + int nStr = (int)strlen(zMatchinfo); + + pRet = sqlite3_malloc(nByte + nStr+1); + if( pRet ){ + memset(pRet, 0, nByte); + pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet; + pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1); + pRet->nElem = nElem; + pRet->zMatchinfo = ((char*)pRet) + nByte; + memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1); + pRet->aRef[0] = 1; + } + + return pRet; +} + +static void fts3MIBufferFree(void *p){ + MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]); + + assert( (u32*)p==&pBuf->aMatchinfo[1] + || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2] + ); + if( (u32*)p==&pBuf->aMatchinfo[1] ){ + pBuf->aRef[1] = 0; + }else{ + pBuf->aRef[2] = 0; + } + + if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){ + sqlite3_free(pBuf); + } +} + +static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){ + void (*xRet)(void*) = 0; + u32 *aOut = 0; + + if( p->aRef[1]==0 ){ + p->aRef[1] = 1; + aOut = &p->aMatchinfo[1]; + xRet = fts3MIBufferFree; + } + else if( p->aRef[2]==0 ){ + p->aRef[2] = 1; + aOut = &p->aMatchinfo[p->nElem+2]; + xRet = fts3MIBufferFree; + }else{ + aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32)); + if( aOut ){ + xRet = sqlite3_free; + if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32)); + } + } + + *paOut = aOut; + return xRet; +} + +static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){ + p->bGlobal = 1; + memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32)); +} + +/* +** Free a MatchinfoBuffer object allocated using fts3MIBufferNew() +*/ +SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){ + if( p ){ + assert( p->aRef[0]==1 ); + p->aRef[0] = 0; + if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){ + sqlite3_free(p); + } + } +} + +/* +** End of MatchinfoBuffer code. +*************************************************************************/ + + +/* +** This function is used to help iterate through a position-list. A position +** list is a list of unique integers, sorted from smallest to largest. Each +** element of the list is represented by an FTS3 varint that takes the value +** of the difference between the current element and the previous one plus +** two. For example, to store the position-list: +** +** 4 9 113 +** +** the three varints: +** +** 6 7 106 +** +** are encoded. +** +** When this function is called, *pp points to the start of an element of +** the list. *piPos contains the value of the previous entry in the list. +** After it returns, *piPos contains the value of the next element of the +** list and *pp is advanced to the following varint. +*/ +static void fts3GetDeltaPosition(char **pp, int *piPos){ + int iVal; + *pp += fts3GetVarint32(*pp, &iVal); + *piPos += (iVal-2); +} + +/* +** Helper function for fts3ExprIterate() (see below). +*/ +static int fts3ExprIterate2( + Fts3Expr *pExpr, /* Expression to iterate phrases of */ + int *piPhrase, /* Pointer to phrase counter */ + int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ + void *pCtx /* Second argument to pass to callback */ +){ + int rc; /* Return code */ + int eType = pExpr->eType; /* Type of expression node pExpr */ + + if( eType!=FTSQUERY_PHRASE ){ + assert( pExpr->pLeft && pExpr->pRight ); + rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); + if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ + rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx); + } + }else{ + rc = x(pExpr, *piPhrase, pCtx); + (*piPhrase)++; + } + return rc; +} + +/* +** Iterate through all phrase nodes in an FTS3 query, except those that +** are part of a sub-tree that is the right-hand-side of a NOT operator. +** For each phrase node found, the supplied callback function is invoked. +** +** If the callback function returns anything other than SQLITE_OK, +** the iteration is abandoned and the error code returned immediately. +** Otherwise, SQLITE_OK is returned after a callback has been made for +** all eligible phrase nodes. +*/ +static int fts3ExprIterate( + Fts3Expr *pExpr, /* Expression to iterate phrases of */ + int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ + void *pCtx /* Second argument to pass to callback */ +){ + int iPhrase = 0; /* Variable used as the phrase counter */ + return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); +} + + +/* +** This is an fts3ExprIterate() callback used while loading the doclists +** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also +** fts3ExprLoadDoclists(). +*/ +static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ + int rc = SQLITE_OK; + Fts3Phrase *pPhrase = pExpr->pPhrase; + LoadDoclistCtx *p = (LoadDoclistCtx *)ctx; + + UNUSED_PARAMETER(iPhrase); + + p->nPhrase++; + p->nToken += pPhrase->nToken; + + return rc; +} + +/* +** Load the doclists for each phrase in the query associated with FTS3 cursor +** pCsr. +** +** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable +** phrases in the expression (all phrases except those directly or +** indirectly descended from the right-hand-side of a NOT operator). If +** pnToken is not NULL, then it is set to the number of tokens in all +** matchable phrases of the expression. +*/ +static int fts3ExprLoadDoclists( + Fts3Cursor *pCsr, /* Fts3 cursor for current query */ + int *pnPhrase, /* OUT: Number of phrases in query */ + int *pnToken /* OUT: Number of tokens in query */ +){ + int rc; /* Return Code */ + LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ + sCtx.pCsr = pCsr; + rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx); + if( pnPhrase ) *pnPhrase = sCtx.nPhrase; + if( pnToken ) *pnToken = sCtx.nToken; + return rc; +} + +static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ + (*(int *)ctx)++; + pExpr->iPhrase = iPhrase; + return SQLITE_OK; +} +static int fts3ExprPhraseCount(Fts3Expr *pExpr){ + int nPhrase = 0; + (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); + return nPhrase; +} + +/* +** Advance the position list iterator specified by the first two +** arguments so that it points to the first element with a value greater +** than or equal to parameter iNext. +*/ +static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){ + char *pIter = *ppIter; + if( pIter ){ + int iIter = *piIter; + + while( iIteriCurrent<0 ){ + /* The SnippetIter object has just been initialized. The first snippet + ** candidate always starts at offset 0 (even if this candidate has a + ** score of 0.0). + */ + pIter->iCurrent = 0; + + /* Advance the 'head' iterator of each phrase to the first offset that + ** is greater than or equal to (iNext+nSnippet). + */ + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet); + } + }else{ + int iStart; + int iEnd = 0x7FFFFFFF; + + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + if( pPhrase->pHead && pPhrase->iHeadiHead; + } + } + if( iEnd==0x7FFFFFFF ){ + return 1; + } + + pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1; + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1); + fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart); + } + } + + return 0; +} + +/* +** Retrieve information about the current candidate snippet of snippet +** iterator pIter. +*/ +static void fts3SnippetDetails( + SnippetIter *pIter, /* Snippet iterator */ + u64 mCovered, /* Bitmask of phrases already covered */ + int *piToken, /* OUT: First token of proposed snippet */ + int *piScore, /* OUT: "Score" for this snippet */ + u64 *pmCover, /* OUT: Bitmask of phrases covered */ + u64 *pmHighlight /* OUT: Bitmask of terms to highlight */ +){ + int iStart = pIter->iCurrent; /* First token of snippet */ + int iScore = 0; /* Score of this snippet */ + int i; /* Loop counter */ + u64 mCover = 0; /* Mask of phrases covered by this snippet */ + u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */ + + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + if( pPhrase->pTail ){ + char *pCsr = pPhrase->pTail; + int iCsr = pPhrase->iTail; + + while( iCsr<(iStart+pIter->nSnippet) ){ + int j; + u64 mPhrase = (u64)1 << i; + u64 mPos = (u64)1 << (iCsr - iStart); + assert( iCsr>=iStart ); + if( (mCover|mCovered)&mPhrase ){ + iScore++; + }else{ + iScore += 1000; + } + mCover |= mPhrase; + + for(j=0; jnToken; j++){ + mHighlight |= (mPos>>j); + } + + if( 0==(*pCsr & 0x0FE) ) break; + fts3GetDeltaPosition(&pCsr, &iCsr); + } + } + } + + /* Set the output variables before returning. */ + *piToken = iStart; + *piScore = iScore; + *pmCover = mCover; + *pmHighlight = mHighlight; +} + +/* +** This function is an fts3ExprIterate() callback used by fts3BestSnippet(). +** Each invocation populates an element of the SnippetIter.aPhrase[] array. +*/ +static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){ + SnippetIter *p = (SnippetIter *)ctx; + SnippetPhrase *pPhrase = &p->aPhrase[iPhrase]; + char *pCsr; + int rc; + + pPhrase->nToken = pExpr->pPhrase->nToken; + rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr); + assert( rc==SQLITE_OK || pCsr==0 ); + if( pCsr ){ + int iFirst = 0; + pPhrase->pList = pCsr; + fts3GetDeltaPosition(&pCsr, &iFirst); + assert( iFirst>=0 ); + pPhrase->pHead = pCsr; + pPhrase->pTail = pCsr; + pPhrase->iHead = iFirst; + pPhrase->iTail = iFirst; + }else{ + assert( rc!=SQLITE_OK || ( + pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 + )); + } + + return rc; +} + +/* +** Select the fragment of text consisting of nFragment contiguous tokens +** from column iCol that represent the "best" snippet. The best snippet +** is the snippet with the highest score, where scores are calculated +** by adding: +** +** (a) +1 point for each occurrence of a matchable phrase in the snippet. +** +** (b) +1000 points for the first occurrence of each matchable phrase in +** the snippet for which the corresponding mCovered bit is not set. +** +** The selected snippet parameters are stored in structure *pFragment before +** returning. The score of the selected snippet is stored in *piScore +** before returning. +*/ +static int fts3BestSnippet( + int nSnippet, /* Desired snippet length */ + Fts3Cursor *pCsr, /* Cursor to create snippet for */ + int iCol, /* Index of column to create snippet from */ + u64 mCovered, /* Mask of phrases already covered */ + u64 *pmSeen, /* IN/OUT: Mask of phrases seen */ + SnippetFragment *pFragment, /* OUT: Best snippet found */ + int *piScore /* OUT: Score of snippet pFragment */ +){ + int rc; /* Return Code */ + int nList; /* Number of phrases in expression */ + SnippetIter sIter; /* Iterates through snippet candidates */ + int nByte; /* Number of bytes of space to allocate */ + int iBestScore = -1; /* Best snippet score found so far */ + int i; /* Loop counter */ + + memset(&sIter, 0, sizeof(sIter)); + + /* Iterate through the phrases in the expression to count them. The same + ** callback makes sure the doclists are loaded for each phrase. + */ + rc = fts3ExprLoadDoclists(pCsr, &nList, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Now that it is known how many phrases there are, allocate and zero + ** the required space using malloc(). + */ + nByte = sizeof(SnippetPhrase) * nList; + sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte); + if( !sIter.aPhrase ){ + return SQLITE_NOMEM; + } + memset(sIter.aPhrase, 0, nByte); + + /* Initialize the contents of the SnippetIter object. Then iterate through + ** the set of phrases in the expression to populate the aPhrase[] array. + */ + sIter.pCsr = pCsr; + sIter.iCol = iCol; + sIter.nSnippet = nSnippet; + sIter.nPhrase = nList; + sIter.iCurrent = -1; + rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter); + if( rc==SQLITE_OK ){ + + /* Set the *pmSeen output variable. */ + for(i=0; iiCol = iCol; + while( !fts3SnippetNextCandidate(&sIter) ){ + int iPos; + int iScore; + u64 mCover; + u64 mHighlite; + fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite); + assert( iScore>=0 ); + if( iScore>iBestScore ){ + pFragment->iPos = iPos; + pFragment->hlmask = mHighlite; + pFragment->covered = mCover; + iBestScore = iScore; + } + } + + *piScore = iBestScore; + } + sqlite3_free(sIter.aPhrase); + return rc; +} + + +/* +** Append a string to the string-buffer passed as the first argument. +** +** If nAppend is negative, then the length of the string zAppend is +** determined using strlen(). +*/ +static int fts3StringAppend( + StrBuffer *pStr, /* Buffer to append to */ + const char *zAppend, /* Pointer to data to append to buffer */ + int nAppend /* Size of zAppend in bytes (or -1) */ +){ + if( nAppend<0 ){ + nAppend = (int)strlen(zAppend); + } + + /* If there is insufficient space allocated at StrBuffer.z, use realloc() + ** to grow the buffer until so that it is big enough to accomadate the + ** appended data. + */ + if( pStr->n+nAppend+1>=pStr->nAlloc ){ + int nAlloc = pStr->nAlloc+nAppend+100; + char *zNew = sqlite3_realloc(pStr->z, nAlloc); + if( !zNew ){ + return SQLITE_NOMEM; + } + pStr->z = zNew; + pStr->nAlloc = nAlloc; + } + assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) ); + + /* Append the data to the string buffer. */ + memcpy(&pStr->z[pStr->n], zAppend, nAppend); + pStr->n += nAppend; + pStr->z[pStr->n] = '\0'; + + return SQLITE_OK; +} + +/* +** The fts3BestSnippet() function often selects snippets that end with a +** query term. That is, the final term of the snippet is always a term +** that requires highlighting. For example, if 'X' is a highlighted term +** and '.' is a non-highlighted term, BestSnippet() may select: +** +** ........X.....X +** +** This function "shifts" the beginning of the snippet forward in the +** document so that there are approximately the same number of +** non-highlighted terms to the right of the final highlighted term as there +** are to the left of the first highlighted term. For example, to this: +** +** ....X.....X.... +** +** This is done as part of extracting the snippet text, not when selecting +** the snippet. Snippet selection is done based on doclists only, so there +** is no way for fts3BestSnippet() to know whether or not the document +** actually contains terms that follow the final highlighted term. +*/ +static int fts3SnippetShift( + Fts3Table *pTab, /* FTS3 table snippet comes from */ + int iLangid, /* Language id to use in tokenizing */ + int nSnippet, /* Number of tokens desired for snippet */ + const char *zDoc, /* Document text to extract snippet from */ + int nDoc, /* Size of buffer zDoc in bytes */ + int *piPos, /* IN/OUT: First token of snippet */ + u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */ +){ + u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */ + + if( hlmask ){ + int nLeft; /* Tokens to the left of first highlight */ + int nRight; /* Tokens to the right of last highlight */ + int nDesired; /* Ideal number of tokens to shift forward */ + + for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++); + for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++); + nDesired = (nLeft-nRight)/2; + + /* Ideally, the start of the snippet should be pushed forward in the + ** document nDesired tokens. This block checks if there are actually + ** nDesired tokens to the right of the snippet. If so, *piPos and + ** *pHlMask are updated to shift the snippet nDesired tokens to the + ** right. Otherwise, the snippet is shifted by the number of tokens + ** available. + */ + if( nDesired>0 ){ + int nShift; /* Number of tokens to shift snippet by */ + int iCurrent = 0; /* Token counter */ + int rc; /* Return Code */ + sqlite3_tokenizer_module *pMod; + sqlite3_tokenizer_cursor *pC; + pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; + + /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired) + ** or more tokens in zDoc/nDoc. + */ + rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC); + if( rc!=SQLITE_OK ){ + return rc; + } + while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){ + const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0; + rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent); + } + pMod->xClose(pC); + if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; } + + nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet; + assert( nShift<=nDesired ); + if( nShift>0 ){ + *piPos += nShift; + *pHlmask = hlmask >> nShift; + } + } + } + return SQLITE_OK; +} + +/* +** Extract the snippet text for fragment pFragment from cursor pCsr and +** append it to string buffer pOut. +*/ +static int fts3SnippetText( + Fts3Cursor *pCsr, /* FTS3 Cursor */ + SnippetFragment *pFragment, /* Snippet to extract */ + int iFragment, /* Fragment number */ + int isLast, /* True for final fragment in snippet */ + int nSnippet, /* Number of tokens in extracted snippet */ + const char *zOpen, /* String inserted before highlighted term */ + const char *zClose, /* String inserted after highlighted term */ + const char *zEllipsis, /* String inserted between snippets */ + StrBuffer *pOut /* Write output here */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc; /* Return code */ + const char *zDoc; /* Document text to extract snippet from */ + int nDoc; /* Size of zDoc in bytes */ + int iCurrent = 0; /* Current token number of document */ + int iEnd = 0; /* Byte offset of end of current token */ + int isShiftDone = 0; /* True after snippet is shifted */ + int iPos = pFragment->iPos; /* First token of snippet */ + u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */ + int iCol = pFragment->iCol+1; /* Query column to extract text from */ + sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */ + sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */ + + zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol); + if( zDoc==0 ){ + if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){ + return SQLITE_NOMEM; + } + return SQLITE_OK; + } + nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol); + + /* Open a token cursor on the document. */ + pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; + rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC); + if( rc!=SQLITE_OK ){ + return rc; + } + + while( rc==SQLITE_OK ){ + const char *ZDUMMY; /* Dummy argument used with tokenizer */ + int DUMMY1 = -1; /* Dummy argument used with tokenizer */ + int iBegin = 0; /* Offset in zDoc of start of token */ + int iFin = 0; /* Offset in zDoc of end of token */ + int isHighlight = 0; /* True for highlighted terms */ + + /* Variable DUMMY1 is initialized to a negative value above. Elsewhere + ** in the FTS code the variable that the third argument to xNext points to + ** is initialized to zero before the first (*but not necessarily + ** subsequent*) call to xNext(). This is done for a particular application + ** that needs to know whether or not the tokenizer is being used for + ** snippet generation or for some other purpose. + ** + ** Extreme care is required when writing code to depend on this + ** initialization. It is not a documented part of the tokenizer interface. + ** If a tokenizer is used directly by any code outside of FTS, this + ** convention might not be respected. */ + rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_DONE ){ + /* Special case - the last token of the snippet is also the last token + ** of the column. Append any punctuation that occurred between the end + ** of the previous token and the end of the document to the output. + ** Then break out of the loop. */ + rc = fts3StringAppend(pOut, &zDoc[iEnd], -1); + } + break; + } + if( iCurrentiLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask + ); + isShiftDone = 1; + + /* Now that the shift has been done, check if the initial "..." are + ** required. They are required if (a) this is not the first fragment, + ** or (b) this fragment does not begin at position 0 of its column. + */ + if( rc==SQLITE_OK ){ + if( iPos>0 || iFragment>0 ){ + rc = fts3StringAppend(pOut, zEllipsis, -1); + }else if( iBegin ){ + rc = fts3StringAppend(pOut, zDoc, iBegin); + } + } + if( rc!=SQLITE_OK || iCurrent=(iPos+nSnippet) ){ + if( isLast ){ + rc = fts3StringAppend(pOut, zEllipsis, -1); + } + break; + } + + /* Set isHighlight to true if this term should be highlighted. */ + isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0; + + if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd); + if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1); + if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin); + if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1); + + iEnd = iFin; + } + + pMod->xClose(pC); + return rc; +} + + +/* +** This function is used to count the entries in a column-list (a +** delta-encoded list of term offsets within a single column of a single +** row). When this function is called, *ppCollist should point to the +** beginning of the first varint in the column-list (the varint that +** contains the position of the first matching term in the column data). +** Before returning, *ppCollist is set to point to the first byte after +** the last varint in the column-list (either the 0x00 signifying the end +** of the position-list, or the 0x01 that precedes the column number of +** the next column in the position-list). +** +** The number of elements in the column-list is returned. +*/ +static int fts3ColumnlistCount(char **ppCollist){ + char *pEnd = *ppCollist; + char c = 0; + int nEntry = 0; + + /* A column-list is terminated by either a 0x01 or 0x00. */ + while( 0xFE & (*pEnd | c) ){ + c = *pEnd++ & 0x80; + if( !c ) nEntry++; + } + + *ppCollist = pEnd; + return nEntry; +} + +/* +** This function gathers 'y' or 'b' data for a single phrase. +*/ +static void fts3ExprLHits( + Fts3Expr *pExpr, /* Phrase expression node */ + MatchInfo *p /* Matchinfo context */ +){ + Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab; + int iStart; + Fts3Phrase *pPhrase = pExpr->pPhrase; + char *pIter = pPhrase->doclist.pList; + int iCol = 0; + + assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS ); + if( p->flag==FTS3_MATCHINFO_LHITS ){ + iStart = pExpr->iPhrase * p->nCol; + }else{ + iStart = pExpr->iPhrase * ((p->nCol + 31) / 32); + } + + while( 1 ){ + int nHit = fts3ColumnlistCount(&pIter); + if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ + if( p->flag==FTS3_MATCHINFO_LHITS ){ + p->aMatchinfo[iStart + iCol] = (u32)nHit; + }else if( nHit ){ + p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F)); + } + } + assert( *pIter==0x00 || *pIter==0x01 ); + if( *pIter!=0x01 ) break; + pIter++; + pIter += fts3GetVarint32(pIter, &iCol); + } +} + +/* +** Gather the results for matchinfo directives 'y' and 'b'. +*/ +static void fts3ExprLHitGather( + Fts3Expr *pExpr, + MatchInfo *p +){ + assert( (pExpr->pLeft==0)==(pExpr->pRight==0) ); + if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){ + if( pExpr->pLeft ){ + fts3ExprLHitGather(pExpr->pLeft, p); + fts3ExprLHitGather(pExpr->pRight, p); + }else{ + fts3ExprLHits(pExpr, p); + } + } +} + +/* +** fts3ExprIterate() callback used to collect the "global" matchinfo stats +** for a single query. +** +** fts3ExprIterate() callback to load the 'global' elements of a +** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements +** of the matchinfo array that are constant for all rows returned by the +** current query. +** +** Argument pCtx is actually a pointer to a struct of type MatchInfo. This +** function populates Matchinfo.aMatchinfo[] as follows: +** +** for(iCol=0; iColpCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol] + ); +} + +/* +** fts3ExprIterate() callback used to collect the "local" part of the +** FTS3_MATCHINFO_HITS array. The local stats are those elements of the +** array that are different for each row returned by the query. +*/ +static int fts3ExprLocalHitsCb( + Fts3Expr *pExpr, /* Phrase expression node */ + int iPhrase, /* Phrase number */ + void *pCtx /* Pointer to MatchInfo structure */ +){ + int rc = SQLITE_OK; + MatchInfo *p = (MatchInfo *)pCtx; + int iStart = iPhrase * p->nCol * 3; + int i; + + for(i=0; inCol && rc==SQLITE_OK; i++){ + char *pCsr; + rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr); + if( pCsr ){ + p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr); + }else{ + p->aMatchinfo[iStart+i*3] = 0; + } + } + + return rc; +} + +static int fts3MatchinfoCheck( + Fts3Table *pTab, + char cArg, + char **pzErr +){ + if( (cArg==FTS3_MATCHINFO_NPHRASE) + || (cArg==FTS3_MATCHINFO_NCOL) + || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4) + || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4) + || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) + || (cArg==FTS3_MATCHINFO_LCS) + || (cArg==FTS3_MATCHINFO_HITS) + || (cArg==FTS3_MATCHINFO_LHITS) + || (cArg==FTS3_MATCHINFO_LHITS_BM) + ){ + return SQLITE_OK; + } + sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg); + return SQLITE_ERROR; +} + +static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ + int nVal; /* Number of integers output by cArg */ + + switch( cArg ){ + case FTS3_MATCHINFO_NDOC: + case FTS3_MATCHINFO_NPHRASE: + case FTS3_MATCHINFO_NCOL: + nVal = 1; + break; + + case FTS3_MATCHINFO_AVGLENGTH: + case FTS3_MATCHINFO_LENGTH: + case FTS3_MATCHINFO_LCS: + nVal = pInfo->nCol; + break; + + case FTS3_MATCHINFO_LHITS: + nVal = pInfo->nCol * pInfo->nPhrase; + break; + + case FTS3_MATCHINFO_LHITS_BM: + nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32); + break; + + default: + assert( cArg==FTS3_MATCHINFO_HITS ); + nVal = pInfo->nCol * pInfo->nPhrase * 3; + break; + } + + return nVal; +} + +static int fts3MatchinfoSelectDoctotal( + Fts3Table *pTab, + sqlite3_stmt **ppStmt, + sqlite3_int64 *pnDoc, + const char **paLen +){ + sqlite3_stmt *pStmt; + const char *a; + sqlite3_int64 nDoc; + + if( !*ppStmt ){ + int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt); + if( rc!=SQLITE_OK ) return rc; + } + pStmt = *ppStmt; + assert( sqlite3_data_count(pStmt)==1 ); + + a = sqlite3_column_blob(pStmt, 0); + a += sqlite3Fts3GetVarint(a, &nDoc); + if( nDoc==0 ) return FTS_CORRUPT_VTAB; + *pnDoc = (u32)nDoc; + + if( paLen ) *paLen = a; + return SQLITE_OK; +} + +/* +** An instance of the following structure is used to store state while +** iterating through a multi-column position-list corresponding to the +** hits for a single phrase on a single row in order to calculate the +** values for a matchinfo() FTS3_MATCHINFO_LCS request. +*/ +typedef struct LcsIterator LcsIterator; +struct LcsIterator { + Fts3Expr *pExpr; /* Pointer to phrase expression */ + int iPosOffset; /* Tokens count up to end of this phrase */ + char *pRead; /* Cursor used to iterate through aDoclist */ + int iPos; /* Current position */ +}; + +/* +** If LcsIterator.iCol is set to the following value, the iterator has +** finished iterating through all offsets for all columns. +*/ +#define LCS_ITERATOR_FINISHED 0x7FFFFFFF; + +static int fts3MatchinfoLcsCb( + Fts3Expr *pExpr, /* Phrase expression node */ + int iPhrase, /* Phrase number (numbered from zero) */ + void *pCtx /* Pointer to MatchInfo structure */ +){ + LcsIterator *aIter = (LcsIterator *)pCtx; + aIter[iPhrase].pExpr = pExpr; + return SQLITE_OK; +} + +/* +** Advance the iterator passed as an argument to the next position. Return +** 1 if the iterator is at EOF or if it now points to the start of the +** position list for the next column. +*/ +static int fts3LcsIteratorAdvance(LcsIterator *pIter){ + char *pRead = pIter->pRead; + sqlite3_int64 iRead; + int rc = 0; + + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + if( iRead==0 || iRead==1 ){ + pRead = 0; + rc = 1; + }else{ + pIter->iPos += (int)(iRead-2); + } + + pIter->pRead = pRead; + return rc; +} + +/* +** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. +** +** If the call is successful, the longest-common-substring lengths for each +** column are written into the first nCol elements of the pInfo->aMatchinfo[] +** array before returning. SQLITE_OK is returned in this case. +** +** Otherwise, if an error occurs, an SQLite error code is returned and the +** data written to the first nCol elements of pInfo->aMatchinfo[] is +** undefined. +*/ +static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ + LcsIterator *aIter; + int i; + int iCol; + int nToken = 0; + + /* Allocate and populate the array of LcsIterator objects. The array + ** contains one element for each matchable phrase in the query. + **/ + aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase); + if( !aIter ) return SQLITE_NOMEM; + memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase); + (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter); + + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + nToken -= pIter->pExpr->pPhrase->nToken; + pIter->iPosOffset = nToken; + } + + for(iCol=0; iColnCol; iCol++){ + int nLcs = 0; /* LCS value for this column */ + int nLive = 0; /* Number of iterators in aIter not at EOF */ + + for(i=0; inPhrase; i++){ + int rc; + LcsIterator *pIt = &aIter[i]; + rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead); + if( rc!=SQLITE_OK ) return rc; + if( pIt->pRead ){ + pIt->iPos = pIt->iPosOffset; + fts3LcsIteratorAdvance(&aIter[i]); + nLive++; + } + } + + while( nLive>0 ){ + LcsIterator *pAdv = 0; /* The iterator to advance by one position */ + int nThisLcs = 0; /* LCS for the current iterator positions */ + + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + if( pIter->pRead==0 ){ + /* This iterator is already at EOF for this column. */ + nThisLcs = 0; + }else{ + if( pAdv==0 || pIter->iPosiPos ){ + pAdv = pIter; + } + if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){ + nThisLcs++; + }else{ + nThisLcs = 1; + } + if( nThisLcs>nLcs ) nLcs = nThisLcs; + } + } + if( fts3LcsIteratorAdvance(pAdv) ) nLive--; + } + + pInfo->aMatchinfo[iCol] = nLcs; + } + + sqlite3_free(aIter); + return SQLITE_OK; +} + +/* +** Populate the buffer pInfo->aMatchinfo[] with an array of integers to +** be returned by the matchinfo() function. Argument zArg contains the +** format string passed as the second argument to matchinfo (or the +** default value "pcx" if no second argument was specified). The format +** string has already been validated and the pInfo->aMatchinfo[] array +** is guaranteed to be large enough for the output. +** +** If bGlobal is true, then populate all fields of the matchinfo() output. +** If it is false, then assume that those fields that do not change between +** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS) +** have already been populated. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error +** occurs. If a value other than SQLITE_OK is returned, the state the +** pInfo->aMatchinfo[] buffer is left in is undefined. +*/ +static int fts3MatchinfoValues( + Fts3Cursor *pCsr, /* FTS3 cursor object */ + int bGlobal, /* True to grab the global stats */ + MatchInfo *pInfo, /* Matchinfo context object */ + const char *zArg /* Matchinfo format string */ +){ + int rc = SQLITE_OK; + int i; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + sqlite3_stmt *pSelect = 0; + + for(i=0; rc==SQLITE_OK && zArg[i]; i++){ + pInfo->flag = zArg[i]; + switch( zArg[i] ){ + case FTS3_MATCHINFO_NPHRASE: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; + break; + + case FTS3_MATCHINFO_NCOL: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol; + break; + + case FTS3_MATCHINFO_NDOC: + if( bGlobal ){ + sqlite3_int64 nDoc = 0; + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0); + pInfo->aMatchinfo[0] = (u32)nDoc; + } + break; + + case FTS3_MATCHINFO_AVGLENGTH: + if( bGlobal ){ + sqlite3_int64 nDoc; /* Number of rows in table */ + const char *a; /* Aggregate column length array */ + + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a); + if( rc==SQLITE_OK ){ + int iCol; + for(iCol=0; iColnCol; iCol++){ + u32 iVal; + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc); + pInfo->aMatchinfo[iCol] = iVal; + } + } + } + break; + + case FTS3_MATCHINFO_LENGTH: { + sqlite3_stmt *pSelectDocsize = 0; + rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize); + if( rc==SQLITE_OK ){ + int iCol; + const char *a = sqlite3_column_blob(pSelectDocsize, 0); + for(iCol=0; iColnCol; iCol++){ + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + pInfo->aMatchinfo[iCol] = (u32)nToken; + } + } + sqlite3_reset(pSelectDocsize); + break; + } + + case FTS3_MATCHINFO_LCS: + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc==SQLITE_OK ){ + rc = fts3MatchinfoLcs(pCsr, pInfo); + } + break; + + case FTS3_MATCHINFO_LHITS_BM: + case FTS3_MATCHINFO_LHITS: { + int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); + memset(pInfo->aMatchinfo, 0, nZero); + fts3ExprLHitGather(pCsr->pExpr, pInfo); + break; + } + + default: { + Fts3Expr *pExpr; + assert( zArg[i]==FTS3_MATCHINFO_HITS ); + pExpr = pCsr->pExpr; + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc!=SQLITE_OK ) break; + if( bGlobal ){ + if( pCsr->pDeferred ){ + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); + if( rc!=SQLITE_OK ) break; + } + rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); + sqlite3Fts3EvalTestDeferred(pCsr, &rc); + if( rc!=SQLITE_OK ) break; + } + (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); + break; + } + } + + pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]); + } + + sqlite3_reset(pSelect); + return rc; +} + + +/* +** Populate pCsr->aMatchinfo[] with data for the current row. The +** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). +*/ +static void fts3GetMatchinfo( + sqlite3_context *pCtx, /* Return results here */ + Fts3Cursor *pCsr, /* FTS3 Cursor object */ + const char *zArg /* Second argument to matchinfo() function */ +){ + MatchInfo sInfo; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int bGlobal = 0; /* Collect 'global' stats as well as local */ + + u32 *aOut = 0; + void (*xDestroyOut)(void*) = 0; + + memset(&sInfo, 0, sizeof(MatchInfo)); + sInfo.pCursor = pCsr; + sInfo.nCol = pTab->nColumn; + + /* If there is cached matchinfo() data, but the format string for the + ** cache does not match the format string for this request, discard + ** the cached data. */ + if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){ + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); + pCsr->pMIBuffer = 0; + } + + /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the + ** matchinfo function has been called for this query. In this case + ** allocate the array used to accumulate the matchinfo data and + ** initialize those elements that are constant for every row. + */ + if( pCsr->pMIBuffer==0 ){ + int nMatchinfo = 0; /* Number of u32 elements in match-info */ + int i; /* Used to iterate through zArg */ + + /* Determine the number of phrases in the query */ + pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); + sInfo.nPhrase = pCsr->nPhrase; + + /* Determine the number of integers in the buffer returned by this call. */ + for(i=0; zArg[i]; i++){ + char *zErr = 0; + if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ + sqlite3_result_error(pCtx, zErr, -1); + sqlite3_free(zErr); + return; + } + nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); + } + + /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ + pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg); + if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM; + + pCsr->isMatchinfoNeeded = 1; + bGlobal = 1; + } + + if( rc==SQLITE_OK ){ + xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut); + if( xDestroyOut==0 ){ + rc = SQLITE_NOMEM; + } + } + + if( rc==SQLITE_OK ){ + sInfo.aMatchinfo = aOut; + sInfo.nPhrase = pCsr->nPhrase; + rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); + if( bGlobal ){ + fts3MIBufferSetGlobal(pCsr->pMIBuffer); + } + } + + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pCtx, rc); + if( xDestroyOut ) xDestroyOut(aOut); + }else{ + int n = pCsr->pMIBuffer->nElem * sizeof(u32); + sqlite3_result_blob(pCtx, aOut, n, xDestroyOut); + } +} + +/* +** Implementation of snippet() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Snippet( + sqlite3_context *pCtx, /* SQLite function call context */ + Fts3Cursor *pCsr, /* Cursor object */ + const char *zStart, /* Snippet start text - "" */ + const char *zEnd, /* Snippet end text - "" */ + const char *zEllipsis, /* Snippet ellipsis text - "..." */ + int iCol, /* Extract snippet from this column */ + int nToken /* Approximate number of tokens in snippet */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int i; + StrBuffer res = {0, 0, 0}; + + /* The returned text includes up to four fragments of text extracted from + ** the data in the current row. The first iteration of the for(...) loop + ** below attempts to locate a single fragment of text nToken tokens in + ** size that contains at least one instance of all phrases in the query + ** expression that appear in the current row. If such a fragment of text + ** cannot be found, the second iteration of the loop attempts to locate + ** a pair of fragments, and so on. + */ + int nSnippet = 0; /* Number of fragments in this snippet */ + SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */ + int nFToken = -1; /* Number of tokens in each fragment */ + + if( !pCsr->pExpr ){ + sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); + return; + } + + for(nSnippet=1; 1; nSnippet++){ + + int iSnip; /* Loop counter 0..nSnippet-1 */ + u64 mCovered = 0; /* Bitmask of phrases covered by snippet */ + u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */ + + if( nToken>=0 ){ + nFToken = (nToken+nSnippet-1) / nSnippet; + }else{ + nFToken = -1 * nToken; + } + + for(iSnip=0; iSnipnColumn; iRead++){ + SnippetFragment sF = {0, 0, 0, 0}; + int iS = 0; + if( iCol>=0 && iRead!=iCol ) continue; + + /* Find the best snippet of nFToken tokens in column iRead. */ + rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); + if( rc!=SQLITE_OK ){ + goto snippet_out; + } + if( iS>iBestScore ){ + *pFragment = sF; + iBestScore = iS; + } + } + + mCovered |= pFragment->covered; + } + + /* If all query phrases seen by fts3BestSnippet() are present in at least + ** one of the nSnippet snippet fragments, break out of the loop. + */ + assert( (mCovered&mSeen)==mCovered ); + if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break; + } + + assert( nFToken>0 ); + + for(i=0; ipCsr, pExpr, p->iCol, &pList); + nTerm = pExpr->pPhrase->nToken; + if( pList ){ + fts3GetDeltaPosition(&pList, &iPos); + assert( iPos>=0 ); + } + + for(iTerm=0; iTermaTerm[p->iTerm++]; + pT->iOff = nTerm-iTerm-1; + pT->pList = pList; + pT->iPos = iPos; + } + + return rc; +} + +/* +** Implementation of offsets() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Offsets( + sqlite3_context *pCtx, /* SQLite function call context */ + Fts3Cursor *pCsr /* Cursor object */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule; + int rc; /* Return Code */ + int nToken; /* Number of tokens in query */ + int iCol; /* Column currently being processed */ + StrBuffer res = {0, 0, 0}; /* Result string */ + TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */ + + if( !pCsr->pExpr ){ + sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); + return; + } + + memset(&sCtx, 0, sizeof(sCtx)); + assert( pCsr->isRequireSeek==0 ); + + /* Count the number of terms in the query */ + rc = fts3ExprLoadDoclists(pCsr, 0, &nToken); + if( rc!=SQLITE_OK ) goto offsets_out; + + /* Allocate the array of TermOffset iterators. */ + sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken); + if( 0==sCtx.aTerm ){ + rc = SQLITE_NOMEM; + goto offsets_out; + } + sCtx.iDocid = pCsr->iPrevId; + sCtx.pCsr = pCsr; + + /* Loop through the table columns, appending offset information to + ** string-buffer res for each column. + */ + for(iCol=0; iColnColumn; iCol++){ + sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */ + const char *ZDUMMY; /* Dummy argument used with xNext() */ + int NDUMMY = 0; /* Dummy argument used with xNext() */ + int iStart = 0; + int iEnd = 0; + int iCurrent = 0; + const char *zDoc; + int nDoc; + + /* Initialize the contents of sCtx.aTerm[] for column iCol. There is + ** no way that this operation can fail, so the return code from + ** fts3ExprIterate() can be discarded. + */ + sCtx.iCol = iCol; + sCtx.iTerm = 0; + (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); + + /* Retreive the text stored in column iCol. If an SQL NULL is stored + ** in column iCol, jump immediately to the next iteration of the loop. + ** If an OOM occurs while retrieving the data (this can happen if SQLite + ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM + ** to the caller. + */ + zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1); + nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1); + if( zDoc==0 ){ + if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){ + continue; + } + rc = SQLITE_NOMEM; + goto offsets_out; + } + + /* Initialize a tokenizer iterator to iterate through column iCol. */ + rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, + zDoc, nDoc, &pC + ); + if( rc!=SQLITE_OK ) goto offsets_out; + + rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); + while( rc==SQLITE_OK ){ + int i; /* Used to loop through terms */ + int iMinPos = 0x7FFFFFFF; /* Position of next token */ + TermOffset *pTerm = 0; /* TermOffset associated with next token */ + + for(i=0; ipList && (pT->iPos-pT->iOff)iPos-pT->iOff; + pTerm = pT; + } + } + + if( !pTerm ){ + /* All offsets for this column have been gathered. */ + rc = SQLITE_DONE; + }else{ + assert( iCurrent<=iMinPos ); + if( 0==(0xFE&*pTerm->pList) ){ + pTerm->pList = 0; + }else{ + fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos); + } + while( rc==SQLITE_OK && iCurrentxNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); + } + if( rc==SQLITE_OK ){ + char aBuffer[64]; + sqlite3_snprintf(sizeof(aBuffer), aBuffer, + "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart + ); + rc = fts3StringAppend(&res, aBuffer, -1); + }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){ + rc = FTS_CORRUPT_VTAB; + } + } + } + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + } + + pMod->xClose(pC); + if( rc!=SQLITE_OK ) goto offsets_out; + } + + offsets_out: + sqlite3_free(sCtx.aTerm); + assert( rc!=SQLITE_DONE ); + sqlite3Fts3SegmentsClose(pTab); + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pCtx, rc); + sqlite3_free(res.z); + }else{ + sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free); + } + return; +} + +/* +** Implementation of matchinfo() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Matchinfo( + sqlite3_context *pContext, /* Function call context */ + Fts3Cursor *pCsr, /* FTS3 table cursor */ + const char *zArg /* Second arg to matchinfo() function */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + const char *zFormat; + + if( zArg ){ + zFormat = zArg; + }else{ + zFormat = FTS3_MATCHINFO_DEFAULT; + } + + if( !pCsr->pExpr ){ + sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); + return; + }else{ + /* Retrieve matchinfo() data. */ + fts3GetMatchinfo(pContext, pCsr, zFormat); + sqlite3Fts3SegmentsClose(pTab); + } +} + +#endif + +/************** End of fts3_snippet.c ****************************************/ +/************** Begin file fts3_unicode.c ************************************/ +/* +** 2012 May 24 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Implementation of the "unicode" full-text-search tokenizer. +*/ + +#ifndef SQLITE_DISABLE_FTS3_UNICODE + +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* #include */ +/* #include */ +/* #include */ +/* #include */ + +/* #include "fts3_tokenizer.h" */ + +/* +** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied +** from the sqlite3 source file utf.c. If this file is compiled as part +** of the amalgamation, they are not required. +*/ +#ifndef SQLITE_AMALGAMATION + +static const unsigned char sqlite3Utf8Trans1[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, +}; + +#define READ_UTF8(zIn, zTerm, c) \ + c = *(zIn++); \ + if( c>=0xc0 ){ \ + c = sqlite3Utf8Trans1[c-0xc0]; \ + while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \ + c = (c<<6) + (0x3f & *(zIn++)); \ + } \ + if( c<0x80 \ + || (c&0xFFFFF800)==0xD800 \ + || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ + } + +#define WRITE_UTF8(zOut, c) { \ + if( c<0x00080 ){ \ + *zOut++ = (u8)(c&0xFF); \ + } \ + else if( c<0x00800 ){ \ + *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + } \ + else if( c<0x10000 ){ \ + *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \ + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + }else{ \ + *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \ + *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \ + *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ + *zOut++ = 0x80 + (u8)(c & 0x3F); \ + } \ +} + +#endif /* ifndef SQLITE_AMALGAMATION */ + +typedef struct unicode_tokenizer unicode_tokenizer; +typedef struct unicode_cursor unicode_cursor; + +struct unicode_tokenizer { + sqlite3_tokenizer base; + int bRemoveDiacritic; + int nException; + int *aiException; +}; + +struct unicode_cursor { + sqlite3_tokenizer_cursor base; + const unsigned char *aInput; /* Input text being tokenized */ + int nInput; /* Size of aInput[] in bytes */ + int iOff; /* Current offset within aInput[] */ + int iToken; /* Index of next token to be returned */ + char *zToken; /* storage for current token */ + int nAlloc; /* space allocated at zToken */ +}; + + +/* +** Destroy a tokenizer allocated by unicodeCreate(). +*/ +static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){ + if( pTokenizer ){ + unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer; + sqlite3_free(p->aiException); + sqlite3_free(p); + } + return SQLITE_OK; +} + +/* +** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE +** statement has specified that the tokenizer for this table shall consider +** all characters in string zIn/nIn to be separators (if bAlnum==0) or +** token characters (if bAlnum==1). +** +** For each codepoint in the zIn/nIn string, this function checks if the +** sqlite3FtsUnicodeIsalnum() function already returns the desired result. +** If so, no action is taken. Otherwise, the codepoint is added to the +** unicode_tokenizer.aiException[] array. For the purposes of tokenization, +** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all +** codepoints in the aiException[] array. +** +** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic() +** identifies as a diacritic) occurs in the zIn/nIn string it is ignored. +** It is not possible to change the behavior of the tokenizer with respect +** to these codepoints. +*/ +static int unicodeAddExceptions( + unicode_tokenizer *p, /* Tokenizer to add exceptions to */ + int bAlnum, /* Replace Isalnum() return value with this */ + const char *zIn, /* Array of characters to make exceptions */ + int nIn /* Length of z in bytes */ +){ + const unsigned char *z = (const unsigned char *)zIn; + const unsigned char *zTerm = &z[nIn]; + int iCode; + int nEntry = 0; + + assert( bAlnum==0 || bAlnum==1 ); + + while( zaiException, (p->nException+nEntry)*sizeof(int)); + if( aNew==0 ) return SQLITE_NOMEM; + nNew = p->nException; + + z = (const unsigned char *)zIn; + while( zi; j--) aNew[j] = aNew[j-1]; + aNew[i] = iCode; + nNew++; + } + } + p->aiException = aNew; + p->nException = nNew; + } + + return SQLITE_OK; +} + +/* +** Return true if the p->aiException[] array contains the value iCode. +*/ +static int unicodeIsException(unicode_tokenizer *p, int iCode){ + if( p->nException>0 ){ + int *a = p->aiException; + int iLo = 0; + int iHi = p->nException-1; + + while( iHi>=iLo ){ + int iTest = (iHi + iLo) / 2; + if( iCode==a[iTest] ){ + return 1; + }else if( iCode>a[iTest] ){ + iLo = iTest+1; + }else{ + iHi = iTest-1; + } + } + } + + return 0; +} + +/* +** Return true if, for the purposes of tokenization, codepoint iCode is +** considered a token character (not a separator). +*/ +static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){ + assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 ); + return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode); +} + +/* +** Create a new tokenizer instance. +*/ +static int unicodeCreate( + int nArg, /* Size of array argv[] */ + const char * const *azArg, /* Tokenizer creation arguments */ + sqlite3_tokenizer **pp /* OUT: New tokenizer handle */ +){ + unicode_tokenizer *pNew; /* New tokenizer object */ + int i; + int rc = SQLITE_OK; + + pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer)); + if( pNew==NULL ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(unicode_tokenizer)); + pNew->bRemoveDiacritic = 1; + + for(i=0; rc==SQLITE_OK && ibRemoveDiacritic = 1; + } + else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){ + pNew->bRemoveDiacritic = 0; + } + else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){ + rc = unicodeAddExceptions(pNew, 1, &z[11], n-11); + } + else if( n>=11 && memcmp("separators=", z, 11)==0 ){ + rc = unicodeAddExceptions(pNew, 0, &z[11], n-11); + } + else{ + /* Unrecognized argument */ + rc = SQLITE_ERROR; + } + } + + if( rc!=SQLITE_OK ){ + unicodeDestroy((sqlite3_tokenizer *)pNew); + pNew = 0; + } + *pp = (sqlite3_tokenizer *)pNew; + return rc; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is pInput[0..nBytes-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int unicodeOpen( + sqlite3_tokenizer *p, /* The tokenizer */ + const char *aInput, /* Input string */ + int nInput, /* Size of string aInput in bytes */ + sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */ +){ + unicode_cursor *pCsr; + + pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor)); + if( pCsr==0 ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(unicode_cursor)); + + pCsr->aInput = (const unsigned char *)aInput; + if( aInput==0 ){ + pCsr->nInput = 0; + }else if( nInput<0 ){ + pCsr->nInput = (int)strlen(aInput); + }else{ + pCsr->nInput = nInput; + } + + *pp = &pCsr->base; + UNUSED_PARAMETER(p); + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to +** simpleOpen() above. +*/ +static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){ + unicode_cursor *pCsr = (unicode_cursor *) pCursor; + sqlite3_free(pCsr->zToken); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to simpleOpen(). +*/ +static int unicodeNext( + sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */ + const char **paToken, /* OUT: Token text */ + int *pnToken, /* OUT: Number of bytes at *paToken */ + int *piStart, /* OUT: Starting offset of token */ + int *piEnd, /* OUT: Ending offset of token */ + int *piPos /* OUT: Position integer of token */ +){ + unicode_cursor *pCsr = (unicode_cursor *)pC; + unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer); + int iCode = 0; + char *zOut; + const unsigned char *z = &pCsr->aInput[pCsr->iOff]; + const unsigned char *zStart = z; + const unsigned char *zEnd; + const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput]; + + /* Scan past any delimiter characters before the start of the next token. + ** Return SQLITE_DONE early if this takes us all the way to the end of + ** the input. */ + while( z=zTerm ) return SQLITE_DONE; + + zOut = pCsr->zToken; + do { + int iOut; + + /* Grow the output buffer if required. */ + if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){ + char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64); + if( !zNew ) return SQLITE_NOMEM; + zOut = &zNew[zOut - pCsr->zToken]; + pCsr->zToken = zNew; + pCsr->nAlloc += 64; + } + + /* Write the folded case of the last character read to the output */ + zEnd = z; + iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic); + if( iOut ){ + WRITE_UTF8(zOut, iOut); + } + + /* If the cursor is not at EOF, read the next character */ + if( z>=zTerm ) break; + READ_UTF8(z, zTerm, iCode); + }while( unicodeIsAlnum(p, iCode) + || sqlite3FtsUnicodeIsdiacritic(iCode) + ); + + /* Set the output variables and return. */ + pCsr->iOff = (int)(z - pCsr->aInput); + *paToken = pCsr->zToken; + *pnToken = (int)(zOut - pCsr->zToken); + *piStart = (int)(zStart - pCsr->aInput); + *piEnd = (int)(zEnd - pCsr->aInput); + *piPos = pCsr->iToken++; + return SQLITE_OK; +} + +/* +** Set *ppModule to a pointer to the sqlite3_tokenizer_module +** structure for the unicode tokenizer. +*/ +SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){ + static const sqlite3_tokenizer_module module = { + 0, + unicodeCreate, + unicodeDestroy, + unicodeOpen, + unicodeClose, + unicodeNext, + 0, + }; + *ppModule = &module; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ +#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */ + +/************** End of fts3_unicode.c ****************************************/ +/************** Begin file fts3_unicode2.c ***********************************/ +/* +** 2012 May 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +*/ + +/* +** DO NOT EDIT THIS MACHINE GENERATED FILE. +*/ + +#ifndef SQLITE_DISABLE_FTS3_UNICODE +#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) + +/* #include */ + +/* +** Return true if the argument corresponds to a unicode codepoint +** classified as either a letter or a number. Otherwise false. +** +** The results are undefined if the value passed to this function +** is less than zero. +*/ +SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){ + /* Each unsigned integer in the following array corresponds to a contiguous + ** range of unicode codepoints that are not either letters or numbers (i.e. + ** codepoints for which this function should return 0). + ** + ** The most significant 22 bits in each 32-bit value contain the first + ** codepoint in the range. The least significant 10 bits are used to store + ** the size of the range (always at least 1). In other words, the value + ** ((C<<22) + N) represents a range of N codepoints starting with codepoint + ** C. It is not possible to represent a range larger than 1023 codepoints + ** using this format. + */ + static const unsigned int aEntry[] = { + 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07, + 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01, + 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401, + 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01, + 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01, + 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802, + 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F, + 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401, + 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804, + 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403, + 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812, + 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001, + 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802, + 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805, + 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401, + 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03, + 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807, + 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001, + 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01, + 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804, + 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001, + 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802, + 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01, + 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06, + 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007, + 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006, + 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417, + 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14, + 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07, + 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01, + 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001, + 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802, + 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F, + 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002, + 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802, + 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006, + 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D, + 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802, + 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027, + 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403, + 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805, + 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04, + 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401, + 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005, + 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B, + 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A, + 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001, + 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59, + 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807, + 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01, + 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E, + 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100, + 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10, + 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402, + 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804, + 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012, + 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, + 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, + 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, + 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, + 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02, + 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802, + 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013, + 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06, + 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003, + 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01, + 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403, + 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009, + 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003, + 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003, + 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E, + 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046, + 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401, + 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401, + 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F, + 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C, + 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002, + 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025, + 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6, + 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46, + 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060, + 0x380400F0, + }; + static const unsigned int aAscii[4] = { + 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, + }; + + if( c<128 ){ + return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 ); + }else if( c<(1<<22) ){ + unsigned int key = (((unsigned int)c)<<10) | 0x000003FF; + int iRes = 0; + int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1; + int iLo = 0; + while( iHi>=iLo ){ + int iTest = (iHi + iLo) / 2; + if( key >= aEntry[iTest] ){ + iRes = iTest; + iLo = iTest+1; + }else{ + iHi = iTest-1; + } + } + assert( aEntry[0]=aEntry[iRes] ); + return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF))); + } + return 1; +} + + +/* +** If the argument is a codepoint corresponding to a lowercase letter +** in the ASCII range with a diacritic added, return the codepoint +** of the ASCII letter only. For example, if passed 235 - "LATIN +** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER +** E"). The resuls of passing a codepoint that corresponds to an +** uppercase letter are undefined. +*/ +static int remove_diacritic(int c){ + unsigned short aDia[] = { + 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995, + 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286, + 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732, + 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336, + 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928, + 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234, + 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504, + 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529, + 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, + 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, + 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, + 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, + 62924, 63050, 63082, 63274, 63390, + }; + char aChar[] = { + '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c', + 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r', + 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o', + 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r', + 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h', + 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't', + 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a', + 'e', 'i', 'o', 'u', 'y', + }; + + unsigned int key = (((unsigned int)c)<<3) | 0x00000007; + int iRes = 0; + int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1; + int iLo = 0; + while( iHi>=iLo ){ + int iTest = (iHi + iLo) / 2; + if( key >= aDia[iTest] ){ + iRes = iTest; + iLo = iTest+1; + }else{ + iHi = iTest-1; + } + } + assert( key>=aDia[iRes] ); + return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]); +} + + +/* +** Return true if the argument interpreted as a unicode codepoint +** is a diacritical modifier character. +*/ +SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){ + unsigned int mask0 = 0x08029FDF; + unsigned int mask1 = 0x000361F8; + if( c<768 || c>817 ) return 0; + return (c < 768+32) ? + (mask0 & (1 << (c-768))) : + (mask1 & (1 << (c-768-32))); +} + + +/* +** Interpret the argument as a unicode codepoint. If the codepoint +** is an upper case character that has a lower case equivalent, +** return the codepoint corresponding to the lower case version. +** Otherwise, return a copy of the argument. +** +** The results are undefined if the value passed to this function +** is less than zero. +*/ +SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){ + /* Each entry in the following array defines a rule for folding a range + ** of codepoints to lower case. The rule applies to a range of nRange + ** codepoints starting at codepoint iCode. + ** + ** If the least significant bit in flags is clear, then the rule applies + ** to all nRange codepoints (i.e. all nRange codepoints are upper case and + ** need to be folded). Or, if it is set, then the rule only applies to + ** every second codepoint in the range, starting with codepoint C. + ** + ** The 7 most significant bits in flags are an index into the aiOff[] + ** array. If a specific codepoint C does require folding, then its lower + ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF). + ** + ** The contents of this array are generated by parsing the CaseFolding.txt + ** file distributed as part of the "Unicode Character Database". See + ** http://www.unicode.org for details. + */ + static const struct TableEntry { + unsigned short iCode; + unsigned char flags; + unsigned char nRange; + } aEntry[] = { + {65, 14, 26}, {181, 64, 1}, {192, 14, 23}, + {216, 14, 7}, {256, 1, 48}, {306, 1, 6}, + {313, 1, 16}, {330, 1, 46}, {376, 116, 1}, + {377, 1, 6}, {383, 104, 1}, {385, 50, 1}, + {386, 1, 4}, {390, 44, 1}, {391, 0, 1}, + {393, 42, 2}, {395, 0, 1}, {398, 32, 1}, + {399, 38, 1}, {400, 40, 1}, {401, 0, 1}, + {403, 42, 1}, {404, 46, 1}, {406, 52, 1}, + {407, 48, 1}, {408, 0, 1}, {412, 52, 1}, + {413, 54, 1}, {415, 56, 1}, {416, 1, 6}, + {422, 60, 1}, {423, 0, 1}, {425, 60, 1}, + {428, 0, 1}, {430, 60, 1}, {431, 0, 1}, + {433, 58, 2}, {435, 1, 4}, {439, 62, 1}, + {440, 0, 1}, {444, 0, 1}, {452, 2, 1}, + {453, 0, 1}, {455, 2, 1}, {456, 0, 1}, + {458, 2, 1}, {459, 1, 18}, {478, 1, 18}, + {497, 2, 1}, {498, 1, 4}, {502, 122, 1}, + {503, 134, 1}, {504, 1, 40}, {544, 110, 1}, + {546, 1, 18}, {570, 70, 1}, {571, 0, 1}, + {573, 108, 1}, {574, 68, 1}, {577, 0, 1}, + {579, 106, 1}, {580, 28, 1}, {581, 30, 1}, + {582, 1, 10}, {837, 36, 1}, {880, 1, 4}, + {886, 0, 1}, {902, 18, 1}, {904, 16, 3}, + {908, 26, 1}, {910, 24, 2}, {913, 14, 17}, + {931, 14, 9}, {962, 0, 1}, {975, 4, 1}, + {976, 140, 1}, {977, 142, 1}, {981, 146, 1}, + {982, 144, 1}, {984, 1, 24}, {1008, 136, 1}, + {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1}, + {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1}, + {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32}, + {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1}, + {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38}, + {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1}, + {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1}, + {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6}, + {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6}, + {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8}, + {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2}, + {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1}, + {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2}, + {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2}, + {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2}, + {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1}, + {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16}, + {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47}, + {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1}, + {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1}, + {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1}, + {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2}, + {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1}, + {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14}, + {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1}, + {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1}, + {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1}, + {65313, 14, 26}, + }; + static const unsigned short aiOff[] = { + 1, 2, 8, 15, 16, 26, 28, 32, + 37, 38, 40, 48, 63, 64, 69, 71, + 79, 80, 116, 202, 203, 205, 206, 207, + 209, 210, 211, 213, 214, 217, 218, 219, + 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721, + 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274, + 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406, + 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, + 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, + 65514, 65521, 65527, 65528, 65529, + }; + + int ret = c; + + assert( c>=0 ); + assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 ); + + if( c<128 ){ + if( c>='A' && c<='Z' ) ret = c + ('a' - 'A'); + }else if( c<65536 ){ + int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1; + int iLo = 0; + int iRes = -1; + + while( iHi>=iLo ){ + int iTest = (iHi + iLo) / 2; + int cmp = (c - aEntry[iTest].iCode); + if( cmp>=0 ){ + iRes = iTest; + iLo = iTest+1; + }else{ + iHi = iTest-1; + } + } + assert( iRes<0 || c>=aEntry[iRes].iCode ); + + if( iRes>=0 ){ + const struct TableEntry *p = &aEntry[iRes]; + if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){ + ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF; + assert( ret>0 ); + } + } + + if( bRemoveDiacritic ) ret = remove_diacritic(ret); + } + + else if( c>=66560 && c<66600 ){ + ret = c + 40; + } + + return ret; +} +#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */ +#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */ + +/************** End of fts3_unicode2.c ***************************************/ +/************** Begin file rtree.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code for implementations of the r-tree and r*-tree +** algorithms packaged as an SQLite virtual table module. +*/ + +/* +** Database Format of R-Tree Tables +** -------------------------------- +** +** The data structure for a single virtual r-tree table is stored in three +** native SQLite tables declared as follows. In each case, the '%' character +** in the table name is replaced with the user-supplied name of the r-tree +** table. +** +** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) +** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) +** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) +** +** The data for each node of the r-tree structure is stored in the %_node +** table. For each node that is not the root node of the r-tree, there is +** an entry in the %_parent table associating the node with its parent. +** And for each row of data in the table, there is an entry in the %_rowid +** table that maps from the entries rowid to the id of the node that it +** is stored on. +** +** The root node of an r-tree always exists, even if the r-tree table is +** empty. The nodeno of the root node is always 1. All other nodes in the +** table must be the same size as the root node. The content of each node +** is formatted as follows: +** +** 1. If the node is the root node (node 1), then the first 2 bytes +** of the node contain the tree depth as a big-endian integer. +** For non-root nodes, the first 2 bytes are left unused. +** +** 2. The next 2 bytes contain the number of entries currently +** stored in the node. +** +** 3. The remainder of the node contains the node entries. Each entry +** consists of a single 8-byte integer followed by an even number +** of 4-byte coordinates. For leaf nodes the integer is the rowid +** of a record. For internal nodes it is the node number of a +** child page. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) + +#ifndef SQLITE_CORE +/* #include "sqlite3ext.h" */ + SQLITE_EXTENSION_INIT1 +#else +/* #include "sqlite3.h" */ +#endif + +/* #include */ +/* #include */ +/* #include */ + +#ifndef SQLITE_AMALGAMATION +#include "sqlite3rtree.h" +typedef sqlite3_int64 i64; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +#endif + +/* The following macro is used to suppress compiler warnings. +*/ +#ifndef UNUSED_PARAMETER +# define UNUSED_PARAMETER(x) (void)(x) +#endif + +typedef struct Rtree Rtree; +typedef struct RtreeCursor RtreeCursor; +typedef struct RtreeNode RtreeNode; +typedef struct RtreeCell RtreeCell; +typedef struct RtreeConstraint RtreeConstraint; +typedef struct RtreeMatchArg RtreeMatchArg; +typedef struct RtreeGeomCallback RtreeGeomCallback; +typedef union RtreeCoord RtreeCoord; +typedef struct RtreeSearchPoint RtreeSearchPoint; + +/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ +#define RTREE_MAX_DIMENSIONS 5 + +/* Size of hash table Rtree.aHash. This hash table is not expected to +** ever contain very many entries, so a fixed number of buckets is +** used. +*/ +#define HASHSIZE 97 + +/* The xBestIndex method of this virtual table requires an estimate of +** the number of rows in the virtual table to calculate the costs of +** various strategies. If possible, this estimate is loaded from the +** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum). +** Otherwise, if no sqlite_stat1 entry is available, use +** RTREE_DEFAULT_ROWEST. +*/ +#define RTREE_DEFAULT_ROWEST 1048576 +#define RTREE_MIN_ROWEST 100 + +/* +** An rtree virtual-table object. +*/ +struct Rtree { + sqlite3_vtab base; /* Base class. Must be first */ + sqlite3 *db; /* Host database connection */ + int iNodeSize; /* Size in bytes of each node in the node table */ + u8 nDim; /* Number of dimensions */ + u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ + u8 nBytesPerCell; /* Bytes consumed per cell */ + int iDepth; /* Current depth of the r-tree structure */ + char *zDb; /* Name of database containing r-tree table */ + char *zName; /* Name of r-tree table */ + int nBusy; /* Current number of users of this structure */ + i64 nRowEst; /* Estimated number of rows in this table */ + + /* List of nodes removed during a CondenseTree operation. List is + ** linked together via the pointer normally used for hash chains - + ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree + ** headed by the node (leaf nodes have RtreeNode.iNode==0). + */ + RtreeNode *pDeleted; + int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */ + + /* Statements to read/write/delete a record from xxx_node */ + sqlite3_stmt *pReadNode; + sqlite3_stmt *pWriteNode; + sqlite3_stmt *pDeleteNode; + + /* Statements to read/write/delete a record from xxx_rowid */ + sqlite3_stmt *pReadRowid; + sqlite3_stmt *pWriteRowid; + sqlite3_stmt *pDeleteRowid; + + /* Statements to read/write/delete a record from xxx_parent */ + sqlite3_stmt *pReadParent; + sqlite3_stmt *pWriteParent; + sqlite3_stmt *pDeleteParent; + + RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ +}; + +/* Possible values for Rtree.eCoordType: */ +#define RTREE_COORD_REAL32 0 +#define RTREE_COORD_INT32 1 + +/* +** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will +** only deal with integer coordinates. No floating point operations +** will be done. +*/ +#ifdef SQLITE_RTREE_INT_ONLY + typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */ + typedef int RtreeValue; /* Low accuracy coordinate */ +# define RTREE_ZERO 0 +#else + typedef double RtreeDValue; /* High accuracy coordinate */ + typedef float RtreeValue; /* Low accuracy coordinate */ +# define RTREE_ZERO 0.0 +#endif + +/* +** When doing a search of an r-tree, instances of the following structure +** record intermediate results from the tree walk. +** +** The id is always a node-id. For iLevel>=1 the id is the node-id of +** the node that the RtreeSearchPoint represents. When iLevel==0, however, +** the id is of the parent node and the cell that RtreeSearchPoint +** represents is the iCell-th entry in the parent node. +*/ +struct RtreeSearchPoint { + RtreeDValue rScore; /* The score for this node. Smallest goes first. */ + sqlite3_int64 id; /* Node ID */ + u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */ + u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */ + u8 iCell; /* Cell index within the node */ +}; + +/* +** The minimum number of cells allowed for a node is a third of the +** maximum. In Gutman's notation: +** +** m = M/3 +** +** If an R*-tree "Reinsert" operation is required, the same number of +** cells are removed from the overfull node and reinserted into the tree. +*/ +#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) +#define RTREE_REINSERT(p) RTREE_MINCELLS(p) +#define RTREE_MAXCELLS 51 + +/* +** The smallest possible node-size is (512-64)==448 bytes. And the largest +** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). +** Therefore all non-root nodes must contain at least 3 entries. Since +** 2^40 is greater than 2^64, an r-tree structure always has a depth of +** 40 or less. +*/ +#define RTREE_MAX_DEPTH 40 + + +/* +** Number of entries in the cursor RtreeNode cache. The first entry is +** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining +** entries cache the RtreeNode for the first elements of the priority queue. +*/ +#define RTREE_CACHE_SZ 5 + +/* +** An rtree cursor object. +*/ +struct RtreeCursor { + sqlite3_vtab_cursor base; /* Base class. Must be first */ + u8 atEOF; /* True if at end of search */ + u8 bPoint; /* True if sPoint is valid */ + int iStrategy; /* Copy of idxNum search parameter */ + int nConstraint; /* Number of entries in aConstraint */ + RtreeConstraint *aConstraint; /* Search constraints. */ + int nPointAlloc; /* Number of slots allocated for aPoint[] */ + int nPoint; /* Number of slots used in aPoint[] */ + int mxLevel; /* iLevel value for root of the tree */ + RtreeSearchPoint *aPoint; /* Priority queue for search points */ + RtreeSearchPoint sPoint; /* Cached next search point */ + RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */ + u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */ +}; + +/* Return the Rtree of a RtreeCursor */ +#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab)) + +/* +** A coordinate can be either a floating point number or a integer. All +** coordinates within a single R-Tree are always of the same time. +*/ +union RtreeCoord { + RtreeValue f; /* Floating point value */ + int i; /* Integer value */ + u32 u; /* Unsigned for byte-order conversions */ +}; + +/* +** The argument is an RtreeCoord. Return the value stored within the RtreeCoord +** formatted as a RtreeDValue (double or int64). This macro assumes that local +** variable pRtree points to the Rtree structure associated with the +** RtreeCoord. +*/ +#ifdef SQLITE_RTREE_INT_ONLY +# define DCOORD(coord) ((RtreeDValue)coord.i) +#else +# define DCOORD(coord) ( \ + (pRtree->eCoordType==RTREE_COORD_REAL32) ? \ + ((double)coord.f) : \ + ((double)coord.i) \ + ) +#endif + +/* +** A search constraint. +*/ +struct RtreeConstraint { + int iCoord; /* Index of constrained coordinate */ + int op; /* Constraining operation */ + union { + RtreeDValue rValue; /* Constraint value. */ + int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*); + int (*xQueryFunc)(sqlite3_rtree_query_info*); + } u; + sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */ +}; + +/* Possible values for RtreeConstraint.op */ +#define RTREE_EQ 0x41 /* A */ +#define RTREE_LE 0x42 /* B */ +#define RTREE_LT 0x43 /* C */ +#define RTREE_GE 0x44 /* D */ +#define RTREE_GT 0x45 /* E */ +#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */ +#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */ + + +/* +** An rtree structure node. +*/ +struct RtreeNode { + RtreeNode *pParent; /* Parent node */ + i64 iNode; /* The node number */ + int nRef; /* Number of references to this node */ + int isDirty; /* True if the node needs to be written to disk */ + u8 *zData; /* Content of the node, as should be on disk */ + RtreeNode *pNext; /* Next node in this hash collision chain */ +}; + +/* Return the number of cells in a node */ +#define NCELL(pNode) readInt16(&(pNode)->zData[2]) + +/* +** A single cell from a node, deserialized +*/ +struct RtreeCell { + i64 iRowid; /* Node or entry ID */ + RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */ +}; + + +/* +** This object becomes the sqlite3_user_data() for the SQL functions +** that are created by sqlite3_rtree_geometry_callback() and +** sqlite3_rtree_query_callback() and which appear on the right of MATCH +** operators in order to constrain a search. +** +** xGeom and xQueryFunc are the callback functions. Exactly one of +** xGeom and xQueryFunc fields is non-NULL, depending on whether the +** SQL function was created using sqlite3_rtree_geometry_callback() or +** sqlite3_rtree_query_callback(). +** +** This object is deleted automatically by the destructor mechanism in +** sqlite3_create_function_v2(). +*/ +struct RtreeGeomCallback { + int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*); + int (*xQueryFunc)(sqlite3_rtree_query_info*); + void (*xDestructor)(void*); + void *pContext; +}; + + +/* +** Value for the first field of every RtreeMatchArg object. The MATCH +** operator tests that the first field of a blob operand matches this +** value to avoid operating on invalid blobs (which could cause a segfault). +*/ +#define RTREE_GEOMETRY_MAGIC 0x891245AB + +/* +** An instance of this structure (in the form of a BLOB) is returned by +** the SQL functions that sqlite3_rtree_geometry_callback() and +** sqlite3_rtree_query_callback() create, and is read as the right-hand +** operand to the MATCH operator of an R-Tree. +*/ +struct RtreeMatchArg { + u32 magic; /* Always RTREE_GEOMETRY_MAGIC */ + RtreeGeomCallback cb; /* Info about the callback functions */ + int nParam; /* Number of parameters to the SQL function */ + sqlite3_value **apSqlParam; /* Original SQL parameter values */ + RtreeDValue aParam[1]; /* Values for parameters to the SQL function */ +}; + +#ifndef MAX +# define MAX(x,y) ((x) < (y) ? (y) : (x)) +#endif +#ifndef MIN +# define MIN(x,y) ((x) > (y) ? (y) : (x)) +#endif + +/* +** Functions to deserialize a 16 bit integer, 32 bit real number and +** 64 bit integer. The deserialized value is returned. +*/ +static int readInt16(u8 *p){ + return (p[0]<<8) + p[1]; +} +static void readCoord(u8 *p, RtreeCoord *pCoord){ + pCoord->u = ( + (((u32)p[0]) << 24) + + (((u32)p[1]) << 16) + + (((u32)p[2]) << 8) + + (((u32)p[3]) << 0) + ); +} +static i64 readInt64(u8 *p){ + return ( + (((i64)p[0]) << 56) + + (((i64)p[1]) << 48) + + (((i64)p[2]) << 40) + + (((i64)p[3]) << 32) + + (((i64)p[4]) << 24) + + (((i64)p[5]) << 16) + + (((i64)p[6]) << 8) + + (((i64)p[7]) << 0) + ); +} + +/* +** Functions to serialize a 16 bit integer, 32 bit real number and +** 64 bit integer. The value returned is the number of bytes written +** to the argument buffer (always 2, 4 and 8 respectively). +*/ +static int writeInt16(u8 *p, int i){ + p[0] = (i>> 8)&0xFF; + p[1] = (i>> 0)&0xFF; + return 2; +} +static int writeCoord(u8 *p, RtreeCoord *pCoord){ + u32 i; + assert( sizeof(RtreeCoord)==4 ); + assert( sizeof(u32)==4 ); + i = pCoord->u; + p[0] = (i>>24)&0xFF; + p[1] = (i>>16)&0xFF; + p[2] = (i>> 8)&0xFF; + p[3] = (i>> 0)&0xFF; + return 4; +} +static int writeInt64(u8 *p, i64 i){ + p[0] = (i>>56)&0xFF; + p[1] = (i>>48)&0xFF; + p[2] = (i>>40)&0xFF; + p[3] = (i>>32)&0xFF; + p[4] = (i>>24)&0xFF; + p[5] = (i>>16)&0xFF; + p[6] = (i>> 8)&0xFF; + p[7] = (i>> 0)&0xFF; + return 8; +} + +/* +** Increment the reference count of node p. +*/ +static void nodeReference(RtreeNode *p){ + if( p ){ + p->nRef++; + } +} + +/* +** Clear the content of node p (set all bytes to 0x00). +*/ +static void nodeZero(Rtree *pRtree, RtreeNode *p){ + memset(&p->zData[2], 0, pRtree->iNodeSize-2); + p->isDirty = 1; +} + +/* +** Given a node number iNode, return the corresponding key to use +** in the Rtree.aHash table. +*/ +static int nodeHash(i64 iNode){ + return iNode % HASHSIZE; +} + +/* +** Search the node hash table for node iNode. If found, return a pointer +** to it. Otherwise, return 0. +*/ +static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ + RtreeNode *p; + for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); + return p; +} + +/* +** Add node pNode to the node hash table. +*/ +static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ + int iHash; + assert( pNode->pNext==0 ); + iHash = nodeHash(pNode->iNode); + pNode->pNext = pRtree->aHash[iHash]; + pRtree->aHash[iHash] = pNode; +} + +/* +** Remove node pNode from the node hash table. +*/ +static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){ + RtreeNode **pp; + if( pNode->iNode!=0 ){ + pp = &pRtree->aHash[nodeHash(pNode->iNode)]; + for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); } + *pp = pNode->pNext; + pNode->pNext = 0; + } +} + +/* +** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), +** indicating that node has not yet been assigned a node number. It is +** assigned a node number when nodeWrite() is called to write the +** node contents out to the database. +*/ +static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){ + RtreeNode *pNode; + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); + if( pNode ){ + memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); + pNode->zData = (u8 *)&pNode[1]; + pNode->nRef = 1; + pNode->pParent = pParent; + pNode->isDirty = 1; + nodeReference(pParent); + } + return pNode; +} + +/* +** Obtain a reference to an r-tree node. +*/ +static int nodeAcquire( + Rtree *pRtree, /* R-tree structure */ + i64 iNode, /* Node number to load */ + RtreeNode *pParent, /* Either the parent node or NULL */ + RtreeNode **ppNode /* OUT: Acquired node */ +){ + int rc; + int rc2 = SQLITE_OK; + RtreeNode *pNode; + + /* Check if the requested node is already in the hash table. If so, + ** increase its reference count and return it. + */ + if( (pNode = nodeHashLookup(pRtree, iNode)) ){ + assert( !pParent || !pNode->pParent || pNode->pParent==pParent ); + if( pParent && !pNode->pParent ){ + nodeReference(pParent); + pNode->pParent = pParent; + } + pNode->nRef++; + *ppNode = pNode; + return SQLITE_OK; + } + + sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); + rc = sqlite3_step(pRtree->pReadNode); + if( rc==SQLITE_ROW ){ + const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); + if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){ + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); + if( !pNode ){ + rc2 = SQLITE_NOMEM; + }else{ + pNode->pParent = pParent; + pNode->zData = (u8 *)&pNode[1]; + pNode->nRef = 1; + pNode->iNode = iNode; + pNode->isDirty = 0; + pNode->pNext = 0; + memcpy(pNode->zData, zBlob, pRtree->iNodeSize); + nodeReference(pParent); + } + } + } + rc = sqlite3_reset(pRtree->pReadNode); + if( rc==SQLITE_OK ) rc = rc2; + + /* If the root node was just loaded, set pRtree->iDepth to the height + ** of the r-tree structure. A height of zero means all data is stored on + ** the root node. A height of one means the children of the root node + ** are the leaves, and so on. If the depth as specified on the root node + ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. + */ + if( pNode && iNode==1 ){ + pRtree->iDepth = readInt16(pNode->zData); + if( pRtree->iDepth>RTREE_MAX_DEPTH ){ + rc = SQLITE_CORRUPT_VTAB; + } + } + + /* If no error has occurred so far, check if the "number of entries" + ** field on the node is too large. If so, set the return code to + ** SQLITE_CORRUPT_VTAB. + */ + if( pNode && rc==SQLITE_OK ){ + if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){ + rc = SQLITE_CORRUPT_VTAB; + } + } + + if( rc==SQLITE_OK ){ + if( pNode!=0 ){ + nodeHashInsert(pRtree, pNode); + }else{ + rc = SQLITE_CORRUPT_VTAB; + } + *ppNode = pNode; + }else{ + sqlite3_free(pNode); + *ppNode = 0; + } + + return rc; +} + +/* +** Overwrite cell iCell of node pNode with the contents of pCell. +*/ +static void nodeOverwriteCell( + Rtree *pRtree, /* The overall R-Tree */ + RtreeNode *pNode, /* The node into which the cell is to be written */ + RtreeCell *pCell, /* The cell to write */ + int iCell /* Index into pNode into which pCell is written */ +){ + int ii; + u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; + p += writeInt64(p, pCell->iRowid); + for(ii=0; ii<(pRtree->nDim*2); ii++){ + p += writeCoord(p, &pCell->aCoord[ii]); + } + pNode->isDirty = 1; +} + +/* +** Remove the cell with index iCell from node pNode. +*/ +static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){ + u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; + u8 *pSrc = &pDst[pRtree->nBytesPerCell]; + int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell; + memmove(pDst, pSrc, nByte); + writeInt16(&pNode->zData[2], NCELL(pNode)-1); + pNode->isDirty = 1; +} + +/* +** Insert the contents of cell pCell into node pNode. If the insert +** is successful, return SQLITE_OK. +** +** If there is not enough free space in pNode, return SQLITE_FULL. +*/ +static int nodeInsertCell( + Rtree *pRtree, /* The overall R-Tree */ + RtreeNode *pNode, /* Write new cell into this node */ + RtreeCell *pCell /* The cell to be inserted */ +){ + int nCell; /* Current number of cells in pNode */ + int nMaxCell; /* Maximum number of cells for pNode */ + + nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; + nCell = NCELL(pNode); + + assert( nCell<=nMaxCell ); + if( nCellzData[2], nCell+1); + pNode->isDirty = 1; + } + + return (nCell==nMaxCell); +} + +/* +** If the node is dirty, write it out to the database. +*/ +static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){ + int rc = SQLITE_OK; + if( pNode->isDirty ){ + sqlite3_stmt *p = pRtree->pWriteNode; + if( pNode->iNode ){ + sqlite3_bind_int64(p, 1, pNode->iNode); + }else{ + sqlite3_bind_null(p, 1); + } + sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC); + sqlite3_step(p); + pNode->isDirty = 0; + rc = sqlite3_reset(p); + if( pNode->iNode==0 && rc==SQLITE_OK ){ + pNode->iNode = sqlite3_last_insert_rowid(pRtree->db); + nodeHashInsert(pRtree, pNode); + } + } + return rc; +} + +/* +** Release a reference to a node. If the node is dirty and the reference +** count drops to zero, the node data is written to the database. +*/ +static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){ + int rc = SQLITE_OK; + if( pNode ){ + assert( pNode->nRef>0 ); + pNode->nRef--; + if( pNode->nRef==0 ){ + if( pNode->iNode==1 ){ + pRtree->iDepth = -1; + } + if( pNode->pParent ){ + rc = nodeRelease(pRtree, pNode->pParent); + } + if( rc==SQLITE_OK ){ + rc = nodeWrite(pRtree, pNode); + } + nodeHashDelete(pRtree, pNode); + sqlite3_free(pNode); + } + } + return rc; +} + +/* +** Return the 64-bit integer value associated with cell iCell of +** node pNode. If pNode is a leaf node, this is a rowid. If it is +** an internal node, then the 64-bit integer is a child page number. +*/ +static i64 nodeGetRowid( + Rtree *pRtree, /* The overall R-Tree */ + RtreeNode *pNode, /* The node from which to extract the ID */ + int iCell /* The cell index from which to extract the ID */ +){ + assert( iCellzData[4 + pRtree->nBytesPerCell*iCell]); +} + +/* +** Return coordinate iCoord from cell iCell in node pNode. +*/ +static void nodeGetCoord( + Rtree *pRtree, /* The overall R-Tree */ + RtreeNode *pNode, /* The node from which to extract a coordinate */ + int iCell, /* The index of the cell within the node */ + int iCoord, /* Which coordinate to extract */ + RtreeCoord *pCoord /* OUT: Space to write result to */ +){ + readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord); +} + +/* +** Deserialize cell iCell of node pNode. Populate the structure pointed +** to by pCell with the results. +*/ +static void nodeGetCell( + Rtree *pRtree, /* The overall R-Tree */ + RtreeNode *pNode, /* The node containing the cell to be read */ + int iCell, /* Index of the cell within the node */ + RtreeCell *pCell /* OUT: Write the cell contents here */ +){ + u8 *pData; + RtreeCoord *pCoord; + int ii; + pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell); + pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell); + pCoord = pCell->aCoord; + for(ii=0; iinDim*2; ii++){ + readCoord(&pData[ii*4], &pCoord[ii]); + } +} + + +/* Forward declaration for the function that does the work of +** the virtual table module xCreate() and xConnect() methods. +*/ +static int rtreeInit( + sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int +); + +/* +** Rtree virtual table module xCreate method. +*/ +static int rtreeCreate( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1); +} + +/* +** Rtree virtual table module xConnect method. +*/ +static int rtreeConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0); +} + +/* +** Increment the r-tree reference count. +*/ +static void rtreeReference(Rtree *pRtree){ + pRtree->nBusy++; +} + +/* +** Decrement the r-tree reference count. When the reference count reaches +** zero the structure is deleted. +*/ +static void rtreeRelease(Rtree *pRtree){ + pRtree->nBusy--; + if( pRtree->nBusy==0 ){ + sqlite3_finalize(pRtree->pReadNode); + sqlite3_finalize(pRtree->pWriteNode); + sqlite3_finalize(pRtree->pDeleteNode); + sqlite3_finalize(pRtree->pReadRowid); + sqlite3_finalize(pRtree->pWriteRowid); + sqlite3_finalize(pRtree->pDeleteRowid); + sqlite3_finalize(pRtree->pReadParent); + sqlite3_finalize(pRtree->pWriteParent); + sqlite3_finalize(pRtree->pDeleteParent); + sqlite3_free(pRtree); + } +} + +/* +** Rtree virtual table module xDisconnect method. +*/ +static int rtreeDisconnect(sqlite3_vtab *pVtab){ + rtreeRelease((Rtree *)pVtab); + return SQLITE_OK; +} + +/* +** Rtree virtual table module xDestroy method. +*/ +static int rtreeDestroy(sqlite3_vtab *pVtab){ + Rtree *pRtree = (Rtree *)pVtab; + int rc; + char *zCreate = sqlite3_mprintf( + "DROP TABLE '%q'.'%q_node';" + "DROP TABLE '%q'.'%q_rowid';" + "DROP TABLE '%q'.'%q_parent';", + pRtree->zDb, pRtree->zName, + pRtree->zDb, pRtree->zName, + pRtree->zDb, pRtree->zName + ); + if( !zCreate ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0); + sqlite3_free(zCreate); + } + if( rc==SQLITE_OK ){ + rtreeRelease(pRtree); + } + + return rc; +} + +/* +** Rtree virtual table module xOpen method. +*/ +static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + int rc = SQLITE_NOMEM; + RtreeCursor *pCsr; + + pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor)); + if( pCsr ){ + memset(pCsr, 0, sizeof(RtreeCursor)); + pCsr->base.pVtab = pVTab; + rc = SQLITE_OK; + } + *ppCursor = (sqlite3_vtab_cursor *)pCsr; + + return rc; +} + + +/* +** Free the RtreeCursor.aConstraint[] array and its contents. +*/ +static void freeCursorConstraints(RtreeCursor *pCsr){ + if( pCsr->aConstraint ){ + int i; /* Used to iterate through constraint array */ + for(i=0; inConstraint; i++){ + sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo; + if( pInfo ){ + if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser); + sqlite3_free(pInfo); + } + } + sqlite3_free(pCsr->aConstraint); + pCsr->aConstraint = 0; + } +} + +/* +** Rtree virtual table module xClose method. +*/ +static int rtreeClose(sqlite3_vtab_cursor *cur){ + Rtree *pRtree = (Rtree *)(cur->pVtab); + int ii; + RtreeCursor *pCsr = (RtreeCursor *)cur; + freeCursorConstraints(pCsr); + sqlite3_free(pCsr->aPoint); + for(ii=0; iiaNode[ii]); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Rtree virtual table module xEof method. +** +** Return non-zero if the cursor does not currently point to a valid +** record (i.e if the scan has finished), or zero otherwise. +*/ +static int rtreeEof(sqlite3_vtab_cursor *cur){ + RtreeCursor *pCsr = (RtreeCursor *)cur; + return pCsr->atEOF; +} + +/* +** Convert raw bits from the on-disk RTree record into a coordinate value. +** The on-disk format is big-endian and needs to be converted for little- +** endian platforms. The on-disk record stores integer coordinates if +** eInt is true and it stores 32-bit floating point records if eInt is +** false. a[] is the four bytes of the on-disk record to be decoded. +** Store the results in "r". +** +** There are three versions of this macro, one each for little-endian and +** big-endian processors and a third generic implementation. The endian- +** specific implementations are much faster and are preferred if the +** processor endianness is known at compile-time. The SQLITE_BYTEORDER +** macro is part of sqliteInt.h and hence the endian-specific +** implementation will only be used if this module is compiled as part +** of the amalgamation. +*/ +#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234 +#define RTREE_DECODE_COORD(eInt, a, r) { \ + RtreeCoord c; /* Coordinate decoded */ \ + memcpy(&c.u,a,4); \ + c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \ + ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \ + r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ +} +#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321 +#define RTREE_DECODE_COORD(eInt, a, r) { \ + RtreeCoord c; /* Coordinate decoded */ \ + memcpy(&c.u,a,4); \ + r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ +} +#else +#define RTREE_DECODE_COORD(eInt, a, r) { \ + RtreeCoord c; /* Coordinate decoded */ \ + c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \ + +((u32)a[2]<<8) + a[3]; \ + r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ +} +#endif + +/* +** Check the RTree node or entry given by pCellData and p against the MATCH +** constraint pConstraint. +*/ +static int rtreeCallbackConstraint( + RtreeConstraint *pConstraint, /* The constraint to test */ + int eInt, /* True if RTree holding integer coordinates */ + u8 *pCellData, /* Raw cell content */ + RtreeSearchPoint *pSearch, /* Container of this cell */ + sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */ + int *peWithin /* OUT: visibility of the cell */ +){ + int i; /* Loop counter */ + sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */ + int nCoord = pInfo->nCoord; /* No. of coordinates */ + int rc; /* Callback return code */ + sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */ + + assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY ); + assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 ); + + if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){ + pInfo->iRowid = readInt64(pCellData); + } + pCellData += 8; + for(i=0; iop==RTREE_MATCH ){ + rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo, + nCoord, aCoord, &i); + if( i==0 ) *peWithin = NOT_WITHIN; + *prScore = RTREE_ZERO; + }else{ + pInfo->aCoord = aCoord; + pInfo->iLevel = pSearch->iLevel - 1; + pInfo->rScore = pInfo->rParentScore = pSearch->rScore; + pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin; + rc = pConstraint->u.xQueryFunc(pInfo); + if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin; + if( pInfo->rScore<*prScore || *prScorerScore; + } + } + return rc; +} + +/* +** Check the internal RTree node given by pCellData against constraint p. +** If this constraint cannot be satisfied by any child within the node, +** set *peWithin to NOT_WITHIN. +*/ +static void rtreeNonleafConstraint( + RtreeConstraint *p, /* The constraint to test */ + int eInt, /* True if RTree holds integer coordinates */ + u8 *pCellData, /* Raw cell content as appears on disk */ + int *peWithin /* Adjust downward, as appropriate */ +){ + sqlite3_rtree_dbl val; /* Coordinate value convert to a double */ + + /* p->iCoord might point to either a lower or upper bound coordinate + ** in a coordinate pair. But make pCellData point to the lower bound. + */ + pCellData += 8 + 4*(p->iCoord&0xfe); + + assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE + || p->op==RTREE_GT || p->op==RTREE_EQ ); + switch( p->op ){ + case RTREE_LE: + case RTREE_LT: + case RTREE_EQ: + RTREE_DECODE_COORD(eInt, pCellData, val); + /* val now holds the lower bound of the coordinate pair */ + if( p->u.rValue>=val ) return; + if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */ + /* Fall through for the RTREE_EQ case */ + + default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */ + pCellData += 4; + RTREE_DECODE_COORD(eInt, pCellData, val); + /* val now holds the upper bound of the coordinate pair */ + if( p->u.rValue<=val ) return; + } + *peWithin = NOT_WITHIN; +} + +/* +** Check the leaf RTree cell given by pCellData against constraint p. +** If this constraint is not satisfied, set *peWithin to NOT_WITHIN. +** If the constraint is satisfied, leave *peWithin unchanged. +** +** The constraint is of the form: xN op $val +** +** The op is given by p->op. The xN is p->iCoord-th coordinate in +** pCellData. $val is given by p->u.rValue. +*/ +static void rtreeLeafConstraint( + RtreeConstraint *p, /* The constraint to test */ + int eInt, /* True if RTree holds integer coordinates */ + u8 *pCellData, /* Raw cell content as appears on disk */ + int *peWithin /* Adjust downward, as appropriate */ +){ + RtreeDValue xN; /* Coordinate value converted to a double */ + + assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE + || p->op==RTREE_GT || p->op==RTREE_EQ ); + pCellData += 8 + p->iCoord*4; + RTREE_DECODE_COORD(eInt, pCellData, xN); + switch( p->op ){ + case RTREE_LE: if( xN <= p->u.rValue ) return; break; + case RTREE_LT: if( xN < p->u.rValue ) return; break; + case RTREE_GE: if( xN >= p->u.rValue ) return; break; + case RTREE_GT: if( xN > p->u.rValue ) return; break; + default: if( xN == p->u.rValue ) return; break; + } + *peWithin = NOT_WITHIN; +} + +/* +** One of the cells in node pNode is guaranteed to have a 64-bit +** integer value equal to iRowid. Return the index of this cell. +*/ +static int nodeRowidIndex( + Rtree *pRtree, + RtreeNode *pNode, + i64 iRowid, + int *piIndex +){ + int ii; + int nCell = NCELL(pNode); + assert( nCell<200 ); + for(ii=0; iipParent; + if( pParent ){ + return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex); + } + *piIndex = -1; + return SQLITE_OK; +} + +/* +** Compare two search points. Return negative, zero, or positive if the first +** is less than, equal to, or greater than the second. +** +** The rScore is the primary key. Smaller rScore values come first. +** If the rScore is a tie, then use iLevel as the tie breaker with smaller +** iLevel values coming first. In this way, if rScore is the same for all +** SearchPoints, then iLevel becomes the deciding factor and the result +** is a depth-first search, which is the desired default behavior. +*/ +static int rtreeSearchPointCompare( + const RtreeSearchPoint *pA, + const RtreeSearchPoint *pB +){ + if( pA->rScorerScore ) return -1; + if( pA->rScore>pB->rScore ) return +1; + if( pA->iLeveliLevel ) return -1; + if( pA->iLevel>pB->iLevel ) return +1; + return 0; +} + +/* +** Interchange to search points in a cursor. +*/ +static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){ + RtreeSearchPoint t = p->aPoint[i]; + assert( iaPoint[i] = p->aPoint[j]; + p->aPoint[j] = t; + i++; j++; + if( i=RTREE_CACHE_SZ ){ + nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]); + p->aNode[i] = 0; + }else{ + RtreeNode *pTemp = p->aNode[i]; + p->aNode[i] = p->aNode[j]; + p->aNode[j] = pTemp; + } + } +} + +/* +** Return the search point with the lowest current score. +*/ +static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){ + return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0; +} + +/* +** Get the RtreeNode for the search point with the lowest score. +*/ +static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){ + sqlite3_int64 id; + int ii = 1 - pCur->bPoint; + assert( ii==0 || ii==1 ); + assert( pCur->bPoint || pCur->nPoint ); + if( pCur->aNode[ii]==0 ){ + assert( pRC!=0 ); + id = ii ? pCur->aPoint[0].id : pCur->sPoint.id; + *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]); + } + return pCur->aNode[ii]; +} + +/* +** Push a new element onto the priority queue +*/ +static RtreeSearchPoint *rtreeEnqueue( + RtreeCursor *pCur, /* The cursor */ + RtreeDValue rScore, /* Score for the new search point */ + u8 iLevel /* Level for the new search point */ +){ + int i, j; + RtreeSearchPoint *pNew; + if( pCur->nPoint>=pCur->nPointAlloc ){ + int nNew = pCur->nPointAlloc*2 + 8; + pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0])); + if( pNew==0 ) return 0; + pCur->aPoint = pNew; + pCur->nPointAlloc = nNew; + } + i = pCur->nPoint++; + pNew = pCur->aPoint + i; + pNew->rScore = rScore; + pNew->iLevel = iLevel; + assert( iLevel<=RTREE_MAX_DEPTH ); + while( i>0 ){ + RtreeSearchPoint *pParent; + j = (i-1)/2; + pParent = pCur->aPoint + j; + if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break; + rtreeSearchPointSwap(pCur, j, i); + i = j; + pNew = pParent; + } + return pNew; +} + +/* +** Allocate a new RtreeSearchPoint and return a pointer to it. Return +** NULL if malloc fails. +*/ +static RtreeSearchPoint *rtreeSearchPointNew( + RtreeCursor *pCur, /* The cursor */ + RtreeDValue rScore, /* Score for the new search point */ + u8 iLevel /* Level for the new search point */ +){ + RtreeSearchPoint *pNew, *pFirst; + pFirst = rtreeSearchPointFirst(pCur); + pCur->anQueue[iLevel]++; + if( pFirst==0 + || pFirst->rScore>rScore + || (pFirst->rScore==rScore && pFirst->iLevel>iLevel) + ){ + if( pCur->bPoint ){ + int ii; + pNew = rtreeEnqueue(pCur, rScore, iLevel); + if( pNew==0 ) return 0; + ii = (int)(pNew - pCur->aPoint) + 1; + if( iiaNode[ii]==0 ); + pCur->aNode[ii] = pCur->aNode[0]; + }else{ + nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]); + } + pCur->aNode[0] = 0; + *pNew = pCur->sPoint; + } + pCur->sPoint.rScore = rScore; + pCur->sPoint.iLevel = iLevel; + pCur->bPoint = 1; + return &pCur->sPoint; + }else{ + return rtreeEnqueue(pCur, rScore, iLevel); + } +} + +#if 0 +/* Tracing routines for the RtreeSearchPoint queue */ +static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){ + if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); } + printf(" %d.%05lld.%02d %g %d", + p->iLevel, p->id, p->iCell, p->rScore, p->eWithin + ); + idx++; + if( idxaNode[idx]); + }else{ + printf("\n"); + } +} +static void traceQueue(RtreeCursor *pCur, const char *zPrefix){ + int ii; + printf("=== %9s ", zPrefix); + if( pCur->bPoint ){ + tracePoint(&pCur->sPoint, -1, pCur); + } + for(ii=0; iinPoint; ii++){ + if( ii>0 || pCur->bPoint ) printf(" "); + tracePoint(&pCur->aPoint[ii], ii, pCur); + } +} +# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B) +#else +# define RTREE_QUEUE_TRACE(A,B) /* no-op */ +#endif + +/* Remove the search point with the lowest current score. +*/ +static void rtreeSearchPointPop(RtreeCursor *p){ + int i, j, k, n; + i = 1 - p->bPoint; + assert( i==0 || i==1 ); + if( p->aNode[i] ){ + nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]); + p->aNode[i] = 0; + } + if( p->bPoint ){ + p->anQueue[p->sPoint.iLevel]--; + p->bPoint = 0; + }else if( p->nPoint ){ + p->anQueue[p->aPoint[0].iLevel]--; + n = --p->nPoint; + p->aPoint[0] = p->aPoint[n]; + if( naNode[1] = p->aNode[n+1]; + p->aNode[n+1] = 0; + } + i = 0; + while( (j = i*2+1)aPoint[k], &p->aPoint[j])<0 ){ + if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){ + rtreeSearchPointSwap(p, i, k); + i = k; + }else{ + break; + } + }else{ + if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){ + rtreeSearchPointSwap(p, i, j); + i = j; + }else{ + break; + } + } + } + } +} + + +/* +** Continue the search on cursor pCur until the front of the queue +** contains an entry suitable for returning as a result-set row, +** or until the RtreeSearchPoint queue is empty, indicating that the +** query has completed. +*/ +static int rtreeStepToLeaf(RtreeCursor *pCur){ + RtreeSearchPoint *p; + Rtree *pRtree = RTREE_OF_CURSOR(pCur); + RtreeNode *pNode; + int eWithin; + int rc = SQLITE_OK; + int nCell; + int nConstraint = pCur->nConstraint; + int ii; + int eInt; + RtreeSearchPoint x; + + eInt = pRtree->eCoordType==RTREE_COORD_INT32; + while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){ + pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc); + if( rc ) return rc; + nCell = NCELL(pNode); + assert( nCell<200 ); + while( p->iCellzData + (4+pRtree->nBytesPerCell*p->iCell); + eWithin = FULLY_WITHIN; + for(ii=0; iiaConstraint + ii; + if( pConstraint->op>=RTREE_MATCH ){ + rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p, + &rScore, &eWithin); + if( rc ) return rc; + }else if( p->iLevel==1 ){ + rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin); + }else{ + rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin); + } + if( eWithin==NOT_WITHIN ) break; + } + p->iCell++; + if( eWithin==NOT_WITHIN ) continue; + x.iLevel = p->iLevel - 1; + if( x.iLevel ){ + x.id = readInt64(pCellData); + x.iCell = 0; + }else{ + x.id = p->id; + x.iCell = p->iCell - 1; + } + if( p->iCell>=nCell ){ + RTREE_QUEUE_TRACE(pCur, "POP-S:"); + rtreeSearchPointPop(pCur); + } + if( rScoreeWithin = eWithin; + p->id = x.id; + p->iCell = x.iCell; + RTREE_QUEUE_TRACE(pCur, "PUSH-S:"); + break; + } + if( p->iCell>=nCell ){ + RTREE_QUEUE_TRACE(pCur, "POP-Se:"); + rtreeSearchPointPop(pCur); + } + } + pCur->atEOF = p==0; + return SQLITE_OK; +} + +/* +** Rtree virtual table module xNext method. +*/ +static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + int rc = SQLITE_OK; + + /* Move to the next entry that matches the configured constraints. */ + RTREE_QUEUE_TRACE(pCsr, "POP-Nx:"); + rtreeSearchPointPop(pCsr); + rc = rtreeStepToLeaf(pCsr); + return rc; +} + +/* +** Rtree virtual table module xRowid method. +*/ +static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){ + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr); + int rc = SQLITE_OK; + RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc); + if( rc==SQLITE_OK && p ){ + *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell); + } + return rc; +} + +/* +** Rtree virtual table module xColumn method. +*/ +static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ + Rtree *pRtree = (Rtree *)cur->pVtab; + RtreeCursor *pCsr = (RtreeCursor *)cur; + RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr); + RtreeCoord c; + int rc = SQLITE_OK; + RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc); + + if( rc ) return rc; + if( p==0 ) return SQLITE_OK; + if( i==0 ){ + sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell)); + }else{ + if( rc ) return rc; + nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c); +#ifndef SQLITE_RTREE_INT_ONLY + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + sqlite3_result_double(ctx, c.f); + }else +#endif + { + assert( pRtree->eCoordType==RTREE_COORD_INT32 ); + sqlite3_result_int(ctx, c.i); + } + } + return SQLITE_OK; +} + +/* +** Use nodeAcquire() to obtain the leaf node containing the record with +** rowid iRowid. If successful, set *ppLeaf to point to the node and +** return SQLITE_OK. If there is no such record in the table, set +** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf +** to zero and return an SQLite error code. +*/ +static int findLeafNode( + Rtree *pRtree, /* RTree to search */ + i64 iRowid, /* The rowid searching for */ + RtreeNode **ppLeaf, /* Write the node here */ + sqlite3_int64 *piNode /* Write the node-id here */ +){ + int rc; + *ppLeaf = 0; + sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid); + if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){ + i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0); + if( piNode ) *piNode = iNode; + rc = nodeAcquire(pRtree, iNode, 0, ppLeaf); + sqlite3_reset(pRtree->pReadRowid); + }else{ + rc = sqlite3_reset(pRtree->pReadRowid); + } + return rc; +} + +/* +** This function is called to configure the RtreeConstraint object passed +** as the second argument for a MATCH constraint. The value passed as the +** first argument to this function is the right-hand operand to the MATCH +** operator. +*/ +static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){ + RtreeMatchArg *pBlob; /* BLOB returned by geometry function */ + sqlite3_rtree_query_info *pInfo; /* Callback information */ + int nBlob; /* Size of the geometry function blob */ + int nExpected; /* Expected size of the BLOB */ + + /* Check that value is actually a blob. */ + if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR; + + /* Check that the blob is roughly the right size. */ + nBlob = sqlite3_value_bytes(pValue); + if( nBlob<(int)sizeof(RtreeMatchArg) ){ + return SQLITE_ERROR; + } + + pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob ); + if( !pInfo ) return SQLITE_NOMEM; + memset(pInfo, 0, sizeof(*pInfo)); + pBlob = (RtreeMatchArg*)&pInfo[1]; + + memcpy(pBlob, sqlite3_value_blob(pValue), nBlob); + nExpected = (int)(sizeof(RtreeMatchArg) + + pBlob->nParam*sizeof(sqlite3_value*) + + (pBlob->nParam-1)*sizeof(RtreeDValue)); + if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){ + sqlite3_free(pInfo); + return SQLITE_ERROR; + } + pInfo->pContext = pBlob->cb.pContext; + pInfo->nParam = pBlob->nParam; + pInfo->aParam = pBlob->aParam; + pInfo->apSqlParam = pBlob->apSqlParam; + + if( pBlob->cb.xGeom ){ + pCons->u.xGeom = pBlob->cb.xGeom; + }else{ + pCons->op = RTREE_QUERY; + pCons->u.xQueryFunc = pBlob->cb.xQueryFunc; + } + pCons->pInfo = pInfo; + return SQLITE_OK; +} + +/* +** Rtree virtual table module xFilter method. +*/ +static int rtreeFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + Rtree *pRtree = (Rtree *)pVtabCursor->pVtab; + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + RtreeNode *pRoot = 0; + int ii; + int rc = SQLITE_OK; + int iCell = 0; + + rtreeReference(pRtree); + + /* Reset the cursor to the same state as rtreeOpen() leaves it in. */ + freeCursorConstraints(pCsr); + sqlite3_free(pCsr->aPoint); + memset(pCsr, 0, sizeof(RtreeCursor)); + pCsr->base.pVtab = (sqlite3_vtab*)pRtree; + + pCsr->iStrategy = idxNum; + if( idxNum==1 ){ + /* Special case - lookup by rowid. */ + RtreeNode *pLeaf; /* Leaf on which the required cell resides */ + RtreeSearchPoint *p; /* Search point for the the leaf */ + i64 iRowid = sqlite3_value_int64(argv[0]); + i64 iNode = 0; + rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode); + if( rc==SQLITE_OK && pLeaf!=0 ){ + p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0); + assert( p!=0 ); /* Always returns pCsr->sPoint */ + pCsr->aNode[0] = pLeaf; + p->id = iNode; + p->eWithin = PARTLY_WITHIN; + rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell); + p->iCell = iCell; + RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:"); + }else{ + pCsr->atEOF = 1; + } + }else{ + /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array + ** with the configured constraints. + */ + rc = nodeAcquire(pRtree, 1, 0, &pRoot); + if( rc==SQLITE_OK && argc>0 ){ + pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); + pCsr->nConstraint = argc; + if( !pCsr->aConstraint ){ + rc = SQLITE_NOMEM; + }else{ + memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); + memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1)); + assert( (idxStr==0 && argc==0) + || (idxStr && (int)strlen(idxStr)==argc*2) ); + for(ii=0; iiaConstraint[ii]; + p->op = idxStr[ii*2]; + p->iCoord = idxStr[ii*2+1]-'0'; + if( p->op>=RTREE_MATCH ){ + /* A MATCH operator. The right-hand-side must be a blob that + ** can be cast into an RtreeMatchArg object. One created using + ** an sqlite3_rtree_geometry_callback() SQL user function. + */ + rc = deserializeGeometry(argv[ii], p); + if( rc!=SQLITE_OK ){ + break; + } + p->pInfo->nCoord = pRtree->nDim*2; + p->pInfo->anQueue = pCsr->anQueue; + p->pInfo->mxLevel = pRtree->iDepth + 1; + }else{ +#ifdef SQLITE_RTREE_INT_ONLY + p->u.rValue = sqlite3_value_int64(argv[ii]); +#else + p->u.rValue = sqlite3_value_double(argv[ii]); +#endif + } + } + } + } + if( rc==SQLITE_OK ){ + RtreeSearchPoint *pNew; + pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1); + if( pNew==0 ) return SQLITE_NOMEM; + pNew->id = 1; + pNew->iCell = 0; + pNew->eWithin = PARTLY_WITHIN; + assert( pCsr->bPoint==1 ); + pCsr->aNode[0] = pRoot; + pRoot = 0; + RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:"); + rc = rtreeStepToLeaf(pCsr); + } + } + + nodeRelease(pRtree, pRoot); + rtreeRelease(pRtree); + return rc; +} + +/* +** Set the pIdxInfo->estimatedRows variable to nRow. Unless this +** extension is currently being used by a version of SQLite too old to +** support estimatedRows. In that case this function is a no-op. +*/ +static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){ +#if SQLITE_VERSION_NUMBER>=3008002 + if( sqlite3_libversion_number()>=3008002 ){ + pIdxInfo->estimatedRows = nRow; + } +#endif +} + +/* +** Rtree virtual table module xBestIndex method. There are three +** table scan strategies to choose from (in order from most to +** least desirable): +** +** idxNum idxStr Strategy +** ------------------------------------------------ +** 1 Unused Direct lookup by rowid. +** 2 See below R-tree query or full-table scan. +** ------------------------------------------------ +** +** If strategy 1 is used, then idxStr is not meaningful. If strategy +** 2 is used, idxStr is formatted to contain 2 bytes for each +** constraint used. The first two bytes of idxStr correspond to +** the constraint in sqlite3_index_info.aConstraintUsage[] with +** (argvIndex==1) etc. +** +** The first of each pair of bytes in idxStr identifies the constraint +** operator as follows: +** +** Operator Byte Value +** ---------------------- +** = 0x41 ('A') +** <= 0x42 ('B') +** < 0x43 ('C') +** >= 0x44 ('D') +** > 0x45 ('E') +** MATCH 0x46 ('F') +** ---------------------- +** +** The second of each pair of bytes identifies the coordinate column +** to which the constraint applies. The leftmost coordinate column +** is 'a', the second from the left 'b' etc. +*/ +static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + Rtree *pRtree = (Rtree*)tab; + int rc = SQLITE_OK; + int ii; + int bMatch = 0; /* True if there exists a MATCH constraint */ + i64 nRow; /* Estimated rows returned by this scan */ + + int iIdx = 0; + char zIdxStr[RTREE_MAX_DIMENSIONS*8+1]; + memset(zIdxStr, 0, sizeof(zIdxStr)); + + /* Check if there exists a MATCH constraint - even an unusable one. If there + ** is, do not consider the lookup-by-rowid plan as using such a plan would + ** require the VDBE to evaluate the MATCH constraint, which is not currently + ** possible. */ + for(ii=0; iinConstraint; ii++){ + if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){ + bMatch = 1; + } + } + + assert( pIdxInfo->idxStr==0 ); + for(ii=0; iinConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii]; + + if( bMatch==0 && p->usable + && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ + ){ + /* We have an equality constraint on the rowid. Use strategy 1. */ + int jj; + for(jj=0; jjaConstraintUsage[jj].argvIndex = 0; + pIdxInfo->aConstraintUsage[jj].omit = 0; + } + pIdxInfo->idxNum = 1; + pIdxInfo->aConstraintUsage[ii].argvIndex = 1; + pIdxInfo->aConstraintUsage[jj].omit = 1; + + /* This strategy involves a two rowid lookups on an B-Tree structures + ** and then a linear search of an R-Tree node. This should be + ** considered almost as quick as a direct rowid lookup (for which + ** sqlite uses an internal cost of 0.0). It is expected to return + ** a single row. + */ + pIdxInfo->estimatedCost = 30.0; + setEstimatedRows(pIdxInfo, 1); + return SQLITE_OK; + } + + if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ + u8 op; + switch( p->op ){ + case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; + case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; + case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; + case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; + case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; + default: + assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); + op = RTREE_MATCH; + break; + } + zIdxStr[iIdx++] = op; + zIdxStr[iIdx++] = p->iColumn - 1 + '0'; + pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2); + pIdxInfo->aConstraintUsage[ii].omit = 1; + } + } + + pIdxInfo->idxNum = 2; + pIdxInfo->needToFreeIdxStr = 1; + if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){ + return SQLITE_NOMEM; + } + + nRow = pRtree->nRowEst >> (iIdx/2); + pIdxInfo->estimatedCost = (double)6.0 * (double)nRow; + setEstimatedRows(pIdxInfo, nRow); + + return rc; +} + +/* +** Return the N-dimensional volumn of the cell stored in *p. +*/ +static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){ + RtreeDValue area = (RtreeDValue)1; + int ii; + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]))); + } + return area; +} + +/* +** Return the margin length of cell p. The margin length is the sum +** of the objects size in each dimension. +*/ +static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){ + RtreeDValue margin = (RtreeDValue)0; + int ii; + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); + } + return margin; +} + +/* +** Store the union of cells p1 and p2 in p1. +*/ +static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ + int ii; + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f); + p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f); + } + }else{ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i); + p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i); + } + } +} + +/* +** Return true if the area covered by p2 is a subset of the area covered +** by p1. False otherwise. +*/ +static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ + int ii; + int isInt = (pRtree->eCoordType==RTREE_COORD_INT32); + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + RtreeCoord *a1 = &p1->aCoord[ii]; + RtreeCoord *a2 = &p2->aCoord[ii]; + if( (!isInt && (a2[0].fa1[1].f)) + || ( isInt && (a2[0].ia1[1].i)) + ){ + return 0; + } + } + return 1; +} + +/* +** Return the amount cell p would grow by if it were unioned with pCell. +*/ +static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){ + RtreeDValue area; + RtreeCell cell; + memcpy(&cell, p, sizeof(RtreeCell)); + area = cellArea(pRtree, &cell); + cellUnion(pRtree, &cell, pCell); + return (cellArea(pRtree, &cell)-area); +} + +static RtreeDValue cellOverlap( + Rtree *pRtree, + RtreeCell *p, + RtreeCell *aCell, + int nCell +){ + int ii; + RtreeDValue overlap = RTREE_ZERO; + for(ii=0; iinDim*2); jj+=2){ + RtreeDValue x1, x2; + x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj])); + x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1])); + if( x2iDepth-iHeight); ii++){ + int iCell; + sqlite3_int64 iBest = 0; + + RtreeDValue fMinGrowth = RTREE_ZERO; + RtreeDValue fMinArea = RTREE_ZERO; + + int nCell = NCELL(pNode); + RtreeCell cell; + RtreeNode *pChild; + + RtreeCell *aCell = 0; + + /* Select the child node which will be enlarged the least if pCell + ** is inserted into it. Resolve ties by choosing the entry with + ** the smallest area. + */ + for(iCell=0; iCellpParent ){ + RtreeNode *pParent = p->pParent; + RtreeCell cell; + int iCell; + + if( nodeParentIndex(pRtree, p, &iCell) ){ + return SQLITE_CORRUPT_VTAB; + } + + nodeGetCell(pRtree, pParent, iCell, &cell); + if( !cellContains(pRtree, &cell, pCell) ){ + cellUnion(pRtree, &cell, pCell); + nodeOverwriteCell(pRtree, pParent, &cell, iCell); + } + + p = pParent; + } + return SQLITE_OK; +} + +/* +** Write mapping (iRowid->iNode) to the _rowid table. +*/ +static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){ + sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid); + sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode); + sqlite3_step(pRtree->pWriteRowid); + return sqlite3_reset(pRtree->pWriteRowid); +} + +/* +** Write mapping (iNode->iPar) to the _parent table. +*/ +static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){ + sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode); + sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar); + sqlite3_step(pRtree->pWriteParent); + return sqlite3_reset(pRtree->pWriteParent); +} + +static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int); + + +/* +** Arguments aIdx, aDistance and aSpare all point to arrays of size +** nIdx. The aIdx array contains the set of integers from 0 to +** (nIdx-1) in no particular order. This function sorts the values +** in aIdx according to the indexed values in aDistance. For +** example, assuming the inputs: +** +** aIdx = { 0, 1, 2, 3 } +** aDistance = { 5.0, 2.0, 7.0, 6.0 } +** +** this function sets the aIdx array to contain: +** +** aIdx = { 0, 1, 2, 3 } +** +** The aSpare array is used as temporary working space by the +** sorting algorithm. +*/ +static void SortByDistance( + int *aIdx, + int nIdx, + RtreeDValue *aDistance, + int *aSpare +){ + if( nIdx>1 ){ + int iLeft = 0; + int iRight = 0; + + int nLeft = nIdx/2; + int nRight = nIdx-nLeft; + int *aLeft = aIdx; + int *aRight = &aIdx[nLeft]; + + SortByDistance(aLeft, nLeft, aDistance, aSpare); + SortByDistance(aRight, nRight, aDistance, aSpare); + + memcpy(aSpare, aLeft, sizeof(int)*nLeft); + aLeft = aSpare; + + while( iLeft1 ){ + + int iLeft = 0; + int iRight = 0; + + int nLeft = nIdx/2; + int nRight = nIdx-nLeft; + int *aLeft = aIdx; + int *aRight = &aIdx[nLeft]; + + SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare); + SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare); + + memcpy(aSpare, aLeft, sizeof(int)*nLeft); + aLeft = aSpare; + while( iLeftnDim+1)*(sizeof(int*)+nCell*sizeof(int)); + + aaSorted = (int **)sqlite3_malloc(nByte); + if( !aaSorted ){ + return SQLITE_NOMEM; + } + + aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell]; + memset(aaSorted, 0, nByte); + for(ii=0; iinDim; ii++){ + int jj; + aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell]; + for(jj=0; jjnDim; ii++){ + RtreeDValue margin = RTREE_ZERO; + RtreeDValue fBestOverlap = RTREE_ZERO; + RtreeDValue fBestArea = RTREE_ZERO; + int iBestLeft = 0; + int nLeft; + + for( + nLeft=RTREE_MINCELLS(pRtree); + nLeft<=(nCell-RTREE_MINCELLS(pRtree)); + nLeft++ + ){ + RtreeCell left; + RtreeCell right; + int kk; + RtreeDValue overlap; + RtreeDValue area; + + memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell)); + memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell)); + for(kk=1; kk<(nCell-1); kk++){ + if( kk0 ){ + RtreeNode *pChild = nodeHashLookup(pRtree, iRowid); + if( pChild ){ + nodeRelease(pRtree, pChild->pParent); + nodeReference(pNode); + pChild->pParent = pNode; + } + } + return xSetMapping(pRtree, iRowid, pNode->iNode); +} + +static int SplitNode( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell, + int iHeight +){ + int i; + int newCellIsRight = 0; + + int rc = SQLITE_OK; + int nCell = NCELL(pNode); + RtreeCell *aCell; + int *aiUsed; + + RtreeNode *pLeft = 0; + RtreeNode *pRight = 0; + + RtreeCell leftbbox; + RtreeCell rightbbox; + + /* Allocate an array and populate it with a copy of pCell and + ** all cells from node pLeft. Then zero the original node. + */ + aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1)); + if( !aCell ){ + rc = SQLITE_NOMEM; + goto splitnode_out; + } + aiUsed = (int *)&aCell[nCell+1]; + memset(aiUsed, 0, sizeof(int)*(nCell+1)); + for(i=0; iiNode==1 ){ + pRight = nodeNew(pRtree, pNode); + pLeft = nodeNew(pRtree, pNode); + pRtree->iDepth++; + pNode->isDirty = 1; + writeInt16(pNode->zData, pRtree->iDepth); + }else{ + pLeft = pNode; + pRight = nodeNew(pRtree, pLeft->pParent); + nodeReference(pLeft); + } + + if( !pLeft || !pRight ){ + rc = SQLITE_NOMEM; + goto splitnode_out; + } + + memset(pLeft->zData, 0, pRtree->iNodeSize); + memset(pRight->zData, 0, pRtree->iNodeSize); + + rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight, + &leftbbox, &rightbbox); + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + + /* Ensure both child nodes have node numbers assigned to them by calling + ** nodeWrite(). Node pRight always needs a node number, as it was created + ** by nodeNew() above. But node pLeft sometimes already has a node number. + ** In this case avoid the all to nodeWrite(). + */ + if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)) + || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) + ){ + goto splitnode_out; + } + + rightbbox.iRowid = pRight->iNode; + leftbbox.iRowid = pLeft->iNode; + + if( pNode->iNode==1 ){ + rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1); + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + }else{ + RtreeNode *pParent = pLeft->pParent; + int iCell; + rc = nodeParentIndex(pRtree, pLeft, &iCell); + if( rc==SQLITE_OK ){ + nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); + rc = AdjustTree(pRtree, pParent, &leftbbox); + } + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + } + if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ + goto splitnode_out; + } + + for(i=0; iiRowid ){ + newCellIsRight = 1; + } + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + } + if( pNode->iNode==1 ){ + for(i=0; iiRowid, pLeft, iHeight); + } + + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pRight); + pRight = 0; + } + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pLeft); + pLeft = 0; + } + +splitnode_out: + nodeRelease(pRtree, pRight); + nodeRelease(pRtree, pLeft); + sqlite3_free(aCell); + return rc; +} + +/* +** If node pLeaf is not the root of the r-tree and its pParent pointer is +** still NULL, load all ancestor nodes of pLeaf into memory and populate +** the pLeaf->pParent chain all the way up to the root node. +** +** This operation is required when a row is deleted (or updated - an update +** is implemented as a delete followed by an insert). SQLite provides the +** rowid of the row to delete, which can be used to find the leaf on which +** the entry resides (argument pLeaf). Once the leaf is located, this +** function is called to determine its ancestry. +*/ +static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ + int rc = SQLITE_OK; + RtreeNode *pChild = pLeaf; + while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){ + int rc2 = SQLITE_OK; /* sqlite3_reset() return code */ + sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode); + rc = sqlite3_step(pRtree->pReadParent); + if( rc==SQLITE_ROW ){ + RtreeNode *pTest; /* Used to test for reference loops */ + i64 iNode; /* Node number of parent node */ + + /* Before setting pChild->pParent, test that we are not creating a + ** loop of references (as we would if, say, pChild==pParent). We don't + ** want to do this as it leads to a memory leak when trying to delete + ** the referenced counted node structures. + */ + iNode = sqlite3_column_int64(pRtree->pReadParent, 0); + for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent); + if( !pTest ){ + rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent); + } + } + rc = sqlite3_reset(pRtree->pReadParent); + if( rc==SQLITE_OK ) rc = rc2; + if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB; + pChild = pChild->pParent; + } + return rc; +} + +static int deleteCell(Rtree *, RtreeNode *, int, int); + +static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ + int rc; + int rc2; + RtreeNode *pParent = 0; + int iCell; + + assert( pNode->nRef==1 ); + + /* Remove the entry in the parent cell. */ + rc = nodeParentIndex(pRtree, pNode, &iCell); + if( rc==SQLITE_OK ){ + pParent = pNode->pParent; + pNode->pParent = 0; + rc = deleteCell(pRtree, pParent, iCell, iHeight+1); + } + rc2 = nodeRelease(pRtree, pParent); + if( rc==SQLITE_OK ){ + rc = rc2; + } + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Remove the xxx_node entry. */ + sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); + sqlite3_step(pRtree->pDeleteNode); + if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){ + return rc; + } + + /* Remove the xxx_parent entry. */ + sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode); + sqlite3_step(pRtree->pDeleteParent); + if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){ + return rc; + } + + /* Remove the node from the in-memory hash table and link it into + ** the Rtree.pDeleted list. Its contents will be re-inserted later on. + */ + nodeHashDelete(pRtree, pNode); + pNode->iNode = iHeight; + pNode->pNext = pRtree->pDeleted; + pNode->nRef++; + pRtree->pDeleted = pNode; + + return SQLITE_OK; +} + +static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ + RtreeNode *pParent = pNode->pParent; + int rc = SQLITE_OK; + if( pParent ){ + int ii; + int nCell = NCELL(pNode); + RtreeCell box; /* Bounding box for pNode */ + nodeGetCell(pRtree, pNode, 0, &box); + for(ii=1; iiiNode; + rc = nodeParentIndex(pRtree, pNode, &ii); + if( rc==SQLITE_OK ){ + nodeOverwriteCell(pRtree, pParent, &box, ii); + rc = fixBoundingBox(pRtree, pParent); + } + } + return rc; +} + +/* +** Delete the cell at index iCell of node pNode. After removing the +** cell, adjust the r-tree data structure if required. +*/ +static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ + RtreeNode *pParent; + int rc; + + if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ + return rc; + } + + /* Remove the cell from the node. This call just moves bytes around + ** the in-memory node image, so it cannot fail. + */ + nodeDeleteCell(pRtree, pNode, iCell); + + /* If the node is not the tree root and now has less than the minimum + ** number of cells, remove it from the tree. Otherwise, update the + ** cell in the parent node so that it tightly contains the updated + ** node. + */ + pParent = pNode->pParent; + assert( pParent || pNode->iNode==1 ); + if( pParent ){ + if( NCELL(pNode)nDim; iDim++){ + aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]); + aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]); + } + } + for(iDim=0; iDimnDim; iDim++){ + aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2)); + } + + for(ii=0; iinDim; iDim++){ + RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - + DCOORD(aCell[ii].aCoord[iDim*2])); + aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]); + } + } + + SortByDistance(aOrder, nCell, aDistance, aSpare); + nodeZero(pRtree, pNode); + + for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){ + RtreeCell *p = &aCell[aOrder[ii]]; + nodeInsertCell(pRtree, pNode, p); + if( p->iRowid==pCell->iRowid ){ + if( iHeight==0 ){ + rc = rowidWrite(pRtree, p->iRowid, pNode->iNode); + }else{ + rc = parentWrite(pRtree, p->iRowid, pNode->iNode); + } + } + } + if( rc==SQLITE_OK ){ + rc = fixBoundingBox(pRtree, pNode); + } + for(; rc==SQLITE_OK && iiiNode currently contains + ** the height of the sub-tree headed by the cell. + */ + RtreeNode *pInsert; + RtreeCell *p = &aCell[aOrder[ii]]; + rc = ChooseLeaf(pRtree, p, iHeight, &pInsert); + if( rc==SQLITE_OK ){ + int rc2; + rc = rtreeInsertCell(pRtree, pInsert, p, iHeight); + rc2 = nodeRelease(pRtree, pInsert); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + + sqlite3_free(aCell); + return rc; +} + +/* +** Insert cell pCell into node pNode. Node pNode is the head of a +** subtree iHeight high (leaf nodes have iHeight==0). +*/ +static int rtreeInsertCell( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell, + int iHeight +){ + int rc = SQLITE_OK; + if( iHeight>0 ){ + RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid); + if( pChild ){ + nodeRelease(pRtree, pChild->pParent); + nodeReference(pNode); + pChild->pParent = pNode; + } + } + if( nodeInsertCell(pRtree, pNode, pCell) ){ + if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){ + rc = SplitNode(pRtree, pNode, pCell, iHeight); + }else{ + pRtree->iReinsertHeight = iHeight; + rc = Reinsert(pRtree, pNode, pCell, iHeight); + } + }else{ + rc = AdjustTree(pRtree, pNode, pCell); + if( rc==SQLITE_OK ){ + if( iHeight==0 ){ + rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); + }else{ + rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); + } + } + } + return rc; +} + +static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){ + int ii; + int rc = SQLITE_OK; + int nCell = NCELL(pNode); + + for(ii=0; rc==SQLITE_OK && iiiNode currently contains + ** the height of the sub-tree headed by the cell. + */ + rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert); + if( rc==SQLITE_OK ){ + int rc2; + rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode); + rc2 = nodeRelease(pRtree, pInsert); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + return rc; +} + +/* +** Select a currently unused rowid for a new r-tree record. +*/ +static int newRowid(Rtree *pRtree, i64 *piRowid){ + int rc; + sqlite3_bind_null(pRtree->pWriteRowid, 1); + sqlite3_bind_null(pRtree->pWriteRowid, 2); + sqlite3_step(pRtree->pWriteRowid); + rc = sqlite3_reset(pRtree->pWriteRowid); + *piRowid = sqlite3_last_insert_rowid(pRtree->db); + return rc; +} + +/* +** Remove the entry with rowid=iDelete from the r-tree structure. +*/ +static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){ + int rc; /* Return code */ + RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */ + int iCell; /* Index of iDelete cell in pLeaf */ + RtreeNode *pRoot; /* Root node of rtree structure */ + + + /* Obtain a reference to the root node to initialize Rtree.iDepth */ + rc = nodeAcquire(pRtree, 1, 0, &pRoot); + + /* Obtain a reference to the leaf node that contains the entry + ** about to be deleted. + */ + if( rc==SQLITE_OK ){ + rc = findLeafNode(pRtree, iDelete, &pLeaf, 0); + } + + /* Delete the cell in question from the leaf node. */ + if( rc==SQLITE_OK ){ + int rc2; + rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell); + if( rc==SQLITE_OK ){ + rc = deleteCell(pRtree, pLeaf, iCell, 0); + } + rc2 = nodeRelease(pRtree, pLeaf); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + + /* Delete the corresponding entry in the _rowid table. */ + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete); + sqlite3_step(pRtree->pDeleteRowid); + rc = sqlite3_reset(pRtree->pDeleteRowid); + } + + /* Check if the root node now has exactly one child. If so, remove + ** it, schedule the contents of the child for reinsertion and + ** reduce the tree height by one. + ** + ** This is equivalent to copying the contents of the child into + ** the root node (the operation that Gutman's paper says to perform + ** in this scenario). + */ + if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){ + int rc2; + RtreeNode *pChild; + i64 iChild = nodeGetRowid(pRtree, pRoot, 0); + rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); + if( rc==SQLITE_OK ){ + rc = removeNode(pRtree, pChild, pRtree->iDepth-1); + } + rc2 = nodeRelease(pRtree, pChild); + if( rc==SQLITE_OK ) rc = rc2; + if( rc==SQLITE_OK ){ + pRtree->iDepth--; + writeInt16(pRoot->zData, pRtree->iDepth); + pRoot->isDirty = 1; + } + } + + /* Re-insert the contents of any underfull nodes removed from the tree. */ + for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ + if( rc==SQLITE_OK ){ + rc = reinsertNodeContent(pRtree, pLeaf); + } + pRtree->pDeleted = pLeaf->pNext; + sqlite3_free(pLeaf); + } + + /* Release the reference to the root node. */ + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pRoot); + }else{ + nodeRelease(pRtree, pRoot); + } + + return rc; +} + +/* +** Rounding constants for float->double conversion. +*/ +#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */ +#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */ + +#if !defined(SQLITE_RTREE_INT_ONLY) +/* +** Convert an sqlite3_value into an RtreeValue (presumably a float) +** while taking care to round toward negative or positive, respectively. +*/ +static RtreeValue rtreeValueDown(sqlite3_value *v){ + double d = sqlite3_value_double(v); + float f = (float)d; + if( f>d ){ + f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS)); + } + return f; +} +static RtreeValue rtreeValueUp(sqlite3_value *v){ + double d = sqlite3_value_double(v); + float f = (float)d; + if( f1 */ + int bHaveRowid = 0; /* Set to 1 after new rowid is determined */ + + rtreeReference(pRtree); + assert(nData>=1); + + cell.iRowid = 0; /* Used only to suppress a compiler warning */ + + /* Constraint handling. A write operation on an r-tree table may return + ** SQLITE_CONSTRAINT for two reasons: + ** + ** 1. A duplicate rowid value, or + ** 2. The supplied data violates the "x2>=x1" constraint. + ** + ** In the first case, if the conflict-handling mode is REPLACE, then + ** the conflicting row can be removed before proceeding. In the second + ** case, SQLITE_CONSTRAINT must be returned regardless of the + ** conflict-handling mode specified by the user. + */ + if( nData>1 ){ + int ii; + + /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. + ** + ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared + ** with "column" that are interpreted as table constraints. + ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); + ** This problem was discovered after years of use, so we silently ignore + ** these kinds of misdeclared tables to avoid breaking any legacy. + */ + assert( nData<=(pRtree->nDim*2 + 3) ); + +#ifndef SQLITE_RTREE_INT_ONLY + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + for(ii=0; iicell.aCoord[ii+1].f ){ + rc = SQLITE_CONSTRAINT; + goto constraint; + } + } + }else +#endif + { + for(ii=0; iicell.aCoord[ii+1].i ){ + rc = SQLITE_CONSTRAINT; + goto constraint; + } + } + } + + /* If a rowid value was supplied, check if it is already present in + ** the table. If so, the constraint has failed. */ + if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){ + cell.iRowid = sqlite3_value_int64(azData[2]); + if( sqlite3_value_type(azData[0])==SQLITE_NULL + || sqlite3_value_int64(azData[0])!=cell.iRowid + ){ + int steprc; + sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid); + steprc = sqlite3_step(pRtree->pReadRowid); + rc = sqlite3_reset(pRtree->pReadRowid); + if( SQLITE_ROW==steprc ){ + if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){ + rc = rtreeDeleteRowid(pRtree, cell.iRowid); + }else{ + rc = SQLITE_CONSTRAINT; + goto constraint; + } + } + } + bHaveRowid = 1; + } + } + + /* If azData[0] is not an SQL NULL value, it is the rowid of a + ** record to delete from the r-tree table. The following block does + ** just that. + */ + if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){ + rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0])); + } + + /* If the azData[] array contains more than one element, elements + ** (azData[2]..azData[argc-1]) contain a new record to insert into + ** the r-tree structure. + */ + if( rc==SQLITE_OK && nData>1 ){ + /* Insert the new record into the r-tree */ + RtreeNode *pLeaf = 0; + + /* Figure out the rowid of the new row. */ + if( bHaveRowid==0 ){ + rc = newRowid(pRtree, &cell.iRowid); + } + *pRowid = cell.iRowid; + + if( rc==SQLITE_OK ){ + rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf); + } + if( rc==SQLITE_OK ){ + int rc2; + pRtree->iReinsertHeight = -1; + rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0); + rc2 = nodeRelease(pRtree, pLeaf); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + +constraint: + rtreeRelease(pRtree); + return rc; +} + +/* +** The xRename method for rtree module virtual tables. +*/ +static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){ + Rtree *pRtree = (Rtree *)pVtab; + int rc = SQLITE_NOMEM; + char *zSql = sqlite3_mprintf( + "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";" + "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";" + "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";" + , pRtree->zDb, pRtree->zName, zNewName + , pRtree->zDb, pRtree->zName, zNewName + , pRtree->zDb, pRtree->zName, zNewName + ); + if( zSql ){ + rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0); + sqlite3_free(zSql); + } + return rc; +} + +/* +** This function populates the pRtree->nRowEst variable with an estimate +** of the number of rows in the virtual table. If possible, this is based +** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST. +*/ +static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){ + const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'"; + char *zSql; + sqlite3_stmt *p; + int rc; + i64 nRow = 0; + + zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0); + if( rc==SQLITE_OK ){ + if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0); + rc = sqlite3_finalize(p); + }else if( rc!=SQLITE_NOMEM ){ + rc = SQLITE_OK; + } + + if( rc==SQLITE_OK ){ + if( nRow==0 ){ + pRtree->nRowEst = RTREE_DEFAULT_ROWEST; + }else{ + pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST); + } + } + sqlite3_free(zSql); + } + + return rc; +} + +static sqlite3_module rtreeModule = { + 0, /* iVersion */ + rtreeCreate, /* xCreate - create a table */ + rtreeConnect, /* xConnect - connect to an existing table */ + rtreeBestIndex, /* xBestIndex - Determine search strategy */ + rtreeDisconnect, /* xDisconnect - Disconnect from a table */ + rtreeDestroy, /* xDestroy - Drop a table */ + rtreeOpen, /* xOpen - open a cursor */ + rtreeClose, /* xClose - close a cursor */ + rtreeFilter, /* xFilter - configure scan constraints */ + rtreeNext, /* xNext - advance a cursor */ + rtreeEof, /* xEof */ + rtreeColumn, /* xColumn - read data */ + rtreeRowid, /* xRowid - read data */ + rtreeUpdate, /* xUpdate - write data */ + 0, /* xBegin - begin transaction */ + 0, /* xSync - sync transaction */ + 0, /* xCommit - commit transaction */ + 0, /* xRollback - rollback transaction */ + 0, /* xFindFunction - function overloading */ + rtreeRename, /* xRename - rename the table */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0 /* xRollbackTo */ +}; + +static int rtreeSqlInit( + Rtree *pRtree, + sqlite3 *db, + const char *zDb, + const char *zPrefix, + int isCreate +){ + int rc = SQLITE_OK; + + #define N_STATEMENT 9 + static const char *azSql[N_STATEMENT] = { + /* Read and write the xxx_node table */ + "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1", + "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1", + + /* Read and write the xxx_rowid table */ + "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1", + "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1", + + /* Read and write the xxx_parent table */ + "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1", + "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1" + }; + sqlite3_stmt **appStmt[N_STATEMENT]; + int i; + + pRtree->db = db; + + if( isCreate ){ + char *zCreate = sqlite3_mprintf( +"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);" +"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);" +"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY," + " parentnode INTEGER);" +"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))", + zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize + ); + if( !zCreate ){ + return SQLITE_NOMEM; + } + rc = sqlite3_exec(db, zCreate, 0, 0, 0); + sqlite3_free(zCreate); + if( rc!=SQLITE_OK ){ + return rc; + } + } + + appStmt[0] = &pRtree->pReadNode; + appStmt[1] = &pRtree->pWriteNode; + appStmt[2] = &pRtree->pDeleteNode; + appStmt[3] = &pRtree->pReadRowid; + appStmt[4] = &pRtree->pWriteRowid; + appStmt[5] = &pRtree->pDeleteRowid; + appStmt[6] = &pRtree->pReadParent; + appStmt[7] = &pRtree->pWriteParent; + appStmt[8] = &pRtree->pDeleteParent; + + rc = rtreeQueryStat1(db, pRtree); + for(i=0; iiNodeSize is populated and SQLITE_OK returned. +** Otherwise, an SQLite error code is returned. +** +** If this function is being called as part of an xConnect(), then the rtree +** table already exists. In this case the node-size is determined by inspecting +** the root node of the tree. +** +** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. +** This ensures that each node is stored on a single database page. If the +** database page-size is so large that more than RTREE_MAXCELLS entries +** would fit in a single node, use a smaller node-size. +*/ +static int getNodeSize( + sqlite3 *db, /* Database handle */ + Rtree *pRtree, /* Rtree handle */ + int isCreate, /* True for xCreate, false for xConnect */ + char **pzErr /* OUT: Error message, if any */ +){ + int rc; + char *zSql; + if( isCreate ){ + int iPageSize = 0; + zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb); + rc = getIntFromStmt(db, zSql, &iPageSize); + if( rc==SQLITE_OK ){ + pRtree->iNodeSize = iPageSize-64; + if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)iNodeSize ){ + pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS; + } + }else{ + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + }else{ + zSql = sqlite3_mprintf( + "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1", + pRtree->zDb, pRtree->zName + ); + rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize); + if( rc!=SQLITE_OK ){ + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + } + + sqlite3_free(zSql); + return rc; +} + +/* +** This function is the implementation of both the xConnect and xCreate +** methods of the r-tree virtual table. +** +** argv[0] -> module name +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> column names... +*/ +static int rtreeInit( + sqlite3 *db, /* Database connection */ + void *pAux, /* One of the RTREE_COORD_* constants */ + int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */ + sqlite3_vtab **ppVtab, /* OUT: New virtual table */ + char **pzErr, /* OUT: Error message, if any */ + int isCreate /* True for xCreate, false for xConnect */ +){ + int rc = SQLITE_OK; + Rtree *pRtree; + int nDb; /* Length of string argv[1] */ + int nName; /* Length of string argv[2] */ + int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); + + const char *aErrMsg[] = { + 0, /* 0 */ + "Wrong number of columns for an rtree table", /* 1 */ + "Too few columns for an rtree table", /* 2 */ + "Too many columns for an rtree table" /* 3 */ + }; + + int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2; + if( aErrMsg[iErr] ){ + *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]); + return SQLITE_ERROR; + } + + sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); + + /* Allocate the sqlite3_vtab structure */ + nDb = (int)strlen(argv[1]); + nName = (int)strlen(argv[2]); + pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2); + if( !pRtree ){ + return SQLITE_NOMEM; + } + memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2); + pRtree->nBusy = 1; + pRtree->base.pModule = &rtreeModule; + pRtree->zDb = (char *)&pRtree[1]; + pRtree->zName = &pRtree->zDb[nDb+1]; + pRtree->nDim = (argc-4)/2; + pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2; + pRtree->eCoordType = eCoordType; + memcpy(pRtree->zDb, argv[1], nDb); + memcpy(pRtree->zName, argv[2], nName); + + /* Figure out the node size to use. */ + rc = getNodeSize(db, pRtree, isCreate, pzErr); + + /* Create/Connect to the underlying relational database schema. If + ** that is successful, call sqlite3_declare_vtab() to configure + ** the r-tree table schema. + */ + if( rc==SQLITE_OK ){ + if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){ + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + }else{ + char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]); + char *zTmp; + int ii; + for(ii=4; zSql && iinBusy==1 ); + rtreeRelease(pRtree); + } + return rc; +} + + +/* +** Implementation of a scalar function that decodes r-tree nodes to +** human readable strings. This can be used for debugging and analysis. +** +** The scalar function takes two arguments: (1) the number of dimensions +** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing +** an r-tree node. For a two-dimensional r-tree structure called "rt", to +** deserialize all nodes, a statement like: +** +** SELECT rtreenode(2, data) FROM rt_node; +** +** The human readable string takes the form of a Tcl list with one +** entry for each cell in the r-tree node. Each entry is itself a +** list, containing the 8-byte rowid/pageno followed by the +** *2 coordinates. +*/ +static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){ + char *zText = 0; + RtreeNode node; + Rtree tree; + int ii; + + UNUSED_PARAMETER(nArg); + memset(&node, 0, sizeof(RtreeNode)); + memset(&tree, 0, sizeof(Rtree)); + tree.nDim = sqlite3_value_int(apArg[0]); + tree.nBytesPerCell = 8 + 8 * tree.nDim; + node.zData = (u8 *)sqlite3_value_blob(apArg[1]); + + for(ii=0; iixDestructor ) pInfo->xDestructor(pInfo->pContext); + sqlite3_free(p); +} + +/* +** This routine frees the BLOB that is returned by geomCallback(). +*/ +static void rtreeMatchArgFree(void *pArg){ + int i; + RtreeMatchArg *p = (RtreeMatchArg*)pArg; + for(i=0; inParam; i++){ + sqlite3_value_free(p->apSqlParam[i]); + } + sqlite3_free(p); +} + +/* +** Each call to sqlite3_rtree_geometry_callback() or +** sqlite3_rtree_query_callback() creates an ordinary SQLite +** scalar function that is implemented by this routine. +** +** All this function does is construct an RtreeMatchArg object that +** contains the geometry-checking callback routines and a list of +** parameters to this function, then return that RtreeMatchArg object +** as a BLOB. +** +** The R-Tree MATCH operator will read the returned BLOB, deserialize +** the RtreeMatchArg object, and use the RtreeMatchArg object to figure +** out which elements of the R-Tree should be returned by the query. +*/ +static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ + RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx); + RtreeMatchArg *pBlob; + int nBlob; + int memErr = 0; + + nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue) + + nArg*sizeof(sqlite3_value*); + pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob); + if( !pBlob ){ + sqlite3_result_error_nomem(ctx); + }else{ + int i; + pBlob->magic = RTREE_GEOMETRY_MAGIC; + pBlob->cb = pGeomCtx[0]; + pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg]; + pBlob->nParam = nArg; + for(i=0; iapSqlParam[i] = sqlite3_value_dup(aArg[i]); + if( pBlob->apSqlParam[i]==0 ) memErr = 1; +#ifdef SQLITE_RTREE_INT_ONLY + pBlob->aParam[i] = sqlite3_value_int64(aArg[i]); +#else + pBlob->aParam[i] = sqlite3_value_double(aArg[i]); +#endif + } + if( memErr ){ + sqlite3_result_error_nomem(ctx); + rtreeMatchArgFree(pBlob); + }else{ + sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree); + } + } +} + +/* +** Register a new geometry function for use with the r-tree MATCH operator. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback( + sqlite3 *db, /* Register SQL function on this connection */ + const char *zGeom, /* Name of the new SQL function */ + int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */ + void *pContext /* Extra data associated with the callback */ +){ + RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ + + /* Allocate and populate the context object. */ + pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); + if( !pGeomCtx ) return SQLITE_NOMEM; + pGeomCtx->xGeom = xGeom; + pGeomCtx->xQueryFunc = 0; + pGeomCtx->xDestructor = 0; + pGeomCtx->pContext = pContext; + return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, + (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback + ); +} + +/* +** Register a new 2nd-generation geometry function for use with the +** r-tree MATCH operator. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback( + sqlite3 *db, /* Register SQL function on this connection */ + const char *zQueryFunc, /* Name of new SQL function */ + int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */ + void *pContext, /* Extra data passed into the callback */ + void (*xDestructor)(void*) /* Destructor for the extra data */ +){ + RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ + + /* Allocate and populate the context object. */ + pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); + if( !pGeomCtx ) return SQLITE_NOMEM; + pGeomCtx->xGeom = 0; + pGeomCtx->xQueryFunc = xQueryFunc; + pGeomCtx->xDestructor = xDestructor; + pGeomCtx->pContext = pContext; + return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY, + (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback + ); +} + +#if !SQLITE_CORE +#ifdef _WIN32 +__declspec(dllexport) +#endif +SQLITE_API int SQLITE_STDCALL sqlite3_rtree_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3RtreeInit(db); +} +#endif + +#endif + +/************** End of rtree.c ***********************************************/ +/************** Begin file icu.c *********************************************/ +/* +** 2007 May 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $ +** +** This file implements an integration between the ICU library +** ("International Components for Unicode", an open-source library +** for handling unicode data) and SQLite. The integration uses +** ICU to provide the following to SQLite: +** +** * An implementation of the SQL regexp() function (and hence REGEXP +** operator) using the ICU uregex_XX() APIs. +** +** * Implementations of the SQL scalar upper() and lower() functions +** for case mapping. +** +** * Integration of ICU and SQLite collation sequences. +** +** * An implementation of the LIKE operator that uses ICU to +** provide case-independent matching. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) + +/* Include ICU headers */ +#include +#include +#include +#include + +/* #include */ + +#ifndef SQLITE_CORE +/* #include "sqlite3ext.h" */ + SQLITE_EXTENSION_INIT1 +#else +/* #include "sqlite3.h" */ +#endif + +/* +** Maximum length (in bytes) of the pattern in a LIKE or GLOB +** operator. +*/ +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000 +#endif + +/* +** Version of sqlite3_free() that is always a function, never a macro. +*/ +static void xFree(void *p){ + sqlite3_free(p); +} + +/* +** This lookup table is used to help decode the first byte of +** a multi-byte UTF8 character. It is copied here from SQLite source +** code file utf8.c. +*/ +static const unsigned char icuUtf8Trans1[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, +}; + +#define SQLITE_ICU_READ_UTF8(zIn, c) \ + c = *(zIn++); \ + if( c>=0xc0 ){ \ + c = icuUtf8Trans1[c-0xc0]; \ + while( (*zIn & 0xc0)==0x80 ){ \ + c = (c<<6) + (0x3f & *(zIn++)); \ + } \ + } + +#define SQLITE_ICU_SKIP_UTF8(zIn) \ + assert( *zIn ); \ + if( *(zIn++)>=0xc0 ){ \ + while( (*zIn & 0xc0)==0x80 ){zIn++;} \ + } + + +/* +** Compare two UTF-8 strings for equality where the first string is +** a "LIKE" expression. Return true (1) if they are the same and +** false (0) if they are different. +*/ +static int icuLikeCompare( + const uint8_t *zPattern, /* LIKE pattern */ + const uint8_t *zString, /* The UTF-8 string to compare against */ + const UChar32 uEsc /* The escape character */ +){ + static const int MATCH_ONE = (UChar32)'_'; + static const int MATCH_ALL = (UChar32)'%'; + + int prevEscape = 0; /* True if the previous character was uEsc */ + + while( 1 ){ + + /* Read (and consume) the next character from the input pattern. */ + UChar32 uPattern; + SQLITE_ICU_READ_UTF8(zPattern, uPattern); + if( uPattern==0 ) break; + + /* There are now 4 possibilities: + ** + ** 1. uPattern is an unescaped match-all character "%", + ** 2. uPattern is an unescaped match-one character "_", + ** 3. uPattern is an unescaped escape character, or + ** 4. uPattern is to be handled as an ordinary character + */ + if( !prevEscape && uPattern==MATCH_ALL ){ + /* Case 1. */ + uint8_t c; + + /* Skip any MATCH_ALL or MATCH_ONE characters that follow a + ** MATCH_ALL. For each MATCH_ONE, skip one character in the + ** test string. + */ + while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){ + if( c==MATCH_ONE ){ + if( *zString==0 ) return 0; + SQLITE_ICU_SKIP_UTF8(zString); + } + zPattern++; + } + + if( *zPattern==0 ) return 1; + + while( *zString ){ + if( icuLikeCompare(zPattern, zString, uEsc) ){ + return 1; + } + SQLITE_ICU_SKIP_UTF8(zString); + } + return 0; + + }else if( !prevEscape && uPattern==MATCH_ONE ){ + /* Case 2. */ + if( *zString==0 ) return 0; + SQLITE_ICU_SKIP_UTF8(zString); + + }else if( !prevEscape && uPattern==uEsc){ + /* Case 3. */ + prevEscape = 1; + + }else{ + /* Case 4. */ + UChar32 uString; + SQLITE_ICU_READ_UTF8(zString, uString); + uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT); + uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT); + if( uString!=uPattern ){ + return 0; + } + prevEscape = 0; + } + } + + return *zString==0; +} + +/* +** Implementation of the like() SQL function. This function implements +** the build-in LIKE operator. The first argument to the function is the +** pattern and the second argument is the string. So, the SQL statements: +** +** A LIKE B +** +** is implemented as like(B, A). If there is an escape character E, +** +** A LIKE B ESCAPE E +** +** is mapped to like(B, A, E). +*/ +static void icuLikeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zA = sqlite3_value_text(argv[0]); + const unsigned char *zB = sqlite3_value_text(argv[1]); + UChar32 uEsc = 0; + + /* Limit the length of the LIKE or GLOB pattern to avoid problems + ** of deep recursion and N*N behavior in patternCompare(). + */ + if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){ + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); + return; + } + + + if( argc==3 ){ + /* The escape character string must consist of a single UTF-8 character. + ** Otherwise, return an error. + */ + int nE= sqlite3_value_bytes(argv[2]); + const unsigned char *zE = sqlite3_value_text(argv[2]); + int i = 0; + if( zE==0 ) return; + U8_NEXT(zE, i, nE, uEsc); + if( i!=nE){ + sqlite3_result_error(context, + "ESCAPE expression must be a single character", -1); + return; + } + } + + if( zA && zB ){ + sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc)); + } +} + +/* +** This function is called when an ICU function called from within +** the implementation of an SQL scalar function returns an error. +** +** The scalar function context passed as the first argument is +** loaded with an error message based on the following two args. +*/ +static void icuFunctionError( + sqlite3_context *pCtx, /* SQLite scalar function context */ + const char *zName, /* Name of ICU function that failed */ + UErrorCode e /* Error code returned by ICU function */ +){ + char zBuf[128]; + sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e)); + zBuf[127] = '\0'; + sqlite3_result_error(pCtx, zBuf, -1); +} + +/* +** Function to delete compiled regexp objects. Registered as +** a destructor function with sqlite3_set_auxdata(). +*/ +static void icuRegexpDelete(void *p){ + URegularExpression *pExpr = (URegularExpression *)p; + uregex_close(pExpr); +} + +/* +** Implementation of SQLite REGEXP operator. This scalar function takes +** two arguments. The first is a regular expression pattern to compile +** the second is a string to match against that pattern. If either +** argument is an SQL NULL, then NULL Is returned. Otherwise, the result +** is 1 if the string matches the pattern, or 0 otherwise. +** +** SQLite maps the regexp() function to the regexp() operator such +** that the following two are equivalent: +** +** zString REGEXP zPattern +** regexp(zPattern, zString) +** +** Uses the following ICU regexp APIs: +** +** uregex_open() +** uregex_matches() +** uregex_close() +*/ +static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){ + UErrorCode status = U_ZERO_ERROR; + URegularExpression *pExpr; + UBool res; + const UChar *zString = sqlite3_value_text16(apArg[1]); + + (void)nArg; /* Unused parameter */ + + /* If the left hand side of the regexp operator is NULL, + ** then the result is also NULL. + */ + if( !zString ){ + return; + } + + pExpr = sqlite3_get_auxdata(p, 0); + if( !pExpr ){ + const UChar *zPattern = sqlite3_value_text16(apArg[0]); + if( !zPattern ){ + return; + } + pExpr = uregex_open(zPattern, -1, 0, 0, &status); + + if( U_SUCCESS(status) ){ + sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete); + }else{ + assert(!pExpr); + icuFunctionError(p, "uregex_open", status); + return; + } + } + + /* Configure the text that the regular expression operates on. */ + uregex_setText(pExpr, zString, -1, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "uregex_setText", status); + return; + } + + /* Attempt the match */ + res = uregex_matches(pExpr, 0, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "uregex_matches", status); + return; + } + + /* Set the text that the regular expression operates on to a NULL + ** pointer. This is not really necessary, but it is tidier than + ** leaving the regular expression object configured with an invalid + ** pointer after this function returns. + */ + uregex_setText(pExpr, 0, 0, &status); + + /* Return 1 or 0. */ + sqlite3_result_int(p, res ? 1 : 0); +} + +/* +** Implementations of scalar functions for case mapping - upper() and +** lower(). Function upper() converts its input to upper-case (ABC). +** Function lower() converts to lower-case (abc). +** +** ICU provides two types of case mapping, "general" case mapping and +** "language specific". Refer to ICU documentation for the differences +** between the two. +** +** To utilise "general" case mapping, the upper() or lower() scalar +** functions are invoked with one argument: +** +** upper('ABC') -> 'abc' +** lower('abc') -> 'ABC' +** +** To access ICU "language specific" case mapping, upper() or lower() +** should be invoked with two arguments. The second argument is the name +** of the locale to use. Passing an empty string ("") or SQL NULL value +** as the second argument is the same as invoking the 1 argument version +** of upper() or lower(). +** +** lower('I', 'en_us') -> 'i' +** lower('I', 'tr_tr') -> 'ı' (small dotless i) +** +** http://www.icu-project.org/userguide/posix.html#case_mappings +*/ +static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ + const UChar *zInput; /* Pointer to input string */ + UChar *zOutput = 0; /* Pointer to output buffer */ + int nInput; /* Size of utf-16 input string in bytes */ + int nOut; /* Size of output buffer in bytes */ + int cnt; + int bToUpper; /* True for toupper(), false for tolower() */ + UErrorCode status; + const char *zLocale = 0; + + assert(nArg==1 || nArg==2); + bToUpper = (sqlite3_user_data(p)!=0); + if( nArg==2 ){ + zLocale = (const char *)sqlite3_value_text(apArg[1]); + } + + zInput = sqlite3_value_text16(apArg[0]); + if( !zInput ){ + return; + } + nOut = nInput = sqlite3_value_bytes16(apArg[0]); + if( nOut==0 ){ + sqlite3_result_text16(p, "", 0, SQLITE_STATIC); + return; + } + + for(cnt=0; cnt<2; cnt++){ + UChar *zNew = sqlite3_realloc(zOutput, nOut); + if( zNew==0 ){ + sqlite3_free(zOutput); + sqlite3_result_error_nomem(p); + return; + } + zOutput = zNew; + status = U_ZERO_ERROR; + if( bToUpper ){ + nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); + }else{ + nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); + } + + if( U_SUCCESS(status) ){ + sqlite3_result_text16(p, zOutput, nOut, xFree); + }else if( status==U_BUFFER_OVERFLOW_ERROR ){ + assert( cnt==0 ); + continue; + }else{ + icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status); + } + return; + } + assert( 0 ); /* Unreachable */ +} + +/* +** Collation sequence destructor function. The pCtx argument points to +** a UCollator structure previously allocated using ucol_open(). +*/ +static void icuCollationDel(void *pCtx){ + UCollator *p = (UCollator *)pCtx; + ucol_close(p); +} + +/* +** Collation sequence comparison function. The pCtx argument points to +** a UCollator structure previously allocated using ucol_open(). +*/ +static int icuCollationColl( + void *pCtx, + int nLeft, + const void *zLeft, + int nRight, + const void *zRight +){ + UCollationResult res; + UCollator *p = (UCollator *)pCtx; + res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2); + switch( res ){ + case UCOL_LESS: return -1; + case UCOL_GREATER: return +1; + case UCOL_EQUAL: return 0; + } + assert(!"Unexpected return value from ucol_strcoll()"); + return 0; +} + +/* +** Implementation of the scalar function icu_load_collation(). +** +** This scalar function is used to add ICU collation based collation +** types to an SQLite database connection. It is intended to be called +** as follows: +** +** SELECT icu_load_collation(, ); +** +** Where is a string containing an ICU locale identifier (i.e. +** "en_AU", "tr_TR" etc.) and is the name of the +** collation sequence to create. +*/ +static void icuLoadCollation( + sqlite3_context *p, + int nArg, + sqlite3_value **apArg +){ + sqlite3 *db = (sqlite3 *)sqlite3_user_data(p); + UErrorCode status = U_ZERO_ERROR; + const char *zLocale; /* Locale identifier - (eg. "jp_JP") */ + const char *zName; /* SQL Collation sequence name (eg. "japanese") */ + UCollator *pUCollator; /* ICU library collation object */ + int rc; /* Return code from sqlite3_create_collation_x() */ + + assert(nArg==2); + (void)nArg; /* Unused parameter */ + zLocale = (const char *)sqlite3_value_text(apArg[0]); + zName = (const char *)sqlite3_value_text(apArg[1]); + + if( !zLocale || !zName ){ + return; + } + + pUCollator = ucol_open(zLocale, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "ucol_open", status); + return; + } + assert(p); + + rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, + icuCollationColl, icuCollationDel + ); + if( rc!=SQLITE_OK ){ + ucol_close(pUCollator); + sqlite3_result_error(p, "Error registering collation function", -1); + } +} + +/* +** Register the ICU extension functions with database db. +*/ +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){ + struct IcuScalar { + const char *zName; /* Function name */ + int nArg; /* Number of arguments */ + int enc; /* Optimal text encoding */ + void *pContext; /* sqlite3_user_data() context */ + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } scalars[] = { + {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc}, + + {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16}, + {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16}, + {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16}, + {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16}, + + {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16}, + {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16}, + {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16}, + {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16}, + + {"like", 2, SQLITE_UTF8, 0, icuLikeFunc}, + {"like", 3, SQLITE_UTF8, 0, icuLikeFunc}, + + {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation}, + }; + + int rc = SQLITE_OK; + int i; + + for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){ + struct IcuScalar *p = &scalars[i]; + rc = sqlite3_create_function( + db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0 + ); + } + + return rc; +} + +#if !SQLITE_CORE +#ifdef _WIN32 +__declspec(dllexport) +#endif +SQLITE_API int SQLITE_STDCALL sqlite3_icu_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3IcuInit(db); +} +#endif + +#endif + +/************** End of icu.c *************************************************/ +/************** Begin file fts3_icu.c ****************************************/ +/* +** 2007 June 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements a tokenizer for fts3 based on the ICU library. +*/ +/* #include "fts3Int.h" */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) +#ifdef SQLITE_ENABLE_ICU + +/* #include */ +/* #include */ +/* #include "fts3_tokenizer.h" */ + +#include +/* #include */ +/* #include */ +#include + +typedef struct IcuTokenizer IcuTokenizer; +typedef struct IcuCursor IcuCursor; + +struct IcuTokenizer { + sqlite3_tokenizer base; + char *zLocale; +}; + +struct IcuCursor { + sqlite3_tokenizer_cursor base; + + UBreakIterator *pIter; /* ICU break-iterator object */ + int nChar; /* Number of UChar elements in pInput */ + UChar *aChar; /* Copy of input using utf-16 encoding */ + int *aOffset; /* Offsets of each character in utf-8 input */ + + int nBuffer; + char *zBuffer; + + int iToken; +}; + +/* +** Create a new tokenizer instance. +*/ +static int icuCreate( + int argc, /* Number of entries in argv[] */ + const char * const *argv, /* Tokenizer creation arguments */ + sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ +){ + IcuTokenizer *p; + int n = 0; + + if( argc>0 ){ + n = strlen(argv[0])+1; + } + p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n); + if( !p ){ + return SQLITE_NOMEM; + } + memset(p, 0, sizeof(IcuTokenizer)); + + if( n ){ + p->zLocale = (char *)&p[1]; + memcpy(p->zLocale, argv[0], n); + } + + *ppTokenizer = (sqlite3_tokenizer *)p; + + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int icuDestroy(sqlite3_tokenizer *pTokenizer){ + IcuTokenizer *p = (IcuTokenizer *)pTokenizer; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is pInput[0..nBytes-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int icuOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *zInput, /* Input string */ + int nInput, /* Length of zInput in bytes */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + IcuTokenizer *p = (IcuTokenizer *)pTokenizer; + IcuCursor *pCsr; + + const int32_t opt = U_FOLD_CASE_DEFAULT; + UErrorCode status = U_ZERO_ERROR; + int nChar; + + UChar32 c; + int iInput = 0; + int iOut = 0; + + *ppCursor = 0; + + if( zInput==0 ){ + nInput = 0; + zInput = ""; + }else if( nInput<0 ){ + nInput = strlen(zInput); + } + nChar = nInput+1; + pCsr = (IcuCursor *)sqlite3_malloc( + sizeof(IcuCursor) + /* IcuCursor */ + ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */ + (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */ + ); + if( !pCsr ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(IcuCursor)); + pCsr->aChar = (UChar *)&pCsr[1]; + pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3]; + + pCsr->aOffset[iOut] = iInput; + U8_NEXT(zInput, iInput, nInput, c); + while( c>0 ){ + int isError = 0; + c = u_foldCase(c, opt); + U16_APPEND(pCsr->aChar, iOut, nChar, c, isError); + if( isError ){ + sqlite3_free(pCsr); + return SQLITE_ERROR; + } + pCsr->aOffset[iOut] = iInput; + + if( iInputpIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status); + if( !U_SUCCESS(status) ){ + sqlite3_free(pCsr); + return SQLITE_ERROR; + } + pCsr->nChar = iOut; + + ubrk_first(pCsr->pIter); + *ppCursor = (sqlite3_tokenizer_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to icuOpen(). +*/ +static int icuClose(sqlite3_tokenizer_cursor *pCursor){ + IcuCursor *pCsr = (IcuCursor *)pCursor; + ubrk_close(pCsr->pIter); + sqlite3_free(pCsr->zBuffer); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Extract the next token from a tokenization cursor. +*/ +static int icuNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ + const char **ppToken, /* OUT: *ppToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + IcuCursor *pCsr = (IcuCursor *)pCursor; + + int iStart = 0; + int iEnd = 0; + int nByte = 0; + + while( iStart==iEnd ){ + UChar32 c; + + iStart = ubrk_current(pCsr->pIter); + iEnd = ubrk_next(pCsr->pIter); + if( iEnd==UBRK_DONE ){ + return SQLITE_DONE; + } + + while( iStartaChar, iWhite, pCsr->nChar, c); + if( u_isspace(c) ){ + iStart = iWhite; + }else{ + break; + } + } + assert(iStart<=iEnd); + } + + do { + UErrorCode status = U_ZERO_ERROR; + if( nByte ){ + char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte); + if( !zNew ){ + return SQLITE_NOMEM; + } + pCsr->zBuffer = zNew; + pCsr->nBuffer = nByte; + } + + u_strToUTF8( + pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */ + &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */ + &status /* Output success/failure */ + ); + } while( nByte>pCsr->nBuffer ); + + *ppToken = pCsr->zBuffer; + *pnBytes = nByte; + *piStartOffset = pCsr->aOffset[iStart]; + *piEndOffset = pCsr->aOffset[iEnd]; + *piPosition = pCsr->iToken++; + + return SQLITE_OK; +} + +/* +** The set of routines that implement the simple tokenizer +*/ +static const sqlite3_tokenizer_module icuTokenizerModule = { + 0, /* iVersion */ + icuCreate, /* xCreate */ + icuDestroy, /* xCreate */ + icuOpen, /* xOpen */ + icuClose, /* xClose */ + icuNext, /* xNext */ + 0, /* xLanguageid */ +}; + +/* +** Set *ppModule to point at the implementation of the ICU tokenizer. +*/ +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &icuTokenizerModule; +} + +#endif /* defined(SQLITE_ENABLE_ICU) */ +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_icu.c ********************************************/ +/************** Begin file sqlite3rbu.c **************************************/ +/* +** 2014 August 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** +** OVERVIEW +** +** The RBU extension requires that the RBU update be packaged as an +** SQLite database. The tables it expects to find are described in +** sqlite3rbu.h. Essentially, for each table xyz in the target database +** that the user wishes to write to, a corresponding data_xyz table is +** created in the RBU database and populated with one row for each row to +** update, insert or delete from the target table. +** +** The update proceeds in three stages: +** +** 1) The database is updated. The modified database pages are written +** to a *-oal file. A *-oal file is just like a *-wal file, except +** that it is named "-oal" instead of "-wal". +** Because regular SQLite clients do not look for file named +** "-oal", they go on using the original database in +** rollback mode while the *-oal file is being generated. +** +** During this stage RBU does not update the database by writing +** directly to the target tables. Instead it creates "imposter" +** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses +** to update each b-tree individually. All updates required by each +** b-tree are completed before moving on to the next, and all +** updates are done in sorted key order. +** +** 2) The "-oal" file is moved to the equivalent "-wal" +** location using a call to rename(2). Before doing this the RBU +** module takes an EXCLUSIVE lock on the database file, ensuring +** that there are no other active readers. +** +** Once the EXCLUSIVE lock is released, any other database readers +** detect the new *-wal file and read the database in wal mode. At +** this point they see the new version of the database - including +** the updates made as part of the RBU update. +** +** 3) The new *-wal file is checkpointed. This proceeds in the same way +** as a regular database checkpoint, except that a single frame is +** checkpointed each time sqlite3rbu_step() is called. If the RBU +** handle is closed before the entire *-wal file is checkpointed, +** the checkpoint progress is saved in the RBU database and the +** checkpoint can be resumed by another RBU client at some point in +** the future. +** +** POTENTIAL PROBLEMS +** +** The rename() call might not be portable. And RBU is not currently +** syncing the directory after renaming the file. +** +** When state is saved, any commit to the *-oal file and the commit to +** the RBU update database are not atomic. So if the power fails at the +** wrong moment they might get out of sync. As the main database will be +** committed before the RBU update database this will likely either just +** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE +** constraint violations). +** +** If some client does modify the target database mid RBU update, or some +** other error occurs, the RBU extension will keep throwing errors. It's +** not really clear how to get out of this state. The system could just +** by delete the RBU update database and *-oal file and have the device +** download the update again and start over. +** +** At present, for an UPDATE, both the new.* and old.* records are +** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all +** fields are collected. This means we're probably writing a lot more +** data to disk when saving the state of an ongoing update to the RBU +** update database than is strictly necessary. +** +*/ + +/* #include */ +/* #include */ +/* #include */ + +/* #include "sqlite3.h" */ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) +/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/ +/************** Begin file sqlite3rbu.h **************************************/ +/* +** 2014 August 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the public interface for the RBU extension. +*/ + +/* +** SUMMARY +** +** Writing a transaction containing a large number of operations on +** b-tree indexes that are collectively larger than the available cache +** memory can be very inefficient. +** +** The problem is that in order to update a b-tree, the leaf page (at least) +** containing the entry being inserted or deleted must be modified. If the +** working set of leaves is larger than the available cache memory, then a +** single leaf that is modified more than once as part of the transaction +** may be loaded from or written to the persistent media multiple times. +** Additionally, because the index updates are likely to be applied in +** random order, access to pages within the database is also likely to be in +** random order, which is itself quite inefficient. +** +** One way to improve the situation is to sort the operations on each index +** by index key before applying them to the b-tree. This leads to an IO +** pattern that resembles a single linear scan through the index b-tree, +** and all but guarantees each modified leaf page is loaded and stored +** exactly once. SQLite uses this trick to improve the performance of +** CREATE INDEX commands. This extension allows it to be used to improve +** the performance of large transactions on existing databases. +** +** Additionally, this extension allows the work involved in writing the +** large transaction to be broken down into sub-transactions performed +** sequentially by separate processes. This is useful if the system cannot +** guarantee that a single update process will run for long enough to apply +** the entire update, for example because the update is being applied on a +** mobile device that is frequently rebooted. Even after the writer process +** has committed one or more sub-transactions, other database clients continue +** to read from the original database snapshot. In other words, partially +** applied transactions are not visible to other clients. +** +** "RBU" stands for "Resumable Bulk Update". As in a large database update +** transmitted via a wireless network to a mobile device. A transaction +** applied using this extension is hence refered to as an "RBU update". +** +** +** LIMITATIONS +** +** An "RBU update" transaction is subject to the following limitations: +** +** * The transaction must consist of INSERT, UPDATE and DELETE operations +** only. +** +** * INSERT statements may not use any default values. +** +** * UPDATE and DELETE statements must identify their target rows by +** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY +** KEY fields may not be updated or deleted. If the table being written +** has no PRIMARY KEY, affected rows must be identified by rowid. +** +** * UPDATE statements may not modify PRIMARY KEY columns. +** +** * No triggers will be fired. +** +** * No foreign key violations are detected or reported. +** +** * CHECK constraints are not enforced. +** +** * No constraint handling mode except for "OR ROLLBACK" is supported. +** +** +** PREPARATION +** +** An "RBU update" is stored as a separate SQLite database. A database +** containing an RBU update is an "RBU database". For each table in the +** target database to be updated, the RBU database should contain a table +** named "data_" containing the same set of columns as the +** target table, and one more - "rbu_control". The data_% table should +** have no PRIMARY KEY or UNIQUE constraints, but each column should have +** the same type as the corresponding column in the target database. +** The "rbu_control" column should have no type at all. For example, if +** the target database contains: +** +** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE); +** +** Then the RBU database should contain: +** +** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control); +** +** The order of the columns in the data_% table does not matter. +** +** Instead of a regular table, the RBU database may also contain virtual +** tables or view named using the data_ naming scheme. +** +** Instead of the plain data_ naming scheme, RBU database tables +** may also be named data_, where is any sequence +** of zero or more numeric characters (0-9). This can be significant because +** tables within the RBU database are always processed in order sorted by +** name. By judicious selection of the the portion of the names +** of the RBU tables the user can therefore control the order in which they +** are processed. This can be useful, for example, to ensure that "external +** content" FTS4 tables are updated before their underlying content tables. +** +** If the target database table is a virtual table or a table that has no +** PRIMARY KEY declaration, the data_% table must also contain a column +** named "rbu_rowid". This column is mapped to the tables implicit primary +** key column - "rowid". Virtual tables for which the "rowid" column does +** not function like a primary key value cannot be updated using RBU. For +** example, if the target db contains either of the following: +** +** CREATE VIRTUAL TABLE x1 USING fts3(a, b); +** CREATE TABLE x1(a, b) +** +** then the RBU database should contain: +** +** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control); +** +** All non-hidden columns (i.e. all columns matched by "SELECT *") of the +** target table must be present in the input table. For virtual tables, +** hidden columns are optional - they are updated by RBU if present in +** the input table, or not otherwise. For example, to write to an fts4 +** table with a hidden languageid column such as: +** +** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid'); +** +** Either of the following input table schemas may be used: +** +** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control); +** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control); +** +** For each row to INSERT into the target database as part of the RBU +** update, the corresponding data_% table should contain a single record +** with the "rbu_control" column set to contain integer value 0. The +** other columns should be set to the values that make up the new record +** to insert. +** +** If the target database table has an INTEGER PRIMARY KEY, it is not +** possible to insert a NULL value into the IPK column. Attempting to +** do so results in an SQLITE_MISMATCH error. +** +** For each row to DELETE from the target database as part of the RBU +** update, the corresponding data_% table should contain a single record +** with the "rbu_control" column set to contain integer value 1. The +** real primary key values of the row to delete should be stored in the +** corresponding columns of the data_% table. The values stored in the +** other columns are not used. +** +** For each row to UPDATE from the target database as part of the RBU +** update, the corresponding data_% table should contain a single record +** with the "rbu_control" column set to contain a value of type text. +** The real primary key values identifying the row to update should be +** stored in the corresponding columns of the data_% table row, as should +** the new values of all columns being update. The text value in the +** "rbu_control" column must contain the same number of characters as +** there are columns in the target database table, and must consist entirely +** of 'x' and '.' characters (or in some special cases 'd' - see below). For +** each column that is being updated, the corresponding character is set to +** 'x'. For those that remain as they are, the corresponding character of the +** rbu_control value should be set to '.'. For example, given the tables +** above, the update statement: +** +** UPDATE t1 SET c = 'usa' WHERE a = 4; +** +** is represented by the data_t1 row created by: +** +** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x'); +** +** Instead of an 'x' character, characters of the rbu_control value specified +** for UPDATEs may also be set to 'd'. In this case, instead of updating the +** target table with the value stored in the corresponding data_% column, the +** user-defined SQL function "rbu_delta()" is invoked and the result stored in +** the target table column. rbu_delta() is invoked with two arguments - the +** original value currently stored in the target table column and the +** value specified in the data_xxx table. +** +** For example, this row: +** +** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d'); +** +** is similar to an UPDATE statement such as: +** +** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4; +** +** Finally, if an 'f' character appears in place of a 'd' or 's' in an +** ota_control string, the contents of the data_xxx table column is assumed +** to be a "fossil delta" - a patch to be applied to a blob value in the +** format used by the fossil source-code management system. In this case +** the existing value within the target database table must be of type BLOB. +** It is replaced by the result of applying the specified fossil delta to +** itself. +** +** If the target database table is a virtual table or a table with no PRIMARY +** KEY, the rbu_control value should not include a character corresponding +** to the rbu_rowid value. For example, this: +** +** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control) +** VALUES(NULL, 'usa', 12, '.x'); +** +** causes a result similar to: +** +** UPDATE ft1 SET b = 'usa' WHERE rowid = 12; +** +** The data_xxx tables themselves should have no PRIMARY KEY declarations. +** However, RBU is more efficient if reading the rows in from each data_xxx +** table in "rowid" order is roughly the same as reading them sorted by +** the PRIMARY KEY of the corresponding target database table. In other +** words, rows should be sorted using the destination table PRIMARY KEY +** fields before they are inserted into the data_xxx tables. +** +** USAGE +** +** The API declared below allows an application to apply an RBU update +** stored on disk to an existing target database. Essentially, the +** application: +** +** 1) Opens an RBU handle using the sqlite3rbu_open() function. +** +** 2) Registers any required virtual table modules with the database +** handle returned by sqlite3rbu_db(). Also, if required, register +** the rbu_delta() implementation. +** +** 3) Calls the sqlite3rbu_step() function one or more times on +** the new handle. Each call to sqlite3rbu_step() performs a single +** b-tree operation, so thousands of calls may be required to apply +** a complete update. +** +** 4) Calls sqlite3rbu_close() to close the RBU update handle. If +** sqlite3rbu_step() has been called enough times to completely +** apply the update to the target database, then the RBU database +** is marked as fully applied. Otherwise, the state of the RBU +** update application is saved in the RBU database for later +** resumption. +** +** See comments below for more detail on APIs. +** +** If an update is only partially applied to the target database by the +** time sqlite3rbu_close() is called, various state information is saved +** within the RBU database. This allows subsequent processes to automatically +** resume the RBU update from where it left off. +** +** To remove all RBU extension state information, returning an RBU database +** to its original contents, it is sufficient to drop all tables that begin +** with the prefix "rbu_" +** +** DATABASE LOCKING +** +** An RBU update may not be applied to a database in WAL mode. Attempting +** to do so is an error (SQLITE_ERROR). +** +** While an RBU handle is open, a SHARED lock may be held on the target +** database file. This means it is possible for other clients to read the +** database, but not to write it. +** +** If an RBU update is started and then suspended before it is completed, +** then an external client writes to the database, then attempting to resume +** the suspended RBU update is also an error (SQLITE_BUSY). +*/ + +#ifndef _SQLITE3RBU_H +#define _SQLITE3RBU_H + +/* #include "sqlite3.h" ** Required for error code definitions ** */ + +#if 0 +extern "C" { +#endif + +typedef struct sqlite3rbu sqlite3rbu; + +/* +** Open an RBU handle. +** +** Argument zTarget is the path to the target database. Argument zRbu is +** the path to the RBU database. Each call to this function must be matched +** by a call to sqlite3rbu_close(). When opening the databases, RBU passes +** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget +** or zRbu begin with "file:", it will be interpreted as an SQLite +** database URI, not a regular file name. +** +** If the zState argument is passed a NULL value, the RBU extension stores +** the current state of the update (how many rows have been updated, which +** indexes are yet to be updated etc.) within the RBU database itself. This +** can be convenient, as it means that the RBU application does not need to +** organize removing a separate state file after the update is concluded. +** Or, if zState is non-NULL, it must be a path to a database file in which +** the RBU extension can store the state of the update. +** +** When resuming an RBU update, the zState argument must be passed the same +** value as when the RBU update was started. +** +** Once the RBU update is finished, the RBU extension does not +** automatically remove any zState database file, even if it created it. +** +** By default, RBU uses the default VFS to access the files on disk. To +** use a VFS other than the default, an SQLite "file:" URI containing a +** "vfs=..." option may be passed as the zTarget option. +** +** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of +** SQLite's built-in VFSs, including the multiplexor VFS. However it does +** not work out of the box with zipvfs. Refer to the comment describing +** the zipvfs_create_vfs() API below for details on using RBU with zipvfs. +*/ +SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open( + const char *zTarget, + const char *zRbu, + const char *zState +); + +/* +** Open an RBU handle to perform an RBU vacuum on database file zTarget. +** An RBU vacuum is similar to SQLite's built-in VACUUM command, except +** that it can be suspended and resumed like an RBU update. +** +** The second argument to this function, which may not be NULL, identifies +** a database in which to store the state of the RBU vacuum operation if +** it is suspended. The first time sqlite3rbu_vacuum() is called, to start +** an RBU vacuum operation, the state database should either not exist or +** be empty (contain no tables). If an RBU vacuum is suspended by calling +** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has +** returned SQLITE_DONE, the vacuum state is stored in the state database. +** The vacuum can be resumed by calling this function to open a new RBU +** handle specifying the same target and state databases. +** +** This function does not delete the state database after an RBU vacuum +** is completed, even if it created it. However, if the call to +** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents +** of the state tables within the state database are zeroed. This way, +** the next call to sqlite3rbu_vacuum() opens a handle that starts a +** new RBU vacuum operation. +** +** As with sqlite3rbu_open(), Zipvfs users should rever to the comment +** describing the sqlite3rbu_create_vfs() API function below for +** a description of the complications associated with using RBU with +** zipvfs databases. +*/ +SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_vacuum( + const char *zTarget, + const char *zState +); + +/* +** Internally, each RBU connection uses a separate SQLite database +** connection to access the target and rbu update databases. This +** API allows the application direct access to these database handles. +** +** The first argument passed to this function must be a valid, open, RBU +** handle. The second argument should be passed zero to access the target +** database handle, or non-zero to access the rbu update database handle. +** Accessing the underlying database handles may be useful in the +** following scenarios: +** +** * If any target tables are virtual tables, it may be necessary to +** call sqlite3_create_module() on the target database handle to +** register the required virtual table implementations. +** +** * If the data_xxx tables in the RBU source database are virtual +** tables, the application may need to call sqlite3_create_module() on +** the rbu update db handle to any required virtual table +** implementations. +** +** * If the application uses the "rbu_delta()" feature described above, +** it must use sqlite3_create_function() or similar to register the +** rbu_delta() implementation with the target database handle. +** +** If an error has occurred, either while opening or stepping the RBU object, +** this function may return NULL. The error code and message may be collected +** when sqlite3rbu_close() is called. +** +** Database handles returned by this function remain valid until the next +** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db(). +*/ +SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu); + +/* +** Do some work towards applying the RBU update to the target db. +** +** Return SQLITE_DONE if the update has been completely applied, or +** SQLITE_OK if no error occurs but there remains work to do to apply +** the RBU update. If an error does occur, some other error code is +** returned. +** +** Once a call to sqlite3rbu_step() has returned a value other than +** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops +** that immediately return the same value. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu); + +/* +** Force RBU to save its state to disk. +** +** If a power failure or application crash occurs during an update, following +** system recovery RBU may resume the update from the point at which the state +** was last saved. In other words, from the most recent successful call to +** sqlite3rbu_close() or this function. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3rbu_savestate(sqlite3rbu *pRbu); + +/* +** Close an RBU handle. +** +** If the RBU update has been completely applied, mark the RBU database +** as fully applied. Otherwise, assuming no error has occurred, save the +** current state of the RBU update appliation to the RBU database. +** +** If an error has already occurred as part of an sqlite3rbu_step() +** or sqlite3rbu_open() call, or if one occurs within this function, an +** SQLite error code is returned. Additionally, *pzErrmsg may be set to +** point to a buffer containing a utf-8 formatted English language error +** message. It is the responsibility of the caller to eventually free any +** such buffer using sqlite3_free(). +** +** Otherwise, if no error occurs, this function returns SQLITE_OK if the +** update has been partially applied, or SQLITE_DONE if it has been +** completely applied. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg); + +/* +** Return the total number of key-value operations (inserts, deletes or +** updates) that have been performed on the target database since the +** current RBU update was started. +*/ +SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu); + +/* +** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100) +** progress indications for the two stages of an RBU update. This API may +** be useful for driving GUI progress indicators and similar. +** +** An RBU update is divided into two stages: +** +** * Stage 1, in which changes are accumulated in an oal/wal file, and +** * Stage 2, in which the contents of the wal file are copied into the +** main database. +** +** The update is visible to non-RBU clients during stage 2. During stage 1 +** non-RBU reader clients may see the original database. +** +** If this API is called during stage 2 of the update, output variable +** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo) +** to a value between 0 and 10000 to indicate the permyriadage progress of +** stage 2. A value of 5000 indicates that stage 2 is half finished, +** 9000 indicates that it is 90% finished, and so on. +** +** If this API is called during stage 1 of the update, output variable +** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The +** value to which (*pnOne) is set depends on whether or not the RBU +** database contains an "rbu_count" table. The rbu_count table, if it +** exists, must contain the same columns as the following: +** +** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID; +** +** There must be one row in the table for each source (data_xxx) table within +** the RBU database. The 'tbl' column should contain the name of the source +** table. The 'cnt' column should contain the number of rows within the +** source table. +** +** If the rbu_count table is present and populated correctly and this +** API is called during stage 1, the *pnOne output variable is set to the +** permyriadage progress of the same stage. If the rbu_count table does +** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count +** table exists but is not correctly populated, the value of the *pnOne +** output variable during stage 1 is undefined. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int *pnTwo); + +/* +** Create an RBU VFS named zName that accesses the underlying file-system +** via existing VFS zParent. Or, if the zParent parameter is passed NULL, +** then the new RBU VFS uses the default system VFS to access the file-system. +** The new object is registered as a non-default VFS with SQLite before +** returning. +** +** Part of the RBU implementation uses a custom VFS object. Usually, this +** object is created and deleted automatically by RBU. +** +** The exception is for applications that also use zipvfs. In this case, +** the custom VFS must be explicitly created by the user before the RBU +** handle is opened. The RBU VFS should be installed so that the zipvfs +** VFS uses the RBU VFS, which in turn uses any other VFS layers in use +** (for example multiplexor) to access the file-system. For example, +** to assemble an RBU enabled VFS stack that uses both zipvfs and +** multiplexor (error checking omitted): +** +** // Create a VFS named "multiplex" (not the default). +** sqlite3_multiplex_initialize(0, 0); +** +** // Create an rbu VFS named "rbu" that uses multiplexor. If the +** // second argument were replaced with NULL, the "rbu" VFS would +** // access the file-system via the system default VFS, bypassing the +** // multiplexor. +** sqlite3rbu_create_vfs("rbu", "multiplex"); +** +** // Create a zipvfs VFS named "zipvfs" that uses rbu. +** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector); +** +** // Make zipvfs the default VFS. +** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1); +** +** Because the default VFS created above includes a RBU functionality, it +** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack +** that does not include the RBU layer results in an error. +** +** The overhead of adding the "rbu" VFS to the system is negligible for +** non-RBU users. There is no harm in an application accessing the +** file-system via "rbu" all the time, even if it only uses RBU functionality +** occasionally. +*/ +SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent); + +/* +** Deregister and destroy an RBU vfs created by an earlier call to +** sqlite3rbu_create_vfs(). +** +** VFS objects are not reference counted. If a VFS object is destroyed +** before all database handles that use it have been closed, the results +** are undefined. +*/ +SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName); + +#if 0 +} /* end of the 'extern "C"' block */ +#endif + +#endif /* _SQLITE3RBU_H */ + +/************** End of sqlite3rbu.h ******************************************/ +/************** Continuing where we left off in sqlite3rbu.c *****************/ + +#if defined(_WIN32_WCE) +/* #include "windows.h" */ +#endif + +/* Maximum number of prepared UPDATE statements held by this module */ +#define SQLITE_RBU_UPDATE_CACHESIZE 16 + +/* +** Swap two objects of type TYPE. +*/ +#if !defined(SQLITE_AMALGAMATION) +# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} +#endif + +/* +** The rbu_state table is used to save the state of a partially applied +** update so that it can be resumed later. The table consists of integer +** keys mapped to values as follows: +** +** RBU_STATE_STAGE: +** May be set to integer values 1, 2, 4 or 5. As follows: +** 1: the *-rbu file is currently under construction. +** 2: the *-rbu file has been constructed, but not yet moved +** to the *-wal path. +** 4: the checkpoint is underway. +** 5: the rbu update has been checkpointed. +** +** RBU_STATE_TBL: +** Only valid if STAGE==1. The target database name of the table +** currently being written. +** +** RBU_STATE_IDX: +** Only valid if STAGE==1. The target database name of the index +** currently being written, or NULL if the main table is currently being +** updated. +** +** RBU_STATE_ROW: +** Only valid if STAGE==1. Number of rows already processed for the current +** table/index. +** +** RBU_STATE_PROGRESS: +** Trbul number of sqlite3rbu_step() calls made so far as part of this +** rbu update. +** +** RBU_STATE_CKPT: +** Valid if STAGE==4. The 64-bit checksum associated with the wal-index +** header created by recovering the *-wal file. This is used to detect +** cases when another client appends frames to the *-wal file in the +** middle of an incremental checkpoint (an incremental checkpoint cannot +** be continued if this happens). +** +** RBU_STATE_COOKIE: +** Valid if STAGE==1. The current change-counter cookie value in the +** target db file. +** +** RBU_STATE_OALSZ: +** Valid if STAGE==1. The size in bytes of the *-oal file. +*/ +#define RBU_STATE_STAGE 1 +#define RBU_STATE_TBL 2 +#define RBU_STATE_IDX 3 +#define RBU_STATE_ROW 4 +#define RBU_STATE_PROGRESS 5 +#define RBU_STATE_CKPT 6 +#define RBU_STATE_COOKIE 7 +#define RBU_STATE_OALSZ 8 +#define RBU_STATE_PHASEONESTEP 9 + +#define RBU_STAGE_OAL 1 +#define RBU_STAGE_MOVE 2 +#define RBU_STAGE_CAPTURE 3 +#define RBU_STAGE_CKPT 4 +#define RBU_STAGE_DONE 5 + + +#define RBU_CREATE_STATE \ + "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)" + +typedef struct RbuFrame RbuFrame; +typedef struct RbuObjIter RbuObjIter; +typedef struct RbuState RbuState; +typedef struct rbu_vfs rbu_vfs; +typedef struct rbu_file rbu_file; +typedef struct RbuUpdateStmt RbuUpdateStmt; + +#if !defined(SQLITE_AMALGAMATION) +typedef unsigned int u32; +typedef unsigned short u16; +typedef unsigned char u8; +typedef sqlite3_int64 i64; +#endif + +/* +** These values must match the values defined in wal.c for the equivalent +** locks. These are not magic numbers as they are part of the SQLite file +** format. +*/ +#define WAL_LOCK_WRITE 0 +#define WAL_LOCK_CKPT 1 +#define WAL_LOCK_READ0 3 + +#define SQLITE_FCNTL_RBUCNT 5149216 + +/* +** A structure to store values read from the rbu_state table in memory. +*/ +struct RbuState { + int eStage; + char *zTbl; + char *zIdx; + i64 iWalCksum; + int nRow; + i64 nProgress; + u32 iCookie; + i64 iOalSz; + i64 nPhaseOneStep; +}; + +struct RbuUpdateStmt { + char *zMask; /* Copy of update mask used with pUpdate */ + sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */ + RbuUpdateStmt *pNext; +}; + +/* +** An iterator of this type is used to iterate through all objects in +** the target database that require updating. For each such table, the +** iterator visits, in order: +** +** * the table itself, +** * each index of the table (zero or more points to visit), and +** * a special "cleanup table" state. +** +** abIndexed: +** If the table has no indexes on it, abIndexed is set to NULL. Otherwise, +** it points to an array of flags nTblCol elements in size. The flag is +** set for each column that is either a part of the PK or a part of an +** index. Or clear otherwise. +** +*/ +struct RbuObjIter { + sqlite3_stmt *pTblIter; /* Iterate through tables */ + sqlite3_stmt *pIdxIter; /* Index iterator */ + int nTblCol; /* Size of azTblCol[] array */ + char **azTblCol; /* Array of unquoted target column names */ + char **azTblType; /* Array of target column types */ + int *aiSrcOrder; /* src table col -> target table col */ + u8 *abTblPk; /* Array of flags, set on target PK columns */ + u8 *abNotNull; /* Array of flags, set on NOT NULL columns */ + u8 *abIndexed; /* Array of flags, set on indexed & PK cols */ + int eType; /* Table type - an RBU_PK_XXX value */ + + /* Output variables. zTbl==0 implies EOF. */ + int bCleanup; /* True in "cleanup" state */ + const char *zTbl; /* Name of target db table */ + const char *zDataTbl; /* Name of rbu db table (or null) */ + const char *zIdx; /* Name of target db index (or null) */ + int iTnum; /* Root page of current object */ + int iPkTnum; /* If eType==EXTERNAL, root of PK index */ + int bUnique; /* Current index is unique */ + int nIndex; /* Number of aux. indexes on table zTbl */ + + /* Statements created by rbuObjIterPrepareAll() */ + int nCol; /* Number of columns in current object */ + sqlite3_stmt *pSelect; /* Source data */ + sqlite3_stmt *pInsert; /* Statement for INSERT operations */ + sqlite3_stmt *pDelete; /* Statement for DELETE ops */ + sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */ + + /* Last UPDATE used (for PK b-tree updates only), or NULL. */ + RbuUpdateStmt *pRbuUpdate; +}; + +/* +** Values for RbuObjIter.eType +** +** 0: Table does not exist (error) +** 1: Table has an implicit rowid. +** 2: Table has an explicit IPK column. +** 3: Table has an external PK index. +** 4: Table is WITHOUT ROWID. +** 5: Table is a virtual table. +*/ +#define RBU_PK_NOTABLE 0 +#define RBU_PK_NONE 1 +#define RBU_PK_IPK 2 +#define RBU_PK_EXTERNAL 3 +#define RBU_PK_WITHOUT_ROWID 4 +#define RBU_PK_VTAB 5 + + +/* +** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs +** one of the following operations. +*/ +#define RBU_INSERT 1 /* Insert on a main table b-tree */ +#define RBU_DELETE 2 /* Delete a row from a main table b-tree */ +#define RBU_REPLACE 3 /* Delete and then insert a row */ +#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */ +#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */ + +#define RBU_UPDATE 6 /* Update a row in a main table b-tree */ + +/* +** A single step of an incremental checkpoint - frame iWalFrame of the wal +** file should be copied to page iDbPage of the database file. +*/ +struct RbuFrame { + u32 iDbPage; + u32 iWalFrame; +}; + +/* +** RBU handle. +** +** nPhaseOneStep: +** If the RBU database contains an rbu_count table, this value is set to +** a running estimate of the number of b-tree operations required to +** finish populating the *-oal file. This allows the sqlite3_bp_progress() +** API to calculate the permyriadage progress of populating the *-oal file +** using the formula: +** +** permyriadage = (10000 * nProgress) / nPhaseOneStep +** +** nPhaseOneStep is initialized to the sum of: +** +** nRow * (nIndex + 1) +** +** for all source tables in the RBU database, where nRow is the number +** of rows in the source table and nIndex the number of indexes on the +** corresponding target database table. +** +** This estimate is accurate if the RBU update consists entirely of +** INSERT operations. However, it is inaccurate if: +** +** * the RBU update contains any UPDATE operations. If the PK specified +** for an UPDATE operation does not exist in the target table, then +** no b-tree operations are required on index b-trees. Or if the +** specified PK does exist, then (nIndex*2) such operations are +** required (one delete and one insert on each index b-tree). +** +** * the RBU update contains any DELETE operations for which the specified +** PK does not exist. In this case no operations are required on index +** b-trees. +** +** * the RBU update contains REPLACE operations. These are similar to +** UPDATE operations. +** +** nPhaseOneStep is updated to account for the conditions above during the +** first pass of each source table. The updated nPhaseOneStep value is +** stored in the rbu_state table if the RBU update is suspended. +*/ +struct sqlite3rbu { + int eStage; /* Value of RBU_STATE_STAGE field */ + sqlite3 *dbMain; /* target database handle */ + sqlite3 *dbRbu; /* rbu database handle */ + char *zTarget; /* Path to target db */ + char *zRbu; /* Path to rbu db */ + char *zState; /* Path to state db (or NULL if zRbu) */ + char zStateDb[5]; /* Db name for state ("stat" or "main") */ + int rc; /* Value returned by last rbu_step() call */ + char *zErrmsg; /* Error message if rc!=SQLITE_OK */ + int nStep; /* Rows processed for current object */ + int nProgress; /* Rows processed for all objects */ + RbuObjIter objiter; /* Iterator for skipping through tbl/idx */ + const char *zVfsName; /* Name of automatically created rbu vfs */ + rbu_file *pTargetFd; /* File handle open on target db */ + i64 iOalSz; + i64 nPhaseOneStep; + + /* The following state variables are used as part of the incremental + ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding + ** function rbuSetupCheckpoint() for details. */ + u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */ + u32 mLock; + int nFrame; /* Entries in aFrame[] array */ + int nFrameAlloc; /* Allocated size of aFrame[] array */ + RbuFrame *aFrame; + int pgsz; + u8 *aBuf; + i64 iWalCksum; + + /* Used in RBU vacuum mode only */ + int nRbu; /* Number of RBU VFS in the stack */ + rbu_file *pRbuFd; /* Fd for main db of dbRbu */ +}; + +/* +** An rbu VFS is implemented using an instance of this structure. +*/ +struct rbu_vfs { + sqlite3_vfs base; /* rbu VFS shim methods */ + sqlite3_vfs *pRealVfs; /* Underlying VFS */ + sqlite3_mutex *mutex; /* Mutex to protect pMain */ + rbu_file *pMain; /* Linked list of main db files */ +}; + +/* +** Each file opened by an rbu VFS is represented by an instance of +** the following structure. +*/ +struct rbu_file { + sqlite3_file base; /* sqlite3_file methods */ + sqlite3_file *pReal; /* Underlying file handle */ + rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */ + sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */ + + int openFlags; /* Flags this file was opened with */ + u32 iCookie; /* Cookie value for main db files */ + u8 iWriteVer; /* "write-version" value for main db files */ + u8 bNolock; /* True to fail EXCLUSIVE locks */ + + int nShm; /* Number of entries in apShm[] array */ + char **apShm; /* Array of mmap'd *-shm regions */ + char *zDel; /* Delete this when closing file */ + + const char *zWal; /* Wal filename for this main db file */ + rbu_file *pWalFd; /* Wal file descriptor for this main db */ + rbu_file *pMainNext; /* Next MAIN_DB file */ +}; + +/* +** True for an RBU vacuum handle, or false otherwise. +*/ +#define rbuIsVacuum(p) ((p)->zTarget==0) + + +/************************************************************************* +** The following three functions, found below: +** +** rbuDeltaGetInt() +** rbuDeltaChecksum() +** rbuDeltaApply() +** +** are lifted from the fossil source code (http://fossil-scm.org). They +** are used to implement the scalar SQL function rbu_fossil_delta(). +*/ + +/* +** Read bytes from *pz and convert them into a positive integer. When +** finished, leave *pz pointing to the first character past the end of +** the integer. The *pLen parameter holds the length of the string +** in *pz and is decremented once for each character in the integer. +*/ +static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){ + static const signed char zValue[] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, + -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36, + -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1, + }; + unsigned int v = 0; + int c; + unsigned char *z = (unsigned char*)*pz; + unsigned char *zStart = z; + while( (c = zValue[0x7f&*(z++)])>=0 ){ + v = (v<<6) + c; + } + z--; + *pLen -= z - zStart; + *pz = (char*)z; + return v; +} + +/* +** Compute a 32-bit checksum on the N-byte buffer. Return the result. +*/ +static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){ + const unsigned char *z = (const unsigned char *)zIn; + unsigned sum0 = 0; + unsigned sum1 = 0; + unsigned sum2 = 0; + unsigned sum3 = 0; + while(N >= 16){ + sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]); + sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]); + sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]); + sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]); + z += 16; + N -= 16; + } + while(N >= 4){ + sum0 += z[0]; + sum1 += z[1]; + sum2 += z[2]; + sum3 += z[3]; + z += 4; + N -= 4; + } + sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24); + switch(N){ + case 3: sum3 += (z[2] << 8); + case 2: sum3 += (z[1] << 16); + case 1: sum3 += (z[0] << 24); + default: ; + } + return sum3; +} + +/* +** Apply a delta. +** +** The output buffer should be big enough to hold the whole output +** file and a NUL terminator at the end. The delta_output_size() +** routine will determine this size for you. +** +** The delta string should be null-terminated. But the delta string +** may contain embedded NUL characters (if the input and output are +** binary files) so we also have to pass in the length of the delta in +** the lenDelta parameter. +** +** This function returns the size of the output file in bytes (excluding +** the final NUL terminator character). Except, if the delta string is +** malformed or intended for use with a source file other than zSrc, +** then this routine returns -1. +** +** Refer to the delta_create() documentation above for a description +** of the delta file format. +*/ +static int rbuDeltaApply( + const char *zSrc, /* The source or pattern file */ + int lenSrc, /* Length of the source file */ + const char *zDelta, /* Delta to apply to the pattern */ + int lenDelta, /* Length of the delta */ + char *zOut /* Write the output into this preallocated buffer */ +){ + unsigned int limit; + unsigned int total = 0; +#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST + char *zOrigOut = zOut; +#endif + + limit = rbuDeltaGetInt(&zDelta, &lenDelta); + if( *zDelta!='\n' ){ + /* ERROR: size integer not terminated by "\n" */ + return -1; + } + zDelta++; lenDelta--; + while( *zDelta && lenDelta>0 ){ + unsigned int cnt, ofst; + cnt = rbuDeltaGetInt(&zDelta, &lenDelta); + switch( zDelta[0] ){ + case '@': { + zDelta++; lenDelta--; + ofst = rbuDeltaGetInt(&zDelta, &lenDelta); + if( lenDelta>0 && zDelta[0]!=',' ){ + /* ERROR: copy command not terminated by ',' */ + return -1; + } + zDelta++; lenDelta--; + total += cnt; + if( total>limit ){ + /* ERROR: copy exceeds output file size */ + return -1; + } + if( (int)(ofst+cnt) > lenSrc ){ + /* ERROR: copy extends past end of input */ + return -1; + } + memcpy(zOut, &zSrc[ofst], cnt); + zOut += cnt; + break; + } + case ':': { + zDelta++; lenDelta--; + total += cnt; + if( total>limit ){ + /* ERROR: insert command gives an output larger than predicted */ + return -1; + } + if( (int)cnt>lenDelta ){ + /* ERROR: insert count exceeds size of delta */ + return -1; + } + memcpy(zOut, zDelta, cnt); + zOut += cnt; + zDelta += cnt; + lenDelta -= cnt; + break; + } + case ';': { + zDelta++; lenDelta--; + zOut[0] = 0; +#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST + if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){ + /* ERROR: bad checksum */ + return -1; + } +#endif + if( total!=limit ){ + /* ERROR: generated size does not match predicted size */ + return -1; + } + return total; + } + default: { + /* ERROR: unknown delta operator */ + return -1; + } + } + } + /* ERROR: unterminated delta */ + return -1; +} + +static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){ + int size; + size = rbuDeltaGetInt(&zDelta, &lenDelta); + if( *zDelta!='\n' ){ + /* ERROR: size integer not terminated by "\n" */ + return -1; + } + return size; +} + +/* +** End of code taken from fossil. +*************************************************************************/ + +/* +** Implementation of SQL scalar function rbu_fossil_delta(). +** +** This function applies a fossil delta patch to a blob. Exactly two +** arguments must be passed to this function. The first is the blob to +** patch and the second the patch to apply. If no error occurs, this +** function returns the patched blob. +*/ +static void rbuFossilDeltaFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *aDelta; + int nDelta; + const char *aOrig; + int nOrig; + + int nOut; + int nOut2; + char *aOut; + + assert( argc==2 ); + + nOrig = sqlite3_value_bytes(argv[0]); + aOrig = (const char*)sqlite3_value_blob(argv[0]); + nDelta = sqlite3_value_bytes(argv[1]); + aDelta = (const char*)sqlite3_value_blob(argv[1]); + + /* Figure out the size of the output */ + nOut = rbuDeltaOutputSize(aDelta, nDelta); + if( nOut<0 ){ + sqlite3_result_error(context, "corrupt fossil delta", -1); + return; + } + + aOut = sqlite3_malloc(nOut+1); + if( aOut==0 ){ + sqlite3_result_error_nomem(context); + }else{ + nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut); + if( nOut2!=nOut ){ + sqlite3_result_error(context, "corrupt fossil delta", -1); + }else{ + sqlite3_result_blob(context, aOut, nOut, sqlite3_free); + } + } +} + + +/* +** Prepare the SQL statement in buffer zSql against database handle db. +** If successful, set *ppStmt to point to the new statement and return +** SQLITE_OK. +** +** Otherwise, if an error does occur, set *ppStmt to NULL and return +** an SQLite error code. Additionally, set output variable *pzErrmsg to +** point to a buffer containing an error message. It is the responsibility +** of the caller to (eventually) free this buffer using sqlite3_free(). +*/ +static int prepareAndCollectError( + sqlite3 *db, + sqlite3_stmt **ppStmt, + char **pzErrmsg, + const char *zSql +){ + int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); + if( rc!=SQLITE_OK ){ + *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + *ppStmt = 0; + } + return rc; +} + +/* +** Reset the SQL statement passed as the first argument. Return a copy +** of the value returned by sqlite3_reset(). +** +** If an error has occurred, then set *pzErrmsg to point to a buffer +** containing an error message. It is the responsibility of the caller +** to eventually free this buffer using sqlite3_free(). +*/ +static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){ + int rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ){ + *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt))); + } + return rc; +} + +/* +** Unless it is NULL, argument zSql points to a buffer allocated using +** sqlite3_malloc containing an SQL statement. This function prepares the SQL +** statement against database db and frees the buffer. If statement +** compilation is successful, *ppStmt is set to point to the new statement +** handle and SQLITE_OK is returned. +** +** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code +** returned. In this case, *pzErrmsg may also be set to point to an error +** message. It is the responsibility of the caller to free this error message +** buffer using sqlite3_free(). +** +** If argument zSql is NULL, this function assumes that an OOM has occurred. +** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL. +*/ +static int prepareFreeAndCollectError( + sqlite3 *db, + sqlite3_stmt **ppStmt, + char **pzErrmsg, + char *zSql +){ + int rc; + assert( *pzErrmsg==0 ); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + *ppStmt = 0; + }else{ + rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql); + sqlite3_free(zSql); + } + return rc; +} + +/* +** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated +** by an earlier call to rbuObjIterCacheTableInfo(). +*/ +static void rbuObjIterFreeCols(RbuObjIter *pIter){ + int i; + for(i=0; inTblCol; i++){ + sqlite3_free(pIter->azTblCol[i]); + sqlite3_free(pIter->azTblType[i]); + } + sqlite3_free(pIter->azTblCol); + pIter->azTblCol = 0; + pIter->azTblType = 0; + pIter->aiSrcOrder = 0; + pIter->abTblPk = 0; + pIter->abNotNull = 0; + pIter->nTblCol = 0; + pIter->eType = 0; /* Invalid value */ +} + +/* +** Finalize all statements and free all allocations that are specific to +** the current object (table/index pair). +*/ +static void rbuObjIterClearStatements(RbuObjIter *pIter){ + RbuUpdateStmt *pUp; + + sqlite3_finalize(pIter->pSelect); + sqlite3_finalize(pIter->pInsert); + sqlite3_finalize(pIter->pDelete); + sqlite3_finalize(pIter->pTmpInsert); + pUp = pIter->pRbuUpdate; + while( pUp ){ + RbuUpdateStmt *pTmp = pUp->pNext; + sqlite3_finalize(pUp->pUpdate); + sqlite3_free(pUp); + pUp = pTmp; + } + + pIter->pSelect = 0; + pIter->pInsert = 0; + pIter->pDelete = 0; + pIter->pRbuUpdate = 0; + pIter->pTmpInsert = 0; + pIter->nCol = 0; +} + +/* +** Clean up any resources allocated as part of the iterator object passed +** as the only argument. +*/ +static void rbuObjIterFinalize(RbuObjIter *pIter){ + rbuObjIterClearStatements(pIter); + sqlite3_finalize(pIter->pTblIter); + sqlite3_finalize(pIter->pIdxIter); + rbuObjIterFreeCols(pIter); + memset(pIter, 0, sizeof(RbuObjIter)); +} + +/* +** Advance the iterator to the next position. +** +** If no error occurs, SQLITE_OK is returned and the iterator is left +** pointing to the next entry. Otherwise, an error code and message is +** left in the RBU handle passed as the first argument. A copy of the +** error code is returned. +*/ +static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){ + int rc = p->rc; + if( rc==SQLITE_OK ){ + + /* Free any SQLite statements used while processing the previous object */ + rbuObjIterClearStatements(pIter); + if( pIter->zIdx==0 ){ + rc = sqlite3_exec(p->dbMain, + "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;" + "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;" + "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;" + "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;" + , 0, 0, &p->zErrmsg + ); + } + + if( rc==SQLITE_OK ){ + if( pIter->bCleanup ){ + rbuObjIterFreeCols(pIter); + pIter->bCleanup = 0; + rc = sqlite3_step(pIter->pTblIter); + if( rc!=SQLITE_ROW ){ + rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg); + pIter->zTbl = 0; + }else{ + pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0); + pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1); + rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM; + } + }else{ + if( pIter->zIdx==0 ){ + sqlite3_stmt *pIdx = pIter->pIdxIter; + rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_step(pIter->pIdxIter); + if( rc!=SQLITE_ROW ){ + rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg); + pIter->bCleanup = 1; + pIter->zIdx = 0; + }else{ + pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0); + pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1); + pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2); + rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM; + } + } + } + } + } + + if( rc!=SQLITE_OK ){ + rbuObjIterFinalize(pIter); + p->rc = rc; + } + return rc; +} + + +/* +** The implementation of the rbu_target_name() SQL function. This function +** accepts one or two arguments. The first argument is the name of a table - +** the name of a table in the RBU database. The second, if it is present, is 1 +** for a view or 0 for a table. +** +** For a non-vacuum RBU handle, if the table name matches the pattern: +** +** data[0-9]_ +** +** where is any sequence of 1 or more characters, is returned. +** Otherwise, if the only argument does not match the above pattern, an SQL +** NULL is returned. +** +** "data_t1" -> "t1" +** "data0123_t2" -> "t2" +** "dataAB_t3" -> NULL +** +** For an rbu vacuum handle, a copy of the first argument is returned if +** the second argument is either missing or 0 (not a view). +*/ +static void rbuTargetNameFunc( + sqlite3_context *pCtx, + int argc, + sqlite3_value **argv +){ + sqlite3rbu *p = sqlite3_user_data(pCtx); + const char *zIn; + assert( argc==1 || argc==2 ); + + zIn = (const char*)sqlite3_value_text(argv[0]); + if( zIn ){ + if( rbuIsVacuum(p) ){ + if( argc==1 || 0==sqlite3_value_int(argv[1]) ){ + sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC); + } + }else{ + if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){ + int i; + for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++); + if( zIn[i]=='_' && zIn[i+1] ){ + sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC); + } + } + } + } +} + +/* +** Initialize the iterator structure passed as the second argument. +** +** If no error occurs, SQLITE_OK is returned and the iterator is left +** pointing to the first entry. Otherwise, an error code and message is +** left in the RBU handle passed as the first argument. A copy of the +** error code is returned. +*/ +static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){ + int rc; + memset(pIter, 0, sizeof(RbuObjIter)); + + rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg, + "SELECT rbu_target_name(name, type='view') AS target, name " + "FROM sqlite_master " + "WHERE type IN ('table', 'view') AND target IS NOT NULL " + "ORDER BY name" + ); + + if( rc==SQLITE_OK ){ + rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg, + "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' " + " FROM main.sqlite_master " + " WHERE type='index' AND tbl_name = ?" + ); + } + + pIter->bCleanup = 1; + p->rc = rc; + return rbuObjIterNext(p, pIter); +} + +/* +** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs, +** an error code is stored in the RBU handle passed as the first argument. +** +** If an error has already occurred (p->rc is already set to something other +** than SQLITE_OK), then this function returns NULL without modifying the +** stored error code. In this case it still calls sqlite3_free() on any +** printf() parameters associated with %z conversions. +*/ +static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){ + char *zSql = 0; + va_list ap; + va_start(ap, zFmt); + zSql = sqlite3_vmprintf(zFmt, ap); + if( p->rc==SQLITE_OK ){ + if( zSql==0 ) p->rc = SQLITE_NOMEM; + }else{ + sqlite3_free(zSql); + zSql = 0; + } + va_end(ap); + return zSql; +} + +/* +** Argument zFmt is a sqlite3_mprintf() style format string. The trailing +** arguments are the usual subsitution values. This function performs +** the printf() style substitutions and executes the result as an SQL +** statement on the RBU handles database. +** +** If an error occurs, an error code and error message is stored in the +** RBU handle. If an error has already occurred when this function is +** called, it is a no-op. +*/ +static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){ + va_list ap; + char *zSql; + va_start(ap, zFmt); + zSql = sqlite3_vmprintf(zFmt, ap); + if( p->rc==SQLITE_OK ){ + if( zSql==0 ){ + p->rc = SQLITE_NOMEM; + }else{ + p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg); + } + } + sqlite3_free(zSql); + va_end(ap); + return p->rc; +} + +/* +** Attempt to allocate and return a pointer to a zeroed block of nByte +** bytes. +** +** If an error (i.e. an OOM condition) occurs, return NULL and leave an +** error code in the rbu handle passed as the first argument. Or, if an +** error has already occurred when this function is called, return NULL +** immediately without attempting the allocation or modifying the stored +** error code. +*/ +static void *rbuMalloc(sqlite3rbu *p, int nByte){ + void *pRet = 0; + if( p->rc==SQLITE_OK ){ + assert( nByte>0 ); + pRet = sqlite3_malloc64(nByte); + if( pRet==0 ){ + p->rc = SQLITE_NOMEM; + }else{ + memset(pRet, 0, nByte); + } + } + return pRet; +} + + +/* +** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that +** there is room for at least nCol elements. If an OOM occurs, store an +** error code in the RBU handle passed as the first argument. +*/ +static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){ + int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol; + char **azNew; + + azNew = (char**)rbuMalloc(p, nByte); + if( azNew ){ + pIter->azTblCol = azNew; + pIter->azTblType = &azNew[nCol]; + pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol]; + pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol]; + pIter->abNotNull = (u8*)&pIter->abTblPk[nCol]; + pIter->abIndexed = (u8*)&pIter->abNotNull[nCol]; + } +} + +/* +** The first argument must be a nul-terminated string. This function +** returns a copy of the string in memory obtained from sqlite3_malloc(). +** It is the responsibility of the caller to eventually free this memory +** using sqlite3_free(). +** +** If an OOM condition is encountered when attempting to allocate memory, +** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise, +** if the allocation succeeds, (*pRc) is left unchanged. +*/ +static char *rbuStrndup(const char *zStr, int *pRc){ + char *zRet = 0; + + assert( *pRc==SQLITE_OK ); + if( zStr ){ + size_t nCopy = strlen(zStr) + 1; + zRet = (char*)sqlite3_malloc64(nCopy); + if( zRet ){ + memcpy(zRet, zStr, nCopy); + }else{ + *pRc = SQLITE_NOMEM; + } + } + + return zRet; +} + +/* +** Finalize the statement passed as the second argument. +** +** If the sqlite3_finalize() call indicates that an error occurs, and the +** rbu handle error code is not already set, set the error code and error +** message accordingly. +*/ +static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){ + sqlite3 *db = sqlite3_db_handle(pStmt); + int rc = sqlite3_finalize(pStmt); + if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){ + p->rc = rc; + p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } +} + +/* Determine the type of a table. +** +** peType is of type (int*), a pointer to an output parameter of type +** (int). This call sets the output parameter as follows, depending +** on the type of the table specified by parameters dbName and zTbl. +** +** RBU_PK_NOTABLE: No such table. +** RBU_PK_NONE: Table has an implicit rowid. +** RBU_PK_IPK: Table has an explicit IPK column. +** RBU_PK_EXTERNAL: Table has an external PK index. +** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID. +** RBU_PK_VTAB: Table is a virtual table. +** +** Argument *piPk is also of type (int*), and also points to an output +** parameter. Unless the table has an external primary key index +** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or, +** if the table does have an external primary key index, then *piPk +** is set to the root page number of the primary key index before +** returning. +** +** ALGORITHM: +** +** if( no entry exists in sqlite_master ){ +** return RBU_PK_NOTABLE +** }else if( sql for the entry starts with "CREATE VIRTUAL" ){ +** return RBU_PK_VTAB +** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){ +** if( the index that is the pk exists in sqlite_master ){ +** *piPK = rootpage of that index. +** return RBU_PK_EXTERNAL +** }else{ +** return RBU_PK_WITHOUT_ROWID +** } +** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){ +** return RBU_PK_IPK +** }else{ +** return RBU_PK_NONE +** } +*/ +static void rbuTableType( + sqlite3rbu *p, + const char *zTab, + int *peType, + int *piTnum, + int *piPk +){ + /* + ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q) + ** 1) PRAGMA index_list = ? + ** 2) SELECT count(*) FROM sqlite_master where name=%Q + ** 3) PRAGMA table_info = ? + */ + sqlite3_stmt *aStmt[4] = {0, 0, 0, 0}; + + *peType = RBU_PK_NOTABLE; + *piPk = 0; + + assert( p->rc==SQLITE_OK ); + p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg, + sqlite3_mprintf( + "SELECT (sql LIKE 'create virtual%%'), rootpage" + " FROM sqlite_master" + " WHERE name=%Q", zTab + )); + if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){ + /* Either an error, or no such table. */ + goto rbuTableType_end; + } + if( sqlite3_column_int(aStmt[0], 0) ){ + *peType = RBU_PK_VTAB; /* virtual table */ + goto rbuTableType_end; + } + *piTnum = sqlite3_column_int(aStmt[0], 1); + + p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg, + sqlite3_mprintf("PRAGMA index_list=%Q",zTab) + ); + if( p->rc ) goto rbuTableType_end; + while( sqlite3_step(aStmt[1])==SQLITE_ROW ){ + const u8 *zOrig = sqlite3_column_text(aStmt[1], 3); + const u8 *zIdx = sqlite3_column_text(aStmt[1], 1); + if( zOrig && zIdx && zOrig[0]=='p' ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg, + sqlite3_mprintf( + "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx + )); + if( p->rc==SQLITE_OK ){ + if( sqlite3_step(aStmt[2])==SQLITE_ROW ){ + *piPk = sqlite3_column_int(aStmt[2], 0); + *peType = RBU_PK_EXTERNAL; + }else{ + *peType = RBU_PK_WITHOUT_ROWID; + } + } + goto rbuTableType_end; + } + } + + p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg, + sqlite3_mprintf("PRAGMA table_info=%Q",zTab) + ); + if( p->rc==SQLITE_OK ){ + while( sqlite3_step(aStmt[3])==SQLITE_ROW ){ + if( sqlite3_column_int(aStmt[3],5)>0 ){ + *peType = RBU_PK_IPK; /* explicit IPK column */ + goto rbuTableType_end; + } + } + *peType = RBU_PK_NONE; + } + +rbuTableType_end: { + unsigned int i; + for(i=0; iabIndexed[] array. +*/ +static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){ + sqlite3_stmt *pList = 0; + int bIndex = 0; + + if( p->rc==SQLITE_OK ){ + memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol); + p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl) + ); + } + + pIter->nIndex = 0; + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){ + const char *zIdx = (const char*)sqlite3_column_text(pList, 1); + sqlite3_stmt *pXInfo = 0; + if( zIdx==0 ) break; + p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx) + ); + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){ + int iCid = sqlite3_column_int(pXInfo, 1); + if( iCid>=0 ) pIter->abIndexed[iCid] = 1; + } + rbuFinalize(p, pXInfo); + bIndex = 1; + pIter->nIndex++; + } + + if( pIter->eType==RBU_PK_WITHOUT_ROWID ){ + /* "PRAGMA index_list" includes the main PK b-tree */ + pIter->nIndex--; + } + + rbuFinalize(p, pList); + if( bIndex==0 ) pIter->abIndexed = 0; +} + + +/* +** If they are not already populated, populate the pIter->azTblCol[], +** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to +** the table (not index) that the iterator currently points to. +** +** Return SQLITE_OK if successful, or an SQLite error code otherwise. If +** an error does occur, an error code and error message are also left in +** the RBU handle. +*/ +static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){ + if( pIter->azTblCol==0 ){ + sqlite3_stmt *pStmt = 0; + int nCol = 0; + int i; /* for() loop iterator variable */ + int bRbuRowid = 0; /* If input table has column "rbu_rowid" */ + int iOrder = 0; + int iTnum = 0; + + /* Figure out the type of table this step will deal with. */ + assert( pIter->eType==0 ); + rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum); + if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl); + } + if( p->rc ) return p->rc; + if( pIter->zIdx==0 ) pIter->iTnum = iTnum; + + assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK + || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID + || pIter->eType==RBU_PK_VTAB + ); + + /* Populate the azTblCol[] and nTblCol variables based on the columns + ** of the input table. Ignore any input table columns that begin with + ** "rbu_". */ + p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg, + sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl) + ); + if( p->rc==SQLITE_OK ){ + nCol = sqlite3_column_count(pStmt); + rbuAllocateIterArrays(p, pIter, nCol); + } + for(i=0; p->rc==SQLITE_OK && irc); + pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol; + pIter->azTblCol[pIter->nTblCol++] = zCopy; + } + else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){ + bRbuRowid = 1; + } + } + sqlite3_finalize(pStmt); + pStmt = 0; + + if( p->rc==SQLITE_OK + && rbuIsVacuum(p)==0 + && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE) + ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf( + "table %q %s rbu_rowid column", pIter->zDataTbl, + (bRbuRowid ? "may not have" : "requires") + ); + } + + /* Check that all non-HIDDEN columns in the destination table are also + ** present in the input table. Populate the abTblPk[], azTblType[] and + ** aiTblOrder[] arrays at the same time. */ + if( p->rc==SQLITE_OK ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg, + sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl) + ); + } + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + const char *zName = (const char*)sqlite3_column_text(pStmt, 1); + if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */ + for(i=iOrder; inTblCol; i++){ + if( 0==strcmp(zName, pIter->azTblCol[i]) ) break; + } + if( i==pIter->nTblCol ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("column missing from %q: %s", + pIter->zDataTbl, zName + ); + }else{ + int iPk = sqlite3_column_int(pStmt, 5); + int bNotNull = sqlite3_column_int(pStmt, 3); + const char *zType = (const char*)sqlite3_column_text(pStmt, 2); + + if( i!=iOrder ){ + SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]); + SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]); + } + + pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc); + pIter->abTblPk[iOrder] = (iPk!=0); + pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0); + iOrder++; + } + } + + rbuFinalize(p, pStmt); + rbuObjIterCacheIndexedCols(p, pIter); + assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 ); + assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 ); + } + + return p->rc; +} + +/* +** This function constructs and returns a pointer to a nul-terminated +** string containing some SQL clause or list based on one or more of the +** column names currently stored in the pIter->azTblCol[] array. +*/ +static char *rbuObjIterGetCollist( + sqlite3rbu *p, /* RBU object */ + RbuObjIter *pIter /* Object iterator for column names */ +){ + char *zList = 0; + const char *zSep = ""; + int i; + for(i=0; inTblCol; i++){ + const char *z = pIter->azTblCol[i]; + zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z); + zSep = ", "; + } + return zList; +} + +/* +** This function is used to create a SELECT list (the list of SQL +** expressions that follows a SELECT keyword) for a SELECT statement +** used to read from an data_xxx or rbu_tmp_xxx table while updating the +** index object currently indicated by the iterator object passed as the +** second argument. A "PRAGMA index_xinfo = " statement is used +** to obtain the required information. +** +** If the index is of the following form: +** +** CREATE INDEX i1 ON t1(c, b COLLATE nocase); +** +** and "t1" is a table with an explicit INTEGER PRIMARY KEY column +** "ipk", the returned string is: +** +** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'" +** +** As well as the returned string, three other malloc'd strings are +** returned via output parameters. As follows: +** +** pzImposterCols: ... +** pzImposterPk: ... +** pzWhere: ... +*/ +static char *rbuObjIterGetIndexCols( + sqlite3rbu *p, /* RBU object */ + RbuObjIter *pIter, /* Object iterator for column names */ + char **pzImposterCols, /* OUT: Columns for imposter table */ + char **pzImposterPk, /* OUT: Imposter PK clause */ + char **pzWhere, /* OUT: WHERE clause */ + int *pnBind /* OUT: Trbul number of columns */ +){ + int rc = p->rc; /* Error code */ + int rc2; /* sqlite3_finalize() return code */ + char *zRet = 0; /* String to return */ + char *zImpCols = 0; /* String to return via *pzImposterCols */ + char *zImpPK = 0; /* String to return via *pzImposterPK */ + char *zWhere = 0; /* String to return via *pzWhere */ + int nBind = 0; /* Value to return via *pnBind */ + const char *zCom = ""; /* Set to ", " later on */ + const char *zAnd = ""; /* Set to " AND " later on */ + sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */ + + if( rc==SQLITE_OK ){ + assert( p->zErrmsg==0 ); + rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx) + ); + } + + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){ + int iCid = sqlite3_column_int(pXInfo, 1); + int bDesc = sqlite3_column_int(pXInfo, 3); + const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4); + const char *zCol; + const char *zType; + + if( iCid<0 ){ + /* An integer primary key. If the table has an explicit IPK, use + ** its name. Otherwise, use "rbu_rowid". */ + if( pIter->eType==RBU_PK_IPK ){ + int i; + for(i=0; pIter->abTblPk[i]==0; i++); + assert( inTblCol ); + zCol = pIter->azTblCol[i]; + }else if( rbuIsVacuum(p) ){ + zCol = "_rowid_"; + }else{ + zCol = "rbu_rowid"; + } + zType = "INTEGER"; + }else{ + zCol = pIter->azTblCol[iCid]; + zType = pIter->azTblType[iCid]; + } + + zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate); + if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){ + const char *zOrder = (bDesc ? " DESC" : ""); + zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s", + zImpPK, zCom, nBind, zCol, zOrder + ); + } + zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q", + zImpCols, zCom, nBind, zCol, zType, zCollate + ); + zWhere = sqlite3_mprintf( + "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol + ); + if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM; + zCom = ", "; + zAnd = " AND "; + nBind++; + } + + rc2 = sqlite3_finalize(pXInfo); + if( rc==SQLITE_OK ) rc = rc2; + + if( rc!=SQLITE_OK ){ + sqlite3_free(zRet); + sqlite3_free(zImpCols); + sqlite3_free(zImpPK); + sqlite3_free(zWhere); + zRet = 0; + zImpCols = 0; + zImpPK = 0; + zWhere = 0; + p->rc = rc; + } + + *pzImposterCols = zImpCols; + *pzImposterPk = zImpPK; + *pzWhere = zWhere; + *pnBind = nBind; + return zRet; +} + +/* +** Assuming the current table columns are "a", "b" and "c", and the zObj +** paramter is passed "old", return a string of the form: +** +** "old.a, old.b, old.b" +** +** With the column names escaped. +** +** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append +** the text ", old._rowid_" to the returned value. +*/ +static char *rbuObjIterGetOldlist( + sqlite3rbu *p, + RbuObjIter *pIter, + const char *zObj +){ + char *zList = 0; + if( p->rc==SQLITE_OK && pIter->abIndexed ){ + const char *zS = ""; + int i; + for(i=0; inTblCol; i++){ + if( pIter->abIndexed[i] ){ + const char *zCol = pIter->azTblCol[i]; + zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol); + }else{ + zList = sqlite3_mprintf("%z%sNULL", zList, zS); + } + zS = ", "; + if( zList==0 ){ + p->rc = SQLITE_NOMEM; + break; + } + } + + /* For a table with implicit rowids, append "old._rowid_" to the list. */ + if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ + zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj); + } + } + return zList; +} + +/* +** Return an expression that can be used in a WHERE clause to match the +** primary key of the current table. For example, if the table is: +** +** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)); +** +** Return the string: +** +** "b = ?1 AND c = ?2" +*/ +static char *rbuObjIterGetWhere( + sqlite3rbu *p, + RbuObjIter *pIter +){ + char *zList = 0; + if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){ + zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1); + }else if( pIter->eType==RBU_PK_EXTERNAL ){ + const char *zSep = ""; + int i; + for(i=0; inTblCol; i++){ + if( pIter->abTblPk[i] ){ + zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1); + zSep = " AND "; + } + } + zList = rbuMPrintf(p, + "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList + ); + + }else{ + const char *zSep = ""; + int i; + for(i=0; inTblCol; i++){ + if( pIter->abTblPk[i] ){ + const char *zCol = pIter->azTblCol[i]; + zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1); + zSep = " AND "; + } + } + } + return zList; +} + +/* +** The SELECT statement iterating through the keys for the current object +** (p->objiter.pSelect) currently points to a valid row. However, there +** is something wrong with the rbu_control value in the rbu_control value +** stored in the (p->nCol+1)'th column. Set the error code and error message +** of the RBU handle to something reflecting this. +*/ +static void rbuBadControlError(sqlite3rbu *p){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("invalid rbu_control value"); +} + + +/* +** Return a nul-terminated string containing the comma separated list of +** assignments that should be included following the "SET" keyword of +** an UPDATE statement used to update the table object that the iterator +** passed as the second argument currently points to if the rbu_control +** column of the data_xxx table entry is set to zMask. +** +** The memory for the returned string is obtained from sqlite3_malloc(). +** It is the responsibility of the caller to eventually free it using +** sqlite3_free(). +** +** If an OOM error is encountered when allocating space for the new +** string, an error code is left in the rbu handle passed as the first +** argument and NULL is returned. Or, if an error has already occurred +** when this function is called, NULL is returned immediately, without +** attempting the allocation or modifying the stored error code. +*/ +static char *rbuObjIterGetSetlist( + sqlite3rbu *p, + RbuObjIter *pIter, + const char *zMask +){ + char *zList = 0; + if( p->rc==SQLITE_OK ){ + int i; + + if( (int)strlen(zMask)!=pIter->nTblCol ){ + rbuBadControlError(p); + }else{ + const char *zSep = ""; + for(i=0; inTblCol; i++){ + char c = zMask[pIter->aiSrcOrder[i]]; + if( c=='x' ){ + zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", + zList, zSep, pIter->azTblCol[i], i+1 + ); + zSep = ", "; + } + else if( c=='d' ){ + zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)", + zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1 + ); + zSep = ", "; + } + else if( c=='f' ){ + zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)", + zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1 + ); + zSep = ", "; + } + } + } + } + return zList; +} + +/* +** Return a nul-terminated string consisting of nByte comma separated +** "?" expressions. For example, if nByte is 3, return a pointer to +** a buffer containing the string "?,?,?". +** +** The memory for the returned string is obtained from sqlite3_malloc(). +** It is the responsibility of the caller to eventually free it using +** sqlite3_free(). +** +** If an OOM error is encountered when allocating space for the new +** string, an error code is left in the rbu handle passed as the first +** argument and NULL is returned. Or, if an error has already occurred +** when this function is called, NULL is returned immediately, without +** attempting the allocation or modifying the stored error code. +*/ +static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){ + char *zRet = 0; + int nByte = nBind*2 + 1; + + zRet = (char*)rbuMalloc(p, nByte); + if( zRet ){ + int i; + for(i=0; izIdx==0 ); + if( p->rc==SQLITE_OK ){ + const char *zSep = "PRIMARY KEY("; + sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */ + sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = */ + + p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl) + ); + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){ + const char *zOrig = (const char*)sqlite3_column_text(pXList,3); + if( zOrig && strcmp(zOrig, "pk")==0 ){ + const char *zIdx = (const char*)sqlite3_column_text(pXList,1); + if( zIdx ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx) + ); + } + break; + } + } + rbuFinalize(p, pXList); + + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){ + if( sqlite3_column_int(pXInfo, 5) ){ + /* int iCid = sqlite3_column_int(pXInfo, 0); */ + const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2); + const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : ""; + z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc); + zSep = ", "; + } + } + z = rbuMPrintf(p, "%z)", z); + rbuFinalize(p, pXInfo); + } + return z; +} + +/* +** This function creates the second imposter table used when writing to +** a table b-tree where the table has an external primary key. If the +** iterator passed as the second argument does not currently point to +** a table (not index) with an external primary key, this function is a +** no-op. +** +** Assuming the iterator does point to a table with an external PK, this +** function creates a WITHOUT ROWID imposter table named "rbu_imposter2" +** used to access that PK index. For example, if the target table is +** declared as follows: +** +** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c)); +** +** then the imposter table schema is: +** +** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID; +** +*/ +static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){ + if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){ + int tnum = pIter->iPkTnum; /* Root page of PK index */ + sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */ + const char *zIdx = 0; /* Name of PK index */ + sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */ + const char *zComma = ""; + char *zCols = 0; /* Used to build up list of table cols */ + char *zPk = 0; /* Used to build up table PK declaration */ + + /* Figure out the name of the primary key index for the current table. + ** This is needed for the argument to "PRAGMA index_xinfo". Set + ** zIdx to point to a nul-terminated string containing this name. */ + p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg, + "SELECT name FROM sqlite_master WHERE rootpage = ?" + ); + if( p->rc==SQLITE_OK ){ + sqlite3_bind_int(pQuery, 1, tnum); + if( SQLITE_ROW==sqlite3_step(pQuery) ){ + zIdx = (const char*)sqlite3_column_text(pQuery, 0); + } + } + if( zIdx ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg, + sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx) + ); + } + rbuFinalize(p, pQuery); + + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){ + int bKey = sqlite3_column_int(pXInfo, 5); + if( bKey ){ + int iCid = sqlite3_column_int(pXInfo, 1); + int bDesc = sqlite3_column_int(pXInfo, 3); + const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4); + zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma, + iCid, pIter->azTblType[iCid], zCollate + ); + zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":""); + zComma = ", "; + } + } + zCols = rbuMPrintf(p, "%z, id INTEGER", zCols); + rbuFinalize(p, pXInfo); + + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum); + rbuMPrintfExec(p, p->dbMain, + "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID", + zCols, zPk + ); + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); + } +} + +/* +** If an error has already occurred when this function is called, it +** immediately returns zero (without doing any work). Or, if an error +** occurs during the execution of this function, it sets the error code +** in the sqlite3rbu object indicated by the first argument and returns +** zero. +** +** The iterator passed as the second argument is guaranteed to point to +** a table (not an index) when this function is called. This function +** attempts to create any imposter table required to write to the main +** table b-tree of the table before returning. Non-zero is returned if +** an imposter table are created, or zero otherwise. +** +** An imposter table is required in all cases except RBU_PK_VTAB. Only +** virtual tables are written to directly. The imposter table has the +** same schema as the actual target table (less any UNIQUE constraints). +** More precisely, the "same schema" means the same columns, types, +** collation sequences. For tables that do not have an external PRIMARY +** KEY, it also means the same PRIMARY KEY declaration. +*/ +static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){ + if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){ + int tnum = pIter->iTnum; + const char *zComma = ""; + char *zSql = 0; + int iCol; + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1); + + for(iCol=0; p->rc==SQLITE_OK && iColnTblCol; iCol++){ + const char *zPk = ""; + const char *zCol = pIter->azTblCol[iCol]; + const char *zColl = 0; + + p->rc = sqlite3_table_column_metadata( + p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0 + ); + + if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){ + /* If the target table column is an "INTEGER PRIMARY KEY", add + ** "PRIMARY KEY" to the imposter table column declaration. */ + zPk = "PRIMARY KEY "; + } + zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s", + zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl, + (pIter->abNotNull[iCol] ? " NOT NULL" : "") + ); + zComma = ", "; + } + + if( pIter->eType==RBU_PK_WITHOUT_ROWID ){ + char *zPk = rbuWithoutRowidPK(p, pIter); + if( zPk ){ + zSql = rbuMPrintf(p, "%z, %z", zSql, zPk); + } + } + + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum); + rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s", + pIter->zTbl, zSql, + (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "") + ); + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); + } +} + +/* +** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table. +** Specifically a statement of the form: +** +** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...); +** +** The number of bound variables is equal to the number of columns in +** the target table, plus one (for the rbu_control column), plus one more +** (for the rbu_rowid column) if the target table is an implicit IPK or +** virtual table. +*/ +static void rbuObjIterPrepareTmpInsert( + sqlite3rbu *p, + RbuObjIter *pIter, + const char *zCollist, + const char *zRbuRowid +){ + int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE); + char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid); + if( zBind ){ + assert( pIter->pTmpInsert==0 ); + p->rc = prepareFreeAndCollectError( + p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf( + "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)", + p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind + )); + } +} + +static void rbuTmpInsertFunc( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + sqlite3rbu *p = sqlite3_user_data(pCtx); + int rc = SQLITE_OK; + int i; + + assert( sqlite3_value_int(apVal[0])!=0 + || p->objiter.eType==RBU_PK_EXTERNAL + || p->objiter.eType==RBU_PK_NONE + ); + if( sqlite3_value_int(apVal[0])!=0 ){ + p->nPhaseOneStep += p->objiter.nIndex; + } + + for(i=0; rc==SQLITE_OK && iobjiter.pTmpInsert, i+1, apVal[i]); + } + if( rc==SQLITE_OK ){ + sqlite3_step(p->objiter.pTmpInsert); + rc = sqlite3_reset(p->objiter.pTmpInsert); + } + + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pCtx, rc); + } +} + +/* +** Ensure that the SQLite statement handles required to update the +** target database object currently indicated by the iterator passed +** as the second argument are available. +*/ +static int rbuObjIterPrepareAll( + sqlite3rbu *p, + RbuObjIter *pIter, + int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */ +){ + assert( pIter->bCleanup==0 ); + if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){ + const int tnum = pIter->iTnum; + char *zCollist = 0; /* List of indexed columns */ + char **pz = &p->zErrmsg; + const char *zIdx = pIter->zIdx; + char *zLimit = 0; + + if( nOffset ){ + zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset); + if( !zLimit ) p->rc = SQLITE_NOMEM; + } + + if( zIdx ){ + const char *zTbl = pIter->zTbl; + char *zImposterCols = 0; /* Columns for imposter table */ + char *zImposterPK = 0; /* Primary key declaration for imposter */ + char *zWhere = 0; /* WHERE clause on PK columns */ + char *zBind = 0; + int nBind = 0; + + assert( pIter->eType!=RBU_PK_VTAB ); + zCollist = rbuObjIterGetIndexCols( + p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind + ); + zBind = rbuObjIterGetBindlist(p, nBind); + + /* Create the imposter table used to write to this index. */ + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1); + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum); + rbuMPrintfExec(p, p->dbMain, + "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID", + zTbl, zImposterCols, zImposterPK + ); + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); + + /* Create the statement to insert index entries */ + pIter->nCol = nBind; + if( p->rc==SQLITE_OK ){ + p->rc = prepareFreeAndCollectError( + p->dbMain, &pIter->pInsert, &p->zErrmsg, + sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind) + ); + } + + /* And to delete index entries */ + if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){ + p->rc = prepareFreeAndCollectError( + p->dbMain, &pIter->pDelete, &p->zErrmsg, + sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere) + ); + } + + /* Create the SELECT statement to read keys in sorted order */ + if( p->rc==SQLITE_OK ){ + char *zSql; + if( rbuIsVacuum(p) ){ + zSql = sqlite3_mprintf( + "SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s", + zCollist, + pIter->zDataTbl, + zCollist, zLimit + ); + }else + + if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ + zSql = sqlite3_mprintf( + "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s", + zCollist, p->zStateDb, pIter->zDataTbl, + zCollist, zLimit + ); + }else{ + zSql = sqlite3_mprintf( + "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' " + "UNION ALL " + "SELECT %s, rbu_control FROM '%q' " + "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 " + "ORDER BY %s%s", + zCollist, p->zStateDb, pIter->zDataTbl, + zCollist, pIter->zDataTbl, + zCollist, zLimit + ); + } + p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql); + } + + sqlite3_free(zImposterCols); + sqlite3_free(zImposterPK); + sqlite3_free(zWhere); + sqlite3_free(zBind); + }else{ + int bRbuRowid = (pIter->eType==RBU_PK_VTAB) + ||(pIter->eType==RBU_PK_NONE) + ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p)); + const char *zTbl = pIter->zTbl; /* Table this step applies to */ + const char *zWrite; /* Imposter table name */ + + char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid); + char *zWhere = rbuObjIterGetWhere(p, pIter); + char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old"); + char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new"); + + zCollist = rbuObjIterGetCollist(p, pIter); + pIter->nCol = pIter->nTblCol; + + /* Create the imposter table or tables (if required). */ + rbuCreateImposterTable(p, pIter); + rbuCreateImposterTable2(p, pIter); + zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_"); + + /* Create the INSERT statement to write to the target PK b-tree */ + if( p->rc==SQLITE_OK ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz, + sqlite3_mprintf( + "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)", + zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings + ) + ); + } + + /* Create the DELETE statement to write to the target PK b-tree. + ** Because it only performs INSERT operations, this is not required for + ** an rbu vacuum handle. */ + if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){ + p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz, + sqlite3_mprintf( + "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere + ) + ); + } + + if( rbuIsVacuum(p)==0 && pIter->abIndexed ){ + const char *zRbuRowid = ""; + if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ + zRbuRowid = ", rbu_rowid"; + } + + /* Create the rbu_tmp_xxx table and the triggers to populate it. */ + rbuMPrintfExec(p, p->dbRbu, + "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS " + "SELECT *%s FROM '%q' WHERE 0;" + , p->zStateDb, pIter->zDataTbl + , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "") + , pIter->zDataTbl + ); + + rbuMPrintfExec(p, p->dbMain, + "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" " + "BEGIN " + " SELECT rbu_tmp_insert(3, %s);" + "END;" + + "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" " + "BEGIN " + " SELECT rbu_tmp_insert(3, %s);" + "END;" + + "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" " + "BEGIN " + " SELECT rbu_tmp_insert(4, %s);" + "END;", + zWrite, zTbl, zOldlist, + zWrite, zTbl, zOldlist, + zWrite, zTbl, zNewlist + ); + + if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ + rbuMPrintfExec(p, p->dbMain, + "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" " + "BEGIN " + " SELECT rbu_tmp_insert(0, %s);" + "END;", + zWrite, zTbl, zNewlist + ); + } + + rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid); + } + + /* Create the SELECT statement to read keys from data_xxx */ + if( p->rc==SQLITE_OK ){ + const char *zRbuRowid = ""; + if( bRbuRowid ){ + zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid"; + } + p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, + sqlite3_mprintf( + "SELECT %s,%s rbu_control%s FROM '%q'%s", + zCollist, + (rbuIsVacuum(p) ? "0 AS " : ""), + zRbuRowid, + pIter->zDataTbl, zLimit + ) + ); + } + + sqlite3_free(zWhere); + sqlite3_free(zOldlist); + sqlite3_free(zNewlist); + sqlite3_free(zBindings); + } + sqlite3_free(zCollist); + sqlite3_free(zLimit); + } + + return p->rc; +} + +/* +** Set output variable *ppStmt to point to an UPDATE statement that may +** be used to update the imposter table for the main table b-tree of the +** table object that pIter currently points to, assuming that the +** rbu_control column of the data_xyz table contains zMask. +** +** If the zMask string does not specify any columns to update, then this +** is not an error. Output variable *ppStmt is set to NULL in this case. +*/ +static int rbuGetUpdateStmt( + sqlite3rbu *p, /* RBU handle */ + RbuObjIter *pIter, /* Object iterator */ + const char *zMask, /* rbu_control value ('x.x.') */ + sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */ +){ + RbuUpdateStmt **pp; + RbuUpdateStmt *pUp = 0; + int nUp = 0; + + /* In case an error occurs */ + *ppStmt = 0; + + /* Search for an existing statement. If one is found, shift it to the front + ** of the LRU queue and return immediately. Otherwise, leave nUp pointing + ** to the number of statements currently in the cache and pUp to the + ** last object in the list. */ + for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){ + pUp = *pp; + if( strcmp(pUp->zMask, zMask)==0 ){ + *pp = pUp->pNext; + pUp->pNext = pIter->pRbuUpdate; + pIter->pRbuUpdate = pUp; + *ppStmt = pUp->pUpdate; + return SQLITE_OK; + } + nUp++; + } + assert( pUp==0 || pUp->pNext==0 ); + + if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){ + for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext)); + *pp = 0; + sqlite3_finalize(pUp->pUpdate); + pUp->pUpdate = 0; + }else{ + pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1); + } + + if( pUp ){ + char *zWhere = rbuObjIterGetWhere(p, pIter); + char *zSet = rbuObjIterGetSetlist(p, pIter, zMask); + char *zUpdate = 0; + + pUp->zMask = (char*)&pUp[1]; + memcpy(pUp->zMask, zMask, pIter->nTblCol); + pUp->pNext = pIter->pRbuUpdate; + pIter->pRbuUpdate = pUp; + + if( zSet ){ + const char *zPrefix = ""; + + if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_"; + zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s", + zPrefix, pIter->zTbl, zSet, zWhere + ); + p->rc = prepareFreeAndCollectError( + p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate + ); + *ppStmt = pUp->pUpdate; + } + sqlite3_free(zWhere); + sqlite3_free(zSet); + } + + return p->rc; +} + +static sqlite3 *rbuOpenDbhandle( + sqlite3rbu *p, + const char *zName, + int bUseVfs +){ + sqlite3 *db = 0; + if( p->rc==SQLITE_OK ){ + const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI; + p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0); + if( p->rc ){ + p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + sqlite3_close(db); + db = 0; + } + } + return db; +} + +/* +** Free an RbuState object allocated by rbuLoadState(). +*/ +static void rbuFreeState(RbuState *p){ + if( p ){ + sqlite3_free(p->zTbl); + sqlite3_free(p->zIdx); + sqlite3_free(p); + } +} + +/* +** Allocate an RbuState object and load the contents of the rbu_state +** table into it. Return a pointer to the new object. It is the +** responsibility of the caller to eventually free the object using +** sqlite3_free(). +** +** If an error occurs, leave an error code and message in the rbu handle +** and return NULL. +*/ +static RbuState *rbuLoadState(sqlite3rbu *p){ + RbuState *pRet = 0; + sqlite3_stmt *pStmt = 0; + int rc; + int rc2; + + pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState)); + if( pRet==0 ) return 0; + + rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg, + sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb) + ); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + switch( sqlite3_column_int(pStmt, 0) ){ + case RBU_STATE_STAGE: + pRet->eStage = sqlite3_column_int(pStmt, 1); + if( pRet->eStage!=RBU_STAGE_OAL + && pRet->eStage!=RBU_STAGE_MOVE + && pRet->eStage!=RBU_STAGE_CKPT + ){ + p->rc = SQLITE_CORRUPT; + } + break; + + case RBU_STATE_TBL: + pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc); + break; + + case RBU_STATE_IDX: + pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc); + break; + + case RBU_STATE_ROW: + pRet->nRow = sqlite3_column_int(pStmt, 1); + break; + + case RBU_STATE_PROGRESS: + pRet->nProgress = sqlite3_column_int64(pStmt, 1); + break; + + case RBU_STATE_CKPT: + pRet->iWalCksum = sqlite3_column_int64(pStmt, 1); + break; + + case RBU_STATE_COOKIE: + pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1); + break; + + case RBU_STATE_OALSZ: + pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1); + break; + + case RBU_STATE_PHASEONESTEP: + pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1); + break; + + default: + rc = SQLITE_CORRUPT; + break; + } + } + rc2 = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ) rc = rc2; + + p->rc = rc; + return pRet; +} + + +/* +** Open the database handle and attach the RBU database as "rbu". If an +** error occurs, leave an error code and message in the RBU handle. +*/ +static void rbuOpenDatabase(sqlite3rbu *p){ + assert( p->rc==SQLITE_OK ); + assert( p->dbMain==0 && p->dbRbu==0 ); + assert( rbuIsVacuum(p) || p->zTarget!=0 ); + + /* Open the RBU database */ + p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1); + + if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){ + sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p); + } + + /* If using separate RBU and state databases, attach the state database to + ** the RBU db handle now. */ + if( p->zState ){ + rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState); + memcpy(p->zStateDb, "stat", 4); + }else{ + memcpy(p->zStateDb, "main", 4); + } + +#if 0 + if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){ + p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0); + } +#endif + + /* If it has not already been created, create the rbu_state table */ + rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb); + +#if 0 + if( rbuIsVacuum(p) ){ + if( p->rc==SQLITE_OK ){ + int rc2; + int bOk = 0; + sqlite3_stmt *pCnt = 0; + p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg, + "SELECT count(*) FROM stat.sqlite_master" + ); + if( p->rc==SQLITE_OK + && sqlite3_step(pCnt)==SQLITE_ROW + && 1==sqlite3_column_int(pCnt, 0) + ){ + bOk = 1; + } + rc2 = sqlite3_finalize(pCnt); + if( p->rc==SQLITE_OK ) p->rc = rc2; + + if( p->rc==SQLITE_OK && bOk==0 ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("invalid state database"); + } + + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0); + } + } + } +#endif + + if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){ + int bOpen = 0; + int rc; + p->nRbu = 0; + p->pRbuFd = 0; + rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p); + if( rc!=SQLITE_NOTFOUND ) p->rc = rc; + if( p->eStage>=RBU_STAGE_MOVE ){ + bOpen = 1; + }else{ + RbuState *pState = rbuLoadState(p); + if( pState ){ + bOpen = (pState->eStage>RBU_STAGE_MOVE); + rbuFreeState(pState); + } + } + if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1); + } + + p->eStage = 0; + if( p->rc==SQLITE_OK && p->dbMain==0 ){ + if( !rbuIsVacuum(p) ){ + p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1); + }else if( p->pRbuFd->pWalFd ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database"); + }else{ + char *zTarget; + char *zExtra = 0; + if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){ + zExtra = &p->zRbu[5]; + while( *zExtra ){ + if( *zExtra++=='?' ) break; + } + if( *zExtra=='\0' ) zExtra = 0; + } + + zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s", + sqlite3_db_filename(p->dbRbu, "main"), + (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra) + ); + + if( zTarget==0 ){ + p->rc = SQLITE_NOMEM; + return; + } + p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1); + sqlite3_free(zTarget); + } + } + + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_create_function(p->dbMain, + "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0 + ); + } + + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_create_function(p->dbMain, + "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0 + ); + } + + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_create_function(p->dbRbu, + "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0 + ); + } + + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p); + } + rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master"); + + /* Mark the database file just opened as an RBU target database. If + ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use. + ** This is an error. */ + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p); + } + + if( p->rc==SQLITE_NOTFOUND ){ + p->rc = SQLITE_ERROR; + p->zErrmsg = sqlite3_mprintf("rbu vfs not found"); + } +} + +/* +** This routine is a copy of the sqlite3FileSuffix3() routine from the core. +** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined. +** +** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database +** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and +** if filename in z[] has a suffix (a.k.a. "extension") that is longer than +** three characters, then shorten the suffix on z[] to be the last three +** characters of the original suffix. +** +** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always +** do the suffix shortening regardless of URI parameter. +** +** Examples: +** +** test.db-journal => test.nal +** test.db-wal => test.wal +** test.db-shm => test.shm +** test.db-mj7f3319fa => test.9fa +*/ +static void rbuFileSuffix3(const char *zBase, char *z){ +#ifdef SQLITE_ENABLE_8_3_NAMES +#if SQLITE_ENABLE_8_3_NAMES<2 + if( sqlite3_uri_boolean(zBase, "8_3_names", 0) ) +#endif + { + int i, sz; + sz = sqlite3Strlen30(z); + for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} + if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); + } +#endif +} + +/* +** Return the current wal-index header checksum for the target database +** as a 64-bit integer. +** +** The checksum is store in the first page of xShmMap memory as an 8-byte +** blob starting at byte offset 40. +*/ +static i64 rbuShmChecksum(sqlite3rbu *p){ + i64 iRet = 0; + if( p->rc==SQLITE_OK ){ + sqlite3_file *pDb = p->pTargetFd->pReal; + u32 volatile *ptr; + p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr); + if( p->rc==SQLITE_OK ){ + iRet = ((i64)ptr[10] << 32) + ptr[11]; + } + } + return iRet; +} + +/* +** This function is called as part of initializing or reinitializing an +** incremental checkpoint. +** +** It populates the sqlite3rbu.aFrame[] array with the set of +** (wal frame -> db page) copy operations required to checkpoint the +** current wal file, and obtains the set of shm locks required to safely +** perform the copy operations directly on the file-system. +** +** If argument pState is not NULL, then the incremental checkpoint is +** being resumed. In this case, if the checksum of the wal-index-header +** following recovery is not the same as the checksum saved in the RbuState +** object, then the rbu handle is set to DONE state. This occurs if some +** other client appends a transaction to the wal file in the middle of +** an incremental checkpoint. +*/ +static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){ + + /* If pState is NULL, then the wal file may not have been opened and + ** recovered. Running a read-statement here to ensure that doing so + ** does not interfere with the "capture" process below. */ + if( pState==0 ){ + p->eStage = 0; + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0); + } + } + + /* Assuming no error has occurred, run a "restart" checkpoint with the + ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following + ** special behaviour in the rbu VFS: + ** + ** * If the exclusive shm WRITER or READ0 lock cannot be obtained, + ** the checkpoint fails with SQLITE_BUSY (normally SQLite would + ** proceed with running a passive checkpoint instead of failing). + ** + ** * Attempts to read from the *-wal file or write to the database file + ** do not perform any IO. Instead, the frame/page combinations that + ** would be read/written are recorded in the sqlite3rbu.aFrame[] + ** array. + ** + ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER, + ** READ0 and CHECKPOINT locks taken as part of the checkpoint are + ** no-ops. These locks will not be released until the connection + ** is closed. + ** + ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL + ** error. + ** + ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the + ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[] + ** array populated with a set of (frame -> page) mappings. Because the + ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy + ** data from the wal file into the database file according to the + ** contents of aFrame[]. + */ + if( p->rc==SQLITE_OK ){ + int rc2; + p->eStage = RBU_STAGE_CAPTURE; + rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0); + if( rc2!=SQLITE_INTERNAL ) p->rc = rc2; + } + + if( p->rc==SQLITE_OK ){ + p->eStage = RBU_STAGE_CKPT; + p->nStep = (pState ? pState->nRow : 0); + p->aBuf = rbuMalloc(p, p->pgsz); + p->iWalCksum = rbuShmChecksum(p); + } + + if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){ + p->rc = SQLITE_DONE; + p->eStage = RBU_STAGE_DONE; + } +} + +/* +** Called when iAmt bytes are read from offset iOff of the wal file while +** the rbu object is in capture mode. Record the frame number of the frame +** being read in the aFrame[] array. +*/ +static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){ + const u32 mReq = (1<mLock!=mReq ){ + pRbu->rc = SQLITE_BUSY; + return SQLITE_INTERNAL; + } + + pRbu->pgsz = iAmt; + if( pRbu->nFrame==pRbu->nFrameAlloc ){ + int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2; + RbuFrame *aNew; + aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame)); + if( aNew==0 ) return SQLITE_NOMEM; + pRbu->aFrame = aNew; + pRbu->nFrameAlloc = nNew; + } + + iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1; + if( pRbu->iMaxFrame